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