3 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * This file contains the driver for the AVR32 series USB Device
33 * NOTE: When the chip detects BUS-reset it will also reset the
34 * endpoints, Function-address and more.
36 #ifdef USB_GLOBAL_INCLUDE_FILE
37 #include USB_GLOBAL_INCLUDE_FILE
39 #include <sys/stdint.h>
40 #include <sys/stddef.h>
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
49 #include <sys/mutex.h>
50 #include <sys/condvar.h>
51 #include <sys/sysctl.h>
53 #include <sys/unistd.h>
54 #include <sys/callout.h>
55 #include <sys/malloc.h>
58 #include <dev/usb/usb.h>
59 #include <dev/usb/usbdi.h>
61 #define USB_DEBUG_VAR avr32dci_debug
63 #include <dev/usb/usb_core.h>
64 #include <dev/usb/usb_debug.h>
65 #include <dev/usb/usb_busdma.h>
66 #include <dev/usb/usb_process.h>
67 #include <dev/usb/usb_transfer.h>
68 #include <dev/usb/usb_device.h>
69 #include <dev/usb/usb_hub.h>
70 #include <dev/usb/usb_util.h>
72 #include <dev/usb/usb_controller.h>
73 #include <dev/usb/usb_bus.h>
74 #endif /* USB_GLOBAL_INCLUDE_FILE */
76 #include <dev/usb/controller/avr32dci.h>
78 #define AVR32_BUS2SC(bus) \
79 ((struct avr32dci_softc *)(((uint8_t *)(bus)) - \
80 ((uint8_t *)&(((struct avr32dci_softc *)0)->sc_bus))))
82 #define AVR32_PC2SC(pc) \
83 AVR32_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
86 static int avr32dci_debug = 0;
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, avr32dci, CTLFLAG_RW, 0, "USB AVR32 DCI");
89 SYSCTL_INT(_hw_usb_avr32dci, OID_AUTO, debug, CTLFLAG_RW,
90 &avr32dci_debug, 0, "AVR32 DCI debug level");
93 #define AVR32_INTR_ENDPT 1
97 struct usb_bus_methods avr32dci_bus_methods;
98 struct usb_pipe_methods avr32dci_device_non_isoc_methods;
99 struct usb_pipe_methods avr32dci_device_isoc_fs_methods;
101 static avr32dci_cmd_t avr32dci_setup_rx;
102 static avr32dci_cmd_t avr32dci_data_rx;
103 static avr32dci_cmd_t avr32dci_data_tx;
104 static avr32dci_cmd_t avr32dci_data_tx_sync;
105 static void avr32dci_device_done(struct usb_xfer *, usb_error_t);
106 static void avr32dci_do_poll(struct usb_bus *);
107 static void avr32dci_standard_done(struct usb_xfer *);
108 static void avr32dci_root_intr(struct avr32dci_softc *sc);
111 * Here is a list of what the chip supports:
113 static const struct usb_hw_ep_profile
114 avr32dci_ep_profile[4] = {
117 .max_in_frame_size = 64,
118 .max_out_frame_size = 64,
120 .support_control = 1,
124 .max_in_frame_size = 512,
125 .max_out_frame_size = 512,
128 .support_interrupt = 1,
129 .support_isochronous = 1,
135 .max_in_frame_size = 64,
136 .max_out_frame_size = 64,
139 .support_interrupt = 1,
145 .max_in_frame_size = 1024,
146 .max_out_frame_size = 1024,
149 .support_interrupt = 1,
150 .support_isochronous = 1,
157 avr32dci_get_hw_ep_profile(struct usb_device *udev,
158 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
161 *ppf = avr32dci_ep_profile;
162 else if (ep_addr < 3)
163 *ppf = avr32dci_ep_profile + 1;
164 else if (ep_addr < 5)
165 *ppf = avr32dci_ep_profile + 2;
166 else if (ep_addr < 7)
167 *ppf = avr32dci_ep_profile + 3;
173 avr32dci_mod_ctrl(struct avr32dci_softc *sc, uint32_t set, uint32_t clear)
177 temp = AVR32_READ_4(sc, AVR32_CTRL);
180 AVR32_WRITE_4(sc, AVR32_CTRL, temp);
184 avr32dci_mod_ien(struct avr32dci_softc *sc, uint32_t set, uint32_t clear)
188 temp = AVR32_READ_4(sc, AVR32_IEN);
191 AVR32_WRITE_4(sc, AVR32_IEN, temp);
195 avr32dci_clocks_on(struct avr32dci_softc *sc)
197 if (sc->sc_flags.clocks_off &&
198 sc->sc_flags.port_powered) {
203 (sc->sc_clocks_on) (&sc->sc_bus);
205 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_EN_USBA, 0);
207 sc->sc_flags.clocks_off = 0;
212 avr32dci_clocks_off(struct avr32dci_softc *sc)
214 if (!sc->sc_flags.clocks_off) {
218 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_EN_USBA);
220 /* turn clocks off */
221 (sc->sc_clocks_off) (&sc->sc_bus);
223 sc->sc_flags.clocks_off = 1;
228 avr32dci_pull_up(struct avr32dci_softc *sc)
230 /* pullup D+, if possible */
232 if (!sc->sc_flags.d_pulled_up &&
233 sc->sc_flags.port_powered) {
234 sc->sc_flags.d_pulled_up = 1;
235 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_DETACH);
240 avr32dci_pull_down(struct avr32dci_softc *sc)
242 /* pulldown D+, if possible */
244 if (sc->sc_flags.d_pulled_up) {
245 sc->sc_flags.d_pulled_up = 0;
246 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_DETACH, 0);
251 avr32dci_wakeup_peer(struct avr32dci_softc *sc)
253 if (!sc->sc_flags.status_suspend) {
256 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_REWAKEUP, 0);
258 /* wait 8 milliseconds */
259 /* Wait for reset to complete. */
260 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
262 /* hardware should have cleared RMWKUP bit */
266 avr32dci_set_address(struct avr32dci_softc *sc, uint8_t addr)
268 DPRINTFN(5, "addr=%d\n", addr);
270 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_FADDR_EN | addr, 0);
274 avr32dci_setup_rx(struct avr32dci_td *td)
276 struct avr32dci_softc *sc;
277 struct usb_device_request req;
281 /* get pointer to softc */
282 sc = AVR32_PC2SC(td->pc);
284 /* check endpoint status */
285 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
287 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
289 if (!(temp & AVR32_EPTSTA_RX_SETUP)) {
292 /* clear did stall */
294 /* get the packet byte count */
295 count = AVR32_EPTSTA_BYTE_COUNT(temp);
297 /* verify data length */
298 if (count != td->remainder) {
299 DPRINTFN(0, "Invalid SETUP packet "
300 "length, %d bytes\n", count);
303 if (count != sizeof(req)) {
304 DPRINTFN(0, "Unsupported SETUP packet "
305 "length, %d bytes\n", count);
309 memcpy(&req, sc->physdata, sizeof(req));
311 /* copy data into real buffer */
312 usbd_copy_in(td->pc, 0, &req, sizeof(req));
314 td->offset = sizeof(req);
317 /* sneak peek the set address */
318 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
319 (req.bRequest == UR_SET_ADDRESS)) {
320 sc->sc_dv_addr = req.wValue[0] & 0x7F;
321 /* must write address before ZLP */
322 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_FADDR_EN |
323 AVR32_CTRL_DEV_ADDR);
324 avr32dci_mod_ctrl(sc, sc->sc_dv_addr, 0);
326 sc->sc_dv_addr = 0xFF;
329 /* clear SETUP packet interrupt */
330 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_SETUP);
331 return (0); /* complete */
334 if (temp & AVR32_EPTSTA_RX_SETUP) {
335 /* clear SETUP packet interrupt */
336 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_SETUP);
338 /* abort any ongoing transfer */
339 if (!td->did_stall) {
340 DPRINTFN(5, "stalling\n");
341 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(td->ep_no),
342 AVR32_EPTSTA_FRCESTALL);
345 return (1); /* not complete */
349 avr32dci_data_rx(struct avr32dci_td *td)
351 struct avr32dci_softc *sc;
352 struct usb_page_search buf_res;
358 to = 4; /* don't loop forever! */
361 /* get pointer to softc */
362 sc = AVR32_PC2SC(td->pc);
365 /* check if any of the FIFO banks have data */
366 /* check endpoint status */
367 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
369 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
371 if (temp & AVR32_EPTSTA_RX_SETUP) {
372 if (td->remainder == 0) {
374 * We are actually complete and have
375 * received the next SETUP
377 DPRINTFN(5, "faking complete\n");
378 return (0); /* complete */
381 * USB Host Aborted the transfer.
384 return (0); /* complete */
387 if (!(temp & AVR32_EPTSTA_RX_BK_RDY)) {
391 /* get the packet byte count */
392 count = AVR32_EPTSTA_BYTE_COUNT(temp);
394 /* verify the packet byte count */
395 if (count != td->max_packet_size) {
396 if (count < td->max_packet_size) {
397 /* we have a short packet */
401 /* invalid USB packet */
403 return (0); /* we are complete */
406 /* verify the packet byte count */
407 if (count > td->remainder) {
408 /* invalid USB packet */
410 return (0); /* we are complete */
413 usbd_get_page(td->pc, td->offset, &buf_res);
415 /* get correct length */
416 if (buf_res.length > count) {
417 buf_res.length = count;
420 memcpy(buf_res.buffer, sc->physdata +
421 (AVR32_EPTSTA_CURRENT_BANK(temp) << td->bank_shift) +
422 (td->ep_no << 16) + (td->offset % td->max_packet_size), buf_res.length);
423 /* update counters */
424 count -= buf_res.length;
425 td->offset += buf_res.length;
426 td->remainder -= buf_res.length;
429 /* clear OUT packet interrupt */
430 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_BK_RDY);
432 /* check if we are complete */
433 if ((td->remainder == 0) || got_short) {
435 /* we are complete */
438 /* else need to receive a zero length packet */
444 return (1); /* not complete */
448 avr32dci_data_tx(struct avr32dci_td *td)
450 struct avr32dci_softc *sc;
451 struct usb_page_search buf_res;
456 to = 4; /* don't loop forever! */
458 /* get pointer to softc */
459 sc = AVR32_PC2SC(td->pc);
463 /* check endpoint status */
464 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
466 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
468 if (temp & AVR32_EPTSTA_RX_SETUP) {
470 * The current transfer was aborted
474 return (0); /* complete */
476 if (temp & AVR32_EPTSTA_TX_PK_RDY) {
477 /* cannot write any data - all banks are busy */
480 count = td->max_packet_size;
481 if (td->remainder < count) {
482 /* we have a short packet */
484 count = td->remainder;
488 usbd_get_page(td->pc, td->offset, &buf_res);
490 /* get correct length */
491 if (buf_res.length > count) {
492 buf_res.length = count;
495 memcpy(sc->physdata +
496 (AVR32_EPTSTA_CURRENT_BANK(temp) << td->bank_shift) +
497 (td->ep_no << 16) + (td->offset % td->max_packet_size),
498 buf_res.buffer, buf_res.length);
499 /* update counters */
500 count -= buf_res.length;
501 td->offset += buf_res.length;
502 td->remainder -= buf_res.length;
505 /* allocate FIFO bank */
506 AVR32_WRITE_4(sc, AVR32_EPTCTL(td->ep_no), AVR32_EPTCTL_TX_PK_RDY);
508 /* check remainder */
509 if (td->remainder == 0) {
511 return (0); /* complete */
513 /* else we need to transmit a short packet */
519 return (1); /* not complete */
523 avr32dci_data_tx_sync(struct avr32dci_td *td)
525 struct avr32dci_softc *sc;
528 /* get pointer to softc */
529 sc = AVR32_PC2SC(td->pc);
531 /* check endpoint status */
532 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
534 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
536 if (temp & AVR32_EPTSTA_RX_SETUP) {
537 DPRINTFN(5, "faking complete\n");
539 return (0); /* complete */
542 * The control endpoint has only got one bank, so if that bank
543 * is free the packet has been transferred!
545 if (AVR32_EPTSTA_BUSY_BANK_STA(temp) != 0) {
546 /* cannot write any data - a bank is busy */
549 if (sc->sc_dv_addr != 0xFF) {
550 /* set new address */
551 avr32dci_set_address(sc, sc->sc_dv_addr);
553 return (0); /* complete */
556 return (1); /* not complete */
560 avr32dci_xfer_do_fifo(struct usb_xfer *xfer)
562 struct avr32dci_td *td;
566 td = xfer->td_transfer_cache;
568 if ((td->func) (td)) {
569 /* operation in progress */
572 if (((void *)td) == xfer->td_transfer_last) {
577 } else if (td->remainder > 0) {
579 * We had a short transfer. If there is no alternate
580 * next, stop processing !
587 * Fetch the next transfer descriptor and transfer
588 * some flags to the next transfer descriptor
591 xfer->td_transfer_cache = td;
593 return (1); /* not complete */
596 /* compute all actual lengths */
598 avr32dci_standard_done(xfer);
599 return (0); /* complete */
603 avr32dci_interrupt_poll(struct avr32dci_softc *sc)
605 struct usb_xfer *xfer;
608 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
609 if (!avr32dci_xfer_do_fifo(xfer)) {
610 /* queue has been modified */
617 avr32dci_vbus_interrupt(struct avr32dci_softc *sc, uint8_t is_on)
619 DPRINTFN(5, "vbus = %u\n", is_on);
622 if (!sc->sc_flags.status_vbus) {
623 sc->sc_flags.status_vbus = 1;
625 /* complete root HUB interrupt endpoint */
627 avr32dci_root_intr(sc);
630 if (sc->sc_flags.status_vbus) {
631 sc->sc_flags.status_vbus = 0;
632 sc->sc_flags.status_bus_reset = 0;
633 sc->sc_flags.status_suspend = 0;
634 sc->sc_flags.change_suspend = 0;
635 sc->sc_flags.change_connect = 1;
637 /* complete root HUB interrupt endpoint */
639 avr32dci_root_intr(sc);
645 avr32dci_interrupt(struct avr32dci_softc *sc)
649 USB_BUS_LOCK(&sc->sc_bus);
651 /* read interrupt status */
652 status = AVR32_READ_4(sc, AVR32_INTSTA);
654 /* clear all set interrupts */
655 AVR32_WRITE_4(sc, AVR32_CLRINT, status);
657 DPRINTFN(14, "INTSTA=0x%08x\n", status);
659 /* check for any bus state change interrupts */
660 if (status & AVR32_INT_ENDRESET) {
662 DPRINTFN(5, "end of reset\n");
664 /* set correct state */
665 sc->sc_flags.status_bus_reset = 1;
666 sc->sc_flags.status_suspend = 0;
667 sc->sc_flags.change_suspend = 0;
668 sc->sc_flags.change_connect = 1;
670 /* disable resume interrupt */
671 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
672 AVR32_INT_ENDRESET, AVR32_INT_WAKE_UP);
674 /* complete root HUB interrupt endpoint */
675 avr32dci_root_intr(sc);
678 * If resume and suspend is set at the same time we interpret
679 * that like RESUME. Resume is set when there is at least 3
680 * milliseconds of inactivity on the USB BUS.
682 if (status & AVR32_INT_WAKE_UP) {
684 DPRINTFN(5, "resume interrupt\n");
686 if (sc->sc_flags.status_suspend) {
687 /* update status bits */
688 sc->sc_flags.status_suspend = 0;
689 sc->sc_flags.change_suspend = 1;
691 /* disable resume interrupt */
692 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
693 AVR32_INT_ENDRESET, AVR32_INT_WAKE_UP);
695 /* complete root HUB interrupt endpoint */
696 avr32dci_root_intr(sc);
698 } else if (status & AVR32_INT_DET_SUSPD) {
700 DPRINTFN(5, "suspend interrupt\n");
702 if (!sc->sc_flags.status_suspend) {
703 /* update status bits */
704 sc->sc_flags.status_suspend = 1;
705 sc->sc_flags.change_suspend = 1;
707 /* disable suspend interrupt */
708 avr32dci_mod_ien(sc, AVR32_INT_WAKE_UP |
709 AVR32_INT_ENDRESET, AVR32_INT_DET_SUSPD);
711 /* complete root HUB interrupt endpoint */
712 avr32dci_root_intr(sc);
715 /* check for any endpoint interrupts */
716 if (status & -AVR32_INT_EPT_INT(0)) {
718 DPRINTFN(5, "real endpoint interrupt\n");
720 avr32dci_interrupt_poll(sc);
722 USB_BUS_UNLOCK(&sc->sc_bus);
726 avr32dci_setup_standard_chain_sub(struct avr32dci_std_temp *temp)
728 struct avr32dci_td *td;
730 /* get current Transfer Descriptor */
734 /* prepare for next TD */
735 temp->td_next = td->obj_next;
737 /* fill out the Transfer Descriptor */
738 td->func = temp->func;
740 td->offset = temp->offset;
741 td->remainder = temp->len;
743 td->did_stall = temp->did_stall;
744 td->short_pkt = temp->short_pkt;
745 td->alt_next = temp->setup_alt_next;
749 avr32dci_setup_standard_chain(struct usb_xfer *xfer)
751 struct avr32dci_std_temp temp;
752 struct avr32dci_softc *sc;
753 struct avr32dci_td *td;
758 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
759 xfer->address, UE_GET_ADDR(xfer->endpointno),
760 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
762 temp.max_frame_size = xfer->max_frame_size;
764 td = xfer->td_start[0];
765 xfer->td_transfer_first = td;
766 xfer->td_transfer_cache = td;
772 temp.td_next = xfer->td_start[0];
774 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
775 temp.did_stall = !xfer->flags_int.control_stall;
777 sc = AVR32_BUS2SC(xfer->xroot->bus);
778 ep_no = (xfer->endpointno & UE_ADDR);
780 /* check if we should prepend a setup message */
782 if (xfer->flags_int.control_xfr) {
783 if (xfer->flags_int.control_hdr) {
785 temp.func = &avr32dci_setup_rx;
786 temp.len = xfer->frlengths[0];
787 temp.pc = xfer->frbuffers + 0;
788 temp.short_pkt = temp.len ? 1 : 0;
789 /* check for last frame */
790 if (xfer->nframes == 1) {
791 /* no STATUS stage yet, SETUP is last */
792 if (xfer->flags_int.control_act)
793 temp.setup_alt_next = 0;
795 avr32dci_setup_standard_chain_sub(&temp);
802 if (x != xfer->nframes) {
803 if (xfer->endpointno & UE_DIR_IN) {
804 temp.func = &avr32dci_data_tx;
807 temp.func = &avr32dci_data_rx;
811 /* setup "pc" pointer */
812 temp.pc = xfer->frbuffers + x;
816 while (x != xfer->nframes) {
818 /* DATA0 / DATA1 message */
820 temp.len = xfer->frlengths[x];
824 if (x == xfer->nframes) {
825 if (xfer->flags_int.control_xfr) {
826 if (xfer->flags_int.control_act) {
827 temp.setup_alt_next = 0;
830 temp.setup_alt_next = 0;
835 /* make sure that we send an USB packet */
841 /* regular data transfer */
843 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
846 avr32dci_setup_standard_chain_sub(&temp);
848 if (xfer->flags_int.isochronous_xfr) {
849 temp.offset += temp.len;
851 /* get next Page Cache pointer */
852 temp.pc = xfer->frbuffers + x;
856 if (xfer->flags_int.control_xfr) {
858 /* always setup a valid "pc" pointer for status and sync */
859 temp.pc = xfer->frbuffers + 0;
862 temp.setup_alt_next = 0;
864 /* check if we need to sync */
866 /* we need a SYNC point after TX */
867 temp.func = &avr32dci_data_tx_sync;
868 avr32dci_setup_standard_chain_sub(&temp);
870 /* check if we should append a status stage */
871 if (!xfer->flags_int.control_act) {
874 * Send a DATA1 message and invert the current
875 * endpoint direction.
877 if (xfer->endpointno & UE_DIR_IN) {
878 temp.func = &avr32dci_data_rx;
881 temp.func = &avr32dci_data_tx;
885 avr32dci_setup_standard_chain_sub(&temp);
887 /* we need a SYNC point after TX */
888 temp.func = &avr32dci_data_tx_sync;
889 avr32dci_setup_standard_chain_sub(&temp);
893 /* must have at least one frame! */
895 xfer->td_transfer_last = td;
899 avr32dci_timeout(void *arg)
901 struct usb_xfer *xfer = arg;
903 DPRINTF("xfer=%p\n", xfer);
905 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
907 /* transfer is transferred */
908 avr32dci_device_done(xfer, USB_ERR_TIMEOUT);
912 avr32dci_start_standard_chain(struct usb_xfer *xfer)
916 /* poll one time - will turn on interrupts */
917 if (avr32dci_xfer_do_fifo(xfer)) {
918 uint8_t ep_no = xfer->endpointno & UE_ADDR;
919 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
921 avr32dci_mod_ien(sc, AVR32_INT_EPT_INT(ep_no), 0);
923 /* put transfer on interrupt queue */
924 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
926 /* start timeout, if any */
927 if (xfer->timeout != 0) {
928 usbd_transfer_timeout_ms(xfer,
929 &avr32dci_timeout, xfer->timeout);
935 avr32dci_root_intr(struct avr32dci_softc *sc)
939 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
942 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
944 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
945 sizeof(sc->sc_hub_idata));
949 avr32dci_standard_done_sub(struct usb_xfer *xfer)
951 struct avr32dci_td *td;
957 td = xfer->td_transfer_cache;
962 if (xfer->aframes != xfer->nframes) {
964 * Verify the length and subtract
965 * the remainder from "frlengths[]":
967 if (len > xfer->frlengths[xfer->aframes]) {
970 xfer->frlengths[xfer->aframes] -= len;
973 /* Check for transfer error */
975 /* the transfer is finished */
980 /* Check for short transfer */
982 if (xfer->flags_int.short_frames_ok) {
983 /* follow alt next */
990 /* the transfer is finished */
998 /* this USB frame is complete */
1004 /* update transfer cache */
1006 xfer->td_transfer_cache = td;
1009 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1013 avr32dci_standard_done(struct usb_xfer *xfer)
1015 usb_error_t err = 0;
1017 DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1018 xfer, xfer->endpoint);
1022 xfer->td_transfer_cache = xfer->td_transfer_first;
1024 if (xfer->flags_int.control_xfr) {
1026 if (xfer->flags_int.control_hdr) {
1028 err = avr32dci_standard_done_sub(xfer);
1032 if (xfer->td_transfer_cache == NULL) {
1036 while (xfer->aframes != xfer->nframes) {
1038 err = avr32dci_standard_done_sub(xfer);
1041 if (xfer->td_transfer_cache == NULL) {
1046 if (xfer->flags_int.control_xfr &&
1047 !xfer->flags_int.control_act) {
1049 err = avr32dci_standard_done_sub(xfer);
1052 avr32dci_device_done(xfer, err);
1055 /*------------------------------------------------------------------------*
1056 * avr32dci_device_done
1058 * NOTE: this function can be called more than one time on the
1059 * same USB transfer!
1060 *------------------------------------------------------------------------*/
1062 avr32dci_device_done(struct usb_xfer *xfer, usb_error_t error)
1064 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
1067 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1069 DPRINTFN(9, "xfer=%p, pipe=%p, error=%d\n",
1070 xfer, xfer->endpoint, error);
1072 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1073 ep_no = (xfer->endpointno & UE_ADDR);
1075 /* disable endpoint interrupt */
1076 avr32dci_mod_ien(sc, 0, AVR32_INT_EPT_INT(ep_no));
1078 DPRINTFN(15, "disabled interrupts!\n");
1080 /* dequeue transfer and start next transfer */
1081 usbd_transfer_done(xfer, error);
1085 avr32dci_xfer_stall(struct usb_xfer *xfer)
1087 avr32dci_device_done(xfer, USB_ERR_STALLED);
1091 avr32dci_set_stall(struct usb_device *udev,
1092 struct usb_endpoint *pipe, uint8_t *did_stall)
1094 struct avr32dci_softc *sc;
1097 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1099 DPRINTFN(5, "pipe=%p\n", pipe);
1101 sc = AVR32_BUS2SC(udev->bus);
1102 /* get endpoint number */
1103 ep_no = (pipe->edesc->bEndpointAddress & UE_ADDR);
1105 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1109 avr32dci_clear_stall_sub(struct avr32dci_softc *sc, uint8_t ep_no,
1110 uint8_t ep_type, uint8_t ep_dir)
1112 const struct usb_hw_ep_profile *pf;
1117 if (ep_type == UE_CONTROL) {
1118 /* clearing stall is not needed */
1121 /* set endpoint reset */
1122 AVR32_WRITE_4(sc, AVR32_EPTRST, AVR32_EPTRST_MASK(ep_no));
1125 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1127 /* reset data toggle */
1128 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(ep_no), AVR32_EPTSTA_TOGGLESQ);
1131 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1133 if (ep_type == UE_BULK) {
1134 temp = AVR32_EPTCFG_TYPE_BULK;
1135 } else if (ep_type == UE_INTERRUPT) {
1136 temp = AVR32_EPTCFG_TYPE_INTR;
1138 temp = AVR32_EPTCFG_TYPE_ISOC |
1139 AVR32_EPTCFG_NB_TRANS(1);
1141 if (ep_dir & UE_DIR_IN) {
1142 temp |= AVR32_EPTCFG_EPDIR_IN;
1144 avr32dci_get_hw_ep_profile(NULL, &pf, ep_no);
1146 /* compute endpoint size (use maximum) */
1147 epsize = pf->max_in_frame_size | pf->max_out_frame_size;
1149 while ((epsize /= 2))
1151 temp |= AVR32_EPTCFG_EPSIZE(n);
1153 /* use the maximum number of banks supported */
1155 temp |= AVR32_EPTCFG_NBANK(1);
1157 temp |= AVR32_EPTCFG_NBANK(2);
1159 temp |= AVR32_EPTCFG_NBANK(3);
1161 AVR32_WRITE_4(sc, AVR32_EPTCFG(ep_no), temp);
1163 temp = AVR32_READ_4(sc, AVR32_EPTCFG(ep_no));
1165 if (!(temp & AVR32_EPTCFG_EPT_MAPD)) {
1166 device_printf(sc->sc_bus.bdev, "Chip rejected configuration\n");
1168 AVR32_WRITE_4(sc, AVR32_EPTCTLENB(ep_no),
1169 AVR32_EPTCTL_EPT_ENABL);
1174 avr32dci_clear_stall(struct usb_device *udev, struct usb_endpoint *pipe)
1176 struct avr32dci_softc *sc;
1177 struct usb_endpoint_descriptor *ed;
1179 DPRINTFN(5, "pipe=%p\n", pipe);
1181 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1184 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1189 sc = AVR32_BUS2SC(udev->bus);
1191 /* get endpoint descriptor */
1194 /* reset endpoint */
1195 avr32dci_clear_stall_sub(sc,
1196 (ed->bEndpointAddress & UE_ADDR),
1197 (ed->bmAttributes & UE_XFERTYPE),
1198 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1202 avr32dci_init(struct avr32dci_softc *sc)
1208 /* set up the bus structure */
1209 sc->sc_bus.usbrev = USB_REV_1_1;
1210 sc->sc_bus.methods = &avr32dci_bus_methods;
1212 USB_BUS_LOCK(&sc->sc_bus);
1214 /* make sure USB is enabled */
1215 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_EN_USBA, 0);
1217 /* turn on clocks */
1218 (sc->sc_clocks_on) (&sc->sc_bus);
1220 /* make sure device is re-enumerated */
1221 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_DETACH, 0);
1223 /* wait a little for things to stabilise */
1224 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
1226 /* disable interrupts */
1227 avr32dci_mod_ien(sc, 0, 0xFFFFFFFF);
1229 /* enable interrupts */
1230 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
1231 AVR32_INT_ENDRESET, 0);
1233 /* reset all endpoints */
1234 AVR32_WRITE_4(sc, AVR32_EPTRST, (1 << AVR32_EP_MAX) - 1);
1236 /* disable all endpoints */
1237 for (n = 0; n != AVR32_EP_MAX; n++) {
1238 /* disable endpoint */
1239 AVR32_WRITE_4(sc, AVR32_EPTCTLDIS(n), AVR32_EPTCTL_EPT_ENABL);
1242 /* turn off clocks */
1244 avr32dci_clocks_off(sc);
1246 USB_BUS_UNLOCK(&sc->sc_bus);
1248 /* catch any lost interrupts */
1250 avr32dci_do_poll(&sc->sc_bus);
1252 return (0); /* success */
1256 avr32dci_uninit(struct avr32dci_softc *sc)
1260 USB_BUS_LOCK(&sc->sc_bus);
1262 /* turn on clocks */
1263 (sc->sc_clocks_on) (&sc->sc_bus);
1265 /* disable interrupts */
1266 avr32dci_mod_ien(sc, 0, 0xFFFFFFFF);
1268 /* reset all endpoints */
1269 AVR32_WRITE_4(sc, AVR32_EPTRST, (1 << AVR32_EP_MAX) - 1);
1271 /* disable all endpoints */
1272 for (n = 0; n != AVR32_EP_MAX; n++) {
1273 /* disable endpoint */
1274 AVR32_WRITE_4(sc, AVR32_EPTCTLDIS(n), AVR32_EPTCTL_EPT_ENABL);
1277 sc->sc_flags.port_powered = 0;
1278 sc->sc_flags.status_vbus = 0;
1279 sc->sc_flags.status_bus_reset = 0;
1280 sc->sc_flags.status_suspend = 0;
1281 sc->sc_flags.change_suspend = 0;
1282 sc->sc_flags.change_connect = 1;
1284 avr32dci_pull_down(sc);
1285 avr32dci_clocks_off(sc);
1287 USB_BUS_UNLOCK(&sc->sc_bus);
1291 avr32dci_suspend(struct avr32dci_softc *sc)
1297 avr32dci_resume(struct avr32dci_softc *sc)
1303 avr32dci_do_poll(struct usb_bus *bus)
1305 struct avr32dci_softc *sc = AVR32_BUS2SC(bus);
1307 USB_BUS_LOCK(&sc->sc_bus);
1308 avr32dci_interrupt_poll(sc);
1309 USB_BUS_UNLOCK(&sc->sc_bus);
1312 /*------------------------------------------------------------------------*
1313 * at91dci bulk support
1314 * at91dci control support
1315 * at91dci interrupt support
1316 *------------------------------------------------------------------------*/
1318 avr32dci_device_non_isoc_open(struct usb_xfer *xfer)
1324 avr32dci_device_non_isoc_close(struct usb_xfer *xfer)
1326 avr32dci_device_done(xfer, USB_ERR_CANCELLED);
1330 avr32dci_device_non_isoc_enter(struct usb_xfer *xfer)
1336 avr32dci_device_non_isoc_start(struct usb_xfer *xfer)
1339 avr32dci_setup_standard_chain(xfer);
1340 avr32dci_start_standard_chain(xfer);
1343 struct usb_pipe_methods avr32dci_device_non_isoc_methods =
1345 .open = avr32dci_device_non_isoc_open,
1346 .close = avr32dci_device_non_isoc_close,
1347 .enter = avr32dci_device_non_isoc_enter,
1348 .start = avr32dci_device_non_isoc_start,
1351 /*------------------------------------------------------------------------*
1352 * at91dci full speed isochronous support
1353 *------------------------------------------------------------------------*/
1355 avr32dci_device_isoc_fs_open(struct usb_xfer *xfer)
1361 avr32dci_device_isoc_fs_close(struct usb_xfer *xfer)
1363 avr32dci_device_done(xfer, USB_ERR_CANCELLED);
1367 avr32dci_device_isoc_fs_enter(struct usb_xfer *xfer)
1369 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
1374 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1375 xfer, xfer->endpoint->isoc_next, xfer->nframes);
1377 /* get the current frame index */
1378 ep_no = xfer->endpointno & UE_ADDR;
1379 nframes = (AVR32_READ_4(sc, AVR32_FNUM) / 8);
1381 nframes &= AVR32_FRAME_MASK;
1384 * check if the frame index is within the window where the frames
1387 temp = (nframes - xfer->endpoint->isoc_next) & AVR32_FRAME_MASK;
1389 if ((xfer->endpoint->is_synced == 0) ||
1390 (temp < xfer->nframes)) {
1392 * If there is data underflow or the pipe queue is
1393 * empty we schedule the transfer a few frames ahead
1394 * of the current frame position. Else two isochronous
1395 * transfers might overlap.
1397 xfer->endpoint->isoc_next = (nframes + 3) & AVR32_FRAME_MASK;
1398 xfer->endpoint->is_synced = 1;
1399 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1402 * compute how many milliseconds the insertion is ahead of the
1403 * current frame position:
1405 temp = (xfer->endpoint->isoc_next - nframes) & AVR32_FRAME_MASK;
1408 * pre-compute when the isochronous transfer will be finished:
1410 xfer->isoc_time_complete =
1411 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1414 /* compute frame number for next insertion */
1415 xfer->endpoint->isoc_next += xfer->nframes;
1418 avr32dci_setup_standard_chain(xfer);
1422 avr32dci_device_isoc_fs_start(struct usb_xfer *xfer)
1424 /* start TD chain */
1425 avr32dci_start_standard_chain(xfer);
1428 struct usb_pipe_methods avr32dci_device_isoc_fs_methods =
1430 .open = avr32dci_device_isoc_fs_open,
1431 .close = avr32dci_device_isoc_fs_close,
1432 .enter = avr32dci_device_isoc_fs_enter,
1433 .start = avr32dci_device_isoc_fs_start,
1436 /*------------------------------------------------------------------------*
1437 * at91dci root control support
1438 *------------------------------------------------------------------------*
1439 * Simulate a hardware HUB by handling all the necessary requests.
1440 *------------------------------------------------------------------------*/
1442 static const struct usb_device_descriptor avr32dci_devd = {
1443 .bLength = sizeof(struct usb_device_descriptor),
1444 .bDescriptorType = UDESC_DEVICE,
1445 .bcdUSB = {0x00, 0x02},
1446 .bDeviceClass = UDCLASS_HUB,
1447 .bDeviceSubClass = UDSUBCLASS_HUB,
1448 .bDeviceProtocol = UDPROTO_HSHUBSTT,
1449 .bMaxPacketSize = 64,
1450 .bcdDevice = {0x00, 0x01},
1453 .bNumConfigurations = 1,
1456 static const struct usb_device_qualifier avr32dci_odevd = {
1457 .bLength = sizeof(struct usb_device_qualifier),
1458 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
1459 .bcdUSB = {0x00, 0x02},
1460 .bDeviceClass = UDCLASS_HUB,
1461 .bDeviceSubClass = UDSUBCLASS_HUB,
1462 .bDeviceProtocol = UDPROTO_FSHUB,
1463 .bMaxPacketSize0 = 0,
1464 .bNumConfigurations = 0,
1467 static const struct avr32dci_config_desc avr32dci_confd = {
1469 .bLength = sizeof(struct usb_config_descriptor),
1470 .bDescriptorType = UDESC_CONFIG,
1471 .wTotalLength[0] = sizeof(avr32dci_confd),
1473 .bConfigurationValue = 1,
1474 .iConfiguration = 0,
1475 .bmAttributes = UC_SELF_POWERED,
1479 .bLength = sizeof(struct usb_interface_descriptor),
1480 .bDescriptorType = UDESC_INTERFACE,
1482 .bInterfaceClass = UICLASS_HUB,
1483 .bInterfaceSubClass = UISUBCLASS_HUB,
1484 .bInterfaceProtocol = 0,
1487 .bLength = sizeof(struct usb_endpoint_descriptor),
1488 .bDescriptorType = UDESC_ENDPOINT,
1489 .bEndpointAddress = (UE_DIR_IN | AVR32_INTR_ENDPT),
1490 .bmAttributes = UE_INTERRUPT,
1491 .wMaxPacketSize[0] = 8,
1496 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
1498 static const struct usb_hub_descriptor_min avr32dci_hubd = {
1499 .bDescLength = sizeof(avr32dci_hubd),
1500 .bDescriptorType = UDESC_HUB,
1502 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
1503 .bPwrOn2PwrGood = 50,
1504 .bHubContrCurrent = 0,
1505 .DeviceRemovable = {0}, /* port is removable */
1508 #define STRING_VENDOR \
1511 #define STRING_PRODUCT \
1512 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
1514 USB_MAKE_STRING_DESC(STRING_VENDOR, avr32dci_vendor);
1515 USB_MAKE_STRING_DESC(STRING_PRODUCT, avr32dci_product);
1518 avr32dci_roothub_exec(struct usb_device *udev,
1519 struct usb_device_request *req, const void **pptr, uint16_t *plength)
1521 struct avr32dci_softc *sc = AVR32_BUS2SC(udev->bus);
1529 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1532 ptr = (const void *)&sc->sc_hub_temp;
1536 value = UGETW(req->wValue);
1537 index = UGETW(req->wIndex);
1539 /* demultiplex the control request */
1541 switch (req->bmRequestType) {
1542 case UT_READ_DEVICE:
1543 switch (req->bRequest) {
1544 case UR_GET_DESCRIPTOR:
1545 goto tr_handle_get_descriptor;
1547 goto tr_handle_get_config;
1549 goto tr_handle_get_status;
1555 case UT_WRITE_DEVICE:
1556 switch (req->bRequest) {
1557 case UR_SET_ADDRESS:
1558 goto tr_handle_set_address;
1560 goto tr_handle_set_config;
1561 case UR_CLEAR_FEATURE:
1562 goto tr_valid; /* nop */
1563 case UR_SET_DESCRIPTOR:
1564 goto tr_valid; /* nop */
1565 case UR_SET_FEATURE:
1571 case UT_WRITE_ENDPOINT:
1572 switch (req->bRequest) {
1573 case UR_CLEAR_FEATURE:
1574 switch (UGETW(req->wValue)) {
1575 case UF_ENDPOINT_HALT:
1576 goto tr_handle_clear_halt;
1577 case UF_DEVICE_REMOTE_WAKEUP:
1578 goto tr_handle_clear_wakeup;
1583 case UR_SET_FEATURE:
1584 switch (UGETW(req->wValue)) {
1585 case UF_ENDPOINT_HALT:
1586 goto tr_handle_set_halt;
1587 case UF_DEVICE_REMOTE_WAKEUP:
1588 goto tr_handle_set_wakeup;
1593 case UR_SYNCH_FRAME:
1594 goto tr_valid; /* nop */
1600 case UT_READ_ENDPOINT:
1601 switch (req->bRequest) {
1603 goto tr_handle_get_ep_status;
1609 case UT_WRITE_INTERFACE:
1610 switch (req->bRequest) {
1611 case UR_SET_INTERFACE:
1612 goto tr_handle_set_interface;
1613 case UR_CLEAR_FEATURE:
1614 goto tr_valid; /* nop */
1615 case UR_SET_FEATURE:
1621 case UT_READ_INTERFACE:
1622 switch (req->bRequest) {
1623 case UR_GET_INTERFACE:
1624 goto tr_handle_get_interface;
1626 goto tr_handle_get_iface_status;
1632 case UT_WRITE_CLASS_INTERFACE:
1633 case UT_WRITE_VENDOR_INTERFACE:
1637 case UT_READ_CLASS_INTERFACE:
1638 case UT_READ_VENDOR_INTERFACE:
1642 case UT_WRITE_CLASS_DEVICE:
1643 switch (req->bRequest) {
1644 case UR_CLEAR_FEATURE:
1646 case UR_SET_DESCRIPTOR:
1647 case UR_SET_FEATURE:
1654 case UT_WRITE_CLASS_OTHER:
1655 switch (req->bRequest) {
1656 case UR_CLEAR_FEATURE:
1657 goto tr_handle_clear_port_feature;
1658 case UR_SET_FEATURE:
1659 goto tr_handle_set_port_feature;
1660 case UR_CLEAR_TT_BUFFER:
1670 case UT_READ_CLASS_OTHER:
1671 switch (req->bRequest) {
1672 case UR_GET_TT_STATE:
1673 goto tr_handle_get_tt_state;
1675 goto tr_handle_get_port_status;
1681 case UT_READ_CLASS_DEVICE:
1682 switch (req->bRequest) {
1683 case UR_GET_DESCRIPTOR:
1684 goto tr_handle_get_class_descriptor;
1686 goto tr_handle_get_class_status;
1697 tr_handle_get_descriptor:
1698 switch (value >> 8) {
1703 len = sizeof(avr32dci_devd);
1704 ptr = (const void *)&avr32dci_devd;
1710 len = sizeof(avr32dci_confd);
1711 ptr = (const void *)&avr32dci_confd;
1714 switch (value & 0xff) {
1715 case 0: /* Language table */
1716 len = sizeof(usb_string_lang_en);
1717 ptr = (const void *)&usb_string_lang_en;
1720 case 1: /* Vendor */
1721 len = sizeof(avr32dci_vendor);
1722 ptr = (const void *)&avr32dci_vendor;
1725 case 2: /* Product */
1726 len = sizeof(avr32dci_product);
1727 ptr = (const void *)&avr32dci_product;
1738 tr_handle_get_config:
1740 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
1743 tr_handle_get_status:
1745 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
1748 tr_handle_set_address:
1749 if (value & 0xFF00) {
1752 sc->sc_rt_addr = value;
1755 tr_handle_set_config:
1759 sc->sc_conf = value;
1762 tr_handle_get_interface:
1764 sc->sc_hub_temp.wValue[0] = 0;
1767 tr_handle_get_tt_state:
1768 tr_handle_get_class_status:
1769 tr_handle_get_iface_status:
1770 tr_handle_get_ep_status:
1772 USETW(sc->sc_hub_temp.wValue, 0);
1776 tr_handle_set_interface:
1777 tr_handle_set_wakeup:
1778 tr_handle_clear_wakeup:
1779 tr_handle_clear_halt:
1782 tr_handle_clear_port_feature:
1786 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
1789 case UHF_PORT_SUSPEND:
1790 avr32dci_wakeup_peer(sc);
1793 case UHF_PORT_ENABLE:
1794 sc->sc_flags.port_enabled = 0;
1798 case UHF_PORT_INDICATOR:
1799 case UHF_C_PORT_ENABLE:
1800 case UHF_C_PORT_OVER_CURRENT:
1801 case UHF_C_PORT_RESET:
1804 case UHF_PORT_POWER:
1805 sc->sc_flags.port_powered = 0;
1806 avr32dci_pull_down(sc);
1807 avr32dci_clocks_off(sc);
1809 case UHF_C_PORT_CONNECTION:
1810 /* clear connect change flag */
1811 sc->sc_flags.change_connect = 0;
1813 if (!sc->sc_flags.status_bus_reset) {
1814 /* we are not connected */
1817 /* configure the control endpoint */
1818 /* set endpoint reset */
1819 AVR32_WRITE_4(sc, AVR32_EPTRST, AVR32_EPTRST_MASK(0));
1822 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(0), AVR32_EPTSTA_FRCESTALL);
1824 /* reset data toggle */
1825 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(0), AVR32_EPTSTA_TOGGLESQ);
1828 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(0), AVR32_EPTSTA_FRCESTALL);
1831 AVR32_WRITE_4(sc, AVR32_EPTCFG(0), AVR32_EPTCFG_TYPE_CTRL |
1832 AVR32_EPTCFG_NBANK(1) | AVR32_EPTCFG_EPSIZE(6));
1834 temp = AVR32_READ_4(sc, AVR32_EPTCFG(0));
1836 if (!(temp & AVR32_EPTCFG_EPT_MAPD)) {
1837 device_printf(sc->sc_bus.bdev,
1838 "Chip rejected configuration\n");
1840 AVR32_WRITE_4(sc, AVR32_EPTCTLENB(0),
1841 AVR32_EPTCTL_EPT_ENABL);
1844 case UHF_C_PORT_SUSPEND:
1845 sc->sc_flags.change_suspend = 0;
1848 err = USB_ERR_IOERROR;
1853 tr_handle_set_port_feature:
1857 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
1860 case UHF_PORT_ENABLE:
1861 sc->sc_flags.port_enabled = 1;
1863 case UHF_PORT_SUSPEND:
1864 case UHF_PORT_RESET:
1866 case UHF_PORT_INDICATOR:
1869 case UHF_PORT_POWER:
1870 sc->sc_flags.port_powered = 1;
1873 err = USB_ERR_IOERROR;
1878 tr_handle_get_port_status:
1880 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
1885 if (sc->sc_flags.status_vbus) {
1886 avr32dci_clocks_on(sc);
1887 avr32dci_pull_up(sc);
1889 avr32dci_pull_down(sc);
1890 avr32dci_clocks_off(sc);
1893 /* Select Device Side Mode */
1895 value = UPS_PORT_MODE_DEVICE;
1897 /* Check for High Speed */
1898 if (AVR32_READ_4(sc, AVR32_INTSTA) & AVR32_INT_SPEED)
1899 value |= UPS_HIGH_SPEED;
1901 if (sc->sc_flags.port_powered) {
1902 value |= UPS_PORT_POWER;
1904 if (sc->sc_flags.port_enabled) {
1905 value |= UPS_PORT_ENABLED;
1907 if (sc->sc_flags.status_vbus &&
1908 sc->sc_flags.status_bus_reset) {
1909 value |= UPS_CURRENT_CONNECT_STATUS;
1911 if (sc->sc_flags.status_suspend) {
1912 value |= UPS_SUSPEND;
1914 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
1918 if (sc->sc_flags.change_connect) {
1919 value |= UPS_C_CONNECT_STATUS;
1921 if (sc->sc_flags.change_suspend) {
1922 value |= UPS_C_SUSPEND;
1924 USETW(sc->sc_hub_temp.ps.wPortChange, value);
1925 len = sizeof(sc->sc_hub_temp.ps);
1928 tr_handle_get_class_descriptor:
1932 ptr = (const void *)&avr32dci_hubd;
1933 len = sizeof(avr32dci_hubd);
1937 err = USB_ERR_STALLED;
1946 avr32dci_xfer_setup(struct usb_setup_params *parm)
1948 const struct usb_hw_ep_profile *pf;
1949 struct avr32dci_softc *sc;
1950 struct usb_xfer *xfer;
1956 sc = AVR32_BUS2SC(parm->udev->bus);
1957 xfer = parm->curr_xfer;
1960 * NOTE: This driver does not use any of the parameters that
1961 * are computed from the following values. Just set some
1962 * reasonable dummies:
1964 parm->hc_max_packet_size = 0x400;
1965 parm->hc_max_packet_count = 1;
1966 parm->hc_max_frame_size = 0x400;
1968 usbd_transfer_setup_sub(parm);
1971 * compute maximum number of TDs
1973 if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
1975 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
1979 ntd = xfer->nframes + 1 /* SYNC */ ;
1983 * check if "usbd_transfer_setup_sub" set an error
1989 * allocate transfer descriptors
1996 ep_no = xfer->endpointno & UE_ADDR;
1997 avr32dci_get_hw_ep_profile(parm->udev, &pf, ep_no);
2000 /* should not happen */
2001 parm->err = USB_ERR_INVAL;
2005 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2007 for (n = 0; n != ntd; n++) {
2009 struct avr32dci_td *td;
2014 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2017 td->max_packet_size = xfer->max_packet_size;
2019 temp = pf->max_in_frame_size | pf->max_out_frame_size;
2023 if (pf->support_multi_buffer) {
2024 td->support_multi_buffer = 1;
2026 td->obj_next = last_obj;
2030 parm->size[0] += sizeof(*td);
2033 xfer->td_start[0] = last_obj;
2037 avr32dci_xfer_unsetup(struct usb_xfer *xfer)
2043 avr32dci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2044 struct usb_endpoint *pipe)
2046 struct avr32dci_softc *sc = AVR32_BUS2SC(udev->bus);
2048 DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2049 pipe, udev->address,
2050 edesc->bEndpointAddress, udev->flags.usb_mode,
2051 sc->sc_rt_addr, udev->device_index);
2053 if (udev->device_index != sc->sc_rt_addr) {
2055 if ((udev->speed != USB_SPEED_FULL) &&
2056 (udev->speed != USB_SPEED_HIGH)) {
2060 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2061 pipe->methods = &avr32dci_device_isoc_fs_methods;
2063 pipe->methods = &avr32dci_device_non_isoc_methods;
2068 avr32dci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
2070 struct avr32dci_softc *sc = AVR32_BUS2SC(bus);
2073 case USB_HW_POWER_SUSPEND:
2074 avr32dci_suspend(sc);
2076 case USB_HW_POWER_SHUTDOWN:
2077 avr32dci_uninit(sc);
2079 case USB_HW_POWER_RESUME:
2080 avr32dci_resume(sc);
2087 struct usb_bus_methods avr32dci_bus_methods =
2089 .endpoint_init = &avr32dci_ep_init,
2090 .xfer_setup = &avr32dci_xfer_setup,
2091 .xfer_unsetup = &avr32dci_xfer_unsetup,
2092 .get_hw_ep_profile = &avr32dci_get_hw_ep_profile,
2093 .xfer_stall = &avr32dci_xfer_stall,
2094 .set_stall = &avr32dci_set_stall,
2095 .clear_stall = &avr32dci_clear_stall,
2096 .roothub_exec = &avr32dci_roothub_exec,
2097 .xfer_poll = &avr32dci_do_poll,
2098 .set_hw_power_sleep = &avr32dci_set_hw_power_sleep,