1 /* $FreeBSD$ */ 2 /*- 3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4 * 5 * Copyright (c) 2008 Hans Petter Selasky <[email protected]> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * This file contains the driver for the USS820 series USB Device 32 * Controller 33 * 34 * NOTE: The datasheet does not document everything. 35 */ 36 37 #ifdef USB_GLOBAL_INCLUDE_FILE 38 #include USB_GLOBAL_INCLUDE_FILE 39 #else 40 #include <sys/stdint.h> 41 #include <sys/stddef.h> 42 #include <sys/param.h> 43 #include <sys/queue.h> 44 #include <sys/types.h> 45 #include <sys/systm.h> 46 #include <sys/kernel.h> 47 #include <sys/bus.h> 48 #include <sys/module.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/condvar.h> 52 #include <sys/sysctl.h> 53 #include <sys/sx.h> 54 #include <sys/unistd.h> 55 #include <sys/callout.h> 56 #include <sys/malloc.h> 57 #include <sys/priv.h> 58 59 #include <dev/usb/usb.h> 60 #include <dev/usb/usbdi.h> 61 62 #define USB_DEBUG_VAR uss820dcidebug 63 64 #include <dev/usb/usb_core.h> 65 #include <dev/usb/usb_debug.h> 66 #include <dev/usb/usb_busdma.h> 67 #include <dev/usb/usb_process.h> 68 #include <dev/usb/usb_transfer.h> 69 #include <dev/usb/usb_device.h> 70 #include <dev/usb/usb_hub.h> 71 #include <dev/usb/usb_util.h> 72 73 #include <dev/usb/usb_controller.h> 74 #include <dev/usb/usb_bus.h> 75 #endif /* USB_GLOBAL_INCLUDE_FILE */ 76 77 #include <dev/usb/controller/uss820dci.h> 78 79 #define USS820_DCI_BUS2SC(bus) \ 80 __containerof(bus, struct uss820dci_softc, sc_bus) 81 82 #define USS820_DCI_PC2SC(pc) \ 83 USS820_DCI_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus) 84 85 #define USS820_DCI_THREAD_IRQ \ 86 (USS820_SSR_SUSPEND | USS820_SSR_RESUME | USS820_SSR_RESET) 87 88 #ifdef USB_DEBUG 89 static int uss820dcidebug = 0; 90 91 static SYSCTL_NODE(_hw_usb, OID_AUTO, uss820dci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 92 "USB uss820dci"); 93 SYSCTL_INT(_hw_usb_uss820dci, OID_AUTO, debug, CTLFLAG_RWTUN, 94 &uss820dcidebug, 0, "uss820dci debug level"); 95 #endif 96 97 #define USS820_DCI_INTR_ENDPT 1 98 99 /* prototypes */ 100 101 static const struct usb_bus_methods uss820dci_bus_methods; 102 static const struct usb_pipe_methods uss820dci_device_bulk_methods; 103 static const struct usb_pipe_methods uss820dci_device_ctrl_methods; 104 static const struct usb_pipe_methods uss820dci_device_intr_methods; 105 static const struct usb_pipe_methods uss820dci_device_isoc_fs_methods; 106 107 static uss820dci_cmd_t uss820dci_setup_rx; 108 static uss820dci_cmd_t uss820dci_data_rx; 109 static uss820dci_cmd_t uss820dci_data_tx; 110 static uss820dci_cmd_t uss820dci_data_tx_sync; 111 static void uss820dci_device_done(struct usb_xfer *, usb_error_t); 112 static void uss820dci_do_poll(struct usb_bus *); 113 static void uss820dci_standard_done(struct usb_xfer *); 114 static void uss820dci_intr_set(struct usb_xfer *, uint8_t); 115 static void uss820dci_update_shared_1(struct uss820dci_softc *, uint8_t, 116 uint8_t, uint8_t); 117 static void uss820dci_root_intr(struct uss820dci_softc *); 118 119 /* 120 * Here is a list of what the USS820D chip can support. The main 121 * limitation is that the sum of the buffer sizes must be less than 122 * 1120 bytes. 123 */ 124 static const struct usb_hw_ep_profile 125 uss820dci_ep_profile[] = { 126 [0] = { 127 .max_in_frame_size = 32, 128 .max_out_frame_size = 32, 129 .is_simplex = 0, 130 .support_control = 1, 131 }, 132 [1] = { 133 .max_in_frame_size = 64, 134 .max_out_frame_size = 64, 135 .is_simplex = 0, 136 .support_multi_buffer = 1, 137 .support_bulk = 1, 138 .support_interrupt = 1, 139 .support_in = 1, 140 .support_out = 1, 141 }, 142 [2] = { 143 .max_in_frame_size = 8, 144 .max_out_frame_size = 8, 145 .is_simplex = 0, 146 .support_multi_buffer = 1, 147 .support_bulk = 1, 148 .support_interrupt = 1, 149 .support_in = 1, 150 .support_out = 1, 151 }, 152 [3] = { 153 .max_in_frame_size = 256, 154 .max_out_frame_size = 256, 155 .is_simplex = 0, 156 .support_multi_buffer = 1, 157 .support_isochronous = 1, 158 .support_in = 1, 159 .support_out = 1, 160 }, 161 }; 162 163 static void 164 uss820dci_update_shared_1(struct uss820dci_softc *sc, uint8_t reg, 165 uint8_t keep_mask, uint8_t set_mask) 166 { 167 uint8_t temp; 168 169 USS820_WRITE_1(sc, USS820_PEND, 1); 170 temp = USS820_READ_1(sc, reg); 171 temp &= (keep_mask); 172 temp |= (set_mask); 173 USS820_WRITE_1(sc, reg, temp); 174 USS820_WRITE_1(sc, USS820_PEND, 0); 175 } 176 177 static void 178 uss820dci_get_hw_ep_profile(struct usb_device *udev, 179 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr) 180 { 181 if (ep_addr == 0) { 182 *ppf = uss820dci_ep_profile + 0; 183 } else if (ep_addr < 5) { 184 *ppf = uss820dci_ep_profile + 1; 185 } else if (ep_addr < 7) { 186 *ppf = uss820dci_ep_profile + 2; 187 } else if (ep_addr == 7) { 188 *ppf = uss820dci_ep_profile + 3; 189 } else { 190 *ppf = NULL; 191 } 192 } 193 194 static void 195 uss820dci_pull_up(struct uss820dci_softc *sc) 196 { 197 uint8_t temp; 198 199 /* pullup D+, if possible */ 200 201 if (!sc->sc_flags.d_pulled_up && 202 sc->sc_flags.port_powered) { 203 sc->sc_flags.d_pulled_up = 1; 204 205 DPRINTF("\n"); 206 207 temp = USS820_READ_1(sc, USS820_MCSR); 208 temp |= USS820_MCSR_DPEN; 209 USS820_WRITE_1(sc, USS820_MCSR, temp); 210 } 211 } 212 213 static void 214 uss820dci_pull_down(struct uss820dci_softc *sc) 215 { 216 uint8_t temp; 217 218 /* pulldown D+, if possible */ 219 220 if (sc->sc_flags.d_pulled_up) { 221 sc->sc_flags.d_pulled_up = 0; 222 223 DPRINTF("\n"); 224 225 temp = USS820_READ_1(sc, USS820_MCSR); 226 temp &= ~USS820_MCSR_DPEN; 227 USS820_WRITE_1(sc, USS820_MCSR, temp); 228 } 229 } 230 231 static void 232 uss820dci_wakeup_peer(struct uss820dci_softc *sc) 233 { 234 if (!(sc->sc_flags.status_suspend)) { 235 return; 236 } 237 DPRINTFN(0, "not supported\n"); 238 } 239 240 static void 241 uss820dci_set_address(struct uss820dci_softc *sc, uint8_t addr) 242 { 243 DPRINTFN(5, "addr=%d\n", addr); 244 245 USS820_WRITE_1(sc, USS820_FADDR, addr); 246 } 247 248 static uint8_t 249 uss820dci_setup_rx(struct uss820dci_softc *sc, struct uss820dci_td *td) 250 { 251 struct usb_device_request req; 252 uint16_t count; 253 uint8_t rx_stat; 254 uint8_t temp; 255 256 /* select the correct endpoint */ 257 USS820_WRITE_1(sc, USS820_EPINDEX, td->ep_index); 258 259 /* read out FIFO status */ 260 rx_stat = USS820_READ_1(sc, USS820_RXSTAT); 261 262 DPRINTFN(5, "rx_stat=0x%02x rem=%u\n", rx_stat, td->remainder); 263 264 if (!(rx_stat & USS820_RXSTAT_RXSETUP)) { 265 goto not_complete; 266 } 267 /* clear did stall */ 268 td->did_stall = 0; 269 270 /* clear stall and all I/O */ 271 uss820dci_update_shared_1(sc, USS820_EPCON, 272 0xFF ^ (USS820_EPCON_TXSTL | 273 USS820_EPCON_RXSTL | 274 USS820_EPCON_RXIE | 275 USS820_EPCON_TXOE), 0); 276 277 /* clear end overwrite flag */ 278 uss820dci_update_shared_1(sc, USS820_RXSTAT, 279 0xFF ^ USS820_RXSTAT_EDOVW, 0); 280 281 /* get the packet byte count */ 282 count = USS820_READ_1(sc, USS820_RXCNTL); 283 count |= (USS820_READ_1(sc, USS820_RXCNTH) << 8); 284 count &= 0x3FF; 285 286 /* verify data length */ 287 if (count != td->remainder) { 288 DPRINTFN(0, "Invalid SETUP packet " 289 "length, %d bytes\n", count); 290 goto setup_not_complete; 291 } 292 if (count != sizeof(req)) { 293 DPRINTFN(0, "Unsupported SETUP packet " 294 "length, %d bytes\n", count); 295 goto setup_not_complete; 296 } 297 /* receive data */ 298 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 299 USS820_RXDAT * USS820_REG_STRIDE, (void *)&req, sizeof(req)); 300 301 /* read out FIFO status */ 302 rx_stat = USS820_READ_1(sc, USS820_RXSTAT); 303 304 if (rx_stat & (USS820_RXSTAT_EDOVW | 305 USS820_RXSTAT_STOVW)) { 306 DPRINTF("new SETUP packet received\n"); 307 return (1); /* not complete */ 308 } 309 /* clear receive setup bit */ 310 uss820dci_update_shared_1(sc, USS820_RXSTAT, 311 0xFF ^ (USS820_RXSTAT_RXSETUP | 312 USS820_RXSTAT_EDOVW | 313 USS820_RXSTAT_STOVW), 0); 314 315 /* set RXFFRC bit */ 316 temp = USS820_READ_1(sc, USS820_RXCON); 317 temp |= USS820_RXCON_RXFFRC; 318 USS820_WRITE_1(sc, USS820_RXCON, temp); 319 320 /* copy data into real buffer */ 321 usbd_copy_in(td->pc, 0, &req, sizeof(req)); 322 323 td->offset = sizeof(req); 324 td->remainder = 0; 325 326 /* sneak peek the set address */ 327 if ((req.bmRequestType == UT_WRITE_DEVICE) && 328 (req.bRequest == UR_SET_ADDRESS)) { 329 sc->sc_dv_addr = req.wValue[0] & 0x7F; 330 } else { 331 sc->sc_dv_addr = 0xFF; 332 } 333 334 /* reset TX FIFO */ 335 temp = USS820_READ_1(sc, USS820_TXCON); 336 temp |= USS820_TXCON_TXCLR; 337 USS820_WRITE_1(sc, USS820_TXCON, temp); 338 temp &= ~USS820_TXCON_TXCLR; 339 USS820_WRITE_1(sc, USS820_TXCON, temp); 340 341 return (0); /* complete */ 342 343 setup_not_complete: 344 345 /* set RXFFRC bit */ 346 temp = USS820_READ_1(sc, USS820_RXCON); 347 temp |= USS820_RXCON_RXFFRC; 348 USS820_WRITE_1(sc, USS820_RXCON, temp); 349 350 /* FALLTHROUGH */ 351 352 not_complete: 353 /* abort any ongoing transfer */ 354 if (!td->did_stall) { 355 DPRINTFN(5, "stalling\n"); 356 /* set stall */ 357 uss820dci_update_shared_1(sc, USS820_EPCON, 0xFF, 358 (USS820_EPCON_TXSTL | USS820_EPCON_RXSTL)); 359 360 td->did_stall = 1; 361 } 362 363 /* clear end overwrite flag, if any */ 364 if (rx_stat & USS820_RXSTAT_RXSETUP) { 365 uss820dci_update_shared_1(sc, USS820_RXSTAT, 366 0xFF ^ (USS820_RXSTAT_EDOVW | 367 USS820_RXSTAT_STOVW | 368 USS820_RXSTAT_RXSETUP), 0); 369 } 370 return (1); /* not complete */ 371 } 372 373 static uint8_t 374 uss820dci_data_rx(struct uss820dci_softc *sc, struct uss820dci_td *td) 375 { 376 struct usb_page_search buf_res; 377 uint16_t count; 378 uint8_t rx_flag; 379 uint8_t rx_stat; 380 uint8_t rx_cntl; 381 uint8_t to; 382 uint8_t got_short; 383 384 to = 2; /* don't loop forever! */ 385 got_short = 0; 386 387 /* select the correct endpoint */ 388 USS820_WRITE_1(sc, USS820_EPINDEX, td->ep_index); 389 390 /* check if any of the FIFO banks have data */ 391 repeat: 392 /* read out FIFO flag */ 393 rx_flag = USS820_READ_1(sc, USS820_RXFLG); 394 /* read out FIFO status */ 395 rx_stat = USS820_READ_1(sc, USS820_RXSTAT); 396 397 DPRINTFN(5, "rx_stat=0x%02x rx_flag=0x%02x rem=%u\n", 398 rx_stat, rx_flag, td->remainder); 399 400 if (rx_stat & (USS820_RXSTAT_RXSETUP | 401 USS820_RXSTAT_RXSOVW | 402 USS820_RXSTAT_EDOVW)) { 403 if (td->remainder == 0 && td->ep_index == 0) { 404 /* 405 * We are actually complete and have 406 * received the next SETUP 407 */ 408 DPRINTFN(5, "faking complete\n"); 409 return (0); /* complete */ 410 } 411 /* 412 * USB Host Aborted the transfer. 413 */ 414 td->error = 1; 415 return (0); /* complete */ 416 } 417 /* check for errors */ 418 if (rx_flag & (USS820_RXFLG_RXOVF | 419 USS820_RXFLG_RXURF)) { 420 DPRINTFN(5, "overflow or underflow\n"); 421 /* should not happen */ 422 td->error = 1; 423 return (0); /* complete */ 424 } 425 /* check status */ 426 if (!(rx_flag & (USS820_RXFLG_RXFIF0 | 427 USS820_RXFLG_RXFIF1))) { 428 /* read out EPCON register */ 429 /* enable RX input */ 430 if (!td->did_enable) { 431 uss820dci_update_shared_1(USS820_DCI_PC2SC(td->pc), 432 USS820_EPCON, 0xFF, USS820_EPCON_RXIE); 433 td->did_enable = 1; 434 } 435 return (1); /* not complete */ 436 } 437 /* get the packet byte count */ 438 count = USS820_READ_1(sc, USS820_RXCNTL); 439 count |= (USS820_READ_1(sc, USS820_RXCNTH) << 8); 440 count &= 0x3FF; 441 442 DPRINTFN(5, "count=0x%04x\n", count); 443 444 /* verify the packet byte count */ 445 if (count != td->max_packet_size) { 446 if (count < td->max_packet_size) { 447 /* we have a short packet */ 448 td->short_pkt = 1; 449 got_short = 1; 450 } else { 451 /* invalid USB packet */ 452 td->error = 1; 453 return (0); /* we are complete */ 454 } 455 } 456 /* verify the packet byte count */ 457 if (count > td->remainder) { 458 /* invalid USB packet */ 459 td->error = 1; 460 return (0); /* we are complete */ 461 } 462 while (count > 0) { 463 usbd_get_page(td->pc, td->offset, &buf_res); 464 465 /* get correct length */ 466 if (buf_res.length > count) { 467 buf_res.length = count; 468 } 469 /* receive data */ 470 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 471 USS820_RXDAT * USS820_REG_STRIDE, buf_res.buffer, buf_res.length); 472 473 /* update counters */ 474 count -= buf_res.length; 475 td->offset += buf_res.length; 476 td->remainder -= buf_res.length; 477 } 478 479 /* set RXFFRC bit */ 480 rx_cntl = USS820_READ_1(sc, USS820_RXCON); 481 rx_cntl |= USS820_RXCON_RXFFRC; 482 USS820_WRITE_1(sc, USS820_RXCON, rx_cntl); 483 484 /* check if we are complete */ 485 if ((td->remainder == 0) || got_short) { 486 if (td->short_pkt) { 487 /* we are complete */ 488 return (0); 489 } 490 /* else need to receive a zero length packet */ 491 } 492 if (--to) { 493 goto repeat; 494 } 495 return (1); /* not complete */ 496 } 497 498 static uint8_t 499 uss820dci_data_tx(struct uss820dci_softc *sc, struct uss820dci_td *td) 500 { 501 struct usb_page_search buf_res; 502 uint16_t count; 503 uint16_t count_copy; 504 uint8_t rx_stat; 505 uint8_t tx_flag; 506 uint8_t to; 507 508 /* select the correct endpoint */ 509 USS820_WRITE_1(sc, USS820_EPINDEX, td->ep_index); 510 511 to = 2; /* don't loop forever! */ 512 513 repeat: 514 /* read out TX FIFO flags */ 515 tx_flag = USS820_READ_1(sc, USS820_TXFLG); 516 517 DPRINTFN(5, "tx_flag=0x%02x rem=%u\n", tx_flag, td->remainder); 518 519 if (td->ep_index == 0) { 520 /* read out RX FIFO status last */ 521 rx_stat = USS820_READ_1(sc, USS820_RXSTAT); 522 523 DPRINTFN(5, "rx_stat=0x%02x\n", rx_stat); 524 525 if (rx_stat & (USS820_RXSTAT_RXSETUP | 526 USS820_RXSTAT_RXSOVW | 527 USS820_RXSTAT_EDOVW)) { 528 /* 529 * The current transfer was aborted by the USB 530 * Host: 531 */ 532 td->error = 1; 533 return (0); /* complete */ 534 } 535 } 536 if (tx_flag & (USS820_TXFLG_TXOVF | 537 USS820_TXFLG_TXURF)) { 538 td->error = 1; 539 return (0); /* complete */ 540 } 541 if (tx_flag & USS820_TXFLG_TXFIF0) { 542 if (tx_flag & USS820_TXFLG_TXFIF1) { 543 return (1); /* not complete */ 544 } 545 } 546 if ((!td->support_multi_buffer) && 547 (tx_flag & (USS820_TXFLG_TXFIF0 | 548 USS820_TXFLG_TXFIF1))) { 549 return (1); /* not complete */ 550 } 551 count = td->max_packet_size; 552 if (td->remainder < count) { 553 /* we have a short packet */ 554 td->short_pkt = 1; 555 count = td->remainder; 556 } 557 count_copy = count; 558 while (count > 0) { 559 usbd_get_page(td->pc, td->offset, &buf_res); 560 561 /* get correct length */ 562 if (buf_res.length > count) { 563 buf_res.length = count; 564 } 565 /* transmit data */ 566 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 567 USS820_TXDAT * USS820_REG_STRIDE, buf_res.buffer, buf_res.length); 568 569 /* update counters */ 570 count -= buf_res.length; 571 td->offset += buf_res.length; 572 td->remainder -= buf_res.length; 573 } 574 575 /* post-write high packet byte count first */ 576 USS820_WRITE_1(sc, USS820_TXCNTH, count_copy >> 8); 577 578 /* post-write low packet byte count last */ 579 USS820_WRITE_1(sc, USS820_TXCNTL, count_copy); 580 581 /* 582 * Enable TX output, which must happen after that we have written 583 * data into the FIFO. This is undocumented. 584 */ 585 if (!td->did_enable) { 586 uss820dci_update_shared_1(USS820_DCI_PC2SC(td->pc), 587 USS820_EPCON, 0xFF, USS820_EPCON_TXOE); 588 td->did_enable = 1; 589 } 590 /* check remainder */ 591 if (td->remainder == 0) { 592 if (td->short_pkt) { 593 return (0); /* complete */ 594 } 595 /* else we need to transmit a short packet */ 596 } 597 if (--to) { 598 goto repeat; 599 } 600 return (1); /* not complete */ 601 } 602 603 static uint8_t 604 uss820dci_data_tx_sync(struct uss820dci_softc *sc, struct uss820dci_td *td) 605 { 606 uint8_t rx_stat; 607 uint8_t tx_flag; 608 609 /* select the correct endpoint */ 610 USS820_WRITE_1(sc, USS820_EPINDEX, td->ep_index); 611 612 /* read out TX FIFO flag */ 613 tx_flag = USS820_READ_1(sc, USS820_TXFLG); 614 615 if (td->ep_index == 0) { 616 /* read out RX FIFO status last */ 617 rx_stat = USS820_READ_1(sc, USS820_RXSTAT); 618 619 DPRINTFN(5, "rx_stat=0x%02x rem=%u\n", rx_stat, td->remainder); 620 621 if (rx_stat & (USS820_RXSTAT_RXSETUP | 622 USS820_RXSTAT_RXSOVW | 623 USS820_RXSTAT_EDOVW)) { 624 DPRINTFN(5, "faking complete\n"); 625 /* Race condition */ 626 return (0); /* complete */ 627 } 628 } 629 DPRINTFN(5, "tx_flag=0x%02x rem=%u\n", tx_flag, td->remainder); 630 631 if (tx_flag & (USS820_TXFLG_TXOVF | 632 USS820_TXFLG_TXURF)) { 633 td->error = 1; 634 return (0); /* complete */ 635 } 636 if (tx_flag & (USS820_TXFLG_TXFIF0 | 637 USS820_TXFLG_TXFIF1)) { 638 return (1); /* not complete */ 639 } 640 if (td->ep_index == 0 && sc->sc_dv_addr != 0xFF) { 641 /* write function address */ 642 uss820dci_set_address(sc, sc->sc_dv_addr); 643 } 644 return (0); /* complete */ 645 } 646 647 static void 648 uss820dci_xfer_do_fifo(struct usb_xfer *xfer) 649 { 650 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 651 struct uss820dci_td *td; 652 653 DPRINTFN(9, "\n"); 654 655 td = xfer->td_transfer_cache; 656 if (td == NULL) 657 return; 658 659 while (1) { 660 if ((td->func) (sc, td)) { 661 /* operation in progress */ 662 break; 663 } 664 if (((void *)td) == xfer->td_transfer_last) { 665 goto done; 666 } 667 if (td->error) { 668 goto done; 669 } else if (td->remainder > 0) { 670 /* 671 * We had a short transfer. If there is no alternate 672 * next, stop processing ! 673 */ 674 if (!td->alt_next) { 675 goto done; 676 } 677 } 678 /* 679 * Fetch the next transfer descriptor. 680 */ 681 td = td->obj_next; 682 xfer->td_transfer_cache = td; 683 } 684 return; 685 686 done: 687 /* compute all actual lengths */ 688 xfer->td_transfer_cache = NULL; 689 sc->sc_xfer_complete = 1; 690 } 691 692 static uint8_t 693 uss820dci_xfer_do_complete(struct usb_xfer *xfer) 694 { 695 struct uss820dci_td *td; 696 697 DPRINTFN(9, "\n"); 698 699 td = xfer->td_transfer_cache; 700 if (td == NULL) { 701 /* compute all actual lengths */ 702 uss820dci_standard_done(xfer); 703 return(1); 704 } 705 return (0); 706 } 707 708 static void 709 uss820dci_interrupt_poll_locked(struct uss820dci_softc *sc) 710 { 711 struct usb_xfer *xfer; 712 713 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) 714 uss820dci_xfer_do_fifo(xfer); 715 } 716 717 static void 718 uss820dci_interrupt_complete_locked(struct uss820dci_softc *sc) 719 { 720 struct usb_xfer *xfer; 721 repeat: 722 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 723 if (uss820dci_xfer_do_complete(xfer)) 724 goto repeat; 725 } 726 } 727 728 static void 729 uss820dci_wait_suspend(struct uss820dci_softc *sc, uint8_t on) 730 { 731 uint8_t scr; 732 uint8_t scratch; 733 734 scr = USS820_READ_1(sc, USS820_SCR); 735 scratch = USS820_READ_1(sc, USS820_SCRATCH); 736 737 if (on) { 738 scr |= USS820_SCR_IE_SUSP; 739 scratch &= ~USS820_SCRATCH_IE_RESUME; 740 } else { 741 scr &= ~USS820_SCR_IE_SUSP; 742 scratch |= USS820_SCRATCH_IE_RESUME; 743 } 744 745 USS820_WRITE_1(sc, USS820_SCR, scr); 746 USS820_WRITE_1(sc, USS820_SCRATCH, scratch); 747 } 748 749 int 750 uss820dci_filter_interrupt(void *arg) 751 { 752 struct uss820dci_softc *sc = arg; 753 int retval = FILTER_HANDLED; 754 uint8_t ssr; 755 756 USB_BUS_SPIN_LOCK(&sc->sc_bus); 757 758 ssr = USS820_READ_1(sc, USS820_SSR); 759 uss820dci_update_shared_1(sc, USS820_SSR, USS820_DCI_THREAD_IRQ, 0); 760 761 if (ssr & USS820_DCI_THREAD_IRQ) 762 retval = FILTER_SCHEDULE_THREAD; 763 764 /* poll FIFOs, if any */ 765 uss820dci_interrupt_poll_locked(sc); 766 767 if (sc->sc_xfer_complete != 0) 768 retval = FILTER_SCHEDULE_THREAD; 769 770 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 771 772 return (retval); 773 } 774 775 void 776 uss820dci_interrupt(void *arg) 777 { 778 struct uss820dci_softc *sc = arg; 779 uint8_t ssr; 780 uint8_t event; 781 782 USB_BUS_LOCK(&sc->sc_bus); 783 USB_BUS_SPIN_LOCK(&sc->sc_bus); 784 785 ssr = USS820_READ_1(sc, USS820_SSR); 786 787 /* acknowledge all interrupts */ 788 789 uss820dci_update_shared_1(sc, USS820_SSR, ~USS820_DCI_THREAD_IRQ, 0); 790 791 /* check for any bus state change interrupts */ 792 793 if (ssr & USS820_DCI_THREAD_IRQ) { 794 event = 0; 795 796 if (ssr & USS820_SSR_RESET) { 797 sc->sc_flags.status_bus_reset = 1; 798 sc->sc_flags.status_suspend = 0; 799 sc->sc_flags.change_suspend = 0; 800 sc->sc_flags.change_connect = 1; 801 802 /* disable resume interrupt */ 803 uss820dci_wait_suspend(sc, 1); 804 805 event = 1; 806 } 807 /* 808 * If "RESUME" and "SUSPEND" is set at the same time 809 * we interpret that like "RESUME". Resume is set when 810 * there is at least 3 milliseconds of inactivity on 811 * the USB BUS. 812 */ 813 if (ssr & USS820_SSR_RESUME) { 814 if (sc->sc_flags.status_suspend) { 815 sc->sc_flags.status_suspend = 0; 816 sc->sc_flags.change_suspend = 1; 817 /* disable resume interrupt */ 818 uss820dci_wait_suspend(sc, 1); 819 event = 1; 820 } 821 } else if (ssr & USS820_SSR_SUSPEND) { 822 if (!sc->sc_flags.status_suspend) { 823 sc->sc_flags.status_suspend = 1; 824 sc->sc_flags.change_suspend = 1; 825 /* enable resume interrupt */ 826 uss820dci_wait_suspend(sc, 0); 827 event = 1; 828 } 829 } 830 if (event) { 831 DPRINTF("real bus interrupt 0x%02x\n", ssr); 832 833 /* complete root HUB interrupt endpoint */ 834 uss820dci_root_intr(sc); 835 } 836 } 837 /* acknowledge all SBI interrupts */ 838 uss820dci_update_shared_1(sc, USS820_SBI, 0, 0); 839 840 /* acknowledge all SBI1 interrupts */ 841 uss820dci_update_shared_1(sc, USS820_SBI1, 0, 0); 842 843 if (sc->sc_xfer_complete != 0) { 844 sc->sc_xfer_complete = 0; 845 846 /* complete FIFOs, if any */ 847 uss820dci_interrupt_complete_locked(sc); 848 } 849 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 850 USB_BUS_UNLOCK(&sc->sc_bus); 851 } 852 853 static void 854 uss820dci_setup_standard_chain_sub(struct uss820_std_temp *temp) 855 { 856 struct uss820dci_td *td; 857 858 /* get current Transfer Descriptor */ 859 td = temp->td_next; 860 temp->td = td; 861 862 /* prepare for next TD */ 863 temp->td_next = td->obj_next; 864 865 /* fill out the Transfer Descriptor */ 866 td->func = temp->func; 867 td->pc = temp->pc; 868 td->offset = temp->offset; 869 td->remainder = temp->len; 870 td->error = 0; 871 td->did_enable = 0; 872 td->did_stall = temp->did_stall; 873 td->short_pkt = temp->short_pkt; 874 td->alt_next = temp->setup_alt_next; 875 } 876 877 static void 878 uss820dci_setup_standard_chain(struct usb_xfer *xfer) 879 { 880 struct uss820_std_temp temp; 881 struct uss820dci_softc *sc; 882 struct uss820dci_td *td; 883 uint32_t x; 884 uint8_t ep_no; 885 886 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n", 887 xfer->address, UE_GET_ADDR(xfer->endpointno), 888 xfer->sumlen, usbd_get_speed(xfer->xroot->udev)); 889 890 temp.max_frame_size = xfer->max_frame_size; 891 892 td = xfer->td_start[0]; 893 xfer->td_transfer_first = td; 894 xfer->td_transfer_cache = td; 895 896 /* setup temp */ 897 898 temp.pc = NULL; 899 temp.td = NULL; 900 temp.td_next = xfer->td_start[0]; 901 temp.offset = 0; 902 temp.setup_alt_next = xfer->flags_int.short_frames_ok || 903 xfer->flags_int.isochronous_xfr; 904 temp.did_stall = !xfer->flags_int.control_stall; 905 906 sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 907 ep_no = (xfer->endpointno & UE_ADDR); 908 909 /* check if we should prepend a setup message */ 910 911 if (xfer->flags_int.control_xfr) { 912 if (xfer->flags_int.control_hdr) { 913 temp.func = &uss820dci_setup_rx; 914 temp.len = xfer->frlengths[0]; 915 temp.pc = xfer->frbuffers + 0; 916 temp.short_pkt = temp.len ? 1 : 0; 917 /* check for last frame */ 918 if (xfer->nframes == 1) { 919 /* no STATUS stage yet, SETUP is last */ 920 if (xfer->flags_int.control_act) 921 temp.setup_alt_next = 0; 922 } 923 924 uss820dci_setup_standard_chain_sub(&temp); 925 } 926 x = 1; 927 } else { 928 x = 0; 929 } 930 931 if (x != xfer->nframes) { 932 if (xfer->endpointno & UE_DIR_IN) { 933 temp.func = &uss820dci_data_tx; 934 } else { 935 temp.func = &uss820dci_data_rx; 936 } 937 938 /* setup "pc" pointer */ 939 temp.pc = xfer->frbuffers + x; 940 } 941 while (x != xfer->nframes) { 942 /* DATA0 / DATA1 message */ 943 944 temp.len = xfer->frlengths[x]; 945 946 x++; 947 948 if (x == xfer->nframes) { 949 if (xfer->flags_int.control_xfr) { 950 if (xfer->flags_int.control_act) { 951 temp.setup_alt_next = 0; 952 } 953 } else { 954 temp.setup_alt_next = 0; 955 } 956 } 957 if (temp.len == 0) { 958 /* make sure that we send an USB packet */ 959 960 temp.short_pkt = 0; 961 962 } else { 963 /* regular data transfer */ 964 965 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1; 966 } 967 968 uss820dci_setup_standard_chain_sub(&temp); 969 970 if (xfer->flags_int.isochronous_xfr) { 971 temp.offset += temp.len; 972 } else { 973 /* get next Page Cache pointer */ 974 temp.pc = xfer->frbuffers + x; 975 } 976 } 977 978 /* check for control transfer */ 979 if (xfer->flags_int.control_xfr) { 980 uint8_t need_sync; 981 982 /* always setup a valid "pc" pointer for status and sync */ 983 temp.pc = xfer->frbuffers + 0; 984 temp.len = 0; 985 temp.short_pkt = 0; 986 temp.setup_alt_next = 0; 987 988 /* check if we should append a status stage */ 989 if (!xfer->flags_int.control_act) { 990 /* 991 * Send a DATA1 message and invert the current 992 * endpoint direction. 993 */ 994 if (xfer->endpointno & UE_DIR_IN) { 995 temp.func = &uss820dci_data_rx; 996 need_sync = 0; 997 } else { 998 temp.func = &uss820dci_data_tx; 999 need_sync = 1; 1000 } 1001 temp.len = 0; 1002 temp.short_pkt = 0; 1003 1004 uss820dci_setup_standard_chain_sub(&temp); 1005 if (need_sync) { 1006 /* we need a SYNC point after TX */ 1007 temp.func = &uss820dci_data_tx_sync; 1008 uss820dci_setup_standard_chain_sub(&temp); 1009 } 1010 } 1011 } 1012 /* must have at least one frame! */ 1013 td = temp.td; 1014 xfer->td_transfer_last = td; 1015 } 1016 1017 static void 1018 uss820dci_timeout(void *arg) 1019 { 1020 struct usb_xfer *xfer = arg; 1021 1022 DPRINTF("xfer=%p\n", xfer); 1023 1024 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1025 1026 /* transfer is transferred */ 1027 uss820dci_device_done(xfer, USB_ERR_TIMEOUT); 1028 } 1029 1030 static void 1031 uss820dci_intr_set(struct usb_xfer *xfer, uint8_t set) 1032 { 1033 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 1034 uint8_t ep_no = (xfer->endpointno & UE_ADDR); 1035 uint8_t ep_reg; 1036 uint8_t temp; 1037 1038 DPRINTFN(15, "endpoint 0x%02x\n", xfer->endpointno); 1039 1040 if (ep_no > 3) { 1041 ep_reg = USS820_SBIE1; 1042 } else { 1043 ep_reg = USS820_SBIE; 1044 } 1045 1046 ep_no &= 3; 1047 ep_no = 1 << (2 * ep_no); 1048 1049 if (xfer->flags_int.control_xfr) { 1050 if (xfer->flags_int.control_hdr) { 1051 ep_no <<= 1; /* RX interrupt only */ 1052 } else { 1053 ep_no |= (ep_no << 1); /* RX and TX interrupt */ 1054 } 1055 } else { 1056 if (!(xfer->endpointno & UE_DIR_IN)) { 1057 ep_no <<= 1; 1058 } 1059 } 1060 temp = USS820_READ_1(sc, ep_reg); 1061 if (set) { 1062 temp |= ep_no; 1063 } else { 1064 temp &= ~ep_no; 1065 } 1066 USS820_WRITE_1(sc, ep_reg, temp); 1067 } 1068 1069 static void 1070 uss820dci_start_standard_chain(struct usb_xfer *xfer) 1071 { 1072 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 1073 1074 DPRINTFN(9, "\n"); 1075 1076 USB_BUS_SPIN_LOCK(&sc->sc_bus); 1077 1078 /* poll one time */ 1079 uss820dci_xfer_do_fifo(xfer); 1080 1081 if (uss820dci_xfer_do_complete(xfer) == 0) { 1082 /* 1083 * Only enable the endpoint interrupt when we are 1084 * actually waiting for data, hence we are dealing 1085 * with level triggered interrupts ! 1086 */ 1087 uss820dci_intr_set(xfer, 1); 1088 1089 /* put transfer on interrupt queue */ 1090 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 1091 1092 /* start timeout, if any */ 1093 if (xfer->timeout != 0) { 1094 usbd_transfer_timeout_ms(xfer, 1095 &uss820dci_timeout, xfer->timeout); 1096 } 1097 } 1098 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 1099 } 1100 1101 static void 1102 uss820dci_root_intr(struct uss820dci_softc *sc) 1103 { 1104 DPRINTFN(9, "\n"); 1105 1106 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1107 1108 /* set port bit */ 1109 sc->sc_hub_idata[0] = 0x02; /* we only have one port */ 1110 1111 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata, 1112 sizeof(sc->sc_hub_idata)); 1113 } 1114 1115 static usb_error_t 1116 uss820dci_standard_done_sub(struct usb_xfer *xfer) 1117 { 1118 struct uss820dci_td *td; 1119 uint32_t len; 1120 uint8_t error; 1121 1122 DPRINTFN(9, "\n"); 1123 1124 td = xfer->td_transfer_cache; 1125 1126 do { 1127 len = td->remainder; 1128 1129 if (xfer->aframes != xfer->nframes) { 1130 /* 1131 * Verify the length and subtract 1132 * the remainder from "frlengths[]": 1133 */ 1134 if (len > xfer->frlengths[xfer->aframes]) { 1135 td->error = 1; 1136 } else { 1137 xfer->frlengths[xfer->aframes] -= len; 1138 } 1139 } 1140 /* Check for transfer error */ 1141 if (td->error) { 1142 /* the transfer is finished */ 1143 error = 1; 1144 td = NULL; 1145 break; 1146 } 1147 /* Check for short transfer */ 1148 if (len > 0) { 1149 if (xfer->flags_int.short_frames_ok || 1150 xfer->flags_int.isochronous_xfr) { 1151 /* follow alt next */ 1152 if (td->alt_next) { 1153 td = td->obj_next; 1154 } else { 1155 td = NULL; 1156 } 1157 } else { 1158 /* the transfer is finished */ 1159 td = NULL; 1160 } 1161 error = 0; 1162 break; 1163 } 1164 td = td->obj_next; 1165 1166 /* this USB frame is complete */ 1167 error = 0; 1168 break; 1169 1170 } while (0); 1171 1172 /* update transfer cache */ 1173 1174 xfer->td_transfer_cache = td; 1175 1176 return (error ? 1177 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION); 1178 } 1179 1180 static void 1181 uss820dci_standard_done(struct usb_xfer *xfer) 1182 { 1183 usb_error_t err = 0; 1184 1185 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n", 1186 xfer, xfer->endpoint); 1187 1188 /* reset scanner */ 1189 1190 xfer->td_transfer_cache = xfer->td_transfer_first; 1191 1192 if (xfer->flags_int.control_xfr) { 1193 if (xfer->flags_int.control_hdr) { 1194 err = uss820dci_standard_done_sub(xfer); 1195 } 1196 xfer->aframes = 1; 1197 1198 if (xfer->td_transfer_cache == NULL) { 1199 goto done; 1200 } 1201 } 1202 while (xfer->aframes != xfer->nframes) { 1203 err = uss820dci_standard_done_sub(xfer); 1204 xfer->aframes++; 1205 1206 if (xfer->td_transfer_cache == NULL) { 1207 goto done; 1208 } 1209 } 1210 1211 if (xfer->flags_int.control_xfr && 1212 !xfer->flags_int.control_act) { 1213 err = uss820dci_standard_done_sub(xfer); 1214 } 1215 done: 1216 uss820dci_device_done(xfer, err); 1217 } 1218 1219 /*------------------------------------------------------------------------* 1220 * uss820dci_device_done 1221 * 1222 * NOTE: this function can be called more than one time on the 1223 * same USB transfer! 1224 *------------------------------------------------------------------------*/ 1225 static void 1226 uss820dci_device_done(struct usb_xfer *xfer, usb_error_t error) 1227 { 1228 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 1229 1230 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1231 1232 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n", 1233 xfer, xfer->endpoint, error); 1234 1235 USB_BUS_SPIN_LOCK(&sc->sc_bus); 1236 1237 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1238 uss820dci_intr_set(xfer, 0); 1239 } 1240 /* dequeue transfer and start next transfer */ 1241 usbd_transfer_done(xfer, error); 1242 1243 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 1244 } 1245 1246 static void 1247 uss820dci_xfer_stall(struct usb_xfer *xfer) 1248 { 1249 uss820dci_device_done(xfer, USB_ERR_STALLED); 1250 } 1251 1252 static void 1253 uss820dci_set_stall(struct usb_device *udev, 1254 struct usb_endpoint *ep, uint8_t *did_stall) 1255 { 1256 struct uss820dci_softc *sc; 1257 uint8_t ep_no; 1258 uint8_t ep_type; 1259 uint8_t ep_dir; 1260 uint8_t temp; 1261 1262 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1263 1264 DPRINTFN(5, "endpoint=%p\n", ep); 1265 1266 /* set FORCESTALL */ 1267 sc = USS820_DCI_BUS2SC(udev->bus); 1268 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR); 1269 ep_dir = (ep->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)); 1270 ep_type = (ep->edesc->bmAttributes & UE_XFERTYPE); 1271 1272 if (ep_type == UE_CONTROL) { 1273 /* should not happen */ 1274 return; 1275 } 1276 USB_BUS_SPIN_LOCK(&sc->sc_bus); 1277 USS820_WRITE_1(sc, USS820_EPINDEX, ep_no); 1278 1279 if (ep_dir == UE_DIR_IN) { 1280 temp = USS820_EPCON_TXSTL; 1281 } else { 1282 temp = USS820_EPCON_RXSTL; 1283 } 1284 uss820dci_update_shared_1(sc, USS820_EPCON, 0xFF, temp); 1285 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 1286 } 1287 1288 static void 1289 uss820dci_clear_stall_sub(struct uss820dci_softc *sc, 1290 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir) 1291 { 1292 uint8_t temp; 1293 1294 if (ep_type == UE_CONTROL) { 1295 /* clearing stall is not needed */ 1296 return; 1297 } 1298 USB_BUS_SPIN_LOCK(&sc->sc_bus); 1299 1300 /* select endpoint index */ 1301 USS820_WRITE_1(sc, USS820_EPINDEX, ep_no); 1302 1303 /* clear stall and disable I/O transfers */ 1304 if (ep_dir == UE_DIR_IN) { 1305 temp = 0xFF ^ (USS820_EPCON_TXOE | 1306 USS820_EPCON_TXSTL); 1307 } else { 1308 temp = 0xFF ^ (USS820_EPCON_RXIE | 1309 USS820_EPCON_RXSTL); 1310 } 1311 uss820dci_update_shared_1(sc, USS820_EPCON, temp, 0); 1312 1313 if (ep_dir == UE_DIR_IN) { 1314 /* reset data toggle */ 1315 USS820_WRITE_1(sc, USS820_TXSTAT, 1316 USS820_TXSTAT_TXSOVW); 1317 1318 /* reset FIFO */ 1319 temp = USS820_READ_1(sc, USS820_TXCON); 1320 temp |= USS820_TXCON_TXCLR; 1321 USS820_WRITE_1(sc, USS820_TXCON, temp); 1322 temp &= ~USS820_TXCON_TXCLR; 1323 USS820_WRITE_1(sc, USS820_TXCON, temp); 1324 } else { 1325 /* reset data toggle */ 1326 uss820dci_update_shared_1(sc, USS820_RXSTAT, 1327 0, USS820_RXSTAT_RXSOVW); 1328 1329 /* reset FIFO */ 1330 temp = USS820_READ_1(sc, USS820_RXCON); 1331 temp |= USS820_RXCON_RXCLR; 1332 temp &= ~USS820_RXCON_RXFFRC; 1333 USS820_WRITE_1(sc, USS820_RXCON, temp); 1334 temp &= ~USS820_RXCON_RXCLR; 1335 USS820_WRITE_1(sc, USS820_RXCON, temp); 1336 } 1337 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 1338 } 1339 1340 static void 1341 uss820dci_clear_stall(struct usb_device *udev, struct usb_endpoint *ep) 1342 { 1343 struct uss820dci_softc *sc; 1344 struct usb_endpoint_descriptor *ed; 1345 1346 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1347 1348 DPRINTFN(5, "endpoint=%p\n", ep); 1349 1350 /* check mode */ 1351 if (udev->flags.usb_mode != USB_MODE_DEVICE) { 1352 /* not supported */ 1353 return; 1354 } 1355 /* get softc */ 1356 sc = USS820_DCI_BUS2SC(udev->bus); 1357 1358 /* get endpoint descriptor */ 1359 ed = ep->edesc; 1360 1361 /* reset endpoint */ 1362 uss820dci_clear_stall_sub(sc, 1363 (ed->bEndpointAddress & UE_ADDR), 1364 (ed->bmAttributes & UE_XFERTYPE), 1365 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT))); 1366 } 1367 1368 usb_error_t 1369 uss820dci_init(struct uss820dci_softc *sc) 1370 { 1371 const struct usb_hw_ep_profile *pf; 1372 uint8_t n; 1373 uint8_t temp; 1374 1375 DPRINTF("start\n"); 1376 1377 /* set up the bus structure */ 1378 sc->sc_bus.usbrev = USB_REV_1_1; 1379 sc->sc_bus.methods = &uss820dci_bus_methods; 1380 1381 USB_BUS_LOCK(&sc->sc_bus); 1382 1383 /* we always have VBUS */ 1384 sc->sc_flags.status_vbus = 1; 1385 1386 /* reset the chip */ 1387 USS820_WRITE_1(sc, USS820_SCR, USS820_SCR_SRESET); 1388 DELAY(100); 1389 USS820_WRITE_1(sc, USS820_SCR, 0); 1390 1391 /* wait for reset to complete */ 1392 for (n = 0;; n++) { 1393 temp = USS820_READ_1(sc, USS820_MCSR); 1394 1395 if (temp & USS820_MCSR_INIT) { 1396 break; 1397 } 1398 if (n == 100) { 1399 USB_BUS_UNLOCK(&sc->sc_bus); 1400 return (USB_ERR_INVAL); 1401 } 1402 /* wait a little for things to stabilise */ 1403 DELAY(100); 1404 } 1405 1406 /* do a pulldown */ 1407 uss820dci_pull_down(sc); 1408 1409 /* wait 10ms for pulldown to stabilise */ 1410 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100); 1411 1412 /* check hardware revision */ 1413 temp = USS820_READ_1(sc, USS820_REV); 1414 1415 if (temp < 0x13) { 1416 USB_BUS_UNLOCK(&sc->sc_bus); 1417 return (USB_ERR_INVAL); 1418 } 1419 /* enable interrupts */ 1420 USS820_WRITE_1(sc, USS820_SCR, 1421 USS820_SCR_T_IRQ | 1422 USS820_SCR_IE_RESET | 1423 /* USS820_SCR_RWUPE | */ 1424 USS820_SCR_IE_SUSP | 1425 USS820_SCR_IRQPOL); 1426 1427 /* enable interrupts */ 1428 USS820_WRITE_1(sc, USS820_SCRATCH, 1429 USS820_SCRATCH_IE_RESUME); 1430 1431 /* enable features */ 1432 USS820_WRITE_1(sc, USS820_MCSR, 1433 USS820_MCSR_BDFEAT | 1434 USS820_MCSR_FEAT); 1435 1436 sc->sc_flags.mcsr_feat = 1; 1437 1438 /* disable interrupts */ 1439 USS820_WRITE_1(sc, USS820_SBIE, 0); 1440 1441 /* disable interrupts */ 1442 USS820_WRITE_1(sc, USS820_SBIE1, 0); 1443 1444 /* disable all endpoints */ 1445 for (n = 0; n != USS820_EP_MAX; n++) { 1446 /* select endpoint */ 1447 USS820_WRITE_1(sc, USS820_EPINDEX, n); 1448 1449 /* disable endpoint */ 1450 uss820dci_update_shared_1(sc, USS820_EPCON, 0, 0); 1451 } 1452 1453 /* 1454 * Initialise default values for some registers that cannot be 1455 * changed during operation! 1456 */ 1457 for (n = 0; n != USS820_EP_MAX; n++) { 1458 uss820dci_get_hw_ep_profile(NULL, &pf, n); 1459 1460 /* the maximum frame sizes should be the same */ 1461 if (pf->max_in_frame_size != pf->max_out_frame_size) { 1462 DPRINTF("Max frame size mismatch %u != %u\n", 1463 pf->max_in_frame_size, pf->max_out_frame_size); 1464 } 1465 if (pf->support_isochronous) { 1466 if (pf->max_in_frame_size <= 64) { 1467 temp = (USS820_TXCON_FFSZ_16_64 | 1468 USS820_TXCON_TXISO | 1469 USS820_TXCON_ATM); 1470 } else if (pf->max_in_frame_size <= 256) { 1471 temp = (USS820_TXCON_FFSZ_64_256 | 1472 USS820_TXCON_TXISO | 1473 USS820_TXCON_ATM); 1474 } else if (pf->max_in_frame_size <= 512) { 1475 temp = (USS820_TXCON_FFSZ_8_512 | 1476 USS820_TXCON_TXISO | 1477 USS820_TXCON_ATM); 1478 } else { /* 1024 bytes */ 1479 temp = (USS820_TXCON_FFSZ_32_1024 | 1480 USS820_TXCON_TXISO | 1481 USS820_TXCON_ATM); 1482 } 1483 } else { 1484 if ((pf->max_in_frame_size <= 8) && 1485 (sc->sc_flags.mcsr_feat)) { 1486 temp = (USS820_TXCON_FFSZ_8_512 | 1487 USS820_TXCON_ATM); 1488 } else if (pf->max_in_frame_size <= 16) { 1489 temp = (USS820_TXCON_FFSZ_16_64 | 1490 USS820_TXCON_ATM); 1491 } else if ((pf->max_in_frame_size <= 32) && 1492 (sc->sc_flags.mcsr_feat)) { 1493 temp = (USS820_TXCON_FFSZ_32_1024 | 1494 USS820_TXCON_ATM); 1495 } else { /* 64 bytes */ 1496 temp = (USS820_TXCON_FFSZ_64_256 | 1497 USS820_TXCON_ATM); 1498 } 1499 } 1500 1501 /* need to configure the chip early */ 1502 1503 USS820_WRITE_1(sc, USS820_EPINDEX, n); 1504 USS820_WRITE_1(sc, USS820_TXCON, temp); 1505 USS820_WRITE_1(sc, USS820_RXCON, temp); 1506 1507 if (pf->support_control) { 1508 temp = USS820_EPCON_CTLEP | 1509 USS820_EPCON_RXSPM | 1510 USS820_EPCON_RXIE | 1511 USS820_EPCON_RXEPEN | 1512 USS820_EPCON_TXOE | 1513 USS820_EPCON_TXEPEN; 1514 } else { 1515 temp = USS820_EPCON_RXEPEN | USS820_EPCON_TXEPEN; 1516 } 1517 1518 uss820dci_update_shared_1(sc, USS820_EPCON, 0, temp); 1519 } 1520 1521 USB_BUS_UNLOCK(&sc->sc_bus); 1522 1523 /* catch any lost interrupts */ 1524 1525 uss820dci_do_poll(&sc->sc_bus); 1526 1527 return (0); /* success */ 1528 } 1529 1530 void 1531 uss820dci_uninit(struct uss820dci_softc *sc) 1532 { 1533 uint8_t temp; 1534 1535 USB_BUS_LOCK(&sc->sc_bus); 1536 1537 /* disable all interrupts */ 1538 temp = USS820_READ_1(sc, USS820_SCR); 1539 temp &= ~USS820_SCR_T_IRQ; 1540 USS820_WRITE_1(sc, USS820_SCR, temp); 1541 1542 sc->sc_flags.port_powered = 0; 1543 sc->sc_flags.status_vbus = 0; 1544 sc->sc_flags.status_bus_reset = 0; 1545 sc->sc_flags.status_suspend = 0; 1546 sc->sc_flags.change_suspend = 0; 1547 sc->sc_flags.change_connect = 1; 1548 1549 uss820dci_pull_down(sc); 1550 USB_BUS_UNLOCK(&sc->sc_bus); 1551 } 1552 1553 static void 1554 uss820dci_suspend(struct uss820dci_softc *sc) 1555 { 1556 /* TODO */ 1557 } 1558 1559 static void 1560 uss820dci_resume(struct uss820dci_softc *sc) 1561 { 1562 /* TODO */ 1563 } 1564 1565 static void 1566 uss820dci_do_poll(struct usb_bus *bus) 1567 { 1568 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(bus); 1569 1570 USB_BUS_LOCK(&sc->sc_bus); 1571 USB_BUS_SPIN_LOCK(&sc->sc_bus); 1572 uss820dci_interrupt_poll_locked(sc); 1573 uss820dci_interrupt_complete_locked(sc); 1574 USB_BUS_SPIN_UNLOCK(&sc->sc_bus); 1575 USB_BUS_UNLOCK(&sc->sc_bus); 1576 } 1577 1578 /*------------------------------------------------------------------------* 1579 * uss820dci bulk support 1580 *------------------------------------------------------------------------*/ 1581 static void 1582 uss820dci_device_bulk_open(struct usb_xfer *xfer) 1583 { 1584 return; 1585 } 1586 1587 static void 1588 uss820dci_device_bulk_close(struct usb_xfer *xfer) 1589 { 1590 uss820dci_device_done(xfer, USB_ERR_CANCELLED); 1591 } 1592 1593 static void 1594 uss820dci_device_bulk_enter(struct usb_xfer *xfer) 1595 { 1596 return; 1597 } 1598 1599 static void 1600 uss820dci_device_bulk_start(struct usb_xfer *xfer) 1601 { 1602 /* setup TDs */ 1603 uss820dci_setup_standard_chain(xfer); 1604 uss820dci_start_standard_chain(xfer); 1605 } 1606 1607 static const struct usb_pipe_methods uss820dci_device_bulk_methods = 1608 { 1609 .open = uss820dci_device_bulk_open, 1610 .close = uss820dci_device_bulk_close, 1611 .enter = uss820dci_device_bulk_enter, 1612 .start = uss820dci_device_bulk_start, 1613 }; 1614 1615 /*------------------------------------------------------------------------* 1616 * uss820dci control support 1617 *------------------------------------------------------------------------*/ 1618 static void 1619 uss820dci_device_ctrl_open(struct usb_xfer *xfer) 1620 { 1621 return; 1622 } 1623 1624 static void 1625 uss820dci_device_ctrl_close(struct usb_xfer *xfer) 1626 { 1627 uss820dci_device_done(xfer, USB_ERR_CANCELLED); 1628 } 1629 1630 static void 1631 uss820dci_device_ctrl_enter(struct usb_xfer *xfer) 1632 { 1633 return; 1634 } 1635 1636 static void 1637 uss820dci_device_ctrl_start(struct usb_xfer *xfer) 1638 { 1639 /* setup TDs */ 1640 uss820dci_setup_standard_chain(xfer); 1641 uss820dci_start_standard_chain(xfer); 1642 } 1643 1644 static const struct usb_pipe_methods uss820dci_device_ctrl_methods = 1645 { 1646 .open = uss820dci_device_ctrl_open, 1647 .close = uss820dci_device_ctrl_close, 1648 .enter = uss820dci_device_ctrl_enter, 1649 .start = uss820dci_device_ctrl_start, 1650 }; 1651 1652 /*------------------------------------------------------------------------* 1653 * uss820dci interrupt support 1654 *------------------------------------------------------------------------*/ 1655 static void 1656 uss820dci_device_intr_open(struct usb_xfer *xfer) 1657 { 1658 return; 1659 } 1660 1661 static void 1662 uss820dci_device_intr_close(struct usb_xfer *xfer) 1663 { 1664 uss820dci_device_done(xfer, USB_ERR_CANCELLED); 1665 } 1666 1667 static void 1668 uss820dci_device_intr_enter(struct usb_xfer *xfer) 1669 { 1670 return; 1671 } 1672 1673 static void 1674 uss820dci_device_intr_start(struct usb_xfer *xfer) 1675 { 1676 /* setup TDs */ 1677 uss820dci_setup_standard_chain(xfer); 1678 uss820dci_start_standard_chain(xfer); 1679 } 1680 1681 static const struct usb_pipe_methods uss820dci_device_intr_methods = 1682 { 1683 .open = uss820dci_device_intr_open, 1684 .close = uss820dci_device_intr_close, 1685 .enter = uss820dci_device_intr_enter, 1686 .start = uss820dci_device_intr_start, 1687 }; 1688 1689 /*------------------------------------------------------------------------* 1690 * uss820dci full speed isochronous support 1691 *------------------------------------------------------------------------*/ 1692 static void 1693 uss820dci_device_isoc_fs_open(struct usb_xfer *xfer) 1694 { 1695 return; 1696 } 1697 1698 static void 1699 uss820dci_device_isoc_fs_close(struct usb_xfer *xfer) 1700 { 1701 uss820dci_device_done(xfer, USB_ERR_CANCELLED); 1702 } 1703 1704 static void 1705 uss820dci_device_isoc_fs_enter(struct usb_xfer *xfer) 1706 { 1707 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus); 1708 uint32_t temp; 1709 uint32_t nframes; 1710 1711 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n", 1712 xfer, xfer->endpoint->isoc_next, xfer->nframes); 1713 1714 /* get the current frame index - we don't need the high bits */ 1715 1716 nframes = USS820_READ_1(sc, USS820_SOFL); 1717 1718 /* 1719 * check if the frame index is within the window where the 1720 * frames will be inserted 1721 */ 1722 temp = (nframes - xfer->endpoint->isoc_next) & USS820_SOFL_MASK; 1723 1724 if ((xfer->endpoint->is_synced == 0) || 1725 (temp < xfer->nframes)) { 1726 /* 1727 * If there is data underflow or the pipe queue is 1728 * empty we schedule the transfer a few frames ahead 1729 * of the current frame position. Else two isochronous 1730 * transfers might overlap. 1731 */ 1732 xfer->endpoint->isoc_next = (nframes + 3) & USS820_SOFL_MASK; 1733 xfer->endpoint->is_synced = 1; 1734 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next); 1735 } 1736 /* 1737 * compute how many milliseconds the insertion is ahead of the 1738 * current frame position: 1739 */ 1740 temp = (xfer->endpoint->isoc_next - nframes) & USS820_SOFL_MASK; 1741 1742 /* 1743 * pre-compute when the isochronous transfer will be finished: 1744 */ 1745 xfer->isoc_time_complete = 1746 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp + 1747 xfer->nframes; 1748 1749 /* compute frame number for next insertion */ 1750 xfer->endpoint->isoc_next += xfer->nframes; 1751 1752 /* setup TDs */ 1753 uss820dci_setup_standard_chain(xfer); 1754 } 1755 1756 static void 1757 uss820dci_device_isoc_fs_start(struct usb_xfer *xfer) 1758 { 1759 /* start TD chain */ 1760 uss820dci_start_standard_chain(xfer); 1761 } 1762 1763 static const struct usb_pipe_methods uss820dci_device_isoc_fs_methods = 1764 { 1765 .open = uss820dci_device_isoc_fs_open, 1766 .close = uss820dci_device_isoc_fs_close, 1767 .enter = uss820dci_device_isoc_fs_enter, 1768 .start = uss820dci_device_isoc_fs_start, 1769 }; 1770 1771 /*------------------------------------------------------------------------* 1772 * uss820dci root control support 1773 *------------------------------------------------------------------------* 1774 * Simulate a hardware HUB by handling all the necessary requests. 1775 *------------------------------------------------------------------------*/ 1776 1777 static const struct usb_device_descriptor uss820dci_devd = { 1778 .bLength = sizeof(struct usb_device_descriptor), 1779 .bDescriptorType = UDESC_DEVICE, 1780 .bcdUSB = {0x00, 0x02}, 1781 .bDeviceClass = UDCLASS_HUB, 1782 .bDeviceSubClass = UDSUBCLASS_HUB, 1783 .bDeviceProtocol = UDPROTO_FSHUB, 1784 .bMaxPacketSize = 64, 1785 .bcdDevice = {0x00, 0x01}, 1786 .iManufacturer = 1, 1787 .iProduct = 2, 1788 .bNumConfigurations = 1, 1789 }; 1790 1791 static const struct usb_device_qualifier uss820dci_odevd = { 1792 .bLength = sizeof(struct usb_device_qualifier), 1793 .bDescriptorType = UDESC_DEVICE_QUALIFIER, 1794 .bcdUSB = {0x00, 0x02}, 1795 .bDeviceClass = UDCLASS_HUB, 1796 .bDeviceSubClass = UDSUBCLASS_HUB, 1797 .bDeviceProtocol = UDPROTO_FSHUB, 1798 .bMaxPacketSize0 = 0, 1799 .bNumConfigurations = 0, 1800 }; 1801 1802 static const struct uss820dci_config_desc uss820dci_confd = { 1803 .confd = { 1804 .bLength = sizeof(struct usb_config_descriptor), 1805 .bDescriptorType = UDESC_CONFIG, 1806 .wTotalLength[0] = sizeof(uss820dci_confd), 1807 .bNumInterface = 1, 1808 .bConfigurationValue = 1, 1809 .iConfiguration = 0, 1810 .bmAttributes = UC_SELF_POWERED, 1811 .bMaxPower = 0, 1812 }, 1813 .ifcd = { 1814 .bLength = sizeof(struct usb_interface_descriptor), 1815 .bDescriptorType = UDESC_INTERFACE, 1816 .bNumEndpoints = 1, 1817 .bInterfaceClass = UICLASS_HUB, 1818 .bInterfaceSubClass = UISUBCLASS_HUB, 1819 .bInterfaceProtocol = 0, 1820 }, 1821 1822 .endpd = { 1823 .bLength = sizeof(struct usb_endpoint_descriptor), 1824 .bDescriptorType = UDESC_ENDPOINT, 1825 .bEndpointAddress = (UE_DIR_IN | USS820_DCI_INTR_ENDPT), 1826 .bmAttributes = UE_INTERRUPT, 1827 .wMaxPacketSize[0] = 8, 1828 .bInterval = 255, 1829 }, 1830 }; 1831 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) } 1832 1833 static const struct usb_hub_descriptor_min uss820dci_hubd = { 1834 .bDescLength = sizeof(uss820dci_hubd), 1835 .bDescriptorType = UDESC_HUB, 1836 .bNbrPorts = 1, 1837 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)), 1838 .bPwrOn2PwrGood = 50, 1839 .bHubContrCurrent = 0, 1840 .DeviceRemovable = {0}, /* port is removable */ 1841 }; 1842 1843 #define STRING_VENDOR \ 1844 "A\0G\0E\0R\0E" 1845 1846 #define STRING_PRODUCT \ 1847 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B" 1848 1849 USB_MAKE_STRING_DESC(STRING_VENDOR, uss820dci_vendor); 1850 USB_MAKE_STRING_DESC(STRING_PRODUCT, uss820dci_product); 1851 1852 static usb_error_t 1853 uss820dci_roothub_exec(struct usb_device *udev, 1854 struct usb_device_request *req, const void **pptr, uint16_t *plength) 1855 { 1856 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(udev->bus); 1857 const void *ptr; 1858 uint16_t len; 1859 uint16_t value; 1860 uint16_t index; 1861 usb_error_t err; 1862 1863 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1864 1865 /* buffer reset */ 1866 ptr = (const void *)&sc->sc_hub_temp; 1867 len = 0; 1868 err = 0; 1869 1870 value = UGETW(req->wValue); 1871 index = UGETW(req->wIndex); 1872 1873 /* demultiplex the control request */ 1874 1875 switch (req->bmRequestType) { 1876 case UT_READ_DEVICE: 1877 switch (req->bRequest) { 1878 case UR_GET_DESCRIPTOR: 1879 goto tr_handle_get_descriptor; 1880 case UR_GET_CONFIG: 1881 goto tr_handle_get_config; 1882 case UR_GET_STATUS: 1883 goto tr_handle_get_status; 1884 default: 1885 goto tr_stalled; 1886 } 1887 break; 1888 1889 case UT_WRITE_DEVICE: 1890 switch (req->bRequest) { 1891 case UR_SET_ADDRESS: 1892 goto tr_handle_set_address; 1893 case UR_SET_CONFIG: 1894 goto tr_handle_set_config; 1895 case UR_CLEAR_FEATURE: 1896 goto tr_valid; /* nop */ 1897 case UR_SET_DESCRIPTOR: 1898 goto tr_valid; /* nop */ 1899 case UR_SET_FEATURE: 1900 default: 1901 goto tr_stalled; 1902 } 1903 break; 1904 1905 case UT_WRITE_ENDPOINT: 1906 switch (req->bRequest) { 1907 case UR_CLEAR_FEATURE: 1908 switch (UGETW(req->wValue)) { 1909 case UF_ENDPOINT_HALT: 1910 goto tr_handle_clear_halt; 1911 case UF_DEVICE_REMOTE_WAKEUP: 1912 goto tr_handle_clear_wakeup; 1913 default: 1914 goto tr_stalled; 1915 } 1916 break; 1917 case UR_SET_FEATURE: 1918 switch (UGETW(req->wValue)) { 1919 case UF_ENDPOINT_HALT: 1920 goto tr_handle_set_halt; 1921 case UF_DEVICE_REMOTE_WAKEUP: 1922 goto tr_handle_set_wakeup; 1923 default: 1924 goto tr_stalled; 1925 } 1926 break; 1927 case UR_SYNCH_FRAME: 1928 goto tr_valid; /* nop */ 1929 default: 1930 goto tr_stalled; 1931 } 1932 break; 1933 1934 case UT_READ_ENDPOINT: 1935 switch (req->bRequest) { 1936 case UR_GET_STATUS: 1937 goto tr_handle_get_ep_status; 1938 default: 1939 goto tr_stalled; 1940 } 1941 break; 1942 1943 case UT_WRITE_INTERFACE: 1944 switch (req->bRequest) { 1945 case UR_SET_INTERFACE: 1946 goto tr_handle_set_interface; 1947 case UR_CLEAR_FEATURE: 1948 goto tr_valid; /* nop */ 1949 case UR_SET_FEATURE: 1950 default: 1951 goto tr_stalled; 1952 } 1953 break; 1954 1955 case UT_READ_INTERFACE: 1956 switch (req->bRequest) { 1957 case UR_GET_INTERFACE: 1958 goto tr_handle_get_interface; 1959 case UR_GET_STATUS: 1960 goto tr_handle_get_iface_status; 1961 default: 1962 goto tr_stalled; 1963 } 1964 break; 1965 1966 case UT_WRITE_CLASS_INTERFACE: 1967 case UT_WRITE_VENDOR_INTERFACE: 1968 /* XXX forward */ 1969 break; 1970 1971 case UT_READ_CLASS_INTERFACE: 1972 case UT_READ_VENDOR_INTERFACE: 1973 /* XXX forward */ 1974 break; 1975 1976 case UT_WRITE_CLASS_DEVICE: 1977 switch (req->bRequest) { 1978 case UR_CLEAR_FEATURE: 1979 goto tr_valid; 1980 case UR_SET_DESCRIPTOR: 1981 case UR_SET_FEATURE: 1982 break; 1983 default: 1984 goto tr_stalled; 1985 } 1986 break; 1987 1988 case UT_WRITE_CLASS_OTHER: 1989 switch (req->bRequest) { 1990 case UR_CLEAR_FEATURE: 1991 goto tr_handle_clear_port_feature; 1992 case UR_SET_FEATURE: 1993 goto tr_handle_set_port_feature; 1994 case UR_CLEAR_TT_BUFFER: 1995 case UR_RESET_TT: 1996 case UR_STOP_TT: 1997 goto tr_valid; 1998 1999 default: 2000 goto tr_stalled; 2001 } 2002 break; 2003 2004 case UT_READ_CLASS_OTHER: 2005 switch (req->bRequest) { 2006 case UR_GET_TT_STATE: 2007 goto tr_handle_get_tt_state; 2008 case UR_GET_STATUS: 2009 goto tr_handle_get_port_status; 2010 default: 2011 goto tr_stalled; 2012 } 2013 break; 2014 2015 case UT_READ_CLASS_DEVICE: 2016 switch (req->bRequest) { 2017 case UR_GET_DESCRIPTOR: 2018 goto tr_handle_get_class_descriptor; 2019 case UR_GET_STATUS: 2020 goto tr_handle_get_class_status; 2021 2022 default: 2023 goto tr_stalled; 2024 } 2025 break; 2026 default: 2027 goto tr_stalled; 2028 } 2029 goto tr_valid; 2030 2031 tr_handle_get_descriptor: 2032 switch (value >> 8) { 2033 case UDESC_DEVICE: 2034 if (value & 0xff) { 2035 goto tr_stalled; 2036 } 2037 len = sizeof(uss820dci_devd); 2038 ptr = (const void *)&uss820dci_devd; 2039 goto tr_valid; 2040 case UDESC_DEVICE_QUALIFIER: 2041 if (value & 0xff) { 2042 goto tr_stalled; 2043 } 2044 len = sizeof(uss820dci_odevd); 2045 ptr = (const void *)&uss820dci_odevd; 2046 goto tr_valid; 2047 case UDESC_CONFIG: 2048 if (value & 0xff) { 2049 goto tr_stalled; 2050 } 2051 len = sizeof(uss820dci_confd); 2052 ptr = (const void *)&uss820dci_confd; 2053 goto tr_valid; 2054 case UDESC_STRING: 2055 switch (value & 0xff) { 2056 case 0: /* Language table */ 2057 len = sizeof(usb_string_lang_en); 2058 ptr = (const void *)&usb_string_lang_en; 2059 goto tr_valid; 2060 2061 case 1: /* Vendor */ 2062 len = sizeof(uss820dci_vendor); 2063 ptr = (const void *)&uss820dci_vendor; 2064 goto tr_valid; 2065 2066 case 2: /* Product */ 2067 len = sizeof(uss820dci_product); 2068 ptr = (const void *)&uss820dci_product; 2069 goto tr_valid; 2070 default: 2071 break; 2072 } 2073 break; 2074 default: 2075 goto tr_stalled; 2076 } 2077 goto tr_stalled; 2078 2079 tr_handle_get_config: 2080 len = 1; 2081 sc->sc_hub_temp.wValue[0] = sc->sc_conf; 2082 goto tr_valid; 2083 2084 tr_handle_get_status: 2085 len = 2; 2086 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED); 2087 goto tr_valid; 2088 2089 tr_handle_set_address: 2090 if (value & 0xFF00) { 2091 goto tr_stalled; 2092 } 2093 sc->sc_rt_addr = value; 2094 goto tr_valid; 2095 2096 tr_handle_set_config: 2097 if (value >= 2) { 2098 goto tr_stalled; 2099 } 2100 sc->sc_conf = value; 2101 goto tr_valid; 2102 2103 tr_handle_get_interface: 2104 len = 1; 2105 sc->sc_hub_temp.wValue[0] = 0; 2106 goto tr_valid; 2107 2108 tr_handle_get_tt_state: 2109 tr_handle_get_class_status: 2110 tr_handle_get_iface_status: 2111 tr_handle_get_ep_status: 2112 len = 2; 2113 USETW(sc->sc_hub_temp.wValue, 0); 2114 goto tr_valid; 2115 2116 tr_handle_set_halt: 2117 tr_handle_set_interface: 2118 tr_handle_set_wakeup: 2119 tr_handle_clear_wakeup: 2120 tr_handle_clear_halt: 2121 goto tr_valid; 2122 2123 tr_handle_clear_port_feature: 2124 if (index != 1) { 2125 goto tr_stalled; 2126 } 2127 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index); 2128 2129 switch (value) { 2130 case UHF_PORT_SUSPEND: 2131 uss820dci_wakeup_peer(sc); 2132 break; 2133 2134 case UHF_PORT_ENABLE: 2135 sc->sc_flags.port_enabled = 0; 2136 break; 2137 2138 case UHF_PORT_TEST: 2139 case UHF_PORT_INDICATOR: 2140 case UHF_C_PORT_ENABLE: 2141 case UHF_C_PORT_OVER_CURRENT: 2142 case UHF_C_PORT_RESET: 2143 /* nops */ 2144 break; 2145 case UHF_PORT_POWER: 2146 sc->sc_flags.port_powered = 0; 2147 uss820dci_pull_down(sc); 2148 break; 2149 case UHF_C_PORT_CONNECTION: 2150 sc->sc_flags.change_connect = 0; 2151 break; 2152 case UHF_C_PORT_SUSPEND: 2153 sc->sc_flags.change_suspend = 0; 2154 break; 2155 default: 2156 err = USB_ERR_IOERROR; 2157 goto done; 2158 } 2159 goto tr_valid; 2160 2161 tr_handle_set_port_feature: 2162 if (index != 1) { 2163 goto tr_stalled; 2164 } 2165 DPRINTFN(9, "UR_SET_PORT_FEATURE\n"); 2166 2167 switch (value) { 2168 case UHF_PORT_ENABLE: 2169 sc->sc_flags.port_enabled = 1; 2170 break; 2171 case UHF_PORT_SUSPEND: 2172 case UHF_PORT_RESET: 2173 case UHF_PORT_TEST: 2174 case UHF_PORT_INDICATOR: 2175 /* nops */ 2176 break; 2177 case UHF_PORT_POWER: 2178 sc->sc_flags.port_powered = 1; 2179 break; 2180 default: 2181 err = USB_ERR_IOERROR; 2182 goto done; 2183 } 2184 goto tr_valid; 2185 2186 tr_handle_get_port_status: 2187 2188 DPRINTFN(9, "UR_GET_PORT_STATUS\n"); 2189 2190 if (index != 1) { 2191 goto tr_stalled; 2192 } 2193 if (sc->sc_flags.status_vbus) { 2194 uss820dci_pull_up(sc); 2195 } else { 2196 uss820dci_pull_down(sc); 2197 } 2198 2199 /* Select FULL-speed and Device Side Mode */ 2200 2201 value = UPS_PORT_MODE_DEVICE; 2202 2203 if (sc->sc_flags.port_powered) { 2204 value |= UPS_PORT_POWER; 2205 } 2206 if (sc->sc_flags.port_enabled) { 2207 value |= UPS_PORT_ENABLED; 2208 } 2209 if (sc->sc_flags.status_vbus && 2210 sc->sc_flags.status_bus_reset) { 2211 value |= UPS_CURRENT_CONNECT_STATUS; 2212 } 2213 if (sc->sc_flags.status_suspend) { 2214 value |= UPS_SUSPEND; 2215 } 2216 USETW(sc->sc_hub_temp.ps.wPortStatus, value); 2217 2218 value = 0; 2219 2220 if (sc->sc_flags.change_connect) { 2221 value |= UPS_C_CONNECT_STATUS; 2222 } 2223 if (sc->sc_flags.change_suspend) { 2224 value |= UPS_C_SUSPEND; 2225 } 2226 USETW(sc->sc_hub_temp.ps.wPortChange, value); 2227 len = sizeof(sc->sc_hub_temp.ps); 2228 goto tr_valid; 2229 2230 tr_handle_get_class_descriptor: 2231 if (value & 0xFF) { 2232 goto tr_stalled; 2233 } 2234 ptr = (const void *)&uss820dci_hubd; 2235 len = sizeof(uss820dci_hubd); 2236 goto tr_valid; 2237 2238 tr_stalled: 2239 err = USB_ERR_STALLED; 2240 tr_valid: 2241 done: 2242 *plength = len; 2243 *pptr = ptr; 2244 return (err); 2245 } 2246 2247 static void 2248 uss820dci_xfer_setup(struct usb_setup_params *parm) 2249 { 2250 const struct usb_hw_ep_profile *pf; 2251 struct uss820dci_softc *sc; 2252 struct usb_xfer *xfer; 2253 void *last_obj; 2254 uint32_t ntd; 2255 uint32_t n; 2256 uint8_t ep_no; 2257 2258 sc = USS820_DCI_BUS2SC(parm->udev->bus); 2259 xfer = parm->curr_xfer; 2260 2261 /* 2262 * NOTE: This driver does not use any of the parameters that 2263 * are computed from the following values. Just set some 2264 * reasonable dummies: 2265 */ 2266 parm->hc_max_packet_size = 0x500; 2267 parm->hc_max_packet_count = 1; 2268 parm->hc_max_frame_size = 0x500; 2269 2270 usbd_transfer_setup_sub(parm); 2271 2272 /* 2273 * compute maximum number of TDs 2274 */ 2275 if (parm->methods == &uss820dci_device_ctrl_methods) { 2276 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ; 2277 2278 } else if (parm->methods == &uss820dci_device_bulk_methods) { 2279 ntd = xfer->nframes + 1 /* SYNC */ ; 2280 2281 } else if (parm->methods == &uss820dci_device_intr_methods) { 2282 ntd = xfer->nframes + 1 /* SYNC */ ; 2283 2284 } else if (parm->methods == &uss820dci_device_isoc_fs_methods) { 2285 ntd = xfer->nframes + 1 /* SYNC */ ; 2286 2287 } else { 2288 ntd = 0; 2289 } 2290 2291 /* 2292 * check if "usbd_transfer_setup_sub" set an error 2293 */ 2294 if (parm->err) { 2295 return; 2296 } 2297 /* 2298 * allocate transfer descriptors 2299 */ 2300 last_obj = NULL; 2301 2302 /* 2303 * get profile stuff 2304 */ 2305 if (ntd) { 2306 ep_no = xfer->endpointno & UE_ADDR; 2307 uss820dci_get_hw_ep_profile(parm->udev, &pf, ep_no); 2308 2309 if (pf == NULL) { 2310 /* should not happen */ 2311 parm->err = USB_ERR_INVAL; 2312 return; 2313 } 2314 } else { 2315 ep_no = 0; 2316 pf = NULL; 2317 } 2318 2319 /* align data */ 2320 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 2321 2322 for (n = 0; n != ntd; n++) { 2323 struct uss820dci_td *td; 2324 2325 if (parm->buf) { 2326 td = USB_ADD_BYTES(parm->buf, parm->size[0]); 2327 2328 /* init TD */ 2329 td->max_packet_size = xfer->max_packet_size; 2330 td->ep_index = ep_no; 2331 if (pf->support_multi_buffer && 2332 (parm->methods != &uss820dci_device_ctrl_methods)) { 2333 td->support_multi_buffer = 1; 2334 } 2335 td->obj_next = last_obj; 2336 2337 last_obj = td; 2338 } 2339 parm->size[0] += sizeof(*td); 2340 } 2341 2342 xfer->td_start[0] = last_obj; 2343 } 2344 2345 static void 2346 uss820dci_xfer_unsetup(struct usb_xfer *xfer) 2347 { 2348 return; 2349 } 2350 2351 static void 2352 uss820dci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc, 2353 struct usb_endpoint *ep) 2354 { 2355 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(udev->bus); 2356 2357 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 2358 ep, udev->address, 2359 edesc->bEndpointAddress, udev->flags.usb_mode, 2360 sc->sc_rt_addr); 2361 2362 if (udev->device_index != sc->sc_rt_addr) { 2363 if (udev->speed != USB_SPEED_FULL) { 2364 /* not supported */ 2365 return; 2366 } 2367 switch (edesc->bmAttributes & UE_XFERTYPE) { 2368 case UE_CONTROL: 2369 ep->methods = &uss820dci_device_ctrl_methods; 2370 break; 2371 case UE_INTERRUPT: 2372 ep->methods = &uss820dci_device_intr_methods; 2373 break; 2374 case UE_ISOCHRONOUS: 2375 ep->methods = &uss820dci_device_isoc_fs_methods; 2376 break; 2377 case UE_BULK: 2378 ep->methods = &uss820dci_device_bulk_methods; 2379 break; 2380 default: 2381 /* do nothing */ 2382 break; 2383 } 2384 } 2385 } 2386 2387 static void 2388 uss820dci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state) 2389 { 2390 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(bus); 2391 2392 switch (state) { 2393 case USB_HW_POWER_SUSPEND: 2394 uss820dci_suspend(sc); 2395 break; 2396 case USB_HW_POWER_SHUTDOWN: 2397 uss820dci_uninit(sc); 2398 break; 2399 case USB_HW_POWER_RESUME: 2400 uss820dci_resume(sc); 2401 break; 2402 default: 2403 break; 2404 } 2405 } 2406 2407 static const struct usb_bus_methods uss820dci_bus_methods = 2408 { 2409 .endpoint_init = &uss820dci_ep_init, 2410 .xfer_setup = &uss820dci_xfer_setup, 2411 .xfer_unsetup = &uss820dci_xfer_unsetup, 2412 .get_hw_ep_profile = &uss820dci_get_hw_ep_profile, 2413 .xfer_stall = &uss820dci_xfer_stall, 2414 .set_stall = &uss820dci_set_stall, 2415 .clear_stall = &uss820dci_clear_stall, 2416 .roothub_exec = &uss820dci_roothub_exec, 2417 .xfer_poll = &uss820dci_do_poll, 2418 .set_hw_power_sleep = uss820dci_set_hw_power_sleep, 2419 }; 2420