5 * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * This file contains the driver for the AT91 series USB Device
35 * Thanks to "David Brownell" for helping out regarding the hardware
40 * NOTE: The "fifo_bank" is not reset in hardware when the endpoint is
43 * NOTE: When the chip detects BUS-reset it will also reset the
44 * endpoints, Function-address and more.
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usb_mfunc.h>
49 #include <dev/usb/usb_error.h>
50 #include <dev/usb/usb_defs.h>
52 #define USB_DEBUG_VAR at91dcidebug
54 #include <dev/usb/usb_core.h>
55 #include <dev/usb/usb_debug.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_sw_transfer.h>
59 #include <dev/usb/usb_transfer.h>
60 #include <dev/usb/usb_device.h>
61 #include <dev/usb/usb_hub.h>
62 #include <dev/usb/usb_util.h>
64 #include <dev/usb/usb_controller.h>
65 #include <dev/usb/usb_bus.h>
66 #include <dev/usb/controller/at91dci.h>
68 #define AT9100_DCI_BUS2SC(bus) \
69 ((struct at91dci_softc *)(((uint8_t *)(bus)) - \
70 USB_P2U(&(((struct at91dci_softc *)0)->sc_bus))))
72 #define AT9100_DCI_PC2SC(pc) \
73 AT9100_DCI_BUS2SC((pc)->tag_parent->info->bus)
76 static int at91dcidebug = 0;
78 SYSCTL_NODE(_hw_usb2, OID_AUTO, at91dci, CTLFLAG_RW, 0, "USB at91dci");
79 SYSCTL_INT(_hw_usb2_at91dci, OID_AUTO, debug, CTLFLAG_RW,
80 &at91dcidebug, 0, "at91dci debug level");
83 #define AT9100_DCI_INTR_ENDPT 1
87 struct usb2_bus_methods at91dci_bus_methods;
88 struct usb2_pipe_methods at91dci_device_bulk_methods;
89 struct usb2_pipe_methods at91dci_device_ctrl_methods;
90 struct usb2_pipe_methods at91dci_device_intr_methods;
91 struct usb2_pipe_methods at91dci_device_isoc_fs_methods;
92 struct usb2_pipe_methods at91dci_root_ctrl_methods;
93 struct usb2_pipe_methods at91dci_root_intr_methods;
95 static at91dci_cmd_t at91dci_setup_rx;
96 static at91dci_cmd_t at91dci_data_rx;
97 static at91dci_cmd_t at91dci_data_tx;
98 static at91dci_cmd_t at91dci_data_tx_sync;
99 static void at91dci_device_done(struct usb2_xfer *, usb2_error_t);
100 static void at91dci_do_poll(struct usb2_bus *);
101 static void at91dci_root_ctrl_poll(struct at91dci_softc *);
102 static void at91dci_standard_done(struct usb2_xfer *);
104 static usb2_sw_transfer_func_t at91dci_root_intr_done;
105 static usb2_sw_transfer_func_t at91dci_root_ctrl_done;
108 * NOTE: Some of the bits in the CSR register have inverse meaning so
109 * we need a helper macro when acknowledging events:
111 #define AT91_CSR_ACK(csr, what) do { \
112 (csr) &= ~((AT91_UDP_CSR_FORCESTALL| \
113 AT91_UDP_CSR_TXPKTRDY| \
114 AT91_UDP_CSR_RXBYTECNT) ^ (what));\
115 (csr) |= ((AT91_UDP_CSR_RX_DATA_BK0| \
116 AT91_UDP_CSR_RX_DATA_BK1| \
117 AT91_UDP_CSR_TXCOMP| \
118 AT91_UDP_CSR_RXSETUP| \
119 AT91_UDP_CSR_STALLSENT) ^ (what)); \
123 * Here is a list of what the chip supports.
124 * Probably it supports more than listed here!
126 static const struct usb2_hw_ep_profile
127 at91dci_ep_profile[AT91_UDP_EP_MAX] = {
130 .max_in_frame_size = 8,
131 .max_out_frame_size = 8,
133 .support_control = 1,
136 .max_in_frame_size = 64,
137 .max_out_frame_size = 64,
139 .support_multi_buffer = 1,
141 .support_interrupt = 1,
142 .support_isochronous = 1,
147 .max_in_frame_size = 64,
148 .max_out_frame_size = 64,
150 .support_multi_buffer = 1,
152 .support_interrupt = 1,
153 .support_isochronous = 1,
158 /* can also do BULK */
159 .max_in_frame_size = 8,
160 .max_out_frame_size = 8,
162 .support_interrupt = 1,
167 .max_in_frame_size = 256,
168 .max_out_frame_size = 256,
170 .support_multi_buffer = 1,
172 .support_interrupt = 1,
173 .support_isochronous = 1,
178 .max_in_frame_size = 256,
179 .max_out_frame_size = 256,
181 .support_multi_buffer = 1,
183 .support_interrupt = 1,
184 .support_isochronous = 1,
191 at91dci_get_hw_ep_profile(struct usb2_device *udev,
192 const struct usb2_hw_ep_profile **ppf, uint8_t ep_addr)
194 if (ep_addr < AT91_UDP_EP_MAX) {
195 *ppf = (at91dci_ep_profile + ep_addr);
202 at91dci_clocks_on(struct at91dci_softc *sc)
204 if (sc->sc_flags.clocks_off &&
205 sc->sc_flags.port_powered) {
209 if (sc->sc_clocks_on) {
210 (sc->sc_clocks_on) (sc->sc_clocks_arg);
212 sc->sc_flags.clocks_off = 0;
214 /* enable Transceiver */
215 AT91_UDP_WRITE_4(sc, AT91_UDP_TXVC, 0);
220 at91dci_clocks_off(struct at91dci_softc *sc)
222 if (!sc->sc_flags.clocks_off) {
226 /* disable Transceiver */
227 AT91_UDP_WRITE_4(sc, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS);
229 if (sc->sc_clocks_off) {
230 (sc->sc_clocks_off) (sc->sc_clocks_arg);
232 sc->sc_flags.clocks_off = 1;
237 at91dci_pull_up(struct at91dci_softc *sc)
239 /* pullup D+, if possible */
241 if (!sc->sc_flags.d_pulled_up &&
242 sc->sc_flags.port_powered) {
243 sc->sc_flags.d_pulled_up = 1;
244 (sc->sc_pull_up) (sc->sc_pull_arg);
249 at91dci_pull_down(struct at91dci_softc *sc)
251 /* pulldown D+, if possible */
253 if (sc->sc_flags.d_pulled_up) {
254 sc->sc_flags.d_pulled_up = 0;
255 (sc->sc_pull_down) (sc->sc_pull_arg);
260 at91dci_wakeup_peer(struct usb2_xfer *xfer)
262 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
265 if (!(sc->sc_flags.status_suspend)) {
268 use_polling = mtx_owned(xfer->xroot->xfer_mtx) ? 1 : 0;
270 AT91_UDP_WRITE_4(sc, AT91_UDP_GSTATE, AT91_UDP_GSTATE_ESR);
272 /* wait 8 milliseconds */
277 /* Wait for reset to complete. */
278 usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
281 AT91_UDP_WRITE_4(sc, AT91_UDP_GSTATE, 0);
285 at91dci_set_address(struct at91dci_softc *sc, uint8_t addr)
287 DPRINTFN(5, "addr=%d\n", addr);
289 AT91_UDP_WRITE_4(sc, AT91_UDP_FADDR, addr |
294 at91dci_setup_rx(struct at91dci_td *td)
296 struct at91dci_softc *sc;
297 struct usb2_device_request req;
302 /* read out FIFO status */
303 csr = bus_space_read_4(td->io_tag, td->io_hdl,
306 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
309 temp &= (AT91_UDP_CSR_RX_DATA_BK0 |
310 AT91_UDP_CSR_RX_DATA_BK1 |
311 AT91_UDP_CSR_STALLSENT |
312 AT91_UDP_CSR_RXSETUP |
313 AT91_UDP_CSR_TXCOMP);
315 if (!(csr & AT91_UDP_CSR_RXSETUP)) {
316 /* abort any ongoing transfer */
317 if (!td->did_stall) {
318 DPRINTFN(5, "stalling\n");
319 temp |= AT91_UDP_CSR_FORCESTALL;
324 /* get the packet byte count */
325 count = (csr & AT91_UDP_CSR_RXBYTECNT) >> 16;
327 /* verify data length */
328 if (count != td->remainder) {
329 DPRINTFN(0, "Invalid SETUP packet "
330 "length, %d bytes\n", count);
333 if (count != sizeof(req)) {
334 DPRINTFN(0, "Unsupported SETUP packet "
335 "length, %d bytes\n", count);
339 bus_space_read_multi_1(td->io_tag, td->io_hdl,
340 td->fifo_reg, (void *)&req, sizeof(req));
342 /* copy data into real buffer */
343 usb2_copy_in(td->pc, 0, &req, sizeof(req));
345 td->offset = sizeof(req);
348 /* get pointer to softc */
349 sc = AT9100_DCI_PC2SC(td->pc);
351 /* sneak peek the set address */
352 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
353 (req.bRequest == UR_SET_ADDRESS)) {
354 sc->sc_dv_addr = req.wValue[0] & 0x7F;
356 sc->sc_dv_addr = 0xFF;
359 /* sneak peek the endpoint direction */
360 if (req.bmRequestType & UE_DIR_IN) {
361 csr |= AT91_UDP_CSR_DIR;
363 csr &= ~AT91_UDP_CSR_DIR;
366 /* write the direction of the control transfer */
367 AT91_CSR_ACK(csr, temp);
368 bus_space_write_4(td->io_tag, td->io_hdl,
369 td->status_reg, csr);
370 return (0); /* complete */
373 /* clear interrupts, if any */
375 DPRINTFN(5, "clearing 0x%08x\n", temp);
376 AT91_CSR_ACK(csr, temp);
377 bus_space_write_4(td->io_tag, td->io_hdl,
378 td->status_reg, csr);
380 return (1); /* not complete */
385 at91dci_data_rx(struct at91dci_td *td)
387 struct usb2_page_search buf_res;
394 to = 2; /* don't loop forever! */
397 /* check if any of the FIFO banks have data */
399 /* read out FIFO status */
400 csr = bus_space_read_4(td->io_tag, td->io_hdl,
403 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
405 if (csr & AT91_UDP_CSR_RXSETUP) {
406 if (td->remainder == 0) {
408 * We are actually complete and have
409 * received the next SETUP
411 DPRINTFN(5, "faking complete\n");
412 return (0); /* complete */
415 * USB Host Aborted the transfer.
418 return (0); /* complete */
420 /* Make sure that "STALLSENT" gets cleared */
422 temp &= AT91_UDP_CSR_STALLSENT;
425 if (!(csr & (AT91_UDP_CSR_RX_DATA_BK0 |
426 AT91_UDP_CSR_RX_DATA_BK1))) {
429 AT91_CSR_ACK(csr, temp);
430 bus_space_write_4(td->io_tag, td->io_hdl,
431 td->status_reg, csr);
433 return (1); /* not complete */
435 /* get the packet byte count */
436 count = (csr & AT91_UDP_CSR_RXBYTECNT) >> 16;
438 /* verify the packet byte count */
439 if (count != td->max_packet_size) {
440 if (count < td->max_packet_size) {
441 /* we have a short packet */
445 /* invalid USB packet */
447 return (0); /* we are complete */
450 /* verify the packet byte count */
451 if (count > td->remainder) {
452 /* invalid USB packet */
454 return (0); /* we are complete */
457 usb2_get_page(td->pc, td->offset, &buf_res);
459 /* get correct length */
460 if (buf_res.length > count) {
461 buf_res.length = count;
464 bus_space_read_multi_1(td->io_tag, td->io_hdl,
465 td->fifo_reg, buf_res.buffer, buf_res.length);
467 /* update counters */
468 count -= buf_res.length;
469 td->offset += buf_res.length;
470 td->remainder -= buf_res.length;
473 /* clear status bits */
474 if (td->support_multi_buffer) {
477 temp |= AT91_UDP_CSR_RX_DATA_BK1;
480 temp |= AT91_UDP_CSR_RX_DATA_BK0;
483 temp |= (AT91_UDP_CSR_RX_DATA_BK0 |
484 AT91_UDP_CSR_RX_DATA_BK1);
488 AT91_CSR_ACK(csr, temp);
489 bus_space_write_4(td->io_tag, td->io_hdl,
490 td->status_reg, csr);
493 * NOTE: We may have to delay a little bit before
494 * proceeding after clearing the DATA_BK bits.
497 /* check if we are complete */
498 if ((td->remainder == 0) || got_short) {
500 /* we are complete */
503 /* else need to receive a zero length packet */
508 return (1); /* not complete */
512 at91dci_data_tx(struct at91dci_td *td)
514 struct usb2_page_search buf_res;
520 to = 2; /* don't loop forever! */
524 /* read out FIFO status */
525 csr = bus_space_read_4(td->io_tag, td->io_hdl,
528 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
530 if (csr & AT91_UDP_CSR_RXSETUP) {
532 * The current transfer was aborted
536 return (0); /* complete */
538 /* Make sure that "STALLSENT" gets cleared */
540 temp &= AT91_UDP_CSR_STALLSENT;
542 if (csr & AT91_UDP_CSR_TXPKTRDY) {
545 AT91_CSR_ACK(csr, temp);
546 bus_space_write_4(td->io_tag, td->io_hdl,
547 td->status_reg, csr);
549 return (1); /* not complete */
551 /* clear TXCOMP and set TXPKTRDY */
552 temp |= (AT91_UDP_CSR_TXCOMP |
553 AT91_UDP_CSR_TXPKTRDY);
556 count = td->max_packet_size;
557 if (td->remainder < count) {
558 /* we have a short packet */
560 count = td->remainder;
564 usb2_get_page(td->pc, td->offset, &buf_res);
566 /* get correct length */
567 if (buf_res.length > count) {
568 buf_res.length = count;
571 bus_space_write_multi_1(td->io_tag, td->io_hdl,
572 td->fifo_reg, buf_res.buffer, buf_res.length);
574 /* update counters */
575 count -= buf_res.length;
576 td->offset += buf_res.length;
577 td->remainder -= buf_res.length;
581 AT91_CSR_ACK(csr, temp);
582 bus_space_write_4(td->io_tag, td->io_hdl,
583 td->status_reg, csr);
585 /* check remainder */
586 if (td->remainder == 0) {
588 return (0); /* complete */
590 /* else we need to transmit a short packet */
595 return (1); /* not complete */
599 at91dci_data_tx_sync(struct at91dci_td *td)
601 struct at91dci_softc *sc;
609 /* read out FIFO status */
610 csr = bus_space_read_4(td->io_tag, td->io_hdl,
613 DPRINTFN(5, "csr=0x%08x\n", csr);
615 if (csr & AT91_UDP_CSR_RXSETUP) {
616 DPRINTFN(5, "faking complete\n");
618 return (0); /* complete */
621 temp &= (AT91_UDP_CSR_STALLSENT |
622 AT91_UDP_CSR_TXCOMP);
625 if (csr & AT91_UDP_CSR_TXPKTRDY) {
628 if (!(csr & AT91_UDP_CSR_TXCOMP)) {
631 sc = AT9100_DCI_PC2SC(td->pc);
632 if (sc->sc_dv_addr != 0xFF) {
634 * The AT91 has a special requirement with regard to
635 * setting the address and that is to write the new
636 * address before clearing TXCOMP:
638 at91dci_set_address(sc, sc->sc_dv_addr);
641 AT91_CSR_ACK(csr, temp);
642 bus_space_write_4(td->io_tag, td->io_hdl,
643 td->status_reg, csr);
645 return (0); /* complete */
650 AT91_CSR_ACK(csr, temp);
651 bus_space_write_4(td->io_tag, td->io_hdl,
652 td->status_reg, csr);
654 return (1); /* not complete */
658 at91dci_xfer_do_fifo(struct usb2_xfer *xfer)
660 struct at91dci_softc *sc;
661 struct at91dci_td *td;
666 td = xfer->td_transfer_cache;
668 if ((td->func) (td)) {
669 /* operation in progress */
672 if (((void *)td) == xfer->td_transfer_last) {
677 } else if (td->remainder > 0) {
679 * We had a short transfer. If there is no alternate
680 * next, stop processing !
687 * Fetch the next transfer descriptor and transfer
688 * some flags to the next transfer descriptor
694 xfer->td_transfer_cache = td;
698 return (1); /* not complete */
701 sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
702 temp = (xfer->endpoint & UE_ADDR);
704 /* update FIFO bank flag and multi buffer */
706 sc->sc_ep_flags[temp].fifo_bank = 1;
708 sc->sc_ep_flags[temp].fifo_bank = 0;
711 /* compute all actual lengths */
713 at91dci_standard_done(xfer);
715 return (0); /* complete */
719 at91dci_interrupt_poll(struct at91dci_softc *sc)
721 struct usb2_xfer *xfer;
724 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
725 if (!at91dci_xfer_do_fifo(xfer)) {
726 /* queue has been modified */
733 at91dci_vbus_interrupt(struct at91dci_softc *sc, uint8_t is_on)
735 DPRINTFN(5, "vbus = %u\n", is_on);
737 USB_BUS_LOCK(&sc->sc_bus);
739 if (!sc->sc_flags.status_vbus) {
740 sc->sc_flags.status_vbus = 1;
742 /* complete root HUB interrupt endpoint */
744 usb2_sw_transfer(&sc->sc_root_intr,
745 &at91dci_root_intr_done);
748 if (sc->sc_flags.status_vbus) {
749 sc->sc_flags.status_vbus = 0;
750 sc->sc_flags.status_bus_reset = 0;
751 sc->sc_flags.status_suspend = 0;
752 sc->sc_flags.change_suspend = 0;
753 sc->sc_flags.change_connect = 1;
755 /* complete root HUB interrupt endpoint */
757 usb2_sw_transfer(&sc->sc_root_intr,
758 &at91dci_root_intr_done);
761 USB_BUS_UNLOCK(&sc->sc_bus);
765 at91dci_interrupt(struct at91dci_softc *sc)
769 USB_BUS_LOCK(&sc->sc_bus);
771 status = AT91_UDP_READ_4(sc, AT91_UDP_ISR);
772 status &= AT91_UDP_INT_DEFAULT;
775 USB_BUS_UNLOCK(&sc->sc_bus);
778 /* acknowledge interrupts */
780 AT91_UDP_WRITE_4(sc, AT91_UDP_ICR, status);
782 /* check for any bus state change interrupts */
784 if (status & AT91_UDP_INT_BUS) {
786 DPRINTFN(5, "real bus interrupt 0x%08x\n", status);
788 if (status & AT91_UDP_INT_END_BR) {
790 /* set correct state */
791 sc->sc_flags.status_bus_reset = 1;
792 sc->sc_flags.status_suspend = 0;
793 sc->sc_flags.change_suspend = 0;
794 sc->sc_flags.change_connect = 1;
796 /* disable resume interrupt */
797 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
799 /* enable suspend interrupt */
800 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
801 AT91_UDP_INT_RXSUSP);
804 * If RXRSM and RXSUSP is set at the same time we interpret
805 * that like RESUME. Resume is set when there is at least 3
806 * milliseconds of inactivity on the USB BUS.
808 if (status & AT91_UDP_INT_RXRSM) {
809 if (sc->sc_flags.status_suspend) {
810 sc->sc_flags.status_suspend = 0;
811 sc->sc_flags.change_suspend = 1;
813 /* disable resume interrupt */
814 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
816 /* enable suspend interrupt */
817 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
818 AT91_UDP_INT_RXSUSP);
820 } else if (status & AT91_UDP_INT_RXSUSP) {
821 if (!sc->sc_flags.status_suspend) {
822 sc->sc_flags.status_suspend = 1;
823 sc->sc_flags.change_suspend = 1;
825 /* disable suspend interrupt */
826 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
827 AT91_UDP_INT_RXSUSP);
829 /* enable resume interrupt */
830 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
834 /* complete root HUB interrupt endpoint */
836 usb2_sw_transfer(&sc->sc_root_intr,
837 &at91dci_root_intr_done);
839 /* check for any endpoint interrupts */
841 if (status & AT91_UDP_INT_EPS) {
843 DPRINTFN(5, "real endpoint interrupt 0x%08x\n", status);
845 at91dci_interrupt_poll(sc);
847 USB_BUS_UNLOCK(&sc->sc_bus);
851 at91dci_setup_standard_chain_sub(struct at91dci_std_temp *temp)
853 struct at91dci_td *td;
855 /* get current Transfer Descriptor */
859 /* prepare for next TD */
860 temp->td_next = td->obj_next;
862 /* fill out the Transfer Descriptor */
863 td->func = temp->func;
865 td->offset = temp->offset;
866 td->remainder = temp->len;
870 td->short_pkt = temp->short_pkt;
871 td->alt_next = temp->setup_alt_next;
875 at91dci_setup_standard_chain(struct usb2_xfer *xfer)
877 struct at91dci_std_temp temp;
878 struct at91dci_softc *sc;
879 struct at91dci_td *td;
884 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
885 xfer->address, UE_GET_ADDR(xfer->endpoint),
886 xfer->sumlen, usb2_get_speed(xfer->xroot->udev));
888 temp.max_frame_size = xfer->max_frame_size;
890 td = xfer->td_start[0];
891 xfer->td_transfer_first = td;
892 xfer->td_transfer_cache = td;
897 temp.td_next = xfer->td_start[0];
898 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
901 sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
902 ep_no = (xfer->endpoint & UE_ADDR);
904 /* check if we should prepend a setup message */
906 if (xfer->flags_int.control_xfr) {
907 if (xfer->flags_int.control_hdr) {
909 temp.func = &at91dci_setup_rx;
910 temp.len = xfer->frlengths[0];
911 temp.pc = xfer->frbuffers + 0;
912 temp.short_pkt = temp.len ? 1 : 0;
914 at91dci_setup_standard_chain_sub(&temp);
921 if (x != xfer->nframes) {
922 if (xfer->endpoint & UE_DIR_IN) {
923 temp.func = &at91dci_data_tx;
926 temp.func = &at91dci_data_rx;
930 /* setup "pc" pointer */
931 temp.pc = xfer->frbuffers + x;
935 while (x != xfer->nframes) {
937 /* DATA0 / DATA1 message */
939 temp.len = xfer->frlengths[x];
943 if (x == xfer->nframes) {
944 temp.setup_alt_next = 0;
948 /* make sure that we send an USB packet */
954 /* regular data transfer */
956 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
959 at91dci_setup_standard_chain_sub(&temp);
961 if (xfer->flags_int.isochronous_xfr) {
962 temp.offset += temp.len;
964 /* get next Page Cache pointer */
965 temp.pc = xfer->frbuffers + x;
969 /* always setup a valid "pc" pointer for status and sync */
970 temp.pc = xfer->frbuffers + 0;
972 /* check if we need to sync */
973 if (need_sync && xfer->flags_int.control_xfr) {
975 /* we need a SYNC point after TX */
976 temp.func = &at91dci_data_tx_sync;
980 at91dci_setup_standard_chain_sub(&temp);
982 /* check if we should append a status stage */
983 if (xfer->flags_int.control_xfr &&
984 !xfer->flags_int.control_act) {
987 * Send a DATA1 message and invert the current
988 * endpoint direction.
990 if (xfer->endpoint & UE_DIR_IN) {
991 temp.func = &at91dci_data_rx;
994 temp.func = &at91dci_data_tx;
1000 at91dci_setup_standard_chain_sub(&temp);
1002 /* we need a SYNC point after TX */
1003 temp.func = &at91dci_data_tx_sync;
1007 at91dci_setup_standard_chain_sub(&temp);
1010 /* must have at least one frame! */
1012 xfer->td_transfer_last = td;
1014 /* setup the correct fifo bank */
1015 if (sc->sc_ep_flags[ep_no].fifo_bank) {
1016 td = xfer->td_transfer_first;
1022 at91dci_timeout(void *arg)
1024 struct usb2_xfer *xfer = arg;
1026 DPRINTF("xfer=%p\n", xfer);
1028 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1030 /* transfer is transferred */
1031 at91dci_device_done(xfer, USB_ERR_TIMEOUT);
1035 at91dci_start_standard_chain(struct usb2_xfer *xfer)
1040 if (at91dci_xfer_do_fifo(xfer)) {
1042 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1043 uint8_t ep_no = xfer->endpoint & UE_ADDR;
1046 * Only enable the endpoint interrupt when we are actually
1047 * waiting for data, hence we are dealing with level
1048 * triggered interrupts !
1050 AT91_UDP_WRITE_4(sc, AT91_UDP_IER, AT91_UDP_INT_EP(ep_no));
1052 DPRINTFN(15, "enable interrupts on endpoint %d\n", ep_no);
1054 /* put transfer on interrupt queue */
1055 usb2_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1057 /* start timeout, if any */
1058 if (xfer->timeout != 0) {
1059 usb2_transfer_timeout_ms(xfer,
1060 &at91dci_timeout, xfer->timeout);
1066 at91dci_root_intr_done(struct usb2_xfer *xfer,
1067 struct usb2_sw_transfer *std)
1069 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1073 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1075 if (std->state != USB_SW_TR_PRE_DATA) {
1076 if (std->state == USB_SW_TR_PRE_CALLBACK) {
1077 /* transfer transferred */
1078 at91dci_device_done(xfer, std->err);
1083 std->ptr = sc->sc_hub_idata;
1084 std->len = sizeof(sc->sc_hub_idata);
1087 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
1094 at91dci_standard_done_sub(struct usb2_xfer *xfer)
1096 struct at91dci_td *td;
1102 td = xfer->td_transfer_cache;
1105 len = td->remainder;
1107 if (xfer->aframes != xfer->nframes) {
1109 * Verify the length and subtract
1110 * the remainder from "frlengths[]":
1112 if (len > xfer->frlengths[xfer->aframes]) {
1115 xfer->frlengths[xfer->aframes] -= len;
1118 /* Check for transfer error */
1120 /* the transfer is finished */
1125 /* Check for short transfer */
1127 if (xfer->flags_int.short_frames_ok) {
1128 /* follow alt next */
1135 /* the transfer is finished */
1143 /* this USB frame is complete */
1149 /* update transfer cache */
1151 xfer->td_transfer_cache = td;
1154 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1158 at91dci_standard_done(struct usb2_xfer *xfer)
1160 usb2_error_t err = 0;
1162 DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1167 xfer->td_transfer_cache = xfer->td_transfer_first;
1169 if (xfer->flags_int.control_xfr) {
1171 if (xfer->flags_int.control_hdr) {
1173 err = at91dci_standard_done_sub(xfer);
1177 if (xfer->td_transfer_cache == NULL) {
1181 while (xfer->aframes != xfer->nframes) {
1183 err = at91dci_standard_done_sub(xfer);
1186 if (xfer->td_transfer_cache == NULL) {
1191 if (xfer->flags_int.control_xfr &&
1192 !xfer->flags_int.control_act) {
1194 err = at91dci_standard_done_sub(xfer);
1197 at91dci_device_done(xfer, err);
1200 /*------------------------------------------------------------------------*
1201 * at91dci_device_done
1203 * NOTE: this function can be called more than one time on the
1204 * same USB transfer!
1205 *------------------------------------------------------------------------*/
1207 at91dci_device_done(struct usb2_xfer *xfer, usb2_error_t error)
1209 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1212 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1214 DPRINTFN(2, "xfer=%p, pipe=%p, error=%d\n",
1215 xfer, xfer->pipe, error);
1217 if (xfer->flags_int.usb2_mode == USB_MODE_DEVICE) {
1218 ep_no = (xfer->endpoint & UE_ADDR);
1220 /* disable endpoint interrupt */
1221 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR, AT91_UDP_INT_EP(ep_no));
1223 DPRINTFN(15, "disable interrupts on endpoint %d\n", ep_no);
1225 /* dequeue transfer and start next transfer */
1226 usb2_transfer_done(xfer, error);
1230 at91dci_set_stall(struct usb2_device *udev, struct usb2_xfer *xfer,
1231 struct usb2_pipe *pipe)
1233 struct at91dci_softc *sc;
1237 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1239 DPRINTFN(5, "pipe=%p\n", pipe);
1242 /* cancel any ongoing transfers */
1243 at91dci_device_done(xfer, USB_ERR_STALLED);
1245 /* set FORCESTALL */
1246 sc = AT9100_DCI_BUS2SC(udev->bus);
1247 csr_reg = (pipe->edesc->bEndpointAddress & UE_ADDR);
1248 csr_reg = AT91_UDP_CSR(csr_reg);
1249 csr_val = AT91_UDP_READ_4(sc, csr_reg);
1250 AT91_CSR_ACK(csr_val, AT91_UDP_CSR_FORCESTALL);
1251 AT91_UDP_WRITE_4(sc, csr_reg, csr_val);
1255 at91dci_clear_stall_sub(struct at91dci_softc *sc, uint8_t ep_no,
1256 uint8_t ep_type, uint8_t ep_dir)
1258 const struct usb2_hw_ep_profile *pf;
1264 if (ep_type == UE_CONTROL) {
1265 /* clearing stall is not needed */
1268 /* compute CSR register offset */
1269 csr_reg = AT91_UDP_CSR(ep_no);
1271 /* compute default CSR value */
1273 AT91_CSR_ACK(csr_val, 0);
1275 /* disable endpoint */
1276 AT91_UDP_WRITE_4(sc, csr_reg, csr_val);
1278 /* get endpoint profile */
1279 at91dci_get_hw_ep_profile(NULL, &pf, ep_no);
1282 AT91_UDP_WRITE_4(sc, AT91_UDP_RST, AT91_UDP_RST_EP(ep_no));
1283 AT91_UDP_WRITE_4(sc, AT91_UDP_RST, 0);
1286 * NOTE: One would assume that a FIFO reset would release the
1287 * FIFO banks aswell, but it doesn't! We have to do this
1291 /* release FIFO banks, if any */
1292 for (to = 0; to != 2; to++) {
1295 csr_val = AT91_UDP_READ_4(sc, csr_reg);
1297 if (csr_val & (AT91_UDP_CSR_RX_DATA_BK0 |
1298 AT91_UDP_CSR_RX_DATA_BK1)) {
1299 /* clear status bits */
1300 if (pf->support_multi_buffer) {
1301 if (sc->sc_ep_flags[ep_no].fifo_bank) {
1302 sc->sc_ep_flags[ep_no].fifo_bank = 0;
1303 temp = AT91_UDP_CSR_RX_DATA_BK1;
1305 sc->sc_ep_flags[ep_no].fifo_bank = 1;
1306 temp = AT91_UDP_CSR_RX_DATA_BK0;
1309 temp = (AT91_UDP_CSR_RX_DATA_BK0 |
1310 AT91_UDP_CSR_RX_DATA_BK1);
1316 /* clear FORCESTALL */
1317 temp |= AT91_UDP_CSR_STALLSENT;
1319 AT91_CSR_ACK(csr_val, temp);
1320 AT91_UDP_WRITE_4(sc, csr_reg, csr_val);
1323 /* compute default CSR value */
1325 AT91_CSR_ACK(csr_val, 0);
1327 /* enable endpoint */
1328 csr_val &= ~AT91_UDP_CSR_ET_MASK;
1329 csr_val |= AT91_UDP_CSR_EPEDS;
1331 if (ep_type == UE_CONTROL) {
1332 csr_val |= AT91_UDP_CSR_ET_CTRL;
1334 if (ep_type == UE_BULK) {
1335 csr_val |= AT91_UDP_CSR_ET_BULK;
1336 } else if (ep_type == UE_INTERRUPT) {
1337 csr_val |= AT91_UDP_CSR_ET_INT;
1339 csr_val |= AT91_UDP_CSR_ET_ISO;
1341 if (ep_dir & UE_DIR_IN) {
1342 csr_val |= AT91_UDP_CSR_ET_DIR_IN;
1346 /* enable endpoint */
1347 AT91_UDP_WRITE_4(sc, AT91_UDP_CSR(ep_no), csr_val);
1351 at91dci_clear_stall(struct usb2_device *udev, struct usb2_pipe *pipe)
1353 struct at91dci_softc *sc;
1354 struct usb2_endpoint_descriptor *ed;
1356 DPRINTFN(5, "pipe=%p\n", pipe);
1358 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1361 if (udev->flags.usb2_mode != USB_MODE_DEVICE) {
1366 sc = AT9100_DCI_BUS2SC(udev->bus);
1368 /* get endpoint descriptor */
1371 /* reset endpoint */
1372 at91dci_clear_stall_sub(sc,
1373 (ed->bEndpointAddress & UE_ADDR),
1374 (ed->bmAttributes & UE_XFERTYPE),
1375 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1379 at91dci_init(struct at91dci_softc *sc)
1386 /* set up the bus structure */
1387 sc->sc_bus.usbrev = USB_REV_1_1;
1388 sc->sc_bus.methods = &at91dci_bus_methods;
1390 USB_BUS_LOCK(&sc->sc_bus);
1392 /* turn on clocks */
1394 if (sc->sc_clocks_on) {
1395 (sc->sc_clocks_on) (sc->sc_clocks_arg);
1397 /* wait a little for things to stabilise */
1398 usb2_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
1400 /* disable and clear all interrupts */
1402 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR, 0xFFFFFFFF);
1403 AT91_UDP_WRITE_4(sc, AT91_UDP_ICR, 0xFFFFFFFF);
1405 /* compute default CSR value */
1408 AT91_CSR_ACK(csr_val, 0);
1410 /* disable all endpoints */
1412 for (n = 0; n != AT91_UDP_EP_MAX; n++) {
1414 /* disable endpoint */
1415 AT91_UDP_WRITE_4(sc, AT91_UDP_CSR(n), csr_val);
1418 /* enable the control endpoint */
1420 AT91_CSR_ACK(csr_val, AT91_UDP_CSR_ET_CTRL |
1421 AT91_UDP_CSR_EPEDS);
1423 /* write to FIFO control register */
1425 AT91_UDP_WRITE_4(sc, AT91_UDP_CSR(0), csr_val);
1427 /* enable the interrupts we want */
1429 AT91_UDP_WRITE_4(sc, AT91_UDP_IER, AT91_UDP_INT_BUS);
1431 /* turn off clocks */
1433 at91dci_clocks_off(sc);
1435 USB_BUS_UNLOCK(&sc->sc_bus);
1437 /* catch any lost interrupts */
1439 at91dci_do_poll(&sc->sc_bus);
1441 return (0); /* success */
1445 at91dci_uninit(struct at91dci_softc *sc)
1447 USB_BUS_LOCK(&sc->sc_bus);
1449 /* disable and clear all interrupts */
1450 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR, 0xFFFFFFFF);
1451 AT91_UDP_WRITE_4(sc, AT91_UDP_ICR, 0xFFFFFFFF);
1453 sc->sc_flags.port_powered = 0;
1454 sc->sc_flags.status_vbus = 0;
1455 sc->sc_flags.status_bus_reset = 0;
1456 sc->sc_flags.status_suspend = 0;
1457 sc->sc_flags.change_suspend = 0;
1458 sc->sc_flags.change_connect = 1;
1460 at91dci_pull_down(sc);
1461 at91dci_clocks_off(sc);
1462 USB_BUS_UNLOCK(&sc->sc_bus);
1466 at91dci_suspend(struct at91dci_softc *sc)
1472 at91dci_resume(struct at91dci_softc *sc)
1478 at91dci_do_poll(struct usb2_bus *bus)
1480 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(bus);
1482 USB_BUS_LOCK(&sc->sc_bus);
1483 at91dci_interrupt_poll(sc);
1484 at91dci_root_ctrl_poll(sc);
1485 USB_BUS_UNLOCK(&sc->sc_bus);
1488 /*------------------------------------------------------------------------*
1489 * at91dci bulk support
1490 *------------------------------------------------------------------------*/
1492 at91dci_device_bulk_open(struct usb2_xfer *xfer)
1498 at91dci_device_bulk_close(struct usb2_xfer *xfer)
1500 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1504 at91dci_device_bulk_enter(struct usb2_xfer *xfer)
1510 at91dci_device_bulk_start(struct usb2_xfer *xfer)
1513 at91dci_setup_standard_chain(xfer);
1514 at91dci_start_standard_chain(xfer);
1517 struct usb2_pipe_methods at91dci_device_bulk_methods =
1519 .open = at91dci_device_bulk_open,
1520 .close = at91dci_device_bulk_close,
1521 .enter = at91dci_device_bulk_enter,
1522 .start = at91dci_device_bulk_start,
1523 .enter_is_cancelable = 1,
1524 .start_is_cancelable = 1,
1527 /*------------------------------------------------------------------------*
1528 * at91dci control support
1529 *------------------------------------------------------------------------*/
1531 at91dci_device_ctrl_open(struct usb2_xfer *xfer)
1537 at91dci_device_ctrl_close(struct usb2_xfer *xfer)
1539 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1543 at91dci_device_ctrl_enter(struct usb2_xfer *xfer)
1549 at91dci_device_ctrl_start(struct usb2_xfer *xfer)
1552 at91dci_setup_standard_chain(xfer);
1553 at91dci_start_standard_chain(xfer);
1556 struct usb2_pipe_methods at91dci_device_ctrl_methods =
1558 .open = at91dci_device_ctrl_open,
1559 .close = at91dci_device_ctrl_close,
1560 .enter = at91dci_device_ctrl_enter,
1561 .start = at91dci_device_ctrl_start,
1562 .enter_is_cancelable = 1,
1563 .start_is_cancelable = 1,
1566 /*------------------------------------------------------------------------*
1567 * at91dci interrupt support
1568 *------------------------------------------------------------------------*/
1570 at91dci_device_intr_open(struct usb2_xfer *xfer)
1576 at91dci_device_intr_close(struct usb2_xfer *xfer)
1578 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1582 at91dci_device_intr_enter(struct usb2_xfer *xfer)
1588 at91dci_device_intr_start(struct usb2_xfer *xfer)
1591 at91dci_setup_standard_chain(xfer);
1592 at91dci_start_standard_chain(xfer);
1595 struct usb2_pipe_methods at91dci_device_intr_methods =
1597 .open = at91dci_device_intr_open,
1598 .close = at91dci_device_intr_close,
1599 .enter = at91dci_device_intr_enter,
1600 .start = at91dci_device_intr_start,
1601 .enter_is_cancelable = 1,
1602 .start_is_cancelable = 1,
1605 /*------------------------------------------------------------------------*
1606 * at91dci full speed isochronous support
1607 *------------------------------------------------------------------------*/
1609 at91dci_device_isoc_fs_open(struct usb2_xfer *xfer)
1615 at91dci_device_isoc_fs_close(struct usb2_xfer *xfer)
1617 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1621 at91dci_device_isoc_fs_enter(struct usb2_xfer *xfer)
1623 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1627 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1628 xfer, xfer->pipe->isoc_next, xfer->nframes);
1630 /* get the current frame index */
1632 nframes = AT91_UDP_READ_4(sc, AT91_UDP_FRM);
1635 * check if the frame index is within the window where the frames
1638 temp = (nframes - xfer->pipe->isoc_next) & AT91_UDP_FRM_MASK;
1640 if ((xfer->pipe->is_synced == 0) ||
1641 (temp < xfer->nframes)) {
1643 * If there is data underflow or the pipe queue is
1644 * empty we schedule the transfer a few frames ahead
1645 * of the current frame position. Else two isochronous
1646 * transfers might overlap.
1648 xfer->pipe->isoc_next = (nframes + 3) & AT91_UDP_FRM_MASK;
1649 xfer->pipe->is_synced = 1;
1650 DPRINTFN(3, "start next=%d\n", xfer->pipe->isoc_next);
1653 * compute how many milliseconds the insertion is ahead of the
1654 * current frame position:
1656 temp = (xfer->pipe->isoc_next - nframes) & AT91_UDP_FRM_MASK;
1659 * pre-compute when the isochronous transfer will be finished:
1661 xfer->isoc_time_complete =
1662 usb2_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1665 /* compute frame number for next insertion */
1666 xfer->pipe->isoc_next += xfer->nframes;
1669 at91dci_setup_standard_chain(xfer);
1673 at91dci_device_isoc_fs_start(struct usb2_xfer *xfer)
1675 /* start TD chain */
1676 at91dci_start_standard_chain(xfer);
1679 struct usb2_pipe_methods at91dci_device_isoc_fs_methods =
1681 .open = at91dci_device_isoc_fs_open,
1682 .close = at91dci_device_isoc_fs_close,
1683 .enter = at91dci_device_isoc_fs_enter,
1684 .start = at91dci_device_isoc_fs_start,
1685 .enter_is_cancelable = 1,
1686 .start_is_cancelable = 1,
1689 /*------------------------------------------------------------------------*
1690 * at91dci root control support
1691 *------------------------------------------------------------------------*
1692 * simulate a hardware HUB by handling
1693 * all the necessary requests
1694 *------------------------------------------------------------------------*/
1697 at91dci_root_ctrl_open(struct usb2_xfer *xfer)
1703 at91dci_root_ctrl_close(struct usb2_xfer *xfer)
1705 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1707 if (sc->sc_root_ctrl.xfer == xfer) {
1708 sc->sc_root_ctrl.xfer = NULL;
1710 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1714 * USB descriptors for the virtual Root HUB:
1717 static const struct usb2_device_descriptor at91dci_devd = {
1718 .bLength = sizeof(struct usb2_device_descriptor),
1719 .bDescriptorType = UDESC_DEVICE,
1720 .bcdUSB = {0x00, 0x02},
1721 .bDeviceClass = UDCLASS_HUB,
1722 .bDeviceSubClass = UDSUBCLASS_HUB,
1723 .bDeviceProtocol = UDPROTO_HSHUBSTT,
1724 .bMaxPacketSize = 64,
1725 .bcdDevice = {0x00, 0x01},
1728 .bNumConfigurations = 1,
1731 static const struct usb2_device_qualifier at91dci_odevd = {
1732 .bLength = sizeof(struct usb2_device_qualifier),
1733 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
1734 .bcdUSB = {0x00, 0x02},
1735 .bDeviceClass = UDCLASS_HUB,
1736 .bDeviceSubClass = UDSUBCLASS_HUB,
1737 .bDeviceProtocol = UDPROTO_FSHUB,
1738 .bMaxPacketSize0 = 0,
1739 .bNumConfigurations = 0,
1742 static const struct at91dci_config_desc at91dci_confd = {
1744 .bLength = sizeof(struct usb2_config_descriptor),
1745 .bDescriptorType = UDESC_CONFIG,
1746 .wTotalLength[0] = sizeof(at91dci_confd),
1748 .bConfigurationValue = 1,
1749 .iConfiguration = 0,
1750 .bmAttributes = UC_SELF_POWERED,
1754 .bLength = sizeof(struct usb2_interface_descriptor),
1755 .bDescriptorType = UDESC_INTERFACE,
1757 .bInterfaceClass = UICLASS_HUB,
1758 .bInterfaceSubClass = UISUBCLASS_HUB,
1759 .bInterfaceProtocol = UIPROTO_HSHUBSTT,
1763 .bLength = sizeof(struct usb2_endpoint_descriptor),
1764 .bDescriptorType = UDESC_ENDPOINT,
1765 .bEndpointAddress = (UE_DIR_IN | AT9100_DCI_INTR_ENDPT),
1766 .bmAttributes = UE_INTERRUPT,
1767 .wMaxPacketSize[0] = 8,
1772 static const struct usb2_hub_descriptor_min at91dci_hubd = {
1773 .bDescLength = sizeof(at91dci_hubd),
1774 .bDescriptorType = UDESC_HUB,
1776 .wHubCharacteristics[0] =
1777 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF,
1778 .wHubCharacteristics[1] =
1779 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8,
1780 .bPwrOn2PwrGood = 50,
1781 .bHubContrCurrent = 0,
1782 .DeviceRemovable = {0}, /* port is removable */
1785 #define STRING_LANG \
1786 0x09, 0x04, /* American English */
1788 #define STRING_VENDOR \
1789 'A', 0, 'T', 0, 'M', 0, 'E', 0, 'L', 0
1791 #define STRING_PRODUCT \
1792 'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
1793 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
1796 USB_MAKE_STRING_DESC(STRING_LANG, at91dci_langtab);
1797 USB_MAKE_STRING_DESC(STRING_VENDOR, at91dci_vendor);
1798 USB_MAKE_STRING_DESC(STRING_PRODUCT, at91dci_product);
1801 at91dci_root_ctrl_enter(struct usb2_xfer *xfer)
1807 at91dci_root_ctrl_start(struct usb2_xfer *xfer)
1809 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1811 sc->sc_root_ctrl.xfer = xfer;
1813 usb2_bus_roothub_exec(xfer->xroot->bus);
1817 at91dci_root_ctrl_task(struct usb2_bus *bus)
1819 at91dci_root_ctrl_poll(AT9100_DCI_BUS2SC(bus));
1823 at91dci_root_ctrl_done(struct usb2_xfer *xfer,
1824 struct usb2_sw_transfer *std)
1826 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1829 uint8_t use_polling;
1831 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1833 if (std->state != USB_SW_TR_SETUP) {
1834 if (std->state == USB_SW_TR_PRE_CALLBACK) {
1835 /* transfer transferred */
1836 at91dci_device_done(xfer, std->err);
1841 std->ptr = USB_ADD_BYTES(&sc->sc_hub_temp, 0);
1844 value = UGETW(std->req.wValue);
1845 index = UGETW(std->req.wIndex);
1847 use_polling = mtx_owned(xfer->xroot->xfer_mtx) ? 1 : 0;
1849 /* demultiplex the control request */
1851 switch (std->req.bmRequestType) {
1852 case UT_READ_DEVICE:
1853 switch (std->req.bRequest) {
1854 case UR_GET_DESCRIPTOR:
1855 goto tr_handle_get_descriptor;
1857 goto tr_handle_get_config;
1859 goto tr_handle_get_status;
1865 case UT_WRITE_DEVICE:
1866 switch (std->req.bRequest) {
1867 case UR_SET_ADDRESS:
1868 goto tr_handle_set_address;
1870 goto tr_handle_set_config;
1871 case UR_CLEAR_FEATURE:
1872 goto tr_valid; /* nop */
1873 case UR_SET_DESCRIPTOR:
1874 goto tr_valid; /* nop */
1875 case UR_SET_FEATURE:
1881 case UT_WRITE_ENDPOINT:
1882 switch (std->req.bRequest) {
1883 case UR_CLEAR_FEATURE:
1884 switch (UGETW(std->req.wValue)) {
1885 case UF_ENDPOINT_HALT:
1886 goto tr_handle_clear_halt;
1887 case UF_DEVICE_REMOTE_WAKEUP:
1888 goto tr_handle_clear_wakeup;
1893 case UR_SET_FEATURE:
1894 switch (UGETW(std->req.wValue)) {
1895 case UF_ENDPOINT_HALT:
1896 goto tr_handle_set_halt;
1897 case UF_DEVICE_REMOTE_WAKEUP:
1898 goto tr_handle_set_wakeup;
1903 case UR_SYNCH_FRAME:
1904 goto tr_valid; /* nop */
1910 case UT_READ_ENDPOINT:
1911 switch (std->req.bRequest) {
1913 goto tr_handle_get_ep_status;
1919 case UT_WRITE_INTERFACE:
1920 switch (std->req.bRequest) {
1921 case UR_SET_INTERFACE:
1922 goto tr_handle_set_interface;
1923 case UR_CLEAR_FEATURE:
1924 goto tr_valid; /* nop */
1925 case UR_SET_FEATURE:
1931 case UT_READ_INTERFACE:
1932 switch (std->req.bRequest) {
1933 case UR_GET_INTERFACE:
1934 goto tr_handle_get_interface;
1936 goto tr_handle_get_iface_status;
1942 case UT_WRITE_CLASS_INTERFACE:
1943 case UT_WRITE_VENDOR_INTERFACE:
1947 case UT_READ_CLASS_INTERFACE:
1948 case UT_READ_VENDOR_INTERFACE:
1952 case UT_WRITE_CLASS_DEVICE:
1953 switch (std->req.bRequest) {
1954 case UR_CLEAR_FEATURE:
1956 case UR_SET_DESCRIPTOR:
1957 case UR_SET_FEATURE:
1964 case UT_WRITE_CLASS_OTHER:
1965 switch (std->req.bRequest) {
1966 case UR_CLEAR_FEATURE:
1967 goto tr_handle_clear_port_feature;
1968 case UR_SET_FEATURE:
1969 goto tr_handle_set_port_feature;
1970 case UR_CLEAR_TT_BUFFER:
1980 case UT_READ_CLASS_OTHER:
1981 switch (std->req.bRequest) {
1982 case UR_GET_TT_STATE:
1983 goto tr_handle_get_tt_state;
1985 goto tr_handle_get_port_status;
1991 case UT_READ_CLASS_DEVICE:
1992 switch (std->req.bRequest) {
1993 case UR_GET_DESCRIPTOR:
1994 goto tr_handle_get_class_descriptor;
1996 goto tr_handle_get_class_status;
2007 tr_handle_get_descriptor:
2008 switch (value >> 8) {
2013 std->len = sizeof(at91dci_devd);
2014 std->ptr = USB_ADD_BYTES(&at91dci_devd, 0);
2020 std->len = sizeof(at91dci_confd);
2021 std->ptr = USB_ADD_BYTES(&at91dci_confd, 0);
2024 switch (value & 0xff) {
2025 case 0: /* Language table */
2026 std->len = sizeof(at91dci_langtab);
2027 std->ptr = USB_ADD_BYTES(&at91dci_langtab, 0);
2030 case 1: /* Vendor */
2031 std->len = sizeof(at91dci_vendor);
2032 std->ptr = USB_ADD_BYTES(&at91dci_vendor, 0);
2035 case 2: /* Product */
2036 std->len = sizeof(at91dci_product);
2037 std->ptr = USB_ADD_BYTES(&at91dci_product, 0);
2048 tr_handle_get_config:
2050 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2053 tr_handle_get_status:
2055 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2058 tr_handle_set_address:
2059 if (value & 0xFF00) {
2062 sc->sc_rt_addr = value;
2065 tr_handle_set_config:
2069 sc->sc_conf = value;
2072 tr_handle_get_interface:
2074 sc->sc_hub_temp.wValue[0] = 0;
2077 tr_handle_get_tt_state:
2078 tr_handle_get_class_status:
2079 tr_handle_get_iface_status:
2080 tr_handle_get_ep_status:
2082 USETW(sc->sc_hub_temp.wValue, 0);
2086 tr_handle_set_interface:
2087 tr_handle_set_wakeup:
2088 tr_handle_clear_wakeup:
2089 tr_handle_clear_halt:
2092 tr_handle_clear_port_feature:
2096 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
2099 case UHF_PORT_SUSPEND:
2100 at91dci_wakeup_peer(xfer);
2103 case UHF_PORT_ENABLE:
2104 sc->sc_flags.port_enabled = 0;
2108 case UHF_PORT_INDICATOR:
2109 case UHF_C_PORT_ENABLE:
2110 case UHF_C_PORT_OVER_CURRENT:
2111 case UHF_C_PORT_RESET:
2114 case UHF_PORT_POWER:
2115 sc->sc_flags.port_powered = 0;
2116 at91dci_pull_down(sc);
2117 at91dci_clocks_off(sc);
2119 case UHF_C_PORT_CONNECTION:
2120 sc->sc_flags.change_connect = 0;
2122 case UHF_C_PORT_SUSPEND:
2123 sc->sc_flags.change_suspend = 0;
2126 std->err = USB_ERR_IOERROR;
2131 tr_handle_set_port_feature:
2135 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
2138 case UHF_PORT_ENABLE:
2139 sc->sc_flags.port_enabled = 1;
2141 case UHF_PORT_SUSPEND:
2142 case UHF_PORT_RESET:
2144 case UHF_PORT_INDICATOR:
2147 case UHF_PORT_POWER:
2148 sc->sc_flags.port_powered = 1;
2151 std->err = USB_ERR_IOERROR;
2156 tr_handle_get_port_status:
2158 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
2163 if (sc->sc_flags.status_vbus) {
2164 at91dci_clocks_on(sc);
2165 at91dci_pull_up(sc);
2167 at91dci_pull_down(sc);
2168 at91dci_clocks_off(sc);
2171 /* Select FULL-speed and Device Side Mode */
2173 value = UPS_PORT_MODE_DEVICE;
2175 if (sc->sc_flags.port_powered) {
2176 value |= UPS_PORT_POWER;
2178 if (sc->sc_flags.port_enabled) {
2179 value |= UPS_PORT_ENABLED;
2181 if (sc->sc_flags.status_vbus &&
2182 sc->sc_flags.status_bus_reset) {
2183 value |= UPS_CURRENT_CONNECT_STATUS;
2185 if (sc->sc_flags.status_suspend) {
2186 value |= UPS_SUSPEND;
2188 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
2192 if (sc->sc_flags.change_connect) {
2193 value |= UPS_C_CONNECT_STATUS;
2195 if (sc->sc_flags.status_vbus &&
2196 sc->sc_flags.status_bus_reset) {
2197 /* reset endpoint flags */
2198 bzero(sc->sc_ep_flags, sizeof(sc->sc_ep_flags));
2201 if (sc->sc_flags.change_suspend) {
2202 value |= UPS_C_SUSPEND;
2204 USETW(sc->sc_hub_temp.ps.wPortChange, value);
2205 std->len = sizeof(sc->sc_hub_temp.ps);
2208 tr_handle_get_class_descriptor:
2212 std->ptr = USB_ADD_BYTES(&at91dci_hubd, 0);
2213 std->len = sizeof(at91dci_hubd);
2217 std->err = USB_ERR_STALLED;
2224 at91dci_root_ctrl_poll(struct at91dci_softc *sc)
2226 usb2_sw_transfer(&sc->sc_root_ctrl,
2227 &at91dci_root_ctrl_done);
2230 struct usb2_pipe_methods at91dci_root_ctrl_methods =
2232 .open = at91dci_root_ctrl_open,
2233 .close = at91dci_root_ctrl_close,
2234 .enter = at91dci_root_ctrl_enter,
2235 .start = at91dci_root_ctrl_start,
2236 .enter_is_cancelable = 1,
2237 .start_is_cancelable = 0,
2240 /*------------------------------------------------------------------------*
2241 * at91dci root interrupt support
2242 *------------------------------------------------------------------------*/
2244 at91dci_root_intr_open(struct usb2_xfer *xfer)
2250 at91dci_root_intr_close(struct usb2_xfer *xfer)
2252 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
2254 if (sc->sc_root_intr.xfer == xfer) {
2255 sc->sc_root_intr.xfer = NULL;
2257 at91dci_device_done(xfer, USB_ERR_CANCELLED);
2261 at91dci_root_intr_enter(struct usb2_xfer *xfer)
2267 at91dci_root_intr_start(struct usb2_xfer *xfer)
2269 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
2271 sc->sc_root_intr.xfer = xfer;
2274 struct usb2_pipe_methods at91dci_root_intr_methods =
2276 .open = at91dci_root_intr_open,
2277 .close = at91dci_root_intr_close,
2278 .enter = at91dci_root_intr_enter,
2279 .start = at91dci_root_intr_start,
2280 .enter_is_cancelable = 1,
2281 .start_is_cancelable = 1,
2285 at91dci_xfer_setup(struct usb2_setup_params *parm)
2287 const struct usb2_hw_ep_profile *pf;
2288 struct at91dci_softc *sc;
2289 struct usb2_xfer *xfer;
2295 sc = AT9100_DCI_BUS2SC(parm->udev->bus);
2296 xfer = parm->curr_xfer;
2299 * NOTE: This driver does not use any of the parameters that
2300 * are computed from the following values. Just set some
2301 * reasonable dummies:
2303 parm->hc_max_packet_size = 0x500;
2304 parm->hc_max_packet_count = 1;
2305 parm->hc_max_frame_size = 0x500;
2307 usb2_transfer_setup_sub(parm);
2310 * compute maximum number of TDs
2312 if (parm->methods == &at91dci_device_ctrl_methods) {
2314 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
2317 } else if (parm->methods == &at91dci_device_bulk_methods) {
2319 ntd = xfer->nframes + 1 /* SYNC */ ;
2321 } else if (parm->methods == &at91dci_device_intr_methods) {
2323 ntd = xfer->nframes + 1 /* SYNC */ ;
2325 } else if (parm->methods == &at91dci_device_isoc_fs_methods) {
2327 ntd = xfer->nframes + 1 /* SYNC */ ;
2335 * check if "usb2_transfer_setup_sub" set an error
2341 * allocate transfer descriptors
2350 ep_no = xfer->endpoint & UE_ADDR;
2351 at91dci_get_hw_ep_profile(parm->udev, &pf, ep_no);
2354 /* should not happen */
2355 parm->err = USB_ERR_INVAL;
2364 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2366 for (n = 0; n != ntd; n++) {
2368 struct at91dci_td *td;
2372 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2375 td->io_tag = sc->sc_io_tag;
2376 td->io_hdl = sc->sc_io_hdl;
2377 td->max_packet_size = xfer->max_packet_size;
2378 td->status_reg = AT91_UDP_CSR(ep_no);
2379 td->fifo_reg = AT91_UDP_FDR(ep_no);
2380 if (pf->support_multi_buffer) {
2381 td->support_multi_buffer = 1;
2383 td->obj_next = last_obj;
2387 parm->size[0] += sizeof(*td);
2390 xfer->td_start[0] = last_obj;
2394 at91dci_xfer_unsetup(struct usb2_xfer *xfer)
2400 at91dci_pipe_init(struct usb2_device *udev, struct usb2_endpoint_descriptor *edesc,
2401 struct usb2_pipe *pipe)
2403 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(udev->bus);
2405 DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
2406 pipe, udev->address,
2407 edesc->bEndpointAddress, udev->flags.usb2_mode,
2410 if (udev->device_index == sc->sc_rt_addr) {
2412 if (udev->flags.usb2_mode != USB_MODE_HOST) {
2416 switch (edesc->bEndpointAddress) {
2417 case USB_CONTROL_ENDPOINT:
2418 pipe->methods = &at91dci_root_ctrl_methods;
2420 case UE_DIR_IN | AT9100_DCI_INTR_ENDPT:
2421 pipe->methods = &at91dci_root_intr_methods;
2429 if (udev->flags.usb2_mode != USB_MODE_DEVICE) {
2433 if (udev->speed != USB_SPEED_FULL) {
2437 switch (edesc->bmAttributes & UE_XFERTYPE) {
2439 pipe->methods = &at91dci_device_ctrl_methods;
2442 pipe->methods = &at91dci_device_intr_methods;
2444 case UE_ISOCHRONOUS:
2445 pipe->methods = &at91dci_device_isoc_fs_methods;
2448 pipe->methods = &at91dci_device_bulk_methods;
2457 struct usb2_bus_methods at91dci_bus_methods =
2459 .pipe_init = &at91dci_pipe_init,
2460 .xfer_setup = &at91dci_xfer_setup,
2461 .xfer_unsetup = &at91dci_xfer_unsetup,
2462 .do_poll = &at91dci_do_poll,
2463 .get_hw_ep_profile = &at91dci_get_hw_ep_profile,
2464 .set_stall = &at91dci_set_stall,
2465 .clear_stall = &at91dci_clear_stall,
2466 .roothub_exec = &at91dci_root_ctrl_task,