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