3 * Copyright (c) 2008 Hans Petter Selasky <hselasky@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the driver for the USS820 series USB Device
32 * NOTE: The datasheet does not document everything.
35 #include <dev/usb/usb.h>
36 #include <dev/usb/usb_mfunc.h>
37 #include <dev/usb/usb_revision.h>
38 #include <dev/usb/usb_error.h>
40 #define USB_DEBUG_VAR uss820dcidebug
42 #include <dev/usb/usb_core.h>
43 #include <dev/usb/usb_debug.h>
44 #include <dev/usb/usb_busdma.h>
45 #include <dev/usb/usb_process.h>
46 #include <dev/usb/usb_transfer.h>
47 #include <dev/usb/usb_device.h>
48 #include <dev/usb/usb_hub.h>
49 #include <dev/usb/usb_util.h>
51 #include <dev/usb/usb_controller.h>
52 #include <dev/usb/usb_bus.h>
53 #include <dev/usb/controller/uss820dci.h>
55 #define USS820_DCI_BUS2SC(bus) \
56 ((struct uss820dci_softc *)(((uint8_t *)(bus)) - \
57 ((uint8_t *)&(((struct uss820dci_softc *)0)->sc_bus))))
59 #define USS820_DCI_PC2SC(pc) \
60 USS820_DCI_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
63 static int uss820dcidebug = 0;
65 SYSCTL_NODE(_hw_usb, OID_AUTO, uss820dci, CTLFLAG_RW, 0, "USB uss820dci");
66 SYSCTL_INT(_hw_usb_uss820dci, OID_AUTO, debug, CTLFLAG_RW,
67 &uss820dcidebug, 0, "uss820dci debug level");
70 #define USS820_DCI_INTR_ENDPT 1
74 struct usb2_bus_methods uss820dci_bus_methods;
75 struct usb2_pipe_methods uss820dci_device_bulk_methods;
76 struct usb2_pipe_methods uss820dci_device_ctrl_methods;
77 struct usb2_pipe_methods uss820dci_device_intr_methods;
78 struct usb2_pipe_methods uss820dci_device_isoc_fs_methods;
80 static uss820dci_cmd_t uss820dci_setup_rx;
81 static uss820dci_cmd_t uss820dci_data_rx;
82 static uss820dci_cmd_t uss820dci_data_tx;
83 static uss820dci_cmd_t uss820dci_data_tx_sync;
84 static void uss820dci_device_done(struct usb2_xfer *, usb2_error_t);
85 static void uss820dci_do_poll(struct usb2_bus *);
86 static void uss820dci_standard_done(struct usb2_xfer *);
87 static void uss820dci_intr_set(struct usb2_xfer *, uint8_t);
88 static void uss820dci_update_shared_1(struct uss820dci_softc *, uint8_t,
90 static void uss820dci_root_intr(struct uss820dci_softc *);
93 * Here is a list of what the USS820D chip can support. The main
94 * limitation is that the sum of the buffer sizes must be less than
97 static const struct usb2_hw_ep_profile
98 uss820dci_ep_profile[] = {
101 .max_in_frame_size = 32,
102 .max_out_frame_size = 32,
104 .support_control = 1,
107 .max_in_frame_size = 64,
108 .max_out_frame_size = 64,
110 .support_multi_buffer = 1,
112 .support_interrupt = 1,
117 .max_in_frame_size = 8,
118 .max_out_frame_size = 8,
120 .support_multi_buffer = 1,
122 .support_interrupt = 1,
127 .max_in_frame_size = 256,
128 .max_out_frame_size = 256,
130 .support_multi_buffer = 1,
131 .support_isochronous = 1,
138 uss820dci_update_shared_1(struct uss820dci_softc *sc, uint8_t reg,
139 uint8_t keep_mask, uint8_t set_mask)
143 USS820_WRITE_1(sc, USS820_PEND, 1);
144 temp = USS820_READ_1(sc, reg);
147 USS820_WRITE_1(sc, reg, temp);
148 USS820_WRITE_1(sc, USS820_PEND, 0);
152 uss820dci_get_hw_ep_profile(struct usb2_device *udev,
153 const struct usb2_hw_ep_profile **ppf, uint8_t ep_addr)
156 *ppf = uss820dci_ep_profile + 0;
157 } else if (ep_addr < 5) {
158 *ppf = uss820dci_ep_profile + 1;
159 } else if (ep_addr < 7) {
160 *ppf = uss820dci_ep_profile + 2;
161 } else if (ep_addr == 7) {
162 *ppf = uss820dci_ep_profile + 3;
169 uss820dci_pull_up(struct uss820dci_softc *sc)
173 /* pullup D+, if possible */
175 if (!sc->sc_flags.d_pulled_up &&
176 sc->sc_flags.port_powered) {
177 sc->sc_flags.d_pulled_up = 1;
181 temp = USS820_READ_1(sc, USS820_MCSR);
182 temp |= USS820_MCSR_DPEN;
183 USS820_WRITE_1(sc, USS820_MCSR, temp);
188 uss820dci_pull_down(struct uss820dci_softc *sc)
192 /* pulldown D+, if possible */
194 if (sc->sc_flags.d_pulled_up) {
195 sc->sc_flags.d_pulled_up = 0;
199 temp = USS820_READ_1(sc, USS820_MCSR);
200 temp &= ~USS820_MCSR_DPEN;
201 USS820_WRITE_1(sc, USS820_MCSR, temp);
206 uss820dci_wakeup_peer(struct uss820dci_softc *sc)
208 if (!(sc->sc_flags.status_suspend)) {
211 DPRINTFN(0, "not supported\n");
215 uss820dci_set_address(struct uss820dci_softc *sc, uint8_t addr)
217 DPRINTFN(5, "addr=%d\n", addr);
219 USS820_WRITE_1(sc, USS820_FADDR, addr);
223 uss820dci_setup_rx(struct uss820dci_td *td)
225 struct uss820dci_softc *sc;
226 struct usb2_device_request req;
231 /* select the correct endpoint */
232 bus_space_write_1(td->io_tag, td->io_hdl,
233 USS820_EPINDEX, td->ep_index);
235 /* read out FIFO status */
236 rx_stat = bus_space_read_1(td->io_tag, td->io_hdl,
239 /* get pointer to softc */
240 sc = USS820_DCI_PC2SC(td->pc);
242 DPRINTFN(5, "rx_stat=0x%02x rem=%u\n", rx_stat, td->remainder);
244 if (!(rx_stat & USS820_RXSTAT_RXSETUP)) {
247 /* clear did stall */
250 /* clear stall and all I/O */
251 uss820dci_update_shared_1(sc, USS820_EPCON,
252 0xFF ^ (USS820_EPCON_TXSTL |
255 USS820_EPCON_TXOE), 0);
257 /* clear end overwrite flag */
258 uss820dci_update_shared_1(sc, USS820_RXSTAT,
259 0xFF ^ USS820_RXSTAT_EDOVW, 0);
261 /* get the packet byte count */
262 count = bus_space_read_1(td->io_tag, td->io_hdl,
264 count |= (bus_space_read_1(td->io_tag, td->io_hdl,
265 USS820_RXCNTH) << 8);
268 /* verify data length */
269 if (count != td->remainder) {
270 DPRINTFN(0, "Invalid SETUP packet "
271 "length, %d bytes\n", count);
272 goto setup_not_complete;
274 if (count != sizeof(req)) {
275 DPRINTFN(0, "Unsupported SETUP packet "
276 "length, %d bytes\n", count);
277 goto setup_not_complete;
280 bus_space_read_multi_1(td->io_tag, td->io_hdl,
281 USS820_RXDAT, (void *)&req, sizeof(req));
283 /* read out FIFO status */
284 rx_stat = bus_space_read_1(td->io_tag, td->io_hdl,
287 if (rx_stat & (USS820_RXSTAT_EDOVW |
288 USS820_RXSTAT_STOVW)) {
289 DPRINTF("new SETUP packet received\n");
290 return (1); /* not complete */
292 /* clear receive setup bit */
293 uss820dci_update_shared_1(sc, USS820_RXSTAT,
294 0xFF ^ (USS820_RXSTAT_RXSETUP |
295 USS820_RXSTAT_EDOVW |
296 USS820_RXSTAT_STOVW), 0);
299 temp = bus_space_read_1(td->io_tag, td->io_hdl,
301 temp |= USS820_RXCON_RXFFRC;
302 bus_space_write_1(td->io_tag, td->io_hdl,
305 /* copy data into real buffer */
306 usb2_copy_in(td->pc, 0, &req, sizeof(req));
308 td->offset = sizeof(req);
311 /* sneak peek the set address */
312 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
313 (req.bRequest == UR_SET_ADDRESS)) {
314 sc->sc_dv_addr = req.wValue[0] & 0x7F;
316 sc->sc_dv_addr = 0xFF;
318 return (0); /* complete */
323 temp = bus_space_read_1(td->io_tag, td->io_hdl,
325 temp |= USS820_RXCON_RXFFRC;
326 bus_space_write_1(td->io_tag, td->io_hdl,
332 /* abort any ongoing transfer */
333 if (!td->did_stall) {
334 DPRINTFN(5, "stalling\n");
336 uss820dci_update_shared_1(sc, USS820_EPCON, 0xFF,
337 (USS820_EPCON_TXSTL | USS820_EPCON_RXSTL));
342 /* clear end overwrite flag, if any */
343 if (rx_stat & USS820_RXSTAT_RXSETUP) {
344 uss820dci_update_shared_1(sc, USS820_RXSTAT,
345 0xFF ^ (USS820_RXSTAT_EDOVW |
346 USS820_RXSTAT_STOVW |
347 USS820_RXSTAT_RXSETUP), 0);
349 return (1); /* not complete */
354 uss820dci_data_rx(struct uss820dci_td *td)
356 struct usb2_page_search buf_res;
364 to = 2; /* don't loop forever! */
367 /* select the correct endpoint */
368 bus_space_write_1(td->io_tag, td->io_hdl, USS820_EPINDEX, td->ep_index);
370 /* check if any of the FIFO banks have data */
372 /* read out FIFO flag */
373 rx_flag = bus_space_read_1(td->io_tag, td->io_hdl,
375 /* read out FIFO status */
376 rx_stat = bus_space_read_1(td->io_tag, td->io_hdl,
379 DPRINTFN(5, "rx_stat=0x%02x rx_flag=0x%02x rem=%u\n",
380 rx_stat, rx_flag, td->remainder);
382 if (rx_stat & (USS820_RXSTAT_RXSETUP |
383 USS820_RXSTAT_RXSOVW |
384 USS820_RXSTAT_EDOVW)) {
385 if (td->remainder == 0) {
387 * We are actually complete and have
388 * received the next SETUP
390 DPRINTFN(5, "faking complete\n");
391 return (0); /* complete */
394 * USB Host Aborted the transfer.
397 return (0); /* complete */
399 /* check for errors */
400 if (rx_flag & (USS820_RXFLG_RXOVF |
401 USS820_RXFLG_RXURF)) {
402 DPRINTFN(5, "overflow or underflow\n");
403 /* should not happen */
405 return (0); /* complete */
408 if (!(rx_flag & (USS820_RXFLG_RXFIF0 |
409 USS820_RXFLG_RXFIF1))) {
411 /* read out EPCON register */
412 /* enable RX input */
413 if (!td->did_enable) {
414 uss820dci_update_shared_1(USS820_DCI_PC2SC(td->pc),
415 USS820_EPCON, 0xFF, USS820_EPCON_RXIE);
418 return (1); /* not complete */
420 /* get the packet byte count */
421 count = bus_space_read_1(td->io_tag, td->io_hdl,
424 count |= (bus_space_read_1(td->io_tag, td->io_hdl,
425 USS820_RXCNTH) << 8);
428 DPRINTFN(5, "count=0x%04x\n", count);
430 /* verify the packet byte count */
431 if (count != td->max_packet_size) {
432 if (count < td->max_packet_size) {
433 /* we have a short packet */
437 /* invalid USB packet */
439 return (0); /* we are complete */
442 /* verify the packet byte count */
443 if (count > td->remainder) {
444 /* invalid USB packet */
446 return (0); /* we are complete */
449 usb2_get_page(td->pc, td->offset, &buf_res);
451 /* get correct length */
452 if (buf_res.length > count) {
453 buf_res.length = count;
456 bus_space_read_multi_1(td->io_tag, td->io_hdl,
457 USS820_RXDAT, buf_res.buffer, buf_res.length);
459 /* update counters */
460 count -= buf_res.length;
461 td->offset += buf_res.length;
462 td->remainder -= buf_res.length;
466 rx_cntl = bus_space_read_1(td->io_tag, td->io_hdl,
468 rx_cntl |= USS820_RXCON_RXFFRC;
469 bus_space_write_1(td->io_tag, td->io_hdl,
470 USS820_RXCON, rx_cntl);
472 /* check if we are complete */
473 if ((td->remainder == 0) || got_short) {
475 /* we are complete */
478 /* else need to receive a zero length packet */
483 return (1); /* not complete */
487 uss820dci_data_tx(struct uss820dci_td *td)
489 struct usb2_page_search buf_res;
496 /* select the correct endpoint */
497 bus_space_write_1(td->io_tag, td->io_hdl,
498 USS820_EPINDEX, td->ep_index);
500 to = 2; /* don't loop forever! */
503 /* read out TX FIFO flags */
504 tx_flag = bus_space_read_1(td->io_tag, td->io_hdl,
507 /* read out RX FIFO status last */
508 rx_stat = bus_space_read_1(td->io_tag, td->io_hdl,
511 DPRINTFN(5, "rx_stat=0x%02x tx_flag=0x%02x rem=%u\n",
512 rx_stat, tx_flag, td->remainder);
514 if (rx_stat & (USS820_RXSTAT_RXSETUP |
515 USS820_RXSTAT_RXSOVW |
516 USS820_RXSTAT_EDOVW)) {
518 * The current transfer was aborted
522 return (0); /* complete */
524 if (tx_flag & (USS820_TXFLG_TXOVF |
525 USS820_TXFLG_TXURF)) {
527 return (0); /* complete */
529 if (tx_flag & USS820_TXFLG_TXFIF0) {
530 if (tx_flag & USS820_TXFLG_TXFIF1) {
531 return (1); /* not complete */
534 if ((!td->support_multi_buffer) &&
535 (tx_flag & (USS820_TXFLG_TXFIF0 |
536 USS820_TXFLG_TXFIF1))) {
537 return (1); /* not complete */
539 count = td->max_packet_size;
540 if (td->remainder < count) {
541 /* we have a short packet */
543 count = td->remainder;
548 usb2_get_page(td->pc, td->offset, &buf_res);
550 /* get correct length */
551 if (buf_res.length > count) {
552 buf_res.length = count;
555 bus_space_write_multi_1(td->io_tag, td->io_hdl,
556 USS820_TXDAT, buf_res.buffer, buf_res.length);
558 /* update counters */
559 count -= buf_res.length;
560 td->offset += buf_res.length;
561 td->remainder -= buf_res.length;
564 /* post-write high packet byte count first */
565 bus_space_write_1(td->io_tag, td->io_hdl,
566 USS820_TXCNTH, count_copy >> 8);
568 /* post-write low packet byte count last */
569 bus_space_write_1(td->io_tag, td->io_hdl,
570 USS820_TXCNTL, count_copy);
573 * Enable TX output, which must happen after that we have written
574 * data into the FIFO. This is undocumented.
576 if (!td->did_enable) {
577 uss820dci_update_shared_1(USS820_DCI_PC2SC(td->pc),
578 USS820_EPCON, 0xFF, USS820_EPCON_TXOE);
581 /* check remainder */
582 if (td->remainder == 0) {
584 return (0); /* complete */
586 /* else we need to transmit a short packet */
591 return (1); /* not complete */
595 uss820dci_data_tx_sync(struct uss820dci_td *td)
597 struct uss820dci_softc *sc;
601 /* select the correct endpoint */
602 bus_space_write_1(td->io_tag, td->io_hdl,
603 USS820_EPINDEX, td->ep_index);
605 /* read out TX FIFO flag */
606 tx_flag = bus_space_read_1(td->io_tag, td->io_hdl,
609 /* read out RX FIFO status last */
610 rx_stat = bus_space_read_1(td->io_tag, td->io_hdl,
613 DPRINTFN(5, "rx_stat=0x%02x rem=%u\n", rx_stat, td->remainder);
615 if (rx_stat & (USS820_RXSTAT_RXSETUP |
616 USS820_RXSTAT_RXSOVW |
617 USS820_RXSTAT_EDOVW)) {
618 DPRINTFN(5, "faking complete\n");
620 return (0); /* complete */
622 DPRINTFN(5, "tx_flag=0x%02x rem=%u\n",
623 tx_flag, td->remainder);
625 if (tx_flag & (USS820_TXFLG_TXOVF |
626 USS820_TXFLG_TXURF)) {
628 return (0); /* complete */
630 if (tx_flag & (USS820_TXFLG_TXFIF0 |
631 USS820_TXFLG_TXFIF1)) {
632 return (1); /* not complete */
634 sc = USS820_DCI_PC2SC(td->pc);
635 if (sc->sc_dv_addr != 0xFF) {
636 /* write function address */
637 uss820dci_set_address(sc, sc->sc_dv_addr);
639 return (0); /* complete */
643 uss820dci_xfer_do_fifo(struct usb2_xfer *xfer)
645 struct uss820dci_td *td;
649 td = xfer->td_transfer_cache;
651 if ((td->func) (td)) {
652 /* operation in progress */
655 if (((void *)td) == xfer->td_transfer_last) {
660 } else if (td->remainder > 0) {
662 * We had a short transfer. If there is no alternate
663 * next, stop processing !
670 * Fetch the next transfer descriptor.
673 xfer->td_transfer_cache = td;
675 return (1); /* not complete */
678 /* compute all actual lengths */
680 uss820dci_standard_done(xfer);
682 return (0); /* complete */
686 uss820dci_interrupt_poll(struct uss820dci_softc *sc)
688 struct usb2_xfer *xfer;
691 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
692 if (!uss820dci_xfer_do_fifo(xfer)) {
693 /* queue has been modified */
700 uss820dci_wait_suspend(struct uss820dci_softc *sc, uint8_t on)
705 scr = USS820_READ_1(sc, USS820_SCR);
706 scratch = USS820_READ_1(sc, USS820_SCRATCH);
709 scr |= USS820_SCR_IE_SUSP;
710 scratch &= ~USS820_SCRATCH_IE_RESUME;
712 scr &= ~USS820_SCR_IE_SUSP;
713 scratch |= USS820_SCRATCH_IE_RESUME;
716 USS820_WRITE_1(sc, USS820_SCR, scr);
717 USS820_WRITE_1(sc, USS820_SCRATCH, scratch);
721 uss820dci_interrupt(struct uss820dci_softc *sc)
726 USB_BUS_LOCK(&sc->sc_bus);
728 ssr = USS820_READ_1(sc, USS820_SSR);
730 ssr &= (USS820_SSR_SUSPEND |
734 /* acknowledge all interrupts */
736 uss820dci_update_shared_1(sc, USS820_SSR, 0, 0);
738 /* check for any bus state change interrupts */
744 if (ssr & USS820_SSR_RESET) {
745 sc->sc_flags.status_bus_reset = 1;
746 sc->sc_flags.status_suspend = 0;
747 sc->sc_flags.change_suspend = 0;
748 sc->sc_flags.change_connect = 1;
750 /* disable resume interrupt */
751 uss820dci_wait_suspend(sc, 1);
756 * If "RESUME" and "SUSPEND" is set at the same time
757 * we interpret that like "RESUME". Resume is set when
758 * there is at least 3 milliseconds of inactivity on
761 if (ssr & USS820_SSR_RESUME) {
762 if (sc->sc_flags.status_suspend) {
763 sc->sc_flags.status_suspend = 0;
764 sc->sc_flags.change_suspend = 1;
765 /* disable resume interrupt */
766 uss820dci_wait_suspend(sc, 1);
769 } else if (ssr & USS820_SSR_SUSPEND) {
770 if (!sc->sc_flags.status_suspend) {
771 sc->sc_flags.status_suspend = 1;
772 sc->sc_flags.change_suspend = 1;
773 /* enable resume interrupt */
774 uss820dci_wait_suspend(sc, 0);
780 DPRINTF("real bus interrupt 0x%02x\n", ssr);
782 /* complete root HUB interrupt endpoint */
783 uss820dci_root_intr(sc);
786 /* acknowledge all SBI interrupts */
787 uss820dci_update_shared_1(sc, USS820_SBI, 0, 0);
789 /* acknowledge all SBI1 interrupts */
790 uss820dci_update_shared_1(sc, USS820_SBI1, 0, 0);
792 /* poll all active transfers */
793 uss820dci_interrupt_poll(sc);
795 USB_BUS_UNLOCK(&sc->sc_bus);
799 uss820dci_setup_standard_chain_sub(struct uss820_std_temp *temp)
801 struct uss820dci_td *td;
803 /* get current Transfer Descriptor */
807 /* prepare for next TD */
808 temp->td_next = td->obj_next;
810 /* fill out the Transfer Descriptor */
811 td->func = temp->func;
813 td->offset = temp->offset;
814 td->remainder = temp->len;
817 td->did_stall = temp->did_stall;
818 td->short_pkt = temp->short_pkt;
819 td->alt_next = temp->setup_alt_next;
823 uss820dci_setup_standard_chain(struct usb2_xfer *xfer)
825 struct uss820_std_temp temp;
826 struct uss820dci_softc *sc;
827 struct uss820dci_td *td;
831 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
832 xfer->address, UE_GET_ADDR(xfer->endpoint),
833 xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
835 temp.max_frame_size = xfer->max_frame_size;
837 td = xfer->td_start[0];
838 xfer->td_transfer_first = td;
839 xfer->td_transfer_cache = td;
844 temp.td_next = xfer->td_start[0];
846 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
847 temp.did_stall = !xfer->flags_int.control_stall;
849 sc = USS820_DCI_BUS2SC(xfer->xroot->bus);
850 ep_no = (xfer->endpoint & UE_ADDR);
852 /* check if we should prepend a setup message */
854 if (xfer->flags_int.control_xfr) {
855 if (xfer->flags_int.control_hdr) {
857 temp.func = &uss820dci_setup_rx;
858 temp.len = xfer->frlengths[0];
859 temp.pc = xfer->frbuffers + 0;
860 temp.short_pkt = temp.len ? 1 : 0;
861 /* check for last frame */
862 if (xfer->nframes == 1) {
863 /* no STATUS stage yet, SETUP is last */
864 if (xfer->flags_int.control_act)
865 temp.setup_alt_next = 0;
868 uss820dci_setup_standard_chain_sub(&temp);
875 if (x != xfer->nframes) {
876 if (xfer->endpoint & UE_DIR_IN) {
877 temp.func = &uss820dci_data_tx;
879 temp.func = &uss820dci_data_rx;
882 /* setup "pc" pointer */
883 temp.pc = xfer->frbuffers + x;
885 while (x != xfer->nframes) {
887 /* DATA0 / DATA1 message */
889 temp.len = xfer->frlengths[x];
893 if (x == xfer->nframes) {
894 if (xfer->flags_int.control_xfr) {
895 if (xfer->flags_int.control_act) {
896 temp.setup_alt_next = 0;
899 temp.setup_alt_next = 0;
904 /* make sure that we send an USB packet */
910 /* regular data transfer */
912 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
915 uss820dci_setup_standard_chain_sub(&temp);
917 if (xfer->flags_int.isochronous_xfr) {
918 temp.offset += temp.len;
920 /* get next Page Cache pointer */
921 temp.pc = xfer->frbuffers + x;
925 /* check for control transfer */
926 if (xfer->flags_int.control_xfr) {
929 /* always setup a valid "pc" pointer for status and sync */
930 temp.pc = xfer->frbuffers + 0;
933 temp.setup_alt_next = 0;
935 /* check if we should append a status stage */
936 if (!xfer->flags_int.control_act) {
939 * Send a DATA1 message and invert the current
940 * endpoint direction.
942 if (xfer->endpoint & UE_DIR_IN) {
943 temp.func = &uss820dci_data_rx;
946 temp.func = &uss820dci_data_tx;
952 uss820dci_setup_standard_chain_sub(&temp);
954 /* we need a SYNC point after TX */
955 temp.func = &uss820dci_data_tx_sync;
956 uss820dci_setup_standard_chain_sub(&temp);
960 /* must have at least one frame! */
962 xfer->td_transfer_last = td;
966 uss820dci_timeout(void *arg)
968 struct usb2_xfer *xfer = arg;
970 DPRINTF("xfer=%p\n", xfer);
972 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
974 /* transfer is transferred */
975 uss820dci_device_done(xfer, USB_ERR_TIMEOUT);
979 uss820dci_intr_set(struct usb2_xfer *xfer, uint8_t set)
981 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus);
982 uint8_t ep_no = (xfer->endpoint & UE_ADDR);
986 DPRINTFN(15, "endpoint 0x%02x\n", xfer->endpoint);
989 ep_reg = USS820_SBIE1;
991 ep_reg = USS820_SBIE;
995 ep_no = 1 << (2 * ep_no);
997 if (xfer->flags_int.control_xfr) {
998 if (xfer->flags_int.control_hdr) {
999 ep_no <<= 1; /* RX interrupt only */
1001 ep_no |= (ep_no << 1); /* RX and TX interrupt */
1004 if (!(xfer->endpoint & UE_DIR_IN)) {
1008 temp = USS820_READ_1(sc, ep_reg);
1014 USS820_WRITE_1(sc, ep_reg, temp);
1018 uss820dci_start_standard_chain(struct usb2_xfer *xfer)
1023 if (uss820dci_xfer_do_fifo(xfer)) {
1026 * Only enable the endpoint interrupt when we are
1027 * actually waiting for data, hence we are dealing
1028 * with level triggered interrupts !
1030 uss820dci_intr_set(xfer, 1);
1032 /* put transfer on interrupt queue */
1033 usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1035 /* start timeout, if any */
1036 if (xfer->timeout != 0) {
1037 usb2_transfer_timeout_ms(xfer,
1038 &uss820dci_timeout, xfer->timeout);
1044 uss820dci_root_intr(struct uss820dci_softc *sc)
1048 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1051 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
1053 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1054 sizeof(sc->sc_hub_idata));
1058 uss820dci_standard_done_sub(struct usb2_xfer *xfer)
1060 struct uss820dci_td *td;
1066 td = xfer->td_transfer_cache;
1069 len = td->remainder;
1071 if (xfer->aframes != xfer->nframes) {
1073 * Verify the length and subtract
1074 * the remainder from "frlengths[]":
1076 if (len > xfer->frlengths[xfer->aframes]) {
1079 xfer->frlengths[xfer->aframes] -= len;
1082 /* Check for transfer error */
1084 /* the transfer is finished */
1089 /* Check for short transfer */
1091 if (xfer->flags_int.short_frames_ok) {
1092 /* follow alt next */
1099 /* the transfer is finished */
1107 /* this USB frame is complete */
1113 /* update transfer cache */
1115 xfer->td_transfer_cache = td;
1118 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1122 uss820dci_standard_done(struct usb2_xfer *xfer)
1124 usb2_error_t err = 0;
1126 DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1131 xfer->td_transfer_cache = xfer->td_transfer_first;
1133 if (xfer->flags_int.control_xfr) {
1135 if (xfer->flags_int.control_hdr) {
1137 err = uss820dci_standard_done_sub(xfer);
1141 if (xfer->td_transfer_cache == NULL) {
1145 while (xfer->aframes != xfer->nframes) {
1147 err = uss820dci_standard_done_sub(xfer);
1150 if (xfer->td_transfer_cache == NULL) {
1155 if (xfer->flags_int.control_xfr &&
1156 !xfer->flags_int.control_act) {
1158 err = uss820dci_standard_done_sub(xfer);
1161 uss820dci_device_done(xfer, err);
1164 /*------------------------------------------------------------------------*
1165 * uss820dci_device_done
1167 * NOTE: this function can be called more than one time on the
1168 * same USB transfer!
1169 *------------------------------------------------------------------------*/
1171 uss820dci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
1173 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1175 DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
1176 xfer, xfer->pipe, error);
1178 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1179 uss820dci_intr_set(xfer, 0);
1181 /* dequeue transfer and start next transfer */
1182 usb2_transfer_done(xfer, error);
1186 uss820dci_set_stall(struct usb2_device *udev, struct usb2_xfer *xfer,
1187 struct usb2_pipe *pipe)
1189 struct uss820dci_softc *sc;
1195 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1197 DPRINTFN(5, "pipe=%p\n", pipe);
1200 /* cancel any ongoing transfers */
1201 uss820dci_device_done(xfer, USB_ERR_STALLED);
1203 /* set FORCESTALL */
1204 sc = USS820_DCI_BUS2SC(udev->bus);
1205 ep_no = (pipe->edesc->bEndpointAddress & UE_ADDR);
1206 ep_dir = (pipe->edesc->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT));
1207 ep_type = (pipe->edesc->bmAttributes & UE_XFERTYPE);
1209 if (ep_type == UE_CONTROL) {
1210 /* should not happen */
1213 USS820_WRITE_1(sc, USS820_EPINDEX, ep_no);
1215 if (ep_dir == UE_DIR_IN) {
1216 temp = USS820_EPCON_TXSTL;
1218 temp = USS820_EPCON_RXSTL;
1220 uss820dci_update_shared_1(sc, USS820_EPCON, 0xFF, temp);
1224 uss820dci_clear_stall_sub(struct uss820dci_softc *sc,
1225 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
1229 if (ep_type == UE_CONTROL) {
1230 /* clearing stall is not needed */
1233 /* select endpoint index */
1234 USS820_WRITE_1(sc, USS820_EPINDEX, ep_no);
1236 /* clear stall and disable I/O transfers */
1237 if (ep_dir == UE_DIR_IN) {
1238 temp = 0xFF ^ (USS820_EPCON_TXOE |
1239 USS820_EPCON_TXSTL);
1241 temp = 0xFF ^ (USS820_EPCON_RXIE |
1242 USS820_EPCON_RXSTL);
1244 uss820dci_update_shared_1(sc, USS820_EPCON, temp, 0);
1246 if (ep_dir == UE_DIR_IN) {
1247 /* reset data toggle */
1248 USS820_WRITE_1(sc, USS820_TXSTAT,
1249 USS820_TXSTAT_TXSOVW);
1252 temp = USS820_READ_1(sc, USS820_TXCON);
1253 temp |= USS820_TXCON_TXCLR;
1254 USS820_WRITE_1(sc, USS820_TXCON, temp);
1255 temp &= ~USS820_TXCON_TXCLR;
1256 USS820_WRITE_1(sc, USS820_TXCON, temp);
1259 /* reset data toggle */
1260 uss820dci_update_shared_1(sc, USS820_RXSTAT,
1261 0, USS820_RXSTAT_RXSOVW);
1264 temp = USS820_READ_1(sc, USS820_RXCON);
1265 temp |= USS820_RXCON_RXCLR;
1266 temp &= ~USS820_RXCON_RXFFRC;
1267 USS820_WRITE_1(sc, USS820_RXCON, temp);
1268 temp &= ~USS820_RXCON_RXCLR;
1269 USS820_WRITE_1(sc, USS820_RXCON, temp);
1274 uss820dci_clear_stall(struct usb2_device *udev, struct usb2_pipe *pipe)
1276 struct uss820dci_softc *sc;
1277 struct usb2_endpoint_descriptor *ed;
1279 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1281 DPRINTFN(5, "pipe=%p\n", pipe);
1284 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1289 sc = USS820_DCI_BUS2SC(udev->bus);
1291 /* get endpoint descriptor */
1294 /* reset endpoint */
1295 uss820dci_clear_stall_sub(sc,
1296 (ed->bEndpointAddress & UE_ADDR),
1297 (ed->bmAttributes & UE_XFERTYPE),
1298 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1302 uss820dci_init(struct uss820dci_softc *sc)
1304 const struct usb2_hw_ep_profile *pf;
1310 /* set up the bus structure */
1311 sc->sc_bus.usbrev = USB_REV_1_1;
1312 sc->sc_bus.methods = &uss820dci_bus_methods;
1314 USB_BUS_LOCK(&sc->sc_bus);
1316 /* we always have VBUS */
1317 sc->sc_flags.status_vbus = 1;
1319 /* reset the chip */
1320 USS820_WRITE_1(sc, USS820_SCR, USS820_SCR_SRESET);
1322 USS820_WRITE_1(sc, USS820_SCR, 0);
1324 /* wait for reset to complete */
1327 temp = USS820_READ_1(sc, USS820_MCSR);
1329 if (temp & USS820_MCSR_INIT) {
1333 USB_BUS_UNLOCK(&sc->sc_bus);
1334 return (USB_ERR_INVAL);
1336 /* wait a little for things to stabilise */
1341 uss820dci_pull_down(sc);
1343 /* wait 10ms for pulldown to stabilise */
1344 usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
1346 /* check hardware revision */
1347 temp = USS820_READ_1(sc, USS820_REV);
1350 USB_BUS_UNLOCK(&sc->sc_bus);
1351 return (USB_ERR_INVAL);
1353 /* enable interrupts */
1354 USS820_WRITE_1(sc, USS820_SCR,
1356 USS820_SCR_IE_RESET |
1357 /* USS820_SCR_RWUPE | */
1358 USS820_SCR_IE_SUSP |
1361 /* enable interrupts */
1362 USS820_WRITE_1(sc, USS820_SCRATCH,
1363 USS820_SCRATCH_IE_RESUME);
1365 /* enable features */
1366 USS820_WRITE_1(sc, USS820_MCSR,
1367 USS820_MCSR_BDFEAT |
1370 sc->sc_flags.mcsr_feat = 1;
1372 /* disable interrupts */
1373 USS820_WRITE_1(sc, USS820_SBIE, 0);
1375 /* disable interrupts */
1376 USS820_WRITE_1(sc, USS820_SBIE1, 0);
1378 /* disable all endpoints */
1379 for (n = 0; n != USS820_EP_MAX; n++) {
1381 /* select endpoint */
1382 USS820_WRITE_1(sc, USS820_EPINDEX, n);
1384 /* disable endpoint */
1385 uss820dci_update_shared_1(sc, USS820_EPCON, 0, 0);
1389 * Initialise default values for some registers that cannot be
1390 * changed during operation!
1392 for (n = 0; n != USS820_EP_MAX; n++) {
1394 uss820dci_get_hw_ep_profile(NULL, &pf, n);
1396 /* the maximum frame sizes should be the same */
1397 if (pf->max_in_frame_size != pf->max_out_frame_size) {
1398 DPRINTF("Max frame size mismatch %u != %u\n",
1399 pf->max_in_frame_size, pf->max_out_frame_size);
1401 if (pf->support_isochronous) {
1402 if (pf->max_in_frame_size <= 64) {
1403 temp = (USS820_TXCON_FFSZ_16_64 |
1404 USS820_TXCON_TXISO |
1406 } else if (pf->max_in_frame_size <= 256) {
1407 temp = (USS820_TXCON_FFSZ_64_256 |
1408 USS820_TXCON_TXISO |
1410 } else if (pf->max_in_frame_size <= 512) {
1411 temp = (USS820_TXCON_FFSZ_8_512 |
1412 USS820_TXCON_TXISO |
1414 } else { /* 1024 bytes */
1415 temp = (USS820_TXCON_FFSZ_32_1024 |
1416 USS820_TXCON_TXISO |
1420 if ((pf->max_in_frame_size <= 8) &&
1421 (sc->sc_flags.mcsr_feat)) {
1422 temp = (USS820_TXCON_FFSZ_8_512 |
1424 } else if (pf->max_in_frame_size <= 16) {
1425 temp = (USS820_TXCON_FFSZ_16_64 |
1427 } else if ((pf->max_in_frame_size <= 32) &&
1428 (sc->sc_flags.mcsr_feat)) {
1429 temp = (USS820_TXCON_FFSZ_32_1024 |
1431 } else { /* 64 bytes */
1432 temp = (USS820_TXCON_FFSZ_64_256 |
1437 /* need to configure the chip early */
1439 USS820_WRITE_1(sc, USS820_EPINDEX, n);
1440 USS820_WRITE_1(sc, USS820_TXCON, temp);
1441 USS820_WRITE_1(sc, USS820_RXCON, temp);
1443 if (pf->support_control) {
1444 temp = USS820_EPCON_CTLEP |
1445 USS820_EPCON_RXSPM |
1447 USS820_EPCON_RXEPEN |
1449 USS820_EPCON_TXEPEN;
1451 temp = USS820_EPCON_RXEPEN | USS820_EPCON_TXEPEN;
1454 uss820dci_update_shared_1(sc, USS820_EPCON, 0xFF, temp);
1457 USB_BUS_UNLOCK(&sc->sc_bus);
1459 /* catch any lost interrupts */
1461 uss820dci_do_poll(&sc->sc_bus);
1463 return (0); /* success */
1467 uss820dci_uninit(struct uss820dci_softc *sc)
1471 USB_BUS_LOCK(&sc->sc_bus);
1473 /* disable all interrupts */
1474 temp = USS820_READ_1(sc, USS820_SCR);
1475 temp &= ~USS820_SCR_T_IRQ;
1476 USS820_WRITE_1(sc, USS820_SCR, temp);
1478 sc->sc_flags.port_powered = 0;
1479 sc->sc_flags.status_vbus = 0;
1480 sc->sc_flags.status_bus_reset = 0;
1481 sc->sc_flags.status_suspend = 0;
1482 sc->sc_flags.change_suspend = 0;
1483 sc->sc_flags.change_connect = 1;
1485 uss820dci_pull_down(sc);
1486 USB_BUS_UNLOCK(&sc->sc_bus);
1490 uss820dci_suspend(struct uss820dci_softc *sc)
1496 uss820dci_resume(struct uss820dci_softc *sc)
1502 uss820dci_do_poll(struct usb2_bus *bus)
1504 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(bus);
1506 USB_BUS_LOCK(&sc->sc_bus);
1507 uss820dci_interrupt_poll(sc);
1508 USB_BUS_UNLOCK(&sc->sc_bus);
1511 /*------------------------------------------------------------------------*
1512 * at91dci bulk support
1513 *------------------------------------------------------------------------*/
1515 uss820dci_device_bulk_open(struct usb2_xfer *xfer)
1521 uss820dci_device_bulk_close(struct usb2_xfer *xfer)
1523 uss820dci_device_done(xfer, USB_ERR_CANCELLED);
1527 uss820dci_device_bulk_enter(struct usb2_xfer *xfer)
1533 uss820dci_device_bulk_start(struct usb2_xfer *xfer)
1536 uss820dci_setup_standard_chain(xfer);
1537 uss820dci_start_standard_chain(xfer);
1540 struct usb2_pipe_methods uss820dci_device_bulk_methods =
1542 .open = uss820dci_device_bulk_open,
1543 .close = uss820dci_device_bulk_close,
1544 .enter = uss820dci_device_bulk_enter,
1545 .start = uss820dci_device_bulk_start,
1548 /*------------------------------------------------------------------------*
1549 * at91dci control support
1550 *------------------------------------------------------------------------*/
1552 uss820dci_device_ctrl_open(struct usb2_xfer *xfer)
1558 uss820dci_device_ctrl_close(struct usb2_xfer *xfer)
1560 uss820dci_device_done(xfer, USB_ERR_CANCELLED);
1564 uss820dci_device_ctrl_enter(struct usb2_xfer *xfer)
1570 uss820dci_device_ctrl_start(struct usb2_xfer *xfer)
1573 uss820dci_setup_standard_chain(xfer);
1574 uss820dci_start_standard_chain(xfer);
1577 struct usb2_pipe_methods uss820dci_device_ctrl_methods =
1579 .open = uss820dci_device_ctrl_open,
1580 .close = uss820dci_device_ctrl_close,
1581 .enter = uss820dci_device_ctrl_enter,
1582 .start = uss820dci_device_ctrl_start,
1585 /*------------------------------------------------------------------------*
1586 * at91dci interrupt support
1587 *------------------------------------------------------------------------*/
1589 uss820dci_device_intr_open(struct usb2_xfer *xfer)
1595 uss820dci_device_intr_close(struct usb2_xfer *xfer)
1597 uss820dci_device_done(xfer, USB_ERR_CANCELLED);
1601 uss820dci_device_intr_enter(struct usb2_xfer *xfer)
1607 uss820dci_device_intr_start(struct usb2_xfer *xfer)
1610 uss820dci_setup_standard_chain(xfer);
1611 uss820dci_start_standard_chain(xfer);
1614 struct usb2_pipe_methods uss820dci_device_intr_methods =
1616 .open = uss820dci_device_intr_open,
1617 .close = uss820dci_device_intr_close,
1618 .enter = uss820dci_device_intr_enter,
1619 .start = uss820dci_device_intr_start,
1622 /*------------------------------------------------------------------------*
1623 * at91dci full speed isochronous support
1624 *------------------------------------------------------------------------*/
1626 uss820dci_device_isoc_fs_open(struct usb2_xfer *xfer)
1632 uss820dci_device_isoc_fs_close(struct usb2_xfer *xfer)
1634 uss820dci_device_done(xfer, USB_ERR_CANCELLED);
1638 uss820dci_device_isoc_fs_enter(struct usb2_xfer *xfer)
1640 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(xfer->xroot->bus);
1644 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1645 xfer, xfer->pipe->isoc_next, xfer->nframes);
1647 /* get the current frame index - we don't need the high bits */
1649 nframes = USS820_READ_1(sc, USS820_SOFL);
1652 * check if the frame index is within the window where the
1653 * frames will be inserted
1655 temp = (nframes - xfer->pipe->isoc_next) & USS820_SOFL_MASK;
1657 if ((xfer->pipe->is_synced == 0) ||
1658 (temp < xfer->nframes)) {
1660 * If there is data underflow or the pipe queue is
1661 * empty we schedule the transfer a few frames ahead
1662 * of the current frame position. Else two isochronous
1663 * transfers might overlap.
1665 xfer->pipe->isoc_next = (nframes + 3) & USS820_SOFL_MASK;
1666 xfer->pipe->is_synced = 1;
1667 DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
1670 * compute how many milliseconds the insertion is ahead of the
1671 * current frame position:
1673 temp = (xfer->pipe->isoc_next - nframes) & USS820_SOFL_MASK;
1676 * pre-compute when the isochronous transfer will be finished:
1678 xfer->isoc_time_complete =
1679 usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1682 /* compute frame number for next insertion */
1683 xfer->pipe->isoc_next += xfer->nframes;
1686 uss820dci_setup_standard_chain(xfer);
1690 uss820dci_device_isoc_fs_start(struct usb2_xfer *xfer)
1692 /* start TD chain */
1693 uss820dci_start_standard_chain(xfer);
1696 struct usb2_pipe_methods uss820dci_device_isoc_fs_methods =
1698 .open = uss820dci_device_isoc_fs_open,
1699 .close = uss820dci_device_isoc_fs_close,
1700 .enter = uss820dci_device_isoc_fs_enter,
1701 .start = uss820dci_device_isoc_fs_start,
1704 /*------------------------------------------------------------------------*
1705 * at91dci root control support
1706 *------------------------------------------------------------------------*
1707 * Simulate a hardware HUB by handling all the necessary requests.
1708 *------------------------------------------------------------------------*/
1710 static const struct usb2_device_descriptor uss820dci_devd = {
1711 .bLength = sizeof(struct usb2_device_descriptor),
1712 .bDescriptorType = UDESC_DEVICE,
1713 .bcdUSB = {0x00, 0x02},
1714 .bDeviceClass = UDCLASS_HUB,
1715 .bDeviceSubClass = UDSUBCLASS_HUB,
1716 .bDeviceProtocol = UDPROTO_HSHUBSTT,
1717 .bMaxPacketSize = 64,
1718 .bcdDevice = {0x00, 0x01},
1721 .bNumConfigurations = 1,
1724 static const struct usb2_device_qualifier uss820dci_odevd = {
1725 .bLength = sizeof(struct usb2_device_qualifier),
1726 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
1727 .bcdUSB = {0x00, 0x02},
1728 .bDeviceClass = UDCLASS_HUB,
1729 .bDeviceSubClass = UDSUBCLASS_HUB,
1730 .bDeviceProtocol = UDPROTO_FSHUB,
1731 .bMaxPacketSize0 = 0,
1732 .bNumConfigurations = 0,
1735 static const struct uss820dci_config_desc uss820dci_confd = {
1737 .bLength = sizeof(struct usb2_config_descriptor),
1738 .bDescriptorType = UDESC_CONFIG,
1739 .wTotalLength[0] = sizeof(uss820dci_confd),
1741 .bConfigurationValue = 1,
1742 .iConfiguration = 0,
1743 .bmAttributes = UC_SELF_POWERED,
1747 .bLength = sizeof(struct usb2_interface_descriptor),
1748 .bDescriptorType = UDESC_INTERFACE,
1750 .bInterfaceClass = UICLASS_HUB,
1751 .bInterfaceSubClass = UISUBCLASS_HUB,
1752 .bInterfaceProtocol = UIPROTO_HSHUBSTT,
1756 .bLength = sizeof(struct usb2_endpoint_descriptor),
1757 .bDescriptorType = UDESC_ENDPOINT,
1758 .bEndpointAddress = (UE_DIR_IN | USS820_DCI_INTR_ENDPT),
1759 .bmAttributes = UE_INTERRUPT,
1760 .wMaxPacketSize[0] = 8,
1765 static const struct usb2_hub_descriptor_min uss820dci_hubd = {
1766 .bDescLength = sizeof(uss820dci_hubd),
1767 .bDescriptorType = UDESC_HUB,
1769 .wHubCharacteristics[0] =
1770 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF,
1771 .wHubCharacteristics[1] =
1772 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8,
1773 .bPwrOn2PwrGood = 50,
1774 .bHubContrCurrent = 0,
1775 .DeviceRemovable = {0}, /* port is removable */
1778 #define STRING_LANG \
1779 0x09, 0x04, /* American English */
1781 #define STRING_VENDOR \
1782 'A', 0, 'G', 0, 'E', 0, 'R', 0, 'E', 0
1784 #define STRING_PRODUCT \
1785 'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
1786 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
1789 USB_MAKE_STRING_DESC(STRING_LANG, uss820dci_langtab);
1790 USB_MAKE_STRING_DESC(STRING_VENDOR, uss820dci_vendor);
1791 USB_MAKE_STRING_DESC(STRING_PRODUCT, uss820dci_product);
1794 uss820dci_roothub_exec(struct usb2_device *udev,
1795 struct usb2_device_request *req, const void **pptr, uint16_t *plength)
1797 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(udev->bus);
1804 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1807 ptr = (const void *)&sc->sc_hub_temp;
1811 value = UGETW(req->wValue);
1812 index = UGETW(req->wIndex);
1814 /* demultiplex the control request */
1816 switch (req->bmRequestType) {
1817 case UT_READ_DEVICE:
1818 switch (req->bRequest) {
1819 case UR_GET_DESCRIPTOR:
1820 goto tr_handle_get_descriptor;
1822 goto tr_handle_get_config;
1824 goto tr_handle_get_status;
1830 case UT_WRITE_DEVICE:
1831 switch (req->bRequest) {
1832 case UR_SET_ADDRESS:
1833 goto tr_handle_set_address;
1835 goto tr_handle_set_config;
1836 case UR_CLEAR_FEATURE:
1837 goto tr_valid; /* nop */
1838 case UR_SET_DESCRIPTOR:
1839 goto tr_valid; /* nop */
1840 case UR_SET_FEATURE:
1846 case UT_WRITE_ENDPOINT:
1847 switch (req->bRequest) {
1848 case UR_CLEAR_FEATURE:
1849 switch (UGETW(req->wValue)) {
1850 case UF_ENDPOINT_HALT:
1851 goto tr_handle_clear_halt;
1852 case UF_DEVICE_REMOTE_WAKEUP:
1853 goto tr_handle_clear_wakeup;
1858 case UR_SET_FEATURE:
1859 switch (UGETW(req->wValue)) {
1860 case UF_ENDPOINT_HALT:
1861 goto tr_handle_set_halt;
1862 case UF_DEVICE_REMOTE_WAKEUP:
1863 goto tr_handle_set_wakeup;
1868 case UR_SYNCH_FRAME:
1869 goto tr_valid; /* nop */
1875 case UT_READ_ENDPOINT:
1876 switch (req->bRequest) {
1878 goto tr_handle_get_ep_status;
1884 case UT_WRITE_INTERFACE:
1885 switch (req->bRequest) {
1886 case UR_SET_INTERFACE:
1887 goto tr_handle_set_interface;
1888 case UR_CLEAR_FEATURE:
1889 goto tr_valid; /* nop */
1890 case UR_SET_FEATURE:
1896 case UT_READ_INTERFACE:
1897 switch (req->bRequest) {
1898 case UR_GET_INTERFACE:
1899 goto tr_handle_get_interface;
1901 goto tr_handle_get_iface_status;
1907 case UT_WRITE_CLASS_INTERFACE:
1908 case UT_WRITE_VENDOR_INTERFACE:
1912 case UT_READ_CLASS_INTERFACE:
1913 case UT_READ_VENDOR_INTERFACE:
1917 case UT_WRITE_CLASS_DEVICE:
1918 switch (req->bRequest) {
1919 case UR_CLEAR_FEATURE:
1921 case UR_SET_DESCRIPTOR:
1922 case UR_SET_FEATURE:
1929 case UT_WRITE_CLASS_OTHER:
1930 switch (req->bRequest) {
1931 case UR_CLEAR_FEATURE:
1932 goto tr_handle_clear_port_feature;
1933 case UR_SET_FEATURE:
1934 goto tr_handle_set_port_feature;
1935 case UR_CLEAR_TT_BUFFER:
1945 case UT_READ_CLASS_OTHER:
1946 switch (req->bRequest) {
1947 case UR_GET_TT_STATE:
1948 goto tr_handle_get_tt_state;
1950 goto tr_handle_get_port_status;
1956 case UT_READ_CLASS_DEVICE:
1957 switch (req->bRequest) {
1958 case UR_GET_DESCRIPTOR:
1959 goto tr_handle_get_class_descriptor;
1961 goto tr_handle_get_class_status;
1972 tr_handle_get_descriptor:
1973 switch (value >> 8) {
1978 len = sizeof(uss820dci_devd);
1979 ptr = (const void *)&uss820dci_devd;
1985 len = sizeof(uss820dci_confd);
1986 ptr = (const void *)&uss820dci_confd;
1989 switch (value & 0xff) {
1990 case 0: /* Language table */
1991 len = sizeof(uss820dci_langtab);
1992 ptr = (const void *)&uss820dci_langtab;
1995 case 1: /* Vendor */
1996 len = sizeof(uss820dci_vendor);
1997 ptr = (const void *)&uss820dci_vendor;
2000 case 2: /* Product */
2001 len = sizeof(uss820dci_product);
2002 ptr = (const void *)&uss820dci_product;
2013 tr_handle_get_config:
2015 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2018 tr_handle_get_status:
2020 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2023 tr_handle_set_address:
2024 if (value & 0xFF00) {
2027 sc->sc_rt_addr = value;
2030 tr_handle_set_config:
2034 sc->sc_conf = value;
2037 tr_handle_get_interface:
2039 sc->sc_hub_temp.wValue[0] = 0;
2042 tr_handle_get_tt_state:
2043 tr_handle_get_class_status:
2044 tr_handle_get_iface_status:
2045 tr_handle_get_ep_status:
2047 USETW(sc->sc_hub_temp.wValue, 0);
2051 tr_handle_set_interface:
2052 tr_handle_set_wakeup:
2053 tr_handle_clear_wakeup:
2054 tr_handle_clear_halt:
2057 tr_handle_clear_port_feature:
2061 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
2064 case UHF_PORT_SUSPEND:
2065 uss820dci_wakeup_peer(sc);
2068 case UHF_PORT_ENABLE:
2069 sc->sc_flags.port_enabled = 0;
2073 case UHF_PORT_INDICATOR:
2074 case UHF_C_PORT_ENABLE:
2075 case UHF_C_PORT_OVER_CURRENT:
2076 case UHF_C_PORT_RESET:
2079 case UHF_PORT_POWER:
2080 sc->sc_flags.port_powered = 0;
2081 uss820dci_pull_down(sc);
2083 case UHF_C_PORT_CONNECTION:
2084 sc->sc_flags.change_connect = 0;
2086 case UHF_C_PORT_SUSPEND:
2087 sc->sc_flags.change_suspend = 0;
2090 err = USB_ERR_IOERROR;
2095 tr_handle_set_port_feature:
2099 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
2102 case UHF_PORT_ENABLE:
2103 sc->sc_flags.port_enabled = 1;
2105 case UHF_PORT_SUSPEND:
2106 case UHF_PORT_RESET:
2108 case UHF_PORT_INDICATOR:
2111 case UHF_PORT_POWER:
2112 sc->sc_flags.port_powered = 1;
2115 err = USB_ERR_IOERROR;
2120 tr_handle_get_port_status:
2122 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
2127 if (sc->sc_flags.status_vbus) {
2128 uss820dci_pull_up(sc);
2130 uss820dci_pull_down(sc);
2133 /* Select FULL-speed and Device Side Mode */
2135 value = UPS_PORT_MODE_DEVICE;
2137 if (sc->sc_flags.port_powered) {
2138 value |= UPS_PORT_POWER;
2140 if (sc->sc_flags.port_enabled) {
2141 value |= UPS_PORT_ENABLED;
2143 if (sc->sc_flags.status_vbus &&
2144 sc->sc_flags.status_bus_reset) {
2145 value |= UPS_CURRENT_CONNECT_STATUS;
2147 if (sc->sc_flags.status_suspend) {
2148 value |= UPS_SUSPEND;
2150 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
2154 if (sc->sc_flags.change_connect) {
2155 value |= UPS_C_CONNECT_STATUS;
2157 if (sc->sc_flags.change_suspend) {
2158 value |= UPS_C_SUSPEND;
2160 USETW(sc->sc_hub_temp.ps.wPortChange, value);
2161 len = sizeof(sc->sc_hub_temp.ps);
2164 tr_handle_get_class_descriptor:
2168 ptr = (const void *)&uss820dci_hubd;
2169 len = sizeof(uss820dci_hubd);
2173 err = USB_ERR_STALLED;
2182 uss820dci_xfer_setup(struct usb2_setup_params *parm)
2184 const struct usb2_hw_ep_profile *pf;
2185 struct uss820dci_softc *sc;
2186 struct usb2_xfer *xfer;
2192 sc = USS820_DCI_BUS2SC(parm->udev->bus);
2193 xfer = parm->curr_xfer;
2196 * NOTE: This driver does not use any of the parameters that
2197 * are computed from the following values. Just set some
2198 * reasonable dummies:
2200 parm->hc_max_packet_size = 0x500;
2201 parm->hc_max_packet_count = 1;
2202 parm->hc_max_frame_size = 0x500;
2204 usb2_transfer_setup_sub(parm);
2207 * compute maximum number of TDs
2209 if (parm->methods == &uss820dci_device_ctrl_methods) {
2211 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
2213 } else if (parm->methods == &uss820dci_device_bulk_methods) {
2215 ntd = xfer->nframes + 1 /* SYNC */ ;
2217 } else if (parm->methods == &uss820dci_device_intr_methods) {
2219 ntd = xfer->nframes + 1 /* SYNC */ ;
2221 } else if (parm->methods == &uss820dci_device_isoc_fs_methods) {
2223 ntd = xfer->nframes + 1 /* SYNC */ ;
2231 * check if "usb2_transfer_setup_sub" set an error
2237 * allocate transfer descriptors
2246 ep_no = xfer->endpoint & UE_ADDR;
2247 uss820dci_get_hw_ep_profile(parm->udev, &pf, ep_no);
2250 /* should not happen */
2251 parm->err = USB_ERR_INVAL;
2260 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2262 for (n = 0; n != ntd; n++) {
2264 struct uss820dci_td *td;
2268 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2271 td->io_tag = sc->sc_io_tag;
2272 td->io_hdl = sc->sc_io_hdl;
2273 td->max_packet_size = xfer->max_packet_size;
2274 td->ep_index = ep_no;
2275 if (pf->support_multi_buffer &&
2276 (parm->methods != &uss820dci_device_ctrl_methods)) {
2277 td->support_multi_buffer = 1;
2279 td->obj_next = last_obj;
2283 parm->size[0] += sizeof(*td);
2286 xfer->td_start[0] = last_obj;
2290 uss820dci_xfer_unsetup(struct usb2_xfer *xfer)
2296 uss820dci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
2297 struct usb2_pipe *pipe)
2299 struct uss820dci_softc *sc = USS820_DCI_BUS2SC(udev->bus);
2301 DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
2302 pipe, udev->address,
2303 edesc->bEndpointAddress, udev->flags.usb_mode,
2306 if (udev->device_index != sc->sc_rt_addr) {
2308 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2312 if (udev->speed != USB_SPEED_FULL) {
2316 switch (edesc->bmAttributes & UE_XFERTYPE) {
2318 pipe->methods = &uss820dci_device_ctrl_methods;
2321 pipe->methods = &uss820dci_device_intr_methods;
2323 case UE_ISOCHRONOUS:
2324 pipe->methods = &uss820dci_device_isoc_fs_methods;
2327 pipe->methods = &uss820dci_device_bulk_methods;
2336 struct usb2_bus_methods uss820dci_bus_methods =
2338 .pipe_init = &uss820dci_pipe_init,
2339 .xfer_setup = &uss820dci_xfer_setup,
2340 .xfer_unsetup = &uss820dci_xfer_unsetup,
2341 .get_hw_ep_profile = &uss820dci_get_hw_ep_profile,
2342 .set_stall = &uss820dci_set_stall,
2343 .clear_stall = &uss820dci_clear_stall,
2344 .roothub_exec = &uss820dci_roothub_exec,