2007-08-27 16:04:32 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-08-27 22:12:35 +00:00
|
|
|
* $Id$
|
2007-08-27 16:04:32 +00:00
|
|
|
*
|
|
|
|
* 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,
|
2007-08-28 20:29:28 +00:00
|
|
|
.speed = usb_storage_driver_speed,
|
2007-08-27 16:04:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#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,
|
|
|
|
};
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
static struct usb_endpoint_descriptor storage_fs_bulk_in_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct usb_endpoint_descriptor storage_fs_bulk_out_desc = {
|
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 64,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct usb_descriptor_header *storage_fs_function[] = {
|
|
|
|
(struct usb_descriptor_header *) &storage_interface_desc,
|
|
|
|
(struct usb_descriptor_header *) &storage_fs_bulk_in_desc,
|
|
|
|
(struct usb_descriptor_header *) &storage_fs_bulk_out_desc,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* USB 2.0 */
|
|
|
|
static struct usb_endpoint_descriptor storage_hs_bulk_in_desc = {
|
2007-08-27 16:04:32 +00:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 512,
|
|
|
|
};
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
static struct usb_endpoint_descriptor storage_hs_bulk_out_desc = {
|
2007-08-27 16:04:32 +00:00
|
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
|
|
.wMaxPacketSize = 512,
|
|
|
|
};
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
struct usb_descriptor_header *storage_hs_function[] = {
|
2007-08-27 16:04:32 +00:00
|
|
|
(struct usb_descriptor_header *) &storage_interface_desc,
|
2007-08-28 20:29:28 +00:00
|
|
|
(struct usb_descriptor_header *) &storage_hs_bulk_in_desc,
|
|
|
|
(struct usb_descriptor_header *) &storage_hs_bulk_out_desc,
|
2007-08-27 16:04:32 +00:00
|
|
|
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);
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
struct device {
|
|
|
|
struct usb_ep* in;
|
|
|
|
struct usb_ep* out;
|
|
|
|
struct usb_ep* intr;
|
|
|
|
struct usb_descriptor_header** descriptors;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct device dev;
|
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2007-08-27 22:07:36 +00:00
|
|
|
void usb_storage_driver_init(void)
|
|
|
|
{
|
2007-08-27 16:04:32 +00:00
|
|
|
logf("usb storage: register");
|
|
|
|
usb_device_driver_register(&usb_storage_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* device driver ops */
|
|
|
|
|
2007-08-28 20:50:41 +00:00
|
|
|
int usb_storage_driver_bind(void* controler_ops)
|
2007-08-27 22:07:36 +00:00
|
|
|
{
|
2007-08-27 16:04:32 +00:00
|
|
|
ops = controler_ops;
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
/* serach and asign endpoints */
|
|
|
|
usb_ep_autoconfig_reset();
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
dev.in = usb_ep_autoconfig(&storage_fs_bulk_in_desc);
|
2007-08-27 22:07:36 +00:00
|
|
|
if (!dev.in) {
|
|
|
|
goto autoconf_fail;
|
|
|
|
}
|
|
|
|
dev.in->claimed = true;
|
|
|
|
logf("usb storage: in: %s", dev.in->name);
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
dev.out = usb_ep_autoconfig(&storage_fs_bulk_out_desc);
|
2007-08-27 22:07:36 +00:00
|
|
|
if (!dev.out) {
|
|
|
|
goto autoconf_fail;
|
|
|
|
}
|
|
|
|
dev.out->claimed = true;
|
|
|
|
logf("usb storage: out: %s", dev.out->name);
|
|
|
|
|
2007-08-28 20:50:41 +00:00
|
|
|
return 0;
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
autoconf_fail:
|
2007-08-27 22:07:36 +00:00
|
|
|
logf("failed to find endpoints");
|
2007-08-28 20:50:41 +00:00
|
|
|
return -EOPNOTSUPP;
|
2007-08-27 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
2007-08-27 22:07:36 +00:00
|
|
|
int usb_storage_driver_request(struct usb_ctrlrequest* request)
|
|
|
|
{
|
2007-08-27 16:04:32 +00:00
|
|
|
int ret = -EOPNOTSUPP;
|
|
|
|
logf("usb storage: request");
|
|
|
|
|
|
|
|
res.length = 0;
|
|
|
|
res.buf = NULL;
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
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");
|
2007-08-27 22:07:36 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2007-08-27 16:04:32 +00:00
|
|
|
res.buf = buf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case USB_REQ_SET_CONFIGURATION:
|
|
|
|
logf("usb storage: set configuration %d", request->wValue);
|
|
|
|
ret = set_config(request->wValue);
|
|
|
|
break;
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
case USB_REQ_SET_INTERFACE:
|
|
|
|
logf("usb storage: set interface");
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
case USB_TYPE_CLASS:
|
2007-08-27 22:07:36 +00:00
|
|
|
|
|
|
|
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;
|
2007-08-27 16:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
res.length = ret;
|
|
|
|
ret = ops->send(NULL, &res);
|
|
|
|
}
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
void usb_storage_driver_speed(enum usb_device_speed speed)
|
|
|
|
{
|
|
|
|
switch (speed) {
|
|
|
|
case USB_SPEED_HIGH:
|
|
|
|
logf("usb storage: using highspeed");
|
|
|
|
dev.descriptors = storage_hs_function;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
logf("usb storage: using fullspeed");
|
|
|
|
dev.descriptors = storage_fs_function;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* S/GET CONFIGURATION helpers */
|
|
|
|
|
2007-08-27 22:07:36 +00:00
|
|
|
static int config_buf(uint8_t *buf, uint8_t type, unsigned index)
|
|
|
|
{
|
|
|
|
int len;
|
2007-08-27 16:04:32 +00:00
|
|
|
|
2007-08-29 00:47:04 +00:00
|
|
|
(void)index;
|
|
|
|
|
2007-08-28 20:29:28 +00:00
|
|
|
len = usb_stack_configdesc(&storage_config_desc, buf, BUFFER_SIZE, dev.descriptors);
|
|
|
|
logf("result %d", len);
|
2007-08-27 22:07:36 +00:00
|
|
|
if (len < 0) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
((struct usb_config_descriptor *)buf)->bDescriptorType = type;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_config(int config)
|
|
|
|
{
|
2007-08-29 00:47:04 +00:00
|
|
|
(void)config;
|
2007-08-28 23:49:03 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
/* enable endpoints */
|
|
|
|
logf("setup %s", dev.in->name);
|
2007-08-28 20:29:28 +00:00
|
|
|
ops->enable(dev.in, (struct usb_endpoint_descriptor*)dev.descriptors[1]);
|
2007-08-27 16:04:32 +00:00
|
|
|
logf("setup %s", dev.out->name);
|
2007-08-28 20:29:28 +00:00
|
|
|
ops->enable(dev.out, (struct usb_endpoint_descriptor*)dev.descriptors[2]);
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
/* setup buffers */
|
2007-08-27 22:07:36 +00:00
|
|
|
|
2007-08-27 16:04:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|