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