1
0
Fork 0
forked from len0rd/rockbox

Usb Stack: only setup packet handling, and not enabled by default as there is a lot to do.

* settings code is not fully ready -> changing device driver has no effect
* clean ups
* check copyriths
* find a way to detect IN transfers
* support for full and highspeed
* ...




git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14470 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Christian Gmeiner 2007-08-27 16:04:32 +00:00
parent 9305c86f5b
commit 8181a0c905
29 changed files with 4585 additions and 72 deletions

View file

@ -0,0 +1,31 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _USBSTACK_CONFIG_H_
#define _USBSTACK_CONFIG_H_
/* default: use no controller */
#ifndef USBSTACK_CAPS
#define USBSTACK_CAPS 0
#endif
#define CONTROLLER_DEVICE (1 << 0)
#define CONTROLLER_HOST (1 << 1)
#endif /*_USBSTACK_CONFIG_H_*/

View file

@ -0,0 +1,68 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: arcotg_udc.c 12340 2007-02-16 22:13:21Z barrywardell $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _USBSTACK_CONTROLLER_H_
#define _USBSTACK_CONTROLLER_H_
struct usb_controller {
const char* name;
enum usb_controller_type type;
enum usb_device_speed speed;
void (*init)(void);
void (*shutdown)(void);
void (*irq)(void);
void (*start)(void);
void (*stop)(void);
void* controller_ops;
struct usb_device_driver* device_driver;
struct usb_host_driver* host_driver;
struct usb_ep* ep0;
struct usb_ep endpoints;
};
struct usb_dcd_controller_ops {
/* endpoint management */
int (*enable)(struct usb_ep* ep);
int (*disable)(struct usb_ep* ep);
int (*set_halt)(struct usb_ep* ep, bool hald);
/* transmitting */
int (*send)(struct usb_ep* ep, struct usb_response* req);
int (*receive)(struct usb_ep* ep, struct usb_response* res);
/* ep0 */
struct usb_ep* ep0;
};
int usb_controller_register(struct usb_controller* ctrl);
int usb_controller_unregister(struct usb_controller* ctrl);
/*
* dcd - device controller driver
*/
void usb_dcd_init(void);
void usb_dcd_shutdown(void);
/*
* hcd - host controller driver
*/
void usb_hcd_init(void);
void usb_hcd_shutdown(void);
#endif /*_USBSTACK_CONTROLLER_H_*/

84
firmware/usbstack/core.h Normal file
View file

@ -0,0 +1,84 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: arcotg_udc.c 12340 2007-02-16 22:13:21Z barrywardell $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _USBSTACK_CORE_H_
#define _USBSTACK_CORE_H_
#include "linkedlist.h"
#include "usb_ch9.h"
#include "logf.h"
#include "system.h"
#include "usbstack.h"
/*
* stack datatypes
*/
struct usb_response {
void* buf;
uint32_t length;
};
struct usb_ep {
const char name[15];
uint8_t type;
uint32_t ep_num; /* which endpoint? */
uint32_t pipe_num; /* which pipe? */
uint32_t maxpacket;
bool claimed;
struct usb_endpoint_descriptor *desc;
struct list_head list;
};
#include "usbstack/controller.h"
#include "usbstack/device.h"
#include "usbstack/host.h"
#define NUM_DRIVERS 3
/*
* usb core
*/
struct usb_core {
/* we can have maximum two controllers (one device, one host) */
struct usb_controller* controller[2];
struct usb_controller* active_controller;
/* device driver used by stack */
struct usb_device_driver* device_driver;
/* for each type of driver use own array */
struct usb_host_driver* host_drivers[NUM_DRIVERS];
struct usb_device_driver* device_drivers[NUM_DRIVERS];
enum usb_controller_type mode;
bool running;
};
void usb_stack_irq(void);
void usb_stack_work(void);
/* endpoint configuration */
void usb_ep_autoconfig_reset(void);
struct usb_ep* usb_ep_autoconfig(struct usb_endpoint_descriptor* desc);
/* only used for debug */
void into_usb_ctrlrequest(struct usb_ctrlrequest* request);
extern struct usb_core usbcore;
#endif /*_USBSTACK_CORE_H_*/

View file

@ -0,0 +1,80 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* Based on linux/drivers/usb/gadget/config.c
* Copyright (C) 2003 David Brownell
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <string.h>
#include "usbstack/core.h"
static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src) {
uint8_t* dest = buf;
if (!src) {
return -EINVAL;
}
/* fill buffer from src[] until null descriptor ptr */
for (; 0 != *src; src++) {
unsigned len = (*src)->bLength;
logf("len: %d", len);
if (len > buflen)
return -EINVAL;
memcpy(dest, *src, len);
buflen -= len;
dest += len;
}
return dest - (uint8_t *)buf;
}
int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc) {
struct usb_config_descriptor* cp = buf;
int len;
if (length < USB_DT_CONFIG_SIZE || !desc) {
return -EINVAL;
}
/* config descriptor first */
*cp = *config;
/* then interface/endpoint/class/vendor/... */
len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf, length - USB_DT_CONFIG_SIZE, desc);
if (len < 0) {
return len;
}
len += USB_DT_CONFIG_SIZE;
if (len > 0xffff) {
return -EINVAL;
}
/* patch up the config descriptor */
cp->bLength = USB_DT_CONFIG_SIZE;
cp->bDescriptorType = USB_DT_CONFIG;
cp->wTotalLength = len;
cp->bmAttributes |= USB_CONFIG_ATT_ONE;
return len;
}

View file

@ -0,0 +1,392 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "usbstack.h"
#include "config.h"
#include "usbstack/core.h"
#include "usbstack/config.h"
#include "usbstack/controller.h"
#include "usbstack/drivers/device/usb_serial.h"
#include "usbstack/drivers/device/usb_storage.h"
struct usb_core usbcore;
/* private used functions */
static void update_driver_names(unsigned char* result);
static void bind_device_driver(struct usb_device_driver* driver);
/**
* Initialize usb stack.
*/
void usb_stack_init(void) {
int i;
logf("usb_stack_init");
/* init datastructures */
usbcore.controller[0] = NULL;
usbcore.controller[1] = NULL;
usbcore.active_controller = NULL;
usbcore.device_driver = NULL;
usbcore.running = false;
memset(&device_driver_names, 0, USB_STACK_MAX_SETTINGS_NAME);
/* init arrays */
for (i = 0; i < NUM_DRIVERS; i++) {
usbcore.device_drivers[i] = NULL;
usbcore.host_drivers[i] = NULL;
}
/* init controllers */
#if (USBSTACK_CAPS & CONTROLLER_DEVICE)
usb_dcd_init();
#endif
#if (USBSTACK_CAPS & CONTROLLER_HOST)
usb_hcd_init();
#endif
/* init drivers */
usb_serial_driver_init();
usb_storage_driver_init();
}
/**
* Start processing of usb stack. This function init
* active usb controller.
*/
void usb_stack_start(void) {
/* are we allready running? */
if (usbcore.running) {
logf("allready running!");
return;
}
if (usbcore.active_controller == NULL) {
logf("no active controller!");
return;
}
/* forward to controller */
logf("starting controller");
usbcore.active_controller->start();
usbcore.running = true;
/* look if started controller is a device controller
* and if it has a device driver bind to it */
logf("check for auto bind");
if (usbcore.active_controller->type == DEVICE) {
if (usbcore.active_controller->device_driver == NULL && usbcore.device_driver != NULL) {
/* bind driver */
logf("binding...");
bind_device_driver(usbcore.device_driver);
}
}
}
/**
* Stop processing of usb stack. This function shutsdown
* active usb controller.
*/
void usb_stack_stop(void) {
/* are we allready stopped? */
if (usbcore.running == false) {
return;
}
/* forward to controller */
usbcore.active_controller->stop();
usbcore.running = false;
}
/**
* Gets called by upper layers to indicate that there is
* an interrupt waiting for the controller.
*/
void usb_stack_irq(void) {
/* simply notify usb controller */
if (usbcore.active_controller != NULL && usbcore.active_controller->irq != NULL) {
usbcore.active_controller->irq();
}
}
/**
* If a host device controller is loaded, we need to have a function
* to call for maintanence. We need to check if a new device has connected,
* find suitable drivers for new devices.
*/
void usb_stack_work(void) {
/* TODO will be used with host device controllers
* and needs to be called in a loop (thread) */
}
/**
* Register an usb controller in the stack. The stack can
* only have two controllers registered at one time.
* One device host controller and one host device controller.
*
* @param ctrl pointer to controller to register.
* @return 0 on success else a defined error code.
*/
int usb_controller_register(struct usb_controller* ctrl) {
if (ctrl == NULL) {
return EINVAL;
}
logf("usb_stack: register usb ctrl");
logf(" -> name: %s", ctrl->name);
logf(" -> type: %d", ctrl->type);
switch (ctrl->type) {
case DEVICE:
if (usbcore.controller[0] == NULL) {
usbcore.controller[0] = ctrl;
return 0;
}
break;
case HOST:
if (usbcore.controller[1] == NULL) {
usbcore.controller[1] = ctrl;
return 0;
}
break;
default:
return EINVAL;
}
return ENOFREESLOT;
}
/**
* Unregister an usb controller from the stack.
*
* @param ctrl pointer to controller to unregister.
* @return 0 on success else a defined error code.
*/
int usb_controller_unregister(struct usb_controller* ctrl) {
if (ctrl == NULL) {
return EINVAL;
}
switch (ctrl->type) {
case DEVICE:
if (usbcore.controller[0] == ctrl) {
usbcore.controller[0] = NULL;
return 0;
}
break;
case HOST:
if (usbcore.controller[1] == ctrl) {
usbcore.controller[1] = NULL;
return 0;
}
break;
default:
return EINVAL;
}
return 0; /* never reached */
}
/**
* Select an usb controller and active it.
*
* @param type of controller to activate.
*/
void usb_controller_select(int type) {
struct usb_controller* new = NULL;
/* check if a controller of the wanted type is already loaded */
if (usbcore.active_controller != NULL && (int)usbcore.active_controller->type == type) {
logf("controller already set");
return;
}
logf("usb_controller_select");
logf(" -> type: %d", type);
usbcore.mode = type;
switch (type) {
case DEVICE:
new = usbcore.controller[0];
break;
case HOST:
new = usbcore.controller[1];
break;
}
/* if there is only one controller, stop here */
if (new == NULL) {
logf("no suitable cntrl found");
return;
}
/* shutdown current used controller */
if (usbcore.active_controller != NULL) {
logf("shuting down old one");
usbcore.active_controller->shutdown();
}
/* set and init new controller */
usbcore.active_controller = new;
logf("init controller");
usbcore.active_controller->init();
}
int usb_stack_get_mode(void) {
return usbcore.mode;
}
/**
* Register an usb device driver.
*
* @param driver pointer to an usb_device_driver struct.
* @return 0 on success, else a defined error code.
*/
int usb_device_driver_register(struct usb_device_driver* driver) {
int i;
if (driver == NULL) {
return EINVAL;
}
/* add to linked list */
logf("usb_stack: register usb driver");
for (i = 0; i < NUM_DRIVERS; i++) {
if (usbcore.device_drivers[i] == NULL) {
usbcore.device_drivers[i] = driver;
update_driver_names(device_driver_names);
return 0;
}
}
update_driver_names(device_driver_names);
return 0;
}
int usb_device_driver_bind(const char* name) {
int i;
struct usb_device_driver *tmp = NULL;
struct usb_device_driver *driver = NULL;
if (name == NULL) {
return EINVAL;
}
/* look for driver */
logf("looking for driver %s", name);
for (i = 0; i < NUM_DRIVERS; i++) {
tmp = usbcore.device_drivers[i];
if (tmp != NULL && strcmp(name, tmp->name) == 0) {
driver = tmp;
}
}
if (driver == NULL) {
logf("no driver found");
return ENODRIVERFOUND;
}
/* look if there is an usb controller loaded */
if (usbcore.active_controller == NULL) {
/* safe choosen driver and set it when controller starts */
usbcore.device_driver = driver;
} else {
/* we need to have an active dcd controller */
if (usbcore.active_controller->type != DEVICE) {
logf("wrong type");
return EWRONGCONTROLLERTYPE;
}
/* bind driver to controller */
bind_device_driver(driver);
}
return 0;
}
void usb_device_driver_unbind(void) {
logf("usb_device_driver_unbind");
if (usbcore.active_controller->device_driver != NULL) {
usbcore.active_controller->device_driver->unbind();
usbcore.active_controller->device_driver = NULL;
}
usbcore.device_driver = NULL;
}
static void update_driver_names(unsigned char* result) {
int i;
int pos = 0;
unsigned char terminator = ',';
struct usb_device_driver* dd = NULL;
/* reset buffer, iterate through drivers and add to char array */
memset(result, 0, USB_STACK_MAX_SETTINGS_NAME);
for (i = 0; i < NUM_DRIVERS; i++) {
int len;
dd = usbcore.device_drivers[i];
if (dd != NULL) {
len = strlen(dd->name);
if (pos > 0) {
memcpy(result + pos, &terminator, 1);
pos++;
}
memcpy(result + pos, dd->name, len);
pos += len;
}
}
}
static void bind_device_driver(struct usb_device_driver* driver) {
/* look if there is an old driver */
if (usbcore.active_controller->device_driver != NULL) {
usbcore.active_controller->device_driver->unbind();
}
/* bind driver to controller */
usbcore.active_controller->device_driver = driver;
/* init dirver */
driver->bind(usbcore.active_controller->controller_ops);
}

View file

@ -0,0 +1,186 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <string.h>
#include <ctype.h>
#include "usbstack/core.h"
/**
*
* Naming Convention for Endpoint Names
*
* - ep1, ep2, ... address is fixed, not direction or type
* - ep1in, ep2out, ... address and direction are fixed, not type
* - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
* - ep1in-bulk, ep2out-iso, ... all three are fixed
* - ep-* ... no functionality restrictions
*
* Type suffixes are "-bulk", "-iso", or "-int". Numbers are decimal.
*
*/
static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc);
void usb_ep_autoconfig_reset(void) {
struct usb_ep* ep = NULL;
if (usbcore.active_controller == NULL) {
return;
}
logf("resetting endpoints");
list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
logf("reset %s", ep->name);
ep->claimed = false;
}
}
/**
* Find a suitable endpoint for the requested endpoint descriptor.
* @param desc usb descritpro to use for seraching.
* @return NULL or a valid endpoint.
*/
struct usb_ep* usb_ep_autoconfig(struct usb_endpoint_descriptor* desc) {
struct usb_ep* ep = NULL;
if (usbcore.active_controller == NULL) {
logf("active controller NULL");
return NULL;
}
list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
if (ep_matches (ep, desc)) {
return ep;
}
}
return NULL;
}
static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc) {
uint8_t type;
const char* tmp;
uint16_t max;
/* endpoint already claimed? */
if (ep->claimed) {
logf("!! claimed !!");
return 0;
}
/* only support ep0 for portable CONTROL traffic */
type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
if (type == USB_ENDPOINT_XFER_CONTROL) {
logf("type == control");
return 0;
}
/* some other naming convention */
if (ep->name[0] != 'e') {
logf("wrong name");
return 0;
}
/* type-restriction: "-iso", "-bulk", or "-int".
* direction-restriction: "in", "out".
*/
if (ep->name[2] != '-' ) {
tmp = strrchr (ep->name, '-');
if (tmp) {
switch (type) {
case USB_ENDPOINT_XFER_INT:
/* bulk endpoints handle interrupt transfers,
* except the toggle-quirky iso-synch kind
*/
if (tmp[2] == 's') { // == "-iso"
return 0;
}
break;
case USB_ENDPOINT_XFER_BULK:
if (tmp[1] != 'b') { // != "-bulk"
return 0;
}
break;
case USB_ENDPOINT_XFER_ISOC:
if (tmp[2] != 's') { // != "-iso"
return 0;
}
}
} else {
tmp = ep->name + strlen (ep->name);
}
/* direction-restriction: "..in-..", "out-.." */
tmp--;
if (!isdigit(*tmp)) {
if (desc->bEndpointAddress & USB_DIR_IN) {
if ('n' != *tmp) {
return 0;
}
} else {
if ('t' != *tmp) {
return 0;
}
}
}
}
/* endpoint maxpacket size is an input parameter, except for bulk
* where it's an output parameter representing the full speed limit.
* the usb spec fixes high speed bulk maxpacket at 512 bytes.
*/
max = 0x7ff & desc->wMaxPacketSize;
switch (type) {
case USB_ENDPOINT_XFER_INT:
/* INT: limit 64 bytes full speed, 1024 high speed */
if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 64)) {
return 0;
}
/* FALLTHROUGH */
case USB_ENDPOINT_XFER_ISOC:
if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && (max > 1023)) {
return 0;
}
break;
}
/* MATCH!! */
/* report address */
desc->bEndpointAddress |= ep->ep_num;
/* report (variable) full speed bulk maxpacket */
if (type == USB_ENDPOINT_XFER_BULK) {
int size = max;
/* min() doesn't work on bitfields with gcc-3.5 */
if (size > 64) {
size = 64;
}
desc->wMaxPacketSize = size;
}
/* save desc in endpoint */
ep->desc = desc;
return 1;
}

View file

@ -0,0 +1,125 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <string.h>
#include "usbstack/core.h"
void into_usb_ctrlrequest(struct usb_ctrlrequest* request) {
char* type = "";
char* req = "";
char* extra = 0;
logf("-usb request-");
/* check if packet is okay */
if (request->bRequestType == 0 &&
request->bRequest == 0 &&
request->wValue == 0 &&
request->wIndex == 0 &&
request->wLength == 0) {
logf(" -> INVALID <-");
return;
}
switch (request->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
type = "standard";
switch (request->bRequest) {
case USB_REQ_GET_STATUS:
req = "get status";
break;
case USB_REQ_CLEAR_FEATURE:
req = "clear feature";
break;
case USB_REQ_SET_FEATURE:
req = "set feature";
break;
case USB_REQ_SET_ADDRESS:
req = "set address";
break;
case USB_REQ_GET_DESCRIPTOR:
req = "get descriptor";
switch (request->wValue >> 8) {
case USB_DT_DEVICE:
extra = "get device descriptor";
break;
case USB_DT_DEVICE_QUALIFIER:
extra = "get device qualifier";
break;
case USB_DT_OTHER_SPEED_CONFIG:
extra = "get other-speed config descriptor";
case USB_DT_CONFIG:
extra = "get configuration descriptor";
break;
case USB_DT_STRING:
extra = "get string descriptor";
break;
case USB_DT_DEBUG:
extra = "debug";
break;
}
break;
break;
case USB_REQ_SET_DESCRIPTOR:
req = "set descriptor";
break;
case USB_REQ_GET_CONFIGURATION:
req = "get configuration";
break;
case USB_REQ_SET_CONFIGURATION:
req = "set configuration";
break;
case USB_REQ_GET_INTERFACE:
req = "get interface";
break;
case USB_REQ_SET_INTERFACE:
req = "set interface";
break;
case USB_REQ_SYNCH_FRAME:
req = "sync frame";
break;
default:
req = "unkown";
break;
}
break;
case USB_TYPE_CLASS:
type = "class";
break;
case USB_TYPE_VENDOR:
type = "vendor";
break;
}
logf(" -b 0x%x", request->bRequestType);
logf(" -b 0x%x", request->bRequest);
logf(" -b 0x%x", request->wValue);
logf(" -b 0x%x", request->wIndex);
logf(" -b 0x%x", request->wLength);
logf(" -> t: %s", type);
logf(" -> r: %s", req);
if (extra != 0) {
logf(" -> e: %s", extra);
}
}

View file

@ -0,0 +1,48 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: arcotg_udc.c 12340 2007-02-16 22:13:21Z barrywardell $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _USBSTACK_DEVICE_H_
#define _USBSTACK_DEVICE_H_
/*
* usb device driver
*/
struct usb_device_driver {
const char* name;
void (*bind)(void* controller_ops);
void (*unbind)(void);
int (*request)(struct usb_ctrlrequest* req);
void (*suspend)(void);
void (*resume)(void);
void (*speed)(enum usb_device_speed speed);
struct usb_descriptors* descriptors;
void* data; /* used to store controller specific ops struct */
};
int usb_device_driver_register(struct usb_device_driver* driver);
/* forward declaration */
struct usb_config_descriptor;
struct usb_descriptor_header;
int usb_stack_configdesc(const struct usb_config_descriptor* config,
void* buf, unsigned length,
struct usb_descriptor_header** desc);
#endif /*_USBSTACK_DEVICE_H_*/

View file

@ -0,0 +1,300 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "usb_serial.h"
#include <string.h>
static struct usb_dcd_controller_ops* ops;
struct usb_device_driver usb_serial_driver = {
.name = "serial",
.bind = usb_serial_driver_bind,
.unbind = NULL,
.request = usb_serial_driver_request,
.suspend = NULL,
.resume = NULL,
.speed = usb_serial_driver_speed,
};
/*-------------------------------------------------------------------------*/
/* usb descriptors */
/* TODO: implement strings */
#define GS_MANUFACTURER_STR_ID 0
#define GS_PRODUCT_STR_ID 0
#define GS_SERIAL_STR_ID 0
#define GS_BULK_CONFIG_STR_ID 0
#define GS_DATA_STR_ID 0
#define GS_BULK_CONFIG_ID 1
static struct usb_device_descriptor serial_device_desc = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = USB_CLASS_COMM,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.idVendor = 0x0525,
.idProduct = 0xa4a6,
.iManufacturer = GS_MANUFACTURER_STR_ID,
.iProduct = GS_PRODUCT_STR_ID,
.iSerialNumber = GS_SERIAL_STR_ID,
.bNumConfigurations = 1,
};
static struct usb_config_descriptor serial_bulk_config_desc = {
.bLength = USB_DT_CONFIG_SIZE,
.bDescriptorType = USB_DT_CONFIG,
.bNumInterfaces = 1,
.bConfigurationValue = GS_BULK_CONFIG_ID,
.iConfiguration = GS_BULK_CONFIG_STR_ID,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 1,
};
static struct usb_interface_descriptor serial_bulk_interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_CDC_DATA,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = GS_DATA_STR_ID,
};
static struct usb_endpoint_descriptor serial_fullspeed_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 8,
};
static struct usb_endpoint_descriptor serial_fullspeed_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 8,
};
static struct usb_debug_descriptor serial_debug_desc = {
.bLength = sizeof(struct usb_debug_descriptor),
.bDescriptorType = USB_DT_DEBUG,
};
static struct usb_qualifier_descriptor serial_qualifier_desc = {
.bLength = sizeof(struct usb_qualifier_descriptor),
.bDescriptorType = USB_DT_DEVICE_QUALIFIER,
.bcdUSB = 0x0200,
.bDeviceClass = USB_CLASS_COMM,
.bNumConfigurations = 1,
};
struct usb_descriptor_header *serial_bulk_fullspeed_function[] = {
(struct usb_descriptor_header *) &serial_bulk_interface_desc,
(struct usb_descriptor_header *) &serial_fullspeed_in_desc,
(struct usb_descriptor_header *) &serial_fullspeed_out_desc,
NULL,
};
#define BUFFER_SIZE 100
uint8_t buf[BUFFER_SIZE];
struct usb_response res;
/* helper functions */
static int config_buf(uint8_t *buf, uint8_t type, unsigned index);
static int set_config(int config);
struct device {
struct usb_ep* in;
struct usb_ep* out;
uint32_t used_config;
};
static struct device dev;
/*-------------------------------------------------------------------------*/
void usb_serial_driver_init(void) {
logf("usb serial: register");
usb_device_driver_register(&usb_serial_driver);
}
/*-------------------------------------------------------------------------*/
void usb_serial_driver_bind(void* controler_ops) {
logf("usb serial: bind");
ops = controler_ops;
/* serach and asign endpoints */
usb_ep_autoconfig_reset();
dev.in = usb_ep_autoconfig(&serial_fullspeed_in_desc);
if (!dev.in) {
goto autoconf_fail;
}
dev.in->claimed = true;
logf("usb serial: in: %s", dev.in->name);
dev.out = usb_ep_autoconfig(&serial_fullspeed_out_desc);
if (!dev.out) {
goto autoconf_fail;
}
dev.out->claimed = true;
logf("usb serial: out: %s", dev.out->name);
/* update device decsriptor */
serial_device_desc.bMaxPacketSize0 = ops->ep0->maxpacket;
/* update qualifie descriptor */
serial_qualifier_desc.bMaxPacketSize0 = ops->ep0->maxpacket;
/* update debug descriptor */
serial_debug_desc.bDebugInEndpoint = dev.in->ep_num;
serial_debug_desc.bDebugOutEndpoint = dev.out->ep_num;
return;
autoconf_fail:
logf("failed to find endpoiunts");
}
int usb_serial_driver_request(struct usb_ctrlrequest* request) {
int ret = -EOPNOTSUPP;
logf("usb serial: request");
res.length = 0;
res.buf = NULL;
switch (request->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (request->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (request->wValue >> 8) {
case USB_DT_DEVICE:
logf("usb serial: sending device desc");
ret = MIN(sizeof(struct usb_device_descriptor), request->wLength);
res.buf = &serial_device_desc;
break;
case USB_DT_DEVICE_QUALIFIER:
logf("usb serial: sending qualifier dec");
ret = MIN(sizeof(struct usb_qualifier_descriptor), request->wLength);
res.buf = &serial_qualifier_desc;
case USB_DT_CONFIG:
logf("usb serial: sending config desc");
ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff);
if (ret >= 0) {
logf("%d, vs %d", request->wLength, ret);
ret = MIN(request->wLength, (uint16_t)ret);
}
res.buf = buf;
break;
case USB_DT_DEBUG:
logf("usb serial: sending debug desc");
ret = MIN(sizeof(struct usb_debug_descriptor), request->wLength);
res.buf = &serial_debug_desc;
break;
}
break;
case USB_REQ_SET_CONFIGURATION:
logf("usb serial: set configuration %d", request->wValue);
ret = set_config(request->wValue);
break;
case USB_REQ_GET_CONFIGURATION:
logf("usb serial: get configuration");
ret = 1;
res.buf = &dev.used_config;
break;
}
}
if (ret >= 0) {
res.length = ret;
ret = ops->send(NULL, &res);
}
return ret;
}
void usb_serial_driver_speed(enum usb_device_speed speed) {
switch (speed) {
case USB_SPEED_LOW:
case USB_SPEED_FULL:
logf("usb serial: using fullspeed");
break;
case USB_SPEED_HIGH:
logf("usb serial: using highspeed");
break;
default:
logf("speed: hmm");
break;
}
}
/*-------------------------------------------------------------------------*/
/* helper functions */
static int config_buf(uint8_t *buf, uint8_t type, unsigned index) {
int len;
/* TODO check index*/
len = usb_stack_configdesc(&serial_bulk_config_desc, buf, BUFFER_SIZE, serial_bulk_fullspeed_function);
if (len < 0) {
return len;
}
((struct usb_config_descriptor *)buf)->bDescriptorType = type;
return len;
}
static int set_config(int config) {
/* TODO check config*/
/* enable endpoints */
logf("setup %s", dev.in->name);
ops->enable(dev.in);
logf("setup %s", dev.out->name);
ops->enable(dev.out);
/* store config */
logf("using config %d", config);
dev.used_config = config;
return 0;
}

View file

@ -0,0 +1,32 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _SERIAL_H_
#define _SERIAL_H_
#include "usbstack/core.h"
/* register serial driver in usb stack */
void usb_serial_driver_init(void);
void usb_serial_driver_bind(void* controller_ops);
int usb_serial_driver_request(struct usb_ctrlrequest* req);
void usb_serial_driver_speed(enum usb_device_speed speed);
#endif /*_SERIAL_H_*/

View file

@ -0,0 +1,269 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "usb_storage.h"
#include <string.h>
/*-------------------------------------------------------------------------*/
static struct usb_dcd_controller_ops* ops;
struct usb_device_driver usb_storage_driver = {
.name = "storage",
.bind = usb_storage_driver_bind,
.unbind = NULL,
.request = usb_storage_driver_request,
.suspend = NULL,
.resume = NULL,
.speed = NULL,
};
struct device {
struct usb_ep* in;
struct usb_ep* out;
struct usb_ep* intr;
};
static struct device dev;
/*-------------------------------------------------------------------------*/
#define PROTO_BULK 0x50 // Bulk only
#define SUBCL_SCSI 0x06 // Transparent SCSI
/* Bulk-only class specific requests */
#define USB_BULK_RESET_REQUEST 0xff
#define USB_BULK_GET_MAX_LUN_REQUEST 0xfe
/*-------------------------------------------------------------------------*/
/* usb descriptors */
static struct usb_device_descriptor storage_device_desc = {
.bLength = USB_DT_DEVICE_SIZE,
.bDescriptorType = USB_DT_DEVICE,
.bcdUSB = 0x0200,
.bDeviceClass = 0,
.bDeviceSubClass = 0,
.bDeviceProtocol = 0,
.bMaxPacketSize0 = 64,
.idVendor = 0xffff,
.idProduct = 0x0001,
.iManufacturer = 0,
.iProduct = 0,
.iSerialNumber = 0,
.bNumConfigurations = 1,
};
static struct usb_config_descriptor storage_config_desc = {
.bLength = USB_DT_CONFIG_SIZE,
.bDescriptorType = USB_DT_CONFIG,
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = 0,
.bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
.bMaxPower = 1,
};
static struct usb_interface_descriptor storage_interface_desc = {
.bLength = USB_DT_INTERFACE_SIZE,
.bDescriptorType = USB_DT_INTERFACE,
.bInterfaceNumber = 0,
.bNumEndpoints = 3,
.bInterfaceClass = USB_CLASS_MASS_STORAGE,
.bInterfaceSubClass = SUBCL_SCSI,
.bInterfaceProtocol = PROTO_BULK,
.iInterface = 0,
};
/* endpoint I -> bulk in */
static struct usb_endpoint_descriptor storage_bulk_in_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_IN,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
};
/* endpoint II -> bulk out */
static struct usb_endpoint_descriptor storage_bulk_out_desc = {
.bLength = USB_DT_ENDPOINT_SIZE,
.bDescriptorType = USB_DT_ENDPOINT,
.bEndpointAddress = USB_DIR_OUT,
.bmAttributes = USB_ENDPOINT_XFER_BULK,
.wMaxPacketSize = 512,
};
struct usb_descriptor_header *storage_fullspeed_function[] = {
(struct usb_descriptor_header *) &storage_interface_desc,
(struct usb_descriptor_header *) &storage_bulk_in_desc,
(struct usb_descriptor_header *) &storage_bulk_out_desc,
NULL,
};
#define BUFFER_SIZE 100
uint8_t buf[BUFFER_SIZE];
struct usb_response res;
/* helper functions */
static int config_buf(uint8_t *buf, uint8_t type, unsigned index);
static int set_config(int config);
/*-------------------------------------------------------------------------*/
void usb_storage_driver_init(void) {
logf("usb storage: register");
usb_device_driver_register(&usb_storage_driver);
}
/*-------------------------------------------------------------------------*/
/* device driver ops */
void usb_storage_driver_bind(void* controler_ops) {
ops = controler_ops;
/* serach and asign endpoints */
usb_ep_autoconfig_reset();
dev.in = usb_ep_autoconfig(&storage_bulk_in_desc);
if (!dev.in) {
goto autoconf_fail;
}
dev.in->claimed = true;
logf("usb storage: in: %s", dev.in->name);
dev.out = usb_ep_autoconfig(&storage_bulk_out_desc);
if (!dev.out) {
goto autoconf_fail;
}
dev.out->claimed = true;
logf("usb storage: out: %s", dev.out->name);
return;
autoconf_fail:
logf("failed to find endpoints");
}
int usb_storage_driver_request(struct usb_ctrlrequest* request) {
int ret = -EOPNOTSUPP;
logf("usb storage: request");
res.length = 0;
res.buf = NULL;
switch (request->bRequestType & USB_TYPE_MASK) {
case USB_TYPE_STANDARD:
switch (request->bRequest) {
case USB_REQ_GET_DESCRIPTOR:
switch (request->wValue >> 8) {
case USB_DT_DEVICE:
logf("usb storage: sending device desc");
ret = MIN(sizeof(struct usb_device_descriptor), request->wLength);
res.buf = &storage_device_desc;
break;
case USB_DT_CONFIG:
logf("usb storage: sending config desc");
ret = config_buf(buf, request->wValue >> 8, request->wValue & 0xff);
if (ret >= 0) {
logf("%d, vs %d", request->wLength, ret);
ret = MIN(request->wLength, (uint16_t)ret);
}
res.buf = buf;
break;
}
break;
case USB_REQ_SET_CONFIGURATION:
logf("usb storage: set configuration %d", request->wValue);
ret = set_config(request->wValue);
break;
case USB_REQ_SET_INTERFACE:
logf("usb storage: set interface");
ret = 0;
break;
}
case USB_TYPE_CLASS:
switch (request->bRequest) {
case USB_BULK_RESET_REQUEST:
logf("usb storage: bulk reset");
break;
case USB_BULK_GET_MAX_LUN_REQUEST:
logf("usb storage: get max lun");
/* we support no LUNs (Logical Unit Number) */
buf[0] = 0;
ret = 1;
break;
}
break;
}
if (ret >= 0) {
res.length = ret;
ret = ops->send(NULL, &res);
}
return ret;
}
/*-------------------------------------------------------------------------*/
/* S/GET CONFIGURATION helpers */
static int config_buf(uint8_t *buf, uint8_t type, unsigned index) {
int len;
/* only one configuration */
if (index != 0) {
return -EINVAL;
}
len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, storage_fullspeed_function);
if (len < 0) {
return len;
}
((struct usb_config_descriptor *)buf)->bDescriptorType = type;
return len;
}
static int set_config(int config) {
/* enable endpoints */
logf("setup %s", dev.in->name);
ops->enable(dev.in);
logf("setup %s", dev.out->name);
ops->enable(dev.out);
/* setup buffers */
return 0;
}

View file

@ -0,0 +1,31 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _STORGAGE_H_
#define _STORGAGE_H_
#include "usbstack/core.h"
/* register serial driver in usb stack */
void usb_storage_driver_init(void);
void usb_storage_driver_bind(void* controller_ops);
int usb_storage_driver_request(struct usb_ctrlrequest* req);
#endif /*_STORGAGE_H_*/

31
firmware/usbstack/host.h Normal file
View file

@ -0,0 +1,31 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id: arcotg_udc.c 12340 2007-02-16 22:13:21Z barrywardell $
*
* Copyright (C) 2007 by Christian Gmeiner
*
* All files in this archive are subject to the GNU General Public License.
* See the file COPYING in the source tree root for full license agreement.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#ifndef _USBSTACK_HOST_H_
#define _USBSTACK_HOST_H_
/*
* usb host driver
*/
struct usb_host_driver {
const char* name;
void* data; /* used to store controller specific ops struct */
};
#endif /*_USBSTACK_HOST_H_*/