make UDisk work well

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2448 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
sc943313837@gmail.com 2012-11-25 16:00:28 +00:00
parent 60c27fc4b5
commit 9789907ca1
1 changed files with 106 additions and 81 deletions

View File

@ -10,6 +10,7 @@
* Change Logs: * Change Logs:
* Date Author Notes * Date Author Notes
* 2012-10-01 Yi Qiu first version * 2012-10-01 Yi Qiu first version
* 2012-11-25 Heyuanjie87 reduce the memory consumption
*/ */
#include <rtthread.h> #include <rtthread.h>
@ -22,6 +23,7 @@
#define STATUS_CBW 0x00 #define STATUS_CBW 0x00
#define STATUS_CSW 0x01 #define STATUS_CSW 0x01
#define STATUS_RECEIVE 0x02 #define STATUS_RECEIVE 0x02
#define STATUS_SEND 0x03
static uclass_t mstorage; static uclass_t mstorage;
static uep_t ep_in, ep_out; static uep_t ep_in, ep_out;
@ -30,6 +32,8 @@ static rt_uint8_t *write_ptr;
static int status = STATUS_CBW; static int status = STATUS_CBW;
static struct ustorage_csw csw; static struct ustorage_csw csw;
static rt_device_t disk; static rt_device_t disk;
static rt_uint32_t _block;
static rt_uint32_t _count, _size;
static struct rt_device_blk_geometry geometry; static struct rt_device_blk_geometry geometry;
static struct udevice_descriptor dev_desc = static struct udevice_descriptor dev_desc =
@ -222,28 +226,32 @@ static rt_err_t _read_capacity(udevice_t device)
*/ */
static rt_err_t _read_10(udevice_t device, ustorage_cbw_t cbw) static rt_err_t _read_10(udevice_t device, ustorage_cbw_t cbw)
{ {
rt_uint32_t block;
rt_uint32_t count;
RT_ASSERT(device != RT_NULL); RT_ASSERT(device != RT_NULL);
RT_ASSERT(cbw != RT_NULL); RT_ASSERT(cbw != RT_NULL);
block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 | _block = cbw->cb[2]<<24 | cbw->cb[3]<<16 | cbw->cb[4]<<8 |
cbw->cb[5]<<0 ; cbw->cb[5]<<0 ;
count = cbw->cb[7]<<8 | cbw->cb[8]<<0 ; _count = cbw->cb[7]<<8 | cbw->cb[8]<<0 ;
RT_ASSERT(count < geometry.sector_count); RT_ASSERT(_count < geometry.sector_count);
rt_device_read(disk, block, buffer, count); rt_device_read(disk, _block, buffer, 1);
dcd_ep_write(device->dcd, ep_in, buffer, count * geometry.bytes_per_sector); dcd_ep_write(device->dcd, ep_in, buffer, geometry.bytes_per_sector);
_count --;
if (_count)
{
_block ++;
status = STATUS_SEND;
}
else
{
status = STATUS_CSW;
}
return RT_EOK; return RT_EOK;
} }
static rt_uint32_t _block;
static rt_uint32_t _count, _size;
/** /**
* This function will handle write_10 request. * This function will handle write_10 request.
* *
@ -302,6 +310,21 @@ static rt_err_t _ep_in_handler(udevice_t device, rt_size_t size)
status = STATUS_CBW; status = STATUS_CBW;
dcd_ep_read(device->dcd, ep_out, ep_out->buffer, SIZEOF_CBW); dcd_ep_read(device->dcd, ep_out, ep_out->buffer, SIZEOF_CBW);
} }
if(status == STATUS_SEND)
{
rt_device_read(disk, _block, buffer, 1);
dcd_ep_write(device->dcd, ep_in, buffer, geometry.bytes_per_sector);
_count --;
if (_count)
{
_block ++;
status = STATUS_SEND;
}
else
{
status = STATUS_CSW;
}
}
return RT_EOK; return RT_EOK;
} }
@ -378,7 +401,6 @@ static rt_err_t _ep_out_handler(udevice_t device, rt_size_t size)
break; break;
case SCSI_READ_10: case SCSI_READ_10:
_read_10(device, cbw); _read_10(device, cbw);
status = STATUS_CSW;
break; break;
case SCSI_WRITE_10: case SCSI_WRITE_10:
_write_10(device, cbw); _write_10(device, cbw);
@ -464,11 +486,14 @@ static rt_err_t _class_run(udevice_t device)
RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage run\n")); RT_DEBUG_LOG(RT_DEBUG_USB, ("mass storage run\n"));
disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME); disk = rt_device_find(RT_USB_MSTORAGE_DISK_NAME);
RT_ASSERT(disk != RT_NULL); if(disk == RT_NULL)
return RT_ERROR;
if(rt_device_control(disk, RT_DEVICE_CTRL_BLK_GETGEOME, (void*)&geometry) != RT_EOK)
return RT_ERROR;
buffer = (rt_uint8_t*)rt_malloc(RT_USB_MSTORAGE_BUFFER_SIZE); buffer = (rt_uint8_t*)rt_malloc(geometry.bytes_per_sector);
if(buffer == RT_NULL)
rt_device_control(disk, RT_DEVICE_CTRL_BLK_GETGEOME, (void*)&geometry); return RT_ERROR;
dcd_ep_read(device->dcd, ep_out, ep_out->buffer, SIZEOF_CBW); dcd_ep_read(device->dcd, ep_out, ep_out->buffer, SIZEOF_CBW);
return RT_EOK; return RT_EOK;