devusb.c 30 KB

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