devusb.c 29 KB

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