2013-01-30 15:26:04 +00:00
|
|
|
/* $FreeBSD$ */
|
2009-01-13 18:49:35 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2009 Hans Petter Selasky. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-04-06 00:22:49 +00:00
|
|
|
* This file contains the driver for the ATMEGA series USB OTG Controller. This
|
|
|
|
* driver currently only supports the DCI mode of the USB hardware.
|
2009-01-13 18:49:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: When the chip detects BUS-reset it will also reset the
|
|
|
|
* endpoints, Function-address and more.
|
|
|
|
*/
|
|
|
|
|
2013-01-30 15:26:04 +00:00
|
|
|
#ifdef USB_GLOBAL_INCLUDE_FILE
|
|
|
|
#include USB_GLOBAL_INCLUDE_FILE
|
|
|
|
#else
|
2009-06-23 02:19:59 +00:00
|
|
|
#include <sys/stdint.h>
|
|
|
|
#include <sys/stddef.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/bus.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/condvar.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/sx.h>
|
|
|
|
#include <sys/unistd.h>
|
|
|
|
#include <sys/callout.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/priv.h>
|
|
|
|
|
2009-02-23 18:31:00 +00:00
|
|
|
#include <dev/usb/usb.h>
|
2009-06-23 02:19:59 +00:00
|
|
|
#include <dev/usb/usbdi.h>
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
#define USB_DEBUG_VAR atmegadci_debug
|
|
|
|
|
2009-02-23 18:31:00 +00:00
|
|
|
#include <dev/usb/usb_core.h>
|
|
|
|
#include <dev/usb/usb_debug.h>
|
|
|
|
#include <dev/usb/usb_busdma.h>
|
|
|
|
#include <dev/usb/usb_process.h>
|
|
|
|
#include <dev/usb/usb_transfer.h>
|
|
|
|
#include <dev/usb/usb_device.h>
|
|
|
|
#include <dev/usb/usb_hub.h>
|
|
|
|
#include <dev/usb/usb_util.h>
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-02-23 18:31:00 +00:00
|
|
|
#include <dev/usb/usb_controller.h>
|
|
|
|
#include <dev/usb/usb_bus.h>
|
2013-01-30 15:26:04 +00:00
|
|
|
#endif /* USB_GLOBAL_INCLUDE_FILE */
|
|
|
|
|
2009-02-23 18:31:00 +00:00
|
|
|
#include <dev/usb/controller/atmegadci.h>
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
#define ATMEGA_BUS2SC(bus) \
|
|
|
|
((struct atmegadci_softc *)(((uint8_t *)(bus)) - \
|
2009-03-20 21:50:54 +00:00
|
|
|
((uint8_t *)&(((struct atmegadci_softc *)0)->sc_bus))))
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
#define ATMEGA_PC2SC(pc) \
|
2009-03-20 21:48:11 +00:00
|
|
|
ATMEGA_BUS2SC(USB_DMATAG_TO_XROOT((pc)->tag_parent)->bus)
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-06-23 02:19:59 +00:00
|
|
|
#ifdef USB_DEBUG
|
2009-01-13 18:49:35 +00:00
|
|
|
static int atmegadci_debug = 0;
|
|
|
|
|
2011-11-07 15:43:11 +00:00
|
|
|
static SYSCTL_NODE(_hw_usb, OID_AUTO, atmegadci, CTLFLAG_RW, 0,
|
|
|
|
"USB ATMEGA DCI");
|
2015-01-05 15:04:17 +00:00
|
|
|
SYSCTL_INT(_hw_usb_atmegadci, OID_AUTO, debug, CTLFLAG_RWTUN,
|
2009-01-13 18:49:35 +00:00
|
|
|
&atmegadci_debug, 0, "ATMEGA DCI debug level");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ATMEGA_INTR_ENDPT 1
|
|
|
|
|
|
|
|
/* prototypes */
|
|
|
|
|
2013-12-11 13:20:32 +00:00
|
|
|
static const struct usb_bus_methods atmegadci_bus_methods;
|
|
|
|
static const struct usb_pipe_methods atmegadci_device_non_isoc_methods;
|
|
|
|
static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
static atmegadci_cmd_t atmegadci_setup_rx;
|
|
|
|
static atmegadci_cmd_t atmegadci_data_rx;
|
|
|
|
static atmegadci_cmd_t atmegadci_data_tx;
|
|
|
|
static atmegadci_cmd_t atmegadci_data_tx_sync;
|
2009-05-29 18:46:57 +00:00
|
|
|
static void atmegadci_device_done(struct usb_xfer *, usb_error_t);
|
2009-05-28 17:36:36 +00:00
|
|
|
static void atmegadci_do_poll(struct usb_bus *);
|
|
|
|
static void atmegadci_standard_done(struct usb_xfer *);
|
2009-04-05 18:20:49 +00:00
|
|
|
static void atmegadci_root_intr(struct atmegadci_softc *sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Here is a list of what the chip supports:
|
|
|
|
*/
|
2009-05-28 17:36:36 +00:00
|
|
|
static const struct usb_hw_ep_profile
|
2009-01-13 18:49:35 +00:00
|
|
|
atmegadci_ep_profile[2] = {
|
|
|
|
|
|
|
|
[0] = {
|
|
|
|
.max_in_frame_size = 64,
|
|
|
|
.max_out_frame_size = 64,
|
|
|
|
.is_simplex = 1,
|
|
|
|
.support_control = 1,
|
|
|
|
},
|
|
|
|
[1] = {
|
|
|
|
.max_in_frame_size = 64,
|
|
|
|
.max_out_frame_size = 64,
|
|
|
|
.is_simplex = 1,
|
|
|
|
.support_bulk = 1,
|
|
|
|
.support_interrupt = 1,
|
|
|
|
.support_isochronous = 1,
|
|
|
|
.support_in = 1,
|
|
|
|
.support_out = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_get_hw_ep_profile(struct usb_device *udev,
|
|
|
|
const struct usb_hw_ep_profile **ppf, uint8_t ep_addr)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
if (ep_addr == 0)
|
|
|
|
*ppf = atmegadci_ep_profile;
|
|
|
|
else if (ep_addr < ATMEGA_EP_MAX)
|
|
|
|
*ppf = atmegadci_ep_profile + 1;
|
|
|
|
else
|
|
|
|
*ppf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_clocks_on(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
if (sc->sc_flags.clocks_off &&
|
|
|
|
sc->sc_flags.port_powered) {
|
|
|
|
|
|
|
|
DPRINTFN(5, "\n");
|
|
|
|
|
|
|
|
/* turn on clocks */
|
|
|
|
(sc->sc_clocks_on) (&sc->sc_bus);
|
|
|
|
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
|
|
|
|
ATMEGA_USBCON_USBE |
|
|
|
|
ATMEGA_USBCON_OTGPADE |
|
|
|
|
ATMEGA_USBCON_VBUSTE);
|
|
|
|
|
|
|
|
sc->sc_flags.clocks_off = 0;
|
|
|
|
|
|
|
|
/* enable transceiver ? */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_clocks_off(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
if (!sc->sc_flags.clocks_off) {
|
|
|
|
|
|
|
|
DPRINTFN(5, "\n");
|
|
|
|
|
|
|
|
/* disable Transceiver ? */
|
|
|
|
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
|
|
|
|
ATMEGA_USBCON_USBE |
|
|
|
|
ATMEGA_USBCON_OTGPADE |
|
|
|
|
ATMEGA_USBCON_FRZCLK |
|
|
|
|
ATMEGA_USBCON_VBUSTE);
|
|
|
|
|
|
|
|
/* turn clocks off */
|
|
|
|
(sc->sc_clocks_off) (&sc->sc_bus);
|
|
|
|
|
|
|
|
sc->sc_flags.clocks_off = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_pull_up(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
/* pullup D+, if possible */
|
|
|
|
|
|
|
|
if (!sc->sc_flags.d_pulled_up &&
|
|
|
|
sc->sc_flags.port_powered) {
|
|
|
|
sc->sc_flags.d_pulled_up = 1;
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDCON, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_pull_down(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
/* pulldown D+, if possible */
|
|
|
|
|
|
|
|
if (sc->sc_flags.d_pulled_up) {
|
|
|
|
sc->sc_flags.d_pulled_up = 0;
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_wakeup_peer(struct atmegadci_softc *sc)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
uint8_t temp;
|
|
|
|
|
|
|
|
if (!sc->sc_flags.status_suspend) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UDCON);
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDCON, temp | ATMEGA_UDCON_RMWKUP);
|
|
|
|
|
|
|
|
/* wait 8 milliseconds */
|
2009-02-24 03:39:13 +00:00
|
|
|
/* Wait for reset to complete. */
|
2009-06-15 01:02:43 +00:00
|
|
|
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 125);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* hardware should have cleared RMWKUP bit */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_set_address(struct atmegadci_softc *sc, uint8_t addr)
|
|
|
|
{
|
|
|
|
DPRINTFN(5, "addr=%d\n", addr);
|
|
|
|
|
|
|
|
addr |= ATMEGA_UDADDR_ADDEN;
|
|
|
|
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
atmegadci_setup_rx(struct atmegadci_td *td)
|
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_device_request req;
|
2009-01-13 18:49:35 +00:00
|
|
|
uint16_t count;
|
|
|
|
uint8_t temp;
|
|
|
|
|
|
|
|
/* get pointer to softc */
|
|
|
|
sc = ATMEGA_PC2SC(td->pc);
|
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
|
|
|
|
|
|
|
|
/* check endpoint status */
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
|
|
|
|
|
|
|
|
DPRINTFN(5, "UEINTX=0x%02x\n", temp);
|
|
|
|
|
|
|
|
if (!(temp & ATMEGA_UEINTX_RXSTPI)) {
|
|
|
|
goto not_complete;
|
|
|
|
}
|
2009-04-05 18:18:16 +00:00
|
|
|
/* clear did stall */
|
|
|
|
td->did_stall = 0;
|
2009-01-13 18:49:35 +00:00
|
|
|
/* get the packet byte count */
|
|
|
|
count =
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
|
|
|
|
|
|
|
|
/* mask away undefined bits */
|
|
|
|
count &= 0x7FF;
|
|
|
|
|
|
|
|
/* verify data length */
|
|
|
|
if (count != td->remainder) {
|
|
|
|
DPRINTFN(0, "Invalid SETUP packet "
|
|
|
|
"length, %d bytes\n", count);
|
|
|
|
goto not_complete;
|
|
|
|
}
|
|
|
|
if (count != sizeof(req)) {
|
|
|
|
DPRINTFN(0, "Unsupported SETUP packet "
|
|
|
|
"length, %d bytes\n", count);
|
|
|
|
goto not_complete;
|
|
|
|
}
|
|
|
|
/* receive data */
|
|
|
|
ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
|
|
|
|
(void *)&req, sizeof(req));
|
|
|
|
|
|
|
|
/* copy data into real buffer */
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_copy_in(td->pc, 0, &req, sizeof(req));
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
td->offset = sizeof(req);
|
|
|
|
td->remainder = 0;
|
|
|
|
|
|
|
|
/* sneak peek the set address */
|
|
|
|
if ((req.bmRequestType == UT_WRITE_DEVICE) &&
|
|
|
|
(req.bRequest == UR_SET_ADDRESS)) {
|
|
|
|
sc->sc_dv_addr = req.wValue[0] & 0x7F;
|
2009-03-11 04:58:21 +00:00
|
|
|
/* must write address before ZLP */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDADDR, sc->sc_dv_addr);
|
2009-01-13 18:49:35 +00:00
|
|
|
} else {
|
|
|
|
sc->sc_dv_addr = 0xFF;
|
|
|
|
}
|
|
|
|
|
2009-09-28 07:10:32 +00:00
|
|
|
/* Clear SETUP packet interrupt and all other previous interrupts */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0);
|
2009-01-13 18:49:35 +00:00
|
|
|
return (0); /* complete */
|
|
|
|
|
|
|
|
not_complete:
|
2009-04-05 18:18:16 +00:00
|
|
|
/* abort any ongoing transfer */
|
|
|
|
if (!td->did_stall) {
|
|
|
|
DPRINTFN(5, "stalling\n");
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_STALLRQ);
|
|
|
|
td->did_stall = 1;
|
|
|
|
}
|
2009-04-05 18:18:25 +00:00
|
|
|
if (temp & ATMEGA_UEINTX_RXSTPI) {
|
|
|
|
/* clear SETUP packet interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ~ATMEGA_UEINTX_RXSTPI);
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
/* we only want to know if there is a SETUP packet */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, ATMEGA_UEIENX_RXSTPE);
|
|
|
|
return (1); /* not complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
atmegadci_data_rx(struct atmegadci_td *td)
|
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_page_search buf_res;
|
2009-01-13 18:49:35 +00:00
|
|
|
uint16_t count;
|
|
|
|
uint8_t temp;
|
|
|
|
uint8_t to;
|
|
|
|
uint8_t got_short;
|
|
|
|
|
|
|
|
to = 3; /* don't loop forever! */
|
|
|
|
got_short = 0;
|
|
|
|
|
|
|
|
/* get pointer to softc */
|
|
|
|
sc = ATMEGA_PC2SC(td->pc);
|
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
|
|
|
|
|
|
|
|
repeat:
|
|
|
|
/* check if any of the FIFO banks have data */
|
|
|
|
/* check endpoint status */
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
|
|
|
|
|
|
|
|
DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
|
|
|
|
|
|
|
|
if (temp & ATMEGA_UEINTX_RXSTPI) {
|
|
|
|
if (td->remainder == 0) {
|
|
|
|
/*
|
|
|
|
* We are actually complete and have
|
|
|
|
* received the next SETUP
|
|
|
|
*/
|
|
|
|
DPRINTFN(5, "faking complete\n");
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* USB Host Aborted the transfer.
|
|
|
|
*/
|
|
|
|
td->error = 1;
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
|
|
|
/* check status */
|
|
|
|
if (!(temp & (ATMEGA_UEINTX_FIFOCON |
|
|
|
|
ATMEGA_UEINTX_RXOUTI))) {
|
|
|
|
/* no data */
|
|
|
|
goto not_complete;
|
|
|
|
}
|
|
|
|
/* get the packet byte count */
|
|
|
|
count =
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UEBCHX) << 8) |
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UEBCLX));
|
|
|
|
|
|
|
|
/* mask away undefined bits */
|
|
|
|
count &= 0x7FF;
|
|
|
|
|
|
|
|
/* verify the packet byte count */
|
|
|
|
if (count != td->max_packet_size) {
|
|
|
|
if (count < td->max_packet_size) {
|
|
|
|
/* we have a short packet */
|
|
|
|
td->short_pkt = 1;
|
|
|
|
got_short = 1;
|
|
|
|
} else {
|
|
|
|
/* invalid USB packet */
|
|
|
|
td->error = 1;
|
|
|
|
return (0); /* we are complete */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* verify the packet byte count */
|
|
|
|
if (count > td->remainder) {
|
|
|
|
/* invalid USB packet */
|
|
|
|
td->error = 1;
|
|
|
|
return (0); /* we are complete */
|
|
|
|
}
|
|
|
|
while (count > 0) {
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_get_page(td->pc, td->offset, &buf_res);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* get correct length */
|
|
|
|
if (buf_res.length > count) {
|
|
|
|
buf_res.length = count;
|
|
|
|
}
|
|
|
|
/* receive data */
|
|
|
|
ATMEGA_READ_MULTI_1(sc, ATMEGA_UEDATX,
|
|
|
|
buf_res.buffer, buf_res.length);
|
|
|
|
|
|
|
|
/* update counters */
|
|
|
|
count -= buf_res.length;
|
|
|
|
td->offset += buf_res.length;
|
|
|
|
td->remainder -= buf_res.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear OUT packet interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_RXOUTI ^ 0xFF);
|
|
|
|
|
|
|
|
/* release FIFO bank */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, ATMEGA_UEINTX_FIFOCON ^ 0xFF);
|
|
|
|
|
|
|
|
/* check if we are complete */
|
|
|
|
if ((td->remainder == 0) || got_short) {
|
|
|
|
if (td->short_pkt) {
|
|
|
|
/* we are complete */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* else need to receive a zero length packet */
|
|
|
|
}
|
|
|
|
if (--to) {
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
not_complete:
|
|
|
|
/* we only want to know if there is a SETUP packet or OUT packet */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
|
|
|
|
ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_RXOUTE);
|
|
|
|
return (1); /* not complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
atmegadci_data_tx(struct atmegadci_td *td)
|
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_page_search buf_res;
|
2009-01-13 18:49:35 +00:00
|
|
|
uint16_t count;
|
|
|
|
uint8_t to;
|
|
|
|
uint8_t temp;
|
|
|
|
|
|
|
|
to = 3; /* don't loop forever! */
|
|
|
|
|
|
|
|
/* get pointer to softc */
|
|
|
|
sc = ATMEGA_PC2SC(td->pc);
|
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
|
|
|
|
|
|
|
|
repeat:
|
|
|
|
|
|
|
|
/* check endpoint status */
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
|
|
|
|
|
|
|
|
DPRINTFN(5, "temp=0x%02x rem=%u\n", temp, td->remainder);
|
|
|
|
|
|
|
|
if (temp & ATMEGA_UEINTX_RXSTPI) {
|
|
|
|
/*
|
|
|
|
* The current transfer was aborted
|
|
|
|
* by the USB Host
|
|
|
|
*/
|
|
|
|
td->error = 1;
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
2009-04-22 17:08:13 +00:00
|
|
|
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
|
|
|
|
if (temp & 3) {
|
|
|
|
/* cannot write any data - a bank is busy */
|
2009-01-13 18:49:35 +00:00
|
|
|
goto not_complete;
|
|
|
|
}
|
2009-04-22 17:08:13 +00:00
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
count = td->max_packet_size;
|
|
|
|
if (td->remainder < count) {
|
|
|
|
/* we have a short packet */
|
|
|
|
td->short_pkt = 1;
|
|
|
|
count = td->remainder;
|
|
|
|
}
|
|
|
|
while (count > 0) {
|
|
|
|
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_get_page(td->pc, td->offset, &buf_res);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* get correct length */
|
|
|
|
if (buf_res.length > count) {
|
|
|
|
buf_res.length = count;
|
|
|
|
}
|
|
|
|
/* transmit data */
|
|
|
|
ATMEGA_WRITE_MULTI_1(sc, ATMEGA_UEDATX,
|
|
|
|
buf_res.buffer, buf_res.length);
|
|
|
|
|
|
|
|
/* update counters */
|
|
|
|
count -= buf_res.length;
|
|
|
|
td->offset += buf_res.length;
|
|
|
|
td->remainder -= buf_res.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clear IN packet interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_TXINI);
|
|
|
|
|
|
|
|
/* allocate FIFO bank */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEINTX, 0xFF ^ ATMEGA_UEINTX_FIFOCON);
|
|
|
|
|
|
|
|
/* check remainder */
|
|
|
|
if (td->remainder == 0) {
|
|
|
|
if (td->short_pkt) {
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
|
|
|
/* else we need to transmit a short packet */
|
|
|
|
}
|
|
|
|
if (--to) {
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
not_complete:
|
|
|
|
/* we only want to know if there is a SETUP packet or free IN packet */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
|
|
|
|
ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
|
|
|
|
return (1); /* not complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
|
|
|
atmegadci_data_tx_sync(struct atmegadci_td *td)
|
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
|
|
|
uint8_t temp;
|
|
|
|
|
|
|
|
/* get pointer to softc */
|
|
|
|
sc = ATMEGA_PC2SC(td->pc);
|
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, td->ep_no);
|
|
|
|
|
|
|
|
/* check endpoint status */
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UEINTX);
|
|
|
|
|
|
|
|
DPRINTFN(5, "temp=0x%02x\n", temp);
|
|
|
|
|
|
|
|
if (temp & ATMEGA_UEINTX_RXSTPI) {
|
|
|
|
DPRINTFN(5, "faking complete\n");
|
|
|
|
/* Race condition */
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The control endpoint has only got one bank, so if that bank
|
|
|
|
* is free the packet has been transferred!
|
|
|
|
*/
|
2009-04-22 17:08:13 +00:00
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
|
|
|
|
if (temp & 3) {
|
|
|
|
/* cannot write any data - a bank is busy */
|
2009-01-13 18:49:35 +00:00
|
|
|
goto not_complete;
|
|
|
|
}
|
|
|
|
if (sc->sc_dv_addr != 0xFF) {
|
|
|
|
/* set new address */
|
|
|
|
atmegadci_set_address(sc, sc->sc_dv_addr);
|
|
|
|
}
|
|
|
|
return (0); /* complete */
|
|
|
|
|
|
|
|
not_complete:
|
|
|
|
/* we only want to know if there is a SETUP packet or free IN packet */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX,
|
|
|
|
ATMEGA_UEIENX_RXSTPE | ATMEGA_UEIENX_TXINE);
|
|
|
|
return (1); /* not complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_xfer_do_fifo(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_td *td;
|
|
|
|
|
|
|
|
DPRINTFN(9, "\n");
|
|
|
|
|
|
|
|
td = xfer->td_transfer_cache;
|
|
|
|
while (1) {
|
|
|
|
if ((td->func) (td)) {
|
|
|
|
/* operation in progress */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (((void *)td) == xfer->td_transfer_last) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (td->error) {
|
|
|
|
goto done;
|
|
|
|
} else if (td->remainder > 0) {
|
|
|
|
/*
|
|
|
|
* We had a short transfer. If there is no alternate
|
|
|
|
* next, stop processing !
|
|
|
|
*/
|
|
|
|
if (!td->alt_next) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Fetch the next transfer descriptor and transfer
|
|
|
|
* some flags to the next transfer descriptor
|
|
|
|
*/
|
|
|
|
td = td->obj_next;
|
|
|
|
xfer->td_transfer_cache = td;
|
|
|
|
}
|
|
|
|
return (1); /* not complete */
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* compute all actual lengths */
|
|
|
|
|
|
|
|
atmegadci_standard_done(xfer);
|
|
|
|
return (0); /* complete */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_interrupt_poll(struct atmegadci_softc *sc)
|
|
|
|
{
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_xfer *xfer;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
repeat:
|
|
|
|
TAILQ_FOREACH(xfer, &sc->sc_bus.intr_q.head, wait_entry) {
|
|
|
|
if (!atmegadci_xfer_do_fifo(xfer)) {
|
|
|
|
/* queue has been modified */
|
|
|
|
goto repeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_vbus_interrupt(struct atmegadci_softc *sc, uint8_t is_on)
|
|
|
|
{
|
|
|
|
DPRINTFN(5, "vbus = %u\n", is_on);
|
|
|
|
|
|
|
|
if (is_on) {
|
|
|
|
if (!sc->sc_flags.status_vbus) {
|
|
|
|
sc->sc_flags.status_vbus = 1;
|
|
|
|
|
|
|
|
/* complete root HUB interrupt endpoint */
|
|
|
|
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sc->sc_flags.status_vbus) {
|
|
|
|
sc->sc_flags.status_vbus = 0;
|
|
|
|
sc->sc_flags.status_bus_reset = 0;
|
|
|
|
sc->sc_flags.status_suspend = 0;
|
|
|
|
sc->sc_flags.change_suspend = 0;
|
|
|
|
sc->sc_flags.change_connect = 1;
|
|
|
|
|
|
|
|
/* complete root HUB interrupt endpoint */
|
|
|
|
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
atmegadci_interrupt(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
uint8_t status;
|
|
|
|
|
|
|
|
USB_BUS_LOCK(&sc->sc_bus);
|
|
|
|
|
|
|
|
/* read interrupt status */
|
|
|
|
status = ATMEGA_READ_1(sc, ATMEGA_UDINT);
|
|
|
|
|
|
|
|
/* clear all set interrupts */
|
2009-04-05 18:18:07 +00:00
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDINT, (~status) & 0x7D);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
DPRINTFN(14, "UDINT=0x%02x\n", status);
|
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
/* check for any bus state change interrupts */
|
|
|
|
if (status & ATMEGA_UDINT_EORSTI) {
|
|
|
|
|
|
|
|
DPRINTFN(5, "end of reset\n");
|
|
|
|
|
|
|
|
/* set correct state */
|
|
|
|
sc->sc_flags.status_bus_reset = 1;
|
|
|
|
sc->sc_flags.status_suspend = 0;
|
|
|
|
sc->sc_flags.change_suspend = 0;
|
|
|
|
sc->sc_flags.change_connect = 1;
|
|
|
|
|
|
|
|
/* disable resume interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
|
|
|
|
ATMEGA_UDINT_SUSPE |
|
|
|
|
ATMEGA_UDINT_EORSTE);
|
|
|
|
|
|
|
|
/* complete root HUB interrupt endpoint */
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If resume and suspend is set at the same time we interpret
|
|
|
|
* that like RESUME. Resume is set when there is at least 3
|
|
|
|
* milliseconds of inactivity on the USB BUS.
|
|
|
|
*/
|
2009-05-20 17:00:55 +00:00
|
|
|
if (status & ATMEGA_UDINT_WAKEUPI) {
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
DPRINTFN(5, "resume interrupt\n");
|
|
|
|
|
|
|
|
if (sc->sc_flags.status_suspend) {
|
|
|
|
/* update status bits */
|
|
|
|
sc->sc_flags.status_suspend = 0;
|
|
|
|
sc->sc_flags.change_suspend = 1;
|
|
|
|
|
|
|
|
/* disable resume interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
|
|
|
|
ATMEGA_UDINT_SUSPE |
|
|
|
|
ATMEGA_UDINT_EORSTE);
|
|
|
|
|
|
|
|
/* complete root HUB interrupt endpoint */
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
} else if (status & ATMEGA_UDINT_SUSPI) {
|
|
|
|
|
|
|
|
DPRINTFN(5, "suspend interrupt\n");
|
|
|
|
|
|
|
|
if (!sc->sc_flags.status_suspend) {
|
|
|
|
/* update status bits */
|
|
|
|
sc->sc_flags.status_suspend = 1;
|
|
|
|
sc->sc_flags.change_suspend = 1;
|
|
|
|
|
|
|
|
/* disable suspend interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
|
2009-05-20 17:00:55 +00:00
|
|
|
ATMEGA_UDINT_WAKEUPE |
|
2009-01-13 18:49:35 +00:00
|
|
|
ATMEGA_UDINT_EORSTE);
|
|
|
|
|
|
|
|
/* complete root HUB interrupt endpoint */
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* check VBUS */
|
|
|
|
status = ATMEGA_READ_1(sc, ATMEGA_USBINT);
|
|
|
|
|
|
|
|
/* clear all set interrupts */
|
2009-04-05 18:18:07 +00:00
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_USBINT, (~status) & 0x03);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
if (status & ATMEGA_USBINT_VBUSTI) {
|
|
|
|
uint8_t temp;
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
DPRINTFN(5, "USBINT=0x%02x\n", status);
|
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
|
|
|
|
atmegadci_vbus_interrupt(sc, temp & ATMEGA_USBSTA_VBUS);
|
|
|
|
}
|
|
|
|
/* check for any endpoint interrupts */
|
|
|
|
status = ATMEGA_READ_1(sc, ATMEGA_UEINT);
|
2009-04-05 18:18:07 +00:00
|
|
|
/* the hardware will clear the UEINT bits automatically */
|
2009-01-13 18:49:35 +00:00
|
|
|
if (status) {
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
DPRINTFN(5, "real endpoint interrupt UEINT=0x%02x\n", status);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
atmegadci_interrupt_poll(sc);
|
|
|
|
}
|
|
|
|
USB_BUS_UNLOCK(&sc->sc_bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_setup_standard_chain_sub(struct atmegadci_std_temp *temp)
|
|
|
|
{
|
|
|
|
struct atmegadci_td *td;
|
|
|
|
|
|
|
|
/* get current Transfer Descriptor */
|
|
|
|
td = temp->td_next;
|
|
|
|
temp->td = td;
|
|
|
|
|
|
|
|
/* prepare for next TD */
|
|
|
|
temp->td_next = td->obj_next;
|
|
|
|
|
|
|
|
/* fill out the Transfer Descriptor */
|
|
|
|
td->func = temp->func;
|
|
|
|
td->pc = temp->pc;
|
|
|
|
td->offset = temp->offset;
|
|
|
|
td->remainder = temp->len;
|
|
|
|
td->error = 0;
|
2009-05-21 17:39:21 +00:00
|
|
|
td->did_stall = temp->did_stall;
|
2009-01-13 18:49:35 +00:00
|
|
|
td->short_pkt = temp->short_pkt;
|
|
|
|
td->alt_next = temp->setup_alt_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_setup_standard_chain(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_std_temp temp;
|
|
|
|
struct atmegadci_softc *sc;
|
|
|
|
struct atmegadci_td *td;
|
|
|
|
uint32_t x;
|
|
|
|
uint8_t ep_no;
|
|
|
|
uint8_t need_sync;
|
|
|
|
|
|
|
|
DPRINTFN(9, "addr=%d endpt=%d sumlen=%d speed=%d\n",
|
2009-06-07 19:41:11 +00:00
|
|
|
xfer->address, UE_GET_ADDR(xfer->endpointno),
|
2009-06-15 01:02:43 +00:00
|
|
|
xfer->sumlen, usbd_get_speed(xfer->xroot->udev));
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
temp.max_frame_size = xfer->max_frame_size;
|
|
|
|
|
|
|
|
td = xfer->td_start[0];
|
|
|
|
xfer->td_transfer_first = td;
|
|
|
|
xfer->td_transfer_cache = td;
|
|
|
|
|
|
|
|
/* setup temp */
|
|
|
|
|
2009-11-22 21:19:01 +00:00
|
|
|
temp.pc = NULL;
|
2009-01-13 18:49:35 +00:00
|
|
|
temp.td = NULL;
|
|
|
|
temp.td_next = xfer->td_start[0];
|
|
|
|
temp.offset = 0;
|
2014-05-13 13:46:38 +00:00
|
|
|
temp.setup_alt_next = xfer->flags_int.short_frames_ok ||
|
|
|
|
xfer->flags_int.isochronous_xfr;
|
2009-05-21 17:39:21 +00:00
|
|
|
temp.did_stall = !xfer->flags_int.control_stall;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
sc = ATMEGA_BUS2SC(xfer->xroot->bus);
|
2009-06-07 19:41:11 +00:00
|
|
|
ep_no = (xfer->endpointno & UE_ADDR);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* check if we should prepend a setup message */
|
|
|
|
|
|
|
|
if (xfer->flags_int.control_xfr) {
|
|
|
|
if (xfer->flags_int.control_hdr) {
|
|
|
|
|
|
|
|
temp.func = &atmegadci_setup_rx;
|
|
|
|
temp.len = xfer->frlengths[0];
|
|
|
|
temp.pc = xfer->frbuffers + 0;
|
|
|
|
temp.short_pkt = temp.len ? 1 : 0;
|
2009-03-20 21:57:54 +00:00
|
|
|
/* check for last frame */
|
|
|
|
if (xfer->nframes == 1) {
|
|
|
|
/* no STATUS stage yet, SETUP is last */
|
|
|
|
if (xfer->flags_int.control_act)
|
|
|
|
temp.setup_alt_next = 0;
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
atmegadci_setup_standard_chain_sub(&temp);
|
|
|
|
}
|
|
|
|
x = 1;
|
|
|
|
} else {
|
|
|
|
x = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x != xfer->nframes) {
|
2009-06-07 19:41:11 +00:00
|
|
|
if (xfer->endpointno & UE_DIR_IN) {
|
2009-01-13 18:49:35 +00:00
|
|
|
temp.func = &atmegadci_data_tx;
|
|
|
|
need_sync = 1;
|
|
|
|
} else {
|
|
|
|
temp.func = &atmegadci_data_rx;
|
|
|
|
need_sync = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* setup "pc" pointer */
|
|
|
|
temp.pc = xfer->frbuffers + x;
|
|
|
|
} else {
|
|
|
|
need_sync = 0;
|
|
|
|
}
|
|
|
|
while (x != xfer->nframes) {
|
|
|
|
|
|
|
|
/* DATA0 / DATA1 message */
|
|
|
|
|
|
|
|
temp.len = xfer->frlengths[x];
|
|
|
|
|
|
|
|
x++;
|
|
|
|
|
|
|
|
if (x == xfer->nframes) {
|
2009-03-20 21:57:54 +00:00
|
|
|
if (xfer->flags_int.control_xfr) {
|
|
|
|
if (xfer->flags_int.control_act) {
|
|
|
|
temp.setup_alt_next = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
temp.setup_alt_next = 0;
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
if (temp.len == 0) {
|
|
|
|
|
|
|
|
/* make sure that we send an USB packet */
|
|
|
|
|
|
|
|
temp.short_pkt = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* regular data transfer */
|
|
|
|
|
|
|
|
temp.short_pkt = (xfer->flags.force_short_xfer) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
atmegadci_setup_standard_chain_sub(&temp);
|
|
|
|
|
|
|
|
if (xfer->flags_int.isochronous_xfr) {
|
|
|
|
temp.offset += temp.len;
|
|
|
|
} else {
|
|
|
|
/* get next Page Cache pointer */
|
|
|
|
temp.pc = xfer->frbuffers + x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-20 21:57:54 +00:00
|
|
|
if (xfer->flags_int.control_xfr) {
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-03-20 21:57:54 +00:00
|
|
|
/* always setup a valid "pc" pointer for status and sync */
|
|
|
|
temp.pc = xfer->frbuffers + 0;
|
2009-01-13 18:49:35 +00:00
|
|
|
temp.len = 0;
|
|
|
|
temp.short_pkt = 0;
|
2009-03-20 21:57:54 +00:00
|
|
|
temp.setup_alt_next = 0;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-03-20 21:57:54 +00:00
|
|
|
/* check if we need to sync */
|
2009-01-13 18:49:35 +00:00
|
|
|
if (need_sync) {
|
|
|
|
/* we need a SYNC point after TX */
|
|
|
|
temp.func = &atmegadci_data_tx_sync;
|
2009-03-20 21:57:54 +00:00
|
|
|
atmegadci_setup_standard_chain_sub(&temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if we should append a status stage */
|
|
|
|
if (!xfer->flags_int.control_act) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a DATA1 message and invert the current
|
|
|
|
* endpoint direction.
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
if (xfer->endpointno & UE_DIR_IN) {
|
2009-03-20 21:57:54 +00:00
|
|
|
temp.func = &atmegadci_data_rx;
|
|
|
|
need_sync = 0;
|
|
|
|
} else {
|
|
|
|
temp.func = &atmegadci_data_tx;
|
|
|
|
need_sync = 1;
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
atmegadci_setup_standard_chain_sub(&temp);
|
2009-03-20 21:57:54 +00:00
|
|
|
if (need_sync) {
|
|
|
|
/* we need a SYNC point after TX */
|
|
|
|
temp.func = &atmegadci_data_tx_sync;
|
|
|
|
atmegadci_setup_standard_chain_sub(&temp);
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* must have at least one frame! */
|
|
|
|
td = temp.td;
|
|
|
|
xfer->td_transfer_last = td;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_timeout(void *arg)
|
|
|
|
{
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_xfer *xfer = arg;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
DPRINTF("xfer=%p\n", xfer);
|
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(xfer->xroot->bus, MA_OWNED);
|
|
|
|
|
|
|
|
/* transfer is transferred */
|
|
|
|
atmegadci_device_done(xfer, USB_ERR_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_start_standard_chain(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
DPRINTFN(9, "\n");
|
|
|
|
|
|
|
|
/* poll one time - will turn on interrupts */
|
|
|
|
if (atmegadci_xfer_do_fifo(xfer)) {
|
|
|
|
|
|
|
|
/* put transfer on interrupt queue */
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_transfer_enqueue(&xfer->xroot->bus->intr_q, xfer);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* start timeout, if any */
|
|
|
|
if (xfer->timeout != 0) {
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_transfer_timeout_ms(xfer,
|
2009-01-13 18:49:35 +00:00
|
|
|
&atmegadci_timeout, xfer->timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_root_intr(struct atmegadci_softc *sc)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
DPRINTFN(9, "\n");
|
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
|
|
|
|
|
|
|
|
/* set port bit */
|
|
|
|
sc->sc_hub_idata[0] = 0x02; /* we only have one port */
|
|
|
|
|
2009-04-05 18:20:49 +00:00
|
|
|
uhub_root_intr(&sc->sc_bus, sc->sc_hub_idata,
|
|
|
|
sizeof(sc->sc_hub_idata));
|
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-05-29 18:46:57 +00:00
|
|
|
static usb_error_t
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_standard_done_sub(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_td *td;
|
|
|
|
uint32_t len;
|
|
|
|
uint8_t error;
|
|
|
|
|
|
|
|
DPRINTFN(9, "\n");
|
|
|
|
|
|
|
|
td = xfer->td_transfer_cache;
|
|
|
|
|
|
|
|
do {
|
|
|
|
len = td->remainder;
|
|
|
|
|
|
|
|
if (xfer->aframes != xfer->nframes) {
|
|
|
|
/*
|
|
|
|
* Verify the length and subtract
|
|
|
|
* the remainder from "frlengths[]":
|
|
|
|
*/
|
|
|
|
if (len > xfer->frlengths[xfer->aframes]) {
|
|
|
|
td->error = 1;
|
|
|
|
} else {
|
|
|
|
xfer->frlengths[xfer->aframes] -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check for transfer error */
|
|
|
|
if (td->error) {
|
|
|
|
/* the transfer is finished */
|
|
|
|
error = 1;
|
|
|
|
td = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Check for short transfer */
|
|
|
|
if (len > 0) {
|
2014-05-13 13:46:38 +00:00
|
|
|
if (xfer->flags_int.short_frames_ok ||
|
|
|
|
xfer->flags_int.isochronous_xfr) {
|
2009-01-13 18:49:35 +00:00
|
|
|
/* follow alt next */
|
|
|
|
if (td->alt_next) {
|
|
|
|
td = td->obj_next;
|
|
|
|
} else {
|
|
|
|
td = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* the transfer is finished */
|
|
|
|
td = NULL;
|
|
|
|
}
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
td = td->obj_next;
|
|
|
|
|
|
|
|
/* this USB frame is complete */
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
/* update transfer cache */
|
|
|
|
|
|
|
|
xfer->td_transfer_cache = td;
|
|
|
|
|
|
|
|
return (error ?
|
|
|
|
USB_ERR_STALLED : USB_ERR_NORMAL_COMPLETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_standard_done(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
2009-05-29 18:46:57 +00:00
|
|
|
usb_error_t err = 0;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
DPRINTFN(13, "xfer=%p endpoint=%p transfer done\n",
|
|
|
|
xfer, xfer->endpoint);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* reset scanner */
|
|
|
|
|
|
|
|
xfer->td_transfer_cache = xfer->td_transfer_first;
|
|
|
|
|
|
|
|
if (xfer->flags_int.control_xfr) {
|
|
|
|
|
|
|
|
if (xfer->flags_int.control_hdr) {
|
|
|
|
|
|
|
|
err = atmegadci_standard_done_sub(xfer);
|
|
|
|
}
|
|
|
|
xfer->aframes = 1;
|
|
|
|
|
|
|
|
if (xfer->td_transfer_cache == NULL) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (xfer->aframes != xfer->nframes) {
|
|
|
|
|
|
|
|
err = atmegadci_standard_done_sub(xfer);
|
|
|
|
xfer->aframes++;
|
|
|
|
|
|
|
|
if (xfer->td_transfer_cache == NULL) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xfer->flags_int.control_xfr &&
|
|
|
|
!xfer->flags_int.control_act) {
|
|
|
|
|
|
|
|
err = atmegadci_standard_done_sub(xfer);
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
atmegadci_device_done(xfer, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
|
|
|
* atmegadci_device_done
|
|
|
|
*
|
|
|
|
* NOTE: this function can be called more than one time on the
|
|
|
|
* same USB transfer!
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
static void
|
2009-05-29 18:46:57 +00:00
|
|
|
atmegadci_device_done(struct usb_xfer *xfer, usb_error_t error)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
|
|
|
|
uint8_t ep_no;
|
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
|
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
DPRINTFN(9, "xfer=%p, endpoint=%p, error=%d\n",
|
|
|
|
xfer, xfer->endpoint, error);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-05-21 00:04:17 +00:00
|
|
|
if (xfer->flags_int.usb_mode == USB_MODE_DEVICE) {
|
2009-06-07 19:41:11 +00:00
|
|
|
ep_no = (xfer->endpointno & UE_ADDR);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
|
|
|
|
|
|
|
|
/* disable endpoint interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
|
|
|
|
|
|
|
|
DPRINTFN(15, "disabled interrupts!\n");
|
|
|
|
}
|
|
|
|
/* dequeue transfer and start next transfer */
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_transfer_done(xfer, error);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-08-12 17:53:06 +00:00
|
|
|
atmegadci_xfer_stall(struct usb_xfer *xfer)
|
|
|
|
{
|
|
|
|
atmegadci_device_done(xfer, USB_ERR_STALLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_set_stall(struct usb_device *udev,
|
2009-06-27 21:23:30 +00:00
|
|
|
struct usb_endpoint *ep, uint8_t *did_stall)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
|
|
|
uint8_t ep_no;
|
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
|
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
DPRINTFN(5, "endpoint=%p\n", ep);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
sc = ATMEGA_BUS2SC(udev->bus);
|
|
|
|
/* get endpoint number */
|
2009-06-07 19:41:11 +00:00
|
|
|
ep_no = (ep->edesc->bEndpointAddress & UE_ADDR);
|
2009-01-13 18:49:35 +00:00
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
|
|
|
|
/* set stall */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_STALLRQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
atmegadci_clear_stall_sub(struct atmegadci_softc *sc, uint8_t ep_no,
|
|
|
|
uint8_t ep_type, uint8_t ep_dir)
|
|
|
|
{
|
|
|
|
uint8_t temp;
|
|
|
|
|
|
|
|
if (ep_type == UE_CONTROL) {
|
|
|
|
/* clearing stall is not needed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, ep_no);
|
|
|
|
|
|
|
|
/* set endpoint reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(ep_no));
|
|
|
|
|
|
|
|
/* clear endpoint reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
|
|
|
|
|
|
|
|
/* set stall */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_STALLRQ);
|
|
|
|
|
|
|
|
/* reset data toggle */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_RSTDT);
|
|
|
|
|
|
|
|
/* clear stall */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_STALLRQC);
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
do {
|
2009-01-13 18:49:35 +00:00
|
|
|
if (ep_type == UE_BULK) {
|
2009-05-20 17:00:55 +00:00
|
|
|
temp = ATMEGA_UECFG0X_EPTYPE2;
|
2009-01-13 18:49:35 +00:00
|
|
|
} else if (ep_type == UE_INTERRUPT) {
|
2009-05-20 17:00:55 +00:00
|
|
|
temp = ATMEGA_UECFG0X_EPTYPE3;
|
2009-01-13 18:49:35 +00:00
|
|
|
} else {
|
2009-05-20 17:00:55 +00:00
|
|
|
temp = ATMEGA_UECFG0X_EPTYPE1;
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
if (ep_dir & UE_DIR_IN) {
|
|
|
|
temp |= ATMEGA_UECFG0X_EPDIR;
|
|
|
|
}
|
|
|
|
/* two banks, 64-bytes wMaxPacket */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X, temp);
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
|
|
|
|
ATMEGA_UECFG1X_ALLOC |
|
2009-04-22 17:08:13 +00:00
|
|
|
ATMEGA_UECFG1X_EPBK0 | /* one bank */
|
2009-03-11 04:58:21 +00:00
|
|
|
ATMEGA_UECFG1X_EPSIZE(3));
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
|
|
|
|
if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
|
2009-11-26 00:43:17 +00:00
|
|
|
device_printf(sc->sc_bus.bdev,
|
|
|
|
"Chip rejected configuration\n");
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
2009-03-11 04:58:21 +00:00
|
|
|
} while (0);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-07 19:41:11 +00:00
|
|
|
atmegadci_clear_stall(struct usb_device *udev, struct usb_endpoint *ep)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc;
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_endpoint_descriptor *ed;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
DPRINTFN(5, "endpoint=%p\n", ep);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
|
|
|
|
|
|
|
|
/* check mode */
|
2009-05-21 00:04:17 +00:00
|
|
|
if (udev->flags.usb_mode != USB_MODE_DEVICE) {
|
2009-01-13 18:49:35 +00:00
|
|
|
/* not supported */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* get softc */
|
|
|
|
sc = ATMEGA_BUS2SC(udev->bus);
|
|
|
|
|
|
|
|
/* get endpoint descriptor */
|
2009-06-07 19:41:11 +00:00
|
|
|
ed = ep->edesc;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* reset endpoint */
|
|
|
|
atmegadci_clear_stall_sub(sc,
|
|
|
|
(ed->bEndpointAddress & UE_ADDR),
|
|
|
|
(ed->bmAttributes & UE_XFERTYPE),
|
|
|
|
(ed->bEndpointAddress & (UE_DIR_IN | UE_DIR_OUT)));
|
|
|
|
}
|
|
|
|
|
2009-05-29 18:46:57 +00:00
|
|
|
usb_error_t
|
2009-01-13 18:49:35 +00:00
|
|
|
atmegadci_init(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
uint8_t n;
|
|
|
|
|
|
|
|
DPRINTF("start\n");
|
|
|
|
|
|
|
|
/* set up the bus structure */
|
|
|
|
sc->sc_bus.usbrev = USB_REV_1_1;
|
|
|
|
sc->sc_bus.methods = &atmegadci_bus_methods;
|
|
|
|
|
|
|
|
USB_BUS_LOCK(&sc->sc_bus);
|
2009-05-20 17:00:55 +00:00
|
|
|
|
|
|
|
/* make sure USB is enabled */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
|
|
|
|
ATMEGA_USBCON_USBE |
|
|
|
|
ATMEGA_USBCON_FRZCLK);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* enable USB PAD regulator */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UHWCON,
|
2009-05-20 17:00:55 +00:00
|
|
|
ATMEGA_UHWCON_UVREGE |
|
|
|
|
ATMEGA_UHWCON_UIMOD);
|
|
|
|
|
|
|
|
/* the following register sets up the USB PLL, assuming 16MHz X-tal */
|
|
|
|
ATMEGA_WRITE_1(sc, 0x49 /* PLLCSR */, 0x14 | 0x02);
|
|
|
|
|
|
|
|
/* wait for PLL to lock */
|
|
|
|
for (n = 0; n != 20; n++) {
|
|
|
|
if (ATMEGA_READ_1(sc, 0x49) & 0x01)
|
|
|
|
break;
|
|
|
|
/* wait a little bit for PLL to start */
|
2009-06-15 01:02:43 +00:00
|
|
|
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 100);
|
2009-05-20 17:00:55 +00:00
|
|
|
}
|
|
|
|
|
2009-04-05 18:18:07 +00:00
|
|
|
/* make sure USB is enabled */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_USBCON,
|
|
|
|
ATMEGA_USBCON_USBE |
|
|
|
|
ATMEGA_USBCON_OTGPADE |
|
|
|
|
ATMEGA_USBCON_VBUSTE);
|
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
/* turn on clocks */
|
|
|
|
(sc->sc_clocks_on) (&sc->sc_bus);
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
/* make sure device is re-enumerated */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDCON, ATMEGA_UDCON_DETACH);
|
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
/* wait a little for things to stabilise */
|
2009-06-15 01:02:43 +00:00
|
|
|
usb_pause_mtx(&sc->sc_bus.bus_mtx, hz / 20);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* enable interrupts */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDIEN,
|
|
|
|
ATMEGA_UDINT_SUSPE |
|
|
|
|
ATMEGA_UDINT_EORSTE);
|
|
|
|
|
|
|
|
/* reset all endpoints */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST,
|
|
|
|
(1 << ATMEGA_EP_MAX) - 1);
|
|
|
|
|
|
|
|
/* disable reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
|
|
|
|
|
|
|
|
/* disable all endpoints */
|
2009-03-11 04:58:21 +00:00
|
|
|
for (n = 0; n != ATMEGA_EP_MAX; n++) {
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* select endpoint */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, n);
|
|
|
|
|
|
|
|
/* disable endpoint interrupt */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UEIENX, 0);
|
|
|
|
|
|
|
|
/* disable endpoint */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* turn off clocks */
|
|
|
|
|
|
|
|
atmegadci_clocks_off(sc);
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
/* read initial VBUS state */
|
|
|
|
|
|
|
|
n = ATMEGA_READ_1(sc, ATMEGA_USBSTA);
|
|
|
|
atmegadci_vbus_interrupt(sc, n & ATMEGA_USBSTA_VBUS);
|
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
USB_BUS_UNLOCK(&sc->sc_bus);
|
|
|
|
|
|
|
|
/* catch any lost interrupts */
|
|
|
|
|
|
|
|
atmegadci_do_poll(&sc->sc_bus);
|
|
|
|
|
|
|
|
return (0); /* success */
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
atmegadci_uninit(struct atmegadci_softc *sc)
|
|
|
|
{
|
|
|
|
USB_BUS_LOCK(&sc->sc_bus);
|
|
|
|
|
|
|
|
/* turn on clocks */
|
|
|
|
(sc->sc_clocks_on) (&sc->sc_bus);
|
|
|
|
|
|
|
|
/* disable interrupts */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UDIEN, 0);
|
|
|
|
|
|
|
|
/* reset all endpoints */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST,
|
|
|
|
(1 << ATMEGA_EP_MAX) - 1);
|
|
|
|
|
|
|
|
/* disable reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
|
|
|
|
|
|
|
|
sc->sc_flags.port_powered = 0;
|
|
|
|
sc->sc_flags.status_vbus = 0;
|
|
|
|
sc->sc_flags.status_bus_reset = 0;
|
|
|
|
sc->sc_flags.status_suspend = 0;
|
|
|
|
sc->sc_flags.change_suspend = 0;
|
|
|
|
sc->sc_flags.change_connect = 1;
|
|
|
|
|
|
|
|
atmegadci_pull_down(sc);
|
|
|
|
atmegadci_clocks_off(sc);
|
|
|
|
|
|
|
|
/* disable USB PAD regulator */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UHWCON, 0);
|
|
|
|
|
|
|
|
USB_BUS_UNLOCK(&sc->sc_bus);
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:28:54 +00:00
|
|
|
static void
|
2009-01-13 18:49:35 +00:00
|
|
|
atmegadci_suspend(struct atmegadci_softc *sc)
|
|
|
|
{
|
2011-12-14 00:28:54 +00:00
|
|
|
/* TODO */
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:28:54 +00:00
|
|
|
static void
|
2009-01-13 18:49:35 +00:00
|
|
|
atmegadci_resume(struct atmegadci_softc *sc)
|
|
|
|
{
|
2011-12-14 00:28:54 +00:00
|
|
|
/* TODO */
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_do_poll(struct usb_bus *bus)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
|
|
|
|
|
|
|
|
USB_BUS_LOCK(&sc->sc_bus);
|
|
|
|
atmegadci_interrupt_poll(sc);
|
|
|
|
USB_BUS_UNLOCK(&sc->sc_bus);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2014-05-13 13:46:38 +00:00
|
|
|
* atmegadci bulk support
|
|
|
|
* atmegadci control support
|
|
|
|
* atmegadci interrupt support
|
2009-01-13 18:49:35 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_non_isoc_open(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_non_isoc_close(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
atmegadci_device_done(xfer, USB_ERR_CANCELLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_non_isoc_enter(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_non_isoc_start(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
/* setup TDs */
|
|
|
|
atmegadci_setup_standard_chain(xfer);
|
|
|
|
atmegadci_start_standard_chain(xfer);
|
|
|
|
}
|
|
|
|
|
2013-12-11 13:20:32 +00:00
|
|
|
static const struct usb_pipe_methods atmegadci_device_non_isoc_methods =
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
2009-04-05 18:21:08 +00:00
|
|
|
.open = atmegadci_device_non_isoc_open,
|
|
|
|
.close = atmegadci_device_non_isoc_close,
|
|
|
|
.enter = atmegadci_device_non_isoc_enter,
|
|
|
|
.start = atmegadci_device_non_isoc_start,
|
2009-01-13 18:49:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2014-05-13 13:46:38 +00:00
|
|
|
* atmegadci full speed isochronous support
|
2009-01-13 18:49:35 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_isoc_fs_open(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_isoc_fs_close(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
atmegadci_device_done(xfer, USB_ERR_CANCELLED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_isoc_fs_enter(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(xfer->xroot->bus);
|
|
|
|
uint32_t temp;
|
|
|
|
uint32_t nframes;
|
|
|
|
|
|
|
|
DPRINTFN(6, "xfer=%p next=%d nframes=%d\n",
|
2009-06-07 19:41:11 +00:00
|
|
|
xfer, xfer->endpoint->isoc_next, xfer->nframes);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* get the current frame index */
|
|
|
|
|
|
|
|
nframes =
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UDFNUMH) << 8) |
|
|
|
|
(ATMEGA_READ_1(sc, ATMEGA_UDFNUML));
|
|
|
|
|
|
|
|
nframes &= ATMEGA_FRAME_MASK;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check if the frame index is within the window where the frames
|
|
|
|
* will be inserted
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
temp = (nframes - xfer->endpoint->isoc_next) & ATMEGA_FRAME_MASK;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
if ((xfer->endpoint->is_synced == 0) ||
|
2009-01-13 18:49:35 +00:00
|
|
|
(temp < xfer->nframes)) {
|
|
|
|
/*
|
|
|
|
* If there is data underflow or the pipe queue is
|
|
|
|
* empty we schedule the transfer a few frames ahead
|
|
|
|
* of the current frame position. Else two isochronous
|
|
|
|
* transfers might overlap.
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
xfer->endpoint->isoc_next = (nframes + 3) & ATMEGA_FRAME_MASK;
|
|
|
|
xfer->endpoint->is_synced = 1;
|
|
|
|
DPRINTFN(3, "start next=%d\n", xfer->endpoint->isoc_next);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* compute how many milliseconds the insertion is ahead of the
|
|
|
|
* current frame position:
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
temp = (xfer->endpoint->isoc_next - nframes) & ATMEGA_FRAME_MASK;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* pre-compute when the isochronous transfer will be finished:
|
|
|
|
*/
|
|
|
|
xfer->isoc_time_complete =
|
2009-06-15 01:02:43 +00:00
|
|
|
usb_isoc_time_expand(&sc->sc_bus, nframes) + temp +
|
2009-01-13 18:49:35 +00:00
|
|
|
xfer->nframes;
|
|
|
|
|
|
|
|
/* compute frame number for next insertion */
|
2009-06-07 19:41:11 +00:00
|
|
|
xfer->endpoint->isoc_next += xfer->nframes;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* setup TDs */
|
|
|
|
atmegadci_setup_standard_chain(xfer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_device_isoc_fs_start(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
/* start TD chain */
|
|
|
|
atmegadci_start_standard_chain(xfer);
|
|
|
|
}
|
|
|
|
|
2013-12-11 13:20:32 +00:00
|
|
|
static const struct usb_pipe_methods atmegadci_device_isoc_fs_methods =
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
.open = atmegadci_device_isoc_fs_open,
|
|
|
|
.close = atmegadci_device_isoc_fs_close,
|
|
|
|
.enter = atmegadci_device_isoc_fs_enter,
|
|
|
|
.start = atmegadci_device_isoc_fs_start,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*
|
2014-05-13 13:46:38 +00:00
|
|
|
* atmegadci root control support
|
2009-01-13 18:49:35 +00:00
|
|
|
*------------------------------------------------------------------------*
|
2009-04-05 18:20:49 +00:00
|
|
|
* Simulate a hardware HUB by handling all the necessary requests.
|
2009-01-13 18:49:35 +00:00
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
2009-05-28 17:36:36 +00:00
|
|
|
static const struct usb_device_descriptor atmegadci_devd = {
|
|
|
|
.bLength = sizeof(struct usb_device_descriptor),
|
2009-01-13 18:49:35 +00:00
|
|
|
.bDescriptorType = UDESC_DEVICE,
|
|
|
|
.bcdUSB = {0x00, 0x02},
|
|
|
|
.bDeviceClass = UDCLASS_HUB,
|
|
|
|
.bDeviceSubClass = UDSUBCLASS_HUB,
|
2010-10-13 20:37:19 +00:00
|
|
|
.bDeviceProtocol = UDPROTO_FSHUB,
|
2009-01-13 18:49:35 +00:00
|
|
|
.bMaxPacketSize = 64,
|
|
|
|
.bcdDevice = {0x00, 0x01},
|
|
|
|
.iManufacturer = 1,
|
|
|
|
.iProduct = 2,
|
|
|
|
.bNumConfigurations = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct atmegadci_config_desc atmegadci_confd = {
|
|
|
|
.confd = {
|
2009-05-28 17:36:36 +00:00
|
|
|
.bLength = sizeof(struct usb_config_descriptor),
|
2009-01-13 18:49:35 +00:00
|
|
|
.bDescriptorType = UDESC_CONFIG,
|
|
|
|
.wTotalLength[0] = sizeof(atmegadci_confd),
|
|
|
|
.bNumInterface = 1,
|
|
|
|
.bConfigurationValue = 1,
|
|
|
|
.iConfiguration = 0,
|
|
|
|
.bmAttributes = UC_SELF_POWERED,
|
|
|
|
.bMaxPower = 0,
|
|
|
|
},
|
|
|
|
.ifcd = {
|
2009-05-28 17:36:36 +00:00
|
|
|
.bLength = sizeof(struct usb_interface_descriptor),
|
2009-01-13 18:49:35 +00:00
|
|
|
.bDescriptorType = UDESC_INTERFACE,
|
|
|
|
.bNumEndpoints = 1,
|
|
|
|
.bInterfaceClass = UICLASS_HUB,
|
|
|
|
.bInterfaceSubClass = UISUBCLASS_HUB,
|
2010-10-13 20:37:19 +00:00
|
|
|
.bInterfaceProtocol = 0,
|
2009-01-13 18:49:35 +00:00
|
|
|
},
|
|
|
|
.endpd = {
|
2009-05-28 17:36:36 +00:00
|
|
|
.bLength = sizeof(struct usb_endpoint_descriptor),
|
2009-01-13 18:49:35 +00:00
|
|
|
.bDescriptorType = UDESC_ENDPOINT,
|
|
|
|
.bEndpointAddress = (UE_DIR_IN | ATMEGA_INTR_ENDPT),
|
|
|
|
.bmAttributes = UE_INTERRUPT,
|
|
|
|
.wMaxPacketSize[0] = 8,
|
|
|
|
.bInterval = 255,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-04-02 10:50:42 +00:00
|
|
|
#define HSETW(ptr, val) ptr = { (uint8_t)(val), (uint8_t)((val) >> 8) }
|
|
|
|
|
2009-05-28 17:36:36 +00:00
|
|
|
static const struct usb_hub_descriptor_min atmegadci_hubd = {
|
2009-01-13 18:49:35 +00:00
|
|
|
.bDescLength = sizeof(atmegadci_hubd),
|
|
|
|
.bDescriptorType = UDESC_HUB,
|
|
|
|
.bNbrPorts = 1,
|
2012-04-02 10:50:42 +00:00
|
|
|
HSETW(.wHubCharacteristics, (UHD_PWR_NO_SWITCH | UHD_OC_INDIVIDUAL)),
|
2009-01-13 18:49:35 +00:00
|
|
|
.bPwrOn2PwrGood = 50,
|
|
|
|
.bHubContrCurrent = 0,
|
|
|
|
.DeviceRemovable = {0}, /* port is removable */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define STRING_VENDOR \
|
2013-01-30 16:05:54 +00:00
|
|
|
"A\0T\0M\0E\0G\0A"
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
#define STRING_PRODUCT \
|
2013-01-30 16:05:54 +00:00
|
|
|
"D\0C\0I\0 \0R\0o\0o\0t\0 \0H\0U\0B"
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
USB_MAKE_STRING_DESC(STRING_VENDOR, atmegadci_vendor);
|
|
|
|
USB_MAKE_STRING_DESC(STRING_PRODUCT, atmegadci_product);
|
|
|
|
|
2009-05-29 18:46:57 +00:00
|
|
|
static usb_error_t
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_roothub_exec(struct usb_device *udev,
|
|
|
|
struct usb_device_request *req, const void **pptr, uint16_t *plength)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
2009-04-22 17:08:16 +00:00
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
|
|
|
|
const void *ptr;
|
|
|
|
uint16_t len;
|
2009-01-13 18:49:35 +00:00
|
|
|
uint16_t value;
|
|
|
|
uint16_t index;
|
2009-03-11 04:58:21 +00:00
|
|
|
uint8_t temp;
|
2009-05-29 18:46:57 +00:00
|
|
|
usb_error_t err;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
USB_BUS_LOCK_ASSERT(&sc->sc_bus, MA_OWNED);
|
|
|
|
|
|
|
|
/* buffer reset */
|
2009-04-22 17:08:16 +00:00
|
|
|
ptr = (const void *)&sc->sc_hub_temp;
|
|
|
|
len = 0;
|
|
|
|
err = 0;
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-04-22 17:08:16 +00:00
|
|
|
value = UGETW(req->wValue);
|
|
|
|
index = UGETW(req->wIndex);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/* demultiplex the control request */
|
|
|
|
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bmRequestType) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UT_READ_DEVICE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_GET_DESCRIPTOR:
|
|
|
|
goto tr_handle_get_descriptor;
|
|
|
|
case UR_GET_CONFIG:
|
|
|
|
goto tr_handle_get_config;
|
|
|
|
case UR_GET_STATUS:
|
|
|
|
goto tr_handle_get_status;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_DEVICE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_SET_ADDRESS:
|
|
|
|
goto tr_handle_set_address;
|
|
|
|
case UR_SET_CONFIG:
|
|
|
|
goto tr_handle_set_config;
|
|
|
|
case UR_CLEAR_FEATURE:
|
|
|
|
goto tr_valid; /* nop */
|
|
|
|
case UR_SET_DESCRIPTOR:
|
|
|
|
goto tr_valid; /* nop */
|
|
|
|
case UR_SET_FEATURE:
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_ENDPOINT:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_CLEAR_FEATURE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (UGETW(req->wValue)) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UF_ENDPOINT_HALT:
|
|
|
|
goto tr_handle_clear_halt;
|
|
|
|
case UF_DEVICE_REMOTE_WAKEUP:
|
|
|
|
goto tr_handle_clear_wakeup;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UR_SET_FEATURE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (UGETW(req->wValue)) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UF_ENDPOINT_HALT:
|
|
|
|
goto tr_handle_set_halt;
|
|
|
|
case UF_DEVICE_REMOTE_WAKEUP:
|
|
|
|
goto tr_handle_set_wakeup;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UR_SYNCH_FRAME:
|
|
|
|
goto tr_valid; /* nop */
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_READ_ENDPOINT:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_GET_STATUS:
|
|
|
|
goto tr_handle_get_ep_status;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_INTERFACE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_SET_INTERFACE:
|
|
|
|
goto tr_handle_set_interface;
|
|
|
|
case UR_CLEAR_FEATURE:
|
|
|
|
goto tr_valid; /* nop */
|
|
|
|
case UR_SET_FEATURE:
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_READ_INTERFACE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_GET_INTERFACE:
|
|
|
|
goto tr_handle_get_interface;
|
|
|
|
case UR_GET_STATUS:
|
|
|
|
goto tr_handle_get_iface_status;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_CLASS_INTERFACE:
|
|
|
|
case UT_WRITE_VENDOR_INTERFACE:
|
|
|
|
/* XXX forward */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_READ_CLASS_INTERFACE:
|
|
|
|
case UT_READ_VENDOR_INTERFACE:
|
|
|
|
/* XXX forward */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_CLASS_DEVICE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_CLEAR_FEATURE:
|
|
|
|
goto tr_valid;
|
|
|
|
case UR_SET_DESCRIPTOR:
|
|
|
|
case UR_SET_FEATURE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_WRITE_CLASS_OTHER:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_CLEAR_FEATURE:
|
|
|
|
goto tr_handle_clear_port_feature;
|
|
|
|
case UR_SET_FEATURE:
|
|
|
|
goto tr_handle_set_port_feature;
|
|
|
|
case UR_CLEAR_TT_BUFFER:
|
|
|
|
case UR_RESET_TT:
|
|
|
|
case UR_STOP_TT:
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_READ_CLASS_OTHER:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_GET_TT_STATE:
|
|
|
|
goto tr_handle_get_tt_state;
|
|
|
|
case UR_GET_STATUS:
|
|
|
|
goto tr_handle_get_port_status;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UT_READ_CLASS_DEVICE:
|
2009-04-22 17:08:16 +00:00
|
|
|
switch (req->bRequest) {
|
2009-01-13 18:49:35 +00:00
|
|
|
case UR_GET_DESCRIPTOR:
|
|
|
|
goto tr_handle_get_class_descriptor;
|
|
|
|
case UR_GET_STATUS:
|
|
|
|
goto tr_handle_get_class_status;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_descriptor:
|
|
|
|
switch (value >> 8) {
|
|
|
|
case UDESC_DEVICE:
|
|
|
|
if (value & 0xff) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
2009-04-22 17:08:16 +00:00
|
|
|
len = sizeof(atmegadci_devd);
|
|
|
|
ptr = (const void *)&atmegadci_devd;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
case UDESC_CONFIG:
|
|
|
|
if (value & 0xff) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
2009-04-22 17:08:16 +00:00
|
|
|
len = sizeof(atmegadci_confd);
|
|
|
|
ptr = (const void *)&atmegadci_confd;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
case UDESC_STRING:
|
|
|
|
switch (value & 0xff) {
|
|
|
|
case 0: /* Language table */
|
2013-01-30 15:46:26 +00:00
|
|
|
len = sizeof(usb_string_lang_en);
|
|
|
|
ptr = (const void *)&usb_string_lang_en;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
case 1: /* Vendor */
|
2009-04-22 17:08:16 +00:00
|
|
|
len = sizeof(atmegadci_vendor);
|
|
|
|
ptr = (const void *)&atmegadci_vendor;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
case 2: /* Product */
|
2009-04-22 17:08:16 +00:00
|
|
|
len = sizeof(atmegadci_product);
|
|
|
|
ptr = (const void *)&atmegadci_product;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
goto tr_stalled;
|
|
|
|
|
|
|
|
tr_handle_get_config:
|
2009-04-22 17:08:16 +00:00
|
|
|
len = 1;
|
2009-01-13 18:49:35 +00:00
|
|
|
sc->sc_hub_temp.wValue[0] = sc->sc_conf;
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_status:
|
2009-04-22 17:08:16 +00:00
|
|
|
len = 2;
|
2009-01-13 18:49:35 +00:00
|
|
|
USETW(sc->sc_hub_temp.wValue, UDS_SELF_POWERED);
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_set_address:
|
|
|
|
if (value & 0xFF00) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
sc->sc_rt_addr = value;
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_set_config:
|
|
|
|
if (value >= 2) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
sc->sc_conf = value;
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_interface:
|
2009-04-22 17:08:16 +00:00
|
|
|
len = 1;
|
2009-01-13 18:49:35 +00:00
|
|
|
sc->sc_hub_temp.wValue[0] = 0;
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_tt_state:
|
|
|
|
tr_handle_get_class_status:
|
|
|
|
tr_handle_get_iface_status:
|
|
|
|
tr_handle_get_ep_status:
|
2009-04-22 17:08:16 +00:00
|
|
|
len = 2;
|
2009-01-13 18:49:35 +00:00
|
|
|
USETW(sc->sc_hub_temp.wValue, 0);
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_set_halt:
|
|
|
|
tr_handle_set_interface:
|
|
|
|
tr_handle_set_wakeup:
|
|
|
|
tr_handle_clear_wakeup:
|
|
|
|
tr_handle_clear_halt:
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_clear_port_feature:
|
|
|
|
if (index != 1) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
DPRINTFN(9, "UR_CLEAR_PORT_FEATURE on port %d\n", index);
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case UHF_PORT_SUSPEND:
|
2009-04-05 18:20:49 +00:00
|
|
|
atmegadci_wakeup_peer(sc);
|
2009-01-13 18:49:35 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case UHF_PORT_ENABLE:
|
|
|
|
sc->sc_flags.port_enabled = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UHF_PORT_TEST:
|
|
|
|
case UHF_PORT_INDICATOR:
|
|
|
|
case UHF_C_PORT_ENABLE:
|
|
|
|
case UHF_C_PORT_OVER_CURRENT:
|
|
|
|
case UHF_C_PORT_RESET:
|
|
|
|
/* nops */
|
|
|
|
break;
|
|
|
|
case UHF_PORT_POWER:
|
|
|
|
sc->sc_flags.port_powered = 0;
|
|
|
|
atmegadci_pull_down(sc);
|
|
|
|
atmegadci_clocks_off(sc);
|
|
|
|
break;
|
|
|
|
case UHF_C_PORT_CONNECTION:
|
2009-03-11 04:58:21 +00:00
|
|
|
/* clear connect change flag */
|
2009-01-13 18:49:35 +00:00
|
|
|
sc->sc_flags.change_connect = 0;
|
2009-03-11 04:58:21 +00:00
|
|
|
|
2009-05-20 17:00:55 +00:00
|
|
|
if (!sc->sc_flags.status_bus_reset) {
|
|
|
|
/* we are not connected */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-11 04:58:21 +00:00
|
|
|
/* configure the control endpoint */
|
|
|
|
|
|
|
|
/* select endpoint number */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UENUM, 0);
|
|
|
|
|
|
|
|
/* set endpoint reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, ATMEGA_UERST_MASK(0));
|
|
|
|
|
|
|
|
/* clear endpoint reset */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UERST, 0);
|
|
|
|
|
|
|
|
/* enable and stall endpoint */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECONX,
|
|
|
|
ATMEGA_UECONX_EPEN |
|
|
|
|
ATMEGA_UECONX_STALLRQ);
|
|
|
|
|
|
|
|
/* one bank, 64-bytes wMaxPacket */
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECFG0X,
|
|
|
|
ATMEGA_UECFG0X_EPTYPE0);
|
|
|
|
ATMEGA_WRITE_1(sc, ATMEGA_UECFG1X,
|
|
|
|
ATMEGA_UECFG1X_ALLOC |
|
|
|
|
ATMEGA_UECFG1X_EPBK0 |
|
|
|
|
ATMEGA_UECFG1X_EPSIZE(3));
|
|
|
|
|
|
|
|
/* check valid config */
|
|
|
|
temp = ATMEGA_READ_1(sc, ATMEGA_UESTA0X);
|
|
|
|
if (!(temp & ATMEGA_UESTA0X_CFGOK)) {
|
2009-11-26 00:43:17 +00:00
|
|
|
device_printf(sc->sc_bus.bdev,
|
|
|
|
"Chip rejected EP0 configuration\n");
|
2009-03-11 04:58:21 +00:00
|
|
|
}
|
2009-01-13 18:49:35 +00:00
|
|
|
break;
|
|
|
|
case UHF_C_PORT_SUSPEND:
|
|
|
|
sc->sc_flags.change_suspend = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2009-04-22 17:08:16 +00:00
|
|
|
err = USB_ERR_IOERROR;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_set_port_feature:
|
|
|
|
if (index != 1) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
DPRINTFN(9, "UR_SET_PORT_FEATURE\n");
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case UHF_PORT_ENABLE:
|
|
|
|
sc->sc_flags.port_enabled = 1;
|
|
|
|
break;
|
|
|
|
case UHF_PORT_SUSPEND:
|
|
|
|
case UHF_PORT_RESET:
|
|
|
|
case UHF_PORT_TEST:
|
|
|
|
case UHF_PORT_INDICATOR:
|
|
|
|
/* nops */
|
|
|
|
break;
|
|
|
|
case UHF_PORT_POWER:
|
|
|
|
sc->sc_flags.port_powered = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2009-04-22 17:08:16 +00:00
|
|
|
err = USB_ERR_IOERROR;
|
2009-01-13 18:49:35 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_port_status:
|
|
|
|
|
|
|
|
DPRINTFN(9, "UR_GET_PORT_STATUS\n");
|
|
|
|
|
|
|
|
if (index != 1) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
|
|
|
if (sc->sc_flags.status_vbus) {
|
|
|
|
atmegadci_clocks_on(sc);
|
|
|
|
atmegadci_pull_up(sc);
|
|
|
|
} else {
|
|
|
|
atmegadci_pull_down(sc);
|
|
|
|
atmegadci_clocks_off(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Select FULL-speed and Device Side Mode */
|
|
|
|
|
|
|
|
value = UPS_PORT_MODE_DEVICE;
|
|
|
|
|
|
|
|
if (sc->sc_flags.port_powered) {
|
|
|
|
value |= UPS_PORT_POWER;
|
|
|
|
}
|
|
|
|
if (sc->sc_flags.port_enabled) {
|
|
|
|
value |= UPS_PORT_ENABLED;
|
|
|
|
}
|
|
|
|
if (sc->sc_flags.status_vbus &&
|
|
|
|
sc->sc_flags.status_bus_reset) {
|
|
|
|
value |= UPS_CURRENT_CONNECT_STATUS;
|
|
|
|
}
|
|
|
|
if (sc->sc_flags.status_suspend) {
|
|
|
|
value |= UPS_SUSPEND;
|
|
|
|
}
|
|
|
|
USETW(sc->sc_hub_temp.ps.wPortStatus, value);
|
|
|
|
|
|
|
|
value = 0;
|
|
|
|
|
|
|
|
if (sc->sc_flags.change_connect) {
|
|
|
|
value |= UPS_C_CONNECT_STATUS;
|
|
|
|
}
|
|
|
|
if (sc->sc_flags.change_suspend) {
|
|
|
|
value |= UPS_C_SUSPEND;
|
|
|
|
}
|
|
|
|
USETW(sc->sc_hub_temp.ps.wPortChange, value);
|
2009-04-22 17:08:16 +00:00
|
|
|
len = sizeof(sc->sc_hub_temp.ps);
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_handle_get_class_descriptor:
|
|
|
|
if (value & 0xFF) {
|
|
|
|
goto tr_stalled;
|
|
|
|
}
|
2009-04-22 17:08:16 +00:00
|
|
|
ptr = (const void *)&atmegadci_hubd;
|
|
|
|
len = sizeof(atmegadci_hubd);
|
2009-01-13 18:49:35 +00:00
|
|
|
goto tr_valid;
|
|
|
|
|
|
|
|
tr_stalled:
|
2009-04-22 17:08:16 +00:00
|
|
|
err = USB_ERR_STALLED;
|
2009-01-13 18:49:35 +00:00
|
|
|
tr_valid:
|
|
|
|
done:
|
2009-04-22 17:08:16 +00:00
|
|
|
*plength = len;
|
|
|
|
*pptr = ptr;
|
|
|
|
return (err);
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_xfer_setup(struct usb_setup_params *parm)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
2009-05-28 17:36:36 +00:00
|
|
|
const struct usb_hw_ep_profile *pf;
|
2009-01-13 18:49:35 +00:00
|
|
|
struct atmegadci_softc *sc;
|
2009-05-28 17:36:36 +00:00
|
|
|
struct usb_xfer *xfer;
|
2009-01-13 18:49:35 +00:00
|
|
|
void *last_obj;
|
|
|
|
uint32_t ntd;
|
|
|
|
uint32_t n;
|
|
|
|
uint8_t ep_no;
|
|
|
|
|
|
|
|
sc = ATMEGA_BUS2SC(parm->udev->bus);
|
|
|
|
xfer = parm->curr_xfer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: This driver does not use any of the parameters that
|
|
|
|
* are computed from the following values. Just set some
|
|
|
|
* reasonable dummies:
|
|
|
|
*/
|
|
|
|
parm->hc_max_packet_size = 0x500;
|
|
|
|
parm->hc_max_packet_count = 1;
|
|
|
|
parm->hc_max_frame_size = 0x500;
|
|
|
|
|
2009-06-15 01:02:43 +00:00
|
|
|
usbd_transfer_setup_sub(parm);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* compute maximum number of TDs
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
if ((xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) == UE_CONTROL) {
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-04-05 18:21:08 +00:00
|
|
|
ntd = xfer->nframes + 1 /* STATUS */ + 1 /* SYNC 1 */
|
2009-01-13 18:49:35 +00:00
|
|
|
+ 1 /* SYNC 2 */ ;
|
|
|
|
} else {
|
|
|
|
|
2009-04-05 18:21:08 +00:00
|
|
|
ntd = xfer->nframes + 1 /* SYNC */ ;
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-15 01:02:43 +00:00
|
|
|
* check if "usbd_transfer_setup_sub" set an error
|
2009-01-13 18:49:35 +00:00
|
|
|
*/
|
2009-04-05 18:21:08 +00:00
|
|
|
if (parm->err)
|
2009-01-13 18:49:35 +00:00
|
|
|
return;
|
2009-04-05 18:21:08 +00:00
|
|
|
|
2009-01-13 18:49:35 +00:00
|
|
|
/*
|
|
|
|
* allocate transfer descriptors
|
|
|
|
*/
|
|
|
|
last_obj = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get profile stuff
|
|
|
|
*/
|
2009-06-07 19:41:11 +00:00
|
|
|
ep_no = xfer->endpointno & UE_ADDR;
|
2009-04-05 18:21:08 +00:00
|
|
|
atmegadci_get_hw_ep_profile(parm->udev, &pf, ep_no);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-04-05 18:21:08 +00:00
|
|
|
if (pf == NULL) {
|
|
|
|
/* should not happen */
|
|
|
|
parm->err = USB_ERR_INVAL;
|
|
|
|
return;
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* align data */
|
|
|
|
parm->size[0] += ((-parm->size[0]) & (USB_HOST_ALIGN - 1));
|
|
|
|
|
|
|
|
for (n = 0; n != ntd; n++) {
|
|
|
|
|
|
|
|
struct atmegadci_td *td;
|
|
|
|
|
|
|
|
if (parm->buf) {
|
|
|
|
|
|
|
|
td = USB_ADD_BYTES(parm->buf, parm->size[0]);
|
|
|
|
|
|
|
|
/* init TD */
|
|
|
|
td->max_packet_size = xfer->max_packet_size;
|
|
|
|
td->ep_no = ep_no;
|
|
|
|
if (pf->support_multi_buffer) {
|
|
|
|
td->support_multi_buffer = 1;
|
|
|
|
}
|
|
|
|
td->obj_next = last_obj;
|
|
|
|
|
|
|
|
last_obj = td;
|
|
|
|
}
|
|
|
|
parm->size[0] += sizeof(*td);
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer->td_start[0] = last_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-05-28 17:36:36 +00:00
|
|
|
atmegadci_xfer_unsetup(struct usb_xfer *xfer)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-07 19:41:11 +00:00
|
|
|
atmegadci_ep_init(struct usb_device *udev, struct usb_endpoint_descriptor *edesc,
|
|
|
|
struct usb_endpoint *ep)
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(udev->bus);
|
|
|
|
|
2009-06-07 19:41:11 +00:00
|
|
|
DPRINTFN(2, "endpoint=%p, addr=%d, endpt=%d, mode=%d (%d,%d)\n",
|
|
|
|
ep, udev->address,
|
2009-05-21 00:04:17 +00:00
|
|
|
edesc->bEndpointAddress, udev->flags.usb_mode,
|
2009-03-11 04:58:21 +00:00
|
|
|
sc->sc_rt_addr, udev->device_index);
|
2009-01-13 18:49:35 +00:00
|
|
|
|
2009-04-05 18:20:49 +00:00
|
|
|
if (udev->device_index != sc->sc_rt_addr) {
|
2009-01-13 18:49:35 +00:00
|
|
|
|
|
|
|
if (udev->speed != USB_SPEED_FULL) {
|
|
|
|
/* not supported */
|
|
|
|
return;
|
|
|
|
}
|
2009-04-05 18:21:08 +00:00
|
|
|
if ((edesc->bmAttributes & UE_XFERTYPE) == UE_ISOCHRONOUS)
|
2009-06-07 19:41:11 +00:00
|
|
|
ep->methods = &atmegadci_device_isoc_fs_methods;
|
2009-04-05 18:21:08 +00:00
|
|
|
else
|
2009-06-07 19:41:11 +00:00
|
|
|
ep->methods = &atmegadci_device_non_isoc_methods;
|
2009-01-13 18:49:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:28:54 +00:00
|
|
|
static void
|
|
|
|
atmegadci_set_hw_power_sleep(struct usb_bus *bus, uint32_t state)
|
|
|
|
{
|
|
|
|
struct atmegadci_softc *sc = ATMEGA_BUS2SC(bus);
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case USB_HW_POWER_SUSPEND:
|
|
|
|
atmegadci_suspend(sc);
|
|
|
|
break;
|
|
|
|
case USB_HW_POWER_SHUTDOWN:
|
|
|
|
atmegadci_uninit(sc);
|
|
|
|
break;
|
|
|
|
case USB_HW_POWER_RESUME:
|
|
|
|
atmegadci_resume(sc);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-11 13:20:32 +00:00
|
|
|
static const struct usb_bus_methods atmegadci_bus_methods =
|
2009-01-13 18:49:35 +00:00
|
|
|
{
|
2009-06-07 19:41:11 +00:00
|
|
|
.endpoint_init = &atmegadci_ep_init,
|
2009-01-13 18:49:35 +00:00
|
|
|
.xfer_setup = &atmegadci_xfer_setup,
|
|
|
|
.xfer_unsetup = &atmegadci_xfer_unsetup,
|
|
|
|
.get_hw_ep_profile = &atmegadci_get_hw_ep_profile,
|
2012-08-12 17:53:06 +00:00
|
|
|
.xfer_stall = &atmegadci_xfer_stall,
|
2009-01-13 18:49:35 +00:00
|
|
|
.set_stall = &atmegadci_set_stall,
|
|
|
|
.clear_stall = &atmegadci_clear_stall,
|
2009-04-05 18:20:49 +00:00
|
|
|
.roothub_exec = &atmegadci_roothub_exec,
|
2009-07-30 00:14:34 +00:00
|
|
|
.xfer_poll = &atmegadci_do_poll,
|
2011-12-14 00:28:54 +00:00
|
|
|
.set_hw_power_sleep = &atmegadci_set_hw_power_sleep,
|
2009-01-13 18:49:35 +00:00
|
|
|
};
|