1 /* $OpenBSD: ehci.c,v 1.102 2009/09/05 10:25:55 miod Exp $ */ 2 /* $NetBSD: ehci.c,v 1.66 2004/06/30 03:11:56 mycroft Exp $ */ 3 4 /* 5 * Copyright (c) 2004-2008 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Lennart Augustsson (lennart@augustsson.net), Charles M. Hannum and 10 * Jeremy Morse (jeremy.morse@gmail.com). 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 36 * 37 * The EHCI 1.0 spec can be found at 38 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 39 * and the USB 2.0 spec at 40 * http://www.usb.org/developers/docs/usb_20.zip 41 */ 42 43 /* 44 * TODO: 45 * 1) The hub driver needs to handle and schedule the transaction translator, 46 * to assign place in frame where different devices get to go. See chapter 47 * on hubs in USB 2.0 for details. 48 * 49 * 2) Command failures are not recovered correctly. 50 */ 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/rwlock.h> 56 #include <sys/malloc.h> 57 #include <sys/device.h> 58 #include <sys/selinfo.h> 59 #include <sys/proc.h> 60 #include <sys/queue.h> 61 #include <sys/timeout.h> 62 63 #include <machine/bus.h> 64 #include <machine/endian.h> 65 66 #include <dev/usb/usb.h> 67 #include <dev/usb/usbdi.h> 68 #include <dev/usb/usbdivar.h> 69 #include <dev/usb/usb_mem.h> 70 #include <dev/usb/usb_quirks.h> 71 72 #include <dev/usb/ehcireg.h> 73 #include <dev/usb/ehcivar.h> 74 75 #include <dev/rndvar.h> 76 77 struct cfdriver ehci_cd = { 78 NULL, "ehci", DV_DULL 79 }; 80 81 #ifdef EHCI_DEBUG 82 #define DPRINTF(x) do { if (ehcidebug) printf x; } while(0) 83 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) printf x; } while (0) 84 int ehcidebug = 0; 85 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f)) 86 #else 87 #define DPRINTF(x) 88 #define DPRINTFN(n,x) 89 #endif 90 91 #define mstohz(ms) ((ms) * hz / 1000) 92 93 struct ehci_pipe { 94 struct usbd_pipe pipe; 95 96 ehci_soft_qh_t *sqh; 97 union { 98 ehci_soft_qtd_t *qtd; 99 /* ehci_soft_itd_t *itd; */ 100 } tail; 101 union { 102 /* Control pipe */ 103 struct { 104 usb_dma_t reqdma; 105 u_int length; 106 /*ehci_soft_qtd_t *setup, *data, *stat;*/ 107 } ctl; 108 /* Interrupt pipe */ 109 struct { 110 u_int length; 111 } intr; 112 /* Bulk pipe */ 113 struct { 114 u_int length; 115 } bulk; 116 /* Iso pipe */ 117 struct { 118 u_int next_frame; 119 u_int cur_xfers; 120 } isoc; 121 } u; 122 }; 123 124 u_int8_t ehci_reverse_bits(u_int8_t, int); 125 126 void ehci_power(int, void *); 127 128 usbd_status ehci_open(usbd_pipe_handle); 129 void ehci_poll(struct usbd_bus *); 130 void ehci_softintr(void *); 131 int ehci_intr1(ehci_softc_t *); 132 void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle); 133 void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *); 134 void ehci_check_qh_intr(ehci_softc_t *, struct ehci_xfer *); 135 void ehci_check_itd_intr(ehci_softc_t *, struct ehci_xfer *); 136 void ehci_idone(struct ehci_xfer *); 137 void ehci_timeout(void *); 138 void ehci_timeout_task(void *); 139 void ehci_intrlist_timeout(void *); 140 141 usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); 142 void ehci_freem(struct usbd_bus *, usb_dma_t *); 143 144 usbd_xfer_handle ehci_allocx(struct usbd_bus *); 145 void ehci_freex(struct usbd_bus *, usbd_xfer_handle); 146 147 usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle); 148 usbd_status ehci_root_ctrl_start(usbd_xfer_handle); 149 void ehci_root_ctrl_abort(usbd_xfer_handle); 150 void ehci_root_ctrl_close(usbd_pipe_handle); 151 void ehci_root_ctrl_done(usbd_xfer_handle); 152 153 usbd_status ehci_root_intr_transfer(usbd_xfer_handle); 154 usbd_status ehci_root_intr_start(usbd_xfer_handle); 155 void ehci_root_intr_abort(usbd_xfer_handle); 156 void ehci_root_intr_close(usbd_pipe_handle); 157 void ehci_root_intr_done(usbd_xfer_handle); 158 159 usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle); 160 usbd_status ehci_device_ctrl_start(usbd_xfer_handle); 161 void ehci_device_ctrl_abort(usbd_xfer_handle); 162 void ehci_device_ctrl_close(usbd_pipe_handle); 163 void ehci_device_ctrl_done(usbd_xfer_handle); 164 165 usbd_status ehci_device_bulk_transfer(usbd_xfer_handle); 166 usbd_status ehci_device_bulk_start(usbd_xfer_handle); 167 void ehci_device_bulk_abort(usbd_xfer_handle); 168 void ehci_device_bulk_close(usbd_pipe_handle); 169 void ehci_device_bulk_done(usbd_xfer_handle); 170 171 usbd_status ehci_device_intr_transfer(usbd_xfer_handle); 172 usbd_status ehci_device_intr_start(usbd_xfer_handle); 173 void ehci_device_intr_abort(usbd_xfer_handle); 174 void ehci_device_intr_close(usbd_pipe_handle); 175 void ehci_device_intr_done(usbd_xfer_handle); 176 177 usbd_status ehci_device_isoc_transfer(usbd_xfer_handle); 178 usbd_status ehci_device_isoc_start(usbd_xfer_handle); 179 void ehci_device_isoc_abort(usbd_xfer_handle); 180 void ehci_device_isoc_close(usbd_pipe_handle); 181 void ehci_device_isoc_done(usbd_xfer_handle); 182 183 void ehci_device_clear_toggle(usbd_pipe_handle pipe); 184 void ehci_noop(usbd_pipe_handle pipe); 185 186 int ehci_str(usb_string_descriptor_t *, int, const char *); 187 void ehci_pcd(ehci_softc_t *, usbd_xfer_handle); 188 void ehci_disown(ehci_softc_t *, int, int); 189 190 ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *); 191 void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *); 192 193 ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *); 194 void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *); 195 usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *, 196 ehci_softc_t *, u_int, int, usbd_xfer_handle, 197 ehci_soft_qtd_t **, ehci_soft_qtd_t **); 198 void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *, 199 ehci_soft_qtd_t *); 200 201 ehci_soft_itd_t *ehci_alloc_itd(ehci_softc_t *sc); 202 void ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd); 203 void ehci_rem_free_itd_chain(ehci_softc_t *sc, 204 struct ehci_xfer *exfer); 205 void ehci_abort_isoc_xfer(usbd_xfer_handle xfer, 206 usbd_status status); 207 208 usbd_status ehci_device_request(usbd_xfer_handle xfer); 209 210 usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *, 211 int ival); 212 213 void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *); 214 void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *, 215 ehci_soft_qh_t *); 216 void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *); 217 void ehci_sync_hc(ehci_softc_t *); 218 219 void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *); 220 void ehci_abort_xfer(usbd_xfer_handle, usbd_status); 221 222 #ifdef EHCI_DEBUG 223 void ehci_dump_regs(ehci_softc_t *); 224 void ehci_dump(void); 225 ehci_softc_t *theehci; 226 void ehci_dump_link(ehci_link_t, int); 227 void ehci_dump_sqtds(ehci_soft_qtd_t *); 228 void ehci_dump_sqtd(ehci_soft_qtd_t *); 229 void ehci_dump_qtd(ehci_qtd_t *); 230 void ehci_dump_sqh(ehci_soft_qh_t *); 231 #if notyet 232 void ehci_dump_sitd(struct ehci_soft_itd *itd); 233 void ehci_dump_itd(struct ehci_soft_itd *); 234 #endif 235 #ifdef DIAGNOSTIC 236 void ehci_dump_exfer(struct ehci_xfer *); 237 #endif 238 #endif 239 240 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE) 241 242 #define EHCI_INTR_ENDPT 1 243 244 #define ehci_add_intr_list(sc, ex) \ 245 TAILQ_INSERT_TAIL(&(sc)->sc_intrhead, (ex), inext); 246 #define ehci_del_intr_list(sc, ex) \ 247 do { \ 248 TAILQ_REMOVE(&sc->sc_intrhead, (ex), inext); \ 249 (ex)->inext.tqe_prev = NULL; \ 250 } while (0) 251 #define ehci_active_intr_list(ex) ((ex)->inext.tqe_prev != NULL) 252 253 struct usbd_bus_methods ehci_bus_methods = { 254 ehci_open, 255 ehci_softintr, 256 ehci_poll, 257 ehci_allocm, 258 ehci_freem, 259 ehci_allocx, 260 ehci_freex, 261 }; 262 263 struct usbd_pipe_methods ehci_root_ctrl_methods = { 264 ehci_root_ctrl_transfer, 265 ehci_root_ctrl_start, 266 ehci_root_ctrl_abort, 267 ehci_root_ctrl_close, 268 ehci_noop, 269 ehci_root_ctrl_done, 270 }; 271 272 struct usbd_pipe_methods ehci_root_intr_methods = { 273 ehci_root_intr_transfer, 274 ehci_root_intr_start, 275 ehci_root_intr_abort, 276 ehci_root_intr_close, 277 ehci_noop, 278 ehci_root_intr_done, 279 }; 280 281 struct usbd_pipe_methods ehci_device_ctrl_methods = { 282 ehci_device_ctrl_transfer, 283 ehci_device_ctrl_start, 284 ehci_device_ctrl_abort, 285 ehci_device_ctrl_close, 286 ehci_noop, 287 ehci_device_ctrl_done, 288 }; 289 290 struct usbd_pipe_methods ehci_device_intr_methods = { 291 ehci_device_intr_transfer, 292 ehci_device_intr_start, 293 ehci_device_intr_abort, 294 ehci_device_intr_close, 295 ehci_device_clear_toggle, 296 ehci_device_intr_done, 297 }; 298 299 struct usbd_pipe_methods ehci_device_bulk_methods = { 300 ehci_device_bulk_transfer, 301 ehci_device_bulk_start, 302 ehci_device_bulk_abort, 303 ehci_device_bulk_close, 304 ehci_device_clear_toggle, 305 ehci_device_bulk_done, 306 }; 307 308 struct usbd_pipe_methods ehci_device_isoc_methods = { 309 ehci_device_isoc_transfer, 310 ehci_device_isoc_start, 311 ehci_device_isoc_abort, 312 ehci_device_isoc_close, 313 ehci_noop, 314 ehci_device_isoc_done, 315 }; 316 317 /* 318 * Reverse a number with nbits bits. Used to evenly distribute lower-level 319 * interrupt heads in the periodic schedule. 320 * Suitable for use with EHCI_IPOLLRATES <= 9. 321 */ 322 u_int8_t 323 ehci_reverse_bits(u_int8_t c, int nbits) 324 { 325 c = ((c >> 1) & 0x55) | ((c << 1) & 0xaa); 326 c = ((c >> 2) & 0x33) | ((c << 2) & 0xcc); 327 c = ((c >> 4) & 0x0f) | ((c << 4) & 0xf0); 328 329 return c >> (8 - nbits); 330 } 331 332 usbd_status 333 ehci_init(ehci_softc_t *sc) 334 { 335 u_int32_t sparams, cparams, hcr; 336 u_int i, j; 337 usbd_status err; 338 ehci_soft_qh_t *sqh; 339 340 #ifdef EHCI_DEBUG 341 u_int32_t vers; 342 theehci = sc; 343 344 DPRINTF(("ehci_init: start\n")); 345 346 vers = EREAD2(sc, EHCI_HCIVERSION); 347 DPRINTF(("%s: EHCI version %x.%x\n", sc->sc_bus.bdev.dv_xname, 348 vers >> 8, vers & 0xff)); 349 #endif 350 351 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH); 352 353 sparams = EREAD4(sc, EHCI_HCSPARAMS); 354 DPRINTF(("ehci_init: sparams=0x%x\n", sparams)); 355 sc->sc_noport = EHCI_HCS_N_PORTS(sparams); 356 cparams = EREAD4(sc, EHCI_HCCPARAMS); 357 DPRINTF(("ehci_init: cparams=0x%x\n", cparams)); 358 359 /* MUST clear segment register if 64 bit capable. */ 360 if (EHCI_HCC_64BIT(cparams)) 361 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0); 362 363 sc->sc_bus.usbrev = USBREV_2_0; 364 365 /* Reset the controller */ 366 DPRINTF(("%s: resetting\n", sc->sc_bus.bdev.dv_xname)); 367 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */ 368 usb_delay_ms(&sc->sc_bus, 1); 369 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET); 370 for (i = 0; i < 100; i++) { 371 usb_delay_ms(&sc->sc_bus, 1); 372 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET; 373 if (!hcr) 374 break; 375 } 376 if (hcr) { 377 printf("%s: reset timeout\n", 378 sc->sc_bus.bdev.dv_xname); 379 return (USBD_IOERROR); 380 } 381 382 /* XXX need proper intr scheduling */ 383 sc->sc_rand = 96; 384 385 /* frame list size at default, read back what we got and use that */ 386 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) { 387 case 0: 388 sc->sc_flsize = 1024; 389 break; 390 case 1: 391 sc->sc_flsize = 512; 392 break; 393 case 2: 394 sc->sc_flsize = 256; 395 break; 396 case 3: 397 return (USBD_IOERROR); 398 } 399 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t), 400 EHCI_FLALIGN_ALIGN, &sc->sc_fldma); 401 if (err) 402 return (err); 403 DPRINTF(("%s: flsize=%d\n", sc->sc_bus.bdev.dv_xname,sc->sc_flsize)); 404 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0); 405 406 for (i = 0; i < sc->sc_flsize; i++) 407 sc->sc_flist[i] = EHCI_NULL; 408 409 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0)); 410 411 sc->sc_softitds = malloc(sc->sc_flsize * sizeof(ehci_soft_itd_t *), 412 M_USB, M_NOWAIT | M_ZERO); 413 if (sc->sc_softitds == NULL) 414 return (ENOMEM); 415 LIST_INIT(&sc->sc_freeitds); 416 TAILQ_INIT(&sc->sc_intrhead); 417 418 /* Set up the bus struct. */ 419 sc->sc_bus.methods = &ehci_bus_methods; 420 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe); 421 422 sc->sc_powerhook = powerhook_establish(ehci_power, sc); 423 424 sc->sc_eintrs = EHCI_NORMAL_INTRS; 425 426 /* 427 * Allocate the interrupt dummy QHs. These are arranged to give poll 428 * intervals that are powers of 2 times 1ms. 429 */ 430 for (i = 0; i < EHCI_INTRQHS; i++) { 431 sqh = ehci_alloc_sqh(sc); 432 if (sqh == NULL) { 433 err = USBD_NOMEM; 434 goto bad1; 435 } 436 sc->sc_islots[i].sqh = sqh; 437 } 438 for (i = 0; i < EHCI_INTRQHS; i++) { 439 sqh = sc->sc_islots[i].sqh; 440 if (i == 0) { 441 /* The last (1ms) QH terminates. */ 442 sqh->qh.qh_link = EHCI_NULL; 443 sqh->next = NULL; 444 } else { 445 /* Otherwise the next QH has half the poll interval */ 446 sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh; 447 sqh->qh.qh_link = htole32(sqh->next->physaddr | 448 EHCI_LINK_QH); 449 } 450 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH)); 451 sqh->qh.qh_endphub = htole32(EHCI_QH_SET_MULT(1)); 452 sqh->qh.qh_curqtd = EHCI_NULL; 453 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 454 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 455 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED); 456 sqh->sqtd = NULL; 457 } 458 /* Point the frame list at the last level (128ms). */ 459 for (i = 0; i < (1 << (EHCI_IPOLLRATES - 1)); i++) 460 for (j = i; j < sc->sc_flsize; j += 1 << (EHCI_IPOLLRATES - 1)) 461 sc->sc_flist[j] = htole32(EHCI_LINK_QH | sc->sc_islots[ 462 EHCI_IQHIDX(EHCI_IPOLLRATES - 1, ehci_reverse_bits( 463 i, EHCI_IPOLLRATES - 1))].sqh->physaddr); 464 465 /* Allocate dummy QH that starts the async list. */ 466 sqh = ehci_alloc_sqh(sc); 467 if (sqh == NULL) { 468 err = USBD_NOMEM; 469 goto bad1; 470 } 471 /* Fill the QH */ 472 sqh->qh.qh_endp = 473 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL); 474 sqh->qh.qh_link = 475 htole32(sqh->physaddr | EHCI_LINK_QH); 476 sqh->qh.qh_curqtd = EHCI_NULL; 477 sqh->prev = sqh; /*It's a circular list.. */ 478 sqh->next = sqh; 479 /* Fill the overlay qTD */ 480 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 481 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 482 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED); 483 sqh->sqtd = NULL; 484 #ifdef EHCI_DEBUG 485 if (ehcidebug) 486 ehci_dump_sqh(sqh); 487 #endif 488 489 /* Point to async list */ 490 sc->sc_async_head = sqh; 491 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH); 492 493 timeout_set(&sc->sc_tmo_intrlist, ehci_intrlist_timeout, sc); 494 495 rw_init(&sc->sc_doorbell_lock, "ehcidb"); 496 497 /* Turn on controller */ 498 EOWRITE4(sc, EHCI_USBCMD, 499 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */ 500 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) | 501 EHCI_CMD_ASE | 502 EHCI_CMD_PSE | 503 EHCI_CMD_RS); 504 505 /* Take over port ownership */ 506 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF); 507 508 for (i = 0; i < 100; i++) { 509 usb_delay_ms(&sc->sc_bus, 1); 510 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 511 if (!hcr) 512 break; 513 } 514 if (hcr) { 515 printf("%s: run timeout\n", sc->sc_bus.bdev.dv_xname); 516 return (USBD_IOERROR); 517 } 518 519 /* Enable interrupts */ 520 DPRINTFN(1,("ehci_init: enabling\n")); 521 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 522 523 return (USBD_NORMAL_COMPLETION); 524 525 #if 0 526 bad2: 527 ehci_free_sqh(sc, sc->sc_async_head); 528 #endif 529 bad1: 530 usb_freemem(&sc->sc_bus, &sc->sc_fldma); 531 return (err); 532 } 533 534 int 535 ehci_intr(void *v) 536 { 537 ehci_softc_t *sc = v; 538 539 if (sc == NULL || sc->sc_dying) 540 return (0); 541 542 /* If we get an interrupt while polling, then just ignore it. */ 543 if (sc->sc_bus.use_polling) { 544 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 545 546 if (intrs) 547 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */ 548 return (0); 549 } 550 551 return (ehci_intr1(sc)); 552 } 553 554 int 555 ehci_intr1(ehci_softc_t *sc) 556 { 557 u_int32_t intrs, eintrs; 558 559 DPRINTFN(20,("ehci_intr1: enter\n")); 560 561 /* In case the interrupt occurs before initialization has completed. */ 562 if (sc == NULL) { 563 #ifdef DIAGNOSTIC 564 printf("ehci_intr1: sc == NULL\n"); 565 #endif 566 return (0); 567 } 568 569 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 570 if (intrs == 0xffffffff) { 571 sc->sc_dying = 1; 572 return (0); 573 } 574 if (!intrs) 575 return (0); 576 577 eintrs = intrs & sc->sc_eintrs; 578 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n", 579 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS), (u_int)eintrs)); 580 if (!eintrs) 581 return (0); 582 583 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */ 584 sc->sc_bus.intr_context++; 585 sc->sc_bus.no_intrs++; 586 if (eintrs & EHCI_STS_IAA) { 587 DPRINTF(("ehci_intr1: door bell\n")); 588 wakeup(&sc->sc_async_head); 589 eintrs &= ~EHCI_STS_IAA; 590 } 591 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) { 592 DPRINTFN(5,("ehci_intr1: %s %s\n", 593 eintrs & EHCI_STS_INT ? "INT" : "", 594 eintrs & EHCI_STS_ERRINT ? "ERRINT" : "")); 595 usb_schedsoftintr(&sc->sc_bus); 596 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT); 597 } 598 if (eintrs & EHCI_STS_HSE) { 599 printf("%s: unrecoverable error, controller halted\n", 600 sc->sc_bus.bdev.dv_xname); 601 /* XXX what else */ 602 } 603 if (eintrs & EHCI_STS_PCD) { 604 ehci_pcd(sc, sc->sc_intrxfer); 605 eintrs &= ~EHCI_STS_PCD; 606 } 607 608 sc->sc_bus.intr_context--; 609 610 if (eintrs != 0) { 611 /* Block unprocessed interrupts. */ 612 sc->sc_eintrs &= ~eintrs; 613 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 614 printf("%s: blocking intrs 0x%x\n", 615 sc->sc_bus.bdev.dv_xname, eintrs); 616 } 617 618 return (1); 619 } 620 621 void 622 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer) 623 { 624 u_char *p; 625 int i, m; 626 627 if (xfer == NULL) { 628 /* Just ignore the change. */ 629 return; 630 } 631 632 p = KERNADDR(&xfer->dmabuf, 0); 633 m = min(sc->sc_noport, xfer->length * 8 - 1); 634 memset(p, 0, xfer->length); 635 for (i = 1; i <= m; i++) { 636 /* Pick out CHANGE bits from the status reg. */ 637 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) 638 p[i/8] |= 1 << (i%8); 639 } 640 DPRINTF(("ehci_pcd: change=0x%02x\n", *p)); 641 xfer->actlen = xfer->length; 642 xfer->status = USBD_NORMAL_COMPLETION; 643 644 usb_transfer_complete(xfer); 645 } 646 647 void 648 ehci_softintr(void *v) 649 { 650 ehci_softc_t *sc = v; 651 struct ehci_xfer *ex, *nextex; 652 653 DPRINTFN(10,("%s: ehci_softintr (%d)\n", sc->sc_bus.bdev.dv_xname, 654 sc->sc_bus.intr_context)); 655 656 sc->sc_bus.intr_context++; 657 658 /* 659 * The only explanation I can think of for why EHCI is as brain dead 660 * as UHCI interrupt-wise is that Intel was involved in both. 661 * An interrupt just tells us that something is done, we have no 662 * clue what, so we need to scan through all active transfers. :-( 663 */ 664 for (ex = TAILQ_FIRST(&sc->sc_intrhead); ex; ex = nextex) { 665 nextex = TAILQ_NEXT(ex, inext); 666 ehci_check_intr(sc, ex); 667 } 668 669 /* Schedule a callout to catch any dropped transactions. */ 670 if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) && 671 !TAILQ_EMPTY(&sc->sc_intrhead)) { 672 timeout_add_sec(&sc->sc_tmo_intrlist, 1); 673 } 674 675 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 676 if (sc->sc_softwake) { 677 sc->sc_softwake = 0; 678 wakeup(&sc->sc_softwake); 679 } 680 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */ 681 682 sc->sc_bus.intr_context--; 683 } 684 685 /* Check for an interrupt. */ 686 void 687 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex) 688 { 689 int attr; 690 691 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex)); 692 693 attr = ex->xfer.pipe->endpoint->edesc->bmAttributes; 694 if (UE_GET_XFERTYPE(attr) == UE_ISOCHRONOUS) 695 ehci_check_itd_intr(sc, ex); 696 else 697 ehci_check_qh_intr(sc, ex); 698 699 return; 700 } 701 702 void 703 ehci_check_qh_intr(ehci_softc_t *sc, struct ehci_xfer *ex) 704 { 705 ehci_soft_qtd_t *sqtd, *lsqtd; 706 __uint32_t status; 707 708 if (ex->sqtdstart == NULL) { 709 printf("ehci_check_qh_intr: not valid sqtd\n"); 710 return; 711 } 712 713 lsqtd = ex->sqtdend; 714 #ifdef DIAGNOSTIC 715 if (lsqtd == NULL) { 716 printf("ehci_check_qh_intr: lsqtd==0\n"); 717 return; 718 } 719 #endif 720 /* 721 * If the last TD is still active we need to check whether there 722 * is a an error somewhere in the middle, or whether there was a 723 * short packet (SPD and not ACTIVE). 724 */ 725 if (letoh32(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) { 726 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex)); 727 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) { 728 status = letoh32(sqtd->qtd.qtd_status); 729 /* If there's an active QTD the xfer isn't done. */ 730 if (status & EHCI_QTD_ACTIVE) 731 break; 732 /* Any kind of error makes the xfer done. */ 733 if (status & EHCI_QTD_HALTED) 734 goto done; 735 /* We want short packets, and it is short: it's done */ 736 if (EHCI_QTD_GET_BYTES(status) != 0) 737 goto done; 738 } 739 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n", 740 ex, ex->sqtdstart)); 741 return; 742 } 743 done: 744 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex)); 745 timeout_del(&ex->xfer.timeout_handle); 746 usb_rem_task(ex->xfer.pipe->device, &ex->abort_task); 747 ehci_idone(ex); 748 } 749 750 void 751 ehci_check_itd_intr(ehci_softc_t *sc, struct ehci_xfer *ex) { 752 ehci_soft_itd_t *itd; 753 int i; 754 755 if (&ex->xfer != SIMPLEQ_FIRST(&ex->xfer.pipe->queue)) 756 return; 757 758 if (ex->itdstart == NULL) { 759 printf("ehci_check_itd_intr: not valid itd\n"); 760 return; 761 } 762 763 itd = ex->itdend; 764 #ifdef DIAGNOSTIC 765 if (itd == NULL) { 766 printf("ehci_check_itd_intr: itdend == 0\n"); 767 return; 768 } 769 #endif 770 771 /* 772 * Step 1, check no active transfers in last itd, meaning we're finished 773 * check no active transfers in last itd, meaning we're finished 774 */ 775 for (i = 0; i < 8; i++) { 776 if (letoh32(itd->itd.itd_ctl[i]) & EHCI_ITD_ACTIVE) 777 break; 778 } 779 780 if (i == 8) { 781 goto done; /* All 8 descriptors inactive, it's done */ 782 } 783 784 DPRINTFN(12, ("ehci_check_itd_intr: ex %p itd %p still active\n", ex, 785 ex->itdstart)); 786 return; 787 done: 788 DPRINTFN(12, ("ehci_check_itd_intr: ex=%p done\n", ex)); 789 timeout_del(&ex->xfer.timeout_handle); 790 ehci_idone(ex); 791 } 792 793 void 794 ehci_idone(struct ehci_xfer *ex) 795 { 796 usbd_xfer_handle xfer = &ex->xfer; 797 #ifdef EHCI_DEBUG 798 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 799 #endif 800 ehci_soft_qtd_t *sqtd, *lsqtd; 801 u_int32_t status = 0, nstatus = 0; 802 int actlen, cerr; 803 804 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex)); 805 #ifdef DIAGNOSTIC 806 { 807 int s = splhigh(); 808 if (ex->isdone) { 809 splx(s); 810 #ifdef EHCI_DEBUG 811 printf("ehci_idone: ex is done!\n "); 812 ehci_dump_exfer(ex); 813 #else 814 printf("ehci_idone: ex=%p is done!\n", ex); 815 #endif 816 return; 817 } 818 ex->isdone = 1; 819 splx(s); 820 } 821 #endif 822 if (xfer->status == USBD_CANCELLED || 823 xfer->status == USBD_TIMEOUT) { 824 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer)); 825 return; 826 } 827 828 #ifdef EHCI_DEBUG 829 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe)); 830 if (ehcidebug > 10) 831 ehci_dump_sqtds(ex->sqtdstart); 832 #endif 833 834 /* The transfer is done, compute actual length and status. */ 835 836 if (UE_GET_XFERTYPE(xfer->pipe->endpoint->edesc->bmAttributes) 837 == UE_ISOCHRONOUS) { 838 /* Isoc transfer */ 839 struct ehci_soft_itd *itd; 840 int i, nframes, len, uframes; 841 842 nframes = 0; 843 actlen = 0; 844 845 switch (xfer->pipe->endpoint->edesc->bInterval) { 846 case 0: 847 panic("ehci: isoc xfer suddenly has 0 bInterval, " 848 "invalid"); 849 case 1: 850 uframes = 1; 851 break; 852 case 2: 853 uframes = 2; 854 break; 855 case 3: 856 uframes = 4; 857 break; 858 default: 859 uframes = 8; 860 break; 861 } 862 863 for (itd = ex->itdstart; itd != NULL; itd = itd->xfer_next) { 864 for (i = 0; i < 8; i += uframes) { 865 /* XXX - driver didn't fill in the frame full 866 * of uframes. This leads to scheduling 867 * inefficiencies, but working around 868 * this doubles complexity of tracking 869 * an xfer. 870 */ 871 if (nframes >= xfer->nframes) 872 break; 873 874 status = letoh32(itd->itd.itd_ctl[i]); 875 len = EHCI_ITD_GET_LEN(status); 876 if (EHCI_ITD_GET_STATUS(status) != 0) 877 len = 0; /*No valid data on error*/ 878 879 xfer->frlengths[nframes++] = len; 880 actlen += len; 881 } 882 883 if (nframes >= xfer->nframes) 884 break; 885 } 886 887 xfer->actlen = actlen; 888 xfer->status = USBD_NORMAL_COMPLETION; 889 890 goto end; 891 } 892 893 /* Continue processing xfers using queue heads */ 894 895 lsqtd = ex->sqtdend; 896 actlen = 0; 897 for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; 898 sqtd = sqtd->nextqtd) { 899 nstatus = letoh32(sqtd->qtd.qtd_status); 900 if (nstatus & EHCI_QTD_ACTIVE) 901 break; 902 903 status = nstatus; 904 /* halt is ok if descriptor is last, and complete */ 905 if (sqtd->qtd.qtd_next == EHCI_NULL && 906 EHCI_QTD_GET_BYTES(status) == 0) 907 status &= ~EHCI_QTD_HALTED; 908 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP) 909 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status); 910 } 911 912 cerr = EHCI_QTD_GET_CERR(status); 913 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, cerr=%d, " 914 "status=0x%x\n", xfer->length, actlen, cerr, status)); 915 xfer->actlen = actlen; 916 if ((status & EHCI_QTD_HALTED) != 0) { 917 #ifdef EHCI_DEBUG 918 char sbuf[128]; 919 920 bitmask_snprintf((u_int32_t)status, 921 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR" 922 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf)); 923 924 DPRINTFN(2, 925 ("ehci_idone: error, addr=%d, endpt=0x%02x, " 926 "status 0x%s\n", 927 xfer->pipe->device->address, 928 xfer->pipe->endpoint->edesc->bEndpointAddress, 929 sbuf)); 930 if (ehcidebug > 2) { 931 ehci_dump_sqh(epipe->sqh); 932 ehci_dump_sqtds(ex->sqtdstart); 933 } 934 #endif 935 if ((status & EHCI_QTD_BABBLE) == 0 && cerr > 0) 936 xfer->status = USBD_STALLED; 937 else 938 xfer->status = USBD_IOERROR; /* more info XXX */ 939 } else 940 xfer->status = USBD_NORMAL_COMPLETION; 941 end: 942 /* XXX transfer_complete memcpys out transfer data (for in endpoints) 943 * during this call, before methods->done is called: dma sync required 944 * beforehand? */ 945 usb_transfer_complete(xfer); 946 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex)); 947 } 948 949 /* 950 * Wait here until controller claims to have an interrupt. 951 * Then call ehci_intr and return. Use timeout to avoid waiting 952 * too long. 953 */ 954 void 955 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer) 956 { 957 int timo; 958 u_int32_t intrs; 959 960 xfer->status = USBD_IN_PROGRESS; 961 for (timo = xfer->timeout; timo >= 0; timo--) { 962 usb_delay_ms(&sc->sc_bus, 1); 963 if (sc->sc_dying) 964 break; 965 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) & 966 sc->sc_eintrs; 967 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs)); 968 #ifdef EHCI_DEBUG 969 if (ehcidebug > 15) 970 ehci_dump_regs(sc); 971 #endif 972 if (intrs) { 973 ehci_intr1(sc); 974 if (xfer->status != USBD_IN_PROGRESS) 975 return; 976 } 977 } 978 979 /* Timeout */ 980 DPRINTF(("ehci_waitintr: timeout\n")); 981 xfer->status = USBD_TIMEOUT; 982 usb_transfer_complete(xfer); 983 /* XXX should free TD */ 984 } 985 986 void 987 ehci_poll(struct usbd_bus *bus) 988 { 989 ehci_softc_t *sc = (ehci_softc_t *)bus; 990 #ifdef EHCI_DEBUG 991 static int last; 992 int new; 993 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 994 if (new != last) { 995 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new)); 996 last = new; 997 } 998 #endif 999 1000 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) 1001 ehci_intr1(sc); 1002 } 1003 1004 int 1005 ehci_detach(struct ehci_softc *sc, int flags) 1006 { 1007 int rv = 0; 1008 1009 if (sc->sc_child != NULL) 1010 rv = config_detach(sc->sc_child, flags); 1011 1012 if (rv != 0) 1013 return (rv); 1014 1015 timeout_del(&sc->sc_tmo_intrlist); 1016 1017 if (sc->sc_powerhook != NULL) 1018 powerhook_disestablish(sc->sc_powerhook); 1019 if (sc->sc_shutdownhook != NULL) 1020 shutdownhook_disestablish(sc->sc_shutdownhook); 1021 1022 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */ 1023 1024 /* XXX free other data structures XXX */ 1025 1026 return (rv); 1027 } 1028 1029 1030 int 1031 ehci_activate(struct device *self, enum devact act) 1032 { 1033 struct ehci_softc *sc = (struct ehci_softc *)self; 1034 int rv = 0; 1035 1036 switch (act) { 1037 case DVACT_ACTIVATE: 1038 break; 1039 1040 case DVACT_DEACTIVATE: 1041 if (sc->sc_child != NULL) 1042 rv = config_deactivate(sc->sc_child); 1043 sc->sc_dying = 1; 1044 break; 1045 } 1046 return (rv); 1047 } 1048 1049 /* 1050 * Handle suspend/resume. 1051 * 1052 * We need to switch to polling mode here, because this routine is 1053 * called from an interrupt context. This is all right since we 1054 * are almost suspended anyway. 1055 */ 1056 void 1057 ehci_power(int why, void *v) 1058 { 1059 ehci_softc_t *sc = v; 1060 u_int32_t cmd, hcr; 1061 int s, i; 1062 1063 #ifdef EHCI_DEBUG 1064 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why)); 1065 if (ehcidebug > 0) 1066 ehci_dump_regs(sc); 1067 #endif 1068 1069 s = splhardusb(); 1070 switch (why) { 1071 case PWR_SUSPEND: 1072 case PWR_STANDBY: 1073 sc->sc_bus.use_polling++; 1074 1075 for (i = 1; i <= sc->sc_noport; i++) { 1076 cmd = EOREAD4(sc, EHCI_PORTSC(i)); 1077 if ((cmd & (EHCI_PS_PO|EHCI_PS_PE)) == EHCI_PS_PE) 1078 EOWRITE4(sc, EHCI_PORTSC(i), 1079 cmd | EHCI_PS_SUSP); 1080 } 1081 1082 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD); 1083 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE); 1084 EOWRITE4(sc, EHCI_USBCMD, cmd); 1085 1086 for (i = 0; i < 100; i++) { 1087 hcr = EOREAD4(sc, EHCI_USBSTS) & 1088 (EHCI_STS_ASS | EHCI_STS_PSS); 1089 if (hcr == 0) 1090 break; 1091 1092 usb_delay_ms(&sc->sc_bus, 1); 1093 } 1094 if (hcr != 0) 1095 printf("%s: reset timeout\n", 1096 sc->sc_bus.bdev.dv_xname); 1097 1098 cmd &= ~EHCI_CMD_RS; 1099 EOWRITE4(sc, EHCI_USBCMD, cmd); 1100 1101 for (i = 0; i < 100; i++) { 1102 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 1103 if (hcr == EHCI_STS_HCH) 1104 break; 1105 1106 usb_delay_ms(&sc->sc_bus, 1); 1107 } 1108 if (hcr != EHCI_STS_HCH) 1109 printf("%s: config timeout\n", 1110 sc->sc_bus.bdev.dv_xname); 1111 1112 sc->sc_bus.use_polling--; 1113 break; 1114 1115 case PWR_RESUME: 1116 sc->sc_bus.use_polling++; 1117 1118 /* restore things in case the bios sucks */ 1119 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0); 1120 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0)); 1121 EOWRITE4(sc, EHCI_ASYNCLISTADDR, 1122 sc->sc_async_head->physaddr | EHCI_LINK_QH); 1123 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 1124 1125 hcr = 0; 1126 for (i = 1; i <= sc->sc_noport; i++) { 1127 cmd = EOREAD4(sc, EHCI_PORTSC(i)); 1128 if ((cmd & (EHCI_PS_PO|EHCI_PS_SUSP)) == EHCI_PS_SUSP) { 1129 EOWRITE4(sc, EHCI_PORTSC(i), 1130 cmd | EHCI_PS_FPR); 1131 hcr = 1; 1132 } 1133 } 1134 1135 if (hcr) { 1136 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 1137 for (i = 1; i <= sc->sc_noport; i++) { 1138 cmd = EOREAD4(sc, EHCI_PORTSC(i)); 1139 if ((cmd & (EHCI_PS_PO|EHCI_PS_SUSP)) == 1140 EHCI_PS_SUSP) 1141 EOWRITE4(sc, EHCI_PORTSC(i), 1142 cmd & ~EHCI_PS_FPR); 1143 } 1144 } 1145 1146 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd); 1147 1148 /* Take over port ownership */ 1149 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF); 1150 1151 for (i = 0; i < 100; i++) { 1152 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 1153 if (hcr != EHCI_STS_HCH) 1154 break; 1155 1156 usb_delay_ms(&sc->sc_bus, 1); 1157 } 1158 if (hcr == EHCI_STS_HCH) 1159 printf("%s: config timeout\n", 1160 sc->sc_bus.bdev.dv_xname); 1161 1162 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 1163 1164 sc->sc_bus.use_polling--; 1165 break; 1166 } 1167 splx(s); 1168 1169 #ifdef EHCI_DEBUG 1170 DPRINTF(("ehci_power: sc=%p\n", sc)); 1171 if (ehcidebug > 0) 1172 ehci_dump_regs(sc); 1173 #endif 1174 } 1175 1176 /* 1177 * Shut down the controller when the system is going down. 1178 */ 1179 void 1180 ehci_shutdown(void *v) 1181 { 1182 ehci_softc_t *sc = v; 1183 1184 DPRINTF(("ehci_shutdown: stopping the HC\n")); 1185 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */ 1186 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET); 1187 } 1188 1189 usbd_status 1190 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) 1191 { 1192 struct ehci_softc *sc = (struct ehci_softc *)bus; 1193 usbd_status err; 1194 1195 err = usb_allocmem(&sc->sc_bus, size, 0, dma); 1196 #ifdef EHCI_DEBUG 1197 if (err) 1198 printf("ehci_allocm: usb_allocmem()=%d\n", err); 1199 #endif 1200 return (err); 1201 } 1202 1203 void 1204 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma) 1205 { 1206 struct ehci_softc *sc = (struct ehci_softc *)bus; 1207 1208 usb_freemem(&sc->sc_bus, dma); 1209 } 1210 1211 usbd_xfer_handle 1212 ehci_allocx(struct usbd_bus *bus) 1213 { 1214 struct ehci_softc *sc = (struct ehci_softc *)bus; 1215 usbd_xfer_handle xfer; 1216 1217 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 1218 if (xfer != NULL) { 1219 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 1220 #ifdef DIAGNOSTIC 1221 if (xfer->busy_free != XFER_FREE) 1222 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", 1223 xfer, xfer->busy_free); 1224 #endif 1225 } else 1226 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_NOWAIT); 1227 1228 if (xfer != NULL) { 1229 memset(xfer, 0, sizeof(struct ehci_xfer)); 1230 usb_init_task(&EXFER(xfer)->abort_task, ehci_timeout_task, 1231 xfer); 1232 EXFER(xfer)->ehci_xfer_flags = 0; 1233 #ifdef DIAGNOSTIC 1234 EXFER(xfer)->isdone = 1; 1235 xfer->busy_free = XFER_BUSY; 1236 #endif 1237 } 1238 return (xfer); 1239 } 1240 1241 void 1242 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) 1243 { 1244 struct ehci_softc *sc = (struct ehci_softc *)bus; 1245 1246 #ifdef DIAGNOSTIC 1247 if (xfer->busy_free != XFER_BUSY) { 1248 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer, 1249 xfer->busy_free); 1250 return; 1251 } 1252 xfer->busy_free = XFER_FREE; 1253 if (!EXFER(xfer)->isdone) { 1254 printf("ehci_freex: !isdone\n"); 1255 return; 1256 } 1257 #endif 1258 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); 1259 } 1260 1261 void 1262 ehci_device_clear_toggle(usbd_pipe_handle pipe) 1263 { 1264 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 1265 1266 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n", 1267 epipe, epipe->sqh->qh.qh_qtd.qtd_status)); 1268 #if defined(EHCI_DEBUG) && defined(USB_DEBUG) 1269 if (ehcidebug) 1270 usbd_dump_pipe(pipe); 1271 #endif 1272 #ifdef DIAGNOSTIC 1273 if ((epipe->sqh->qh.qh_qtd.qtd_status & htole32(EHCI_QTD_ACTIVE)) != 0) 1274 panic("ehci_device_clear_toggle: queue active"); 1275 #endif 1276 epipe->sqh->qh.qh_qtd.qtd_status &= htole32(~EHCI_QTD_TOGGLE_MASK); 1277 } 1278 1279 void 1280 ehci_noop(usbd_pipe_handle pipe) 1281 { 1282 } 1283 1284 #ifdef EHCI_DEBUG 1285 void 1286 ehci_dump_regs(ehci_softc_t *sc) 1287 { 1288 int i; 1289 1290 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n", 1291 EOREAD4(sc, EHCI_USBCMD), 1292 EOREAD4(sc, EHCI_USBSTS), 1293 EOREAD4(sc, EHCI_USBINTR)); 1294 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n", 1295 EOREAD4(sc, EHCI_FRINDEX), 1296 EOREAD4(sc, EHCI_CTRLDSSEGMENT), 1297 EOREAD4(sc, EHCI_PERIODICLISTBASE), 1298 EOREAD4(sc, EHCI_ASYNCLISTADDR)); 1299 for (i = 1; i <= sc->sc_noport; i++) 1300 printf("port %d status=0x%08x\n", i, 1301 EOREAD4(sc, EHCI_PORTSC(i))); 1302 } 1303 1304 /* 1305 * Unused function - this is meant to be called from a kernel 1306 * debugger. 1307 */ 1308 void 1309 ehci_dump() 1310 { 1311 ehci_dump_regs(theehci); 1312 } 1313 1314 void 1315 ehci_dump_link(ehci_link_t link, int type) 1316 { 1317 link = letoh32(link); 1318 printf("0x%08x", link); 1319 if (link & EHCI_LINK_TERMINATE) 1320 printf("<T>"); 1321 else { 1322 printf("<"); 1323 if (type) { 1324 switch (EHCI_LINK_TYPE(link)) { 1325 case EHCI_LINK_ITD: 1326 printf("ITD"); 1327 break; 1328 case EHCI_LINK_QH: 1329 printf("QH"); 1330 break; 1331 case EHCI_LINK_SITD: 1332 printf("SITD"); 1333 break; 1334 case EHCI_LINK_FSTN: 1335 printf("FSTN"); 1336 break; 1337 } 1338 } 1339 printf(">"); 1340 } 1341 } 1342 1343 void 1344 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd) 1345 { 1346 int i; 1347 u_int32_t stop; 1348 1349 stop = 0; 1350 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) { 1351 ehci_dump_sqtd(sqtd); 1352 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE); 1353 } 1354 if (!stop) 1355 printf("dump aborted, too many TDs\n"); 1356 } 1357 1358 void 1359 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd) 1360 { 1361 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr); 1362 ehci_dump_qtd(&sqtd->qtd); 1363 } 1364 1365 void 1366 ehci_dump_qtd(ehci_qtd_t *qtd) 1367 { 1368 u_int32_t s; 1369 char sbuf[128]; 1370 1371 printf(" next="); ehci_dump_link(qtd->qtd_next, 0); 1372 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0); 1373 printf("\n"); 1374 s = letoh32(qtd->qtd_status); 1375 bitmask_snprintf(EHCI_QTD_GET_STATUS(s), "\20\10ACTIVE\7HALTED" 1376 "\6BUFERR\5BABBLE\4XACTERR\3MISSED\2SPLIT\1PING", 1377 sbuf, sizeof(sbuf)); 1378 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n", 1379 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s), 1380 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s)); 1381 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s), 1382 EHCI_QTD_GET_PID(s), sbuf); 1383 for (s = 0; s < 5; s++) 1384 printf(" buffer[%d]=0x%08x\n", s, letoh32(qtd->qtd_buffer[s])); 1385 } 1386 1387 void 1388 ehci_dump_sqh(ehci_soft_qh_t *sqh) 1389 { 1390 ehci_qh_t *qh = &sqh->qh; 1391 u_int32_t endp, endphub; 1392 1393 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr); 1394 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n"); 1395 endp = letoh32(qh->qh_endp); 1396 printf(" endp=0x%08x\n", endp); 1397 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n", 1398 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp), 1399 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp), 1400 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp)); 1401 printf(" mpl=0x%x ctl=%d nrl=%d\n", 1402 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp), 1403 EHCI_QH_GET_NRL(endp)); 1404 endphub = letoh32(qh->qh_endphub); 1405 printf(" endphub=0x%08x\n", endphub); 1406 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n", 1407 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 1408 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub), 1409 EHCI_QH_GET_MULT(endphub)); 1410 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n"); 1411 printf("Overlay qTD:\n"); 1412 ehci_dump_qtd(&qh->qh_qtd); 1413 } 1414 1415 #if notyet 1416 void 1417 ehci_dump_itd(struct ehci_soft_itd *itd) 1418 { 1419 ehci_isoc_trans_t t; 1420 ehci_isoc_bufr_ptr_t b, b2, b3; 1421 int i; 1422 1423 printf("ITD: next phys=%X\n", itd->itd.itd_next); 1424 1425 for (i = 0; i < 8;i++) { 1426 t = letoh32(itd->itd.itd_ctl[i]); 1427 printf("ITDctl %d: stat=%X len=%X ioc=%X pg=%X offs=%X\n", i, 1428 EHCI_ITD_GET_STATUS(t), EHCI_ITD_GET_LEN(t), 1429 EHCI_ITD_GET_IOC(t), EHCI_ITD_GET_PG(t), 1430 EHCI_ITD_GET_OFFS(t)); 1431 } 1432 printf("ITDbufr: "); 1433 for (i = 0; i < 7; i++) 1434 printf("%X,", EHCI_ITD_GET_BPTR(letoh32(itd->itd.itd_bufr[i]))); 1435 1436 b = letoh32(itd->itd.itd_bufr[0]); 1437 b2 = letoh32(itd->itd.itd_bufr[1]); 1438 b3 = letoh32(itd->itd.itd_bufr[2]); 1439 printf("\nep=%X daddr=%X dir=%d maxpkt=%X multi=%X\n", 1440 EHCI_ITD_GET_EP(b), EHCI_ITD_GET_DADDR(b), EHCI_ITD_GET_DIR(b2), 1441 EHCI_ITD_GET_MAXPKT(b2), EHCI_ITD_GET_MULTI(b3)); 1442 } 1443 1444 void 1445 ehci_dump_sitd(struct ehci_soft_itd *itd) 1446 { 1447 printf("SITD %p next=%p prev=%p xfernext=%p physaddr=%X slot=%d\n", 1448 itd, itd->u.frame_list.next, itd->u.frame_list.prev, 1449 itd->xfer_next, itd->physaddr, itd->slot); 1450 } 1451 #endif 1452 1453 #ifdef DIAGNOSTIC 1454 void 1455 ehci_dump_exfer(struct ehci_xfer *ex) 1456 { 1457 printf("ehci_dump_exfer: ex=%p sqtdstart=%p end=%p itdstart=%p end=%p " 1458 "isdone=%d\n", ex, ex->sqtdstart, ex->sqtdend, ex->itdstart, 1459 ex->itdend, ex->isdone); 1460 } 1461 #endif 1462 1463 #endif /* EHCI_DEBUG */ 1464 1465 usbd_status 1466 ehci_open(usbd_pipe_handle pipe) 1467 { 1468 usbd_device_handle dev = pipe->device; 1469 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 1470 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; 1471 u_int8_t addr = dev->address; 1472 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE; 1473 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 1474 ehci_soft_qh_t *sqh; 1475 usbd_status err; 1476 int s; 1477 int ival, speed, naks; 1478 int hshubaddr, hshubport; 1479 1480 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n", 1481 pipe, addr, ed->bEndpointAddress, sc->sc_addr)); 1482 1483 if (sc->sc_dying) 1484 return (USBD_IOERROR); 1485 1486 if (dev->myhsport) { 1487 hshubaddr = dev->myhsport->parent->address; 1488 hshubport = dev->myhsport->portno; 1489 } else { 1490 hshubaddr = 0; 1491 hshubport = 0; 1492 } 1493 1494 if (addr == sc->sc_addr) { 1495 switch (ed->bEndpointAddress) { 1496 case USB_CONTROL_ENDPOINT: 1497 pipe->methods = &ehci_root_ctrl_methods; 1498 break; 1499 case UE_DIR_IN | EHCI_INTR_ENDPT: 1500 pipe->methods = &ehci_root_intr_methods; 1501 break; 1502 default: 1503 return (USBD_INVAL); 1504 } 1505 return (USBD_NORMAL_COMPLETION); 1506 } 1507 1508 /* XXX All this stuff is only valid for async. */ 1509 switch (dev->speed) { 1510 case USB_SPEED_LOW: 1511 speed = EHCI_QH_SPEED_LOW; 1512 break; 1513 case USB_SPEED_FULL: 1514 speed = EHCI_QH_SPEED_FULL; 1515 break; 1516 case USB_SPEED_HIGH: 1517 speed = EHCI_QH_SPEED_HIGH; 1518 break; 1519 default: 1520 panic("ehci_open: bad device speed %d", dev->speed); 1521 } 1522 if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) { 1523 printf("%s: Error opening low/full speed isoc endpoint.\n" 1524 "A low/full speed device is attached to a USB2 hub, and " 1525 "transaction translations are not yet supported.\n" 1526 "Reattach the device to the root hub instead.\n", 1527 sc->sc_bus.bdev.dv_xname); 1528 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n", 1529 hshubaddr, hshubport)); 1530 return (USBD_INVAL); 1531 } 1532 1533 naks = 8; /* XXX */ 1534 1535 /* Allocate sqh for everything, save isoc xfers */ 1536 if (xfertype != UE_ISOCHRONOUS) { 1537 sqh = ehci_alloc_sqh(sc); 1538 if (sqh == NULL) 1539 return (USBD_NOMEM); 1540 /* qh_link filled when the QH is added */ 1541 sqh->qh.qh_endp = htole32( 1542 EHCI_QH_SET_ADDR(addr) | 1543 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) | 1544 EHCI_QH_SET_EPS(speed) | 1545 (xfertype == UE_CONTROL ? EHCI_QH_DTC : 0) | 1546 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) | 1547 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ? 1548 EHCI_QH_CTL : 0) | 1549 EHCI_QH_SET_NRL(naks) 1550 ); 1551 sqh->qh.qh_endphub = htole32( 1552 EHCI_QH_SET_MULT(1) | 1553 EHCI_QH_SET_HUBA(hshubaddr) | 1554 EHCI_QH_SET_PORT(hshubport) | 1555 EHCI_QH_SET_CMASK(0x1c) | /* XXX */ 1556 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x01 : 0) 1557 ); 1558 sqh->qh.qh_curqtd = EHCI_NULL; 1559 /* Fill the overlay qTD */ 1560 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 1561 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 1562 sqh->qh.qh_qtd.qtd_status = 1563 htole32(EHCI_QTD_SET_TOGGLE(pipe->endpoint->savedtoggle)); 1564 epipe->sqh = sqh; 1565 } else { 1566 sqh = NULL; 1567 } /*xfertype == UE_ISOC*/ 1568 1569 switch (xfertype) { 1570 case UE_CONTROL: 1571 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t), 1572 0, &epipe->u.ctl.reqdma); 1573 #ifdef EHCI_DEBUG 1574 if (err) 1575 printf("ehci_open: usb_allocmem()=%d\n", err); 1576 #endif 1577 if (err) 1578 goto bad; 1579 pipe->methods = &ehci_device_ctrl_methods; 1580 s = splusb(); 1581 ehci_add_qh(sqh, sc->sc_async_head); 1582 splx(s); 1583 break; 1584 case UE_BULK: 1585 pipe->methods = &ehci_device_bulk_methods; 1586 s = splusb(); 1587 ehci_add_qh(sqh, sc->sc_async_head); 1588 splx(s); 1589 break; 1590 case UE_INTERRUPT: 1591 pipe->methods = &ehci_device_intr_methods; 1592 ival = pipe->interval; 1593 if (ival == USBD_DEFAULT_INTERVAL) 1594 ival = ed->bInterval; 1595 s = splusb(); 1596 err = ehci_device_setintr(sc, sqh, ival); 1597 splx(s); 1598 return (err); 1599 case UE_ISOCHRONOUS: 1600 pipe->methods = &ehci_device_isoc_methods; 1601 if (ed->bInterval == 0 || ed->bInterval > 16) { 1602 printf("ehci: opening pipe with invalid bInterval\n"); 1603 err = USBD_INVAL; 1604 goto bad; 1605 } 1606 if (UGETW(ed->wMaxPacketSize) == 0) { 1607 printf("ehci: zero length endpoint open request\n"); 1608 err = USBD_INVAL; 1609 goto bad; 1610 } 1611 epipe->u.isoc.next_frame = 0; 1612 epipe->u.isoc.cur_xfers = 0; 1613 break; 1614 default: 1615 DPRINTF(("ehci: bad xfer type %d\n", xfertype)); 1616 return (USBD_INVAL); 1617 } 1618 return (USBD_NORMAL_COMPLETION); 1619 1620 bad: 1621 if (sqh != NULL) 1622 ehci_free_sqh(sc, sqh); 1623 return (err); 1624 } 1625 1626 /* 1627 * Add an ED to the schedule. Called at splusb(). 1628 * If in the async schedule, it will always have a next. 1629 * If in the intr schedule it may not. 1630 */ 1631 void 1632 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head) 1633 { 1634 SPLUSBCHECK; 1635 1636 sqh->next = head->next; 1637 sqh->prev = head; 1638 sqh->qh.qh_link = head->qh.qh_link; 1639 head->next = sqh; 1640 if (sqh->next) 1641 sqh->next->prev = sqh; 1642 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH); 1643 1644 #ifdef EHCI_DEBUG 1645 if (ehcidebug > 5) { 1646 printf("ehci_add_qh:\n"); 1647 ehci_dump_sqh(sqh); 1648 } 1649 #endif 1650 } 1651 1652 /* 1653 * Remove an ED from the schedule. Called at splusb(). 1654 * Will always have a 'next' if it's in the async list as it's circular. 1655 */ 1656 void 1657 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head) 1658 { 1659 SPLUSBCHECK; 1660 /* XXX */ 1661 sqh->prev->qh.qh_link = sqh->qh.qh_link; 1662 sqh->prev->next = sqh->next; 1663 if (sqh->next) 1664 sqh->next->prev = sqh->prev; 1665 ehci_sync_hc(sc); 1666 } 1667 1668 void 1669 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd) 1670 { 1671 int i; 1672 u_int32_t status; 1673 1674 /* Save toggle bit and ping status. */ 1675 status = sqh->qh.qh_qtd.qtd_status & 1676 htole32(EHCI_QTD_TOGGLE_MASK | 1677 EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE)); 1678 /* Set HALTED to make hw leave it alone. */ 1679 sqh->qh.qh_qtd.qtd_status = 1680 htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED)); 1681 sqh->qh.qh_curqtd = 0; 1682 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr); 1683 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 1684 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) 1685 sqh->qh.qh_qtd.qtd_buffer[i] = 0; 1686 sqh->sqtd = sqtd; 1687 /* Set !HALTED && !ACTIVE to start execution, preserve some fields */ 1688 sqh->qh.qh_qtd.qtd_status = status; 1689 } 1690 1691 /* 1692 * Ensure that the HC has released all references to the QH. We do this 1693 * by asking for a Async Advance Doorbell interrupt and then we wait for 1694 * the interrupt. 1695 * To make this easier we first obtain exclusive use of the doorbell. 1696 */ 1697 void 1698 ehci_sync_hc(ehci_softc_t *sc) 1699 { 1700 int s, error; 1701 int tries = 0; 1702 1703 if (sc->sc_dying) { 1704 DPRINTFN(2,("ehci_sync_hc: dying\n")); 1705 return; 1706 } 1707 DPRINTFN(2,("ehci_sync_hc: enter\n")); 1708 /* get doorbell */ 1709 rw_enter_write(&sc->sc_doorbell_lock); 1710 s = splhardusb(); 1711 do { 1712 /* ask for doorbell */ 1713 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | 1714 EHCI_CMD_IAAD); 1715 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n", 1716 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS))); 1717 /* bell wait */ 1718 error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz / 2); 1719 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n", 1720 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS))); 1721 } while (error && ++tries < 10); 1722 splx(s); 1723 /* release doorbell */ 1724 rw_exit_write(&sc->sc_doorbell_lock); 1725 #ifdef DIAGNOSTIC 1726 if (error) 1727 printf("ehci_sync_hc: tsleep() = %d\n", error); 1728 #endif 1729 DPRINTFN(2,("ehci_sync_hc: exit\n")); 1730 } 1731 1732 /*Call at splusb*/ 1733 void 1734 ehci_rem_free_itd_chain(ehci_softc_t *sc, struct ehci_xfer *exfer) 1735 { 1736 struct ehci_soft_itd *itd, *prev; 1737 1738 prev = NULL; 1739 1740 if (exfer->itdstart == NULL || exfer->itdend == NULL) 1741 panic("ehci isoc xfer being freed, but with no itd chain"); 1742 1743 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) { 1744 prev = itd->u.frame_list.prev; 1745 /* Unlink itd from hardware chain, or frame array */ 1746 if (prev == NULL) { /* We're at the table head */ 1747 sc->sc_softitds[itd->slot] = itd->u.frame_list.next; 1748 sc->sc_flist[itd->slot] = itd->itd.itd_next; 1749 1750 if (itd->u.frame_list.next != NULL) 1751 itd->u.frame_list.next->u.frame_list.prev = 1752 NULL; 1753 } else { 1754 /* XXX this part is untested... */ 1755 prev->itd.itd_next = itd->itd.itd_next; 1756 prev->u.frame_list.next = itd->u.frame_list.next; 1757 if (itd->u.frame_list.next != NULL) 1758 itd->u.frame_list.next->u.frame_list.prev = 1759 prev; 1760 } 1761 } 1762 1763 prev = NULL; 1764 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) { 1765 if (prev != NULL) 1766 ehci_free_itd(sc, prev); 1767 prev = itd; 1768 } 1769 if (prev) 1770 ehci_free_itd(sc, prev); 1771 exfer->itdstart = NULL; 1772 exfer->itdend = NULL; 1773 } 1774 1775 /***********/ 1776 1777 /* 1778 * Data structures and routines to emulate the root hub. 1779 */ 1780 usb_device_descriptor_t ehci_devd = { 1781 USB_DEVICE_DESCRIPTOR_SIZE, 1782 UDESC_DEVICE, /* type */ 1783 {0x00, 0x02}, /* USB version */ 1784 UDCLASS_HUB, /* class */ 1785 UDSUBCLASS_HUB, /* subclass */ 1786 UDPROTO_HSHUBSTT, /* protocol */ 1787 64, /* max packet */ 1788 {0},{0},{0x00,0x01}, /* device id */ 1789 1,2,0, /* string indicies */ 1790 1 /* # of configurations */ 1791 }; 1792 1793 usb_device_qualifier_t ehci_odevd = { 1794 USB_DEVICE_DESCRIPTOR_SIZE, 1795 UDESC_DEVICE_QUALIFIER, /* type */ 1796 {0x00, 0x02}, /* USB version */ 1797 UDCLASS_HUB, /* class */ 1798 UDSUBCLASS_HUB, /* subclass */ 1799 UDPROTO_FSHUB, /* protocol */ 1800 64, /* max packet */ 1801 1, /* # of configurations */ 1802 0 1803 }; 1804 1805 usb_config_descriptor_t ehci_confd = { 1806 USB_CONFIG_DESCRIPTOR_SIZE, 1807 UDESC_CONFIG, 1808 {USB_CONFIG_DESCRIPTOR_SIZE + 1809 USB_INTERFACE_DESCRIPTOR_SIZE + 1810 USB_ENDPOINT_DESCRIPTOR_SIZE}, 1811 1, 1812 1, 1813 0, 1814 UC_SELF_POWERED, 1815 0 /* max power */ 1816 }; 1817 1818 usb_interface_descriptor_t ehci_ifcd = { 1819 USB_INTERFACE_DESCRIPTOR_SIZE, 1820 UDESC_INTERFACE, 1821 0, 1822 0, 1823 1, 1824 UICLASS_HUB, 1825 UISUBCLASS_HUB, 1826 UIPROTO_HSHUBSTT, 1827 0 1828 }; 1829 1830 usb_endpoint_descriptor_t ehci_endpd = { 1831 USB_ENDPOINT_DESCRIPTOR_SIZE, 1832 UDESC_ENDPOINT, 1833 UE_DIR_IN | EHCI_INTR_ENDPT, 1834 UE_INTERRUPT, 1835 {8, 0}, /* max packet */ 1836 255 1837 }; 1838 1839 usb_hub_descriptor_t ehci_hubd = { 1840 USB_HUB_DESCRIPTOR_SIZE, 1841 UDESC_HUB, 1842 0, 1843 {0,0}, 1844 0, 1845 0, 1846 {0}, 1847 }; 1848 1849 int 1850 ehci_str(usb_string_descriptor_t *p, int l, const char *s) 1851 { 1852 int i; 1853 1854 if (l == 0) 1855 return (0); 1856 p->bLength = 2 * strlen(s) + 2; 1857 if (l == 1) 1858 return (1); 1859 p->bDescriptorType = UDESC_STRING; 1860 l -= 2; 1861 for (i = 0; s[i] && l > 1; i++, l -= 2) 1862 USETW2(p->bString[i], 0, s[i]); 1863 return (2*i+2); 1864 } 1865 1866 /* 1867 * Simulate a hardware hub by handling all the necessary requests. 1868 */ 1869 usbd_status 1870 ehci_root_ctrl_transfer(usbd_xfer_handle xfer) 1871 { 1872 usbd_status err; 1873 1874 /* Insert last in queue. */ 1875 err = usb_insert_transfer(xfer); 1876 if (err) 1877 return (err); 1878 1879 /* Pipe isn't running, start first */ 1880 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 1881 } 1882 1883 usbd_status 1884 ehci_root_ctrl_start(usbd_xfer_handle xfer) 1885 { 1886 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 1887 usb_device_request_t *req; 1888 void *buf = NULL; 1889 int port, i; 1890 int s, len, value, index, l, totlen = 0; 1891 usb_port_status_t ps; 1892 usb_hub_descriptor_t hubd; 1893 usbd_status err; 1894 u_int32_t v; 1895 1896 if (sc->sc_dying) 1897 return (USBD_IOERROR); 1898 1899 #ifdef DIAGNOSTIC 1900 if (!(xfer->rqflags & URQ_REQUEST)) 1901 /* XXX panic */ 1902 return (USBD_INVAL); 1903 #endif 1904 req = &xfer->request; 1905 1906 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n", 1907 req->bmRequestType, req->bRequest)); 1908 1909 len = UGETW(req->wLength); 1910 value = UGETW(req->wValue); 1911 index = UGETW(req->wIndex); 1912 1913 if (len != 0) 1914 buf = KERNADDR(&xfer->dmabuf, 0); 1915 1916 #define C(x,y) ((x) | ((y) << 8)) 1917 switch(C(req->bRequest, req->bmRequestType)) { 1918 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 1919 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 1920 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 1921 /* 1922 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 1923 * for the integrated root hub. 1924 */ 1925 break; 1926 case C(UR_GET_CONFIG, UT_READ_DEVICE): 1927 if (len > 0) { 1928 *(u_int8_t *)buf = sc->sc_conf; 1929 totlen = 1; 1930 } 1931 break; 1932 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 1933 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value)); 1934 switch(value >> 8) { 1935 case UDESC_DEVICE: 1936 if ((value & 0xff) != 0) { 1937 err = USBD_IOERROR; 1938 goto ret; 1939 } 1940 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 1941 USETW(ehci_devd.idVendor, sc->sc_id_vendor); 1942 memcpy(buf, &ehci_devd, l); 1943 break; 1944 /* 1945 * We can't really operate at another speed, but the spec says 1946 * we need this descriptor. 1947 */ 1948 case UDESC_DEVICE_QUALIFIER: 1949 if ((value & 0xff) != 0) { 1950 err = USBD_IOERROR; 1951 goto ret; 1952 } 1953 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 1954 memcpy(buf, &ehci_odevd, l); 1955 break; 1956 /* 1957 * We can't really operate at another speed, but the spec says 1958 * we need this descriptor. 1959 */ 1960 case UDESC_OTHER_SPEED_CONFIGURATION: 1961 case UDESC_CONFIG: 1962 if ((value & 0xff) != 0) { 1963 err = USBD_IOERROR; 1964 goto ret; 1965 } 1966 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); 1967 memcpy(buf, &ehci_confd, l); 1968 ((usb_config_descriptor_t *)buf)->bDescriptorType = 1969 value >> 8; 1970 buf = (char *)buf + l; 1971 len -= l; 1972 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); 1973 totlen += l; 1974 memcpy(buf, &ehci_ifcd, l); 1975 buf = (char *)buf + l; 1976 len -= l; 1977 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); 1978 totlen += l; 1979 memcpy(buf, &ehci_endpd, l); 1980 break; 1981 case UDESC_STRING: 1982 if (len == 0) 1983 break; 1984 *(u_int8_t *)buf = 0; 1985 totlen = 1; 1986 switch (value & 0xff) { 1987 case 0: /* Language table */ 1988 totlen = ehci_str(buf, len, "\001"); 1989 break; 1990 case 1: /* Vendor */ 1991 totlen = ehci_str(buf, len, sc->sc_vendor); 1992 break; 1993 case 2: /* Product */ 1994 totlen = ehci_str(buf, len, "EHCI root hub"); 1995 break; 1996 } 1997 break; 1998 default: 1999 err = USBD_IOERROR; 2000 goto ret; 2001 } 2002 break; 2003 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 2004 if (len > 0) { 2005 *(u_int8_t *)buf = 0; 2006 totlen = 1; 2007 } 2008 break; 2009 case C(UR_GET_STATUS, UT_READ_DEVICE): 2010 if (len > 1) { 2011 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED); 2012 totlen = 2; 2013 } 2014 break; 2015 case C(UR_GET_STATUS, UT_READ_INTERFACE): 2016 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 2017 if (len > 1) { 2018 USETW(((usb_status_t *)buf)->wStatus, 0); 2019 totlen = 2; 2020 } 2021 break; 2022 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 2023 if (value >= USB_MAX_DEVICES) { 2024 err = USBD_IOERROR; 2025 goto ret; 2026 } 2027 sc->sc_addr = value; 2028 break; 2029 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 2030 if (value != 0 && value != 1) { 2031 err = USBD_IOERROR; 2032 goto ret; 2033 } 2034 sc->sc_conf = value; 2035 break; 2036 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 2037 break; 2038 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 2039 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 2040 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 2041 err = USBD_IOERROR; 2042 goto ret; 2043 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 2044 break; 2045 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 2046 break; 2047 /* Hub requests */ 2048 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 2049 break; 2050 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 2051 DPRINTFN(8, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE " 2052 "port=%d feature=%d\n", index, value)); 2053 if (index < 1 || index > sc->sc_noport) { 2054 err = USBD_IOERROR; 2055 goto ret; 2056 } 2057 port = EHCI_PORTSC(index); 2058 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR; 2059 switch(value) { 2060 case UHF_PORT_ENABLE: 2061 EOWRITE4(sc, port, v &~ EHCI_PS_PE); 2062 break; 2063 case UHF_PORT_SUSPEND: 2064 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP); 2065 break; 2066 case UHF_PORT_POWER: 2067 EOWRITE4(sc, port, v &~ EHCI_PS_PP); 2068 break; 2069 case UHF_PORT_TEST: 2070 DPRINTFN(2,("ehci_root_ctrl_start: " 2071 "clear port test %d\n", index)); 2072 break; 2073 case UHF_PORT_INDICATOR: 2074 DPRINTFN(2,("ehci_root_ctrl_start: " 2075 "clear port index %d\n", index)); 2076 EOWRITE4(sc, port, v &~ EHCI_PS_PIC); 2077 break; 2078 case UHF_C_PORT_CONNECTION: 2079 EOWRITE4(sc, port, v | EHCI_PS_CSC); 2080 break; 2081 case UHF_C_PORT_ENABLE: 2082 EOWRITE4(sc, port, v | EHCI_PS_PEC); 2083 break; 2084 case UHF_C_PORT_SUSPEND: 2085 /* how? */ 2086 break; 2087 case UHF_C_PORT_OVER_CURRENT: 2088 EOWRITE4(sc, port, v | EHCI_PS_OCC); 2089 break; 2090 case UHF_C_PORT_RESET: 2091 sc->sc_isreset = 0; 2092 break; 2093 default: 2094 err = USBD_IOERROR; 2095 goto ret; 2096 } 2097 break; 2098 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 2099 if ((value & 0xff) != 0) { 2100 err = USBD_IOERROR; 2101 goto ret; 2102 } 2103 hubd = ehci_hubd; 2104 hubd.bNbrPorts = sc->sc_noport; 2105 v = EOREAD4(sc, EHCI_HCSPARAMS); 2106 USETW(hubd.wHubCharacteristics, 2107 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH | 2108 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) 2109 ? UHD_PORT_IND : 0); 2110 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */ 2111 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8) 2112 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */ 2113 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i; 2114 l = min(len, hubd.bDescLength); 2115 totlen = l; 2116 memcpy(buf, &hubd, l); 2117 break; 2118 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 2119 if (len != 4) { 2120 err = USBD_IOERROR; 2121 goto ret; 2122 } 2123 memset(buf, 0, len); /* ? XXX */ 2124 totlen = len; 2125 break; 2126 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 2127 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n", 2128 index)); 2129 if (index < 1 || index > sc->sc_noport) { 2130 err = USBD_IOERROR; 2131 goto ret; 2132 } 2133 if (len != 4) { 2134 err = USBD_IOERROR; 2135 goto ret; 2136 } 2137 v = EOREAD4(sc, EHCI_PORTSC(index)); 2138 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n", v)); 2139 i = UPS_HIGH_SPEED; 2140 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS; 2141 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED; 2142 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND; 2143 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR; 2144 if (v & EHCI_PS_PR) i |= UPS_RESET; 2145 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER; 2146 USETW(ps.wPortStatus, i); 2147 i = 0; 2148 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS; 2149 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED; 2150 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR; 2151 if (sc->sc_isreset) i |= UPS_C_PORT_RESET; 2152 USETW(ps.wPortChange, i); 2153 l = min(len, sizeof(ps)); 2154 memcpy(buf, &ps, l); 2155 totlen = l; 2156 break; 2157 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 2158 err = USBD_IOERROR; 2159 goto ret; 2160 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 2161 break; 2162 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 2163 if (index < 1 || index > sc->sc_noport) { 2164 err = USBD_IOERROR; 2165 goto ret; 2166 } 2167 port = EHCI_PORTSC(index); 2168 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR; 2169 switch(value) { 2170 case UHF_PORT_ENABLE: 2171 EOWRITE4(sc, port, v | EHCI_PS_PE); 2172 break; 2173 case UHF_PORT_SUSPEND: 2174 EOWRITE4(sc, port, v | EHCI_PS_SUSP); 2175 break; 2176 case UHF_PORT_DISOWN_TO_1_1: 2177 /* enter to Port Reset State */ 2178 v &= ~EHCI_PS_PE; 2179 EOWRITE4(sc, port, v | EHCI_PS_PR); 2180 ehci_disown(sc, index, 0); 2181 break; 2182 case UHF_PORT_RESET: 2183 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n", 2184 index)); 2185 if (EHCI_PS_IS_LOWSPEED(v)) { 2186 /* Low speed device, give up ownership. */ 2187 ehci_disown(sc, index, 1); 2188 break; 2189 } 2190 /* Start reset sequence. */ 2191 v &= ~ (EHCI_PS_PE | EHCI_PS_PR); 2192 EOWRITE4(sc, port, v | EHCI_PS_PR); 2193 /* Wait for reset to complete. */ 2194 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY); 2195 if (sc->sc_dying) { 2196 err = USBD_IOERROR; 2197 goto ret; 2198 } 2199 /* Terminate reset sequence. */ 2200 v = EOREAD4(sc, port); 2201 EOWRITE4(sc, port, v & ~EHCI_PS_PR); 2202 /* Wait for HC to complete reset. */ 2203 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE); 2204 if (sc->sc_dying) { 2205 err = USBD_IOERROR; 2206 goto ret; 2207 } 2208 v = EOREAD4(sc, port); 2209 DPRINTF(("ehci after reset, status=0x%08x\n", v)); 2210 if (v & EHCI_PS_PR) { 2211 printf("%s: port reset timeout\n", 2212 sc->sc_bus.bdev.dv_xname); 2213 return (USBD_TIMEOUT); 2214 } 2215 if (!(v & EHCI_PS_PE)) { 2216 /* Not a high speed device, give up ownership.*/ 2217 ehci_disown(sc, index, 0); 2218 break; 2219 } 2220 sc->sc_isreset = 1; 2221 DPRINTF(("ehci port %d reset, status = 0x%08x\n", 2222 index, v)); 2223 break; 2224 case UHF_PORT_POWER: 2225 DPRINTFN(2,("ehci_root_ctrl_start: " 2226 "set port power %d\n", index)); 2227 EOWRITE4(sc, port, v | EHCI_PS_PP); 2228 break; 2229 case UHF_PORT_TEST: 2230 DPRINTFN(2,("ehci_root_ctrl_start: " 2231 "set port test %d\n", index)); 2232 break; 2233 case UHF_PORT_INDICATOR: 2234 DPRINTFN(2,("ehci_root_ctrl_start: " 2235 "set port ind %d\n", index)); 2236 EOWRITE4(sc, port, v | EHCI_PS_PIC); 2237 break; 2238 default: 2239 err = USBD_IOERROR; 2240 goto ret; 2241 } 2242 break; 2243 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER): 2244 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER): 2245 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER): 2246 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER): 2247 break; 2248 default: 2249 err = USBD_IOERROR; 2250 goto ret; 2251 } 2252 xfer->actlen = totlen; 2253 err = USBD_NORMAL_COMPLETION; 2254 ret: 2255 xfer->status = err; 2256 s = splusb(); 2257 usb_transfer_complete(xfer); 2258 splx(s); 2259 return (USBD_IN_PROGRESS); 2260 } 2261 2262 void 2263 ehci_disown(ehci_softc_t *sc, int index, int lowspeed) 2264 { 2265 int port; 2266 u_int32_t v; 2267 2268 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed)); 2269 2270 port = EHCI_PORTSC(index); 2271 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR; 2272 EOWRITE4(sc, port, v | EHCI_PS_PO); 2273 } 2274 2275 /* Abort a root control request. */ 2276 void 2277 ehci_root_ctrl_abort(usbd_xfer_handle xfer) 2278 { 2279 /* Nothing to do, all transfers are synchronous. */ 2280 } 2281 2282 /* Close the root pipe. */ 2283 void 2284 ehci_root_ctrl_close(usbd_pipe_handle pipe) 2285 { 2286 DPRINTF(("ehci_root_ctrl_close\n")); 2287 /* Nothing to do. */ 2288 } 2289 2290 void 2291 ehci_root_intr_done(usbd_xfer_handle xfer) 2292 { 2293 } 2294 2295 usbd_status 2296 ehci_root_intr_transfer(usbd_xfer_handle xfer) 2297 { 2298 usbd_status err; 2299 2300 /* Insert last in queue. */ 2301 err = usb_insert_transfer(xfer); 2302 if (err) 2303 return (err); 2304 2305 /* Pipe isn't running, start first */ 2306 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2307 } 2308 2309 usbd_status 2310 ehci_root_intr_start(usbd_xfer_handle xfer) 2311 { 2312 usbd_pipe_handle pipe = xfer->pipe; 2313 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2314 2315 if (sc->sc_dying) 2316 return (USBD_IOERROR); 2317 2318 sc->sc_intrxfer = xfer; 2319 2320 return (USBD_IN_PROGRESS); 2321 } 2322 2323 /* Abort a root interrupt request. */ 2324 void 2325 ehci_root_intr_abort(usbd_xfer_handle xfer) 2326 { 2327 int s; 2328 2329 if (xfer->pipe->intrxfer == xfer) { 2330 DPRINTF(("ehci_root_intr_abort: remove\n")); 2331 xfer->pipe->intrxfer = NULL; 2332 } 2333 xfer->status = USBD_CANCELLED; 2334 s = splusb(); 2335 usb_transfer_complete(xfer); 2336 splx(s); 2337 } 2338 2339 /* Close the root pipe. */ 2340 void 2341 ehci_root_intr_close(usbd_pipe_handle pipe) 2342 { 2343 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2344 2345 DPRINTF(("ehci_root_intr_close\n")); 2346 2347 sc->sc_intrxfer = NULL; 2348 } 2349 2350 void 2351 ehci_root_ctrl_done(usbd_xfer_handle xfer) 2352 { 2353 } 2354 2355 /************************/ 2356 2357 ehci_soft_qh_t * 2358 ehci_alloc_sqh(ehci_softc_t *sc) 2359 { 2360 ehci_soft_qh_t *sqh; 2361 usbd_status err; 2362 int i, offs; 2363 usb_dma_t dma; 2364 2365 if (sc->sc_freeqhs == NULL) { 2366 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n")); 2367 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK, 2368 EHCI_PAGE_SIZE, &dma); 2369 #ifdef EHCI_DEBUG 2370 if (err) 2371 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err); 2372 #endif 2373 if (err) 2374 return (NULL); 2375 for(i = 0; i < EHCI_SQH_CHUNK; i++) { 2376 offs = i * EHCI_SQH_SIZE; 2377 sqh = KERNADDR(&dma, offs); 2378 sqh->physaddr = DMAADDR(&dma, offs); 2379 sqh->next = sc->sc_freeqhs; 2380 sc->sc_freeqhs = sqh; 2381 } 2382 } 2383 sqh = sc->sc_freeqhs; 2384 sc->sc_freeqhs = sqh->next; 2385 memset(&sqh->qh, 0, sizeof(ehci_qh_t)); 2386 sqh->next = NULL; 2387 sqh->prev = NULL; 2388 return (sqh); 2389 } 2390 2391 void 2392 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh) 2393 { 2394 sqh->next = sc->sc_freeqhs; 2395 sc->sc_freeqhs = sqh; 2396 } 2397 2398 ehci_soft_qtd_t * 2399 ehci_alloc_sqtd(ehci_softc_t *sc) 2400 { 2401 ehci_soft_qtd_t *sqtd; 2402 usbd_status err; 2403 int i, offs; 2404 usb_dma_t dma; 2405 int s; 2406 2407 if (sc->sc_freeqtds == NULL) { 2408 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n")); 2409 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK, 2410 EHCI_PAGE_SIZE, &dma); 2411 #ifdef EHCI_DEBUG 2412 if (err) 2413 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err); 2414 #endif 2415 if (err) 2416 return (NULL); 2417 s = splusb(); 2418 for(i = 0; i < EHCI_SQTD_CHUNK; i++) { 2419 offs = i * EHCI_SQTD_SIZE; 2420 sqtd = KERNADDR(&dma, offs); 2421 sqtd->physaddr = DMAADDR(&dma, offs); 2422 sqtd->nextqtd = sc->sc_freeqtds; 2423 sc->sc_freeqtds = sqtd; 2424 } 2425 splx(s); 2426 } 2427 2428 s = splusb(); 2429 sqtd = sc->sc_freeqtds; 2430 sc->sc_freeqtds = sqtd->nextqtd; 2431 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t)); 2432 sqtd->nextqtd = NULL; 2433 sqtd->xfer = NULL; 2434 splx(s); 2435 2436 return (sqtd); 2437 } 2438 2439 void 2440 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd) 2441 { 2442 int s; 2443 2444 s = splusb(); 2445 sqtd->nextqtd = sc->sc_freeqtds; 2446 sc->sc_freeqtds = sqtd; 2447 splx(s); 2448 } 2449 2450 usbd_status 2451 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc, u_int alen, 2452 int rd, usbd_xfer_handle xfer, ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep) 2453 { 2454 ehci_soft_qtd_t *next, *cur; 2455 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys; 2456 u_int32_t qtdstatus; 2457 u_int len, curlen; 2458 int mps, i, iscontrol, forceshort; 2459 usb_dma_t *dma = &xfer->dmabuf; 2460 2461 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen)); 2462 2463 len = alen; 2464 iscontrol = (epipe->pipe.endpoint->edesc->bmAttributes & UE_XFERTYPE) == 2465 UE_CONTROL; 2466 2467 dataphys = DMAADDR(dma, 0); 2468 dataphyslastpage = EHCI_PAGE(dataphys + len - 1); 2469 qtdstatus = EHCI_QTD_ACTIVE | 2470 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) | 2471 EHCI_QTD_SET_CERR(3); /* IOC and BYTES set below */ 2472 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize); 2473 forceshort = ((xfer->flags & USBD_FORCE_SHORT_XFER) || len == 0) && 2474 len % mps == 0; 2475 /* 2476 * The control transfer data stage always starts with a toggle of 1. 2477 * For other transfers we let the hardware track the toggle state. 2478 */ 2479 if (iscontrol) 2480 qtdstatus |= EHCI_QTD_SET_TOGGLE(1); 2481 2482 cur = ehci_alloc_sqtd(sc); 2483 *sp = cur; 2484 if (cur == NULL) 2485 goto nomem; 2486 for (;;) { 2487 dataphyspage = EHCI_PAGE(dataphys); 2488 /* The EHCI hardware can handle at most 5 pages. */ 2489 if (dataphyslastpage - dataphyspage < 2490 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) { 2491 /* we can handle it in this QTD */ 2492 curlen = len; 2493 } else { 2494 /* must use multiple TDs, fill as much as possible. */ 2495 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE - 2496 EHCI_PAGE_OFFSET(dataphys); 2497 #ifdef DIAGNOSTIC 2498 if (curlen > len) { 2499 printf("ehci_alloc_sqtd_chain: curlen=%u " 2500 "len=%u offs=0x%x\n", curlen, len, 2501 EHCI_PAGE_OFFSET(dataphys)); 2502 printf("lastpage=0x%x page=0x%x phys=0x%x\n", 2503 dataphyslastpage, dataphyspage, dataphys); 2504 curlen = len; 2505 } 2506 #endif 2507 /* the length must be a multiple of the max size */ 2508 curlen -= curlen % mps; 2509 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, " 2510 "curlen=%u\n", curlen)); 2511 #ifdef DIAGNOSTIC 2512 if (curlen == 0) 2513 panic("ehci_alloc_std: curlen == 0"); 2514 #endif 2515 } 2516 2517 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x " 2518 "dataphyslastpage=0x%08x len=%u curlen=%u\n", 2519 dataphys, dataphyslastpage, len, curlen)); 2520 len -= curlen; 2521 2522 /* 2523 * Allocate another transfer if there's more data left, 2524 * or if force last short transfer flag is set and we're 2525 * allocating a multiple of the max packet size. 2526 */ 2527 if (len != 0 || forceshort) { 2528 next = ehci_alloc_sqtd(sc); 2529 if (next == NULL) 2530 goto nomem; 2531 nextphys = htole32(next->physaddr); 2532 } else { 2533 next = NULL; 2534 nextphys = EHCI_NULL; 2535 } 2536 2537 for (i = 0; i * EHCI_PAGE_SIZE < 2538 curlen + EHCI_PAGE_OFFSET(dataphys); i++) { 2539 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE; 2540 if (i != 0) /* use offset only in first buffer */ 2541 a = EHCI_PAGE(a); 2542 #ifdef DIAGNOSTIC 2543 if (i >= EHCI_QTD_NBUFFERS) { 2544 printf("ehci_alloc_sqtd_chain: i=%d\n", i); 2545 goto nomem; 2546 } 2547 #endif 2548 cur->qtd.qtd_buffer[i] = htole32(a); 2549 cur->qtd.qtd_buffer_hi[i] = 0; 2550 } 2551 cur->nextqtd = next; 2552 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys; 2553 cur->qtd.qtd_status = htole32(qtdstatus | 2554 EHCI_QTD_SET_BYTES(curlen)); 2555 cur->xfer = xfer; 2556 cur->len = curlen; 2557 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n", 2558 dataphys, dataphys + curlen)); 2559 DPRINTFN(10,("ehci_alloc_sqtd_chain: curlen=%u\n", curlen)); 2560 if (iscontrol) { 2561 /* 2562 * adjust the toggle based on the number of packets 2563 * in this qtd 2564 */ 2565 if ((((curlen + mps - 1) / mps) & 1) || curlen == 0) 2566 qtdstatus ^= EHCI_QTD_TOGGLE_MASK; 2567 } 2568 if (len == 0) { 2569 if (! forceshort) 2570 break; 2571 forceshort = 0; 2572 } 2573 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n")); 2574 dataphys += curlen; 2575 cur = next; 2576 } 2577 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC); 2578 *ep = cur; 2579 2580 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n", 2581 *sp, *ep)); 2582 2583 return (USBD_NORMAL_COMPLETION); 2584 2585 nomem: 2586 /* XXX free chain */ 2587 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n")); 2588 return (USBD_NOMEM); 2589 } 2590 2591 void 2592 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd, 2593 ehci_soft_qtd_t *sqtdend) 2594 { 2595 ehci_soft_qtd_t *p; 2596 int i; 2597 2598 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n", 2599 sqtd, sqtdend)); 2600 2601 for (i = 0; sqtd != sqtdend; sqtd = p, i++) { 2602 p = sqtd->nextqtd; 2603 ehci_free_sqtd(sc, sqtd); 2604 } 2605 } 2606 2607 ehci_soft_itd_t * 2608 ehci_alloc_itd(ehci_softc_t *sc) 2609 { 2610 struct ehci_soft_itd *itd, *freeitd; 2611 usbd_status err; 2612 int i, s, offs, frindex, previndex; 2613 usb_dma_t dma; 2614 2615 s = splusb(); 2616 2617 /* Find an itd that wasn't freed this frame or last frame. This can 2618 * discard itds that were freed before frindex wrapped around 2619 * XXX - can this lead to thrashing? Could fix by enabling wrap-around 2620 * interrupt and fiddling with list when that happens */ 2621 frindex = (EOREAD4(sc, EHCI_FRINDEX) + 1) >> 3; 2622 previndex = (frindex != 0) ? frindex - 1 : sc->sc_flsize; 2623 2624 freeitd = NULL; 2625 LIST_FOREACH(itd, &sc->sc_freeitds, u.free_list) { 2626 if (itd == NULL) 2627 break; 2628 if (itd->slot != frindex && itd->slot != previndex) { 2629 freeitd = itd; 2630 break; 2631 } 2632 } 2633 2634 if (freeitd == NULL) { 2635 DPRINTFN(2, ("ehci_alloc_itd allocating chunk\n")); 2636 err = usb_allocmem(&sc->sc_bus, EHCI_ITD_SIZE * EHCI_ITD_CHUNK, 2637 EHCI_PAGE_SIZE, &dma); 2638 2639 if (err) { 2640 DPRINTF(("ehci_alloc_itd, alloc returned %d\n", err)); 2641 return (NULL); 2642 } 2643 2644 for (i = 0; i < EHCI_ITD_CHUNK; i++) { 2645 offs = i * EHCI_ITD_SIZE; 2646 itd = KERNADDR(&dma, offs); 2647 itd->physaddr = DMAADDR(&dma, offs); 2648 itd->dma = dma; 2649 itd->offs = offs; 2650 LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list); 2651 } 2652 freeitd = LIST_FIRST(&sc->sc_freeitds); 2653 } 2654 2655 itd = freeitd; 2656 LIST_REMOVE(itd, u.free_list); 2657 memset(&itd->itd, 0, sizeof(ehci_itd_t)); 2658 itd->u.frame_list.next = NULL; 2659 itd->u.frame_list.prev = NULL; 2660 itd->xfer_next = NULL; 2661 itd->slot = 0; 2662 splx(s); 2663 2664 return (itd); 2665 } 2666 2667 void 2668 ehci_free_itd(ehci_softc_t *sc, ehci_soft_itd_t *itd) 2669 { 2670 int s; 2671 2672 s = splusb(); 2673 LIST_INSERT_HEAD(&sc->sc_freeitds, itd, u.free_list); 2674 splx(s); 2675 } 2676 2677 /****************/ 2678 2679 /* 2680 * Close a reqular pipe. 2681 * Assumes that there are no pending transactions. 2682 */ 2683 void 2684 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head) 2685 { 2686 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 2687 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2688 ehci_soft_qh_t *sqh = epipe->sqh; 2689 int s; 2690 2691 s = splusb(); 2692 ehci_rem_qh(sc, sqh, head); 2693 splx(s); 2694 pipe->endpoint->savedtoggle = 2695 EHCI_QTD_GET_TOGGLE(letoh32(sqh->qh.qh_qtd.qtd_status)); 2696 ehci_free_sqh(sc, epipe->sqh); 2697 } 2698 2699 /* 2700 * Abort a device request. 2701 * If this routine is called at splusb() it guarantees that the request 2702 * will be removed from the hardware scheduling and that the callback 2703 * for it will be called with USBD_CANCELLED status. 2704 * It's impossible to guarantee that the requested transfer will not 2705 * have happened since the hardware runs concurrently. 2706 * If the transaction has already happened we rely on the ordinary 2707 * interrupt processing to process it. 2708 */ 2709 void 2710 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status) 2711 { 2712 #define exfer EXFER(xfer) 2713 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 2714 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus; 2715 ehci_soft_qh_t *sqh = epipe->sqh; 2716 ehci_soft_qtd_t *sqtd, *snext, **psqtd; 2717 ehci_physaddr_t cur, us, next; 2718 int s; 2719 int hit; 2720 ehci_soft_qh_t *psqh; 2721 2722 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe)); 2723 2724 if (sc->sc_dying) { 2725 /* If we're dying, just do the software part. */ 2726 s = splusb(); 2727 xfer->status = status; /* make software ignore it */ 2728 timeout_del(&xfer->timeout_handle); 2729 usb_rem_task(epipe->pipe.device, &exfer->abort_task); 2730 usb_transfer_complete(xfer); 2731 splx(s); 2732 return; 2733 } 2734 2735 if (xfer->device->bus->intr_context) 2736 panic("ehci_abort_xfer: not in process context"); 2737 2738 /* 2739 * If an abort is already in progress then just wait for it to 2740 * complete and return. 2741 */ 2742 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) { 2743 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n")); 2744 /* No need to wait if we're aborting from a timeout. */ 2745 if (status == USBD_TIMEOUT) 2746 return; 2747 /* Override the status which might be USBD_TIMEOUT. */ 2748 xfer->status = status; 2749 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n")); 2750 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTWAIT; 2751 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) 2752 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciaw", 0); 2753 return; 2754 } 2755 2756 /* 2757 * Step 1: Make interrupt routine and timeouts ignore xfer. 2758 */ 2759 s = splusb(); 2760 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING; 2761 xfer->status = status; /* make software ignore it */ 2762 timeout_del(&xfer->timeout_handle); 2763 usb_rem_task(epipe->pipe.device, &exfer->abort_task); 2764 splx(s); 2765 2766 /* 2767 * Step 2: Wait until we know hardware has finished any possible 2768 * use of the xfer. We do this by removing the entire 2769 * queue from the async schedule and waiting for the doorbell. 2770 * Nothing else should be touching the queue now. 2771 */ 2772 psqh = sqh->prev; 2773 ehci_rem_qh(sc, sqh, psqh); 2774 2775 /* 2776 * Step 3: Deactivate all of the qTDs that we will be removing, 2777 * otherwise the queue head may go active again. The EHCI spec 2778 * suggests we should perform the deactivation before removing the 2779 * queue head from the schedule, however the VT6202 (at least) only 2780 * behaves correctly when we deactivate them afterwards. 2781 */ 2782 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) { 2783 sqtd->qtd.qtd_status = htole32(EHCI_QTD_HALTED); 2784 if (sqtd == exfer->sqtdend) 2785 break; 2786 } 2787 ehci_sync_hc(sc); 2788 2789 /* 2790 * Step 4: make sure the soft interrupt routine 2791 * has run. This should remove any completed items off the queue. 2792 * The hardware has no reference to completed items (TDs). 2793 * It's safe to remove them at any time. 2794 * use of the xfer. Also make sure the soft interrupt routine 2795 * has run. 2796 */ 2797 s = splusb(); 2798 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 2799 sc->sc_softwake = 1; 2800 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */ 2801 usb_schedsoftintr(&sc->sc_bus); 2802 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 2803 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0); 2804 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */ 2805 2806 /* 2807 * Step 5: Remove any vestiges of the xfer from the hardware. 2808 * The complication here is that the hardware may have executed 2809 * into or even beyond the xfer we're trying to abort. 2810 * So as we're scanning the TDs of this xfer we check if 2811 * the hardware points to any of them. 2812 * 2813 * first we need to see if there are any transfers 2814 * on this queue before the xfer we are aborting.. we need 2815 * to update any pointers that point to us to point past 2816 * the aborting xfer. (If there is something past us). 2817 * Hardware and software. 2818 */ 2819 cur = EHCI_LINK_ADDR(letoh32(sqh->qh.qh_curqtd)); 2820 hit = 0; 2821 2822 /* If they initially point here. */ 2823 us = exfer->sqtdstart->physaddr; 2824 2825 /* We will change them to point here */ 2826 snext = exfer->sqtdend->nextqtd; 2827 next = snext ? snext->physaddr : EHCI_NULL; 2828 2829 /* 2830 * Now loop through any qTDs before us and keep track of the pointer 2831 * that points to us for the end. 2832 */ 2833 psqtd = &sqh->sqtd; 2834 sqtd = sqh->sqtd; 2835 while (sqtd && sqtd != exfer->sqtdstart) { 2836 hit |= (cur == sqtd->physaddr); 2837 if (EHCI_LINK_ADDR(letoh32(sqtd->qtd.qtd_next)) == us) 2838 sqtd->qtd.qtd_next = next; 2839 if (EHCI_LINK_ADDR(letoh32(sqtd->qtd.qtd_altnext)) == us) 2840 sqtd->qtd.qtd_altnext = next; 2841 psqtd = &sqtd->nextqtd; 2842 sqtd = sqtd->nextqtd; 2843 } 2844 /* make the software pointer bypass us too */ 2845 *psqtd = exfer->sqtdend->nextqtd; 2846 2847 /* 2848 * If we already saw the active one then we are pretty much done. 2849 * We've done all the relinking we need to do. 2850 */ 2851 if (!hit) { 2852 2853 /* 2854 * Now reinitialise the QH to point to the next qTD 2855 * (if there is one). We only need to do this if 2856 * it was previously pointing to us. 2857 * XXX Not quite sure what to do about the data toggle. 2858 */ 2859 sqtd = exfer->sqtdstart; 2860 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) { 2861 if (cur == sqtd->physaddr) { 2862 hit++; 2863 } 2864 if (sqtd == exfer->sqtdend) 2865 break; 2866 } 2867 /* 2868 * Only need to alter the QH if it was pointing at a qTD 2869 * that we are removing. 2870 */ 2871 if (hit) { 2872 if (snext) { 2873 ehci_set_qh_qtd(sqh, snext); 2874 } else { 2875 2876 sqh->qh.qh_curqtd = 0; /* unlink qTDs */ 2877 sqh->qh.qh_qtd.qtd_status = 0; 2878 sqh->qh.qh_qtd.qtd_next = 2879 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 2880 DPRINTFN(1,("ehci_abort_xfer: no hit\n")); 2881 } 2882 } 2883 } 2884 ehci_add_qh(sqh, psqh); 2885 2886 /* 2887 * Step 6: Execute callback. 2888 */ 2889 #ifdef DIAGNOSTIC 2890 exfer->isdone = 1; 2891 #endif 2892 /* Do the wakeup first to avoid touching the xfer after the callback. */ 2893 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTING; 2894 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTWAIT) { 2895 exfer->ehci_xfer_flags &= ~EHCI_XFER_ABORTWAIT; 2896 wakeup(&exfer->ehci_xfer_flags); 2897 } 2898 usb_transfer_complete(xfer); 2899 2900 splx(s); 2901 #undef exfer 2902 } 2903 2904 void 2905 ehci_abort_isoc_xfer(usbd_xfer_handle xfer, usbd_status status) 2906 { 2907 ehci_isoc_trans_t trans_status; 2908 struct ehci_pipe *epipe; 2909 struct ehci_xfer *exfer; 2910 ehci_softc_t *sc; 2911 struct ehci_soft_itd *itd; 2912 int s, i, wake; 2913 2914 epipe = (struct ehci_pipe *) xfer->pipe; 2915 exfer = EXFER(xfer); 2916 sc = (ehci_softc_t *)epipe->pipe.device->bus; 2917 2918 DPRINTF(("ehci_abort_isoc_xfer: xfer %p pipe %p\n", xfer, epipe)); 2919 2920 if (sc->sc_dying) { 2921 s = splusb(); 2922 xfer->status = status; 2923 timeout_del(&xfer->timeout_handle); 2924 usb_transfer_complete(xfer); 2925 splx(s); 2926 return; 2927 } 2928 2929 if (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) { 2930 DPRINTFN(2, ("ehci_abort_isoc_xfer: already aborting\n")); 2931 2932 #ifdef DIAGNOSTIC 2933 if (status == USBD_TIMEOUT) 2934 printf("ehci_abort_xfer: TIMEOUT while aborting\n"); 2935 #endif 2936 2937 xfer->status = status; 2938 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n")); 2939 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING; 2940 while (exfer->ehci_xfer_flags & EHCI_XFER_ABORTING) 2941 tsleep(&exfer->ehci_xfer_flags, PZERO, "ehciiaw", 0); 2942 return; 2943 } 2944 exfer->ehci_xfer_flags |= EHCI_XFER_ABORTING; 2945 2946 xfer->status = status; 2947 timeout_del(&xfer->timeout_handle); 2948 2949 s = splusb(); 2950 for (itd = exfer->itdstart; itd != NULL; itd = itd->xfer_next) { 2951 for (i = 0; i < 8; i++) { 2952 trans_status = letoh32(itd->itd.itd_ctl[i]); 2953 trans_status &= ~EHCI_ITD_ACTIVE; 2954 itd->itd.itd_ctl[i] = htole32(trans_status); 2955 } 2956 } 2957 splx(s); 2958 2959 s = splusb(); 2960 #ifdef USB_USE_SOFTINTR 2961 sc->sc_softwake = 1; 2962 #endif /* USB_USE_SOFTINTR */ 2963 usb_schedsoftintr(&sc->sc_bus); 2964 #ifdef USB_USE_SOFTINTR 2965 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0); 2966 #endif /* USB_USE_SOFTINTR */ 2967 splx(s); 2968 2969 #ifdef DIAGNOSTIC 2970 exfer->isdone = 1; 2971 #endif 2972 wake = exfer->ehci_xfer_flags & EHCI_XFER_ABORTING; 2973 exfer->ehci_xfer_flags &= ~(EHCI_XFER_ABORTING | EHCI_XFER_ABORTWAIT); 2974 usb_transfer_complete(xfer); 2975 if (wake) 2976 wakeup(&exfer->ehci_xfer_flags); 2977 2978 return; 2979 } 2980 2981 void 2982 ehci_timeout(void *addr) 2983 { 2984 struct ehci_xfer *exfer = addr; 2985 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe; 2986 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus; 2987 2988 DPRINTF(("ehci_timeout: exfer=%p\n", exfer)); 2989 #if defined(EHCI_DEBUG) && defined(USB_DEBUG) 2990 if (ehcidebug > 1) 2991 usbd_dump_pipe(exfer->xfer.pipe); 2992 #endif 2993 2994 if (sc->sc_dying) { 2995 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT); 2996 return; 2997 } 2998 2999 /* Execute the abort in a process context. */ 3000 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task); 3001 } 3002 3003 void 3004 ehci_timeout_task(void *addr) 3005 { 3006 usbd_xfer_handle xfer = addr; 3007 int s; 3008 3009 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer)); 3010 3011 s = splusb(); 3012 ehci_abort_xfer(xfer, USBD_TIMEOUT); 3013 splx(s); 3014 } 3015 3016 /* 3017 * Some EHCI chips from VIA seem to trigger interrupts before writing back the 3018 * qTD status, or miss signalling occasionally under heavy load. If the host 3019 * machine is too fast, we we can miss transaction completion - when we scan 3020 * the active list the transaction still seems to be active. This generally 3021 * exhibits itself as a umass stall that never recovers. 3022 * 3023 * We work around this behaviour by setting up this callback after any softintr 3024 * that completes with transactions still pending, giving us another chance to 3025 * check for completion after the writeback has taken place. 3026 */ 3027 void 3028 ehci_intrlist_timeout(void *arg) 3029 { 3030 ehci_softc_t *sc = arg; 3031 int s = splusb(); 3032 3033 DPRINTFN(1, ("ehci_intrlist_timeout\n")); 3034 usb_schedsoftintr(&sc->sc_bus); 3035 3036 splx(s); 3037 } 3038 3039 /************************/ 3040 3041 usbd_status 3042 ehci_device_ctrl_transfer(usbd_xfer_handle xfer) 3043 { 3044 usbd_status err; 3045 3046 /* Insert last in queue. */ 3047 err = usb_insert_transfer(xfer); 3048 if (err) 3049 return (err); 3050 3051 /* Pipe isn't running, start first */ 3052 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3053 } 3054 3055 usbd_status 3056 ehci_device_ctrl_start(usbd_xfer_handle xfer) 3057 { 3058 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3059 usbd_status err; 3060 3061 if (sc->sc_dying) 3062 return (USBD_IOERROR); 3063 3064 #ifdef DIAGNOSTIC 3065 if (!(xfer->rqflags & URQ_REQUEST)) { 3066 /* XXX panic */ 3067 printf("ehci_device_ctrl_transfer: not a request\n"); 3068 return (USBD_INVAL); 3069 } 3070 #endif 3071 3072 err = ehci_device_request(xfer); 3073 if (err) 3074 return (err); 3075 3076 if (sc->sc_bus.use_polling) 3077 ehci_waitintr(sc, xfer); 3078 return (USBD_IN_PROGRESS); 3079 } 3080 3081 void 3082 ehci_device_ctrl_done(usbd_xfer_handle xfer) 3083 { 3084 struct ehci_xfer *ex = EXFER(xfer); 3085 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3086 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/ 3087 3088 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer)); 3089 3090 #ifdef DIAGNOSTIC 3091 if (!(xfer->rqflags & URQ_REQUEST)) { 3092 panic("ehci_ctrl_done: not a request"); 3093 } 3094 #endif 3095 3096 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 3097 ehci_del_intr_list(sc, ex); /* remove from active list */ 3098 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3099 } 3100 3101 DPRINTFN(5, ("ehci_ctrl_done: length=%u\n", xfer->actlen)); 3102 } 3103 3104 /* Abort a device control request. */ 3105 void 3106 ehci_device_ctrl_abort(usbd_xfer_handle xfer) 3107 { 3108 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer)); 3109 ehci_abort_xfer(xfer, USBD_CANCELLED); 3110 } 3111 3112 /* Close a device control pipe. */ 3113 void 3114 ehci_device_ctrl_close(usbd_pipe_handle pipe) 3115 { 3116 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 3117 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/ 3118 3119 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe)); 3120 ehci_close_pipe(pipe, sc->sc_async_head); 3121 } 3122 3123 usbd_status 3124 ehci_device_request(usbd_xfer_handle xfer) 3125 { 3126 #define exfer EXFER(xfer) 3127 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3128 usb_device_request_t *req = &xfer->request; 3129 usbd_device_handle dev = epipe->pipe.device; 3130 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 3131 int addr = dev->address; 3132 ehci_soft_qtd_t *setup, *stat, *next; 3133 ehci_soft_qh_t *sqh; 3134 int isread; 3135 u_int len; 3136 usbd_status err; 3137 int s; 3138 3139 isread = req->bmRequestType & UT_READ; 3140 len = UGETW(req->wLength); 3141 3142 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, " 3143 "wValue=0x%04x, wIndex=0x%04x len=%u, addr=%d, endpt=%d\n", 3144 req->bmRequestType, req->bRequest, UGETW(req->wValue), 3145 UGETW(req->wIndex), len, addr, 3146 epipe->pipe.endpoint->edesc->bEndpointAddress)); 3147 3148 setup = ehci_alloc_sqtd(sc); 3149 if (setup == NULL) { 3150 err = USBD_NOMEM; 3151 goto bad1; 3152 } 3153 stat = ehci_alloc_sqtd(sc); 3154 if (stat == NULL) { 3155 err = USBD_NOMEM; 3156 goto bad2; 3157 } 3158 3159 sqh = epipe->sqh; 3160 epipe->u.ctl.length = len; 3161 3162 /* Update device address and length since they may have changed 3163 during the setup of the control pipe in usbd_new_device(). */ 3164 /* XXX This only needs to be done once, but it's too early in open. */ 3165 /* XXXX Should not touch ED here! */ 3166 sqh->qh.qh_endp = 3167 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) | 3168 htole32( 3169 EHCI_QH_SET_ADDR(addr) | 3170 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize)) 3171 ); 3172 3173 /* Set up data transaction */ 3174 if (len != 0) { 3175 ehci_soft_qtd_t *end; 3176 3177 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, 3178 &next, &end); 3179 if (err) 3180 goto bad3; 3181 end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC); 3182 end->nextqtd = stat; 3183 end->qtd.qtd_next = 3184 end->qtd.qtd_altnext = htole32(stat->physaddr); 3185 } else { 3186 next = stat; 3187 } 3188 3189 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof(*req)); 3190 3191 /* Clear toggle */ 3192 setup->qtd.qtd_status = htole32( 3193 EHCI_QTD_ACTIVE | 3194 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) | 3195 EHCI_QTD_SET_CERR(3) | 3196 EHCI_QTD_SET_TOGGLE(0) | 3197 EHCI_QTD_SET_BYTES(sizeof(*req))); 3198 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0)); 3199 setup->qtd.qtd_buffer_hi[0] = 0; 3200 setup->nextqtd = next; 3201 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr); 3202 setup->xfer = xfer; 3203 setup->len = sizeof(*req); 3204 3205 stat->qtd.qtd_status = htole32( 3206 EHCI_QTD_ACTIVE | 3207 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) | 3208 EHCI_QTD_SET_CERR(3) | 3209 EHCI_QTD_SET_TOGGLE(1) | 3210 EHCI_QTD_IOC); 3211 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */ 3212 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */ 3213 stat->nextqtd = NULL; 3214 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL; 3215 stat->xfer = xfer; 3216 stat->len = 0; 3217 3218 #ifdef EHCI_DEBUG 3219 if (ehcidebug > 5) { 3220 DPRINTF(("ehci_device_request:\n")); 3221 ehci_dump_sqh(sqh); 3222 ehci_dump_sqtds(setup); 3223 } 3224 #endif 3225 3226 exfer->sqtdstart = setup; 3227 exfer->sqtdend = stat; 3228 #ifdef DIAGNOSTIC 3229 if (!exfer->isdone) { 3230 printf("ehci_device_request: not done, exfer=%p\n", exfer); 3231 } 3232 exfer->isdone = 0; 3233 #endif 3234 3235 /* Insert qTD in QH list. */ 3236 s = splusb(); 3237 ehci_set_qh_qtd(sqh, setup); 3238 if (xfer->timeout && !sc->sc_bus.use_polling) { 3239 timeout_del(&xfer->timeout_handle); 3240 timeout_set(&xfer->timeout_handle, ehci_timeout, xfer); 3241 timeout_add_msec(&xfer->timeout_handle, xfer->timeout); 3242 } 3243 ehci_add_intr_list(sc, exfer); 3244 xfer->status = USBD_IN_PROGRESS; 3245 splx(s); 3246 3247 #ifdef EHCI_DEBUG 3248 if (ehcidebug > 10) { 3249 DPRINTF(("ehci_device_request: status=%x\n", 3250 EOREAD4(sc, EHCI_USBSTS))); 3251 delay(10000); 3252 ehci_dump_regs(sc); 3253 ehci_dump_sqh(sc->sc_async_head); 3254 ehci_dump_sqh(sqh); 3255 ehci_dump_sqtds(setup); 3256 } 3257 #endif 3258 3259 return (USBD_NORMAL_COMPLETION); 3260 3261 bad3: 3262 ehci_free_sqtd(sc, stat); 3263 bad2: 3264 ehci_free_sqtd(sc, setup); 3265 bad1: 3266 DPRINTFN(-1,("ehci_device_request: no memory\n")); 3267 xfer->status = err; 3268 usb_transfer_complete(xfer); 3269 return (err); 3270 #undef exfer 3271 } 3272 3273 /************************/ 3274 3275 usbd_status 3276 ehci_device_bulk_transfer(usbd_xfer_handle xfer) 3277 { 3278 usbd_status err; 3279 3280 /* Insert last in queue. */ 3281 err = usb_insert_transfer(xfer); 3282 if (err) 3283 return (err); 3284 3285 /* Pipe isn't running, start first */ 3286 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3287 } 3288 3289 usbd_status 3290 ehci_device_bulk_start(usbd_xfer_handle xfer) 3291 { 3292 #define exfer EXFER(xfer) 3293 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3294 usbd_device_handle dev = epipe->pipe.device; 3295 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 3296 ehci_soft_qtd_t *data, *dataend; 3297 ehci_soft_qh_t *sqh; 3298 usbd_status err; 3299 u_int len; 3300 int isread, endpt; 3301 int s; 3302 3303 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%u flags=%d\n", 3304 xfer, xfer->length, xfer->flags)); 3305 3306 if (sc->sc_dying) 3307 return (USBD_IOERROR); 3308 3309 #ifdef DIAGNOSTIC 3310 if (xfer->rqflags & URQ_REQUEST) 3311 panic("ehci_device_bulk_start: a request"); 3312 #endif 3313 3314 len = xfer->length; 3315 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 3316 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3317 sqh = epipe->sqh; 3318 3319 epipe->u.bulk.length = len; 3320 3321 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data, 3322 &dataend); 3323 if (err) { 3324 DPRINTFN(-1,("ehci_device_bulk_start: no memory\n")); 3325 xfer->status = err; 3326 usb_transfer_complete(xfer); 3327 return (err); 3328 } 3329 3330 #ifdef EHCI_DEBUG 3331 if (ehcidebug > 5) { 3332 DPRINTF(("ehci_device_bulk_start: data(1)\n")); 3333 ehci_dump_sqh(sqh); 3334 ehci_dump_sqtds(data); 3335 } 3336 #endif 3337 3338 /* Set up interrupt info. */ 3339 exfer->sqtdstart = data; 3340 exfer->sqtdend = dataend; 3341 #ifdef DIAGNOSTIC 3342 if (!exfer->isdone) { 3343 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer); 3344 } 3345 exfer->isdone = 0; 3346 #endif 3347 3348 s = splusb(); 3349 ehci_set_qh_qtd(sqh, data); 3350 if (xfer->timeout && !sc->sc_bus.use_polling) { 3351 timeout_del(&xfer->timeout_handle); 3352 timeout_set(&xfer->timeout_handle, ehci_timeout, xfer); 3353 timeout_add_msec(&xfer->timeout_handle, xfer->timeout); 3354 } 3355 ehci_add_intr_list(sc, exfer); 3356 xfer->status = USBD_IN_PROGRESS; 3357 splx(s); 3358 3359 #ifdef EHCI_DEBUG 3360 if (ehcidebug > 10) { 3361 DPRINTF(("ehci_device_bulk_start: data(2)\n")); 3362 delay(10000); 3363 DPRINTF(("ehci_device_bulk_start: data(3)\n")); 3364 ehci_dump_regs(sc); 3365 #if 0 3366 printf("async_head:\n"); 3367 ehci_dump_sqh(sc->sc_async_head); 3368 #endif 3369 printf("sqh:\n"); 3370 ehci_dump_sqh(sqh); 3371 ehci_dump_sqtds(data); 3372 } 3373 #endif 3374 3375 if (sc->sc_bus.use_polling) 3376 ehci_waitintr(sc, xfer); 3377 3378 return (USBD_IN_PROGRESS); 3379 #undef exfer 3380 } 3381 3382 void 3383 ehci_device_bulk_abort(usbd_xfer_handle xfer) 3384 { 3385 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer)); 3386 ehci_abort_xfer(xfer, USBD_CANCELLED); 3387 } 3388 3389 /* 3390 * Close a device bulk pipe. 3391 */ 3392 void 3393 ehci_device_bulk_close(usbd_pipe_handle pipe) 3394 { 3395 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 3396 3397 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe)); 3398 ehci_close_pipe(pipe, sc->sc_async_head); 3399 } 3400 3401 void 3402 ehci_device_bulk_done(usbd_xfer_handle xfer) 3403 { 3404 struct ehci_xfer *ex = EXFER(xfer); 3405 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3406 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/ 3407 3408 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n", 3409 xfer, xfer->actlen)); 3410 3411 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 3412 ehci_del_intr_list(sc, ex); /* remove from active list */ 3413 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3414 } 3415 3416 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen)); 3417 } 3418 3419 /************************/ 3420 3421 usbd_status 3422 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival) 3423 { 3424 struct ehci_soft_islot *isp; 3425 int islot, lev; 3426 3427 /* Find a poll rate that is large enough. */ 3428 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--) 3429 if (EHCI_ILEV_IVAL(lev) <= ival) 3430 break; 3431 3432 /* Pick an interrupt slot at the right level. */ 3433 /* XXX could do better than picking at random */ 3434 if (cold) { 3435 /* XXX prevent panics at boot by not using arc4random */ 3436 sc->sc_rand = (sc->sc_rand + 192) % sc->sc_flsize; 3437 islot = EHCI_IQHIDX(lev, sc->sc_rand); 3438 } else 3439 islot = EHCI_IQHIDX(lev, arc4random()); 3440 3441 sqh->islot = islot; 3442 isp = &sc->sc_islots[islot]; 3443 ehci_add_qh(sqh, isp->sqh); 3444 3445 return (USBD_NORMAL_COMPLETION); 3446 } 3447 3448 usbd_status 3449 ehci_device_intr_transfer(usbd_xfer_handle xfer) 3450 { 3451 usbd_status err; 3452 3453 /* Insert last in queue. */ 3454 err = usb_insert_transfer(xfer); 3455 if (err) 3456 return (err); 3457 3458 /* 3459 * Pipe isn't running (otherwise err would be USBD_INPROG), 3460 * so start it first. 3461 */ 3462 return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3463 } 3464 3465 usbd_status 3466 ehci_device_intr_start(usbd_xfer_handle xfer) 3467 { 3468 #define exfer EXFER(xfer) 3469 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3470 usbd_device_handle dev = xfer->pipe->device; 3471 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 3472 ehci_soft_qtd_t *data, *dataend; 3473 ehci_soft_qh_t *sqh; 3474 usbd_status err; 3475 u_int len; 3476 int isread, endpt; 3477 int s; 3478 3479 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%u flags=%d\n", 3480 xfer, xfer->length, xfer->flags)); 3481 3482 if (sc->sc_dying) 3483 return (USBD_IOERROR); 3484 3485 #ifdef DIAGNOSTIC 3486 if (xfer->rqflags & URQ_REQUEST) 3487 panic("ehci_device_intr_start: a request"); 3488 #endif 3489 3490 len = xfer->length; 3491 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 3492 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3493 sqh = epipe->sqh; 3494 3495 epipe->u.intr.length = len; 3496 3497 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data, 3498 &dataend); 3499 if (err) { 3500 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n")); 3501 xfer->status = err; 3502 usb_transfer_complete(xfer); 3503 return (err); 3504 } 3505 3506 #ifdef EHCI_DEBUG 3507 if (ehcidebug > 5) { 3508 DPRINTF(("ehci_device_intr_start: data(1)\n")); 3509 ehci_dump_sqh(sqh); 3510 ehci_dump_sqtds(data); 3511 } 3512 #endif 3513 3514 /* Set up interrupt info. */ 3515 exfer->sqtdstart = data; 3516 exfer->sqtdend = dataend; 3517 #ifdef DIAGNOSTIC 3518 if (!exfer->isdone) 3519 printf("ehci_device_intr_start: not done, ex=%p\n", exfer); 3520 exfer->isdone = 0; 3521 #endif 3522 3523 s = splusb(); 3524 ehci_set_qh_qtd(sqh, data); 3525 if (xfer->timeout && !sc->sc_bus.use_polling) { 3526 timeout_del(&xfer->timeout_handle); 3527 timeout_set(&xfer->timeout_handle, ehci_timeout, xfer); 3528 timeout_add_msec(&xfer->timeout_handle, xfer->timeout); 3529 } 3530 ehci_add_intr_list(sc, exfer); 3531 xfer->status = USBD_IN_PROGRESS; 3532 splx(s); 3533 3534 #ifdef EHCI_DEBUG 3535 if (ehcidebug > 10) { 3536 DPRINTF(("ehci_device_intr_start: data(2)\n")); 3537 delay(10000); 3538 DPRINTF(("ehci_device_intr_start: data(3)\n")); 3539 ehci_dump_regs(sc); 3540 printf("sqh:\n"); 3541 ehci_dump_sqh(sqh); 3542 ehci_dump_sqtds(data); 3543 } 3544 #endif 3545 3546 if (sc->sc_bus.use_polling) 3547 ehci_waitintr(sc, xfer); 3548 3549 return (USBD_IN_PROGRESS); 3550 #undef exfer 3551 } 3552 3553 void 3554 ehci_device_intr_abort(usbd_xfer_handle xfer) 3555 { 3556 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer)); 3557 if (xfer->pipe->intrxfer == xfer) { 3558 DPRINTFN(1, ("ehci_device_intr_abort: remove\n")); 3559 xfer->pipe->intrxfer = NULL; 3560 } 3561 /* 3562 * XXX - abort_xfer uses ehci_sync_hc, which syncs via the advance 3563 * async doorbell. That's dependant on the async list, wheras 3564 * intr xfers are periodic, should not use this? 3565 */ 3566 ehci_abort_xfer(xfer, USBD_CANCELLED); 3567 } 3568 3569 void 3570 ehci_device_intr_close(usbd_pipe_handle pipe) 3571 { 3572 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 3573 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 3574 struct ehci_soft_islot *isp; 3575 3576 isp = &sc->sc_islots[epipe->sqh->islot]; 3577 ehci_close_pipe(pipe, isp->sqh); 3578 } 3579 3580 void 3581 ehci_device_intr_done(usbd_xfer_handle xfer) 3582 { 3583 #define exfer EXFER(xfer) 3584 struct ehci_xfer *ex = EXFER(xfer); 3585 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3586 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3587 ehci_soft_qtd_t *data, *dataend; 3588 ehci_soft_qh_t *sqh; 3589 usbd_status err; 3590 u_int len; 3591 int isread, endpt, s; 3592 3593 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n", 3594 xfer, xfer->actlen)); 3595 3596 if (xfer->pipe->repeat) { 3597 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3598 3599 len = epipe->u.intr.length; 3600 xfer->length = len; 3601 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 3602 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3603 sqh = epipe->sqh; 3604 3605 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, 3606 &data, &dataend); 3607 if (err) { 3608 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n")); 3609 xfer->status = err; 3610 return; 3611 } 3612 3613 /* Set up interrupt info. */ 3614 exfer->sqtdstart = data; 3615 exfer->sqtdend = dataend; 3616 #ifdef DIAGNOSTIC 3617 if (!exfer->isdone) { 3618 printf("ehci_device_intr_done: not done, ex=%p\n", 3619 exfer); 3620 } 3621 exfer->isdone = 0; 3622 #endif 3623 3624 s = splusb(); 3625 ehci_set_qh_qtd(sqh, data); 3626 if (xfer->timeout && !sc->sc_bus.use_polling) { 3627 timeout_del(&xfer->timeout_handle); 3628 timeout_set(&xfer->timeout_handle, ehci_timeout, xfer); 3629 timeout_add_msec(&xfer->timeout_handle, xfer->timeout); 3630 } 3631 splx(s); 3632 3633 xfer->status = USBD_IN_PROGRESS; 3634 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 3635 ehci_del_intr_list(sc, ex); /* remove from active list */ 3636 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3637 } 3638 #undef exfer 3639 } 3640 3641 /************************/ 3642 3643 usbd_status 3644 ehci_device_isoc_transfer(usbd_xfer_handle xfer) 3645 { 3646 usbd_status err; 3647 3648 err = usb_insert_transfer(xfer); 3649 if (err && err != USBD_IN_PROGRESS) 3650 return (err); 3651 3652 return (ehci_device_isoc_start(xfer)); 3653 } 3654 3655 usbd_status 3656 ehci_device_isoc_start(usbd_xfer_handle xfer) 3657 { 3658 struct ehci_pipe *epipe; 3659 ehci_softc_t *sc; 3660 struct ehci_xfer *exfer; 3661 ehci_soft_itd_t *itd, *prev, *start, *stop; 3662 usb_dma_t *dma_buf; 3663 int i, j, k, frames, uframes, ufrperframe; 3664 int s, trans_count, offs, total_length; 3665 int frindex; 3666 3667 start = NULL; 3668 prev = NULL; 3669 itd = NULL; 3670 trans_count = 0; 3671 total_length = 0; 3672 exfer = (struct ehci_xfer *) xfer; 3673 sc = (ehci_softc_t *)xfer->pipe->device->bus; 3674 epipe = (struct ehci_pipe *)xfer->pipe; 3675 3676 /* 3677 * To allow continuous transfers, above we start all transfers 3678 * immediately. However, we're still going to get usbd_start_next call 3679 * this when another xfer completes. So, check if this is already 3680 * in progress or not 3681 */ 3682 3683 if (exfer->itdstart != NULL) 3684 return (USBD_IN_PROGRESS); 3685 3686 DPRINTFN(2, ("ehci_device_isoc_start: xfer %p len %u flags %d\n", 3687 xfer, xfer->length, xfer->flags)); 3688 3689 if (sc->sc_dying) 3690 return (USBD_IOERROR); 3691 3692 /* 3693 * To avoid complication, don't allow a request right now that'll span 3694 * the entire frame table. To within 4 frames, to allow some leeway 3695 * on either side of where the hc currently is. 3696 */ 3697 if ((1 << (epipe->pipe.endpoint->edesc->bInterval)) * 3698 xfer->nframes >= (sc->sc_flsize - 4) * 8) { 3699 printf("ehci: isoc descriptor requested that spans the entire " 3700 "frametable, too many frames\n"); 3701 return (USBD_INVAL); 3702 } 3703 3704 #ifdef DIAGNOSTIC 3705 if (xfer->rqflags & URQ_REQUEST) 3706 panic("ehci_device_isoc_start: request"); 3707 3708 if (!exfer->isdone) 3709 printf("ehci_device_isoc_start: not done, ex = %p\n", exfer); 3710 exfer->isdone = 0; 3711 #endif 3712 3713 /* 3714 * Step 1: Allocate and initialize itds, how many do we need? 3715 * One per transfer if interval >= 8 microframes, fewer if we use 3716 * multiple microframes per frame. 3717 */ 3718 3719 i = epipe->pipe.endpoint->edesc->bInterval; 3720 if (i > 16 || i == 0) { 3721 /* Spec page 271 says intervals > 16 are invalid */ 3722 DPRINTF(("ehci_device_isoc_start: bInvertal %d invalid\n", i)); 3723 return (USBD_INVAL); 3724 } 3725 3726 switch (i) { 3727 case 1: 3728 ufrperframe = 8; 3729 break; 3730 case 2: 3731 ufrperframe = 4; 3732 break; 3733 case 3: 3734 ufrperframe = 2; 3735 break; 3736 default: 3737 ufrperframe = 1; 3738 break; 3739 } 3740 frames = (xfer->nframes + (ufrperframe - 1)) / ufrperframe; 3741 uframes = 8 / ufrperframe; 3742 3743 if (frames == 0) { 3744 DPRINTF(("ehci_device_isoc_start: frames == 0\n")); 3745 return (USBD_INVAL); 3746 } 3747 3748 dma_buf = &xfer->dmabuf; 3749 offs = 0; 3750 3751 for (i = 0; i < frames; i++) { 3752 int froffs = offs; 3753 itd = ehci_alloc_itd(sc); 3754 3755 if (prev != NULL) { 3756 prev->itd.itd_next = 3757 htole32(itd->physaddr | EHCI_LINK_ITD); 3758 prev->xfer_next = itd; 3759 } else { 3760 start = itd; 3761 } 3762 3763 /* 3764 * Step 1.5, initialize uframes 3765 */ 3766 for (j = 0; j < 8; j += uframes) { 3767 /* Calculate which page in the list this starts in */ 3768 int addr = DMAADDR(dma_buf, froffs); 3769 addr = EHCI_PAGE_OFFSET(addr); 3770 addr += (offs - froffs); 3771 addr = EHCI_PAGE(addr); 3772 addr /= EHCI_PAGE_SIZE; 3773 3774 /* This gets the initial offset into the first page, 3775 * looks how far further along the current uframe 3776 * offset is. Works out how many pages that is. 3777 */ 3778 3779 itd->itd.itd_ctl[j] = htole32 ( EHCI_ITD_ACTIVE | 3780 EHCI_ITD_SET_LEN(xfer->frlengths[trans_count]) | 3781 EHCI_ITD_SET_PG(addr) | 3782 EHCI_ITD_SET_OFFS(EHCI_PAGE_OFFSET(DMAADDR(dma_buf, 3783 offs)))); 3784 3785 total_length += xfer->frlengths[trans_count]; 3786 offs += xfer->frlengths[trans_count]; 3787 trans_count++; 3788 3789 if (trans_count >= xfer->nframes) { /*Set IOC*/ 3790 itd->itd.itd_ctl[j] |= htole32(EHCI_ITD_IOC); 3791 break; 3792 } 3793 } 3794 3795 /* Step 1.75, set buffer pointers. To simplify matters, all 3796 * pointers are filled out for the next 7 hardware pages in 3797 * the dma block, so no need to worry what pages to cover 3798 * and what to not. 3799 */ 3800 3801 for (j=0; j < 7; j++) { 3802 /* 3803 * Don't try to lookup a page that's past the end 3804 * of buffer 3805 */ 3806 int page_offs = EHCI_PAGE(froffs + 3807 (EHCI_PAGE_SIZE * j)); 3808 3809 if (page_offs >= dma_buf->block->size) 3810 break; 3811 3812 long long page = DMAADDR(dma_buf, page_offs); 3813 page = EHCI_PAGE(page); 3814 itd->itd.itd_bufr[j] = 3815 htole32(EHCI_ITD_SET_BPTR(page)); 3816 itd->itd.itd_bufr_hi[j] = 3817 htole32(page >> 32); 3818 } 3819 3820 /* 3821 * Other special values 3822 */ 3823 3824 k = epipe->pipe.endpoint->edesc->bEndpointAddress; 3825 itd->itd.itd_bufr[0] |= 3826 htole32(EHCI_ITD_SET_EP(UE_GET_ADDR(k)) | 3827 EHCI_ITD_SET_DADDR(epipe->pipe.device->address)); 3828 3829 k = (UE_GET_DIR(epipe->pipe.endpoint->edesc->bEndpointAddress)) 3830 ? 1 : 0; 3831 j = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize); 3832 itd->itd.itd_bufr[1] |= htole32(EHCI_ITD_SET_DIR(k) | 3833 EHCI_ITD_SET_MAXPKT(UE_GET_SIZE(j))); 3834 3835 /* FIXME: handle invalid trans */ 3836 itd->itd.itd_bufr[2] |= 3837 htole32(EHCI_ITD_SET_MULTI(UE_GET_TRANS(j)+1)); 3838 prev = itd; 3839 } /* End of frame */ 3840 3841 stop = itd; 3842 stop->xfer_next = NULL; 3843 exfer->isoc_len = total_length; 3844 3845 /* 3846 * Part 2: Transfer descriptors have now been set up, now they must 3847 * be scheduled into the period frame list. Erk. Not wanting to 3848 * complicate matters, transfer is denied if the transfer spans 3849 * more than the period frame list. 3850 */ 3851 3852 s = splusb(); 3853 3854 /* Start inserting frames */ 3855 if (epipe->u.isoc.cur_xfers > 0) { 3856 frindex = epipe->u.isoc.next_frame; 3857 } else { 3858 frindex = EOREAD4(sc, EHCI_FRINDEX); 3859 frindex = frindex >> 3; /* Erase microframe index */ 3860 frindex += 2; 3861 } 3862 3863 if (frindex >= sc->sc_flsize) 3864 frindex &= (sc->sc_flsize - 1); 3865 3866 /* Whats the frame interval? */ 3867 i = (1 << epipe->pipe.endpoint->edesc->bInterval); 3868 if (i / 8 == 0) 3869 i = 1; 3870 else 3871 i /= 8; 3872 3873 itd = start; 3874 for (j = 0; j < frames; j++) { 3875 if (itd == NULL) 3876 panic("ehci: unexpectedly ran out of isoc itds, " 3877 "isoc_start"); 3878 3879 itd->itd.itd_next = sc->sc_flist[frindex]; 3880 if (itd->itd.itd_next == 0) 3881 /* FIXME: frindex table gets initialized to NULL 3882 * or EHCI_NULL? */ 3883 itd->itd.itd_next = htole32(EHCI_NULL); 3884 3885 sc->sc_flist[frindex] = htole32(EHCI_LINK_ITD | itd->physaddr); 3886 itd->u.frame_list.next = sc->sc_softitds[frindex]; 3887 sc->sc_softitds[frindex] = itd; 3888 if (itd->u.frame_list.next != NULL) 3889 itd->u.frame_list.next->u.frame_list.prev = itd; 3890 itd->slot = frindex; 3891 itd->u.frame_list.prev = NULL; 3892 3893 frindex += i; 3894 if (frindex >= sc->sc_flsize) 3895 frindex -= sc->sc_flsize; 3896 3897 itd = itd->xfer_next; 3898 } 3899 3900 epipe->u.isoc.cur_xfers++; 3901 epipe->u.isoc.next_frame = frindex; 3902 3903 exfer->itdstart = start; 3904 exfer->itdend = stop; 3905 exfer->sqtdstart = NULL; 3906 exfer->sqtdstart = NULL; 3907 3908 ehci_add_intr_list(sc, exfer); 3909 xfer->status = USBD_IN_PROGRESS; 3910 xfer->done = 0; 3911 splx(s); 3912 3913 if (sc->sc_bus.use_polling) { 3914 printf("Starting ehci isoc xfer with polling. Bad idea?\n"); 3915 ehci_waitintr(sc, xfer); 3916 } 3917 3918 return (USBD_IN_PROGRESS); 3919 } 3920 3921 void 3922 ehci_device_isoc_abort(usbd_xfer_handle xfer) 3923 { 3924 DPRINTFN(1, ("ehci_device_isoc_abort: xfer = %p\n", xfer)); 3925 ehci_abort_isoc_xfer(xfer, USBD_CANCELLED); 3926 } 3927 3928 void 3929 ehci_device_isoc_close(usbd_pipe_handle pipe) 3930 { 3931 DPRINTFN(1, ("ehci_device_isoc_close: nothing in the pipe to free?\n")); 3932 } 3933 3934 void 3935 ehci_device_isoc_done(usbd_xfer_handle xfer) 3936 { 3937 struct ehci_xfer *exfer; 3938 ehci_softc_t *sc; 3939 struct ehci_pipe *epipe; 3940 int s; 3941 3942 exfer = EXFER(xfer); 3943 sc = (ehci_softc_t *)xfer->pipe->device->bus; 3944 epipe = (struct ehci_pipe *) xfer->pipe; 3945 3946 s = splusb(); 3947 epipe->u.isoc.cur_xfers--; 3948 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(exfer)) { 3949 ehci_del_intr_list(sc, exfer); 3950 ehci_rem_free_itd_chain(sc, exfer); 3951 } 3952 splx(s); 3953 } 3954