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