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