1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Thanks to Mentor Graphics for providing a reference driver for this USB chip 29 * at their homepage. 30 */ 31 32 /* 33 * This file contains the driver for the Mentor Graphics Inventra USB 34 * 2.0 High Speed Dual-Role controller. 35 * 36 * NOTE: The current implementation only supports Device Side Mode! 37 */ 38 39 #include <sys/stdint.h> 40 #include <sys/stddef.h> 41 #include <sys/param.h> 42 #include <sys/queue.h> 43 #include <sys/types.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/bus.h> 47 #include <sys/module.h> 48 #include <sys/lock.h> 49 #include <sys/mutex.h> 50 #include <sys/condvar.h> 51 #include <sys/sysctl.h> 52 #include <sys/sx.h> 53 #include <sys/unistd.h> 54 #include <sys/callout.h> 55 #include <sys/malloc.h> 56 #include <sys/priv.h> 57 58 #include <dev/usb/usb.h> 59 #include <dev/usb/usbdi.h> 60 61 #define USB_DEBUG_VAR musbotgdebug 62 63 #include <dev/usb/usb_core.h> 64 #include <dev/usb/usb_debug.h> 65 #include <dev/usb/usb_busdma.h> 66 #include <dev/usb/usb_process.h> 67 #include <dev/usb/usb_transfer.h> 68 #include <dev/usb/usb_device.h> 69 #include <dev/usb/usb_hub.h> 70 #include <dev/usb/usb_util.h> 71 72 #include <dev/usb/usb_controller.h> 73 #include <dev/usb/usb_bus.h> 74 #include <dev/usb/controller/musb_otg.h> 75 76 #define MUSBOTG_INTR_ENDPT 1 77 78 #define MUSBOTG_BUS2SC(bus) \ 79 ((struct musbotg_softc *)(((uint8_t *)(bus)) - \ 80 USB_P2U(&(((struct musbotg_softc *)0)->sc_bus)))) 81 82 #define MUSBOTG_PC2SC(pc) \ 83 MUSBOTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus) 84 85 #ifdef USB_DEBUG 86 static int musbotgdebug = 0; 87 88 static SYSCTL_NODE(_hw_usb, OID_AUTO, musbotg, CTLFLAG_RW, 0, "USB musbotg"); 89 SYSCTL_INT(_hw_usb_musbotg, OID_AUTO, debug, CTLFLAG_RW, 90 &musbotgdebug, 0, "Debug level"); 91 #endif 92 93 /* prototypes */ 94 95 struct usb_bus_methods musbotg_bus_methods; 96 struct usb_pipe_methods musbotg_device_bulk_methods; 97 struct usb_pipe_methods musbotg_device_ctrl_methods; 98 struct usb_pipe_methods musbotg_device_intr_methods; 99 struct usb_pipe_methods musbotg_device_isoc_methods; 100 101 static musbotg_cmd_t musbotg_setup_rx; 102 static musbotg_cmd_t musbotg_setup_data_rx; 103 static musbotg_cmd_t musbotg_setup_data_tx; 104 static musbotg_cmd_t musbotg_setup_status; 105 static musbotg_cmd_t musbotg_data_rx; 106 static musbotg_cmd_t musbotg_data_tx; 107 static void musbotg_device_done(struct usb_xfer *, usb_error_t); 108 static void musbotg_do_poll(struct usb_bus *); 109 static void musbotg_standard_done(struct usb_xfer *); 110 static void musbotg_interrupt_poll(struct musbotg_softc *); 111 static void musbotg_root_intr(struct musbotg_softc *); 112 113 /* 114 * Here is a configuration that the chip supports. 115 */ 116 static const struct usb_hw_ep_profile musbotg_ep_profile[1] = { 117 118 [0] = { 119 .max_in_frame_size = 64,/* fixed */ 120 .max_out_frame_size = 64, /* fixed */ 121 .is_simplex = 1, 122 .support_control = 1, 123 } 124 }; 125 126 static void 127 musbotg_get_hw_ep_profile(struct usb_device *udev, 128 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr) 129 { 130 struct musbotg_softc *sc; 131 132 sc = MUSBOTG_BUS2SC(udev->bus); 133 134 if (ep_addr == 0) { 135 /* control endpoint */ 136 *ppf = musbotg_ep_profile; 137 } else if (ep_addr <= sc->sc_ep_max) { 138 /* other endpoints */ 139 *ppf = sc->sc_hw_ep_profile + ep_addr; 140 } else { 141 *ppf = NULL; 142 } 143 } 144 145 static void 146 musbotg_clocks_on(struct musbotg_softc *sc) 147 { 148 if (sc->sc_flags.clocks_off && 149 sc->sc_flags.port_powered) { 150 151 DPRINTFN(4, "\n"); 152 153 if (sc->sc_clocks_on) { 154 (sc->sc_clocks_on) (sc->sc_clocks_arg); 155 } 156 sc->sc_flags.clocks_off = 0; 157 158 /* XXX enable Transceiver */ 159 } 160 } 161 162 static void 163 musbotg_clocks_off(struct musbotg_softc *sc) 164 { 165 if (!sc->sc_flags.clocks_off) { 166 167 DPRINTFN(4, "\n"); 168 169 /* XXX disable Transceiver */ 170 171 if (sc->sc_clocks_off) { 172 (sc->sc_clocks_off) (sc->sc_clocks_arg); 173 } 174 sc->sc_flags.clocks_off = 1; 175 } 176 } 177 178 static void 179 musbotg_pull_common(struct musbotg_softc *sc, uint8_t on) 180 { 181 uint8_t temp; 182 183 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 184 if (on) 185 temp |= MUSB2_MASK_SOFTC; 186 else 187 temp &= ~MUSB2_MASK_SOFTC; 188 189 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 190 } 191 192 static void 193 musbotg_pull_up(struct musbotg_softc *sc) 194 { 195 /* pullup D+, if possible */ 196 197 if (!sc->sc_flags.d_pulled_up && 198 sc->sc_flags.port_powered) { 199 sc->sc_flags.d_pulled_up = 1; 200 musbotg_pull_common(sc, 1); 201 } 202 } 203 204 static void 205 musbotg_pull_down(struct musbotg_softc *sc) 206 { 207 /* pulldown D+, if possible */ 208 209 if (sc->sc_flags.d_pulled_up) { 210 sc->sc_flags.d_pulled_up = 0; 211 musbotg_pull_common(sc, 0); 212 } 213 } 214 215 static void 216 musbotg_wakeup_peer(struct musbotg_softc *sc) 217 { 218 uint8_t temp; 219 220 if (!(sc->sc_flags.status_suspend)) { 221 return; 222 } 223 224 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 225 temp |= MUSB2_MASK_RESUME; 226 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 227 228 /* wait 8 milliseconds */ 229 /* Wait for reset to complete. */ 230 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125); 231 232 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 233 temp &= ~MUSB2_MASK_RESUME; 234 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 235 } 236 237 static void 238 musbotg_set_address(struct musbotg_softc *sc, uint8_t addr) 239 { 240 DPRINTFN(4, "addr=%d\n", addr); 241 addr &= 0x7F; 242 MUSB2_WRITE_1(sc, MUSB2_REG_FADDR, addr); 243 } 244 245 static uint8_t 246 musbotg_setup_rx(struct musbotg_td *td) 247 { 248 struct musbotg_softc *sc; 249 struct usb_device_request req; 250 uint16_t count; 251 uint8_t csr; 252 253 /* get pointer to softc */ 254 sc = MUSBOTG_PC2SC(td->pc); 255 256 /* select endpoint 0 */ 257 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 258 259 /* read out FIFO status */ 260 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 261 262 DPRINTFN(4, "csr=0x%02x\n", csr); 263 264 /* 265 * NOTE: If DATAEND is set we should not call the 266 * callback, hence the status stage is not complete. 267 */ 268 if (csr & MUSB2_MASK_CSR0L_DATAEND) { 269 /* do not stall at this point */ 270 td->did_stall = 1; 271 /* wait for interrupt */ 272 goto not_complete; 273 } 274 if (csr & MUSB2_MASK_CSR0L_SENTSTALL) { 275 /* clear SENTSTALL */ 276 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 277 /* get latest status */ 278 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 279 /* update EP0 state */ 280 sc->sc_ep0_busy = 0; 281 } 282 if (csr & MUSB2_MASK_CSR0L_SETUPEND) { 283 /* clear SETUPEND */ 284 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 285 MUSB2_MASK_CSR0L_SETUPEND_CLR); 286 /* get latest status */ 287 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 288 /* update EP0 state */ 289 sc->sc_ep0_busy = 0; 290 } 291 if (sc->sc_ep0_busy) { 292 goto not_complete; 293 } 294 if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) { 295 goto not_complete; 296 } 297 /* clear did stall flag */ 298 td->did_stall = 0; 299 /* get the packet byte count */ 300 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 301 302 /* verify data length */ 303 if (count != td->remainder) { 304 DPRINTFN(0, "Invalid SETUP packet " 305 "length, %d bytes\n", count); 306 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 307 MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 308 goto not_complete; 309 } 310 if (count != sizeof(req)) { 311 DPRINTFN(0, "Unsupported SETUP packet " 312 "length, %d bytes\n", count); 313 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 314 MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 315 goto not_complete; 316 } 317 /* receive data */ 318 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 319 MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req)); 320 321 /* copy data into real buffer */ 322 usbd_copy_in(td->pc, 0, &req, sizeof(req)); 323 324 td->offset = sizeof(req); 325 td->remainder = 0; 326 327 /* set pending command */ 328 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR; 329 330 /* we need set stall or dataend after this */ 331 sc->sc_ep0_busy = 1; 332 333 /* sneak peek the set address */ 334 if ((req.bmRequestType == UT_WRITE_DEVICE) && 335 (req.bRequest == UR_SET_ADDRESS)) { 336 sc->sc_dv_addr = req.wValue[0] & 0x7F; 337 } else { 338 sc->sc_dv_addr = 0xFF; 339 } 340 return (0); /* complete */ 341 342 not_complete: 343 /* abort any ongoing transfer */ 344 if (!td->did_stall) { 345 DPRINTFN(4, "stalling\n"); 346 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 347 MUSB2_MASK_CSR0L_SENDSTALL); 348 td->did_stall = 1; 349 } 350 return (1); /* not complete */ 351 } 352 353 /* Control endpoint only data handling functions (RX/TX/SYNC) */ 354 355 static uint8_t 356 musbotg_setup_data_rx(struct musbotg_td *td) 357 { 358 struct usb_page_search buf_res; 359 struct musbotg_softc *sc; 360 uint16_t count; 361 uint8_t csr; 362 uint8_t got_short; 363 364 /* get pointer to softc */ 365 sc = MUSBOTG_PC2SC(td->pc); 366 367 /* select endpoint 0 */ 368 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 369 370 /* check if a command is pending */ 371 if (sc->sc_ep0_cmd) { 372 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 373 sc->sc_ep0_cmd = 0; 374 } 375 /* read out FIFO status */ 376 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 377 378 DPRINTFN(4, "csr=0x%02x\n", csr); 379 380 got_short = 0; 381 382 if (csr & (MUSB2_MASK_CSR0L_SETUPEND | 383 MUSB2_MASK_CSR0L_SENTSTALL)) { 384 if (td->remainder == 0) { 385 /* 386 * We are actually complete and have 387 * received the next SETUP 388 */ 389 DPRINTFN(4, "faking complete\n"); 390 return (0); /* complete */ 391 } 392 /* 393 * USB Host Aborted the transfer. 394 */ 395 td->error = 1; 396 return (0); /* complete */ 397 } 398 if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) { 399 return (1); /* not complete */ 400 } 401 /* get the packet byte count */ 402 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 403 404 /* verify the packet byte count */ 405 if (count != td->max_frame_size) { 406 if (count < td->max_frame_size) { 407 /* we have a short packet */ 408 td->short_pkt = 1; 409 got_short = 1; 410 } else { 411 /* invalid USB packet */ 412 td->error = 1; 413 return (0); /* we are complete */ 414 } 415 } 416 /* verify the packet byte count */ 417 if (count > td->remainder) { 418 /* invalid USB packet */ 419 td->error = 1; 420 return (0); /* we are complete */ 421 } 422 while (count > 0) { 423 uint32_t temp; 424 425 usbd_get_page(td->pc, td->offset, &buf_res); 426 427 /* get correct length */ 428 if (buf_res.length > count) { 429 buf_res.length = count; 430 } 431 /* check for unaligned memory address */ 432 if (USB_P2U(buf_res.buffer) & 3) { 433 434 temp = count & ~3; 435 436 if (temp) { 437 /* receive data 4 bytes at a time */ 438 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 439 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf, 440 temp / 4); 441 } 442 temp = count & 3; 443 if (temp) { 444 /* receive data 1 byte at a time */ 445 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 446 MUSB2_REG_EPFIFO(0), 447 (void *)(&sc->sc_bounce_buf[count / 4]), temp); 448 } 449 usbd_copy_in(td->pc, td->offset, 450 sc->sc_bounce_buf, count); 451 452 /* update offset and remainder */ 453 td->offset += count; 454 td->remainder -= count; 455 break; 456 } 457 /* check if we can optimise */ 458 if (buf_res.length >= 4) { 459 460 /* receive data 4 bytes at a time */ 461 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 462 MUSB2_REG_EPFIFO(0), buf_res.buffer, 463 buf_res.length / 4); 464 465 temp = buf_res.length & ~3; 466 467 /* update counters */ 468 count -= temp; 469 td->offset += temp; 470 td->remainder -= temp; 471 continue; 472 } 473 /* receive data */ 474 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 475 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length); 476 477 /* update counters */ 478 count -= buf_res.length; 479 td->offset += buf_res.length; 480 td->remainder -= buf_res.length; 481 } 482 483 /* check if we are complete */ 484 if ((td->remainder == 0) || got_short) { 485 if (td->short_pkt) { 486 /* we are complete */ 487 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR; 488 return (0); 489 } 490 /* else need to receive a zero length packet */ 491 } 492 /* write command - need more data */ 493 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 494 MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 495 return (1); /* not complete */ 496 } 497 498 static uint8_t 499 musbotg_setup_data_tx(struct musbotg_td *td) 500 { 501 struct usb_page_search buf_res; 502 struct musbotg_softc *sc; 503 uint16_t count; 504 uint8_t csr; 505 506 /* get pointer to softc */ 507 sc = MUSBOTG_PC2SC(td->pc); 508 509 /* select endpoint 0 */ 510 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 511 512 /* check if a command is pending */ 513 if (sc->sc_ep0_cmd) { 514 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 515 sc->sc_ep0_cmd = 0; 516 } 517 /* read out FIFO status */ 518 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 519 520 DPRINTFN(4, "csr=0x%02x\n", csr); 521 522 if (csr & (MUSB2_MASK_CSR0L_SETUPEND | 523 MUSB2_MASK_CSR0L_SENTSTALL)) { 524 /* 525 * The current transfer was aborted 526 * by the USB Host 527 */ 528 td->error = 1; 529 return (0); /* complete */ 530 } 531 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY) { 532 return (1); /* not complete */ 533 } 534 count = td->max_frame_size; 535 if (td->remainder < count) { 536 /* we have a short packet */ 537 td->short_pkt = 1; 538 count = td->remainder; 539 } 540 while (count > 0) { 541 uint32_t temp; 542 543 usbd_get_page(td->pc, td->offset, &buf_res); 544 545 /* get correct length */ 546 if (buf_res.length > count) { 547 buf_res.length = count; 548 } 549 /* check for unaligned memory address */ 550 if (USB_P2U(buf_res.buffer) & 3) { 551 552 usbd_copy_out(td->pc, td->offset, 553 sc->sc_bounce_buf, count); 554 555 temp = count & ~3; 556 557 if (temp) { 558 /* transmit data 4 bytes at a time */ 559 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 560 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf, 561 temp / 4); 562 } 563 temp = count & 3; 564 if (temp) { 565 /* receive data 1 byte at a time */ 566 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 567 MUSB2_REG_EPFIFO(0), 568 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 569 } 570 /* update offset and remainder */ 571 td->offset += count; 572 td->remainder -= count; 573 break; 574 } 575 /* check if we can optimise */ 576 if (buf_res.length >= 4) { 577 578 /* transmit data 4 bytes at a time */ 579 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 580 MUSB2_REG_EPFIFO(0), buf_res.buffer, 581 buf_res.length / 4); 582 583 temp = buf_res.length & ~3; 584 585 /* update counters */ 586 count -= temp; 587 td->offset += temp; 588 td->remainder -= temp; 589 continue; 590 } 591 /* transmit data */ 592 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 593 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length); 594 595 /* update counters */ 596 count -= buf_res.length; 597 td->offset += buf_res.length; 598 td->remainder -= buf_res.length; 599 } 600 601 /* check remainder */ 602 if (td->remainder == 0) { 603 if (td->short_pkt) { 604 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_TXPKTRDY; 605 return (0); /* complete */ 606 } 607 /* else we need to transmit a short packet */ 608 } 609 /* write command */ 610 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 611 MUSB2_MASK_CSR0L_TXPKTRDY); 612 613 return (1); /* not complete */ 614 } 615 616 static uint8_t 617 musbotg_setup_status(struct musbotg_td *td) 618 { 619 struct musbotg_softc *sc; 620 uint8_t csr; 621 622 /* get pointer to softc */ 623 sc = MUSBOTG_PC2SC(td->pc); 624 625 /* select endpoint 0 */ 626 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 627 628 if (sc->sc_ep0_busy) { 629 sc->sc_ep0_busy = 0; 630 sc->sc_ep0_cmd |= MUSB2_MASK_CSR0L_DATAEND; 631 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 632 sc->sc_ep0_cmd = 0; 633 } 634 /* read out FIFO status */ 635 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 636 637 DPRINTFN(4, "csr=0x%02x\n", csr); 638 639 if (csr & MUSB2_MASK_CSR0L_DATAEND) { 640 /* wait for interrupt */ 641 return (1); /* not complete */ 642 } 643 if (sc->sc_dv_addr != 0xFF) { 644 /* write function address */ 645 musbotg_set_address(sc, sc->sc_dv_addr); 646 } 647 return (0); /* complete */ 648 } 649 650 static uint8_t 651 musbotg_data_rx(struct musbotg_td *td) 652 { 653 struct usb_page_search buf_res; 654 struct musbotg_softc *sc; 655 uint16_t count; 656 uint8_t csr; 657 uint8_t to; 658 uint8_t got_short; 659 660 to = 8; /* don't loop forever! */ 661 got_short = 0; 662 663 /* get pointer to softc */ 664 sc = MUSBOTG_PC2SC(td->pc); 665 666 /* select endpoint */ 667 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->ep_no); 668 669 repeat: 670 /* read out FIFO status */ 671 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 672 673 DPRINTFN(4, "csr=0x%02x\n", csr); 674 675 /* clear overrun */ 676 if (csr & MUSB2_MASK_CSRL_RXOVERRUN) { 677 /* make sure we don't clear "RXPKTRDY" */ 678 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 679 MUSB2_MASK_CSRL_RXPKTRDY); 680 } 681 /* check status */ 682 if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY)) { 683 return (1); /* not complete */ 684 } 685 /* get the packet byte count */ 686 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 687 688 DPRINTFN(4, "count=0x%04x\n", count); 689 690 /* 691 * Check for short or invalid packet: 692 */ 693 if (count != td->max_frame_size) { 694 if (count < td->max_frame_size) { 695 /* we have a short packet */ 696 td->short_pkt = 1; 697 got_short = 1; 698 } else { 699 /* invalid USB packet */ 700 td->error = 1; 701 return (0); /* we are complete */ 702 } 703 } 704 /* verify the packet byte count */ 705 if (count > td->remainder) { 706 /* invalid USB packet */ 707 td->error = 1; 708 return (0); /* we are complete */ 709 } 710 while (count > 0) { 711 uint32_t temp; 712 713 usbd_get_page(td->pc, td->offset, &buf_res); 714 715 /* get correct length */ 716 if (buf_res.length > count) { 717 buf_res.length = count; 718 } 719 /* check for unaligned memory address */ 720 if (USB_P2U(buf_res.buffer) & 3) { 721 722 temp = count & ~3; 723 724 if (temp) { 725 /* receive data 4 bytes at a time */ 726 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 727 MUSB2_REG_EPFIFO(td->ep_no), sc->sc_bounce_buf, 728 temp / 4); 729 } 730 temp = count & 3; 731 if (temp) { 732 /* receive data 1 byte at a time */ 733 bus_space_read_multi_1(sc->sc_io_tag, 734 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->ep_no), 735 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 736 } 737 usbd_copy_in(td->pc, td->offset, 738 sc->sc_bounce_buf, count); 739 740 /* update offset and remainder */ 741 td->offset += count; 742 td->remainder -= count; 743 break; 744 } 745 /* check if we can optimise */ 746 if (buf_res.length >= 4) { 747 748 /* receive data 4 bytes at a time */ 749 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 750 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 751 buf_res.length / 4); 752 753 temp = buf_res.length & ~3; 754 755 /* update counters */ 756 count -= temp; 757 td->offset += temp; 758 td->remainder -= temp; 759 continue; 760 } 761 /* receive data */ 762 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 763 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 764 buf_res.length); 765 766 /* update counters */ 767 count -= buf_res.length; 768 td->offset += buf_res.length; 769 td->remainder -= buf_res.length; 770 } 771 772 /* clear status bits */ 773 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 774 775 /* check if we are complete */ 776 if ((td->remainder == 0) || got_short) { 777 if (td->short_pkt) { 778 /* we are complete */ 779 return (0); 780 } 781 /* else need to receive a zero length packet */ 782 } 783 if (--to) { 784 goto repeat; 785 } 786 return (1); /* not complete */ 787 } 788 789 static uint8_t 790 musbotg_data_tx(struct musbotg_td *td) 791 { 792 struct usb_page_search buf_res; 793 struct musbotg_softc *sc; 794 uint16_t count; 795 uint8_t csr; 796 uint8_t to; 797 798 to = 8; /* don't loop forever! */ 799 800 /* get pointer to softc */ 801 sc = MUSBOTG_PC2SC(td->pc); 802 803 /* select endpoint */ 804 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->ep_no); 805 806 repeat: 807 808 /* read out FIFO status */ 809 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 810 811 DPRINTFN(4, "csr=0x%02x\n", csr); 812 813 if (csr & (MUSB2_MASK_CSRL_TXINCOMP | 814 MUSB2_MASK_CSRL_TXUNDERRUN)) { 815 /* clear status bits */ 816 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 817 } 818 if (csr & MUSB2_MASK_CSRL_TXPKTRDY) { 819 return (1); /* not complete */ 820 } 821 /* check for short packet */ 822 count = td->max_frame_size; 823 if (td->remainder < count) { 824 /* we have a short packet */ 825 td->short_pkt = 1; 826 count = td->remainder; 827 } 828 while (count > 0) { 829 uint32_t temp; 830 831 usbd_get_page(td->pc, td->offset, &buf_res); 832 833 /* get correct length */ 834 if (buf_res.length > count) { 835 buf_res.length = count; 836 } 837 /* check for unaligned memory address */ 838 if (USB_P2U(buf_res.buffer) & 3) { 839 840 usbd_copy_out(td->pc, td->offset, 841 sc->sc_bounce_buf, count); 842 843 temp = count & ~3; 844 845 if (temp) { 846 /* transmit data 4 bytes at a time */ 847 bus_space_write_multi_4(sc->sc_io_tag, 848 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->ep_no), 849 sc->sc_bounce_buf, temp / 4); 850 } 851 temp = count & 3; 852 if (temp) { 853 /* receive data 1 byte at a time */ 854 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 855 MUSB2_REG_EPFIFO(td->ep_no), 856 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 857 } 858 /* update offset and remainder */ 859 td->offset += count; 860 td->remainder -= count; 861 break; 862 } 863 /* check if we can optimise */ 864 if (buf_res.length >= 4) { 865 866 /* transmit data 4 bytes at a time */ 867 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 868 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 869 buf_res.length / 4); 870 871 temp = buf_res.length & ~3; 872 873 /* update counters */ 874 count -= temp; 875 td->offset += temp; 876 td->remainder -= temp; 877 continue; 878 } 879 /* transmit data */ 880 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 881 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 882 buf_res.length); 883 884 /* update counters */ 885 count -= buf_res.length; 886 td->offset += buf_res.length; 887 td->remainder -= buf_res.length; 888 } 889 890 /* write command */ 891 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 892 MUSB2_MASK_CSRL_TXPKTRDY); 893 894 /* check remainder */ 895 if (td->remainder == 0) { 896 if (td->short_pkt) { 897 return (0); /* complete */ 898 } 899 /* else we need to transmit a short packet */ 900 } 901 if (--to) { 902 goto repeat; 903 } 904 return (1); /* not complete */ 905 } 906 907 static uint8_t 908 musbotg_xfer_do_fifo(struct usb_xfer *xfer) 909 { 910 struct musbotg_softc *sc; 911 struct musbotg_td *td; 912 913 DPRINTFN(8, "\n"); 914 915 td = xfer->td_transfer_cache; 916 while (1) { 917 if ((td->func) (td)) { 918 /* operation in progress */ 919 break; 920 } 921 if (((void *)td) == xfer->td_transfer_last) { 922 goto done; 923 } 924 if (td->error) { 925 goto done; 926 } else if (td->remainder > 0) { 927 /* 928 * We had a short transfer. If there is no alternate 929 * next, stop processing ! 930 */ 931 if (!td->alt_next) { 932 goto done; 933 } 934 } 935 /* 936 * Fetch the next transfer descriptor and transfer 937 * some flags to the next transfer descriptor 938 */ 939 td = td->obj_next; 940 xfer->td_transfer_cache = td; 941 } 942 return (1); /* not complete */ 943 944 done: 945 sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 946 947 /* compute all actual lengths */ 948 949 musbotg_standard_done(xfer); 950 951 return (0); /* complete */ 952 } 953 954 static void 955 musbotg_interrupt_poll(struct musbotg_softc *sc) 956 { 957 struct usb_xfer *xfer; 958 959 repeat: 960 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 961 if (!musbotg_xfer_do_fifo(xfer)) { 962 /* queue has been modified */ 963 goto repeat; 964 } 965 } 966 } 967 968 void 969 musbotg_vbus_interrupt(struct musbotg_softc *sc, uint8_t is_on) 970 { 971 DPRINTFN(4, "vbus = %u\n", is_on); 972 973 USB_BUS_LOCK(&sc->sc_bus); 974 if (is_on) { 975 if (!sc->sc_flags.status_vbus) { 976 sc->sc_flags.status_vbus = 1; 977 978 /* complete root HUB interrupt endpoint */ 979 musbotg_root_intr(sc); 980 } 981 } else { 982 if (sc->sc_flags.status_vbus) { 983 sc->sc_flags.status_vbus = 0; 984 sc->sc_flags.status_bus_reset = 0; 985 sc->sc_flags.status_suspend = 0; 986 sc->sc_flags.change_suspend = 0; 987 sc->sc_flags.change_connect = 1; 988 989 /* complete root HUB interrupt endpoint */ 990 musbotg_root_intr(sc); 991 } 992 } 993 994 USB_BUS_UNLOCK(&sc->sc_bus); 995 } 996 997 void 998 musbotg_interrupt(struct musbotg_softc *sc) 999 { 1000 uint16_t rx_status; 1001 uint16_t tx_status; 1002 uint8_t usb_status; 1003 uint8_t temp; 1004 uint8_t to = 2; 1005 1006 USB_BUS_LOCK(&sc->sc_bus); 1007 1008 repeat: 1009 1010 /* read all interrupt registers */ 1011 usb_status = MUSB2_READ_1(sc, MUSB2_REG_INTUSB); 1012 1013 /* read all FIFO interrupts */ 1014 rx_status = MUSB2_READ_2(sc, MUSB2_REG_INTRX); 1015 tx_status = MUSB2_READ_2(sc, MUSB2_REG_INTTX); 1016 1017 /* check for any bus state change interrupts */ 1018 1019 if (usb_status & (MUSB2_MASK_IRESET | 1020 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP)) { 1021 1022 DPRINTFN(4, "real bus interrupt 0x%08x\n", usb_status); 1023 1024 if (usb_status & MUSB2_MASK_IRESET) { 1025 1026 /* set correct state */ 1027 sc->sc_flags.status_bus_reset = 1; 1028 sc->sc_flags.status_suspend = 0; 1029 sc->sc_flags.change_suspend = 0; 1030 sc->sc_flags.change_connect = 1; 1031 1032 /* determine line speed */ 1033 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 1034 if (temp & MUSB2_MASK_HSMODE) 1035 sc->sc_flags.status_high_speed = 1; 1036 else 1037 sc->sc_flags.status_high_speed = 0; 1038 1039 /* 1040 * After reset all interrupts are on and we need to 1041 * turn them off! 1042 */ 1043 temp = MUSB2_MASK_IRESET; 1044 /* disable resume interrupt */ 1045 temp &= ~MUSB2_MASK_IRESUME; 1046 /* enable suspend interrupt */ 1047 temp |= MUSB2_MASK_ISUSP; 1048 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1049 /* disable TX and RX interrupts */ 1050 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1051 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1052 } 1053 /* 1054 * If RXRSM and RXSUSP is set at the same time we interpret 1055 * that like RESUME. Resume is set when there is at least 3 1056 * milliseconds of inactivity on the USB BUS. 1057 */ 1058 if (usb_status & MUSB2_MASK_IRESUME) { 1059 if (sc->sc_flags.status_suspend) { 1060 sc->sc_flags.status_suspend = 0; 1061 sc->sc_flags.change_suspend = 1; 1062 1063 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE); 1064 /* disable resume interrupt */ 1065 temp &= ~MUSB2_MASK_IRESUME; 1066 /* enable suspend interrupt */ 1067 temp |= MUSB2_MASK_ISUSP; 1068 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1069 } 1070 } else if (usb_status & MUSB2_MASK_ISUSP) { 1071 if (!sc->sc_flags.status_suspend) { 1072 sc->sc_flags.status_suspend = 1; 1073 sc->sc_flags.change_suspend = 1; 1074 1075 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE); 1076 /* disable suspend interrupt */ 1077 temp &= ~MUSB2_MASK_ISUSP; 1078 /* enable resume interrupt */ 1079 temp |= MUSB2_MASK_IRESUME; 1080 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1081 } 1082 } 1083 /* complete root HUB interrupt endpoint */ 1084 musbotg_root_intr(sc); 1085 } 1086 /* check for any endpoint interrupts */ 1087 1088 if (rx_status || tx_status) { 1089 DPRINTFN(4, "real endpoint interrupt " 1090 "rx=0x%04x, tx=0x%04x\n", rx_status, tx_status); 1091 } 1092 /* poll one time regardless of FIFO status */ 1093 1094 musbotg_interrupt_poll(sc); 1095 1096 if (--to) 1097 goto repeat; 1098 1099 USB_BUS_UNLOCK(&sc->sc_bus); 1100 } 1101 1102 static void 1103 musbotg_setup_standard_chain_sub(struct musbotg_std_temp *temp) 1104 { 1105 struct musbotg_td *td; 1106 1107 /* get current Transfer Descriptor */ 1108 td = temp->td_next; 1109 temp->td = td; 1110 1111 /* prepare for next TD */ 1112 temp->td_next = td->obj_next; 1113 1114 /* fill out the Transfer Descriptor */ 1115 td->func = temp->func; 1116 td->pc = temp->pc; 1117 td->offset = temp->offset; 1118 td->remainder = temp->len; 1119 td->error = 0; 1120 td->did_stall = temp->did_stall; 1121 td->short_pkt = temp->short_pkt; 1122 td->alt_next = temp->setup_alt_next; 1123 } 1124 1125 static void 1126 musbotg_setup_standard_chain(struct usb_xfer *xfer) 1127 { 1128 struct musbotg_std_temp temp; 1129 struct musbotg_softc *sc; 1130 struct musbotg_td *td; 1131 uint32_t x; 1132 uint8_t ep_no; 1133 1134 DPRINTFN(8, "addr=%d endpt=%d sumlen=%d speed=%d\n", 1135 xfer->address, UE_GET_ADDR(xfer->endpointno), 1136 xfer->sumlen, usbd_get_speed(xfer->xroot->udev)); 1137 1138 temp.max_frame_size = xfer->max_frame_size; 1139 1140 td = xfer->td_start[0]; 1141 xfer->td_transfer_first = td; 1142 xfer->td_transfer_cache = td; 1143 1144 /* setup temp */ 1145 1146 temp.pc = NULL; 1147 temp.td = NULL; 1148 temp.td_next = xfer->td_start[0]; 1149 temp.offset = 0; 1150 temp.setup_alt_next = xfer->flags_int.short_frames_ok; 1151 temp.did_stall = !xfer->flags_int.control_stall; 1152 1153 sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 1154 ep_no = (xfer->endpointno & UE_ADDR); 1155 1156 /* check if we should prepend a setup message */ 1157 1158 if (xfer->flags_int.control_xfr) { 1159 if (xfer->flags_int.control_hdr) { 1160 1161 temp.func = &musbotg_setup_rx; 1162 temp.len = xfer->frlengths[0]; 1163 temp.pc = xfer->frbuffers + 0; 1164 temp.short_pkt = temp.len ? 1 : 0; 1165 1166 musbotg_setup_standard_chain_sub(&temp); 1167 } 1168 x = 1; 1169 } else { 1170 x = 0; 1171 } 1172 1173 if (x != xfer->nframes) { 1174 if (xfer->endpointno & UE_DIR_IN) { 1175 if (xfer->flags_int.control_xfr) 1176 temp.func = &musbotg_setup_data_tx; 1177 else 1178 temp.func = &musbotg_data_tx; 1179 } else { 1180 if (xfer->flags_int.control_xfr) 1181 temp.func = &musbotg_setup_data_rx; 1182 else 1183 temp.func = &musbotg_data_rx; 1184 } 1185 1186 /* setup "pc" pointer */ 1187 temp.pc = xfer->frbuffers + x; 1188 } 1189 while (x != xfer->nframes) { 1190 1191 /* DATA0 / DATA1 message */ 1192 1193 temp.len = xfer->frlengths[x]; 1194 1195 x++; 1196 1197 if (x == xfer->nframes) { 1198 if (xfer->flags_int.control_xfr) { 1199 if (xfer->flags_int.control_act) { 1200 temp.setup_alt_next = 0; 1201 } 1202 } else { 1203 temp.setup_alt_next = 0; 1204 } 1205 } 1206 if (temp.len == 0) { 1207 1208 /* make sure that we send an USB packet */ 1209 1210 temp.short_pkt = 0; 1211 1212 } else { 1213 1214 /* regular data transfer */ 1215 1216 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1; 1217 } 1218 1219 musbotg_setup_standard_chain_sub(&temp); 1220 1221 if (xfer->flags_int.isochronous_xfr) { 1222 temp.offset += temp.len; 1223 } else { 1224 /* get next Page Cache pointer */ 1225 temp.pc = xfer->frbuffers + x; 1226 } 1227 } 1228 1229 /* check for control transfer */ 1230 if (xfer->flags_int.control_xfr) { 1231 1232 /* always setup a valid "pc" pointer for status and sync */ 1233 temp.pc = xfer->frbuffers + 0; 1234 temp.len = 0; 1235 temp.short_pkt = 0; 1236 temp.setup_alt_next = 0; 1237 1238 /* check if we should append a status stage */ 1239 if (!xfer->flags_int.control_act) { 1240 /* 1241 * Send a DATA1 message and invert the current 1242 * endpoint direction. 1243 */ 1244 temp.func = &musbotg_setup_status; 1245 musbotg_setup_standard_chain_sub(&temp); 1246 } 1247 } 1248 /* must have at least one frame! */ 1249 td = temp.td; 1250 xfer->td_transfer_last = td; 1251 } 1252 1253 static void 1254 musbotg_timeout(void *arg) 1255 { 1256 struct usb_xfer *xfer = arg; 1257 1258 DPRINTFN(1, "xfer=%p\n", xfer); 1259 1260 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1261 1262 /* transfer is transferred */ 1263 musbotg_device_done(xfer, USB_ERR_TIMEOUT); 1264 } 1265 1266 static void 1267 musbotg_ep_int_set(struct usb_xfer *xfer, uint8_t on) 1268 { 1269 struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 1270 uint16_t temp; 1271 uint8_t ep_no = xfer->endpointno & UE_ADDR; 1272 1273 /* 1274 * Only enable the endpoint interrupt when we are 1275 * actually waiting for data, hence we are dealing 1276 * with level triggered interrupts ! 1277 */ 1278 if (ep_no == 0) { 1279 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE); 1280 if (on) 1281 temp |= MUSB2_MASK_EPINT(0); 1282 else 1283 temp &= ~MUSB2_MASK_EPINT(0); 1284 1285 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp); 1286 } else { 1287 if (USB_GET_DATA_ISREAD(xfer)) { 1288 temp = MUSB2_READ_2(sc, MUSB2_REG_INTRXE); 1289 if (on) 1290 temp |= MUSB2_MASK_EPINT(ep_no); 1291 else 1292 temp &= ~MUSB2_MASK_EPINT(ep_no); 1293 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, temp); 1294 1295 } else { 1296 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE); 1297 if (on) 1298 temp |= MUSB2_MASK_EPINT(ep_no); 1299 else 1300 temp &= ~MUSB2_MASK_EPINT(ep_no); 1301 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp); 1302 } 1303 } 1304 } 1305 1306 static void 1307 musbotg_start_standard_chain(struct usb_xfer *xfer) 1308 { 1309 DPRINTFN(8, "\n"); 1310 1311 /* poll one time */ 1312 if (musbotg_xfer_do_fifo(xfer)) { 1313 1314 musbotg_ep_int_set(xfer, 1); 1315 1316 DPRINTFN(14, "enabled interrupts on endpoint\n"); 1317 1318 /* put transfer on interrupt queue */ 1319 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 1320 1321 /* start timeout, if any */ 1322 if (xfer->timeout != 0) { 1323 usbd_transfer_timeout_ms(xfer, 1324 &musbotg_timeout, xfer->timeout); 1325 } 1326 } 1327 } 1328 1329 static void 1330 musbotg_root_intr(struct musbotg_softc *sc) 1331 { 1332 DPRINTFN(8, "\n"); 1333 1334 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1335 1336 /* set port bit */ 1337 sc->sc_hub_idata[0] = 0x02; /* we only have one port */ 1338 1339 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata, 1340 sizeof(sc->sc_hub_idata)); 1341 } 1342 1343 static usb_error_t 1344 musbotg_standard_done_sub(struct usb_xfer *xfer) 1345 { 1346 struct musbotg_td *td; 1347 uint32_t len; 1348 uint8_t error; 1349 1350 DPRINTFN(8, "\n"); 1351 1352 td = xfer->td_transfer_cache; 1353 1354 do { 1355 len = td->remainder; 1356 1357 if (xfer->aframes != xfer->nframes) { 1358 /* 1359 * Verify the length and subtract 1360 * the remainder from "frlengths[]": 1361 */ 1362 if (len > xfer->frlengths[xfer->aframes]) { 1363 td->error = 1; 1364 } else { 1365 xfer->frlengths[xfer->aframes] -= len; 1366 } 1367 } 1368 /* Check for transfer error */ 1369 if (td->error) { 1370 /* the transfer is finished */ 1371 error = 1; 1372 td = NULL; 1373 break; 1374 } 1375 /* Check for short transfer */ 1376 if (len > 0) { 1377 if (xfer->flags_int.short_frames_ok) { 1378 /* follow alt next */ 1379 if (td->alt_next) { 1380 td = td->obj_next; 1381 } else { 1382 td = NULL; 1383 } 1384 } else { 1385 /* the transfer is finished */ 1386 td = NULL; 1387 } 1388 error = 0; 1389 break; 1390 } 1391 td = td->obj_next; 1392 1393 /* this USB frame is complete */ 1394 error = 0; 1395 break; 1396 1397 } while (0); 1398 1399 /* update transfer cache */ 1400 1401 xfer->td_transfer_cache = td; 1402 1403 return (error ? 1404 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION); 1405 } 1406 1407 static void 1408 musbotg_standard_done(struct usb_xfer *xfer) 1409 { 1410 usb_error_t err = 0; 1411 1412 DPRINTFN(12, "xfer=%p endpoint=%p transfer done\n", 1413 xfer, xfer->endpoint); 1414 1415 /* reset scanner */ 1416 1417 xfer->td_transfer_cache = xfer->td_transfer_first; 1418 1419 if (xfer->flags_int.control_xfr) { 1420 1421 if (xfer->flags_int.control_hdr) { 1422 1423 err = musbotg_standard_done_sub(xfer); 1424 } 1425 xfer->aframes = 1; 1426 1427 if (xfer->td_transfer_cache == NULL) { 1428 goto done; 1429 } 1430 } 1431 while (xfer->aframes != xfer->nframes) { 1432 1433 err = musbotg_standard_done_sub(xfer); 1434 xfer->aframes++; 1435 1436 if (xfer->td_transfer_cache == NULL) { 1437 goto done; 1438 } 1439 } 1440 1441 if (xfer->flags_int.control_xfr && 1442 !xfer->flags_int.control_act) { 1443 1444 err = musbotg_standard_done_sub(xfer); 1445 } 1446 done: 1447 musbotg_device_done(xfer, err); 1448 } 1449 1450 /*------------------------------------------------------------------------* 1451 * musbotg_device_done 1452 * 1453 * NOTE: this function can be called more than one time on the 1454 * same USB transfer! 1455 *------------------------------------------------------------------------*/ 1456 static void 1457 musbotg_device_done(struct usb_xfer *xfer, usb_error_t error) 1458 { 1459 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1460 1461 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n", 1462 xfer, xfer->endpoint, error); 1463 1464 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1465 1466 musbotg_ep_int_set(xfer, 0); 1467 1468 DPRINTFN(14, "disabled interrupts on endpoint\n"); 1469 } 1470 /* dequeue transfer and start next transfer */ 1471 usbd_transfer_done(xfer, error); 1472 } 1473 1474 static void 1475 musbotg_xfer_stall(struct usb_xfer *xfer) 1476 { 1477 musbotg_device_done(xfer, USB_ERR_STALLED); 1478 } 1479 1480 static void 1481 musbotg_set_stall(struct usb_device *udev, 1482 struct usb_endpoint *ep, uint8_t *did_stall) 1483 { 1484 struct musbotg_softc *sc; 1485 uint8_t ep_no; 1486 1487 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1488 1489 DPRINTFN(4, "endpoint=%p\n", ep); 1490 1491 /* set FORCESTALL */ 1492 sc = MUSBOTG_BUS2SC(udev->bus); 1493 1494 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR); 1495 1496 /* select endpoint */ 1497 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no); 1498 1499 if (ep->edesc->bEndpointAddress & UE_DIR_IN) { 1500 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1501 MUSB2_MASK_CSRL_TXSENDSTALL); 1502 } else { 1503 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1504 MUSB2_MASK_CSRL_RXSENDSTALL); 1505 } 1506 } 1507 1508 static void 1509 musbotg_clear_stall_sub(struct musbotg_softc *sc, uint16_t wMaxPacket, 1510 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir) 1511 { 1512 uint16_t mps; 1513 uint16_t temp; 1514 uint8_t csr; 1515 1516 if (ep_type == UE_CONTROL) { 1517 /* clearing stall is not needed */ 1518 return; 1519 } 1520 /* select endpoint */ 1521 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no); 1522 1523 /* compute max frame size */ 1524 mps = wMaxPacket & 0x7FF; 1525 switch ((wMaxPacket >> 11) & 3) { 1526 case 1: 1527 mps *= 2; 1528 break; 1529 case 2: 1530 mps *= 3; 1531 break; 1532 default: 1533 break; 1534 } 1535 1536 if (ep_dir == UE_DIR_IN) { 1537 1538 temp = 0; 1539 1540 /* Configure endpoint */ 1541 switch (ep_type) { 1542 case UE_INTERRUPT: 1543 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1544 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1545 MUSB2_MASK_CSRH_TXMODE | temp); 1546 break; 1547 case UE_ISOCHRONOUS: 1548 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1549 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1550 MUSB2_MASK_CSRH_TXMODE | 1551 MUSB2_MASK_CSRH_TXISO | temp); 1552 break; 1553 case UE_BULK: 1554 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1555 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1556 MUSB2_MASK_CSRH_TXMODE | temp); 1557 break; 1558 default: 1559 break; 1560 } 1561 1562 /* Need to flush twice in case of double bufring */ 1563 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1564 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 1565 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1566 MUSB2_MASK_CSRL_TXFFLUSH); 1567 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1568 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 1569 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1570 MUSB2_MASK_CSRL_TXFFLUSH); 1571 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1572 } 1573 } 1574 /* reset data toggle */ 1575 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1576 MUSB2_MASK_CSRL_TXDT_CLR); 1577 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 1578 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1579 1580 /* set double/single buffering */ 1581 temp = MUSB2_READ_2(sc, MUSB2_REG_TXDBDIS); 1582 if (mps <= (sc->sc_hw_ep_profile[ep_no]. 1583 max_in_frame_size / 2)) { 1584 /* double buffer */ 1585 temp &= ~(1 << ep_no); 1586 } else { 1587 /* single buffer */ 1588 temp |= (1 << ep_no); 1589 } 1590 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, temp); 1591 1592 /* clear sent stall */ 1593 if (csr & MUSB2_MASK_CSRL_TXSENTSTALL) { 1594 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 1595 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1596 } 1597 } else { 1598 1599 temp = 0; 1600 1601 /* Configure endpoint */ 1602 switch (ep_type) { 1603 case UE_INTERRUPT: 1604 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1605 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, 1606 MUSB2_MASK_CSRH_RXNYET | temp); 1607 break; 1608 case UE_ISOCHRONOUS: 1609 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1610 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, 1611 MUSB2_MASK_CSRH_RXNYET | 1612 MUSB2_MASK_CSRH_RXISO | temp); 1613 break; 1614 case UE_BULK: 1615 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1616 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, temp); 1617 break; 1618 default: 1619 break; 1620 } 1621 1622 /* Need to flush twice in case of double bufring */ 1623 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1624 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) { 1625 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1626 MUSB2_MASK_CSRL_RXFFLUSH); 1627 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1628 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) { 1629 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1630 MUSB2_MASK_CSRL_RXFFLUSH); 1631 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1632 } 1633 } 1634 /* reset data toggle */ 1635 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1636 MUSB2_MASK_CSRL_RXDT_CLR); 1637 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 1638 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1639 1640 /* set double/single buffering */ 1641 temp = MUSB2_READ_2(sc, MUSB2_REG_RXDBDIS); 1642 if (mps <= (sc->sc_hw_ep_profile[ep_no]. 1643 max_out_frame_size / 2)) { 1644 /* double buffer */ 1645 temp &= ~(1 << ep_no); 1646 } else { 1647 /* single buffer */ 1648 temp |= (1 << ep_no); 1649 } 1650 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, temp); 1651 1652 /* clear sent stall */ 1653 if (csr & MUSB2_MASK_CSRL_RXSENTSTALL) { 1654 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 1655 } 1656 } 1657 } 1658 1659 static void 1660 musbotg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep) 1661 { 1662 struct musbotg_softc *sc; 1663 struct usb_endpoint_descriptor *ed; 1664 1665 DPRINTFN(4, "endpoint=%p\n", ep); 1666 1667 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1668 1669 /* check mode */ 1670 if (udev->flags.usb_mode != USB_MODE_DEVICE) { 1671 /* not supported */ 1672 return; 1673 } 1674 /* get softc */ 1675 sc = MUSBOTG_BUS2SC(udev->bus); 1676 1677 /* get endpoint descriptor */ 1678 ed = ep->edesc; 1679 1680 /* reset endpoint */ 1681 musbotg_clear_stall_sub(sc, 1682 UGETW(ed->wMaxPacketSize), 1683 (ed->bEndpointAddress & UE_ADDR), 1684 (ed->bmAttributes & UE_XFERTYPE), 1685 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT))); 1686 } 1687 1688 usb_error_t 1689 musbotg_init(struct musbotg_softc *sc) 1690 { 1691 struct usb_hw_ep_profile *pf; 1692 uint16_t offset; 1693 uint8_t nrx; 1694 uint8_t ntx; 1695 uint8_t temp; 1696 uint8_t fsize; 1697 uint8_t frx; 1698 uint8_t ftx; 1699 uint8_t dynfifo; 1700 1701 DPRINTFN(1, "start\n"); 1702 1703 /* set up the bus structure */ 1704 sc->sc_bus.usbrev = USB_REV_2_0; 1705 sc->sc_bus.methods = &musbotg_bus_methods; 1706 1707 USB_BUS_LOCK(&sc->sc_bus); 1708 1709 /* turn on clocks */ 1710 1711 if (sc->sc_clocks_on) { 1712 (sc->sc_clocks_on) (sc->sc_clocks_arg); 1713 } 1714 /* wait a little for things to stabilise */ 1715 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000); 1716 1717 /* disable all interrupts */ 1718 1719 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0); 1720 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1721 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1722 1723 /* disable pullup */ 1724 1725 musbotg_pull_common(sc, 0); 1726 1727 /* wait a little bit (10ms) */ 1728 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100); 1729 1730 /* disable double packet buffering */ 1731 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, 0xFFFF); 1732 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, 0xFFFF); 1733 1734 /* enable HighSpeed and ISO Update flags */ 1735 1736 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, 1737 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD); 1738 1739 /* clear Session bit, if set */ 1740 1741 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL); 1742 temp &= ~MUSB2_MASK_SESS; 1743 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp); 1744 1745 DPRINTF("DEVCTL=0x%02x\n", temp); 1746 1747 /* disable testmode */ 1748 1749 MUSB2_WRITE_1(sc, MUSB2_REG_TESTMODE, 0); 1750 1751 /* set default value */ 1752 1753 MUSB2_WRITE_1(sc, MUSB2_REG_MISC, 0); 1754 1755 /* select endpoint index 0 */ 1756 1757 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 1758 1759 /* read out number of endpoints */ 1760 1761 nrx = 1762 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) / 16); 1763 1764 ntx = 1765 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) % 16); 1766 1767 /* these numbers exclude the control endpoint */ 1768 1769 DPRINTFN(2, "RX/TX endpoints: %u/%u\n", nrx, ntx); 1770 1771 sc->sc_ep_max = (nrx > ntx) ? nrx : ntx; 1772 if (sc->sc_ep_max == 0) { 1773 DPRINTFN(2, "ERROR: Looks like the clocks are off!\n"); 1774 } 1775 /* read out configuration data */ 1776 1777 sc->sc_conf_data = MUSB2_READ_1(sc, MUSB2_REG_CONFDATA); 1778 1779 DPRINTFN(2, "Config Data: 0x%02x\n", 1780 sc->sc_conf_data); 1781 1782 dynfifo = (sc->sc_conf_data & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0; 1783 1784 if (dynfifo) { 1785 device_printf(sc->sc_bus.bdev, "Dynamic FIFO sizing detected, " 1786 "assuming 16Kbytes of FIFO RAM\n"); 1787 } 1788 1789 DPRINTFN(2, "HW version: 0x%04x\n", 1790 MUSB2_READ_1(sc, MUSB2_REG_HWVERS)); 1791 1792 /* initialise endpoint profiles */ 1793 1794 offset = 0; 1795 1796 for (temp = 1; temp <= sc->sc_ep_max; temp++) { 1797 pf = sc->sc_hw_ep_profile + temp; 1798 1799 /* select endpoint */ 1800 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, temp); 1801 1802 fsize = MUSB2_READ_1(sc, MUSB2_REG_FSIZE); 1803 frx = (fsize & MUSB2_MASK_RX_FSIZE) / 16; 1804 ftx = (fsize & MUSB2_MASK_TX_FSIZE); 1805 1806 DPRINTF("Endpoint %u FIFO size: IN=%u, OUT=%u, DYN=%d\n", 1807 temp, ftx, frx, dynfifo); 1808 1809 if (dynfifo) { 1810 if (frx && (temp <= nrx)) { 1811 if (temp < 8) { 1812 frx = 10; /* 1K */ 1813 MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 1814 MUSB2_VAL_FIFOSZ_512 | 1815 MUSB2_MASK_FIFODB); 1816 } else { 1817 frx = 7; /* 128 bytes */ 1818 MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 1819 MUSB2_VAL_FIFOSZ_128); 1820 } 1821 1822 MUSB2_WRITE_2(sc, MUSB2_REG_RXFIFOADD, 1823 offset >> 3); 1824 1825 offset += (1 << frx); 1826 } 1827 if (ftx && (temp <= ntx)) { 1828 if (temp < 8) { 1829 ftx = 10; /* 1K */ 1830 MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ, 1831 MUSB2_VAL_FIFOSZ_512 | 1832 MUSB2_MASK_FIFODB); 1833 } else { 1834 ftx = 7; /* 128 bytes */ 1835 MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ, 1836 MUSB2_VAL_FIFOSZ_128); 1837 } 1838 1839 MUSB2_WRITE_2(sc, MUSB2_REG_TXFIFOADD, 1840 offset >> 3); 1841 1842 offset += (1 << ftx); 1843 } 1844 } 1845 1846 if (frx && ftx && (temp <= nrx) && (temp <= ntx)) { 1847 pf->max_in_frame_size = 1 << ftx; 1848 pf->max_out_frame_size = 1 << frx; 1849 pf->is_simplex = 0; /* duplex */ 1850 pf->support_multi_buffer = 1; 1851 pf->support_bulk = 1; 1852 pf->support_interrupt = 1; 1853 pf->support_isochronous = 1; 1854 pf->support_in = 1; 1855 pf->support_out = 1; 1856 } else if (frx && (temp <= nrx)) { 1857 pf->max_out_frame_size = 1 << frx; 1858 pf->is_simplex = 1; /* simplex */ 1859 pf->support_multi_buffer = 1; 1860 pf->support_bulk = 1; 1861 pf->support_interrupt = 1; 1862 pf->support_isochronous = 1; 1863 pf->support_out = 1; 1864 } else if (ftx && (temp <= ntx)) { 1865 pf->max_in_frame_size = 1 << ftx; 1866 pf->is_simplex = 1; /* simplex */ 1867 pf->support_multi_buffer = 1; 1868 pf->support_bulk = 1; 1869 pf->support_interrupt = 1; 1870 pf->support_isochronous = 1; 1871 pf->support_in = 1; 1872 } 1873 } 1874 1875 DPRINTFN(2, "Dynamic FIFO size = %d bytes\n", offset); 1876 1877 /* turn on default interrupts */ 1878 1879 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 1880 MUSB2_MASK_IRESET); 1881 1882 musbotg_clocks_off(sc); 1883 1884 USB_BUS_UNLOCK(&sc->sc_bus); 1885 1886 /* catch any lost interrupts */ 1887 1888 musbotg_do_poll(&sc->sc_bus); 1889 1890 return (0); /* success */ 1891 } 1892 1893 void 1894 musbotg_uninit(struct musbotg_softc *sc) 1895 { 1896 USB_BUS_LOCK(&sc->sc_bus); 1897 1898 /* disable all interrupts */ 1899 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0); 1900 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1901 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1902 1903 sc->sc_flags.port_powered = 0; 1904 sc->sc_flags.status_vbus = 0; 1905 sc->sc_flags.status_bus_reset = 0; 1906 sc->sc_flags.status_suspend = 0; 1907 sc->sc_flags.change_suspend = 0; 1908 sc->sc_flags.change_connect = 1; 1909 1910 musbotg_pull_down(sc); 1911 musbotg_clocks_off(sc); 1912 USB_BUS_UNLOCK(&sc->sc_bus); 1913 } 1914 1915 static void 1916 musbotg_suspend(struct musbotg_softc *sc) 1917 { 1918 /* TODO */ 1919 } 1920 1921 static void 1922 musbotg_resume(struct musbotg_softc *sc) 1923 { 1924 /* TODO */ 1925 } 1926 1927 static void 1928 musbotg_do_poll(struct usb_bus *bus) 1929 { 1930 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus); 1931 1932 USB_BUS_LOCK(&sc->sc_bus); 1933 musbotg_interrupt_poll(sc); 1934 USB_BUS_UNLOCK(&sc->sc_bus); 1935 } 1936 1937 /*------------------------------------------------------------------------* 1938 * musbotg bulk support 1939 *------------------------------------------------------------------------*/ 1940 static void 1941 musbotg_device_bulk_open(struct usb_xfer *xfer) 1942 { 1943 return; 1944 } 1945 1946 static void 1947 musbotg_device_bulk_close(struct usb_xfer *xfer) 1948 { 1949 musbotg_device_done(xfer, USB_ERR_CANCELLED); 1950 } 1951 1952 static void 1953 musbotg_device_bulk_enter(struct usb_xfer *xfer) 1954 { 1955 return; 1956 } 1957 1958 static void 1959 musbotg_device_bulk_start(struct usb_xfer *xfer) 1960 { 1961 /* setup TDs */ 1962 musbotg_setup_standard_chain(xfer); 1963 musbotg_start_standard_chain(xfer); 1964 } 1965 1966 struct usb_pipe_methods musbotg_device_bulk_methods = 1967 { 1968 .open = musbotg_device_bulk_open, 1969 .close = musbotg_device_bulk_close, 1970 .enter = musbotg_device_bulk_enter, 1971 .start = musbotg_device_bulk_start, 1972 }; 1973 1974 /*------------------------------------------------------------------------* 1975 * musbotg control support 1976 *------------------------------------------------------------------------*/ 1977 static void 1978 musbotg_device_ctrl_open(struct usb_xfer *xfer) 1979 { 1980 return; 1981 } 1982 1983 static void 1984 musbotg_device_ctrl_close(struct usb_xfer *xfer) 1985 { 1986 musbotg_device_done(xfer, USB_ERR_CANCELLED); 1987 } 1988 1989 static void 1990 musbotg_device_ctrl_enter(struct usb_xfer *xfer) 1991 { 1992 return; 1993 } 1994 1995 static void 1996 musbotg_device_ctrl_start(struct usb_xfer *xfer) 1997 { 1998 /* setup TDs */ 1999 musbotg_setup_standard_chain(xfer); 2000 musbotg_start_standard_chain(xfer); 2001 } 2002 2003 struct usb_pipe_methods musbotg_device_ctrl_methods = 2004 { 2005 .open = musbotg_device_ctrl_open, 2006 .close = musbotg_device_ctrl_close, 2007 .enter = musbotg_device_ctrl_enter, 2008 .start = musbotg_device_ctrl_start, 2009 }; 2010 2011 /*------------------------------------------------------------------------* 2012 * musbotg interrupt support 2013 *------------------------------------------------------------------------*/ 2014 static void 2015 musbotg_device_intr_open(struct usb_xfer *xfer) 2016 { 2017 return; 2018 } 2019 2020 static void 2021 musbotg_device_intr_close(struct usb_xfer *xfer) 2022 { 2023 musbotg_device_done(xfer, USB_ERR_CANCELLED); 2024 } 2025 2026 static void 2027 musbotg_device_intr_enter(struct usb_xfer *xfer) 2028 { 2029 return; 2030 } 2031 2032 static void 2033 musbotg_device_intr_start(struct usb_xfer *xfer) 2034 { 2035 /* setup TDs */ 2036 musbotg_setup_standard_chain(xfer); 2037 musbotg_start_standard_chain(xfer); 2038 } 2039 2040 struct usb_pipe_methods musbotg_device_intr_methods = 2041 { 2042 .open = musbotg_device_intr_open, 2043 .close = musbotg_device_intr_close, 2044 .enter = musbotg_device_intr_enter, 2045 .start = musbotg_device_intr_start, 2046 }; 2047 2048 /*------------------------------------------------------------------------* 2049 * musbotg full speed isochronous support 2050 *------------------------------------------------------------------------*/ 2051 static void 2052 musbotg_device_isoc_open(struct usb_xfer *xfer) 2053 { 2054 return; 2055 } 2056 2057 static void 2058 musbotg_device_isoc_close(struct usb_xfer *xfer) 2059 { 2060 musbotg_device_done(xfer, USB_ERR_CANCELLED); 2061 } 2062 2063 static void 2064 musbotg_device_isoc_enter(struct usb_xfer *xfer) 2065 { 2066 struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 2067 uint32_t temp; 2068 uint32_t nframes; 2069 uint32_t fs_frames; 2070 2071 DPRINTFN(5, "xfer=%p next=%d nframes=%d\n", 2072 xfer, xfer->endpoint->isoc_next, xfer->nframes); 2073 2074 /* get the current frame index */ 2075 2076 nframes = MUSB2_READ_2(sc, MUSB2_REG_FRAME); 2077 2078 /* 2079 * check if the frame index is within the window where the frames 2080 * will be inserted 2081 */ 2082 temp = (nframes - xfer->endpoint->isoc_next) & MUSB2_MASK_FRAME; 2083 2084 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) { 2085 fs_frames = (xfer->nframes + 7) / 8; 2086 } else { 2087 fs_frames = xfer->nframes; 2088 } 2089 2090 if ((xfer->endpoint->is_synced == 0) || 2091 (temp < fs_frames)) { 2092 /* 2093 * If there is data underflow or the pipe queue is 2094 * empty we schedule the transfer a few frames ahead 2095 * of the current frame position. Else two isochronous 2096 * transfers might overlap. 2097 */ 2098 xfer->endpoint->isoc_next = (nframes + 3) & MUSB2_MASK_FRAME; 2099 xfer->endpoint->is_synced = 1; 2100 DPRINTFN(2, "start next=%d\n", xfer->endpoint->isoc_next); 2101 } 2102 /* 2103 * compute how many milliseconds the insertion is ahead of the 2104 * current frame position: 2105 */ 2106 temp = (xfer->endpoint->isoc_next - nframes) & MUSB2_MASK_FRAME; 2107 2108 /* 2109 * pre-compute when the isochronous transfer will be finished: 2110 */ 2111 xfer->isoc_time_complete = 2112 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp + 2113 fs_frames; 2114 2115 /* compute frame number for next insertion */ 2116 xfer->endpoint->isoc_next += fs_frames; 2117 2118 /* setup TDs */ 2119 musbotg_setup_standard_chain(xfer); 2120 } 2121 2122 static void 2123 musbotg_device_isoc_start(struct usb_xfer *xfer) 2124 { 2125 /* start TD chain */ 2126 musbotg_start_standard_chain(xfer); 2127 } 2128 2129 struct usb_pipe_methods musbotg_device_isoc_methods = 2130 { 2131 .open = musbotg_device_isoc_open, 2132 .close = musbotg_device_isoc_close, 2133 .enter = musbotg_device_isoc_enter, 2134 .start = musbotg_device_isoc_start, 2135 }; 2136 2137 /*------------------------------------------------------------------------* 2138 * musbotg root control support 2139 *------------------------------------------------------------------------* 2140 * Simulate a hardware HUB by handling all the necessary requests. 2141 *------------------------------------------------------------------------*/ 2142 2143 static const struct usb_device_descriptor musbotg_devd = { 2144 .bLength = sizeof(struct usb_device_descriptor), 2145 .bDescriptorType = UDESC_DEVICE, 2146 .bcdUSB = {0x00, 0x02}, 2147 .bDeviceClass = UDCLASS_HUB, 2148 .bDeviceSubClass = UDSUBCLASS_HUB, 2149 .bDeviceProtocol = UDPROTO_HSHUBSTT, 2150 .bMaxPacketSize = 64, 2151 .bcdDevice = {0x00, 0x01}, 2152 .iManufacturer = 1, 2153 .iProduct = 2, 2154 .bNumConfigurations = 1, 2155 }; 2156 2157 static const struct usb_device_qualifier musbotg_odevd = { 2158 .bLength = sizeof(struct usb_device_qualifier), 2159 .bDescriptorType = UDESC_DEVICE_QUALIFIER, 2160 .bcdUSB = {0x00, 0x02}, 2161 .bDeviceClass = UDCLASS_HUB, 2162 .bDeviceSubClass = UDSUBCLASS_HUB, 2163 .bDeviceProtocol = UDPROTO_FSHUB, 2164 .bMaxPacketSize0 = 0, 2165 .bNumConfigurations = 0, 2166 }; 2167 2168 static const struct musbotg_config_desc musbotg_confd = { 2169 .confd = { 2170 .bLength = sizeof(struct usb_config_descriptor), 2171 .bDescriptorType = UDESC_CONFIG, 2172 .wTotalLength[0] = sizeof(musbotg_confd), 2173 .bNumInterface = 1, 2174 .bConfigurationValue = 1, 2175 .iConfiguration = 0, 2176 .bmAttributes = UC_SELF_POWERED, 2177 .bMaxPower = 0, 2178 }, 2179 .ifcd = { 2180 .bLength = sizeof(struct usb_interface_descriptor), 2181 .bDescriptorType = UDESC_INTERFACE, 2182 .bNumEndpoints = 1, 2183 .bInterfaceClass = UICLASS_HUB, 2184 .bInterfaceSubClass = UISUBCLASS_HUB, 2185 .bInterfaceProtocol = 0, 2186 }, 2187 .endpd = { 2188 .bLength = sizeof(struct usb_endpoint_descriptor), 2189 .bDescriptorType = UDESC_ENDPOINT, 2190 .bEndpointAddress = (UE_DIR_IN | MUSBOTG_INTR_ENDPT), 2191 .bmAttributes = UE_INTERRUPT, 2192 .wMaxPacketSize[0] = 8, 2193 .bInterval = 255, 2194 }, 2195 }; 2196 2197 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) } 2198 2199 static const struct usb_hub_descriptor_min musbotg_hubd = { 2200 .bDescLength = sizeof(musbotg_hubd), 2201 .bDescriptorType = UDESC_HUB, 2202 .bNbrPorts = 1, 2203 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)), 2204 .bPwrOn2PwrGood = 50, 2205 .bHubContrCurrent = 0, 2206 .DeviceRemovable = {0}, /* port is removable */ 2207 }; 2208 2209 #define STRING_LANG \ 2210 0x09, 0x04, /* American English */ 2211 2212 #define STRING_VENDOR \ 2213 'M', 0, 'e', 0, 'n', 0, 't', 0, 'o', 0, 'r', 0, ' ', 0, \ 2214 'G', 0, 'r', 0, 'a', 0, 'p', 0, 'h', 0, 'i', 0, 'c', 0, 's', 0 2215 2216 #define STRING_PRODUCT \ 2217 'O', 0, 'T', 0, 'G', 0, ' ', 0, 'R', 0, \ 2218 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \ 2219 'U', 0, 'B', 0, 2220 2221 USB_MAKE_STRING_DESC(STRING_LANG, musbotg_langtab); 2222 USB_MAKE_STRING_DESC(STRING_VENDOR, musbotg_vendor); 2223 USB_MAKE_STRING_DESC(STRING_PRODUCT, musbotg_product); 2224 2225 static usb_error_t 2226 musbotg_roothub_exec(struct usb_device *udev, 2227 struct usb_device_request *req, const void **pptr, uint16_t *plength) 2228 { 2229 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus); 2230 const void *ptr; 2231 uint16_t len; 2232 uint16_t value; 2233 uint16_t index; 2234 usb_error_t err; 2235 2236 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 2237 2238 /* buffer reset */ 2239 ptr = (const void *)&sc->sc_hub_temp; 2240 len = 0; 2241 err = 0; 2242 2243 value = UGETW(req->wValue); 2244 index = UGETW(req->wIndex); 2245 2246 /* demultiplex the control request */ 2247 2248 switch (req->bmRequestType) { 2249 case UT_READ_DEVICE: 2250 switch (req->bRequest) { 2251 case UR_GET_DESCRIPTOR: 2252 goto tr_handle_get_descriptor; 2253 case UR_GET_CONFIG: 2254 goto tr_handle_get_config; 2255 case UR_GET_STATUS: 2256 goto tr_handle_get_status; 2257 default: 2258 goto tr_stalled; 2259 } 2260 break; 2261 2262 case UT_WRITE_DEVICE: 2263 switch (req->bRequest) { 2264 case UR_SET_ADDRESS: 2265 goto tr_handle_set_address; 2266 case UR_SET_CONFIG: 2267 goto tr_handle_set_config; 2268 case UR_CLEAR_FEATURE: 2269 goto tr_valid; /* nop */ 2270 case UR_SET_DESCRIPTOR: 2271 goto tr_valid; /* nop */ 2272 case UR_SET_FEATURE: 2273 default: 2274 goto tr_stalled; 2275 } 2276 break; 2277 2278 case UT_WRITE_ENDPOINT: 2279 switch (req->bRequest) { 2280 case UR_CLEAR_FEATURE: 2281 switch (UGETW(req->wValue)) { 2282 case UF_ENDPOINT_HALT: 2283 goto tr_handle_clear_halt; 2284 case UF_DEVICE_REMOTE_WAKEUP: 2285 goto tr_handle_clear_wakeup; 2286 default: 2287 goto tr_stalled; 2288 } 2289 break; 2290 case UR_SET_FEATURE: 2291 switch (UGETW(req->wValue)) { 2292 case UF_ENDPOINT_HALT: 2293 goto tr_handle_set_halt; 2294 case UF_DEVICE_REMOTE_WAKEUP: 2295 goto tr_handle_set_wakeup; 2296 default: 2297 goto tr_stalled; 2298 } 2299 break; 2300 case UR_SYNCH_FRAME: 2301 goto tr_valid; /* nop */ 2302 default: 2303 goto tr_stalled; 2304 } 2305 break; 2306 2307 case UT_READ_ENDPOINT: 2308 switch (req->bRequest) { 2309 case UR_GET_STATUS: 2310 goto tr_handle_get_ep_status; 2311 default: 2312 goto tr_stalled; 2313 } 2314 break; 2315 2316 case UT_WRITE_INTERFACE: 2317 switch (req->bRequest) { 2318 case UR_SET_INTERFACE: 2319 goto tr_handle_set_interface; 2320 case UR_CLEAR_FEATURE: 2321 goto tr_valid; /* nop */ 2322 case UR_SET_FEATURE: 2323 default: 2324 goto tr_stalled; 2325 } 2326 break; 2327 2328 case UT_READ_INTERFACE: 2329 switch (req->bRequest) { 2330 case UR_GET_INTERFACE: 2331 goto tr_handle_get_interface; 2332 case UR_GET_STATUS: 2333 goto tr_handle_get_iface_status; 2334 default: 2335 goto tr_stalled; 2336 } 2337 break; 2338 2339 case UT_WRITE_CLASS_INTERFACE: 2340 case UT_WRITE_VENDOR_INTERFACE: 2341 /* XXX forward */ 2342 break; 2343 2344 case UT_READ_CLASS_INTERFACE: 2345 case UT_READ_VENDOR_INTERFACE: 2346 /* XXX forward */ 2347 break; 2348 2349 case UT_WRITE_CLASS_DEVICE: 2350 switch (req->bRequest) { 2351 case UR_CLEAR_FEATURE: 2352 goto tr_valid; 2353 case UR_SET_DESCRIPTOR: 2354 case UR_SET_FEATURE: 2355 break; 2356 default: 2357 goto tr_stalled; 2358 } 2359 break; 2360 2361 case UT_WRITE_CLASS_OTHER: 2362 switch (req->bRequest) { 2363 case UR_CLEAR_FEATURE: 2364 goto tr_handle_clear_port_feature; 2365 case UR_SET_FEATURE: 2366 goto tr_handle_set_port_feature; 2367 case UR_CLEAR_TT_BUFFER: 2368 case UR_RESET_TT: 2369 case UR_STOP_TT: 2370 goto tr_valid; 2371 2372 default: 2373 goto tr_stalled; 2374 } 2375 break; 2376 2377 case UT_READ_CLASS_OTHER: 2378 switch (req->bRequest) { 2379 case UR_GET_TT_STATE: 2380 goto tr_handle_get_tt_state; 2381 case UR_GET_STATUS: 2382 goto tr_handle_get_port_status; 2383 default: 2384 goto tr_stalled; 2385 } 2386 break; 2387 2388 case UT_READ_CLASS_DEVICE: 2389 switch (req->bRequest) { 2390 case UR_GET_DESCRIPTOR: 2391 goto tr_handle_get_class_descriptor; 2392 case UR_GET_STATUS: 2393 goto tr_handle_get_class_status; 2394 2395 default: 2396 goto tr_stalled; 2397 } 2398 break; 2399 default: 2400 goto tr_stalled; 2401 } 2402 goto tr_valid; 2403 2404 tr_handle_get_descriptor: 2405 switch (value >> 8) { 2406 case UDESC_DEVICE: 2407 if (value & 0xff) { 2408 goto tr_stalled; 2409 } 2410 len = sizeof(musbotg_devd); 2411 ptr = (const void *)&musbotg_devd; 2412 goto tr_valid; 2413 case UDESC_CONFIG: 2414 if (value & 0xff) { 2415 goto tr_stalled; 2416 } 2417 len = sizeof(musbotg_confd); 2418 ptr = (const void *)&musbotg_confd; 2419 goto tr_valid; 2420 case UDESC_STRING: 2421 switch (value & 0xff) { 2422 case 0: /* Language table */ 2423 len = sizeof(musbotg_langtab); 2424 ptr = (const void *)&musbotg_langtab; 2425 goto tr_valid; 2426 2427 case 1: /* Vendor */ 2428 len = sizeof(musbotg_vendor); 2429 ptr = (const void *)&musbotg_vendor; 2430 goto tr_valid; 2431 2432 case 2: /* Product */ 2433 len = sizeof(musbotg_product); 2434 ptr = (const void *)&musbotg_product; 2435 goto tr_valid; 2436 default: 2437 break; 2438 } 2439 break; 2440 default: 2441 goto tr_stalled; 2442 } 2443 goto tr_stalled; 2444 2445 tr_handle_get_config: 2446 len = 1; 2447 sc->sc_hub_temp.wValue[0] = sc->sc_conf; 2448 goto tr_valid; 2449 2450 tr_handle_get_status: 2451 len = 2; 2452 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED); 2453 goto tr_valid; 2454 2455 tr_handle_set_address: 2456 if (value & 0xFF00) { 2457 goto tr_stalled; 2458 } 2459 sc->sc_rt_addr = value; 2460 goto tr_valid; 2461 2462 tr_handle_set_config: 2463 if (value >= 2) { 2464 goto tr_stalled; 2465 } 2466 sc->sc_conf = value; 2467 goto tr_valid; 2468 2469 tr_handle_get_interface: 2470 len = 1; 2471 sc->sc_hub_temp.wValue[0] = 0; 2472 goto tr_valid; 2473 2474 tr_handle_get_tt_state: 2475 tr_handle_get_class_status: 2476 tr_handle_get_iface_status: 2477 tr_handle_get_ep_status: 2478 len = 2; 2479 USETW(sc->sc_hub_temp.wValue, 0); 2480 goto tr_valid; 2481 2482 tr_handle_set_halt: 2483 tr_handle_set_interface: 2484 tr_handle_set_wakeup: 2485 tr_handle_clear_wakeup: 2486 tr_handle_clear_halt: 2487 goto tr_valid; 2488 2489 tr_handle_clear_port_feature: 2490 if (index != 1) { 2491 goto tr_stalled; 2492 } 2493 DPRINTFN(8, "UR_CLEAR_PORT_FEATURE on port %d\n", index); 2494 2495 switch (value) { 2496 case UHF_PORT_SUSPEND: 2497 musbotg_wakeup_peer(sc); 2498 break; 2499 2500 case UHF_PORT_ENABLE: 2501 sc->sc_flags.port_enabled = 0; 2502 break; 2503 2504 case UHF_PORT_TEST: 2505 case UHF_PORT_INDICATOR: 2506 case UHF_C_PORT_ENABLE: 2507 case UHF_C_PORT_OVER_CURRENT: 2508 case UHF_C_PORT_RESET: 2509 /* nops */ 2510 break; 2511 case UHF_PORT_POWER: 2512 sc->sc_flags.port_powered = 0; 2513 musbotg_pull_down(sc); 2514 musbotg_clocks_off(sc); 2515 break; 2516 case UHF_C_PORT_CONNECTION: 2517 sc->sc_flags.change_connect = 0; 2518 break; 2519 case UHF_C_PORT_SUSPEND: 2520 sc->sc_flags.change_suspend = 0; 2521 break; 2522 default: 2523 err = USB_ERR_IOERROR; 2524 goto done; 2525 } 2526 goto tr_valid; 2527 2528 tr_handle_set_port_feature: 2529 if (index != 1) { 2530 goto tr_stalled; 2531 } 2532 DPRINTFN(8, "UR_SET_PORT_FEATURE\n"); 2533 2534 switch (value) { 2535 case UHF_PORT_ENABLE: 2536 sc->sc_flags.port_enabled = 1; 2537 break; 2538 case UHF_PORT_SUSPEND: 2539 case UHF_PORT_RESET: 2540 case UHF_PORT_TEST: 2541 case UHF_PORT_INDICATOR: 2542 /* nops */ 2543 break; 2544 case UHF_PORT_POWER: 2545 sc->sc_flags.port_powered = 1; 2546 break; 2547 default: 2548 err = USB_ERR_IOERROR; 2549 goto done; 2550 } 2551 goto tr_valid; 2552 2553 tr_handle_get_port_status: 2554 2555 DPRINTFN(8, "UR_GET_PORT_STATUS\n"); 2556 2557 if (index != 1) { 2558 goto tr_stalled; 2559 } 2560 if (sc->sc_flags.status_vbus) { 2561 musbotg_clocks_on(sc); 2562 musbotg_pull_up(sc); 2563 } else { 2564 musbotg_pull_down(sc); 2565 musbotg_clocks_off(sc); 2566 } 2567 2568 /* Select Device Side Mode */ 2569 value = UPS_PORT_MODE_DEVICE; 2570 2571 if (sc->sc_flags.status_high_speed) { 2572 value |= UPS_HIGH_SPEED; 2573 } 2574 if (sc->sc_flags.port_powered) { 2575 value |= UPS_PORT_POWER; 2576 } 2577 if (sc->sc_flags.port_enabled) { 2578 value |= UPS_PORT_ENABLED; 2579 } 2580 if (sc->sc_flags.status_vbus && 2581 sc->sc_flags.status_bus_reset) { 2582 value |= UPS_CURRENT_CONNECT_STATUS; 2583 } 2584 if (sc->sc_flags.status_suspend) { 2585 value |= UPS_SUSPEND; 2586 } 2587 USETW(sc->sc_hub_temp.ps.wPortStatus, value); 2588 2589 value = 0; 2590 2591 if (sc->sc_flags.change_connect) { 2592 value |= UPS_C_CONNECT_STATUS; 2593 2594 if (sc->sc_flags.status_vbus && 2595 sc->sc_flags.status_bus_reset) { 2596 /* reset EP0 state */ 2597 sc->sc_ep0_busy = 0; 2598 sc->sc_ep0_cmd = 0; 2599 } 2600 } 2601 if (sc->sc_flags.change_suspend) { 2602 value |= UPS_C_SUSPEND; 2603 } 2604 USETW(sc->sc_hub_temp.ps.wPortChange, value); 2605 len = sizeof(sc->sc_hub_temp.ps); 2606 goto tr_valid; 2607 2608 tr_handle_get_class_descriptor: 2609 if (value & 0xFF) { 2610 goto tr_stalled; 2611 } 2612 ptr = (const void *)&musbotg_hubd; 2613 len = sizeof(musbotg_hubd); 2614 goto tr_valid; 2615 2616 tr_stalled: 2617 err = USB_ERR_STALLED; 2618 tr_valid: 2619 done: 2620 *plength = len; 2621 *pptr = ptr; 2622 return (err); 2623 } 2624 2625 static void 2626 musbotg_xfer_setup(struct usb_setup_params *parm) 2627 { 2628 const struct usb_hw_ep_profile *pf; 2629 struct musbotg_softc *sc; 2630 struct usb_xfer *xfer; 2631 void *last_obj; 2632 uint32_t ntd; 2633 uint32_t n; 2634 uint8_t ep_no; 2635 2636 sc = MUSBOTG_BUS2SC(parm->udev->bus); 2637 xfer = parm->curr_xfer; 2638 2639 /* 2640 * NOTE: This driver does not use any of the parameters that 2641 * are computed from the following values. Just set some 2642 * reasonable dummies: 2643 */ 2644 parm->hc_max_packet_size = 0x400; 2645 parm->hc_max_frame_size = 0x400; 2646 2647 if ((parm->methods == &musbotg_device_isoc_methods) || 2648 (parm->methods == &musbotg_device_intr_methods)) 2649 parm->hc_max_packet_count = 3; 2650 else 2651 parm->hc_max_packet_count = 1; 2652 2653 usbd_transfer_setup_sub(parm); 2654 2655 /* 2656 * compute maximum number of TDs 2657 */ 2658 if (parm->methods == &musbotg_device_ctrl_methods) { 2659 2660 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ; 2661 2662 } else if (parm->methods == &musbotg_device_bulk_methods) { 2663 2664 ntd = xfer->nframes + 1 /* SYNC */ ; 2665 2666 } else if (parm->methods == &musbotg_device_intr_methods) { 2667 2668 ntd = xfer->nframes + 1 /* SYNC */ ; 2669 2670 } else if (parm->methods == &musbotg_device_isoc_methods) { 2671 2672 ntd = xfer->nframes + 1 /* SYNC */ ; 2673 2674 } else { 2675 2676 ntd = 0; 2677 } 2678 2679 /* 2680 * check if "usbd_transfer_setup_sub" set an error 2681 */ 2682 if (parm->err) { 2683 return; 2684 } 2685 /* 2686 * allocate transfer descriptors 2687 */ 2688 last_obj = NULL; 2689 2690 /* 2691 * get profile stuff 2692 */ 2693 if (ntd) { 2694 2695 ep_no = xfer->endpointno & UE_ADDR; 2696 musbotg_get_hw_ep_profile(parm->udev, &pf, ep_no); 2697 2698 if (pf == NULL) { 2699 /* should not happen */ 2700 parm->err = USB_ERR_INVAL; 2701 return; 2702 } 2703 } else { 2704 ep_no = 0; 2705 pf = NULL; 2706 } 2707 2708 /* align data */ 2709 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 2710 2711 for (n = 0; n != ntd; n++) { 2712 2713 struct musbotg_td *td; 2714 2715 if (parm->buf) { 2716 2717 td = USB_ADD_BYTES(parm->buf, parm->size[0]); 2718 2719 /* init TD */ 2720 td->max_frame_size = xfer->max_frame_size; 2721 td->ep_no = ep_no; 2722 td->obj_next = last_obj; 2723 2724 last_obj = td; 2725 } 2726 parm->size[0] += sizeof(*td); 2727 } 2728 2729 xfer->td_start[0] = last_obj; 2730 } 2731 2732 static void 2733 musbotg_xfer_unsetup(struct usb_xfer *xfer) 2734 { 2735 return; 2736 } 2737 2738 static void 2739 musbotg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc, 2740 struct usb_endpoint *ep) 2741 { 2742 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus); 2743 2744 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 2745 ep, udev->address, 2746 edesc->bEndpointAddress, udev->flags.usb_mode, 2747 sc->sc_rt_addr); 2748 2749 if (udev->device_index != sc->sc_rt_addr) { 2750 2751 if ((udev->speed != USB_SPEED_FULL) && 2752 (udev->speed != USB_SPEED_HIGH)) { 2753 /* not supported */ 2754 return; 2755 } 2756 switch (edesc->bmAttributes & UE_XFERTYPE) { 2757 case UE_CONTROL: 2758 ep->methods = &musbotg_device_ctrl_methods; 2759 break; 2760 case UE_INTERRUPT: 2761 ep->methods = &musbotg_device_intr_methods; 2762 break; 2763 case UE_ISOCHRONOUS: 2764 ep->methods = &musbotg_device_isoc_methods; 2765 break; 2766 case UE_BULK: 2767 ep->methods = &musbotg_device_bulk_methods; 2768 break; 2769 default: 2770 /* do nothing */ 2771 break; 2772 } 2773 } 2774 } 2775 2776 static void 2777 musbotg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state) 2778 { 2779 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus); 2780 2781 switch (state) { 2782 case USB_HW_POWER_SUSPEND: 2783 musbotg_suspend(sc); 2784 break; 2785 case USB_HW_POWER_SHUTDOWN: 2786 musbotg_uninit(sc); 2787 break; 2788 case USB_HW_POWER_RESUME: 2789 musbotg_resume(sc); 2790 break; 2791 default: 2792 break; 2793 } 2794 } 2795 2796 struct usb_bus_methods musbotg_bus_methods = 2797 { 2798 .endpoint_init = &musbotg_ep_init, 2799 .xfer_setup = &musbotg_xfer_setup, 2800 .xfer_unsetup = &musbotg_xfer_unsetup, 2801 .get_hw_ep_profile = &musbotg_get_hw_ep_profile, 2802 .xfer_stall = &musbotg_xfer_stall, 2803 .set_stall = &musbotg_set_stall, 2804 .clear_stall = &musbotg_clear_stall, 2805 .roothub_exec = &musbotg_roothub_exec, 2806 .xfer_poll = &musbotg_do_poll, 2807 .set_hw_power_sleep = &musbotg_set_hw_power_sleep, 2808 }; 2809