/* perform 512 time the same read */
s32 USBStorage_Read_Stress(u32 sector, u32 numSectors, void *buffer)
{
    s32 retval;
    int i;
    if(__mounted != 1)
        return false;
    
    for(i=0;i<512;i++){
        retval = USBStorage_Read(&__usbfd, __lun, sector, numSectors, buffer);
        sector+=numSectors;
        if(retval == USBSTORAGE_ETIMEDOUT)
        {
            __mounted = 0;
            USBStorage_Close(&__usbfd);
        }
        if(retval < 0)
            return false;
    }
    return true;
    
}
// temp function before libfat is available */
s32 USBStorage_Try_Device(struct ehci_device *fd)
{
    int maxLun,j,retval;
    if(USBStorage_Open(&__usbfd, fd) < 0)
        return -EINVAL;
    
    maxLun = USBStorage_GetMaxLUN(&__usbfd);
    if(maxLun == USBSTORAGE_ETIMEDOUT)
        return -EINVAL;
    for(j = 0; j < maxLun; j++)
    {
        retval = USBStorage_MountLUN(&__usbfd, j);
        if(retval == USBSTORAGE_ETIMEDOUT)
        {
            USBStorage_Reset(&__usbfd);
            USBStorage_Close(&__usbfd);
            break;
        }
        
        if(retval < 0)
            continue;
        
        (*context.device)->GetDeviceVendor(context.device, &__vid);
        (*context.device)->GetDeviceProduct(context.device, &__pid);
        __mounted = 1;
        __lun = j;
        return 0;
    }
    return -EINVAL;
}
示例#2
0
static bool __usbstorage_IsInserted(void)
{
	usb_device_entry *buffer;
	u8 device_count;
	u8 i, j;
	u16 vid, pid;
	s32 maxLun;
	s32 retval;

#ifdef DEBUG_USB		
	usb_log("__usbstorage_IsInserted\n");
	if(__mounted)
		usb_log("device previously mounted.\n");
#endif

	if(__mounted)
		return true;

	if(!__inited)
		return false;

	buffer = (usb_device_entry*)__lwp_heap_allocate(&__heap, DEVLIST_MAXSIZE * sizeof(usb_device_entry));
	if (!buffer)
		return false;

	memset(buffer, 0, DEVLIST_MAXSIZE * sizeof(usb_device_entry));

	if (USB_GetDeviceList(buffer, DEVLIST_MAXSIZE, USB_CLASS_MASS_STORAGE, &device_count) < 0)
	{
		if (__vid != 0 || __pid != 0)
			USBStorage_Close(&__usbfd);

		__lwp_heap_free(&__heap, buffer);
		usb_log("Error in USB_GetDeviceList\n");
		return false;
	}

	usleep(100);

	if (__vid != 0 || __pid != 0) {
		for(i = 0; i < device_count; i++) {
			vid = buffer[i].vid;
			pid = buffer[i].pid;
			if(vid != 0 || pid != 0) {
				if((vid == __vid) && (pid == __pid)) {
					__mounted = true;
					__lwp_heap_free(&__heap,buffer);
					usleep(50); // I don't know why I have to wait but it's needed
					return true;
				}
			}
		}
		USBStorage_Close(&__usbfd);  // device changed or unplugged, return false the first time to notify to the client that he must unmount devices
		__lwp_heap_free(&__heap,buffer);
		usb_log("USB_GetDeviceList. device_count: %i\n",device_count);
		return false;
	}
	usb_log("USB_GetDeviceList. device_count: %i\n",device_count);
	for (i = 0; i < device_count; i++) {
		vid = buffer[i].vid;
		pid = buffer[i].pid;
		if (vid == 0 || pid == 0)
			continue;

		if (USBStorage_Open(&__usbfd, buffer[i].device_id, vid, pid) < 0)
		{
			usb_log("Error USBStorage_Open (%i,%i)\n",vid, pid);
			continue;
		}

		maxLun = USBStorage_GetMaxLUN(&__usbfd);
		usb_log("GetMaxLUN: %i\n",maxLun);
		for (j = 0; j < maxLun; j++) {
			retval = USBStorage_MountLUN(&__usbfd, j);
			if(retval<0)
				usb_log("Error USBStorage_MountLUN(%i): %i\n",j,retval);
			else
				usb_log("USBStorage_MountLUN(%i) Ok\n",j);
			//if (retval == USBSTORAGE_ETIMEDOUT)
			//	break;

			if (retval < 0)
			{
				__usbstorage_reset(&__usbfd);
				continue;
			}

			__mounted = true;
			__lun = j;
			__vid = vid;
			__pid = pid;
			usb_last_used = gettime()-secs_to_ticks(100);
			usleep(100);
#ifdef DEBUG_USB
			{
				u8 bf[2048];
				if(USBStorage_Read(&__usbfd, __lun, 0, 1, bf)<0)
				{
					usb_log("Error reading sector 0\n");
					USBStorage_Close(&__usbfd);
					return false;
				}
				else
					usb_log("Read sector 0 ok\n");
			}
#endif
			break;
		}

		if (__mounted)
			break;

		USBStorage_Close(&__usbfd);
	}
	__lwp_heap_free(&__heap, buffer);
	return __mounted;
}