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 = 10, /* 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)
520 qh->link = PADDR(next)|Lqh;
526 qhsetaddr(Qh *qh, ulong addr)
530 eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
531 qh->eps0 = eps0 | addr & Devmax | ((addr >> 7) & Epmax) << 8;
536 * return smallest power of 2 <= n
543 for(i = 0; (1 << (i + 1)) <= n; i++)
549 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
551 int i, j, d, upperb, q;
552 ulong best, worst, total;
554 d = flog2lower(pollival);
560 upperb = (1 << (d+1)) - 1;
561 for(i = (1 << d) - 1; i < upperb; i++){
563 for(j = i; j > 0; j = (j - 1) / 2)
572 if(worst + bw >= limit)
578 schedq(Ctlr *ctlr, Qh *qh, int pollival)
585 q = pickschedq(ctlr->tree, pollival, 0, ~0);
586 ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
587 qh->io, q, pollival, bw);
589 print("ehci: no room for ed\n");
592 ctlr->tree->bw[q] += bw;
593 tqh = ctlr->tree->root[q];
595 qhlinkqh(qh, tqh->next);
598 qh->inext = ctlr->intrqhs;
605 unschedq(Ctlr *ctlr, Qh *qh)
608 Qh *prev, *this, *next;
616 ctlr->tree->bw[q] -= bw;
618 prev = ctlr->tree->root[q];
620 while(this != nil && this != qh){
625 print("ehci: unschedq %d: not found\n", q);
628 qhlinkqh(prev, next);
630 for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
635 print("ehci: unschedq: qh %#p not found\n", qh);
641 return (qh->eps0 >> Qhmplshift) & Qhmplmask;
645 qhsetmaxpkt(Qh *qh, int maxpkt)
649 eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
650 qh->eps0 = eps0 | (maxpkt & Qhmplmask) << Qhmplshift;
655 * Initialize the round-robin circular list of ctl/bulk Qhs
656 * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
659 qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
673 qhsetmaxpkt(qh, ep->maxpkt);
674 if(ep->dev->speed == Lowspeed)
676 if(ep->dev->speed == Highspeed)
678 else if(ep->ttype == Tctl)
680 qh->eps0 |= Qhdtc | 8 << Qhrlcshift; /* 8 naks max */
682 qhsetaddr(qh, io->usbid);
683 qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
684 qh->eps1 |= ep->dev->port << Qhportshift;
685 qh->eps1 |= ep->dev->hub << Qhhubshift;
686 qh->eps1 |= 034 << Qhscmshift;
687 if(ep->ttype == Tintr)
688 qh->eps1 |= 1 << Qhismshift; /* intr. start µf. */
700 if(ctlr->qhs == nil){
701 ctlr->qhs = qhlinkqh(qh, qh);
702 qh->eps0 |= Qhhigh | Qhhrl;
704 ctlr->opio->link = PADDR(qh)|Lqh;
707 qhlinkqh(qh, ctlr->qhs->next);
708 qhlinkqh(ctlr->qhs, qh);
712 schedq(ctlr, qh, ep->pollival);
715 print("ehci: qhalloc called for ttype != ctl/bulk\n");
727 return (ctlr->opio->cmd & Ciasync) == 0;
731 * called when a qh is removed, to be sure the hw is not
732 * keeping pointers into it.
735 qhcoherency(Ctlr *ctlr)
739 qlock(&ctlr->portlck);
740 ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
742 for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++){
745 tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
748 dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
750 print("ehci: async advance doorbell did not ring\n");
751 ctlr->opio->cmd &= ~Ciasync; /* try to clean */
752 qunlock(&ctlr->portlck);
756 qhfree(Ctlr *ctlr, Qh *qh)
763 for(q = ctlr->qhs; q != nil; q = q->next)
767 panic("qhfree: nil q");
773 qh->state = Qfree; /* paranoia */
778 while((td = qh->tds) != nil){
787 qhlinktd(Qh *qh, Td *td)
795 qh->csw = (csw & ~Tdactive) | Tdhalt;
797 csw &= Tddata1 | Tdping; /* save */
802 qh->nlink = PADDR(td);
803 for(i = 0; i < nelem(qh->buffer); i++)
806 qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
812 seprintlink(char *s, char *se, char *name, ulong l, int typed)
814 s = seprint(s, se, "%s %ulx", name, l);
816 return seprint(s, se, "T");
821 return seprint(s, se, "I");
823 return seprint(s, se, "Q");
825 return seprint(s, se, "S");
827 return seprint(s, se, "F");
832 seprintitd(char *s, char *se, Itd *td)
840 return seprint(s, se, "<nil itd>\n");
844 s = seprint(s, se, "itd %#p", td);
845 rw = (b1 & Itdin) ? "in" : "out";
846 s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld",
847 rw, (b0>>8)&Epmax, (b0&Devmax),
848 td->buffer[1] & 0x7ff, b1 & 3);
849 s = seprintlink(s, se, " link", td->link, 1);
850 s = seprint(s, se, "\n");
851 for(i = 0; i < nelem(td->csw); i++){
852 memset(flags, '-', 5);
853 if((td->csw[i] & Itdactive) != 0)
855 if((td->csw[i] & Itdioc) != 0)
857 if((td->csw[i] & Itddberr) != 0)
859 if((td->csw[i] & Itdbabble) != 0)
861 if((td->csw[i] & Itdtrerr) != 0)
864 s = seprint(s, se, "\ttd%d %s", i, flags);
865 s = seprint(s, se, " len %uld", (td->csw[i] >> 16) & 0x7ff);
866 s = seprint(s, se, " pg %uld", (td->csw[i] >> 12) & 0x7);
867 s = seprint(s, se, " off %uld\n", td->csw[i] & 0xfff);
869 s = seprint(s, se, "\tbuffs:");
870 for(i = 0; i < nelem(td->buffer); i++)
871 s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
872 return seprint(s, se, "\n");
876 seprintsitd(char *s, char *se, Sitd *td)
880 static char pc[4] = { 'a', 'b', 'm', 'e' };
883 return seprint(s, se, "<nil sitd>\n");
884 s = seprint(s, se, "sitd %#p", td);
885 rw = (td->epc & Stdin) ? 'r' : 'w';
886 s = seprint(s, se, " %c ep %uld dev %uld",
887 rw, (td->epc>>8)&0xf, td->epc&0x7f);
888 s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
889 s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
890 s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
891 memset(flags, '-', 7);
892 if((td->csw & Stdactive) != 0)
894 if((td->csw & Stdioc) != 0)
896 if((td->csw & Stderr) != 0)
898 if((td->csw & Stddberr) != 0)
900 if((td->csw & Stdbabble) != 0)
902 if((td->csw & Stdtrerr) != 0)
904 if((td->csw & Stdmmf) != 0)
907 ss = (td->csw & Stddcs) ? 'c' : 's';
908 pg = (td->csw & Stdpg) ? '1' : '0';
909 s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
910 s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
911 td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
912 s = seprint(s, se, "\ttpos %c tcnt %uld",
913 pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
914 s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
915 td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
916 s = seprintlink(s, se, " link", td->link, 1);
917 s = seprintlink(s, se, " blink", td->blink, 0);
918 return seprint(s, se, "\n");
924 return (td->csw >> Tdlenshift) & Tdlenmask;
932 return td->ndata - maxtdlen(td);
936 seprinttd(char *s, char *se, Td *td, char *tag)
941 static char *tok[4] = { "out", "in", "setup", "BUG" };
944 return seprint(s, se, "%s <nil td>\n", tag);
945 s = seprint(s, se, "%s %#p", tag, td);
946 s = seprintlink(s, se, " nlink", td->nlink, 0);
947 s = seprintlink(s, se, " alink", td->alink, 0);
948 s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
949 if((td->csw & Tdping) != 0)
950 s = seprint(s, se, " png");
951 memset(flags, '-', 8);
952 if((td->csw & Tdactive) != 0)
954 if((td->csw & Tdioc) != 0)
956 if((td->csw & Tdhalt) != 0)
958 if((td->csw & Tddberr) != 0)
960 if((td->csw & Tdbabble) != 0)
962 if((td->csw & Tdtrerr) != 0)
964 if((td->csw & Tdmmf) != 0)
966 if((td->csw & (Tderr2|Tderr1)) == 0)
969 t = (td->csw & Tddata1) ? '1' : '0';
970 ss = (td->csw & Tddcs) ? 'c' : 's';
971 s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
972 s = seprint(s, se, " max %uld", maxtdlen(td));
973 s = seprint(s, se, " pg %uld off %#lux\n",
974 (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
975 s = seprint(s, se, "\tbuffs:");
976 for(i = 0; i < nelem(td->buffer); i++)
977 s = seprint(s, se, " %#lux", td->buffer[i]>>12);
979 s = seprintdata(s, se, td->data, td->ndata);
980 return seprint(s, se, "\n");
984 dumptd(Td *td, char *pref)
991 se = buf+sizeof(buf);
992 for(; td != nil; td = td->next){
993 seprinttd(buf, se, td, pref);
996 print("...more tds...\n");
1008 static char *speed[] = {"full", "low", "high", "BUG"};
1011 print("<nil qh>\n");
1018 se = buf+sizeof(buf);
1019 s = seprint(buf, se, "%s %#p", tag, qh);
1020 s = seprint(s, se, " ep %uld dev %uld",
1021 (qh->eps0>>8)&0xf, qh->eps0&0x7f);
1022 s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
1023 s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
1024 s = seprintlink(s, se, " link", qh->link, 1);
1025 seprint(s, se, " clink %#lux", qh->tclink);
1027 s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
1028 s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
1029 s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
1030 if((qh->eps0 & Qhnhctl) != 0)
1031 s = seprint(s, se, "c");
1032 if((qh->eps0 & Qhhrl) != 0)
1033 s = seprint(s, se, "h");
1034 if((qh->eps0 & Qhdtc) != 0)
1035 s = seprint(s, se, "d");
1036 if((qh->eps0 & Qhint) != 0)
1037 s = seprint(s, se, "i");
1038 s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
1039 s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
1040 seprint(s, se, " scm %#lux ism %#lux\n",
1041 (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
1043 memset(&td, 0, sizeof(td));
1044 memmove(&td, &qh->nlink, 32); /* overlay area */
1045 seprinttd(buf, se, &td, "\tovl");
1050 isodump(Isoio* iso, int all)
1052 Itd *td, *tdi, *tdu;
1053 Sitd *std, *stdi, *stdu;
1058 print("<nil iso>\n");
1061 print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
1062 iso, iso->tok == Tdtokin ? "in" : "out",
1063 iso->hs ? "high" : "full",
1064 iso->state, iso->nframes, iso->maxsize);
1065 print(" td0 %uld tdi %#p tdu %#p data %#p\n",
1066 iso->td0frno, iso->tdi, iso->tdu, iso->data);
1068 print("\terr %s\n", iso->err);
1070 print("\terr='%s'\n", iso->err);
1074 seprintitd(buf, buf+sizeof(buf), tdi);
1075 print("\ttdi %s\n", buf);
1077 seprintitd(buf, buf+sizeof(buf), tdu);
1078 print("\ttdu %s\n", buf);
1081 seprintsitd(buf, buf+sizeof(buf), stdi);
1082 print("\tstdi %s\n", buf);
1084 seprintsitd(buf, buf+sizeof(buf), stdu);
1085 print("\tstdu %s\n", buf);
1088 for(i = 0; i < Nisoframes; i++)
1089 if(iso->tdps[i] != nil)
1092 seprintitd(buf, buf+sizeof(buf), td);
1097 print("[%d]\t%s", i, buf);
1099 std = iso->sitdps[i];
1100 seprintsitd(buf, buf+sizeof(buf), std);
1101 if(std == iso->stdi)
1103 if(std == iso->stdu)
1105 print("[%d]\t%s", i, buf);
1123 print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
1124 ctlr->capio, ctlr->frames, ctlr->nframes,
1125 ctlr->nintr, ctlr->ntdintr);
1126 print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
1127 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1128 opio->cmd, opio->sts, opio->intr, opio->frno);
1129 print(" base %#lux link %#lux fr0 %#lux\n",
1130 opio->frbase, opio->link, ctlr->frames[0]);
1131 se = buf+sizeof(buf);
1132 s = seprint(buf, se, "\t");
1133 for(i = 0; i < hp->nports; i++){
1134 s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
1135 if(hp->nports > 4 && i == hp->nports/2 - 1)
1136 s = seprint(s, se, "\n\t");
1144 }while(qh != ctlr->qhs && i++ < 100);
1146 print("...too many Qhs...\n");
1147 if(ctlr->intrqhs != nil)
1148 print("intr qhs:\n");
1149 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1151 if(ctlr->iso != nil)
1153 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1154 isodump(ctlr->iso, 0);
1155 print("%d eds in tree\n", ctlr->ntree);
1158 print("%d eds allocated = %d in use + %d free\n",
1159 edpool.nalloc, edpool.ninuse, edpool.nfree);
1169 return "data buffer error";
1171 return "babble detected";
1173 return "transaction error";
1175 return "missed µframe";
1177 return Estalled; /* [uo]hci report this error */
1187 return "data buffer error";
1189 return "babble detected";
1191 return "transaction error";
1199 return "translation translator error";
1200 /* other errors have same numbers than Td errors */
1210 if(iso->state == Qclose)
1212 if(iso->state == Qrun && iso->tok == Tdtokin){
1213 if(iso->hs != 0 && iso->tdi != iso->tdu)
1215 if(iso->hs == 0 && iso->stdi != iso->stdu)
1222 isocanwrite(void *a)
1227 if(iso->state == Qclose)
1229 if(iso->state == Qrun && iso->tok == Tdtokout){
1230 if(iso->hs != 0 && iso->tdu->next != iso->tdi)
1232 if(iso->hs == 0 && iso->stdu->next != iso->stdi)
1239 itdinit(Isoio *iso, Itd *td)
1242 ulong pa, tsize, size;
1245 * BUG: This does not put an integral number of samples
1246 * on each µframe unless samples per packet % 8 == 0
1247 * Also, all samples are packed early on each frame.
1250 size = td->ndata = td->mdata;
1251 pa = PADDR(td->data);
1252 for(t = 0; size > 0 && t < 8; t++){
1254 if(tsize > iso->maxsize)
1255 tsize = iso->maxsize;
1257 assert(p < nelem(td->buffer));
1258 td->csw[t] = tsize << Itdlenshift | p << Itdpgshift |
1259 (pa & 0xFFF) << Itdoffshift | Itdactive | Itdioc;
1261 if(((pa+tsize) & ~0xFFF) != (pa & ~0xFFF))
1268 sitdinit(Isoio *iso, Sitd *td)
1270 td->ndata = td->mdata & Stdlenmask;
1271 td->buffer[0] = PADDR(td->data);
1272 td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
1273 if(iso->tok == Tdtokin || td->ndata <= 188)
1274 td->buffer[1] |= Stdtpall;
1276 td->buffer[1] |= Stdtpbegin;
1277 if(iso->tok == Tdtokin)
1280 td->buffer[1] |= ((td->ndata + 187) / 188) & Stdtcntmask;
1282 td->csw = td->ndata << Stdlenshift | Stdactive | Stdioc;
1291 for(i = 0; i < nelem(td->csw); i++)
1292 if((td->csw[i] & Itdactive) != 0)
1304 if(iso->state == Qclose || iso->err || iso->delay == 0)
1311 for(i = iso->tdi; i->next != iso->tdu; i = i->next){
1315 if(delay > iso->delay)
1321 for(i = iso->stdi; i->next != iso->stdu; i = i->next){
1322 if((i->csw & Stdactive) == 0)
1325 if(delay > iso->delay)
1330 return delay <= iso->delay;
1335 isohsinterrupt(Ctlr *ctlr, Isoio *iso)
1337 int err, i, nframes, t;
1341 if(tdi == nil || itdactive(tdi)) /* not all tds are done */
1344 ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
1345 if(iso->state != Qrun && iso->state != Qdone)
1346 panic("isofsintr: iso state");
1347 if(ehcidebug > 1 || iso->debug > 1)
1350 nframes = iso->nframes / 2; /* limit how many we look */
1351 if(nframes > Nisoframes)
1352 nframes = Nisoframes;
1354 if(iso->tok == Tdtokin)
1356 /* else, it has the number of bytes transferred */
1358 for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
1359 if(iso->tok == Tdtokin)
1360 tdi->ndata += (tdi->csw[i] >> Itdlenshift) & Itdlenmask;
1363 for(t = 0; t < nelem(tdi->csw); t++){
1364 tdi->csw[t] &= ~Itdioc;
1366 err |= tdi->csw[t] & Itderrors;
1370 else if(iso->nerrs++ > iso->nframes/2){
1371 if(iso->err == nil){
1372 iso->err = ierrmsg(err);
1373 diprint("isohsintr: tdi %#p error %#ux %s\n",
1374 tdi, err, iso->err);
1375 diprint("ctlr load %uld\n", ctlr->load);
1380 if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
1381 memset(iso->tdu->data, 0, iso->tdu->mdata);
1382 itdinit(iso, iso->tdu);
1383 iso->tdu = iso->tdu->next;
1389 ddiprint("isohsintr: %d frames processed\n", nframes);
1391 tdi->csw[0] |= Itdioc;
1396 if(isocanwrite(iso) || isocanread(iso)){
1397 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1398 iso->tdi, iso->tdu);
1405 isofsinterrupt(Ctlr *ctlr, Isoio *iso)
1407 int err, i, nframes;
1411 if(stdi == nil || (stdi->csw & Stdactive) != 0) /* nothing new done */
1414 ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
1415 if(iso->state != Qrun && iso->state != Qdone)
1416 panic("isofsintr: iso state");
1417 if(ehcidebug > 1 || iso->debug > 1)
1420 nframes = iso->nframes / 2; /* limit how many we look */
1421 if(nframes > Nisoframes)
1422 nframes = Nisoframes;
1424 for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
1425 stdi->csw &= ~Stdioc;
1426 /* write back csw and see if it produces errors */
1428 err = stdi->csw & Stderrors;
1431 if(iso->tok == Tdtokin)
1432 stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
1433 /* else len is assumed correct */
1434 }else if(iso->nerrs++ > iso->nframes/2){
1435 if(iso->err == nil){
1436 iso->err = serrmsg(err);
1437 diprint("isofsintr: tdi %#p error %#ux %s\n",
1438 stdi, err, iso->err);
1439 diprint("ctlr load %uld\n", ctlr->load);
1445 if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
1446 memset(iso->stdu->data, 0, iso->stdu->mdata);
1448 sitdinit(iso, iso->stdu);
1449 iso->stdu = iso->stdu->next;
1455 ddiprint("isofsintr: %d frames processed\n", nframes);
1457 stdi->csw |= Stdioc;
1462 if(isocanwrite(iso) || isocanread(iso)){
1463 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1464 iso->stdi, iso->stdu);
1471 qhinterrupt(Ctlr *ctlr, Qh *qh)
1476 if(qh->state != Qrun)
1477 panic("qhinterrupt: qh state");
1481 if((td->csw & Tdactive) == 0)
1482 ddqprint("qhinterrupt port %#p qh %#p\n", ctlr->capio, qh);
1483 for(; td != nil; td = td->next){
1484 if(td->csw & Tdactive)
1486 err = td->csw & Tderrors;
1488 if(qh->io->err == nil){
1489 qh->io->err = errmsg(err);
1490 dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
1491 td, td->csw, err, qh->io->err);
1495 td->ndata = tdlen(td);
1497 if(td->ndata < maxtdlen(td)){ /* EOT */
1503 * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
1505 for(; td != nil; td = td->next)
1515 ctlrinterrupt(Ctlr *ctlr)
1525 * Will we know in USB 3.0 who the interrupt was for?.
1526 * Do they still teach indexing in CS?
1527 * This is Intel's doing.
1529 sts = opio->sts & Sintrs;
1530 if(sts == 0) /* not ours; shared intr. */
1535 if((sts & Sherr) != 0)
1536 iprint("ehci: port %#p fatal host system error\n", ctlr->capio);
1537 if((sts & Shalted) != 0)
1538 iprint("ehci: port %#p: halted\n", ctlr->capio);
1539 if((sts & Sasync) != 0){
1540 dprint("ehci: doorbell\n");
1544 * We enter always this if, even if it seems the
1545 * interrupt does not report anything done/failed.
1546 * Some controllers don't post interrupts right.
1549 if((sts & (Serrintr|Sintr)) != 0){
1552 iprint("ehci port %#p frames %#p nintr %d ntdintr %d",
1553 ctlr->capio, ctlr->frames,
1554 ctlr->nintr, ctlr->ntdintr);
1555 iprint(" nqhintr %d nisointr %d\n",
1556 ctlr->nqhintr, ctlr->nisointr);
1557 iprint("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1558 opio->cmd, opio->sts, opio->intr, opio->frno);
1561 /* process the Iso transfers */
1562 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1563 if(iso->state == Qrun || iso->state == Qdone)
1565 some += isohsinterrupt(ctlr, iso);
1567 some += isofsinterrupt(ctlr, iso);
1569 /* process the qhs in the periodic tree */
1570 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1571 if(qh->state == Qrun)
1572 some += qhinterrupt(ctlr, qh);
1574 /* process the async Qh circular list */
1580 if(qh->state == Qrun)
1581 some += qhinterrupt(ctlr, qh);
1583 }while(qh != ctlr->qhs && i++ < 100);
1585 iprint("echi: interrupt: qh loop?\n");
1598 some = ctlrinterrupt(ctlr);
1604 interrupt(Ureg*, void* a)
1610 portenable(Hci *hp, int port, int on)
1618 s = opio->portsc[port-1];
1619 eqlock(&ctlr->portlck);
1621 qunlock(&ctlr->portlck);
1624 dprint("ehci %#p port %d enable=%d; sts %#x\n",
1625 ctlr->capio, port, on, s);
1627 if(s & (Psstatuschg | Pschange))
1628 opio->portsc[port-1] = s;
1630 opio->portsc[port-1] |= Psenable;
1632 opio->portsc[port-1] &= ~Psenable;
1636 tsleep(&up->sleep, return0, 0, Enabledelay);
1637 dprint("ehci %#p port %d enable=%d: sts %#lux\n",
1638 ctlr->capio, port, on, opio->portsc[port-1]);
1639 qunlock(&ctlr->portlck);
1645 * If we detect during status that the port is low-speed or
1646 * during reset that it's full-speed, the device is not for
1647 * ourselves. The companion controller will take care.
1648 * Low-speed devices will not be seen by usbd. Full-speed
1649 * ones are seen because it's only after reset that we know what
1650 * they are (usbd may notice a device not enabled in this case).
1653 portlend(Ctlr *ctlr, int port, char *ss)
1660 dprint("ehci %#p port %d: %s speed device: no longer owned\n",
1661 ctlr->capio, port, ss);
1662 s = opio->portsc[port-1] & ~(Pschange|Psstatuschg);
1663 opio->portsc[port-1] = s | Psowner;
1668 portreset(Hci *hp, int port, int on)
1680 eqlock(&ctlr->portlck);
1683 qunlock(&ctlr->portlck);
1686 portscp = &opio->portsc[port-1];
1687 dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, *portscp);
1689 /* Shalted must be zero, else Psreset will stay set */
1690 if (opio->sts & Shalted)
1691 iprint("ehci %#p: halted yet trying to reset port\n",
1694 *portscp = (*portscp & ~Psenable) | Psreset; /* initiate reset */
1696 * usb 2 spec: reset must finish within 20 ms.
1697 * linux says spec says it can take 50 ms. for hubs.
1700 *portscp &= ~Psreset; /* terminate reset */
1703 for(i = 0; *portscp & Psreset && i < 10; i++)
1706 if (*portscp & Psreset)
1707 iprint("ehci %#p: port %d didn't reset; sts %#lux\n",
1708 ctlr->capio, port, *portscp);
1710 delay(10); /* ehci spec: enable within 2 ms. */
1711 if((*portscp & Psenable) == 0)
1712 portlend(ctlr, port, "full");
1715 dprint("ehci %#p after port %d reset; sts %#lux\n",
1716 ctlr->capio, port, *portscp);
1717 qunlock(&ctlr->portlck);
1723 portstatus(Hci *hp, int port)
1731 eqlock(&ctlr->portlck);
1734 qunlock(&ctlr->portlck);
1738 s = opio->portsc[port-1];
1739 if(s & (Psstatuschg | Pschange)){
1740 opio->portsc[port-1] = s;
1742 ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
1745 * If the port is a low speed port we yield ownership now
1746 * to the [uo]hci companion controller and pretend it's not here.
1748 if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
1749 portlend(ctlr, port, "low");
1750 s &= ~Pspresent; /* not for us this time */
1753 qunlock(&ctlr->portlck);
1757 * We must return status bits as a
1758 * get port status hub request would do.
1762 r |= HPpresent|HPhigh;
1777 seprintio(char *s, char *e, Qio *io, char *pref)
1779 s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
1780 s = seprint(s,e," iot %ld", io->iotime);
1781 s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
1786 seprintep(char *s, char *e, Ep *ep)
1802 s = seprintio(s, e, cio, "c");
1803 s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
1808 if(ep->mode != OWRITE)
1809 s = seprintio(s, e, &io[OREAD], "r");
1810 if(ep->mode != OREAD)
1811 s = seprintio(s, e, &io[OWRITE], "w");
1822 * halt condition was cleared on the endpoint. update our toggles.
1835 if(ep->mode != OREAD){
1837 io[OWRITE].toggle = Tddata0;
1838 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1839 qunlock(&io[OWRITE]);
1841 if(ep->mode != OWRITE){
1843 io[OREAD].toggle = Tddata0;
1844 deprint("ep clrhalt for io %#p\n", io+OREAD);
1845 qunlock(&io[OREAD]);
1852 xdump(char* pref, void *qh)
1858 print("%s %#p:", pref, u);
1859 for(i = 0; i < 16; i++)
1861 print("\n %#8.8ulx", u[i]);
1863 print(" %#8.8ulx", u[i]);
1868 episohscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1874 for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
1879 if(tot + nr > count)
1882 print("ehci: ep%d.%d: too many polls\n",
1883 ep->dev->nb, ep->nb);
1885 iunlock(ctlr); /* We could page fault here */
1886 memmove(b+tot, tdu->data, nr);
1891 memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
1895 if(tdu->ndata == 0){
1897 iso->tdu = tdu->next;
1904 episofscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1910 for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
1912 if(stdu->csw & Stdactive){
1913 diprint("ehci: episoread: %#p tdu active\n", iso);
1917 if(tot + nr > count)
1920 print("ehci: ep%d.%d: too many polls\n",
1921 ep->dev->nb, ep->nb);
1923 iunlock(ctlr); /* We could page fault here */
1924 memmove(b+tot, stdu->data, nr);
1926 if(iso->stdu != stdu)
1928 if(nr < stdu->ndata)
1929 memmove(stdu->data, stdu->data+nr,
1934 if(stdu->ndata == 0){
1935 sitdinit(iso, stdu);
1936 iso->stdu = stdu->next;
1943 episoread(Ep *ep, Isoio *iso, void *a, long count)
1949 iso->debug = ep->debug;
1950 diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1962 if(iso->state == Qclose){
1964 error(iso->err ? iso->err : Eio);
1968 while(isocanread(iso) == 0){
1970 diprint("ehci: episoread: %#p sleep\n", iso);
1973 iso->err = "I/O timed out";
1977 tsleep(iso, isocanread, iso, ep->tmout);
1981 if(iso->state == Qclose){
1983 error(iso->err ? iso->err : Eio);
1987 assert(iso->tdu != iso->tdi);
1990 tot = episohscpy(ctlr, ep, iso, b, count);
1992 tot = episofscpy(ctlr, ep, iso, b, count);
1996 diprint("uhci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
2003 * iso->tdu is the next place to put data. When it gets full
2004 * it is activated and tdu advanced.
2007 putsamples(Ctlr *ctlr, Isoio *iso, uchar *b, long count)
2013 for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
2018 if(n > tdu->mdata - left)
2019 n = tdu->mdata - left;
2020 iunlock(ctlr); /* We could page fault here */
2021 memmove(tdu->data + left, b + tot, n);
2026 if(iso->nleft == tdu->mdata){
2028 iso->tdu = tdu->next;
2033 if(n > stdu->mdata - left)
2034 n = stdu->mdata - left;
2035 iunlock(ctlr); /* We could page fault here */
2036 memmove(stdu->data + left, b + tot, n);
2038 if(iso->stdu != stdu)
2041 if(iso->nleft == stdu->mdata){
2042 sitdinit(iso, stdu);
2043 iso->stdu = stdu->next;
2052 * Queue data for writing and return error status from
2053 * last writes done, to maintain buffered data.
2056 episowrite(Ep *ep, Isoio *iso, void *a, long count)
2063 iso->delay = ep->sampledelay * ep->samplesz;
2064 iso->debug = ep->debug;
2065 diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
2074 if(iso->state == Qclose){
2076 error(iso->err ? iso->err : Eio);
2081 for(tot = 0; tot < count; tot += nw){
2082 while(isocanwrite(iso) == 0){
2084 diprint("ehci: episowrite: %#p sleep\n", iso);
2087 iso->err = "I/O timed out";
2091 tsleep(iso, isocanwrite, iso, ep->tmout);
2097 if(iso->state == Qclose || err != nil){
2099 error(err ? err : Eio);
2101 if(iso->state != Qrun)
2102 panic("episowrite: iso not running");
2103 nw = putsamples(ctlr, iso, b+tot, count-tot);
2105 while(isodelay(iso) == 0){
2107 sleep(iso, isodelay, iso);
2110 if(iso->state != Qclose)
2113 err = iso->err; /* in case it failed early */
2119 diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
2124 nexttoggle(int toggle, int count, int maxpkt)
2128 np = count / maxpkt;
2133 if(toggle == Tddata1)
2140 epgettd(Qio *io, int flags, void *a, int count, int maxpkt)
2146 if(count > Tdmaxpkt)
2147 panic("ehci: epgettd: too many bytes");
2149 td->csw = flags | io->toggle | io->tok | count << Tdlenshift |
2154 * use the space wasted by alignment as an
2155 * embedded buffer if count bytes fit in there.
2157 assert(Align > sizeof(Td));
2158 if(count <= Align - sizeof(Td)){
2159 td->data = td->sbuff;
2161 } else if(count <= 0x4000){
2162 td->buff = td->data = smalloc(count);
2164 td->buff = smalloc(count + 0x1000);
2165 td->data = (uchar*)ROUND((uintptr)td->buff, 0x1000);
2168 pa = PADDR(td->data);
2169 for(i = 0; i < nelem(td->buffer); i++){
2175 if(a != nil && count > 0)
2176 memmove(td->data, a, count);
2178 io->toggle = nexttoggle(io->toggle, count, maxpkt);
2184 * Try to get them idle
2191 if(qh->sched >= 0 && (qh->eps0 & Qhspeedmask) != Qhhigh)
2192 qh->eps0 |= Qhint; /* inactivate on next pass */
2193 qh->csw = (qh->csw & ~Tdactive) | Tdhalt;
2195 for(td = qh->tds; td != nil; td = td->next){
2196 if(td->csw & Tdactive){
2205 * Some controllers do not post the usb/error interrupt after
2206 * the work has been done. It seems that we must poll for them.
2209 workpending(void *a)
2214 return ctlr->nreqs > 0;
2229 if(ctlr->nreqs == 0){
2230 if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
2231 sleep(poll, workpending, ctlr);
2232 if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
2234 for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
2235 if(ehciintr(hp) == 0)
2238 tsleep(&up->sleep, return0, 0, 1);
2240 }while(ctlr->nreqs > 0);
2253 if(poll->must != 0 && poll->does == 0){
2255 if(poll->must != 0 && poll->does == 0){
2257 print("ehci %#p: polling\n", ctlr->capio);
2258 kproc("ehcipoll", ehcipoll, hp);
2270 return qh->state != Qrun;
2274 epiowait(Hci *hp, Qio *io, int tmout, ulong load)
2282 ddqprint("ehci io %#p sleep on qh %#p state %s\n",
2283 io, qh, qhsname[qh->state]);
2286 dqprint("ehci io %#p qh %#p timed out\n", io, qh);
2290 sleep(io, epiodone, qh);
2292 tsleep(io, epiodone, qh, tmout);
2297 /* Are we missing interrupts? */
2298 if(qh->state == Qrun){
2299 ctlrinterrupt(ctlr);
2300 if(qh->state == Qdone){
2301 dqprint("ehci %#p: polling required\n", ctlr->capio);
2302 ctlr->poll.must = 1;
2307 if(qh->state == Qrun){
2308 dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
2310 }else if(qh->state != Qdone && qh->state != Qclose)
2311 panic("ehci: epio: queue state %d", qh->state);
2316 io->err = "request timed out";
2320 tsleep(&up->sleep, return0, 0, Abortdelay);
2324 if(qh->state != Qclose)
2334 * To make it work for control transfers, the caller may
2335 * lock the Qio for the entire control transfer.
2338 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
2340 int saved, ntds, tmout;
2348 Td *td, *ltd, *td0, *ntd;
2351 io->debug = ep->debug;
2353 ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
2354 io->tok == Tdtokin ? "in" : "out",
2355 ep->dev->nb, ep->nb, io, count, ctlr->load);
2356 if((ehcidebug > 1 || ep->debug > 1) && io->tok != Tdtokin){
2357 seprintdata(buf, buf+sizeof(buf), a, count);
2358 print("echi epio: user data: %s\n", buf);
2370 if(qh == nil || qh->state == Qclose){ /* Tds released by cancelio */
2372 error(io->err ? io->err : Eio);
2374 if(qh->state != Qidle)
2375 panic("epio: qh not idle");
2376 qh->state = Qinstall;
2383 n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
2386 if(c != nil && io->tok != Tdtokin)
2387 td = epgettd(io, Tdactive, c+tot, n, ep->maxpkt);
2389 td = epgettd(io, Tdactive, nil, n, ep->maxpkt);
2397 }while(tot < count);
2398 if(td0 == nil || ltd == nil)
2399 panic("epio: no td");
2401 ltd->csw |= Tdioc; /* the last one interrupts */
2404 ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
2405 if(ehcidebug > 1 || ep->debug > 1)
2406 dumptd(td0, "epio: put: ");
2409 if(qh->state != Qclose){
2410 io->iotime = TK2MS(MACHP(0)->ticks);
2420 wakeup(&ctlr->poll);
2422 epiowait(ep->hp, io, tmout, load);
2423 if(ehcidebug > 1 || ep->debug > 1){
2424 dumptd(td0, "epio: got: ");
2433 for(td = td0; td != nil; td = ntd){
2436 * Use td tok, not io tok, because of setup packets.
2437 * Also, we must save the next toggle value from the
2438 * last completed Td (in case of a short packet, or
2439 * fewer than the requested number of packets in the
2440 * Td being transferred).
2442 if(td->csw & (Tdhalt|Tdactive))
2446 io->toggle = td->csw & Tddata1;
2449 if(err == nil && (n = td->ndata) > 0 && tot < count){
2450 if((tot + n) > count)
2452 if(c != nil && (td->csw & Tdtok) == Tdtokin){
2453 memmove(c, td->data, n);
2466 ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
2467 io, ntds, tot, err);
2469 return 0; /* that's our convention */
2476 epread(Ep *ep, void *a, long count)
2484 ddeprint("ehci: epread\n");
2486 panic("epread: not open");
2498 ddeprint("epread ctl ndata %d\n", cio->ndata);
2500 error("request expected");
2501 else if(cio->ndata == 0){
2505 if(count > cio->ndata)
2508 memmove(a, cio->data, count);
2509 /* BUG for big transfers */
2512 cio->ndata = 0; /* signal EOF next time */
2516 if(ehcidebug>1 || ep->debug){
2517 seprintdata(buf, buf+sizeof(buf), a, count);
2518 print("epread: %s\n", buf);
2525 return epio(ep, &io[OREAD], a, count, 1);
2528 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
2529 if(delta < ep->pollival / 2)
2530 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
2533 return epio(ep, &io[OREAD], a, count, 1);
2536 return episoread(ep, iso, a, count);
2542 * Control transfers are one setup write (data0)
2543 * plus zero or more reads/writes (data1, data0, ...)
2544 * plus a final write/read with data1 to ack.
2545 * For both host to device and device to host we perform
2546 * the entire transfer when the user writes the request,
2547 * and keep any data read from the device for a later read.
2548 * We call epio three times instead of placing all Tds at
2549 * the same time because doing so leads to crc/tmout errors
2551 * Upon errors on the data phase we must still run the status
2552 * phase or the device may cease responding in the future.
2555 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
2560 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
2561 cio, ep->dev->nb, ep->nb, count);
2562 if(count < Rsetuplen)
2563 error("short usb comand");
2576 /* set the address if unset and out of configuration state */
2577 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
2578 if(cio->usbid == 0){
2579 cio->usbid = (ep->nb&Epmax) << 7 | ep->dev->nb&Devmax;
2581 qhsetaddr(cio->qh, cio->usbid);
2583 /* adjust maxpkt if the user has learned a different one */
2584 if(qhmaxpkt(cio->qh) != ep->maxpkt)
2585 qhsetmaxpkt(cio->qh, ep->maxpkt);
2587 cio->tok = Tdtoksetup;
2588 cio->toggle = Tddata0;
2590 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
2595 cio->toggle = Tddata1;
2596 if(c[Rtype] & Rd2h){
2598 len = GET2(c+Rcount);
2600 error("bad length in d2h request");
2602 error("d2h data too large to fit in ehci");
2603 a = cio->data = smalloc(len+1);
2605 cio->tok = Tdtokout;
2613 len = epio(ep, cio, a, len, 0);
2616 if(c[Rtype] & Rd2h){
2619 cio->tok = Tdtokout;
2624 count = Rsetuplen + len;
2627 cio->toggle = Tddata1;
2629 epio(ep, cio, nil, 0, 0);
2632 ddeprint("epctlio cio %#p return %ld\n", cio, count);
2637 epwrite(Ep *ep, void *a, long count)
2646 ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
2648 panic("ehci: epwrite: not open");
2652 return epctlio(ep, cio, a, count);
2657 return epio(ep, &io[OWRITE], a, count, 1);
2660 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
2661 if(delta < ep->pollival)
2662 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
2665 return epio(ep, &io[OWRITE], a, count, 1);
2668 return episowrite(ep, iso, a, count);
2674 isofsinit(Ep *ep, Isoio *iso)
2683 frno = iso->td0frno;
2684 for(i = 0; i < iso->nframes; i++){
2686 td->data = iso->data + i * ep->maxpkt;
2687 td->epc = ep->dev->port << Stdportshift;
2688 td->epc |= ep->dev->hub << Stdhubshift;
2689 td->epc |= ep->nb << Stdepshift;
2690 td->epc |= ep->dev->nb << Stddevshift;
2691 td->mfs = 034 << Stdscmshift | 1 << Stdssmshift;
2692 if(ep->mode == OREAD){
2694 td->mdata = ep->maxpkt;
2696 td->mdata = (ep->hz+left) * ep->pollival / 1000;
2697 td->mdata *= ep->samplesz;
2698 left = (ep->hz+left) * ep->pollival % 1000;
2699 if(td->mdata > ep->maxpkt){
2700 print("ehci: ep%d.%d: size > maxpkt\n",
2701 ep->dev->nb, ep->nb);
2702 print("size = %ld max = %ld\n",
2703 td->mdata,ep->maxpkt);
2704 td->mdata = ep->maxpkt;
2709 iso->sitdps[frno] = td;
2715 frno = TRUNC(frno+ep->pollival, Nisoframes);
2717 ltd->next = iso->sitdps[iso->td0frno];
2722 isohsinit(Ep *ep, Isoio *iso)
2731 if(ep->pollival > 8)
2732 ival = ep->pollival/8;
2735 frno = iso->td0frno;
2736 for(i = 0; i < iso->nframes; i++){
2738 td->data = iso->data + i * 8 * iso->maxsize;
2739 pa = PADDR(td->data) & ~0xFFF;
2740 for(p = 0; p < nelem(td->buffer); p++){
2744 td->buffer[0] |= ep->nb << Itdepshift | ep->dev->nb << Itddevshift;
2745 if(ep->mode == OREAD)
2746 td->buffer[1] |= Itdin;
2748 td->buffer[1] |= Itdout;
2749 td->buffer[1] |= ep->maxpkt << Itdmaxpktshift;
2750 td->buffer[2] |= ep->ntds << Itdntdsshift;
2752 if(ep->mode == OREAD)
2753 td->mdata = 8 * iso->maxsize;
2755 td->mdata = (ep->hz + left) * ep->pollival / 1000;
2756 td->mdata *= ep->samplesz;
2757 left = (ep->hz + left) * ep->pollival % 1000;
2760 iso->itdps[frno] = td;
2766 frno = TRUNC(frno + ival, Nisoframes);
2771 isoopen(Ctlr *ctlr, Ep *ep)
2773 int ival; /* pollival in ms */
2774 int tpf; /* tds per frame */
2785 iso->tok = Tdtokout;
2788 error("iso i/o is half-duplex");
2790 iso->usbid = ep->nb << 7 | ep->dev->nb & Devmax;
2793 iso->debug = ep->debug;
2794 ival = ep->pollival;
2796 if(ep->dev->speed == Highspeed){
2804 iso->nframes = Nisoframes / ival;
2805 if(iso->nframes < 3)
2806 error("uhci isoopen bug"); /* we need at least 3 tds */
2807 iso->maxsize = ep->ntds * ep->maxpkt;
2808 if(ctlr->load + ep->load > 800)
2809 print("usb: ehci: bandwidth may be exceeded\n");
2811 ctlr->load += ep->load;
2812 ctlr->isoload += ep->load;
2814 dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
2815 diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
2816 iso->nframes, ep->pollival, ival, ep->maxpkt, ep->ntds);
2819 wakeup(&ctlr->poll);
2822 * From here on this cannot raise errors
2823 * unless we catch them and release here all memory allocated.
2825 assert(ep->maxpkt > 0 && ep->ntds > 0 && ep->ntds < 4);
2826 assert(ep->maxpkt <= 1024);
2827 iso->tdps = smalloc(sizeof(uintptr) * Nisoframes);
2828 iso->data = smalloc(iso->nframes * tpf * ep->ntds * ep->maxpkt);
2829 iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
2830 /* read: now; write: 1s ahead */
2832 if(ep->dev->speed == Highspeed)
2836 iso->tdu = iso->tdi = iso->itdps[iso->td0frno];
2837 iso->stdu = iso->stdi = iso->sitdps[iso->td0frno];
2841 frno = iso->td0frno;
2842 for(i = 0; i < iso->nframes; i++){
2843 *iso->tdps[frno] = ctlr->frames[frno];
2844 frno = TRUNC(frno+ival, Nisoframes);
2848 * Iso uses a virtual frame window of Nisoframes, and we must
2849 * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
2850 * copies of the window in the frame array.
2852 assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
2853 assert(Nisoframes >= Nintrleafs);
2854 n = ctlr->nframes / Nisoframes;
2855 for(w = 0; w < n; w++){
2856 frno = iso->td0frno;
2857 woff = w * Nisoframes;
2858 for(i = 0; i < iso->nframes ; i++){
2859 assert(woff+frno < ctlr->nframes);
2860 assert(iso->tdps[frno] != nil);
2861 if(ep->dev->speed == Highspeed)
2862 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2865 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2868 frno = TRUNC(frno+ep->pollival, Nisoframes);
2872 iso->next = ctlr->iso;
2877 if(ehcidebug > 1 || iso->debug >1)
2882 * Allocate the endpoint and set it up for I/O
2883 * in the controller. This must follow what's said
2884 * in Ep regarding configuration, including perhaps
2885 * the saved toggles (saved on a previous close of
2886 * the endpoint data file by epclose).
2897 deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2899 panic("ehci: epopen called with open ep");
2907 error("endpoint not configured");
2909 ep->aux = smalloc(sizeof(Isoio));
2913 cio = ep->aux = smalloc(sizeof(Ctlio));
2914 cio->debug = ep->debug;
2917 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2919 cio->qh = qhalloc(ctlr, ep, cio, "epc");
2922 ep->pollival = 1; /* assume this; doesn't really matter */
2925 io = ep->aux = smalloc(sizeof(Qio)*2);
2926 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2927 usbid = (ep->nb&Epmax) << 7 | ep->dev->nb &Devmax;
2928 assert(ep->pollival != 0);
2929 if(ep->mode != OREAD){
2930 if(ep->toggle[OWRITE] != 0)
2931 io[OWRITE].toggle = Tddata1;
2933 io[OWRITE].toggle = Tddata0;
2934 io[OWRITE].tok = Tdtokout;
2935 io[OWRITE].usbid = usbid;
2936 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2937 io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
2939 if(ep->mode != OWRITE){
2940 if(ep->toggle[OREAD] != 0)
2941 io[OREAD].toggle = Tddata1;
2943 io[OREAD].toggle = Tddata0;
2944 io[OREAD].tok = Tdtokin;
2945 io[OREAD].usbid = usbid;
2946 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2947 io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
2952 if(ehcidebug>1 || ep->debug)
2954 deprint("ehci: epopen done\n");
2959 cancelio(Ctlr *ctlr, Qio *io)
2965 if(qh == nil || qh->state == Qclose){
2969 dqprint("ehci: cancelio for qh %#p state %s\n",
2970 qh, qhsname[qh->state]);
2976 tsleep(&up->sleep, return0, 0, Abortdelay);
2980 /* wait for epio if running */
2989 cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
2991 int frno, i, n, t, w, woff;
2998 if(iso->state == Qclose){
3003 if(iso->state != Qrun && iso->state != Qdone)
3004 panic("bad iso state");
3005 iso->state = Qclose;
3007 if(ctlr->isoload < load)
3008 panic("ehci: low isoload");
3009 ctlr->isoload -= load;
3011 for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
3015 panic("cancleiso: not found");
3018 frno = iso->td0frno;
3019 for(i = 0; i < iso->nframes; i++){
3020 tp = iso->tdps[frno];
3022 td = iso->itdps[frno];
3023 for(t = 0; t < nelem(td->csw); t++)
3024 td->csw[t] &= ~(Itdioc|Itdactive);
3026 std = iso->sitdps[frno];
3027 std->csw &= ~(Stdioc|Stdactive);
3030 for(lp = &ctlr->frames[frno]; !(*lp & Lterm);
3035 panic("cancelisoio: td not found");
3038 * Iso uses a virtual frame window of Nisoframes, and we must
3039 * restore pointers in copies of the window kept at ctlr->frames.
3041 if(lp == &ctlr->frames[frno]){
3042 n = ctlr->nframes / Nisoframes;
3043 for(w = 1; w < n; w++){
3044 woff = w * Nisoframes;
3045 ctlr->frames[woff+frno] = *lp;
3049 frno = TRUNC(frno+pollival, Nisoframes);
3054 * wakeup anyone waiting for I/O and
3055 * wait to be sure no I/O is in progress in the controller.
3056 * and then wait to be sure episo* is no longer running.
3059 diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
3060 tsleep(&up->sleep, return0, 0, 5);
3063 diprint("cancelisoio iso %#p releasing iso\n", iso);
3065 frno = iso->td0frno;
3066 for(i = 0; i < iso->nframes; i++){
3068 itdfree(iso->itdps[frno]);
3070 sitdfree(iso->sitdps[frno]);
3071 iso->tdps[frno] = nil;
3072 frno = TRUNC(frno+pollival, Nisoframes);
3090 deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
3093 panic("ehci: epclose called with closed ep");
3097 cancelio(ctlr, cio);
3104 ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
3105 if(ep->mode != OWRITE){
3106 cancelio(ctlr, &io[OREAD]);
3107 if(io[OREAD].toggle == Tddata1)
3108 ep->toggle[OREAD] = 1;
3110 if(ep->mode != OREAD){
3111 cancelio(ctlr, &io[OWRITE]);
3112 if(io[OWRITE].toggle == Tddata1)
3113 ep->toggle[OWRITE] = 1;
3119 cancelisoio(ctlr, iso, ep->pollival, ep->load);
3122 panic("epclose: bad ttype");
3129 * return smallest power of 2 >= n
3136 for(i = 0; (1 << i) < n; i++)
3142 * build the periodic scheduling tree:
3143 * framesize must be a multiple of the tree size
3146 mkqhtree(Ctlr *ctlr)
3148 int i, n, d, o, leaf0, depth;
3149 ulong leafs[Nintrleafs];
3154 depth = flog2(Nintrleafs);
3155 n = (1 << (depth+1)) - 1;
3156 qt = mallocz(sizeof(*qt), 1);
3158 panic("ehci: mkqhtree: no memory");
3161 qt->bw = mallocz(n * sizeof(qt->bw), 1);
3162 qt->root = tree = mallocz(n * sizeof(Qh *), 1);
3163 if(qt->bw == nil || tree == nil)
3164 panic("ehci: mkqhtree: no memory");
3165 for(i = 0; i < n; i++){
3166 tree[i] = qh = edalloc();
3168 panic("ehci: mkqhtree: no memory");
3169 qh->nlink = qh->alink = qh->link = Lterm;
3174 qhlinkqh(tree[i], tree[(i-1)/2]);
3177 dprint("ehci: tree: %d endpoints allocated\n", i);
3179 /* distribute leaves evenly round the frame list */
3181 for(i = 0; i < Nintrleafs; i++){
3183 for(d = 0; d < depth; d++){
3189 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
3192 leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
3194 assert((ctlr->nframes % Nintrleafs) == 0);
3195 for(i = 0; i < ctlr->nframes; i += Nintrleafs){
3196 memmove(ctlr->frames + i, leafs, sizeof leafs);
3204 ehcimeminit(Ctlr *ctlr)
3210 frsize = ctlr->nframes * sizeof(ulong);
3211 assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
3212 ctlr->frames = xspanalloc(frsize, frsize, 0);
3213 if(ctlr->frames == nil)
3214 panic("ehci reset: no memory");
3216 for (i = 0; i < ctlr->nframes; i++)
3217 ctlr->frames[i] = Lterm;
3218 opio->frbase = PADDR(ctlr->frames);
3222 qhalloc(ctlr, nil, nil, nil); /* init async list */
3223 mkqhtree(ctlr); /* init sync list */
3224 edfree(edalloc()); /* try to get some ones pre-allocated */
3226 dprint("ehci %#p flb %#lux frno %#lux\n",
3227 ctlr->capio, opio->frbase, opio->frno);
3241 dprint("ehci %#p init\n", ctlr->capio);
3245 * Unless we activate frroll interrupt
3246 * some machines won't post other interrupts.
3248 opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
3256 /* route all ports to us */
3257 opio->config = Callmine;
3260 for (i = 0; i < hp->nports; i++)
3261 opio->portsc[i] = Pspower;
3269 ehcilinkage(Hci *hp)
3273 hp->interrupt = interrupt;
3274 hp->epopen = epopen;
3275 hp->epclose = epclose;
3276 hp->epread = epread;
3277 hp->epwrite = epwrite;
3278 hp->seprintep = seprintep;
3279 hp->portenable = portenable;
3280 hp->portreset = portreset;
3281 hp->portstatus = portstatus;
3282 // hp->shutdown = shutdown;
3283 // hp->debug = setdebug;