3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Thanks to Mentor Graphics for providing a reference driver for this USB chip
35 * This file contains the driver for the Mentor Graphics Inventra USB
36 * 2.0 High Speed Dual-Role controller.
40 #ifdef USB_GLOBAL_INCLUDE_FILE
41 #include USB_GLOBAL_INCLUDE_FILE
43 #include <sys/stdint.h>
44 #include <sys/stddef.h>
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/types.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
51 #include <sys/module.h>
53 #include <sys/mutex.h>
54 #include <sys/condvar.h>
55 #include <sys/sysctl.h>
57 #include <sys/unistd.h>
58 #include <sys/callout.h>
59 #include <sys/malloc.h>
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbdi.h>
65 #define USB_DEBUG_VAR musbotgdebug
67 #include <dev/usb/usb_core.h>
68 #include <dev/usb/usb_debug.h>
69 #include <dev/usb/usb_busdma.h>
70 #include <dev/usb/usb_process.h>
71 #include <dev/usb/usb_transfer.h>
72 #include <dev/usb/usb_device.h>
73 #include <dev/usb/usb_hub.h>
74 #include <dev/usb/usb_util.h>
76 #include <dev/usb/usb_controller.h>
77 #include <dev/usb/usb_bus.h>
78 #endif /* USB_GLOBAL_INCLUDE_FILE */
80 #include <dev/usb/controller/musb_otg.h>
82 #define MUSBOTG_INTR_ENDPT 1
84 #define MUSBOTG_BUS2SC(bus) \
85 ((struct musbotg_softc *)(((uint8_t *)(bus)) - \
86 USB_P2U(&(((struct musbotg_softc *)0)->sc_bus))))
88 #define MUSBOTG_PC2SC(pc) \
89 MUSBOTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
92 static int musbotgdebug = 0;
94 static SYSCTL_NODE(_hw_usb, OID_AUTO, musbotg, CTLFLAG_RW, 0, "USB musbotg");
95 SYSCTL_INT(_hw_usb_musbotg, OID_AUTO, debug, CTLFLAG_RWTUN,
96 &musbotgdebug, 0, "Debug level");
103 static const struct usb_bus_methods musbotg_bus_methods;
104 static const struct usb_pipe_methods musbotg_device_bulk_methods;
105 static const struct usb_pipe_methods musbotg_device_ctrl_methods;
106 static const struct usb_pipe_methods musbotg_device_intr_methods;
107 static const struct usb_pipe_methods musbotg_device_isoc_methods;
109 /* Control transfers: Device mode */
110 static musbotg_cmd_t musbotg_dev_ctrl_setup_rx;
111 static musbotg_cmd_t musbotg_dev_ctrl_data_rx;
112 static musbotg_cmd_t musbotg_dev_ctrl_data_tx;
113 static musbotg_cmd_t musbotg_dev_ctrl_status;
115 /* Control transfers: Host mode */
116 static musbotg_cmd_t musbotg_host_ctrl_setup_tx;
117 static musbotg_cmd_t musbotg_host_ctrl_data_rx;
118 static musbotg_cmd_t musbotg_host_ctrl_data_tx;
119 static musbotg_cmd_t musbotg_host_ctrl_status_rx;
120 static musbotg_cmd_t musbotg_host_ctrl_status_tx;
122 /* Bulk, Interrupt, Isochronous: Device mode */
123 static musbotg_cmd_t musbotg_dev_data_rx;
124 static musbotg_cmd_t musbotg_dev_data_tx;
126 /* Bulk, Interrupt, Isochronous: Host mode */
127 static musbotg_cmd_t musbotg_host_data_rx;
128 static musbotg_cmd_t musbotg_host_data_tx;
130 static void musbotg_device_done(struct usb_xfer *, usb_error_t);
131 static void musbotg_do_poll(struct usb_bus *);
132 static void musbotg_standard_done(struct usb_xfer *);
133 static void musbotg_interrupt_poll(struct musbotg_softc *);
134 static void musbotg_root_intr(struct musbotg_softc *);
135 static int musbotg_channel_alloc(struct musbotg_softc *, struct musbotg_td *td, uint8_t);
136 static void musbotg_channel_free(struct musbotg_softc *, struct musbotg_td *td);
137 static void musbotg_ep_int_set(struct musbotg_softc *sc, int channel, int on);
140 * Here is a configuration that the chip supports.
142 static const struct usb_hw_ep_profile musbotg_ep_profile[1] = {
145 .max_in_frame_size = 64,/* fixed */
146 .max_out_frame_size = 64, /* fixed */
148 .support_control = 1,
152 static const struct musb_otg_ep_cfg musbotg_ep_default[] = {
155 .ep_fifosz_shift = 12,
156 .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_4096 | MUSB2_MASK_FIFODB,
160 .ep_fifosz_shift = 10,
161 .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512 | MUSB2_MASK_FIFODB,
165 .ep_fifosz_shift = 7,
166 .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_128,
174 musbotg_channel_alloc(struct musbotg_softc *sc, struct musbotg_td *td, uint8_t is_tx)
181 /* In device mode each EP got its own channel */
182 if (sc->sc_mode == MUSB2_DEVICE_MODE) {
183 musbotg_ep_int_set(sc, ep, 1);
188 * All control transactions go through EP0
191 if (sc->sc_channel_mask & (1 << 0))
193 sc->sc_channel_mask |= (1 << 0);
194 musbotg_ep_int_set(sc, ep, 1);
198 for (ch = sc->sc_ep_max; ch != 0; ch--) {
199 if (sc->sc_channel_mask & (1 << ch))
202 /* check FIFO size requirement */
204 if (td->max_frame_size >
205 sc->sc_hw_ep_profile[ch].max_in_frame_size)
208 if (td->max_frame_size >
209 sc->sc_hw_ep_profile[ch].max_out_frame_size)
212 sc->sc_channel_mask |= (1 << ch);
213 musbotg_ep_int_set(sc, ch, 1);
217 DPRINTFN(-1, "No available channels. Mask: %04x\n", sc->sc_channel_mask);
223 musbotg_channel_free(struct musbotg_softc *sc, struct musbotg_td *td)
226 DPRINTFN(1, "ep_no=%d\n", td->channel);
228 if (sc->sc_mode == MUSB2_DEVICE_MODE)
233 if (td->channel == -1)
236 musbotg_ep_int_set(sc, td->channel, 0);
237 sc->sc_channel_mask &= ~(1 << td->channel);
243 musbotg_get_hw_ep_profile(struct usb_device *udev,
244 const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
246 struct musbotg_softc *sc;
248 sc = MUSBOTG_BUS2SC(udev->bus);
251 /* control endpoint */
252 *ppf = musbotg_ep_profile;
253 } else if (ep_addr <= sc->sc_ep_max) {
254 /* other endpoints */
255 *ppf = sc->sc_hw_ep_profile + ep_addr;
262 musbotg_clocks_on(struct musbotg_softc *sc)
264 if (sc->sc_flags.clocks_off &&
265 sc->sc_flags.port_powered) {
269 if (sc->sc_clocks_on) {
270 (sc->sc_clocks_on) (sc->sc_clocks_arg);
272 sc->sc_flags.clocks_off = 0;
274 /* XXX enable Transceiver */
279 musbotg_clocks_off(struct musbotg_softc *sc)
281 if (!sc->sc_flags.clocks_off) {
285 /* XXX disable Transceiver */
287 if (sc->sc_clocks_off) {
288 (sc->sc_clocks_off) (sc->sc_clocks_arg);
290 sc->sc_flags.clocks_off = 1;
295 musbotg_pull_common(struct musbotg_softc *sc, uint8_t on)
299 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
301 temp |= MUSB2_MASK_SOFTC;
303 temp &= ~MUSB2_MASK_SOFTC;
305 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
309 musbotg_pull_up(struct musbotg_softc *sc)
311 /* pullup D+, if possible */
313 if (!sc->sc_flags.d_pulled_up &&
314 sc->sc_flags.port_powered) {
315 sc->sc_flags.d_pulled_up = 1;
316 musbotg_pull_common(sc, 1);
321 musbotg_pull_down(struct musbotg_softc *sc)
323 /* pulldown D+, if possible */
325 if (sc->sc_flags.d_pulled_up) {
326 sc->sc_flags.d_pulled_up = 0;
327 musbotg_pull_common(sc, 0);
332 musbotg_suspend_host(struct musbotg_softc *sc)
336 if (sc->sc_flags.status_suspend) {
340 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
341 temp |= MUSB2_MASK_SUSPMODE;
342 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
343 sc->sc_flags.status_suspend = 1;
347 musbotg_wakeup_host(struct musbotg_softc *sc)
351 if (!(sc->sc_flags.status_suspend)) {
355 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
356 temp &= ~MUSB2_MASK_SUSPMODE;
357 temp |= MUSB2_MASK_RESUME;
358 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
360 /* wait 20 milliseconds */
361 /* Wait for reset to complete. */
362 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 50);
364 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
365 temp &= ~MUSB2_MASK_RESUME;
366 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
368 sc->sc_flags.status_suspend = 0;
372 musbotg_wakeup_peer(struct musbotg_softc *sc)
376 if (!(sc->sc_flags.status_suspend)) {
380 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
381 temp |= MUSB2_MASK_RESUME;
382 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
384 /* wait 8 milliseconds */
385 /* Wait for reset to complete. */
386 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
388 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
389 temp &= ~MUSB2_MASK_RESUME;
390 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp);
394 musbotg_set_address(struct musbotg_softc *sc, uint8_t addr)
396 DPRINTFN(4, "addr=%d\n", addr);
398 MUSB2_WRITE_1(sc, MUSB2_REG_FADDR, addr);
402 musbotg_dev_ctrl_setup_rx(struct musbotg_td *td)
404 struct musbotg_softc *sc;
405 struct usb_device_request req;
409 /* get pointer to softc */
410 sc = MUSBOTG_PC2SC(td->pc);
412 if (td->channel == -1)
413 td->channel = musbotg_channel_alloc(sc, td, 0);
415 /* EP0 is busy, wait */
416 if (td->channel == -1)
419 DPRINTFN(1, "ep_no=%d\n", td->channel);
421 /* select endpoint 0 */
422 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
424 /* read out FIFO status */
425 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
427 DPRINTFN(4, "csr=0x%02x\n", csr);
430 * NOTE: If DATAEND is set we should not call the
431 * callback, hence the status stage is not complete.
433 if (csr & MUSB2_MASK_CSR0L_DATAEND) {
434 /* do not stall at this point */
436 /* wait for interrupt */
437 DPRINTFN(1, "CSR0 DATAEND\n");
441 if (csr & MUSB2_MASK_CSR0L_SENTSTALL) {
442 /* clear SENTSTALL */
443 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
444 /* get latest status */
445 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
446 /* update EP0 state */
449 if (csr & MUSB2_MASK_CSR0L_SETUPEND) {
451 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
452 MUSB2_MASK_CSR0L_SETUPEND_CLR);
453 /* get latest status */
454 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
455 /* update EP0 state */
458 if (sc->sc_ep0_busy) {
459 DPRINTFN(1, "EP0 BUSY\n");
462 if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) {
465 /* get the packet byte count */
466 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
468 /* verify data length */
469 if (count != td->remainder) {
470 DPRINTFN(1, "Invalid SETUP packet "
471 "length, %d bytes\n", count);
472 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
473 MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
474 /* don't clear stall */
478 if (count != sizeof(req)) {
479 DPRINTFN(1, "Unsupported SETUP packet "
480 "length, %d bytes\n", count);
481 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
482 MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
483 /* don't clear stall */
487 /* clear did stall flag */
491 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
492 MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req));
494 /* copy data into real buffer */
495 usbd_copy_in(td->pc, 0, &req, sizeof(req));
497 td->offset = sizeof(req);
500 /* set pending command */
501 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR;
503 /* we need set stall or dataend after this */
506 /* sneak peek the set address */
507 if ((req.bmRequestType == UT_WRITE_DEVICE) &&
508 (req.bRequest == UR_SET_ADDRESS)) {
509 sc->sc_dv_addr = req.wValue[0] & 0x7F;
511 sc->sc_dv_addr = 0xFF;
514 musbotg_channel_free(sc, td);
515 return (0); /* complete */
518 /* abort any ongoing transfer */
519 if (!td->did_stall) {
520 DPRINTFN(4, "stalling\n");
521 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
522 MUSB2_MASK_CSR0L_SENDSTALL);
525 return (1); /* not complete */
529 musbotg_host_ctrl_setup_tx(struct musbotg_td *td)
531 struct musbotg_softc *sc;
532 struct usb_device_request req;
535 /* get pointer to softc */
536 sc = MUSBOTG_PC2SC(td->pc);
538 if (td->channel == -1)
539 td->channel = musbotg_channel_alloc(sc, td, 1);
541 /* EP0 is busy, wait */
542 if (td->channel == -1)
545 DPRINTFN(1, "ep_no=%d\n", td->channel);
547 /* select endpoint 0 */
548 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
550 /* read out FIFO status */
551 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
552 DPRINTFN(4, "csr=0x%02x\n", csr);
554 /* Not ready yet yet */
555 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
559 if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
560 MUSB2_MASK_CSR0L_ERROR))
562 /* Clear status bit */
563 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
564 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
568 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
569 DPRINTFN(1, "NAK timeout\n");
571 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
572 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
573 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
574 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
575 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
576 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
577 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
578 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
579 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
580 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
584 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
585 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
591 musbotg_channel_free(sc, td);
595 /* Fifo is not empty and there is no NAK timeout */
596 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
599 /* check if we are complete */
600 if (td->remainder == 0) {
601 /* we are complete */
602 musbotg_channel_free(sc, td);
606 /* copy data into real buffer */
607 usbd_copy_out(td->pc, 0, &req, sizeof(req));
610 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
611 MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req));
613 /* update offset and remainder */
614 td->offset += sizeof(req);
615 td->remainder -= sizeof(req);
618 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
619 MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
620 MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
621 MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
622 MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
625 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
626 MUSB2_MASK_CSR0L_TXPKTRDY |
627 MUSB2_MASK_CSR0L_SETUPPKT);
629 /* Just to be consistent, not used above */
630 td->transaction_started = 1;
632 return (1); /* in progress */
635 /* Control endpoint only data handling functions (RX/TX/SYNC) */
638 musbotg_dev_ctrl_data_rx(struct musbotg_td *td)
640 struct usb_page_search buf_res;
641 struct musbotg_softc *sc;
646 /* get pointer to softc */
647 sc = MUSBOTG_PC2SC(td->pc);
649 /* select endpoint 0 */
650 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
652 /* check if a command is pending */
653 if (sc->sc_ep0_cmd) {
654 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
657 /* read out FIFO status */
658 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
660 DPRINTFN(4, "csr=0x%02x\n", csr);
664 if (csr & (MUSB2_MASK_CSR0L_SETUPEND |
665 MUSB2_MASK_CSR0L_SENTSTALL)) {
666 if (td->remainder == 0) {
668 * We are actually complete and have
669 * received the next SETUP
671 DPRINTFN(4, "faking complete\n");
672 return (0); /* complete */
675 * USB Host Aborted the transfer.
678 return (0); /* complete */
680 if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) {
681 return (1); /* not complete */
683 /* get the packet byte count */
684 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
686 /* verify the packet byte count */
687 if (count != td->max_frame_size) {
688 if (count < td->max_frame_size) {
689 /* we have a short packet */
693 /* invalid USB packet */
695 return (0); /* we are complete */
698 /* verify the packet byte count */
699 if (count > td->remainder) {
700 /* invalid USB packet */
702 return (0); /* we are complete */
707 usbd_get_page(td->pc, td->offset, &buf_res);
709 /* get correct length */
710 if (buf_res.length > count) {
711 buf_res.length = count;
713 /* check for unaligned memory address */
714 if (USB_P2U(buf_res.buffer) & 3) {
719 /* receive data 4 bytes at a time */
720 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
721 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
726 /* receive data 1 byte at a time */
727 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
729 (void *)(&sc->sc_bounce_buf[count / 4]), temp);
731 usbd_copy_in(td->pc, td->offset,
732 sc->sc_bounce_buf, count);
734 /* update offset and remainder */
736 td->remainder -= count;
739 /* check if we can optimise */
740 if (buf_res.length >= 4) {
742 /* receive data 4 bytes at a time */
743 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
744 MUSB2_REG_EPFIFO(0), buf_res.buffer,
747 temp = buf_res.length & ~3;
749 /* update counters */
752 td->remainder -= temp;
756 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
757 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
759 /* update counters */
760 count -= buf_res.length;
761 td->offset += buf_res.length;
762 td->remainder -= buf_res.length;
765 /* check if we are complete */
766 if ((td->remainder == 0) || got_short) {
768 /* we are complete */
769 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR;
772 /* else need to receive a zero length packet */
774 /* write command - need more data */
775 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
776 MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
777 return (1); /* not complete */
781 musbotg_dev_ctrl_data_tx(struct musbotg_td *td)
783 struct usb_page_search buf_res;
784 struct musbotg_softc *sc;
788 /* get pointer to softc */
789 sc = MUSBOTG_PC2SC(td->pc);
791 /* select endpoint 0 */
792 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
794 /* check if a command is pending */
795 if (sc->sc_ep0_cmd) {
796 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
799 /* read out FIFO status */
800 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
802 DPRINTFN(4, "csr=0x%02x\n", csr);
804 if (csr & (MUSB2_MASK_CSR0L_SETUPEND |
805 MUSB2_MASK_CSR0L_SENTSTALL)) {
807 * The current transfer was aborted
811 return (0); /* complete */
813 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY) {
814 return (1); /* not complete */
816 count = td->max_frame_size;
817 if (td->remainder < count) {
818 /* we have a short packet */
820 count = td->remainder;
825 usbd_get_page(td->pc, td->offset, &buf_res);
827 /* get correct length */
828 if (buf_res.length > count) {
829 buf_res.length = count;
831 /* check for unaligned memory address */
832 if (USB_P2U(buf_res.buffer) & 3) {
834 usbd_copy_out(td->pc, td->offset,
835 sc->sc_bounce_buf, count);
840 /* transmit data 4 bytes at a time */
841 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
842 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
847 /* receive data 1 byte at a time */
848 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
850 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
852 /* update offset and remainder */
854 td->remainder -= count;
857 /* check if we can optimise */
858 if (buf_res.length >= 4) {
860 /* transmit data 4 bytes at a time */
861 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
862 MUSB2_REG_EPFIFO(0), buf_res.buffer,
865 temp = buf_res.length & ~3;
867 /* update counters */
870 td->remainder -= temp;
874 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
875 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
877 /* update counters */
878 count -= buf_res.length;
879 td->offset += buf_res.length;
880 td->remainder -= buf_res.length;
883 /* check remainder */
884 if (td->remainder == 0) {
886 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_TXPKTRDY;
887 return (0); /* complete */
889 /* else we need to transmit a short packet */
892 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
893 MUSB2_MASK_CSR0L_TXPKTRDY);
895 return (1); /* not complete */
899 musbotg_host_ctrl_data_rx(struct musbotg_td *td)
901 struct usb_page_search buf_res;
902 struct musbotg_softc *sc;
907 /* get pointer to softc */
908 sc = MUSBOTG_PC2SC(td->pc);
910 if (td->channel == -1)
911 td->channel = musbotg_channel_alloc(sc, td, 0);
913 /* EP0 is busy, wait */
914 if (td->channel == -1)
917 DPRINTFN(1, "ep_no=%d\n", td->channel);
919 /* select endpoint 0 */
920 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
922 /* read out FIFO status */
923 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
925 DPRINTFN(4, "csr=0x%02x\n", csr);
928 if (!td->transaction_started) {
929 td->transaction_started = 1;
931 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
933 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(0),
935 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(0), td->haddr);
936 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(0), td->hport);
937 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
939 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
940 MUSB2_MASK_CSR0L_REQPKT);
945 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
946 csr &= ~MUSB2_MASK_CSR0L_REQPKT;
947 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
949 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
950 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
956 if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
957 MUSB2_MASK_CSR0L_ERROR))
959 /* Clear status bit */
960 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
961 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
966 musbotg_channel_free(sc, td);
967 return (0); /* we are complete */
970 if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY))
971 return (1); /* not yet */
973 /* get the packet byte count */
974 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
976 /* verify the packet byte count */
977 if (count != td->max_frame_size) {
978 if (count < td->max_frame_size) {
979 /* we have a short packet */
983 /* invalid USB packet */
985 musbotg_channel_free(sc, td);
986 return (0); /* we are complete */
989 /* verify the packet byte count */
990 if (count > td->remainder) {
991 /* invalid USB packet */
993 musbotg_channel_free(sc, td);
994 return (0); /* we are complete */
999 usbd_get_page(td->pc, td->offset, &buf_res);
1001 /* get correct length */
1002 if (buf_res.length > count) {
1003 buf_res.length = count;
1005 /* check for unaligned memory address */
1006 if (USB_P2U(buf_res.buffer) & 3) {
1011 /* receive data 4 bytes at a time */
1012 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1013 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf,
1018 /* receive data 1 byte at a time */
1019 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1020 MUSB2_REG_EPFIFO(0),
1021 (void *)(&sc->sc_bounce_buf[count / 4]), temp);
1023 usbd_copy_in(td->pc, td->offset,
1024 sc->sc_bounce_buf, count);
1026 /* update offset and remainder */
1027 td->offset += count;
1028 td->remainder -= count;
1031 /* check if we can optimise */
1032 if (buf_res.length >= 4) {
1034 /* receive data 4 bytes at a time */
1035 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1036 MUSB2_REG_EPFIFO(0), buf_res.buffer,
1037 buf_res.length / 4);
1039 temp = buf_res.length & ~3;
1041 /* update counters */
1044 td->remainder -= temp;
1048 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1049 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length);
1051 /* update counters */
1052 count -= buf_res.length;
1053 td->offset += buf_res.length;
1054 td->remainder -= buf_res.length;
1057 csr &= ~MUSB2_MASK_CSR0L_RXPKTRDY;
1058 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1060 /* check if we are complete */
1061 if ((td->remainder == 0) || got_short) {
1062 if (td->short_pkt) {
1063 /* we are complete */
1065 musbotg_channel_free(sc, td);
1068 /* else need to receive a zero length packet */
1071 td->transaction_started = 1;
1072 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1073 MUSB2_MASK_CSR0L_REQPKT);
1075 return (1); /* not complete */
1079 musbotg_host_ctrl_data_tx(struct musbotg_td *td)
1081 struct usb_page_search buf_res;
1082 struct musbotg_softc *sc;
1086 /* get pointer to softc */
1087 sc = MUSBOTG_PC2SC(td->pc);
1089 if (td->channel == -1)
1090 td->channel = musbotg_channel_alloc(sc, td, 1);
1093 if (td->channel == -1)
1096 DPRINTFN(1, "ep_no=%d\n", td->channel);
1098 /* select endpoint */
1099 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1101 /* read out FIFO status */
1102 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1103 DPRINTFN(4, "csr=0x%02x\n", csr);
1105 if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1106 MUSB2_MASK_CSR0L_ERROR)) {
1107 /* clear status bits */
1108 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1112 if (csr & MUSB2_MASK_CSR0L_NAKTIMO ) {
1114 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1115 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
1116 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
1117 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
1118 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1119 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY) {
1120 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
1121 csrh |= MUSB2_MASK_CSR0H_FFLUSH;
1122 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
1123 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1127 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1128 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1135 musbotg_channel_free(sc, td);
1136 return (0); /* complete */
1140 * Wait while FIFO is empty.
1141 * Do not flush it because it will cause transactions
1142 * with size more then packet size. It might upset
1145 if (csr & MUSB2_MASK_CSR0L_TXFIFONEMPTY)
1148 /* Packet still being processed */
1149 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
1152 if (td->transaction_started) {
1153 /* check remainder */
1154 if (td->remainder == 0) {
1155 if (td->short_pkt) {
1156 musbotg_channel_free(sc, td);
1157 return (0); /* complete */
1159 /* else we need to transmit a short packet */
1162 /* We're not complete - more transactions required */
1163 td->transaction_started = 0;
1166 /* check for short packet */
1167 count = td->max_frame_size;
1168 if (td->remainder < count) {
1169 /* we have a short packet */
1171 count = td->remainder;
1177 usbd_get_page(td->pc, td->offset, &buf_res);
1179 /* get correct length */
1180 if (buf_res.length > count) {
1181 buf_res.length = count;
1183 /* check for unaligned memory address */
1184 if (USB_P2U(buf_res.buffer) & 3) {
1186 usbd_copy_out(td->pc, td->offset,
1187 sc->sc_bounce_buf, count);
1192 /* transmit data 4 bytes at a time */
1193 bus_space_write_multi_4(sc->sc_io_tag,
1194 sc->sc_io_hdl, MUSB2_REG_EPFIFO(0),
1195 sc->sc_bounce_buf, temp / 4);
1199 /* receive data 1 byte at a time */
1200 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1201 MUSB2_REG_EPFIFO(0),
1202 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1204 /* update offset and remainder */
1205 td->offset += count;
1206 td->remainder -= count;
1209 /* check if we can optimise */
1210 if (buf_res.length >= 4) {
1212 /* transmit data 4 bytes at a time */
1213 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1214 MUSB2_REG_EPFIFO(0), buf_res.buffer,
1215 buf_res.length / 4);
1217 temp = buf_res.length & ~3;
1219 /* update counters */
1222 td->remainder -= temp;
1226 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1227 MUSB2_REG_EPFIFO(0), buf_res.buffer,
1230 /* update counters */
1231 count -= buf_res.length;
1232 td->offset += buf_res.length;
1233 td->remainder -= buf_res.length;
1236 /* Function address */
1237 MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
1238 MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
1239 MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
1240 MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
1242 /* TX NAK timeout */
1243 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
1246 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1247 MUSB2_MASK_CSR0L_TXPKTRDY);
1249 td->transaction_started = 1;
1251 return (1); /* not complete */
1255 musbotg_dev_ctrl_status(struct musbotg_td *td)
1257 struct musbotg_softc *sc;
1260 /* get pointer to softc */
1261 sc = MUSBOTG_PC2SC(td->pc);
1263 /* select endpoint 0 */
1264 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1266 if (sc->sc_ep0_busy) {
1267 sc->sc_ep0_busy = 0;
1268 sc->sc_ep0_cmd |= MUSB2_MASK_CSR0L_DATAEND;
1269 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd);
1272 /* read out FIFO status */
1273 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1275 DPRINTFN(4, "csr=0x%02x\n", csr);
1277 if (csr & MUSB2_MASK_CSR0L_DATAEND) {
1278 /* wait for interrupt */
1279 return (1); /* not complete */
1281 if (sc->sc_dv_addr != 0xFF) {
1282 /* write function address */
1283 musbotg_set_address(sc, sc->sc_dv_addr);
1286 musbotg_channel_free(sc, td);
1287 return (0); /* complete */
1291 musbotg_host_ctrl_status_rx(struct musbotg_td *td)
1293 struct musbotg_softc *sc;
1296 /* get pointer to softc */
1297 sc = MUSBOTG_PC2SC(td->pc);
1299 if (td->channel == -1)
1300 td->channel = musbotg_channel_alloc(sc, td, 0);
1302 /* EP0 is busy, wait */
1303 if (td->channel == -1)
1306 DPRINTFN(1, "ep_no=%d\n", td->channel);
1308 /* select endpoint 0 */
1309 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1311 if (!td->transaction_started) {
1312 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(0),
1315 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(0), td->haddr);
1316 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(0), td->hport);
1317 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
1319 /* RX NAK timeout */
1320 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
1322 td->transaction_started = 1;
1325 csrh = MUSB2_READ_1(sc, MUSB2_REG_RXCSRH);
1326 csrh |= MUSB2_MASK_CSR0H_PING_DIS;
1327 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, csrh);
1330 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1331 MUSB2_MASK_CSR0L_STATUSPKT |
1332 MUSB2_MASK_CSR0L_REQPKT);
1334 return (1); /* Just started */
1338 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1340 DPRINTFN(4, "IN STATUS csr=0x%02x\n", csr);
1342 if (csr & MUSB2_MASK_CSR0L_RXPKTRDY) {
1343 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1344 MUSB2_MASK_CSR0L_RXPKTRDY_CLR);
1345 musbotg_channel_free(sc, td);
1346 return (0); /* complete */
1349 if (csr & MUSB2_MASK_CSR0L_NAKTIMO) {
1350 csr &= ~ (MUSB2_MASK_CSR0L_STATUSPKT |
1351 MUSB2_MASK_CSR0L_REQPKT);
1352 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1354 csr &= ~MUSB2_MASK_CSR0L_NAKTIMO;
1355 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1360 if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1361 MUSB2_MASK_CSR0L_ERROR))
1363 /* Clear status bit */
1364 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1365 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
1370 musbotg_channel_free(sc, td);
1374 return (1); /* Not ready yet */
1378 musbotg_host_ctrl_status_tx(struct musbotg_td *td)
1380 struct musbotg_softc *sc;
1383 /* get pointer to softc */
1384 sc = MUSBOTG_PC2SC(td->pc);
1386 if (td->channel == -1)
1387 td->channel = musbotg_channel_alloc(sc, td, 1);
1389 /* EP0 is busy, wait */
1390 if (td->channel == -1)
1393 DPRINTFN(1, "ep_no=%d/%d [%d@%d.%d/%02x]\n", td->channel, td->transaction_started,
1394 td->dev_addr,td->haddr,td->hport, td->transfer_type);
1396 /* select endpoint 0 */
1397 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
1399 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1400 DPRINTFN(4, "csr=0x%02x\n", csr);
1403 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY)
1407 if (csr & (MUSB2_MASK_CSR0L_RXSTALL |
1408 MUSB2_MASK_CSR0L_ERROR))
1410 /* Clear status bit */
1411 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1412 DPRINTFN(1, "error bit set, csr=0x%02x\n", csr);
1414 musbotg_channel_free(sc, td);
1415 return (0); /* complete */
1418 if (td->transaction_started) {
1419 musbotg_channel_free(sc, td);
1420 return (0); /* complete */
1423 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, MUSB2_MASK_CSR0H_PING_DIS);
1425 MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(0), td->dev_addr);
1426 MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(0), td->haddr);
1427 MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(0), td->hport);
1428 MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
1430 /* TX NAK timeout */
1431 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
1433 td->transaction_started = 1;
1436 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1437 MUSB2_MASK_CSR0L_STATUSPKT |
1438 MUSB2_MASK_CSR0L_TXPKTRDY);
1440 return (1); /* wait for interrupt */
1444 musbotg_dev_data_rx(struct musbotg_td *td)
1446 struct usb_page_search buf_res;
1447 struct musbotg_softc *sc;
1453 to = 8; /* don't loop forever! */
1456 /* get pointer to softc */
1457 sc = MUSBOTG_PC2SC(td->pc);
1459 if (td->channel == -1)
1460 td->channel = musbotg_channel_alloc(sc, td, 0);
1462 /* EP0 is busy, wait */
1463 if (td->channel == -1)
1466 /* select endpoint */
1467 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1470 /* read out FIFO status */
1471 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
1473 DPRINTFN(4, "csr=0x%02x\n", csr);
1476 if (csr & MUSB2_MASK_CSRL_RXOVERRUN) {
1477 /* make sure we don't clear "RXPKTRDY" */
1478 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
1479 MUSB2_MASK_CSRL_RXPKTRDY);
1483 if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY))
1484 return (1); /* not complete */
1486 /* get the packet byte count */
1487 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
1489 DPRINTFN(4, "count=0x%04x\n", count);
1492 * Check for short or invalid packet:
1494 if (count != td->max_frame_size) {
1495 if (count < td->max_frame_size) {
1496 /* we have a short packet */
1500 /* invalid USB packet */
1502 musbotg_channel_free(sc, td);
1503 return (0); /* we are complete */
1506 /* verify the packet byte count */
1507 if (count > td->remainder) {
1508 /* invalid USB packet */
1510 musbotg_channel_free(sc, td);
1511 return (0); /* we are complete */
1516 usbd_get_page(td->pc, td->offset, &buf_res);
1518 /* get correct length */
1519 if (buf_res.length > count) {
1520 buf_res.length = count;
1522 /* check for unaligned memory address */
1523 if (USB_P2U(buf_res.buffer) & 3) {
1528 /* receive data 4 bytes at a time */
1529 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1530 MUSB2_REG_EPFIFO(td->channel), sc->sc_bounce_buf,
1535 /* receive data 1 byte at a time */
1536 bus_space_read_multi_1(sc->sc_io_tag,
1537 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1538 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1540 usbd_copy_in(td->pc, td->offset,
1541 sc->sc_bounce_buf, count);
1543 /* update offset and remainder */
1544 td->offset += count;
1545 td->remainder -= count;
1548 /* check if we can optimise */
1549 if (buf_res.length >= 4) {
1551 /* receive data 4 bytes at a time */
1552 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1553 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1554 buf_res.length / 4);
1556 temp = buf_res.length & ~3;
1558 /* update counters */
1561 td->remainder -= temp;
1565 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1566 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1569 /* update counters */
1570 count -= buf_res.length;
1571 td->offset += buf_res.length;
1572 td->remainder -= buf_res.length;
1575 /* clear status bits */
1576 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
1578 /* check if we are complete */
1579 if ((td->remainder == 0) || got_short) {
1580 if (td->short_pkt) {
1581 /* we are complete */
1582 musbotg_channel_free(sc, td);
1585 /* else need to receive a zero length packet */
1590 return (1); /* not complete */
1594 musbotg_dev_data_tx(struct musbotg_td *td)
1596 struct usb_page_search buf_res;
1597 struct musbotg_softc *sc;
1602 to = 8; /* don't loop forever! */
1604 /* get pointer to softc */
1605 sc = MUSBOTG_PC2SC(td->pc);
1607 if (td->channel == -1)
1608 td->channel = musbotg_channel_alloc(sc, td, 1);
1610 /* EP0 is busy, wait */
1611 if (td->channel == -1)
1614 /* select endpoint */
1615 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1619 /* read out FIFO status */
1620 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1622 DPRINTFN(4, "csr=0x%02x\n", csr);
1624 if (csr & (MUSB2_MASK_CSRL_TXINCOMP |
1625 MUSB2_MASK_CSRL_TXUNDERRUN)) {
1626 /* clear status bits */
1627 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1629 if (csr & MUSB2_MASK_CSRL_TXPKTRDY) {
1630 return (1); /* not complete */
1632 /* check for short packet */
1633 count = td->max_frame_size;
1634 if (td->remainder < count) {
1635 /* we have a short packet */
1637 count = td->remainder;
1642 usbd_get_page(td->pc, td->offset, &buf_res);
1644 /* get correct length */
1645 if (buf_res.length > count) {
1646 buf_res.length = count;
1648 /* check for unaligned memory address */
1649 if (USB_P2U(buf_res.buffer) & 3) {
1651 usbd_copy_out(td->pc, td->offset,
1652 sc->sc_bounce_buf, count);
1657 /* transmit data 4 bytes at a time */
1658 bus_space_write_multi_4(sc->sc_io_tag,
1659 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1660 sc->sc_bounce_buf, temp / 4);
1664 /* receive data 1 byte at a time */
1665 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1666 MUSB2_REG_EPFIFO(td->channel),
1667 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1669 /* update offset and remainder */
1670 td->offset += count;
1671 td->remainder -= count;
1674 /* check if we can optimise */
1675 if (buf_res.length >= 4) {
1677 /* transmit data 4 bytes at a time */
1678 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1679 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1680 buf_res.length / 4);
1682 temp = buf_res.length & ~3;
1684 /* update counters */
1687 td->remainder -= temp;
1691 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1692 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1695 /* update counters */
1696 count -= buf_res.length;
1697 td->offset += buf_res.length;
1698 td->remainder -= buf_res.length;
1701 /* Max packet size */
1702 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, td->reg_max_packet);
1705 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
1706 MUSB2_MASK_CSRL_TXPKTRDY);
1708 /* check remainder */
1709 if (td->remainder == 0) {
1710 if (td->short_pkt) {
1711 musbotg_channel_free(sc, td);
1712 return (0); /* complete */
1714 /* else we need to transmit a short packet */
1719 return (1); /* not complete */
1723 musbotg_host_data_rx(struct musbotg_td *td)
1725 struct usb_page_search buf_res;
1726 struct musbotg_softc *sc;
1732 /* get pointer to softc */
1733 sc = MUSBOTG_PC2SC(td->pc);
1735 if (td->channel == -1)
1736 td->channel = musbotg_channel_alloc(sc, td, 0);
1739 if (td->channel == -1)
1742 DPRINTFN(1, "ep_no=%d\n", td->channel);
1744 to = 8; /* don't loop forever! */
1747 /* select endpoint */
1748 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1751 /* read out FIFO status */
1752 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
1753 DPRINTFN(4, "csr=0x%02x\n", csr);
1755 if (!td->transaction_started) {
1756 /* Function address */
1757 MUSB2_WRITE_1(sc, MUSB2_REG_RXFADDR(td->channel),
1760 /* SPLIT transaction */
1761 MUSB2_WRITE_1(sc, MUSB2_REG_RXHADDR(td->channel),
1763 MUSB2_WRITE_1(sc, MUSB2_REG_RXHUBPORT(td->channel),
1766 /* RX NAK timeout */
1767 if (td->transfer_type & MUSB2_MASK_TI_PROTO_ISOC)
1768 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, 0);
1770 MUSB2_WRITE_1(sc, MUSB2_REG_RXNAKLIMIT, MAX_NAK_TO);
1772 /* Protocol, speed, device endpoint */
1773 MUSB2_WRITE_1(sc, MUSB2_REG_RXTI, td->transfer_type);
1775 /* Max packet size */
1776 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, td->reg_max_packet);
1779 csrh = MUSB2_READ_1(sc, MUSB2_REG_RXCSRH);
1780 DPRINTFN(4, "csrh=0x%02x\n", csrh);
1782 csrh |= MUSB2_MASK_CSRH_RXDT_WREN;
1784 csrh |= MUSB2_MASK_CSRH_RXDT_VAL;
1786 csrh &= ~MUSB2_MASK_CSRH_RXDT_VAL;
1788 /* Set data toggle */
1789 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, csrh);
1792 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
1793 MUSB2_MASK_CSRL_RXREQPKT);
1795 td->transaction_started = 1;
1799 /* clear NAK timeout */
1800 if (csr & MUSB2_MASK_CSRL_RXNAKTO) {
1801 DPRINTFN(4, "NAK Timeout\n");
1802 if (csr & MUSB2_MASK_CSRL_RXREQPKT) {
1803 csr &= ~MUSB2_MASK_CSRL_RXREQPKT;
1804 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, csr);
1806 csr &= ~MUSB2_MASK_CSRL_RXNAKTO;
1807 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, csr);
1813 if (csr & MUSB2_MASK_CSRL_RXERROR) {
1814 DPRINTFN(4, "RXERROR\n");
1818 if (csr & MUSB2_MASK_CSRL_RXSTALL) {
1819 DPRINTFN(4, "RXSTALL\n");
1824 musbotg_channel_free(sc, td);
1825 return (0); /* we are complete */
1828 if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY)) {
1829 /* No data available yet */
1834 /* get the packet byte count */
1835 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT);
1836 DPRINTFN(4, "count=0x%04x\n", count);
1839 * Check for short or invalid packet:
1841 if (count != td->max_frame_size) {
1842 if (count < td->max_frame_size) {
1843 /* we have a short packet */
1847 /* invalid USB packet */
1849 musbotg_channel_free(sc, td);
1850 return (0); /* we are complete */
1854 /* verify the packet byte count */
1855 if (count > td->remainder) {
1856 /* invalid USB packet */
1858 musbotg_channel_free(sc, td);
1859 return (0); /* we are complete */
1865 usbd_get_page(td->pc, td->offset, &buf_res);
1867 /* get correct length */
1868 if (buf_res.length > count) {
1869 buf_res.length = count;
1871 /* check for unaligned memory address */
1872 if (USB_P2U(buf_res.buffer) & 3) {
1877 /* receive data 4 bytes at a time */
1878 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1879 MUSB2_REG_EPFIFO(td->channel), sc->sc_bounce_buf,
1884 /* receive data 1 byte at a time */
1885 bus_space_read_multi_1(sc->sc_io_tag,
1886 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
1887 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
1889 usbd_copy_in(td->pc, td->offset,
1890 sc->sc_bounce_buf, count);
1892 /* update offset and remainder */
1893 td->offset += count;
1894 td->remainder -= count;
1897 /* check if we can optimise */
1898 if (buf_res.length >= 4) {
1900 /* receive data 4 bytes at a time */
1901 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
1902 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1903 buf_res.length / 4);
1905 temp = buf_res.length & ~3;
1907 /* update counters */
1910 td->remainder -= temp;
1914 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
1915 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
1918 /* update counters */
1919 count -= buf_res.length;
1920 td->offset += buf_res.length;
1921 td->remainder -= buf_res.length;
1924 /* clear status bits */
1925 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
1927 /* check if we are complete */
1928 if ((td->remainder == 0) || got_short) {
1929 if (td->short_pkt) {
1930 /* we are complete */
1931 musbotg_channel_free(sc, td);
1934 /* else need to receive a zero length packet */
1937 /* Reset transaction state and restart */
1938 td->transaction_started = 0;
1943 return (1); /* not complete */
1947 musbotg_host_data_tx(struct musbotg_td *td)
1949 struct usb_page_search buf_res;
1950 struct musbotg_softc *sc;
1954 /* get pointer to softc */
1955 sc = MUSBOTG_PC2SC(td->pc);
1957 if (td->channel == -1)
1958 td->channel = musbotg_channel_alloc(sc, td, 1);
1961 if (td->channel == -1)
1964 DPRINTFN(1, "ep_no=%d\n", td->channel);
1966 /* select endpoint */
1967 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->channel);
1969 /* read out FIFO status */
1970 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1971 DPRINTFN(4, "csr=0x%02x\n", csr);
1973 if (csr & (MUSB2_MASK_CSRL_TXSTALLED |
1974 MUSB2_MASK_CSRL_TXERROR)) {
1975 /* clear status bits */
1976 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
1978 musbotg_channel_free(sc, td);
1979 return (0); /* complete */
1982 if (csr & MUSB2_MASK_CSRL_TXNAKTO) {
1984 * Flush TX FIFO before clearing NAK TO
1986 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
1987 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
1988 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1989 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1990 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
1991 csr |= MUSB2_MASK_CSRL_TXFFLUSH;
1992 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
1993 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
1997 csr &= ~MUSB2_MASK_CSRL_TXNAKTO;
1998 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, csr);
2001 musbotg_channel_free(sc, td);
2002 return (0); /* complete */
2006 * Wait while FIFO is empty.
2007 * Do not flush it because it will cause transactions
2008 * with size more then packet size. It might upset
2011 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY)
2014 /* Packet still being processed */
2015 if (csr & MUSB2_MASK_CSRL_TXPKTRDY)
2018 if (td->transaction_started) {
2019 /* check remainder */
2020 if (td->remainder == 0) {
2021 if (td->short_pkt) {
2022 musbotg_channel_free(sc, td);
2023 return (0); /* complete */
2025 /* else we need to transmit a short packet */
2028 /* We're not complete - more transactions required */
2029 td->transaction_started = 0;
2032 /* check for short packet */
2033 count = td->max_frame_size;
2034 if (td->remainder < count) {
2035 /* we have a short packet */
2037 count = td->remainder;
2043 usbd_get_page(td->pc, td->offset, &buf_res);
2045 /* get correct length */
2046 if (buf_res.length > count) {
2047 buf_res.length = count;
2049 /* check for unaligned memory address */
2050 if (USB_P2U(buf_res.buffer) & 3) {
2052 usbd_copy_out(td->pc, td->offset,
2053 sc->sc_bounce_buf, count);
2058 /* transmit data 4 bytes at a time */
2059 bus_space_write_multi_4(sc->sc_io_tag,
2060 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->channel),
2061 sc->sc_bounce_buf, temp / 4);
2065 /* receive data 1 byte at a time */
2066 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
2067 MUSB2_REG_EPFIFO(td->channel),
2068 ((void *)&sc->sc_bounce_buf[count / 4]), temp);
2070 /* update offset and remainder */
2071 td->offset += count;
2072 td->remainder -= count;
2075 /* check if we can optimise */
2076 if (buf_res.length >= 4) {
2078 /* transmit data 4 bytes at a time */
2079 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl,
2080 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
2081 buf_res.length / 4);
2083 temp = buf_res.length & ~3;
2085 /* update counters */
2088 td->remainder -= temp;
2092 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl,
2093 MUSB2_REG_EPFIFO(td->channel), buf_res.buffer,
2096 /* update counters */
2097 count -= buf_res.length;
2098 td->offset += buf_res.length;
2099 td->remainder -= buf_res.length;
2102 /* Function address */
2103 MUSB2_WRITE_1(sc, MUSB2_REG_TXFADDR(td->channel),
2106 /* SPLIT transaction */
2107 MUSB2_WRITE_1(sc, MUSB2_REG_TXHADDR(td->channel),
2109 MUSB2_WRITE_1(sc, MUSB2_REG_TXHUBPORT(td->channel),
2112 /* TX NAK timeout */
2113 if (td->transfer_type & MUSB2_MASK_TI_PROTO_ISOC)
2114 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, 0);
2116 MUSB2_WRITE_1(sc, MUSB2_REG_TXNAKLIMIT, MAX_NAK_TO);
2118 /* Protocol, speed, device endpoint */
2119 MUSB2_WRITE_1(sc, MUSB2_REG_TXTI, td->transfer_type);
2121 /* Max packet size */
2122 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, td->reg_max_packet);
2124 if (!td->transaction_started) {
2125 csrh = MUSB2_READ_1(sc, MUSB2_REG_TXCSRH);
2126 DPRINTFN(4, "csrh=0x%02x\n", csrh);
2128 csrh |= MUSB2_MASK_CSRH_TXDT_WREN;
2130 csrh |= MUSB2_MASK_CSRH_TXDT_VAL;
2132 csrh &= ~MUSB2_MASK_CSRH_TXDT_VAL;
2134 /* Set data toggle */
2135 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, csrh);
2139 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2140 MUSB2_MASK_CSRL_TXPKTRDY);
2142 /* Update Data Toggle */
2144 td->transaction_started = 1;
2146 return (1); /* not complete */
2150 musbotg_xfer_do_fifo(struct usb_xfer *xfer)
2152 struct musbotg_softc *sc;
2153 struct musbotg_td *td;
2156 sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2158 td = xfer->td_transfer_cache;
2161 if ((td->func) (td)) {
2162 /* operation in progress */
2166 if (((void *)td) == xfer->td_transfer_last) {
2171 } else if (td->remainder > 0) {
2173 * We had a short transfer. If there is no alternate
2174 * next, stop processing !
2176 if (!td->alt_next) {
2181 * Fetch the next transfer descriptor and transfer
2182 * some flags to the next transfer descriptor
2185 xfer->td_transfer_cache = td;
2188 return (1); /* not complete */
2190 /* compute all actual lengths */
2191 musbotg_standard_done(xfer);
2193 return (0); /* complete */
2197 musbotg_interrupt_poll(struct musbotg_softc *sc)
2199 struct usb_xfer *xfer;
2202 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
2203 if (!musbotg_xfer_do_fifo(xfer)) {
2204 /* queue has been modified */
2211 musbotg_vbus_interrupt(struct musbotg_softc *sc, uint8_t is_on)
2213 DPRINTFN(4, "vbus = %u\n", is_on);
2215 USB_BUS_LOCK(&sc->sc_bus);
2217 if (!sc->sc_flags.status_vbus) {
2218 sc->sc_flags.status_vbus = 1;
2220 /* complete root HUB interrupt endpoint */
2221 musbotg_root_intr(sc);
2224 if (sc->sc_flags.status_vbus) {
2225 sc->sc_flags.status_vbus = 0;
2226 sc->sc_flags.status_bus_reset = 0;
2227 sc->sc_flags.status_suspend = 0;
2228 sc->sc_flags.change_suspend = 0;
2229 sc->sc_flags.change_connect = 1;
2231 /* complete root HUB interrupt endpoint */
2232 musbotg_root_intr(sc);
2236 USB_BUS_UNLOCK(&sc->sc_bus);
2240 musbotg_connect_interrupt(struct musbotg_softc *sc)
2242 USB_BUS_LOCK(&sc->sc_bus);
2243 sc->sc_flags.change_connect = 1;
2245 /* complete root HUB interrupt endpoint */
2246 musbotg_root_intr(sc);
2247 USB_BUS_UNLOCK(&sc->sc_bus);
2251 musbotg_interrupt(struct musbotg_softc *sc,
2252 uint16_t rxstat, uint16_t txstat, uint8_t stat)
2260 USB_BUS_LOCK(&sc->sc_bus);
2264 /* read all interrupt registers */
2265 usb_status = MUSB2_READ_1(sc, MUSB2_REG_INTUSB);
2267 /* read all FIFO interrupts */
2268 rx_status = MUSB2_READ_2(sc, MUSB2_REG_INTRX);
2269 tx_status = MUSB2_READ_2(sc, MUSB2_REG_INTTX);
2270 rx_status |= rxstat;
2271 tx_status |= txstat;
2274 /* Clear platform flags after first time */
2279 /* check for any bus state change interrupts */
2281 if (usb_status & (MUSB2_MASK_IRESET |
2282 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP |
2283 MUSB2_MASK_ICONN | MUSB2_MASK_IDISC |
2284 MUSB2_MASK_IVBUSERR)) {
2286 DPRINTFN(4, "real bus interrupt 0x%08x\n", usb_status);
2288 if (usb_status & MUSB2_MASK_IRESET) {
2290 /* set correct state */
2291 sc->sc_flags.status_bus_reset = 1;
2292 sc->sc_flags.status_suspend = 0;
2293 sc->sc_flags.change_suspend = 0;
2294 sc->sc_flags.change_connect = 1;
2296 /* determine line speed */
2297 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER);
2298 if (temp & MUSB2_MASK_HSMODE)
2299 sc->sc_flags.status_high_speed = 1;
2301 sc->sc_flags.status_high_speed = 0;
2304 * After reset all interrupts are on and we need to
2307 temp = MUSB2_MASK_IRESET;
2308 /* disable resume interrupt */
2309 temp &= ~MUSB2_MASK_IRESUME;
2310 /* enable suspend interrupt */
2311 temp |= MUSB2_MASK_ISUSP;
2312 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2313 /* disable TX and RX interrupts */
2314 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
2315 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
2318 * If RXRSM and RXSUSP is set at the same time we interpret
2319 * that like RESUME. Resume is set when there is at least 3
2320 * milliseconds of inactivity on the USB BUS.
2322 if (usb_status & MUSB2_MASK_IRESUME) {
2323 if (sc->sc_flags.status_suspend) {
2324 sc->sc_flags.status_suspend = 0;
2325 sc->sc_flags.change_suspend = 1;
2327 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE);
2328 /* disable resume interrupt */
2329 temp &= ~MUSB2_MASK_IRESUME;
2330 /* enable suspend interrupt */
2331 temp |= MUSB2_MASK_ISUSP;
2332 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2334 } else if (usb_status & MUSB2_MASK_ISUSP) {
2335 if (!sc->sc_flags.status_suspend) {
2336 sc->sc_flags.status_suspend = 1;
2337 sc->sc_flags.change_suspend = 1;
2339 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE);
2340 /* disable suspend interrupt */
2341 temp &= ~MUSB2_MASK_ISUSP;
2342 /* enable resume interrupt */
2343 temp |= MUSB2_MASK_IRESUME;
2344 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp);
2348 (MUSB2_MASK_ICONN | MUSB2_MASK_IDISC))
2349 sc->sc_flags.change_connect = 1;
2352 * Host Mode: There is no IRESET so assume bus is
2353 * always in reset state once device is connected.
2355 if (sc->sc_mode == MUSB2_HOST_MODE) {
2356 /* check for VBUS error in USB host mode */
2357 if (usb_status & MUSB2_MASK_IVBUSERR) {
2358 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
2359 temp |= MUSB2_MASK_SESS;
2360 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp);
2362 if (usb_status & MUSB2_MASK_ICONN)
2363 sc->sc_flags.status_bus_reset = 1;
2364 if (usb_status & MUSB2_MASK_IDISC)
2365 sc->sc_flags.status_bus_reset = 0;
2368 /* complete root HUB interrupt endpoint */
2369 musbotg_root_intr(sc);
2371 /* check for any endpoint interrupts */
2373 if (rx_status || tx_status) {
2374 DPRINTFN(4, "real endpoint interrupt "
2375 "rx=0x%04x, tx=0x%04x\n", rx_status, tx_status);
2377 /* poll one time regardless of FIFO status */
2379 musbotg_interrupt_poll(sc);
2384 USB_BUS_UNLOCK(&sc->sc_bus);
2388 musbotg_setup_standard_chain_sub(struct musbotg_std_temp *temp)
2390 struct musbotg_td *td;
2392 /* get current Transfer Descriptor */
2396 /* prepare for next TD */
2397 temp->td_next = td->obj_next;
2399 /* fill out the Transfer Descriptor */
2400 td->func = temp->func;
2402 td->offset = temp->offset;
2403 td->remainder = temp->len;
2405 td->transaction_started = 0;
2406 td->did_stall = temp->did_stall;
2407 td->short_pkt = temp->short_pkt;
2408 td->alt_next = temp->setup_alt_next;
2409 td->channel = temp->channel;
2410 td->dev_addr = temp->dev_addr;
2411 td->haddr = temp->haddr;
2412 td->hport = temp->hport;
2413 td->transfer_type = temp->transfer_type;
2417 musbotg_setup_standard_chain(struct usb_xfer *xfer)
2419 struct musbotg_std_temp temp;
2420 struct musbotg_softc *sc;
2421 struct musbotg_td *td;
2425 enum usb_dev_speed speed;
2429 DPRINTFN(8, "addr=%d endpt=%d sumlen=%d speed=%d\n",
2430 xfer->address, UE_GET_ADDR(xfer->endpointno),
2431 xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
2433 sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2434 ep_no = (xfer->endpointno & UE_ADDR);
2436 temp.max_frame_size = xfer->max_frame_size;
2438 td = xfer->td_start[0];
2439 xfer->td_transfer_first = td;
2440 xfer->td_transfer_cache = td;
2443 dev_addr = xfer->address;
2445 xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
2449 temp.td_next = xfer->td_start[0];
2451 temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
2452 xfer->flags_int.isochronous_xfr;
2453 temp.did_stall = !xfer->flags_int.control_stall;
2455 temp.dev_addr = dev_addr;
2456 temp.haddr = xfer->xroot->udev->hs_hub_addr;
2457 temp.hport = xfer->xroot->udev->hs_port_no;
2459 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2460 speed = usbd_get_speed(xfer->xroot->udev);
2464 temp.transfer_type = MUSB2_MASK_TI_SPEED_LO;
2466 case USB_SPEED_FULL:
2467 temp.transfer_type = MUSB2_MASK_TI_SPEED_FS;
2469 case USB_SPEED_HIGH:
2470 temp.transfer_type = MUSB2_MASK_TI_SPEED_HS;
2473 temp.transfer_type = 0;
2474 DPRINTFN(-1, "Invalid USB speed: %d\n", speed);
2478 switch (xfer_type) {
2480 temp.transfer_type |= MUSB2_MASK_TI_PROTO_CTRL;
2482 case UE_ISOCHRONOUS:
2483 temp.transfer_type |= MUSB2_MASK_TI_PROTO_ISOC;
2486 temp.transfer_type |= MUSB2_MASK_TI_PROTO_BULK;
2489 temp.transfer_type |= MUSB2_MASK_TI_PROTO_INTR;
2492 DPRINTFN(-1, "Invalid USB transfer type: %d\n",
2497 temp.transfer_type |= ep_no;
2498 td->toggle = xfer->endpoint->toggle_next;
2501 /* check if we should prepend a setup message */
2503 if (xfer->flags_int.control_xfr) {
2504 if (xfer->flags_int.control_hdr) {
2506 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE)
2507 temp.func = &musbotg_dev_ctrl_setup_rx;
2509 temp.func = &musbotg_host_ctrl_setup_tx;
2511 temp.len = xfer->frlengths[0];
2512 temp.pc = xfer->frbuffers + 0;
2513 temp.short_pkt = temp.len ? 1 : 0;
2515 musbotg_setup_standard_chain_sub(&temp);
2524 if (x != xfer->nframes) {
2525 if (xfer->endpointno & UE_DIR_IN)
2528 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
2532 if (xfer->flags_int.control_xfr)
2533 temp.func = &musbotg_host_ctrl_data_tx;
2535 temp.func = &musbotg_host_data_tx;
2537 if (xfer->flags_int.control_xfr)
2538 temp.func = &musbotg_host_ctrl_data_rx;
2540 temp.func = &musbotg_host_data_rx;
2545 if (xfer->flags_int.control_xfr)
2546 temp.func = &musbotg_dev_ctrl_data_tx;
2548 temp.func = &musbotg_dev_data_tx;
2550 if (xfer->flags_int.control_xfr)
2551 temp.func = &musbotg_dev_ctrl_data_rx;
2553 temp.func = &musbotg_dev_data_rx;
2557 /* setup "pc" pointer */
2558 temp.pc = xfer->frbuffers + x;
2560 while (x != xfer->nframes) {
2562 /* DATA0 / DATA1 message */
2564 temp.len = xfer->frlengths[x];
2568 if (x == xfer->nframes) {
2569 if (xfer->flags_int.control_xfr) {
2570 if (xfer->flags_int.control_act) {
2571 temp.setup_alt_next = 0;
2574 temp.setup_alt_next = 0;
2577 if (temp.len == 0) {
2579 /* make sure that we send an USB packet */
2585 if (xfer->flags_int.isochronous_xfr) {
2586 /* isochronous data transfer */
2587 /* don't force short */
2590 /* regular data transfer */
2591 temp.short_pkt = (xfer->flags.force_short_xfer ? 0 : 1);
2595 musbotg_setup_standard_chain_sub(&temp);
2597 if (xfer->flags_int.isochronous_xfr) {
2598 temp.offset += temp.len;
2600 /* get next Page Cache pointer */
2601 temp.pc = xfer->frbuffers + x;
2605 /* check for control transfer */
2606 if (xfer->flags_int.control_xfr) {
2608 /* always setup a valid "pc" pointer for status and sync */
2609 temp.pc = xfer->frbuffers + 0;
2612 temp.setup_alt_next = 0;
2614 /* check if we should append a status stage */
2615 if (!xfer->flags_int.control_act) {
2617 * Send a DATA1 message and invert the current
2618 * endpoint direction.
2620 if (sc->sc_mode == MUSB2_DEVICE_MODE)
2621 temp.func = &musbotg_dev_ctrl_status;
2623 if (xfer->endpointno & UE_DIR_IN)
2624 temp.func = musbotg_host_ctrl_status_tx;
2626 temp.func = musbotg_host_ctrl_status_rx;
2628 musbotg_setup_standard_chain_sub(&temp);
2631 /* must have at least one frame! */
2633 xfer->td_transfer_last = td;
2637 musbotg_timeout(void *arg)
2639 struct usb_xfer *xfer = arg;
2641 DPRINTFN(1, "xfer=%p\n", xfer);
2643 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2645 /* transfer is transferred */
2646 musbotg_device_done(xfer, USB_ERR_TIMEOUT);
2650 musbotg_ep_int_set(struct musbotg_softc *sc, int channel, int on)
2655 * Only enable the endpoint interrupt when we are
2656 * actually waiting for data, hence we are dealing
2657 * with level triggered interrupts !
2659 DPRINTFN(1, "ep_no=%d, on=%d\n", channel, on);
2665 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE);
2667 temp |= MUSB2_MASK_EPINT(0);
2669 temp &= ~MUSB2_MASK_EPINT(0);
2671 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp);
2673 temp = MUSB2_READ_2(sc, MUSB2_REG_INTRXE);
2675 temp |= MUSB2_MASK_EPINT(channel);
2677 temp &= ~MUSB2_MASK_EPINT(channel);
2678 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, temp);
2680 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE);
2682 temp |= MUSB2_MASK_EPINT(channel);
2684 temp &= ~MUSB2_MASK_EPINT(channel);
2685 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp);
2688 if (sc->sc_ep_int_set)
2689 sc->sc_ep_int_set(sc, channel, on);
2693 musbotg_start_standard_chain(struct usb_xfer *xfer)
2698 if (musbotg_xfer_do_fifo(xfer)) {
2700 DPRINTFN(14, "enabled interrupts on endpoint\n");
2702 /* put transfer on interrupt queue */
2703 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
2705 /* start timeout, if any */
2706 if (xfer->timeout != 0) {
2707 usbd_transfer_timeout_ms(xfer,
2708 &musbotg_timeout, xfer->timeout);
2714 musbotg_root_intr(struct musbotg_softc *sc)
2718 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
2721 sc->sc_hub_idata[0] = 0x02; /* we only have one port */
2723 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
2724 sizeof(sc->sc_hub_idata));
2728 musbotg_standard_done_sub(struct usb_xfer *xfer)
2730 struct musbotg_td *td;
2736 td = xfer->td_transfer_cache;
2739 len = td->remainder;
2741 xfer->endpoint->toggle_next = td->toggle;
2743 if (xfer->aframes != xfer->nframes) {
2745 * Verify the length and subtract
2746 * the remainder from "frlengths[]":
2748 if (len > xfer->frlengths[xfer->aframes]) {
2751 xfer->frlengths[xfer->aframes] -= len;
2754 /* Check for transfer error */
2756 /* the transfer is finished */
2761 /* Check for short transfer */
2763 if (xfer->flags_int.short_frames_ok ||
2764 xfer->flags_int.isochronous_xfr) {
2765 /* follow alt next */
2772 /* the transfer is finished */
2780 /* this USB frame is complete */
2786 /* update transfer cache */
2788 xfer->td_transfer_cache = td;
2791 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
2795 musbotg_standard_done(struct usb_xfer *xfer)
2797 usb_error_t err = 0;
2799 DPRINTFN(12, "xfer=%p endpoint=%p transfer done\n",
2800 xfer, xfer->endpoint);
2804 xfer->td_transfer_cache = xfer->td_transfer_first;
2806 if (xfer->flags_int.control_xfr) {
2808 if (xfer->flags_int.control_hdr) {
2810 err = musbotg_standard_done_sub(xfer);
2814 if (xfer->td_transfer_cache == NULL) {
2818 while (xfer->aframes != xfer->nframes) {
2820 err = musbotg_standard_done_sub(xfer);
2823 if (xfer->td_transfer_cache == NULL) {
2828 if (xfer->flags_int.control_xfr &&
2829 !xfer->flags_int.control_act) {
2831 err = musbotg_standard_done_sub(xfer);
2834 musbotg_device_done(xfer, err);
2837 /*------------------------------------------------------------------------*
2838 * musbotg_device_done
2840 * NOTE: this function can be called more than one time on the
2841 * same USB transfer!
2842 *------------------------------------------------------------------------*/
2844 musbotg_device_done(struct usb_xfer *xfer, usb_error_t error)
2846 struct musbotg_td *td;
2847 struct musbotg_softc *sc;
2849 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
2851 DPRINTFN(1, "xfer=%p, endpoint=%p, error=%d\n",
2852 xfer, xfer->endpoint, error);
2854 DPRINTFN(14, "disabled interrupts on endpoint\n");
2856 sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
2857 td = xfer->td_transfer_cache;
2859 if (td && (td->channel != -1))
2860 musbotg_channel_free(sc, td);
2862 /* dequeue transfer and start next transfer */
2863 usbd_transfer_done(xfer, error);
2867 musbotg_xfer_stall(struct usb_xfer *xfer)
2869 musbotg_device_done(xfer, USB_ERR_STALLED);
2873 musbotg_set_stall(struct usb_device *udev,
2874 struct usb_endpoint *ep, uint8_t *did_stall)
2876 struct musbotg_softc *sc;
2879 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
2881 DPRINTFN(4, "endpoint=%p\n", ep);
2883 /* set FORCESTALL */
2884 sc = MUSBOTG_BUS2SC(udev->bus);
2886 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
2888 /* select endpoint */
2889 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no);
2891 if (ep->edesc->bEndpointAddress & UE_DIR_IN) {
2892 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2893 MUSB2_MASK_CSRL_TXSENDSTALL);
2895 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
2896 MUSB2_MASK_CSRL_RXSENDSTALL);
2901 musbotg_clear_stall_sub(struct musbotg_softc *sc, uint16_t wMaxPacket,
2902 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir)
2908 if (ep_type == UE_CONTROL) {
2909 /* clearing stall is not needed */
2912 /* select endpoint */
2913 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no);
2915 /* compute max frame size */
2916 mps = wMaxPacket & 0x7FF;
2917 switch ((wMaxPacket >> 11) & 3) {
2928 if (ep_dir == UE_DIR_IN) {
2932 /* Configure endpoint */
2935 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2936 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2937 MUSB2_MASK_CSRH_TXMODE | temp);
2939 case UE_ISOCHRONOUS:
2940 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2941 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2942 MUSB2_MASK_CSRH_TXMODE |
2943 MUSB2_MASK_CSRH_TXISO | temp);
2946 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket);
2947 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH,
2948 MUSB2_MASK_CSRH_TXMODE | temp);
2954 /* Need to flush twice in case of double bufring */
2955 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2956 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2957 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2958 MUSB2_MASK_CSRL_TXFFLUSH);
2959 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2960 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) {
2961 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2962 MUSB2_MASK_CSRL_TXFFLUSH);
2963 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2966 /* reset data toggle */
2967 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL,
2968 MUSB2_MASK_CSRL_TXDT_CLR);
2969 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
2970 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2972 /* set double/single buffering */
2973 temp = MUSB2_READ_2(sc, MUSB2_REG_TXDBDIS);
2974 if (mps <= (sc->sc_hw_ep_profile[ep_no].
2975 max_in_frame_size / 2)) {
2977 temp &= ~(1 << ep_no);
2980 temp |= (1 << ep_no);
2982 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, temp);
2984 /* clear sent stall */
2985 if (csr & MUSB2_MASK_CSRL_TXSENTSTALL) {
2986 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0);
2987 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL);
2993 /* Configure endpoint */
2996 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
2997 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH,
2998 MUSB2_MASK_CSRH_RXNYET | temp);
3000 case UE_ISOCHRONOUS:
3001 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
3002 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH,
3003 MUSB2_MASK_CSRH_RXNYET |
3004 MUSB2_MASK_CSRH_RXISO | temp);
3007 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket);
3008 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, temp);
3014 /* Need to flush twice in case of double bufring */
3015 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
3016 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
3017 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
3018 MUSB2_MASK_CSRL_RXFFLUSH);
3019 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
3020 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) {
3021 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
3022 MUSB2_MASK_CSRL_RXFFLUSH);
3023 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
3026 /* reset data toggle */
3027 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL,
3028 MUSB2_MASK_CSRL_RXDT_CLR);
3029 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
3030 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL);
3032 /* set double/single buffering */
3033 temp = MUSB2_READ_2(sc, MUSB2_REG_RXDBDIS);
3034 if (mps <= (sc->sc_hw_ep_profile[ep_no].
3035 max_out_frame_size / 2)) {
3037 temp &= ~(1 << ep_no);
3040 temp |= (1 << ep_no);
3042 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, temp);
3044 /* clear sent stall */
3045 if (csr & MUSB2_MASK_CSRL_RXSENTSTALL) {
3046 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0);
3052 musbotg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
3054 struct musbotg_softc *sc;
3055 struct usb_endpoint_descriptor *ed;
3057 DPRINTFN(4, "endpoint=%p\n", ep);
3059 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
3062 if (udev->flags.usb_mode != USB_MODE_DEVICE) {
3067 sc = MUSBOTG_BUS2SC(udev->bus);
3069 /* get endpoint descriptor */
3072 /* reset endpoint */
3073 musbotg_clear_stall_sub(sc,
3074 UGETW(ed->wMaxPacketSize),
3075 (ed->bEndpointAddress & UE_ADDR),
3076 (ed->bmAttributes & UE_XFERTYPE),
3077 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
3081 musbotg_init(struct musbotg_softc *sc)
3083 const struct musb_otg_ep_cfg *cfg;
3084 struct usb_hw_ep_profile *pf;
3095 DPRINTFN(1, "start\n");
3097 /* set up the bus structure */
3098 sc->sc_bus.usbrev = USB_REV_2_0;
3099 sc->sc_bus.methods = &musbotg_bus_methods;
3101 /* Set a default endpoint configuration */
3102 if (sc->sc_ep_cfg == NULL)
3103 sc->sc_ep_cfg = musbotg_ep_default;
3105 USB_BUS_LOCK(&sc->sc_bus);
3107 /* turn on clocks */
3109 if (sc->sc_clocks_on) {
3110 (sc->sc_clocks_on) (sc->sc_clocks_arg);
3113 /* wait a little for things to stabilise */
3114 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000);
3116 /* disable all interrupts */
3118 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3119 DPRINTF("pre-DEVCTL=0x%02x\n", temp);
3121 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0);
3122 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
3123 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
3125 /* disable pullup */
3127 musbotg_pull_common(sc, 0);
3129 /* wait a little bit (10ms) */
3130 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
3133 /* disable double packet buffering */
3134 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, 0xFFFF);
3135 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, 0xFFFF);
3137 /* enable HighSpeed and ISO Update flags */
3139 MUSB2_WRITE_1(sc, MUSB2_REG_POWER,
3140 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD);
3142 if (sc->sc_mode == MUSB2_DEVICE_MODE) {
3143 /* clear Session bit, if set */
3144 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3145 temp &= ~MUSB2_MASK_SESS;
3146 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp);
3148 /* Enter session for Host mode */
3149 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL);
3150 temp |= MUSB2_MASK_SESS;
3151 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp);
3154 /* wait a little for things to stabilise */
3155 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 10);
3157 DPRINTF("DEVCTL=0x%02x\n", temp);
3159 /* disable testmode */
3161 MUSB2_WRITE_1(sc, MUSB2_REG_TESTMODE, 0);
3163 /* set default value */
3165 MUSB2_WRITE_1(sc, MUSB2_REG_MISC, 0);
3167 /* select endpoint index 0 */
3169 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0);
3171 if (sc->sc_ep_max == 0) {
3172 /* read out number of endpoints */
3175 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) / 16);
3178 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) % 16);
3180 sc->sc_ep_max = (nrx > ntx) ? nrx : ntx;
3182 nrx = ntx = sc->sc_ep_max;
3185 /* these numbers exclude the control endpoint */
3187 DPRINTFN(2, "RX/TX endpoints: %u/%u\n", nrx, ntx);
3189 if (sc->sc_ep_max == 0) {
3190 DPRINTFN(2, "ERROR: Looks like the clocks are off!\n");
3192 /* read out configuration data */
3194 sc->sc_conf_data = MUSB2_READ_1(sc, MUSB2_REG_CONFDATA);
3196 DPRINTFN(2, "Config Data: 0x%02x\n",
3199 dynfifo = (sc->sc_conf_data & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0;
3202 device_printf(sc->sc_bus.bdev, "Dynamic FIFO sizing detected, "
3203 "assuming 16Kbytes of FIFO RAM\n");
3206 DPRINTFN(2, "HW version: 0x%04x\n",
3207 MUSB2_READ_1(sc, MUSB2_REG_HWVERS));
3209 /* initialise endpoint profiles */
3213 for (temp = 1; temp <= sc->sc_ep_max; temp++) {
3214 pf = sc->sc_hw_ep_profile + temp;
3216 /* select endpoint */
3217 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, temp);
3219 fsize = MUSB2_READ_1(sc, MUSB2_REG_FSIZE);
3220 frx = (fsize & MUSB2_MASK_RX_FSIZE) / 16;
3221 ftx = (fsize & MUSB2_MASK_TX_FSIZE);
3223 DPRINTF("Endpoint %u FIFO size: IN=%u, OUT=%u, DYN=%d\n",
3224 temp, ftx, frx, dynfifo);
3227 if (frx && (temp <= nrx)) {
3228 for (i = 0; sc->sc_ep_cfg[i].ep_end >= 0; i++) {
3229 cfg = &sc->sc_ep_cfg[i];
3230 if (temp <= cfg->ep_end) {
3231 frx = cfg->ep_fifosz_shift;
3234 cfg->ep_fifosz_reg);
3239 MUSB2_WRITE_2(sc, MUSB2_REG_RXFIFOADD,
3242 offset += (1 << frx);
3244 if (ftx && (temp <= ntx)) {
3245 for (i = 0; sc->sc_ep_cfg[i].ep_end >= 0; i++) {
3246 cfg = &sc->sc_ep_cfg[i];
3247 if (temp <= cfg->ep_end) {
3248 ftx = cfg->ep_fifosz_shift;
3251 cfg->ep_fifosz_reg);
3256 MUSB2_WRITE_2(sc, MUSB2_REG_TXFIFOADD,
3259 offset += (1 << ftx);
3263 if (frx && ftx && (temp <= nrx) && (temp <= ntx)) {
3264 pf->max_in_frame_size = 1 << ftx;
3265 pf->max_out_frame_size = 1 << frx;
3266 pf->is_simplex = 0; /* duplex */
3267 pf->support_multi_buffer = 1;
3268 pf->support_bulk = 1;
3269 pf->support_interrupt = 1;
3270 pf->support_isochronous = 1;
3272 pf->support_out = 1;
3273 } else if (frx && (temp <= nrx)) {
3274 pf->max_out_frame_size = 1 << frx;
3275 pf->max_in_frame_size = 0;
3276 pf->is_simplex = 1; /* simplex */
3277 pf->support_multi_buffer = 1;
3278 pf->support_bulk = 1;
3279 pf->support_interrupt = 1;
3280 pf->support_isochronous = 1;
3281 pf->support_out = 1;
3282 } else if (ftx && (temp <= ntx)) {
3283 pf->max_in_frame_size = 1 << ftx;
3284 pf->max_out_frame_size = 0;
3285 pf->is_simplex = 1; /* simplex */
3286 pf->support_multi_buffer = 1;
3287 pf->support_bulk = 1;
3288 pf->support_interrupt = 1;
3289 pf->support_isochronous = 1;
3294 DPRINTFN(2, "Dynamic FIFO size = %d bytes\n", offset);
3296 /* turn on default interrupts */
3298 if (sc->sc_mode == MUSB2_HOST_MODE)
3299 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0xff);
3301 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE,
3304 musbotg_clocks_off(sc);
3306 USB_BUS_UNLOCK(&sc->sc_bus);
3308 /* catch any lost interrupts */
3310 musbotg_do_poll(&sc->sc_bus);
3312 return (0); /* success */
3316 musbotg_uninit(struct musbotg_softc *sc)
3318 USB_BUS_LOCK(&sc->sc_bus);
3320 /* disable all interrupts */
3321 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0);
3322 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0);
3323 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0);
3325 sc->sc_flags.port_powered = 0;
3326 sc->sc_flags.status_vbus = 0;
3327 sc->sc_flags.status_bus_reset = 0;
3328 sc->sc_flags.status_suspend = 0;
3329 sc->sc_flags.change_suspend = 0;
3330 sc->sc_flags.change_connect = 1;
3332 musbotg_pull_down(sc);
3333 musbotg_clocks_off(sc);
3334 USB_BUS_UNLOCK(&sc->sc_bus);
3338 musbotg_do_poll(struct usb_bus *bus)
3340 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus);
3342 USB_BUS_LOCK(&sc->sc_bus);
3343 musbotg_interrupt_poll(sc);
3344 USB_BUS_UNLOCK(&sc->sc_bus);
3347 /*------------------------------------------------------------------------*
3348 * musbotg bulk support
3349 *------------------------------------------------------------------------*/
3351 musbotg_device_bulk_open(struct usb_xfer *xfer)
3357 musbotg_device_bulk_close(struct usb_xfer *xfer)
3359 musbotg_device_done(xfer, USB_ERR_CANCELLED);
3363 musbotg_device_bulk_enter(struct usb_xfer *xfer)
3369 musbotg_device_bulk_start(struct usb_xfer *xfer)
3372 musbotg_setup_standard_chain(xfer);
3373 musbotg_start_standard_chain(xfer);
3376 static const struct usb_pipe_methods musbotg_device_bulk_methods =
3378 .open = musbotg_device_bulk_open,
3379 .close = musbotg_device_bulk_close,
3380 .enter = musbotg_device_bulk_enter,
3381 .start = musbotg_device_bulk_start,
3384 /*------------------------------------------------------------------------*
3385 * musbotg control support
3386 *------------------------------------------------------------------------*/
3388 musbotg_device_ctrl_open(struct usb_xfer *xfer)
3394 musbotg_device_ctrl_close(struct usb_xfer *xfer)
3396 musbotg_device_done(xfer, USB_ERR_CANCELLED);
3400 musbotg_device_ctrl_enter(struct usb_xfer *xfer)
3406 musbotg_device_ctrl_start(struct usb_xfer *xfer)
3409 musbotg_setup_standard_chain(xfer);
3410 musbotg_start_standard_chain(xfer);
3413 static const struct usb_pipe_methods musbotg_device_ctrl_methods =
3415 .open = musbotg_device_ctrl_open,
3416 .close = musbotg_device_ctrl_close,
3417 .enter = musbotg_device_ctrl_enter,
3418 .start = musbotg_device_ctrl_start,
3421 /*------------------------------------------------------------------------*
3422 * musbotg interrupt support
3423 *------------------------------------------------------------------------*/
3425 musbotg_device_intr_open(struct usb_xfer *xfer)
3431 musbotg_device_intr_close(struct usb_xfer *xfer)
3433 musbotg_device_done(xfer, USB_ERR_CANCELLED);
3437 musbotg_device_intr_enter(struct usb_xfer *xfer)
3443 musbotg_device_intr_start(struct usb_xfer *xfer)
3446 musbotg_setup_standard_chain(xfer);
3447 musbotg_start_standard_chain(xfer);
3450 static const struct usb_pipe_methods musbotg_device_intr_methods =
3452 .open = musbotg_device_intr_open,
3453 .close = musbotg_device_intr_close,
3454 .enter = musbotg_device_intr_enter,
3455 .start = musbotg_device_intr_start,
3458 /*------------------------------------------------------------------------*
3459 * musbotg full speed isochronous support
3460 *------------------------------------------------------------------------*/
3462 musbotg_device_isoc_open(struct usb_xfer *xfer)
3468 musbotg_device_isoc_close(struct usb_xfer *xfer)
3470 musbotg_device_done(xfer, USB_ERR_CANCELLED);
3474 musbotg_device_isoc_enter(struct usb_xfer *xfer)
3476 struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus);
3481 DPRINTFN(5, "xfer=%p next=%d nframes=%d\n",
3482 xfer, xfer->endpoint->isoc_next, xfer->nframes);
3484 /* get the current frame index */
3486 nframes = MUSB2_READ_2(sc, MUSB2_REG_FRAME);
3489 * check if the frame index is within the window where the frames
3492 temp = (nframes - xfer->endpoint->isoc_next) & MUSB2_MASK_FRAME;
3494 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) {
3495 fs_frames = (xfer->nframes + 7) / 8;
3497 fs_frames = xfer->nframes;
3500 if ((xfer->endpoint->is_synced == 0) ||
3501 (temp < fs_frames)) {
3503 * If there is data underflow or the pipe queue is
3504 * empty we schedule the transfer a few frames ahead
3505 * of the current frame position. Else two isochronous
3506 * transfers might overlap.
3508 xfer->endpoint->isoc_next = (nframes + 3) & MUSB2_MASK_FRAME;
3509 xfer->endpoint->is_synced = 1;
3510 DPRINTFN(2, "start next=%d\n", xfer->endpoint->isoc_next);
3513 * compute how many milliseconds the insertion is ahead of the
3514 * current frame position:
3516 temp = (xfer->endpoint->isoc_next - nframes) & MUSB2_MASK_FRAME;
3519 * pre-compute when the isochronous transfer will be finished:
3521 xfer->isoc_time_complete =
3522 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
3525 /* compute frame number for next insertion */
3526 xfer->endpoint->isoc_next += fs_frames;
3529 musbotg_setup_standard_chain(xfer);
3533 musbotg_device_isoc_start(struct usb_xfer *xfer)
3535 /* start TD chain */
3536 musbotg_start_standard_chain(xfer);
3539 static const struct usb_pipe_methods musbotg_device_isoc_methods =
3541 .open = musbotg_device_isoc_open,
3542 .close = musbotg_device_isoc_close,
3543 .enter = musbotg_device_isoc_enter,
3544 .start = musbotg_device_isoc_start,
3547 /*------------------------------------------------------------------------*
3548 * musbotg root control support
3549 *------------------------------------------------------------------------*
3550 * Simulate a hardware HUB by handling all the necessary requests.
3551 *------------------------------------------------------------------------*/
3553 static const struct usb_device_descriptor musbotg_devd = {
3554 .bLength = sizeof(struct usb_device_descriptor),
3555 .bDescriptorType = UDESC_DEVICE,
3556 .bcdUSB = {0x00, 0x02},
3557 .bDeviceClass = UDCLASS_HUB,
3558 .bDeviceSubClass = UDSUBCLASS_HUB,
3559 .bDeviceProtocol = UDPROTO_HSHUBSTT,
3560 .bMaxPacketSize = 64,
3561 .bcdDevice = {0x00, 0x01},
3564 .bNumConfigurations = 1,
3567 static const struct usb_device_qualifier musbotg_odevd = {
3568 .bLength = sizeof(struct usb_device_qualifier),
3569 .bDescriptorType = UDESC_DEVICE_QUALIFIER,
3570 .bcdUSB = {0x00, 0x02},
3571 .bDeviceClass = UDCLASS_HUB,
3572 .bDeviceSubClass = UDSUBCLASS_HUB,
3573 .bDeviceProtocol = UDPROTO_FSHUB,
3574 .bMaxPacketSize0 = 0,
3575 .bNumConfigurations = 0,
3578 static const struct musbotg_config_desc musbotg_confd = {
3580 .bLength = sizeof(struct usb_config_descriptor),
3581 .bDescriptorType = UDESC_CONFIG,
3582 .wTotalLength[0] = sizeof(musbotg_confd),
3584 .bConfigurationValue = 1,
3585 .iConfiguration = 0,
3586 .bmAttributes = UC_SELF_POWERED,
3590 .bLength = sizeof(struct usb_interface_descriptor),
3591 .bDescriptorType = UDESC_INTERFACE,
3593 .bInterfaceClass = UICLASS_HUB,
3594 .bInterfaceSubClass = UISUBCLASS_HUB,
3595 .bInterfaceProtocol = 0,
3598 .bLength = sizeof(struct usb_endpoint_descriptor),
3599 .bDescriptorType = UDESC_ENDPOINT,
3600 .bEndpointAddress = (UE_DIR_IN | MUSBOTG_INTR_ENDPT),
3601 .bmAttributes = UE_INTERRUPT,
3602 .wMaxPacketSize[0] = 8,
3607 #define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
3609 static const struct usb_hub_descriptor_min musbotg_hubd = {
3610 .bDescLength = sizeof(musbotg_hubd),
3611 .bDescriptorType = UDESC_HUB,
3613 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
3614 .bPwrOn2PwrGood = 50,
3615 .bHubContrCurrent = 0,
3616 .DeviceRemovable = {0}, /* port is removable */
3619 #define STRING_VENDOR \
3620 "M\0e\0n\0t\0o\0r\0 \0G\0r\0a\0p\0h\0i\0c\0s"
3622 #define STRING_PRODUCT \
3623 "O\0T\0G\0 \0R\0o\0o\0t\0 \0H\0U\0B"
3625 USB_MAKE_STRING_DESC(STRING_VENDOR, musbotg_vendor);
3626 USB_MAKE_STRING_DESC(STRING_PRODUCT, musbotg_product);
3629 musbotg_roothub_exec(struct usb_device *udev,
3630 struct usb_device_request *req, const void **pptr, uint16_t *plength)
3632 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
3640 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
3643 ptr = (const void *)&sc->sc_hub_temp;
3647 value = UGETW(req->wValue);
3648 index = UGETW(req->wIndex);
3650 /* demultiplex the control request */
3652 switch (req->bmRequestType) {
3653 case UT_READ_DEVICE:
3654 switch (req->bRequest) {
3655 case UR_GET_DESCRIPTOR:
3656 goto tr_handle_get_descriptor;
3658 goto tr_handle_get_config;
3660 goto tr_handle_get_status;
3666 case UT_WRITE_DEVICE:
3667 switch (req->bRequest) {
3668 case UR_SET_ADDRESS:
3669 goto tr_handle_set_address;
3671 goto tr_handle_set_config;
3672 case UR_CLEAR_FEATURE:
3673 goto tr_valid; /* nop */
3674 case UR_SET_DESCRIPTOR:
3675 goto tr_valid; /* nop */
3676 case UR_SET_FEATURE:
3682 case UT_WRITE_ENDPOINT:
3683 switch (req->bRequest) {
3684 case UR_CLEAR_FEATURE:
3685 switch (UGETW(req->wValue)) {
3686 case UF_ENDPOINT_HALT:
3687 goto tr_handle_clear_halt;
3688 case UF_DEVICE_REMOTE_WAKEUP:
3689 goto tr_handle_clear_wakeup;
3694 case UR_SET_FEATURE:
3695 switch (UGETW(req->wValue)) {
3696 case UF_ENDPOINT_HALT:
3697 goto tr_handle_set_halt;
3698 case UF_DEVICE_REMOTE_WAKEUP:
3699 goto tr_handle_set_wakeup;
3704 case UR_SYNCH_FRAME:
3705 goto tr_valid; /* nop */
3711 case UT_READ_ENDPOINT:
3712 switch (req->bRequest) {
3714 goto tr_handle_get_ep_status;
3720 case UT_WRITE_INTERFACE:
3721 switch (req->bRequest) {
3722 case UR_SET_INTERFACE:
3723 goto tr_handle_set_interface;
3724 case UR_CLEAR_FEATURE:
3725 goto tr_valid; /* nop */
3726 case UR_SET_FEATURE:
3732 case UT_READ_INTERFACE:
3733 switch (req->bRequest) {
3734 case UR_GET_INTERFACE:
3735 goto tr_handle_get_interface;
3737 goto tr_handle_get_iface_status;
3743 case UT_WRITE_CLASS_INTERFACE:
3744 case UT_WRITE_VENDOR_INTERFACE:
3748 case UT_READ_CLASS_INTERFACE:
3749 case UT_READ_VENDOR_INTERFACE:
3753 case UT_WRITE_CLASS_DEVICE:
3754 switch (req->bRequest) {
3755 case UR_CLEAR_FEATURE:
3757 case UR_SET_DESCRIPTOR:
3758 case UR_SET_FEATURE:
3765 case UT_WRITE_CLASS_OTHER:
3766 switch (req->bRequest) {
3767 case UR_CLEAR_FEATURE:
3768 goto tr_handle_clear_port_feature;
3769 case UR_SET_FEATURE:
3770 goto tr_handle_set_port_feature;
3771 case UR_CLEAR_TT_BUFFER:
3781 case UT_READ_CLASS_OTHER:
3782 switch (req->bRequest) {
3783 case UR_GET_TT_STATE:
3784 goto tr_handle_get_tt_state;
3786 goto tr_handle_get_port_status;
3792 case UT_READ_CLASS_DEVICE:
3793 switch (req->bRequest) {
3794 case UR_GET_DESCRIPTOR:
3795 goto tr_handle_get_class_descriptor;
3797 goto tr_handle_get_class_status;
3808 tr_handle_get_descriptor:
3809 switch (value >> 8) {
3814 len = sizeof(musbotg_devd);
3815 ptr = (const void *)&musbotg_devd;
3817 case UDESC_DEVICE_QUALIFIER:
3821 len = sizeof(musbotg_odevd);
3822 ptr = (const void *)&musbotg_odevd;
3828 len = sizeof(musbotg_confd);
3829 ptr = (const void *)&musbotg_confd;
3832 switch (value & 0xff) {
3833 case 0: /* Language table */
3834 len = sizeof(usb_string_lang_en);
3835 ptr = (const void *)&usb_string_lang_en;
3838 case 1: /* Vendor */
3839 len = sizeof(musbotg_vendor);
3840 ptr = (const void *)&musbotg_vendor;
3843 case 2: /* Product */
3844 len = sizeof(musbotg_product);
3845 ptr = (const void *)&musbotg_product;
3856 tr_handle_get_config:
3858 sc->sc_hub_temp.wValue[0] = sc->sc_conf;
3861 tr_handle_get_status:
3863 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
3866 tr_handle_set_address:
3867 if (value & 0xFF00) {
3870 sc->sc_rt_addr = value;
3873 tr_handle_set_config:
3877 sc->sc_conf = value;
3880 tr_handle_get_interface:
3882 sc->sc_hub_temp.wValue[0] = 0;
3885 tr_handle_get_tt_state:
3886 tr_handle_get_class_status:
3887 tr_handle_get_iface_status:
3888 tr_handle_get_ep_status:
3890 USETW(sc->sc_hub_temp.wValue, 0);
3894 tr_handle_set_interface:
3895 tr_handle_set_wakeup:
3896 tr_handle_clear_wakeup:
3897 tr_handle_clear_halt:
3900 tr_handle_clear_port_feature:
3904 DPRINTFN(8, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
3907 case UHF_PORT_SUSPEND:
3908 if (sc->sc_mode == MUSB2_HOST_MODE)
3909 musbotg_wakeup_host(sc);
3911 musbotg_wakeup_peer(sc);
3914 case UHF_PORT_ENABLE:
3915 sc->sc_flags.port_enabled = 0;
3918 case UHF_C_PORT_ENABLE:
3919 sc->sc_flags.change_enabled = 0;
3922 case UHF_C_PORT_OVER_CURRENT:
3923 sc->sc_flags.change_over_current = 0;
3926 case UHF_C_PORT_RESET:
3927 sc->sc_flags.change_reset = 0;
3931 case UHF_PORT_INDICATOR:
3935 case UHF_PORT_POWER:
3936 sc->sc_flags.port_powered = 0;
3937 musbotg_pull_down(sc);
3938 musbotg_clocks_off(sc);
3940 case UHF_C_PORT_CONNECTION:
3941 sc->sc_flags.change_connect = 0;
3943 case UHF_C_PORT_SUSPEND:
3944 sc->sc_flags.change_suspend = 0;
3947 err = USB_ERR_IOERROR;
3952 tr_handle_set_port_feature:
3956 DPRINTFN(8, "UR_SET_PORT_FEATURE\n");
3959 case UHF_PORT_ENABLE:
3960 sc->sc_flags.port_enabled = 1;
3962 case UHF_PORT_SUSPEND:
3963 if (sc->sc_mode == MUSB2_HOST_MODE)
3964 musbotg_suspend_host(sc);
3967 case UHF_PORT_RESET:
3968 if (sc->sc_mode == MUSB2_HOST_MODE) {
3969 reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3970 reg |= MUSB2_MASK_RESET;
3971 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, reg);
3973 /* Wait for 20 msec */
3974 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 5);
3976 reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3977 reg &= ~MUSB2_MASK_RESET;
3978 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, reg);
3980 /* determine line speed */
3981 reg = MUSB2_READ_1(sc, MUSB2_REG_POWER);
3982 if (reg & MUSB2_MASK_HSMODE)
3983 sc->sc_flags.status_high_speed = 1;
3985 sc->sc_flags.status_high_speed = 0;
3987 sc->sc_flags.change_reset = 1;
3989 err = USB_ERR_IOERROR;
3993 case UHF_PORT_INDICATOR:
3996 case UHF_PORT_POWER:
3997 sc->sc_flags.port_powered = 1;
4000 err = USB_ERR_IOERROR;
4005 tr_handle_get_port_status:
4007 DPRINTFN(8, "UR_GET_PORT_STATUS\n");
4012 if (sc->sc_flags.status_vbus) {
4013 musbotg_clocks_on(sc);
4014 musbotg_pull_up(sc);
4016 musbotg_pull_down(sc);
4017 musbotg_clocks_off(sc);
4020 /* Select Device Side Mode */
4021 if (sc->sc_mode == MUSB2_DEVICE_MODE)
4022 value = UPS_PORT_MODE_DEVICE;
4026 if (sc->sc_flags.status_high_speed) {
4027 value |= UPS_HIGH_SPEED;
4029 if (sc->sc_flags.port_powered) {
4030 value |= UPS_PORT_POWER;
4032 if (sc->sc_flags.port_enabled) {
4033 value |= UPS_PORT_ENABLED;
4036 if (sc->sc_flags.port_over_current)
4037 value |= UPS_OVERCURRENT_INDICATOR;
4039 if (sc->sc_flags.status_vbus &&
4040 sc->sc_flags.status_bus_reset) {
4041 value |= UPS_CURRENT_CONNECT_STATUS;
4043 if (sc->sc_flags.status_suspend) {
4044 value |= UPS_SUSPEND;
4046 USETW(sc->sc_hub_temp.ps.wPortStatus, value);
4050 if (sc->sc_flags.change_connect) {
4051 value |= UPS_C_CONNECT_STATUS;
4053 if (sc->sc_mode == MUSB2_DEVICE_MODE) {
4054 if (sc->sc_flags.status_vbus &&
4055 sc->sc_flags.status_bus_reset) {
4056 /* reset EP0 state */
4057 sc->sc_ep0_busy = 0;
4062 if (sc->sc_flags.change_suspend)
4063 value |= UPS_C_SUSPEND;
4064 if (sc->sc_flags.change_reset)
4065 value |= UPS_C_PORT_RESET;
4066 if (sc->sc_flags.change_over_current)
4067 value |= UPS_C_OVERCURRENT_INDICATOR;
4069 USETW(sc->sc_hub_temp.ps.wPortChange, value);
4070 len = sizeof(sc->sc_hub_temp.ps);
4073 tr_handle_get_class_descriptor:
4077 ptr = (const void *)&musbotg_hubd;
4078 len = sizeof(musbotg_hubd);
4082 err = USB_ERR_STALLED;
4091 musbotg_xfer_setup(struct usb_setup_params *parm)
4093 struct musbotg_softc *sc;
4094 struct usb_xfer *xfer;
4100 sc = MUSBOTG_BUS2SC(parm->udev->bus);
4101 xfer = parm->curr_xfer;
4104 * NOTE: This driver does not use any of the parameters that
4105 * are computed from the following values. Just set some
4106 * reasonable dummies:
4108 parm->hc_max_packet_size = 0x400;
4109 parm->hc_max_frame_size = 0xc00;
4111 if ((parm->methods == &musbotg_device_isoc_methods) ||
4112 (parm->methods == &musbotg_device_intr_methods))
4113 parm->hc_max_packet_count = 3;
4115 parm->hc_max_packet_count = 1;
4117 usbd_transfer_setup_sub(parm);
4120 * compute maximum number of TDs
4122 if (parm->methods == &musbotg_device_ctrl_methods) {
4124 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ;
4126 } else if (parm->methods == &musbotg_device_bulk_methods) {
4128 ntd = xfer->nframes + 1 /* SYNC */ ;
4130 } else if (parm->methods == &musbotg_device_intr_methods) {
4132 ntd = xfer->nframes + 1 /* SYNC */ ;
4134 } else if (parm->methods == &musbotg_device_isoc_methods) {
4136 ntd = xfer->nframes + 1 /* SYNC */ ;
4144 * check if "usbd_transfer_setup_sub" set an error
4150 * allocate transfer descriptors
4154 ep_no = xfer->endpointno & UE_ADDR;
4157 * Check for a valid endpoint profile in USB device mode:
4159 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
4160 const struct usb_hw_ep_profile *pf;
4162 musbotg_get_hw_ep_profile(parm->udev, &pf, ep_no);
4165 /* should not happen */
4166 parm->err = USB_ERR_INVAL;
4172 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
4174 for (n = 0; n != ntd; n++) {
4176 struct musbotg_td *td;
4180 td = USB_ADD_BYTES(parm->buf, parm->size[0]);
4183 td->max_frame_size = xfer->max_frame_size;
4184 td->reg_max_packet = xfer->max_packet_size |
4185 ((xfer->max_packet_count - 1) << 11);
4187 td->obj_next = last_obj;
4191 parm->size[0] += sizeof(*td);
4194 xfer->td_start[0] = last_obj;
4198 musbotg_xfer_unsetup(struct usb_xfer *xfer)
4204 musbotg_get_dma_delay(struct usb_device *udev, uint32_t *pus)
4206 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
4208 if (sc->sc_mode == MUSB2_HOST_MODE)
4209 *pus = 2000; /* microseconds */
4215 musbotg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
4216 struct usb_endpoint *ep)
4218 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus);
4220 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n",
4222 edesc->bEndpointAddress, udev->flags.usb_mode,
4225 if (udev->device_index != sc->sc_rt_addr) {
4226 switch (edesc->bmAttributes & UE_XFERTYPE) {
4228 ep->methods = &musbotg_device_ctrl_methods;
4231 ep->methods = &musbotg_device_intr_methods;
4233 case UE_ISOCHRONOUS:
4234 ep->methods = &musbotg_device_isoc_methods;
4237 ep->methods = &musbotg_device_bulk_methods;
4247 musbotg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
4249 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus);
4252 case USB_HW_POWER_SUSPEND:
4255 case USB_HW_POWER_SHUTDOWN:
4258 case USB_HW_POWER_RESUME:
4266 static const struct usb_bus_methods musbotg_bus_methods =
4268 .endpoint_init = &musbotg_ep_init,
4269 .get_dma_delay = &musbotg_get_dma_delay,
4270 .xfer_setup = &musbotg_xfer_setup,
4271 .xfer_unsetup = &musbotg_xfer_unsetup,
4272 .get_hw_ep_profile = &musbotg_get_hw_ep_profile,
4273 .xfer_stall = &musbotg_xfer_stall,
4274 .set_stall = &musbotg_set_stall,
4275 .clear_stall = &musbotg_clear_stall,
4276 .roothub_exec = &musbotg_roothub_exec,
4277 .xfer_poll = &musbotg_do_poll,
4278 .set_hw_power_sleep = &musbotg_set_hw_power_sleep,