devusb.c 29 KB

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