1 /* $NetBSD: xhci.c,v 1.33 2016/01/06 22:12:49 skrll Exp $ */ 2 3 /* 4 * Copyright (c) 2013 Jonathan A. Kollasch 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.33 2016/01/06 22:12:49 skrll Exp $"); 31 32 #include "opt_usb.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/kmem.h> 38 #include <sys/malloc.h> 39 #include <sys/device.h> 40 #include <sys/select.h> 41 #include <sys/proc.h> 42 #include <sys/queue.h> 43 #include <sys/mutex.h> 44 #include <sys/condvar.h> 45 #include <sys/bus.h> 46 #include <sys/cpu.h> 47 #include <sys/sysctl.h> 48 49 #include <machine/endian.h> 50 51 #include <dev/usb/usb.h> 52 #include <dev/usb/usbdi.h> 53 #include <dev/usb/usbdivar.h> 54 #include <dev/usb/usbhist.h> 55 #include <dev/usb/usb_mem.h> 56 #include <dev/usb/usb_quirks.h> 57 58 #include <dev/usb/xhcireg.h> 59 #include <dev/usb/xhcivar.h> 60 #include <dev/usb/usbroothub_subr.h> 61 62 63 #ifdef USB_DEBUG 64 #ifndef XHCI_DEBUG 65 #define xhcidebug 0 66 #else 67 static int xhcidebug = 0; 68 69 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup") 70 { 71 int err; 72 const struct sysctlnode *rnode; 73 const struct sysctlnode *cnode; 74 75 err = sysctl_createv(clog, 0, NULL, &rnode, 76 CTLFLAG_PERMANENT, CTLTYPE_NODE, "xhci", 77 SYSCTL_DESCR("xhci global controls"), 78 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL); 79 80 if (err) 81 goto fail; 82 83 /* control debugging printfs */ 84 err = sysctl_createv(clog, 0, &rnode, &cnode, 85 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 86 "debug", SYSCTL_DESCR("Enable debugging output"), 87 NULL, 0, &xhcidebug, sizeof(xhcidebug), CTL_CREATE, CTL_EOL); 88 if (err) 89 goto fail; 90 91 return; 92 fail: 93 aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err); 94 } 95 96 #endif /* XHCI_DEBUG */ 97 #endif /* USB_DEBUG */ 98 99 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D) 100 #define XHCIHIST_FUNC() USBHIST_FUNC() 101 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug) 102 103 #define XHCI_DCI_SLOT 0 104 #define XHCI_DCI_EP_CONTROL 1 105 106 #define XHCI_ICI_INPUT_CONTROL 0 107 108 struct xhci_pipe { 109 struct usbd_pipe xp_pipe; 110 }; 111 112 #define XHCI_INTR_ENDPT 1 113 #define XHCI_COMMAND_RING_TRBS 256 114 #define XHCI_EVENT_RING_TRBS 256 115 #define XHCI_EVENT_RING_SEGMENTS 1 116 #define XHCI_TRB_3_ED_BIT XHCI_TRB_3_ISP_BIT 117 118 static usbd_status xhci_open(usbd_pipe_handle); 119 static int xhci_intr1(struct xhci_softc * const); 120 static void xhci_softintr(void *); 121 static void xhci_poll(struct usbd_bus *); 122 static usbd_status xhci_allocm(struct usbd_bus *, usb_dma_t *, uint32_t); 123 static void xhci_freem(struct usbd_bus *, usb_dma_t *); 124 static usbd_xfer_handle xhci_allocx(struct usbd_bus *); 125 static void xhci_freex(struct usbd_bus *, usbd_xfer_handle); 126 static void xhci_get_lock(struct usbd_bus *, kmutex_t **); 127 static usbd_status xhci_new_device(device_t, usbd_bus_handle, int, int, int, 128 struct usbd_port *); 129 130 static usbd_status xhci_configure_endpoint(usbd_pipe_handle); 131 static usbd_status xhci_unconfigure_endpoint(usbd_pipe_handle); 132 static usbd_status xhci_reset_endpoint(usbd_pipe_handle); 133 //static usbd_status xhci_stop_endpoint(usbd_pipe_handle); 134 135 static usbd_status xhci_set_dequeue(usbd_pipe_handle); 136 137 static usbd_status xhci_do_command(struct xhci_softc * const, 138 struct xhci_trb * const, int); 139 static usbd_status xhci_init_slot(struct xhci_softc * const, uint32_t, 140 int, int, int, int); 141 static usbd_status xhci_enable_slot(struct xhci_softc * const, 142 uint8_t * const); 143 static usbd_status xhci_address_device(struct xhci_softc * const, 144 uint64_t, uint8_t, bool); 145 static usbd_status xhci_update_ep0_mps(struct xhci_softc * const, 146 struct xhci_slot * const, u_int); 147 static usbd_status xhci_ring_init(struct xhci_softc * const, 148 struct xhci_ring * const, size_t, size_t); 149 static void xhci_ring_free(struct xhci_softc * const, struct xhci_ring * const); 150 151 static void xhci_noop(usbd_pipe_handle); 152 153 static usbd_status xhci_root_ctrl_transfer(usbd_xfer_handle); 154 static usbd_status xhci_root_ctrl_start(usbd_xfer_handle); 155 static void xhci_root_ctrl_abort(usbd_xfer_handle); 156 static void xhci_root_ctrl_close(usbd_pipe_handle); 157 static void xhci_root_ctrl_done(usbd_xfer_handle); 158 159 static usbd_status xhci_root_intr_transfer(usbd_xfer_handle); 160 static usbd_status xhci_root_intr_start(usbd_xfer_handle); 161 static void xhci_root_intr_abort(usbd_xfer_handle); 162 static void xhci_root_intr_close(usbd_pipe_handle); 163 static void xhci_root_intr_done(usbd_xfer_handle); 164 165 static usbd_status xhci_device_ctrl_transfer(usbd_xfer_handle); 166 static usbd_status xhci_device_ctrl_start(usbd_xfer_handle); 167 static void xhci_device_ctrl_abort(usbd_xfer_handle); 168 static void xhci_device_ctrl_close(usbd_pipe_handle); 169 static void xhci_device_ctrl_done(usbd_xfer_handle); 170 171 static usbd_status xhci_device_intr_transfer(usbd_xfer_handle); 172 static usbd_status xhci_device_intr_start(usbd_xfer_handle); 173 static void xhci_device_intr_abort(usbd_xfer_handle); 174 static void xhci_device_intr_close(usbd_pipe_handle); 175 static void xhci_device_intr_done(usbd_xfer_handle); 176 177 static usbd_status xhci_device_bulk_transfer(usbd_xfer_handle); 178 static usbd_status xhci_device_bulk_start(usbd_xfer_handle); 179 static void xhci_device_bulk_abort(usbd_xfer_handle); 180 static void xhci_device_bulk_close(usbd_pipe_handle); 181 static void xhci_device_bulk_done(usbd_xfer_handle); 182 183 static void xhci_timeout(void *); 184 static void xhci_timeout_task(void *); 185 186 static const struct usbd_bus_methods xhci_bus_methods = { 187 .open_pipe = xhci_open, 188 .soft_intr = xhci_softintr, 189 .do_poll = xhci_poll, 190 .allocm = xhci_allocm, 191 .freem = xhci_freem, 192 .allocx = xhci_allocx, 193 .freex = xhci_freex, 194 .get_lock = xhci_get_lock, 195 .new_device = xhci_new_device, 196 }; 197 198 static const struct usbd_pipe_methods xhci_root_ctrl_methods = { 199 .transfer = xhci_root_ctrl_transfer, 200 .start = xhci_root_ctrl_start, 201 .abort = xhci_root_ctrl_abort, 202 .close = xhci_root_ctrl_close, 203 .cleartoggle = xhci_noop, 204 .done = xhci_root_ctrl_done, 205 }; 206 207 static const struct usbd_pipe_methods xhci_root_intr_methods = { 208 .transfer = xhci_root_intr_transfer, 209 .start = xhci_root_intr_start, 210 .abort = xhci_root_intr_abort, 211 .close = xhci_root_intr_close, 212 .cleartoggle = xhci_noop, 213 .done = xhci_root_intr_done, 214 }; 215 216 217 static const struct usbd_pipe_methods xhci_device_ctrl_methods = { 218 .transfer = xhci_device_ctrl_transfer, 219 .start = xhci_device_ctrl_start, 220 .abort = xhci_device_ctrl_abort, 221 .close = xhci_device_ctrl_close, 222 .cleartoggle = xhci_noop, 223 .done = xhci_device_ctrl_done, 224 }; 225 226 static const struct usbd_pipe_methods xhci_device_isoc_methods = { 227 .cleartoggle = xhci_noop, 228 }; 229 230 static const struct usbd_pipe_methods xhci_device_bulk_methods = { 231 .transfer = xhci_device_bulk_transfer, 232 .start = xhci_device_bulk_start, 233 .abort = xhci_device_bulk_abort, 234 .close = xhci_device_bulk_close, 235 .cleartoggle = xhci_noop, 236 .done = xhci_device_bulk_done, 237 }; 238 239 static const struct usbd_pipe_methods xhci_device_intr_methods = { 240 .transfer = xhci_device_intr_transfer, 241 .start = xhci_device_intr_start, 242 .abort = xhci_device_intr_abort, 243 .close = xhci_device_intr_close, 244 .cleartoggle = xhci_noop, 245 .done = xhci_device_intr_done, 246 }; 247 248 static inline uint32_t 249 xhci_read_4(const struct xhci_softc * const sc, bus_size_t offset) 250 { 251 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, offset); 252 } 253 254 #if 0 /* unused */ 255 static inline void 256 xhci_write_4(const struct xhci_softc * const sc, bus_size_t offset, 257 uint32_t value) 258 { 259 bus_space_write_4(sc->sc_iot, sc->sc_ioh, offset, value); 260 } 261 #endif /* unused */ 262 263 static inline uint32_t 264 xhci_cap_read_4(const struct xhci_softc * const sc, bus_size_t offset) 265 { 266 return bus_space_read_4(sc->sc_iot, sc->sc_cbh, offset); 267 } 268 269 static inline uint32_t 270 xhci_op_read_4(const struct xhci_softc * const sc, bus_size_t offset) 271 { 272 return bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 273 } 274 275 static inline void 276 xhci_op_write_4(const struct xhci_softc * const sc, bus_size_t offset, 277 uint32_t value) 278 { 279 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 280 } 281 282 #if 0 /* unused */ 283 static inline uint64_t 284 xhci_op_read_8(const struct xhci_softc * const sc, bus_size_t offset) 285 { 286 uint64_t value; 287 288 if (sc->sc_ac64) { 289 #ifdef XHCI_USE_BUS_SPACE_8 290 value = bus_space_read_8(sc->sc_iot, sc->sc_obh, offset); 291 #else 292 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 293 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_obh, 294 offset + 4) << 32; 295 #endif 296 } else { 297 value = bus_space_read_4(sc->sc_iot, sc->sc_obh, offset); 298 } 299 300 return value; 301 } 302 #endif /* unused */ 303 304 static inline void 305 xhci_op_write_8(const struct xhci_softc * const sc, bus_size_t offset, 306 uint64_t value) 307 { 308 if (sc->sc_ac64) { 309 #ifdef XHCI_USE_BUS_SPACE_8 310 bus_space_write_8(sc->sc_iot, sc->sc_obh, offset, value); 311 #else 312 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 0, 313 (value >> 0) & 0xffffffff); 314 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset + 4, 315 (value >> 32) & 0xffffffff); 316 #endif 317 } else { 318 bus_space_write_4(sc->sc_iot, sc->sc_obh, offset, value); 319 } 320 } 321 322 static inline uint32_t 323 xhci_rt_read_4(const struct xhci_softc * const sc, bus_size_t offset) 324 { 325 return bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 326 } 327 328 static inline void 329 xhci_rt_write_4(const struct xhci_softc * const sc, bus_size_t offset, 330 uint32_t value) 331 { 332 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 333 } 334 335 #if 0 /* unused */ 336 static inline uint64_t 337 xhci_rt_read_8(const struct xhci_softc * const sc, bus_size_t offset) 338 { 339 uint64_t value; 340 341 if (sc->sc_ac64) { 342 #ifdef XHCI_USE_BUS_SPACE_8 343 value = bus_space_read_8(sc->sc_iot, sc->sc_rbh, offset); 344 #else 345 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 346 value |= (uint64_t)bus_space_read_4(sc->sc_iot, sc->sc_rbh, 347 offset + 4) << 32; 348 #endif 349 } else { 350 value = bus_space_read_4(sc->sc_iot, sc->sc_rbh, offset); 351 } 352 353 return value; 354 } 355 #endif /* unused */ 356 357 static inline void 358 xhci_rt_write_8(const struct xhci_softc * const sc, bus_size_t offset, 359 uint64_t value) 360 { 361 if (sc->sc_ac64) { 362 #ifdef XHCI_USE_BUS_SPACE_8 363 bus_space_write_8(sc->sc_iot, sc->sc_rbh, offset, value); 364 #else 365 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 0, 366 (value >> 0) & 0xffffffff); 367 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset + 4, 368 (value >> 32) & 0xffffffff); 369 #endif 370 } else { 371 bus_space_write_4(sc->sc_iot, sc->sc_rbh, offset, value); 372 } 373 } 374 375 #if 0 /* unused */ 376 static inline uint32_t 377 xhci_db_read_4(const struct xhci_softc * const sc, bus_size_t offset) 378 { 379 return bus_space_read_4(sc->sc_iot, sc->sc_dbh, offset); 380 } 381 #endif /* unused */ 382 383 static inline void 384 xhci_db_write_4(const struct xhci_softc * const sc, bus_size_t offset, 385 uint32_t value) 386 { 387 bus_space_write_4(sc->sc_iot, sc->sc_dbh, offset, value); 388 } 389 390 /* --- */ 391 392 static inline uint8_t 393 xhci_ep_get_type(usb_endpoint_descriptor_t * const ed) 394 { 395 u_int eptype; 396 397 switch (UE_GET_XFERTYPE(ed->bmAttributes)) { 398 case UE_CONTROL: 399 eptype = 0x0; 400 break; 401 case UE_ISOCHRONOUS: 402 eptype = 0x1; 403 break; 404 case UE_BULK: 405 eptype = 0x2; 406 break; 407 case UE_INTERRUPT: 408 eptype = 0x3; 409 break; 410 } 411 412 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 413 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 414 return eptype | 0x4; 415 else 416 return eptype; 417 } 418 419 static u_int 420 xhci_ep_get_dci(usb_endpoint_descriptor_t * const ed) 421 { 422 /* xHCI 1.0 section 4.5.1 */ 423 u_int epaddr = UE_GET_ADDR(ed->bEndpointAddress); 424 u_int in = 0; 425 426 if ((UE_GET_XFERTYPE(ed->bmAttributes) == UE_CONTROL) || 427 (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN)) 428 in = 1; 429 430 return epaddr * 2 + in; 431 } 432 433 static inline u_int 434 xhci_dci_to_ici(const u_int i) 435 { 436 return i + 1; 437 } 438 439 static inline void * 440 xhci_slot_get_dcv(struct xhci_softc * const sc, struct xhci_slot * const xs, 441 const u_int dci) 442 { 443 return KERNADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 444 } 445 446 #if 0 /* unused */ 447 static inline bus_addr_t 448 xhci_slot_get_dcp(struct xhci_softc * const sc, struct xhci_slot * const xs, 449 const u_int dci) 450 { 451 return DMAADDR(&xs->xs_dc_dma, sc->sc_ctxsz * dci); 452 } 453 #endif /* unused */ 454 455 static inline void * 456 xhci_slot_get_icv(struct xhci_softc * const sc, struct xhci_slot * const xs, 457 const u_int ici) 458 { 459 return KERNADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 460 } 461 462 static inline bus_addr_t 463 xhci_slot_get_icp(struct xhci_softc * const sc, struct xhci_slot * const xs, 464 const u_int ici) 465 { 466 return DMAADDR(&xs->xs_ic_dma, sc->sc_ctxsz * ici); 467 } 468 469 static inline struct xhci_trb * 470 xhci_ring_trbv(struct xhci_ring * const xr, u_int idx) 471 { 472 return KERNADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 473 } 474 475 static inline bus_addr_t 476 xhci_ring_trbp(struct xhci_ring * const xr, u_int idx) 477 { 478 return DMAADDR(&xr->xr_dma, XHCI_TRB_SIZE * idx); 479 } 480 481 static inline void 482 xhci_trb_put(struct xhci_trb * const trb, uint64_t parameter, uint32_t status, 483 uint32_t control) 484 { 485 trb->trb_0 = parameter; 486 trb->trb_2 = status; 487 trb->trb_3 = control; 488 } 489 490 /* --- */ 491 492 void 493 xhci_childdet(device_t self, device_t child) 494 { 495 struct xhci_softc * const sc = device_private(self); 496 497 KASSERT(sc->sc_child == child); 498 if (child == sc->sc_child) 499 sc->sc_child = NULL; 500 } 501 502 int 503 xhci_detach(struct xhci_softc *sc, int flags) 504 { 505 int rv = 0; 506 507 if (sc->sc_child != NULL) 508 rv = config_detach(sc->sc_child, flags); 509 510 if (rv != 0) 511 return (rv); 512 513 /* XXX unconfigure/free slots */ 514 515 /* verify: */ 516 xhci_rt_write_4(sc, XHCI_IMAN(0), 0); 517 xhci_op_write_4(sc, XHCI_USBCMD, 0); 518 /* do we need to wait for stop? */ 519 520 xhci_op_write_8(sc, XHCI_CRCR, 0); 521 xhci_ring_free(sc, &sc->sc_cr); 522 cv_destroy(&sc->sc_command_cv); 523 524 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), 0); 525 xhci_rt_write_8(sc, XHCI_ERSTBA(0), 0); 526 xhci_rt_write_8(sc, XHCI_ERDP(0), 0|XHCI_ERDP_LO_BUSY); 527 xhci_ring_free(sc, &sc->sc_er); 528 529 usb_freemem(&sc->sc_bus, &sc->sc_eventst_dma); 530 531 xhci_op_write_8(sc, XHCI_DCBAAP, 0); 532 usb_freemem(&sc->sc_bus, &sc->sc_dcbaa_dma); 533 534 kmem_free(sc->sc_slots, sizeof(*sc->sc_slots) * sc->sc_maxslots); 535 536 mutex_destroy(&sc->sc_lock); 537 mutex_destroy(&sc->sc_intr_lock); 538 539 pool_cache_destroy(sc->sc_xferpool); 540 541 return rv; 542 } 543 544 int 545 xhci_activate(device_t self, enum devact act) 546 { 547 struct xhci_softc * const sc = device_private(self); 548 549 switch (act) { 550 case DVACT_DEACTIVATE: 551 sc->sc_dying = true; 552 return 0; 553 default: 554 return EOPNOTSUPP; 555 } 556 } 557 558 bool 559 xhci_suspend(device_t dv, const pmf_qual_t *qual) 560 { 561 return false; 562 } 563 564 bool 565 xhci_resume(device_t dv, const pmf_qual_t *qual) 566 { 567 return false; 568 } 569 570 bool 571 xhci_shutdown(device_t self, int flags) 572 { 573 return false; 574 } 575 576 577 static void 578 hexdump(const char *msg, const void *base, size_t len) 579 { 580 #if 0 581 size_t cnt; 582 const uint32_t *p; 583 extern paddr_t vtophys(vaddr_t); 584 585 p = base; 586 cnt = 0; 587 588 printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base, 589 (void *)vtophys((vaddr_t)base)); 590 591 while (cnt < len) { 592 if (cnt % 16 == 0) 593 printf("%p: ", p); 594 else if (cnt % 8 == 0) 595 printf(" |"); 596 printf(" %08x", *p++); 597 cnt += 4; 598 if (cnt % 16 == 0) 599 printf("\n"); 600 } 601 #endif 602 } 603 604 605 int 606 xhci_init(struct xhci_softc *sc) 607 { 608 bus_size_t bsz; 609 uint32_t cap, hcs1, hcs2, hcc, dboff, rtsoff; 610 uint32_t ecp, ecr; 611 uint32_t usbcmd, usbsts, pagesize, config; 612 int i; 613 uint16_t hciversion; 614 uint8_t caplength; 615 616 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 617 618 /* XXX Low/Full/High speeds for now */ 619 sc->sc_bus.usbrev = USBREV_2_0; 620 621 cap = xhci_read_4(sc, XHCI_CAPLENGTH); 622 caplength = XHCI_CAP_CAPLENGTH(cap); 623 hciversion = XHCI_CAP_HCIVERSION(cap); 624 625 if ((hciversion < 0x0096) || (hciversion > 0x0100)) { 626 aprint_normal_dev(sc->sc_dev, 627 "xHCI version %x.%x not known to be supported\n", 628 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 629 } else { 630 aprint_verbose_dev(sc->sc_dev, "xHCI version %x.%x\n", 631 (hciversion >> 8) & 0xff, (hciversion >> 0) & 0xff); 632 } 633 634 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0, caplength, 635 &sc->sc_cbh) != 0) { 636 aprint_error_dev(sc->sc_dev, "capability subregion failure\n"); 637 return ENOMEM; 638 } 639 640 hcs1 = xhci_cap_read_4(sc, XHCI_HCSPARAMS1); 641 sc->sc_maxslots = XHCI_HCS1_MAXSLOTS(hcs1); 642 sc->sc_maxintrs = XHCI_HCS1_MAXINTRS(hcs1); 643 sc->sc_maxports = XHCI_HCS1_MAXPORTS(hcs1); 644 hcs2 = xhci_cap_read_4(sc, XHCI_HCSPARAMS2); 645 (void)xhci_cap_read_4(sc, XHCI_HCSPARAMS3); 646 hcc = xhci_cap_read_4(sc, XHCI_HCCPARAMS); 647 648 sc->sc_ac64 = XHCI_HCC_AC64(hcc); 649 sc->sc_ctxsz = XHCI_HCC_CSZ(hcc) ? 64 : 32; 650 aprint_debug_dev(sc->sc_dev, "ac64 %d ctxsz %d\n", sc->sc_ac64, 651 sc->sc_ctxsz); 652 653 aprint_debug_dev(sc->sc_dev, "xECP %x\n", XHCI_HCC_XECP(hcc) * 4); 654 ecp = XHCI_HCC_XECP(hcc) * 4; 655 while (ecp != 0) { 656 ecr = xhci_read_4(sc, ecp); 657 aprint_debug_dev(sc->sc_dev, "ECR %x: %08x\n", ecp, ecr); 658 switch (XHCI_XECP_ID(ecr)) { 659 case XHCI_ID_PROTOCOLS: { 660 uint32_t w0, w4, w8; 661 uint16_t w2; 662 w0 = xhci_read_4(sc, ecp + 0); 663 w2 = (w0 >> 16) & 0xffff; 664 w4 = xhci_read_4(sc, ecp + 4); 665 w8 = xhci_read_4(sc, ecp + 8); 666 aprint_debug_dev(sc->sc_dev, "SP: %08x %08x %08x\n", 667 w0, w4, w8); 668 if (w4 == 0x20425355 && w2 == 0x0300) { 669 sc->sc_ss_port_start = (w8 >> 0) & 0xff;; 670 sc->sc_ss_port_count = (w8 >> 8) & 0xff;; 671 } 672 if (w4 == 0x20425355 && w2 == 0x0200) { 673 sc->sc_hs_port_start = (w8 >> 0) & 0xff; 674 sc->sc_hs_port_count = (w8 >> 8) & 0xff; 675 } 676 break; 677 } 678 default: 679 break; 680 } 681 ecr = xhci_read_4(sc, ecp); 682 if (XHCI_XECP_NEXT(ecr) == 0) { 683 ecp = 0; 684 } else { 685 ecp += XHCI_XECP_NEXT(ecr) * 4; 686 } 687 } 688 689 bsz = XHCI_PORTSC(sc->sc_maxports + 1); 690 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, caplength, bsz, 691 &sc->sc_obh) != 0) { 692 aprint_error_dev(sc->sc_dev, "operational subregion failure\n"); 693 return ENOMEM; 694 } 695 696 dboff = xhci_cap_read_4(sc, XHCI_DBOFF); 697 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, dboff, 698 sc->sc_maxslots * 4, &sc->sc_dbh) != 0) { 699 aprint_error_dev(sc->sc_dev, "doorbell subregion failure\n"); 700 return ENOMEM; 701 } 702 703 rtsoff = xhci_cap_read_4(sc, XHCI_RTSOFF); 704 if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, rtsoff, 705 sc->sc_maxintrs * 0x20, &sc->sc_rbh) != 0) { 706 aprint_error_dev(sc->sc_dev, "runtime subregion failure\n"); 707 return ENOMEM; 708 } 709 710 for (i = 0; i < 100; i++) { 711 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 712 if ((usbsts & XHCI_STS_CNR) == 0) 713 break; 714 usb_delay_ms(&sc->sc_bus, 1); 715 } 716 if (i >= 100) 717 return EIO; 718 719 usbcmd = 0; 720 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 721 usb_delay_ms(&sc->sc_bus, 1); 722 723 usbcmd = XHCI_CMD_HCRST; 724 xhci_op_write_4(sc, XHCI_USBCMD, usbcmd); 725 for (i = 0; i < 100; i++) { 726 usbcmd = xhci_op_read_4(sc, XHCI_USBCMD); 727 if ((usbcmd & XHCI_CMD_HCRST) == 0) 728 break; 729 usb_delay_ms(&sc->sc_bus, 1); 730 } 731 if (i >= 100) 732 return EIO; 733 734 for (i = 0; i < 100; i++) { 735 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 736 if ((usbsts & XHCI_STS_CNR) == 0) 737 break; 738 usb_delay_ms(&sc->sc_bus, 1); 739 } 740 if (i >= 100) 741 return EIO; 742 743 pagesize = xhci_op_read_4(sc, XHCI_PAGESIZE); 744 aprint_debug_dev(sc->sc_dev, "PAGESIZE 0x%08x\n", pagesize); 745 pagesize = ffs(pagesize); 746 if (pagesize == 0) 747 return EIO; 748 sc->sc_pgsz = 1 << (12 + (pagesize - 1)); 749 aprint_debug_dev(sc->sc_dev, "sc_pgsz 0x%08x\n", (uint32_t)sc->sc_pgsz); 750 aprint_debug_dev(sc->sc_dev, "sc_maxslots 0x%08x\n", 751 (uint32_t)sc->sc_maxslots); 752 753 usbd_status err; 754 755 sc->sc_maxspbuf = XHCI_HCS2_MAXSPBUF(hcs2); 756 aprint_debug_dev(sc->sc_dev, "sc_maxspbuf %d\n", sc->sc_maxspbuf); 757 if (sc->sc_maxspbuf != 0) { 758 err = usb_allocmem(&sc->sc_bus, 759 sizeof(uint64_t) * sc->sc_maxspbuf, sizeof(uint64_t), 760 &sc->sc_spbufarray_dma); 761 if (err) 762 return err; 763 764 sc->sc_spbuf_dma = kmem_zalloc(sizeof(*sc->sc_spbuf_dma) * sc->sc_maxspbuf, KM_SLEEP); 765 uint64_t *spbufarray = KERNADDR(&sc->sc_spbufarray_dma, 0); 766 for (i = 0; i < sc->sc_maxspbuf; i++) { 767 usb_dma_t * const dma = &sc->sc_spbuf_dma[i]; 768 /* allocate contexts */ 769 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, 770 sc->sc_pgsz, dma); 771 if (err) 772 return err; 773 spbufarray[i] = htole64(DMAADDR(dma, 0)); 774 usb_syncmem(dma, 0, sc->sc_pgsz, 775 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 776 } 777 778 usb_syncmem(&sc->sc_spbufarray_dma, 0, 779 sizeof(uint64_t) * sc->sc_maxspbuf, BUS_DMASYNC_PREWRITE); 780 } 781 782 config = xhci_op_read_4(sc, XHCI_CONFIG); 783 config &= ~0xFF; 784 config |= sc->sc_maxslots & 0xFF; 785 xhci_op_write_4(sc, XHCI_CONFIG, config); 786 787 err = xhci_ring_init(sc, &sc->sc_cr, XHCI_COMMAND_RING_TRBS, 788 XHCI_COMMAND_RING_SEGMENTS_ALIGN); 789 if (err) { 790 aprint_error_dev(sc->sc_dev, "command ring init fail\n"); 791 return err; 792 } 793 794 err = xhci_ring_init(sc, &sc->sc_er, XHCI_EVENT_RING_TRBS, 795 XHCI_EVENT_RING_SEGMENTS_ALIGN); 796 if (err) { 797 aprint_error_dev(sc->sc_dev, "event ring init fail\n"); 798 return err; 799 } 800 801 usb_dma_t *dma; 802 size_t size; 803 size_t align; 804 805 dma = &sc->sc_eventst_dma; 806 size = roundup2(XHCI_EVENT_RING_SEGMENTS * XHCI_ERSTE_SIZE, 807 XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN); 808 KASSERT(size <= (512 * 1024)); 809 align = XHCI_EVENT_RING_SEGMENT_TABLE_ALIGN; 810 err = usb_allocmem(&sc->sc_bus, size, align, dma); 811 812 memset(KERNADDR(dma, 0), 0, size); 813 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE); 814 aprint_debug_dev(sc->sc_dev, "eventst: %s %016jx %p %zx\n", 815 usbd_errstr(err), 816 (uintmax_t)DMAADDR(&sc->sc_eventst_dma, 0), 817 KERNADDR(&sc->sc_eventst_dma, 0), 818 sc->sc_eventst_dma.block->size); 819 820 dma = &sc->sc_dcbaa_dma; 821 size = (1 + sc->sc_maxslots) * sizeof(uint64_t); 822 KASSERT(size <= 2048); 823 align = XHCI_DEVICE_CONTEXT_BASE_ADDRESS_ARRAY_ALIGN; 824 err = usb_allocmem(&sc->sc_bus, size, align, dma); 825 826 memset(KERNADDR(dma, 0), 0, size); 827 if (sc->sc_maxspbuf != 0) { 828 /* 829 * DCBA entry 0 hold the scratchbuf array pointer. 830 */ 831 *(uint64_t *)KERNADDR(dma, 0) = 832 htole64(DMAADDR(&sc->sc_spbufarray_dma, 0)); 833 } 834 usb_syncmem(dma, 0, size, BUS_DMASYNC_PREWRITE); 835 aprint_debug_dev(sc->sc_dev, "dcbaa: %s %016jx %p %zx\n", 836 usbd_errstr(err), 837 (uintmax_t)DMAADDR(&sc->sc_dcbaa_dma, 0), 838 KERNADDR(&sc->sc_dcbaa_dma, 0), 839 sc->sc_dcbaa_dma.block->size); 840 841 sc->sc_slots = kmem_zalloc(sizeof(*sc->sc_slots) * sc->sc_maxslots, 842 KM_SLEEP); 843 844 cv_init(&sc->sc_command_cv, "xhcicmd"); 845 846 struct xhci_erste *erst; 847 erst = KERNADDR(&sc->sc_eventst_dma, 0); 848 erst[0].erste_0 = htole64(xhci_ring_trbp(&sc->sc_er, 0)); 849 erst[0].erste_2 = htole32(XHCI_EVENT_RING_TRBS); 850 erst[0].erste_3 = htole32(0); 851 usb_syncmem(&sc->sc_eventst_dma, 0, 852 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS, BUS_DMASYNC_PREWRITE); 853 854 xhci_rt_write_4(sc, XHCI_ERSTSZ(0), XHCI_EVENT_RING_SEGMENTS); 855 xhci_rt_write_8(sc, XHCI_ERSTBA(0), DMAADDR(&sc->sc_eventst_dma, 0)); 856 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(&sc->sc_er, 0) | 857 XHCI_ERDP_LO_BUSY); 858 xhci_op_write_8(sc, XHCI_DCBAAP, DMAADDR(&sc->sc_dcbaa_dma, 0)); 859 xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) | 860 sc->sc_cr.xr_cs); 861 862 #if 0 863 hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0), 864 XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS); 865 #endif 866 867 xhci_rt_write_4(sc, XHCI_IMAN(0), XHCI_IMAN_INTR_ENA); 868 xhci_rt_write_4(sc, XHCI_IMOD(0), 0); 869 870 xhci_op_write_4(sc, XHCI_USBCMD, XHCI_CMD_INTE|XHCI_CMD_RS); /* Go! */ 871 aprint_debug_dev(sc->sc_dev, "USBCMD %08"PRIx32"\n", 872 xhci_op_read_4(sc, XHCI_USBCMD)); 873 874 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 875 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_USB); 876 cv_init(&sc->sc_softwake_cv, "xhciab"); 877 878 sc->sc_xferpool = pool_cache_init(sizeof(struct xhci_xfer), 0, 0, 0, 879 "xhcixfer", NULL, IPL_USB, NULL, NULL, NULL); 880 881 /* Set up the bus struct. */ 882 sc->sc_bus.methods = &xhci_bus_methods; 883 sc->sc_bus.pipe_size = sizeof(struct xhci_pipe); 884 885 return USBD_NORMAL_COMPLETION; 886 } 887 888 int 889 xhci_intr(void *v) 890 { 891 struct xhci_softc * const sc = v; 892 int ret = 0; 893 894 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 895 896 if (sc == NULL) 897 return 0; 898 899 mutex_spin_enter(&sc->sc_intr_lock); 900 901 if (sc->sc_dying || !device_has_power(sc->sc_dev)) 902 goto done; 903 904 /* If we get an interrupt while polling, then just ignore it. */ 905 if (sc->sc_bus.use_polling) { 906 #ifdef DIAGNOSTIC 907 DPRINTFN(16, "ignored interrupt while polling", 0, 0, 0, 0); 908 #endif 909 goto done; 910 } 911 912 ret = xhci_intr1(sc); 913 done: 914 mutex_spin_exit(&sc->sc_intr_lock); 915 return ret; 916 } 917 918 int 919 xhci_intr1(struct xhci_softc * const sc) 920 { 921 uint32_t usbsts; 922 uint32_t iman; 923 924 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 925 926 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 927 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0); 928 #if 0 929 if ((usbsts & (XHCI_STS_EINT|XHCI_STS_PCD)) == 0) { 930 return 0; 931 } 932 #endif 933 xhci_op_write_4(sc, XHCI_USBSTS, 934 usbsts & (2|XHCI_STS_EINT|XHCI_STS_PCD)); /* XXX */ 935 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 936 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0); 937 938 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 939 DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0); 940 if ((iman & XHCI_IMAN_INTR_PEND) == 0) { 941 return 0; 942 } 943 xhci_rt_write_4(sc, XHCI_IMAN(0), iman); 944 iman = xhci_rt_read_4(sc, XHCI_IMAN(0)); 945 DPRINTFN(16, "IMAN0 %08x", iman, 0, 0, 0); 946 usbsts = xhci_op_read_4(sc, XHCI_USBSTS); 947 DPRINTFN(16, "USBSTS %08x", usbsts, 0, 0, 0); 948 949 sc->sc_bus.no_intrs++; 950 usb_schedsoftintr(&sc->sc_bus); 951 952 return 1; 953 } 954 955 static usbd_status 956 xhci_configure_endpoint(usbd_pipe_handle pipe) 957 { 958 struct xhci_softc * const sc = pipe->device->bus->hci_private; 959 struct xhci_slot * const xs = pipe->device->hci_private; 960 const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc); 961 usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc; 962 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 963 struct xhci_trb trb; 964 usbd_status err; 965 uint32_t *cp; 966 967 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 968 DPRINTFN(4, "dci %u epaddr 0x%02x attr 0x%02x", 969 dci, ed->bEndpointAddress, ed->bmAttributes, 0); 970 971 /* XXX ensure input context is available? */ 972 973 memset(xhci_slot_get_icv(sc, xs, 0), 0, sc->sc_pgsz); 974 975 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 976 cp[0] = htole32(0); 977 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(dci)); 978 979 /* set up input slot context */ 980 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT)); 981 cp[0] = htole32(XHCI_SCTX_0_CTX_NUM_SET(dci)); 982 cp[1] = htole32(0); 983 cp[2] = htole32(0); 984 cp[3] = htole32(0); 985 986 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(dci)); 987 uint8_t eptype = xhci_ep_get_type(pipe->endpoint->edesc); 988 if (xfertype == UE_INTERRUPT) { 989 cp[0] = htole32( 990 XHCI_EPCTX_0_IVAL_SET(3) /* XXX */ 991 ); 992 cp[1] = htole32( 993 XHCI_EPCTX_1_CERR_SET(3) | 994 XHCI_EPCTX_1_EPTYPE_SET(eptype) | 995 XHCI_EPCTX_1_MAXB_SET(0) | 996 XHCI_EPCTX_1_MAXP_SIZE_SET(8) /* XXX */ 997 ); 998 cp[4] = htole32( 999 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8) 1000 ); 1001 } else { 1002 cp[0] = htole32(0); 1003 cp[1] = htole32( 1004 XHCI_EPCTX_1_CERR_SET(3) | 1005 XHCI_EPCTX_1_EPTYPE_SET(eptype) | 1006 XHCI_EPCTX_1_MAXB_SET(0) | 1007 XHCI_EPCTX_1_MAXP_SIZE_SET(512) /* XXX */ 1008 ); 1009 } 1010 *(uint64_t *)(&cp[2]) = htole64( 1011 xhci_ring_trbp(&xs->xs_ep[dci].xe_tr, 0) | 1012 XHCI_EPCTX_2_DCS_SET(1)); 1013 1014 /* sync input contexts before they are read from memory */ 1015 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 1016 hexdump("input control context", xhci_slot_get_icv(sc, xs, 0), 1017 sc->sc_ctxsz * 1); 1018 hexdump("input endpoint context", xhci_slot_get_icv(sc, xs, 1019 xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1); 1020 1021 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 1022 trb.trb_2 = 0; 1023 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1024 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_CONFIGURE_EP); 1025 1026 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1027 1028 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 1029 hexdump("output context", xhci_slot_get_dcv(sc, xs, dci), 1030 sc->sc_ctxsz * 1); 1031 1032 return err; 1033 } 1034 1035 static usbd_status 1036 xhci_unconfigure_endpoint(usbd_pipe_handle pipe) 1037 { 1038 #ifdef USB_DEBUG 1039 struct xhci_slot * const xs = pipe->device->hci_private; 1040 #endif 1041 1042 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1043 DPRINTFN(4, "slot %u", xs->xs_idx, 0, 0, 0); 1044 1045 return USBD_NORMAL_COMPLETION; 1046 } 1047 1048 static usbd_status 1049 xhci_reset_endpoint(usbd_pipe_handle pipe) 1050 { 1051 struct xhci_softc * const sc = pipe->device->bus->hci_private; 1052 struct xhci_slot * const xs = pipe->device->hci_private; 1053 const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc); 1054 struct xhci_trb trb; 1055 usbd_status err; 1056 1057 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1058 DPRINTFN(4, "dci %u", dci, 0, 0, 0); 1059 1060 trb.trb_0 = 0; 1061 trb.trb_2 = 0; 1062 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1063 XHCI_TRB_3_EP_SET(dci) | 1064 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_RESET_EP); 1065 1066 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1067 1068 return err; 1069 } 1070 1071 #if 0 1072 static usbd_status 1073 xhci_stop_endpoint(usbd_pipe_handle pipe) 1074 { 1075 struct xhci_softc * const sc = pipe->device->bus->hci_private; 1076 struct xhci_slot * const xs = pipe->device->hci_private; 1077 struct xhci_trb trb; 1078 usbd_status err; 1079 const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc); 1080 1081 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1082 DPRINTFN(4, "dci %u", dci, 0, 0, 0); 1083 1084 trb.trb_0 = 0; 1085 trb.trb_2 = 0; 1086 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1087 XHCI_TRB_3_EP_SET(dci) | 1088 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STOP_EP); 1089 1090 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1091 1092 return err; 1093 } 1094 #endif 1095 1096 static usbd_status 1097 xhci_set_dequeue(usbd_pipe_handle pipe) 1098 { 1099 struct xhci_softc * const sc = pipe->device->bus->hci_private; 1100 struct xhci_slot * const xs = pipe->device->hci_private; 1101 const u_int dci = xhci_ep_get_dci(pipe->endpoint->edesc); 1102 struct xhci_ring * const xr = &xs->xs_ep[dci].xe_tr; 1103 struct xhci_trb trb; 1104 usbd_status err; 1105 1106 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1107 DPRINTFN(4, "slot %u dci %u", xs->xs_idx, dci, 0, 0); 1108 1109 memset(xr->xr_trb, 0, xr->xr_ntrb * XHCI_TRB_SIZE); 1110 usb_syncmem(&xr->xr_dma, 0, xr->xr_ntrb * XHCI_TRB_SIZE, 1111 BUS_DMASYNC_PREWRITE); 1112 1113 xr->xr_ep = 0; 1114 xr->xr_cs = 1; 1115 1116 trb.trb_0 = xhci_ring_trbp(xr, 0) | 1; /* XXX */ 1117 trb.trb_2 = 0; 1118 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1119 XHCI_TRB_3_EP_SET(dci) | 1120 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SET_TR_DEQUEUE); 1121 1122 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1123 1124 return err; 1125 } 1126 1127 static usbd_status 1128 xhci_open(usbd_pipe_handle pipe) 1129 { 1130 usbd_device_handle const dev = pipe->device; 1131 struct xhci_softc * const sc = dev->bus->hci_private; 1132 usb_endpoint_descriptor_t * const ed = pipe->endpoint->edesc; 1133 const uint8_t xfertype = UE_GET_XFERTYPE(ed->bmAttributes); 1134 1135 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1136 DPRINTFN(1, "addr %d depth %d port %d speed %d", 1137 dev->address, dev->depth, dev->powersrc->portno, dev->speed); 1138 1139 if (sc->sc_dying) 1140 return USBD_IOERROR; 1141 1142 /* Root Hub */ 1143 if (dev->depth == 0 && dev->powersrc->portno == 0 && 1144 dev->speed != USB_SPEED_SUPER) { 1145 switch (ed->bEndpointAddress) { 1146 case USB_CONTROL_ENDPOINT: 1147 pipe->methods = &xhci_root_ctrl_methods; 1148 break; 1149 case UE_DIR_IN | XHCI_INTR_ENDPT: 1150 pipe->methods = &xhci_root_intr_methods; 1151 break; 1152 default: 1153 pipe->methods = NULL; 1154 DPRINTFN(0, "bad bEndpointAddress 0x%02x", 1155 ed->bEndpointAddress, 0, 0, 0); 1156 return USBD_INVAL; 1157 } 1158 return USBD_NORMAL_COMPLETION; 1159 } 1160 1161 switch (xfertype) { 1162 case UE_CONTROL: 1163 pipe->methods = &xhci_device_ctrl_methods; 1164 break; 1165 case UE_ISOCHRONOUS: 1166 pipe->methods = &xhci_device_isoc_methods; 1167 return USBD_INVAL; 1168 break; 1169 case UE_BULK: 1170 pipe->methods = &xhci_device_bulk_methods; 1171 break; 1172 case UE_INTERRUPT: 1173 pipe->methods = &xhci_device_intr_methods; 1174 break; 1175 default: 1176 return USBD_IOERROR; 1177 break; 1178 } 1179 1180 if (ed->bEndpointAddress != USB_CONTROL_ENDPOINT) 1181 xhci_configure_endpoint(pipe); 1182 1183 return USBD_NORMAL_COMPLETION; 1184 } 1185 1186 static void 1187 xhci_rhpsc(struct xhci_softc * const sc, u_int port) 1188 { 1189 usbd_xfer_handle const xfer = sc->sc_intrxfer; 1190 uint8_t *p; 1191 1192 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1193 DPRINTFN(4, "port %u status change", port, 0, 0, 0); 1194 1195 if (xfer == NULL) 1196 return; 1197 1198 if (!(port >= sc->sc_hs_port_start && 1199 port < sc->sc_hs_port_start + sc->sc_hs_port_count)) 1200 return; 1201 1202 port -= sc->sc_hs_port_start; 1203 port += 1; 1204 DPRINTFN(4, "hs port %u status change", port, 0, 0, 0); 1205 1206 p = KERNADDR(&xfer->dmabuf, 0); 1207 memset(p, 0, xfer->length); 1208 p[port/NBBY] |= 1 << (port%NBBY); 1209 xfer->actlen = xfer->length; 1210 xfer->status = USBD_NORMAL_COMPLETION; 1211 usb_transfer_complete(xfer); 1212 } 1213 1214 static void 1215 xhci_handle_event(struct xhci_softc * const sc, 1216 const struct xhci_trb * const trb) 1217 { 1218 uint64_t trb_0; 1219 uint32_t trb_2, trb_3; 1220 1221 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1222 1223 trb_0 = le64toh(trb->trb_0); 1224 trb_2 = le32toh(trb->trb_2); 1225 trb_3 = le32toh(trb->trb_3); 1226 1227 DPRINTFN(14, "event: %p 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32, 1228 trb, trb_0, trb_2, trb_3); 1229 1230 switch (XHCI_TRB_3_TYPE_GET(trb_3)){ 1231 case XHCI_TRB_EVENT_TRANSFER: { 1232 u_int slot, dci; 1233 struct xhci_slot *xs; 1234 struct xhci_ring *xr; 1235 struct xhci_xfer *xx; 1236 usbd_xfer_handle xfer; 1237 usbd_status err; 1238 1239 slot = XHCI_TRB_3_SLOT_GET(trb_3); 1240 dci = XHCI_TRB_3_EP_GET(trb_3); 1241 1242 xs = &sc->sc_slots[slot]; 1243 xr = &xs->xs_ep[dci].xe_tr; 1244 1245 if ((trb_3 & XHCI_TRB_3_ED_BIT) == 0) { 1246 xx = xr->xr_cookies[(trb_0 - xhci_ring_trbp(xr, 0))/ 1247 sizeof(struct xhci_trb)]; 1248 } else { 1249 xx = (void *)(uintptr_t)(trb_0 & ~0x3); 1250 } 1251 xfer = &xx->xx_xfer; 1252 DPRINTFN(14, "xfer %p", xfer, 0, 0, 0); 1253 1254 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) { 1255 DPRINTFN(14, "transfer event data: " 1256 "0x%016"PRIx64" 0x%08"PRIx32" %02x", 1257 trb_0, XHCI_TRB_2_REM_GET(trb_2), 1258 XHCI_TRB_2_ERROR_GET(trb_2), 0); 1259 if ((trb_0 & 0x3) == 0x3) { 1260 xfer->actlen = XHCI_TRB_2_REM_GET(trb_2); 1261 } 1262 } 1263 1264 if (XHCI_TRB_2_ERROR_GET(trb_2) == 1265 XHCI_TRB_ERROR_SUCCESS) { 1266 xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2); 1267 err = USBD_NORMAL_COMPLETION; 1268 } else if (XHCI_TRB_2_ERROR_GET(trb_2) == 1269 XHCI_TRB_ERROR_SHORT_PKT) { 1270 xfer->actlen = xfer->length - XHCI_TRB_2_REM_GET(trb_2); 1271 err = USBD_NORMAL_COMPLETION; 1272 } else if (XHCI_TRB_2_ERROR_GET(trb_2) == 1273 XHCI_TRB_ERROR_STALL) { 1274 err = USBD_STALLED; 1275 xr->is_halted = true; 1276 DPRINTFN(1, "ev: xfer done: err %u slot %u dci %u", 1277 XHCI_TRB_2_ERROR_GET(trb_2), slot, dci, 0); 1278 } else { 1279 err = USBD_IOERROR; 1280 } 1281 xfer->status = err; 1282 1283 //mutex_enter(&sc->sc_lock); /* XXX ??? */ 1284 if ((trb_3 & XHCI_TRB_3_ED_BIT) != 0) { 1285 if ((trb_0 & 0x3) == 0x0) { 1286 usb_transfer_complete(xfer); 1287 } 1288 } else { 1289 usb_transfer_complete(xfer); 1290 } 1291 //mutex_exit(&sc->sc_lock); /* XXX ??? */ 1292 1293 } 1294 break; 1295 case XHCI_TRB_EVENT_CMD_COMPLETE: 1296 if (trb_0 == sc->sc_command_addr) { 1297 sc->sc_result_trb.trb_0 = trb_0; 1298 sc->sc_result_trb.trb_2 = trb_2; 1299 sc->sc_result_trb.trb_3 = trb_3; 1300 if (XHCI_TRB_2_ERROR_GET(trb_2) != 1301 XHCI_TRB_ERROR_SUCCESS) { 1302 DPRINTFN(1, "command completion " 1303 "failure: 0x%016"PRIx64" 0x%08"PRIx32" " 1304 "0x%08"PRIx32, trb_0, trb_2, trb_3, 0); 1305 } 1306 cv_signal(&sc->sc_command_cv); 1307 } else { 1308 DPRINTFN(1, "event: %p 0x%016"PRIx64" " 1309 "0x%08"PRIx32" 0x%08"PRIx32, trb, trb_0, 1310 trb_2, trb_3); 1311 } 1312 break; 1313 case XHCI_TRB_EVENT_PORT_STS_CHANGE: 1314 xhci_rhpsc(sc, (uint32_t)((trb_0 >> 24) & 0xff)); 1315 break; 1316 default: 1317 break; 1318 } 1319 } 1320 1321 static void 1322 xhci_softintr(void *v) 1323 { 1324 struct usbd_bus * const bus = v; 1325 struct xhci_softc * const sc = bus->hci_private; 1326 struct xhci_ring * const er = &sc->sc_er; 1327 struct xhci_trb *trb; 1328 int i, j, k; 1329 1330 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1331 1332 i = er->xr_ep; 1333 j = er->xr_cs; 1334 1335 DPRINTFN(16, "xr_ep %d xr_cs %d", i, j, 0, 0); 1336 1337 while (1) { 1338 usb_syncmem(&er->xr_dma, XHCI_TRB_SIZE * i, XHCI_TRB_SIZE, 1339 BUS_DMASYNC_POSTREAD); 1340 trb = &er->xr_trb[i]; 1341 k = (le32toh(trb->trb_3) & XHCI_TRB_3_CYCLE_BIT) ? 1 : 0; 1342 1343 if (j != k) 1344 break; 1345 1346 xhci_handle_event(sc, trb); 1347 1348 i++; 1349 if (i == XHCI_EVENT_RING_TRBS) { 1350 i = 0; 1351 j ^= 1; 1352 } 1353 } 1354 1355 er->xr_ep = i; 1356 er->xr_cs = j; 1357 1358 xhci_rt_write_8(sc, XHCI_ERDP(0), xhci_ring_trbp(er, er->xr_ep) | 1359 XHCI_ERDP_LO_BUSY); 1360 1361 DPRINTFN(16, "ends", 0, 0, 0, 0); 1362 1363 return; 1364 } 1365 1366 static void 1367 xhci_poll(struct usbd_bus *bus) 1368 { 1369 struct xhci_softc * const sc = bus->hci_private; 1370 1371 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1372 1373 mutex_spin_enter(&sc->sc_intr_lock); 1374 xhci_intr1(sc); 1375 mutex_spin_exit(&sc->sc_intr_lock); 1376 1377 return; 1378 } 1379 1380 static usbd_status 1381 xhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, uint32_t size) 1382 { 1383 struct xhci_softc * const sc = bus->hci_private; 1384 usbd_status err; 1385 1386 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1387 1388 err = usb_allocmem(&sc->sc_bus, size, 0, dma); 1389 #if 0 1390 if (err == USBD_NOMEM) 1391 err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size); 1392 #endif 1393 #ifdef XHCI_DEBUG 1394 if (err) 1395 DPRINTFN(1, "usb_allocmem(%u)=%d", err, size, 0, 0); 1396 #endif 1397 1398 return err; 1399 } 1400 1401 static void 1402 xhci_freem(struct usbd_bus *bus, usb_dma_t *dma) 1403 { 1404 struct xhci_softc * const sc = bus->hci_private; 1405 1406 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1407 1408 #if 0 1409 if (dma->block->flags & USB_DMA_RESERVE) { 1410 usb_reserve_freem(&sc->sc_dma_reserve, dma); 1411 return; 1412 } 1413 #endif 1414 usb_freemem(&sc->sc_bus, dma); 1415 } 1416 1417 static usbd_xfer_handle 1418 xhci_allocx(struct usbd_bus *bus) 1419 { 1420 struct xhci_softc * const sc = bus->hci_private; 1421 usbd_xfer_handle xfer; 1422 1423 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1424 1425 xfer = pool_cache_get(sc->sc_xferpool, PR_NOWAIT); 1426 if (xfer != NULL) { 1427 memset(xfer, 0, sizeof(struct xhci_xfer)); 1428 #ifdef DIAGNOSTIC 1429 xfer->busy_free = XFER_BUSY; 1430 #endif 1431 } 1432 1433 return xfer; 1434 } 1435 1436 static void 1437 xhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) 1438 { 1439 struct xhci_softc * const sc = bus->hci_private; 1440 1441 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1442 1443 #ifdef DIAGNOSTIC 1444 if (xfer->busy_free != XFER_BUSY) { 1445 DPRINTFN(0, "xfer=%p not busy, 0x%08x", 1446 xfer, xfer->busy_free, 0, 0); 1447 } 1448 xfer->busy_free = XFER_FREE; 1449 #endif 1450 pool_cache_put(sc->sc_xferpool, xfer); 1451 } 1452 1453 static void 1454 xhci_get_lock(struct usbd_bus *bus, kmutex_t **lock) 1455 { 1456 struct xhci_softc * const sc = bus->hci_private; 1457 1458 *lock = &sc->sc_lock; 1459 } 1460 1461 extern u_int32_t usb_cookie_no; 1462 1463 static usbd_status 1464 xhci_new_device(device_t parent, usbd_bus_handle bus, int depth, 1465 int speed, int port, struct usbd_port *up) 1466 { 1467 struct xhci_softc * const sc = bus->hci_private; 1468 usbd_device_handle dev; 1469 usbd_status err; 1470 usb_device_descriptor_t *dd; 1471 struct usbd_device *hub; 1472 struct usbd_device *adev; 1473 int rhport = 0; 1474 struct xhci_slot *xs; 1475 uint32_t *cp; 1476 uint8_t slot; 1477 uint8_t addr; 1478 1479 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1480 DPRINTFN(4, "port=%d depth=%d speed=%d upport %d", 1481 port, depth, speed, up->portno); 1482 1483 dev = malloc(sizeof *dev, M_USB, M_NOWAIT|M_ZERO); 1484 if (dev == NULL) 1485 return USBD_NOMEM; 1486 1487 dev->bus = bus; 1488 1489 /* Set up default endpoint handle. */ 1490 dev->def_ep.edesc = &dev->def_ep_desc; 1491 1492 /* Set up default endpoint descriptor. */ 1493 dev->def_ep_desc.bLength = USB_ENDPOINT_DESCRIPTOR_SIZE; 1494 dev->def_ep_desc.bDescriptorType = UDESC_ENDPOINT; 1495 dev->def_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 1496 dev->def_ep_desc.bmAttributes = UE_CONTROL; 1497 /* XXX */ 1498 if (speed == USB_SPEED_LOW) 1499 USETW(dev->def_ep_desc.wMaxPacketSize, USB_MAX_IPACKET); 1500 else 1501 USETW(dev->def_ep_desc.wMaxPacketSize, 64); 1502 dev->def_ep_desc.bInterval = 0; 1503 1504 /* doesn't matter, just don't let it uninitialized */ 1505 dev->def_ep.datatoggle = 0; 1506 1507 DPRINTFN(4, "up %p portno %d", up, up->portno, 0, 0); 1508 1509 dev->quirks = &usbd_no_quirk; 1510 dev->address = 0; 1511 dev->ddesc.bMaxPacketSize = 0; 1512 dev->depth = depth; 1513 dev->powersrc = up; 1514 dev->myhub = up->parent; 1515 1516 up->device = dev; 1517 1518 /* Locate root hub port */ 1519 for (adev = dev, hub = dev; 1520 hub != NULL; 1521 adev = hub, hub = hub->myhub) { 1522 DPRINTFN(4, "hub %p", hub, 0, 0, 0); 1523 } 1524 DPRINTFN(4, "hub %p", hub, 0, 0, 0); 1525 1526 if (hub != NULL) { 1527 for (int p = 0; p < hub->hub->hubdesc.bNbrPorts; p++) { 1528 if (hub->hub->ports[p].device == adev) { 1529 rhport = p; 1530 } 1531 } 1532 } else { 1533 rhport = port; 1534 } 1535 if (speed == USB_SPEED_SUPER) { 1536 rhport += sc->sc_ss_port_start - 1; 1537 } else { 1538 rhport += sc->sc_hs_port_start - 1; 1539 } 1540 DPRINTFN(4, "rhport %d", rhport, 0, 0, 0); 1541 1542 dev->speed = speed; 1543 dev->langid = USBD_NOLANG; 1544 dev->cookie.cookie = ++usb_cookie_no; 1545 1546 /* Establish the default pipe. */ 1547 err = usbd_setup_pipe(dev, 0, &dev->def_ep, USBD_DEFAULT_INTERVAL, 1548 &dev->default_pipe); 1549 if (err) { 1550 usbd_remove_device(dev, up); 1551 return (err); 1552 } 1553 1554 dd = &dev->ddesc; 1555 1556 if ((depth == 0) && (port == 0)) { 1557 KASSERT(bus->devices[dev->address] == NULL); 1558 bus->devices[dev->address] = dev; 1559 err = usbd_get_initial_ddesc(dev, dd); 1560 if (err) 1561 return err; 1562 err = usbd_reload_device_desc(dev); 1563 if (err) 1564 return err; 1565 } else { 1566 err = xhci_enable_slot(sc, &slot); 1567 if (err) 1568 return err; 1569 err = xhci_init_slot(sc, slot, depth, speed, port, rhport); 1570 if (err) 1571 return err; 1572 xs = &sc->sc_slots[slot]; 1573 dev->hci_private = xs; 1574 cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT); 1575 //hexdump("slot context", cp, sc->sc_ctxsz); 1576 addr = XHCI_SCTX_3_DEV_ADDR_GET(cp[3]); 1577 DPRINTFN(4, "device address %u", addr, 0, 0, 0); 1578 /* XXX ensure we know when the hardware does something 1579 we can't yet cope with */ 1580 KASSERT(addr >= 1 && addr <= 127); 1581 dev->address = addr; 1582 /* XXX dev->address not necessarily unique on bus */ 1583 KASSERT(bus->devices[dev->address] == NULL); 1584 bus->devices[dev->address] = dev; 1585 1586 err = usbd_get_initial_ddesc(dev, dd); 1587 if (err) 1588 return err; 1589 /* 4.8.2.1 */ 1590 if (speed == USB_SPEED_SUPER) 1591 USETW(dev->def_ep_desc.wMaxPacketSize, 1592 (1 << dd->bMaxPacketSize)); 1593 else 1594 USETW(dev->def_ep_desc.wMaxPacketSize, 1595 dd->bMaxPacketSize); 1596 DPRINTFN(4, "bMaxPacketSize %u", dd->bMaxPacketSize, 0, 0, 0); 1597 xhci_update_ep0_mps(sc, xs, 1598 UGETW(dev->def_ep_desc.wMaxPacketSize)); 1599 err = usbd_reload_device_desc(dev); 1600 if (err) 1601 return err; 1602 1603 usbd_kill_pipe(dev->default_pipe); 1604 err = usbd_setup_pipe(dev, 0, &dev->def_ep, 1605 USBD_DEFAULT_INTERVAL, &dev->default_pipe); 1606 } 1607 1608 DPRINTFN(1, "adding unit addr=%d, rev=%02x,", 1609 dev->address, UGETW(dd->bcdUSB), 0, 0); 1610 DPRINTFN(1, " class=%d, subclass=%d, protocol=%d,", 1611 dd->bDeviceClass, dd->bDeviceSubClass, 1612 dd->bDeviceProtocol, 0); 1613 DPRINTFN(1, " mps=%d, len=%d, noconf=%d, speed=%d", 1614 dd->bMaxPacketSize, dd->bLength, dd->bNumConfigurations, 1615 dev->speed); 1616 1617 usbd_get_device_strings(dev); 1618 1619 usbd_add_dev_event(USB_EVENT_DEVICE_ATTACH, dev); 1620 1621 if ((depth == 0) && (port == 0)) { 1622 usbd_attach_roothub(parent, dev); 1623 DPRINTFN(1, "root_hub %p", bus->root_hub, 0, 0, 0); 1624 return USBD_NORMAL_COMPLETION; 1625 } 1626 1627 1628 err = usbd_probe_and_attach(parent, dev, port, dev->address); 1629 if (err) { 1630 usbd_remove_device(dev, up); 1631 return (err); 1632 } 1633 1634 return USBD_NORMAL_COMPLETION; 1635 } 1636 1637 static usbd_status 1638 xhci_ring_init(struct xhci_softc * const sc, struct xhci_ring * const xr, 1639 size_t ntrb, size_t align) 1640 { 1641 usbd_status err; 1642 size_t size = ntrb * XHCI_TRB_SIZE; 1643 1644 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1645 1646 err = usb_allocmem(&sc->sc_bus, size, align, &xr->xr_dma); 1647 if (err) 1648 return err; 1649 mutex_init(&xr->xr_lock, MUTEX_DEFAULT, IPL_SOFTUSB); 1650 xr->xr_cookies = kmem_zalloc(sizeof(*xr->xr_cookies) * ntrb, KM_SLEEP); 1651 xr->xr_trb = xhci_ring_trbv(xr, 0); 1652 xr->xr_ntrb = ntrb; 1653 xr->xr_ep = 0; 1654 xr->xr_cs = 1; 1655 memset(xr->xr_trb, 0, size); 1656 usb_syncmem(&xr->xr_dma, 0, size, BUS_DMASYNC_PREWRITE); 1657 xr->is_halted = false; 1658 1659 return USBD_NORMAL_COMPLETION; 1660 } 1661 1662 static void 1663 xhci_ring_free(struct xhci_softc * const sc, struct xhci_ring * const xr) 1664 { 1665 usb_freemem(&sc->sc_bus, &xr->xr_dma); 1666 mutex_destroy(&xr->xr_lock); 1667 kmem_free(xr->xr_cookies, sizeof(*xr->xr_cookies) * xr->xr_ntrb); 1668 } 1669 1670 static void 1671 xhci_ring_put(struct xhci_softc * const sc, struct xhci_ring * const xr, 1672 void *cookie, struct xhci_trb * const trbs, size_t ntrbs) 1673 { 1674 size_t i; 1675 u_int ri; 1676 u_int cs; 1677 uint64_t parameter; 1678 uint32_t status; 1679 uint32_t control; 1680 1681 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1682 1683 for (i = 0; i < ntrbs; i++) { 1684 DPRINTFN(12, "xr %p trbs %p num %zu", xr, trbs, i, 0); 1685 DPRINTFN(12, " %016"PRIx64" %08"PRIx32" %08"PRIx32, 1686 trbs[i].trb_0, trbs[i].trb_2, trbs[i].trb_3, 0); 1687 KASSERT(XHCI_TRB_3_TYPE_GET(trbs[i].trb_3) != 1688 XHCI_TRB_TYPE_LINK); 1689 } 1690 1691 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0); 1692 1693 ri = xr->xr_ep; 1694 cs = xr->xr_cs; 1695 1696 /* 1697 * Although the xhci hardware can do scatter/gather dma from 1698 * arbitrary sized buffers, there is a non-obvious restriction 1699 * that a LINK trb is only allowed at the end of a burst of 1700 * transfers - which might be 16kB. 1701 * Arbitrary aligned LINK trb definitely fail on Ivy bridge. 1702 * The simple solution is not to allow a LINK trb in the middle 1703 * of anything - as here. 1704 * XXX: (dsl) There are xhci controllers out there (eg some made by 1705 * ASMedia) that seem to lock up if they process a LINK trb but 1706 * cannot process the linked-to trb yet. 1707 * The code should write the 'cycle' bit on the link trb AFTER 1708 * adding the other trb. 1709 */ 1710 if (ri + ntrbs >= (xr->xr_ntrb - 1)) { 1711 parameter = xhci_ring_trbp(xr, 0); 1712 status = 0; 1713 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_LINK) | 1714 XHCI_TRB_3_TC_BIT | (cs ? XHCI_TRB_3_CYCLE_BIT : 0); 1715 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter), 1716 htole32(status), htole32(control)); 1717 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1, 1718 BUS_DMASYNC_PREWRITE); 1719 xr->xr_cookies[ri] = NULL; 1720 xr->xr_ep = 0; 1721 xr->xr_cs ^= 1; 1722 ri = xr->xr_ep; 1723 cs = xr->xr_cs; 1724 } 1725 1726 ri++; 1727 1728 /* Write any subsequent TRB first */ 1729 for (i = 1; i < ntrbs; i++) { 1730 parameter = trbs[i].trb_0; 1731 status = trbs[i].trb_2; 1732 control = trbs[i].trb_3; 1733 1734 if (cs) { 1735 control |= XHCI_TRB_3_CYCLE_BIT; 1736 } else { 1737 control &= ~XHCI_TRB_3_CYCLE_BIT; 1738 } 1739 1740 xhci_trb_put(&xr->xr_trb[ri], htole64(parameter), 1741 htole32(status), htole32(control)); 1742 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1, 1743 BUS_DMASYNC_PREWRITE); 1744 xr->xr_cookies[ri] = cookie; 1745 ri++; 1746 } 1747 1748 /* Write the first TRB last */ 1749 i = 0; 1750 { 1751 parameter = trbs[i].trb_0; 1752 status = trbs[i].trb_2; 1753 control = trbs[i].trb_3; 1754 1755 if (xr->xr_cs) { 1756 control |= XHCI_TRB_3_CYCLE_BIT; 1757 } else { 1758 control &= ~XHCI_TRB_3_CYCLE_BIT; 1759 } 1760 1761 xhci_trb_put(&xr->xr_trb[xr->xr_ep], htole64(parameter), 1762 htole32(status), htole32(control)); 1763 usb_syncmem(&xr->xr_dma, XHCI_TRB_SIZE * ri, XHCI_TRB_SIZE * 1, 1764 BUS_DMASYNC_PREWRITE); 1765 xr->xr_cookies[xr->xr_ep] = cookie; 1766 } 1767 1768 xr->xr_ep = ri; 1769 xr->xr_cs = cs; 1770 1771 DPRINTFN(12, "%p xr_ep 0x%x xr_cs %u", xr, xr->xr_ep, xr->xr_cs, 0); 1772 } 1773 1774 static usbd_status 1775 xhci_do_command(struct xhci_softc * const sc, struct xhci_trb * const trb, 1776 int timeout) 1777 { 1778 struct xhci_ring * const cr = &sc->sc_cr; 1779 usbd_status err; 1780 1781 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1782 DPRINTFN(12, "input: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32, 1783 trb->trb_0, trb->trb_2, trb->trb_3, 0); 1784 1785 mutex_enter(&sc->sc_lock); 1786 1787 KASSERT(sc->sc_command_addr == 0); 1788 sc->sc_command_addr = xhci_ring_trbp(cr, cr->xr_ep); 1789 1790 mutex_enter(&cr->xr_lock); 1791 xhci_ring_put(sc, cr, NULL, trb, 1); 1792 mutex_exit(&cr->xr_lock); 1793 1794 xhci_db_write_4(sc, XHCI_DOORBELL(0), 0); 1795 1796 if (cv_timedwait(&sc->sc_command_cv, &sc->sc_lock, 1797 MAX(1, mstohz(timeout))) == EWOULDBLOCK) { 1798 err = USBD_TIMEOUT; 1799 goto timedout; 1800 } 1801 1802 trb->trb_0 = sc->sc_result_trb.trb_0; 1803 trb->trb_2 = sc->sc_result_trb.trb_2; 1804 trb->trb_3 = sc->sc_result_trb.trb_3; 1805 1806 DPRINTFN(12, "output: 0x%016"PRIx64" 0x%08"PRIx32" 0x%08"PRIx32"", 1807 trb->trb_0, trb->trb_2, trb->trb_3, 0); 1808 1809 switch (XHCI_TRB_2_ERROR_GET(trb->trb_2)) { 1810 case XHCI_TRB_ERROR_SUCCESS: 1811 err = USBD_NORMAL_COMPLETION; 1812 break; 1813 default: 1814 case 192 ... 223: 1815 err = USBD_IOERROR; 1816 break; 1817 case 224 ... 255: 1818 err = USBD_NORMAL_COMPLETION; 1819 break; 1820 } 1821 1822 timedout: 1823 sc->sc_command_addr = 0; 1824 mutex_exit(&sc->sc_lock); 1825 return err; 1826 } 1827 1828 static usbd_status 1829 xhci_enable_slot(struct xhci_softc * const sc, uint8_t * const slotp) 1830 { 1831 struct xhci_trb trb; 1832 usbd_status err; 1833 1834 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1835 1836 trb.trb_0 = 0; 1837 trb.trb_2 = 0; 1838 trb.trb_3 = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ENABLE_SLOT); 1839 1840 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1841 if (err != USBD_NORMAL_COMPLETION) { 1842 return err; 1843 } 1844 1845 *slotp = XHCI_TRB_3_SLOT_GET(trb.trb_3); 1846 1847 return err; 1848 } 1849 1850 static usbd_status 1851 xhci_address_device(struct xhci_softc * const sc, 1852 uint64_t icp, uint8_t slot_id, bool bsr) 1853 { 1854 struct xhci_trb trb; 1855 usbd_status err; 1856 1857 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1858 1859 trb.trb_0 = icp; 1860 trb.trb_2 = 0; 1861 trb.trb_3 = XHCI_TRB_3_SLOT_SET(slot_id) | 1862 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_ADDRESS_DEVICE) | 1863 (bsr ? XHCI_TRB_3_BSR_BIT : 0); 1864 1865 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1866 return err; 1867 } 1868 1869 static usbd_status 1870 xhci_update_ep0_mps(struct xhci_softc * const sc, 1871 struct xhci_slot * const xs, u_int mps) 1872 { 1873 struct xhci_trb trb; 1874 usbd_status err; 1875 uint32_t * cp; 1876 1877 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1878 DPRINTFN(4, "slot %u mps %u", xs->xs_idx, mps, 0, 0); 1879 1880 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 1881 cp[0] = htole32(0); 1882 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)); 1883 1884 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL)); 1885 cp[1] = htole32(XHCI_EPCTX_1_MAXP_SIZE_SET(mps)); 1886 1887 /* sync input contexts before they are read from memory */ 1888 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 1889 hexdump("input context", xhci_slot_get_icv(sc, xs, 0), 1890 sc->sc_ctxsz * 4); 1891 1892 trb.trb_0 = xhci_slot_get_icp(sc, xs, 0); 1893 trb.trb_2 = 0; 1894 trb.trb_3 = XHCI_TRB_3_SLOT_SET(xs->xs_idx) | 1895 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVALUATE_CTX); 1896 1897 err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT); 1898 KASSERT(err == USBD_NORMAL_COMPLETION); /* XXX */ 1899 return err; 1900 } 1901 1902 static void 1903 xhci_set_dcba(struct xhci_softc * const sc, uint64_t dcba, int si) 1904 { 1905 uint64_t * const dcbaa = KERNADDR(&sc->sc_dcbaa_dma, 0); 1906 1907 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1908 DPRINTFN(4, "dcbaa %p dc %016"PRIx64" slot %d", 1909 &dcbaa[si], dcba, si, 0); 1910 1911 dcbaa[si] = htole64(dcba); 1912 usb_syncmem(&sc->sc_dcbaa_dma, si * sizeof(uint64_t), sizeof(uint64_t), 1913 BUS_DMASYNC_PREWRITE); 1914 } 1915 1916 static usbd_status 1917 xhci_init_slot(struct xhci_softc * const sc, uint32_t slot, int depth, 1918 int speed, int port, int rhport) 1919 { 1920 struct xhci_slot *xs; 1921 usbd_status err; 1922 u_int dci; 1923 uint32_t *cp; 1924 uint32_t mps; 1925 uint32_t xspeed; 1926 1927 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 1928 DPRINTFN(4, "slot %u depth %d speed %d", slot, depth, speed, 0); 1929 DPRINTFN(4, " port %d rhport %d", port, rhport, 0, 0); 1930 1931 switch (speed) { 1932 case USB_SPEED_LOW: 1933 xspeed = 2; 1934 mps = USB_MAX_IPACKET; 1935 break; 1936 case USB_SPEED_FULL: 1937 xspeed = 1; 1938 mps = 64; 1939 break; 1940 case USB_SPEED_HIGH: 1941 xspeed = 3; 1942 mps = USB_2_MAX_CTRL_PACKET; 1943 break; 1944 case USB_SPEED_SUPER: 1945 xspeed = 4; 1946 mps = USB_3_MAX_CTRL_PACKET; 1947 break; 1948 default: 1949 DPRINTFN(0, "impossible speed: %x", speed, 0, 0, 0); 1950 return USBD_INVAL; 1951 } 1952 1953 xs = &sc->sc_slots[slot]; 1954 xs->xs_idx = slot; 1955 1956 /* allocate contexts */ 1957 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz, 1958 &xs->xs_dc_dma); 1959 if (err) 1960 return err; 1961 memset(KERNADDR(&xs->xs_dc_dma, 0), 0, sc->sc_pgsz); 1962 1963 err = usb_allocmem(&sc->sc_bus, sc->sc_pgsz, sc->sc_pgsz, 1964 &xs->xs_ic_dma); 1965 if (err) 1966 return err; 1967 memset(KERNADDR(&xs->xs_ic_dma, 0), 0, sc->sc_pgsz); 1968 1969 for (dci = 0; dci < 32; dci++) { 1970 //CTASSERT(sizeof(xs->xs_ep[dci]) == sizeof(struct xhci_endpoint)); 1971 memset(&xs->xs_ep[dci], 0, sizeof(xs->xs_ep[dci])); 1972 if (dci == XHCI_DCI_SLOT) 1973 continue; 1974 err = xhci_ring_init(sc, &xs->xs_ep[dci].xe_tr, 1975 XHCI_TRANSFER_RING_TRBS, XHCI_TRB_ALIGN); 1976 if (err) { 1977 DPRINTFN(0, "ring init failure", 0, 0, 0, 0); 1978 return err; 1979 } 1980 } 1981 1982 /* set up initial input control context */ 1983 cp = xhci_slot_get_icv(sc, xs, XHCI_ICI_INPUT_CONTROL); 1984 cp[0] = htole32(0); 1985 cp[1] = htole32(XHCI_INCTX_1_ADD_MASK(XHCI_DCI_EP_CONTROL)| 1986 XHCI_INCTX_1_ADD_MASK(XHCI_DCI_SLOT)); 1987 1988 /* set up input slot context */ 1989 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_SLOT)); 1990 cp[0] = htole32( 1991 XHCI_SCTX_0_CTX_NUM_SET(1) | 1992 XHCI_SCTX_0_SPEED_SET(xspeed) 1993 ); 1994 cp[1] = htole32( 1995 XHCI_SCTX_1_RH_PORT_SET(rhport) 1996 ); 1997 cp[2] = htole32( 1998 XHCI_SCTX_2_IRQ_TARGET_SET(0) 1999 ); 2000 cp[3] = htole32(0); 2001 2002 /* set up input EP0 context */ 2003 cp = xhci_slot_get_icv(sc, xs, xhci_dci_to_ici(XHCI_DCI_EP_CONTROL)); 2004 cp[0] = htole32(0); 2005 cp[1] = htole32( 2006 XHCI_EPCTX_1_MAXP_SIZE_SET(mps) | 2007 XHCI_EPCTX_1_EPTYPE_SET(4) | 2008 XHCI_EPCTX_1_CERR_SET(3) 2009 ); 2010 /* can't use xhci_ep_get_dci() yet? */ 2011 *(uint64_t *)(&cp[2]) = htole64( 2012 xhci_ring_trbp(&xs->xs_ep[XHCI_DCI_EP_CONTROL].xe_tr, 0) | 2013 XHCI_EPCTX_2_DCS_SET(1)); 2014 cp[4] = htole32( 2015 XHCI_EPCTX_4_AVG_TRB_LEN_SET(8) 2016 ); 2017 2018 /* sync input contexts before they are read from memory */ 2019 usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE); 2020 hexdump("input context", xhci_slot_get_icv(sc, xs, 0), 2021 sc->sc_ctxsz * 3); 2022 2023 xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot); 2024 2025 err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, 2026 false); 2027 2028 usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD); 2029 hexdump("output context", xhci_slot_get_dcv(sc, xs, 0), 2030 sc->sc_ctxsz * 2); 2031 2032 return err; 2033 } 2034 2035 /* ----- */ 2036 2037 static void 2038 xhci_noop(usbd_pipe_handle pipe) 2039 { 2040 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2041 } 2042 2043 /* root hub descriptors */ 2044 2045 static const usb_device_descriptor_t xhci_devd = { 2046 USB_DEVICE_DESCRIPTOR_SIZE, 2047 UDESC_DEVICE, /* type */ 2048 {0x00, 0x02}, /* USB version */ 2049 UDCLASS_HUB, /* class */ 2050 UDSUBCLASS_HUB, /* subclass */ 2051 UDPROTO_HSHUBSTT, /* protocol */ 2052 64, /* max packet */ 2053 {0},{0},{0x00,0x01}, /* device id */ 2054 1,2,0, /* string indexes */ 2055 1 /* # of configurations */ 2056 }; 2057 2058 static const usb_device_qualifier_t xhci_odevd = { 2059 USB_DEVICE_DESCRIPTOR_SIZE, 2060 UDESC_DEVICE_QUALIFIER, /* type */ 2061 {0x00, 0x02}, /* USB version */ 2062 UDCLASS_HUB, /* class */ 2063 UDSUBCLASS_HUB, /* subclass */ 2064 UDPROTO_FSHUB, /* protocol */ 2065 64, /* max packet */ 2066 1, /* # of configurations */ 2067 0 2068 }; 2069 2070 static const usb_config_descriptor_t xhci_confd = { 2071 USB_CONFIG_DESCRIPTOR_SIZE, 2072 UDESC_CONFIG, 2073 {USB_CONFIG_DESCRIPTOR_SIZE + 2074 USB_INTERFACE_DESCRIPTOR_SIZE + 2075 USB_ENDPOINT_DESCRIPTOR_SIZE}, 2076 1, 2077 1, 2078 0, 2079 UC_ATTR_MBO | UC_SELF_POWERED, 2080 0 /* max power */ 2081 }; 2082 2083 static const usb_interface_descriptor_t xhci_ifcd = { 2084 USB_INTERFACE_DESCRIPTOR_SIZE, 2085 UDESC_INTERFACE, 2086 0, 2087 0, 2088 1, 2089 UICLASS_HUB, 2090 UISUBCLASS_HUB, 2091 UIPROTO_HSHUBSTT, 2092 0 2093 }; 2094 2095 static const usb_endpoint_descriptor_t xhci_endpd = { 2096 USB_ENDPOINT_DESCRIPTOR_SIZE, 2097 UDESC_ENDPOINT, 2098 UE_DIR_IN | XHCI_INTR_ENDPT, 2099 UE_INTERRUPT, 2100 {8, 0}, /* max packet */ 2101 12 2102 }; 2103 2104 static const usb_hub_descriptor_t xhci_hubd = { 2105 USB_HUB_DESCRIPTOR_SIZE, 2106 UDESC_HUB, 2107 0, 2108 {0,0}, 2109 0, 2110 0, 2111 {""}, 2112 {""}, 2113 }; 2114 2115 /* root hub control */ 2116 2117 static usbd_status 2118 xhci_root_ctrl_transfer(usbd_xfer_handle xfer) 2119 { 2120 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2121 usbd_status err; 2122 2123 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2124 2125 /* Insert last in queue. */ 2126 mutex_enter(&sc->sc_lock); 2127 err = usb_insert_transfer(xfer); 2128 mutex_exit(&sc->sc_lock); 2129 if (err) 2130 return err; 2131 2132 /* Pipe isn't running, start first */ 2133 return (xhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2134 } 2135 2136 static usbd_status 2137 xhci_root_ctrl_start(usbd_xfer_handle xfer) 2138 { 2139 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2140 usb_port_status_t ps; 2141 usb_device_request_t *req; 2142 void *buf = NULL; 2143 usb_hub_descriptor_t hubd; 2144 usbd_status err; 2145 int len, value, index; 2146 int l, totlen = 0; 2147 int port, i; 2148 uint32_t v; 2149 2150 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2151 2152 if (sc->sc_dying) 2153 return USBD_IOERROR; 2154 2155 req = &xfer->request; 2156 2157 value = UGETW(req->wValue); 2158 index = UGETW(req->wIndex); 2159 len = UGETW(req->wLength); 2160 2161 if (len != 0) 2162 buf = KERNADDR(&xfer->dmabuf, 0); 2163 2164 DPRINTFN(12, "rhreq: %04x %04x %04x %04x", 2165 req->bmRequestType | (req->bRequest << 8), value, index, len); 2166 2167 #define C(x,y) ((x) | ((y) << 8)) 2168 switch(C(req->bRequest, req->bmRequestType)) { 2169 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 2170 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 2171 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 2172 /* 2173 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 2174 * for the integrated root hub. 2175 */ 2176 break; 2177 case C(UR_GET_CONFIG, UT_READ_DEVICE): 2178 if (len > 0) { 2179 *(uint8_t *)buf = sc->sc_conf; 2180 totlen = 1; 2181 } 2182 break; 2183 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 2184 DPRINTFN(8, "getdesc: wValue=0x%04x", value, 0, 0, 0); 2185 if (len == 0) 2186 break; 2187 switch(value >> 8) { 2188 case UDESC_DEVICE: 2189 if ((value & 0xff) != 0) { 2190 err = USBD_IOERROR; 2191 goto ret; 2192 } 2193 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 2194 memcpy(buf, &xhci_devd, min(l, sizeof(xhci_devd))); 2195 break; 2196 case UDESC_DEVICE_QUALIFIER: 2197 if ((value & 0xff) != 0) { 2198 } 2199 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 2200 memcpy(buf, &xhci_odevd, min(l, sizeof(xhci_odevd))); 2201 break; 2202 case UDESC_OTHER_SPEED_CONFIGURATION: 2203 case UDESC_CONFIG: 2204 if ((value & 0xff) != 0) { 2205 err = USBD_IOERROR; 2206 goto ret; 2207 } 2208 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); 2209 memcpy(buf, &xhci_confd, min(l, sizeof(xhci_confd))); 2210 ((usb_config_descriptor_t *)buf)->bDescriptorType = 2211 value >> 8; 2212 buf = (char *)buf + l; 2213 len -= l; 2214 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); 2215 totlen += l; 2216 memcpy(buf, &xhci_ifcd, min(l, sizeof(xhci_ifcd))); 2217 buf = (char *)buf + l; 2218 len -= l; 2219 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); 2220 totlen += l; 2221 memcpy(buf, &xhci_endpd, min(l, sizeof(xhci_endpd))); 2222 break; 2223 case UDESC_STRING: 2224 #define sd ((usb_string_descriptor_t *)buf) 2225 switch (value & 0xff) { 2226 case 0: /* Language table */ 2227 totlen = usb_makelangtbl(sd, len); 2228 break; 2229 case 1: /* Vendor */ 2230 totlen = usb_makestrdesc(sd, len, "NetBSD"); 2231 break; 2232 case 2: /* Product */ 2233 totlen = usb_makestrdesc(sd, len, 2234 "xHCI Root Hub"); 2235 break; 2236 } 2237 #undef sd 2238 break; 2239 default: 2240 err = USBD_IOERROR; 2241 goto ret; 2242 } 2243 break; 2244 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 2245 if (len > 0) { 2246 *(uint8_t *)buf = 0; 2247 totlen = 1; 2248 } 2249 break; 2250 case C(UR_GET_STATUS, UT_READ_DEVICE): 2251 if (len > 1) { 2252 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED); 2253 totlen = 2; 2254 } 2255 break; 2256 case C(UR_GET_STATUS, UT_READ_INTERFACE): 2257 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 2258 if (len > 1) { 2259 USETW(((usb_status_t *)buf)->wStatus, 0); 2260 totlen = 2; 2261 } 2262 break; 2263 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 2264 if (value >= USB_MAX_DEVICES) { 2265 err = USBD_IOERROR; 2266 goto ret; 2267 } 2268 //sc->sc_addr = value; 2269 break; 2270 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 2271 if (value != 0 && value != 1) { 2272 err = USBD_IOERROR; 2273 goto ret; 2274 } 2275 sc->sc_conf = value; 2276 break; 2277 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 2278 break; 2279 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 2280 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 2281 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 2282 err = USBD_IOERROR; 2283 goto ret; 2284 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 2285 break; 2286 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 2287 break; 2288 /* Hub requests */ 2289 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 2290 break; 2291 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 2292 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE port=%d feature=%d", 2293 index, value, 0, 0); 2294 if (index < 1 || index > sc->sc_hs_port_count) { 2295 err = USBD_IOERROR; 2296 goto ret; 2297 } 2298 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index); 2299 v = xhci_op_read_4(sc, port); 2300 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0); 2301 v &= ~XHCI_PS_CLEAR; 2302 switch (value) { 2303 case UHF_PORT_ENABLE: 2304 xhci_op_write_4(sc, port, v &~ XHCI_PS_PED); 2305 break; 2306 case UHF_PORT_SUSPEND: 2307 err = USBD_IOERROR; 2308 goto ret; 2309 case UHF_PORT_POWER: 2310 break; 2311 case UHF_PORT_TEST: 2312 case UHF_PORT_INDICATOR: 2313 err = USBD_IOERROR; 2314 goto ret; 2315 case UHF_C_PORT_CONNECTION: 2316 xhci_op_write_4(sc, port, v | XHCI_PS_CSC); 2317 break; 2318 case UHF_C_PORT_ENABLE: 2319 case UHF_C_PORT_SUSPEND: 2320 case UHF_C_PORT_OVER_CURRENT: 2321 err = USBD_IOERROR; 2322 goto ret; 2323 case UHF_C_PORT_RESET: 2324 xhci_op_write_4(sc, port, v | XHCI_PS_PRC); 2325 break; 2326 default: 2327 err = USBD_IOERROR; 2328 goto ret; 2329 } 2330 2331 break; 2332 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 2333 if (len == 0) 2334 break; 2335 if ((value & 0xff) != 0) { 2336 err = USBD_IOERROR; 2337 goto ret; 2338 } 2339 hubd = xhci_hubd; 2340 hubd.bNbrPorts = sc->sc_hs_port_count; 2341 USETW(hubd.wHubCharacteristics, UHD_PWR_NO_SWITCH); 2342 hubd.bPwrOn2PwrGood = 200; 2343 for (i = 0, l = sc->sc_maxports; l > 0; i++, l -= 8) 2344 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */ 2345 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i; 2346 l = min(len, hubd.bDescLength); 2347 totlen = l; 2348 memcpy(buf, &hubd, l); 2349 break; 2350 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 2351 if (len != 4) { 2352 err = USBD_IOERROR; 2353 goto ret; 2354 } 2355 memset(buf, 0, len); /* ? XXX */ 2356 totlen = len; 2357 break; 2358 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 2359 DPRINTFN(8, "get port status i=%d", index, 0, 0, 0); 2360 if (index < 1 || index > sc->sc_maxports) { 2361 err = USBD_IOERROR; 2362 goto ret; 2363 } 2364 if (len != 4) { 2365 err = USBD_IOERROR; 2366 goto ret; 2367 } 2368 v = xhci_op_read_4(sc, XHCI_PORTSC(sc->sc_hs_port_start - 1 + 2369 index)); 2370 DPRINTFN(4, "READ_CLASS_OTHER GET_STATUS PORTSC %d (%d) %08x", 2371 index, sc->sc_hs_port_start - 1 + index, v, 0); 2372 switch (XHCI_PS_SPEED_GET(v)) { 2373 case 1: 2374 i = UPS_FULL_SPEED; 2375 break; 2376 case 2: 2377 i = UPS_LOW_SPEED; 2378 break; 2379 case 3: 2380 i = UPS_HIGH_SPEED; 2381 break; 2382 default: 2383 i = 0; 2384 break; 2385 } 2386 if (v & XHCI_PS_CCS) i |= UPS_CURRENT_CONNECT_STATUS; 2387 if (v & XHCI_PS_PED) i |= UPS_PORT_ENABLED; 2388 if (v & XHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR; 2389 //if (v & XHCI_PS_SUSP) i |= UPS_SUSPEND; 2390 if (v & XHCI_PS_PR) i |= UPS_RESET; 2391 if (v & XHCI_PS_PP) i |= UPS_PORT_POWER; 2392 USETW(ps.wPortStatus, i); 2393 i = 0; 2394 if (v & XHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS; 2395 if (v & XHCI_PS_PEC) i |= UPS_C_PORT_ENABLED; 2396 if (v & XHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR; 2397 if (v & XHCI_PS_PRC) i |= UPS_C_PORT_RESET; 2398 USETW(ps.wPortChange, i); 2399 l = min(len, sizeof ps); 2400 memcpy(buf, &ps, l); 2401 totlen = l; 2402 break; 2403 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 2404 err = USBD_IOERROR; 2405 goto ret; 2406 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 2407 break; 2408 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 2409 if (index < 1 || index > sc->sc_hs_port_count) { 2410 err = USBD_IOERROR; 2411 goto ret; 2412 } 2413 port = XHCI_PORTSC(sc->sc_hs_port_start - 1 + index); 2414 v = xhci_op_read_4(sc, port); 2415 DPRINTFN(4, "portsc=0x%08x", v, 0, 0, 0); 2416 v &= ~XHCI_PS_CLEAR; 2417 switch (value) { 2418 case UHF_PORT_ENABLE: 2419 xhci_op_write_4(sc, port, v | XHCI_PS_PED); 2420 break; 2421 case UHF_PORT_SUSPEND: 2422 /* XXX suspend */ 2423 break; 2424 case UHF_PORT_RESET: 2425 v &= ~ (XHCI_PS_PED | XHCI_PS_PR); 2426 xhci_op_write_4(sc, port, v | XHCI_PS_PR); 2427 /* Wait for reset to complete. */ 2428 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY); 2429 if (sc->sc_dying) { 2430 err = USBD_IOERROR; 2431 goto ret; 2432 } 2433 v = xhci_op_read_4(sc, port); 2434 if (v & XHCI_PS_PR) { 2435 xhci_op_write_4(sc, port, v & ~XHCI_PS_PR); 2436 usb_delay_ms(&sc->sc_bus, 10); 2437 /* XXX */ 2438 } 2439 break; 2440 case UHF_PORT_POWER: 2441 /* XXX power control */ 2442 break; 2443 /* XXX more */ 2444 case UHF_C_PORT_RESET: 2445 xhci_op_write_4(sc, port, v | XHCI_PS_PRC); 2446 break; 2447 default: 2448 err = USBD_IOERROR; 2449 goto ret; 2450 } 2451 break; 2452 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER): 2453 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER): 2454 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER): 2455 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER): 2456 break; 2457 default: 2458 err = USBD_IOERROR; 2459 goto ret; 2460 } 2461 xfer->actlen = totlen; 2462 err = USBD_NORMAL_COMPLETION; 2463 ret: 2464 xfer->status = err; 2465 mutex_enter(&sc->sc_lock); 2466 usb_transfer_complete(xfer); 2467 mutex_exit(&sc->sc_lock); 2468 return USBD_IN_PROGRESS; 2469 } 2470 2471 2472 static void 2473 xhci_root_ctrl_abort(usbd_xfer_handle xfer) 2474 { 2475 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2476 /* Nothing to do, all transfers are synchronous. */ 2477 } 2478 2479 2480 static void 2481 xhci_root_ctrl_close(usbd_pipe_handle pipe) 2482 { 2483 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2484 /* Nothing to do. */ 2485 } 2486 2487 static void 2488 xhci_root_ctrl_done(usbd_xfer_handle xfer) 2489 { 2490 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2491 2492 xfer->hcpriv = NULL; 2493 } 2494 2495 /* root hub interrupt */ 2496 2497 static usbd_status 2498 xhci_root_intr_transfer(usbd_xfer_handle xfer) 2499 { 2500 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2501 usbd_status err; 2502 2503 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2504 2505 /* Insert last in queue. */ 2506 mutex_enter(&sc->sc_lock); 2507 err = usb_insert_transfer(xfer); 2508 mutex_exit(&sc->sc_lock); 2509 if (err) 2510 return err; 2511 2512 /* Pipe isn't running, start first */ 2513 return (xhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2514 } 2515 2516 static usbd_status 2517 xhci_root_intr_start(usbd_xfer_handle xfer) 2518 { 2519 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2520 2521 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2522 2523 if (sc->sc_dying) 2524 return USBD_IOERROR; 2525 2526 mutex_enter(&sc->sc_lock); 2527 sc->sc_intrxfer = xfer; 2528 mutex_exit(&sc->sc_lock); 2529 2530 return USBD_IN_PROGRESS; 2531 } 2532 2533 static void 2534 xhci_root_intr_abort(usbd_xfer_handle xfer) 2535 { 2536 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2537 2538 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2539 2540 KASSERT(mutex_owned(&sc->sc_lock)); 2541 KASSERT(xfer->pipe->intrxfer == xfer); 2542 2543 DPRINTFN(1, "remove", 0, 0, 0, 0); 2544 2545 sc->sc_intrxfer = NULL; 2546 2547 xfer->status = USBD_CANCELLED; 2548 usb_transfer_complete(xfer); 2549 } 2550 2551 static void 2552 xhci_root_intr_close(usbd_pipe_handle pipe) 2553 { 2554 struct xhci_softc * const sc = pipe->device->bus->hci_private; 2555 2556 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2557 2558 KASSERT(mutex_owned(&sc->sc_lock)); 2559 2560 sc->sc_intrxfer = NULL; 2561 } 2562 2563 static void 2564 xhci_root_intr_done(usbd_xfer_handle xfer) 2565 { 2566 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2567 2568 xfer->hcpriv = NULL; 2569 } 2570 2571 /* -------------- */ 2572 /* device control */ 2573 2574 static usbd_status 2575 xhci_device_ctrl_transfer(usbd_xfer_handle xfer) 2576 { 2577 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2578 usbd_status err; 2579 2580 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2581 2582 /* Insert last in queue. */ 2583 mutex_enter(&sc->sc_lock); 2584 err = usb_insert_transfer(xfer); 2585 mutex_exit(&sc->sc_lock); 2586 if (err) 2587 return (err); 2588 2589 /* Pipe isn't running, start first */ 2590 return (xhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2591 } 2592 2593 static usbd_status 2594 xhci_device_ctrl_start(usbd_xfer_handle xfer) 2595 { 2596 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2597 struct xhci_slot * const xs = xfer->pipe->device->hci_private; 2598 const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc); 2599 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr; 2600 struct xhci_xfer * const xx = (void *)xfer; 2601 usb_device_request_t * const req = &xfer->request; 2602 const bool isread = UT_GET_DIR(req->bmRequestType) == UT_READ; 2603 const uint32_t len = UGETW(req->wLength); 2604 usb_dma_t * const dma = &xfer->dmabuf; 2605 uint64_t parameter; 2606 uint32_t status; 2607 uint32_t control; 2608 u_int i; 2609 2610 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2611 DPRINTFN(12, "req: %04x %04x %04x %04x", 2612 req->bmRequestType | (req->bRequest << 8), UGETW(req->wValue), 2613 UGETW(req->wIndex), UGETW(req->wLength)); 2614 2615 /* XXX */ 2616 if (tr->is_halted) { 2617 xhci_reset_endpoint(xfer->pipe); 2618 tr->is_halted = false; 2619 xhci_set_dequeue(xfer->pipe); 2620 } 2621 2622 /* we rely on the bottom bits for extra info */ 2623 KASSERT(((uintptr_t)xfer & 0x3) == 0x0); 2624 2625 KASSERT((xfer->rqflags & URQ_REQUEST) != 0); 2626 2627 i = 0; 2628 2629 /* setup phase */ 2630 memcpy(¶meter, req, sizeof(*req)); 2631 parameter = le64toh(parameter); 2632 status = XHCI_TRB_2_IRQ_SET(0) | XHCI_TRB_2_BYTES_SET(sizeof(*req)); 2633 control = ((len == 0) ? XHCI_TRB_3_TRT_NONE : 2634 (isread ? XHCI_TRB_3_TRT_IN : XHCI_TRB_3_TRT_OUT)) | 2635 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_SETUP_STAGE) | 2636 XHCI_TRB_3_IDT_BIT; 2637 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2638 2639 if (len == 0) 2640 goto no_data; 2641 2642 /* data phase */ 2643 parameter = DMAADDR(dma, 0); 2644 KASSERT(len <= 0x10000); 2645 status = XHCI_TRB_2_IRQ_SET(0) | 2646 XHCI_TRB_2_TDSZ_SET(1) | 2647 XHCI_TRB_2_BYTES_SET(len); 2648 control = (isread ? XHCI_TRB_3_DIR_IN : 0) | 2649 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_DATA_STAGE) | 2650 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT; 2651 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2652 2653 parameter = (uintptr_t)xfer | 0x3; 2654 status = XHCI_TRB_2_IRQ_SET(0); 2655 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) | 2656 XHCI_TRB_3_IOC_BIT; 2657 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2658 2659 no_data: 2660 parameter = 0; 2661 status = XHCI_TRB_2_IRQ_SET(0); 2662 /* the status stage has inverted direction */ 2663 control = ((isread && (len > 0)) ? 0 : XHCI_TRB_3_DIR_IN) | 2664 XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_STATUS_STAGE) | 2665 XHCI_TRB_3_CHAIN_BIT | XHCI_TRB_3_ENT_BIT; 2666 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2667 2668 parameter = (uintptr_t)xfer | 0x0; 2669 status = XHCI_TRB_2_IRQ_SET(0); 2670 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_EVENT_DATA) | 2671 XHCI_TRB_3_IOC_BIT; 2672 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2673 2674 mutex_enter(&tr->xr_lock); 2675 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i); 2676 mutex_exit(&tr->xr_lock); 2677 2678 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci); 2679 2680 if (xfer->timeout && !sc->sc_bus.use_polling) { 2681 callout_reset(&xfer->timeout_handle, mstohz(xfer->timeout), 2682 xhci_timeout, xfer); 2683 } 2684 2685 if (sc->sc_bus.use_polling) { 2686 DPRINTFN(1, "polling", 0, 0, 0, 0); 2687 //xhci_waitintr(sc, xfer); 2688 } 2689 2690 return USBD_IN_PROGRESS; 2691 } 2692 2693 static void 2694 xhci_device_ctrl_done(usbd_xfer_handle xfer) 2695 { 2696 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2697 2698 callout_stop(&xfer->timeout_handle); /* XXX wrong place */ 2699 2700 } 2701 2702 static void 2703 xhci_device_ctrl_abort(usbd_xfer_handle xfer) 2704 { 2705 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2706 } 2707 2708 static void 2709 xhci_device_ctrl_close(usbd_pipe_handle pipe) 2710 { 2711 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2712 } 2713 2714 /* ----------------- */ 2715 /* device isochronus */ 2716 2717 /* ----------- */ 2718 /* device bulk */ 2719 2720 static usbd_status 2721 xhci_device_bulk_transfer(usbd_xfer_handle xfer) 2722 { 2723 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2724 usbd_status err; 2725 2726 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2727 2728 /* Insert last in queue. */ 2729 mutex_enter(&sc->sc_lock); 2730 err = usb_insert_transfer(xfer); 2731 mutex_exit(&sc->sc_lock); 2732 if (err) 2733 return err; 2734 2735 /* 2736 * Pipe isn't running (otherwise err would be USBD_INPROG), 2737 * so start it first. 2738 */ 2739 return (xhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2740 } 2741 2742 static usbd_status 2743 xhci_device_bulk_start(usbd_xfer_handle xfer) 2744 { 2745 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2746 struct xhci_slot * const xs = xfer->pipe->device->hci_private; 2747 const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc); 2748 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr; 2749 struct xhci_xfer * const xx = (void *)xfer; 2750 const uint32_t len = xfer->length; 2751 usb_dma_t * const dma = &xfer->dmabuf; 2752 uint64_t parameter; 2753 uint32_t status; 2754 uint32_t control; 2755 u_int i = 0; 2756 2757 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2758 2759 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0); 2760 2761 if (sc->sc_dying) 2762 return USBD_IOERROR; 2763 2764 KASSERT((xfer->rqflags & URQ_REQUEST) == 0); 2765 2766 parameter = DMAADDR(dma, 0); 2767 /* 2768 * XXX: (dsl) The physical buffer must not cross a 64k boundary. 2769 * If the user supplied buffer crosses such a boundary then 2 2770 * (or more) TRB should be used. 2771 * If multiple TRB are used the td_size field must be set correctly. 2772 * For v1.0 devices (like ivy bridge) this is the number of usb data 2773 * blocks needed to complete the transfer. 2774 * Setting it to 1 in the last TRB causes an extra zero-length 2775 * data block be sent. 2776 * The earlier documentation differs, I don't know how it behaves. 2777 */ 2778 KASSERT(len <= 0x10000); 2779 status = XHCI_TRB_2_IRQ_SET(0) | 2780 XHCI_TRB_2_TDSZ_SET(1) | 2781 XHCI_TRB_2_BYTES_SET(len); 2782 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) | 2783 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT; 2784 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2785 2786 mutex_enter(&tr->xr_lock); 2787 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i); 2788 mutex_exit(&tr->xr_lock); 2789 2790 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci); 2791 2792 if (sc->sc_bus.use_polling) { 2793 DPRINTFN(1, "polling", 0, 0, 0, 0); 2794 //xhci_waitintr(sc, xfer); 2795 } 2796 2797 return USBD_IN_PROGRESS; 2798 } 2799 2800 static void 2801 xhci_device_bulk_done(usbd_xfer_handle xfer) 2802 { 2803 #ifdef USB_DEBUG 2804 struct xhci_slot * const xs = xfer->pipe->device->hci_private; 2805 const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc); 2806 #endif 2807 const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress; 2808 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2809 2810 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2811 2812 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0); 2813 2814 callout_stop(&xfer->timeout_handle); /* XXX wrong place */ 2815 2816 usb_syncmem(&xfer->dmabuf, 0, xfer->length, 2817 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2818 2819 2820 } 2821 2822 static void 2823 xhci_device_bulk_abort(usbd_xfer_handle xfer) 2824 { 2825 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2826 } 2827 2828 static void 2829 xhci_device_bulk_close(usbd_pipe_handle pipe) 2830 { 2831 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2832 } 2833 2834 /* --------------- */ 2835 /* device intrrupt */ 2836 2837 static usbd_status 2838 xhci_device_intr_transfer(usbd_xfer_handle xfer) 2839 { 2840 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2841 usbd_status err; 2842 2843 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2844 2845 /* Insert last in queue. */ 2846 mutex_enter(&sc->sc_lock); 2847 err = usb_insert_transfer(xfer); 2848 mutex_exit(&sc->sc_lock); 2849 if (err) 2850 return err; 2851 2852 /* 2853 * Pipe isn't running (otherwise err would be USBD_INPROG), 2854 * so start it first. 2855 */ 2856 return (xhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2857 } 2858 2859 static usbd_status 2860 xhci_device_intr_start(usbd_xfer_handle xfer) 2861 { 2862 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2863 struct xhci_slot * const xs = xfer->pipe->device->hci_private; 2864 const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc); 2865 struct xhci_ring * const tr = &xs->xs_ep[dci].xe_tr; 2866 struct xhci_xfer * const xx = (void *)xfer; 2867 const uint32_t len = xfer->length; 2868 usb_dma_t * const dma = &xfer->dmabuf; 2869 uint64_t parameter; 2870 uint32_t status; 2871 uint32_t control; 2872 u_int i = 0; 2873 2874 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2875 2876 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0); 2877 2878 if (sc->sc_dying) 2879 return USBD_IOERROR; 2880 2881 KASSERT((xfer->rqflags & URQ_REQUEST) == 0); 2882 2883 parameter = DMAADDR(dma, 0); 2884 KASSERT(len <= 0x10000); 2885 status = XHCI_TRB_2_IRQ_SET(0) | 2886 XHCI_TRB_2_TDSZ_SET(1) | 2887 XHCI_TRB_2_BYTES_SET(len); 2888 control = XHCI_TRB_3_TYPE_SET(XHCI_TRB_TYPE_NORMAL) | 2889 XHCI_TRB_3_ISP_BIT | XHCI_TRB_3_IOC_BIT; 2890 xhci_trb_put(&xx->xx_trb[i++], parameter, status, control); 2891 2892 mutex_enter(&tr->xr_lock); 2893 xhci_ring_put(sc, tr, xfer, xx->xx_trb, i); 2894 mutex_exit(&tr->xr_lock); 2895 2896 xhci_db_write_4(sc, XHCI_DOORBELL(xs->xs_idx), dci); 2897 2898 if (sc->sc_bus.use_polling) { 2899 DPRINTFN(1, "polling", 0, 0, 0, 0); 2900 //xhci_waitintr(sc, xfer); 2901 } 2902 2903 return USBD_IN_PROGRESS; 2904 } 2905 2906 static void 2907 xhci_device_intr_done(usbd_xfer_handle xfer) 2908 { 2909 struct xhci_softc * const sc __diagused = 2910 xfer->pipe->device->bus->hci_private; 2911 #ifdef USB_DEBUG 2912 struct xhci_slot * const xs = xfer->pipe->device->hci_private; 2913 const u_int dci = xhci_ep_get_dci(xfer->pipe->endpoint->edesc); 2914 #endif 2915 const u_int endpt = xfer->pipe->endpoint->edesc->bEndpointAddress; 2916 const bool isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2917 2918 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2919 2920 DPRINTFN(15, "%p slot %u dci %u", xfer, xs->xs_idx, dci, 0); 2921 2922 KASSERT(sc->sc_bus.use_polling || mutex_owned(&sc->sc_lock)); 2923 2924 usb_syncmem(&xfer->dmabuf, 0, xfer->length, 2925 isread ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE); 2926 2927 #if 0 2928 device_printf(sc->sc_dev, ""); 2929 for (size_t i = 0; i < xfer->length; i++) { 2930 printf(" %02x", ((uint8_t const *)xfer->buffer)[i]); 2931 } 2932 printf("\n"); 2933 #endif 2934 2935 if (xfer->pipe->repeat) { 2936 xfer->status = xhci_device_intr_start(xfer); 2937 } else { 2938 callout_stop(&xfer->timeout_handle); /* XXX */ 2939 } 2940 2941 } 2942 2943 static void 2944 xhci_device_intr_abort(usbd_xfer_handle xfer) 2945 { 2946 struct xhci_softc * const sc __diagused = 2947 xfer->pipe->device->bus->hci_private; 2948 2949 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2950 2951 KASSERT(mutex_owned(&sc->sc_lock)); 2952 DPRINTFN(15, "%p", xfer, 0, 0, 0); 2953 KASSERT(xfer->pipe->intrxfer == xfer); 2954 xfer->status = USBD_CANCELLED; 2955 usb_transfer_complete(xfer); 2956 } 2957 2958 static void 2959 xhci_device_intr_close(usbd_pipe_handle pipe) 2960 { 2961 //struct xhci_softc * const sc = pipe->device->bus->hci_private; 2962 2963 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2964 DPRINTFN(15, "%p", pipe, 0, 0, 0); 2965 2966 xhci_unconfigure_endpoint(pipe); 2967 } 2968 2969 /* ------------ */ 2970 2971 static void 2972 xhci_timeout(void *addr) 2973 { 2974 struct xhci_xfer * const xx = addr; 2975 usbd_xfer_handle const xfer = &xx->xx_xfer; 2976 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2977 2978 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2979 2980 if (sc->sc_dying) { 2981 return; 2982 } 2983 2984 usb_init_task(&xx->xx_abort_task, xhci_timeout_task, addr, 2985 USB_TASKQ_MPSAFE); 2986 usb_add_task(xx->xx_xfer.pipe->device, &xx->xx_abort_task, 2987 USB_TASKQ_HC); 2988 } 2989 2990 static void 2991 xhci_timeout_task(void *addr) 2992 { 2993 usbd_xfer_handle const xfer = addr; 2994 struct xhci_softc * const sc = xfer->pipe->device->bus->hci_private; 2995 2996 XHCIHIST_FUNC(); XHCIHIST_CALLED(); 2997 2998 mutex_enter(&sc->sc_lock); 2999 #if 0 3000 xhci_abort_xfer(xfer, USBD_TIMEOUT); 3001 #else 3002 xfer->status = USBD_TIMEOUT; 3003 usb_transfer_complete(xfer); 3004 #endif 3005 mutex_exit(&sc->sc_lock); 3006 } 3007