Exemple #1
0
void *USB_Alloc(int size)
{
  void * ret = 0;
  ret= os_heap_alloc_aligned(heap, size, 32);
 // ret= os_heap_alloc(heap, size);
  if(ret==0)
	{
    os_puts("USB Alloc: not enough memory!\n");
     while(1) {swi_mload_led_on();ehci_msleep(200);swi_mload_led_off();ehci_msleep(200);}
	}
  return ret;
}
Exemple #2
0
void USBStorage_Umount(void) {
	if (!ums_init_done) return;
	
	ehci_device_data * device_data = find_ehci_data(ehci);

	if (__mounted && !unplug_device) {
		if (__usbstorage_start_stop(&__usbfd, __lun, 0x0) == 0) // stop
			ehci_msleep(1000);
	}

	USBStorage_Close(&__usbfd);
	__lun = 16;
	__mounted = 0;
	ums_init_done = 0;
	unplug_device = 0;
	//unregister_bdev(__bdev);
}
Exemple #3
0
int unplug_procedure(int device) {
	int retval = 1;
	if (unplug_device != 0) {

		if (_ehci_data[device].__usbfd.usb_fd)
			if (ehci_reset_port2(_ehci_data[device].__ehci, /*__usbfd.usb_fd->port*/0) >= 0) {
				if (_ehci_data[device].__usbfd.buffer != NULL)
					USB_Free(_ehci_data[device].__usbfd.buffer);
				_ehci_data[device].__usbfd.buffer = NULL;

				if (ehci_reset_port(_ehci_data[device].__ehci, 0) >= 0) {
					handshake_mode = 1;
					if (USBStorage_Try_Device(&_ehci_data[device]) == 0) {
						retval = 0;
						unplug_device = 0;
					}
					handshake_mode = 0;
				}
			}
		ehci_msleep(100);
	}

	return retval;
}
Exemple #4
0
static s32 __usbstorage_reset(struct ehci_hcd * ehci, usbstorage_handle *dev, int hard_reset) {
	s32 retval;
	u32 old_usb_timeout = usb_timeout;
	usb_timeout = 1000 * 1000;
	//int retry = hard_reset;
	int retry = 0; //first try soft reset
retry:
	if (retry >= 1) {
		u8 conf;
		debug_printf("reset device..\n");
		retval = ehci_reset_device(ehci, dev->usb_fd);
		ehci_msleep(10);
		if (retval == -ENODEV) return retval;

		if (retval < 0 && retval != -7004)
			goto end;
		// reset configuration
		if (USB_GetConfiguration(ehci, dev->usb_fd, &conf) < 0)
			goto end;
		if (/*conf != dev->configuration &&*/ USB_SetConfiguration(ehci, dev->usb_fd, dev->configuration) < 0)
			goto end;
		if (dev->altInterface != 0 && USB_SetAlternativeInterface(ehci, dev->usb_fd, dev->interface, dev->altInterface) < 0)
			goto end;
		
		
		
		// find the device with the same ehci ptr
		ehci_device_data * _device_data = find_ehci_data(ehci);
		
		if (_device_data->__lun != 16) {
			if (USBStorage_MountLUN(ehci, &_device_data->__usbfd, _device_data->__lun) < 0)
				goto end;
		}
	}
	debug_printf("usbstorage reset..\n");
	retval = __USB_CtrlMsgTimeout(ehci, dev, (USB_CTRLTYPE_DIR_HOST2DEVICE | USB_CTRLTYPE_TYPE_CLASS | USB_CTRLTYPE_REC_INTERFACE), USBSTORAGE_RESET, 0, dev->interface, 0, NULL);

#ifdef MEM_PRINT
	s_printf("usbstorage reset: Reset ret %i\n", retval);

#endif

	/* FIXME?: some devices return -7004 here which definitely violates the usb ms protocol but they still seem to be working... */
	if (retval < 0 && retval != -7004)
		goto end;


	/* gives device enough time to process the reset */
	ehci_msleep(10);


	debug_printf("clear halt on bulk ep..\n");
	retval = USB_ClearHalt(ehci, dev->usb_fd, dev->ep_in);

#ifdef MEM_PRINT
	s_printf("usbstorage reset: clearhalt in ret %i\n", retval);

#endif
	if (retval < 0)
		goto end;
	ehci_msleep(10);
	retval = USB_ClearHalt(ehci, dev->usb_fd, dev->ep_out);

#ifdef MEM_PRINT
	s_printf("usbstorage reset: clearhalt in ret %i\n", retval);

#endif
	if (retval < 0)
		goto end;
	ehci_msleep(50);
	usb_timeout = old_usb_timeout;
	return retval;

end:
	if (retval == -ENODEV) return retval;
#ifdef HOMEBREW
	if (disable_hardreset) return retval;
	if (retry < 1) { //only 1 hard reset
		ehci_msleep(100);
		debug_printf("retry with hard reset..\n");
		retry++;
		goto retry;
	}
#else
	if (retry < 5) {
		ehci_msleep(100);
		debug_printf("retry with hard reset..\n");

		retry++;
		goto retry;
	}
#endif
	usb_timeout = old_usb_timeout;
	return retval;
}
Exemple #5
0
s32 USBStorage_Init(void) {
	int i, j, retries = 1;
	//	debug_printf("usbstorage init %d\n", ums_init_done);
	if (!ums_init_done)
	{
		_ehci_device_count = 0;
		init_ehci_device_struct();
	}
	struct ehci_hcd *ehci = &ehci_hcds[0];

	try_status = -1;

	for (j = 0; j < EHCI_HCD_COUNT; j++) {

		for (i = 0; i < ehci->num_port; i++) {
			struct ehci_device *dev = &ehci->devices[i];

retry:
			dev->port = i;
			if (dev->id != 0 && dev->busy == 0) {
				handshake_mode = 1;
				if (ehci_reset_port(ehci, i) >= 0) {

					//ehci_device_data * device_data = find_ehci_data(ehci);
					
					ehci_device_data * device_data = &_ehci_data[_ehci_device_count];
					dev->busy = 1;
					device_data->__ehci = ehci;
					device_data->__dev = dev;					

					if (USBStorage_Try_Device(device_data) == 0) {
						printf("EHCI bus %d device %d: vendor %04X product %04X : Mass-Storage Device\n", j, dev->id, device_data->__vid, device_data->__pid);

						first_access = TRUE;
						handshake_mode = 0;
						ums_init_done = 1;
						unplug_device = 0;
						//__bdev=register_bdev(NULL, &usb2mass_ops, "uda");
						//register_disc_interface(&usb2mass_ops);
						device_data->__ready = 1;
						if (mount_usb_device)						
							mount_usb_device(_ehci_device_count);						
						_ehci_device_count++;
#ifdef MEM_PRINT
						s_printf("USBStorage_Init() Ok\n");

#endif

						//return 0;
					}
				}
			} else if (dev->busy == 0) {
				u32 status;
				handshake_mode = 1;
				status = ehci_readl(&ehci->regs->port_status[i]);

#ifdef MEM_PRINT
				s_printf("USBStorage_Init() status %x\n", status);

#endif

				if (status & 1) {
					if (ehci_reset_port2(ehci, i) < 0) {
						ehci_msleep(100);
						ehci_reset_port(ehci, i);
					}
					if (retries) {
						retries--;
						goto retry;
					}
				}
			}
		}

		ehci++;
	}

	return try_status;
}