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