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