3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2009 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 AVR32 series USB Device
35 * NOTE: When the chip detects BUS-reset it will also reset the
36 * endpoints, Function-address and more.
38 #ifdef USB_GLOBAL_INCLUDE_FILE
39 #include USB_GLOBAL_INCLUDE_FILE
41 #include <sys/stdint.h>
42 #include <sys/stddef.h>
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/condvar.h>
53 #include <sys/sysctl.h>
55 #include <sys/unistd.h>
56 #include <sys/callout.h>
57 #include <sys/malloc.h>
60 #include <dev/usb/usb.h>
61 #include <dev/usb/usbdi.h>
63 #define USB_DEBUG_VAR avr32dci_debug
65 #include <dev/usb/usb_core.h>
66 #include <dev/usb/usb_debug.h>
67 #include <dev/usb/usb_busdma.h>
68 #include <dev/usb/usb_process.h>
69 #include <dev/usb/usb_transfer.h>
70 #include <dev/usb/usb_device.h>
71 #include <dev/usb/usb_hub.h>
72 #include <dev/usb/usb_util.h>
74 #include <dev/usb/usb_controller.h>
75 #include <dev/usb/usb_bus.h>
76 #endif /* USB_GLOBAL_INCLUDE_FILE */
78 #include <dev/usb/controller/avr32dci.h>
80 #define AVR32_BUS2SC(bus) \
81 ((struct avr32dci_softc *)(((uint8_t *)(bus)) - \
82 ((uint8_t *)&(((struct avr32dci_softc *)0)->sc_bus))))
84 #define AVR32_PC2SC(pc) \
85 AVR32_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
88 static int avr32dci_debug = 0;
90 static SYSCTL_NODE(_hw_usb, OID_AUTO, avr32dci,
91 CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
93 SYSCTL_INT(_hw_usb_avr32dci, OID_AUTO, debug, CTLFLAG_RWTUN,
94 &avr32dci_debug, 0, "AVR32 DCI debug level");
97 #define AVR32_INTR_ENDPT 1
101 static const struct usb_bus_methods avr32dci_bus_methods;
102 static const struct usb_pipe_methods avr32dci_device_non_isoc_methods;
103 static const struct usb_pipe_methods avr32dci_device_isoc_fs_methods;
105 static avr32dci_cmd_t avr32dci_setup_rx;
106 static avr32dci_cmd_t avr32dci_data_rx;
107 static avr32dci_cmd_t avr32dci_data_tx;
108 static avr32dci_cmd_t avr32dci_data_tx_sync;
109 static void avr32dci_device_done(struct usb_xfer *, usb_error_t);
110 static void avr32dci_do_poll(struct usb_bus *);
111 static void avr32dci_standard_done(struct usb_xfer *);
112 static void avr32dci_root_intr(struct avr32dci_softc *sc);
115 * Here is a list of what the chip supports:
117 static const struct usb_hw_ep_profile
118 avr32dci_ep_profile[4] = {
120 .max_in_frame_size = 64,
121 .max_out_frame_size = 64,
123 .support_control = 1,
127 .max_in_frame_size = 512,
128 .max_out_frame_size = 512,
131 .support_interrupt = 1,
132 .support_isochronous = 1,
138 .max_in_frame_size = 64,
139 .max_out_frame_size = 64,
142 .support_interrupt = 1,
148 .max_in_frame_size = 1024,
149 .max_out_frame_size = 1024,
152 .support_interrupt = 1,
153 .support_isochronous = 1,
160 avr32dci_get_hw_ep_profile(struct usb_device *udev,
161 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
164 *ppf = avr32dci_ep_profile;
165 else if (ep_addr < 3)
166 *ppf = avr32dci_ep_profile + 1;
167 else if (ep_addr < 5)
168 *ppf = avr32dci_ep_profile + 2;
169 else if (ep_addr < 7)
170 *ppf = avr32dci_ep_profile + 3;
176 avr32dci_mod_ctrl(struct avr32dci_softc *sc, uint32_t set, uint32_t clear)
180 temp = AVR32_READ_4(sc, AVR32_CTRL);
183 AVR32_WRITE_4(sc, AVR32_CTRL, temp);
187 avr32dci_mod_ien(struct avr32dci_softc *sc, uint32_t set, uint32_t clear)
191 temp = AVR32_READ_4(sc, AVR32_IEN);
194 AVR32_WRITE_4(sc, AVR32_IEN, temp);
198 avr32dci_clocks_on(struct avr32dci_softc *sc)
200 if (sc->sc_flags.clocks_off &&
201 sc->sc_flags.port_powered) {
205 (sc->sc_clocks_on) (&sc->sc_bus);
207 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_EN_USBA, 0);
209 sc->sc_flags.clocks_off = 0;
214 avr32dci_clocks_off(struct avr32dci_softc *sc)
216 if (!sc->sc_flags.clocks_off) {
219 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_EN_USBA);
221 /* turn clocks off */
222 (sc->sc_clocks_off) (&sc->sc_bus);
224 sc->sc_flags.clocks_off = 1;
229 avr32dci_pull_up(struct avr32dci_softc *sc)
231 /* pullup D+, if possible */
233 if (!sc->sc_flags.d_pulled_up &&
234 sc->sc_flags.port_powered) {
235 sc->sc_flags.d_pulled_up = 1;
236 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_DETACH);
241 avr32dci_pull_down(struct avr32dci_softc *sc)
243 /* pulldown D+, if possible */
245 if (sc->sc_flags.d_pulled_up) {
246 sc->sc_flags.d_pulled_up = 0;
247 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_DETACH, 0);
252 avr32dci_wakeup_peer(struct avr32dci_softc *sc)
254 if (!sc->sc_flags.status_suspend) {
257 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_REWAKEUP, 0);
259 /* wait 8 milliseconds */
260 /* Wait for reset to complete. */
261 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
263 /* hardware should have cleared RMWKUP bit */
267 avr32dci_set_address(struct avr32dci_softc *sc, uint8_t addr)
269 DPRINTFN(5, "addr=%d\n", addr);
271 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_FADDR_EN | addr, 0);
275 avr32dci_setup_rx(struct avr32dci_td *td)
277 struct avr32dci_softc *sc;
278 struct usb_device_request req;
282 /* get pointer to softc */
283 sc = AVR32_PC2SC(td->pc);
285 /* check endpoint status */
286 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
288 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
290 if (!(temp & AVR32_EPTSTA_RX_SETUP)) {
293 /* clear did stall */
295 /* get the packet byte count */
296 count = AVR32_EPTSTA_BYTE_COUNT(temp);
298 /* verify data length */
299 if (count != td->remainder) {
300 DPRINTFN(0, "Invalid SETUP packet "
301 "length, %d bytes\n", count);
304 if (count != sizeof(req)) {
305 DPRINTFN(0, "Unsupported SETUP packet "
306 "length, %d bytes\n", count);
310 memcpy(&req, sc->physdata, sizeof(req));
312 /* copy data into real buffer */
313 usbd_copy_in(td->pc, 0, &req, sizeof(req));
315 td->offset = sizeof(req);
318 /* sneak peek the set address */
319 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
320 (req.bRequest == UR_SET_ADDRESS)) {
321 sc->sc_dv_addr = req.wValue[0] & 0x7F;
322 /* must write address before ZLP */
323 avr32dci_mod_ctrl(sc, 0, AVR32_CTRL_DEV_FADDR_EN |
324 AVR32_CTRL_DEV_ADDR);
325 avr32dci_mod_ctrl(sc, sc->sc_dv_addr, 0);
327 sc->sc_dv_addr = 0xFF;
330 /* clear SETUP packet interrupt */
331 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_SETUP);
332 return (0); /* complete */
335 if (temp & AVR32_EPTSTA_RX_SETUP) {
336 /* clear SETUP packet interrupt */
337 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_SETUP);
339 /* abort any ongoing transfer */
340 if (!td->did_stall) {
341 DPRINTFN(5, "stalling\n");
342 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(td->ep_no),
343 AVR32_EPTSTA_FRCESTALL);
346 return (1); /* not complete */
350 avr32dci_data_rx(struct avr32dci_td *td)
352 struct avr32dci_softc *sc;
353 struct usb_page_search buf_res;
359 to = 4; /* don't loop forever! */
362 /* get pointer to softc */
363 sc = AVR32_PC2SC(td->pc);
366 /* check if any of the FIFO banks have data */
367 /* check endpoint status */
368 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
370 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
372 if (temp & AVR32_EPTSTA_RX_SETUP) {
373 if (td->remainder == 0) {
375 * We are actually complete and have
376 * received the next SETUP
378 DPRINTFN(5, "faking complete\n");
379 return (0); /* complete */
382 * USB Host Aborted the transfer.
385 return (0); /* complete */
388 if (!(temp & AVR32_EPTSTA_RX_BK_RDY)) {
392 /* get the packet byte count */
393 count = AVR32_EPTSTA_BYTE_COUNT(temp);
395 /* verify the packet byte count */
396 if (count != td->max_packet_size) {
397 if (count < td->max_packet_size) {
398 /* we have a short packet */
402 /* invalid USB packet */
404 return (0); /* we are complete */
407 /* verify the packet byte count */
408 if (count > td->remainder) {
409 /* invalid USB packet */
411 return (0); /* we are complete */
414 usbd_get_page(td->pc, td->offset, &buf_res);
416 /* get correct length */
417 if (buf_res.length > count) {
418 buf_res.length = count;
421 memcpy(buf_res.buffer, sc->physdata +
422 (AVR32_EPTSTA_CURRENT_BANK(temp) << td->bank_shift) +
423 (td->ep_no << 16) + (td->offset % td->max_packet_size), buf_res.length);
424 /* update counters */
425 count -= buf_res.length;
426 td->offset += buf_res.length;
427 td->remainder -= buf_res.length;
430 /* clear OUT packet interrupt */
431 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(td->ep_no), AVR32_EPTSTA_RX_BK_RDY);
433 /* check if we are complete */
434 if ((td->remainder == 0) || got_short) {
436 /* we are complete */
439 /* else need to receive a zero length packet */
445 return (1); /* not complete */
449 avr32dci_data_tx(struct avr32dci_td *td)
451 struct avr32dci_softc *sc;
452 struct usb_page_search buf_res;
457 to = 4; /* don't loop forever! */
459 /* get pointer to softc */
460 sc = AVR32_PC2SC(td->pc);
464 /* check endpoint status */
465 temp = AVR32_READ_4(sc, AVR32_EPTSTA(td->ep_no));
467 DPRINTFN(5, "EPTSTA(%u)=0x%08x\n", td->ep_no, temp);
469 if (temp & AVR32_EPTSTA_RX_SETUP) {
471 * The current transfer was aborted
475 return (0); /* complete */
477 if (temp & AVR32_EPTSTA_TX_PK_RDY) {
478 /* cannot write any data - all banks are busy */
481 count = td->max_packet_size;
482 if (td->remainder < count) {
483 /* we have a short packet */
485 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) {
661 DPRINTFN(5, "end of reset\n");
663 /* set correct state */
664 sc->sc_flags.status_bus_reset = 1;
665 sc->sc_flags.status_suspend = 0;
666 sc->sc_flags.change_suspend = 0;
667 sc->sc_flags.change_connect = 1;
669 /* disable resume interrupt */
670 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
671 AVR32_INT_ENDRESET, AVR32_INT_WAKE_UP);
673 /* complete root HUB interrupt endpoint */
674 avr32dci_root_intr(sc);
677 * If resume and suspend is set at the same time we interpret
678 * that like RESUME. Resume is set when there is at least 3
679 * milliseconds of inactivity on the USB BUS.
681 if (status & AVR32_INT_WAKE_UP) {
682 DPRINTFN(5, "resume interrupt\n");
684 if (sc->sc_flags.status_suspend) {
685 /* update status bits */
686 sc->sc_flags.status_suspend = 0;
687 sc->sc_flags.change_suspend = 1;
689 /* disable resume interrupt */
690 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
691 AVR32_INT_ENDRESET, AVR32_INT_WAKE_UP);
693 /* complete root HUB interrupt endpoint */
694 avr32dci_root_intr(sc);
696 } else if (status & AVR32_INT_DET_SUSPD) {
697 DPRINTFN(5, "suspend interrupt\n");
699 if (!sc->sc_flags.status_suspend) {
700 /* update status bits */
701 sc->sc_flags.status_suspend = 1;
702 sc->sc_flags.change_suspend = 1;
704 /* disable suspend interrupt */
705 avr32dci_mod_ien(sc, AVR32_INT_WAKE_UP |
706 AVR32_INT_ENDRESET, AVR32_INT_DET_SUSPD);
708 /* complete root HUB interrupt endpoint */
709 avr32dci_root_intr(sc);
712 /* check for any endpoint interrupts */
713 if (status & -AVR32_INT_EPT_INT(0)) {
714 DPRINTFN(5, "real endpoint interrupt\n");
716 avr32dci_interrupt_poll(sc);
718 USB_BUS_UNLOCK(&sc->sc_bus);
722 avr32dci_setup_standard_chain_sub(struct avr32dci_std_temp *temp)
724 struct avr32dci_td *td;
726 /* get current Transfer Descriptor */
730 /* prepare for next TD */
731 temp->td_next = td->obj_next;
733 /* fill out the Transfer Descriptor */
734 td->func = temp->func;
736 td->offset = temp->offset;
737 td->remainder = temp->len;
739 td->did_stall = temp->did_stall;
740 td->short_pkt = temp->short_pkt;
741 td->alt_next = temp->setup_alt_next;
745 avr32dci_setup_standard_chain(struct usb_xfer *xfer)
747 struct avr32dci_std_temp temp;
748 struct avr32dci_softc *sc;
749 struct avr32dci_td *td;
754 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
755 xfer->address, UE_GET_ADDR(xfer->endpointno),
756 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
758 temp.max_frame_size = xfer->max_frame_size;
760 td = xfer->td_start[0];
761 xfer->td_transfer_first = td;
762 xfer->td_transfer_cache = td;
768 temp.td_next = xfer->td_start[0];
770 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
771 xfer->flags_int.isochronous_xfr;
772 temp.did_stall = !xfer->flags_int.control_stall;
774 sc = AVR32_BUS2SC(xfer->xroot->bus);
775 ep_no = (xfer->endpointno & UE_ADDR);
777 /* check if we should prepend a setup message */
779 if (xfer->flags_int.control_xfr) {
780 if (xfer->flags_int.control_hdr) {
781 temp.func = &avr32dci_setup_rx;
782 temp.len = xfer->frlengths[0];
783 temp.pc = xfer->frbuffers + 0;
784 temp.short_pkt = temp.len ? 1 : 0;
785 /* check for last frame */
786 if (xfer->nframes == 1) {
787 /* no STATUS stage yet, SETUP is last */
788 if (xfer->flags_int.control_act)
789 temp.setup_alt_next = 0;
791 avr32dci_setup_standard_chain_sub(&temp);
798 if (x != xfer->nframes) {
799 if (xfer->endpointno & UE_DIR_IN) {
800 temp.func = &avr32dci_data_tx;
803 temp.func = &avr32dci_data_rx;
807 /* setup "pc" pointer */
808 temp.pc = xfer->frbuffers + x;
812 while (x != xfer->nframes) {
813 /* DATA0 / DATA1 message */
815 temp.len = xfer->frlengths[x];
819 if (x == xfer->nframes) {
820 if (xfer->flags_int.control_xfr) {
821 if (xfer->flags_int.control_act) {
822 temp.setup_alt_next = 0;
825 temp.setup_alt_next = 0;
829 /* make sure that we send an USB packet */
834 /* regular data transfer */
836 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
839 avr32dci_setup_standard_chain_sub(&temp);
841 if (xfer->flags_int.isochronous_xfr) {
842 temp.offset += temp.len;
844 /* get next Page Cache pointer */
845 temp.pc = xfer->frbuffers + x;
849 if (xfer->flags_int.control_xfr) {
850 /* always setup a valid "pc" pointer for status and sync */
851 temp.pc = xfer->frbuffers + 0;
854 temp.setup_alt_next = 0;
856 /* check if we need to sync */
858 /* we need a SYNC point after TX */
859 temp.func = &avr32dci_data_tx_sync;
860 avr32dci_setup_standard_chain_sub(&temp);
862 /* check if we should append a status stage */
863 if (!xfer->flags_int.control_act) {
865 * Send a DATA1 message and invert the current
866 * endpoint direction.
868 if (xfer->endpointno & UE_DIR_IN) {
869 temp.func = &avr32dci_data_rx;
872 temp.func = &avr32dci_data_tx;
876 avr32dci_setup_standard_chain_sub(&temp);
878 /* we need a SYNC point after TX */
879 temp.func = &avr32dci_data_tx_sync;
880 avr32dci_setup_standard_chain_sub(&temp);
884 /* must have at least one frame! */
886 xfer->td_transfer_last = td;
890 avr32dci_timeout(void *arg)
892 struct usb_xfer *xfer = arg;
894 DPRINTF("xfer=%p\n", xfer);
896 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
898 /* transfer is transferred */
899 avr32dci_device_done(xfer, USB_ERR_TIMEOUT);
903 avr32dci_start_standard_chain(struct usb_xfer *xfer)
907 /* poll one time - will turn on interrupts */
908 if (avr32dci_xfer_do_fifo(xfer)) {
909 uint8_t ep_no = xfer->endpointno & UE_ADDR;
910 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
912 avr32dci_mod_ien(sc, AVR32_INT_EPT_INT(ep_no), 0);
914 /* put transfer on interrupt queue */
915 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
917 /* start timeout, if any */
918 if (xfer->timeout != 0) {
919 usbd_transfer_timeout_ms(xfer,
920 &avr32dci_timeout, xfer->timeout);
926 avr32dci_root_intr(struct avr32dci_softc *sc)
930 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
933 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
935 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
936 sizeof(sc->sc_hub_idata));
940 avr32dci_standard_done_sub(struct usb_xfer *xfer)
942 struct avr32dci_td *td;
948 td = xfer->td_transfer_cache;
953 if (xfer->aframes != xfer->nframes) {
955 * Verify the length and subtract
956 * the remainder from "frlengths[]":
958 if (len > xfer->frlengths[xfer->aframes]) {
961 xfer->frlengths[xfer->aframes] -= len;
964 /* Check for transfer error */
966 /* the transfer is finished */
971 /* Check for short transfer */
973 if (xfer->flags_int.short_frames_ok ||
974 xfer->flags_int.isochronous_xfr) {
975 /* follow alt next */
982 /* the transfer is finished */
990 /* this USB frame is complete */
996 /* update transfer cache */
998 xfer->td_transfer_cache = td;
1001 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1005 avr32dci_standard_done(struct usb_xfer *xfer)
1007 usb_error_t err = 0;
1009 DPRINTFN(13, "xfer=%p pipe=%p transfer done\n",
1010 xfer, xfer->endpoint);
1014 xfer->td_transfer_cache = xfer->td_transfer_first;
1016 if (xfer->flags_int.control_xfr) {
1017 if (xfer->flags_int.control_hdr) {
1018 err = avr32dci_standard_done_sub(xfer);
1022 if (xfer->td_transfer_cache == NULL) {
1026 while (xfer->aframes != xfer->nframes) {
1027 err = avr32dci_standard_done_sub(xfer);
1030 if (xfer->td_transfer_cache == NULL) {
1035 if (xfer->flags_int.control_xfr &&
1036 !xfer->flags_int.control_act) {
1037 err = avr32dci_standard_done_sub(xfer);
1040 avr32dci_device_done(xfer, err);
1043 /*------------------------------------------------------------------------*
1044 * avr32dci_device_done
1046 * NOTE: this function can be called more than one time on the
1047 * same USB transfer!
1048 *------------------------------------------------------------------------*/
1050 avr32dci_device_done(struct usb_xfer *xfer, usb_error_t error)
1052 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
1055 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1057 DPRINTFN(9, "xfer=%p, pipe=%p, error=%d\n",
1058 xfer, xfer->endpoint, error);
1060 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1061 ep_no = (xfer->endpointno & UE_ADDR);
1063 /* disable endpoint interrupt */
1064 avr32dci_mod_ien(sc, 0, AVR32_INT_EPT_INT(ep_no));
1066 DPRINTFN(15, "disabled interrupts!\n");
1068 /* dequeue transfer and start next transfer */
1069 usbd_transfer_done(xfer, error);
1073 avr32dci_xfer_stall(struct usb_xfer *xfer)
1075 avr32dci_device_done(xfer, USB_ERR_STALLED);
1079 avr32dci_set_stall(struct usb_device *udev,
1080 struct usb_endpoint *pipe, uint8_t *did_stall)
1082 struct avr32dci_softc *sc;
1085 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1087 DPRINTFN(5, "pipe=%p\n", pipe);
1089 sc = AVR32_BUS2SC(udev->bus);
1090 /* get endpoint number */
1091 ep_no = (pipe->edesc->bEndpointAddress & UE_ADDR);
1093 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1097 avr32dci_clear_stall_sub(struct avr32dci_softc *sc, uint8_t ep_no,
1098 uint8_t ep_type, uint8_t ep_dir)
1100 const struct usb_hw_ep_profile *pf;
1105 if (ep_type == UE_CONTROL) {
1106 /* clearing stall is not needed */
1109 /* set endpoint reset */
1110 AVR32_WRITE_4(sc, AVR32_EPTRST, AVR32_EPTRST_MASK(ep_no));
1113 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1115 /* reset data toggle */
1116 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(ep_no), AVR32_EPTSTA_TOGGLESQ);
1119 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(ep_no), AVR32_EPTSTA_FRCESTALL);
1121 if (ep_type == UE_BULK) {
1122 temp = AVR32_EPTCFG_TYPE_BULK;
1123 } else if (ep_type == UE_INTERRUPT) {
1124 temp = AVR32_EPTCFG_TYPE_INTR;
1126 temp = AVR32_EPTCFG_TYPE_ISOC |
1127 AVR32_EPTCFG_NB_TRANS(1);
1129 if (ep_dir & UE_DIR_IN) {
1130 temp |= AVR32_EPTCFG_EPDIR_IN;
1132 avr32dci_get_hw_ep_profile(NULL, &pf, ep_no);
1134 /* compute endpoint size (use maximum) */
1135 epsize = pf->max_in_frame_size | pf->max_out_frame_size;
1137 while ((epsize /= 2))
1139 temp |= AVR32_EPTCFG_EPSIZE(n);
1141 /* use the maximum number of banks supported */
1143 temp |= AVR32_EPTCFG_NBANK(1);
1145 temp |= AVR32_EPTCFG_NBANK(2);
1147 temp |= AVR32_EPTCFG_NBANK(3);
1149 AVR32_WRITE_4(sc, AVR32_EPTCFG(ep_no), temp);
1151 temp = AVR32_READ_4(sc, AVR32_EPTCFG(ep_no));
1153 if (!(temp & AVR32_EPTCFG_EPT_MAPD)) {
1154 device_printf(sc->sc_bus.bdev, "Chip rejected configuration\n");
1156 AVR32_WRITE_4(sc, AVR32_EPTCTLENB(ep_no),
1157 AVR32_EPTCTL_EPT_ENABL);
1162 avr32dci_clear_stall(struct usb_device *udev, struct usb_endpoint *pipe)
1164 struct avr32dci_softc *sc;
1165 struct usb_endpoint_descriptor *ed;
1167 DPRINTFN(5, "pipe=%p\n", pipe);
1169 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1172 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1177 sc = AVR32_BUS2SC(udev->bus);
1179 /* get endpoint descriptor */
1182 /* reset endpoint */
1183 avr32dci_clear_stall_sub(sc,
1184 (ed->bEndpointAddress & UE_ADDR),
1185 (ed->bmAttributes & UE_XFERTYPE),
1186 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1190 avr32dci_init(struct avr32dci_softc *sc)
1196 /* set up the bus structure */
1197 sc->sc_bus.usbrev = USB_REV_1_1;
1198 sc->sc_bus.methods = &avr32dci_bus_methods;
1200 USB_BUS_LOCK(&sc->sc_bus);
1202 /* make sure USB is enabled */
1203 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_EN_USBA, 0);
1205 /* turn on clocks */
1206 (sc->sc_clocks_on) (&sc->sc_bus);
1208 /* make sure device is re-enumerated */
1209 avr32dci_mod_ctrl(sc, AVR32_CTRL_DEV_DETACH, 0);
1211 /* wait a little for things to stabilise */
1212 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
1214 /* disable interrupts */
1215 avr32dci_mod_ien(sc, 0, 0xFFFFFFFF);
1217 /* enable interrupts */
1218 avr32dci_mod_ien(sc, AVR32_INT_DET_SUSPD |
1219 AVR32_INT_ENDRESET, 0);
1221 /* reset all endpoints */
1222 AVR32_WRITE_4(sc, AVR32_EPTRST, (1 << AVR32_EP_MAX) - 1);
1224 /* disable all endpoints */
1225 for (n = 0; n != AVR32_EP_MAX; n++) {
1226 /* disable endpoint */
1227 AVR32_WRITE_4(sc, AVR32_EPTCTLDIS(n), AVR32_EPTCTL_EPT_ENABL);
1230 /* turn off clocks */
1232 avr32dci_clocks_off(sc);
1234 USB_BUS_UNLOCK(&sc->sc_bus);
1236 /* catch any lost interrupts */
1238 avr32dci_do_poll(&sc->sc_bus);
1240 return (0); /* success */
1244 avr32dci_uninit(struct avr32dci_softc *sc)
1248 USB_BUS_LOCK(&sc->sc_bus);
1250 /* turn on clocks */
1251 (sc->sc_clocks_on) (&sc->sc_bus);
1253 /* disable interrupts */
1254 avr32dci_mod_ien(sc, 0, 0xFFFFFFFF);
1256 /* reset all endpoints */
1257 AVR32_WRITE_4(sc, AVR32_EPTRST, (1 << AVR32_EP_MAX) - 1);
1259 /* disable all endpoints */
1260 for (n = 0; n != AVR32_EP_MAX; n++) {
1261 /* disable endpoint */
1262 AVR32_WRITE_4(sc, AVR32_EPTCTLDIS(n), AVR32_EPTCTL_EPT_ENABL);
1265 sc->sc_flags.port_powered = 0;
1266 sc->sc_flags.status_vbus = 0;
1267 sc->sc_flags.status_bus_reset = 0;
1268 sc->sc_flags.status_suspend = 0;
1269 sc->sc_flags.change_suspend = 0;
1270 sc->sc_flags.change_connect = 1;
1272 avr32dci_pull_down(sc);
1273 avr32dci_clocks_off(sc);
1275 USB_BUS_UNLOCK(&sc->sc_bus);
1279 avr32dci_suspend(struct avr32dci_softc *sc)
1285 avr32dci_resume(struct avr32dci_softc *sc)
1291 avr32dci_do_poll(struct usb_bus *bus)
1293 struct avr32dci_softc *sc = AVR32_BUS2SC(bus);
1295 USB_BUS_LOCK(&sc->sc_bus);
1296 avr32dci_interrupt_poll(sc);
1297 USB_BUS_UNLOCK(&sc->sc_bus);
1300 /*------------------------------------------------------------------------*
1301 * avr32dci bulk support
1302 * avr32dci control support
1303 * avr32dci interrupt support
1304 *------------------------------------------------------------------------*/
1306 avr32dci_device_non_isoc_open(struct usb_xfer *xfer)
1312 avr32dci_device_non_isoc_close(struct usb_xfer *xfer)
1314 avr32dci_device_done(xfer, USB_ERR_CANCELLED);
1318 avr32dci_device_non_isoc_enter(struct usb_xfer *xfer)
1324 avr32dci_device_non_isoc_start(struct usb_xfer *xfer)
1327 avr32dci_setup_standard_chain(xfer);
1328 avr32dci_start_standard_chain(xfer);
1331 static const struct usb_pipe_methods avr32dci_device_non_isoc_methods =
1333 .open = avr32dci_device_non_isoc_open,
1334 .close = avr32dci_device_non_isoc_close,
1335 .enter = avr32dci_device_non_isoc_enter,
1336 .start = avr32dci_device_non_isoc_start,
1339 /*------------------------------------------------------------------------*
1340 * avr32dci full speed isochronous support
1341 *------------------------------------------------------------------------*/
1343 avr32dci_device_isoc_fs_open(struct usb_xfer *xfer)
1349 avr32dci_device_isoc_fs_close(struct usb_xfer *xfer)
1351 avr32dci_device_done(xfer, USB_ERR_CANCELLED);
1355 avr32dci_device_isoc_fs_enter(struct usb_xfer *xfer)
1357 struct avr32dci_softc *sc = AVR32_BUS2SC(xfer->xroot->bus);
1361 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1362 xfer, xfer->endpoint->isoc_next, xfer->nframes);
1364 /* get the current frame index */
1365 ep_no = xfer->endpointno & UE_ADDR;
1366 nframes = (AVR32_READ_4(sc, AVR32_FNUM) / 8);
1368 if (usbd_xfer_get_isochronous_start_frame(
1369 xfer, nframes, 0, 1, AVR32_FRAME_MASK, NULL))
1370 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1373 avr32dci_setup_standard_chain(xfer);
1377 avr32dci_device_isoc_fs_start(struct usb_xfer *xfer)
1379 /* start TD chain */
1380 avr32dci_start_standard_chain(xfer);
1383 static const struct usb_pipe_methods avr32dci_device_isoc_fs_methods =
1385 .open = avr32dci_device_isoc_fs_open,
1386 .close = avr32dci_device_isoc_fs_close,
1387 .enter = avr32dci_device_isoc_fs_enter,
1388 .start = avr32dci_device_isoc_fs_start,
1391 /*------------------------------------------------------------------------*
1392 * avr32dci root control support
1393 *------------------------------------------------------------------------*
1394 * Simulate a hardware HUB by handling all the necessary requests.
1395 *------------------------------------------------------------------------*/
1397 static const struct usb_device_descriptor avr32dci_devd = {
1398 .bLength = sizeof(struct usb_device_descriptor),
1399 .bDescriptorType = UDESC_DEVICE,
1400 .bcdUSB = {0x00, 0x02},
1401 .bDeviceClass = UDCLASS_HUB,
1402 .bDeviceSubClass = UDSUBCLASS_HUB,
1403 .bDeviceProtocol = UDPROTO_HSHUBSTT,
1404 .bMaxPacketSize = 64,
1405 .bcdDevice = {0x00, 0x01},
1408 .bNumConfigurations = 1,
1411 static const struct usb_device_qualifier avr32dci_odevd = {
1412 .bLength = sizeof(struct usb_device_qualifier),
1413 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
1414 .bcdUSB = {0x00, 0x02},
1415 .bDeviceClass = UDCLASS_HUB,
1416 .bDeviceSubClass = UDSUBCLASS_HUB,
1417 .bDeviceProtocol = UDPROTO_FSHUB,
1418 .bMaxPacketSize0 = 0,
1419 .bNumConfigurations = 0,
1422 static const struct avr32dci_config_desc avr32dci_confd = {
1424 .bLength = sizeof(struct usb_config_descriptor),
1425 .bDescriptorType = UDESC_CONFIG,
1426 .wTotalLength[0] = sizeof(avr32dci_confd),
1428 .bConfigurationValue = 1,
1429 .iConfiguration = 0,
1430 .bmAttributes = UC_SELF_POWERED,
1434 .bLength = sizeof(struct usb_interface_descriptor),
1435 .bDescriptorType = UDESC_INTERFACE,
1437 .bInterfaceClass = UICLASS_HUB,
1438 .bInterfaceSubClass = UISUBCLASS_HUB,
1439 .bInterfaceProtocol = 0,
1442 .bLength = sizeof(struct usb_endpoint_descriptor),
1443 .bDescriptorType = UDESC_ENDPOINT,
1444 .bEndpointAddress = (UE_DIR_IN | AVR32_INTR_ENDPT),
1445 .bmAttributes = UE_INTERRUPT,
1446 .wMaxPacketSize[0] = 8,
1450 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
1452 static const struct usb_hub_descriptor_min avr32dci_hubd = {
1453 .bDescLength = sizeof(avr32dci_hubd),
1454 .bDescriptorType = UDESC_HUB,
1456 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
1457 .bPwrOn2PwrGood = 50,
1458 .bHubContrCurrent = 0,
1459 .DeviceRemovable = {0}, /* port is removable */
1462 #define STRING_VENDOR \
1465 #define STRING_PRODUCT \
1466 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
1468 USB_MAKE_STRING_DESC(STRING_VENDOR, avr32dci_vendor);
1469 USB_MAKE_STRING_DESC(STRING_PRODUCT, avr32dci_product);
1472 avr32dci_roothub_exec(struct usb_device *udev,
1473 struct usb_device_request *req, const void **pptr, uint16_t *plength)
1475 struct avr32dci_softc *sc = AVR32_BUS2SC(udev->bus);
1483 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1486 ptr = (const void *)&sc->sc_hub_temp;
1490 value = UGETW(req->wValue);
1491 index = UGETW(req->wIndex);
1493 /* demultiplex the control request */
1495 switch (req->bmRequestType) {
1496 case UT_READ_DEVICE:
1497 switch (req->bRequest) {
1498 case UR_GET_DESCRIPTOR:
1499 goto tr_handle_get_descriptor;
1501 goto tr_handle_get_config;
1503 goto tr_handle_get_status;
1509 case UT_WRITE_DEVICE:
1510 switch (req->bRequest) {
1511 case UR_SET_ADDRESS:
1512 goto tr_handle_set_address;
1514 goto tr_handle_set_config;
1515 case UR_CLEAR_FEATURE:
1516 goto tr_valid; /* nop */
1517 case UR_SET_DESCRIPTOR:
1518 goto tr_valid; /* nop */
1519 case UR_SET_FEATURE:
1525 case UT_WRITE_ENDPOINT:
1526 switch (req->bRequest) {
1527 case UR_CLEAR_FEATURE:
1528 switch (UGETW(req->wValue)) {
1529 case UF_ENDPOINT_HALT:
1530 goto tr_handle_clear_halt;
1531 case UF_DEVICE_REMOTE_WAKEUP:
1532 goto tr_handle_clear_wakeup;
1537 case UR_SET_FEATURE:
1538 switch (UGETW(req->wValue)) {
1539 case UF_ENDPOINT_HALT:
1540 goto tr_handle_set_halt;
1541 case UF_DEVICE_REMOTE_WAKEUP:
1542 goto tr_handle_set_wakeup;
1547 case UR_SYNCH_FRAME:
1548 goto tr_valid; /* nop */
1554 case UT_READ_ENDPOINT:
1555 switch (req->bRequest) {
1557 goto tr_handle_get_ep_status;
1563 case UT_WRITE_INTERFACE:
1564 switch (req->bRequest) {
1565 case UR_SET_INTERFACE:
1566 goto tr_handle_set_interface;
1567 case UR_CLEAR_FEATURE:
1568 goto tr_valid; /* nop */
1569 case UR_SET_FEATURE:
1575 case UT_READ_INTERFACE:
1576 switch (req->bRequest) {
1577 case UR_GET_INTERFACE:
1578 goto tr_handle_get_interface;
1580 goto tr_handle_get_iface_status;
1586 case UT_WRITE_CLASS_INTERFACE:
1587 case UT_WRITE_VENDOR_INTERFACE:
1591 case UT_READ_CLASS_INTERFACE:
1592 case UT_READ_VENDOR_INTERFACE:
1596 case UT_WRITE_CLASS_DEVICE:
1597 switch (req->bRequest) {
1598 case UR_CLEAR_FEATURE:
1600 case UR_SET_DESCRIPTOR:
1601 case UR_SET_FEATURE:
1608 case UT_WRITE_CLASS_OTHER:
1609 switch (req->bRequest) {
1610 case UR_CLEAR_FEATURE:
1611 goto tr_handle_clear_port_feature;
1612 case UR_SET_FEATURE:
1613 goto tr_handle_set_port_feature;
1614 case UR_CLEAR_TT_BUFFER:
1624 case UT_READ_CLASS_OTHER:
1625 switch (req->bRequest) {
1626 case UR_GET_TT_STATE:
1627 goto tr_handle_get_tt_state;
1629 goto tr_handle_get_port_status;
1635 case UT_READ_CLASS_DEVICE:
1636 switch (req->bRequest) {
1637 case UR_GET_DESCRIPTOR:
1638 goto tr_handle_get_class_descriptor;
1640 goto tr_handle_get_class_status;
1651 tr_handle_get_descriptor:
1652 switch (value >> 8) {
1657 len = sizeof(avr32dci_devd);
1658 ptr = (const void *)&avr32dci_devd;
1660 case UDESC_DEVICE_QUALIFIER:
1663 len = sizeof(avr32dci_odevd);
1664 ptr = (const void *)&avr32dci_odevd;
1670 len = sizeof(avr32dci_confd);
1671 ptr = (const void *)&avr32dci_confd;
1674 switch (value & 0xff) {
1675 case 0: /* Language table */
1676 len = sizeof(usb_string_lang_en);
1677 ptr = (const void *)&usb_string_lang_en;
1680 case 1: /* Vendor */
1681 len = sizeof(avr32dci_vendor);
1682 ptr = (const void *)&avr32dci_vendor;
1685 case 2: /* Product */
1686 len = sizeof(avr32dci_product);
1687 ptr = (const void *)&avr32dci_product;
1698 tr_handle_get_config:
1700 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
1703 tr_handle_get_status:
1705 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
1708 tr_handle_set_address:
1709 if (value & 0xFF00) {
1712 sc->sc_rt_addr = value;
1715 tr_handle_set_config:
1719 sc->sc_conf = value;
1722 tr_handle_get_interface:
1724 sc->sc_hub_temp.wValue[0] = 0;
1727 tr_handle_get_tt_state:
1728 tr_handle_get_class_status:
1729 tr_handle_get_iface_status:
1730 tr_handle_get_ep_status:
1732 USETW(sc->sc_hub_temp.wValue, 0);
1736 tr_handle_set_interface:
1737 tr_handle_set_wakeup:
1738 tr_handle_clear_wakeup:
1739 tr_handle_clear_halt:
1742 tr_handle_clear_port_feature:
1746 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
1749 case UHF_PORT_SUSPEND:
1750 avr32dci_wakeup_peer(sc);
1753 case UHF_PORT_ENABLE:
1754 sc->sc_flags.port_enabled = 0;
1758 case UHF_PORT_INDICATOR:
1759 case UHF_C_PORT_ENABLE:
1760 case UHF_C_PORT_OVER_CURRENT:
1761 case UHF_C_PORT_RESET:
1764 case UHF_PORT_POWER:
1765 sc->sc_flags.port_powered = 0;
1766 avr32dci_pull_down(sc);
1767 avr32dci_clocks_off(sc);
1769 case UHF_C_PORT_CONNECTION:
1770 /* clear connect change flag */
1771 sc->sc_flags.change_connect = 0;
1773 if (!sc->sc_flags.status_bus_reset) {
1774 /* we are not connected */
1777 /* configure the control endpoint */
1778 /* set endpoint reset */
1779 AVR32_WRITE_4(sc, AVR32_EPTRST, AVR32_EPTRST_MASK(0));
1782 AVR32_WRITE_4(sc, AVR32_EPTSETSTA(0), AVR32_EPTSTA_FRCESTALL);
1784 /* reset data toggle */
1785 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(0), AVR32_EPTSTA_TOGGLESQ);
1788 AVR32_WRITE_4(sc, AVR32_EPTCLRSTA(0), AVR32_EPTSTA_FRCESTALL);
1791 AVR32_WRITE_4(sc, AVR32_EPTCFG(0), AVR32_EPTCFG_TYPE_CTRL |
1792 AVR32_EPTCFG_NBANK(1) | AVR32_EPTCFG_EPSIZE(6));
1794 temp = AVR32_READ_4(sc, AVR32_EPTCFG(0));
1796 if (!(temp & AVR32_EPTCFG_EPT_MAPD)) {
1797 device_printf(sc->sc_bus.bdev,
1798 "Chip rejected configuration\n");
1800 AVR32_WRITE_4(sc, AVR32_EPTCTLENB(0),
1801 AVR32_EPTCTL_EPT_ENABL);
1804 case UHF_C_PORT_SUSPEND:
1805 sc->sc_flags.change_suspend = 0;
1808 err = USB_ERR_IOERROR;
1813 tr_handle_set_port_feature:
1817 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
1820 case UHF_PORT_ENABLE:
1821 sc->sc_flags.port_enabled = 1;
1823 case UHF_PORT_SUSPEND:
1824 case UHF_PORT_RESET:
1826 case UHF_PORT_INDICATOR:
1829 case UHF_PORT_POWER:
1830 sc->sc_flags.port_powered = 1;
1833 err = USB_ERR_IOERROR;
1838 tr_handle_get_port_status:
1840 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
1845 if (sc->sc_flags.status_vbus) {
1846 avr32dci_clocks_on(sc);
1847 avr32dci_pull_up(sc);
1849 avr32dci_pull_down(sc);
1850 avr32dci_clocks_off(sc);
1853 /* Select Device Side Mode */
1855 value = UPS_PORT_MODE_DEVICE;
1857 /* Check for High Speed */
1858 if (AVR32_READ_4(sc, AVR32_INTSTA) & AVR32_INT_SPEED)
1859 value |= UPS_HIGH_SPEED;
1861 if (sc->sc_flags.port_powered) {
1862 value |= UPS_PORT_POWER;
1864 if (sc->sc_flags.port_enabled) {
1865 value |= UPS_PORT_ENABLED;
1867 if (sc->sc_flags.status_vbus &&
1868 sc->sc_flags.status_bus_reset) {
1869 value |= UPS_CURRENT_CONNECT_STATUS;
1871 if (sc->sc_flags.status_suspend) {
1872 value |= UPS_SUSPEND;
1874 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
1878 if (sc->sc_flags.change_connect) {
1879 value |= UPS_C_CONNECT_STATUS;
1881 if (sc->sc_flags.change_suspend) {
1882 value |= UPS_C_SUSPEND;
1884 USETW(sc->sc_hub_temp.ps.wPortChange, value);
1885 len = sizeof(sc->sc_hub_temp.ps);
1888 tr_handle_get_class_descriptor:
1892 ptr = (const void *)&avr32dci_hubd;
1893 len = sizeof(avr32dci_hubd);
1897 err = USB_ERR_STALLED;
1906 avr32dci_xfer_setup(struct usb_setup_params *parm)
1908 const struct usb_hw_ep_profile *pf;
1909 struct avr32dci_softc *sc;
1910 struct usb_xfer *xfer;
1916 sc = AVR32_BUS2SC(parm->udev->bus);
1917 xfer = parm->curr_xfer;
1920 * NOTE: This driver does not use any of the parameters that
1921 * are computed from the following values. Just set some
1922 * reasonable dummies:
1924 parm->hc_max_packet_size = 0x400;
1925 parm->hc_max_packet_count = 1;
1926 parm->hc_max_frame_size = 0x400;
1928 usbd_transfer_setup_sub(parm);
1931 * compute maximum number of TDs
1933 if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
1934 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
1937 ntd = xfer->nframes + 1 /* SYNC */ ;
1941 * check if "usbd_transfer_setup_sub" set an error
1947 * allocate transfer descriptors
1954 ep_no = xfer->endpointno & UE_ADDR;
1955 avr32dci_get_hw_ep_profile(parm->udev, &pf, ep_no);
1958 /* should not happen */
1959 parm->err = USB_ERR_INVAL;
1963 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
1965 for (n = 0; n != ntd; n++) {
1966 struct avr32dci_td *td;
1971 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
1974 td->max_packet_size = xfer->max_packet_size;
1976 temp = pf->max_in_frame_size | pf->max_out_frame_size;
1980 if (pf->support_multi_buffer) {
1981 td->support_multi_buffer = 1;
1983 td->obj_next = last_obj;
1987 parm->size[0] += sizeof(*td);
1990 xfer->td_start[0] = last_obj;
1994 avr32dci_xfer_unsetup(struct usb_xfer *xfer)
2000 avr32dci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2001 struct usb_endpoint *pipe)
2003 struct avr32dci_softc *sc = AVR32_BUS2SC(udev->bus);
2005 DPRINTFN(2, "pipe=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2006 pipe, udev->address,
2007 edesc->bEndpointAddress, udev->flags.usb_mode,
2008 sc->sc_rt_addr, udev->device_index);
2010 if (udev->device_index != sc->sc_rt_addr) {
2011 if ((udev->speed != USB_SPEED_FULL) &&
2012 (udev->speed != USB_SPEED_HIGH)) {
2016 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2017 pipe->methods = &avr32dci_device_isoc_fs_methods;
2019 pipe->methods = &avr32dci_device_non_isoc_methods;
2024 avr32dci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
2026 struct avr32dci_softc *sc = AVR32_BUS2SC(bus);
2029 case USB_HW_POWER_SUSPEND:
2030 avr32dci_suspend(sc);
2032 case USB_HW_POWER_SHUTDOWN:
2033 avr32dci_uninit(sc);
2035 case USB_HW_POWER_RESUME:
2036 avr32dci_resume(sc);
2043 static const struct usb_bus_methods avr32dci_bus_methods =
2045 .endpoint_init = &avr32dci_ep_init,
2046 .xfer_setup = &avr32dci_xfer_setup,
2047 .xfer_unsetup = &avr32dci_xfer_unsetup,
2048 .get_hw_ep_profile = &avr32dci_get_hw_ep_profile,
2049 .xfer_stall = &avr32dci_xfer_stall,
2050 .set_stall = &avr32dci_set_stall,
2051 .clear_stall = &avr32dci_clear_stall,
2052 .roothub_exec = &avr32dci_roothub_exec,
2053 .xfer_poll = &avr32dci_do_poll,
2054 .set_hw_power_sleep = &avr32dci_set_hw_power_sleep,