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