1 /* $NetBSD: uhci.c,v 1.215 2008/02/22 23:04:52 dyoung Exp $ */ 2 /* $FreeBSD: src/sys/dev/usb/uhci.c,v 1.33 1999/11/17 22:33:41 n_hibma Exp $ */ 3 4 /* 5 * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net) at 10 * Carlstedt Research & Technology. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * USB Universal Host Controller driver. 43 * Handles e.g. PIIX3 and PIIX4. 44 * 45 * UHCI spec: http://developer.intel.com/design/USB/UHCI11D.htm 46 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 47 * PIIXn spec: ftp://download.intel.com/design/intarch/datashts/29055002.pdf 48 * ftp://download.intel.com/design/intarch/datashts/29056201.pdf 49 */ 50 51 #include <sys/cdefs.h> 52 __KERNEL_RCSID(0, "$NetBSD: uhci.c,v 1.215 2008/02/22 23:04:52 dyoung Exp $"); 53 54 #include <sys/param.h> 55 #include <sys/systm.h> 56 #include <sys/kernel.h> 57 #include <sys/malloc.h> 58 #if defined(__NetBSD__) || defined(__OpenBSD__) 59 #include <sys/device.h> 60 #include <sys/select.h> 61 #include <sys/extent.h> 62 #include <uvm/uvm_extern.h> 63 #elif defined(__FreeBSD__) 64 #include <sys/module.h> 65 #include <sys/bus.h> 66 #include <machine/bus_pio.h> 67 #if defined(DIAGNOSTIC) && defined(__i386__) 68 #include <sys/cpu.h> 69 #endif 70 #endif 71 #include <sys/proc.h> 72 #include <sys/queue.h> 73 #include <sys/bus.h> 74 75 #include <machine/endian.h> 76 77 #include <dev/usb/usb.h> 78 #include <dev/usb/usbdi.h> 79 #include <dev/usb/usbdivar.h> 80 #include <dev/usb/usb_mem.h> 81 #include <dev/usb/usb_quirks.h> 82 83 #include <dev/usb/uhcireg.h> 84 #include <dev/usb/uhcivar.h> 85 #include <dev/usb/usbroothub_subr.h> 86 87 /* Use bandwidth reclamation for control transfers. Some devices choke on it. */ 88 /*#define UHCI_CTL_LOOP */ 89 90 #if defined(__FreeBSD__) 91 #include <machine/clock.h> 92 93 #define delay(d) DELAY(d) 94 #endif 95 96 #if defined(__OpenBSD__) 97 struct cfdriver uhci_cd = { 98 NULL, "uhci", DV_DULL 99 }; 100 #endif 101 102 #ifdef UHCI_DEBUG 103 uhci_softc_t *thesc; 104 #define DPRINTF(x) if (uhcidebug) printf x 105 #define DPRINTFN(n,x) if (uhcidebug>(n)) printf x 106 int uhcidebug = 0; 107 int uhcinoloop = 0; 108 #ifndef __NetBSD__ 109 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f)) 110 #endif 111 #else 112 #define DPRINTF(x) 113 #define DPRINTFN(n,x) 114 #endif 115 116 /* 117 * The UHCI controller is little endian, so on big endian machines 118 * the data stored in memory needs to be swapped. 119 */ 120 #if defined(__FreeBSD__) || defined(__OpenBSD__) 121 #if BYTE_ORDER == BIG_ENDIAN 122 #define htole32(x) (bswap32(x)) 123 #define le32toh(x) (bswap32(x)) 124 #else 125 #define htole32(x) (x) 126 #define le32toh(x) (x) 127 #endif 128 #endif 129 130 struct uhci_pipe { 131 struct usbd_pipe pipe; 132 int nexttoggle; 133 134 u_char aborting; 135 usbd_xfer_handle abortstart, abortend; 136 137 /* Info needed for different pipe kinds. */ 138 union { 139 /* Control pipe */ 140 struct { 141 uhci_soft_qh_t *sqh; 142 usb_dma_t reqdma; 143 uhci_soft_td_t *setup, *stat; 144 u_int length; 145 } ctl; 146 /* Interrupt pipe */ 147 struct { 148 int npoll; 149 int isread; 150 uhci_soft_qh_t **qhs; 151 } intr; 152 /* Bulk pipe */ 153 struct { 154 uhci_soft_qh_t *sqh; 155 u_int length; 156 int isread; 157 } bulk; 158 /* Iso pipe */ 159 struct iso { 160 uhci_soft_td_t **stds; 161 int next, inuse; 162 } iso; 163 } u; 164 }; 165 166 Static void uhci_globalreset(uhci_softc_t *); 167 Static usbd_status uhci_portreset(uhci_softc_t*, int); 168 Static void uhci_reset(uhci_softc_t *); 169 Static usbd_status uhci_run(uhci_softc_t *, int run); 170 Static uhci_soft_td_t *uhci_alloc_std(uhci_softc_t *); 171 Static void uhci_free_std(uhci_softc_t *, uhci_soft_td_t *); 172 Static uhci_soft_qh_t *uhci_alloc_sqh(uhci_softc_t *); 173 Static void uhci_free_sqh(uhci_softc_t *, uhci_soft_qh_t *); 174 #if 0 175 Static void uhci_enter_ctl_q(uhci_softc_t *, uhci_soft_qh_t *, 176 uhci_intr_info_t *); 177 Static void uhci_exit_ctl_q(uhci_softc_t *, uhci_soft_qh_t *); 178 #endif 179 180 Static void uhci_free_std_chain(uhci_softc_t *, 181 uhci_soft_td_t *, uhci_soft_td_t *); 182 Static usbd_status uhci_alloc_std_chain(struct uhci_pipe *, 183 uhci_softc_t *, int, int, u_int16_t, usb_dma_t *, 184 uhci_soft_td_t **, uhci_soft_td_t **); 185 Static void uhci_poll_hub(void *); 186 Static void uhci_waitintr(uhci_softc_t *, usbd_xfer_handle); 187 Static void uhci_check_intr(uhci_softc_t *, uhci_intr_info_t *); 188 Static void uhci_idone(uhci_intr_info_t *); 189 190 Static void uhci_abort_xfer(usbd_xfer_handle, usbd_status status); 191 192 Static void uhci_timeout(void *); 193 Static void uhci_timeout_task(void *); 194 Static void uhci_add_ls_ctrl(uhci_softc_t *, uhci_soft_qh_t *); 195 Static void uhci_add_hs_ctrl(uhci_softc_t *, uhci_soft_qh_t *); 196 Static void uhci_add_bulk(uhci_softc_t *, uhci_soft_qh_t *); 197 Static void uhci_remove_ls_ctrl(uhci_softc_t *,uhci_soft_qh_t *); 198 Static void uhci_remove_hs_ctrl(uhci_softc_t *,uhci_soft_qh_t *); 199 Static void uhci_remove_bulk(uhci_softc_t *,uhci_soft_qh_t *); 200 Static void uhci_add_loop(uhci_softc_t *sc); 201 Static void uhci_rem_loop(uhci_softc_t *sc); 202 203 Static usbd_status uhci_setup_isoc(usbd_pipe_handle pipe); 204 Static void uhci_device_isoc_enter(usbd_xfer_handle); 205 206 Static usbd_status uhci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); 207 Static void uhci_freem(struct usbd_bus *, usb_dma_t *); 208 209 Static usbd_xfer_handle uhci_allocx(struct usbd_bus *); 210 Static void uhci_freex(struct usbd_bus *, usbd_xfer_handle); 211 212 Static usbd_status uhci_device_ctrl_transfer(usbd_xfer_handle); 213 Static usbd_status uhci_device_ctrl_start(usbd_xfer_handle); 214 Static void uhci_device_ctrl_abort(usbd_xfer_handle); 215 Static void uhci_device_ctrl_close(usbd_pipe_handle); 216 Static void uhci_device_ctrl_done(usbd_xfer_handle); 217 218 Static usbd_status uhci_device_intr_transfer(usbd_xfer_handle); 219 Static usbd_status uhci_device_intr_start(usbd_xfer_handle); 220 Static void uhci_device_intr_abort(usbd_xfer_handle); 221 Static void uhci_device_intr_close(usbd_pipe_handle); 222 Static void uhci_device_intr_done(usbd_xfer_handle); 223 224 Static usbd_status uhci_device_bulk_transfer(usbd_xfer_handle); 225 Static usbd_status uhci_device_bulk_start(usbd_xfer_handle); 226 Static void uhci_device_bulk_abort(usbd_xfer_handle); 227 Static void uhci_device_bulk_close(usbd_pipe_handle); 228 Static void uhci_device_bulk_done(usbd_xfer_handle); 229 230 Static usbd_status uhci_device_isoc_transfer(usbd_xfer_handle); 231 Static usbd_status uhci_device_isoc_start(usbd_xfer_handle); 232 Static void uhci_device_isoc_abort(usbd_xfer_handle); 233 Static void uhci_device_isoc_close(usbd_pipe_handle); 234 Static void uhci_device_isoc_done(usbd_xfer_handle); 235 236 Static usbd_status uhci_root_ctrl_transfer(usbd_xfer_handle); 237 Static usbd_status uhci_root_ctrl_start(usbd_xfer_handle); 238 Static void uhci_root_ctrl_abort(usbd_xfer_handle); 239 Static void uhci_root_ctrl_close(usbd_pipe_handle); 240 Static void uhci_root_ctrl_done(usbd_xfer_handle); 241 242 Static usbd_status uhci_root_intr_transfer(usbd_xfer_handle); 243 Static usbd_status uhci_root_intr_start(usbd_xfer_handle); 244 Static void uhci_root_intr_abort(usbd_xfer_handle); 245 Static void uhci_root_intr_close(usbd_pipe_handle); 246 Static void uhci_root_intr_done(usbd_xfer_handle); 247 248 Static usbd_status uhci_open(usbd_pipe_handle); 249 Static void uhci_poll(struct usbd_bus *); 250 Static void uhci_softintr(void *); 251 252 Static usbd_status uhci_device_request(usbd_xfer_handle xfer); 253 254 Static void uhci_add_intr(uhci_softc_t *, uhci_soft_qh_t *); 255 Static void uhci_remove_intr(uhci_softc_t *, uhci_soft_qh_t *); 256 Static usbd_status uhci_device_setintr(uhci_softc_t *sc, 257 struct uhci_pipe *pipe, int ival); 258 259 Static void uhci_device_clear_toggle(usbd_pipe_handle pipe); 260 Static void uhci_noop(usbd_pipe_handle pipe); 261 262 Static inline uhci_soft_qh_t *uhci_find_prev_qh(uhci_soft_qh_t *, 263 uhci_soft_qh_t *); 264 265 #ifdef UHCI_DEBUG 266 Static void uhci_dump_all(uhci_softc_t *); 267 Static void uhci_dumpregs(uhci_softc_t *); 268 Static void uhci_dump_qhs(uhci_soft_qh_t *); 269 Static void uhci_dump_qh(uhci_soft_qh_t *); 270 Static void uhci_dump_tds(uhci_soft_td_t *); 271 Static void uhci_dump_td(uhci_soft_td_t *); 272 Static void uhci_dump_ii(uhci_intr_info_t *ii); 273 void uhci_dump(void); 274 #endif 275 276 #define UBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \ 277 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 278 #define UWRITE1(sc, r, x) \ 279 do { UBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); \ 280 } while (/*CONSTCOND*/0) 281 #define UWRITE2(sc, r, x) \ 282 do { UBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); \ 283 } while (/*CONSTCOND*/0) 284 #define UWRITE4(sc, r, x) \ 285 do { UBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); \ 286 } while (/*CONSTCOND*/0) 287 static __inline uint8_t 288 UREAD1(uhci_softc_t *sc, bus_size_t r) 289 { 290 291 UBARR(sc); 292 return bus_space_read_1(sc->iot, sc->ioh, r); 293 } 294 295 static __inline uint16_t 296 UREAD2(uhci_softc_t *sc, bus_size_t r) 297 { 298 299 UBARR(sc); 300 return bus_space_read_2(sc->iot, sc->ioh, r); 301 } 302 303 static __inline uint32_t 304 UREAD4(uhci_softc_t *sc, bus_size_t r) 305 { 306 307 UBARR(sc); 308 return bus_space_read_4(sc->iot, sc->ioh, r); 309 } 310 311 #define UHCICMD(sc, cmd) UWRITE2(sc, UHCI_CMD, cmd) 312 #define UHCISTS(sc) UREAD2(sc, UHCI_STS) 313 314 #define UHCI_RESET_TIMEOUT 100 /* ms, reset timeout */ 315 316 #define UHCI_CURFRAME(sc) (UREAD2(sc, UHCI_FRNUM) & UHCI_FRNUM_MASK) 317 318 #define UHCI_INTR_ENDPT 1 319 320 const struct usbd_bus_methods uhci_bus_methods = { 321 uhci_open, 322 uhci_softintr, 323 uhci_poll, 324 uhci_allocm, 325 uhci_freem, 326 uhci_allocx, 327 uhci_freex, 328 }; 329 330 const struct usbd_pipe_methods uhci_root_ctrl_methods = { 331 uhci_root_ctrl_transfer, 332 uhci_root_ctrl_start, 333 uhci_root_ctrl_abort, 334 uhci_root_ctrl_close, 335 uhci_noop, 336 uhci_root_ctrl_done, 337 }; 338 339 const struct usbd_pipe_methods uhci_root_intr_methods = { 340 uhci_root_intr_transfer, 341 uhci_root_intr_start, 342 uhci_root_intr_abort, 343 uhci_root_intr_close, 344 uhci_noop, 345 uhci_root_intr_done, 346 }; 347 348 const struct usbd_pipe_methods uhci_device_ctrl_methods = { 349 uhci_device_ctrl_transfer, 350 uhci_device_ctrl_start, 351 uhci_device_ctrl_abort, 352 uhci_device_ctrl_close, 353 uhci_noop, 354 uhci_device_ctrl_done, 355 }; 356 357 const struct usbd_pipe_methods uhci_device_intr_methods = { 358 uhci_device_intr_transfer, 359 uhci_device_intr_start, 360 uhci_device_intr_abort, 361 uhci_device_intr_close, 362 uhci_device_clear_toggle, 363 uhci_device_intr_done, 364 }; 365 366 const struct usbd_pipe_methods uhci_device_bulk_methods = { 367 uhci_device_bulk_transfer, 368 uhci_device_bulk_start, 369 uhci_device_bulk_abort, 370 uhci_device_bulk_close, 371 uhci_device_clear_toggle, 372 uhci_device_bulk_done, 373 }; 374 375 const struct usbd_pipe_methods uhci_device_isoc_methods = { 376 uhci_device_isoc_transfer, 377 uhci_device_isoc_start, 378 uhci_device_isoc_abort, 379 uhci_device_isoc_close, 380 uhci_noop, 381 uhci_device_isoc_done, 382 }; 383 384 #define uhci_add_intr_info(sc, ii) \ 385 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ii), list) 386 #define uhci_del_intr_info(ii) \ 387 do { \ 388 LIST_REMOVE((ii), list); \ 389 (ii)->list.le_prev = NULL; \ 390 } while (0) 391 #define uhci_active_intr_info(ii) ((ii)->list.le_prev != NULL) 392 393 Static inline uhci_soft_qh_t * 394 uhci_find_prev_qh(uhci_soft_qh_t *pqh, uhci_soft_qh_t *sqh) 395 { 396 DPRINTFN(15,("uhci_find_prev_qh: pqh=%p sqh=%p\n", pqh, sqh)); 397 398 for (; pqh->hlink != sqh; pqh = pqh->hlink) { 399 #if defined(DIAGNOSTIC) || defined(UHCI_DEBUG) 400 if (le32toh(pqh->qh.qh_hlink) & UHCI_PTR_T) { 401 printf("uhci_find_prev_qh: QH not found\n"); 402 return (NULL); 403 } 404 #endif 405 } 406 return (pqh); 407 } 408 409 void 410 uhci_globalreset(uhci_softc_t *sc) 411 { 412 UHCICMD(sc, UHCI_CMD_GRESET); /* global reset */ 413 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); /* wait a little */ 414 UHCICMD(sc, 0); /* do nothing */ 415 } 416 417 usbd_status 418 uhci_init(uhci_softc_t *sc) 419 { 420 usbd_status err; 421 int i, j; 422 uhci_soft_qh_t *clsqh, *chsqh, *bsqh, *sqh, *lsqh; 423 uhci_soft_td_t *std; 424 425 DPRINTFN(1,("uhci_init: start\n")); 426 427 #ifdef UHCI_DEBUG 428 thesc = sc; 429 430 if (uhcidebug > 2) 431 uhci_dumpregs(sc); 432 #endif 433 434 UWRITE2(sc, UHCI_INTR, 0); /* disable interrupts */ 435 uhci_globalreset(sc); /* reset the controller */ 436 uhci_reset(sc); 437 438 #ifdef __NetBSD__ 439 usb_setup_reserve(sc, &sc->sc_dma_reserve, sc->sc_bus.dmatag, 440 USB_MEM_RESERVE); 441 #endif 442 443 /* Allocate and initialize real frame array. */ 444 err = usb_allocmem(&sc->sc_bus, 445 UHCI_FRAMELIST_COUNT * sizeof(uhci_physaddr_t), 446 UHCI_FRAMELIST_ALIGN, &sc->sc_dma); 447 if (err) 448 return (err); 449 sc->sc_pframes = KERNADDR(&sc->sc_dma, 0); 450 UWRITE2(sc, UHCI_FRNUM, 0); /* set frame number to 0 */ 451 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); /* set frame list*/ 452 453 /* 454 * Allocate a TD, inactive, that hangs from the last QH. 455 * This is to avoid a bug in the PIIX that makes it run berserk 456 * otherwise. 457 */ 458 std = uhci_alloc_std(sc); 459 if (std == NULL) 460 return (USBD_NOMEM); 461 std->link.std = NULL; 462 std->td.td_link = htole32(UHCI_PTR_T); 463 std->td.td_status = htole32(0); /* inactive */ 464 std->td.td_token = htole32(0); 465 std->td.td_buffer = htole32(0); 466 467 /* Allocate the dummy QH marking the end and used for looping the QHs.*/ 468 lsqh = uhci_alloc_sqh(sc); 469 if (lsqh == NULL) 470 return (USBD_NOMEM); 471 lsqh->hlink = NULL; 472 lsqh->qh.qh_hlink = htole32(UHCI_PTR_T); /* end of QH chain */ 473 lsqh->elink = std; 474 lsqh->qh.qh_elink = htole32(std->physaddr | UHCI_PTR_TD); 475 sc->sc_last_qh = lsqh; 476 477 /* Allocate the dummy QH where bulk traffic will be queued. */ 478 bsqh = uhci_alloc_sqh(sc); 479 if (bsqh == NULL) 480 return (USBD_NOMEM); 481 bsqh->hlink = lsqh; 482 bsqh->qh.qh_hlink = htole32(lsqh->physaddr | UHCI_PTR_QH); 483 bsqh->elink = NULL; 484 bsqh->qh.qh_elink = htole32(UHCI_PTR_T); 485 sc->sc_bulk_start = sc->sc_bulk_end = bsqh; 486 487 /* Allocate dummy QH where high speed control traffic will be queued. */ 488 chsqh = uhci_alloc_sqh(sc); 489 if (chsqh == NULL) 490 return (USBD_NOMEM); 491 chsqh->hlink = bsqh; 492 chsqh->qh.qh_hlink = htole32(bsqh->physaddr | UHCI_PTR_QH); 493 chsqh->elink = NULL; 494 chsqh->qh.qh_elink = htole32(UHCI_PTR_T); 495 sc->sc_hctl_start = sc->sc_hctl_end = chsqh; 496 497 /* Allocate dummy QH where control traffic will be queued. */ 498 clsqh = uhci_alloc_sqh(sc); 499 if (clsqh == NULL) 500 return (USBD_NOMEM); 501 clsqh->hlink = bsqh; 502 clsqh->qh.qh_hlink = htole32(chsqh->physaddr | UHCI_PTR_QH); 503 clsqh->elink = NULL; 504 clsqh->qh.qh_elink = htole32(UHCI_PTR_T); 505 sc->sc_lctl_start = sc->sc_lctl_end = clsqh; 506 507 /* 508 * Make all (virtual) frame list pointers point to the interrupt 509 * queue heads and the interrupt queue heads at the control 510 * queue head and point the physical frame list to the virtual. 511 */ 512 for(i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 513 std = uhci_alloc_std(sc); 514 sqh = uhci_alloc_sqh(sc); 515 if (std == NULL || sqh == NULL) 516 return (USBD_NOMEM); 517 std->link.sqh = sqh; 518 std->td.td_link = htole32(sqh->physaddr | UHCI_PTR_QH); 519 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */ 520 std->td.td_token = htole32(0); 521 std->td.td_buffer = htole32(0); 522 sqh->hlink = clsqh; 523 sqh->qh.qh_hlink = htole32(clsqh->physaddr | UHCI_PTR_QH); 524 sqh->elink = NULL; 525 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 526 sc->sc_vframes[i].htd = std; 527 sc->sc_vframes[i].etd = std; 528 sc->sc_vframes[i].hqh = sqh; 529 sc->sc_vframes[i].eqh = sqh; 530 for (j = i; 531 j < UHCI_FRAMELIST_COUNT; 532 j += UHCI_VFRAMELIST_COUNT) 533 sc->sc_pframes[j] = htole32(std->physaddr); 534 } 535 536 LIST_INIT(&sc->sc_intrhead); 537 538 SIMPLEQ_INIT(&sc->sc_free_xfers); 539 540 usb_callout_init(sc->sc_poll_handle); 541 542 /* Set up the bus struct. */ 543 sc->sc_bus.methods = &uhci_bus_methods; 544 sc->sc_bus.pipe_size = sizeof(struct uhci_pipe); 545 546 UHCICMD(sc, UHCI_CMD_MAXP); /* Assume 64 byte packets at frame end */ 547 548 DPRINTFN(1,("uhci_init: enabling\n")); 549 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | UHCI_INTR_RIE | 550 UHCI_INTR_IOCE | UHCI_INTR_SPIE); /* enable interrupts */ 551 552 return (uhci_run(sc, 1)); /* and here we go... */ 553 } 554 555 #if defined(__NetBSD__) || defined(__OpenBSD__) 556 int 557 uhci_activate(device_t self, enum devact act) 558 { 559 struct uhci_softc *sc = device_private(self); 560 int rv = 0; 561 562 switch (act) { 563 case DVACT_ACTIVATE: 564 return (EOPNOTSUPP); 565 566 case DVACT_DEACTIVATE: 567 sc->sc_dying = 1; 568 if (sc->sc_child != NULL) 569 rv = config_deactivate(sc->sc_child); 570 break; 571 } 572 return (rv); 573 } 574 575 void 576 uhci_childdet(device_t self, device_t child) 577 { 578 struct uhci_softc *sc = device_private(self); 579 580 KASSERT(sc->sc_child == child); 581 sc->sc_child = NULL; 582 } 583 584 int 585 uhci_detach(struct uhci_softc *sc, int flags) 586 { 587 usbd_xfer_handle xfer; 588 int rv = 0; 589 590 if (sc->sc_child != NULL) 591 rv = config_detach(sc->sc_child, flags); 592 593 if (rv != 0) 594 return (rv); 595 596 /* Free all xfers associated with this HC. */ 597 for (;;) { 598 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 599 if (xfer == NULL) 600 break; 601 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 602 free(xfer, M_USB); 603 } 604 605 /* XXX free other data structures XXX */ 606 607 return (rv); 608 } 609 #endif 610 611 usbd_status 612 uhci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) 613 { 614 struct uhci_softc *sc = (struct uhci_softc *)bus; 615 usbd_status status; 616 u_int32_t n; 617 618 /* 619 * XXX 620 * Since we are allocating a buffer we can assume that we will 621 * need TDs for it. Since we don't want to allocate those from 622 * an interrupt context, we allocate them here and free them again. 623 * This is no guarantee that we'll get the TDs next time... 624 */ 625 n = size / 8; 626 if (n > 16) { 627 u_int32_t i; 628 uhci_soft_td_t **stds; 629 DPRINTF(("uhci_allocm: get %d TDs\n", n)); 630 stds = malloc(sizeof(uhci_soft_td_t *) * n, M_TEMP, 631 M_WAITOK|M_ZERO); 632 for(i=0; i < n; i++) 633 stds[i] = uhci_alloc_std(sc); 634 for(i=0; i < n; i++) 635 if (stds[i] != NULL) 636 uhci_free_std(sc, stds[i]); 637 free(stds, M_TEMP); 638 } 639 640 641 status = usb_allocmem(&sc->sc_bus, size, 0, dma); 642 #ifdef __NetBSD__ 643 if (status == USBD_NOMEM) 644 status = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size); 645 #endif 646 return status; 647 } 648 649 void 650 uhci_freem(struct usbd_bus *bus, usb_dma_t *dma) 651 { 652 #ifdef __NetBSD__ 653 if (dma->block->flags & USB_DMA_RESERVE) { 654 usb_reserve_freem(&((struct uhci_softc *)bus)->sc_dma_reserve, 655 dma); 656 return; 657 } 658 #endif 659 usb_freemem(&((struct uhci_softc *)bus)->sc_bus, dma); 660 } 661 662 usbd_xfer_handle 663 uhci_allocx(struct usbd_bus *bus) 664 { 665 struct uhci_softc *sc = (struct uhci_softc *)bus; 666 usbd_xfer_handle xfer; 667 668 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 669 if (xfer != NULL) { 670 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 671 #ifdef DIAGNOSTIC 672 if (xfer->busy_free != XFER_FREE) { 673 printf("uhci_allocx: xfer=%p not free, 0x%08x\n", xfer, 674 xfer->busy_free); 675 } 676 #endif 677 } else { 678 xfer = malloc(sizeof(struct uhci_xfer), M_USB, M_NOWAIT); 679 } 680 if (xfer != NULL) { 681 memset(xfer, 0, sizeof (struct uhci_xfer)); 682 UXFER(xfer)->iinfo.sc = sc; 683 #ifdef DIAGNOSTIC 684 UXFER(xfer)->iinfo.isdone = 1; 685 xfer->busy_free = XFER_BUSY; 686 #endif 687 } 688 return (xfer); 689 } 690 691 void 692 uhci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) 693 { 694 struct uhci_softc *sc = (struct uhci_softc *)bus; 695 696 #ifdef DIAGNOSTIC 697 if (xfer->busy_free != XFER_BUSY) { 698 printf("uhci_freex: xfer=%p not busy, 0x%08x\n", xfer, 699 xfer->busy_free); 700 } 701 xfer->busy_free = XFER_FREE; 702 if (!UXFER(xfer)->iinfo.isdone) { 703 printf("uhci_freex: !isdone\n"); 704 } 705 #endif 706 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); 707 } 708 709 /* 710 * Handle suspend/resume. 711 * 712 * We need to switch to polling mode here, because this routine is 713 * called from an interrupt context. This is all right since we 714 * are almost suspended anyway. 715 */ 716 bool 717 uhci_resume(device_t dv PMF_FN_ARGS) 718 { 719 uhci_softc_t *sc = device_private(dv); 720 int cmd; 721 int s; 722 723 s = splhardusb(); 724 725 cmd = UREAD2(sc, UHCI_CMD); 726 sc->sc_bus.use_polling++; 727 UWRITE2(sc, UHCI_INTR, 0); 728 uhci_globalreset(sc); 729 uhci_reset(sc); 730 if (cmd & UHCI_CMD_RS) 731 uhci_run(sc, 0); 732 733 /* restore saved state */ 734 UWRITE4(sc, UHCI_FLBASEADDR, DMAADDR(&sc->sc_dma, 0)); 735 UWRITE2(sc, UHCI_FRNUM, sc->sc_saved_frnum); 736 UWRITE1(sc, UHCI_SOF, sc->sc_saved_sof); 737 738 UHCICMD(sc, cmd | UHCI_CMD_FGR); /* force resume */ 739 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY); 740 UHCICMD(sc, cmd & ~UHCI_CMD_EGSM); /* back to normal */ 741 UWRITE2(sc, UHCI_INTR, UHCI_INTR_TOCRCIE | 742 UHCI_INTR_RIE | UHCI_INTR_IOCE | UHCI_INTR_SPIE); 743 UHCICMD(sc, UHCI_CMD_MAXP); 744 uhci_run(sc, 1); /* and start traffic again */ 745 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY); 746 sc->sc_bus.use_polling--; 747 if (sc->sc_intr_xfer != NULL) 748 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, 749 sc->sc_intr_xfer); 750 #ifdef UHCI_DEBUG 751 if (uhcidebug > 2) 752 uhci_dumpregs(sc); 753 #endif 754 755 splx(s); 756 757 return true; 758 } 759 760 bool 761 uhci_suspend(device_t dv PMF_FN_ARGS) 762 { 763 uhci_softc_t *sc = device_private(dv); 764 int cmd; 765 int s; 766 767 s = splhardusb(); 768 769 cmd = UREAD2(sc, UHCI_CMD); 770 771 #ifdef UHCI_DEBUG 772 if (uhcidebug > 2) 773 uhci_dumpregs(sc); 774 #endif 775 if (sc->sc_intr_xfer != NULL) 776 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, 777 sc->sc_intr_xfer); 778 sc->sc_bus.use_polling++; 779 uhci_run(sc, 0); /* stop the controller */ 780 cmd &= ~UHCI_CMD_RS; 781 782 /* save some state if BIOS doesn't */ 783 sc->sc_saved_frnum = UREAD2(sc, UHCI_FRNUM); 784 sc->sc_saved_sof = UREAD1(sc, UHCI_SOF); 785 786 UWRITE2(sc, UHCI_INTR, 0); /* disable intrs */ 787 788 UHCICMD(sc, cmd | UHCI_CMD_EGSM); /* enter suspend */ 789 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 790 sc->sc_bus.use_polling--; 791 792 splx(s); 793 794 return true; 795 } 796 797 #ifdef UHCI_DEBUG 798 Static void 799 uhci_dumpregs(uhci_softc_t *sc) 800 { 801 DPRINTFN(-1,("%s regs: cmd=%04x, sts=%04x, intr=%04x, frnum=%04x, " 802 "flbase=%08x, sof=%04x, portsc1=%04x, portsc2=%04x\n", 803 USBDEVNAME(sc->sc_bus.bdev), 804 UREAD2(sc, UHCI_CMD), 805 UREAD2(sc, UHCI_STS), 806 UREAD2(sc, UHCI_INTR), 807 UREAD2(sc, UHCI_FRNUM), 808 UREAD4(sc, UHCI_FLBASEADDR), 809 UREAD1(sc, UHCI_SOF), 810 UREAD2(sc, UHCI_PORTSC1), 811 UREAD2(sc, UHCI_PORTSC2))); 812 } 813 814 void 815 uhci_dump_td(uhci_soft_td_t *p) 816 { 817 char sbuf[128], sbuf2[128]; 818 819 DPRINTFN(-1,("TD(%p) at %08lx = link=0x%08lx status=0x%08lx " 820 "token=0x%08lx buffer=0x%08lx\n", 821 p, (long)p->physaddr, 822 (long)le32toh(p->td.td_link), 823 (long)le32toh(p->td.td_status), 824 (long)le32toh(p->td.td_token), 825 (long)le32toh(p->td.td_buffer))); 826 827 bitmask_snprintf((u_int32_t)le32toh(p->td.td_link), "\20\1T\2Q\3VF", 828 sbuf, sizeof(sbuf)); 829 bitmask_snprintf((u_int32_t)le32toh(p->td.td_status), 830 "\20\22BITSTUFF\23CRCTO\24NAK\25BABBLE\26DBUFFER\27" 831 "STALLED\30ACTIVE\31IOC\32ISO\33LS\36SPD", 832 sbuf2, sizeof(sbuf2)); 833 834 DPRINTFN(-1,(" %s %s,errcnt=%d,actlen=%d pid=%02x,addr=%d,endpt=%d," 835 "D=%d,maxlen=%d\n", sbuf, sbuf2, 836 UHCI_TD_GET_ERRCNT(le32toh(p->td.td_status)), 837 UHCI_TD_GET_ACTLEN(le32toh(p->td.td_status)), 838 UHCI_TD_GET_PID(le32toh(p->td.td_token)), 839 UHCI_TD_GET_DEVADDR(le32toh(p->td.td_token)), 840 UHCI_TD_GET_ENDPT(le32toh(p->td.td_token)), 841 UHCI_TD_GET_DT(le32toh(p->td.td_token)), 842 UHCI_TD_GET_MAXLEN(le32toh(p->td.td_token)))); 843 } 844 845 void 846 uhci_dump_qh(uhci_soft_qh_t *sqh) 847 { 848 DPRINTFN(-1,("QH(%p) at %08x: hlink=%08x elink=%08x\n", sqh, 849 (int)sqh->physaddr, le32toh(sqh->qh.qh_hlink), 850 le32toh(sqh->qh.qh_elink))); 851 } 852 853 854 #if 1 855 void 856 uhci_dump(void) 857 { 858 uhci_dump_all(thesc); 859 } 860 #endif 861 862 void 863 uhci_dump_all(uhci_softc_t *sc) 864 { 865 uhci_dumpregs(sc); 866 printf("intrs=%d\n", sc->sc_bus.no_intrs); 867 /*printf("framelist[i].link = %08x\n", sc->sc_framelist[0].link);*/ 868 uhci_dump_qh(sc->sc_lctl_start); 869 } 870 871 872 void 873 uhci_dump_qhs(uhci_soft_qh_t *sqh) 874 { 875 uhci_dump_qh(sqh); 876 877 /* uhci_dump_qhs displays all the QHs and TDs from the given QH onwards 878 * Traverses sideways first, then down. 879 * 880 * QH1 881 * QH2 882 * No QH 883 * TD2.1 884 * TD2.2 885 * TD1.1 886 * etc. 887 * 888 * TD2.x being the TDs queued at QH2 and QH1 being referenced from QH1. 889 */ 890 891 892 if (sqh->hlink != NULL && !(le32toh(sqh->qh.qh_hlink) & UHCI_PTR_T)) 893 uhci_dump_qhs(sqh->hlink); 894 else 895 DPRINTF(("No QH\n")); 896 897 if (sqh->elink != NULL && !(le32toh(sqh->qh.qh_elink) & UHCI_PTR_T)) 898 uhci_dump_tds(sqh->elink); 899 else 900 DPRINTF(("No TD\n")); 901 } 902 903 void 904 uhci_dump_tds(uhci_soft_td_t *std) 905 { 906 uhci_soft_td_t *td; 907 908 for(td = std; td != NULL; td = td->link.std) { 909 uhci_dump_td(td); 910 911 /* Check whether the link pointer in this TD marks 912 * the link pointer as end of queue. This avoids 913 * printing the free list in case the queue/TD has 914 * already been moved there (seatbelt). 915 */ 916 if (le32toh(td->td.td_link) & UHCI_PTR_T || 917 le32toh(td->td.td_link) == 0) 918 break; 919 } 920 } 921 922 Static void 923 uhci_dump_ii(uhci_intr_info_t *ii) 924 { 925 usbd_pipe_handle pipe; 926 usb_endpoint_descriptor_t *ed; 927 usbd_device_handle dev; 928 929 #ifdef DIAGNOSTIC 930 #define DONE ii->isdone 931 #else 932 #define DONE 0 933 #endif 934 if (ii == NULL) { 935 printf("ii NULL\n"); 936 return; 937 } 938 if (ii->xfer == NULL) { 939 printf("ii %p: done=%d xfer=NULL\n", 940 ii, DONE); 941 return; 942 } 943 pipe = ii->xfer->pipe; 944 if (pipe == NULL) { 945 printf("ii %p: done=%d xfer=%p pipe=NULL\n", 946 ii, DONE, ii->xfer); 947 return; 948 } 949 if (pipe->endpoint == NULL) { 950 printf("ii %p: done=%d xfer=%p pipe=%p pipe->endpoint=NULL\n", 951 ii, DONE, ii->xfer, pipe); 952 return; 953 } 954 if (pipe->device == NULL) { 955 printf("ii %p: done=%d xfer=%p pipe=%p pipe->device=NULL\n", 956 ii, DONE, ii->xfer, pipe); 957 return; 958 } 959 ed = pipe->endpoint->edesc; 960 dev = pipe->device; 961 printf("ii %p: done=%d xfer=%p dev=%p vid=0x%04x pid=0x%04x addr=%d pipe=%p ep=0x%02x attr=0x%02x\n", 962 ii, DONE, ii->xfer, dev, 963 UGETW(dev->ddesc.idVendor), 964 UGETW(dev->ddesc.idProduct), 965 dev->address, pipe, 966 ed->bEndpointAddress, ed->bmAttributes); 967 #undef DONE 968 } 969 970 void uhci_dump_iis(struct uhci_softc *sc); 971 void 972 uhci_dump_iis(struct uhci_softc *sc) 973 { 974 uhci_intr_info_t *ii; 975 976 printf("intr_info list:\n"); 977 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = LIST_NEXT(ii, list)) 978 uhci_dump_ii(ii); 979 } 980 981 void iidump(void); 982 void iidump(void) { uhci_dump_iis(thesc); } 983 984 #endif 985 986 /* 987 * This routine is executed periodically and simulates interrupts 988 * from the root controller interrupt pipe for port status change. 989 */ 990 void 991 uhci_poll_hub(void *addr) 992 { 993 usbd_xfer_handle xfer = addr; 994 usbd_pipe_handle pipe = xfer->pipe; 995 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; 996 int s; 997 u_char *p; 998 999 DPRINTFN(20, ("uhci_poll_hub\n")); 1000 1001 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer); 1002 1003 p = KERNADDR(&xfer->dmabuf, 0); 1004 p[0] = 0; 1005 if (UREAD2(sc, UHCI_PORTSC1) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC)) 1006 p[0] |= 1<<1; 1007 if (UREAD2(sc, UHCI_PORTSC2) & (UHCI_PORTSC_CSC|UHCI_PORTSC_OCIC)) 1008 p[0] |= 1<<2; 1009 if (p[0] == 0) 1010 /* No change, try again in a while */ 1011 return; 1012 1013 xfer->actlen = 1; 1014 xfer->status = USBD_NORMAL_COMPLETION; 1015 s = splusb(); 1016 xfer->device->bus->intr_context++; 1017 usb_transfer_complete(xfer); 1018 xfer->device->bus->intr_context--; 1019 splx(s); 1020 } 1021 1022 void 1023 uhci_root_intr_done(usbd_xfer_handle xfer) 1024 { 1025 } 1026 1027 void 1028 uhci_root_ctrl_done(usbd_xfer_handle xfer) 1029 { 1030 } 1031 1032 /* 1033 * Let the last QH loop back to the high speed control transfer QH. 1034 * This is what intel calls "bandwidth reclamation" and improves 1035 * USB performance a lot for some devices. 1036 * If we are already looping, just count it. 1037 */ 1038 void 1039 uhci_add_loop(uhci_softc_t *sc) { 1040 #ifdef UHCI_DEBUG 1041 if (uhcinoloop) 1042 return; 1043 #endif 1044 if (++sc->sc_loops == 1) { 1045 DPRINTFN(5,("uhci_start_loop: add\n")); 1046 /* Note, we don't loop back the soft pointer. */ 1047 sc->sc_last_qh->qh.qh_hlink = 1048 htole32(sc->sc_hctl_start->physaddr | UHCI_PTR_QH); 1049 } 1050 } 1051 1052 void 1053 uhci_rem_loop(uhci_softc_t *sc) { 1054 #ifdef UHCI_DEBUG 1055 if (uhcinoloop) 1056 return; 1057 #endif 1058 if (--sc->sc_loops == 0) { 1059 DPRINTFN(5,("uhci_end_loop: remove\n")); 1060 sc->sc_last_qh->qh.qh_hlink = htole32(UHCI_PTR_T); 1061 } 1062 } 1063 1064 /* Add high speed control QH, called at splusb(). */ 1065 void 1066 uhci_add_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1067 { 1068 uhci_soft_qh_t *eqh; 1069 1070 SPLUSBCHECK; 1071 1072 DPRINTFN(10, ("uhci_add_ctrl: sqh=%p\n", sqh)); 1073 eqh = sc->sc_hctl_end; 1074 sqh->hlink = eqh->hlink; 1075 sqh->qh.qh_hlink = eqh->qh.qh_hlink; 1076 eqh->hlink = sqh; 1077 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH); 1078 sc->sc_hctl_end = sqh; 1079 #ifdef UHCI_CTL_LOOP 1080 uhci_add_loop(sc); 1081 #endif 1082 } 1083 1084 /* Remove high speed control QH, called at splusb(). */ 1085 void 1086 uhci_remove_hs_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1087 { 1088 uhci_soft_qh_t *pqh; 1089 1090 SPLUSBCHECK; 1091 1092 DPRINTFN(10, ("uhci_remove_hs_ctrl: sqh=%p\n", sqh)); 1093 #ifdef UHCI_CTL_LOOP 1094 uhci_rem_loop(sc); 1095 #endif 1096 /* 1097 * The T bit should be set in the elink of the QH so that the HC 1098 * doesn't follow the pointer. This condition may fail if the 1099 * the transferred packet was short so that the QH still points 1100 * at the last used TD. 1101 * In this case we set the T bit and wait a little for the HC 1102 * to stop looking at the TD. 1103 */ 1104 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { 1105 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 1106 delay(UHCI_QH_REMOVE_DELAY); 1107 } 1108 1109 pqh = uhci_find_prev_qh(sc->sc_hctl_start, sqh); 1110 pqh->hlink = sqh->hlink; 1111 pqh->qh.qh_hlink = sqh->qh.qh_hlink; 1112 delay(UHCI_QH_REMOVE_DELAY); 1113 if (sc->sc_hctl_end == sqh) 1114 sc->sc_hctl_end = pqh; 1115 } 1116 1117 /* Add low speed control QH, called at splusb(). */ 1118 void 1119 uhci_add_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1120 { 1121 uhci_soft_qh_t *eqh; 1122 1123 SPLUSBCHECK; 1124 1125 DPRINTFN(10, ("uhci_add_ls_ctrl: sqh=%p\n", sqh)); 1126 eqh = sc->sc_lctl_end; 1127 sqh->hlink = eqh->hlink; 1128 sqh->qh.qh_hlink = eqh->qh.qh_hlink; 1129 eqh->hlink = sqh; 1130 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH); 1131 sc->sc_lctl_end = sqh; 1132 } 1133 1134 /* Remove low speed control QH, called at splusb(). */ 1135 void 1136 uhci_remove_ls_ctrl(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1137 { 1138 uhci_soft_qh_t *pqh; 1139 1140 SPLUSBCHECK; 1141 1142 DPRINTFN(10, ("uhci_remove_ls_ctrl: sqh=%p\n", sqh)); 1143 /* See comment in uhci_remove_hs_ctrl() */ 1144 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { 1145 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 1146 delay(UHCI_QH_REMOVE_DELAY); 1147 } 1148 pqh = uhci_find_prev_qh(sc->sc_lctl_start, sqh); 1149 pqh->hlink = sqh->hlink; 1150 pqh->qh.qh_hlink = sqh->qh.qh_hlink; 1151 delay(UHCI_QH_REMOVE_DELAY); 1152 if (sc->sc_lctl_end == sqh) 1153 sc->sc_lctl_end = pqh; 1154 } 1155 1156 /* Add bulk QH, called at splusb(). */ 1157 void 1158 uhci_add_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1159 { 1160 uhci_soft_qh_t *eqh; 1161 1162 SPLUSBCHECK; 1163 1164 DPRINTFN(10, ("uhci_add_bulk: sqh=%p\n", sqh)); 1165 eqh = sc->sc_bulk_end; 1166 sqh->hlink = eqh->hlink; 1167 sqh->qh.qh_hlink = eqh->qh.qh_hlink; 1168 eqh->hlink = sqh; 1169 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH); 1170 sc->sc_bulk_end = sqh; 1171 uhci_add_loop(sc); 1172 } 1173 1174 /* Remove bulk QH, called at splusb(). */ 1175 void 1176 uhci_remove_bulk(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1177 { 1178 uhci_soft_qh_t *pqh; 1179 1180 SPLUSBCHECK; 1181 1182 DPRINTFN(10, ("uhci_remove_bulk: sqh=%p\n", sqh)); 1183 uhci_rem_loop(sc); 1184 /* See comment in uhci_remove_hs_ctrl() */ 1185 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { 1186 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 1187 delay(UHCI_QH_REMOVE_DELAY); 1188 } 1189 pqh = uhci_find_prev_qh(sc->sc_bulk_start, sqh); 1190 pqh->hlink = sqh->hlink; 1191 pqh->qh.qh_hlink = sqh->qh.qh_hlink; 1192 delay(UHCI_QH_REMOVE_DELAY); 1193 if (sc->sc_bulk_end == sqh) 1194 sc->sc_bulk_end = pqh; 1195 } 1196 1197 Static int uhci_intr1(uhci_softc_t *); 1198 1199 int 1200 uhci_intr(void *arg) 1201 { 1202 uhci_softc_t *sc = arg; 1203 1204 if (sc->sc_dying || !device_has_power(&sc->sc_bus.bdev)) 1205 return (0); 1206 1207 if (sc->sc_bus.use_polling) { 1208 #ifdef DIAGNOSTIC 1209 DPRINTFN(16, ("uhci_intr: ignored interrupt while polling\n")); 1210 #endif 1211 return (0); 1212 } 1213 1214 return (uhci_intr1(sc)); 1215 } 1216 1217 int 1218 uhci_intr1(uhci_softc_t *sc) 1219 { 1220 int status; 1221 int ack; 1222 1223 #ifdef UHCI_DEBUG 1224 if (uhcidebug > 15) { 1225 DPRINTF(("%s: uhci_intr1\n", USBDEVNAME(sc->sc_bus.bdev))); 1226 uhci_dumpregs(sc); 1227 } 1228 #endif 1229 1230 status = UREAD2(sc, UHCI_STS) & UHCI_STS_ALLINTRS; 1231 if (status == 0) /* The interrupt was not for us. */ 1232 return (0); 1233 1234 if (sc->sc_suspend != PWR_RESUME) { 1235 #ifdef DIAGNOSTIC 1236 printf("%s: interrupt while not operating ignored\n", 1237 USBDEVNAME(sc->sc_bus.bdev)); 1238 #endif 1239 UWRITE2(sc, UHCI_STS, status); /* acknowledge the ints */ 1240 return (0); 1241 } 1242 1243 ack = 0; 1244 if (status & UHCI_STS_USBINT) 1245 ack |= UHCI_STS_USBINT; 1246 if (status & UHCI_STS_USBEI) 1247 ack |= UHCI_STS_USBEI; 1248 if (status & UHCI_STS_RD) { 1249 ack |= UHCI_STS_RD; 1250 #ifdef UHCI_DEBUG 1251 printf("%s: resume detect\n", USBDEVNAME(sc->sc_bus.bdev)); 1252 #endif 1253 } 1254 if (status & UHCI_STS_HSE) { 1255 ack |= UHCI_STS_HSE; 1256 printf("%s: host system error\n", USBDEVNAME(sc->sc_bus.bdev)); 1257 } 1258 if (status & UHCI_STS_HCPE) { 1259 ack |= UHCI_STS_HCPE; 1260 printf("%s: host controller process error\n", 1261 USBDEVNAME(sc->sc_bus.bdev)); 1262 } 1263 if (status & UHCI_STS_HCH) { 1264 /* no acknowledge needed */ 1265 if (!sc->sc_dying) { 1266 printf("%s: host controller halted\n", 1267 USBDEVNAME(sc->sc_bus.bdev)); 1268 #ifdef UHCI_DEBUG 1269 uhci_dump_all(sc); 1270 #endif 1271 } 1272 sc->sc_dying = 1; 1273 } 1274 1275 if (!ack) 1276 return (0); /* nothing to acknowledge */ 1277 UWRITE2(sc, UHCI_STS, ack); /* acknowledge the ints */ 1278 1279 sc->sc_bus.no_intrs++; 1280 usb_schedsoftintr(&sc->sc_bus); 1281 1282 DPRINTFN(15, ("%s: uhci_intr: exit\n", USBDEVNAME(sc->sc_bus.bdev))); 1283 1284 return (1); 1285 } 1286 1287 void 1288 uhci_softintr(void *v) 1289 { 1290 uhci_softc_t *sc = v; 1291 uhci_intr_info_t *ii, *nextii; 1292 1293 DPRINTFN(10,("%s: uhci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev), 1294 sc->sc_bus.intr_context)); 1295 1296 sc->sc_bus.intr_context++; 1297 1298 /* 1299 * Interrupts on UHCI really suck. When the host controller 1300 * interrupts because a transfer is completed there is no 1301 * way of knowing which transfer it was. You can scan down 1302 * the TDs and QHs of the previous frame to limit the search, 1303 * but that assumes that the interrupt was not delayed by more 1304 * than 1 ms, which may not always be true (e.g. after debug 1305 * output on a slow console). 1306 * We scan all interrupt descriptors to see if any have 1307 * completed. 1308 */ 1309 for (ii = LIST_FIRST(&sc->sc_intrhead); ii; ii = nextii) { 1310 nextii = LIST_NEXT(ii, list); 1311 uhci_check_intr(sc, ii); 1312 } 1313 1314 #ifdef USB_USE_SOFTINTR 1315 if (sc->sc_softwake) { 1316 sc->sc_softwake = 0; 1317 wakeup(&sc->sc_softwake); 1318 } 1319 #endif /* USB_USE_SOFTINTR */ 1320 1321 sc->sc_bus.intr_context--; 1322 } 1323 1324 /* Check for an interrupt. */ 1325 void 1326 uhci_check_intr(uhci_softc_t *sc, uhci_intr_info_t *ii) 1327 { 1328 uhci_soft_td_t *std, *lstd; 1329 u_int32_t status; 1330 1331 DPRINTFN(15, ("uhci_check_intr: ii=%p\n", ii)); 1332 #ifdef DIAGNOSTIC 1333 if (ii == NULL) { 1334 printf("uhci_check_intr: no ii? %p\n", ii); 1335 return; 1336 } 1337 #endif 1338 if (ii->xfer->status == USBD_CANCELLED || 1339 ii->xfer->status == USBD_TIMEOUT) { 1340 DPRINTF(("uhci_check_intr: aborted xfer=%p\n", ii->xfer)); 1341 return; 1342 } 1343 1344 if (ii->stdstart == NULL) 1345 return; 1346 lstd = ii->stdend; 1347 #ifdef DIAGNOSTIC 1348 if (lstd == NULL) { 1349 printf("uhci_check_intr: std==0\n"); 1350 return; 1351 } 1352 #endif 1353 /* 1354 * If the last TD is still active we need to check whether there 1355 * is an error somewhere in the middle, or whether there was a 1356 * short packet (SPD and not ACTIVE). 1357 */ 1358 if (le32toh(lstd->td.td_status) & UHCI_TD_ACTIVE) { 1359 DPRINTFN(12, ("uhci_check_intr: active ii=%p\n", ii)); 1360 for (std = ii->stdstart; std != lstd; std = std->link.std) { 1361 status = le32toh(std->td.td_status); 1362 /* If there's an active TD the xfer isn't done. */ 1363 if (status & UHCI_TD_ACTIVE) 1364 break; 1365 /* Any kind of error makes the xfer done. */ 1366 if (status & UHCI_TD_STALLED) 1367 goto done; 1368 /* We want short packets, and it is short: it's done */ 1369 if ((status & UHCI_TD_SPD) && 1370 UHCI_TD_GET_ACTLEN(status) < 1371 UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token))) 1372 goto done; 1373 } 1374 DPRINTFN(12, ("uhci_check_intr: ii=%p std=%p still active\n", 1375 ii, ii->stdstart)); 1376 return; 1377 } 1378 done: 1379 DPRINTFN(12, ("uhci_check_intr: ii=%p done\n", ii)); 1380 usb_uncallout(ii->xfer->timeout_handle, uhci_timeout, ii); 1381 uhci_idone(ii); 1382 } 1383 1384 /* Called at splusb() */ 1385 void 1386 uhci_idone(uhci_intr_info_t *ii) 1387 { 1388 usbd_xfer_handle xfer = ii->xfer; 1389 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 1390 uhci_soft_td_t *std; 1391 u_int32_t status = 0, nstatus; 1392 int actlen; 1393 1394 DPRINTFN(12, ("uhci_idone: ii=%p\n", ii)); 1395 #ifdef DIAGNOSTIC 1396 { 1397 int s = splhigh(); 1398 if (ii->isdone) { 1399 splx(s); 1400 #ifdef UHCI_DEBUG 1401 printf("uhci_idone: ii is done!\n "); 1402 uhci_dump_ii(ii); 1403 #else 1404 printf("uhci_idone: ii=%p is done!\n", ii); 1405 #endif 1406 return; 1407 } 1408 ii->isdone = 1; 1409 splx(s); 1410 } 1411 #endif 1412 1413 if (xfer->nframes != 0) { 1414 /* Isoc transfer, do things differently. */ 1415 uhci_soft_td_t **stds = upipe->u.iso.stds; 1416 int i, n, nframes, len; 1417 1418 DPRINTFN(5,("uhci_idone: ii=%p isoc ready\n", ii)); 1419 1420 nframes = xfer->nframes; 1421 actlen = 0; 1422 n = UXFER(xfer)->curframe; 1423 for (i = 0; i < nframes; i++) { 1424 std = stds[n]; 1425 #ifdef UHCI_DEBUG 1426 if (uhcidebug > 5) { 1427 DPRINTFN(-1,("uhci_idone: isoc TD %d\n", i)); 1428 uhci_dump_td(std); 1429 } 1430 #endif 1431 if (++n >= UHCI_VFRAMELIST_COUNT) 1432 n = 0; 1433 status = le32toh(std->td.td_status); 1434 len = UHCI_TD_GET_ACTLEN(status); 1435 xfer->frlengths[i] = len; 1436 actlen += len; 1437 } 1438 upipe->u.iso.inuse -= nframes; 1439 xfer->actlen = actlen; 1440 xfer->status = USBD_NORMAL_COMPLETION; 1441 goto end; 1442 } 1443 1444 #ifdef UHCI_DEBUG 1445 DPRINTFN(10, ("uhci_idone: ii=%p, xfer=%p, pipe=%p ready\n", 1446 ii, xfer, upipe)); 1447 if (uhcidebug > 10) 1448 uhci_dump_tds(ii->stdstart); 1449 #endif 1450 1451 /* The transfer is done, compute actual length and status. */ 1452 actlen = 0; 1453 for (std = ii->stdstart; std != NULL; std = std->link.std) { 1454 nstatus = le32toh(std->td.td_status); 1455 if (nstatus & UHCI_TD_ACTIVE) 1456 break; 1457 1458 status = nstatus; 1459 if (UHCI_TD_GET_PID(le32toh(std->td.td_token)) != 1460 UHCI_TD_PID_SETUP) 1461 actlen += UHCI_TD_GET_ACTLEN(status); 1462 else { 1463 /* 1464 * UHCI will report CRCTO in addition to a STALL or NAK 1465 * for a SETUP transaction. See section 3.2.2, "TD 1466 * CONTROL AND STATUS". 1467 */ 1468 if (status & (UHCI_TD_STALLED | UHCI_TD_NAK)) 1469 status &= ~UHCI_TD_CRCTO; 1470 } 1471 } 1472 /* If there are left over TDs we need to update the toggle. */ 1473 if (std != NULL) 1474 upipe->nexttoggle = UHCI_TD_GET_DT(le32toh(std->td.td_token)); 1475 1476 status &= UHCI_TD_ERROR; 1477 DPRINTFN(10, ("uhci_idone: actlen=%d, status=0x%x\n", 1478 actlen, status)); 1479 xfer->actlen = actlen; 1480 if (status != 0) { 1481 #ifdef UHCI_DEBUG 1482 char sbuf[128]; 1483 1484 bitmask_snprintf((u_int32_t)status, 1485 "\20\22BITSTUFF\23CRCTO\24NAK\25" 1486 "BABBLE\26DBUFFER\27STALLED\30ACTIVE", 1487 sbuf, sizeof(sbuf)); 1488 1489 DPRINTFN((status == UHCI_TD_STALLED)*10, 1490 ("uhci_idone: error, addr=%d, endpt=0x%02x, " 1491 "status 0x%s\n", 1492 xfer->pipe->device->address, 1493 xfer->pipe->endpoint->edesc->bEndpointAddress, 1494 sbuf)); 1495 #endif 1496 1497 if (status == UHCI_TD_STALLED) 1498 xfer->status = USBD_STALLED; 1499 else 1500 xfer->status = USBD_IOERROR; /* more info XXX */ 1501 } else { 1502 xfer->status = USBD_NORMAL_COMPLETION; 1503 } 1504 1505 end: 1506 usb_transfer_complete(xfer); 1507 DPRINTFN(12, ("uhci_idone: ii=%p done\n", ii)); 1508 } 1509 1510 /* 1511 * Called when a request does not complete. 1512 */ 1513 void 1514 uhci_timeout(void *addr) 1515 { 1516 uhci_intr_info_t *ii = addr; 1517 struct uhci_xfer *uxfer = UXFER(ii->xfer); 1518 struct uhci_pipe *upipe = (struct uhci_pipe *)uxfer->xfer.pipe; 1519 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus; 1520 1521 DPRINTF(("uhci_timeout: uxfer=%p\n", uxfer)); 1522 1523 if (sc->sc_dying) { 1524 uhci_abort_xfer(&uxfer->xfer, USBD_TIMEOUT); 1525 return; 1526 } 1527 1528 /* Execute the abort in a process context. */ 1529 usb_init_task(&uxfer->abort_task, uhci_timeout_task, ii->xfer); 1530 usb_add_task(uxfer->xfer.pipe->device, &uxfer->abort_task, 1531 USB_TASKQ_HC); 1532 } 1533 1534 void 1535 uhci_timeout_task(void *addr) 1536 { 1537 usbd_xfer_handle xfer = addr; 1538 int s; 1539 1540 DPRINTF(("uhci_timeout_task: xfer=%p\n", xfer)); 1541 1542 s = splusb(); 1543 uhci_abort_xfer(xfer, USBD_TIMEOUT); 1544 splx(s); 1545 } 1546 1547 /* 1548 * Wait here until controller claims to have an interrupt. 1549 * Then call uhci_intr and return. Use timeout to avoid waiting 1550 * too long. 1551 * Only used during boot when interrupts are not enabled yet. 1552 */ 1553 void 1554 uhci_waitintr(uhci_softc_t *sc, usbd_xfer_handle xfer) 1555 { 1556 int timo = xfer->timeout; 1557 uhci_intr_info_t *ii; 1558 1559 DPRINTFN(10,("uhci_waitintr: timeout = %dms\n", timo)); 1560 1561 xfer->status = USBD_IN_PROGRESS; 1562 for (; timo >= 0; timo--) { 1563 usb_delay_ms(&sc->sc_bus, 1); 1564 DPRINTFN(20,("uhci_waitintr: 0x%04x\n", UREAD2(sc, UHCI_STS))); 1565 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) { 1566 uhci_intr1(sc); 1567 if (xfer->status != USBD_IN_PROGRESS) 1568 return; 1569 } 1570 } 1571 1572 /* Timeout */ 1573 DPRINTF(("uhci_waitintr: timeout\n")); 1574 for (ii = LIST_FIRST(&sc->sc_intrhead); 1575 ii != NULL && ii->xfer != xfer; 1576 ii = LIST_NEXT(ii, list)) 1577 ; 1578 #ifdef DIAGNOSTIC 1579 if (ii == NULL) 1580 panic("uhci_waitintr: lost intr_info"); 1581 #endif 1582 uhci_idone(ii); 1583 } 1584 1585 void 1586 uhci_poll(struct usbd_bus *bus) 1587 { 1588 uhci_softc_t *sc = (uhci_softc_t *)bus; 1589 1590 if (UREAD2(sc, UHCI_STS) & UHCI_STS_USBINT) 1591 uhci_intr1(sc); 1592 } 1593 1594 void 1595 uhci_reset(uhci_softc_t *sc) 1596 { 1597 int n; 1598 1599 UHCICMD(sc, UHCI_CMD_HCRESET); 1600 /* The reset bit goes low when the controller is done. */ 1601 for (n = 0; n < UHCI_RESET_TIMEOUT && 1602 (UREAD2(sc, UHCI_CMD) & UHCI_CMD_HCRESET); n++) 1603 usb_delay_ms(&sc->sc_bus, 1); 1604 if (n >= UHCI_RESET_TIMEOUT) 1605 printf("%s: controller did not reset\n", 1606 USBDEVNAME(sc->sc_bus.bdev)); 1607 } 1608 1609 usbd_status 1610 uhci_run(uhci_softc_t *sc, int run) 1611 { 1612 int s, n, running; 1613 u_int16_t cmd; 1614 1615 run = run != 0; 1616 s = splhardusb(); 1617 DPRINTF(("uhci_run: setting run=%d\n", run)); 1618 cmd = UREAD2(sc, UHCI_CMD); 1619 if (run) 1620 cmd |= UHCI_CMD_RS; 1621 else 1622 cmd &= ~UHCI_CMD_RS; 1623 UHCICMD(sc, cmd); 1624 for(n = 0; n < 10; n++) { 1625 running = !(UREAD2(sc, UHCI_STS) & UHCI_STS_HCH); 1626 /* return when we've entered the state we want */ 1627 if (run == running) { 1628 splx(s); 1629 DPRINTF(("uhci_run: done cmd=0x%x sts=0x%x\n", 1630 UREAD2(sc, UHCI_CMD), UREAD2(sc, UHCI_STS))); 1631 return (USBD_NORMAL_COMPLETION); 1632 } 1633 usb_delay_ms(&sc->sc_bus, 1); 1634 } 1635 splx(s); 1636 printf("%s: cannot %s\n", USBDEVNAME(sc->sc_bus.bdev), 1637 run ? "start" : "stop"); 1638 return (USBD_IOERROR); 1639 } 1640 1641 /* 1642 * Memory management routines. 1643 * uhci_alloc_std allocates TDs 1644 * uhci_alloc_sqh allocates QHs 1645 * These two routines do their own free list management, 1646 * partly for speed, partly because allocating DMAable memory 1647 * has page size granularaity so much memory would be wasted if 1648 * only one TD/QH (32 bytes) was placed in each allocated chunk. 1649 */ 1650 1651 uhci_soft_td_t * 1652 uhci_alloc_std(uhci_softc_t *sc) 1653 { 1654 uhci_soft_td_t *std; 1655 usbd_status err; 1656 int i, offs; 1657 usb_dma_t dma; 1658 1659 if (sc->sc_freetds == NULL) { 1660 DPRINTFN(2,("uhci_alloc_std: allocating chunk\n")); 1661 err = usb_allocmem(&sc->sc_bus, UHCI_STD_SIZE * UHCI_STD_CHUNK, 1662 UHCI_TD_ALIGN, &dma); 1663 if (err) 1664 return (0); 1665 for(i = 0; i < UHCI_STD_CHUNK; i++) { 1666 offs = i * UHCI_STD_SIZE; 1667 std = KERNADDR(&dma, offs); 1668 std->physaddr = DMAADDR(&dma, offs); 1669 std->link.std = sc->sc_freetds; 1670 sc->sc_freetds = std; 1671 } 1672 } 1673 std = sc->sc_freetds; 1674 sc->sc_freetds = std->link.std; 1675 memset(&std->td, 0, sizeof(uhci_td_t)); 1676 return std; 1677 } 1678 1679 void 1680 uhci_free_std(uhci_softc_t *sc, uhci_soft_td_t *std) 1681 { 1682 #ifdef DIAGNOSTIC 1683 #define TD_IS_FREE 0x12345678 1684 if (le32toh(std->td.td_token) == TD_IS_FREE) { 1685 printf("uhci_free_std: freeing free TD %p\n", std); 1686 return; 1687 } 1688 std->td.td_token = htole32(TD_IS_FREE); 1689 #endif 1690 std->link.std = sc->sc_freetds; 1691 sc->sc_freetds = std; 1692 } 1693 1694 uhci_soft_qh_t * 1695 uhci_alloc_sqh(uhci_softc_t *sc) 1696 { 1697 uhci_soft_qh_t *sqh; 1698 usbd_status err; 1699 int i, offs; 1700 usb_dma_t dma; 1701 1702 if (sc->sc_freeqhs == NULL) { 1703 DPRINTFN(2, ("uhci_alloc_sqh: allocating chunk\n")); 1704 err = usb_allocmem(&sc->sc_bus, UHCI_SQH_SIZE * UHCI_SQH_CHUNK, 1705 UHCI_QH_ALIGN, &dma); 1706 if (err) 1707 return (0); 1708 for(i = 0; i < UHCI_SQH_CHUNK; i++) { 1709 offs = i * UHCI_SQH_SIZE; 1710 sqh = KERNADDR(&dma, offs); 1711 sqh->physaddr = DMAADDR(&dma, offs); 1712 sqh->hlink = sc->sc_freeqhs; 1713 sc->sc_freeqhs = sqh; 1714 } 1715 } 1716 sqh = sc->sc_freeqhs; 1717 sc->sc_freeqhs = sqh->hlink; 1718 memset(&sqh->qh, 0, sizeof(uhci_qh_t)); 1719 return (sqh); 1720 } 1721 1722 void 1723 uhci_free_sqh(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 1724 { 1725 sqh->hlink = sc->sc_freeqhs; 1726 sc->sc_freeqhs = sqh; 1727 } 1728 1729 void 1730 uhci_free_std_chain(uhci_softc_t *sc, uhci_soft_td_t *std, 1731 uhci_soft_td_t *stdend) 1732 { 1733 uhci_soft_td_t *p; 1734 1735 for (; std != stdend; std = p) { 1736 p = std->link.std; 1737 uhci_free_std(sc, std); 1738 } 1739 } 1740 1741 usbd_status 1742 uhci_alloc_std_chain(struct uhci_pipe *upipe, uhci_softc_t *sc, int len, 1743 int rd, u_int16_t flags, usb_dma_t *dma, 1744 uhci_soft_td_t **sp, uhci_soft_td_t **ep) 1745 { 1746 uhci_soft_td_t *p, *lastp; 1747 uhci_physaddr_t lastlink; 1748 int i, ntd, l, tog, maxp; 1749 u_int32_t status; 1750 int addr = upipe->pipe.device->address; 1751 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 1752 1753 DPRINTFN(8, ("uhci_alloc_std_chain: addr=%d endpt=%d len=%d speed=%d " 1754 "flags=0x%x\n", addr, UE_GET_ADDR(endpt), len, 1755 upipe->pipe.device->speed, flags)); 1756 maxp = UGETW(upipe->pipe.endpoint->edesc->wMaxPacketSize); 1757 if (maxp == 0) { 1758 printf("uhci_alloc_std_chain: maxp=0\n"); 1759 return (USBD_INVAL); 1760 } 1761 ntd = (len + maxp - 1) / maxp; 1762 if ((flags & USBD_FORCE_SHORT_XFER) && len % maxp == 0) 1763 ntd++; 1764 DPRINTFN(10, ("uhci_alloc_std_chain: maxp=%d ntd=%d\n", maxp, ntd)); 1765 if (ntd == 0) { 1766 *sp = *ep = 0; 1767 DPRINTFN(-1,("uhci_alloc_std_chain: ntd=0\n")); 1768 return (USBD_NORMAL_COMPLETION); 1769 } 1770 tog = upipe->nexttoggle; 1771 if (ntd % 2 == 0) 1772 tog ^= 1; 1773 upipe->nexttoggle = tog ^ 1; 1774 lastp = NULL; 1775 lastlink = UHCI_PTR_T; 1776 ntd--; 1777 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(3) | UHCI_TD_ACTIVE); 1778 if (upipe->pipe.device->speed == USB_SPEED_LOW) 1779 status |= UHCI_TD_LS; 1780 if (flags & USBD_SHORT_XFER_OK) 1781 status |= UHCI_TD_SPD; 1782 for (i = ntd; i >= 0; i--) { 1783 p = uhci_alloc_std(sc); 1784 if (p == NULL) { 1785 KASSERT(lastp != NULL); 1786 uhci_free_std_chain(sc, lastp, NULL); 1787 return (USBD_NOMEM); 1788 } 1789 p->link.std = lastp; 1790 p->td.td_link = htole32(lastlink | UHCI_PTR_VF | UHCI_PTR_TD); 1791 lastp = p; 1792 lastlink = p->physaddr; 1793 p->td.td_status = htole32(status); 1794 if (i == ntd) { 1795 /* last TD */ 1796 l = len % maxp; 1797 if (l == 0 && !(flags & USBD_FORCE_SHORT_XFER)) 1798 l = maxp; 1799 *ep = p; 1800 } else 1801 l = maxp; 1802 p->td.td_token = 1803 htole32(rd ? UHCI_TD_IN (l, endpt, addr, tog) : 1804 UHCI_TD_OUT(l, endpt, addr, tog)); 1805 p->td.td_buffer = htole32(DMAADDR(dma, i * maxp)); 1806 tog ^= 1; 1807 } 1808 *sp = lastp; 1809 DPRINTFN(10, ("uhci_alloc_std_chain: nexttog=%d\n", 1810 upipe->nexttoggle)); 1811 return (USBD_NORMAL_COMPLETION); 1812 } 1813 1814 void 1815 uhci_device_clear_toggle(usbd_pipe_handle pipe) 1816 { 1817 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 1818 upipe->nexttoggle = 0; 1819 } 1820 1821 void 1822 uhci_noop(usbd_pipe_handle pipe) 1823 { 1824 } 1825 1826 usbd_status 1827 uhci_device_bulk_transfer(usbd_xfer_handle xfer) 1828 { 1829 usbd_status err; 1830 1831 /* Insert last in queue. */ 1832 err = usb_insert_transfer(xfer); 1833 if (err) 1834 return (err); 1835 1836 /* 1837 * Pipe isn't running (otherwise err would be USBD_INPROG), 1838 * so start it first. 1839 */ 1840 return (uhci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 1841 } 1842 1843 usbd_status 1844 uhci_device_bulk_start(usbd_xfer_handle xfer) 1845 { 1846 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 1847 usbd_device_handle dev = upipe->pipe.device; 1848 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 1849 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 1850 uhci_soft_td_t *data, *dataend; 1851 uhci_soft_qh_t *sqh; 1852 usbd_status err; 1853 int len, isread, endpt; 1854 int s; 1855 1856 DPRINTFN(3, ("uhci_device_bulk_start: xfer=%p len=%d flags=%d ii=%p\n", 1857 xfer, xfer->length, xfer->flags, ii)); 1858 1859 if (sc->sc_dying) 1860 return (USBD_IOERROR); 1861 1862 #ifdef DIAGNOSTIC 1863 if (xfer->rqflags & URQ_REQUEST) 1864 panic("uhci_device_bulk_transfer: a request"); 1865 #endif 1866 1867 len = xfer->length; 1868 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 1869 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 1870 sqh = upipe->u.bulk.sqh; 1871 1872 upipe->u.bulk.isread = isread; 1873 upipe->u.bulk.length = len; 1874 1875 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, 1876 &xfer->dmabuf, &data, &dataend); 1877 if (err) 1878 return (err); 1879 dataend->td.td_status |= htole32(UHCI_TD_IOC); 1880 1881 #ifdef UHCI_DEBUG 1882 if (uhcidebug > 8) { 1883 DPRINTF(("uhci_device_bulk_transfer: data(1)\n")); 1884 uhci_dump_tds(data); 1885 } 1886 #endif 1887 1888 /* Set up interrupt info. */ 1889 ii->xfer = xfer; 1890 ii->stdstart = data; 1891 ii->stdend = dataend; 1892 #ifdef DIAGNOSTIC 1893 if (!ii->isdone) { 1894 printf("uhci_device_bulk_transfer: not done, ii=%p\n", ii); 1895 } 1896 ii->isdone = 0; 1897 #endif 1898 1899 sqh->elink = data; 1900 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD); 1901 1902 s = splusb(); 1903 uhci_add_bulk(sc, sqh); 1904 uhci_add_intr_info(sc, ii); 1905 1906 if (xfer->timeout && !sc->sc_bus.use_polling) { 1907 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout), 1908 uhci_timeout, ii); 1909 } 1910 xfer->status = USBD_IN_PROGRESS; 1911 splx(s); 1912 1913 #ifdef UHCI_DEBUG 1914 if (uhcidebug > 10) { 1915 DPRINTF(("uhci_device_bulk_transfer: data(2)\n")); 1916 uhci_dump_tds(data); 1917 } 1918 #endif 1919 1920 if (sc->sc_bus.use_polling) 1921 uhci_waitintr(sc, xfer); 1922 1923 return (USBD_IN_PROGRESS); 1924 } 1925 1926 /* Abort a device bulk request. */ 1927 void 1928 uhci_device_bulk_abort(usbd_xfer_handle xfer) 1929 { 1930 DPRINTF(("uhci_device_bulk_abort:\n")); 1931 uhci_abort_xfer(xfer, USBD_CANCELLED); 1932 } 1933 1934 /* 1935 * Abort a device request. 1936 * If this routine is called at splusb() it guarantees that the request 1937 * will be removed from the hardware scheduling and that the callback 1938 * for it will be called with USBD_CANCELLED status. 1939 * It's impossible to guarantee that the requested transfer will not 1940 * have happened since the hardware runs concurrently. 1941 * If the transaction has already happened we rely on the ordinary 1942 * interrupt processing to process it. 1943 */ 1944 void 1945 uhci_abort_xfer(usbd_xfer_handle xfer, usbd_status status) 1946 { 1947 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 1948 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 1949 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus; 1950 uhci_soft_td_t *std; 1951 int s; 1952 int wake; 1953 1954 DPRINTFN(1,("uhci_abort_xfer: xfer=%p, status=%d\n", xfer, status)); 1955 1956 if (sc->sc_dying) { 1957 /* If we're dying, just do the software part. */ 1958 s = splusb(); 1959 xfer->status = status; /* make software ignore it */ 1960 usb_uncallout(xfer->timeout_handle, uhci_timeout, xfer); 1961 usb_transfer_complete(xfer); 1962 splx(s); 1963 return; 1964 } 1965 1966 if (xfer->device->bus->intr_context || !curproc) 1967 panic("uhci_abort_xfer: not in process context"); 1968 1969 /* 1970 * If an abort is already in progress then just wait for it to 1971 * complete and return. 1972 */ 1973 if (xfer->hcflags & UXFER_ABORTING) { 1974 DPRINTFN(2, ("uhci_abort_xfer: already aborting\n")); 1975 #ifdef DIAGNOSTIC 1976 if (status == USBD_TIMEOUT) 1977 printf("uhci_abort_xfer: TIMEOUT while aborting\n"); 1978 #endif 1979 /* Override the status which might be USBD_TIMEOUT. */ 1980 xfer->status = status; 1981 DPRINTFN(2, ("uhci_abort_xfer: waiting for abort to finish\n")); 1982 xfer->hcflags |= UXFER_ABORTWAIT; 1983 while (xfer->hcflags & UXFER_ABORTING) 1984 tsleep(&xfer->hcflags, PZERO, "uhciaw", 0); 1985 return; 1986 } 1987 xfer->hcflags |= UXFER_ABORTING; 1988 1989 /* 1990 * Step 1: Make interrupt routine and hardware ignore xfer. 1991 */ 1992 s = splusb(); 1993 xfer->status = status; /* make software ignore it */ 1994 usb_uncallout(xfer->timeout_handle, uhci_timeout, ii); 1995 DPRINTFN(1,("uhci_abort_xfer: stop ii=%p\n", ii)); 1996 for (std = ii->stdstart; std != NULL; std = std->link.std) 1997 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC)); 1998 splx(s); 1999 2000 /* 2001 * Step 2: Wait until we know hardware has finished any possible 2002 * use of the xfer. Also make sure the soft interrupt routine 2003 * has run. 2004 */ 2005 usb_delay_ms(upipe->pipe.device->bus, 2); /* Hardware finishes in 1ms */ 2006 s = splusb(); 2007 #ifdef USB_USE_SOFTINTR 2008 sc->sc_softwake = 1; 2009 #endif /* USB_USE_SOFTINTR */ 2010 usb_schedsoftintr(&sc->sc_bus); 2011 #ifdef USB_USE_SOFTINTR 2012 DPRINTFN(1,("uhci_abort_xfer: tsleep\n")); 2013 tsleep(&sc->sc_softwake, PZERO, "uhciab", 0); 2014 #endif /* USB_USE_SOFTINTR */ 2015 splx(s); 2016 2017 /* 2018 * Step 3: Execute callback. 2019 */ 2020 DPRINTFN(1,("uhci_abort_xfer: callback\n")); 2021 s = splusb(); 2022 #ifdef DIAGNOSTIC 2023 ii->isdone = 1; 2024 #endif 2025 wake = xfer->hcflags & UXFER_ABORTWAIT; 2026 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT); 2027 usb_transfer_complete(xfer); 2028 if (wake) 2029 wakeup(&xfer->hcflags); 2030 splx(s); 2031 } 2032 2033 /* Close a device bulk pipe. */ 2034 void 2035 uhci_device_bulk_close(usbd_pipe_handle pipe) 2036 { 2037 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2038 usbd_device_handle dev = upipe->pipe.device; 2039 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2040 2041 uhci_free_sqh(sc, upipe->u.bulk.sqh); 2042 } 2043 2044 usbd_status 2045 uhci_device_ctrl_transfer(usbd_xfer_handle xfer) 2046 { 2047 usbd_status err; 2048 2049 /* Insert last in queue. */ 2050 err = usb_insert_transfer(xfer); 2051 if (err) 2052 return (err); 2053 2054 /* 2055 * Pipe isn't running (otherwise err would be USBD_INPROG), 2056 * so start it first. 2057 */ 2058 return (uhci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2059 } 2060 2061 usbd_status 2062 uhci_device_ctrl_start(usbd_xfer_handle xfer) 2063 { 2064 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus; 2065 usbd_status err; 2066 2067 if (sc->sc_dying) 2068 return (USBD_IOERROR); 2069 2070 #ifdef DIAGNOSTIC 2071 if (!(xfer->rqflags & URQ_REQUEST)) 2072 panic("uhci_device_ctrl_transfer: not a request"); 2073 #endif 2074 2075 err = uhci_device_request(xfer); 2076 if (err) 2077 return (err); 2078 2079 if (sc->sc_bus.use_polling) 2080 uhci_waitintr(sc, xfer); 2081 return (USBD_IN_PROGRESS); 2082 } 2083 2084 usbd_status 2085 uhci_device_intr_transfer(usbd_xfer_handle xfer) 2086 { 2087 usbd_status err; 2088 2089 /* Insert last in queue. */ 2090 err = usb_insert_transfer(xfer); 2091 if (err) 2092 return (err); 2093 2094 /* 2095 * Pipe isn't running (otherwise err would be USBD_INPROG), 2096 * so start it first. 2097 */ 2098 return (uhci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2099 } 2100 2101 usbd_status 2102 uhci_device_intr_start(usbd_xfer_handle xfer) 2103 { 2104 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2105 usbd_device_handle dev = upipe->pipe.device; 2106 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2107 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2108 uhci_soft_td_t *data, *dataend; 2109 uhci_soft_qh_t *sqh; 2110 usbd_status err; 2111 int isread, endpt; 2112 int i, s; 2113 2114 if (sc->sc_dying) 2115 return (USBD_IOERROR); 2116 2117 DPRINTFN(3,("uhci_device_intr_transfer: xfer=%p len=%d flags=%d\n", 2118 xfer, xfer->length, xfer->flags)); 2119 2120 #ifdef DIAGNOSTIC 2121 if (xfer->rqflags & URQ_REQUEST) 2122 panic("uhci_device_intr_transfer: a request"); 2123 #endif 2124 2125 endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2126 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2127 2128 upipe->u.intr.isread = isread; 2129 2130 err = uhci_alloc_std_chain(upipe, sc, xfer->length, isread, 2131 xfer->flags, &xfer->dmabuf, &data, 2132 &dataend); 2133 if (err) 2134 return (err); 2135 dataend->td.td_status |= htole32(UHCI_TD_IOC); 2136 2137 #ifdef UHCI_DEBUG 2138 if (uhcidebug > 10) { 2139 DPRINTF(("uhci_device_intr_transfer: data(1)\n")); 2140 uhci_dump_tds(data); 2141 uhci_dump_qh(upipe->u.intr.qhs[0]); 2142 } 2143 #endif 2144 2145 s = splusb(); 2146 /* Set up interrupt info. */ 2147 ii->xfer = xfer; 2148 ii->stdstart = data; 2149 ii->stdend = dataend; 2150 #ifdef DIAGNOSTIC 2151 if (!ii->isdone) { 2152 printf("uhci_device_intr_transfer: not done, ii=%p\n", ii); 2153 } 2154 ii->isdone = 0; 2155 #endif 2156 2157 DPRINTFN(10,("uhci_device_intr_transfer: qhs[0]=%p\n", 2158 upipe->u.intr.qhs[0])); 2159 for (i = 0; i < upipe->u.intr.npoll; i++) { 2160 sqh = upipe->u.intr.qhs[i]; 2161 sqh->elink = data; 2162 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD); 2163 } 2164 uhci_add_intr_info(sc, ii); 2165 xfer->status = USBD_IN_PROGRESS; 2166 splx(s); 2167 2168 #ifdef UHCI_DEBUG 2169 if (uhcidebug > 10) { 2170 DPRINTF(("uhci_device_intr_transfer: data(2)\n")); 2171 uhci_dump_tds(data); 2172 uhci_dump_qh(upipe->u.intr.qhs[0]); 2173 } 2174 #endif 2175 2176 return (USBD_IN_PROGRESS); 2177 } 2178 2179 /* Abort a device control request. */ 2180 void 2181 uhci_device_ctrl_abort(usbd_xfer_handle xfer) 2182 { 2183 DPRINTF(("uhci_device_ctrl_abort:\n")); 2184 uhci_abort_xfer(xfer, USBD_CANCELLED); 2185 } 2186 2187 /* Close a device control pipe. */ 2188 void 2189 uhci_device_ctrl_close(usbd_pipe_handle pipe) 2190 { 2191 } 2192 2193 /* Abort a device interrupt request. */ 2194 void 2195 uhci_device_intr_abort(usbd_xfer_handle xfer) 2196 { 2197 DPRINTFN(1,("uhci_device_intr_abort: xfer=%p\n", xfer)); 2198 if (xfer->pipe->intrxfer == xfer) { 2199 DPRINTFN(1,("uhci_device_intr_abort: remove\n")); 2200 xfer->pipe->intrxfer = NULL; 2201 } 2202 uhci_abort_xfer(xfer, USBD_CANCELLED); 2203 } 2204 2205 /* Close a device interrupt pipe. */ 2206 void 2207 uhci_device_intr_close(usbd_pipe_handle pipe) 2208 { 2209 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2210 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; 2211 int i, npoll; 2212 int s; 2213 2214 /* Unlink descriptors from controller data structures. */ 2215 npoll = upipe->u.intr.npoll; 2216 s = splusb(); 2217 for (i = 0; i < npoll; i++) 2218 uhci_remove_intr(sc, upipe->u.intr.qhs[i]); 2219 splx(s); 2220 2221 /* 2222 * We now have to wait for any activity on the physical 2223 * descriptors to stop. 2224 */ 2225 usb_delay_ms(&sc->sc_bus, 2); 2226 2227 for(i = 0; i < npoll; i++) 2228 uhci_free_sqh(sc, upipe->u.intr.qhs[i]); 2229 free(upipe->u.intr.qhs, M_USBHC); 2230 2231 /* XXX free other resources */ 2232 } 2233 2234 usbd_status 2235 uhci_device_request(usbd_xfer_handle xfer) 2236 { 2237 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2238 usb_device_request_t *req = &xfer->request; 2239 usbd_device_handle dev = upipe->pipe.device; 2240 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2241 int addr = dev->address; 2242 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2243 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2244 uhci_soft_td_t *setup, *data, *stat, *next, *dataend; 2245 uhci_soft_qh_t *sqh; 2246 int len; 2247 u_int32_t ls; 2248 usbd_status err; 2249 int isread; 2250 int s; 2251 2252 DPRINTFN(3,("uhci_device_control type=0x%02x, request=0x%02x, " 2253 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n", 2254 req->bmRequestType, req->bRequest, UGETW(req->wValue), 2255 UGETW(req->wIndex), UGETW(req->wLength), 2256 addr, endpt)); 2257 2258 ls = dev->speed == USB_SPEED_LOW ? UHCI_TD_LS : 0; 2259 isread = req->bmRequestType & UT_READ; 2260 len = UGETW(req->wLength); 2261 2262 setup = upipe->u.ctl.setup; 2263 stat = upipe->u.ctl.stat; 2264 sqh = upipe->u.ctl.sqh; 2265 2266 /* Set up data transaction */ 2267 if (len != 0) { 2268 upipe->nexttoggle = 1; 2269 err = uhci_alloc_std_chain(upipe, sc, len, isread, xfer->flags, 2270 &xfer->dmabuf, &data, &dataend); 2271 if (err) 2272 return (err); 2273 next = data; 2274 dataend->link.std = stat; 2275 dataend->td.td_link = htole32(stat->physaddr | UHCI_PTR_VF | UHCI_PTR_TD); 2276 } else { 2277 next = stat; 2278 } 2279 upipe->u.ctl.length = len; 2280 2281 memcpy(KERNADDR(&upipe->u.ctl.reqdma, 0), req, sizeof *req); 2282 2283 setup->link.std = next; 2284 setup->td.td_link = htole32(next->physaddr | UHCI_PTR_VF | UHCI_PTR_TD); 2285 setup->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls | 2286 UHCI_TD_ACTIVE); 2287 setup->td.td_token = htole32(UHCI_TD_SETUP(sizeof *req, endpt, addr)); 2288 setup->td.td_buffer = htole32(DMAADDR(&upipe->u.ctl.reqdma, 0)); 2289 2290 stat->link.std = NULL; 2291 stat->td.td_link = htole32(UHCI_PTR_T); 2292 stat->td.td_status = htole32(UHCI_TD_SET_ERRCNT(3) | ls | 2293 UHCI_TD_ACTIVE | UHCI_TD_IOC); 2294 stat->td.td_token = 2295 htole32(isread ? UHCI_TD_OUT(0, endpt, addr, 1) : 2296 UHCI_TD_IN (0, endpt, addr, 1)); 2297 stat->td.td_buffer = htole32(0); 2298 2299 #ifdef UHCI_DEBUG 2300 if (uhcidebug > 10) { 2301 DPRINTF(("uhci_device_request: before transfer\n")); 2302 uhci_dump_tds(setup); 2303 } 2304 #endif 2305 2306 /* Set up interrupt info. */ 2307 ii->xfer = xfer; 2308 ii->stdstart = setup; 2309 ii->stdend = stat; 2310 #ifdef DIAGNOSTIC 2311 if (!ii->isdone) { 2312 printf("uhci_device_request: not done, ii=%p\n", ii); 2313 } 2314 ii->isdone = 0; 2315 #endif 2316 2317 sqh->elink = setup; 2318 sqh->qh.qh_elink = htole32(setup->physaddr | UHCI_PTR_TD); 2319 2320 s = splusb(); 2321 if (dev->speed == USB_SPEED_LOW) 2322 uhci_add_ls_ctrl(sc, sqh); 2323 else 2324 uhci_add_hs_ctrl(sc, sqh); 2325 uhci_add_intr_info(sc, ii); 2326 #ifdef UHCI_DEBUG 2327 if (uhcidebug > 12) { 2328 uhci_soft_td_t *std; 2329 uhci_soft_qh_t *xqh; 2330 uhci_soft_qh_t *sxqh; 2331 int maxqh = 0; 2332 uhci_physaddr_t link; 2333 DPRINTF(("uhci_enter_ctl_q: follow from [0]\n")); 2334 for (std = sc->sc_vframes[0].htd, link = 0; 2335 (link & UHCI_PTR_QH) == 0; 2336 std = std->link.std) { 2337 link = le32toh(std->td.td_link); 2338 uhci_dump_td(std); 2339 } 2340 sxqh = (uhci_soft_qh_t *)std; 2341 uhci_dump_qh(sxqh); 2342 for (xqh = sxqh; 2343 xqh != NULL; 2344 xqh = (maxqh++ == 5 || xqh->hlink == sxqh || 2345 xqh->hlink == xqh ? NULL : xqh->hlink)) { 2346 uhci_dump_qh(xqh); 2347 } 2348 DPRINTF(("Enqueued QH:\n")); 2349 uhci_dump_qh(sqh); 2350 uhci_dump_tds(sqh->elink); 2351 } 2352 #endif 2353 if (xfer->timeout && !sc->sc_bus.use_polling) { 2354 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout), 2355 uhci_timeout, ii); 2356 } 2357 xfer->status = USBD_IN_PROGRESS; 2358 splx(s); 2359 2360 return (USBD_NORMAL_COMPLETION); 2361 } 2362 2363 usbd_status 2364 uhci_device_isoc_transfer(usbd_xfer_handle xfer) 2365 { 2366 usbd_status err; 2367 2368 DPRINTFN(5,("uhci_device_isoc_transfer: xfer=%p\n", xfer)); 2369 2370 /* Put it on our queue, */ 2371 err = usb_insert_transfer(xfer); 2372 2373 /* bail out on error, */ 2374 if (err && err != USBD_IN_PROGRESS) 2375 return (err); 2376 2377 /* XXX should check inuse here */ 2378 2379 /* insert into schedule, */ 2380 uhci_device_isoc_enter(xfer); 2381 2382 /* and start if the pipe wasn't running */ 2383 if (!err) 2384 uhci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue)); 2385 2386 return (err); 2387 } 2388 2389 void 2390 uhci_device_isoc_enter(usbd_xfer_handle xfer) 2391 { 2392 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2393 usbd_device_handle dev = upipe->pipe.device; 2394 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2395 struct iso *iso = &upipe->u.iso; 2396 uhci_soft_td_t *std; 2397 u_int32_t buf, len, status; 2398 int s, i, next, nframes; 2399 2400 DPRINTFN(5,("uhci_device_isoc_enter: used=%d next=%d xfer=%p " 2401 "nframes=%d\n", 2402 iso->inuse, iso->next, xfer, xfer->nframes)); 2403 2404 if (sc->sc_dying) 2405 return; 2406 2407 if (xfer->status == USBD_IN_PROGRESS) { 2408 /* This request has already been entered into the frame list */ 2409 printf("uhci_device_isoc_enter: xfer=%p in frame list\n", xfer); 2410 /* XXX */ 2411 } 2412 2413 #ifdef DIAGNOSTIC 2414 if (iso->inuse >= UHCI_VFRAMELIST_COUNT) 2415 printf("uhci_device_isoc_enter: overflow!\n"); 2416 #endif 2417 2418 next = iso->next; 2419 if (next == -1) { 2420 /* Not in use yet, schedule it a few frames ahead. */ 2421 next = (UREAD2(sc, UHCI_FRNUM) + 3) % UHCI_VFRAMELIST_COUNT; 2422 DPRINTFN(2,("uhci_device_isoc_enter: start next=%d\n", next)); 2423 } 2424 2425 xfer->status = USBD_IN_PROGRESS; 2426 UXFER(xfer)->curframe = next; 2427 2428 buf = DMAADDR(&xfer->dmabuf, 0); 2429 status = UHCI_TD_ZERO_ACTLEN(UHCI_TD_SET_ERRCNT(0) | 2430 UHCI_TD_ACTIVE | 2431 UHCI_TD_IOS); 2432 nframes = xfer->nframes; 2433 s = splusb(); 2434 for (i = 0; i < nframes; i++) { 2435 std = iso->stds[next]; 2436 if (++next >= UHCI_VFRAMELIST_COUNT) 2437 next = 0; 2438 len = xfer->frlengths[i]; 2439 std->td.td_buffer = htole32(buf); 2440 if (i == nframes - 1) 2441 status |= UHCI_TD_IOC; 2442 std->td.td_status = htole32(status); 2443 std->td.td_token &= htole32(~UHCI_TD_MAXLEN_MASK); 2444 std->td.td_token |= htole32(UHCI_TD_SET_MAXLEN(len)); 2445 #ifdef UHCI_DEBUG 2446 if (uhcidebug > 5) { 2447 DPRINTFN(5,("uhci_device_isoc_enter: TD %d\n", i)); 2448 uhci_dump_td(std); 2449 } 2450 #endif 2451 buf += len; 2452 } 2453 iso->next = next; 2454 iso->inuse += xfer->nframes; 2455 2456 splx(s); 2457 } 2458 2459 usbd_status 2460 uhci_device_isoc_start(usbd_xfer_handle xfer) 2461 { 2462 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2463 uhci_softc_t *sc = (uhci_softc_t *)upipe->pipe.device->bus; 2464 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2465 uhci_soft_td_t *end; 2466 int s, i; 2467 2468 DPRINTFN(5,("uhci_device_isoc_start: xfer=%p\n", xfer)); 2469 2470 if (sc->sc_dying) 2471 return (USBD_IOERROR); 2472 2473 #ifdef DIAGNOSTIC 2474 if (xfer->status != USBD_IN_PROGRESS) 2475 printf("uhci_device_isoc_start: not in progress %p\n", xfer); 2476 #endif 2477 2478 /* Find the last TD */ 2479 i = UXFER(xfer)->curframe + xfer->nframes; 2480 if (i >= UHCI_VFRAMELIST_COUNT) 2481 i -= UHCI_VFRAMELIST_COUNT; 2482 end = upipe->u.iso.stds[i]; 2483 2484 #ifdef DIAGNOSTIC 2485 if (end == NULL) { 2486 printf("uhci_device_isoc_start: end == NULL\n"); 2487 return (USBD_INVAL); 2488 } 2489 #endif 2490 2491 s = splusb(); 2492 2493 /* Set up interrupt info. */ 2494 ii->xfer = xfer; 2495 ii->stdstart = end; 2496 ii->stdend = end; 2497 #ifdef DIAGNOSTIC 2498 if (!ii->isdone) 2499 printf("uhci_device_isoc_start: not done, ii=%p\n", ii); 2500 ii->isdone = 0; 2501 #endif 2502 uhci_add_intr_info(sc, ii); 2503 2504 splx(s); 2505 2506 return (USBD_IN_PROGRESS); 2507 } 2508 2509 void 2510 uhci_device_isoc_abort(usbd_xfer_handle xfer) 2511 { 2512 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2513 uhci_soft_td_t **stds = upipe->u.iso.stds; 2514 uhci_soft_td_t *std; 2515 int i, n, s, nframes, maxlen, len; 2516 2517 s = splusb(); 2518 2519 /* Transfer is already done. */ 2520 if (xfer->status != USBD_NOT_STARTED && 2521 xfer->status != USBD_IN_PROGRESS) { 2522 splx(s); 2523 return; 2524 } 2525 2526 /* Give xfer the requested abort code. */ 2527 xfer->status = USBD_CANCELLED; 2528 2529 /* make hardware ignore it, */ 2530 nframes = xfer->nframes; 2531 n = UXFER(xfer)->curframe; 2532 maxlen = 0; 2533 for (i = 0; i < nframes; i++) { 2534 std = stds[n]; 2535 std->td.td_status &= htole32(~(UHCI_TD_ACTIVE | UHCI_TD_IOC)); 2536 len = UHCI_TD_GET_MAXLEN(le32toh(std->td.td_token)); 2537 if (len > maxlen) 2538 maxlen = len; 2539 if (++n >= UHCI_VFRAMELIST_COUNT) 2540 n = 0; 2541 } 2542 2543 /* and wait until we are sure the hardware has finished. */ 2544 delay(maxlen); 2545 2546 #ifdef DIAGNOSTIC 2547 UXFER(xfer)->iinfo.isdone = 1; 2548 #endif 2549 /* Run callback and remove from interrupt list. */ 2550 usb_transfer_complete(xfer); 2551 2552 splx(s); 2553 } 2554 2555 void 2556 uhci_device_isoc_close(usbd_pipe_handle pipe) 2557 { 2558 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2559 usbd_device_handle dev = upipe->pipe.device; 2560 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2561 uhci_soft_td_t *std, *vstd; 2562 struct iso *iso; 2563 int i, s; 2564 2565 /* 2566 * Make sure all TDs are marked as inactive. 2567 * Wait for completion. 2568 * Unschedule. 2569 * Deallocate. 2570 */ 2571 iso = &upipe->u.iso; 2572 2573 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) 2574 iso->stds[i]->td.td_status &= htole32(~UHCI_TD_ACTIVE); 2575 usb_delay_ms(&sc->sc_bus, 2); /* wait for completion */ 2576 2577 s = splusb(); 2578 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2579 std = iso->stds[i]; 2580 for (vstd = sc->sc_vframes[i].htd; 2581 vstd != NULL && vstd->link.std != std; 2582 vstd = vstd->link.std) 2583 ; 2584 if (vstd == NULL) { 2585 /*panic*/ 2586 printf("uhci_device_isoc_close: %p not found\n", std); 2587 splx(s); 2588 return; 2589 } 2590 vstd->link = std->link; 2591 vstd->td.td_link = std->td.td_link; 2592 uhci_free_std(sc, std); 2593 } 2594 splx(s); 2595 2596 free(iso->stds, M_USBHC); 2597 } 2598 2599 usbd_status 2600 uhci_setup_isoc(usbd_pipe_handle pipe) 2601 { 2602 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2603 usbd_device_handle dev = upipe->pipe.device; 2604 uhci_softc_t *sc = (uhci_softc_t *)dev->bus; 2605 int addr = upipe->pipe.device->address; 2606 int endpt = upipe->pipe.endpoint->edesc->bEndpointAddress; 2607 int rd = UE_GET_DIR(endpt) == UE_DIR_IN; 2608 uhci_soft_td_t *std, *vstd; 2609 u_int32_t token; 2610 struct iso *iso; 2611 int i, s; 2612 2613 iso = &upipe->u.iso; 2614 iso->stds = malloc(UHCI_VFRAMELIST_COUNT * sizeof (uhci_soft_td_t *), 2615 M_USBHC, M_WAITOK); 2616 2617 token = rd ? UHCI_TD_IN (0, endpt, addr, 0) : 2618 UHCI_TD_OUT(0, endpt, addr, 0); 2619 2620 /* Allocate the TDs and mark as inactive; */ 2621 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2622 std = uhci_alloc_std(sc); 2623 if (std == 0) 2624 goto bad; 2625 std->td.td_status = htole32(UHCI_TD_IOS); /* iso, inactive */ 2626 std->td.td_token = htole32(token); 2627 iso->stds[i] = std; 2628 } 2629 2630 /* Insert TDs into schedule. */ 2631 s = splusb(); 2632 for (i = 0; i < UHCI_VFRAMELIST_COUNT; i++) { 2633 std = iso->stds[i]; 2634 vstd = sc->sc_vframes[i].htd; 2635 std->link = vstd->link; 2636 std->td.td_link = vstd->td.td_link; 2637 vstd->link.std = std; 2638 vstd->td.td_link = htole32(std->physaddr | UHCI_PTR_TD); 2639 } 2640 splx(s); 2641 2642 iso->next = -1; 2643 iso->inuse = 0; 2644 2645 return (USBD_NORMAL_COMPLETION); 2646 2647 bad: 2648 while (--i >= 0) 2649 uhci_free_std(sc, iso->stds[i]); 2650 free(iso->stds, M_USBHC); 2651 return (USBD_NOMEM); 2652 } 2653 2654 void 2655 uhci_device_isoc_done(usbd_xfer_handle xfer) 2656 { 2657 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2658 2659 DPRINTFN(4, ("uhci_isoc_done: length=%d, busy_free=0x%08x\n", 2660 xfer->actlen, xfer->busy_free)); 2661 2662 if (ii->xfer != xfer) 2663 /* Not on interrupt list, ignore it. */ 2664 return; 2665 2666 if (!uhci_active_intr_info(ii)) 2667 return; 2668 2669 #ifdef DIAGNOSTIC 2670 if (ii->stdend == NULL) { 2671 printf("uhci_device_isoc_done: xfer=%p stdend==NULL\n", xfer); 2672 #ifdef UHCI_DEBUG 2673 uhci_dump_ii(ii); 2674 #endif 2675 return; 2676 } 2677 #endif 2678 2679 /* Turn off the interrupt since it is active even if the TD is not. */ 2680 ii->stdend->td.td_status &= htole32(~UHCI_TD_IOC); 2681 2682 uhci_del_intr_info(ii); /* remove from active list */ 2683 } 2684 2685 void 2686 uhci_device_intr_done(usbd_xfer_handle xfer) 2687 { 2688 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2689 uhci_softc_t *sc = ii->sc; 2690 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2691 uhci_soft_qh_t *sqh; 2692 int i, npoll; 2693 2694 DPRINTFN(5, ("uhci_device_intr_done: length=%d\n", xfer->actlen)); 2695 2696 npoll = upipe->u.intr.npoll; 2697 for(i = 0; i < npoll; i++) { 2698 sqh = upipe->u.intr.qhs[i]; 2699 sqh->elink = NULL; 2700 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 2701 } 2702 uhci_free_std_chain(sc, ii->stdstart, NULL); 2703 2704 /* XXX Wasteful. */ 2705 if (xfer->pipe->repeat) { 2706 uhci_soft_td_t *data, *dataend; 2707 2708 DPRINTFN(5,("uhci_device_intr_done: requeing\n")); 2709 2710 /* This alloc cannot fail since we freed the chain above. */ 2711 uhci_alloc_std_chain(upipe, sc, xfer->length, 1, xfer->flags, 2712 &xfer->dmabuf, &data, &dataend); 2713 dataend->td.td_status |= htole32(UHCI_TD_IOC); 2714 2715 #ifdef UHCI_DEBUG 2716 if (uhcidebug > 10) { 2717 DPRINTF(("uhci_device_intr_done: data(1)\n")); 2718 uhci_dump_tds(data); 2719 uhci_dump_qh(upipe->u.intr.qhs[0]); 2720 } 2721 #endif 2722 2723 ii->stdstart = data; 2724 ii->stdend = dataend; 2725 #ifdef DIAGNOSTIC 2726 if (!ii->isdone) { 2727 printf("uhci_device_intr_done: not done, ii=%p\n", ii); 2728 } 2729 ii->isdone = 0; 2730 #endif 2731 for (i = 0; i < npoll; i++) { 2732 sqh = upipe->u.intr.qhs[i]; 2733 sqh->elink = data; 2734 sqh->qh.qh_elink = htole32(data->physaddr | UHCI_PTR_TD); 2735 } 2736 xfer->status = USBD_IN_PROGRESS; 2737 /* The ii is already on the examined list, just leave it. */ 2738 } else { 2739 DPRINTFN(5,("uhci_device_intr_done: removing\n")); 2740 if (uhci_active_intr_info(ii)) 2741 uhci_del_intr_info(ii); 2742 } 2743 } 2744 2745 /* Deallocate request data structures */ 2746 void 2747 uhci_device_ctrl_done(usbd_xfer_handle xfer) 2748 { 2749 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2750 uhci_softc_t *sc = ii->sc; 2751 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2752 2753 #ifdef DIAGNOSTIC 2754 if (!(xfer->rqflags & URQ_REQUEST)) 2755 panic("uhci_device_ctrl_done: not a request"); 2756 #endif 2757 2758 if (!uhci_active_intr_info(ii)) 2759 return; 2760 2761 uhci_del_intr_info(ii); /* remove from active list */ 2762 2763 if (upipe->pipe.device->speed == USB_SPEED_LOW) 2764 uhci_remove_ls_ctrl(sc, upipe->u.ctl.sqh); 2765 else 2766 uhci_remove_hs_ctrl(sc, upipe->u.ctl.sqh); 2767 2768 if (upipe->u.ctl.length != 0) 2769 uhci_free_std_chain(sc, ii->stdstart->link.std, ii->stdend); 2770 2771 DPRINTFN(5, ("uhci_device_ctrl_done: length=%d\n", xfer->actlen)); 2772 } 2773 2774 /* Deallocate request data structures */ 2775 void 2776 uhci_device_bulk_done(usbd_xfer_handle xfer) 2777 { 2778 uhci_intr_info_t *ii = &UXFER(xfer)->iinfo; 2779 uhci_softc_t *sc = ii->sc; 2780 struct uhci_pipe *upipe = (struct uhci_pipe *)xfer->pipe; 2781 2782 DPRINTFN(5,("uhci_device_bulk_done: xfer=%p ii=%p sc=%p upipe=%p\n", 2783 xfer, ii, sc, upipe)); 2784 2785 if (!uhci_active_intr_info(ii)) 2786 return; 2787 2788 uhci_del_intr_info(ii); /* remove from active list */ 2789 2790 uhci_remove_bulk(sc, upipe->u.bulk.sqh); 2791 2792 uhci_free_std_chain(sc, ii->stdstart, NULL); 2793 2794 DPRINTFN(5, ("uhci_device_bulk_done: length=%d\n", xfer->actlen)); 2795 } 2796 2797 /* Add interrupt QH, called with vflock. */ 2798 void 2799 uhci_add_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 2800 { 2801 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos]; 2802 uhci_soft_qh_t *eqh; 2803 2804 DPRINTFN(4, ("uhci_add_intr: n=%d sqh=%p\n", sqh->pos, sqh)); 2805 2806 eqh = vf->eqh; 2807 sqh->hlink = eqh->hlink; 2808 sqh->qh.qh_hlink = eqh->qh.qh_hlink; 2809 eqh->hlink = sqh; 2810 eqh->qh.qh_hlink = htole32(sqh->physaddr | UHCI_PTR_QH); 2811 vf->eqh = sqh; 2812 vf->bandwidth++; 2813 } 2814 2815 /* Remove interrupt QH. */ 2816 void 2817 uhci_remove_intr(uhci_softc_t *sc, uhci_soft_qh_t *sqh) 2818 { 2819 struct uhci_vframe *vf = &sc->sc_vframes[sqh->pos]; 2820 uhci_soft_qh_t *pqh; 2821 2822 DPRINTFN(4, ("uhci_remove_intr: n=%d sqh=%p\n", sqh->pos, sqh)); 2823 2824 /* See comment in uhci_remove_ctrl() */ 2825 if (!(sqh->qh.qh_elink & htole32(UHCI_PTR_T))) { 2826 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 2827 delay(UHCI_QH_REMOVE_DELAY); 2828 } 2829 2830 pqh = uhci_find_prev_qh(vf->hqh, sqh); 2831 pqh->hlink = sqh->hlink; 2832 pqh->qh.qh_hlink = sqh->qh.qh_hlink; 2833 delay(UHCI_QH_REMOVE_DELAY); 2834 if (vf->eqh == sqh) 2835 vf->eqh = pqh; 2836 vf->bandwidth--; 2837 } 2838 2839 usbd_status 2840 uhci_device_setintr(uhci_softc_t *sc, struct uhci_pipe *upipe, int ival) 2841 { 2842 uhci_soft_qh_t *sqh; 2843 int i, npoll, s; 2844 u_int bestbw, bw, bestoffs, offs; 2845 2846 DPRINTFN(2, ("uhci_device_setintr: pipe=%p\n", upipe)); 2847 if (ival == 0) { 2848 printf("uhci_device_setintr: 0 interval\n"); 2849 return (USBD_INVAL); 2850 } 2851 2852 if (ival > UHCI_VFRAMELIST_COUNT) 2853 ival = UHCI_VFRAMELIST_COUNT; 2854 npoll = (UHCI_VFRAMELIST_COUNT + ival - 1) / ival; 2855 DPRINTFN(2, ("uhci_device_setintr: ival=%d npoll=%d\n", ival, npoll)); 2856 2857 upipe->u.intr.npoll = npoll; 2858 upipe->u.intr.qhs = 2859 malloc(npoll * sizeof(uhci_soft_qh_t *), M_USBHC, M_WAITOK); 2860 2861 /* 2862 * Figure out which offset in the schedule that has most 2863 * bandwidth left over. 2864 */ 2865 #define MOD(i) ((i) & (UHCI_VFRAMELIST_COUNT-1)) 2866 for (bestoffs = offs = 0, bestbw = ~0; offs < ival; offs++) { 2867 for (bw = i = 0; i < npoll; i++) 2868 bw += sc->sc_vframes[MOD(i * ival + offs)].bandwidth; 2869 if (bw < bestbw) { 2870 bestbw = bw; 2871 bestoffs = offs; 2872 } 2873 } 2874 DPRINTFN(1, ("uhci_device_setintr: bw=%d offs=%d\n", bestbw, bestoffs)); 2875 2876 for(i = 0; i < npoll; i++) { 2877 upipe->u.intr.qhs[i] = sqh = uhci_alloc_sqh(sc); 2878 sqh->elink = NULL; 2879 sqh->qh.qh_elink = htole32(UHCI_PTR_T); 2880 sqh->pos = MOD(i * ival + bestoffs); 2881 } 2882 #undef MOD 2883 2884 s = splusb(); 2885 /* Enter QHs into the controller data structures. */ 2886 for(i = 0; i < npoll; i++) 2887 uhci_add_intr(sc, upipe->u.intr.qhs[i]); 2888 splx(s); 2889 2890 DPRINTFN(5, ("uhci_device_setintr: returns %p\n", upipe)); 2891 return (USBD_NORMAL_COMPLETION); 2892 } 2893 2894 /* Open a new pipe. */ 2895 usbd_status 2896 uhci_open(usbd_pipe_handle pipe) 2897 { 2898 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; 2899 struct uhci_pipe *upipe = (struct uhci_pipe *)pipe; 2900 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; 2901 usbd_status err; 2902 int ival; 2903 2904 DPRINTFN(1, ("uhci_open: pipe=%p, addr=%d, endpt=%d (%d)\n", 2905 pipe, pipe->device->address, 2906 ed->bEndpointAddress, sc->sc_addr)); 2907 2908 upipe->aborting = 0; 2909 upipe->nexttoggle = 0; 2910 2911 if (pipe->device->address == sc->sc_addr) { 2912 switch (ed->bEndpointAddress) { 2913 case USB_CONTROL_ENDPOINT: 2914 pipe->methods = &uhci_root_ctrl_methods; 2915 break; 2916 case UE_DIR_IN | UHCI_INTR_ENDPT: 2917 pipe->methods = &uhci_root_intr_methods; 2918 break; 2919 default: 2920 return (USBD_INVAL); 2921 } 2922 } else { 2923 switch (ed->bmAttributes & UE_XFERTYPE) { 2924 case UE_CONTROL: 2925 pipe->methods = &uhci_device_ctrl_methods; 2926 upipe->u.ctl.sqh = uhci_alloc_sqh(sc); 2927 if (upipe->u.ctl.sqh == NULL) 2928 goto bad; 2929 upipe->u.ctl.setup = uhci_alloc_std(sc); 2930 if (upipe->u.ctl.setup == NULL) { 2931 uhci_free_sqh(sc, upipe->u.ctl.sqh); 2932 goto bad; 2933 } 2934 upipe->u.ctl.stat = uhci_alloc_std(sc); 2935 if (upipe->u.ctl.stat == NULL) { 2936 uhci_free_sqh(sc, upipe->u.ctl.sqh); 2937 uhci_free_std(sc, upipe->u.ctl.setup); 2938 goto bad; 2939 } 2940 err = usb_allocmem(&sc->sc_bus, 2941 sizeof(usb_device_request_t), 2942 0, &upipe->u.ctl.reqdma); 2943 if (err) { 2944 uhci_free_sqh(sc, upipe->u.ctl.sqh); 2945 uhci_free_std(sc, upipe->u.ctl.setup); 2946 uhci_free_std(sc, upipe->u.ctl.stat); 2947 goto bad; 2948 } 2949 break; 2950 case UE_INTERRUPT: 2951 pipe->methods = &uhci_device_intr_methods; 2952 ival = pipe->interval; 2953 if (ival == USBD_DEFAULT_INTERVAL) 2954 ival = ed->bInterval; 2955 return (uhci_device_setintr(sc, upipe, ival)); 2956 case UE_ISOCHRONOUS: 2957 pipe->methods = &uhci_device_isoc_methods; 2958 return (uhci_setup_isoc(pipe)); 2959 case UE_BULK: 2960 pipe->methods = &uhci_device_bulk_methods; 2961 upipe->u.bulk.sqh = uhci_alloc_sqh(sc); 2962 if (upipe->u.bulk.sqh == NULL) 2963 goto bad; 2964 break; 2965 } 2966 } 2967 return (USBD_NORMAL_COMPLETION); 2968 2969 bad: 2970 return (USBD_NOMEM); 2971 } 2972 2973 /* 2974 * Data structures and routines to emulate the root hub. 2975 */ 2976 usb_device_descriptor_t uhci_devd = { 2977 USB_DEVICE_DESCRIPTOR_SIZE, 2978 UDESC_DEVICE, /* type */ 2979 {0x00, 0x01}, /* USB version */ 2980 UDCLASS_HUB, /* class */ 2981 UDSUBCLASS_HUB, /* subclass */ 2982 UDPROTO_FSHUB, /* protocol */ 2983 64, /* max packet */ 2984 {0},{0},{0x00,0x01}, /* device id */ 2985 1,2,0, /* string indicies */ 2986 1 /* # of configurations */ 2987 }; 2988 2989 const usb_config_descriptor_t uhci_confd = { 2990 USB_CONFIG_DESCRIPTOR_SIZE, 2991 UDESC_CONFIG, 2992 {USB_CONFIG_DESCRIPTOR_SIZE + 2993 USB_INTERFACE_DESCRIPTOR_SIZE + 2994 USB_ENDPOINT_DESCRIPTOR_SIZE}, 2995 1, 2996 1, 2997 0, 2998 UC_ATTR_MBO | UC_SELF_POWERED, 2999 0 /* max power */ 3000 }; 3001 3002 const usb_interface_descriptor_t uhci_ifcd = { 3003 USB_INTERFACE_DESCRIPTOR_SIZE, 3004 UDESC_INTERFACE, 3005 0, 3006 0, 3007 1, 3008 UICLASS_HUB, 3009 UISUBCLASS_HUB, 3010 UIPROTO_FSHUB, 3011 0 3012 }; 3013 3014 const usb_endpoint_descriptor_t uhci_endpd = { 3015 USB_ENDPOINT_DESCRIPTOR_SIZE, 3016 UDESC_ENDPOINT, 3017 UE_DIR_IN | UHCI_INTR_ENDPT, 3018 UE_INTERRUPT, 3019 {8}, 3020 255 3021 }; 3022 3023 const usb_hub_descriptor_t uhci_hubd_piix = { 3024 USB_HUB_DESCRIPTOR_SIZE, 3025 UDESC_HUB, 3026 2, 3027 { UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL, 0 }, 3028 50, /* power on to power good */ 3029 0, 3030 { 0x00 }, /* both ports are removable */ 3031 { 0 }, 3032 }; 3033 3034 /* 3035 * The USB hub protocol requires that SET_FEATURE(PORT_RESET) also 3036 * enables the port, and also states that SET_FEATURE(PORT_ENABLE) 3037 * should not be used by the USB subsystem. As we cannot issue a 3038 * SET_FEATURE(PORT_ENABLE) externally, we must ensure that the port 3039 * will be enabled as part of the reset. 3040 * 3041 * On the VT83C572, the port cannot be successfully enabled until the 3042 * outstanding "port enable change" and "connection status change" 3043 * events have been reset. 3044 */ 3045 Static usbd_status 3046 uhci_portreset(uhci_softc_t *sc, int index) 3047 { 3048 int lim, port, x; 3049 3050 if (index == 1) 3051 port = UHCI_PORTSC1; 3052 else if (index == 2) 3053 port = UHCI_PORTSC2; 3054 else 3055 return (USBD_IOERROR); 3056 3057 x = URWMASK(UREAD2(sc, port)); 3058 UWRITE2(sc, port, x | UHCI_PORTSC_PR); 3059 3060 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY); 3061 3062 DPRINTFN(3,("uhci port %d reset, status0 = 0x%04x\n", 3063 index, UREAD2(sc, port))); 3064 3065 x = URWMASK(UREAD2(sc, port)); 3066 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR); 3067 3068 delay(100); 3069 3070 DPRINTFN(3,("uhci port %d reset, status1 = 0x%04x\n", 3071 index, UREAD2(sc, port))); 3072 3073 x = URWMASK(UREAD2(sc, port)); 3074 UWRITE2(sc, port, x | UHCI_PORTSC_PE); 3075 3076 for (lim = 10; --lim > 0;) { 3077 usb_delay_ms(&sc->sc_bus, USB_PORT_RESET_DELAY); 3078 3079 x = UREAD2(sc, port); 3080 3081 DPRINTFN(3,("uhci port %d iteration %u, status = 0x%04x\n", 3082 index, lim, x)); 3083 3084 if (!(x & UHCI_PORTSC_CCS)) { 3085 /* 3086 * No device is connected (or was disconnected 3087 * during reset). Consider the port reset. 3088 * The delay must be long enough to ensure on 3089 * the initial iteration that the device 3090 * connection will have been registered. 50ms 3091 * appears to be sufficient, but 20ms is not. 3092 */ 3093 DPRINTFN(3,("uhci port %d loop %u, device detached\n", 3094 index, lim)); 3095 break; 3096 } 3097 3098 if (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC)) { 3099 /* 3100 * Port enabled changed and/or connection 3101 * status changed were set. Reset either or 3102 * both raised flags (by writing a 1 to that 3103 * bit), and wait again for state to settle. 3104 */ 3105 UWRITE2(sc, port, URWMASK(x) | 3106 (x & (UHCI_PORTSC_POEDC | UHCI_PORTSC_CSC))); 3107 continue; 3108 } 3109 3110 if (x & UHCI_PORTSC_PE) 3111 /* Port is enabled */ 3112 break; 3113 3114 UWRITE2(sc, port, URWMASK(x) | UHCI_PORTSC_PE); 3115 } 3116 3117 DPRINTFN(3,("uhci port %d reset, status2 = 0x%04x\n", 3118 index, UREAD2(sc, port))); 3119 3120 if (lim <= 0) { 3121 DPRINTFN(1,("uhci port %d reset timed out\n", index)); 3122 return (USBD_TIMEOUT); 3123 } 3124 3125 sc->sc_isreset = 1; 3126 return (USBD_NORMAL_COMPLETION); 3127 } 3128 3129 /* 3130 * Simulate a hardware hub by handling all the necessary requests. 3131 */ 3132 usbd_status 3133 uhci_root_ctrl_transfer(usbd_xfer_handle xfer) 3134 { 3135 usbd_status err; 3136 3137 /* Insert last in queue. */ 3138 err = usb_insert_transfer(xfer); 3139 if (err) 3140 return (err); 3141 3142 /* 3143 * Pipe isn't running (otherwise err would be USBD_INPROG), 3144 * so start it first. 3145 */ 3146 return (uhci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3147 } 3148 3149 usbd_status 3150 uhci_root_ctrl_start(usbd_xfer_handle xfer) 3151 { 3152 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus; 3153 usb_device_request_t *req; 3154 void *buf = NULL; 3155 int port, x; 3156 int s, len, value, index, status, change, l, totlen = 0; 3157 usb_port_status_t ps; 3158 usbd_status err; 3159 3160 if (sc->sc_dying) 3161 return (USBD_IOERROR); 3162 3163 #ifdef DIAGNOSTIC 3164 if (!(xfer->rqflags & URQ_REQUEST)) 3165 panic("uhci_root_ctrl_transfer: not a request"); 3166 #endif 3167 req = &xfer->request; 3168 3169 DPRINTFN(2,("uhci_root_ctrl_control type=0x%02x request=%02x\n", 3170 req->bmRequestType, req->bRequest)); 3171 3172 len = UGETW(req->wLength); 3173 value = UGETW(req->wValue); 3174 index = UGETW(req->wIndex); 3175 3176 if (len != 0) 3177 buf = KERNADDR(&xfer->dmabuf, 0); 3178 3179 #define C(x,y) ((x) | ((y) << 8)) 3180 switch(C(req->bRequest, req->bmRequestType)) { 3181 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 3182 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 3183 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 3184 /* 3185 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 3186 * for the integrated root hub. 3187 */ 3188 break; 3189 case C(UR_GET_CONFIG, UT_READ_DEVICE): 3190 if (len > 0) { 3191 *(u_int8_t *)buf = sc->sc_conf; 3192 totlen = 1; 3193 } 3194 break; 3195 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 3196 DPRINTFN(2,("uhci_root_ctrl_control wValue=0x%04x\n", value)); 3197 if (len == 0) 3198 break; 3199 switch(value >> 8) { 3200 case UDESC_DEVICE: 3201 if ((value & 0xff) != 0) { 3202 err = USBD_IOERROR; 3203 goto ret; 3204 } 3205 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 3206 USETW(uhci_devd.idVendor, sc->sc_id_vendor); 3207 memcpy(buf, &uhci_devd, l); 3208 break; 3209 case UDESC_CONFIG: 3210 if ((value & 0xff) != 0) { 3211 err = USBD_IOERROR; 3212 goto ret; 3213 } 3214 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); 3215 memcpy(buf, &uhci_confd, l); 3216 buf = (char *)buf + l; 3217 len -= l; 3218 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); 3219 totlen += l; 3220 memcpy(buf, &uhci_ifcd, l); 3221 buf = (char *)buf + l; 3222 len -= l; 3223 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); 3224 totlen += l; 3225 memcpy(buf, &uhci_endpd, l); 3226 break; 3227 case UDESC_STRING: 3228 #define sd ((usb_string_descriptor_t *)buf) 3229 switch (value & 0xff) { 3230 case 0: /* Language table */ 3231 totlen = usb_makelangtbl(sd, len); 3232 break; 3233 case 1: /* Vendor */ 3234 totlen = usb_makestrdesc(sd, len, 3235 sc->sc_vendor); 3236 break; 3237 case 2: /* Product */ 3238 totlen = usb_makestrdesc(sd, len, 3239 "UHCI root hub"); 3240 break; 3241 } 3242 #undef sd 3243 break; 3244 default: 3245 err = USBD_IOERROR; 3246 goto ret; 3247 } 3248 break; 3249 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 3250 if (len > 0) { 3251 *(u_int8_t *)buf = 0; 3252 totlen = 1; 3253 } 3254 break; 3255 case C(UR_GET_STATUS, UT_READ_DEVICE): 3256 if (len > 1) { 3257 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED); 3258 totlen = 2; 3259 } 3260 break; 3261 case C(UR_GET_STATUS, UT_READ_INTERFACE): 3262 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 3263 if (len > 1) { 3264 USETW(((usb_status_t *)buf)->wStatus, 0); 3265 totlen = 2; 3266 } 3267 break; 3268 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 3269 if (value >= USB_MAX_DEVICES) { 3270 err = USBD_IOERROR; 3271 goto ret; 3272 } 3273 sc->sc_addr = value; 3274 break; 3275 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 3276 if (value != 0 && value != 1) { 3277 err = USBD_IOERROR; 3278 goto ret; 3279 } 3280 sc->sc_conf = value; 3281 break; 3282 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 3283 break; 3284 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 3285 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 3286 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 3287 err = USBD_IOERROR; 3288 goto ret; 3289 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 3290 break; 3291 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 3292 break; 3293 /* Hub requests */ 3294 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 3295 break; 3296 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 3297 DPRINTFN(3, ("uhci_root_ctrl_control: UR_CLEAR_PORT_FEATURE " 3298 "port=%d feature=%d\n", 3299 index, value)); 3300 if (index == 1) 3301 port = UHCI_PORTSC1; 3302 else if (index == 2) 3303 port = UHCI_PORTSC2; 3304 else { 3305 err = USBD_IOERROR; 3306 goto ret; 3307 } 3308 switch(value) { 3309 case UHF_PORT_ENABLE: 3310 x = URWMASK(UREAD2(sc, port)); 3311 UWRITE2(sc, port, x & ~UHCI_PORTSC_PE); 3312 break; 3313 case UHF_PORT_SUSPEND: 3314 x = URWMASK(UREAD2(sc, port)); 3315 UWRITE2(sc, port, x & ~UHCI_PORTSC_SUSP); 3316 break; 3317 case UHF_PORT_RESET: 3318 x = URWMASK(UREAD2(sc, port)); 3319 UWRITE2(sc, port, x & ~UHCI_PORTSC_PR); 3320 break; 3321 case UHF_C_PORT_CONNECTION: 3322 x = URWMASK(UREAD2(sc, port)); 3323 UWRITE2(sc, port, x | UHCI_PORTSC_CSC); 3324 break; 3325 case UHF_C_PORT_ENABLE: 3326 x = URWMASK(UREAD2(sc, port)); 3327 UWRITE2(sc, port, x | UHCI_PORTSC_POEDC); 3328 break; 3329 case UHF_C_PORT_OVER_CURRENT: 3330 x = URWMASK(UREAD2(sc, port)); 3331 UWRITE2(sc, port, x | UHCI_PORTSC_OCIC); 3332 break; 3333 case UHF_C_PORT_RESET: 3334 sc->sc_isreset = 0; 3335 err = USBD_NORMAL_COMPLETION; 3336 goto ret; 3337 case UHF_PORT_CONNECTION: 3338 case UHF_PORT_OVER_CURRENT: 3339 case UHF_PORT_POWER: 3340 case UHF_PORT_LOW_SPEED: 3341 case UHF_C_PORT_SUSPEND: 3342 default: 3343 err = USBD_IOERROR; 3344 goto ret; 3345 } 3346 break; 3347 case C(UR_GET_BUS_STATE, UT_READ_CLASS_OTHER): 3348 if (index == 1) 3349 port = UHCI_PORTSC1; 3350 else if (index == 2) 3351 port = UHCI_PORTSC2; 3352 else { 3353 err = USBD_IOERROR; 3354 goto ret; 3355 } 3356 if (len > 0) { 3357 *(u_int8_t *)buf = 3358 (UREAD2(sc, port) & UHCI_PORTSC_LS) >> 3359 UHCI_PORTSC_LS_SHIFT; 3360 totlen = 1; 3361 } 3362 break; 3363 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 3364 if (len == 0) 3365 break; 3366 if ((value & 0xff) != 0) { 3367 err = USBD_IOERROR; 3368 goto ret; 3369 } 3370 l = min(len, USB_HUB_DESCRIPTOR_SIZE); 3371 totlen = l; 3372 memcpy(buf, &uhci_hubd_piix, l); 3373 break; 3374 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 3375 if (len != 4) { 3376 err = USBD_IOERROR; 3377 goto ret; 3378 } 3379 memset(buf, 0, len); 3380 totlen = len; 3381 break; 3382 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 3383 if (index == 1) 3384 port = UHCI_PORTSC1; 3385 else if (index == 2) 3386 port = UHCI_PORTSC2; 3387 else { 3388 err = USBD_IOERROR; 3389 goto ret; 3390 } 3391 if (len != 4) { 3392 err = USBD_IOERROR; 3393 goto ret; 3394 } 3395 x = UREAD2(sc, port); 3396 status = change = 0; 3397 if (x & UHCI_PORTSC_CCS) 3398 status |= UPS_CURRENT_CONNECT_STATUS; 3399 if (x & UHCI_PORTSC_CSC) 3400 change |= UPS_C_CONNECT_STATUS; 3401 if (x & UHCI_PORTSC_PE) 3402 status |= UPS_PORT_ENABLED; 3403 if (x & UHCI_PORTSC_POEDC) 3404 change |= UPS_C_PORT_ENABLED; 3405 if (x & UHCI_PORTSC_OCI) 3406 status |= UPS_OVERCURRENT_INDICATOR; 3407 if (x & UHCI_PORTSC_OCIC) 3408 change |= UPS_C_OVERCURRENT_INDICATOR; 3409 if (x & UHCI_PORTSC_SUSP) 3410 status |= UPS_SUSPEND; 3411 if (x & UHCI_PORTSC_LSDA) 3412 status |= UPS_LOW_SPEED; 3413 status |= UPS_PORT_POWER; 3414 if (sc->sc_isreset) 3415 change |= UPS_C_PORT_RESET; 3416 USETW(ps.wPortStatus, status); 3417 USETW(ps.wPortChange, change); 3418 l = min(len, sizeof ps); 3419 memcpy(buf, &ps, l); 3420 totlen = l; 3421 break; 3422 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 3423 err = USBD_IOERROR; 3424 goto ret; 3425 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 3426 break; 3427 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 3428 if (index == 1) 3429 port = UHCI_PORTSC1; 3430 else if (index == 2) 3431 port = UHCI_PORTSC2; 3432 else { 3433 err = USBD_IOERROR; 3434 goto ret; 3435 } 3436 switch(value) { 3437 case UHF_PORT_ENABLE: 3438 x = URWMASK(UREAD2(sc, port)); 3439 UWRITE2(sc, port, x | UHCI_PORTSC_PE); 3440 break; 3441 case UHF_PORT_SUSPEND: 3442 x = URWMASK(UREAD2(sc, port)); 3443 UWRITE2(sc, port, x | UHCI_PORTSC_SUSP); 3444 break; 3445 case UHF_PORT_RESET: 3446 err = uhci_portreset(sc, index); 3447 goto ret; 3448 case UHF_PORT_POWER: 3449 /* Pretend we turned on power */ 3450 err = USBD_NORMAL_COMPLETION; 3451 goto ret; 3452 case UHF_C_PORT_CONNECTION: 3453 case UHF_C_PORT_ENABLE: 3454 case UHF_C_PORT_OVER_CURRENT: 3455 case UHF_PORT_CONNECTION: 3456 case UHF_PORT_OVER_CURRENT: 3457 case UHF_PORT_LOW_SPEED: 3458 case UHF_C_PORT_SUSPEND: 3459 case UHF_C_PORT_RESET: 3460 default: 3461 err = USBD_IOERROR; 3462 goto ret; 3463 } 3464 break; 3465 default: 3466 err = USBD_IOERROR; 3467 goto ret; 3468 } 3469 xfer->actlen = totlen; 3470 err = USBD_NORMAL_COMPLETION; 3471 ret: 3472 xfer->status = err; 3473 s = splusb(); 3474 usb_transfer_complete(xfer); 3475 splx(s); 3476 return (USBD_IN_PROGRESS); 3477 } 3478 3479 /* Abort a root control request. */ 3480 void 3481 uhci_root_ctrl_abort(usbd_xfer_handle xfer) 3482 { 3483 /* Nothing to do, all transfers are synchronous. */ 3484 } 3485 3486 /* Close the root pipe. */ 3487 void 3488 uhci_root_ctrl_close(usbd_pipe_handle pipe) 3489 { 3490 DPRINTF(("uhci_root_ctrl_close\n")); 3491 } 3492 3493 /* Abort a root interrupt request. */ 3494 void 3495 uhci_root_intr_abort(usbd_xfer_handle xfer) 3496 { 3497 uhci_softc_t *sc = (uhci_softc_t *)xfer->pipe->device->bus; 3498 3499 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, xfer); 3500 sc->sc_intr_xfer = NULL; 3501 3502 if (xfer->pipe->intrxfer == xfer) { 3503 DPRINTF(("uhci_root_intr_abort: remove\n")); 3504 xfer->pipe->intrxfer = 0; 3505 } 3506 xfer->status = USBD_CANCELLED; 3507 #ifdef DIAGNOSTIC 3508 UXFER(xfer)->iinfo.isdone = 1; 3509 #endif 3510 usb_transfer_complete(xfer); 3511 } 3512 3513 usbd_status 3514 uhci_root_intr_transfer(usbd_xfer_handle xfer) 3515 { 3516 usbd_status err; 3517 3518 /* Insert last in queue. */ 3519 err = usb_insert_transfer(xfer); 3520 if (err) 3521 return (err); 3522 3523 /* 3524 * Pipe isn't running (otherwise err would be USBD_INPROG), 3525 * start first 3526 */ 3527 return (uhci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3528 } 3529 3530 /* Start a transfer on the root interrupt pipe */ 3531 usbd_status 3532 uhci_root_intr_start(usbd_xfer_handle xfer) 3533 { 3534 usbd_pipe_handle pipe = xfer->pipe; 3535 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; 3536 unsigned int ival; 3537 3538 DPRINTFN(3, ("uhci_root_intr_start: xfer=%p len=%d flags=%d\n", 3539 xfer, xfer->length, xfer->flags)); 3540 3541 if (sc->sc_dying) 3542 return (USBD_IOERROR); 3543 3544 /* XXX temporary variable needed to avoid gcc3 warning */ 3545 ival = xfer->pipe->endpoint->edesc->bInterval; 3546 sc->sc_ival = mstohz(ival); 3547 usb_callout(sc->sc_poll_handle, sc->sc_ival, uhci_poll_hub, xfer); 3548 sc->sc_intr_xfer = xfer; 3549 return (USBD_IN_PROGRESS); 3550 } 3551 3552 /* Close the root interrupt pipe. */ 3553 void 3554 uhci_root_intr_close(usbd_pipe_handle pipe) 3555 { 3556 uhci_softc_t *sc = (uhci_softc_t *)pipe->device->bus; 3557 3558 usb_uncallout(sc->sc_poll_handle, uhci_poll_hub, sc->sc_intr_xfer); 3559 sc->sc_intr_xfer = NULL; 3560 DPRINTF(("uhci_root_intr_close\n")); 3561 } 3562