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 if (sc->sc_softwake) {
1272 sc->sc_softwake = 0;
1273 wakeup(&sc->sc_softwake);
1276 sc->sc_bus.intr_context--;
1279 /* Check for an interrupt. */
1281 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii)
1283 uhci_soft_td_t *std, *lstd;
1286 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii));
1289 printf("uhci_check_intr: no ii? %p\n", ii);
1293 if (ii->xfer->status == USBD_CANCELLED ||
1294 ii->xfer->status == USBD_TIMEOUT) {
1295 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer));
1299 if (ii->stdstart == NULL)
1304 printf("uhci_check_intr: std==0\n");
1309 * If the last TD is still active we need to check whether there
1310 * is a an error somewhere in the middle, or whether there was a
1311 * short packet (SPD and not ACTIVE).
1313 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) {
1314 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii));
1315 for (std = ii->stdstart; std != lstd; std = std->link.std) {
1316 status = le32toh(std->td.td_status);
1317 /* If there's an active TD the xfer isn't done. */
1318 if (status & UHCI_TD_ACTIVE)
1320 /* Any kind of error makes the xfer done. */
1321 if (status & UHCI_TD_STALLED)
1323 /* We want short packets, and it is short: it's done */
1324 if ((status & UHCI_TD_SPD) &&
1325 UHCI_TD_GET_ACTLEN(status) <
1326 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)))
1329 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n",
1334 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii));
1335 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii);
1339 /* Called at splusb() */
1341 uhci_idone(uhci_intr_info_t *ii)
1343 usbd_xfer_handle xfer = ii->xfer;
1344 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1345 uhci_soft_td_t *std;
1346 u_int32_t status = 0, nstatus;
1349 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii));
1356 printf("uhci_idone: ii is done!\n ");
1359 printf("uhci_idone: ii=%p is done!\n", ii);
1368 if (xfer->nframes != 0) {
1369 /* Isoc transfer, do things differently. */
1370 uhci_soft_td_t **stds = upipe->u.iso.stds;
1371 int i, n, nframes, len;
1373 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii));
1375 nframes = xfer->nframes;
1377 n = UXFER(xfer)->curframe;
1378 for (i = 0; i < nframes; i++) {
1381 if (uhcidebug > 5) {
1382 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i));
1386 if (++n >= UHCI_VFRAMELIST_COUNT)
1388 status = le32toh(std->td.td_status);
1389 len = UHCI_TD_GET_ACTLEN(status);
1390 xfer->frlengths[i] = len;
1393 upipe->u.iso.inuse -= nframes;
1394 xfer->actlen = actlen;
1395 xfer->status = USBD_NORMAL_COMPLETION;
1400 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n",
1403 uhci_dump_tds(ii->stdstart);
1406 /* The transfer is done, compute actual length and status. */
1408 for (std = ii->stdstart; std != NULL; std = std->link.std) {
1409 nstatus = le32toh(std->td.td_status);
1410 if (nstatus & UHCI_TD_ACTIVE)
1414 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) !=
1416 actlen += UHCI_TD_GET_ACTLEN(status);
1418 /* If there are left over TDs we need to update the toggle. */
1420 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token));
1422 status &= UHCI_TD_ERROR;
1423 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n",
1425 xfer->actlen = actlen;
1430 bitmask_snprintf((u_int32_t)status,
1431 "\20\22BITSTUFF\23CRCTO\24NAK\25"
1432 "BABBLE\26DBUFFER\27STALLED\30ACTIVE",
1433 sbuf, sizeof(sbuf));
1435 DPRINTFN((status == UHCI_TD_STALLED)*10,
1436 ("uhci_idone: error, addr=%d, endpt=0x%02x, "
1438 xfer->pipe->device->address,
1439 xfer->pipe->endpoint->edesc->bEndpointAddress,
1443 if (status == UHCI_TD_STALLED)
1444 xfer->status = USBD_STALLED;
1446 xfer->status = USBD_IOERROR; /* more info XXX */
1448 xfer->status = USBD_NORMAL_COMPLETION;
1452 usb_transfer_complete(xfer);
1453 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii));
1457 * Called when a request does not complete.
1460 uhci_timeout(void *addr)
1462 uhci_intr_info_t *ii = addr;
1463 struct uhci_xfer *uxfer = UXFER(ii->xfer);
1464 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe;
1465 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1467 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer));
1470 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT);
1474 /* Execute the abort in a process context. */
1475 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer);
1476 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task);
1480 uhci_timeout_task(void *addr)
1482 usbd_xfer_handle xfer = addr;
1485 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer));
1488 uhci_abort_xfer(xfer, USBD_TIMEOUT);
1493 * Wait here until controller claims to have an interrupt.
1494 * Then call uhci_intr and return. Use timeout to avoid waiting
1496 * Only used during boot when interrupts are not enabled yet.
1499 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer)
1501 int timo = xfer->timeout;
1502 uhci_intr_info_t *ii;
1504 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo));
1506 xfer->status = USBD_IN_PROGRESS;
1507 for (; timo >= 0; timo--) {
1508 usb_delay_ms(&sc->sc_bus, 1);
1509 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS)));
1510 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) {
1512 if (xfer->status != USBD_IN_PROGRESS)
1518 DPRINTF(("uhci_waitintr: timeout\n"));
1519 for (ii = LIST_FIRST(&sc->sc_intrhead);
1520 ii != NULL && ii->xfer != xfer;
1521 ii = LIST_NEXT(ii, list))
1525 panic("uhci_waitintr: lost intr_info\n");
1531 uhci_poll(struct usbd_bus *bus)
1533 uhci_softc_t *sc = (uhci_softc_t *)bus;
1535 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT)
1540 uhci_reset(uhci_softc_t *sc)
1544 UHCICMD(sc, UHCI_CMD_HCRESET);
1545 /* The reset bit goes low when the controller is done. */
1546 for (n = 0; n < UHCI_RESET_TIMEOUT &&
1547 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++)
1548 usb_delay_ms(&sc->sc_bus, 1);
1549 if (n >= UHCI_RESET_TIMEOUT)
1550 printf("%s: controller did not reset\n",
1551 USBDEVNAME(sc->sc_bus.bdev));
1555 uhci_run(uhci_softc_t *sc, int run)
1562 DPRINTF(("uhci_run: setting run=%d\n", run));
1563 cmd = UREAD2(sc, UHCI_CMD);
1567 cmd &= ~UHCI_CMD_RS;
1569 for(n = 0; n < 10; n++) {
1570 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH);
1571 /* return when we've entered the state we want */
1572 if (run == running) {
1574 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n",
1575 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS)));
1576 return (USBD_NORMAL_COMPLETION);
1578 usb_delay_ms(&sc->sc_bus, 1);
1581 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev),
1582 run ? "start" : "stop");
1583 return (USBD_IOERROR);
1587 * Memory management routines.
1588 * uhci_alloc_std allocates TDs
1589 * uhci_alloc_sqh allocates QHs
1590 * These two routines do their own free list management,
1591 * partly for speed, partly because allocating DMAable memory
1592 * has page size granularaity so much memory would be wasted if
1593 * only one TD/QH (32 bytes) was placed in each allocated chunk.
1597 uhci_alloc_std(uhci_softc_t *sc)
1599 uhci_soft_td_t *std;
1604 if (sc->sc_freetds == NULL) {
1605 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n"));
1606 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK,
1607 UHCI_TD_ALIGN, &dma);
1610 for(i = 0; i < UHCI_STD_CHUNK; i++) {
1611 offs = i * UHCI_STD_SIZE;
1612 std = KERNADDR(&dma, offs);
1613 std->physaddr = DMAADDR(&dma, offs);
1614 std->link.std = sc->sc_freetds;
1615 sc->sc_freetds = std;
1618 std = sc->sc_freetds;
1619 sc->sc_freetds = std->link.std;
1620 memset(&std->td, 0, sizeof(uhci_td_t));
1625 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std)
1628 #define TD_IS_FREE 0x12345678
1629 if (le32toh(std->td.td_token) == TD_IS_FREE) {
1630 printf("uhci_free_std: freeing free TD %p\n", std);
1633 std->td.td_token = htole32(TD_IS_FREE);
1635 std->link.std = sc->sc_freetds;
1636 sc->sc_freetds = std;
1640 uhci_alloc_sqh(uhci_softc_t *sc)
1642 uhci_soft_qh_t *sqh;
1647 if (sc->sc_freeqhs == NULL) {
1648 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n"));
1649 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK,
1650 UHCI_QH_ALIGN, &dma);
1653 for(i = 0; i < UHCI_SQH_CHUNK; i++) {
1654 offs = i * UHCI_SQH_SIZE;
1655 sqh = KERNADDR(&dma, offs);
1656 sqh->physaddr = DMAADDR(&dma, offs);
1657 sqh->hlink = sc->sc_freeqhs;
1658 sc->sc_freeqhs = sqh;
1661 sqh = sc->sc_freeqhs;
1662 sc->sc_freeqhs = sqh->hlink;
1663 memset(&sqh->qh, 0, sizeof(uhci_qh_t));
1668 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
1670 sqh->hlink = sc->sc_freeqhs;
1671 sc->sc_freeqhs = sqh;
1675 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std,
1676 uhci_soft_td_t *stdend)
1680 for (; std != stdend; std = p) {
1682 uhci_free_std(sc, std);
1687 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len,
1688 int rd, u_int16_t flags, usb_dma_t *dma,
1689 uhci_soft_td_t **sp, uhci_soft_td_t **ep)
1691 uhci_soft_td_t *p, *lastp;
1692 uhci_physaddr_t lastlink;
1693 int i, ntd, l, tog, maxp;
1695 int addr = upipe->pipe.device->address;
1696 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1698 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d "
1699 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len,
1700 upipe->pipe.device->speed, flags));
1701 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize);
1703 printf("uhci_alloc_std_chain: maxp=0\n");
1704 return (USBD_INVAL);
1706 ntd = (len + maxp - 1) / maxp;
1707 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0)
1709 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd));
1712 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n"));
1713 return (USBD_NORMAL_COMPLETION);
1715 tog = upipe->nexttoggle;
1718 upipe->nexttoggle = tog ^ 1;
1720 lastlink = UHCI_PTR_T;
1722 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE);
1723 if (upipe->pipe.device->speed == USB_SPEED_LOW)
1724 status |= UHCI_TD_LS;
1725 if (flags & USBD_SHORT_XFER_OK)
1726 status |= UHCI_TD_SPD;
1727 for (i = ntd; i >= 0; i--) {
1728 p = uhci_alloc_std(sc);
1730 uhci_free_std_chain(sc, lastp, NULL);
1731 return (USBD_NOMEM);
1733 p->link.std = lastp;
1734 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD);
1736 lastlink = p->physaddr;
1737 p->td.td_status = htole32(status);
1741 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER))
1747 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) :
1748 UHCI_TD_OUT(l, endpt, addr, tog));
1749 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp));
1753 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n",
1754 upipe->nexttoggle));
1755 return (USBD_NORMAL_COMPLETION);
1759 uhci_device_clear_toggle(usbd_pipe_handle pipe)
1761 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1762 upipe->nexttoggle = 0;
1766 uhci_noop(usbd_pipe_handle pipe)
1771 uhci_device_bulk_transfer(usbd_xfer_handle xfer)
1775 /* Insert last in queue. */
1776 err = usb_insert_transfer(xfer);
1781 * Pipe isn't running (otherwise err would be USBD_INPROG),
1782 * so start it first.
1784 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1788 uhci_device_bulk_start(usbd_xfer_handle xfer)
1790 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1791 usbd_device_handle dev = upipe->pipe.device;
1792 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1793 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1794 uhci_soft_td_t *data, *dataend;
1795 uhci_soft_qh_t *sqh;
1797 int len, isread, endpt;
1800 DPRINTFN(3, ("uhci_device_bulk_transfer: xfer=%p len=%d flags=%d\n",
1801 xfer, xfer->length, xfer->flags));
1804 return (USBD_IOERROR);
1807 if (xfer->rqflags & URQ_REQUEST)
1808 panic("uhci_device_bulk_transfer: a request\n");
1812 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
1813 isread = UE_GET_DIR(endpt) == UE_DIR_IN;
1814 sqh = upipe->u.bulk.sqh;
1816 upipe->u.bulk.isread = isread;
1817 upipe->u.bulk.length = len;
1819 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
1820 &xfer->dmabuf, &data, &dataend);
1823 dataend->td.td_status |= htole32(UHCI_TD_IOC);
1826 if (uhcidebug > 8) {
1827 DPRINTF(("uhci_device_bulk_transfer: data(1)\n"));
1828 uhci_dump_tds(data);
1832 /* Set up interrupt info. */
1834 ii->stdstart = data;
1835 ii->stdend = dataend;
1838 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii);
1844 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
1847 uhci_add_bulk(sc, sqh);
1848 uhci_add_intr_info(sc, ii);
1850 if (xfer->timeout && !sc->sc_bus.use_polling) {
1851 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
1854 xfer->status = USBD_IN_PROGRESS;
1858 if (uhcidebug > 10) {
1859 DPRINTF(("uhci_device_bulk_transfer: data(2)\n"));
1860 uhci_dump_tds(data);
1864 if (sc->sc_bus.use_polling)
1865 uhci_waitintr(sc, xfer);
1867 return (USBD_IN_PROGRESS);
1870 /* Abort a device bulk request. */
1872 uhci_device_bulk_abort(usbd_xfer_handle xfer)
1874 DPRINTF(("uhci_device_bulk_abort:\n"));
1875 uhci_abort_xfer(xfer, USBD_CANCELLED);
1879 * Abort a device request.
1880 * If this routine is called at splusb() it guarantees that the request
1881 * will be removed from the hardware scheduling and that the callback
1882 * for it will be called with USBD_CANCELLED status.
1883 * It's impossible to guarantee that the requested transfer will not
1884 * have happened since the hardware runs concurrently.
1885 * If the transaction has already happened we rely on the ordinary
1886 * interrupt processing to process it.
1889 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status)
1891 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
1892 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
1893 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
1894 uhci_soft_td_t *std;
1897 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status));
1900 /* If we're dying, just do the software part. */
1902 xfer->status = status; /* make software ignore it */
1903 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer);
1904 usb_transfer_complete(xfer);
1909 if (xfer->device->bus->intr_context || !curproc)
1910 panic("uhci_abort_xfer: not in process context\n");
1913 * Step 1: Make interrupt routine and hardware ignore xfer.
1916 xfer->status = status; /* make software ignore it */
1917 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii);
1918 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii));
1919 for (std = ii->stdstart; std != NULL; std = std->link.std)
1920 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
1924 * Step 2: Wait until we know hardware has finished any possible
1925 * use of the xfer. Also make sure the soft interrupt routine
1928 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */
1930 sc->sc_softwake = 1;
1931 usb_schedsoftintr(&sc->sc_bus);
1932 DPRINTFN(1,("uhci_abort_xfer: tsleep\n"));
1933 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0);
1937 * Step 3: Execute callback.
1941 DPRINTFN(1,("uhci_abort_xfer: callback\n"));
1946 usb_transfer_complete(xfer);
1950 /* Close a device bulk pipe. */
1952 uhci_device_bulk_close(usbd_pipe_handle pipe)
1954 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
1955 usbd_device_handle dev = upipe->pipe.device;
1956 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
1958 uhci_free_sqh(sc, upipe->u.bulk.sqh);
1962 uhci_device_ctrl_transfer(usbd_xfer_handle xfer)
1966 /* Insert last in queue. */
1967 err = usb_insert_transfer(xfer);
1972 * Pipe isn't running (otherwise err would be USBD_INPROG),
1973 * so start it first.
1975 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
1979 uhci_device_ctrl_start(usbd_xfer_handle xfer)
1981 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
1985 return (USBD_IOERROR);
1988 if (!(xfer->rqflags & URQ_REQUEST))
1989 panic("uhci_device_ctrl_transfer: not a request\n");
1992 err = uhci_device_request(xfer);
1996 if (sc->sc_bus.use_polling)
1997 uhci_waitintr(sc, xfer);
1998 return (USBD_IN_PROGRESS);
2002 uhci_device_intr_transfer(usbd_xfer_handle xfer)
2006 /* Insert last in queue. */
2007 err = usb_insert_transfer(xfer);
2012 * Pipe isn't running (otherwise err would be USBD_INPROG),
2013 * so start it first.
2015 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2019 uhci_device_intr_start(usbd_xfer_handle xfer)
2021 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2022 usbd_device_handle dev = upipe->pipe.device;
2023 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2024 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2025 uhci_soft_td_t *data, *dataend;
2026 uhci_soft_qh_t *sqh;
2031 return (USBD_IOERROR);
2033 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n",
2034 xfer, xfer->length, xfer->flags));
2037 if (xfer->rqflags & URQ_REQUEST)
2038 panic("uhci_device_intr_transfer: a request\n");
2041 err = uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2042 &xfer->dmabuf, &data, &dataend);
2045 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2048 if (uhcidebug > 10) {
2049 DPRINTF(("uhci_device_intr_transfer: data(1)\n"));
2050 uhci_dump_tds(data);
2051 uhci_dump_qh(upipe->u.intr.qhs[0]);
2056 /* Set up interrupt info. */
2058 ii->stdstart = data;
2059 ii->stdend = dataend;
2062 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii);
2067 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n",
2068 upipe->u.intr.qhs[0]));
2069 for (i = 0; i < upipe->u.intr.npoll; i++) {
2070 sqh = upipe->u.intr.qhs[i];
2072 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2074 uhci_add_intr_info(sc, ii);
2075 xfer->status = USBD_IN_PROGRESS;
2079 if (uhcidebug > 10) {
2080 DPRINTF(("uhci_device_intr_transfer: data(2)\n"));
2081 uhci_dump_tds(data);
2082 uhci_dump_qh(upipe->u.intr.qhs[0]);
2086 return (USBD_IN_PROGRESS);
2089 /* Abort a device control request. */
2091 uhci_device_ctrl_abort(usbd_xfer_handle xfer)
2093 DPRINTF(("uhci_device_ctrl_abort:\n"));
2094 uhci_abort_xfer(xfer, USBD_CANCELLED);
2097 /* Close a device control pipe. */
2099 uhci_device_ctrl_close(usbd_pipe_handle pipe)
2103 /* Abort a device interrupt request. */
2105 uhci_device_intr_abort(usbd_xfer_handle xfer)
2107 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer));
2108 if (xfer->pipe->intrxfer == xfer) {
2109 DPRINTFN(1,("uhci_device_intr_abort: remove\n"));
2110 xfer->pipe->intrxfer = NULL;
2112 uhci_abort_xfer(xfer, USBD_CANCELLED);
2115 /* Close a device interrupt pipe. */
2117 uhci_device_intr_close(usbd_pipe_handle pipe)
2119 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2120 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2124 /* Unlink descriptors from controller data structures. */
2125 npoll = upipe->u.intr.npoll;
2127 for (i = 0; i < npoll; i++)
2128 uhci_remove_intr(sc, upipe->u.intr.qhs[i]);
2132 * We now have to wait for any activity on the physical
2133 * descriptors to stop.
2135 usb_delay_ms(&sc->sc_bus, 2);
2137 for(i = 0; i < npoll; i++)
2138 uhci_free_sqh(sc, upipe->u.intr.qhs[i]);
2139 free(upipe->u.intr.qhs, M_USBHC);
2141 /* XXX free other resources */
2145 uhci_device_request(usbd_xfer_handle xfer)
2147 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2148 usb_device_request_t *req = &xfer->request;
2149 usbd_device_handle dev = upipe->pipe.device;
2150 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2151 int addr = dev->address;
2152 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2153 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2154 uhci_soft_td_t *setup, *data, *stat, *next, *dataend;
2155 uhci_soft_qh_t *sqh;
2162 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, "
2163 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n",
2164 req->bmRequestType, req->bRequest, UGETW(req->wValue),
2165 UGETW(req->wIndex), UGETW(req->wLength),
2168 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0;
2169 isread = req->bmRequestType & UT_READ;
2170 len = UGETW(req->wLength);
2172 setup = upipe->u.ctl.setup;
2173 stat = upipe->u.ctl.stat;
2174 sqh = upipe->u.ctl.sqh;
2176 /* Set up data transaction */
2178 upipe->nexttoggle = 1;
2179 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags,
2180 &xfer->dmabuf, &data, &dataend);
2184 dataend->link.std = stat;
2185 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2189 upipe->u.ctl.length = len;
2191 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req);
2193 setup->link.std = next;
2194 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD);
2195 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2197 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr));
2198 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0));
2200 stat->link.std = NULL;
2201 stat->td.td_link = htole32(UHCI_PTR_T);
2202 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls |
2203 UHCI_TD_ACTIVE | UHCI_TD_IOC);
2205 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) :
2206 UHCI_TD_IN (0, endpt, addr, 1));
2207 stat->td.td_buffer = htole32(0);
2210 if (uhcidebug > 10) {
2211 DPRINTF(("uhci_device_request: before transfer\n"));
2212 uhci_dump_tds(setup);
2216 /* Set up interrupt info. */
2218 ii->stdstart = setup;
2222 printf("uhci_device_request: not done, ii=%p\n", ii);
2228 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD);
2231 if (dev->speed == USB_SPEED_LOW)
2232 uhci_add_ls_ctrl(sc, sqh);
2234 uhci_add_hs_ctrl(sc, sqh);
2235 uhci_add_intr_info(sc, ii);
2237 if (uhcidebug > 12) {
2238 uhci_soft_td_t *std;
2239 uhci_soft_qh_t *xqh;
2240 uhci_soft_qh_t *sxqh;
2242 uhci_physaddr_t link;
2243 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n"));
2244 for (std = sc->sc_vframes[0].htd, link = 0;
2245 (link & UHCI_PTR_QH) == 0;
2246 std = std->link.std) {
2247 link = le32toh(std->td.td_link);
2250 sxqh = (uhci_soft_qh_t *)std;
2254 xqh = (maxqh++ == 5 || xqh->hlink == sxqh ||
2255 xqh->hlink == xqh ? NULL : xqh->hlink)) {
2258 DPRINTF(("Enqueued QH:\n"));
2260 uhci_dump_tds(sqh->elink);
2263 if (xfer->timeout && !sc->sc_bus.use_polling) {
2264 usb_callout(xfer->timeout_handle, MS_TO_TICKS(xfer->timeout),
2267 xfer->status = USBD_IN_PROGRESS;
2270 return (USBD_NORMAL_COMPLETION);
2274 uhci_device_isoc_transfer(usbd_xfer_handle xfer)
2278 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer));
2280 /* Put it on our queue, */
2281 err = usb_insert_transfer(xfer);
2283 /* bail out on error, */
2284 if (err && err != USBD_IN_PROGRESS)
2287 /* XXX should check inuse here */
2289 /* insert into schedule, */
2290 uhci_device_isoc_enter(xfer);
2292 /* and start if the pipe wasn't running */
2294 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue));
2300 uhci_device_isoc_enter(usbd_xfer_handle xfer)
2302 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2303 usbd_device_handle dev = upipe->pipe.device;
2304 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2305 struct iso *iso = &upipe->u.iso;
2306 uhci_soft_td_t *std;
2307 u_int32_t buf, len, status;
2308 int s, i, next, nframes;
2310 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p "
2312 iso->inuse, iso->next, xfer, xfer->nframes));
2317 if (xfer->status == USBD_IN_PROGRESS) {
2318 /* This request has already been entered into the frame list */
2319 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer);
2324 if (iso->inuse >= UHCI_VFRAMELIST_COUNT)
2325 printf("uhci_device_isoc_enter: overflow!\n");
2330 /* Not in use yet, schedule it a few frames ahead. */
2331 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT;
2332 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next));
2335 xfer->status = USBD_IN_PROGRESS;
2336 UXFER(xfer)->curframe = next;
2338 buf = DMAADDR(&xfer->dmabuf, 0);
2339 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) |
2342 nframes = xfer->nframes;
2344 for (i = 0; i < nframes; i++) {
2345 std = iso->stds[next];
2346 if (++next >= UHCI_VFRAMELIST_COUNT)
2348 len = xfer->frlengths[i];
2349 std->td.td_buffer = htole32(buf);
2350 if (i == nframes - 1)
2351 status |= UHCI_TD_IOC;
2352 std->td.td_status = htole32(status);
2353 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK);
2354 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len));
2356 if (uhcidebug > 5) {
2357 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i));
2364 iso->inuse += xfer->nframes;
2370 uhci_device_isoc_start(usbd_xfer_handle xfer)
2372 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2373 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus;
2374 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2375 uhci_soft_td_t *end;
2378 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer));
2381 return (USBD_IOERROR);
2384 if (xfer->status != USBD_IN_PROGRESS)
2385 printf("uhci_device_isoc_start: not in progress %p\n", xfer);
2388 /* Find the last TD */
2389 i = UXFER(xfer)->curframe + xfer->nframes;
2390 if (i >= UHCI_VFRAMELIST_COUNT)
2391 i -= UHCI_VFRAMELIST_COUNT;
2392 end = upipe->u.iso.stds[i];
2396 printf("uhci_device_isoc_start: end == NULL\n");
2397 return (USBD_INVAL);
2403 /* Set up interrupt info. */
2409 printf("uhci_device_isoc_start: not done, ii=%p\n", ii);
2412 uhci_add_intr_info(sc, ii);
2416 return (USBD_IN_PROGRESS);
2420 uhci_device_isoc_abort(usbd_xfer_handle xfer)
2422 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2423 uhci_soft_td_t **stds = upipe->u.iso.stds;
2424 uhci_soft_td_t *std;
2425 int i, n, s, nframes, maxlen, len;
2429 /* Transfer is already done. */
2430 if (xfer->status != USBD_NOT_STARTED &&
2431 xfer->status != USBD_IN_PROGRESS) {
2436 /* Give xfer the requested abort code. */
2437 xfer->status = USBD_CANCELLED;
2439 /* make hardware ignore it, */
2440 nframes = xfer->nframes;
2441 n = UXFER(xfer)->curframe;
2443 for (i = 0; i < nframes; i++) {
2445 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC));
2446 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token));
2449 if (++n >= UHCI_VFRAMELIST_COUNT)
2453 /* and wait until we are sure the hardware has finished. */
2457 UXFER(xfer)->iinfo.isdone = 1;
2459 /* Run callback and remove from interrupt list. */
2460 usb_transfer_complete(xfer);
2466 uhci_device_isoc_close(usbd_pipe_handle pipe)
2468 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2469 usbd_device_handle dev = upipe->pipe.device;
2470 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2471 uhci_soft_td_t *std, *vstd;
2476 * Make sure all TDs are marked as inactive.
2477 * Wait for completion.
2481 iso = &upipe->u.iso;
2483 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++)
2484 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE);
2485 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */
2488 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2490 for (vstd = sc->sc_vframes[i].htd;
2491 vstd != NULL && vstd->link.std != std;
2492 vstd = vstd->link.std)
2496 printf("uhci_device_isoc_close: %p not found\n", std);
2500 vstd->link = std->link;
2501 vstd->td.td_link = std->td.td_link;
2502 uhci_free_std(sc, std);
2506 free(iso->stds, M_USBHC);
2510 uhci_setup_isoc(usbd_pipe_handle pipe)
2512 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2513 usbd_device_handle dev = upipe->pipe.device;
2514 uhci_softc_t *sc = (uhci_softc_t *)dev->bus;
2515 int addr = upipe->pipe.device->address;
2516 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress;
2517 int rd = UE_GET_DIR(endpt) == UE_DIR_IN;
2518 uhci_soft_td_t *std, *vstd;
2523 iso = &upipe->u.iso;
2524 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *),
2527 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) :
2528 UHCI_TD_OUT(0, endpt, addr, 0);
2530 /* Allocate the TDs and mark as inactive; */
2531 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2532 std = uhci_alloc_std(sc);
2535 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */
2536 std->td.td_token = htole32(token);
2540 /* Insert TDs into schedule. */
2542 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) {
2544 vstd = sc->sc_vframes[i].htd;
2545 std->link = vstd->link;
2546 std->td.td_link = vstd->td.td_link;
2547 vstd->link.std = std;
2548 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD);
2555 return (USBD_NORMAL_COMPLETION);
2559 uhci_free_std(sc, iso->stds[i]);
2560 free(iso->stds, M_USBHC);
2561 return (USBD_NOMEM);
2565 uhci_device_isoc_done(usbd_xfer_handle xfer)
2567 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2569 DPRINTFN(4, ("uhci_isoc_done: length=%d\n", xfer->actlen));
2571 if (ii->xfer != xfer)
2572 /* Not on interrupt list, ignore it. */
2576 if (xfer->busy_free != XFER_BUSY) {
2577 printf("uhci_device_isoc_done: xfer=%p not busy 0x%08x\n",
2578 xfer, xfer->busy_free);
2582 if (ii->stdend == NULL) {
2583 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2591 /* Turn off the interrupt since it is active even if the TD is not. */
2592 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC);
2594 uhci_del_intr_info(ii); /* remove from active list */
2597 if (ii->stdend == NULL) {
2598 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer);
2608 uhci_device_intr_done(usbd_xfer_handle xfer)
2610 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2611 uhci_softc_t *sc = ii->sc;
2612 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2613 uhci_soft_qh_t *sqh;
2616 DPRINTFN(5, ("uhci_intr_done: length=%d\n", xfer->actlen));
2618 npoll = upipe->u.intr.npoll;
2619 for(i = 0; i < npoll; i++) {
2620 sqh = upipe->u.intr.qhs[i];
2622 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2624 uhci_free_std_chain(sc, ii->stdstart, NULL);
2627 if (xfer->pipe->repeat) {
2628 uhci_soft_td_t *data, *dataend;
2630 DPRINTFN(5,("uhci_device_intr_done: requeing\n"));
2632 /* This alloc cannot fail since we freed the chain above. */
2633 uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags,
2634 &xfer->dmabuf, &data, &dataend);
2635 dataend->td.td_status |= htole32(UHCI_TD_IOC);
2638 if (uhcidebug > 10) {
2639 DPRINTF(("uhci_device_intr_done: data(1)\n"));
2640 uhci_dump_tds(data);
2641 uhci_dump_qh(upipe->u.intr.qhs[0]);
2645 ii->stdstart = data;
2646 ii->stdend = dataend;
2649 printf("uhci_device_intr_done: not done, ii=%p\n", ii);
2653 for (i = 0; i < npoll; i++) {
2654 sqh = upipe->u.intr.qhs[i];
2656 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD);
2658 xfer->status = USBD_IN_PROGRESS;
2659 /* The ii is already on the examined list, just leave it. */
2661 DPRINTFN(5,("uhci_device_intr_done: removing\n"));
2662 uhci_del_intr_info(ii);
2666 /* Deallocate request data structures */
2668 uhci_device_ctrl_done(usbd_xfer_handle xfer)
2670 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2671 uhci_softc_t *sc = ii->sc;
2672 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2675 if (!(xfer->rqflags & URQ_REQUEST))
2676 panic("uhci_ctrl_done: not a request\n");
2679 uhci_del_intr_info(ii); /* remove from active list */
2681 if (upipe->pipe.device->speed == USB_SPEED_LOW)
2682 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh);
2684 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh);
2686 if (upipe->u.ctl.length != 0)
2687 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend);
2689 DPRINTFN(5, ("uhci_ctrl_done: length=%d\n", xfer->actlen));
2692 /* Deallocate request data structures */
2694 uhci_device_bulk_done(usbd_xfer_handle xfer)
2696 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo;
2697 uhci_softc_t *sc = ii->sc;
2698 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe;
2700 uhci_del_intr_info(ii); /* remove from active list */
2702 uhci_remove_bulk(sc, upipe->u.bulk.sqh);
2704 uhci_free_std_chain(sc, ii->stdstart, NULL);
2706 DPRINTFN(5, ("uhci_bulk_done: length=%d\n", xfer->actlen));
2709 /* Add interrupt QH, called with vflock. */
2711 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2713 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2714 uhci_soft_qh_t *eqh;
2716 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2719 sqh->hlink = eqh->hlink;
2720 sqh->qh.qh_hlink = eqh->qh.qh_hlink;
2722 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH);
2727 /* Remove interrupt QH. */
2729 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh)
2731 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos];
2732 uhci_soft_qh_t *pqh;
2734 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh));
2736 /* See comment in uhci_remove_ctrl() */
2737 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) {
2738 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2739 delay(UHCI_QH_REMOVE_DELAY);
2742 pqh = uhci_find_prev_qh(vf->hqh, sqh);
2743 pqh->hlink = sqh->hlink;
2744 pqh->qh.qh_hlink = sqh->qh.qh_hlink;
2745 delay(UHCI_QH_REMOVE_DELAY);
2752 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival)
2754 uhci_soft_qh_t *sqh;
2756 u_int bestbw, bw, bestoffs, offs;
2758 DPRINTFN(2, ("uhci_setintr: pipe=%p\n", upipe));
2760 printf("uhci_setintr: 0 interval\n");
2761 return (USBD_INVAL);
2764 if (ival > UHCI_VFRAMELIST_COUNT)
2765 ival = UHCI_VFRAMELIST_COUNT;
2766 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival;
2767 DPRINTFN(2, ("uhci_setintr: ival=%d npoll=%d\n", ival, npoll));
2769 upipe->u.intr.npoll = npoll;
2771 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK);
2774 * Figure out which offset in the schedule that has most
2775 * bandwidth left over.
2777 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1))
2778 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) {
2779 for (bw = i = 0; i < npoll; i++)
2780 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth;
2786 DPRINTFN(1, ("uhci_setintr: bw=%d offs=%d\n", bestbw, bestoffs));
2788 for(i = 0; i < npoll; i++) {
2789 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc);
2791 sqh->qh.qh_elink = htole32(UHCI_PTR_T);
2792 sqh->pos = MOD(i * ival + bestoffs);
2797 /* Enter QHs into the controller data structures. */
2798 for(i = 0; i < npoll; i++)
2799 uhci_add_intr(sc, upipe->u.intr.qhs[i]);
2802 DPRINTFN(5, ("uhci_setintr: returns %p\n", upipe));
2803 return (USBD_NORMAL_COMPLETION);
2806 /* Open a new pipe. */
2808 uhci_open(usbd_pipe_handle pipe)
2810 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
2811 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe;
2812 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc;
2816 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n",
2817 pipe, pipe->device->address,
2818 ed->bEndpointAddress, sc->sc_addr));
2820 upipe->aborting = 0;
2821 upipe->nexttoggle = 0;
2823 if (pipe->device->address == sc->sc_addr) {
2824 switch (ed->bEndpointAddress) {
2825 case USB_CONTROL_ENDPOINT:
2826 pipe->methods = &uhci_root_ctrl_methods;
2828 case UE_DIR_IN | UHCI_INTR_ENDPT:
2829 pipe->methods = &uhci_root_intr_methods;
2832 return (USBD_INVAL);
2835 switch (ed->bmAttributes & UE_XFERTYPE) {
2837 pipe->methods = &uhci_device_ctrl_methods;
2838 upipe->u.ctl.sqh = uhci_alloc_sqh(sc);
2839 if (upipe->u.ctl.sqh == NULL)
2841 upipe->u.ctl.setup = uhci_alloc_std(sc);
2842 if (upipe->u.ctl.setup == NULL) {
2843 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2846 upipe->u.ctl.stat = uhci_alloc_std(sc);
2847 if (upipe->u.ctl.stat == NULL) {
2848 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2849 uhci_free_std(sc, upipe->u.ctl.setup);
2852 err = usb_allocmem(&sc->sc_bus,
2853 sizeof(usb_device_request_t),
2854 0, &upipe->u.ctl.reqdma);
2856 uhci_free_sqh(sc, upipe->u.ctl.sqh);
2857 uhci_free_std(sc, upipe->u.ctl.setup);
2858 uhci_free_std(sc, upipe->u.ctl.stat);
2863 pipe->methods = &uhci_device_intr_methods;
2864 ival = pipe->interval;
2865 if (ival == USBD_DEFAULT_INTERVAL)
2866 ival = ed->bInterval;
2867 return (uhci_device_setintr(sc, upipe, ival));
2868 case UE_ISOCHRONOUS:
2869 pipe->methods = &uhci_device_isoc_methods;
2870 return (uhci_setup_isoc(pipe));
2872 pipe->methods = &uhci_device_bulk_methods;
2873 upipe->u.bulk.sqh = uhci_alloc_sqh(sc);
2874 if (upipe->u.bulk.sqh == NULL)
2879 return (USBD_NORMAL_COMPLETION);
2882 return (USBD_NOMEM);
2886 * Data structures and routines to emulate the root hub.
2888 usb_device_descriptor_t uhci_devd = {
2889 USB_DEVICE_DESCRIPTOR_SIZE,
2890 UDESC_DEVICE, /* type */
2891 {0x00, 0x01}, /* USB version */
2892 UDCLASS_HUB, /* class */
2893 UDSUBCLASS_HUB, /* subclass */
2894 UDPROTO_FSHUB, /* protocol */
2895 64, /* max packet */
2896 {0},{0},{0x00,0x01}, /* device id */
2897 1,2,0, /* string indicies */
2898 1 /* # of configurations */
2901 usb_config_descriptor_t uhci_confd = {
2902 USB_CONFIG_DESCRIPTOR_SIZE,
2904 {USB_CONFIG_DESCRIPTOR_SIZE +
2905 USB_INTERFACE_DESCRIPTOR_SIZE +
2906 USB_ENDPOINT_DESCRIPTOR_SIZE},
2914 usb_interface_descriptor_t uhci_ifcd = {
2915 USB_INTERFACE_DESCRIPTOR_SIZE,
2926 usb_endpoint_descriptor_t uhci_endpd = {
2927 USB_ENDPOINT_DESCRIPTOR_SIZE,
2929 UE_DIR_IN | UHCI_INTR_ENDPT,
2935 usb_hub_descriptor_t uhci_hubd_piix = {
2936 USB_HUB_DESCRIPTOR_SIZE,
2939 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 },
2940 50, /* power on to power good */
2942 { 0x00 }, /* both ports are removable */
2946 uhci_str(usb_string_descriptor_t *p, int l, char *s)
2952 p->bLength = 2 * strlen(s) + 2;
2955 p->bDescriptorType = UDESC_STRING;
2957 for (i = 0; s[i] && l > 1; i++, l -= 2)
2958 USETW2(p->bString[i], 0, s[i]);
2963 * Simulate a hardware hub by handling all the necessary requests.
2966 uhci_root_ctrl_transfer(usbd_xfer_handle xfer)
2970 /* Insert last in queue. */
2971 err = usb_insert_transfer(xfer);
2976 * Pipe isn't running (otherwise err would be USBD_INPROG),
2977 * so start it first.
2979 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
2983 uhci_root_ctrl_start(usbd_xfer_handle xfer)
2985 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
2986 usb_device_request_t *req;
2989 int s, len, value, index, status, change, l, totlen = 0;
2990 usb_port_status_t ps;
2994 return (USBD_IOERROR);
2997 if (!(xfer->rqflags & URQ_REQUEST))
2998 panic("uhci_root_ctrl_transfer: not a request\n");
3000 req = &xfer->request;
3002 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n",
3003 req->bmRequestType, req->bRequest));
3005 len = UGETW(req->wLength);
3006 value = UGETW(req->wValue);
3007 index = UGETW(req->wIndex);
3010 buf = KERNADDR(&xfer->dmabuf, 0);
3012 #define C(x,y) ((x) | ((y) << 8))
3013 switch(C(req->bRequest, req->bmRequestType)) {
3014 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3015 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3016 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3018 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3019 * for the integrated root hub.
3022 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3024 *(u_int8_t *)buf = sc->sc_conf;
3028 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3029 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value));
3030 switch(value >> 8) {
3032 if ((value & 0xff) != 0) {
3036 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE);
3037 USETW(uhci_devd.idVendor, sc->sc_id_vendor);
3038 memcpy(buf, &uhci_devd, l);
3041 if ((value & 0xff) != 0) {
3045 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE);
3046 memcpy(buf, &uhci_confd, l);
3047 buf = (char *)buf + l;
3049 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE);
3051 memcpy(buf, &uhci_ifcd, l);
3052 buf = (char *)buf + l;
3054 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE);
3056 memcpy(buf, &uhci_endpd, l);
3061 *(u_int8_t *)buf = 0;
3063 switch (value & 0xff) {
3064 case 1: /* Vendor */
3065 totlen = uhci_str(buf, len, sc->sc_vendor);
3067 case 2: /* Product */
3068 totlen = uhci_str(buf, len, "UHCI root hub");
3077 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3079 *(u_int8_t *)buf = 0;
3083 case C(UR_GET_STATUS, UT_READ_DEVICE):
3085 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED);
3089 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3090 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3092 USETW(((usb_status_t *)buf)->wStatus, 0);
3096 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3097 if (value >= USB_MAX_DEVICES) {
3101 sc->sc_addr = value;
3103 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3104 if (value != 0 && value != 1) {
3108 sc->sc_conf = value;
3110 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3112 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3113 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3114 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3117 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3119 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3122 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3124 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3125 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE "
3126 "port=%d feature=%d\n",
3129 port = UHCI_PORTSC1;
3130 else if (index == 2)
3131 port = UHCI_PORTSC2;
3137 case UHF_PORT_ENABLE:
3138 x = URWMASK(UREAD2(sc, port));
3139 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE);
3141 case UHF_PORT_SUSPEND:
3142 x = URWMASK(UREAD2(sc, port));
3143 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP);
3145 case UHF_PORT_RESET:
3146 x = URWMASK(UREAD2(sc, port));
3147 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3149 case UHF_C_PORT_CONNECTION:
3150 x = URWMASK(UREAD2(sc, port));
3151 UWRITE2(sc, port, x | UHCI_PORTSC_CSC);
3153 case UHF_C_PORT_ENABLE:
3154 x = URWMASK(UREAD2(sc, port));
3155 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC);
3157 case UHF_C_PORT_OVER_CURRENT:
3158 x = URWMASK(UREAD2(sc, port));
3159 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC);
3161 case UHF_C_PORT_RESET:
3163 err = USBD_NORMAL_COMPLETION;
3165 case UHF_PORT_CONNECTION:
3166 case UHF_PORT_OVER_CURRENT:
3167 case UHF_PORT_POWER:
3168 case UHF_PORT_LOW_SPEED:
3169 case UHF_C_PORT_SUSPEND:
3175 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER):
3177 port = UHCI_PORTSC1;
3178 else if (index == 2)
3179 port = UHCI_PORTSC2;
3186 (UREAD2(sc, port) & UHCI_PORTSC_LS) >>
3187 UHCI_PORTSC_LS_SHIFT;
3191 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3196 l = min(len, USB_HUB_DESCRIPTOR_SIZE);
3198 memcpy(buf, &uhci_hubd_piix, l);
3200 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3205 memset(buf, 0, len);
3208 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3210 port = UHCI_PORTSC1;
3211 else if (index == 2)
3212 port = UHCI_PORTSC2;
3221 x = UREAD2(sc, port);
3222 status = change = 0;
3223 if (x & UHCI_PORTSC_CCS)
3224 status |= UPS_CURRENT_CONNECT_STATUS;
3225 if (x & UHCI_PORTSC_CSC)
3226 change |= UPS_C_CONNECT_STATUS;
3227 if (x & UHCI_PORTSC_PE)
3228 status |= UPS_PORT_ENABLED;
3229 if (x & UHCI_PORTSC_POEDC)
3230 change |= UPS_C_PORT_ENABLED;
3231 if (x & UHCI_PORTSC_OCI)
3232 status |= UPS_OVERCURRENT_INDICATOR;
3233 if (x & UHCI_PORTSC_OCIC)
3234 change |= UPS_C_OVERCURRENT_INDICATOR;
3235 if (x & UHCI_PORTSC_SUSP)
3236 status |= UPS_SUSPEND;
3237 if (x & UHCI_PORTSC_LSDA)
3238 status |= UPS_LOW_SPEED;
3239 status |= UPS_PORT_POWER;
3241 change |= UPS_C_PORT_RESET;
3242 USETW(ps.wPortStatus, status);
3243 USETW(ps.wPortChange, change);
3244 l = min(len, sizeof ps);
3245 memcpy(buf, &ps, l);
3248 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3251 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3253 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3255 port = UHCI_PORTSC1;
3256 else if (index == 2)
3257 port = UHCI_PORTSC2;
3263 case UHF_PORT_ENABLE:
3264 x = URWMASK(UREAD2(sc, port));
3265 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3267 case UHF_PORT_SUSPEND:
3268 x = URWMASK(UREAD2(sc, port));
3269 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP);
3271 case UHF_PORT_RESET:
3272 x = URWMASK(UREAD2(sc, port));
3273 UWRITE2(sc, port, x | UHCI_PORTSC_PR);
3274 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY);
3275 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR);
3277 x = UREAD2(sc, port);
3278 UWRITE2(sc, port, x | UHCI_PORTSC_PE);
3279 usb_delay_ms(&sc->sc_bus, 10); /* XXX */
3280 DPRINTFN(3,("uhci port %d reset, status = 0x%04x\n",
3281 index, UREAD2(sc, port)));
3284 case UHF_PORT_POWER:
3285 /* Pretend we turned on power */
3286 err = USBD_NORMAL_COMPLETION;
3288 case UHF_C_PORT_CONNECTION:
3289 case UHF_C_PORT_ENABLE:
3290 case UHF_C_PORT_OVER_CURRENT:
3291 case UHF_PORT_CONNECTION:
3292 case UHF_PORT_OVER_CURRENT:
3293 case UHF_PORT_LOW_SPEED:
3294 case UHF_C_PORT_SUSPEND:
3295 case UHF_C_PORT_RESET:
3305 xfer->actlen = totlen;
3306 err = USBD_NORMAL_COMPLETION;
3310 usb_transfer_complete(xfer);
3312 return (USBD_IN_PROGRESS);
3315 /* Abort a root control request. */
3317 uhci_root_ctrl_abort(usbd_xfer_handle xfer)
3319 /* Nothing to do, all transfers are synchronous. */
3322 /* Close the root pipe. */
3324 uhci_root_ctrl_close(usbd_pipe_handle pipe)
3326 DPRINTF(("uhci_root_ctrl_close\n"));
3329 /* Abort a root interrupt request. */
3331 uhci_root_intr_abort(usbd_xfer_handle xfer)
3333 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus;
3335 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer);
3336 sc->sc_intr_xfer = NULL;
3338 if (xfer->pipe->intrxfer == xfer) {
3339 DPRINTF(("uhci_root_intr_abort: remove\n"));
3340 xfer->pipe->intrxfer = 0;
3342 xfer->status = USBD_CANCELLED;
3344 UXFER(xfer)->iinfo.isdone = 1;
3346 usb_transfer_complete(xfer);
3350 uhci_root_intr_transfer(usbd_xfer_handle xfer)
3354 /* Insert last in queue. */
3355 err = usb_insert_transfer(xfer);
3360 * Pipe isn't running (otherwise err would be USBD_INPROG),
3361 * so start it first.
3363 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue)));
3366 /* Start a transfer on the root interrupt pipe */
3368 uhci_root_intr_start(usbd_xfer_handle xfer)
3370 usbd_pipe_handle pipe = xfer->pipe;
3371 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3373 DPRINTFN(3, ("uhci_root_intr_transfer: xfer=%p len=%d flags=%d\n",
3374 xfer, xfer->length, xfer->flags));
3377 return (USBD_IOERROR);
3379 sc->sc_ival = MS_TO_TICKS(xfer->pipe->endpoint->edesc->bInterval);
3380 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer);
3381 sc->sc_intr_xfer = xfer;
3382 return (USBD_IN_PROGRESS);
3385 /* Close the root interrupt pipe. */
3387 uhci_root_intr_close(usbd_pipe_handle pipe)
3389 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus;
3391 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer);
3392 sc->sc_intr_xfer = NULL;
3393 DPRINTF(("uhci_root_intr_close\n"));