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 hs; /* is high speed? */
219 Isoio* next; /* in list of active Isoios */
220 ulong td0frno; /* first frame used in ctlr */
222 Itd* tdi; /* next td processed by interrupt */
226 Itd* tdu; /* next td for user I/O in tdps */
230 Itd** itdps; /* itdps[i]: ptr to Itd for i-th frame or nil */
231 Sitd** sitdps; /* sitdps[i]: ptr to Sitd for i-th frame or nil */
232 ulong** tdps; /* same thing, as seen by hw */
246 * We use the 64-bit version for Itd, Sitd, Td, and Qh.
247 * If the ehci is 64-bit capable it assumes we are using those
248 * structures even when the system is 32 bits.
252 * Iso transfer descriptor. hw: 92 bytes, 108 bytes total
257 ulong link; /* to next hw struct */
258 ulong csw[8]; /* sts/length/pg/off. updated by hw */
259 ulong buffer[7]; /* buffer pointers, addrs, maxsz */
260 ulong xbuffer[7]; /* high 32 bits of buffer for 64-bits */
262 ulong _pad0; /* pad to next cache line */
263 /* cache-line boundary here */
267 ulong ndata; /* number of bytes in data */
268 ulong mdata; /* max number of bytes in data */
273 * Split transaction iso transfer descriptor.
274 * hw: 36 bytes, 52 bytes total. aligned to 32.
278 ulong link; /* to next hw struct */
279 ulong epc; /* static endpoint state. addrs */
280 ulong mfs; /* static endpoint state. µ-frame sched. */
281 ulong csw; /* transfer state. updated by hw */
282 ulong buffer[2]; /* buf. ptr/offset. offset updated by hw */
283 /* buf ptr/TP/Tcnt. TP/Tcnt updated by hw */
284 ulong blink; /* back pointer */
285 /* cache-line boundary after xbuffer[0] */
286 ulong xbuffer[2]; /* high 32 bits of buffer for 64-bits */
290 ulong ndata; /* number of bytes in data */
291 ulong mdata; /* max number of bytes in data */
296 * Queue element transfer descriptor.
297 * hw: first 52 bytes, total 68+sbuff bytes. aligned to 32 bytes.
301 ulong nlink; /* to next Td */
302 ulong alink; /* alternate link to next Td */
303 ulong csw; /* cmd/sts. updated by hw */
304 ulong buffer[5]; /* buf ptrs. offset updated by hw */
305 /* cache-line boundary here */
306 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
309 Td* next; /* in qh or Isoio or free list */
310 ulong ndata; /* bytes available/used at data */
311 uchar* data; /* pointer to actual data */
312 uchar* buff; /* allocated data buffer or nil */
313 uchar sbuff[1]; /* first byte of embedded buffer */
317 * Queue head. Aligned to 32 bytes.
318 * hw: first 68 bytes, 92 total.
322 ulong link; /* to next Qh in round robin */
323 ulong eps0; /* static endpoint state. addrs */
324 ulong eps1; /* static endpoint state. µ-frame sched. */
327 ulong tclink; /* current Td (No Term bit here!) */
328 ulong nlink; /* to next Td */
329 ulong alink; /* alternate link to next Td */
330 ulong csw; /* cmd/sts. updated by hw */
331 /* cache-line boundary after buffer[0] */
332 ulong buffer[5]; /* buf ptrs. offset updated by hw */
333 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
336 Qh* next; /* in controller list/tree of Qhs */
337 int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
338 Qio* io; /* for this queue */
339 Td* tds; /* for this queue */
340 int sched; /* slot for for intr. Qhs */
341 Qh* inext; /* next in list of intr. qhs */
345 * We can avoid frame span traversal nodes if we don't span frames.
346 * Just schedule transfers that can fit on the current frame and
347 * wait a little bit otherwise.
351 * Software. Ehci descriptors provided by pool.
352 * There are soo few because we avoid using Fstn.
356 Ed* next; /* in free list */
366 static Edpool edpool;
367 static char Ebug[] = "not yet implemented";
368 static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
370 Ecapio* ehcidebugcapio;
374 ehcirun(Ctlr *ctlr, int on)
379 ddprint("ehci %#p %s\n", ctlr->capio, on ? "starting" : "halting");
386 for(i = 0; i < 100; i++)
387 if(on == 0 && (opio->sts & Shalted) != 0)
389 else if(on != 0 && (opio->sts & Shalted) == 0)
394 print("ehci %#p %s cmd timed out\n",
395 ctlr->capio, on ? "run" : "halt");
396 ddprint("ehci %#p cmd %#lux sts %#lux\n",
397 ctlr->capio, opio->cmd, opio->sts);
407 if(edpool.free == nil){
408 pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
411 for(i=Incr; --i>=0;){
412 pool[i].next = edpool.free;
413 edpool.free = &pool[i];
415 edpool.nalloc += Incr;
416 edpool.nfree += Incr;
417 dprint("ehci: edalloc: %d eds\n", edpool.nalloc);
420 edpool.free = ed->next;
425 memset(ed, 0, sizeof(Ed)); /* safety */
426 assert(((ulong)ed & 0xF) == 0);
437 ed->next = edpool.free;
445 * Allocate and do some initialization.
446 * Free after releasing buffers used.
471 td->link = td->blink = Lterm;
487 td->nlink = td->alink = Lterm;
501 tdlinktd(Td *td, Td *next)
508 td->nlink = PADDR(next);
513 qhlinkqh(Qh *qh, Qh *next)
516 qh->link = PADDR(next)|Lqh;
522 qhsetaddr(Qh *qh, ulong addr)
526 eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
527 qh->eps0 = eps0 | addr & Devmax | ((addr >> 7) & Epmax) << 8;
532 * return smallest power of 2 <= n
539 for(i = 0; (1 << (i + 1)) <= n; i++)
545 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
547 int i, j, d, upperb, q;
548 ulong best, worst, total;
550 d = flog2lower(pollival);
556 upperb = (1 << (d+1)) - 1;
557 for(i = (1 << d) - 1; i < upperb; i++){
559 for(j = i; j > 0; j = (j - 1) / 2)
568 if(worst + bw >= limit)
574 schedq(Ctlr *ctlr, Qh *qh, int pollival)
581 q = pickschedq(ctlr->tree, pollival, 0, ~0);
582 ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
583 qh->io, q, pollival, bw);
585 print("ehci: no room for ed\n");
588 ctlr->tree->bw[q] += bw;
589 tqh = ctlr->tree->root[q];
591 qhlinkqh(qh, tqh->next);
594 qh->inext = ctlr->intrqhs;
601 unschedq(Ctlr *ctlr, Qh *qh)
604 Qh *prev, *this, *next;
612 ctlr->tree->bw[q] -= bw;
614 prev = ctlr->tree->root[q];
616 while(this != nil && this != qh){
621 print("ehci: unschedq %d: not found\n", q);
624 qhlinkqh(prev, next);
626 for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
631 print("ehci: unschedq: qh %#p not found\n", qh);
637 return (qh->eps0 >> Qhmplshift) & Qhmplmask;
641 qhsetmaxpkt(Qh *qh, int maxpkt)
645 eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
646 qh->eps0 = eps0 | (maxpkt & Qhmplmask) << Qhmplshift;
651 * Initialize the round-robin circular list of ctl/bulk Qhs
652 * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
655 qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
669 qhsetmaxpkt(qh, ep->maxpkt);
670 if(ep->dev->speed == Lowspeed)
672 if(ep->dev->speed == Highspeed)
674 else if(ep->ttype == Tctl)
676 qh->eps0 |= Qhdtc | 8 << Qhrlcshift; /* 8 naks max */
678 qhsetaddr(qh, io->usbid);
679 qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
680 qh->eps1 |= ep->dev->port << Qhportshift;
681 qh->eps1 |= ep->dev->hub << Qhhubshift;
682 qh->eps1 |= 034 << Qhscmshift;
683 if(ep->ttype == Tintr)
684 qh->eps1 |= 1 << Qhismshift; /* intr. start µf. */
696 if(ctlr->qhs == nil){
697 ctlr->qhs = qhlinkqh(qh, qh);
698 qh->eps0 |= Qhhigh | Qhhrl;
700 ctlr->opio->link = PADDR(qh)|Lqh;
703 qhlinkqh(qh, ctlr->qhs->next);
704 qhlinkqh(ctlr->qhs, qh);
708 schedq(ctlr, qh, ep->pollival);
711 print("ehci: qhalloc called for ttype != ctl/bulk\n");
723 return (ctlr->opio->cmd & Ciasync) == 0;
727 * called when a qh is removed, to be sure the hw is not
728 * keeping pointers into it.
731 qhcoherency(Ctlr *ctlr)
735 qlock(&ctlr->portlck);
736 ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
738 for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++)
740 tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
743 dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
745 print("ehci: async advance doorbell did not ring\n");
746 ctlr->opio->cmd &= ~Ciasync; /* try to clean */
747 qunlock(&ctlr->portlck);
751 qhfree(Ctlr *ctlr, Qh *qh)
760 for(q = ctlr->qhs; q != nil; q = q->next)
764 panic("qhfree: nil q");
774 for(td = qh->tds; td != nil; td = ltd){
783 qhlinktd(Qh *qh, Td *td)
791 qh->csw = (csw & ~Tdactive) | Tdhalt;
793 csw &= Tddata1 | Tdping; /* save */
798 qh->nlink = PADDR(td);
799 for(i = 0; i < nelem(qh->buffer); i++)
802 qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
808 seprintlink(char *s, char *se, char *name, ulong l, int typed)
810 s = seprint(s, se, "%s %ulx", name, l);
812 return seprint(s, se, "T");
817 return seprint(s, se, "I");
819 return seprint(s, se, "Q");
821 return seprint(s, se, "S");
823 return seprint(s, se, "F");
828 seprintitd(char *s, char *se, Itd *td)
836 return seprint(s, se, "<nil itd>\n");
840 s = seprint(s, se, "itd %#p", td);
841 rw = (b1 & Itdin) ? "in" : "out";
842 s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld",
843 rw, (b0>>8)&Epmax, (b0&Devmax),
844 td->buffer[1] & 0x7ff, b1 & 3);
845 s = seprintlink(s, se, " link", td->link, 1);
846 s = seprint(s, se, "\n");
847 for(i = 0; i < nelem(td->csw); i++){
848 memset(flags, '-', 5);
849 if((td->csw[i] & Itdactive) != 0)
851 if((td->csw[i] & Itdioc) != 0)
853 if((td->csw[i] & Itddberr) != 0)
855 if((td->csw[i] & Itdbabble) != 0)
857 if((td->csw[i] & Itdtrerr) != 0)
860 s = seprint(s, se, "\ttd%d %s", i, flags);
861 s = seprint(s, se, " len %uld", (td->csw[i] >> 16) & 0x7ff);
862 s = seprint(s, se, " pg %uld", (td->csw[i] >> 12) & 0x7);
863 s = seprint(s, se, " off %uld\n", td->csw[i] & 0xfff);
865 s = seprint(s, se, "\tbuffs:");
866 for(i = 0; i < nelem(td->buffer); i++)
867 s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
868 return seprint(s, se, "\n");
872 seprintsitd(char *s, char *se, Sitd *td)
876 static char pc[4] = { 'a', 'b', 'm', 'e' };
879 return seprint(s, se, "<nil sitd>\n");
880 s = seprint(s, se, "sitd %#p", td);
881 rw = (td->epc & Stdin) ? 'r' : 'w';
882 s = seprint(s, se, " %c ep %uld dev %uld",
883 rw, (td->epc>>8)&0xf, td->epc&0x7f);
884 s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
885 s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
886 s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
887 memset(flags, '-', 7);
888 if((td->csw & Stdactive) != 0)
890 if((td->csw & Stdioc) != 0)
892 if((td->csw & Stderr) != 0)
894 if((td->csw & Stddberr) != 0)
896 if((td->csw & Stdbabble) != 0)
898 if((td->csw & Stdtrerr) != 0)
900 if((td->csw & Stdmmf) != 0)
903 ss = (td->csw & Stddcs) ? 'c' : 's';
904 pg = (td->csw & Stdpg) ? '1' : '0';
905 s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
906 s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
907 td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
908 s = seprint(s, se, "\ttpos %c tcnt %uld",
909 pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
910 s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
911 td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
912 s = seprintlink(s, se, " link", td->link, 1);
913 s = seprintlink(s, se, " blink", td->blink, 0);
914 return seprint(s, se, "\n");
920 return (td->csw >> Tdlenshift) & Tdlenmask;
928 return td->ndata - maxtdlen(td);
932 seprinttd(char *s, char *se, Td *td, char *tag)
937 static char *tok[4] = { "out", "in", "setup", "BUG" };
940 return seprint(s, se, "%s <nil td>\n", tag);
941 s = seprint(s, se, "%s %#p", tag, td);
942 s = seprintlink(s, se, " nlink", td->nlink, 0);
943 s = seprintlink(s, se, " alink", td->alink, 0);
944 s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
945 if((td->csw & Tdping) != 0)
946 s = seprint(s, se, " png");
947 memset(flags, '-', 8);
948 if((td->csw & Tdactive) != 0)
950 if((td->csw & Tdioc) != 0)
952 if((td->csw & Tdhalt) != 0)
954 if((td->csw & Tddberr) != 0)
956 if((td->csw & Tdbabble) != 0)
958 if((td->csw & Tdtrerr) != 0)
960 if((td->csw & Tdmmf) != 0)
962 if((td->csw & (Tderr2|Tderr1)) == 0)
965 t = (td->csw & Tddata1) ? '1' : '0';
966 ss = (td->csw & Tddcs) ? 'c' : 's';
967 s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
968 s = seprint(s, se, " max %uld", maxtdlen(td));
969 s = seprint(s, se, " pg %uld off %#lux\n",
970 (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
971 s = seprint(s, se, "\tbuffs:");
972 for(i = 0; i < nelem(td->buffer); i++)
973 s = seprint(s, se, " %#lux", td->buffer[i]>>12);
975 s = seprintdata(s, se, td->data, td->ndata);
976 return seprint(s, se, "\n");
980 dumptd(Td *td, char *pref)
987 se = buf+sizeof(buf);
988 for(; td != nil; td = td->next){
989 seprinttd(buf, se, td, pref);
992 print("...more tds...\n");
1004 static char *speed[] = {"full", "low", "high", "BUG"};
1007 print("<nil qh>\n");
1014 se = buf+sizeof(buf);
1015 s = seprint(buf, se, "%s %#p", tag, qh);
1016 s = seprint(s, se, " ep %uld dev %uld",
1017 (qh->eps0>>8)&0xf, qh->eps0&0x7f);
1018 s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
1019 s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
1020 s = seprintlink(s, se, " link", qh->link, 1);
1021 seprint(s, se, " clink %#lux", qh->tclink);
1023 s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
1024 s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
1025 s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
1026 if((qh->eps0 & Qhnhctl) != 0)
1027 s = seprint(s, se, "c");
1028 if((qh->eps0 & Qhhrl) != 0)
1029 s = seprint(s, se, "h");
1030 if((qh->eps0 & Qhdtc) != 0)
1031 s = seprint(s, se, "d");
1032 if((qh->eps0 & Qhint) != 0)
1033 s = seprint(s, se, "i");
1034 s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
1035 s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
1036 seprint(s, se, " scm %#lux ism %#lux\n",
1037 (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
1039 memset(&td, 0, sizeof(td));
1040 memmove(&td, &qh->nlink, 32); /* overlay area */
1041 seprinttd(buf, se, &td, "\tovl");
1046 isodump(Isoio* iso, int all)
1048 Itd *td, *tdi, *tdu;
1049 Sitd *std, *stdi, *stdu;
1054 print("<nil iso>\n");
1057 print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
1058 iso, iso->tok == Tdtokin ? "in" : "out",
1059 iso->hs ? "high" : "full",
1060 iso->state, iso->nframes, iso->maxsize);
1061 print(" td0 %uld tdi %#p tdu %#p data %#p\n",
1062 iso->td0frno, iso->tdi, iso->tdu, iso->data);
1064 print("\terr %s\n", iso->err);
1066 print("\terr='%s'\n", iso->err);
1070 seprintitd(buf, buf+sizeof(buf), tdi);
1071 print("\ttdi %s\n", buf);
1073 seprintitd(buf, buf+sizeof(buf), tdu);
1074 print("\ttdu %s\n", buf);
1077 seprintsitd(buf, buf+sizeof(buf), stdi);
1078 print("\tstdi %s\n", buf);
1080 seprintsitd(buf, buf+sizeof(buf), stdu);
1081 print("\tstdu %s\n", buf);
1084 for(i = 0; i < Nisoframes; i++)
1085 if(iso->tdps[i] != nil)
1088 seprintitd(buf, buf+sizeof(buf), td);
1093 print("[%d]\t%s", i, buf);
1095 std = iso->sitdps[i];
1096 seprintsitd(buf, buf+sizeof(buf), std);
1097 if(std == iso->stdi)
1099 if(std == iso->stdu)
1101 print("[%d]\t%s", i, buf);
1119 print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
1120 ctlr->capio, ctlr->frames, ctlr->nframes,
1121 ctlr->nintr, ctlr->ntdintr);
1122 print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
1123 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1124 opio->cmd, opio->sts, opio->intr, opio->frno);
1125 print(" base %#lux link %#lux fr0 %#lux\n",
1126 opio->frbase, opio->link, ctlr->frames[0]);
1127 se = buf+sizeof(buf);
1128 s = seprint(buf, se, "\t");
1129 for(i = 0; i < hp->nports; i++){
1130 s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
1131 if(hp->nports > 4 && i == hp->nports/2 - 1)
1132 s = seprint(s, se, "\n\t");
1140 }while(qh != ctlr->qhs && i++ < 100);
1142 print("...too many Qhs...\n");
1143 if(ctlr->intrqhs != nil)
1144 print("intr qhs:\n");
1145 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1147 if(ctlr->iso != nil)
1149 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1150 isodump(ctlr->iso, 0);
1151 print("%d eds in tree\n", ctlr->ntree);
1154 print("%d eds allocated = %d in use + %d free\n",
1155 edpool.nalloc, edpool.ninuse, edpool.nfree);
1165 return "data buffer error";
1167 return "babble detected";
1169 return "transaction error";
1171 return "missed µframe";
1173 return Estalled; /* [uo]hci report this error */
1183 return "data buffer error";
1185 return "babble detected";
1187 return "transaction error";
1195 return "translation translator error";
1196 /* other errors have same numbers than Td errors */
1206 if(iso->state == Qclose)
1208 if(iso->state == Qrun && iso->tok == Tdtokin){
1209 if(iso->hs != 0 && iso->tdi != iso->tdu)
1211 if(iso->hs == 0 && iso->stdi != iso->stdu)
1218 isocanwrite(void *a)
1223 if(iso->state == Qclose)
1225 if(iso->state == Qrun && iso->tok == Tdtokout){
1226 if(iso->hs != 0 && iso->tdu->next != iso->tdi)
1228 if(iso->hs == 0 && iso->stdu->next != iso->stdi)
1235 itdinit(Isoio *iso, Itd *td)
1238 ulong pa, tsize, size;
1241 * BUG: This does not put an integral number of samples
1242 * on each µframe unless samples per packet % 8 == 0
1243 * Also, all samples are packed early on each frame.
1246 size = td->ndata = td->mdata;
1247 pa = PADDR(td->data);
1248 for(t = 0; size > 0 && t < 8; t++){
1250 if(tsize > iso->maxsize)
1251 tsize = iso->maxsize;
1253 assert(p < nelem(td->buffer));
1254 td->csw[t] = tsize << Itdlenshift | p << Itdpgshift |
1255 (pa & 0xFFF) << Itdoffshift | Itdactive | Itdioc;
1257 if(((pa+tsize) & ~0xFFF) != (pa & ~0xFFF))
1264 sitdinit(Isoio *iso, Sitd *td)
1266 td->ndata = td->mdata & Stdlenmask;
1267 td->buffer[0] = PADDR(td->data);
1268 td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
1269 if(iso->tok == Tdtokin || td->ndata <= 188)
1270 td->buffer[1] |= Stdtpall;
1272 td->buffer[1] |= Stdtpbegin;
1273 if(iso->tok == Tdtokin)
1276 td->buffer[1] |= ((td->ndata + 187) / 188) & Stdtcntmask;
1278 td->csw = td->ndata << Stdlenshift | Stdactive | Stdioc;
1287 for(i = 0; i < nelem(td->csw); i++)
1288 if((td->csw[i] & Itdactive) != 0)
1294 isohsinterrupt(Ctlr *ctlr, Isoio *iso)
1296 int err, i, nframes, t;
1301 if(itdactive(tdi)) /* not all tds are done */
1304 ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
1305 if(iso->state != Qrun && iso->state != Qdone)
1306 panic("isofsintr: iso state");
1307 if(ehcidebug > 1 || iso->debug > 1)
1310 nframes = iso->nframes / 2; /* limit how many we look */
1311 if(nframes > Nisoframes)
1312 nframes = Nisoframes;
1314 if(iso->tok == Tdtokin)
1316 /* else, it has the number of bytes transferred */
1318 for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
1319 if(iso->tok == Tdtokin)
1320 tdi->ndata += (tdi->csw[i] >> Itdlenshift) & Itdlenmask;
1323 for(t = 0; t < nelem(tdi->csw); t++){
1324 tdi->csw[t] &= ~Itdioc;
1326 err |= tdi->csw[t] & Itderrors;
1330 else if(iso->nerrs++ > iso->nframes/2){
1331 if(iso->err == nil){
1332 iso->err = ierrmsg(err);
1333 diprint("isohsintr: tdi %#p error %#ux %s\n",
1334 tdi, err, iso->err);
1335 diprint("ctlr load %uld\n", ctlr->load);
1340 if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
1341 memset(iso->tdu->data, 0, iso->tdu->mdata);
1342 itdinit(iso, iso->tdu);
1343 iso->tdu = iso->tdu->next;
1349 ddiprint("isohsintr: %d frames processed\n", nframes);
1351 tdi->csw[0] |= Itdioc;
1356 if(isocanwrite(iso) || isocanread(iso)){
1357 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1358 iso->tdi, iso->tdu);
1365 isofsinterrupt(Ctlr *ctlr, Isoio *iso)
1367 int err, i, nframes;
1371 assert(stdi != nil);
1372 if((stdi->csw & Stdactive) != 0) /* nothing new done */
1375 ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
1376 if(iso->state != Qrun && iso->state != Qdone)
1377 panic("isofsintr: iso state");
1378 if(ehcidebug > 1 || iso->debug > 1)
1381 nframes = iso->nframes / 2; /* limit how many we look */
1382 if(nframes > Nisoframes)
1383 nframes = Nisoframes;
1385 for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
1386 stdi->csw &= ~Stdioc;
1387 /* write back csw and see if it produces errors */
1389 err = stdi->csw & Stderrors;
1392 if(iso->tok == Tdtokin)
1393 stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
1394 /* else len is assumed correct */
1395 }else if(iso->nerrs++ > iso->nframes/2){
1396 if(iso->err == nil){
1397 iso->err = serrmsg(err);
1398 diprint("isofsintr: tdi %#p error %#ux %s\n",
1399 stdi, err, iso->err);
1400 diprint("ctlr load %uld\n", ctlr->load);
1406 if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
1407 memset(iso->stdu->data, 0, iso->stdu->mdata);
1409 sitdinit(iso, iso->stdu);
1410 iso->stdu = iso->stdu->next;
1416 ddiprint("isofsintr: %d frames processed\n", nframes);
1418 stdi->csw |= Stdioc;
1423 if(isocanwrite(iso) || isocanread(iso)){
1424 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1425 iso->stdi, iso->stdu);
1432 qhinterrupt(Ctlr *ctlr, Qh *qh)
1437 if(qh->state != Qrun)
1438 panic("qhinterrupt: qh state");
1441 panic("qhinterrupt: no tds");
1442 if((td->csw & Tdactive) == 0)
1443 ddqprint("qhinterrupt port %#p qh %#p\n", ctlr->capio, qh);
1444 for(; td != nil; td = td->next){
1445 if(td->csw & Tdactive)
1447 err = td->csw & Tderrors;
1449 if(qh->io->err == nil){
1450 qh->io->err = errmsg(err);
1451 dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
1452 td, td->csw, err, qh->io->err);
1456 td->ndata = tdlen(td);
1458 if(td->ndata < maxtdlen(td)){ /* EOT */
1464 * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
1466 for(; td != nil; td = td->next)
1489 * Will we know in USB 3.0 who the interrupt was for?.
1490 * Do they still teach indexing in CS?
1491 * This is Intel's doing.
1495 sts = opio->sts & Sintrs;
1496 if(sts == 0){ /* not ours; shared intr. */
1502 if((sts & Sherr) != 0)
1503 print("ehci: port %#p fatal host system error\n", ctlr->capio);
1504 if((sts & Shalted) != 0)
1505 print("ehci: port %#p: halted\n", ctlr->capio);
1506 if((sts & Sasync) != 0){
1507 dprint("ehci: doorbell\n");
1511 * We enter always this if, even if it seems the
1512 * interrupt does not report anything done/failed.
1513 * Some controllers don't post interrupts right.
1516 if((sts & (Serrintr|Sintr)) != 0){
1519 print("ehci port %#p frames %#p nintr %d ntdintr %d",
1520 ctlr->capio, ctlr->frames,
1521 ctlr->nintr, ctlr->ntdintr);
1522 print(" nqhintr %d nisointr %d\n",
1523 ctlr->nqhintr, ctlr->nisointr);
1524 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1525 opio->cmd, opio->sts, opio->intr, opio->frno);
1528 /* process the Iso transfers */
1529 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1530 if(iso->state == Qrun || iso->state == Qdone)
1532 some += isohsinterrupt(ctlr, iso);
1534 some += isofsinterrupt(ctlr, iso);
1536 /* process the qhs in the periodic tree */
1537 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1538 if(qh->state == Qrun)
1539 some += qhinterrupt(ctlr, qh);
1541 /* process the async Qh circular list */
1546 panic("ehciintr: nil qh");
1547 if(qh->state == Qrun)
1548 some += qhinterrupt(ctlr, qh);
1550 }while(qh != ctlr->qhs && i++ < 100);
1552 print("echi: interrupt: qh loop?\n");
1555 // panic("ehciintr: no work");
1561 interrupt(Ureg*, void* a)
1567 portenable(Hci *hp, int port, int on)
1575 s = opio->portsc[port-1];
1576 qlock(&ctlr->portlck);
1578 qunlock(&ctlr->portlck);
1581 dprint("ehci %#p port %d enable=%d; sts %#x\n",
1582 ctlr->capio, port, on, s);
1584 if(s & (Psstatuschg | Pschange))
1585 opio->portsc[port-1] = s;
1587 opio->portsc[port-1] |= Psenable;
1589 opio->portsc[port-1] &= ~Psenable;
1593 tsleep(&up->sleep, return0, 0, Enabledelay);
1594 dprint("ehci %#p port %d enable=%d: sts %#lux\n",
1595 ctlr->capio, port, on, opio->portsc[port-1]);
1596 qunlock(&ctlr->portlck);
1602 * If we detect during status that the port is low-speed or
1603 * during reset that it's full-speed, the device is not for
1604 * ourselves. The companion controller will take care.
1605 * Low-speed devices will not be seen by usbd. Full-speed
1606 * ones are seen because it's only after reset that we know what
1607 * they are (usbd may notice a device not enabled in this case).
1610 portlend(Ctlr *ctlr, int port, char *ss)
1617 dprint("ehci %#p port %d: %s speed device: no longer owned\n",
1618 ctlr->capio, port, ss);
1619 s = opio->portsc[port-1] & ~(Pschange|Psstatuschg);
1620 opio->portsc[port-1] = s | Psowner;
1625 portreset(Hci *hp, int port, int on)
1637 qlock(&ctlr->portlck);
1640 qunlock(&ctlr->portlck);
1643 s = opio->portsc[port-1];
1644 dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, s);
1646 s &= ~(Psenable|Psreset);
1647 opio->portsc[port-1] = s | Psreset; /* initiate reset */
1650 for(i = 0; i < 50; i++){ /* was 10 */
1652 if((opio->portsc[port-1] & Psreset) == 0)
1655 if (opio->portsc[port-1] & Psreset)
1656 iprint("ehci %#p: port %d didn't reset after %d ms; sts %#lux\n",
1657 ctlr->capio, port, i * 10, opio->portsc[port-1]);
1658 opio->portsc[port-1] &= ~Psreset; /* force appearance of reset done */
1662 if((opio->portsc[port-1] & Psenable) == 0)
1663 portlend(ctlr, port, "full");
1666 dprint("ehci %#p after port %d reset; sts %#lux\n",
1667 ctlr->capio, port, opio->portsc[port-1]);
1668 qunlock(&ctlr->portlck);
1674 portstatus(Hci *hp, int port)
1682 qlock(&ctlr->portlck);
1685 qunlock(&ctlr->portlck);
1689 s = opio->portsc[port-1];
1690 if(s & (Psstatuschg | Pschange)){
1691 opio->portsc[port-1] = s;
1693 ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
1696 * If the port is a low speed port we yield ownership now
1697 * to the [uo]hci companion controller and pretend it's not here.
1699 if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
1700 portlend(ctlr, port, "low");
1701 s &= ~Pspresent; /* not for us this time */
1704 qunlock(&ctlr->portlck);
1708 * We must return status bits as a
1709 * get port status hub request would do.
1713 r |= HPpresent|HPhigh;
1728 seprintio(char *s, char *e, Qio *io, char *pref)
1730 s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
1731 s = seprint(s,e," iot %ld", io->iotime);
1732 s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
1737 seprintep(char *s, char *e, Ep *ep)
1753 s = seprintio(s, e, cio, "c");
1754 s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
1759 if(ep->mode != OWRITE)
1760 s = seprintio(s, e, &io[OREAD], "r");
1761 if(ep->mode != OREAD)
1762 s = seprintio(s, e, &io[OWRITE], "w");
1773 * halt condition was cleared on the endpoint. update our toggles.
1786 if(ep->mode != OREAD){
1788 io[OWRITE].toggle = Tddata0;
1789 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1790 qunlock(&io[OWRITE]);
1792 if(ep->mode != OWRITE){
1794 io[OREAD].toggle = Tddata0;
1795 deprint("ep clrhalt for io %#p\n", io+OREAD);
1796 qunlock(&io[OREAD]);
1803 xdump(char* pref, void *qh)
1809 print("%s %#p:", pref, u);
1810 for(i = 0; i < 16; i++)
1812 print("\n %#8.8ulx", u[i]);
1814 print(" %#8.8ulx", u[i]);
1819 episohscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1825 for(tot = 0; iso->tdi != iso->tdu && tot < count; tot += nr){
1830 if(tot + nr > count)
1833 print("ehci: ep%d.%d: too many polls\n",
1834 ep->dev->nb, ep->nb);
1836 iunlock(ctlr); /* We could page fault here */
1837 memmove(b+tot, tdu->data, nr);
1840 memmove(tdu->data, tdu->data+nr, tdu->ndata - nr);
1844 if(tdu->ndata == 0){
1846 iso->tdu = tdu->next;
1853 episofscpy(Ctlr *ctlr, Ep *ep, Isoio* iso, uchar *b, long count)
1859 for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
1861 if(stdu->csw & Stdactive){
1862 diprint("ehci: episoread: %#p tdu active\n", iso);
1866 if(tot + nr > count)
1869 print("ehci: ep%d.%d: too many polls\n",
1870 ep->dev->nb, ep->nb);
1872 iunlock(ctlr); /* We could page fault here */
1873 memmove(b+tot, stdu->data, nr);
1875 if(nr < stdu->ndata)
1876 memmove(stdu->data, stdu->data+nr,
1881 if(stdu->ndata == 0){
1882 sitdinit(iso, stdu);
1883 iso->stdu = stdu->next;
1890 episoread(Ep *ep, Isoio *iso, void *a, long count)
1896 iso->debug = ep->debug;
1897 diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1909 if(iso->state == Qclose){
1911 error(iso->err ? iso->err : Eio);
1915 while(isocanread(iso) == 0){
1917 diprint("ehci: episoread: %#p sleep\n", iso);
1920 iso->err = "I/O timed out";
1924 tsleep(iso, isocanread, iso, ep->tmout);
1928 if(iso->state == Qclose){
1930 error(iso->err ? iso->err : Eio);
1934 assert(iso->tdu != iso->tdi);
1937 tot = episohscpy(ctlr, ep, iso, b, count);
1939 tot = episofscpy(ctlr, ep, iso, b, count);
1943 diprint("uhci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
1950 * iso->tdu is the next place to put data. When it gets full
1951 * it is activated and tdu advanced.
1954 putsamples(Isoio *iso, uchar *b, long count)
1958 for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
1961 if(n > iso->tdu->mdata - iso->nleft)
1962 n = iso->tdu->mdata - iso->nleft;
1963 memmove(iso->tdu->data + iso->nleft, b + tot, n);
1966 if(iso->nleft == iso->tdu->mdata){
1967 itdinit(iso, iso->tdu);
1969 iso->tdu = iso->tdu->next;
1972 if(n > iso->stdu->mdata - iso->nleft)
1973 n = iso->stdu->mdata - iso->nleft;
1974 memmove(iso->stdu->data + iso->nleft, b + tot, n);
1977 if(iso->nleft == iso->stdu->mdata){
1978 sitdinit(iso, iso->stdu);
1980 iso->stdu = iso->stdu->next;
1988 * Queue data for writing and return error status from
1989 * last writes done, to maintain buffered data.
1992 episowrite(Ep *ep, Isoio *iso, void *a, long count)
1999 iso->debug = ep->debug;
2000 diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
2009 if(iso->state == Qclose){
2011 error(iso->err ? iso->err : Eio);
2016 for(tot = 0; tot < count; tot += nw){
2017 while(isocanwrite(iso) == 0){
2019 diprint("ehci: episowrite: %#p sleep\n", iso);
2022 iso->err = "I/O timed out";
2026 tsleep(iso, isocanwrite, iso, ep->tmout);
2032 if(iso->state == Qclose || err != nil){
2034 error(err ? err : Eio);
2036 if(iso->state != Qrun)
2037 panic("episowrite: iso not running");
2038 iunlock(ctlr); /* We could page fault here */
2039 nw = putsamples(iso, b+tot, count-tot);
2042 if(iso->state != Qclose)
2045 err = iso->err; /* in case it failed early */
2051 diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
2056 nexttoggle(int toggle, int count, int maxpkt)
2060 np = count / maxpkt;
2065 if(toggle == Tddata1)
2072 epgettd(Qio *io, int flags, void *a, int count, int maxpkt)
2078 if(count > Tdmaxpkt)
2079 panic("ehci: epgettd: too many bytes");
2081 td->csw = flags | io->toggle | io->tok | count << Tdlenshift |
2086 * use the space wasted by alignment as an
2087 * embedded buffer if count bytes fit in there.
2089 assert(Align > sizeof(Td));
2090 if(count <= Align - sizeof(Td)){
2091 td->data = td->sbuff;
2094 td->data = td->buff = smalloc(Tdmaxpkt);
2096 pa = PADDR(td->data);
2097 for(i = 0; i < nelem(td->buffer); i++){
2100 td->buffer[i] &= ~0xFFF;
2104 if(a != nil && count > 0)
2105 memmove(td->data, a, count);
2107 io->toggle = nexttoggle(io->toggle, count, maxpkt);
2113 * Try to get them idle
2122 if(qh->sched >= 0 && (qh->eps0 & Qhspeedmask) != Qhhigh)
2123 qh->eps0 |= Qhint; /* inactivate on next pass */
2125 for(td = qh->tds; td != nil; td = td->next){
2126 if(td->csw & Tdactive)
2134 * Some controllers do not post the usb/error interrupt after
2135 * the work has been done. It seems that we must poll for them.
2138 workpending(void *a)
2143 return ctlr->nreqs > 0;
2158 if(ctlr->nreqs == 0){
2159 if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
2160 sleep(poll, workpending, ctlr);
2161 if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
2163 for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
2164 if(ehciintr(hp) == 0)
2167 tsleep(&up->sleep, return0, 0, 1);
2169 }while(ctlr->nreqs > 0);
2182 if(poll->must != 0 && poll->does == 0){
2184 if(poll->must != 0 && poll->does == 0){
2186 print("ehci %#p: polling\n", ctlr->capio);
2187 kproc("ehcipoll", ehcipoll, hp);
2199 return qh->state != Qrun;
2203 epiowait(Hci *hp, Qio *io, int tmout, ulong load)
2211 ddqprint("ehci io %#p sleep on qh %#p state %s\n",
2212 io, qh, qhsname[qh->state]);
2215 dqprint("ehci io %#p qh %#p timed out\n", io, qh);
2219 sleep(io, epiodone, qh);
2221 tsleep(io, epiodone, qh, tmout);
2226 /* Are we missing interrupts? */
2227 if(qh->state == Qrun){
2231 if(qh->state == Qdone){
2232 dqprint("ehci %#p: polling required\n", ctlr->capio);
2233 ctlr->poll.must = 1;
2238 if(qh->state == Qrun){
2239 dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
2241 }else if(qh->state != Qdone && qh->state != Qclose)
2242 panic("ehci: epio: queue state %d", qh->state);
2245 io->err = "request timed out";
2248 tsleep(&up->sleep, return0, 0, Abortdelay);
2253 if(qh->state != Qclose)
2264 * To make it work for control transfers, the caller may
2265 * lock the Qio for the entire control transfer.
2268 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
2270 int saved, ntds, tmout;
2278 Td *td, *ltd, *td0, *ntd;
2282 io->debug = ep->debug;
2284 ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
2285 io->tok == Tdtokin ? "in" : "out",
2286 ep->dev->nb, ep->nb, io, count, ctlr->load);
2287 if((ehcidebug > 1 || ep->debug > 1) && io->tok != Tdtokin){
2288 seprintdata(buf, buf+sizeof(buf), a, count);
2289 print("echi epio: user data: %s\n", buf);
2300 if(qh->state == Qclose){ /* Tds released by cancelio */
2302 error(io->err ? io->err : Eio);
2304 if(qh->state != Qidle)
2305 panic("epio: qh not idle");
2306 qh->state = Qinstall;
2313 n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
2316 if(c != nil && io->tok != Tdtokin)
2317 td = epgettd(io, Tdactive, c+tot, n, ep->maxpkt);
2319 td = epgettd(io, Tdactive, nil, n, ep->maxpkt);
2327 }while(tot < count);
2328 if(td0 == nil || ltd == nil)
2329 panic("epio: no td");
2331 ltd->csw |= Tdioc; /* the last one interrupts */
2334 ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
2335 if(ehcidebug > 1 || ep->debug > 1)
2336 dumptd(td0, "epio: put: ");
2339 if(qh->state != Qclose){
2340 io->iotime = TK2MS(MACHP(0)->ticks);
2350 wakeup(&ctlr->poll);
2352 epiowait(ep->hp, io, tmout, load);
2353 if(ehcidebug > 1 || ep->debug > 1){
2354 dumptd(td0, "epio: got: ");
2362 for(td = td0; td != nil; td = ntd){
2365 * Use td tok, not io tok, because of setup packets.
2366 * Also, if the Td was stalled or active (previous Td
2367 * was a short packet), we must save the toggle as it is.
2369 if(td->csw & (Tdhalt|Tdactive)){
2371 io->toggle = td->csw & Tddata1;
2376 if(c != nil && (td->csw & Tdtok) == Tdtokin && td->ndata > 0){
2377 memmove(c, td->data, td->ndata);
2389 ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
2390 io, ntds, tot, err);
2392 return 0; /* that's our convention */
2401 epread(Ep *ep, void *a, long count)
2409 ddeprint("ehci: epread\n");
2411 panic("epread: not open");
2423 ddeprint("epread ctl ndata %d\n", cio->ndata);
2425 error("request expected");
2426 else if(cio->ndata == 0){
2430 if(count > cio->ndata)
2433 memmove(a, cio->data, count);
2434 /* BUG for big transfers */
2437 cio->ndata = 0; /* signal EOF next time */
2441 if(ehcidebug>1 || ep->debug){
2442 seprintdata(buf, buf+sizeof(buf), a, count);
2443 print("epread: %s\n", buf);
2450 return epio(ep, &io[OREAD], a, count, 1);
2453 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
2454 if(delta < ep->pollival / 2)
2455 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
2458 return epio(ep, &io[OREAD], a, count, 1);
2461 return episoread(ep, iso, a, count);
2467 * Control transfers are one setup write (data0)
2468 * plus zero or more reads/writes (data1, data0, ...)
2469 * plus a final write/read with data1 to ack.
2470 * For both host to device and device to host we perform
2471 * the entire transfer when the user writes the request,
2472 * and keep any data read from the device for a later read.
2473 * We call epio three times instead of placing all Tds at
2474 * the same time because doing so leads to crc/tmout errors
2476 * Upon errors on the data phase we must still run the status
2477 * phase or the device may cease responding in the future.
2480 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
2485 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
2486 cio, ep->dev->nb, ep->nb, count);
2487 if(count < Rsetuplen)
2488 error("short usb comand");
2501 /* set the address if unset and out of configuration state */
2502 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
2503 if(cio->usbid == 0){
2504 cio->usbid = (ep->nb&Epmax) << 7 | ep->dev->nb&Devmax;
2506 qhsetaddr(cio->qh, cio->usbid);
2508 /* adjust maxpkt if the user has learned a different one */
2509 if(qhmaxpkt(cio->qh) != ep->maxpkt)
2510 qhsetmaxpkt(cio->qh, ep->maxpkt);
2512 cio->tok = Tdtoksetup;
2513 cio->toggle = Tddata0;
2515 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
2520 cio->toggle = Tddata1;
2521 if(c[Rtype] & Rd2h){
2523 len = GET2(c+Rcount);
2525 error("bad length in d2h request");
2527 error("d2h data too large to fit in ehci");
2528 a = cio->data = smalloc(len+1);
2530 cio->tok = Tdtokout;
2538 len = epio(ep, cio, a, len, 0);
2541 if(c[Rtype] & Rd2h){
2544 cio->tok = Tdtokout;
2549 count = Rsetuplen + len;
2552 cio->toggle = Tddata1;
2554 epio(ep, cio, nil, 0, 0);
2557 ddeprint("epctlio cio %#p return %ld\n", cio, count);
2562 epwrite(Ep *ep, void *a, long count)
2571 ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
2573 panic("ehci: epwrite: not open");
2577 return epctlio(ep, cio, a, count);
2582 return epio(ep, &io[OWRITE], a, count, 1);
2585 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
2586 if(delta < ep->pollival)
2587 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
2590 return epio(ep, &io[OWRITE], a, count, 1);
2593 return episowrite(ep, iso, a, count);
2599 isofsinit(Ep *ep, Isoio *iso)
2608 frno = iso->td0frno;
2609 for(i = 0; i < iso->nframes; i++){
2611 td->data = iso->data + i * ep->maxpkt;
2612 td->epc = ep->dev->port << Stdportshift;
2613 td->epc |= ep->dev->hub << Stdhubshift;
2614 td->epc |= ep->nb << Stdepshift;
2615 td->epc |= ep->dev->nb << Stddevshift;
2616 td->mfs = 034 << Stdscmshift | 1 << Stdssmshift;
2617 if(ep->mode == OREAD){
2619 td->mdata = ep->maxpkt;
2621 td->mdata = (ep->hz+left) * ep->pollival / 1000;
2622 td->mdata *= ep->samplesz;
2623 left = (ep->hz+left) * ep->pollival % 1000;
2624 if(td->mdata > ep->maxpkt){
2625 print("ehci: ep%d.%d: size > maxpkt\n",
2626 ep->dev->nb, ep->nb);
2627 print("size = %ld max = %ld\n",
2628 td->mdata,ep->maxpkt);
2629 td->mdata = ep->maxpkt;
2634 iso->sitdps[frno] = td;
2640 frno = TRUNC(frno+ep->pollival, Nisoframes);
2642 ltd->next = iso->sitdps[iso->td0frno];
2647 isohsinit(Ep *ep, Isoio *iso)
2656 if(ep->pollival > 8)
2657 ival = ep->pollival/8;
2660 frno = iso->td0frno;
2661 for(i = 0; i < iso->nframes; i++){
2663 td->data = iso->data + i * 8 * iso->maxsize;
2664 pa = PADDR(td->data) & ~0xFFF;
2665 for(p = 0; p < 8; p++)
2666 td->buffer[i] = pa + p * 0x1000;
2667 td->buffer[0] = PADDR(iso->data) & ~0xFFF |
2668 ep->nb << Itdepshift | ep->dev->nb << Itddevshift;
2669 if(ep->mode == OREAD)
2670 td->buffer[1] |= Itdin;
2672 td->buffer[1] |= Itdout;
2673 td->buffer[1] |= ep->maxpkt << Itdmaxpktshift;
2674 td->buffer[2] |= ep->ntds << Itdntdsshift;
2676 if(ep->mode == OREAD)
2677 td->mdata = 8 * iso->maxsize;
2679 td->mdata = (ep->hz + left) * ep->pollival / 1000;
2680 td->mdata *= ep->samplesz;
2681 left = (ep->hz + left) * ep->pollival % 1000;
2684 iso->itdps[frno] = td;
2690 frno = TRUNC(frno + ival, Nisoframes);
2695 isoopen(Ctlr *ctlr, Ep *ep)
2697 int ival; /* pollival in ms */
2698 int tpf; /* tds per frame */
2709 iso->tok = Tdtokout;
2712 error("iso i/o is half-duplex");
2714 iso->usbid = ep->nb << 7 | ep->dev->nb & Devmax;
2717 iso->debug = ep->debug;
2718 ival = ep->pollival;
2720 if(ep->dev->speed == Highspeed){
2728 iso->nframes = Nisoframes / ival;
2729 if(iso->nframes < 3)
2730 error("uhci isoopen bug"); /* we need at least 3 tds */
2731 iso->maxsize = ep->ntds * ep->maxpkt;
2732 if(ctlr->load + ep->load > 800)
2733 print("usb: ehci: bandwidth may be exceeded\n");
2735 ctlr->load += ep->load;
2736 ctlr->isoload += ep->load;
2738 dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
2739 diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
2740 iso->nframes, ep->pollival, ival, ep->maxpkt, ep->ntds);
2743 wakeup(&ctlr->poll);
2746 * From here on this cannot raise errors
2747 * unless we catch them and release here all memory allocated.
2749 assert(ep->maxpkt > 0 && ep->ntds > 0 && ep->ntds < 4);
2750 assert(ep->maxpkt <= 1024);
2751 iso->tdps = smalloc(sizeof(uintptr) * Nisoframes);
2752 iso->data = smalloc(iso->nframes * tpf * ep->ntds * ep->maxpkt);
2753 iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
2754 /* read: now; write: 1s ahead */
2756 if(ep->dev->speed == Highspeed)
2760 iso->tdu = iso->tdi = iso->itdps[iso->td0frno];
2761 iso->stdu = iso->stdi = iso->sitdps[iso->td0frno];
2765 frno = iso->td0frno;
2766 for(i = 0; i < iso->nframes; i++){
2767 *iso->tdps[frno] = ctlr->frames[frno];
2768 frno = TRUNC(frno+ival, Nisoframes);
2772 * Iso uses a virtual frame window of Nisoframes, and we must
2773 * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
2774 * copies of the window in the frame array.
2776 assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
2777 assert(Nisoframes >= Nintrleafs);
2778 n = ctlr->nframes / Nisoframes;
2779 for(w = 0; w < n; w++){
2780 frno = iso->td0frno;
2781 woff = w * Nisoframes;
2782 for(i = 0; i < iso->nframes ; i++){
2783 assert(woff+frno < ctlr->nframes);
2784 assert(iso->tdps[frno] != nil);
2785 if(ep->dev->speed == Highspeed)
2786 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2789 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno])
2792 frno = TRUNC(frno+ep->pollival, Nisoframes);
2796 iso->next = ctlr->iso;
2801 if(ehcidebug > 1 || iso->debug >1)
2806 * Allocate the endpoint and set it up for I/O
2807 * in the controller. This must follow what's said
2808 * in Ep regarding configuration, including perhaps
2809 * the saved toggles (saved on a previous close of
2810 * the endpoint data file by epclose).
2821 deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2823 panic("ehci: epopen called with open ep");
2831 error("endpoint not configured");
2833 ep->aux = smalloc(sizeof(Isoio));
2837 cio = ep->aux = smalloc(sizeof(Ctlio));
2838 cio->debug = ep->debug;
2841 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2843 cio->qh = qhalloc(ctlr, ep, cio, "epc");
2846 ep->pollival = 1; /* assume this; doesn't really matter */
2849 io = ep->aux = smalloc(sizeof(Qio)*2);
2850 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2851 usbid = (ep->nb&Epmax) << 7 | ep->dev->nb &Devmax;
2852 assert(ep->pollival != 0);
2853 if(ep->mode != OREAD){
2854 if(ep->toggle[OWRITE] != 0)
2855 io[OWRITE].toggle = Tddata1;
2857 io[OWRITE].toggle = Tddata0;
2858 io[OWRITE].tok = Tdtokout;
2859 io[OWRITE].usbid = usbid;
2860 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2861 io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
2863 if(ep->mode != OWRITE){
2864 if(ep->toggle[OREAD] != 0)
2865 io[OREAD].toggle = Tddata1;
2867 io[OREAD].toggle = Tddata0;
2868 io[OREAD].tok = Tdtokin;
2869 io[OREAD].usbid = usbid;
2870 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2871 io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
2876 if(ehcidebug>1 || ep->debug)
2878 deprint("ehci: epopen done\n");
2883 cancelio(Ctlr *ctlr, Qio *io)
2889 if(io == nil || io->qh == nil || io->qh->state == Qclose){
2893 dqprint("ehci: cancelio for qh %#p state %s\n",
2894 qh, qhsname[qh->state]);
2899 tsleep(&up->sleep, return0, 0, Abortdelay);
2904 /* wait for epio if running */
2912 cancelisoio(Ctlr *ctlr, Isoio *iso, int pollival, ulong load)
2914 int frno, i, n, t, w, woff;
2921 if(iso->state == Qclose){
2926 if(iso->state != Qrun && iso->state != Qdone)
2927 panic("bad iso state");
2928 iso->state = Qclose;
2930 if(ctlr->isoload < load)
2931 panic("ehci: low isoload");
2932 ctlr->isoload -= load;
2934 for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
2938 panic("cancleiso: not found");
2941 frno = iso->td0frno;
2942 for(i = 0; i < iso->nframes; i++){
2943 tp = iso->tdps[frno];
2945 td = iso->itdps[frno];
2946 for(t = 0; t < nelem(td->csw); t++)
2947 td->csw[t] &= ~(Itdioc|Itdactive);
2949 std = iso->sitdps[frno];
2950 std->csw &= ~(Stdioc|Stdactive);
2953 for(lp = &ctlr->frames[frno]; !(*lp & Lterm);
2958 panic("cancelisoio: td not found");
2961 * Iso uses a virtual frame window of Nisoframes, and we must
2962 * restore pointers in copies of the window kept at ctlr->frames.
2964 if(lp == &ctlr->frames[frno]){
2965 n = ctlr->nframes / Nisoframes;
2966 for(w = 1; w < n; w++){
2967 woff = w * Nisoframes;
2968 ctlr->frames[woff+frno] = *lp;
2972 frno = TRUNC(frno+pollival, Nisoframes);
2977 * wakeup anyone waiting for I/O and
2978 * wait to be sure no I/O is in progress in the controller.
2979 * and then wait to be sure episo* is no longer running.
2982 diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
2983 tsleep(&up->sleep, return0, 0, 5);
2986 diprint("cancelisoio iso %#p releasing iso\n", iso);
2988 frno = iso->td0frno;
2989 for(i = 0; i < iso->nframes; i++){
2991 itdfree(iso->itdps[frno]);
2993 sitdfree(iso->sitdps[frno]);
2994 iso->tdps[frno] = nil;
2995 frno = TRUNC(frno+pollival, Nisoframes);
3013 deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
3016 panic("ehci: epclose called with closed ep");
3020 cancelio(ctlr, cio);
3027 ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
3028 if(ep->mode != OWRITE){
3029 cancelio(ctlr, &io[OREAD]);
3030 if(io[OREAD].toggle == Tddata1)
3031 ep->toggle[OREAD] = 1;
3033 if(ep->mode != OREAD){
3034 cancelio(ctlr, &io[OWRITE]);
3035 if(io[OWRITE].toggle == Tddata1)
3036 ep->toggle[OWRITE] = 1;
3042 cancelisoio(ctlr, iso, ep->pollival, ep->load);
3045 panic("epclose: bad ttype");
3052 * return smallest power of 2 >= n
3059 for(i = 0; (1 << i) < n; i++)
3065 * build the periodic scheduling tree:
3066 * framesize must be a multiple of the tree size
3069 mkqhtree(Ctlr *ctlr)
3071 int i, n, d, o, leaf0, depth;
3072 ulong leafs[Nintrleafs];
3077 depth = flog2(Nintrleafs);
3078 n = (1 << (depth+1)) - 1;
3079 qt = mallocz(sizeof(*qt), 1);
3081 panic("ehci: mkqhtree: no memory");
3084 qt->bw = mallocz(n * sizeof(qt->bw), 1);
3085 qt->root = tree = mallocz(n * sizeof(Qh *), 1);
3086 if(qt->bw == nil || tree == nil)
3087 panic("ehci: mkqhtree: no memory");
3088 for(i = 0; i < n; i++){
3089 tree[i] = qh = edalloc();
3091 panic("ehci: mkqhtree: no memory");
3092 qh->nlink = qh->alink = qh->link = Lterm;
3097 qhlinkqh(tree[i], tree[(i-1)/2]);
3100 dprint("ehci: tree: %d endpoints allocated\n", i);
3102 /* distribute leaves evenly round the frame list */
3104 for(i = 0; i < Nintrleafs; i++){
3106 for(d = 0; d < depth; d++){
3112 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
3115 leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
3117 assert((ctlr->nframes % Nintrleafs) == 0);
3118 for(i = 0; i < ctlr->nframes; i += Nintrleafs){
3119 memmove(ctlr->frames + i, leafs, sizeof leafs);
3127 ehcimeminit(Ctlr *ctlr)
3133 frsize = ctlr->nframes * sizeof(ulong);
3134 assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
3135 ctlr->frames = xspanalloc(frsize, frsize, 0);
3136 if(ctlr->frames == nil)
3137 panic("ehci reset: no memory");
3139 for (i = 0; i < ctlr->nframes; i++)
3140 ctlr->frames[i] = Lterm;
3141 opio->frbase = PADDR(ctlr->frames);
3145 qhalloc(ctlr, nil, nil, nil); /* init async list */
3146 mkqhtree(ctlr); /* init sync list */
3147 edfree(edalloc()); /* try to get some ones pre-allocated */
3149 dprint("ehci %#p flb %#lux frno %#lux\n",
3150 ctlr->capio, opio->frbase, opio->frno);
3163 dprint("ehci %#p init\n", ctlr->capio);
3167 * Unless we activate frroll interrupt
3168 * some machines won't post other interrupts.
3170 opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
3177 opio->config = Callmine; /* reclaim all ports */
3180 for (i = 0; i < hp->nports; i++)
3181 opio->portsc[i] = Pspower;
3188 ehcilinkage(Hci *hp)
3192 hp->interrupt = interrupt;
3193 hp->epopen = epopen;
3194 hp->epclose = epclose;
3195 hp->epread = epread;
3196 hp->epwrite = epwrite;
3197 hp->seprintep = seprintep;
3198 hp->portenable = portenable;
3199 hp->portreset = portreset;
3200 hp->portstatus = portstatus;
3201 // hp->shutdown = shutdown;
3202 // hp->debug = setdebug;