1 /*- 2 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 3 * Copyright (c) 2004 The NetBSD Foundation, Inc. All rights reserved. 4 * Copyright (c) 2004 Lennart Augustsson. All rights reserved. 5 * Copyright (c) 2004 Charles M. Hannum. 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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* 30 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 31 * 32 * The EHCI 0.96 spec can be found at 33 * http://developer.intel.com/technology/usb/download/ehci-r096.pdf 34 * The EHCI 1.0 spec can be found at 35 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 36 * and the USB 2.0 spec at 37 * http://www.usb.org/developers/docs/usb_20.zip 38 * 39 */ 40 41 /* 42 * TODO: 43 * 1) command failures are not recovered correctly 44 */ 45 46 #include <sys/stdint.h> 47 #include <sys/param.h> 48 #include <sys/queue.h> 49 #include <sys/types.h> 50 #include <sys/systm.h> 51 #include <sys/kernel.h> 52 #include <sys/bus.h> 53 #include <sys/module.h> 54 #include <sys/lock.h> 55 #include <sys/condvar.h> 56 #include <sys/sysctl.h> 57 #include <sys/unistd.h> 58 #include <sys/callout.h> 59 #include <sys/malloc.h> 60 #include <sys/priv.h> 61 62 #include <bus/u4b/usb.h> 63 #include <bus/u4b/usbdi.h> 64 65 #define USB_DEBUG_VAR ehcidebug 66 67 #include <bus/u4b/usb_core.h> 68 #include <bus/u4b/usb_debug.h> 69 #include <bus/u4b/usb_busdma.h> 70 #include <bus/u4b/usb_process.h> 71 #include <bus/u4b/usb_transfer.h> 72 #include <bus/u4b/usb_device.h> 73 #include <bus/u4b/usb_hub.h> 74 #include <bus/u4b/usb_util.h> 75 76 #include <bus/u4b/usb_controller.h> 77 #include <bus/u4b/usb_bus.h> 78 #include <bus/u4b/controller/ehci.h> 79 #include <bus/u4b/controller/ehcireg.h> 80 81 #define EHCI_BUS2SC(bus) \ 82 ((ehci_softc_t *)(((uint8_t *)(bus)) - \ 83 ((uint8_t *)&(((ehci_softc_t *)0)->sc_bus)))) 84 85 #ifdef USB_DEBUG 86 static int ehcidebug = 0; 87 static int ehcinohighspeed = 0; 88 static int ehciiaadbug = 0; 89 static int ehcilostintrbug = 0; 90 91 static SYSCTL_NODE(_hw_usb, OID_AUTO, ehci, CTLFLAG_RW, 0, "USB ehci"); 92 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, debug, CTLFLAG_RW, 93 &ehcidebug, 0, "Debug level"); 94 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, no_hs, CTLFLAG_RW, 95 &ehcinohighspeed, 0, "Disable High Speed USB"); 96 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, iaadbug, CTLFLAG_RW, 97 &ehciiaadbug, 0, "Enable doorbell bug workaround"); 98 SYSCTL_INT(_hw_usb_ehci, OID_AUTO, lostintrbug, CTLFLAG_RW, 99 &ehcilostintrbug, 0, "Enable lost interrupt bug workaround"); 100 101 TUNABLE_INT("hw.usb.ehci.debug", &ehcidebug); 102 TUNABLE_INT("hw.usb.ehci.no_hs", &ehcinohighspeed); 103 TUNABLE_INT("hw.usb.ehci.iaadbug", &ehciiaadbug); 104 TUNABLE_INT("hw.usb.ehci.lostintrbug", &ehcilostintrbug); 105 106 static void ehci_dump_regs(ehci_softc_t *sc); 107 static void ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *sqh); 108 109 #endif 110 111 #define EHCI_INTR_ENDPT 1 112 113 extern struct usb_bus_methods ehci_bus_methods; 114 extern struct usb_pipe_methods ehci_device_bulk_methods; 115 extern struct usb_pipe_methods ehci_device_ctrl_methods; 116 extern struct usb_pipe_methods ehci_device_intr_methods; 117 extern struct usb_pipe_methods ehci_device_isoc_fs_methods; 118 extern struct usb_pipe_methods ehci_device_isoc_hs_methods; 119 120 static void ehci_do_poll(struct usb_bus *); 121 static void ehci_device_done(struct usb_xfer *, usb_error_t); 122 static uint8_t ehci_check_transfer(struct usb_xfer *); 123 static void ehci_timeout(void *); 124 static void ehci_poll_timeout(void *); 125 126 static void ehci_root_intr(ehci_softc_t *sc); 127 128 struct ehci_std_temp { 129 ehci_softc_t *sc; 130 struct usb_page_cache *pc; 131 ehci_qtd_t *td; 132 ehci_qtd_t *td_next; 133 uint32_t average; 134 uint32_t qtd_status; 135 uint32_t len; 136 uint16_t max_frame_size; 137 uint8_t shortpkt; 138 uint8_t auto_data_toggle; 139 uint8_t setup_alt_next; 140 uint8_t last_frame; 141 }; 142 143 void 144 ehci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb) 145 { 146 ehci_softc_t *sc = EHCI_BUS2SC(bus); 147 uint32_t i; 148 149 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg, 150 sizeof(uint32_t) * EHCI_FRAMELIST_COUNT, EHCI_FRAMELIST_ALIGN); 151 152 cb(bus, &sc->sc_hw.terminate_pc, &sc->sc_hw.terminate_pg, 153 sizeof(struct ehci_qh_sub), EHCI_QH_ALIGN); 154 155 cb(bus, &sc->sc_hw.async_start_pc, &sc->sc_hw.async_start_pg, 156 sizeof(ehci_qh_t), EHCI_QH_ALIGN); 157 158 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) { 159 cb(bus, sc->sc_hw.intr_start_pc + i, 160 sc->sc_hw.intr_start_pg + i, 161 sizeof(ehci_qh_t), EHCI_QH_ALIGN); 162 } 163 164 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) { 165 cb(bus, sc->sc_hw.isoc_hs_start_pc + i, 166 sc->sc_hw.isoc_hs_start_pg + i, 167 sizeof(ehci_itd_t), EHCI_ITD_ALIGN); 168 } 169 170 for (i = 0; i != EHCI_VIRTUAL_FRAMELIST_COUNT; i++) { 171 cb(bus, sc->sc_hw.isoc_fs_start_pc + i, 172 sc->sc_hw.isoc_fs_start_pg + i, 173 sizeof(ehci_sitd_t), EHCI_SITD_ALIGN); 174 } 175 } 176 177 usb_error_t 178 ehci_reset(ehci_softc_t *sc) 179 { 180 uint32_t hcr; 181 int i; 182 183 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET); 184 for (i = 0; i < 100; i++) { 185 usb_pause_mtx(NULL, hz / 128); 186 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET; 187 if (!hcr) { 188 if (sc->sc_flags & (EHCI_SCFLG_SETMODE | EHCI_SCFLG_BIGEMMIO)) { 189 /* 190 * Force USBMODE as requested. Controllers 191 * may have multiple operating modes. 192 */ 193 uint32_t usbmode = EOREAD4(sc, EHCI_USBMODE); 194 if (sc->sc_flags & EHCI_SCFLG_SETMODE) { 195 usbmode = (usbmode &~ EHCI_UM_CM) | EHCI_UM_CM_HOST; 196 device_printf(sc->sc_bus.bdev, 197 "set host controller mode\n"); 198 } 199 if (sc->sc_flags & EHCI_SCFLG_BIGEMMIO) { 200 usbmode = (usbmode &~ EHCI_UM_ES) | EHCI_UM_ES_BE; 201 device_printf(sc->sc_bus.bdev, 202 "set big-endian mode\n"); 203 } 204 EOWRITE4(sc, EHCI_USBMODE, usbmode); 205 } 206 return (0); 207 } 208 } 209 device_printf(sc->sc_bus.bdev, "Reset timeout\n"); 210 return (USB_ERR_IOERROR); 211 } 212 213 static usb_error_t 214 ehci_hcreset(ehci_softc_t *sc) 215 { 216 uint32_t hcr; 217 int i; 218 219 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */ 220 for (i = 0; i < 100; i++) { 221 usb_pause_mtx(NULL, hz / 128); 222 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 223 if (hcr) 224 break; 225 } 226 if (!hcr) 227 /* 228 * Fall through and try reset anyway even though 229 * Table 2-9 in the EHCI spec says this will result 230 * in undefined behavior. 231 */ 232 device_printf(sc->sc_bus.bdev, "stop timeout\n"); 233 234 return (ehci_reset(sc)); 235 } 236 237 static int 238 ehci_init_sub(struct ehci_softc *sc) 239 { 240 struct usb_page_search buf_res; 241 uint32_t cparams; 242 uint32_t hcr; 243 uint8_t i; 244 245 cparams = EREAD4(sc, EHCI_HCCPARAMS); 246 247 DPRINTF("cparams=0x%x\n", cparams); 248 249 if (EHCI_HCC_64BIT(cparams)) { 250 DPRINTF("HCC uses 64-bit structures\n"); 251 252 /* MUST clear segment register if 64 bit capable */ 253 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0); 254 } 255 256 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res); 257 EOWRITE4(sc, EHCI_PERIODICLISTBASE, buf_res.physaddr); 258 259 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res); 260 EOWRITE4(sc, EHCI_ASYNCLISTADDR, buf_res.physaddr | EHCI_LINK_QH); 261 262 /* enable interrupts */ 263 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 264 265 /* turn on controller */ 266 EOWRITE4(sc, EHCI_USBCMD, 267 EHCI_CMD_ITC_1 | /* 1 microframes interrupt delay */ 268 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) | 269 EHCI_CMD_ASE | 270 EHCI_CMD_PSE | 271 EHCI_CMD_RS); 272 273 /* Take over port ownership */ 274 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF); 275 276 for (i = 0; i < 100; i++) { 277 usb_pause_mtx(NULL, hz / 128); 278 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 279 if (!hcr) { 280 break; 281 } 282 } 283 if (hcr) { 284 device_printf(sc->sc_bus.bdev, "Run timeout\n"); 285 return (USB_ERR_IOERROR); 286 } 287 return (USB_ERR_NORMAL_COMPLETION); 288 } 289 290 usb_error_t 291 ehci_init(ehci_softc_t *sc) 292 { 293 struct usb_page_search buf_res; 294 uint32_t version; 295 uint32_t sparams; 296 uint16_t i; 297 uint16_t x; 298 uint16_t y; 299 uint16_t bit; 300 usb_error_t err = 0; 301 302 DPRINTF("start\n"); 303 304 usb_callout_init_mtx(&sc->sc_tmo_pcd, &sc->sc_bus.bus_lock, 0); 305 usb_callout_init_mtx(&sc->sc_tmo_poll, &sc->sc_bus.bus_lock, 0); 306 307 sc->sc_offs = EHCI_CAPLENGTH(EREAD4(sc, EHCI_CAPLEN_HCIVERSION)); 308 309 #ifdef USB_DEBUG 310 if (ehciiaadbug) 311 sc->sc_flags |= EHCI_SCFLG_IAADBUG; 312 if (ehcilostintrbug) 313 sc->sc_flags |= EHCI_SCFLG_LOSTINTRBUG; 314 if (ehcidebug > 2) { 315 ehci_dump_regs(sc); 316 } 317 #endif 318 319 version = EHCI_HCIVERSION(EREAD4(sc, EHCI_CAPLEN_HCIVERSION)); 320 device_printf(sc->sc_bus.bdev, "EHCI version %x.%x\n", 321 version >> 8, version & 0xff); 322 323 sparams = EREAD4(sc, EHCI_HCSPARAMS); 324 DPRINTF("sparams=0x%x\n", sparams); 325 326 sc->sc_noport = EHCI_HCS_N_PORTS(sparams); 327 sc->sc_bus.usbrev = USB_REV_2_0; 328 329 /* Reset the controller */ 330 DPRINTF("%s: resetting\n", device_get_nameunit(sc->sc_bus.bdev)); 331 332 err = ehci_hcreset(sc); 333 if (err) { 334 device_printf(sc->sc_bus.bdev, "reset timeout\n"); 335 return (err); 336 } 337 /* 338 * use current frame-list-size selection 0: 1024*4 bytes 1: 512*4 339 * bytes 2: 256*4 bytes 3: unknown 340 */ 341 if (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD)) == 3) { 342 device_printf(sc->sc_bus.bdev, "invalid frame-list-size\n"); 343 return (USB_ERR_IOERROR); 344 } 345 /* set up the bus struct */ 346 sc->sc_bus.methods = &ehci_bus_methods; 347 348 sc->sc_eintrs = EHCI_NORMAL_INTRS; 349 350 if (1) { 351 struct ehci_qh_sub *qh; 352 353 usbd_get_page(&sc->sc_hw.terminate_pc, 0, &buf_res); 354 355 qh = buf_res.buffer; 356 357 sc->sc_terminate_self = htohc32(sc, buf_res.physaddr); 358 359 /* init terminate TD */ 360 qh->qtd_next = 361 htohc32(sc, EHCI_LINK_TERMINATE); 362 qh->qtd_altnext = 363 htohc32(sc, EHCI_LINK_TERMINATE); 364 qh->qtd_status = 365 htohc32(sc, EHCI_QTD_HALTED); 366 } 367 368 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) { 369 ehci_qh_t *qh; 370 371 usbd_get_page(sc->sc_hw.intr_start_pc + i, 0, &buf_res); 372 373 qh = buf_res.buffer; 374 375 /* initialize page cache pointer */ 376 377 qh->page_cache = sc->sc_hw.intr_start_pc + i; 378 379 /* store a pointer to queue head */ 380 381 sc->sc_intr_p_last[i] = qh; 382 383 qh->qh_self = 384 htohc32(sc, buf_res.physaddr) | 385 htohc32(sc, EHCI_LINK_QH); 386 387 qh->qh_endp = 388 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH)); 389 qh->qh_endphub = 390 htohc32(sc, EHCI_QH_SET_MULT(1)); 391 qh->qh_curqtd = 0; 392 393 qh->qh_qtd.qtd_next = 394 htohc32(sc, EHCI_LINK_TERMINATE); 395 qh->qh_qtd.qtd_altnext = 396 htohc32(sc, EHCI_LINK_TERMINATE); 397 qh->qh_qtd.qtd_status = 398 htohc32(sc, EHCI_QTD_HALTED); 399 } 400 401 /* 402 * the QHs are arranged to give poll intervals that are 403 * powers of 2 times 1ms 404 */ 405 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2; 406 while (bit) { 407 x = bit; 408 while (x & bit) { 409 ehci_qh_t *qh_x; 410 ehci_qh_t *qh_y; 411 412 y = (x ^ bit) | (bit / 2); 413 414 qh_x = sc->sc_intr_p_last[x]; 415 qh_y = sc->sc_intr_p_last[y]; 416 417 /* 418 * the next QH has half the poll interval 419 */ 420 qh_x->qh_link = qh_y->qh_self; 421 422 x++; 423 } 424 bit >>= 1; 425 } 426 427 if (1) { 428 ehci_qh_t *qh; 429 430 qh = sc->sc_intr_p_last[0]; 431 432 /* the last (1ms) QH terminates */ 433 qh->qh_link = htohc32(sc, EHCI_LINK_TERMINATE); 434 } 435 for (i = 0; i < EHCI_VIRTUAL_FRAMELIST_COUNT; i++) { 436 ehci_sitd_t *sitd; 437 ehci_itd_t *itd; 438 439 usbd_get_page(sc->sc_hw.isoc_fs_start_pc + i, 0, &buf_res); 440 441 sitd = buf_res.buffer; 442 443 /* initialize page cache pointer */ 444 445 sitd->page_cache = sc->sc_hw.isoc_fs_start_pc + i; 446 447 /* store a pointer to the transfer descriptor */ 448 449 sc->sc_isoc_fs_p_last[i] = sitd; 450 451 /* initialize full speed isochronous */ 452 453 sitd->sitd_self = 454 htohc32(sc, buf_res.physaddr) | 455 htohc32(sc, EHCI_LINK_SITD); 456 457 sitd->sitd_back = 458 htohc32(sc, EHCI_LINK_TERMINATE); 459 460 sitd->sitd_next = 461 sc->sc_intr_p_last[i | (EHCI_VIRTUAL_FRAMELIST_COUNT / 2)]->qh_self; 462 463 464 usbd_get_page(sc->sc_hw.isoc_hs_start_pc + i, 0, &buf_res); 465 466 itd = buf_res.buffer; 467 468 /* initialize page cache pointer */ 469 470 itd->page_cache = sc->sc_hw.isoc_hs_start_pc + i; 471 472 /* store a pointer to the transfer descriptor */ 473 474 sc->sc_isoc_hs_p_last[i] = itd; 475 476 /* initialize high speed isochronous */ 477 478 itd->itd_self = 479 htohc32(sc, buf_res.physaddr) | 480 htohc32(sc, EHCI_LINK_ITD); 481 482 itd->itd_next = 483 sitd->sitd_self; 484 } 485 486 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res); 487 488 if (1) { 489 uint32_t *pframes; 490 491 pframes = buf_res.buffer; 492 493 /* 494 * execution order: 495 * pframes -> high speed isochronous -> 496 * full speed isochronous -> interrupt QH's 497 */ 498 for (i = 0; i < EHCI_FRAMELIST_COUNT; i++) { 499 pframes[i] = sc->sc_isoc_hs_p_last 500 [i & (EHCI_VIRTUAL_FRAMELIST_COUNT - 1)]->itd_self; 501 } 502 } 503 usbd_get_page(&sc->sc_hw.async_start_pc, 0, &buf_res); 504 505 if (1) { 506 507 ehci_qh_t *qh; 508 509 qh = buf_res.buffer; 510 511 /* initialize page cache pointer */ 512 513 qh->page_cache = &sc->sc_hw.async_start_pc; 514 515 /* store a pointer to the queue head */ 516 517 sc->sc_async_p_last = qh; 518 519 /* init dummy QH that starts the async list */ 520 521 qh->qh_self = 522 htohc32(sc, buf_res.physaddr) | 523 htohc32(sc, EHCI_LINK_QH); 524 525 /* fill the QH */ 526 qh->qh_endp = 527 htohc32(sc, EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL); 528 qh->qh_endphub = htohc32(sc, EHCI_QH_SET_MULT(1)); 529 qh->qh_link = qh->qh_self; 530 qh->qh_curqtd = 0; 531 532 /* fill the overlay qTD */ 533 qh->qh_qtd.qtd_next = htohc32(sc, EHCI_LINK_TERMINATE); 534 qh->qh_qtd.qtd_altnext = htohc32(sc, EHCI_LINK_TERMINATE); 535 qh->qh_qtd.qtd_status = htohc32(sc, EHCI_QTD_HALTED); 536 } 537 /* flush all cache into memory */ 538 539 usb_bus_mem_flush_all(&sc->sc_bus, &ehci_iterate_hw_softc); 540 541 #ifdef USB_DEBUG 542 if (ehcidebug) { 543 ehci_dump_sqh(sc, sc->sc_async_p_last); 544 } 545 #endif 546 547 /* finial setup */ 548 err = ehci_init_sub(sc); 549 550 if (!err) { 551 /* catch any lost interrupts */ 552 ehci_do_poll(&sc->sc_bus); 553 } 554 return (err); 555 } 556 557 /* 558 * shut down the controller when the system is going down 559 */ 560 void 561 ehci_detach(ehci_softc_t *sc) 562 { 563 USB_BUS_LOCK(&sc->sc_bus); 564 565 usb_callout_stop(&sc->sc_tmo_pcd); 566 usb_callout_stop(&sc->sc_tmo_poll); 567 568 EOWRITE4(sc, EHCI_USBINTR, 0); 569 USB_BUS_UNLOCK(&sc->sc_bus); 570 571 if (ehci_hcreset(sc)) { 572 DPRINTF("reset failed!\n"); 573 } 574 575 /* XXX let stray task complete */ 576 usb_pause_mtx(NULL, hz / 20); 577 578 usb_callout_drain(&sc->sc_tmo_pcd); 579 usb_callout_drain(&sc->sc_tmo_poll); 580 } 581 582 static void 583 ehci_suspend(ehci_softc_t *sc) 584 { 585 DPRINTF("stopping the HC\n"); 586 587 /* reset HC */ 588 ehci_hcreset(sc); 589 } 590 591 static void 592 ehci_resume(ehci_softc_t *sc) 593 { 594 /* reset HC */ 595 ehci_hcreset(sc); 596 597 /* setup HC */ 598 ehci_init_sub(sc); 599 600 /* catch any lost interrupts */ 601 ehci_do_poll(&sc->sc_bus); 602 } 603 604 #ifdef USB_DEBUG 605 static void 606 ehci_dump_regs(ehci_softc_t *sc) 607 { 608 uint32_t i; 609 610 i = EOREAD4(sc, EHCI_USBCMD); 611 kprintf("cmd=0x%08x\n", i); 612 613 if (i & EHCI_CMD_ITC_1) 614 kprintf(" EHCI_CMD_ITC_1\n"); 615 if (i & EHCI_CMD_ITC_2) 616 kprintf(" EHCI_CMD_ITC_2\n"); 617 if (i & EHCI_CMD_ITC_4) 618 kprintf(" EHCI_CMD_ITC_4\n"); 619 if (i & EHCI_CMD_ITC_8) 620 kprintf(" EHCI_CMD_ITC_8\n"); 621 if (i & EHCI_CMD_ITC_16) 622 kprintf(" EHCI_CMD_ITC_16\n"); 623 if (i & EHCI_CMD_ITC_32) 624 kprintf(" EHCI_CMD_ITC_32\n"); 625 if (i & EHCI_CMD_ITC_64) 626 kprintf(" EHCI_CMD_ITC_64\n"); 627 if (i & EHCI_CMD_ASPME) 628 kprintf(" EHCI_CMD_ASPME\n"); 629 if (i & EHCI_CMD_ASPMC) 630 kprintf(" EHCI_CMD_ASPMC\n"); 631 if (i & EHCI_CMD_LHCR) 632 kprintf(" EHCI_CMD_LHCR\n"); 633 if (i & EHCI_CMD_IAAD) 634 kprintf(" EHCI_CMD_IAAD\n"); 635 if (i & EHCI_CMD_ASE) 636 kprintf(" EHCI_CMD_ASE\n"); 637 if (i & EHCI_CMD_PSE) 638 kprintf(" EHCI_CMD_PSE\n"); 639 if (i & EHCI_CMD_FLS_M) 640 kprintf(" EHCI_CMD_FLS_M\n"); 641 if (i & EHCI_CMD_HCRESET) 642 kprintf(" EHCI_CMD_HCRESET\n"); 643 if (i & EHCI_CMD_RS) 644 kprintf(" EHCI_CMD_RS\n"); 645 646 i = EOREAD4(sc, EHCI_USBSTS); 647 648 kprintf("sts=0x%08x\n", i); 649 650 if (i & EHCI_STS_ASS) 651 kprintf(" EHCI_STS_ASS\n"); 652 if (i & EHCI_STS_PSS) 653 kprintf(" EHCI_STS_PSS\n"); 654 if (i & EHCI_STS_REC) 655 kprintf(" EHCI_STS_REC\n"); 656 if (i & EHCI_STS_HCH) 657 kprintf(" EHCI_STS_HCH\n"); 658 if (i & EHCI_STS_IAA) 659 kprintf(" EHCI_STS_IAA\n"); 660 if (i & EHCI_STS_HSE) 661 kprintf(" EHCI_STS_HSE\n"); 662 if (i & EHCI_STS_FLR) 663 kprintf(" EHCI_STS_FLR\n"); 664 if (i & EHCI_STS_PCD) 665 kprintf(" EHCI_STS_PCD\n"); 666 if (i & EHCI_STS_ERRINT) 667 kprintf(" EHCI_STS_ERRINT\n"); 668 if (i & EHCI_STS_INT) 669 kprintf(" EHCI_STS_INT\n"); 670 671 kprintf("ien=0x%08x\n", 672 EOREAD4(sc, EHCI_USBINTR)); 673 kprintf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n", 674 EOREAD4(sc, EHCI_FRINDEX), 675 EOREAD4(sc, EHCI_CTRLDSSEGMENT), 676 EOREAD4(sc, EHCI_PERIODICLISTBASE), 677 EOREAD4(sc, EHCI_ASYNCLISTADDR)); 678 for (i = 1; i <= sc->sc_noport; i++) { 679 kprintf("port %d status=0x%08x\n", i, 680 EOREAD4(sc, EHCI_PORTSC(i))); 681 } 682 } 683 684 static void 685 ehci_dump_link(ehci_softc_t *sc, uint32_t link, int type) 686 { 687 link = hc32toh(sc, link); 688 kprintf("0x%08x", link); 689 if (link & EHCI_LINK_TERMINATE) 690 kprintf("<T>"); 691 else { 692 kprintf("<"); 693 if (type) { 694 switch (EHCI_LINK_TYPE(link)) { 695 case EHCI_LINK_ITD: 696 kprintf("ITD"); 697 break; 698 case EHCI_LINK_QH: 699 kprintf("QH"); 700 break; 701 case EHCI_LINK_SITD: 702 kprintf("SITD"); 703 break; 704 case EHCI_LINK_FSTN: 705 kprintf("FSTN"); 706 break; 707 } 708 } 709 kprintf(">"); 710 } 711 } 712 713 static void 714 ehci_dump_qtd(ehci_softc_t *sc, ehci_qtd_t *qtd) 715 { 716 uint32_t s; 717 718 kprintf(" next="); 719 ehci_dump_link(sc, qtd->qtd_next, 0); 720 kprintf(" altnext="); 721 ehci_dump_link(sc, qtd->qtd_altnext, 0); 722 kprintf("\n"); 723 s = hc32toh(sc, qtd->qtd_status); 724 kprintf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n", 725 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s), 726 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s)); 727 kprintf(" cerr=%d pid=%d stat=%s%s%s%s%s%s%s%s\n", 728 EHCI_QTD_GET_CERR(s), EHCI_QTD_GET_PID(s), 729 (s & EHCI_QTD_ACTIVE) ? "ACTIVE" : "NOT_ACTIVE", 730 (s & EHCI_QTD_HALTED) ? "-HALTED" : "", 731 (s & EHCI_QTD_BUFERR) ? "-BUFERR" : "", 732 (s & EHCI_QTD_BABBLE) ? "-BABBLE" : "", 733 (s & EHCI_QTD_XACTERR) ? "-XACTERR" : "", 734 (s & EHCI_QTD_MISSEDMICRO) ? "-MISSED" : "", 735 (s & EHCI_QTD_SPLITXSTATE) ? "-SPLIT" : "", 736 (s & EHCI_QTD_PINGSTATE) ? "-PING" : ""); 737 738 for (s = 0; s < 5; s++) { 739 kprintf(" buffer[%d]=0x%08x\n", s, 740 hc32toh(sc, qtd->qtd_buffer[s])); 741 } 742 for (s = 0; s < 5; s++) { 743 kprintf(" buffer_hi[%d]=0x%08x\n", s, 744 hc32toh(sc, qtd->qtd_buffer_hi[s])); 745 } 746 } 747 748 static uint8_t 749 ehci_dump_sqtd(ehci_softc_t *sc, ehci_qtd_t *sqtd) 750 { 751 uint8_t temp; 752 753 usb_pc_cpu_invalidate(sqtd->page_cache); 754 kprintf("QTD(%p) at 0x%08x:\n", sqtd, hc32toh(sc, sqtd->qtd_self)); 755 ehci_dump_qtd(sc, sqtd); 756 temp = (sqtd->qtd_next & htohc32(sc, EHCI_LINK_TERMINATE)) ? 1 : 0; 757 return (temp); 758 } 759 760 static void 761 ehci_dump_sqtds(ehci_softc_t *sc, ehci_qtd_t *sqtd) 762 { 763 uint16_t i; 764 uint8_t stop; 765 766 stop = 0; 767 for (i = 0; sqtd && (i < 20) && !stop; sqtd = sqtd->obj_next, i++) { 768 stop = ehci_dump_sqtd(sc, sqtd); 769 } 770 if (sqtd) { 771 kprintf("dump aborted, too many TDs\n"); 772 } 773 } 774 775 static void 776 ehci_dump_sqh(ehci_softc_t *sc, ehci_qh_t *qh) 777 { 778 uint32_t endp; 779 uint32_t endphub; 780 781 usb_pc_cpu_invalidate(qh->page_cache); 782 kprintf("QH(%p) at 0x%08x:\n", qh, hc32toh(sc, qh->qh_self) & ~0x1F); 783 kprintf(" link="); 784 ehci_dump_link(sc, qh->qh_link, 1); 785 kprintf("\n"); 786 endp = hc32toh(sc, qh->qh_endp); 787 kprintf(" endp=0x%08x\n", endp); 788 kprintf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n", 789 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp), 790 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp), 791 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp)); 792 kprintf(" mpl=0x%x ctl=%d nrl=%d\n", 793 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp), 794 EHCI_QH_GET_NRL(endp)); 795 endphub = hc32toh(sc, qh->qh_endphub); 796 kprintf(" endphub=0x%08x\n", endphub); 797 kprintf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n", 798 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 799 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub), 800 EHCI_QH_GET_MULT(endphub)); 801 kprintf(" curqtd="); 802 ehci_dump_link(sc, qh->qh_curqtd, 0); 803 kprintf("\n"); 804 kprintf("Overlay qTD:\n"); 805 ehci_dump_qtd(sc, (void *)&qh->qh_qtd); 806 } 807 808 static void 809 ehci_dump_sitd(ehci_softc_t *sc, ehci_sitd_t *sitd) 810 { 811 usb_pc_cpu_invalidate(sitd->page_cache); 812 kprintf("SITD(%p) at 0x%08x\n", sitd, hc32toh(sc, sitd->sitd_self) & ~0x1F); 813 kprintf(" next=0x%08x\n", hc32toh(sc, sitd->sitd_next)); 814 kprintf(" portaddr=0x%08x dir=%s addr=%d endpt=0x%x port=0x%x huba=0x%x\n", 815 hc32toh(sc, sitd->sitd_portaddr), 816 (sitd->sitd_portaddr & htohc32(sc, EHCI_SITD_SET_DIR_IN)) 817 ? "in" : "out", 818 EHCI_SITD_GET_ADDR(hc32toh(sc, sitd->sitd_portaddr)), 819 EHCI_SITD_GET_ENDPT(hc32toh(sc, sitd->sitd_portaddr)), 820 EHCI_SITD_GET_PORT(hc32toh(sc, sitd->sitd_portaddr)), 821 EHCI_SITD_GET_HUBA(hc32toh(sc, sitd->sitd_portaddr))); 822 kprintf(" mask=0x%08x\n", hc32toh(sc, sitd->sitd_mask)); 823 kprintf(" status=0x%08x <%s> len=0x%x\n", hc32toh(sc, sitd->sitd_status), 824 (sitd->sitd_status & htohc32(sc, EHCI_SITD_ACTIVE)) ? "ACTIVE" : "", 825 EHCI_SITD_GET_LEN(hc32toh(sc, sitd->sitd_status))); 826 kprintf(" back=0x%08x, bp=0x%08x,0x%08x,0x%08x,0x%08x\n", 827 hc32toh(sc, sitd->sitd_back), 828 hc32toh(sc, sitd->sitd_bp[0]), 829 hc32toh(sc, sitd->sitd_bp[1]), 830 hc32toh(sc, sitd->sitd_bp_hi[0]), 831 hc32toh(sc, sitd->sitd_bp_hi[1])); 832 } 833 834 static void 835 ehci_dump_itd(ehci_softc_t *sc, ehci_itd_t *itd) 836 { 837 usb_pc_cpu_invalidate(itd->page_cache); 838 kprintf("ITD(%p) at 0x%08x\n", itd, hc32toh(sc, itd->itd_self) & ~0x1F); 839 kprintf(" next=0x%08x\n", hc32toh(sc, itd->itd_next)); 840 kprintf(" status[0]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[0]), 841 (itd->itd_status[0] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 842 kprintf(" status[1]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[1]), 843 (itd->itd_status[1] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 844 kprintf(" status[2]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[2]), 845 (itd->itd_status[2] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 846 kprintf(" status[3]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[3]), 847 (itd->itd_status[3] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 848 kprintf(" status[4]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[4]), 849 (itd->itd_status[4] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 850 kprintf(" status[5]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[5]), 851 (itd->itd_status[5] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 852 kprintf(" status[6]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[6]), 853 (itd->itd_status[6] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 854 kprintf(" status[7]=0x%08x; <%s>\n", hc32toh(sc, itd->itd_status[7]), 855 (itd->itd_status[7] & htohc32(sc, EHCI_ITD_ACTIVE)) ? "ACTIVE" : ""); 856 kprintf(" bp[0]=0x%08x\n", hc32toh(sc, itd->itd_bp[0])); 857 kprintf(" addr=0x%02x; endpt=0x%01x\n", 858 EHCI_ITD_GET_ADDR(hc32toh(sc, itd->itd_bp[0])), 859 EHCI_ITD_GET_ENDPT(hc32toh(sc, itd->itd_bp[0]))); 860 kprintf(" bp[1]=0x%08x\n", hc32toh(sc, itd->itd_bp[1])); 861 kprintf(" dir=%s; mpl=0x%02x\n", 862 (hc32toh(sc, itd->itd_bp[1]) & EHCI_ITD_SET_DIR_IN) ? "in" : "out", 863 EHCI_ITD_GET_MPL(hc32toh(sc, itd->itd_bp[1]))); 864 kprintf(" bp[2..6]=0x%08x,0x%08x,0x%08x,0x%08x,0x%08x\n", 865 hc32toh(sc, itd->itd_bp[2]), 866 hc32toh(sc, itd->itd_bp[3]), 867 hc32toh(sc, itd->itd_bp[4]), 868 hc32toh(sc, itd->itd_bp[5]), 869 hc32toh(sc, itd->itd_bp[6])); 870 kprintf(" bp_hi=0x%08x,0x%08x,0x%08x,0x%08x,\n" 871 " 0x%08x,0x%08x,0x%08x\n", 872 hc32toh(sc, itd->itd_bp_hi[0]), 873 hc32toh(sc, itd->itd_bp_hi[1]), 874 hc32toh(sc, itd->itd_bp_hi[2]), 875 hc32toh(sc, itd->itd_bp_hi[3]), 876 hc32toh(sc, itd->itd_bp_hi[4]), 877 hc32toh(sc, itd->itd_bp_hi[5]), 878 hc32toh(sc, itd->itd_bp_hi[6])); 879 } 880 881 static void 882 ehci_dump_isoc(ehci_softc_t *sc) 883 { 884 ehci_itd_t *itd; 885 ehci_sitd_t *sitd; 886 uint16_t max = 1000; 887 uint16_t pos; 888 889 pos = (EOREAD4(sc, EHCI_FRINDEX) / 8) & 890 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 891 892 kprintf("%s: isochronous dump from frame 0x%03x:\n", 893 __func__, pos); 894 895 itd = sc->sc_isoc_hs_p_last[pos]; 896 sitd = sc->sc_isoc_fs_p_last[pos]; 897 898 while (itd && max && max--) { 899 ehci_dump_itd(sc, itd); 900 itd = itd->prev; 901 } 902 903 while (sitd && max && max--) { 904 ehci_dump_sitd(sc, sitd); 905 sitd = sitd->prev; 906 } 907 } 908 909 #endif 910 911 static void 912 ehci_transfer_intr_enqueue(struct usb_xfer *xfer) 913 { 914 /* check for early completion */ 915 if (ehci_check_transfer(xfer)) { 916 return; 917 } 918 /* put transfer on interrupt queue */ 919 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 920 921 /* start timeout, if any */ 922 if (xfer->timeout != 0) { 923 usbd_transfer_timeout_ms(xfer, &ehci_timeout, xfer->timeout); 924 } 925 } 926 927 #define EHCI_APPEND_FS_TD(std,last) (last) = _ehci_append_fs_td(std,last) 928 static ehci_sitd_t * 929 _ehci_append_fs_td(ehci_sitd_t *std, ehci_sitd_t *last) 930 { 931 DPRINTFN(11, "%p to %p\n", std, last); 932 933 /* (sc->sc_bus.mtx) must be locked */ 934 935 std->next = last->next; 936 std->sitd_next = last->sitd_next; 937 938 std->prev = last; 939 940 usb_pc_cpu_flush(std->page_cache); 941 942 /* 943 * the last->next->prev is never followed: std->next->prev = std; 944 */ 945 last->next = std; 946 last->sitd_next = std->sitd_self; 947 948 usb_pc_cpu_flush(last->page_cache); 949 950 return (std); 951 } 952 953 #define EHCI_APPEND_HS_TD(std,last) (last) = _ehci_append_hs_td(std,last) 954 static ehci_itd_t * 955 _ehci_append_hs_td(ehci_itd_t *std, ehci_itd_t *last) 956 { 957 DPRINTFN(11, "%p to %p\n", std, last); 958 959 /* (sc->sc_bus.mtx) must be locked */ 960 961 std->next = last->next; 962 std->itd_next = last->itd_next; 963 964 std->prev = last; 965 966 usb_pc_cpu_flush(std->page_cache); 967 968 /* 969 * the last->next->prev is never followed: std->next->prev = std; 970 */ 971 last->next = std; 972 last->itd_next = std->itd_self; 973 974 usb_pc_cpu_flush(last->page_cache); 975 976 return (std); 977 } 978 979 #define EHCI_APPEND_QH(sqh,last) (last) = _ehci_append_qh(sqh,last) 980 static ehci_qh_t * 981 _ehci_append_qh(ehci_qh_t *sqh, ehci_qh_t *last) 982 { 983 DPRINTFN(11, "%p to %p\n", sqh, last); 984 985 if (sqh->prev != NULL) { 986 /* should not happen */ 987 DPRINTFN(0, "QH already linked!\n"); 988 return (last); 989 } 990 /* (sc->sc_bus.mtx) must be locked */ 991 992 sqh->next = last->next; 993 sqh->qh_link = last->qh_link; 994 995 sqh->prev = last; 996 997 usb_pc_cpu_flush(sqh->page_cache); 998 999 /* 1000 * the last->next->prev is never followed: sqh->next->prev = sqh; 1001 */ 1002 1003 last->next = sqh; 1004 last->qh_link = sqh->qh_self; 1005 1006 usb_pc_cpu_flush(last->page_cache); 1007 1008 return (sqh); 1009 } 1010 1011 #define EHCI_REMOVE_FS_TD(std,last) (last) = _ehci_remove_fs_td(std,last) 1012 static ehci_sitd_t * 1013 _ehci_remove_fs_td(ehci_sitd_t *std, ehci_sitd_t *last) 1014 { 1015 DPRINTFN(11, "%p from %p\n", std, last); 1016 1017 /* (sc->sc_bus.mtx) must be locked */ 1018 1019 std->prev->next = std->next; 1020 std->prev->sitd_next = std->sitd_next; 1021 1022 usb_pc_cpu_flush(std->prev->page_cache); 1023 1024 if (std->next) { 1025 std->next->prev = std->prev; 1026 usb_pc_cpu_flush(std->next->page_cache); 1027 } 1028 return ((last == std) ? std->prev : last); 1029 } 1030 1031 #define EHCI_REMOVE_HS_TD(std,last) (last) = _ehci_remove_hs_td(std,last) 1032 static ehci_itd_t * 1033 _ehci_remove_hs_td(ehci_itd_t *std, ehci_itd_t *last) 1034 { 1035 DPRINTFN(11, "%p from %p\n", std, last); 1036 1037 /* (sc->sc_bus.mtx) must be locked */ 1038 1039 std->prev->next = std->next; 1040 std->prev->itd_next = std->itd_next; 1041 1042 usb_pc_cpu_flush(std->prev->page_cache); 1043 1044 if (std->next) { 1045 std->next->prev = std->prev; 1046 usb_pc_cpu_flush(std->next->page_cache); 1047 } 1048 return ((last == std) ? std->prev : last); 1049 } 1050 1051 #define EHCI_REMOVE_QH(sqh,last) (last) = _ehci_remove_qh(sqh,last) 1052 static ehci_qh_t * 1053 _ehci_remove_qh(ehci_qh_t *sqh, ehci_qh_t *last) 1054 { 1055 DPRINTFN(11, "%p from %p\n", sqh, last); 1056 1057 /* (sc->sc_bus.mtx) must be locked */ 1058 1059 /* only remove if not removed from a queue */ 1060 if (sqh->prev) { 1061 1062 sqh->prev->next = sqh->next; 1063 sqh->prev->qh_link = sqh->qh_link; 1064 1065 usb_pc_cpu_flush(sqh->prev->page_cache); 1066 1067 if (sqh->next) { 1068 sqh->next->prev = sqh->prev; 1069 usb_pc_cpu_flush(sqh->next->page_cache); 1070 } 1071 last = ((last == sqh) ? sqh->prev : last); 1072 1073 sqh->prev = 0; 1074 1075 usb_pc_cpu_flush(sqh->page_cache); 1076 } 1077 return (last); 1078 } 1079 1080 static void 1081 ehci_data_toggle_update(struct usb_xfer *xfer, uint16_t actlen, uint16_t xlen) 1082 { 1083 uint16_t rem; 1084 uint8_t dt; 1085 1086 /* count number of full packets */ 1087 dt = (actlen / xfer->max_packet_size) & 1; 1088 1089 /* compute remainder */ 1090 rem = actlen % xfer->max_packet_size; 1091 1092 if (rem > 0) 1093 dt ^= 1; /* short packet at the end */ 1094 else if (actlen != xlen) 1095 dt ^= 1; /* zero length packet at the end */ 1096 else if (xlen == 0) 1097 dt ^= 1; /* zero length transfer */ 1098 1099 xfer->endpoint->toggle_next ^= dt; 1100 } 1101 1102 static usb_error_t 1103 ehci_non_isoc_done_sub(struct usb_xfer *xfer) 1104 { 1105 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 1106 ehci_qtd_t *td; 1107 ehci_qtd_t *td_alt_next; 1108 uint32_t status; 1109 uint16_t len; 1110 1111 td = xfer->td_transfer_cache; 1112 td_alt_next = td->alt_next; 1113 1114 if (xfer->aframes != xfer->nframes) { 1115 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0); 1116 } 1117 while (1) { 1118 1119 usb_pc_cpu_invalidate(td->page_cache); 1120 status = hc32toh(sc, td->qtd_status); 1121 1122 len = EHCI_QTD_GET_BYTES(status); 1123 1124 /* 1125 * Verify the status length and 1126 * add the length to "frlengths[]": 1127 */ 1128 if (len > td->len) { 1129 /* should not happen */ 1130 DPRINTF("Invalid status length, " 1131 "0x%04x/0x%04x bytes\n", len, td->len); 1132 status |= EHCI_QTD_HALTED; 1133 } else if (xfer->aframes != xfer->nframes) { 1134 xfer->frlengths[xfer->aframes] += td->len - len; 1135 /* manually update data toggle */ 1136 ehci_data_toggle_update(xfer, td->len - len, td->len); 1137 } 1138 1139 /* Check for last transfer */ 1140 if (((void *)td) == xfer->td_transfer_last) { 1141 td = NULL; 1142 break; 1143 } 1144 /* Check for transfer error */ 1145 if (status & EHCI_QTD_HALTED) { 1146 /* the transfer is finished */ 1147 td = NULL; 1148 break; 1149 } 1150 /* Check for short transfer */ 1151 if (len > 0) { 1152 if (xfer->flags_int.short_frames_ok) { 1153 /* follow alt next */ 1154 td = td->alt_next; 1155 } else { 1156 /* the transfer is finished */ 1157 td = NULL; 1158 } 1159 break; 1160 } 1161 td = td->obj_next; 1162 1163 if (td->alt_next != td_alt_next) { 1164 /* this USB frame is complete */ 1165 break; 1166 } 1167 } 1168 1169 /* update transfer cache */ 1170 1171 xfer->td_transfer_cache = td; 1172 1173 #ifdef USB_DEBUG 1174 if (status & EHCI_QTD_STATERRS) { 1175 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x" 1176 "status=%s%s%s%s%s%s%s%s\n", 1177 xfer->address, xfer->endpointno, xfer->aframes, 1178 (status & EHCI_QTD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]", 1179 (status & EHCI_QTD_HALTED) ? "[HALTED]" : "", 1180 (status & EHCI_QTD_BUFERR) ? "[BUFERR]" : "", 1181 (status & EHCI_QTD_BABBLE) ? "[BABBLE]" : "", 1182 (status & EHCI_QTD_XACTERR) ? "[XACTERR]" : "", 1183 (status & EHCI_QTD_MISSEDMICRO) ? "[MISSED]" : "", 1184 (status & EHCI_QTD_SPLITXSTATE) ? "[SPLIT]" : "", 1185 (status & EHCI_QTD_PINGSTATE) ? "[PING]" : ""); 1186 } 1187 #endif 1188 1189 return ((status & EHCI_QTD_HALTED) ? 1190 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION); 1191 } 1192 1193 static void 1194 ehci_non_isoc_done(struct usb_xfer *xfer) 1195 { 1196 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 1197 ehci_qh_t *qh; 1198 uint32_t status; 1199 usb_error_t err = 0; 1200 1201 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 1202 xfer, xfer->endpoint); 1203 1204 #ifdef USB_DEBUG 1205 if (ehcidebug > 10) { 1206 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 1207 1208 ehci_dump_sqtds(sc, xfer->td_transfer_first); 1209 } 1210 #endif 1211 1212 /* extract data toggle directly from the QH's overlay area */ 1213 1214 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1215 1216 usb_pc_cpu_invalidate(qh->page_cache); 1217 1218 status = hc32toh(sc, qh->qh_qtd.qtd_status); 1219 1220 /* reset scanner */ 1221 1222 xfer->td_transfer_cache = xfer->td_transfer_first; 1223 1224 if (xfer->flags_int.control_xfr) { 1225 1226 if (xfer->flags_int.control_hdr) { 1227 1228 err = ehci_non_isoc_done_sub(xfer); 1229 } 1230 xfer->aframes = 1; 1231 1232 if (xfer->td_transfer_cache == NULL) { 1233 goto done; 1234 } 1235 } 1236 while (xfer->aframes != xfer->nframes) { 1237 1238 err = ehci_non_isoc_done_sub(xfer); 1239 xfer->aframes++; 1240 1241 if (xfer->td_transfer_cache == NULL) { 1242 goto done; 1243 } 1244 } 1245 1246 if (xfer->flags_int.control_xfr && 1247 !xfer->flags_int.control_act) { 1248 1249 err = ehci_non_isoc_done_sub(xfer); 1250 } 1251 done: 1252 ehci_device_done(xfer, err); 1253 } 1254 1255 /*------------------------------------------------------------------------* 1256 * ehci_check_transfer 1257 * 1258 * Return values: 1259 * 0: USB transfer is not finished 1260 * Else: USB transfer is finished 1261 *------------------------------------------------------------------------*/ 1262 static uint8_t 1263 ehci_check_transfer(struct usb_xfer *xfer) 1264 { 1265 struct usb_pipe_methods *methods = xfer->endpoint->methods; 1266 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 1267 1268 uint32_t status; 1269 1270 DPRINTFN(13, "xfer=%p checking transfer\n", xfer); 1271 1272 if (methods == &ehci_device_isoc_fs_methods) { 1273 ehci_sitd_t *td; 1274 1275 /* isochronous full speed transfer */ 1276 1277 td = xfer->td_transfer_last; 1278 usb_pc_cpu_invalidate(td->page_cache); 1279 status = hc32toh(sc, td->sitd_status); 1280 1281 /* also check if first is complete */ 1282 1283 td = xfer->td_transfer_first; 1284 usb_pc_cpu_invalidate(td->page_cache); 1285 status |= hc32toh(sc, td->sitd_status); 1286 1287 if (!(status & EHCI_SITD_ACTIVE)) { 1288 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION); 1289 goto transferred; 1290 } 1291 } else if (methods == &ehci_device_isoc_hs_methods) { 1292 ehci_itd_t *td; 1293 1294 /* isochronous high speed transfer */ 1295 1296 /* check last transfer */ 1297 td = xfer->td_transfer_last; 1298 usb_pc_cpu_invalidate(td->page_cache); 1299 status = td->itd_status[0]; 1300 status |= td->itd_status[1]; 1301 status |= td->itd_status[2]; 1302 status |= td->itd_status[3]; 1303 status |= td->itd_status[4]; 1304 status |= td->itd_status[5]; 1305 status |= td->itd_status[6]; 1306 status |= td->itd_status[7]; 1307 1308 /* also check first transfer */ 1309 td = xfer->td_transfer_first; 1310 usb_pc_cpu_invalidate(td->page_cache); 1311 status |= td->itd_status[0]; 1312 status |= td->itd_status[1]; 1313 status |= td->itd_status[2]; 1314 status |= td->itd_status[3]; 1315 status |= td->itd_status[4]; 1316 status |= td->itd_status[5]; 1317 status |= td->itd_status[6]; 1318 status |= td->itd_status[7]; 1319 1320 /* if no transactions are active we continue */ 1321 if (!(status & htohc32(sc, EHCI_ITD_ACTIVE))) { 1322 ehci_device_done(xfer, USB_ERR_NORMAL_COMPLETION); 1323 goto transferred; 1324 } 1325 } else { 1326 ehci_qtd_t *td; 1327 ehci_qh_t *qh; 1328 1329 /* non-isochronous transfer */ 1330 1331 /* 1332 * check whether there is an error somewhere in the middle, 1333 * or whether there was a short packet (SPD and not ACTIVE) 1334 */ 1335 td = xfer->td_transfer_cache; 1336 1337 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1338 1339 usb_pc_cpu_invalidate(qh->page_cache); 1340 1341 status = hc32toh(sc, qh->qh_qtd.qtd_status); 1342 if (status & EHCI_QTD_ACTIVE) { 1343 /* transfer is pending */ 1344 goto done; 1345 } 1346 1347 while (1) { 1348 usb_pc_cpu_invalidate(td->page_cache); 1349 status = hc32toh(sc, td->qtd_status); 1350 1351 /* 1352 * Check if there is an active TD which 1353 * indicates that the transfer isn't done. 1354 */ 1355 if (status & EHCI_QTD_ACTIVE) { 1356 /* update cache */ 1357 xfer->td_transfer_cache = td; 1358 goto done; 1359 } 1360 /* 1361 * last transfer descriptor makes the transfer done 1362 */ 1363 if (((void *)td) == xfer->td_transfer_last) { 1364 break; 1365 } 1366 /* 1367 * any kind of error makes the transfer done 1368 */ 1369 if (status & EHCI_QTD_HALTED) { 1370 break; 1371 } 1372 /* 1373 * if there is no alternate next transfer, a short 1374 * packet also makes the transfer done 1375 */ 1376 if (EHCI_QTD_GET_BYTES(status)) { 1377 if (xfer->flags_int.short_frames_ok) { 1378 /* follow alt next */ 1379 if (td->alt_next) { 1380 td = td->alt_next; 1381 continue; 1382 } 1383 } 1384 /* transfer is done */ 1385 break; 1386 } 1387 td = td->obj_next; 1388 } 1389 ehci_non_isoc_done(xfer); 1390 goto transferred; 1391 } 1392 1393 done: 1394 DPRINTFN(13, "xfer=%p is still active\n", xfer); 1395 return (0); 1396 1397 transferred: 1398 return (1); 1399 } 1400 1401 static void 1402 ehci_pcd_enable(ehci_softc_t *sc) 1403 { 1404 USB_BUS_LOCK_ASSERT(&sc->sc_bus); 1405 1406 sc->sc_eintrs |= EHCI_STS_PCD; 1407 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 1408 1409 /* acknowledge any PCD interrupt */ 1410 EOWRITE4(sc, EHCI_USBSTS, EHCI_STS_PCD); 1411 1412 ehci_root_intr(sc); 1413 } 1414 1415 static void 1416 ehci_interrupt_poll(ehci_softc_t *sc) 1417 { 1418 struct usb_xfer *xfer; 1419 1420 repeat: 1421 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 1422 /* 1423 * check if transfer is transferred 1424 */ 1425 if (ehci_check_transfer(xfer)) { 1426 /* queue has been modified */ 1427 goto repeat; 1428 } 1429 } 1430 } 1431 1432 /* 1433 * Some EHCI chips from VIA / ATI seem to trigger interrupts before 1434 * writing back the qTD status, or miss signalling occasionally under 1435 * heavy load. If the host machine is too fast, we can miss 1436 * transaction completion - when we scan the active list the 1437 * transaction still seems to be active. This generally exhibits 1438 * itself as a umass stall that never recovers. 1439 * 1440 * We work around this behaviour by setting up this callback after any 1441 * softintr that completes with transactions still pending, giving us 1442 * another chance to check for completion after the writeback has 1443 * taken place. 1444 */ 1445 static void 1446 ehci_poll_timeout(void *arg) 1447 { 1448 ehci_softc_t *sc = arg; 1449 1450 DPRINTFN(3, "\n"); 1451 ehci_interrupt_poll(sc); 1452 } 1453 1454 /*------------------------------------------------------------------------* 1455 * ehci_interrupt - EHCI interrupt handler 1456 * 1457 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler, 1458 * hence the interrupt handler will be setup before "sc->sc_bus.bdev" 1459 * is present ! 1460 *------------------------------------------------------------------------*/ 1461 void 1462 ehci_interrupt(ehci_softc_t *sc) 1463 { 1464 uint32_t status; 1465 1466 USB_BUS_LOCK(&sc->sc_bus); 1467 1468 DPRINTFN(16, "real interrupt\n"); 1469 1470 #ifdef USB_DEBUG 1471 if (ehcidebug > 15) { 1472 ehci_dump_regs(sc); 1473 } 1474 #endif 1475 1476 status = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 1477 if (status == 0) { 1478 /* the interrupt was not for us */ 1479 goto done; 1480 } 1481 if (!(status & sc->sc_eintrs)) { 1482 goto done; 1483 } 1484 EOWRITE4(sc, EHCI_USBSTS, status); /* acknowledge */ 1485 1486 status &= sc->sc_eintrs; 1487 1488 if (status & EHCI_STS_HSE) { 1489 kprintf("%s: unrecoverable error, " 1490 "controller halted\n", __func__); 1491 #ifdef USB_DEBUG 1492 ehci_dump_regs(sc); 1493 ehci_dump_isoc(sc); 1494 #endif 1495 } 1496 if (status & EHCI_STS_PCD) { 1497 /* 1498 * Disable PCD interrupt for now, because it will be 1499 * on until the port has been reset. 1500 */ 1501 sc->sc_eintrs &= ~EHCI_STS_PCD; 1502 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 1503 1504 ehci_root_intr(sc); 1505 1506 /* do not allow RHSC interrupts > 1 per second */ 1507 usb_callout_reset(&sc->sc_tmo_pcd, hz, 1508 (void *)&ehci_pcd_enable, sc); 1509 } 1510 status &= ~(EHCI_STS_INT | EHCI_STS_ERRINT | EHCI_STS_PCD | EHCI_STS_IAA); 1511 1512 if (status != 0) { 1513 /* block unprocessed interrupts */ 1514 sc->sc_eintrs &= ~status; 1515 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 1516 kprintf("%s: blocking interrupts 0x%x\n", __func__, status); 1517 } 1518 /* poll all the USB transfers */ 1519 ehci_interrupt_poll(sc); 1520 1521 if (sc->sc_flags & EHCI_SCFLG_LOSTINTRBUG) { 1522 usb_callout_reset(&sc->sc_tmo_poll, hz / 128, 1523 (void *)&ehci_poll_timeout, sc); 1524 } 1525 1526 done: 1527 USB_BUS_UNLOCK(&sc->sc_bus); 1528 } 1529 1530 /* 1531 * called when a request does not complete 1532 */ 1533 static void 1534 ehci_timeout(void *arg) 1535 { 1536 struct usb_xfer *xfer = arg; 1537 1538 DPRINTF("xfer=%p\n", xfer); 1539 1540 USB_BUS_LOCK_ASSERT(xfer->xroot->bus); 1541 1542 /* transfer is transferred */ 1543 ehci_device_done(xfer, USB_ERR_TIMEOUT); 1544 } 1545 1546 static void 1547 ehci_do_poll(struct usb_bus *bus) 1548 { 1549 ehci_softc_t *sc = EHCI_BUS2SC(bus); 1550 1551 USB_BUS_LOCK(&sc->sc_bus); 1552 ehci_interrupt_poll(sc); 1553 USB_BUS_UNLOCK(&sc->sc_bus); 1554 } 1555 1556 static void 1557 ehci_setup_standard_chain_sub(struct ehci_std_temp *temp) 1558 { 1559 struct usb_page_search buf_res; 1560 ehci_qtd_t *td; 1561 ehci_qtd_t *td_next; 1562 ehci_qtd_t *td_alt_next; 1563 uint32_t buf_offset; 1564 uint32_t average; 1565 uint32_t len_old; 1566 uint32_t terminate; 1567 uint32_t qtd_altnext; 1568 uint8_t shortpkt_old; 1569 uint8_t precompute; 1570 1571 terminate = temp->sc->sc_terminate_self; 1572 qtd_altnext = temp->sc->sc_terminate_self; 1573 td_alt_next = NULL; 1574 buf_offset = 0; 1575 shortpkt_old = temp->shortpkt; 1576 len_old = temp->len; 1577 precompute = 1; 1578 1579 restart: 1580 1581 td = temp->td; 1582 td_next = temp->td_next; 1583 1584 while (1) { 1585 1586 if (temp->len == 0) { 1587 1588 if (temp->shortpkt) { 1589 break; 1590 } 1591 /* send a Zero Length Packet, ZLP, last */ 1592 1593 temp->shortpkt = 1; 1594 average = 0; 1595 1596 } else { 1597 1598 average = temp->average; 1599 1600 if (temp->len < average) { 1601 if (temp->len % temp->max_frame_size) { 1602 temp->shortpkt = 1; 1603 } 1604 average = temp->len; 1605 } 1606 } 1607 1608 if (td_next == NULL) { 1609 panic("%s: out of EHCI transfer descriptors!", __func__); 1610 } 1611 /* get next TD */ 1612 1613 td = td_next; 1614 td_next = td->obj_next; 1615 1616 /* check if we are pre-computing */ 1617 1618 if (precompute) { 1619 1620 /* update remaining length */ 1621 1622 temp->len -= average; 1623 1624 continue; 1625 } 1626 /* fill out current TD */ 1627 1628 td->qtd_status = 1629 temp->qtd_status | 1630 htohc32(temp->sc, EHCI_QTD_IOC | 1631 EHCI_QTD_SET_BYTES(average)); 1632 1633 if (average == 0) { 1634 1635 if (temp->auto_data_toggle == 0) { 1636 1637 /* update data toggle, ZLP case */ 1638 1639 temp->qtd_status ^= 1640 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK); 1641 } 1642 td->len = 0; 1643 1644 td->qtd_buffer[0] = 0; 1645 td->qtd_buffer_hi[0] = 0; 1646 1647 td->qtd_buffer[1] = 0; 1648 td->qtd_buffer_hi[1] = 0; 1649 1650 } else { 1651 1652 uint8_t x; 1653 1654 if (temp->auto_data_toggle == 0) { 1655 1656 /* update data toggle */ 1657 1658 if (((average + temp->max_frame_size - 1) / 1659 temp->max_frame_size) & 1) { 1660 temp->qtd_status ^= 1661 htohc32(temp->sc, EHCI_QTD_TOGGLE_MASK); 1662 } 1663 } 1664 td->len = average; 1665 1666 /* update remaining length */ 1667 1668 temp->len -= average; 1669 1670 /* fill out buffer pointers */ 1671 1672 usbd_get_page(temp->pc, buf_offset, &buf_res); 1673 td->qtd_buffer[0] = 1674 htohc32(temp->sc, buf_res.physaddr); 1675 td->qtd_buffer_hi[0] = 0; 1676 1677 x = 1; 1678 1679 while (average > EHCI_PAGE_SIZE) { 1680 average -= EHCI_PAGE_SIZE; 1681 buf_offset += EHCI_PAGE_SIZE; 1682 usbd_get_page(temp->pc, buf_offset, &buf_res); 1683 td->qtd_buffer[x] = 1684 htohc32(temp->sc, 1685 buf_res.physaddr & (~0xFFF)); 1686 td->qtd_buffer_hi[x] = 0; 1687 x++; 1688 } 1689 1690 /* 1691 * NOTE: The "average" variable is never zero after 1692 * exiting the loop above ! 1693 * 1694 * NOTE: We have to subtract one from the offset to 1695 * ensure that we are computing the physical address 1696 * of a valid page ! 1697 */ 1698 buf_offset += average; 1699 usbd_get_page(temp->pc, buf_offset - 1, &buf_res); 1700 td->qtd_buffer[x] = 1701 htohc32(temp->sc, 1702 buf_res.physaddr & (~0xFFF)); 1703 td->qtd_buffer_hi[x] = 0; 1704 } 1705 1706 if (td_next) { 1707 /* link the current TD with the next one */ 1708 td->qtd_next = td_next->qtd_self; 1709 } 1710 td->qtd_altnext = qtd_altnext; 1711 td->alt_next = td_alt_next; 1712 1713 usb_pc_cpu_flush(td->page_cache); 1714 } 1715 1716 if (precompute) { 1717 precompute = 0; 1718 1719 /* setup alt next pointer, if any */ 1720 if (temp->last_frame) { 1721 td_alt_next = NULL; 1722 qtd_altnext = terminate; 1723 } else { 1724 /* we use this field internally */ 1725 td_alt_next = td_next; 1726 if (temp->setup_alt_next) { 1727 qtd_altnext = td_next->qtd_self; 1728 } else { 1729 qtd_altnext = terminate; 1730 } 1731 } 1732 1733 /* restore */ 1734 temp->shortpkt = shortpkt_old; 1735 temp->len = len_old; 1736 goto restart; 1737 } 1738 temp->td = td; 1739 temp->td_next = td_next; 1740 } 1741 1742 static void 1743 ehci_setup_standard_chain(struct usb_xfer *xfer, ehci_qh_t **qh_last) 1744 { 1745 struct ehci_std_temp temp; 1746 struct usb_pipe_methods *methods; 1747 ehci_qh_t *qh; 1748 ehci_qtd_t *td; 1749 uint32_t qh_endp; 1750 uint32_t qh_endphub; 1751 uint32_t x; 1752 1753 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n", 1754 xfer->address, UE_GET_ADDR(xfer->endpointno), 1755 xfer->sumlen, usbd_get_speed(xfer->xroot->udev)); 1756 1757 temp.average = xfer->max_hc_frame_size; 1758 temp.max_frame_size = xfer->max_frame_size; 1759 temp.sc = EHCI_BUS2SC(xfer->xroot->bus); 1760 1761 /* toggle the DMA set we are using */ 1762 xfer->flags_int.curr_dma_set ^= 1; 1763 1764 /* get next DMA set */ 1765 td = xfer->td_start[xfer->flags_int.curr_dma_set]; 1766 1767 xfer->td_transfer_first = td; 1768 xfer->td_transfer_cache = td; 1769 1770 temp.td = NULL; 1771 temp.td_next = td; 1772 temp.qtd_status = 0; 1773 temp.last_frame = 0; 1774 temp.setup_alt_next = xfer->flags_int.short_frames_ok; 1775 1776 if (xfer->flags_int.control_xfr) { 1777 if (xfer->endpoint->toggle_next) { 1778 /* DATA1 is next */ 1779 temp.qtd_status |= 1780 htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1)); 1781 } 1782 temp.auto_data_toggle = 0; 1783 } else { 1784 temp.auto_data_toggle = 1; 1785 } 1786 1787 if ((xfer->xroot->udev->parent_hs_hub != NULL) || 1788 (xfer->xroot->udev->address != 0)) { 1789 /* max 3 retries */ 1790 temp.qtd_status |= 1791 htohc32(temp.sc, EHCI_QTD_SET_CERR(3)); 1792 } 1793 /* check if we should prepend a setup message */ 1794 1795 if (xfer->flags_int.control_xfr) { 1796 if (xfer->flags_int.control_hdr) { 1797 1798 xfer->endpoint->toggle_next = 0; 1799 1800 temp.qtd_status &= 1801 htohc32(temp.sc, EHCI_QTD_SET_CERR(3)); 1802 temp.qtd_status |= htohc32(temp.sc, 1803 EHCI_QTD_ACTIVE | 1804 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) | 1805 EHCI_QTD_SET_TOGGLE(0)); 1806 1807 temp.len = xfer->frlengths[0]; 1808 temp.pc = xfer->frbuffers + 0; 1809 temp.shortpkt = temp.len ? 1 : 0; 1810 /* check for last frame */ 1811 if (xfer->nframes == 1) { 1812 /* no STATUS stage yet, SETUP is last */ 1813 if (xfer->flags_int.control_act) { 1814 temp.last_frame = 1; 1815 temp.setup_alt_next = 0; 1816 } 1817 } 1818 ehci_setup_standard_chain_sub(&temp); 1819 } 1820 x = 1; 1821 } else { 1822 x = 0; 1823 } 1824 1825 while (x != xfer->nframes) { 1826 1827 /* DATA0 / DATA1 message */ 1828 1829 temp.len = xfer->frlengths[x]; 1830 temp.pc = xfer->frbuffers + x; 1831 1832 x++; 1833 1834 if (x == xfer->nframes) { 1835 if (xfer->flags_int.control_xfr) { 1836 /* no STATUS stage yet, DATA is last */ 1837 if (xfer->flags_int.control_act) { 1838 temp.last_frame = 1; 1839 temp.setup_alt_next = 0; 1840 } 1841 } else { 1842 temp.last_frame = 1; 1843 temp.setup_alt_next = 0; 1844 } 1845 } 1846 /* keep previous data toggle and error count */ 1847 1848 temp.qtd_status &= 1849 htohc32(temp.sc, EHCI_QTD_SET_CERR(3) | 1850 EHCI_QTD_SET_TOGGLE(1)); 1851 1852 if (temp.len == 0) { 1853 1854 /* make sure that we send an USB packet */ 1855 1856 temp.shortpkt = 0; 1857 1858 } else { 1859 1860 /* regular data transfer */ 1861 1862 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1; 1863 } 1864 1865 /* set endpoint direction */ 1866 1867 temp.qtd_status |= 1868 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ? 1869 htohc32(temp.sc, EHCI_QTD_ACTIVE | 1870 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN)) : 1871 htohc32(temp.sc, EHCI_QTD_ACTIVE | 1872 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT)); 1873 1874 ehci_setup_standard_chain_sub(&temp); 1875 } 1876 1877 /* check if we should append a status stage */ 1878 1879 if (xfer->flags_int.control_xfr && 1880 !xfer->flags_int.control_act) { 1881 1882 /* 1883 * Send a DATA1 message and invert the current endpoint 1884 * direction. 1885 */ 1886 1887 temp.qtd_status &= htohc32(temp.sc, EHCI_QTD_SET_CERR(3) | 1888 EHCI_QTD_SET_TOGGLE(1)); 1889 temp.qtd_status |= 1890 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ? 1891 htohc32(temp.sc, EHCI_QTD_ACTIVE | 1892 EHCI_QTD_SET_PID(EHCI_QTD_PID_IN) | 1893 EHCI_QTD_SET_TOGGLE(1)) : 1894 htohc32(temp.sc, EHCI_QTD_ACTIVE | 1895 EHCI_QTD_SET_PID(EHCI_QTD_PID_OUT) | 1896 EHCI_QTD_SET_TOGGLE(1)); 1897 1898 temp.len = 0; 1899 temp.pc = NULL; 1900 temp.shortpkt = 0; 1901 temp.last_frame = 1; 1902 temp.setup_alt_next = 0; 1903 1904 ehci_setup_standard_chain_sub(&temp); 1905 } 1906 td = temp.td; 1907 1908 /* the last TD terminates the transfer: */ 1909 td->qtd_next = htohc32(temp.sc, EHCI_LINK_TERMINATE); 1910 td->qtd_altnext = htohc32(temp.sc, EHCI_LINK_TERMINATE); 1911 1912 usb_pc_cpu_flush(td->page_cache); 1913 1914 /* must have at least one frame! */ 1915 1916 xfer->td_transfer_last = td; 1917 1918 #ifdef USB_DEBUG 1919 if (ehcidebug > 8) { 1920 DPRINTF("nexttog=%d; data before transfer:\n", 1921 xfer->endpoint->toggle_next); 1922 ehci_dump_sqtds(temp.sc, 1923 xfer->td_transfer_first); 1924 } 1925 #endif 1926 1927 methods = xfer->endpoint->methods; 1928 1929 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1930 1931 /* the "qh_link" field is filled when the QH is added */ 1932 1933 qh_endp = 1934 (EHCI_QH_SET_ADDR(xfer->address) | 1935 EHCI_QH_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) | 1936 EHCI_QH_SET_MPL(xfer->max_packet_size)); 1937 1938 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) { 1939 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH); 1940 if (methods != &ehci_device_intr_methods) 1941 qh_endp |= EHCI_QH_SET_NRL(8); 1942 } else { 1943 1944 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_FULL) { 1945 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_FULL); 1946 } else { 1947 qh_endp |= EHCI_QH_SET_EPS(EHCI_QH_SPEED_LOW); 1948 } 1949 1950 if (methods == &ehci_device_ctrl_methods) { 1951 qh_endp |= EHCI_QH_CTL; 1952 } 1953 if (methods != &ehci_device_intr_methods) { 1954 /* Only try one time per microframe! */ 1955 qh_endp |= EHCI_QH_SET_NRL(1); 1956 } 1957 } 1958 1959 if (temp.auto_data_toggle == 0) { 1960 /* software computes the data toggle */ 1961 qh_endp |= EHCI_QH_DTC; 1962 } 1963 1964 qh->qh_endp = htohc32(temp.sc, qh_endp); 1965 1966 qh_endphub = 1967 (EHCI_QH_SET_MULT(xfer->max_packet_count & 3) | 1968 EHCI_QH_SET_CMASK(xfer->endpoint->usb_cmask) | 1969 EHCI_QH_SET_SMASK(xfer->endpoint->usb_smask) | 1970 EHCI_QH_SET_HUBA(xfer->xroot->udev->hs_hub_addr) | 1971 EHCI_QH_SET_PORT(xfer->xroot->udev->hs_port_no)); 1972 1973 qh->qh_endphub = htohc32(temp.sc, qh_endphub); 1974 qh->qh_curqtd = 0; 1975 1976 /* fill the overlay qTD */ 1977 1978 if (temp.auto_data_toggle && xfer->endpoint->toggle_next) { 1979 /* DATA1 is next */ 1980 qh->qh_qtd.qtd_status = htohc32(temp.sc, EHCI_QTD_SET_TOGGLE(1)); 1981 } else { 1982 qh->qh_qtd.qtd_status = 0; 1983 } 1984 1985 td = xfer->td_transfer_first; 1986 1987 qh->qh_qtd.qtd_next = td->qtd_self; 1988 qh->qh_qtd.qtd_altnext = 1989 htohc32(temp.sc, EHCI_LINK_TERMINATE); 1990 1991 usb_pc_cpu_flush(qh->page_cache); 1992 1993 if (xfer->xroot->udev->flags.self_suspended == 0) { 1994 EHCI_APPEND_QH(qh, *qh_last); 1995 } 1996 } 1997 1998 static void 1999 ehci_root_intr(ehci_softc_t *sc) 2000 { 2001 uint16_t i; 2002 uint16_t m; 2003 2004 USB_BUS_LOCK_ASSERT(&sc->sc_bus); 2005 2006 /* clear any old interrupt data */ 2007 memset(sc->sc_hub_idata, 0, sizeof(sc->sc_hub_idata)); 2008 2009 /* set bits */ 2010 m = (sc->sc_noport + 1); 2011 if (m > (8 * sizeof(sc->sc_hub_idata))) { 2012 m = (8 * sizeof(sc->sc_hub_idata)); 2013 } 2014 for (i = 1; i < m; i++) { 2015 /* pick out CHANGE bits from the status register */ 2016 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) { 2017 sc->sc_hub_idata[i / 8] |= 1 << (i % 8); 2018 DPRINTF("port %d changed\n", i); 2019 } 2020 } 2021 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata, 2022 sizeof(sc->sc_hub_idata)); 2023 } 2024 2025 static void 2026 ehci_isoc_fs_done(ehci_softc_t *sc, struct usb_xfer *xfer) 2027 { 2028 uint32_t nframes = xfer->nframes; 2029 uint32_t status; 2030 uint32_t *plen = xfer->frlengths; 2031 uint16_t len = 0; 2032 ehci_sitd_t *td = xfer->td_transfer_first; 2033 ehci_sitd_t **pp_last = &sc->sc_isoc_fs_p_last[xfer->qh_pos]; 2034 2035 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 2036 xfer, xfer->endpoint); 2037 2038 while (nframes--) { 2039 if (td == NULL) { 2040 panic("%s:%d: out of TD's\n", 2041 __func__, __LINE__); 2042 } 2043 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) { 2044 pp_last = &sc->sc_isoc_fs_p_last[0]; 2045 } 2046 #ifdef USB_DEBUG 2047 if (ehcidebug > 15) { 2048 DPRINTF("isoc FS-TD\n"); 2049 ehci_dump_sitd(sc, td); 2050 } 2051 #endif 2052 usb_pc_cpu_invalidate(td->page_cache); 2053 status = hc32toh(sc, td->sitd_status); 2054 2055 len = EHCI_SITD_GET_LEN(status); 2056 2057 DPRINTFN(2, "status=0x%08x, rem=%u\n", status, len); 2058 2059 if (*plen >= len) { 2060 len = *plen - len; 2061 } else { 2062 len = 0; 2063 } 2064 2065 *plen = len; 2066 2067 /* remove FS-TD from schedule */ 2068 EHCI_REMOVE_FS_TD(td, *pp_last); 2069 2070 pp_last++; 2071 plen++; 2072 td = td->obj_next; 2073 } 2074 2075 xfer->aframes = xfer->nframes; 2076 } 2077 2078 static void 2079 ehci_isoc_hs_done(ehci_softc_t *sc, struct usb_xfer *xfer) 2080 { 2081 uint32_t nframes = xfer->nframes; 2082 uint32_t status; 2083 uint32_t *plen = xfer->frlengths; 2084 uint16_t len = 0; 2085 uint8_t td_no = 0; 2086 ehci_itd_t *td = xfer->td_transfer_first; 2087 ehci_itd_t **pp_last = &sc->sc_isoc_hs_p_last[xfer->qh_pos]; 2088 2089 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 2090 xfer, xfer->endpoint); 2091 2092 while (nframes) { 2093 if (td == NULL) { 2094 panic("%s:%d: out of TD's\n", 2095 __func__, __LINE__); 2096 } 2097 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) { 2098 pp_last = &sc->sc_isoc_hs_p_last[0]; 2099 } 2100 #ifdef USB_DEBUG 2101 if (ehcidebug > 15) { 2102 DPRINTF("isoc HS-TD\n"); 2103 ehci_dump_itd(sc, td); 2104 } 2105 #endif 2106 2107 usb_pc_cpu_invalidate(td->page_cache); 2108 status = hc32toh(sc, td->itd_status[td_no]); 2109 2110 len = EHCI_ITD_GET_LEN(status); 2111 2112 DPRINTFN(2, "status=0x%08x, len=%u\n", status, len); 2113 2114 if (xfer->endpoint->usb_smask & (1 << td_no)) { 2115 2116 if (*plen >= len) { 2117 /* 2118 * The length is valid. NOTE: The 2119 * complete length is written back 2120 * into the status field, and not the 2121 * remainder like with other transfer 2122 * descriptor types. 2123 */ 2124 } else { 2125 /* Invalid length - truncate */ 2126 len = 0; 2127 } 2128 2129 *plen = len; 2130 plen++; 2131 nframes--; 2132 } 2133 2134 td_no++; 2135 2136 if ((td_no == 8) || (nframes == 0)) { 2137 /* remove HS-TD from schedule */ 2138 EHCI_REMOVE_HS_TD(td, *pp_last); 2139 pp_last++; 2140 2141 td_no = 0; 2142 td = td->obj_next; 2143 } 2144 } 2145 xfer->aframes = xfer->nframes; 2146 } 2147 2148 /* NOTE: "done" can be run two times in a row, 2149 * from close and from interrupt 2150 */ 2151 static void 2152 ehci_device_done(struct usb_xfer *xfer, usb_error_t error) 2153 { 2154 struct usb_pipe_methods *methods = xfer->endpoint->methods; 2155 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2156 2157 USB_BUS_LOCK_ASSERT(&sc->sc_bus); 2158 2159 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n", 2160 xfer, xfer->endpoint, error); 2161 2162 if ((methods == &ehci_device_bulk_methods) || 2163 (methods == &ehci_device_ctrl_methods)) { 2164 #ifdef USB_DEBUG 2165 if (ehcidebug > 8) { 2166 DPRINTF("nexttog=%d; data after transfer:\n", 2167 xfer->endpoint->toggle_next); 2168 ehci_dump_sqtds(sc, 2169 xfer->td_transfer_first); 2170 } 2171 #endif 2172 2173 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 2174 sc->sc_async_p_last); 2175 } 2176 if (methods == &ehci_device_intr_methods) { 2177 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 2178 sc->sc_intr_p_last[xfer->qh_pos]); 2179 } 2180 /* 2181 * Only finish isochronous transfers once which will update 2182 * "xfer->frlengths". 2183 */ 2184 if (xfer->td_transfer_first && 2185 xfer->td_transfer_last) { 2186 if (methods == &ehci_device_isoc_fs_methods) { 2187 ehci_isoc_fs_done(sc, xfer); 2188 } 2189 if (methods == &ehci_device_isoc_hs_methods) { 2190 ehci_isoc_hs_done(sc, xfer); 2191 } 2192 xfer->td_transfer_first = NULL; 2193 xfer->td_transfer_last = NULL; 2194 } 2195 /* dequeue transfer and start next transfer */ 2196 usbd_transfer_done(xfer, error); 2197 } 2198 2199 /*------------------------------------------------------------------------* 2200 * ehci bulk support 2201 *------------------------------------------------------------------------*/ 2202 static void 2203 ehci_device_bulk_open(struct usb_xfer *xfer) 2204 { 2205 return; 2206 } 2207 2208 static void 2209 ehci_device_bulk_close(struct usb_xfer *xfer) 2210 { 2211 ehci_device_done(xfer, USB_ERR_CANCELLED); 2212 } 2213 2214 static void 2215 ehci_device_bulk_enter(struct usb_xfer *xfer) 2216 { 2217 return; 2218 } 2219 2220 static void 2221 ehci_device_bulk_start(struct usb_xfer *xfer) 2222 { 2223 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2224 uint32_t temp; 2225 2226 /* setup TD's and QH */ 2227 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last); 2228 2229 /* put transfer on interrupt queue */ 2230 ehci_transfer_intr_enqueue(xfer); 2231 2232 /* 2233 * XXX Certain nVidia chipsets choke when using the IAAD 2234 * feature too frequently. 2235 */ 2236 if (sc->sc_flags & EHCI_SCFLG_IAADBUG) 2237 return; 2238 2239 /* XXX Performance quirk: Some Host Controllers have a too low 2240 * interrupt rate. Issue an IAAD to stimulate the Host 2241 * Controller after queueing the BULK transfer. 2242 */ 2243 temp = EOREAD4(sc, EHCI_USBCMD); 2244 if (!(temp & EHCI_CMD_IAAD)) 2245 EOWRITE4(sc, EHCI_USBCMD, temp | EHCI_CMD_IAAD); 2246 } 2247 2248 struct usb_pipe_methods ehci_device_bulk_methods = 2249 { 2250 .open = ehci_device_bulk_open, 2251 .close = ehci_device_bulk_close, 2252 .enter = ehci_device_bulk_enter, 2253 .start = ehci_device_bulk_start, 2254 }; 2255 2256 /*------------------------------------------------------------------------* 2257 * ehci control support 2258 *------------------------------------------------------------------------*/ 2259 static void 2260 ehci_device_ctrl_open(struct usb_xfer *xfer) 2261 { 2262 return; 2263 } 2264 2265 static void 2266 ehci_device_ctrl_close(struct usb_xfer *xfer) 2267 { 2268 ehci_device_done(xfer, USB_ERR_CANCELLED); 2269 } 2270 2271 static void 2272 ehci_device_ctrl_enter(struct usb_xfer *xfer) 2273 { 2274 return; 2275 } 2276 2277 static void 2278 ehci_device_ctrl_start(struct usb_xfer *xfer) 2279 { 2280 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2281 2282 /* setup TD's and QH */ 2283 ehci_setup_standard_chain(xfer, &sc->sc_async_p_last); 2284 2285 /* put transfer on interrupt queue */ 2286 ehci_transfer_intr_enqueue(xfer); 2287 } 2288 2289 struct usb_pipe_methods ehci_device_ctrl_methods = 2290 { 2291 .open = ehci_device_ctrl_open, 2292 .close = ehci_device_ctrl_close, 2293 .enter = ehci_device_ctrl_enter, 2294 .start = ehci_device_ctrl_start, 2295 }; 2296 2297 /*------------------------------------------------------------------------* 2298 * ehci interrupt support 2299 *------------------------------------------------------------------------*/ 2300 static void 2301 ehci_device_intr_open(struct usb_xfer *xfer) 2302 { 2303 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2304 uint16_t best; 2305 uint16_t bit; 2306 uint16_t x; 2307 2308 usb_hs_bandwidth_alloc(xfer); 2309 2310 /* 2311 * Find the best QH position corresponding to the given interval: 2312 */ 2313 2314 best = 0; 2315 bit = EHCI_VIRTUAL_FRAMELIST_COUNT / 2; 2316 while (bit) { 2317 if (xfer->interval >= bit) { 2318 x = bit; 2319 best = bit; 2320 while (x & bit) { 2321 if (sc->sc_intr_stat[x] < 2322 sc->sc_intr_stat[best]) { 2323 best = x; 2324 } 2325 x++; 2326 } 2327 break; 2328 } 2329 bit >>= 1; 2330 } 2331 2332 sc->sc_intr_stat[best]++; 2333 xfer->qh_pos = best; 2334 2335 DPRINTFN(3, "best=%d interval=%d\n", 2336 best, xfer->interval); 2337 } 2338 2339 static void 2340 ehci_device_intr_close(struct usb_xfer *xfer) 2341 { 2342 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2343 2344 sc->sc_intr_stat[xfer->qh_pos]--; 2345 2346 ehci_device_done(xfer, USB_ERR_CANCELLED); 2347 2348 /* bandwidth must be freed after device done */ 2349 usb_hs_bandwidth_free(xfer); 2350 } 2351 2352 static void 2353 ehci_device_intr_enter(struct usb_xfer *xfer) 2354 { 2355 return; 2356 } 2357 2358 static void 2359 ehci_device_intr_start(struct usb_xfer *xfer) 2360 { 2361 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2362 2363 /* setup TD's and QH */ 2364 ehci_setup_standard_chain(xfer, &sc->sc_intr_p_last[xfer->qh_pos]); 2365 2366 /* put transfer on interrupt queue */ 2367 ehci_transfer_intr_enqueue(xfer); 2368 } 2369 2370 struct usb_pipe_methods ehci_device_intr_methods = 2371 { 2372 .open = ehci_device_intr_open, 2373 .close = ehci_device_intr_close, 2374 .enter = ehci_device_intr_enter, 2375 .start = ehci_device_intr_start, 2376 }; 2377 2378 /*------------------------------------------------------------------------* 2379 * ehci full speed isochronous support 2380 *------------------------------------------------------------------------*/ 2381 static void 2382 ehci_device_isoc_fs_open(struct usb_xfer *xfer) 2383 { 2384 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2385 ehci_sitd_t *td; 2386 uint32_t sitd_portaddr; 2387 uint8_t ds; 2388 2389 sitd_portaddr = 2390 EHCI_SITD_SET_ADDR(xfer->address) | 2391 EHCI_SITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno)) | 2392 EHCI_SITD_SET_HUBA(xfer->xroot->udev->hs_hub_addr) | 2393 EHCI_SITD_SET_PORT(xfer->xroot->udev->hs_port_no); 2394 2395 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) { 2396 sitd_portaddr |= EHCI_SITD_SET_DIR_IN; 2397 } 2398 sitd_portaddr = htohc32(sc, sitd_portaddr); 2399 2400 /* initialize all TD's */ 2401 2402 for (ds = 0; ds != 2; ds++) { 2403 2404 for (td = xfer->td_start[ds]; td; td = td->obj_next) { 2405 2406 td->sitd_portaddr = sitd_portaddr; 2407 2408 /* 2409 * TODO: make some kind of automatic 2410 * SMASK/CMASK selection based on micro-frame 2411 * usage 2412 * 2413 * micro-frame usage (8 microframes per 1ms) 2414 */ 2415 td->sitd_back = htohc32(sc, EHCI_LINK_TERMINATE); 2416 2417 usb_pc_cpu_flush(td->page_cache); 2418 } 2419 } 2420 } 2421 2422 static void 2423 ehci_device_isoc_fs_close(struct usb_xfer *xfer) 2424 { 2425 ehci_device_done(xfer, USB_ERR_CANCELLED); 2426 } 2427 2428 static void 2429 ehci_device_isoc_fs_enter(struct usb_xfer *xfer) 2430 { 2431 struct usb_page_search buf_res; 2432 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2433 ehci_sitd_t *td; 2434 ehci_sitd_t *td_last = NULL; 2435 ehci_sitd_t **pp_last; 2436 uint32_t *plen; 2437 uint32_t buf_offset; 2438 uint32_t nframes; 2439 uint32_t temp; 2440 uint32_t sitd_mask; 2441 uint16_t tlen; 2442 uint8_t sa; 2443 uint8_t sb; 2444 2445 #ifdef USB_DEBUG 2446 uint8_t once = 1; 2447 2448 #endif 2449 2450 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n", 2451 xfer, xfer->endpoint->isoc_next, xfer->nframes); 2452 2453 /* get the current frame index */ 2454 2455 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8; 2456 2457 /* 2458 * check if the frame index is within the window where the frames 2459 * will be inserted 2460 */ 2461 buf_offset = (nframes - xfer->endpoint->isoc_next) & 2462 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2463 2464 if ((xfer->endpoint->is_synced == 0) || 2465 (buf_offset < xfer->nframes)) { 2466 /* 2467 * If there is data underflow or the pipe queue is empty we 2468 * schedule the transfer a few frames ahead of the current 2469 * frame position. Else two isochronous transfers might 2470 * overlap. 2471 */ 2472 xfer->endpoint->isoc_next = (nframes + 3) & 2473 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2474 xfer->endpoint->is_synced = 1; 2475 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next); 2476 } 2477 /* 2478 * compute how many milliseconds the insertion is ahead of the 2479 * current frame position: 2480 */ 2481 buf_offset = (xfer->endpoint->isoc_next - nframes) & 2482 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2483 2484 /* 2485 * pre-compute when the isochronous transfer will be finished: 2486 */ 2487 xfer->isoc_time_complete = 2488 usb_isoc_time_expand(&sc->sc_bus, nframes) + 2489 buf_offset + xfer->nframes; 2490 2491 /* get the real number of frames */ 2492 2493 nframes = xfer->nframes; 2494 2495 buf_offset = 0; 2496 2497 plen = xfer->frlengths; 2498 2499 /* toggle the DMA set we are using */ 2500 xfer->flags_int.curr_dma_set ^= 1; 2501 2502 /* get next DMA set */ 2503 td = xfer->td_start[xfer->flags_int.curr_dma_set]; 2504 xfer->td_transfer_first = td; 2505 2506 pp_last = &sc->sc_isoc_fs_p_last[xfer->endpoint->isoc_next]; 2507 2508 /* store starting position */ 2509 2510 xfer->qh_pos = xfer->endpoint->isoc_next; 2511 2512 while (nframes--) { 2513 if (td == NULL) { 2514 panic("%s:%d: out of TD's\n", 2515 __func__, __LINE__); 2516 } 2517 if (pp_last >= &sc->sc_isoc_fs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) { 2518 pp_last = &sc->sc_isoc_fs_p_last[0]; 2519 } 2520 /* reuse sitd_portaddr and sitd_back from last transfer */ 2521 2522 if (*plen > xfer->max_frame_size) { 2523 #ifdef USB_DEBUG 2524 if (once) { 2525 once = 0; 2526 kprintf("%s: frame length(%d) exceeds %d " 2527 "bytes (frame truncated)\n", 2528 __func__, *plen, 2529 xfer->max_frame_size); 2530 } 2531 #endif 2532 *plen = xfer->max_frame_size; 2533 } 2534 /* allocate a slot */ 2535 2536 sa = usbd_fs_isoc_schedule_alloc_slot(xfer, 2537 xfer->isoc_time_complete - nframes -1); 2538 2539 if(sa == 255) { 2540 /* 2541 * Schedule is FULL, set length to zero: 2542 */ 2543 *plen = 0; 2544 sa = USB_FS_ISOC_UFRAME_MAX - 1; 2545 } 2546 if (*plen) { 2547 /* 2548 * only call "usbd_get_page()" when we have a 2549 * non-zero length 2550 */ 2551 usbd_get_page(xfer->frbuffers, buf_offset, &buf_res); 2552 td->sitd_bp[0] = htohc32(sc, buf_res.physaddr); 2553 buf_offset += *plen; 2554 /* 2555 * NOTE: We need to subtract one from the offset so 2556 * that we are on a valid page! 2557 */ 2558 usbd_get_page(xfer->frbuffers, buf_offset - 1, 2559 &buf_res); 2560 temp = buf_res.physaddr & ~0xFFF; 2561 } else { 2562 td->sitd_bp[0] = 0; 2563 temp = 0; 2564 } 2565 2566 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) { 2567 tlen = *plen; 2568 if (tlen <= 188) { 2569 temp |= 1; /* T-count = 1, TP = ALL */ 2570 tlen = 1; 2571 } else { 2572 tlen += 187; 2573 tlen /= 188; 2574 temp |= tlen; /* T-count = [1..6] */ 2575 temp |= 8; /* TP = Begin */ 2576 } 2577 2578 tlen += sa; 2579 2580 if (tlen >= 8) { 2581 sb = 0; 2582 } else { 2583 sb = (1 << tlen); 2584 } 2585 2586 sa = (1 << sa); 2587 sa = (sb - sa) & 0x3F; 2588 sb = 0; 2589 } else { 2590 sb = (-(4 << sa)) & 0xFE; 2591 sa = (1 << sa) & 0x3F; 2592 } 2593 2594 sitd_mask = (EHCI_SITD_SET_SMASK(sa) | 2595 EHCI_SITD_SET_CMASK(sb)); 2596 2597 td->sitd_bp[1] = htohc32(sc, temp); 2598 2599 td->sitd_mask = htohc32(sc, sitd_mask); 2600 2601 if (nframes == 0) { 2602 td->sitd_status = htohc32(sc, 2603 EHCI_SITD_IOC | 2604 EHCI_SITD_ACTIVE | 2605 EHCI_SITD_SET_LEN(*plen)); 2606 } else { 2607 td->sitd_status = htohc32(sc, 2608 EHCI_SITD_ACTIVE | 2609 EHCI_SITD_SET_LEN(*plen)); 2610 } 2611 usb_pc_cpu_flush(td->page_cache); 2612 2613 #ifdef USB_DEBUG 2614 if (ehcidebug > 15) { 2615 DPRINTF("FS-TD %d\n", nframes); 2616 ehci_dump_sitd(sc, td); 2617 } 2618 #endif 2619 /* insert TD into schedule */ 2620 EHCI_APPEND_FS_TD(td, *pp_last); 2621 pp_last++; 2622 2623 plen++; 2624 td_last = td; 2625 td = td->obj_next; 2626 } 2627 2628 xfer->td_transfer_last = td_last; 2629 2630 /* update isoc_next */ 2631 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_fs_p_last[0]) & 2632 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2633 2634 /* 2635 * We don't allow cancelling of the SPLIT transaction USB FULL 2636 * speed transfer, because it disturbs the bandwidth 2637 * computation algorithm. 2638 */ 2639 xfer->flags_int.can_cancel_immed = 0; 2640 } 2641 2642 static void 2643 ehci_device_isoc_fs_start(struct usb_xfer *xfer) 2644 { 2645 /* 2646 * We don't allow cancelling of the SPLIT transaction USB FULL 2647 * speed transfer, because it disturbs the bandwidth 2648 * computation algorithm. 2649 */ 2650 xfer->flags_int.can_cancel_immed = 0; 2651 2652 /* set a default timeout */ 2653 if (xfer->timeout == 0) 2654 xfer->timeout = 500; /* ms */ 2655 2656 /* put transfer on interrupt queue */ 2657 ehci_transfer_intr_enqueue(xfer); 2658 } 2659 2660 struct usb_pipe_methods ehci_device_isoc_fs_methods = 2661 { 2662 .open = ehci_device_isoc_fs_open, 2663 .close = ehci_device_isoc_fs_close, 2664 .enter = ehci_device_isoc_fs_enter, 2665 .start = ehci_device_isoc_fs_start, 2666 }; 2667 2668 /*------------------------------------------------------------------------* 2669 * ehci high speed isochronous support 2670 *------------------------------------------------------------------------*/ 2671 static void 2672 ehci_device_isoc_hs_open(struct usb_xfer *xfer) 2673 { 2674 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2675 ehci_itd_t *td; 2676 uint32_t temp; 2677 uint8_t ds; 2678 2679 usb_hs_bandwidth_alloc(xfer); 2680 2681 /* initialize all TD's */ 2682 2683 for (ds = 0; ds != 2; ds++) { 2684 2685 for (td = xfer->td_start[ds]; td; td = td->obj_next) { 2686 2687 /* set TD inactive */ 2688 td->itd_status[0] = 0; 2689 td->itd_status[1] = 0; 2690 td->itd_status[2] = 0; 2691 td->itd_status[3] = 0; 2692 td->itd_status[4] = 0; 2693 td->itd_status[5] = 0; 2694 td->itd_status[6] = 0; 2695 td->itd_status[7] = 0; 2696 2697 /* set endpoint and address */ 2698 td->itd_bp[0] = htohc32(sc, 2699 EHCI_ITD_SET_ADDR(xfer->address) | 2700 EHCI_ITD_SET_ENDPT(UE_GET_ADDR(xfer->endpointno))); 2701 2702 temp = 2703 EHCI_ITD_SET_MPL(xfer->max_packet_size & 0x7FF); 2704 2705 /* set direction */ 2706 if (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) { 2707 temp |= EHCI_ITD_SET_DIR_IN; 2708 } 2709 /* set maximum packet size */ 2710 td->itd_bp[1] = htohc32(sc, temp); 2711 2712 /* set transfer multiplier */ 2713 td->itd_bp[2] = htohc32(sc, xfer->max_packet_count & 3); 2714 2715 usb_pc_cpu_flush(td->page_cache); 2716 } 2717 } 2718 } 2719 2720 static void 2721 ehci_device_isoc_hs_close(struct usb_xfer *xfer) 2722 { 2723 ehci_device_done(xfer, USB_ERR_CANCELLED); 2724 2725 /* bandwidth must be freed after device done */ 2726 usb_hs_bandwidth_free(xfer); 2727 } 2728 2729 static void 2730 ehci_device_isoc_hs_enter(struct usb_xfer *xfer) 2731 { 2732 struct usb_page_search buf_res; 2733 ehci_softc_t *sc = EHCI_BUS2SC(xfer->xroot->bus); 2734 ehci_itd_t *td; 2735 ehci_itd_t *td_last = NULL; 2736 ehci_itd_t **pp_last; 2737 bus_size_t page_addr; 2738 uint32_t *plen; 2739 uint32_t status; 2740 uint32_t buf_offset; 2741 uint32_t nframes; 2742 uint32_t itd_offset[8 + 1]; 2743 uint8_t x; 2744 uint8_t td_no; 2745 uint8_t page_no; 2746 uint8_t shift = usbd_xfer_get_fps_shift(xfer); 2747 2748 #ifdef USB_DEBUG 2749 uint8_t once = 1; 2750 2751 #endif 2752 2753 DPRINTFN(6, "xfer=%p next=%d nframes=%d shift=%d\n", 2754 xfer, xfer->endpoint->isoc_next, xfer->nframes, (int)shift); 2755 2756 /* get the current frame index */ 2757 2758 nframes = EOREAD4(sc, EHCI_FRINDEX) / 8; 2759 2760 /* 2761 * check if the frame index is within the window where the frames 2762 * will be inserted 2763 */ 2764 buf_offset = (nframes - xfer->endpoint->isoc_next) & 2765 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2766 2767 if ((xfer->endpoint->is_synced == 0) || 2768 (buf_offset < (((xfer->nframes << shift) + 7) / 8))) { 2769 /* 2770 * If there is data underflow or the pipe queue is empty we 2771 * schedule the transfer a few frames ahead of the current 2772 * frame position. Else two isochronous transfers might 2773 * overlap. 2774 */ 2775 xfer->endpoint->isoc_next = (nframes + 3) & 2776 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2777 xfer->endpoint->is_synced = 1; 2778 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next); 2779 } 2780 /* 2781 * compute how many milliseconds the insertion is ahead of the 2782 * current frame position: 2783 */ 2784 buf_offset = (xfer->endpoint->isoc_next - nframes) & 2785 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2786 2787 /* 2788 * pre-compute when the isochronous transfer will be finished: 2789 */ 2790 xfer->isoc_time_complete = 2791 usb_isoc_time_expand(&sc->sc_bus, nframes) + buf_offset + 2792 (((xfer->nframes << shift) + 7) / 8); 2793 2794 /* get the real number of frames */ 2795 2796 nframes = xfer->nframes; 2797 2798 buf_offset = 0; 2799 td_no = 0; 2800 2801 plen = xfer->frlengths; 2802 2803 /* toggle the DMA set we are using */ 2804 xfer->flags_int.curr_dma_set ^= 1; 2805 2806 /* get next DMA set */ 2807 td = xfer->td_start[xfer->flags_int.curr_dma_set]; 2808 xfer->td_transfer_first = td; 2809 2810 pp_last = &sc->sc_isoc_hs_p_last[xfer->endpoint->isoc_next]; 2811 2812 /* store starting position */ 2813 2814 xfer->qh_pos = xfer->endpoint->isoc_next; 2815 2816 while (nframes) { 2817 if (td == NULL) { 2818 panic("%s:%d: out of TD's\n", 2819 __func__, __LINE__); 2820 } 2821 if (pp_last >= &sc->sc_isoc_hs_p_last[EHCI_VIRTUAL_FRAMELIST_COUNT]) { 2822 pp_last = &sc->sc_isoc_hs_p_last[0]; 2823 } 2824 /* range check */ 2825 if (*plen > xfer->max_frame_size) { 2826 #ifdef USB_DEBUG 2827 if (once) { 2828 once = 0; 2829 kprintf("%s: frame length(%d) exceeds %d bytes " 2830 "(frame truncated)\n", 2831 __func__, *plen, xfer->max_frame_size); 2832 } 2833 #endif 2834 *plen = xfer->max_frame_size; 2835 } 2836 2837 if (xfer->endpoint->usb_smask & (1 << td_no)) { 2838 status = (EHCI_ITD_SET_LEN(*plen) | 2839 EHCI_ITD_ACTIVE | 2840 EHCI_ITD_SET_PG(0)); 2841 td->itd_status[td_no] = htohc32(sc, status); 2842 itd_offset[td_no] = buf_offset; 2843 buf_offset += *plen; 2844 plen++; 2845 nframes --; 2846 } else { 2847 td->itd_status[td_no] = 0; /* not active */ 2848 itd_offset[td_no] = buf_offset; 2849 } 2850 2851 td_no++; 2852 2853 if ((td_no == 8) || (nframes == 0)) { 2854 2855 /* the rest of the transfers are not active, if any */ 2856 for (x = td_no; x != 8; x++) { 2857 td->itd_status[x] = 0; /* not active */ 2858 } 2859 2860 /* check if there is any data to be transferred */ 2861 if (itd_offset[0] != buf_offset) { 2862 page_no = 0; 2863 itd_offset[td_no] = buf_offset; 2864 2865 /* get first page offset */ 2866 usbd_get_page(xfer->frbuffers, itd_offset[0], &buf_res); 2867 /* get page address */ 2868 page_addr = buf_res.physaddr & ~0xFFF; 2869 /* update page address */ 2870 td->itd_bp[0] &= htohc32(sc, 0xFFF); 2871 td->itd_bp[0] |= htohc32(sc, page_addr); 2872 2873 for (x = 0; x != td_no; x++) { 2874 /* set page number and page offset */ 2875 status = (EHCI_ITD_SET_PG(page_no) | 2876 (buf_res.physaddr & 0xFFF)); 2877 td->itd_status[x] |= htohc32(sc, status); 2878 2879 /* get next page offset */ 2880 if (itd_offset[x + 1] == buf_offset) { 2881 /* 2882 * We subtract one so that 2883 * we don't go off the last 2884 * page! 2885 */ 2886 usbd_get_page(xfer->frbuffers, buf_offset - 1, &buf_res); 2887 } else { 2888 usbd_get_page(xfer->frbuffers, itd_offset[x + 1], &buf_res); 2889 } 2890 2891 /* check if we need a new page */ 2892 if ((buf_res.physaddr ^ page_addr) & ~0xFFF) { 2893 /* new page needed */ 2894 page_addr = buf_res.physaddr & ~0xFFF; 2895 if (page_no == 6) { 2896 panic("%s: too many pages\n", __func__); 2897 } 2898 page_no++; 2899 /* update page address */ 2900 td->itd_bp[page_no] &= htohc32(sc, 0xFFF); 2901 td->itd_bp[page_no] |= htohc32(sc, page_addr); 2902 } 2903 } 2904 } 2905 /* set IOC bit if we are complete */ 2906 if (nframes == 0) { 2907 td->itd_status[td_no - 1] |= htohc32(sc, EHCI_ITD_IOC); 2908 } 2909 usb_pc_cpu_flush(td->page_cache); 2910 #ifdef USB_DEBUG 2911 if (ehcidebug > 15) { 2912 DPRINTF("HS-TD %d\n", nframes); 2913 ehci_dump_itd(sc, td); 2914 } 2915 #endif 2916 /* insert TD into schedule */ 2917 EHCI_APPEND_HS_TD(td, *pp_last); 2918 pp_last++; 2919 2920 td_no = 0; 2921 td_last = td; 2922 td = td->obj_next; 2923 } 2924 } 2925 2926 xfer->td_transfer_last = td_last; 2927 2928 /* update isoc_next */ 2929 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_hs_p_last[0]) & 2930 (EHCI_VIRTUAL_FRAMELIST_COUNT - 1); 2931 } 2932 2933 static void 2934 ehci_device_isoc_hs_start(struct usb_xfer *xfer) 2935 { 2936 /* put transfer on interrupt queue */ 2937 ehci_transfer_intr_enqueue(xfer); 2938 } 2939 2940 struct usb_pipe_methods ehci_device_isoc_hs_methods = 2941 { 2942 .open = ehci_device_isoc_hs_open, 2943 .close = ehci_device_isoc_hs_close, 2944 .enter = ehci_device_isoc_hs_enter, 2945 .start = ehci_device_isoc_hs_start, 2946 }; 2947 2948 /*------------------------------------------------------------------------* 2949 * ehci root control support 2950 *------------------------------------------------------------------------* 2951 * Simulate a hardware hub by handling all the necessary requests. 2952 *------------------------------------------------------------------------*/ 2953 2954 static const 2955 struct usb_device_descriptor ehci_devd = 2956 { 2957 sizeof(struct usb_device_descriptor), 2958 UDESC_DEVICE, /* type */ 2959 {0x00, 0x02}, /* USB version */ 2960 UDCLASS_HUB, /* class */ 2961 UDSUBCLASS_HUB, /* subclass */ 2962 UDPROTO_HSHUBSTT, /* protocol */ 2963 64, /* max packet */ 2964 {0}, {0}, {0x00, 0x01}, /* device id */ 2965 1, 2, 0, /* string indicies */ 2966 1 /* # of configurations */ 2967 }; 2968 2969 static const 2970 struct usb_device_qualifier ehci_odevd = 2971 { 2972 sizeof(struct usb_device_qualifier), 2973 UDESC_DEVICE_QUALIFIER, /* type */ 2974 {0x00, 0x02}, /* USB version */ 2975 UDCLASS_HUB, /* class */ 2976 UDSUBCLASS_HUB, /* subclass */ 2977 UDPROTO_FSHUB, /* protocol */ 2978 0, /* max packet */ 2979 0, /* # of configurations */ 2980 0 2981 }; 2982 2983 static const struct ehci_config_desc ehci_confd = { 2984 .confd = { 2985 .bLength = sizeof(struct usb_config_descriptor), 2986 .bDescriptorType = UDESC_CONFIG, 2987 .wTotalLength[0] = sizeof(ehci_confd), 2988 .bNumInterface = 1, 2989 .bConfigurationValue = 1, 2990 .iConfiguration = 0, 2991 .bmAttributes = UC_SELF_POWERED, 2992 .bMaxPower = 0 /* max power */ 2993 }, 2994 .ifcd = { 2995 .bLength = sizeof(struct usb_interface_descriptor), 2996 .bDescriptorType = UDESC_INTERFACE, 2997 .bNumEndpoints = 1, 2998 .bInterfaceClass = UICLASS_HUB, 2999 .bInterfaceSubClass = UISUBCLASS_HUB, 3000 .bInterfaceProtocol = 0, 3001 }, 3002 .endpd = { 3003 .bLength = sizeof(struct usb_endpoint_descriptor), 3004 .bDescriptorType = UDESC_ENDPOINT, 3005 .bEndpointAddress = UE_DIR_IN | EHCI_INTR_ENDPT, 3006 .bmAttributes = UE_INTERRUPT, 3007 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */ 3008 .bInterval = 255, 3009 }, 3010 }; 3011 3012 static const 3013 struct usb_hub_descriptor ehci_hubd = 3014 { 3015 0, /* dynamic length */ 3016 UDESC_HUB, 3017 0, 3018 {0, 0}, 3019 0, 3020 0, 3021 {0}, 3022 }; 3023 3024 static void 3025 ehci_disown(ehci_softc_t *sc, uint16_t index, uint8_t lowspeed) 3026 { 3027 uint32_t port; 3028 uint32_t v; 3029 3030 DPRINTF("index=%d lowspeed=%d\n", index, lowspeed); 3031 3032 port = EHCI_PORTSC(index); 3033 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR; 3034 EOWRITE4(sc, port, v | EHCI_PS_PO); 3035 } 3036 3037 static usb_error_t 3038 ehci_roothub_exec(struct usb_device *udev, 3039 struct usb_device_request *req, const void **pptr, uint16_t *plength) 3040 { 3041 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus); 3042 const char *str_ptr; 3043 const void *ptr; 3044 uint32_t port; 3045 uint32_t v; 3046 uint16_t len; 3047 uint16_t i; 3048 uint16_t value; 3049 uint16_t index; 3050 usb_error_t err; 3051 3052 USB_BUS_LOCK_ASSERT(&sc->sc_bus); 3053 3054 /* buffer reset */ 3055 ptr = (const void *)&sc->sc_hub_desc; 3056 len = 0; 3057 err = 0; 3058 3059 value = UGETW(req->wValue); 3060 index = UGETW(req->wIndex); 3061 3062 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x " 3063 "wValue=0x%04x wIndex=0x%04x\n", 3064 req->bmRequestType, req->bRequest, 3065 UGETW(req->wLength), value, index); 3066 3067 #define C(x,y) ((x) | ((y) << 8)) 3068 switch (C(req->bRequest, req->bmRequestType)) { 3069 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 3070 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 3071 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 3072 /* 3073 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 3074 * for the integrated root hub. 3075 */ 3076 break; 3077 case C(UR_GET_CONFIG, UT_READ_DEVICE): 3078 len = 1; 3079 sc->sc_hub_desc.temp[0] = sc->sc_conf; 3080 break; 3081 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 3082 switch (value >> 8) { 3083 case UDESC_DEVICE: 3084 if ((value & 0xff) != 0) { 3085 err = USB_ERR_IOERROR; 3086 goto done; 3087 } 3088 len = sizeof(ehci_devd); 3089 ptr = (const void *)&ehci_devd; 3090 break; 3091 /* 3092 * We can't really operate at another speed, 3093 * but the specification says we need this 3094 * descriptor: 3095 */ 3096 case UDESC_DEVICE_QUALIFIER: 3097 if ((value & 0xff) != 0) { 3098 err = USB_ERR_IOERROR; 3099 goto done; 3100 } 3101 len = sizeof(ehci_odevd); 3102 ptr = (const void *)&ehci_odevd; 3103 break; 3104 3105 case UDESC_CONFIG: 3106 if ((value & 0xff) != 0) { 3107 err = USB_ERR_IOERROR; 3108 goto done; 3109 } 3110 len = sizeof(ehci_confd); 3111 ptr = (const void *)&ehci_confd; 3112 break; 3113 3114 case UDESC_STRING: 3115 switch (value & 0xff) { 3116 case 0: /* Language table */ 3117 str_ptr = "\001"; 3118 break; 3119 3120 case 1: /* Vendor */ 3121 str_ptr = sc->sc_vendor; 3122 break; 3123 3124 case 2: /* Product */ 3125 str_ptr = "EHCI root HUB"; 3126 break; 3127 3128 default: 3129 str_ptr = ""; 3130 break; 3131 } 3132 3133 len = usb_make_str_desc( 3134 sc->sc_hub_desc.temp, 3135 sizeof(sc->sc_hub_desc.temp), 3136 str_ptr); 3137 break; 3138 default: 3139 err = USB_ERR_IOERROR; 3140 goto done; 3141 } 3142 break; 3143 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 3144 len = 1; 3145 sc->sc_hub_desc.temp[0] = 0; 3146 break; 3147 case C(UR_GET_STATUS, UT_READ_DEVICE): 3148 len = 2; 3149 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED); 3150 break; 3151 case C(UR_GET_STATUS, UT_READ_INTERFACE): 3152 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 3153 len = 2; 3154 USETW(sc->sc_hub_desc.stat.wStatus, 0); 3155 break; 3156 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 3157 if (value >= EHCI_MAX_DEVICES) { 3158 err = USB_ERR_IOERROR; 3159 goto done; 3160 } 3161 sc->sc_addr = value; 3162 break; 3163 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 3164 if ((value != 0) && (value != 1)) { 3165 err = USB_ERR_IOERROR; 3166 goto done; 3167 } 3168 sc->sc_conf = value; 3169 break; 3170 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 3171 break; 3172 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 3173 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 3174 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 3175 err = USB_ERR_IOERROR; 3176 goto done; 3177 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 3178 break; 3179 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 3180 break; 3181 /* Hub requests */ 3182 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 3183 break; 3184 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 3185 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE\n"); 3186 3187 if ((index < 1) || 3188 (index > sc->sc_noport)) { 3189 err = USB_ERR_IOERROR; 3190 goto done; 3191 } 3192 port = EHCI_PORTSC(index); 3193 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR; 3194 switch (value) { 3195 case UHF_PORT_ENABLE: 3196 EOWRITE4(sc, port, v & ~EHCI_PS_PE); 3197 break; 3198 case UHF_PORT_SUSPEND: 3199 if ((v & EHCI_PS_SUSP) && (!(v & EHCI_PS_FPR))) { 3200 3201 /* 3202 * waking up a High Speed device is rather 3203 * complicated if 3204 */ 3205 EOWRITE4(sc, port, v | EHCI_PS_FPR); 3206 } 3207 /* wait 20ms for resume sequence to complete */ 3208 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 50); 3209 3210 EOWRITE4(sc, port, v & ~(EHCI_PS_SUSP | 3211 EHCI_PS_FPR | (3 << 10) /* High Speed */ )); 3212 3213 /* 4ms settle time */ 3214 usb_pause_mtx(&sc->sc_bus.bus_lock, hz / 250); 3215 break; 3216 case UHF_PORT_POWER: 3217 EOWRITE4(sc, port, v & ~EHCI_PS_PP); 3218 break; 3219 case UHF_PORT_TEST: 3220 DPRINTFN(3, "clear port test " 3221 "%d\n", index); 3222 break; 3223 case UHF_PORT_INDICATOR: 3224 DPRINTFN(3, "clear port ind " 3225 "%d\n", index); 3226 EOWRITE4(sc, port, v & ~EHCI_PS_PIC); 3227 break; 3228 case UHF_C_PORT_CONNECTION: 3229 EOWRITE4(sc, port, v | EHCI_PS_CSC); 3230 break; 3231 case UHF_C_PORT_ENABLE: 3232 EOWRITE4(sc, port, v | EHCI_PS_PEC); 3233 break; 3234 case UHF_C_PORT_SUSPEND: 3235 EOWRITE4(sc, port, v | EHCI_PS_SUSP); 3236 break; 3237 case UHF_C_PORT_OVER_CURRENT: 3238 EOWRITE4(sc, port, v | EHCI_PS_OCC); 3239 break; 3240 case UHF_C_PORT_RESET: 3241 sc->sc_isreset = 0; 3242 break; 3243 default: 3244 err = USB_ERR_IOERROR; 3245 goto done; 3246 } 3247 break; 3248 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 3249 if ((value & 0xff) != 0) { 3250 err = USB_ERR_IOERROR; 3251 goto done; 3252 } 3253 v = EREAD4(sc, EHCI_HCSPARAMS); 3254 3255 sc->sc_hub_desc.hubd = ehci_hubd; 3256 sc->sc_hub_desc.hubd.bNbrPorts = sc->sc_noport; 3257 3258 if (EHCI_HCS_PPC(v)) 3259 i = UHD_PWR_INDIVIDUAL; 3260 else 3261 i = UHD_PWR_NO_SWITCH; 3262 3263 if (EHCI_HCS_P_INDICATOR(v)) 3264 i |= UHD_PORT_IND; 3265 3266 USETW(sc->sc_hub_desc.hubd.wHubCharacteristics, i); 3267 /* XXX can't find out? */ 3268 sc->sc_hub_desc.hubd.bPwrOn2PwrGood = 200; 3269 /* XXX don't know if ports are removable or not */ 3270 sc->sc_hub_desc.hubd.bDescLength = 3271 8 + ((sc->sc_noport + 7) / 8); 3272 len = sc->sc_hub_desc.hubd.bDescLength; 3273 break; 3274 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 3275 len = 16; 3276 memset(sc->sc_hub_desc.temp, 0, 16); 3277 break; 3278 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 3279 DPRINTFN(9, "get port status i=%d\n", 3280 index); 3281 if ((index < 1) || 3282 (index > sc->sc_noport)) { 3283 err = USB_ERR_IOERROR; 3284 goto done; 3285 } 3286 v = EOREAD4(sc, EHCI_PORTSC(index)); 3287 DPRINTFN(9, "port status=0x%04x\n", v); 3288 if (sc->sc_flags & (EHCI_SCFLG_FORCESPEED | EHCI_SCFLG_TT)) { 3289 if ((v & 0xc000000) == 0x8000000) 3290 i = UPS_HIGH_SPEED; 3291 else if ((v & 0xc000000) == 0x4000000) 3292 i = UPS_LOW_SPEED; 3293 else 3294 i = 0; 3295 } else { 3296 i = UPS_HIGH_SPEED; 3297 } 3298 if (v & EHCI_PS_CS) 3299 i |= UPS_CURRENT_CONNECT_STATUS; 3300 if (v & EHCI_PS_PE) 3301 i |= UPS_PORT_ENABLED; 3302 if ((v & EHCI_PS_SUSP) && !(v & EHCI_PS_FPR)) 3303 i |= UPS_SUSPEND; 3304 if (v & EHCI_PS_OCA) 3305 i |= UPS_OVERCURRENT_INDICATOR; 3306 if (v & EHCI_PS_PR) 3307 i |= UPS_RESET; 3308 if (v & EHCI_PS_PP) 3309 i |= UPS_PORT_POWER; 3310 USETW(sc->sc_hub_desc.ps.wPortStatus, i); 3311 i = 0; 3312 if (v & EHCI_PS_CSC) 3313 i |= UPS_C_CONNECT_STATUS; 3314 if (v & EHCI_PS_PEC) 3315 i |= UPS_C_PORT_ENABLED; 3316 if (v & EHCI_PS_OCC) 3317 i |= UPS_C_OVERCURRENT_INDICATOR; 3318 if (v & EHCI_PS_FPR) 3319 i |= UPS_C_SUSPEND; 3320 if (sc->sc_isreset) 3321 i |= UPS_C_PORT_RESET; 3322 USETW(sc->sc_hub_desc.ps.wPortChange, i); 3323 len = sizeof(sc->sc_hub_desc.ps); 3324 break; 3325 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 3326 err = USB_ERR_IOERROR; 3327 goto done; 3328 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 3329 break; 3330 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 3331 if ((index < 1) || 3332 (index > sc->sc_noport)) { 3333 err = USB_ERR_IOERROR; 3334 goto done; 3335 } 3336 port = EHCI_PORTSC(index); 3337 v = EOREAD4(sc, port) & ~EHCI_PS_CLEAR; 3338 switch (value) { 3339 case UHF_PORT_ENABLE: 3340 EOWRITE4(sc, port, v | EHCI_PS_PE); 3341 break; 3342 case UHF_PORT_SUSPEND: 3343 EOWRITE4(sc, port, v | EHCI_PS_SUSP); 3344 break; 3345 case UHF_PORT_RESET: 3346 DPRINTFN(6, "reset port %d\n", index); 3347 #ifdef USB_DEBUG 3348 if (ehcinohighspeed) { 3349 /* 3350 * Connect USB device to companion 3351 * controller. 3352 */ 3353 ehci_disown(sc, index, 1); 3354 break; 3355 } 3356 #endif 3357 if (EHCI_PS_IS_LOWSPEED(v) && 3358 (sc->sc_flags & EHCI_SCFLG_TT) == 0) { 3359 /* Low speed device, give up ownership. */ 3360 ehci_disown(sc, index, 1); 3361 break; 3362 } 3363 /* Start reset sequence. */ 3364 v &= ~(EHCI_PS_PE | EHCI_PS_PR); 3365 EOWRITE4(sc, port, v | EHCI_PS_PR); 3366 3367 /* Wait for reset to complete. */ 3368 usb_pause_mtx(&sc->sc_bus.bus_lock, 3369 USB_MS_TO_TICKS(USB_PORT_ROOT_RESET_DELAY)); 3370 3371 /* Terminate reset sequence. */ 3372 if (!(sc->sc_flags & EHCI_SCFLG_NORESTERM)) 3373 EOWRITE4(sc, port, v); 3374 3375 /* Wait for HC to complete reset. */ 3376 usb_pause_mtx(&sc->sc_bus.bus_lock, 3377 USB_MS_TO_TICKS(EHCI_PORT_RESET_COMPLETE)); 3378 3379 v = EOREAD4(sc, port); 3380 DPRINTF("ehci after reset, status=0x%08x\n", v); 3381 if (v & EHCI_PS_PR) { 3382 device_printf(sc->sc_bus.bdev, 3383 "port reset timeout\n"); 3384 err = USB_ERR_TIMEOUT; 3385 goto done; 3386 } 3387 if (!(v & EHCI_PS_PE) && 3388 (sc->sc_flags & EHCI_SCFLG_TT) == 0) { 3389 /* Not a high speed device, give up ownership.*/ 3390 ehci_disown(sc, index, 0); 3391 break; 3392 } 3393 sc->sc_isreset = 1; 3394 DPRINTF("ehci port %d reset, status = 0x%08x\n", 3395 index, v); 3396 break; 3397 3398 case UHF_PORT_POWER: 3399 DPRINTFN(3, "set port power %d\n", index); 3400 EOWRITE4(sc, port, v | EHCI_PS_PP); 3401 break; 3402 3403 case UHF_PORT_TEST: 3404 DPRINTFN(3, "set port test %d\n", index); 3405 break; 3406 3407 case UHF_PORT_INDICATOR: 3408 DPRINTFN(3, "set port ind %d\n", index); 3409 EOWRITE4(sc, port, v | EHCI_PS_PIC); 3410 break; 3411 3412 default: 3413 err = USB_ERR_IOERROR; 3414 goto done; 3415 } 3416 break; 3417 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER): 3418 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER): 3419 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER): 3420 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER): 3421 break; 3422 default: 3423 err = USB_ERR_IOERROR; 3424 goto done; 3425 } 3426 done: 3427 *plength = len; 3428 *pptr = ptr; 3429 return (err); 3430 } 3431 3432 static void 3433 ehci_xfer_setup(struct usb_setup_params *parm) 3434 { 3435 struct usb_page_search page_info; 3436 struct usb_page_cache *pc; 3437 ehci_softc_t *sc; 3438 struct usb_xfer *xfer; 3439 void *last_obj; 3440 uint32_t nqtd; 3441 uint32_t nqh; 3442 uint32_t nsitd; 3443 uint32_t nitd; 3444 uint32_t n; 3445 3446 sc = EHCI_BUS2SC(parm->udev->bus); 3447 xfer = parm->curr_xfer; 3448 3449 nqtd = 0; 3450 nqh = 0; 3451 nsitd = 0; 3452 nitd = 0; 3453 3454 /* 3455 * compute maximum number of some structures 3456 */ 3457 if (parm->methods == &ehci_device_ctrl_methods) { 3458 3459 /* 3460 * The proof for the "nqtd" formula is illustrated like 3461 * this: 3462 * 3463 * +------------------------------------+ 3464 * | | 3465 * | |remainder -> | 3466 * | +-----+---+ | 3467 * | | xxx | x | frm 0 | 3468 * | +-----+---++ | 3469 * | | xxx | xx | frm 1 | 3470 * | +-----+----+ | 3471 * | ... | 3472 * +------------------------------------+ 3473 * 3474 * "xxx" means a completely full USB transfer descriptor 3475 * 3476 * "x" and "xx" means a short USB packet 3477 * 3478 * For the remainder of an USB transfer modulo 3479 * "max_data_length" we need two USB transfer descriptors. 3480 * One to transfer the remaining data and one to finalise 3481 * with a zero length packet in case the "force_short_xfer" 3482 * flag is set. We only need two USB transfer descriptors in 3483 * the case where the transfer length of the first one is a 3484 * factor of "max_frame_size". The rest of the needed USB 3485 * transfer descriptors is given by the buffer size divided 3486 * by the maximum data payload. 3487 */ 3488 parm->hc_max_packet_size = 0x400; 3489 parm->hc_max_packet_count = 1; 3490 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX; 3491 xfer->flags_int.bdma_enable = 1; 3492 3493 usbd_transfer_setup_sub(parm); 3494 3495 nqh = 1; 3496 nqtd = ((2 * xfer->nframes) + 1 /* STATUS */ 3497 + (xfer->max_data_length / xfer->max_hc_frame_size)); 3498 3499 } else if (parm->methods == &ehci_device_bulk_methods) { 3500 3501 parm->hc_max_packet_size = 0x400; 3502 parm->hc_max_packet_count = 1; 3503 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX; 3504 xfer->flags_int.bdma_enable = 1; 3505 3506 usbd_transfer_setup_sub(parm); 3507 3508 nqh = 1; 3509 nqtd = ((2 * xfer->nframes) 3510 + (xfer->max_data_length / xfer->max_hc_frame_size)); 3511 3512 } else if (parm->methods == &ehci_device_intr_methods) { 3513 3514 if (parm->speed == USB_SPEED_HIGH) { 3515 parm->hc_max_packet_size = 0x400; 3516 parm->hc_max_packet_count = 3; 3517 } else if (parm->speed == USB_SPEED_FULL) { 3518 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME; 3519 parm->hc_max_packet_count = 1; 3520 } else { 3521 parm->hc_max_packet_size = USB_FS_BYTES_PER_HS_UFRAME / 8; 3522 parm->hc_max_packet_count = 1; 3523 } 3524 3525 parm->hc_max_frame_size = EHCI_QTD_PAYLOAD_MAX; 3526 xfer->flags_int.bdma_enable = 1; 3527 3528 usbd_transfer_setup_sub(parm); 3529 3530 nqh = 1; 3531 nqtd = ((2 * xfer->nframes) 3532 + (xfer->max_data_length / xfer->max_hc_frame_size)); 3533 3534 } else if (parm->methods == &ehci_device_isoc_fs_methods) { 3535 3536 parm->hc_max_packet_size = 0x3FF; 3537 parm->hc_max_packet_count = 1; 3538 parm->hc_max_frame_size = 0x3FF; 3539 xfer->flags_int.bdma_enable = 1; 3540 3541 usbd_transfer_setup_sub(parm); 3542 3543 nsitd = xfer->nframes; 3544 3545 } else if (parm->methods == &ehci_device_isoc_hs_methods) { 3546 3547 parm->hc_max_packet_size = 0x400; 3548 parm->hc_max_packet_count = 3; 3549 parm->hc_max_frame_size = 0xC00; 3550 xfer->flags_int.bdma_enable = 1; 3551 3552 usbd_transfer_setup_sub(parm); 3553 3554 nitd = ((xfer->nframes + 7) / 8) << 3555 usbd_xfer_get_fps_shift(xfer); 3556 3557 } else { 3558 3559 parm->hc_max_packet_size = 0x400; 3560 parm->hc_max_packet_count = 1; 3561 parm->hc_max_frame_size = 0x400; 3562 3563 usbd_transfer_setup_sub(parm); 3564 } 3565 3566 alloc_dma_set: 3567 3568 if (parm->err) { 3569 return; 3570 } 3571 /* 3572 * Allocate queue heads and transfer descriptors 3573 */ 3574 last_obj = NULL; 3575 3576 if (usbd_transfer_setup_sub_malloc( 3577 parm, &pc, sizeof(ehci_itd_t), 3578 EHCI_ITD_ALIGN, nitd)) { 3579 parm->err = USB_ERR_NOMEM; 3580 return; 3581 } 3582 if (parm->buf) { 3583 for (n = 0; n != nitd; n++) { 3584 ehci_itd_t *td; 3585 3586 usbd_get_page(pc + n, 0, &page_info); 3587 3588 td = page_info.buffer; 3589 3590 /* init TD */ 3591 td->itd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_ITD); 3592 td->obj_next = last_obj; 3593 td->page_cache = pc + n; 3594 3595 last_obj = td; 3596 3597 usb_pc_cpu_flush(pc + n); 3598 } 3599 } 3600 if (usbd_transfer_setup_sub_malloc( 3601 parm, &pc, sizeof(ehci_sitd_t), 3602 EHCI_SITD_ALIGN, nsitd)) { 3603 parm->err = USB_ERR_NOMEM; 3604 return; 3605 } 3606 if (parm->buf) { 3607 for (n = 0; n != nsitd; n++) { 3608 ehci_sitd_t *td; 3609 3610 usbd_get_page(pc + n, 0, &page_info); 3611 3612 td = page_info.buffer; 3613 3614 /* init TD */ 3615 td->sitd_self = htohc32(sc, page_info.physaddr | EHCI_LINK_SITD); 3616 td->obj_next = last_obj; 3617 td->page_cache = pc + n; 3618 3619 last_obj = td; 3620 3621 usb_pc_cpu_flush(pc + n); 3622 } 3623 } 3624 if (usbd_transfer_setup_sub_malloc( 3625 parm, &pc, sizeof(ehci_qtd_t), 3626 EHCI_QTD_ALIGN, nqtd)) { 3627 parm->err = USB_ERR_NOMEM; 3628 return; 3629 } 3630 if (parm->buf) { 3631 for (n = 0; n != nqtd; n++) { 3632 ehci_qtd_t *qtd; 3633 3634 usbd_get_page(pc + n, 0, &page_info); 3635 3636 qtd = page_info.buffer; 3637 3638 /* init TD */ 3639 qtd->qtd_self = htohc32(sc, page_info.physaddr); 3640 qtd->obj_next = last_obj; 3641 qtd->page_cache = pc + n; 3642 3643 last_obj = qtd; 3644 3645 usb_pc_cpu_flush(pc + n); 3646 } 3647 } 3648 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj; 3649 3650 last_obj = NULL; 3651 3652 if (usbd_transfer_setup_sub_malloc( 3653 parm, &pc, sizeof(ehci_qh_t), 3654 EHCI_QH_ALIGN, nqh)) { 3655 parm->err = USB_ERR_NOMEM; 3656 return; 3657 } 3658 if (parm->buf) { 3659 for (n = 0; n != nqh; n++) { 3660 ehci_qh_t *qh; 3661 3662 usbd_get_page(pc + n, 0, &page_info); 3663 3664 qh = page_info.buffer; 3665 3666 /* init QH */ 3667 qh->qh_self = htohc32(sc, page_info.physaddr | EHCI_LINK_QH); 3668 qh->obj_next = last_obj; 3669 qh->page_cache = pc + n; 3670 3671 last_obj = qh; 3672 3673 usb_pc_cpu_flush(pc + n); 3674 } 3675 } 3676 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj; 3677 3678 if (!xfer->flags_int.curr_dma_set) { 3679 xfer->flags_int.curr_dma_set = 1; 3680 goto alloc_dma_set; 3681 } 3682 } 3683 3684 static void 3685 ehci_xfer_unsetup(struct usb_xfer *xfer) 3686 { 3687 return; 3688 } 3689 3690 static void 3691 ehci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc, 3692 struct usb_endpoint *ep) 3693 { 3694 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus); 3695 3696 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 3697 ep, udev->address, 3698 edesc->bEndpointAddress, udev->flags.usb_mode, 3699 sc->sc_addr); 3700 3701 if (udev->flags.usb_mode != USB_MODE_HOST) { 3702 /* not supported */ 3703 return; 3704 } 3705 if (udev->device_index != sc->sc_addr) { 3706 3707 if ((udev->speed != USB_SPEED_HIGH) && 3708 ((udev->hs_hub_addr == 0) || 3709 (udev->hs_port_no == 0) || 3710 (udev->parent_hs_hub == NULL) || 3711 (udev->parent_hs_hub->hub == NULL))) { 3712 /* We need a transaction translator */ 3713 goto done; 3714 } 3715 switch (edesc->bmAttributes & UE_XFERTYPE) { 3716 case UE_CONTROL: 3717 ep->methods = &ehci_device_ctrl_methods; 3718 break; 3719 case UE_INTERRUPT: 3720 ep->methods = &ehci_device_intr_methods; 3721 break; 3722 case UE_ISOCHRONOUS: 3723 if (udev->speed == USB_SPEED_HIGH) { 3724 ep->methods = &ehci_device_isoc_hs_methods; 3725 } else if (udev->speed == USB_SPEED_FULL) { 3726 ep->methods = &ehci_device_isoc_fs_methods; 3727 } 3728 break; 3729 case UE_BULK: 3730 ep->methods = &ehci_device_bulk_methods; 3731 break; 3732 default: 3733 /* do nothing */ 3734 break; 3735 } 3736 } 3737 done: 3738 return; 3739 } 3740 3741 static void 3742 ehci_get_dma_delay(struct usb_device *udev, uint32_t *pus) 3743 { 3744 /* 3745 * Wait until the hardware has finished any possible use of 3746 * the transfer descriptor(s) and QH 3747 */ 3748 *pus = (188); /* microseconds */ 3749 } 3750 3751 static void 3752 ehci_device_resume(struct usb_device *udev) 3753 { 3754 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus); 3755 struct usb_xfer *xfer; 3756 struct usb_pipe_methods *methods; 3757 3758 DPRINTF("\n"); 3759 3760 USB_BUS_LOCK(udev->bus); 3761 3762 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 3763 3764 if (xfer->xroot->udev == udev) { 3765 3766 methods = xfer->endpoint->methods; 3767 3768 if ((methods == &ehci_device_bulk_methods) || 3769 (methods == &ehci_device_ctrl_methods)) { 3770 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 3771 sc->sc_async_p_last); 3772 } 3773 if (methods == &ehci_device_intr_methods) { 3774 EHCI_APPEND_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 3775 sc->sc_intr_p_last[xfer->qh_pos]); 3776 } 3777 } 3778 } 3779 3780 USB_BUS_UNLOCK(udev->bus); 3781 3782 return; 3783 } 3784 3785 static void 3786 ehci_device_suspend(struct usb_device *udev) 3787 { 3788 ehci_softc_t *sc = EHCI_BUS2SC(udev->bus); 3789 struct usb_xfer *xfer; 3790 struct usb_pipe_methods *methods; 3791 3792 DPRINTF("\n"); 3793 3794 USB_BUS_LOCK(udev->bus); 3795 3796 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 3797 3798 if (xfer->xroot->udev == udev) { 3799 3800 methods = xfer->endpoint->methods; 3801 3802 if ((methods == &ehci_device_bulk_methods) || 3803 (methods == &ehci_device_ctrl_methods)) { 3804 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 3805 sc->sc_async_p_last); 3806 } 3807 if (methods == &ehci_device_intr_methods) { 3808 EHCI_REMOVE_QH(xfer->qh_start[xfer->flags_int.curr_dma_set], 3809 sc->sc_intr_p_last[xfer->qh_pos]); 3810 } 3811 } 3812 } 3813 3814 USB_BUS_UNLOCK(udev->bus); 3815 } 3816 3817 static void 3818 ehci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state) 3819 { 3820 struct ehci_softc *sc = EHCI_BUS2SC(bus); 3821 3822 switch (state) { 3823 case USB_HW_POWER_SUSPEND: 3824 case USB_HW_POWER_SHUTDOWN: 3825 ehci_suspend(sc); 3826 break; 3827 case USB_HW_POWER_RESUME: 3828 ehci_resume(sc); 3829 break; 3830 default: 3831 break; 3832 } 3833 } 3834 3835 static void 3836 ehci_set_hw_power(struct usb_bus *bus) 3837 { 3838 ehci_softc_t *sc = EHCI_BUS2SC(bus); 3839 uint32_t temp; 3840 uint32_t flags; 3841 3842 DPRINTF("\n"); 3843 3844 USB_BUS_LOCK(bus); 3845 3846 flags = bus->hw_power_state; 3847 3848 temp = EOREAD4(sc, EHCI_USBCMD); 3849 3850 temp &= ~(EHCI_CMD_ASE | EHCI_CMD_PSE); 3851 3852 if (flags & (USB_HW_POWER_CONTROL | 3853 USB_HW_POWER_BULK)) { 3854 DPRINTF("Async is active\n"); 3855 temp |= EHCI_CMD_ASE; 3856 } 3857 if (flags & (USB_HW_POWER_INTERRUPT | 3858 USB_HW_POWER_ISOC)) { 3859 DPRINTF("Periodic is active\n"); 3860 temp |= EHCI_CMD_PSE; 3861 } 3862 EOWRITE4(sc, EHCI_USBCMD, temp); 3863 3864 USB_BUS_UNLOCK(bus); 3865 3866 return; 3867 } 3868 3869 struct usb_bus_methods ehci_bus_methods = 3870 { 3871 .endpoint_init = ehci_ep_init, 3872 .xfer_setup = ehci_xfer_setup, 3873 .xfer_unsetup = ehci_xfer_unsetup, 3874 .get_dma_delay = ehci_get_dma_delay, 3875 .device_resume = ehci_device_resume, 3876 .device_suspend = ehci_device_suspend, 3877 .set_hw_power = ehci_set_hw_power, 3878 .set_hw_power_sleep = ehci_set_hw_power_sleep, 3879 .roothub_exec = ehci_roothub_exec, 3880 .xfer_poll = ehci_do_poll, 3881 }; 3882