usbuhci.c 30 KB

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