2 * Copyright (c) 2010 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * USB eXtensible Host Controller Interface, a.k.a. USB 3.0 controller.
29 * The XHCI 1.0 spec can be found at
30 * http://www.intel.com/technology/usb/download/xHCI_Specification_for_USB.pdf
31 * and the USB 3.0 spec at
32 * http://www.usb.org/developers/docs/usb_30_spec_060910.zip
36 * A few words about the design implementation: This driver emulates
37 * the concept about TDs which is found in EHCI specification. This
38 * way we avoid too much diveration among USB drivers.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/stdint.h>
45 #include <sys/stddef.h>
46 #include <sys/param.h>
47 #include <sys/queue.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
52 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/sysctl.h>
58 #include <sys/unistd.h>
59 #include <sys/callout.h>
60 #include <sys/malloc.h>
63 #include <dev/usb/usb.h>
64 #include <dev/usb/usbdi.h>
66 #define USB_DEBUG_VAR xhcidebug
68 #include <dev/usb/usb_core.h>
69 #include <dev/usb/usb_debug.h>
70 #include <dev/usb/usb_busdma.h>
71 #include <dev/usb/usb_process.h>
72 #include <dev/usb/usb_transfer.h>
73 #include <dev/usb/usb_device.h>
74 #include <dev/usb/usb_hub.h>
75 #include <dev/usb/usb_util.h>
77 #include <dev/usb/usb_controller.h>
78 #include <dev/usb/usb_bus.h>
79 #include <dev/usb/controller/xhci.h>
80 #include <dev/usb/controller/xhcireg.h>
82 #define XHCI_BUS2SC(bus) \
83 ((struct xhci_softc *)(((uint8_t *)(bus)) - \
84 ((uint8_t *)&(((struct xhci_softc *)0)->sc_bus))))
90 SYSCTL_NODE(_hw_usb, OID_AUTO, xhci, CTLFLAG_RW, 0, "USB XHCI");
91 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN,
92 &xhcidebug, 0, "Debug level");
93 TUNABLE_INT("hw.usb.xhci.debug", &xhcidebug);
94 SYSCTL_INT(_hw_usb_xhci, OID_AUTO, xhci_port_route, CTLFLAG_RW | CTLFLAG_TUN,
95 &xhciroute, 0, "Routing bitmap for switching EHCI ports to XHCI controller");
96 TUNABLE_INT("hw.usb.xhci.xhci_port_route", &xhciroute);
101 #define XHCI_INTR_ENDPT 1
103 struct xhci_std_temp {
104 struct xhci_softc *sc;
105 struct usb_page_cache *pc;
107 struct xhci_td *td_next;
110 uint32_t max_packet_size;
122 uint8_t do_isoc_sync;
125 static void xhci_do_poll(struct usb_bus *);
126 static void xhci_device_done(struct usb_xfer *, usb_error_t);
127 static void xhci_root_intr(struct xhci_softc *);
128 static void xhci_free_device_ext(struct usb_device *);
129 static struct xhci_endpoint_ext *xhci_get_endpoint_ext(struct usb_device *,
130 struct usb_endpoint_descriptor *);
131 static usb_proc_callback_t xhci_configure_msg;
132 static usb_error_t xhci_configure_device(struct usb_device *);
133 static usb_error_t xhci_configure_endpoint(struct usb_device *,
134 struct usb_endpoint_descriptor *, uint64_t, uint16_t,
135 uint8_t, uint8_t, uint8_t, uint16_t, uint16_t);
136 static usb_error_t xhci_configure_mask(struct usb_device *,
138 static usb_error_t xhci_cmd_evaluate_ctx(struct xhci_softc *,
140 static void xhci_endpoint_doorbell(struct usb_xfer *);
141 static void xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val);
142 static uint32_t xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr);
143 static void xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val);
145 static uint64_t xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr);
148 extern struct usb_bus_methods xhci_bus_methods;
152 xhci_dump_trb(struct xhci_trb *trb)
154 DPRINTFN(5, "trb = %p\n", trb);
155 DPRINTFN(5, "qwTrb0 = 0x%016llx\n", (long long)le64toh(trb->qwTrb0));
156 DPRINTFN(5, "dwTrb2 = 0x%08x\n", le32toh(trb->dwTrb2));
157 DPRINTFN(5, "dwTrb3 = 0x%08x\n", le32toh(trb->dwTrb3));
161 xhci_dump_endpoint(struct xhci_softc *sc, struct xhci_endp_ctx *pep)
163 DPRINTFN(5, "pep = %p\n", pep);
164 DPRINTFN(5, "dwEpCtx0=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx0));
165 DPRINTFN(5, "dwEpCtx1=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx1));
166 DPRINTFN(5, "qwEpCtx2=0x%016llx\n", (long long)xhci_ctx_get_le64(sc, &pep->qwEpCtx2));
167 DPRINTFN(5, "dwEpCtx4=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx4));
168 DPRINTFN(5, "dwEpCtx5=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx5));
169 DPRINTFN(5, "dwEpCtx6=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx6));
170 DPRINTFN(5, "dwEpCtx7=0x%08x\n", xhci_ctx_get_le32(sc, &pep->dwEpCtx7));
174 xhci_dump_device(struct xhci_softc *sc, struct xhci_slot_ctx *psl)
176 DPRINTFN(5, "psl = %p\n", psl);
177 DPRINTFN(5, "dwSctx0=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx0));
178 DPRINTFN(5, "dwSctx1=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx1));
179 DPRINTFN(5, "dwSctx2=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx2));
180 DPRINTFN(5, "dwSctx3=0x%08x\n", xhci_ctx_get_le32(sc, &psl->dwSctx3));
185 xhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb)
187 struct xhci_softc *sc = XHCI_BUS2SC(bus);
190 cb(bus, &sc->sc_hw.root_pc, &sc->sc_hw.root_pg,
191 sizeof(struct xhci_hw_root), XHCI_PAGE_SIZE);
193 cb(bus, &sc->sc_hw.ctx_pc, &sc->sc_hw.ctx_pg,
194 sizeof(struct xhci_dev_ctx_addr), XHCI_PAGE_SIZE);
196 for (i = 0; i != XHCI_MAX_SCRATCHPADS; i++) {
197 cb(bus, &sc->sc_hw.scratch_pc[i], &sc->sc_hw.scratch_pg[i],
198 XHCI_PAGE_SIZE, XHCI_PAGE_SIZE);
203 xhci_ctx_set_le32(struct xhci_softc *sc, volatile uint32_t *ptr, uint32_t val)
205 if (sc->sc_ctx_is_64_byte) {
207 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
208 /* all contexts are initially 32-bytes */
209 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
210 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
216 xhci_ctx_get_le32(struct xhci_softc *sc, volatile uint32_t *ptr)
218 if (sc->sc_ctx_is_64_byte) {
220 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
221 /* all contexts are initially 32-bytes */
222 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
223 ptr = (volatile uint32_t *)(((volatile uint8_t *)ptr) + offset);
225 return (le32toh(*ptr));
229 xhci_ctx_set_le64(struct xhci_softc *sc, volatile uint64_t *ptr, uint64_t val)
231 if (sc->sc_ctx_is_64_byte) {
233 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
234 /* all contexts are initially 32-bytes */
235 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
236 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
243 xhci_ctx_get_le64(struct xhci_softc *sc, volatile uint64_t *ptr)
245 if (sc->sc_ctx_is_64_byte) {
247 /* exploit the fact that our structures are XHCI_PAGE_SIZE aligned */
248 /* all contexts are initially 32-bytes */
249 offset = ((uintptr_t)ptr) & ((XHCI_PAGE_SIZE - 1) & ~(31U));
250 ptr = (volatile uint64_t *)(((volatile uint8_t *)ptr) + offset);
252 return (le64toh(*ptr));
257 xhci_start_controller(struct xhci_softc *sc)
259 struct usb_page_search buf_res;
260 struct xhci_hw_root *phwr;
261 struct xhci_dev_ctx_addr *pdctxa;
269 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
270 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0x1F;
271 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
273 DPRINTF("CAPLENGTH=0x%x\n", sc->sc_oper_off);
274 DPRINTF("RUNTIMEOFFSET=0x%x\n", sc->sc_runt_off);
275 DPRINTF("DOOROFFSET=0x%x\n", sc->sc_door_off);
277 sc->sc_event_ccs = 1;
278 sc->sc_event_idx = 0;
279 sc->sc_command_ccs = 1;
280 sc->sc_command_idx = 0;
282 DPRINTF("xHCI version = 0x%04x\n", XREAD2(sc, capa, XHCI_HCIVERSION));
284 temp = XREAD4(sc, capa, XHCI_HCSPARAMS0);
286 DPRINTF("HCS0 = 0x%08x\n", temp);
288 if (XHCI_HCS0_CSZ(temp)) {
289 sc->sc_ctx_is_64_byte = 1;
290 device_printf(sc->sc_bus.parent, "64 byte context size.\n");
292 sc->sc_ctx_is_64_byte = 0;
293 device_printf(sc->sc_bus.parent, "32 byte context size.\n");
296 /* Reset controller */
297 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_HCRST);
299 for (i = 0; i != 100; i++) {
300 usb_pause_mtx(NULL, hz / 100);
301 temp = XREAD4(sc, oper, XHCI_USBCMD) &
302 (XHCI_CMD_HCRST | XHCI_STS_CNR);
308 device_printf(sc->sc_bus.parent, "Controller "
310 return (USB_ERR_IOERROR);
313 if (!(XREAD4(sc, oper, XHCI_PAGESIZE) & XHCI_PAGESIZE_4K)) {
314 device_printf(sc->sc_bus.parent, "Controller does "
315 "not support 4K page size.\n");
316 return (USB_ERR_IOERROR);
319 temp = XREAD4(sc, capa, XHCI_HCSPARAMS1);
321 i = XHCI_HCS1_N_PORTS(temp);
324 device_printf(sc->sc_bus.parent, "Invalid number "
325 "of ports: %u\n", i);
326 return (USB_ERR_IOERROR);
330 sc->sc_noslot = XHCI_HCS1_DEVSLOT_MAX(temp);
332 if (sc->sc_noslot > XHCI_MAX_DEVICES)
333 sc->sc_noslot = XHCI_MAX_DEVICES;
335 /* setup number of device slots */
337 DPRINTF("CONFIG=0x%08x -> 0x%08x\n",
338 XREAD4(sc, oper, XHCI_CONFIG), sc->sc_noslot);
340 XWRITE4(sc, oper, XHCI_CONFIG, sc->sc_noslot);
342 DPRINTF("Max slots: %u\n", sc->sc_noslot);
344 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
346 sc->sc_noscratch = XHCI_HCS2_SPB_MAX(temp);
348 if (sc->sc_noscratch > XHCI_MAX_SCRATCHPADS) {
349 device_printf(sc->sc_bus.parent, "XHCI request "
350 "too many scratchpads\n");
351 return (USB_ERR_NOMEM);
354 DPRINTF("Max scratch: %u\n", sc->sc_noscratch);
356 temp = XREAD4(sc, capa, XHCI_HCSPARAMS3);
358 sc->sc_exit_lat_max = XHCI_HCS3_U1_DEL(temp) +
359 XHCI_HCS3_U2_DEL(temp) + 250 /* us */;
361 temp = XREAD4(sc, oper, XHCI_USBSTS);
363 /* clear interrupts */
364 XWRITE4(sc, oper, XHCI_USBSTS, temp);
365 /* disable all device notifications */
366 XWRITE4(sc, oper, XHCI_DNCTRL, 0);
368 /* setup device context base address */
369 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
370 pdctxa = buf_res.buffer;
371 memset(pdctxa, 0, sizeof(*pdctxa));
373 addr = buf_res.physaddr;
374 addr += (uintptr_t)&((struct xhci_dev_ctx_addr *)0)->qwSpBufPtr[0];
376 /* slot 0 points to the table of scratchpad pointers */
377 pdctxa->qwBaaDevCtxAddr[0] = htole64(addr);
379 for (i = 0; i != sc->sc_noscratch; i++) {
380 struct usb_page_search buf_scp;
381 usbd_get_page(&sc->sc_hw.scratch_pc[i], 0, &buf_scp);
382 pdctxa->qwSpBufPtr[i] = htole64((uint64_t)buf_scp.physaddr);
385 addr = buf_res.physaddr;
387 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
388 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
389 XWRITE4(sc, oper, XHCI_DCBAAP_LO, (uint32_t)addr);
390 XWRITE4(sc, oper, XHCI_DCBAAP_HI, (uint32_t)(addr >> 32));
392 /* Setup event table size */
394 temp = XREAD4(sc, capa, XHCI_HCSPARAMS2);
396 DPRINTF("HCS2=0x%08x\n", temp);
398 temp = XHCI_HCS2_ERST_MAX(temp);
400 if (temp > XHCI_MAX_RSEG)
401 temp = XHCI_MAX_RSEG;
403 sc->sc_erst_max = temp;
405 DPRINTF("ERSTSZ=0x%08x -> 0x%08x\n",
406 XREAD4(sc, runt, XHCI_ERSTSZ(0)), temp);
408 XWRITE4(sc, runt, XHCI_ERSTSZ(0), XHCI_ERSTS_SET(temp));
410 /* Setup interrupt rate */
411 XWRITE4(sc, runt, XHCI_IMOD(0), XHCI_IMOD_DEFAULT);
413 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
415 phwr = buf_res.buffer;
416 addr = buf_res.physaddr;
417 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[0];
419 /* reset hardware root structure */
420 memset(phwr, 0, sizeof(*phwr));
422 phwr->hwr_ring_seg[0].qwEvrsTablePtr = htole64(addr);
423 phwr->hwr_ring_seg[0].dwEvrsTableSize = htole32(XHCI_MAX_EVENTS);
425 DPRINTF("ERDP(0)=0x%016llx\n", (unsigned long long)addr);
427 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
428 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
430 addr = (uint64_t)buf_res.physaddr;
432 DPRINTF("ERSTBA(0)=0x%016llx\n", (unsigned long long)addr);
434 XWRITE4(sc, runt, XHCI_ERSTBA_LO(0), (uint32_t)addr);
435 XWRITE4(sc, runt, XHCI_ERSTBA_HI(0), (uint32_t)(addr >> 32));
437 /* Setup interrupter registers */
439 temp = XREAD4(sc, runt, XHCI_IMAN(0));
440 temp |= XHCI_IMAN_INTR_ENA;
441 XWRITE4(sc, runt, XHCI_IMAN(0), temp);
443 /* setup command ring control base address */
444 addr = buf_res.physaddr;
445 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[0];
447 DPRINTF("CRCR=0x%016llx\n", (unsigned long long)addr);
449 XWRITE4(sc, oper, XHCI_CRCR_LO, ((uint32_t)addr) | XHCI_CRCR_LO_RCS);
450 XWRITE4(sc, oper, XHCI_CRCR_HI, (uint32_t)(addr >> 32));
452 phwr->hwr_commands[XHCI_MAX_COMMANDS - 1].qwTrb0 = htole64(addr);
454 usb_bus_mem_flush_all(&sc->sc_bus, &xhci_iterate_hw_softc);
457 XWRITE4(sc, oper, XHCI_USBCMD, XHCI_CMD_RS |
458 XHCI_CMD_INTE | XHCI_CMD_HSEE);
460 for (i = 0; i != 100; i++) {
461 usb_pause_mtx(NULL, hz / 100);
462 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
467 XWRITE4(sc, oper, XHCI_USBCMD, 0);
468 device_printf(sc->sc_bus.parent, "Run timeout.\n");
469 return (USB_ERR_IOERROR);
472 /* catch any lost interrupts */
473 xhci_do_poll(&sc->sc_bus);
475 if (sc->sc_port_route != NULL) {
476 /* Route all ports to the XHCI by default */
477 sc->sc_port_route(sc->sc_bus.parent,
478 ~xhciroute, xhciroute);
484 xhci_halt_controller(struct xhci_softc *sc)
492 sc->sc_oper_off = XREAD1(sc, capa, XHCI_CAPLENGTH);
493 sc->sc_runt_off = XREAD4(sc, capa, XHCI_RTSOFF) & ~0xF;
494 sc->sc_door_off = XREAD4(sc, capa, XHCI_DBOFF) & ~0x3;
496 /* Halt controller */
497 XWRITE4(sc, oper, XHCI_USBCMD, 0);
499 for (i = 0; i != 100; i++) {
500 usb_pause_mtx(NULL, hz / 100);
501 temp = XREAD4(sc, oper, XHCI_USBSTS) & XHCI_STS_HCH;
507 device_printf(sc->sc_bus.parent, "Controller halt timeout.\n");
508 return (USB_ERR_IOERROR);
514 xhci_init(struct xhci_softc *sc, device_t self)
516 /* initialise some bus fields */
517 sc->sc_bus.parent = self;
519 /* set the bus revision */
520 sc->sc_bus.usbrev = USB_REV_3_0;
522 /* set up the bus struct */
523 sc->sc_bus.methods = &xhci_bus_methods;
525 /* setup devices array */
526 sc->sc_bus.devices = sc->sc_devices;
527 sc->sc_bus.devices_max = XHCI_MAX_DEVICES;
529 /* setup command queue mutex and condition varible */
530 cv_init(&sc->sc_cmd_cv, "CMDQ");
531 sx_init(&sc->sc_cmd_sx, "CMDQ lock");
533 /* get all DMA memory */
534 if (usb_bus_mem_alloc_all(&sc->sc_bus,
535 USB_GET_DMA_TAG(self), &xhci_iterate_hw_softc)) {
539 sc->sc_config_msg[0].hdr.pm_callback = &xhci_configure_msg;
540 sc->sc_config_msg[0].bus = &sc->sc_bus;
541 sc->sc_config_msg[1].hdr.pm_callback = &xhci_configure_msg;
542 sc->sc_config_msg[1].bus = &sc->sc_bus;
544 if (usb_proc_create(&sc->sc_config_proc,
545 &sc->sc_bus.bus_mtx, device_get_nameunit(self), USB_PRI_MED)) {
546 printf("WARNING: Creation of XHCI configure "
547 "callback process failed.\n");
553 xhci_uninit(struct xhci_softc *sc)
555 usb_proc_free(&sc->sc_config_proc);
557 usb_bus_mem_free_all(&sc->sc_bus, &xhci_iterate_hw_softc);
559 cv_destroy(&sc->sc_cmd_cv);
560 sx_destroy(&sc->sc_cmd_sx);
564 xhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
566 struct xhci_softc *sc = XHCI_BUS2SC(bus);
569 case USB_HW_POWER_SUSPEND:
570 DPRINTF("Stopping the XHCI\n");
571 xhci_halt_controller(sc);
573 case USB_HW_POWER_SHUTDOWN:
574 DPRINTF("Stopping the XHCI\n");
575 xhci_halt_controller(sc);
577 case USB_HW_POWER_RESUME:
578 DPRINTF("Starting the XHCI\n");
579 xhci_start_controller(sc);
587 xhci_generic_done_sub(struct usb_xfer *xfer)
590 struct xhci_td *td_alt_next;
594 td = xfer->td_transfer_cache;
595 td_alt_next = td->alt_next;
597 if (xfer->aframes != xfer->nframes)
598 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0);
602 usb_pc_cpu_invalidate(td->page_cache);
607 DPRINTFN(4, "xfer=%p[%u/%u] rem=%u/%u status=%u\n",
608 xfer, (unsigned int)xfer->aframes,
609 (unsigned int)xfer->nframes,
610 (unsigned int)len, (unsigned int)td->len,
611 (unsigned int)status);
614 * Verify the status length and
615 * add the length to "frlengths[]":
618 /* should not happen */
619 DPRINTF("Invalid status length, "
620 "0x%04x/0x%04x bytes\n", len, td->len);
621 status = XHCI_TRB_ERROR_LENGTH;
622 } else if (xfer->aframes != xfer->nframes) {
623 xfer->frlengths[xfer->aframes] += td->len - len;
625 /* Check for last transfer */
626 if (((void *)td) == xfer->td_transfer_last) {
630 /* Check for transfer error */
631 if (status != XHCI_TRB_ERROR_SHORT_PKT &&
632 status != XHCI_TRB_ERROR_SUCCESS) {
633 /* the transfer is finished */
637 /* Check for short transfer */
639 if (xfer->flags_int.short_frames_ok ||
640 xfer->flags_int.isochronous_xfr ||
641 xfer->flags_int.control_xfr) {
642 /* follow alt next */
645 /* the transfer is finished */
652 if (td->alt_next != td_alt_next) {
653 /* this USB frame is complete */
658 /* update transfer cache */
660 xfer->td_transfer_cache = td;
662 return ((status == XHCI_TRB_ERROR_STALL) ? USB_ERR_STALLED :
663 (status != XHCI_TRB_ERROR_SHORT_PKT &&
664 status != XHCI_TRB_ERROR_SUCCESS) ? USB_ERR_IOERROR :
665 USB_ERR_NORMAL_COMPLETION);
669 xhci_generic_done(struct usb_xfer *xfer)
673 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
674 xfer, xfer->endpoint);
678 xfer->td_transfer_cache = xfer->td_transfer_first;
680 if (xfer->flags_int.control_xfr) {
682 if (xfer->flags_int.control_hdr)
683 err = xhci_generic_done_sub(xfer);
687 if (xfer->td_transfer_cache == NULL)
691 while (xfer->aframes != xfer->nframes) {
693 err = xhci_generic_done_sub(xfer);
696 if (xfer->td_transfer_cache == NULL)
700 if (xfer->flags_int.control_xfr &&
701 !xfer->flags_int.control_act)
702 err = xhci_generic_done_sub(xfer);
704 /* transfer is complete */
705 xhci_device_done(xfer, err);
709 xhci_activate_transfer(struct usb_xfer *xfer)
713 td = xfer->td_transfer_cache;
715 usb_pc_cpu_invalidate(td->page_cache);
717 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
719 /* activate the transfer */
721 td->td_trb[0].dwTrb3 |= htole32(XHCI_TRB_3_CYCLE_BIT);
722 usb_pc_cpu_flush(td->page_cache);
724 xhci_endpoint_doorbell(xfer);
729 xhci_skip_transfer(struct usb_xfer *xfer)
732 struct xhci_td *td_last;
734 td = xfer->td_transfer_cache;
735 td_last = xfer->td_transfer_last;
739 usb_pc_cpu_invalidate(td->page_cache);
741 if (!(td->td_trb[0].dwTrb3 & htole32(XHCI_TRB_3_CYCLE_BIT))) {
743 usb_pc_cpu_invalidate(td_last->page_cache);
745 /* copy LINK TRB to current waiting location */
747 td->td_trb[0].qwTrb0 = td_last->td_trb[td_last->ntrb].qwTrb0;
748 td->td_trb[0].dwTrb2 = td_last->td_trb[td_last->ntrb].dwTrb2;
749 usb_pc_cpu_flush(td->page_cache);
751 td->td_trb[0].dwTrb3 = td_last->td_trb[td_last->ntrb].dwTrb3;
752 usb_pc_cpu_flush(td->page_cache);
754 xhci_endpoint_doorbell(xfer);
758 /*------------------------------------------------------------------------*
759 * xhci_check_transfer
760 *------------------------------------------------------------------------*/
762 xhci_check_transfer(struct xhci_softc *sc, struct xhci_trb *trb)
775 td_event = le64toh(trb->qwTrb0);
776 temp = le32toh(trb->dwTrb2);
778 remainder = XHCI_TRB_2_REM_GET(temp);
779 status = XHCI_TRB_2_ERROR_GET(temp);
781 temp = le32toh(trb->dwTrb3);
782 epno = XHCI_TRB_3_EP_GET(temp);
783 index = XHCI_TRB_3_SLOT_GET(temp);
785 /* check if error means halted */
786 halted = (status != XHCI_TRB_ERROR_SHORT_PKT &&
787 status != XHCI_TRB_ERROR_SUCCESS);
789 DPRINTF("slot=%u epno=%u remainder=%u status=%u\n",
790 index, epno, remainder, status);
792 if (index > sc->sc_noslot) {
793 DPRINTF("Invalid slot.\n");
797 if ((epno == 0) || (epno >= XHCI_MAX_ENDPOINTS)) {
798 DPRINTF("Invalid endpoint.\n");
802 /* try to find the USB transfer that generated the event */
803 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
804 struct usb_xfer *xfer;
806 struct xhci_endpoint_ext *pepext;
808 pepext = &sc->sc_hw.devs[index].endp[epno];
810 xfer = pepext->xfer[i];
814 td = xfer->td_transfer_cache;
816 DPRINTFN(5, "Checking if 0x%016llx == (0x%016llx .. 0x%016llx)\n",
818 (long long)td->td_self,
819 (long long)td->td_self + sizeof(td->td_trb));
822 * NOTE: Some XHCI implementations might not trigger
823 * an event on the last LINK TRB so we need to
824 * consider both the last and second last event
825 * address as conditions for a successful transfer.
827 * NOTE: We assume that the XHCI will only trigger one
828 * event per chain of TRBs.
831 offset = td_event - td->td_self;
834 offset < (int64_t)sizeof(td->td_trb)) {
836 usb_pc_cpu_invalidate(td->page_cache);
838 /* compute rest of remainder, if any */
839 for (i = (offset / 16) + 1; i < td->ntrb; i++) {
840 temp = le32toh(td->td_trb[i].dwTrb2);
841 remainder += XHCI_TRB_2_BYTES_GET(temp);
844 DPRINTFN(5, "New remainder: %u\n", remainder);
846 /* clear isochronous transfer errors */
847 if (xfer->flags_int.isochronous_xfr) {
850 status = XHCI_TRB_ERROR_SUCCESS;
855 /* "td->remainder" is verified later */
856 td->remainder = remainder;
859 usb_pc_cpu_flush(td->page_cache);
862 * 1) Last transfer descriptor makes the
865 if (((void *)td) == xfer->td_transfer_last) {
866 DPRINTF("TD is last\n");
867 xhci_generic_done(xfer);
872 * 2) Any kind of error makes the transfer
876 DPRINTF("TD has I/O error\n");
877 xhci_generic_done(xfer);
882 * 3) If there is no alternate next transfer,
883 * a short packet also makes the transfer done
885 if (td->remainder > 0) {
886 if (td->alt_next == NULL) {
888 "short TD has no alternate next\n");
889 xhci_generic_done(xfer);
892 DPRINTF("TD has short pkt\n");
893 if (xfer->flags_int.short_frames_ok ||
894 xfer->flags_int.isochronous_xfr ||
895 xfer->flags_int.control_xfr) {
896 /* follow the alt next */
897 xfer->td_transfer_cache = td->alt_next;
898 xhci_activate_transfer(xfer);
901 xhci_skip_transfer(xfer);
902 xhci_generic_done(xfer);
907 * 4) Transfer complete - go to next TD
909 DPRINTF("Following next TD\n");
910 xfer->td_transfer_cache = td->obj_next;
911 xhci_activate_transfer(xfer);
912 break; /* there should only be one match */
918 xhci_check_command(struct xhci_softc *sc, struct xhci_trb *trb)
920 if (sc->sc_cmd_addr == trb->qwTrb0) {
921 DPRINTF("Received command event\n");
922 sc->sc_cmd_result[0] = trb->dwTrb2;
923 sc->sc_cmd_result[1] = trb->dwTrb3;
924 cv_signal(&sc->sc_cmd_cv);
925 return (1); /* command match */
931 xhci_interrupt_poll(struct xhci_softc *sc)
933 struct usb_page_search buf_res;
934 struct xhci_hw_root *phwr;
944 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
946 phwr = buf_res.buffer;
948 /* Receive any events */
950 usb_pc_cpu_invalidate(&sc->sc_hw.root_pc);
952 i = sc->sc_event_idx;
953 j = sc->sc_event_ccs;
958 temp = le32toh(phwr->hwr_events[i].dwTrb3);
960 k = (temp & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0;
965 event = XHCI_TRB_3_TYPE_GET(temp);
967 DPRINTFN(10, "event[%u] = %u (0x%016llx 0x%08lx 0x%08lx)\n",
968 i, event, (long long)le64toh(phwr->hwr_events[i].qwTrb0),
969 (long)le32toh(phwr->hwr_events[i].dwTrb2),
970 (long)le32toh(phwr->hwr_events[i].dwTrb3));
973 case XHCI_TRB_EVENT_TRANSFER:
974 xhci_check_transfer(sc, &phwr->hwr_events[i]);
976 case XHCI_TRB_EVENT_CMD_COMPLETE:
977 retval |= xhci_check_command(sc, &phwr->hwr_events[i]);
980 DPRINTF("Unhandled event = %u\n", event);
986 if (i == XHCI_MAX_EVENTS) {
990 /* check for timeout */
996 sc->sc_event_idx = i;
997 sc->sc_event_ccs = j;
1000 * NOTE: The Event Ring Dequeue Pointer Register is 64-bit
1001 * latched. That means to activate the register we need to
1002 * write both the low and high double word of the 64-bit
1006 addr = (uint32_t)buf_res.physaddr;
1007 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_events[i];
1009 /* try to clear busy bit */
1010 addr |= XHCI_ERDP_LO_BUSY;
1012 XWRITE4(sc, runt, XHCI_ERDP_LO(0), (uint32_t)addr);
1013 XWRITE4(sc, runt, XHCI_ERDP_HI(0), (uint32_t)(addr >> 32));
1019 xhci_do_command(struct xhci_softc *sc, struct xhci_trb *trb,
1020 uint16_t timeout_ms)
1022 struct usb_page_search buf_res;
1023 struct xhci_hw_root *phwr;
1030 XHCI_CMD_ASSERT_LOCKED(sc);
1032 /* get hardware root structure */
1034 usbd_get_page(&sc->sc_hw.root_pc, 0, &buf_res);
1036 phwr = buf_res.buffer;
1040 USB_BUS_LOCK(&sc->sc_bus);
1042 i = sc->sc_command_idx;
1043 j = sc->sc_command_ccs;
1045 DPRINTFN(10, "command[%u] = %u (0x%016llx, 0x%08lx, 0x%08lx)\n",
1046 i, XHCI_TRB_3_TYPE_GET(le32toh(trb->dwTrb3)),
1047 (long long)le64toh(trb->qwTrb0),
1048 (long)le32toh(trb->dwTrb2),
1049 (long)le32toh(trb->dwTrb3));
1051 phwr->hwr_commands[i].qwTrb0 = trb->qwTrb0;
1052 phwr->hwr_commands[i].dwTrb2 = trb->dwTrb2;
1054 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1059 temp |= htole32(XHCI_TRB_3_CYCLE_BIT);
1061 temp &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1063 temp &= ~htole32(XHCI_TRB_3_TC_BIT);
1065 phwr->hwr_commands[i].dwTrb3 = temp;
1067 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1069 addr = buf_res.physaddr;
1070 addr += (uintptr_t)&((struct xhci_hw_root *)0)->hwr_commands[i];
1072 sc->sc_cmd_addr = htole64(addr);
1076 if (i == (XHCI_MAX_COMMANDS - 1)) {
1079 temp = htole32(XHCI_TRB_3_TC_BIT |
1080 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1081 XHCI_TRB_3_CYCLE_BIT);
1083 temp = htole32(XHCI_TRB_3_TC_BIT |
1084 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
1087 phwr->hwr_commands[i].dwTrb3 = temp;
1089 usb_pc_cpu_flush(&sc->sc_hw.root_pc);
1095 sc->sc_command_idx = i;
1096 sc->sc_command_ccs = j;
1098 XWRITE4(sc, door, XHCI_DOORBELL(0), 0);
1100 err = cv_timedwait(&sc->sc_cmd_cv, &sc->sc_bus.bus_mtx,
1101 USB_MS_TO_TICKS(timeout_ms));
1104 * In some error cases event interrupts are not generated.
1105 * Poll one time to see if the command has completed.
1107 if (err != 0 && xhci_interrupt_poll(sc) != 0) {
1108 DPRINTF("Command was completed when polling\n");
1112 DPRINTFN(0, "Command timeout!\n");
1113 err = USB_ERR_TIMEOUT;
1117 temp = le32toh(sc->sc_cmd_result[0]);
1118 if (XHCI_TRB_2_ERROR_GET(temp) != XHCI_TRB_ERROR_SUCCESS)
1119 err = USB_ERR_IOERROR;
1121 trb->dwTrb2 = sc->sc_cmd_result[0];
1122 trb->dwTrb3 = sc->sc_cmd_result[1];
1125 USB_BUS_UNLOCK(&sc->sc_bus);
1132 xhci_cmd_nop(struct xhci_softc *sc)
1134 struct xhci_trb trb;
1141 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NOOP);
1143 trb.dwTrb3 = htole32(temp);
1145 return (xhci_do_command(sc, &trb, 100 /* ms */));
1150 xhci_cmd_enable_slot(struct xhci_softc *sc, uint8_t *pslot)
1152 struct xhci_trb trb;
1160 trb.dwTrb3 = htole32(XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT));
1162 err = xhci_do_command(sc, &trb, 100 /* ms */);
1166 temp = le32toh(trb.dwTrb3);
1168 *pslot = XHCI_TRB_3_SLOT_GET(temp);
1175 xhci_cmd_disable_slot(struct xhci_softc *sc, uint8_t slot_id)
1177 struct xhci_trb trb;
1184 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DISABLE_SLOT) |
1185 XHCI_TRB_3_SLOT_SET(slot_id);
1187 trb.dwTrb3 = htole32(temp);
1189 return (xhci_do_command(sc, &trb, 100 /* ms */));
1193 xhci_cmd_set_address(struct xhci_softc *sc, uint64_t input_ctx,
1194 uint8_t bsr, uint8_t slot_id)
1196 struct xhci_trb trb;
1201 trb.qwTrb0 = htole64(input_ctx);
1203 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) |
1204 XHCI_TRB_3_SLOT_SET(slot_id);
1207 temp |= XHCI_TRB_3_BSR_BIT;
1209 trb.dwTrb3 = htole32(temp);
1211 return (xhci_do_command(sc, &trb, 500 /* ms */));
1215 xhci_set_address(struct usb_device *udev, struct mtx *mtx, uint16_t address)
1217 struct usb_page_search buf_inp;
1218 struct usb_page_search buf_dev;
1219 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
1220 struct xhci_hw_dev *hdev;
1221 struct xhci_dev_ctx *pdev;
1222 struct xhci_endpoint_ext *pepext;
1228 /* the root HUB case is not handled here */
1229 if (udev->parent_hub == NULL)
1230 return (USB_ERR_INVAL);
1232 index = udev->controller_slot_id;
1234 hdev = &sc->sc_hw.devs[index];
1241 switch (hdev->state) {
1242 case XHCI_ST_DEFAULT:
1243 case XHCI_ST_ENABLED:
1245 hdev->state = XHCI_ST_ENABLED;
1247 /* set configure mask to slot and EP0 */
1248 xhci_configure_mask(udev, 3, 0);
1250 /* configure input slot context structure */
1251 err = xhci_configure_device(udev);
1254 DPRINTF("Could not configure device\n");
1258 /* configure input endpoint context structure */
1259 switch (udev->speed) {
1261 case USB_SPEED_FULL:
1264 case USB_SPEED_HIGH:
1272 pepext = xhci_get_endpoint_ext(udev,
1273 &udev->ctrl_ep_desc);
1274 err = xhci_configure_endpoint(udev,
1275 &udev->ctrl_ep_desc, pepext->physaddr,
1276 0, 1, 1, 0, mps, mps);
1279 DPRINTF("Could not configure default endpoint\n");
1283 /* execute set address command */
1284 usbd_get_page(&hdev->input_pc, 0, &buf_inp);
1286 err = xhci_cmd_set_address(sc, buf_inp.physaddr,
1287 (address == 0), index);
1290 temp = le32toh(sc->sc_cmd_result[0]);
1291 if (address == 0 && sc->sc_port_route != NULL &&
1292 XHCI_TRB_2_ERROR_GET(temp) ==
1293 XHCI_TRB_ERROR_PARAMETER) {
1294 /* LynxPoint XHCI - ports are not switchable */
1295 /* Un-route all ports from the XHCI */
1296 sc->sc_port_route(sc->sc_bus.parent, 0, ~0);
1298 DPRINTF("Could not set address "
1299 "for slot %u.\n", index);
1304 /* update device address to new value */
1306 usbd_get_page(&hdev->device_pc, 0, &buf_dev);
1307 pdev = buf_dev.buffer;
1308 usb_pc_cpu_invalidate(&hdev->device_pc);
1310 temp = xhci_ctx_get_le32(sc, &pdev->ctx_slot.dwSctx3);
1311 udev->address = XHCI_SCTX_3_DEV_ADDR_GET(temp);
1313 /* update device state to new value */
1316 hdev->state = XHCI_ST_ADDRESSED;
1318 hdev->state = XHCI_ST_DEFAULT;
1322 DPRINTF("Wrong state for set address.\n");
1323 err = USB_ERR_IOERROR;
1326 XHCI_CMD_UNLOCK(sc);
1335 xhci_cmd_configure_ep(struct xhci_softc *sc, uint64_t input_ctx,
1336 uint8_t deconfigure, uint8_t slot_id)
1338 struct xhci_trb trb;
1343 trb.qwTrb0 = htole64(input_ctx);
1345 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP) |
1346 XHCI_TRB_3_SLOT_SET(slot_id);
1349 temp |= XHCI_TRB_3_DCEP_BIT;
1351 trb.dwTrb3 = htole32(temp);
1353 return (xhci_do_command(sc, &trb, 100 /* ms */));
1357 xhci_cmd_evaluate_ctx(struct xhci_softc *sc, uint64_t input_ctx,
1360 struct xhci_trb trb;
1365 trb.qwTrb0 = htole64(input_ctx);
1367 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX) |
1368 XHCI_TRB_3_SLOT_SET(slot_id);
1369 trb.dwTrb3 = htole32(temp);
1371 return (xhci_do_command(sc, &trb, 100 /* ms */));
1375 xhci_cmd_reset_ep(struct xhci_softc *sc, uint8_t preserve,
1376 uint8_t ep_id, uint8_t slot_id)
1378 struct xhci_trb trb;
1385 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP) |
1386 XHCI_TRB_3_SLOT_SET(slot_id) |
1387 XHCI_TRB_3_EP_SET(ep_id);
1390 temp |= XHCI_TRB_3_PRSV_BIT;
1392 trb.dwTrb3 = htole32(temp);
1394 return (xhci_do_command(sc, &trb, 100 /* ms */));
1398 xhci_cmd_set_tr_dequeue_ptr(struct xhci_softc *sc, uint64_t dequeue_ptr,
1399 uint16_t stream_id, uint8_t ep_id, uint8_t slot_id)
1401 struct xhci_trb trb;
1406 trb.qwTrb0 = htole64(dequeue_ptr);
1408 temp = XHCI_TRB_2_STREAM_SET(stream_id);
1409 trb.dwTrb2 = htole32(temp);
1411 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE) |
1412 XHCI_TRB_3_SLOT_SET(slot_id) |
1413 XHCI_TRB_3_EP_SET(ep_id);
1414 trb.dwTrb3 = htole32(temp);
1416 return (xhci_do_command(sc, &trb, 100 /* ms */));
1420 xhci_cmd_stop_ep(struct xhci_softc *sc, uint8_t suspend,
1421 uint8_t ep_id, uint8_t slot_id)
1423 struct xhci_trb trb;
1430 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP) |
1431 XHCI_TRB_3_SLOT_SET(slot_id) |
1432 XHCI_TRB_3_EP_SET(ep_id);
1435 temp |= XHCI_TRB_3_SUSP_EP_BIT;
1437 trb.dwTrb3 = htole32(temp);
1439 return (xhci_do_command(sc, &trb, 100 /* ms */));
1443 xhci_cmd_reset_dev(struct xhci_softc *sc, uint8_t slot_id)
1445 struct xhci_trb trb;
1452 temp = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_DEVICE) |
1453 XHCI_TRB_3_SLOT_SET(slot_id);
1455 trb.dwTrb3 = htole32(temp);
1457 return (xhci_do_command(sc, &trb, 100 /* ms */));
1460 /*------------------------------------------------------------------------*
1461 * xhci_interrupt - XHCI interrupt handler
1462 *------------------------------------------------------------------------*/
1464 xhci_interrupt(struct xhci_softc *sc)
1468 USB_BUS_LOCK(&sc->sc_bus);
1470 status = XREAD4(sc, oper, XHCI_USBSTS);
1474 /* acknowledge interrupts */
1476 XWRITE4(sc, oper, XHCI_USBSTS, status);
1478 DPRINTFN(16, "real interrupt (status=0x%08x)\n", status);
1480 if (status & XHCI_STS_EINT) {
1481 /* check for event(s) */
1482 xhci_interrupt_poll(sc);
1485 if (status & (XHCI_STS_PCD | XHCI_STS_HCH |
1486 XHCI_STS_HSE | XHCI_STS_HCE)) {
1488 if (status & XHCI_STS_PCD) {
1492 if (status & XHCI_STS_HCH) {
1493 printf("%s: host controller halted\n",
1497 if (status & XHCI_STS_HSE) {
1498 printf("%s: host system error\n",
1502 if (status & XHCI_STS_HCE) {
1503 printf("%s: host controller error\n",
1508 USB_BUS_UNLOCK(&sc->sc_bus);
1511 /*------------------------------------------------------------------------*
1512 * xhci_timeout - XHCI timeout handler
1513 *------------------------------------------------------------------------*/
1515 xhci_timeout(void *arg)
1517 struct usb_xfer *xfer = arg;
1519 DPRINTF("xfer=%p\n", xfer);
1521 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1523 /* transfer is transferred */
1524 xhci_device_done(xfer, USB_ERR_TIMEOUT);
1528 xhci_do_poll(struct usb_bus *bus)
1530 struct xhci_softc *sc = XHCI_BUS2SC(bus);
1532 USB_BUS_LOCK(&sc->sc_bus);
1533 xhci_interrupt_poll(sc);
1534 USB_BUS_UNLOCK(&sc->sc_bus);
1538 xhci_setup_generic_chain_sub(struct xhci_std_temp *temp)
1540 struct usb_page_search buf_res;
1542 struct xhci_td *td_next;
1543 struct xhci_td *td_alt_next;
1544 struct xhci_td *td_first;
1545 uint32_t buf_offset;
1550 uint8_t shortpkt_old;
1556 shortpkt_old = temp->shortpkt;
1557 len_old = temp->len;
1564 td_next = td_first = temp->td_next;
1568 if (temp->len == 0) {
1573 /* send a Zero Length Packet, ZLP, last */
1580 average = temp->average;
1582 if (temp->len < average) {
1583 if (temp->len % temp->max_packet_size) {
1586 average = temp->len;
1590 if (td_next == NULL)
1591 panic("%s: out of XHCI transfer descriptors!", __FUNCTION__);
1596 td_next = td->obj_next;
1598 /* check if we are pre-computing */
1602 /* update remaining length */
1604 temp->len -= average;
1608 /* fill out current TD */
1614 /* update remaining length */
1616 temp->len -= average;
1618 /* reset TRB index */
1622 if (temp->trb_type == XHCI_TRB_TYPE_SETUP_STAGE) {
1623 /* immediate data */
1628 td->td_trb[0].qwTrb0 = 0;
1630 usbd_copy_out(temp->pc, temp->offset + buf_offset,
1631 (uint8_t *)(uintptr_t)&td->td_trb[0].qwTrb0,
1634 dword = XHCI_TRB_2_BYTES_SET(8) |
1635 XHCI_TRB_2_TDSZ_SET(0) |
1636 XHCI_TRB_2_IRQ_SET(0);
1638 td->td_trb[0].dwTrb2 = htole32(dword);
1640 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) |
1641 XHCI_TRB_3_IDT_BIT | XHCI_TRB_3_CYCLE_BIT;
1644 if (td->td_trb[0].qwTrb0 &
1645 htole64(XHCI_TRB_0_WLENGTH_MASK)) {
1646 if (td->td_trb[0].qwTrb0 & htole64(1))
1647 dword |= XHCI_TRB_3_TRT_IN;
1649 dword |= XHCI_TRB_3_TRT_OUT;
1652 td->td_trb[0].dwTrb3 = htole32(dword);
1654 xhci_dump_trb(&td->td_trb[x]);
1662 /* fill out buffer pointers */
1665 memset(&buf_res, 0, sizeof(buf_res));
1667 usbd_get_page(temp->pc, temp->offset +
1668 buf_offset, &buf_res);
1670 /* get length to end of page */
1671 if (buf_res.length > average)
1672 buf_res.length = average;
1674 /* check for maximum length */
1675 if (buf_res.length > XHCI_TD_PAGE_SIZE)
1676 buf_res.length = XHCI_TD_PAGE_SIZE;
1678 npkt_off += buf_res.length;
1682 npkt = (len_old - npkt_off + temp->max_packet_size - 1) /
1683 temp->max_packet_size;
1690 /* fill out TRB's */
1691 td->td_trb[x].qwTrb0 =
1692 htole64((uint64_t)buf_res.physaddr);
1695 XHCI_TRB_2_BYTES_SET(buf_res.length) |
1696 XHCI_TRB_2_TDSZ_SET(npkt) |
1697 XHCI_TRB_2_IRQ_SET(0);
1699 td->td_trb[x].dwTrb2 = htole32(dword);
1701 switch (temp->trb_type) {
1702 case XHCI_TRB_TYPE_ISOCH:
1703 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1704 XHCI_TRB_3_TBC_SET(temp->tbc) |
1705 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1706 if (td != td_first) {
1707 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL);
1708 } else if (temp->do_isoc_sync != 0) {
1709 temp->do_isoc_sync = 0;
1710 /* wait until "isoc_frame" */
1711 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1712 XHCI_TRB_3_FRID_SET(temp->isoc_frame / 8);
1714 /* start data transfer at next interval */
1715 dword |= XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ISOCH) |
1716 XHCI_TRB_3_ISO_SIA_BIT;
1718 if (temp->direction == UE_DIR_IN)
1719 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1721 case XHCI_TRB_TYPE_DATA_STAGE:
1722 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1723 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) |
1724 XHCI_TRB_3_TBC_SET(temp->tbc) |
1725 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1726 if (temp->direction == UE_DIR_IN)
1727 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1729 case XHCI_TRB_TYPE_STATUS_STAGE:
1730 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1731 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) |
1732 XHCI_TRB_3_TBC_SET(temp->tbc) |
1733 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1734 if (temp->direction == UE_DIR_IN)
1735 dword |= XHCI_TRB_3_DIR_IN;
1737 default: /* XHCI_TRB_TYPE_NORMAL */
1738 dword = XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_CYCLE_BIT |
1739 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) |
1740 XHCI_TRB_3_TBC_SET(temp->tbc) |
1741 XHCI_TRB_3_TLBPC_SET(temp->tlbpc);
1742 if (temp->direction == UE_DIR_IN)
1743 dword |= XHCI_TRB_3_DIR_IN | XHCI_TRB_3_ISP_BIT;
1746 td->td_trb[x].dwTrb3 = htole32(dword);
1748 average -= buf_res.length;
1749 buf_offset += buf_res.length;
1751 xhci_dump_trb(&td->td_trb[x]);
1755 } while (average != 0);
1757 td->td_trb[x-1].dwTrb3 |= htole32(XHCI_TRB_3_IOC_BIT);
1759 /* store number of data TRB's */
1763 DPRINTF("NTRB=%u\n", x);
1765 /* fill out link TRB */
1767 if (td_next != NULL) {
1768 /* link the current TD with the next one */
1769 td->td_trb[x].qwTrb0 = htole64((uint64_t)td_next->td_self);
1770 DPRINTF("LINK=0x%08llx\n", (long long)td_next->td_self);
1772 /* this field will get updated later */
1773 DPRINTF("NOLINK\n");
1776 dword = XHCI_TRB_2_IRQ_SET(0);
1778 td->td_trb[x].dwTrb2 = htole32(dword);
1780 dword = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) |
1781 XHCI_TRB_3_CYCLE_BIT | XHCI_TRB_3_IOC_BIT;
1783 td->td_trb[x].dwTrb3 = htole32(dword);
1785 td->alt_next = td_alt_next;
1787 xhci_dump_trb(&td->td_trb[x]);
1789 usb_pc_cpu_flush(td->page_cache);
1795 /* setup alt next pointer, if any */
1796 if (temp->last_frame) {
1799 /* we use this field internally */
1800 td_alt_next = td_next;
1804 temp->shortpkt = shortpkt_old;
1805 temp->len = len_old;
1810 * Remove cycle bit from the first TRB if we are
1813 if (temp->step_td != 0) {
1814 td_first->td_trb[0].dwTrb3 &= ~htole32(XHCI_TRB_3_CYCLE_BIT);
1815 usb_pc_cpu_flush(td_first->page_cache);
1818 /* clear TD SIZE to zero, hence this is the last TRB */
1819 /* remove chain bit because this is the last TRB in the chain */
1820 td->td_trb[td->ntrb - 1].dwTrb2 &= ~htole32(XHCI_TRB_2_TDSZ_SET(15));
1821 td->td_trb[td->ntrb - 1].dwTrb3 &= ~htole32(XHCI_TRB_3_CHAIN_BIT);
1823 usb_pc_cpu_flush(td->page_cache);
1826 temp->td_next = td_next;
1830 xhci_setup_generic_chain(struct usb_xfer *xfer)
1832 struct xhci_std_temp temp;
1838 temp.do_isoc_sync = 0;
1842 temp.average = xfer->max_hc_frame_size;
1843 temp.max_packet_size = xfer->max_packet_size;
1844 temp.sc = XHCI_BUS2SC(xfer->xroot->bus);
1846 temp.last_frame = 0;
1848 temp.multishort = xfer->flags_int.isochronous_xfr ||
1849 xfer->flags_int.control_xfr ||
1850 xfer->flags_int.short_frames_ok;
1852 /* toggle the DMA set we are using */
1853 xfer->flags_int.curr_dma_set ^= 1;
1855 /* get next DMA set */
1856 td = xfer->td_start[xfer->flags_int.curr_dma_set];
1861 xfer->td_transfer_first = td;
1862 xfer->td_transfer_cache = td;
1864 if (xfer->flags_int.isochronous_xfr) {
1867 /* compute multiplier for ISOCHRONOUS transfers */
1868 mult = xfer->endpoint->ecomp ?
1869 (xfer->endpoint->ecomp->bmAttributes & 3) : 0;
1870 /* check for USB 2.0 multiplier */
1872 mult = (xfer->endpoint->edesc->
1873 wMaxPacketSize[1] >> 3) & 3;
1881 x = XREAD4(temp.sc, runt, XHCI_MFINDEX);
1883 DPRINTF("MFINDEX=0x%08x\n", x);
1885 switch (usbd_get_speed(xfer->xroot->udev)) {
1886 case USB_SPEED_FULL:
1888 temp.isoc_delta = 8; /* 1ms */
1889 x += temp.isoc_delta - 1;
1890 x &= ~(temp.isoc_delta - 1);
1893 shift = usbd_xfer_get_fps_shift(xfer);
1894 temp.isoc_delta = 1U << shift;
1895 x += temp.isoc_delta - 1;
1896 x &= ~(temp.isoc_delta - 1);
1897 /* simple frame load balancing */
1898 x += xfer->endpoint->usb_uframe;
1902 y = XHCI_MFINDEX_GET(x - xfer->endpoint->isoc_next);
1904 if ((xfer->endpoint->is_synced == 0) ||
1905 (y < (xfer->nframes << shift)) ||
1906 (XHCI_MFINDEX_GET(-y) >= (128 * 8))) {
1908 * If there is data underflow or the pipe
1909 * queue is empty we schedule the transfer a
1910 * few frames ahead of the current frame
1911 * position. Else two isochronous transfers
1914 xfer->endpoint->isoc_next = XHCI_MFINDEX_GET(x + (3 * 8));
1915 xfer->endpoint->is_synced = 1;
1916 temp.do_isoc_sync = 1;
1918 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1921 /* compute isochronous completion time */
1923 y = XHCI_MFINDEX_GET(xfer->endpoint->isoc_next - (x & ~7));
1925 xfer->isoc_time_complete =
1926 usb_isoc_time_expand(&temp.sc->sc_bus, x / 8) +
1927 (y / 8) + (((xfer->nframes << shift) + 7) / 8);
1930 temp.isoc_frame = xfer->endpoint->isoc_next;
1931 temp.trb_type = XHCI_TRB_TYPE_ISOCH;
1933 xfer->endpoint->isoc_next += xfer->nframes << shift;
1935 } else if (xfer->flags_int.control_xfr) {
1937 /* check if we should prepend a setup message */
1939 if (xfer->flags_int.control_hdr) {
1941 temp.len = xfer->frlengths[0];
1942 temp.pc = xfer->frbuffers + 0;
1943 temp.shortpkt = temp.len ? 1 : 0;
1944 temp.trb_type = XHCI_TRB_TYPE_SETUP_STAGE;
1947 /* check for last frame */
1948 if (xfer->nframes == 1) {
1949 /* no STATUS stage yet, SETUP is last */
1950 if (xfer->flags_int.control_act)
1951 temp.last_frame = 1;
1954 xhci_setup_generic_chain_sub(&temp);
1958 temp.isoc_delta = 0;
1959 temp.isoc_frame = 0;
1960 temp.trb_type = XHCI_TRB_TYPE_DATA_STAGE;
1964 temp.isoc_delta = 0;
1965 temp.isoc_frame = 0;
1966 temp.trb_type = XHCI_TRB_TYPE_NORMAL;
1969 if (x != xfer->nframes) {
1970 /* setup page_cache pointer */
1971 temp.pc = xfer->frbuffers + x;
1972 /* set endpoint direction */
1973 temp.direction = UE_GET_DIR(xfer->endpointno);
1976 while (x != xfer->nframes) {
1978 /* DATA0 / DATA1 message */
1980 temp.len = xfer->frlengths[x];
1981 temp.step_td = ((xfer->endpointno & UE_DIR_IN) &&
1982 x != 0 && temp.multishort == 0);
1986 if (x == xfer->nframes) {
1987 if (xfer->flags_int.control_xfr) {
1988 /* no STATUS stage yet, DATA is last */
1989 if (xfer->flags_int.control_act)
1990 temp.last_frame = 1;
1992 temp.last_frame = 1;
1995 if (temp.len == 0) {
1997 /* make sure that we send an USB packet */
2002 temp.tlbpc = mult - 1;
2004 } else if (xfer->flags_int.isochronous_xfr) {
2009 * Isochronous transfers don't have short
2010 * packet termination:
2015 /* isochronous transfers have a transfer limit */
2017 if (temp.len > xfer->max_frame_size)
2018 temp.len = xfer->max_frame_size;
2020 /* compute TD packet count */
2021 tdpc = (temp.len + xfer->max_packet_size - 1) /
2022 xfer->max_packet_size;
2024 temp.tbc = ((tdpc + mult - 1) / mult) - 1;
2025 temp.tlbpc = (tdpc % mult);
2027 if (temp.tlbpc == 0)
2028 temp.tlbpc = mult - 1;
2033 /* regular data transfer */
2035 temp.shortpkt = xfer->flags.force_short_xfer ? 0 : 1;
2038 xhci_setup_generic_chain_sub(&temp);
2040 if (xfer->flags_int.isochronous_xfr) {
2041 temp.offset += xfer->frlengths[x - 1];
2042 temp.isoc_frame += temp.isoc_delta;
2044 /* get next Page Cache pointer */
2045 temp.pc = xfer->frbuffers + x;
2049 /* check if we should append a status stage */
2051 if (xfer->flags_int.control_xfr &&
2052 !xfer->flags_int.control_act) {
2055 * Send a DATA1 message and invert the current
2056 * endpoint direction.
2058 temp.step_td = (xfer->nframes != 0);
2059 temp.direction = UE_GET_DIR(xfer->endpointno) ^ UE_DIR_IN;
2063 temp.last_frame = 1;
2064 temp.trb_type = XHCI_TRB_TYPE_STATUS_STAGE;
2066 xhci_setup_generic_chain_sub(&temp);
2071 /* must have at least one frame! */
2073 xfer->td_transfer_last = td;
2075 DPRINTF("first=%p last=%p\n", xfer->td_transfer_first, td);
2079 xhci_set_slot_pointer(struct xhci_softc *sc, uint8_t index, uint64_t dev_addr)
2081 struct usb_page_search buf_res;
2082 struct xhci_dev_ctx_addr *pdctxa;
2084 usbd_get_page(&sc->sc_hw.ctx_pc, 0, &buf_res);
2086 pdctxa = buf_res.buffer;
2088 DPRINTF("addr[%u]=0x%016llx\n", index, (long long)dev_addr);
2090 pdctxa->qwBaaDevCtxAddr[index] = htole64(dev_addr);
2092 usb_pc_cpu_flush(&sc->sc_hw.ctx_pc);
2096 xhci_configure_mask(struct usb_device *udev, uint32_t mask, uint8_t drop)
2098 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2099 struct usb_page_search buf_inp;
2100 struct xhci_input_dev_ctx *pinp;
2105 index = udev->controller_slot_id;
2107 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2109 pinp = buf_inp.buffer;
2112 mask &= XHCI_INCTX_NON_CTRL_MASK;
2113 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, mask);
2114 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, 0);
2116 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx0, 0);
2117 xhci_ctx_set_le32(sc, &pinp->ctx_input.dwInCtx1, mask);
2119 /* find most significant set bit */
2120 for (x = 31; x != 1; x--) {
2121 if (mask & (1 << x))
2128 /* figure out maximum */
2129 if (x > sc->sc_hw.devs[index].context_num) {
2130 sc->sc_hw.devs[index].context_num = x;
2131 temp = xhci_ctx_get_le32(sc, &pinp->ctx_slot.dwSctx0);
2132 temp &= ~XHCI_SCTX_0_CTX_NUM_SET(31);
2133 temp |= XHCI_SCTX_0_CTX_NUM_SET(x + 1);
2134 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2141 xhci_configure_endpoint(struct usb_device *udev,
2142 struct usb_endpoint_descriptor *edesc, uint64_t ring_addr,
2143 uint16_t interval, uint8_t max_packet_count, uint8_t mult,
2144 uint8_t fps_shift, uint16_t max_packet_size, uint16_t max_frame_size)
2146 struct usb_page_search buf_inp;
2147 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2148 struct xhci_input_dev_ctx *pinp;
2154 index = udev->controller_slot_id;
2156 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
2158 pinp = buf_inp.buffer;
2160 epno = edesc->bEndpointAddress;
2161 type = edesc->bmAttributes & UE_XFERTYPE;
2163 if (type == UE_CONTROL)
2166 epno = XHCI_EPNO2EPID(epno);
2169 return (USB_ERR_NO_PIPE); /* invalid */
2171 if (max_packet_count == 0)
2172 return (USB_ERR_BAD_BUFSIZE);
2177 return (USB_ERR_BAD_BUFSIZE);
2179 temp = XHCI_EPCTX_0_EPSTATE_SET(0) |
2180 XHCI_EPCTX_0_MAXP_STREAMS_SET(0) |
2181 XHCI_EPCTX_0_LSA_SET(0);
2183 switch (udev->speed) {
2184 case USB_SPEED_FULL:
2197 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2199 case UE_ISOCHRONOUS:
2200 temp |= XHCI_EPCTX_0_IVAL_SET(fps_shift);
2202 switch (udev->speed) {
2203 case USB_SPEED_SUPER:
2206 temp |= XHCI_EPCTX_0_MULT_SET(mult - 1);
2207 max_packet_count /= mult;
2217 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx0, temp);
2220 XHCI_EPCTX_1_HID_SET(0) |
2221 XHCI_EPCTX_1_MAXB_SET(max_packet_count) |
2222 XHCI_EPCTX_1_MAXP_SIZE_SET(max_packet_size);
2224 if ((udev->parent_hs_hub != NULL) || (udev->address != 0)) {
2225 if (type != UE_ISOCHRONOUS)
2226 temp |= XHCI_EPCTX_1_CERR_SET(3);
2231 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2233 case UE_ISOCHRONOUS:
2234 temp |= XHCI_EPCTX_1_EPTYPE_SET(1);
2237 temp |= XHCI_EPCTX_1_EPTYPE_SET(2);
2240 temp |= XHCI_EPCTX_1_EPTYPE_SET(3);
2244 /* check for IN direction */
2246 temp |= XHCI_EPCTX_1_EPTYPE_SET(4);
2248 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx1, temp);
2250 ring_addr |= XHCI_EPCTX_2_DCS_SET(1);
2252 xhci_ctx_set_le64(sc, &pinp->ctx_ep[epno - 1].qwEpCtx2, ring_addr);
2254 switch (edesc->bmAttributes & UE_XFERTYPE) {
2256 case UE_ISOCHRONOUS:
2257 temp = XHCI_EPCTX_4_MAX_ESIT_PAYLOAD_SET(max_frame_size) |
2258 XHCI_EPCTX_4_AVG_TRB_LEN_SET(MIN(XHCI_PAGE_SIZE,
2262 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(8);
2265 temp = XHCI_EPCTX_4_AVG_TRB_LEN_SET(XHCI_PAGE_SIZE);
2269 xhci_ctx_set_le32(sc, &pinp->ctx_ep[epno - 1].dwEpCtx4, temp);
2272 xhci_dump_endpoint(sc, &pinp->ctx_ep[epno - 1]);
2274 usb_pc_cpu_flush(&sc->sc_hw.devs[index].input_pc);
2276 return (0); /* success */
2280 xhci_configure_endpoint_by_xfer(struct usb_xfer *xfer)
2282 struct xhci_endpoint_ext *pepext;
2283 struct usb_endpoint_ss_comp_descriptor *ecomp;
2285 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2286 xfer->endpoint->edesc);
2288 ecomp = xfer->endpoint->ecomp;
2290 pepext->trb[0].dwTrb3 = 0; /* halt any transfers */
2291 usb_pc_cpu_flush(pepext->page_cache);
2293 return (xhci_configure_endpoint(xfer->xroot->udev,
2294 xfer->endpoint->edesc, pepext->physaddr,
2295 xfer->interval, xfer->max_packet_count,
2296 (ecomp != NULL) ? (ecomp->bmAttributes & 3) + 1 : 1,
2297 usbd_xfer_get_fps_shift(xfer), xfer->max_packet_size,
2298 xfer->max_frame_size));
2302 xhci_configure_device(struct usb_device *udev)
2304 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2305 struct usb_page_search buf_inp;
2306 struct usb_page_cache *pcinp;
2307 struct xhci_input_dev_ctx *pinp;
2308 struct usb_device *hubdev;
2316 index = udev->controller_slot_id;
2318 DPRINTF("index=%u\n", index);
2320 pcinp = &sc->sc_hw.devs[index].input_pc;
2322 usbd_get_page(pcinp, 0, &buf_inp);
2324 pinp = buf_inp.buffer;
2329 /* figure out route string and root HUB port number */
2331 for (hubdev = udev; hubdev != NULL; hubdev = hubdev->parent_hub) {
2333 if (hubdev->parent_hub == NULL)
2336 depth = hubdev->parent_hub->depth;
2339 * NOTE: HS/FS/LS devices and the SS root HUB can have
2340 * more than 15 ports
2343 rh_port = hubdev->port_no;
2352 route |= rh_port << (4 * (depth - 1));
2355 DPRINTF("Route=0x%08x\n", route);
2357 temp = XHCI_SCTX_0_ROUTE_SET(route) |
2358 XHCI_SCTX_0_CTX_NUM_SET(
2359 sc->sc_hw.devs[index].context_num + 1);
2361 switch (udev->speed) {
2363 temp |= XHCI_SCTX_0_SPEED_SET(2);
2364 if (udev->parent_hs_hub != NULL &&
2365 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2367 DPRINTF("Device inherits MTT\n");
2368 temp |= XHCI_SCTX_0_MTT_SET(1);
2371 case USB_SPEED_HIGH:
2372 temp |= XHCI_SCTX_0_SPEED_SET(3);
2373 if (sc->sc_hw.devs[index].nports != 0 &&
2374 udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) {
2375 DPRINTF("HUB supports MTT\n");
2376 temp |= XHCI_SCTX_0_MTT_SET(1);
2379 case USB_SPEED_FULL:
2380 temp |= XHCI_SCTX_0_SPEED_SET(1);
2381 if (udev->parent_hs_hub != NULL &&
2382 udev->parent_hs_hub->ddesc.bDeviceProtocol ==
2384 DPRINTF("Device inherits MTT\n");
2385 temp |= XHCI_SCTX_0_MTT_SET(1);
2389 temp |= XHCI_SCTX_0_SPEED_SET(4);
2393 is_hub = sc->sc_hw.devs[index].nports != 0 &&
2394 (udev->speed == USB_SPEED_SUPER ||
2395 udev->speed == USB_SPEED_HIGH);
2398 temp |= XHCI_SCTX_0_HUB_SET(1);
2400 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx0, temp);
2402 temp = XHCI_SCTX_1_RH_PORT_SET(rh_port);
2405 temp |= XHCI_SCTX_1_NUM_PORTS_SET(
2406 sc->sc_hw.devs[index].nports);
2409 switch (udev->speed) {
2410 case USB_SPEED_SUPER:
2411 switch (sc->sc_hw.devs[index].state) {
2412 case XHCI_ST_ADDRESSED:
2413 case XHCI_ST_CONFIGURED:
2414 /* enable power save */
2415 temp |= XHCI_SCTX_1_MAX_EL_SET(sc->sc_exit_lat_max);
2418 /* disable power save */
2426 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx1, temp);
2428 temp = XHCI_SCTX_2_IRQ_TARGET_SET(0);
2431 temp |= XHCI_SCTX_2_TT_THINK_TIME_SET(
2432 sc->sc_hw.devs[index].tt);
2435 hubdev = udev->parent_hs_hub;
2437 /* check if we should activate the transaction translator */
2438 switch (udev->speed) {
2439 case USB_SPEED_FULL:
2441 if (hubdev != NULL) {
2442 temp |= XHCI_SCTX_2_TT_HUB_SID_SET(
2443 hubdev->controller_slot_id);
2444 temp |= XHCI_SCTX_2_TT_PORT_NUM_SET(
2452 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx2, temp);
2454 temp = XHCI_SCTX_3_DEV_ADDR_SET(udev->address) |
2455 XHCI_SCTX_3_SLOT_STATE_SET(0);
2457 xhci_ctx_set_le32(sc, &pinp->ctx_slot.dwSctx3, temp);
2460 xhci_dump_device(sc, &pinp->ctx_slot);
2462 usb_pc_cpu_flush(pcinp);
2464 return (0); /* success */
2468 xhci_alloc_device_ext(struct usb_device *udev)
2470 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2471 struct usb_page_search buf_dev;
2472 struct usb_page_search buf_ep;
2473 struct xhci_trb *trb;
2474 struct usb_page_cache *pc;
2475 struct usb_page *pg;
2480 index = udev->controller_slot_id;
2482 pc = &sc->sc_hw.devs[index].device_pc;
2483 pg = &sc->sc_hw.devs[index].device_pg;
2485 /* need to initialize the page cache */
2486 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2488 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2489 (2 * sizeof(struct xhci_dev_ctx)) :
2490 sizeof(struct xhci_dev_ctx), XHCI_PAGE_SIZE))
2493 usbd_get_page(pc, 0, &buf_dev);
2495 pc = &sc->sc_hw.devs[index].input_pc;
2496 pg = &sc->sc_hw.devs[index].input_pg;
2498 /* need to initialize the page cache */
2499 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2501 if (usb_pc_alloc_mem(pc, pg, sc->sc_ctx_is_64_byte ?
2502 (2 * sizeof(struct xhci_input_dev_ctx)) :
2503 sizeof(struct xhci_input_dev_ctx), XHCI_PAGE_SIZE)) {
2507 pc = &sc->sc_hw.devs[index].endpoint_pc;
2508 pg = &sc->sc_hw.devs[index].endpoint_pg;
2510 /* need to initialize the page cache */
2511 pc->tag_parent = sc->sc_bus.dma_parent_tag;
2513 if (usb_pc_alloc_mem(pc, pg,
2514 sizeof(struct xhci_dev_endpoint_trbs), XHCI_PAGE_SIZE)) {
2518 /* initialise all endpoint LINK TRBs */
2520 for (i = 0; i != XHCI_MAX_ENDPOINTS; i++) {
2522 /* lookup endpoint TRB ring */
2523 usbd_get_page(pc, (uintptr_t)&
2524 ((struct xhci_dev_endpoint_trbs *)0)->trb[i][0], &buf_ep);
2526 /* get TRB pointer */
2527 trb = buf_ep.buffer;
2528 trb += XHCI_MAX_TRANSFERS - 1;
2530 /* get TRB start address */
2531 addr = buf_ep.physaddr;
2533 /* create LINK TRB */
2534 trb->qwTrb0 = htole64(addr);
2535 trb->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2536 trb->dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2537 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2540 usb_pc_cpu_flush(pc);
2542 xhci_set_slot_pointer(sc, index, buf_dev.physaddr);
2547 xhci_free_device_ext(udev);
2549 return (USB_ERR_NOMEM);
2553 xhci_free_device_ext(struct usb_device *udev)
2555 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2558 index = udev->controller_slot_id;
2559 xhci_set_slot_pointer(sc, index, 0);
2561 usb_pc_free_mem(&sc->sc_hw.devs[index].device_pc);
2562 usb_pc_free_mem(&sc->sc_hw.devs[index].input_pc);
2563 usb_pc_free_mem(&sc->sc_hw.devs[index].endpoint_pc);
2566 static struct xhci_endpoint_ext *
2567 xhci_get_endpoint_ext(struct usb_device *udev, struct usb_endpoint_descriptor *edesc)
2569 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
2570 struct xhci_endpoint_ext *pepext;
2571 struct usb_page_cache *pc;
2572 struct usb_page_search buf_ep;
2576 epno = edesc->bEndpointAddress;
2577 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
2580 epno = XHCI_EPNO2EPID(epno);
2582 index = udev->controller_slot_id;
2584 pc = &sc->sc_hw.devs[index].endpoint_pc;
2586 usbd_get_page(pc, (uintptr_t)&((struct xhci_dev_endpoint_trbs *)0)->trb[epno][0], &buf_ep);
2588 pepext = &sc->sc_hw.devs[index].endp[epno];
2589 pepext->page_cache = pc;
2590 pepext->trb = buf_ep.buffer;
2591 pepext->physaddr = buf_ep.physaddr;
2597 xhci_endpoint_doorbell(struct usb_xfer *xfer)
2599 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2603 epno = xfer->endpointno;
2604 if (xfer->flags_int.control_xfr)
2607 epno = XHCI_EPNO2EPID(epno);
2608 index = xfer->xroot->udev->controller_slot_id;
2610 if (xfer->xroot->udev->flags.self_suspended == 0) {
2611 XWRITE4(sc, door, XHCI_DOORBELL(index),
2612 epno | XHCI_DB_SID_SET(/*xfer->stream_id*/ 0));
2617 xhci_transfer_remove(struct usb_xfer *xfer, usb_error_t error)
2619 struct xhci_endpoint_ext *pepext;
2621 if (xfer->flags_int.bandwidth_reclaimed) {
2622 xfer->flags_int.bandwidth_reclaimed = 0;
2624 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2625 xfer->endpoint->edesc);
2629 pepext->xfer[xfer->qh_pos] = NULL;
2631 if (error && pepext->trb_running != 0) {
2632 pepext->trb_halted = 1;
2633 pepext->trb_running = 0;
2639 xhci_transfer_insert(struct usb_xfer *xfer)
2641 struct xhci_td *td_first;
2642 struct xhci_td *td_last;
2643 struct xhci_trb *trb_link;
2644 struct xhci_endpoint_ext *pepext;
2652 /* check if already inserted */
2653 if (xfer->flags_int.bandwidth_reclaimed) {
2654 DPRINTFN(8, "Already in schedule\n");
2658 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
2659 xfer->endpoint->edesc);
2661 td_first = xfer->td_transfer_first;
2662 td_last = xfer->td_transfer_last;
2663 addr = pepext->physaddr;
2665 switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
2668 /* single buffered */
2672 /* multi buffered */
2673 trb_limit = (XHCI_MAX_TRANSFERS - 2);
2677 if (pepext->trb_used >= trb_limit) {
2678 DPRINTFN(8, "Too many TDs queued.\n");
2679 return (USB_ERR_NOMEM);
2682 /* check for stopped condition, after putting transfer on interrupt queue */
2683 if (pepext->trb_running == 0) {
2684 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
2686 DPRINTFN(8, "Not running\n");
2688 /* start configuration */
2689 (void)usb_proc_msignal(&sc->sc_config_proc,
2690 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
2696 /* get current TRB index */
2697 i = pepext->trb_index;
2699 /* get next TRB index */
2702 /* the last entry of the ring is a hardcoded link TRB */
2703 if (inext >= (XHCI_MAX_TRANSFERS - 1))
2706 /* compute terminating return address */
2707 addr += inext * sizeof(struct xhci_trb);
2709 /* compute link TRB pointer */
2710 trb_link = td_last->td_trb + td_last->ntrb;
2712 /* update next pointer of last link TRB */
2713 trb_link->qwTrb0 = htole64(addr);
2714 trb_link->dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2715 trb_link->dwTrb3 = htole32(XHCI_TRB_3_IOC_BIT |
2716 XHCI_TRB_3_CYCLE_BIT |
2717 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2720 xhci_dump_trb(&td_last->td_trb[td_last->ntrb]);
2722 usb_pc_cpu_flush(td_last->page_cache);
2724 /* write ahead chain end marker */
2726 pepext->trb[inext].qwTrb0 = 0;
2727 pepext->trb[inext].dwTrb2 = 0;
2728 pepext->trb[inext].dwTrb3 = 0;
2730 /* update next pointer of link TRB */
2732 pepext->trb[i].qwTrb0 = htole64((uint64_t)td_first->td_self);
2733 pepext->trb[i].dwTrb2 = htole32(XHCI_TRB_2_IRQ_SET(0));
2736 xhci_dump_trb(&pepext->trb[i]);
2738 usb_pc_cpu_flush(pepext->page_cache);
2740 /* toggle cycle bit which activates the transfer chain */
2742 pepext->trb[i].dwTrb3 = htole32(XHCI_TRB_3_CYCLE_BIT |
2743 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK));
2745 usb_pc_cpu_flush(pepext->page_cache);
2747 DPRINTF("qh_pos = %u\n", i);
2749 pepext->xfer[i] = xfer;
2753 xfer->flags_int.bandwidth_reclaimed = 1;
2755 pepext->trb_index = inext;
2757 xhci_endpoint_doorbell(xfer);
2763 xhci_root_intr(struct xhci_softc *sc)
2767 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2769 /* clear any old interrupt data */
2770 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata));
2772 for (i = 1; i <= sc->sc_noport; i++) {
2773 /* pick out CHANGE bits from the status register */
2774 if (XREAD4(sc, oper, XHCI_PORTSC(i)) & (
2775 XHCI_PS_CSC | XHCI_PS_PEC |
2776 XHCI_PS_OCC | XHCI_PS_WRC |
2777 XHCI_PS_PRC | XHCI_PS_PLC |
2779 sc->sc_hub_idata[i / 8] |= 1 << (i % 8);
2780 DPRINTF("port %d changed\n", i);
2783 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2784 sizeof(sc->sc_hub_idata));
2787 /*------------------------------------------------------------------------*
2788 * xhci_device_done - XHCI done handler
2790 * NOTE: This function can be called two times in a row on
2791 * the same USB transfer. From close and from interrupt.
2792 *------------------------------------------------------------------------*/
2794 xhci_device_done(struct usb_xfer *xfer, usb_error_t error)
2796 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n",
2797 xfer, xfer->endpoint, error);
2799 /* remove transfer from HW queue */
2800 xhci_transfer_remove(xfer, error);
2802 /* dequeue transfer and start next transfer */
2803 usbd_transfer_done(xfer, error);
2806 /*------------------------------------------------------------------------*
2807 * XHCI data transfer support (generic type)
2808 *------------------------------------------------------------------------*/
2810 xhci_device_generic_open(struct usb_xfer *xfer)
2812 if (xfer->flags_int.isochronous_xfr) {
2813 switch (xfer->xroot->udev->speed) {
2814 case USB_SPEED_FULL:
2817 usb_hs_bandwidth_alloc(xfer);
2824 xhci_device_generic_close(struct usb_xfer *xfer)
2828 xhci_device_done(xfer, USB_ERR_CANCELLED);
2830 if (xfer->flags_int.isochronous_xfr) {
2831 switch (xfer->xroot->udev->speed) {
2832 case USB_SPEED_FULL:
2835 usb_hs_bandwidth_free(xfer);
2842 xhci_device_generic_multi_enter(struct usb_endpoint *ep,
2843 struct usb_xfer *enter_xfer)
2845 struct usb_xfer *xfer;
2847 /* check if there is a current transfer */
2848 xfer = ep->endpoint_q.curr;
2853 * Check if the current transfer is started and then pickup
2854 * the next one, if any. Else wait for next start event due to
2855 * block on failure feature.
2857 if (!xfer->flags_int.bandwidth_reclaimed)
2860 xfer = TAILQ_FIRST(&ep->endpoint_q.head);
2863 * In case of enter we have to consider that the
2864 * transfer is queued by the USB core after the enter
2873 /* try to multi buffer */
2874 xhci_transfer_insert(xfer);
2878 xhci_device_generic_enter(struct usb_xfer *xfer)
2882 /* setup TD's and QH */
2883 xhci_setup_generic_chain(xfer);
2885 xhci_device_generic_multi_enter(xfer->endpoint, xfer);
2889 xhci_device_generic_start(struct usb_xfer *xfer)
2893 /* try to insert xfer on HW queue */
2894 xhci_transfer_insert(xfer);
2896 /* try to multi buffer */
2897 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
2899 /* add transfer last on interrupt queue */
2900 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2902 /* start timeout, if any */
2903 if (xfer->timeout != 0)
2904 usbd_transfer_timeout_ms(xfer, &xhci_timeout, xfer->timeout);
2907 struct usb_pipe_methods xhci_device_generic_methods =
2909 .open = xhci_device_generic_open,
2910 .close = xhci_device_generic_close,
2911 .enter = xhci_device_generic_enter,
2912 .start = xhci_device_generic_start,
2915 /*------------------------------------------------------------------------*
2916 * xhci root HUB support
2917 *------------------------------------------------------------------------*
2918 * Simulate a hardware HUB by handling all the necessary requests.
2919 *------------------------------------------------------------------------*/
2921 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
2924 struct usb_device_descriptor xhci_devd =
2926 .bLength = sizeof(xhci_devd),
2927 .bDescriptorType = UDESC_DEVICE, /* type */
2928 HSETW(.bcdUSB, 0x0300), /* USB version */
2929 .bDeviceClass = UDCLASS_HUB, /* class */
2930 .bDeviceSubClass = UDSUBCLASS_HUB, /* subclass */
2931 .bDeviceProtocol = UDPROTO_SSHUB, /* protocol */
2932 .bMaxPacketSize = 9, /* max packet size */
2933 HSETW(.idVendor, 0x0000), /* vendor */
2934 HSETW(.idProduct, 0x0000), /* product */
2935 HSETW(.bcdDevice, 0x0100), /* device version */
2939 .bNumConfigurations = 1, /* # of configurations */
2943 struct xhci_bos_desc xhci_bosd = {
2945 .bLength = sizeof(xhci_bosd.bosd),
2946 .bDescriptorType = UDESC_BOS,
2947 HSETW(.wTotalLength, sizeof(xhci_bosd)),
2948 .bNumDeviceCaps = 3,
2951 .bLength = sizeof(xhci_bosd.usb2extd),
2952 .bDescriptorType = 1,
2953 .bDevCapabilityType = 2,
2954 .bmAttributes[0] = 2,
2957 .bLength = sizeof(xhci_bosd.usbdcd),
2958 .bDescriptorType = UDESC_DEVICE_CAPABILITY,
2959 .bDevCapabilityType = 3,
2960 .bmAttributes = 0, /* XXX */
2961 HSETW(.wSpeedsSupported, 0x000C),
2962 .bFunctionalitySupport = 8,
2963 .bU1DevExitLat = 255, /* dummy - not used */
2964 .wU2DevExitLat = { 0x00, 0x08 },
2967 .bLength = sizeof(xhci_bosd.cidd),
2968 .bDescriptorType = 1,
2969 .bDevCapabilityType = 4,
2971 .bContainerID = 0, /* XXX */
2976 struct xhci_config_desc xhci_confd = {
2978 .bLength = sizeof(xhci_confd.confd),
2979 .bDescriptorType = UDESC_CONFIG,
2980 .wTotalLength[0] = sizeof(xhci_confd),
2982 .bConfigurationValue = 1,
2983 .iConfiguration = 0,
2984 .bmAttributes = UC_SELF_POWERED,
2985 .bMaxPower = 0 /* max power */
2988 .bLength = sizeof(xhci_confd.ifcd),
2989 .bDescriptorType = UDESC_INTERFACE,
2991 .bInterfaceClass = UICLASS_HUB,
2992 .bInterfaceSubClass = UISUBCLASS_HUB,
2993 .bInterfaceProtocol = 0,
2996 .bLength = sizeof(xhci_confd.endpd),
2997 .bDescriptorType = UDESC_ENDPOINT,
2998 .bEndpointAddress = UE_DIR_IN | XHCI_INTR_ENDPT,
2999 .bmAttributes = UE_INTERRUPT,
3000 .wMaxPacketSize[0] = 2, /* max 15 ports */
3004 .bLength = sizeof(xhci_confd.endpcd),
3005 .bDescriptorType = UDESC_ENDPOINT_SS_COMP,
3012 struct usb_hub_ss_descriptor xhci_hubd = {
3013 .bLength = sizeof(xhci_hubd),
3014 .bDescriptorType = UDESC_SS_HUB,
3018 xhci_roothub_exec(struct usb_device *udev,
3019 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3021 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3022 const char *str_ptr;
3033 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3036 ptr = (const void *)&sc->sc_hub_desc;
3040 value = UGETW(req->wValue);
3041 index = UGETW(req->wIndex);
3043 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x "
3044 "wValue=0x%04x wIndex=0x%04x\n",
3045 req->bmRequestType, req->bRequest,
3046 UGETW(req->wLength), value, index);
3048 #define C(x,y) ((x) | ((y) << 8))
3049 switch (C(req->bRequest, req->bmRequestType)) {
3050 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE):
3051 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE):
3052 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT):
3054 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops
3055 * for the integrated root hub.
3058 case C(UR_GET_CONFIG, UT_READ_DEVICE):
3060 sc->sc_hub_desc.temp[0] = sc->sc_conf;
3062 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE):
3063 switch (value >> 8) {
3065 if ((value & 0xff) != 0) {
3066 err = USB_ERR_IOERROR;
3069 len = sizeof(xhci_devd);
3070 ptr = (const void *)&xhci_devd;
3074 if ((value & 0xff) != 0) {
3075 err = USB_ERR_IOERROR;
3078 len = sizeof(xhci_bosd);
3079 ptr = (const void *)&xhci_bosd;
3083 if ((value & 0xff) != 0) {
3084 err = USB_ERR_IOERROR;
3087 len = sizeof(xhci_confd);
3088 ptr = (const void *)&xhci_confd;
3092 switch (value & 0xff) {
3093 case 0: /* Language table */
3097 case 1: /* Vendor */
3098 str_ptr = sc->sc_vendor;
3101 case 2: /* Product */
3102 str_ptr = "XHCI root HUB";
3110 len = usb_make_str_desc(
3111 sc->sc_hub_desc.temp,
3112 sizeof(sc->sc_hub_desc.temp),
3117 err = USB_ERR_IOERROR;
3121 case C(UR_GET_INTERFACE, UT_READ_INTERFACE):
3123 sc->sc_hub_desc.temp[0] = 0;
3125 case C(UR_GET_STATUS, UT_READ_DEVICE):
3127 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED);
3129 case C(UR_GET_STATUS, UT_READ_INTERFACE):
3130 case C(UR_GET_STATUS, UT_READ_ENDPOINT):
3132 USETW(sc->sc_hub_desc.stat.wStatus, 0);
3134 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE):
3135 if (value >= XHCI_MAX_DEVICES) {
3136 err = USB_ERR_IOERROR;
3140 case C(UR_SET_CONFIG, UT_WRITE_DEVICE):
3141 if (value != 0 && value != 1) {
3142 err = USB_ERR_IOERROR;
3145 sc->sc_conf = value;
3147 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE):
3149 case C(UR_SET_FEATURE, UT_WRITE_DEVICE):
3150 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE):
3151 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT):
3152 err = USB_ERR_IOERROR;
3154 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE):
3156 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT):
3159 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE):
3161 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER):
3162 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n");
3165 (index > sc->sc_noport)) {
3166 err = USB_ERR_IOERROR;
3169 port = XHCI_PORTSC(index);
3171 v = XREAD4(sc, oper, port);
3172 i = XHCI_PS_PLS_GET(v);
3173 v &= ~XHCI_PS_CLEAR;
3176 case UHF_C_BH_PORT_RESET:
3177 XWRITE4(sc, oper, port, v | XHCI_PS_WRC);
3179 case UHF_C_PORT_CONFIG_ERROR:
3180 XWRITE4(sc, oper, port, v | XHCI_PS_CEC);
3182 case UHF_C_PORT_SUSPEND:
3183 case UHF_C_PORT_LINK_STATE:
3184 XWRITE4(sc, oper, port, v | XHCI_PS_PLC);
3186 case UHF_C_PORT_CONNECTION:
3187 XWRITE4(sc, oper, port, v | XHCI_PS_CSC);
3189 case UHF_C_PORT_ENABLE:
3190 XWRITE4(sc, oper, port, v | XHCI_PS_PEC);
3192 case UHF_C_PORT_OVER_CURRENT:
3193 XWRITE4(sc, oper, port, v | XHCI_PS_OCC);
3195 case UHF_C_PORT_RESET:
3196 XWRITE4(sc, oper, port, v | XHCI_PS_PRC);
3198 case UHF_PORT_ENABLE:
3199 XWRITE4(sc, oper, port, v | XHCI_PS_PED);
3201 case UHF_PORT_POWER:
3202 XWRITE4(sc, oper, port, v & ~XHCI_PS_PP);
3204 case UHF_PORT_INDICATOR:
3205 XWRITE4(sc, oper, port, v & ~XHCI_PS_PIC_SET(3));
3207 case UHF_PORT_SUSPEND:
3211 XWRITE4(sc, oper, port, v |
3212 XHCI_PS_PLS_SET(0xF) | XHCI_PS_LWS);
3215 /* wait 20ms for resume sequence to complete */
3216 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
3219 XWRITE4(sc, oper, port, v |
3220 XHCI_PS_PLS_SET(0) | XHCI_PS_LWS);
3223 err = USB_ERR_IOERROR;
3228 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE):
3229 if ((value & 0xff) != 0) {
3230 err = USB_ERR_IOERROR;
3234 v = XREAD4(sc, capa, XHCI_HCSPARAMS0);
3236 sc->sc_hub_desc.hubd = xhci_hubd;
3238 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport;
3240 if (XHCI_HCS0_PPC(v))
3241 i = UHD_PWR_INDIVIDUAL;
3245 if (XHCI_HCS0_PIND(v))
3248 i |= UHD_OC_INDIVIDUAL;
3250 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i);
3252 /* see XHCI section 5.4.9: */
3253 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 10;
3255 for (j = 1; j <= sc->sc_noport; j++) {
3257 v = XREAD4(sc, oper, XHCI_PORTSC(j));
3258 if (v & XHCI_PS_DR) {
3259 sc->sc_hub_desc.hubd.
3260 DeviceRemovable[j / 8] |= 1U << (j % 8);
3263 len = sc->sc_hub_desc.hubd.bLength;
3266 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE):
3268 memset(sc->sc_hub_desc.temp, 0, 16);
3271 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER):
3272 DPRINTFN(9, "UR_GET_STATUS i=%d\n", index);
3275 (index > sc->sc_noport)) {
3276 err = USB_ERR_IOERROR;
3280 v = XREAD4(sc, oper, XHCI_PORTSC(index));
3282 DPRINTFN(9, "port status=0x%08x\n", v);
3284 i = UPS_PORT_LINK_STATE_SET(XHCI_PS_PLS_GET(v));
3286 switch (XHCI_PS_SPEED_GET(v)) {
3288 i |= UPS_HIGH_SPEED;
3297 i |= UPS_OTHER_SPEED;
3301 if (v & XHCI_PS_CCS)
3302 i |= UPS_CURRENT_CONNECT_STATUS;
3303 if (v & XHCI_PS_PED)
3304 i |= UPS_PORT_ENABLED;
3305 if (v & XHCI_PS_OCA)
3306 i |= UPS_OVERCURRENT_INDICATOR;
3309 if (v & XHCI_PS_PP) {
3311 * The USB 3.0 RH is using the
3312 * USB 2.0's power bit
3314 i |= UPS_PORT_POWER;
3316 USETW(sc->sc_hub_desc.ps.wPortStatus, i);
3319 if (v & XHCI_PS_CSC)
3320 i |= UPS_C_CONNECT_STATUS;
3321 if (v & XHCI_PS_PEC)
3322 i |= UPS_C_PORT_ENABLED;
3323 if (v & XHCI_PS_OCC)
3324 i |= UPS_C_OVERCURRENT_INDICATOR;
3325 if (v & XHCI_PS_WRC)
3326 i |= UPS_C_BH_PORT_RESET;
3327 if (v & XHCI_PS_PRC)
3328 i |= UPS_C_PORT_RESET;
3329 if (v & XHCI_PS_PLC)
3330 i |= UPS_C_PORT_LINK_STATE;
3331 if (v & XHCI_PS_CEC)
3332 i |= UPS_C_PORT_CONFIG_ERROR;
3334 USETW(sc->sc_hub_desc.ps.wPortChange, i);
3335 len = sizeof(sc->sc_hub_desc.ps);
3338 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE):
3339 err = USB_ERR_IOERROR;
3342 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE):
3345 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER):
3351 (index > sc->sc_noport)) {
3352 err = USB_ERR_IOERROR;
3356 port = XHCI_PORTSC(index);
3357 v = XREAD4(sc, oper, port) & ~XHCI_PS_CLEAR;
3360 case UHF_PORT_U1_TIMEOUT:
3361 if (XHCI_PS_SPEED_GET(v) != 4) {
3362 err = USB_ERR_IOERROR;
3365 port = XHCI_PORTPMSC(index);
3366 v = XREAD4(sc, oper, port);
3367 v &= ~XHCI_PM3_U1TO_SET(0xFF);
3368 v |= XHCI_PM3_U1TO_SET(i);
3369 XWRITE4(sc, oper, port, v);
3371 case UHF_PORT_U2_TIMEOUT:
3372 if (XHCI_PS_SPEED_GET(v) != 4) {
3373 err = USB_ERR_IOERROR;
3376 port = XHCI_PORTPMSC(index);
3377 v = XREAD4(sc, oper, port);
3378 v &= ~XHCI_PM3_U2TO_SET(0xFF);
3379 v |= XHCI_PM3_U2TO_SET(i);
3380 XWRITE4(sc, oper, port, v);
3382 case UHF_BH_PORT_RESET:
3383 XWRITE4(sc, oper, port, v | XHCI_PS_WPR);
3385 case UHF_PORT_LINK_STATE:
3386 XWRITE4(sc, oper, port, v |
3387 XHCI_PS_PLS_SET(i) | XHCI_PS_LWS);
3388 /* 4ms settle time */
3389 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 250);
3391 case UHF_PORT_ENABLE:
3392 DPRINTFN(3, "set port enable %d\n", index);
3394 case UHF_PORT_SUSPEND:
3395 DPRINTFN(6, "suspend port %u (LPM=%u)\n", index, i);
3396 j = XHCI_PS_SPEED_GET(v);
3397 if ((j < 1) || (j > 3)) {
3398 /* non-supported speed */
3399 err = USB_ERR_IOERROR;
3402 XWRITE4(sc, oper, port, v |
3403 XHCI_PS_PLS_SET(i ? 2 /* LPM */ : 3) | XHCI_PS_LWS);
3405 case UHF_PORT_RESET:
3406 DPRINTFN(6, "reset port %d\n", index);
3407 XWRITE4(sc, oper, port, v | XHCI_PS_PR);
3409 case UHF_PORT_POWER:
3410 DPRINTFN(3, "set port power %d\n", index);
3411 XWRITE4(sc, oper, port, v | XHCI_PS_PP);
3414 DPRINTFN(3, "set port test %d\n", index);
3416 case UHF_PORT_INDICATOR:
3417 DPRINTFN(3, "set port indicator %d\n", index);
3419 v &= ~XHCI_PS_PIC_SET(3);
3420 v |= XHCI_PS_PIC_SET(1);
3422 XWRITE4(sc, oper, port, v);
3425 err = USB_ERR_IOERROR;
3430 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER):
3431 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER):
3432 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER):
3433 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER):
3436 err = USB_ERR_IOERROR;
3446 xhci_xfer_setup(struct usb_setup_params *parm)
3448 struct usb_page_search page_info;
3449 struct usb_page_cache *pc;
3450 struct xhci_softc *sc;
3451 struct usb_xfer *xfer;
3456 sc = XHCI_BUS2SC(parm->udev->bus);
3457 xfer = parm->curr_xfer;
3460 * The proof for the "ntd" formula is illustrated like this:
3462 * +------------------------------------+
3466 * | | xxx | x | frm 0 |
3468 * | | xxx | xx | frm 1 |
3471 * +------------------------------------+
3473 * "xxx" means a completely full USB transfer descriptor
3475 * "x" and "xx" means a short USB packet
3477 * For the remainder of an USB transfer modulo
3478 * "max_data_length" we need two USB transfer descriptors.
3479 * One to transfer the remaining data and one to finalise with
3480 * a zero length packet in case the "force_short_xfer" flag is
3481 * set. We only need two USB transfer descriptors in the case
3482 * where the transfer length of the first one is a factor of
3483 * "max_frame_size". The rest of the needed USB transfer
3484 * descriptors is given by the buffer size divided by the
3485 * maximum data payload.
3487 parm->hc_max_packet_size = 0x400;
3488 parm->hc_max_packet_count = 16 * 3;
3489 parm->hc_max_frame_size = XHCI_TD_PAYLOAD_MAX;
3491 xfer->flags_int.bdma_enable = 1;
3493 usbd_transfer_setup_sub(parm);
3495 if (xfer->flags_int.isochronous_xfr) {
3496 ntd = ((1 * xfer->nframes)
3497 + (xfer->max_data_length / xfer->max_hc_frame_size));
3498 } else if (xfer->flags_int.control_xfr) {
3499 ntd = ((2 * xfer->nframes) + 1 /* STATUS */
3500 + (xfer->max_data_length / xfer->max_hc_frame_size));
3502 ntd = ((2 * xfer->nframes)
3503 + (xfer->max_data_length / xfer->max_hc_frame_size));
3512 * Allocate queue heads and transfer descriptors
3516 if (usbd_transfer_setup_sub_malloc(
3517 parm, &pc, sizeof(struct xhci_td),
3518 XHCI_TD_ALIGN, ntd)) {
3519 parm->err = USB_ERR_NOMEM;
3523 for (n = 0; n != ntd; n++) {
3526 usbd_get_page(pc + n, 0, &page_info);
3528 td = page_info.buffer;
3531 td->td_self = page_info.physaddr;
3532 td->obj_next = last_obj;
3533 td->page_cache = pc + n;
3537 usb_pc_cpu_flush(pc + n);
3540 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj;
3542 if (!xfer->flags_int.curr_dma_set) {
3543 xfer->flags_int.curr_dma_set = 1;
3549 xhci_configure_reset_endpoint(struct usb_xfer *xfer)
3551 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3552 struct usb_page_search buf_inp;
3553 struct usb_device *udev;
3554 struct xhci_endpoint_ext *pepext;
3555 struct usb_endpoint_descriptor *edesc;
3556 struct usb_page_cache *pcinp;
3561 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3562 xfer->endpoint->edesc);
3564 udev = xfer->xroot->udev;
3565 index = udev->controller_slot_id;
3567 pcinp = &sc->sc_hw.devs[index].input_pc;
3569 usbd_get_page(pcinp, 0, &buf_inp);
3571 edesc = xfer->endpoint->edesc;
3573 epno = edesc->bEndpointAddress;
3575 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL)
3578 epno = XHCI_EPNO2EPID(epno);
3581 return (USB_ERR_NO_PIPE); /* invalid */
3585 /* configure endpoint */
3587 err = xhci_configure_endpoint_by_xfer(xfer);
3590 XHCI_CMD_UNLOCK(sc);
3595 * Get the endpoint into the stopped state according to the
3596 * endpoint context state diagram in the XHCI specification:
3599 err = xhci_cmd_stop_ep(sc, 0, epno, index);
3602 DPRINTF("Could not stop endpoint %u\n", epno);
3604 err = xhci_cmd_reset_ep(sc, 0, epno, index);
3607 DPRINTF("Could not reset endpoint %u\n", epno);
3609 err = xhci_cmd_set_tr_dequeue_ptr(sc, pepext->physaddr |
3610 XHCI_EPCTX_2_DCS_SET(1), 0, epno, index);
3613 DPRINTF("Could not set dequeue ptr for endpoint %u\n", epno);
3616 * Get the endpoint into the running state according to the
3617 * endpoint context state diagram in the XHCI specification:
3620 xhci_configure_mask(udev, (1U << epno) | 1U, 0);
3622 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
3625 DPRINTF("Could not configure endpoint %u\n", epno);
3627 err = xhci_cmd_configure_ep(sc, buf_inp.physaddr, 0, index);
3630 DPRINTF("Could not configure endpoint %u\n", epno);
3632 XHCI_CMD_UNLOCK(sc);
3638 xhci_xfer_unsetup(struct usb_xfer *xfer)
3644 xhci_start_dma_delay(struct usb_xfer *xfer)
3646 struct xhci_softc *sc = XHCI_BUS2SC(xfer->xroot->bus);
3648 /* put transfer on interrupt queue (again) */
3649 usbd_transfer_enqueue(&sc->sc_bus.intr_q, xfer);
3651 (void)usb_proc_msignal(&sc->sc_config_proc,
3652 &sc->sc_config_msg[0], &sc->sc_config_msg[1]);
3656 xhci_configure_msg(struct usb_proc_msg *pm)
3658 struct xhci_softc *sc;
3659 struct xhci_endpoint_ext *pepext;
3660 struct usb_xfer *xfer;
3662 sc = XHCI_BUS2SC(((struct usb_bus_msg *)pm)->bus);
3665 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3667 pepext = xhci_get_endpoint_ext(xfer->xroot->udev,
3668 xfer->endpoint->edesc);
3670 if ((pepext->trb_halted != 0) ||
3671 (pepext->trb_running == 0)) {
3675 /* clear halted and running */
3676 pepext->trb_halted = 0;
3677 pepext->trb_running = 0;
3679 /* nuke remaining buffered transfers */
3681 for (i = 0; i != (XHCI_MAX_TRANSFERS - 1); i++) {
3683 * NOTE: We need to use the timeout
3684 * error code here else existing
3685 * isochronous clients can get
3688 if (pepext->xfer[i] != NULL) {
3689 xhci_device_done(pepext->xfer[i],
3695 * NOTE: The USB transfer cannot vanish in
3699 USB_BUS_UNLOCK(&sc->sc_bus);
3701 xhci_configure_reset_endpoint(xfer);
3703 USB_BUS_LOCK(&sc->sc_bus);
3705 /* check if halted is still cleared */
3706 if (pepext->trb_halted == 0) {
3707 pepext->trb_running = 1;
3708 pepext->trb_index = 0;
3713 if (xfer->flags_int.did_dma_delay) {
3715 /* remove transfer from interrupt queue (again) */
3716 usbd_transfer_dequeue(xfer);
3718 /* we are finally done */
3719 usb_dma_delay_done_cb(xfer);
3721 /* queue changed - restart */
3726 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
3728 /* try to insert xfer on HW queue */
3729 xhci_transfer_insert(xfer);
3731 /* try to multi buffer */
3732 xhci_device_generic_multi_enter(xfer->endpoint, NULL);
3737 xhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
3738 struct usb_endpoint *ep)
3740 struct xhci_endpoint_ext *pepext;
3742 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d\n",
3743 ep, udev->address, edesc->bEndpointAddress, udev->flags.usb_mode);
3745 if (udev->flags.usb_mode != USB_MODE_HOST) {
3749 if (udev->parent_hub == NULL) {
3750 /* root HUB has special endpoint handling */
3754 ep->methods = &xhci_device_generic_methods;
3756 pepext = xhci_get_endpoint_ext(udev, edesc);
3758 USB_BUS_LOCK(udev->bus);
3759 pepext->trb_halted = 1;
3760 pepext->trb_running = 0;
3761 USB_BUS_UNLOCK(udev->bus);
3765 xhci_ep_uninit(struct usb_device *udev, struct usb_endpoint *ep)
3771 xhci_ep_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3773 struct xhci_endpoint_ext *pepext;
3777 if (udev->flags.usb_mode != USB_MODE_HOST) {
3781 if (udev->parent_hub == NULL) {
3782 /* root HUB has special endpoint handling */
3786 pepext = xhci_get_endpoint_ext(udev, ep->edesc);
3788 USB_BUS_LOCK(udev->bus);
3789 pepext->trb_halted = 1;
3790 pepext->trb_running = 0;
3791 USB_BUS_UNLOCK(udev->bus);
3795 xhci_device_init(struct usb_device *udev)
3797 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3801 /* no init for root HUB */
3802 if (udev->parent_hub == NULL)
3807 /* set invalid default */
3809 udev->controller_slot_id = sc->sc_noslot + 1;
3811 /* try to get a new slot ID from the XHCI */
3813 err = xhci_cmd_enable_slot(sc, &temp);
3816 XHCI_CMD_UNLOCK(sc);
3820 if (temp > sc->sc_noslot) {
3821 XHCI_CMD_UNLOCK(sc);
3822 return (USB_ERR_BAD_ADDRESS);
3825 if (sc->sc_hw.devs[temp].state != XHCI_ST_DISABLED) {
3826 DPRINTF("slot %u already allocated.\n", temp);
3827 XHCI_CMD_UNLOCK(sc);
3828 return (USB_ERR_BAD_ADDRESS);
3831 /* store slot ID for later reference */
3833 udev->controller_slot_id = temp;
3835 /* reset data structure */
3837 memset(&sc->sc_hw.devs[temp], 0, sizeof(sc->sc_hw.devs[0]));
3839 /* set mark slot allocated */
3841 sc->sc_hw.devs[temp].state = XHCI_ST_ENABLED;
3843 err = xhci_alloc_device_ext(udev);
3845 XHCI_CMD_UNLOCK(sc);
3847 /* get device into default state */
3850 err = xhci_set_address(udev, NULL, 0);
3856 xhci_device_uninit(struct usb_device *udev)
3858 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3861 /* no init for root HUB */
3862 if (udev->parent_hub == NULL)
3867 index = udev->controller_slot_id;
3869 if (index <= sc->sc_noslot) {
3870 xhci_cmd_disable_slot(sc, index);
3871 sc->sc_hw.devs[index].state = XHCI_ST_DISABLED;
3873 /* free device extension */
3874 xhci_free_device_ext(udev);
3877 XHCI_CMD_UNLOCK(sc);
3881 xhci_get_dma_delay(struct usb_device *udev, uint32_t *pus)
3884 * Wait until the hardware has finished any possible use of
3885 * the transfer descriptor(s)
3887 *pus = 2048; /* microseconds */
3891 xhci_device_resume(struct usb_device *udev)
3893 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3900 /* check for root HUB */
3901 if (udev->parent_hub == NULL)
3904 index = udev->controller_slot_id;
3908 /* blindly resume all endpoints */
3910 USB_BUS_LOCK(udev->bus);
3912 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3913 for (p = 0; p != 1 /*XHCI_MAX_STREAMS*/; p++) {
3914 XWRITE4(sc, door, XHCI_DOORBELL(index),
3915 n | XHCI_DB_SID_SET(p));
3919 USB_BUS_UNLOCK(udev->bus);
3921 XHCI_CMD_UNLOCK(sc);
3925 xhci_device_suspend(struct usb_device *udev)
3927 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3934 /* check for root HUB */
3935 if (udev->parent_hub == NULL)
3938 index = udev->controller_slot_id;
3942 /* blindly suspend all endpoints */
3944 for (n = 1; n != XHCI_MAX_ENDPOINTS; n++) {
3945 err = xhci_cmd_stop_ep(sc, 1, n, index);
3947 DPRINTF("Failed to suspend endpoint "
3948 "%u on slot %u (ignored).\n", n, index);
3952 XHCI_CMD_UNLOCK(sc);
3956 xhci_set_hw_power(struct usb_bus *bus)
3962 xhci_device_state_change(struct usb_device *udev)
3964 struct xhci_softc *sc = XHCI_BUS2SC(udev->bus);
3965 struct usb_page_search buf_inp;
3969 /* check for root HUB */
3970 if (udev->parent_hub == NULL)
3973 index = udev->controller_slot_id;
3977 if (usb_get_device_state(udev) == USB_STATE_CONFIGURED) {
3978 err = uhub_query_info(udev, &sc->sc_hw.devs[index].nports,
3979 &sc->sc_hw.devs[index].tt);
3981 sc->sc_hw.devs[index].nports = 0;
3986 switch (usb_get_device_state(udev)) {
3987 case USB_STATE_POWERED:
3988 if (sc->sc_hw.devs[index].state == XHCI_ST_DEFAULT)
3991 /* set default state */
3992 sc->sc_hw.devs[index].state = XHCI_ST_DEFAULT;
3994 /* reset number of contexts */
3995 sc->sc_hw.devs[index].context_num = 0;
3997 err = xhci_cmd_reset_dev(sc, index);
4000 DPRINTF("Device reset failed "
4001 "for slot %u.\n", index);
4005 case USB_STATE_ADDRESSED:
4006 if (sc->sc_hw.devs[index].state == XHCI_ST_ADDRESSED)
4009 sc->sc_hw.devs[index].state = XHCI_ST_ADDRESSED;
4011 err = xhci_cmd_configure_ep(sc, 0, 1, index);
4014 DPRINTF("Failed to deconfigure "
4015 "slot %u.\n", index);
4019 case USB_STATE_CONFIGURED:
4020 if (sc->sc_hw.devs[index].state == XHCI_ST_CONFIGURED)
4023 /* set configured state */
4024 sc->sc_hw.devs[index].state = XHCI_ST_CONFIGURED;
4026 /* reset number of contexts */
4027 sc->sc_hw.devs[index].context_num = 0;
4029 usbd_get_page(&sc->sc_hw.devs[index].input_pc, 0, &buf_inp);
4031 xhci_configure_mask(udev, 3, 0);
4033 err = xhci_configure_device(udev);
4035 DPRINTF("Could not configure device "
4036 "at slot %u.\n", index);
4039 err = xhci_cmd_evaluate_ctx(sc, buf_inp.physaddr, index);
4041 DPRINTF("Could not evaluate device "
4042 "context at slot %u.\n", index);
4049 XHCI_CMD_UNLOCK(sc);
4052 struct usb_bus_methods xhci_bus_methods = {
4053 .endpoint_init = xhci_ep_init,
4054 .endpoint_uninit = xhci_ep_uninit,
4055 .xfer_setup = xhci_xfer_setup,
4056 .xfer_unsetup = xhci_xfer_unsetup,
4057 .get_dma_delay = xhci_get_dma_delay,
4058 .device_init = xhci_device_init,
4059 .device_uninit = xhci_device_uninit,
4060 .device_resume = xhci_device_resume,
4061 .device_suspend = xhci_device_suspend,
4062 .set_hw_power = xhci_set_hw_power,
4063 .roothub_exec = xhci_roothub_exec,
4064 .xfer_poll = xhci_do_poll,
4065 .start_dma_delay = xhci_start_dma_delay,
4066 .set_address = xhci_set_address,
4067 .clear_stall = xhci_ep_clear_stall,
4068 .device_state_change = xhci_device_state_change,
4069 .set_hw_power_sleep = xhci_set_hw_power_sleep,