devusb.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. if(cardno < 0)
  613. for(cardno = 0; cardno < Nhcis; cardno++){
  614. if(hcitypes[cardno].type == nil)
  615. break;
  616. type = hp->type;
  617. if(type==nil || *type==0)
  618. type = "uhci";
  619. if(cistrcmp(hcitypes[cardno].type, type) == 0)
  620. break;
  621. }
  622. if(cardno >= Nhcis || hcitypes[cardno].type == nil){
  623. free(hp);
  624. return nil;
  625. }
  626. dprint("%s...", hcitypes[cardno].type);
  627. if(hcitypes[cardno].reset(hp) < 0){
  628. free(hp);
  629. return nil;
  630. }
  631. snprint(name, sizeof(name), "usb%s", hcitypes[cardno].type);
  632. intrenable(hp->irq, hp->interrupt, hp, UNKNOWN, name);
  633. print("#u/usb/ep%d.0: %s: port %#luX irq %d\n",
  634. epnb, hcitypes[cardno].type, hp->port, hp->irq);
  635. epnb++;
  636. return hp;
  637. }
  638. static void
  639. usbreset(void)
  640. {
  641. int cardno, ctlrno;
  642. Hci *hp;
  643. dprint("usbreset\n");
  644. for(ctlrno = 0; ctlrno < Nhcis; ctlrno++)
  645. if((hp = hciprobe(-1, ctlrno)) != nil)
  646. hcis[ctlrno] = hp;
  647. cardno = ctlrno = 0;
  648. while(cardno < Nhcis && ctlrno < Nhcis && hcitypes[cardno].type != nil)
  649. if(hcis[ctlrno] != nil)
  650. ctlrno++;
  651. else{
  652. hp = hciprobe(cardno, ctlrno);
  653. if(hp == nil)
  654. cardno++;
  655. hcis[ctlrno++] = hp;
  656. }
  657. if(hcis[Nhcis-1] != nil)
  658. print("usbreset: bug: Nhcis too small\n");
  659. }
  660. static void
  661. usbinit(void)
  662. {
  663. Hci *hp;
  664. int ctlrno;
  665. Ep *d;
  666. char info[40];
  667. dprint("usbinit\n");
  668. for(ctlrno = 0; ctlrno < Nhcis; ctlrno++){
  669. hp = hcis[ctlrno];
  670. if(hp != nil){
  671. if(hp->init != nil)
  672. hp->init(hp);
  673. d = newdev(hp, 1, 1); /* new root hub */
  674. d->dev->state = Denabled; /* although addr == 0 */
  675. d->maxpkt = 64;
  676. snprint(info, sizeof(info), "ports %d", hp->nports);
  677. kstrdup(&d->info, info);
  678. }
  679. }
  680. }
  681. static Chan*
  682. usbattach(char *spec)
  683. {
  684. return devattach(L'u', spec);
  685. }
  686. static Walkqid*
  687. usbwalk(Chan *c, Chan *nc, char **name, int nname)
  688. {
  689. return devwalk(c, nc, name, nname, nil, 0, usbgen);
  690. }
  691. static int
  692. usbstat(Chan *c, uchar *db, int n)
  693. {
  694. return devstat(c, db, n, nil, 0, usbgen);
  695. }
  696. /*
  697. * µs for the given transfer, for bandwidth allocation.
  698. * This is a very rough worst case for what 5.11.3
  699. * of the usb 2.0 spec says.
  700. * Also, we are using maxpkt and not actual transfer sizes.
  701. * Only when we are sure we
  702. * are not exceeding b/w might we consider adjusting it.
  703. */
  704. static ulong
  705. usbload(int speed, int maxpkt)
  706. {
  707. enum{ Hostns = 1000, Hubns = 333 };
  708. ulong l;
  709. ulong bs;
  710. l = 0;
  711. bs = 10UL * maxpkt;
  712. switch(speed){
  713. case Highspeed:
  714. l = 55*8*2 + 2 * (3 + bs) + Hostns;
  715. break;
  716. case Fullspeed:
  717. l = 9107 + 84 * (4 + bs) + Hostns;
  718. break;
  719. case Lowspeed:
  720. l = 64107 + 2 * Hubns + 667 * (3 + bs) + Hostns;
  721. break;
  722. default:
  723. print("usbload: bad speed %d\n", speed);
  724. /* let it run */
  725. }
  726. return l / 1000UL; /* in µs */
  727. }
  728. static Chan*
  729. usbopen(Chan *c, int omode)
  730. {
  731. int q;
  732. Ep *ep;
  733. int mode;
  734. mode = openmode(omode);
  735. q = QID(c->qid);
  736. if(q >= Qep0dir && qid2epidx(q) < 0)
  737. error(Eio);
  738. if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
  739. return devopen(c, omode, nil, 0, usbgen);
  740. ep = getep(qid2epidx(q));
  741. if(ep == nil)
  742. error(Eio);
  743. deprint("usbopen q %#x fid %d omode %d\n", q, c->fid, mode);
  744. if(waserror()){
  745. putep(ep);
  746. nexterror();
  747. }
  748. qlock(ep);
  749. if(ep->inuse){
  750. qunlock(ep);
  751. error(Einuse);
  752. }
  753. ep->inuse = 1;
  754. qunlock(ep);
  755. if(waserror()){
  756. ep->inuse = 0;
  757. nexterror();
  758. }
  759. if(mode != OREAD && ep->mode == OREAD)
  760. error(Eperm);
  761. if(mode != OWRITE && ep->mode == OWRITE)
  762. error(Eperm);
  763. if(ep->ttype == Tnone)
  764. error(Enotconf);
  765. ep->clrhalt = 0;
  766. ep->rhrepl = -1;
  767. if(ep->load == 0)
  768. ep->load = usbload(ep->dev->speed, ep->maxpkt);
  769. ep->hp->epopen(ep);
  770. poperror(); /* ep->inuse */
  771. poperror(); /* don't putep(): ref kept for fid using the ep. */
  772. c->mode = mode;
  773. c->flag |= COPEN;
  774. c->offset = 0;
  775. c->aux = nil; /* paranoia */
  776. return c;
  777. }
  778. static void
  779. epclose(Ep *ep)
  780. {
  781. qlock(ep);
  782. if(waserror()){
  783. qunlock(ep);
  784. nexterror();
  785. }
  786. if(ep->inuse){
  787. ep->hp->epclose(ep);
  788. ep->inuse = 0;
  789. }
  790. qunlock(ep);
  791. poperror();
  792. }
  793. static void
  794. usbclose(Chan *c)
  795. {
  796. int q;
  797. Ep *ep;
  798. q = QID(c->qid);
  799. if(q < Qep0dir || isqtype(q, Qepctl) || isqtype(q, Qepdir))
  800. return;
  801. ep = getep(qid2epidx(q));
  802. if(ep == nil)
  803. return;
  804. deprint("usbclose q %#x fid %d ref %ld\n", q, c->fid, ep->ref);
  805. if(waserror()){
  806. putep(ep);
  807. nexterror();
  808. }
  809. if(c->flag & COPEN){
  810. free(c->aux);
  811. c->aux = nil;
  812. epclose(ep);
  813. putep(ep); /* release ref kept since usbopen */
  814. c->flag &= ~COPEN;
  815. }
  816. poperror();
  817. putep(ep);
  818. }
  819. static long
  820. ctlread(Chan *c, void *a, long n, vlong offset)
  821. {
  822. int q;
  823. char *s;
  824. char *us;
  825. char *se;
  826. Ep *ep;
  827. int i;
  828. q = QID(c->qid);
  829. us = s = smalloc(READSTR);
  830. se = s + READSTR;
  831. if(waserror()){
  832. free(us);
  833. nexterror();
  834. }
  835. if(q == Qctl)
  836. for(i = 0; i < epmax; i++){
  837. ep = getep(i);
  838. if(ep != nil){
  839. if(waserror()){
  840. putep(ep);
  841. nexterror();
  842. }
  843. s = seprint(s, se, "ep%d.%d ", ep->dev->nb, ep->nb);
  844. s = seprintep(s, se, ep, 0);
  845. poperror();
  846. }
  847. putep(ep);
  848. }
  849. else{
  850. ep = getep(qid2epidx(q));
  851. if(ep == nil)
  852. error(Eio);
  853. if(waserror()){
  854. putep(ep);
  855. nexterror();
  856. }
  857. if(c->aux != nil){
  858. /* After a new endpoint request we read
  859. * the new endpoint name back.
  860. */
  861. strecpy(s, se, c->aux);
  862. free(c->aux);
  863. c->aux = nil;
  864. }else
  865. seprintep(s, se, ep, 0);
  866. poperror();
  867. putep(ep);
  868. }
  869. n = readstr(offset, a, n, us);
  870. poperror();
  871. free(us);
  872. return n;
  873. }
  874. /*
  875. * Fake root hub emulation.
  876. */
  877. static long
  878. rhubread(Ep *ep, void *a, long n)
  879. {
  880. char *b;
  881. if(ep->dev->isroot == 0 || ep->nb != 0 || n < 2)
  882. return -1;
  883. if(ep->rhrepl < 0)
  884. return -1;
  885. b = a;
  886. memset(b, 0, n);
  887. PUT2(b, ep->rhrepl);
  888. ep->rhrepl = -1;
  889. return n;
  890. }
  891. static long
  892. rhubwrite(Ep *ep, void *a, long n)
  893. {
  894. uchar *s;
  895. int cmd;
  896. int feature;
  897. int port;
  898. Hci *hp;
  899. if(ep->dev == nil || ep->dev->isroot == 0 || ep->nb != 0)
  900. return -1;
  901. if(n != Rsetuplen)
  902. error("root hub is a toy hub");
  903. ep->rhrepl = -1;
  904. s = a;
  905. if(s[Rtype] != (Rh2d|Rclass|Rother) && s[Rtype] != (Rd2h|Rclass|Rother))
  906. error("root hub is a toy hub");
  907. hp = ep->hp;
  908. cmd = s[Rreq];
  909. feature = GET2(s+Rvalue);
  910. port = GET2(s+Rindex);
  911. if(port < 1 || port > hp->nports)
  912. error("bad hub port number");
  913. switch(feature){
  914. case Rportenable:
  915. ep->rhrepl = hp->portenable(hp, port, cmd == Rsetfeature);
  916. break;
  917. case Rportreset:
  918. ep->rhrepl = hp->portreset(hp, port, cmd == Rsetfeature);
  919. break;
  920. case Rgetstatus:
  921. ep->rhrepl = hp->portstatus(hp, port);
  922. break;
  923. default:
  924. ep->rhrepl = 0;
  925. }
  926. return n;
  927. }
  928. static long
  929. usbread(Chan *c, void *a, long n, vlong offset)
  930. {
  931. int q;
  932. Ep *ep;
  933. int nr;
  934. q = QID(c->qid);
  935. if(c->qid.type == QTDIR)
  936. return devdirread(c, a, n, nil, 0, usbgen);
  937. if(q == Qctl || isqtype(q, Qepctl))
  938. return ctlread(c, a, n, offset);
  939. ep = getep(qid2epidx(q));
  940. if(ep == nil)
  941. error(Eio);
  942. if(waserror()){
  943. putep(ep);
  944. nexterror();
  945. }
  946. if(ep->dev->state == Ddetach)
  947. error(Edetach);
  948. if(ep->mode == OWRITE || ep->inuse == 0)
  949. error(Ebadusefd);
  950. switch(ep->ttype){
  951. case Tnone:
  952. error("endpoint not configured");
  953. case Tctl:
  954. nr = rhubread(ep, a, n);
  955. if(nr >= 0){
  956. n = nr;
  957. break;
  958. }
  959. /* else fall */
  960. default:
  961. ddeprint("\nusbread q %#x fid %d cnt %ld off %lld\n",q,c->fid,n,offset);
  962. n = ep->hp->epread(ep, a, n);
  963. break;
  964. }
  965. poperror();
  966. putep(ep);
  967. return n;
  968. }
  969. static long
  970. pow2(int n)
  971. {
  972. return 1 << n;
  973. }
  974. static void
  975. setmaxpkt(Ep *ep, char* s)
  976. {
  977. long spp; /* samples per packet */
  978. if(ep->dev->speed == Highspeed)
  979. spp = (ep->hz * ep->pollival * ep->ntds + 7999) / 8000;
  980. else
  981. spp = (ep->hz * ep->pollival + 999) / 1000;
  982. ep->maxpkt = spp * ep->samplesz;
  983. deprint("usb: %s: setmaxpkt: hz %ld poll %ld"
  984. " ntds %d %s speed -> spp %ld maxpkt %ld\n", s,
  985. ep->hz, ep->pollival, ep->ntds, spname[ep->dev->speed],
  986. spp, ep->maxpkt);
  987. if(ep->maxpkt > 1024){
  988. print("usb: %s: maxpkt %ld > 1024. truncating\n", s, ep->maxpkt);
  989. ep->maxpkt = 1024;
  990. }
  991. }
  992. /*
  993. * Many endpoint ctls. simply update the portable representation
  994. * of the endpoint. The actual controller driver will look
  995. * at them to setup the endpoints as dictated.
  996. */
  997. static long
  998. epctl(Ep *ep, Chan *c, void *a, long n)
  999. {
  1000. int i, l, mode, nb, tt;
  1001. char *b, *s;
  1002. Cmdbuf *cb;
  1003. Cmdtab *ct;
  1004. Ep *nep;
  1005. Udev *d;
  1006. static char *Info = "info ";
  1007. d = ep->dev;
  1008. cb = parsecmd(a, n);
  1009. if(waserror()){
  1010. free(cb);
  1011. nexterror();
  1012. }
  1013. ct = lookupcmd(cb, epctls, nelem(epctls));
  1014. if(ct == nil)
  1015. error(Ebadctl);
  1016. i = ct->index;
  1017. if(i == CMnew || i == CMspeed || i == CMhub || i == CMpreset)
  1018. if(ep != ep->ep0)
  1019. error("allowed only on a setup endpoint");
  1020. if(i != CMclrhalt && i != CMdetach && i != CMdebugep && i != CMname)
  1021. if(ep != ep->ep0 && ep->inuse != 0)
  1022. error("must configure before using");
  1023. switch(i){
  1024. case CMnew:
  1025. deprint("usb epctl %s\n", cb->f[0]);
  1026. nb = strtol(cb->f[1], nil, 0);
  1027. if(nb < 0 || nb >= Ndeveps)
  1028. error("bad endpoint number");
  1029. tt = name2ttype(cb->f[2]);
  1030. if(tt == Tnone)
  1031. error("unknown endpoint type");
  1032. mode = name2mode(cb->f[3]);
  1033. if(mode < 0)
  1034. error("unknown i/o mode");
  1035. newdevep(ep, nb, tt, mode);
  1036. break;
  1037. case CMnewdev:
  1038. deprint("usb epctl %s\n", cb->f[0]);
  1039. if(ep != ep->ep0 || d->ishub == 0)
  1040. error("not a hub setup endpoint");
  1041. l = name2speed(cb->f[1]);
  1042. if(l == Nospeed)
  1043. error("speed must be full|low|high");
  1044. nep = newdev(ep->hp, 0, 0);
  1045. nep->dev->speed = l;
  1046. if(nep->dev->speed != Lowspeed)
  1047. nep->maxpkt = 64; /* assume full speed */
  1048. nep->dev->hub = d->nb;
  1049. nep->dev->port = atoi(cb->f[2]);
  1050. /* next read request will read
  1051. * the name for the new endpoint
  1052. */
  1053. l = sizeof(up->genbuf);
  1054. snprint(up->genbuf, l, "ep%d.%d", nep->dev->nb, nep->nb);
  1055. kstrdup(&c->aux, up->genbuf);
  1056. break;
  1057. case CMhub:
  1058. deprint("usb epctl %s\n", cb->f[0]);
  1059. d->ishub = 1;
  1060. break;
  1061. case CMspeed:
  1062. l = name2speed(cb->f[1]);
  1063. deprint("usb epctl %s %d\n", cb->f[0], l);
  1064. if(l == Nospeed)
  1065. error("speed must be full|low|high");
  1066. qlock(ep->ep0);
  1067. d->speed = l;
  1068. qunlock(ep->ep0);
  1069. break;
  1070. case CMmaxpkt:
  1071. l = strtoul(cb->f[1], nil, 0);
  1072. deprint("usb epctl %s %d\n", cb->f[0], l);
  1073. if(l < 1 || l > 1024)
  1074. error("maxpkt not in [1:1024]");
  1075. qlock(ep);
  1076. ep->maxpkt = l;
  1077. qunlock(ep);
  1078. break;
  1079. case CMntds:
  1080. l = strtoul(cb->f[1], nil, 0);
  1081. deprint("usb epctl %s %d\n", cb->f[0], l);
  1082. if(l < 1 || l > 3)
  1083. error("ntds not in [1:3]");
  1084. qlock(ep);
  1085. ep->ntds = l;
  1086. qunlock(ep);
  1087. break;
  1088. case CMpollival:
  1089. if(ep->ttype != Tintr && ep->ttype != Tiso)
  1090. error("not an intr or iso endpoint");
  1091. l = strtoul(cb->f[1], nil, 0);
  1092. deprint("usb epctl %s %d\n", cb->f[0], l);
  1093. if(ep->ttype == Tiso ||
  1094. (ep->ttype == Tintr && ep->dev->speed == Highspeed)){
  1095. if(l < 1 || l > 16)
  1096. error("pollival power not in [1:16]");
  1097. l = pow2(l-1);
  1098. }else
  1099. if(l < 1 || l > 255)
  1100. error("pollival not in [1:255]");
  1101. qlock(ep);
  1102. ep->pollival = l;
  1103. if(ep->ttype == Tiso)
  1104. setmaxpkt(ep, "pollival");
  1105. qunlock(ep);
  1106. break;
  1107. case CMsamplesz:
  1108. if(ep->ttype != Tiso)
  1109. error("not an iso endpoint");
  1110. l = strtoul(cb->f[1], nil, 0);
  1111. deprint("usb epctl %s %d\n", cb->f[0], l);
  1112. if(l <= 0 || l > 8)
  1113. error("samplesz not in [1:8]");
  1114. qlock(ep);
  1115. ep->samplesz = l;
  1116. setmaxpkt(ep, "samplesz");
  1117. qunlock(ep);
  1118. break;
  1119. case CMhz:
  1120. if(ep->ttype != Tiso)
  1121. error("not an iso endpoint");
  1122. l = strtoul(cb->f[1], nil, 0);
  1123. deprint("usb epctl %s %d\n", cb->f[0], l);
  1124. if(l <= 0 || l > 100000)
  1125. error("hz not in [1:100000]");
  1126. qlock(ep);
  1127. ep->hz = l;
  1128. setmaxpkt(ep, "hz");
  1129. qunlock(ep);
  1130. break;
  1131. case CMclrhalt:
  1132. qlock(ep);
  1133. deprint("usb epctl %s\n", cb->f[0]);
  1134. ep->clrhalt = 1;
  1135. qunlock(ep);
  1136. break;
  1137. case CMinfo:
  1138. deprint("usb epctl %s\n", cb->f[0]);
  1139. l = strlen(Info);
  1140. s = a;
  1141. if(n < l+2 || strncmp(Info, s, l) != 0)
  1142. error(Ebadctl);
  1143. if(n > 1024)
  1144. n = 1024;
  1145. b = smalloc(n);
  1146. memmove(b, s+l, n-l);
  1147. b[n-l] = 0;
  1148. if(b[n-l-1] == '\n')
  1149. b[n-l-1] = 0;
  1150. qlock(ep);
  1151. free(ep->info);
  1152. ep->info = b;
  1153. qunlock(ep);
  1154. break;
  1155. case CMaddress:
  1156. deprint("usb epctl %s\n", cb->f[0]);
  1157. ep->dev->state = Denabled;
  1158. break;
  1159. case CMdetach:
  1160. if(ep->dev->isroot != 0)
  1161. error("can't detach a root hub");
  1162. deprint("usb epctl %s ep%d.%d\n",
  1163. cb->f[0], ep->dev->nb, ep->nb);
  1164. ep->dev->state = Ddetach;
  1165. /* Release file system ref. for its endpoints */
  1166. for(i = 0; i < nelem(ep->dev->eps); i++)
  1167. putep(ep->dev->eps[i]);
  1168. break;
  1169. case CMdebugep:
  1170. if(strcmp(cb->f[1], "on") == 0)
  1171. ep->debug = 1;
  1172. else if(strcmp(cb->f[1], "off") == 0)
  1173. ep->debug = 0;
  1174. else
  1175. ep->debug = strtoul(cb->f[1], nil, 0);
  1176. print("usb: ep%d.%d debug %d\n",
  1177. ep->dev->nb, ep->nb, ep->debug);
  1178. break;
  1179. case CMname:
  1180. deprint("usb epctl %s %s\n", cb->f[0], cb->f[1]);
  1181. validname(cb->f[1], 0);
  1182. kstrdup(&ep->name, cb->f[1]);
  1183. break;
  1184. case CMtmout:
  1185. deprint("usb epctl %s\n", cb->f[0]);
  1186. if(ep->ttype == Tiso || ep->ttype == Tctl)
  1187. error("ctl ignored for this endpoint type");
  1188. ep->tmout = strtoul(cb->f[1], nil, 0);
  1189. if(ep->tmout != 0 && ep->tmout < Xfertmout)
  1190. ep->tmout = Xfertmout;
  1191. break;
  1192. case CMpreset:
  1193. deprint("usb epctl %s\n", cb->f[0]);
  1194. if(ep->ttype != Tctl)
  1195. error("not a control endpoint");
  1196. if(ep->dev->state != Denabled)
  1197. error("forbidden on devices not enabled");
  1198. ep->dev->state = Dreset;
  1199. break;
  1200. default:
  1201. panic("usb: unknown epctl %d", ct->index);
  1202. }
  1203. free(cb);
  1204. poperror();
  1205. return n;
  1206. }
  1207. static long
  1208. usbctl(void *a, long n)
  1209. {
  1210. Cmdtab *ct;
  1211. Cmdbuf *cb;
  1212. Ep *ep;
  1213. int i;
  1214. cb = parsecmd(a, n);
  1215. if(waserror()){
  1216. free(cb);
  1217. nexterror();
  1218. }
  1219. ct = lookupcmd(cb, usbctls, nelem(usbctls));
  1220. dprint("usb ctl %s\n", cb->f[0]);
  1221. switch(ct->index){
  1222. case CMdebug:
  1223. if(strcmp(cb->f[1], "on") == 0)
  1224. debug = 1;
  1225. else if(strcmp(cb->f[1], "off") == 0)
  1226. debug = 0;
  1227. else
  1228. debug = strtol(cb->f[1], nil, 0);
  1229. print("usb: debug %d\n", debug);
  1230. for(i = 0; i < epmax; i++)
  1231. if((ep = getep(i)) != nil){
  1232. ep->hp->debug(ep->hp, debug);
  1233. putep(ep);
  1234. }
  1235. break;
  1236. case CMdump:
  1237. dumpeps();
  1238. break;
  1239. }
  1240. free(cb);
  1241. poperror();
  1242. return n;
  1243. }
  1244. static long
  1245. ctlwrite(Chan *c, void *a, long n)
  1246. {
  1247. int q;
  1248. Ep *ep;
  1249. q = QID(c->qid);
  1250. if(q == Qctl)
  1251. return usbctl(a, n);
  1252. ep = getep(qid2epidx(q));
  1253. if(ep == nil)
  1254. error(Eio);
  1255. if(waserror()){
  1256. putep(ep);
  1257. nexterror();
  1258. }
  1259. if(ep->dev->state == Ddetach)
  1260. error(Edetach);
  1261. if(isqtype(q, Qepctl) && c->aux != nil){
  1262. /* Be sure we don't keep a cloned ep name */
  1263. free(c->aux);
  1264. c->aux = nil;
  1265. error("read, not write, expected");
  1266. }
  1267. n = epctl(ep, c, a, n);
  1268. putep(ep);
  1269. poperror();
  1270. return n;
  1271. }
  1272. static long
  1273. usbwrite(Chan *c, void *a, long n, vlong off)
  1274. {
  1275. int nr, q;
  1276. Ep *ep;
  1277. if(c->qid.type == QTDIR)
  1278. error(Eisdir);
  1279. q = QID(c->qid);
  1280. if(q == Qctl || isqtype(q, Qepctl))
  1281. return ctlwrite(c, a, n);
  1282. ep = getep(qid2epidx(q));
  1283. if(ep == nil)
  1284. error(Eio);
  1285. if(waserror()){
  1286. putep(ep);
  1287. nexterror();
  1288. }
  1289. if(ep->dev->state == Ddetach)
  1290. error(Edetach);
  1291. if(ep->mode == OREAD || ep->inuse == 0)
  1292. error(Ebadusefd);
  1293. switch(ep->ttype){
  1294. case Tnone:
  1295. error("endpoint not configured");
  1296. case Tctl:
  1297. nr = rhubwrite(ep, a, n);
  1298. if(nr >= 0){
  1299. n = nr;
  1300. break;
  1301. }
  1302. /* else fall */
  1303. default:
  1304. ddeprint("\nusbwrite q %#x fid %d cnt %ld off %lld\n",q, c->fid, n, off);
  1305. ep->hp->epwrite(ep, a, n);
  1306. }
  1307. putep(ep);
  1308. poperror();
  1309. return n;
  1310. }
  1311. void
  1312. usbshutdown(void)
  1313. {
  1314. Hci *hp;
  1315. int i;
  1316. for(i = 0; i < Nhcis; i++){
  1317. hp = hcis[i];
  1318. if(hp == nil)
  1319. continue;
  1320. if(hp->shutdown == nil)
  1321. print("#u: no shutdown function for %s\n", hp->type);
  1322. else
  1323. hp->shutdown(hp);
  1324. }
  1325. }
  1326. Dev usbdevtab = {
  1327. L'u',
  1328. "usb",
  1329. usbreset,
  1330. usbinit,
  1331. usbshutdown,
  1332. usbattach,
  1333. usbwalk,
  1334. usbstat,
  1335. usbopen,
  1336. devcreate,
  1337. usbclose,
  1338. usbread,
  1339. devbread,
  1340. usbwrite,
  1341. devbwrite,
  1342. devremove,
  1343. devwstat,
  1344. };