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