2 * USB Enhanced Host Controller Interface (EHCI) driver
5 * Note that all of our unlock routines call coherence.
8 * - Too many delays and ilocks.
9 * - bandwidth admission control must be done per-frame.
10 * - requires polling (some controllers miss interrupts).
11 * - must warn of power overruns.
15 #include "../port/lib.h"
20 #include "../port/error.h"
21 #include "../port/usb.h"
25 #define diprint if(ehcidebug || iso->debug)print
26 #define ddiprint if(ehcidebug>1 || iso->debug>1)print
27 #define dqprint if(ehcidebug || (qh->io && qh->io->debug))print
28 #define ddqprint if(ehcidebug>1 || (qh->io && qh->io->debug>1))print
30 #define TRUNC(x, sz) ((x) & ((sz)-1))
31 #define LPTR(q) ((ulong*)KADDR((q) & ~0x1F))
33 typedef struct Ctlio Ctlio;
35 typedef struct Edpool Edpool;
36 typedef struct Itd Itd;
37 typedef struct Qio Qio;
38 typedef struct Qtd Qtd;
39 typedef struct Sitd Sitd;
43 * EHCI interface registers and bits
47 /* Queue states (software) */
55 Enabledelay = 100, /* waiting for a port to enable */
56 Abortdelay = 5, /* delay after cancelling Tds (ms) */
58 Incr = 64, /* for pools of Tds, Qhs, etc. */
59 Align = 128, /* in bytes for all those descriptors */
61 /* Keep them as a power of 2, lower than ctlr->nframes */
62 /* Also, keep Nisoframes >= Nintrleafs */
63 Nintrleafs = 32, /* nb. of leaf frames in intr. tree */
64 Nisoframes = 64, /* nb. of iso frames (in window) */
70 /* Itd bits (csw[]) */
71 Itdactive = 0x80000000, /* execution enabled */
72 Itddberr = 0x40000000, /* data buffer error */
73 Itdbabble = 0x20000000, /* babble error */
74 Itdtrerr = 0x10000000, /* transaction error */
75 Itdlenshift = 16, /* transaction length */
77 Itdioc = 0x00008000, /* interrupt on complete */
78 Itdpgshift = 12, /* page select field */
79 Itdoffshift = 0, /* transaction offset */
80 /* Itd bits, buffer[] */
81 Itdepshift = 8, /* endpoint address (buffer[0]) */
82 Itddevshift = 0, /* device address (buffer[0]) */
83 Itdin = 0x800, /* is input (buffer[1]) */
85 Itdmaxpktshift = 0, /* max packet (buffer[1]) */
86 Itdntdsshift = 0, /* nb. of tds per µframe (buffer[2]) */
88 Itderrors = Itddberr|Itdbabble|Itdtrerr,
91 Stdin = 0x80000000, /* input direction */
92 Stdportshift = 24, /* hub port number */
93 Stdhubshift = 16, /* hub address */
94 Stdepshift = 8, /* endpoint address */
95 Stddevshift = 0, /* device address */
97 Stdssmshift = 0, /* split start mask */
98 Stdscmshift = 8, /* split complete mask */
100 Stdioc = 0x80000000, /* interrupt on complete */
101 Stdpg = 0x40000000, /* page select */
102 Stdlenshift = 16, /* total bytes to transfer */
104 Stdactive = 0x00000080, /* active */
105 Stderr = 0x00000040, /* tr. translator error */
106 Stddberr = 0x00000020, /* data buffer error */
107 Stdbabble = 0x00000010, /* babble error */
108 Stdtrerr = 0x00000008, /* transaction error */
109 Stdmmf = 0x00000004, /* missed µframe */
110 Stddcs = 0x00000002, /* do complete split */
112 Stderrors = Stderr|Stddberr|Stdbabble|Stdtrerr|Stdmmf,
114 /* Sitd bits buffer[1] */
115 Stdtpall = 0x00000000, /* all payload here (188 bytes) */
116 Stdtpbegin = 0x00000008, /* first payload for fs trans. */
117 Stdtcntmask = 0x00000007, /* T-count */
120 Tddata1 = 0x80000000, /* data toggle 1 */
121 Tddata0 = 0x00000000, /* data toggle 0 */
122 Tdlenshift = 16, /* total bytes to transfer */
124 Tdmaxpkt = 0x5000, /* max buffer for a Td */
125 Tdioc = 0x00008000, /* interrupt on complete */
126 Tdpgshift = 12, /* current page */
128 Tderr1 = 0x00000400, /* bit 0 of error counter */
129 Tderr2 = 0x00000800, /* bit 1 of error counter */
130 Tdtokout = 0x00000000, /* direction out */
131 Tdtokin = 0x00000100, /* direction in */
132 Tdtoksetup = 0x00000200, /* setup packet */
133 Tdtok = 0x00000300, /* token bits */
134 Tdactive = 0x00000080, /* active */
135 Tdhalt = 0x00000040, /* halted */
136 Tddberr = 0x00000020, /* data buffer error */
137 Tdbabble = 0x00000010, /* babble error */
138 Tdtrerr = 0x00000008, /* transaction error */
139 Tdmmf = 0x00000004, /* missed µframe */
140 Tddcs = 0x00000002, /* do complete split */
141 Tdping = 0x00000001, /* do ping */
143 Tderrors = Tdhalt|Tddberr|Tdbabble|Tdtrerr|Tdmmf,
146 Qhrlcmask = 0xF, /* nak reload count */
147 Qhrlcshift = 28, /* nak reload count */
148 Qhnhctl = 0x08000000, /* not-high speed ctl */
149 Qhmplmask = 0x7FF, /* max packet */
151 Qhhrl = 0x00008000, /* head of reclamation list */
152 Qhdtc = 0x00004000, /* data toggle ctl. */
153 Qhint = 0x00000080, /* inactivate on next transition */
154 Qhspeedmask = 0x00003000, /* speed bits */
155 Qhfull = 0x00000000, /* full speed */
156 Qhlow = 0x00001000, /* low speed */
157 Qhhigh = 0x00002000, /* high speed */
160 Qhmultshift = 30, /* multiple tds per µframe */
162 Qhportshift = 23, /* hub port number */
163 Qhhubshift = 16, /* hub address */
164 Qhscmshift = 8, /* split completion mask bits */
165 Qhismshift = 0, /* interrupt sched. mask bits */
169 * Endpoint tree (software)
180 * One per endpoint per direction, to control I/O.
184 QLock; /* for the entire I/O process */
185 Rendez; /* wait for completion */
186 Qh* qh; /* Td list (field const after init) */
187 int usbid; /* usb address for endpoint/device */
188 int toggle; /* Tddata0/Tddata1 */
189 int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
190 ulong iotime; /* last I/O time; to hold interrupt polls */
191 int debug; /* debug flag from the endpoint */
192 char* err; /* error string */
193 char* tag; /* debug (no room in Qh for this) */
199 Qio; /* a single Qio for each RPC */
200 uchar* data; /* read from last ctl req. */
201 int ndata; /* number of bytes read */
207 Rendez; /* wait for space/completion/errors */
208 int usbid; /* address used for device/endpoint */
209 int tok; /* Tdtokin or Tdtokout */
210 int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
211 int nframes; /* number of frames ([S]Itds) used */
212 uchar* data; /* iso data buffers if not embedded */
213 char* err; /* error string */
214 int nerrs; /* nb of consecutive I/O errors */
215 ulong maxsize; /* ntds * ep->maxpkt */
216 long nleft; /* number of bytes left from last write */
217 int debug; /* debug flag from the endpoint */
218 int delay; /* max number of bytes to buffer */
219 int hs; /* is high speed? */
220 Isoio* next; /* in list of active Isoios */
221 ulong td0frno; /* first frame used in ctlr */
223 Itd* tdi; /* next td processed by interrupt */
227 Itd* tdu; /* next td for user I/O in tdps */
231 Itd** itdps; /* itdps[i]: ptr to Itd for i-th frame or nil */
232 Sitd** sitdps; /* sitdps[i]: ptr to Sitd for i-th frame or nil */
233 ulong** tdps; /* same thing, as seen by hw */
247 * We use the 64-bit version for Itd, Sitd, Td, and Qh.
248 * If the ehci is 64-bit capable it assumes we are using those
249 * structures even when the system is 32 bits.
253 * Iso transfer descriptor. hw: 92 bytes, 108 bytes total
258 ulong link; /* to next hw struct */
259 ulong csw[8]; /* sts/length/pg/off. updated by hw */
260 ulong buffer[7]; /* buffer pointers, addrs, maxsz */
261 ulong xbuffer[7]; /* high 32 bits of buffer for 64-bits */
263 ulong _pad0; /* pad to next cache line */
264 /* cache-line boundary here */
268 ulong ndata; /* number of bytes in data */
269 ulong mdata; /* max number of bytes in data */
274 * Split transaction iso transfer descriptor.
275 * hw: 36 bytes, 52 bytes total. aligned to 32.
279 ulong link; /* to next hw struct */
280 ulong epc; /* static endpoint state. addrs */
281 ulong mfs; /* static endpoint state. µ-frame sched. */
282 ulong csw; /* transfer state. updated by hw */
283 ulong buffer[2]; /* buf. ptr/offset. offset updated by hw */
284 /* buf ptr/TP/Tcnt. TP/Tcnt updated by hw */
285 ulong blink; /* back pointer */
286 /* cache-line boundary after xbuffer[0] */
287 ulong xbuffer[2]; /* high 32 bits of buffer for 64-bits */
291 ulong ndata; /* number of bytes in data */
292 ulong mdata; /* max number of bytes in data */
297 * Queue element transfer descriptor.
298 * hw: first 52 bytes, total 68+sbuff bytes. aligned to 32 bytes.
302 ulong nlink; /* to next Td */
303 ulong alink; /* alternate link to next Td */
304 ulong csw; /* cmd/sts. updated by hw */
305 ulong buffer[5]; /* buf ptrs. offset updated by hw */
306 /* cache-line boundary here */
307 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
310 Td* next; /* in qh or Isoio or free list */
311 ulong ndata; /* bytes available/used at data */
312 uchar* data; /* pointer to actual data */
313 uchar* buff; /* allocated data buffer or nil */
314 uchar sbuff[1]; /* first byte of embedded buffer */
318 * Queue head. Aligned to 32 bytes.
319 * hw: first 68 bytes, 92 total.
323 ulong link; /* to next Qh in round robin */
324 ulong eps0; /* static endpoint state. addrs */
325 ulong eps1; /* static endpoint state. µ-frame sched. */
328 ulong tclink; /* current Td (No Term bit here!) */
329 ulong nlink; /* to next Td */
330 ulong alink; /* alternate link to next Td */
331 ulong csw; /* cmd/sts. updated by hw */
332 /* cache-line boundary after buffer[0] */
333 ulong buffer[5]; /* buf ptrs. offset updated by hw */
334 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
337 Qh* next; /* in controller list/tree of Qhs */
338 int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
339 Qio* io; /* for this queue */
340 Td* tds; /* for this queue */
341 int sched; /* slot for for intr. Qhs */
342 Qh* inext; /* next in list of intr. qhs */
346 * We can avoid frame span traversal nodes if we don't span frames.
347 * Just schedule transfers that can fit on the current frame and
348 * wait a little bit otherwise.
352 * Software. Ehci descriptors provided by pool.
353 * There are soo few because we avoid using Fstn.
357 Ed* next; /* in free list */
367 static Edpool edpool;
368 static char Ebug[] = "not yet implemented";
369 static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
371 Ecapio* ehcidebugcapio;
375 ehcirun(Ctlr *ctlr, int on)
380 ddprint("ehci %#p %s\n", ctlr->capio, on ? "starting" : "halting");
387 for(i = 0; i < 100; i++)
388 if(on == 0 && (opio->sts & Shalted) != 0)
390 else if(on != 0 && (opio->sts & Shalted) == 0)
395 print("ehci %#p %s cmd timed out\n",
396 ctlr->capio, on ? "run" : "halt");
397 ddprint("ehci %#p cmd %#lux sts %#lux\n",
398 ctlr->capio, opio->cmd, opio->sts);
408 if(edpool.free == nil){
409 pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
412 for(i=Incr; --i>=0;){
413 pool[i].next = edpool.free;
414 edpool.free = &pool[i];
416 edpool.nalloc += Incr;
417 edpool.nfree += Incr;
418 dprint("ehci: edalloc: %d eds\n", edpool.nalloc);
421 edpool.free = ed->next;
426 memset(ed, 0, sizeof(Ed)); /* safety */
427 assert(((ulong)ed & 0xF) == 0);
438 ed->next = edpool.free;
446 * Allocate and do some initialization.
447 * Free after releasing buffers used.
472 td->link = td->blink = Lterm;
488 td->nlink = td->alink = Lterm;
502 tdlinktd(Td *td, Td *next)
509 td->nlink = PADDR(next);
514 qhlinkqh(Qh *qh, Qh *next)
517 qh->link = PADDR(next)|Lqh;
523 qhsetaddr(Qh *qh, ulong addr)
527 eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
528 qh->eps0 = eps0 | addr & Devmax | ((addr >> 7) & Epmax) << 8;
533 * return smallest power of 2 <= n
540 for(i = 0; (1 << (i + 1)) <= n; i++)
546 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
548 int i, j, d, upperb, q;
549 ulong best, worst, total;
551 d = flog2lower(pollival);
557 upperb = (1 << (d+1)) - 1;
558 for(i = (1 << d) - 1; i < upperb; i++){
560 for(j = i; j > 0; j = (j - 1) / 2)
569 if(worst + bw >= limit)
575 schedq(Ctlr *ctlr, Qh *qh, int pollival)
582 q = pickschedq(ctlr->tree, pollival, 0, ~0);
583 ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
584 qh->io, q, pollival, bw);
586 print("ehci: no room for ed\n");
589 ctlr->tree->bw[q] += bw;
590 tqh = ctlr->tree->root[q];
592 qhlinkqh(qh, tqh->next);
595 qh->inext = ctlr->intrqhs;
602 unschedq(Ctlr *ctlr, Qh *qh)
605 Qh *prev, *this, *next;
613 ctlr->tree->bw[q] -= bw;
615 prev = ctlr->tree->root[q];
617 while(this != nil && this != qh){
622 print("ehci: unschedq %d: not found\n", q);
625 qhlinkqh(prev, next);
627 for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
632 print("ehci: unschedq: qh %#p not found\n", qh);
638 return (qh->eps0 >> Qhmplshift) & Qhmplmask;
642 qhsetmaxpkt(Qh *qh, int maxpkt)
646 eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
647 qh->eps0 = eps0 | (maxpkt & Qhmplmask) << Qhmplshift;
652 * Initialize the round-robin circular list of ctl/bulk Qhs
653 * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
656 qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
670 qhsetmaxpkt(qh, ep->maxpkt);
671 if(ep->dev->speed == Lowspeed)
673 if(ep->dev->speed == Highspeed)
675 else if(ep->ttype == Tctl)
677 qh->eps0 |= Qhdtc | 8 << Qhrlcshift; /* 8 naks max */
679 qhsetaddr(qh, io->usbid);
680 qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
681 qh->eps1 |= ep->dev->port << Qhportshift;
682 qh->eps1 |= ep->dev->hub << Qhhubshift;
683 qh->eps1 |= 034 << Qhscmshift;
684 if(ep->ttype == Tintr)
685 qh->eps1 |= 1 << Qhismshift; /* intr. start µf. */
697 if(ctlr->qhs == nil){
698 ctlr->qhs = qhlinkqh(qh, qh);
699 qh->eps0 |= Qhhigh | Qhhrl;
701 ctlr->opio->link = PADDR(qh)|Lqh;
704 qhlinkqh(qh, ctlr->qhs->next);
705 qhlinkqh(ctlr->qhs, qh);
709 schedq(ctlr, qh, ep->pollival);
712 print("ehci: qhalloc called for ttype != ctl/bulk\n");
724 return (ctlr->opio->cmd & Ciasync) == 0;
728 * called when a qh is removed, to be sure the hw is not
729 * keeping pointers into it.
732 qhcoherency(Ctlr *ctlr)
736 qlock(&ctlr->portlck);
737 ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
739 for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++)
741 tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
744 dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
746 print("ehci: async advance doorbell did not ring\n");
747 ctlr->opio->cmd &= ~Ciasync; /* try to clean */
748 qunlock(&ctlr->portlck);
752 qhfree(Ctlr *ctlr, Qh *qh)
761 for(q = ctlr->qhs; q != nil; q = q->next)
765 panic("qhfree: nil q");
775 for(td = qh->tds; td != nil; td = ltd){
784 qhlinktd(Qh *qh, Td *td)
792 qh->csw = (csw & ~Tdactive) | Tdhalt;
794 csw &= Tddata1 | Tdping; /* save */
799 qh->nlink = PADDR(td);
800 for(i = 0; i < nelem(qh->buffer); i++)
803 qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
809 seprintlink(char *s, char *se, char *name, ulong l, int typed)
811 s = seprint(s, se, "%s %ulx", name, l);
813 return seprint(s, se, "T");
818 return seprint(s, se, "I");
820 return seprint(s, se, "Q");
822 return seprint(s, se, "S");
824 return seprint(s, se, "F");
829 seprintitd(char *s, char *se, Itd *td)
837 return seprint(s, se, "<nil itd>\n");
841 s = seprint(s, se, "itd %#p", td);
842 rw = (b1 & Itdin) ? "in" : "out";
843 s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld",
844 rw, (b0>>8)&Epmax, (b0&Devmax),
845 td->buffer[1] & 0x7ff, b1 & 3);
846 s = seprintlink(s, se, " link", td->link, 1);
847 s = seprint(s, se, "\n");
848 for(i = 0; i < nelem(td->csw); i++){
849 memset(flags, '-', 5);
850 if((td->csw[i] & Itdactive) != 0)
852 if((td->csw[i] & Itdioc) != 0)
854 if((td->csw[i] & Itddberr) != 0)
856 if((td->csw[i] & Itdbabble) != 0)
858 if((td->csw[i] & Itdtrerr) != 0)
861 s = seprint(s, se, "\ttd%d %s", i, flags);
862 s = seprint(s, se, " len %uld", (td->csw[i] >> 16) & 0x7ff);
863 s = seprint(s, se, " pg %uld", (td->csw[i] >> 12) & 0x7);
864 s = seprint(s, se, " off %uld\n", td->csw[i] & 0xfff);
866 s = seprint(s, se, "\tbuffs:");
867 for(i = 0; i < nelem(td->buffer); i++)
868 s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
869 return seprint(s, se, "\n");
873 seprintsitd(char *s, char *se, Sitd *td)
877 static char pc[4] = { 'a', 'b', 'm', 'e' };
880 return seprint(s, se, "<nil sitd>\n");
881 s = seprint(s, se, "sitd %#p", td);
882 rw = (td->epc & Stdin) ? 'r' : 'w';
883 s = seprint(s, se, " %c ep %uld dev %uld",
884 rw, (td->epc>>8)&0xf, td->epc&0x7f);
885 s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
886 s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
887 s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
888 memset(flags, '-', 7);
889 if((td->csw & Stdactive) != 0)
891 if((td->csw & Stdioc) != 0)
893 if((td->csw & Stderr) != 0)
895 if((td->csw & Stddberr) != 0)
897 if((td->csw & Stdbabble) != 0)
899 if((td->csw & Stdtrerr) != 0)
901 if((td->csw & Stdmmf) != 0)
904 ss = (td->csw & Stddcs) ? 'c' : 's';
905 pg = (td->csw & Stdpg) ? '1' : '0';
906 s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
907 s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
908 td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
909 s = seprint(s, se, "\ttpos %c tcnt %uld",
910 pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
911 s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
912 td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
913 s = seprintlink(s, se, " link", td->link, 1);
914 s = seprintlink(s, se, " blink", td->blink, 0);
915 return seprint(s, se, "\n");
921 return (td->csw >> Tdlenshift) & Tdlenmask;
929 return td->ndata - maxtdlen(td);
933 seprinttd(char *s, char *se, Td *td, char *tag)
938 static char *tok[4] = { "out", "in", "setup", "BUG" };
941 return seprint(s, se, "%s <nil td>\n", tag);
942 s = seprint(s, se, "%s %#p", tag, td);
943 s = seprintlink(s, se, " nlink", td->nlink, 0);
944 s = seprintlink(s, se, " alink", td->alink, 0);
945 s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
946 if((td->csw & Tdping) != 0)
947 s = seprint(s, se, " png");
948 memset(flags, '-', 8);
949 if((td->csw & Tdactive) != 0)
951 if((td->csw & Tdioc) != 0)
953 if((td->csw & Tdhalt) != 0)
955 if((td->csw & Tddberr) != 0)
957 if((td->csw & Tdbabble) != 0)
959 if((td->csw & Tdtrerr) != 0)
961 if((td->csw & Tdmmf) != 0)
963 if((td->csw & (Tderr2|Tderr1)) == 0)
966 t = (td->csw & Tddata1) ? '1' : '0';
967 ss = (td->csw & Tddcs) ? 'c' : 's';
968 s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
969 s = seprint(s, se, " max %uld", maxtdlen(td));
970 s = seprint(s, se, " pg %uld off %#lux\n",
971 (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
972 s = seprint(s, se, "\tbuffs:");
973 for(i = 0; i < nelem(td->buffer); i++)
974 s = seprint(s, se, " %#lux", td->buffer[i]>>12);
976 s = seprintdata(s, se, td->data, td->ndata);
977 return seprint(s, se, "\n");
981 dumptd(Td *td, char *pref)
988 se = buf+sizeof(buf);
989 for(; td != nil; td = td->next){
990 seprinttd(buf, se, td, pref);
993 print("...more tds...\n");
1005 static char *speed[] = {"full", "low", "high", "BUG"};
1008 print("<nil qh>\n");
1015 se = buf+sizeof(buf);
1016 s = seprint(buf, se, "%s %#p", tag, qh);
1017 s = seprint(s, se, " ep %uld dev %uld",
1018 (qh->eps0>>8)&0xf, qh->eps0&0x7f);
1019 s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
1020 s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
1021 s = seprintlink(s, se, " link", qh->link, 1);
1022 seprint(s, se, " clink %#lux", qh->tclink);
1024 s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
1025 s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
1026 s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
1027 if((qh->eps0 & Qhnhctl) != 0)
1028 s = seprint(s, se, "c");
1029 if((qh->eps0 & Qhhrl) != 0)
1030 s = seprint(s, se, "h");
1031 if((qh->eps0 & Qhdtc) != 0)
1032 s = seprint(s, se, "d");
1033 if((qh->eps0 & Qhint) != 0)
1034 s = seprint(s, se, "i");
1035 s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
1036 s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
1037 seprint(s, se, " scm %#lux ism %#lux\n",
1038 (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
1040 memset(&td, 0, sizeof(td));
1041 memmove(&td, &qh->nlink, 32); /* overlay area */
1042 seprinttd(buf, se, &td, "\tovl");
1047 isodump(Isoio* iso, int all)
1049 Itd *td, *tdi, *tdu;
1050 Sitd *std, *stdi, *stdu;
1055 print("<nil iso>\n");
1058 print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
1059 iso, iso->tok == Tdtokin ? "in" : "out",
1060 iso->hs ? "high" : "full",
1061 iso->state, iso->nframes, iso->maxsize);
1062 print(" td0 %uld tdi %#p tdu %#p data %#p\n",
1063 iso->td0frno, iso->tdi, iso->tdu, iso->data);
1065 print("\terr %s\n", iso->err);
1067 print("\terr='%s'\n", iso->err);
1071 seprintitd(buf, buf+sizeof(buf), tdi);
1072 print("\ttdi %s\n", buf);
1074 seprintitd(buf, buf+sizeof(buf), tdu);
1075 print("\ttdu %s\n", buf);
1078 seprintsitd(buf, buf+sizeof(buf), stdi);
1079 print("\tstdi %s\n", buf);
1081 seprintsitd(buf, buf+sizeof(buf), stdu);
1082 print("\tstdu %s\n", buf);
1085 for(i = 0; i < Nisoframes; i++)
1086 if(iso->tdps[i] != nil)
1089 seprintitd(buf, buf+sizeof(buf), td);
1094 print("[%d]\t%s", i, buf);
1096 std = iso->sitdps[i];
1097 seprintsitd(buf, buf+sizeof(buf), std);
1098 if(std == iso->stdi)
1100 if(std == iso->stdu)
1102 print("[%d]\t%s", i, buf);
1120 print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
1121 ctlr->capio, ctlr->frames, ctlr->nframes,
1122 ctlr->nintr, ctlr->ntdintr);
1123 print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
1124 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1125 opio->cmd, opio->sts, opio->intr, opio->frno);
1126 print(" base %#lux link %#lux fr0 %#lux\n",
1127 opio->frbase, opio->link, ctlr->frames[0]);
1128 se = buf+sizeof(buf);
1129 s = seprint(buf, se, "\t");
1130 for(i = 0; i < hp->nports; i++){
1131 s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
1132 if(hp->nports > 4 && i == hp->nports/2 - 1)
1133 s = seprint(s, se, "\n\t");
1141 }while(qh != ctlr->qhs && i++ < 100);
1143 print("...too many Qhs...\n");
1144 if(ctlr->intrqhs != nil)
1145 print("intr qhs:\n");
1146 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1148 if(ctlr->iso != nil)
1150 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1151 isodump(ctlr->iso, 0);
1152 print("%d eds in tree\n", ctlr->ntree);
1155 print("%d eds allocated = %d in use + %d free\n",
1156 edpool.nalloc, edpool.ninuse, edpool.nfree);
1166 return "data buffer error";
1168 return "babble detected";
1170 return "transaction error";
1172 return "missed µframe";
1174 return Estalled; /* [uo]hci report this error */
1184 return "data buffer error";
1186 return "babble detected";
1188 return "transaction error";
1196 return "translation translator error";
1197 /* other errors have same numbers than Td errors */
1207 if(iso->state == Qclose)
1209 if(iso->state == Qrun && iso->tok == Tdtokin){
1210 if(iso->hs != 0 && iso->tdi != iso->tdu)
1212 if(iso->hs == 0 && iso->stdi != iso->stdu)
1219 isocanwrite(void *a)
1224 if(iso->state == Qclose)
1226 if(iso->state == Qrun && iso->tok == Tdtokout){
1227 if(iso->hs != 0 && iso->tdu->next != iso->tdi)
1229 if(iso->hs == 0 && iso->stdu->next != iso->stdi)
1236 itdinit(Isoio *iso, Itd *td)
1239 ulong pa, tsize, size;
1242 * BUG: This does not put an integral number of samples
1243 * on each µframe unless samples per packet % 8 == 0
1244 * Also, all samples are packed early on each frame.
1247 size = td->ndata = td->mdata;
1248 pa = PADDR(td->data);
1249 for(t = 0; size > 0 && t < 8; t++){
1251 if(tsize > iso->maxsize)
1252 tsize = iso->maxsize;
1254 assert(p < nelem(td->buffer));
1255 td->csw[t] = tsize << Itdlenshift | p << Itdpgshift |
1256 (pa & 0xFFF) << Itdoffshift | Itdactive | Itdioc;
1258 if(((pa+tsize) & ~0xFFF) != (pa & ~0xFFF))
1265 sitdinit(Isoio *iso, Sitd *td)
1267 td->ndata = td->mdata & Stdlenmask;
1268 td->buffer[0] = PADDR(td->data);
1269 td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
1270 if(iso->tok == Tdtokin || td->ndata <= 188)
1271 td->buffer[1] |= Stdtpall;
1273 td->buffer[1] |= Stdtpbegin;
1274 if(iso->tok == Tdtokin)
1277 td->buffer[1] |= ((td->ndata + 187) / 188) & Stdtcntmask;
1279 td->csw = td->ndata << Stdlenshift | Stdactive | Stdioc;
1288 for(i = 0; i < nelem(td->csw); i++)
1289 if((td->csw[i] & Itdactive) != 0)
1301 if(iso->state == Qclose || iso->err || iso->delay == 0)
1308 for(i = iso->tdi; i->next != iso->tdu; i = i->next){
1312 if(delay > iso->delay)
1318 for(i = iso->stdi; i->next != iso->stdu; i = i->next){
1319 if((i->csw & Stdactive) == 0)
1322 if(delay > iso->delay)
1327 return delay <= iso->delay;
1332 isohsinterrupt(Ctlr *ctlr, Isoio *iso)
1334 int err, i, nframes, t;
1339 if(itdactive(tdi)) /* not all tds are done */
1342 ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
1343 if(iso->state != Qrun && iso->state != Qdone)
1344 panic("isofsintr: iso state");
1345 if(ehcidebug > 1 || iso->debug > 1)
1348 nframes = iso->nframes / 2; /* limit how many we look */
1349 if(nframes > Nisoframes)
1350 nframes = Nisoframes;
1352 if(iso->tok == Tdtokin)
1354 /* else, it has the number of bytes transferred */
1356 for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
1357 if(iso->tok == Tdtokin)
1358 tdi->ndata += (tdi->csw[i] >> Itdlenshift) & Itdlenmask;
1361 for(t = 0; t < nelem(tdi->csw); t++){
1362 tdi->csw[t] &= ~Itdioc;
1364 err |= tdi->csw[t] & Itderrors;
1368 else if(iso->nerrs++ > iso->nframes/2){
1369 if(iso->err == nil){
1370 iso->err = ierrmsg(err);
1371 diprint("isohsintr: tdi %#p error %#ux %s\n",
1372 tdi, err, iso->err);
1373 diprint("ctlr load %uld\n", ctlr->load);
1378 if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
1379 memset(iso->tdu->data, 0, iso->tdu->mdata);
1380 itdinit(iso, iso->tdu);
1381 iso->tdu = iso->tdu->next;
1387 ddiprint("isohsintr: %d frames processed\n", nframes);
1389 tdi->csw[0] |= Itdioc;
1394 if(isocanwrite(iso) || isocanread(iso)){
1395 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1396 iso->tdi, iso->tdu);
1403 isofsinterrupt(Ctlr *ctlr, Isoio *iso)
1405 int err, i, nframes;
1409 assert(stdi != nil);
1410 if((stdi->csw & Stdactive) != 0) /* nothing new done */
1413 ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
1414 if(iso->state != Qrun && iso->state != Qdone)
1415 panic("isofsintr: iso state");
1416 if(ehcidebug > 1 || iso->debug > 1)
1419 nframes = iso->nframes / 2; /* limit how many we look */
1420 if(nframes > Nisoframes)
1421 nframes = Nisoframes;
1423 for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
1424 stdi->csw &= ~Stdioc;
1425 /* write back csw and see if it produces errors */
1427 err = stdi->csw & Stderrors;
1430 if(iso->tok == Tdtokin)
1431 stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
1432 /* else len is assumed correct */
1433 }else if(iso->nerrs++ > iso->nframes/2){
1434 if(iso->err == nil){
1435 iso->err = serrmsg(err);
1436 diprint("isofsintr: tdi %#p error %#ux %s\n",
1437 stdi, err, iso->err);
1438 diprint("ctlr load %uld\n", ctlr->load);
1444 if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
1445 memset(iso->stdu->data, 0, iso->stdu->mdata);
1447 sitdinit(iso, iso->stdu);
1448 iso->stdu = iso->stdu->next;
1454 ddiprint("isofsintr: %d frames processed\n", nframes);
1456 stdi->csw |= Stdioc;
1461 if(isocanwrite(iso) || isocanread(iso)){
1462 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1463 iso->stdi, iso->stdu);
1470 qhinterrupt(Ctlr *ctlr, Qh *qh)
1475 if(qh->state != Qrun)
1476 panic("qhinterrupt: qh state");
1479 panic("qhinterrupt: no tds");
1480 if((td->csw & Tdactive) == 0)
1481 ddqprint("qhinterrupt port %#p qh %#p\n", ctlr->capio, qh);
1482 for(; td != nil; td = td->next){
1483 if(td->csw & Tdactive)
1485 err = td->csw & Tderrors;
1487 if(qh->io->err == nil){
1488 qh->io->err = errmsg(err);
1489 dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
1490 td, td->csw, err, qh->io->err);
1494 td->ndata = tdlen(td);
1496 if(td->ndata < maxtdlen(td)){ /* EOT */
1502 * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
1504 for(; td != nil; td = td->next)
1527 * Will we know in USB 3.0 who the interrupt was for?.
1528 * Do they still teach indexing in CS?
1529 * This is Intel's doing.
1533 sts = opio->sts & Sintrs;
1534 if(sts == 0){ /* not ours; shared intr. */
1540 if((sts & Sherr) != 0)
1541 print("ehci: port %#p fatal host system error\n", ctlr->capio);
1542 if((sts & Shalted) != 0)
1543 print("ehci: port %#p: halted\n", ctlr->capio);
1544 if((sts & Sasync) != 0){
1545 dprint("ehci: doorbell\n");
1549 * We enter always this if, even if it seems the
1550 * interrupt does not report anything done/failed.
1551 * Some controllers don't post interrupts right.
1554 if((sts & (Serrintr|Sintr)) != 0){
1557 print("ehci port %#p frames %#p nintr %d ntdintr %d",
1558 ctlr->capio, ctlr->frames,
1559 ctlr->nintr, ctlr->ntdintr);
1560 print(" nqhintr %d nisointr %d\n",
1561 ctlr->nqhintr, ctlr->nisointr);
1562 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1563 opio->cmd, opio->sts, opio->intr, opio->frno);
1566 /* process the Iso transfers */
1567 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1568 if(iso->state == Qrun || iso->state == Qdone)
1570 some += isohsinterrupt(ctlr, iso);
1572 some += isofsinterrupt(ctlr, iso);
1574 /* process the qhs in the periodic tree */
1575 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1576 if(qh->state == Qrun)
1577 some += qhinterrupt(ctlr, qh);
1579 /* process the async Qh circular list */
1584 panic("ehciintr: nil qh");
1585 if(qh->state == Qrun)
1586 some += qhinterrupt(ctlr, qh);
1588 }while(qh != ctlr->qhs && i++ < 100);
1590 print("echi: interrupt: qh loop?\n");
1593 // panic("ehciintr: no work");
1599 interrupt(Ureg*, void* a)
1605 portenable(Hci *hp, int port, int on)
1613 s = opio->portsc[port-1];
1614 eqlock(&ctlr->portlck);
1616 qunlock(&ctlr->portlck);
1619 dprint("ehci %#p port %d enable=%d; sts %#x\n",
1620 ctlr->capio, port, on, s);
1622 if(s & (Psstatuschg | Pschange))
1623 opio->portsc[port-1] = s;
1625 opio->portsc[port-1] |= Psenable;
1627 opio->portsc[port-1] &= ~Psenable;
1631 tsleep(&up->sleep, return0, 0, Enabledelay);
1632 dprint("ehci %#p port %d enable=%d: sts %#lux\n",
1633 ctlr->capio, port, on, opio->portsc[port-1]);
1634 qunlock(&ctlr->portlck);
1640 * If we detect during status that the port is low-speed or
1641 * during reset that it's full-speed, the device is not for
1642 * ourselves. The companion controller will take care.
1643 * Low-speed devices will not be seen by usbd. Full-speed
1644 * ones are seen because it's only after reset that we know what
1645 * they are (usbd may notice a device not enabled in this case).
1648 portlend(Ctlr *ctlr, int port, char *ss)
1655 dprint("ehci %#p port %d: %s speed device: no longer owned\n",
1656 ctlr->capio, port, ss);
1657 s = opio->portsc[port-1] & ~(Pschange|Psstatuschg);
1658 opio->portsc[port-1] = s | Psowner;
1663 portreset(Hci *hp, int port, int on)
1675 eqlock(&ctlr->portlck);
1678 qunlock(&ctlr->portlck);
1681 s = opio->portsc[port-1];
1682 dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, s);
1684 s &= ~(Psenable|Psreset);
1685 opio->portsc[port-1] = s | Psreset; /* initiate reset */
1688 for(i = 0; i < 50; i++){ /* was 10 */
1690 if((opio->portsc[port-1] & Psreset) == 0)
1693 if (opio->portsc[port-1] & Psreset)
1694 iprint("ehci %#p: port %d didn't reset after %d ms; sts %#lux\n",
1695 ctlr->capio, port, i * 10, opio->portsc[port-1]);
1696 opio->portsc[port-1] &= ~Psreset; /* force appearance of reset done */
1700 if((opio->portsc[port-1] & Psenable) == 0)
1701 portlend(ctlr, port, "full");
1704 dprint("ehci %#p after port %d reset; sts %#lux\n",
1705 ctlr->capio, port, opio->portsc[port-1]);
1706 qunlock(&ctlr->portlck);
1712 portstatus(Hci *hp, int port)
1720 eqlock(&ctlr->portlck);
1723 qunlock(&ctlr->portlck);
1727 s = opio->portsc[port-1];
1728 if(s & (Psstatuschg | Pschange)){
1729 opio->portsc[port-1] = s;
1731 ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
1734 * If the port is a low speed port we yield ownership now
1735 * to the [uo]hci companion controller and pretend it's not here.
1737 if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
1738 portlend(ctlr, port, "low");
1739 s &= ~Pspresent; /* not for us this time */
1742 qunlock(&ctlr->portlck);
1746 * We must return status bits as a
1747 * get port status hub request would do.
1751 r |= HPpresent|HPhigh;
1766 seprintio(char *s, char *e, Qio *io, char *pref)
1768 s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
1769 s = seprint(s,e," iot %ld", io->iotime);
1770 s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
1775 seprintep(char *s, char *e, Ep *ep)
1791 s = seprintio(s, e, cio, "c");
1792 s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
1797 if(ep->mode != OWRITE)
1798 s = seprintio(s, e, &io[OREAD], "r");
1799 if(ep->mode != OREAD)
1800 s = seprintio(s, e, &io[OWRITE], "w");
1811 * halt condition was cleared on the endpoint. update our toggles.
1824 if(ep->mode != OREAD){
1826 io[OWRITE].toggle = Tddata0;
1827 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1828 qunlock(&io[OWRITE]);
1830 if(ep->mode != OWRITE){
1832 io[OREAD].toggle = Tddata0;
1833 deprint("ep clrhalt for io %#p\n", io+OREAD);
1834 qunlock(&io[OREAD]);
1841 xdump(char* pref, void *qh)
1847 print("%s %#p:", pref, u);
1848 for(i = 0; i < 16; i++)
1850 print("\n %#8.8ulx", u[i]);
1852 print(" %#8.8ulx", u[i]);
1857 episohscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1863 for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
1868 if(tot + nr > count)
1871 print("ehci: ep%d.%d: too many polls\n",
1872 ep->dev->nb, ep->nb);
1874 iunlock(ctlr); /* We could page fault here */
1875 memmove(b+tot, tdu->data, nr);
1878 memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
1882 if(tdu->ndata == 0){
1884 iso->tdu = tdu->next;
1891 episofscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1897 for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
1899 if(stdu->csw & Stdactive){
1900 diprint("ehci: episoread: %#p tdu active\n", iso);
1904 if(tot + nr > count)
1907 print("ehci: ep%d.%d: too many polls\n",
1908 ep->dev->nb, ep->nb);
1910 iunlock(ctlr); /* We could page fault here */
1911 memmove(b+tot, stdu->data, nr);
1913 if(nr < stdu->ndata)
1914 memmove(stdu->data, stdu->data+nr,
1919 if(stdu->ndata == 0){
1920 sitdinit(iso, stdu);
1921 iso->stdu = stdu->next;
1928 episoread(Ep *ep, Isoio *iso, void *a, long count)
1934 iso->debug = ep->debug;
1935 diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1947 if(iso->state == Qclose){
1949 error(iso->err ? iso->err : Eio);
1953 while(isocanread(iso) == 0){
1955 diprint("ehci: episoread: %#p sleep\n", iso);
1958 iso->err = "I/O timed out";
1962 tsleep(iso, isocanread, iso, ep->tmout);
1966 if(iso->state == Qclose){
1968 error(iso->err ? iso->err : Eio);
1972 assert(iso->tdu != iso->tdi);
1975 tot = episohscpy(ctlr, ep, iso, b, count);
1977 tot = episofscpy(ctlr, ep, iso, b, count);
1981 diprint("uhci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
1988 * iso->tdu is the next place to put data. When it gets full
1989 * it is activated and tdu advanced.
1992 putsamples(Isoio *iso, uchar *b, long count)
1996 for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
1999 if(n > iso->tdu->mdata - iso->nleft)
2000 n = iso->tdu->mdata - iso->nleft;
2001 memmove(iso->tdu->data + iso->nleft, b + tot, n);
2004 if(iso->nleft == iso->tdu->mdata){
2005 itdinit(iso, iso->tdu);
2007 iso->tdu = iso->tdu->next;
2010 if(n > iso->stdu->mdata - iso->nleft)
2011 n = iso->stdu->mdata - iso->nleft;
2012 memmove(iso->stdu->data + iso->nleft, b + tot, n);
2015 if(iso->nleft == iso->stdu->mdata){
2016 sitdinit(iso, iso->stdu);
2018 iso->stdu = iso->stdu->next;
2026 * Queue data for writing and return error status from
2027 * last writes done, to maintain buffered data.
2030 episowrite(Ep *ep, Isoio *iso, void *a, long count)
2037 iso->delay = ep->sampledelay * ep->samplesz;
2038 iso->debug = ep->debug;
2039 diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
2048 if(iso->state == Qclose){
2050 error(iso->err ? iso->err : Eio);
2055 for(tot = 0; tot < count; tot += nw){
2056 while(isocanwrite(iso) == 0){
2058 diprint("ehci: episowrite: %#p sleep\n", iso);
2061 iso->err = "I/O timed out";
2065 tsleep(iso, isocanwrite, iso, ep->tmout);
2071 if(iso->state == Qclose || err != nil){
2073 error(err ? err : Eio);
2075 if(iso->state != Qrun)
2076 panic("episowrite: iso not running");
2077 iunlock(ctlr); /* We could page fault here */
2078 nw = putsamples(iso, b+tot, count-tot);
2081 while(isodelay(iso) == 0){
2083 sleep(iso, isodelay, iso);
2086 if(iso->state != Qclose)
2089 err = iso->err; /* in case it failed early */
2095 diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
2100 nexttoggle(int toggle, int count, int maxpkt)
2104 np = count / maxpkt;
2109 if(toggle == Tddata1)
2116 epgettd(Qio *io, int flags, void *a, int count, int maxpkt)
2122 if(count > Tdmaxpkt)
2123 panic("ehci: epgettd: too many bytes");
2125 td->csw = flags | io->toggle | io->tok | count << Tdlenshift |
2130 * use the space wasted by alignment as an
2131 * embedded buffer if count bytes fit in there.
2133 assert(Align > sizeof(Td));
2134 if(count <= Align - sizeof(Td)){
2135 td->data = td->sbuff;
2138 td->data = td->buff = smalloc(Tdmaxpkt);
2140 pa = PADDR(td->data);
2141 for(i = 0; i < nelem(td->buffer); i++){
2144 td->buffer[i] &= ~0xFFF;
2148 if(a != nil && count > 0)
2149 memmove(td->data, a, count);
2151 io->toggle = nexttoggle(io->toggle, count, maxpkt);
2157 * Try to get them idle
2166 if(qh->sched >= 0 && (qh->eps0 & Qhspeedmask) != Qhhigh)
2167 qh->eps0 |= Qhint; /* inactivate on next pass */
2169 for(td = qh->tds; td != nil; td = td->next){
2170 if(td->csw & Tdactive)
2178 * Some controllers do not post the usb/error interrupt after
2179 * the work has been done. It seems that we must poll for them.
2182 workpending(void *a)
2187 return ctlr->nreqs > 0;
2202 if(ctlr->nreqs == 0){
2203 if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
2204 sleep(poll, workpending, ctlr);
2205 if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
2207 for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
2208 if(ehciintr(hp) == 0)
2211 tsleep(&up->sleep, return0, 0, 1);
2213 }while(ctlr->nreqs > 0);
2226 if(poll->must != 0 && poll->does == 0){
2228 if(poll->must != 0 && poll->does == 0){
2230 print("ehci %#p: polling\n", ctlr->capio);
2231 kproc("ehcipoll", ehcipoll, hp);
2243 return qh->state != Qrun;
2247 epiowait(Hci *hp, Qio *io, int tmout, ulong load)
2255 ddqprint("ehci io %#p sleep on qh %#p state %s\n",
2256 io, qh, qhsname[qh->state]);
2259 dqprint("ehci io %#p qh %#p timed out\n", io, qh);
2263 sleep(io, epiodone, qh);
2265 tsleep(io, epiodone, qh, tmout);
2270 /* Are we missing interrupts? */
2271 if(qh->state == Qrun){
2275 if(qh->state == Qdone){
2276 dqprint("ehci %#p: polling required\n", ctlr->capio);
2277 ctlr->poll.must = 1;
2282 if(qh->state == Qrun){
2283 dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
2285 }else if(qh->state != Qdone && qh->state != Qclose)
2286 panic("ehci: epio: queue state %d", qh->state);
2289 io->err = "request timed out";
2292 tsleep(&up->sleep, return0, 0, Abortdelay);
2297 if(qh->state != Qclose)
2308 * To make it work for control transfers, the caller may
2309 * lock the Qio for the entire control transfer.
2312 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
2314 int saved, ntds, tmout;
2322 Td *td, *ltd, *td0, *ntd;
2326 io->debug = ep->debug;
2328 ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
2329 io->tok == Tdtokin ? "in" : "out",
2330 ep->dev->nb, ep->nb, io, count, ctlr->load);
2331 if((ehcidebug > 1 || ep->debug > 1) && io->tok != Tdtokin){
2332 seprintdata(buf, buf+sizeof(buf), a, count);
2333 print("echi epio: user data: %s\n", buf);
2344 if(qh->state == Qclose){ /* Tds released by cancelio */
2346 error(io->err ? io->err : Eio);
2348 if(qh->state != Qidle)
2349 panic("epio: qh not idle");
2350 qh->state = Qinstall;
2357 n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
2360 if(c != nil && io->tok != Tdtokin)
2361 td = epgettd(io, Tdactive, c+tot, n, ep->maxpkt);
2363 td = epgettd(io, Tdactive, nil, n, ep->maxpkt);
2371 }while(tot < count);
2372 if(td0 == nil || ltd == nil)
2373 panic("epio: no td");
2375 ltd->csw |= Tdioc; /* the last one interrupts */
2378 ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
2379 if(ehcidebug > 1 || ep->debug > 1)
2380 dumptd(td0, "epio: put: ");
2383 if(qh->state != Qclose){
2384 io->iotime = TK2MS(MACHP(0)->ticks);
2394 wakeup(&ctlr->poll);
2396 epiowait(ep->hp, io, tmout, load);
2397 if(ehcidebug > 1 || ep->debug > 1){
2398 dumptd(td0, "epio: got: ");
2406 for(td = td0; td != nil; td = ntd){
2409 * Use td tok, not io tok, because of setup packets.
2410 * Also, if the Td was stalled or active (previous Td
2411 * was a short packet), we must save the toggle as it is.
2413 if(td->csw & (Tdhalt|Tdactive)){
2415 io->toggle = td->csw & Tddata1;
2420 if(c != nil && (td->csw & Tdtok) == Tdtokin && td->ndata > 0){
2421 memmove(c, td->data, td->ndata);
2433 ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
2434 io, ntds, tot, err);
2436 return 0; /* that's our convention */
2445 epread(Ep *ep, void *a, long count)
2453 ddeprint("ehci: epread\n");
2455 panic("epread: not open");
2467 ddeprint("epread ctl ndata %d\n", cio->ndata);
2469 error("request expected");
2470 else if(cio->ndata == 0){
2474 if(count > cio->ndata)
2477 memmove(a, cio->data, count);
2478 /* BUG for big transfers */
2481 cio->ndata = 0; /* signal EOF next time */
2485 if(ehcidebug>1 || ep->debug){
2486 seprintdata(buf, buf+sizeof(buf), a, count);
2487 print("epread: %s\n", buf);
2494 return epio(ep, &io[OREAD], a, count, 1);
2497 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
2498 if(delta < ep->pollival / 2)
2499 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
2502 return epio(ep, &io[OREAD], a, count, 1);
2505 return episoread(ep, iso, a, count);
2511 * Control transfers are one setup write (data0)
2512 * plus zero or more reads/writes (data1, data0, ...)
2513 * plus a final write/read with data1 to ack.
2514 * For both host to device and device to host we perform
2515 * the entire transfer when the user writes the request,
2516 * and keep any data read from the device for a later read.
2517 * We call epio three times instead of placing all Tds at
2518 * the same time because doing so leads to crc/tmout errors
2520 * Upon errors on the data phase we must still run the status
2521 * phase or the device may cease responding in the future.
2524 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
2529 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
2530 cio, ep->dev->nb, ep->nb, count);
2531 if(count < Rsetuplen)
2532 error("short usb comand");
2545 /* set the address if unset and out of configuration state */
2546 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
2547 if(cio->usbid == 0){
2548 cio->usbid = (ep->nb&Epmax) << 7 | ep->dev->nb&Devmax;
2550 qhsetaddr(cio->qh, cio->usbid);
2552 /* adjust maxpkt if the user has learned a different one */
2553 if(qhmaxpkt(cio->qh) != ep->maxpkt)
2554 qhsetmaxpkt(cio->qh, ep->maxpkt);
2556 cio->tok = Tdtoksetup;
2557 cio->toggle = Tddata0;
2559 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
2564 cio->toggle = Tddata1;
2565 if(c[Rtype] & Rd2h){
2567 len = GET2(c+Rcount);
2569 error("bad length in d2h request");
2571 error("d2h data too large to fit in ehci");
2572 a = cio->data = smalloc(len+1);
2574 cio->tok = Tdtokout;
2582 len = epio(ep, cio, a, len, 0);
2585 if(c[Rtype] & Rd2h){
2588 cio->tok = Tdtokout;
2593 count = Rsetuplen + len;
2596 cio->toggle = Tddata1;
2598 epio(ep, cio, nil, 0, 0);
2601 ddeprint("epctlio cio %#p return %ld\n", cio, count);
2606 epwrite(Ep *ep, void *a, long count)
2615 ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
2617 panic("ehci: epwrite: not open");
2621 return epctlio(ep, cio, a, count);
2626 return epio(ep, &io[OWRITE], a, count, 1);
2629 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
2630 if(delta < ep->pollival)
2631 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
2634 return epio(ep, &io[OWRITE], a, count, 1);
2637 return episowrite(ep, iso, a, count);
2643 isofsinit(Ep *ep, Isoio *iso)
2652 frno = iso->td0frno;
2653 for(i = 0; i < iso->nframes; i++){
2655 td->data = iso->data + i * ep->maxpkt;
2656 td->epc = ep->dev->port << Stdportshift;
2657 td->epc |= ep->dev->hub << Stdhubshift;
2658 td->epc |= ep->nb << Stdepshift;
2659 td->epc |= ep->dev->nb << Stddevshift;
2660 td->mfs = 034 << Stdscmshift | 1 << Stdssmshift;
2661 if(ep->mode == OREAD){
2663 td->mdata = ep->maxpkt;
2665 td->mdata = (ep->hz+left) * ep->pollival / 1000;
2666 td->mdata *= ep->samplesz;
2667 left = (ep->hz+left) * ep->pollival % 1000;
2668 if(td->mdata > ep->maxpkt){
2669 print("ehci: ep%d.%d: size > maxpkt\n",
2670 ep->dev->nb, ep->nb);
2671 print("size = %ld max = %ld\n",
2672 td->mdata,ep->maxpkt);
2673 td->mdata = ep->maxpkt;
2678 iso->sitdps[frno] = td;
2684 frno = TRUNC(frno+ep->pollival, Nisoframes);
2686 ltd->next = iso->sitdps[iso->td0frno];
2691 isohsinit(Ep *ep, Isoio *iso)
2700 if(ep->pollival > 8)
2701 ival = ep->pollival/8;
2704 frno = iso->td0frno;
2705 for(i = 0; i < iso->nframes; i++){
2707 td->data = iso->data + i * 8 * iso->maxsize;
2708 pa = PADDR(td->data) & ~0xFFF;
2709 for(p = 0; p < 8; p++)
2710 td->buffer[i] = pa + p * 0x1000;
2711 td->buffer[0] = PADDR(iso->data) & ~0xFFF |
2712 ep->nb << Itdepshift | ep->dev->nb << Itddevshift;
2713 if(ep->mode == OREAD)
2714 td->buffer[1] |= Itdin;
2716 td->buffer[1] |= Itdout;
2717 td->buffer[1] |= ep->maxpkt << Itdmaxpktshift;
2718 td->buffer[2] |= ep->ntds << Itdntdsshift;
2720 if(ep->mode == OREAD)
2721 td->mdata = 8 * iso->maxsize;
2723 td->mdata = (ep->hz + left) * ep->pollival / 1000;
2724 td->mdata *= ep->samplesz;
2725 left = (ep->hz + left) * ep->pollival % 1000;
2728 iso->itdps[frno] = td;
2734 frno = TRUNC(frno + ival, Nisoframes);
2739 isoopen(Ctlr *ctlr, Ep *ep)
2741 int ival; /* pollival in ms */
2742 int tpf; /* tds per frame */
2753 iso->tok = Tdtokout;
2756 error("iso i/o is half-duplex");
2758 iso->usbid = ep->nb << 7 | ep->dev->nb & Devmax;
2761 iso->debug = ep->debug;
2762 ival = ep->pollival;
2764 if(ep->dev->speed == Highspeed){
2772 iso->nframes = Nisoframes / ival;
2773 if(iso->nframes < 3)
2774 error("uhci isoopen bug"); /* we need at least 3 tds */
2775 iso->maxsize = ep->ntds * ep->maxpkt;
2776 if(ctlr->load + ep->load > 800)
2777 print("usb: ehci: bandwidth may be exceeded\n");
2779 ctlr->load += ep->load;
2780 ctlr->isoload += ep->load;
2782 dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
2783 diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
2784 iso->nframes, ep->pollival, ival, ep->maxpkt, ep->ntds);
2787 wakeup(&ctlr->poll);
2790 * From here on this cannot raise errors
2791 * unless we catch them and release here all memory allocated.
2793 assert(ep->maxpkt > 0 && ep->ntds > 0 && ep->ntds < 4);
2794 assert(ep->maxpkt <= 1024);
2795 iso->tdps = smalloc(sizeof(uintptr) * Nisoframes);
2796 iso->data = smalloc(iso->nframes * tpf * ep->ntds * ep->maxpkt);
2797 iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
2798 /* read: now; write: 1s ahead */
2800 if(ep->dev->speed == Highspeed)
2804 iso->tdu = iso->tdi = iso->itdps[iso->td0frno];
2805 iso->stdu = iso->stdi = iso->sitdps[iso->td0frno];
2809 frno = iso->td0frno;
2810 for(i = 0; i < iso->nframes; i++){
2811 *iso->tdps[frno] = ctlr->frames[frno];
2812 frno = TRUNC(frno+ival, Nisoframes);
2816 * Iso uses a virtual frame window of Nisoframes, and we must
2817 * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
2818 * copies of the window in the frame array.
2820 assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
2821 assert(Nisoframes >= Nintrleafs);
2822 n = ctlr->nframes / Nisoframes;
2823 for(w = 0; w < n; w++){
2824 frno = iso->td0frno;
2825 woff = w * Nisoframes;
2826 for(i = 0; i < iso->nframes ; i++){
2827 assert(woff+frno < ctlr->nframes);
2828 assert(iso->tdps[frno] != nil);
2829 if(ep->dev->speed == Highspeed)
2830 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2833 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2836 frno = TRUNC(frno+ep->pollival, Nisoframes);
2840 iso->next = ctlr->iso;
2845 if(ehcidebug > 1 || iso->debug >1)
2850 * Allocate the endpoint and set it up for I/O
2851 * in the controller. This must follow what's said
2852 * in Ep regarding configuration, including perhaps
2853 * the saved toggles (saved on a previous close of
2854 * the endpoint data file by epclose).
2865 deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2867 panic("ehci: epopen called with open ep");
2875 error("endpoint not configured");
2877 ep->aux = smalloc(sizeof(Isoio));
2881 cio = ep->aux = smalloc(sizeof(Ctlio));
2882 cio->debug = ep->debug;
2885 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2887 cio->qh = qhalloc(ctlr, ep, cio, "epc");
2890 ep->pollival = 1; /* assume this; doesn't really matter */
2893 io = ep->aux = smalloc(sizeof(Qio)*2);
2894 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2895 usbid = (ep->nb&Epmax) << 7 | ep->dev->nb &Devmax;
2896 assert(ep->pollival != 0);
2897 if(ep->mode != OREAD){
2898 if(ep->toggle[OWRITE] != 0)
2899 io[OWRITE].toggle = Tddata1;
2901 io[OWRITE].toggle = Tddata0;
2902 io[OWRITE].tok = Tdtokout;
2903 io[OWRITE].usbid = usbid;
2904 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2905 io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
2907 if(ep->mode != OWRITE){
2908 if(ep->toggle[OREAD] != 0)
2909 io[OREAD].toggle = Tddata1;
2911 io[OREAD].toggle = Tddata0;
2912 io[OREAD].tok = Tdtokin;
2913 io[OREAD].usbid = usbid;
2914 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2915 io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
2920 if(ehcidebug>1 || ep->debug)
2922 deprint("ehci: epopen done\n");
2927 cancelio(Ctlr *ctlr, Qio *io)
2933 if(io == nil || io->qh == nil || io->qh->state == Qclose){
2937 dqprint("ehci: cancelio for qh %#p state %s\n",
2938 qh, qhsname[qh->state]);
2943 tsleep(&up->sleep, return0, 0, Abortdelay);
2948 /* wait for epio if running */
2956 cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
2958 int frno, i, n, t, w, woff;
2965 if(iso->state == Qclose){
2970 if(iso->state != Qrun && iso->state != Qdone)
2971 panic("bad iso state");
2972 iso->state = Qclose;
2974 if(ctlr->isoload < load)
2975 panic("ehci: low isoload");
2976 ctlr->isoload -= load;
2978 for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
2982 panic("cancleiso: not found");
2985 frno = iso->td0frno;
2986 for(i = 0; i < iso->nframes; i++){
2987 tp = iso->tdps[frno];
2989 td = iso->itdps[frno];
2990 for(t = 0; t < nelem(td->csw); t++)
2991 td->csw[t] &= ~(Itdioc|Itdactive);
2993 std = iso->sitdps[frno];
2994 std->csw &= ~(Stdioc|Stdactive);
2997 for(lp = &ctlr->frames[frno]; !(*lp & Lterm);
3002 panic("cancelisoio: td not found");
3005 * Iso uses a virtual frame window of Nisoframes, and we must
3006 * restore pointers in copies of the window kept at ctlr->frames.
3008 if(lp == &ctlr->frames[frno]){
3009 n = ctlr->nframes / Nisoframes;
3010 for(w = 1; w < n; w++){
3011 woff = w * Nisoframes;
3012 ctlr->frames[woff+frno] = *lp;
3016 frno = TRUNC(frno+pollival, Nisoframes);
3021 * wakeup anyone waiting for I/O and
3022 * wait to be sure no I/O is in progress in the controller.
3023 * and then wait to be sure episo* is no longer running.
3026 diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
3027 tsleep(&up->sleep, return0, 0, 5);
3030 diprint("cancelisoio iso %#p releasing iso\n", iso);
3032 frno = iso->td0frno;
3033 for(i = 0; i < iso->nframes; i++){
3035 itdfree(iso->itdps[frno]);
3037 sitdfree(iso->sitdps[frno]);
3038 iso->tdps[frno] = nil;
3039 frno = TRUNC(frno+pollival, Nisoframes);
3057 deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
3060 panic("ehci: epclose called with closed ep");
3064 cancelio(ctlr, cio);
3071 ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
3072 if(ep->mode != OWRITE){
3073 cancelio(ctlr, &io[OREAD]);
3074 if(io[OREAD].toggle == Tddata1)
3075 ep->toggle[OREAD] = 1;
3077 if(ep->mode != OREAD){
3078 cancelio(ctlr, &io[OWRITE]);
3079 if(io[OWRITE].toggle == Tddata1)
3080 ep->toggle[OWRITE] = 1;
3086 cancelisoio(ctlr, iso, ep->pollival, ep->load);
3089 panic("epclose: bad ttype");
3096 * return smallest power of 2 >= n
3103 for(i = 0; (1 << i) < n; i++)
3109 * build the periodic scheduling tree:
3110 * framesize must be a multiple of the tree size
3113 mkqhtree(Ctlr *ctlr)
3115 int i, n, d, o, leaf0, depth;
3116 ulong leafs[Nintrleafs];
3121 depth = flog2(Nintrleafs);
3122 n = (1 << (depth+1)) - 1;
3123 qt = mallocz(sizeof(*qt), 1);
3125 panic("ehci: mkqhtree: no memory");
3128 qt->bw = mallocz(n * sizeof(qt->bw), 1);
3129 qt->root = tree = mallocz(n * sizeof(Qh *), 1);
3130 if(qt->bw == nil || tree == nil)
3131 panic("ehci: mkqhtree: no memory");
3132 for(i = 0; i < n; i++){
3133 tree[i] = qh = edalloc();
3135 panic("ehci: mkqhtree: no memory");
3136 qh->nlink = qh->alink = qh->link = Lterm;
3141 qhlinkqh(tree[i], tree[(i-1)/2]);
3144 dprint("ehci: tree: %d endpoints allocated\n", i);
3146 /* distribute leaves evenly round the frame list */
3148 for(i = 0; i < Nintrleafs; i++){
3150 for(d = 0; d < depth; d++){
3156 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
3159 leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
3161 assert((ctlr->nframes % Nintrleafs) == 0);
3162 for(i = 0; i < ctlr->nframes; i += Nintrleafs){
3163 memmove(ctlr->frames + i, leafs, sizeof leafs);
3171 ehcimeminit(Ctlr *ctlr)
3177 frsize = ctlr->nframes * sizeof(ulong);
3178 assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
3179 ctlr->frames = xspanalloc(frsize, frsize, 0);
3180 if(ctlr->frames == nil)
3181 panic("ehci reset: no memory");
3183 for (i = 0; i < ctlr->nframes; i++)
3184 ctlr->frames[i] = Lterm;
3185 opio->frbase = PADDR(ctlr->frames);
3189 qhalloc(ctlr, nil, nil, nil); /* init async list */
3190 mkqhtree(ctlr); /* init sync list */
3191 edfree(edalloc()); /* try to get some ones pre-allocated */
3193 dprint("ehci %#p flb %#lux frno %#lux\n",
3194 ctlr->capio, opio->frbase, opio->frno);
3207 dprint("ehci %#p init\n", ctlr->capio);
3211 * Unless we activate frroll interrupt
3212 * some machines won't post other interrupts.
3214 opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
3221 opio->config = Callmine; /* reclaim all ports */
3224 for (i = 0; i < hp->nports; i++)
3225 opio->portsc[i] = Pspower;
3232 ehcilinkage(Hci *hp)
3236 hp->interrupt = interrupt;
3237 hp->epopen = epopen;
3238 hp->epclose = epclose;
3239 hp->epread = epread;
3240 hp->epwrite = epwrite;
3241 hp->seprintep = seprintep;
3242 hp->portenable = portenable;
3243 hp->portreset = portreset;
3244 hp->portstatus = portstatus;
3245 // hp->shutdown = shutdown;
3246 // hp->debug = setdebug;