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