devusb.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*
  2. * USB device driver framework.
  3. *
  4. * This is in charge of providing access to actual HCIs
  5. * and providing I/O to the various endpoints of devices.
  6. * A separate user program (usbd) is in charge of
  7. * enumerating the bus, setting up endpoints and
  8. * starting devices (also user programs).
  9. *
  10. * The interface provided is a violation of the standard:
  11. * you're welcome.
  12. *
  13. * The interface consists of a root directory with several files
  14. * plus a directory (epN.M) with two files per endpoint.
  15. * A device is represented by its first endpoint, which
  16. * is a control endpoint automatically allocated for each device.
  17. * Device control endpoints may be used to create new endpoints.
  18. * Devices corresponding to hubs may also allocate new devices,
  19. * perhaps also hubs. Initially, a hub device is allocated for
  20. * each controller present, to represent its root hub. Those can
  21. * never be removed.
  22. *
  23. * All endpoints refer to the first endpoint (epN.0) of the device,
  24. * which keeps per-device information, and also to the HCI used
  25. * to reach them. Although all endpoints cache that information.
  26. *
  27. * epN.M/data files permit I/O and are considered DMEXCL.
  28. * epN.M/ctl files provide status info and accept control requests.
  29. *
  30. * Endpoints may be given file names to be listed also at #u,
  31. * for those drivers that have nothing to do after configuring the
  32. * device and its endpoints.
  33. *
  34. * Drivers for different controllers are kept at usb[oue]hci.c
  35. * It's likely we could factor out much from controllers into
  36. * a generic controller driver, the problem is that details
  37. * regarding how to handle toggles, tokens, Tds, etc. will
  38. * get in the way. Thus, code is probably easier the way it is.
  39. */
  40. #include "u.h"
  41. #include "../port/lib.h"
  42. #include "mem.h"
  43. #include "dat.h"
  44. #include "fns.h"
  45. #include "io.h"
  46. #include "../port/error.h"
  47. #include "../port/usb.h"
  48. typedef struct Hcitype Hcitype;
  49. enum
  50. {
  51. /* Qid numbers */
  52. Qdir = 0, /* #u */
  53. Qusbdir, /* #u/usb */
  54. Qctl, /* #u/usb/ctl - control requests */
  55. Qep0dir, /* #u/usb/ep0.0 - endpoint 0 dir */
  56. Qep0io, /* #u/usb/ep0.0/data - endpoint 0 I/O */
  57. Qep0ctl, /* #u/usb/ep0.0/ctl - endpoint 0 ctl. */
  58. Qep0dummy, /* give 4 qids to each endpoint */
  59. Qepdir = 0, /* (qid-qep0dir)&3 is one of these */
  60. Qepio, /* to identify which file for the endpoint */
  61. Qepctl,
  62. /* ... */
  63. /* Usb ctls. */
  64. CMdebug = 0, /* debug on|off */
  65. CMdump, /* dump (data structures for debug) */
  66. /* Ep. ctls */
  67. CMnew = 0, /* new nb ctl|bulk|intr|iso r|w|rw (endpoint) */
  68. CMnewdev, /* newdev full|low|high portnb (allocate new devices) */
  69. CMhub, /* hub (set the device as a hub) */
  70. CMspeed, /* speed full|low|high|no */
  71. CMmaxpkt, /* maxpkt size */
  72. CMntds, /* ntds nb (max nb. of tds per µframe) */
  73. CMclrhalt, /* clrhalt (halt was cleared on endpoint) */
  74. CMpollival, /* pollival interval (interrupt/iso) */
  75. CMhz, /* hz n (samples/sec; iso) */
  76. CMsamplesz, /* samplesz n (sample size; iso) */
  77. CMinfo, /* info infostr (ke.ep info for humans) */
  78. CMdetach, /* detach (abort I/O forever on this ep). */
  79. CMaddress, /* address (address is assigned) */
  80. CMdebugep, /* debug n (set/clear debug for this ep) */
  81. CMname, /* name str (show up as #u/name as well) */
  82. CMtmout, /* timeout n (activate timeouts for ep) */
  83. CMpreset, /* reset the port */
  84. /* Hub feature selectors */
  85. Rportenable = 1,
  86. Rportreset = 4,
  87. };
  88. struct Hcitype
  89. {
  90. char* type;
  91. int (*reset)(Hci*);
  92. };
  93. #define QID(q) ((int)(q).path)
  94. static char Edetach[] = "device is detached";
  95. static char Enotconf[] = "endpoint not configured";
  96. char Estalled[] = "endpoint stalled";
  97. static Cmdtab usbctls[] =
  98. {
  99. {CMdebug, "debug", 2},
  100. {CMdump, "dump", 1},
  101. };
  102. static Cmdtab epctls[] =
  103. {
  104. {CMnew, "new", 4},
  105. {CMnewdev, "newdev", 3},
  106. {CMhub, "hub", 1},
  107. {CMspeed, "speed", 2},
  108. {CMmaxpkt, "maxpkt", 2},
  109. {CMntds, "ntds", 2},
  110. {CMpollival, "pollival", 2},
  111. {CMsamplesz, "samplesz", 2},
  112. {CMhz, "hz", 2},
  113. {CMinfo, "info", 0},
  114. {CMdetach, "detach", 1},
  115. {CMaddress, "address", 1},
  116. {CMdebugep, "debug", 2},
  117. {CMclrhalt, "clrhalt", 1},
  118. {CMname, "name", 2},
  119. {CMtmout, "timeout", 2},
  120. {CMpreset, "reset", 1},
  121. };
  122. static Dirtab usbdir[] =
  123. {
  124. "ctl", {Qctl}, 0, 0666,
  125. };
  126. char *usbmodename[] =
  127. {
  128. [OREAD] "r",
  129. [OWRITE] "w",
  130. [ORDWR] "rw",
  131. };
  132. static char *ttname[] =
  133. {
  134. [Tnone] "none",
  135. [Tctl] "control",
  136. [Tiso] "iso",
  137. [Tintr] "interrupt",
  138. [Tbulk] "bulk",
  139. };
  140. static char *spname[] =
  141. {
  142. [Fullspeed] "full",
  143. [Lowspeed] "low",
  144. [Highspeed] "high",
  145. [Nospeed] "no",
  146. };
  147. static int debug;
  148. static Hcitype hcitypes[Nhcis];
  149. static Hci* hcis[Nhcis];
  150. static QLock epslck; /* add, del, lookup endpoints */
  151. static Ep* eps[Neps]; /* all endpoints known */
  152. static int epmax; /* 1 + last endpoint index used */
  153. static int usbidgen; /* device address generator */
  154. /*
  155. * Is there something like this in a library? should it be?
  156. */
  157. char*
  158. seprintdata(char *s, char *se, uchar *d, int n)
  159. {
  160. int i, l;
  161. s = seprint(s, se, " %#p[%d]: ", d, n);
  162. l = n;
  163. if(l > 10)
  164. l = 10;
  165. for(i=0; i<l; i++)
  166. s = seprint(s, se, " %2.2ux", d[i]);
  167. if(l < n)
  168. s = seprint(s, se, "...");
  169. return s;
  170. }
  171. static int
  172. name2speed(char *name)
  173. {
  174. int i;
  175. for(i = 0; i < nelem(spname); i++)
  176. if(strcmp(name, spname[i]) == 0)
  177. return i;
  178. return Nospeed;
  179. }
  180. static int
  181. name2ttype(char *name)
  182. {
  183. int i;
  184. for(i = 0; i < nelem(ttname); i++)
  185. if(strcmp(name, ttname[i]) == 0)
  186. return i;
  187. /* may be a std. USB ep. type */
  188. i = strtol(name, nil, 0);
  189. switch(i+1){
  190. case Tctl:
  191. case Tiso:
  192. case Tbulk:
  193. case Tintr:
  194. return i+1;
  195. default:
  196. return Tnone;
  197. }
  198. }
  199. static int
  200. name2mode(char *mode)
  201. {
  202. int i;
  203. for(i = 0; i < nelem(usbmodename); i++)
  204. if(strcmp(mode, usbmodename[i]) == 0)
  205. return i;
  206. return -1;
  207. }
  208. static int
  209. qid2epidx(int q)
  210. {
  211. q = (q-Qep0dir)/4;
  212. if(q < 0 || q >= epmax || eps[q] == nil)
  213. return -1;
  214. return q;
  215. }
  216. static int
  217. isqtype(int q, int type)
  218. {
  219. if(q < Qep0dir)
  220. return 0;
  221. q -= Qep0dir;
  222. return (q & 3) == type;
  223. }
  224. void
  225. addhcitype(char* t, int (*r)(Hci*))
  226. {
  227. static int ntype;
  228. if(ntype == Nhcis)
  229. panic("too many USB host interface types");
  230. hcitypes[ntype].type = t;
  231. hcitypes[ntype].reset = r;
  232. ntype++;
  233. }
  234. static char*
  235. seprintep(char *s, char *se, Ep *ep, int all)
  236. {
  237. static char* dsnames[] = { "config", "enabled", "detached", "reset" };
  238. Udev *d;
  239. int i;
  240. int di;
  241. d = ep->dev;
  242. qlock(ep);
  243. if(waserror()){
  244. qunlock(ep);
  245. nexterror();
  246. }
  247. di = ep->dev->nb;
  248. if(all)
  249. s = seprint(s, se, "dev %d ep %d ", di, ep->nb);
  250. s = seprint(s, se, "%s", dsnames[ep->dev->state]);
  251. s = seprint(s, se, " %s", ttname[ep->ttype]);
  252. assert(ep->mode == OREAD || ep->mode == OWRITE || ep->mode == ORDWR);
  253. s = seprint(s, se, " %s", usbmodename[ep->mode]);
  254. s = seprint(s, se, " speed %s", spname[d->speed]);
  255. s = seprint(s, se, " maxpkt %ld", ep->maxpkt);
  256. s = seprint(s, se, " pollival %ld", ep->pollival);
  257. s = seprint(s, se, " samplesz %ld", ep->samplesz);
  258. s = seprint(s, se, " hz %ld", ep->hz);
  259. s = seprint(s, se, " hub %d", ep->dev->hub);
  260. s = seprint(s, se, " port %d", ep->dev->port);
  261. if(ep->inuse)
  262. s = seprint(s, se, " busy");
  263. else
  264. s = seprint(s, se, " idle");
  265. if(all){
  266. s = seprint(s, se, " load %uld", ep->load);
  267. s = seprint(s, se, " ref %ld addr %#p", ep->ref, ep);
  268. s = seprint(s, se, " idx %d", ep->idx);
  269. if(ep->name != nil)
  270. s = seprint(s, se, " name '%s'", ep->name);
  271. if(ep->tmout != 0)
  272. s = seprint(s, se, " tmout");
  273. if(ep == ep->ep0){
  274. s = seprint(s, se, " ctlrno %#x", ep->hp->ctlrno);
  275. s = seprint(s, se, " eps:");
  276. for(i = 0; i < nelem(d->eps); i++)
  277. if(d->eps[i] != nil)
  278. s = seprint(s, se, " ep%d.%d", di, i);
  279. }
  280. }
  281. if(ep->info != nil)
  282. s = seprint(s, se, "\n%s %s\n", ep->info, ep->hp->type);
  283. else
  284. s = seprint(s, se, "\n");
  285. qunlock(ep);
  286. poperror();
  287. return s;
  288. }
  289. static Ep*
  290. epalloc(Hci *hp)
  291. {
  292. Ep *ep;
  293. int i;
  294. ep = mallocz(sizeof(Ep), 1);
  295. ep->ref = 1;
  296. qlock(&epslck);
  297. for(i = 0; i < Neps; i++)
  298. if(eps[i] == nil)
  299. break;
  300. if(i == Neps){
  301. qunlock(&epslck);
  302. free(ep);
  303. print("usb: bug: too few endpoints.\n");
  304. return nil;
  305. }
  306. ep->idx = i;
  307. if(epmax <= i)
  308. epmax = i+1;
  309. eps[i] = ep;
  310. ep->hp = hp;
  311. ep->maxpkt = 8;
  312. ep->ntds = 1;
  313. ep->samplesz = ep->pollival = ep->hz = 0; /* make them void */
  314. qunlock(&epslck);
  315. return ep;
  316. }
  317. static Ep*
  318. getep(int i)
  319. {
  320. Ep *ep;
  321. if(i < 0 || i >= epmax || eps[i] == nil)
  322. return nil;
  323. qlock(&epslck);
  324. ep = eps[i];
  325. if(ep != nil)
  326. incref(ep);
  327. qunlock(&epslck);
  328. return ep;
  329. }
  330. static void
  331. putep(Ep *ep)
  332. {
  333. Udev *d;
  334. if(ep != nil && decref(ep) == 0){
  335. d = ep->dev;
  336. deprint("usb: ep%d.%d %#p released\n", d->nb, ep->nb, ep);
  337. qlock(&epslck);
  338. eps[ep->idx] = nil;
  339. if(ep->idx == epmax-1)
  340. epmax--;
  341. if(ep == ep->ep0 && ep->dev != nil && ep->dev->nb == usbidgen)
  342. usbidgen--;
  343. qunlock(&epslck);
  344. if(d != nil){
  345. qlock(ep->ep0);
  346. d->eps[ep->nb] = nil;
  347. qunlock(ep->ep0);
  348. }
  349. if(ep->ep0 != ep){
  350. putep(ep->ep0);
  351. ep->ep0 = nil;
  352. }
  353. free(ep->info);
  354. free(ep->name);
  355. free(ep);
  356. }
  357. }
  358. static void
  359. dumpeps(void)
  360. {
  361. int i;
  362. static char buf[512];
  363. char *s;
  364. char *e;
  365. Ep *ep;
  366. print("usb dump eps: epmax %d Neps %d (ref=1+ for dump):\n", epmax, Neps);
  367. for(i = 0; i < epmax; i++){
  368. s = buf;
  369. e = buf+sizeof(buf);
  370. ep = getep(i);
  371. if(ep != nil){
  372. if(waserror()){
  373. putep(ep);
  374. nexterror();
  375. }
  376. s = seprint(s, e, "ep%d.%d ", ep->dev->nb, ep->nb);
  377. seprintep(s, e, ep, 1);
  378. print("%s", buf);
  379. ep->hp->seprintep(buf, e, ep);
  380. print("%s", buf);
  381. poperror();
  382. putep(ep);
  383. }
  384. }
  385. print("usb dump hcis:\n");
  386. for(i = 0; i < Nhcis; i++)
  387. if(hcis[i] != nil)
  388. hcis[i]->dump(hcis[i]);
  389. }
  390. static int
  391. newusbid(Hci *)
  392. {
  393. int id;
  394. qlock(&epslck);
  395. id = ++usbidgen;
  396. if(id >= 0x7F)
  397. print("#u: too many device addresses; reuse them more\n");
  398. qunlock(&epslck);
  399. return id;
  400. }
  401. /*
  402. * Create endpoint 0 for a new device
  403. */
  404. static Ep*
  405. newdev(Hci *hp, int ishub, int isroot)
  406. {
  407. Ep *ep;
  408. Udev *d;
  409. ep = epalloc(hp);
  410. d = ep->dev = mallocz(sizeof(Udev), 1);
  411. d->nb = newusbid(hp);
  412. d->eps[0] = ep;
  413. ep->nb = 0;
  414. ep->toggle[0] = ep->toggle[1] = 0;
  415. d->ishub = ishub;
  416. d->isroot = isroot;
  417. if(hp->highspeed != 0)
  418. d->speed = Highspeed;
  419. else
  420. d->speed = Fullspeed;
  421. d->state = Dconfig; /* address not yet set */
  422. ep->dev = d;
  423. ep->ep0 = ep; /* no ref counted here */
  424. ep->ttype = Tctl;
  425. ep->tmout = Xfertmout;
  426. ep->mode = ORDWR;
  427. dprint("newdev %#p ep%d.%d %#p\n", d, d->nb, ep->nb, ep);
  428. return ep;
  429. }
  430. /*
  431. * Create a new endpoint for the device
  432. * accessed via the given endpoint 0.
  433. */
  434. static Ep*
  435. newdevep(Ep *ep, int i, int tt, int mode)
  436. {
  437. Ep *nep;
  438. Udev *d;
  439. d = ep->dev;
  440. if(d->eps[i] != nil)
  441. error("endpoint already in use");
  442. nep = epalloc(ep->hp);
  443. incref(ep);
  444. d->eps[i] = nep;
  445. nep->nb = i;
  446. nep->toggle[0] = nep->toggle[1] = 0;
  447. nep->ep0 = ep;
  448. nep->dev = ep->dev;
  449. nep->mode = mode;
  450. nep->ttype = tt;
  451. nep->debug = ep->debug;
  452. /* set defaults */
  453. switch(tt){
  454. case Tctl:
  455. nep->tmout = Xfertmout;
  456. break;
  457. case Tintr:
  458. nep->pollival = 10;
  459. break;
  460. case Tiso:
  461. nep->tmout = Xfertmout;
  462. nep->pollival = 10;
  463. nep->samplesz = 4;
  464. nep->hz = 44100;
  465. break;
  466. }
  467. deprint("newdevep ep%d.%d %#p\n", d->nb, nep->nb, nep);
  468. return ep;
  469. }
  470. static int
  471. epdataperm(int mode)
  472. {
  473. switch(mode){
  474. case OREAD:
  475. return 0440|DMEXCL;
  476. break;
  477. case OWRITE:
  478. return 0220|DMEXCL;
  479. break;
  480. default:
  481. return 0660|DMEXCL;
  482. }
  483. }
  484. static int
  485. usbgen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
  486. {
  487. Qid q;
  488. Dirtab *dir;
  489. int perm;
  490. char *se;
  491. Ep *ep;
  492. int nb;
  493. int mode;
  494. if(0)ddprint("usbgen q %#x s %d...", QID(c->qid), s);
  495. if(s == DEVDOTDOT){
  496. if(QID(c->qid) <= Qusbdir){
  497. mkqid(&q, Qdir, 0, QTDIR);
  498. devdir(c, q, "#u", 0, eve, 0555, dp);
  499. }else{
  500. mkqid(&q, Qusbdir, 0, QTDIR);
  501. devdir(c, q, "usb", 0, eve, 0555, dp);
  502. }
  503. if(0)ddprint("ok\n");
  504. return 1;
  505. }
  506. switch(QID(c->qid)){
  507. case Qdir: /* list #u */
  508. if(s == 0){
  509. mkqid(&q, Qusbdir, 0, QTDIR);
  510. devdir(c, q, "usb", 0, eve, 0555, dp);
  511. if(0)ddprint("ok\n");
  512. return 1;
  513. }
  514. s--;
  515. if(s < 0 || s >= epmax)
  516. goto Fail;
  517. ep = getep(s);
  518. if(ep == nil || ep->name == nil){
  519. if(ep != nil)
  520. putep(ep);
  521. if(0)ddprint("skip\n");
  522. return 0;
  523. }
  524. if(waserror()){
  525. putep(ep);
  526. nexterror();
  527. }
  528. mkqid(&q, Qep0io+s*4, 0, QTFILE);
  529. devdir(c, q, ep->name, 0, eve, epdataperm(ep->mode), dp);
  530. putep(ep);
  531. poperror();
  532. if(0)ddprint("ok\n");
  533. return 1;
  534. case Qusbdir: /* list #u/usb */
  535. Usbdir:
  536. if(s < nelem(usbdir)){
  537. dir = &usbdir[s];
  538. mkqid(&q, dir->qid.path, 0, QTFILE);
  539. devdir(c, q, dir->name, dir->length, eve, dir->perm, dp);
  540. if(0)ddprint("ok\n");
  541. return 1;
  542. }
  543. s -= nelem(usbdir);
  544. if(s < 0 || s >= epmax)
  545. goto Fail;
  546. ep = getep(s);
  547. if(ep == nil){
  548. if(0)ddprint("skip\n");
  549. return 0;
  550. }
  551. if(waserror()){
  552. putep(ep);
  553. nexterror();
  554. }
  555. se = up->genbuf+sizeof(up->genbuf);
  556. seprint(up->genbuf, se, "ep%d.%d", ep->dev->nb, ep->nb);
  557. mkqid(&q, Qep0dir+4*s, 0, QTDIR);
  558. putep(ep);
  559. poperror();
  560. devdir(c, q, up->genbuf, 0, eve, 0755, dp);
  561. if(0)ddprint("ok\n");
  562. return 1;
  563. case Qctl:
  564. s = 0;
  565. goto Usbdir;
  566. default: /* list #u/usb/epN.M */
  567. nb = qid2epidx(QID(c->qid));
  568. ep = getep(nb);
  569. if(ep == nil)
  570. goto Fail;
  571. mode = ep->mode;
  572. putep(ep);
  573. if(isqtype(QID(c->qid), Qepdir)){
  574. Epdir:
  575. switch(s){
  576. case 0:
  577. mkqid(&q, Qep0io+nb*4, 0, QTFILE);
  578. perm = epdataperm(mode);
  579. devdir(c, q, "data", 0, eve, perm, dp);
  580. break;
  581. case 1:
  582. mkqid(&q, Qep0ctl+nb*4, 0, QTFILE);
  583. devdir(c, q, "ctl", 0, eve, 0664, dp);
  584. break;
  585. default:
  586. goto Fail;
  587. }
  588. }else if(isqtype(QID(c->qid), Qepctl)){
  589. s = 1;
  590. goto Epdir;
  591. }else{
  592. s = 0;
  593. goto Epdir;
  594. }
  595. if(0)ddprint("ok\n");
  596. return 1;
  597. }
  598. Fail:
  599. if(0)ddprint("fail\n");
  600. return -1;
  601. }
  602. static Hci*
  603. hciprobe(int cardno, int ctlrno)
  604. {
  605. Hci *hp;
  606. char *type;
  607. char name[64];
  608. static int epnb = 1; /* guess the endpoint nb. for the controller */
  609. ddprint("hciprobe %d %d\n", cardno, ctlrno);
  610. hp = mallocz(sizeof(Hci), 1);
  611. hp->ctlrno = ctlrno;
  612. hp->tbdf = BUSUNKNOWN;
  613. if(cardno < 0)
  614. for(cardno = 0; cardno < Nhcis; cardno++){
  615. if(hcitypes[cardno].type == nil)
  616. break;
  617. type = hp->type;
  618. if(type==nil || *type==0)
  619. type = "uhci";
  620. if(cistrcmp(hcitypes[cardno].type, type) == 0)
  621. break;
  622. }
  623. if(cardno >= Nhcis || hcitypes[cardno].type == nil){
  624. free(hp);
  625. return nil;
  626. }
  627. dprint("%s...", hcitypes[cardno].type);
  628. if(hcitypes[cardno].reset(hp) < 0){
  629. free(hp);
  630. return nil;
  631. }
  632. snprint(name, sizeof(name), "usb%s", hcitypes[cardno].type);
  633. intrenable(Irqlo, hp->irq, hp->interrupt, hp, name);
  634. print("#u/usb/ep%d.0: %s: port %#luX irq %d\n",
  635. epnb, hcitypes[cardno].type, hp->port, hp->irq);
  636. epnb++;
  637. return hp;
  638. }
  639. static void
  640. usbreset(void)
  641. {
  642. int cardno, ctlrno;
  643. Hci *hp;
  644. dprint("usbreset\n");
  645. for(ctlrno = 0; ctlrno < Nhcis; ctlrno++)
  646. if((hp = hciprobe(-1, ctlrno)) != nil)
  647. hcis[ctlrno] = hp;
  648. cardno = ctlrno = 0;
  649. while(cardno < Nhcis && ctlrno < Nhcis && hcitypes[cardno].type != nil)
  650. if(hcis[ctlrno] != nil)
  651. ctlrno++;
  652. else{
  653. hp = hciprobe(cardno, ctlrno);
  654. if(hp == nil)
  655. cardno++;
  656. hcis[ctlrno++] = hp;
  657. }
  658. if(hcis[Nhcis-1] != nil)
  659. print("usbreset: bug: Nhcis too small\n");
  660. }
  661. static void
  662. usbinit(void)
  663. {
  664. Hci *hp;
  665. int ctlrno;
  666. Ep *d;
  667. char info[40];
  668. dprint("usbinit\n");
  669. for(ctlrno = 0; ctlrno < Nhcis; ctlrno++){
  670. hp = hcis[ctlrno];
  671. if(hp != nil){
  672. if(hp->init != nil)
  673. hp->init(hp);
  674. d = newdev(hp, 1, 1); /* new root hub */
  675. d->dev->state = Denabled; /* although addr == 0 */
  676. d->maxpkt = 64;
  677. snprint(info, sizeof(info), "ports %d", hp->nports);
  678. kstrdup(&d->info, info);
  679. }
  680. }
  681. }
  682. static Chan*
  683. usbattach(char *spec)
  684. {
  685. return devattach(L'u', spec);
  686. }
  687. static Walkqid*
  688. usbwalk(Chan *c, Chan *nc, char **name, int nname)
  689. {
  690. return devwalk(c, nc, name, nname, nil, 0, usbgen);
  691. }
  692. static int
  693. usbstat(Chan *c, uchar *db, int n)
  694. {
  695. return devstat(c, db, n, nil, 0, usbgen);
  696. }
  697. /*
  698. * µs for the given transfer, for bandwidth allocation.
  699. * This is a very rough worst case for what 5.11.3
  700. * of the usb 2.0 spec says.
  701. * Also, we are using maxpkt and not actual transfer sizes.
  702. * Only when we are sure we
  703. * are not exceeding b/w might we consider adjusting it.
  704. */
  705. static ulong
  706. usbload(int speed, int maxpkt)
  707. {
  708. enum{ Hostns = 1000, Hubns = 333 };
  709. ulong l;
  710. ulong bs;
  711. l = 0;
  712. bs = 10UL * maxpkt;
  713. switch(speed){
  714. case Highspeed:
  715. l = 55*8*2 + 2 * (3 + bs) + Hostns;
  716. break;
  717. case Fullspeed:
  718. l = 9107 + 84 * (4 + bs) + Hostns;
  719. break;
  720. case Lowspeed:
  721. l = 64107 + 2 * Hubns + 667 * (3 + bs) + Hostns;
  722. break;
  723. default:
  724. print("usbload: bad speed %d\n", speed);
  725. /* let it run */
  726. }
  727. return l / 1000UL; /* in µs */
  728. }
  729. static Chan*
  730. usbopen(Chan *c, int omode)
  731. {
  732. int q;
  733. Ep *ep;
  734. int mode;
  735. mode = openmode(omode);
  736. q = QID(c->qid);
  737. if(q >= Qep0dir && qid2epidx(q) < 0)
  738. error(Eio);
  739. if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
  740. return devopen(c, omode, nil, 0, usbgen);
  741. ep = getep(qid2epidx(q));
  742. if(ep == nil)
  743. error(Eio);
  744. deprint("usbopen q %#x fid %d omode %d\n", q, c->fid, mode);
  745. if(waserror()){
  746. putep(ep);
  747. nexterror();
  748. }
  749. qlock(ep);
  750. if(ep->inuse){
  751. qunlock(ep);
  752. error(Einuse);
  753. }
  754. ep->inuse = 1;
  755. qunlock(ep);
  756. if(waserror()){
  757. ep->inuse = 0;
  758. nexterror();
  759. }
  760. if(mode != OREAD && ep->mode == OREAD)
  761. error(Eperm);
  762. if(mode != OWRITE && ep->mode == OWRITE)
  763. error(Eperm);
  764. if(ep->ttype == Tnone)
  765. error(Enotconf);
  766. ep->clrhalt = 0;
  767. ep->rhrepl = -1;
  768. if(ep->load == 0)
  769. ep->load = usbload(ep->dev->speed, ep->maxpkt);
  770. ep->hp->epopen(ep);
  771. poperror(); /* ep->inuse */
  772. poperror(); /* don't putep(): ref kept for fid using the ep. */
  773. c->mode = mode;
  774. c->flag |= COPEN;
  775. c->offset = 0;
  776. c->aux = nil; /* paranoia */
  777. return c;
  778. }
  779. static void
  780. epclose(Ep *ep)
  781. {
  782. qlock(ep);
  783. if(waserror()){
  784. qunlock(ep);
  785. nexterror();
  786. }
  787. if(ep->inuse){
  788. ep->hp->epclose(ep);
  789. ep->inuse = 0;
  790. }
  791. qunlock(ep);
  792. poperror();
  793. }
  794. static void
  795. usbclose(Chan *c)
  796. {
  797. int q;
  798. Ep *ep;
  799. q = QID(c->qid);
  800. if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
  801. return;
  802. ep = getep(qid2epidx(q));
  803. if(ep == nil)
  804. return;
  805. deprint("usbclose q %#x fid %d ref %ld\n", q, c->fid, ep->ref);
  806. if(waserror()){
  807. putep(ep);
  808. nexterror();
  809. }
  810. if(c->flag & COPEN){
  811. free(c->aux);
  812. c->aux = nil;
  813. epclose(ep);
  814. putep(ep); /* release ref kept since usbopen */
  815. c->flag &= ~COPEN;
  816. }
  817. poperror();
  818. putep(ep);
  819. }
  820. static long
  821. ctlread(Chan *c, void *a, long n, vlong offset)
  822. {
  823. int q;
  824. char *s;
  825. char *us;
  826. char *se;
  827. Ep *ep;
  828. int i;
  829. q = QID(c->qid);
  830. us = s = smalloc(READSTR);
  831. se = s + READSTR;
  832. if(waserror()){
  833. free(us);
  834. nexterror();
  835. }
  836. if(q == Qctl)
  837. for(i = 0; i < epmax; i++){
  838. ep = getep(i);
  839. if(ep != nil){
  840. if(waserror()){
  841. putep(ep);
  842. nexterror();
  843. }
  844. s = seprint(s, se, "ep%d.%d ", ep->dev->nb, ep->nb);
  845. s = seprintep(s, se, ep, 0);
  846. poperror();
  847. }
  848. putep(ep);
  849. }
  850. else{
  851. ep = getep(qid2epidx(q));
  852. if(ep == nil)
  853. error(Eio);
  854. if(waserror()){
  855. putep(ep);
  856. nexterror();
  857. }
  858. if(c->aux != nil){
  859. /* After a new endpoint request we read
  860. * the new endpoint name back.
  861. */
  862. strecpy(s, se, c->aux);
  863. free(c->aux);
  864. c->aux = nil;
  865. }else
  866. seprintep(s, se, ep, 0);
  867. poperror();
  868. putep(ep);
  869. }
  870. n = readstr(offset, a, n, us);
  871. poperror();
  872. free(us);
  873. return n;
  874. }
  875. /*
  876. * Fake root hub emulation.
  877. */
  878. static long
  879. rhubread(Ep *ep, void *a, long n)
  880. {
  881. char *b;
  882. if(ep->dev->isroot == 0 || ep->nb != 0 || n < 2)
  883. return -1;
  884. if(ep->rhrepl < 0)
  885. return -1;
  886. b = a;
  887. memset(b, 0, n);
  888. PUT2(b, ep->rhrepl);
  889. ep->rhrepl = -1;
  890. return n;
  891. }
  892. static long
  893. rhubwrite(Ep *ep, void *a, long n)
  894. {
  895. uchar *s;
  896. int cmd;
  897. int feature;
  898. int port;
  899. Hci *hp;
  900. if(ep->dev == nil || ep->dev->isroot == 0 || ep->nb != 0)
  901. return -1;
  902. if(n != Rsetuplen)
  903. error("root hub is a toy hub");
  904. ep->rhrepl = -1;
  905. s = a;
  906. if(s[Rtype] != (Rh2d|Rclass|Rother) && s[Rtype] != (Rd2h|Rclass|Rother))
  907. error("root hub is a toy hub");
  908. hp = ep->hp;
  909. cmd = s[Rreq];
  910. feature = GET2(s+Rvalue);
  911. port = GET2(s+Rindex);
  912. if(port < 1 || port > hp->nports)
  913. error("bad hub port number");
  914. switch(feature){
  915. case Rportenable:
  916. ep->rhrepl = hp->portenable(hp, port, cmd == Rsetfeature);
  917. break;
  918. case Rportreset:
  919. ep->rhrepl = hp->portreset(hp, port, cmd == Rsetfeature);
  920. break;
  921. case Rgetstatus:
  922. ep->rhrepl = hp->portstatus(hp, port);
  923. break;
  924. default:
  925. ep->rhrepl = 0;
  926. }
  927. return n;
  928. }
  929. static long
  930. usbread(Chan *c, void *a, long n, vlong offset)
  931. {
  932. int q;
  933. Ep *ep;
  934. int nr;
  935. q = QID(c->qid);
  936. if(c->qid.type == QTDIR)
  937. return devdirread(c, a, n, nil, 0, usbgen);
  938. if(q == Qctl || isqtype(q, Qepctl))
  939. return ctlread(c, a, n, offset);
  940. ep = getep(qid2epidx(q));
  941. if(ep == nil)
  942. error(Eio);
  943. if(waserror()){
  944. putep(ep);
  945. nexterror();
  946. }
  947. if(ep->dev->state == Ddetach)
  948. error(Edetach);
  949. if(ep->mode == OWRITE || ep->inuse == 0)
  950. error(Ebadusefd);
  951. switch(ep->ttype){
  952. case Tnone:
  953. error("endpoint not configured");
  954. case Tctl:
  955. nr = rhubread(ep, a, n);
  956. if(nr >= 0){
  957. n = nr;
  958. break;
  959. }
  960. /* else fall */
  961. default:
  962. ddeprint("\nusbread q %#x fid %d cnt %ld off %lld\n",q,c->fid,n,offset);
  963. n = ep->hp->epread(ep, a, n);
  964. break;
  965. }
  966. poperror();
  967. putep(ep);
  968. return n;
  969. }
  970. static long
  971. pow2(int n)
  972. {
  973. return 1 << n;
  974. }
  975. static void
  976. setmaxpkt(Ep *ep, char* s)
  977. {
  978. long spp; /* samples per packet */
  979. if(ep->dev->speed == Highspeed)
  980. spp = (ep->hz * ep->pollival * ep->ntds + 7999) / 8000;
  981. else
  982. spp = (ep->hz * ep->pollival + 999) / 1000;
  983. ep->maxpkt = spp * ep->samplesz;
  984. deprint("usb: %s: setmaxpkt: hz %ld poll %ld"
  985. " ntds %d %s speed -> spp %ld maxpkt %ld\n", s,
  986. ep->hz, ep->pollival, ep->ntds, spname[ep->dev->speed],
  987. spp, ep->maxpkt);
  988. if(ep->maxpkt > 1024){
  989. print("usb: %s: maxpkt %ld > 1024. truncating\n", s, ep->maxpkt);
  990. ep->maxpkt = 1024;
  991. }
  992. }
  993. /*
  994. * Many endpoint ctls. simply update the portable representation
  995. * of the endpoint. The actual controller driver will look
  996. * at them to setup the endpoints as dictated.
  997. */
  998. static long
  999. epctl(Ep *ep, Chan *c, void *a, long n)
  1000. {
  1001. int i, l, mode, nb, tt;
  1002. char *b, *s;
  1003. Cmdbuf *cb;
  1004. Cmdtab *ct;
  1005. Ep *nep;
  1006. Udev *d;
  1007. static char *Info = "info ";
  1008. d = ep->dev;
  1009. cb = parsecmd(a, n);
  1010. if(waserror()){
  1011. free(cb);
  1012. nexterror();
  1013. }
  1014. ct = lookupcmd(cb, epctls, nelem(epctls));
  1015. if(ct == nil)
  1016. error(Ebadctl);
  1017. i = ct->index;
  1018. if(i == CMnew || i == CMspeed || i == CMhub || i == CMpreset)
  1019. if(ep != ep->ep0)
  1020. error("allowed only on a setup endpoint");
  1021. if(i != CMclrhalt && i != CMdetach && i != CMdebugep && i != CMname)
  1022. if(ep != ep->ep0 && ep->inuse != 0)
  1023. error("must configure before using");
  1024. switch(i){
  1025. case CMnew:
  1026. deprint("usb epctl %s\n", cb->f[0]);
  1027. nb = strtol(cb->f[1], nil, 0);
  1028. if(nb < 0 || nb >= Ndeveps)
  1029. error("bad endpoint number");
  1030. tt = name2ttype(cb->f[2]);
  1031. if(tt == Tnone)
  1032. error("unknown endpoint type");
  1033. mode = name2mode(cb->f[3]);
  1034. if(mode < 0)
  1035. error("unknown i/o mode");
  1036. newdevep(ep, nb, tt, mode);
  1037. break;
  1038. case CMnewdev:
  1039. deprint("usb epctl %s\n", cb->f[0]);
  1040. if(ep != ep->ep0 || d->ishub == 0)
  1041. error("not a hub setup endpoint");
  1042. l = name2speed(cb->f[1]);
  1043. if(l == Nospeed)
  1044. error("speed must be full|low|high");
  1045. nep = newdev(ep->hp, 0, 0);
  1046. nep->dev->speed = l;
  1047. if(nep->dev->speed != Lowspeed)
  1048. nep->maxpkt = 64; /* assume full speed */
  1049. nep->dev->hub = d->nb;
  1050. nep->dev->port = atoi(cb->f[2]);
  1051. /* next read request will read
  1052. * the name for the new endpoint
  1053. */
  1054. l = sizeof(up->genbuf);
  1055. snprint(up->genbuf, l, "ep%d.%d", nep->dev->nb, nep->nb);
  1056. kstrdup(&c->aux, up->genbuf);
  1057. break;
  1058. case CMhub:
  1059. deprint("usb epctl %s\n", cb->f[0]);
  1060. d->ishub = 1;
  1061. break;
  1062. case CMspeed:
  1063. l = name2speed(cb->f[1]);
  1064. deprint("usb epctl %s %d\n", cb->f[0], l);
  1065. if(l == Nospeed)
  1066. error("speed must be full|low|high");
  1067. qlock(ep->ep0);
  1068. d->speed = l;
  1069. qunlock(ep->ep0);
  1070. break;
  1071. case CMmaxpkt:
  1072. l = strtoul(cb->f[1], nil, 0);
  1073. deprint("usb epctl %s %d\n", cb->f[0], l);
  1074. if(l < 1 || l > 1024)
  1075. error("maxpkt not in [1:1024]");
  1076. qlock(ep);
  1077. ep->maxpkt = l;
  1078. qunlock(ep);
  1079. break;
  1080. case CMntds:
  1081. l = strtoul(cb->f[1], nil, 0);
  1082. deprint("usb epctl %s %d\n", cb->f[0], l);
  1083. if(l < 1 || l > 3)
  1084. error("ntds not in [1:3]");
  1085. qlock(ep);
  1086. ep->ntds = l;
  1087. qunlock(ep);
  1088. break;
  1089. case CMpollival:
  1090. if(ep->ttype != Tintr && ep->ttype != Tiso)
  1091. error("not an intr or iso endpoint");
  1092. l = strtoul(cb->f[1], nil, 0);
  1093. deprint("usb epctl %s %d\n", cb->f[0], l);
  1094. if(ep->ttype == Tiso ||
  1095. (ep->ttype == Tintr && ep->dev->speed == Highspeed)){
  1096. if(l < 1 || l > 16)
  1097. error("pollival power not in [1:16]");
  1098. l = pow2(l-1);
  1099. }else
  1100. if(l < 1 || l > 255)
  1101. error("pollival not in [1:255]");
  1102. qlock(ep);
  1103. ep->pollival = l;
  1104. if(ep->ttype == Tiso)
  1105. setmaxpkt(ep, "pollival");
  1106. qunlock(ep);
  1107. break;
  1108. case CMsamplesz:
  1109. if(ep->ttype != Tiso)
  1110. error("not an iso endpoint");
  1111. l = strtoul(cb->f[1], nil, 0);
  1112. deprint("usb epctl %s %d\n", cb->f[0], l);
  1113. if(l <= 0 || l > 8)
  1114. error("samplesz not in [1:8]");
  1115. qlock(ep);
  1116. ep->samplesz = l;
  1117. setmaxpkt(ep, "samplesz");
  1118. qunlock(ep);
  1119. break;
  1120. case CMhz:
  1121. if(ep->ttype != Tiso)
  1122. error("not an iso endpoint");
  1123. l = strtoul(cb->f[1], nil, 0);
  1124. deprint("usb epctl %s %d\n", cb->f[0], l);
  1125. if(l <= 0 || l > 100000)
  1126. error("hz not in [1:100000]");
  1127. qlock(ep);
  1128. ep->hz = l;
  1129. setmaxpkt(ep, "hz");
  1130. qunlock(ep);
  1131. break;
  1132. case CMclrhalt:
  1133. qlock(ep);
  1134. deprint("usb epctl %s\n", cb->f[0]);
  1135. ep->clrhalt = 1;
  1136. qunlock(ep);
  1137. break;
  1138. case CMinfo:
  1139. deprint("usb epctl %s\n", cb->f[0]);
  1140. l = strlen(Info);
  1141. s = a;
  1142. if(n < l+2 || strncmp(Info, s, l) != 0)
  1143. error(Ebadctl);
  1144. if(n > 1024)
  1145. n = 1024;
  1146. b = smalloc(n);
  1147. memmove(b, s+l, n-l);
  1148. b[n-l] = 0;
  1149. if(b[n-l-1] == '\n')
  1150. b[n-l-1] = 0;
  1151. qlock(ep);
  1152. free(ep->info);
  1153. ep->info = b;
  1154. qunlock(ep);
  1155. break;
  1156. case CMaddress:
  1157. deprint("usb epctl %s\n", cb->f[0]);
  1158. ep->dev->state = Denabled;
  1159. break;
  1160. case CMdetach:
  1161. if(ep->dev->isroot != 0)
  1162. error("can't detach a root hub");
  1163. deprint("usb epctl %s ep%d.%d\n",
  1164. cb->f[0], ep->dev->nb, ep->nb);
  1165. ep->dev->state = Ddetach;
  1166. /* Release file system ref. for its endpoints */
  1167. for(i = 0; i < nelem(ep->dev->eps); i++)
  1168. putep(ep->dev->eps[i]);
  1169. break;
  1170. case CMdebugep:
  1171. if(strcmp(cb->f[1], "on") == 0)
  1172. ep->debug = 1;
  1173. else if(strcmp(cb->f[1], "off") == 0)
  1174. ep->debug = 0;
  1175. else
  1176. ep->debug = strtoul(cb->f[1], nil, 0);
  1177. print("usb: ep%d.%d debug %d\n",
  1178. ep->dev->nb, ep->nb, ep->debug);
  1179. break;
  1180. case CMname:
  1181. deprint("usb epctl %s %s\n", cb->f[0], cb->f[1]);
  1182. validname(cb->f[1], 0);
  1183. kstrdup(&ep->name, cb->f[1]);
  1184. break;
  1185. case CMtmout:
  1186. deprint("usb epctl %s\n", cb->f[0]);
  1187. if(ep->ttype == Tiso || ep->ttype == Tctl)
  1188. error("ctl ignored for this endpoint type");
  1189. ep->tmout = strtoul(cb->f[1], nil, 0);
  1190. if(ep->tmout != 0 && ep->tmout < Xfertmout)
  1191. ep->tmout = Xfertmout;
  1192. break;
  1193. case CMpreset:
  1194. deprint("usb epctl %s\n", cb->f[0]);
  1195. if(ep->ttype != Tctl)
  1196. error("not a control endpoint");
  1197. if(ep->dev->state != Denabled)
  1198. error("forbidden on devices not enabled");
  1199. ep->dev->state = Dreset;
  1200. break;
  1201. default:
  1202. panic("usb: unknown epctl %d", ct->index);
  1203. }
  1204. free(cb);
  1205. poperror();
  1206. return n;
  1207. }
  1208. static long
  1209. usbctl(void *a, long n)
  1210. {
  1211. Cmdtab *ct;
  1212. Cmdbuf *cb;
  1213. Ep *ep;
  1214. int i;
  1215. cb = parsecmd(a, n);
  1216. if(waserror()){
  1217. free(cb);
  1218. nexterror();
  1219. }
  1220. ct = lookupcmd(cb, usbctls, nelem(usbctls));
  1221. dprint("usb ctl %s\n", cb->f[0]);
  1222. switch(ct->index){
  1223. case CMdebug:
  1224. if(strcmp(cb->f[1], "on") == 0)
  1225. debug = 1;
  1226. else if(strcmp(cb->f[1], "off") == 0)
  1227. debug = 0;
  1228. else
  1229. debug = strtol(cb->f[1], nil, 0);
  1230. print("usb: debug %d\n", debug);
  1231. for(i = 0; i < epmax; i++)
  1232. if((ep = getep(i)) != nil){
  1233. ep->hp->debug(ep->hp, debug);
  1234. putep(ep);
  1235. }
  1236. break;
  1237. case CMdump:
  1238. dumpeps();
  1239. break;
  1240. }
  1241. free(cb);
  1242. poperror();
  1243. return n;
  1244. }
  1245. static long
  1246. ctlwrite(Chan *c, void *a, long n)
  1247. {
  1248. int q;
  1249. Ep *ep;
  1250. q = QID(c->qid);
  1251. if(q == Qctl)
  1252. return usbctl(a, n);
  1253. ep = getep(qid2epidx(q));
  1254. if(ep == nil)
  1255. error(Eio);
  1256. if(waserror()){
  1257. putep(ep);
  1258. nexterror();
  1259. }
  1260. if(ep->dev->state == Ddetach)
  1261. error(Edetach);
  1262. if(isqtype(q, Qepctl) && c->aux != nil){
  1263. /* Be sure we don't keep a cloned ep name */
  1264. free(c->aux);
  1265. c->aux = nil;
  1266. error("read, not write, expected");
  1267. }
  1268. n = epctl(ep, c, a, n);
  1269. putep(ep);
  1270. poperror();
  1271. return n;
  1272. }
  1273. static long
  1274. usbwrite(Chan *c, void *a, long n, vlong off)
  1275. {
  1276. int nr, q;
  1277. Ep *ep;
  1278. if(c->qid.type == QTDIR)
  1279. error(Eisdir);
  1280. q = QID(c->qid);
  1281. if(q == Qctl || isqtype(q, Qepctl))
  1282. return ctlwrite(c, a, n);
  1283. ep = getep(qid2epidx(q));
  1284. if(ep == nil)
  1285. error(Eio);
  1286. if(waserror()){
  1287. putep(ep);
  1288. nexterror();
  1289. }
  1290. if(ep->dev->state == Ddetach)
  1291. error(Edetach);
  1292. if(ep->mode == OREAD || ep->inuse == 0)
  1293. error(Ebadusefd);
  1294. switch(ep->ttype){
  1295. case Tnone:
  1296. error("endpoint not configured");
  1297. case Tctl:
  1298. nr = rhubwrite(ep, a, n);
  1299. if(nr >= 0){
  1300. n = nr;
  1301. break;
  1302. }
  1303. /* else fall */
  1304. default:
  1305. ddeprint("\nusbwrite q %#x fid %d cnt %ld off %lld\n",q, c->fid, n, off);
  1306. ep->hp->epwrite(ep, a, n);
  1307. }
  1308. putep(ep);
  1309. poperror();
  1310. return n;
  1311. }
  1312. void
  1313. usbshutdown(void)
  1314. {
  1315. Hci *hp;
  1316. int i;
  1317. for(i = 0; i < Nhcis; i++){
  1318. hp = hcis[i];
  1319. if(hp == nil)
  1320. continue;
  1321. if(hp->shutdown == nil)
  1322. print("#u: no shutdown function for %s\n", hp->type);
  1323. else
  1324. hp->shutdown(hp);
  1325. }
  1326. }
  1327. Dev usbdevtab = {
  1328. L'u',
  1329. "usb",
  1330. usbreset,
  1331. usbinit,
  1332. usbshutdown,
  1333. usbattach,
  1334. usbwalk,
  1335. usbstat,
  1336. usbopen,
  1337. devcreate,
  1338. usbclose,
  1339. usbread,
  1340. devbread,
  1341. usbwrite,
  1342. devbwrite,
  1343. devremove,
  1344. devwstat,
  1345. };