1 /* $NetBSD: ehci.c,v 1.108 2006/01/17 12:30:00 xtraeme Exp $ */ 2 3 /* 4 * Copyright (c) 2004,2005 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) and by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * USB Enhanced Host Controller Driver, a.k.a. USB 2.0 controller. 41 * 42 * The EHCI 1.0 spec can be found at 43 * http://developer.intel.com/technology/usb/download/ehci-r10.pdf 44 * and the USB 2.0 spec at 45 * http://www.usb.org/developers/docs/usb_20.zip 46 * 47 */ 48 49 /* 50 * TODO: 51 * 1) hold off explorations by companion controllers until ehci has started. 52 * 53 * 2) The EHCI driver lacks support for isochronous transfers, so 54 * devices using them don't work. 55 * 56 * 3) The hub driver needs to handle and schedule the transaction translator, 57 * to assign place in frame where different devices get to go. See chapter 58 * on hubs in USB 2.0 for details. 59 * 60 * 4) command failures are not recovered correctly 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: ehci.c,v 1.108 2006/01/17 12:30:00 xtraeme Exp $"); 65 66 #include "ohci.h" 67 #include "uhci.h" 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/malloc.h> 73 #include <sys/device.h> 74 #include <sys/select.h> 75 #include <sys/proc.h> 76 #include <sys/queue.h> 77 78 #include <machine/bus.h> 79 #include <machine/endian.h> 80 81 #include <dev/usb/usb.h> 82 #include <dev/usb/usbdi.h> 83 #include <dev/usb/usbdivar.h> 84 #include <dev/usb/usb_mem.h> 85 #include <dev/usb/usb_quirks.h> 86 87 #include <dev/usb/ehcireg.h> 88 #include <dev/usb/ehcivar.h> 89 90 #ifdef EHCI_DEBUG 91 #define DPRINTF(x) do { if (ehcidebug) printf x; } while(0) 92 #define DPRINTFN(n,x) do { if (ehcidebug>(n)) printf x; } while (0) 93 int ehcidebug = 0; 94 #ifndef __NetBSD__ 95 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f)) 96 #endif 97 #else 98 #define DPRINTF(x) 99 #define DPRINTFN(n,x) 100 #endif 101 102 struct ehci_pipe { 103 struct usbd_pipe pipe; 104 int nexttoggle; 105 106 ehci_soft_qh_t *sqh; 107 union { 108 ehci_soft_qtd_t *qtd; 109 /* ehci_soft_itd_t *itd; */ 110 } tail; 111 union { 112 /* Control pipe */ 113 struct { 114 usb_dma_t reqdma; 115 u_int length; 116 } ctl; 117 /* Interrupt pipe */ 118 struct { 119 u_int length; 120 } intr; 121 /* Bulk pipe */ 122 struct { 123 u_int length; 124 } bulk; 125 /* Iso pipe */ 126 /* XXX */ 127 } u; 128 }; 129 130 Static void ehci_shutdown(void *); 131 Static void ehci_power(int, void *); 132 133 Static usbd_status ehci_open(usbd_pipe_handle); 134 Static void ehci_poll(struct usbd_bus *); 135 Static void ehci_softintr(void *); 136 Static int ehci_intr1(ehci_softc_t *); 137 Static void ehci_waitintr(ehci_softc_t *, usbd_xfer_handle); 138 Static void ehci_check_intr(ehci_softc_t *, struct ehci_xfer *); 139 Static void ehci_idone(struct ehci_xfer *); 140 Static void ehci_timeout(void *); 141 Static void ehci_timeout_task(void *); 142 Static void ehci_intrlist_timeout(void *); 143 144 Static usbd_status ehci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); 145 Static void ehci_freem(struct usbd_bus *, usb_dma_t *); 146 147 Static usbd_xfer_handle ehci_allocx(struct usbd_bus *); 148 Static void ehci_freex(struct usbd_bus *, usbd_xfer_handle); 149 150 Static usbd_status ehci_root_ctrl_transfer(usbd_xfer_handle); 151 Static usbd_status ehci_root_ctrl_start(usbd_xfer_handle); 152 Static void ehci_root_ctrl_abort(usbd_xfer_handle); 153 Static void ehci_root_ctrl_close(usbd_pipe_handle); 154 Static void ehci_root_ctrl_done(usbd_xfer_handle); 155 156 Static usbd_status ehci_root_intr_transfer(usbd_xfer_handle); 157 Static usbd_status ehci_root_intr_start(usbd_xfer_handle); 158 Static void ehci_root_intr_abort(usbd_xfer_handle); 159 Static void ehci_root_intr_close(usbd_pipe_handle); 160 Static void ehci_root_intr_done(usbd_xfer_handle); 161 162 Static usbd_status ehci_device_ctrl_transfer(usbd_xfer_handle); 163 Static usbd_status ehci_device_ctrl_start(usbd_xfer_handle); 164 Static void ehci_device_ctrl_abort(usbd_xfer_handle); 165 Static void ehci_device_ctrl_close(usbd_pipe_handle); 166 Static void ehci_device_ctrl_done(usbd_xfer_handle); 167 168 Static usbd_status ehci_device_bulk_transfer(usbd_xfer_handle); 169 Static usbd_status ehci_device_bulk_start(usbd_xfer_handle); 170 Static void ehci_device_bulk_abort(usbd_xfer_handle); 171 Static void ehci_device_bulk_close(usbd_pipe_handle); 172 Static void ehci_device_bulk_done(usbd_xfer_handle); 173 174 Static usbd_status ehci_device_intr_transfer(usbd_xfer_handle); 175 Static usbd_status ehci_device_intr_start(usbd_xfer_handle); 176 Static void ehci_device_intr_abort(usbd_xfer_handle); 177 Static void ehci_device_intr_close(usbd_pipe_handle); 178 Static void ehci_device_intr_done(usbd_xfer_handle); 179 180 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle); 181 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle); 182 Static void ehci_device_isoc_abort(usbd_xfer_handle); 183 Static void ehci_device_isoc_close(usbd_pipe_handle); 184 Static void ehci_device_isoc_done(usbd_xfer_handle); 185 186 Static void ehci_device_clear_toggle(usbd_pipe_handle pipe); 187 Static void ehci_noop(usbd_pipe_handle pipe); 188 189 Static int ehci_str(usb_string_descriptor_t *, int, const char *); 190 Static void ehci_pcd(ehci_softc_t *, usbd_xfer_handle); 191 Static void ehci_pcd_able(ehci_softc_t *, int); 192 Static void ehci_pcd_enable(void *); 193 Static void ehci_disown(ehci_softc_t *, int, int); 194 195 Static ehci_soft_qh_t *ehci_alloc_sqh(ehci_softc_t *); 196 Static void ehci_free_sqh(ehci_softc_t *, ehci_soft_qh_t *); 197 198 Static ehci_soft_qtd_t *ehci_alloc_sqtd(ehci_softc_t *); 199 Static void ehci_free_sqtd(ehci_softc_t *, ehci_soft_qtd_t *); 200 Static usbd_status ehci_alloc_sqtd_chain(struct ehci_pipe *, 201 ehci_softc_t *, int, int, usbd_xfer_handle, 202 ehci_soft_qtd_t **, ehci_soft_qtd_t **); 203 Static void ehci_free_sqtd_chain(ehci_softc_t *, ehci_soft_qtd_t *, 204 ehci_soft_qtd_t *); 205 206 Static usbd_status ehci_device_request(usbd_xfer_handle xfer); 207 208 Static usbd_status ehci_device_setintr(ehci_softc_t *, ehci_soft_qh_t *, 209 int ival); 210 211 Static void ehci_add_qh(ehci_soft_qh_t *, ehci_soft_qh_t *); 212 Static void ehci_rem_qh(ehci_softc_t *, ehci_soft_qh_t *, 213 ehci_soft_qh_t *); 214 Static void ehci_set_qh_qtd(ehci_soft_qh_t *, ehci_soft_qtd_t *); 215 Static void ehci_sync_hc(ehci_softc_t *); 216 217 Static void ehci_close_pipe(usbd_pipe_handle, ehci_soft_qh_t *); 218 Static void ehci_abort_xfer(usbd_xfer_handle, usbd_status); 219 220 #ifdef EHCI_DEBUG 221 Static void ehci_dump_regs(ehci_softc_t *); 222 void ehci_dump(void); 223 Static ehci_softc_t *theehci; 224 Static void ehci_dump_link(ehci_link_t, int); 225 Static void ehci_dump_sqtds(ehci_soft_qtd_t *); 226 Static void ehci_dump_sqtd(ehci_soft_qtd_t *); 227 Static void ehci_dump_qtd(ehci_qtd_t *); 228 Static void ehci_dump_sqh(ehci_soft_qh_t *); 229 #ifdef DIAGNOSTIC 230 Static void ehci_dump_exfer(struct ehci_xfer *); 231 #endif 232 #endif 233 234 #define EHCI_NULL htole32(EHCI_LINK_TERMINATE) 235 236 #define EHCI_INTR_ENDPT 1 237 238 #define ehci_add_intr_list(sc, ex) \ 239 LIST_INSERT_HEAD(&(sc)->sc_intrhead, (ex), inext); 240 #define ehci_del_intr_list(ex) \ 241 do { \ 242 LIST_REMOVE((ex), inext); \ 243 (ex)->inext.le_prev = NULL; \ 244 } while (0) 245 #define ehci_active_intr_list(ex) ((ex)->inext.le_prev != NULL) 246 247 Static struct usbd_bus_methods ehci_bus_methods = { 248 ehci_open, 249 ehci_softintr, 250 ehci_poll, 251 ehci_allocm, 252 ehci_freem, 253 ehci_allocx, 254 ehci_freex, 255 }; 256 257 Static struct usbd_pipe_methods ehci_root_ctrl_methods = { 258 ehci_root_ctrl_transfer, 259 ehci_root_ctrl_start, 260 ehci_root_ctrl_abort, 261 ehci_root_ctrl_close, 262 ehci_noop, 263 ehci_root_ctrl_done, 264 }; 265 266 Static struct usbd_pipe_methods ehci_root_intr_methods = { 267 ehci_root_intr_transfer, 268 ehci_root_intr_start, 269 ehci_root_intr_abort, 270 ehci_root_intr_close, 271 ehci_noop, 272 ehci_root_intr_done, 273 }; 274 275 Static struct usbd_pipe_methods ehci_device_ctrl_methods = { 276 ehci_device_ctrl_transfer, 277 ehci_device_ctrl_start, 278 ehci_device_ctrl_abort, 279 ehci_device_ctrl_close, 280 ehci_noop, 281 ehci_device_ctrl_done, 282 }; 283 284 Static struct usbd_pipe_methods ehci_device_intr_methods = { 285 ehci_device_intr_transfer, 286 ehci_device_intr_start, 287 ehci_device_intr_abort, 288 ehci_device_intr_close, 289 ehci_device_clear_toggle, 290 ehci_device_intr_done, 291 }; 292 293 Static struct usbd_pipe_methods ehci_device_bulk_methods = { 294 ehci_device_bulk_transfer, 295 ehci_device_bulk_start, 296 ehci_device_bulk_abort, 297 ehci_device_bulk_close, 298 ehci_device_clear_toggle, 299 ehci_device_bulk_done, 300 }; 301 302 Static struct usbd_pipe_methods ehci_device_isoc_methods = { 303 ehci_device_isoc_transfer, 304 ehci_device_isoc_start, 305 ehci_device_isoc_abort, 306 ehci_device_isoc_close, 307 ehci_noop, 308 ehci_device_isoc_done, 309 }; 310 311 static uint8_t revbits[EHCI_MAX_POLLRATE] = { 312 0x00,0x40,0x20,0x60,0x10,0x50,0x30,0x70,0x08,0x48,0x28,0x68,0x18,0x58,0x38,0x78, 313 0x04,0x44,0x24,0x64,0x14,0x54,0x34,0x74,0x0c,0x4c,0x2c,0x6c,0x1c,0x5c,0x3c,0x7c, 314 0x02,0x42,0x22,0x62,0x12,0x52,0x32,0x72,0x0a,0x4a,0x2a,0x6a,0x1a,0x5a,0x3a,0x7a, 315 0x06,0x46,0x26,0x66,0x16,0x56,0x36,0x76,0x0e,0x4e,0x2e,0x6e,0x1e,0x5e,0x3e,0x7e, 316 0x01,0x41,0x21,0x61,0x11,0x51,0x31,0x71,0x09,0x49,0x29,0x69,0x19,0x59,0x39,0x79, 317 0x05,0x45,0x25,0x65,0x15,0x55,0x35,0x75,0x0d,0x4d,0x2d,0x6d,0x1d,0x5d,0x3d,0x7d, 318 0x03,0x43,0x23,0x63,0x13,0x53,0x33,0x73,0x0b,0x4b,0x2b,0x6b,0x1b,0x5b,0x3b,0x7b, 319 0x07,0x47,0x27,0x67,0x17,0x57,0x37,0x77,0x0f,0x4f,0x2f,0x6f,0x1f,0x5f,0x3f,0x7f, 320 }; 321 322 usbd_status 323 ehci_init(ehci_softc_t *sc) 324 { 325 u_int32_t vers, sparams, cparams, hcr; 326 u_int i; 327 usbd_status err; 328 ehci_soft_qh_t *sqh; 329 u_int ncomp; 330 331 DPRINTF(("ehci_init: start\n")); 332 #ifdef EHCI_DEBUG 333 theehci = sc; 334 #endif 335 336 sc->sc_offs = EREAD1(sc, EHCI_CAPLENGTH); 337 338 vers = EREAD2(sc, EHCI_HCIVERSION); 339 aprint_normal("%s: EHCI version %x.%x\n", USBDEVNAME(sc->sc_bus.bdev), 340 vers >> 8, vers & 0xff); 341 342 sparams = EREAD4(sc, EHCI_HCSPARAMS); 343 DPRINTF(("ehci_init: sparams=0x%x\n", sparams)); 344 sc->sc_npcomp = EHCI_HCS_N_PCC(sparams); 345 ncomp = EHCI_HCS_N_CC(sparams); 346 if (ncomp != sc->sc_ncomp) { 347 aprint_error("%s: wrong number of companions (%d != %d)\n", 348 USBDEVNAME(sc->sc_bus.bdev), 349 ncomp, sc->sc_ncomp); 350 #if NOHCI == 0 || NUHCI == 0 351 aprint_error("%s: ohci or uhci probably not configured\n", 352 USBDEVNAME(sc->sc_bus.bdev)); 353 #endif 354 if (ncomp < sc->sc_ncomp) 355 sc->sc_ncomp = ncomp; 356 } 357 if (sc->sc_ncomp > 0) { 358 aprint_normal("%s: companion controller%s, %d port%s each:", 359 USBDEVNAME(sc->sc_bus.bdev), sc->sc_ncomp!=1 ? "s" : "", 360 EHCI_HCS_N_PCC(sparams), 361 EHCI_HCS_N_PCC(sparams)!=1 ? "s" : ""); 362 for (i = 0; i < sc->sc_ncomp; i++) 363 aprint_normal(" %s", USBDEVNAME(sc->sc_comps[i]->bdev)); 364 aprint_normal("\n"); 365 } 366 sc->sc_noport = EHCI_HCS_N_PORTS(sparams); 367 cparams = EREAD4(sc, EHCI_HCCPARAMS); 368 DPRINTF(("ehci_init: cparams=0x%x\n", cparams)); 369 sc->sc_hasppc = EHCI_HCS_PPC(sparams); 370 371 if (EHCI_HCC_64BIT(cparams)) { 372 /* MUST clear segment register if 64 bit capable. */ 373 EWRITE4(sc, EHCI_CTRLDSSEGMENT, 0); 374 } 375 376 sc->sc_bus.usbrev = USBREV_2_0; 377 378 usb_setup_reserve(sc, &sc->sc_dma_reserve, sc->sc_bus.dmatag, 379 USB_MEM_RESERVE); 380 381 /* Reset the controller */ 382 DPRINTF(("%s: resetting\n", USBDEVNAME(sc->sc_bus.bdev))); 383 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */ 384 usb_delay_ms(&sc->sc_bus, 1); 385 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET); 386 for (i = 0; i < 100; i++) { 387 usb_delay_ms(&sc->sc_bus, 1); 388 hcr = EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_HCRESET; 389 if (!hcr) 390 break; 391 } 392 if (hcr) { 393 aprint_error("%s: reset timeout\n", 394 USBDEVNAME(sc->sc_bus.bdev)); 395 return (USBD_IOERROR); 396 } 397 398 /* XXX need proper intr scheduling */ 399 sc->sc_rand = 96; 400 401 /* frame list size at default, read back what we got and use that */ 402 switch (EHCI_CMD_FLS(EOREAD4(sc, EHCI_USBCMD))) { 403 case 0: sc->sc_flsize = 1024; break; 404 case 1: sc->sc_flsize = 512; break; 405 case 2: sc->sc_flsize = 256; break; 406 case 3: return (USBD_IOERROR); 407 } 408 err = usb_allocmem(&sc->sc_bus, sc->sc_flsize * sizeof(ehci_link_t), 409 EHCI_FLALIGN_ALIGN, &sc->sc_fldma); 410 if (err) 411 return (err); 412 DPRINTF(("%s: flsize=%d\n", USBDEVNAME(sc->sc_bus.bdev),sc->sc_flsize)); 413 sc->sc_flist = KERNADDR(&sc->sc_fldma, 0); 414 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0)); 415 416 /* Set up the bus struct. */ 417 sc->sc_bus.methods = &ehci_bus_methods; 418 sc->sc_bus.pipe_size = sizeof(struct ehci_pipe); 419 420 sc->sc_powerhook = powerhook_establish(ehci_power, sc); 421 sc->sc_shutdownhook = shutdownhook_establish(ehci_shutdown, sc); 422 423 sc->sc_eintrs = EHCI_NORMAL_INTRS; 424 425 /* 426 * Allocate the interrupt dummy QHs. These are arranged to give poll 427 * intervals that are powers of 2 times 1ms. 428 */ 429 for (i = 0; i < EHCI_INTRQHS; i++) { 430 sqh = ehci_alloc_sqh(sc); 431 if (sqh == NULL) { 432 err = USBD_NOMEM; 433 goto bad1; 434 } 435 sc->sc_islots[i].sqh = sqh; 436 } 437 for (i = 0; i < EHCI_INTRQHS; i++) { 438 sqh = sc->sc_islots[i].sqh; 439 if (i == 0) { 440 /* The last (1ms) QH terminates. */ 441 sqh->qh.qh_link = EHCI_NULL; 442 sqh->next = NULL; 443 } else { 444 /* Otherwise the next QH has half the poll interval */ 445 sqh->next = sc->sc_islots[(i + 1) / 2 - 1].sqh; 446 sqh->qh.qh_link = htole32(sqh->next->physaddr | 447 EHCI_LINK_QH); 448 } 449 sqh->qh.qh_endp = htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH)); 450 sqh->qh.qh_curqtd = EHCI_NULL; 451 sqh->next = NULL; 452 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 453 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 454 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED); 455 sqh->sqtd = NULL; 456 } 457 /* Point the frame list at the last level (128ms). */ 458 for (i = 0; i < sc->sc_flsize; i++) { 459 int j; 460 461 j = (i & ~(EHCI_MAX_POLLRATE-1)) | 462 revbits[i & (EHCI_MAX_POLLRATE-1)]; 463 sc->sc_flist[j] = htole32(EHCI_LINK_QH | 464 sc->sc_islots[EHCI_IQHIDX(EHCI_IPOLLRATES - 1, 465 i)].sqh->physaddr); 466 } 467 468 /* Allocate dummy QH that starts the async list. */ 469 sqh = ehci_alloc_sqh(sc); 470 if (sqh == NULL) { 471 err = USBD_NOMEM; 472 goto bad1; 473 } 474 /* Fill the QH */ 475 sqh->qh.qh_endp = 476 htole32(EHCI_QH_SET_EPS(EHCI_QH_SPEED_HIGH) | EHCI_QH_HRECL); 477 sqh->qh.qh_link = 478 htole32(sqh->physaddr | EHCI_LINK_QH); 479 sqh->qh.qh_curqtd = EHCI_NULL; 480 sqh->next = NULL; 481 /* Fill the overlay qTD */ 482 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 483 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 484 sqh->qh.qh_qtd.qtd_status = htole32(EHCI_QTD_HALTED); 485 sqh->sqtd = NULL; 486 #ifdef EHCI_DEBUG 487 if (ehcidebug) { 488 ehci_dump_sqh(sqh); 489 } 490 #endif 491 492 /* Point to async list */ 493 sc->sc_async_head = sqh; 494 EOWRITE4(sc, EHCI_ASYNCLISTADDR, sqh->physaddr | EHCI_LINK_QH); 495 496 usb_callout_init(sc->sc_tmo_pcd); 497 usb_callout_init(sc->sc_tmo_intrlist); 498 499 lockinit(&sc->sc_doorbell_lock, PZERO, "ehcidb", 0, 0); 500 501 /* Turn on controller */ 502 EOWRITE4(sc, EHCI_USBCMD, 503 EHCI_CMD_ITC_2 | /* 2 microframes interrupt delay */ 504 (EOREAD4(sc, EHCI_USBCMD) & EHCI_CMD_FLS_M) | 505 EHCI_CMD_ASE | 506 EHCI_CMD_PSE | 507 EHCI_CMD_RS); 508 509 /* Take over port ownership */ 510 EOWRITE4(sc, EHCI_CONFIGFLAG, EHCI_CONF_CF); 511 512 for (i = 0; i < 100; i++) { 513 usb_delay_ms(&sc->sc_bus, 1); 514 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 515 if (!hcr) 516 break; 517 } 518 if (hcr) { 519 aprint_error("%s: run timeout\n", USBDEVNAME(sc->sc_bus.bdev)); 520 return (USBD_IOERROR); 521 } 522 523 /* Enable interrupts */ 524 DPRINTFN(1,("ehci_init: enabling\n")); 525 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 526 527 return (USBD_NORMAL_COMPLETION); 528 529 #if 0 530 bad2: 531 ehci_free_sqh(sc, sc->sc_async_head); 532 #endif 533 bad1: 534 usb_freemem(&sc->sc_bus, &sc->sc_fldma); 535 return (err); 536 } 537 538 int 539 ehci_intr(void *v) 540 { 541 ehci_softc_t *sc = v; 542 543 if (sc == NULL || sc->sc_dying) 544 return (0); 545 546 /* If we get an interrupt while polling, then just ignore it. */ 547 if (sc->sc_bus.use_polling) { 548 u_int32_t intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 549 550 if (intrs) 551 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */ 552 #ifdef DIAGNOSTIC 553 DPRINTFN(16, ("ehci_intr: ignored interrupt while polling\n")); 554 #endif 555 return (0); 556 } 557 558 return (ehci_intr1(sc)); 559 } 560 561 Static int 562 ehci_intr1(ehci_softc_t *sc) 563 { 564 u_int32_t intrs, eintrs; 565 566 DPRINTFN(20,("ehci_intr1: enter\n")); 567 568 /* In case the interrupt occurs before initialization has completed. */ 569 if (sc == NULL) { 570 #ifdef DIAGNOSTIC 571 printf("ehci_intr1: sc == NULL\n"); 572 #endif 573 return (0); 574 } 575 576 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 577 if (!intrs) 578 return (0); 579 580 eintrs = intrs & sc->sc_eintrs; 581 DPRINTFN(7, ("ehci_intr1: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n", 582 sc, (u_int)intrs, EOREAD4(sc, EHCI_USBSTS), 583 (u_int)eintrs)); 584 if (!eintrs) 585 return (0); 586 587 EOWRITE4(sc, EHCI_USBSTS, intrs); /* Acknowledge */ 588 sc->sc_bus.intr_context++; 589 sc->sc_bus.no_intrs++; 590 if (eintrs & EHCI_STS_IAA) { 591 DPRINTF(("ehci_intr1: door bell\n")); 592 wakeup(&sc->sc_async_head); 593 eintrs &= ~EHCI_STS_IAA; 594 } 595 if (eintrs & (EHCI_STS_INT | EHCI_STS_ERRINT)) { 596 DPRINTFN(5,("ehci_intr1: %s %s\n", 597 eintrs & EHCI_STS_INT ? "INT" : "", 598 eintrs & EHCI_STS_ERRINT ? "ERRINT" : "")); 599 usb_schedsoftintr(&sc->sc_bus); 600 eintrs &= ~(EHCI_STS_INT | EHCI_STS_ERRINT); 601 } 602 if (eintrs & EHCI_STS_HSE) { 603 printf("%s: unrecoverable error, controller halted\n", 604 USBDEVNAME(sc->sc_bus.bdev)); 605 /* XXX what else */ 606 } 607 if (eintrs & EHCI_STS_PCD) { 608 ehci_pcd(sc, sc->sc_intrxfer); 609 /* 610 * Disable PCD interrupt for now, because it will be 611 * on until the port has been reset. 612 */ 613 ehci_pcd_able(sc, 0); 614 /* Do not allow RHSC interrupts > 1 per second */ 615 usb_callout(sc->sc_tmo_pcd, hz, ehci_pcd_enable, sc); 616 eintrs &= ~EHCI_STS_PCD; 617 } 618 619 sc->sc_bus.intr_context--; 620 621 if (eintrs != 0) { 622 /* Block unprocessed interrupts. */ 623 sc->sc_eintrs &= ~eintrs; 624 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 625 printf("%s: blocking intrs 0x%x\n", 626 USBDEVNAME(sc->sc_bus.bdev), eintrs); 627 } 628 629 return (1); 630 } 631 632 void 633 ehci_pcd_able(ehci_softc_t *sc, int on) 634 { 635 DPRINTFN(4, ("ehci_pcd_able: on=%d\n", on)); 636 if (on) 637 sc->sc_eintrs |= EHCI_STS_PCD; 638 else 639 sc->sc_eintrs &= ~EHCI_STS_PCD; 640 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 641 } 642 643 void 644 ehci_pcd_enable(void *v_sc) 645 { 646 ehci_softc_t *sc = v_sc; 647 648 ehci_pcd_able(sc, 1); 649 } 650 651 void 652 ehci_pcd(ehci_softc_t *sc, usbd_xfer_handle xfer) 653 { 654 usbd_pipe_handle pipe; 655 u_char *p; 656 int i, m; 657 658 if (xfer == NULL) { 659 /* Just ignore the change. */ 660 return; 661 } 662 663 pipe = xfer->pipe; 664 665 p = KERNADDR(&xfer->dmabuf, 0); 666 m = min(sc->sc_noport, xfer->length * 8 - 1); 667 memset(p, 0, xfer->length); 668 for (i = 1; i <= m; i++) { 669 /* Pick out CHANGE bits from the status reg. */ 670 if (EOREAD4(sc, EHCI_PORTSC(i)) & EHCI_PS_CLEAR) 671 p[i/8] |= 1 << (i%8); 672 } 673 DPRINTF(("ehci_pcd: change=0x%02x\n", *p)); 674 xfer->actlen = xfer->length; 675 xfer->status = USBD_NORMAL_COMPLETION; 676 677 usb_transfer_complete(xfer); 678 } 679 680 void 681 ehci_softintr(void *v) 682 { 683 ehci_softc_t *sc = v; 684 struct ehci_xfer *ex, *nextex; 685 686 DPRINTFN(10,("%s: ehci_softintr (%d)\n", USBDEVNAME(sc->sc_bus.bdev), 687 sc->sc_bus.intr_context)); 688 689 sc->sc_bus.intr_context++; 690 691 /* 692 * The only explanation I can think of for why EHCI is as brain dead 693 * as UHCI interrupt-wise is that Intel was involved in both. 694 * An interrupt just tells us that something is done, we have no 695 * clue what, so we need to scan through all active transfers. :-( 696 */ 697 for (ex = LIST_FIRST(&sc->sc_intrhead); ex; ex = nextex) { 698 nextex = LIST_NEXT(ex, inext); 699 ehci_check_intr(sc, ex); 700 } 701 702 /* Schedule a callout to catch any dropped transactions. */ 703 if ((sc->sc_flags & EHCIF_DROPPED_INTR_WORKAROUND) && 704 !LIST_EMPTY(&sc->sc_intrhead)) 705 usb_callout(sc->sc_tmo_intrlist, hz, 706 ehci_intrlist_timeout, sc); 707 708 #ifdef USB_USE_SOFTINTR 709 if (sc->sc_softwake) { 710 sc->sc_softwake = 0; 711 wakeup(&sc->sc_softwake); 712 } 713 #endif /* USB_USE_SOFTINTR */ 714 715 sc->sc_bus.intr_context--; 716 } 717 718 /* Check for an interrupt. */ 719 void 720 ehci_check_intr(ehci_softc_t *sc, struct ehci_xfer *ex) 721 { 722 ehci_soft_qtd_t *sqtd, *lsqtd; 723 u_int32_t status; 724 725 DPRINTFN(/*15*/2, ("ehci_check_intr: ex=%p\n", ex)); 726 727 if (ex->sqtdstart == NULL) { 728 printf("ehci_check_intr: sqtdstart=NULL\n"); 729 return; 730 } 731 lsqtd = ex->sqtdend; 732 #ifdef DIAGNOSTIC 733 if (lsqtd == NULL) { 734 printf("ehci_check_intr: lsqtd==0\n"); 735 return; 736 } 737 #endif 738 /* 739 * If the last TD is still active we need to check whether there 740 * is a an error somewhere in the middle, or whether there was a 741 * short packet (SPD and not ACTIVE). 742 */ 743 if (le32toh(lsqtd->qtd.qtd_status) & EHCI_QTD_ACTIVE) { 744 DPRINTFN(12, ("ehci_check_intr: active ex=%p\n", ex)); 745 for (sqtd = ex->sqtdstart; sqtd != lsqtd; sqtd=sqtd->nextqtd) { 746 status = le32toh(sqtd->qtd.qtd_status); 747 /* If there's an active QTD the xfer isn't done. */ 748 if (status & EHCI_QTD_ACTIVE) 749 break; 750 /* Any kind of error makes the xfer done. */ 751 if (status & EHCI_QTD_HALTED) 752 goto done; 753 /* We want short packets, and it is short: it's done */ 754 if (EHCI_QTD_GET_BYTES(status) != 0) 755 goto done; 756 } 757 DPRINTFN(12, ("ehci_check_intr: ex=%p std=%p still active\n", 758 ex, ex->sqtdstart)); 759 return; 760 } 761 done: 762 DPRINTFN(12, ("ehci_check_intr: ex=%p done\n", ex)); 763 usb_uncallout(ex->xfer.timeout_handle, ehci_timeout, ex); 764 ehci_idone(ex); 765 } 766 767 void 768 ehci_idone(struct ehci_xfer *ex) 769 { 770 usbd_xfer_handle xfer = &ex->xfer; 771 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 772 ehci_soft_qtd_t *sqtd, *lsqtd; 773 u_int32_t status = 0, nstatus = 0; 774 int actlen; 775 uint pkts_left; 776 777 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p\n", ex)); 778 #ifdef DIAGNOSTIC 779 { 780 int s = splhigh(); 781 if (ex->isdone) { 782 splx(s); 783 #ifdef EHCI_DEBUG 784 printf("ehci_idone: ex is done!\n "); 785 ehci_dump_exfer(ex); 786 #else 787 printf("ehci_idone: ex=%p is done!\n", ex); 788 #endif 789 return; 790 } 791 ex->isdone = 1; 792 splx(s); 793 } 794 #endif 795 796 if (xfer->status == USBD_CANCELLED || 797 xfer->status == USBD_TIMEOUT) { 798 DPRINTF(("ehci_idone: aborted xfer=%p\n", xfer)); 799 return; 800 } 801 802 #ifdef EHCI_DEBUG 803 DPRINTFN(/*10*/2, ("ehci_idone: xfer=%p, pipe=%p ready\n", xfer, epipe)); 804 if (ehcidebug > 10) 805 ehci_dump_sqtds(ex->sqtdstart); 806 #endif 807 808 /* The transfer is done, compute actual length and status. */ 809 lsqtd = ex->sqtdend; 810 actlen = 0; 811 for (sqtd = ex->sqtdstart; sqtd != lsqtd->nextqtd; sqtd=sqtd->nextqtd) { 812 nstatus = le32toh(sqtd->qtd.qtd_status); 813 if (nstatus & EHCI_QTD_ACTIVE) 814 break; 815 816 status = nstatus; 817 if (EHCI_QTD_GET_PID(status) != EHCI_QTD_PID_SETUP) 818 actlen += sqtd->len - EHCI_QTD_GET_BYTES(status); 819 } 820 821 /* 822 * If there are left over TDs we need to update the toggle. 823 * The default pipe doesn't need it since control transfers 824 * start the toggle at 0 every time. 825 */ 826 if (sqtd != lsqtd->nextqtd && 827 xfer->pipe->device->default_pipe != xfer->pipe) { 828 printf("ehci_idone: need toggle update status=%08x nstatus=%08x\n", status, nstatus); 829 #if 0 830 ehci_dump_sqh(epipe->sqh); 831 ehci_dump_sqtds(ex->sqtdstart); 832 #endif 833 epipe->nexttoggle = EHCI_QTD_GET_TOGGLE(nstatus); 834 } 835 836 /* 837 * For a short transfer we need to update the toggle for the missing 838 * packets within the qTD. 839 */ 840 pkts_left = EHCI_QTD_GET_BYTES(status) / 841 UGETW(xfer->pipe->endpoint->edesc->wMaxPacketSize); 842 epipe->nexttoggle ^= pkts_left % 2; 843 844 DPRINTFN(/*10*/2, ("ehci_idone: len=%d, actlen=%d, status=0x%x\n", 845 xfer->length, actlen, status)); 846 xfer->actlen = actlen; 847 if (status & EHCI_QTD_HALTED) { 848 #ifdef EHCI_DEBUG 849 char sbuf[128]; 850 851 bitmask_snprintf((u_int32_t)status, 852 "\20\7HALTED\6BUFERR\5BABBLE\4XACTERR" 853 "\3MISSED\1PINGSTATE", sbuf, sizeof(sbuf)); 854 855 DPRINTFN(2, ("ehci_idone: error, addr=%d, endpt=0x%02x, " 856 "status 0x%s\n", 857 xfer->pipe->device->address, 858 xfer->pipe->endpoint->edesc->bEndpointAddress, 859 sbuf)); 860 if (ehcidebug > 2) { 861 ehci_dump_sqh(epipe->sqh); 862 ehci_dump_sqtds(ex->sqtdstart); 863 } 864 #endif 865 /* low&full speed has an extra error flag */ 866 if (EHCI_QH_GET_EPS(epipe->sqh->qh.qh_endp) != 867 EHCI_QH_SPEED_HIGH) 868 status &= EHCI_QTD_STATERRS | EHCI_QTD_PINGSTATE; 869 else 870 status &= EHCI_QTD_STATERRS; 871 if (status == 0) /* no other errors means a stall */ 872 xfer->status = USBD_STALLED; 873 else 874 xfer->status = USBD_IOERROR; /* more info XXX */ 875 /* XXX need to reset TT on missed microframe */ 876 if (status & EHCI_QTD_MISSEDMICRO) { 877 ehci_softc_t *sc = (ehci_softc_t *) 878 xfer->pipe->device->bus; 879 880 printf("%s: missed microframe, TT reset not " 881 "implemented, hub might be inoperational\n", 882 USBDEVNAME(sc->sc_bus.bdev)); 883 } 884 } else { 885 xfer->status = USBD_NORMAL_COMPLETION; 886 } 887 888 usb_transfer_complete(xfer); 889 DPRINTFN(/*12*/2, ("ehci_idone: ex=%p done\n", ex)); 890 } 891 892 /* 893 * Wait here until controller claims to have an interrupt. 894 * Then call ehci_intr and return. Use timeout to avoid waiting 895 * too long. 896 */ 897 void 898 ehci_waitintr(ehci_softc_t *sc, usbd_xfer_handle xfer) 899 { 900 int timo; 901 u_int32_t intrs; 902 903 xfer->status = USBD_IN_PROGRESS; 904 for (timo = xfer->timeout; timo >= 0; timo--) { 905 usb_delay_ms(&sc->sc_bus, 1); 906 if (sc->sc_dying) 907 break; 908 intrs = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)) & 909 sc->sc_eintrs; 910 DPRINTFN(15,("ehci_waitintr: 0x%04x\n", intrs)); 911 #ifdef EHCI_DEBUG 912 if (ehcidebug > 15) 913 ehci_dump_regs(sc); 914 #endif 915 if (intrs) { 916 ehci_intr1(sc); 917 if (xfer->status != USBD_IN_PROGRESS) 918 return; 919 } 920 } 921 922 /* Timeout */ 923 DPRINTF(("ehci_waitintr: timeout\n")); 924 xfer->status = USBD_TIMEOUT; 925 usb_transfer_complete(xfer); 926 /* XXX should free TD */ 927 } 928 929 void 930 ehci_poll(struct usbd_bus *bus) 931 { 932 ehci_softc_t *sc = (ehci_softc_t *)bus; 933 #ifdef EHCI_DEBUG 934 static int last; 935 int new; 936 new = EHCI_STS_INTRS(EOREAD4(sc, EHCI_USBSTS)); 937 if (new != last) { 938 DPRINTFN(10,("ehci_poll: intrs=0x%04x\n", new)); 939 last = new; 940 } 941 #endif 942 943 if (EOREAD4(sc, EHCI_USBSTS) & sc->sc_eintrs) 944 ehci_intr1(sc); 945 } 946 947 int 948 ehci_detach(struct ehci_softc *sc, int flags) 949 { 950 int rv = 0; 951 952 if (sc->sc_child != NULL) 953 rv = config_detach(sc->sc_child, flags); 954 955 if (rv != 0) 956 return (rv); 957 958 usb_uncallout(sc->sc_tmo_intrlist, ehci_intrlist_timeout, sc); 959 usb_uncallout(sc->sc_tmo_pcd, ehci_pcd_enable, sc); 960 961 if (sc->sc_powerhook != NULL) 962 powerhook_disestablish(sc->sc_powerhook); 963 if (sc->sc_shutdownhook != NULL) 964 shutdownhook_disestablish(sc->sc_shutdownhook); 965 966 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */ 967 968 /* XXX free other data structures XXX */ 969 970 return (rv); 971 } 972 973 974 int 975 ehci_activate(device_ptr_t self, enum devact act) 976 { 977 struct ehci_softc *sc = (struct ehci_softc *)self; 978 int rv = 0; 979 980 switch (act) { 981 case DVACT_ACTIVATE: 982 return (EOPNOTSUPP); 983 984 case DVACT_DEACTIVATE: 985 if (sc->sc_child != NULL) 986 rv = config_deactivate(sc->sc_child); 987 sc->sc_dying = 1; 988 break; 989 } 990 return (rv); 991 } 992 993 /* 994 * Handle suspend/resume. 995 * 996 * We need to switch to polling mode here, because this routine is 997 * called from an interrupt context. This is all right since we 998 * are almost suspended anyway. 999 */ 1000 void 1001 ehci_power(int why, void *v) 1002 { 1003 ehci_softc_t *sc = v; 1004 u_int32_t cmd, hcr; 1005 int s, i; 1006 1007 #ifdef EHCI_DEBUG 1008 DPRINTF(("ehci_power: sc=%p, why=%d\n", sc, why)); 1009 if (ehcidebug > 0) 1010 ehci_dump_regs(sc); 1011 #endif 1012 1013 s = splhardusb(); 1014 switch (why) { 1015 case PWR_SUSPEND: 1016 case PWR_STANDBY: 1017 sc->sc_bus.use_polling++; 1018 1019 sc->sc_cmd = EOREAD4(sc, EHCI_USBCMD); 1020 1021 cmd = sc->sc_cmd & ~(EHCI_CMD_ASE | EHCI_CMD_PSE); 1022 EOWRITE4(sc, EHCI_USBCMD, cmd); 1023 1024 for (i = 0; i < 100; i++) { 1025 hcr = EOREAD4(sc, EHCI_USBSTS) & 1026 (EHCI_STS_ASS | EHCI_STS_PSS); 1027 if (hcr == 0) 1028 break; 1029 1030 usb_delay_ms(&sc->sc_bus, 1); 1031 } 1032 if (hcr != 0) { 1033 printf("%s: reset timeout\n", 1034 USBDEVNAME(sc->sc_bus.bdev)); 1035 } 1036 1037 cmd &= ~EHCI_CMD_RS; 1038 EOWRITE4(sc, EHCI_USBCMD, cmd); 1039 1040 for (i = 0; i < 100; i++) { 1041 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 1042 if (hcr == EHCI_STS_HCH) 1043 break; 1044 1045 usb_delay_ms(&sc->sc_bus, 1); 1046 } 1047 if (hcr != EHCI_STS_HCH) { 1048 printf("%s: config timeout\n", 1049 USBDEVNAME(sc->sc_bus.bdev)); 1050 } 1051 1052 sc->sc_bus.use_polling--; 1053 break; 1054 1055 case PWR_RESUME: 1056 sc->sc_bus.use_polling++; 1057 1058 /* restore things in case the bios sucks */ 1059 EOWRITE4(sc, EHCI_CTRLDSSEGMENT, 0); 1060 EOWRITE4(sc, EHCI_PERIODICLISTBASE, DMAADDR(&sc->sc_fldma, 0)); 1061 EOWRITE4(sc, EHCI_ASYNCLISTADDR, 1062 sc->sc_async_head->physaddr | EHCI_LINK_QH); 1063 EOWRITE4(sc, EHCI_USBINTR, sc->sc_eintrs); 1064 1065 EOWRITE4(sc, EHCI_USBCMD, sc->sc_cmd); 1066 1067 for (i = 0; i < 100; i++) { 1068 hcr = EOREAD4(sc, EHCI_USBSTS) & EHCI_STS_HCH; 1069 if (hcr != EHCI_STS_HCH) 1070 break; 1071 1072 usb_delay_ms(&sc->sc_bus, 1); 1073 } 1074 if (hcr == EHCI_STS_HCH) { 1075 printf("%s: config timeout\n", 1076 USBDEVNAME(sc->sc_bus.bdev)); 1077 } 1078 1079 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 1080 1081 sc->sc_bus.use_polling--; 1082 break; 1083 case PWR_SOFTSUSPEND: 1084 case PWR_SOFTSTANDBY: 1085 case PWR_SOFTRESUME: 1086 break; 1087 } 1088 splx(s); 1089 1090 #ifdef EHCI_DEBUG 1091 DPRINTF(("ehci_power: sc=%p\n", sc)); 1092 if (ehcidebug > 0) 1093 ehci_dump_regs(sc); 1094 #endif 1095 } 1096 1097 /* 1098 * Shut down the controller when the system is going down. 1099 */ 1100 void 1101 ehci_shutdown(void *v) 1102 { 1103 ehci_softc_t *sc = v; 1104 1105 DPRINTF(("ehci_shutdown: stopping the HC\n")); 1106 EOWRITE4(sc, EHCI_USBCMD, 0); /* Halt controller */ 1107 EOWRITE4(sc, EHCI_USBCMD, EHCI_CMD_HCRESET); 1108 } 1109 1110 usbd_status 1111 ehci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) 1112 { 1113 struct ehci_softc *sc = (struct ehci_softc *)bus; 1114 usbd_status err; 1115 1116 err = usb_allocmem(&sc->sc_bus, size, 0, dma); 1117 if (err == USBD_NOMEM) 1118 err = usb_reserve_allocm(&sc->sc_dma_reserve, dma, size); 1119 #ifdef EHCI_DEBUG 1120 if (err) 1121 printf("ehci_allocm: usb_allocmem()=%d\n", err); 1122 #endif 1123 return (err); 1124 } 1125 1126 void 1127 ehci_freem(struct usbd_bus *bus, usb_dma_t *dma) 1128 { 1129 struct ehci_softc *sc = (struct ehci_softc *)bus; 1130 1131 if (dma->block->flags & USB_DMA_RESERVE) { 1132 usb_reserve_freem(&((struct ehci_softc *)bus)->sc_dma_reserve, 1133 dma); 1134 return; 1135 } 1136 usb_freemem(&sc->sc_bus, dma); 1137 } 1138 1139 usbd_xfer_handle 1140 ehci_allocx(struct usbd_bus *bus) 1141 { 1142 struct ehci_softc *sc = (struct ehci_softc *)bus; 1143 usbd_xfer_handle xfer; 1144 1145 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 1146 if (xfer != NULL) { 1147 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 1148 #ifdef DIAGNOSTIC 1149 if (xfer->busy_free != XFER_FREE) { 1150 printf("ehci_allocx: xfer=%p not free, 0x%08x\n", xfer, 1151 xfer->busy_free); 1152 } 1153 #endif 1154 } else { 1155 xfer = malloc(sizeof(struct ehci_xfer), M_USB, M_NOWAIT); 1156 } 1157 if (xfer != NULL) { 1158 memset(xfer, 0, sizeof(struct ehci_xfer)); 1159 #ifdef DIAGNOSTIC 1160 EXFER(xfer)->isdone = 1; 1161 xfer->busy_free = XFER_BUSY; 1162 #endif 1163 } 1164 return (xfer); 1165 } 1166 1167 void 1168 ehci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) 1169 { 1170 struct ehci_softc *sc = (struct ehci_softc *)bus; 1171 1172 #ifdef DIAGNOSTIC 1173 if (xfer->busy_free != XFER_BUSY) { 1174 printf("ehci_freex: xfer=%p not busy, 0x%08x\n", xfer, 1175 xfer->busy_free); 1176 return; 1177 } 1178 xfer->busy_free = XFER_FREE; 1179 if (!EXFER(xfer)->isdone) { 1180 printf("ehci_freex: !isdone\n"); 1181 return; 1182 } 1183 #endif 1184 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); 1185 } 1186 1187 Static void 1188 ehci_device_clear_toggle(usbd_pipe_handle pipe) 1189 { 1190 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 1191 1192 DPRINTF(("ehci_device_clear_toggle: epipe=%p status=0x%x\n", 1193 epipe, epipe->sqh->qh.qh_qtd.qtd_status)); 1194 #ifdef USB_DEBUG 1195 if (ehcidebug) 1196 usbd_dump_pipe(pipe); 1197 #endif 1198 epipe->nexttoggle = 0; 1199 } 1200 1201 Static void 1202 ehci_noop(usbd_pipe_handle pipe) 1203 { 1204 } 1205 1206 #ifdef EHCI_DEBUG 1207 void 1208 ehci_dump_regs(ehci_softc_t *sc) 1209 { 1210 int i; 1211 printf("cmd=0x%08x, sts=0x%08x, ien=0x%08x\n", 1212 EOREAD4(sc, EHCI_USBCMD), 1213 EOREAD4(sc, EHCI_USBSTS), 1214 EOREAD4(sc, EHCI_USBINTR)); 1215 printf("frindex=0x%08x ctrdsegm=0x%08x periodic=0x%08x async=0x%08x\n", 1216 EOREAD4(sc, EHCI_FRINDEX), 1217 EOREAD4(sc, EHCI_CTRLDSSEGMENT), 1218 EOREAD4(sc, EHCI_PERIODICLISTBASE), 1219 EOREAD4(sc, EHCI_ASYNCLISTADDR)); 1220 for (i = 1; i <= sc->sc_noport; i++) 1221 printf("port %d status=0x%08x\n", i, 1222 EOREAD4(sc, EHCI_PORTSC(i))); 1223 } 1224 1225 /* 1226 * Unused function - this is meant to be called from a kernel 1227 * debugger. 1228 */ 1229 void 1230 ehci_dump() 1231 { 1232 ehci_dump_regs(theehci); 1233 } 1234 1235 void 1236 ehci_dump_link(ehci_link_t link, int type) 1237 { 1238 link = le32toh(link); 1239 printf("0x%08x", link); 1240 if (link & EHCI_LINK_TERMINATE) 1241 printf("<T>"); 1242 else { 1243 printf("<"); 1244 if (type) { 1245 switch (EHCI_LINK_TYPE(link)) { 1246 case EHCI_LINK_ITD: printf("ITD"); break; 1247 case EHCI_LINK_QH: printf("QH"); break; 1248 case EHCI_LINK_SITD: printf("SITD"); break; 1249 case EHCI_LINK_FSTN: printf("FSTN"); break; 1250 } 1251 } 1252 printf(">"); 1253 } 1254 } 1255 1256 void 1257 ehci_dump_sqtds(ehci_soft_qtd_t *sqtd) 1258 { 1259 int i; 1260 u_int32_t stop; 1261 1262 stop = 0; 1263 for (i = 0; sqtd && i < 20 && !stop; sqtd = sqtd->nextqtd, i++) { 1264 ehci_dump_sqtd(sqtd); 1265 stop = sqtd->qtd.qtd_next & htole32(EHCI_LINK_TERMINATE); 1266 } 1267 if (sqtd) 1268 printf("dump aborted, too many TDs\n"); 1269 } 1270 1271 void 1272 ehci_dump_sqtd(ehci_soft_qtd_t *sqtd) 1273 { 1274 printf("QTD(%p) at 0x%08x:\n", sqtd, sqtd->physaddr); 1275 ehci_dump_qtd(&sqtd->qtd); 1276 } 1277 1278 void 1279 ehci_dump_qtd(ehci_qtd_t *qtd) 1280 { 1281 u_int32_t s; 1282 char sbuf[128]; 1283 1284 printf(" next="); ehci_dump_link(qtd->qtd_next, 0); 1285 printf(" altnext="); ehci_dump_link(qtd->qtd_altnext, 0); 1286 printf("\n"); 1287 s = le32toh(qtd->qtd_status); 1288 bitmask_snprintf(EHCI_QTD_GET_STATUS(s), 1289 "\20\10ACTIVE\7HALTED\6BUFERR\5BABBLE\4XACTERR" 1290 "\3MISSED\2SPLIT\1PING", sbuf, sizeof(sbuf)); 1291 printf(" status=0x%08x: toggle=%d bytes=0x%x ioc=%d c_page=0x%x\n", 1292 s, EHCI_QTD_GET_TOGGLE(s), EHCI_QTD_GET_BYTES(s), 1293 EHCI_QTD_GET_IOC(s), EHCI_QTD_GET_C_PAGE(s)); 1294 printf(" cerr=%d pid=%d stat=0x%s\n", EHCI_QTD_GET_CERR(s), 1295 EHCI_QTD_GET_PID(s), sbuf); 1296 for (s = 0; s < 5; s++) 1297 printf(" buffer[%d]=0x%08x\n", s, le32toh(qtd->qtd_buffer[s])); 1298 } 1299 1300 void 1301 ehci_dump_sqh(ehci_soft_qh_t *sqh) 1302 { 1303 ehci_qh_t *qh = &sqh->qh; 1304 u_int32_t endp, endphub; 1305 1306 printf("QH(%p) at 0x%08x:\n", sqh, sqh->physaddr); 1307 printf(" link="); ehci_dump_link(qh->qh_link, 1); printf("\n"); 1308 endp = le32toh(qh->qh_endp); 1309 printf(" endp=0x%08x\n", endp); 1310 printf(" addr=0x%02x inact=%d endpt=%d eps=%d dtc=%d hrecl=%d\n", 1311 EHCI_QH_GET_ADDR(endp), EHCI_QH_GET_INACT(endp), 1312 EHCI_QH_GET_ENDPT(endp), EHCI_QH_GET_EPS(endp), 1313 EHCI_QH_GET_DTC(endp), EHCI_QH_GET_HRECL(endp)); 1314 printf(" mpl=0x%x ctl=%d nrl=%d\n", 1315 EHCI_QH_GET_MPL(endp), EHCI_QH_GET_CTL(endp), 1316 EHCI_QH_GET_NRL(endp)); 1317 endphub = le32toh(qh->qh_endphub); 1318 printf(" endphub=0x%08x\n", endphub); 1319 printf(" smask=0x%02x cmask=0x%02x huba=0x%02x port=%d mult=%d\n", 1320 EHCI_QH_GET_SMASK(endphub), EHCI_QH_GET_CMASK(endphub), 1321 EHCI_QH_GET_HUBA(endphub), EHCI_QH_GET_PORT(endphub), 1322 EHCI_QH_GET_MULT(endphub)); 1323 printf(" curqtd="); ehci_dump_link(qh->qh_curqtd, 0); printf("\n"); 1324 printf("Overlay qTD:\n"); 1325 ehci_dump_qtd(&qh->qh_qtd); 1326 } 1327 1328 #ifdef DIAGNOSTIC 1329 Static void 1330 ehci_dump_exfer(struct ehci_xfer *ex) 1331 { 1332 printf("ehci_dump_exfer: ex=%p\n", ex); 1333 } 1334 #endif 1335 #endif 1336 1337 usbd_status 1338 ehci_open(usbd_pipe_handle pipe) 1339 { 1340 usbd_device_handle dev = pipe->device; 1341 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 1342 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; 1343 u_int8_t addr = dev->address; 1344 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE; 1345 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 1346 ehci_soft_qh_t *sqh; 1347 usbd_status err; 1348 int s; 1349 int ival, speed, naks; 1350 int hshubaddr, hshubport; 1351 1352 DPRINTFN(1, ("ehci_open: pipe=%p, addr=%d, endpt=%d (%d)\n", 1353 pipe, addr, ed->bEndpointAddress, sc->sc_addr)); 1354 1355 if (dev->myhsport) { 1356 hshubaddr = dev->myhsport->parent->address; 1357 hshubport = dev->myhsport->portno; 1358 } else { 1359 hshubaddr = 0; 1360 hshubport = 0; 1361 } 1362 1363 if (sc->sc_dying) 1364 return (USBD_IOERROR); 1365 1366 epipe->nexttoggle = 0; 1367 1368 if (addr == sc->sc_addr) { 1369 switch (ed->bEndpointAddress) { 1370 case USB_CONTROL_ENDPOINT: 1371 pipe->methods = &ehci_root_ctrl_methods; 1372 break; 1373 case UE_DIR_IN | EHCI_INTR_ENDPT: 1374 pipe->methods = &ehci_root_intr_methods; 1375 break; 1376 default: 1377 return (USBD_INVAL); 1378 } 1379 return (USBD_NORMAL_COMPLETION); 1380 } 1381 1382 /* XXX All this stuff is only valid for async. */ 1383 switch (dev->speed) { 1384 case USB_SPEED_LOW: speed = EHCI_QH_SPEED_LOW; break; 1385 case USB_SPEED_FULL: speed = EHCI_QH_SPEED_FULL; break; 1386 case USB_SPEED_HIGH: speed = EHCI_QH_SPEED_HIGH; break; 1387 default: panic("ehci_open: bad device speed %d", dev->speed); 1388 } 1389 if (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_ISOCHRONOUS) { 1390 printf("%s: *** WARNING: opening low/full speed isoc device, " 1391 "this does not work yet.\n", 1392 USBDEVNAME(sc->sc_bus.bdev)); 1393 DPRINTFN(1,("ehci_open: hshubaddr=%d hshubport=%d\n", 1394 hshubaddr, hshubport)); 1395 return USBD_INVAL; 1396 } 1397 1398 naks = 8; /* XXX */ 1399 sqh = ehci_alloc_sqh(sc); 1400 if (sqh == NULL) 1401 goto bad0; 1402 /* qh_link filled when the QH is added */ 1403 sqh->qh.qh_endp = htole32( 1404 EHCI_QH_SET_ADDR(addr) | 1405 EHCI_QH_SET_ENDPT(UE_GET_ADDR(ed->bEndpointAddress)) | 1406 EHCI_QH_SET_EPS(speed) | 1407 EHCI_QH_DTC | 1408 EHCI_QH_SET_MPL(UGETW(ed->wMaxPacketSize)) | 1409 (speed != EHCI_QH_SPEED_HIGH && xfertype == UE_CONTROL ? 1410 EHCI_QH_CTL : 0) | 1411 EHCI_QH_SET_NRL(naks) 1412 ); 1413 sqh->qh.qh_endphub = htole32( 1414 EHCI_QH_SET_MULT(1) | 1415 EHCI_QH_SET_HUBA(hshubaddr) | 1416 EHCI_QH_SET_PORT(hshubport) | 1417 EHCI_QH_SET_CMASK(0x08) | /* XXX */ 1418 EHCI_QH_SET_SMASK(xfertype == UE_INTERRUPT ? 0x02 : 0) 1419 ); 1420 sqh->qh.qh_curqtd = EHCI_NULL; 1421 /* Fill the overlay qTD */ 1422 sqh->qh.qh_qtd.qtd_next = EHCI_NULL; 1423 sqh->qh.qh_qtd.qtd_altnext = EHCI_NULL; 1424 sqh->qh.qh_qtd.qtd_status = htole32(0); 1425 1426 epipe->sqh = sqh; 1427 1428 switch (xfertype) { 1429 case UE_CONTROL: 1430 err = usb_allocmem(&sc->sc_bus, sizeof(usb_device_request_t), 1431 0, &epipe->u.ctl.reqdma); 1432 #ifdef EHCI_DEBUG 1433 if (err) 1434 printf("ehci_open: usb_allocmem()=%d\n", err); 1435 #endif 1436 if (err) 1437 goto bad1; 1438 pipe->methods = &ehci_device_ctrl_methods; 1439 s = splusb(); 1440 ehci_add_qh(sqh, sc->sc_async_head); 1441 splx(s); 1442 break; 1443 case UE_BULK: 1444 pipe->methods = &ehci_device_bulk_methods; 1445 s = splusb(); 1446 ehci_add_qh(sqh, sc->sc_async_head); 1447 splx(s); 1448 break; 1449 case UE_INTERRUPT: 1450 pipe->methods = &ehci_device_intr_methods; 1451 ival = pipe->interval; 1452 if (ival == USBD_DEFAULT_INTERVAL) 1453 ival = ed->bInterval; 1454 return (ehci_device_setintr(sc, sqh, ival)); 1455 case UE_ISOCHRONOUS: 1456 pipe->methods = &ehci_device_isoc_methods; 1457 return (USBD_INVAL); 1458 default: 1459 return (USBD_INVAL); 1460 } 1461 return (USBD_NORMAL_COMPLETION); 1462 1463 bad1: 1464 ehci_free_sqh(sc, sqh); 1465 bad0: 1466 return (USBD_NOMEM); 1467 } 1468 1469 /* 1470 * Add an ED to the schedule. Called at splusb(). 1471 */ 1472 void 1473 ehci_add_qh(ehci_soft_qh_t *sqh, ehci_soft_qh_t *head) 1474 { 1475 SPLUSBCHECK; 1476 1477 sqh->next = head->next; 1478 sqh->qh.qh_link = head->qh.qh_link; 1479 head->next = sqh; 1480 head->qh.qh_link = htole32(sqh->physaddr | EHCI_LINK_QH); 1481 1482 #ifdef EHCI_DEBUG 1483 if (ehcidebug > 5) { 1484 printf("ehci_add_qh:\n"); 1485 ehci_dump_sqh(sqh); 1486 } 1487 #endif 1488 } 1489 1490 /* 1491 * Remove an ED from the schedule. Called at splusb(). 1492 */ 1493 void 1494 ehci_rem_qh(ehci_softc_t *sc, ehci_soft_qh_t *sqh, ehci_soft_qh_t *head) 1495 { 1496 ehci_soft_qh_t *p; 1497 1498 SPLUSBCHECK; 1499 /* XXX */ 1500 for (p = head; p != NULL && p->next != sqh; p = p->next) 1501 ; 1502 if (p == NULL) 1503 panic("ehci_rem_qh: ED not found"); 1504 p->next = sqh->next; 1505 p->qh.qh_link = sqh->qh.qh_link; 1506 1507 ehci_sync_hc(sc); 1508 } 1509 1510 void 1511 ehci_set_qh_qtd(ehci_soft_qh_t *sqh, ehci_soft_qtd_t *sqtd) 1512 { 1513 int i; 1514 u_int32_t status; 1515 1516 /* Save toggle bit and ping status. */ 1517 status = sqh->qh.qh_qtd.qtd_status & 1518 htole32(EHCI_QTD_TOGGLE_MASK | 1519 EHCI_QTD_SET_STATUS(EHCI_QTD_PINGSTATE)); 1520 /* Set HALTED to make hw leave it alone. */ 1521 sqh->qh.qh_qtd.qtd_status = 1522 htole32(EHCI_QTD_SET_STATUS(EHCI_QTD_HALTED)); 1523 sqh->qh.qh_curqtd = 0; 1524 sqh->qh.qh_qtd.qtd_next = htole32(sqtd->physaddr); 1525 sqh->qh.qh_qtd.qtd_altnext = 0; 1526 for (i = 0; i < EHCI_QTD_NBUFFERS; i++) 1527 sqh->qh.qh_qtd.qtd_buffer[i] = 0; 1528 sqh->sqtd = sqtd; 1529 /* Set !HALTED && !ACTIVE to start execution, preserve some fields */ 1530 sqh->qh.qh_qtd.qtd_status = status; 1531 } 1532 1533 /* 1534 * Ensure that the HC has released all references to the QH. We do this 1535 * by asking for a Async Advance Doorbell interrupt and then we wait for 1536 * the interrupt. 1537 * To make this easier we first obtain exclusive use of the doorbell. 1538 */ 1539 void 1540 ehci_sync_hc(ehci_softc_t *sc) 1541 { 1542 int s, error; 1543 1544 if (sc->sc_dying) { 1545 DPRINTFN(2,("ehci_sync_hc: dying\n")); 1546 return; 1547 } 1548 DPRINTFN(2,("ehci_sync_hc: enter\n")); 1549 usb_lockmgr(&sc->sc_doorbell_lock, LK_EXCLUSIVE, NULL); /* get doorbell */ 1550 s = splhardusb(); 1551 /* ask for doorbell */ 1552 EOWRITE4(sc, EHCI_USBCMD, EOREAD4(sc, EHCI_USBCMD) | EHCI_CMD_IAAD); 1553 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n", 1554 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS))); 1555 error = tsleep(&sc->sc_async_head, PZERO, "ehcidi", hz); /* bell wait */ 1556 DPRINTFN(1,("ehci_sync_hc: cmd=0x%08x sts=0x%08x\n", 1557 EOREAD4(sc, EHCI_USBCMD), EOREAD4(sc, EHCI_USBSTS))); 1558 splx(s); 1559 usb_lockmgr(&sc->sc_doorbell_lock, LK_RELEASE, NULL); /* release doorbell */ 1560 #ifdef DIAGNOSTIC 1561 if (error) 1562 printf("ehci_sync_hc: tsleep() = %d\n", error); 1563 #endif 1564 DPRINTFN(2,("ehci_sync_hc: exit\n")); 1565 } 1566 1567 /***********/ 1568 1569 /* 1570 * Data structures and routines to emulate the root hub. 1571 */ 1572 Static usb_device_descriptor_t ehci_devd = { 1573 USB_DEVICE_DESCRIPTOR_SIZE, 1574 UDESC_DEVICE, /* type */ 1575 {0x00, 0x02}, /* USB version */ 1576 UDCLASS_HUB, /* class */ 1577 UDSUBCLASS_HUB, /* subclass */ 1578 UDPROTO_HSHUBSTT, /* protocol */ 1579 64, /* max packet */ 1580 {0},{0},{0x00,0x01}, /* device id */ 1581 1,2,0, /* string indicies */ 1582 1 /* # of configurations */ 1583 }; 1584 1585 Static usb_device_qualifier_t ehci_odevd = { 1586 USB_DEVICE_DESCRIPTOR_SIZE, 1587 UDESC_DEVICE_QUALIFIER, /* type */ 1588 {0x00, 0x02}, /* USB version */ 1589 UDCLASS_HUB, /* class */ 1590 UDSUBCLASS_HUB, /* subclass */ 1591 UDPROTO_FSHUB, /* protocol */ 1592 64, /* max packet */ 1593 1, /* # of configurations */ 1594 0 1595 }; 1596 1597 Static usb_config_descriptor_t ehci_confd = { 1598 USB_CONFIG_DESCRIPTOR_SIZE, 1599 UDESC_CONFIG, 1600 {USB_CONFIG_DESCRIPTOR_SIZE + 1601 USB_INTERFACE_DESCRIPTOR_SIZE + 1602 USB_ENDPOINT_DESCRIPTOR_SIZE}, 1603 1, 1604 1, 1605 0, 1606 UC_SELF_POWERED, 1607 0 /* max power */ 1608 }; 1609 1610 Static usb_interface_descriptor_t ehci_ifcd = { 1611 USB_INTERFACE_DESCRIPTOR_SIZE, 1612 UDESC_INTERFACE, 1613 0, 1614 0, 1615 1, 1616 UICLASS_HUB, 1617 UISUBCLASS_HUB, 1618 UIPROTO_HSHUBSTT, 1619 0 1620 }; 1621 1622 Static usb_endpoint_descriptor_t ehci_endpd = { 1623 USB_ENDPOINT_DESCRIPTOR_SIZE, 1624 UDESC_ENDPOINT, 1625 UE_DIR_IN | EHCI_INTR_ENDPT, 1626 UE_INTERRUPT, 1627 {8, 0}, /* max packet */ 1628 255 1629 }; 1630 1631 Static usb_hub_descriptor_t ehci_hubd = { 1632 USB_HUB_DESCRIPTOR_SIZE, 1633 UDESC_HUB, 1634 0, 1635 {0,0}, 1636 0, 1637 0, 1638 {0}, 1639 }; 1640 1641 Static int 1642 ehci_str(usb_string_descriptor_t *p, int l, const char *s) 1643 { 1644 int i; 1645 1646 if (l == 0) 1647 return (0); 1648 p->bLength = 2 * strlen(s) + 2; 1649 if (l == 1) 1650 return (1); 1651 p->bDescriptorType = UDESC_STRING; 1652 l -= 2; 1653 for (i = 0; s[i] && l > 1; i++, l -= 2) 1654 USETW2(p->bString[i], 0, s[i]); 1655 return (2*i+2); 1656 } 1657 1658 /* 1659 * Simulate a hardware hub by handling all the necessary requests. 1660 */ 1661 Static usbd_status 1662 ehci_root_ctrl_transfer(usbd_xfer_handle xfer) 1663 { 1664 usbd_status err; 1665 1666 /* Insert last in queue. */ 1667 err = usb_insert_transfer(xfer); 1668 if (err) 1669 return (err); 1670 1671 /* Pipe isn't running, start first */ 1672 return (ehci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 1673 } 1674 1675 Static usbd_status 1676 ehci_root_ctrl_start(usbd_xfer_handle xfer) 1677 { 1678 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 1679 usb_device_request_t *req; 1680 void *buf = NULL; 1681 int port, i; 1682 int s, len, value, index, l, totlen = 0; 1683 usb_port_status_t ps; 1684 usb_hub_descriptor_t hubd; 1685 usbd_status err; 1686 u_int32_t v; 1687 1688 if (sc->sc_dying) 1689 return (USBD_IOERROR); 1690 1691 #ifdef DIAGNOSTIC 1692 if (!(xfer->rqflags & URQ_REQUEST)) 1693 /* XXX panic */ 1694 return (USBD_INVAL); 1695 #endif 1696 req = &xfer->request; 1697 1698 DPRINTFN(4,("ehci_root_ctrl_start: type=0x%02x request=%02x\n", 1699 req->bmRequestType, req->bRequest)); 1700 1701 len = UGETW(req->wLength); 1702 value = UGETW(req->wValue); 1703 index = UGETW(req->wIndex); 1704 1705 if (len != 0) 1706 buf = KERNADDR(&xfer->dmabuf, 0); 1707 1708 #define C(x,y) ((x) | ((y) << 8)) 1709 switch(C(req->bRequest, req->bmRequestType)) { 1710 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 1711 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 1712 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 1713 /* 1714 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 1715 * for the integrated root hub. 1716 */ 1717 break; 1718 case C(UR_GET_CONFIG, UT_READ_DEVICE): 1719 if (len > 0) { 1720 *(u_int8_t *)buf = sc->sc_conf; 1721 totlen = 1; 1722 } 1723 break; 1724 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 1725 DPRINTFN(8,("ehci_root_ctrl_start: wValue=0x%04x\n", value)); 1726 switch(value >> 8) { 1727 case UDESC_DEVICE: 1728 if ((value & 0xff) != 0) { 1729 err = USBD_IOERROR; 1730 goto ret; 1731 } 1732 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 1733 USETW(ehci_devd.idVendor, sc->sc_id_vendor); 1734 memcpy(buf, &ehci_devd, l); 1735 break; 1736 /* 1737 * We can't really operate at another speed, but the spec says 1738 * we need this descriptor. 1739 */ 1740 case UDESC_DEVICE_QUALIFIER: 1741 if ((value & 0xff) != 0) { 1742 err = USBD_IOERROR; 1743 goto ret; 1744 } 1745 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 1746 memcpy(buf, &ehci_odevd, l); 1747 break; 1748 /* 1749 * We can't really operate at another speed, but the spec says 1750 * we need this descriptor. 1751 */ 1752 case UDESC_OTHER_SPEED_CONFIGURATION: 1753 case UDESC_CONFIG: 1754 if ((value & 0xff) != 0) { 1755 err = USBD_IOERROR; 1756 goto ret; 1757 } 1758 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); 1759 memcpy(buf, &ehci_confd, l); 1760 ((usb_config_descriptor_t *)buf)->bDescriptorType = 1761 value >> 8; 1762 buf = (char *)buf + l; 1763 len -= l; 1764 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); 1765 totlen += l; 1766 memcpy(buf, &ehci_ifcd, l); 1767 buf = (char *)buf + l; 1768 len -= l; 1769 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); 1770 totlen += l; 1771 memcpy(buf, &ehci_endpd, l); 1772 break; 1773 case UDESC_STRING: 1774 if (len == 0) 1775 break; 1776 *(u_int8_t *)buf = 0; 1777 totlen = 1; 1778 switch (value & 0xff) { 1779 case 0: /* Language table */ 1780 totlen = ehci_str(buf, len, "\001"); 1781 break; 1782 case 1: /* Vendor */ 1783 totlen = ehci_str(buf, len, sc->sc_vendor); 1784 break; 1785 case 2: /* Product */ 1786 totlen = ehci_str(buf, len, "EHCI root hub"); 1787 break; 1788 } 1789 break; 1790 default: 1791 err = USBD_IOERROR; 1792 goto ret; 1793 } 1794 break; 1795 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 1796 if (len > 0) { 1797 *(u_int8_t *)buf = 0; 1798 totlen = 1; 1799 } 1800 break; 1801 case C(UR_GET_STATUS, UT_READ_DEVICE): 1802 if (len > 1) { 1803 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED); 1804 totlen = 2; 1805 } 1806 break; 1807 case C(UR_GET_STATUS, UT_READ_INTERFACE): 1808 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 1809 if (len > 1) { 1810 USETW(((usb_status_t *)buf)->wStatus, 0); 1811 totlen = 2; 1812 } 1813 break; 1814 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 1815 if (value >= USB_MAX_DEVICES) { 1816 err = USBD_IOERROR; 1817 goto ret; 1818 } 1819 sc->sc_addr = value; 1820 break; 1821 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 1822 if (value != 0 && value != 1) { 1823 err = USBD_IOERROR; 1824 goto ret; 1825 } 1826 sc->sc_conf = value; 1827 break; 1828 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 1829 break; 1830 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 1831 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 1832 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 1833 err = USBD_IOERROR; 1834 goto ret; 1835 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 1836 break; 1837 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 1838 break; 1839 /* Hub requests */ 1840 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 1841 break; 1842 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 1843 DPRINTFN(4, ("ehci_root_ctrl_start: UR_CLEAR_PORT_FEATURE " 1844 "port=%d feature=%d\n", 1845 index, value)); 1846 if (index < 1 || index > sc->sc_noport) { 1847 err = USBD_IOERROR; 1848 goto ret; 1849 } 1850 port = EHCI_PORTSC(index); 1851 v = EOREAD4(sc, port); 1852 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v)); 1853 v &= ~EHCI_PS_CLEAR; 1854 switch(value) { 1855 case UHF_PORT_ENABLE: 1856 EOWRITE4(sc, port, v &~ EHCI_PS_PE); 1857 break; 1858 case UHF_PORT_SUSPEND: 1859 EOWRITE4(sc, port, v &~ EHCI_PS_SUSP); 1860 break; 1861 case UHF_PORT_POWER: 1862 if (sc->sc_hasppc) 1863 EOWRITE4(sc, port, v &~ EHCI_PS_PP); 1864 break; 1865 case UHF_PORT_TEST: 1866 DPRINTFN(2,("ehci_root_ctrl_start: clear port test " 1867 "%d\n", index)); 1868 break; 1869 case UHF_PORT_INDICATOR: 1870 DPRINTFN(2,("ehci_root_ctrl_start: clear port ind " 1871 "%d\n", index)); 1872 EOWRITE4(sc, port, v &~ EHCI_PS_PIC); 1873 break; 1874 case UHF_C_PORT_CONNECTION: 1875 EOWRITE4(sc, port, v | EHCI_PS_CSC); 1876 break; 1877 case UHF_C_PORT_ENABLE: 1878 EOWRITE4(sc, port, v | EHCI_PS_PEC); 1879 break; 1880 case UHF_C_PORT_SUSPEND: 1881 /* how? */ 1882 break; 1883 case UHF_C_PORT_OVER_CURRENT: 1884 EOWRITE4(sc, port, v | EHCI_PS_OCC); 1885 break; 1886 case UHF_C_PORT_RESET: 1887 sc->sc_isreset[index] = 0; 1888 break; 1889 default: 1890 err = USBD_IOERROR; 1891 goto ret; 1892 } 1893 #if 0 1894 switch(value) { 1895 case UHF_C_PORT_CONNECTION: 1896 case UHF_C_PORT_ENABLE: 1897 case UHF_C_PORT_SUSPEND: 1898 case UHF_C_PORT_OVER_CURRENT: 1899 case UHF_C_PORT_RESET: 1900 /* Enable RHSC interrupt if condition is cleared. */ 1901 if ((OREAD4(sc, port) >> 16) == 0) 1902 ehci_pcd_able(sc, 1); 1903 break; 1904 default: 1905 break; 1906 } 1907 #endif 1908 break; 1909 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 1910 if ((value & 0xff) != 0) { 1911 err = USBD_IOERROR; 1912 goto ret; 1913 } 1914 hubd = ehci_hubd; 1915 hubd.bNbrPorts = sc->sc_noport; 1916 v = EOREAD4(sc, EHCI_HCSPARAMS); 1917 USETW(hubd.wHubCharacteristics, 1918 EHCI_HCS_PPC(v) ? UHD_PWR_INDIVIDUAL : UHD_PWR_NO_SWITCH | 1919 EHCI_HCS_P_INDICATOR(EREAD4(sc, EHCI_HCSPARAMS)) 1920 ? UHD_PORT_IND : 0); 1921 hubd.bPwrOn2PwrGood = 200; /* XXX can't find out? */ 1922 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8) 1923 hubd.DeviceRemovable[i++] = 0; /* XXX can't find out? */ 1924 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i; 1925 l = min(len, hubd.bDescLength); 1926 totlen = l; 1927 memcpy(buf, &hubd, l); 1928 break; 1929 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 1930 if (len != 4) { 1931 err = USBD_IOERROR; 1932 goto ret; 1933 } 1934 memset(buf, 0, len); /* ? XXX */ 1935 totlen = len; 1936 break; 1937 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 1938 DPRINTFN(8,("ehci_root_ctrl_start: get port status i=%d\n", 1939 index)); 1940 if (index < 1 || index > sc->sc_noport) { 1941 err = USBD_IOERROR; 1942 goto ret; 1943 } 1944 if (len != 4) { 1945 err = USBD_IOERROR; 1946 goto ret; 1947 } 1948 v = EOREAD4(sc, EHCI_PORTSC(index)); 1949 DPRINTFN(8,("ehci_root_ctrl_start: port status=0x%04x\n", 1950 v)); 1951 i = UPS_HIGH_SPEED; 1952 if (v & EHCI_PS_CS) i |= UPS_CURRENT_CONNECT_STATUS; 1953 if (v & EHCI_PS_PE) i |= UPS_PORT_ENABLED; 1954 if (v & EHCI_PS_SUSP) i |= UPS_SUSPEND; 1955 if (v & EHCI_PS_OCA) i |= UPS_OVERCURRENT_INDICATOR; 1956 if (v & EHCI_PS_PR) i |= UPS_RESET; 1957 if (v & EHCI_PS_PP) i |= UPS_PORT_POWER; 1958 USETW(ps.wPortStatus, i); 1959 i = 0; 1960 if (v & EHCI_PS_CSC) i |= UPS_C_CONNECT_STATUS; 1961 if (v & EHCI_PS_PEC) i |= UPS_C_PORT_ENABLED; 1962 if (v & EHCI_PS_OCC) i |= UPS_C_OVERCURRENT_INDICATOR; 1963 if (sc->sc_isreset[index]) i |= UPS_C_PORT_RESET; 1964 USETW(ps.wPortChange, i); 1965 l = min(len, sizeof ps); 1966 memcpy(buf, &ps, l); 1967 totlen = l; 1968 break; 1969 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 1970 err = USBD_IOERROR; 1971 goto ret; 1972 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 1973 break; 1974 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 1975 if (index < 1 || index > sc->sc_noport) { 1976 err = USBD_IOERROR; 1977 goto ret; 1978 } 1979 port = EHCI_PORTSC(index); 1980 v = EOREAD4(sc, port); 1981 DPRINTFN(4, ("ehci_root_ctrl_start: portsc=0x%08x\n", v)); 1982 v &= ~EHCI_PS_CLEAR; 1983 switch(value) { 1984 case UHF_PORT_ENABLE: 1985 EOWRITE4(sc, port, v | EHCI_PS_PE); 1986 break; 1987 case UHF_PORT_SUSPEND: 1988 EOWRITE4(sc, port, v | EHCI_PS_SUSP); 1989 break; 1990 case UHF_PORT_RESET: 1991 DPRINTFN(5,("ehci_root_ctrl_start: reset port %d\n", 1992 index)); 1993 if (EHCI_PS_IS_LOWSPEED(v)) { 1994 /* Low speed device, give up ownership. */ 1995 ehci_disown(sc, index, 1); 1996 break; 1997 } 1998 /* Start reset sequence. */ 1999 v &= ~ (EHCI_PS_PE | EHCI_PS_PR); 2000 EOWRITE4(sc, port, v | EHCI_PS_PR); 2001 /* Wait for reset to complete. */ 2002 usb_delay_ms(&sc->sc_bus, USB_PORT_ROOT_RESET_DELAY); 2003 if (sc->sc_dying) { 2004 err = USBD_IOERROR; 2005 goto ret; 2006 } 2007 /* Terminate reset sequence. */ 2008 EOWRITE4(sc, port, v); 2009 /* Wait for HC to complete reset. */ 2010 usb_delay_ms(&sc->sc_bus, EHCI_PORT_RESET_COMPLETE); 2011 if (sc->sc_dying) { 2012 err = USBD_IOERROR; 2013 goto ret; 2014 } 2015 v = EOREAD4(sc, port); 2016 DPRINTF(("ehci after reset, status=0x%08x\n", v)); 2017 if (v & EHCI_PS_PR) { 2018 printf("%s: port reset timeout\n", 2019 USBDEVNAME(sc->sc_bus.bdev)); 2020 return (USBD_TIMEOUT); 2021 } 2022 if (!(v & EHCI_PS_PE)) { 2023 /* Not a high speed device, give up ownership.*/ 2024 ehci_disown(sc, index, 0); 2025 break; 2026 } 2027 sc->sc_isreset[index] = 1; 2028 DPRINTF(("ehci port %d reset, status = 0x%08x\n", 2029 index, v)); 2030 break; 2031 case UHF_PORT_POWER: 2032 DPRINTFN(2,("ehci_root_ctrl_start: set port power " 2033 "%d (has PPC = %d)\n", index, 2034 sc->sc_hasppc)); 2035 if (sc->sc_hasppc) 2036 EOWRITE4(sc, port, v | EHCI_PS_PP); 2037 break; 2038 case UHF_PORT_TEST: 2039 DPRINTFN(2,("ehci_root_ctrl_start: set port test " 2040 "%d\n", index)); 2041 break; 2042 case UHF_PORT_INDICATOR: 2043 DPRINTFN(2,("ehci_root_ctrl_start: set port ind " 2044 "%d\n", index)); 2045 EOWRITE4(sc, port, v | EHCI_PS_PIC); 2046 break; 2047 default: 2048 err = USBD_IOERROR; 2049 goto ret; 2050 } 2051 break; 2052 case C(UR_CLEAR_TT_BUFFER, UT_WRITE_CLASS_OTHER): 2053 case C(UR_RESET_TT, UT_WRITE_CLASS_OTHER): 2054 case C(UR_GET_TT_STATE, UT_READ_CLASS_OTHER): 2055 case C(UR_STOP_TT, UT_WRITE_CLASS_OTHER): 2056 break; 2057 default: 2058 err = USBD_IOERROR; 2059 goto ret; 2060 } 2061 xfer->actlen = totlen; 2062 err = USBD_NORMAL_COMPLETION; 2063 ret: 2064 xfer->status = err; 2065 s = splusb(); 2066 usb_transfer_complete(xfer); 2067 splx(s); 2068 return (USBD_IN_PROGRESS); 2069 } 2070 2071 void 2072 ehci_disown(ehci_softc_t *sc, int index, int lowspeed) 2073 { 2074 int port; 2075 u_int32_t v; 2076 2077 DPRINTF(("ehci_disown: index=%d lowspeed=%d\n", index, lowspeed)); 2078 #ifdef DIAGNOSTIC 2079 if (sc->sc_npcomp != 0) { 2080 int i = (index-1) / sc->sc_npcomp; 2081 if (i >= sc->sc_ncomp) 2082 printf("%s: strange port\n", 2083 USBDEVNAME(sc->sc_bus.bdev)); 2084 else 2085 printf("%s: handing over %s speed device on " 2086 "port %d to %s\n", 2087 USBDEVNAME(sc->sc_bus.bdev), 2088 lowspeed ? "low" : "full", 2089 index, USBDEVNAME(sc->sc_comps[i]->bdev)); 2090 } else { 2091 printf("%s: npcomp == 0\n", USBDEVNAME(sc->sc_bus.bdev)); 2092 } 2093 #endif 2094 port = EHCI_PORTSC(index); 2095 v = EOREAD4(sc, port) &~ EHCI_PS_CLEAR; 2096 EOWRITE4(sc, port, v | EHCI_PS_PO); 2097 } 2098 2099 /* Abort a root control request. */ 2100 Static void 2101 ehci_root_ctrl_abort(usbd_xfer_handle xfer) 2102 { 2103 /* Nothing to do, all transfers are synchronous. */ 2104 } 2105 2106 /* Close the root pipe. */ 2107 Static void 2108 ehci_root_ctrl_close(usbd_pipe_handle pipe) 2109 { 2110 DPRINTF(("ehci_root_ctrl_close\n")); 2111 /* Nothing to do. */ 2112 } 2113 2114 void 2115 ehci_root_intr_done(usbd_xfer_handle xfer) 2116 { 2117 xfer->hcpriv = NULL; 2118 } 2119 2120 Static usbd_status 2121 ehci_root_intr_transfer(usbd_xfer_handle xfer) 2122 { 2123 usbd_status err; 2124 2125 /* Insert last in queue. */ 2126 err = usb_insert_transfer(xfer); 2127 if (err) 2128 return (err); 2129 2130 /* Pipe isn't running, start first */ 2131 return (ehci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2132 } 2133 2134 Static usbd_status 2135 ehci_root_intr_start(usbd_xfer_handle xfer) 2136 { 2137 usbd_pipe_handle pipe = xfer->pipe; 2138 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2139 2140 if (sc->sc_dying) 2141 return (USBD_IOERROR); 2142 2143 sc->sc_intrxfer = xfer; 2144 2145 return (USBD_IN_PROGRESS); 2146 } 2147 2148 /* Abort a root interrupt request. */ 2149 Static void 2150 ehci_root_intr_abort(usbd_xfer_handle xfer) 2151 { 2152 int s; 2153 2154 if (xfer->pipe->intrxfer == xfer) { 2155 DPRINTF(("ehci_root_intr_abort: remove\n")); 2156 xfer->pipe->intrxfer = NULL; 2157 } 2158 xfer->status = USBD_CANCELLED; 2159 s = splusb(); 2160 usb_transfer_complete(xfer); 2161 splx(s); 2162 } 2163 2164 /* Close the root pipe. */ 2165 Static void 2166 ehci_root_intr_close(usbd_pipe_handle pipe) 2167 { 2168 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2169 2170 DPRINTF(("ehci_root_intr_close\n")); 2171 2172 sc->sc_intrxfer = NULL; 2173 } 2174 2175 void 2176 ehci_root_ctrl_done(usbd_xfer_handle xfer) 2177 { 2178 xfer->hcpriv = NULL; 2179 } 2180 2181 /************************/ 2182 2183 ehci_soft_qh_t * 2184 ehci_alloc_sqh(ehci_softc_t *sc) 2185 { 2186 ehci_soft_qh_t *sqh; 2187 usbd_status err; 2188 int i, offs; 2189 usb_dma_t dma; 2190 2191 if (sc->sc_freeqhs == NULL) { 2192 DPRINTFN(2, ("ehci_alloc_sqh: allocating chunk\n")); 2193 err = usb_allocmem(&sc->sc_bus, EHCI_SQH_SIZE * EHCI_SQH_CHUNK, 2194 EHCI_PAGE_SIZE, &dma); 2195 #ifdef EHCI_DEBUG 2196 if (err) 2197 printf("ehci_alloc_sqh: usb_allocmem()=%d\n", err); 2198 #endif 2199 if (err) 2200 return (NULL); 2201 for(i = 0; i < EHCI_SQH_CHUNK; i++) { 2202 offs = i * EHCI_SQH_SIZE; 2203 sqh = KERNADDR(&dma, offs); 2204 sqh->physaddr = DMAADDR(&dma, offs); 2205 sqh->next = sc->sc_freeqhs; 2206 sc->sc_freeqhs = sqh; 2207 } 2208 } 2209 sqh = sc->sc_freeqhs; 2210 sc->sc_freeqhs = sqh->next; 2211 memset(&sqh->qh, 0, sizeof(ehci_qh_t)); 2212 sqh->next = NULL; 2213 return (sqh); 2214 } 2215 2216 void 2217 ehci_free_sqh(ehci_softc_t *sc, ehci_soft_qh_t *sqh) 2218 { 2219 sqh->next = sc->sc_freeqhs; 2220 sc->sc_freeqhs = sqh; 2221 } 2222 2223 ehci_soft_qtd_t * 2224 ehci_alloc_sqtd(ehci_softc_t *sc) 2225 { 2226 ehci_soft_qtd_t *sqtd; 2227 usbd_status err; 2228 int i, offs; 2229 usb_dma_t dma; 2230 int s; 2231 2232 if (sc->sc_freeqtds == NULL) { 2233 DPRINTFN(2, ("ehci_alloc_sqtd: allocating chunk\n")); 2234 err = usb_allocmem(&sc->sc_bus, EHCI_SQTD_SIZE*EHCI_SQTD_CHUNK, 2235 EHCI_PAGE_SIZE, &dma); 2236 #ifdef EHCI_DEBUG 2237 if (err) 2238 printf("ehci_alloc_sqtd: usb_allocmem()=%d\n", err); 2239 #endif 2240 if (err) 2241 return (NULL); 2242 s = splusb(); 2243 for(i = 0; i < EHCI_SQTD_CHUNK; i++) { 2244 offs = i * EHCI_SQTD_SIZE; 2245 sqtd = KERNADDR(&dma, offs); 2246 sqtd->physaddr = DMAADDR(&dma, offs); 2247 sqtd->nextqtd = sc->sc_freeqtds; 2248 sc->sc_freeqtds = sqtd; 2249 } 2250 splx(s); 2251 } 2252 2253 s = splusb(); 2254 sqtd = sc->sc_freeqtds; 2255 sc->sc_freeqtds = sqtd->nextqtd; 2256 memset(&sqtd->qtd, 0, sizeof(ehci_qtd_t)); 2257 sqtd->nextqtd = NULL; 2258 sqtd->xfer = NULL; 2259 splx(s); 2260 2261 return (sqtd); 2262 } 2263 2264 void 2265 ehci_free_sqtd(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd) 2266 { 2267 int s; 2268 2269 s = splusb(); 2270 sqtd->nextqtd = sc->sc_freeqtds; 2271 sc->sc_freeqtds = sqtd; 2272 splx(s); 2273 } 2274 2275 usbd_status 2276 ehci_alloc_sqtd_chain(struct ehci_pipe *epipe, ehci_softc_t *sc, 2277 int alen, int rd, usbd_xfer_handle xfer, 2278 ehci_soft_qtd_t **sp, ehci_soft_qtd_t **ep) 2279 { 2280 ehci_soft_qtd_t *next, *cur; 2281 ehci_physaddr_t dataphys, dataphyspage, dataphyslastpage, nextphys; 2282 u_int32_t qtdstatus; 2283 int len, curlen, mps; 2284 int i, tog; 2285 usb_dma_t *dma = &xfer->dmabuf; 2286 u_int16_t flags = xfer->flags; 2287 2288 DPRINTFN(alen<4*4096,("ehci_alloc_sqtd_chain: start len=%d\n", alen)); 2289 2290 len = alen; 2291 dataphys = DMAADDR(dma, 0); 2292 dataphyslastpage = EHCI_PAGE(dataphys + len - 1); 2293 qtdstatus = EHCI_QTD_ACTIVE | 2294 EHCI_QTD_SET_PID(rd ? EHCI_QTD_PID_IN : EHCI_QTD_PID_OUT) | 2295 EHCI_QTD_SET_CERR(3) 2296 /* IOC set below */ 2297 /* BYTES set below */ 2298 ; 2299 mps = UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize); 2300 tog = epipe->nexttoggle; 2301 qtdstatus |= EHCI_QTD_SET_TOGGLE(tog); 2302 2303 cur = ehci_alloc_sqtd(sc); 2304 *sp = cur; 2305 if (cur == NULL) 2306 goto nomem; 2307 for (;;) { 2308 dataphyspage = EHCI_PAGE(dataphys); 2309 /* The EHCI hardware can handle at most 5 pages. */ 2310 if (dataphyslastpage - dataphyspage < 2311 EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE) { 2312 /* we can handle it in this QTD */ 2313 curlen = len; 2314 } else { 2315 /* must use multiple TDs, fill as much as possible. */ 2316 curlen = EHCI_QTD_NBUFFERS * EHCI_PAGE_SIZE - 2317 EHCI_PAGE_OFFSET(dataphys); 2318 #ifdef DIAGNOSTIC 2319 if (curlen > len) { 2320 printf("ehci_alloc_sqtd_chain: curlen=0x%x " 2321 "len=0x%x offs=0x%x\n", curlen, len, 2322 EHCI_PAGE_OFFSET(dataphys)); 2323 printf("lastpage=0x%x page=0x%x phys=0x%x\n", 2324 dataphyslastpage, dataphyspage, 2325 dataphys); 2326 curlen = len; 2327 } 2328 #endif 2329 /* the length must be a multiple of the max size */ 2330 curlen -= curlen % mps; 2331 DPRINTFN(1,("ehci_alloc_sqtd_chain: multiple QTDs, " 2332 "curlen=%d\n", curlen)); 2333 #ifdef DIAGNOSTIC 2334 if (curlen == 0) 2335 panic("ehci_alloc_sqtd_chain: curlen == 0"); 2336 #endif 2337 } 2338 DPRINTFN(4,("ehci_alloc_sqtd_chain: dataphys=0x%08x " 2339 "dataphyslastpage=0x%08x len=%d curlen=%d\n", 2340 dataphys, dataphyslastpage, 2341 len, curlen)); 2342 len -= curlen; 2343 2344 /* 2345 * Allocate another transfer if there's more data left, 2346 * or if force last short transfer flag is set and we're 2347 * allocating a multiple of the max packet size. 2348 */ 2349 if (len != 0 || 2350 ((curlen % mps) == 0 && !rd && curlen != 0 && 2351 (flags & USBD_FORCE_SHORT_XFER))) { 2352 next = ehci_alloc_sqtd(sc); 2353 if (next == NULL) 2354 goto nomem; 2355 nextphys = htole32(next->physaddr); 2356 } else { 2357 next = NULL; 2358 nextphys = EHCI_NULL; 2359 } 2360 2361 for (i = 0; i * EHCI_PAGE_SIZE < 2362 curlen + EHCI_PAGE_OFFSET(dataphys); i++) { 2363 ehci_physaddr_t a = dataphys + i * EHCI_PAGE_SIZE; 2364 if (i != 0) /* use offset only in first buffer */ 2365 a = EHCI_PAGE(a); 2366 cur->qtd.qtd_buffer[i] = htole32(a); 2367 cur->qtd.qtd_buffer_hi[i] = 0; 2368 #ifdef DIAGNOSTIC 2369 if (i >= EHCI_QTD_NBUFFERS) { 2370 printf("ehci_alloc_sqtd_chain: i=%d\n", i); 2371 goto nomem; 2372 } 2373 #endif 2374 } 2375 cur->nextqtd = next; 2376 cur->qtd.qtd_next = cur->qtd.qtd_altnext = nextphys; 2377 cur->qtd.qtd_status = 2378 htole32(qtdstatus | EHCI_QTD_SET_BYTES(curlen)); 2379 cur->xfer = xfer; 2380 cur->len = curlen; 2381 DPRINTFN(10,("ehci_alloc_sqtd_chain: cbp=0x%08x end=0x%08x\n", 2382 dataphys, dataphys + curlen)); 2383 /* adjust the toggle based on the number of packets in this 2384 qtd */ 2385 if (((curlen + mps - 1) / mps) & 1) { 2386 tog ^= 1; 2387 qtdstatus ^= EHCI_QTD_TOGGLE_MASK; 2388 } 2389 if (next == NULL) 2390 break; 2391 DPRINTFN(10,("ehci_alloc_sqtd_chain: extend chain\n")); 2392 dataphys += curlen; 2393 cur = next; 2394 } 2395 cur->qtd.qtd_status |= htole32(EHCI_QTD_IOC); 2396 *ep = cur; 2397 epipe->nexttoggle = tog; 2398 2399 DPRINTFN(10,("ehci_alloc_sqtd_chain: return sqtd=%p sqtdend=%p\n", 2400 *sp, *ep)); 2401 2402 return (USBD_NORMAL_COMPLETION); 2403 2404 nomem: 2405 /* XXX free chain */ 2406 DPRINTFN(-1,("ehci_alloc_sqtd_chain: no memory\n")); 2407 return (USBD_NOMEM); 2408 } 2409 2410 Static void 2411 ehci_free_sqtd_chain(ehci_softc_t *sc, ehci_soft_qtd_t *sqtd, 2412 ehci_soft_qtd_t *sqtdend) 2413 { 2414 ehci_soft_qtd_t *p; 2415 int i; 2416 2417 DPRINTFN(10,("ehci_free_sqtd_chain: sqtd=%p sqtdend=%p\n", 2418 sqtd, sqtdend)); 2419 2420 for (i = 0; sqtd != sqtdend; sqtd = p, i++) { 2421 p = sqtd->nextqtd; 2422 ehci_free_sqtd(sc, sqtd); 2423 } 2424 } 2425 2426 /****************/ 2427 2428 /* 2429 * Close a reqular pipe. 2430 * Assumes that there are no pending transactions. 2431 */ 2432 void 2433 ehci_close_pipe(usbd_pipe_handle pipe, ehci_soft_qh_t *head) 2434 { 2435 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 2436 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2437 ehci_soft_qh_t *sqh = epipe->sqh; 2438 int s; 2439 2440 s = splusb(); 2441 ehci_rem_qh(sc, sqh, head); 2442 splx(s); 2443 ehci_free_sqh(sc, epipe->sqh); 2444 } 2445 2446 /* 2447 * Abort a device request. 2448 * If this routine is called at splusb() it guarantees that the request 2449 * will be removed from the hardware scheduling and that the callback 2450 * for it will be called with USBD_CANCELLED status. 2451 * It's impossible to guarantee that the requested transfer will not 2452 * have happened since the hardware runs concurrently. 2453 * If the transaction has already happened we rely on the ordinary 2454 * interrupt processing to process it. 2455 * XXX This is most probably wrong. 2456 */ 2457 void 2458 ehci_abort_xfer(usbd_xfer_handle xfer, usbd_status status) 2459 { 2460 #define exfer EXFER(xfer) 2461 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 2462 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus; 2463 ehci_soft_qh_t *sqh = epipe->sqh; 2464 ehci_soft_qtd_t *sqtd; 2465 ehci_physaddr_t cur; 2466 u_int32_t qhstatus; 2467 int s; 2468 int hit; 2469 int wake; 2470 2471 DPRINTF(("ehci_abort_xfer: xfer=%p pipe=%p\n", xfer, epipe)); 2472 2473 if (sc->sc_dying) { 2474 /* If we're dying, just do the software part. */ 2475 s = splusb(); 2476 xfer->status = status; /* make software ignore it */ 2477 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer); 2478 usb_transfer_complete(xfer); 2479 splx(s); 2480 return; 2481 } 2482 2483 if (xfer->device->bus->intr_context || !curproc) 2484 panic("ehci_abort_xfer: not in process context"); 2485 2486 /* 2487 * If an abort is already in progress then just wait for it to 2488 * complete and return. 2489 */ 2490 if (xfer->hcflags & UXFER_ABORTING) { 2491 DPRINTFN(2, ("ehci_abort_xfer: already aborting\n")); 2492 #ifdef DIAGNOSTIC 2493 if (status == USBD_TIMEOUT) 2494 printf("ehci_abort_xfer: TIMEOUT while aborting\n"); 2495 #endif 2496 /* Override the status which might be USBD_TIMEOUT. */ 2497 xfer->status = status; 2498 DPRINTFN(2, ("ehci_abort_xfer: waiting for abort to finish\n")); 2499 xfer->hcflags |= UXFER_ABORTWAIT; 2500 while (xfer->hcflags & UXFER_ABORTING) 2501 tsleep(&xfer->hcflags, PZERO, "ehciaw", 0); 2502 return; 2503 } 2504 xfer->hcflags |= UXFER_ABORTING; 2505 2506 /* 2507 * Step 1: Make interrupt routine and hardware ignore xfer. 2508 */ 2509 s = splusb(); 2510 xfer->status = status; /* make software ignore it */ 2511 usb_uncallout(xfer->timeout_handle, ehci_timeout, xfer); 2512 qhstatus = sqh->qh.qh_qtd.qtd_status; 2513 sqh->qh.qh_qtd.qtd_status = qhstatus | htole32(EHCI_QTD_HALTED); 2514 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) { 2515 sqtd->qtd.qtd_status |= htole32(EHCI_QTD_HALTED); 2516 if (sqtd == exfer->sqtdend) 2517 break; 2518 } 2519 splx(s); 2520 2521 /* 2522 * Step 2: Wait until we know hardware has finished any possible 2523 * use of the xfer. Also make sure the soft interrupt routine 2524 * has run. 2525 */ 2526 ehci_sync_hc(sc); 2527 s = splusb(); 2528 #ifdef USB_USE_SOFTINTR 2529 sc->sc_softwake = 1; 2530 #endif /* USB_USE_SOFTINTR */ 2531 usb_schedsoftintr(&sc->sc_bus); 2532 #ifdef USB_USE_SOFTINTR 2533 tsleep(&sc->sc_softwake, PZERO, "ehciab", 0); 2534 #endif /* USB_USE_SOFTINTR */ 2535 splx(s); 2536 2537 /* 2538 * Step 3: Remove any vestiges of the xfer from the hardware. 2539 * The complication here is that the hardware may have executed 2540 * beyond the xfer we're trying to abort. So as we're scanning 2541 * the TDs of this xfer we check if the hardware points to 2542 * any of them. 2543 */ 2544 s = splusb(); /* XXX why? */ 2545 cur = EHCI_LINK_ADDR(le32toh(sqh->qh.qh_curqtd)); 2546 hit = 0; 2547 for (sqtd = exfer->sqtdstart; ; sqtd = sqtd->nextqtd) { 2548 hit |= cur == sqtd->physaddr; 2549 if (sqtd == exfer->sqtdend) 2550 break; 2551 } 2552 sqtd = sqtd->nextqtd; 2553 /* Zap curqtd register if hardware pointed inside the xfer. */ 2554 if (hit && sqtd != NULL) { 2555 DPRINTFN(1,("ehci_abort_xfer: cur=0x%08x\n", sqtd->physaddr)); 2556 sqh->qh.qh_curqtd = htole32(sqtd->physaddr); /* unlink qTDs */ 2557 sqh->qh.qh_qtd.qtd_status = qhstatus; 2558 } else { 2559 DPRINTFN(1,("ehci_abort_xfer: no hit\n")); 2560 } 2561 2562 /* 2563 * Step 4: Execute callback. 2564 */ 2565 #ifdef DIAGNOSTIC 2566 exfer->isdone = 1; 2567 #endif 2568 wake = xfer->hcflags & UXFER_ABORTWAIT; 2569 xfer->hcflags &= ~(UXFER_ABORTING | UXFER_ABORTWAIT); 2570 usb_transfer_complete(xfer); 2571 if (wake) 2572 wakeup(&xfer->hcflags); 2573 2574 splx(s); 2575 #undef exfer 2576 } 2577 2578 void 2579 ehci_timeout(void *addr) 2580 { 2581 struct ehci_xfer *exfer = addr; 2582 struct ehci_pipe *epipe = (struct ehci_pipe *)exfer->xfer.pipe; 2583 ehci_softc_t *sc = (ehci_softc_t *)epipe->pipe.device->bus; 2584 2585 DPRINTF(("ehci_timeout: exfer=%p\n", exfer)); 2586 #ifdef USB_DEBUG 2587 if (ehcidebug > 1) 2588 usbd_dump_pipe(exfer->xfer.pipe); 2589 #endif 2590 2591 if (sc->sc_dying) { 2592 ehci_abort_xfer(&exfer->xfer, USBD_TIMEOUT); 2593 return; 2594 } 2595 2596 /* Execute the abort in a process context. */ 2597 usb_init_task(&exfer->abort_task, ehci_timeout_task, addr); 2598 usb_add_task(exfer->xfer.pipe->device, &exfer->abort_task); 2599 } 2600 2601 void 2602 ehci_timeout_task(void *addr) 2603 { 2604 usbd_xfer_handle xfer = addr; 2605 int s; 2606 2607 DPRINTF(("ehci_timeout_task: xfer=%p\n", xfer)); 2608 2609 s = splusb(); 2610 ehci_abort_xfer(xfer, USBD_TIMEOUT); 2611 splx(s); 2612 } 2613 2614 /************************/ 2615 2616 Static usbd_status 2617 ehci_device_ctrl_transfer(usbd_xfer_handle xfer) 2618 { 2619 usbd_status err; 2620 2621 /* Insert last in queue. */ 2622 err = usb_insert_transfer(xfer); 2623 if (err) 2624 return (err); 2625 2626 /* Pipe isn't running, start first */ 2627 return (ehci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2628 } 2629 2630 Static usbd_status 2631 ehci_device_ctrl_start(usbd_xfer_handle xfer) 2632 { 2633 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 2634 usbd_status err; 2635 2636 if (sc->sc_dying) 2637 return (USBD_IOERROR); 2638 2639 #ifdef DIAGNOSTIC 2640 if (!(xfer->rqflags & URQ_REQUEST)) { 2641 /* XXX panic */ 2642 printf("ehci_device_ctrl_transfer: not a request\n"); 2643 return (USBD_INVAL); 2644 } 2645 #endif 2646 2647 err = ehci_device_request(xfer); 2648 if (err) 2649 return (err); 2650 2651 if (sc->sc_bus.use_polling) 2652 ehci_waitintr(sc, xfer); 2653 return (USBD_IN_PROGRESS); 2654 } 2655 2656 void 2657 ehci_device_ctrl_done(usbd_xfer_handle xfer) 2658 { 2659 struct ehci_xfer *ex = EXFER(xfer); 2660 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 2661 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/ 2662 2663 DPRINTFN(10,("ehci_ctrl_done: xfer=%p\n", xfer)); 2664 2665 #ifdef DIAGNOSTIC 2666 if (!(xfer->rqflags & URQ_REQUEST)) { 2667 panic("ehci_ctrl_done: not a request"); 2668 } 2669 #endif 2670 2671 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 2672 ehci_del_intr_list(ex); /* remove from active list */ 2673 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 2674 } 2675 2676 DPRINTFN(5, ("ehci_ctrl_done: length=%d\n", xfer->actlen)); 2677 } 2678 2679 /* Abort a device control request. */ 2680 Static void 2681 ehci_device_ctrl_abort(usbd_xfer_handle xfer) 2682 { 2683 DPRINTF(("ehci_device_ctrl_abort: xfer=%p\n", xfer)); 2684 ehci_abort_xfer(xfer, USBD_CANCELLED); 2685 } 2686 2687 /* Close a device control pipe. */ 2688 Static void 2689 ehci_device_ctrl_close(usbd_pipe_handle pipe) 2690 { 2691 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2692 /*struct ehci_pipe *epipe = (struct ehci_pipe *)pipe;*/ 2693 2694 DPRINTF(("ehci_device_ctrl_close: pipe=%p\n", pipe)); 2695 ehci_close_pipe(pipe, sc->sc_async_head); 2696 } 2697 2698 usbd_status 2699 ehci_device_request(usbd_xfer_handle xfer) 2700 { 2701 #define exfer EXFER(xfer) 2702 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 2703 usb_device_request_t *req = &xfer->request; 2704 usbd_device_handle dev = epipe->pipe.device; 2705 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 2706 int addr = dev->address; 2707 ehci_soft_qtd_t *setup, *stat, *next; 2708 ehci_soft_qh_t *sqh; 2709 int isread; 2710 int len; 2711 usbd_status err; 2712 int s; 2713 2714 isread = req->bmRequestType & UT_READ; 2715 len = UGETW(req->wLength); 2716 2717 DPRINTFN(3,("ehci_device_request: type=0x%02x, request=0x%02x, " 2718 "wValue=0x%04x, wIndex=0x%04x len=%d, addr=%d, endpt=%d\n", 2719 req->bmRequestType, req->bRequest, UGETW(req->wValue), 2720 UGETW(req->wIndex), len, addr, 2721 epipe->pipe.endpoint->edesc->bEndpointAddress)); 2722 2723 setup = ehci_alloc_sqtd(sc); 2724 if (setup == NULL) { 2725 err = USBD_NOMEM; 2726 goto bad1; 2727 } 2728 stat = ehci_alloc_sqtd(sc); 2729 if (stat == NULL) { 2730 err = USBD_NOMEM; 2731 goto bad2; 2732 } 2733 2734 sqh = epipe->sqh; 2735 epipe->u.ctl.length = len; 2736 2737 /* Update device address and length since they may have changed 2738 during the setup of the control pipe in usbd_new_device(). */ 2739 /* XXX This only needs to be done once, but it's too early in open. */ 2740 /* XXXX Should not touch ED here! */ 2741 sqh->qh.qh_endp = 2742 (sqh->qh.qh_endp & htole32(~(EHCI_QH_ADDRMASK | EHCI_QH_MPLMASK))) | 2743 htole32( 2744 EHCI_QH_SET_ADDR(addr) | 2745 EHCI_QH_SET_MPL(UGETW(epipe->pipe.endpoint->edesc->wMaxPacketSize)) 2746 ); 2747 2748 /* Set up data transaction */ 2749 if (len != 0) { 2750 ehci_soft_qtd_t *end; 2751 2752 /* Start toggle at 1. */ 2753 epipe->nexttoggle = 1; 2754 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, 2755 &next, &end); 2756 if (err) 2757 goto bad3; 2758 end->qtd.qtd_status &= htole32(~EHCI_QTD_IOC); 2759 end->nextqtd = stat; 2760 end->qtd.qtd_next = 2761 end->qtd.qtd_altnext = htole32(stat->physaddr); 2762 } else { 2763 next = stat; 2764 } 2765 2766 memcpy(KERNADDR(&epipe->u.ctl.reqdma, 0), req, sizeof *req); 2767 2768 /* Clear toggle */ 2769 setup->qtd.qtd_status = htole32( 2770 EHCI_QTD_ACTIVE | 2771 EHCI_QTD_SET_PID(EHCI_QTD_PID_SETUP) | 2772 EHCI_QTD_SET_CERR(3) | 2773 EHCI_QTD_SET_TOGGLE(0) | 2774 EHCI_QTD_SET_BYTES(sizeof *req) 2775 ); 2776 setup->qtd.qtd_buffer[0] = htole32(DMAADDR(&epipe->u.ctl.reqdma, 0)); 2777 setup->qtd.qtd_buffer_hi[0] = 0; 2778 setup->nextqtd = next; 2779 setup->qtd.qtd_next = setup->qtd.qtd_altnext = htole32(next->physaddr); 2780 setup->xfer = xfer; 2781 setup->len = sizeof *req; 2782 2783 stat->qtd.qtd_status = htole32( 2784 EHCI_QTD_ACTIVE | 2785 EHCI_QTD_SET_PID(isread ? EHCI_QTD_PID_OUT : EHCI_QTD_PID_IN) | 2786 EHCI_QTD_SET_CERR(3) | 2787 EHCI_QTD_SET_TOGGLE(1) | 2788 EHCI_QTD_IOC 2789 ); 2790 stat->qtd.qtd_buffer[0] = 0; /* XXX not needed? */ 2791 stat->qtd.qtd_buffer_hi[0] = 0; /* XXX not needed? */ 2792 stat->nextqtd = NULL; 2793 stat->qtd.qtd_next = stat->qtd.qtd_altnext = EHCI_NULL; 2794 stat->xfer = xfer; 2795 stat->len = 0; 2796 2797 #ifdef EHCI_DEBUG 2798 if (ehcidebug > 5) { 2799 DPRINTF(("ehci_device_request:\n")); 2800 ehci_dump_sqh(sqh); 2801 ehci_dump_sqtds(setup); 2802 } 2803 #endif 2804 2805 exfer->sqtdstart = setup; 2806 exfer->sqtdend = stat; 2807 #ifdef DIAGNOSTIC 2808 if (!exfer->isdone) { 2809 printf("ehci_device_request: not done, exfer=%p\n", exfer); 2810 } 2811 exfer->isdone = 0; 2812 #endif 2813 2814 /* Insert qTD in QH list. */ 2815 s = splusb(); 2816 ehci_set_qh_qtd(sqh, setup); 2817 if (xfer->timeout && !sc->sc_bus.use_polling) { 2818 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout), 2819 ehci_timeout, xfer); 2820 } 2821 ehci_add_intr_list(sc, exfer); 2822 xfer->status = USBD_IN_PROGRESS; 2823 splx(s); 2824 2825 #ifdef EHCI_DEBUG 2826 if (ehcidebug > 10) { 2827 DPRINTF(("ehci_device_request: status=%x\n", 2828 EOREAD4(sc, EHCI_USBSTS))); 2829 delay(10000); 2830 ehci_dump_regs(sc); 2831 ehci_dump_sqh(sc->sc_async_head); 2832 ehci_dump_sqh(sqh); 2833 ehci_dump_sqtds(setup); 2834 } 2835 #endif 2836 2837 return (USBD_NORMAL_COMPLETION); 2838 2839 bad3: 2840 ehci_free_sqtd(sc, stat); 2841 bad2: 2842 ehci_free_sqtd(sc, setup); 2843 bad1: 2844 DPRINTFN(-1,("ehci_device_request: no memory\n")); 2845 xfer->status = err; 2846 usb_transfer_complete(xfer); 2847 return (err); 2848 #undef exfer 2849 } 2850 2851 /* 2852 * Some EHCI chips from VIA seem to trigger interrupts before writing back the 2853 * qTD status, or miss signalling occasionally under heavy load. If the host 2854 * machine is too fast, we we can miss transaction completion - when we scan 2855 * the active list the transaction still seems to be active. This generally 2856 * exhibits itself as a umass stall that never recovers. 2857 * 2858 * We work around this behaviour by setting up this callback after any softintr 2859 * that completes with transactions still pending, giving us another chance to 2860 * check for completion after the writeback has taken place. 2861 */ 2862 void 2863 ehci_intrlist_timeout(void *arg) 2864 { 2865 ehci_softc_t *sc = arg; 2866 int s = splusb(); 2867 2868 DPRINTF(("ehci_intrlist_timeout\n")); 2869 usb_schedsoftintr(&sc->sc_bus); 2870 2871 splx(s); 2872 } 2873 2874 /************************/ 2875 2876 Static usbd_status 2877 ehci_device_bulk_transfer(usbd_xfer_handle xfer) 2878 { 2879 usbd_status err; 2880 2881 /* Insert last in queue. */ 2882 err = usb_insert_transfer(xfer); 2883 if (err) 2884 return (err); 2885 2886 /* Pipe isn't running, start first */ 2887 return (ehci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2888 } 2889 2890 usbd_status 2891 ehci_device_bulk_start(usbd_xfer_handle xfer) 2892 { 2893 #define exfer EXFER(xfer) 2894 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 2895 usbd_device_handle dev = epipe->pipe.device; 2896 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 2897 ehci_soft_qtd_t *data, *dataend; 2898 ehci_soft_qh_t *sqh; 2899 usbd_status err; 2900 int len, isread, endpt; 2901 int s; 2902 2903 DPRINTFN(2, ("ehci_device_bulk_start: xfer=%p len=%d flags=%d\n", 2904 xfer, xfer->length, xfer->flags)); 2905 2906 if (sc->sc_dying) 2907 return (USBD_IOERROR); 2908 2909 #ifdef DIAGNOSTIC 2910 if (xfer->rqflags & URQ_REQUEST) 2911 panic("ehci_device_bulk_start: a request"); 2912 #endif 2913 2914 len = xfer->length; 2915 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 2916 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2917 sqh = epipe->sqh; 2918 2919 epipe->u.bulk.length = len; 2920 2921 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data, 2922 &dataend); 2923 if (err) { 2924 DPRINTFN(-1,("ehci_device_bulk_transfer: no memory\n")); 2925 xfer->status = err; 2926 usb_transfer_complete(xfer); 2927 return (err); 2928 } 2929 2930 #ifdef EHCI_DEBUG 2931 if (ehcidebug > 5) { 2932 DPRINTF(("ehci_device_bulk_start: data(1)\n")); 2933 ehci_dump_sqh(sqh); 2934 ehci_dump_sqtds(data); 2935 } 2936 #endif 2937 2938 /* Set up interrupt info. */ 2939 exfer->sqtdstart = data; 2940 exfer->sqtdend = dataend; 2941 #ifdef DIAGNOSTIC 2942 if (!exfer->isdone) { 2943 printf("ehci_device_bulk_start: not done, ex=%p\n", exfer); 2944 } 2945 exfer->isdone = 0; 2946 #endif 2947 2948 s = splusb(); 2949 ehci_set_qh_qtd(sqh, data); 2950 if (xfer->timeout && !sc->sc_bus.use_polling) { 2951 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout), 2952 ehci_timeout, xfer); 2953 } 2954 ehci_add_intr_list(sc, exfer); 2955 xfer->status = USBD_IN_PROGRESS; 2956 splx(s); 2957 2958 #ifdef EHCI_DEBUG 2959 if (ehcidebug > 10) { 2960 DPRINTF(("ehci_device_bulk_start: data(2)\n")); 2961 delay(10000); 2962 DPRINTF(("ehci_device_bulk_start: data(3)\n")); 2963 ehci_dump_regs(sc); 2964 #if 0 2965 printf("async_head:\n"); 2966 ehci_dump_sqh(sc->sc_async_head); 2967 #endif 2968 printf("sqh:\n"); 2969 ehci_dump_sqh(sqh); 2970 ehci_dump_sqtds(data); 2971 } 2972 #endif 2973 2974 if (sc->sc_bus.use_polling) 2975 ehci_waitintr(sc, xfer); 2976 2977 return (USBD_IN_PROGRESS); 2978 #undef exfer 2979 } 2980 2981 Static void 2982 ehci_device_bulk_abort(usbd_xfer_handle xfer) 2983 { 2984 DPRINTF(("ehci_device_bulk_abort: xfer=%p\n", xfer)); 2985 ehci_abort_xfer(xfer, USBD_CANCELLED); 2986 } 2987 2988 /* 2989 * Close a device bulk pipe. 2990 */ 2991 Static void 2992 ehci_device_bulk_close(usbd_pipe_handle pipe) 2993 { 2994 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 2995 2996 DPRINTF(("ehci_device_bulk_close: pipe=%p\n", pipe)); 2997 ehci_close_pipe(pipe, sc->sc_async_head); 2998 } 2999 3000 void 3001 ehci_device_bulk_done(usbd_xfer_handle xfer) 3002 { 3003 struct ehci_xfer *ex = EXFER(xfer); 3004 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3005 /*struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe;*/ 3006 3007 DPRINTFN(10,("ehci_bulk_done: xfer=%p, actlen=%d\n", 3008 xfer, xfer->actlen)); 3009 3010 if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 3011 ehci_del_intr_list(ex); /* remove from active list */ 3012 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3013 } 3014 3015 DPRINTFN(5, ("ehci_bulk_done: length=%d\n", xfer->actlen)); 3016 } 3017 3018 /************************/ 3019 3020 Static usbd_status 3021 ehci_device_setintr(ehci_softc_t *sc, ehci_soft_qh_t *sqh, int ival) 3022 { 3023 struct ehci_soft_islot *isp; 3024 int islot, lev; 3025 3026 /* Find a poll rate that is large enough. */ 3027 for (lev = EHCI_IPOLLRATES - 1; lev > 0; lev--) 3028 if (EHCI_ILEV_IVAL(lev) <= ival) 3029 break; 3030 3031 /* Pick an interrupt slot at the right level. */ 3032 /* XXX could do better than picking at random */ 3033 sc->sc_rand = (sc->sc_rand + 191) % sc->sc_flsize; 3034 islot = EHCI_IQHIDX(lev, sc->sc_rand); 3035 3036 sqh->islot = islot; 3037 isp = &sc->sc_islots[islot]; 3038 ehci_add_qh(sqh, isp->sqh); 3039 3040 return (USBD_NORMAL_COMPLETION); 3041 } 3042 3043 Static usbd_status 3044 ehci_device_intr_transfer(usbd_xfer_handle xfer) 3045 { 3046 usbd_status err; 3047 3048 /* Insert last in queue. */ 3049 err = usb_insert_transfer(xfer); 3050 if (err) 3051 return (err); 3052 3053 /* 3054 * Pipe isn't running (otherwise err would be USBD_INPROG), 3055 * so start it first. 3056 */ 3057 return (ehci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 3058 } 3059 3060 Static usbd_status 3061 ehci_device_intr_start(usbd_xfer_handle xfer) 3062 { 3063 #define exfer EXFER(xfer) 3064 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3065 usbd_device_handle dev = xfer->pipe->device; 3066 ehci_softc_t *sc = (ehci_softc_t *)dev->bus; 3067 ehci_soft_qtd_t *data, *dataend; 3068 ehci_soft_qh_t *sqh; 3069 usbd_status err; 3070 int len, isread, endpt; 3071 int s; 3072 3073 DPRINTFN(2, ("ehci_device_intr_start: xfer=%p len=%d flags=%d\n", 3074 xfer, xfer->length, xfer->flags)); 3075 3076 if (sc->sc_dying) 3077 return (USBD_IOERROR); 3078 3079 #ifdef DIAGNOSTIC 3080 if (xfer->rqflags & URQ_REQUEST) 3081 panic("ehci_device_intr_start: a request"); 3082 #endif 3083 3084 len = xfer->length; 3085 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 3086 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3087 sqh = epipe->sqh; 3088 3089 epipe->u.intr.length = len; 3090 3091 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, &data, 3092 &dataend); 3093 if (err) { 3094 DPRINTFN(-1, ("ehci_device_intr_start: no memory\n")); 3095 xfer->status = err; 3096 usb_transfer_complete(xfer); 3097 return (err); 3098 } 3099 3100 #ifdef EHCI_DEBUG 3101 if (ehcidebug > 5) { 3102 DPRINTF(("ehci_device_intr_start: data(1)\n")); 3103 ehci_dump_sqh(sqh); 3104 ehci_dump_sqtds(data); 3105 } 3106 #endif 3107 3108 /* Set up interrupt info. */ 3109 exfer->sqtdstart = data; 3110 exfer->sqtdend = dataend; 3111 #ifdef DIAGNOSTIC 3112 if (!exfer->isdone) { 3113 printf("ehci_device_intr_start: not done, ex=%p\n", exfer); 3114 } 3115 exfer->isdone = 0; 3116 #endif 3117 3118 s = splusb(); 3119 ehci_set_qh_qtd(sqh, data); 3120 if (xfer->timeout && !sc->sc_bus.use_polling) { 3121 usb_callout(xfer->timeout_handle, mstohz(xfer->timeout), 3122 ehci_timeout, xfer); 3123 } 3124 ehci_add_intr_list(sc, exfer); 3125 xfer->status = USBD_IN_PROGRESS; 3126 splx(s); 3127 3128 #ifdef EHCI_DEBUG 3129 if (ehcidebug > 10) { 3130 DPRINTF(("ehci_device_intr_start: data(2)\n")); 3131 delay(10000); 3132 DPRINTF(("ehci_device_intr_start: data(3)\n")); 3133 ehci_dump_regs(sc); 3134 printf("sqh:\n"); 3135 ehci_dump_sqh(sqh); 3136 ehci_dump_sqtds(data); 3137 } 3138 #endif 3139 3140 if (sc->sc_bus.use_polling) 3141 ehci_waitintr(sc, xfer); 3142 3143 return (USBD_IN_PROGRESS); 3144 #undef exfer 3145 } 3146 3147 Static void 3148 ehci_device_intr_abort(usbd_xfer_handle xfer) 3149 { 3150 DPRINTFN(1, ("ehci_device_intr_abort: xfer=%p\n", xfer)); 3151 if (xfer->pipe->intrxfer == xfer) { 3152 DPRINTFN(1, ("echi_device_intr_abort: remove\n")); 3153 xfer->pipe->intrxfer = NULL; 3154 } 3155 ehci_abort_xfer(xfer, USBD_CANCELLED); 3156 } 3157 3158 Static void 3159 ehci_device_intr_close(usbd_pipe_handle pipe) 3160 { 3161 ehci_softc_t *sc = (ehci_softc_t *)pipe->device->bus; 3162 struct ehci_pipe *epipe = (struct ehci_pipe *)pipe; 3163 struct ehci_soft_islot *isp; 3164 3165 isp = &sc->sc_islots[epipe->sqh->islot]; 3166 ehci_close_pipe(pipe, isp->sqh); 3167 } 3168 3169 Static void 3170 ehci_device_intr_done(usbd_xfer_handle xfer) 3171 { 3172 #define exfer EXFER(xfer) 3173 struct ehci_xfer *ex = EXFER(xfer); 3174 ehci_softc_t *sc = (ehci_softc_t *)xfer->pipe->device->bus; 3175 struct ehci_pipe *epipe = (struct ehci_pipe *)xfer->pipe; 3176 ehci_soft_qtd_t *data, *dataend; 3177 ehci_soft_qh_t *sqh; 3178 usbd_status err; 3179 int len, isread, endpt, s; 3180 3181 DPRINTFN(10, ("ehci_device_intr_done: xfer=%p, actlen=%d\n", 3182 xfer, xfer->actlen)); 3183 3184 if (xfer->pipe->repeat) { 3185 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3186 3187 len = epipe->u.intr.length; 3188 xfer->length = len; 3189 endpt = epipe->pipe.endpoint->edesc->bEndpointAddress; 3190 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3191 sqh = epipe->sqh; 3192 3193 err = ehci_alloc_sqtd_chain(epipe, sc, len, isread, xfer, 3194 &data, &dataend); 3195 if (err) { 3196 DPRINTFN(-1, ("ehci_device_intr_done: no memory\n")); 3197 xfer->status = err; 3198 return; 3199 } 3200 3201 /* Set up interrupt info. */ 3202 exfer->sqtdstart = data; 3203 exfer->sqtdend = dataend; 3204 #ifdef DIAGNOSTIC 3205 if (!exfer->isdone) { 3206 printf("ehci_device_intr_done: not done, ex=%p\n", 3207 exfer); 3208 } 3209 exfer->isdone = 0; 3210 #endif 3211 3212 s = splusb(); 3213 ehci_set_qh_qtd(sqh, data); 3214 if (xfer->timeout && !sc->sc_bus.use_polling) { 3215 usb_callout(xfer->timeout_handle, 3216 mstohz(xfer->timeout), ehci_timeout, xfer); 3217 } 3218 splx(s); 3219 3220 xfer->status = USBD_IN_PROGRESS; 3221 } else if (xfer->status != USBD_NOMEM && ehci_active_intr_list(ex)) { 3222 ehci_del_intr_list(ex); /* remove from active list */ 3223 ehci_free_sqtd_chain(sc, ex->sqtdstart, NULL); 3224 } 3225 #undef exfer 3226 } 3227 3228 /************************/ 3229 3230 Static usbd_status ehci_device_isoc_transfer(usbd_xfer_handle xfer) { return USBD_IOERROR; } 3231 Static usbd_status ehci_device_isoc_start(usbd_xfer_handle xfer) { return USBD_IOERROR; } 3232 Static void ehci_device_isoc_abort(usbd_xfer_handle xfer) { } 3233 Static void ehci_device_isoc_close(usbd_pipe_handle pipe) { } 3234 Static void ehci_device_isoc_done(usbd_xfer_handle xfer) { } 3235