2 * USB Open Host Controller Interface (Ohci) driver
5 * - Missing isochronous input streams.
6 * - Too many delays and ilocks.
7 * - bandwidth admission control must be done per-frame.
8 * - Buffering could be handled like in uhci, to avoid
9 * needed block allocation and avoid allocs for small Tds.
10 * - must warn of power overruns.
14 #include "../port/lib.h"
19 #include "../port/error.h"
21 #include "../port/usb.h"
23 typedef struct Ctlio Ctlio;
24 typedef struct Ctlr Ctlr;
26 typedef struct Edpool Edpool;
27 typedef struct Epx Epx;
28 typedef struct Hcca Hcca;
29 typedef struct Isoio Isoio;
30 typedef struct Ohci Ohci;
31 typedef struct Qio Qio;
32 typedef struct Qtree Qtree;
34 typedef struct Tdpool Tdpool;
38 Incr = 64, /* for Td and Ed pools */
43 Abortdelay = 1, /* delay after cancelling Tds (ms) */
44 Tdatomic = 8, /* max nb. of Tds per bulk I/O op. */
45 Enabledelay = 100, /* waiting for a port to enable */
48 /* Queue states (software) */
57 Edmpsmask = 0x7ff, /* max packet size */
59 Edlow = 1 << 13, /* low speed */
60 Edskip = 1 << 14, /* skip this ed */
61 Ediso = 1 << 15, /* iso Tds used */
62 Edtddir = 0, /* get dir from td */
63 Edin = 2 << 11, /* direction in */
64 Edout = 1 << 11, /* direction out */
65 Eddirmask = 3 << 11, /* direction bits */
66 Edhalt = 1, /* halted (in head ptr) */
67 Edtoggle = 2, /* toggle (in head ptr) 1 == data1 */
70 Tdround = 1<<18, /* (rounding) short packets ok */
71 Tdtoksetup = 0<<19, /* setup packet */
72 Tdtokin = 2<<19, /* in packet */
73 Tdtokout = 1<<19, /* out packet */
74 Tdtokmask = 3<<19, /* in/out/setup bits */
75 Tdnoioc = 7<<21, /* intr. cnt. value for no interrupt */
76 Tdusetog = 1<<25, /* use toggle from Td (1) or Ed (0) */
77 Tddata1 = 1<<24, /* data toggle (1 == data1) */
79 Tdfcmask = 7, /* frame count (iso) */
81 Tdsfmask = 0xFFFF, /* starting frame (iso) */
82 Tderrmask = 3, /* error counter */
84 Tdccmask = 0xf, /* condition code (status) */
86 Tdiccmask = 0xf, /* condition code (iso, offsets) */
89 Ntdframes = 0x10000, /* # of different iso frame numbers */
91 /* Td errors (condition code) */
106 /* control register */
107 Cple = 0x04, /* periodic list enable */
108 Cie = 0x08, /* iso. list enable */
109 Ccle = 0x10, /* ctl list enable */
110 Cble = 0x20, /* bulk list enable */
111 Cir = 0x100, /* interrupt routing (smm active) */
112 Cfsmask = 3 << 6, /* functional state... */
119 Socr = 1 << 3, /* ownership change request */
120 Sblf = 1 << 2, /* bulk list (load) flag */
121 Sclf = 1 << 1, /* control list (load) flag */
122 Shcr = 1 << 0, /* host controller reset */
135 Fmaxpktmask = 0x7fff,
137 HcRhDescA_POTPGT_MASK = 0xff << 24,
138 HcRhDescA_POTPGT_SHIFT = 24,
153 Ccs = 0x00001, /* current connect status */
154 Pes = 0x00002, /* port enable status */
155 Pss = 0x00004, /* port suspend status */
156 Poci = 0x00008, /* over current indicator */
157 Prs = 0x00010, /* port reset status */
158 Pps = 0x00100, /* port power status */
159 Lsda = 0x00200, /* low speed device attached */
160 Csc = 0x10000, /* connect status change */
161 Pesc = 0x20000, /* enable status change */
162 Pssc = 0x40000, /* suspend status change */
163 Ocic = 0x80000, /* over current ind. change */
164 Prsc = 0x100000, /* reset status change */
166 /* port status write bits */
167 Cpe = 0x001, /* clear port enable */
168 Spe = 0x002, /* set port enable */
169 Spr = 0x010, /* set port reset */
170 Spp = 0x100, /* set port power */
171 Cpp = 0x200, /* clear port power */
176 * Endpoint descriptor. (first 4 words used by hardware)
180 ulong tail; /* transfer descriptor */
184 Ed* next; /* sw; in free list or next in list */
185 Td* tds; /* in use by current xfer; all for iso */
186 Ep* ep; /* debug/align */
187 Ed* inext; /* debug/align (dump interrupt eds). */
191 * Endpoint I/O state (software), per direction.
195 QLock; /* for the entire I/O process */
196 Rendez; /* wait for completion */
197 Ed* ed; /* to place Tds on it */
198 int sched; /* queue number (intr/iso) */
199 int toggle; /* Tddata0/Tddata1 */
200 ulong usbid; /* device/endpoint address */
201 int tok; /* Tdsetup, Tdtokin, Tdtokout */
202 long iotime; /* last I/O time; to hold interrupt polls */
203 int debug; /* for the endpoint */
204 char* err; /* error status */
205 int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
206 long bw; /* load (intr/iso) */
211 Qio; /* single Ed for all transfers */
212 uchar* data; /* read from last ctl req. */
213 int ndata; /* number of bytes read */
219 int nframes; /* number of frames for a full second */
220 Td* atds; /* Tds avail for further I/O */
221 int navail; /* number of avail Tds */
222 ulong frno; /* next frame number avail for I/O */
223 ulong left; /* remainder after rounding Hz to samples/ms */
224 int nerrs; /* consecutive errors on iso I/O */
225 int delay; /* maximum number of frames to buffer */
229 * Transfer descriptor. Size must be multiple of 32
230 * First block is used by hardware (aligned to 32).
235 ulong cbp; /* current buffer pointer */
238 ushort offsets[8]; /* used by Iso Tds only */
240 Td* next; /* in free or Ed tds list */
241 Td* anext; /* in avail td list (iso) */
242 Ep* ep; /* using this Td for I/O */
243 Qio* io; /* using this Td for I/O */
244 Block* bp; /* data for this Td */
245 ulong nbytes; /* bytes in this Td */
246 ulong cbp0; /* initial value for cbp */
247 ulong last; /* true for last Td in Qio */
251 * Host controller communication area (hardware)
267 /* control and status group */
268 ulong revision; /*00*/
269 ulong control; /*04*/
271 ulong intrsts; /*0c*/
272 ulong intrenable; /*10*/
273 ulong intrdisable; /*14*/
275 /* memory pointer group */
277 ulong periodcurred; /*1c*/
278 ulong ctlheaded; /*20*/
279 ulong ctlcurred; /*24*/
280 ulong bulkheaded; /*28*/
281 ulong bulkcurred; /*2c*/
282 ulong donehead; /*30*/
284 /* frame counter group */
285 ulong fminterval; /*34*/
286 ulong fmremaining; /*38*/
287 ulong fmnumber; /*3c*/
288 ulong periodicstart; /*40*/
289 ulong lsthreshold; /*44*/
292 ulong rhdesca; /*48*/
293 ulong rhdescb; /*4c*/
295 ulong rhportsts[15]; /*54*/
296 ulong pad25[20]; /*90*/
299 ulong hostueaddr; /*e0*/
300 ulong hostuests; /*e4*/
301 ulong hosttimeoutctrl; /*e8*/
304 ulong hostrevision; /*f4*/
310 * Endpoint tree (software)
340 Lock; /* for ilock; lists and basic ctlr I/O */
341 QLock resetl; /* lock controller during USB reset */
346 Ohci* ohci; /* base I/O address */
347 Hcca* hcca; /* intr/done Td lists (used by hardware) */
348 int overrun; /* sched. overrun */
349 Ed* intrhd; /* list of intr. eds in tree */
350 Qtree* tree; /* tree for t Ep i/o */
351 int ntree; /* number of dummy Eds in tree */
356 #define dqprint if(debug || io && io->debug)print
357 #define ddqprint if(debug>1 || (io && io->debug>1))print
358 #define diprint if(debug || iso && iso->debug)print
359 #define ddiprint if(debug>1 || (iso && iso->debug>1))print
360 #define TRUNC(x, sz) ((x) & ((sz)-1))
362 static int ohciinterrupts[Nttypes];
363 static char* iosname[] = { "idle", "install", "run", "done", "close", "FREE" };
366 static Edpool edpool;
367 static Tdpool tdpool;
368 static Ctlr* ctlrs[Nhcis];
370 static char EnotWritten[] = "usb write unfinished";
371 static char EnotRead[] = "usb read unfinished";
372 static char Eunderrun[] = "usb endpoint underrun";
374 static QLock usbhstate; /* protects name space state */
376 static int schedendpt(Ctlr *ub, Ep *ep);
377 static void unschedendpt(Ctlr *ub, Ep *ep);
378 static long qtd(Ctlr*, Ep*, int, Block*, uchar*, uchar*, int, ulong);
380 static char* errmsgs[] =
383 [Tdbitstuff] "bit stuffing error",
384 [Tdbadtog] "bad toggle",
385 [Tdstalled] Estalled,
386 [Tdtmout] "timeout error",
387 [Tdpidchk] "pid check error",
388 [Tdbadpid] "bad pid",
389 [Tddataovr] "data overrun",
390 [Tddataund] "data underrun",
391 [Tdbufovr] "buffer overrun",
392 [Tdbufund] "buffer underrun",
393 [Tdnotacc] "not accessed"
417 int frame = ub->hcca->framenumber & 0x3f;
421 } while(frame == (ub->hcca->framenumber & 0x3f));
428 if(err < nelem(errmsgs))
436 return pa2ptr(ctlr->ohci->ctlheaded);
442 return pa2ptr(ctlr->ohci->bulkheaded);
446 edlinked(Ed *ed, Ed *next)
449 print("edlinked: nil ed: pc %#p\n", getcallerpc(&ed));
450 ed->nexted = ptr2pa(next);
455 setctlhd(Ctlr *ctlr, Ed *ed)
457 ctlr->ohci->ctlheaded = ptr2pa(ed);
459 ctlr->ohci->cmdsts |= Sclf; /* reload it on next pass */
463 setbulkhd(Ctlr *ctlr, Ed *ed)
465 ctlr->ohci->bulkheaded = ptr2pa(ed);
467 ctlr->ohci->cmdsts |= Sblf; /* reload it on next pass */
471 unlinkctl(Ctlr *ctlr, Ed *ed)
473 Ed *this, *prev, *next;
475 ctlr->ohci->control &= ~Ccle;
478 ctlr->ohci->ctlcurred = 0;
480 while(this != nil && this != ed){
485 print("unlinkctl: not found\n");
490 setctlhd(ctlr, next);
492 edlinked(prev, next);
493 ctlr->ohci->control |= Ccle;
494 edlinked(ed, nil); /* wipe out next field */
498 unlinkbulk(Ctlr *ctlr, Ed *ed)
500 Ed *this, *prev, *next;
502 ctlr->ohci->control &= ~Cble;
505 ctlr->ohci->bulkcurred = 0;
507 while(this != nil && this != ed){
512 print("unlinkbulk: not found\n");
517 setbulkhd(ctlr, next);
519 edlinked(prev, next);
520 ctlr->ohci->control |= Cble;
521 edlinked(ed, nil); /* wipe out next field */
525 edsetaddr(Ed *ed, ulong addr)
529 ctrl = ed->ctrl & ~((Epmax<<7)|Devmax);
530 ctrl |= (addr & ((Epmax<<7)|Devmax));
535 edsettog(Ed *ed, int c)
538 ed->head |= Edtoggle;
540 ed->head &= ~Edtoggle;
546 return ed->head & Edtoggle;
552 return ed->head & Edhalt;
558 return (ed->ctrl >> Edmpsshift) & Edmpsmask;
562 edsetmaxpkt(Ed *ed, int m)
566 c = ed->ctrl & ~(Edmpsmask << Edmpsshift);
567 ed->ctrl = c | ((m&Edmpsmask) << Edmpsshift);
573 return (td->ctrl >> Tdccshift) & Tdccmask;
579 return (td->ctrl & Tdtokmask);
590 if(tdpool.free == nil){
591 ddprint("ohci: tdalloc %d Tds\n", Incr);
592 pool = xspanalloc(Incr*ROUND(sizeof(Td), Tdalign), Tdalign, 0);
594 panic("ohci: tdalloc");
595 for(i=Incr; --i>=0;){
596 td = (Td*)(pool + i*ROUND(sizeof(Td), Tdalign));
597 td->next = tdpool.free;
600 tdpool.nalloc += Incr;
601 tdpool.nfree += Incr;
606 tdpool.free = td->next;
608 assert(((uintptr)td & 0x1F) == 0);
609 memset(td, 0, sizeof(Td));
621 if(td->nexttd == 0x77777777)
622 panic("ohci: tdfree: double free");
623 memset(td, 7, sizeof(Td)); /* poison */
624 td->next = tdpool.free;
639 if(edpool.free == nil){
640 ddprint("ohci: edalloc %d Eds\n", Incr);
641 pool = xspanalloc(Incr*ROUND(sizeof(Ed), Edalign), Edalign, 0);
643 panic("ohci: edalloc");
644 for(i=Incr; --i>=0;){
645 ed = (Ed*)(pool + i*ROUND(sizeof(Ed), Edalign));
646 ed->next = edpool.free;
649 edpool.nalloc += Incr;
650 edpool.nfree += Incr;
655 edpool.free = ed->next;
657 assert(((uintptr)ed & 0xF) == 0);
658 memset(ed, 0, sizeof(Ed));
671 for(td = ed->tds; td != nil; td = next){
675 print("ohci: bug: ed with more than 2000 tds\n");
680 if(ed->nexted == 0x99999999)
681 panic("ohci: edfree: double free");
682 memset(ed, 9, sizeof(Ed)); /* poison */
683 ed->next = edpool.free;
688 ddprint("edfree: ed %#p\n", ed);
692 * return smallest power of 2 >= n
699 for(i = 0; (1 << i) < n; i++)
705 * return smallest power of 2 <= n
712 for(i = 0; (1 << (i + 1)) <= n; i++)
718 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
720 int i, j, d, upperb, q;
721 ulong best, worst, total;
723 d = flog2lower(pollival);
729 upperb = (1 << (d+1)) - 1;
730 for(i = (1 << d) - 1; i < upperb; i++){
732 for(j = i; j > 0; j = (j - 1) / 2)
741 if(worst + bw >= limit)
747 schedq(Ctlr *ctlr, Qio *io, int pollival)
752 q = pickschedq(ctlr->tree, pollival, io->bw, ~0);
753 ddqprint("ohci: sched %#p q %d, ival %d, bw %ld\n", io, q, pollival, io->bw);
755 print("ohci: no room for ed\n");
758 ctlr->tree->bw[q] += io->bw;
759 ted = ctlr->tree->root[q];
761 edlinked(io->ed, ted->next);
762 edlinked(ted, io->ed);
763 io->ed->inext = ctlr->intrhd;
764 ctlr->intrhd = io->ed;
769 unschedq(Ctlr *ctlr, Qio *qio)
772 Ed *prev, *this, *next;
778 ctlr->tree->bw[q] -= qio->bw;
780 prev = ctlr->tree->root[q];
782 while(this != nil && this != qio->ed){
787 print("ohci: unschedq %d: not found\n", q);
790 edlinked(prev, next);
793 for(l = &ctlr->intrhd; *l != nil; l = &(*l)->inext)
798 print("ohci: unschedq: ed %#p not found\n", qio->ed);
802 seprinttdtok(char *s, char *e, int tok)
806 s = seprint(s, e, " setup");
809 s = seprint(s, e, " in");
812 s = seprint(s, e, " out");
820 seprinttd(char *s, char *e, Td *td, int iso)
826 return seprint(s, e, "<nil td>\n");
827 s = seprint(s, e, "%#p ep %#p ctrl %#lux", td, td->ep, td->ctrl);
828 s = seprint(s, e, " cc=%#ulx", (td->ctrl >> Tdccshift) & Tdccmask);
830 if((td->ctrl & Tdround) != 0)
831 s = seprint(s, e, " rnd");
832 s = seprinttdtok(s, e, td->ctrl & Tdtokmask);
833 if((td->ctrl & Tdusetog) != 0)
834 s = seprint(s, e, " d%d", (td->ctrl & Tddata1) ? 1 : 0);
836 s = seprint(s, e, " d-");
837 s = seprint(s, e, " ec=%uld", (td->ctrl >> Tderrshift) & Tderrmask);
839 s = seprint(s, e, " fc=%uld", (td->ctrl >> Tdfcshift) & Tdfcmask);
840 s = seprint(s, e, " sf=%uld", td->ctrl & Tdsfmask);
842 s = seprint(s, e, " cbp0 %#lux cbp %#lux next %#lux be %#lux %s",
843 td->cbp0, td->cbp, td->nexttd, td->be, td->last ? "last" : "");
844 s = seprint(s, e, "\n\t\t%ld bytes", td->nbytes);
845 if((bp = td->bp) != nil){
846 s = seprint(s, e, " rp %#p wp %#p ", bp->rp, bp->wp);
848 s = seprintdata(s, e, bp->rp, bp->wp - bp->rp);
851 return seprint(s, e, "\n");
852 s = seprint(s, e, "\n\t\t");
853 /* we use only offsets[0] */
855 s = seprint(s, e, "[%d] %#ux cc=%#ux sz=%ud\n", i, td->offsets[i],
856 (td->offsets[i] >> Tdiccshift) & Tdiccmask,
857 td->offsets[i] & 0x7FF);
862 dumptd(Td *td, char *p, int iso)
864 static char buf[512]; /* Too much */
867 s = seprint(buf, buf+sizeof(buf), "%s: ", p);
868 s = seprinttd(s, buf+sizeof(buf), td, iso);
869 if(s > buf && s[-1] != '\n')
875 dumptds(Td *td, char *p, int iso)
879 for(i = 0; td != nil; td = td->next){
883 if(tdtok(td) == Tdtokin && ++i > 2){
896 print("<null ed>\n");
900 /* no waserror; may want to use from interrupt context */
904 s = seprint(buf, e, "\ted %#p: ctrl %#lux", ed, ed->ctrl);
905 if((ed->ctrl & Edskip) != 0)
906 s = seprint(s, e, " skip");
907 if((ed->ctrl & Ediso) != 0)
908 s = seprint(s, e, " iso");
909 if((ed->ctrl & Edlow) != 0)
910 s = seprint(s, e, " low");
911 s = seprint(s, e, " d%d", (ed->head & Edtoggle) ? 1 : 0);
912 if((ed->ctrl & Eddirmask) == Edin)
913 s = seprint(s, e, " in");
914 if((ed->ctrl & Eddirmask) == Edout)
915 s = seprint(s, e, " out");
917 s = seprint(s, e, " hlt");
918 s = seprint(s, e, " ep%uld.%uld", (ed->ctrl>>7)&Epmax, ed->ctrl&0x7f);
919 s = seprint(s, e, " maxpkt %uld", (ed->ctrl>>Edmpsshift)&Edmpsmask);
920 seprint(s, e, " tail %#lux head %#lux next %#lux\n",ed->tail,ed->head,ed->nexted);
923 if(ed->tds != nil && (ed->ctrl & Ediso) == 0)
924 dumptds(ed->tds, "td", 0);
928 seprintio(char *s, char *e, Qio *io, char *pref)
930 s = seprint(s, e, "%s qio %#p ed %#p", pref, io, io->ed);
931 s = seprint(s, e, " tog %d iot %ld err %s id %#ulx",
932 io->toggle, io->iotime, io->err, io->usbid);
933 s = seprinttdtok(s, e, io->tok);
934 s = seprint(s, e, " %s\n", iosname[io->state]);
939 seprintep(char* s, char* e, Ep *ep)
946 return seprint(s, e, "<nil ep>\n");
948 return seprint(s, e, "no mdep\n");
952 s = seprintio(s, e, cio, "c");
953 s = seprint(s, e, "\trepl %llux ndata %d\n", ep->rhrepl, cio->ndata);
958 if(ep->mode != OWRITE)
959 s = seprintio(s, e, &io[OREAD], "r");
960 if(ep->mode != OREAD)
961 s = seprintio(s, e, &io[OWRITE], "w");
965 s = seprintio(s, e, iso, "w");
966 s = seprint(s, e, "\tntds %d avail %d frno %uld left %uld next avail %#p\n",
967 iso->nframes, iso->navail, iso->frno, iso->left, iso->atds);
974 seprintctl(char *s, char *se, ulong ctl)
976 s = seprint(s, se, "en=");
978 s = seprint(s, se, "p");
980 s = seprint(s, se, "i");
982 s = seprint(s, se, "c");
984 s = seprint(s, se, "b");
985 switch(ctl & Cfsmask){
987 return seprint(s, se, " reset");
989 return seprint(s, se, " resume");
991 return seprint(s, se, " run");
993 return seprint(s, se, " suspend");
995 return seprint(s, se, " ???");
1008 seprintctl(cs, cs+sizeof(cs), ctlr->ohci->control);
1009 print("ohci ctlr %#p: frno %#ux ctl %#lux %s sts %#lux intr %#lux\n",
1010 ctlr, ctlr->hcca->framenumber, ctlr->ohci->control, cs,
1011 ctlr->ohci->cmdsts, ctlr->ohci->intrsts);
1012 print("ctlhd %#ulx cur %#ulx bulkhd %#ulx cur %#ulx done %#ulx\n",
1013 ctlr->ohci->ctlheaded, ctlr->ohci->ctlcurred,
1014 ctlr->ohci->bulkheaded, ctlr->ohci->bulkcurred,
1015 ctlr->ohci->donehead);
1016 if(ctlhd(ctlr) != nil)
1018 for(ed = ctlhd(ctlr); ed != nil; ed = ed->next)
1020 if(bulkhd(ctlr) != nil)
1022 for(ed = bulkhd(ctlr); ed != nil; ed = ed->next)
1024 if(ctlr->intrhd != nil)
1026 for(ed = ctlr->intrhd; ed != nil; ed = ed->inext)
1028 if(ctlr->tree->root[0]->next != nil)
1030 for(ed = ctlr->tree->root[0]->next; ed != nil; ed = ed->next)
1032 print("%d eds in tree\n", ctlr->ntree);
1035 print("%d tds allocated = %d in use + %d free\n",
1036 tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
1039 print("%d eds allocated = %d in use + %d free\n",
1040 edpool.nalloc, edpool.ninuse, edpool.nfree);
1045 * Compute size for the next iso Td and setup its
1046 * descriptor for I/O according to the buffer size.
1049 isodtdinit(Ep *ep, Isoio *iso, Td *td)
1056 assert(bp != nil && BLEN(bp) == 0);
1057 size = (ep->hz+iso->left) * ep->pollival / 1000;
1058 iso->left = (ep->hz+iso->left) * ep->pollival % 1000;
1059 size *= ep->samplesz;
1060 if(size > ep->maxpkt){
1061 print("ohci: ep%d.%d: size > maxpkt\n",
1062 ep->dev->nb, ep->nb);
1063 print("size = %uld max = %ld\n", size, ep->maxpkt);
1067 memset(bp->wp, 0, size); /* in case we don't fill it on time */
1068 td->cbp0 = td->cbp = ptr2pa(bp->rp) & ~0xFFF;
1069 td->ctrl = TRUNC(iso->frno, Ntdframes);
1070 td->offsets[0] = (ptr2pa(bp->rp) & 0xFFF);
1071 td->offsets[0] |= (Tdnotacc << Tdiccshift);
1072 /* in case the controller checks out the offests... */
1073 for(i = 1; i < nelem(td->offsets); i++)
1074 td->offsets[i] = td->offsets[0];
1075 td->be = ptr2pa(bp->rp + size - 1);
1076 td->ctrl |= (0 << Tdfcshift); /* frame count is 1 */
1078 iso->frno = TRUNC(iso->frno + ep->pollival, Ntdframes);
1082 * start I/O on the dummy td and setup a new dummy to fill up.
1085 isoadvance(Ep *ep, Isoio *iso, Td *td)
1090 iso->atds = dtd->anext;
1093 dtd->bp->wp = dtd->bp->rp;
1095 td->nexttd = ptr2pa(dtd);
1096 isodtdinit(ep, iso, dtd);
1097 iso->ed->tail = ptr2pa(dtd);
1101 isocanwrite(void *a)
1106 return iso->state == Qclose || iso->err != nil ||
1107 iso->navail > iso->nframes / 2;
1116 if(iso->state == Qclose || iso->err != nil || iso->delay == 0)
1118 return (iso->nframes - iso->navail) <= iso->delay;
1122 * Service a completed/failed Td from the done queue.
1123 * It may be of any transfer type.
1124 * The queue is not in completion order.
1125 * (It's actually in reverse completion order).
1127 * When an error, a short packet, or a last Td is found
1128 * we awake the process waiting for the transfer.
1129 * Although later we will process other Tds completed
1130 * before, epio won't be able to touch the current Td
1131 * until interrupt returns and releases the lock on the
1135 qhinterrupt(Ctlr *, Ep *ep, Qio *io, Td *td, int)
1142 if(io->state != Qrun)
1144 if(tdtok(td) == Tdtokin)
1152 case Tddataovr: /* Overrun is not an error */
1154 /* virtualbox doesn't always report underflow on short packets */
1159 /* short input packets are ok */
1162 panic("ohci: short packet but cbp == 0");
1164 * td->cbp and td->cbp0 are the real addresses
1165 * corresponding to virtual addresses bp->wp and
1166 * bp->rp respectively.
1168 bp->wp = bp->rp + (td->cbp - td->cbp0);
1170 panic("ohci: wp < rp");
1172 * It's ok. clear error and flag as last in xfer.
1173 * epio must ignore following Tds.
1176 td->ctrl &= ~(Tdccmask << Tdccshift);
1179 /* else fall; it's an error */
1187 bp->wp = bp->rp; /* no bytes in xfer. */
1188 io->err = errmsg(err);
1189 if(debug || ep->debug){
1190 print("tdinterrupt: failed err %d (%s)\n", err, io->err);
1191 dumptd(td, "failed", ed->ctrl & Ediso);
1196 panic("ohci: td cc %ud unknown", err);
1201 * clear td list and halt flag.
1203 ed->head = (ed->head & Edtoggle) | ed->tail;
1204 ed->tds = pa2ptr(ed->tail);
1211 * BUG: Iso input streams are not implemented.
1214 isointerrupt(Ctlr *ctlr, Ep *ep, Qio *io, Td *td, int)
1223 if(io->state == Qclose)
1227 * When we get more than half the frames consecutive errors
1228 * we signal an actual error. Errors in the entire Td are
1229 * more serious and are always singaled.
1230 * Errors like overrun are not really errors. In fact, for
1231 * output, errors cannot be really detected. The driver will
1232 * hopefully notice I/O errors on input endpoints and detach the device.
1235 isoerr = (td->offsets[0] >> Tdiccshift) & Tdiccmask;
1236 if(isoerr == Tdok || isoerr == Tdnotacc)
1238 else if(iso->nerrs++ > iso->nframes/2)
1240 if(err != Tdok && err != Tddataovr){
1242 io->err = errmsg(err);
1243 if(debug || ep->debug){
1244 print("ohci: isointerrupt: ep%d.%d: err %d (%s) frnum 0x%lux\n",
1245 ep->dev->nb, ep->nb,
1246 err, errmsg(err), ctlr->ohci->fmnumber);
1247 dumptd(td, "failed", ed->ctrl & Ediso);
1250 td->bp->wp = td->bp->rp;
1252 td->anext = iso->atds;
1256 * If almost all Tds are avail the user is not doing I/O at the
1257 * required rate. We put another Td in place to keep the polling rate.
1259 if(iso->err == nil && iso->navail > iso->nframes - 10)
1260 isoadvance(ep, iso, pa2ptr(iso->ed->tail));
1262 * If there's enough buffering futher I/O can be done.
1264 if(isocanwrite(iso))
1269 interrupt(Ureg *, void *arg)
1274 ulong status, curred, done;
1280 done = ctlr->hcca->donehead;
1281 status = ctlr->ohci->intrsts;
1287 ctlr->hcca->donehead = 0;
1290 else if(status & Wdh){
1291 done = ctlr->hcca->donehead;
1292 ctlr->hcca->donehead = 0;
1299 ctlr->ohci->intrsts = status;
1300 status &= ctlr->ohci->intrenable;
1301 status &= Oc|Rhsc|Fno|Ue|Rd|Sf|Wdh|So;
1303 frno = TRUNC(ctlr->ohci->fmnumber, Ntdframes);
1304 td = pa2ptr(done & ~0xF);
1305 for(i = 0; td != nil && i < 1024; i++){
1306 if(0)ddprint("ohci tdinterrupt: td %#p\n", td);
1307 ntd = pa2ptr(td->nexttd & ~0xF);
1309 if(td->ep == nil || td->io == nil)
1310 panic("ohci: interrupt: ep %#p io %#p", td->ep, td->io);
1311 ohciinterrupts[td->ep->ttype]++;
1312 if(td->ep->ttype == Tiso)
1313 isointerrupt(ctlr, td->ep, td->io, td, frno);
1315 qhinterrupt(ctlr, td->ep, td->io, td, frno);
1319 iprint("ohci: bug: more than 1024 done Tds?\n");
1324 iprint("ohci: sched overrun: too much load\n");
1329 curred = ctlr->ohci->periodcurred;
1330 iprint("ohci: unrecoverable error frame 0x%.8lux ed 0x%.8lux, "
1331 "ints %d %d %d %d\n",
1332 ctlr->ohci->fmnumber, curred,
1333 ohciinterrupts[Tctl], ohciinterrupts[Tintr],
1334 ohciinterrupts[Tbulk], ohciinterrupts[Tiso]);
1336 dumped(pa2ptr(curred));
1340 iprint("ohci interrupt: unhandled sts 0x%.8lux\n", status);
1341 ctlr->ohci->intrdisable = status;
1347 * The old dummy Td is used to implement the new Td.
1348 * A new dummy is linked at the end of the old one and
1349 * returned, to link further Tds if needed.
1352 epgettd(Ep *ep, Qio *io, Td **dtdp, int flags, void *a, int count)
1361 panic("ohci: transfer > two pages");
1362 /* maximum of one physical page crossing allowed */
1363 bp = allocb(count+BY2PG);
1364 bp->rp = (uchar*)PGROUND((uintptr)bp->rp);
1371 td->cbp0 = td->cbp = ptr2pa(bp->wp);
1372 td->be = ptr2pa(bp->wp + count - 1);
1374 /* validaddr((uintptr)a, count, 0); DEBUG */
1376 assert(bp->wp != nil);
1377 memmove(bp->wp, a, count);
1382 td->ctrl = io->tok|Tdusetog|io->toggle|flags;
1383 if(io->toggle == Tddata0)
1384 io->toggle = Tddata1;
1386 io->toggle = Tddata0;
1387 assert(td->ep == ep);
1389 dtd = tdalloc(); /* new dummy */
1391 td->nexttd = ptr2pa(dtd);
1398 * Try to get them idle
1410 for(td = ed->tds; td != nil; td = td->next)
1412 td->bp->wp = td->bp->rp;
1413 ed->head = (ed->head&0xF) | ed->tail;
1414 if((ed->ctrl & Ediso) == 0)
1415 ed->tds = pa2ptr(ed->tail);
1424 return io->state != Qrun;
1428 epiowait(Ctlr *ctlr, Qio *io, int tmout, ulong)
1434 if(0)ddqprint("ohci io %#p sleep on ed %#p state %s\n",
1435 io, ed, iosname[io->state]);
1438 dqprint("ohci io %#p ed %#p timed out\n", io, ed);
1442 sleep(io, epiodone, io);
1444 tsleep(io, epiodone, io, tmout);
1448 if(io->state == Qrun)
1450 else if(io->state != Qdone && io->state != Qclose)
1451 panic("epio: ed not done and not closed");
1454 io->err = "request timed out";
1457 tsleep(&up->sleep, return0, 0, Abortdelay);
1462 if(io->state != Qclose)
1469 * To make it work for control transfers, the caller may
1470 * lock the Qio for the entire control transfer.
1473 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
1480 Td *td, *ltd, *ntd, *td0;
1481 int last, ntds, tmout;
1487 io->debug = ep->debug;
1489 ddeprint("ohci: %s ep%d.%d io %#p count %ld\n",
1490 io->tok == Tdtokin ? "in" : "out",
1491 ep->dev->nb, ep->nb, io, count);
1492 if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
1493 seprintdata(buf, buf+sizeof(buf), a, count);
1494 print("\t%s\n", buf);
1505 if(io->state == Qclose){ /* Tds released by cancelio */
1507 error(io->err ? io->err : Eio);
1509 if(io->state != Qidle)
1510 panic("epio: qio not idle");
1511 io->state = Qinstall;
1514 ltd = td0 = ed->tds;
1520 if(c != nil && io->tok != Tdtokin)
1521 td = epgettd(ep, io, <d, 0, c+tot, n);
1523 td = epgettd(ep, io, <d, 0, nil, n);
1526 }while(tot < count);
1527 if(td0 == nil || ltd == nil || td0 == ltd)
1528 panic("epio: no td");
1530 if(debug > 2 || ep->debug > 2)
1531 dumptds(td0, "put td", ep->ttype == Tiso);
1535 if(io->state != Qclose){
1536 io->iotime = TK2MS(MACHP(0)->ticks);
1538 ed->tail = ptr2pa(ltd);
1539 if(ep->ttype == Tctl)
1540 ctlr->ohci->cmdsts |= Sclf;
1541 else if(ep->ttype == Tbulk)
1542 ctlr->ohci->cmdsts |= Sblf;
1546 epiowait(ctlr, io, tmout, load);
1548 if(debug > 1 || ep->debug > 1)
1549 dumptds(td0, "got td", 0);
1555 for(td = td0; td != ltd; td = ntd){
1558 * If the Td is flagged as last we must
1559 * ignore any following Td. The block may
1560 * seem to have bytes but interrupt has not seen
1561 * those Tds through the done queue, and they are void.
1563 if(last == 0 && tderrs(td) == Tdok){
1566 if(c != nil && tdtok(td) == Tdtokin && n > 0){
1567 memmove(c, td->bp->rp, n);
1575 if(edtoggle(ed) == 0)
1576 io->toggle = Tddata0;
1578 io->toggle = Tddata1;
1585 ddeprint("ohci: io %#p: %d tds: return %ld err '%s'\n\n",
1586 io, ntds, tot, err);
1595 * halt condition was cleared on the endpoint. update our toggles.
1607 if(ep->mode != OREAD){
1609 io[OWRITE].toggle = Tddata0;
1610 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1611 qunlock(&io[OWRITE]);
1613 if(ep->mode != OWRITE){
1615 io[OREAD].toggle = Tddata0;
1616 deprint("ep clrhalt for io %#p\n", io+OREAD);
1617 qunlock(&io[OREAD]);
1624 epread(Ep *ep, void *a, long count)
1632 panic("epread: not open");
1642 ddeprint("epread ctl ndata %d\n", cio->ndata);
1644 error("request expected");
1645 else if(cio->ndata == 0){
1649 if(count > cio->ndata)
1652 memmove(a, cio->data, count);
1653 /* BUG for big transfers */
1656 cio->ndata = 0; /* signal EOF next time */
1660 if(debug>1 || ep->debug){
1661 seprintdata(buf, buf+sizeof(buf), a, count);
1662 print("epread: %s\n", buf);
1669 return epio(ep, &io[OREAD], a, count, 1);
1672 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
1673 if(delta < ep->pollival / 2)
1674 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
1677 return epio(ep, &io[OREAD], a, count, 1);
1679 error("iso read not implemented");
1682 panic("epread: bad ep ttype %d", ep->ttype);
1688 * Control transfers are one setup write (data0)
1689 * plus zero or more reads/writes (data1, data0, ...)
1690 * plus a final write/read with data1 to ack.
1691 * For both host to device and device to host we perform
1692 * the entire transfer when the user writes the request,
1693 * and keep any data read from the device for a later read.
1694 * We call epio three times instead of placing all Tds at
1695 * the same time because doing so leads to crc/tmout errors
1697 * Upon errors on the data phase we must still run the status
1698 * phase or the device may cease responding in the future.
1701 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
1706 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
1707 cio, ep->dev->nb, ep->nb, count);
1708 if(count < Rsetuplen)
1709 error("short usb command");
1722 /* set the address if unset and out of configuration state */
1723 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
1724 if(cio->usbid == 0){
1725 cio->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
1726 edsetaddr(cio->ed, cio->usbid);
1728 /* adjust maxpkt if the user has learned a different one */
1729 if(edmaxpkt(cio->ed) != ep->maxpkt)
1730 edsetmaxpkt(cio->ed, ep->maxpkt);
1732 cio->tok = Tdtoksetup;
1733 cio->toggle = Tddata0;
1734 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
1740 cio->toggle = Tddata1;
1741 if(c[Rtype] & Rd2h){
1743 len = GET2(c+Rcount);
1745 error("bad length in d2h request");
1747 error("d2h data too large to fit in ohci");
1748 a = cio->data = smalloc(len+1);
1750 cio->tok = Tdtokout;
1757 len = epio(ep, cio, a, len, 0);
1760 if(c[Rtype] & Rd2h){
1763 cio->tok = Tdtokout;
1768 count = Rsetuplen + len;
1771 cio->toggle = Tddata1;
1772 epio(ep, cio, nil, 0, 0);
1775 ddeprint("epctlio cio %#p return %ld\n", cio, count);
1780 * Put new samples in the dummy Td.
1783 putsamples(Ctlr *ctlr, Ep *ep, Isoio *iso, uchar *b, long n)
1787 td = pa2ptr(iso->ed->tail);
1788 if(n > td->nbytes - BLEN(td->bp))
1789 n = td->nbytes - BLEN(td->bp);
1790 assert(td->bp->wp + n <= td->bp->lim);
1791 iunlock(ctlr); /* We could page fault here */
1792 memmove(td->bp->wp, b, n);
1794 if(td == pa2ptr(iso->ed->tail)){
1796 if(BLEN(td->bp) == td->nbytes) /* full Td: activate it */
1797 isoadvance(ep, iso, td);
1803 episowrite(Ep *ep, void *a, long count)
1813 iso->delay = (ep->sampledelay*ep->samplesz + ep->maxpkt-1) / ep->maxpkt;
1814 iso->debug = ep->debug;
1821 diprint("ohci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1823 if(iso->state == Qclose){
1825 error(iso->err ? iso->err : Eio);
1829 for(tot = 0; tot < count; tot += nw){
1830 while(isocanwrite(iso) == 0){
1832 diprint("ohci: episowrite: %#p sleep\n", iso);
1835 iso->err = "I/O timed out";
1839 tsleep(iso, isocanwrite, iso, ep->tmout);
1845 if(iso->state == Qclose || err != nil){
1847 error(err ? err : Eio);
1849 if(iso->state != Qrun)
1850 panic("episowrite: iso not running");
1851 nw = putsamples(ctlr, ep, iso, b+tot, count-tot);
1853 while(isodelay(iso) == 0){
1855 sleep(iso, isodelay, iso);
1858 if(iso->state != Qclose)
1861 err = iso->err; /* in case it failed early */
1867 diprint("ohci: episowrite: %#p %ld bytes\n", iso, tot);
1872 epwrite(Ep *ep, void *a, long count)
1881 panic("ohci: epwrite: not open");
1885 return epctlio(ep, cio, a, count);
1891 * Put at most Tdatomic Tds (512 bytes) at a time.
1892 * Otherwise some devices produce babble errors.
1896 for(tot = 0; tot < count ; tot += nw){
1898 if(nw > Tdatomic * ep->maxpkt)
1899 nw = Tdatomic * ep->maxpkt;
1900 nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
1905 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
1906 if(delta < ep->pollival)
1907 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
1910 return epio(ep, &io[OWRITE], a, count, 1);
1912 return episowrite(ep, a, count);
1914 panic("ohci: epwrite: bad ep ttype %d", ep->ttype);
1920 newed(Ctlr *ctlr, Ep *ep, Qio *io, char *)
1925 ed = io->ed = edalloc(); /* no errors raised here, really */
1929 ed->tail = ptr2pa(td);
1930 ed->head = ptr2pa(td);
1933 ed->ctrl = (ep->maxpkt & Edmpsmask) << Edmpsshift;
1934 if(ep->ttype == Tiso)
1941 /* For setup endpoints we start with the config address */
1942 if(ep->ttype != Tctl)
1943 edsetaddr(io->ed, io->usbid);
1944 if(ep->dev->speed == Lowspeed)
1954 ed->ctrl |= Edtddir; /* Td will say */
1961 edlinked(ed, ctlhd(ctlr));
1967 edlinked(ed, bulkhd(ctlr));
1968 setbulkhd(ctlr, ed);
1974 schedq(ctlr, io, ep->pollival);
1978 panic("ohci: newed: bad ttype");
1985 isoopen(Ctlr *ctlr, Ep *ep)
1992 iso->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
1993 iso->bw = ep->hz * ep->samplesz; /* bytes/sec */
1994 if(ep->mode != OWRITE){
1995 print("ohci: bug: iso input streams not implemented\n");
1996 error("ohci iso input streams not implemented");
1998 iso->tok = Tdtokout;
2002 iso->frno = TRUNC(ctlr->ohci->fmnumber + 10, Ntdframes);
2003 iso->nframes = 1000 / ep->pollival;
2004 if(iso->nframes < 10){
2005 print("ohci: isoopen: less than 10 frames; using 10.\n");
2008 iso->navail = iso->nframes;
2009 iso->atds = edtds = nil;
2010 for(i = 0; i < iso->nframes-1; i++){ /* -1 for dummy */
2014 td->bp = allocb(ep->maxpkt);
2015 td->anext = iso->atds; /* link as avail */
2020 newed(ctlr, ep, iso, "iso"); /* allocates a dummy td */
2021 iso->ed->tds->bp = allocb(ep->maxpkt); /* but not its block */
2022 iso->ed->tds->next = edtds;
2023 isodtdinit(ep, iso, iso->ed->tds);
2027 * Allocate the endpoint and set it up for I/O
2028 * in the controller. This must follow what's said
2029 * in Ep regarding configuration, including perhaps
2030 * the saved toggles (saved on a previous close of
2031 * the endpoint data file by epclose).
2042 deprint("ohci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2044 panic("ohci: epopen called with open ep");
2052 error("endpoint not configured");
2054 ep->aux = smalloc(sizeof(Isoio));
2058 cio = ep->aux = smalloc(sizeof(Ctlio));
2059 cio->debug = ep->debug;
2062 cio->tok = -1; /* invalid; Tds will say */
2063 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2065 newed(ctlr, ep, cio, "epc");
2068 ep->pollival = 1; /* assume this; doesn't really matter */
2071 io = ep->aux = smalloc(sizeof(Qio)*2);
2072 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2073 usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
2074 if(ep->mode != OREAD){
2075 if(ep->toggle[OWRITE] != 0)
2076 io[OWRITE].toggle = Tddata1;
2078 io[OWRITE].toggle = Tddata0;
2079 io[OWRITE].tok = Tdtokout;
2080 io[OWRITE].usbid = usbid;
2081 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2082 newed(ctlr, ep, io+OWRITE, "epw");
2084 if(ep->mode != OWRITE){
2085 if(ep->toggle[OREAD] != 0)
2086 io[OREAD].toggle = Tddata1;
2088 io[OREAD].toggle = Tddata0;
2089 io[OREAD].tok = Tdtokin;
2090 io[OREAD].usbid = usbid;
2091 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2092 newed(ctlr, ep, io+OREAD, "epr");
2096 deprint("ohci: epopen done:\n");
2097 if(debug || ep->debug)
2103 cancelio(Ep *ep, Qio *io)
2111 if(io == nil || io->state == Qclose){
2112 assert(io == nil || io->ed == nil);
2122 tsleep(&up->sleep, return0, 0, Abortdelay);
2128 /* wait for epio if running */
2134 unlinkctl(ctlr, ed);
2137 unlinkbulk(ctlr, ed);
2144 panic("ohci cancelio: bad ttype");
2158 deprint("ohci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
2160 panic("ohci: epclose called with closed ep");
2171 if(ep->mode != OWRITE){
2172 cancelio(ep, &io[OREAD]);
2173 if(io[OREAD].toggle == Tddata1)
2174 ep->toggle[OREAD] = 1;
2176 if(ep->mode != OREAD){
2177 cancelio(ep, &io[OWRITE]);
2178 if(io[OWRITE].toggle == Tddata1)
2179 ep->toggle[OWRITE] = 1;
2187 panic("epclose: bad ttype %d", ep->ttype);
2190 deprint("ohci: epclose ep%d.%d: done\n", ep->dev->nb, ep->nb);
2196 portreset(Hci *hp, int port, int on)
2205 eqlock(&ctlr->resetl);
2207 qunlock(&ctlr->resetl);
2212 ohci->rhportsts[port - 1] = Spp;
2213 if((ohci->rhportsts[port - 1] & Ccs) == 0){
2215 error("port not connected");
2217 ohci->rhportsts[port - 1] = Spr;
2218 while((ohci->rhportsts[port - 1] & Prsc) == 0){
2220 dprint("ohci: portreset, wait for reset complete\n");
2223 ohci->rhportsts[port - 1] = Prsc;
2226 qunlock(&ctlr->resetl);
2231 portenable(Hci *hp, int port, int on)
2236 dprint("ohci: %#p port %d enable=%d\n", ctlr->ohci, port, on);
2237 eqlock(&ctlr->resetl);
2239 qunlock(&ctlr->resetl);
2244 ctlr->ohci->rhportsts[port - 1] = Spe | Spp;
2246 ctlr->ohci->rhportsts[port - 1] = Cpe;
2248 tsleep(&up->sleep, return0, 0, Enabledelay);
2250 qunlock(&ctlr->resetl);
2255 portstatus(Hci *hp, int port)
2262 * We must return status bits as a
2263 * get port status hub request would do.
2266 ohcistatus = ub->ohci->rhportsts[port - 1];
2268 if(ohcistatus & Ccs)
2270 if(ohcistatus & Pes)
2272 if(ohcistatus & Pss)
2274 if(ohcistatus & Prs)
2277 /* port is not in reset; these potential writes are ok */
2278 if(ohcistatus & Csc){
2280 ub->ohci->rhportsts[port - 1] = Csc;
2282 if(ohcistatus & Pesc){
2284 ub->ohci->rhportsts[port - 1] = Pesc;
2287 if(ohcistatus & Lsda)
2289 if(v & (HPstatuschg|HPchange))
2290 ddprint("ohci port %d sts %#ulx hub sts %#x\n", port, ohcistatus, v);
2295 dumpohci(Ctlr *ctlr)
2300 ohci = &ctlr->ohci->revision;
2301 print("ohci registers: \n");
2302 for(i = 0; i < sizeof(Ohci)/sizeof(ulong); i++)
2303 if(i < 3 || ohci[i] != 0)
2304 print("\t[%#2.2x]\t%#8.8ulx\n", i * 4, ohci[i]);
2314 ulong ival, ctrl, fmi;
2317 dprint("ohci %#p init\n", ctlr->ohci);
2320 fmi = ohci->fminterval;
2321 ohci->cmdsts = Shcr; /* reset the block */
2322 for(i = 0; i<100; i++){
2323 if((ohci->cmdsts & Shcr) == 0)
2325 delay(1); /* wait till reset complete, Ohci says 10us max. */
2328 print("ohci: reset timed out\n");
2329 ohci->fminterval = fmi;
2332 * now that soft reset is done we are in suspend state.
2333 * Setup registers which take in suspend state
2334 * (will only be here for 2ms).
2337 ctlr->ohci->hcca = ptr2pa(ctlr->hcca);
2338 setctlhd(ctlr, nil);
2339 ctlr->ohci->ctlcurred = 0;
2340 setbulkhd(ctlr, nil);
2341 ctlr->ohci->bulkcurred = 0;
2343 ohci->intrenable = Mie | Wdh | Ue;
2344 ohci->control |= Ccle | Cble | Cple | Cie | Cfsoper;
2346 /* set frame after operational */
2347 ohci->rhdesca = Nps; /* no power switching */
2348 if(ohci->rhdesca & Nps){
2349 dprint("ohci: ports are not power switched\n");
2351 dprint("ohci: ports are power switched\n");
2352 ohci->rhdesca &= ~Psm;
2353 ohci->rhsts &= ~Lpsc;
2355 for(i = 0; i < ctlr->nports; i++) /* paranoia */
2356 ohci->rhportsts[i] = 0; /* this has no effect */
2359 for(i = 0; i < ctlr->nports; i++){
2360 ohci->rhportsts[i] = Spp;
2361 if((ohci->rhportsts[i] & Ccs) != 0)
2362 ohci->rhportsts[i] |= Spr;
2366 ctrl = ohci->control;
2367 if((ctrl & Cfsmask) != Cfsoper){
2368 ctrl = (ctrl & ~Cfsmask) | Cfsoper;
2369 ohci->control = ctrl;
2372 ival = ohci->fminterval & ~(Fmaxpktmask << Fmaxpktshift);
2373 ohci->fminterval = ival | (5120 << Fmaxpktshift);
2386 static int already = 0;
2392 while(p = pcimatch(p, 0, 0)) {
2394 * Find Ohci controllers (Programming Interface = 0x10).
2396 if(p->ccrb != Pcibcserial || p->ccru != Pciscusb || p->ccrp != 0x10)
2398 io = p->mem[0].bar & ~0x0F;
2401 print("usbohci: %#x %#x: port %#p size %#x irq %d\n",
2402 p->vid, p->did, io, p->mem[0].size, p->intl);
2403 ctlr = malloc(sizeof(Ctlr));
2405 print("ohci: no memory\n");
2408 if((ctlr->ohci = vmap(io, p->mem[0].size)) == nil){
2409 print("ohci: can't map ohci\n");
2415 dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
2416 for(i = 0; i < Nhcis; i++)
2417 if(ctlrs[i] == nil){
2422 print("ohci: bug: no more controllers\n");
2427 usbdebug(Hci*, int d)
2433 * build the periodic scheduling tree:
2434 * framesize must be a multiple of the tree size
2437 mkqhtree(Ctlr *ctlr)
2439 int i, n, d, o, leaf0, depth;
2444 n = (1 << (depth+1)) - 1;
2445 qt = mallocz(sizeof(*qt), 1);
2447 panic("ohci: can't allocate scheduling tree");
2450 qt->bw = mallocz(n * sizeof(qt->bw), 1);
2451 qt->root = tree = mallocz(n * sizeof(Ed *), 1);
2452 if(qt->bw == nil || qt->root == nil)
2453 panic("ohci: can't allocate scheduling tree");
2454 for(i = 0; i < n; i++){
2455 if((tree[i] = edalloc()) == nil)
2456 panic("ohci: mkqhtree");
2457 tree[i]->ctrl = (8 << Edmpsshift); /* not needed */
2458 tree[i]->ctrl |= Edskip;
2461 edlinked(tree[i], tree[(i-1)/2]);
2463 edlinked(tree[i], nil);
2466 dprint("ohci: tree: %d endpoints allocated\n", i);
2468 /* distribute leaves evenly round the frame list */
2470 for(i = 0; i < 32; i++){
2472 for(d = 0; d < depth; d++){
2478 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
2481 ctlr->hcca->intrtable[i] = ptr2pa(tree[leaf0 + o]);
2487 ohcimeminit(Ctlr *ctlr)
2491 edfree(edalloc()); /* allocate pools now */
2494 hcca = xspanalloc(sizeof(Hcca), 256, 0);
2496 panic("ohci: no memory for Hcca");
2497 memset(hcca, 0, sizeof(*hcca));
2504 ohcireset(Ctlr *ctlr)
2509 dprint("ohci %#p reset\n", ctlr->ohci);
2511 if(ctlr->ohci->control & Cir){
2512 dprint("ohci: smm active, taking over\n");
2513 ctlr->ohci->cmdsts |= Socr; /* take ownership */
2514 for(i = 0; i<100; i++){
2515 if((ctlr->ohci->control & Cir) == 0)
2520 print("ohci: smm takeover timed out\n");
2524 * usually enter here in reset, wait till its through,
2525 * then do our own so we are on known timing conditions.
2529 ctlr->ohci->control = 0;
2532 /* legacy support register: turn off lunacy mode */
2533 pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
2546 ctlr->ohci->intrdisable = Mie;
2547 ctlr->ohci->intrenable = 0;
2548 ctlr->ohci->control = 0;
2559 static Lock resetlck;
2561 if(getconf("*nousbohci"))
2567 * Any adapter matches if no hp->port is supplied,
2568 * otherwise the ports must match.
2571 for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
2573 if(ctlr->active == 0)
2574 if(hp->port == 0 || hp->port == ctlr->base){
2580 if(ctlrs[i] == nil || i == Nhcis)
2586 if(ctlr->ohci->control == ~0){
2592 hp->port = ctlr->base;
2595 ctlr->nports = hp->nports = ctlr->ohci->rhdesca & 0xff;
2603 * Linkage to the generic HCI driver.
2607 hp->interrupt = interrupt;
2608 hp->epopen = epopen;
2609 hp->epclose = epclose;
2610 hp->epread = epread;
2611 hp->epwrite = epwrite;
2612 hp->seprintep = seprintep;
2613 hp->portenable = portenable;
2614 hp->portreset = portreset;
2615 hp->portstatus = portstatus;
2616 hp->shutdown = shutdown;
2617 hp->debug = usbdebug;
2619 intrenable(hp->irq, hp->interrupt, hp, hp->tbdf, hp->type);
2627 addhcitype("ohci", reset);