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