1 /* $NetBSD: uhci.c,v 1.160 2002/05/28 12:42:39 augustss Exp $ */
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
42 * USB Universal Host Controller driver.
43 * Handles e.g. PIIX3 and PIIX4.
45 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm
46 * USB spec: http://www.usb.org/developers/data/usbspec.zip
47 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf
48 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #if defined(__NetBSD__) || defined(__OpenBSD__)
56 #include <sys/device.h>
57 #include <sys/select.h>
58 #elif defined(__FreeBSD__)
59 #include <sys/endian.h>
60 #include <sys/module.h>
62 #include <machine/bus_pio.h>
63 #if defined(DIAGNOSTIC) && defined(__i386__)
64 #include <machine/cpu.h>
68 #include <sys/queue.h>
69 #include <sys/sysctl.h>
71 #include <machine/bus.h>
72 #include <machine/endian.h>
74 #include <dev/usb/usb.h>
75 #include <dev/usb/usbdi.h>
76 #include <dev/usb/usbdivar.h>
77 #include <dev/usb/usb_mem.h>
78 #include <dev/usb/usb_quirks.h>
80 #include <dev/usb/uhcireg.h>
81 #include <dev/usb/uhcivar.h>
83 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */
84 /*#define UHCI_CTL_LOOP */
86 #if defined(__FreeBSD__)
87 #include <machine/clock.h>
89 #define delay(d) DELAY(d)
92 #define MS_TO_TICKS(ms) ((ms) * hz / 1000)
94 #if defined(__OpenBSD__)
95 struct cfdriver uhci_cd = {
102 #define DPRINTF(x) if (uhcidebug) printf x
103 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x
106 SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci");
107 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RW,
108 &uhcidebug, 0, "uhci debug level");
109 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RW,
110 &uhcinoloop, 0, "uhci noloop");
112 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f))
116 #define DPRINTFN(n,x)
120 * The UHCI controller is little endian, so on big endian machines
121 * the data strored in memory needs to be swapped.
123 #if defined(__OpenBSD__)
124 #if BYTE_ORDER == BIG_ENDIAN
125 #define htole32(x) (bswap32(x))
126 #define le32toh(x) (bswap32(x))
128 #define htole32(x) (x)
129 #define le32toh(x) (x)
134 struct usbd_pipe pipe;
138 usbd_xfer_handle abortstart, abortend;
140 /* Info needed for different pipe kinds. */
146 uhci_soft_td_t *setup, *stat;
152 uhci_soft_qh_t **qhs;
162 uhci_soft_td_t **stds;
168 Static void uhci_globalreset(uhci_softc_t *);
169 Static void uhci_reset(uhci_softc_t *);
170 #if defined(__NetBSD__) || defined(__OpenBSD__)
171 Static void uhci_shutdown(void *v);
172 Static void uhci_power(int, void *);
174 Static usbd_status uhci_run(uhci_softc_t *, int run);
175 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *);
176 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *);
177 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *);
178 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *);
180 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *,
182 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *);
185 Static void uhci_free_std_chain(uhci_softc_t *,
186 uhci_soft_td_t *, uhci_soft_td_t *);
187 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *,
188 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *,
189 uhci_soft_td_t **, uhci_soft_td_t **);
190 Static void uhci_poll_hub(void *);
191 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle);
192 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *);
193 Static void uhci_idone(uhci_intr_info_t *);
195 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status);
197 Static void uhci_timeout(void *);
198 Static void uhci_timeout_task(void *);
199 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
200 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *);
201 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *);
202 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
203 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *);
204 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *);
205 Static int uhci_str(usb_string_descriptor_t *, int, char *);
206 Static void uhci_add_loop(uhci_softc_t *sc);
207 Static void uhci_rem_loop(uhci_softc_t *sc);
209 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe);
210 Static void uhci_device_isoc_enter(usbd_xfer_handle);
212 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t);
213 Static void uhci_freem(struct usbd_bus *, usb_dma_t *);
215 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *);
216 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle);
218 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle);
219 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle);
220 Static void uhci_device_ctrl_abort(usbd_xfer_handle);
221 Static void uhci_device_ctrl_close(usbd_pipe_handle);
222 Static void uhci_device_ctrl_done(usbd_xfer_handle);
224 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle);
225 Static usbd_status uhci_device_intr_start(usbd_xfer_handle);
226 Static void uhci_device_intr_abort(usbd_xfer_handle);
227 Static void uhci_device_intr_close(usbd_pipe_handle);
228 Static void uhci_device_intr_done(usbd_xfer_handle);
230 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle);
231 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle);
232 Static void uhci_device_bulk_abort(usbd_xfer_handle);
233 Static void uhci_device_bulk_close(usbd_pipe_handle);
234 Static void uhci_device_bulk_done(usbd_xfer_handle);
236 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle);
237 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle);
238 Static void uhci_device_isoc_abort(usbd_xfer_handle);
239 Static void uhci_device_isoc_close(usbd_pipe_handle);
240 Static void uhci_device_isoc_done(usbd_xfer_handle);
242 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle);
243 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle);
244 Static void uhci_root_ctrl_abort(usbd_xfer_handle);
245 Static void uhci_root_ctrl_close(usbd_pipe_handle);
246 Static void uhci_root_ctrl_done(usbd_xfer_handle);
248 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle);
249 Static usbd_status uhci_root_intr_start(usbd_xfer_handle);
250 Static void uhci_root_intr_abort(usbd_xfer_handle);
251 Static void uhci_root_intr_close(usbd_pipe_handle);
252 Static void uhci_root_intr_done(usbd_xfer_handle);
254 Static usbd_status uhci_open(usbd_pipe_handle);
255 Static void uhci_poll(struct usbd_bus *);
256 Static void uhci_softintr(void *);
258 Static usbd_status uhci_device_request(usbd_xfer_handle xfer);
260 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *);
261 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *);
262 Static usbd_status uhci_device_setintr(uhci_softc_t *sc,
263 struct uhci_pipe *pipe, int ival);
265 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe);
266 Static void uhci_noop(usbd_pipe_handle pipe);
268 Static __inline__ uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *,
272 Static void uhci_dump_all(uhci_softc_t *);
273 Static void uhci_dumpregs(uhci_softc_t *);
274 Static void uhci_dump_qhs(uhci_soft_qh_t *);
275 Static void uhci_dump_qh(uhci_soft_qh_t *);
276 Static void uhci_dump_tds(uhci_soft_td_t *);
277 Static void uhci_dump_td(uhci_soft_td_t *);
278 Static void uhci_dump_ii(uhci_intr_info_t *ii);
279 void uhci_dump(void);
282 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \
283 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE)
284 #define UWRITE1(sc, r, x) \
285 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
286 #define UWRITE2(sc, r, x) \
287 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
288 #define UWRITE4(sc, r, x) \
289 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0)
290 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r)))
291 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r)))
292 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r)))
294 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd)
295 #define UHCISTS(sc) UREAD2(sc, UHCI_STS)
297 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */
299 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK)
301 #define UHCI_INTR_ENDPT 1
303 struct usbd_bus_methods uhci_bus_methods = {
313 struct usbd_pipe_methods uhci_root_ctrl_methods = {
314 uhci_root_ctrl_transfer,
315 uhci_root_ctrl_start,
316 uhci_root_ctrl_abort,
317 uhci_root_ctrl_close,
322 struct usbd_pipe_methods uhci_root_intr_methods = {
323 uhci_root_intr_transfer,
324 uhci_root_intr_start,
325 uhci_root_intr_abort,
326 uhci_root_intr_close,
331 struct usbd_pipe_methods uhci_device_ctrl_methods = {
332 uhci_device_ctrl_transfer,
333 uhci_device_ctrl_start,
334 uhci_device_ctrl_abort,
335 uhci_device_ctrl_close,
337 uhci_device_ctrl_done,
340 struct usbd_pipe_methods uhci_device_intr_methods = {
341 uhci_device_intr_transfer,
342 uhci_device_intr_start,
343 uhci_device_intr_abort,
344 uhci_device_intr_close,
345 uhci_device_clear_toggle,
346 uhci_device_intr_done,
349 struct usbd_pipe_methods uhci_device_bulk_methods = {
350 uhci_device_bulk_transfer,
351 uhci_device_bulk_start,
352 uhci_device_bulk_abort,
353 uhci_device_bulk_close,
354 uhci_device_clear_toggle,
355 uhci_device_bulk_done,
358 struct usbd_pipe_methods uhci_device_isoc_methods = {
359 uhci_device_isoc_transfer,
360 uhci_device_isoc_start,
361 uhci_device_isoc_abort,
362 uhci_device_isoc_close,
364 uhci_device_isoc_done,
367 #define uhci_add_intr_info(sc, ii) \
368 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list);
369 #define uhci_del_intr_info(ii) \
370 LIST_REMOVE((ii), list)
372 Static __inline__ uhci_soft_qh_t *
373 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh)
375 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh));
377 for (; pqh->hlink != sqh; pqh = pqh->hlink) {
378 #if defined(DIAGNOSTIC) || defined(USB_DEBUG)
379 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) {
380 printf("uhci_find_prev_qh: QH not found\n");
389 uhci_globalreset(uhci_softc_t *sc)
391 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */
392 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */
393 UHCICMD(sc, 0); /* do nothing */
397 uhci_init(uhci_softc_t *sc)
401 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh;
404 DPRINTFN(1,("uhci_init: start\n"));
413 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
414 uhci_globalreset(sc); /* reset the controller */
417 /* Allocate and initialize real frame array. */
418 err = usb_allocmem(&sc->sc_bus,
419 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t),
420 UHCI_FRAMELIST_ALIGN, &sc->sc_dma);
423 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0);
424 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */
425 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/
428 * Allocate a TD, inactive, that hangs from the last QH.
429 * This is to avoid a bug in the PIIX that makes it run berserk
432 std = uhci_alloc_std(sc);
435 std->link.std = NULL;
436 std->td.td_link = htole32(UHCI_PTR_T);
437 std->td.td_status = htole32(0); /* inactive */
438 std->td.td_token = htole32(0);
439 std->td.td_buffer = htole32(0);
441 /* Allocate the dummy QH marking the end and used for looping the QHs.*/
442 lsqh = uhci_alloc_sqh(sc);
446 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */
448 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD);
449 sc->sc_last_qh = lsqh;
451 /* Allocate the dummy QH where bulk traffic will be queued. */
452 bsqh = uhci_alloc_sqh(sc);
456 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH);
458 bsqh->qh.qh_elink = htole32(UHCI_PTR_T);
459 sc->sc_bulk_start = sc->sc_bulk_end = bsqh;
461 /* Allocate dummy QH where high speed control traffic will be queued. */
462 chsqh = uhci_alloc_sqh(sc);
466 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH);
468 chsqh->qh.qh_elink = htole32(UHCI_PTR_T);
469 sc->sc_hctl_start = sc->sc_hctl_end = chsqh;
471 /* Allocate dummy QH where control traffic will be queued. */
472 clsqh = uhci_alloc_sqh(sc);
476 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH);
478 clsqh->qh.qh_elink = htole32(UHCI_PTR_T);
479 sc->sc_lctl_start = sc->sc_lctl_end = clsqh;
482 * Make all (virtual) frame list pointers point to the interrupt
483 * queue heads and the interrupt queue heads at the control
484 * queue head and point the physical frame list to the virtual.
486 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
487 std = uhci_alloc_std(sc);
488 sqh = uhci_alloc_sqh(sc);
489 if (std == NULL || sqh == NULL)
492 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH);
493 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
494 std->td.td_token = htole32(0);
495 std->td.td_buffer = htole32(0);
497 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH);
499 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
500 sc->sc_vframes[i].htd = std;
501 sc->sc_vframes[i].etd = std;
502 sc->sc_vframes[i].hqh = sqh;
503 sc->sc_vframes[i].eqh = sqh;
505 j < UHCI_FRAMELIST_COUNT;
506 j += UHCI_VFRAMELIST_COUNT)
507 sc->sc_pframes[j] = htole32(std->physaddr);
510 LIST_INIT(&sc->sc_intrhead);
512 SIMPLEQ_INIT(&sc->sc_free_xfers);
514 usb_callout_init(sc->sc_poll_handle);
516 /* Set up the bus struct. */
517 sc->sc_bus.methods = &uhci_bus_methods;
518 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe);
520 #if defined(__NetBSD__) || defined(__OpenBSD__)
521 sc->sc_suspend = PWR_RESUME;
522 sc->sc_powerhook = powerhook_establish(uhci_power, sc);
523 sc->sc_shutdownhook = shutdownhook_establish(uhci_shutdown, sc);
526 DPRINTFN(1,("uhci_init: enabling\n"));
527 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
528 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */
530 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */
532 return (uhci_run(sc, 1)); /* and here we go... */
535 #if defined(__NetBSD__) || defined(__OpenBSD__)
537 uhci_activate(device_ptr_t self, enum devact act)
539 struct uhci_softc *sc = (struct uhci_softc *)self;
547 case DVACT_DEACTIVATE:
548 if (sc->sc_child != NULL)
549 rv = config_deactivate(sc->sc_child);
556 uhci_detach(struct uhci_softc *sc, int flags)
558 usbd_xfer_handle xfer;
561 if (sc->sc_child != NULL)
562 rv = config_detach(sc->sc_child, flags);
567 #if defined(__NetBSD__) || defined(__OpenBSD__)
568 powerhook_disestablish(sc->sc_powerhook);
569 shutdownhook_disestablish(sc->sc_shutdownhook);
572 /* Free all xfers associated with this HC. */
574 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
577 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
581 /* XXX free other data structures XXX */
588 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size)
590 return (usb_allocmem(&((struct uhci_softc *)bus)->sc_bus, size, 0,
595 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma)
597 usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma);
601 uhci_allocx(struct usbd_bus *bus)
603 struct uhci_softc *sc = (struct uhci_softc *)bus;
604 usbd_xfer_handle xfer;
606 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers);
608 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, xfer, next);
610 if (xfer->busy_free != XFER_FREE) {
611 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer,
616 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT);
619 memset(xfer, 0, sizeof (struct uhci_xfer));
620 UXFER(xfer)->iinfo.sc = sc;
622 UXFER(xfer)->iinfo.isdone = 1;
623 xfer->busy_free = XFER_BUSY;
630 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer)
632 struct uhci_softc *sc = (struct uhci_softc *)bus;
635 if (xfer->busy_free != XFER_BUSY) {
636 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer,
640 xfer->busy_free = XFER_FREE;
641 if (!UXFER(xfer)->iinfo.isdone) {
642 printf("uhci_freex: !isdone\n");
646 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next);
650 * Shut down the controller when the system is going down.
653 uhci_shutdown(void *v)
655 uhci_softc_t *sc = v;
657 DPRINTF(("uhci_shutdown: stopping the HC\n"));
658 uhci_run(sc, 0); /* stop the controller */
662 * Handle suspend/resume.
664 * We need to switch to polling mode here, because this routine is
665 * called from an interrupt context. This is all right since we
666 * are almost suspended anyway.
669 uhci_power(int why, void *v)
671 uhci_softc_t *sc = v;
676 cmd = UREAD2(sc, UHCI_CMD);
678 DPRINTF(("uhci_power: sc=%p, why=%d (was %d), cmd=0x%x\n",
679 sc, why, sc->sc_suspend, cmd));
681 if (why != PWR_RESUME) {
686 if (sc->sc_intr_xfer != NULL)
687 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub,
689 sc->sc_bus.use_polling++;
690 uhci_run(sc, 0); /* stop the controller */
692 /* save some state if BIOS doesn't */
693 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM);
694 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF);
696 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */
698 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter global suspend */
699 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT);
700 sc->sc_suspend = why;
701 sc->sc_bus.use_polling--;
702 DPRINTF(("uhci_power: cmd=0x%x\n", UREAD2(sc, UHCI_CMD)));
705 if (sc->sc_suspend == PWR_RESUME)
706 printf("uhci_power: weird, resume without suspend.\n");
708 sc->sc_bus.use_polling++;
709 sc->sc_suspend = why;
710 if (cmd & UHCI_CMD_RS)
711 uhci_run(sc, 0); /* in case BIOS has started it */
713 /* restore saved state */
714 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0));
715 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum);
716 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof);
718 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force global resume */
719 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY);
720 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */
721 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE |
722 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* re-enable intrs */
723 uhci_run(sc, 1); /* and start traffic again */
724 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY);
725 sc->sc_bus.use_polling--;
726 if (sc->sc_intr_xfer != NULL)
727 usb_callout(sc->sc_poll_handle, sc->sc_ival,
728 uhci_poll_hub, sc->sc_intr_xfer);
739 uhci_dumpregs(uhci_softc_t *sc)
741 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, "
742 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n",
743 USBDEVNAME(sc->sc_bus.bdev),
744 UREAD2(sc, UHCI_CMD),
745 UREAD2(sc, UHCI_STS),
746 UREAD2(sc, UHCI_INTR),
747 UREAD2(sc, UHCI_FRNUM),
748 UREAD4(sc, UHCI_FLBASEADDR),
749 UREAD1(sc, UHCI_SOF),
750 UREAD2(sc, UHCI_PORTSC1),
751 UREAD2(sc, UHCI_PORTSC2)));
755 uhci_dump_td(uhci_soft_td_t *p)
757 char sbuf[128], sbuf2[128];
759 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx "
760 "token=0x%08lx buffer=0x%08lx\n",
761 p, (long)p->physaddr,
762 (long)le32toh(p->td.td_link),
763 (long)le32toh(p->td.td_status),
764 (long)le32toh(p->td.td_token),
765 (long)le32toh(p->td.td_buffer)));
767 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF",
769 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status),
770 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27"
771 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD",
772 sbuf2, sizeof(sbuf2));
774 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d,"
775 "D=%d,maxlen=%d\n", sbuf, sbuf2,
776 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)),
777 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)),
778 UHCI_TD_GET_PID(le32toh(p->td.td_token)),
779 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)),
780 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)),
781 UHCI_TD_GET_DT(le32toh(p->td.td_token)),
782 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token))));
786 uhci_dump_qh(uhci_soft_qh_t *sqh)
788 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh,
789 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink),
790 le32toh(sqh->qh.qh_elink)));
798 uhci_dump_all(thesc);
803 uhci_dump_all(uhci_softc_t *sc)
806 printf("intrs=%d\n", sc->sc_bus.no_intrs);
807 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/
808 uhci_dump_qh(sc->sc_lctl_start);
813 uhci_dump_qhs(uhci_soft_qh_t *sqh)
817 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards
818 * Traverses sideways first, then down.
828 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1.
832 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T))
833 uhci_dump_qhs(sqh->hlink);
835 DPRINTF(("No QH\n"));
837 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T))
838 uhci_dump_tds(sqh->elink);
840 DPRINTF(("No TD\n"));
844 uhci_dump_tds(uhci_soft_td_t *std)
848 for(td = std; td != NULL; td = td->link.std) {
851 /* Check whether the link pointer in this TD marks
852 * the link pointer as end of queue. This avoids
853 * printing the free list in case the queue/TD has
854 * already been moved there (seatbelt).
856 if (le32toh(td->td.td_link) & UHCI_PTR_T ||
857 le32toh(td->td.td_link) == 0)
863 uhci_dump_ii(uhci_intr_info_t *ii)
865 usbd_pipe_handle pipe;
866 usb_endpoint_descriptor_t *ed;
867 usbd_device_handle dev;
870 #define DONE ii->isdone
878 if (ii->xfer == NULL) {
879 printf("ii %p: done=%d xfer=NULL\n",
883 pipe = ii->xfer->pipe;
885 printf("ii %p: done=%d xfer=%p pipe=NULL\n",
889 if (pipe->endpoint == NULL) {
890 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n",
891 ii, DONE, ii->xfer, pipe);
894 if (pipe->device == NULL) {
895 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n",
896 ii, DONE, ii->xfer, pipe);
899 ed = pipe->endpoint->edesc;
901 printf("ii %p: done=%d xfer=%p dev=%p vid=0x%04x pid=0x%04x addr=%d pipe=%p ep=0x%02x attr=0x%02x\n",
902 ii, DONE, ii->xfer, dev,
903 UGETW(dev->ddesc.idVendor),
904 UGETW(dev->ddesc.idProduct),
906 ed->bEndpointAddress, ed->bmAttributes);
910 void uhci_dump_iis(struct uhci_softc *sc);
912 uhci_dump_iis(struct uhci_softc *sc)
914 uhci_intr_info_t *ii;
916 printf("intr_info list:\n");
917 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list))
922 void iidump(void) { uhci_dump_iis(thesc); }
927 * This routine is executed periodically and simulates interrupts
928 * from the root controller interrupt pipe for port status change.
931 uhci_poll_hub(void *addr)
933 usbd_xfer_handle xfer = addr;
934 usbd_pipe_handle pipe = xfer->pipe;
935 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
939 DPRINTFN(20, ("uhci_poll_hub\n"));
941 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
943 p = KERNADDR(&xfer->dmabuf, 0);
945 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
947 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC))
950 /* No change, try again in a while */
954 xfer->status = USBD_NORMAL_COMPLETION;
956 xfer->device->bus->intr_context++;
957 usb_transfer_complete(xfer);
958 xfer->device->bus->intr_context--;
963 uhci_root_intr_done(usbd_xfer_handle xfer)
968 uhci_root_ctrl_done(usbd_xfer_handle xfer)
973 * Let the last QH loop back to the high speed control transfer QH.
974 * This is what intel calls "bandwidth reclamation" and improves
975 * USB performance a lot for some devices.
976 * If we are already looping, just count it.
979 uhci_add_loop(uhci_softc_t *sc) {
984 if (++sc->sc_loops == 1) {
985 DPRINTFN(5,("uhci_start_loop: add\n"));
986 /* Note, we don't loop back the soft pointer. */
987 sc->sc_last_qh->qh.qh_hlink =
988 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH);
993 uhci_rem_loop(uhci_softc_t *sc) {
998 if (--sc->sc_loops == 0) {
999 DPRINTFN(5,("uhci_end_loop: remove\n"));
1000 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T);
1004 /* Add high speed control QH, called at splusb(). */
1006 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1008 uhci_soft_qh_t *eqh;
1012 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh));
1013 eqh = sc->sc_hctl_end;
1014 sqh->hlink = eqh->hlink;
1015 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1017 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1018 sc->sc_hctl_end = sqh;
1019 #ifdef UHCI_CTL_LOOP
1024 /* Remove high speed control QH, called at splusb(). */
1026 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1028 uhci_soft_qh_t *pqh;
1032 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh));
1033 #ifdef UHCI_CTL_LOOP
1037 * The T bit should be set in the elink of the QH so that the HC
1038 * doesn't follow the pointer. This condition may fail if the
1039 * the transferred packet was short so that the QH still points
1040 * at the last used TD.
1041 * In this case we set the T bit and wait a little for the HC
1042 * to stop looking at the TD.
1044 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1045 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1046 delay(UHCI_QH_REMOVE_DELAY);
1049 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh);
1050 pqh->hlink = sqh->hlink;
1051 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1052 delay(UHCI_QH_REMOVE_DELAY);
1053 if (sc->sc_hctl_end == sqh)
1054 sc->sc_hctl_end = pqh;
1057 /* Add low speed control QH, called at splusb(). */
1059 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1061 uhci_soft_qh_t *eqh;
1065 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh));
1066 eqh = sc->sc_lctl_end;
1067 sqh->hlink = eqh->hlink;
1068 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1070 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1071 sc->sc_lctl_end = sqh;
1074 /* Remove low speed control QH, called at splusb(). */
1076 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1078 uhci_soft_qh_t *pqh;
1082 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh));
1083 /* See comment in uhci_remove_hs_ctrl() */
1084 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1085 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1086 delay(UHCI_QH_REMOVE_DELAY);
1088 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh);
1089 pqh->hlink = sqh->hlink;
1090 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1091 delay(UHCI_QH_REMOVE_DELAY);
1092 if (sc->sc_lctl_end == sqh)
1093 sc->sc_lctl_end = pqh;
1096 /* Add bulk QH, called at splusb(). */
1098 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1100 uhci_soft_qh_t *eqh;
1104 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh));
1105 eqh = sc->sc_bulk_end;
1106 sqh->hlink = eqh->hlink;
1107 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
1109 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
1110 sc->sc_bulk_end = sqh;
1114 /* Remove bulk QH, called at splusb(). */
1116 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1118 uhci_soft_qh_t *pqh;
1122 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh));
1124 /* See comment in uhci_remove_hs_ctrl() */
1125 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
1126 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
1127 delay(UHCI_QH_REMOVE_DELAY);
1129 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh);
1130 pqh->hlink = sqh->hlink;
1131 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
1132 delay(UHCI_QH_REMOVE_DELAY);
1133 if (sc->sc_bulk_end == sqh)
1134 sc->sc_bulk_end = pqh;
1137 Static int uhci_intr1(uhci_softc_t *);
1140 uhci_intr(void *arg)
1142 uhci_softc_t *sc = arg;
1147 DPRINTFN(15,("uhci_intr: real interrupt\n"));
1148 if (sc->sc_bus.use_polling) {
1150 printf("uhci_intr: ignored interrupt while polling\n");
1154 return (uhci_intr1(sc));
1158 uhci_intr1(uhci_softc_t *sc)
1165 * It can happen that an interrupt will be delivered to
1166 * us before the device has been fully attached and the
1167 * softc struct has been configured. Usually this happens
1168 * when kldloading the USB support as a module after the
1169 * system has been booted. If we detect this condition,
1170 * we need to squelch the unwanted interrupts until we're
1173 if (sc->sc_bus.bdev == NULL) {
1174 UWRITE2(sc, UHCI_STS, 0xFFFF); /* ack pending interrupts */
1175 uhci_run(sc, 0); /* stop the controller */
1176 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */
1181 if (uhcidebug > 15) {
1182 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev)));
1186 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS;
1187 if (status == 0) /* The interrupt was not for us. */
1190 #if defined(DIAGNOSTIC) && defined(__NetBSD__)
1191 if (sc->sc_suspend != PWR_RESUME)
1192 printf("uhci_intr: suspended sts=0x%x\n", status);
1195 if (sc->sc_suspend != PWR_RESUME) {
1196 printf("%s: interrupt while not operating ignored\n",
1197 USBDEVNAME(sc->sc_bus.bdev));
1198 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */
1203 if (status & UHCI_STS_USBINT)
1204 ack |= UHCI_STS_USBINT;
1205 if (status & UHCI_STS_USBEI)
1206 ack |= UHCI_STS_USBEI;
1207 if (status & UHCI_STS_RD) {
1210 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev));
1213 if (status & UHCI_STS_HSE) {
1214 ack |= UHCI_STS_HSE;
1215 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev));
1217 if (status & UHCI_STS_HCPE) {
1218 ack |= UHCI_STS_HCPE;
1219 printf("%s: host controller process error\n",
1220 USBDEVNAME(sc->sc_bus.bdev));
1222 if (status & UHCI_STS_HCH) {
1223 /* no acknowledge needed */
1224 if (!sc->sc_dying) {
1225 printf("%s: host controller halted\n",
1226 USBDEVNAME(sc->sc_bus.bdev));
1235 return (0); /* nothing to acknowledge */
1236 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */
1238 sc->sc_bus.no_intrs++;
1239 usb_schedsoftintr(&sc->sc_bus);
1241 DPRINTFN(10, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev)));
1247 uhci_softintr(void *v)
1249 uhci_softc_t *sc = v;
1250 uhci_intr_info_t *ii;
1252 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev),
1253 sc->sc_bus.intr_context));
1255 sc->sc_bus.intr_context++;
1258 * Interrupts on UHCI really suck. When the host controller
1259 * interrupts because a transfer is completed there is no
1260 * way of knowing which transfer it was. You can scan down
1261 * the TDs and QHs of the previous frame to limit the search,
1262 * but that assumes that the interrupt was not delayed by more
1263 * than 1 ms, which may not always be true (e.g. after debug
1264 * output on a slow console).
1265 * We scan all interrupt descriptors to see if any have
1268 LIST_FOREACH(ii, &sc->sc_intrhead, list)
1269 uhci_check_intr(sc, ii);
1271 #ifdef USB_USE_SOFTINTR
1272 if (sc->sc_softwake) {
1273 sc->sc_softwake = 0;
1274 wakeup(&sc->sc_softwake);
1276 #endif /* USB_USE_SOFTINTR */
1278 sc->sc_bus.intr_context--;
1281 /* Check for an interrupt. */
1283 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1285 uhci_soft_td_t *std, *lstd;
1288 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1291 printf("uhci_check_intr: no ii? %p\n", ii);
1295 if (ii->xfer->status == USBD_CANCELLED ||
1296 ii->xfer->status == USBD_TIMEOUT) {
1297 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1301 if (ii->stdstart == NULL)
1306 printf("uhci_check_intr: std==0\n");
1311 * If the last TD is still active we need to check whether there
1312 * is a an error somewhere in the middle, or whether there was a
1313 * short packet (SPD and not ACTIVE).
1315 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1316 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1317 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1318 status = le32toh(std->td.td_status);
1319 /* If there's an active TD the xfer isn't done. */
1320 if (status & UHCI_TD_ACTIVE)
1322 /* Any kind of error makes the xfer done. */
1323 if (status & UHCI_TD_STALLED)
1325 /* We want short packets, and it is short: it's done */
1326 if ((status & UHCI_TD_SPD) &&
1327 UHCI_TD_GET_ACTLEN(status) <
1328 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1331 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1336 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1337 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1341 /* Called at splusb() */
1343 uhci_idone(uhci_intr_info_t *ii)
1345 usbd_xfer_handle xfer = ii->xfer;
1346 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1347 uhci_soft_td_t *std;
1348 u_int32_t status = 0, nstatus;
1351 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1358 printf("uhci_idone: ii is done!\n ");
1361 printf("uhci_idone: ii=%p is done!\n", ii);
1370 if (xfer->nframes != 0) {
1371 /* Isoc transfer, do things differently. */
1372 uhci_soft_td_t **stds = upipe->u.iso.stds;
1373 int i, n, nframes, len;
1375 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1377 nframes = xfer->nframes;
1379 n = UXFER(xfer)->curframe;
1380 for (i = 0; i < nframes; i++) {
1383 if (uhcidebug > 5) {
1384 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1388 if (++n >= UHCI_VFRAMELIST_COUNT)
1390 status = le32toh(std->td.td_status);
1391 len = UHCI_TD_GET_ACTLEN(status);
1392 xfer->frlengths[i] = len;
1395 upipe->u.iso.inuse -= nframes;
1396 xfer->actlen = actlen;
1397 xfer->status = USBD_NORMAL_COMPLETION;
1402 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1405 uhci_dump_tds(ii->stdstart);
1408 /* The transfer is done, compute actual length and status. */
1410 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1411 nstatus = le32toh(std->td.td_status);
1412 if (nstatus & UHCI_TD_ACTIVE)
1416 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1418 actlen += UHCI_TD_GET_ACTLEN(status);
1420 /* If there are left over TDs we need to update the toggle. */
1422 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1424 status &= UHCI_TD_ERROR;
1425 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1427 xfer->actlen = actlen;
1432 bitmask_snprintf((u_int32_t)status,
1433 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1434 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1435 sbuf, sizeof(sbuf));
1437 DPRINTFN((status == UHCI_TD_STALLED)*10,
1438 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1440 xfer->pipe->device->address,
1441 xfer->pipe->endpoint->edesc->bEndpointAddress,
1445 if (status == UHCI_TD_STALLED)
1446 xfer->status = USBD_STALLED;
1448 xfer->status = USBD_IOERROR; /* more info XXX */
1450 xfer->status = USBD_NORMAL_COMPLETION;
1454 usb_transfer_complete(xfer);
1455 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1459 * Called when a request does not complete.
1462 uhci_timeout(void *addr)
1464 uhci_intr_info_t *ii = addr;
1465 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1466 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1467 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1469 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1472 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1476 /* Execute the abort in a process context. */
1477 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1478 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1482 uhci_timeout_task(void *addr)
1484 usbd_xfer_handle xfer = addr;
1487 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1490 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1495 * Wait here until controller claims to have an interrupt.
1496 * Then call uhci_intr and return. Use timeout to avoid waiting
1498 * Only used during boot when interrupts are not enabled yet.
1501 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1503 int timo = xfer->timeout;
1504 uhci_intr_info_t *ii;
1506 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1508 xfer->status = USBD_IN_PROGRESS;
1509 for (; timo >= 0; timo--) {
1510 usb_delay_ms(&sc->sc_bus, 1);
1511 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1512 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1514 if (xfer->status != USBD_IN_PROGRESS)
1520 DPRINTF(("uhci_waitintr: timeout\n"));
1521 for (ii = LIST_FIRST(&sc->sc_intrhead);
1522 ii != NULL && ii->xfer != xfer;
1523 ii = LIST_NEXT(ii, list))
1527 panic("uhci_waitintr: lost intr_info\n");
1533 uhci_poll(struct usbd_bus *bus)
1535 uhci_softc_t *sc = (uhci_softc_t *)bus;
1537 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1542 uhci_reset(uhci_softc_t *sc)
1546 UHCICMD(sc, UHCI_CMD_HCRESET);
1547 /* The reset bit goes low when the controller is done. */
1548 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1549 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1550 usb_delay_ms(&sc->sc_bus, 1);
1551 if (n >= UHCI_RESET_TIMEOUT)
1552 printf("%s: controller did not reset\n",
1553 USBDEVNAME(sc->sc_bus.bdev));
1557 uhci_run(uhci_softc_t *sc, int run)
1564 DPRINTF(("uhci_run: setting run=%d\n", run));
1565 cmd = UREAD2(sc, UHCI_CMD);
1569 cmd &= ~UHCI_CMD_RS;
1571 for(n = 0; n < 10; n++) {
1572 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1573 /* return when we've entered the state we want */
1574 if (run == running) {
1576 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1577 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1578 return (USBD_NORMAL_COMPLETION);
1580 usb_delay_ms(&sc->sc_bus, 1);
1583 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1584 run ? "start" : "stop");
1585 return (USBD_IOERROR);
1589 * Memory management routines.
1590 * uhci_alloc_std allocates TDs
1591 * uhci_alloc_sqh allocates QHs
1592 * These two routines do their own free list management,
1593 * partly for speed, partly because allocating DMAable memory
1594 * has page size granularaity so much memory would be wasted if
1595 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1599 uhci_alloc_std(uhci_softc_t *sc)
1601 uhci_soft_td_t *std;
1606 if (sc->sc_freetds == NULL) {
1607 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1608 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1609 UHCI_TD_ALIGN, &dma);
1612 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1613 offs = i * UHCI_STD_SIZE;
1614 std = KERNADDR(&dma, offs);
1615 std->physaddr = DMAADDR(&dma, offs);
1616 std->link.std = sc->sc_freetds;
1617 sc->sc_freetds = std;
1620 std = sc->sc_freetds;
1621 sc->sc_freetds = std->link.std;
1622 memset(&std->td, 0, sizeof(uhci_td_t));
1627 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1630 #define TD_IS_FREE 0x12345678
1631 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1632 printf("uhci_free_std: freeing free TD %p\n", std);
1635 std->td.td_token = htole32(TD_IS_FREE);
1637 std->link.std = sc->sc_freetds;
1638 sc->sc_freetds = std;
1642 uhci_alloc_sqh(uhci_softc_t *sc)
1644 uhci_soft_qh_t *sqh;
1649 if (sc->sc_freeqhs == NULL) {
1650 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1651 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1652 UHCI_QH_ALIGN, &dma);
1655 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1656 offs = i * UHCI_SQH_SIZE;
1657 sqh = KERNADDR(&dma, offs);
1658 sqh->physaddr = DMAADDR(&dma, offs);
1659 sqh->hlink = sc->sc_freeqhs;
1660 sc->sc_freeqhs = sqh;
1663 sqh = sc->sc_freeqhs;
1664 sc->sc_freeqhs = sqh->hlink;
1665 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1670 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1672 sqh->hlink = sc->sc_freeqhs;
1673 sc->sc_freeqhs = sqh;
1677 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1678 uhci_soft_td_t *stdend)
1682 for (; std != stdend; std = p) {
1684 uhci_free_std(sc, std);
1689 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1690 int rd, u_int16_t flags, usb_dma_t *dma,
1691 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1693 uhci_soft_td_t *p, *lastp;
1694 uhci_physaddr_t lastlink;
1695 int i, ntd, l, tog, maxp;
1697 int addr = upipe->pipe.device->address;
1698 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1700 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1701 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1702 upipe->pipe.device->speed, flags));
1703 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1705 printf("uhci_alloc_std_chain: maxp=0\n");
1706 return (USBD_INVAL);
1708 ntd = (len + maxp - 1) / maxp;
1709 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1711 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1714 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1715 return (USBD_NORMAL_COMPLETION);
1717 tog = upipe->nexttoggle;
1720 upipe->nexttoggle = tog ^ 1;
1722 lastlink = UHCI_PTR_T;
1724 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1725 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1726 status |= UHCI_TD_LS;
1727 if (flags & USBD_SHORT_XFER_OK)
1728 status |= UHCI_TD_SPD;
1729 for (i = ntd; i >= 0; i--) {
1730 p = uhci_alloc_std(sc);
1732 uhci_free_std_chain(sc, lastp, NULL);
1733 return (USBD_NOMEM);
1735 p->link.std = lastp;
1736 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1738 lastlink = p->physaddr;
1739 p->td.td_status = htole32(status);
1743 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1749 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1750 UHCI_TD_OUT(l, endpt, addr, tog));
1751 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1755 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1756 upipe->nexttoggle));
1757 return (USBD_NORMAL_COMPLETION);
1761 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1763 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1764 upipe->nexttoggle = 0;
1768 uhci_noop(usbd_pipe_handle pipe)
1773 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1777 /* Insert last in queue. */
1778 err = usb_insert_transfer(xfer);
1783 * Pipe isn't running (otherwise err would be USBD_INPROG),
1784 * so start it first.
1786 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1790 uhci_device_bulk_start(usbd_xfer_handle xfer)
1792 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1793 usbd_device_handle dev = upipe->pipe.device;
1794 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1795 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1796 uhci_soft_td_t *data, *dataend;
1797 uhci_soft_qh_t *sqh;
1799 int len, isread, endpt;
1802 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1803 xfer, xfer->length, xfer->flags));
1806 return (USBD_IOERROR);
1809 if (xfer->rqflags & URQ_REQUEST)
1810 panic("uhci_device_bulk_transfer: a request\n");
1814 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1815 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1816 sqh = upipe->u.bulk.sqh;
1818 upipe->u.bulk.isread = isread;
1819 upipe->u.bulk.length = len;
1821 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1822 &xfer->dmabuf, &data, &dataend);
1825 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1828 if (uhcidebug > 8) {
1829 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1830 uhci_dump_tds(data);
1834 /* Set up interrupt info. */
1836 ii->stdstart = data;
1837 ii->stdend = dataend;
1840 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1846 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1849 uhci_add_bulk(sc, sqh);
1850 uhci_add_intr_info(sc, ii);
1852 if (xfer->timeout && !sc->sc_bus.use_polling) {
1853 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1856 xfer->status = USBD_IN_PROGRESS;
1860 if (uhcidebug > 10) {
1861 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1862 uhci_dump_tds(data);
1866 if (sc->sc_bus.use_polling)
1867 uhci_waitintr(sc, xfer);
1869 return (USBD_IN_PROGRESS);
1872 /* Abort a device bulk request. */
1874 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1876 DPRINTF(("uhci_device_bulk_abort:\n"));
1877 uhci_abort_xfer(xfer, USBD_CANCELLED);
1881 * Abort a device request.
1882 * If this routine is called at splusb() it guarantees that the request
1883 * will be removed from the hardware scheduling and that the callback
1884 * for it will be called with USBD_CANCELLED status.
1885 * It's impossible to guarantee that the requested transfer will not
1886 * have happened since the hardware runs concurrently.
1887 * If the transaction has already happened we rely on the ordinary
1888 * interrupt processing to process it.
1891 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1893 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1894 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1895 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1896 uhci_soft_td_t *std;
1899 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1902 /* If we're dying, just do the software part. */
1904 xfer->status = status; /* make software ignore it */
1905 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1906 usb_transfer_complete(xfer);
1911 if (xfer->device->bus->intr_context || !curproc)
1912 panic("uhci_abort_xfer: not in process context\n");
1915 * Step 1: Make interrupt routine and hardware ignore xfer.
1918 xfer->status = status; /* make software ignore it */
1919 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1920 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1921 for (std = ii->stdstart; std != NULL; std = std->link.std)
1922 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1926 * Step 2: Wait until we know hardware has finished any possible
1927 * use of the xfer. Also make sure the soft interrupt routine
1930 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1932 #ifdef USB_USE_SOFTINTR
1933 sc->sc_softwake = 1;
1934 #endif /* USB_USE_SOFTINTR */
1935 usb_schedsoftintr(&sc->sc_bus);
1936 #ifdef USB_USE_SOFTINTR
1937 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1938 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
1939 #endif /* USB_USE_SOFTINTR */
1943 * Step 3: Execute callback.
1947 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1952 usb_transfer_complete(xfer);
1956 /* Close a device bulk pipe. */
1958 uhci_device_bulk_close(usbd_pipe_handle pipe)
1960 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1961 usbd_device_handle dev = upipe->pipe.device;
1962 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1964 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1968 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1972 /* Insert last in queue. */
1973 err = usb_insert_transfer(xfer);
1978 * Pipe isn't running (otherwise err would be USBD_INPROG),
1979 * so start it first.
1981 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1985 uhci_device_ctrl_start(usbd_xfer_handle xfer)
1987 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1991 return (USBD_IOERROR);
1994 if (!(xfer->rqflags & URQ_REQUEST))
1995 panic("uhci_device_ctrl_transfer: not a request\n");
1998 err = uhci_device_request(xfer);
2002 if (sc->sc_bus.use_polling)
2003 uhci_waitintr(sc, xfer);
2004 return (USBD_IN_PROGRESS);
2008 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2012 /* Insert last in queue. */
2013 err = usb_insert_transfer(xfer);
2018 * Pipe isn't running (otherwise err would be USBD_INPROG),
2019 * so start it first.
2021 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2025 uhci_device_intr_start(usbd_xfer_handle xfer)
2027 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2028 usbd_device_handle dev = upipe->pipe.device;
2029 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2030 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2031 uhci_soft_td_t *data, *dataend;
2032 uhci_soft_qh_t *sqh;
2037 return (USBD_IOERROR);
2039 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2040 xfer, xfer->length, xfer->flags));
2043 if (xfer->rqflags & URQ_REQUEST)
2044 panic("uhci_device_intr_transfer: a request\n");
2047 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2048 &xfer->dmabuf, &data, &dataend);
2051 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2054 if (uhcidebug > 10) {
2055 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2056 uhci_dump_tds(data);
2057 uhci_dump_qh(upipe->u.intr.qhs[0]);
2062 /* Set up interrupt info. */
2064 ii->stdstart = data;
2065 ii->stdend = dataend;
2068 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2073 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2074 upipe->u.intr.qhs[0]));
2075 for (i = 0; i < upipe->u.intr.npoll; i++) {
2076 sqh = upipe->u.intr.qhs[i];
2078 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2080 uhci_add_intr_info(sc, ii);
2081 xfer->status = USBD_IN_PROGRESS;
2085 if (uhcidebug > 10) {
2086 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2087 uhci_dump_tds(data);
2088 uhci_dump_qh(upipe->u.intr.qhs[0]);
2092 return (USBD_IN_PROGRESS);
2095 /* Abort a device control request. */
2097 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2099 DPRINTF(("uhci_device_ctrl_abort:\n"));
2100 uhci_abort_xfer(xfer, USBD_CANCELLED);
2103 /* Close a device control pipe. */
2105 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2109 /* Abort a device interrupt request. */
2111 uhci_device_intr_abort(usbd_xfer_handle xfer)
2113 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2114 if (xfer->pipe->intrxfer == xfer) {
2115 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2116 xfer->pipe->intrxfer = NULL;
2118 uhci_abort_xfer(xfer, USBD_CANCELLED);
2121 /* Close a device interrupt pipe. */
2123 uhci_device_intr_close(usbd_pipe_handle pipe)
2125 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2126 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2130 /* Unlink descriptors from controller data structures. */
2131 npoll = upipe->u.intr.npoll;
2133 for (i = 0; i < npoll; i++)
2134 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2138 * We now have to wait for any activity on the physical
2139 * descriptors to stop.
2141 usb_delay_ms(&sc->sc_bus, 2);
2143 for(i = 0; i < npoll; i++)
2144 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2145 free(upipe->u.intr.qhs, M_USBHC);
2147 /* XXX free other resources */
2151 uhci_device_request(usbd_xfer_handle xfer)
2153 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2154 usb_device_request_t *req = &xfer->request;
2155 usbd_device_handle dev = upipe->pipe.device;
2156 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2157 int addr = dev->address;
2158 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2159 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2160 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2161 uhci_soft_qh_t *sqh;
2168 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2169 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2170 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2171 UGETW(req->wIndex), UGETW(req->wLength),
2174 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2175 isread = req->bmRequestType & UT_READ;
2176 len = UGETW(req->wLength);
2178 setup = upipe->u.ctl.setup;
2179 stat = upipe->u.ctl.stat;
2180 sqh = upipe->u.ctl.sqh;
2182 /* Set up data transaction */
2184 upipe->nexttoggle = 1;
2185 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2186 &xfer->dmabuf, &data, &dataend);
2190 dataend->link.std = stat;
2191 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2195 upipe->u.ctl.length = len;
2197 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2199 setup->link.std = next;
2200 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2201 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2203 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2204 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2206 stat->link.std = NULL;
2207 stat->td.td_link = htole32(UHCI_PTR_T);
2208 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2209 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2211 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2212 UHCI_TD_IN (0, endpt, addr, 1));
2213 stat->td.td_buffer = htole32(0);
2216 if (uhcidebug > 10) {
2217 DPRINTF(("uhci_device_request: before transfer\n"));
2218 uhci_dump_tds(setup);
2222 /* Set up interrupt info. */
2224 ii->stdstart = setup;
2228 printf("uhci_device_request: not done, ii=%p\n", ii);
2234 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2237 if (dev->speed == USB_SPEED_LOW)
2238 uhci_add_ls_ctrl(sc, sqh);
2240 uhci_add_hs_ctrl(sc, sqh);
2241 uhci_add_intr_info(sc, ii);
2243 if (uhcidebug > 12) {
2244 uhci_soft_td_t *std;
2245 uhci_soft_qh_t *xqh;
2246 uhci_soft_qh_t *sxqh;
2248 uhci_physaddr_t link;
2249 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2250 for (std = sc->sc_vframes[0].htd, link = 0;
2251 (link & UHCI_PTR_QH) == 0;
2252 std = std->link.std) {
2253 link = le32toh(std->td.td_link);
2256 sxqh = (uhci_soft_qh_t *)std;
2260 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2261 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2264 DPRINTF(("Enqueued QH:\n"));
2266 uhci_dump_tds(sqh->elink);
2269 if (xfer->timeout && !sc->sc_bus.use_polling) {
2270 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2273 xfer->status = USBD_IN_PROGRESS;
2276 return (USBD_NORMAL_COMPLETION);
2280 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2284 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2286 /* Put it on our queue, */
2287 err = usb_insert_transfer(xfer);
2289 /* bail out on error, */
2290 if (err && err != USBD_IN_PROGRESS)
2293 /* XXX should check inuse here */
2295 /* insert into schedule, */
2296 uhci_device_isoc_enter(xfer);
2298 /* and start if the pipe wasn't running */
2300 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2306 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2308 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2309 usbd_device_handle dev = upipe->pipe.device;
2310 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2311 struct iso *iso = &upipe->u.iso;
2312 uhci_soft_td_t *std;
2313 u_int32_t buf, len, status;
2314 int s, i, next, nframes;
2316 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2318 iso->inuse, iso->next, xfer, xfer->nframes));
2323 if (xfer->status == USBD_IN_PROGRESS) {
2324 /* This request has already been entered into the frame list */
2325 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2330 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2331 printf("uhci_device_isoc_enter: overflow!\n");
2336 /* Not in use yet, schedule it a few frames ahead. */
2337 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2338 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2341 xfer->status = USBD_IN_PROGRESS;
2342 UXFER(xfer)->curframe = next;
2344 buf = DMAADDR(&xfer->dmabuf, 0);
2345 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2348 nframes = xfer->nframes;
2350 for (i = 0; i < nframes; i++) {
2351 std = iso->stds[next];
2352 if (++next >= UHCI_VFRAMELIST_COUNT)
2354 len = xfer->frlengths[i];
2355 std->td.td_buffer = htole32(buf);
2356 if (i == nframes - 1)
2357 status |= UHCI_TD_IOC;
2358 std->td.td_status = htole32(status);
2359 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2360 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2362 if (uhcidebug > 5) {
2363 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2370 iso->inuse += xfer->nframes;
2376 uhci_device_isoc_start(usbd_xfer_handle xfer)
2378 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2379 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2380 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2381 uhci_soft_td_t *end;
2384 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2387 return (USBD_IOERROR);
2390 if (xfer->status != USBD_IN_PROGRESS)
2391 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2394 /* Find the last TD */
2395 i = UXFER(xfer)->curframe + xfer->nframes;
2396 if (i >= UHCI_VFRAMELIST_COUNT)
2397 i -= UHCI_VFRAMELIST_COUNT;
2398 end = upipe->u.iso.stds[i];
2402 printf("uhci_device_isoc_start: end == NULL\n");
2403 return (USBD_INVAL);
2409 /* Set up interrupt info. */
2415 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2418 uhci_add_intr_info(sc, ii);
2422 return (USBD_IN_PROGRESS);
2426 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2428 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2429 uhci_soft_td_t **stds = upipe->u.iso.stds;
2430 uhci_soft_td_t *std;
2431 int i, n, s, nframes, maxlen, len;
2435 /* Transfer is already done. */
2436 if (xfer->status != USBD_NOT_STARTED &&
2437 xfer->status != USBD_IN_PROGRESS) {
2442 /* Give xfer the requested abort code. */
2443 xfer->status = USBD_CANCELLED;
2445 /* make hardware ignore it, */
2446 nframes = xfer->nframes;
2447 n = UXFER(xfer)->curframe;
2449 for (i = 0; i < nframes; i++) {
2451 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2452 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2455 if (++n >= UHCI_VFRAMELIST_COUNT)
2459 /* and wait until we are sure the hardware has finished. */
2463 UXFER(xfer)->iinfo.isdone = 1;
2465 /* Run callback and remove from interrupt list. */
2466 usb_transfer_complete(xfer);
2472 uhci_device_isoc_close(usbd_pipe_handle pipe)
2474 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2475 usbd_device_handle dev = upipe->pipe.device;
2476 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2477 uhci_soft_td_t *std, *vstd;
2482 * Make sure all TDs are marked as inactive.
2483 * Wait for completion.
2487 iso = &upipe->u.iso;
2489 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2490 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2491 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2494 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2496 for (vstd = sc->sc_vframes[i].htd;
2497 vstd != NULL && vstd->link.std != std;
2498 vstd = vstd->link.std)
2502 printf("uhci_device_isoc_close: %p not found\n", std);
2506 vstd->link = std->link;
2507 vstd->td.td_link = std->td.td_link;
2508 uhci_free_std(sc, std);
2512 free(iso->stds, M_USBHC);
2516 uhci_setup_isoc(usbd_pipe_handle pipe)
2518 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2519 usbd_device_handle dev = upipe->pipe.device;
2520 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2521 int addr = upipe->pipe.device->address;
2522 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2523 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2524 uhci_soft_td_t *std, *vstd;
2529 iso = &upipe->u.iso;
2530 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2533 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2534 UHCI_TD_OUT(0, endpt, addr, 0);
2536 /* Allocate the TDs and mark as inactive; */
2537 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2538 std = uhci_alloc_std(sc);
2541 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2542 std->td.td_token = htole32(token);
2546 /* Insert TDs into schedule. */
2548 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2550 vstd = sc->sc_vframes[i].htd;
2551 std->link = vstd->link;
2552 std->td.td_link = vstd->td.td_link;
2553 vstd->link.std = std;
2554 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2561 return (USBD_NORMAL_COMPLETION);
2565 uhci_free_std(sc, iso->stds[i]);
2566 free(iso->stds, M_USBHC);
2567 return (USBD_NOMEM);
2571 uhci_device_isoc_done(usbd_xfer_handle xfer)
2573 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2575 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2577 if (ii->xfer != xfer)
2578 /* Not on interrupt list, ignore it. */
2582 if (xfer->busy_free != XFER_BUSY) {
2583 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2584 xfer, xfer->busy_free);
2588 if (ii->stdend == NULL) {
2589 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2597 /* Turn off the interrupt since it is active even if the TD is not. */
2598 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2600 uhci_del_intr_info(ii); /* remove from active list */
2603 if (ii->stdend == NULL) {
2604 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2614 uhci_device_intr_done(usbd_xfer_handle xfer)
2616 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2617 uhci_softc_t *sc = ii->sc;
2618 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2619 uhci_soft_qh_t *sqh;
2622 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2624 npoll = upipe->u.intr.npoll;
2625 for(i = 0; i < npoll; i++) {
2626 sqh = upipe->u.intr.qhs[i];
2628 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2630 uhci_free_std_chain(sc, ii->stdstart, NULL);
2633 if (xfer->pipe->repeat) {
2634 uhci_soft_td_t *data, *dataend;
2636 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2638 /* This alloc cannot fail since we freed the chain above. */
2639 uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2640 &xfer->dmabuf, &data, &dataend);
2641 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2644 if (uhcidebug > 10) {
2645 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2646 uhci_dump_tds(data);
2647 uhci_dump_qh(upipe->u.intr.qhs[0]);
2651 ii->stdstart = data;
2652 ii->stdend = dataend;
2655 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2659 for (i = 0; i < npoll; i++) {
2660 sqh = upipe->u.intr.qhs[i];
2662 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2664 xfer->status = USBD_IN_PROGRESS;
2665 /* The ii is already on the examined list, just leave it. */
2667 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2668 uhci_del_intr_info(ii);
2672 /* Deallocate request data structures */
2674 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2676 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2677 uhci_softc_t *sc = ii->sc;
2678 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2681 if (!(xfer->rqflags & URQ_REQUEST))
2682 panic("uhci_ctrl_done: not a request\n");
2685 uhci_del_intr_info(ii); /* remove from active list */
2687 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2688 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2690 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2692 if (upipe->u.ctl.length != 0)
2693 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2695 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2698 /* Deallocate request data structures */
2700 uhci_device_bulk_done(usbd_xfer_handle xfer)
2702 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2703 uhci_softc_t *sc = ii->sc;
2704 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2706 uhci_del_intr_info(ii); /* remove from active list */
2708 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2710 uhci_free_std_chain(sc, ii->stdstart, NULL);
2712 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2715 /* Add interrupt QH, called with vflock. */
2717 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2719 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2720 uhci_soft_qh_t *eqh;
2722 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2725 sqh->hlink = eqh->hlink;
2726 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2728 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2733 /* Remove interrupt QH. */
2735 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2737 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2738 uhci_soft_qh_t *pqh;
2740 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2742 /* See comment in uhci_remove_ctrl() */
2743 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2744 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2745 delay(UHCI_QH_REMOVE_DELAY);
2748 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2749 pqh->hlink = sqh->hlink;
2750 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2751 delay(UHCI_QH_REMOVE_DELAY);
2758 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2760 uhci_soft_qh_t *sqh;
2762 u_int bestbw, bw, bestoffs, offs;
2764 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2766 printf("uhci_setintr: 0 interval\n");
2767 return (USBD_INVAL);
2770 if (ival > UHCI_VFRAMELIST_COUNT)
2771 ival = UHCI_VFRAMELIST_COUNT;
2772 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2773 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2775 upipe->u.intr.npoll = npoll;
2777 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2780 * Figure out which offset in the schedule that has most
2781 * bandwidth left over.
2783 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2784 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2785 for (bw = i = 0; i < npoll; i++)
2786 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2792 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2794 for(i = 0; i < npoll; i++) {
2795 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2797 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2798 sqh->pos = MOD(i * ival + bestoffs);
2803 /* Enter QHs into the controller data structures. */
2804 for(i = 0; i < npoll; i++)
2805 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2808 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2809 return (USBD_NORMAL_COMPLETION);
2812 /* Open a new pipe. */
2814 uhci_open(usbd_pipe_handle pipe)
2816 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2817 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2818 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2822 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2823 pipe, pipe->device->address,
2824 ed->bEndpointAddress, sc->sc_addr));
2826 upipe->aborting = 0;
2827 upipe->nexttoggle = 0;
2829 if (pipe->device->address == sc->sc_addr) {
2830 switch (ed->bEndpointAddress) {
2831 case USB_CONTROL_ENDPOINT:
2832 pipe->methods = &uhci_root_ctrl_methods;
2834 case UE_DIR_IN | UHCI_INTR_ENDPT:
2835 pipe->methods = &uhci_root_intr_methods;
2838 return (USBD_INVAL);
2841 switch (ed->bmAttributes & UE_XFERTYPE) {
2843 pipe->methods = &uhci_device_ctrl_methods;
2844 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2845 if (upipe->u.ctl.sqh == NULL)
2847 upipe->u.ctl.setup = uhci_alloc_std(sc);
2848 if (upipe->u.ctl.setup == NULL) {
2849 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2852 upipe->u.ctl.stat = uhci_alloc_std(sc);
2853 if (upipe->u.ctl.stat == NULL) {
2854 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2855 uhci_free_std(sc, upipe->u.ctl.setup);
2858 err = usb_allocmem(&sc->sc_bus,
2859 sizeof(usb_device_request_t),
2860 0, &upipe->u.ctl.reqdma);
2862 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2863 uhci_free_std(sc, upipe->u.ctl.setup);
2864 uhci_free_std(sc, upipe->u.ctl.stat);
2869 pipe->methods = &uhci_device_intr_methods;
2870 ival = pipe->interval;
2871 if (ival == USBD_DEFAULT_INTERVAL)
2872 ival = ed->bInterval;
2873 return (uhci_device_setintr(sc, upipe, ival));
2874 case UE_ISOCHRONOUS:
2875 pipe->methods = &uhci_device_isoc_methods;
2876 return (uhci_setup_isoc(pipe));
2878 pipe->methods = &uhci_device_bulk_methods;
2879 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2880 if (upipe->u.bulk.sqh == NULL)
2885 return (USBD_NORMAL_COMPLETION);
2888 return (USBD_NOMEM);
2892 * Data structures and routines to emulate the root hub.
2894 usb_device_descriptor_t uhci_devd = {
2895 USB_DEVICE_DESCRIPTOR_SIZE,
2896 UDESC_DEVICE, /* type */
2897 {0x00, 0x01}, /* USB version */
2898 UDCLASS_HUB, /* class */
2899 UDSUBCLASS_HUB, /* subclass */
2900 UDPROTO_FSHUB, /* protocol */
2901 64, /* max packet */
2902 {0},{0},{0x00,0x01}, /* device id */
2903 1,2,0, /* string indicies */
2904 1 /* # of configurations */
2907 usb_config_descriptor_t uhci_confd = {
2908 USB_CONFIG_DESCRIPTOR_SIZE,
2910 {USB_CONFIG_DESCRIPTOR_SIZE +
2911 USB_INTERFACE_DESCRIPTOR_SIZE +
2912 USB_ENDPOINT_DESCRIPTOR_SIZE},
2920 usb_interface_descriptor_t uhci_ifcd = {
2921 USB_INTERFACE_DESCRIPTOR_SIZE,
2932 usb_endpoint_descriptor_t uhci_endpd = {
2933 USB_ENDPOINT_DESCRIPTOR_SIZE,
2935 UE_DIR_IN | UHCI_INTR_ENDPT,
2941 usb_hub_descriptor_t uhci_hubd_piix = {
2942 USB_HUB_DESCRIPTOR_SIZE,
2945 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2946 50, /* power on to power good */
2948 { 0x00 }, /* both ports are removable */
2952 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2958 p->bLength = 2 * strlen(s) + 2;
2961 p->bDescriptorType = UDESC_STRING;
2963 for (i = 0; s[i] && l > 1; i++, l -= 2)
2964 USETW2(p->bString[i], 0, s[i]);
2969 * Simulate a hardware hub by handling all the necessary requests.
2972 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
2976 /* Insert last in queue. */
2977 err = usb_insert_transfer(xfer);
2982 * Pipe isn't running (otherwise err would be USBD_INPROG),
2983 * so start it first.
2985 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2989 uhci_root_ctrl_start(usbd_xfer_handle xfer)
2991 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2992 usb_device_request_t *req;
2995 int s, len, value, index, status, change, l, totlen = 0;
2996 usb_port_status_t ps;
3000 return (USBD_IOERROR);
3003 if (!(xfer->rqflags & URQ_REQUEST))
3004 panic("uhci_root_ctrl_transfer: not a request\n");
3006 req = &xfer->request;
3008 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3009 req->bmRequestType, req->bRequest));
3011 len = UGETW(req->wLength);
3012 value = UGETW(req->wValue);
3013 index = UGETW(req->wIndex);
3016 buf = KERNADDR(&xfer->dmabuf, 0);
3018 #define C(x,y) ((x) | ((y) << 8))
3019 switch(C(req->bRequest, req->bmRequestType)) {
3020 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3021 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3022 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3024 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3025 * for the integrated root hub.
3028 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3030 *(u_int8_t *)buf = sc->sc_conf;
3034 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3035 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3036 switch(value >> 8) {
3038 if ((value & 0xff) != 0) {
3042 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3043 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3044 memcpy(buf, &uhci_devd, l);
3047 if ((value & 0xff) != 0) {
3051 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3052 memcpy(buf, &uhci_confd, l);
3053 buf = (char *)buf + l;
3055 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3057 memcpy(buf, &uhci_ifcd, l);
3058 buf = (char *)buf + l;
3060 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3062 memcpy(buf, &uhci_endpd, l);
3067 *(u_int8_t *)buf = 0;
3069 switch (value & 0xff) {
3070 case 1: /* Vendor */
3071 totlen = uhci_str(buf, len, sc->sc_vendor);
3073 case 2: /* Product */
3074 totlen = uhci_str(buf, len, "UHCI root hub");
3083 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3085 *(u_int8_t *)buf = 0;
3089 case C(UR_GET_STATUS, UT_READ_DEVICE):
3091 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3095 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3096 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3098 USETW(((usb_status_t *)buf)->wStatus, 0);
3102 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3103 if (value >= USB_MAX_DEVICES) {
3107 sc->sc_addr = value;
3109 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3110 if (value != 0 && value != 1) {
3114 sc->sc_conf = value;
3116 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3118 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3119 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3120 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3123 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3125 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3128 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3130 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3131 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3132 "port=%d feature=%d\n",
3135 port = UHCI_PORTSC1;
3136 else if (index == 2)
3137 port = UHCI_PORTSC2;
3143 case UHF_PORT_ENABLE:
3144 x = URWMASK(UREAD2(sc, port));
3145 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3147 case UHF_PORT_SUSPEND:
3148 x = URWMASK(UREAD2(sc, port));
3149 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3151 case UHF_PORT_RESET:
3152 x = URWMASK(UREAD2(sc, port));
3153 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3155 case UHF_C_PORT_CONNECTION:
3156 x = URWMASK(UREAD2(sc, port));
3157 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3159 case UHF_C_PORT_ENABLE:
3160 x = URWMASK(UREAD2(sc, port));
3161 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3163 case UHF_C_PORT_OVER_CURRENT:
3164 x = URWMASK(UREAD2(sc, port));
3165 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3167 case UHF_C_PORT_RESET:
3169 err = USBD_NORMAL_COMPLETION;
3171 case UHF_PORT_CONNECTION:
3172 case UHF_PORT_OVER_CURRENT:
3173 case UHF_PORT_POWER:
3174 case UHF_PORT_LOW_SPEED:
3175 case UHF_C_PORT_SUSPEND:
3181 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3183 port = UHCI_PORTSC1;
3184 else if (index == 2)
3185 port = UHCI_PORTSC2;
3192 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3193 UHCI_PORTSC_LS_SHIFT;
3197 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3202 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3204 memcpy(buf, &uhci_hubd_piix, l);
3206 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3211 memset(buf, 0, len);
3214 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3216 port = UHCI_PORTSC1;
3217 else if (index == 2)
3218 port = UHCI_PORTSC2;
3227 x = UREAD2(sc, port);
3228 status = change = 0;
3229 if (x & UHCI_PORTSC_CCS)
3230 status |= UPS_CURRENT_CONNECT_STATUS;
3231 if (x & UHCI_PORTSC_CSC)
3232 change |= UPS_C_CONNECT_STATUS;
3233 if (x & UHCI_PORTSC_PE)
3234 status |= UPS_PORT_ENABLED;
3235 if (x & UHCI_PORTSC_POEDC)
3236 change |= UPS_C_PORT_ENABLED;
3237 if (x & UHCI_PORTSC_OCI)
3238 status |= UPS_OVERCURRENT_INDICATOR;
3239 if (x & UHCI_PORTSC_OCIC)
3240 change |= UPS_C_OVERCURRENT_INDICATOR;
3241 if (x & UHCI_PORTSC_SUSP)
3242 status |= UPS_SUSPEND;
3243 if (x & UHCI_PORTSC_LSDA)
3244 status |= UPS_LOW_SPEED;
3245 status |= UPS_PORT_POWER;
3247 change |= UPS_C_PORT_RESET;
3248 USETW(ps.wPortStatus, status);
3249 USETW(ps.wPortChange, change);
3250 l = min(len, sizeof ps);
3251 memcpy(buf, &ps, l);
3254 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3257 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3259 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3261 port = UHCI_PORTSC1;
3262 else if (index == 2)
3263 port = UHCI_PORTSC2;
3269 case UHF_PORT_ENABLE:
3270 x = URWMASK(UREAD2(sc, port));
3271 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3273 case UHF_PORT_SUSPEND:
3274 x = URWMASK(UREAD2(sc, port));
3275 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3277 case UHF_PORT_RESET:
3278 x = URWMASK(UREAD2(sc, port));
3279 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3280 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3281 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3283 x = UREAD2(sc, port);
3284 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3285 usb_delay_ms(&sc->sc_bus, 10); /* XXX */
3286 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
3287 index, UREAD2(sc, port)));
3290 case UHF_PORT_POWER:
3291 /* Pretend we turned on power */
3292 err = USBD_NORMAL_COMPLETION;
3294 case UHF_C_PORT_CONNECTION:
3295 case UHF_C_PORT_ENABLE:
3296 case UHF_C_PORT_OVER_CURRENT:
3297 case UHF_PORT_CONNECTION:
3298 case UHF_PORT_OVER_CURRENT:
3299 case UHF_PORT_LOW_SPEED:
3300 case UHF_C_PORT_SUSPEND:
3301 case UHF_C_PORT_RESET:
3311 xfer->actlen = totlen;
3312 err = USBD_NORMAL_COMPLETION;
3316 usb_transfer_complete(xfer);
3318 return (USBD_IN_PROGRESS);
3321 /* Abort a root control request. */
3323 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3325 /* Nothing to do, all transfers are synchronous. */
3328 /* Close the root pipe. */
3330 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3332 DPRINTF(("uhci_root_ctrl_close\n"));
3335 /* Abort a root interrupt request. */
3337 uhci_root_intr_abort(usbd_xfer_handle xfer)
3339 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3341 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3342 sc->sc_intr_xfer = NULL;
3344 if (xfer->pipe->intrxfer == xfer) {
3345 DPRINTF(("uhci_root_intr_abort: remove\n"));
3346 xfer->pipe->intrxfer = 0;
3348 xfer->status = USBD_CANCELLED;
3350 UXFER(xfer)->iinfo.isdone = 1;
3352 usb_transfer_complete(xfer);
3356 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3360 /* Insert last in queue. */
3361 err = usb_insert_transfer(xfer);
3366 * Pipe isn't running (otherwise err would be USBD_INPROG),
3367 * so start it first.
3369 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3372 /* Start a transfer on the root interrupt pipe */
3374 uhci_root_intr_start(usbd_xfer_handle xfer)
3376 usbd_pipe_handle pipe = xfer->pipe;
3377 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3379 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3380 xfer, xfer->length, xfer->flags));
3383 return (USBD_IOERROR);
3385 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3386 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3387 sc->sc_intr_xfer = xfer;
3388 return (USBD_IN_PROGRESS);
3391 /* Close the root interrupt pipe. */
3393 uhci_root_intr_close(usbd_pipe_handle pipe)
3395 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3397 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3398 sc->sc_intr_xfer = NULL;
3399 DPRINTF(("uhci_root_intr_close\n"));