Beispiel #1
0
s32 USBStorage_Reset(struct ehci_hcd * ehci, usbstorage_handle *dev) {
	s32 retval;

	retval = __usbstorage_reset(ehci, dev, 0);

	return retval;
}
s32 USBStorage_Reset(usbstorage_handle *dev)
{
	s32 retval;
    
	retval = __usbstorage_reset(dev);
    
	return retval;
}
Beispiel #3
0
s32 USBStorageOGC_Reset(usbstorage_handle *dev)
{
	s32 retval;

	LWP_MutexLock(dev->lock);
	retval = __usbstorage_reset(dev);
	LWP_MutexUnlock(dev->lock);

	return retval;
}
Beispiel #4
0
static bool __usbstorage_IsInserted(void)
{
	usb_device_entry *buffer;
	u8 device_count;
	u8 i, j;
	u16 vid, pid;
	s32 maxLun;
	s32 retval;
	u32 sectorsize, numSectors;

	if(__mounted)
	{
		// device is not a USB DVD drive - always return true
		if (__usbfd.sector_size[__lun] != 2048)
			return true;

		// check if DVD is inserted
		if (USBStorageOGC_ReadCapacity(&__usbfd, __lun, &sectorsize, &numSectors) < 0)
			return false;
		else
			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_OGC_GetDeviceList(buffer, DEVLIST_MAXSIZE, USB_CLASS_MASS_STORAGE, &device_count) < 0)
	{
		if (__vid != 0 || __pid != 0)
			USBStorageOGC_Close(&__usbfd);

		__lwp_heap_free(&__heap, buffer);
		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;
				}
			}
		}
		USBStorageOGC_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);
		return false;
	}
	for (i = 0; i < device_count; i++) {
		vid = buffer[i].vid;
		pid = buffer[i].pid;
		if (vid == 0 || pid == 0)
			continue;
			
		if (vid == 0x0b95 && pid == 0x7720) // USB LAN
			continue;

		if (USBStorageOGC_Open(&__usbfd, buffer[i].device_id, vid, pid) < 0)
			continue;

		maxLun = USBStorageOGC_GetMaxLUN(&__usbfd);
		for (j = 0; j < maxLun; j++) {
			retval = USBStorageOGC_MountLUN(&__usbfd, j);

			if (retval == INVALID_LUN)
				continue;

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

			__mounted = true;
			__lun = j;
			__vid = vid;
			__pid = pid;
			usb_last_used = gettime()-secs_to_ticks(100);
			usleep(10000);
			break;
		}

		if (__mounted)
			break;

		USBStorageOGC_Close(&__usbfd);
	}
	__lwp_heap_free(&__heap, buffer);

	return __mounted;
}
Beispiel #5
0
static s32 __cycle(usbstorage_handle *dev, u8 lun, u8 *buffer, u32 len, u8 *cb, u8 cbLen, u8 write, u8 *_status, u32 *_dataResidue)
{
	s32 retval = USBSTORAGE_OK;

	u8 status=0;
	u32 dataResidue = 0;
	u16 max_size;
	u8 ep = write ? dev->ep_out : dev->ep_in;
	s8 retries = USBSTORAGE_CYCLE_RETRIES + 1;
	
	if(usb2_mode)
		max_size=MAX_TRANSFER_SIZE_V5;
	else
		max_size=MAX_TRANSFER_SIZE_V0;

	LWP_MutexLock(dev->lock);
	do
	{
		u8 *_buffer = buffer;
		u32 _len = len;
		retries--;

		if(retval == USBSTORAGE_ETIMEDOUT)
			break;

		retval = __send_cbw(dev, lun, len, (write ? CBW_OUT:CBW_IN), cb, cbLen);

		while(_len > 0 && retval >= 0)
		{
			u32 thisLen = _len > max_size ? max_size : _len;

			if ((u32)_buffer&0x1F || !((u32)_buffer&0x10000000)) {
				if (write) memcpy(dev->buffer, _buffer, thisLen);
				retval = __USB_BlkMsgTimeout(dev, ep, thisLen, dev->buffer, usbtimeout);
				if (!write && retval > 0)
					memcpy(_buffer, dev->buffer, retval);
			} else
				retval = __USB_BlkMsgTimeout(dev, ep, thisLen, _buffer, usbtimeout);

			if (retval == thisLen) {
				_len -= retval;
				_buffer += retval;
			}
			else if (retval != USBSTORAGE_ETIMEDOUT)
				retval = USBSTORAGE_EDATARESIDUE;
		}

		if (retval >= 0)
			retval = __read_csw(dev, &status, &dataResidue, usbtimeout);

		if (retval < 0) {
			if (__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
				retval = USBSTORAGE_ETIMEDOUT;
		}
	} while (retval < 0 && retries > 0);

	LWP_MutexUnlock(dev->lock);

	if(_status != NULL)
		*_status = status;
	if(_dataResidue != NULL)
		*_dataResidue = dataResidue;

	return retval;
}
Beispiel #6
0
static s32 __cycle(struct ehci_hcd * ehci, usbstorage_handle *dev, u8 lun, u8 *buffer, u32 len, u8 *cb, u8 cbLen, u8 write, u8 *_status, u32 *_dataResidue) {
	s32 retval = USBSTORAGE_OK;

	u8 status = 0;
	u32 dataResidue = 0;
	u32 thisLen;

	u8 *buffer2 = buffer;
	u32 len2 = len;

	s8 retries = USBSTORAGE_CYCLE_RETRIES + 1;

	do {

		if (retval == -ENODEV) {
			unplug_device = 1;
			return -ENODEV;
		}



		if (retval < 0)
			retval = __usbstorage_reset(ehci, dev, retries < USBSTORAGE_CYCLE_RETRIES);

		retries--;
		if (retval < 0) continue; // nuevo

		buffer = buffer2;
		len = len2;

		if (write) {

			retval = __send_cbw(ehci, dev, lun, len, CBW_OUT, cb, cbLen);
			if (retval < 0)
				continue; //reset+retry
			while (len > 0) {
				thisLen = len;
				retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_out, thisLen, buffer);

				if (retval == -ENODEV || retval == -ETIMEDOUT) break;

				if (retval < 0)
					continue; //reset+retry

				if (retval != thisLen && len > 0) {
					retval = USBSTORAGE_EDATARESIDUE;
					continue; //reset+retry
				}
				len -= retval;
				buffer += retval;
			}

			if (retval < 0)
				continue;
		} else {
			retval = __send_cbw(ehci, dev, lun, len, CBW_IN, cb, cbLen);

			if (retval < 0)
				continue; //reset+retry
			while (len > 0) {
				thisLen = len;

				retval = __USB_BlkMsgTimeout(ehci, dev, dev->ep_in, thisLen, buffer);

				if (retval == -ENODEV || retval == -ETIMEDOUT) break;

				if (retval < 0)
					continue; //reset+retry
				//hexdump(buffer,retval);

				len -= retval;
				buffer += retval;

				if (retval != thisLen) {
					retval = -1;
					continue; //reset+retry
				}
			}

			if (retval < 0)
				continue;
		}

		retval = __read_csw(ehci, dev, &status, &dataResidue);

		if (retval < 0)
			continue;

		retval = USBSTORAGE_OK;
	} while (retval < 0 && retries > 0);

	// force unplug
	if (retval < 0 && retries <= 0 && handshake_mode == 0) {
		unplug_device = 1;
		return -ENODEV;
	}

	if (retval < 0 && retval != USBSTORAGE_ETIMEDOUT) {
		if (__usbstorage_reset(ehci, dev, 0) == USBSTORAGE_ETIMEDOUT)
			retval = USBSTORAGE_ETIMEDOUT;
	}


	if (_status != NULL)
		*_status = status;
	if (_dataResidue != NULL)
		*_dataResidue = dataResidue;

	return retval;
}
Beispiel #7
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;
}
static s32 __cycle(usbstorage_handle *dev, u8 lun, u8 *buffer, u32 len, u8 *cb, u8 cbLen, u8 write, u8 *_status, u32 *_dataResidue)
{
	s32 retval = USBSTORAGE_OK;
    
	u8 status = 0;
	u32 dataResidue = 0;
	u32 thisLen;
    
	s8 retries = USBSTORAGE_CYCLE_RETRIES + 1;
    
	do
	{
		retries--;
		if(retval == USBSTORAGE_ETIMEDOUT)
			break;
        
		if(write)
		{
			retval = __send_cbw(dev, lun, len, CBW_OUT, cb, cbLen);
			if(retval == USBSTORAGE_ETIMEDOUT)
				break;
			if(retval < 0)
			{
				if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
					retval = USBSTORAGE_ETIMEDOUT;
				continue;
			}
			while(len > 0)
			{
                thisLen=len;
				retval = __USB_BlkMsgTimeout(dev, dev->ep_out, thisLen, buffer);
                
				if(retval == USBSTORAGE_ETIMEDOUT)
					break;
                
				if(retval < 0)
				{
					retval = USBSTORAGE_EDATARESIDUE;
					break;
				}
                
                
				if(retval != thisLen && len > 0)
				{
					retval = USBSTORAGE_EDATARESIDUE;
					break;
				}
				len -= retval;
				buffer += retval;
			}
            
			if(retval < 0)
			{
				if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
					retval = USBSTORAGE_ETIMEDOUT;
				continue;
			}
		}
		else
		{
			retval = __send_cbw(dev, lun, len, CBW_IN, cb, cbLen);
            
			if(retval == USBSTORAGE_ETIMEDOUT)
				break;
            
			if(retval < 0)
			{
				if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
					retval = USBSTORAGE_ETIMEDOUT;
				continue;
			}
			while(len > 0)
			{
                thisLen=len;
				retval = __USB_BlkMsgTimeout(dev, dev->ep_in, thisLen, buffer);
                //print_hex_dump_bytes("usbs in:",DUMP_PREFIX_OFFSET,dev->buffer,36);
				if(retval < 0)
					break;
                
				len -= retval;
				buffer += retval;
                
				if(retval != thisLen)
					break;
			}
            
			if(retval < 0)
			{
				if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
					retval = USBSTORAGE_ETIMEDOUT;
				continue;
			}
		}
        
		retval = __read_csw(dev, &status, &dataResidue);
        
		if(retval == USBSTORAGE_ETIMEDOUT)
			break;
        
		if(retval < 0)
		{
			if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
				retval = USBSTORAGE_ETIMEDOUT;
			continue;
		}
        
		retval = USBSTORAGE_OK;
	} while(retval < 0 && retries > 0);
    
	if(retval < 0 && retval != USBSTORAGE_ETIMEDOUT)
	{
		if(__usbstorage_reset(dev) == USBSTORAGE_ETIMEDOUT)
			retval = USBSTORAGE_ETIMEDOUT;
	}
    
    
	if(_status != NULL)
		*_status = status;
	if(_dataResidue != NULL)
		*_dataResidue = dataResidue;
    
	return retval;
}