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