rt-thread/components/drivers/usb/usbhost/class/udisk.c

443 lines
12 KiB
C
Raw Normal View History

2017-11-11 10:51:47 +08:00
/*
2018-10-14 19:37:18 +08:00
* Copyright (c) 2006-2018, RT-Thread Development Team
2017-11-11 10:51:47 +08:00
*
2018-10-14 19:37:18 +08:00
* SPDX-License-Identifier: Apache-2.0
2017-11-11 10:51:47 +08:00
*
* Change Logs:
* Date Author Notes
* 2011-12-12 Yi Qiu first version
*/
#include <rtthread.h>
#include <dfs_fs.h>
#include <drivers/usb_host.h>
#include "mass.h"
#ifdef RT_USBH_MSTORAGE
#define UDISK_MAX_COUNT 8
static rt_uint8_t _udisk_idset = 0;
static int udisk_get_id(void)
{
int i;
for(i=0; i< UDISK_MAX_COUNT; i++)
{
if((_udisk_idset & (1 << i)) != 0) continue;
else break;
}
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* it should not happen */
if(i == UDISK_MAX_COUNT) RT_ASSERT(0);
_udisk_idset |= (1 << i);
return i;
}
static void udisk_free_id(int id)
{
RT_ASSERT(id < UDISK_MAX_COUNT)
2018-06-10 17:59:17 +08:00
_udisk_idset &= ~(1 << id);
2017-11-11 10:51:47 +08:00
}
/**
* This function will initialize the udisk device
*
* @param dev the pointer of device driver structure
2018-06-10 17:59:17 +08:00
*
2017-11-11 10:51:47 +08:00
* @return RT_EOK
*/
static rt_err_t rt_udisk_init(rt_device_t dev)
{
return RT_EOK;
}
/**
* This function will read some data from a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of reading
* @param buffer the data buffer to save read data
* @param size the size of buffer
*
* @return the actually read size on successful, otherwise negative returned.
*/
2018-06-10 17:59:17 +08:00
static rt_size_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void* buffer,
2017-11-11 10:51:47 +08:00
rt_size_t size)
{
rt_err_t ret;
2017-12-14 03:14:44 +08:00
struct uhintf* intf;
2017-11-11 10:51:47 +08:00
struct ustor_data* data;
int timeout = USB_TIMEOUT_LONG;
2017-11-11 10:51:47 +08:00
/* check parameter */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(buffer != RT_NULL);
if(size > 4096) timeout *= 2;
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
data = (struct ustor_data*)dev->user_data;
intf = data->intf;
ret = rt_usbh_storage_read10(intf, (rt_uint8_t*)buffer, pos, size, timeout);
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
if (ret != RT_EOK)
{
rt_kprintf("usb mass_storage read failed\n");
return 0;
}
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
return size;
}
/**
* This function will write some data to a device.
*
* @param dev the pointer of device driver structure
* @param pos the position of written
* @param buffer the data buffer to be written to device
* @param size the size of buffer
*
* @return the actually written size on successful, otherwise negative returned.
*/
2018-06-10 17:59:17 +08:00
static rt_size_t rt_udisk_write (rt_device_t dev, rt_off_t pos, const void* buffer,
2017-11-11 10:51:47 +08:00
rt_size_t size)
{
rt_err_t ret;
2017-12-14 03:14:44 +08:00
struct uhintf* intf;
2017-11-11 10:51:47 +08:00
struct ustor_data* data;
int timeout = USB_TIMEOUT_LONG;
2017-11-11 10:51:47 +08:00
/* check parameter */
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(buffer != RT_NULL);
if(size * SECTOR_SIZE > 4096) timeout *= 2;
2017-11-11 10:51:47 +08:00
data = (struct ustor_data*)dev->user_data;
intf = data->intf;
ret = rt_usbh_storage_write10(intf, (rt_uint8_t*)buffer, pos, size, timeout);
if (ret != RT_EOK)
{
rt_kprintf("usb mass_storage write %d sector failed\n", size);
return 0;
}
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
return size;
}
/**
* This function will execute SCSI_INQUIRY_CMD command to get inquiry data.
*
* @param intf the interface instance.
* @param buffer the data buffer to save inquiry data
2018-06-10 17:59:17 +08:00
*
2017-11-11 10:51:47 +08:00
* @return the error code, RT_EOK on successfully.
2018-06-10 17:59:17 +08:00
*/
static rt_err_t rt_udisk_control(rt_device_t dev, int cmd, void *args)
2017-11-11 10:51:47 +08:00
{
ustor_t stor;
struct ustor_data* data;
/* check parameter */
RT_ASSERT(dev != RT_NULL);
data = (struct ustor_data*)dev->user_data;
stor = (ustor_t)data->intf->user_data;
if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
{
struct rt_device_blk_geometry *geometry;
geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL) return -RT_ERROR;
geometry->bytes_per_sector = SECTOR_SIZE;
geometry->block_size = stor->capicity[1];
geometry->sector_count = stor->capicity[0];
}
2018-06-10 17:59:17 +08:00
return RT_EOK;
2017-11-11 10:51:47 +08:00
}
2018-06-10 17:59:17 +08:00
#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops udisk_device_ops =
{
rt_udisk_init,
RT_NULL,
RT_NULL,
rt_udisk_read,
rt_udisk_write,
rt_udisk_control
};
#endif
2017-11-11 10:51:47 +08:00
/**
* This function will run udisk driver when usb disk is detected.
*
* @param intf the usb interface instance.
2018-06-10 17:59:17 +08:00
*
2017-11-11 10:51:47 +08:00
* @return the error code, RT_EOK on successfully.
*/
2017-12-14 03:14:44 +08:00
rt_err_t rt_udisk_run(struct uhintf* intf)
2017-11-11 10:51:47 +08:00
{
int i = 0;
rt_err_t ret;
char dname[4];
2018-06-10 17:59:17 +08:00
char sname[8];
2017-11-11 10:51:47 +08:00
rt_uint8_t max_lun, *sector, sense[18], inquiry[36];
2018-06-10 17:59:17 +08:00
struct dfs_partition part[MAX_PARTITION_COUNT];
ustor_t stor;
2017-11-11 10:51:47 +08:00
/* check parameter */
RT_ASSERT(intf != RT_NULL);
/* set interface */
// ret = rt_usbh_set_interface(intf->device, intf->intf_desc->bInterfaceNumber);
// if(ret != RT_EOK)
// rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
/* reset mass storage class device */
ret = rt_usbh_storage_reset(intf);
2018-06-10 17:59:17 +08:00
if(ret != RT_EOK) return ret;
2017-11-11 10:51:47 +08:00
stor = (ustor_t)intf->user_data;
2018-06-10 17:59:17 +08:00
/* get max logic unit number */
2017-11-11 10:51:47 +08:00
ret = rt_usbh_storage_get_max_lun(intf, &max_lun);
2018-06-10 17:59:17 +08:00
if(ret != RT_EOK)
2017-11-11 10:51:47 +08:00
rt_usbh_clear_feature(intf->device, 0, USB_FEATURE_ENDPOINT_HALT);
/* reset pipe in endpoint */
if(stor->pipe_in->status == UPIPE_STATUS_STALL)
{
2018-06-10 17:59:17 +08:00
ret = rt_usbh_clear_feature(intf->device,
2017-11-11 10:51:47 +08:00
stor->pipe_in->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
2018-06-10 17:59:17 +08:00
if(ret != RT_EOK) return ret;
}
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* reset pipe out endpoint */
if(stor->pipe_out->status == UPIPE_STATUS_STALL)
{
2018-06-10 17:59:17 +08:00
ret = rt_usbh_clear_feature(intf->device,
stor->pipe_out->ep.bEndpointAddress, USB_FEATURE_ENDPOINT_HALT);
2018-06-10 17:59:17 +08:00
if(ret != RT_EOK) return ret;
}
2017-11-11 10:51:47 +08:00
while((ret = rt_usbh_storage_inquiry(intf, inquiry)) != RT_EOK)
2018-06-10 17:59:17 +08:00
{
2017-11-11 10:51:47 +08:00
if(ret == -RT_EIO) return ret;
2018-06-10 17:59:17 +08:00
rt_thread_delay(5);
2017-11-11 10:51:47 +08:00
if(i++ < 10) continue;
rt_kprintf("rt_usbh_storage_inquiry error\n");
2018-06-10 17:59:17 +08:00
return -RT_ERROR;
2017-11-11 10:51:47 +08:00
}
i = 0;
2017-11-11 10:51:47 +08:00
/* wait device ready */
while((ret = rt_usbh_storage_test_unit_ready(intf)) != RT_EOK)
{
if(ret == -RT_EIO) return ret;
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
ret = rt_usbh_storage_request_sense(intf, sense);
if(ret == -RT_EIO) return ret;
2018-06-10 17:59:17 +08:00
rt_thread_delay(10);
2017-11-11 10:51:47 +08:00
if(i++ < 10) continue;
rt_kprintf("rt_usbh_storage_test_unit_ready error\n");
2018-06-10 17:59:17 +08:00
return -RT_ERROR;
2017-11-11 10:51:47 +08:00
}
i = 0;
rt_memset(stor->capicity, 0, sizeof(stor->capicity));
/* get storage capacity */
2018-06-10 17:59:17 +08:00
while((ret = rt_usbh_storage_get_capacity(intf,
2017-11-11 10:51:47 +08:00
(rt_uint8_t*)stor->capicity)) != RT_EOK)
2018-06-10 17:59:17 +08:00
{
if(ret == -RT_EIO) return ret;
2017-11-11 10:51:47 +08:00
rt_thread_delay(50);
if(i++ < 10) continue;
stor->capicity[0] = 2880;
stor->capicity[1] = 0x200;
rt_kprintf("rt_usbh_storage_get_capacity error\n");
2018-06-10 17:59:17 +08:00
break;
2017-11-11 10:51:47 +08:00
}
stor->capicity[0] = uswap_32(stor->capicity[0]);
stor->capicity[1] = uswap_32(stor->capicity[1]);
stor->capicity[0] += 1;
2018-06-10 17:59:17 +08:00
RT_DEBUG_LOG(RT_DEBUG_USB, ("capicity %d, block size %d\n",
2017-11-11 10:51:47 +08:00
stor->capicity[0], stor->capicity[1]));
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* get the first sector to read partition table */
sector = (rt_uint8_t*) rt_malloc (SECTOR_SIZE);
if (sector == RT_NULL)
{
rt_kprintf("allocate partition sector buffer failed\n");
return -RT_ERROR;
}
rt_memset(sector, 0, SECTOR_SIZE);
RT_DEBUG_LOG(RT_DEBUG_USB, ("read partition table\n"));
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* get the partition table */
ret = rt_usbh_storage_read10(intf, sector, 0, 1, USB_TIMEOUT_LONG);
2017-11-11 10:51:47 +08:00
if(ret != RT_EOK)
{
rt_kprintf("read parition table error\n");
2018-06-10 17:59:17 +08:00
rt_free(sector);
return -RT_ERROR;
2017-11-11 10:51:47 +08:00
}
RT_DEBUG_LOG(RT_DEBUG_USB, ("finished reading partition\n"));
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
for(i=0; i<MAX_PARTITION_COUNT; i++)
2018-06-10 17:59:17 +08:00
{
2017-11-11 10:51:47 +08:00
/* get the first partition */
ret = dfs_filesystem_get_partition(&part[i], sector, i);
if (ret == RT_EOK)
2018-06-10 17:59:17 +08:00
{
struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
2017-11-11 10:51:47 +08:00
rt_memset(data, 0, sizeof(struct ustor_data));
2018-06-10 17:59:17 +08:00
data->intf = intf;
2017-11-11 10:51:47 +08:00
data->udisk_id = udisk_get_id();
rt_snprintf(dname, 6, "ud%d-%d", data->udisk_id, i);
rt_snprintf(sname, 8, "sem_ud%d", i);
2018-04-27 15:27:26 +08:00
data->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
2017-11-11 10:51:47 +08:00
/* register sdcard device */
2018-06-10 17:59:17 +08:00
stor->dev[i].type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
stor->dev[i].ops = &udisk_device_ops;
#else
stor->dev[i].init = rt_udisk_init;
stor->dev[i].read = rt_udisk_read;
stor->dev[i].write = rt_udisk_write;
2017-11-11 10:51:47 +08:00
stor->dev[i].control = rt_udisk_control;
2018-06-10 17:59:17 +08:00
#endif
2017-11-11 10:51:47 +08:00
stor->dev[i].user_data = (void*)data;
2018-06-10 17:59:17 +08:00
rt_device_register(&stor->dev[i], dname, RT_DEVICE_FLAG_RDWR |
2017-11-11 10:51:47 +08:00
RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
stor->dev_cnt++;
if (dfs_mount(stor->dev[i].parent.name, UDISK_MOUNTPOINT, "elm",
0, 0) == 0)
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount successfully\n", i));
}
else
{
RT_DEBUG_LOG(RT_DEBUG_USB, ("udisk part %d mount failed\n", i));
2018-06-10 17:59:17 +08:00
}
2017-11-11 10:51:47 +08:00
}
else
{
if(i == 0)
2018-06-10 17:59:17 +08:00
{
struct ustor_data* data = rt_malloc(sizeof(struct ustor_data));
rt_memset(data, 0, sizeof(struct ustor_data));
2017-11-11 10:51:47 +08:00
data->udisk_id = udisk_get_id();
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* there is no partition table */
data->part.offset = 0;
data->part.size = 0;
data->intf = intf;
data->part.lock = rt_sem_create("sem_ud", 1, RT_IPC_FLAG_FIFO);
rt_snprintf(dname, 7, "udisk%d", data->udisk_id);
/* register sdcard device */
2018-06-10 17:59:17 +08:00
stor->dev[0].type = RT_Device_Class_Block;
#ifdef RT_USING_DEVICE_OPS
stor->dev[i].ops = &udisk_device_ops;
#else
stor->dev[0].init = rt_udisk_init;
stor->dev[0].read = rt_udisk_read;
stor->dev[0].write = rt_udisk_write;
2017-11-11 10:51:47 +08:00
stor->dev[0].control = rt_udisk_control;
2018-06-10 17:59:17 +08:00
#endif
2017-11-11 10:51:47 +08:00
stor->dev[0].user_data = (void*)data;
rt_device_register(&stor->dev[0], dname,
2018-06-10 17:59:17 +08:00
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE
2017-11-11 10:51:47 +08:00
| RT_DEVICE_FLAG_STANDALONE);
stor->dev_cnt++;
2018-06-10 17:59:17 +08:00
if (dfs_mount(stor->dev[0].parent.name, UDISK_MOUNTPOINT,
2017-11-11 10:51:47 +08:00
"elm", 0, 0) == 0)
2018-06-10 17:59:17 +08:00
{
2017-11-11 10:51:47 +08:00
rt_kprintf("Mount FAT on Udisk successful.\n");
2018-06-10 17:59:17 +08:00
}
2017-11-11 10:51:47 +08:00
else
{
rt_kprintf("Mount FAT on Udisk failed.\n");
}
}
break;
}
}
rt_free(sector);
return RT_EOK;
}
/**
2018-06-10 17:59:17 +08:00
* This function will be invoked when usb disk plug out is detected and it would clean
2017-11-11 10:51:47 +08:00
* and release all udisk related resources.
*
* @param intf the usb interface instance.
2018-06-10 17:59:17 +08:00
*
2017-11-11 10:51:47 +08:00
* @return the error code, RT_EOK on successfully.
*/
2017-12-14 03:14:44 +08:00
rt_err_t rt_udisk_stop(struct uhintf* intf)
2017-11-11 10:51:47 +08:00
{
int i;
2018-06-10 17:59:17 +08:00
ustor_t stor;
2017-11-11 10:51:47 +08:00
struct ustor_data* data;
/* check parameter */
RT_ASSERT(intf != RT_NULL);
RT_ASSERT(intf->device != RT_NULL);
2018-06-10 17:59:17 +08:00
stor = (ustor_t)intf->user_data;
2017-11-11 10:51:47 +08:00
RT_ASSERT(stor != RT_NULL);
for(i=0; i<stor->dev_cnt; i++)
{
rt_device_t dev = &stor->dev[i];
data = (struct ustor_data*)dev->user_data;
2018-06-10 17:59:17 +08:00
2017-11-11 10:51:47 +08:00
/* unmount filesystem */
dfs_unmount(UDISK_MOUNTPOINT);
/* delete semaphore */
rt_sem_delete(data->part.lock);
udisk_free_id(data->udisk_id);
rt_free(data);
/* unregister device */
rt_device_unregister(&stor->dev[i]);
}
return RT_EOK;
}
#endif