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