9os

Experimental kernel using plan9 ideas for embedded device
git clone git://git.simple-cc.org/9os
Log | Files | Refs | README | LICENSE

devroot.c (2338B)


      1 #include <os9/os9.h>
      2 #include <libk.h>
      3 
      4 #include "dev.h"
      5 
      6 enum Orootqid {
      7 	Qroot,
      8 	Qdev,
      9 	Qdevuart0,
     10 	Qdevuart1,
     11 	Qdevuart2,
     12 	Qdevuart3,
     13 	Qdevcons,
     14 	Qdevblk,
     15 	Qarfs,
     16 	Qfip,
     17 	Qproc,
     18 	Qblobs,
     19 	Qblobctl,
     20 };
     21 
     22 #include "blobfil.h"
     23 #include "blobs.h"
     24 
     25 static Dirtab dirtab[] = {
     26 	{"dev",  QID(CHDIR, 0, Qdev), 0, O_READ},
     27 	{"blobs", QID(CHDIR, 0, Qblobs), 0, O_READ},
     28 	{"arfs", QID(CHDIR, 0, Qarfs), 0, O_READ},
     29 	{"fip", QID(CHDIR, 0, Qfip), 0, O_READ},
     30 	{"proc", QID(CHDIR, 0, Qproc), 0, O_READ},
     31 };
     32 
     33 static Dirtab devfstab[] = {
     34 	{"uart0", QID(CHDIR, 0, Qdevuart0), 0, O_READ},
     35 	{"uart1", QID(CHDIR, 0, Qdevuart1), 0, O_READ},
     36 	{"uart2", QID(CHDIR, 0, Qdevuart2), 0, O_READ},
     37 	{"uart3", QID(CHDIR, 0, Qdevuart3), 0, O_READ},
     38 	{"cons", QID(CHDIR, 0, Qdevcons), 0, O_READ},
     39 	{"blk", QID(CHDIR, 0, Qdevblk), 0, O_READ},
     40 };
     41 
     42 static int
     43 rootgen(Chan *c, Dirtab *tab, int ntab, int n, Dir *dir)
     44 {
     45 	switch (c->qid.path) {
     46 	case Qroot:
     47 		tab = dirtab;
     48 		ntab = NELEM(dirtab);
     49 		break;
     50 	case Qdev:
     51 		tab = devfstab;
     52 		ntab = NELEM(devfstab);
     53 		break;
     54 	case Qblobs:
     55 		tab = blobtab;
     56 		ntab = NELEM(blobtab);
     57 		break;
     58 	default:
     59 		return -1;
     60 	}
     61 
     62 	return devgen(c, tab, ntab, n, dir);
     63 }
     64 
     65 static int
     66 rootwalk(Chan *c, char *name)
     67 {
     68 	return devwalk(c, name, NULL, 0, rootgen);
     69 }
     70 
     71 static int
     72 blobstatus(Chan *c, void *buf, int n)
     73 {
     74 	int i, len;
     75 	Dirtab *dp;
     76 	char info[NELEM(blobtab) * (NAMELEN + 20)];
     77 
     78 	len = 0;
     79 	for (i = 1; i < NELEM(blobtab); i++) {
     80 		dp = &blobtab[i];
     81 		len += ksnprint(info+len,
     82 				   sizeof(info) - len,
     83 				   "%s 0x%p %lu\n",
     84 				   dp->name,
     85 				   dp->data,
     86 				   dp->length);
     87 	}
     88 	if (len == sizeof(info))
     89 		panic("blobstatus");
     90 
     91 	return buf2chan(c, buf, info, n, len);
     92 }
     93 
     94 static int
     95 rootread(Chan *c, void *buf, int n)
     96 {
     97 	Dirtab *dp;
     98 
     99 	if (c->qid.type == CHDIR)
    100 		return dirread(c, buf, n, NULL, 0, rootgen);
    101 
    102 	if (c->qid.path == Qblobctl)
    103 		return blobstatus(c, buf, n);
    104 
    105 	dp = &blobtab[c->qid.path - Qblobctl];
    106 	return buf2chan(c, buf, dp->data, n, dp->length);
    107 }
    108 
    109 static int
    110 rootstat(Chan *c, char *file, unsigned char *buf, int n)
    111 {
    112 	return devstat(c, file, buf, n, NULL, 0, rootgen);
    113 }
    114 
    115 Dev rootdevtab = {
    116 	.id = '/',
    117 	.stat = rootstat,
    118 	.clone = devclone,
    119 	.attach = devattach,
    120 	.walk = rootwalk,
    121 	.read = rootread,
    122 	.write = deverrwrite,
    123 	.mount = deverrmount,
    124 	.seek = devseek,
    125 	.sync = devsync,
    126 	.close = devclose,
    127 };