/* 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;
}
Exemple #2
0
s32 USBStorage_Try_Device(ehci_device_data * device_data) {
	struct ehci_hcd * ehci; struct ehci_device *fd;
	int maxLun, j, retval;
	int test_max_lun = 1;
	
	ehci = device_data->__ehci;
	fd = device_data->__dev;

	try_status = -120;
	if (USBStorage_Open(device_data) < 0)
		return -EINVAL;

	/*
	   maxLun = USBStorage_GetMaxLUN(&__usbfd);
	   if(maxLun == USBSTORAGE_ETIMEDOUT)
			{
			__mounted = 0;
			USBStorage_Close(&__usbfd);
			return -EINVAL;
			}
	 */

	maxLun = 1;
	device_data->__usbfd.max_lun = 1;

	j = 0;
	//for(j = 0; j < maxLun; j++)
	while (1) {

#ifdef MEM_PRINT
		s_printf("USBStorage_MountLUN %i#\n", j);
#endif
		retval = USBStorage_MountLUN(ehci, &device_data->__usbfd, j);
#ifdef MEM_PRINT
		s_printf("USBStorage_MountLUN: ret %i\n", retval);
#endif


		if (retval == USBSTORAGE_ETIMEDOUT /*&& test_max_lun==0*/) {
			//USBStorage_Reset(&__usbfd);
			try_status = -121;
			device_data->__mounted = 0;
			USBStorage_Close(&device_data->__usbfd);
			return -EINVAL;
			//  break;
		}

		if (retval < 0) {
			if (test_max_lun) {
				device_data->__usbfd.max_lun = 0;
				retval = __USB_CtrlMsgTimeout(ehci, &device_data->__usbfd,
						(USB_CTRLTYPE_DIR_DEVICE2HOST | USB_CTRLTYPE_TYPE_CLASS | USB_CTRLTYPE_REC_INTERFACE),
						USBSTORAGE_GET_MAX_LUN, 0, device_data->__usbfd.interface, 1, &device_data->__usbfd.max_lun);
				if (retval < 0)
					device_data->__usbfd.max_lun = 1;
				else device_data->__usbfd.max_lun++;
				maxLun =device_data-> __usbfd.max_lun;

#ifdef MEM_PRINT
				s_printf("USBSTORAGE_GET_MAX_LUN ret %i maxlun %i\n", retval, maxLun);
#endif
				test_max_lun = 0;
			} else j++;

			if (j >= maxLun) break;
			continue;
		}

		device_data->__vid = fd->desc.idVendor;
		device_data->__pid = fd->desc.idProduct;
		device_data->__mounted = 1;
		device_data->__lun = j;
		usb_timeout = 1000 * 1000;
		try_status = 0;
		return 0;
	}
	try_status = -122;
	device_data->__mounted = 0;
	USBStorage_Close(&device_data->__usbfd);

#ifdef MEM_PRINT
	s_printf("USBStorage_MountLUN fail!!!\n");
#endif

	return -EINVAL;
}
Exemple #3
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;
}