devwpsd.c 28 KB

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