ohci.c revision 1.72 1 /* $NetBSD: ohci.c,v 1.72 2000/02/22 11:30:54 augustss Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/ohci.c,v 1.22 1999/11/17 22:33:40 n_hibma Exp $ */
3
4 /*
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (augustss (at) carlstedt.se) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 /*
42 * USB Open Host Controller driver.
43 *
44 * OHCI spec: ftp://ftp.compaq.com/pub/supportinformation/papers/hcir1_0a.exe
45 * USB spec: http://www.usb.org/developers/data/usb11.pdf
46 */
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #if defined(__NetBSD__) || defined(__OpenBSD__)
52 #include <sys/kernel.h>
53 #include <sys/device.h>
54 #include <sys/select.h>
55 #elif defined(__FreeBSD__)
56 #include <sys/module.h>
57 #include <sys/bus.h>
58 #include <machine/bus_pio.h>
59 #include <machine/bus_memio.h>
60 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
61 #include <machine/cpu.h>
62 #endif
63 #endif
64 #include <sys/proc.h>
65 #include <sys/queue.h>
66
67 #include <machine/bus.h>
68 #include <machine/endian.h>
69
70 #include <dev/usb/usb.h>
71 #include <dev/usb/usbdi.h>
72 #include <dev/usb/usbdivar.h>
73 #include <dev/usb/usb_mem.h>
74 #include <dev/usb/usb_quirks.h>
75
76 #include <dev/usb/ohcireg.h>
77 #include <dev/usb/ohcivar.h>
78
79 #if defined(__FreeBSD__)
80 #include <machine/clock.h>
81
82 #define delay(d) DELAY(d)
83 #endif
84
85 #if defined(__OpenBSD__)
86 struct cfdriver ohci_cd = {
87 NULL, "ohci", DV_DULL
88 };
89 #endif
90
91 #ifdef OHCI_DEBUG
92 #define DPRINTF(x) if (ohcidebug) logprintf x
93 #define DPRINTFN(n,x) if (ohcidebug>(n)) logprintf x
94 int ohcidebug = 0;
95 #else
96 #define DPRINTF(x)
97 #define DPRINTFN(n,x)
98 #endif
99
100 /*
101 * The OHCI controller is little endian, so on big endian machines
102 * the data strored in memory needs to be swapped.
103 */
104 #if BYTE_ORDER == BIG_ENDIAN
105 #define LE(x) (bswap32(x))
106 #else
107 #define LE(x) (x)
108 #endif
109
110 struct ohci_pipe;
111
112 static ohci_soft_ed_t *ohci_alloc_sed __P((ohci_softc_t *));
113 static void ohci_free_sed __P((ohci_softc_t *, ohci_soft_ed_t *));
114
115 static ohci_soft_td_t *ohci_alloc_std __P((ohci_softc_t *));
116 static void ohci_free_std __P((ohci_softc_t *, ohci_soft_td_t *));
117
118 static ohci_soft_itd_t *ohci_alloc_sitd __P((ohci_softc_t *));
119 static void ohci_free_sitd __P((ohci_softc_t *,ohci_soft_itd_t *));
120
121 #if 0
122 static void ohci_free_std_chain __P((ohci_softc_t *,
123 ohci_soft_td_t *, ohci_soft_td_t *));
124 #endif
125 static usbd_status ohci_alloc_std_chain __P((struct ohci_pipe *,
126 ohci_softc_t *, int, int, u_int16_t, usb_dma_t *,
127 ohci_soft_td_t *, ohci_soft_td_t **));
128
129 static void ohci_shutdown __P((void *v));
130 static void ohci_power __P((int, void *));
131 static usbd_status ohci_open __P((usbd_pipe_handle));
132 static void ohci_poll __P((struct usbd_bus *));
133 static void ohci_softintr __P((struct usbd_bus *));
134 static void ohci_waitintr __P((ohci_softc_t *,
135 usbd_xfer_handle));
136 static void ohci_rhsc __P((ohci_softc_t *, usbd_xfer_handle));
137
138 static usbd_status ohci_device_request __P((usbd_xfer_handle xfer));
139 static void ohci_add_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
140 static void ohci_rem_ed __P((ohci_soft_ed_t *, ohci_soft_ed_t *));
141 static void ohci_hash_add_td __P((ohci_softc_t *,
142 ohci_soft_td_t *));
143 static void ohci_hash_rem_td __P((ohci_softc_t *,
144 ohci_soft_td_t *));
145 static ohci_soft_td_t *ohci_hash_find_td __P((ohci_softc_t *,
146 ohci_physaddr_t));
147
148 static usbd_status ohci_setup_isoc __P((usbd_pipe_handle pipe));
149 static void ohci_device_isoc_enter __P((usbd_xfer_handle));
150
151 static usbd_status ohci_allocm __P((struct usbd_bus *, usb_dma_t *,
152 u_int32_t));
153 static void ohci_freem __P((struct usbd_bus *, usb_dma_t *));
154
155 static usbd_xfer_handle ohci_allocx __P((struct usbd_bus *));
156 static void ohci_freex __P((struct usbd_bus *, usbd_xfer_handle));
157
158 static usbd_status ohci_root_ctrl_transfer __P((usbd_xfer_handle));
159 static usbd_status ohci_root_ctrl_start __P((usbd_xfer_handle));
160 static void ohci_root_ctrl_abort __P((usbd_xfer_handle));
161 static void ohci_root_ctrl_close __P((usbd_pipe_handle));
162 static void ohci_root_ctrl_done __P((usbd_xfer_handle));
163
164 static usbd_status ohci_root_intr_transfer __P((usbd_xfer_handle));
165 static usbd_status ohci_root_intr_start __P((usbd_xfer_handle));
166 static void ohci_root_intr_abort __P((usbd_xfer_handle));
167 static void ohci_root_intr_close __P((usbd_pipe_handle));
168 static void ohci_root_intr_done __P((usbd_xfer_handle));
169
170 static usbd_status ohci_device_ctrl_transfer __P((usbd_xfer_handle));
171 static usbd_status ohci_device_ctrl_start __P((usbd_xfer_handle));
172 static void ohci_device_ctrl_abort __P((usbd_xfer_handle));
173 static void ohci_device_ctrl_close __P((usbd_pipe_handle));
174 static void ohci_device_ctrl_done __P((usbd_xfer_handle));
175
176 static usbd_status ohci_device_bulk_transfer __P((usbd_xfer_handle));
177 static usbd_status ohci_device_bulk_start __P((usbd_xfer_handle));
178 static void ohci_device_bulk_abort __P((usbd_xfer_handle));
179 static void ohci_device_bulk_close __P((usbd_pipe_handle));
180 static void ohci_device_bulk_done __P((usbd_xfer_handle));
181
182 static usbd_status ohci_device_intr_transfer __P((usbd_xfer_handle));
183 static usbd_status ohci_device_intr_start __P((usbd_xfer_handle));
184 static void ohci_device_intr_abort __P((usbd_xfer_handle));
185 static void ohci_device_intr_close __P((usbd_pipe_handle));
186 static void ohci_device_intr_done __P((usbd_xfer_handle));
187
188 static usbd_status ohci_device_isoc_transfer __P((usbd_xfer_handle));
189 static usbd_status ohci_device_isoc_start __P((usbd_xfer_handle));
190 static void ohci_device_isoc_abort __P((usbd_xfer_handle));
191 static void ohci_device_isoc_close __P((usbd_pipe_handle));
192 static void ohci_device_isoc_done __P((usbd_xfer_handle));
193
194 static usbd_status ohci_device_setintr __P((ohci_softc_t *sc,
195 struct ohci_pipe *pipe, int ival));
196
197 static int ohci_str __P((usb_string_descriptor_t *, int, char *));
198
199 static void ohci_timeout __P((void *));
200 static void ohci_rhsc_able __P((ohci_softc_t *, int));
201
202 static void ohci_close_pipe __P((usbd_pipe_handle pipe,
203 ohci_soft_ed_t *head));
204 static void ohci_abort_xfer __P((usbd_xfer_handle xfer,
205 usbd_status status));
206 static void ohci_abort_xfer_end __P((void *));
207
208 static void ohci_device_clear_toggle __P((usbd_pipe_handle pipe));
209 static void ohci_noop __P((usbd_pipe_handle pipe));
210
211 #ifdef OHCI_DEBUG
212 static void ohci_dumpregs __P((ohci_softc_t *));
213 static void ohci_dump_tds __P((ohci_soft_td_t *));
214 static void ohci_dump_td __P((ohci_soft_td_t *));
215 static void ohci_dump_ed __P((ohci_soft_ed_t *));
216 #endif
217
218 #define OWRITE4(sc, r, x) bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x))
219 #define OREAD4(sc, r) bus_space_read_4((sc)->iot, (sc)->ioh, (r))
220 #define OREAD2(sc, r) bus_space_read_2((sc)->iot, (sc)->ioh, (r))
221
222 /* Reverse the bits in a value 0 .. 31 */
223 static u_int8_t revbits[OHCI_NO_INTRS] =
224 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c,
225 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e,
226 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d,
227 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f };
228
229 struct ohci_pipe {
230 struct usbd_pipe pipe;
231 ohci_soft_ed_t *sed;
232 union {
233 ohci_soft_td_t *td;
234 ohci_soft_itd_t *itd;
235 } tail;
236 /* Info needed for different pipe kinds. */
237 union {
238 /* Control pipe */
239 struct {
240 usb_dma_t reqdma;
241 u_int length;
242 ohci_soft_td_t *setup, *data, *stat;
243 } ctl;
244 /* Interrupt pipe */
245 struct {
246 int nslots;
247 int pos;
248 } intr;
249 /* Bulk pipe */
250 struct {
251 u_int length;
252 int isread;
253 } bulk;
254 /* Iso pipe */
255 struct iso {
256 int next, inuse;
257 } iso;
258 } u;
259 };
260
261 #define OHCI_INTR_ENDPT 1
262
263 static struct usbd_bus_methods ohci_bus_methods = {
264 ohci_open,
265 ohci_poll,
266 ohci_softintr,
267 ohci_allocm,
268 ohci_freem,
269 ohci_allocx,
270 ohci_freex,
271 };
272
273 static struct usbd_pipe_methods ohci_root_ctrl_methods = {
274 ohci_root_ctrl_transfer,
275 ohci_root_ctrl_start,
276 ohci_root_ctrl_abort,
277 ohci_root_ctrl_close,
278 ohci_noop,
279 ohci_root_ctrl_done,
280 };
281
282 static struct usbd_pipe_methods ohci_root_intr_methods = {
283 ohci_root_intr_transfer,
284 ohci_root_intr_start,
285 ohci_root_intr_abort,
286 ohci_root_intr_close,
287 ohci_noop,
288 ohci_root_intr_done,
289 };
290
291 static struct usbd_pipe_methods ohci_device_ctrl_methods = {
292 ohci_device_ctrl_transfer,
293 ohci_device_ctrl_start,
294 ohci_device_ctrl_abort,
295 ohci_device_ctrl_close,
296 ohci_noop,
297 ohci_device_ctrl_done,
298 };
299
300 static struct usbd_pipe_methods ohci_device_intr_methods = {
301 ohci_device_intr_transfer,
302 ohci_device_intr_start,
303 ohci_device_intr_abort,
304 ohci_device_intr_close,
305 ohci_device_clear_toggle,
306 ohci_device_intr_done,
307 };
308
309 static struct usbd_pipe_methods ohci_device_bulk_methods = {
310 ohci_device_bulk_transfer,
311 ohci_device_bulk_start,
312 ohci_device_bulk_abort,
313 ohci_device_bulk_close,
314 ohci_device_clear_toggle,
315 ohci_device_bulk_done,
316 };
317
318 static struct usbd_pipe_methods ohci_device_isoc_methods = {
319 ohci_device_isoc_transfer,
320 ohci_device_isoc_start,
321 ohci_device_isoc_abort,
322 ohci_device_isoc_close,
323 ohci_noop,
324 ohci_device_isoc_done,
325 };
326
327 #if defined(__NetBSD__) || defined(__OpenBSD__)
328 int
329 ohci_activate(self, act)
330 device_ptr_t self;
331 enum devact act;
332 {
333 struct ohci_softc *sc = (struct ohci_softc *)self;
334 int rv = 0;
335
336 switch (act) {
337 case DVACT_ACTIVATE:
338 return (EOPNOTSUPP);
339 break;
340
341 case DVACT_DEACTIVATE:
342 if (sc->sc_child != NULL)
343 rv = config_deactivate(sc->sc_child);
344 break;
345 }
346 return (rv);
347 }
348
349 int
350 ohci_detach(sc, flags)
351 struct ohci_softc *sc;
352 int flags;
353 {
354 int rv = 0;
355
356 if (sc->sc_child != NULL)
357 rv = config_detach(sc->sc_child, flags);
358
359 if (rv != 0)
360 return (rv);
361
362 #if defined(__NetBSD__) || defined(__OpenBSD__)
363 powerhook_disestablish(sc->sc_powerhook);
364 shutdownhook_disestablish(sc->sc_shutdownhook);
365 #endif
366
367 /* free data structures XXX */
368
369 return (rv);
370 }
371 #endif
372
373 ohci_soft_ed_t *
374 ohci_alloc_sed(sc)
375 ohci_softc_t *sc;
376 {
377 ohci_soft_ed_t *sed;
378 usbd_status err;
379 int i, offs;
380 usb_dma_t dma;
381
382 if (sc->sc_freeeds == NULL) {
383 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n"));
384 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK,
385 OHCI_ED_ALIGN, &dma);
386 if (err)
387 return (0);
388 for(i = 0; i < OHCI_SED_CHUNK; i++) {
389 offs = i * OHCI_SED_SIZE;
390 sed = (ohci_soft_ed_t *)((char *)KERNADDR(&dma) +offs);
391 sed->physaddr = DMAADDR(&dma) + offs;
392 sed->next = sc->sc_freeeds;
393 sc->sc_freeeds = sed;
394 }
395 }
396 sed = sc->sc_freeeds;
397 sc->sc_freeeds = sed->next;
398 memset(&sed->ed, 0, sizeof(ohci_ed_t));
399 sed->next = 0;
400 return (sed);
401 }
402
403 void
404 ohci_free_sed(sc, sed)
405 ohci_softc_t *sc;
406 ohci_soft_ed_t *sed;
407 {
408 sed->next = sc->sc_freeeds;
409 sc->sc_freeeds = sed;
410 }
411
412 ohci_soft_td_t *
413 ohci_alloc_std(sc)
414 ohci_softc_t *sc;
415 {
416 ohci_soft_td_t *std;
417 usbd_status err;
418 int i, offs;
419 usb_dma_t dma;
420 int s;
421
422 if (sc->sc_freetds == NULL) {
423 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n"));
424 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
425 OHCI_TD_ALIGN, &dma);
426 if (err)
427 return (0);
428 s = splusb();
429 for(i = 0; i < OHCI_STD_CHUNK; i++) {
430 offs = i * OHCI_STD_SIZE;
431 std = (ohci_soft_td_t *)((char *)KERNADDR(&dma) +offs);
432 std->physaddr = DMAADDR(&dma) + offs;
433 std->nexttd = sc->sc_freetds;
434 sc->sc_freetds = std;
435 }
436 splx(s);
437 }
438
439 s = splusb();
440 std = sc->sc_freetds;
441 sc->sc_freetds = std->nexttd;
442 memset(&std->td, 0, sizeof(ohci_td_t));
443 std->nexttd = 0;
444
445 ohci_hash_add_td(sc, std);
446 splx(s);
447
448 return (std);
449 }
450
451 void
452 ohci_free_std(sc, std)
453 ohci_softc_t *sc;
454 ohci_soft_td_t *std;
455 {
456 int s;
457
458 s = splusb();
459 ohci_hash_rem_td(sc, std);
460
461 std->nexttd = sc->sc_freetds;
462 sc->sc_freetds = std;
463 splx(s);
464 }
465
466 usbd_status
467 ohci_alloc_std_chain(opipe, sc, len, rd, flags, dma, sp, ep)
468 struct ohci_pipe *opipe;
469 ohci_softc_t *sc;
470 int len, rd;
471 u_int16_t flags;
472 usb_dma_t *dma;
473 ohci_soft_td_t *sp, **ep;
474 {
475 ohci_soft_td_t *next, *cur;
476 ohci_physaddr_t dataphys, dataphysend;
477 u_int32_t intr, tdflags;
478 int curlen;
479
480 DPRINTFN(len < 4096,("ohci_alloc_std_chain: start len=%d\n", len));
481 cur = sp;
482 dataphys = DMAADDR(dma);
483 dataphysend = OHCI_PAGE(dataphys + len - 1);
484 tdflags =
485 (rd ? OHCI_TD_IN : OHCI_TD_OUT) |
486 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY |
487 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0);
488
489 for (;;) {
490 next = ohci_alloc_std(sc);
491 if (next == 0)
492 goto nomem;
493
494 /* The OHCI hardware can handle at most one page crossing. */
495 if (OHCI_PAGE(dataphys) == dataphysend ||
496 OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend) {
497 /* we can handle it in this TD */
498 curlen = len;
499 } else {
500 /* must use multiple TDs, fill as much as possible. */
501 curlen = 2 * OHCI_PAGE_SIZE -
502 (dataphys & (OHCI_PAGE_SIZE-1));
503 }
504 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x "
505 "dataphysend=0x%08x len=%d curlen=%d\n",
506 dataphys, dataphysend,
507 len, curlen));
508 len -= curlen;
509
510 intr = len == 0 ? OHCI_TD_SET_DI(1) : OHCI_TD_NOINTR;
511 cur->td.td_flags = LE(tdflags | intr);
512 cur->td.td_cbp = LE(dataphys);
513 cur->nexttd = next;
514 cur->td.td_nexttd = LE(next->physaddr);
515 cur->td.td_be = LE(dataphys + curlen - 1);
516 cur->len = curlen;
517 cur->flags = OHCI_ADD_LEN;
518 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n",
519 dataphys, dataphys + curlen - 1));
520 if (len == 0)
521 break;
522 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n"));
523 dataphys += curlen;
524 cur = next;
525 }
526 if ((flags & USBD_FORCE_SHORT_XFER) &&
527 len % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) {
528 /* Force a 0 length transfer at the end. */
529 next = ohci_alloc_std(sc);
530 if (next == 0)
531 goto nomem;
532
533 cur->td.td_flags = LE(tdflags | OHCI_TD_SET_DI(1));
534 cur->td.td_cbp = 0; /* indicate 0 length packet */
535 cur->nexttd = next;
536 cur->td.td_nexttd = LE(next->physaddr);
537 cur->td.td_be = LE(dataphys - 1);
538 cur->len = 0;
539 cur->flags = 0;
540 cur = next;
541 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n"));
542 }
543 cur->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
544 *ep = next;
545
546 return (USBD_NORMAL_COMPLETION);
547
548 nomem:
549 /* XXX free chain */
550 return (USBD_NOMEM);
551 }
552
553 #if 0
554 static void
555 ohci_free_std_chain(sc, std, stdend)
556 ohci_softc_t *sc;
557 ohci_soft_td_t *std;
558 ohci_soft_td_t *stdend;
559 {
560 ohci_soft_td_t *p;
561
562 for (; std != stdend; std = p) {
563 p = std->nexttd;
564 ohci_free_std(sc, std);
565 }
566 }
567 #endif
568
569 ohci_soft_itd_t *
570 ohci_alloc_sitd(sc)
571 ohci_softc_t *sc;
572 {
573 ohci_soft_itd_t *sitd;
574 usbd_status err;
575 int i, offs;
576 usb_dma_t dma;
577
578 if (sc->sc_freeitds == NULL) {
579 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n"));
580 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK,
581 OHCI_TD_ALIGN, &dma);
582 if (err)
583 return (0);
584 for(i = 0; i < OHCI_STD_CHUNK; i++) {
585 offs = i * OHCI_STD_SIZE;
586 sitd = (ohci_soft_itd_t *)((char*)KERNADDR(&dma)+offs);
587 sitd->physaddr = DMAADDR(&dma) + offs;
588 sitd->nextitd = sc->sc_freeitds;
589 sc->sc_freeitds = sitd;
590 }
591 }
592 sitd = sc->sc_freeitds;
593 sc->sc_freeitds = sitd->nextitd;
594 memset(&sitd->itd, 0, sizeof(ohci_itd_t));
595 sitd->nextitd = 0;
596 return (sitd);
597 }
598
599 void
600 ohci_free_sitd(sc, sitd)
601 ohci_softc_t *sc;
602 ohci_soft_itd_t *sitd;
603 {
604 sitd->nextitd = sc->sc_freeitds;
605 sc->sc_freeitds = sitd;
606 }
607
608 usbd_status
609 ohci_init(sc)
610 ohci_softc_t *sc;
611 {
612 ohci_soft_ed_t *sed, *psed;
613 usbd_status err;
614 int i;
615 u_int32_t s, ctl, ival, hcr, fm, per, rev, desca;
616
617 DPRINTF(("ohci_init: start\n"));
618 #if defined(__OpenBSD__)
619 printf(",");
620 #else
621 printf("%s:", USBDEVNAME(sc->sc_bus.bdev));
622 #endif
623 rev = OREAD4(sc, OHCI_REVISION);
624 printf(" OHCI version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev),
625 OHCI_REV_LEGACY(rev) ? ", legacy support" : "");
626
627 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) {
628 printf("%s: unsupported OHCI revision\n",
629 USBDEVNAME(sc->sc_bus.bdev));
630 sc->sc_bus.usbrev = USBREV_UNKNOWN;
631 return (USBD_INVAL);
632 }
633 sc->sc_bus.usbrev = USBREV_1_0;
634
635 for (i = 0; i < OHCI_HASH_SIZE; i++)
636 LIST_INIT(&sc->sc_hash_tds[i]);
637
638 SIMPLEQ_INIT(&sc->sc_free_xfers);
639
640 /* Allocate the HCCA area. */
641 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE,
642 OHCI_HCCA_ALIGN, &sc->sc_hccadma);
643 if (err)
644 return (err);
645 sc->sc_hcca = (struct ohci_hcca *)KERNADDR(&sc->sc_hccadma);
646 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE);
647
648 sc->sc_eintrs = OHCI_NORMAL_INTRS;
649
650 /* Allocate dummy ED that starts the control list. */
651 sc->sc_ctrl_head = ohci_alloc_sed(sc);
652 if (sc->sc_ctrl_head == NULL) {
653 err = USBD_NOMEM;
654 goto bad1;
655 }
656 sc->sc_ctrl_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
657
658 /* Allocate dummy ED that starts the bulk list. */
659 sc->sc_bulk_head = ohci_alloc_sed(sc);
660 if (sc->sc_bulk_head == NULL) {
661 err = USBD_NOMEM;
662 goto bad2;
663 }
664 sc->sc_bulk_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
665
666 /* Allocate dummy ED that starts the isochronous list. */
667 sc->sc_isoc_head = ohci_alloc_sed(sc);
668 if (sc->sc_isoc_head == NULL) {
669 err = USBD_NOMEM;
670 goto bad3;
671 }
672 sc->sc_isoc_head->ed.ed_flags |= LE(OHCI_ED_SKIP);
673
674 /* Allocate all the dummy EDs that make up the interrupt tree. */
675 for (i = 0; i < OHCI_NO_EDS; i++) {
676 sed = ohci_alloc_sed(sc);
677 if (sed == NULL) {
678 while (--i >= 0)
679 ohci_free_sed(sc, sc->sc_eds[i]);
680 err = USBD_NOMEM;
681 goto bad4;
682 }
683 /* All ED fields are set to 0. */
684 sc->sc_eds[i] = sed;
685 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
686 if (i != 0)
687 psed = sc->sc_eds[(i-1) / 2];
688 else
689 psed= sc->sc_isoc_head;
690 sed->next = psed;
691 sed->ed.ed_nexted = LE(psed->physaddr);
692 }
693 /*
694 * Fill HCCA interrupt table. The bit reversal is to get
695 * the tree set up properly to spread the interrupts.
696 */
697 for (i = 0; i < OHCI_NO_INTRS; i++)
698 sc->sc_hcca->hcca_interrupt_table[revbits[i]] =
699 LE(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr);
700
701 /* Determine in what context we are running. */
702 ctl = OREAD4(sc, OHCI_CONTROL);
703 if (ctl & OHCI_IR) {
704 /* SMM active, request change */
705 DPRINTF(("ohci_init: SMM active, request owner change\n"));
706 s = OREAD4(sc, OHCI_COMMAND_STATUS);
707 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR);
708 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) {
709 usb_delay_ms(&sc->sc_bus, 1);
710 ctl = OREAD4(sc, OHCI_CONTROL);
711 }
712 if ((ctl & OHCI_IR) == 0) {
713 printf("%s: SMM does not respond, resetting\n",
714 USBDEVNAME(sc->sc_bus.bdev));
715 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
716 goto reset;
717 }
718 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) {
719 /* BIOS started controller. */
720 DPRINTF(("ohci_init: BIOS active\n"));
721 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) {
722 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL);
723 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
724 }
725 } else {
726 DPRINTF(("ohci_init: cold started\n"));
727 reset:
728 /* Controller was cold started. */
729 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
730 }
731
732 /*
733 * This reset should not be necessary according to the OHCI spec, but
734 * without it some controllers do not start.
735 */
736 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev)));
737 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
738 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY);
739
740 /* We now own the host controller and the bus has been reset. */
741 ival = OHCI_GET_IVAL(OREAD4(sc, OHCI_FM_INTERVAL));
742
743 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */
744 /* Nominal time for a reset is 10 us. */
745 for (i = 0; i < 10; i++) {
746 delay(10);
747 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR;
748 if (!hcr)
749 break;
750 }
751 if (hcr) {
752 printf("%s: reset timeout\n", USBDEVNAME(sc->sc_bus.bdev));
753 err = USBD_IOERROR;
754 goto bad5;
755 }
756 #ifdef OHCI_DEBUG
757 if (ohcidebug > 15)
758 ohci_dumpregs(sc);
759 #endif
760
761 /* The controller is now in SUSPEND state, we have 2ms to finish. */
762
763 /* Set up HC registers. */
764 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma));
765 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr);
766 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr);
767 /* disable all interrupts and then switch on all desired interrupts */
768 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
769 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE);
770 /* switch on desired functional features */
771 ctl = OREAD4(sc, OHCI_CONTROL);
772 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR);
773 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE |
774 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL;
775 /* And finally start it! */
776 OWRITE4(sc, OHCI_CONTROL, ctl);
777
778 /*
779 * The controller is now OPERATIONAL. Set a some final
780 * registers that should be set earlier, but that the
781 * controller ignores when in the SUSPEND state.
782 */
783 fm = (OREAD4(sc, OHCI_FM_INTERVAL) & OHCI_FIT) ^ OHCI_FIT;
784 fm |= OHCI_FSMPS(ival) | ival;
785 OWRITE4(sc, OHCI_FM_INTERVAL, fm);
786 per = OHCI_PERIODIC(ival); /* 90% periodic */
787 OWRITE4(sc, OHCI_PERIODIC_START, per);
788
789 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */
790 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
791 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP);
792 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */
793 usb_delay_ms(&sc->sc_bus, 5);
794 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca);
795
796 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A));
797
798 #ifdef OHCI_DEBUG
799 if (ohcidebug > 5)
800 ohci_dumpregs(sc);
801 #endif
802
803 /* Set up the bus struct. */
804 sc->sc_bus.methods = &ohci_bus_methods;
805 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe);
806
807 #if defined(__NetBSD__) || defined(__OpenBSD__)
808 sc->sc_powerhook = powerhook_establish(ohci_power, sc);
809 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc);
810 #endif
811
812 return (USBD_NORMAL_COMPLETION);
813
814 bad5:
815 for (i = 0; i < OHCI_NO_EDS; i++)
816 ohci_free_sed(sc, sc->sc_eds[i]);
817 bad4:
818 ohci_free_sed(sc, sc->sc_isoc_head);
819 bad3:
820 ohci_free_sed(sc, sc->sc_ctrl_head);
821 bad2:
822 ohci_free_sed(sc, sc->sc_bulk_head);
823 bad1:
824 usb_freemem(&sc->sc_bus, &sc->sc_hccadma);
825 return (err);
826 }
827
828 usbd_status
829 ohci_allocm(bus, dma, size)
830 struct usbd_bus *bus;
831 usb_dma_t *dma;
832 u_int32_t size;
833 {
834 #if defined(__NetBSD__) || defined(__OpenBSD__)
835 struct ohci_softc *sc = (struct ohci_softc *)bus;
836 #endif
837
838 return (usb_allocmem(&sc->sc_bus, size, 0, dma));
839 }
840
841 void
842 ohci_freem(bus, dma)
843 struct usbd_bus *bus;
844 usb_dma_t *dma;
845 {
846 #if defined(__NetBSD__) || defined(__OpenBSD__)
847 struct ohci_softc *sc = (struct ohci_softc *)bus;
848 #endif
849
850 usb_freemem(&sc->sc_bus, dma);
851 }
852
853 usbd_xfer_handle
854 ohci_allocx(bus)
855 struct usbd_bus *bus;
856 {
857 struct ohci_softc *sc = (struct ohci_softc *)bus;
858 usbd_xfer_handle xfer;
859
860 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
861 if (xfer != NULL)
862 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
863 else
864 xfer = malloc(sizeof(*xfer), M_USB, M_NOWAIT);
865 if (xfer != NULL)
866 memset(xfer, 0, sizeof *xfer);
867 return (xfer);
868 }
869
870 void
871 ohci_freex(bus, xfer)
872 struct usbd_bus *bus;
873 usbd_xfer_handle xfer;
874 {
875 struct ohci_softc *sc = (struct ohci_softc *)bus;
876
877 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
878 }
879
880 /*
881 * Shut down the controller when the system is going down.
882 */
883 void
884 ohci_shutdown(v)
885 void *v;
886 {
887 ohci_softc_t *sc = v;
888
889 DPRINTF(("ohci_shutdown: stopping the HC\n"));
890 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
891 }
892
893 /*
894 * Handle suspend/resume.
895 *
896 * We need to switch to polling mode here, because this routine is
897 * called from an intterupt context. This is all right since we
898 * are almost suspended anyway.
899 */
900 void
901 ohci_power(why, v)
902 int why;
903 void *v;
904 {
905 #ifdef OHCI_DEBUG
906 ohci_softc_t *sc = v;
907
908 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why));
909 /* XXX should suspend/resume */
910 ohci_dumpregs(sc);
911 #endif
912 }
913
914 #ifdef OHCI_DEBUG
915 void
916 ohci_dumpregs(sc)
917 ohci_softc_t *sc;
918 {
919 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n",
920 OREAD4(sc, OHCI_REVISION),
921 OREAD4(sc, OHCI_CONTROL),
922 OREAD4(sc, OHCI_COMMAND_STATUS)));
923 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n",
924 OREAD4(sc, OHCI_INTERRUPT_STATUS),
925 OREAD4(sc, OHCI_INTERRUPT_ENABLE),
926 OREAD4(sc, OHCI_INTERRUPT_DISABLE)));
927 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n",
928 OREAD4(sc, OHCI_HCCA),
929 OREAD4(sc, OHCI_PERIOD_CURRENT_ED),
930 OREAD4(sc, OHCI_CONTROL_HEAD_ED)));
931 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n",
932 OREAD4(sc, OHCI_CONTROL_CURRENT_ED),
933 OREAD4(sc, OHCI_BULK_HEAD_ED),
934 OREAD4(sc, OHCI_BULK_CURRENT_ED)));
935 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n",
936 OREAD4(sc, OHCI_DONE_HEAD),
937 OREAD4(sc, OHCI_FM_INTERVAL),
938 OREAD4(sc, OHCI_FM_REMAINING)));
939 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n",
940 OREAD4(sc, OHCI_FM_NUMBER),
941 OREAD4(sc, OHCI_PERIODIC_START),
942 OREAD4(sc, OHCI_LS_THRESHOLD)));
943 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n",
944 OREAD4(sc, OHCI_RH_DESCRIPTOR_A),
945 OREAD4(sc, OHCI_RH_DESCRIPTOR_B),
946 OREAD4(sc, OHCI_RH_STATUS)));
947 DPRINTF((" port1=0x%08x port2=0x%08x\n",
948 OREAD4(sc, OHCI_RH_PORT_STATUS(1)),
949 OREAD4(sc, OHCI_RH_PORT_STATUS(2))));
950 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n",
951 LE(sc->sc_hcca->hcca_frame_number),
952 LE(sc->sc_hcca->hcca_done_head)));
953 }
954 #endif
955
956 static int ohci_intr1 __P((ohci_softc_t *));
957
958 int
959 ohci_intr(p)
960 void *p;
961 {
962 ohci_softc_t *sc = p;
963
964 /* If we get an interrupt while polling, then just ignore it. */
965 if (sc->sc_bus.use_polling) {
966 #ifdef DIAGNOSTIC
967 printf("ohci_intr: ignored interrupt while polling\n");
968 #endif
969 return (0);
970 }
971
972 return (ohci_intr1(sc));
973 }
974
975 static int
976 ohci_intr1(sc)
977 ohci_softc_t *sc;
978 {
979 u_int32_t intrs, eintrs;
980 ohci_physaddr_t done;
981
982 /* In case the interrupt occurs before initialization has completed. */
983 if (sc == NULL || sc->sc_hcca == NULL) {
984 #ifdef DIAGNOSTIC
985 printf("ohci_intr: sc->sc_hcca == NULL\n");
986 #endif
987 return (0);
988 }
989
990 intrs = 0;
991 done = LE(sc->sc_hcca->hcca_done_head);
992 if (done != 0) {
993 if (done & ~OHCI_DONE_INTRS)
994 intrs = OHCI_WDH;
995 if (done & OHCI_DONE_INTRS)
996 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS);
997 } else
998 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS);
999
1000 if (!intrs)
1001 return (0);
1002
1003 intrs &= ~OHCI_MIE;
1004 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */
1005 eintrs = intrs & sc->sc_eintrs;
1006 if (!eintrs)
1007 return (0);
1008
1009 sc->sc_bus.intr_context++;
1010 sc->sc_bus.no_intrs++;
1011 DPRINTFN(7, ("ohci_intr: sc=%p intrs=%x(%x) eintr=%x\n",
1012 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS),
1013 (u_int)eintrs));
1014
1015 if (eintrs & OHCI_SO) {
1016 printf("%s: scheduling overrun\n",USBDEVNAME(sc->sc_bus.bdev));
1017 /* XXX do what */
1018 intrs &= ~OHCI_SO;
1019 }
1020 if (eintrs & OHCI_WDH) {
1021 done &= OHCI_DONE_INTRS;
1022 if (sc->sc_done == 0)
1023 sc->sc_done = done;
1024 else {
1025 /* Tack on at the end of sc_done. */
1026 ohci_physaddr_t ldone;
1027 ohci_soft_td_t *std;
1028
1029 for (ldone = sc->sc_done; ldone != 0;
1030 ldone = LE(std->td.td_nexttd))
1031 std = ohci_hash_find_td(sc, done);
1032 std->td.td_nexttd = LE(done);
1033 }
1034 sc->sc_hcca->hcca_done_head = 0;
1035 usb_schedsoftintr(&sc->sc_bus);
1036 intrs &= ~OHCI_WDH;
1037 }
1038 if (eintrs & OHCI_RD) {
1039 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1040 /* XXX process resume detect */
1041 }
1042 if (eintrs & OHCI_UE) {
1043 printf("%s: unrecoverable error, controller halted\n",
1044 USBDEVNAME(sc->sc_bus.bdev));
1045 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET);
1046 /* XXX what else */
1047 }
1048 if (eintrs & OHCI_RHSC) {
1049 ohci_rhsc(sc, sc->sc_intrxfer);
1050 intrs &= ~OHCI_RHSC;
1051
1052 /*
1053 * Disable RHSC interrupt for now, because it will be
1054 * on until the port has been reset.
1055 */
1056 ohci_rhsc_able(sc, 0);
1057 }
1058
1059 sc->sc_bus.intr_context--;
1060
1061 /* Block unprocessed interrupts. XXX */
1062 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, intrs);
1063 sc->sc_eintrs &= ~intrs;
1064
1065 return (1);
1066 }
1067
1068 void
1069 ohci_rhsc_able(sc, on)
1070 ohci_softc_t *sc;
1071 int on;
1072 {
1073 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on));
1074 if (on) {
1075 sc->sc_eintrs |= OHCI_RHSC;
1076 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC);
1077 } else {
1078 sc->sc_eintrs &= ~OHCI_RHSC;
1079 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC);
1080 }
1081 }
1082
1083 #ifdef OHCI_DEBUG
1084 char *ohci_cc_strs[] = {
1085 "NO_ERROR",
1086 "CRC",
1087 "BIT_STUFFING",
1088 "DATA_TOGGLE_MISMATCH",
1089 "STALL",
1090 "DEVICE_NOT_RESPONDING",
1091 "PID_CHECK_FAILURE",
1092 "UNEXPECTED_PID",
1093 "DATA_OVERRUN",
1094 "DATA_UNDERRUN",
1095 "BUFFER_OVERRUN",
1096 "BUFFER_UNDERRUN",
1097 "reserved",
1098 "reserved",
1099 "NOT_ACCESSED",
1100 "NOT_ACCESSED",
1101 };
1102 #endif
1103
1104 void
1105 ohci_softintr(bus)
1106 struct usbd_bus *bus;
1107 {
1108 ohci_softc_t *sc = (ohci_softc_t *)bus;
1109 ohci_physaddr_t done;
1110 ohci_soft_td_t *std, *sdone, *stdnext;
1111 usbd_xfer_handle xfer;
1112 int len, cc, s;
1113
1114 sc->sc_bus.intr_context++;
1115
1116 s = splhardusb();
1117 done = sc->sc_done;
1118 sc->sc_done = 0;
1119 splx(s);
1120
1121 DPRINTFN(10,("ohci_process_done: done=0x%08lx\n", (u_long)done));
1122
1123 /* Reverse the done list. */
1124 for (sdone = NULL; done != 0; done = LE(std->td.td_nexttd)) {
1125 std = ohci_hash_find_td(sc, done);
1126 std->dnext = sdone;
1127 sdone = std;
1128 }
1129
1130 #ifdef OHCI_DEBUG
1131 if (ohcidebug > 10) {
1132 DPRINTF(("ohci_process_done: TD done:\n"));
1133 ohci_dump_tds(sdone);
1134 }
1135 #endif
1136
1137 for (std = sdone; std; std = stdnext) {
1138 xfer = std->xfer;
1139 stdnext = std->dnext;
1140 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n",
1141 std, xfer, xfer ? xfer->hcpriv : 0));
1142 if (xfer == NULL) {
1143 /* xfer == NULL: There seems to be no xfer associated
1144 * with this TD. It is tailp that happened to end up on
1145 * the done queue.
1146 */
1147 continue;
1148 }
1149 cc = OHCI_TD_GET_CC(LE(std->td.td_flags));
1150 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1151 if (xfer->status == USBD_CANCELLED ||
1152 xfer->status == USBD_TIMEOUT) {
1153 DPRINTF(("ohci_process_done: cancel/timeout %p\n",
1154 xfer));
1155 /* Handled by abort routine. */
1156 } else if (cc == OHCI_CC_NO_ERROR) {
1157 len = std->len;
1158 if (std->td.td_cbp != 0)
1159 len -= LE(std->td.td_be) -
1160 LE(std->td.td_cbp) + 1;
1161 if (std->flags & OHCI_ADD_LEN)
1162 xfer->actlen += len;
1163 if (std->flags & OHCI_CALL_DONE) {
1164 xfer->status = USBD_NORMAL_COMPLETION;
1165 usb_transfer_complete(xfer);
1166 }
1167 ohci_free_std(sc, std);
1168 } else {
1169 /*
1170 * Endpoint is halted. First unlink all the TDs
1171 * belonging to the failed transfer, and then restart
1172 * the endpoint.
1173 */
1174 ohci_soft_td_t *p, *n;
1175 struct ohci_pipe *opipe =
1176 (struct ohci_pipe *)xfer->pipe;
1177
1178 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n",
1179 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1180 ohci_cc_strs[OHCI_TD_GET_CC(LE(std->td.td_flags))]));
1181
1182 /* remove TDs */
1183 for (p = std; p->xfer == xfer; p = n) {
1184 n = p->nexttd;
1185 ohci_free_std(sc, p);
1186 }
1187
1188 /* clear halt */
1189 opipe->sed->ed.ed_headp = LE(p->physaddr);
1190 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1191
1192 if (cc == OHCI_CC_STALL)
1193 xfer->status = USBD_STALLED;
1194 else
1195 xfer->status = USBD_IOERROR;
1196 usb_transfer_complete(xfer);
1197 }
1198 }
1199
1200 sc->sc_bus.intr_context--;
1201 }
1202
1203 void
1204 ohci_device_ctrl_done(xfer)
1205 usbd_xfer_handle xfer;
1206 {
1207 DPRINTFN(10,("ohci_ctrl_done: xfer=%p\n", xfer));
1208
1209 #ifdef DIAGNOSTIC
1210 if (!(xfer->rqflags & URQ_REQUEST)) {
1211 panic("ohci_ctrl_done: not a request\n");
1212 }
1213 #endif
1214 xfer->hcpriv = NULL;
1215 }
1216
1217 void
1218 ohci_device_intr_done(xfer)
1219 usbd_xfer_handle xfer;
1220 {
1221 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1222 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1223 ohci_soft_ed_t *sed = opipe->sed;
1224 ohci_soft_td_t *data, *tail;
1225
1226
1227 DPRINTFN(10,("ohci_intr_done: xfer=%p, actlen=%d\n",
1228 xfer, xfer->actlen));
1229
1230 xfer->hcpriv = NULL;
1231
1232 if (xfer->pipe->repeat) {
1233 data = opipe->tail.td;
1234 tail = ohci_alloc_std(sc); /* XXX should reuse TD */
1235 if (tail == NULL) {
1236 xfer->status = USBD_NOMEM;
1237 return;
1238 }
1239 tail->xfer = NULL;
1240
1241 data->td.td_flags = LE(
1242 OHCI_TD_IN | OHCI_TD_NOCC |
1243 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
1244 if (xfer->flags & USBD_SHORT_XFER_OK)
1245 data->td.td_flags |= LE(OHCI_TD_R);
1246 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf));
1247 data->nexttd = tail;
1248 data->td.td_nexttd = LE(tail->physaddr);
1249 data->td.td_be = LE(LE(data->td.td_cbp) + xfer->length - 1);
1250 data->len = xfer->length;
1251 data->xfer = xfer;
1252 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1253 xfer->hcpriv = data;
1254 xfer->actlen = 0;
1255
1256 sed->ed.ed_tailp = LE(tail->physaddr);
1257 opipe->tail.td = tail;
1258 }
1259 }
1260
1261 void
1262 ohci_device_bulk_done(xfer)
1263 usbd_xfer_handle xfer;
1264 {
1265 DPRINTFN(10,("ohci_bulk_done: xfer=%p, actlen=%d\n",
1266 xfer, xfer->actlen));
1267
1268 xfer->hcpriv = NULL;
1269 }
1270
1271 void
1272 ohci_rhsc(sc, xfer)
1273 ohci_softc_t *sc;
1274 usbd_xfer_handle xfer;
1275 {
1276 usbd_pipe_handle pipe;
1277 struct ohci_pipe *opipe;
1278 u_char *p;
1279 int i, m;
1280 int hstatus;
1281
1282 hstatus = OREAD4(sc, OHCI_RH_STATUS);
1283 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n",
1284 sc, xfer, hstatus));
1285
1286 if (xfer == NULL) {
1287 /* Just ignore the change. */
1288 return;
1289 }
1290
1291 pipe = xfer->pipe;
1292 opipe = (struct ohci_pipe *)pipe;
1293
1294 p = KERNADDR(&xfer->dmabuf);
1295 m = min(sc->sc_noport, xfer->length * 8 - 1);
1296 memset(p, 0, xfer->length);
1297 for (i = 1; i <= m; i++) {
1298 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16)
1299 p[i/8] |= 1 << (i%8);
1300 }
1301 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p));
1302 xfer->actlen = xfer->length;
1303 xfer->status = USBD_NORMAL_COMPLETION;
1304
1305 usb_transfer_complete(xfer);
1306 }
1307
1308 void
1309 ohci_root_intr_done(xfer)
1310 usbd_xfer_handle xfer;
1311 {
1312 xfer->hcpriv = NULL;
1313 }
1314
1315 void
1316 ohci_root_ctrl_done(xfer)
1317 usbd_xfer_handle xfer;
1318 {
1319 xfer->hcpriv = NULL;
1320 }
1321
1322 /*
1323 * Wait here until controller claims to have an interrupt.
1324 * Then call ohci_intr and return. Use timeout to avoid waiting
1325 * too long.
1326 */
1327 void
1328 ohci_waitintr(sc, xfer)
1329 ohci_softc_t *sc;
1330 usbd_xfer_handle xfer;
1331 {
1332 int timo = xfer->timeout;
1333 int usecs;
1334 u_int32_t intrs;
1335
1336 xfer->status = USBD_IN_PROGRESS;
1337 for (usecs = timo * 1000000 / hz; usecs > 0; usecs -= 1000) {
1338 usb_delay_ms(&sc->sc_bus, 1);
1339 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs;
1340 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs));
1341 #ifdef OHCI_DEBUG
1342 if (ohcidebug > 15)
1343 ohci_dumpregs(sc);
1344 #endif
1345 if (intrs) {
1346 ohci_intr1(sc);
1347 if (xfer->status != USBD_IN_PROGRESS)
1348 return;
1349 }
1350 }
1351
1352 /* Timeout */
1353 DPRINTF(("ohci_waitintr: timeout\n"));
1354 xfer->status = USBD_TIMEOUT;
1355 usb_transfer_complete(xfer);
1356 /* XXX should free TD */
1357 }
1358
1359 void
1360 ohci_poll(bus)
1361 struct usbd_bus *bus;
1362 {
1363 ohci_softc_t *sc = (ohci_softc_t *)bus;
1364
1365 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs)
1366 ohci_intr1(sc);
1367 }
1368
1369 usbd_status
1370 ohci_device_request(xfer)
1371 usbd_xfer_handle xfer;
1372 {
1373 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1374 usb_device_request_t *req = &xfer->request;
1375 usbd_device_handle dev = opipe->pipe.device;
1376 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1377 int addr = dev->address;
1378 ohci_soft_td_t *setup, *data = 0, *stat, *next, *tail;
1379 ohci_soft_ed_t *sed;
1380 int isread;
1381 int len;
1382 usbd_status err;
1383 int s;
1384
1385 isread = req->bmRequestType & UT_READ;
1386 len = UGETW(req->wLength);
1387
1388 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, "
1389 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
1390 req->bmRequestType, req->bRequest, UGETW(req->wValue),
1391 UGETW(req->wIndex), len, addr,
1392 opipe->pipe.endpoint->edesc->bEndpointAddress));
1393
1394 setup = opipe->tail.td;
1395 stat = ohci_alloc_std(sc);
1396 if (stat == NULL) {
1397 err = USBD_NOMEM;
1398 goto bad1;
1399 }
1400 tail = ohci_alloc_std(sc);
1401 if (tail == NULL) {
1402 err = USBD_NOMEM;
1403 goto bad2;
1404 }
1405 tail->xfer = NULL;
1406
1407 sed = opipe->sed;
1408 opipe->u.ctl.length = len;
1409
1410 /* Update device address and length since they may have changed. */
1411 /* XXX This only needs to be done once, but it's too early in open. */
1412 sed->ed.ed_flags = LE(
1413 (LE(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) |
1414 OHCI_ED_SET_FA(addr) |
1415 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize)));
1416
1417 /* Set up data transaction */
1418 if (len != 0) {
1419 data = ohci_alloc_std(sc);
1420 if (data == NULL) {
1421 err = USBD_NOMEM;
1422 goto bad3;
1423 }
1424 data->td.td_flags = LE(
1425 (isread ? OHCI_TD_IN : OHCI_TD_OUT) | OHCI_TD_NOCC |
1426 OHCI_TD_TOGGLE_1 | OHCI_TD_NOINTR |
1427 (xfer->flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0));
1428 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf));
1429 data->nexttd = stat;
1430 data->td.td_nexttd = LE(stat->physaddr);
1431 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
1432 data->len = len;
1433 data->xfer = xfer;
1434 data->flags = OHCI_ADD_LEN;
1435
1436 next = data;
1437 stat->flags = OHCI_CALL_DONE;
1438 } else {
1439 next = stat;
1440 /* XXX ADD_LEN? */
1441 stat->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
1442 }
1443
1444 memcpy(KERNADDR(&opipe->u.ctl.reqdma), req, sizeof *req);
1445
1446 setup->td.td_flags = LE(OHCI_TD_SETUP | OHCI_TD_NOCC |
1447 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR);
1448 setup->td.td_cbp = LE(DMAADDR(&opipe->u.ctl.reqdma));
1449 setup->nexttd = next;
1450 setup->td.td_nexttd = LE(next->physaddr);
1451 setup->td.td_be = LE(LE(setup->td.td_cbp) + sizeof *req - 1);
1452 setup->len = 0; /* XXX The number of byte we count */
1453 setup->xfer = xfer;
1454 setup->flags = 0;
1455 xfer->hcpriv = setup;
1456
1457 stat->td.td_flags = LE(
1458 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | OHCI_TD_NOCC |
1459 OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1));
1460 stat->td.td_cbp = 0;
1461 stat->nexttd = tail;
1462 stat->td.td_nexttd = LE(tail->physaddr);
1463 stat->td.td_be = 0;
1464 stat->len = 0;
1465 stat->xfer = xfer;
1466
1467 #ifdef OHCI_DEBUG
1468 if (ohcidebug > 5) {
1469 DPRINTF(("ohci_device_request:\n"));
1470 ohci_dump_ed(sed);
1471 ohci_dump_tds(setup);
1472 }
1473 #endif
1474
1475 /* Insert ED in schedule */
1476 s = splusb();
1477 sed->ed.ed_tailp = LE(tail->physaddr);
1478 opipe->tail.td = tail;
1479 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF);
1480 if (xfer->timeout && !sc->sc_bus.use_polling) {
1481 usb_timeout(ohci_timeout, xfer,
1482 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
1483 }
1484 splx(s);
1485
1486 #ifdef OHCI_DEBUG
1487 if (ohcidebug > 5) {
1488 usb_delay_ms(&sc->sc_bus, 5);
1489 DPRINTF(("ohci_device_request: status=%x\n",
1490 OREAD4(sc, OHCI_COMMAND_STATUS)));
1491 ohci_dump_ed(sed);
1492 ohci_dump_tds(setup);
1493 }
1494 #endif
1495
1496 return (USBD_NORMAL_COMPLETION);
1497
1498 bad3:
1499 ohci_free_std(sc, tail);
1500 bad2:
1501 ohci_free_std(sc, stat);
1502 bad1:
1503 return (err);
1504 }
1505
1506 /*
1507 * Add an ED to the schedule. Called at splusb().
1508 */
1509 void
1510 ohci_add_ed(sed, head)
1511 ohci_soft_ed_t *sed;
1512 ohci_soft_ed_t *head;
1513 {
1514 SPLUSBCHECK;
1515 sed->next = head->next;
1516 sed->ed.ed_nexted = head->ed.ed_nexted;
1517 head->next = sed;
1518 head->ed.ed_nexted = LE(sed->physaddr);
1519 }
1520
1521 /*
1522 * Remove an ED from the schedule. Called at splusb().
1523 */
1524 void
1525 ohci_rem_ed(sed, head)
1526 ohci_soft_ed_t *sed;
1527 ohci_soft_ed_t *head;
1528 {
1529 ohci_soft_ed_t *p;
1530
1531 SPLUSBCHECK;
1532
1533 /* XXX */
1534 for (p = head; p == NULL && p->next != sed; p = p->next)
1535 ;
1536 if (p == NULL)
1537 panic("ohci_rem_ed: ED not found\n");
1538 p->next = sed->next;
1539 p->ed.ed_nexted = sed->ed.ed_nexted;
1540 }
1541
1542 /*
1543 * When a transfer is completed the TD is added to the done queue by
1544 * the host controller. This queue is the processed by software.
1545 * Unfortunately the queue contains the physical address of the TD
1546 * and we have no simple way to translate this back to a kernel address.
1547 * To make the translation possible (and fast) we use a hash table of
1548 * TDs currently in the schedule. The physical address is used as the
1549 * hash value.
1550 */
1551
1552 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE)
1553 /* Called at splusb() */
1554 void
1555 ohci_hash_add_td(sc, std)
1556 ohci_softc_t *sc;
1557 ohci_soft_td_t *std;
1558 {
1559 int h = HASH(std->physaddr);
1560
1561 SPLUSBCHECK;
1562
1563 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext);
1564 }
1565
1566 /* Called at splusb() */
1567 void
1568 ohci_hash_rem_td(sc, std)
1569 ohci_softc_t *sc;
1570 ohci_soft_td_t *std;
1571 {
1572 SPLUSBCHECK;
1573
1574 LIST_REMOVE(std, hnext);
1575 }
1576
1577 ohci_soft_td_t *
1578 ohci_hash_find_td(sc, a)
1579 ohci_softc_t *sc;
1580 ohci_physaddr_t a;
1581 {
1582 int h = HASH(a);
1583 ohci_soft_td_t *std;
1584
1585 for (std = LIST_FIRST(&sc->sc_hash_tds[h]);
1586 std != NULL;
1587 std = LIST_NEXT(std, hnext))
1588 if (std->physaddr == a)
1589 return (std);
1590 panic("ohci_hash_find_td: addr 0x%08lx not found\n", (u_long)a);
1591 }
1592
1593 void
1594 ohci_timeout(addr)
1595 void *addr;
1596 {
1597 usbd_xfer_handle xfer = addr;
1598 int s;
1599
1600 DPRINTF(("ohci_timeout: xfer=%p\n", xfer));
1601
1602 s = splusb();
1603 xfer->device->bus->intr_context++;
1604 ohci_abort_xfer(xfer, USBD_TIMEOUT);
1605 xfer->device->bus->intr_context--;
1606 splx(s);
1607 }
1608
1609 #ifdef OHCI_DEBUG
1610 void
1611 ohci_dump_tds(std)
1612 ohci_soft_td_t *std;
1613 {
1614 for (; std; std = std->nexttd)
1615 ohci_dump_td(std);
1616 }
1617
1618 void
1619 ohci_dump_td(std)
1620 ohci_soft_td_t *std;
1621 {
1622 DPRINTF(("TD(%p) at %08lx: %b delay=%d ec=%d cc=%d\ncbp=0x%08lx "
1623 "nexttd=0x%08lx be=0x%08lx\n",
1624 std, (u_long)std->physaddr,
1625 (int)LE(std->td.td_flags),
1626 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE",
1627 OHCI_TD_GET_DI(LE(std->td.td_flags)),
1628 OHCI_TD_GET_EC(LE(std->td.td_flags)),
1629 OHCI_TD_GET_CC(LE(std->td.td_flags)),
1630 (u_long)LE(std->td.td_cbp),
1631 (u_long)LE(std->td.td_nexttd), (u_long)LE(std->td.td_be)));
1632 }
1633
1634 void
1635 ohci_dump_ed(sed)
1636 ohci_soft_ed_t *sed;
1637 {
1638 DPRINTF(("ED(%p) at %08lx: addr=%d endpt=%d maxp=%d %b\ntailp=0x%08lx "
1639 "headp=%b nexted=0x%08lx\n",
1640 sed, (u_long)sed->physaddr,
1641 OHCI_ED_GET_FA(LE(sed->ed.ed_flags)),
1642 OHCI_ED_GET_EN(LE(sed->ed.ed_flags)),
1643 OHCI_ED_GET_MAXP(LE(sed->ed.ed_flags)),
1644 (int)LE(sed->ed.ed_flags),
1645 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO",
1646 (u_long)LE(sed->ed.ed_tailp),
1647 (u_long)LE(sed->ed.ed_headp),
1648 "\20\1HALT\2CARRY",
1649 (u_long)LE(sed->ed.ed_nexted)));
1650 }
1651 #endif
1652
1653 usbd_status
1654 ohci_open(pipe)
1655 usbd_pipe_handle pipe;
1656 {
1657 usbd_device_handle dev = pipe->device;
1658 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
1659 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
1660 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1661 u_int8_t addr = dev->address;
1662 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE;
1663 ohci_soft_ed_t *sed;
1664 ohci_soft_td_t *std;
1665 ohci_soft_itd_t *sitd;
1666 ohci_physaddr_t tdphys;
1667 u_int32_t fmt;
1668 usbd_status err;
1669 int s;
1670 int ival;
1671
1672 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
1673 pipe, addr, ed->bEndpointAddress, sc->sc_addr));
1674 if (addr == sc->sc_addr) {
1675 switch (ed->bEndpointAddress) {
1676 case USB_CONTROL_ENDPOINT:
1677 pipe->methods = &ohci_root_ctrl_methods;
1678 break;
1679 case UE_DIR_IN | OHCI_INTR_ENDPT:
1680 pipe->methods = &ohci_root_intr_methods;
1681 break;
1682 default:
1683 return (USBD_INVAL);
1684 }
1685 } else {
1686 sed = ohci_alloc_sed(sc);
1687 if (sed == NULL)
1688 goto bad0;
1689 opipe->sed = sed;
1690 if (xfertype == UE_ISOCHRONOUS) {
1691 sitd = ohci_alloc_sitd(sc);
1692 if (sitd == NULL) {
1693 ohci_free_sitd(sc, sitd);
1694 goto bad1;
1695 }
1696 opipe->tail.itd = sitd;
1697 tdphys = LE(sitd->physaddr);
1698 fmt = OHCI_ED_FORMAT_ISO;
1699 } else {
1700 std = ohci_alloc_std(sc);
1701 if (std == NULL) {
1702 ohci_free_std(sc, std);
1703 goto bad1;
1704 }
1705 opipe->tail.td = std;
1706 tdphys = LE(std->physaddr);
1707 fmt = OHCI_ED_FORMAT_GEN;
1708 }
1709 sed->ed.ed_flags = LE(
1710 OHCI_ED_SET_FA(addr) |
1711 OHCI_ED_SET_EN(ed->bEndpointAddress) |
1712 OHCI_ED_DIR_TD |
1713 (dev->lowspeed ? OHCI_ED_SPEED : 0) | fmt |
1714 OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize)));
1715 sed->ed.ed_headp = sed->ed.ed_tailp = tdphys;
1716
1717 switch (xfertype) {
1718 case UE_CONTROL:
1719 pipe->methods = &ohci_device_ctrl_methods;
1720 err = usb_allocmem(&sc->sc_bus,
1721 sizeof(usb_device_request_t),
1722 0, &opipe->u.ctl.reqdma);
1723 if (err)
1724 goto bad;
1725 s = splusb();
1726 ohci_add_ed(sed, sc->sc_ctrl_head);
1727 splx(s);
1728 break;
1729 case UE_INTERRUPT:
1730 pipe->methods = &ohci_device_intr_methods;
1731 ival = pipe->interval;
1732 if (ival == USBD_DEFAULT_INTERVAL)
1733 ival = ed->bInterval;
1734 return (ohci_device_setintr(sc, opipe, ival));
1735 case UE_ISOCHRONOUS:
1736 pipe->methods = &ohci_device_isoc_methods;
1737 return (ohci_setup_isoc(pipe));
1738 case UE_BULK:
1739 pipe->methods = &ohci_device_bulk_methods;
1740 s = splusb();
1741 ohci_add_ed(sed, sc->sc_bulk_head);
1742 splx(s);
1743 break;
1744 }
1745 }
1746 return (USBD_NORMAL_COMPLETION);
1747
1748 bad:
1749 ohci_free_std(sc, std);
1750 bad1:
1751 ohci_free_sed(sc, sed);
1752 bad0:
1753 return (USBD_NOMEM);
1754
1755 }
1756
1757 /*
1758 * Close a reqular pipe.
1759 * Assumes that there are no pending transactions.
1760 */
1761 void
1762 ohci_close_pipe(pipe, head)
1763 usbd_pipe_handle pipe;
1764 ohci_soft_ed_t *head;
1765 {
1766 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
1767 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
1768 ohci_soft_ed_t *sed = opipe->sed;
1769 int s;
1770
1771 s = splusb();
1772 #ifdef DIAGNOSTIC
1773 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
1774 if ((sed->ed.ed_tailp & LE(OHCI_HEADMASK)) !=
1775 (sed->ed.ed_headp & LE(OHCI_HEADMASK))) {
1776 ohci_physaddr_t td = sed->ed.ed_headp;
1777 ohci_soft_td_t *std;
1778 for (std = LIST_FIRST(&sc->sc_hash_tds[HASH(td)]);
1779 std != NULL;
1780 std = LIST_NEXT(std, hnext))
1781 if (std->physaddr == td)
1782 break;
1783 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x "
1784 "tl=0x%x pipe=%p, std=%p\n", sed,
1785 (int)LE(sed->ed.ed_headp), (int)LE(sed->ed.ed_tailp),
1786 pipe, std);
1787 usb_delay_ms(&sc->sc_bus, 2);
1788 if ((sed->ed.ed_tailp & LE(OHCI_HEADMASK)) !=
1789 (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
1790 printf("ohci_close_pipe: pipe still not empty\n");
1791 }
1792 #endif
1793 ohci_rem_ed(sed, head);
1794 splx(s);
1795 ohci_free_sed(sc, opipe->sed);
1796 }
1797
1798 /*
1799 * Abort a device request.
1800 * If this routine is called at splusb() it guarantees that the request
1801 * will be removed from the hardware scheduling and that the callback
1802 * for it will be called with USBD_CANCELLED status.
1803 * It's impossible to guarantee that the requested transfer will not
1804 * have happened since the hardware runs concurrently.
1805 * If the transaction has already happened we rely on the ordinary
1806 * interrupt processing to process it.
1807 */
1808 void
1809 ohci_abort_xfer(xfer, status)
1810 usbd_xfer_handle xfer;
1811 usbd_status status;
1812 {
1813 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1814 ohci_soft_ed_t *sed;
1815
1816 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p\n", xfer, opipe));
1817
1818 xfer->status = status;
1819
1820 usb_untimeout(ohci_timeout, xfer, xfer->timo_handle);
1821
1822 sed = opipe->sed;
1823 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed));
1824 sed->ed.ed_flags |= LE(OHCI_ED_SKIP); /* force hardware skip */
1825
1826 #if 1
1827 if (xfer->device->bus->intr_context) {
1828 /* We have no process context, so we can't use tsleep(). */
1829 timeout(ohci_abort_xfer_end, xfer, hz / USB_FRAMES_PER_SECOND);
1830 } else {
1831 #if defined(DIAGNOSTIC) && defined(__i386__) && defined(__FreeBSD__)
1832 KASSERT(intr_nesting_level == 0,
1833 ("ohci_abort_req in interrupt context"));
1834 #endif
1835 usb_delay_ms(opipe->pipe.device->bus, 1);
1836 ohci_abort_xfer_end(xfer);
1837 }
1838 #else
1839 delay(1000);
1840 ohci_abort_xfer_end(xfer);
1841 #endif
1842 }
1843
1844 void
1845 ohci_abort_xfer_end(v)
1846 void *v;
1847 {
1848 usbd_xfer_handle xfer = v;
1849 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
1850 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus;
1851 ohci_soft_ed_t *sed;
1852 ohci_soft_td_t *p, *n;
1853 int s;
1854
1855 s = splusb();
1856
1857 p = xfer->hcpriv;
1858 #ifdef DIAGNOSTIC
1859 if (p == NULL) {
1860 printf("ohci_abort_xfer: hcpriv==0\n");
1861 return;
1862 }
1863 #endif
1864 for (; p->xfer == xfer; p = n) {
1865 n = p->nexttd;
1866 ohci_free_std(sc, p);
1867 }
1868
1869 sed = opipe->sed;
1870 DPRINTFN(2,("ohci_abort_xfer: set hd=%x, tl=%x\n",
1871 (int)LE(p->physaddr), (int)LE(sed->ed.ed_tailp)));
1872 sed->ed.ed_headp = p->physaddr; /* unlink TDs */
1873 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP); /* remove hardware skip */
1874
1875 usb_transfer_complete(xfer);
1876
1877 splx(s);
1878 }
1879
1880 /*
1881 * Data structures and routines to emulate the root hub.
1882 */
1883 static usb_device_descriptor_t ohci_devd = {
1884 USB_DEVICE_DESCRIPTOR_SIZE,
1885 UDESC_DEVICE, /* type */
1886 {0x00, 0x01}, /* USB version */
1887 UCLASS_HUB, /* class */
1888 USUBCLASS_HUB, /* subclass */
1889 0, /* protocol */
1890 64, /* max packet */
1891 {0},{0},{0x00,0x01}, /* device id */
1892 1,2,0, /* string indicies */
1893 1 /* # of configurations */
1894 };
1895
1896 static usb_config_descriptor_t ohci_confd = {
1897 USB_CONFIG_DESCRIPTOR_SIZE,
1898 UDESC_CONFIG,
1899 {USB_CONFIG_DESCRIPTOR_SIZE +
1900 USB_INTERFACE_DESCRIPTOR_SIZE +
1901 USB_ENDPOINT_DESCRIPTOR_SIZE},
1902 1,
1903 1,
1904 0,
1905 UC_SELF_POWERED,
1906 0 /* max power */
1907 };
1908
1909 static usb_interface_descriptor_t ohci_ifcd = {
1910 USB_INTERFACE_DESCRIPTOR_SIZE,
1911 UDESC_INTERFACE,
1912 0,
1913 0,
1914 1,
1915 UCLASS_HUB,
1916 USUBCLASS_HUB,
1917 0,
1918 0
1919 };
1920
1921 static usb_endpoint_descriptor_t ohci_endpd = {
1922 USB_ENDPOINT_DESCRIPTOR_SIZE,
1923 UDESC_ENDPOINT,
1924 UE_DIR_IN | OHCI_INTR_ENDPT,
1925 UE_INTERRUPT,
1926 {8, 0}, /* max packet */
1927 255
1928 };
1929
1930 static usb_hub_descriptor_t ohci_hubd = {
1931 USB_HUB_DESCRIPTOR_SIZE,
1932 UDESC_HUB,
1933 0,
1934 {0,0},
1935 0,
1936 0,
1937 {0},
1938 };
1939
1940 static int
1941 ohci_str(p, l, s)
1942 usb_string_descriptor_t *p;
1943 int l;
1944 char *s;
1945 {
1946 int i;
1947
1948 if (l == 0)
1949 return (0);
1950 p->bLength = 2 * strlen(s) + 2;
1951 if (l == 1)
1952 return (1);
1953 p->bDescriptorType = UDESC_STRING;
1954 l -= 2;
1955 for (i = 0; s[i] && l > 1; i++, l -= 2)
1956 USETW2(p->bString[i], 0, s[i]);
1957 return (2*i+2);
1958 }
1959
1960 /*
1961 * Simulate a hardware hub by handling all the necessary requests.
1962 */
1963 static usbd_status
1964 ohci_root_ctrl_transfer(xfer)
1965 usbd_xfer_handle xfer;
1966 {
1967 usbd_status err;
1968
1969 /* Insert last in queue. */
1970 err = usb_insert_transfer(xfer);
1971 if (err)
1972 return (err);
1973
1974 /* Pipe isn't running, start first */
1975 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1976 }
1977
1978 static usbd_status
1979 ohci_root_ctrl_start(xfer)
1980 usbd_xfer_handle xfer;
1981 {
1982 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
1983 usb_device_request_t *req;
1984 void *buf = NULL;
1985 int port, i;
1986 int s, len, value, index, l, totlen = 0;
1987 usb_port_status_t ps;
1988 usb_hub_descriptor_t hubd;
1989 usbd_status err;
1990 u_int32_t v;
1991
1992 #ifdef DIAGNOSTIC
1993 if (!(xfer->rqflags & URQ_REQUEST))
1994 /* XXX panic */
1995 return (USBD_INVAL);
1996 #endif
1997 req = &xfer->request;
1998
1999 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n",
2000 req->bmRequestType, req->bRequest));
2001
2002 len = UGETW(req->wLength);
2003 value = UGETW(req->wValue);
2004 index = UGETW(req->wIndex);
2005
2006 if (len != 0)
2007 buf = KERNADDR(&xfer->dmabuf);
2008
2009 #define C(x,y) ((x) | ((y) << 8))
2010 switch(C(req->bRequest, req->bmRequestType)) {
2011 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
2012 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
2013 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
2014 /*
2015 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
2016 * for the integrated root hub.
2017 */
2018 break;
2019 case C(UR_GET_CONFIG, UT_READ_DEVICE):
2020 if (len > 0) {
2021 *(u_int8_t *)buf = sc->sc_conf;
2022 totlen = 1;
2023 }
2024 break;
2025 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
2026 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value));
2027 switch(value >> 8) {
2028 case UDESC_DEVICE:
2029 if ((value & 0xff) != 0) {
2030 err = USBD_IOERROR;
2031 goto ret;
2032 }
2033 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
2034 USETW(ohci_devd.idVendor, sc->sc_id_vendor);
2035 memcpy(buf, &ohci_devd, l);
2036 break;
2037 case UDESC_CONFIG:
2038 if ((value & 0xff) != 0) {
2039 err = USBD_IOERROR;
2040 goto ret;
2041 }
2042 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
2043 memcpy(buf, &ohci_confd, l);
2044 buf = (char *)buf + l;
2045 len -= l;
2046 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
2047 totlen += l;
2048 memcpy(buf, &ohci_ifcd, l);
2049 buf = (char *)buf + l;
2050 len -= l;
2051 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
2052 totlen += l;
2053 memcpy(buf, &ohci_endpd, l);
2054 break;
2055 case UDESC_STRING:
2056 if (len == 0)
2057 break;
2058 *(u_int8_t *)buf = 0;
2059 totlen = 1;
2060 switch (value & 0xff) {
2061 case 1: /* Vendor */
2062 totlen = ohci_str(buf, len, sc->sc_vendor);
2063 break;
2064 case 2: /* Product */
2065 totlen = ohci_str(buf, len, "OHCI root hub");
2066 break;
2067 }
2068 break;
2069 default:
2070 err = USBD_IOERROR;
2071 goto ret;
2072 }
2073 break;
2074 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
2075 if (len > 0) {
2076 *(u_int8_t *)buf = 0;
2077 totlen = 1;
2078 }
2079 break;
2080 case C(UR_GET_STATUS, UT_READ_DEVICE):
2081 if (len > 1) {
2082 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
2083 totlen = 2;
2084 }
2085 break;
2086 case C(UR_GET_STATUS, UT_READ_INTERFACE):
2087 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
2088 if (len > 1) {
2089 USETW(((usb_status_t *)buf)->wStatus, 0);
2090 totlen = 2;
2091 }
2092 break;
2093 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
2094 if (value >= USB_MAX_DEVICES) {
2095 err = USBD_IOERROR;
2096 goto ret;
2097 }
2098 sc->sc_addr = value;
2099 break;
2100 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
2101 if (value != 0 && value != 1) {
2102 err = USBD_IOERROR;
2103 goto ret;
2104 }
2105 sc->sc_conf = value;
2106 break;
2107 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
2108 break;
2109 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
2110 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
2111 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
2112 err = USBD_IOERROR;
2113 goto ret;
2114 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
2115 break;
2116 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
2117 break;
2118 /* Hub requests */
2119 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
2120 break;
2121 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
2122 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
2123 "port=%d feature=%d\n",
2124 index, value));
2125 if (index < 1 || index > sc->sc_noport) {
2126 err = USBD_IOERROR;
2127 goto ret;
2128 }
2129 port = OHCI_RH_PORT_STATUS(index);
2130 switch(value) {
2131 case UHF_PORT_ENABLE:
2132 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS);
2133 break;
2134 case UHF_PORT_SUSPEND:
2135 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR);
2136 break;
2137 case UHF_PORT_POWER:
2138 OWRITE4(sc, port, UPS_LOW_SPEED);
2139 break;
2140 case UHF_C_PORT_CONNECTION:
2141 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16);
2142 break;
2143 case UHF_C_PORT_ENABLE:
2144 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16);
2145 break;
2146 case UHF_C_PORT_SUSPEND:
2147 OWRITE4(sc, port, UPS_C_SUSPEND << 16);
2148 break;
2149 case UHF_C_PORT_OVER_CURRENT:
2150 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16);
2151 break;
2152 case UHF_C_PORT_RESET:
2153 OWRITE4(sc, port, UPS_C_PORT_RESET << 16);
2154 break;
2155 default:
2156 err = USBD_IOERROR;
2157 goto ret;
2158 }
2159 switch(value) {
2160 case UHF_C_PORT_CONNECTION:
2161 case UHF_C_PORT_ENABLE:
2162 case UHF_C_PORT_SUSPEND:
2163 case UHF_C_PORT_OVER_CURRENT:
2164 case UHF_C_PORT_RESET:
2165 /* Enable RHSC interrupt if condition is cleared. */
2166 if ((OREAD4(sc, port) >> 16) == 0)
2167 ohci_rhsc_able(sc, 1);
2168 break;
2169 default:
2170 break;
2171 }
2172 break;
2173 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
2174 if (value != 0) {
2175 err = USBD_IOERROR;
2176 goto ret;
2177 }
2178 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A);
2179 hubd = ohci_hubd;
2180 hubd.bNbrPorts = sc->sc_noport;
2181 USETW(hubd.wHubCharacteristics,
2182 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH :
2183 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL)
2184 /* XXX overcurrent */
2185 );
2186 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v);
2187 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B);
2188 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8)
2189 hubd.DeviceRemovable[i++] = (u_int8_t)v;
2190 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i;
2191 l = min(len, hubd.bDescLength);
2192 totlen = l;
2193 memcpy(buf, &hubd, l);
2194 break;
2195 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
2196 if (len != 4) {
2197 err = USBD_IOERROR;
2198 goto ret;
2199 }
2200 memset(buf, 0, len); /* ? XXX */
2201 totlen = len;
2202 break;
2203 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
2204 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n",
2205 index));
2206 if (index < 1 || index > sc->sc_noport) {
2207 err = USBD_IOERROR;
2208 goto ret;
2209 }
2210 if (len != 4) {
2211 err = USBD_IOERROR;
2212 goto ret;
2213 }
2214 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index));
2215 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n",
2216 v));
2217 USETW(ps.wPortStatus, v);
2218 USETW(ps.wPortChange, v >> 16);
2219 l = min(len, sizeof ps);
2220 memcpy(buf, &ps, l);
2221 totlen = l;
2222 break;
2223 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
2224 err = USBD_IOERROR;
2225 goto ret;
2226 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
2227 break;
2228 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
2229 if (index < 1 || index > sc->sc_noport) {
2230 err = USBD_IOERROR;
2231 goto ret;
2232 }
2233 port = OHCI_RH_PORT_STATUS(index);
2234 switch(value) {
2235 case UHF_PORT_ENABLE:
2236 OWRITE4(sc, port, UPS_PORT_ENABLED);
2237 break;
2238 case UHF_PORT_SUSPEND:
2239 OWRITE4(sc, port, UPS_SUSPEND);
2240 break;
2241 case UHF_PORT_RESET:
2242 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n",
2243 index));
2244 OWRITE4(sc, port, UPS_RESET);
2245 for (i = 0; i < 10; i++) {
2246 usb_delay_ms(&sc->sc_bus, 10);
2247 if ((OREAD4(sc, port) & UPS_RESET) == 0)
2248 break;
2249 }
2250 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n",
2251 index, OREAD4(sc, port)));
2252 break;
2253 case UHF_PORT_POWER:
2254 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power "
2255 "%d\n", index));
2256 OWRITE4(sc, port, UPS_PORT_POWER);
2257 break;
2258 default:
2259 err = USBD_IOERROR;
2260 goto ret;
2261 }
2262 break;
2263 default:
2264 err = USBD_IOERROR;
2265 goto ret;
2266 }
2267 xfer->actlen = totlen;
2268 err = USBD_NORMAL_COMPLETION;
2269 ret:
2270 xfer->status = err;
2271 s = splusb();
2272 usb_transfer_complete(xfer);
2273 splx(s);
2274 return (USBD_IN_PROGRESS);
2275 }
2276
2277 /* Abort a root control request. */
2278 static void
2279 ohci_root_ctrl_abort(xfer)
2280 usbd_xfer_handle xfer;
2281 {
2282 /* Nothing to do, all transfers are synchronous. */
2283 }
2284
2285 /* Close the root pipe. */
2286 static void
2287 ohci_root_ctrl_close(pipe)
2288 usbd_pipe_handle pipe;
2289 {
2290 DPRINTF(("ohci_root_ctrl_close\n"));
2291 /* Nothing to do. */
2292 }
2293
2294 static usbd_status
2295 ohci_root_intr_transfer(xfer)
2296 usbd_xfer_handle xfer;
2297 {
2298 usbd_status err;
2299
2300 /* Insert last in queue. */
2301 err = usb_insert_transfer(xfer);
2302 if (err)
2303 return (err);
2304
2305 /* Pipe isn't running, start first */
2306 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2307 }
2308
2309 static usbd_status
2310 ohci_root_intr_start(xfer)
2311 usbd_xfer_handle xfer;
2312 {
2313 usbd_pipe_handle pipe = xfer->pipe;
2314 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2315
2316 sc->sc_intrxfer = xfer;
2317
2318 return (USBD_IN_PROGRESS);
2319 }
2320
2321 /* Abort a root interrupt request. */
2322 static void
2323 ohci_root_intr_abort(xfer)
2324 usbd_xfer_handle xfer;
2325 {
2326 int s;
2327
2328 if (xfer->pipe->intrxfer == xfer) {
2329 DPRINTF(("ohci_root_intr_abort: remove\n"));
2330 xfer->pipe->intrxfer = NULL;
2331 }
2332 xfer->status = USBD_CANCELLED;
2333 s = splusb();
2334 usb_transfer_complete(xfer);
2335 splx(s);
2336 }
2337
2338 /* Close the root pipe. */
2339 static void
2340 ohci_root_intr_close(pipe)
2341 usbd_pipe_handle pipe;
2342 {
2343 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2344
2345 DPRINTF(("ohci_root_intr_close\n"));
2346
2347 sc->sc_intrxfer = NULL;
2348 }
2349
2350 /************************/
2351
2352 static usbd_status
2353 ohci_device_ctrl_transfer(xfer)
2354 usbd_xfer_handle xfer;
2355 {
2356 usbd_status err;
2357
2358 /* Insert last in queue. */
2359 err = usb_insert_transfer(xfer);
2360 if (err)
2361 return (err);
2362
2363 /* Pipe isn't running, start first */
2364 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2365 }
2366
2367 static usbd_status
2368 ohci_device_ctrl_start(xfer)
2369 usbd_xfer_handle xfer;
2370 {
2371 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus;
2372 usbd_status err;
2373
2374 #ifdef DIAGNOSTIC
2375 if (!(xfer->rqflags & URQ_REQUEST)) {
2376 /* XXX panic */
2377 printf("ohci_device_ctrl_transfer: not a request\n");
2378 return (USBD_INVAL);
2379 }
2380 #endif
2381
2382 err = ohci_device_request(xfer);
2383 if (err)
2384 return (err);
2385
2386 if (sc->sc_bus.use_polling)
2387 ohci_waitintr(sc, xfer);
2388 return (USBD_IN_PROGRESS);
2389 }
2390
2391 /* Abort a device control request. */
2392 static void
2393 ohci_device_ctrl_abort(xfer)
2394 usbd_xfer_handle xfer;
2395 {
2396 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer));
2397 ohci_abort_xfer(xfer, USBD_CANCELLED);
2398 }
2399
2400 /* Close a device control pipe. */
2401 static void
2402 ohci_device_ctrl_close(pipe)
2403 usbd_pipe_handle pipe;
2404 {
2405 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2406 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2407
2408 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe));
2409 ohci_close_pipe(pipe, sc->sc_ctrl_head);
2410 ohci_free_std(sc, opipe->tail.td);
2411 }
2412
2413 /************************/
2414
2415 static void
2416 ohci_device_clear_toggle(pipe)
2417 usbd_pipe_handle pipe;
2418 {
2419 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2420
2421 opipe->sed->ed.ed_headp &= LE(~OHCI_TOGGLECARRY);
2422 }
2423
2424 static void
2425 ohci_noop(pipe)
2426 usbd_pipe_handle pipe;
2427 {
2428 }
2429
2430 static usbd_status
2431 ohci_device_bulk_transfer(xfer)
2432 usbd_xfer_handle xfer;
2433 {
2434 usbd_status err;
2435
2436 /* Insert last in queue. */
2437 err = usb_insert_transfer(xfer);
2438 if (err)
2439 return (err);
2440
2441 /* Pipe isn't running, start first */
2442 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2443 }
2444
2445 static usbd_status
2446 ohci_device_bulk_start(xfer)
2447 usbd_xfer_handle xfer;
2448 {
2449 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2450 usbd_device_handle dev = opipe->pipe.device;
2451 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2452 int addr = dev->address;
2453 ohci_soft_td_t *data, *tail, *tdp;
2454 ohci_soft_ed_t *sed;
2455 int s, len, isread, endpt;
2456 usbd_status err;
2457
2458 #ifdef DIAGNOSTIC
2459 if (xfer->rqflags & URQ_REQUEST) {
2460 /* XXX panic */
2461 printf("ohci_device_bulk_start: a request\n");
2462 return (USBD_INVAL);
2463 }
2464 #endif
2465
2466 len = xfer->length;
2467 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress;
2468 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
2469 sed = opipe->sed;
2470
2471 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%d isread=%d "
2472 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags,
2473 endpt));
2474
2475 opipe->u.bulk.isread = isread;
2476 opipe->u.bulk.length = len;
2477
2478 /* Update device address */
2479 sed->ed.ed_flags = LE(
2480 (LE(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) |
2481 OHCI_ED_SET_FA(addr));
2482
2483 /* Allocate a chain of new TDs (including a new tail). */
2484 data = opipe->tail.td;
2485 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer->flags,
2486 &xfer->dmabuf, data, &tail);
2487 if (err)
2488 return (err);
2489
2490 tail->xfer = NULL;
2491 xfer->hcpriv = data;
2492
2493 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x "
2494 "td_cbp=0x%08x td_be=0x%08x\n",
2495 (int)LE(sed->ed.ed_flags), (int)LE(data->td.td_flags),
2496 (int)LE(data->td.td_cbp), (int)LE(data->td.td_be)));
2497
2498 #ifdef OHCI_DEBUG
2499 if (ohcidebug > 4) {
2500 ohci_dump_ed(sed);
2501 ohci_dump_tds(data);
2502 }
2503 #endif
2504
2505 /* Insert ED in schedule */
2506 s = splusb();
2507 for (tdp = data; tdp != tail; tdp = tdp->nexttd) {
2508 tdp->xfer = xfer;
2509 }
2510 sed->ed.ed_tailp = LE(tail->physaddr);
2511 opipe->tail.td = tail;
2512 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2513 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF);
2514 if (xfer->timeout && !sc->sc_bus.use_polling) {
2515 usb_timeout(ohci_timeout, xfer,
2516 MS_TO_TICKS(xfer->timeout), xfer->timo_handle);
2517 }
2518
2519 #if 0
2520 /* This goes wrong if we are too slow. */
2521 if (ohcidebug > 5) {
2522 usb_delay_ms(&sc->sc_bus, 5);
2523 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2524 OREAD4(sc, OHCI_COMMAND_STATUS)));
2525 ohci_dump_ed(sed);
2526 ohci_dump_tds(data);
2527 }
2528 #endif
2529
2530 splx(s);
2531
2532 return (USBD_IN_PROGRESS);
2533 }
2534
2535 static void
2536 ohci_device_bulk_abort(xfer)
2537 usbd_xfer_handle xfer;
2538 {
2539 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer));
2540 ohci_abort_xfer(xfer, USBD_CANCELLED);
2541 }
2542
2543 /*
2544 * Close a device bulk pipe.
2545 */
2546 static void
2547 ohci_device_bulk_close(pipe)
2548 usbd_pipe_handle pipe;
2549 {
2550 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2551 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2552
2553 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe));
2554 ohci_close_pipe(pipe, sc->sc_bulk_head);
2555 ohci_free_std(sc, opipe->tail.td);
2556 }
2557
2558 /************************/
2559
2560 static usbd_status
2561 ohci_device_intr_transfer(xfer)
2562 usbd_xfer_handle xfer;
2563 {
2564 usbd_status err;
2565
2566 /* Insert last in queue. */
2567 err = usb_insert_transfer(xfer);
2568 if (err)
2569 return (err);
2570
2571 /* Pipe isn't running, start first */
2572 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2573 }
2574
2575 static usbd_status
2576 ohci_device_intr_start(xfer)
2577 usbd_xfer_handle xfer;
2578 {
2579 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2580 usbd_device_handle dev = opipe->pipe.device;
2581 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2582 ohci_soft_ed_t *sed = opipe->sed;
2583 ohci_soft_td_t *data, *tail;
2584 int len;
2585 int s;
2586
2587 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%d "
2588 "flags=%d priv=%p\n",
2589 xfer, xfer->length, xfer->flags, xfer->priv));
2590
2591 #ifdef DIAGNOSTIC
2592 if (xfer->rqflags & URQ_REQUEST)
2593 panic("ohci_device_intr_transfer: a request\n");
2594 #endif
2595
2596 len = xfer->length;
2597
2598 data = opipe->tail.td;
2599 tail = ohci_alloc_std(sc);
2600 if (tail == NULL)
2601 return (USBD_NOMEM);
2602 tail->xfer = NULL;
2603
2604 data->td.td_flags = LE(
2605 OHCI_TD_IN | OHCI_TD_NOCC |
2606 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY);
2607 if (xfer->flags & USBD_SHORT_XFER_OK)
2608 data->td.td_flags |= LE(OHCI_TD_R);
2609 data->td.td_cbp = LE(DMAADDR(&xfer->dmabuf));
2610 data->nexttd = tail;
2611 data->td.td_nexttd = LE(tail->physaddr);
2612 data->td.td_be = LE(LE(data->td.td_cbp) + len - 1);
2613 data->len = len;
2614 data->xfer = xfer;
2615 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN;
2616 xfer->hcpriv = data;
2617
2618 #ifdef OHCI_DEBUG
2619 if (ohcidebug > 5) {
2620 DPRINTF(("ohci_device_intr_transfer:\n"));
2621 ohci_dump_ed(sed);
2622 ohci_dump_tds(data);
2623 }
2624 #endif
2625
2626 /* Insert ED in schedule */
2627 s = splusb();
2628 sed->ed.ed_tailp = LE(tail->physaddr);
2629 opipe->tail.td = tail;
2630 sed->ed.ed_flags &= LE(~OHCI_ED_SKIP);
2631
2632 #if 0
2633 /*
2634 * This goes horribly wrong, printing thousands of descriptors,
2635 * because false references are followed due to the fact that the
2636 * TD is gone.
2637 */
2638 if (ohcidebug > 5) {
2639 usb_delay_ms(&sc->sc_bus, 5);
2640 DPRINTF(("ohci_device_intr_transfer: status=%x\n",
2641 OREAD4(sc, OHCI_COMMAND_STATUS)));
2642 ohci_dump_ed(sed);
2643 ohci_dump_tds(data);
2644 }
2645 #endif
2646 splx(s);
2647
2648 return (USBD_IN_PROGRESS);
2649 }
2650
2651 /* Abort a device control request. */
2652 static void
2653 ohci_device_intr_abort(xfer)
2654 usbd_xfer_handle xfer;
2655 {
2656 if (xfer->pipe->intrxfer == xfer) {
2657 DPRINTF(("ohci_device_intr_abort: remove\n"));
2658 xfer->pipe->intrxfer = NULL;
2659 }
2660 ohci_abort_xfer(xfer, USBD_CANCELLED);
2661 }
2662
2663 /* Close a device interrupt pipe. */
2664 static void
2665 ohci_device_intr_close(pipe)
2666 usbd_pipe_handle pipe;
2667 {
2668 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2669 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2670 int nslots = opipe->u.intr.nslots;
2671 int pos = opipe->u.intr.pos;
2672 int j;
2673 ohci_soft_ed_t *p, *sed = opipe->sed;
2674 int s;
2675
2676 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n",
2677 pipe, nslots, pos));
2678 s = splusb();
2679 sed->ed.ed_flags |= LE(OHCI_ED_SKIP);
2680 if ((sed->ed.ed_tailp & LE(OHCI_HEADMASK)) !=
2681 (sed->ed.ed_headp & LE(OHCI_HEADMASK)))
2682 usb_delay_ms(&sc->sc_bus, 2);
2683
2684 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next)
2685 ;
2686 #ifdef DIAGNOSTIC
2687 if (p == NULL)
2688 panic("ohci_device_intr_close: ED not found\n");
2689 #endif
2690 p->next = sed->next;
2691 p->ed.ed_nexted = sed->ed.ed_nexted;
2692 splx(s);
2693
2694 for (j = 0; j < nslots; j++)
2695 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS];
2696
2697 ohci_free_std(sc, opipe->tail.td);
2698 ohci_free_sed(sc, opipe->sed);
2699 }
2700
2701 static usbd_status
2702 ohci_device_setintr(sc, opipe, ival)
2703 ohci_softc_t *sc;
2704 struct ohci_pipe *opipe;
2705 int ival;
2706 {
2707 int i, j, s, best;
2708 u_int npoll, slow, shigh, nslots;
2709 u_int bestbw, bw;
2710 ohci_soft_ed_t *hsed, *sed = opipe->sed;
2711
2712 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe));
2713 if (ival == 0) {
2714 printf("ohci_setintr: 0 interval\n");
2715 return (USBD_INVAL);
2716 }
2717
2718 npoll = OHCI_NO_INTRS;
2719 while (npoll > ival)
2720 npoll /= 2;
2721 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll));
2722
2723 /*
2724 * We now know which level in the tree the ED must go into.
2725 * Figure out which slot has most bandwidth left over.
2726 * Slots to examine:
2727 * npoll
2728 * 1 0
2729 * 2 1 2
2730 * 4 3 4 5 6
2731 * 8 7 8 9 10 11 12 13 14
2732 * N (N-1) .. (N-1+N-1)
2733 */
2734 slow = npoll-1;
2735 shigh = slow + npoll;
2736 nslots = OHCI_NO_INTRS / npoll;
2737 for (best = i = slow, bestbw = ~0; i < shigh; i++) {
2738 bw = 0;
2739 for (j = 0; j < nslots; j++)
2740 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS];
2741 if (bw < bestbw) {
2742 best = i;
2743 bestbw = bw;
2744 }
2745 }
2746 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n",
2747 best, slow, shigh, bestbw));
2748
2749 s = splusb();
2750 hsed = sc->sc_eds[best];
2751 sed->next = hsed->next;
2752 sed->ed.ed_nexted = hsed->ed.ed_nexted;
2753 hsed->next = sed;
2754 hsed->ed.ed_nexted = LE(sed->physaddr);
2755 splx(s);
2756
2757 for (j = 0; j < nslots; j++)
2758 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS];
2759 opipe->u.intr.nslots = nslots;
2760 opipe->u.intr.pos = best;
2761
2762 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe));
2763 return (USBD_NORMAL_COMPLETION);
2764 }
2765
2766 /***********************/
2767
2768 usbd_status
2769 ohci_device_isoc_transfer(xfer)
2770 usbd_xfer_handle xfer;
2771 {
2772 usbd_status err;
2773
2774 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer));
2775
2776 /* Put it on our queue, */
2777 err = usb_insert_transfer(xfer);
2778
2779 /* bail out on error, */
2780 if (err && err != USBD_IN_PROGRESS)
2781 return (err);
2782
2783 /* XXX should check inuse here */
2784
2785 /* insert into schedule, */
2786 ohci_device_isoc_enter(xfer);
2787
2788 /* and put on interrupt list if the pipe wasn't running */
2789 if (!err)
2790 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2791
2792 return (err);
2793 }
2794
2795 void
2796 ohci_device_isoc_enter(xfer)
2797 usbd_xfer_handle xfer;
2798 {
2799 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe;
2800 usbd_device_handle dev = opipe->pipe.device;
2801 ohci_softc_t *sc = (ohci_softc_t *)dev->bus;
2802 ohci_soft_ed_t *sed = opipe->sed;
2803 struct iso *iso = &opipe->u.iso;
2804 ohci_soft_itd_t *sitd, *nsitd;
2805 ohci_physaddr_t buf, offs;
2806 int i, ncur, nframes;
2807 int ncross;
2808 int s;
2809
2810 s = splusb();
2811 sitd = opipe->tail.itd;
2812 buf = DMAADDR(&xfer->dmabuf);
2813 sitd->itd.itd_bp0 = LE(buf & OHCI_ITD_PAGE_MASK);
2814 nframes = xfer->nframes;
2815 offs = buf & OHCI_ITD_OFFSET_MASK;
2816 for (i = ncur = 0; i < nframes; i++, ncur++) {
2817 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */
2818 ncross > 1) { /* too many page crossings */
2819
2820 nsitd = ohci_alloc_sitd(sc);
2821 if (nsitd == NULL) {
2822 /* XXX what now? */
2823 return;
2824 }
2825 sitd->nextitd = nsitd;
2826 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2827 sitd->itd.itd_flags = LE(
2828 OHCI_ITD_NOCC |
2829 OHCI_ITD_SET_SF(iso->next) |
2830 OHCI_ITD_NOINTR |
2831 OHCI_ITD_SET_FC(OHCI_ITD_NOFFSET));
2832 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2833 nsitd->itd.itd_bp0 = LE((buf + offs) & OHCI_ITD_PAGE_MASK);
2834 sitd = nsitd;
2835 iso->next = iso->next + ncur;
2836 ncur = 0;
2837 ncross = 0;
2838 }
2839 /* XXX byte order */
2840 sitd->itd.itd_offset[i] =
2841 offs | (ncross == 1 ? OHCI_ITD_PAGE_SELECT : 0);
2842 offs += xfer->frlengths[i];
2843 /* XXX update ncross */
2844 }
2845 nsitd = ohci_alloc_sitd(sc);
2846 if (nsitd == NULL) {
2847 /* XXX what now? */
2848 return;
2849 }
2850 sitd->nextitd = nsitd;
2851 sitd->itd.itd_nextitd = LE(nsitd->physaddr);
2852 sitd->itd.itd_flags = LE(
2853 OHCI_ITD_NOCC |
2854 OHCI_ITD_SET_SF(iso->next) |
2855 OHCI_ITD_SET_DI(0) |
2856 OHCI_ITD_SET_FC(ncur));
2857 sitd->itd.itd_be = LE(LE(sitd->itd.itd_bp0) + offs - 1);
2858 iso->next = iso->next + ncur;
2859
2860 opipe->tail.itd = nsitd;
2861 sed->ed.ed_tailp = LE(nsitd->physaddr);
2862 /* XXX update ED */
2863 splx(s);
2864 }
2865
2866 usbd_status
2867 ohci_device_isoc_start(xfer)
2868 usbd_xfer_handle xfer;
2869 {
2870 printf("ohci_device_isoc_start: not implemented\n");
2871 return (USBD_INVAL);
2872 }
2873
2874 void
2875 ohci_device_isoc_abort(xfer)
2876 usbd_xfer_handle xfer;
2877 {
2878 }
2879
2880 void
2881 ohci_device_isoc_done(xfer)
2882 usbd_xfer_handle xfer;
2883 {
2884 printf("ohci_device_isoc_done: not implemented\n");
2885 }
2886
2887 usbd_status
2888 ohci_setup_isoc(pipe)
2889 usbd_pipe_handle pipe;
2890 {
2891 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2892 struct iso *iso = &opipe->u.iso;
2893
2894 iso->next = -1;
2895 iso->inuse = 0;
2896
2897 return (USBD_NORMAL_COMPLETION);
2898 }
2899
2900 void
2901 ohci_device_isoc_close(pipe)
2902 usbd_pipe_handle pipe;
2903 {
2904 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe;
2905 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus;
2906
2907 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe));
2908 ohci_close_pipe(pipe, sc->sc_isoc_head);
2909 ohci_free_sitd(sc, opipe->tail.itd);
2910 }
2911