2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * This file contains the driver for the ATMEGA series USB OTG Controller. This
30 * driver currently only supports the DCI mode of the USB hardware.
34 * NOTE: When the chip detects BUS-reset it will also reset the
35 * 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 atmegadci_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/atmegadci.h>
80 #define ATMEGA_BUS2SC(bus) \
81 __containerof(bus, struct atmegadci_softc, sc_bus)
83 #define ATMEGA_PC2SC(pc) \
84 ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
87 static int atmegadci_debug = 0;
89 static SYSCTL_NODE(_hw_usb, OID_AUTO, atmegadci, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
91 SYSCTL_INT(_hw_usb_atmegadci, OID_AUTO, debug, CTLFLAG_RWTUN,
92 &atmegadci_debug, 0, "ATMEGA DCI debug level");
95 #define ATMEGA_INTR_ENDPT 1
99 static const struct usb_bus_methods atmegadci_bus_methods;
100 static const struct usb_pipe_methods atmegadci_device_non_isoc_methods;
101 static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods;
103 static atmegadci_cmd_t atmegadci_setup_rx;
104 static atmegadci_cmd_t atmegadci_data_rx;
105 static atmegadci_cmd_t atmegadci_data_tx;
106 static atmegadci_cmd_t atmegadci_data_tx_sync;
107 static void atmegadci_device_done(struct usb_xfer *, usb_error_t);
108 static void atmegadci_do_poll(struct usb_bus *);
109 static void atmegadci_standard_done(struct usb_xfer *);
110 static void atmegadci_root_intr(struct atmegadci_softc *sc);
113 * Here is a list of what the chip supports:
115 static const struct usb_hw_ep_profile
116 atmegadci_ep_profile[2] = {
118 .max_in_frame_size = 64,
119 .max_out_frame_size = 64,
121 .support_control = 1,
124 .max_in_frame_size = 64,
125 .max_out_frame_size = 64,
128 .support_interrupt = 1,
129 .support_isochronous = 1,
136 atmegadci_get_hw_ep_profile(struct usb_device *udev,
137 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
140 *ppf = atmegadci_ep_profile;
141 else if (ep_addr < ATMEGA_EP_MAX)
142 *ppf = atmegadci_ep_profile + 1;
148 atmegadci_clocks_on(struct atmegadci_softc *sc)
150 if (sc->sc_flags.clocks_off &&
151 sc->sc_flags.port_powered) {
155 (sc->sc_clocks_on) (&sc->sc_bus);
157 ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
159 ATMEGA_USBCON_OTGPADE |
160 ATMEGA_USBCON_VBUSTE);
162 sc->sc_flags.clocks_off = 0;
164 /* enable transceiver ? */
169 atmegadci_clocks_off(struct atmegadci_softc *sc)
171 if (!sc->sc_flags.clocks_off) {
174 /* disable Transceiver ? */
176 ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
178 ATMEGA_USBCON_OTGPADE |
179 ATMEGA_USBCON_FRZCLK |
180 ATMEGA_USBCON_VBUSTE);
182 /* turn clocks off */
183 (sc->sc_clocks_off) (&sc->sc_bus);
185 sc->sc_flags.clocks_off = 1;
190 atmegadci_pull_up(struct atmegadci_softc *sc)
192 /* pullup D+, if possible */
194 if (!sc->sc_flags.d_pulled_up &&
195 sc->sc_flags.port_powered) {
196 sc->sc_flags.d_pulled_up = 1;
197 ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0);
202 atmegadci_pull_down(struct atmegadci_softc *sc)
204 /* pulldown D+, if possible */
206 if (sc->sc_flags.d_pulled_up) {
207 sc->sc_flags.d_pulled_up = 0;
208 ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
213 atmegadci_wakeup_peer(struct atmegadci_softc *sc)
217 if (!sc->sc_flags.status_suspend) {
221 temp = ATMEGA_READ_1(sc, ATMEGA_UDCON);
222 ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP);
224 /* wait 8 milliseconds */
225 /* Wait for reset to complete. */
226 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
228 /* hardware should have cleared RMWKUP bit */
232 atmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr)
234 DPRINTFN(5, "addr=%d\n", addr);
236 addr |= ATMEGA_UDADDR_ADDEN;
238 ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr);
242 atmegadci_setup_rx(struct atmegadci_td *td)
244 struct atmegadci_softc *sc;
245 struct usb_device_request req;
249 /* get pointer to softc */
250 sc = ATMEGA_PC2SC(td->pc);
252 /* select endpoint number */
253 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
255 /* check endpoint status */
256 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
258 DPRINTFN(5, "UEINTX=0x%02x\n", temp);
260 if (!(temp & ATMEGA_UEINTX_RXSTPI)) {
263 /* clear did stall */
265 /* get the packet byte count */
267 (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
268 (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
270 /* mask away undefined bits */
273 /* verify data length */
274 if (count != td->remainder) {
275 DPRINTFN(0, "Invalid SETUP packet "
276 "length, %d bytes\n", count);
279 if (count != sizeof(req)) {
280 DPRINTFN(0, "Unsupported SETUP packet "
281 "length, %d bytes\n", count);
285 ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
286 (void *)&req, sizeof(req));
288 /* copy data into real buffer */
289 usbd_copy_in(td->pc, 0, &req, sizeof(req));
291 td->offset = sizeof(req);
294 /* sneak peek the set address */
295 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
296 (req.bRequest == UR_SET_ADDRESS)) {
297 sc->sc_dv_addr = req.wValue[0] & 0x7F;
298 /* must write address before ZLP */
299 ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr);
301 sc->sc_dv_addr = 0xFF;
304 /* Clear SETUP packet interrupt and all other previous interrupts */
305 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0);
306 return (0); /* complete */
309 /* abort any ongoing transfer */
310 if (!td->did_stall) {
311 DPRINTFN(5, "stalling\n");
312 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
314 ATMEGA_UECONX_STALLRQ);
317 if (temp & ATMEGA_UEINTX_RXSTPI) {
318 /* clear SETUP packet interrupt */
319 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI);
321 /* we only want to know if there is a SETUP packet */
322 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE);
323 return (1); /* not complete */
327 atmegadci_data_rx(struct atmegadci_td *td)
329 struct atmegadci_softc *sc;
330 struct usb_page_search buf_res;
336 to = 3; /* don't loop forever! */
339 /* get pointer to softc */
340 sc = ATMEGA_PC2SC(td->pc);
342 /* select endpoint number */
343 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
346 /* check if any of the FIFO banks have data */
347 /* check endpoint status */
348 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
350 DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
352 if (temp & ATMEGA_UEINTX_RXSTPI) {
353 if (td->remainder == 0) {
355 * We are actually complete and have
356 * received the next SETUP
358 DPRINTFN(5, "faking complete\n");
359 return (0); /* complete */
362 * USB Host Aborted the transfer.
365 return (0); /* complete */
368 if (!(temp & (ATMEGA_UEINTX_FIFOCON |
369 ATMEGA_UEINTX_RXOUTI))) {
373 /* get the packet byte count */
375 (ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
376 (ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
378 /* mask away undefined bits */
381 /* verify the packet byte count */
382 if (count != td->max_packet_size) {
383 if (count < td->max_packet_size) {
384 /* we have a short packet */
388 /* invalid USB packet */
390 return (0); /* we are complete */
393 /* verify the packet byte count */
394 if (count > td->remainder) {
395 /* invalid USB packet */
397 return (0); /* we are complete */
400 usbd_get_page(td->pc, td->offset, &buf_res);
402 /* get correct length */
403 if (buf_res.length > count) {
404 buf_res.length = count;
407 ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
408 buf_res.buffer, buf_res.length);
410 /* update counters */
411 count -= buf_res.length;
412 td->offset += buf_res.length;
413 td->remainder -= buf_res.length;
416 /* clear OUT packet interrupt */
417 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF);
419 /* release FIFO bank */
420 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF);
422 /* check if we are complete */
423 if ((td->remainder == 0) || got_short) {
425 /* we are complete */
428 /* else need to receive a zero length packet */
434 /* we only want to know if there is a SETUP packet or OUT packet */
435 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
436 ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE);
437 return (1); /* not complete */
441 atmegadci_data_tx(struct atmegadci_td *td)
443 struct atmegadci_softc *sc;
444 struct usb_page_search buf_res;
449 to = 3; /* don't loop forever! */
451 /* get pointer to softc */
452 sc = ATMEGA_PC2SC(td->pc);
454 /* select endpoint number */
455 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
459 /* check endpoint status */
460 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
462 DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
464 if (temp & ATMEGA_UEINTX_RXSTPI) {
466 * The current transfer was aborted
470 return (0); /* complete */
473 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
475 /* cannot write any data - a bank is busy */
479 count = td->max_packet_size;
480 if (td->remainder < count) {
481 /* we have a short packet */
483 count = td->remainder;
486 usbd_get_page(td->pc, td->offset, &buf_res);
488 /* get correct length */
489 if (buf_res.length > count) {
490 buf_res.length = count;
493 ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX,
494 buf_res.buffer, buf_res.length);
496 /* update counters */
497 count -= buf_res.length;
498 td->offset += buf_res.length;
499 td->remainder -= buf_res.length;
502 /* clear IN packet interrupt */
503 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI);
505 /* allocate FIFO bank */
506 ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON);
508 /* check remainder */
509 if (td->remainder == 0) {
511 return (0); /* complete */
513 /* else we need to transmit a short packet */
519 /* we only want to know if there is a SETUP packet or free IN packet */
520 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
521 ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
522 return (1); /* not complete */
526 atmegadci_data_tx_sync(struct atmegadci_td *td)
528 struct atmegadci_softc *sc;
531 /* get pointer to softc */
532 sc = ATMEGA_PC2SC(td->pc);
534 /* select endpoint number */
535 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
537 /* check endpoint status */
538 temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
540 DPRINTFN(5, "temp=0x%02x\n", temp);
542 if (temp & ATMEGA_UEINTX_RXSTPI) {
543 DPRINTFN(5, "faking complete\n");
545 return (0); /* complete */
548 * The control endpoint has only got one bank, so if that bank
549 * is free the packet has been transferred!
551 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
553 /* cannot write any data - a bank is busy */
556 if (sc->sc_dv_addr != 0xFF) {
557 /* set new address */
558 atmegadci_set_address(sc, sc->sc_dv_addr);
560 return (0); /* complete */
563 /* we only want to know if there is a SETUP packet or free IN packet */
564 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
565 ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
566 return (1); /* not complete */
570 atmegadci_xfer_do_fifo(struct usb_xfer *xfer)
572 struct atmegadci_td *td;
576 td = xfer->td_transfer_cache;
578 if ((td->func) (td)) {
579 /* operation in progress */
582 if (((void *)td) == xfer->td_transfer_last) {
587 } else if (td->remainder > 0) {
589 * We had a short transfer. If there is no alternate
590 * next, stop processing !
597 * Fetch the next transfer descriptor and transfer
598 * some flags to the next transfer descriptor
601 xfer->td_transfer_cache = td;
603 return (1); /* not complete */
606 /* compute all actual lengths */
608 atmegadci_standard_done(xfer);
609 return (0); /* complete */
613 atmegadci_interrupt_poll(struct atmegadci_softc *sc)
615 struct usb_xfer *xfer;
618 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
619 if (!atmegadci_xfer_do_fifo(xfer)) {
620 /* queue has been modified */
627 atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on)
629 DPRINTFN(5, "vbus = %u\n", is_on);
632 if (!sc->sc_flags.status_vbus) {
633 sc->sc_flags.status_vbus = 1;
635 /* complete root HUB interrupt endpoint */
637 atmegadci_root_intr(sc);
640 if (sc->sc_flags.status_vbus) {
641 sc->sc_flags.status_vbus = 0;
642 sc->sc_flags.status_bus_reset = 0;
643 sc->sc_flags.status_suspend = 0;
644 sc->sc_flags.change_suspend = 0;
645 sc->sc_flags.change_connect = 1;
647 /* complete root HUB interrupt endpoint */
649 atmegadci_root_intr(sc);
655 atmegadci_interrupt(struct atmegadci_softc *sc)
659 USB_BUS_LOCK(&sc->sc_bus);
661 /* read interrupt status */
662 status = ATMEGA_READ_1(sc, ATMEGA_UDINT);
664 /* clear all set interrupts */
665 ATMEGA_WRITE_1(sc, ATMEGA_UDINT, (~status) & 0x7D);
667 DPRINTFN(14, "UDINT=0x%02x\n", status);
669 /* check for any bus state change interrupts */
670 if (status & ATMEGA_UDINT_EORSTI) {
671 DPRINTFN(5, "end of reset\n");
673 /* set correct state */
674 sc->sc_flags.status_bus_reset = 1;
675 sc->sc_flags.status_suspend = 0;
676 sc->sc_flags.change_suspend = 0;
677 sc->sc_flags.change_connect = 1;
679 /* disable resume interrupt */
680 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
682 ATMEGA_UDINT_EORSTE);
684 /* complete root HUB interrupt endpoint */
685 atmegadci_root_intr(sc);
688 * If resume and suspend is set at the same time we interpret
689 * that like RESUME. Resume is set when there is at least 3
690 * milliseconds of inactivity on the USB BUS.
692 if (status & ATMEGA_UDINT_WAKEUPI) {
693 DPRINTFN(5, "resume interrupt\n");
695 if (sc->sc_flags.status_suspend) {
696 /* update status bits */
697 sc->sc_flags.status_suspend = 0;
698 sc->sc_flags.change_suspend = 1;
700 /* disable resume interrupt */
701 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
703 ATMEGA_UDINT_EORSTE);
705 /* complete root HUB interrupt endpoint */
706 atmegadci_root_intr(sc);
708 } else if (status & ATMEGA_UDINT_SUSPI) {
709 DPRINTFN(5, "suspend interrupt\n");
711 if (!sc->sc_flags.status_suspend) {
712 /* update status bits */
713 sc->sc_flags.status_suspend = 1;
714 sc->sc_flags.change_suspend = 1;
716 /* disable suspend interrupt */
717 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
718 ATMEGA_UDINT_WAKEUPE |
719 ATMEGA_UDINT_EORSTE);
721 /* complete root HUB interrupt endpoint */
722 atmegadci_root_intr(sc);
726 status = ATMEGA_READ_1(sc, ATMEGA_USBINT);
728 /* clear all set interrupts */
729 ATMEGA_WRITE_1(sc, ATMEGA_USBINT, (~status) & 0x03);
731 if (status & ATMEGA_USBINT_VBUSTI) {
734 DPRINTFN(5, "USBINT=0x%02x\n", status);
736 temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
737 atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS);
739 /* check for any endpoint interrupts */
740 status = ATMEGA_READ_1(sc, ATMEGA_UEINT);
741 /* the hardware will clear the UEINT bits automatically */
743 DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status);
745 atmegadci_interrupt_poll(sc);
747 USB_BUS_UNLOCK(&sc->sc_bus);
751 atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp)
753 struct atmegadci_td *td;
755 /* get current Transfer Descriptor */
759 /* prepare for next TD */
760 temp->td_next = td->obj_next;
762 /* fill out the Transfer Descriptor */
763 td->func = temp->func;
765 td->offset = temp->offset;
766 td->remainder = temp->len;
768 td->did_stall = temp->did_stall;
769 td->short_pkt = temp->short_pkt;
770 td->alt_next = temp->setup_alt_next;
774 atmegadci_setup_standard_chain(struct usb_xfer *xfer)
776 struct atmegadci_std_temp temp;
777 struct atmegadci_td *td;
781 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
782 xfer->address, UE_GET_ADDR(xfer->endpointno),
783 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
785 temp.max_frame_size = xfer->max_frame_size;
787 td = xfer->td_start[0];
788 xfer->td_transfer_first = td;
789 xfer->td_transfer_cache = td;
795 temp.td_next = xfer->td_start[0];
797 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
798 xfer->flags_int.isochronous_xfr;
799 temp.did_stall = !xfer->flags_int.control_stall;
801 /* check if we should prepend a setup message */
803 if (xfer->flags_int.control_xfr) {
804 if (xfer->flags_int.control_hdr) {
805 temp.func = &atmegadci_setup_rx;
806 temp.len = xfer->frlengths[0];
807 temp.pc = xfer->frbuffers + 0;
808 temp.short_pkt = temp.len ? 1 : 0;
809 /* check for last frame */
810 if (xfer->nframes == 1) {
811 /* no STATUS stage yet, SETUP is last */
812 if (xfer->flags_int.control_act)
813 temp.setup_alt_next = 0;
816 atmegadci_setup_standard_chain_sub(&temp);
823 if (x != xfer->nframes) {
824 if (xfer->endpointno & UE_DIR_IN) {
825 temp.func = &atmegadci_data_tx;
828 temp.func = &atmegadci_data_rx;
832 /* setup "pc" pointer */
833 temp.pc = xfer->frbuffers + x;
837 while (x != xfer->nframes) {
838 /* DATA0 / DATA1 message */
840 temp.len = xfer->frlengths[x];
844 if (x == xfer->nframes) {
845 if (xfer->flags_int.control_xfr) {
846 if (xfer->flags_int.control_act) {
847 temp.setup_alt_next = 0;
850 temp.setup_alt_next = 0;
854 /* make sure that we send an USB packet */
859 /* regular data transfer */
861 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
864 atmegadci_setup_standard_chain_sub(&temp);
866 if (xfer->flags_int.isochronous_xfr) {
867 temp.offset += temp.len;
869 /* get next Page Cache pointer */
870 temp.pc = xfer->frbuffers + x;
874 if (xfer->flags_int.control_xfr) {
875 /* always setup a valid "pc" pointer for status and sync */
876 temp.pc = xfer->frbuffers + 0;
879 temp.setup_alt_next = 0;
881 /* check if we need to sync */
883 /* we need a SYNC point after TX */
884 temp.func = &atmegadci_data_tx_sync;
885 atmegadci_setup_standard_chain_sub(&temp);
888 /* check if we should append a status stage */
889 if (!xfer->flags_int.control_act) {
891 * Send a DATA1 message and invert the current
892 * endpoint direction.
894 if (xfer->endpointno & UE_DIR_IN) {
895 temp.func = &atmegadci_data_rx;
898 temp.func = &atmegadci_data_tx;
902 atmegadci_setup_standard_chain_sub(&temp);
904 /* we need a SYNC point after TX */
905 temp.func = &atmegadci_data_tx_sync;
906 atmegadci_setup_standard_chain_sub(&temp);
910 /* must have at least one frame! */
912 xfer->td_transfer_last = td;
916 atmegadci_timeout(void *arg)
918 struct usb_xfer *xfer = arg;
920 DPRINTF("xfer=%p\n", xfer);
922 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
924 /* transfer is transferred */
925 atmegadci_device_done(xfer, USB_ERR_TIMEOUT);
929 atmegadci_start_standard_chain(struct usb_xfer *xfer)
933 /* poll one time - will turn on interrupts */
934 if (atmegadci_xfer_do_fifo(xfer)) {
935 /* put transfer on interrupt queue */
936 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
938 /* start timeout, if any */
939 if (xfer->timeout != 0) {
940 usbd_transfer_timeout_ms(xfer,
941 &atmegadci_timeout, xfer->timeout);
947 atmegadci_root_intr(struct atmegadci_softc *sc)
951 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
954 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
956 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
957 sizeof(sc->sc_hub_idata));
961 atmegadci_standard_done_sub(struct usb_xfer *xfer)
963 struct atmegadci_td *td;
969 td = xfer->td_transfer_cache;
974 if (xfer->aframes != xfer->nframes) {
976 * Verify the length and subtract
977 * the remainder from "frlengths[]":
979 if (len > xfer->frlengths[xfer->aframes]) {
982 xfer->frlengths[xfer->aframes] -= len;
985 /* Check for transfer error */
987 /* the transfer is finished */
992 /* Check for short transfer */
994 if (xfer->flags_int.short_frames_ok ||
995 xfer->flags_int.isochronous_xfr) {
996 /* follow alt next */
1003 /* the transfer is finished */
1011 /* this USB frame is complete */
1017 /* update transfer cache */
1019 xfer->td_transfer_cache = td;
1022 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1026 atmegadci_standard_done(struct usb_xfer *xfer)
1028 usb_error_t err = 0;
1030 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1031 xfer, xfer->endpoint);
1035 xfer->td_transfer_cache = xfer->td_transfer_first;
1037 if (xfer->flags_int.control_xfr) {
1038 if (xfer->flags_int.control_hdr) {
1039 err = atmegadci_standard_done_sub(xfer);
1043 if (xfer->td_transfer_cache == NULL) {
1047 while (xfer->aframes != xfer->nframes) {
1048 err = atmegadci_standard_done_sub(xfer);
1051 if (xfer->td_transfer_cache == NULL) {
1056 if (xfer->flags_int.control_xfr &&
1057 !xfer->flags_int.control_act) {
1058 err = atmegadci_standard_done_sub(xfer);
1061 atmegadci_device_done(xfer, err);
1064 /*------------------------------------------------------------------------*
1065 * atmegadci_device_done
1067 * NOTE: this function can be called more than one time on the
1068 * same USB transfer!
1069 *------------------------------------------------------------------------*/
1071 atmegadci_device_done(struct usb_xfer *xfer, usb_error_t error)
1073 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
1076 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1078 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
1079 xfer, xfer->endpoint, error);
1081 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1082 ep_no = (xfer->endpointno & UE_ADDR);
1084 /* select endpoint number */
1085 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
1087 /* disable endpoint interrupt */
1088 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
1090 DPRINTFN(15, "disabled interrupts!\n");
1092 /* dequeue transfer and start next transfer */
1093 usbd_transfer_done(xfer, error);
1097 atmegadci_xfer_stall(struct usb_xfer *xfer)
1099 atmegadci_device_done(xfer, USB_ERR_STALLED);
1103 atmegadci_set_stall(struct usb_device *udev,
1104 struct usb_endpoint *ep, uint8_t *did_stall)
1106 struct atmegadci_softc *sc;
1109 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1111 DPRINTFN(5, "endpoint=%p\n", ep);
1113 sc = ATMEGA_BUS2SC(udev->bus);
1114 /* get endpoint number */
1115 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
1116 /* select endpoint number */
1117 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
1119 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1120 ATMEGA_UECONX_EPEN |
1121 ATMEGA_UECONX_STALLRQ);
1125 atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no,
1126 uint8_t ep_type, uint8_t ep_dir)
1130 if (ep_type == UE_CONTROL) {
1131 /* clearing stall is not needed */
1134 /* select endpoint number */
1135 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
1137 /* set endpoint reset */
1138 ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no));
1140 /* clear endpoint reset */
1141 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1144 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1145 ATMEGA_UECONX_EPEN |
1146 ATMEGA_UECONX_STALLRQ);
1148 /* reset data toggle */
1149 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1150 ATMEGA_UECONX_EPEN |
1151 ATMEGA_UECONX_RSTDT);
1154 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1155 ATMEGA_UECONX_EPEN |
1156 ATMEGA_UECONX_STALLRQC);
1159 if (ep_type == UE_BULK) {
1160 temp = ATMEGA_UECFG0X_EPTYPE2;
1161 } else if (ep_type == UE_INTERRUPT) {
1162 temp = ATMEGA_UECFG0X_EPTYPE3;
1164 temp = ATMEGA_UECFG0X_EPTYPE1;
1166 if (ep_dir & UE_DIR_IN) {
1167 temp |= ATMEGA_UECFG0X_EPDIR;
1169 /* two banks, 64-bytes wMaxPacket */
1170 ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp);
1171 ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
1172 ATMEGA_UECFG1X_ALLOC |
1173 ATMEGA_UECFG1X_EPBK0 | /* one bank */
1174 ATMEGA_UECFG1X_EPSIZE(3));
1176 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
1177 if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1178 device_printf(sc->sc_bus.bdev,
1179 "Chip rejected configuration\n");
1185 atmegadci_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
1187 struct atmegadci_softc *sc;
1188 struct usb_endpoint_descriptor *ed;
1190 DPRINTFN(5, "endpoint=%p\n", ep);
1192 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1195 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1200 sc = ATMEGA_BUS2SC(udev->bus);
1202 /* get endpoint descriptor */
1205 /* reset endpoint */
1206 atmegadci_clear_stall_sub(sc,
1207 (ed->bEndpointAddress & UE_ADDR),
1208 (ed->bmAttributes & UE_XFERTYPE),
1209 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1213 atmegadci_init(struct atmegadci_softc *sc)
1219 /* set up the bus structure */
1220 sc->sc_bus.usbrev = USB_REV_1_1;
1221 sc->sc_bus.methods = &atmegadci_bus_methods;
1223 USB_BUS_LOCK(&sc->sc_bus);
1225 /* make sure USB is enabled */
1226 ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
1227 ATMEGA_USBCON_USBE |
1228 ATMEGA_USBCON_FRZCLK);
1230 /* enable USB PAD regulator */
1231 ATMEGA_WRITE_1(sc, ATMEGA_UHWCON,
1232 ATMEGA_UHWCON_UVREGE |
1233 ATMEGA_UHWCON_UIMOD);
1235 /* the following register sets up the USB PLL, assuming 16MHz X-tal */
1236 ATMEGA_WRITE_1(sc, 0x49 /* PLLCSR */, 0x14 | 0x02);
1238 /* wait for PLL to lock */
1239 for (n = 0; n != 20; n++) {
1240 if (ATMEGA_READ_1(sc, 0x49) & 0x01)
1242 /* wait a little bit for PLL to start */
1243 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
1246 /* make sure USB is enabled */
1247 ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
1248 ATMEGA_USBCON_USBE |
1249 ATMEGA_USBCON_OTGPADE |
1250 ATMEGA_USBCON_VBUSTE);
1252 /* turn on clocks */
1253 (sc->sc_clocks_on) (&sc->sc_bus);
1255 /* make sure device is re-enumerated */
1256 ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
1258 /* wait a little for things to stabilise */
1259 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
1261 /* enable interrupts */
1262 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
1263 ATMEGA_UDINT_SUSPE |
1264 ATMEGA_UDINT_EORSTE);
1266 /* reset all endpoints */
1267 ATMEGA_WRITE_1(sc, ATMEGA_UERST,
1268 (1 << ATMEGA_EP_MAX) - 1);
1271 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1273 /* disable all endpoints */
1274 for (n = 0; n != ATMEGA_EP_MAX; n++) {
1275 /* select endpoint */
1276 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n);
1278 /* disable endpoint interrupt */
1279 ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
1281 /* disable endpoint */
1282 ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0);
1285 /* turn off clocks */
1287 atmegadci_clocks_off(sc);
1289 /* read initial VBUS state */
1291 n = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
1292 atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS);
1294 USB_BUS_UNLOCK(&sc->sc_bus);
1296 /* catch any lost interrupts */
1298 atmegadci_do_poll(&sc->sc_bus);
1300 return (0); /* success */
1304 atmegadci_uninit(struct atmegadci_softc *sc)
1306 USB_BUS_LOCK(&sc->sc_bus);
1308 /* turn on clocks */
1309 (sc->sc_clocks_on) (&sc->sc_bus);
1311 /* disable interrupts */
1312 ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0);
1314 /* reset all endpoints */
1315 ATMEGA_WRITE_1(sc, ATMEGA_UERST,
1316 (1 << ATMEGA_EP_MAX) - 1);
1319 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1321 sc->sc_flags.port_powered = 0;
1322 sc->sc_flags.status_vbus = 0;
1323 sc->sc_flags.status_bus_reset = 0;
1324 sc->sc_flags.status_suspend = 0;
1325 sc->sc_flags.change_suspend = 0;
1326 sc->sc_flags.change_connect = 1;
1328 atmegadci_pull_down(sc);
1329 atmegadci_clocks_off(sc);
1331 /* disable USB PAD regulator */
1332 ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0);
1334 USB_BUS_UNLOCK(&sc->sc_bus);
1338 atmegadci_suspend(struct atmegadci_softc *sc)
1344 atmegadci_resume(struct atmegadci_softc *sc)
1350 atmegadci_do_poll(struct usb_bus *bus)
1352 struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
1354 USB_BUS_LOCK(&sc->sc_bus);
1355 atmegadci_interrupt_poll(sc);
1356 USB_BUS_UNLOCK(&sc->sc_bus);
1359 /*------------------------------------------------------------------------*
1360 * atmegadci bulk support
1361 * atmegadci control support
1362 * atmegadci interrupt support
1363 *------------------------------------------------------------------------*/
1365 atmegadci_device_non_isoc_open(struct usb_xfer *xfer)
1371 atmegadci_device_non_isoc_close(struct usb_xfer *xfer)
1373 atmegadci_device_done(xfer, USB_ERR_CANCELLED);
1377 atmegadci_device_non_isoc_enter(struct usb_xfer *xfer)
1383 atmegadci_device_non_isoc_start(struct usb_xfer *xfer)
1386 atmegadci_setup_standard_chain(xfer);
1387 atmegadci_start_standard_chain(xfer);
1390 static const struct usb_pipe_methods atmegadci_device_non_isoc_methods =
1392 .open = atmegadci_device_non_isoc_open,
1393 .close = atmegadci_device_non_isoc_close,
1394 .enter = atmegadci_device_non_isoc_enter,
1395 .start = atmegadci_device_non_isoc_start,
1398 /*------------------------------------------------------------------------*
1399 * atmegadci full speed isochronous support
1400 *------------------------------------------------------------------------*/
1402 atmegadci_device_isoc_fs_open(struct usb_xfer *xfer)
1408 atmegadci_device_isoc_fs_close(struct usb_xfer *xfer)
1410 atmegadci_device_done(xfer, USB_ERR_CANCELLED);
1414 atmegadci_device_isoc_fs_enter(struct usb_xfer *xfer)
1416 struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
1419 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1420 xfer, xfer->endpoint->isoc_next, xfer->nframes);
1422 /* get the current frame index */
1425 (ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) |
1426 (ATMEGA_READ_1(sc, ATMEGA_UDFNUML));
1428 if (usbd_xfer_get_isochronous_start_frame(
1429 xfer, nframes, 0, 1, ATMEGA_FRAME_MASK, NULL))
1430 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1433 atmegadci_setup_standard_chain(xfer);
1437 atmegadci_device_isoc_fs_start(struct usb_xfer *xfer)
1439 /* start TD chain */
1440 atmegadci_start_standard_chain(xfer);
1443 static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods =
1445 .open = atmegadci_device_isoc_fs_open,
1446 .close = atmegadci_device_isoc_fs_close,
1447 .enter = atmegadci_device_isoc_fs_enter,
1448 .start = atmegadci_device_isoc_fs_start,
1451 /*------------------------------------------------------------------------*
1452 * atmegadci root control support
1453 *------------------------------------------------------------------------*
1454 * Simulate a hardware HUB by handling all the necessary requests.
1455 *------------------------------------------------------------------------*/
1457 static const struct usb_device_descriptor atmegadci_devd = {
1458 .bLength = sizeof(struct usb_device_descriptor),
1459 .bDescriptorType = UDESC_DEVICE,
1460 .bcdUSB = {0x00, 0x02},
1461 .bDeviceClass = UDCLASS_HUB,
1462 .bDeviceSubClass = UDSUBCLASS_HUB,
1463 .bDeviceProtocol = UDPROTO_FSHUB,
1464 .bMaxPacketSize = 64,
1465 .bcdDevice = {0x00, 0x01},
1468 .bNumConfigurations = 1,
1471 static const struct atmegadci_config_desc atmegadci_confd = {
1473 .bLength = sizeof(struct usb_config_descriptor),
1474 .bDescriptorType = UDESC_CONFIG,
1475 .wTotalLength[0] = sizeof(atmegadci_confd),
1477 .bConfigurationValue = 1,
1478 .iConfiguration = 0,
1479 .bmAttributes = UC_SELF_POWERED,
1483 .bLength = sizeof(struct usb_interface_descriptor),
1484 .bDescriptorType = UDESC_INTERFACE,
1486 .bInterfaceClass = UICLASS_HUB,
1487 .bInterfaceSubClass = UISUBCLASS_HUB,
1488 .bInterfaceProtocol = 0,
1491 .bLength = sizeof(struct usb_endpoint_descriptor),
1492 .bDescriptorType = UDESC_ENDPOINT,
1493 .bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT),
1494 .bmAttributes = UE_INTERRUPT,
1495 .wMaxPacketSize[0] = 8,
1499 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
1501 static const struct usb_hub_descriptor_min atmegadci_hubd = {
1502 .bDescLength = sizeof(atmegadci_hubd),
1503 .bDescriptorType = UDESC_HUB,
1505 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
1506 .bPwrOn2PwrGood = 50,
1507 .bHubContrCurrent = 0,
1508 .DeviceRemovable = {0}, /* port is removable */
1511 #define STRING_VENDOR \
1514 #define STRING_PRODUCT \
1515 "D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
1517 USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor);
1518 USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product);
1521 atmegadci_roothub_exec(struct usb_device *udev,
1522 struct usb_device_request *req, const void **pptr, uint16_t *plength)
1524 struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
1532 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1535 ptr = (const void *)&sc->sc_hub_temp;
1539 value = UGETW(req->wValue);
1540 index = UGETW(req->wIndex);
1542 /* demultiplex the control request */
1544 switch (req->bmRequestType) {
1545 case UT_READ_DEVICE:
1546 switch (req->bRequest) {
1547 case UR_GET_DESCRIPTOR:
1548 goto tr_handle_get_descriptor;
1550 goto tr_handle_get_config;
1552 goto tr_handle_get_status;
1558 case UT_WRITE_DEVICE:
1559 switch (req->bRequest) {
1560 case UR_SET_ADDRESS:
1561 goto tr_handle_set_address;
1563 goto tr_handle_set_config;
1564 case UR_CLEAR_FEATURE:
1565 goto tr_valid; /* nop */
1566 case UR_SET_DESCRIPTOR:
1567 goto tr_valid; /* nop */
1568 case UR_SET_FEATURE:
1574 case UT_WRITE_ENDPOINT:
1575 switch (req->bRequest) {
1576 case UR_CLEAR_FEATURE:
1577 switch (UGETW(req->wValue)) {
1578 case UF_ENDPOINT_HALT:
1579 goto tr_handle_clear_halt;
1580 case UF_DEVICE_REMOTE_WAKEUP:
1581 goto tr_handle_clear_wakeup;
1586 case UR_SET_FEATURE:
1587 switch (UGETW(req->wValue)) {
1588 case UF_ENDPOINT_HALT:
1589 goto tr_handle_set_halt;
1590 case UF_DEVICE_REMOTE_WAKEUP:
1591 goto tr_handle_set_wakeup;
1596 case UR_SYNCH_FRAME:
1597 goto tr_valid; /* nop */
1603 case UT_READ_ENDPOINT:
1604 switch (req->bRequest) {
1606 goto tr_handle_get_ep_status;
1612 case UT_WRITE_INTERFACE:
1613 switch (req->bRequest) {
1614 case UR_SET_INTERFACE:
1615 goto tr_handle_set_interface;
1616 case UR_CLEAR_FEATURE:
1617 goto tr_valid; /* nop */
1618 case UR_SET_FEATURE:
1624 case UT_READ_INTERFACE:
1625 switch (req->bRequest) {
1626 case UR_GET_INTERFACE:
1627 goto tr_handle_get_interface;
1629 goto tr_handle_get_iface_status;
1635 case UT_WRITE_CLASS_INTERFACE:
1636 case UT_WRITE_VENDOR_INTERFACE:
1640 case UT_READ_CLASS_INTERFACE:
1641 case UT_READ_VENDOR_INTERFACE:
1645 case UT_WRITE_CLASS_DEVICE:
1646 switch (req->bRequest) {
1647 case UR_CLEAR_FEATURE:
1649 case UR_SET_DESCRIPTOR:
1650 case UR_SET_FEATURE:
1657 case UT_WRITE_CLASS_OTHER:
1658 switch (req->bRequest) {
1659 case UR_CLEAR_FEATURE:
1660 goto tr_handle_clear_port_feature;
1661 case UR_SET_FEATURE:
1662 goto tr_handle_set_port_feature;
1663 case UR_CLEAR_TT_BUFFER:
1673 case UT_READ_CLASS_OTHER:
1674 switch (req->bRequest) {
1675 case UR_GET_TT_STATE:
1676 goto tr_handle_get_tt_state;
1678 goto tr_handle_get_port_status;
1684 case UT_READ_CLASS_DEVICE:
1685 switch (req->bRequest) {
1686 case UR_GET_DESCRIPTOR:
1687 goto tr_handle_get_class_descriptor;
1689 goto tr_handle_get_class_status;
1700 tr_handle_get_descriptor:
1701 switch (value >> 8) {
1706 len = sizeof(atmegadci_devd);
1707 ptr = (const void *)&atmegadci_devd;
1713 len = sizeof(atmegadci_confd);
1714 ptr = (const void *)&atmegadci_confd;
1717 switch (value & 0xff) {
1718 case 0: /* Language table */
1719 len = sizeof(usb_string_lang_en);
1720 ptr = (const void *)&usb_string_lang_en;
1723 case 1: /* Vendor */
1724 len = sizeof(atmegadci_vendor);
1725 ptr = (const void *)&atmegadci_vendor;
1728 case 2: /* Product */
1729 len = sizeof(atmegadci_product);
1730 ptr = (const void *)&atmegadci_product;
1741 tr_handle_get_config:
1743 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
1746 tr_handle_get_status:
1748 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
1751 tr_handle_set_address:
1752 if (value & 0xFF00) {
1755 sc->sc_rt_addr = value;
1758 tr_handle_set_config:
1762 sc->sc_conf = value;
1765 tr_handle_get_interface:
1767 sc->sc_hub_temp.wValue[0] = 0;
1770 tr_handle_get_tt_state:
1771 tr_handle_get_class_status:
1772 tr_handle_get_iface_status:
1773 tr_handle_get_ep_status:
1775 USETW(sc->sc_hub_temp.wValue, 0);
1779 tr_handle_set_interface:
1780 tr_handle_set_wakeup:
1781 tr_handle_clear_wakeup:
1782 tr_handle_clear_halt:
1785 tr_handle_clear_port_feature:
1789 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
1792 case UHF_PORT_SUSPEND:
1793 atmegadci_wakeup_peer(sc);
1796 case UHF_PORT_ENABLE:
1797 sc->sc_flags.port_enabled = 0;
1801 case UHF_PORT_INDICATOR:
1802 case UHF_C_PORT_ENABLE:
1803 case UHF_C_PORT_OVER_CURRENT:
1804 case UHF_C_PORT_RESET:
1807 case UHF_PORT_POWER:
1808 sc->sc_flags.port_powered = 0;
1809 atmegadci_pull_down(sc);
1810 atmegadci_clocks_off(sc);
1812 case UHF_C_PORT_CONNECTION:
1813 /* clear connect change flag */
1814 sc->sc_flags.change_connect = 0;
1816 if (!sc->sc_flags.status_bus_reset) {
1817 /* we are not connected */
1821 /* configure the control endpoint */
1823 /* select endpoint number */
1824 ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0);
1826 /* set endpoint reset */
1827 ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0));
1829 /* clear endpoint reset */
1830 ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
1832 /* enable and stall endpoint */
1833 ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
1834 ATMEGA_UECONX_EPEN |
1835 ATMEGA_UECONX_STALLRQ);
1837 /* one bank, 64-bytes wMaxPacket */
1838 ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X,
1839 ATMEGA_UECFG0X_EPTYPE0);
1840 ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
1841 ATMEGA_UECFG1X_ALLOC |
1842 ATMEGA_UECFG1X_EPBK0 |
1843 ATMEGA_UECFG1X_EPSIZE(3));
1845 /* check valid config */
1846 temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
1847 if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
1848 device_printf(sc->sc_bus.bdev,
1849 "Chip rejected EP0 configuration\n");
1852 case UHF_C_PORT_SUSPEND:
1853 sc->sc_flags.change_suspend = 0;
1856 err = USB_ERR_IOERROR;
1861 tr_handle_set_port_feature:
1865 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
1868 case UHF_PORT_ENABLE:
1869 sc->sc_flags.port_enabled = 1;
1871 case UHF_PORT_SUSPEND:
1872 case UHF_PORT_RESET:
1874 case UHF_PORT_INDICATOR:
1877 case UHF_PORT_POWER:
1878 sc->sc_flags.port_powered = 1;
1881 err = USB_ERR_IOERROR;
1886 tr_handle_get_port_status:
1888 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
1893 if (sc->sc_flags.status_vbus) {
1894 atmegadci_clocks_on(sc);
1895 atmegadci_pull_up(sc);
1897 atmegadci_pull_down(sc);
1898 atmegadci_clocks_off(sc);
1901 /* Select FULL-speed and Device Side Mode */
1903 value = UPS_PORT_MODE_DEVICE;
1905 if (sc->sc_flags.port_powered) {
1906 value |= UPS_PORT_POWER;
1908 if (sc->sc_flags.port_enabled) {
1909 value |= UPS_PORT_ENABLED;
1911 if (sc->sc_flags.status_vbus &&
1912 sc->sc_flags.status_bus_reset) {
1913 value |= UPS_CURRENT_CONNECT_STATUS;
1915 if (sc->sc_flags.status_suspend) {
1916 value |= UPS_SUSPEND;
1918 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
1922 if (sc->sc_flags.change_connect) {
1923 value |= UPS_C_CONNECT_STATUS;
1925 if (sc->sc_flags.change_suspend) {
1926 value |= UPS_C_SUSPEND;
1928 USETW(sc->sc_hub_temp.ps.wPortChange, value);
1929 len = sizeof(sc->sc_hub_temp.ps);
1932 tr_handle_get_class_descriptor:
1936 ptr = (const void *)&atmegadci_hubd;
1937 len = sizeof(atmegadci_hubd);
1941 err = USB_ERR_STALLED;
1950 atmegadci_xfer_setup(struct usb_setup_params *parm)
1952 const struct usb_hw_ep_profile *pf;
1953 struct usb_xfer *xfer;
1959 xfer = parm->curr_xfer;
1962 * NOTE: This driver does not use any of the parameters that
1963 * are computed from the following values. Just set some
1964 * reasonable dummies:
1966 parm->hc_max_packet_size = 0x500;
1967 parm->hc_max_packet_count = 1;
1968 parm->hc_max_frame_size = 0x500;
1970 usbd_transfer_setup_sub(parm);
1973 * compute maximum number of TDs
1975 if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
1976 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 atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no);
2000 /* should not happen */
2001 parm->err = USB_ERR_INVAL;
2006 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2008 for (n = 0; n != ntd; n++) {
2009 struct atmegadci_td *td;
2012 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2015 td->max_packet_size = xfer->max_packet_size;
2017 if (pf->support_multi_buffer) {
2018 td->support_multi_buffer = 1;
2020 td->obj_next = last_obj;
2024 parm->size[0] += sizeof(*td);
2027 xfer->td_start[0] = last_obj;
2031 atmegadci_xfer_unsetup(struct usb_xfer *xfer)
2037 atmegadci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2038 struct usb_endpoint *ep)
2040 struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
2042 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2044 edesc->bEndpointAddress, udev->flags.usb_mode,
2045 sc->sc_rt_addr, udev->device_index);
2047 if (udev->device_index != sc->sc_rt_addr) {
2048 if (udev->speed != USB_SPEED_FULL) {
2052 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2053 ep->methods = &atmegadci_device_isoc_fs_methods;
2055 ep->methods = &atmegadci_device_non_isoc_methods;
2060 atmegadci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
2062 struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
2065 case USB_HW_POWER_SUSPEND:
2066 atmegadci_suspend(sc);
2068 case USB_HW_POWER_SHUTDOWN:
2069 atmegadci_uninit(sc);
2071 case USB_HW_POWER_RESUME:
2072 atmegadci_resume(sc);
2079 static const struct usb_bus_methods atmegadci_bus_methods =
2081 .endpoint_init = &atmegadci_ep_init,
2082 .xfer_setup = &atmegadci_xfer_setup,
2083 .xfer_unsetup = &atmegadci_xfer_unsetup,
2084 .get_hw_ep_profile = &atmegadci_get_hw_ep_profile,
2085 .xfer_stall = &atmegadci_xfer_stall,
2086 .set_stall = &atmegadci_set_stall,
2087 .clear_stall = &atmegadci_clear_stall,
2088 .roothub_exec = &atmegadci_roothub_exec,
2089 .xfer_poll = &atmegadci_do_poll,
2090 .set_hw_power_sleep = &atmegadci_set_hw_power_sleep,