musb_otg.c revision 235000
1139825Simp/* $FreeBSD: stable/9/sys/dev/usb/controller/musb_otg.c 235000 2012-05-04 15:05:30Z hselasky $ */ 294755Sbenno/*- 394755Sbenno * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 494755Sbenno * 594755Sbenno * Redistribution and use in source and binary forms, with or without 694755Sbenno * modification, are permitted provided that the following conditions 794755Sbenno * are met: 894755Sbenno * 1. Redistributions of source code must retain the above copyright 994755Sbenno * notice, this list of conditions and the following disclaimer. 1094755Sbenno * 2. Redistributions in binary form must reproduce the above copyright 1194755Sbenno * notice, this list of conditions and the following disclaimer in the 1294755Sbenno * documentation and/or other materials provided with the distribution. 1394755Sbenno * 1494755Sbenno * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15125702Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1694755Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1794755Sbenno * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1894755Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1994755Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2094755Sbenno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2194755Sbenno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2294755Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2394755Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2494755Sbenno * SUCH DAMAGE. 2594755Sbenno */ 2694755Sbenno 2794755Sbenno/* 2894755Sbenno * Thanks to Mentor Graphics for providing a reference driver for this USB chip 2994755Sbenno * at their homepage. 3094755Sbenno */ 3194755Sbenno 3294755Sbenno/* 3394755Sbenno * This file contains the driver for the Mentor Graphics Inventra USB 3494755Sbenno * 2.0 High Speed Dual-Role controller. 3594755Sbenno * 3694755Sbenno * NOTE: The current implementation only supports Device Side Mode! 3794755Sbenno */ 3894755Sbenno 3994755Sbenno#include <sys/stdint.h> 4094755Sbenno#include <sys/stddef.h> 4194755Sbenno#include <sys/param.h> 4294755Sbenno#include <sys/queue.h> 4394755Sbenno#include <sys/types.h> 4494755Sbenno#include <sys/systm.h> 4594755Sbenno#include <sys/kernel.h> 4694755Sbenno#include <sys/bus.h> 4794755Sbenno#include <sys/module.h> 4894755Sbenno#include <sys/lock.h> 4994755Sbenno#include <sys/mutex.h> 5094755Sbenno#include <sys/condvar.h> 5194755Sbenno#include <sys/sysctl.h> 5294755Sbenno#include <sys/sx.h> 5394755Sbenno#include <sys/unistd.h> 5494755Sbenno#include <sys/callout.h> 5594755Sbenno#include <sys/malloc.h> 56227843Smarius#include <sys/priv.h> 57227843Smarius 58227843Smarius#include <dev/usb/usb.h> 5994755Sbenno#include <dev/usb/usbdi.h> 6094755Sbenno 61131102Sgrehan#define USB_DEBUG_VAR musbotgdebug 6294755Sbenno 6394755Sbenno#include <dev/usb/usb_core.h> 6494755Sbenno#include <dev/usb/usb_debug.h> 6594755Sbenno#include <dev/usb/usb_busdma.h> 6694755Sbenno#include <dev/usb/usb_process.h> 67212239Smav#include <dev/usb/usb_transfer.h> 6894755Sbenno#include <dev/usb/usb_device.h> 6994755Sbenno#include <dev/usb/usb_hub.h> 7094755Sbenno#include <dev/usb/usb_util.h> 7194755Sbenno 7299652Sbenno#include <dev/usb/usb_controller.h> 7394755Sbenno#include <dev/usb/usb_bus.h> 7494755Sbenno#include <dev/usb/controller/musb_otg.h> 7599652Sbenno 7699652Sbenno#define MUSBOTG_INTR_ENDPT 1 77168885Sgrehan 7899652Sbenno#define MUSBOTG_BUS2SC(bus) \ 7999652Sbenno ((struct musbotg_softc *)(((uint8_t *)(bus)) - \ 8094755Sbenno USB_P2U(&(((struct musbotg_softc *)0)->sc_bus)))) 8194755Sbenno 82133862Smarius#define MUSBOTG_PC2SC(pc) \ 8394755Sbenno MUSBOTG_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus) 8494755Sbenno 8594755Sbenno#ifdef USB_DEBUG 86133862Smariusstatic int musbotgdebug = 0; 8794755Sbenno 8894755SbennoSYSCTL_NODE(_hw_usb, OID_AUTO, musbotg, CTLFLAG_RW, 0, "USB musbotg"); 8994755SbennoSYSCTL_INT(_hw_usb_musbotg, OID_AUTO, debug, CTLFLAG_RW, 9094755Sbenno &musbotgdebug, 0, "Debug level"); 91183882Snwhitehorn#endif 92183882Snwhitehorn 93183882Snwhitehorn/* prototypes */ 94183882Snwhitehorn 95183882Snwhitehornstruct usb_bus_methods musbotg_bus_methods; 96183882Snwhitehornstruct usb_pipe_methods musbotg_device_bulk_methods; 97183882Snwhitehornstruct usb_pipe_methods musbotg_device_ctrl_methods; 9894755Sbennostruct usb_pipe_methods musbotg_device_intr_methods; 9994755Sbennostruct usb_pipe_methods musbotg_device_isoc_methods; 10094755Sbenno 101125702Sgrehanstatic musbotg_cmd_t musbotg_setup_rx; 102125702Sgrehanstatic musbotg_cmd_t musbotg_setup_data_rx; 103125702Sgrehanstatic musbotg_cmd_t musbotg_setup_data_tx; 10494755Sbennostatic musbotg_cmd_t musbotg_setup_status; 10594755Sbennostatic musbotg_cmd_t musbotg_data_rx; 10694755Sbennostatic musbotg_cmd_t musbotg_data_tx; 107171805Smarcelstatic void musbotg_device_done(struct usb_xfer *, usb_error_t); 10894755Sbennostatic void musbotg_do_poll(struct usb_bus *); 10994755Sbennostatic void musbotg_standard_done(struct usb_xfer *); 11099652Sbennostatic void musbotg_interrupt_poll(struct musbotg_softc *); 11199652Sbennostatic void musbotg_root_intr(struct musbotg_softc *); 11299652Sbenno 11399652Sbenno/* 114178367Smarcel * Here is a configuration that the chip supports. 11594755Sbenno */ 11699652Sbennostatic const struct usb_hw_ep_profile musbotg_ep_profile[1] = { 11799652Sbenno 11899652Sbenno [0] = { 119212413Savg .max_in_frame_size = 64,/* fixed */ 120178367Smarcel .max_out_frame_size = 64, /* fixed */ 12199652Sbenno .is_simplex = 1, 12299652Sbenno .support_control = 1, 123209486Snwhitehorn } 124209486Snwhitehorn}; 125209486Snwhitehorn 126209486Snwhitehornstatic void 127209486Snwhitehornmusbotg_get_hw_ep_profile(struct usb_device *udev, 128209486Snwhitehorn const struct usb_hw_ep_profile **ppf, uint8_t ep_addr) 12999652Sbenno{ 130167289Spiso struct musbotg_softc *sc; 13199652Sbenno 13299652Sbenno sc = MUSBOTG_BUS2SC(udev->bus); 13399652Sbenno 13499652Sbenno if (ep_addr == 0) { 13599652Sbenno /* control endpoint */ 13699652Sbenno *ppf = musbotg_ep_profile; 13799652Sbenno } else if (ep_addr <= sc->sc_ep_max) { 13899652Sbenno /* other endpoints */ 13999652Sbenno *ppf = sc->sc_hw_ep_profile + ep_addr; 14099652Sbenno } else { 14199652Sbenno *ppf = NULL; 142178367Smarcel } 143178367Smarcel} 144178367Smarcel 145168885Sgrehanstatic void 146168885Sgrehanmusbotg_clocks_on(struct musbotg_softc *sc) 147168885Sgrehan{ 148168885Sgrehan if (sc->sc_flags.clocks_off && 149168885Sgrehan sc->sc_flags.port_powered) { 15099652Sbenno 15199652Sbenno DPRINTFN(4, "\n"); 15299652Sbenno 153124468Sgrehan if (sc->sc_clocks_on) { 15499652Sbenno (sc->sc_clocks_on) (sc->sc_clocks_arg); 15594755Sbenno } 15694755Sbenno sc->sc_flags.clocks_off = 0; 15794755Sbenno 158178367Smarcel /* XXX enable Transceiver */ 15994755Sbenno } 16094755Sbenno} 16194755Sbenno 16294755Sbennostatic void 16394755Sbennomusbotg_clocks_off(struct musbotg_softc *sc) 16494755Sbenno{ 165124468Sgrehan if (!sc->sc_flags.clocks_off) { 166212239Smav 16794755Sbenno DPRINTFN(4, "\n"); 16894755Sbenno 16994755Sbenno /* XXX disable Transceiver */ 17099652Sbenno 17199652Sbenno if (sc->sc_clocks_off) { 172209486Snwhitehorn (sc->sc_clocks_off) (sc->sc_clocks_arg); 173209486Snwhitehorn } 174209486Snwhitehorn sc->sc_flags.clocks_off = 1; 175209486Snwhitehorn } 17699652Sbenno} 17799652Sbenno 17899652Sbennostatic void 17999652Sbennomusbotg_pull_common(struct musbotg_softc *sc, uint8_t on) 18094755Sbenno{ 181168885Sgrehan uint8_t temp; 182168885Sgrehan 183168885Sgrehan temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 184168885Sgrehan if (on) 185168885Sgrehan temp |= MUSB2_MASK_SOFTC; 186168885Sgrehan else 187227843Smarius temp &= ~MUSB2_MASK_SOFTC; 18894755Sbenno 18994755Sbenno MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 19094755Sbenno} 19194755Sbenno 19294755Sbennostatic void 19394755Sbennomusbotg_pull_up(struct musbotg_softc *sc) 19494755Sbenno{ 19594755Sbenno /* pullup D+, if possible */ 19694755Sbenno 19794755Sbenno if (!sc->sc_flags.d_pulled_up && 19894755Sbenno sc->sc_flags.port_powered) { 19994755Sbenno sc->sc_flags.d_pulled_up = 1; 20094755Sbenno musbotg_pull_common(sc, 1); 20194755Sbenno } 20294755Sbenno} 203178367Smarcel 204178367Smarcelstatic void 205178367Smarcelmusbotg_pull_down(struct musbotg_softc *sc) 206178367Smarcel{ 207178367Smarcel /* pulldown D+, if possible */ 208178367Smarcel 209178367Smarcel if (sc->sc_flags.d_pulled_up) { 210178367Smarcel sc->sc_flags.d_pulled_up = 0; 21194755Sbenno musbotg_pull_common(sc, 0); 212124468Sgrehan } 21394755Sbenno} 214171805Smarcel 21594755Sbennostatic void 21699652Sbennomusbotg_wakeup_peer(struct musbotg_softc *sc) 21799652Sbenno{ 218171805Smarcel uint8_t temp; 219209298Snwhitehorn 220171805Smarcel if (!(sc->sc_flags.status_suspend)) { 221171805Smarcel return; 222171805Smarcel } 223171805Smarcel 224171805Smarcel temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 225171805Smarcel temp |= MUSB2_MASK_RESUME; 226171805Smarcel MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 227171805Smarcel 228171805Smarcel /* wait 8 milliseconds */ 229215067Snwhitehorn /* Wait for reset to complete. */ 230215067Snwhitehorn usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125); 231215067Snwhitehorn 232124468Sgrehan temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 233124468Sgrehan temp &= ~MUSB2_MASK_RESUME; 234124468Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_POWER, temp); 235124468Sgrehan} 236124468Sgrehan 237124468Sgrehanstatic void 238124468Sgrehanmusbotg_set_address(struct musbotg_softc *sc, uint8_t addr) 23999652Sbenno{ 24094755Sbenno DPRINTFN(4, "addr=%d\n", addr); 241178367Smarcel addr &= 0x7F; 242178367Smarcel MUSB2_WRITE_1(sc, MUSB2_REG_FADDR, addr); 24394755Sbenno} 24494755Sbenno 24594755Sbennostatic uint8_t 24694755Sbennomusbotg_setup_rx(struct musbotg_td *td) 24794755Sbenno{ 24894755Sbenno struct musbotg_softc *sc; 24994755Sbenno struct usb_device_request req; 25094755Sbenno uint16_t count; 25194755Sbenno uint8_t csr; 25294755Sbenno 25394755Sbenno /* get pointer to softc */ 25494755Sbenno sc = MUSBOTG_PC2SC(td->pc); 25594755Sbenno 25694755Sbenno /* select endpoint 0 */ 25794755Sbenno MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 258124468Sgrehan 259124468Sgrehan /* read out FIFO status */ 260124468Sgrehan csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 261124468Sgrehan 26294755Sbenno DPRINTFN(4, "csr=0x%02x\n", csr); 26394755Sbenno 264124468Sgrehan /* 265212413Savg * NOTE: If DATAEND is set we should not call the 266124468Sgrehan * callback, hence the status stage is not complete. 267124468Sgrehan */ 268124468Sgrehan if (csr & MUSB2_MASK_CSR0L_DATAEND) { 269124468Sgrehan /* do not stall at this point */ 270124468Sgrehan td->did_stall = 1; 271125702Sgrehan /* wait for interrupt */ 272124468Sgrehan goto not_complete; 273125702Sgrehan } 274124468Sgrehan if (csr & MUSB2_MASK_CSR0L_SENTSTALL) { 275124468Sgrehan /* clear SENTSTALL */ 276124468Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 277124468Sgrehan /* get latest status */ 278124468Sgrehan csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 279124468Sgrehan /* update EP0 state */ 280125702Sgrehan sc->sc_ep0_busy = 0; 281125702Sgrehan } 282124468Sgrehan if (csr & MUSB2_MASK_CSR0L_SETUPEND) { 283124468Sgrehan /* clear SETUPEND */ 284124468Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 285124468Sgrehan MUSB2_MASK_CSR0L_SETUPEND_CLR); 28694755Sbenno /* get latest status */ 28794755Sbenno csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 28894755Sbenno /* update EP0 state */ 28994755Sbenno sc->sc_ep0_busy = 0; 29094755Sbenno } 29194755Sbenno if (sc->sc_ep0_busy) { 29294755Sbenno goto not_complete; 29394755Sbenno } 29494755Sbenno if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) { 29594755Sbenno goto not_complete; 29694755Sbenno } 29794755Sbenno /* clear did stall flag */ 29894755Sbenno td->did_stall = 0; 29994755Sbenno /* get the packet byte count */ 30094755Sbenno count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 30194755Sbenno 30294755Sbenno /* verify data length */ 30399652Sbenno if (count != td->remainder) { 30499652Sbenno DPRINTFN(0, "Invalid SETUP packet " 30599652Sbenno "length, %d bytes\n", count); 30694755Sbenno MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 30794755Sbenno MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 30894755Sbenno goto not_complete; 30994755Sbenno } 31094755Sbenno if (count != sizeof(req)) { 31194755Sbenno DPRINTFN(0, "Unsupported SETUP packet " 31294755Sbenno "length, %d bytes\n", count); 31394755Sbenno MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 31494755Sbenno MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 31594755Sbenno goto not_complete; 31694755Sbenno } 31794755Sbenno /* receive data */ 31894755Sbenno bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 31994755Sbenno MUSB2_REG_EPFIFO(0), (void *)&req, sizeof(req)); 32094755Sbenno 32194755Sbenno /* copy data into real buffer */ 322124468Sgrehan usbd_copy_in(td->pc, 0, &req, sizeof(req)); 323124468Sgrehan 324125434Sgrehan td->offset = sizeof(req); 325125434Sgrehan td->remainder = 0; 326125434Sgrehan 327125434Sgrehan /* set pending command */ 328125434Sgrehan sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR; 32994755Sbenno 330124468Sgrehan /* we need set stall or dataend after this */ 331124468Sgrehan sc->sc_ep0_busy = 1; 332124468Sgrehan 33394755Sbenno /* sneak peek the set address */ 33494755Sbenno if ((req.bmRequestType == UT_WRITE_DEVICE) && 33594755Sbenno (req.bRequest == UR_SET_ADDRESS)) { 33694755Sbenno sc->sc_dv_addr = req.wValue[0] & 0x7F; 33794755Sbenno } else { 33899652Sbenno sc->sc_dv_addr = 0xFF; 33999652Sbenno } 34099652Sbenno return (0); /* complete */ 341171805Smarcel 34299652Sbennonot_complete: 343171805Smarcel /* abort any ongoing transfer */ 34499652Sbenno if (!td->did_stall) { 345171805Smarcel DPRINTFN(4, "stalling\n"); 346171805Smarcel MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 347171805Smarcel MUSB2_MASK_CSR0L_SENDSTALL); 34899652Sbenno td->did_stall = 1; 349171805Smarcel } 350171805Smarcel return (1); /* not complete */ 351171805Smarcel} 35299652Sbenno 353171805Smarcel/* Control endpoint only data handling functions (RX/TX/SYNC) */ 354171805Smarcel 355171805Smarcelstatic uint8_t 356171805Smarcelmusbotg_setup_data_rx(struct musbotg_td *td) 357171805Smarcel{ 358171805Smarcel struct usb_page_search buf_res; 359171805Smarcel struct musbotg_softc *sc; 360171805Smarcel uint16_t count; 361171805Smarcel uint8_t csr; 362171805Smarcel uint8_t got_short; 363171805Smarcel 36499652Sbenno /* get pointer to softc */ 36599652Sbenno sc = MUSBOTG_PC2SC(td->pc); 36699652Sbenno 36799652Sbenno /* select endpoint 0 */ 368171805Smarcel MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 36999652Sbenno 37099652Sbenno /* check if a command is pending */ 371171805Smarcel if (sc->sc_ep0_cmd) { 37299652Sbenno MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 37399652Sbenno sc->sc_ep0_cmd = 0; 374209486Snwhitehorn } 375209486Snwhitehorn /* read out FIFO status */ 376209486Snwhitehorn csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 377209486Snwhitehorn 378209486Snwhitehorn DPRINTFN(4, "csr=0x%02x\n", csr); 379209486Snwhitehorn 380209486Snwhitehorn got_short = 0; 381209486Snwhitehorn 382209486Snwhitehorn if (csr & (MUSB2_MASK_CSR0L_SETUPEND | 383209486Snwhitehorn MUSB2_MASK_CSR0L_SENTSTALL)) { 384209486Snwhitehorn if (td->remainder == 0) { 385209486Snwhitehorn /* 386209486Snwhitehorn * We are actually complete and have 387209486Snwhitehorn * received the next SETUP 388209486Snwhitehorn */ 389209486Snwhitehorn DPRINTFN(4, "faking complete\n"); 390209486Snwhitehorn return (0); /* complete */ 39199652Sbenno } 39299652Sbenno /* 39399652Sbenno * USB Host Aborted the transfer. 39499652Sbenno */ 39599652Sbenno td->error = 1; 39699652Sbenno return (0); /* complete */ 39799652Sbenno } 39899652Sbenno if (!(csr & MUSB2_MASK_CSR0L_RXPKTRDY)) { 399171805Smarcel return (1); /* not complete */ 400171805Smarcel } 40199652Sbenno /* get the packet byte count */ 40299652Sbenno count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 40399652Sbenno 40499652Sbenno /* verify the packet byte count */ 40599652Sbenno if (count != td->max_frame_size) { 40699652Sbenno if (count < td->max_frame_size) { 40799652Sbenno /* we have a short packet */ 408171805Smarcel td->short_pkt = 1; 409171805Smarcel got_short = 1; 410171805Smarcel } else { 411171805Smarcel /* invalid USB packet */ 412171805Smarcel td->error = 1; 41399652Sbenno return (0); /* we are complete */ 414171805Smarcel } 41599652Sbenno } 416171805Smarcel /* verify the packet byte count */ 417171805Smarcel if (count > td->remainder) { 418171805Smarcel /* invalid USB packet */ 419171805Smarcel td->error = 1; 420171805Smarcel return (0); /* we are complete */ 421171805Smarcel } 422171805Smarcel while (count > 0) { 423171805Smarcel uint32_t temp; 42499652Sbenno 42599652Sbenno usbd_get_page(td->pc, td->offset, &buf_res); 42699652Sbenno 42799652Sbenno /* get correct length */ 42899652Sbenno if (buf_res.length > count) { 42999652Sbenno buf_res.length = count; 43099652Sbenno } 43199652Sbenno /* check for unaligned memory address */ 43299652Sbenno if (USB_P2U(buf_res.buffer) & 3) { 43399652Sbenno 43499652Sbenno temp = count & ~3; 43599652Sbenno 43699652Sbenno if (temp) { 43799652Sbenno /* receive data 4 bytes at a time */ 43899652Sbenno bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 43999652Sbenno MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf, 44099652Sbenno temp / 4); 44199652Sbenno } 44299652Sbenno temp = count & 3; 44399652Sbenno if (temp) { 44499652Sbenno /* receive data 1 byte at a time */ 44599652Sbenno bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 44699652Sbenno MUSB2_REG_EPFIFO(0), 44799652Sbenno (void *)(&sc->sc_bounce_buf[count / 4]), temp); 44899652Sbenno } 44999652Sbenno usbd_copy_in(td->pc, td->offset, 45099652Sbenno sc->sc_bounce_buf, count); 45199652Sbenno 45299652Sbenno /* update offset and remainder */ 453124468Sgrehan td->offset += count; 45499652Sbenno td->remainder -= count; 45599652Sbenno break; 456171805Smarcel } 45799652Sbenno /* check if we can optimise */ 45899652Sbenno if (buf_res.length >= 4) { 45999652Sbenno 460124468Sgrehan /* receive data 4 bytes at a time */ 46199652Sbenno bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 46299652Sbenno MUSB2_REG_EPFIFO(0), buf_res.buffer, 46399652Sbenno buf_res.length / 4); 46499652Sbenno 46599652Sbenno temp = buf_res.length & ~3; 46699652Sbenno 46799652Sbenno /* update counters */ 46899652Sbenno count -= temp; 46999652Sbenno td->offset += temp; 47099652Sbenno td->remainder -= temp; 471111119Simp continue; 47299652Sbenno } 47399652Sbenno /* receive data */ 47499652Sbenno bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 47599652Sbenno MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length); 47699652Sbenno 47799652Sbenno /* update counters */ 47899652Sbenno count -= buf_res.length; 47999652Sbenno td->offset += buf_res.length; 48099652Sbenno td->remainder -= buf_res.length; 48199652Sbenno } 482124468Sgrehan 483168885Sgrehan /* check if we are complete */ 484168885Sgrehan if ((td->remainder == 0) || got_short) { 485168885Sgrehan if (td->short_pkt) { 486168885Sgrehan /* we are complete */ 487168885Sgrehan sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_RXPKTRDY_CLR; 488168885Sgrehan return (0); 489168885Sgrehan } 490168885Sgrehan /* else need to receive a zero length packet */ 491168885Sgrehan } 492168885Sgrehan /* write command - need more data */ 493168885Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 494168885Sgrehan MUSB2_MASK_CSR0L_RXPKTRDY_CLR); 495168885Sgrehan return (1); /* not complete */ 496168885Sgrehan} 497168885Sgrehan 498168885Sgrehanstatic uint8_t 499168885Sgrehanmusbotg_setup_data_tx(struct musbotg_td *td) 500168885Sgrehan{ 501168885Sgrehan struct usb_page_search buf_res; 502168885Sgrehan struct musbotg_softc *sc; 503168885Sgrehan uint16_t count; 504168885Sgrehan uint8_t csr; 505168885Sgrehan 506168885Sgrehan /* get pointer to softc */ 507168885Sgrehan sc = MUSBOTG_PC2SC(td->pc); 508168885Sgrehan 509168885Sgrehan /* select endpoint 0 */ 510168885Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 511168885Sgrehan 512168885Sgrehan /* check if a command is pending */ 513168885Sgrehan if (sc->sc_ep0_cmd) { 514168885Sgrehan MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 515168885Sgrehan sc->sc_ep0_cmd = 0; 516168885Sgrehan } 517168885Sgrehan /* read out FIFO status */ 518168885Sgrehan csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 519168885Sgrehan 520168885Sgrehan DPRINTFN(4, "csr=0x%02x\n", csr); 521168885Sgrehan 522168885Sgrehan if (csr & (MUSB2_MASK_CSR0L_SETUPEND | 523168885Sgrehan MUSB2_MASK_CSR0L_SENTSTALL)) { 524168885Sgrehan /* 525168885Sgrehan * The current transfer was aborted 526178367Smarcel * by the USB Host 527 */ 528 td->error = 1; 529 return (0); /* complete */ 530 } 531 if (csr & MUSB2_MASK_CSR0L_TXPKTRDY) { 532 return (1); /* not complete */ 533 } 534 count = td->max_frame_size; 535 if (td->remainder < count) { 536 /* we have a short packet */ 537 td->short_pkt = 1; 538 count = td->remainder; 539 } 540 while (count > 0) { 541 uint32_t temp; 542 543 usbd_get_page(td->pc, td->offset, &buf_res); 544 545 /* get correct length */ 546 if (buf_res.length > count) { 547 buf_res.length = count; 548 } 549 /* check for unaligned memory address */ 550 if (USB_P2U(buf_res.buffer) & 3) { 551 552 usbd_copy_out(td->pc, td->offset, 553 sc->sc_bounce_buf, count); 554 555 temp = count & ~3; 556 557 if (temp) { 558 /* transmit data 4 bytes at a time */ 559 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 560 MUSB2_REG_EPFIFO(0), sc->sc_bounce_buf, 561 temp / 4); 562 } 563 temp = count & 3; 564 if (temp) { 565 /* receive data 1 byte at a time */ 566 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 567 MUSB2_REG_EPFIFO(0), 568 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 569 } 570 /* update offset and remainder */ 571 td->offset += count; 572 td->remainder -= count; 573 break; 574 } 575 /* check if we can optimise */ 576 if (buf_res.length >= 4) { 577 578 /* transmit data 4 bytes at a time */ 579 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 580 MUSB2_REG_EPFIFO(0), buf_res.buffer, 581 buf_res.length / 4); 582 583 temp = buf_res.length & ~3; 584 585 /* update counters */ 586 count -= temp; 587 td->offset += temp; 588 td->remainder -= temp; 589 continue; 590 } 591 /* transmit data */ 592 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 593 MUSB2_REG_EPFIFO(0), buf_res.buffer, buf_res.length); 594 595 /* update counters */ 596 count -= buf_res.length; 597 td->offset += buf_res.length; 598 td->remainder -= buf_res.length; 599 } 600 601 /* check remainder */ 602 if (td->remainder == 0) { 603 if (td->short_pkt) { 604 sc->sc_ep0_cmd = MUSB2_MASK_CSR0L_TXPKTRDY; 605 return (0); /* complete */ 606 } 607 /* else we need to transmit a short packet */ 608 } 609 /* write command */ 610 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 611 MUSB2_MASK_CSR0L_TXPKTRDY); 612 613 return (1); /* not complete */ 614} 615 616static uint8_t 617musbotg_setup_status(struct musbotg_td *td) 618{ 619 struct musbotg_softc *sc; 620 uint8_t csr; 621 622 /* get pointer to softc */ 623 sc = MUSBOTG_PC2SC(td->pc); 624 625 /* select endpoint 0 */ 626 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 627 628 if (sc->sc_ep0_busy) { 629 sc->sc_ep0_busy = 0; 630 sc->sc_ep0_cmd |= MUSB2_MASK_CSR0L_DATAEND; 631 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, sc->sc_ep0_cmd); 632 sc->sc_ep0_cmd = 0; 633 } 634 /* read out FIFO status */ 635 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 636 637 DPRINTFN(4, "csr=0x%02x\n", csr); 638 639 if (csr & MUSB2_MASK_CSR0L_DATAEND) { 640 /* wait for interrupt */ 641 return (1); /* not complete */ 642 } 643 if (sc->sc_dv_addr != 0xFF) { 644 /* write function address */ 645 musbotg_set_address(sc, sc->sc_dv_addr); 646 } 647 return (0); /* complete */ 648} 649 650static uint8_t 651musbotg_data_rx(struct musbotg_td *td) 652{ 653 struct usb_page_search buf_res; 654 struct musbotg_softc *sc; 655 uint16_t count; 656 uint8_t csr; 657 uint8_t to; 658 uint8_t got_short; 659 660 to = 8; /* don't loop forever! */ 661 got_short = 0; 662 663 /* get pointer to softc */ 664 sc = MUSBOTG_PC2SC(td->pc); 665 666 /* select endpoint */ 667 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->ep_no); 668 669repeat: 670 /* read out FIFO status */ 671 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 672 673 DPRINTFN(4, "csr=0x%02x\n", csr); 674 675 /* clear overrun */ 676 if (csr & MUSB2_MASK_CSRL_RXOVERRUN) { 677 /* make sure we don't clear "RXPKTRDY" */ 678 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 679 MUSB2_MASK_CSRL_RXPKTRDY); 680 } 681 /* check status */ 682 if (!(csr & MUSB2_MASK_CSRL_RXPKTRDY)) { 683 return (1); /* not complete */ 684 } 685 /* get the packet byte count */ 686 count = MUSB2_READ_2(sc, MUSB2_REG_RXCOUNT); 687 688 DPRINTFN(4, "count=0x%04x\n", count); 689 690 /* 691 * Check for short or invalid packet: 692 */ 693 if (count != td->max_frame_size) { 694 if (count < td->max_frame_size) { 695 /* we have a short packet */ 696 td->short_pkt = 1; 697 got_short = 1; 698 } else { 699 /* invalid USB packet */ 700 td->error = 1; 701 return (0); /* we are complete */ 702 } 703 } 704 /* verify the packet byte count */ 705 if (count > td->remainder) { 706 /* invalid USB packet */ 707 td->error = 1; 708 return (0); /* we are complete */ 709 } 710 while (count > 0) { 711 uint32_t temp; 712 713 usbd_get_page(td->pc, td->offset, &buf_res); 714 715 /* get correct length */ 716 if (buf_res.length > count) { 717 buf_res.length = count; 718 } 719 /* check for unaligned memory address */ 720 if (USB_P2U(buf_res.buffer) & 3) { 721 722 temp = count & ~3; 723 724 if (temp) { 725 /* receive data 4 bytes at a time */ 726 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 727 MUSB2_REG_EPFIFO(td->ep_no), sc->sc_bounce_buf, 728 temp / 4); 729 } 730 temp = count & 3; 731 if (temp) { 732 /* receive data 1 byte at a time */ 733 bus_space_read_multi_1(sc->sc_io_tag, 734 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->ep_no), 735 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 736 } 737 usbd_copy_in(td->pc, td->offset, 738 sc->sc_bounce_buf, count); 739 740 /* update offset and remainder */ 741 td->offset += count; 742 td->remainder -= count; 743 break; 744 } 745 /* check if we can optimise */ 746 if (buf_res.length >= 4) { 747 748 /* receive data 4 bytes at a time */ 749 bus_space_read_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 750 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 751 buf_res.length / 4); 752 753 temp = buf_res.length & ~3; 754 755 /* update counters */ 756 count -= temp; 757 td->offset += temp; 758 td->remainder -= temp; 759 continue; 760 } 761 /* receive data */ 762 bus_space_read_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 763 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 764 buf_res.length); 765 766 /* update counters */ 767 count -= buf_res.length; 768 td->offset += buf_res.length; 769 td->remainder -= buf_res.length; 770 } 771 772 /* clear status bits */ 773 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 774 775 /* check if we are complete */ 776 if ((td->remainder == 0) || got_short) { 777 if (td->short_pkt) { 778 /* we are complete */ 779 return (0); 780 } 781 /* else need to receive a zero length packet */ 782 } 783 if (--to) { 784 goto repeat; 785 } 786 return (1); /* not complete */ 787} 788 789static uint8_t 790musbotg_data_tx(struct musbotg_td *td) 791{ 792 struct usb_page_search buf_res; 793 struct musbotg_softc *sc; 794 uint16_t count; 795 uint8_t csr; 796 uint8_t to; 797 798 to = 8; /* don't loop forever! */ 799 800 /* get pointer to softc */ 801 sc = MUSBOTG_PC2SC(td->pc); 802 803 /* select endpoint */ 804 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, td->ep_no); 805 806repeat: 807 808 /* read out FIFO status */ 809 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 810 811 DPRINTFN(4, "csr=0x%02x\n", csr); 812 813 if (csr & (MUSB2_MASK_CSRL_TXINCOMP | 814 MUSB2_MASK_CSRL_TXUNDERRUN)) { 815 /* clear status bits */ 816 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 817 } 818 if (csr & MUSB2_MASK_CSRL_TXPKTRDY) { 819 return (1); /* not complete */ 820 } 821 /* check for short packet */ 822 count = td->max_frame_size; 823 if (td->remainder < count) { 824 /* we have a short packet */ 825 td->short_pkt = 1; 826 count = td->remainder; 827 } 828 while (count > 0) { 829 uint32_t temp; 830 831 usbd_get_page(td->pc, td->offset, &buf_res); 832 833 /* get correct length */ 834 if (buf_res.length > count) { 835 buf_res.length = count; 836 } 837 /* check for unaligned memory address */ 838 if (USB_P2U(buf_res.buffer) & 3) { 839 840 usbd_copy_out(td->pc, td->offset, 841 sc->sc_bounce_buf, count); 842 843 temp = count & ~3; 844 845 if (temp) { 846 /* transmit data 4 bytes at a time */ 847 bus_space_write_multi_4(sc->sc_io_tag, 848 sc->sc_io_hdl, MUSB2_REG_EPFIFO(td->ep_no), 849 sc->sc_bounce_buf, temp / 4); 850 } 851 temp = count & 3; 852 if (temp) { 853 /* receive data 1 byte at a time */ 854 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 855 MUSB2_REG_EPFIFO(td->ep_no), 856 ((void *)&sc->sc_bounce_buf[count / 4]), temp); 857 } 858 /* update offset and remainder */ 859 td->offset += count; 860 td->remainder -= count; 861 break; 862 } 863 /* check if we can optimise */ 864 if (buf_res.length >= 4) { 865 866 /* transmit data 4 bytes at a time */ 867 bus_space_write_multi_4(sc->sc_io_tag, sc->sc_io_hdl, 868 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 869 buf_res.length / 4); 870 871 temp = buf_res.length & ~3; 872 873 /* update counters */ 874 count -= temp; 875 td->offset += temp; 876 td->remainder -= temp; 877 continue; 878 } 879 /* transmit data */ 880 bus_space_write_multi_1(sc->sc_io_tag, sc->sc_io_hdl, 881 MUSB2_REG_EPFIFO(td->ep_no), buf_res.buffer, 882 buf_res.length); 883 884 /* update counters */ 885 count -= buf_res.length; 886 td->offset += buf_res.length; 887 td->remainder -= buf_res.length; 888 } 889 890 /* write command */ 891 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 892 MUSB2_MASK_CSRL_TXPKTRDY); 893 894 /* check remainder */ 895 if (td->remainder == 0) { 896 if (td->short_pkt) { 897 return (0); /* complete */ 898 } 899 /* else we need to transmit a short packet */ 900 } 901 if (--to) { 902 goto repeat; 903 } 904 return (1); /* not complete */ 905} 906 907static uint8_t 908musbotg_xfer_do_fifo(struct usb_xfer *xfer) 909{ 910 struct musbotg_softc *sc; 911 struct musbotg_td *td; 912 913 DPRINTFN(8, "\n"); 914 915 td = xfer->td_transfer_cache; 916 while (1) { 917 if ((td->func) (td)) { 918 /* operation in progress */ 919 break; 920 } 921 if (((void *)td) == xfer->td_transfer_last) { 922 goto done; 923 } 924 if (td->error) { 925 goto done; 926 } else if (td->remainder > 0) { 927 /* 928 * We had a short transfer. If there is no alternate 929 * next, stop processing ! 930 */ 931 if (!td->alt_next) { 932 goto done; 933 } 934 } 935 /* 936 * Fetch the next transfer descriptor and transfer 937 * some flags to the next transfer descriptor 938 */ 939 td = td->obj_next; 940 xfer->td_transfer_cache = td; 941 } 942 return (1); /* not complete */ 943 944done: 945 sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 946 947 /* compute all actual lengths */ 948 949 musbotg_standard_done(xfer); 950 951 return (0); /* complete */ 952} 953 954static void 955musbotg_interrupt_poll(struct musbotg_softc *sc) 956{ 957 struct usb_xfer *xfer; 958 959repeat: 960 TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) { 961 if (!musbotg_xfer_do_fifo(xfer)) { 962 /* queue has been modified */ 963 goto repeat; 964 } 965 } 966} 967 968void 969musbotg_vbus_interrupt(struct musbotg_softc *sc, uint8_t is_on) 970{ 971 DPRINTFN(4, "vbus = %u\n", is_on); 972 973 USB_BUS_LOCK(&sc->sc_bus); 974 if (is_on) { 975 if (!sc->sc_flags.status_vbus) { 976 sc->sc_flags.status_vbus = 1; 977 978 /* complete root HUB interrupt endpoint */ 979 musbotg_root_intr(sc); 980 } 981 } else { 982 if (sc->sc_flags.status_vbus) { 983 sc->sc_flags.status_vbus = 0; 984 sc->sc_flags.status_bus_reset = 0; 985 sc->sc_flags.status_suspend = 0; 986 sc->sc_flags.change_suspend = 0; 987 sc->sc_flags.change_connect = 1; 988 989 /* complete root HUB interrupt endpoint */ 990 musbotg_root_intr(sc); 991 } 992 } 993 994 USB_BUS_UNLOCK(&sc->sc_bus); 995} 996 997void 998musbotg_interrupt(struct musbotg_softc *sc) 999{ 1000 uint16_t rx_status; 1001 uint16_t tx_status; 1002 uint8_t usb_status; 1003 uint8_t temp; 1004 uint8_t to = 2; 1005 1006 USB_BUS_LOCK(&sc->sc_bus); 1007 1008repeat: 1009 1010 /* read all interrupt registers */ 1011 usb_status = MUSB2_READ_1(sc, MUSB2_REG_INTUSB); 1012 1013 /* read all FIFO interrupts */ 1014 rx_status = MUSB2_READ_2(sc, MUSB2_REG_INTRX); 1015 tx_status = MUSB2_READ_2(sc, MUSB2_REG_INTTX); 1016 1017 /* check for any bus state change interrupts */ 1018 1019 if (usb_status & (MUSB2_MASK_IRESET | 1020 MUSB2_MASK_IRESUME | MUSB2_MASK_ISUSP)) { 1021 1022 DPRINTFN(4, "real bus interrupt 0x%08x\n", usb_status); 1023 1024 if (usb_status & MUSB2_MASK_IRESET) { 1025 1026 /* set correct state */ 1027 sc->sc_flags.status_bus_reset = 1; 1028 sc->sc_flags.status_suspend = 0; 1029 sc->sc_flags.change_suspend = 0; 1030 sc->sc_flags.change_connect = 1; 1031 1032 /* determine line speed */ 1033 temp = MUSB2_READ_1(sc, MUSB2_REG_POWER); 1034 if (temp & MUSB2_MASK_HSMODE) 1035 sc->sc_flags.status_high_speed = 1; 1036 else 1037 sc->sc_flags.status_high_speed = 0; 1038 1039 /* 1040 * After reset all interrupts are on and we need to 1041 * turn them off! 1042 */ 1043 temp = MUSB2_MASK_IRESET; 1044 /* disable resume interrupt */ 1045 temp &= ~MUSB2_MASK_IRESUME; 1046 /* enable suspend interrupt */ 1047 temp |= MUSB2_MASK_ISUSP; 1048 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1049 /* disable TX and RX interrupts */ 1050 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1051 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1052 } 1053 /* 1054 * If RXRSM and RXSUSP is set at the same time we interpret 1055 * that like RESUME. Resume is set when there is at least 3 1056 * milliseconds of inactivity on the USB BUS. 1057 */ 1058 if (usb_status & MUSB2_MASK_IRESUME) { 1059 if (sc->sc_flags.status_suspend) { 1060 sc->sc_flags.status_suspend = 0; 1061 sc->sc_flags.change_suspend = 1; 1062 1063 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE); 1064 /* disable resume interrupt */ 1065 temp &= ~MUSB2_MASK_IRESUME; 1066 /* enable suspend interrupt */ 1067 temp |= MUSB2_MASK_ISUSP; 1068 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1069 } 1070 } else if (usb_status & MUSB2_MASK_ISUSP) { 1071 if (!sc->sc_flags.status_suspend) { 1072 sc->sc_flags.status_suspend = 1; 1073 sc->sc_flags.change_suspend = 1; 1074 1075 temp = MUSB2_READ_1(sc, MUSB2_REG_INTUSBE); 1076 /* disable suspend interrupt */ 1077 temp &= ~MUSB2_MASK_ISUSP; 1078 /* enable resume interrupt */ 1079 temp |= MUSB2_MASK_IRESUME; 1080 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, temp); 1081 } 1082 } 1083 /* complete root HUB interrupt endpoint */ 1084 musbotg_root_intr(sc); 1085 } 1086 /* check for any endpoint interrupts */ 1087 1088 if (rx_status || tx_status) { 1089 DPRINTFN(4, "real endpoint interrupt " 1090 "rx=0x%04x, tx=0x%04x\n", rx_status, tx_status); 1091 } 1092 /* poll one time regardless of FIFO status */ 1093 1094 musbotg_interrupt_poll(sc); 1095 1096 if (--to) 1097 goto repeat; 1098 1099 USB_BUS_UNLOCK(&sc->sc_bus); 1100} 1101 1102static void 1103musbotg_setup_standard_chain_sub(struct musbotg_std_temp *temp) 1104{ 1105 struct musbotg_td *td; 1106 1107 /* get current Transfer Descriptor */ 1108 td = temp->td_next; 1109 temp->td = td; 1110 1111 /* prepare for next TD */ 1112 temp->td_next = td->obj_next; 1113 1114 /* fill out the Transfer Descriptor */ 1115 td->func = temp->func; 1116 td->pc = temp->pc; 1117 td->offset = temp->offset; 1118 td->remainder = temp->len; 1119 td->error = 0; 1120 td->did_stall = temp->did_stall; 1121 td->short_pkt = temp->short_pkt; 1122 td->alt_next = temp->setup_alt_next; 1123} 1124 1125static void 1126musbotg_setup_standard_chain(struct usb_xfer *xfer) 1127{ 1128 struct musbotg_std_temp temp; 1129 struct musbotg_softc *sc; 1130 struct musbotg_td *td; 1131 uint32_t x; 1132 uint8_t ep_no; 1133 1134 DPRINTFN(8, "addr=%d endpt=%d sumlen=%d speed=%d\n", 1135 xfer->address, UE_GET_ADDR(xfer->endpointno), 1136 xfer->sumlen, usbd_get_speed(xfer->xroot->udev)); 1137 1138 temp.max_frame_size = xfer->max_frame_size; 1139 1140 td = xfer->td_start[0]; 1141 xfer->td_transfer_first = td; 1142 xfer->td_transfer_cache = td; 1143 1144 /* setup temp */ 1145 1146 temp.pc = NULL; 1147 temp.td = NULL; 1148 temp.td_next = xfer->td_start[0]; 1149 temp.offset = 0; 1150 temp.setup_alt_next = xfer->flags_int.short_frames_ok; 1151 temp.did_stall = !xfer->flags_int.control_stall; 1152 1153 sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 1154 ep_no = (xfer->endpointno & UE_ADDR); 1155 1156 /* check if we should prepend a setup message */ 1157 1158 if (xfer->flags_int.control_xfr) { 1159 if (xfer->flags_int.control_hdr) { 1160 1161 temp.func = &musbotg_setup_rx; 1162 temp.len = xfer->frlengths[0]; 1163 temp.pc = xfer->frbuffers + 0; 1164 temp.short_pkt = temp.len ? 1 : 0; 1165 1166 musbotg_setup_standard_chain_sub(&temp); 1167 } 1168 x = 1; 1169 } else { 1170 x = 0; 1171 } 1172 1173 if (x != xfer->nframes) { 1174 if (xfer->endpointno & UE_DIR_IN) { 1175 if (xfer->flags_int.control_xfr) 1176 temp.func = &musbotg_setup_data_tx; 1177 else 1178 temp.func = &musbotg_data_tx; 1179 } else { 1180 if (xfer->flags_int.control_xfr) 1181 temp.func = &musbotg_setup_data_rx; 1182 else 1183 temp.func = &musbotg_data_rx; 1184 } 1185 1186 /* setup "pc" pointer */ 1187 temp.pc = xfer->frbuffers + x; 1188 } 1189 while (x != xfer->nframes) { 1190 1191 /* DATA0 / DATA1 message */ 1192 1193 temp.len = xfer->frlengths[x]; 1194 1195 x++; 1196 1197 if (x == xfer->nframes) { 1198 if (xfer->flags_int.control_xfr) { 1199 if (xfer->flags_int.control_act) { 1200 temp.setup_alt_next = 0; 1201 } 1202 } else { 1203 temp.setup_alt_next = 0; 1204 } 1205 } 1206 if (temp.len == 0) { 1207 1208 /* make sure that we send an USB packet */ 1209 1210 temp.short_pkt = 0; 1211 1212 } else { 1213 1214 /* regular data transfer */ 1215 1216 temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1; 1217 } 1218 1219 musbotg_setup_standard_chain_sub(&temp); 1220 1221 if (xfer->flags_int.isochronous_xfr) { 1222 temp.offset += temp.len; 1223 } else { 1224 /* get next Page Cache pointer */ 1225 temp.pc = xfer->frbuffers + x; 1226 } 1227 } 1228 1229 /* check for control transfer */ 1230 if (xfer->flags_int.control_xfr) { 1231 1232 /* always setup a valid "pc" pointer for status and sync */ 1233 temp.pc = xfer->frbuffers + 0; 1234 temp.len = 0; 1235 temp.short_pkt = 0; 1236 temp.setup_alt_next = 0; 1237 1238 /* check if we should append a status stage */ 1239 if (!xfer->flags_int.control_act) { 1240 /* 1241 * Send a DATA1 message and invert the current 1242 * endpoint direction. 1243 */ 1244 temp.func = &musbotg_setup_status; 1245 musbotg_setup_standard_chain_sub(&temp); 1246 } 1247 } 1248 /* must have at least one frame! */ 1249 td = temp.td; 1250 xfer->td_transfer_last = td; 1251} 1252 1253static void 1254musbotg_timeout(void *arg) 1255{ 1256 struct usb_xfer *xfer = arg; 1257 1258 DPRINTFN(1, "xfer=%p\n", xfer); 1259 1260 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1261 1262 /* transfer is transferred */ 1263 musbotg_device_done(xfer, USB_ERR_TIMEOUT); 1264} 1265 1266static void 1267musbotg_ep_int_set(struct usb_xfer *xfer, uint8_t on) 1268{ 1269 struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 1270 uint16_t temp; 1271 uint8_t ep_no = xfer->endpointno & UE_ADDR; 1272 1273 /* 1274 * Only enable the endpoint interrupt when we are 1275 * actually waiting for data, hence we are dealing 1276 * with level triggered interrupts ! 1277 */ 1278 if (ep_no == 0) { 1279 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE); 1280 if (on) 1281 temp |= MUSB2_MASK_EPINT(0); 1282 else 1283 temp &= ~MUSB2_MASK_EPINT(0); 1284 1285 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp); 1286 } else { 1287 if (USB_GET_DATA_ISREAD(xfer)) { 1288 temp = MUSB2_READ_2(sc, MUSB2_REG_INTRXE); 1289 if (on) 1290 temp |= MUSB2_MASK_EPINT(ep_no); 1291 else 1292 temp &= ~MUSB2_MASK_EPINT(ep_no); 1293 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, temp); 1294 1295 } else { 1296 temp = MUSB2_READ_2(sc, MUSB2_REG_INTTXE); 1297 if (on) 1298 temp |= MUSB2_MASK_EPINT(ep_no); 1299 else 1300 temp &= ~MUSB2_MASK_EPINT(ep_no); 1301 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, temp); 1302 } 1303 } 1304} 1305 1306static void 1307musbotg_start_standard_chain(struct usb_xfer *xfer) 1308{ 1309 DPRINTFN(8, "\n"); 1310 1311 /* poll one time */ 1312 if (musbotg_xfer_do_fifo(xfer)) { 1313 1314 musbotg_ep_int_set(xfer, 1); 1315 1316 DPRINTFN(14, "enabled interrupts on endpoint\n"); 1317 1318 /* put transfer on interrupt queue */ 1319 usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer); 1320 1321 /* start timeout, if any */ 1322 if (xfer->timeout != 0) { 1323 usbd_transfer_timeout_ms(xfer, 1324 &musbotg_timeout, xfer->timeout); 1325 } 1326 } 1327} 1328 1329static void 1330musbotg_root_intr(struct musbotg_softc *sc) 1331{ 1332 DPRINTFN(8, "\n"); 1333 1334 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 1335 1336 /* set port bit */ 1337 sc->sc_hub_idata[0] = 0x02; /* we only have one port */ 1338 1339 uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata, 1340 sizeof(sc->sc_hub_idata)); 1341} 1342 1343static usb_error_t 1344musbotg_standard_done_sub(struct usb_xfer *xfer) 1345{ 1346 struct musbotg_td *td; 1347 uint32_t len; 1348 uint8_t error; 1349 1350 DPRINTFN(8, "\n"); 1351 1352 td = xfer->td_transfer_cache; 1353 1354 do { 1355 len = td->remainder; 1356 1357 if (xfer->aframes != xfer->nframes) { 1358 /* 1359 * Verify the length and subtract 1360 * the remainder from "frlengths[]": 1361 */ 1362 if (len > xfer->frlengths[xfer->aframes]) { 1363 td->error = 1; 1364 } else { 1365 xfer->frlengths[xfer->aframes] -= len; 1366 } 1367 } 1368 /* Check for transfer error */ 1369 if (td->error) { 1370 /* the transfer is finished */ 1371 error = 1; 1372 td = NULL; 1373 break; 1374 } 1375 /* Check for short transfer */ 1376 if (len > 0) { 1377 if (xfer->flags_int.short_frames_ok) { 1378 /* follow alt next */ 1379 if (td->alt_next) { 1380 td = td->obj_next; 1381 } else { 1382 td = NULL; 1383 } 1384 } else { 1385 /* the transfer is finished */ 1386 td = NULL; 1387 } 1388 error = 0; 1389 break; 1390 } 1391 td = td->obj_next; 1392 1393 /* this USB frame is complete */ 1394 error = 0; 1395 break; 1396 1397 } while (0); 1398 1399 /* update transfer cache */ 1400 1401 xfer->td_transfer_cache = td; 1402 1403 return (error ? 1404 USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION); 1405} 1406 1407static void 1408musbotg_standard_done(struct usb_xfer *xfer) 1409{ 1410 usb_error_t err = 0; 1411 1412 DPRINTFN(12, "xfer=%p endpoint=%p transfer done\n", 1413 xfer, xfer->endpoint); 1414 1415 /* reset scanner */ 1416 1417 xfer->td_transfer_cache = xfer->td_transfer_first; 1418 1419 if (xfer->flags_int.control_xfr) { 1420 1421 if (xfer->flags_int.control_hdr) { 1422 1423 err = musbotg_standard_done_sub(xfer); 1424 } 1425 xfer->aframes = 1; 1426 1427 if (xfer->td_transfer_cache == NULL) { 1428 goto done; 1429 } 1430 } 1431 while (xfer->aframes != xfer->nframes) { 1432 1433 err = musbotg_standard_done_sub(xfer); 1434 xfer->aframes++; 1435 1436 if (xfer->td_transfer_cache == NULL) { 1437 goto done; 1438 } 1439 } 1440 1441 if (xfer->flags_int.control_xfr && 1442 !xfer->flags_int.control_act) { 1443 1444 err = musbotg_standard_done_sub(xfer); 1445 } 1446done: 1447 musbotg_device_done(xfer, err); 1448} 1449 1450/*------------------------------------------------------------------------* 1451 * musbotg_device_done 1452 * 1453 * NOTE: this function can be called more than one time on the 1454 * same USB transfer! 1455 *------------------------------------------------------------------------*/ 1456static void 1457musbotg_device_done(struct usb_xfer *xfer, usb_error_t error) 1458{ 1459 USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED); 1460 1461 DPRINTFN(2, "xfer=%p, endpoint=%p, error=%d\n", 1462 xfer, xfer->endpoint, error); 1463 1464 if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) { 1465 1466 musbotg_ep_int_set(xfer, 0); 1467 1468 DPRINTFN(14, "disabled interrupts on endpoint\n"); 1469 } 1470 /* dequeue transfer and start next transfer */ 1471 usbd_transfer_done(xfer, error); 1472} 1473 1474static void 1475musbotg_set_stall(struct usb_device *udev, struct usb_xfer *xfer, 1476 struct usb_endpoint *ep, uint8_t *did_stall) 1477{ 1478 struct musbotg_softc *sc; 1479 uint8_t ep_no; 1480 1481 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1482 1483 DPRINTFN(4, "endpoint=%p\n", ep); 1484 1485 if (xfer) { 1486 /* cancel any ongoing transfers */ 1487 musbotg_device_done(xfer, USB_ERR_STALLED); 1488 } 1489 /* set FORCESTALL */ 1490 sc = MUSBOTG_BUS2SC(udev->bus); 1491 1492 ep_no = (ep->edesc->bEndpointAddress & UE_ADDR); 1493 1494 /* select endpoint */ 1495 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no); 1496 1497 if (ep->edesc->bEndpointAddress & UE_DIR_IN) { 1498 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1499 MUSB2_MASK_CSRL_TXSENDSTALL); 1500 } else { 1501 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1502 MUSB2_MASK_CSRL_RXSENDSTALL); 1503 } 1504} 1505 1506static void 1507musbotg_clear_stall_sub(struct musbotg_softc *sc, uint16_t wMaxPacket, 1508 uint8_t ep_no, uint8_t ep_type, uint8_t ep_dir) 1509{ 1510 uint16_t mps; 1511 uint16_t temp; 1512 uint8_t csr; 1513 1514 if (ep_type == UE_CONTROL) { 1515 /* clearing stall is not needed */ 1516 return; 1517 } 1518 /* select endpoint */ 1519 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, ep_no); 1520 1521 /* compute max frame size */ 1522 mps = wMaxPacket & 0x7FF; 1523 switch ((wMaxPacket >> 11) & 3) { 1524 case 1: 1525 mps *= 2; 1526 break; 1527 case 2: 1528 mps *= 3; 1529 break; 1530 default: 1531 break; 1532 } 1533 1534 if (ep_dir == UE_DIR_IN) { 1535 1536 temp = 0; 1537 1538 /* Configure endpoint */ 1539 switch (ep_type) { 1540 case UE_INTERRUPT: 1541 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1542 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1543 MUSB2_MASK_CSRH_TXMODE | temp); 1544 break; 1545 case UE_ISOCHRONOUS: 1546 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1547 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1548 MUSB2_MASK_CSRH_TXMODE | 1549 MUSB2_MASK_CSRH_TXISO | temp); 1550 break; 1551 case UE_BULK: 1552 MUSB2_WRITE_2(sc, MUSB2_REG_TXMAXP, wMaxPacket); 1553 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRH, 1554 MUSB2_MASK_CSRH_TXMODE | temp); 1555 break; 1556 default: 1557 break; 1558 } 1559 1560 /* Need to flush twice in case of double bufring */ 1561 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1562 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 1563 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1564 MUSB2_MASK_CSRL_TXFFLUSH); 1565 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1566 if (csr & MUSB2_MASK_CSRL_TXFIFONEMPTY) { 1567 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1568 MUSB2_MASK_CSRL_TXFFLUSH); 1569 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1570 } 1571 } 1572 /* reset data toggle */ 1573 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 1574 MUSB2_MASK_CSRL_TXDT_CLR); 1575 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 1576 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1577 1578 /* set double/single buffering */ 1579 temp = MUSB2_READ_2(sc, MUSB2_REG_TXDBDIS); 1580 if (mps <= (sc->sc_hw_ep_profile[ep_no]. 1581 max_in_frame_size / 2)) { 1582 /* double buffer */ 1583 temp &= ~(1 << ep_no); 1584 } else { 1585 /* single buffer */ 1586 temp |= (1 << ep_no); 1587 } 1588 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, temp); 1589 1590 /* clear sent stall */ 1591 if (csr & MUSB2_MASK_CSRL_TXSENTSTALL) { 1592 MUSB2_WRITE_1(sc, MUSB2_REG_TXCSRL, 0); 1593 csr = MUSB2_READ_1(sc, MUSB2_REG_TXCSRL); 1594 } 1595 } else { 1596 1597 temp = 0; 1598 1599 /* Configure endpoint */ 1600 switch (ep_type) { 1601 case UE_INTERRUPT: 1602 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1603 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, 1604 MUSB2_MASK_CSRH_RXNYET | temp); 1605 break; 1606 case UE_ISOCHRONOUS: 1607 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1608 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, 1609 MUSB2_MASK_CSRH_RXNYET | 1610 MUSB2_MASK_CSRH_RXISO | temp); 1611 break; 1612 case UE_BULK: 1613 MUSB2_WRITE_2(sc, MUSB2_REG_RXMAXP, wMaxPacket); 1614 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRH, temp); 1615 break; 1616 default: 1617 break; 1618 } 1619 1620 /* Need to flush twice in case of double bufring */ 1621 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1622 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) { 1623 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1624 MUSB2_MASK_CSRL_RXFFLUSH); 1625 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1626 if (csr & MUSB2_MASK_CSRL_RXPKTRDY) { 1627 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1628 MUSB2_MASK_CSRL_RXFFLUSH); 1629 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1630 } 1631 } 1632 /* reset data toggle */ 1633 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 1634 MUSB2_MASK_CSRL_RXDT_CLR); 1635 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 1636 csr = MUSB2_READ_1(sc, MUSB2_REG_RXCSRL); 1637 1638 /* set double/single buffering */ 1639 temp = MUSB2_READ_2(sc, MUSB2_REG_RXDBDIS); 1640 if (mps <= (sc->sc_hw_ep_profile[ep_no]. 1641 max_out_frame_size / 2)) { 1642 /* double buffer */ 1643 temp &= ~(1 << ep_no); 1644 } else { 1645 /* single buffer */ 1646 temp |= (1 << ep_no); 1647 } 1648 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, temp); 1649 1650 /* clear sent stall */ 1651 if (csr & MUSB2_MASK_CSRL_RXSENTSTALL) { 1652 MUSB2_WRITE_1(sc, MUSB2_REG_RXCSRL, 0); 1653 } 1654 } 1655} 1656 1657static void 1658musbotg_clear_stall(struct usb_device *udev, struct usb_endpoint *ep) 1659{ 1660 struct musbotg_softc *sc; 1661 struct usb_endpoint_descriptor *ed; 1662 1663 DPRINTFN(4, "endpoint=%p\n", ep); 1664 1665 USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED); 1666 1667 /* check mode */ 1668 if (udev->flags.usb_mode != USB_MODE_DEVICE) { 1669 /* not supported */ 1670 return; 1671 } 1672 /* get softc */ 1673 sc = MUSBOTG_BUS2SC(udev->bus); 1674 1675 /* get endpoint descriptor */ 1676 ed = ep->edesc; 1677 1678 /* reset endpoint */ 1679 musbotg_clear_stall_sub(sc, 1680 UGETW(ed->wMaxPacketSize), 1681 (ed->bEndpointAddress & UE_ADDR), 1682 (ed->bmAttributes & UE_XFERTYPE), 1683 (ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT))); 1684} 1685 1686usb_error_t 1687musbotg_init(struct musbotg_softc *sc) 1688{ 1689 struct usb_hw_ep_profile *pf; 1690 uint16_t offset; 1691 uint8_t nrx; 1692 uint8_t ntx; 1693 uint8_t temp; 1694 uint8_t fsize; 1695 uint8_t frx; 1696 uint8_t ftx; 1697 uint8_t dynfifo; 1698 1699 DPRINTFN(1, "start\n"); 1700 1701 /* set up the bus structure */ 1702 sc->sc_bus.usbrev = USB_REV_2_0; 1703 sc->sc_bus.methods = &musbotg_bus_methods; 1704 1705 USB_BUS_LOCK(&sc->sc_bus); 1706 1707 /* turn on clocks */ 1708 1709 if (sc->sc_clocks_on) { 1710 (sc->sc_clocks_on) (sc->sc_clocks_arg); 1711 } 1712 /* wait a little for things to stabilise */ 1713 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 1000); 1714 1715 /* disable all interrupts */ 1716 1717 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0); 1718 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1719 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1720 1721 /* disable pullup */ 1722 1723 musbotg_pull_common(sc, 0); 1724 1725 /* wait a little bit (10ms) */ 1726 usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100); 1727 1728 /* disable double packet buffering */ 1729 MUSB2_WRITE_2(sc, MUSB2_REG_RXDBDIS, 0xFFFF); 1730 MUSB2_WRITE_2(sc, MUSB2_REG_TXDBDIS, 0xFFFF); 1731 1732 /* enable HighSpeed and ISO Update flags */ 1733 1734 MUSB2_WRITE_1(sc, MUSB2_REG_POWER, 1735 MUSB2_MASK_HSENAB | MUSB2_MASK_ISOUPD); 1736 1737 /* clear Session bit, if set */ 1738 1739 temp = MUSB2_READ_1(sc, MUSB2_REG_DEVCTL); 1740 temp &= ~MUSB2_MASK_SESS; 1741 MUSB2_WRITE_1(sc, MUSB2_REG_DEVCTL, temp); 1742 1743 DPRINTF("DEVCTL=0x%02x\n", temp); 1744 1745 /* disable testmode */ 1746 1747 MUSB2_WRITE_1(sc, MUSB2_REG_TESTMODE, 0); 1748 1749 /* set default value */ 1750 1751 MUSB2_WRITE_1(sc, MUSB2_REG_MISC, 0); 1752 1753 /* select endpoint index 0 */ 1754 1755 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, 0); 1756 1757 /* read out number of endpoints */ 1758 1759 nrx = 1760 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) / 16); 1761 1762 ntx = 1763 (MUSB2_READ_1(sc, MUSB2_REG_EPINFO) % 16); 1764 1765 /* these numbers exclude the control endpoint */ 1766 1767 DPRINTFN(2, "RX/TX endpoints: %u/%u\n", nrx, ntx); 1768 1769 sc->sc_ep_max = (nrx > ntx) ? nrx : ntx; 1770 if (sc->sc_ep_max == 0) { 1771 DPRINTFN(2, "ERROR: Looks like the clocks are off!\n"); 1772 } 1773 /* read out configuration data */ 1774 1775 sc->sc_conf_data = MUSB2_READ_1(sc, MUSB2_REG_CONFDATA); 1776 1777 DPRINTFN(2, "Config Data: 0x%02x\n", 1778 sc->sc_conf_data); 1779 1780 dynfifo = (sc->sc_conf_data & MUSB2_MASK_CD_DYNFIFOSZ) ? 1 : 0; 1781 1782 if (dynfifo) { 1783 device_printf(sc->sc_bus.bdev, "Dynamic FIFO sizing detected, " 1784 "assuming 16Kbytes of FIFO RAM\n"); 1785 } 1786 1787 DPRINTFN(2, "HW version: 0x%04x\n", 1788 MUSB2_READ_1(sc, MUSB2_REG_HWVERS)); 1789 1790 /* initialise endpoint profiles */ 1791 1792 offset = 0; 1793 1794 for (temp = 1; temp <= sc->sc_ep_max; temp++) { 1795 pf = sc->sc_hw_ep_profile + temp; 1796 1797 /* select endpoint */ 1798 MUSB2_WRITE_1(sc, MUSB2_REG_EPINDEX, temp); 1799 1800 fsize = MUSB2_READ_1(sc, MUSB2_REG_FSIZE); 1801 frx = (fsize & MUSB2_MASK_RX_FSIZE) / 16; 1802 ftx = (fsize & MUSB2_MASK_TX_FSIZE); 1803 1804 DPRINTF("Endpoint %u FIFO size: IN=%u, OUT=%u, DYN=%d\n", 1805 temp, ftx, frx, dynfifo); 1806 1807 if (dynfifo) { 1808 if (frx && (temp <= nrx)) { 1809 if (temp < 8) { 1810 frx = 10; /* 1K */ 1811 MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 1812 MUSB2_VAL_FIFOSZ_512 | 1813 MUSB2_MASK_FIFODB); 1814 } else { 1815 frx = 7; /* 128 bytes */ 1816 MUSB2_WRITE_1(sc, MUSB2_REG_RXFIFOSZ, 1817 MUSB2_VAL_FIFOSZ_128); 1818 } 1819 1820 MUSB2_WRITE_2(sc, MUSB2_REG_RXFIFOADD, 1821 offset >> 3); 1822 1823 offset += (1 << frx); 1824 } 1825 if (ftx && (temp <= ntx)) { 1826 if (temp < 8) { 1827 ftx = 10; /* 1K */ 1828 MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ, 1829 MUSB2_VAL_FIFOSZ_512 | 1830 MUSB2_MASK_FIFODB); 1831 } else { 1832 ftx = 7; /* 128 bytes */ 1833 MUSB2_WRITE_1(sc, MUSB2_REG_TXFIFOSZ, 1834 MUSB2_VAL_FIFOSZ_128); 1835 } 1836 1837 MUSB2_WRITE_2(sc, MUSB2_REG_TXFIFOADD, 1838 offset >> 3); 1839 1840 offset += (1 << ftx); 1841 } 1842 } 1843 1844 if (frx && ftx && (temp <= nrx) && (temp <= ntx)) { 1845 pf->max_in_frame_size = 1 << ftx; 1846 pf->max_out_frame_size = 1 << frx; 1847 pf->is_simplex = 0; /* duplex */ 1848 pf->support_multi_buffer = 1; 1849 pf->support_bulk = 1; 1850 pf->support_interrupt = 1; 1851 pf->support_isochronous = 1; 1852 pf->support_in = 1; 1853 pf->support_out = 1; 1854 } else if (frx && (temp <= nrx)) { 1855 pf->max_out_frame_size = 1 << frx; 1856 pf->is_simplex = 1; /* simplex */ 1857 pf->support_multi_buffer = 1; 1858 pf->support_bulk = 1; 1859 pf->support_interrupt = 1; 1860 pf->support_isochronous = 1; 1861 pf->support_out = 1; 1862 } else if (ftx && (temp <= ntx)) { 1863 pf->max_in_frame_size = 1 << ftx; 1864 pf->is_simplex = 1; /* simplex */ 1865 pf->support_multi_buffer = 1; 1866 pf->support_bulk = 1; 1867 pf->support_interrupt = 1; 1868 pf->support_isochronous = 1; 1869 pf->support_in = 1; 1870 } 1871 } 1872 1873 DPRINTFN(2, "Dynamic FIFO size = %d bytes\n", offset); 1874 1875 /* turn on default interrupts */ 1876 1877 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 1878 MUSB2_MASK_IRESET); 1879 1880 musbotg_clocks_off(sc); 1881 1882 USB_BUS_UNLOCK(&sc->sc_bus); 1883 1884 /* catch any lost interrupts */ 1885 1886 musbotg_do_poll(&sc->sc_bus); 1887 1888 return (0); /* success */ 1889} 1890 1891void 1892musbotg_uninit(struct musbotg_softc *sc) 1893{ 1894 USB_BUS_LOCK(&sc->sc_bus); 1895 1896 /* disable all interrupts */ 1897 MUSB2_WRITE_1(sc, MUSB2_REG_INTUSBE, 0); 1898 MUSB2_WRITE_2(sc, MUSB2_REG_INTTXE, 0); 1899 MUSB2_WRITE_2(sc, MUSB2_REG_INTRXE, 0); 1900 1901 sc->sc_flags.port_powered = 0; 1902 sc->sc_flags.status_vbus = 0; 1903 sc->sc_flags.status_bus_reset = 0; 1904 sc->sc_flags.status_suspend = 0; 1905 sc->sc_flags.change_suspend = 0; 1906 sc->sc_flags.change_connect = 1; 1907 1908 musbotg_pull_down(sc); 1909 musbotg_clocks_off(sc); 1910 USB_BUS_UNLOCK(&sc->sc_bus); 1911} 1912 1913static void 1914musbotg_suspend(struct musbotg_softc *sc) 1915{ 1916 /* TODO */ 1917} 1918 1919static void 1920musbotg_resume(struct musbotg_softc *sc) 1921{ 1922 /* TODO */ 1923} 1924 1925static void 1926musbotg_do_poll(struct usb_bus *bus) 1927{ 1928 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus); 1929 1930 USB_BUS_LOCK(&sc->sc_bus); 1931 musbotg_interrupt_poll(sc); 1932 USB_BUS_UNLOCK(&sc->sc_bus); 1933} 1934 1935/*------------------------------------------------------------------------* 1936 * musbotg bulk support 1937 *------------------------------------------------------------------------*/ 1938static void 1939musbotg_device_bulk_open(struct usb_xfer *xfer) 1940{ 1941 return; 1942} 1943 1944static void 1945musbotg_device_bulk_close(struct usb_xfer *xfer) 1946{ 1947 musbotg_device_done(xfer, USB_ERR_CANCELLED); 1948} 1949 1950static void 1951musbotg_device_bulk_enter(struct usb_xfer *xfer) 1952{ 1953 return; 1954} 1955 1956static void 1957musbotg_device_bulk_start(struct usb_xfer *xfer) 1958{ 1959 /* setup TDs */ 1960 musbotg_setup_standard_chain(xfer); 1961 musbotg_start_standard_chain(xfer); 1962} 1963 1964struct usb_pipe_methods musbotg_device_bulk_methods = 1965{ 1966 .open = musbotg_device_bulk_open, 1967 .close = musbotg_device_bulk_close, 1968 .enter = musbotg_device_bulk_enter, 1969 .start = musbotg_device_bulk_start, 1970}; 1971 1972/*------------------------------------------------------------------------* 1973 * musbotg control support 1974 *------------------------------------------------------------------------*/ 1975static void 1976musbotg_device_ctrl_open(struct usb_xfer *xfer) 1977{ 1978 return; 1979} 1980 1981static void 1982musbotg_device_ctrl_close(struct usb_xfer *xfer) 1983{ 1984 musbotg_device_done(xfer, USB_ERR_CANCELLED); 1985} 1986 1987static void 1988musbotg_device_ctrl_enter(struct usb_xfer *xfer) 1989{ 1990 return; 1991} 1992 1993static void 1994musbotg_device_ctrl_start(struct usb_xfer *xfer) 1995{ 1996 /* setup TDs */ 1997 musbotg_setup_standard_chain(xfer); 1998 musbotg_start_standard_chain(xfer); 1999} 2000 2001struct usb_pipe_methods musbotg_device_ctrl_methods = 2002{ 2003 .open = musbotg_device_ctrl_open, 2004 .close = musbotg_device_ctrl_close, 2005 .enter = musbotg_device_ctrl_enter, 2006 .start = musbotg_device_ctrl_start, 2007}; 2008 2009/*------------------------------------------------------------------------* 2010 * musbotg interrupt support 2011 *------------------------------------------------------------------------*/ 2012static void 2013musbotg_device_intr_open(struct usb_xfer *xfer) 2014{ 2015 return; 2016} 2017 2018static void 2019musbotg_device_intr_close(struct usb_xfer *xfer) 2020{ 2021 musbotg_device_done(xfer, USB_ERR_CANCELLED); 2022} 2023 2024static void 2025musbotg_device_intr_enter(struct usb_xfer *xfer) 2026{ 2027 return; 2028} 2029 2030static void 2031musbotg_device_intr_start(struct usb_xfer *xfer) 2032{ 2033 /* setup TDs */ 2034 musbotg_setup_standard_chain(xfer); 2035 musbotg_start_standard_chain(xfer); 2036} 2037 2038struct usb_pipe_methods musbotg_device_intr_methods = 2039{ 2040 .open = musbotg_device_intr_open, 2041 .close = musbotg_device_intr_close, 2042 .enter = musbotg_device_intr_enter, 2043 .start = musbotg_device_intr_start, 2044}; 2045 2046/*------------------------------------------------------------------------* 2047 * musbotg full speed isochronous support 2048 *------------------------------------------------------------------------*/ 2049static void 2050musbotg_device_isoc_open(struct usb_xfer *xfer) 2051{ 2052 return; 2053} 2054 2055static void 2056musbotg_device_isoc_close(struct usb_xfer *xfer) 2057{ 2058 musbotg_device_done(xfer, USB_ERR_CANCELLED); 2059} 2060 2061static void 2062musbotg_device_isoc_enter(struct usb_xfer *xfer) 2063{ 2064 struct musbotg_softc *sc = MUSBOTG_BUS2SC(xfer->xroot->bus); 2065 uint32_t temp; 2066 uint32_t nframes; 2067 uint32_t fs_frames; 2068 2069 DPRINTFN(5, "xfer=%p next=%d nframes=%d\n", 2070 xfer, xfer->endpoint->isoc_next, xfer->nframes); 2071 2072 /* get the current frame index */ 2073 2074 nframes = MUSB2_READ_2(sc, MUSB2_REG_FRAME); 2075 2076 /* 2077 * check if the frame index is within the window where the frames 2078 * will be inserted 2079 */ 2080 temp = (nframes - xfer->endpoint->isoc_next) & MUSB2_MASK_FRAME; 2081 2082 if (usbd_get_speed(xfer->xroot->udev) == USB_SPEED_HIGH) { 2083 fs_frames = (xfer->nframes + 7) / 8; 2084 } else { 2085 fs_frames = xfer->nframes; 2086 } 2087 2088 if ((xfer->endpoint->is_synced == 0) || 2089 (temp < fs_frames)) { 2090 /* 2091 * If there is data underflow or the pipe queue is 2092 * empty we schedule the transfer a few frames ahead 2093 * of the current frame position. Else two isochronous 2094 * transfers might overlap. 2095 */ 2096 xfer->endpoint->isoc_next = (nframes + 3) & MUSB2_MASK_FRAME; 2097 xfer->endpoint->is_synced = 1; 2098 DPRINTFN(2, "start next=%d\n", xfer->endpoint->isoc_next); 2099 } 2100 /* 2101 * compute how many milliseconds the insertion is ahead of the 2102 * current frame position: 2103 */ 2104 temp = (xfer->endpoint->isoc_next - nframes) & MUSB2_MASK_FRAME; 2105 2106 /* 2107 * pre-compute when the isochronous transfer will be finished: 2108 */ 2109 xfer->isoc_time_complete = 2110 usb_isoc_time_expand(&sc->sc_bus, nframes) + temp + 2111 fs_frames; 2112 2113 /* compute frame number for next insertion */ 2114 xfer->endpoint->isoc_next += fs_frames; 2115 2116 /* setup TDs */ 2117 musbotg_setup_standard_chain(xfer); 2118} 2119 2120static void 2121musbotg_device_isoc_start(struct usb_xfer *xfer) 2122{ 2123 /* start TD chain */ 2124 musbotg_start_standard_chain(xfer); 2125} 2126 2127struct usb_pipe_methods musbotg_device_isoc_methods = 2128{ 2129 .open = musbotg_device_isoc_open, 2130 .close = musbotg_device_isoc_close, 2131 .enter = musbotg_device_isoc_enter, 2132 .start = musbotg_device_isoc_start, 2133}; 2134 2135/*------------------------------------------------------------------------* 2136 * musbotg root control support 2137 *------------------------------------------------------------------------* 2138 * Simulate a hardware HUB by handling all the necessary requests. 2139 *------------------------------------------------------------------------*/ 2140 2141static const struct usb_device_descriptor musbotg_devd = { 2142 .bLength = sizeof(struct usb_device_descriptor), 2143 .bDescriptorType = UDESC_DEVICE, 2144 .bcdUSB = {0x00, 0x02}, 2145 .bDeviceClass = UDCLASS_HUB, 2146 .bDeviceSubClass = UDSUBCLASS_HUB, 2147 .bDeviceProtocol = UDPROTO_HSHUBSTT, 2148 .bMaxPacketSize = 64, 2149 .bcdDevice = {0x00, 0x01}, 2150 .iManufacturer = 1, 2151 .iProduct = 2, 2152 .bNumConfigurations = 1, 2153}; 2154 2155static const struct usb_device_qualifier musbotg_odevd = { 2156 .bLength = sizeof(struct usb_device_qualifier), 2157 .bDescriptorType = UDESC_DEVICE_QUALIFIER, 2158 .bcdUSB = {0x00, 0x02}, 2159 .bDeviceClass = UDCLASS_HUB, 2160 .bDeviceSubClass = UDSUBCLASS_HUB, 2161 .bDeviceProtocol = UDPROTO_FSHUB, 2162 .bMaxPacketSize0 = 0, 2163 .bNumConfigurations = 0, 2164}; 2165 2166static const struct musbotg_config_desc musbotg_confd = { 2167 .confd = { 2168 .bLength = sizeof(struct usb_config_descriptor), 2169 .bDescriptorType = UDESC_CONFIG, 2170 .wTotalLength[0] = sizeof(musbotg_confd), 2171 .bNumInterface = 1, 2172 .bConfigurationValue = 1, 2173 .iConfiguration = 0, 2174 .bmAttributes = UC_SELF_POWERED, 2175 .bMaxPower = 0, 2176 }, 2177 .ifcd = { 2178 .bLength = sizeof(struct usb_interface_descriptor), 2179 .bDescriptorType = UDESC_INTERFACE, 2180 .bNumEndpoints = 1, 2181 .bInterfaceClass = UICLASS_HUB, 2182 .bInterfaceSubClass = UISUBCLASS_HUB, 2183 .bInterfaceProtocol = 0, 2184 }, 2185 .endpd = { 2186 .bLength = sizeof(struct usb_endpoint_descriptor), 2187 .bDescriptorType = UDESC_ENDPOINT, 2188 .bEndpointAddress = (UE_DIR_IN | MUSBOTG_INTR_ENDPT), 2189 .bmAttributes = UE_INTERRUPT, 2190 .wMaxPacketSize[0] = 8, 2191 .bInterval = 255, 2192 }, 2193}; 2194 2195#define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) } 2196 2197static const struct usb_hub_descriptor_min musbotg_hubd = { 2198 .bDescLength = sizeof(musbotg_hubd), 2199 .bDescriptorType = UDESC_HUB, 2200 .bNbrPorts = 1, 2201 HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)), 2202 .bPwrOn2PwrGood = 50, 2203 .bHubContrCurrent = 0, 2204 .DeviceRemovable = {0}, /* port is removable */ 2205}; 2206 2207#define STRING_LANG \ 2208 0x09, 0x04, /* American English */ 2209 2210#define STRING_VENDOR \ 2211 'M', 0, 'e', 0, 'n', 0, 't', 0, 'o', 0, 'r', 0, ' ', 0, \ 2212 'G', 0, 'r', 0, 'a', 0, 'p', 0, 'h', 0, 'i', 0, 'c', 0, 's', 0 2213 2214#define STRING_PRODUCT \ 2215 'O', 0, 'T', 0, 'G', 0, ' ', 0, 'R', 0, \ 2216 'o', 0, 'o', 0, 't', 0, ' ', 0, 'H', 0, \ 2217 'U', 0, 'B', 0, 2218 2219USB_MAKE_STRING_DESC(STRING_LANG, musbotg_langtab); 2220USB_MAKE_STRING_DESC(STRING_VENDOR, musbotg_vendor); 2221USB_MAKE_STRING_DESC(STRING_PRODUCT, musbotg_product); 2222 2223static usb_error_t 2224musbotg_roothub_exec(struct usb_device *udev, 2225 struct usb_device_request *req, const void **pptr, uint16_t *plength) 2226{ 2227 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus); 2228 const void *ptr; 2229 uint16_t len; 2230 uint16_t value; 2231 uint16_t index; 2232 usb_error_t err; 2233 2234 USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED); 2235 2236 /* buffer reset */ 2237 ptr = (const void *)&sc->sc_hub_temp; 2238 len = 0; 2239 err = 0; 2240 2241 value = UGETW(req->wValue); 2242 index = UGETW(req->wIndex); 2243 2244 /* demultiplex the control request */ 2245 2246 switch (req->bmRequestType) { 2247 case UT_READ_DEVICE: 2248 switch (req->bRequest) { 2249 case UR_GET_DESCRIPTOR: 2250 goto tr_handle_get_descriptor; 2251 case UR_GET_CONFIG: 2252 goto tr_handle_get_config; 2253 case UR_GET_STATUS: 2254 goto tr_handle_get_status; 2255 default: 2256 goto tr_stalled; 2257 } 2258 break; 2259 2260 case UT_WRITE_DEVICE: 2261 switch (req->bRequest) { 2262 case UR_SET_ADDRESS: 2263 goto tr_handle_set_address; 2264 case UR_SET_CONFIG: 2265 goto tr_handle_set_config; 2266 case UR_CLEAR_FEATURE: 2267 goto tr_valid; /* nop */ 2268 case UR_SET_DESCRIPTOR: 2269 goto tr_valid; /* nop */ 2270 case UR_SET_FEATURE: 2271 default: 2272 goto tr_stalled; 2273 } 2274 break; 2275 2276 case UT_WRITE_ENDPOINT: 2277 switch (req->bRequest) { 2278 case UR_CLEAR_FEATURE: 2279 switch (UGETW(req->wValue)) { 2280 case UF_ENDPOINT_HALT: 2281 goto tr_handle_clear_halt; 2282 case UF_DEVICE_REMOTE_WAKEUP: 2283 goto tr_handle_clear_wakeup; 2284 default: 2285 goto tr_stalled; 2286 } 2287 break; 2288 case UR_SET_FEATURE: 2289 switch (UGETW(req->wValue)) { 2290 case UF_ENDPOINT_HALT: 2291 goto tr_handle_set_halt; 2292 case UF_DEVICE_REMOTE_WAKEUP: 2293 goto tr_handle_set_wakeup; 2294 default: 2295 goto tr_stalled; 2296 } 2297 break; 2298 case UR_SYNCH_FRAME: 2299 goto tr_valid; /* nop */ 2300 default: 2301 goto tr_stalled; 2302 } 2303 break; 2304 2305 case UT_READ_ENDPOINT: 2306 switch (req->bRequest) { 2307 case UR_GET_STATUS: 2308 goto tr_handle_get_ep_status; 2309 default: 2310 goto tr_stalled; 2311 } 2312 break; 2313 2314 case UT_WRITE_INTERFACE: 2315 switch (req->bRequest) { 2316 case UR_SET_INTERFACE: 2317 goto tr_handle_set_interface; 2318 case UR_CLEAR_FEATURE: 2319 goto tr_valid; /* nop */ 2320 case UR_SET_FEATURE: 2321 default: 2322 goto tr_stalled; 2323 } 2324 break; 2325 2326 case UT_READ_INTERFACE: 2327 switch (req->bRequest) { 2328 case UR_GET_INTERFACE: 2329 goto tr_handle_get_interface; 2330 case UR_GET_STATUS: 2331 goto tr_handle_get_iface_status; 2332 default: 2333 goto tr_stalled; 2334 } 2335 break; 2336 2337 case UT_WRITE_CLASS_INTERFACE: 2338 case UT_WRITE_VENDOR_INTERFACE: 2339 /* XXX forward */ 2340 break; 2341 2342 case UT_READ_CLASS_INTERFACE: 2343 case UT_READ_VENDOR_INTERFACE: 2344 /* XXX forward */ 2345 break; 2346 2347 case UT_WRITE_CLASS_DEVICE: 2348 switch (req->bRequest) { 2349 case UR_CLEAR_FEATURE: 2350 goto tr_valid; 2351 case UR_SET_DESCRIPTOR: 2352 case UR_SET_FEATURE: 2353 break; 2354 default: 2355 goto tr_stalled; 2356 } 2357 break; 2358 2359 case UT_WRITE_CLASS_OTHER: 2360 switch (req->bRequest) { 2361 case UR_CLEAR_FEATURE: 2362 goto tr_handle_clear_port_feature; 2363 case UR_SET_FEATURE: 2364 goto tr_handle_set_port_feature; 2365 case UR_CLEAR_TT_BUFFER: 2366 case UR_RESET_TT: 2367 case UR_STOP_TT: 2368 goto tr_valid; 2369 2370 default: 2371 goto tr_stalled; 2372 } 2373 break; 2374 2375 case UT_READ_CLASS_OTHER: 2376 switch (req->bRequest) { 2377 case UR_GET_TT_STATE: 2378 goto tr_handle_get_tt_state; 2379 case UR_GET_STATUS: 2380 goto tr_handle_get_port_status; 2381 default: 2382 goto tr_stalled; 2383 } 2384 break; 2385 2386 case UT_READ_CLASS_DEVICE: 2387 switch (req->bRequest) { 2388 case UR_GET_DESCRIPTOR: 2389 goto tr_handle_get_class_descriptor; 2390 case UR_GET_STATUS: 2391 goto tr_handle_get_class_status; 2392 2393 default: 2394 goto tr_stalled; 2395 } 2396 break; 2397 default: 2398 goto tr_stalled; 2399 } 2400 goto tr_valid; 2401 2402tr_handle_get_descriptor: 2403 switch (value >> 8) { 2404 case UDESC_DEVICE: 2405 if (value & 0xff) { 2406 goto tr_stalled; 2407 } 2408 len = sizeof(musbotg_devd); 2409 ptr = (const void *)&musbotg_devd; 2410 goto tr_valid; 2411 case UDESC_CONFIG: 2412 if (value & 0xff) { 2413 goto tr_stalled; 2414 } 2415 len = sizeof(musbotg_confd); 2416 ptr = (const void *)&musbotg_confd; 2417 goto tr_valid; 2418 case UDESC_STRING: 2419 switch (value & 0xff) { 2420 case 0: /* Language table */ 2421 len = sizeof(musbotg_langtab); 2422 ptr = (const void *)&musbotg_langtab; 2423 goto tr_valid; 2424 2425 case 1: /* Vendor */ 2426 len = sizeof(musbotg_vendor); 2427 ptr = (const void *)&musbotg_vendor; 2428 goto tr_valid; 2429 2430 case 2: /* Product */ 2431 len = sizeof(musbotg_product); 2432 ptr = (const void *)&musbotg_product; 2433 goto tr_valid; 2434 default: 2435 break; 2436 } 2437 break; 2438 default: 2439 goto tr_stalled; 2440 } 2441 goto tr_stalled; 2442 2443tr_handle_get_config: 2444 len = 1; 2445 sc->sc_hub_temp.wValue[0] = sc->sc_conf; 2446 goto tr_valid; 2447 2448tr_handle_get_status: 2449 len = 2; 2450 USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED); 2451 goto tr_valid; 2452 2453tr_handle_set_address: 2454 if (value & 0xFF00) { 2455 goto tr_stalled; 2456 } 2457 sc->sc_rt_addr = value; 2458 goto tr_valid; 2459 2460tr_handle_set_config: 2461 if (value >= 2) { 2462 goto tr_stalled; 2463 } 2464 sc->sc_conf = value; 2465 goto tr_valid; 2466 2467tr_handle_get_interface: 2468 len = 1; 2469 sc->sc_hub_temp.wValue[0] = 0; 2470 goto tr_valid; 2471 2472tr_handle_get_tt_state: 2473tr_handle_get_class_status: 2474tr_handle_get_iface_status: 2475tr_handle_get_ep_status: 2476 len = 2; 2477 USETW(sc->sc_hub_temp.wValue, 0); 2478 goto tr_valid; 2479 2480tr_handle_set_halt: 2481tr_handle_set_interface: 2482tr_handle_set_wakeup: 2483tr_handle_clear_wakeup: 2484tr_handle_clear_halt: 2485 goto tr_valid; 2486 2487tr_handle_clear_port_feature: 2488 if (index != 1) { 2489 goto tr_stalled; 2490 } 2491 DPRINTFN(8, "UR_CLEAR_PORT_FEATURE on port %d\n", index); 2492 2493 switch (value) { 2494 case UHF_PORT_SUSPEND: 2495 musbotg_wakeup_peer(sc); 2496 break; 2497 2498 case UHF_PORT_ENABLE: 2499 sc->sc_flags.port_enabled = 0; 2500 break; 2501 2502 case UHF_PORT_TEST: 2503 case UHF_PORT_INDICATOR: 2504 case UHF_C_PORT_ENABLE: 2505 case UHF_C_PORT_OVER_CURRENT: 2506 case UHF_C_PORT_RESET: 2507 /* nops */ 2508 break; 2509 case UHF_PORT_POWER: 2510 sc->sc_flags.port_powered = 0; 2511 musbotg_pull_down(sc); 2512 musbotg_clocks_off(sc); 2513 break; 2514 case UHF_C_PORT_CONNECTION: 2515 sc->sc_flags.change_connect = 0; 2516 break; 2517 case UHF_C_PORT_SUSPEND: 2518 sc->sc_flags.change_suspend = 0; 2519 break; 2520 default: 2521 err = USB_ERR_IOERROR; 2522 goto done; 2523 } 2524 goto tr_valid; 2525 2526tr_handle_set_port_feature: 2527 if (index != 1) { 2528 goto tr_stalled; 2529 } 2530 DPRINTFN(8, "UR_SET_PORT_FEATURE\n"); 2531 2532 switch (value) { 2533 case UHF_PORT_ENABLE: 2534 sc->sc_flags.port_enabled = 1; 2535 break; 2536 case UHF_PORT_SUSPEND: 2537 case UHF_PORT_RESET: 2538 case UHF_PORT_TEST: 2539 case UHF_PORT_INDICATOR: 2540 /* nops */ 2541 break; 2542 case UHF_PORT_POWER: 2543 sc->sc_flags.port_powered = 1; 2544 break; 2545 default: 2546 err = USB_ERR_IOERROR; 2547 goto done; 2548 } 2549 goto tr_valid; 2550 2551tr_handle_get_port_status: 2552 2553 DPRINTFN(8, "UR_GET_PORT_STATUS\n"); 2554 2555 if (index != 1) { 2556 goto tr_stalled; 2557 } 2558 if (sc->sc_flags.status_vbus) { 2559 musbotg_clocks_on(sc); 2560 musbotg_pull_up(sc); 2561 } else { 2562 musbotg_pull_down(sc); 2563 musbotg_clocks_off(sc); 2564 } 2565 2566 /* Select Device Side Mode */ 2567 value = UPS_PORT_MODE_DEVICE; 2568 2569 if (sc->sc_flags.status_high_speed) { 2570 value |= UPS_HIGH_SPEED; 2571 } 2572 if (sc->sc_flags.port_powered) { 2573 value |= UPS_PORT_POWER; 2574 } 2575 if (sc->sc_flags.port_enabled) { 2576 value |= UPS_PORT_ENABLED; 2577 } 2578 if (sc->sc_flags.status_vbus && 2579 sc->sc_flags.status_bus_reset) { 2580 value |= UPS_CURRENT_CONNECT_STATUS; 2581 } 2582 if (sc->sc_flags.status_suspend) { 2583 value |= UPS_SUSPEND; 2584 } 2585 USETW(sc->sc_hub_temp.ps.wPortStatus, value); 2586 2587 value = 0; 2588 2589 if (sc->sc_flags.change_connect) { 2590 value |= UPS_C_CONNECT_STATUS; 2591 2592 if (sc->sc_flags.status_vbus && 2593 sc->sc_flags.status_bus_reset) { 2594 /* reset EP0 state */ 2595 sc->sc_ep0_busy = 0; 2596 sc->sc_ep0_cmd = 0; 2597 } 2598 } 2599 if (sc->sc_flags.change_suspend) { 2600 value |= UPS_C_SUSPEND; 2601 } 2602 USETW(sc->sc_hub_temp.ps.wPortChange, value); 2603 len = sizeof(sc->sc_hub_temp.ps); 2604 goto tr_valid; 2605 2606tr_handle_get_class_descriptor: 2607 if (value & 0xFF) { 2608 goto tr_stalled; 2609 } 2610 ptr = (const void *)&musbotg_hubd; 2611 len = sizeof(musbotg_hubd); 2612 goto tr_valid; 2613 2614tr_stalled: 2615 err = USB_ERR_STALLED; 2616tr_valid: 2617done: 2618 *plength = len; 2619 *pptr = ptr; 2620 return (err); 2621} 2622 2623static void 2624musbotg_xfer_setup(struct usb_setup_params *parm) 2625{ 2626 const struct usb_hw_ep_profile *pf; 2627 struct musbotg_softc *sc; 2628 struct usb_xfer *xfer; 2629 void *last_obj; 2630 uint32_t ntd; 2631 uint32_t n; 2632 uint8_t ep_no; 2633 2634 sc = MUSBOTG_BUS2SC(parm->udev->bus); 2635 xfer = parm->curr_xfer; 2636 2637 /* 2638 * NOTE: This driver does not use any of the parameters that 2639 * are computed from the following values. Just set some 2640 * reasonable dummies: 2641 */ 2642 parm->hc_max_packet_size = 0x400; 2643 parm->hc_max_frame_size = 0x400; 2644 2645 if ((parm->methods == &musbotg_device_isoc_methods) || 2646 (parm->methods == &musbotg_device_intr_methods)) 2647 parm->hc_max_packet_count = 3; 2648 else 2649 parm->hc_max_packet_count = 1; 2650 2651 usbd_transfer_setup_sub(parm); 2652 2653 /* 2654 * compute maximum number of TDs 2655 */ 2656 if (parm->methods == &musbotg_device_ctrl_methods) { 2657 2658 ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC */ ; 2659 2660 } else if (parm->methods == &musbotg_device_bulk_methods) { 2661 2662 ntd = xfer->nframes + 1 /* SYNC */ ; 2663 2664 } else if (parm->methods == &musbotg_device_intr_methods) { 2665 2666 ntd = xfer->nframes + 1 /* SYNC */ ; 2667 2668 } else if (parm->methods == &musbotg_device_isoc_methods) { 2669 2670 ntd = xfer->nframes + 1 /* SYNC */ ; 2671 2672 } else { 2673 2674 ntd = 0; 2675 } 2676 2677 /* 2678 * check if "usbd_transfer_setup_sub" set an error 2679 */ 2680 if (parm->err) { 2681 return; 2682 } 2683 /* 2684 * allocate transfer descriptors 2685 */ 2686 last_obj = NULL; 2687 2688 /* 2689 * get profile stuff 2690 */ 2691 if (ntd) { 2692 2693 ep_no = xfer->endpointno & UE_ADDR; 2694 musbotg_get_hw_ep_profile(parm->udev, &pf, ep_no); 2695 2696 if (pf == NULL) { 2697 /* should not happen */ 2698 parm->err = USB_ERR_INVAL; 2699 return; 2700 } 2701 } else { 2702 ep_no = 0; 2703 pf = NULL; 2704 } 2705 2706 /* align data */ 2707 parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1)); 2708 2709 for (n = 0; n != ntd; n++) { 2710 2711 struct musbotg_td *td; 2712 2713 if (parm->buf) { 2714 2715 td = USB_ADD_BYTES(parm->buf, parm->size[0]); 2716 2717 /* init TD */ 2718 td->max_frame_size = xfer->max_frame_size; 2719 td->ep_no = ep_no; 2720 td->obj_next = last_obj; 2721 2722 last_obj = td; 2723 } 2724 parm->size[0] += sizeof(*td); 2725 } 2726 2727 xfer->td_start[0] = last_obj; 2728} 2729 2730static void 2731musbotg_xfer_unsetup(struct usb_xfer *xfer) 2732{ 2733 return; 2734} 2735 2736static void 2737musbotg_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc, 2738 struct usb_endpoint *ep) 2739{ 2740 struct musbotg_softc *sc = MUSBOTG_BUS2SC(udev->bus); 2741 2742 DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d)\n", 2743 ep, udev->address, 2744 edesc->bEndpointAddress, udev->flags.usb_mode, 2745 sc->sc_rt_addr); 2746 2747 if (udev->device_index != sc->sc_rt_addr) { 2748 2749 if (udev->flags.usb_mode != USB_MODE_DEVICE) { 2750 /* not supported */ 2751 return; 2752 } 2753 if ((udev->speed != USB_SPEED_FULL) && 2754 (udev->speed != USB_SPEED_HIGH)) { 2755 /* not supported */ 2756 return; 2757 } 2758 switch (edesc->bmAttributes & UE_XFERTYPE) { 2759 case UE_CONTROL: 2760 ep->methods = &musbotg_device_ctrl_methods; 2761 break; 2762 case UE_INTERRUPT: 2763 ep->methods = &musbotg_device_intr_methods; 2764 break; 2765 case UE_ISOCHRONOUS: 2766 ep->methods = &musbotg_device_isoc_methods; 2767 break; 2768 case UE_BULK: 2769 ep->methods = &musbotg_device_bulk_methods; 2770 break; 2771 default: 2772 /* do nothing */ 2773 break; 2774 } 2775 } 2776} 2777 2778static void 2779musbotg_set_hw_power_sleep(struct usb_bus *bus, uint32_t state) 2780{ 2781 struct musbotg_softc *sc = MUSBOTG_BUS2SC(bus); 2782 2783 switch (state) { 2784 case USB_HW_POWER_SUSPEND: 2785 musbotg_suspend(sc); 2786 break; 2787 case USB_HW_POWER_SHUTDOWN: 2788 musbotg_uninit(sc); 2789 break; 2790 case USB_HW_POWER_RESUME: 2791 musbotg_resume(sc); 2792 break; 2793 default: 2794 break; 2795 } 2796} 2797 2798struct usb_bus_methods musbotg_bus_methods = 2799{ 2800 .endpoint_init = &musbotg_ep_init, 2801 .xfer_setup = &musbotg_xfer_setup, 2802 .xfer_unsetup = &musbotg_xfer_unsetup, 2803 .get_hw_ep_profile = &musbotg_get_hw_ep_profile, 2804 .set_stall = &musbotg_set_stall, 2805 .clear_stall = &musbotg_clear_stall, 2806 .roothub_exec = &musbotg_roothub_exec, 2807 .xfer_poll = &musbotg_do_poll, 2808 .set_hw_power_sleep = &musbotg_set_hw_power_sleep, 2809}; 2810