2012-11-25 08:02:10 +08:00
|
|
|
/*
|
|
|
|
* File : mstorage.c
|
|
|
|
* This file is part of RT-Thread RTOS
|
|
|
|
* COPYRIGHT (C) 2012, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* The license and distribution terms for this file may be
|
|
|
|
* found in the file LICENSE in this distribution or at
|
|
|
|
* http://www.rt-thread.org/license/LICENSE
|
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2012-12-13 00:37:10 +08:00
|
|
|
* 2012-10-01 Yi Qiu first version
|
|
|
|
* 2012-11-25 Heyuanjie87 reduce the memory consumption
|
|
|
|
* 2012-12-09 Heyuanjie87 change class and endpoint handler
|
2012-11-25 08:02:10 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtservice.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include "mstorage.h"
|
|
|
|
|
|
|
|
#ifdef RT_USB_DEVICE_MSTORAGE
|
|
|
|
|
|
|
|
#define STATUS_CBW 0x00
|
|
|
|
#define STATUS_CSW 0x01
|
|
|
|
#define STATUS_RECEIVE 0x02
|
2012-11-26 00:00:28 +08:00
|
|
|
#define STATUS_SEND 0x03
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
static int status = STATUS_CBW;
|
2012-12-08 11:37:33 +08:00
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
2012-11-25 08:02:10 +08:00
|
|
|
static struct ustorage_csw csw;
|
|
|
|
static rt_device_t disk;
|
2012-11-26 00:00:28 +08:00
|
|
|
static rt_uint32_t _block;
|
|
|
|
static rt_uint32_t _count, _size;
|
2012-11-25 08:02:10 +08:00
|
|
|
static struct rt_device_blk_geometry geometry;
|
|
|
|
|
|
|
|
static struct udevice_descriptor dev_desc =
|
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_DEVICE, //bLength;
|
|
|
|
USB_DESC_TYPE_DEVICE, //type;
|
|
|
|
USB_BCD_VERSION, //bcdUSB;
|
|
|
|
USB_CLASS_MASS_STORAGE, //bDeviceClass;
|
|
|
|
0x00, //bDeviceSubClass;
|
|
|
|
0x00, //bDeviceProtocol;
|
|
|
|
0x40, //bMaxPacketSize0;
|
|
|
|
USB_VENDOR_ID, //idVendor;
|
|
|
|
USB_MASS_STORAGE_PRODUCT_ID,//idProduct;
|
|
|
|
USB_BCD_DEVICE, //bcdDevice;
|
|
|
|
USB_STRING_MANU_INDEX, //iManufacturer;
|
|
|
|
USB_STRING_PRODUCT_INDEX, //iProduct;
|
|
|
|
USB_STRING_SERIAL_INDEX, //iSerialNumber;
|
|
|
|
USB_DYNAMIC, //bNumConfigurations;
|
|
|
|
};
|
|
|
|
|
2012-12-17 17:31:57 +08:00
|
|
|
const static struct umass_descriptor _mass_desc =
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_INTERFACE, //bLength;
|
|
|
|
USB_DESC_TYPE_INTERFACE, //type;
|
|
|
|
USB_DYNAMIC, //bInterfaceNumber;
|
|
|
|
0x00, //bAlternateSetting;
|
|
|
|
0x02, //bNumEndpoints
|
|
|
|
USB_CLASS_MASS_STORAGE, //bInterfaceClass;
|
|
|
|
0x06, //bInterfaceSubClass;
|
|
|
|
0x50, //bInterfaceProtocol;
|
|
|
|
0x00, //iInterface;
|
|
|
|
|
|
|
|
USB_DESC_LENGTH_ENDPOINT, //bLength;
|
|
|
|
USB_DESC_TYPE_ENDPOINT, //type;
|
|
|
|
USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress;
|
|
|
|
USB_EP_ATTR_BULK, //bmAttributes;
|
|
|
|
0x40, //wMaxPacketSize;
|
2012-11-26 00:00:28 +08:00
|
|
|
0x00, //bInterval;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
USB_DESC_LENGTH_ENDPOINT, //bLength;
|
|
|
|
USB_DESC_TYPE_ENDPOINT, //type;
|
|
|
|
USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress;
|
|
|
|
USB_EP_ATTR_BULK, //bmAttributes;
|
|
|
|
0x40, //wMaxPacketSize;
|
2012-11-26 00:00:28 +08:00
|
|
|
0x00, //bInterval;
|
2012-11-25 08:02:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will allocate an usb device instance from system.
|
|
|
|
*
|
|
|
|
* @param parent the hub instance to which the new allocated device attached.
|
|
|
|
* @param port the hub port.
|
|
|
|
*
|
|
|
|
* @return the allocate instance on successful, or RT_NULL on failure.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _inquiry_cmd(udevice_t device, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t data[36];
|
|
|
|
|
|
|
|
*(rt_uint32_t*)&data[0] = 0x0 | (0x80 << 8);
|
|
|
|
*(rt_uint32_t*)&data[4] = 31;
|
|
|
|
|
|
|
|
rt_memset(&data[8], 0x20, 28);
|
|
|
|
rt_memcpy(&data[8], "RTT", 3);
|
|
|
|
rt_memcpy(&data[16], "USB Disk", 8);
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 36);
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle sense request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _request_sense(udevice_t device, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
struct request_sense_data data;
|
|
|
|
|
|
|
|
data.ErrorCode = 0x70;
|
|
|
|
data.Valid = 0;
|
|
|
|
data.SenseKey = 5;
|
|
|
|
data.Information[0] = 0;
|
|
|
|
data.Information[1] = 0;
|
|
|
|
data.Information[2] = 0;
|
|
|
|
data.Information[3] = 0;
|
|
|
|
data.AdditionalSenseLength = 0x0b;
|
|
|
|
data.AdditionalSenseCode = 0x20;
|
|
|
|
data.AdditionalSenseCodeQualifier =0;
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, sizeof(struct request_sense_data));
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mode_sense_6 request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _mode_sense_6(udevice_t device, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t data[4];
|
|
|
|
|
|
|
|
data[0]=3;
|
|
|
|
data[1]=0;
|
|
|
|
data[2]=0;
|
|
|
|
data[3]=0;
|
|
|
|
|
|
|
|
dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 4);
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
return RT_EOK;
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_capacities request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _read_capacities(udevice_t device, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t data[12];
|
|
|
|
rt_uint32_t sector_count, sector_size;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
|
|
|
sector_count = geometry.sector_count;
|
|
|
|
sector_size = geometry.bytes_per_sector;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
*(rt_uint32_t*)&data[0] = 0x08000000;
|
|
|
|
data[4] = sector_count >> 24;
|
|
|
|
data[5] = 0xff & (sector_count >> 16);
|
|
|
|
data[6] = 0xff & (sector_count >> 8);
|
|
|
|
data[7] = 0xff & (sector_count);
|
2012-11-26 00:00:28 +08:00
|
|
|
data[8] = 0x02;
|
2012-11-25 08:02:10 +08:00
|
|
|
data[9] = 0xff & (sector_size >> 16);
|
|
|
|
data[10] = 0xff & (sector_size >> 8);
|
2012-11-26 00:00:28 +08:00
|
|
|
data[11] = 0xff & sector_size;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 12);
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_capacity request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _read_capacity(udevice_t device, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t data[8];
|
|
|
|
rt_uint32_t sector_count, sector_size;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
|
|
|
sector_count = geometry.sector_count;
|
|
|
|
sector_size = geometry.bytes_per_sector;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
data[0] = sector_count >> 24;
|
|
|
|
data[1] = 0xff & (sector_count >> 16);
|
|
|
|
data[2] = 0xff & (sector_count >> 8);
|
|
|
|
data[3] = 0xff & (sector_count);
|
2012-11-26 00:00:28 +08:00
|
|
|
data[4] = 0x0;
|
2012-11-25 08:02:10 +08:00
|
|
|
data[5] = 0xff & (sector_size >> 16);
|
|
|
|
data[6] = 0xff & (sector_size >> 8);
|
2012-11-26 00:00:28 +08:00
|
|
|
data[7] = 0xff & sector_size;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
dcd_ep_write(device->dcd, ep_in, (rt_uint8_t*)&data, 8);
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
return RT_EOK;
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_10 request.
|
|
|
|
*
|
2012-11-26 00:00:28 +08:00
|
|
|
* @param device the usb device object.
|
2012-11-25 08:02:10 +08:00
|
|
|
* @param cbw the command block wrapper.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _read_10(udevice_t device, ustorage_cbw_t cbw, uep_t ep_in)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
_block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
|
|
|
|
cbw->cb[5]<<0 ;
|
|
|
|
|
|
|
|
_count = cbw->cb[7]<<8 | cbw->cb[8]<<0 ;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
RT_ASSERT(_count < geometry.sector_count);
|
|
|
|
|
2012-12-13 00:37:10 +08:00
|
|
|
rt_device_read(disk, _block, ep_in->buffer, 1);
|
|
|
|
dcd_ep_write(device->dcd, ep_in, ep_in->buffer, geometry.bytes_per_sector);
|
2012-11-26 00:00:28 +08:00
|
|
|
_count --;
|
|
|
|
if (_count)
|
|
|
|
{
|
|
|
|
_block ++;
|
|
|
|
status = STATUS_SEND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = STATUS_CSW;
|
|
|
|
}
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle write_10 request.
|
|
|
|
*
|
2012-11-26 00:00:28 +08:00
|
|
|
* @param device the usb device object.
|
2012-11-25 08:02:10 +08:00
|
|
|
* @param cbw the command block wrapper.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _write_10(udevice_t device, ustorage_cbw_t cbw, uep_t ep_out)
|
2012-11-26 00:00:28 +08:00
|
|
|
{
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
|
|
|
_block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
|
|
|
|
cbw->cb[5]<<0 ;
|
2012-11-25 08:02:10 +08:00
|
|
|
_count = cbw->cb[7]<<8 | cbw->cb[8]<<0;
|
|
|
|
csw.data_reside = cbw->xfer_len;
|
|
|
|
_size = _count * geometry.bytes_per_sector;
|
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("_write_10 count 0x%x 0x%x\n",
|
|
|
|
_count, geometry.sector_count));
|
|
|
|
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_read(device->dcd, ep_out, ep_out->buffer, geometry.bytes_per_sector);
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle verify_10 request.
|
|
|
|
*
|
2012-11-26 00:00:28 +08:00
|
|
|
* @param device the usb device object.
|
2012-11-25 08:02:10 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
|
|
|
static rt_err_t _verify_10(udevice_t device)
|
|
|
|
{
|
2012-11-26 00:00:28 +08:00
|
|
|
return RT_EOK;
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mass storage bulk in endpoint request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
* @param size request size.
|
|
|
|
*
|
|
|
|
* @return RT_EOK.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _ep_in_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
mass_eps_t eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2012-12-13 00:37:10 +08:00
|
|
|
|
|
|
|
eps = cls->eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
if(status == STATUS_CSW)
|
2012-11-26 00:00:28 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
|
2012-11-25 08:02:10 +08:00
|
|
|
status = STATUS_CBW;
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
|
2012-11-26 00:00:28 +08:00
|
|
|
}
|
|
|
|
if(status == STATUS_SEND)
|
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
rt_device_read(disk, _block, eps->ep_in->buffer, 1);
|
|
|
|
dcd_ep_write(device->dcd, eps->ep_in, eps->ep_in->buffer,
|
|
|
|
geometry.bytes_per_sector);
|
2012-11-26 00:00:28 +08:00
|
|
|
_count --;
|
|
|
|
if (_count)
|
|
|
|
{
|
|
|
|
_block ++;
|
|
|
|
status = STATUS_SEND;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = STATUS_CSW;
|
|
|
|
}
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2012-12-13 00:37:10 +08:00
|
|
|
#ifdef MASS_CBW_DUMP
|
2012-11-25 08:02:10 +08:00
|
|
|
static void cbw_dump(struct ustorage_cbw* cbw)
|
|
|
|
{
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("signature 0x%x\n", cbw->signature));
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("tag 0x%x\n", cbw->tag));
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("xfer_len 0x%x\n", cbw->xfer_len));
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("dflags 0x%x\n", cbw->dflags));
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("lun 0x%x\n", cbw->lun));
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("cb_len 0x%x\n", cbw->cb_len));
|
2012-11-26 00:00:28 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("cb[0] 0x%x\n", cbw->cb[0]));
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
2012-12-13 00:37:10 +08:00
|
|
|
#endif
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mass storage bulk out endpoint request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
* @param size request size.
|
|
|
|
*
|
|
|
|
* @return RT_EOK.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _ep_out_handler(udevice_t device, uclass_t cls, rt_size_t size)
|
2012-11-26 00:00:28 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
mass_eps_t eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
2012-12-13 00:37:10 +08:00
|
|
|
|
|
|
|
eps = (mass_eps_t)cls->eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
if(status == STATUS_CBW)
|
|
|
|
{
|
|
|
|
struct ustorage_cbw* cbw;
|
|
|
|
|
|
|
|
/* dump cbw information */
|
2012-12-13 00:37:10 +08:00
|
|
|
cbw = (struct ustorage_cbw*)eps->ep_out->buffer;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
if(cbw->signature == CBW_SIGNATURE)
|
|
|
|
{
|
|
|
|
csw.signature = CSW_SIGNATURE;
|
|
|
|
csw.tag = cbw->tag;
|
|
|
|
csw.data_reside = 0;
|
|
|
|
csw.status = 0;
|
|
|
|
}
|
2012-12-13 00:37:10 +08:00
|
|
|
else
|
|
|
|
return -RT_ERROR;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
switch(cbw->cb[0])
|
|
|
|
{
|
|
|
|
case SCSI_TEST_UNIT_READY:
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
|
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_REQUEST_SENSE:
|
2012-12-13 00:37:10 +08:00
|
|
|
_request_sense(device, eps->ep_in);
|
2012-11-25 08:02:10 +08:00
|
|
|
status = STATUS_CSW;
|
|
|
|
break;
|
|
|
|
case SCSI_INQUIRY_CMD:
|
2012-12-13 00:37:10 +08:00
|
|
|
_inquiry_cmd(device, eps->ep_in);
|
2012-11-25 08:02:10 +08:00
|
|
|
status = STATUS_CSW;
|
|
|
|
break;
|
|
|
|
case SCSI_MODE_SENSE_6:
|
2012-12-13 00:37:10 +08:00
|
|
|
_mode_sense_6(device, eps->ep_in);
|
2012-11-26 00:00:28 +08:00
|
|
|
status = STATUS_CSW;
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_ALLOW_MEDIUM_REMOVAL:
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
|
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_READ_CAPACITIES:
|
2012-12-13 00:37:10 +08:00
|
|
|
_read_capacities(device, eps->ep_in);
|
2012-11-25 08:02:10 +08:00
|
|
|
status = STATUS_CSW;
|
|
|
|
break;
|
|
|
|
case SCSI_READ_CAPACITY:
|
2012-12-13 00:37:10 +08:00
|
|
|
_read_capacity(device, eps->ep_in);
|
2012-11-26 00:00:28 +08:00
|
|
|
status = STATUS_CSW;
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_READ_10:
|
2012-12-13 00:37:10 +08:00
|
|
|
_read_10(device, cbw, eps->ep_in);
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_WRITE_10:
|
2012-12-13 00:37:10 +08:00
|
|
|
_write_10(device, cbw, eps->ep_out);
|
2012-11-26 00:00:28 +08:00
|
|
|
status = STATUS_RECEIVE;
|
2012-11-25 08:02:10 +08:00
|
|
|
break;
|
|
|
|
case SCSI_VERIFY_10:
|
|
|
|
_verify_10(device);
|
|
|
|
break;
|
2012-11-26 00:00:28 +08:00
|
|
|
}
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
else if(status == STATUS_RECEIVE)
|
|
|
|
{
|
2012-11-26 00:00:28 +08:00
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("write size 0x%x block 0x%x oount 0x%x\n",
|
|
|
|
size, _block, _size));
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
_size -= size;
|
|
|
|
csw.data_reside -= size;
|
2012-12-01 16:00:57 +08:00
|
|
|
|
2012-12-13 00:37:10 +08:00
|
|
|
rt_device_write(disk, _block, eps->ep_in->buffer, 1);
|
2012-12-01 16:00:57 +08:00
|
|
|
_block ++;
|
2012-11-25 08:02:10 +08:00
|
|
|
if(_size == 0)
|
2012-12-01 16:00:57 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_write(device->dcd, eps->ep_in, (rt_uint8_t*)&csw, SIZEOF_CSW);
|
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
|
2012-11-25 08:02:10 +08:00
|
|
|
status = STATUS_CBW;
|
|
|
|
}
|
|
|
|
else
|
2012-11-26 00:00:28 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer,
|
|
|
|
geometry.bytes_per_sector);
|
2012-11-26 00:00:28 +08:00
|
|
|
}
|
2012-11-25 08:02:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_kprintf("none cbw status\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mass storage interface request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
* @param setup the setup request.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-30 17:43:19 +08:00
|
|
|
static rt_err_t _interface_handler(udevice_t device, uclass_t cls, ureq_t setup)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t lun = 0;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
RT_ASSERT(setup != RT_NULL);
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("_interface_handler\n"));
|
|
|
|
|
|
|
|
switch(setup->request)
|
|
|
|
{
|
|
|
|
case USBREQ_GET_MAX_LUN:
|
|
|
|
dcd_ep_write(device->dcd, 0, &lun, 1);
|
|
|
|
break;
|
|
|
|
case USBREQ_MASS_STORAGE_RESET:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_kprintf("unknown interface request\n");
|
|
|
|
break;
|
|
|
|
}
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will run mass storage class, it will be called on handle set configuration request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _class_run(udevice_t device, uclass_t cls)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
mass_eps_t eps;
|
|
|
|
rt_uint8_t *buffer;
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage run\n"));
|
2012-12-13 00:37:10 +08:00
|
|
|
eps = (mass_eps_t)cls->eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
2012-11-26 00:00:28 +08:00
|
|
|
disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
|
|
|
|
if(disk == RT_NULL)
|
2012-11-26 11:39:00 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("no disk named %s\n", RT_USB_MSTORAGE_DISK_NAME);
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2012-11-26 00:00:28 +08:00
|
|
|
if(rt_device_control(disk, RT_DEVICE_CTRL_BLK_GETGEOME, (void*)&geometry) != RT_EOK)
|
2012-11-26 11:39:00 +08:00
|
|
|
return -RT_ERROR;
|
2012-11-26 00:00:28 +08:00
|
|
|
|
|
|
|
buffer = (rt_uint8_t*)rt_malloc(geometry.bytes_per_sector);
|
|
|
|
if(buffer == RT_NULL)
|
2012-11-26 11:39:00 +08:00
|
|
|
return -RT_ENOMEM;
|
2012-12-13 00:37:10 +08:00
|
|
|
eps->ep_out->buffer = buffer;
|
|
|
|
eps->ep_in->buffer = buffer;
|
|
|
|
|
|
|
|
dcd_ep_read(device->dcd, eps->ep_out, eps->ep_out->buffer, SIZEOF_CBW);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will stop mass storage class, it will be called on handle set configuration request.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2012-12-13 00:37:10 +08:00
|
|
|
static rt_err_t _class_stop(udevice_t device, uclass_t cls)
|
2012-11-25 08:02:10 +08:00
|
|
|
{
|
2012-12-13 00:37:10 +08:00
|
|
|
mass_eps_t eps;
|
2012-11-25 08:02:10 +08:00
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
|
|
|
RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage stop\n"));
|
2012-12-13 00:37:10 +08:00
|
|
|
eps = (mass_eps_t)cls->eps;
|
|
|
|
rt_free(eps->ep_in->buffer);
|
|
|
|
eps->ep_out->buffer = RT_NULL;
|
|
|
|
eps->ep_in->buffer = RT_NULL;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct uclass_ops ops =
|
|
|
|
{
|
|
|
|
_class_run,
|
|
|
|
_class_stop,
|
|
|
|
RT_NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will create a mass storage class instance.
|
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
|
|
|
uclass_t rt_usbd_class_mstorage_create(udevice_t device)
|
|
|
|
{
|
|
|
|
uintf_t intf;
|
2012-12-13 00:37:10 +08:00
|
|
|
mass_eps_t eps;
|
|
|
|
uclass_t mstorage;
|
2012-11-25 08:02:10 +08:00
|
|
|
ualtsetting_t setting;
|
2012-12-17 17:31:57 +08:00
|
|
|
umass_desc_t mass_desc;
|
2012-11-25 08:02:10 +08:00
|
|
|
|
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
/* create a mass storage class */
|
|
|
|
mstorage = rt_usbd_class_create(device, &dev_desc, &ops);
|
2012-12-13 00:37:10 +08:00
|
|
|
/* create a mass storage endpoints collection */
|
|
|
|
eps = (mass_eps_t)rt_malloc(sizeof(struct mass_eps));
|
|
|
|
mstorage->eps = (void*)eps;
|
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
/* create an interface */
|
|
|
|
intf = rt_usbd_interface_create(device, _interface_handler);
|
|
|
|
|
|
|
|
/* create an alternate setting */
|
2012-12-17 17:31:57 +08:00
|
|
|
setting = rt_usbd_altsetting_create(sizeof(struct umass_descriptor));
|
|
|
|
/* config desc in alternate setting */
|
|
|
|
rt_usbd_altsetting_config_descriptor(setting, &_mass_desc, 0);
|
|
|
|
|
|
|
|
/* create a bulk out and a bulk in endpoint */
|
|
|
|
mass_desc = (umass_desc_t)setting->desc;
|
|
|
|
eps->ep_in = rt_usbd_endpoint_create(&mass_desc->ep_in_desc, _ep_in_handler);
|
|
|
|
eps->ep_out = rt_usbd_endpoint_create(&mass_desc->ep_out_desc, _ep_out_handler);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
/* add the bulk out and bulk in endpoint to the alternate setting */
|
2012-12-13 00:37:10 +08:00
|
|
|
rt_usbd_altsetting_add_endpoint(setting, eps->ep_out);
|
|
|
|
rt_usbd_altsetting_add_endpoint(setting, eps->ep_in);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
/* add the alternate setting to the interface, then set default setting */
|
|
|
|
rt_usbd_interface_add_altsetting(intf, setting);
|
|
|
|
rt_usbd_set_altsetting(intf, 0);
|
|
|
|
|
|
|
|
/* add the interface to the mass storage class */
|
|
|
|
rt_usbd_class_add_interface(mstorage, intf);
|
2012-11-26 00:00:28 +08:00
|
|
|
|
2012-11-25 08:02:10 +08:00
|
|
|
return mstorage;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|