1 /*
2 * USB Open Host Controller Interface (Ohci) driver
3 *
4 * BUGS:
5 * - Missing isochronous input streams.
6 * - Too many delays and ilocks.
7 * - bandwidth admission control must be done per-frame.
8 * - Buffering could be handled like in uhci, to avoid
9 * needed block allocation and avoid allocs for small Tds.
10 * - must warn of power overruns.
11 */
12
13 #include "u.h"
14 #include "../port/lib.h"
15 #include "mem.h"
16 #include "dat.h"
17 #include "fns.h"
18 #include "io.h"
19 #include "../port/error.h"
20
21 #include "../port/usb.h"
22
23 typedef struct Ctlio Ctlio;
24 typedef struct Ctlr Ctlr;
25 typedef struct Ed Ed;
26 typedef struct Edpool Edpool;
27 typedef struct Epx Epx;
28 typedef struct Hcca Hcca;
29 typedef struct Isoio Isoio;
30 typedef struct Ohci Ohci;
31 typedef struct Qio Qio;
32 typedef struct Qtree Qtree;
33 typedef struct Td Td;
34 typedef struct Tdpool Tdpool;
35
36 enum
37 {
38 Incr = 64, /* for Td and Ed pools */
39
40 Align = 0x20, /* OHCI only requires 0x10 */
41 /* use always a power of 2 */
42
43 Abortdelay = 1, /* delay after cancelling Tds (ms) */
44 Tdatomic = 8, /* max nb. of Tds per bulk I/O op. */
45 Enabledelay = 100, /* waiting for a port to enable */
46
47
48 /* Queue states (software) */
49 Qidle = 0,
50 Qinstall,
51 Qrun,
52 Qdone,
53 Qclose,
54 Qfree,
55
56 /* Ed control bits */
57 Edmpsmask = 0x7ff, /* max packet size */
58 Edmpsshift = 16,
59 Edlow = 1 << 13, /* low speed */
60 Edskip = 1 << 14, /* skip this ed */
61 Ediso = 1 << 15, /* iso Tds used */
62 Edtddir = 0, /* get dir from td */
63 Edin = 2 << 11, /* direction in */
64 Edout = 1 << 11, /* direction out */
65 Eddirmask = 3 << 11, /* direction bits */
66 Edhalt = 1, /* halted (in head ptr) */
67 Edtoggle = 2, /* toggle (in head ptr) 1 == data1 */
68
69 /* Td control bits */
70 Tdround = 1<<18, /* (rounding) short packets ok */
71 Tdtoksetup = 0<<19, /* setup packet */
72 Tdtokin = 2<<19, /* in packet */
73 Tdtokout = 1<<19, /* out packet */
74 Tdtokmask = 3<<19, /* in/out/setup bits */
75 Tdnoioc = 7<<21, /* intr. cnt. value for no interrupt */
76 Tdusetog = 1<<25, /* use toggle from Td (1) or Ed (0) */
77 Tddata1 = 1<<24, /* data toggle (1 == data1) */
78 Tddata0 = 0<<24,
79 Tdfcmask = 7, /* frame count (iso) */
80 Tdfcshift = 24,
81 Tdsfmask = 0xFFFF, /* starting frame (iso) */
82 Tderrmask = 3, /* error counter */
83 Tderrshift = 26,
84 Tdccmask = 0xf, /* condition code (status) */
85 Tdccshift = 28,
86 Tdiccmask = 0xf, /* condition code (iso, offsets) */
87 Tdiccshift = 12,
88
89 Ntdframes = 0x10000, /* # of different iso frame numbers */
90
91 /* Td errors (condition code) */
92 Tdok = 0,
93 Tdcrc = 1,
94 Tdbitstuff = 2,
95 Tdbadtog = 3,
96 Tdstalled = 4,
97 Tdtmout = 5,
98 Tdpidchk = 6,
99 Tdbadpid = 7,
100 Tddataovr = 8,
101 Tddataund = 9,
102 Tdbufovr = 0xC,
103 Tdbufund = 0xD,
104 Tdnotacc = 0xE,
105
106 /* control register */
107 Cple = 0x04, /* periodic list enable */
108 Cie = 0x08, /* iso. list enable */
109 Ccle = 0x10, /* ctl list enable */
110 Cble = 0x20, /* bulk list enable */
111 Cfsmask = 3 << 6, /* functional state... */
112 Cfsreset = 0 << 6,
113 Cfsresume = 1 << 6,
114 Cfsoper = 2 << 6,
115 Cfssuspend = 3 << 6,
116
117 /* command status */
118 Sblf = 1 << 2, /* bulk list (load) flag */
119 Sclf = 1 << 1, /* control list (load) flag */
120 Shcr = 1 << 0, /* host controller reset */
121
122 /* intr enable */
123 Mie = 1 << 31,
124 Oc = 1 << 30,
125 Rhsc = 1 << 6,
126 Fno = 1 << 5,
127 Ue = 1 << 4,
128 Rd = 1 << 3,
129 Sf = 1 << 2,
130 Wdh = 1 << 1,
131 So = 1 << 0,
132
133 Fmaxpktmask = 0x7fff,
134 Fmaxpktshift = 16,
135 HcRhDescA_POTPGT_MASK = 0xff << 24,
136 HcRhDescA_POTPGT_SHIFT = 24,
137
138 /* Rh status */
139 Lps = 1 << 0,
140 Cgp = 1 << 0,
141 Oci = 1 << 1,
142 Psm = 1 << 8,
143 Nps = 1 << 9,
144 Drwe = 1 << 15,
145 Srwe = 1 << 15,
146 Lpsc = 1 << 16,
147 Ccic = 1 << 17,
148 Crwe = 1 << 31,
149
150 /* port status */
151 Ccs = 0x00001, /* current connect status */
152 Pes = 0x00002, /* port enable status */
153 Pss = 0x00004, /* port suspend status */
154 Poci = 0x00008, /* over current indicator */
155 Prs = 0x00010, /* port reset status */
156 Pps = 0x00100, /* port power status */
157 Lsda = 0x00200, /* low speed device attached */
158 Csc = 0x10000, /* connect status change */
159 Pesc = 0x20000, /* enable status change */
160 Pssc = 0x40000, /* suspend status change */
161 Ocic = 0x80000, /* over current ind. change */
162 Prsc = 0x100000, /* reset status change */
163
164 /* port status write bits */
165 Cpe = 0x001, /* clear port enable */
166 Spe = 0x002, /* set port enable */
167 Spr = 0x010, /* set port reset */
168 Spp = 0x100, /* set port power */
169 Cpp = 0x200, /* clear port power */
170
171 };
172
173 /*
174 * Endpoint descriptor. (first 4 words used by hardware)
175 */
176 struct Ed {
177 ulong ctrl;
178 ulong tail; /* transfer descriptor */
179 ulong head;
180 ulong nexted;
181
182 Ed* next; /* sw; in free list or next in list */
183 Td* tds; /* in use by current xfer; all for iso */
184 Ep* ep; /* debug/align */
185 Ed* inext; /* debug/align (dump interrupt eds). */
186 };
187
188 /*
189 * Endpoint I/O state (software), per direction.
190 */
191 struct Qio
192 {
193 QLock; /* for the entire I/O process */
194 Rendez; /* wait for completion */
195 Ed* ed; /* to place Tds on it */
196 int sched; /* queue number (intr/iso) */
197 int toggle; /* Tddata0/Tddata1 */
198 ulong usbid; /* device/endpoint address */
199 int tok; /* Tdsetup, Tdtokin, Tdtokout */
200 long iotime; /* last I/O time; to hold interrupt polls */
201 int debug; /* for the endpoint */
202 char* err; /* error status */
203 int state; /* Qidle -> Qinstall -> Qrun -> Qdone | Qclose */
204 long bw; /* load (intr/iso) */
205 };
206
207 struct Ctlio
208 {
209 Qio; /* single Ed for all transfers */
210 uchar* data; /* read from last ctl req. */
211 int ndata; /* number of bytes read */
212 };
213
214 struct Isoio
215 {
216 Qio;
217 int nframes; /* number of frames for a full second */
218 Td* atds; /* Tds avail for further I/O */
219 int navail; /* number of avail Tds */
220 ulong frno; /* next frame number avail for I/O */
221 ulong left; /* remainder after rounding Hz to samples/ms */
222 int nerrs; /* consecutive errors on iso I/O */
223 };
224
225 /*
226 * Transfer descriptor. Size must be multiple of 32
227 * First block is used by hardware (aligned to 32).
228 */
229 struct Td
230 {
231 ulong ctrl;
232 ulong cbp; /* current buffer pointer */
233 ulong nexttd;
234 ulong be;
235 ushort offsets[8]; /* used by Iso Tds only */
236
237 Td* next; /* in free or Ed tds list */
238 Td* anext; /* in avail td list (iso) */
239 Ep* ep; /* using this Td for I/O */
240 Qio* io; /* using this Td for I/O */
241 Block* bp; /* data for this Td */
242 ulong nbytes; /* bytes in this Td */
243 ulong cbp0; /* initial value for cbp */
244 ulong last; /* true for last Td in Qio */
245 };
246
247 /*
248 * Host controller communication area (hardware)
249 */
250 struct Hcca
251 {
252 ulong intrtable[32];
253 ushort framenumber;
254 ushort pad1;
255 ulong donehead;
256 uchar reserved[116];
257 };
258
259 /*
260 * I/O registers
261 */
262 struct Ohci
263 {
264 /* control and status group */
265 ulong revision; /*00*/
266 ulong control; /*04*/
267 ulong cmdsts; /*08*/
268 ulong intrsts; /*0c*/
269 ulong intrenable; /*10*/
270 ulong intrdisable; /*14*/
271
272 /* memory pointer group */
273 ulong hcca; /*18*/
274 ulong periodcurred; /*1c*/
275 ulong ctlheaded; /*20*/
276 ulong ctlcurred; /*24*/
277 ulong bulkheaded; /*28*/
278 ulong bulkcurred; /*2c*/
279 ulong donehead; /*30*/
280
281 /* frame counter group */
282 ulong fminterval; /*34*/
283 ulong fmremaining; /*38*/
284 ulong fmnumber; /*3c*/
285 ulong periodicstart; /*40*/
286 ulong lsthreshold; /*44*/
287
288 /* root hub group */
289 ulong rhdesca; /*48*/
290 ulong rhdescb; /*4c*/
291 ulong rhsts; /*50*/
292 ulong rhportsts[15]; /*54*/
293 ulong pad25[20]; /*90*/
294
295 /* unknown */
296 ulong hostueaddr; /*e0*/
297 ulong hostuests; /*e4*/
298 ulong hosttimeoutctrl; /*e8*/
299 ulong pad59; /*ec*/
300 ulong pad60; /*f0*/
301 ulong hostrevision; /*f4*/
302 ulong pad62[2];
303 /*100*/
304 };
305
306 /*
307 * Endpoint tree (software)
308 */
309 struct Qtree
310 {
311 int nel;
312 int depth;
313 ulong* bw;
314 Ed** root;
315 };
316
317 struct Tdpool
318 {
319 Lock;
320 Td* free;
321 int nalloc;
322 int ninuse;
323 int nfree;
324 };
325
326 struct Edpool
327 {
328 Lock;
329 Ed* free;
330 int nalloc;
331 int ninuse;
332 int nfree;
333 };
334
335 struct Ctlr
336 {
337 Lock; /* for ilock; lists and basic ctlr I/O */
338 QLock resetl; /* lock controller during USB reset */
339 int active;
340 Ctlr* next;
341 int nports;
342
343 Ohci* ohci; /* base I/O address */
344 Hcca* hcca; /* intr/done Td lists (used by hardware) */
345 int overrun; /* sched. overrun */
346 Ed* intrhd; /* list of intr. eds in tree */
347 Qtree* tree; /* tree for t Ep i/o */
348 int ntree; /* number of dummy Eds in tree */
349 Pcidev* pcidev;
350 };
351
352 #define dqprint if(debug || io && io->debug)print
353 #define ddqprint if(debug>1 || (io && io->debug>1))print
354 #define diprint if(debug || iso && iso->debug)print
355 #define ddiprint if(debug>1 || (iso && iso->debug>1))print
356 #define TRUNC(x, sz) ((x) & ((sz)-1))
357
358 static int ohciinterrupts[Nttypes];
359 static char* iosname[] = { "idle", "install", "run", "done", "close", "FREE" };
360
361 static int debug;
362 static Edpool edpool;
363 static Tdpool tdpool;
364 static Ctlr* ctlrs[Nhcis];
365
366 static QLock usbhstate; /* protects name space state */
367
368 static int schedendpt(Ctlr *ub, Ep *ep);
369 static void unschedendpt(Ctlr *ub, Ep *ep);
370 static long qtd(Ctlr*, Ep*, int, Block*, uchar*, uchar*, int, ulong);
371
372 static char* errmsgs[] =
373 {
374 [Tdcrc] "crc error",
375 [Tdbitstuff] "bit stuffing error",
376 [Tdbadtog] "bad toggle",
377 [Tdstalled] Estalled,
378 [Tdtmout] "timeout error",
379 [Tdpidchk] "pid check error",
380 [Tdbadpid] "bad pid",
381 [Tddataovr] "data overrun",
382 [Tddataund] "data underrun",
383 [Tdbufovr] "buffer overrun",
384 [Tdbufund] "buffer underrun",
385 [Tdnotacc] "not accessed"
386 };
387
388 static void*
pa2ptr(ulong pa)389 pa2ptr(ulong pa)
390 {
391 if(pa == 0)
392 return nil;
393 else
394 return KSEG1ADDR(pa);
395 }
396
397 static ulong
ptr2pa(void * p)398 ptr2pa(void *p)
399 {
400 if(p == nil)
401 return 0;
402 else
403 return PCIWADDR(p); // XXX
404 }
405
406 static void
waitSOF(Ctlr * ub)407 waitSOF(Ctlr *ub)
408 {
409 int frame = ub->hcca->framenumber & 0x3f;
410
411 do {
412 delay(2);
413 } while(frame == (ub->hcca->framenumber & 0x3f));
414 }
415
416 static char*
errmsg(int err)417 errmsg(int err)
418 {
419
420 if(err < nelem(errmsgs))
421 return errmsgs[err];
422 return nil;
423 }
424
425 static Ed*
ctlhd(Ctlr * ctlr)426 ctlhd(Ctlr *ctlr)
427 {
428 return pa2ptr(ctlr->ohci->ctlheaded);
429 }
430
431 static Ed*
bulkhd(Ctlr * ctlr)432 bulkhd(Ctlr *ctlr)
433 {
434 return pa2ptr(ctlr->ohci->bulkheaded);
435 }
436
437 static void
edlinked(Ed * ed,Ed * next)438 edlinked(Ed *ed, Ed *next)
439 {
440 if(ed == nil)
441 print("edlinked: nil ed: pc %#p\n", getcallerpc(&ed));
442 ed->nexted = ptr2pa(next);
443 ed->next = next;
444 }
445
446 static void
setctlhd(Ctlr * ctlr,Ed * ed)447 setctlhd(Ctlr *ctlr, Ed *ed)
448 {
449 ctlr->ohci->ctlheaded = ptr2pa(ed);
450 if(ed != nil)
451 ctlr->ohci->cmdsts |= Sclf; /* reload it on next pass */
452 }
453
454 static void
setbulkhd(Ctlr * ctlr,Ed * ed)455 setbulkhd(Ctlr *ctlr, Ed *ed)
456 {
457 ctlr->ohci->bulkheaded = ptr2pa(ed);
458 if(ed != nil)
459 ctlr->ohci->cmdsts |= Sblf; /* reload it on next pass */
460 }
461
462 static void
unlinkctl(Ctlr * ctlr,Ed * ed)463 unlinkctl(Ctlr *ctlr, Ed *ed)
464 {
465 Ed *this, *prev, *next;
466
467 ctlr->ohci->control &= ~Ccle;
468 waitSOF(ctlr);
469 this = ctlhd(ctlr);
470 ctlr->ohci->ctlcurred = 0;
471 prev = nil;
472 while(this != nil && this != ed){
473 prev = this;
474 this = this->next;
475 }
476 if(this == nil){
477 print("unlinkctl: not found\n");
478 return;
479 }
480 next = this->next;
481 if(prev == nil)
482 setctlhd(ctlr, next);
483 else
484 edlinked(prev, next);
485 ctlr->ohci->control |= Ccle;
486 edlinked(ed, nil); /* wipe out next field */
487 }
488
489 static void
unlinkbulk(Ctlr * ctlr,Ed * ed)490 unlinkbulk(Ctlr *ctlr, Ed *ed)
491 {
492 Ed *this, *prev, *next;
493
494 ctlr->ohci->control &= ~Cble;
495 waitSOF(ctlr);
496 this = bulkhd(ctlr);
497 ctlr->ohci->bulkcurred = 0;
498 prev = nil;
499 while(this != nil && this != ed){
500 prev = this;
501 this = this->next;
502 }
503 if(this == nil){
504 print("unlinkbulk: not found\n");
505 return;
506 }
507 next = this->next;
508 if(prev == nil)
509 setbulkhd(ctlr, next);
510 else
511 edlinked(prev, next);
512 ctlr->ohci->control |= Cble;
513 edlinked(ed, nil); /* wipe out next field */
514 }
515
516 static void
edsetaddr(Ed * ed,ulong addr)517 edsetaddr(Ed *ed, ulong addr)
518 {
519 ulong ctrl;
520
521 ctrl = ed->ctrl & ~((Epmax<<7)|Devmax);
522 ctrl |= (addr & ((Epmax<<7)|Devmax));
523 ed->ctrl = ctrl;
524 }
525
526 static void
edsettog(Ed * ed,int c)527 edsettog(Ed *ed, int c)
528 {
529 if(c != 0)
530 ed->head |= Edtoggle;
531 else
532 ed->head &= ~Edtoggle;
533 }
534
535 static int
edtoggle(Ed * ed)536 edtoggle(Ed *ed)
537 {
538 return ed->head & Edtoggle;
539 }
540
541 static int
edhalted(Ed * ed)542 edhalted(Ed *ed)
543 {
544 return ed->head & Edhalt;
545 }
546
547 static int
edmaxpkt(Ed * ed)548 edmaxpkt(Ed *ed)
549 {
550 return (ed->ctrl >> Edmpsshift) & Edmpsmask;
551 }
552
553 static void
edsetmaxpkt(Ed * ed,int m)554 edsetmaxpkt(Ed *ed, int m)
555 {
556 ulong c;
557
558 c = ed->ctrl & ~(Edmpsmask << Edmpsshift);
559 ed->ctrl = c | ((m&Edmpsmask) << Edmpsshift);
560 }
561
562 static int
tderrs(Td * td)563 tderrs(Td *td)
564 {
565 return (td->ctrl >> Tdccshift) & Tdccmask;
566 }
567
568 static int
tdtok(Td * td)569 tdtok(Td *td)
570 {
571 return (td->ctrl & Tdtokmask);
572 }
573
574 static Td*
tdalloc(void)575 tdalloc(void)
576 {
577 Td *td;
578 Td *pool;
579 int i;
580
581 lock(&tdpool);
582 if(tdpool.free == nil){
583 ddprint("ohci: tdalloc %d Tds\n", Incr);
584 pool = xspanalloc(Incr*sizeof(Td), Align, 0);
585 if(pool == nil)
586 panic("tdalloc");
587 pool = KSEG1ADDR(pool); // XXX
588 for(i=Incr; --i>=0;){
589 pool[i].next = tdpool.free;
590 tdpool.free = &pool[i];
591 }
592 tdpool.nalloc += Incr;
593 tdpool.nfree += Incr;
594 }
595 tdpool.ninuse++;
596 tdpool.nfree--;
597 td = tdpool.free;
598 tdpool.free = td->next;
599 memset(td, 0, sizeof(Td));
600 unlock(&tdpool);
601
602 assert(((uintptr)td & 0xF) == 0);
603 return td;
604 }
605
606 static void
tdfree(Td * td)607 tdfree(Td *td)
608 {
609 if(td == 0)
610 return;
611 freeb(td->bp);
612 td->bp = nil;
613 lock(&tdpool);
614 if(td->nexttd == 0x77777777)
615 panic("ohci: tdfree: double free");
616 memset(td, 7, sizeof(Td)); /* poison */
617 td->next = tdpool.free;
618 tdpool.free = td;
619 tdpool.ninuse--;
620 tdpool.nfree++;
621 unlock(&tdpool);
622 }
623
624 static Ed*
edalloc(void)625 edalloc(void)
626 {
627 Ed *ed, *pool;
628 int i;
629
630 lock(&edpool);
631 if(edpool.free == nil){
632 ddprint("ohci: edalloc %d Eds\n", Incr);
633 pool = xspanalloc(Incr*sizeof(Ed), Align, 0);
634 if(pool == nil)
635 panic("edalloc");
636 pool = KSEG1ADDR(pool); // XXX
637 for(i=Incr; --i>=0;){
638 pool[i].next = edpool.free;
639 edpool.free = &pool[i];
640 }
641 edpool.nalloc += Incr;
642 edpool.nfree += Incr;
643 }
644 edpool.ninuse++;
645 edpool.nfree--;
646 ed = edpool.free;
647 edpool.free = ed->next;
648 memset(ed, 0, sizeof(Ed));
649 unlock(&edpool);
650
651 return ed;
652 }
653
654 static void
edfree(Ed * ed)655 edfree(Ed *ed)
656 {
657 Td *td, *next;
658 int i;
659
660 if(ed == 0)
661 return;
662 i = 0;
663 for(td = ed->tds; td != nil; td = next){
664 next = td->next;
665 tdfree(td);
666 if(i++ > 2000){
667 print("ohci: bug: ed with more than 2000 tds\n");
668 break;
669 }
670 }
671 lock(&edpool);
672 if(ed->nexted == 0x99999999)
673 panic("ohci: edfree: double free");
674 memset(ed, 9, sizeof(Ed)); /* poison */
675 ed->next = edpool.free;
676 edpool.free = ed;
677 edpool.ninuse--;
678 edpool.nfree++;
679 unlock(&edpool);
680 ddprint("edfree: ed %#p\n", ed);
681 }
682
683 /*
684 * return smallest power of 2 >= n
685 */
686 static int
flog2(int n)687 flog2(int n)
688 {
689 int i;
690
691 for(i = 0; (1 << i) < n; i++)
692 ;
693 return i;
694 }
695
696 /*
697 * return smallest power of 2 <= n
698 */
699 static int
flog2lower(int n)700 flog2lower(int n)
701 {
702 int i;
703
704 for(i = 0; (1 << (i + 1)) <= n; i++)
705 ;
706 return i;
707 }
708
709 static int
pickschedq(Qtree * qt,int pollival,ulong bw,ulong limit)710 pickschedq(Qtree *qt, int pollival, ulong bw, ulong limit)
711 {
712 int i, j, d, upperb, q;
713 ulong best, worst, total;
714
715 d = flog2lower(pollival);
716 if(d > qt->depth)
717 d = qt->depth;
718 q = -1;
719 worst = 0;
720 best = ~0;
721 upperb = (1 << (d+1)) - 1;
722 for(i = (1 << d) - 1; i < upperb; i++){
723 total = qt->bw[0];
724 for(j = i; j > 0; j = (j - 1) / 2)
725 total += qt->bw[j];
726 if(total < best){
727 best = total;
728 q = i;
729 }
730 if(total > worst)
731 worst = total;
732 }
733 if(worst + bw >= limit)
734 return -1;
735 return q;
736 }
737
738 static int
schedq(Ctlr * ctlr,Qio * io,int pollival)739 schedq(Ctlr *ctlr, Qio *io, int pollival)
740 {
741 int q;
742 Ed *ted;
743
744 q = pickschedq(ctlr->tree, pollival, io->bw, ~0);
745 ddqprint("ohci: sched %#p q %d, ival %d, bw %ld\n", io, q, pollival, io->bw);
746 if(q < 0){
747 print("ohci: no room for ed\n");
748 return -1;
749 }
750 ctlr->tree->bw[q] += io->bw;
751 ted = ctlr->tree->root[q];
752 io->sched = q;
753 edlinked(io->ed, ted->next);
754 edlinked(ted, io->ed);
755 io->ed->inext = ctlr->intrhd;
756 ctlr->intrhd = io->ed;
757 return 0;
758 }
759
760 static void
unschedq(Ctlr * ctlr,Qio * qio)761 unschedq(Ctlr *ctlr, Qio *qio)
762 {
763 int q;
764 Ed *prev, *this, *next;
765 Ed **l;
766
767 q = qio->sched;
768 if(q < 0)
769 return;
770 ctlr->tree->bw[q] -= qio->bw;
771
772 prev = ctlr->tree->root[q];
773 this = prev->next;
774 while(this != nil && this != qio->ed){
775 prev = this;
776 this = this->next;
777 }
778 if(this == nil)
779 print("ohci: unschedq %d: not found\n", q);
780 else{
781 next = this->next;
782 edlinked(prev, next);
783 }
784 waitSOF(ctlr);
785 for(l = &ctlr->intrhd; *l != nil; l = &(*l)->inext)
786 if(*l == qio->ed){
787 *l = (*l)->inext;
788 return;
789 }
790 print("ohci: unschedq: ed %#p not found\n", qio->ed);
791 }
792
793 static char*
seprinttdtok(char * s,char * e,int tok)794 seprinttdtok(char *s, char *e, int tok)
795 {
796 switch(tok){
797 case Tdtoksetup:
798 s = seprint(s, e, " setup");
799 break;
800 case Tdtokin:
801 s = seprint(s, e, " in");
802 break;
803 case Tdtokout:
804 s = seprint(s, e, " out");
805 break;
806 }
807 return s;
808 }
809
810
811 static char*
seprinttd(char * s,char * e,Td * td,int iso)812 seprinttd(char *s, char *e, Td *td, int iso)
813 {
814 int i;
815 Block *bp;
816
817 if(td == nil)
818 return seprint(s, e, "<nil td>\n");
819 s = seprint(s, e, "%#p ep %#p ctrl %#p", td, td->ep, td->ctrl);
820 s = seprint(s, e, " cc=%#ulx", (td->ctrl >> Tdccshift) & Tdccmask);
821 if(iso == 0){
822 if((td->ctrl & Tdround) != 0)
823 s = seprint(s, e, " rnd");
824 s = seprinttdtok(s, e, td->ctrl & Tdtokmask);
825 if((td->ctrl & Tdusetog) != 0)
826 s = seprint(s, e, " d%d", (td->ctrl & Tddata1) ? 1 : 0);
827 else
828 s = seprint(s, e, " d-");
829 s = seprint(s, e, " ec=%uld", (td->ctrl >> Tderrshift) & Tderrmask);
830 }else{
831 s = seprint(s, e, " fc=%uld", (td->ctrl >> Tdfcshift) & Tdfcmask);
832 s = seprint(s, e, " sf=%uld", td->ctrl & Tdsfmask);
833 }
834 s = seprint(s, e, " cbp0 %#p cbp %#p next %#p be %#p %s",
835 td->cbp0, td->cbp, td->nexttd, td->be, td->last ? "last" : "");
836 s = seprint(s, e, "\n\t\t%ld bytes", td->nbytes);
837 if((bp = td->bp) != nil){
838 s = seprint(s, e, " rp %#p wp %#p ", bp->rp, bp->wp);
839 if(BLEN(bp) > 0)
840 s = seprintdata(s, e, bp->rp, bp->wp - bp->rp);
841 }
842 if(iso == 0)
843 return seprint(s, e, "\n");
844 s = seprint(s, e, "\n\t\t");
845 /* we use only offsets[0] */
846 i = 0;
847 s = seprint(s, e, "[%d] %#ux cc=%#ux sz=%ud\n", i, td->offsets[i],
848 (td->offsets[i] >> Tdiccshift) & Tdiccmask,
849 td->offsets[i] & 0x7FF);
850 return s;
851 }
852
853 static void
dumptd(Td * td,char * p,int iso)854 dumptd(Td *td, char *p, int iso)
855 {
856 static char buf[512]; /* Too much */
857 char *s;
858
859 s = seprint(buf, buf+sizeof(buf), "%s: ", p);
860 s = seprinttd(s, buf+sizeof(buf), td, iso);
861 if(s > buf && s[-1] != '\n')
862 s[-1] = '\n';
863 print("\t%s", buf);
864 }
865
866 static void
dumptds(Td * td,char * p,int iso)867 dumptds(Td *td, char *p, int iso)
868 {
869 int i;
870
871 for(i = 0; td != nil; td = td->next){
872 dumptd(td, p, iso);
873 if(td->last)
874 break;
875 if(tdtok(td) == Tdtokin && ++i > 2){
876 print("\t\t...\n");
877 break;
878 }
879 }
880 }
881
882 static void
dumped(Ed * ed)883 dumped(Ed *ed)
884 {
885 char *buf, *s, *e;
886
887 if(ed == nil){
888 print("<null ed>\n");
889 return;
890 }
891 buf = malloc(512);
892 /* no waserror; may want to use from interrupt context */
893 if(buf == nil)
894 return;
895 e = buf+512;
896 s = seprint(buf, e, "\ted %#p: ctrl %#p", ed, ed->ctrl);
897 if((ed->ctrl & Edskip) != 0)
898 s = seprint(s, e, " skip");
899 if((ed->ctrl & Ediso) != 0)
900 s = seprint(s, e, " iso");
901 if((ed->ctrl & Edlow) != 0)
902 s = seprint(s, e, " low");
903 s = seprint(s, e, " d%d", (ed->head & Edtoggle) ? 1 : 0);
904 if((ed->ctrl & Eddirmask) == Edin)
905 s = seprint(s, e, " in");
906 if((ed->ctrl & Eddirmask) == Edout)
907 s = seprint(s, e, " out");
908 if(edhalted(ed))
909 s = seprint(s, e, " hlt");
910 s = seprint(s, e, " ep%uld.%uld", (ed->ctrl>>7)&Epmax, ed->ctrl&0x7f);
911 s = seprint(s, e, " maxpkt %uld", (ed->ctrl>>Edmpsshift)&Edmpsmask);
912 seprint(s, e, " tail %#p head %#p next %#p\n",ed->tail,ed->head,ed->nexted);
913 print("%s", buf);
914 free(buf);
915 if(ed->tds != nil && (ed->ctrl & Ediso) == 0)
916 dumptds(ed->tds, "td", 0);
917 }
918
919 static char*
seprintio(char * s,char * e,Qio * io,char * pref)920 seprintio(char *s, char *e, Qio *io, char *pref)
921 {
922 s = seprint(s, e, "%s qio %#p ed %#p", pref, io, io->ed);
923 s = seprint(s, e, " tog %d iot %ld err %s id %#ulx",
924 io->toggle, io->iotime, io->err, io->usbid);
925 s = seprinttdtok(s, e, io->tok);
926 s = seprint(s, e, " %s\n", iosname[io->state]);
927 return s;
928 }
929
930 static char*
seprintep(char * s,char * e,Ep * ep)931 seprintep(char* s, char* e, Ep *ep)
932 {
933 Isoio *iso;
934 Qio *io;
935 Ctlio *cio;
936
937 if(ep == nil)
938 return seprint(s, e, "<nil ep>\n");
939 if(ep->aux == nil)
940 return seprint(s, e, "no mdep\n");
941 switch(ep->ttype){
942 case Tctl:
943 cio = ep->aux;
944 s = seprintio(s, e, cio, "c");
945 s = seprint(s, e, "\trepl %d ndata %d\n", ep->rhrepl, cio->ndata);
946 break;
947 case Tbulk:
948 case Tintr:
949 io = ep->aux;
950 if(ep->mode != OWRITE)
951 s = seprintio(s, e, &io[OREAD], "r");
952 if(ep->mode != OREAD)
953 s = seprintio(s, e, &io[OWRITE], "w");
954 break;
955 case Tiso:
956 iso = ep->aux;
957 s = seprintio(s, e, iso, "w");
958 s = seprint(s, e, "\tntds %d avail %d frno %uld left %uld next avail %#p\n",
959 iso->nframes, iso->navail, iso->frno, iso->left, iso->atds);
960 break;
961 }
962 return s;
963 }
964
965 static char*
seprintctl(char * s,char * se,ulong ctl)966 seprintctl(char *s, char *se, ulong ctl)
967 {
968 s = seprint(s, se, "en=");
969 if((ctl&Cple) != 0)
970 s = seprint(s, se, "p");
971 if((ctl&Cie) != 0)
972 s = seprint(s, se, "i");
973 if((ctl&Ccle) != 0)
974 s = seprint(s, se, "c");
975 if((ctl&Cble) != 0)
976 s = seprint(s, se, "b");
977 switch(ctl & Cfsmask){
978 case Cfsreset:
979 return seprint(s, se, " reset");
980 case Cfsresume:
981 return seprint(s, se, " resume");
982 case Cfsoper:
983 return seprint(s, se, " run");
984 case Cfssuspend:
985 return seprint(s, se, " suspend");
986 default:
987 return seprint(s, se, " ???");
988 }
989 }
990
991 static void
dump(Hci * hp)992 dump(Hci *hp)
993 {
994 Ctlr *ctlr;
995 Ed *ed;
996 char cs[20];
997
998 ctlr = hp->aux;
999 ilock(ctlr);
1000 seprintctl(cs, cs+sizeof(cs), ctlr->ohci->control);
1001 print("ohci ctlr %#p: frno %#ux ctl %#lux %s sts %#lux intr %#lux\n",
1002 ctlr, ctlr->hcca->framenumber, ctlr->ohci->control, cs,
1003 ctlr->ohci->cmdsts, ctlr->ohci->intrsts);
1004 print("ctlhd %#ulx cur %#ulx bulkhd %#ulx cur %#ulx done %#ulx\n",
1005 ctlr->ohci->ctlheaded, ctlr->ohci->ctlcurred,
1006 ctlr->ohci->bulkheaded, ctlr->ohci->bulkcurred,
1007 ctlr->ohci->donehead);
1008 if(ctlhd(ctlr) != nil)
1009 print("[ctl]\n");
1010 for(ed = ctlhd(ctlr); ed != nil; ed = ed->next)
1011 dumped(ed);
1012 if(bulkhd(ctlr) != nil)
1013 print("[bulk]\n");
1014 for(ed = bulkhd(ctlr); ed != nil; ed = ed->next)
1015 dumped(ed);
1016 if(ctlr->intrhd != nil)
1017 print("[intr]\n");
1018 for(ed = ctlr->intrhd; ed != nil; ed = ed->inext)
1019 dumped(ed);
1020 if(ctlr->tree->root[0]->next != nil)
1021 print("[iso]");
1022 for(ed = ctlr->tree->root[0]->next; ed != nil; ed = ed->next)
1023 dumped(ed);
1024 print("%d eds in tree\n", ctlr->ntree);
1025 iunlock(ctlr);
1026 lock(&tdpool);
1027 print("%d tds allocated = %d in use + %d free\n",
1028 tdpool.nalloc, tdpool.ninuse, tdpool.nfree);
1029 unlock(&tdpool);
1030 lock(&edpool);
1031 print("%d eds allocated = %d in use + %d free\n",
1032 edpool.nalloc, edpool.ninuse, edpool.nfree);
1033 unlock(&edpool);
1034 }
1035
1036 /*
1037 * Compute size for the next iso Td and setup its
1038 * descriptor for I/O according to the buffer size.
1039 */
1040 static void
isodtdinit(Ep * ep,Isoio * iso,Td * td)1041 isodtdinit(Ep *ep, Isoio *iso, Td *td)
1042 {
1043 Block *bp;
1044 long size;
1045 int i;
1046
1047 bp = td->bp;
1048 assert(bp != nil && BLEN(bp) == 0);
1049 size = (ep->hz+iso->left) * ep->pollival / 1000;
1050 iso->left = (ep->hz+iso->left) * ep->pollival % 1000;
1051 size *= ep->samplesz;
1052 if(size > ep->maxpkt){
1053 print("ohci: ep%d.%d: size > maxpkt\n",
1054 ep->dev->nb, ep->nb);
1055 print("size = %uld max = %ld\n", size, ep->maxpkt);
1056 size = ep->maxpkt;
1057 }
1058 td->nbytes = size;
1059 memset(KSEG1ADDR(bp->wp), 0, size); /* in case we don't fill it on time */
1060 td->cbp0 = td->cbp = ptr2pa(bp->rp) & ~0xFFF;
1061 td->ctrl = TRUNC(iso->frno, Ntdframes);
1062 td->offsets[0] = (ptr2pa(bp->rp) & 0xFFF);
1063 td->offsets[0] |= (Tdnotacc << Tdiccshift);
1064 /* in case the controller checks out the offests... */
1065 for(i = 1; i < nelem(td->offsets); i++)
1066 td->offsets[i] = td->offsets[0];
1067 td->be = ptr2pa(bp->rp + size - 1);
1068 td->ctrl |= (0 << Tdfcshift); /* frame count is 1 */
1069
1070 iso->frno = TRUNC(iso->frno + ep->pollival, Ntdframes);
1071 }
1072
1073 /*
1074 * start I/O on the dummy td and setup a new dummy to fill up.
1075 */
1076 static void
isoadvance(Ep * ep,Isoio * iso,Td * td)1077 isoadvance(Ep *ep, Isoio *iso, Td *td)
1078 {
1079 Td *dtd;
1080
1081 dtd = iso->atds;
1082 iso->atds = dtd->anext;
1083 iso->navail--;
1084 dtd->anext = nil;
1085 dtd->bp->wp = dtd->bp->rp;
1086 dtd->nexttd = 0;
1087 td->nexttd = ptr2pa(dtd);
1088 isodtdinit(ep, iso, dtd);
1089 iso->ed->tail = ptr2pa(dtd);
1090 }
1091
1092 static int
isocanwrite(void * a)1093 isocanwrite(void *a)
1094 {
1095 Isoio *iso;
1096
1097 iso = a;
1098 return iso->state == Qclose || iso->err != nil ||
1099 iso->navail > iso->nframes / 2;
1100 }
1101
1102 /*
1103 * Service a completed/failed Td from the done queue.
1104 * It may be of any transfer type.
1105 * The queue is not in completion order.
1106 * (It's actually in reverse completion order).
1107 *
1108 * When an error, a short packet, or a last Td is found
1109 * we awake the process waiting for the transfer.
1110 * Although later we will process other Tds completed
1111 * before, epio won't be able to touch the current Td
1112 * until interrupt returns and releases the lock on the
1113 * controller.
1114 */
1115 static void
qhinterrupt(Ctlr *,Ep * ep,Qio * io,Td * td,int)1116 qhinterrupt(Ctlr *, Ep *ep, Qio *io, Td *td, int)
1117 {
1118 Block *bp;
1119 int mode, err;
1120 Ed *ed;
1121
1122 ed = io->ed;
1123 if(io->state != Qrun)
1124 return;
1125 if(tdtok(td) == Tdtokin)
1126 mode = OREAD;
1127 else
1128 mode = OWRITE;
1129 bp = td->bp;
1130 err = tderrs(td);
1131
1132 switch(err){
1133 case Tddataovr: /* Overrun is not an error */
1134 break;
1135 case Tdok:
1136 /* virtualbox doesn't always report underflow on short packets */
1137 if(td->cbp == 0)
1138 break;
1139 /* fall through */
1140 case Tddataund:
1141 /* short input packets are ok */
1142 if(mode == OREAD){
1143 if(td->cbp == 0)
1144 panic("ohci: short packet but cbp == 0");
1145 /*
1146 * td->cbp and td->cbp0 are the real addresses
1147 * corresponding to virtual addresses bp->wp and
1148 * bp->rp respectively.
1149 */
1150 bp->wp = bp->rp + (td->cbp - td->cbp0);
1151 if(bp->wp < bp->rp)
1152 panic("ohci: wp < rp");
1153 /*
1154 * It's ok. clear error and flag as last in xfer.
1155 * epio must ignore following Tds.
1156 */
1157 td->last = 1;
1158 td->ctrl &= ~(Tdccmask << Tdccshift);
1159 break;
1160 }
1161 /* else fall; it's an error */
1162 case Tdcrc:
1163 case Tdbitstuff:
1164 case Tdbadtog:
1165 case Tdstalled:
1166 case Tdtmout:
1167 case Tdpidchk:
1168 case Tdbadpid:
1169 bp->wp = bp->rp; /* no bytes in xfer. */
1170 io->err = errmsg(err);
1171 if(debug || ep->debug){
1172 print("tdinterrupt: failed err %d (%s)\n", err, io->err);
1173 dumptd(td, "failed", ed->ctrl & Ediso);
1174 }
1175 td->last = 1;
1176 break;
1177 default:
1178 panic("ohci: td cc %ud unknown", err);
1179 }
1180
1181 if(td->last != 0){
1182 /*
1183 * clear td list and halt flag.
1184 */
1185 ed->head = (ed->head & Edtoggle) | ed->tail;
1186 ed->tds = pa2ptr(ed->tail);
1187 io->state = Qdone;
1188 wakeup(io);
1189 }
1190 }
1191
1192 /*
1193 * BUG: Iso input streams are not implemented.
1194 */
1195 static void
isointerrupt(Ctlr * ctlr,Ep * ep,Qio * io,Td * td,int)1196 isointerrupt(Ctlr *ctlr, Ep *ep, Qio *io, Td *td, int)
1197 {
1198 Isoio *iso;
1199 Block *bp;
1200 Ed *ed;
1201 int err, isoerr;
1202
1203 iso = ep->aux;
1204 ed = io->ed;
1205 if(io->state == Qclose)
1206 return;
1207 bp = td->bp;
1208 /*
1209 * When we get more than half the frames consecutive errors
1210 * we signal an actual error. Errors in the entire Td are
1211 * more serious and are always singaled.
1212 * Errors like overrun are not really errors. In fact, for
1213 * output, errors cannot be really detected. The driver will
1214 * hopefully notice I/O errors on input endpoints and detach the device.
1215 */
1216 err = tderrs(td);
1217 isoerr = (td->offsets[0] >> Tdiccshift) & Tdiccmask;
1218 if(isoerr == Tdok || isoerr == Tdnotacc)
1219 iso->nerrs = 0;
1220 else if(iso->nerrs++ > iso->nframes/2)
1221 err = Tdstalled;
1222 if(err != Tdok && err != Tddataovr){
1223 bp->wp = bp->rp;
1224 io->err = errmsg(err);
1225 if(debug || ep->debug){
1226 print("ohci: isointerrupt: ep%d.%d: err %d (%s) frnum 0x%lux\n",
1227 ep->dev->nb, ep->nb,
1228 err, errmsg(err), ctlr->ohci->fmnumber);
1229 dumptd(td, "failed", ed->ctrl & Ediso);
1230 }
1231 }
1232 td->bp->wp = td->bp->rp;
1233 td->nbytes = 0;
1234 td->anext = iso->atds;
1235 iso->atds = td;
1236 iso->navail++;
1237 /*
1238 * If almost all Tds are avail the user is not doing I/O at the
1239 * required rate. We put another Td in place to keep the polling rate.
1240 */
1241 if(iso->err == nil && iso->navail > iso->nframes - 10)
1242 isoadvance(ep, iso, pa2ptr(iso->ed->tail));
1243 /*
1244 * If there's enough buffering futher I/O can be done.
1245 */
1246 if(isocanwrite(iso))
1247 wakeup(iso);
1248 }
1249
1250 static void
interrupt(Ureg *,void * arg)1251 interrupt(Ureg *, void *arg)
1252 {
1253 Td *td, *ntd;
1254 Hci *hp;
1255 Ctlr *ctlr;
1256 ulong status, curred;
1257 int i, frno;
1258
1259 hp = arg;
1260 ctlr = hp->aux;
1261 ilock(ctlr);
1262 ctlr->ohci->intrdisable = Mie;
1263 coherence();
1264 status = ctlr->ohci->intrsts & ctlr->ohci->intrenable;
1265 status &= Oc|Rhsc|Fno|Ue|Rd|Sf|Wdh|So;
1266 frno = TRUNC(ctlr->ohci->fmnumber, Ntdframes);
1267 if(status & Wdh){
1268 /* lsb of donehead has bit to flag other intrs. */
1269 td = pa2ptr(ctlr->hcca->donehead & ~0xF);
1270
1271 for(i = 0; td != nil && i < 1024; i++){
1272 // if(0)ddprint("ohci tdinterrupt: td %#p\n", td);
1273 ntd = pa2ptr(td->nexttd & ~0xF);
1274 td->nexttd = 0;
1275 if(td->ep == nil || td->io == nil)
1276 panic("ohci: interrupt: ep %#p io %#p",
1277 td->ep, td->io);
1278 ohciinterrupts[td->ep->ttype]++;
1279 if(td->ep->ttype == Tiso)
1280 isointerrupt(ctlr, td->ep, td->io, td, frno);
1281 else
1282 qhinterrupt(ctlr, td->ep, td->io, td, frno);
1283 td = ntd;
1284 }
1285 if(i >= 1024)
1286 print("ohci: bug: more than 1024 done Tds?\n");
1287 ctlr->hcca->donehead = 0;
1288 }
1289
1290 ctlr->ohci->intrsts = status;
1291 status &= ~Wdh;
1292 status &= ~Sf;
1293 if(status & So){
1294 print("ohci: sched overrun: too much load\n");
1295 ctlr->overrun++;
1296 status &= ~So;
1297 }
1298 if((status & Ue) != 0){
1299 curred = ctlr->ohci->periodcurred;
1300 print("ohci: unrecoverable error frame 0x%.8lux ed 0x%.8lux, "
1301 "ints %d %d %d %d\n",
1302 ctlr->ohci->fmnumber, curred,
1303 ohciinterrupts[Tctl], ohciinterrupts[Tintr],
1304 ohciinterrupts[Tbulk], ohciinterrupts[Tiso]);
1305 if(curred != 0)
1306 dumped(pa2ptr(curred));
1307 status &= ~Ue;
1308 }
1309 if(status != 0)
1310 print("ohci interrupt: unhandled sts 0x%.8lux\n", status);
1311 ctlr->ohci->intrenable = Mie | Wdh | Ue;
1312 iunlock(ctlr);
1313 }
1314
1315 /*
1316 * The old dummy Td is used to implement the new Td.
1317 * A new dummy is linked at the end of the old one and
1318 * returned, to link further Tds if needed.
1319 */
1320 static Td*
epgettd(Ep * ep,Qio * io,Td ** dtdp,int flags,void * a,int count)1321 epgettd(Ep *ep, Qio *io, Td **dtdp, int flags, void *a, int count)
1322 {
1323 Td *td, *dtd;
1324 Block *bp;
1325
1326 if(count <= BY2PG)
1327 bp = allocb(count);
1328 else{
1329 if(count > 2*BY2PG)
1330 panic("ohci: transfer > two pages");
1331 /* maximum of one physical page crossing allowed */
1332 bp = allocb(count+BY2PG);
1333 bp->rp = (uchar*)PGROUND((uintptr)bp->rp);
1334 bp->wp = bp->rp;
1335 }
1336 dtd = *dtdp;
1337 td = dtd;
1338 td->bp = bp;
1339 if(count > 0){
1340 td->cbp0 = td->cbp = ptr2pa(bp->wp);
1341 td->be = ptr2pa(bp->wp + count - 1);
1342 if(a != nil){
1343 /* validaddr((uintptr)a, count, 0); DEBUG */
1344 memmove(KSEG1ADDR(bp->wp), a, count);
1345 }
1346 bp->wp += count;
1347 }
1348 td->nbytes = count;
1349 td->ctrl = io->tok|Tdusetog|io->toggle|flags;
1350 if(io->toggle == Tddata0)
1351 io->toggle = Tddata1;
1352 else
1353 io->toggle = Tddata0;
1354 assert(td->ep == ep);
1355 td->io = io;
1356 dtd = tdalloc(); /* new dummy */
1357 dtd->ep = ep;
1358 td->nexttd = ptr2pa(dtd);
1359 td->next = dtd;
1360 *dtdp = dtd;
1361 return td;
1362 }
1363
1364 /*
1365 * Try to get them idle
1366 */
1367 static void
aborttds(Qio * io)1368 aborttds(Qio *io)
1369 {
1370 Ed *ed;
1371 Td *td;
1372
1373 ed = io->ed;
1374 if(ed == nil)
1375 return;
1376 ed->ctrl |= Edskip;
1377 for(td = ed->tds; td != nil; td = td->next)
1378 if(td->bp != nil)
1379 td->bp->wp = td->bp->rp;
1380 ed->head = (ed->head&0xF) | ed->tail;
1381 if((ed->ctrl & Ediso) == 0)
1382 ed->tds = pa2ptr(ed->tail);
1383 }
1384
1385 static int
epiodone(void * a)1386 epiodone(void *a)
1387 {
1388 Qio *io;
1389
1390 io = a;
1391 return io->state != Qrun;
1392 }
1393
1394 static void
epiowait(Ctlr * ctlr,Qio * io,int tmout,ulong)1395 epiowait(Ctlr *ctlr, Qio *io, int tmout, ulong)
1396 {
1397 Ed *ed;
1398 int timedout;
1399
1400 ed = io->ed;
1401 // if(0)ddqprint("ohci io %#p sleep on ed %#p state %s\n",
1402 // io, ed, iosname[io->state]);
1403 timedout = 0;
1404 if(waserror()){
1405 dqprint("ohci io %#p ed %#p timed out\n", io, ed);
1406 timedout++;
1407 }else{
1408 if(tmout == 0)
1409 sleep(io, epiodone, io);
1410 else
1411 tsleep(io, epiodone, io, tmout);
1412 poperror();
1413 }
1414 ilock(ctlr);
1415 if(io->state == Qrun)
1416 timedout = 1;
1417 else if(io->state != Qdone && io->state != Qclose)
1418 panic("epio: ed not done and not closed");
1419 if(timedout){
1420 aborttds(io);
1421 io->err = "request timed out";
1422 iunlock(ctlr);
1423 if(!waserror()){
1424 tsleep(&up->sleep, return0, 0, Abortdelay);
1425 poperror();
1426 }
1427 ilock(ctlr);
1428 }
1429 if(io->state != Qclose)
1430 io->state = Qidle;
1431 iunlock(ctlr);
1432 }
1433
1434 /*
1435 * Non iso I/O.
1436 * To make it work for control transfers, the caller may
1437 * lock the Qio for the entire control transfer.
1438 */
1439 static long
epio(Ep * ep,Qio * io,void * a,long count,int mustlock)1440 epio(Ep *ep, Qio *io, void *a, long count, int mustlock)
1441 {
1442 Ed *ed;
1443 Ctlr *ctlr;
1444 char buf[80];
1445 char *err;
1446 uchar *c;
1447 Td *td, *ltd, *ntd, *td0;
1448 int last, ntds, tmout;
1449 long tot, n;
1450 ulong load;
1451
1452 ed = io->ed;
1453 ctlr = ep->hp->aux;
1454 io->debug = ep->debug;
1455 tmout = ep->tmout;
1456 ddeprint("ohci: %s ep%d.%d io %#p count %ld\n",
1457 io->tok == Tdtokin ? "in" : "out",
1458 ep->dev->nb, ep->nb, io, count);
1459 if((debug > 1 || ep->debug > 1) && io->tok != Tdtokin){
1460 seprintdata(buf, buf+sizeof(buf), a, count);
1461 print("\t%s\n", buf);
1462 }
1463 if(mustlock){
1464 qlock(io);
1465 if(waserror()){
1466 qunlock(io);
1467 nexterror();
1468 }
1469 }
1470 io->err = nil;
1471 ilock(ctlr);
1472 if(io->state == Qclose){ /* Tds released by cancelio */
1473 iunlock(ctlr);
1474 error(io->err ? io->err : Eio);
1475 }
1476 if(io->state != Qidle)
1477 panic("epio: qio not idle");
1478 io->state = Qinstall;
1479
1480 c = a;
1481 ltd = td0 = ed->tds;
1482 load = tot = 0;
1483 do{
1484 n = 2*BY2PG;
1485 if(count-tot < n)
1486 n = count-tot;
1487 if(c != nil && io->tok != Tdtokin)
1488 td = epgettd(ep, io, <d, 0, c+tot, n);
1489 else
1490 td = epgettd(ep, io, <d, 0, nil, n);
1491 tot += n;
1492 load += ep->load;
1493 }while(tot < count);
1494 if(td0 == nil || ltd == nil || td0 == ltd)
1495 panic("epio: no td");
1496 td->last = 1;
1497 if(debug > 2 || ep->debug > 2)
1498 dumptds(td0, "put td", ep->ttype == Tiso);
1499 iunlock(ctlr);
1500
1501 ilock(ctlr);
1502 if(io->state != Qclose){
1503 io->iotime = TK2MS(MACHP(0)->ticks);
1504 io->state = Qrun;
1505 ed->tail = ptr2pa(ltd);
1506 if(ep->ttype == Tctl)
1507 ctlr->ohci->cmdsts |= Sclf;
1508 else if(ep->ttype == Tbulk)
1509 ctlr->ohci->cmdsts |= Sblf;
1510 }
1511 iunlock(ctlr);
1512
1513 epiowait(ctlr, io, tmout, load);
1514 ilock(ctlr);
1515 if(debug > 1 || ep->debug > 1)
1516 dumptds(td0, "got td", 0);
1517 iunlock(ctlr);
1518
1519 tot = 0;
1520 c = a;
1521 ntds = last = 0;
1522 for(td = td0; td != ltd; td = ntd){
1523 ntds++;
1524 /*
1525 * If the Td is flagged as last we must
1526 * ignore any following Td. The block may
1527 * seem to have bytes but interrupt has not seen
1528 * those Tds through the done queue, and they are void.
1529 */
1530 if(last == 0 && tderrs(td) == Tdok){
1531 n = BLEN(td->bp);
1532 tot += n;
1533 if(c != nil && tdtok(td) == Tdtokin && n > 0){
1534 memmove(c, KSEG1ADDR(td->bp->rp), n);
1535 c += n;
1536 }
1537 }
1538 last |= td->last;
1539 ntd = td->next;
1540 tdfree(td);
1541 }
1542 if(edtoggle(ed) == 0)
1543 io->toggle = Tddata0;
1544 else
1545 io->toggle = Tddata1;
1546
1547 err = io->err;
1548 if(mustlock){
1549 qunlock(io);
1550 poperror();
1551 }
1552 ddeprint("ohci: io %#p: %d tds: return %ld err '%s'\n\n",
1553 io, ntds, tot, err);
1554 if(err != nil)
1555 error(err);
1556 if(tot < 0)
1557 error(Eio);
1558 return tot;
1559 }
1560
1561 /*
1562 * halt condition was cleared on the endpoint. update our toggles.
1563 */
1564 static void
clrhalt(Ep * ep)1565 clrhalt(Ep *ep)
1566 {
1567 Qio *io;
1568
1569 ep->clrhalt = 0;
1570 switch(ep->ttype){
1571 case Tbulk:
1572 case Tintr:
1573 io = ep->aux;
1574 if(ep->mode != OREAD){
1575 qlock(&io[OWRITE]);
1576 io[OWRITE].toggle = Tddata0;
1577 deprint("ep clrhalt for io %#p\n", io+OWRITE);
1578 qunlock(&io[OWRITE]);
1579 }
1580 if(ep->mode != OWRITE){
1581 qlock(&io[OREAD]);
1582 io[OREAD].toggle = Tddata0;
1583 deprint("ep clrhalt for io %#p\n", io+OREAD);
1584 qunlock(&io[OREAD]);
1585 }
1586 break;
1587 }
1588 }
1589
1590 static long
epread(Ep * ep,void * a,long count)1591 epread(Ep *ep, void *a, long count)
1592 {
1593 Ctlio *cio;
1594 Qio *io;
1595 char buf[80];
1596 ulong delta;
1597
1598 if(ep->aux == nil)
1599 panic("epread: not open");
1600
1601 switch(ep->ttype){
1602 case Tctl:
1603 cio = ep->aux;
1604 qlock(cio);
1605 if(waserror()){
1606 qunlock(cio);
1607 nexterror();
1608 }
1609 ddeprint("epread ctl ndata %d\n", cio->ndata);
1610 if(cio->ndata < 0)
1611 error("request expected");
1612 else if(cio->ndata == 0){
1613 cio->ndata = -1;
1614 count = 0;
1615 }else{
1616 if(count > cio->ndata)
1617 count = cio->ndata;
1618 if(count > 0)
1619 memmove(a, KSEG1ADDR(cio->data), count);
1620 /* BUG for big transfers */
1621 free(cio->data);
1622 cio->data = nil;
1623 cio->ndata = 0; /* signal EOF next time */
1624 }
1625 qunlock(cio);
1626 poperror();
1627 if(debug>1 || ep->debug){
1628 seprintdata(buf, buf+sizeof(buf), a, count);
1629 print("epread: %s\n", buf);
1630 }
1631 return count;
1632 case Tbulk:
1633 io = ep->aux;
1634 if(ep->clrhalt)
1635 clrhalt(ep);
1636 return epio(ep, &io[OREAD], a, count, 1);
1637 case Tintr:
1638 io = ep->aux;
1639 delta = TK2MS(MACHP(0)->ticks) - io[OREAD].iotime + 1;
1640 if(delta < ep->pollival / 2)
1641 tsleep(&up->sleep, return0, 0, ep->pollival/2 - delta);
1642 if(ep->clrhalt)
1643 clrhalt(ep);
1644 return epio(ep, &io[OREAD], a, count, 1);
1645 case Tiso:
1646 panic("ohci: iso read not implemented");
1647 break;
1648 default:
1649 panic("epread: bad ep ttype %d", ep->ttype);
1650 }
1651 return -1;
1652 }
1653
1654 /*
1655 * Control transfers are one setup write (data0)
1656 * plus zero or more reads/writes (data1, data0, ...)
1657 * plus a final write/read with data1 to ack.
1658 * For both host to device and device to host we perform
1659 * the entire transfer when the user writes the request,
1660 * and keep any data read from the device for a later read.
1661 * We call epio three times instead of placing all Tds at
1662 * the same time because doing so leads to crc/tmout errors
1663 * for some devices.
1664 * Upon errors on the data phase we must still run the status
1665 * phase or the device may cease responding in the future.
1666 */
1667 static long
epctlio(Ep * ep,Ctlio * cio,void * a,long count)1668 epctlio(Ep *ep, Ctlio *cio, void *a, long count)
1669 {
1670 uchar *c;
1671 long len;
1672
1673 ddeprint("epctlio: cio %#p ep%d.%d count %ld\n",
1674 cio, ep->dev->nb, ep->nb, count);
1675 if(count < Rsetuplen)
1676 error("short usb command");
1677 qlock(cio);
1678 free(cio->data);
1679 cio->data = nil;
1680 cio->ndata = 0;
1681 if(waserror()){
1682 qunlock(cio);
1683 free(cio->data);
1684 cio->data = nil;
1685 cio->ndata = 0;
1686 nexterror();
1687 }
1688
1689 /* set the address if unset and out of configuration state */
1690 if(ep->dev->state != Dconfig && ep->dev->state != Dreset)
1691 if(cio->usbid == 0){
1692 cio->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
1693 edsetaddr(cio->ed, cio->usbid);
1694 }
1695 /* adjust maxpkt if the user has learned a different one */
1696 if(edmaxpkt(cio->ed) != ep->maxpkt)
1697 edsetmaxpkt(cio->ed, ep->maxpkt);
1698 c = a;
1699 cio->tok = Tdtoksetup;
1700 cio->toggle = Tddata0;
1701 if(epio(ep, cio, a, Rsetuplen, 0) < Rsetuplen)
1702 error(Eio);
1703
1704 a = c + Rsetuplen;
1705 count -= Rsetuplen;
1706
1707 cio->toggle = Tddata1;
1708 if(c[Rtype] & Rd2h){
1709 cio->tok = Tdtokin;
1710 len = GET2(c+Rcount);
1711 if(len <= 0)
1712 error("bad length in d2h request");
1713 if(len > Maxctllen)
1714 error("d2h data too large to fit in ohci");
1715 a = cio->data = smalloc(len+1);
1716 }else{
1717 cio->tok = Tdtokout;
1718 len = count;
1719 }
1720 if(len > 0)
1721 if(waserror())
1722 len = -1;
1723 else{
1724 len = epio(ep, cio, a, len, 0);
1725 poperror();
1726 }
1727 if(c[Rtype] & Rd2h){
1728 count = Rsetuplen;
1729 cio->ndata = len;
1730 cio->tok = Tdtokout;
1731 }else{
1732 if(len < 0)
1733 count = -1;
1734 else
1735 count = Rsetuplen + len;
1736 cio->tok = Tdtokin;
1737 }
1738 cio->toggle = Tddata1;
1739 epio(ep, cio, nil, 0, 0);
1740 qunlock(cio);
1741 poperror();
1742 ddeprint("epctlio cio %#p return %ld\n", cio, count);
1743 return count;
1744 }
1745
1746 /*
1747 * Put new samples in the dummy Td.
1748 * BUG: This does only a transfer per Td. We could do up to 8.
1749 */
1750 static long
putsamples(Ctlr * ctlr,Ep * ep,Isoio * iso,uchar * b,long count)1751 putsamples(Ctlr *ctlr, Ep *ep, Isoio *iso, uchar *b, long count)
1752 {
1753 Td *td;
1754 ulong n;
1755
1756 td = pa2ptr(iso->ed->tail);
1757 n = count;
1758 if(n > td->nbytes - BLEN(td->bp))
1759 n = td->nbytes - BLEN(td->bp);
1760 assert(td->bp->wp + n <= td->bp->lim);
1761 memmove(KSEG1ADDR(td->bp->wp), b, n);
1762 td->bp->wp += n;
1763 if(BLEN(td->bp) == td->nbytes){ /* full Td: activate it */
1764 ilock(ctlr);
1765 isoadvance(ep, iso, td);
1766 iunlock(ctlr);
1767 }
1768 return n;
1769 }
1770
1771 static long
episowrite(Ep * ep,void * a,long count)1772 episowrite(Ep *ep, void *a, long count)
1773 {
1774 long tot, nw;
1775 char *err;
1776 uchar *b;
1777 Ctlr *ctlr;
1778 Isoio *iso;
1779
1780 ctlr = ep->hp->aux;
1781 iso = ep->aux;
1782 iso->debug = ep->debug;
1783
1784 qlock(iso);
1785 if(waserror()){
1786 qunlock(iso);
1787 nexterror();
1788 }
1789 diprint("ohci: episowrite: %#p ep%d.%d\n", iso, ep->dev->nb, ep->nb);
1790 ilock(ctlr);
1791 if(iso->state == Qclose){
1792 iunlock(ctlr);
1793 error(iso->err ? iso->err : Eio);
1794 }
1795 iso->state = Qrun;
1796 b = a;
1797 for(tot = 0; tot < count; tot += nw){
1798 while(isocanwrite(iso) == 0){
1799 iunlock(ctlr);
1800 diprint("ohci: episowrite: %#p sleep\n", iso);
1801 if(waserror()){
1802 if(iso->err == nil)
1803 iso->err = "I/O timed out";
1804 ilock(ctlr);
1805 break;
1806 }
1807 tsleep(iso, isocanwrite, iso, ep->tmout);
1808 poperror();
1809 ilock(ctlr);
1810 }
1811 err = iso->err;
1812 iso->err = nil;
1813 if(iso->state == Qclose || err != nil){
1814 iunlock(ctlr);
1815 error(err ? err : Eio);
1816 }
1817 if(iso->state != Qrun)
1818 panic("episowrite: iso not running");
1819 iunlock(ctlr); /* We could page fault here */
1820 nw = putsamples(ctlr, ep, iso, b+tot, count-tot);
1821 ilock(ctlr);
1822 }
1823 if(iso->state != Qclose)
1824 iso->state = Qdone;
1825 iunlock(ctlr);
1826 err = iso->err; /* in case it failed early */
1827 iso->err = nil;
1828 qunlock(iso);
1829 poperror();
1830 if(err != nil)
1831 error(err);
1832 diprint("ohci: episowrite: %#p %ld bytes\n", iso, tot);
1833 return tot;
1834 }
1835
1836 static long
epwrite(Ep * ep,void * a,long count)1837 epwrite(Ep *ep, void *a, long count)
1838 {
1839 Qio *io;
1840 Ctlio *cio;
1841 ulong delta;
1842 uchar *b;
1843 long tot, nw;
1844
1845 if(ep->aux == nil)
1846 panic("ohci: epwrite: not open");
1847 switch(ep->ttype){
1848 case Tctl:
1849 cio = ep->aux;
1850 return epctlio(ep, cio, a, count);
1851 case Tbulk:
1852 io = ep->aux;
1853 if(ep->clrhalt)
1854 clrhalt(ep);
1855 /*
1856 * Put at most Tdatomic Tds (512 bytes) at a time.
1857 * Otherwise some devices produce babble errors.
1858 */
1859 b = a;
1860 assert(a != nil);
1861 for(tot = 0; tot < count ; tot += nw){
1862 nw = count - tot;
1863 if(nw > Tdatomic * ep->maxpkt)
1864 nw = Tdatomic * ep->maxpkt;
1865 nw = epio(ep, &io[OWRITE], b+tot, nw, 1);
1866 }
1867 return tot;
1868 case Tintr:
1869 io = ep->aux;
1870 delta = TK2MS(MACHP(0)->ticks) - io[OWRITE].iotime + 1;
1871 if(delta < ep->pollival)
1872 tsleep(&up->sleep, return0, 0, ep->pollival - delta);
1873 if(ep->clrhalt)
1874 clrhalt(ep);
1875 return epio(ep, &io[OWRITE], a, count, 1);
1876 case Tiso:
1877 return episowrite(ep, a, count);
1878 default:
1879 panic("ohci: epwrite: bad ep ttype %d", ep->ttype);
1880 }
1881 return -1;
1882 }
1883
1884 static Ed*
newed(Ctlr * ctlr,Ep * ep,Qio * io,char *)1885 newed(Ctlr *ctlr, Ep *ep, Qio *io, char *)
1886 {
1887 Ed *ed;
1888 Td *td;
1889
1890 ed = io->ed = edalloc(); /* no errors raised here, really */
1891 td = tdalloc();
1892 td->ep = ep;
1893 td->io = io;
1894 ed->tail = ptr2pa(td);
1895 ed->head = ptr2pa(td);
1896 ed->tds = td;
1897 ed->ep = ep;
1898 ed->ctrl = (ep->maxpkt & Edmpsmask) << Edmpsshift;
1899 if(ep->ttype == Tiso)
1900 ed->ctrl |= Ediso;
1901 if(waserror()){
1902 edfree(ed);
1903 io->ed = nil;
1904 nexterror();
1905 }
1906 /* For setup endpoints we start with the config address */
1907 if(ep->ttype != Tctl)
1908 edsetaddr(io->ed, io->usbid);
1909 if(ep->dev->speed == Lowspeed)
1910 ed->ctrl |= Edlow;
1911 switch(io->tok){
1912 case Tdtokin:
1913 ed->ctrl |= Edin;
1914 break;
1915 case Tdtokout:
1916 ed->ctrl |= Edout;
1917 break;
1918 default:
1919 ed->ctrl |= Edtddir; /* Td will say */
1920 break;
1921 }
1922
1923 switch(ep->ttype){
1924 case Tctl:
1925 ilock(ctlr);
1926 edlinked(ed, ctlhd(ctlr));
1927 setctlhd(ctlr, ed);
1928 iunlock(ctlr);
1929 break;
1930 case Tbulk:
1931 ilock(ctlr);
1932 edlinked(ed, bulkhd(ctlr));
1933 setbulkhd(ctlr, ed);
1934 iunlock(ctlr);
1935 break;
1936 case Tintr:
1937 case Tiso:
1938 ilock(ctlr);
1939 schedq(ctlr, io, ep->pollival);
1940 iunlock(ctlr);
1941 break;
1942 default:
1943 panic("ohci: newed: bad ttype");
1944 }
1945 poperror();
1946 return ed;
1947 }
1948
1949 static void
isoopen(Ctlr * ctlr,Ep * ep)1950 isoopen(Ctlr *ctlr, Ep *ep)
1951 {
1952 Td *td, *edtds;
1953 Isoio *iso;
1954 int i;
1955
1956 iso = ep->aux;
1957 iso->usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
1958 iso->bw = ep->hz * ep->samplesz; /* bytes/sec */
1959 if(ep->mode != OWRITE){
1960 print("ohci: bug: iso input streams not implemented\n");
1961 error("ohci iso input streams not implemented");
1962 }else
1963 iso->tok = Tdtokout;
1964
1965 iso->left = 0;
1966 iso->nerrs = 0;
1967 iso->frno = TRUNC(ctlr->ohci->fmnumber + 10, Ntdframes);
1968 iso->nframes = 1000 / ep->pollival;
1969 if(iso->nframes < 10){
1970 print("ohci: isoopen: less than 10 frames; using 10.\n");
1971 iso->nframes = 10;
1972 }
1973 iso->navail = iso->nframes;
1974 iso->atds = edtds = nil;
1975 for(i = 0; i < iso->nframes-1; i++){ /* -1 for dummy */
1976 td = tdalloc();
1977 td->ep = ep;
1978 td->io = iso;
1979 td->bp = allocb(ep->maxpkt);
1980 td->anext = iso->atds; /* link as avail */
1981 iso->atds = td;
1982 td->next = edtds;
1983 edtds = td;
1984 }
1985 newed(ctlr, ep, iso, "iso"); /* allocates a dummy td */
1986 iso->ed->tds->bp = allocb(ep->maxpkt); /* but not its block */
1987 iso->ed->tds->next = edtds;
1988 isodtdinit(ep, iso, iso->ed->tds);
1989 }
1990
1991 /*
1992 * Allocate the endpoint and set it up for I/O
1993 * in the controller. This must follow what's said
1994 * in Ep regarding configuration, including perhaps
1995 * the saved toggles (saved on a previous close of
1996 * the endpoint data file by epclose).
1997 */
1998 static void
epopen(Ep * ep)1999 epopen(Ep *ep)
2000 {
2001 Ctlr *ctlr;
2002 Qio *io;
2003 Ctlio *cio;
2004 ulong usbid;
2005
2006 ctlr = ep->hp->aux;
2007 deprint("ohci: epopen ep%d.%d\n", ep->dev->nb, ep->nb);
2008 if(ep->aux != nil)
2009 panic("ohci: epopen called with open ep");
2010 if(waserror()){
2011 free(ep->aux);
2012 ep->aux = nil;
2013 nexterror();
2014 }
2015 switch(ep->ttype){
2016 case Tnone:
2017 error("endpoint not configured");
2018 case Tiso:
2019 ep->aux = smalloc(sizeof(Isoio));
2020 isoopen(ctlr, ep);
2021 break;
2022 case Tctl:
2023 cio = ep->aux = smalloc(sizeof(Ctlio));
2024 cio->debug = ep->debug;
2025 cio->ndata = -1;
2026 cio->data = nil;
2027 cio->tok = -1; /* invalid; Tds will say */
2028 if(ep->dev->isroot != 0 && ep->nb == 0) /* root hub */
2029 break;
2030 newed(ctlr, ep, cio, "epc");
2031 break;
2032 case Tbulk:
2033 ep->pollival = 1; /* assume this; doesn't really matter */
2034 /* and fall... */
2035 case Tintr:
2036 io = ep->aux = smalloc(sizeof(Qio)*2);
2037 io[OREAD].debug = io[OWRITE].debug = ep->debug;
2038 usbid = (ep->nb<<7)|(ep->dev->nb & Devmax);
2039 if(ep->mode != OREAD){
2040 if(ep->toggle[OWRITE] != 0)
2041 io[OWRITE].toggle = Tddata1;
2042 else
2043 io[OWRITE].toggle = Tddata0;
2044 io[OWRITE].tok = Tdtokout;
2045 io[OWRITE].usbid = usbid;
2046 io[OWRITE].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2047 newed(ctlr, ep, io+OWRITE, "epw");
2048 }
2049 if(ep->mode != OWRITE){
2050 if(ep->toggle[OREAD] != 0)
2051 io[OREAD].toggle = Tddata1;
2052 else
2053 io[OREAD].toggle = Tddata0;
2054 io[OREAD].tok = Tdtokin;
2055 io[OREAD].usbid = usbid;
2056 io[OREAD].bw = ep->maxpkt*1000/ep->pollival; /* bytes/s */
2057 newed(ctlr, ep, io+OREAD, "epr");
2058 }
2059 break;
2060 }
2061 deprint("ohci: epopen done:\n");
2062 if(debug || ep->debug)
2063 dump(ep->hp);
2064 poperror();
2065 }
2066
2067 static void
cancelio(Ep * ep,Qio * io)2068 cancelio(Ep *ep, Qio *io)
2069 {
2070 Ed *ed;
2071 Ctlr *ctlr;
2072
2073 ctlr = ep->hp->aux;
2074
2075 ilock(ctlr);
2076 if(io == nil || io->state == Qclose){
2077 assert(io == nil || io->ed == nil);
2078 iunlock(ctlr);
2079 return;
2080 }
2081 ed = io->ed;
2082 io->state = Qclose;
2083 io->err = Eio;
2084 aborttds(io);
2085 iunlock(ctlr);
2086 if(!waserror()){
2087 tsleep(&up->sleep, return0, 0, Abortdelay);
2088 poperror();
2089 }
2090
2091 wakeup(io);
2092 qlock(io);
2093 /* wait for epio if running */
2094 qunlock(io);
2095
2096 ilock(ctlr);
2097 switch(ep->ttype){
2098 case Tctl:
2099 unlinkctl(ctlr, ed);
2100 break;
2101 case Tbulk:
2102 unlinkbulk(ctlr, ed);
2103 break;
2104 case Tintr:
2105 case Tiso:
2106 unschedq(ctlr, io);
2107 break;
2108 default:
2109 panic("ohci cancelio: bad ttype");
2110 }
2111 iunlock(ctlr);
2112 edfree(io->ed);
2113 io->ed = nil;
2114 }
2115
2116 static void
epclose(Ep * ep)2117 epclose(Ep *ep)
2118 {
2119 Ctlio *cio;
2120 Isoio *iso;
2121 Qio *io;
2122
2123 deprint("ohci: epclose ep%d.%d\n", ep->dev->nb, ep->nb);
2124 if(ep->aux == nil)
2125 panic("ohci: epclose called with closed ep");
2126 switch(ep->ttype){
2127 case Tctl:
2128 cio = ep->aux;
2129 cancelio(ep, cio);
2130 free(cio->data);
2131 cio->data = nil;
2132 break;
2133 case Tbulk:
2134 case Tintr:
2135 io = ep->aux;
2136 if(ep->mode != OWRITE){
2137 cancelio(ep, &io[OREAD]);
2138 if(io[OREAD].toggle == Tddata1)
2139 ep->toggle[OREAD] = 1;
2140 }
2141 if(ep->mode != OREAD){
2142 cancelio(ep, &io[OWRITE]);
2143 if(io[OWRITE].toggle == Tddata1)
2144 ep->toggle[OWRITE] = 1;
2145 }
2146 break;
2147 case Tiso:
2148 iso = ep->aux;
2149 cancelio(ep, iso);
2150 break;
2151 default:
2152 panic("epclose: bad ttype %d", ep->ttype);
2153 }
2154
2155 deprint("ohci: epclose ep%d.%d: done\n", ep->dev->nb, ep->nb);
2156 free(ep->aux);
2157 ep->aux = nil;
2158 }
2159
2160 static int
portreset(Hci * hp,int port,int on)2161 portreset(Hci *hp, int port, int on)
2162 {
2163 Ctlr *ctlr;
2164 Ohci *ohci;
2165
2166 if(on == 0)
2167 return 0;
2168
2169 ctlr = hp->aux;
2170 qlock(&ctlr->resetl);
2171 if(waserror()){
2172 qunlock(&ctlr->resetl);
2173 nexterror();
2174 }
2175 ilock(ctlr);
2176 ohci = ctlr->ohci;
2177 ohci->rhportsts[port - 1] = Spp;
2178 if((ohci->rhportsts[port - 1] & Ccs) == 0){
2179 iunlock(ctlr);
2180 error("port not connected");
2181 }
2182 ohci->rhportsts[port - 1] = Spr;
2183 while((ohci->rhportsts[port - 1] & Prsc) == 0){
2184 iunlock(ctlr);
2185 dprint("ohci: portreset, wait for reset complete\n");
2186 ilock(ctlr);
2187 }
2188 ohci->rhportsts[port - 1] = Prsc;
2189 iunlock(ctlr);
2190 poperror();
2191 qunlock(&ctlr->resetl);
2192 return 0;
2193 }
2194
2195 static int
portenable(Hci * hp,int port,int on)2196 portenable(Hci *hp, int port, int on)
2197 {
2198 Ctlr *ctlr;
2199
2200 ctlr = hp->aux;
2201 dprint("ohci: %#p port %d enable=%d\n", ctlr->ohci, port, on);
2202 qlock(&ctlr->resetl);
2203 if(waserror()){
2204 qunlock(&ctlr->resetl);
2205 nexterror();
2206 }
2207 ilock(ctlr);
2208 if(on)
2209 ctlr->ohci->rhportsts[port - 1] = Spe | Spp;
2210 else
2211 ctlr->ohci->rhportsts[port - 1] = Cpe;
2212 iunlock(ctlr);
2213 tsleep(&up->sleep, return0, 0, Enabledelay);
2214 poperror();
2215 qunlock(&ctlr->resetl);
2216 return 0;
2217 }
2218
2219 static int
portstatus(Hci * hp,int port)2220 portstatus(Hci *hp, int port)
2221 {
2222 int v;
2223 Ctlr *ub;
2224 ulong ohcistatus;
2225
2226 /*
2227 * We must return status bits as a
2228 * get port status hub request would do.
2229 */
2230 ub = hp->aux;
2231 ohcistatus = ub->ohci->rhportsts[port - 1];
2232 v = 0;
2233 if(ohcistatus & Ccs)
2234 v |= HPpresent;
2235 if(ohcistatus & Pes)
2236 v |= HPenable;
2237 if(ohcistatus & Pss)
2238 v |= HPsuspend;
2239 if(ohcistatus & Prs)
2240 v |= HPreset;
2241 else {
2242 /* port is not in reset; these potential writes are ok */
2243 if(ohcistatus & Csc){
2244 v |= HPstatuschg;
2245 ub->ohci->rhportsts[port - 1] = Csc;
2246 }
2247 if(ohcistatus & Pesc){
2248 v |= HPchange;
2249 ub->ohci->rhportsts[port - 1] = Pesc;
2250 }
2251 }
2252 if(ohcistatus & Lsda)
2253 v |= HPslow;
2254 if(v & (HPstatuschg|HPchange))
2255 ddprint("ohci port %d sts %#ulx hub sts %#x\n", port, ohcistatus, v);
2256 return v;
2257 }
2258
2259 static void
dumpohci(Ctlr * ctlr)2260 dumpohci(Ctlr *ctlr)
2261 {
2262 int i;
2263 ulong *ohci;
2264
2265 ohci = &ctlr->ohci->revision;
2266 print("ohci registers: \n");
2267 for(i = 0; i < sizeof(Ohci)/sizeof(ulong); i++)
2268 if(i < 3 || ohci[i] != 0)
2269 print("\t[%#2.2x]\t%#8.8ulx\n", i * 4, ohci[i]);
2270 print("\n");
2271 }
2272
2273 static void
init(Hci * hp)2274 init(Hci *hp)
2275 {
2276 Ctlr *ctlr;
2277 Ohci *ohci;
2278 int i;
2279 ulong ival, ctrl, fmi;
2280
2281 ctlr = hp->aux;
2282 dprint("ohci %#p init\n", ctlr->ohci);
2283 ohci = ctlr->ohci;
2284
2285 fmi = ctlr->ohci->fminterval;
2286 ctlr->ohci->cmdsts = Shcr; /* reset the block */
2287 while(ctlr->ohci->cmdsts & Shcr)
2288 delay(1); /* wait till reset complete, Ohci says 10us max. */
2289 ctlr->ohci->fminterval = fmi;
2290
2291 /*
2292 * now that soft reset is done we are in suspend state.
2293 * Setup registers which take in suspend state
2294 * (will only be here for 2ms).
2295 */
2296
2297 ctlr->ohci->hcca = ptr2pa(ctlr->hcca);
2298 setctlhd(ctlr, nil);
2299 ctlr->ohci->ctlcurred = 0;
2300 setbulkhd(ctlr, nil);
2301 ctlr->ohci->bulkcurred = 0;
2302
2303 ohci->intrenable = Mie | Wdh | Ue;
2304 ohci->control |= Ccle | Cble | Cple | Cie | Cfsoper;
2305
2306 /* set frame after operational */
2307 ohci->rhdesca = Nps; /* no power switching */
2308 if(ohci->rhdesca & Nps){
2309 dprint("ohci: ports are not power switched\n");
2310 }else{
2311 dprint("ohci: ports are power switched\n");
2312 ohci->rhdesca &= ~Psm;
2313 ohci->rhsts &= ~Lpsc;
2314 }
2315 for(i = 0; i < ctlr->nports; i++) /* paranoia */
2316 ohci->rhportsts[i] = 0; /* this has no effect */
2317 delay(50);
2318
2319 for(i = 0; i < ctlr->nports; i++){
2320 ohci->rhportsts[i] = Spp;
2321 if((ohci->rhportsts[i] & Ccs) != 0)
2322 ohci->rhportsts[i] |= Spr;
2323 }
2324 delay(100);
2325
2326 ctrl = ohci->control;
2327 if((ctrl & Cfsmask) != Cfsoper){
2328 ctrl = (ctrl & ~Cfsmask) | Cfsoper;
2329 ohci->control = ctrl;
2330 ohci->rhsts = Lpsc;
2331 }
2332 ival = ohci->fminterval & ~(Fmaxpktmask << Fmaxpktshift);
2333 ohci->fminterval = ival | (5120 << Fmaxpktshift);
2334
2335 if(debug > 1)
2336 dumpohci(ctlr);
2337 }
2338
2339 static void
scanpci(void)2340 scanpci(void)
2341 {
2342 ulong mem;
2343 Ctlr *ctlr;
2344 Pcidev *p;
2345 int i;
2346 static int already = 0;
2347
2348 if(already)
2349 return;
2350 already = 1;
2351 p = nil;
2352 while(p = pcimatch(p, 0, 0)) {
2353 /*
2354 * Find Ohci controllers (Programming Interface = 0x10).
2355 */
2356 if(p->ccrb != Pcibcserial || p->ccru != Pciscusb ||
2357 p->ccrp != 0x10)
2358 continue;
2359 mem = p->mem[0].bar & ~0x0F;
2360 dprint("ohci: %x/%x port 0x%lux size 0x%x irq %d\n",
2361 p->vid, p->did, mem, p->mem[0].size, p->intl);
2362 if(mem == 0){
2363 print("ohci: failed to map registers\n");
2364 continue;
2365 }
2366 // if(p->intl == 0xFF || p->intl == 0) {
2367 // print("ohci: no irq assigned for port %#lux\n", mem);
2368 // continue;
2369 // }
2370
2371 ctlr = malloc(sizeof(Ctlr));
2372 if (ctlr == nil)
2373 panic("ohci: out of memory");
2374 ctlr->pcidev = p;
2375 ctlr->ohci = KSEG1ADDR(PCIMEMADDR(mem));
2376 dprint("scanpci: ctlr %#p, ohci %#p\n", ctlr, ctlr->ohci);
2377 pcisetbme(p);
2378 pcisetpms(p, 0);
2379 for(i = 0; i < Nhcis; i++)
2380 if(ctlrs[i] == nil){
2381 ctlrs[i] = ctlr;
2382 break;
2383 }
2384 if(i == Nhcis)
2385 print("ohci: bug: no more controllers\n");
2386 }
2387 }
2388
2389 static void
usbdebug(Hci *,int d)2390 usbdebug(Hci*, int d)
2391 {
2392 debug = d;
2393 }
2394
2395 /*
2396 * build the periodic scheduling tree:
2397 * framesize must be a multiple of the tree size
2398 */
2399 static void
mkqhtree(Ctlr * ctlr)2400 mkqhtree(Ctlr *ctlr)
2401 {
2402 int i, n, d, o, leaf0, depth;
2403 Ed **tree;
2404 Qtree *qt;
2405
2406 depth = flog2(32);
2407 n = (1 << (depth+1)) - 1;
2408 qt = mallocz(sizeof(*qt), 1);
2409 if(qt == nil)
2410 panic("usb: can't allocate scheduling tree");
2411 qt->nel = n;
2412 qt->depth = depth;
2413 qt->bw = mallocz(n * sizeof(qt->bw), 1);
2414 qt->root = tree = mallocz(n * sizeof(Ed *), 1);
2415 if(qt->bw == nil || qt->root == nil)
2416 panic("usb: can't allocate scheduling tree");
2417 for(i = 0; i < n; i++){
2418 if((tree[i] = edalloc()) == nil)
2419 panic("mkqhtree");
2420 tree[i]->ctrl = (8 << Edmpsshift); /* not needed */
2421 tree[i]->ctrl |= Edskip;
2422
2423 if(i > 0)
2424 edlinked(tree[i], tree[(i-1)/2]);
2425 else
2426 edlinked(tree[i], nil);
2427 }
2428 ctlr->ntree = i;
2429 dprint("ohci: tree: %d endpoints allocated\n", i);
2430
2431 /* distribute leaves evenly round the frame list */
2432 leaf0 = n / 2;
2433 for(i = 0; i < 32; i++){
2434 o = 0;
2435 for(d = 0; d < depth; d++){
2436 o <<= 1;
2437 if(i & (1 << d))
2438 o |= 1;
2439 }
2440 if(leaf0 + o >= n){
2441 print("leaf0=%d o=%d i=%d n=%d\n", leaf0, o, i, n);
2442 break;
2443 }
2444 ctlr->hcca->intrtable[i] = ptr2pa(tree[leaf0 + o]);
2445 }
2446 ctlr->tree = qt;
2447 }
2448
2449 static void
ohcimeminit(Ctlr * ctlr)2450 ohcimeminit(Ctlr *ctlr)
2451 {
2452 Hcca *hcca;
2453
2454 edfree(edalloc()); /* allocate pools now */
2455 tdfree(tdalloc());
2456
2457 hcca = xspanalloc(sizeof(Hcca), 256, 0);
2458 if(hcca == nil)
2459 panic("usbhreset: no memory for Hcca");
2460 hcca = KSEG1ADDR(hcca); // XXX
2461 memset(hcca, 0, sizeof(*hcca));
2462 ctlr->hcca = hcca;
2463
2464 mkqhtree(ctlr);
2465 }
2466
2467 static void
ohcireset(Ctlr * ctlr)2468 ohcireset(Ctlr *ctlr)
2469 {
2470 ilock(ctlr);
2471 dprint("ohci %#p reset\n", ctlr->ohci);
2472
2473 /*
2474 * usually enter here in reset, wait till its through,
2475 * then do our own so we are on known timing conditions.
2476 * Is this needed?
2477 */
2478 delay(100);
2479 ctlr->ohci->control = 0;
2480 delay(100);
2481
2482 /* legacy support register: turn off lunacy mode */
2483 pcicfgw16(ctlr->pcidev, 0xc0, 0x2000);
2484
2485 iunlock(ctlr);
2486 }
2487
2488 static void
shutdown(Hci * hp)2489 shutdown(Hci *hp)
2490 {
2491 Ctlr *ctlr;
2492
2493 ctlr = hp->aux;
2494
2495 ilock(ctlr);
2496 ctlr->ohci->intrdisable = Mie;
2497 ctlr->ohci->control = 0;
2498 coherence();
2499 delay(100);
2500 iunlock(ctlr);
2501 }
2502
2503 static int
reset(Hci * hp)2504 reset(Hci *hp)
2505 {
2506 int i;
2507 Ctlr *ctlr;
2508 Pcidev *p;
2509 static Lock resetlck;
2510
2511 if(getconf("*nousbohci"))
2512 return -1;
2513 ilock(&resetlck);
2514 scanpci();
2515
2516 /*
2517 * Any adapter matches if no hp->port is supplied,
2518 * otherwise the ports must match.
2519 */
2520 ctlr = nil;
2521 for(i = 0; i < Nhcis && ctlrs[i] != nil; i++){
2522 ctlr = ctlrs[i];
2523 if(ctlr->active == 0)
2524 if(hp->port == 0 || hp->port == (uintptr)ctlr->ohci){
2525 ctlr->active = 1;
2526 break;
2527 }
2528 }
2529 iunlock(&resetlck);
2530 if(ctlrs[i] == nil || i == Nhcis)
2531 return -1;
2532 if(ctlr->ohci->control == ~0)
2533 return -1;
2534
2535
2536 p = ctlr->pcidev;
2537 hp->aux = ctlr;
2538 hp->port = (uintptr)ctlr->ohci;
2539 hp->irq = ILpci;
2540 hp->tbdf = p->tbdf;
2541 ctlr->nports = hp->nports = ctlr->ohci->rhdesca & 0xff;
2542
2543 ohcireset(ctlr);
2544 ohcimeminit(ctlr);
2545
2546 /*
2547 * Linkage to the generic HCI driver.
2548 */
2549 hp->init = init;
2550 hp->dump = dump;
2551 hp->interrupt = interrupt;
2552 hp->epopen = epopen;
2553 hp->epclose = epclose;
2554 hp->epread = epread;
2555 hp->epwrite = epwrite;
2556 hp->seprintep = seprintep;
2557 hp->portenable = portenable;
2558 hp->portreset = portreset;
2559 hp->portstatus = portstatus;
2560 hp->shutdown = shutdown;
2561 hp->debug = usbdebug;
2562 hp->type = "ohci";
2563 return 0;
2564 }
2565
2566 void
usbohcilink(void)2567 usbohcilink(void)
2568 {
2569 addhcitype("ohci", reset);
2570 }
2571