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 <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <sys/linker_set.h>
56 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/sysctl.h>
62 #include <sys/unistd.h>
63 #include <sys/callout.h>
64 #include <sys/malloc.h>
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usbdi.h>
70 #define USB_DEBUG_VAR at91dcidebug
72 #include <dev/usb/usb_core.h>
73 #include <dev/usb/usb_debug.h>
74 #include <dev/usb/usb_busdma.h>
75 #include <dev/usb/usb_process.h>
76 #include <dev/usb/usb_transfer.h>
77 #include <dev/usb/usb_device.h>
78 #include <dev/usb/usb_hub.h>
79 #include <dev/usb/usb_util.h>
81 #include <dev/usb/usb_controller.h>
82 #include <dev/usb/usb_bus.h>
83 #include <dev/usb/controller/at91dci.h>
85 #define AT9100_DCI_BUS2SC(bus) \
86 ((struct at91dci_softc *)(((uint8_t *)(bus)) - \
87 ((uint8_t *)&(((struct at91dci_softc *)0)->sc_bus))))
89 #define AT9100_DCI_PC2SC(pc) \
90 AT9100_DCI_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
93 static int at91dcidebug = 0;
95 SYSCTL_NODE(_hw_usb, OID_AUTO, at91dci, CTLFLAG_RW, 0, "USB at91dci");
96 SYSCTL_INT(_hw_usb_at91dci, OID_AUTO, debug, CTLFLAG_RW,
97 &at91dcidebug, 0, "at91dci debug level");
100 #define AT9100_DCI_INTR_ENDPT 1
104 struct usb_bus_methods at91dci_bus_methods;
105 struct usb_pipe_methods at91dci_device_bulk_methods;
106 struct usb_pipe_methods at91dci_device_ctrl_methods;
107 struct usb_pipe_methods at91dci_device_intr_methods;
108 struct usb_pipe_methods at91dci_device_isoc_fs_methods;
110 static at91dci_cmd_t at91dci_setup_rx;
111 static at91dci_cmd_t at91dci_data_rx;
112 static at91dci_cmd_t at91dci_data_tx;
113 static at91dci_cmd_t at91dci_data_tx_sync;
114 static void at91dci_device_done(struct usb_xfer *, usb_error_t);
115 static void at91dci_do_poll(struct usb_bus *);
116 static void at91dci_standard_done(struct usb_xfer *);
117 static void at91dci_root_intr(struct at91dci_softc *sc);
120 * NOTE: Some of the bits in the CSR register have inverse meaning so
121 * we need a helper macro when acknowledging events:
123 #define AT91_CSR_ACK(csr, what) do { \
124 (csr) &= ~((AT91_UDP_CSR_FORCESTALL| \
125 AT91_UDP_CSR_TXPKTRDY| \
126 AT91_UDP_CSR_RXBYTECNT) ^ (what));\
127 (csr) |= ((AT91_UDP_CSR_RX_DATA_BK0| \
128 AT91_UDP_CSR_RX_DATA_BK1| \
129 AT91_UDP_CSR_TXCOMP| \
130 AT91_UDP_CSR_RXSETUP| \
131 AT91_UDP_CSR_STALLSENT) ^ (what)); \
135 * Here is a list of what the chip supports.
136 * Probably it supports more than listed here!
138 static const struct usb_hw_ep_profile
139 at91dci_ep_profile[AT91_UDP_EP_MAX] = {
142 .max_in_frame_size = 8,
143 .max_out_frame_size = 8,
145 .support_control = 1,
148 .max_in_frame_size = 64,
149 .max_out_frame_size = 64,
151 .support_multi_buffer = 1,
153 .support_interrupt = 1,
154 .support_isochronous = 1,
159 .max_in_frame_size = 64,
160 .max_out_frame_size = 64,
162 .support_multi_buffer = 1,
164 .support_interrupt = 1,
165 .support_isochronous = 1,
170 /* can also do BULK */
171 .max_in_frame_size = 8,
172 .max_out_frame_size = 8,
174 .support_interrupt = 1,
179 .max_in_frame_size = 256,
180 .max_out_frame_size = 256,
182 .support_multi_buffer = 1,
184 .support_interrupt = 1,
185 .support_isochronous = 1,
190 .max_in_frame_size = 256,
191 .max_out_frame_size = 256,
193 .support_multi_buffer = 1,
195 .support_interrupt = 1,
196 .support_isochronous = 1,
203 at91dci_get_hw_ep_profile(struct usb_device *udev,
204 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
206 if (ep_addr < AT91_UDP_EP_MAX) {
207 *ppf = (at91dci_ep_profile + ep_addr);
214 at91dci_clocks_on(struct at91dci_softc *sc)
216 if (sc->sc_flags.clocks_off &&
217 sc->sc_flags.port_powered) {
221 if (sc->sc_clocks_on) {
222 (sc->sc_clocks_on) (sc->sc_clocks_arg);
224 sc->sc_flags.clocks_off = 0;
226 /* enable Transceiver */
227 AT91_UDP_WRITE_4(sc, AT91_UDP_TXVC, 0);
232 at91dci_clocks_off(struct at91dci_softc *sc)
234 if (!sc->sc_flags.clocks_off) {
238 /* disable Transceiver */
239 AT91_UDP_WRITE_4(sc, AT91_UDP_TXVC, AT91_UDP_TXVC_DIS);
241 if (sc->sc_clocks_off) {
242 (sc->sc_clocks_off) (sc->sc_clocks_arg);
244 sc->sc_flags.clocks_off = 1;
249 at91dci_pull_up(struct at91dci_softc *sc)
251 /* pullup D+, if possible */
253 if (!sc->sc_flags.d_pulled_up &&
254 sc->sc_flags.port_powered) {
255 sc->sc_flags.d_pulled_up = 1;
256 (sc->sc_pull_up) (sc->sc_pull_arg);
261 at91dci_pull_down(struct at91dci_softc *sc)
263 /* pulldown D+, if possible */
265 if (sc->sc_flags.d_pulled_up) {
266 sc->sc_flags.d_pulled_up = 0;
267 (sc->sc_pull_down) (sc->sc_pull_arg);
272 at91dci_wakeup_peer(struct at91dci_softc *sc)
274 if (!(sc->sc_flags.status_suspend)) {
278 AT91_UDP_WRITE_4(sc, AT91_UDP_GSTATE, AT91_UDP_GSTATE_ESR);
280 /* wait 8 milliseconds */
281 /* Wait for reset to complete. */
282 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
284 AT91_UDP_WRITE_4(sc, AT91_UDP_GSTATE, 0);
288 at91dci_set_address(struct at91dci_softc *sc, uint8_t addr)
290 DPRINTFN(5, "addr=%d\n", addr);
292 AT91_UDP_WRITE_4(sc, AT91_UDP_FADDR, addr |
297 at91dci_setup_rx(struct at91dci_td *td)
299 struct at91dci_softc *sc;
300 struct usb_device_request req;
305 /* read out FIFO status */
306 csr = bus_space_read_4(td->io_tag, td->io_hdl,
309 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
312 temp &= (AT91_UDP_CSR_RX_DATA_BK0 |
313 AT91_UDP_CSR_RX_DATA_BK1 |
314 AT91_UDP_CSR_STALLSENT |
315 AT91_UDP_CSR_RXSETUP |
316 AT91_UDP_CSR_TXCOMP);
318 if (!(csr & AT91_UDP_CSR_RXSETUP)) {
321 /* clear did stall */
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 usbd_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 /* abort any ongoing transfer */
374 if (!td->did_stall) {
375 DPRINTFN(5, "stalling\n");
376 temp |= AT91_UDP_CSR_FORCESTALL;
380 /* clear interrupts, if any */
382 DPRINTFN(5, "clearing 0x%08x\n", temp);
383 AT91_CSR_ACK(csr, temp);
384 bus_space_write_4(td->io_tag, td->io_hdl,
385 td->status_reg, csr);
387 return (1); /* not complete */
392 at91dci_data_rx(struct at91dci_td *td)
394 struct usb_page_search buf_res;
401 to = 2; /* don't loop forever! */
404 /* check if any of the FIFO banks have data */
406 /* read out FIFO status */
407 csr = bus_space_read_4(td->io_tag, td->io_hdl,
410 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
412 if (csr & AT91_UDP_CSR_RXSETUP) {
413 if (td->remainder == 0) {
415 * We are actually complete and have
416 * received the next SETUP
418 DPRINTFN(5, "faking complete\n");
419 return (0); /* complete */
422 * USB Host Aborted the transfer.
425 return (0); /* complete */
427 /* Make sure that "STALLSENT" gets cleared */
429 temp &= AT91_UDP_CSR_STALLSENT;
432 if (!(csr & (AT91_UDP_CSR_RX_DATA_BK0 |
433 AT91_UDP_CSR_RX_DATA_BK1))) {
436 AT91_CSR_ACK(csr, temp);
437 bus_space_write_4(td->io_tag, td->io_hdl,
438 td->status_reg, csr);
440 return (1); /* not complete */
442 /* get the packet byte count */
443 count = (csr & AT91_UDP_CSR_RXBYTECNT) >> 16;
445 /* verify the packet byte count */
446 if (count != td->max_packet_size) {
447 if (count < td->max_packet_size) {
448 /* we have a short packet */
452 /* invalid USB packet */
454 return (0); /* we are complete */
457 /* verify the packet byte count */
458 if (count > td->remainder) {
459 /* invalid USB packet */
461 return (0); /* we are complete */
464 usbd_get_page(td->pc, td->offset, &buf_res);
466 /* get correct length */
467 if (buf_res.length > count) {
468 buf_res.length = count;
471 bus_space_read_multi_1(td->io_tag, td->io_hdl,
472 td->fifo_reg, buf_res.buffer, buf_res.length);
474 /* update counters */
475 count -= buf_res.length;
476 td->offset += buf_res.length;
477 td->remainder -= buf_res.length;
480 /* clear status bits */
481 if (td->support_multi_buffer) {
484 temp |= AT91_UDP_CSR_RX_DATA_BK1;
487 temp |= AT91_UDP_CSR_RX_DATA_BK0;
490 temp |= (AT91_UDP_CSR_RX_DATA_BK0 |
491 AT91_UDP_CSR_RX_DATA_BK1);
495 AT91_CSR_ACK(csr, temp);
496 bus_space_write_4(td->io_tag, td->io_hdl,
497 td->status_reg, csr);
500 * NOTE: We may have to delay a little bit before
501 * proceeding after clearing the DATA_BK bits.
504 /* check if we are complete */
505 if ((td->remainder == 0) || got_short) {
507 /* we are complete */
510 /* else need to receive a zero length packet */
515 return (1); /* not complete */
519 at91dci_data_tx(struct at91dci_td *td)
521 struct usb_page_search buf_res;
527 to = 2; /* don't loop forever! */
531 /* read out FIFO status */
532 csr = bus_space_read_4(td->io_tag, td->io_hdl,
535 DPRINTFN(5, "csr=0x%08x rem=%u\n", csr, td->remainder);
537 if (csr & AT91_UDP_CSR_RXSETUP) {
539 * The current transfer was aborted
543 return (0); /* complete */
545 /* Make sure that "STALLSENT" gets cleared */
547 temp &= AT91_UDP_CSR_STALLSENT;
549 if (csr & AT91_UDP_CSR_TXPKTRDY) {
552 AT91_CSR_ACK(csr, temp);
553 bus_space_write_4(td->io_tag, td->io_hdl,
554 td->status_reg, csr);
556 return (1); /* not complete */
558 /* clear TXCOMP and set TXPKTRDY */
559 temp |= (AT91_UDP_CSR_TXCOMP |
560 AT91_UDP_CSR_TXPKTRDY);
563 count = td->max_packet_size;
564 if (td->remainder < count) {
565 /* we have a short packet */
567 count = td->remainder;
571 usbd_get_page(td->pc, td->offset, &buf_res);
573 /* get correct length */
574 if (buf_res.length > count) {
575 buf_res.length = count;
578 bus_space_write_multi_1(td->io_tag, td->io_hdl,
579 td->fifo_reg, buf_res.buffer, buf_res.length);
581 /* update counters */
582 count -= buf_res.length;
583 td->offset += buf_res.length;
584 td->remainder -= buf_res.length;
588 AT91_CSR_ACK(csr, temp);
589 bus_space_write_4(td->io_tag, td->io_hdl,
590 td->status_reg, csr);
592 /* check remainder */
593 if (td->remainder == 0) {
595 return (0); /* complete */
597 /* else we need to transmit a short packet */
602 return (1); /* not complete */
606 at91dci_data_tx_sync(struct at91dci_td *td)
608 struct at91dci_softc *sc;
616 /* read out FIFO status */
617 csr = bus_space_read_4(td->io_tag, td->io_hdl,
620 DPRINTFN(5, "csr=0x%08x\n", csr);
622 if (csr & AT91_UDP_CSR_RXSETUP) {
623 DPRINTFN(5, "faking complete\n");
625 return (0); /* complete */
628 temp &= (AT91_UDP_CSR_STALLSENT |
629 AT91_UDP_CSR_TXCOMP);
632 if (csr & AT91_UDP_CSR_TXPKTRDY) {
635 if (!(csr & AT91_UDP_CSR_TXCOMP)) {
638 sc = AT9100_DCI_PC2SC(td->pc);
639 if (sc->sc_dv_addr != 0xFF) {
641 * The AT91 has a special requirement with regard to
642 * setting the address and that is to write the new
643 * address before clearing TXCOMP:
645 at91dci_set_address(sc, sc->sc_dv_addr);
648 AT91_CSR_ACK(csr, temp);
649 bus_space_write_4(td->io_tag, td->io_hdl,
650 td->status_reg, csr);
652 return (0); /* complete */
657 AT91_CSR_ACK(csr, temp);
658 bus_space_write_4(td->io_tag, td->io_hdl,
659 td->status_reg, csr);
661 return (1); /* not complete */
665 at91dci_xfer_do_fifo(struct usb_xfer *xfer)
667 struct at91dci_softc *sc;
668 struct at91dci_td *td;
673 td = xfer->td_transfer_cache;
675 if ((td->func) (td)) {
676 /* operation in progress */
679 if (((void *)td) == xfer->td_transfer_last) {
684 } else if (td->remainder > 0) {
686 * We had a short transfer. If there is no alternate
687 * next, stop processing !
694 * Fetch the next transfer descriptor and transfer
695 * some flags to the next transfer descriptor
701 xfer->td_transfer_cache = td;
705 return (1); /* not complete */
708 sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
709 temp = (xfer->endpointno & UE_ADDR);
711 /* update FIFO bank flag and multi buffer */
713 sc->sc_ep_flags[temp].fifo_bank = 1;
715 sc->sc_ep_flags[temp].fifo_bank = 0;
718 /* compute all actual lengths */
720 at91dci_standard_done(xfer);
722 return (0); /* complete */
726 at91dci_interrupt_poll(struct at91dci_softc *sc)
728 struct usb_xfer *xfer;
731 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
732 if (!at91dci_xfer_do_fifo(xfer)) {
733 /* queue has been modified */
740 at91dci_vbus_interrupt(struct at91dci_softc *sc, uint8_t is_on)
742 DPRINTFN(5, "vbus = %u\n", is_on);
744 USB_BUS_LOCK(&sc->sc_bus);
746 if (!sc->sc_flags.status_vbus) {
747 sc->sc_flags.status_vbus = 1;
749 /* complete root HUB interrupt endpoint */
750 at91dci_root_intr(sc);
753 if (sc->sc_flags.status_vbus) {
754 sc->sc_flags.status_vbus = 0;
755 sc->sc_flags.status_bus_reset = 0;
756 sc->sc_flags.status_suspend = 0;
757 sc->sc_flags.change_suspend = 0;
758 sc->sc_flags.change_connect = 1;
760 /* complete root HUB interrupt endpoint */
761 at91dci_root_intr(sc);
764 USB_BUS_UNLOCK(&sc->sc_bus);
768 at91dci_interrupt(struct at91dci_softc *sc)
772 USB_BUS_LOCK(&sc->sc_bus);
774 status = AT91_UDP_READ_4(sc, AT91_UDP_ISR);
775 status &= AT91_UDP_INT_DEFAULT;
778 USB_BUS_UNLOCK(&sc->sc_bus);
781 /* acknowledge interrupts */
783 AT91_UDP_WRITE_4(sc, AT91_UDP_ICR, status);
785 /* check for any bus state change interrupts */
787 if (status & AT91_UDP_INT_BUS) {
789 DPRINTFN(5, "real bus interrupt 0x%08x\n", status);
791 if (status & AT91_UDP_INT_END_BR) {
793 /* set correct state */
794 sc->sc_flags.status_bus_reset = 1;
795 sc->sc_flags.status_suspend = 0;
796 sc->sc_flags.change_suspend = 0;
797 sc->sc_flags.change_connect = 1;
799 /* disable resume interrupt */
800 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
802 /* enable suspend interrupt */
803 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
804 AT91_UDP_INT_RXSUSP);
807 * If RXRSM and RXSUSP is set at the same time we interpret
808 * that like RESUME. Resume is set when there is at least 3
809 * milliseconds of inactivity on the USB BUS.
811 if (status & AT91_UDP_INT_RXRSM) {
812 if (sc->sc_flags.status_suspend) {
813 sc->sc_flags.status_suspend = 0;
814 sc->sc_flags.change_suspend = 1;
816 /* disable resume interrupt */
817 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
819 /* enable suspend interrupt */
820 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
821 AT91_UDP_INT_RXSUSP);
823 } else if (status & AT91_UDP_INT_RXSUSP) {
824 if (!sc->sc_flags.status_suspend) {
825 sc->sc_flags.status_suspend = 1;
826 sc->sc_flags.change_suspend = 1;
828 /* disable suspend interrupt */
829 AT91_UDP_WRITE_4(sc, AT91_UDP_IDR,
830 AT91_UDP_INT_RXSUSP);
832 /* enable resume interrupt */
833 AT91_UDP_WRITE_4(sc, AT91_UDP_IER,
837 /* complete root HUB interrupt endpoint */
838 at91dci_root_intr(sc);
840 /* check for any endpoint interrupts */
842 if (status & AT91_UDP_INT_EPS) {
844 DPRINTFN(5, "real endpoint interrupt 0x%08x\n", status);
846 at91dci_interrupt_poll(sc);
848 USB_BUS_UNLOCK(&sc->sc_bus);
852 at91dci_setup_standard_chain_sub(struct at91dci_std_temp *temp)
854 struct at91dci_td *td;
856 /* get current Transfer Descriptor */
860 /* prepare for next TD */
861 temp->td_next = td->obj_next;
863 /* fill out the Transfer Descriptor */
864 td->func = temp->func;
866 td->offset = temp->offset;
867 td->remainder = temp->len;
870 td->did_stall = temp->did_stall;
871 td->short_pkt = temp->short_pkt;
872 td->alt_next = temp->setup_alt_next;
876 at91dci_setup_standard_chain(struct usb_xfer *xfer)
878 struct at91dci_std_temp temp;
879 struct at91dci_softc *sc;
880 struct at91dci_td *td;
885 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
886 xfer->address, UE_GET_ADDR(xfer->endpointno),
887 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
889 temp.max_frame_size = xfer->max_frame_size;
891 td = xfer->td_start[0];
892 xfer->td_transfer_first = td;
893 xfer->td_transfer_cache = td;
899 temp.td_next = xfer->td_start[0];
901 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
902 temp.did_stall = !xfer->flags_int.control_stall;
904 sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
905 ep_no = (xfer->endpointno & UE_ADDR);
907 /* check if we should prepend a setup message */
909 if (xfer->flags_int.control_xfr) {
910 if (xfer->flags_int.control_hdr) {
912 temp.func = &at91dci_setup_rx;
913 temp.len = xfer->frlengths[0];
914 temp.pc = xfer->frbuffers + 0;
915 temp.short_pkt = temp.len ? 1 : 0;
916 /* check for last frame */
917 if (xfer->nframes == 1) {
918 /* no STATUS stage yet, SETUP is last */
919 if (xfer->flags_int.control_act)
920 temp.setup_alt_next = 0;
923 at91dci_setup_standard_chain_sub(&temp);
930 if (x != xfer->nframes) {
931 if (xfer->endpointno & UE_DIR_IN) {
932 temp.func = &at91dci_data_tx;
935 temp.func = &at91dci_data_rx;
939 /* setup "pc" pointer */
940 temp.pc = xfer->frbuffers + x;
944 while (x != xfer->nframes) {
946 /* DATA0 / DATA1 message */
948 temp.len = xfer->frlengths[x];
952 if (x == xfer->nframes) {
953 if (xfer->flags_int.control_xfr) {
954 if (xfer->flags_int.control_act) {
955 temp.setup_alt_next = 0;
958 temp.setup_alt_next = 0;
963 /* make sure that we send an USB packet */
969 /* regular data transfer */
971 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
974 at91dci_setup_standard_chain_sub(&temp);
976 if (xfer->flags_int.isochronous_xfr) {
977 temp.offset += temp.len;
979 /* get next Page Cache pointer */
980 temp.pc = xfer->frbuffers + x;
984 /* check for control transfer */
985 if (xfer->flags_int.control_xfr) {
987 /* always setup a valid "pc" pointer for status and sync */
988 temp.pc = xfer->frbuffers + 0;
991 temp.setup_alt_next = 0;
993 /* check if we need to sync */
995 /* we need a SYNC point after TX */
996 temp.func = &at91dci_data_tx_sync;
997 at91dci_setup_standard_chain_sub(&temp);
1000 /* check if we should append a status stage */
1001 if (!xfer->flags_int.control_act) {
1004 * Send a DATA1 message and invert the current
1005 * endpoint direction.
1007 if (xfer->endpointno & UE_DIR_IN) {
1008 temp.func = &at91dci_data_rx;
1011 temp.func = &at91dci_data_tx;
1015 at91dci_setup_standard_chain_sub(&temp);
1017 /* we need a SYNC point after TX */
1018 temp.func = &at91dci_data_tx_sync;
1019 at91dci_setup_standard_chain_sub(&temp);
1024 /* must have at least one frame! */
1026 xfer->td_transfer_last = td;
1028 /* setup the correct fifo bank */
1029 if (sc->sc_ep_flags[ep_no].fifo_bank) {
1030 td = xfer->td_transfer_first;
1036 at91dci_timeout(void *arg)
1038 struct usb_xfer *xfer = arg;
1040 DPRINTF("xfer=%p\n", xfer);
1042 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1044 /* transfer is transferred */
1045 at91dci_device_done(xfer, USB_ERR_TIMEOUT);
1049 at91dci_start_standard_chain(struct usb_xfer *xfer)
1054 if (at91dci_xfer_do_fifo(xfer)) {
1056 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1057 uint8_t ep_no = xfer->endpointno & UE_ADDR;
1060 * Only enable the endpoint interrupt when we are actually
1061 * waiting for data, hence we are dealing with level
1062 * triggered interrupts !
1064 AT91_UDP_WRITE_4(sc, AT91_UDP_IER, AT91_UDP_INT_EP(ep_no));
1066 DPRINTFN(15, "enable interrupts on endpoint %d\n", ep_no);
1068 /* put transfer on interrupt queue */
1069 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1071 /* start timeout, if any */
1072 if (xfer->timeout != 0) {
1073 usbd_transfer_timeout_ms(xfer,
1074 &at91dci_timeout, xfer->timeout);
1080 at91dci_root_intr(struct at91dci_softc *sc)
1084 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1087 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
1089 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1090 sizeof(sc->sc_hub_idata));
1094 at91dci_standard_done_sub(struct usb_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 usb_xfer *xfer)
1160 usb_error_t err = 0;
1162 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1163 xfer, xfer->endpoint);
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 usb_xfer *xfer, usb_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, endpoint=%p, error=%d\n",
1215 xfer, xfer->endpoint, error);
1217 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1218 ep_no = (xfer->endpointno & 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 usbd_transfer_done(xfer, error);
1230 at91dci_set_stall(struct usb_device *udev, struct usb_xfer *xfer,
1231 struct usb_endpoint *ep, uint8_t *did_stall)
1233 struct at91dci_softc *sc;
1237 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1239 DPRINTFN(5, "endpoint=%p\n", ep);
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 = (ep->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 usb_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 usb_device *udev, struct usb_endpoint *ep)
1353 struct at91dci_softc *sc;
1354 struct usb_endpoint_descriptor *ed;
1356 DPRINTFN(5, "endpoint=%p\n", ep);
1358 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1361 if (udev->flags.usb_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 usb_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 usb_bus *bus)
1480 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(bus);
1482 USB_BUS_LOCK(&sc->sc_bus);
1483 at91dci_interrupt_poll(sc);
1484 USB_BUS_UNLOCK(&sc->sc_bus);
1487 /*------------------------------------------------------------------------*
1488 * at91dci bulk support
1489 *------------------------------------------------------------------------*/
1491 at91dci_device_bulk_open(struct usb_xfer *xfer)
1497 at91dci_device_bulk_close(struct usb_xfer *xfer)
1499 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1503 at91dci_device_bulk_enter(struct usb_xfer *xfer)
1509 at91dci_device_bulk_start(struct usb_xfer *xfer)
1512 at91dci_setup_standard_chain(xfer);
1513 at91dci_start_standard_chain(xfer);
1516 struct usb_pipe_methods at91dci_device_bulk_methods =
1518 .open = at91dci_device_bulk_open,
1519 .close = at91dci_device_bulk_close,
1520 .enter = at91dci_device_bulk_enter,
1521 .start = at91dci_device_bulk_start,
1524 /*------------------------------------------------------------------------*
1525 * at91dci control support
1526 *------------------------------------------------------------------------*/
1528 at91dci_device_ctrl_open(struct usb_xfer *xfer)
1534 at91dci_device_ctrl_close(struct usb_xfer *xfer)
1536 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1540 at91dci_device_ctrl_enter(struct usb_xfer *xfer)
1546 at91dci_device_ctrl_start(struct usb_xfer *xfer)
1549 at91dci_setup_standard_chain(xfer);
1550 at91dci_start_standard_chain(xfer);
1553 struct usb_pipe_methods at91dci_device_ctrl_methods =
1555 .open = at91dci_device_ctrl_open,
1556 .close = at91dci_device_ctrl_close,
1557 .enter = at91dci_device_ctrl_enter,
1558 .start = at91dci_device_ctrl_start,
1561 /*------------------------------------------------------------------------*
1562 * at91dci interrupt support
1563 *------------------------------------------------------------------------*/
1565 at91dci_device_intr_open(struct usb_xfer *xfer)
1571 at91dci_device_intr_close(struct usb_xfer *xfer)
1573 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1577 at91dci_device_intr_enter(struct usb_xfer *xfer)
1583 at91dci_device_intr_start(struct usb_xfer *xfer)
1586 at91dci_setup_standard_chain(xfer);
1587 at91dci_start_standard_chain(xfer);
1590 struct usb_pipe_methods at91dci_device_intr_methods =
1592 .open = at91dci_device_intr_open,
1593 .close = at91dci_device_intr_close,
1594 .enter = at91dci_device_intr_enter,
1595 .start = at91dci_device_intr_start,
1598 /*------------------------------------------------------------------------*
1599 * at91dci full speed isochronous support
1600 *------------------------------------------------------------------------*/
1602 at91dci_device_isoc_fs_open(struct usb_xfer *xfer)
1608 at91dci_device_isoc_fs_close(struct usb_xfer *xfer)
1610 at91dci_device_done(xfer, USB_ERR_CANCELLED);
1614 at91dci_device_isoc_fs_enter(struct usb_xfer *xfer)
1616 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(xfer->xroot->bus);
1620 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1621 xfer, xfer->endpoint->isoc_next, xfer->nframes);
1623 /* get the current frame index */
1625 nframes = AT91_UDP_READ_4(sc, AT91_UDP_FRM);
1628 * check if the frame index is within the window where the frames
1631 temp = (nframes - xfer->endpoint->isoc_next) & AT91_UDP_FRM_MASK;
1633 if ((xfer->endpoint->is_synced == 0) ||
1634 (temp < xfer->nframes)) {
1636 * If there is data underflow or the endpoint queue is
1637 * empty we schedule the transfer a few frames ahead
1638 * of the current frame position. Else two isochronous
1639 * transfers might overlap.
1641 xfer->endpoint->isoc_next = (nframes + 3) & AT91_UDP_FRM_MASK;
1642 xfer->endpoint->is_synced = 1;
1643 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1646 * compute how many milliseconds the insertion is ahead of the
1647 * current frame position:
1649 temp = (xfer->endpoint->isoc_next - nframes) & AT91_UDP_FRM_MASK;
1652 * pre-compute when the isochronous transfer will be finished:
1654 xfer->isoc_time_complete =
1655 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1658 /* compute frame number for next insertion */
1659 xfer->endpoint->isoc_next += xfer->nframes;
1662 at91dci_setup_standard_chain(xfer);
1666 at91dci_device_isoc_fs_start(struct usb_xfer *xfer)
1668 /* start TD chain */
1669 at91dci_start_standard_chain(xfer);
1672 struct usb_pipe_methods at91dci_device_isoc_fs_methods =
1674 .open = at91dci_device_isoc_fs_open,
1675 .close = at91dci_device_isoc_fs_close,
1676 .enter = at91dci_device_isoc_fs_enter,
1677 .start = at91dci_device_isoc_fs_start,
1680 /*------------------------------------------------------------------------*
1681 * at91dci root control support
1682 *------------------------------------------------------------------------*
1683 * Simulate a hardware HUB by handling all the necessary requests.
1684 *------------------------------------------------------------------------*/
1686 static const struct usb_device_descriptor at91dci_devd = {
1687 .bLength = sizeof(struct usb_device_descriptor),
1688 .bDescriptorType = UDESC_DEVICE,
1689 .bcdUSB = {0x00, 0x02},
1690 .bDeviceClass = UDCLASS_HUB,
1691 .bDeviceSubClass = UDSUBCLASS_HUB,
1692 .bDeviceProtocol = UDPROTO_FSHUB,
1693 .bMaxPacketSize = 64,
1694 .bcdDevice = {0x00, 0x01},
1697 .bNumConfigurations = 1,
1700 static const struct at91dci_config_desc at91dci_confd = {
1702 .bLength = sizeof(struct usb_config_descriptor),
1703 .bDescriptorType = UDESC_CONFIG,
1704 .wTotalLength[0] = sizeof(at91dci_confd),
1706 .bConfigurationValue = 1,
1707 .iConfiguration = 0,
1708 .bmAttributes = UC_SELF_POWERED,
1712 .bLength = sizeof(struct usb_interface_descriptor),
1713 .bDescriptorType = UDESC_INTERFACE,
1715 .bInterfaceClass = UICLASS_HUB,
1716 .bInterfaceSubClass = UISUBCLASS_HUB,
1717 .bInterfaceProtocol = 0,
1720 .bLength = sizeof(struct usb_endpoint_descriptor),
1721 .bDescriptorType = UDESC_ENDPOINT,
1722 .bEndpointAddress = (UE_DIR_IN | AT9100_DCI_INTR_ENDPT),
1723 .bmAttributes = UE_INTERRUPT,
1724 .wMaxPacketSize[0] = 8,
1729 static const struct usb_hub_descriptor_min at91dci_hubd = {
1730 .bDescLength = sizeof(at91dci_hubd),
1731 .bDescriptorType = UDESC_HUB,
1733 .wHubCharacteristics[0] =
1734 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF,
1735 .wHubCharacteristics[1] =
1736 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8,
1737 .bPwrOn2PwrGood = 50,
1738 .bHubContrCurrent = 0,
1739 .DeviceRemovable = {0}, /* port is removable */
1742 #define STRING_LANG \
1743 0x09, 0x04, /* American English */
1745 #define STRING_VENDOR \
1746 'A', 0, 'T', 0, 'M', 0, 'E', 0, 'L', 0
1748 #define STRING_PRODUCT \
1749 'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
1750 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
1753 USB_MAKE_STRING_DESC(STRING_LANG, at91dci_langtab);
1754 USB_MAKE_STRING_DESC(STRING_VENDOR, at91dci_vendor);
1755 USB_MAKE_STRING_DESC(STRING_PRODUCT, at91dci_product);
1758 at91dci_roothub_exec(struct usb_device *udev,
1759 struct usb_device_request *req, const void **pptr, uint16_t *plength)
1761 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(udev->bus);
1768 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1771 ptr = (const void *)&sc->sc_hub_temp;
1775 value = UGETW(req->wValue);
1776 index = UGETW(req->wIndex);
1778 /* demultiplex the control request */
1780 switch (req->bmRequestType) {
1781 case UT_READ_DEVICE:
1782 switch (req->bRequest) {
1783 case UR_GET_DESCRIPTOR:
1784 goto tr_handle_get_descriptor;
1786 goto tr_handle_get_config;
1788 goto tr_handle_get_status;
1794 case UT_WRITE_DEVICE:
1795 switch (req->bRequest) {
1796 case UR_SET_ADDRESS:
1797 goto tr_handle_set_address;
1799 goto tr_handle_set_config;
1800 case UR_CLEAR_FEATURE:
1801 goto tr_valid; /* nop */
1802 case UR_SET_DESCRIPTOR:
1803 goto tr_valid; /* nop */
1804 case UR_SET_FEATURE:
1810 case UT_WRITE_ENDPOINT:
1811 switch (req->bRequest) {
1812 case UR_CLEAR_FEATURE:
1813 switch (UGETW(req->wValue)) {
1814 case UF_ENDPOINT_HALT:
1815 goto tr_handle_clear_halt;
1816 case UF_DEVICE_REMOTE_WAKEUP:
1817 goto tr_handle_clear_wakeup;
1822 case UR_SET_FEATURE:
1823 switch (UGETW(req->wValue)) {
1824 case UF_ENDPOINT_HALT:
1825 goto tr_handle_set_halt;
1826 case UF_DEVICE_REMOTE_WAKEUP:
1827 goto tr_handle_set_wakeup;
1832 case UR_SYNCH_FRAME:
1833 goto tr_valid; /* nop */
1839 case UT_READ_ENDPOINT:
1840 switch (req->bRequest) {
1842 goto tr_handle_get_ep_status;
1848 case UT_WRITE_INTERFACE:
1849 switch (req->bRequest) {
1850 case UR_SET_INTERFACE:
1851 goto tr_handle_set_interface;
1852 case UR_CLEAR_FEATURE:
1853 goto tr_valid; /* nop */
1854 case UR_SET_FEATURE:
1860 case UT_READ_INTERFACE:
1861 switch (req->bRequest) {
1862 case UR_GET_INTERFACE:
1863 goto tr_handle_get_interface;
1865 goto tr_handle_get_iface_status;
1871 case UT_WRITE_CLASS_INTERFACE:
1872 case UT_WRITE_VENDOR_INTERFACE:
1876 case UT_READ_CLASS_INTERFACE:
1877 case UT_READ_VENDOR_INTERFACE:
1881 case UT_WRITE_CLASS_DEVICE:
1882 switch (req->bRequest) {
1883 case UR_CLEAR_FEATURE:
1885 case UR_SET_DESCRIPTOR:
1886 case UR_SET_FEATURE:
1893 case UT_WRITE_CLASS_OTHER:
1894 switch (req->bRequest) {
1895 case UR_CLEAR_FEATURE:
1896 goto tr_handle_clear_port_feature;
1897 case UR_SET_FEATURE:
1898 goto tr_handle_set_port_feature;
1899 case UR_CLEAR_TT_BUFFER:
1909 case UT_READ_CLASS_OTHER:
1910 switch (req->bRequest) {
1911 case UR_GET_TT_STATE:
1912 goto tr_handle_get_tt_state;
1914 goto tr_handle_get_port_status;
1920 case UT_READ_CLASS_DEVICE:
1921 switch (req->bRequest) {
1922 case UR_GET_DESCRIPTOR:
1923 goto tr_handle_get_class_descriptor;
1925 goto tr_handle_get_class_status;
1936 tr_handle_get_descriptor:
1937 switch (value >> 8) {
1942 len = sizeof(at91dci_devd);
1943 ptr = (const void *)&at91dci_devd;
1949 len = sizeof(at91dci_confd);
1950 ptr = (const void *)&at91dci_confd;
1953 switch (value & 0xff) {
1954 case 0: /* Language table */
1955 len = sizeof(at91dci_langtab);
1956 ptr = (const void *)&at91dci_langtab;
1959 case 1: /* Vendor */
1960 len = sizeof(at91dci_vendor);
1961 ptr = (const void *)&at91dci_vendor;
1964 case 2: /* Product */
1965 len = sizeof(at91dci_product);
1966 ptr = (const void *)&at91dci_product;
1977 tr_handle_get_config:
1979 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
1982 tr_handle_get_status:
1984 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
1987 tr_handle_set_address:
1988 if (value & 0xFF00) {
1991 sc->sc_rt_addr = value;
1994 tr_handle_set_config:
1998 sc->sc_conf = value;
2001 tr_handle_get_interface:
2003 sc->sc_hub_temp.wValue[0] = 0;
2006 tr_handle_get_tt_state:
2007 tr_handle_get_class_status:
2008 tr_handle_get_iface_status:
2009 tr_handle_get_ep_status:
2011 USETW(sc->sc_hub_temp.wValue, 0);
2015 tr_handle_set_interface:
2016 tr_handle_set_wakeup:
2017 tr_handle_clear_wakeup:
2018 tr_handle_clear_halt:
2021 tr_handle_clear_port_feature:
2025 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
2028 case UHF_PORT_SUSPEND:
2029 at91dci_wakeup_peer(sc);
2032 case UHF_PORT_ENABLE:
2033 sc->sc_flags.port_enabled = 0;
2037 case UHF_PORT_INDICATOR:
2038 case UHF_C_PORT_ENABLE:
2039 case UHF_C_PORT_OVER_CURRENT:
2040 case UHF_C_PORT_RESET:
2043 case UHF_PORT_POWER:
2044 sc->sc_flags.port_powered = 0;
2045 at91dci_pull_down(sc);
2046 at91dci_clocks_off(sc);
2048 case UHF_C_PORT_CONNECTION:
2049 sc->sc_flags.change_connect = 0;
2051 case UHF_C_PORT_SUSPEND:
2052 sc->sc_flags.change_suspend = 0;
2055 err = USB_ERR_IOERROR;
2060 tr_handle_set_port_feature:
2064 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
2067 case UHF_PORT_ENABLE:
2068 sc->sc_flags.port_enabled = 1;
2070 case UHF_PORT_SUSPEND:
2071 case UHF_PORT_RESET:
2073 case UHF_PORT_INDICATOR:
2076 case UHF_PORT_POWER:
2077 sc->sc_flags.port_powered = 1;
2080 err = USB_ERR_IOERROR;
2085 tr_handle_get_port_status:
2087 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
2092 if (sc->sc_flags.status_vbus) {
2093 at91dci_clocks_on(sc);
2094 at91dci_pull_up(sc);
2096 at91dci_pull_down(sc);
2097 at91dci_clocks_off(sc);
2100 /* Select FULL-speed and Device Side Mode */
2102 value = UPS_PORT_MODE_DEVICE;
2104 if (sc->sc_flags.port_powered) {
2105 value |= UPS_PORT_POWER;
2107 if (sc->sc_flags.port_enabled) {
2108 value |= UPS_PORT_ENABLED;
2110 if (sc->sc_flags.status_vbus &&
2111 sc->sc_flags.status_bus_reset) {
2112 value |= UPS_CURRENT_CONNECT_STATUS;
2114 if (sc->sc_flags.status_suspend) {
2115 value |= UPS_SUSPEND;
2117 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
2121 if (sc->sc_flags.change_connect) {
2122 value |= UPS_C_CONNECT_STATUS;
2124 if (sc->sc_flags.status_vbus &&
2125 sc->sc_flags.status_bus_reset) {
2126 /* reset endpoint flags */
2127 bzero(sc->sc_ep_flags, sizeof(sc->sc_ep_flags));
2130 if (sc->sc_flags.change_suspend) {
2131 value |= UPS_C_SUSPEND;
2133 USETW(sc->sc_hub_temp.ps.wPortChange, value);
2134 len = sizeof(sc->sc_hub_temp.ps);
2137 tr_handle_get_class_descriptor:
2141 ptr = (const void *)&at91dci_hubd;
2142 len = sizeof(at91dci_hubd);
2146 err = USB_ERR_STALLED;
2155 at91dci_xfer_setup(struct usb_setup_params *parm)
2157 const struct usb_hw_ep_profile *pf;
2158 struct at91dci_softc *sc;
2159 struct usb_xfer *xfer;
2165 sc = AT9100_DCI_BUS2SC(parm->udev->bus);
2166 xfer = parm->curr_xfer;
2169 * NOTE: This driver does not use any of the parameters that
2170 * are computed from the following values. Just set some
2171 * reasonable dummies:
2173 parm->hc_max_packet_size = 0x500;
2174 parm->hc_max_packet_count = 1;
2175 parm->hc_max_frame_size = 0x500;
2177 usbd_transfer_setup_sub(parm);
2180 * compute maximum number of TDs
2182 if (parm->methods == &at91dci_device_ctrl_methods) {
2184 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
2187 } else if (parm->methods == &at91dci_device_bulk_methods) {
2189 ntd = xfer->nframes + 1 /* SYNC */ ;
2191 } else if (parm->methods == &at91dci_device_intr_methods) {
2193 ntd = xfer->nframes + 1 /* SYNC */ ;
2195 } else if (parm->methods == &at91dci_device_isoc_fs_methods) {
2197 ntd = xfer->nframes + 1 /* SYNC */ ;
2205 * check if "usbd_transfer_setup_sub" set an error
2211 * allocate transfer descriptors
2220 ep_no = xfer->endpointno & UE_ADDR;
2221 at91dci_get_hw_ep_profile(parm->udev, &pf, ep_no);
2224 /* should not happen */
2225 parm->err = USB_ERR_INVAL;
2234 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2236 for (n = 0; n != ntd; n++) {
2238 struct at91dci_td *td;
2242 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2245 td->io_tag = sc->sc_io_tag;
2246 td->io_hdl = sc->sc_io_hdl;
2247 td->max_packet_size = xfer->max_packet_size;
2248 td->status_reg = AT91_UDP_CSR(ep_no);
2249 td->fifo_reg = AT91_UDP_FDR(ep_no);
2250 if (pf->support_multi_buffer) {
2251 td->support_multi_buffer = 1;
2253 td->obj_next = last_obj;
2257 parm->size[0] += sizeof(*td);
2260 xfer->td_start[0] = last_obj;
2264 at91dci_xfer_unsetup(struct usb_xfer *xfer)
2270 at91dci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2271 struct usb_endpoint *ep)
2273 struct at91dci_softc *sc = AT9100_DCI_BUS2SC(udev->bus);
2275 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
2277 edesc->bEndpointAddress, udev->flags.usb_mode,
2280 if (udev->device_index != sc->sc_rt_addr) {
2282 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2286 if (udev->speed != USB_SPEED_FULL) {
2290 switch (edesc->bmAttributes & UE_XFERTYPE) {
2292 ep->methods = &at91dci_device_ctrl_methods;
2295 ep->methods = &at91dci_device_intr_methods;
2297 case UE_ISOCHRONOUS:
2298 ep->methods = &at91dci_device_isoc_fs_methods;
2301 ep->methods = &at91dci_device_bulk_methods;
2310 struct usb_bus_methods at91dci_bus_methods =
2312 .endpoint_init = &at91dci_ep_init,
2313 .xfer_setup = &at91dci_xfer_setup,
2314 .xfer_unsetup = &at91dci_xfer_unsetup,
2315 .get_hw_ep_profile = &at91dci_get_hw_ep_profile,
2316 .set_stall = &at91dci_set_stall,
2317 .clear_stall = &at91dci_clear_stall,
2318 .roothub_exec = &at91dci_roothub_exec,
2319 .xfer_poll = &at91dci_do_poll,