2 * Copyright (c) 2012 Hans Petter Selasky. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * This file contains the driver for the DesignWare series USB 2.0 OTG
28 * Controller. This driver currently only supports the device mode of
33 * LIMITATION: Drivers must be bound to all OUT endpoints in the
34 * active configuration for this driver to work properly. Blocking any
35 * OUT endpoint will block all OUT endpoints including the control
36 * endpoint. Usually this is not a problem.
40 * NOTE: Writing to non-existing registers appears to cause an
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/stdint.h>
48 #include <sys/stddef.h>
49 #include <sys/param.h>
50 #include <sys/queue.h>
51 #include <sys/types.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
55 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/sysctl.h>
61 #include <sys/unistd.h>
62 #include <sys/callout.h>
63 #include <sys/malloc.h>
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbdi.h>
69 #define USB_DEBUG_VAR dwc_otg_debug
71 #include <dev/usb/usb_core.h>
72 #include <dev/usb/usb_debug.h>
73 #include <dev/usb/usb_busdma.h>
74 #include <dev/usb/usb_process.h>
75 #include <dev/usb/usb_transfer.h>
76 #include <dev/usb/usb_device.h>
77 #include <dev/usb/usb_hub.h>
78 #include <dev/usb/usb_util.h>
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
83 #include <dev/usb/controller/dwc_otg.h>
85 #define DWC_OTG_BUS2SC(bus) \
86 ((struct dwc_otg_softc *)(((uint8_t *)(bus)) - \
87 ((uint8_t *)&(((struct dwc_otg_softc *)0)->sc_bus))))
89 #define DWC_OTG_PC2SC(pc) \
90 DWC_OTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
92 #define DWC_OTG_MSK_GINT_ENABLED \
93 (DWC_OTG_MSK_GINT_ENUM_DONE | \
94 DWC_OTG_MSK_GINT_USB_SUSPEND | \
95 DWC_OTG_MSK_GINT_INEP | \
96 DWC_OTG_MSK_GINT_RXFLVL | \
97 DWC_OTG_MSK_GINT_SESSREQINT)
99 #define DWC_OTG_USE_HSIC 0
102 static int dwc_otg_debug = 0;
104 static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW, 0, "USB DWC OTG");
105 SYSCTL_INT(_hw_usb_dwc_otg, OID_AUTO, debug, CTLFLAG_RW,
106 &dwc_otg_debug, 0, "DWC OTG debug level");
109 #define DWC_OTG_INTR_ENDPT 1
113 struct usb_bus_methods dwc_otg_bus_methods;
114 struct usb_pipe_methods dwc_otg_device_non_isoc_methods;
115 struct usb_pipe_methods dwc_otg_device_isoc_fs_methods;
117 static dwc_otg_cmd_t dwc_otg_setup_rx;
118 static dwc_otg_cmd_t dwc_otg_data_rx;
119 static dwc_otg_cmd_t dwc_otg_data_tx;
120 static dwc_otg_cmd_t dwc_otg_data_tx_sync;
121 static void dwc_otg_device_done(struct usb_xfer *, usb_error_t);
122 static void dwc_otg_do_poll(struct usb_bus *);
123 static void dwc_otg_standard_done(struct usb_xfer *);
124 static void dwc_otg_root_intr(struct dwc_otg_softc *sc);
127 * Here is a configuration that the chip supports.
129 static const struct usb_hw_ep_profile dwc_otg_ep_profile[1] = {
132 .max_in_frame_size = 64,/* fixed */
133 .max_out_frame_size = 64, /* fixed */
135 .support_control = 1,
140 dwc_otg_get_hw_ep_profile(struct usb_device *udev,
141 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
143 struct dwc_otg_softc *sc;
145 sc = DWC_OTG_BUS2SC(udev->bus);
147 if (ep_addr < sc->sc_dev_ep_max)
148 *ppf = &sc->sc_hw_ep_profile[ep_addr].usb;
154 dwc_otg_init_fifo(struct dwc_otg_softc *sc)
156 struct dwc_otg_profile *pf;
162 fifo_size = sc->sc_fifo_size;
164 fifo_regs = 4 * (sc->sc_dev_ep_max + sc->sc_dev_in_ep_max);
166 if (fifo_size >= fifo_regs)
167 fifo_size -= fifo_regs;
171 /* split equally for IN and OUT */
174 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRXFSIZ, fifo_size / 4);
176 /* align to 4-bytes */
179 tx_start = fifo_size;
181 if (fifo_size < 0x40) {
182 DPRINTFN(-1, "Not enough data space for EP0 FIFO.\n");
183 USB_BUS_UNLOCK(&sc->sc_bus);
187 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GNPTXFSIZ, (0x10 << 16) | (tx_start / 4));
191 /* setup control endpoint profile */
192 sc->sc_hw_ep_profile[0].usb = dwc_otg_ep_profile[0];
194 for (x = 1; x != sc->sc_dev_ep_max; x++) {
196 pf = sc->sc_hw_ep_profile + x;
198 pf->usb.max_out_frame_size = 1024 * 3;
199 pf->usb.is_simplex = 0; /* assume duplex */
200 pf->usb.support_bulk = 1;
201 pf->usb.support_interrupt = 1;
202 pf->usb.support_isochronous = 1;
203 pf->usb.support_out = 1;
205 if (x < sc->sc_dev_in_ep_max) {
208 limit = (x == 1) ? DWC_OTG_MAX_TXN :
209 (DWC_OTG_MAX_TXN / 2);
211 if (fifo_size >= limit) {
212 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
213 ((limit / 4) << 16) |
217 pf->usb.max_in_frame_size = 0x200;
218 pf->usb.support_in = 1;
219 pf->max_buffer = limit;
221 } else if (fifo_size >= 0x80) {
222 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
223 ((0x80 / 4) << 16) | (tx_start / 4));
226 pf->usb.max_in_frame_size = 0x40;
227 pf->usb.support_in = 1;
230 pf->usb.is_simplex = 1;
231 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTXF(x),
232 (0x0 << 16) | (tx_start / 4));
235 pf->usb.is_simplex = 1;
238 DPRINTF("FIFO%d = IN:%d / OUT:%d\n", x,
239 pf->usb.max_in_frame_size,
240 pf->usb.max_out_frame_size);
244 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
245 DWC_OTG_MSK_GRSTCTL_RXFFLUSH);
247 /* reset all TX FIFOs */
248 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
249 DWC_OTG_MSK_GRSTCTL_TXFIFO(0x10) |
250 DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
256 dwc_otg_clocks_on(struct dwc_otg_softc *sc)
258 if (sc->sc_flags.clocks_off &&
259 sc->sc_flags.port_powered) {
263 /* TODO - platform specific */
265 sc->sc_flags.clocks_off = 0;
270 dwc_otg_clocks_off(struct dwc_otg_softc *sc)
272 if (!sc->sc_flags.clocks_off) {
276 /* TODO - platform specific */
278 sc->sc_flags.clocks_off = 1;
283 dwc_otg_pull_up(struct dwc_otg_softc *sc)
287 /* pullup D+, if possible */
289 if (!sc->sc_flags.d_pulled_up &&
290 sc->sc_flags.port_powered) {
291 sc->sc_flags.d_pulled_up = 1;
293 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
294 temp &= ~DWC_OTG_MSK_DCTL_SOFT_DISC;
295 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
300 dwc_otg_pull_down(struct dwc_otg_softc *sc)
304 /* pulldown D+, if possible */
306 if (sc->sc_flags.d_pulled_up) {
307 sc->sc_flags.d_pulled_up = 0;
309 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
310 temp |= DWC_OTG_MSK_DCTL_SOFT_DISC;
311 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
316 dwc_otg_resume_irq(struct dwc_otg_softc *sc)
318 if (sc->sc_flags.status_suspend) {
319 /* update status bits */
320 sc->sc_flags.status_suspend = 0;
321 sc->sc_flags.change_suspend = 1;
324 * Disable resume interrupt and enable suspend
327 sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_WKUPINT;
328 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_USB_SUSPEND;
329 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
331 /* complete root HUB interrupt endpoint */
332 dwc_otg_root_intr(sc);
337 dwc_otg_wakeup_peer(struct dwc_otg_softc *sc)
341 if (!sc->sc_flags.status_suspend)
344 DPRINTFN(5, "Remote wakeup\n");
346 /* enable remote wakeup signalling */
347 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCTL);
348 temp |= DWC_OTG_MSK_DCTL_REMOTE_WAKEUP;
349 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
351 /* Wait 8ms for remote wakeup to complete. */
352 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
354 temp &= ~DWC_OTG_MSK_DCTL_REMOTE_WAKEUP;
355 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL, temp);
357 /* need to fake resume IRQ */
358 dwc_otg_resume_irq(sc);
362 dwc_otg_set_address(struct dwc_otg_softc *sc, uint8_t addr)
366 DPRINTFN(5, "addr=%d\n", addr);
368 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DCFG);
369 temp &= ~DWC_OTG_MSK_DCFG_SET_DEV_ADDR(0x7F);
370 temp |= DWC_OTG_MSK_DCFG_SET_DEV_ADDR(addr);
371 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCFG, temp);
375 dwc_otg_common_rx_ack(struct dwc_otg_softc *sc)
377 DPRINTFN(5, "RX status clear\n");
379 /* enable RX FIFO level interrupt */
380 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_RXFLVL;
381 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
383 /* clear cached status */
384 sc->sc_last_rx_status = 0;
388 dwc_otg_setup_rx(struct dwc_otg_td *td)
390 struct dwc_otg_softc *sc;
391 struct usb_device_request req __aligned(4);
395 /* get pointer to softc */
396 sc = DWC_OTG_PC2SC(td->pc);
398 /* check endpoint status */
400 if (sc->sc_last_rx_status == 0)
403 if (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(sc->sc_last_rx_status) != 0)
406 if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PID) !=
407 DWC_OTG_MSK_GRXSTS_PID_DATA0) {
409 dwc_otg_common_rx_ack(sc);
413 if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
414 DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
416 dwc_otg_common_rx_ack(sc);
420 DPRINTFN(5, "GRXSTSR=0x%08x\n", sc->sc_last_rx_status);
422 /* clear did stall */
425 /* get the packet byte count */
426 count = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status);
428 /* verify data length */
429 if (count != td->remainder) {
430 DPRINTFN(0, "Invalid SETUP packet "
431 "length, %d bytes\n", count);
433 dwc_otg_common_rx_ack(sc);
436 if (count != sizeof(req)) {
437 DPRINTFN(0, "Unsupported SETUP packet "
438 "length, %d bytes\n", count);
440 dwc_otg_common_rx_ack(sc);
444 /* copy in control request */
445 memcpy(&req, sc->sc_rx_bounce_buffer, sizeof(req));
447 /* copy data into real buffer */
448 usbd_copy_in(td->pc, 0, &req, sizeof(req));
450 td->offset = sizeof(req);
453 /* sneak peek the set address */
454 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
455 (req.bRequest == UR_SET_ADDRESS)) {
456 /* must write address before ZLP */
457 dwc_otg_set_address(sc, req.wValue[0] & 0x7F);
460 /* don't send any data by default */
461 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTSIZ(0),
462 DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(0) |
463 DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(0));
465 temp = sc->sc_in_ctl[0];
467 /* enable IN endpoint */
468 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
469 temp | DWC_OTG_MSK_DIEPCTL_ENABLE);
470 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
471 temp | DWC_OTG_MSK_DIEPCTL_SET_NAK);
473 /* reset IN endpoint buffer */
474 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
475 DWC_OTG_MSK_GRSTCTL_TXFIFO(0) |
476 DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
478 /* acknowledge RX status */
479 dwc_otg_common_rx_ack(sc);
480 return (0); /* complete */
483 /* abort any ongoing transfer, before enabling again */
485 temp = sc->sc_out_ctl[0];
487 temp |= DWC_OTG_MSK_DOEPCTL_ENABLE |
488 DWC_OTG_MSK_DOEPCTL_SET_NAK;
490 /* enable OUT endpoint */
491 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(0), temp);
493 if (!td->did_stall) {
496 DPRINTFN(5, "stalling IN and OUT direction\n");
498 /* set stall after enabling endpoint */
499 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(0),
500 temp | DWC_OTG_MSK_DOEPCTL_STALL);
502 temp = sc->sc_in_ctl[0];
504 /* set stall assuming endpoint is enabled */
505 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(0),
506 temp | DWC_OTG_MSK_DIEPCTL_STALL);
509 /* setup number of buffers to receive */
510 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(0),
511 DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(3) |
512 DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(1) |
513 DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(sizeof(req)));
515 return (1); /* not complete */
519 dwc_otg_data_rx(struct dwc_otg_td *td)
521 struct dwc_otg_softc *sc;
528 /* get pointer to softc */
529 sc = DWC_OTG_PC2SC(td->pc);
531 /* check endpoint status */
532 if (sc->sc_last_rx_status == 0)
535 if (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(sc->sc_last_rx_status) != td->ep_no)
538 /* check for SETUP packet */
539 if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) ==
540 DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
541 if (td->remainder == 0) {
543 * We are actually complete and have
544 * received the next SETUP
546 DPRINTFN(5, "faking complete\n");
547 return (0); /* complete */
550 * USB Host Aborted the transfer.
553 return (0); /* complete */
556 if ((sc->sc_last_rx_status & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
557 DWC_OTG_MSK_GRXSTS_DEV_OUT_DATA) {
559 dwc_otg_common_rx_ack(sc);
563 /* get the packet byte count */
564 count = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status);
566 /* verify the packet byte count */
567 if (count != td->max_packet_size) {
568 if (count < td->max_packet_size) {
569 /* we have a short packet */
573 /* invalid USB packet */
577 dwc_otg_common_rx_ack(sc);
578 return (0); /* we are complete */
581 /* verify the packet byte count */
582 if (count > td->remainder) {
583 /* invalid USB packet */
587 dwc_otg_common_rx_ack(sc);
588 return (0); /* we are complete */
591 usbd_copy_in(td->pc, td->offset, sc->sc_rx_bounce_buffer, count);
592 td->remainder -= count;
596 dwc_otg_common_rx_ack(sc);
598 /* check if we are complete */
599 if ((td->remainder == 0) || got_short) {
601 /* we are complete */
604 /* else need to receive a zero length packet */
609 temp = sc->sc_out_ctl[td->ep_no];
611 temp |= DWC_OTG_MSK_DOEPCTL_ENABLE |
612 DWC_OTG_MSK_DOEPCTL_CLR_NAK;
614 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(td->ep_no), temp);
616 /* enable SETUP and transfer complete interrupt */
617 if (td->ep_no == 0) {
618 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(0),
619 DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(1) |
620 DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(td->max_packet_size));
622 /* allow reception of multiple packets */
623 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPTSIZ(td->ep_no),
624 DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(1) |
625 DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(4) |
626 DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(4 *
627 ((td->max_packet_size + 3) & ~3)));
629 return (1); /* not complete */
633 dwc_otg_data_tx(struct dwc_otg_td *td)
635 struct dwc_otg_softc *sc;
643 to = 3; /* don't loop forever! */
645 /* get pointer to softc */
646 sc = DWC_OTG_PC2SC(td->pc);
648 max_buffer = sc->sc_hw_ep_profile[td->ep_no].max_buffer;
651 /* check for for endpoint 0 data */
653 temp = sc->sc_last_rx_status;
655 if ((td->ep_no == 0) && (temp != 0) &&
656 (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(temp) == 0)) {
658 if ((temp & DWC_OTG_MSK_GRXSTS_PACKET_STS) !=
659 DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
661 /* dump data - wrong direction */
662 dwc_otg_common_rx_ack(sc);
665 * The current transfer was cancelled
669 return (0); /* complete */
673 /* fill in more TX data, if possible */
674 if (td->tx_bytes != 0) {
678 /* check if packets have been transferred */
679 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
681 /* get current packet number */
682 cpkt = DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp);
684 if (cpkt >= td->npkt) {
687 if (max_buffer != 0) {
688 fifo_left = (td->npkt - cpkt) *
691 if (fifo_left > max_buffer)
692 fifo_left = max_buffer;
694 fifo_left = td->max_packet_size;
698 count = td->tx_bytes;
699 if (count > fifo_left)
704 /* clear topmost word before copy */
705 sc->sc_tx_bounce_buffer[(count - 1) / 4] = 0;
708 usbd_copy_out(td->pc, td->offset,
709 sc->sc_tx_bounce_buffer, count);
711 /* transfer data into FIFO */
712 bus_space_write_region_4(sc->sc_io_tag, sc->sc_io_hdl,
713 DWC_OTG_REG_DFIFO(td->ep_no),
714 sc->sc_tx_bounce_buffer, (count + 3) / 4);
716 td->tx_bytes -= count;
717 td->remainder -= count;
721 if (td->tx_bytes != 0)
724 /* check remainder */
725 if (td->remainder == 0) {
727 return (0); /* complete */
729 /* else we need to transmit a short packet */
733 /* check if no packets have been transferred */
734 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
736 if (DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp) != 0) {
738 DPRINTFN(5, "busy ep=%d npkt=%d DIEPTSIZ=0x%08x "
739 "DIEPCTL=0x%08x\n", td->ep_no,
740 DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp),
741 temp, DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPCTL(td->ep_no)));
746 DPRINTFN(5, "rem=%u ep=%d\n", td->remainder, td->ep_no);
748 /* try to optimise by sending more data */
749 if ((max_buffer != 0) && ((td->max_packet_size & 3) == 0)) {
751 /* send multiple packets at the same time */
752 mpkt = max_buffer / td->max_packet_size;
757 count = td->remainder;
758 if (count > 0x7FFFFF)
759 count = 0x7FFFFF - (0x7FFFFF % td->max_packet_size);
761 td->npkt = count / td->max_packet_size;
764 * NOTE: We could use 0x3FE instead of "mpkt" in the
765 * check below to get more throughput, but then we
766 * have a dependency towards non-generic chip features
767 * to disable the TX-FIFO-EMPTY interrupts on a per
768 * endpoint basis. Increase the maximum buffer size of
769 * the IN endpoint to increase the performance.
771 if (td->npkt > mpkt) {
773 count = td->max_packet_size * mpkt;
774 } else if ((count == 0) || (count % td->max_packet_size)) {
775 /* we are transmitting a short packet */
780 /* send one packet at a time */
782 count = td->max_packet_size;
783 if (td->remainder < count) {
784 /* we have a short packet */
786 count = td->remainder;
790 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no),
791 DWC_OTG_MSK_DXEPTSIZ_SET_MULTI(1) |
792 DWC_OTG_MSK_DXEPTSIZ_SET_NPKT(td->npkt) |
793 DWC_OTG_MSK_DXEPTSIZ_SET_NBYTES(count));
795 /* make room for buffering */
798 temp = sc->sc_in_ctl[td->ep_no];
800 /* must enable before writing data to FIFO */
801 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(td->ep_no), temp |
802 DWC_OTG_MSK_DIEPCTL_ENABLE |
803 DWC_OTG_MSK_DIEPCTL_CLR_NAK);
805 td->tx_bytes = count;
807 /* check remainder */
808 if (td->tx_bytes == 0 &&
809 td->remainder == 0) {
811 return (0); /* complete */
813 /* else we need to transmit a short packet */
820 return (1); /* not complete */
824 dwc_otg_data_tx_sync(struct dwc_otg_td *td)
826 struct dwc_otg_softc *sc;
829 /* get pointer to softc */
830 sc = DWC_OTG_PC2SC(td->pc);
833 * If all packets are transferred we are complete:
835 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPTSIZ(td->ep_no));
837 /* check that all packets have been transferred */
838 if (DWC_OTG_MSK_DXEPTSIZ_GET_NPKT(temp) != 0) {
839 DPRINTFN(5, "busy ep=%d\n", td->ep_no);
846 /* we only want to know if there is a SETUP packet or free IN packet */
848 temp = sc->sc_last_rx_status;
850 if ((td->ep_no == 0) && (temp != 0) &&
851 (DWC_OTG_MSK_GRXSTS_GET_CHANNEL(temp) == 0)) {
853 if ((temp & DWC_OTG_MSK_GRXSTS_PACKET_STS) ==
854 DWC_OTG_MSK_GRXSTS_DEV_STP_DATA) {
855 DPRINTFN(5, "faking complete\n");
857 * Race condition: We are complete!
861 /* dump data - wrong direction */
862 dwc_otg_common_rx_ack(sc);
865 return (1); /* not complete */
869 dwc_otg_xfer_do_fifo(struct usb_xfer *xfer)
871 struct dwc_otg_td *td;
875 td = xfer->td_transfer_cache;
877 if ((td->func) (td)) {
878 /* operation in progress */
881 if (((void *)td) == xfer->td_transfer_last) {
886 } else if (td->remainder > 0) {
888 * We had a short transfer. If there is no alternate
889 * next, stop processing !
896 * Fetch the next transfer descriptor and transfer
897 * some flags to the next transfer descriptor
900 xfer->td_transfer_cache = td;
902 return (1); /* not complete */
905 /* compute all actual lengths */
907 dwc_otg_standard_done(xfer);
908 return (0); /* complete */
912 dwc_otg_interrupt_poll(struct dwc_otg_softc *sc)
914 struct usb_xfer *xfer;
916 uint8_t got_rx_status;
919 if (sc->sc_last_rx_status == 0) {
921 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GINTSTS);
922 if (temp & DWC_OTG_MSK_GINT_RXFLVL) {
923 /* pop current status */
924 sc->sc_last_rx_status =
925 DWC_OTG_READ_4(sc, DWC_OTG_REG_GRXSTSP);
928 if (sc->sc_last_rx_status != 0) {
933 temp = DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(
934 sc->sc_last_rx_status);
935 ep_no = DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
936 sc->sc_last_rx_status);
938 /* receive data, if any */
940 DPRINTF("Reading %d bytes from ep %d\n", temp, ep_no);
941 bus_space_read_region_4(sc->sc_io_tag, sc->sc_io_hdl,
942 DWC_OTG_REG_DFIFO(ep_no),
943 sc->sc_rx_bounce_buffer, (temp + 3) / 4);
946 temp = sc->sc_last_rx_status &
947 DWC_OTG_MSK_GRXSTS_PACKET_STS;
949 /* non-data messages we simply skip */
950 if (temp != DWC_OTG_MSK_GRXSTS_DEV_STP_DATA &&
951 temp != DWC_OTG_MSK_GRXSTS_DEV_OUT_DATA) {
952 dwc_otg_common_rx_ack(sc);
956 /* check if we should dump the data */
957 if (!(sc->sc_active_out_ep & (1U << ep_no))) {
958 dwc_otg_common_rx_ack(sc);
964 DPRINTFN(5, "RX status = 0x%08x: ch=%d pid=%d bytes=%d sts=%d\n",
965 sc->sc_last_rx_status, ep_no,
966 (sc->sc_last_rx_status >> 15) & 3,
967 DWC_OTG_MSK_GRXSTS_GET_BYTE_CNT(sc->sc_last_rx_status),
968 (sc->sc_last_rx_status >> 17) & 15);
975 ep_no = DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
976 sc->sc_last_rx_status);
978 /* check if we should dump the data */
979 if (!(sc->sc_active_out_ep & (1U << ep_no))) {
980 dwc_otg_common_rx_ack(sc);
987 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
988 if (!dwc_otg_xfer_do_fifo(xfer)) {
989 /* queue has been modified */
995 if (sc->sc_last_rx_status == 0)
998 /* disable RX FIFO level interrupt */
999 sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_RXFLVL;
1000 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1005 dwc_otg_vbus_interrupt(struct dwc_otg_softc *sc, uint8_t is_on)
1007 DPRINTFN(5, "vbus = %u\n", is_on);
1010 if (!sc->sc_flags.status_vbus) {
1011 sc->sc_flags.status_vbus = 1;
1013 /* complete root HUB interrupt endpoint */
1015 dwc_otg_root_intr(sc);
1018 if (sc->sc_flags.status_vbus) {
1019 sc->sc_flags.status_vbus = 0;
1020 sc->sc_flags.status_bus_reset = 0;
1021 sc->sc_flags.status_suspend = 0;
1022 sc->sc_flags.change_suspend = 0;
1023 sc->sc_flags.change_connect = 1;
1025 /* complete root HUB interrupt endpoint */
1027 dwc_otg_root_intr(sc);
1033 dwc_otg_interrupt(struct dwc_otg_softc *sc)
1037 USB_BUS_LOCK(&sc->sc_bus);
1039 /* read and clear interrupt status */
1040 status = DWC_OTG_READ_4(sc, DWC_OTG_REG_GINTSTS);
1041 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTSTS, status);
1043 DPRINTFN(14, "GINTSTS=0x%08x\n", status);
1045 /* check for any bus state change interrupts */
1046 if (status & DWC_OTG_MSK_GINT_ENUM_DONE) {
1050 DPRINTFN(5, "end of reset\n");
1052 /* set correct state */
1053 sc->sc_flags.status_bus_reset = 1;
1054 sc->sc_flags.status_suspend = 0;
1055 sc->sc_flags.change_suspend = 0;
1056 sc->sc_flags.change_connect = 1;
1059 dwc_otg_init_fifo(sc);
1061 /* reset function address */
1062 dwc_otg_set_address(sc, 0);
1064 /* reset active endpoints */
1065 sc->sc_active_out_ep = 1;
1067 /* figure out enumeration speed */
1068 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DSTS);
1069 if (DWC_OTG_MSK_DSTS_GET_ENUM_SPEED(temp) ==
1070 DWC_OTG_MSK_DSTS_ENUM_SPEED_HI)
1071 sc->sc_flags.status_high_speed = 1;
1073 sc->sc_flags.status_high_speed = 0;
1075 /* disable resume interrupt and enable suspend interrupt */
1077 sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_WKUPINT;
1078 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_USB_SUSPEND;
1079 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1081 /* complete root HUB interrupt endpoint */
1082 dwc_otg_root_intr(sc);
1085 * If resume and suspend is set at the same time we interpret
1086 * that like RESUME. Resume is set when there is at least 3
1087 * milliseconds of inactivity on the USB BUS.
1089 if (status & DWC_OTG_MSK_GINT_WKUPINT) {
1091 DPRINTFN(5, "resume interrupt\n");
1093 dwc_otg_resume_irq(sc);
1095 } else if (status & DWC_OTG_MSK_GINT_USB_SUSPEND) {
1097 DPRINTFN(5, "suspend interrupt\n");
1099 if (!sc->sc_flags.status_suspend) {
1100 /* update status bits */
1101 sc->sc_flags.status_suspend = 1;
1102 sc->sc_flags.change_suspend = 1;
1105 * Disable suspend interrupt and enable resume
1108 sc->sc_irq_mask &= ~DWC_OTG_MSK_GINT_USB_SUSPEND;
1109 sc->sc_irq_mask |= DWC_OTG_MSK_GINT_WKUPINT;
1110 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1112 /* complete root HUB interrupt endpoint */
1113 dwc_otg_root_intr(sc);
1117 if (status & (DWC_OTG_MSK_GINT_USB_SUSPEND |
1118 DWC_OTG_MSK_GINT_SESSREQINT)) {
1121 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GOTGCTL);
1123 DPRINTFN(5, "GOTGCTL=0x%08x\n", temp);
1125 dwc_otg_vbus_interrupt(sc,
1126 (temp & DWC_OTG_MSK_GOTGCTL_BSESS_VALID) ? 1 : 0);
1129 /* clear all IN endpoint interrupts */
1130 if (status & DWC_OTG_MSK_GINT_INEP) {
1134 for (x = 0; x != sc->sc_dev_in_ep_max; x++) {
1135 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DIEPINT(x));
1138 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPINT(x), temp);
1143 /* check if we should poll the FIFOs */
1144 if (status & (DWC_OTG_MSK_GINT_RXFLVL | DWC_OTG_MSK_GINT_INEP))
1147 dwc_otg_interrupt_poll(sc);
1149 USB_BUS_UNLOCK(&sc->sc_bus);
1153 dwc_otg_setup_standard_chain_sub(struct dwc_otg_std_temp *temp)
1155 struct dwc_otg_td *td;
1157 /* get current Transfer Descriptor */
1161 /* prepare for next TD */
1162 temp->td_next = td->obj_next;
1164 /* fill out the Transfer Descriptor */
1165 td->func = temp->func;
1167 td->offset = temp->offset;
1168 td->remainder = temp->len;
1172 td->did_stall = temp->did_stall;
1173 td->short_pkt = temp->short_pkt;
1174 td->alt_next = temp->setup_alt_next;
1178 dwc_otg_setup_standard_chain(struct usb_xfer *xfer)
1180 struct dwc_otg_std_temp temp;
1181 struct dwc_otg_td *td;
1185 DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
1186 xfer->address, UE_GET_ADDR(xfer->endpointno),
1187 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
1189 temp.max_frame_size = xfer->max_frame_size;
1191 td = xfer->td_start[0];
1192 xfer->td_transfer_first = td;
1193 xfer->td_transfer_cache = td;
1199 temp.td_next = xfer->td_start[0];
1201 temp.setup_alt_next = xfer->flags_int.short_frames_ok;
1202 temp.did_stall = !xfer->flags_int.control_stall;
1204 /* check if we should prepend a setup message */
1206 if (xfer->flags_int.control_xfr) {
1207 if (xfer->flags_int.control_hdr) {
1209 temp.func = &dwc_otg_setup_rx;
1210 temp.len = xfer->frlengths[0];
1211 temp.pc = xfer->frbuffers + 0;
1212 temp.short_pkt = temp.len ? 1 : 0;
1214 /* check for last frame */
1215 if (xfer->nframes == 1) {
1216 /* no STATUS stage yet, SETUP is last */
1217 if (xfer->flags_int.control_act)
1218 temp.setup_alt_next = 0;
1221 dwc_otg_setup_standard_chain_sub(&temp);
1228 if (x != xfer->nframes) {
1229 if (xfer->endpointno & UE_DIR_IN) {
1230 temp.func = &dwc_otg_data_tx;
1233 temp.func = &dwc_otg_data_rx;
1237 /* setup "pc" pointer */
1238 temp.pc = xfer->frbuffers + x;
1242 while (x != xfer->nframes) {
1244 /* DATA0 / DATA1 message */
1246 temp.len = xfer->frlengths[x];
1250 if (x == xfer->nframes) {
1251 if (xfer->flags_int.control_xfr) {
1252 if (xfer->flags_int.control_act) {
1253 temp.setup_alt_next = 0;
1256 temp.setup_alt_next = 0;
1259 if (temp.len == 0) {
1261 /* make sure that we send an USB packet */
1267 /* regular data transfer */
1269 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
1272 dwc_otg_setup_standard_chain_sub(&temp);
1274 if (xfer->flags_int.isochronous_xfr) {
1275 temp.offset += temp.len;
1277 /* get next Page Cache pointer */
1278 temp.pc = xfer->frbuffers + x;
1282 if (xfer->flags_int.control_xfr) {
1284 /* always setup a valid "pc" pointer for status and sync */
1285 temp.pc = xfer->frbuffers + 0;
1288 temp.setup_alt_next = 0;
1290 /* check if we need to sync */
1292 /* we need a SYNC point after TX */
1293 temp.func = &dwc_otg_data_tx_sync;
1294 dwc_otg_setup_standard_chain_sub(&temp);
1297 /* check if we should append a status stage */
1298 if (!xfer->flags_int.control_act) {
1301 * Send a DATA1 message and invert the current
1302 * endpoint direction.
1304 if (xfer->endpointno & UE_DIR_IN) {
1305 temp.func = &dwc_otg_data_rx;
1308 temp.func = &dwc_otg_data_tx;
1312 dwc_otg_setup_standard_chain_sub(&temp);
1314 /* we need a SYNC point after TX */
1315 temp.func = &dwc_otg_data_tx_sync;
1316 dwc_otg_setup_standard_chain_sub(&temp);
1320 /* check if we need to sync */
1323 temp.pc = xfer->frbuffers + 0;
1326 temp.setup_alt_next = 0;
1328 /* we need a SYNC point after TX */
1329 temp.func = &dwc_otg_data_tx_sync;
1330 dwc_otg_setup_standard_chain_sub(&temp);
1334 /* must have at least one frame! */
1336 xfer->td_transfer_last = td;
1340 dwc_otg_timeout(void *arg)
1342 struct usb_xfer *xfer = arg;
1344 DPRINTF("xfer=%p\n", xfer);
1346 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
1348 /* transfer is transferred */
1349 dwc_otg_device_done(xfer, USB_ERR_TIMEOUT);
1353 dwc_otg_start_standard_chain(struct usb_xfer *xfer)
1357 /* poll one time - will turn on interrupts */
1358 if (dwc_otg_xfer_do_fifo(xfer)) {
1360 /* put transfer on interrupt queue */
1361 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
1363 /* start timeout, if any */
1364 if (xfer->timeout != 0) {
1365 usbd_transfer_timeout_ms(xfer,
1366 &dwc_otg_timeout, xfer->timeout);
1372 dwc_otg_root_intr(struct dwc_otg_softc *sc)
1376 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
1379 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
1381 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
1382 sizeof(sc->sc_hub_idata));
1386 dwc_otg_standard_done_sub(struct usb_xfer *xfer)
1388 struct dwc_otg_td *td;
1394 td = xfer->td_transfer_cache;
1397 len = td->remainder;
1399 if (xfer->aframes != xfer->nframes) {
1401 * Verify the length and subtract
1402 * the remainder from "frlengths[]":
1404 if (len > xfer->frlengths[xfer->aframes]) {
1407 xfer->frlengths[xfer->aframes] -= len;
1410 /* Check for transfer error */
1412 /* the transfer is finished */
1417 /* Check for short transfer */
1419 if (xfer->flags_int.short_frames_ok) {
1420 /* follow alt next */
1427 /* the transfer is finished */
1435 /* this USB frame is complete */
1441 /* update transfer cache */
1443 xfer->td_transfer_cache = td;
1446 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
1450 dwc_otg_standard_done(struct usb_xfer *xfer)
1452 usb_error_t err = 0;
1454 DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
1455 xfer, xfer->endpoint);
1459 xfer->td_transfer_cache = xfer->td_transfer_first;
1461 if (xfer->flags_int.control_xfr) {
1463 if (xfer->flags_int.control_hdr) {
1465 err = dwc_otg_standard_done_sub(xfer);
1469 if (xfer->td_transfer_cache == NULL) {
1473 while (xfer->aframes != xfer->nframes) {
1475 err = dwc_otg_standard_done_sub(xfer);
1478 if (xfer->td_transfer_cache == NULL) {
1483 if (xfer->flags_int.control_xfr &&
1484 !xfer->flags_int.control_act) {
1486 err = dwc_otg_standard_done_sub(xfer);
1489 dwc_otg_device_done(xfer, err);
1492 /*------------------------------------------------------------------------*
1493 * dwc_otg_device_done
1495 * NOTE: this function can be called more than one time on the
1496 * same USB transfer!
1497 *------------------------------------------------------------------------*/
1499 dwc_otg_device_done(struct usb_xfer *xfer, usb_error_t error)
1501 DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
1502 xfer, xfer->endpoint, error);
1504 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
1505 DPRINTFN(15, "disabled interrupts!\n");
1507 /* dequeue transfer and start next transfer */
1508 usbd_transfer_done(xfer, error);
1512 dwc_otg_set_stall(struct usb_device *udev, struct usb_xfer *xfer,
1513 struct usb_endpoint *ep, uint8_t *did_stall)
1515 struct dwc_otg_softc *sc;
1520 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1523 /* cancel any ongoing transfers */
1524 dwc_otg_device_done(xfer, USB_ERR_STALLED);
1526 sc = DWC_OTG_BUS2SC(udev->bus);
1528 /* get endpoint address */
1529 ep_no = ep->edesc->bEndpointAddress;
1531 DPRINTFN(5, "endpoint=0x%x\n", ep_no);
1533 if (ep_no & UE_DIR_IN) {
1534 reg = DWC_OTG_REG_DIEPCTL(ep_no & UE_ADDR);
1535 temp = sc->sc_in_ctl[ep_no & UE_ADDR];
1537 reg = DWC_OTG_REG_DOEPCTL(ep_no & UE_ADDR);
1538 temp = sc->sc_out_ctl[ep_no & UE_ADDR];
1541 /* disable and stall endpoint */
1542 DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_DISABLE);
1543 DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_STALL);
1545 /* clear active OUT ep */
1546 if (!(ep_no & UE_DIR_IN)) {
1548 sc->sc_active_out_ep &= ~(1U << (ep_no & UE_ADDR));
1550 if (sc->sc_last_rx_status != 0 &&
1551 (ep_no & UE_ADDR) == DWC_OTG_MSK_GRXSTS_GET_CHANNEL(
1552 sc->sc_last_rx_status)) {
1554 dwc_otg_common_rx_ack(sc);
1555 /* poll interrupt */
1556 dwc_otg_interrupt_poll(sc);
1562 dwc_otg_clear_stall_sub(struct dwc_otg_softc *sc, uint32_t mps,
1563 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
1568 if (ep_type == UE_CONTROL) {
1569 /* clearing stall is not needed */
1574 reg = DWC_OTG_REG_DIEPCTL(ep_no);
1576 reg = DWC_OTG_REG_DOEPCTL(ep_no);
1577 sc->sc_active_out_ep |= (1U << ep_no);
1580 /* round up and mask away the multiplier count */
1581 mps = (mps + 3) & 0x7FC;
1583 if (ep_type == UE_BULK) {
1584 temp = DWC_OTG_MSK_EP_SET_TYPE(
1585 DWC_OTG_MSK_EP_TYPE_BULK) |
1586 DWC_OTG_MSK_DIEPCTL_USB_AEP;
1587 } else if (ep_type == UE_INTERRUPT) {
1588 temp = DWC_OTG_MSK_EP_SET_TYPE(
1589 DWC_OTG_MSK_EP_TYPE_INTERRUPT) |
1590 DWC_OTG_MSK_DIEPCTL_USB_AEP;
1592 temp = DWC_OTG_MSK_EP_SET_TYPE(
1593 DWC_OTG_MSK_EP_TYPE_ISOC) |
1594 DWC_OTG_MSK_DIEPCTL_USB_AEP;
1597 temp |= DWC_OTG_MSK_DIEPCTL_MPS(mps);
1598 temp |= DWC_OTG_MSK_DIEPCTL_FNUM(ep_no);
1601 sc->sc_in_ctl[ep_no] = temp;
1603 sc->sc_out_ctl[ep_no] = temp;
1605 DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_DISABLE);
1606 DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DOEPCTL_SET_DATA0);
1607 DWC_OTG_WRITE_4(sc, reg, temp | DWC_OTG_MSK_DIEPCTL_SET_NAK);
1609 /* we only reset the transmit FIFO */
1611 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
1612 DWC_OTG_MSK_GRSTCTL_TXFIFO(ep_no) |
1613 DWC_OTG_MSK_GRSTCTL_TXFFLUSH);
1616 DWC_OTG_REG_DIEPTSIZ(ep_no), 0);
1619 /* poll interrupt */
1620 dwc_otg_interrupt_poll(sc);
1624 dwc_otg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
1626 struct dwc_otg_softc *sc;
1627 struct usb_endpoint_descriptor *ed;
1629 DPRINTFN(5, "endpoint=%p\n", ep);
1631 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
1634 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
1639 sc = DWC_OTG_BUS2SC(udev->bus);
1641 /* get endpoint descriptor */
1644 /* reset endpoint */
1645 dwc_otg_clear_stall_sub(sc,
1646 UGETW(ed->wMaxPacketSize),
1647 (ed->bEndpointAddress & UE_ADDR),
1648 (ed->bmAttributes & UE_XFERTYPE),
1649 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
1653 dwc_otg_device_state_change(struct usb_device *udev)
1655 struct dwc_otg_softc *sc;
1659 sc = DWC_OTG_BUS2SC(udev->bus);
1661 /* deactivate all other endpoint but the control endpoint */
1662 if (udev->state == USB_STATE_CONFIGURED ||
1663 udev->state == USB_STATE_ADDRESSED) {
1665 USB_BUS_LOCK(&sc->sc_bus);
1667 for (x = 1; x != sc->sc_dev_ep_max; x++) {
1669 if (x < sc->sc_dev_in_ep_max) {
1670 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(x),
1671 DWC_OTG_MSK_DIEPCTL_DISABLE);
1672 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPCTL(x), 0);
1675 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(x),
1676 DWC_OTG_MSK_DOEPCTL_DISABLE);
1677 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPCTL(x), 0);
1679 USB_BUS_UNLOCK(&sc->sc_bus);
1684 dwc_otg_init(struct dwc_otg_softc *sc)
1690 /* set up the bus structure */
1691 sc->sc_bus.usbrev = USB_REV_2_0;
1692 sc->sc_bus.methods = &dwc_otg_bus_methods;
1694 /* reset active endpoints */
1695 sc->sc_active_out_ep = 1;
1697 USB_BUS_LOCK(&sc->sc_bus);
1699 /* turn on clocks */
1700 dwc_otg_clocks_on(sc);
1702 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GSNPSID);
1703 DPRINTF("Version = 0x%08x\n", temp);
1706 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1707 DWC_OTG_MSK_DCTL_SOFT_DISC);
1709 /* wait for host to detect disconnect */
1710 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 32);
1712 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GRSTCTL,
1713 DWC_OTG_MSK_GRSTCTL_CSFTRST);
1715 /* wait a little bit for block to reset */
1716 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 128);
1718 /* select HSIC or non-HSIC mode */
1719 if (DWC_OTG_USE_HSIC) {
1720 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GUSBCFG,
1721 DWC_OTG_MSK_GUSBCFG_PHY_INTF |
1722 DWC_OTG_MSK_GUSBCFG_TRD_TIM(5));
1723 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GOTGCTL,
1726 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GLPMCFG);
1727 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1728 temp & ~DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1729 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1730 temp | DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1732 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GUSBCFG,
1733 DWC_OTG_MSK_GUSBCFG_ULPI_UMTI_SEL |
1734 DWC_OTG_MSK_GUSBCFG_TRD_TIM(5));
1735 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GOTGCTL, 0);
1737 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GLPMCFG);
1738 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GLPMCFG,
1739 temp & ~DWC_OTG_MSK_GLPMCFG_HSIC_CONN);
1742 /* clear global nak */
1743 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1744 DWC_OTG_MSK_DCTL_CGOUT_NAK |
1745 DWC_OTG_MSK_DCTL_CGNPIN_NAK);
1747 /* enable USB port */
1748 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_PCGCCTL, 0);
1751 dwc_otg_pull_up(sc);
1753 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG3);
1755 sc->sc_fifo_size = 4 * DWC_OTG_MSK_GHWCFG3_GET_DFIFO(temp);
1757 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG2);
1759 sc->sc_dev_ep_max = DWC_OTG_MSK_GHWCFG2_NUM_DEV_EP(temp);
1761 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GHWCFG4);
1763 sc->sc_dev_in_ep_max = DWC_OTG_MSK_GHWCFG4_NUM_IN_EPS(temp);
1765 DPRINTF("Total FIFO size = %d bytes, Device EPs = %d/%d\n",
1766 sc->sc_fifo_size, sc->sc_dev_ep_max, sc->sc_dev_in_ep_max);
1769 if (dwc_otg_init_fifo(sc))
1772 /* enable interrupts */
1773 sc->sc_irq_mask = DWC_OTG_MSK_GINT_ENABLED;
1774 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GINTMSK, sc->sc_irq_mask);
1777 * Disable all endpoint interrupts,
1778 * we use the SOF IRQ for transmit:
1781 /* enable all endpoint interrupts */
1782 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DIEPMSK,
1783 /* DWC_OTG_MSK_DIEP_FIFO_EMPTY | */
1784 DWC_OTG_MSK_DIEP_XFER_COMPLETE);
1785 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DOEPMSK, 0);
1786 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DAINTMSK, 0xFFFF);
1788 /* enable global IRQ */
1789 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GAHBCFG,
1790 DWC_OTG_MSK_GAHBCFG_GLOBAL_IRQ);
1792 /* turn off clocks */
1793 dwc_otg_clocks_off(sc);
1795 /* read initial VBUS state */
1797 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_GOTGCTL);
1799 DPRINTFN(5, "GOTCTL=0x%08x\n", temp);
1801 dwc_otg_vbus_interrupt(sc,
1802 (temp & DWC_OTG_MSK_GOTGCTL_BSESS_VALID) ? 1 : 0);
1804 USB_BUS_UNLOCK(&sc->sc_bus);
1806 /* catch any lost interrupts */
1808 dwc_otg_do_poll(&sc->sc_bus);
1810 return (0); /* success */
1814 dwc_otg_uninit(struct dwc_otg_softc *sc)
1816 USB_BUS_LOCK(&sc->sc_bus);
1818 /* set disconnect */
1819 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_DCTL,
1820 DWC_OTG_MSK_DCTL_SOFT_DISC);
1822 /* turn off global IRQ */
1823 DWC_OTG_WRITE_4(sc, DWC_OTG_REG_GAHBCFG, 0);
1825 sc->sc_flags.port_powered = 0;
1826 sc->sc_flags.status_vbus = 0;
1827 sc->sc_flags.status_bus_reset = 0;
1828 sc->sc_flags.status_suspend = 0;
1829 sc->sc_flags.change_suspend = 0;
1830 sc->sc_flags.change_connect = 1;
1832 dwc_otg_pull_down(sc);
1833 dwc_otg_clocks_off(sc);
1835 USB_BUS_UNLOCK(&sc->sc_bus);
1839 dwc_otg_suspend(struct dwc_otg_softc *sc)
1845 dwc_otg_resume(struct dwc_otg_softc *sc)
1851 dwc_otg_do_poll(struct usb_bus *bus)
1853 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
1855 USB_BUS_LOCK(&sc->sc_bus);
1856 dwc_otg_interrupt_poll(sc);
1857 USB_BUS_UNLOCK(&sc->sc_bus);
1860 /*------------------------------------------------------------------------*
1861 * at91dci bulk support
1862 * at91dci control support
1863 * at91dci interrupt support
1864 *------------------------------------------------------------------------*/
1866 dwc_otg_device_non_isoc_open(struct usb_xfer *xfer)
1872 dwc_otg_device_non_isoc_close(struct usb_xfer *xfer)
1874 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
1878 dwc_otg_device_non_isoc_enter(struct usb_xfer *xfer)
1884 dwc_otg_device_non_isoc_start(struct usb_xfer *xfer)
1887 dwc_otg_setup_standard_chain(xfer);
1888 dwc_otg_start_standard_chain(xfer);
1891 struct usb_pipe_methods dwc_otg_device_non_isoc_methods =
1893 .open = dwc_otg_device_non_isoc_open,
1894 .close = dwc_otg_device_non_isoc_close,
1895 .enter = dwc_otg_device_non_isoc_enter,
1896 .start = dwc_otg_device_non_isoc_start,
1899 /*------------------------------------------------------------------------*
1900 * at91dci full speed isochronous support
1901 *------------------------------------------------------------------------*/
1903 dwc_otg_device_isoc_fs_open(struct usb_xfer *xfer)
1909 dwc_otg_device_isoc_fs_close(struct usb_xfer *xfer)
1911 dwc_otg_device_done(xfer, USB_ERR_CANCELLED);
1915 dwc_otg_device_isoc_fs_enter(struct usb_xfer *xfer)
1917 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(xfer->xroot->bus);
1921 DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
1922 xfer, xfer->endpoint->isoc_next, xfer->nframes);
1924 temp = DWC_OTG_READ_4(sc, DWC_OTG_REG_DSTS);
1926 /* get the current frame index */
1928 nframes = DWC_OTG_MSK_DSTS_GET_FNUM(temp);
1930 if (sc->sc_flags.status_high_speed)
1933 nframes &= DWC_OTG_FRAME_MASK;
1936 * check if the frame index is within the window where the frames
1939 temp = (nframes - xfer->endpoint->isoc_next) & DWC_OTG_FRAME_MASK;
1941 if ((xfer->endpoint->is_synced == 0) ||
1942 (temp < xfer->nframes)) {
1944 * If there is data underflow or the pipe queue is
1945 * empty we schedule the transfer a few frames ahead
1946 * of the current frame position. Else two isochronous
1947 * transfers might overlap.
1949 xfer->endpoint->isoc_next = (nframes + 3) & DWC_OTG_FRAME_MASK;
1950 xfer->endpoint->is_synced = 1;
1951 DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
1954 * compute how many milliseconds the insertion is ahead of the
1955 * current frame position:
1957 temp = (xfer->endpoint->isoc_next - nframes) & DWC_OTG_FRAME_MASK;
1960 * pre-compute when the isochronous transfer will be finished:
1962 xfer->isoc_time_complete =
1963 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
1966 /* compute frame number for next insertion */
1967 xfer->endpoint->isoc_next += xfer->nframes;
1970 dwc_otg_setup_standard_chain(xfer);
1974 dwc_otg_device_isoc_fs_start(struct usb_xfer *xfer)
1976 /* start TD chain */
1977 dwc_otg_start_standard_chain(xfer);
1980 struct usb_pipe_methods dwc_otg_device_isoc_fs_methods =
1982 .open = dwc_otg_device_isoc_fs_open,
1983 .close = dwc_otg_device_isoc_fs_close,
1984 .enter = dwc_otg_device_isoc_fs_enter,
1985 .start = dwc_otg_device_isoc_fs_start,
1988 /*------------------------------------------------------------------------*
1989 * at91dci root control support
1990 *------------------------------------------------------------------------*
1991 * Simulate a hardware HUB by handling all the necessary requests.
1992 *------------------------------------------------------------------------*/
1994 static const struct usb_device_descriptor dwc_otg_devd = {
1995 .bLength = sizeof(struct usb_device_descriptor),
1996 .bDescriptorType = UDESC_DEVICE,
1997 .bcdUSB = {0x00, 0x02},
1998 .bDeviceClass = UDCLASS_HUB,
1999 .bDeviceSubClass = UDSUBCLASS_HUB,
2000 .bDeviceProtocol = UDPROTO_HSHUBSTT,
2001 .bMaxPacketSize = 64,
2002 .bcdDevice = {0x00, 0x01},
2005 .bNumConfigurations = 1,
2008 static const struct dwc_otg_config_desc dwc_otg_confd = {
2010 .bLength = sizeof(struct usb_config_descriptor),
2011 .bDescriptorType = UDESC_CONFIG,
2012 .wTotalLength[0] = sizeof(dwc_otg_confd),
2014 .bConfigurationValue = 1,
2015 .iConfiguration = 0,
2016 .bmAttributes = UC_SELF_POWERED,
2020 .bLength = sizeof(struct usb_interface_descriptor),
2021 .bDescriptorType = UDESC_INTERFACE,
2023 .bInterfaceClass = UICLASS_HUB,
2024 .bInterfaceSubClass = UISUBCLASS_HUB,
2025 .bInterfaceProtocol = 0,
2028 .bLength = sizeof(struct usb_endpoint_descriptor),
2029 .bDescriptorType = UDESC_ENDPOINT,
2030 .bEndpointAddress = (UE_DIR_IN | DWC_OTG_INTR_ENDPT),
2031 .bmAttributes = UE_INTERRUPT,
2032 .wMaxPacketSize[0] = 8,
2037 static const struct usb_hub_descriptor_min dwc_otg_hubd = {
2038 .bDescLength = sizeof(dwc_otg_hubd),
2039 .bDescriptorType = UDESC_HUB,
2041 .wHubCharacteristics[0] =
2042 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) & 0xFF,
2043 .wHubCharacteristics[1] =
2044 (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL) >> 8,
2045 .bPwrOn2PwrGood = 50,
2046 .bHubContrCurrent = 0,
2047 .DeviceRemovable = {0}, /* port is removable */
2050 #define STRING_LANG \
2051 0x09, 0x04, /* American English */
2053 #define STRING_VENDOR \
2054 'D', 0, 'W', 0, 'C', 0, 'O', 0, 'T', 0, 'G', 0
2056 #define STRING_PRODUCT \
2057 'D', 0, 'C', 0, 'I', 0, ' ', 0, 'R', 0, \
2058 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \
2061 USB_MAKE_STRING_DESC(STRING_LANG, dwc_otg_langtab);
2062 USB_MAKE_STRING_DESC(STRING_VENDOR, dwc_otg_vendor);
2063 USB_MAKE_STRING_DESC(STRING_PRODUCT, dwc_otg_product);
2066 dwc_otg_roothub_exec(struct usb_device *udev,
2067 struct usb_device_request *req, const void **pptr, uint16_t *plength)
2069 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
2076 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2079 ptr = (const void *)&sc->sc_hub_temp;
2083 value = UGETW(req->wValue);
2084 index = UGETW(req->wIndex);
2086 /* demultiplex the control request */
2088 switch (req->bmRequestType) {
2089 case UT_READ_DEVICE:
2090 switch (req->bRequest) {
2091 case UR_GET_DESCRIPTOR:
2092 goto tr_handle_get_descriptor;
2094 goto tr_handle_get_config;
2096 goto tr_handle_get_status;
2102 case UT_WRITE_DEVICE:
2103 switch (req->bRequest) {
2104 case UR_SET_ADDRESS:
2105 goto tr_handle_set_address;
2107 goto tr_handle_set_config;
2108 case UR_CLEAR_FEATURE:
2109 goto tr_valid; /* nop */
2110 case UR_SET_DESCRIPTOR:
2111 goto tr_valid; /* nop */
2112 case UR_SET_FEATURE:
2118 case UT_WRITE_ENDPOINT:
2119 switch (req->bRequest) {
2120 case UR_CLEAR_FEATURE:
2121 switch (UGETW(req->wValue)) {
2122 case UF_ENDPOINT_HALT:
2123 goto tr_handle_clear_halt;
2124 case UF_DEVICE_REMOTE_WAKEUP:
2125 goto tr_handle_clear_wakeup;
2130 case UR_SET_FEATURE:
2131 switch (UGETW(req->wValue)) {
2132 case UF_ENDPOINT_HALT:
2133 goto tr_handle_set_halt;
2134 case UF_DEVICE_REMOTE_WAKEUP:
2135 goto tr_handle_set_wakeup;
2140 case UR_SYNCH_FRAME:
2141 goto tr_valid; /* nop */
2147 case UT_READ_ENDPOINT:
2148 switch (req->bRequest) {
2150 goto tr_handle_get_ep_status;
2156 case UT_WRITE_INTERFACE:
2157 switch (req->bRequest) {
2158 case UR_SET_INTERFACE:
2159 goto tr_handle_set_interface;
2160 case UR_CLEAR_FEATURE:
2161 goto tr_valid; /* nop */
2162 case UR_SET_FEATURE:
2168 case UT_READ_INTERFACE:
2169 switch (req->bRequest) {
2170 case UR_GET_INTERFACE:
2171 goto tr_handle_get_interface;
2173 goto tr_handle_get_iface_status;
2179 case UT_WRITE_CLASS_INTERFACE:
2180 case UT_WRITE_VENDOR_INTERFACE:
2184 case UT_READ_CLASS_INTERFACE:
2185 case UT_READ_VENDOR_INTERFACE:
2189 case UT_WRITE_CLASS_DEVICE:
2190 switch (req->bRequest) {
2191 case UR_CLEAR_FEATURE:
2193 case UR_SET_DESCRIPTOR:
2194 case UR_SET_FEATURE:
2201 case UT_WRITE_CLASS_OTHER:
2202 switch (req->bRequest) {
2203 case UR_CLEAR_FEATURE:
2204 goto tr_handle_clear_port_feature;
2205 case UR_SET_FEATURE:
2206 goto tr_handle_set_port_feature;
2207 case UR_CLEAR_TT_BUFFER:
2217 case UT_READ_CLASS_OTHER:
2218 switch (req->bRequest) {
2219 case UR_GET_TT_STATE:
2220 goto tr_handle_get_tt_state;
2222 goto tr_handle_get_port_status;
2228 case UT_READ_CLASS_DEVICE:
2229 switch (req->bRequest) {
2230 case UR_GET_DESCRIPTOR:
2231 goto tr_handle_get_class_descriptor;
2233 goto tr_handle_get_class_status;
2244 tr_handle_get_descriptor:
2245 switch (value >> 8) {
2250 len = sizeof(dwc_otg_devd);
2251 ptr = (const void *)&dwc_otg_devd;
2257 len = sizeof(dwc_otg_confd);
2258 ptr = (const void *)&dwc_otg_confd;
2261 switch (value & 0xff) {
2262 case 0: /* Language table */
2263 len = sizeof(dwc_otg_langtab);
2264 ptr = (const void *)&dwc_otg_langtab;
2267 case 1: /* Vendor */
2268 len = sizeof(dwc_otg_vendor);
2269 ptr = (const void *)&dwc_otg_vendor;
2272 case 2: /* Product */
2273 len = sizeof(dwc_otg_product);
2274 ptr = (const void *)&dwc_otg_product;
2285 tr_handle_get_config:
2287 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
2290 tr_handle_get_status:
2292 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
2295 tr_handle_set_address:
2296 if (value & 0xFF00) {
2299 sc->sc_rt_addr = value;
2302 tr_handle_set_config:
2306 sc->sc_conf = value;
2309 tr_handle_get_interface:
2311 sc->sc_hub_temp.wValue[0] = 0;
2314 tr_handle_get_tt_state:
2315 tr_handle_get_class_status:
2316 tr_handle_get_iface_status:
2317 tr_handle_get_ep_status:
2319 USETW(sc->sc_hub_temp.wValue, 0);
2323 tr_handle_set_interface:
2324 tr_handle_set_wakeup:
2325 tr_handle_clear_wakeup:
2326 tr_handle_clear_halt:
2329 tr_handle_clear_port_feature:
2333 DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
2336 case UHF_PORT_SUSPEND:
2337 dwc_otg_wakeup_peer(sc);
2340 case UHF_PORT_ENABLE:
2341 sc->sc_flags.port_enabled = 0;
2345 case UHF_PORT_INDICATOR:
2346 case UHF_C_PORT_ENABLE:
2347 case UHF_C_PORT_OVER_CURRENT:
2348 case UHF_C_PORT_RESET:
2351 case UHF_PORT_POWER:
2352 sc->sc_flags.port_powered = 0;
2353 dwc_otg_pull_down(sc);
2354 dwc_otg_clocks_off(sc);
2356 case UHF_C_PORT_CONNECTION:
2357 /* clear connect change flag */
2358 sc->sc_flags.change_connect = 0;
2360 if (!sc->sc_flags.status_bus_reset) {
2361 /* we are not connected */
2365 case UHF_C_PORT_SUSPEND:
2366 sc->sc_flags.change_suspend = 0;
2369 err = USB_ERR_IOERROR;
2374 tr_handle_set_port_feature:
2378 DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
2381 case UHF_PORT_ENABLE:
2382 sc->sc_flags.port_enabled = 1;
2384 case UHF_PORT_SUSPEND:
2385 case UHF_PORT_RESET:
2387 case UHF_PORT_INDICATOR:
2390 case UHF_PORT_POWER:
2391 sc->sc_flags.port_powered = 1;
2394 err = USB_ERR_IOERROR;
2399 tr_handle_get_port_status:
2401 DPRINTFN(9, "UR_GET_PORT_STATUS\n");
2406 if (sc->sc_flags.status_vbus) {
2407 dwc_otg_clocks_on(sc);
2409 dwc_otg_clocks_off(sc);
2412 /* Select Device Side Mode */
2414 value = UPS_PORT_MODE_DEVICE;
2416 if (sc->sc_flags.status_high_speed) {
2417 value |= UPS_HIGH_SPEED;
2419 if (sc->sc_flags.port_powered) {
2420 value |= UPS_PORT_POWER;
2422 if (sc->sc_flags.port_enabled) {
2423 value |= UPS_PORT_ENABLED;
2425 if (sc->sc_flags.status_vbus &&
2426 sc->sc_flags.status_bus_reset) {
2427 value |= UPS_CURRENT_CONNECT_STATUS;
2429 if (sc->sc_flags.status_suspend) {
2430 value |= UPS_SUSPEND;
2432 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
2436 if (sc->sc_flags.change_connect) {
2437 value |= UPS_C_CONNECT_STATUS;
2439 if (sc->sc_flags.change_suspend) {
2440 value |= UPS_C_SUSPEND;
2442 USETW(sc->sc_hub_temp.ps.wPortChange, value);
2443 len = sizeof(sc->sc_hub_temp.ps);
2446 tr_handle_get_class_descriptor:
2450 ptr = (const void *)&dwc_otg_hubd;
2451 len = sizeof(dwc_otg_hubd);
2455 err = USB_ERR_STALLED;
2464 dwc_otg_xfer_setup(struct usb_setup_params *parm)
2466 const struct usb_hw_ep_profile *pf;
2467 struct usb_xfer *xfer;
2473 xfer = parm->curr_xfer;
2476 * NOTE: This driver does not use any of the parameters that
2477 * are computed from the following values. Just set some
2478 * reasonable dummies:
2480 parm->hc_max_packet_size = 0x500;
2481 parm->hc_max_packet_count = 1;
2482 parm->hc_max_frame_size = 0x500;
2484 usbd_transfer_setup_sub(parm);
2487 * compute maximum number of TDs
2489 if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
2491 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
2495 ntd = xfer->nframes + 1 /* SYNC */ ;
2499 * check if "usbd_transfer_setup_sub" set an error
2505 * allocate transfer descriptors
2512 ep_no = xfer->endpointno & UE_ADDR;
2513 dwc_otg_get_hw_ep_profile(parm->udev, &pf, ep_no);
2516 /* should not happen */
2517 parm->err = USB_ERR_INVAL;
2522 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
2524 for (n = 0; n != ntd; n++) {
2526 struct dwc_otg_td *td;
2530 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
2533 td->max_packet_size = xfer->max_packet_size;
2535 td->obj_next = last_obj;
2539 parm->size[0] += sizeof(*td);
2542 xfer->td_start[0] = last_obj;
2546 dwc_otg_xfer_unsetup(struct usb_xfer *xfer)
2552 dwc_otg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
2553 struct usb_endpoint *ep)
2555 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(udev->bus);
2557 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
2559 edesc->bEndpointAddress, udev->flags.usb_mode,
2560 sc->sc_rt_addr, udev->device_index);
2562 if (udev->device_index != sc->sc_rt_addr) {
2564 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
2568 if (udev->speed != USB_SPEED_FULL &&
2569 udev->speed != USB_SPEED_HIGH) {
2573 if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
2574 ep->methods = &dwc_otg_device_isoc_fs_methods;
2576 ep->methods = &dwc_otg_device_non_isoc_methods;
2581 dwc_otg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
2583 struct dwc_otg_softc *sc = DWC_OTG_BUS2SC(bus);
2586 case USB_HW_POWER_SUSPEND:
2587 dwc_otg_suspend(sc);
2589 case USB_HW_POWER_SHUTDOWN:
2592 case USB_HW_POWER_RESUME:
2600 struct usb_bus_methods dwc_otg_bus_methods =
2602 .endpoint_init = &dwc_otg_ep_init,
2603 .xfer_setup = &dwc_otg_xfer_setup,
2604 .xfer_unsetup = &dwc_otg_xfer_unsetup,
2605 .get_hw_ep_profile = &dwc_otg_get_hw_ep_profile,
2606 .set_stall = &dwc_otg_set_stall,
2607 .clear_stall = &dwc_otg_clear_stall,
2608 .roothub_exec = &dwc_otg_roothub_exec,
2609 .xfer_poll = &dwc_otg_do_poll,
2610 .device_state_change = &dwc_otg_device_state_change,
2611 .set_hw_power_sleep = &dwc_otg_set_hw_power_sleep,