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