devsd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Storage Device.
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "io.h"
  10. #include "ureg.h"
  11. #include "../port/error.h"
  12. #include "../port/sd.h"
  13. extern Dev sddevtab;
  14. extern SDifc* sdifc[];
  15. typedef struct SDevgrp {
  16. SDev* dev;
  17. int nunits; /* num units in dev */
  18. } SDevgrp;
  19. static SDevgrp* devs; /* all devices */
  20. static QLock devslock; /* insertion and removal of devices */
  21. static int ndevs; /* total number of devices in the system */
  22. enum {
  23. Rawcmd,
  24. Rawdata,
  25. Rawstatus,
  26. };
  27. enum {
  28. Qtopdir = 1, /* top level directory */
  29. Qtopbase,
  30. Qtopctl = Qtopbase,
  31. Qtopstat,
  32. Qunitdir, /* directory per unit */
  33. Qunitbase,
  34. Qctl = Qunitbase,
  35. Qraw,
  36. Qpart,
  37. TypeLOG = 4,
  38. NType = (1<<TypeLOG),
  39. TypeMASK = (NType-1),
  40. TypeSHIFT = 0,
  41. PartLOG = 8,
  42. NPart = (1<<PartLOG),
  43. PartMASK = (NPart-1),
  44. PartSHIFT = TypeLOG,
  45. UnitLOG = 8,
  46. NUnit = (1<<UnitLOG),
  47. UnitMASK = (NUnit-1),
  48. UnitSHIFT = (PartLOG+TypeLOG),
  49. DevLOG = 8,
  50. NDev = (1 << DevLOG),
  51. DevMASK = (NDev-1),
  52. DevSHIFT = (UnitLOG+PartLOG+TypeLOG),
  53. Ncmd = 20,
  54. };
  55. #define TYPE(q) ((((ulong)(q).path)>>TypeSHIFT) & TypeMASK)
  56. #define PART(q) ((((ulong)(q).path)>>PartSHIFT) & PartMASK)
  57. #define UNIT(q) ((((ulong)(q).path)>>UnitSHIFT) & UnitMASK)
  58. #define DEV(q) ((((ulong)(q).path)>>DevSHIFT) & DevMASK)
  59. #define QID(d,u, p, t) (((d)<<DevSHIFT)|((u)<<UnitSHIFT)|\
  60. ((p)<<PartSHIFT)|((t)<<TypeSHIFT))
  61. static void
  62. sdaddpart(SDunit* unit, char* name, ulong start, ulong end)
  63. {
  64. SDpart *pp;
  65. int i, partno;
  66. /*
  67. * Check name not already used
  68. * and look for a free slot.
  69. */
  70. if(unit->part != nil){
  71. partno = -1;
  72. for(i = 0; i < unit->npart; i++){
  73. pp = &unit->part[i];
  74. if(!pp->valid){
  75. if(partno == -1)
  76. partno = i;
  77. break;
  78. }
  79. if(strcmp(name, pp->name) == 0){
  80. if(pp->start == start && pp->end == end)
  81. return;
  82. error(Ebadctl);
  83. }
  84. }
  85. }
  86. else{
  87. if((unit->part = malloc(sizeof(SDpart)*SDnpart)) == nil)
  88. error(Enomem);
  89. unit->npart = SDnpart;
  90. partno = 0;
  91. }
  92. /*
  93. * If no free slot found then increase the
  94. * array size (can't get here with unit->part == nil).
  95. */
  96. if(partno == -1){
  97. if(unit->npart >= NPart)
  98. error(Enomem);
  99. if((pp = malloc(sizeof(SDpart)*(unit->npart+SDnpart))) == nil)
  100. error(Enomem);
  101. memmove(pp, unit->part, sizeof(SDpart)*unit->npart);
  102. free(unit->part);
  103. unit->part = pp;
  104. partno = unit->npart;
  105. unit->npart += SDnpart;
  106. }
  107. /*
  108. * Check size and extent are valid.
  109. */
  110. if(start > end || end > unit->sectors)
  111. error(Eio);
  112. pp = &unit->part[partno];
  113. pp->start = start;
  114. pp->end = end;
  115. kstrdup(&pp->name, name);
  116. kstrdup(&pp->user, eve);
  117. pp->perm = 0640;
  118. pp->valid = 1;
  119. }
  120. static void
  121. sddelpart(SDunit* unit, char* name)
  122. {
  123. int i;
  124. SDpart *pp;
  125. /*
  126. * Look for the partition to delete.
  127. * Can't delete if someone still has it open.
  128. */
  129. pp = unit->part;
  130. for(i = 0; i < unit->npart; i++){
  131. if(strcmp(name, pp->name) == 0)
  132. break;
  133. pp++;
  134. }
  135. if(i >= unit->npart)
  136. error(Ebadctl);
  137. if(strcmp(up->user, pp->user) && !iseve())
  138. error(Eperm);
  139. pp->valid = 0;
  140. pp->vers++;
  141. }
  142. static int
  143. sdinitpart(SDunit* unit)
  144. {
  145. int i, nf;
  146. ulong start, end;
  147. char *f[4], *p, *q, buf[10];
  148. unit->vers++;
  149. unit->sectors = unit->secsize = 0;
  150. if(unit->part){
  151. for(i = 0; i < unit->npart; i++){
  152. unit->part[i].valid = 0;
  153. unit->part[i].vers++;
  154. }
  155. }
  156. if(unit->inquiry[0] & 0xC0)
  157. return 0;
  158. switch(unit->inquiry[0] & 0x1F){
  159. case 0x00: /* DA */
  160. case 0x04: /* WORM */
  161. case 0x05: /* CD-ROM */
  162. case 0x07: /* MO */
  163. break;
  164. default:
  165. return 0;
  166. }
  167. if(unit->dev->ifc->online)
  168. unit->dev->ifc->online(unit);
  169. if(unit->sectors){
  170. sdaddpart(unit, "data", 0, unit->sectors);
  171. /*
  172. * Use partitions passed from boot program,
  173. * e.g.
  174. * sdC0part=dos 63 123123/plan9 123123 456456
  175. * This happens before /boot sets hostname so the
  176. * partitions will have the null-string for user.
  177. * The gen functions patch it up.
  178. */
  179. snprint(buf, sizeof buf, "%spart", unit->name);
  180. for(p = getconf(buf); p != nil; p = q){
  181. if(q = strchr(p, '/'))
  182. *q++ = '\0';
  183. nf = tokenize(p, f, nelem(f));
  184. if(nf < 3)
  185. continue;
  186. start = strtoul(f[1], 0, 0);
  187. end = strtoul(f[2], 0, 0);
  188. if(!waserror()){
  189. sdaddpart(unit, f[0], start, end);
  190. poperror();
  191. }
  192. }
  193. }
  194. return 1;
  195. }
  196. static SDev*
  197. sdgetdev(int idno)
  198. {
  199. SDev *sdev;
  200. int i;
  201. qlock(&devslock);
  202. for(i = 0; i != ndevs; i++)
  203. if(devs[i].dev->idno == idno)
  204. break;
  205. if(i == ndevs)
  206. sdev = nil;
  207. else{
  208. sdev = devs[i].dev;
  209. incref(&sdev->r);
  210. }
  211. qunlock(&devslock);
  212. return sdev;
  213. }
  214. static SDunit*
  215. sdgetunit(SDev* sdev, int subno)
  216. {
  217. SDunit *unit;
  218. char buf[32];
  219. /*
  220. * Associate a unit with a given device and sub-unit
  221. * number on that device.
  222. * The device will be probed if it has not already been
  223. * successfully accessed.
  224. */
  225. qlock(&sdev->unitlock);
  226. if(subno > sdev->nunit){
  227. qunlock(&sdev->unitlock);
  228. return nil;
  229. }
  230. unit = sdev->unit[subno];
  231. if(unit == nil){
  232. /*
  233. * Probe the unit only once. This decision
  234. * may be a little severe and reviewed later.
  235. */
  236. if(sdev->unitflg[subno]){
  237. qunlock(&sdev->unitlock);
  238. return nil;
  239. }
  240. if((unit = malloc(sizeof(SDunit))) == nil){
  241. qunlock(&sdev->unitlock);
  242. return nil;
  243. }
  244. sdev->unitflg[subno] = 1;
  245. snprint(buf, sizeof(buf), "%s%d", sdev->name, subno);
  246. kstrdup(&unit->name, buf);
  247. kstrdup(&unit->user, eve);
  248. unit->perm = 0555;
  249. unit->subno = subno;
  250. unit->dev = sdev;
  251. if(sdev->enabled == 0 && sdev->ifc->enable)
  252. sdev->ifc->enable(sdev);
  253. sdev->enabled = 1;
  254. /*
  255. * No need to lock anything here as this is only
  256. * called before the unit is made available in the
  257. * sdunit[] array.
  258. */
  259. if(unit->dev->ifc->verify(unit) == 0){
  260. qunlock(&sdev->unitlock);
  261. free(unit);
  262. return nil;
  263. }
  264. sdev->unit[subno] = unit;
  265. }
  266. qunlock(&sdev->unitlock);
  267. return unit;
  268. }
  269. static void
  270. sdreset(void)
  271. {
  272. int i;
  273. SDev *sdev, *tail, *sdlist;
  274. /*
  275. * Probe all configured controllers and make a list
  276. * of devices found, accumulating a possible maximum number
  277. * of units attached and marking each device with an index
  278. * into the linear top-level directory array of units.
  279. */
  280. tail = sdlist = nil;
  281. for(i = 0; sdifc[i] != nil; i++){
  282. if(sdifc[i]->pnp == nil || (sdev = sdifc[i]->pnp()) == nil)
  283. continue;
  284. if(sdlist != nil)
  285. tail->next = sdev;
  286. else
  287. sdlist = sdev;
  288. for(tail = sdev; tail->next != nil; tail = tail->next){
  289. tail->unit = (SDunit**)malloc(tail->nunit * sizeof(SDunit*));
  290. tail->unitflg = (int*)malloc(tail->nunit * sizeof(int));
  291. assert(tail->unit && tail->unitflg);
  292. ndevs++;
  293. }
  294. tail->unit = (SDunit**)malloc(tail->nunit * sizeof(SDunit*));
  295. tail->unitflg = (int*)malloc(tail->nunit * sizeof(int));
  296. ndevs++;
  297. }
  298. /*
  299. * Legacy and option code goes here. This will be hard...
  300. */
  301. /*
  302. * The maximum number of possible units is known, allocate
  303. * placeholders for their datastructures; the units will be
  304. * probed and structures allocated when attached.
  305. * Allocate controller names for the different types.
  306. */
  307. if(ndevs == 0)
  308. return;
  309. for(i = 0; sdifc[i] != nil; i++){
  310. /*
  311. * BUG: no check is made here or later when a
  312. * unit is attached that the id and name are set.
  313. */
  314. if(sdifc[i]->id)
  315. sdifc[i]->id(sdlist);
  316. }
  317. /*
  318. * The IDs have been set, unlink the sdlist and copy the spec to
  319. * the devtab.
  320. */
  321. devs = (SDevgrp*)malloc(ndevs * sizeof(SDevgrp));
  322. memset(devs, 0, ndevs * sizeof(SDevgrp));
  323. i = 0;
  324. while(sdlist != nil){
  325. devs[i].dev = sdlist;
  326. devs[i].nunits = sdlist->nunit;
  327. sdlist = sdlist->next;
  328. devs[i].dev->next = nil;
  329. i++;
  330. }
  331. }
  332. static int
  333. sd2gen(Chan* c, int i, Dir* dp)
  334. {
  335. Qid q;
  336. vlong l;
  337. SDpart *pp;
  338. SDperm *perm;
  339. SDunit *unit;
  340. SDev *sdev;
  341. int rv;
  342. sdev = sdgetdev(DEV(c->qid));
  343. assert(sdev);
  344. unit = sdev->unit[UNIT(c->qid)];
  345. rv = -1;
  346. switch(i){
  347. case Qctl:
  348. mkqid(&q, QID(DEV(c->qid), UNIT(c->qid), PART(c->qid), Qctl),
  349. unit->vers, QTFILE);
  350. perm = &unit->ctlperm;
  351. if(emptystr(perm->user)){
  352. kstrdup(&perm->user, eve);
  353. perm->perm = 0640;
  354. }
  355. devdir(c, q, "ctl", 0, perm->user, perm->perm, dp);
  356. rv = 1;
  357. break;
  358. case Qraw:
  359. mkqid(&q, QID(DEV(c->qid), UNIT(c->qid), PART(c->qid), Qraw),
  360. unit->vers, QTFILE);
  361. perm = &unit->rawperm;
  362. if(emptystr(perm->user)){
  363. kstrdup(&perm->user, eve);
  364. perm->perm = DMEXCL|0600;
  365. }
  366. devdir(c, q, "raw", 0, perm->user, perm->perm, dp);
  367. rv = 1;
  368. break;
  369. case Qpart:
  370. pp = &unit->part[PART(c->qid)];
  371. l = (pp->end - pp->start) * (vlong)unit->secsize;
  372. mkqid(&q, QID(DEV(c->qid), UNIT(c->qid), PART(c->qid), Qpart),
  373. unit->vers+pp->vers, QTFILE);
  374. if(emptystr(pp->user))
  375. kstrdup(&pp->user, eve);
  376. devdir(c, q, pp->name, l, pp->user, pp->perm, dp);
  377. rv = 1;
  378. break;
  379. }
  380. decref(&sdev->r);
  381. return rv;
  382. }
  383. static int
  384. sd1gen(Chan* c, int i, Dir* dp)
  385. {
  386. Qid q;
  387. switch(i){
  388. case Qtopctl:
  389. mkqid(&q, QID(0, 0, 0, Qtopctl), 0, QTFILE);
  390. devdir(c, q, "sdctl", 0, eve, 0640, dp);
  391. return 1;
  392. case Qtopstat:
  393. mkqid(&q, QID(0, 0, 0, Qtopstat), 0, QTFILE);
  394. devdir(c, q, "sdstat", 0, eve, 0640, dp);
  395. return 1;
  396. }
  397. return -1;
  398. }
  399. static int
  400. sdgen(Chan* c, char*, Dirtab*, int, int s, Dir* dp)
  401. {
  402. Qid q;
  403. vlong l;
  404. int i, r;
  405. SDpart *pp;
  406. SDunit *unit;
  407. SDev *sdev;
  408. switch(TYPE(c->qid)){
  409. case Qtopdir:
  410. if(s == DEVDOTDOT){
  411. mkqid(&q, QID(0, 0, 0, Qtopdir), 0, QTDIR);
  412. sprint(up->genbuf, "#%C", sddevtab.dc);
  413. devdir(c, q, up->genbuf, 0, eve, 0555, dp);
  414. return 1;
  415. }
  416. if(s == 0 || s == 1)
  417. return sd1gen(c, s + Qtopbase, dp);
  418. s -= 2;
  419. qlock(&devslock);
  420. for(i = 0; i != ndevs; i++){
  421. if(s < devs[i].nunits)
  422. break;
  423. s -= devs[i].nunits;
  424. }
  425. if(i == ndevs){
  426. /* Run of the end of the list */
  427. qunlock(&devslock);
  428. return -1;
  429. }
  430. if((sdev = devs[i].dev) == nil){
  431. qunlock(&devslock);
  432. return 0;
  433. }
  434. incref(&sdev->r);
  435. qunlock(&devslock);
  436. if((unit = sdev->unit[s]) == nil)
  437. if((unit = sdgetunit(sdev, s)) == nil){
  438. decref(&sdev->r);
  439. return 0;
  440. }
  441. mkqid(&q, QID(sdev->idno, s, 0, Qunitdir), 0, QTDIR);
  442. if(emptystr(unit->user))
  443. kstrdup(&unit->user, eve);
  444. devdir(c, q, unit->name, 0, unit->user, unit->perm, dp);
  445. decref(&sdev->r);
  446. return 1;
  447. case Qunitdir:
  448. if(s == DEVDOTDOT){
  449. mkqid(&q, QID(0, 0, 0, Qtopdir), 0, QTDIR);
  450. sprint(up->genbuf, "#%C", sddevtab.dc);
  451. devdir(c, q, up->genbuf, 0, eve, 0555, dp);
  452. return 1;
  453. }
  454. if((sdev = sdgetdev(DEV(c->qid))) == nil){
  455. devdir(c, q, "unavailable", 0, eve, 0, dp);
  456. return 1;
  457. }
  458. unit = sdev->unit[UNIT(c->qid)];
  459. qlock(&unit->ctl);
  460. /*
  461. * Check for media change.
  462. * If one has already been detected, sectors will be zero.
  463. * If there is one waiting to be detected, online
  464. * will return > 1.
  465. * Online is a bit of a large hammer but does the job.
  466. */
  467. if(unit->sectors == 0
  468. || (unit->dev->ifc->online && unit->dev->ifc->online(unit) > 1))
  469. sdinitpart(unit);
  470. i = s+Qunitbase;
  471. if(i < Qpart){
  472. r = sd2gen(c, i, dp);
  473. qunlock(&unit->ctl);
  474. decref(&sdev->r);
  475. return r;
  476. }
  477. i -= Qpart;
  478. if(unit->part == nil || i >= unit->npart){
  479. qunlock(&unit->ctl);
  480. decref(&sdev->r);
  481. break;
  482. }
  483. pp = &unit->part[i];
  484. if(!pp->valid){
  485. qunlock(&unit->ctl);
  486. decref(&sdev->r);
  487. return 0;
  488. }
  489. l = (pp->end - pp->start) * (vlong)unit->secsize;
  490. mkqid(&q, QID(DEV(c->qid), UNIT(c->qid), i, Qpart),
  491. unit->vers+pp->vers, QTFILE);
  492. if(emptystr(pp->user))
  493. kstrdup(&pp->user, eve);
  494. devdir(c, q, pp->name, l, pp->user, pp->perm, dp);
  495. qunlock(&unit->ctl);
  496. decref(&sdev->r);
  497. return 1;
  498. case Qraw:
  499. case Qctl:
  500. case Qpart:
  501. if((sdev = sdgetdev(DEV(c->qid))) == nil){
  502. devdir(c, q, "unavailable", 0, eve, 0, dp);
  503. return 1;
  504. }
  505. unit = sdev->unit[UNIT(c->qid)];
  506. qlock(&unit->ctl);
  507. r = sd2gen(c, TYPE(c->qid), dp);
  508. qunlock(&unit->ctl);
  509. decref(&sdev->r);
  510. return r;
  511. case Qtopctl:
  512. case Qtopstat:
  513. return sd1gen(c, TYPE(c->qid), dp);
  514. default:
  515. break;
  516. }
  517. return -1;
  518. }
  519. static Chan*
  520. sdattach(char* spec)
  521. {
  522. Chan *c;
  523. char *p;
  524. SDev *sdev;
  525. int idno, subno, i;
  526. if(ndevs == 0 || *spec == '\0'){
  527. c = devattach(sddevtab.dc, spec);
  528. mkqid(&c->qid, QID(0, 0, 0, Qtopdir), 0, QTDIR);
  529. return c;
  530. }
  531. if(spec[0] != 's' || spec[1] != 'd')
  532. error(Ebadspec);
  533. idno = spec[2];
  534. subno = strtol(&spec[3], &p, 0);
  535. if(p == &spec[3])
  536. error(Ebadspec);
  537. qlock(&devslock);
  538. for (sdev = nil, i = 0; i != ndevs; i++)
  539. if((sdev = devs[i].dev) != nil && sdev->idno == idno)
  540. break;
  541. if(i == ndevs || subno >= sdev->nunit || sdgetunit(sdev, subno) == nil){
  542. qunlock(&devslock);
  543. error(Enonexist);
  544. }
  545. incref(&sdev->r);
  546. qunlock(&devslock);
  547. c = devattach(sddevtab.dc, spec);
  548. mkqid(&c->qid, QID(sdev->idno, subno, 0, Qunitdir), 0, QTDIR);
  549. c->dev = (sdev->idno << UnitLOG) + subno;
  550. decref(&sdev->r);
  551. return c;
  552. }
  553. static Walkqid*
  554. sdwalk(Chan* c, Chan* nc, char** name, int nname)
  555. {
  556. return devwalk(c, nc, name, nname, nil, 0, sdgen);
  557. }
  558. static int
  559. sdstat(Chan* c, uchar* db, int n)
  560. {
  561. return devstat(c, db, n, nil, 0, sdgen);
  562. }
  563. static Chan*
  564. sdopen(Chan* c, int omode)
  565. {
  566. SDpart *pp;
  567. SDunit *unit;
  568. SDev *sdev;
  569. uchar tp;
  570. c = devopen(c, omode, 0, 0, sdgen);
  571. if((tp = TYPE(c->qid)) != Qctl && tp != Qraw && tp != Qpart)
  572. return c;
  573. sdev = sdgetdev(DEV(c->qid));
  574. if(sdev == nil)
  575. error(Enonexist);
  576. unit = sdev->unit[UNIT(c->qid)];
  577. switch(TYPE(c->qid)){
  578. case Qctl:
  579. c->qid.vers = unit->vers;
  580. break;
  581. case Qraw:
  582. c->qid.vers = unit->vers;
  583. if(tas(&unit->rawinuse) != 0){
  584. c->flag &= ~COPEN;
  585. error(Einuse);
  586. }
  587. unit->state = Rawcmd;
  588. break;
  589. case Qpart:
  590. qlock(&unit->ctl);
  591. if(waserror()){
  592. qunlock(&unit->ctl);
  593. c->flag &= ~COPEN;
  594. nexterror();
  595. }
  596. pp = &unit->part[PART(c->qid)];
  597. c->qid.vers = unit->vers+pp->vers;
  598. qunlock(&unit->ctl);
  599. poperror();
  600. break;
  601. }
  602. decref(&sdev->r);
  603. return c;
  604. }
  605. static void
  606. sdclose(Chan* c)
  607. {
  608. SDunit *unit;
  609. SDev *sdev;
  610. if(c->qid.type & QTDIR)
  611. return;
  612. if(!(c->flag & COPEN))
  613. return;
  614. switch(TYPE(c->qid)){
  615. default:
  616. break;
  617. case Qraw:
  618. sdev = sdgetdev(DEV(c->qid));
  619. if(sdev){
  620. unit = sdev->unit[UNIT(c->qid)];
  621. unit->rawinuse = 0;
  622. decref(&sdev->r);
  623. }
  624. break;
  625. }
  626. }
  627. static long
  628. sdbio(Chan* c, int write, char* a, long len, vlong off)
  629. {
  630. int nchange;
  631. long l;
  632. uchar *b;
  633. SDpart *pp;
  634. SDunit *unit;
  635. SDev *sdev;
  636. ulong bno, max, nb, offset;
  637. sdev = sdgetdev(DEV(c->qid));
  638. if(sdev == nil)
  639. error(Enonexist);
  640. unit = sdev->unit[UNIT(c->qid)];
  641. if(unit == nil)
  642. error(Enonexist);
  643. nchange = 0;
  644. qlock(&unit->ctl);
  645. while(waserror()){
  646. /* notification of media change; go around again */
  647. if(strcmp(up->errstr, Eio) == 0 && unit->sectors == 0 && nchange++ == 0){
  648. sdinitpart(unit);
  649. continue;
  650. }
  651. /* other errors; give up */
  652. qunlock(&unit->ctl);
  653. decref(&sdev->r);
  654. nexterror();
  655. }
  656. pp = &unit->part[PART(c->qid)];
  657. if(unit->vers+pp->vers != c->qid.vers)
  658. error(Eio);
  659. /*
  660. * Check the request is within bounds.
  661. * Removeable drives are locked throughout the I/O
  662. * in case the media changes unexpectedly.
  663. * Non-removeable drives are not locked during the I/O
  664. * to allow the hardware to optimise if it can; this is
  665. * a little fast and loose.
  666. * It's assumed that non-removeable media parameters
  667. * (sectors, secsize) can't change once the drive has
  668. * been brought online.
  669. */
  670. bno = (off/unit->secsize) + pp->start;
  671. nb = ((off+len+unit->secsize-1)/unit->secsize) + pp->start - bno;
  672. max = SDmaxio/unit->secsize;
  673. if(nb > max)
  674. nb = max;
  675. if(bno+nb > pp->end)
  676. nb = pp->end - bno;
  677. if(bno >= pp->end || nb == 0){
  678. if(write)
  679. error(Eio);
  680. qunlock(&unit->ctl);
  681. decref(&sdev->r);
  682. poperror();
  683. return 0;
  684. }
  685. if(!(unit->inquiry[1] & 0x80)){
  686. qunlock(&unit->ctl);
  687. poperror();
  688. }
  689. b = sdmalloc(nb*unit->secsize);
  690. if(b == nil)
  691. error(Enomem);
  692. if(waserror()){
  693. sdfree(b);
  694. if(!(unit->inquiry[1] & 0x80))
  695. decref(&sdev->r); /* gadverdamme! */
  696. nexterror();
  697. }
  698. offset = off%unit->secsize;
  699. if(offset+len > nb*unit->secsize)
  700. len = nb*unit->secsize - offset;
  701. if(write){
  702. if(offset || (len%unit->secsize)){
  703. l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno);
  704. if(l < 0)
  705. error(Eio);
  706. if(l < (nb*unit->secsize)){
  707. nb = l/unit->secsize;
  708. l = nb*unit->secsize - offset;
  709. if(len > l)
  710. len = l;
  711. }
  712. }
  713. memmove(b+offset, a, len);
  714. l = unit->dev->ifc->bio(unit, 0, 1, b, nb, bno);
  715. if(l < 0)
  716. error(Eio);
  717. if(l < offset)
  718. len = 0;
  719. else if(len > l - offset)
  720. len = l - offset;
  721. }
  722. else{
  723. l = unit->dev->ifc->bio(unit, 0, 0, b, nb, bno);
  724. if(l < 0)
  725. error(Eio);
  726. if(l < offset)
  727. len = 0;
  728. else if(len > l - offset)
  729. len = l - offset;
  730. memmove(a, b+offset, len);
  731. }
  732. sdfree(b);
  733. poperror();
  734. if(unit->inquiry[1] & 0x80){
  735. qunlock(&unit->ctl);
  736. poperror();
  737. }
  738. decref(&sdev->r);
  739. return len;
  740. }
  741. static long
  742. sdrio(SDreq* r, void* a, long n)
  743. {
  744. void *data;
  745. if(n >= SDmaxio || n < 0)
  746. error(Etoobig);
  747. data = nil;
  748. if(n){
  749. if((data = sdmalloc(n)) == nil)
  750. error(Enomem);
  751. if(r->write)
  752. memmove(data, a, n);
  753. }
  754. r->data = data;
  755. r->dlen = n;
  756. if(waserror()){
  757. if(data != nil){
  758. sdfree(data);
  759. r->data = nil;
  760. }
  761. nexterror();
  762. }
  763. if(r->unit->dev->ifc->rio(r) != SDok)
  764. error(Eio);
  765. if(!r->write && r->rlen > 0)
  766. memmove(a, data, r->rlen);
  767. if(data != nil){
  768. sdfree(data);
  769. r->data = nil;
  770. }
  771. poperror();
  772. return r->rlen;
  773. }
  774. static long
  775. sdread(Chan *c, void *a, long n, vlong off)
  776. {
  777. char *p, *e, *buf;
  778. SDpart *pp;
  779. SDunit *unit;
  780. SDev *sdev;
  781. ulong offset;
  782. int i, l, status;
  783. offset = off;
  784. switch(TYPE(c->qid)){
  785. default:
  786. error(Eperm);
  787. case Qtopstat:
  788. p = buf = malloc(READSTR);
  789. assert(p);
  790. e = p + READSTR;
  791. qlock(&devslock);
  792. for(i = 0; i != ndevs; i++){
  793. SDev *sdev = devs[i].dev;
  794. if(sdev->ifc->stat)
  795. p = sdev->ifc->stat(sdev, p, e);
  796. else
  797. p = seprint(e, "%s; no statistics available\n", sdev->name);
  798. }
  799. qunlock(&devslock);
  800. n = readstr(off, a, n, buf);
  801. free(buf);
  802. return n;
  803. case Qtopdir:
  804. case Qunitdir:
  805. return devdirread(c, a, n, 0, 0, sdgen);
  806. case Qctl:
  807. sdev = sdgetdev(DEV(c->qid));
  808. if(sdev == nil)
  809. error(Enonexist);
  810. unit = sdev->unit[UNIT(c->qid)];
  811. p = malloc(READSTR);
  812. l = snprint(p, READSTR, "inquiry %.48s\n",
  813. (char*)unit->inquiry+8);
  814. qlock(&unit->ctl);
  815. /*
  816. * If there's a device specific routine it must
  817. * provide all information pertaining to night geometry
  818. * and the garscadden trains.
  819. */
  820. if(unit->dev->ifc->rctl)
  821. l += unit->dev->ifc->rctl(unit, p+l, READSTR-l);
  822. if(unit->sectors == 0)
  823. sdinitpart(unit);
  824. if(unit->sectors){
  825. if(unit->dev->ifc->rctl == nil)
  826. l += snprint(p+l, READSTR-l,
  827. "geometry %ld %ld\n",
  828. unit->sectors, unit->secsize);
  829. pp = unit->part;
  830. for(i = 0; i < unit->npart; i++){
  831. if(pp->valid)
  832. l += snprint(p+l, READSTR-l,
  833. "part %s %lud %lud\n",
  834. pp->name, pp->start, pp->end);
  835. pp++;
  836. }
  837. }
  838. qunlock(&unit->ctl);
  839. decref(&sdev->r);
  840. l = readstr(offset, a, n, p);
  841. free(p);
  842. return l;
  843. case Qraw:
  844. sdev = sdgetdev(DEV(c->qid));
  845. if(sdev == nil)
  846. error(Enonexist);
  847. unit = sdev->unit[UNIT(c->qid)];
  848. qlock(&unit->raw);
  849. if(waserror()){
  850. qunlock(&unit->raw);
  851. decref(&sdev->r);
  852. nexterror();
  853. }
  854. if(unit->state == Rawdata){
  855. unit->state = Rawstatus;
  856. i = sdrio(unit->req, a, n);
  857. }
  858. else if(unit->state == Rawstatus){
  859. status = unit->req->status;
  860. unit->state = Rawcmd;
  861. free(unit->req);
  862. unit->req = nil;
  863. i = readnum(0, a, n, status, NUMSIZE);
  864. } else
  865. i = 0;
  866. qunlock(&unit->raw);
  867. decref(&sdev->r);
  868. poperror();
  869. return i;
  870. case Qpart:
  871. return sdbio(c, 0, a, n, off);
  872. }
  873. return 0;
  874. }
  875. typedef struct Confdata Confdata;
  876. struct Confdata {
  877. int on;
  878. char* spec;
  879. DevConf cf;
  880. };
  881. static void
  882. parseswitch(Confdata* cd, char* option)
  883. {
  884. if(!strcmp("on", option))
  885. cd->on = 1;
  886. else if(!strcmp("off", option))
  887. cd->on = 0;
  888. else
  889. error(Ebadarg);
  890. }
  891. static void
  892. parsespec(Confdata* cd, char* option)
  893. {
  894. if(strlen(option) > 1)
  895. error(Ebadarg);
  896. cd->spec = option;
  897. }
  898. static Devport*
  899. getnewport(DevConf* dc)
  900. {
  901. Devport *p;
  902. p = (Devport *)malloc((dc->nports + 1) * sizeof(Devport));
  903. if(dc->nports > 0){
  904. memmove(p, dc->ports, dc->nports * sizeof(Devport));
  905. free(dc->ports);
  906. }
  907. dc->ports = p;
  908. p = &dc->ports[dc->nports++];
  909. p->size = -1;
  910. p->port = (ulong)-1;
  911. return p;
  912. }
  913. static void
  914. parseport(Confdata* cd, char* option)
  915. {
  916. char *e;
  917. Devport *p;
  918. if(cd->cf.nports == 0 || cd->cf.ports[cd->cf.nports-1].port != (ulong)-1)
  919. p = getnewport(&cd->cf);
  920. else
  921. p = &cd->cf.ports[cd->cf.nports-1];
  922. p->port = strtol(option, &e, 0);
  923. if(e == nil || *e != '\0')
  924. error(Ebadarg);
  925. }
  926. static void
  927. parsesize(Confdata* cd, char* option)
  928. {
  929. char *e;
  930. Devport *p;
  931. if(cd->cf.nports == 0 || cd->cf.ports[cd->cf.nports-1].size != -1)
  932. p = getnewport(&cd->cf);
  933. else
  934. p = &cd->cf.ports[cd->cf.nports-1];
  935. p->size = (int)strtol(option, &e, 0);
  936. if(e == nil || *e != '\0')
  937. error(Ebadarg);
  938. }
  939. static void
  940. parseirq(Confdata* cd, char* option)
  941. {
  942. char *e;
  943. cd->cf.intnum = strtoul(option, &e, 0);
  944. if(e == nil || *e != '\0')
  945. error(Ebadarg);
  946. }
  947. static void
  948. parsetype(Confdata* cd, char* option)
  949. {
  950. cd->cf.type = option;
  951. }
  952. static struct {
  953. char *option;
  954. void (*parse)(Confdata*, char*);
  955. } options[] = {
  956. { "switch", parseswitch, },
  957. { "spec", parsespec, },
  958. { "port", parseport, },
  959. { "size", parsesize, },
  960. { "irq", parseirq, },
  961. { "type", parsetype, },
  962. };
  963. static long
  964. sdwrite(Chan* c, void* a, long n, vlong off)
  965. {
  966. Cmdbuf *cb;
  967. SDreq *req;
  968. SDunit *unit;
  969. SDev *sdev;
  970. ulong end, start;
  971. switch(TYPE(c->qid)){
  972. default:
  973. error(Eperm);
  974. case Qtopctl: {
  975. Confdata cd;
  976. char buf[256], *field[Ncmd];
  977. int nf, i, j;
  978. memset(&cd, 0, sizeof(Confdata));
  979. if(n > sizeof(buf)-1) n = sizeof(buf)-1;
  980. memmove(buf, a, n);
  981. buf[n] = '\0';
  982. cd.on = -1;
  983. cd.spec = '\0';
  984. memset(&cd.cf, 0, sizeof(DevConf));
  985. nf = tokenize(buf, field, Ncmd);
  986. for(i = 0; i < nf; i++){
  987. char *opt = field[i++];
  988. if(i >= nf)
  989. error(Ebadarg);
  990. for(j = 0; j != nelem(options); j++)
  991. if(!strcmp(opt, options[j].option))
  992. break;
  993. if(j == nelem(options))
  994. error(Ebadarg);
  995. options[j].parse(&cd, field[i]);
  996. }
  997. if(cd.on < 0)
  998. error(Ebadarg);
  999. if(cd.on){
  1000. if(cd.spec == '\0' || cd.cf.nports == 0 ||
  1001. cd.cf.intnum == 0 || cd.cf.type == nil)
  1002. error(Ebadarg);
  1003. }
  1004. else{
  1005. if(cd.spec == '\0')
  1006. error(Ebadarg);
  1007. }
  1008. if(sddevtab.config == nil)
  1009. error("No configuration function");
  1010. sddevtab.config(cd.on, cd.spec, &cd.cf);
  1011. break;
  1012. }
  1013. case Qctl:
  1014. cb = parsecmd(a, n);
  1015. sdev = sdgetdev(DEV(c->qid));
  1016. if(sdev == nil)
  1017. error(Enonexist);
  1018. unit = sdev->unit[UNIT(c->qid)];
  1019. qlock(&unit->ctl);
  1020. if(waserror()){
  1021. qunlock(&unit->ctl);
  1022. decref(&sdev->r);
  1023. free(cb);
  1024. nexterror();
  1025. }
  1026. if(unit->vers != c->qid.vers)
  1027. error(Eio);
  1028. if(cb->nf < 1)
  1029. error(Ebadctl);
  1030. if(strcmp(cb->f[0], "part") == 0){
  1031. if(cb->nf != 4)
  1032. error(Ebadctl);
  1033. if(unit->sectors == 0 && !sdinitpart(unit))
  1034. error(Eio);
  1035. start = strtoul(cb->f[2], 0, 0);
  1036. end = strtoul(cb->f[3], 0, 0);
  1037. sdaddpart(unit, cb->f[1], start, end);
  1038. }
  1039. else if(strcmp(cb->f[0], "delpart") == 0){
  1040. if(cb->nf != 2 || unit->part == nil)
  1041. error(Ebadctl);
  1042. sddelpart(unit, cb->f[1]);
  1043. }
  1044. else if(unit->dev->ifc->wctl)
  1045. unit->dev->ifc->wctl(unit, cb);
  1046. else
  1047. error(Ebadctl);
  1048. qunlock(&unit->ctl);
  1049. decref(&sdev->r);
  1050. poperror();
  1051. free(cb);
  1052. break;
  1053. case Qraw:
  1054. sdev = sdgetdev(DEV(c->qid));
  1055. if(sdev == nil)
  1056. error(Enonexist);
  1057. unit = sdev->unit[UNIT(c->qid)];
  1058. qlock(&unit->raw);
  1059. if(waserror()){
  1060. qunlock(&unit->raw);
  1061. decref(&sdev->r);
  1062. nexterror();
  1063. }
  1064. switch(unit->state){
  1065. case Rawcmd:
  1066. if(n < 6 || n > sizeof(req->cmd))
  1067. error(Ebadarg);
  1068. if((req = malloc(sizeof(SDreq))) == nil)
  1069. error(Enomem);
  1070. req->unit = unit;
  1071. memmove(req->cmd, a, n);
  1072. req->clen = n;
  1073. req->flags = SDnosense;
  1074. req->status = ~0;
  1075. unit->req = req;
  1076. unit->state = Rawdata;
  1077. break;
  1078. case Rawstatus:
  1079. unit->state = Rawcmd;
  1080. free(unit->req);
  1081. unit->req = nil;
  1082. error(Ebadusefd);
  1083. case Rawdata:
  1084. if(unit->state != Rawdata)
  1085. error(Ebadusefd);
  1086. unit->state = Rawstatus;
  1087. unit->req->write = 1;
  1088. n = sdrio(unit->req, a, n);
  1089. }
  1090. qunlock(&unit->raw);
  1091. decref(&sdev->r);
  1092. poperror();
  1093. break;
  1094. case Qpart:
  1095. return sdbio(c, 1, a, n, off);
  1096. }
  1097. return n;
  1098. }
  1099. static int
  1100. sdwstat(Chan* c, uchar* dp, int n)
  1101. {
  1102. Dir *d;
  1103. SDpart *pp;
  1104. SDperm *perm;
  1105. SDunit *unit;
  1106. SDev *sdev;
  1107. if(c->qid.type & QTDIR)
  1108. error(Eperm);
  1109. sdev = sdgetdev(DEV(c->qid));
  1110. if(sdev == nil)
  1111. error(Enonexist);
  1112. unit = sdev->unit[UNIT(c->qid)];
  1113. qlock(&unit->ctl);
  1114. d = nil;
  1115. if(waserror()){
  1116. free(d);
  1117. qunlock(&unit->ctl);
  1118. decref(&sdev->r);
  1119. nexterror();
  1120. }
  1121. switch(TYPE(c->qid)){
  1122. default:
  1123. error(Eperm);
  1124. case Qctl:
  1125. perm = &unit->ctlperm;
  1126. break;
  1127. case Qraw:
  1128. perm = &unit->rawperm;
  1129. break;
  1130. case Qpart:
  1131. pp = &unit->part[PART(c->qid)];
  1132. if(unit->vers+pp->vers != c->qid.vers)
  1133. error(Enonexist);
  1134. perm = &pp->SDperm;
  1135. break;
  1136. }
  1137. if(strcmp(up->user, perm->user) && !iseve())
  1138. error(Eperm);
  1139. d = smalloc(sizeof(Dir)+n);
  1140. n = convM2D(dp, n, &d[0], (char*)&d[1]);
  1141. if(n == 0)
  1142. error(Eshortstat);
  1143. if(!emptystr(d[0].uid))
  1144. kstrdup(&perm->user, d[0].uid);
  1145. if(d[0].mode != ~0UL)
  1146. perm->perm = (perm->perm & ~0777) | (d[0].mode & 0777);
  1147. free(d);
  1148. qunlock(&unit->ctl);
  1149. decref(&sdev->r);
  1150. poperror();
  1151. return n;
  1152. }
  1153. static char
  1154. getspec(char base)
  1155. {
  1156. while(1){
  1157. int i;
  1158. SDev *sdev;
  1159. for(i = 0; i != ndevs; i++)
  1160. if((sdev = devs[i].dev) != nil && (char)sdev->idno == base)
  1161. break;
  1162. if(i == ndevs)
  1163. return base;
  1164. base++;
  1165. }
  1166. return '\0';
  1167. }
  1168. static int
  1169. configure(char* spec, DevConf* cf)
  1170. {
  1171. ISAConf isa;
  1172. SDevgrp *tmpdevs;
  1173. SDev *tail, *sdev, *(*probe)(DevConf*);
  1174. char *p, name[32];
  1175. int i, nnew;
  1176. if((p = strchr(cf->type, '/')) != nil)
  1177. *p++ = '\0';
  1178. for(i = 0; sdifc[i] != nil; i++)
  1179. if(!strcmp(sdifc[i]->name, cf->type))
  1180. break;
  1181. if(sdifc[i] == nil)
  1182. error("type not found");
  1183. if((probe = sdifc[i]->probe) == nil)
  1184. error("No probe function");
  1185. if(p){
  1186. /* Try to find the card on the ISA bus. This code really belongs
  1187. in sdata and I'll move it later. Really! */
  1188. memset(&isa, 0, sizeof(isa));
  1189. isa.port = cf->ports[0].port;
  1190. isa.irq = cf->intnum;
  1191. if(pcmspecial(p, &isa) < 0)
  1192. error("Cannot find controller");
  1193. }
  1194. qlock(&devslock);
  1195. if(waserror()){
  1196. qunlock(&devslock);
  1197. nexterror();
  1198. }
  1199. for(i = 0; i != ndevs; i++)
  1200. if((sdev = devs[i].dev) != nil && sdev->idno == *spec)
  1201. break;
  1202. if(i != ndevs)
  1203. error(Eexist);
  1204. if((sdev = (*probe)(cf)) == nil)
  1205. error("Cannot probe controller");
  1206. poperror();
  1207. nnew = 0;
  1208. tail = sdev;
  1209. while(tail){
  1210. nnew++;
  1211. tail = tail->next;
  1212. }
  1213. tmpdevs = (SDevgrp*)malloc((ndevs + nnew) * sizeof(SDevgrp));
  1214. memmove(tmpdevs, devs, ndevs * sizeof(SDevgrp));
  1215. free(devs);
  1216. devs = tmpdevs;
  1217. while(sdev){
  1218. /* Assign `spec' to the device */
  1219. *spec = getspec(*spec);
  1220. snprint(name, sizeof(name), "sd%c", *spec);
  1221. kstrdup(&sdev->name, name);
  1222. sdev->idno = *spec;
  1223. sdev->unit = (SDunit **)malloc(sdev->nunit * sizeof(SDunit*));
  1224. sdev->unitflg = (int *)malloc(sdev->nunit * sizeof(int));
  1225. assert(sdev->unit && sdev->unitflg);
  1226. devs[ndevs].dev = sdev;
  1227. devs[ndevs].nunits = sdev->nunit;
  1228. sdev = sdev->next;
  1229. devs[ndevs].dev->next = nil;
  1230. ndevs++;
  1231. }
  1232. qunlock(&devslock);
  1233. return 0;
  1234. }
  1235. static int
  1236. unconfigure(char* spec)
  1237. {
  1238. int i;
  1239. SDev *sdev;
  1240. qlock(&devslock);
  1241. if(waserror()){
  1242. qunlock(&devslock);
  1243. nexterror();
  1244. }
  1245. sdev = nil;
  1246. for(i = 0; i != ndevs; i++)
  1247. if((sdev = devs[i].dev) != nil && sdev->idno == *spec)
  1248. break;
  1249. if(i == ndevs)
  1250. error(Enonexist);
  1251. if(sdev->r.ref)
  1252. error(Einuse);
  1253. /* make sure no interrupts arrive anymore before removing resources */
  1254. if(sdev->enabled && sdev->ifc->disable)
  1255. sdev->ifc->disable(sdev);
  1256. /* we're alone and the device tab is locked; make the device unavailable */
  1257. memmove(&devs[i], &devs[ndevs - 1], sizeof(SDevgrp));
  1258. memset(&devs[ndevs - 1], 0, sizeof(SDevgrp));
  1259. ndevs--;
  1260. qunlock(&devslock);
  1261. poperror();
  1262. for(i = 0; i != sdev->nunit; i++)
  1263. if(sdev->unit[i]){
  1264. SDunit *unit = sdev->unit[i];
  1265. free(unit->name);
  1266. free(unit->user);
  1267. free(unit);
  1268. }
  1269. if(sdev->ifc->clear)
  1270. sdev->ifc->clear(sdev);
  1271. return 0;
  1272. }
  1273. static int
  1274. sdconfig(int on, char* spec, DevConf* cf)
  1275. {
  1276. if(on)
  1277. return configure(spec, cf);
  1278. return unconfigure(spec);
  1279. }
  1280. Dev sddevtab = {
  1281. 'S',
  1282. "sd",
  1283. sdreset,
  1284. devinit,
  1285. devshutdown,
  1286. sdattach,
  1287. sdwalk,
  1288. sdstat,
  1289. sdopen,
  1290. devcreate,
  1291. sdclose,
  1292. sdread,
  1293. devbread,
  1294. sdwrite,
  1295. devbwrite,
  1296. devremove,
  1297. sdwstat,
  1298. devpower,
  1299. sdconfig,
  1300. };