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