2013-01-08 22:40:58 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
|
|
|
* 2012-10-01 Yi Qiu first version
|
|
|
|
* 2012-11-25 Heyuanjie87 reduce the memory consumption
|
2021-03-08 18:19:04 +08:00
|
|
|
* 2012-12-09 Heyuanjie87 change function and endpoint handler
|
2014-02-16 17:42:35 +08:00
|
|
|
* 2013-07-25 Yi Qiu update for USB CV test
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2023-10-18 20:50:30 +08:00
|
|
|
#include <rtdevice.h>
|
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
#include "drivers/usb_device.h"
|
2013-01-08 22:40:58 +08:00
|
|
|
#include "mstorage.h"
|
|
|
|
|
2020-03-16 10:22:37 +08:00
|
|
|
#ifdef RT_USING_DFS_MNTTABLE
|
|
|
|
#include "dfs_fs.h"
|
|
|
|
#endif
|
2013-01-08 22:40:58 +08:00
|
|
|
#ifdef RT_USB_DEVICE_MSTORAGE
|
2021-03-11 17:29:41 +08:00
|
|
|
#define MSTRORAGE_INTF_STR_INDEX 11
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
#define DBG_TAG "usbdevice.mstorage"
|
|
|
|
#define DBG_LVL DBG_INFO
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
enum STAT
|
|
|
|
{
|
|
|
|
STAT_CBW,
|
2021-03-08 18:19:04 +08:00
|
|
|
STAT_CMD,
|
2014-02-16 17:42:35 +08:00
|
|
|
STAT_CSW,
|
|
|
|
STAT_RECEIVE,
|
2021-03-08 18:19:04 +08:00
|
|
|
STAT_SEND,
|
2014-02-16 17:42:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
FIXED,
|
|
|
|
COUNT,
|
|
|
|
BLOCK_COUNT,
|
|
|
|
}CB_SIZE_TYPE;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
DIR_IN,
|
|
|
|
DIR_OUT,
|
|
|
|
DIR_NONE,
|
|
|
|
}CB_DIR;
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
typedef rt_ssize_t (*cbw_handler)(ufunction_t func, ustorage_cbw_t cbw);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
struct scsi_cmd
|
|
|
|
{
|
|
|
|
rt_uint16_t cmd;
|
2021-03-08 18:19:04 +08:00
|
|
|
cbw_handler handler;
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_size_t cmd_len;
|
|
|
|
CB_SIZE_TYPE type;
|
|
|
|
rt_size_t data_size;
|
|
|
|
CB_DIR dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mstorage
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct ustorage_csw csw_response;
|
|
|
|
uep_t ep_in;
|
2021-03-08 18:19:04 +08:00
|
|
|
uep_t ep_out;
|
2014-02-16 17:42:35 +08:00
|
|
|
int status;
|
|
|
|
rt_uint32_t cb_data_size;
|
|
|
|
rt_device_t disk;
|
|
|
|
rt_uint32_t block;
|
|
|
|
rt_int32_t count;
|
|
|
|
rt_int32_t size;
|
|
|
|
struct scsi_cmd* processing;
|
2021-03-08 18:19:04 +08:00
|
|
|
struct rt_device_blk_geometry geometry;
|
2014-02-16 17:42:35 +08:00
|
|
|
};
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(4)
|
2013-01-08 22:40:58 +08:00
|
|
|
static struct udevice_descriptor dev_desc =
|
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_DEVICE, //bLength;
|
|
|
|
USB_DESC_TYPE_DEVICE, //type;
|
|
|
|
USB_BCD_VERSION, //bcdUSB;
|
2020-07-03 11:13:24 +08:00
|
|
|
USB_CLASS_MASS_STORAGE, //bDeviceClass;
|
|
|
|
0x06, //bDeviceSubClass;
|
|
|
|
0x50, //bDeviceProtocol;
|
2013-01-08 22:40:58 +08:00
|
|
|
0x40, //bMaxPacketSize0;
|
2013-01-09 12:18:00 +08:00
|
|
|
_VENDOR_ID, //idVendor;
|
|
|
|
_PRODUCT_ID, //idProduct;
|
2013-01-08 22:40:58 +08:00
|
|
|
USB_BCD_DEVICE, //bcdDevice;
|
|
|
|
USB_STRING_MANU_INDEX, //iManufacturer;
|
|
|
|
USB_STRING_PRODUCT_INDEX, //iProduct;
|
|
|
|
USB_STRING_SERIAL_INDEX, //iSerialNumber;
|
|
|
|
USB_DYNAMIC, //bNumConfigurations;
|
|
|
|
};
|
|
|
|
|
2018-03-16 18:36:14 +08:00
|
|
|
//FS and HS needed
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(4)
|
2014-02-16 17:42:35 +08:00
|
|
|
static struct usb_qualifier_descriptor dev_qualifier =
|
|
|
|
{
|
2018-03-16 18:36:14 +08:00
|
|
|
sizeof(dev_qualifier), //bLength
|
|
|
|
USB_DESC_TYPE_DEVICEQUALIFIER, //bDescriptorType
|
|
|
|
0x0200, //bcdUSB
|
2020-07-03 11:13:24 +08:00
|
|
|
USB_CLASS_MASS_STORAGE, //bDeviceClass
|
|
|
|
0x06, //bDeviceSubClass
|
|
|
|
0x50, //bDeviceProtocol
|
2018-03-16 18:36:14 +08:00
|
|
|
64, //bMaxPacketSize0
|
|
|
|
0x01, //bNumConfigurations
|
2014-02-16 17:42:35 +08:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
2017-11-11 10:51:47 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(4)
|
2013-01-08 22:40:58 +08:00
|
|
|
const static struct umass_descriptor _mass_desc =
|
|
|
|
{
|
2017-11-29 18:33:09 +08:00
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
/* Interface Association Descriptor */
|
2018-08-28 03:18:39 +08:00
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_IAD,
|
|
|
|
USB_DESC_TYPE_IAD,
|
|
|
|
USB_DYNAMIC,
|
|
|
|
0x01,
|
|
|
|
USB_CLASS_MASS_STORAGE,
|
|
|
|
0x06,
|
|
|
|
0x50,
|
|
|
|
0x00,
|
|
|
|
},
|
2017-11-29 18:33:09 +08:00
|
|
|
#endif
|
2018-08-28 03:18:39 +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;
|
2021-03-11 17:29:41 +08:00
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
MSTRORAGE_INTF_STR_INDEX,
|
|
|
|
#else
|
2018-08-28 03:18:39 +08:00
|
|
|
0x00, //iInterface;
|
2021-03-11 17:29:41 +08:00
|
|
|
#endif
|
2018-08-28 03:18:39 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_ENDPOINT, //bLength;
|
|
|
|
USB_DESC_TYPE_ENDPOINT, //type;
|
|
|
|
USB_DYNAMIC | USB_DIR_OUT, //bEndpointAddress;
|
|
|
|
USB_EP_ATTR_BULK, //bmAttributes;
|
|
|
|
USB_DYNAMIC, //wMaxPacketSize;
|
|
|
|
0x00, //bInterval;
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
USB_DESC_LENGTH_ENDPOINT, //bLength;
|
|
|
|
USB_DESC_TYPE_ENDPOINT, //type;
|
|
|
|
USB_DYNAMIC | USB_DIR_IN, //bEndpointAddress;
|
|
|
|
USB_EP_ATTR_BULK, //bmAttributes;
|
|
|
|
USB_DYNAMIC, //wMaxPacketSize;
|
|
|
|
0x00, //bInterval;
|
|
|
|
},
|
2013-01-08 22:40:58 +08:00
|
|
|
};
|
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(4)
|
2013-01-24 20:59:43 +08:00
|
|
|
const static char* _ustring[] =
|
|
|
|
{
|
|
|
|
"Language",
|
|
|
|
"RT-Thread Team.",
|
|
|
|
"RTT Mass Storage",
|
2014-02-16 17:42:35 +08:00
|
|
|
"320219198301",
|
2013-01-24 20:59:43 +08:00
|
|
|
"Configuration",
|
|
|
|
"Interface",
|
|
|
|
};
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _test_unit_ready(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _request_sense(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _inquiry_cmd(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _allow_removal(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _start_stop(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _mode_sense_6(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _read_capacities(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _read_capacity(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _read_10(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _write_10(ufunction_t func, ustorage_cbw_t cbw);
|
|
|
|
static rt_ssize_t _verify_10(ufunction_t func, ustorage_cbw_t cbw);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2022-12-12 02:12:03 +08:00
|
|
|
rt_align(4)
|
2014-02-16 17:42:35 +08:00
|
|
|
static struct scsi_cmd cmd_data[] =
|
|
|
|
{
|
|
|
|
{SCSI_TEST_UNIT_READY, _test_unit_ready, 6, FIXED, 0, DIR_NONE},
|
|
|
|
{SCSI_REQUEST_SENSE, _request_sense, 6, COUNT, 0, DIR_IN},
|
|
|
|
{SCSI_INQUIRY_CMD, _inquiry_cmd, 6, COUNT, 0, DIR_IN},
|
|
|
|
{SCSI_ALLOW_REMOVAL, _allow_removal, 6, FIXED, 0, DIR_NONE},
|
2021-03-08 18:19:04 +08:00
|
|
|
{SCSI_MODE_SENSE_6, _mode_sense_6, 6, COUNT, 0, DIR_IN},
|
|
|
|
{SCSI_START_STOP, _start_stop, 6, FIXED, 0, DIR_NONE},
|
|
|
|
{SCSI_READ_CAPACITIES, _read_capacities, 10, COUNT, 0, DIR_NONE},
|
2014-02-16 17:42:35 +08:00
|
|
|
{SCSI_READ_CAPACITY, _read_capacity, 10, FIXED, 8, DIR_IN},
|
|
|
|
{SCSI_READ_10, _read_10, 10, BLOCK_COUNT, 0, DIR_IN},
|
|
|
|
{SCSI_WRITE_10, _write_10, 10, BLOCK_COUNT, 0, DIR_OUT},
|
|
|
|
{SCSI_VERIFY_10, _verify_10, 10, FIXED, 0, DIR_NONE},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void _send_status(ufunction_t func)
|
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_send_status");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_in->request.buffer = (rt_uint8_t*)&data->csw_response;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_in->request.size = SIZEOF_CSW;
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CSW;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _test_unit_ready(ufunction_t func, ustorage_cbw_t cbw)
|
2014-02-16 17:42:35 +08:00
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_test_unit_ready");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
data->csw_response.status = 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _allow_removal(ufunction_t func, ustorage_cbw_t cbw)
|
2014-02-16 17:42:35 +08:00
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_allow_removal");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
data->csw_response.status = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/**
|
2014-02-16 17:42:35 +08:00
|
|
|
* This function will handle inquiry command request.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
2021-03-08 18:19:04 +08:00
|
|
|
* @param func the usb function object.
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param cbw the command block wrapper.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @return RT_EOK on successful.
|
2013-01-08 22:40:58 +08:00
|
|
|
*/
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _inquiry_cmd(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
rt_uint8_t *buf;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_inquiry_cmd");
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
buf = data->ep_in->buffer;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
*(rt_uint32_t*)&buf[0] = 0x0 | (0x80 << 8);
|
|
|
|
*(rt_uint32_t*)&buf[4] = 31;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_memset(&buf[8], 0x20, 28);
|
|
|
|
rt_memcpy(&buf[8], "RTT", 3);
|
|
|
|
rt_memcpy(&buf[16], "USB Disk", 8);
|
|
|
|
|
|
|
|
data->cb_data_size = MIN(data->cb_data_size, SIZEOF_INQUIRY_CMD);
|
|
|
|
data->ep_in->request.buffer = buf;
|
|
|
|
data->ep_in->request.size = data->cb_data_size;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CMD;
|
|
|
|
|
|
|
|
return data->cb_data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle sense request.
|
|
|
|
*
|
2021-03-08 18:19:04 +08:00
|
|
|
* @param func the usb function object.
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param cbw the command block wrapper.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _request_sense(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
struct request_sense_data *buf;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_request_sense");
|
2021-03-08 18:19:04 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
buf = (struct request_sense_data *)data->ep_in->buffer;
|
|
|
|
|
|
|
|
buf->ErrorCode = 0x70;
|
|
|
|
buf->Valid = 0;
|
|
|
|
buf->SenseKey = 2;
|
|
|
|
buf->Information[0] = 0;
|
|
|
|
buf->Information[1] = 0;
|
|
|
|
buf->Information[2] = 0;
|
|
|
|
buf->Information[3] = 0;
|
|
|
|
buf->AdditionalSenseLength = 0x0a;
|
|
|
|
buf->AdditionalSenseCode = 0x3a;
|
|
|
|
buf->AdditionalSenseCodeQualifier = 0;
|
|
|
|
|
|
|
|
data->cb_data_size = MIN(data->cb_data_size, SIZEOF_REQUEST_SENSE);
|
|
|
|
data->ep_in->request.buffer = (rt_uint8_t*)data->ep_in->buffer;
|
|
|
|
data->ep_in->request.size = data->cb_data_size;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CMD;
|
|
|
|
|
|
|
|
return data->cb_data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mode_sense_6 request.
|
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2021-03-08 18:19:04 +08:00
|
|
|
* @param cbw the command block wrapper.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _mode_sense_6(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
rt_uint8_t *buf;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_mode_sense_6");
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
buf = data->ep_in->buffer;
|
|
|
|
buf[0] = 3;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = 0;
|
|
|
|
buf[3] = 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->cb_data_size = MIN(data->cb_data_size, SIZEOF_MODE_SENSE_6);
|
|
|
|
data->ep_in->request.buffer = buf;
|
|
|
|
data->ep_in->request.size = data->cb_data_size;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CMD;
|
|
|
|
|
|
|
|
return data->cb_data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_capacities request.
|
|
|
|
*
|
2021-03-08 18:19:04 +08:00
|
|
|
* @param func the usb function object.
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param cbw the command block wrapper.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _read_capacities(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
rt_uint8_t *buf;
|
2013-01-08 22:40:58 +08:00
|
|
|
rt_uint32_t sector_count, sector_size;
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_read_capacities");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
buf = data->ep_in->buffer;
|
|
|
|
sector_count = data->geometry.sector_count;
|
|
|
|
sector_size = data->geometry.bytes_per_sector;
|
|
|
|
|
|
|
|
*(rt_uint32_t*)&buf[0] = 0x08000000;
|
|
|
|
buf[4] = sector_count >> 24;
|
|
|
|
buf[5] = 0xff & (sector_count >> 16);
|
|
|
|
buf[6] = 0xff & (sector_count >> 8);
|
|
|
|
buf[7] = 0xff & (sector_count);
|
|
|
|
buf[8] = 0x02;
|
|
|
|
buf[9] = 0xff & (sector_size >> 16);
|
|
|
|
buf[10] = 0xff & (sector_size >> 8);
|
|
|
|
buf[11] = 0xff & sector_size;
|
|
|
|
|
|
|
|
data->cb_data_size = MIN(data->cb_data_size, SIZEOF_READ_CAPACITIES);
|
|
|
|
data->ep_in->request.buffer = buf;
|
|
|
|
data->ep_in->request.size = data->cb_data_size;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CMD;
|
|
|
|
|
|
|
|
return data->cb_data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_capacity request.
|
|
|
|
*
|
2021-03-08 18:19:04 +08:00
|
|
|
* @param func the usb function object.
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param cbw the command block wapper.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _read_capacity(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_uint8_t *buf;
|
|
|
|
rt_uint32_t sector_count, sector_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_read_capacity");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
buf = data->ep_in->buffer;
|
2020-04-28 19:07:08 +08:00
|
|
|
sector_count = data->geometry.sector_count - 1; /* Last Logical Block Address */
|
2014-02-16 17:42:35 +08:00
|
|
|
sector_size = data->geometry.bytes_per_sector;
|
|
|
|
|
|
|
|
buf[0] = sector_count >> 24;
|
|
|
|
buf[1] = 0xff & (sector_count >> 16);
|
|
|
|
buf[2] = 0xff & (sector_count >> 8);
|
|
|
|
buf[3] = 0xff & (sector_count);
|
|
|
|
buf[4] = 0x0;
|
|
|
|
buf[5] = 0xff & (sector_size >> 16);
|
|
|
|
buf[6] = 0xff & (sector_size >> 8);
|
|
|
|
buf[7] = 0xff & sector_size;
|
|
|
|
|
|
|
|
data->cb_data_size = MIN(data->cb_data_size, SIZEOF_READ_CAPACITY);
|
|
|
|
data->ep_in->request.buffer = buf;
|
|
|
|
data->ep_in->request.size = data->cb_data_size;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_CMD;
|
|
|
|
|
|
|
|
return data->cb_data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle read_10 request.
|
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @param cbw the command block wrapper.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _read_10(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
rt_size_t size;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
data->block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
|
|
|
|
cbw->cb[5]<<0;
|
|
|
|
data->count = cbw->cb[7]<<8 | cbw->cb[8]<<0;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(data->count < data->geometry.sector_count);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data->csw_response.data_reside = data->cb_data_size;
|
2014-02-16 17:42:35 +08:00
|
|
|
size = rt_device_read(data->disk, data->block, data->ep_in->buffer, 1);
|
|
|
|
if(size == 0)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_kprintf("read data error\n");
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_in->request.buffer = data->ep_in->buffer;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_in->request.size = data->geometry.bytes_per_sector;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
|
|
|
data->status = STAT_SEND;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return data->geometry.bytes_per_sector;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle write_10 request.
|
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @param cbw the command block wrapper.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _write_10(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data->block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
|
|
|
|
cbw->cb[5]<<0;
|
|
|
|
data->count = cbw->cb[7]<<8 | cbw->cb[8];
|
|
|
|
data->csw_response.data_reside = cbw->xfer_len;
|
|
|
|
data->size = data->count * data->geometry.bytes_per_sector;
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_write_10 count 0x%x block 0x%x 0x%x",
|
|
|
|
data->count, data->block, data->geometry.sector_count);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data->csw_response.data_reside = data->cb_data_size;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_out->request.buffer = data->ep_out->buffer;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_out->request.size = data->geometry.bytes_per_sector;
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
|
|
|
data->status = STAT_RECEIVE;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return data->geometry.bytes_per_sector;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle verify_10 request.
|
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _verify_10(ufunction_t func, ustorage_cbw_t cbw)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_verify_10");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
data->csw_response.status = 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return 0;
|
2013-06-15 23:02:50 +08:00
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _start_stop(ufunction_t func,
|
2014-02-16 17:42:35 +08:00
|
|
|
ustorage_cbw_t cbw)
|
2013-06-15 23:02:50 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_start_stop");
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
data->csw_response.status = 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return 0;
|
2013-06-15 23:02:50 +08:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
static rt_err_t _ep_in_handler(ufunction_t func, rt_size_t size)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2013-01-24 20:59:43 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_ep_in_handler");
|
2021-03-08 18:19:04 +08:00
|
|
|
|
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
switch(data->status)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
case STAT_CSW:
|
|
|
|
if(data->ep_in->request.size != SIZEOF_CSW)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_kprintf("Size of csw command error\n");
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_in);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("return to cbw status");
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_out->request.buffer = data->ep_out->buffer;
|
|
|
|
data->ep_out->request.size = SIZEOF_CBW;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
2014-02-16 17:42:35 +08:00
|
|
|
data->status = STAT_CBW;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
break;
|
|
|
|
case STAT_CMD:
|
|
|
|
if(data->csw_response.data_reside == 0xFF)
|
|
|
|
{
|
|
|
|
data->csw_response.data_reside = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data->csw_response.data_reside -= data->ep_in->request.size;
|
|
|
|
if(data->csw_response.data_reside != 0)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("data_reside %d, request %d",
|
|
|
|
data->csw_response.data_reside, data->ep_in->request.size);
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->processing->dir == DIR_OUT)
|
|
|
|
{
|
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_out);
|
|
|
|
}
|
|
|
|
else
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2020-07-03 11:13:24 +08:00
|
|
|
//rt_kprintf("warning:in stall path but not stall\n");
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-07-03 11:13:24 +08:00
|
|
|
/* FIXME: Disable the operation or the disk cannot work. */
|
2021-03-08 18:19:04 +08:00
|
|
|
//rt_usbd_ep_set_stall(func->device, data->ep_in);
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
data->csw_response.data_reside = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_send_status(func);
|
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
case STAT_SEND:
|
2014-02-16 17:42:35 +08:00
|
|
|
data->csw_response.data_reside -= data->ep_in->request.size;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->count--;
|
|
|
|
data->block++;
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->count > 0 && data->csw_response.data_reside > 0)
|
|
|
|
{
|
|
|
|
if(rt_device_read(data->disk, data->block, data->ep_in->buffer, 1) == 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("disk read error\n");
|
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_in);
|
2021-03-08 18:19:04 +08:00
|
|
|
return -RT_ERROR;
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
data->ep_in->request.buffer = data->ep_in->buffer;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_in->request.size = data->geometry.bytes_per_sector;
|
|
|
|
data->ep_in->request.req_type = UIO_REQUEST_WRITE;
|
|
|
|
rt_usbd_io_request(func->device, data->ep_in, &data->ep_in->request);
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
_send_status(func);
|
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return RT_EOK;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MASS_CBW_DUMP
|
|
|
|
static void cbw_dump(struct ustorage_cbw* cbw)
|
|
|
|
{
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("signature 0x%x", cbw->signature);
|
|
|
|
LOG_D("tag 0x%x", cbw->tag);
|
|
|
|
LOG_D("xfer_len 0x%x", cbw->xfer_len);
|
|
|
|
LOG_D("dflags 0x%x", cbw->dflags);
|
|
|
|
LOG_D("lun 0x%x", cbw->lun);
|
|
|
|
LOG_D("cb_len 0x%x", cbw->cb_len);
|
|
|
|
LOG_D("cb[0] 0x%x", cbw->cb[0]);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
static struct scsi_cmd* _find_cbw_command(rt_uint16_t cmd)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
int i;
|
2013-01-24 20:59:43 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
for(i=0; i<sizeof(cmd_data)/sizeof(struct scsi_cmd); i++)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
if(cmd_data[i].cmd == cmd)
|
|
|
|
return &cmd_data[i];
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
static void _cb_len_calc(ufunction_t func, struct scsi_cmd* cmd,
|
|
|
|
ustorage_cbw_t cbw)
|
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
RT_ASSERT(cmd != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
if(cmd->cmd_len == 6)
|
|
|
|
{
|
|
|
|
switch(cmd->type)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
case COUNT:
|
|
|
|
data->cb_data_size = cbw->cb[4];
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2014-02-16 17:42:35 +08:00
|
|
|
case BLOCK_COUNT:
|
|
|
|
data->cb_data_size = cbw->cb[4] * data->geometry.bytes_per_sector;
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2014-02-16 17:42:35 +08:00
|
|
|
case FIXED:
|
|
|
|
data->cb_data_size = cmd->data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
default:
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
else if(cmd->cmd_len == 10)
|
|
|
|
{
|
|
|
|
switch(cmd->type)
|
|
|
|
{
|
|
|
|
case COUNT:
|
|
|
|
data->cb_data_size = cbw->cb[7]<<8 | cbw->cb[8];
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2014-02-16 17:42:35 +08:00
|
|
|
case BLOCK_COUNT:
|
2021-03-08 18:19:04 +08:00
|
|
|
data->cb_data_size = (cbw->cb[7]<<8 | cbw->cb[8]) *
|
2014-02-16 17:42:35 +08:00
|
|
|
data->geometry.bytes_per_sector;
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2014-02-16 17:42:35 +08:00
|
|
|
case FIXED:
|
|
|
|
data->cb_data_size = cmd->data_size;
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
default:
|
2013-06-15 23:02:50 +08:00
|
|
|
break;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2020-07-03 11:13:24 +08:00
|
|
|
//workaround: for stability in full-speed mode
|
|
|
|
else if(cmd->cmd_len == 12)
|
|
|
|
{
|
|
|
|
switch(cmd->type)
|
|
|
|
{
|
|
|
|
case COUNT:
|
2021-03-08 18:19:04 +08:00
|
|
|
data->cb_data_size = cbw->cb[4];
|
2020-07-03 11:13:24 +08:00
|
|
|
break;
|
2021-03-08 18:19:04 +08:00
|
|
|
default:
|
2020-07-03 11:13:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
else
|
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
rt_kprintf("cmd_len error %d\n", cmd->cmd_len);
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static rt_bool_t _cbw_verify(ufunction_t func, struct scsi_cmd* cmd,
|
|
|
|
ustorage_cbw_t cbw)
|
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(cmd != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
if(cmd->cmd_len != cbw->cb_len)
|
|
|
|
{
|
2020-07-03 11:13:24 +08:00
|
|
|
rt_kprintf("cb_len error\n");
|
2014-02-16 17:42:35 +08:00
|
|
|
cmd->cmd_len = cbw->cb_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cbw->xfer_len > 0 && data->cb_data_size == 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("xfer_len > 0 && data_size == 0\n");
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cbw->xfer_len == 0 && data->cb_data_size > 0)
|
|
|
|
{
|
|
|
|
rt_kprintf("xfer_len == 0 && data_size > 0");
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
2018-08-28 03:18:39 +08:00
|
|
|
if(((cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_OUT)) ||
|
|
|
|
(!(cbw->dflags & USB_DIR_IN) && (cmd->dir == DIR_IN)))
|
2014-02-16 17:42:35 +08:00
|
|
|
{
|
|
|
|
rt_kprintf("dir error\n");
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cbw->xfer_len > data->cb_data_size)
|
|
|
|
{
|
|
|
|
rt_kprintf("xfer_len > data_size\n");
|
|
|
|
return RT_FALSE;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
if(cbw->xfer_len < data->cb_data_size)
|
|
|
|
{
|
2020-07-03 11:13:24 +08:00
|
|
|
rt_kprintf("xfer_len < data_size\n");
|
2014-02-16 17:42:35 +08:00
|
|
|
data->cb_data_size = cbw->xfer_len;
|
|
|
|
data->csw_response.status = 1;
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
return RT_TRUE;
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
|
2023-02-06 07:35:33 +08:00
|
|
|
static rt_ssize_t _cbw_handler(ufunction_t func, struct scsi_cmd* cmd,
|
2014-02-16 17:42:35 +08:00
|
|
|
ustorage_cbw_t cbw)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
RT_ASSERT(cbw != RT_NULL);
|
|
|
|
RT_ASSERT(cmd->handler != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
|
|
|
data->processing = cmd;
|
|
|
|
return cmd->handler(func, cbw);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mass storage bulk out endpoint request.
|
|
|
|
*
|
|
|
|
* @param func the usb function object.
|
|
|
|
* @param size request size.
|
|
|
|
*
|
|
|
|
* @return RT_EOK.
|
|
|
|
*/
|
|
|
|
static rt_err_t _ep_out_handler(ufunction_t func, rt_size_t size)
|
|
|
|
{
|
|
|
|
struct mstorage *data;
|
|
|
|
struct scsi_cmd* cmd;
|
|
|
|
rt_size_t len;
|
|
|
|
struct ustorage_cbw* cbw;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("_ep_out_handler %d", size);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2021-03-08 18:19:04 +08:00
|
|
|
cbw = (struct ustorage_cbw*)data->ep_out->buffer;
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->status == STAT_CBW)
|
|
|
|
{
|
|
|
|
/* dump cbw information */
|
|
|
|
if(cbw->signature != CBW_SIGNATURE || size != SIZEOF_CBW)
|
|
|
|
{
|
|
|
|
goto exit;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
data->csw_response.signature = CSW_SIGNATURE;
|
|
|
|
data->csw_response.tag = cbw->tag;
|
|
|
|
data->csw_response.data_reside = cbw->xfer_len;
|
|
|
|
data->csw_response.status = 0;
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("ep_out reside %d", data->csw_response.data_reside);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
cmd = _find_cbw_command(cbw->cb[0]);
|
|
|
|
if(cmd == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("can't find cbw command\n");
|
|
|
|
goto exit;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
_cb_len_calc(func, cmd, cbw);
|
|
|
|
if(!_cbw_verify(func, cmd, cbw))
|
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
len = _cbw_handler(func, cmd, cbw);
|
|
|
|
if(len == 0)
|
|
|
|
{
|
|
|
|
_send_status(func);
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
else if(data->status == STAT_RECEIVE)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("write size %d block 0x%x oount 0x%x",
|
|
|
|
size, data->block, data->size);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->size -= size;
|
|
|
|
data->csw_response.data_reside -= size;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_device_write(data->disk, data->block, data->ep_out->buffer, 1);
|
2013-01-24 20:59:43 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->csw_response.data_reside != 0)
|
2013-01-24 20:59:43 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_out->request.buffer = data->ep_out->buffer;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_out->request.size = data->geometry.bytes_per_sector;
|
|
|
|
data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
2021-03-08 18:19:04 +08:00
|
|
|
data->block ++;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
_send_status(func);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
return RT_EOK;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if(data->csw_response.data_reside)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
if(cbw->dflags & USB_DIR_IN)
|
|
|
|
{
|
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_in);
|
|
|
|
rt_usbd_ep_set_stall(func->device, data->ep_out);
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2014-02-16 17:42:35 +08:00
|
|
|
data->csw_response.status = 1;
|
|
|
|
_send_status(func);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return -RT_ERROR;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will handle mass storage interface request.
|
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2013-01-08 22:40:58 +08:00
|
|
|
* @param setup the setup request.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2014-02-16 17:42:35 +08:00
|
|
|
static rt_err_t _interface_handler(ufunction_t func, ureq_t setup)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
rt_uint8_t lun = 0;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
RT_ASSERT(func != RT_NULL);
|
2021-03-08 18:19:04 +08:00
|
|
|
RT_ASSERT(func->device != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
RT_ASSERT(setup != RT_NULL);
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("mstorage_interface_handler");
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2017-11-29 14:26:15 +08:00
|
|
|
switch(setup->bRequest)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2021-03-08 18:19:04 +08:00
|
|
|
case USBREQ_GET_MAX_LUN:
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("USBREQ_GET_MAX_LUN");
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2017-11-29 14:26:15 +08:00
|
|
|
if(setup->wValue || setup->wLength != 1)
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_ep0_set_stall(func->device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-11-29 14:26:15 +08:00
|
|
|
rt_usbd_ep0_write(func->device, &lun, setup->wLength);
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
|
|
|
case USBREQ_MASS_STORAGE_RESET:
|
2014-02-16 17:42:35 +08:00
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("USBREQ_MASS_STORAGE_RESET");
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2017-11-29 14:26:15 +08:00
|
|
|
if(setup->wValue || setup->wLength != 0)
|
2014-02-16 17:42:35 +08:00
|
|
|
{
|
|
|
|
rt_usbd_ep0_set_stall(func->device);
|
|
|
|
}
|
|
|
|
else
|
2021-03-08 18:19:04 +08:00
|
|
|
{
|
|
|
|
dcd_ep0_send_status(func->device->dcd);
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rt_kprintf("unknown interface request\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-16 17:42:35 +08:00
|
|
|
* This function will run mass storage function, it will be called on handle set configuration request.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
2014-02-16 17:42:35 +08:00
|
|
|
* @param func the usb function object.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2014-02-16 17:42:35 +08:00
|
|
|
static rt_err_t _function_enable(ufunction_t func)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("Mass storage function enabled");
|
2020-03-16 10:22:37 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
|
|
|
|
if(data->disk == RT_NULL)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_kprintf("no data->disk named %s\n", RT_USB_MSTORAGE_DISK_NAME);
|
2013-01-08 22:40:58 +08:00
|
|
|
return -RT_ERROR;
|
2013-01-24 20:59:43 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2020-03-16 10:22:37 +08:00
|
|
|
#ifdef RT_USING_DFS_MNTTABLE
|
|
|
|
dfs_unmount_device(data->disk);
|
|
|
|
#endif
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
if(rt_device_open(data->disk, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("disk open error\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
|
|
|
if(rt_device_control(data->disk, RT_DEVICE_CTRL_BLK_GETGEOME,
|
2014-02-16 17:42:35 +08:00
|
|
|
(void*)&data->geometry) != RT_EOK)
|
|
|
|
{
|
|
|
|
rt_kprintf("get disk info error\n");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_in->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
|
|
|
|
if(data->ep_in->buffer == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_kprintf("no memory\n");
|
2013-01-08 22:40:58 +08:00
|
|
|
return -RT_ENOMEM;
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
|
|
|
data->ep_out->buffer = (rt_uint8_t*)rt_malloc(data->geometry.bytes_per_sector);
|
|
|
|
if(data->ep_out->buffer == RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(data->ep_in->buffer);
|
|
|
|
rt_kprintf("no memory\n");
|
|
|
|
return -RT_ENOMEM;
|
2021-03-08 18:19:04 +08:00
|
|
|
}
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* prepare to read CBW request */
|
|
|
|
data->ep_out->request.buffer = data->ep_out->buffer;
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_out->request.size = SIZEOF_CBW;
|
|
|
|
data->ep_out->request.req_type = UIO_REQUEST_READ_FULL;
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_io_request(func->device, data->ep_out, &data->ep_out->request);
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-02-16 17:42:35 +08:00
|
|
|
* This function will stop mass storage function, it will be called on handle set configuration request.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2014-02-16 17:42:35 +08:00
|
|
|
static rt_err_t _function_disable(ufunction_t func)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
RT_ASSERT(func != RT_NULL);
|
|
|
|
|
2023-06-29 23:24:25 +08:00
|
|
|
LOG_D("Mass storage function disabled");
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
data = (struct mstorage*)func->user_data;
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->ep_in->buffer != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(data->ep_in->buffer);
|
2021-03-08 18:19:04 +08:00
|
|
|
data->ep_in->buffer = RT_NULL;
|
2014-02-16 17:42:35 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
if(data->ep_out->buffer != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_free(data->ep_out->buffer);
|
|
|
|
data->ep_out->buffer = RT_NULL;
|
|
|
|
}
|
2017-12-21 21:26:40 +08:00
|
|
|
if(data->disk != RT_NULL)
|
|
|
|
{
|
|
|
|
rt_device_close(data->disk);
|
2020-03-16 10:22:37 +08:00
|
|
|
#ifdef RT_USING_DFS_MNTTABLE
|
|
|
|
dfs_mount_device(data->disk);
|
|
|
|
#endif
|
2017-12-21 21:26:40 +08:00
|
|
|
data->disk = RT_NULL;
|
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
data->status = STAT_CBW;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
static struct ufunction_ops ops =
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2014-02-16 17:42:35 +08:00
|
|
|
_function_enable,
|
|
|
|
_function_disable,
|
2013-01-08 22:40:58 +08:00
|
|
|
RT_NULL,
|
|
|
|
};
|
2018-03-16 18:36:14 +08:00
|
|
|
static rt_err_t _mstorage_descriptor_config(umass_desc_t desc, rt_uint8_t cintf_nr, rt_uint8_t device_is_hs)
|
2017-11-29 18:33:09 +08:00
|
|
|
{
|
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
desc->iad_desc.bFirstInterface = cintf_nr;
|
|
|
|
#endif
|
2018-03-16 18:36:14 +08:00
|
|
|
desc->ep_out_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
|
|
|
|
desc->ep_in_desc.wMaxPacketSize = device_is_hs ? 512 : 64;
|
2017-11-29 18:33:09 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
/**
|
2014-02-16 17:42:35 +08:00
|
|
|
* This function will create a mass storage function instance.
|
2013-01-08 22:40:58 +08:00
|
|
|
*
|
|
|
|
* @param device the usb device object.
|
|
|
|
*
|
|
|
|
* @return RT_EOK on successful.
|
|
|
|
*/
|
2014-02-16 17:42:35 +08:00
|
|
|
ufunction_t rt_usbd_function_mstorage_create(udevice_t device)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
|
|
|
uintf_t intf;
|
2014-02-16 17:42:35 +08:00
|
|
|
struct mstorage *data;
|
|
|
|
ufunction_t func;
|
2013-01-08 22:40:58 +08:00
|
|
|
ualtsetting_t setting;
|
|
|
|
umass_desc_t mass_desc;
|
|
|
|
|
|
|
|
/* parameter check */
|
|
|
|
RT_ASSERT(device != RT_NULL);
|
|
|
|
|
2013-01-24 20:59:43 +08:00
|
|
|
/* set usb device string description */
|
2021-03-11 17:29:41 +08:00
|
|
|
#ifdef RT_USB_DEVICE_COMPOSITE
|
|
|
|
rt_usbd_device_set_interface_string(device, MSTRORAGE_INTF_STR_INDEX, _ustring[2]);
|
|
|
|
#else
|
2013-01-24 20:59:43 +08:00
|
|
|
rt_usbd_device_set_string(device, _ustring);
|
2021-03-11 17:29:41 +08:00
|
|
|
#endif
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* create a mass storage function */
|
|
|
|
func = rt_usbd_function_new(device, &dev_desc, &ops);
|
|
|
|
device->dev_qualifier = &dev_qualifier;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* allocate memory for mass storage function data */
|
|
|
|
data = (struct mstorage*)rt_malloc(sizeof(struct mstorage));
|
|
|
|
rt_memset(data, 0, sizeof(struct mstorage));
|
|
|
|
func->user_data = (void*)data;
|
|
|
|
|
|
|
|
/* create an interface object */
|
|
|
|
intf = rt_usbd_interface_new(device, _interface_handler);
|
|
|
|
|
|
|
|
/* create an alternate setting object */
|
|
|
|
setting = rt_usbd_altsetting_new(sizeof(struct umass_descriptor));
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-01-24 20:59:43 +08:00
|
|
|
/* config desc in alternate setting */
|
2017-11-29 18:33:09 +08:00
|
|
|
rt_usbd_altsetting_config_descriptor(setting, &_mass_desc, (rt_off_t)&((umass_desc_t)0)->intf_desc);
|
|
|
|
|
|
|
|
/* configure the msc interface descriptor */
|
2018-03-16 18:36:14 +08:00
|
|
|
_mstorage_descriptor_config(setting->desc, intf->intf_num, device->dcd->device_is_hs);
|
2013-01-24 20:59:43 +08:00
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
/* create a bulk out and a bulk in endpoint */
|
|
|
|
mass_desc = (umass_desc_t)setting->desc;
|
2014-02-16 17:42:35 +08:00
|
|
|
data->ep_in = rt_usbd_endpoint_new(&mass_desc->ep_in_desc, _ep_in_handler);
|
|
|
|
data->ep_out = rt_usbd_endpoint_new(&mass_desc->ep_out_desc, _ep_out_handler);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
/* add the bulk out and bulk in endpoint to the alternate setting */
|
2014-02-16 17:42:35 +08:00
|
|
|
rt_usbd_altsetting_add_endpoint(setting, data->ep_out);
|
|
|
|
rt_usbd_altsetting_add_endpoint(setting, data->ep_in);
|
2013-01-08 22:40:58 +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);
|
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
/* add the interface to the mass storage function */
|
|
|
|
rt_usbd_function_add_interface(func, intf);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2014-02-16 17:42:35 +08:00
|
|
|
return func;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
struct udclass msc_class =
|
2018-04-24 12:48:05 +08:00
|
|
|
{
|
|
|
|
.rt_usbd_function_create = rt_usbd_function_mstorage_create
|
|
|
|
};
|
|
|
|
|
|
|
|
int rt_usbd_msc_class_register(void)
|
|
|
|
{
|
|
|
|
rt_usbd_class_register(&msc_class);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
INIT_PREV_EXPORT(rt_usbd_msc_class_register);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
|
|
|
#endif
|