1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved. 5 * Copyright (c) 1998 Lennart Augustsson. 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 Universal Host Controller driver. 31 * Handles e.g. PIIX3 and PIIX4. 32 * 33 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm 34 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 35 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf 36 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf 37 */ 38 39 #ifdef USB_GLOBAL_INCLUDE_FILE 40 #include USB_GLOBAL_INCLUDE_FILE 41 #else 42 #include <sys/stdint.h> 43 #include <sys/stddef.h> 44 #include <sys/param.h> 45 #include <sys/queue.h> 46 #include <sys/types.h> 47 #include <sys/systm.h> 48 #include <sys/kernel.h> 49 #include <sys/bus.h> 50 #include <sys/module.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/condvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/sx.h> 56 #include <sys/unistd.h> 57 #include <sys/callout.h> 58 #include <sys/malloc.h> 59 #include <sys/priv.h> 60 61 #include <dev/usb/usb.h> 62 #include <dev/usb/usbdi.h> 63 64 #define USB_DEBUG_VAR uhcidebug 65 66 #include <dev/usb/usb_core.h> 67 #include <dev/usb/usb_debug.h> 68 #include <dev/usb/usb_busdma.h> 69 #include <dev/usb/usb_process.h> 70 #include <dev/usb/usb_transfer.h> 71 #include <dev/usb/usb_device.h> 72 #include <dev/usb/usb_hub.h> 73 #include <dev/usb/usb_util.h> 74 75 #include <dev/usb/usb_controller.h> 76 #include <dev/usb/usb_bus.h> 77 #endif /* USB_GLOBAL_INCLUDE_FILE */ 78 79 #include <dev/usb/controller/uhci.h> 80 #include <dev/usb/controller/uhcireg.h> 81 82 #define alt_next next 83 #define UHCI_BUS2SC(bus) \ 84 ((uhci_softc_t *)(((uint8_t *)(bus)) - \ 85 ((uint8_t *)&(((uhci_softc_t *)0)->sc_bus)))) 86 87 #ifdef USB_DEBUG 88 static int uhcidebug = 0; 89 static int uhcinoloop = 0; 90 91 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhci, CTLFLAG_RW, 0, "USB uhci"); 92 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, debug, CTLFLAG_RWTUN, 93 &uhcidebug, 0, "uhci debug level"); 94 SYSCTL_INT(_hw_usb_uhci, OID_AUTO, loop, CTLFLAG_RWTUN, 95 &uhcinoloop, 0, "uhci noloop"); 96 97 static void uhci_dumpregs(uhci_softc_t *sc); 98 static void uhci_dump_tds(uhci_td_t *td); 99 100 #endif 101 102 #define UBARR(sc) bus_space_barrier((sc)->sc_io_tag, (sc)->sc_io_hdl, 0, (sc)->sc_io_size, \ 103 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 104 #define UWRITE1(sc, r, x) \ 105 do { UBARR(sc); bus_space_write_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \ 106 } while (/*CONSTCOND*/0) 107 #define UWRITE2(sc, r, x) \ 108 do { UBARR(sc); bus_space_write_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \ 109 } while (/*CONSTCOND*/0) 110 #define UWRITE4(sc, r, x) \ 111 do { UBARR(sc); bus_space_write_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r), (x)); \ 112 } while (/*CONSTCOND*/0) 113 #define UREAD1(sc, r) (UBARR(sc), bus_space_read_1((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 114 #define UREAD2(sc, r) (UBARR(sc), bus_space_read_2((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 115 #define UREAD4(sc, r) (UBARR(sc), bus_space_read_4((sc)->sc_io_tag, (sc)->sc_io_hdl, (r))) 116 117 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd) 118 #define UHCISTS(sc) UREAD2(sc, UHCI_STS) 119 120 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */ 121 122 #define UHCI_INTR_ENDPT 1 123 124 struct uhci_mem_layout { 125 126 struct usb_page_search buf_res; 127 struct usb_page_search fix_res; 128 129 struct usb_page_cache *buf_pc; 130 struct usb_page_cache *fix_pc; 131 132 uint32_t buf_offset; 133 134 uint16_t max_frame_size; 135 }; 136 137 struct uhci_std_temp { 138 139 struct uhci_mem_layout ml; 140 uhci_td_t *td; 141 uhci_td_t *td_next; 142 uint32_t average; 143 uint32_t td_status; 144 uint32_t td_token; 145 uint32_t len; 146 uint16_t max_frame_size; 147 uint8_t shortpkt; 148 uint8_t setup_alt_next; 149 uint8_t last_frame; 150 }; 151 152 static const struct usb_bus_methods uhci_bus_methods; 153 static const struct usb_pipe_methods uhci_device_bulk_methods; 154 static const struct usb_pipe_methods uhci_device_ctrl_methods; 155 static const struct usb_pipe_methods uhci_device_intr_methods; 156 static const struct usb_pipe_methods uhci_device_isoc_methods; 157 158 static uint8_t uhci_restart(uhci_softc_t *sc); 159 static void uhci_do_poll(struct usb_bus *); 160 static void uhci_device_done(struct usb_xfer *, usb_error_t); 161 static void uhci_transfer_intr_enqueue(struct usb_xfer *); 162 static void uhci_timeout(void *); 163 static uint8_t uhci_check_transfer(struct usb_xfer *); 164 static void uhci_root_intr(uhci_softc_t *sc); 165 166 void 167 uhci_iterate_hw_softc(struct usb_bus *bus, usb_bus_mem_sub_cb_t *cb) 168 { 169 struct uhci_softc *sc = UHCI_BUS2SC(bus); 170 uint32_t i; 171 172 cb(bus, &sc->sc_hw.pframes_pc, &sc->sc_hw.pframes_pg, 173 sizeof(uint32_t) * UHCI_FRAMELIST_COUNT, UHCI_FRAMELIST_ALIGN); 174 175 cb(bus, &sc->sc_hw.ls_ctl_start_pc, &sc->sc_hw.ls_ctl_start_pg, 176 sizeof(uhci_qh_t), UHCI_QH_ALIGN); 177 178 cb(bus, &sc->sc_hw.fs_ctl_start_pc, &sc->sc_hw.fs_ctl_start_pg, 179 sizeof(uhci_qh_t), UHCI_QH_ALIGN); 180 181 cb(bus, &sc->sc_hw.bulk_start_pc, &sc->sc_hw.bulk_start_pg, 182 sizeof(uhci_qh_t), UHCI_QH_ALIGN); 183 184 cb(bus, &sc->sc_hw.last_qh_pc, &sc->sc_hw.last_qh_pg, 185 sizeof(uhci_qh_t), UHCI_QH_ALIGN); 186 187 cb(bus, &sc->sc_hw.last_td_pc, &sc->sc_hw.last_td_pg, 188 sizeof(uhci_td_t), UHCI_TD_ALIGN); 189 190 for (i = 0; i != UHCI_VFRAMELIST_COUNT; i++) { 191 cb(bus, sc->sc_hw.isoc_start_pc + i, 192 sc->sc_hw.isoc_start_pg + i, 193 sizeof(uhci_td_t), UHCI_TD_ALIGN); 194 } 195 196 for (i = 0; i != UHCI_IFRAMELIST_COUNT; i++) { 197 cb(bus, sc->sc_hw.intr_start_pc + i, 198 sc->sc_hw.intr_start_pg + i, 199 sizeof(uhci_qh_t), UHCI_QH_ALIGN); 200 } 201 } 202 203 static void 204 uhci_mem_layout_init(struct uhci_mem_layout *ml, struct usb_xfer *xfer) 205 { 206 ml->buf_pc = xfer->frbuffers + 0; 207 ml->fix_pc = xfer->buf_fixup; 208 209 ml->buf_offset = 0; 210 211 ml->max_frame_size = xfer->max_frame_size; 212 } 213 214 static void 215 uhci_mem_layout_fixup(struct uhci_mem_layout *ml, struct uhci_td *td) 216 { 217 usbd_get_page(ml->buf_pc, ml->buf_offset, &ml->buf_res); 218 219 if (ml->buf_res.length < td->len) { 220 221 /* need to do a fixup */ 222 223 usbd_get_page(ml->fix_pc, 0, &ml->fix_res); 224 225 td->td_buffer = htole32(ml->fix_res.physaddr); 226 227 /* 228 * The UHCI driver cannot handle 229 * page crossings, so a fixup is 230 * needed: 231 * 232 * +----+----+ - - - 233 * | YYY|Y | 234 * +----+----+ - - - 235 * \ \ 236 * \ \ 237 * +----+ 238 * |YYYY| (fixup) 239 * +----+ 240 */ 241 242 if ((td->td_token & htole32(UHCI_TD_PID)) == 243 htole32(UHCI_TD_PID_IN)) { 244 td->fix_pc = ml->fix_pc; 245 usb_pc_cpu_invalidate(ml->fix_pc); 246 247 } else { 248 td->fix_pc = NULL; 249 250 /* copy data to fixup location */ 251 252 usbd_copy_out(ml->buf_pc, ml->buf_offset, 253 ml->fix_res.buffer, td->len); 254 255 usb_pc_cpu_flush(ml->fix_pc); 256 } 257 258 /* prepare next fixup */ 259 260 ml->fix_pc++; 261 262 } else { 263 264 td->td_buffer = htole32(ml->buf_res.physaddr); 265 td->fix_pc = NULL; 266 } 267 268 /* prepare next data location */ 269 270 ml->buf_offset += td->len; 271 } 272 273 /* 274 * Return values: 275 * 0: Success 276 * Else: Failure 277 */ 278 static uint8_t 279 uhci_restart(uhci_softc_t *sc) 280 { 281 struct usb_page_search buf_res; 282 283 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 284 285 if (UREAD2(sc, UHCI_CMD) & UHCI_CMD_RS) { 286 DPRINTFN(2, "Already started\n"); 287 return (0); 288 } 289 290 DPRINTFN(2, "Restarting\n"); 291 292 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res); 293 294 /* Reload fresh base address */ 295 UWRITE4(sc, UHCI_FLBASEADDR, buf_res.physaddr); 296 297 /* 298 * Assume 64 byte packets at frame end and start HC controller: 299 */ 300 UHCICMD(sc, (UHCI_CMD_MAXP | UHCI_CMD_RS)); 301 302 /* wait 10 milliseconds */ 303 304 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100); 305 306 /* check that controller has started */ 307 308 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) { 309 DPRINTFN(2, "Failed\n"); 310 return (1); 311 } 312 return (0); 313 } 314 315 void 316 uhci_reset(uhci_softc_t *sc) 317 { 318 uint16_t n; 319 320 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 321 322 DPRINTF("resetting the HC\n"); 323 324 /* disable interrupts */ 325 326 UWRITE2(sc, UHCI_INTR, 0); 327 328 /* global reset */ 329 330 UHCICMD(sc, UHCI_CMD_GRESET); 331 332 /* wait */ 333 334 usb_pause_mtx(&sc->sc_bus.bus_mtx, 335 USB_MS_TO_TICKS(USB_BUS_RESET_DELAY)); 336 337 /* terminate all transfers */ 338 339 UHCICMD(sc, UHCI_CMD_HCRESET); 340 341 /* the reset bit goes low when the controller is done */ 342 343 n = UHCI_RESET_TIMEOUT; 344 while (n--) { 345 /* wait one millisecond */ 346 347 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000); 348 349 if (!(UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET)) { 350 goto done_1; 351 } 352 } 353 354 device_printf(sc->sc_bus.bdev, 355 "controller did not reset\n"); 356 357 done_1: 358 359 n = 10; 360 while (n--) { 361 /* wait one millisecond */ 362 363 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000); 364 365 /* check if HC is stopped */ 366 if (UREAD2(sc, UHCI_STS) & UHCI_STS_HCH) { 367 goto done_2; 368 } 369 } 370 371 device_printf(sc->sc_bus.bdev, 372 "controller did not stop\n"); 373 374 done_2: 375 376 /* reset frame number */ 377 UWRITE2(sc, UHCI_FRNUM, 0); 378 /* set default SOF value */ 379 UWRITE1(sc, UHCI_SOF, 0x40); 380 381 USB_BUS_UNLOCK(&sc->sc_bus); 382 383 /* stop root interrupt */ 384 usb_callout_drain(&sc->sc_root_intr); 385 386 USB_BUS_LOCK(&sc->sc_bus); 387 } 388 389 static void 390 uhci_start(uhci_softc_t *sc) 391 { 392 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 393 394 DPRINTFN(2, "enabling\n"); 395 396 /* enable interrupts */ 397 398 UWRITE2(sc, UHCI_INTR, 399 (UHCI_INTR_TOCRCIE | 400 UHCI_INTR_RIE | 401 UHCI_INTR_IOCE | 402 UHCI_INTR_SPIE)); 403 404 if (uhci_restart(sc)) { 405 device_printf(sc->sc_bus.bdev, 406 "cannot start HC controller\n"); 407 } 408 409 /* start root interrupt */ 410 uhci_root_intr(sc); 411 } 412 413 static struct uhci_qh * 414 uhci_init_qh(struct usb_page_cache *pc) 415 { 416 struct usb_page_search buf_res; 417 struct uhci_qh *qh; 418 419 usbd_get_page(pc, 0, &buf_res); 420 421 qh = buf_res.buffer; 422 423 qh->qh_self = 424 htole32(buf_res.physaddr) | 425 htole32(UHCI_PTR_QH); 426 427 qh->page_cache = pc; 428 429 return (qh); 430 } 431 432 static struct uhci_td * 433 uhci_init_td(struct usb_page_cache *pc) 434 { 435 struct usb_page_search buf_res; 436 struct uhci_td *td; 437 438 usbd_get_page(pc, 0, &buf_res); 439 440 td = buf_res.buffer; 441 442 td->td_self = 443 htole32(buf_res.physaddr) | 444 htole32(UHCI_PTR_TD); 445 446 td->page_cache = pc; 447 448 return (td); 449 } 450 451 usb_error_t 452 uhci_init(uhci_softc_t *sc) 453 { 454 uint16_t bit; 455 uint16_t x; 456 uint16_t y; 457 458 DPRINTF("start\n"); 459 460 usb_callout_init_mtx(&sc->sc_root_intr, &sc->sc_bus.bus_mtx, 0); 461 462 #ifdef USB_DEBUG 463 if (uhcidebug > 2) { 464 uhci_dumpregs(sc); 465 } 466 #endif 467 /* 468 * Setup QH's 469 */ 470 sc->sc_ls_ctl_p_last = 471 uhci_init_qh(&sc->sc_hw.ls_ctl_start_pc); 472 473 sc->sc_fs_ctl_p_last = 474 uhci_init_qh(&sc->sc_hw.fs_ctl_start_pc); 475 476 sc->sc_bulk_p_last = 477 uhci_init_qh(&sc->sc_hw.bulk_start_pc); 478 #if 0 479 sc->sc_reclaim_qh_p = 480 sc->sc_fs_ctl_p_last; 481 #else 482 /* setup reclaim looping point */ 483 sc->sc_reclaim_qh_p = 484 sc->sc_bulk_p_last; 485 #endif 486 487 sc->sc_last_qh_p = 488 uhci_init_qh(&sc->sc_hw.last_qh_pc); 489 490 sc->sc_last_td_p = 491 uhci_init_td(&sc->sc_hw.last_td_pc); 492 493 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) { 494 sc->sc_isoc_p_last[x] = 495 uhci_init_td(sc->sc_hw.isoc_start_pc + x); 496 } 497 498 for (x = 0; x != UHCI_IFRAMELIST_COUNT; x++) { 499 sc->sc_intr_p_last[x] = 500 uhci_init_qh(sc->sc_hw.intr_start_pc + x); 501 } 502 503 /* 504 * the QHs are arranged to give poll intervals that are 505 * powers of 2 times 1ms 506 */ 507 bit = UHCI_IFRAMELIST_COUNT / 2; 508 while (bit) { 509 x = bit; 510 while (x & bit) { 511 uhci_qh_t *qh_x; 512 uhci_qh_t *qh_y; 513 514 y = (x ^ bit) | (bit / 2); 515 516 /* 517 * the next QH has half the poll interval 518 */ 519 qh_x = sc->sc_intr_p_last[x]; 520 qh_y = sc->sc_intr_p_last[y]; 521 522 qh_x->h_next = NULL; 523 qh_x->qh_h_next = qh_y->qh_self; 524 qh_x->e_next = NULL; 525 qh_x->qh_e_next = htole32(UHCI_PTR_T); 526 x++; 527 } 528 bit >>= 1; 529 } 530 531 if (1) { 532 uhci_qh_t *qh_ls; 533 uhci_qh_t *qh_intr; 534 535 qh_ls = sc->sc_ls_ctl_p_last; 536 qh_intr = sc->sc_intr_p_last[0]; 537 538 /* start QH for interrupt traffic */ 539 qh_intr->h_next = qh_ls; 540 qh_intr->qh_h_next = qh_ls->qh_self; 541 qh_intr->e_next = 0; 542 qh_intr->qh_e_next = htole32(UHCI_PTR_T); 543 } 544 for (x = 0; x != UHCI_VFRAMELIST_COUNT; x++) { 545 546 uhci_td_t *td_x; 547 uhci_qh_t *qh_intr; 548 549 td_x = sc->sc_isoc_p_last[x]; 550 qh_intr = sc->sc_intr_p_last[x | (UHCI_IFRAMELIST_COUNT / 2)]; 551 552 /* start TD for isochronous traffic */ 553 td_x->next = NULL; 554 td_x->td_next = qh_intr->qh_self; 555 td_x->td_status = htole32(UHCI_TD_IOS); 556 td_x->td_token = htole32(0); 557 td_x->td_buffer = htole32(0); 558 } 559 560 if (1) { 561 uhci_qh_t *qh_ls; 562 uhci_qh_t *qh_fs; 563 564 qh_ls = sc->sc_ls_ctl_p_last; 565 qh_fs = sc->sc_fs_ctl_p_last; 566 567 /* start QH where low speed control traffic will be queued */ 568 qh_ls->h_next = qh_fs; 569 qh_ls->qh_h_next = qh_fs->qh_self; 570 qh_ls->e_next = 0; 571 qh_ls->qh_e_next = htole32(UHCI_PTR_T); 572 } 573 if (1) { 574 uhci_qh_t *qh_ctl; 575 uhci_qh_t *qh_blk; 576 uhci_qh_t *qh_lst; 577 uhci_td_t *td_lst; 578 579 qh_ctl = sc->sc_fs_ctl_p_last; 580 qh_blk = sc->sc_bulk_p_last; 581 582 /* start QH where full speed control traffic will be queued */ 583 qh_ctl->h_next = qh_blk; 584 qh_ctl->qh_h_next = qh_blk->qh_self; 585 qh_ctl->e_next = 0; 586 qh_ctl->qh_e_next = htole32(UHCI_PTR_T); 587 588 qh_lst = sc->sc_last_qh_p; 589 590 /* start QH where bulk traffic will be queued */ 591 qh_blk->h_next = qh_lst; 592 qh_blk->qh_h_next = qh_lst->qh_self; 593 qh_blk->e_next = 0; 594 qh_blk->qh_e_next = htole32(UHCI_PTR_T); 595 596 td_lst = sc->sc_last_td_p; 597 598 /* end QH which is used for looping the QHs */ 599 qh_lst->h_next = 0; 600 qh_lst->qh_h_next = htole32(UHCI_PTR_T); /* end of QH chain */ 601 qh_lst->e_next = td_lst; 602 qh_lst->qh_e_next = td_lst->td_self; 603 604 /* 605 * end TD which hangs from the last QH, to avoid a bug in the PIIX 606 * that makes it run berserk otherwise 607 */ 608 td_lst->next = 0; 609 td_lst->td_next = htole32(UHCI_PTR_T); 610 td_lst->td_status = htole32(0); /* inactive */ 611 td_lst->td_token = htole32(0); 612 td_lst->td_buffer = htole32(0); 613 } 614 if (1) { 615 struct usb_page_search buf_res; 616 uint32_t *pframes; 617 618 usbd_get_page(&sc->sc_hw.pframes_pc, 0, &buf_res); 619 620 pframes = buf_res.buffer; 621 622 623 /* 624 * Setup UHCI framelist 625 * 626 * Execution order: 627 * 628 * pframes -> full speed isochronous -> interrupt QH's -> low 629 * speed control -> full speed control -> bulk transfers 630 * 631 */ 632 633 for (x = 0; x != UHCI_FRAMELIST_COUNT; x++) { 634 pframes[x] = 635 sc->sc_isoc_p_last[x % UHCI_VFRAMELIST_COUNT]->td_self; 636 } 637 } 638 /* flush all cache into memory */ 639 640 usb_bus_mem_flush_all(&sc->sc_bus, &uhci_iterate_hw_softc); 641 642 /* set up the bus struct */ 643 sc->sc_bus.methods = &uhci_bus_methods; 644 645 USB_BUS_LOCK(&sc->sc_bus); 646 /* reset the controller */ 647 uhci_reset(sc); 648 649 /* start the controller */ 650 uhci_start(sc); 651 USB_BUS_UNLOCK(&sc->sc_bus); 652 653 /* catch lost interrupts */ 654 uhci_do_poll(&sc->sc_bus); 655 656 return (0); 657 } 658 659 static void 660 uhci_suspend(uhci_softc_t *sc) 661 { 662 #ifdef USB_DEBUG 663 if (uhcidebug > 2) { 664 uhci_dumpregs(sc); 665 } 666 #endif 667 668 USB_BUS_LOCK(&sc->sc_bus); 669 670 /* stop the controller */ 671 672 uhci_reset(sc); 673 674 /* enter global suspend */ 675 676 UHCICMD(sc, UHCI_CMD_EGSM); 677 678 USB_BUS_UNLOCK(&sc->sc_bus); 679 } 680 681 static void 682 uhci_resume(uhci_softc_t *sc) 683 { 684 USB_BUS_LOCK(&sc->sc_bus); 685 686 /* reset the controller */ 687 688 uhci_reset(sc); 689 690 /* force global resume */ 691 692 UHCICMD(sc, UHCI_CMD_FGR); 693 694 /* and start traffic again */ 695 696 uhci_start(sc); 697 698 USB_BUS_UNLOCK(&sc->sc_bus); 699 700 #ifdef USB_DEBUG 701 if (uhcidebug > 2) 702 uhci_dumpregs(sc); 703 #endif 704 705 /* catch lost interrupts */ 706 uhci_do_poll(&sc->sc_bus); 707 } 708 709 #ifdef USB_DEBUG 710 static void 711 uhci_dumpregs(uhci_softc_t *sc) 712 { 713 DPRINTFN(0, "%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, " 714 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n", 715 device_get_nameunit(sc->sc_bus.bdev), 716 UREAD2(sc, UHCI_CMD), 717 UREAD2(sc, UHCI_STS), 718 UREAD2(sc, UHCI_INTR), 719 UREAD2(sc, UHCI_FRNUM), 720 UREAD4(sc, UHCI_FLBASEADDR), 721 UREAD1(sc, UHCI_SOF), 722 UREAD2(sc, UHCI_PORTSC1), 723 UREAD2(sc, UHCI_PORTSC2)); 724 } 725 726 static uint8_t 727 uhci_dump_td(uhci_td_t *p) 728 { 729 uint32_t td_next; 730 uint32_t td_status; 731 uint32_t td_token; 732 uint8_t temp; 733 734 usb_pc_cpu_invalidate(p->page_cache); 735 736 td_next = le32toh(p->td_next); 737 td_status = le32toh(p->td_status); 738 td_token = le32toh(p->td_token); 739 740 /* 741 * Check whether the link pointer in this TD marks the link pointer 742 * as end of queue: 743 */ 744 temp = ((td_next & UHCI_PTR_T) || (td_next == 0)); 745 746 printf("TD(%p) at 0x%08x = link=0x%08x status=0x%08x " 747 "token=0x%08x buffer=0x%08x\n", 748 p, 749 le32toh(p->td_self), 750 td_next, 751 td_status, 752 td_token, 753 le32toh(p->td_buffer)); 754 755 printf("TD(%p) td_next=%s%s%s td_status=%s%s%s%s%s%s%s%s%s%s%s, errcnt=%d, actlen=%d pid=%02x," 756 "addr=%d,endpt=%d,D=%d,maxlen=%d\n", 757 p, 758 (td_next & 1) ? "-T" : "", 759 (td_next & 2) ? "-Q" : "", 760 (td_next & 4) ? "-VF" : "", 761 (td_status & UHCI_TD_BITSTUFF) ? "-BITSTUFF" : "", 762 (td_status & UHCI_TD_CRCTO) ? "-CRCTO" : "", 763 (td_status & UHCI_TD_NAK) ? "-NAK" : "", 764 (td_status & UHCI_TD_BABBLE) ? "-BABBLE" : "", 765 (td_status & UHCI_TD_DBUFFER) ? "-DBUFFER" : "", 766 (td_status & UHCI_TD_STALLED) ? "-STALLED" : "", 767 (td_status & UHCI_TD_ACTIVE) ? "-ACTIVE" : "", 768 (td_status & UHCI_TD_IOC) ? "-IOC" : "", 769 (td_status & UHCI_TD_IOS) ? "-IOS" : "", 770 (td_status & UHCI_TD_LS) ? "-LS" : "", 771 (td_status & UHCI_TD_SPD) ? "-SPD" : "", 772 UHCI_TD_GET_ERRCNT(td_status), 773 UHCI_TD_GET_ACTLEN(td_status), 774 UHCI_TD_GET_PID(td_token), 775 UHCI_TD_GET_DEVADDR(td_token), 776 UHCI_TD_GET_ENDPT(td_token), 777 UHCI_TD_GET_DT(td_token), 778 UHCI_TD_GET_MAXLEN(td_token)); 779 780 return (temp); 781 } 782 783 static uint8_t 784 uhci_dump_qh(uhci_qh_t *sqh) 785 { 786 uint8_t temp; 787 uint32_t qh_h_next; 788 uint32_t qh_e_next; 789 790 usb_pc_cpu_invalidate(sqh->page_cache); 791 792 qh_h_next = le32toh(sqh->qh_h_next); 793 qh_e_next = le32toh(sqh->qh_e_next); 794 795 DPRINTFN(0, "QH(%p) at 0x%08x: h_next=0x%08x e_next=0x%08x\n", sqh, 796 le32toh(sqh->qh_self), qh_h_next, qh_e_next); 797 798 temp = ((((sqh->h_next != NULL) && !(qh_h_next & UHCI_PTR_T)) ? 1 : 0) | 799 (((sqh->e_next != NULL) && !(qh_e_next & UHCI_PTR_T)) ? 2 : 0)); 800 801 return (temp); 802 } 803 804 static void 805 uhci_dump_all(uhci_softc_t *sc) 806 { 807 uhci_dumpregs(sc); 808 uhci_dump_qh(sc->sc_ls_ctl_p_last); 809 uhci_dump_qh(sc->sc_fs_ctl_p_last); 810 uhci_dump_qh(sc->sc_bulk_p_last); 811 uhci_dump_qh(sc->sc_last_qh_p); 812 } 813 814 static void 815 uhci_dump_tds(uhci_td_t *td) 816 { 817 for (; 818 td != NULL; 819 td = td->obj_next) { 820 if (uhci_dump_td(td)) { 821 break; 822 } 823 } 824 } 825 826 #endif 827 828 /* 829 * Let the last QH loop back to the full speed control transfer QH. 830 * This is what intel calls "bandwidth reclamation" and improves 831 * USB performance a lot for some devices. 832 * If we are already looping, just count it. 833 */ 834 static void 835 uhci_add_loop(uhci_softc_t *sc) 836 { 837 struct uhci_qh *qh_lst; 838 struct uhci_qh *qh_rec; 839 840 #ifdef USB_DEBUG 841 if (uhcinoloop) { 842 return; 843 } 844 #endif 845 if (++(sc->sc_loops) == 1) { 846 DPRINTFN(6, "add\n"); 847 848 qh_lst = sc->sc_last_qh_p; 849 qh_rec = sc->sc_reclaim_qh_p; 850 851 /* NOTE: we don't loop back the soft pointer */ 852 853 qh_lst->qh_h_next = qh_rec->qh_self; 854 usb_pc_cpu_flush(qh_lst->page_cache); 855 } 856 } 857 858 static void 859 uhci_rem_loop(uhci_softc_t *sc) 860 { 861 struct uhci_qh *qh_lst; 862 863 #ifdef USB_DEBUG 864 if (uhcinoloop) { 865 return; 866 } 867 #endif 868 if (--(sc->sc_loops) == 0) { 869 DPRINTFN(6, "remove\n"); 870 871 qh_lst = sc->sc_last_qh_p; 872 qh_lst->qh_h_next = htole32(UHCI_PTR_T); 873 usb_pc_cpu_flush(qh_lst->page_cache); 874 } 875 } 876 877 static void 878 uhci_transfer_intr_enqueue(struct usb_xfer *xfer) 879 { 880 /* check for early completion */ 881 if (uhci_check_transfer(xfer)) { 882 return; 883 } 884 /* put transfer on interrupt queue */ 885 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 886 887 /* start timeout, if any */ 888 if (xfer->timeout != 0) { 889 usbd_transfer_timeout_ms(xfer, &uhci_timeout, xfer->timeout); 890 } 891 } 892 893 #define UHCI_APPEND_TD(std,last) (last) = _uhci_append_td(std,last) 894 static uhci_td_t * 895 _uhci_append_td(uhci_td_t *std, uhci_td_t *last) 896 { 897 DPRINTFN(11, "%p to %p\n", std, last); 898 899 /* (sc->sc_bus.mtx) must be locked */ 900 901 std->next = last->next; 902 std->td_next = last->td_next; 903 904 std->prev = last; 905 906 usb_pc_cpu_flush(std->page_cache); 907 908 /* 909 * the last->next->prev is never followed: std->next->prev = std; 910 */ 911 last->next = std; 912 last->td_next = std->td_self; 913 914 usb_pc_cpu_flush(last->page_cache); 915 916 return (std); 917 } 918 919 #define UHCI_APPEND_QH(sqh,last) (last) = _uhci_append_qh(sqh,last) 920 static uhci_qh_t * 921 _uhci_append_qh(uhci_qh_t *sqh, uhci_qh_t *last) 922 { 923 DPRINTFN(11, "%p to %p\n", sqh, last); 924 925 if (sqh->h_prev != NULL) { 926 /* should not happen */ 927 DPRINTFN(0, "QH already linked!\n"); 928 return (last); 929 } 930 /* (sc->sc_bus.mtx) must be locked */ 931 932 sqh->h_next = last->h_next; 933 sqh->qh_h_next = last->qh_h_next; 934 935 sqh->h_prev = last; 936 937 usb_pc_cpu_flush(sqh->page_cache); 938 939 /* 940 * The "last->h_next->h_prev" is never followed: 941 * 942 * "sqh->h_next->h_prev" = sqh; 943 */ 944 945 last->h_next = sqh; 946 last->qh_h_next = sqh->qh_self; 947 948 usb_pc_cpu_flush(last->page_cache); 949 950 return (sqh); 951 } 952 953 /**/ 954 955 #define UHCI_REMOVE_TD(std,last) (last) = _uhci_remove_td(std,last) 956 static uhci_td_t * 957 _uhci_remove_td(uhci_td_t *std, uhci_td_t *last) 958 { 959 DPRINTFN(11, "%p from %p\n", std, last); 960 961 /* (sc->sc_bus.mtx) must be locked */ 962 963 std->prev->next = std->next; 964 std->prev->td_next = std->td_next; 965 966 usb_pc_cpu_flush(std->prev->page_cache); 967 968 if (std->next) { 969 std->next->prev = std->prev; 970 usb_pc_cpu_flush(std->next->page_cache); 971 } 972 return ((last == std) ? std->prev : last); 973 } 974 975 #define UHCI_REMOVE_QH(sqh,last) (last) = _uhci_remove_qh(sqh,last) 976 static uhci_qh_t * 977 _uhci_remove_qh(uhci_qh_t *sqh, uhci_qh_t *last) 978 { 979 DPRINTFN(11, "%p from %p\n", sqh, last); 980 981 /* (sc->sc_bus.mtx) must be locked */ 982 983 /* only remove if not removed from a queue */ 984 if (sqh->h_prev) { 985 986 sqh->h_prev->h_next = sqh->h_next; 987 sqh->h_prev->qh_h_next = sqh->qh_h_next; 988 989 usb_pc_cpu_flush(sqh->h_prev->page_cache); 990 991 if (sqh->h_next) { 992 sqh->h_next->h_prev = sqh->h_prev; 993 usb_pc_cpu_flush(sqh->h_next->page_cache); 994 } 995 last = ((last == sqh) ? sqh->h_prev : last); 996 997 sqh->h_prev = 0; 998 999 usb_pc_cpu_flush(sqh->page_cache); 1000 } 1001 return (last); 1002 } 1003 1004 static void 1005 uhci_isoc_done(uhci_softc_t *sc, struct usb_xfer *xfer) 1006 { 1007 struct usb_page_search res; 1008 uint32_t nframes = xfer->nframes; 1009 uint32_t status; 1010 uint32_t offset = 0; 1011 uint32_t *plen = xfer->frlengths; 1012 uint16_t len = 0; 1013 uhci_td_t *td = xfer->td_transfer_first; 1014 uhci_td_t **pp_last = &sc->sc_isoc_p_last[xfer->qh_pos]; 1015 1016 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 1017 xfer, xfer->endpoint); 1018 1019 /* sync any DMA memory before doing fixups */ 1020 1021 usb_bdma_post_sync(xfer); 1022 1023 while (nframes--) { 1024 if (td == NULL) { 1025 panic("%s:%d: out of TD's\n", 1026 __FUNCTION__, __LINE__); 1027 } 1028 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) { 1029 pp_last = &sc->sc_isoc_p_last[0]; 1030 } 1031 #ifdef USB_DEBUG 1032 if (uhcidebug > 5) { 1033 DPRINTF("isoc TD\n"); 1034 uhci_dump_td(td); 1035 } 1036 #endif 1037 usb_pc_cpu_invalidate(td->page_cache); 1038 status = le32toh(td->td_status); 1039 1040 len = UHCI_TD_GET_ACTLEN(status); 1041 1042 if (len > *plen) { 1043 len = *plen; 1044 } 1045 if (td->fix_pc) { 1046 1047 usbd_get_page(td->fix_pc, 0, &res); 1048 1049 /* copy data from fixup location to real location */ 1050 1051 usb_pc_cpu_invalidate(td->fix_pc); 1052 1053 usbd_copy_in(xfer->frbuffers, offset, 1054 res.buffer, len); 1055 } 1056 offset += *plen; 1057 1058 *plen = len; 1059 1060 /* remove TD from schedule */ 1061 UHCI_REMOVE_TD(td, *pp_last); 1062 1063 pp_last++; 1064 plen++; 1065 td = td->obj_next; 1066 } 1067 1068 xfer->aframes = xfer->nframes; 1069 } 1070 1071 static usb_error_t 1072 uhci_non_isoc_done_sub(struct usb_xfer *xfer) 1073 { 1074 struct usb_page_search res; 1075 uhci_td_t *td; 1076 uhci_td_t *td_alt_next; 1077 uint32_t status; 1078 uint32_t token; 1079 uint16_t len; 1080 1081 td = xfer->td_transfer_cache; 1082 td_alt_next = td->alt_next; 1083 1084 if (xfer->aframes != xfer->nframes) { 1085 usbd_xfer_set_frame_len(xfer, xfer->aframes, 0); 1086 } 1087 while (1) { 1088 1089 usb_pc_cpu_invalidate(td->page_cache); 1090 status = le32toh(td->td_status); 1091 token = le32toh(td->td_token); 1092 1093 /* 1094 * Verify the status and add 1095 * up the actual length: 1096 */ 1097 1098 len = UHCI_TD_GET_ACTLEN(status); 1099 if (len > td->len) { 1100 /* should not happen */ 1101 DPRINTF("Invalid status length, " 1102 "0x%04x/0x%04x bytes\n", len, td->len); 1103 status |= UHCI_TD_STALLED; 1104 1105 } else if ((xfer->aframes != xfer->nframes) && (len > 0)) { 1106 1107 if (td->fix_pc) { 1108 1109 usbd_get_page(td->fix_pc, 0, &res); 1110 1111 /* 1112 * copy data from fixup location to real 1113 * location 1114 */ 1115 1116 usb_pc_cpu_invalidate(td->fix_pc); 1117 1118 usbd_copy_in(xfer->frbuffers + xfer->aframes, 1119 xfer->frlengths[xfer->aframes], res.buffer, len); 1120 } 1121 /* update actual length */ 1122 1123 xfer->frlengths[xfer->aframes] += len; 1124 } 1125 /* Check for last transfer */ 1126 if (((void *)td) == xfer->td_transfer_last) { 1127 td = NULL; 1128 break; 1129 } 1130 if (status & UHCI_TD_STALLED) { 1131 /* the transfer is finished */ 1132 td = NULL; 1133 break; 1134 } 1135 /* Check for short transfer */ 1136 if (len != td->len) { 1137 if (xfer->flags_int.short_frames_ok) { 1138 /* follow alt next */ 1139 td = td->alt_next; 1140 } else { 1141 /* the transfer is finished */ 1142 td = NULL; 1143 } 1144 break; 1145 } 1146 td = td->obj_next; 1147 1148 if (td->alt_next != td_alt_next) { 1149 /* this USB frame is complete */ 1150 break; 1151 } 1152 } 1153 1154 /* update transfer cache */ 1155 1156 xfer->td_transfer_cache = td; 1157 1158 /* update data toggle */ 1159 1160 xfer->endpoint->toggle_next = (token & UHCI_TD_SET_DT(1)) ? 0 : 1; 1161 1162 #ifdef USB_DEBUG 1163 if (status & UHCI_TD_ERROR) { 1164 DPRINTFN(11, "error, addr=%d, endpt=0x%02x, frame=0x%02x " 1165 "status=%s%s%s%s%s%s%s%s%s%s%s\n", 1166 xfer->address, xfer->endpointno, xfer->aframes, 1167 (status & UHCI_TD_BITSTUFF) ? "[BITSTUFF]" : "", 1168 (status & UHCI_TD_CRCTO) ? "[CRCTO]" : "", 1169 (status & UHCI_TD_NAK) ? "[NAK]" : "", 1170 (status & UHCI_TD_BABBLE) ? "[BABBLE]" : "", 1171 (status & UHCI_TD_DBUFFER) ? "[DBUFFER]" : "", 1172 (status & UHCI_TD_STALLED) ? "[STALLED]" : "", 1173 (status & UHCI_TD_ACTIVE) ? "[ACTIVE]" : "[NOT_ACTIVE]", 1174 (status & UHCI_TD_IOC) ? "[IOC]" : "", 1175 (status & UHCI_TD_IOS) ? "[IOS]" : "", 1176 (status & UHCI_TD_LS) ? "[LS]" : "", 1177 (status & UHCI_TD_SPD) ? "[SPD]" : ""); 1178 } 1179 #endif 1180 if (status & UHCI_TD_STALLED) { 1181 /* try to separate I/O errors from STALL */ 1182 if (UHCI_TD_GET_ERRCNT(status) == 0) 1183 return (USB_ERR_IOERROR); 1184 return (USB_ERR_STALLED); 1185 } 1186 return (USB_ERR_NORMAL_COMPLETION); 1187 } 1188 1189 static void 1190 uhci_non_isoc_done(struct usb_xfer *xfer) 1191 { 1192 usb_error_t err = 0; 1193 1194 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 1195 xfer, xfer->endpoint); 1196 1197 #ifdef USB_DEBUG 1198 if (uhcidebug > 10) { 1199 uhci_dump_tds(xfer->td_transfer_first); 1200 } 1201 #endif 1202 1203 /* sync any DMA memory before doing fixups */ 1204 1205 usb_bdma_post_sync(xfer); 1206 1207 /* reset scanner */ 1208 1209 xfer->td_transfer_cache = xfer->td_transfer_first; 1210 1211 if (xfer->flags_int.control_xfr) { 1212 if (xfer->flags_int.control_hdr) { 1213 1214 err = uhci_non_isoc_done_sub(xfer); 1215 } 1216 xfer->aframes = 1; 1217 1218 if (xfer->td_transfer_cache == NULL) { 1219 goto done; 1220 } 1221 } 1222 while (xfer->aframes != xfer->nframes) { 1223 1224 err = uhci_non_isoc_done_sub(xfer); 1225 xfer->aframes++; 1226 1227 if (xfer->td_transfer_cache == NULL) { 1228 goto done; 1229 } 1230 } 1231 1232 if (xfer->flags_int.control_xfr && 1233 !xfer->flags_int.control_act) { 1234 1235 err = uhci_non_isoc_done_sub(xfer); 1236 } 1237 done: 1238 uhci_device_done(xfer, err); 1239 } 1240 1241 /*------------------------------------------------------------------------* 1242 * uhci_check_transfer_sub 1243 * 1244 * The main purpose of this function is to update the data-toggle 1245 * in case it is wrong. 1246 *------------------------------------------------------------------------*/ 1247 static void 1248 uhci_check_transfer_sub(struct usb_xfer *xfer) 1249 { 1250 uhci_qh_t *qh; 1251 uhci_td_t *td; 1252 uhci_td_t *td_alt_next; 1253 1254 uint32_t td_token; 1255 uint32_t td_self; 1256 1257 td = xfer->td_transfer_cache; 1258 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1259 1260 td_token = td->obj_next->td_token; 1261 td = td->alt_next; 1262 xfer->td_transfer_cache = td; 1263 td_self = td->td_self; 1264 td_alt_next = td->alt_next; 1265 1266 if (xfer->flags_int.control_xfr) 1267 goto skip; /* don't touch the DT value! */ 1268 1269 if (!((td->td_token ^ td_token) & htole32(UHCI_TD_SET_DT(1)))) 1270 goto skip; /* data toggle has correct value */ 1271 1272 /* 1273 * The data toggle is wrong and we need to toggle it ! 1274 */ 1275 while (1) { 1276 1277 td->td_token ^= htole32(UHCI_TD_SET_DT(1)); 1278 usb_pc_cpu_flush(td->page_cache); 1279 1280 if (td == xfer->td_transfer_last) { 1281 /* last transfer */ 1282 break; 1283 } 1284 td = td->obj_next; 1285 1286 if (td->alt_next != td_alt_next) { 1287 /* next frame */ 1288 break; 1289 } 1290 } 1291 skip: 1292 1293 /* update the QH */ 1294 qh->qh_e_next = td_self; 1295 usb_pc_cpu_flush(qh->page_cache); 1296 1297 DPRINTFN(13, "xfer=%p following alt next\n", xfer); 1298 } 1299 1300 /*------------------------------------------------------------------------* 1301 * uhci_check_transfer 1302 * 1303 * Return values: 1304 * 0: USB transfer is not finished 1305 * Else: USB transfer is finished 1306 *------------------------------------------------------------------------*/ 1307 static uint8_t 1308 uhci_check_transfer(struct usb_xfer *xfer) 1309 { 1310 uint32_t status; 1311 uint32_t token; 1312 uhci_td_t *td; 1313 1314 DPRINTFN(16, "xfer=%p checking transfer\n", xfer); 1315 1316 if (xfer->endpoint->methods == &uhci_device_isoc_methods) { 1317 /* isochronous transfer */ 1318 1319 td = xfer->td_transfer_last; 1320 1321 usb_pc_cpu_invalidate(td->page_cache); 1322 status = le32toh(td->td_status); 1323 1324 /* check also if the first is complete */ 1325 1326 td = xfer->td_transfer_first; 1327 1328 usb_pc_cpu_invalidate(td->page_cache); 1329 status |= le32toh(td->td_status); 1330 1331 if (!(status & UHCI_TD_ACTIVE)) { 1332 uhci_device_done(xfer, USB_ERR_NORMAL_COMPLETION); 1333 goto transferred; 1334 } 1335 } else { 1336 /* non-isochronous transfer */ 1337 1338 /* 1339 * check whether there is an error somewhere 1340 * in the middle, or whether there was a short 1341 * packet (SPD and not ACTIVE) 1342 */ 1343 td = xfer->td_transfer_cache; 1344 1345 while (1) { 1346 usb_pc_cpu_invalidate(td->page_cache); 1347 status = le32toh(td->td_status); 1348 token = le32toh(td->td_token); 1349 1350 /* 1351 * if there is an active TD the transfer isn't done 1352 */ 1353 if (status & UHCI_TD_ACTIVE) { 1354 /* update cache */ 1355 xfer->td_transfer_cache = td; 1356 goto done; 1357 } 1358 /* 1359 * last transfer descriptor makes the transfer done 1360 */ 1361 if (((void *)td) == xfer->td_transfer_last) { 1362 break; 1363 } 1364 /* 1365 * any kind of error makes the transfer done 1366 */ 1367 if (status & UHCI_TD_STALLED) { 1368 break; 1369 } 1370 /* 1371 * check if we reached the last packet 1372 * or if there is a short packet: 1373 */ 1374 if ((td->td_next == htole32(UHCI_PTR_T)) || 1375 (UHCI_TD_GET_ACTLEN(status) < td->len)) { 1376 1377 if (xfer->flags_int.short_frames_ok) { 1378 /* follow alt next */ 1379 if (td->alt_next) { 1380 /* update cache */ 1381 xfer->td_transfer_cache = td; 1382 uhci_check_transfer_sub(xfer); 1383 goto done; 1384 } 1385 } 1386 /* transfer is done */ 1387 break; 1388 } 1389 td = td->obj_next; 1390 } 1391 uhci_non_isoc_done(xfer); 1392 goto transferred; 1393 } 1394 1395 done: 1396 DPRINTFN(13, "xfer=%p is still active\n", xfer); 1397 return (0); 1398 1399 transferred: 1400 return (1); 1401 } 1402 1403 static void 1404 uhci_interrupt_poll(uhci_softc_t *sc) 1405 { 1406 struct usb_xfer *xfer; 1407 1408 repeat: 1409 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 1410 /* 1411 * check if transfer is transferred 1412 */ 1413 if (uhci_check_transfer(xfer)) { 1414 /* queue has been modified */ 1415 goto repeat; 1416 } 1417 } 1418 } 1419 1420 /*------------------------------------------------------------------------* 1421 * uhci_interrupt - UHCI interrupt handler 1422 * 1423 * NOTE: Do not access "sc->sc_bus.bdev" inside the interrupt handler, 1424 * hence the interrupt handler will be setup before "sc->sc_bus.bdev" 1425 * is present ! 1426 *------------------------------------------------------------------------*/ 1427 void 1428 uhci_interrupt(uhci_softc_t *sc) 1429 { 1430 uint32_t status; 1431 1432 USB_BUS_LOCK(&sc->sc_bus); 1433 1434 DPRINTFN(16, "real interrupt\n"); 1435 1436 #ifdef USB_DEBUG 1437 if (uhcidebug > 15) { 1438 uhci_dumpregs(sc); 1439 } 1440 #endif 1441 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS; 1442 if (status == 0) { 1443 /* the interrupt was not for us */ 1444 goto done; 1445 } 1446 if (status & (UHCI_STS_RD | UHCI_STS_HSE | 1447 UHCI_STS_HCPE | UHCI_STS_HCH)) { 1448 1449 if (status & UHCI_STS_RD) { 1450 #ifdef USB_DEBUG 1451 printf("%s: resume detect\n", 1452 __FUNCTION__); 1453 #endif 1454 } 1455 if (status & UHCI_STS_HSE) { 1456 printf("%s: host system error\n", 1457 __FUNCTION__); 1458 } 1459 if (status & UHCI_STS_HCPE) { 1460 printf("%s: host controller process error\n", 1461 __FUNCTION__); 1462 } 1463 if (status & UHCI_STS_HCH) { 1464 /* no acknowledge needed */ 1465 DPRINTF("%s: host controller halted\n", 1466 __FUNCTION__); 1467 #ifdef USB_DEBUG 1468 if (uhcidebug > 0) { 1469 uhci_dump_all(sc); 1470 } 1471 #endif 1472 } 1473 } 1474 /* get acknowledge bits */ 1475 status &= (UHCI_STS_USBINT | 1476 UHCI_STS_USBEI | 1477 UHCI_STS_RD | 1478 UHCI_STS_HSE | 1479 UHCI_STS_HCPE); 1480 1481 if (status == 0) { 1482 /* nothing to acknowledge */ 1483 goto done; 1484 } 1485 /* acknowledge interrupts */ 1486 UWRITE2(sc, UHCI_STS, status); 1487 1488 /* poll all the USB transfers */ 1489 uhci_interrupt_poll(sc); 1490 1491 done: 1492 USB_BUS_UNLOCK(&sc->sc_bus); 1493 } 1494 1495 /* 1496 * called when a request does not complete 1497 */ 1498 static void 1499 uhci_timeout(void *arg) 1500 { 1501 struct usb_xfer *xfer = arg; 1502 1503 DPRINTF("xfer=%p\n", xfer); 1504 1505 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1506 1507 /* transfer is transferred */ 1508 uhci_device_done(xfer, USB_ERR_TIMEOUT); 1509 } 1510 1511 static void 1512 uhci_do_poll(struct usb_bus *bus) 1513 { 1514 struct uhci_softc *sc = UHCI_BUS2SC(bus); 1515 1516 USB_BUS_LOCK(&sc->sc_bus); 1517 uhci_interrupt_poll(sc); 1518 USB_BUS_UNLOCK(&sc->sc_bus); 1519 } 1520 1521 static void 1522 uhci_setup_standard_chain_sub(struct uhci_std_temp *temp) 1523 { 1524 uhci_td_t *td; 1525 uhci_td_t *td_next; 1526 uhci_td_t *td_alt_next; 1527 uint32_t average; 1528 uint32_t len_old; 1529 uint8_t shortpkt_old; 1530 uint8_t precompute; 1531 1532 td_alt_next = NULL; 1533 shortpkt_old = temp->shortpkt; 1534 len_old = temp->len; 1535 precompute = 1; 1536 1537 /* software is used to detect short incoming transfers */ 1538 1539 if ((temp->td_token & htole32(UHCI_TD_PID)) == htole32(UHCI_TD_PID_IN)) { 1540 temp->td_status |= htole32(UHCI_TD_SPD); 1541 } else { 1542 temp->td_status &= ~htole32(UHCI_TD_SPD); 1543 } 1544 1545 temp->ml.buf_offset = 0; 1546 1547 restart: 1548 1549 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0)); 1550 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->average)); 1551 1552 td = temp->td; 1553 td_next = temp->td_next; 1554 1555 while (1) { 1556 1557 if (temp->len == 0) { 1558 1559 if (temp->shortpkt) { 1560 break; 1561 } 1562 /* send a Zero Length Packet, ZLP, last */ 1563 1564 temp->shortpkt = 1; 1565 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(0)); 1566 average = 0; 1567 1568 } else { 1569 1570 average = temp->average; 1571 1572 if (temp->len < average) { 1573 temp->shortpkt = 1; 1574 temp->td_token &= ~htole32(UHCI_TD_SET_MAXLEN(0)); 1575 temp->td_token |= htole32(UHCI_TD_SET_MAXLEN(temp->len)); 1576 average = temp->len; 1577 } 1578 } 1579 1580 if (td_next == NULL) { 1581 panic("%s: out of UHCI transfer descriptors!", __FUNCTION__); 1582 } 1583 /* get next TD */ 1584 1585 td = td_next; 1586 td_next = td->obj_next; 1587 1588 /* check if we are pre-computing */ 1589 1590 if (precompute) { 1591 1592 /* update remaining length */ 1593 1594 temp->len -= average; 1595 1596 continue; 1597 } 1598 /* fill out current TD */ 1599 1600 td->td_status = temp->td_status; 1601 td->td_token = temp->td_token; 1602 1603 /* update data toggle */ 1604 1605 temp->td_token ^= htole32(UHCI_TD_SET_DT(1)); 1606 1607 if (average == 0) { 1608 1609 td->len = 0; 1610 td->td_buffer = 0; 1611 td->fix_pc = NULL; 1612 1613 } else { 1614 1615 /* update remaining length */ 1616 1617 temp->len -= average; 1618 1619 td->len = average; 1620 1621 /* fill out buffer pointer and do fixup, if any */ 1622 1623 uhci_mem_layout_fixup(&temp->ml, td); 1624 } 1625 1626 td->alt_next = td_alt_next; 1627 1628 if ((td_next == td_alt_next) && temp->setup_alt_next) { 1629 /* we need to receive these frames one by one ! */ 1630 td->td_status |= htole32(UHCI_TD_IOC); 1631 td->td_next = htole32(UHCI_PTR_T); 1632 } else { 1633 if (td_next) { 1634 /* link the current TD with the next one */ 1635 td->td_next = td_next->td_self; 1636 } 1637 } 1638 1639 usb_pc_cpu_flush(td->page_cache); 1640 } 1641 1642 if (precompute) { 1643 precompute = 0; 1644 1645 /* setup alt next pointer, if any */ 1646 if (temp->last_frame) { 1647 td_alt_next = NULL; 1648 } else { 1649 /* we use this field internally */ 1650 td_alt_next = td_next; 1651 } 1652 1653 /* restore */ 1654 temp->shortpkt = shortpkt_old; 1655 temp->len = len_old; 1656 goto restart; 1657 } 1658 temp->td = td; 1659 temp->td_next = td_next; 1660 } 1661 1662 static uhci_td_t * 1663 uhci_setup_standard_chain(struct usb_xfer *xfer) 1664 { 1665 struct uhci_std_temp temp; 1666 uhci_td_t *td; 1667 uint32_t x; 1668 1669 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n", 1670 xfer->address, UE_GET_ADDR(xfer->endpointno), 1671 xfer->sumlen, usbd_get_speed(xfer->xroot->udev)); 1672 1673 temp.average = xfer->max_frame_size; 1674 temp.max_frame_size = xfer->max_frame_size; 1675 1676 /* toggle the DMA set we are using */ 1677 xfer->flags_int.curr_dma_set ^= 1; 1678 1679 /* get next DMA set */ 1680 td = xfer->td_start[xfer->flags_int.curr_dma_set]; 1681 xfer->td_transfer_first = td; 1682 xfer->td_transfer_cache = td; 1683 1684 temp.td = NULL; 1685 temp.td_next = td; 1686 temp.last_frame = 0; 1687 temp.setup_alt_next = xfer->flags_int.short_frames_ok; 1688 1689 uhci_mem_layout_init(&temp.ml, xfer); 1690 1691 temp.td_status = 1692 htole32(UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | 1693 UHCI_TD_ACTIVE)); 1694 1695 if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 1696 temp.td_status |= htole32(UHCI_TD_LS); 1697 } 1698 temp.td_token = 1699 htole32(UHCI_TD_SET_ENDPT(xfer->endpointno) | 1700 UHCI_TD_SET_DEVADDR(xfer->address)); 1701 1702 if (xfer->endpoint->toggle_next) { 1703 /* DATA1 is next */ 1704 temp.td_token |= htole32(UHCI_TD_SET_DT(1)); 1705 } 1706 /* check if we should prepend a setup message */ 1707 1708 if (xfer->flags_int.control_xfr) { 1709 1710 if (xfer->flags_int.control_hdr) { 1711 1712 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) | 1713 UHCI_TD_SET_ENDPT(0xF)); 1714 temp.td_token |= htole32(UHCI_TD_PID_SETUP | 1715 UHCI_TD_SET_DT(0)); 1716 1717 temp.len = xfer->frlengths[0]; 1718 temp.ml.buf_pc = xfer->frbuffers + 0; 1719 temp.shortpkt = temp.len ? 1 : 0; 1720 /* check for last frame */ 1721 if (xfer->nframes == 1) { 1722 /* no STATUS stage yet, SETUP is last */ 1723 if (xfer->flags_int.control_act) { 1724 temp.last_frame = 1; 1725 temp.setup_alt_next = 0; 1726 } 1727 } 1728 uhci_setup_standard_chain_sub(&temp); 1729 } 1730 x = 1; 1731 } else { 1732 x = 0; 1733 } 1734 1735 while (x != xfer->nframes) { 1736 1737 /* DATA0 / DATA1 message */ 1738 1739 temp.len = xfer->frlengths[x]; 1740 temp.ml.buf_pc = xfer->frbuffers + x; 1741 1742 x++; 1743 1744 if (x == xfer->nframes) { 1745 if (xfer->flags_int.control_xfr) { 1746 /* no STATUS stage yet, DATA is last */ 1747 if (xfer->flags_int.control_act) { 1748 temp.last_frame = 1; 1749 temp.setup_alt_next = 0; 1750 } 1751 } else { 1752 temp.last_frame = 1; 1753 temp.setup_alt_next = 0; 1754 } 1755 } 1756 /* 1757 * Keep previous data toggle, 1758 * device address and endpoint number: 1759 */ 1760 1761 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) | 1762 UHCI_TD_SET_ENDPT(0xF) | 1763 UHCI_TD_SET_DT(1)); 1764 1765 if (temp.len == 0) { 1766 1767 /* make sure that we send an USB packet */ 1768 1769 temp.shortpkt = 0; 1770 1771 } else { 1772 1773 /* regular data transfer */ 1774 1775 temp.shortpkt = (xfer->flags.force_short_xfer) ? 0 : 1; 1776 } 1777 1778 /* set endpoint direction */ 1779 1780 temp.td_token |= 1781 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ? 1782 htole32(UHCI_TD_PID_IN) : 1783 htole32(UHCI_TD_PID_OUT); 1784 1785 uhci_setup_standard_chain_sub(&temp); 1786 } 1787 1788 /* check if we should append a status stage */ 1789 1790 if (xfer->flags_int.control_xfr && 1791 !xfer->flags_int.control_act) { 1792 1793 /* 1794 * send a DATA1 message and reverse the current endpoint 1795 * direction 1796 */ 1797 1798 temp.td_token &= htole32(UHCI_TD_SET_DEVADDR(0x7F) | 1799 UHCI_TD_SET_ENDPT(0xF) | 1800 UHCI_TD_SET_DT(1)); 1801 temp.td_token |= 1802 (UE_GET_DIR(xfer->endpointno) == UE_DIR_OUT) ? 1803 htole32(UHCI_TD_PID_IN | UHCI_TD_SET_DT(1)) : 1804 htole32(UHCI_TD_PID_OUT | UHCI_TD_SET_DT(1)); 1805 1806 temp.len = 0; 1807 temp.ml.buf_pc = NULL; 1808 temp.shortpkt = 0; 1809 temp.last_frame = 1; 1810 temp.setup_alt_next = 0; 1811 1812 uhci_setup_standard_chain_sub(&temp); 1813 } 1814 td = temp.td; 1815 1816 /* Ensure that last TD is terminating: */ 1817 td->td_next = htole32(UHCI_PTR_T); 1818 1819 /* set interrupt bit */ 1820 1821 td->td_status |= htole32(UHCI_TD_IOC); 1822 1823 usb_pc_cpu_flush(td->page_cache); 1824 1825 /* must have at least one frame! */ 1826 1827 xfer->td_transfer_last = td; 1828 1829 #ifdef USB_DEBUG 1830 if (uhcidebug > 8) { 1831 DPRINTF("nexttog=%d; data before transfer:\n", 1832 xfer->endpoint->toggle_next); 1833 uhci_dump_tds(xfer->td_transfer_first); 1834 } 1835 #endif 1836 return (xfer->td_transfer_first); 1837 } 1838 1839 /* NOTE: "done" can be run two times in a row, 1840 * from close and from interrupt 1841 */ 1842 1843 static void 1844 uhci_device_done(struct usb_xfer *xfer, usb_error_t error) 1845 { 1846 const struct usb_pipe_methods *methods = xfer->endpoint->methods; 1847 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 1848 uhci_qh_t *qh; 1849 1850 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1851 1852 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n", 1853 xfer, xfer->endpoint, error); 1854 1855 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1856 if (qh) { 1857 usb_pc_cpu_invalidate(qh->page_cache); 1858 } 1859 if (xfer->flags_int.bandwidth_reclaimed) { 1860 xfer->flags_int.bandwidth_reclaimed = 0; 1861 uhci_rem_loop(sc); 1862 } 1863 if (methods == &uhci_device_bulk_methods) { 1864 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last); 1865 } 1866 if (methods == &uhci_device_ctrl_methods) { 1867 if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 1868 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last); 1869 } else { 1870 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last); 1871 } 1872 } 1873 if (methods == &uhci_device_intr_methods) { 1874 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]); 1875 } 1876 /* 1877 * Only finish isochronous transfers once 1878 * which will update "xfer->frlengths". 1879 */ 1880 if (xfer->td_transfer_first && 1881 xfer->td_transfer_last) { 1882 if (methods == &uhci_device_isoc_methods) { 1883 uhci_isoc_done(sc, xfer); 1884 } 1885 xfer->td_transfer_first = NULL; 1886 xfer->td_transfer_last = NULL; 1887 } 1888 /* dequeue transfer and start next transfer */ 1889 usbd_transfer_done(xfer, error); 1890 } 1891 1892 /*------------------------------------------------------------------------* 1893 * uhci bulk support 1894 *------------------------------------------------------------------------*/ 1895 static void 1896 uhci_device_bulk_open(struct usb_xfer *xfer) 1897 { 1898 return; 1899 } 1900 1901 static void 1902 uhci_device_bulk_close(struct usb_xfer *xfer) 1903 { 1904 uhci_device_done(xfer, USB_ERR_CANCELLED); 1905 } 1906 1907 static void 1908 uhci_device_bulk_enter(struct usb_xfer *xfer) 1909 { 1910 return; 1911 } 1912 1913 static void 1914 uhci_device_bulk_start(struct usb_xfer *xfer) 1915 { 1916 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 1917 uhci_td_t *td; 1918 uhci_qh_t *qh; 1919 1920 /* setup TD's */ 1921 td = uhci_setup_standard_chain(xfer); 1922 1923 /* setup QH */ 1924 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1925 1926 qh->e_next = td; 1927 qh->qh_e_next = td->td_self; 1928 1929 if (xfer->xroot->udev->flags.self_suspended == 0) { 1930 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last); 1931 uhci_add_loop(sc); 1932 xfer->flags_int.bandwidth_reclaimed = 1; 1933 } else { 1934 usb_pc_cpu_flush(qh->page_cache); 1935 } 1936 1937 /* put transfer on interrupt queue */ 1938 uhci_transfer_intr_enqueue(xfer); 1939 } 1940 1941 static const struct usb_pipe_methods uhci_device_bulk_methods = 1942 { 1943 .open = uhci_device_bulk_open, 1944 .close = uhci_device_bulk_close, 1945 .enter = uhci_device_bulk_enter, 1946 .start = uhci_device_bulk_start, 1947 }; 1948 1949 /*------------------------------------------------------------------------* 1950 * uhci control support 1951 *------------------------------------------------------------------------*/ 1952 static void 1953 uhci_device_ctrl_open(struct usb_xfer *xfer) 1954 { 1955 return; 1956 } 1957 1958 static void 1959 uhci_device_ctrl_close(struct usb_xfer *xfer) 1960 { 1961 uhci_device_done(xfer, USB_ERR_CANCELLED); 1962 } 1963 1964 static void 1965 uhci_device_ctrl_enter(struct usb_xfer *xfer) 1966 { 1967 return; 1968 } 1969 1970 static void 1971 uhci_device_ctrl_start(struct usb_xfer *xfer) 1972 { 1973 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 1974 uhci_qh_t *qh; 1975 uhci_td_t *td; 1976 1977 /* setup TD's */ 1978 td = uhci_setup_standard_chain(xfer); 1979 1980 /* setup QH */ 1981 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 1982 1983 qh->e_next = td; 1984 qh->qh_e_next = td->td_self; 1985 1986 /* 1987 * NOTE: some devices choke on bandwidth- reclamation for control 1988 * transfers 1989 */ 1990 if (xfer->xroot->udev->flags.self_suspended == 0) { 1991 if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 1992 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last); 1993 } else { 1994 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last); 1995 } 1996 } else { 1997 usb_pc_cpu_flush(qh->page_cache); 1998 } 1999 /* put transfer on interrupt queue */ 2000 uhci_transfer_intr_enqueue(xfer); 2001 } 2002 2003 static const struct usb_pipe_methods uhci_device_ctrl_methods = 2004 { 2005 .open = uhci_device_ctrl_open, 2006 .close = uhci_device_ctrl_close, 2007 .enter = uhci_device_ctrl_enter, 2008 .start = uhci_device_ctrl_start, 2009 }; 2010 2011 /*------------------------------------------------------------------------* 2012 * uhci interrupt support 2013 *------------------------------------------------------------------------*/ 2014 static void 2015 uhci_device_intr_open(struct usb_xfer *xfer) 2016 { 2017 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 2018 uint16_t best; 2019 uint16_t bit; 2020 uint16_t x; 2021 2022 best = 0; 2023 bit = UHCI_IFRAMELIST_COUNT / 2; 2024 while (bit) { 2025 if (xfer->interval >= bit) { 2026 x = bit; 2027 best = bit; 2028 while (x & bit) { 2029 if (sc->sc_intr_stat[x] < 2030 sc->sc_intr_stat[best]) { 2031 best = x; 2032 } 2033 x++; 2034 } 2035 break; 2036 } 2037 bit >>= 1; 2038 } 2039 2040 sc->sc_intr_stat[best]++; 2041 xfer->qh_pos = best; 2042 2043 DPRINTFN(3, "best=%d interval=%d\n", 2044 best, xfer->interval); 2045 } 2046 2047 static void 2048 uhci_device_intr_close(struct usb_xfer *xfer) 2049 { 2050 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 2051 2052 sc->sc_intr_stat[xfer->qh_pos]--; 2053 2054 uhci_device_done(xfer, USB_ERR_CANCELLED); 2055 } 2056 2057 static void 2058 uhci_device_intr_enter(struct usb_xfer *xfer) 2059 { 2060 return; 2061 } 2062 2063 static void 2064 uhci_device_intr_start(struct usb_xfer *xfer) 2065 { 2066 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 2067 uhci_qh_t *qh; 2068 uhci_td_t *td; 2069 2070 /* setup TD's */ 2071 td = uhci_setup_standard_chain(xfer); 2072 2073 /* setup QH */ 2074 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 2075 2076 qh->e_next = td; 2077 qh->qh_e_next = td->td_self; 2078 2079 if (xfer->xroot->udev->flags.self_suspended == 0) { 2080 /* enter QHs into the controller data structures */ 2081 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]); 2082 } else { 2083 usb_pc_cpu_flush(qh->page_cache); 2084 } 2085 2086 /* put transfer on interrupt queue */ 2087 uhci_transfer_intr_enqueue(xfer); 2088 } 2089 2090 static const struct usb_pipe_methods uhci_device_intr_methods = 2091 { 2092 .open = uhci_device_intr_open, 2093 .close = uhci_device_intr_close, 2094 .enter = uhci_device_intr_enter, 2095 .start = uhci_device_intr_start, 2096 }; 2097 2098 /*------------------------------------------------------------------------* 2099 * uhci isochronous support 2100 *------------------------------------------------------------------------*/ 2101 static void 2102 uhci_device_isoc_open(struct usb_xfer *xfer) 2103 { 2104 uhci_td_t *td; 2105 uint32_t td_token; 2106 uint8_t ds; 2107 2108 td_token = 2109 (UE_GET_DIR(xfer->endpointno) == UE_DIR_IN) ? 2110 UHCI_TD_IN(0, xfer->endpointno, xfer->address, 0) : 2111 UHCI_TD_OUT(0, xfer->endpointno, xfer->address, 0); 2112 2113 td_token = htole32(td_token); 2114 2115 /* initialize all TD's */ 2116 2117 for (ds = 0; ds != 2; ds++) { 2118 2119 for (td = xfer->td_start[ds]; td; td = td->obj_next) { 2120 2121 /* mark TD as inactive */ 2122 td->td_status = htole32(UHCI_TD_IOS); 2123 td->td_token = td_token; 2124 2125 usb_pc_cpu_flush(td->page_cache); 2126 } 2127 } 2128 } 2129 2130 static void 2131 uhci_device_isoc_close(struct usb_xfer *xfer) 2132 { 2133 uhci_device_done(xfer, USB_ERR_CANCELLED); 2134 } 2135 2136 static void 2137 uhci_device_isoc_enter(struct usb_xfer *xfer) 2138 { 2139 struct uhci_mem_layout ml; 2140 uhci_softc_t *sc = UHCI_BUS2SC(xfer->xroot->bus); 2141 uint32_t nframes; 2142 uint32_t temp; 2143 uint32_t *plen; 2144 2145 #ifdef USB_DEBUG 2146 uint8_t once = 1; 2147 2148 #endif 2149 uhci_td_t *td; 2150 uhci_td_t *td_last = NULL; 2151 uhci_td_t **pp_last; 2152 2153 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n", 2154 xfer, xfer->endpoint->isoc_next, xfer->nframes); 2155 2156 nframes = UREAD2(sc, UHCI_FRNUM); 2157 2158 temp = (nframes - xfer->endpoint->isoc_next) & 2159 (UHCI_VFRAMELIST_COUNT - 1); 2160 2161 if ((xfer->endpoint->is_synced == 0) || 2162 (temp < xfer->nframes)) { 2163 /* 2164 * If there is data underflow or the pipe queue is empty we 2165 * schedule the transfer a few frames ahead of the current 2166 * frame position. Else two isochronous transfers might 2167 * overlap. 2168 */ 2169 xfer->endpoint->isoc_next = (nframes + 3) & (UHCI_VFRAMELIST_COUNT - 1); 2170 xfer->endpoint->is_synced = 1; 2171 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next); 2172 } 2173 /* 2174 * compute how many milliseconds the insertion is ahead of the 2175 * current frame position: 2176 */ 2177 temp = (xfer->endpoint->isoc_next - nframes) & 2178 (UHCI_VFRAMELIST_COUNT - 1); 2179 2180 /* 2181 * pre-compute when the isochronous transfer will be finished: 2182 */ 2183 xfer->isoc_time_complete = 2184 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp + 2185 xfer->nframes; 2186 2187 /* get the real number of frames */ 2188 2189 nframes = xfer->nframes; 2190 2191 uhci_mem_layout_init(&ml, xfer); 2192 2193 plen = xfer->frlengths; 2194 2195 /* toggle the DMA set we are using */ 2196 xfer->flags_int.curr_dma_set ^= 1; 2197 2198 /* get next DMA set */ 2199 td = xfer->td_start[xfer->flags_int.curr_dma_set]; 2200 xfer->td_transfer_first = td; 2201 2202 pp_last = &sc->sc_isoc_p_last[xfer->endpoint->isoc_next]; 2203 2204 /* store starting position */ 2205 2206 xfer->qh_pos = xfer->endpoint->isoc_next; 2207 2208 while (nframes--) { 2209 if (td == NULL) { 2210 panic("%s:%d: out of TD's\n", 2211 __FUNCTION__, __LINE__); 2212 } 2213 if (pp_last >= &sc->sc_isoc_p_last[UHCI_VFRAMELIST_COUNT]) { 2214 pp_last = &sc->sc_isoc_p_last[0]; 2215 } 2216 if (*plen > xfer->max_frame_size) { 2217 #ifdef USB_DEBUG 2218 if (once) { 2219 once = 0; 2220 printf("%s: frame length(%d) exceeds %d " 2221 "bytes (frame truncated)\n", 2222 __FUNCTION__, *plen, 2223 xfer->max_frame_size); 2224 } 2225 #endif 2226 *plen = xfer->max_frame_size; 2227 } 2228 /* reuse td_token from last transfer */ 2229 2230 td->td_token &= htole32(~UHCI_TD_MAXLEN_MASK); 2231 td->td_token |= htole32(UHCI_TD_SET_MAXLEN(*plen)); 2232 2233 td->len = *plen; 2234 2235 if (td->len == 0) { 2236 /* 2237 * Do not call "uhci_mem_layout_fixup()" when the 2238 * length is zero! 2239 */ 2240 td->td_buffer = 0; 2241 td->fix_pc = NULL; 2242 2243 } else { 2244 2245 /* fill out buffer pointer and do fixup, if any */ 2246 2247 uhci_mem_layout_fixup(&ml, td); 2248 2249 } 2250 2251 /* update status */ 2252 if (nframes == 0) { 2253 td->td_status = htole32 2254 (UHCI_TD_ZERO_ACTLEN 2255 (UHCI_TD_SET_ERRCNT(0) | 2256 UHCI_TD_ACTIVE | 2257 UHCI_TD_IOS | 2258 UHCI_TD_IOC)); 2259 } else { 2260 td->td_status = htole32 2261 (UHCI_TD_ZERO_ACTLEN 2262 (UHCI_TD_SET_ERRCNT(0) | 2263 UHCI_TD_ACTIVE | 2264 UHCI_TD_IOS)); 2265 } 2266 2267 usb_pc_cpu_flush(td->page_cache); 2268 2269 #ifdef USB_DEBUG 2270 if (uhcidebug > 5) { 2271 DPRINTF("TD %d\n", nframes); 2272 uhci_dump_td(td); 2273 } 2274 #endif 2275 /* insert TD into schedule */ 2276 UHCI_APPEND_TD(td, *pp_last); 2277 pp_last++; 2278 2279 plen++; 2280 td_last = td; 2281 td = td->obj_next; 2282 } 2283 2284 xfer->td_transfer_last = td_last; 2285 2286 /* update isoc_next */ 2287 xfer->endpoint->isoc_next = (pp_last - &sc->sc_isoc_p_last[0]) & 2288 (UHCI_VFRAMELIST_COUNT - 1); 2289 } 2290 2291 static void 2292 uhci_device_isoc_start(struct usb_xfer *xfer) 2293 { 2294 /* put transfer on interrupt queue */ 2295 uhci_transfer_intr_enqueue(xfer); 2296 } 2297 2298 static const struct usb_pipe_methods uhci_device_isoc_methods = 2299 { 2300 .open = uhci_device_isoc_open, 2301 .close = uhci_device_isoc_close, 2302 .enter = uhci_device_isoc_enter, 2303 .start = uhci_device_isoc_start, 2304 }; 2305 2306 /*------------------------------------------------------------------------* 2307 * uhci root control support 2308 *------------------------------------------------------------------------* 2309 * Simulate a hardware hub by handling all the necessary requests. 2310 *------------------------------------------------------------------------*/ 2311 2312 static const 2313 struct usb_device_descriptor uhci_devd = 2314 { 2315 sizeof(struct usb_device_descriptor), 2316 UDESC_DEVICE, /* type */ 2317 {0x00, 0x01}, /* USB version */ 2318 UDCLASS_HUB, /* class */ 2319 UDSUBCLASS_HUB, /* subclass */ 2320 UDPROTO_FSHUB, /* protocol */ 2321 64, /* max packet */ 2322 {0}, {0}, {0x00, 0x01}, /* device id */ 2323 1, 2, 0, /* string indicies */ 2324 1 /* # of configurations */ 2325 }; 2326 2327 static const struct uhci_config_desc uhci_confd = { 2328 .confd = { 2329 .bLength = sizeof(struct usb_config_descriptor), 2330 .bDescriptorType = UDESC_CONFIG, 2331 .wTotalLength[0] = sizeof(uhci_confd), 2332 .bNumInterface = 1, 2333 .bConfigurationValue = 1, 2334 .iConfiguration = 0, 2335 .bmAttributes = UC_SELF_POWERED, 2336 .bMaxPower = 0 /* max power */ 2337 }, 2338 .ifcd = { 2339 .bLength = sizeof(struct usb_interface_descriptor), 2340 .bDescriptorType = UDESC_INTERFACE, 2341 .bNumEndpoints = 1, 2342 .bInterfaceClass = UICLASS_HUB, 2343 .bInterfaceSubClass = UISUBCLASS_HUB, 2344 .bInterfaceProtocol = UIPROTO_FSHUB, 2345 }, 2346 .endpd = { 2347 .bLength = sizeof(struct usb_endpoint_descriptor), 2348 .bDescriptorType = UDESC_ENDPOINT, 2349 .bEndpointAddress = UE_DIR_IN | UHCI_INTR_ENDPT, 2350 .bmAttributes = UE_INTERRUPT, 2351 .wMaxPacketSize[0] = 8, /* max packet (63 ports) */ 2352 .bInterval = 255, 2353 }, 2354 }; 2355 2356 static const 2357 struct usb_hub_descriptor_min uhci_hubd_piix = 2358 { 2359 .bDescLength = sizeof(uhci_hubd_piix), 2360 .bDescriptorType = UDESC_HUB, 2361 .bNbrPorts = 2, 2362 .wHubCharacteristics = {UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0}, 2363 .bPwrOn2PwrGood = 50, 2364 }; 2365 2366 /* 2367 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also 2368 * enables the port, and also states that SET_FEATURE(PORT_ENABLE) 2369 * should not be used by the USB subsystem. As we cannot issue a 2370 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port 2371 * will be enabled as part of the reset. 2372 * 2373 * On the VT83C572, the port cannot be successfully enabled until the 2374 * outstanding "port enable change" and "connection status change" 2375 * events have been reset. 2376 */ 2377 static usb_error_t 2378 uhci_portreset(uhci_softc_t *sc, uint16_t index) 2379 { 2380 uint16_t port; 2381 uint16_t x; 2382 uint8_t lim; 2383 2384 if (index == 1) 2385 port = UHCI_PORTSC1; 2386 else if (index == 2) 2387 port = UHCI_PORTSC2; 2388 else 2389 return (USB_ERR_IOERROR); 2390 2391 /* 2392 * Before we do anything, turn on SOF messages on the USB 2393 * BUS. Some USB devices do not cope without them! 2394 */ 2395 uhci_restart(sc); 2396 2397 x = URWMASK(UREAD2(sc, port)); 2398 UWRITE2(sc, port, x | UHCI_PORTSC_PR); 2399 2400 usb_pause_mtx(&sc->sc_bus.bus_mtx, 2401 USB_MS_TO_TICKS(usb_port_root_reset_delay)); 2402 2403 DPRINTFN(4, "uhci port %d reset, status0 = 0x%04x\n", 2404 index, UREAD2(sc, port)); 2405 2406 x = URWMASK(UREAD2(sc, port)); 2407 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR); 2408 2409 2410 mtx_unlock(&sc->sc_bus.bus_mtx); 2411 2412 /* 2413 * This delay needs to be exactly 100us, else some USB devices 2414 * fail to attach! 2415 */ 2416 DELAY(100); 2417 2418 mtx_lock(&sc->sc_bus.bus_mtx); 2419 2420 DPRINTFN(4, "uhci port %d reset, status1 = 0x%04x\n", 2421 index, UREAD2(sc, port)); 2422 2423 x = URWMASK(UREAD2(sc, port)); 2424 UWRITE2(sc, port, x | UHCI_PORTSC_PE); 2425 2426 for (lim = 0; lim < 12; lim++) { 2427 2428 usb_pause_mtx(&sc->sc_bus.bus_mtx, 2429 USB_MS_TO_TICKS(usb_port_reset_delay)); 2430 2431 x = UREAD2(sc, port); 2432 2433 DPRINTFN(4, "uhci port %d iteration %u, status = 0x%04x\n", 2434 index, lim, x); 2435 2436 if (!(x & UHCI_PORTSC_CCS)) { 2437 /* 2438 * No device is connected (or was disconnected 2439 * during reset). Consider the port reset. 2440 * The delay must be long enough to ensure on 2441 * the initial iteration that the device 2442 * connection will have been registered. 50ms 2443 * appears to be sufficient, but 20ms is not. 2444 */ 2445 DPRINTFN(4, "uhci port %d loop %u, device detached\n", 2446 index, lim); 2447 goto done; 2448 } 2449 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) { 2450 /* 2451 * Port enabled changed and/or connection 2452 * status changed were set. Reset either or 2453 * both raised flags (by writing a 1 to that 2454 * bit), and wait again for state to settle. 2455 */ 2456 UWRITE2(sc, port, URWMASK(x) | 2457 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC))); 2458 continue; 2459 } 2460 if (x & UHCI_PORTSC_PE) { 2461 /* port is enabled */ 2462 goto done; 2463 } 2464 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE); 2465 } 2466 2467 DPRINTFN(2, "uhci port %d reset timed out\n", index); 2468 return (USB_ERR_TIMEOUT); 2469 2470 done: 2471 DPRINTFN(4, "uhci port %d reset, status2 = 0x%04x\n", 2472 index, UREAD2(sc, port)); 2473 2474 sc->sc_isreset = 1; 2475 return (USB_ERR_NORMAL_COMPLETION); 2476 } 2477 2478 static usb_error_t 2479 uhci_roothub_exec(struct usb_device *udev, 2480 struct usb_device_request *req, const void **pptr, uint16_t *plength) 2481 { 2482 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus); 2483 const void *ptr; 2484 const char *str_ptr; 2485 uint16_t x; 2486 uint16_t port; 2487 uint16_t value; 2488 uint16_t index; 2489 uint16_t status; 2490 uint16_t change; 2491 uint16_t len; 2492 usb_error_t err; 2493 2494 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 2495 2496 /* buffer reset */ 2497 ptr = (const void *)&sc->sc_hub_desc.temp; 2498 len = 0; 2499 err = 0; 2500 2501 value = UGETW(req->wValue); 2502 index = UGETW(req->wIndex); 2503 2504 DPRINTFN(3, "type=0x%02x request=0x%02x wLen=0x%04x " 2505 "wValue=0x%04x wIndex=0x%04x\n", 2506 req->bmRequestType, req->bRequest, 2507 UGETW(req->wLength), value, index); 2508 2509 #define C(x,y) ((x) | ((y) << 8)) 2510 switch (C(req->bRequest, req->bmRequestType)) { 2511 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 2512 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 2513 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 2514 /* 2515 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 2516 * for the integrated root hub. 2517 */ 2518 break; 2519 case C(UR_GET_CONFIG, UT_READ_DEVICE): 2520 len = 1; 2521 sc->sc_hub_desc.temp[0] = sc->sc_conf; 2522 break; 2523 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 2524 switch (value >> 8) { 2525 case UDESC_DEVICE: 2526 if ((value & 0xff) != 0) { 2527 err = USB_ERR_IOERROR; 2528 goto done; 2529 } 2530 len = sizeof(uhci_devd); 2531 ptr = (const void *)&uhci_devd; 2532 break; 2533 2534 case UDESC_CONFIG: 2535 if ((value & 0xff) != 0) { 2536 err = USB_ERR_IOERROR; 2537 goto done; 2538 } 2539 len = sizeof(uhci_confd); 2540 ptr = (const void *)&uhci_confd; 2541 break; 2542 2543 case UDESC_STRING: 2544 switch (value & 0xff) { 2545 case 0: /* Language table */ 2546 str_ptr = "\001"; 2547 break; 2548 2549 case 1: /* Vendor */ 2550 str_ptr = sc->sc_vendor; 2551 break; 2552 2553 case 2: /* Product */ 2554 str_ptr = "UHCI root HUB"; 2555 break; 2556 2557 default: 2558 str_ptr = ""; 2559 break; 2560 } 2561 2562 len = usb_make_str_desc 2563 (sc->sc_hub_desc.temp, 2564 sizeof(sc->sc_hub_desc.temp), 2565 str_ptr); 2566 break; 2567 2568 default: 2569 err = USB_ERR_IOERROR; 2570 goto done; 2571 } 2572 break; 2573 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 2574 len = 1; 2575 sc->sc_hub_desc.temp[0] = 0; 2576 break; 2577 case C(UR_GET_STATUS, UT_READ_DEVICE): 2578 len = 2; 2579 USETW(sc->sc_hub_desc.stat.wStatus, UDS_SELF_POWERED); 2580 break; 2581 case C(UR_GET_STATUS, UT_READ_INTERFACE): 2582 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 2583 len = 2; 2584 USETW(sc->sc_hub_desc.stat.wStatus, 0); 2585 break; 2586 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 2587 if (value >= UHCI_MAX_DEVICES) { 2588 err = USB_ERR_IOERROR; 2589 goto done; 2590 } 2591 sc->sc_addr = value; 2592 break; 2593 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 2594 if ((value != 0) && (value != 1)) { 2595 err = USB_ERR_IOERROR; 2596 goto done; 2597 } 2598 sc->sc_conf = value; 2599 break; 2600 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 2601 break; 2602 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 2603 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 2604 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 2605 err = USB_ERR_IOERROR; 2606 goto done; 2607 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 2608 break; 2609 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 2610 break; 2611 /* Hub requests */ 2612 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 2613 break; 2614 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 2615 DPRINTFN(4, "UR_CLEAR_PORT_FEATURE " 2616 "port=%d feature=%d\n", 2617 index, value); 2618 if (index == 1) 2619 port = UHCI_PORTSC1; 2620 else if (index == 2) 2621 port = UHCI_PORTSC2; 2622 else { 2623 err = USB_ERR_IOERROR; 2624 goto done; 2625 } 2626 switch (value) { 2627 case UHF_PORT_ENABLE: 2628 x = URWMASK(UREAD2(sc, port)); 2629 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE); 2630 break; 2631 case UHF_PORT_SUSPEND: 2632 x = URWMASK(UREAD2(sc, port)); 2633 UWRITE2(sc, port, x & ~(UHCI_PORTSC_SUSP)); 2634 break; 2635 case UHF_PORT_RESET: 2636 x = URWMASK(UREAD2(sc, port)); 2637 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR); 2638 break; 2639 case UHF_C_PORT_CONNECTION: 2640 x = URWMASK(UREAD2(sc, port)); 2641 UWRITE2(sc, port, x | UHCI_PORTSC_CSC); 2642 break; 2643 case UHF_C_PORT_ENABLE: 2644 x = URWMASK(UREAD2(sc, port)); 2645 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC); 2646 break; 2647 case UHF_C_PORT_OVER_CURRENT: 2648 x = URWMASK(UREAD2(sc, port)); 2649 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC); 2650 break; 2651 case UHF_C_PORT_RESET: 2652 sc->sc_isreset = 0; 2653 err = USB_ERR_NORMAL_COMPLETION; 2654 goto done; 2655 case UHF_C_PORT_SUSPEND: 2656 sc->sc_isresumed &= ~(1 << index); 2657 break; 2658 case UHF_PORT_CONNECTION: 2659 case UHF_PORT_OVER_CURRENT: 2660 case UHF_PORT_POWER: 2661 case UHF_PORT_LOW_SPEED: 2662 default: 2663 err = USB_ERR_IOERROR; 2664 goto done; 2665 } 2666 break; 2667 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER): 2668 if (index == 1) 2669 port = UHCI_PORTSC1; 2670 else if (index == 2) 2671 port = UHCI_PORTSC2; 2672 else { 2673 err = USB_ERR_IOERROR; 2674 goto done; 2675 } 2676 len = 1; 2677 sc->sc_hub_desc.temp[0] = 2678 ((UREAD2(sc, port) & UHCI_PORTSC_LS) >> 2679 UHCI_PORTSC_LS_SHIFT); 2680 break; 2681 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 2682 if ((value & 0xff) != 0) { 2683 err = USB_ERR_IOERROR; 2684 goto done; 2685 } 2686 len = sizeof(uhci_hubd_piix); 2687 ptr = (const void *)&uhci_hubd_piix; 2688 break; 2689 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 2690 len = 16; 2691 memset(sc->sc_hub_desc.temp, 0, 16); 2692 break; 2693 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 2694 if (index == 1) 2695 port = UHCI_PORTSC1; 2696 else if (index == 2) 2697 port = UHCI_PORTSC2; 2698 else { 2699 err = USB_ERR_IOERROR; 2700 goto done; 2701 } 2702 x = UREAD2(sc, port); 2703 status = change = 0; 2704 if (x & UHCI_PORTSC_CCS) 2705 status |= UPS_CURRENT_CONNECT_STATUS; 2706 if (x & UHCI_PORTSC_CSC) 2707 change |= UPS_C_CONNECT_STATUS; 2708 if (x & UHCI_PORTSC_PE) 2709 status |= UPS_PORT_ENABLED; 2710 if (x & UHCI_PORTSC_POEDC) 2711 change |= UPS_C_PORT_ENABLED; 2712 if (x & UHCI_PORTSC_OCI) 2713 status |= UPS_OVERCURRENT_INDICATOR; 2714 if (x & UHCI_PORTSC_OCIC) 2715 change |= UPS_C_OVERCURRENT_INDICATOR; 2716 if (x & UHCI_PORTSC_LSDA) 2717 status |= UPS_LOW_SPEED; 2718 if ((x & UHCI_PORTSC_PE) && (x & UHCI_PORTSC_RD)) { 2719 /* need to do a write back */ 2720 UWRITE2(sc, port, URWMASK(x)); 2721 2722 /* wait 20ms for resume sequence to complete */ 2723 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50); 2724 2725 /* clear suspend and resume detect */ 2726 UWRITE2(sc, port, URWMASK(x) & ~(UHCI_PORTSC_RD | 2727 UHCI_PORTSC_SUSP)); 2728 2729 /* wait a little bit */ 2730 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 500); 2731 2732 sc->sc_isresumed |= (1 << index); 2733 2734 } else if (x & UHCI_PORTSC_SUSP) { 2735 status |= UPS_SUSPEND; 2736 } 2737 status |= UPS_PORT_POWER; 2738 if (sc->sc_isresumed & (1 << index)) 2739 change |= UPS_C_SUSPEND; 2740 if (sc->sc_isreset) 2741 change |= UPS_C_PORT_RESET; 2742 USETW(sc->sc_hub_desc.ps.wPortStatus, status); 2743 USETW(sc->sc_hub_desc.ps.wPortChange, change); 2744 len = sizeof(sc->sc_hub_desc.ps); 2745 break; 2746 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 2747 err = USB_ERR_IOERROR; 2748 goto done; 2749 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 2750 break; 2751 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 2752 if (index == 1) 2753 port = UHCI_PORTSC1; 2754 else if (index == 2) 2755 port = UHCI_PORTSC2; 2756 else { 2757 err = USB_ERR_IOERROR; 2758 goto done; 2759 } 2760 switch (value) { 2761 case UHF_PORT_ENABLE: 2762 x = URWMASK(UREAD2(sc, port)); 2763 UWRITE2(sc, port, x | UHCI_PORTSC_PE); 2764 break; 2765 case UHF_PORT_SUSPEND: 2766 x = URWMASK(UREAD2(sc, port)); 2767 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP); 2768 break; 2769 case UHF_PORT_RESET: 2770 err = uhci_portreset(sc, index); 2771 goto done; 2772 case UHF_PORT_POWER: 2773 /* pretend we turned on power */ 2774 err = USB_ERR_NORMAL_COMPLETION; 2775 goto done; 2776 case UHF_C_PORT_CONNECTION: 2777 case UHF_C_PORT_ENABLE: 2778 case UHF_C_PORT_OVER_CURRENT: 2779 case UHF_PORT_CONNECTION: 2780 case UHF_PORT_OVER_CURRENT: 2781 case UHF_PORT_LOW_SPEED: 2782 case UHF_C_PORT_SUSPEND: 2783 case UHF_C_PORT_RESET: 2784 default: 2785 err = USB_ERR_IOERROR; 2786 goto done; 2787 } 2788 break; 2789 default: 2790 err = USB_ERR_IOERROR; 2791 goto done; 2792 } 2793 done: 2794 *plength = len; 2795 *pptr = ptr; 2796 return (err); 2797 } 2798 2799 /* 2800 * This routine is executed periodically and simulates interrupts from 2801 * the root controller interrupt pipe for port status change: 2802 */ 2803 static void 2804 uhci_root_intr(uhci_softc_t *sc) 2805 { 2806 DPRINTFN(21, "\n"); 2807 2808 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 2809 2810 sc->sc_hub_idata[0] = 0; 2811 2812 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC | 2813 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) { 2814 sc->sc_hub_idata[0] |= 1 << 1; 2815 } 2816 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC | 2817 UHCI_PORTSC_OCIC | UHCI_PORTSC_RD)) { 2818 sc->sc_hub_idata[0] |= 1 << 2; 2819 } 2820 2821 /* restart timer */ 2822 usb_callout_reset(&sc->sc_root_intr, hz, 2823 (void *)&uhci_root_intr, sc); 2824 2825 if (sc->sc_hub_idata[0] != 0) { 2826 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata, 2827 sizeof(sc->sc_hub_idata)); 2828 } 2829 } 2830 2831 static void 2832 uhci_xfer_setup(struct usb_setup_params *parm) 2833 { 2834 struct usb_page_search page_info; 2835 struct usb_page_cache *pc; 2836 uhci_softc_t *sc; 2837 struct usb_xfer *xfer; 2838 void *last_obj; 2839 uint32_t ntd; 2840 uint32_t nqh; 2841 uint32_t nfixup; 2842 uint32_t n; 2843 uint16_t align; 2844 2845 sc = UHCI_BUS2SC(parm->udev->bus); 2846 xfer = parm->curr_xfer; 2847 2848 parm->hc_max_packet_size = 0x500; 2849 parm->hc_max_packet_count = 1; 2850 parm->hc_max_frame_size = 0x500; 2851 2852 /* 2853 * compute ntd and nqh 2854 */ 2855 if (parm->methods == &uhci_device_ctrl_methods) { 2856 xfer->flags_int.bdma_enable = 1; 2857 xfer->flags_int.bdma_no_post_sync = 1; 2858 2859 usbd_transfer_setup_sub(parm); 2860 2861 /* see EHCI HC driver for proof of "ntd" formula */ 2862 2863 nqh = 1; 2864 ntd = ((2 * xfer->nframes) + 1 /* STATUS */ 2865 + (xfer->max_data_length / xfer->max_frame_size)); 2866 2867 } else if (parm->methods == &uhci_device_bulk_methods) { 2868 xfer->flags_int.bdma_enable = 1; 2869 xfer->flags_int.bdma_no_post_sync = 1; 2870 2871 usbd_transfer_setup_sub(parm); 2872 2873 nqh = 1; 2874 ntd = ((2 * xfer->nframes) 2875 + (xfer->max_data_length / xfer->max_frame_size)); 2876 2877 } else if (parm->methods == &uhci_device_intr_methods) { 2878 xfer->flags_int.bdma_enable = 1; 2879 xfer->flags_int.bdma_no_post_sync = 1; 2880 2881 usbd_transfer_setup_sub(parm); 2882 2883 nqh = 1; 2884 ntd = ((2 * xfer->nframes) 2885 + (xfer->max_data_length / xfer->max_frame_size)); 2886 2887 } else if (parm->methods == &uhci_device_isoc_methods) { 2888 xfer->flags_int.bdma_enable = 1; 2889 xfer->flags_int.bdma_no_post_sync = 1; 2890 2891 usbd_transfer_setup_sub(parm); 2892 2893 nqh = 0; 2894 ntd = xfer->nframes; 2895 2896 } else { 2897 2898 usbd_transfer_setup_sub(parm); 2899 2900 nqh = 0; 2901 ntd = 0; 2902 } 2903 2904 if (parm->err) { 2905 return; 2906 } 2907 /* 2908 * NOTE: the UHCI controller requires that 2909 * every packet must be contiguous on 2910 * the same USB memory page ! 2911 */ 2912 nfixup = (parm->bufsize / USB_PAGE_SIZE) + 1; 2913 2914 /* 2915 * Compute a suitable power of two alignment 2916 * for our "max_frame_size" fixup buffer(s): 2917 */ 2918 align = xfer->max_frame_size; 2919 n = 0; 2920 while (align) { 2921 align >>= 1; 2922 n++; 2923 } 2924 2925 /* check for power of two */ 2926 if (!(xfer->max_frame_size & 2927 (xfer->max_frame_size - 1))) { 2928 n--; 2929 } 2930 /* 2931 * We don't allow alignments of 2932 * less than 8 bytes: 2933 * 2934 * NOTE: Allocating using an aligment 2935 * of 1 byte has special meaning! 2936 */ 2937 if (n < 3) { 2938 n = 3; 2939 } 2940 align = (1 << n); 2941 2942 if (usbd_transfer_setup_sub_malloc( 2943 parm, &pc, xfer->max_frame_size, 2944 align, nfixup)) { 2945 parm->err = USB_ERR_NOMEM; 2946 return; 2947 } 2948 xfer->buf_fixup = pc; 2949 2950 alloc_dma_set: 2951 2952 if (parm->err) { 2953 return; 2954 } 2955 last_obj = NULL; 2956 2957 if (usbd_transfer_setup_sub_malloc( 2958 parm, &pc, sizeof(uhci_td_t), 2959 UHCI_TD_ALIGN, ntd)) { 2960 parm->err = USB_ERR_NOMEM; 2961 return; 2962 } 2963 if (parm->buf) { 2964 for (n = 0; n != ntd; n++) { 2965 uhci_td_t *td; 2966 2967 usbd_get_page(pc + n, 0, &page_info); 2968 2969 td = page_info.buffer; 2970 2971 /* init TD */ 2972 if ((parm->methods == &uhci_device_bulk_methods) || 2973 (parm->methods == &uhci_device_ctrl_methods) || 2974 (parm->methods == &uhci_device_intr_methods)) { 2975 /* set depth first bit */ 2976 td->td_self = htole32(page_info.physaddr | 2977 UHCI_PTR_TD | UHCI_PTR_VF); 2978 } else { 2979 td->td_self = htole32(page_info.physaddr | 2980 UHCI_PTR_TD); 2981 } 2982 2983 td->obj_next = last_obj; 2984 td->page_cache = pc + n; 2985 2986 last_obj = td; 2987 2988 usb_pc_cpu_flush(pc + n); 2989 } 2990 } 2991 xfer->td_start[xfer->flags_int.curr_dma_set] = last_obj; 2992 2993 last_obj = NULL; 2994 2995 if (usbd_transfer_setup_sub_malloc( 2996 parm, &pc, sizeof(uhci_qh_t), 2997 UHCI_QH_ALIGN, nqh)) { 2998 parm->err = USB_ERR_NOMEM; 2999 return; 3000 } 3001 if (parm->buf) { 3002 for (n = 0; n != nqh; n++) { 3003 uhci_qh_t *qh; 3004 3005 usbd_get_page(pc + n, 0, &page_info); 3006 3007 qh = page_info.buffer; 3008 3009 /* init QH */ 3010 qh->qh_self = htole32(page_info.physaddr | UHCI_PTR_QH); 3011 qh->obj_next = last_obj; 3012 qh->page_cache = pc + n; 3013 3014 last_obj = qh; 3015 3016 usb_pc_cpu_flush(pc + n); 3017 } 3018 } 3019 xfer->qh_start[xfer->flags_int.curr_dma_set] = last_obj; 3020 3021 if (!xfer->flags_int.curr_dma_set) { 3022 xfer->flags_int.curr_dma_set = 1; 3023 goto alloc_dma_set; 3024 } 3025 } 3026 3027 static void 3028 uhci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc, 3029 struct usb_endpoint *ep) 3030 { 3031 uhci_softc_t *sc = UHCI_BUS2SC(udev->bus); 3032 3033 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 3034 ep, udev->address, 3035 edesc->bEndpointAddress, udev->flags.usb_mode, 3036 sc->sc_addr); 3037 3038 if (udev->device_index != sc->sc_addr) { 3039 switch (edesc->bmAttributes & UE_XFERTYPE) { 3040 case UE_CONTROL: 3041 ep->methods = &uhci_device_ctrl_methods; 3042 break; 3043 case UE_INTERRUPT: 3044 ep->methods = &uhci_device_intr_methods; 3045 break; 3046 case UE_ISOCHRONOUS: 3047 if (udev->speed == USB_SPEED_FULL) { 3048 ep->methods = &uhci_device_isoc_methods; 3049 } 3050 break; 3051 case UE_BULK: 3052 ep->methods = &uhci_device_bulk_methods; 3053 break; 3054 default: 3055 /* do nothing */ 3056 break; 3057 } 3058 } 3059 } 3060 3061 static void 3062 uhci_xfer_unsetup(struct usb_xfer *xfer) 3063 { 3064 return; 3065 } 3066 3067 static void 3068 uhci_get_dma_delay(struct usb_device *udev, uint32_t *pus) 3069 { 3070 /* 3071 * Wait until hardware has finished any possible use of the 3072 * transfer descriptor(s) and QH 3073 */ 3074 *pus = (1125); /* microseconds */ 3075 } 3076 3077 static void 3078 uhci_device_resume(struct usb_device *udev) 3079 { 3080 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus); 3081 struct usb_xfer *xfer; 3082 const struct usb_pipe_methods *methods; 3083 uhci_qh_t *qh; 3084 3085 DPRINTF("\n"); 3086 3087 USB_BUS_LOCK(udev->bus); 3088 3089 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 3090 3091 if (xfer->xroot->udev == udev) { 3092 3093 methods = xfer->endpoint->methods; 3094 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 3095 3096 if (methods == &uhci_device_bulk_methods) { 3097 UHCI_APPEND_QH(qh, sc->sc_bulk_p_last); 3098 uhci_add_loop(sc); 3099 xfer->flags_int.bandwidth_reclaimed = 1; 3100 } 3101 if (methods == &uhci_device_ctrl_methods) { 3102 if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 3103 UHCI_APPEND_QH(qh, sc->sc_ls_ctl_p_last); 3104 } else { 3105 UHCI_APPEND_QH(qh, sc->sc_fs_ctl_p_last); 3106 } 3107 } 3108 if (methods == &uhci_device_intr_methods) { 3109 UHCI_APPEND_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]); 3110 } 3111 } 3112 } 3113 3114 USB_BUS_UNLOCK(udev->bus); 3115 3116 return; 3117 } 3118 3119 static void 3120 uhci_device_suspend(struct usb_device *udev) 3121 { 3122 struct uhci_softc *sc = UHCI_BUS2SC(udev->bus); 3123 struct usb_xfer *xfer; 3124 const struct usb_pipe_methods *methods; 3125 uhci_qh_t *qh; 3126 3127 DPRINTF("\n"); 3128 3129 USB_BUS_LOCK(udev->bus); 3130 3131 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 3132 3133 if (xfer->xroot->udev == udev) { 3134 3135 methods = xfer->endpoint->methods; 3136 qh = xfer->qh_start[xfer->flags_int.curr_dma_set]; 3137 3138 if (xfer->flags_int.bandwidth_reclaimed) { 3139 xfer->flags_int.bandwidth_reclaimed = 0; 3140 uhci_rem_loop(sc); 3141 } 3142 if (methods == &uhci_device_bulk_methods) { 3143 UHCI_REMOVE_QH(qh, sc->sc_bulk_p_last); 3144 } 3145 if (methods == &uhci_device_ctrl_methods) { 3146 if (xfer->xroot->udev->speed == USB_SPEED_LOW) { 3147 UHCI_REMOVE_QH(qh, sc->sc_ls_ctl_p_last); 3148 } else { 3149 UHCI_REMOVE_QH(qh, sc->sc_fs_ctl_p_last); 3150 } 3151 } 3152 if (methods == &uhci_device_intr_methods) { 3153 UHCI_REMOVE_QH(qh, sc->sc_intr_p_last[xfer->qh_pos]); 3154 } 3155 } 3156 } 3157 3158 USB_BUS_UNLOCK(udev->bus); 3159 3160 return; 3161 } 3162 3163 static void 3164 uhci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state) 3165 { 3166 struct uhci_softc *sc = UHCI_BUS2SC(bus); 3167 3168 switch (state) { 3169 case USB_HW_POWER_SUSPEND: 3170 case USB_HW_POWER_SHUTDOWN: 3171 uhci_suspend(sc); 3172 break; 3173 case USB_HW_POWER_RESUME: 3174 uhci_resume(sc); 3175 break; 3176 default: 3177 break; 3178 } 3179 } 3180 3181 static void 3182 uhci_set_hw_power(struct usb_bus *bus) 3183 { 3184 struct uhci_softc *sc = UHCI_BUS2SC(bus); 3185 uint32_t flags; 3186 3187 DPRINTF("\n"); 3188 3189 USB_BUS_LOCK(bus); 3190 3191 flags = bus->hw_power_state; 3192 3193 /* 3194 * WARNING: Some FULL speed USB devices require periodic SOF 3195 * messages! If any USB devices are connected through the 3196 * UHCI, power save will be disabled! 3197 */ 3198 if (flags & (USB_HW_POWER_CONTROL | 3199 USB_HW_POWER_NON_ROOT_HUB | 3200 USB_HW_POWER_BULK | 3201 USB_HW_POWER_INTERRUPT | 3202 USB_HW_POWER_ISOC)) { 3203 DPRINTF("Some USB transfer is " 3204 "active on unit %u.\n", 3205 device_get_unit(sc->sc_bus.bdev)); 3206 uhci_restart(sc); 3207 } else { 3208 DPRINTF("Power save on unit %u.\n", 3209 device_get_unit(sc->sc_bus.bdev)); 3210 UHCICMD(sc, UHCI_CMD_MAXP); 3211 } 3212 3213 USB_BUS_UNLOCK(bus); 3214 3215 return; 3216 } 3217 3218 3219 static const struct usb_bus_methods uhci_bus_methods = 3220 { 3221 .endpoint_init = uhci_ep_init, 3222 .xfer_setup = uhci_xfer_setup, 3223 .xfer_unsetup = uhci_xfer_unsetup, 3224 .get_dma_delay = uhci_get_dma_delay, 3225 .device_resume = uhci_device_resume, 3226 .device_suspend = uhci_device_suspend, 3227 .set_hw_power = uhci_set_hw_power, 3228 .set_hw_power_sleep = uhci_set_hw_power_sleep, 3229 .roothub_exec = uhci_roothub_exec, 3230 .xfer_poll = uhci_do_poll, 3231 }; 3232