usbuhci.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * USB Universal Host Controller Interface (UHCI) driver
  3. */
  4. #include "u.h"
  5. #include "../port/lib.h"
  6. #include "mem.h"
  7. #include "dat.h"
  8. #include "fns.h"
  9. #include "io.h"
  10. #include "../port/error.h"
  11. #include "usb.h"
  12. #define XPRINT if(debug)print
  13. #define XXPRINT if(0)print
  14. static int Chatty = 0;
  15. static int debug = 0;
  16. static char Estalled[] = "usb endpoint stalled";
  17. /*
  18. * UHCI interface registers and bits
  19. */
  20. enum
  21. {
  22. /*
  23. * USB packet definitions...
  24. */
  25. /* low nibbles are on-the-wire */
  26. Utokin = 0x69,
  27. Utokout = 0xE1,
  28. Utoksetup = 0x2D,
  29. /* i/o space */
  30. Cmd = 0,
  31. Status = 2,
  32. Usbintr = 4,
  33. Frnum = 6,
  34. Flbaseadd = 8,
  35. SOFMod = 0xC,
  36. Portsc0 = 0x10,
  37. Portsc1 = 0x12,
  38. /* port status */
  39. Suspend = 1<<12,
  40. PortReset = 1<<9,
  41. SlowDevice = 1<<8,
  42. ResumeDetect = 1<<6,
  43. PortChange = 1<<3, /* write 1 to clear */
  44. PortEnable = 1<<2,
  45. StatusChange = 1<<1, /* write 1 to clear */
  46. DevicePresent = 1<<0,
  47. NFRAME = 1024, /* must be 2ⁿ for xspanalloc; max 1024 */
  48. FRAMESIZE= NFRAME*sizeof(ulong), /* fixed by hardware; aligned to same */
  49. Vf = 1<<2, /* TD only */
  50. IsQH = 1<<1,
  51. Terminate = 1<<0,
  52. /* TD.status */
  53. SPD = 1<<29,
  54. ErrLimit0 = 0<<27,
  55. ErrLimit1 = 1<<27,
  56. ErrLimit2 = 2<<27,
  57. ErrLimit3 = 3<<27,
  58. LowSpeed = 1<<26,
  59. IsoSelect = 1<<25,
  60. IOC = 1<<24,
  61. Active = 1<<23,
  62. Stalled = 1<<22,
  63. DataBufferErr = 1<<21,
  64. Babbling = 1<<20,
  65. NAKed = 1<<19,
  66. CRCorTimeout = 1<<18,
  67. BitstuffErr = 1<<17,
  68. AnyError = (Stalled | DataBufferErr | Babbling | NAKed | CRCorTimeout |
  69. BitstuffErr),
  70. /* TD.dev */
  71. IsDATA1 = 1<<19,
  72. /* TD.flags (software) */
  73. CancelTD= 1<<0,
  74. IsoClean= 1<<2,
  75. };
  76. static struct
  77. {
  78. int bit;
  79. char *name;
  80. }
  81. portstatus[] =
  82. {
  83. { Suspend, "suspend", },
  84. { PortReset, "reset", },
  85. { SlowDevice, "lowspeed", },
  86. { ResumeDetect, "resume", },
  87. { PortChange, "portchange", },
  88. { PortEnable, "enable", },
  89. { StatusChange, "statuschange", },
  90. { DevicePresent, "present", },
  91. };
  92. typedef struct Ctlr Ctlr;
  93. typedef struct Endptx Endptx;
  94. typedef struct QH QH;
  95. typedef struct TD TD;
  96. /*
  97. * software structures
  98. */
  99. struct Ctlr
  100. {
  101. Lock; /* protects state shared with interrupt (eg, free list) */
  102. Ctlr* next;
  103. Pcidev* pcidev;
  104. int active;
  105. int io;
  106. ulong* frames; /* frame list */
  107. ulong* frameld; /* real time load on each frame list entry */
  108. QLock resetl; /* lock controller during USB reset */
  109. TD* tdpool;
  110. TD* freetd;
  111. QH* qhpool;
  112. QH* freeqh;
  113. QH* ctlq; /* queue for control i/o */
  114. QH* bwsop; /* empty bandwidth sop (to PIIX4 errata specs) */
  115. QH* bulkq; /* queue for bulk i/o (points back to bandwidth sop) */
  116. QH* recvq; /* receive queues for bulk i/o */
  117. Udev* ports[2];
  118. struct {
  119. Lock;
  120. Endpt* f;
  121. } activends;
  122. long usbints; /* debugging */
  123. long framenumber;
  124. long frameptr;
  125. long usbbogus;
  126. };
  127. #define IN(x) ins(ctlr->io+(x))
  128. #define OUT(x, v) outs(ctlr->io+(x), (v))
  129. static Ctlr* ctlrhead;
  130. static Ctlr* ctlrtail;
  131. struct Endptx
  132. {
  133. QH* epq; /* queue of TDs for this endpoint */
  134. /* ISO related: */
  135. void* tdalloc;
  136. void* bpalloc;
  137. uchar* bp0; /* first block in array */
  138. TD* td0; /* first td in array */
  139. TD* etd; /* pointer into circular list of TDs for isochronous ept */
  140. TD* xtd; /* next td to be cleaned */
  141. };
  142. /*
  143. * UHCI hardware structures, aligned on 16-byte boundary
  144. */
  145. struct TD
  146. {
  147. ulong link;
  148. ulong status; /* controller r/w */
  149. ulong dev;
  150. ulong buffer;
  151. /* software */
  152. ulong flags;
  153. union{
  154. Block* bp; /* non-iso */
  155. ulong offset; /* iso */
  156. };
  157. Endpt* ep;
  158. TD* next;
  159. };
  160. #define TFOL(p) ((TD*)KADDR((ulong)(p) & ~(0xF|PCIWINDOW)))
  161. struct QH
  162. {
  163. ulong head;
  164. ulong entries; /* address of next TD or QH to process (updated by controller) */
  165. /* software */
  166. QH* hlink;
  167. TD* first;
  168. QH* next; /* free list */
  169. TD* last;
  170. ulong _d1; /* fillers */
  171. ulong _d2;
  172. };
  173. #define QFOL(p) ((QH*)KADDR((ulong)(p) & ~(0xF|PCIWINDOW)))
  174. static TD *
  175. alloctd(Ctlr *ctlr)
  176. {
  177. TD *t;
  178. ilock(ctlr);
  179. t = ctlr->freetd;
  180. if(t == nil)
  181. panic("alloctd"); /* TO DO */
  182. ctlr->freetd = t->next;
  183. t->next = nil;
  184. iunlock(ctlr);
  185. t->ep = nil;
  186. t->bp = nil;
  187. t->status = 0;
  188. t->link = Terminate;
  189. t->buffer = 0;
  190. t->flags = 0;
  191. return t;
  192. }
  193. static void
  194. freetd(Ctlr *ctlr, TD *t)
  195. {
  196. t->ep = nil;
  197. if(t->bp)
  198. freeb(t->bp);
  199. t->bp = nil;
  200. ilock(ctlr);
  201. t->buffer = 0xdeadbeef;
  202. t->next = ctlr->freetd;
  203. ctlr->freetd = t;
  204. iunlock(ctlr);
  205. }
  206. static void
  207. dumpdata(Block *b, int n)
  208. {
  209. int i;
  210. XPRINT("\tb %#p[%d]: ", b->rp, n);
  211. if(n > 16)
  212. n = 16;
  213. for(i=0; i<n; i++)
  214. XPRINT(" %2.2ux", b->rp[i]);
  215. XPRINT("\n");
  216. }
  217. static void
  218. dumptd(TD *t, int follow)
  219. {
  220. int i, n;
  221. char buf[20], *s;
  222. TD *t0;
  223. t0 = t;
  224. while(t){
  225. i = t->dev & 0xFF;
  226. if(i == Utokout || i == Utoksetup)
  227. n = ((t->dev>>21) + 1) & 0x7FF;
  228. else if((t->status & Active) == 0)
  229. n = (t->status + 1) & 0x7FF;
  230. else
  231. n = 0;
  232. s = buf;
  233. if(t->status & Active)
  234. *s++ = 'A';
  235. if(t->status & Stalled)
  236. *s++ = 'S';
  237. if(t->status & DataBufferErr)
  238. *s++ = 'D';
  239. if(t->status & Babbling)
  240. *s++ = 'B';
  241. if(t->status & NAKed)
  242. *s++ = 'N';
  243. if(t->status & CRCorTimeout)
  244. *s++ = 'T';
  245. if(t->status & BitstuffErr)
  246. *s++ = 'b';
  247. if(t->status & LowSpeed)
  248. *s++ = 'L';
  249. *s = 0;
  250. XPRINT("td %#p: ", t);
  251. XPRINT("l=%#lux s=%#lux d=%#lux b=%#lux %#lux f=%#lux\n",
  252. t->link, t->status, t->dev, t->buffer,
  253. (t->bp? (ulong)t->bp->rp: 0), t->flags);
  254. XPRINT("\ts=%s,ep=%ld,d=%ld,D=%ld\n", buf, (t->dev>>15)&0xF,
  255. (t->dev>>8)&0xFF, (t->dev>>19)&1);
  256. if(debug && t->bp && (t->flags & CancelTD) == 0)
  257. dumpdata(t->bp, n);
  258. if(!follow || t->link & Terminate || t->link & IsQH)
  259. break;
  260. t = TFOL(t->link);
  261. if(t == t0)
  262. break; /* looped */
  263. }
  264. }
  265. static TD *
  266. alloctde(Ctlr *ctlr, Endpt *e, int pid, int n)
  267. {
  268. TD *t;
  269. int tog, id;
  270. t = alloctd(ctlr);
  271. id = (e->x<<7)|(e->dev->x&0x7F);
  272. tog = 0;
  273. if((pid == Utokout && e->wdata01) || (pid == Utokin && e->rdata01))
  274. tog = IsDATA1;
  275. t->ep = e;
  276. t->status = ErrLimit3 | Active | IOC; /* or put IOC only on last? */
  277. if(e->dev->speed == Lowspeed)
  278. t->status |= LowSpeed;
  279. t->dev = ((n-1)<<21) | ((id&0x7FF)<<8) | pid | tog;
  280. return t;
  281. }
  282. static QH *
  283. allocqh(Ctlr *ctlr)
  284. {
  285. QH *qh;
  286. ilock(ctlr);
  287. qh = ctlr->freeqh;
  288. if(qh == nil)
  289. panic("allocqh"); /* TO DO */
  290. ctlr->freeqh = qh->next;
  291. qh->next = nil;
  292. iunlock(ctlr);
  293. qh->head = Terminate;
  294. qh->entries = Terminate;
  295. qh->hlink = nil;
  296. qh->first = nil;
  297. qh->last = nil;
  298. return qh;
  299. }
  300. static void
  301. freeqh(Ctlr *ctlr, QH *qh)
  302. {
  303. ilock(ctlr);
  304. qh->next = ctlr->freeqh;
  305. ctlr->freeqh = qh;
  306. iunlock(ctlr);
  307. }
  308. static void
  309. dumpqh(QH *q)
  310. {
  311. int i;
  312. QH *q0;
  313. q0 = q;
  314. for(i = 0; q != nil && i < 10; i++){
  315. XPRINT("qh %#p: %#lux %#lux\n", q, q->head, q->entries);
  316. if((q->entries & (IsQH|Terminate)) == 0)
  317. dumptd(TFOL(q->entries), 1);
  318. if(q->head & Terminate)
  319. break;
  320. if((q->head & IsQH) == 0){
  321. XPRINT("head:");
  322. dumptd(TFOL(q->head), 1);
  323. break;
  324. }
  325. q = QFOL(q->head);
  326. if(q == q0)
  327. break; /* looped */
  328. }
  329. }
  330. static void
  331. queuetd(Ctlr *ctlr, QH *q, TD *t, int vf, char *why)
  332. {
  333. TD *lt;
  334. for(lt = t; lt->next != nil; lt = lt->next)
  335. lt->link = PCIWADDR(lt->next) | vf;
  336. lt->link = Terminate;
  337. ilock(ctlr);
  338. XPRINT("queuetd %s: t=%p lt=%p q=%p first=%p last=%p entries=%.8lux\n",
  339. why, t, lt, q, q->first, q->last, q->entries);
  340. if(q->first != nil){
  341. q->last->link = PCIWADDR(t) | vf;
  342. q->last->next = t;
  343. }else{
  344. q->first = t;
  345. q->entries = PCIWADDR(t);
  346. }
  347. q->last = lt;
  348. XPRINT(" t=%p q=%p first=%p last=%p entries=%.8lux\n",
  349. t, q, q->first, q->last, q->entries);
  350. dumpqh(q);
  351. iunlock(ctlr);
  352. }
  353. static void
  354. cleantd(Ctlr *ctlr, TD *t, int discard)
  355. {
  356. Block *b;
  357. int n, err;
  358. XPRINT("cleanTD: %#lux %#lux %#lux %#lux\n",
  359. t->link, t->status, t->dev, t->buffer);
  360. if(t->ep != nil && t->ep->debug)
  361. dumptd(t, 0);
  362. if(t->status & Active)
  363. panic("cleantd Active");
  364. err = t->status & (AnyError & ~NAKed);
  365. /* TO DO: on t->status&AnyError, q->entries will not have advanced */
  366. if (err) {
  367. XPRINT("cleanTD: Error %#lux %#lux %#lux %#lux\n",
  368. t->link, t->status, t->dev, t->buffer);
  369. print("cleanTD %d/%d: Error %#lux %#lux %#lux %#lux\n",
  370. t->ep->dev->x, t->ep->x,
  371. t->link, t->status, t->dev, t->buffer);
  372. }
  373. switch(t->dev&0xFF){
  374. case Utokin:
  375. if(discard || (t->flags & CancelTD) || t->ep == nil ||
  376. t->ep->x!=0&&err){
  377. if(t->ep != nil){
  378. if(err != 0)
  379. t->ep->dir[Dirin].err = err & Stalled
  380. ? Estalled : Eio;
  381. wakeup(&t->ep->dir[Dirin].rend); /* in case anyone cares */
  382. }
  383. break;
  384. }
  385. b = t->bp;
  386. n = (t->status + 1) & 0x7FF;
  387. if(n > b->lim - b->wp)
  388. n = 0;
  389. b->wp += n;
  390. if(Chatty)
  391. dumpdata(b, n);
  392. t->bp = nil;
  393. t->ep->nbytes += n;
  394. t->ep->nblocks++;
  395. qpass(t->ep->rq, b); /* TO DO: flow control */
  396. wakeup(&t->ep->dir[Dirin].rend); /* TO DO */
  397. break;
  398. case Utoksetup:
  399. XPRINT("cleanTD: Utoksetup %#p\n", &t->ep);
  400. /*
  401. * don't really need to wakeup: subsequent IN or OUT
  402. * gives status./
  403. */
  404. if(t->ep != nil) {
  405. wakeup(&t->ep->dir[Dirout].rend); /* TO DO */
  406. XPRINT("cleanTD: wakeup %#p\n", &t->ep->dir[Dirout].rend);
  407. }
  408. break;
  409. case Utokout:
  410. /* TO DO: mark it done somewhere */
  411. XPRINT("cleanTD: TokOut %#p\n", &t->ep);
  412. ilock(ctlr); /* e->ntd++ is ilocked */
  413. if(t->ep != nil){
  414. if(t->bp){
  415. n = BLEN(t->bp);
  416. t->ep->nbytes += n;
  417. t->ep->nblocks++;
  418. }
  419. if(t->ep->x && err)
  420. t->ep->dir[Dirout].err = err&Stalled
  421. ? Estalled : Eio;
  422. if(--t->ep->ntd < 0)
  423. panic("cleantd ntd");
  424. wakeup(&t->ep->dir[Dirout].rend); /* TO DO */
  425. XPRINT("cleanTD: wakeup %#p\n", &t->ep->dir[Dirout].rend);
  426. }
  427. iunlock(ctlr);
  428. break;
  429. }
  430. freetd(ctlr, t);
  431. }
  432. static void
  433. cleanq(Ctlr *ctlr, QH *q, int discard, int vf)
  434. {
  435. TD *t, *tp;
  436. ilock(ctlr);
  437. tp = nil;
  438. for(t = q->first; t != nil;){
  439. XPRINT("cleanq: %#lux %#lux %#lux %#lux %#lux %#p\n",
  440. t->link, t->status, t->dev, t->buffer, t->flags, t->next);
  441. if(t->status & Active){
  442. if(t->status & NAKed){
  443. /* ensure interrupt next frame */
  444. t->status = (t->status & ~NAKed) | IOC;
  445. tp = t;
  446. t = t->next;
  447. continue;
  448. }
  449. if(t->flags & CancelTD){
  450. XPRINT("cancelTD: %#p\n", t);
  451. /* ensure interrupt next frame */
  452. t->status = (t->status & ~Active) | IOC;
  453. tp = t;
  454. t = t->next;
  455. continue;
  456. }
  457. tp = t;
  458. t = t->next;
  459. continue;
  460. }
  461. t->status &= ~IOC;
  462. if (tp == nil) {
  463. q->first = t->next;
  464. if(q->first != nil)
  465. q->entries = PCIWADDR(q->first);
  466. else
  467. q->entries = Terminate;
  468. } else {
  469. tp->next = t->next;
  470. if (t->next != nil)
  471. tp->link = PCIWADDR(t->next) | vf;
  472. else
  473. tp->link = Terminate;
  474. }
  475. if (q->last == t)
  476. q->last = tp;
  477. iunlock(ctlr);
  478. cleantd(ctlr, t, discard);
  479. ilock(ctlr);
  480. if (tp)
  481. t = tp->next;
  482. else
  483. t = q->first;
  484. XPRINT("t = %#p\n", t);
  485. dumpqh(q);
  486. }
  487. if(q->first && q->entries != PCIWADDR(q->first)){
  488. ctlr->usbbogus++;
  489. q->entries = PCIWADDR(q->first);
  490. }
  491. iunlock(ctlr);
  492. }
  493. static void
  494. canceltds(Ctlr *ctlr, QH *q, Endpt *e)
  495. {
  496. TD *t;
  497. if(q != nil){
  498. ilock(ctlr);
  499. for(t = q->first; t != nil; t = t->next)
  500. if(t->ep == e)
  501. t->flags |= CancelTD;
  502. iunlock(ctlr);
  503. XPRINT("cancel:\n");
  504. dumpqh(q);
  505. }
  506. }
  507. static void
  508. eptcancel(Ctlr *ctlr, Endpt *e)
  509. {
  510. Endptx *x;
  511. if(e == nil)
  512. return;
  513. x = e->private;
  514. canceltds(ctlr, x->epq, e);
  515. canceltds(ctlr, ctlr->ctlq, e);
  516. canceltds(ctlr, ctlr->bulkq, e);
  517. }
  518. static void
  519. eptactivate(Ctlr *ctlr, Endpt *e)
  520. {
  521. ilock(&ctlr->activends);
  522. if(e->active == 0){
  523. XPRINT("activate 0x%p\n", e);
  524. e->active = 1;
  525. e->activef = ctlr->activends.f;
  526. ctlr->activends.f = e;
  527. }
  528. iunlock(&ctlr->activends);
  529. }
  530. static void
  531. eptdeactivate(Ctlr *ctlr, Endpt *e)
  532. {
  533. Endpt **l;
  534. /* could be O(1) but not worth it yet */
  535. ilock(&ctlr->activends);
  536. if(e->active){
  537. e->active = 0;
  538. XPRINT("deactivate 0x%p\n", e);
  539. for(l = &ctlr->activends.f; *l != e; l = &(*l)->activef)
  540. if(*l == nil){
  541. iunlock(&ctlr->activends);
  542. panic("usb eptdeactivate");
  543. }
  544. *l = e->activef;
  545. }
  546. iunlock(&ctlr->activends);
  547. }
  548. static void
  549. queueqh(Ctlr *ctlr, QH *qh)
  550. {
  551. QH *q;
  552. /* See if it's already queued */
  553. for (q = ctlr->recvq->next; q; q = q->hlink)
  554. if (q == qh)
  555. return;
  556. if ((qh->hlink = ctlr->recvq->next) == nil)
  557. qh->head = Terminate;
  558. else
  559. qh->head = PCIWADDR(ctlr->recvq->next) | IsQH;
  560. ctlr->recvq->next = qh;
  561. ctlr->recvq->entries = PCIWADDR(qh) | IsQH;
  562. }
  563. static QH*
  564. qxmit(Ctlr *ctlr, Endpt *e, Block *b, int pid)
  565. {
  566. TD *t;
  567. int n, vf;
  568. QH *qh;
  569. Endptx *x;
  570. x = e->private;
  571. if(b != nil){
  572. n = BLEN(b);
  573. t = alloctde(ctlr, e, pid, n);
  574. t->bp = b;
  575. t->buffer = PCIWADDR(b->rp);
  576. }else
  577. t = alloctde(ctlr, e, pid, 0);
  578. ilock(ctlr);
  579. e->ntd++;
  580. iunlock(ctlr);
  581. if(e->debug)
  582. pprint("QTD: %#p n=%ld\n", t, b? BLEN(b): 0);
  583. vf = 0;
  584. if(e->x == 0){
  585. qh = ctlr->ctlq;
  586. vf = 0;
  587. }else if((qh = x->epq) == nil || e->mode != OWRITE){
  588. qh = ctlr->bulkq;
  589. vf = Vf;
  590. }
  591. queuetd(ctlr, qh, t, vf, "qxmit");
  592. return qh;
  593. }
  594. static QH*
  595. qrcv(Ctlr *ctlr, Endpt *e)
  596. {
  597. TD *t;
  598. Block *b;
  599. QH *qh;
  600. int vf;
  601. Endptx *x;
  602. x = e->private;
  603. t = alloctde(ctlr, e, Utokin, e->maxpkt);
  604. b = allocb(e->maxpkt);
  605. t->bp = b;
  606. t->buffer = PCIWADDR(b->wp);
  607. vf = 0;
  608. if(e->x == 0){
  609. qh = ctlr->ctlq;
  610. }else if((qh = x->epq) == nil || e->mode != OREAD){
  611. qh = ctlr->bulkq;
  612. vf = Vf;
  613. }
  614. queuetd(ctlr, qh, t, vf, "qrcv");
  615. return qh;
  616. }
  617. static int
  618. usbsched(Ctlr *ctlr, int pollms, ulong load)
  619. {
  620. int i, d, q;
  621. ulong best, worst;
  622. best = 1000000;
  623. q = -1;
  624. for (d = 0; d < pollms; d++){
  625. worst = 0;
  626. for (i = d; i < NFRAME; i++)
  627. if (ctlr->frameld[i] + load > worst)
  628. worst = ctlr->frameld[i] + load;
  629. if (worst < best){
  630. best = worst;
  631. q = d;
  632. }
  633. }
  634. return q;
  635. }
  636. static int
  637. schedendpt(Ctlr *ctlr, Endpt *e)
  638. {
  639. TD *td;
  640. Endptx *x;
  641. uchar *bp;
  642. int i, id, ix, size, frnum;
  643. /* Only for isochronous endpoints */
  644. if(e->epmode != Isomode || e->sched >= 0)
  645. return 0;
  646. /* Interrupt mode should also go here */
  647. if (e->active)
  648. return -1;
  649. e->off = 0;
  650. e->sched = usbsched(ctlr, e->pollms, e->maxpkt);
  651. if(e->sched < 0)
  652. return -1;
  653. x = e->private;
  654. if (x->tdalloc || x->bpalloc)
  655. panic("usb: tdalloc/bpalloc");
  656. x->tdalloc = mallocz(0x10 + NFRAME*sizeof(TD), 1);
  657. x->bpalloc = mallocz(0x10 + e->maxpkt*NFRAME/e->pollms, 1);
  658. x->td0 = (TD*)(((ulong)x->tdalloc + 0xf) & ~0xf);
  659. x->bp0 = (uchar *)(((ulong)x->bpalloc + 0xf) & ~0xf);
  660. frnum = (IN(Frnum) + 1) & 0x3ff;
  661. frnum = (frnum & ~(e->pollms - 1)) + e->sched;
  662. x->xtd = &x->td0[(frnum+8)&0x3ff]; /* Next td to finish */
  663. x->etd = nil;
  664. e->remain = 0;
  665. e->nbytes = 0;
  666. td = x->td0;
  667. for(i = e->sched; i < NFRAME; i += e->pollms){
  668. bp = x->bp0 + e->maxpkt*i/e->pollms;
  669. td->buffer = PCIWADDR(bp);
  670. td->ep = e;
  671. td->next = &td[1];
  672. ctlr->frameld[i] += e->maxpkt;
  673. td++;
  674. }
  675. td[-1].next = x->td0;
  676. for(i = e->sched; i < NFRAME; i += e->pollms){
  677. ix = (frnum+i) & 0x3ff;
  678. td = &x->td0[ix];
  679. id = (e->x<<7)|(e->dev->x&0x7F);
  680. if (e->mode == OREAD)
  681. /* enable receive on this entry */
  682. td->dev = (e->maxpkt-1)<<21 | (id&0x7FF)<<8 | Utokin;
  683. else{
  684. size = (e->hz + e->remain)*e->pollms/1000;
  685. e->remain = (e->hz + e->remain)*e->pollms%1000;
  686. size *= e->samplesz;
  687. td->dev = (size-1)<<21 | (id&0x7FF)<<8 | Utokout;
  688. }
  689. td->status = ErrLimit1 | Active | IsoSelect | IOC;
  690. td->link = ctlr->frames[ix];
  691. td->flags |= IsoClean;
  692. ctlr->frames[ix] = PCIWADDR(td);
  693. }
  694. return 0;
  695. }
  696. static void
  697. unschedendpt(Ctlr *ctlr, Endpt *e)
  698. {
  699. int q;
  700. TD *td;
  701. Endptx *x;
  702. ulong *addr;
  703. if(e->epmode != Isomode || e->sched < 0)
  704. return;
  705. x = e->private;
  706. if (x->tdalloc == nil)
  707. panic("tdalloc");
  708. for (q = e->sched; q < NFRAME; q += e->pollms){
  709. td = x->td0++;
  710. addr = &ctlr->frames[q];
  711. while(*addr != PADDR(td)) {
  712. if(*addr & IsQH)
  713. panic("usb: TD expected");
  714. addr = &TFOL(*addr)->link;
  715. }
  716. *addr = td->link;
  717. ctlr->frameld[q] -= e->maxpkt;
  718. }
  719. free(x->tdalloc);
  720. free(x->bpalloc);
  721. x->tdalloc = nil;
  722. x->bpalloc = nil;
  723. x->etd = nil;
  724. x->td0 = nil;
  725. e->sched = -1;
  726. }
  727. static void
  728. epalloc(Usbhost *uh, Endpt *e)
  729. {
  730. Endptx *x;
  731. x = malloc(sizeof(Endptx));
  732. e->private = x;
  733. x->epq = allocqh(uh->ctlr);
  734. if(x->epq == nil)
  735. panic("devendptx");
  736. }
  737. static void
  738. epfree(Usbhost *uh, Endpt *e)
  739. {
  740. Ctlr *ctlr;
  741. Endptx *x;
  742. ctlr = uh->ctlr;
  743. x = e->private;
  744. if(x->epq != nil)
  745. freeqh(ctlr, x->epq);
  746. }
  747. static void
  748. epopen(Usbhost *uh, Endpt *e)
  749. {
  750. Ctlr *ctlr;
  751. ctlr = uh->ctlr;
  752. if(e->epmode == Isomode && e->active)
  753. error("already open");
  754. if(schedendpt(ctlr, e) < 0){
  755. if(e->active)
  756. error("cannot schedule USB endpoint, active");
  757. else
  758. error("cannot schedule USB endpoint");
  759. }
  760. eptactivate(ctlr, e);
  761. }
  762. static void
  763. epclose(Usbhost *uh, Endpt *e)
  764. {
  765. Ctlr *ctlr;
  766. ctlr = uh->ctlr;
  767. eptdeactivate(ctlr, e);
  768. unschedendpt(ctlr, e);
  769. }
  770. static void
  771. epmode(Usbhost *uh, Endpt *e)
  772. {
  773. Ctlr *ctlr;
  774. Endptx *x;
  775. if(e->epnewmode == e->epmode)
  776. return;
  777. ctlr = uh->ctlr;
  778. x = e->private;
  779. if(e->epnewmode == Isomode){
  780. if(x->epq != nil){
  781. freeqh(ctlr, x->epq);
  782. x->epq = nil;
  783. }
  784. }else{
  785. /* Each bulk device gets a queue head hanging off the
  786. * bulk queue head
  787. */
  788. if(x->epq == nil){
  789. x->epq = allocqh(ctlr);
  790. if(x->epq == nil)
  791. panic("epbulk: allocqh");
  792. }
  793. queueqh(ctlr, x->epq);
  794. }
  795. e->epmode = e->epnewmode;
  796. }
  797. static int ioport[] = {-1, Portsc0, Portsc1};
  798. static void
  799. portreset(Usbhost *uh, int port)
  800. {
  801. int i, p;
  802. Ctlr *ctlr;
  803. ctlr = uh->ctlr;
  804. if(port != 1 && port != 2)
  805. error(Ebadarg);
  806. /* should check that device not being configured on other port? */
  807. p = ioport[port];
  808. qlock(&ctlr->resetl);
  809. if(waserror()){
  810. qunlock(&ctlr->resetl);
  811. nexterror();
  812. }
  813. XPRINT("r: %x\n", IN(p));
  814. ilock(ctlr);
  815. OUT(p, PortReset);
  816. delay(12); /* BUG */
  817. XPRINT("r2: %x\n", IN(p));
  818. OUT(p, IN(p) & ~PortReset);
  819. XPRINT("r3: %x\n", IN(p));
  820. OUT(p, IN(p) | PortEnable);
  821. microdelay(64);
  822. for(i=0; i<1000 && (IN(p) & PortEnable) == 0; i++)
  823. ;
  824. XPRINT("r': %x %d\n", IN(p), i);
  825. OUT(p, (IN(p) & ~PortReset)|PortEnable);
  826. iunlock(ctlr);
  827. poperror();
  828. qunlock(&ctlr->resetl);
  829. }
  830. static void
  831. portenable(Usbhost *uh, int port, int on)
  832. {
  833. int w, p;
  834. Ctlr *ctlr;
  835. ctlr = uh->ctlr;
  836. if(port != 1 && port != 2)
  837. error(Ebadarg);
  838. /* should check that device not being configured on other port? */
  839. p = ioport[port];
  840. qlock(&ctlr->resetl);
  841. if(waserror()){
  842. qunlock(&ctlr->resetl);
  843. nexterror();
  844. }
  845. ilock(ctlr);
  846. w = IN(p);
  847. if(on)
  848. w |= PortEnable;
  849. else
  850. w &= ~PortEnable;
  851. OUT(p, w);
  852. microdelay(64);
  853. iunlock(ctlr);
  854. XPRINT("e: %x\n", IN(p));
  855. poperror();
  856. qunlock(&ctlr->resetl);
  857. }
  858. static void
  859. portinfo(Usbhost *uh, char *s, char *se)
  860. {
  861. int x, i, j;
  862. Ctlr *ctlr;
  863. ctlr = uh->ctlr;
  864. for(i = 1; i <= 2; i++) {
  865. ilock(ctlr);
  866. x = IN(ioport[i]);
  867. if((x & (PortChange|StatusChange)) != 0)
  868. /* TODO: could notify usbd equivalent here */
  869. OUT(ioport[i], x);
  870. iunlock(ctlr);
  871. s = seprint(s, se, "%d %ux", i, x);
  872. for(j = 0; j < nelem(portstatus); j++) {
  873. if((x & portstatus[j].bit) != 0)
  874. s = seprint(s, se, " %s", portstatus[j].name);
  875. }
  876. s = seprint(s, se, "\n");
  877. }
  878. }
  879. static void
  880. cleaniso(Endpt *e, int frnum)
  881. {
  882. TD *td;
  883. int id, n, i;
  884. Endptx *x;
  885. uchar *bp;
  886. x = e->private;
  887. td = x->xtd;
  888. if (td->status & Active)
  889. return;
  890. id = e->x<<7 | (e->dev->x&0x7F);
  891. do {
  892. if (td->status & AnyError)
  893. XPRINT("usbisoerror 0x%lux\n", td->status);
  894. n = (td->status + 1) & 0x3ff;
  895. e->nbytes += n;
  896. if ((td->flags & IsoClean) == 0)
  897. e->nblocks++;
  898. if (e->mode == OREAD){
  899. e->buffered += n;
  900. e->poffset += (td->status + 1) & 0x3ff;
  901. td->offset = e->poffset;
  902. td->dev = (e->maxpkt-1)<<21 | (id&0x7FF)<<8 | Utokin;
  903. e->toffset = td->offset;
  904. }else{
  905. if ((td->flags & IsoClean) == 0){
  906. e->buffered -= n;
  907. if (e->buffered < 0){
  908. // print("e->buffered %d?\n", e->buffered);
  909. e->buffered = 0;
  910. }
  911. }
  912. e->toffset = td->offset;
  913. n = (e->hz + e->remain)*e->pollms/1000;
  914. e->remain = (e->hz + e->remain)*e->pollms%1000;
  915. n *= e->samplesz;
  916. td->dev = (n-1)<<21 | (id&0x7FF)<<8 | Utokout;
  917. td->offset = e->poffset;
  918. e->poffset += n;
  919. }
  920. td = td->next;
  921. if (x->xtd == td){
  922. XPRINT("@");
  923. break;
  924. }
  925. } while ((td->status & Active) == 0);
  926. e->time = todget(nil);
  927. x->xtd = td;
  928. for (n = 2; n < 4; n++){
  929. i = (frnum + n) & 0x3ff;
  930. td = x->td0 + i;
  931. bp = x->bp0 + e->maxpkt*i/e->pollms;
  932. if (td->status & Active)
  933. continue;
  934. if (e->mode == OWRITE){
  935. if (td == x->etd) {
  936. XPRINT("*");
  937. memset(bp+e->off, 0, e->maxpkt-e->off);
  938. if (e->off == 0)
  939. td->flags |= IsoClean;
  940. else
  941. e->buffered += (((td->dev>>21) +1) &
  942. 0x3ff) - e->off;
  943. x->etd = nil;
  944. }else if ((td->flags & IsoClean) == 0){
  945. XPRINT("-");
  946. memset(bp, 0, e->maxpkt);
  947. td->flags |= IsoClean;
  948. }
  949. } else
  950. /* Unread bytes are now lost */
  951. e->buffered -= (td->status + 1) & 0x3ff;
  952. td->status = ErrLimit1 | Active | IsoSelect | IOC;
  953. }
  954. wakeup(&e->dir[Dirout].rend);
  955. }
  956. static void
  957. interrupt(Ureg*, void *a)
  958. {
  959. QH *q;
  960. Ctlr *ctlr;
  961. Endpt *e;
  962. Endptx *x;
  963. int s, frnum;
  964. Usbhost *uh;
  965. uh = a;
  966. ctlr = uh->ctlr;
  967. s = IN(Status);
  968. ctlr->frameptr = inl(ctlr->io+Flbaseadd);
  969. ctlr->framenumber = IN(Frnum) & 0x3ff;
  970. OUT(Status, s);
  971. if ((s & 0x1f) == 0)
  972. return;
  973. ctlr->usbints++;
  974. frnum = IN(Frnum) & 0x3ff;
  975. if (s & 0x1a) {
  976. XPRINT("cmd #%x sofmod #%x\n", IN(Cmd), inb(ctlr->io+SOFMod));
  977. XPRINT("sc0 #%x sc1 #%x\n", IN(Portsc0), IN(Portsc1));
  978. }
  979. ilock(&ctlr->activends);
  980. for(e = ctlr->activends.f; e != nil; e = e->activef){
  981. x = e->private;
  982. if(e->epmode == Isomode){
  983. XXPRINT("cleaniso(e)\n");
  984. cleaniso(e, frnum);
  985. }else if(x->epq != nil){
  986. XXPRINT("cleanq(ctlr, x->epq, 0, 0)\n");
  987. cleanq(ctlr, x->epq, 0, 0);
  988. }
  989. }
  990. iunlock(&ctlr->activends);
  991. XXPRINT("cleanq(ctlr, ctlr->ctlq, 0, 0)\n");
  992. cleanq(ctlr, ctlr->ctlq, 0, 0);
  993. XXPRINT("cleanq(ctlr, ctlr->bulkq, 0, Vf)\n");
  994. cleanq(ctlr, ctlr->bulkq, 0, Vf);
  995. XXPRINT("clean recvq\n");
  996. for (q = ctlr->recvq->next; q; q = q->hlink) {
  997. XXPRINT("cleanq(ctlr, q, 0, Vf)\n");
  998. cleanq(ctlr, q, 0, Vf);
  999. }
  1000. }
  1001. static int
  1002. eptinput(void *arg)
  1003. {
  1004. Endpt *e;
  1005. e = arg;
  1006. return e->eof || e->dir[Dirin].err || qcanread(e->rq);
  1007. }
  1008. static int
  1009. isoready(void *a)
  1010. {
  1011. Endptx *x;
  1012. TD *etd;
  1013. x = a;
  1014. return (etd = x->etd) == nil ||
  1015. (etd != x->xtd && (etd->status & Active) == 0);
  1016. }
  1017. static long
  1018. isoio(Ctlr *ctlr, Endpt *e, void *a, long n, ulong offset, int w)
  1019. {
  1020. TD *td;
  1021. Endptx *x;
  1022. int i, frnum;
  1023. uchar *p, *q, *bp;
  1024. volatile int isolock;
  1025. x = e->private;
  1026. qlock(&e->rlock);
  1027. isolock = 0;
  1028. if(waserror()){
  1029. if (isolock){
  1030. isolock = 0;
  1031. iunlock(&ctlr->activends);
  1032. }
  1033. qunlock(&e->rlock);
  1034. eptcancel(ctlr, e);
  1035. nexterror();
  1036. }
  1037. p = a;
  1038. if (offset != 0 && offset != e->foffset){
  1039. iprint("offset %lud, foffset %llud\n", offset, e->foffset);
  1040. /* Seek to a specific position */
  1041. frnum = (IN(Frnum) + 8) & 0x3ff;
  1042. td = x->td0 + frnum;
  1043. if (offset < td->offset)
  1044. error("ancient history");
  1045. while (offset > e->toffset)
  1046. tsleep(&e->dir[Dirout].rend, return0, 0, 500);
  1047. /* TODO: how should this be parenthesised? */
  1048. while (offset >= td->offset +
  1049. (((w? (td->dev>>21): td->status) + 1) & 0x7ff)){
  1050. td = td->next;
  1051. if (td == x->xtd)
  1052. iprint("trouble\n");
  1053. }
  1054. ilock(&ctlr->activends);
  1055. isolock = 1;
  1056. e->off = td->offset - offset;
  1057. if (e->off >= e->maxpkt){
  1058. iprint("I can't program: %d\n", e->off);
  1059. e->off = 0;
  1060. }
  1061. x->etd = td;
  1062. e->foffset = offset;
  1063. }
  1064. do {
  1065. if (isolock == 0){
  1066. ilock(&ctlr->activends);
  1067. isolock = 1;
  1068. }
  1069. td = x->etd;
  1070. if (td == nil || e->off == 0){
  1071. if (td == nil){
  1072. XPRINT("0");
  1073. if (w){
  1074. frnum = (IN(Frnum) + 1) & 0x3ff;
  1075. td = x->td0 + frnum;
  1076. while(td->status & Active)
  1077. td = td->next;
  1078. }else{
  1079. frnum = (IN(Frnum) - 4) & 0x3ff;
  1080. td = x->td0 + frnum;
  1081. while(td->next != x->xtd)
  1082. td = td->next;
  1083. }
  1084. x->etd = td;
  1085. e->off = 0;
  1086. }else{
  1087. /* New td, make sure it's ready */
  1088. while (isoready(x) == 0){
  1089. isolock = 0;
  1090. iunlock(&ctlr->activends);
  1091. sleep(&e->dir[Dirout].rend, isoready, x);
  1092. ilock(&ctlr->activends);
  1093. isolock = 1;
  1094. }
  1095. if (x->etd == nil){
  1096. XPRINT("!");
  1097. continue;
  1098. }
  1099. }
  1100. if (w)
  1101. e->psize = ((td->dev >> 21) + 1) & 0x7ff;
  1102. else
  1103. e->psize = (x->etd->status + 1) & 0x7ff;
  1104. if(e->psize > e->maxpkt)
  1105. panic("packet size > maximum");
  1106. }
  1107. if((i = n) >= e->psize)
  1108. i = e->psize;
  1109. if (w)
  1110. e->buffered += i;
  1111. else{
  1112. e->buffered -= i;
  1113. if (e->buffered < 0)
  1114. e->buffered = 0;
  1115. }
  1116. isolock = 0;
  1117. iunlock(&ctlr->activends);
  1118. td->flags &= ~IsoClean;
  1119. bp = x->bp0 + (td - x->td0) * e->maxpkt / e->pollms;
  1120. q = bp + e->off;
  1121. if (w)
  1122. memmove(q, p, i);
  1123. else
  1124. memmove(p, q, i);
  1125. p += i;
  1126. n -= i;
  1127. e->off += i;
  1128. e->psize -= i;
  1129. if (e->psize){
  1130. if (n != 0)
  1131. panic("usb iso: can't happen");
  1132. break;
  1133. }
  1134. if(w)
  1135. td->offset = offset + (p-(uchar*)a) -
  1136. (((td->dev >> 21) + 1) & 0x7ff);
  1137. td->status = ErrLimit3 | Active | IsoSelect | IOC;
  1138. x->etd = td->next;
  1139. e->off = 0;
  1140. } while(n > 0);
  1141. n = p-(uchar*)a;
  1142. e->foffset += n;
  1143. poperror();
  1144. if (isolock)
  1145. iunlock(&ctlr->activends);
  1146. qunlock(&e->rlock);
  1147. return n;
  1148. }
  1149. static long
  1150. read(Usbhost *uh, Endpt *e, void *a, long n, vlong offset)
  1151. {
  1152. long l, i;
  1153. Block *b;
  1154. Ctlr *ctlr;
  1155. uchar *p;
  1156. ctlr = uh->ctlr;
  1157. if(e->epmode == Isomode)
  1158. return isoio(ctlr, e, a, n, (ulong)offset, 0);
  1159. XPRINT("qlock(%p)\n", &e->rlock);
  1160. qlock(&e->rlock);
  1161. XPRINT("got qlock(%p)\n", &e->rlock);
  1162. if(waserror()){
  1163. e->dir[Dirin].err = nil;
  1164. qunlock(&e->rlock);
  1165. eptcancel(ctlr, e);
  1166. nexterror();
  1167. }
  1168. p = a;
  1169. do {
  1170. if(e->eof) {
  1171. XPRINT("e->eof\n");
  1172. break;
  1173. }
  1174. if(e->dir[Dirin].err)
  1175. error(e->dir[Dirin].err);
  1176. qrcv(ctlr, e);
  1177. e->rdata01 ^= 1;
  1178. sleep(&e->dir[Dirin].rend, eptinput, e);
  1179. if(e->dir[Dirin].err)
  1180. error(e->dir[Dirin].err);
  1181. b = qget(e->rq); /* TO DO */
  1182. if(b == nil) {
  1183. XPRINT("b == nil\n");
  1184. break;
  1185. }
  1186. if(waserror()){
  1187. freeb(b);
  1188. nexterror();
  1189. }
  1190. l = BLEN(b);
  1191. if((i = l) > n)
  1192. i = n;
  1193. if(i > 0){
  1194. memmove(p, b->rp, i);
  1195. p += i;
  1196. }
  1197. poperror();
  1198. freeb(b);
  1199. n -= i;
  1200. if (l != e->maxpkt)
  1201. break;
  1202. } while (n > 0);
  1203. poperror();
  1204. qunlock(&e->rlock);
  1205. return p - (uchar*)a;
  1206. }
  1207. static int
  1208. qisempty(void *arg)
  1209. {
  1210. return ((QH*)arg)->entries & Terminate;
  1211. }
  1212. static long
  1213. write(Usbhost *uh, Endpt *e, void *a, long n, vlong offset, int tok)
  1214. {
  1215. int i, j;
  1216. QH *qh;
  1217. Block *b;
  1218. Ctlr *ctlr;
  1219. uchar *p;
  1220. ctlr = uh->ctlr;
  1221. if(e->epmode == Isomode)
  1222. return isoio(ctlr, e, a, n, (ulong)offset, 1);
  1223. p = a;
  1224. qlock(&e->wlock);
  1225. if(waserror()){
  1226. e->dir[Dirout].err = nil;
  1227. qunlock(&e->wlock);
  1228. eptcancel(ctlr, e);
  1229. nexterror();
  1230. }
  1231. do {
  1232. if(e->dir[Dirout].err)
  1233. error(e->dir[Dirout].err);
  1234. if((i = n) >= e->maxpkt)
  1235. i = e->maxpkt;
  1236. b = allocb(i);
  1237. if(waserror()){
  1238. freeb(b);
  1239. nexterror();
  1240. }
  1241. XPRINT("out [%d]", i);
  1242. for (j = 0; j < i; j++)
  1243. XPRINT(" %.2x", p[j]);
  1244. XPRINT("\n");
  1245. memmove(b->wp, p, i);
  1246. b->wp += i;
  1247. p += i;
  1248. n -= i;
  1249. poperror();
  1250. qh = qxmit(ctlr, e, b, tok);
  1251. tok = Utokout;
  1252. e->wdata01 ^= 1;
  1253. if(e->ntd >= e->nbuf) {
  1254. XPRINT("qh %s: q=%p first=%p last=%p entries=%.8lux\n",
  1255. "writeusb sleep", qh, qh->first, qh->last,
  1256. qh->entries);
  1257. XPRINT("write: sleep %#p\n", &e->dir[Dirout].rend);
  1258. sleep(&e->dir[Dirout].rend, qisempty, qh);
  1259. XPRINT("write: awake\n");
  1260. }
  1261. } while(n > 0);
  1262. poperror();
  1263. qunlock(&e->wlock);
  1264. return p - (uchar*)a;
  1265. }
  1266. static void
  1267. init(Usbhost* uh)
  1268. {
  1269. Ctlr *ctlr;
  1270. ctlr = uh->ctlr;
  1271. ilock(ctlr);
  1272. outl(ctlr->io+Flbaseadd, PCIWADDR(ctlr->frames));
  1273. OUT(Frnum, 0);
  1274. OUT(Usbintr, 0xF); /* enable all interrupts */
  1275. XPRINT("cmd %#x sofmod %#x\n", IN(Cmd), inb(ctlr->io+SOFMod));
  1276. XPRINT("sc0 %#x sc1 %#x\n", IN(Portsc0), IN(Portsc1));
  1277. if((IN(Cmd)&1)==0)
  1278. OUT(Cmd, 1); /* run */
  1279. // pprint("at: c=%#x s=%#x c0=%x\n", IN(Cmd), IN(Status), IN(Portsc0));
  1280. iunlock(ctlr);
  1281. }
  1282. static void
  1283. scanpci(void)
  1284. {
  1285. int io;
  1286. Ctlr *ctlr;
  1287. Pcidev *p;
  1288. static int already = 0;
  1289. if(already)
  1290. return;
  1291. already = 1;
  1292. p = nil;
  1293. while(p = pcimatch(p, 0, 0)) {
  1294. /*
  1295. * Find UHCI controllers (Programming Interface = 0).
  1296. */
  1297. if(p->ccrb != Pcibcserial || p->ccru != Pciscusb)
  1298. continue;
  1299. switch(p->ccrp){
  1300. case 0:
  1301. io = p->mem[4].bar & ~0x0F;
  1302. break;
  1303. default:
  1304. continue;
  1305. }
  1306. if(io == 0) {
  1307. print("usbuhci: %x/%x failed to map registers\n",
  1308. p->vid, p->did);
  1309. continue;
  1310. }
  1311. if(ioalloc(io, p->mem[4].size, 0, "usbuhci") < 0){
  1312. print("usbuhci: port %#ux in use\n", io);
  1313. continue;
  1314. }
  1315. if(p->intl == 0xFF || p->intl == 0) {
  1316. print("usbuhci: no irq assigned for port %#ux\n", io);
  1317. continue;
  1318. }
  1319. XPRINT("usbuhci: %x/%x port 0x%ux size 0x%x irq %d\n",
  1320. p->vid, p->did, io, p->mem[4].size, p->intl);
  1321. ctlr = malloc(sizeof(Ctlr));
  1322. ctlr->pcidev = p;
  1323. ctlr->io = io;
  1324. if(ctlrhead != nil)
  1325. ctlrtail->next = ctlr;
  1326. else
  1327. ctlrhead = ctlr;
  1328. ctlrtail = ctlr;
  1329. }
  1330. }
  1331. static int
  1332. reset(Usbhost *uh)
  1333. {
  1334. int i;
  1335. TD *t;
  1336. ulong io;
  1337. Ctlr *ctlr;
  1338. Pcidev *p;
  1339. scanpci();
  1340. /*
  1341. * Any adapter matches if no uh->port is supplied,
  1342. * otherwise the ports must match.
  1343. */
  1344. for(ctlr = ctlrhead; ctlr != nil; ctlr = ctlr->next){
  1345. if(ctlr->active)
  1346. continue;
  1347. if(uh->port == 0 || uh->port == ctlr->io){
  1348. ctlr->active = 1;
  1349. break;
  1350. }
  1351. }
  1352. if(ctlr == nil)
  1353. return -1;
  1354. io = ctlr->io;
  1355. p = ctlr->pcidev;
  1356. uh->ctlr = ctlr;
  1357. uh->port = io;
  1358. uh->irq = p->intl;
  1359. uh->tbdf = p->tbdf;
  1360. XPRINT("usbcmd\t0x%.4x\nusbsts\t0x%.4x\nusbintr\t0x%.4x\nfrnum\t0x%.2x\n",
  1361. IN(Cmd), IN(Status), IN(Usbintr), inb(io+Frnum));
  1362. XPRINT("frbaseadd\t0x%.4x\nsofmod\t0x%x\nportsc1\t0x%.4x\nportsc2\t0x%.4x\n",
  1363. IN(Flbaseadd), inb(io+SOFMod), IN(Portsc0), IN(Portsc1));
  1364. OUT(Cmd, 0); /* stop */
  1365. while((IN(Status) & (1<<5)) == 0) /* wait for halt */
  1366. ;
  1367. OUT(Status, 0xFF); /* clear pending interrupts */
  1368. /* legacy support register: turn off lunacy mode */
  1369. pcicfgw16(p, 0xc0, 0x2000);
  1370. if(0){
  1371. i = inb(io+SOFMod);
  1372. OUT(Cmd, 4); /* global reset */
  1373. delay(15);
  1374. OUT(Cmd, 0); /* end reset */
  1375. delay(4);
  1376. outb(io+SOFMod, i);
  1377. }
  1378. ctlr->tdpool = xspanalloc(128*sizeof(TD), 16, 0);
  1379. for(i=128; --i>=0;){
  1380. ctlr->tdpool[i].next = ctlr->freetd;
  1381. ctlr->freetd = &ctlr->tdpool[i];
  1382. }
  1383. ctlr->qhpool = xspanalloc(64*sizeof(QH), 16, 0);
  1384. for(i=64; --i>=0;){
  1385. ctlr->qhpool[i].next = ctlr->freeqh;
  1386. ctlr->freeqh = &ctlr->qhpool[i];
  1387. }
  1388. /*
  1389. * the last entries of the periodic (interrupt & isochronous)
  1390. * scheduling TD entries points to the control queue and the
  1391. * bandwidth sop for bulk traffic. this is looped following the
  1392. * instructions in PIIX4 errata 29773804.pdf: a QH links to a
  1393. * looped but inactive TD as its sole entry, with its head entry
  1394. * leading on to the bulk traffic, the last QH of which links
  1395. * back to the empty QH.
  1396. */
  1397. ctlr->ctlq = allocqh(ctlr);
  1398. ctlr->bwsop = allocqh(ctlr);
  1399. ctlr->bulkq = allocqh(ctlr);
  1400. ctlr->recvq = allocqh(ctlr);
  1401. t = alloctd(ctlr); /* inactive TD, looped */
  1402. t->link = PCIWADDR(t);
  1403. ctlr->bwsop->entries = PCIWADDR(t);
  1404. ctlr->ctlq->head = PCIWADDR(ctlr->bulkq) | IsQH;
  1405. ctlr->bulkq->head = PCIWADDR(ctlr->recvq) | IsQH;
  1406. ctlr->recvq->head = PCIWADDR(ctlr->bwsop) | IsQH;
  1407. if (1) /* don't use loop back */
  1408. ctlr->bwsop->head = Terminate;
  1409. else /* set up loop back */
  1410. ctlr->bwsop->head = PCIWADDR(ctlr->bwsop) | IsQH;
  1411. ctlr->frames = xspanalloc(FRAMESIZE, FRAMESIZE, 0);
  1412. ctlr->frameld = xallocz(FRAMESIZE, 1);
  1413. if (ctlr->frames == nil || ctlr->frameld == nil)
  1414. panic("uhci reset: no memory");
  1415. for (i = 0; i < NFRAME; i++)
  1416. ctlr->frames[i] = PCIWADDR(ctlr->ctlq) | IsQH;
  1417. /*
  1418. * Linkage to the generic USB driver.
  1419. */
  1420. uh->init = init;
  1421. uh->interrupt = interrupt;
  1422. uh->portinfo = portinfo;
  1423. uh->portreset = portreset;
  1424. uh->portenable = portenable;
  1425. uh->epalloc = epalloc;
  1426. uh->epfree = epfree;
  1427. uh->epopen = epopen;
  1428. uh->epclose = epclose;
  1429. uh->epmode = epmode;
  1430. uh->read = read;
  1431. uh->write = write;
  1432. uh->tokin = Utokin;
  1433. uh->tokout = Utokout;
  1434. uh->toksetup = Utoksetup;
  1435. return 0;
  1436. }
  1437. void
  1438. usbuhcilink(void)
  1439. {
  1440. addusbtype("uhci", reset);
  1441. }