devusb.c 29 KB

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