1 /* $OpenBSD: ohci.c,v 1.93 2009/11/04 19:14:10 kettenis Exp $ */ 2 /* $NetBSD: ohci.c,v 1.139 2003/02/22 05:24:16 tsutsui Exp $ */ 3 /* $FreeBSD: src/sys/dev/usb/ohci.c,v 1.22 1999/11/17 22:33:40 n_hibma Exp $ */ 4 5 /* 6 * Copyright (c) 1998 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Lennart Augustsson (lennart@augustsson.net) at 11 * Carlstedt Research & Technology. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * USB Open Host Controller driver. 37 * 38 * OHCI spec: http://www.compaq.com/productinfo/development/openhci.html 39 * USB spec: http://www.usb.org/developers/docs/usbspec.zip 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/malloc.h> 45 #include <sys/kernel.h> 46 #include <sys/device.h> 47 #include <sys/selinfo.h> 48 #include <sys/proc.h> 49 #include <sys/queue.h> 50 51 #include <machine/bus.h> 52 #include <machine/endian.h> 53 54 #include <dev/usb/usb.h> 55 #include <dev/usb/usbdi.h> 56 #include <dev/usb/usbdivar.h> 57 #include <dev/usb/usb_mem.h> 58 #include <dev/usb/usb_quirks.h> 59 60 #include <dev/usb/ohcireg.h> 61 #include <dev/usb/ohcivar.h> 62 63 struct cfdriver ohci_cd = { 64 NULL, "ohci", DV_DULL 65 }; 66 67 #ifdef OHCI_DEBUG 68 #define DPRINTF(x) do { if (ohcidebug) printf x; } while (0) 69 #define DPRINTFN(n,x) do { if (ohcidebug>(n)) printf x; } while (0) 70 int ohcidebug = 0; 71 #define bitmask_snprintf(q,f,b,l) snprintf((b), (l), "%b", (q), (f)) 72 #else 73 #define DPRINTF(x) 74 #define DPRINTFN(n,x) 75 #endif 76 77 #define mstohz(ms) ((ms) * hz / 1000) 78 79 /* 80 * The OHCI controller is little endian, so on big endian machines 81 * the data stored in memory needs to be swapped. 82 */ 83 84 struct ohci_pipe; 85 86 ohci_soft_ed_t *ohci_alloc_sed(ohci_softc_t *); 87 void ohci_free_sed(ohci_softc_t *, ohci_soft_ed_t *); 88 89 ohci_soft_td_t *ohci_alloc_std(ohci_softc_t *); 90 void ohci_free_std(ohci_softc_t *, ohci_soft_td_t *); 91 92 ohci_soft_itd_t *ohci_alloc_sitd(ohci_softc_t *); 93 void ohci_free_sitd(ohci_softc_t *,ohci_soft_itd_t *); 94 95 #if 0 96 void ohci_free_std_chain(ohci_softc_t *, ohci_soft_td_t *, 97 ohci_soft_td_t *); 98 #endif 99 usbd_status ohci_alloc_std_chain(struct ohci_pipe *, 100 ohci_softc_t *, u_int, int, usbd_xfer_handle, 101 ohci_soft_td_t *, ohci_soft_td_t **); 102 103 void ohci_shutdown(void *v); 104 usbd_status ohci_open(usbd_pipe_handle); 105 void ohci_poll(struct usbd_bus *); 106 void ohci_softintr(void *); 107 void ohci_waitintr(ohci_softc_t *, usbd_xfer_handle); 108 void ohci_add_done(ohci_softc_t *, ohci_physaddr_t); 109 void ohci_rhsc(ohci_softc_t *, usbd_xfer_handle); 110 111 usbd_status ohci_device_request(usbd_xfer_handle xfer); 112 void ohci_add_ed(ohci_soft_ed_t *, ohci_soft_ed_t *); 113 void ohci_rem_ed(ohci_soft_ed_t *, ohci_soft_ed_t *); 114 void ohci_hash_add_td(ohci_softc_t *, ohci_soft_td_t *); 115 void ohci_hash_rem_td(ohci_softc_t *, ohci_soft_td_t *); 116 ohci_soft_td_t *ohci_hash_find_td(ohci_softc_t *, ohci_physaddr_t); 117 void ohci_hash_add_itd(ohci_softc_t *, ohci_soft_itd_t *); 118 void ohci_hash_rem_itd(ohci_softc_t *, ohci_soft_itd_t *); 119 ohci_soft_itd_t *ohci_hash_find_itd(ohci_softc_t *, ohci_physaddr_t); 120 121 usbd_status ohci_setup_isoc(usbd_pipe_handle pipe); 122 void ohci_device_isoc_enter(usbd_xfer_handle); 123 124 usbd_status ohci_allocm(struct usbd_bus *, usb_dma_t *, u_int32_t); 125 void ohci_freem(struct usbd_bus *, usb_dma_t *); 126 127 usbd_xfer_handle ohci_allocx(struct usbd_bus *); 128 void ohci_freex(struct usbd_bus *, usbd_xfer_handle); 129 130 usbd_status ohci_root_ctrl_transfer(usbd_xfer_handle); 131 usbd_status ohci_root_ctrl_start(usbd_xfer_handle); 132 void ohci_root_ctrl_abort(usbd_xfer_handle); 133 void ohci_root_ctrl_close(usbd_pipe_handle); 134 void ohci_root_ctrl_done(usbd_xfer_handle); 135 136 usbd_status ohci_root_intr_transfer(usbd_xfer_handle); 137 usbd_status ohci_root_intr_start(usbd_xfer_handle); 138 void ohci_root_intr_abort(usbd_xfer_handle); 139 void ohci_root_intr_close(usbd_pipe_handle); 140 void ohci_root_intr_done(usbd_xfer_handle); 141 142 usbd_status ohci_device_ctrl_transfer(usbd_xfer_handle); 143 usbd_status ohci_device_ctrl_start(usbd_xfer_handle); 144 void ohci_device_ctrl_abort(usbd_xfer_handle); 145 void ohci_device_ctrl_close(usbd_pipe_handle); 146 void ohci_device_ctrl_done(usbd_xfer_handle); 147 148 usbd_status ohci_device_bulk_transfer(usbd_xfer_handle); 149 usbd_status ohci_device_bulk_start(usbd_xfer_handle); 150 void ohci_device_bulk_abort(usbd_xfer_handle); 151 void ohci_device_bulk_close(usbd_pipe_handle); 152 void ohci_device_bulk_done(usbd_xfer_handle); 153 154 usbd_status ohci_device_intr_transfer(usbd_xfer_handle); 155 usbd_status ohci_device_intr_start(usbd_xfer_handle); 156 void ohci_device_intr_abort(usbd_xfer_handle); 157 void ohci_device_intr_close(usbd_pipe_handle); 158 void ohci_device_intr_done(usbd_xfer_handle); 159 160 usbd_status ohci_device_isoc_transfer(usbd_xfer_handle); 161 usbd_status ohci_device_isoc_start(usbd_xfer_handle); 162 void ohci_device_isoc_abort(usbd_xfer_handle); 163 void ohci_device_isoc_close(usbd_pipe_handle); 164 void ohci_device_isoc_done(usbd_xfer_handle); 165 166 usbd_status ohci_device_setintr(ohci_softc_t *sc, 167 struct ohci_pipe *pipe, int ival); 168 169 int ohci_str(usb_string_descriptor_t *, int, const char *); 170 171 void ohci_timeout(void *); 172 void ohci_timeout_task(void *); 173 void ohci_rhsc_able(ohci_softc_t *, int); 174 void ohci_rhsc_enable(void *); 175 176 void ohci_close_pipe(usbd_pipe_handle, ohci_soft_ed_t *); 177 void ohci_abort_xfer(usbd_xfer_handle, usbd_status); 178 179 void ohci_device_clear_toggle(usbd_pipe_handle pipe); 180 void ohci_noop(usbd_pipe_handle pipe); 181 182 #ifdef OHCI_DEBUG 183 void ohci_dumpregs(ohci_softc_t *); 184 void ohci_dump_tds(ohci_soft_td_t *); 185 void ohci_dump_td(ohci_soft_td_t *); 186 void ohci_dump_ed(ohci_soft_ed_t *); 187 void ohci_dump_itd(ohci_soft_itd_t *); 188 void ohci_dump_itds(ohci_soft_itd_t *); 189 #endif 190 191 #define OBARR(sc) bus_space_barrier((sc)->iot, (sc)->ioh, 0, (sc)->sc_size, \ 192 BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE) 193 #define OWRITE1(sc, r, x) \ 194 do { OBARR(sc); bus_space_write_1((sc)->iot, (sc)->ioh, (r), (x)); } while (0) 195 #define OWRITE2(sc, r, x) \ 196 do { OBARR(sc); bus_space_write_2((sc)->iot, (sc)->ioh, (r), (x)); } while (0) 197 #define OWRITE4(sc, r, x) \ 198 do { OBARR(sc); bus_space_write_4((sc)->iot, (sc)->ioh, (r), (x)); } while (0) 199 #define OREAD1(sc, r) (OBARR(sc), bus_space_read_1((sc)->iot, (sc)->ioh, (r))) 200 #define OREAD2(sc, r) (OBARR(sc), bus_space_read_2((sc)->iot, (sc)->ioh, (r))) 201 #define OREAD4(sc, r) (OBARR(sc), bus_space_read_4((sc)->iot, (sc)->ioh, (r))) 202 203 /* Reverse the bits in a value 0 .. 31 */ 204 u_int8_t revbits[OHCI_NO_INTRS] = 205 { 0x00, 0x10, 0x08, 0x18, 0x04, 0x14, 0x0c, 0x1c, 206 0x02, 0x12, 0x0a, 0x1a, 0x06, 0x16, 0x0e, 0x1e, 207 0x01, 0x11, 0x09, 0x19, 0x05, 0x15, 0x0d, 0x1d, 208 0x03, 0x13, 0x0b, 0x1b, 0x07, 0x17, 0x0f, 0x1f }; 209 210 struct ohci_pipe { 211 struct usbd_pipe pipe; 212 ohci_soft_ed_t *sed; 213 union { 214 ohci_soft_td_t *td; 215 ohci_soft_itd_t *itd; 216 } tail; 217 /* Info needed for different pipe kinds. */ 218 union { 219 /* Control pipe */ 220 struct { 221 usb_dma_t reqdma; 222 u_int length; 223 ohci_soft_td_t *setup, *data, *stat; 224 } ctl; 225 /* Interrupt pipe */ 226 struct { 227 int nslots; 228 int pos; 229 } intr; 230 /* Bulk pipe */ 231 struct { 232 u_int length; 233 int isread; 234 } bulk; 235 /* Iso pipe */ 236 struct iso { 237 int next, inuse; 238 } iso; 239 } u; 240 }; 241 242 #define OHCI_INTR_ENDPT 1 243 244 struct usbd_bus_methods ohci_bus_methods = { 245 ohci_open, 246 ohci_softintr, 247 ohci_poll, 248 ohci_allocm, 249 ohci_freem, 250 ohci_allocx, 251 ohci_freex, 252 }; 253 254 struct usbd_pipe_methods ohci_root_ctrl_methods = { 255 ohci_root_ctrl_transfer, 256 ohci_root_ctrl_start, 257 ohci_root_ctrl_abort, 258 ohci_root_ctrl_close, 259 ohci_noop, 260 ohci_root_ctrl_done, 261 }; 262 263 struct usbd_pipe_methods ohci_root_intr_methods = { 264 ohci_root_intr_transfer, 265 ohci_root_intr_start, 266 ohci_root_intr_abort, 267 ohci_root_intr_close, 268 ohci_noop, 269 ohci_root_intr_done, 270 }; 271 272 struct usbd_pipe_methods ohci_device_ctrl_methods = { 273 ohci_device_ctrl_transfer, 274 ohci_device_ctrl_start, 275 ohci_device_ctrl_abort, 276 ohci_device_ctrl_close, 277 ohci_noop, 278 ohci_device_ctrl_done, 279 }; 280 281 struct usbd_pipe_methods ohci_device_intr_methods = { 282 ohci_device_intr_transfer, 283 ohci_device_intr_start, 284 ohci_device_intr_abort, 285 ohci_device_intr_close, 286 ohci_device_clear_toggle, 287 ohci_device_intr_done, 288 }; 289 290 struct usbd_pipe_methods ohci_device_bulk_methods = { 291 ohci_device_bulk_transfer, 292 ohci_device_bulk_start, 293 ohci_device_bulk_abort, 294 ohci_device_bulk_close, 295 ohci_device_clear_toggle, 296 ohci_device_bulk_done, 297 }; 298 299 struct usbd_pipe_methods ohci_device_isoc_methods = { 300 ohci_device_isoc_transfer, 301 ohci_device_isoc_start, 302 ohci_device_isoc_abort, 303 ohci_device_isoc_close, 304 ohci_noop, 305 ohci_device_isoc_done, 306 }; 307 308 int 309 ohci_activate(struct device *self, int act) 310 { 311 struct ohci_softc *sc = (struct ohci_softc *)self; 312 int rv = 0; 313 314 switch (act) { 315 case DVACT_ACTIVATE: 316 break; 317 318 case DVACT_DEACTIVATE: 319 if (sc->sc_child != NULL) 320 rv = config_deactivate(sc->sc_child); 321 sc->sc_dying = 1; 322 break; 323 } 324 return (rv); 325 } 326 327 int 328 ohci_detach(struct ohci_softc *sc, int flags) 329 { 330 int rv = 0; 331 332 if (sc->sc_child != NULL) 333 rv = config_detach(sc->sc_child, flags); 334 335 if (rv != 0) 336 return (rv); 337 338 timeout_del(&sc->sc_tmo_rhsc); 339 340 if (sc->sc_shutdownhook != NULL) 341 shutdownhook_disestablish(sc->sc_shutdownhook); 342 343 usb_delay_ms(&sc->sc_bus, 300); /* XXX let stray task complete */ 344 345 /* free data structures XXX */ 346 347 return (rv); 348 } 349 350 ohci_soft_ed_t * 351 ohci_alloc_sed(ohci_softc_t *sc) 352 { 353 ohci_soft_ed_t *sed; 354 usbd_status err; 355 int i, offs; 356 usb_dma_t dma; 357 358 if (sc->sc_freeeds == NULL) { 359 DPRINTFN(2, ("ohci_alloc_sed: allocating chunk\n")); 360 err = usb_allocmem(&sc->sc_bus, OHCI_SED_SIZE * OHCI_SED_CHUNK, 361 OHCI_ED_ALIGN, &dma); 362 if (err) 363 return (0); 364 for(i = 0; i < OHCI_SED_CHUNK; i++) { 365 offs = i * OHCI_SED_SIZE; 366 sed = KERNADDR(&dma, offs); 367 sed->physaddr = DMAADDR(&dma, offs); 368 sed->next = sc->sc_freeeds; 369 sc->sc_freeeds = sed; 370 } 371 } 372 sed = sc->sc_freeeds; 373 sc->sc_freeeds = sed->next; 374 memset(&sed->ed, 0, sizeof(ohci_ed_t)); 375 sed->next = 0; 376 return (sed); 377 } 378 379 void 380 ohci_free_sed(ohci_softc_t *sc, ohci_soft_ed_t *sed) 381 { 382 sed->next = sc->sc_freeeds; 383 sc->sc_freeeds = sed; 384 } 385 386 ohci_soft_td_t * 387 ohci_alloc_std(ohci_softc_t *sc) 388 { 389 ohci_soft_td_t *std; 390 usbd_status err; 391 int i, offs; 392 usb_dma_t dma; 393 int s; 394 395 if (sc->sc_freetds == NULL) { 396 DPRINTFN(2, ("ohci_alloc_std: allocating chunk\n")); 397 err = usb_allocmem(&sc->sc_bus, OHCI_STD_SIZE * OHCI_STD_CHUNK, 398 OHCI_TD_ALIGN, &dma); 399 if (err) 400 return (NULL); 401 s = splusb(); 402 for(i = 0; i < OHCI_STD_CHUNK; i++) { 403 offs = i * OHCI_STD_SIZE; 404 std = KERNADDR(&dma, offs); 405 std->physaddr = DMAADDR(&dma, offs); 406 std->nexttd = sc->sc_freetds; 407 sc->sc_freetds = std; 408 } 409 splx(s); 410 } 411 412 s = splusb(); 413 std = sc->sc_freetds; 414 sc->sc_freetds = std->nexttd; 415 memset(&std->td, 0, sizeof(ohci_td_t)); 416 std->nexttd = NULL; 417 std->xfer = NULL; 418 ohci_hash_add_td(sc, std); 419 splx(s); 420 421 return (std); 422 } 423 424 void 425 ohci_free_std(ohci_softc_t *sc, ohci_soft_td_t *std) 426 { 427 int s; 428 429 s = splusb(); 430 ohci_hash_rem_td(sc, std); 431 std->nexttd = sc->sc_freetds; 432 sc->sc_freetds = std; 433 splx(s); 434 } 435 436 usbd_status 437 ohci_alloc_std_chain(struct ohci_pipe *opipe, ohci_softc_t *sc, 438 u_int alen, int rd, usbd_xfer_handle xfer, 439 ohci_soft_td_t *sp, ohci_soft_td_t **ep) 440 { 441 ohci_soft_td_t *next, *cur; 442 ohci_physaddr_t dataphys, dataphysend; 443 u_int32_t tdflags; 444 u_int len, curlen; 445 usb_dma_t *dma = &xfer->dmabuf; 446 u_int16_t flags = xfer->flags; 447 448 DPRINTFN(alen < 4096,("ohci_alloc_std_chain: start len=%u\n", alen)); 449 450 len = alen; 451 cur = sp; 452 dataphys = DMAADDR(dma, 0); 453 dataphysend = OHCI_PAGE(dataphys + len - 1); 454 tdflags = htole32( 455 (rd ? OHCI_TD_IN : OHCI_TD_OUT) | 456 (flags & USBD_SHORT_XFER_OK ? OHCI_TD_R : 0) | 457 OHCI_TD_NOCC | OHCI_TD_TOGGLE_CARRY | OHCI_TD_NOINTR); 458 459 for (;;) { 460 next = ohci_alloc_std(sc); 461 if (next == NULL) 462 goto nomem; 463 464 /* The OHCI hardware can handle at most one page crossing. */ 465 if (OHCI_PAGE(dataphys) == dataphysend || 466 OHCI_PAGE(dataphys) + OHCI_PAGE_SIZE == dataphysend) { 467 /* we can handle it in this TD */ 468 curlen = len; 469 } else { 470 /* must use multiple TDs, fill as much as possible. */ 471 curlen = 2 * OHCI_PAGE_SIZE - 472 (dataphys & (OHCI_PAGE_SIZE-1)); 473 /* the length must be a multiple of the max size */ 474 curlen -= curlen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize); 475 #ifdef DIAGNOSTIC 476 if (curlen == 0) 477 panic("ohci_alloc_std: curlen == 0"); 478 #endif 479 } 480 DPRINTFN(4,("ohci_alloc_std_chain: dataphys=0x%08x " 481 "dataphysend=0x%08x len=%u curlen=%u\n", 482 dataphys, dataphysend, 483 len, curlen)); 484 len -= curlen; 485 486 cur->td.td_flags = tdflags; 487 cur->td.td_cbp = htole32(dataphys); 488 cur->nexttd = next; 489 cur->td.td_nexttd = htole32(next->physaddr); 490 cur->td.td_be = htole32(dataphys + curlen - 1); 491 cur->len = curlen; 492 cur->flags = OHCI_ADD_LEN; 493 cur->xfer = xfer; 494 DPRINTFN(10,("ohci_alloc_std_chain: cbp=0x%08x be=0x%08x\n", 495 dataphys, dataphys + curlen - 1)); 496 if (len == 0) 497 break; 498 DPRINTFN(10,("ohci_alloc_std_chain: extend chain\n")); 499 dataphys += curlen; 500 cur = next; 501 } 502 if (!rd && (flags & USBD_FORCE_SHORT_XFER) && 503 alen % UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize) == 0) { 504 /* Force a 0 length transfer at the end. */ 505 506 cur = next; 507 next = ohci_alloc_std(sc); 508 if (next == NULL) 509 goto nomem; 510 511 cur->td.td_flags = tdflags; 512 cur->td.td_cbp = 0; /* indicate 0 length packet */ 513 cur->nexttd = next; 514 cur->td.td_nexttd = htole32(next->physaddr); 515 cur->td.td_be = ~0; 516 cur->len = 0; 517 cur->flags = 0; 518 cur->xfer = xfer; 519 DPRINTFN(2,("ohci_alloc_std_chain: add 0 xfer\n")); 520 } 521 *ep = cur; 522 523 return (USBD_NORMAL_COMPLETION); 524 525 nomem: 526 /* XXX free chain */ 527 return (USBD_NOMEM); 528 } 529 530 #if 0 531 void 532 ohci_free_std_chain(ohci_softc_t *sc, ohci_soft_td_t *std, 533 ohci_soft_td_t *stdend) 534 { 535 ohci_soft_td_t *p; 536 537 for (; std != stdend; std = p) { 538 p = std->nexttd; 539 ohci_free_std(sc, std); 540 } 541 } 542 #endif 543 544 ohci_soft_itd_t * 545 ohci_alloc_sitd(ohci_softc_t *sc) 546 { 547 ohci_soft_itd_t *sitd; 548 usbd_status err; 549 int i, s, offs; 550 usb_dma_t dma; 551 552 if (sc->sc_freeitds == NULL) { 553 DPRINTFN(2, ("ohci_alloc_sitd: allocating chunk\n")); 554 err = usb_allocmem(&sc->sc_bus, OHCI_SITD_SIZE * OHCI_SITD_CHUNK, 555 OHCI_ITD_ALIGN, &dma); 556 if (err) 557 return (NULL); 558 s = splusb(); 559 for(i = 0; i < OHCI_SITD_CHUNK; i++) { 560 offs = i * OHCI_SITD_SIZE; 561 sitd = KERNADDR(&dma, offs); 562 sitd->physaddr = DMAADDR(&dma, offs); 563 sitd->nextitd = sc->sc_freeitds; 564 sc->sc_freeitds = sitd; 565 } 566 splx(s); 567 } 568 569 s = splusb(); 570 sitd = sc->sc_freeitds; 571 sc->sc_freeitds = sitd->nextitd; 572 memset(&sitd->itd, 0, sizeof(ohci_itd_t)); 573 sitd->nextitd = NULL; 574 sitd->xfer = NULL; 575 ohci_hash_add_itd(sc, sitd); 576 splx(s); 577 578 #ifdef DIAGNOSTIC 579 sitd->isdone = 0; 580 #endif 581 582 return (sitd); 583 } 584 585 void 586 ohci_free_sitd(ohci_softc_t *sc, ohci_soft_itd_t *sitd) 587 { 588 int s; 589 590 DPRINTFN(10,("ohci_free_sitd: sitd=%p\n", sitd)); 591 592 #ifdef DIAGNOSTIC 593 if (!sitd->isdone) { 594 panic("ohci_free_sitd: sitd=%p not done", sitd); 595 return; 596 } 597 /* Warn double free */ 598 sitd->isdone = 0; 599 #endif 600 601 s = splusb(); 602 ohci_hash_rem_itd(sc, sitd); 603 sitd->nextitd = sc->sc_freeitds; 604 sc->sc_freeitds = sitd; 605 splx(s); 606 } 607 608 usbd_status 609 ohci_checkrev(ohci_softc_t *sc) 610 { 611 u_int32_t rev; 612 613 printf(","); 614 rev = OREAD4(sc, OHCI_REVISION); 615 printf(" version %d.%d%s\n", OHCI_REV_HI(rev), OHCI_REV_LO(rev), 616 OHCI_REV_LEGACY(rev) ? ", legacy support" : ""); 617 618 if (OHCI_REV_HI(rev) != 1 || OHCI_REV_LO(rev) != 0) { 619 printf("%s: unsupported OHCI revision\n", 620 sc->sc_bus.bdev.dv_xname); 621 sc->sc_bus.usbrev = USBREV_UNKNOWN; 622 return (USBD_INVAL); 623 } 624 sc->sc_bus.usbrev = USBREV_1_0; 625 626 return (USBD_NORMAL_COMPLETION); 627 } 628 629 usbd_status 630 ohci_handover(ohci_softc_t *sc) 631 { 632 u_int32_t s, ctl; 633 int i; 634 635 ctl = OREAD4(sc, OHCI_CONTROL); 636 if (ctl & OHCI_IR) { 637 /* SMM active, request change */ 638 DPRINTF(("ohci_handover: SMM active, request owner change\n")); 639 if ((sc->sc_intre & (OHCI_OC | OHCI_MIE)) == 640 (OHCI_OC | OHCI_MIE)) 641 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_MIE); 642 s = OREAD4(sc, OHCI_COMMAND_STATUS); 643 OWRITE4(sc, OHCI_COMMAND_STATUS, s | OHCI_OCR); 644 for (i = 0; i < 100 && (ctl & OHCI_IR); i++) { 645 usb_delay_ms(&sc->sc_bus, 1); 646 ctl = OREAD4(sc, OHCI_CONTROL); 647 } 648 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_MIE); 649 if (ctl & OHCI_IR) { 650 printf("%s: SMM does not respond, will reset\n", 651 sc->sc_bus.bdev.dv_xname); 652 } 653 } 654 655 return (USBD_NORMAL_COMPLETION); 656 } 657 658 usbd_status 659 ohci_init(ohci_softc_t *sc) 660 { 661 ohci_soft_ed_t *sed, *psed; 662 usbd_status err; 663 int i; 664 u_int32_t ctl, rwc, ival, hcr, fm, per, desca, descb; 665 666 DPRINTF(("ohci_init: start\n")); 667 668 for (i = 0; i < OHCI_HASH_SIZE; i++) 669 LIST_INIT(&sc->sc_hash_tds[i]); 670 for (i = 0; i < OHCI_HASH_SIZE; i++) 671 LIST_INIT(&sc->sc_hash_itds[i]); 672 673 SIMPLEQ_INIT(&sc->sc_free_xfers); 674 675 /* XXX determine alignment by R/W */ 676 /* Allocate the HCCA area. */ 677 err = usb_allocmem(&sc->sc_bus, OHCI_HCCA_SIZE, 678 OHCI_HCCA_ALIGN, &sc->sc_hccadma); 679 if (err) 680 return (err); 681 sc->sc_hcca = KERNADDR(&sc->sc_hccadma, 0); 682 memset(sc->sc_hcca, 0, OHCI_HCCA_SIZE); 683 684 sc->sc_eintrs = OHCI_NORMAL_INTRS; 685 686 /* Allocate dummy ED that starts the control list. */ 687 sc->sc_ctrl_head = ohci_alloc_sed(sc); 688 if (sc->sc_ctrl_head == NULL) { 689 err = USBD_NOMEM; 690 goto bad1; 691 } 692 sc->sc_ctrl_head->ed.ed_flags |= htole32(OHCI_ED_SKIP); 693 694 /* Allocate dummy ED that starts the bulk list. */ 695 sc->sc_bulk_head = ohci_alloc_sed(sc); 696 if (sc->sc_bulk_head == NULL) { 697 err = USBD_NOMEM; 698 goto bad2; 699 } 700 sc->sc_bulk_head->ed.ed_flags |= htole32(OHCI_ED_SKIP); 701 702 /* Allocate dummy ED that starts the isochronous list. */ 703 sc->sc_isoc_head = ohci_alloc_sed(sc); 704 if (sc->sc_isoc_head == NULL) { 705 err = USBD_NOMEM; 706 goto bad3; 707 } 708 sc->sc_isoc_head->ed.ed_flags |= htole32(OHCI_ED_SKIP); 709 710 /* Allocate all the dummy EDs that make up the interrupt tree. */ 711 for (i = 0; i < OHCI_NO_EDS; i++) { 712 sed = ohci_alloc_sed(sc); 713 if (sed == NULL) { 714 while (--i >= 0) 715 ohci_free_sed(sc, sc->sc_eds[i]); 716 err = USBD_NOMEM; 717 goto bad4; 718 } 719 /* All ED fields are set to 0. */ 720 sc->sc_eds[i] = sed; 721 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); 722 if (i != 0) 723 psed = sc->sc_eds[(i-1) / 2]; 724 else 725 psed= sc->sc_isoc_head; 726 sed->next = psed; 727 sed->ed.ed_nexted = htole32(psed->physaddr); 728 } 729 /* 730 * Fill HCCA interrupt table. The bit reversal is to get 731 * the tree set up properly to spread the interrupts. 732 */ 733 for (i = 0; i < OHCI_NO_INTRS; i++) 734 sc->sc_hcca->hcca_interrupt_table[revbits[i]] = 735 htole32(sc->sc_eds[OHCI_NO_EDS-OHCI_NO_INTRS+i]->physaddr); 736 737 #ifdef OHCI_DEBUG 738 if (ohcidebug > 15) { 739 for (i = 0; i < OHCI_NO_EDS; i++) { 740 printf("ed#%d ", i); 741 ohci_dump_ed(sc->sc_eds[i]); 742 } 743 printf("iso "); 744 ohci_dump_ed(sc->sc_isoc_head); 745 } 746 #endif 747 /* Preserve values programmed by SMM/BIOS but lost over reset. */ 748 ctl = OREAD4(sc, OHCI_CONTROL); 749 rwc = ctl & OHCI_RWC; 750 fm = OREAD4(sc, OHCI_FM_INTERVAL); 751 desca = OREAD4(sc, OHCI_RH_DESCRIPTOR_A); 752 descb = OREAD4(sc, OHCI_RH_DESCRIPTOR_B); 753 754 /* Determine in what context we are running. */ 755 if (ctl & OHCI_IR) { 756 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET | rwc); 757 goto reset; 758 #if 0 759 /* Don't bother trying to reuse the BIOS init, we'll reset it anyway. */ 760 } else if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_RESET) { 761 /* BIOS started controller. */ 762 DPRINTF(("ohci_init: BIOS active\n")); 763 if ((ctl & OHCI_HCFS_MASK) != OHCI_HCFS_OPERATIONAL) { 764 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_OPERATIONAL | rwc); 765 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY); 766 } 767 #endif 768 } else { 769 DPRINTF(("ohci_init: cold started\n")); 770 reset: 771 /* Controller was cold started. */ 772 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); 773 } 774 775 /* 776 * This reset should not be necessary according to the OHCI spec, but 777 * without it some controllers do not start. 778 */ 779 DPRINTF(("%s: resetting\n", sc->sc_bus.bdev.dv_xname)); 780 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET | rwc); 781 usb_delay_ms(&sc->sc_bus, USB_BUS_RESET_DELAY); 782 783 /* We now own the host controller and the bus has been reset. */ 784 785 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_HCR); /* Reset HC */ 786 /* Nominal time for a reset is 10 us. */ 787 for (i = 0; i < 10; i++) { 788 delay(10); 789 hcr = OREAD4(sc, OHCI_COMMAND_STATUS) & OHCI_HCR; 790 if (!hcr) 791 break; 792 } 793 if (hcr) { 794 printf("%s: reset timeout\n", sc->sc_bus.bdev.dv_xname); 795 err = USBD_IOERROR; 796 goto bad5; 797 } 798 #ifdef OHCI_DEBUG 799 if (ohcidebug > 15) 800 ohci_dumpregs(sc); 801 #endif 802 803 /* The controller is now in SUSPEND state, we have 2ms to finish. */ 804 805 /* Set up HC registers. */ 806 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0)); 807 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr); 808 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr); 809 /* disable all interrupts and then switch on all desired interrupts */ 810 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS); 811 /* switch on desired functional features */ 812 ctl = OREAD4(sc, OHCI_CONTROL); 813 ctl &= ~(OHCI_CBSR_MASK | OHCI_LES | OHCI_HCFS_MASK | OHCI_IR); 814 ctl |= OHCI_PLE | OHCI_IE | OHCI_CLE | OHCI_BLE | 815 OHCI_RATIO_1_4 | OHCI_HCFS_OPERATIONAL | rwc; 816 /* And finally start it! */ 817 OWRITE4(sc, OHCI_CONTROL, ctl); 818 819 /* 820 * The controller is now OPERATIONAL. Set a some final 821 * registers that should be set earlier, but that the 822 * controller ignores when in the SUSPEND state. 823 */ 824 ival = OHCI_GET_IVAL(fm); 825 fm = (OREAD4(sc, OHCI_FM_REMAINING) & OHCI_FIT) ^ OHCI_FIT; 826 fm |= OHCI_FSMPS(ival) | ival; 827 OWRITE4(sc, OHCI_FM_INTERVAL, fm); 828 per = OHCI_PERIODIC(ival); /* 90% periodic */ 829 OWRITE4(sc, OHCI_PERIODIC_START, per); 830 831 /* Fiddle the No OverCurrent Protection bit to avoid chip bug. */ 832 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca | OHCI_NOCP); 833 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */ 834 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY); 835 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, desca); 836 OWRITE4(sc, OHCI_RH_DESCRIPTOR_B, descb); 837 usb_delay_ms(&sc->sc_bus, OHCI_GET_POTPGT(desca) * UHD_PWRON_FACTOR); 838 839 /* 840 * The AMD756 requires a delay before re-reading the register, 841 * otherwise it will occasionally report 0 ports. 842 */ 843 sc->sc_noport = 0; 844 for (i = 0; i < 10 && sc->sc_noport == 0; i++) { 845 usb_delay_ms(&sc->sc_bus, OHCI_READ_DESC_DELAY); 846 sc->sc_noport = OHCI_GET_NDP(OREAD4(sc, OHCI_RH_DESCRIPTOR_A)); 847 } 848 849 #ifdef OHCI_DEBUG 850 if (ohcidebug > 5) 851 ohci_dumpregs(sc); 852 #endif 853 854 /* Set up the bus struct. */ 855 sc->sc_bus.methods = &ohci_bus_methods; 856 sc->sc_bus.pipe_size = sizeof(struct ohci_pipe); 857 858 sc->sc_control = sc->sc_intre = 0; 859 sc->sc_shutdownhook = shutdownhook_establish(ohci_shutdown, sc); 860 861 timeout_set(&sc->sc_tmo_rhsc, ohci_rhsc_enable, sc); 862 863 /* Finally, turn on interrupts. */ 864 DPRINTFN(1,("ohci_init: enabling\n")); 865 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, sc->sc_eintrs | OHCI_MIE); 866 867 return (USBD_NORMAL_COMPLETION); 868 869 bad5: 870 for (i = 0; i < OHCI_NO_EDS; i++) 871 ohci_free_sed(sc, sc->sc_eds[i]); 872 bad4: 873 ohci_free_sed(sc, sc->sc_isoc_head); 874 bad3: 875 ohci_free_sed(sc, sc->sc_bulk_head); 876 bad2: 877 ohci_free_sed(sc, sc->sc_ctrl_head); 878 bad1: 879 usb_freemem(&sc->sc_bus, &sc->sc_hccadma); 880 return (err); 881 } 882 883 usbd_status 884 ohci_allocm(struct usbd_bus *bus, usb_dma_t *dma, u_int32_t size) 885 { 886 struct ohci_softc *sc = (struct ohci_softc *)bus; 887 888 return (usb_allocmem(&sc->sc_bus, size, 0, dma)); 889 } 890 891 void 892 ohci_freem(struct usbd_bus *bus, usb_dma_t *dma) 893 { 894 struct ohci_softc *sc = (struct ohci_softc *)bus; 895 896 usb_freemem(&sc->sc_bus, dma); 897 } 898 899 usbd_xfer_handle 900 ohci_allocx(struct usbd_bus *bus) 901 { 902 struct ohci_softc *sc = (struct ohci_softc *)bus; 903 usbd_xfer_handle xfer; 904 905 xfer = SIMPLEQ_FIRST(&sc->sc_free_xfers); 906 if (xfer != NULL) { 907 SIMPLEQ_REMOVE_HEAD(&sc->sc_free_xfers, next); 908 #ifdef DIAGNOSTIC 909 if (xfer->busy_free != XFER_FREE) { 910 printf("ohci_allocx: xfer=%p not free, 0x%08x\n", xfer, 911 xfer->busy_free); 912 } 913 #endif 914 } else { 915 xfer = malloc(sizeof(struct ohci_xfer), M_USB, M_NOWAIT); 916 } 917 if (xfer != NULL) { 918 memset(xfer, 0, sizeof (struct ohci_xfer)); 919 #ifdef DIAGNOSTIC 920 xfer->busy_free = XFER_BUSY; 921 #endif 922 } 923 return (xfer); 924 } 925 926 void 927 ohci_freex(struct usbd_bus *bus, usbd_xfer_handle xfer) 928 { 929 struct ohci_softc *sc = (struct ohci_softc *)bus; 930 931 #ifdef DIAGNOSTIC 932 if (xfer->busy_free != XFER_BUSY) { 933 printf("ohci_freex: xfer=%p not busy, 0x%08x\n", xfer, 934 xfer->busy_free); 935 return; 936 } 937 xfer->busy_free = XFER_FREE; 938 #endif 939 SIMPLEQ_INSERT_HEAD(&sc->sc_free_xfers, xfer, next); 940 } 941 942 /* 943 * Shut down the controller when the system is going down. 944 */ 945 void 946 ohci_shutdown(void *v) 947 { 948 ohci_softc_t *sc = v; 949 950 DPRINTF(("ohci_shutdown: stopping the HC\n")); 951 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 952 } 953 954 /* 955 * Handle suspend/resume. 956 * 957 * We need to switch to polling mode here, because this routine is 958 * called from an interrupt context. This is all right since we 959 * are almost suspended anyway. 960 */ 961 void 962 ohci_power(int why, void *v) 963 { 964 ohci_softc_t *sc = v; 965 u_int32_t reg; 966 int s; 967 968 #ifdef OHCI_DEBUG 969 DPRINTF(("ohci_power: sc=%p, why=%d\n", sc, why)); 970 ohci_dumpregs(sc); 971 #endif 972 973 s = splhardusb(); 974 switch (why) { 975 case PWR_SUSPEND: 976 case PWR_STANDBY: 977 sc->sc_bus.use_polling++; 978 reg = OREAD4(sc, OHCI_CONTROL) & ~OHCI_HCFS_MASK; 979 if (sc->sc_control == 0) { 980 /* 981 * Preserve register values, in case that APM BIOS 982 * does not recover them. 983 */ 984 sc->sc_control = reg; 985 sc->sc_intre = OREAD4(sc, OHCI_INTERRUPT_ENABLE); 986 sc->sc_ival = OHCI_GET_IVAL(OREAD4(sc, 987 OHCI_FM_INTERVAL)); 988 } 989 reg |= OHCI_HCFS_SUSPEND; 990 OWRITE4(sc, OHCI_CONTROL, reg); 991 usb_delay_ms(&sc->sc_bus, USB_RESUME_WAIT); 992 sc->sc_bus.use_polling--; 993 break; 994 case PWR_RESUME: 995 sc->sc_bus.use_polling++; 996 997 /* Some broken BIOSes do not recover these values */ 998 OWRITE4(sc, OHCI_HCCA, DMAADDR(&sc->sc_hccadma, 0)); 999 OWRITE4(sc, OHCI_CONTROL_HEAD_ED, sc->sc_ctrl_head->physaddr); 1000 OWRITE4(sc, OHCI_BULK_HEAD_ED, sc->sc_bulk_head->physaddr); 1001 if (sc->sc_intre) 1002 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, 1003 sc->sc_intre & (OHCI_ALL_INTRS | OHCI_MIE)); 1004 if (sc->sc_control) 1005 reg = sc->sc_control; 1006 else 1007 reg = OREAD4(sc, OHCI_CONTROL); 1008 reg |= OHCI_HCFS_RESUME; 1009 OWRITE4(sc, OHCI_CONTROL, reg); 1010 usb_delay_ms(&sc->sc_bus, USB_RESUME_DELAY); 1011 reg = (reg & ~OHCI_HCFS_MASK) | OHCI_HCFS_OPERATIONAL; 1012 OWRITE4(sc, OHCI_CONTROL, reg); 1013 1014 reg = (OREAD4(sc, OHCI_FM_REMAINING) & OHCI_FIT) ^ OHCI_FIT; 1015 reg |= OHCI_FSMPS(sc->sc_ival) | sc->sc_ival; 1016 OWRITE4(sc, OHCI_FM_INTERVAL, reg); 1017 OWRITE4(sc, OHCI_PERIODIC_START, OHCI_PERIODIC(sc->sc_ival)); 1018 1019 /* Fiddle the No OverCurrent Protection to avoid a chip bug */ 1020 reg = OREAD4(sc, OHCI_RH_DESCRIPTOR_A); 1021 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, reg | OHCI_NOCP); 1022 OWRITE4(sc, OHCI_RH_STATUS, OHCI_LPSC); /* Enable port power */ 1023 usb_delay_ms(&sc->sc_bus, OHCI_ENABLE_POWER_DELAY); 1024 OWRITE4(sc, OHCI_RH_DESCRIPTOR_A, reg); 1025 1026 usb_delay_ms(&sc->sc_bus, USB_RESUME_RECOVERY); 1027 sc->sc_control = sc->sc_intre = sc->sc_ival = 0; 1028 sc->sc_bus.use_polling--; 1029 break; 1030 } 1031 splx(s); 1032 } 1033 1034 #ifdef OHCI_DEBUG 1035 void 1036 ohci_dumpregs(ohci_softc_t *sc) 1037 { 1038 DPRINTF(("ohci_dumpregs: rev=0x%08x control=0x%08x command=0x%08x\n", 1039 OREAD4(sc, OHCI_REVISION), 1040 OREAD4(sc, OHCI_CONTROL), 1041 OREAD4(sc, OHCI_COMMAND_STATUS))); 1042 DPRINTF((" intrstat=0x%08x intre=0x%08x intrd=0x%08x\n", 1043 OREAD4(sc, OHCI_INTERRUPT_STATUS), 1044 OREAD4(sc, OHCI_INTERRUPT_ENABLE), 1045 OREAD4(sc, OHCI_INTERRUPT_DISABLE))); 1046 DPRINTF((" hcca=0x%08x percur=0x%08x ctrlhd=0x%08x\n", 1047 OREAD4(sc, OHCI_HCCA), 1048 OREAD4(sc, OHCI_PERIOD_CURRENT_ED), 1049 OREAD4(sc, OHCI_CONTROL_HEAD_ED))); 1050 DPRINTF((" ctrlcur=0x%08x bulkhd=0x%08x bulkcur=0x%08x\n", 1051 OREAD4(sc, OHCI_CONTROL_CURRENT_ED), 1052 OREAD4(sc, OHCI_BULK_HEAD_ED), 1053 OREAD4(sc, OHCI_BULK_CURRENT_ED))); 1054 DPRINTF((" done=0x%08x fmival=0x%08x fmrem=0x%08x\n", 1055 OREAD4(sc, OHCI_DONE_HEAD), 1056 OREAD4(sc, OHCI_FM_INTERVAL), 1057 OREAD4(sc, OHCI_FM_REMAINING))); 1058 DPRINTF((" fmnum=0x%08x perst=0x%08x lsthrs=0x%08x\n", 1059 OREAD4(sc, OHCI_FM_NUMBER), 1060 OREAD4(sc, OHCI_PERIODIC_START), 1061 OREAD4(sc, OHCI_LS_THRESHOLD))); 1062 DPRINTF((" desca=0x%08x descb=0x%08x stat=0x%08x\n", 1063 OREAD4(sc, OHCI_RH_DESCRIPTOR_A), 1064 OREAD4(sc, OHCI_RH_DESCRIPTOR_B), 1065 OREAD4(sc, OHCI_RH_STATUS))); 1066 DPRINTF((" port1=0x%08x port2=0x%08x\n", 1067 OREAD4(sc, OHCI_RH_PORT_STATUS(1)), 1068 OREAD4(sc, OHCI_RH_PORT_STATUS(2)))); 1069 DPRINTF((" HCCA: frame_number=0x%04x done_head=0x%08x\n", 1070 letoh32(sc->sc_hcca->hcca_frame_number), 1071 letoh32(sc->sc_hcca->hcca_done_head))); 1072 } 1073 #endif 1074 1075 int ohci_intr1(ohci_softc_t *); 1076 1077 int 1078 ohci_intr(void *p) 1079 { 1080 ohci_softc_t *sc = p; 1081 1082 if (sc == NULL || sc->sc_dying) 1083 return (0); 1084 1085 /* If we get an interrupt while polling, then just ignore it. */ 1086 if (!cold && sc->sc_bus.use_polling) { 1087 #ifdef DIAGNOSTIC 1088 static struct timeval ohci_intr_tv; 1089 if ((OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs) && 1090 usbd_ratecheck(&ohci_intr_tv)) 1091 DPRINTFN(16, 1092 ("ohci_intr: ignored interrupt while polling\n")); 1093 #endif 1094 return (0); 1095 } 1096 1097 return (ohci_intr1(sc)); 1098 } 1099 1100 int 1101 ohci_intr1(ohci_softc_t *sc) 1102 { 1103 u_int32_t intrs, eintrs; 1104 ohci_physaddr_t done; 1105 1106 DPRINTFN(14,("ohci_intr1: enter\n")); 1107 1108 /* In case the interrupt occurs before initialization has completed. */ 1109 if (sc == NULL || sc->sc_hcca == NULL) { 1110 #ifdef DIAGNOSTIC 1111 printf("ohci_intr: sc->sc_hcca == NULL\n"); 1112 #endif 1113 return (0); 1114 } 1115 1116 intrs = 0; 1117 done = letoh32(sc->sc_hcca->hcca_done_head); 1118 if (done != 0) { 1119 if (done & ~OHCI_DONE_INTRS) 1120 intrs = OHCI_WDH; 1121 if (done & OHCI_DONE_INTRS) 1122 intrs |= OREAD4(sc, OHCI_INTERRUPT_STATUS); 1123 sc->sc_hcca->hcca_done_head = 0; 1124 } else { 1125 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS); 1126 /* If we've flushed out a WDH then reread */ 1127 if (intrs & OHCI_WDH) { 1128 done = letoh32(sc->sc_hcca->hcca_done_head); 1129 sc->sc_hcca->hcca_done_head = 0; 1130 } 1131 } 1132 1133 if (intrs == 0xffffffff) { 1134 sc->sc_dying = 1; 1135 return (0); 1136 } 1137 1138 if (!intrs) 1139 return (0); 1140 1141 intrs &= ~OHCI_MIE; 1142 OWRITE4(sc, OHCI_INTERRUPT_STATUS, intrs); /* Acknowledge */ 1143 eintrs = intrs & sc->sc_eintrs; 1144 if (!eintrs) 1145 return (0); 1146 1147 sc->sc_bus.intr_context++; 1148 sc->sc_bus.no_intrs++; 1149 DPRINTFN(7, ("ohci_intr: sc=%p intrs=0x%x(0x%x) eintrs=0x%x\n", 1150 sc, (u_int)intrs, OREAD4(sc, OHCI_INTERRUPT_STATUS), 1151 (u_int)eintrs)); 1152 1153 if (eintrs & OHCI_SO) { 1154 sc->sc_overrun_cnt++; 1155 if (usbd_ratecheck(&sc->sc_overrun_ntc)) { 1156 printf("%s: %u scheduling overruns\n", 1157 sc->sc_bus.bdev.dv_xname, sc->sc_overrun_cnt); 1158 sc->sc_overrun_cnt = 0; 1159 } 1160 /* XXX do what */ 1161 eintrs &= ~OHCI_SO; 1162 } 1163 if (eintrs & OHCI_WDH) { 1164 ohci_add_done(sc, done &~ OHCI_DONE_INTRS); 1165 usb_schedsoftintr(&sc->sc_bus); 1166 eintrs &= ~OHCI_WDH; 1167 } 1168 if (eintrs & OHCI_RD) { 1169 printf("%s: resume detect\n", sc->sc_bus.bdev.dv_xname); 1170 /* XXX process resume detect */ 1171 } 1172 if (eintrs & OHCI_UE) { 1173 printf("%s: unrecoverable error, controller halted\n", 1174 sc->sc_bus.bdev.dv_xname); 1175 OWRITE4(sc, OHCI_CONTROL, OHCI_HCFS_RESET); 1176 /* XXX what else */ 1177 } 1178 if (eintrs & OHCI_RHSC) { 1179 ohci_rhsc(sc, sc->sc_intrxfer); 1180 /* 1181 * Disable RHSC interrupt for now, because it will be 1182 * on until the port has been reset. 1183 */ 1184 ohci_rhsc_able(sc, 0); 1185 DPRINTFN(2, ("%s: rhsc interrupt disabled\n", 1186 sc->sc_bus.bdev.dv_xname)); 1187 1188 /* Do not allow RHSC interrupts > 1 per second */ 1189 timeout_add_sec(&sc->sc_tmo_rhsc, 1); 1190 eintrs &= ~OHCI_RHSC; 1191 } 1192 1193 sc->sc_bus.intr_context--; 1194 1195 if (eintrs != 0) { 1196 /* Block unprocessed interrupts. XXX */ 1197 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, eintrs); 1198 sc->sc_eintrs &= ~eintrs; 1199 printf("%s: blocking intrs 0x%x\n", 1200 sc->sc_bus.bdev.dv_xname, eintrs); 1201 } 1202 1203 return (1); 1204 } 1205 1206 void 1207 ohci_rhsc_able(ohci_softc_t *sc, int on) 1208 { 1209 DPRINTFN(4, ("ohci_rhsc_able: on=%d\n", on)); 1210 if (on) { 1211 sc->sc_eintrs |= OHCI_RHSC; 1212 OWRITE4(sc, OHCI_INTERRUPT_ENABLE, OHCI_RHSC); 1213 } else { 1214 sc->sc_eintrs &= ~OHCI_RHSC; 1215 OWRITE4(sc, OHCI_INTERRUPT_DISABLE, OHCI_RHSC); 1216 } 1217 } 1218 1219 void 1220 ohci_rhsc_enable(void *v_sc) 1221 { 1222 ohci_softc_t *sc = v_sc; 1223 int s; 1224 1225 s = splhardusb(); 1226 ohci_rhsc(sc, sc->sc_intrxfer); 1227 DPRINTFN(2, ("%s: rhsc interrupt enabled\n", 1228 sc->sc_bus.bdev.dv_xname)); 1229 1230 ohci_rhsc_able(sc, 1); 1231 splx(s); 1232 } 1233 1234 #ifdef OHCI_DEBUG 1235 char *ohci_cc_strs[] = { 1236 "NO_ERROR", 1237 "CRC", 1238 "BIT_STUFFING", 1239 "DATA_TOGGLE_MISMATCH", 1240 "STALL", 1241 "DEVICE_NOT_RESPONDING", 1242 "PID_CHECK_FAILURE", 1243 "UNEXPECTED_PID", 1244 "DATA_OVERRUN", 1245 "DATA_UNDERRUN", 1246 "BUFFER_OVERRUN", 1247 "BUFFER_UNDERRUN", 1248 "reserved", 1249 "reserved", 1250 "NOT_ACCESSED", 1251 "NOT_ACCESSED", 1252 }; 1253 #endif 1254 1255 void 1256 ohci_add_done(ohci_softc_t *sc, ohci_physaddr_t done) 1257 { 1258 ohci_soft_itd_t *sitd, *sidone, **ip; 1259 ohci_soft_td_t *std, *sdone, **p; 1260 1261 /* Reverse the done list. */ 1262 for (sdone = NULL, sidone = NULL; done != 0; ) { 1263 std = ohci_hash_find_td(sc, done); 1264 if (std != NULL) { 1265 std->dnext = sdone; 1266 done = letoh32(std->td.td_nexttd); 1267 sdone = std; 1268 DPRINTFN(10,("add TD %p\n", std)); 1269 continue; 1270 } 1271 sitd = ohci_hash_find_itd(sc, done); 1272 if (sitd != NULL) { 1273 sitd->dnext = sidone; 1274 done = letoh32(sitd->itd.itd_nextitd); 1275 sidone = sitd; 1276 DPRINTFN(5,("add ITD %p\n", sitd)); 1277 continue; 1278 } 1279 panic("ohci_add_done: addr 0x%08lx not found", (u_long)done); 1280 } 1281 1282 /* sdone & sidone now hold the done lists. */ 1283 /* Put them on the already processed lists. */ 1284 for (p = &sc->sc_sdone; *p != NULL; p = &(*p)->dnext) 1285 ; 1286 *p = sdone; 1287 for (ip = &sc->sc_sidone; *ip != NULL; ip = &(*ip)->dnext) 1288 ; 1289 *ip = sidone; 1290 } 1291 1292 void 1293 ohci_softintr(void *v) 1294 { 1295 ohci_softc_t *sc = v; 1296 ohci_soft_itd_t *sitd, *sidone, *sitdnext; 1297 ohci_soft_td_t *std, *sdone, *stdnext; 1298 usbd_xfer_handle xfer; 1299 struct ohci_pipe *opipe; 1300 int len, cc, s; 1301 int i, j, actlen, iframes, uedir; 1302 1303 DPRINTFN(10,("ohci_softintr: enter\n")); 1304 1305 sc->sc_bus.intr_context++; 1306 1307 s = splhardusb(); 1308 sdone = sc->sc_sdone; 1309 sc->sc_sdone = NULL; 1310 sidone = sc->sc_sidone; 1311 sc->sc_sidone = NULL; 1312 splx(s); 1313 1314 DPRINTFN(10,("ohci_softintr: sdone=%p sidone=%p\n", sdone, sidone)); 1315 1316 #ifdef OHCI_DEBUG 1317 if (ohcidebug > 10) { 1318 DPRINTF(("ohci_process_done: TD done:\n")); 1319 ohci_dump_tds(sdone); 1320 } 1321 #endif 1322 1323 for (std = sdone; std; std = stdnext) { 1324 xfer = std->xfer; 1325 stdnext = std->dnext; 1326 DPRINTFN(10, ("ohci_process_done: std=%p xfer=%p hcpriv=%p\n", 1327 std, xfer, xfer ? xfer->hcpriv : 0)); 1328 if (xfer == NULL) { 1329 /* 1330 * xfer == NULL: There seems to be no xfer associated 1331 * with this TD. It is tailp that happened to end up on 1332 * the done queue. 1333 * Shouldn't happen, but some chips are broken(?). 1334 */ 1335 continue; 1336 } 1337 if (xfer->status == USBD_CANCELLED || 1338 xfer->status == USBD_TIMEOUT) { 1339 DPRINTF(("ohci_process_done: cancel/timeout %p\n", 1340 xfer)); 1341 /* Handled by abort routine. */ 1342 continue; 1343 } 1344 timeout_del(&xfer->timeout_handle); 1345 1346 len = std->len; 1347 if (std->td.td_cbp != 0) 1348 len -= letoh32(std->td.td_be) - 1349 letoh32(std->td.td_cbp) + 1; 1350 DPRINTFN(10, ("ohci_process_done: len=%d, flags=0x%x\n", len, 1351 std->flags)); 1352 if (std->flags & OHCI_ADD_LEN) 1353 xfer->actlen += len; 1354 1355 cc = OHCI_TD_GET_CC(letoh32(std->td.td_flags)); 1356 if (cc == OHCI_CC_NO_ERROR) { 1357 if (std->flags & OHCI_CALL_DONE) { 1358 xfer->status = USBD_NORMAL_COMPLETION; 1359 s = splusb(); 1360 usb_transfer_complete(xfer); 1361 splx(s); 1362 } 1363 ohci_free_std(sc, std); 1364 } else { 1365 /* 1366 * Endpoint is halted. First unlink all the TDs 1367 * belonging to the failed transfer, and then restart 1368 * the endpoint. 1369 */ 1370 ohci_soft_td_t *p, *n; 1371 opipe = (struct ohci_pipe *)xfer->pipe; 1372 1373 DPRINTFN(15,("ohci_process_done: error cc=%d (%s)\n", 1374 OHCI_TD_GET_CC(letoh32(std->td.td_flags)), 1375 ohci_cc_strs[OHCI_TD_GET_CC(letoh32(std->td.td_flags))])); 1376 1377 /* remove TDs */ 1378 for (p = std; p->xfer == xfer; p = n) { 1379 n = p->nexttd; 1380 ohci_free_std(sc, p); 1381 } 1382 1383 /* clear halt */ 1384 opipe->sed->ed.ed_headp = htole32(p->physaddr); 1385 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF); 1386 1387 if (cc == OHCI_CC_STALL) 1388 xfer->status = USBD_STALLED; 1389 else 1390 xfer->status = USBD_IOERROR; 1391 s = splusb(); 1392 usb_transfer_complete(xfer); 1393 splx(s); 1394 } 1395 } 1396 1397 #ifdef OHCI_DEBUG 1398 if (ohcidebug > 10) { 1399 DPRINTF(("ohci_softintr: ITD done:\n")); 1400 ohci_dump_itds(sidone); 1401 } 1402 #endif 1403 1404 for (sitd = sidone; sitd != NULL; sitd = sitdnext) { 1405 xfer = sitd->xfer; 1406 sitdnext = sitd->dnext; 1407 DPRINTFN(1, ("ohci_process_done: sitd=%p xfer=%p hcpriv=%p\n", 1408 sitd, xfer, xfer ? xfer->hcpriv : 0)); 1409 if (xfer == NULL) 1410 continue; 1411 if (xfer->status == USBD_CANCELLED || 1412 xfer->status == USBD_TIMEOUT) { 1413 DPRINTF(("ohci_process_done: cancel/timeout %p\n", 1414 xfer)); 1415 /* Handled by abort routine. */ 1416 continue; 1417 } 1418 #ifdef DIAGNOSTIC 1419 if (sitd->isdone) 1420 printf("ohci_softintr: sitd=%p is done\n", sitd); 1421 sitd->isdone = 1; 1422 #endif 1423 if (sitd->flags & OHCI_CALL_DONE) { 1424 ohci_soft_itd_t *next; 1425 1426 opipe = (struct ohci_pipe *)xfer->pipe; 1427 opipe->u.iso.inuse -= xfer->nframes; 1428 uedir = UE_GET_DIR(xfer->pipe->endpoint->edesc-> 1429 bEndpointAddress); 1430 xfer->status = USBD_NORMAL_COMPLETION; 1431 actlen = 0; 1432 for (i = 0, sitd = xfer->hcpriv;; 1433 sitd = next) { 1434 next = sitd->nextitd; 1435 if (OHCI_ITD_GET_CC(letoh32(sitd-> 1436 itd.itd_flags)) != OHCI_CC_NO_ERROR) 1437 xfer->status = USBD_IOERROR; 1438 /* For input, update frlengths with actual */ 1439 /* XXX anything necessary for output? */ 1440 if (uedir == UE_DIR_IN && 1441 xfer->status == USBD_NORMAL_COMPLETION) { 1442 iframes = OHCI_ITD_GET_FC(letoh32( 1443 sitd->itd.itd_flags)); 1444 for (j = 0; j < iframes; i++, j++) { 1445 len = letoh16(sitd-> 1446 itd.itd_offset[j]); 1447 if ((OHCI_ITD_PSW_GET_CC(len) & 1448 OHCI_CC_NOT_ACCESSED_MASK) 1449 == OHCI_CC_NOT_ACCESSED) 1450 len = 0; 1451 else 1452 len = OHCI_ITD_PSW_LENGTH(len); 1453 xfer->frlengths[i] = len; 1454 actlen += len; 1455 } 1456 } 1457 if (sitd->flags & OHCI_CALL_DONE) 1458 break; 1459 ohci_free_sitd(sc, sitd); 1460 } 1461 ohci_free_sitd(sc, sitd); 1462 if (uedir == UE_DIR_IN && 1463 xfer->status == USBD_NORMAL_COMPLETION) 1464 xfer->actlen = actlen; 1465 xfer->hcpriv = NULL; 1466 1467 s = splusb(); 1468 usb_transfer_complete(xfer); 1469 splx(s); 1470 } 1471 } 1472 1473 if (sc->sc_softwake) { 1474 sc->sc_softwake = 0; 1475 wakeup(&sc->sc_softwake); 1476 } 1477 1478 sc->sc_bus.intr_context--; 1479 DPRINTFN(10,("ohci_softintr: done:\n")); 1480 } 1481 1482 void 1483 ohci_device_ctrl_done(usbd_xfer_handle xfer) 1484 { 1485 DPRINTFN(10,("ohci_device_ctrl_done: xfer=%p\n", xfer)); 1486 1487 #ifdef DIAGNOSTIC 1488 if (!(xfer->rqflags & URQ_REQUEST)) { 1489 panic("ohci_device_ctrl_done: not a request"); 1490 } 1491 #endif 1492 } 1493 1494 void 1495 ohci_device_intr_done(usbd_xfer_handle xfer) 1496 { 1497 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 1498 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus; 1499 ohci_soft_ed_t *sed = opipe->sed; 1500 ohci_soft_td_t *data, *tail; 1501 1502 1503 DPRINTFN(10, ("ohci_device_intr_done: xfer=%p, actlen=%d\n", xfer, 1504 xfer->actlen)); 1505 1506 if (xfer->pipe->repeat) { 1507 data = opipe->tail.td; 1508 tail = ohci_alloc_std(sc); /* XXX should reuse TD */ 1509 if (tail == NULL) { 1510 xfer->status = USBD_NOMEM; 1511 return; 1512 } 1513 tail->xfer = NULL; 1514 1515 data->td.td_flags = htole32( 1516 OHCI_TD_IN | OHCI_TD_NOCC | 1517 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY); 1518 if (xfer->flags & USBD_SHORT_XFER_OK) 1519 data->td.td_flags |= htole32(OHCI_TD_R); 1520 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0)); 1521 data->nexttd = tail; 1522 data->td.td_nexttd = htole32(tail->physaddr); 1523 data->td.td_be = htole32(letoh32(data->td.td_cbp) + 1524 xfer->length - 1); 1525 data->len = xfer->length; 1526 data->xfer = xfer; 1527 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN; 1528 xfer->hcpriv = data; 1529 xfer->actlen = 0; 1530 1531 sed->ed.ed_tailp = htole32(tail->physaddr); 1532 opipe->tail.td = tail; 1533 } 1534 } 1535 1536 void 1537 ohci_device_bulk_done(usbd_xfer_handle xfer) 1538 { 1539 DPRINTFN(10, ("ohci_device_bulk_done: xfer=%p, actlen=%d\n", xfer, 1540 xfer->actlen)); 1541 } 1542 1543 void 1544 ohci_rhsc(ohci_softc_t *sc, usbd_xfer_handle xfer) 1545 { 1546 u_char *p; 1547 int i, m; 1548 int hstatus; 1549 1550 hstatus = OREAD4(sc, OHCI_RH_STATUS); 1551 DPRINTF(("ohci_rhsc: sc=%p xfer=%p hstatus=0x%08x\n", 1552 sc, xfer, hstatus)); 1553 1554 if (xfer == NULL) { 1555 /* Just ignore the change. */ 1556 return; 1557 } 1558 1559 p = KERNADDR(&xfer->dmabuf, 0); 1560 m = min(sc->sc_noport, xfer->length * 8 - 1); 1561 memset(p, 0, xfer->length); 1562 for (i = 1; i <= m; i++) { 1563 /* Pick out CHANGE bits from the status reg. */ 1564 if (OREAD4(sc, OHCI_RH_PORT_STATUS(i)) >> 16) 1565 p[i/8] |= 1 << (i%8); 1566 } 1567 DPRINTF(("ohci_rhsc: change=0x%02x\n", *p)); 1568 xfer->actlen = xfer->length; 1569 xfer->status = USBD_NORMAL_COMPLETION; 1570 1571 usb_transfer_complete(xfer); 1572 } 1573 1574 void 1575 ohci_root_intr_done(usbd_xfer_handle xfer) 1576 { 1577 } 1578 1579 void 1580 ohci_root_ctrl_done(usbd_xfer_handle xfer) 1581 { 1582 } 1583 1584 /* 1585 * Wait here until controller claims to have an interrupt. 1586 * Then call ohci_intr and return. Use timeout to avoid waiting 1587 * too long. 1588 */ 1589 void 1590 ohci_waitintr(ohci_softc_t *sc, usbd_xfer_handle xfer) 1591 { 1592 int timo; 1593 u_int32_t intrs; 1594 1595 xfer->status = USBD_IN_PROGRESS; 1596 for (timo = xfer->timeout; timo >= 0; timo--) { 1597 usb_delay_ms(&sc->sc_bus, 1); 1598 if (sc->sc_dying) 1599 break; 1600 intrs = OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs; 1601 DPRINTFN(15,("ohci_waitintr: 0x%04x\n", intrs)); 1602 #ifdef OHCI_DEBUG 1603 if (ohcidebug > 15) 1604 ohci_dumpregs(sc); 1605 #endif 1606 if (intrs) { 1607 ohci_intr1(sc); 1608 if (xfer->status != USBD_IN_PROGRESS) 1609 return; 1610 } 1611 } 1612 1613 /* Timeout */ 1614 DPRINTF(("ohci_waitintr: timeout\n")); 1615 xfer->status = USBD_TIMEOUT; 1616 usb_transfer_complete(xfer); 1617 /* XXX should free TD */ 1618 } 1619 1620 void 1621 ohci_poll(struct usbd_bus *bus) 1622 { 1623 ohci_softc_t *sc = (ohci_softc_t *)bus; 1624 #ifdef OHCI_DEBUG 1625 static int last; 1626 int new; 1627 new = OREAD4(sc, OHCI_INTERRUPT_STATUS); 1628 if (new != last) { 1629 DPRINTFN(10,("ohci_poll: intrs=0x%04x\n", new)); 1630 last = new; 1631 } 1632 #endif 1633 1634 if (OREAD4(sc, OHCI_INTERRUPT_STATUS) & sc->sc_eintrs) 1635 ohci_intr1(sc); 1636 } 1637 1638 usbd_status 1639 ohci_device_request(usbd_xfer_handle xfer) 1640 { 1641 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 1642 usb_device_request_t *req = &xfer->request; 1643 usbd_device_handle dev = opipe->pipe.device; 1644 ohci_softc_t *sc = (ohci_softc_t *)dev->bus; 1645 int addr = dev->address; 1646 ohci_soft_td_t *setup, *stat, *next, *tail; 1647 ohci_soft_ed_t *sed; 1648 int isread; 1649 u_int len; 1650 usbd_status err; 1651 int s; 1652 1653 isread = req->bmRequestType & UT_READ; 1654 len = UGETW(req->wLength); 1655 1656 DPRINTFN(3,("ohci_device_control type=0x%02x, request=0x%02x, " 1657 "wValue=0x%04x, wIndex=0x%04x len=%u, addr=%d, endpt=%d\n", 1658 req->bmRequestType, req->bRequest, UGETW(req->wValue), 1659 UGETW(req->wIndex), len, addr, 1660 opipe->pipe.endpoint->edesc->bEndpointAddress)); 1661 1662 setup = opipe->tail.td; 1663 stat = ohci_alloc_std(sc); 1664 if (stat == NULL) { 1665 err = USBD_NOMEM; 1666 goto bad1; 1667 } 1668 tail = ohci_alloc_std(sc); 1669 if (tail == NULL) { 1670 err = USBD_NOMEM; 1671 goto bad2; 1672 } 1673 tail->xfer = NULL; 1674 1675 sed = opipe->sed; 1676 opipe->u.ctl.length = len; 1677 1678 /* Update device address and length since they may have changed 1679 during the setup of the control pipe in usbd_new_device(). */ 1680 /* XXX This only needs to be done once, but it's too early in open. */ 1681 /* XXXX Should not touch ED here! */ 1682 sed->ed.ed_flags = htole32( 1683 (letoh32(sed->ed.ed_flags) & ~(OHCI_ED_ADDRMASK | OHCI_ED_MAXPMASK)) | 1684 OHCI_ED_SET_FA(addr) | 1685 OHCI_ED_SET_MAXP(UGETW(opipe->pipe.endpoint->edesc->wMaxPacketSize))); 1686 1687 next = stat; 1688 1689 /* Set up data transaction */ 1690 if (len != 0) { 1691 ohci_soft_td_t *std = stat; 1692 1693 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer, 1694 std, &stat); 1695 stat = stat->nexttd; /* point at free TD */ 1696 if (err) 1697 goto bad3; 1698 /* Start toggle at 1 and then use the carried toggle. */ 1699 std->td.td_flags &= htole32(~OHCI_TD_TOGGLE_MASK); 1700 std->td.td_flags |= htole32(OHCI_TD_TOGGLE_1); 1701 } 1702 1703 memcpy(KERNADDR(&opipe->u.ctl.reqdma, 0), req, sizeof *req); 1704 1705 setup->td.td_flags = htole32(OHCI_TD_SETUP | OHCI_TD_NOCC | 1706 OHCI_TD_TOGGLE_0 | OHCI_TD_NOINTR); 1707 setup->td.td_cbp = htole32(DMAADDR(&opipe->u.ctl.reqdma, 0)); 1708 setup->nexttd = next; 1709 setup->td.td_nexttd = htole32(next->physaddr); 1710 setup->td.td_be = htole32(letoh32(setup->td.td_cbp) + sizeof *req - 1); 1711 setup->len = 0; 1712 setup->xfer = xfer; 1713 setup->flags = 0; 1714 xfer->hcpriv = setup; 1715 1716 stat->td.td_flags = htole32( 1717 (isread ? OHCI_TD_OUT : OHCI_TD_IN) | 1718 OHCI_TD_NOCC | OHCI_TD_TOGGLE_1 | OHCI_TD_SET_DI(1)); 1719 stat->td.td_cbp = 0; 1720 stat->nexttd = tail; 1721 stat->td.td_nexttd = htole32(tail->physaddr); 1722 stat->td.td_be = 0; 1723 stat->flags = OHCI_CALL_DONE; 1724 stat->len = 0; 1725 stat->xfer = xfer; 1726 1727 #ifdef OHCI_DEBUG 1728 if (ohcidebug > 5) { 1729 DPRINTF(("ohci_device_request:\n")); 1730 ohci_dump_ed(sed); 1731 ohci_dump_tds(setup); 1732 } 1733 #endif 1734 1735 /* Insert ED in schedule */ 1736 s = splusb(); 1737 sed->ed.ed_tailp = htole32(tail->physaddr); 1738 opipe->tail.td = tail; 1739 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_CLF); 1740 if (xfer->timeout && !sc->sc_bus.use_polling) { 1741 timeout_del(&xfer->timeout_handle); 1742 timeout_set(&xfer->timeout_handle, ohci_timeout, xfer); 1743 timeout_add(&xfer->timeout_handle, mstohz(xfer->timeout)); 1744 } 1745 splx(s); 1746 1747 #ifdef OHCI_DEBUG 1748 if (ohcidebug > 20) { 1749 delay(10000); 1750 DPRINTF(("ohci_device_request: status=%x\n", 1751 OREAD4(sc, OHCI_COMMAND_STATUS))); 1752 ohci_dumpregs(sc); 1753 printf("ctrl head:\n"); 1754 ohci_dump_ed(sc->sc_ctrl_head); 1755 printf("sed:\n"); 1756 ohci_dump_ed(sed); 1757 ohci_dump_tds(setup); 1758 } 1759 #endif 1760 1761 return (USBD_NORMAL_COMPLETION); 1762 1763 bad3: 1764 ohci_free_std(sc, tail); 1765 bad2: 1766 ohci_free_std(sc, stat); 1767 bad1: 1768 return (err); 1769 } 1770 1771 /* 1772 * Add an ED to the schedule. Called at splusb(). 1773 */ 1774 void 1775 ohci_add_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head) 1776 { 1777 DPRINTFN(8,("ohci_add_ed: sed=%p head=%p\n", sed, head)); 1778 1779 SPLUSBCHECK; 1780 sed->next = head->next; 1781 sed->ed.ed_nexted = head->ed.ed_nexted; 1782 head->next = sed; 1783 head->ed.ed_nexted = htole32(sed->physaddr); 1784 } 1785 1786 /* 1787 * Remove an ED from the schedule. Called at splusb(). 1788 */ 1789 void 1790 ohci_rem_ed(ohci_soft_ed_t *sed, ohci_soft_ed_t *head) 1791 { 1792 ohci_soft_ed_t *p; 1793 1794 SPLUSBCHECK; 1795 1796 /* XXX */ 1797 for (p = head; p != NULL && p->next != sed; p = p->next) 1798 ; 1799 if (p == NULL) 1800 panic("ohci_rem_ed: ED not found"); 1801 p->next = sed->next; 1802 p->ed.ed_nexted = sed->ed.ed_nexted; 1803 } 1804 1805 /* 1806 * When a transfer is completed the TD is added to the done queue by 1807 * the host controller. This queue is the processed by software. 1808 * Unfortunately the queue contains the physical address of the TD 1809 * and we have no simple way to translate this back to a kernel address. 1810 * To make the translation possible (and fast) we use a hash table of 1811 * TDs currently in the schedule. The physical address is used as the 1812 * hash value. 1813 */ 1814 1815 #define HASH(a) (((a) >> 4) % OHCI_HASH_SIZE) 1816 /* Called at splusb() */ 1817 void 1818 ohci_hash_add_td(ohci_softc_t *sc, ohci_soft_td_t *std) 1819 { 1820 int h = HASH(std->physaddr); 1821 1822 SPLUSBCHECK; 1823 1824 LIST_INSERT_HEAD(&sc->sc_hash_tds[h], std, hnext); 1825 } 1826 1827 /* Called at splusb() */ 1828 void 1829 ohci_hash_rem_td(ohci_softc_t *sc, ohci_soft_td_t *std) 1830 { 1831 SPLUSBCHECK; 1832 1833 LIST_REMOVE(std, hnext); 1834 } 1835 1836 ohci_soft_td_t * 1837 ohci_hash_find_td(ohci_softc_t *sc, ohci_physaddr_t a) 1838 { 1839 int h = HASH(a); 1840 ohci_soft_td_t *std; 1841 1842 for (std = LIST_FIRST(&sc->sc_hash_tds[h]); 1843 std != NULL; 1844 std = LIST_NEXT(std, hnext)) 1845 if (std->physaddr == a) 1846 return (std); 1847 return (NULL); 1848 } 1849 1850 /* Called at splusb() */ 1851 void 1852 ohci_hash_add_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd) 1853 { 1854 int h = HASH(sitd->physaddr); 1855 1856 SPLUSBCHECK; 1857 1858 DPRINTFN(10,("ohci_hash_add_itd: sitd=%p physaddr=0x%08lx\n", 1859 sitd, (u_long)sitd->physaddr)); 1860 1861 LIST_INSERT_HEAD(&sc->sc_hash_itds[h], sitd, hnext); 1862 } 1863 1864 /* Called at splusb() */ 1865 void 1866 ohci_hash_rem_itd(ohci_softc_t *sc, ohci_soft_itd_t *sitd) 1867 { 1868 SPLUSBCHECK; 1869 1870 DPRINTFN(10,("ohci_hash_rem_itd: sitd=%p physaddr=0x%08lx\n", 1871 sitd, (u_long)sitd->physaddr)); 1872 1873 LIST_REMOVE(sitd, hnext); 1874 } 1875 1876 ohci_soft_itd_t * 1877 ohci_hash_find_itd(ohci_softc_t *sc, ohci_physaddr_t a) 1878 { 1879 int h = HASH(a); 1880 ohci_soft_itd_t *sitd; 1881 1882 for (sitd = LIST_FIRST(&sc->sc_hash_itds[h]); 1883 sitd != NULL; 1884 sitd = LIST_NEXT(sitd, hnext)) 1885 if (sitd->physaddr == a) 1886 return (sitd); 1887 return (NULL); 1888 } 1889 1890 void 1891 ohci_timeout(void *addr) 1892 { 1893 struct ohci_xfer *oxfer = addr; 1894 struct ohci_pipe *opipe = (struct ohci_pipe *)oxfer->xfer.pipe; 1895 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus; 1896 1897 DPRINTF(("ohci_timeout: oxfer=%p\n", oxfer)); 1898 1899 if (sc->sc_dying) { 1900 ohci_abort_xfer(&oxfer->xfer, USBD_TIMEOUT); 1901 return; 1902 } 1903 1904 /* Execute the abort in a process context. */ 1905 usb_init_task(&oxfer->abort_task, ohci_timeout_task, addr); 1906 usb_add_task(oxfer->xfer.pipe->device, &oxfer->abort_task); 1907 } 1908 1909 void 1910 ohci_timeout_task(void *addr) 1911 { 1912 usbd_xfer_handle xfer = addr; 1913 int s; 1914 1915 DPRINTF(("ohci_timeout_task: xfer=%p\n", xfer)); 1916 1917 s = splusb(); 1918 ohci_abort_xfer(xfer, USBD_TIMEOUT); 1919 splx(s); 1920 } 1921 1922 #ifdef OHCI_DEBUG 1923 void 1924 ohci_dump_tds(ohci_soft_td_t *std) 1925 { 1926 for (; std; std = std->nexttd) 1927 ohci_dump_td(std); 1928 } 1929 1930 void 1931 ohci_dump_td(ohci_soft_td_t *std) 1932 { 1933 char sbuf[128]; 1934 1935 bitmask_snprintf((u_int32_t)letoh32(std->td.td_flags), 1936 "\20\23R\24OUT\25IN\31TOG1\32SETTOGGLE", 1937 sbuf, sizeof(sbuf)); 1938 1939 printf("TD(%p) at %08lx: %s delay=%d ec=%d cc=%d\ncbp=0x%08lx " 1940 "nexttd=0x%08lx be=0x%08lx\n", 1941 std, (u_long)std->physaddr, sbuf, 1942 OHCI_TD_GET_DI(letoh32(std->td.td_flags)), 1943 OHCI_TD_GET_EC(letoh32(std->td.td_flags)), 1944 OHCI_TD_GET_CC(letoh32(std->td.td_flags)), 1945 (u_long)letoh32(std->td.td_cbp), 1946 (u_long)letoh32(std->td.td_nexttd), 1947 (u_long)letoh32(std->td.td_be)); 1948 } 1949 1950 void 1951 ohci_dump_itd(ohci_soft_itd_t *sitd) 1952 { 1953 int i; 1954 1955 printf("ITD(%p) at %08lx: sf=%d di=%d fc=%d cc=%d\n" 1956 "bp0=0x%08lx next=0x%08lx be=0x%08lx\n", 1957 sitd, (u_long)sitd->physaddr, 1958 OHCI_ITD_GET_SF(letoh32(sitd->itd.itd_flags)), 1959 OHCI_ITD_GET_DI(letoh32(sitd->itd.itd_flags)), 1960 OHCI_ITD_GET_FC(letoh32(sitd->itd.itd_flags)), 1961 OHCI_ITD_GET_CC(letoh32(sitd->itd.itd_flags)), 1962 (u_long)letoh32(sitd->itd.itd_bp0), 1963 (u_long)letoh32(sitd->itd.itd_nextitd), 1964 (u_long)letoh32(sitd->itd.itd_be)); 1965 for (i = 0; i < OHCI_ITD_NOFFSET; i++) 1966 printf("offs[%d]=0x%04x ", i, 1967 (u_int)letoh16(sitd->itd.itd_offset[i])); 1968 printf("\n"); 1969 } 1970 1971 void 1972 ohci_dump_itds(ohci_soft_itd_t *sitd) 1973 { 1974 for (; sitd; sitd = sitd->nextitd) 1975 ohci_dump_itd(sitd); 1976 } 1977 1978 void 1979 ohci_dump_ed(ohci_soft_ed_t *sed) 1980 { 1981 char sbuf[128], sbuf2[128]; 1982 1983 bitmask_snprintf((u_int32_t)letoh32(sed->ed.ed_flags), 1984 "\20\14OUT\15IN\16LOWSPEED\17SKIP\20ISO", 1985 sbuf, sizeof(sbuf)); 1986 bitmask_snprintf((u_int32_t)letoh32(sed->ed.ed_headp), 1987 "\20\1HALT\2CARRY", sbuf2, sizeof(sbuf2)); 1988 1989 printf("ED(%p) at 0x%08lx: addr=%d endpt=%d maxp=%d flags=%s\n" 1990 "tailp=0x%08lx headflags=%s headp=0x%08lx nexted=0x%08lx\n", 1991 sed, (u_long)sed->physaddr, 1992 OHCI_ED_GET_FA(letoh32(sed->ed.ed_flags)), 1993 OHCI_ED_GET_EN(letoh32(sed->ed.ed_flags)), 1994 OHCI_ED_GET_MAXP(letoh32(sed->ed.ed_flags)), sbuf, 1995 (u_long)letoh32(sed->ed.ed_tailp), sbuf2, 1996 (u_long)letoh32(sed->ed.ed_headp), 1997 (u_long)letoh32(sed->ed.ed_nexted)); 1998 } 1999 #endif 2000 2001 usbd_status 2002 ohci_open(usbd_pipe_handle pipe) 2003 { 2004 usbd_device_handle dev = pipe->device; 2005 ohci_softc_t *sc = (ohci_softc_t *)dev->bus; 2006 usb_endpoint_descriptor_t *ed = pipe->endpoint->edesc; 2007 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 2008 u_int8_t addr = dev->address; 2009 u_int8_t xfertype = ed->bmAttributes & UE_XFERTYPE; 2010 ohci_soft_ed_t *sed; 2011 ohci_soft_td_t *std; 2012 ohci_soft_itd_t *sitd; 2013 ohci_physaddr_t tdphys; 2014 u_int32_t fmt; 2015 usbd_status err; 2016 int s; 2017 int ival; 2018 2019 DPRINTFN(1, ("ohci_open: pipe=%p, addr=%d, endpt=%d (%d)\n", 2020 pipe, addr, ed->bEndpointAddress, sc->sc_addr)); 2021 2022 if (sc->sc_dying) 2023 return (USBD_IOERROR); 2024 2025 std = NULL; 2026 sed = NULL; 2027 2028 if (addr == sc->sc_addr) { 2029 switch (ed->bEndpointAddress) { 2030 case USB_CONTROL_ENDPOINT: 2031 pipe->methods = &ohci_root_ctrl_methods; 2032 break; 2033 case UE_DIR_IN | OHCI_INTR_ENDPT: 2034 pipe->methods = &ohci_root_intr_methods; 2035 break; 2036 default: 2037 return (USBD_INVAL); 2038 } 2039 } else { 2040 sed = ohci_alloc_sed(sc); 2041 if (sed == NULL) 2042 goto bad0; 2043 opipe->sed = sed; 2044 if (xfertype == UE_ISOCHRONOUS) { 2045 sitd = ohci_alloc_sitd(sc); 2046 if (sitd == NULL) 2047 goto bad1; 2048 opipe->tail.itd = sitd; 2049 tdphys = sitd->physaddr; 2050 fmt = OHCI_ED_FORMAT_ISO; 2051 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN) 2052 fmt |= OHCI_ED_DIR_IN; 2053 else 2054 fmt |= OHCI_ED_DIR_OUT; 2055 } else { 2056 std = ohci_alloc_std(sc); 2057 if (std == NULL) 2058 goto bad1; 2059 opipe->tail.td = std; 2060 tdphys = std->physaddr; 2061 fmt = OHCI_ED_FORMAT_GEN | OHCI_ED_DIR_TD; 2062 } 2063 sed->ed.ed_flags = htole32( 2064 OHCI_ED_SET_FA(addr) | 2065 OHCI_ED_SET_EN(UE_GET_ADDR(ed->bEndpointAddress)) | 2066 (dev->speed == USB_SPEED_LOW ? OHCI_ED_SPEED : 0) | 2067 fmt | OHCI_ED_SET_MAXP(UGETW(ed->wMaxPacketSize))); 2068 sed->ed.ed_headp = htole32(tdphys | 2069 (pipe->endpoint->savedtoggle ? OHCI_TOGGLECARRY : 0)); 2070 sed->ed.ed_tailp = htole32(tdphys); 2071 2072 switch (xfertype) { 2073 case UE_CONTROL: 2074 pipe->methods = &ohci_device_ctrl_methods; 2075 err = usb_allocmem(&sc->sc_bus, 2076 sizeof(usb_device_request_t), 2077 0, &opipe->u.ctl.reqdma); 2078 if (err) 2079 goto bad; 2080 s = splusb(); 2081 ohci_add_ed(sed, sc->sc_ctrl_head); 2082 splx(s); 2083 break; 2084 case UE_INTERRUPT: 2085 pipe->methods = &ohci_device_intr_methods; 2086 ival = pipe->interval; 2087 if (ival == USBD_DEFAULT_INTERVAL) 2088 ival = ed->bInterval; 2089 return (ohci_device_setintr(sc, opipe, ival)); 2090 case UE_ISOCHRONOUS: 2091 pipe->methods = &ohci_device_isoc_methods; 2092 return (ohci_setup_isoc(pipe)); 2093 case UE_BULK: 2094 pipe->methods = &ohci_device_bulk_methods; 2095 s = splusb(); 2096 ohci_add_ed(sed, sc->sc_bulk_head); 2097 splx(s); 2098 break; 2099 } 2100 } 2101 return (USBD_NORMAL_COMPLETION); 2102 2103 bad: 2104 if (std != NULL) 2105 ohci_free_std(sc, std); 2106 bad1: 2107 if (sed != NULL) 2108 ohci_free_sed(sc, sed); 2109 bad0: 2110 return (USBD_NOMEM); 2111 2112 } 2113 2114 /* 2115 * Close a reqular pipe. 2116 * Assumes that there are no pending transactions. 2117 */ 2118 void 2119 ohci_close_pipe(usbd_pipe_handle pipe, ohci_soft_ed_t *head) 2120 { 2121 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 2122 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 2123 ohci_soft_ed_t *sed = opipe->sed; 2124 int s; 2125 2126 s = splusb(); 2127 #ifdef DIAGNOSTIC 2128 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); 2129 if ((letoh32(sed->ed.ed_tailp) & OHCI_HEADMASK) != 2130 (letoh32(sed->ed.ed_headp) & OHCI_HEADMASK)) { 2131 ohci_soft_td_t *std; 2132 std = ohci_hash_find_td(sc, letoh32(sed->ed.ed_headp)); 2133 printf("ohci_close_pipe: pipe not empty sed=%p hd=0x%x " 2134 "tl=0x%x pipe=%p, std=%p\n", sed, 2135 (int)letoh32(sed->ed.ed_headp), 2136 (int)letoh32(sed->ed.ed_tailp), 2137 pipe, std); 2138 #ifdef USB_DEBUG 2139 usbd_dump_pipe(&opipe->pipe); 2140 #endif 2141 #ifdef OHCI_DEBUG 2142 ohci_dump_ed(sed); 2143 if (std) 2144 ohci_dump_td(std); 2145 #endif 2146 usb_delay_ms(&sc->sc_bus, 2); 2147 if ((letoh32(sed->ed.ed_tailp) & OHCI_HEADMASK) != 2148 (letoh32(sed->ed.ed_headp) & OHCI_HEADMASK)) 2149 printf("ohci_close_pipe: pipe still not empty\n"); 2150 } 2151 #endif 2152 ohci_rem_ed(sed, head); 2153 /* Make sure the host controller is not touching this ED */ 2154 usb_delay_ms(&sc->sc_bus, 1); 2155 splx(s); 2156 pipe->endpoint->savedtoggle = 2157 (letoh32(sed->ed.ed_headp) & OHCI_TOGGLECARRY) ? 1 : 0; 2158 ohci_free_sed(sc, opipe->sed); 2159 } 2160 2161 /* 2162 * Abort a device request. 2163 * If this routine is called at splusb() it guarantees that the request 2164 * will be removed from the hardware scheduling and that the callback 2165 * for it will be called with USBD_CANCELLED status. 2166 * It's impossible to guarantee that the requested transfer will not 2167 * have happened since the hardware runs concurrently. 2168 * If the transaction has already happened we rely on the ordinary 2169 * interrupt processing to process it. 2170 */ 2171 void 2172 ohci_abort_xfer(usbd_xfer_handle xfer, usbd_status status) 2173 { 2174 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 2175 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus; 2176 ohci_soft_ed_t *sed = opipe->sed; 2177 ohci_soft_td_t *p, *n; 2178 ohci_physaddr_t headp; 2179 int s, hit; 2180 2181 DPRINTF(("ohci_abort_xfer: xfer=%p pipe=%p sed=%p\n", xfer, opipe, 2182 sed)); 2183 2184 if (sc->sc_dying) { 2185 /* If we're dying, just do the software part. */ 2186 s = splusb(); 2187 xfer->status = status; /* make software ignore it */ 2188 timeout_del(&xfer->timeout_handle); 2189 usb_transfer_complete(xfer); 2190 splx(s); 2191 return; 2192 } 2193 2194 if (xfer->device->bus->intr_context || !curproc) 2195 panic("ohci_abort_xfer: not in process context"); 2196 2197 /* 2198 * Step 1: Make interrupt routine and hardware ignore xfer. 2199 */ 2200 s = splusb(); 2201 xfer->status = status; /* make software ignore it */ 2202 timeout_del(&xfer->timeout_handle); 2203 splx(s); 2204 DPRINTFN(1,("ohci_abort_xfer: stop ed=%p\n", sed)); 2205 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */ 2206 2207 /* 2208 * Step 2: Wait until we know hardware has finished any possible 2209 * use of the xfer. Also make sure the soft interrupt routine 2210 * has run. 2211 */ 2212 usb_delay_ms(opipe->pipe.device->bus, 20); /* Hardware finishes in 1ms */ 2213 s = splusb(); 2214 sc->sc_softwake = 1; 2215 usb_schedsoftintr(&sc->sc_bus); 2216 tsleep(&sc->sc_softwake, PZERO, "ohciab", 0); 2217 splx(s); 2218 2219 /* 2220 * Step 3: Remove any vestiges of the xfer from the hardware. 2221 * The complication here is that the hardware may have executed 2222 * beyond the xfer we're trying to abort. So as we're scanning 2223 * the TDs of this xfer we check if the hardware points to 2224 * any of them. 2225 */ 2226 s = splusb(); /* XXX why? */ 2227 p = xfer->hcpriv; 2228 #ifdef DIAGNOSTIC 2229 if (p == NULL) { 2230 splx(s); 2231 printf("ohci_abort_xfer: hcpriv is NULL\n"); 2232 return; 2233 } 2234 #endif 2235 #ifdef OHCI_DEBUG 2236 if (ohcidebug > 1) { 2237 DPRINTF(("ohci_abort_xfer: sed=\n")); 2238 ohci_dump_ed(sed); 2239 ohci_dump_tds(p); 2240 } 2241 #endif 2242 headp = letoh32(sed->ed.ed_headp) & OHCI_HEADMASK; 2243 hit = 0; 2244 for (; p->xfer == xfer; p = n) { 2245 hit |= headp == p->physaddr; 2246 n = p->nexttd; 2247 if (OHCI_TD_GET_CC(letoh32(p->td.td_flags)) == 2248 OHCI_CC_NOT_ACCESSED) 2249 ohci_free_std(sc, p); 2250 } 2251 /* Zap headp register if hardware pointed inside the xfer. */ 2252 if (hit) { 2253 DPRINTFN(1,("ohci_abort_xfer: set hd=0x%08x, tl=0x%08x\n", 2254 (int)p->physaddr, (int)letoh32(sed->ed.ed_tailp))); 2255 sed->ed.ed_headp = htole32(p->physaddr); /* unlink TDs */ 2256 } else { 2257 DPRINTFN(1,("ohci_abort_xfer: no hit\n")); 2258 } 2259 2260 /* 2261 * Step 4: Turn on hardware again. 2262 */ 2263 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */ 2264 2265 /* 2266 * Step 5: Execute callback. 2267 */ 2268 usb_transfer_complete(xfer); 2269 2270 splx(s); 2271 } 2272 2273 /* 2274 * Data structures and routines to emulate the root hub. 2275 */ 2276 usb_device_descriptor_t ohci_devd = { 2277 USB_DEVICE_DESCRIPTOR_SIZE, 2278 UDESC_DEVICE, /* type */ 2279 {0x00, 0x01}, /* USB version */ 2280 UDCLASS_HUB, /* class */ 2281 UDSUBCLASS_HUB, /* subclass */ 2282 UDPROTO_FSHUB, 2283 64, /* max packet */ 2284 {0},{0},{0x00,0x01}, /* device id */ 2285 1,2,0, /* string indices */ 2286 1 /* # of configurations */ 2287 }; 2288 2289 usb_config_descriptor_t ohci_confd = { 2290 USB_CONFIG_DESCRIPTOR_SIZE, 2291 UDESC_CONFIG, 2292 {USB_CONFIG_DESCRIPTOR_SIZE + 2293 USB_INTERFACE_DESCRIPTOR_SIZE + 2294 USB_ENDPOINT_DESCRIPTOR_SIZE}, 2295 1, 2296 1, 2297 0, 2298 UC_SELF_POWERED, 2299 0 /* max power */ 2300 }; 2301 2302 usb_interface_descriptor_t ohci_ifcd = { 2303 USB_INTERFACE_DESCRIPTOR_SIZE, 2304 UDESC_INTERFACE, 2305 0, 2306 0, 2307 1, 2308 UICLASS_HUB, 2309 UISUBCLASS_HUB, 2310 UIPROTO_FSHUB, 2311 0 2312 }; 2313 2314 usb_endpoint_descriptor_t ohci_endpd = { 2315 USB_ENDPOINT_DESCRIPTOR_SIZE, 2316 UDESC_ENDPOINT, 2317 UE_DIR_IN | OHCI_INTR_ENDPT, 2318 UE_INTERRUPT, 2319 {8, 0}, /* max packet */ 2320 255 2321 }; 2322 2323 usb_hub_descriptor_t ohci_hubd = { 2324 USB_HUB_DESCRIPTOR_SIZE, 2325 UDESC_HUB, 2326 0, 2327 {0,0}, 2328 0, 2329 0, 2330 {0}, 2331 }; 2332 2333 int 2334 ohci_str(usb_string_descriptor_t *p, int l, const char *s) 2335 { 2336 int i; 2337 2338 if (l == 0) 2339 return (0); 2340 p->bLength = 2 * strlen(s) + 2; 2341 if (l == 1) 2342 return (1); 2343 p->bDescriptorType = UDESC_STRING; 2344 l -= 2; 2345 for (i = 0; s[i] && l > 1; i++, l -= 2) 2346 USETW2(p->bString[i], 0, s[i]); 2347 return (2*i+2); 2348 } 2349 2350 /* 2351 * Simulate a hardware hub by handling all the necessary requests. 2352 */ 2353 usbd_status 2354 ohci_root_ctrl_transfer(usbd_xfer_handle xfer) 2355 { 2356 usbd_status err; 2357 2358 /* Insert last in queue. */ 2359 err = usb_insert_transfer(xfer); 2360 if (err) 2361 return (err); 2362 2363 /* Pipe isn't running, start first */ 2364 return (ohci_root_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2365 } 2366 2367 usbd_status 2368 ohci_root_ctrl_start(usbd_xfer_handle xfer) 2369 { 2370 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus; 2371 usb_device_request_t *req; 2372 void *buf = NULL; 2373 int port, i; 2374 int s, len, value, index, l, totlen = 0; 2375 usb_port_status_t ps; 2376 usb_hub_descriptor_t hubd; 2377 usbd_status err; 2378 u_int32_t v; 2379 2380 if (sc->sc_dying) 2381 return (USBD_IOERROR); 2382 2383 #ifdef DIAGNOSTIC 2384 if (!(xfer->rqflags & URQ_REQUEST)) 2385 /* XXX panic */ 2386 return (USBD_INVAL); 2387 #endif 2388 req = &xfer->request; 2389 2390 DPRINTFN(4,("ohci_root_ctrl_control type=0x%02x request=%02x\n", 2391 req->bmRequestType, req->bRequest)); 2392 2393 len = UGETW(req->wLength); 2394 value = UGETW(req->wValue); 2395 index = UGETW(req->wIndex); 2396 2397 if (len != 0) 2398 buf = KERNADDR(&xfer->dmabuf, 0); 2399 2400 #define C(x,y) ((x) | ((y) << 8)) 2401 switch(C(req->bRequest, req->bmRequestType)) { 2402 case C(UR_CLEAR_FEATURE, UT_WRITE_DEVICE): 2403 case C(UR_CLEAR_FEATURE, UT_WRITE_INTERFACE): 2404 case C(UR_CLEAR_FEATURE, UT_WRITE_ENDPOINT): 2405 /* 2406 * DEVICE_REMOTE_WAKEUP and ENDPOINT_HALT are no-ops 2407 * for the integrated root hub. 2408 */ 2409 break; 2410 case C(UR_GET_CONFIG, UT_READ_DEVICE): 2411 if (len > 0) { 2412 *(u_int8_t *)buf = sc->sc_conf; 2413 totlen = 1; 2414 } 2415 break; 2416 case C(UR_GET_DESCRIPTOR, UT_READ_DEVICE): 2417 DPRINTFN(8,("ohci_root_ctrl_control wValue=0x%04x\n", value)); 2418 switch(value >> 8) { 2419 case UDESC_DEVICE: 2420 if ((value & 0xff) != 0) { 2421 err = USBD_IOERROR; 2422 goto ret; 2423 } 2424 totlen = l = min(len, USB_DEVICE_DESCRIPTOR_SIZE); 2425 USETW(ohci_devd.idVendor, sc->sc_id_vendor); 2426 memcpy(buf, &ohci_devd, l); 2427 break; 2428 case UDESC_CONFIG: 2429 if ((value & 0xff) != 0) { 2430 err = USBD_IOERROR; 2431 goto ret; 2432 } 2433 totlen = l = min(len, USB_CONFIG_DESCRIPTOR_SIZE); 2434 memcpy(buf, &ohci_confd, l); 2435 buf = (char *)buf + l; 2436 len -= l; 2437 l = min(len, USB_INTERFACE_DESCRIPTOR_SIZE); 2438 totlen += l; 2439 memcpy(buf, &ohci_ifcd, l); 2440 buf = (char *)buf + l; 2441 len -= l; 2442 l = min(len, USB_ENDPOINT_DESCRIPTOR_SIZE); 2443 totlen += l; 2444 memcpy(buf, &ohci_endpd, l); 2445 break; 2446 case UDESC_STRING: 2447 if (len == 0) 2448 break; 2449 *(u_int8_t *)buf = 0; 2450 totlen = 1; 2451 switch (value & 0xff) { 2452 case 0: /* Language table */ 2453 totlen = ohci_str(buf, len, "\001"); 2454 break; 2455 case 1: /* Vendor */ 2456 totlen = ohci_str(buf, len, sc->sc_vendor); 2457 break; 2458 case 2: /* Product */ 2459 totlen = ohci_str(buf, len, "OHCI root hub"); 2460 break; 2461 } 2462 break; 2463 default: 2464 err = USBD_IOERROR; 2465 goto ret; 2466 } 2467 break; 2468 case C(UR_GET_INTERFACE, UT_READ_INTERFACE): 2469 if (len > 0) { 2470 *(u_int8_t *)buf = 0; 2471 totlen = 1; 2472 } 2473 break; 2474 case C(UR_GET_STATUS, UT_READ_DEVICE): 2475 if (len > 1) { 2476 USETW(((usb_status_t *)buf)->wStatus,UDS_SELF_POWERED); 2477 totlen = 2; 2478 } 2479 break; 2480 case C(UR_GET_STATUS, UT_READ_INTERFACE): 2481 case C(UR_GET_STATUS, UT_READ_ENDPOINT): 2482 if (len > 1) { 2483 USETW(((usb_status_t *)buf)->wStatus, 0); 2484 totlen = 2; 2485 } 2486 break; 2487 case C(UR_SET_ADDRESS, UT_WRITE_DEVICE): 2488 if (value >= USB_MAX_DEVICES) { 2489 err = USBD_IOERROR; 2490 goto ret; 2491 } 2492 sc->sc_addr = value; 2493 break; 2494 case C(UR_SET_CONFIG, UT_WRITE_DEVICE): 2495 if (value != 0 && value != 1) { 2496 err = USBD_IOERROR; 2497 goto ret; 2498 } 2499 sc->sc_conf = value; 2500 break; 2501 case C(UR_SET_DESCRIPTOR, UT_WRITE_DEVICE): 2502 break; 2503 case C(UR_SET_FEATURE, UT_WRITE_DEVICE): 2504 case C(UR_SET_FEATURE, UT_WRITE_INTERFACE): 2505 case C(UR_SET_FEATURE, UT_WRITE_ENDPOINT): 2506 err = USBD_IOERROR; 2507 goto ret; 2508 case C(UR_SET_INTERFACE, UT_WRITE_INTERFACE): 2509 break; 2510 case C(UR_SYNCH_FRAME, UT_WRITE_ENDPOINT): 2511 break; 2512 /* Hub requests */ 2513 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_DEVICE): 2514 break; 2515 case C(UR_CLEAR_FEATURE, UT_WRITE_CLASS_OTHER): 2516 DPRINTFN(8, ("ohci_root_ctrl_control: UR_CLEAR_PORT_FEATURE " 2517 "port=%d feature=%d\n", 2518 index, value)); 2519 if (index < 1 || index > sc->sc_noport) { 2520 err = USBD_IOERROR; 2521 goto ret; 2522 } 2523 port = OHCI_RH_PORT_STATUS(index); 2524 switch(value) { 2525 case UHF_PORT_ENABLE: 2526 OWRITE4(sc, port, UPS_CURRENT_CONNECT_STATUS); 2527 break; 2528 case UHF_PORT_SUSPEND: 2529 OWRITE4(sc, port, UPS_OVERCURRENT_INDICATOR); 2530 break; 2531 case UHF_PORT_POWER: 2532 /* Yes, writing to the LOW_SPEED bit clears power. */ 2533 OWRITE4(sc, port, UPS_LOW_SPEED); 2534 break; 2535 case UHF_C_PORT_CONNECTION: 2536 OWRITE4(sc, port, UPS_C_CONNECT_STATUS << 16); 2537 break; 2538 case UHF_C_PORT_ENABLE: 2539 OWRITE4(sc, port, UPS_C_PORT_ENABLED << 16); 2540 break; 2541 case UHF_C_PORT_SUSPEND: 2542 OWRITE4(sc, port, UPS_C_SUSPEND << 16); 2543 break; 2544 case UHF_C_PORT_OVER_CURRENT: 2545 OWRITE4(sc, port, UPS_C_OVERCURRENT_INDICATOR << 16); 2546 break; 2547 case UHF_C_PORT_RESET: 2548 OWRITE4(sc, port, UPS_C_PORT_RESET << 16); 2549 break; 2550 default: 2551 err = USBD_IOERROR; 2552 goto ret; 2553 } 2554 switch(value) { 2555 case UHF_C_PORT_CONNECTION: 2556 case UHF_C_PORT_ENABLE: 2557 case UHF_C_PORT_SUSPEND: 2558 case UHF_C_PORT_OVER_CURRENT: 2559 case UHF_C_PORT_RESET: 2560 /* Enable RHSC interrupt if condition is cleared. */ 2561 if ((OREAD4(sc, port) >> 16) == 0) 2562 ohci_rhsc_able(sc, 1); 2563 break; 2564 default: 2565 break; 2566 } 2567 break; 2568 case C(UR_GET_DESCRIPTOR, UT_READ_CLASS_DEVICE): 2569 if ((value & 0xff) != 0) { 2570 err = USBD_IOERROR; 2571 goto ret; 2572 } 2573 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_A); 2574 hubd = ohci_hubd; 2575 hubd.bNbrPorts = sc->sc_noport; 2576 USETW(hubd.wHubCharacteristics, 2577 (v & OHCI_NPS ? UHD_PWR_NO_SWITCH : 2578 v & OHCI_PSM ? UHD_PWR_GANGED : UHD_PWR_INDIVIDUAL) 2579 /* XXX overcurrent */ 2580 ); 2581 hubd.bPwrOn2PwrGood = OHCI_GET_POTPGT(v); 2582 v = OREAD4(sc, OHCI_RH_DESCRIPTOR_B); 2583 for (i = 0, l = sc->sc_noport; l > 0; i++, l -= 8, v >>= 8) 2584 hubd.DeviceRemovable[i++] = (u_int8_t)v; 2585 hubd.bDescLength = USB_HUB_DESCRIPTOR_SIZE + i; 2586 l = min(len, hubd.bDescLength); 2587 totlen = l; 2588 memcpy(buf, &hubd, l); 2589 break; 2590 case C(UR_GET_STATUS, UT_READ_CLASS_DEVICE): 2591 if (len != 4) { 2592 err = USBD_IOERROR; 2593 goto ret; 2594 } 2595 memset(buf, 0, len); /* ? XXX */ 2596 totlen = len; 2597 break; 2598 case C(UR_GET_STATUS, UT_READ_CLASS_OTHER): 2599 DPRINTFN(8,("ohci_root_ctrl_transfer: get port status i=%d\n", 2600 index)); 2601 if (index < 1 || index > sc->sc_noport) { 2602 err = USBD_IOERROR; 2603 goto ret; 2604 } 2605 if (len != 4) { 2606 err = USBD_IOERROR; 2607 goto ret; 2608 } 2609 v = OREAD4(sc, OHCI_RH_PORT_STATUS(index)); 2610 DPRINTFN(8,("ohci_root_ctrl_transfer: port status=0x%04x\n", 2611 v)); 2612 USETW(ps.wPortStatus, v); 2613 USETW(ps.wPortChange, v >> 16); 2614 l = min(len, sizeof ps); 2615 memcpy(buf, &ps, l); 2616 totlen = l; 2617 break; 2618 case C(UR_SET_DESCRIPTOR, UT_WRITE_CLASS_DEVICE): 2619 err = USBD_IOERROR; 2620 goto ret; 2621 case C(UR_SET_FEATURE, UT_WRITE_CLASS_DEVICE): 2622 break; 2623 case C(UR_SET_FEATURE, UT_WRITE_CLASS_OTHER): 2624 if (index < 1 || index > sc->sc_noport) { 2625 err = USBD_IOERROR; 2626 goto ret; 2627 } 2628 port = OHCI_RH_PORT_STATUS(index); 2629 switch(value) { 2630 case UHF_PORT_ENABLE: 2631 OWRITE4(sc, port, UPS_PORT_ENABLED); 2632 break; 2633 case UHF_PORT_SUSPEND: 2634 OWRITE4(sc, port, UPS_SUSPEND); 2635 break; 2636 case UHF_PORT_RESET: 2637 DPRINTFN(5,("ohci_root_ctrl_transfer: reset port %d\n", 2638 index)); 2639 OWRITE4(sc, port, UPS_RESET); 2640 for (i = 0; i < 5; i++) { 2641 usb_delay_ms(&sc->sc_bus, 2642 USB_PORT_ROOT_RESET_DELAY); 2643 if (sc->sc_dying) { 2644 err = USBD_IOERROR; 2645 goto ret; 2646 } 2647 if ((OREAD4(sc, port) & UPS_RESET) == 0) 2648 break; 2649 } 2650 DPRINTFN(8,("ohci port %d reset, status = 0x%04x\n", 2651 index, OREAD4(sc, port))); 2652 break; 2653 case UHF_PORT_POWER: 2654 DPRINTFN(2,("ohci_root_ctrl_transfer: set port power " 2655 "%d\n", index)); 2656 OWRITE4(sc, port, UPS_PORT_POWER); 2657 break; 2658 case UHF_PORT_DISOWN_TO_1_1: 2659 /* accept, but do nothing */ 2660 break; 2661 default: 2662 err = USBD_IOERROR; 2663 goto ret; 2664 } 2665 break; 2666 default: 2667 err = USBD_IOERROR; 2668 goto ret; 2669 } 2670 xfer->actlen = totlen; 2671 err = USBD_NORMAL_COMPLETION; 2672 ret: 2673 xfer->status = err; 2674 s = splusb(); 2675 usb_transfer_complete(xfer); 2676 splx(s); 2677 return (USBD_IN_PROGRESS); 2678 } 2679 2680 /* Abort a root control request. */ 2681 void 2682 ohci_root_ctrl_abort(usbd_xfer_handle xfer) 2683 { 2684 /* Nothing to do, all transfers are synchronous. */ 2685 } 2686 2687 /* Close the root pipe. */ 2688 void 2689 ohci_root_ctrl_close(usbd_pipe_handle pipe) 2690 { 2691 DPRINTF(("ohci_root_ctrl_close\n")); 2692 /* Nothing to do. */ 2693 } 2694 2695 usbd_status 2696 ohci_root_intr_transfer(usbd_xfer_handle xfer) 2697 { 2698 usbd_status err; 2699 2700 /* Insert last in queue. */ 2701 err = usb_insert_transfer(xfer); 2702 if (err) 2703 return (err); 2704 2705 /* Pipe isn't running, start first */ 2706 return (ohci_root_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2707 } 2708 2709 usbd_status 2710 ohci_root_intr_start(usbd_xfer_handle xfer) 2711 { 2712 usbd_pipe_handle pipe = xfer->pipe; 2713 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 2714 2715 if (sc->sc_dying) 2716 return (USBD_IOERROR); 2717 2718 sc->sc_intrxfer = xfer; 2719 2720 return (USBD_IN_PROGRESS); 2721 } 2722 2723 /* Abort a root interrupt request. */ 2724 void 2725 ohci_root_intr_abort(usbd_xfer_handle xfer) 2726 { 2727 int s; 2728 2729 if (xfer->pipe->intrxfer == xfer) { 2730 DPRINTF(("ohci_root_intr_abort: remove\n")); 2731 xfer->pipe->intrxfer = NULL; 2732 } 2733 xfer->status = USBD_CANCELLED; 2734 s = splusb(); 2735 usb_transfer_complete(xfer); 2736 splx(s); 2737 } 2738 2739 /* Close the root pipe. */ 2740 void 2741 ohci_root_intr_close(usbd_pipe_handle pipe) 2742 { 2743 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 2744 2745 DPRINTF(("ohci_root_intr_close\n")); 2746 2747 sc->sc_intrxfer = NULL; 2748 } 2749 2750 /************************/ 2751 2752 usbd_status 2753 ohci_device_ctrl_transfer(usbd_xfer_handle xfer) 2754 { 2755 usbd_status err; 2756 2757 /* Insert last in queue. */ 2758 err = usb_insert_transfer(xfer); 2759 if (err) 2760 return (err); 2761 2762 /* Pipe isn't running, start first */ 2763 return (ohci_device_ctrl_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2764 } 2765 2766 usbd_status 2767 ohci_device_ctrl_start(usbd_xfer_handle xfer) 2768 { 2769 ohci_softc_t *sc = (ohci_softc_t *)xfer->pipe->device->bus; 2770 usbd_status err; 2771 2772 if (sc->sc_dying) 2773 return (USBD_IOERROR); 2774 2775 #ifdef DIAGNOSTIC 2776 if (!(xfer->rqflags & URQ_REQUEST)) { 2777 /* XXX panic */ 2778 printf("ohci_device_ctrl_transfer: not a request\n"); 2779 return (USBD_INVAL); 2780 } 2781 #endif 2782 2783 err = ohci_device_request(xfer); 2784 if (err) 2785 return (err); 2786 2787 if (sc->sc_bus.use_polling) 2788 ohci_waitintr(sc, xfer); 2789 2790 return (USBD_IN_PROGRESS); 2791 } 2792 2793 /* Abort a device control request. */ 2794 void 2795 ohci_device_ctrl_abort(usbd_xfer_handle xfer) 2796 { 2797 DPRINTF(("ohci_device_ctrl_abort: xfer=%p\n", xfer)); 2798 ohci_abort_xfer(xfer, USBD_CANCELLED); 2799 } 2800 2801 /* Close a device control pipe. */ 2802 void 2803 ohci_device_ctrl_close(usbd_pipe_handle pipe) 2804 { 2805 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 2806 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 2807 2808 DPRINTF(("ohci_device_ctrl_close: pipe=%p\n", pipe)); 2809 ohci_close_pipe(pipe, sc->sc_ctrl_head); 2810 ohci_free_std(sc, opipe->tail.td); 2811 } 2812 2813 /************************/ 2814 2815 void 2816 ohci_device_clear_toggle(usbd_pipe_handle pipe) 2817 { 2818 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 2819 2820 opipe->sed->ed.ed_headp &= htole32(~OHCI_TOGGLECARRY); 2821 } 2822 2823 void 2824 ohci_noop(usbd_pipe_handle pipe) 2825 { 2826 } 2827 2828 usbd_status 2829 ohci_device_bulk_transfer(usbd_xfer_handle xfer) 2830 { 2831 usbd_status err; 2832 2833 /* Insert last in queue. */ 2834 err = usb_insert_transfer(xfer); 2835 if (err) 2836 return (err); 2837 2838 /* Pipe isn't running, start first */ 2839 return (ohci_device_bulk_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2840 } 2841 2842 usbd_status 2843 ohci_device_bulk_start(usbd_xfer_handle xfer) 2844 { 2845 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 2846 usbd_device_handle dev = opipe->pipe.device; 2847 ohci_softc_t *sc = (ohci_softc_t *)dev->bus; 2848 int addr = dev->address; 2849 ohci_soft_td_t *data, *tail, *tdp; 2850 ohci_soft_ed_t *sed; 2851 u_int len; 2852 int s, isread, endpt; 2853 usbd_status err; 2854 2855 if (sc->sc_dying) 2856 return (USBD_IOERROR); 2857 2858 #ifdef DIAGNOSTIC 2859 if (xfer->rqflags & URQ_REQUEST) { 2860 /* XXX panic */ 2861 printf("ohci_device_bulk_start: a request\n"); 2862 return (USBD_INVAL); 2863 } 2864 #endif 2865 2866 len = xfer->length; 2867 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress; 2868 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 2869 sed = opipe->sed; 2870 2871 DPRINTFN(4,("ohci_device_bulk_start: xfer=%p len=%u isread=%d " 2872 "flags=%d endpt=%d\n", xfer, len, isread, xfer->flags, 2873 endpt)); 2874 2875 opipe->u.bulk.isread = isread; 2876 opipe->u.bulk.length = len; 2877 2878 /* Update device address */ 2879 sed->ed.ed_flags = htole32( 2880 (letoh32(sed->ed.ed_flags) & ~OHCI_ED_ADDRMASK) | 2881 OHCI_ED_SET_FA(addr)); 2882 2883 /* Allocate a chain of new TDs (including a new tail). */ 2884 data = opipe->tail.td; 2885 err = ohci_alloc_std_chain(opipe, sc, len, isread, xfer, 2886 data, &tail); 2887 /* We want interrupt at the end of the transfer. */ 2888 tail->td.td_flags &= htole32(~OHCI_TD_INTR_MASK); 2889 tail->td.td_flags |= htole32(OHCI_TD_SET_DI(1)); 2890 tail->flags |= OHCI_CALL_DONE; 2891 tail = tail->nexttd; /* point at sentinel */ 2892 if (err) 2893 return (err); 2894 2895 tail->xfer = NULL; 2896 xfer->hcpriv = data; 2897 2898 DPRINTFN(4,("ohci_device_bulk_start: ed_flags=0x%08x td_flags=0x%08x " 2899 "td_cbp=0x%08x td_be=0x%08x\n", 2900 (int)letoh32(sed->ed.ed_flags), 2901 (int)letoh32(data->td.td_flags), 2902 (int)letoh32(data->td.td_cbp), 2903 (int)letoh32(data->td.td_be))); 2904 2905 #ifdef OHCI_DEBUG 2906 if (ohcidebug > 5) { 2907 ohci_dump_ed(sed); 2908 ohci_dump_tds(data); 2909 } 2910 #endif 2911 2912 /* Insert ED in schedule */ 2913 s = splusb(); 2914 for (tdp = data; tdp != tail; tdp = tdp->nexttd) { 2915 tdp->xfer = xfer; 2916 } 2917 sed->ed.ed_tailp = htole32(tail->physaddr); 2918 opipe->tail.td = tail; 2919 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); 2920 OWRITE4(sc, OHCI_COMMAND_STATUS, OHCI_BLF); 2921 if (xfer->timeout && !sc->sc_bus.use_polling) { 2922 timeout_del(&xfer->timeout_handle); 2923 timeout_set(&xfer->timeout_handle, ohci_timeout, xfer); 2924 timeout_add(&xfer->timeout_handle, mstohz(xfer->timeout)); 2925 } 2926 2927 #if 0 2928 /* This goes wrong if we are too slow. */ 2929 if (ohcidebug > 10) { 2930 delay(10000); 2931 DPRINTF(("ohci_device_intr_transfer: status=%x\n", 2932 OREAD4(sc, OHCI_COMMAND_STATUS))); 2933 ohci_dump_ed(sed); 2934 ohci_dump_tds(data); 2935 } 2936 #endif 2937 2938 splx(s); 2939 2940 if (sc->sc_bus.use_polling) 2941 ohci_waitintr(sc, xfer); 2942 2943 return (USBD_IN_PROGRESS); 2944 } 2945 2946 void 2947 ohci_device_bulk_abort(usbd_xfer_handle xfer) 2948 { 2949 DPRINTF(("ohci_device_bulk_abort: xfer=%p\n", xfer)); 2950 ohci_abort_xfer(xfer, USBD_CANCELLED); 2951 } 2952 2953 /* 2954 * Close a device bulk pipe. 2955 */ 2956 void 2957 ohci_device_bulk_close(usbd_pipe_handle pipe) 2958 { 2959 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 2960 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 2961 2962 DPRINTF(("ohci_device_bulk_close: pipe=%p\n", pipe)); 2963 ohci_close_pipe(pipe, sc->sc_bulk_head); 2964 ohci_free_std(sc, opipe->tail.td); 2965 } 2966 2967 /************************/ 2968 2969 usbd_status 2970 ohci_device_intr_transfer(usbd_xfer_handle xfer) 2971 { 2972 usbd_status err; 2973 2974 /* Insert last in queue. */ 2975 err = usb_insert_transfer(xfer); 2976 if (err) 2977 return (err); 2978 2979 /* Pipe isn't running, start first */ 2980 return (ohci_device_intr_start(SIMPLEQ_FIRST(&xfer->pipe->queue))); 2981 } 2982 2983 usbd_status 2984 ohci_device_intr_start(usbd_xfer_handle xfer) 2985 { 2986 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 2987 usbd_device_handle dev = opipe->pipe.device; 2988 ohci_softc_t *sc = (ohci_softc_t *)dev->bus; 2989 ohci_soft_ed_t *sed = opipe->sed; 2990 ohci_soft_td_t *data, *tail; 2991 int s, len, isread, endpt; 2992 2993 if (sc->sc_dying) 2994 return (USBD_IOERROR); 2995 2996 DPRINTFN(3, ("ohci_device_intr_transfer: xfer=%p len=%u " 2997 "flags=%d priv=%p\n", 2998 xfer, xfer->length, xfer->flags, xfer->priv)); 2999 3000 #ifdef DIAGNOSTIC 3001 if (xfer->rqflags & URQ_REQUEST) 3002 panic("ohci_device_intr_transfer: a request"); 3003 #endif 3004 3005 len = xfer->length; 3006 endpt = xfer->pipe->endpoint->edesc->bEndpointAddress; 3007 isread = UE_GET_DIR(endpt) == UE_DIR_IN; 3008 3009 data = opipe->tail.td; 3010 tail = ohci_alloc_std(sc); 3011 if (tail == NULL) 3012 return (USBD_NOMEM); 3013 tail->xfer = NULL; 3014 3015 data->td.td_flags = htole32( 3016 isread ? OHCI_TD_IN : OHCI_TD_OUT | 3017 OHCI_TD_NOCC | 3018 OHCI_TD_SET_DI(1) | OHCI_TD_TOGGLE_CARRY); 3019 if (xfer->flags & USBD_SHORT_XFER_OK) 3020 data->td.td_flags |= htole32(OHCI_TD_R); 3021 data->td.td_cbp = htole32(DMAADDR(&xfer->dmabuf, 0)); 3022 data->nexttd = tail; 3023 data->td.td_nexttd = htole32(tail->physaddr); 3024 data->td.td_be = htole32(letoh32(data->td.td_cbp) + len - 1); 3025 data->len = len; 3026 data->xfer = xfer; 3027 data->flags = OHCI_CALL_DONE | OHCI_ADD_LEN; 3028 xfer->hcpriv = data; 3029 3030 #ifdef OHCI_DEBUG 3031 if (ohcidebug > 5) { 3032 DPRINTF(("ohci_device_intr_transfer:\n")); 3033 ohci_dump_ed(sed); 3034 ohci_dump_tds(data); 3035 } 3036 #endif 3037 3038 /* Insert ED in schedule */ 3039 s = splusb(); 3040 sed->ed.ed_tailp = htole32(tail->physaddr); 3041 opipe->tail.td = tail; 3042 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); 3043 3044 #if 0 3045 /* 3046 * This goes horribly wrong, printing thousands of descriptors, 3047 * because false references are followed due to the fact that the 3048 * TD is gone. 3049 */ 3050 if (ohcidebug > 5) { 3051 usb_delay_ms(&sc->sc_bus, 5); 3052 DPRINTF(("ohci_device_intr_transfer: status=%x\n", 3053 OREAD4(sc, OHCI_COMMAND_STATUS))); 3054 ohci_dump_ed(sed); 3055 ohci_dump_tds(data); 3056 } 3057 #endif 3058 splx(s); 3059 3060 return (USBD_IN_PROGRESS); 3061 } 3062 3063 /* Abort a device control request. */ 3064 void 3065 ohci_device_intr_abort(usbd_xfer_handle xfer) 3066 { 3067 if (xfer->pipe->intrxfer == xfer) { 3068 DPRINTF(("ohci_device_intr_abort: remove\n")); 3069 xfer->pipe->intrxfer = NULL; 3070 } 3071 ohci_abort_xfer(xfer, USBD_CANCELLED); 3072 } 3073 3074 /* Close a device interrupt pipe. */ 3075 void 3076 ohci_device_intr_close(usbd_pipe_handle pipe) 3077 { 3078 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 3079 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 3080 int nslots = opipe->u.intr.nslots; 3081 int pos = opipe->u.intr.pos; 3082 int j; 3083 ohci_soft_ed_t *p, *sed = opipe->sed; 3084 int s; 3085 3086 DPRINTFN(1,("ohci_device_intr_close: pipe=%p nslots=%d pos=%d\n", 3087 pipe, nslots, pos)); 3088 s = splusb(); 3089 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); 3090 if ((letoh32(sed->ed.ed_tailp) & OHCI_HEADMASK) != 3091 (letoh32(sed->ed.ed_headp) & OHCI_HEADMASK)) 3092 usb_delay_ms(&sc->sc_bus, 2); 3093 3094 for (p = sc->sc_eds[pos]; p && p->next != sed; p = p->next) 3095 ; 3096 #ifdef DIAGNOSTIC 3097 if (p == NULL) 3098 panic("ohci_device_intr_close: ED not found"); 3099 #endif 3100 p->next = sed->next; 3101 p->ed.ed_nexted = sed->ed.ed_nexted; 3102 splx(s); 3103 3104 for (j = 0; j < nslots; j++) 3105 --sc->sc_bws[(pos * nslots + j) % OHCI_NO_INTRS]; 3106 3107 ohci_free_std(sc, opipe->tail.td); 3108 ohci_free_sed(sc, opipe->sed); 3109 } 3110 3111 usbd_status 3112 ohci_device_setintr(ohci_softc_t *sc, struct ohci_pipe *opipe, int ival) 3113 { 3114 int i, j, s, best; 3115 u_int npoll, slow, shigh, nslots; 3116 u_int bestbw, bw; 3117 ohci_soft_ed_t *hsed, *sed = opipe->sed; 3118 3119 DPRINTFN(2, ("ohci_setintr: pipe=%p\n", opipe)); 3120 if (ival == 0) { 3121 printf("ohci_setintr: 0 interval\n"); 3122 return (USBD_INVAL); 3123 } 3124 3125 npoll = OHCI_NO_INTRS; 3126 while (npoll > ival) 3127 npoll /= 2; 3128 DPRINTFN(2, ("ohci_setintr: ival=%d npoll=%d\n", ival, npoll)); 3129 3130 /* 3131 * We now know which level in the tree the ED must go into. 3132 * Figure out which slot has most bandwidth left over. 3133 * Slots to examine: 3134 * npoll 3135 * 1 0 3136 * 2 1 2 3137 * 4 3 4 5 6 3138 * 8 7 8 9 10 11 12 13 14 3139 * N (N-1) .. (N-1+N-1) 3140 */ 3141 slow = npoll-1; 3142 shigh = slow + npoll; 3143 nslots = OHCI_NO_INTRS / npoll; 3144 for (best = i = slow, bestbw = ~0; i < shigh; i++) { 3145 bw = 0; 3146 for (j = 0; j < nslots; j++) 3147 bw += sc->sc_bws[(i * nslots + j) % OHCI_NO_INTRS]; 3148 if (bw < bestbw) { 3149 best = i; 3150 bestbw = bw; 3151 } 3152 } 3153 DPRINTFN(2, ("ohci_setintr: best=%d(%d..%d) bestbw=%d\n", 3154 best, slow, shigh, bestbw)); 3155 3156 s = splusb(); 3157 hsed = sc->sc_eds[best]; 3158 sed->next = hsed->next; 3159 sed->ed.ed_nexted = hsed->ed.ed_nexted; 3160 hsed->next = sed; 3161 hsed->ed.ed_nexted = htole32(sed->physaddr); 3162 splx(s); 3163 3164 for (j = 0; j < nslots; j++) 3165 ++sc->sc_bws[(best * nslots + j) % OHCI_NO_INTRS]; 3166 opipe->u.intr.nslots = nslots; 3167 opipe->u.intr.pos = best; 3168 3169 DPRINTFN(5, ("ohci_setintr: returns %p\n", opipe)); 3170 return (USBD_NORMAL_COMPLETION); 3171 } 3172 3173 /***********************/ 3174 3175 usbd_status 3176 ohci_device_isoc_transfer(usbd_xfer_handle xfer) 3177 { 3178 usbd_status err; 3179 3180 DPRINTFN(5,("ohci_device_isoc_transfer: xfer=%p\n", xfer)); 3181 3182 /* Put it on our queue, */ 3183 err = usb_insert_transfer(xfer); 3184 3185 /* bail out on error, */ 3186 if (err && err != USBD_IN_PROGRESS) 3187 return (err); 3188 3189 /* XXX should check inuse here */ 3190 3191 /* insert into schedule, */ 3192 ohci_device_isoc_enter(xfer); 3193 3194 /* and start if the pipe wasn't running */ 3195 if (!err) 3196 ohci_device_isoc_start(SIMPLEQ_FIRST(&xfer->pipe->queue)); 3197 3198 return (err); 3199 } 3200 3201 void 3202 ohci_device_isoc_enter(usbd_xfer_handle xfer) 3203 { 3204 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 3205 usbd_device_handle dev = opipe->pipe.device; 3206 ohci_softc_t *sc = (ohci_softc_t *)dev->bus; 3207 ohci_soft_ed_t *sed = opipe->sed; 3208 struct iso *iso = &opipe->u.iso; 3209 ohci_soft_itd_t *sitd, *nsitd; 3210 ohci_physaddr_t buf, offs, noffs, bp0; 3211 int i, ncur, nframes; 3212 int s; 3213 3214 DPRINTFN(1,("ohci_device_isoc_enter: used=%d next=%d xfer=%p " 3215 "nframes=%d\n", 3216 iso->inuse, iso->next, xfer, xfer->nframes)); 3217 3218 if (sc->sc_dying) 3219 return; 3220 3221 if (iso->next == -1) { 3222 /* Not in use yet, schedule it a few frames ahead. */ 3223 iso->next = letoh32(sc->sc_hcca->hcca_frame_number) + 5; 3224 DPRINTFN(2,("ohci_device_isoc_enter: start next=%d\n", 3225 iso->next)); 3226 } 3227 3228 sitd = opipe->tail.itd; 3229 buf = DMAADDR(&xfer->dmabuf, 0); 3230 bp0 = OHCI_PAGE(buf); 3231 offs = OHCI_PAGE_OFFSET(buf); 3232 nframes = xfer->nframes; 3233 xfer->hcpriv = sitd; 3234 for (i = ncur = 0; i < nframes; i++, ncur++) { 3235 noffs = offs + xfer->frlengths[i]; 3236 if (ncur == OHCI_ITD_NOFFSET || /* all offsets used */ 3237 OHCI_PAGE(buf + noffs) > bp0 + OHCI_PAGE_SIZE) { /* too many page crossings */ 3238 3239 /* Allocate next ITD */ 3240 nsitd = ohci_alloc_sitd(sc); 3241 if (nsitd == NULL) { 3242 /* XXX what now? */ 3243 printf("%s: isoc TD alloc failed\n", 3244 sc->sc_bus.bdev.dv_xname); 3245 return; 3246 } 3247 3248 /* Fill current ITD */ 3249 sitd->itd.itd_flags = htole32( 3250 OHCI_ITD_NOCC | 3251 OHCI_ITD_SET_SF(iso->next) | 3252 OHCI_ITD_SET_DI(6) | /* delay intr a little */ 3253 OHCI_ITD_SET_FC(ncur)); 3254 sitd->itd.itd_bp0 = htole32(bp0); 3255 sitd->nextitd = nsitd; 3256 sitd->itd.itd_nextitd = htole32(nsitd->physaddr); 3257 sitd->itd.itd_be = htole32(bp0 + offs - 1); 3258 sitd->xfer = xfer; 3259 sitd->flags = 0; 3260 3261 sitd = nsitd; 3262 iso->next = iso->next + ncur; 3263 bp0 = OHCI_PAGE(buf + offs); 3264 ncur = 0; 3265 } 3266 sitd->itd.itd_offset[ncur] = htole16(OHCI_ITD_MK_OFFS(offs)); 3267 offs = noffs; 3268 } 3269 nsitd = ohci_alloc_sitd(sc); 3270 if (nsitd == NULL) { 3271 /* XXX what now? */ 3272 printf("%s: isoc TD alloc failed\n", 3273 sc->sc_bus.bdev.dv_xname); 3274 return; 3275 } 3276 /* Fixup last used ITD */ 3277 sitd->itd.itd_flags = htole32( 3278 OHCI_ITD_NOCC | 3279 OHCI_ITD_SET_SF(iso->next) | 3280 OHCI_ITD_SET_DI(0) | 3281 OHCI_ITD_SET_FC(ncur)); 3282 sitd->itd.itd_bp0 = htole32(bp0); 3283 sitd->nextitd = nsitd; 3284 sitd->itd.itd_nextitd = htole32(nsitd->physaddr); 3285 sitd->itd.itd_be = htole32(bp0 + offs - 1); 3286 sitd->xfer = xfer; 3287 sitd->flags = OHCI_CALL_DONE; 3288 3289 iso->next = iso->next + ncur; 3290 iso->inuse += nframes; 3291 3292 xfer->actlen = offs; /* XXX pretend we did it all */ 3293 3294 xfer->status = USBD_IN_PROGRESS; 3295 3296 #ifdef OHCI_DEBUG 3297 if (ohcidebug > 5) { 3298 DPRINTF(("ohci_device_isoc_enter: frame=%d\n", 3299 letoh32(sc->sc_hcca->hcca_frame_number))); 3300 ohci_dump_itds(xfer->hcpriv); 3301 ohci_dump_ed(sed); 3302 } 3303 #endif 3304 3305 s = splusb(); 3306 sed->ed.ed_tailp = htole32(nsitd->physaddr); 3307 opipe->tail.itd = nsitd; 3308 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); 3309 splx(s); 3310 3311 #ifdef OHCI_DEBUG 3312 if (ohcidebug > 5) { 3313 delay(150000); 3314 DPRINTF(("ohci_device_isoc_enter: after frame=%d\n", 3315 letoh32(sc->sc_hcca->hcca_frame_number))); 3316 ohci_dump_itds(xfer->hcpriv); 3317 ohci_dump_ed(sed); 3318 } 3319 #endif 3320 } 3321 3322 usbd_status 3323 ohci_device_isoc_start(usbd_xfer_handle xfer) 3324 { 3325 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 3326 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus; 3327 3328 DPRINTFN(5,("ohci_device_isoc_start: xfer=%p\n", xfer)); 3329 3330 if (sc->sc_dying) 3331 return (USBD_IOERROR); 3332 3333 #ifdef DIAGNOSTIC 3334 if (xfer->status != USBD_IN_PROGRESS) 3335 printf("ohci_device_isoc_start: not in progress %p\n", xfer); 3336 #endif 3337 3338 /* XXX anything to do? */ 3339 3340 return (USBD_IN_PROGRESS); 3341 } 3342 3343 void 3344 ohci_device_isoc_abort(usbd_xfer_handle xfer) 3345 { 3346 struct ohci_pipe *opipe = (struct ohci_pipe *)xfer->pipe; 3347 ohci_softc_t *sc = (ohci_softc_t *)opipe->pipe.device->bus; 3348 ohci_soft_ed_t *sed; 3349 ohci_soft_itd_t *sitd; 3350 int s; 3351 3352 s = splusb(); 3353 3354 DPRINTFN(1,("ohci_device_isoc_abort: xfer=%p\n", xfer)); 3355 3356 /* Transfer is already done. */ 3357 if (xfer->status != USBD_NOT_STARTED && 3358 xfer->status != USBD_IN_PROGRESS) { 3359 splx(s); 3360 printf("ohci_device_isoc_abort: early return\n"); 3361 return; 3362 } 3363 3364 /* Give xfer the requested abort code. */ 3365 xfer->status = USBD_CANCELLED; 3366 3367 sed = opipe->sed; 3368 sed->ed.ed_flags |= htole32(OHCI_ED_SKIP); /* force hardware skip */ 3369 3370 sitd = xfer->hcpriv; 3371 #ifdef DIAGNOSTIC 3372 if (sitd == NULL) { 3373 splx(s); 3374 printf("ohci_device_isoc_abort: hcpriv==0\n"); 3375 return; 3376 } 3377 #endif 3378 for (; sitd->xfer == xfer; sitd = sitd->nextitd) { 3379 #ifdef DIAGNOSTIC 3380 DPRINTFN(1,("abort sets done sitd=%p\n", sitd)); 3381 sitd->isdone = 1; 3382 #endif 3383 } 3384 3385 splx(s); 3386 3387 usb_delay_ms(&sc->sc_bus, OHCI_ITD_NOFFSET); 3388 3389 s = splusb(); 3390 3391 /* Run callback. */ 3392 usb_transfer_complete(xfer); 3393 3394 sed->ed.ed_headp = htole32(sitd->physaddr); /* unlink TDs */ 3395 sed->ed.ed_flags &= htole32(~OHCI_ED_SKIP); /* remove hardware skip */ 3396 3397 splx(s); 3398 } 3399 3400 void 3401 ohci_device_isoc_done(usbd_xfer_handle xfer) 3402 { 3403 DPRINTFN(1,("ohci_device_isoc_done: xfer=%p\n", xfer)); 3404 } 3405 3406 usbd_status 3407 ohci_setup_isoc(usbd_pipe_handle pipe) 3408 { 3409 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 3410 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 3411 struct iso *iso = &opipe->u.iso; 3412 int s; 3413 3414 iso->next = -1; 3415 iso->inuse = 0; 3416 3417 s = splusb(); 3418 ohci_add_ed(opipe->sed, sc->sc_isoc_head); 3419 splx(s); 3420 3421 return (USBD_NORMAL_COMPLETION); 3422 } 3423 3424 void 3425 ohci_device_isoc_close(usbd_pipe_handle pipe) 3426 { 3427 struct ohci_pipe *opipe = (struct ohci_pipe *)pipe; 3428 ohci_softc_t *sc = (ohci_softc_t *)pipe->device->bus; 3429 3430 DPRINTF(("ohci_device_isoc_close: pipe=%p\n", pipe)); 3431 ohci_close_pipe(pipe, sc->sc_isoc_head); 3432 #ifdef DIAGNOSTIC 3433 opipe->tail.itd->isdone = 1; 3434 #endif 3435 ohci_free_sitd(sc, opipe->tail.itd); 3436 } 3437