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"
24 #define diprint if(ehcidebug || iso->debug)print
25 #define ddiprint if(ehcidebug>1 || iso->debug>1)print
26 #define dqprint if(ehcidebug || (qh->io && qh->io->debug))print
27 #define ddqprint if(ehcidebug>1 || (qh->io && qh->io->debug>1))print
29 #define TRUNC(x, sz) ((x) & ((sz)-1))
30 #define LPTR(q) ((ulong*)KADDR((q) & ~0x1F))
32 typedef struct Ctlio Ctlio;
34 typedef struct Edpool Edpool;
35 typedef struct Itd Itd;
36 typedef struct Qio Qio;
37 typedef struct Qtd Qtd;
38 typedef struct Sitd Sitd;
42 * EHCI interface registers and bits
46 /* Queue states (software) */
54 Enabledelay = 100, /* waiting for a port to enable */
55 Abortdelay = 10, /* delay after cancelling Tds (ms) */
57 Incr = 64, /* for pools of Tds, Qhs, etc. */
58 Align = 128, /* in bytes for all those descriptors */
60 /* Keep them as a power of 2, lower than ctlr->nframes */
61 /* Also, keep Nisoframes >= Nintrleafs */
62 Nintrleafs = 32, /* nb. of leaf frames in intr. tree */
63 Nisoframes = 64, /* nb. of iso frames (in window) */
69 /* Itd bits (csw[]) */
70 Itdactive = 0x80000000, /* execution enabled */
71 Itddberr = 0x40000000, /* data buffer error */
72 Itdbabble = 0x20000000, /* babble error */
73 Itdtrerr = 0x10000000, /* transaction error */
74 Itdlenshift = 16, /* transaction length */
76 Itdioc = 0x00008000, /* interrupt on complete */
77 Itdpgshift = 12, /* page select field */
78 Itdoffshift = 0, /* transaction offset */
79 /* Itd bits, buffer[] */
80 Itdepshift = 8, /* endpoint address (buffer[0]) */
81 Itddevshift = 0, /* device address (buffer[0]) */
82 Itdin = 0x800, /* is input (buffer[1]) */
84 Itdmaxpktshift = 0, /* max packet (buffer[1]) */
85 Itdntdsshift = 0, /* nb. of tds per µframe (buffer[2]) */
87 Itderrors = Itddberr|Itdbabble|Itdtrerr,
90 Stdin = 0x80000000, /* input direction */
91 Stdportshift = 24, /* hub port number */
92 Stdhubshift = 16, /* hub address */
93 Stdepshift = 8, /* endpoint address */
94 Stddevshift = 0, /* device address */
96 Stdssmshift = 0, /* split start mask */
97 Stdscmshift = 8, /* split complete mask */
99 Stdioc = 0x80000000, /* interrupt on complete */
100 Stdpg = 0x40000000, /* page select */
101 Stdlenshift = 16, /* total bytes to transfer */
103 Stdactive = 0x00000080, /* active */
104 Stderr = 0x00000040, /* tr. translator error */
105 Stddberr = 0x00000020, /* data buffer error */
106 Stdbabble = 0x00000010, /* babble error */
107 Stdtrerr = 0x00000008, /* transaction error */
108 Stdmmf = 0x00000004, /* missed µframe */
109 Stddcs = 0x00000002, /* do complete split */
111 Stderrors = Stderr|Stddberr|Stdbabble|Stdtrerr|Stdmmf,
113 /* Sitd bits buffer[1] */
114 Stdtpall = 0x00000000, /* all payload here (188 bytes) */
115 Stdtpbegin = 0x00000008, /* first payload for fs trans. */
116 Stdtcntmask = 0x00000007, /* T-count */
119 Tddata1 = 0x80000000, /* data toggle 1 */
120 Tddata0 = 0x00000000, /* data toggle 0 */
121 Tdlenshift = 16, /* total bytes to transfer */
123 Tdmaxpkt = 0x5000, /* max buffer for a Td */
124 Tdioc = 0x00008000, /* interrupt on complete */
125 Tdpgshift = 12, /* current page */
127 Tderr1 = 0x00000400, /* bit 0 of error counter */
128 Tderr2 = 0x00000800, /* bit 1 of error counter */
129 Tdtokout = 0x00000000, /* direction out */
130 Tdtokin = 0x00000100, /* direction in */
131 Tdtoksetup = 0x00000200, /* setup packet */
132 Tdtok = 0x00000300, /* token bits */
133 Tdactive = 0x00000080, /* active */
134 Tdhalt = 0x00000040, /* halted */
135 Tddberr = 0x00000020, /* data buffer error */
136 Tdbabble = 0x00000010, /* babble error */
137 Tdtrerr = 0x00000008, /* transaction error */
138 Tdmmf = 0x00000004, /* missed µframe */
139 Tddcs = 0x00000002, /* do complete split */
140 Tdping = 0x00000001, /* do ping */
142 Tderrors = Tdhalt|Tddberr|Tdbabble|Tdtrerr|Tdmmf,
145 Qhrlcmask = 0xF, /* nak reload count */
146 Qhrlcshift = 28, /* nak reload count */
147 Qhnhctl = 0x08000000, /* not-high speed ctl */
148 Qhmplmask = 0x7FF, /* max packet */
150 Qhhrl = 0x00008000, /* head of reclamation list */
151 Qhdtc = 0x00004000, /* data toggle ctl. */
152 Qhint = 0x00000080, /* inactivate on next transition */
153 Qhspeedmask = 0x00003000, /* speed bits */
154 Qhfull = 0x00000000, /* full speed */
155 Qhlow = 0x00001000, /* low speed */
156 Qhhigh = 0x00002000, /* high speed */
159 Qhmultshift = 30, /* multiple tds per µframe */
161 Qhportshift = 23, /* hub port number */
162 Qhhubshift = 16, /* hub address */
163 Qhscmshift = 8, /* split completion mask bits */
164 Qhismshift = 0, /* interrupt sched. mask bits */
168 * Endpoint tree (software)
179 * One per endpoint per direction, to control I/O.
183 QLock; /* for the entire I/O process */
184 Rendez; /* wait for completion */
185 Qh* qh; /* Td list (field const after init) */
186 int usbid; /* usb address for endpoint/device */
187 int toggle; /* Tddata0/Tddata1 */
188 int tok; /* Tdtoksetup, Tdtokin, Tdtokout */
189 ulong iotime; /* last I/O time; to hold interrupt polls */
190 int debug; /* debug flag from the endpoint */
191 char* err; /* error string */
192 char* tag; /* debug (no room in Qh for this) */
198 Qio; /* a single Qio for each RPC */
199 uchar* data; /* read from last ctl req. */
200 int ndata; /* number of bytes read */
206 Rendez; /* wait for space/completion/errors */
207 int usbid; /* address used for device/endpoint */
208 int tok; /* Tdtokin or Tdtokout */
209 int state; /* Qrun -> Qdone -> Qrun... -> Qclose */
210 int nframes; /* number of frames ([S]Itds) used */
211 uchar* data; /* iso data buffers if not embedded */
212 char* err; /* error string */
213 int nerrs; /* nb of consecutive I/O errors */
214 ulong maxsize; /* ntds * ep->maxpkt */
215 long nleft; /* number of bytes left from last write */
216 int debug; /* debug flag from the endpoint */
217 int delay; /* max number of bytes to buffer */
218 int hs; /* is high speed? */
219 Isoio* next; /* in list of active Isoios */
220 int ival; /* ep->pollival (/8 for HS) */
221 int uframes; /* ep->uframes */
222 ulong td0frno; /* first frame used in ctlr */
224 Itd* tdi; /* next td processed by interrupt */
228 Itd* tdu; /* next td for user I/O in tdps */
232 Itd** itdps; /* itdps[i]: ptr to Itd for i-th frame or nil */
233 Sitd** sitdps; /* sitdps[i]: ptr to Sitd for i-th frame or nil */
234 ulong** tdps; /* same thing, as seen by hw */
248 * We use the 64-bit version for Itd, Sitd, Td, and Qh.
249 * If the ehci is 64-bit capable it assumes we are using those
250 * structures even when the system is 32 bits.
254 * Iso transfer descriptor. hw: 92 bytes, 108 bytes total
259 ulong link; /* to next hw struct */
260 ulong csw[8]; /* sts/length/pg/off. updated by hw */
261 ulong buffer[7]; /* buffer pointers, addrs, maxsz */
262 ulong xbuffer[7]; /* high 32 bits of buffer for 64-bits */
264 ulong _pad0; /* pad to next cache line */
265 /* cache-line boundary here */
269 ulong ndata; /* number of bytes in data */
270 ulong mdata; /* max number of bytes in data */
276 * Split transaction iso transfer descriptor.
277 * hw: 36 bytes, 52 bytes total. aligned to 32.
281 ulong link; /* to next hw struct */
282 ulong epc; /* static endpoint state. addrs */
283 ulong mfs; /* static endpoint state. µ-frame sched. */
284 ulong csw; /* transfer state. updated by hw */
285 ulong buffer[2]; /* buf. ptr/offset. offset updated by hw */
286 /* buf ptr/TP/Tcnt. TP/Tcnt updated by hw */
287 ulong blink; /* back pointer */
288 /* cache-line boundary after xbuffer[0] */
289 ulong xbuffer[2]; /* high 32 bits of buffer for 64-bits */
293 ulong ndata; /* number of bytes in data */
294 ulong mdata; /* max number of bytes in data */
299 * Queue element transfer descriptor.
300 * hw: first 52 bytes, total 68+sbuff bytes. aligned to 32 bytes.
304 ulong nlink; /* to next Td */
305 ulong alink; /* alternate link to next Td */
306 ulong csw; /* cmd/sts. updated by hw */
307 ulong buffer[5]; /* buf ptrs. offset updated by hw */
308 /* cache-line boundary here */
309 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
312 Td* next; /* in qh or Isoio or free list */
313 ulong ndata; /* bytes available/used at data */
314 uchar* data; /* pointer to actual data */
315 uchar* buff; /* allocated data buffer or nil */
316 uchar sbuff[1]; /* first byte of embedded buffer */
320 * Queue head. Aligned to 32 bytes.
321 * hw: first 68 bytes, 92 total.
325 ulong link; /* to next Qh in round robin */
326 ulong eps0; /* static endpoint state. addrs */
327 ulong eps1; /* static endpoint state. µ-frame sched. */
330 ulong tclink; /* current Td (No Term bit here!) */
331 ulong nlink; /* to next Td */
332 ulong alink; /* alternate link to next Td */
333 ulong csw; /* cmd/sts. updated by hw */
334 /* cache-line boundary after buffer[0] */
335 ulong buffer[5]; /* buf ptrs. offset updated by hw */
336 ulong xbuffer[5]; /* high 32 bits of buffer for 64-bits */
339 Qh* next; /* in controller list/tree of Qhs */
340 int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
341 Qio* io; /* for this queue */
342 Td* tds; /* for this queue */
343 int sched; /* slot for for intr. Qhs */
344 Qh* inext; /* next in list of intr. qhs */
348 * We can avoid frame span traversal nodes if we don't span frames.
349 * Just schedule transfers that can fit on the current frame and
350 * wait a little bit otherwise.
354 * Software. Ehci descriptors provided by pool.
355 * There are soo few because we avoid using Fstn.
359 Ed* next; /* in free list */
369 static Edpool edpool;
370 static char Ebug[] = "not yet implemented";
371 static char* qhsname[] = { "idle", "install", "run", "done", "close", "FREE" };
373 Ecapio* ehcidebugcapio;
377 ehcirun(Ctlr *ctlr, int on)
382 ddprint("ehci %#p %s\n", ctlr->capio, on ? "starting" : "halting");
389 for(i = 0; i < 100; i++)
390 if(on == 0 && (opio->sts & Shalted) != 0)
392 else if(on != 0 && (opio->sts & Shalted) == 0)
397 print("ehci %#p %s cmd timed out\n",
398 ctlr->capio, on ? "run" : "halt");
399 ddprint("ehci %#p cmd %#lux sts %#lux\n",
400 ctlr->capio, opio->cmd, opio->sts);
410 if(edpool.free == nil){
411 pool = (*ctlr->tdalloc)(Incr*sizeof(Ed), Align, 0);
414 for(i=Incr; --i>=0;){
415 pool[i].next = edpool.free;
416 edpool.free = &pool[i];
418 edpool.nalloc += Incr;
419 edpool.nfree += Incr;
420 dprint("ehci: edalloc: %d eds\n", edpool.nalloc);
423 edpool.free = ed->next;
428 memset(ed, 0, sizeof(Ed)); /* safety */
439 ed->next = edpool.free;
447 * Allocate and do some initialization.
448 * Free after releasing buffers used.
462 itdfree(Ctlr*, Itd *td)
468 sitdalloc(Ctlr *ctlr)
473 td->link = td->blink = Lterm;
478 sitdfree(Ctlr*, Sitd *td)
489 td->nlink = td->alink = Lterm;
494 tdfree(Ctlr *ctlr, Td *td)
499 (*ctlr->dmafree)(td->buff);
504 tdlinktd(Td *td, Td *next)
511 td->nlink = PADDR(next);
516 qhlinkqh(Qh *qh, Qh *next)
522 qh->link = PADDR(next)|Lqh;
528 qhsetaddr(Qh *qh, ulong addr)
532 eps0 = qh->eps0 & ~((Epmax<<8)|Devmax);
533 qh->eps0 = eps0 | addr & Devmax | ((addr >> 7) & Epmax) << 8;
538 * return smallest power of 2 <= n
545 for(i = 0; (1 << (i + 1)) <= n; i++)
551 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
553 int i, j, d, upperb, q;
554 ulong best, worst, total;
556 d = flog2lower(pollival);
562 upperb = (1 << (d+1)) - 1;
563 for(i = (1 << d) - 1; i < upperb; i++){
565 for(j = i; j > 0; j = (j - 1) / 2)
574 if(worst + bw >= limit)
580 schedq(Ctlr *ctlr, Qh *qh, int pollival)
587 q = pickschedq(ctlr->tree, pollival, 0, ~0);
588 ddqprint("ehci: sched %#p q %d, ival %d, bw %uld\n",
589 qh->io, q, pollival, bw);
591 print("ehci: no room for ed\n");
594 ctlr->tree->bw[q] += bw;
595 tqh = ctlr->tree->root[q];
597 qhlinkqh(qh, tqh->next);
600 qh->inext = ctlr->intrqhs;
607 unschedq(Ctlr *ctlr, Qh *qh)
610 Qh *prev, *this, *next;
618 ctlr->tree->bw[q] -= bw;
620 prev = ctlr->tree->root[q];
622 while(this != nil && this != qh){
627 print("ehci: unschedq %d: not found\n", q);
630 qhlinkqh(prev, next);
632 for(l = &ctlr->intrqhs; *l != nil; l = &(*l)->inext)
637 print("ehci: unschedq: qh %#p not found\n", qh);
643 return (qh->eps0 >> Qhmplshift) & Qhmplmask;
647 qhsetmaxpkt(Qh *qh, int maxpkt)
651 eps0 = qh->eps0 & ~(Qhmplmask << Qhmplshift);
652 qh->eps0 = eps0 | (maxpkt & Qhmplmask) << Qhmplshift;
657 * Initialize the round-robin circular list of ctl/bulk Qhs
658 * if ep is nil. Otherwise, allocate and link a new Qh in the ctlr.
661 qhalloc(Ctlr *ctlr, Ep *ep, Qio *io, char* tag)
675 qhsetmaxpkt(qh, ep->maxpkt);
676 if(ep->dev->speed == Lowspeed)
678 if(ep->dev->speed == Highspeed)
680 else if(ep->ttype == Tctl)
682 qh->eps0 |= Qhdtc | 8 << Qhrlcshift; /* 8 naks max */
684 qhsetaddr(qh, io->usbid);
685 qh->eps1 = (ep->ntds & Qhmultmask) << Qhmultshift;
686 qh->eps1 |= ep->dev->port << Qhportshift;
687 qh->eps1 |= ep->dev->hub << Qhhubshift;
688 qh->eps1 |= 034 << Qhscmshift;
689 if(ep->ttype == Tintr)
690 qh->eps1 |= 1 << Qhismshift; /* intr. start µf. */
702 if(ctlr->qhs == nil){
703 ctlr->qhs = qhlinkqh(qh, qh);
704 qh->eps0 |= Qhhigh | Qhhrl;
706 ctlr->opio->link = PADDR(qh)|Lqh;
709 qhlinkqh(qh, ctlr->qhs->next);
710 qhlinkqh(ctlr->qhs, qh);
714 schedq(ctlr, qh, ep->pollival);
717 print("ehci: qhalloc called for ttype != ctl/bulk\n");
729 return (ctlr->opio->cmd & Ciasync) == 0;
733 * called when a qh is removed, to be sure the hw is not
734 * keeping pointers into it.
737 qhcoherency(Ctlr *ctlr)
741 qlock(&ctlr->portlck);
742 ctlr->opio->cmd |= Ciasync; /* ask for intr. on async advance */
744 for(i = 0; i < 3 && qhadvanced(ctlr) == 0; i++){
747 tsleep(ctlr, qhadvanced, ctlr, Abortdelay);
750 dprint("ehci: qhcoherency: doorbell %d\n", qhadvanced(ctlr));
752 print("ehci: async advance doorbell did not ring\n");
753 ctlr->opio->cmd &= ~Ciasync; /* try to clean */
754 qunlock(&ctlr->portlck);
758 qhfree(Ctlr *ctlr, Qh *qh)
765 for(q = ctlr->qhs; q != nil; q = q->next)
769 panic("qhfree: nil q");
775 qh->state = Qfree; /* paranoia */
780 while((td = qh->tds) != nil){
789 qhlinktd(Qh *qh, Td *td)
797 qh->csw = (csw & ~Tdactive) | Tdhalt;
799 csw &= Tddata1 | Tdping; /* save */
804 qh->nlink = PADDR(td);
805 for(i = 0; i < nelem(qh->buffer); i++)
808 qh->csw = csw & ~(Tdhalt|Tdactive); /* activate next */
814 seprintlink(char *s, char *se, char *name, ulong l, int typed)
816 s = seprint(s, se, "%s %ulx", name, l);
818 return seprint(s, se, "T");
823 return seprint(s, se, "I");
825 return seprint(s, se, "Q");
827 return seprint(s, se, "S");
829 return seprint(s, se, "F");
834 seprintitd(char *s, char *se, Itd *td)
842 return seprint(s, se, "<nil itd>\n");
847 s = seprint(s, se, "itd %#p", td);
848 rw = (b1 & Itdin) ? "in" : "out";
849 s = seprint(s, se, " %s ep %uld dev %uld max %uld mult %uld mdata %uld pos (%ud,%ud)",
850 rw, (b0>>8)&Epmax, (b0&Devmax), b1 & 0x7ff, b2 & 3, td->mdata, td->posi, td->posp);
851 s = seprintlink(s, se, " link", td->link, 1);
852 s = seprint(s, se, "\n");
853 for(i = 0; i < nelem(td->csw); i++){
854 memset(flags, '-', 5);
855 if((td->csw[i] & Itdactive) != 0)
857 if((td->csw[i] & Itdioc) != 0)
859 if((td->csw[i] & Itddberr) != 0)
861 if((td->csw[i] & Itdbabble) != 0)
863 if((td->csw[i] & Itdtrerr) != 0)
866 s = seprint(s, se, "\ttd%d %s", i, flags);
867 s = seprint(s, se, " len %lud", (td->csw[i] >> 16) & 0xfff);
868 s = seprint(s, se, " pg %lud", (td->csw[i] >> 12) & 0x7);
869 s = seprint(s, se, " off %lud\n", td->csw[i] & 0xfff);
871 s = seprint(s, se, "\tbuffs:");
872 for(i = 0; i < nelem(td->buffer); i++)
873 s = seprint(s, se, " %#lux", td->buffer[i] >> 12);
874 return seprint(s, se, "\n");
878 seprintsitd(char *s, char *se, Sitd *td)
882 static char pc[4] = { 'a', 'b', 'm', 'e' };
885 return seprint(s, se, "<nil sitd>\n");
886 s = seprint(s, se, "sitd %#p", td);
887 rw = (td->epc & Stdin) ? 'r' : 'w';
888 s = seprint(s, se, " %c ep %uld dev %uld",
889 rw, (td->epc>>8)&0xf, td->epc&0x7f);
890 s = seprint(s, se, " max %uld", (td->csw >> 16) & 0x3ff);
891 s = seprint(s, se, " hub %uld", (td->epc >> 16) & 0x7f);
892 s = seprint(s, se, " port %uld\n", (td->epc >> 24) & 0x7f);
893 memset(flags, '-', 7);
894 if((td->csw & Stdactive) != 0)
896 if((td->csw & Stdioc) != 0)
898 if((td->csw & Stderr) != 0)
900 if((td->csw & Stddberr) != 0)
902 if((td->csw & Stdbabble) != 0)
904 if((td->csw & Stdtrerr) != 0)
906 if((td->csw & Stdmmf) != 0)
909 ss = (td->csw & Stddcs) ? 'c' : 's';
910 pg = (td->csw & Stdpg) ? '1' : '0';
911 s = seprint(s, se, "\t%s %cs pg%c", flags, ss, pg);
912 s = seprint(s, se, " b0 %#lux b1 %#lux off %uld\n",
913 td->buffer[0] >> 12, td->buffer[1] >> 12, td->buffer[0] & 0xfff);
914 s = seprint(s, se, "\ttpos %c tcnt %uld",
915 pc[(td->buffer[0]>>3)&3], td->buffer[1] & 7);
916 s = seprint(s, se, " ssm %#lux csm %#lux cspm %#lux",
917 td->mfs & 0xff, (td->mfs>>8) & 0xff, (td->csw>>8) & 0xff);
918 s = seprintlink(s, se, " link", td->link, 1);
919 s = seprintlink(s, se, " blink", td->blink, 0);
920 return seprint(s, se, "\n");
926 return (td->csw >> Tdlenshift) & Tdlenmask;
934 return td->ndata - maxtdlen(td);
938 seprinttd(char *s, char *se, Td *td, char *tag)
943 static char *tok[4] = { "out", "in", "setup", "BUG" };
946 return seprint(s, se, "%s <nil td>\n", tag);
947 s = seprint(s, se, "%s %#p", tag, td);
948 s = seprintlink(s, se, " nlink", td->nlink, 0);
949 s = seprintlink(s, se, " alink", td->alink, 0);
950 s = seprint(s, se, " %s", tok[(td->csw & Tdtok) >> 8]);
951 if((td->csw & Tdping) != 0)
952 s = seprint(s, se, " png");
953 memset(flags, '-', 8);
954 if((td->csw & Tdactive) != 0)
956 if((td->csw & Tdioc) != 0)
958 if((td->csw & Tdhalt) != 0)
960 if((td->csw & Tddberr) != 0)
962 if((td->csw & Tdbabble) != 0)
964 if((td->csw & Tdtrerr) != 0)
966 if((td->csw & Tdmmf) != 0)
968 if((td->csw & (Tderr2|Tderr1)) == 0)
971 t = (td->csw & Tddata1) ? '1' : '0';
972 ss = (td->csw & Tddcs) ? 'c' : 's';
973 s = seprint(s, se, "\n\td%c %s %cs", t, flags, ss);
974 s = seprint(s, se, " max %uld", maxtdlen(td));
975 s = seprint(s, se, " pg %uld off %#lux\n",
976 (td->csw >> Tdpgshift) & Tdpgmask, td->buffer[0] & 0xFFF);
977 s = seprint(s, se, "\tbuffs:");
978 for(i = 0; i < nelem(td->buffer); i++)
979 s = seprint(s, se, " %#lux", td->buffer[i]>>12);
981 s = seprintdata(s, se, td->data, td->ndata);
982 return seprint(s, se, "\n");
986 dumptd(Td *td, char *pref)
993 se = buf+sizeof(buf);
994 for(; td != nil; td = td->next){
995 seprinttd(buf, se, td, pref);
998 print("...more tds...\n");
1010 static char *speed[] = {"full", "low", "high", "BUG"};
1013 print("<nil qh>\n");
1020 se = buf+sizeof(buf);
1021 s = seprint(buf, se, "%s %#p", tag, qh);
1022 s = seprint(s, se, " ep %uld dev %uld",
1023 (qh->eps0>>8)&0xf, qh->eps0&0x7f);
1024 s = seprint(s, se, " hub %uld", (qh->eps1 >> 16) & 0x7f);
1025 s = seprint(s, se, " port %uld", (qh->eps1 >> 23) & 0x7f);
1026 s = seprintlink(s, se, " link", qh->link, 1);
1027 seprint(s, se, " clink %#lux", qh->tclink);
1029 s = seprint(buf, se, "\tnrld %uld", (qh->eps0 >> Qhrlcshift) & Qhrlcmask);
1030 s = seprint(s, se, " nak %uld", (qh->alink >> 1) & 0xf);
1031 s = seprint(s, se, " max %uld ", qhmaxpkt(qh));
1032 if((qh->eps0 & Qhnhctl) != 0)
1033 s = seprint(s, se, "c");
1034 if((qh->eps0 & Qhhrl) != 0)
1035 s = seprint(s, se, "h");
1036 if((qh->eps0 & Qhdtc) != 0)
1037 s = seprint(s, se, "d");
1038 if((qh->eps0 & Qhint) != 0)
1039 s = seprint(s, se, "i");
1040 s = seprint(s, se, " %s", speed[(qh->eps0 >> 12) & 3]);
1041 s = seprint(s, se, " mult %uld", (qh->eps1 >> Qhmultshift) & Qhmultmask);
1042 seprint(s, se, " scm %#lux ism %#lux\n",
1043 (qh->eps1 >> 8 & 0xff), qh->eps1 & 0xff);
1045 memset(&td, 0, sizeof(td));
1046 memmove(&td, &qh->nlink, 32); /* overlay area */
1047 seprinttd(buf, se, &td, "\tovl");
1052 isodump(Isoio* iso, int all)
1054 Itd *td, *tdi, *tdu;
1055 Sitd *std, *stdi, *stdu;
1060 print("<nil iso>\n");
1063 print("iso %#p %s %s speed state %d nframes %d maxsz %uld",
1064 iso, iso->tok == Tdtokin ? "in" : "out",
1065 iso->hs ? "high" : "full",
1066 iso->state, iso->nframes, iso->maxsize);
1067 print(" td0 %uld tdi %#p tdu %#p data %#p\n",
1068 iso->td0frno, iso->tdi, iso->tdu, iso->data);
1070 print("\terr %s\n", iso->err);
1072 print("\terr='%s'\n", iso->err);
1076 seprintitd(buf, buf+sizeof(buf), tdi);
1078 putstrn(buf, strlen(buf));
1080 seprintitd(buf, buf+sizeof(buf), tdu);
1082 putstrn(buf, strlen(buf));
1085 seprintsitd(buf, buf+sizeof(buf), stdi);
1087 putstrn(buf, strlen(buf));
1089 seprintsitd(buf, buf+sizeof(buf), stdu);
1091 putstrn(buf, strlen(buf));
1094 for(i = 0; i < Nisoframes; i++)
1095 if(iso->tdps[i] != nil)
1098 seprintitd(buf, buf+sizeof(buf), td);
1104 putstrn(buf, strlen(buf));
1106 std = iso->sitdps[i];
1107 seprintsitd(buf, buf+sizeof(buf), std);
1108 if(std == iso->stdi)
1110 if(std == iso->stdu)
1113 putstrn(buf, strlen(buf));
1131 print("ehci port %#p frames %#p (%d fr.) nintr %d ntdintr %d",
1132 ctlr->capio, ctlr->frames, ctlr->nframes,
1133 ctlr->nintr, ctlr->ntdintr);
1134 print(" nqhintr %d nisointr %d\n", ctlr->nqhintr, ctlr->nisointr);
1135 print("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1136 opio->cmd, opio->sts, opio->intr, opio->frno);
1137 print(" base %#lux link %#lux fr0 %#lux\n",
1138 opio->frbase, opio->link, ctlr->frames[0]);
1139 se = buf+sizeof(buf);
1140 s = seprint(buf, se, "\t");
1141 for(i = 0; i < hp->nports; i++){
1142 s = seprint(s, se, "p%d %#lux ", i, opio->portsc[i]);
1143 if(hp->nports > 4 && i == hp->nports/2 - 1)
1144 s = seprint(s, se, "\n\t");
1152 }while(qh != ctlr->qhs && i++ < 100);
1154 print("...too many Qhs...\n");
1155 if(ctlr->intrqhs != nil)
1156 print("intr qhs:\n");
1157 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1159 if(ctlr->iso != nil)
1161 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1162 isodump(ctlr->iso, 0);
1163 print("%d eds in tree\n", ctlr->ntree);
1166 print("%d eds allocated = %d in use + %d free\n",
1167 edpool.nalloc, edpool.ninuse, edpool.nfree);
1177 return "data buffer error";
1179 return "babble detected";
1181 return "transaction error";
1183 return "missed µframe";
1185 return Estalled; /* [uo]hci report this error */
1195 return "data buffer error";
1197 return "babble detected";
1199 return "transaction error";
1207 return "translation translator error";
1208 /* other errors have same numbers than Td errors */
1218 if(iso->state == Qclose)
1220 if(iso->state == Qrun && iso->tok == Tdtokin){
1221 if(iso->hs != 0 && iso->tdi != iso->tdu)
1223 if(iso->hs == 0 && iso->stdi != iso->stdu)
1230 isocanwrite(void *a)
1235 if(iso->state == Qclose)
1237 if(iso->state == Qrun && iso->tok == Tdtokout){
1238 if(iso->hs != 0 && iso->tdu->next != iso->tdi)
1240 if(iso->hs == 0 && iso->stdu->next != iso->stdi)
1247 itdinit(Ctlr*, Isoio *iso, Itd *td)
1250 ulong pg, pa, tsize, size;
1253 * BUG: This does not put an integral number of samples
1254 * on each µframe unless samples per packet % 8 == 0
1255 * Also, all samples are packed early on each frame.
1257 size = td->ndata = td->mdata;
1258 td->posi = td->posp = 0;
1259 dmaflush(1, td->data, size);
1260 pa = PADDR(td->data);
1262 for(p = 0; p < nelem(td->buffer); p++)
1263 td->buffer[p] = (td->buffer[p] & 0xFFF) | ((p << 12) + pg);
1265 for(t = 0; t < nelem(td->csw); t++){
1271 if(tsize > iso->maxsize)
1272 tsize = iso->maxsize;
1273 p = (pa - pg) >> 12;
1274 assert(p < nelem(td->buffer));
1275 assert(p < 6 || (pa & ~0xFFF) == ((pa+tsize-1) & ~0xFFF));
1276 td->csw[t] = tsize << Itdlenshift | p << Itdpgshift |
1277 (pa & 0xFFF) << Itdoffshift | Itdactive | Itdioc;
1281 if(iso->debug >= 3){
1283 seprintitd(buf, buf + sizeof(buf), td);
1284 putstrn(buf, strlen(buf));
1290 sitdinit(Ctlr*, Isoio *iso, Sitd *td)
1292 dmaflush(1, td->data, td->mdata);
1293 td->ndata = td->mdata & Stdlenmask;
1294 td->buffer[0] = PADDR(td->data);
1295 td->buffer[1] = (td->buffer[0] & ~0xFFF) + 0x1000;
1296 if(iso->tok == Tdtokin || td->ndata <= 188)
1297 td->buffer[1] |= Stdtpall;
1299 td->buffer[1] |= Stdtpbegin;
1300 if(iso->tok == Tdtokin)
1303 td->buffer[1] |= ((td->ndata + 187) / 188) & Stdtcntmask;
1305 td->csw = td->ndata << Stdlenshift | Stdactive | Stdioc;
1314 for(i = 0; i < nelem(td->csw); i++)
1315 if((td->csw[i] & Itdactive) != 0)
1327 if(iso->state == Qclose || iso->err || iso->delay == 0)
1334 for(i = iso->tdi; i->next != iso->tdu; i = i->next){
1338 if(delay > iso->delay)
1344 for(i = iso->stdi; i->next != iso->stdu; i = i->next){
1345 if((i->csw & Stdactive) == 0)
1348 if(delay > iso->delay)
1353 return delay <= iso->delay;
1357 isohsinterrupt(Ctlr *ctlr, Isoio *iso)
1359 int err, i, nframes, t;
1363 if(tdi == nil || itdactive(tdi)) /* not all tds are done */
1367 ddiprint("isohsintr: iso %#p: tdi %#p tdu %#p\n", iso, tdi, iso->tdu);
1368 if(iso->state != Qrun && iso->state != Qdone)
1369 panic("isofsintr: iso state");
1370 if(ehcidebug > 1 || iso->debug > 1)
1373 nframes = iso->nframes / 2; /* limit how many we look */
1374 if(nframes > Nisoframes)
1375 nframes = Nisoframes;
1377 for(i = 0; i < nframes && itdactive(tdi) == 0; i++){
1379 for(t = 0; t < nelem(tdi->csw); t++){
1380 tdi->csw[t] &= ~Itdioc;
1382 err |= tdi->csw[t] & Itderrors;
1386 } else if(iso->nerrs++ > iso->nframes/2){
1387 if(iso->err == nil){
1388 iso->err = ierrmsg(err);
1389 diprint("isohsintr: tdi %#p error %#ux %s\n",
1390 tdi, err, iso->err);
1391 diprint("ctlr load %uld\n", ctlr->load);
1397 if(tdi->next == iso->tdu || tdi->next->next == iso->tdu){
1398 memset(iso->tdu->data, 0, iso->tdu->mdata);
1399 itdinit(ctlr, iso, iso->tdu);
1400 iso->tdu = iso->tdu->next;
1406 ddiprint("isohsintr: %d frames processed\n", nframes);
1408 tdi->csw[0] |= Itdioc;
1413 if(isocanwrite(iso) || isocanread(iso)){
1414 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1415 iso->tdi, iso->tdu);
1422 isofsinterrupt(Ctlr *ctlr, Isoio *iso)
1424 int err, i, nframes;
1428 if(stdi == nil || (stdi->csw & Stdactive) != 0) /* nothing new done */
1431 ddiprint("isofsintr: iso %#p: tdi %#p tdu %#p\n", iso, stdi, iso->stdu);
1432 if(iso->state != Qrun && iso->state != Qdone)
1433 panic("isofsintr: iso state");
1434 if(ehcidebug > 1 || iso->debug > 1)
1437 nframes = iso->nframes / 2; /* limit how many we look */
1438 if(nframes > Nisoframes)
1439 nframes = Nisoframes;
1441 for(i = 0; i < nframes && (stdi->csw & Stdactive) == 0; i++){
1442 stdi->csw &= ~Stdioc;
1443 /* write back csw and see if it produces errors */
1445 err = stdi->csw & Stderrors;
1448 if(iso->tok == Tdtokin)
1449 stdi->ndata = (stdi->csw>>Stdlenshift)&Stdlenmask;
1450 }else if(iso->nerrs++ > iso->nframes/2){
1451 if(iso->err == nil){
1452 iso->err = serrmsg(err);
1453 diprint("isofsintr: tdi %#p error %#ux %s\n",
1454 stdi, err, iso->err);
1455 diprint("ctlr load %uld\n", ctlr->load);
1461 if(stdi->next == iso->stdu || stdi->next->next == iso->stdu){
1462 memset(iso->stdu->data, 0, iso->stdu->mdata);
1463 sitdinit(ctlr, iso, iso->stdu);
1464 iso->stdu = iso->stdu->next;
1470 ddiprint("isofsintr: %d frames processed\n", nframes);
1472 stdi->csw |= Stdioc;
1477 if(isocanwrite(iso) || isocanread(iso)){
1478 diprint("wakeup iso %#p tdi %#p tdu %#p\n", iso,
1479 iso->stdi, iso->stdu);
1486 qhinterrupt(Ctlr *ctlr, Qh *qh)
1491 if(qh->state != Qrun)
1492 panic("qhinterrupt: qh state");
1496 if((td->csw & Tdactive) == 0)
1497 ddqprint("qhinterrupt port %#p qh %#p\n", ctlr->capio, qh);
1498 for(; td != nil; td = td->next){
1499 if(td->csw & Tdactive)
1501 err = td->csw & Tderrors;
1503 if(qh->io->err == nil){
1504 qh->io->err = errmsg(err);
1505 dqprint("qhintr: td %#p csw %#lux error %#ux %s\n",
1506 td, td->csw, err, qh->io->err);
1510 td->ndata = tdlen(td);
1512 if(td->ndata < maxtdlen(td)){ /* EOT */
1518 * Done. Make void the Tds not used (errors or EOT) and wakeup epio.
1520 for(; td != nil; td = td->next)
1530 ctlrinterrupt(Ctlr *ctlr)
1540 * Will we know in USB 3.0 who the interrupt was for?.
1541 * Do they still teach indexing in CS?
1542 * This is Intel's doing.
1544 sts = opio->sts & Sintrs;
1545 if(sts == 0) /* not ours; shared intr. */
1550 if((sts & Sherr) != 0)
1551 iprint("ehci: port %#p fatal host system error\n", ctlr->capio);
1552 if((sts & Shalted) != 0)
1553 iprint("ehci: port %#p: halted\n", ctlr->capio);
1554 if((sts & Sasync) != 0){
1555 dprint("ehci: doorbell\n");
1559 * We enter always this if, even if it seems the
1560 * interrupt does not report anything done/failed.
1561 * Some controllers don't post interrupts right.
1564 if((sts & (Serrintr|Sintr)) != 0){
1567 iprint("ehci port %#p frames %#p nintr %d ntdintr %d",
1568 ctlr->capio, ctlr->frames,
1569 ctlr->nintr, ctlr->ntdintr);
1570 iprint(" nqhintr %d nisointr %d\n",
1571 ctlr->nqhintr, ctlr->nisointr);
1572 iprint("\tcmd %#lux sts %#lux intr %#lux frno %uld",
1573 opio->cmd, opio->sts, opio->intr, opio->frno);
1576 /* process the Iso transfers */
1577 for(iso = ctlr->iso; iso != nil; iso = iso->next)
1578 if(iso->state == Qrun || iso->state == Qdone)
1580 some += isohsinterrupt(ctlr, iso);
1582 some += isofsinterrupt(ctlr, iso);
1584 /* process the qhs in the periodic tree */
1585 for(qh = ctlr->intrqhs; qh != nil; qh = qh->inext)
1586 if(qh->state == Qrun)
1587 some += qhinterrupt(ctlr, qh);
1589 /* process the async Qh circular list */
1595 if(qh->state == Qrun)
1596 some += qhinterrupt(ctlr, qh);
1598 }while(qh != ctlr->qhs && i++ < 100);
1600 iprint("echi: interrupt: qh loop?\n");
1613 some = ctlrinterrupt(ctlr);
1619 interrupt(Ureg*, void* a)
1625 portenable(Hci *hp, int port, int on)
1633 s = opio->portsc[port-1];
1634 eqlock(&ctlr->portlck);
1636 qunlock(&ctlr->portlck);
1639 dprint("ehci %#p port %d enable=%d; sts %#x\n",
1640 ctlr->capio, port, on, s);
1642 if(s & (Psstatuschg | Pschange))
1643 opio->portsc[port-1] = s;
1645 opio->portsc[port-1] |= Psenable;
1647 opio->portsc[port-1] &= ~Psenable;
1651 tsleep(&up->sleep, return0, 0, Enabledelay);
1652 dprint("ehci %#p port %d enable=%d: sts %#lux\n",
1653 ctlr->capio, port, on, opio->portsc[port-1]);
1654 qunlock(&ctlr->portlck);
1660 * If we detect during status that the port is low-speed or
1661 * during reset that it's full-speed, the device is not for
1662 * ourselves. The companion controller will take care.
1663 * Low-speed devices will not be seen by usbd. Full-speed
1664 * ones are seen because it's only after reset that we know what
1665 * they are (usbd may notice a device not enabled in this case).
1668 portlend(Ctlr *ctlr, int port, char *ss)
1675 dprint("ehci %#p port %d: %s speed device: no longer owned\n",
1676 ctlr->capio, port, ss);
1677 s = opio->portsc[port-1] & ~(Pschange|Psstatuschg);
1678 opio->portsc[port-1] = s | Psowner;
1683 portreset(Hci *hp, int port, int on)
1695 eqlock(&ctlr->portlck);
1698 qunlock(&ctlr->portlck);
1701 portscp = &opio->portsc[port-1];
1702 dprint("ehci %#p port %d reset; sts %#lux\n", ctlr->capio, port, *portscp);
1704 /* Shalted must be zero, else Psreset will stay set */
1705 if (opio->sts & Shalted)
1706 iprint("ehci %#p: halted yet trying to reset port\n",
1709 *portscp = (*portscp & ~Psenable) | Psreset; /* initiate reset */
1711 * usb 2 spec: reset must finish within 20 ms.
1712 * linux says spec says it can take 50 ms. for hubs.
1715 *portscp &= ~Psreset; /* terminate reset */
1718 for(i = 0; *portscp & Psreset && i < 10; i++)
1721 if (*portscp & Psreset)
1722 iprint("ehci %#p: port %d didn't reset; sts %#lux\n",
1723 ctlr->capio, port, *portscp);
1725 delay(10); /* ehci spec: enable within 2 ms. */
1726 if((*portscp & Psenable) == 0)
1727 portlend(ctlr, port, "full");
1730 dprint("ehci %#p after port %d reset; sts %#lux\n",
1731 ctlr->capio, port, *portscp);
1732 qunlock(&ctlr->portlck);
1738 portstatus(Hci *hp, int port)
1746 eqlock(&ctlr->portlck);
1749 qunlock(&ctlr->portlck);
1753 s = opio->portsc[port-1];
1754 if(s & (Psstatuschg | Pschange)){
1755 opio->portsc[port-1] = s;
1757 ddprint("ehci %#p port %d status %#x\n", ctlr->capio, port, s);
1760 * If the port is a low speed port we yield ownership now
1761 * to the [uo]hci companion controller and pretend it's not here.
1763 if((s & Pspresent) != 0 && (s & Pslinemask) == Pslow){
1764 portlend(ctlr, port, "low");
1765 s &= ~Pspresent; /* not for us this time */
1768 qunlock(&ctlr->portlck);
1772 * We must return status bits as a
1773 * get port status hub request would do.
1777 r |= HPpresent|HPhigh;
1792 seprintio(char *s, char *e, Qio *io, char *pref)
1794 s = seprint(s,e,"%s io %#p qh %#p id %#x", pref, io, io->qh, io->usbid);
1795 s = seprint(s,e," iot %ld", io->iotime);
1796 s = seprint(s,e," tog %#x tok %#x err %s", io->toggle, io->tok, io->err);
1801 seprintep(char *s, char *e, Ep *ep)
1817 s = seprintio(s, e, cio, "c");
1818 s = seprint(s, e, "\trepl %llux ndata %d\n", ep->rhrepl, cio->ndata);
1823 if(ep->mode != OWRITE)
1824 s = seprintio(s, e, &io[OREAD], "r");
1825 if(ep->mode != OREAD)
1826 s = seprintio(s, e, &io[OWRITE], "w");
1837 * halt condition was cleared on the endpoint. update our toggles.
1850 if(ep->mode != OREAD){
1852 io[OWRITE].toggle = Tddata0;
1853 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1854 qunlock(&io[OWRITE]);
1856 if(ep->mode != OWRITE){
1858 io[OREAD].toggle = Tddata0;
1859 deprint("ep clrhalt for io %#p\n", io+OREAD);
1860 qunlock(&io[OREAD]);
1867 xdump(char* pref, void *qh)
1873 print("%s %#p:", pref, u);
1874 for(i = 0; i < 16; i++)
1876 print("\n %#8.8ulx", u[i]);
1878 print(" %#8.8ulx", u[i]);
1883 isohscpy(Ctlr *ctlr, Isoio* iso, uchar *b, long count)
1890 ddiprint("hscpy: tdi %p tdu %p\n", iso->tdi, iso->tdu);
1891 for(tot = 0; iso->tdi != iso->tdu && tot < count; ){
1896 while(tdu->posi < 8 && tot < count){
1897 len = tdu->csw[tdu->posi] >> Itdlenshift & Itdlenmask;
1898 if((tdu->csw[tdu->posi] & Itderrors) != 0 || tdu->posp > len)
1900 nr = len - tdu->posp;
1901 if(nr > count - tot) nr = count - tot;
1902 ddiprint("hscpy: tdi %p tdu %p posi %d posp %d len %d nr %d\n", iso->tdi, iso->tdu, tdu->posi, tdu->posp, len, nr);
1903 dp = tdu->data + tdu->posi * iso->maxsize + tdu->posp;
1906 dmaflush(0, dp, nr);
1907 memmove(b+tot, dp, nr);
1909 if(iso->tdu != tdu || dp != tdu->data + tdu->posi * iso->maxsize + tdu->posp)
1913 if(iso->uframes == 1)
1917 if(tdu->posp == len){
1924 itdinit(ctlr, iso, tdu);
1925 iso->tdu = tdu->next;
1932 isofscpy(Ctlr *ctlr, Isoio* iso, uchar *b, long count)
1938 for(tot = 0; iso->stdi != iso->stdu && tot < count; tot += nr){
1940 if(stdu->csw & Stdactive)
1943 if(tot + nr > count)
1946 iunlock(ctlr); /* We could page fault here */
1947 dmaflush(0, stdu->data, stdu->mdata);
1948 memmove(b+tot, stdu->data, nr);
1950 if(iso->stdu != stdu)
1952 if(nr < stdu->ndata)
1953 memmove(stdu->data, stdu->data+nr,
1958 if(stdu->ndata == 0){
1959 sitdinit(ctlr, iso, stdu);
1960 iso->stdu = stdu->next;
1967 episoread(Ep *ep, Isoio *iso, void *a, long count)
1973 iso->debug = ep->debug;
1974 diprint("ehci: episoread: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1986 if(iso->state == Qclose){
1988 error(iso->err ? iso->err : Eio);
1992 while(isocanread(iso) == 0){
1994 diprint("ehci: episoread: %#p sleep\n", iso);
1997 iso->err = "I/O timed out";
2001 tsleep(iso, isocanread, iso, ep->tmout);
2005 if(iso->state == Qclose){
2007 error(iso->err ? iso->err : Eio);
2012 tot = isohscpy(ctlr, iso, b, count);
2014 tot = isofscpy(ctlr, iso, b, count);
2018 diprint("ehci: episoread: %#p %uld bytes err '%s'\n", iso, tot, iso->err);
2025 * iso->tdu is the next place to put data. When it gets full
2026 * it is activated and tdu advanced.
2029 putsamples(Ctlr *ctlr, Isoio *iso, uchar *b, long count)
2035 for(tot = 0; isocanwrite(iso) && tot < count; tot += n){
2040 if(n > tdu->mdata - left)
2041 n = tdu->mdata - left;
2042 iunlock(ctlr); /* We could page fault here */
2043 memmove(tdu->data + left, b + tot, n);
2048 if(iso->nleft == tdu->mdata){
2049 itdinit(ctlr, iso, tdu);
2050 iso->tdu = tdu->next;
2055 if(n > stdu->mdata - left)
2056 n = stdu->mdata - left;
2057 iunlock(ctlr); /* We could page fault here */
2058 memmove(stdu->data + left, b + tot, n);
2060 if(iso->stdu != stdu)
2063 if(iso->nleft == stdu->mdata){
2064 sitdinit(ctlr, iso, stdu);
2065 iso->stdu = stdu->next;
2074 * Queue data for writing and return error status from
2075 * last writes done, to maintain buffered data.
2078 episowrite(Ep *ep, Isoio *iso, void *a, long count)
2085 iso->delay = ep->sampledelay * ep->samplesz;
2086 iso->debug = ep->debug;
2087 diprint("ehci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
2096 if(iso->state == Qclose){
2098 error(iso->err ? iso->err : Eio);
2103 for(tot = 0; tot < count; tot += nw){
2104 while(isocanwrite(iso) == 0){
2106 diprint("ehci: episowrite: %#p sleep\n", iso);
2109 iso->err = "I/O timed out";
2113 tsleep(iso, isocanwrite, iso, ep->tmout);
2119 if(iso->state == Qclose || err != nil){
2121 error(err ? err : Eio);
2123 if(iso->state != Qrun)
2124 panic("episowrite: iso not running");
2125 nw = putsamples(ctlr, iso, b+tot, count-tot);
2127 while(isodelay(iso) == 0){
2129 sleep(iso, isodelay, iso);
2132 if(iso->state != Qclose)
2135 err = iso->err; /* in case it failed early */
2141 diprint("ehci: episowrite: %#p %d bytes\n", iso, tot);
2146 nexttoggle(int toggle, int count, int maxpkt)
2150 np = count / maxpkt;
2155 if(toggle == Tddata1)
2162 epgettd(Ctlr *ctlr, Qio *io, int flags, void *a, int count, int maxpkt)
2168 if(count > Tdmaxpkt)
2169 panic("ehci: epgettd: too many bytes");
2171 td->csw = flags | io->toggle | io->tok | count << Tdlenshift |
2176 * use the space wasted by alignment as an
2177 * embedded buffer if count bytes fit in there.
2179 assert(Align > sizeof(Td));
2180 if(count <= Align - sizeof(Td)){
2181 td->data = td->sbuff;
2183 } else if(count <= 0x4000){
2184 td->buff = td->data = (*ctlr->dmaalloc)(count);
2186 td->buff = (*ctlr->dmaalloc)(count+0x1000);
2187 td->data = (uchar*)ROUND((uintptr)td->buff, 0x1000);
2189 if(a != nil && count > 0)
2190 memmove(td->data, a, count);
2191 if(td->buff != nil){
2192 dmaflush(1, td->data, count);
2194 pa = PADDR(td->data);
2195 for(i = 0; i < nelem(td->buffer); i++){
2202 io->toggle = nexttoggle(io->toggle, count, maxpkt);
2208 * Try to get them idle
2215 if(qh->sched >= 0 && (qh->eps0 & Qhspeedmask) != Qhhigh)
2216 qh->eps0 |= Qhint; /* inactivate on next pass */
2217 qh->csw = (qh->csw & ~Tdactive) | Tdhalt;
2219 for(td = qh->tds; td != nil; td = td->next){
2220 if(td->csw & Tdactive){
2229 * Some controllers do not post the usb/error interrupt after
2230 * the work has been done. It seems that we must poll for them.
2233 workpending(void *a)
2238 return ctlr->nreqs > 0;
2255 if(ctlr->nreqs == 0){
2256 if(0)ddprint("ehcipoll %#p sleep\n", ctlr->capio);
2257 sleep(poll, workpending, ctlr);
2258 if(0)ddprint("ehcipoll %#p awaken\n", ctlr->capio);
2260 for(i = 0; i < 16 && ctlr->nreqs > 0; i++)
2261 if(ehciintr(hp) == 0)
2264 tsleep(&up->sleep, return0, 0, 1);
2266 }while(ctlr->nreqs > 0);
2279 if(poll->must != 0 && poll->does == 0){
2281 if(poll->must == 0 || poll->does != 0) {
2288 print("ehci %#p: polling\n", ctlr->capio);
2289 kproc("ehcipoll", ehcipoll, hp);
2299 return qh->state != Qrun;
2303 epiowait(Hci *hp, Qio *io, int tmout, ulong load)
2311 ddqprint("ehci io %#p sleep on qh %#p state %s\n",
2312 io, qh, qhsname[qh->state]);
2315 dqprint("ehci io %#p qh %#p timed out\n", io, qh);
2319 sleep(io, epiodone, qh);
2321 tsleep(io, epiodone, qh, tmout);
2326 /* Are we missing interrupts? */
2327 if(qh->state == Qrun){
2328 ctlrinterrupt(ctlr);
2329 if(qh->state == Qdone){
2331 dqprint("ehci %#p: polling required\n", ctlr->capio);
2332 ctlr->poll.must = 1;
2338 if(qh->state == Qrun){
2339 dqprint("ehci io %#p qh %#p timed out (no intr?)\n", io, qh);
2341 }else if(qh->state != Qdone && qh->state != Qclose)
2342 panic("ehci: epio: queue state %d", qh->state);
2347 io->err = "request timed out";
2351 tsleep(&up->sleep, return0, 0, Abortdelay);
2355 if(qh->state != Qclose)
2365 * To make it work for control transfers, the caller may
2366 * lock the Qio for the entire control transfer.
2369 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
2371 int saved, ntds, tmout;
2379 Td *td, *ltd, *td0, *ntd;
2382 io->debug = ep->debug;
2384 ddeprint("epio: %s ep%d.%d io %#p count %ld load %uld\n",
2385 io->tok == Tdtokin ? "in" : "out",
2386 ep->dev->nb, ep->nb, io, count, ctlr->load);
2387 if((ehcidebug > 1 || ep->debug > 1) && io->tok != Tdtokin){
2388 seprintdata(buf, buf+sizeof(buf), a, count);
2389 print("echi epio: user data: %s\n", buf);
2401 if(qh == nil || qh->state == Qclose){ /* Tds released by cancelio */
2403 error(io->err ? io->err : Eio);
2405 if(qh->state != Qidle)
2406 panic("epio: qh not idle");
2407 qh->state = Qinstall;
2414 n = (Tdmaxpkt / ep->maxpkt) * ep->maxpkt;
2417 if(c != nil && io->tok != Tdtokin)
2418 td = epgettd(ctlr, io, Tdactive, c+tot, n, ep->maxpkt);
2420 td = epgettd(ctlr, io, Tdactive, nil, n, ep->maxpkt);
2428 }while(tot < count);
2429 if(td0 == nil || ltd == nil)
2430 panic("epio: no td");
2432 ltd->csw |= Tdioc; /* the last one interrupts */
2435 ddeprint("ehci: load %uld ctlr load %uld\n", load, ctlr->load);
2436 if(ehcidebug > 1 || ep->debug > 1)
2437 dumptd(td0, "epio: put: ");
2440 if(qh->state != Qclose){
2441 io->iotime = TK2MS(MACHP(0)->ticks);
2451 wakeup(&ctlr->poll);
2453 epiowait(ep->hp, io, tmout, load);
2454 if(ehcidebug > 1 || ep->debug > 1){
2455 dumptd(td0, "epio: got: ");
2464 for(td = td0; td != nil; td = ntd){
2467 * Use td tok, not io tok, because of setup packets.
2468 * Also, we must save the next toggle value from the
2469 * last completed Td (in case of a short packet, or
2470 * fewer than the requested number of packets in the
2471 * Td being transferred).
2473 if(td->csw & (Tdhalt|Tdactive))
2477 io->toggle = td->csw & Tddata1;
2480 if(err == nil && (n = td->ndata) > 0 && tot < count){
2481 if((tot + n) > count)
2483 if(c != nil && (td->csw & Tdtok) == Tdtokin){
2484 if(td->buff != nil){
2485 dmaflush(0, td->data, n);
2487 memmove(c, td->data, n);
2500 ddeprint("epio: io %#p: %d tds: return %ld err '%s'\n",
2501 io, ntds, tot, err);
2503 return 0; /* that's our convention */
2510 epread(Ep *ep, void *a, long count)
2518 ddeprint("ehci: epread\n");
2520 panic("epread: not open");
2532 ddeprint("epread ctl ndata %d\n", cio->ndata);
2534 error("request expected");
2535 else if(cio->ndata == 0){
2539 if(count > cio->ndata)
2542 memmove(a, cio->data, count);
2543 /* BUG for big transfers */
2546 cio->ndata = 0; /* signal EOF next time */
2550 if(ehcidebug>1 || ep->debug){
2551 seprintdata(buf, buf+sizeof(buf), a, count);
2552 print("epread: %s\n", buf);
2559 return epio(ep, &io[OREAD], a, count, 1);
2562 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
2563 if(delta < ep->pollival / 2)
2564 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
2567 return epio(ep, &io[OREAD], a, count, 1);
2570 return episoread(ep, iso, a, count);
2576 * Control transfers are one setup write (data0)
2577 * plus zero or more reads/writes (data1, data0, ...)
2578 * plus a final write/read with data1 to ack.
2579 * For both host to device and device to host we perform
2580 * the entire transfer when the user writes the request,
2581 * and keep any data read from the device for a later read.
2582 * We call epio three times instead of placing all Tds at
2583 * the same time because doing so leads to crc/tmout errors
2585 * Upon errors on the data phase we must still run the status
2586 * phase or the device may cease responding in the future.
2589 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
2594 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
2595 cio, ep->dev->nb, ep->nb, count);
2596 if(count < Rsetuplen)
2597 error("short usb comand");
2610 /* set the address if unset and out of configuration state */
2611 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
2612 if(cio->usbid == 0){
2613 cio->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
2615 qhsetaddr(cio->qh, cio->usbid);
2617 /* adjust maxpkt if the user has learned a different one */
2618 if(qhmaxpkt(cio->qh) != ep->maxpkt)
2619 qhsetmaxpkt(cio->qh, ep->maxpkt);
2621 cio->tok = Tdtoksetup;
2622 cio->toggle = Tddata0;
2624 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
2629 cio->toggle = Tddata1;
2630 if(c[Rtype] & Rd2h){
2632 len = GET2(c+Rcount);
2634 error("bad length in d2h request");
2636 error("d2h data too large to fit in ehci");
2637 a = cio->data = smalloc(len+1);
2639 cio->tok = Tdtokout;
2647 len = epio(ep, cio, a, len, 0);
2650 if(c[Rtype] & Rd2h){
2653 cio->tok = Tdtokout;
2658 count = Rsetuplen + len;
2661 cio->toggle = Tddata1;
2663 epio(ep, cio, nil, 0, 0);
2666 ddeprint("epctlio cio %#p return %ld\n", cio, count);
2671 epwrite(Ep *ep, void *a, long count)
2680 ddeprint("ehci: epwrite ep%d.%d\n", ep->dev->nb, ep->nb);
2682 panic("ehci: epwrite: not open");
2686 return epctlio(ep, cio, a, count);
2691 return epio(ep, &io[OWRITE], a, count, 1);
2694 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
2695 if(delta < ep->pollival)
2696 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
2699 return epio(ep, &io[OWRITE], a, count, 1);
2702 return episowrite(ep, iso, a, count);
2708 isofsinit(Ep *ep, Isoio *iso)
2719 frno = iso->td0frno;
2720 for(i = 0; i < iso->nframes; i++){
2721 td = sitdalloc(ctlr);
2722 td->data = iso->data + i * ep->maxpkt;
2723 td->epc = ep->dev->port << Stdportshift;
2724 td->epc |= ep->dev->hub << Stdhubshift;
2725 td->epc |= (ep->nb&Epmax) << Stdepshift;
2726 td->epc |= (ep->dev->nb&Devmax) << Stddevshift;
2727 td->mfs = 034 << Stdscmshift | 1 << Stdssmshift;
2728 if(ep->mode == OREAD){
2730 td->mdata = ep->maxpkt;
2732 td->mdata = (ep->hz+left) * ep->pollival / 1000;
2733 td->mdata *= ep->samplesz;
2734 left = (ep->hz+left) * ep->pollival % 1000;
2735 if(td->mdata > ep->maxpkt){
2736 print("ehci: ep%d.%d: size > maxpkt\n",
2737 ep->dev->nb, ep->nb);
2738 print("size = %ld max = %ld\n",
2739 td->mdata,ep->maxpkt);
2740 td->mdata = ep->maxpkt;
2743 iso->sitdps[frno] = td;
2744 sitdinit(ctlr, iso, td);
2748 frno = TRUNC(frno+iso->ival, Nisoframes);
2750 iso->stdu = iso->stdi = ltd->next = iso->sitdps[iso->td0frno];
2754 isohsinit(Ep *ep, Isoio *iso)
2764 frno = iso->td0frno;
2765 for(i = 0; i < iso->nframes; i++){
2766 td = itdalloc(ctlr);
2767 td->buffer[0] = (ep->nb&Epmax)<<Itdepshift | (ep->dev->nb&Devmax)<<Itddevshift;
2768 td->buffer[1] = (ep->maxpkt << Itdmaxpktshift) | (ep->mode == OREAD? Itdin: Itdout);
2769 td->buffer[2] = (ep->ntds << Itdntdsshift);
2770 td->data = iso->data + i * 8*iso->maxsize;
2771 if(ep->mode == OREAD)
2772 td->mdata = 8*iso->maxsize;
2774 td->mdata = (ep->hz + left) * ep->pollival / 1000;
2775 td->mdata *= ep->samplesz;
2776 left = (ep->hz + left) * ep->pollival % 1000;
2777 if(td->mdata > 8*iso->maxsize)
2778 td->mdata = 8*iso->maxsize;
2780 iso->itdps[frno] = td;
2781 itdinit(ctlr, iso, td);
2785 frno = TRUNC(frno + iso->ival, Nisoframes);
2787 iso->tdu = iso->tdi = ltd->next = iso->itdps[iso->td0frno];
2791 isoopen(Ctlr *ctlr, Ep *ep)
2793 int tpf; /* tds per frame */
2804 iso->tok = Tdtokout;
2807 error("iso i/o is half-duplex");
2809 iso->usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
2812 iso->debug = ep->debug;
2813 iso->hs = (ep->dev->speed == Highspeed);
2816 iso->ival = (ep->pollival+7)/8;
2819 iso->ival = ep->pollival;
2822 error("bad pollival");
2823 iso->uframes = ep->uframes;
2824 iso->nframes = Nisoframes / iso->ival;
2825 if(iso->nframes < 3)
2826 error("ehci isoopen bug"); /* we need at least 3 tds */
2827 iso->maxsize = ep->ntds * ep->maxpkt;
2828 if(ctlr->load + ep->load > 800)
2829 print("usb: ehci: bandwidth may be exceeded\n");
2831 ctlr->load += ep->load;
2832 ctlr->isoload += ep->load;
2834 dprint("ehci: load %uld isoload %uld\n", ctlr->load, ctlr->isoload);
2835 diprint("iso nframes %d pollival %uld ival %d maxpkt %uld ntds %d\n",
2836 iso->nframes, ep->pollival, iso->ival, ep->maxpkt, ep->ntds);
2839 wakeup(&ctlr->poll);
2842 * From here on this cannot raise errors
2843 * unless we catch them and release here all memory allocated.
2845 iso->tdps = smalloc(sizeof(void*) * Nisoframes);
2846 iso->data = (*ctlr->dmaalloc)(iso->nframes * tpf * iso->maxsize);
2847 iso->td0frno = TRUNC(ctlr->opio->frno + 10, Nisoframes);
2848 /* read: now; write: 1s ahead */
2856 frno = iso->td0frno;
2857 for(i = 0; i < iso->nframes; i++){
2858 *iso->tdps[frno] = ctlr->frames[frno];
2859 frno = TRUNC(frno+iso->ival, Nisoframes);
2864 * Iso uses a virtual frame window of Nisoframes, and we must
2865 * fill the actual ctlr frame array by placing ctlr->nframes/Nisoframes
2866 * copies of the window in the frame array.
2868 assert(ctlr->nframes >= Nisoframes && Nisoframes >= iso->nframes);
2869 assert(Nisoframes >= Nintrleafs);
2870 n = ctlr->nframes / Nisoframes;
2871 for(w = 0; w < n; w++){
2872 frno = iso->td0frno;
2873 woff = w * Nisoframes;
2874 for(i = 0; i < iso->nframes; i++){
2875 assert(woff+frno < ctlr->nframes);
2876 assert(iso->tdps[frno] != nil);
2877 ctlr->frames[woff+frno] = PADDR(iso->tdps[frno]) | (iso->hs? Litd: Lsitd);
2879 frno = TRUNC(frno+iso->ival, Nisoframes);
2883 iso->next = ctlr->iso;
2888 if(ehcidebug > 1 || iso->debug >1)
2893 * Allocate the endpoint and set it up for I/O
2894 * in the controller. This must follow what's said
2895 * in Ep regarding configuration, including perhaps
2896 * the saved toggles (saved on a previous close of
2897 * the endpoint data file by epclose).
2908 deprint("ehci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2910 panic("ehci: epopen called with open ep");
2918 error("endpoint not configured");
2920 ep->aux = smalloc(sizeof(Isoio));
2924 cio = ep->aux = smalloc(sizeof(Ctlio));
2925 cio->debug = ep->debug;
2928 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2930 cio->qh = qhalloc(ctlr, ep, cio, "epc");
2933 ep->pollival = 1; /* assume this; doesn't really matter */
2936 io = ep->aux = smalloc(sizeof(Qio)*2);
2937 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2938 usbid = (ep->nb&Epmax)<<7 | (ep->dev->nb&Devmax);
2939 assert(ep->pollival != 0);
2940 if(ep->mode != OREAD){
2941 if(ep->toggle[OWRITE] != 0)
2942 io[OWRITE].toggle = Tddata1;
2944 io[OWRITE].toggle = Tddata0;
2945 io[OWRITE].tok = Tdtokout;
2946 io[OWRITE].usbid = usbid;
2947 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2948 io[OWRITE].qh = qhalloc(ctlr, ep, io+OWRITE, "epw");
2950 if(ep->mode != OWRITE){
2951 if(ep->toggle[OREAD] != 0)
2952 io[OREAD].toggle = Tddata1;
2954 io[OREAD].toggle = Tddata0;
2955 io[OREAD].tok = Tdtokin;
2956 io[OREAD].usbid = usbid;
2957 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2958 io[OREAD].qh = qhalloc(ctlr, ep, io+OREAD, "epr");
2963 if(ehcidebug>1 || ep->debug)
2965 deprint("ehci: epopen done\n");
2970 cancelio(Ctlr *ctlr, Qio *io)
2976 if(qh == nil || qh->state == Qclose){
2980 dqprint("ehci: cancelio for qh %#p state %s\n",
2981 qh, qhsname[qh->state]);
2987 tsleep(&up->sleep, return0, 0, Abortdelay);
2991 /* wait for epio if running */
3000 cancelisoio(Ctlr *ctlr, Isoio *iso, ulong load)
3002 int frno, i, n, t, w, woff;
3007 if(iso->state == Qclose){
3012 if(iso->state != Qrun && iso->state != Qdone)
3013 panic("bad iso state");
3014 iso->state = Qclose;
3016 if(ctlr->isoload < load)
3017 panic("ehci: low isoload");
3018 ctlr->isoload -= load;
3020 for(il = &ctlr->iso; *il != nil; il = &(*il)->next)
3024 panic("cancleiso: not found");
3028 frno = iso->td0frno;
3029 for(i = 0; i < iso->nframes; i++){
3030 tp = iso->tdps[frno];
3032 Itd *td = iso->itdps[frno];
3033 for(t = 0; t < nelem(td->csw); t++)
3034 td->csw[t] &= ~(Itdioc|Itdactive);
3036 Sitd *std = iso->sitdps[frno];
3037 std->csw &= ~(Stdioc|Stdactive);
3040 for(lp = &ctlr->frames[frno]; !(*lp & Lterm); lp = &LPTR(*lp)[0])
3044 panic("cancelisoio: td not found");
3047 * Iso uses a virtual frame window of Nisoframes, and we must
3048 * restore pointers in copies of the window kept at ctlr->frames.
3050 if(lp == &ctlr->frames[frno]){
3051 n = ctlr->nframes / Nisoframes;
3052 for(w = 1; w < n; w++){
3053 woff = w * Nisoframes;
3054 ctlr->frames[woff+frno] = *lp;
3058 frno = TRUNC(frno+iso->ival, Nisoframes);
3063 * wakeup anyone waiting for I/O and
3064 * wait to be sure no I/O is in progress in the controller.
3065 * and then wait to be sure episo* is no longer running.
3068 diprint("cancelisoio iso %#p waiting for I/O to cease\n", iso);
3070 tsleep(&up->sleep, return0, 0, 5);
3075 diprint("cancelisoio iso %#p releasing iso\n", iso);
3077 frno = iso->td0frno;
3078 for(i = 0; i < iso->nframes; i++){
3080 itdfree(ctlr, iso->itdps[frno]);
3082 sitdfree(ctlr, iso->sitdps[frno]);
3083 iso->tdps[frno] = nil;
3084 frno = TRUNC(frno+iso->ival, Nisoframes);
3088 (*ctlr->dmafree)(iso->data);
3101 deprint("ehci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
3104 panic("ehci: epclose called with closed ep");
3108 cancelio(ctlr, cio);
3115 ep->toggle[OREAD] = ep->toggle[OWRITE] = 0;
3116 if(ep->mode != OWRITE){
3117 cancelio(ctlr, &io[OREAD]);
3118 if(io[OREAD].toggle == Tddata1)
3119 ep->toggle[OREAD] = 1;
3121 if(ep->mode != OREAD){
3122 cancelio(ctlr, &io[OWRITE]);
3123 if(io[OWRITE].toggle == Tddata1)
3124 ep->toggle[OWRITE] = 1;
3130 cancelisoio(ctlr, iso, ep->load);
3133 panic("epclose: bad ttype");
3140 * return smallest power of 2 >= n
3147 for(i = 0; (1 << i) < n; i++)
3153 * build the periodic scheduling tree:
3154 * framesize must be a multiple of the tree size
3157 mkqhtree(Ctlr *ctlr)
3159 int i, n, d, o, leaf0, depth;
3160 ulong leafs[Nintrleafs];
3165 depth = flog2(Nintrleafs);
3166 n = (1 << (depth+1)) - 1;
3167 qt = mallocz(sizeof(*qt), 1);
3169 panic("ehci: mkqhtree: no memory");
3172 qt->bw = mallocz(n * sizeof(qt->bw), 1);
3173 qt->root = tree = mallocz(n * sizeof(Qh *), 1);
3174 if(qt->bw == nil || tree == nil)
3175 panic("ehci: mkqhtree: no memory");
3176 for(i = 0; i < n; i++){
3177 tree[i] = qh = edalloc(ctlr);
3179 panic("ehci: mkqhtree: no memory");
3180 qh->nlink = qh->alink = qh->link = Lterm;
3185 qhlinkqh(tree[i], tree[(i-1)/2]);
3188 dprint("ehci: tree: %d endpoints allocated\n", i);
3190 /* distribute leaves evenly round the frame list */
3192 for(i = 0; i < Nintrleafs; i++){
3194 for(d = 0; d < depth; d++){
3200 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
3203 leafs[i] = PADDR(tree[leaf0 + o]) | Lqh;
3205 assert((ctlr->nframes % Nintrleafs) == 0);
3206 for(i = 0; i < ctlr->nframes; i += Nintrleafs){
3207 memmove(ctlr->frames + i, leafs, sizeof leafs);
3215 ehcimeminit(Ctlr *ctlr)
3220 if(ctlr->tdalloc == nil)
3221 ctlr->tdalloc = xspanalloc;
3222 if(ctlr->dmaalloc == nil)
3223 ctlr->dmaalloc = smalloc;
3224 if(ctlr->dmafree == nil)
3225 ctlr->dmafree = free;
3228 frsize = ctlr->nframes * sizeof(ulong);
3229 assert((frsize & 0xFFF) == 0); /* must be 4k aligned */
3230 ctlr->frames = (*ctlr->tdalloc)(frsize, frsize, 0);
3231 if(ctlr->frames == nil)
3232 panic("ehci reset: no memory");
3234 for (i = 0; i < ctlr->nframes; i++)
3235 ctlr->frames[i] = Lterm;
3236 opio->frbase = PADDR(ctlr->frames);
3240 qhalloc(ctlr, nil, nil, nil); /* init async list */
3241 mkqhtree(ctlr); /* init sync list */
3242 edfree(edalloc(ctlr)); /* try to get some ones pre-allocated */
3244 dprint("ehci %#p flb %#lux frno %#lux\n",
3245 ctlr->capio, opio->frbase, opio->frno);
3259 dprint("ehci %#p init\n", ctlr->capio);
3263 * Unless we activate frroll interrupt
3264 * some machines won't post other interrupts.
3266 opio->intr = Iusb|Ierr|Iportchg|Ihcerr|Iasync;
3274 /* route all ports to us */
3275 opio->config = Callmine;
3278 for (i = 0; i < hp->nports; i++)
3279 opio->portsc[i] = Pspower;
3287 ehcilinkage(Hci *hp)
3291 hp->interrupt = interrupt;
3292 hp->epopen = epopen;
3293 hp->epclose = epclose;
3294 hp->epread = epread;
3295 hp->epwrite = epwrite;
3296 hp->seprintep = seprintep;
3297 hp->portenable = portenable;
3298 hp->portreset = portreset;
3299 hp->portstatus = portstatus;
3300 // hp->shutdown = shutdown;
3301 // hp->debug = setdebug;