-fixed some usb hdd problems for people with 3tb+ devices

introduced with r598
This commit is contained in:
fix94.1 2012-07-20 10:02:53 +00:00
parent c52009ea57
commit ef87b82320

View File

@ -57,11 +57,10 @@
#define USB_IOCTL_WBFS_SET_DEVICE (WBFS_BASE+0x50)
#define USB_IOCTL_WBFS_SET_FRAGLIST (WBFS_BASE+0x51)
#define isMEM2Buffer(p) (((u32) p & 0x10000000) != 0)
#define MAX_SECTOR_SIZE 4096
#define MAX_BUFFER_SECTORS 128
#define UMS_HEAPSIZE 2*1024
#define USB_MEM2_SIZE MAX_BUFFER_SECTORS*MAX_SECTOR_SIZE
/* Variables */
static char fs[] ATTRIBUTE_ALIGN(32) = "/dev/usb2";
@ -74,6 +73,14 @@ static u32 usb2_port = -1; //current USB port
bool hddInUse[2] = { false, false };
u32 hdd_sector_size[2] = { 512, 512 };
extern void* SYS_AllocArena2MemLo(u32 size,u32 align);
inline s32 __USBStorage_isMEM2Buffer(const void *buffer)
{
u32 high_addr = ((u32)buffer) >> 24;
return(high_addr == 0x90) || (high_addr == 0xD0);
}
s32 USBStorage2_Init(u32 port)
{
if(hddInUse[port])
@ -85,6 +92,9 @@ s32 USBStorage2_Init(u32 port)
hid = iosCreateHeap(UMS_HEAPSIZE);
if (hid < 0) return IPC_ENOMEM;
}
/* allocate buf2 */
if(mem2_ptr == NULL)
mem2_ptr = SYS_AllocArena2MemLo(USB_MEM2_SIZE, 32);
/* Open USB device */
if (fd < 0) fd = IOS_Open(fs, 0);
@ -161,14 +171,21 @@ s32 USBStorage2_GetCapacity(u32 port, u32 *_sector_size)
s32 USBStorage2_ReadSectors(u32 port, u32 sector, u32 numSectors, void *buffer)
{
bool isMEM2Buffer = __USBStorage_isMEM2Buffer(buffer);
u8 *buf = (u8 *)buffer;
s32 ret = -1;
/* Device not opened */
if (fd < 0) return fd;
if(fd < 0)
return fd;
if (!mem2_ptr)
mem2_ptr = (u8 *) MEM2_alloc(MAX_SECTOR_SIZE * MAX_BUFFER_SECTORS);
/* MEM1 buffer */
if(!isMEM2Buffer)
{
buf = mem2_ptr;
if(!buf)
return IPC_ENOMEM;
}
USBStorage2_SetPort(port);
@ -179,26 +196,20 @@ s32 USBStorage2_ReadSectors(u32 port, u32 sector, u32 numSectors, void *buffer)
read_secs = numSectors > MAX_BUFFER_SECTORS ? MAX_BUFFER_SECTORS : numSectors;
read_size = read_secs * hdd_sector_size[port];
// Do not read more than MAX_BUFFER_SECTORS sectors at once and create a mem overflow!
if (!isMEM2Buffer(buffer))
{
ret = IOS_IoctlvFormat(hid, fd, USB_IOCTL_UMS_READ_SECTORS, "ii:d", sector, read_secs, mem2_ptr, read_size);
if(ret < 0)
return ret;
memcpy(buf, mem2_ptr, read_size);
}
else
{
/* Read data */
ret = IOS_IoctlvFormat(hid, fd, USB_IOCTL_UMS_READ_SECTORS, "ii:d", sector, read_secs, buf, read_size);
if(ret < 0)
return ret;
/* Copy data */
if(!isMEM2Buffer)
{
memcpy(buffer, buf, read_size);
buffer += read_size;
}
else
buf += read_size;
sector += read_secs;
numSectors -= read_secs;
buf += read_size;
}
return ret;
@ -206,15 +217,13 @@ s32 USBStorage2_ReadSectors(u32 port, u32 sector, u32 numSectors, void *buffer)
s32 USBStorage2_WriteSectors(u32 port, u32 sector, u32 numSectors, const void *buffer)
{
bool isMEM2Buffer = __USBStorage_isMEM2Buffer(buffer);
u8 *buf = (u8 *)buffer;
s32 ret = -1;
/* Device not opened */
if (fd < 0) return fd;
/* Device not opened */
if (!mem2_ptr)
mem2_ptr = (u8 *) MEM2_alloc(MAX_SECTOR_SIZE * MAX_BUFFER_SECTORS);
if(fd < 0)
return fd;
USBStorage2_SetPort(port);
@ -226,7 +235,7 @@ s32 USBStorage2_WriteSectors(u32 port, u32 sector, u32 numSectors, const void *b
write_size = write_secs * hdd_sector_size[port];
/* MEM1 buffer */
if (!isMEM2Buffer(buffer))
if(!isMEM2Buffer)
{
// Do not read more than MAX_BUFFER_SECTORS sectors at once and create a mem overflow!
memcpy(mem2_ptr, buf, write_size);