devusb.c 30 KB

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