int _ss_open(struct ss_device *dev) { /* always try to close the device first */ _ss_close(dev); usb_device_entry dev_entry[SS_MAX_DEV]; unsigned char dev_count = 0; if (USB_GetDeviceList(dev_entry, SS_MAX_DEV, USB_CLASS_HID, &dev_count) < 0) { return -2; } int i; for (i = 0; i < dev_count; ++i) { if ((dev_entry[i].vid == SS_VENDOR_ID) && (dev_entry[i].pid == SS_PRODUCT_ID)) { if (!_ss_dev_id_list_exists(dev_entry[i].device_id)) { int fd; if (USB_OpenDevice(dev_entry[i].device_id, SS_VENDOR_ID, SS_PRODUCT_ID, &fd) < 0) { return -3; } dev->device_id = dev_entry[i].device_id; dev->connected = 1; dev->fd = fd; _ss_set_operational(dev); USB_DeviceRemovalNotifyAsync(dev->fd, &_ss_removal_cb, dev); return 1; } } } return -4; }
int ss_open(struct ss_device *dev) { usb_device_entry dev_entry[8]; unsigned char dev_count; if (!_ss_inited) return -1; if (dev->connected) ss_close(dev); if (USB_GetDeviceList(dev_entry, 8, USB_CLASS_HID, &dev_count) < 0) return -2; int i; for (i = 0; i < dev_count; ++i) { if ((dev_entry[i].vid == SS_VENDOR_ID) && (dev_entry[i].pid == SS_PRODUCT_ID)) { if (!_ss_dev_id_list_exists(dev_entry[i].device_id)) { if (USB_OpenDevice(dev_entry[i].device_id, SS_VENDOR_ID, SS_PRODUCT_ID, &dev->fd) < 0) return -3; dev->device_id = dev_entry[i].device_id; dev->connected = 1; dev->enabled = 0; dev->reading = 0; _ss_set_operational(dev); ss_set_led(dev, _ss_dev_number); _ss_dev_id_list_add(dev_entry[i].device_id); _ss_dev_number++; USB_DeviceRemovalNotifyAsync(dev->fd, &_ss_removal_cb, dev); return 1; } } } return -4; }
static int wiiusb_hid_add_adapter(void *data, usb_device_entry *dev) { usb_devdesc desc; const char *device_name = NULL; wiiusb_hid_t *hid = (wiiusb_hid_t*)data; struct wiiusb_adapter *adapter = (struct wiiusb_adapter*) calloc(1, sizeof(struct wiiusb_adapter)); if (!adapter) return -1; if (!hid) { free(adapter); RARCH_ERR("Allocation of adapter failed.\n"); return -1; } if (USB_OpenDevice(dev->device_id, dev->vid, dev->pid, &adapter->handle) < 0) { RARCH_ERR("Error opening device 0x%p (VID/PID: %04x:%04x).\n", dev->device_id, dev->vid, dev->pid); free(adapter); return -1; } adapter->device_id = dev->device_id; USB_GetDescriptors(adapter->handle, &desc); wiiusb_get_description(dev, adapter, &desc); if (adapter->endpoint_in == 0) { RARCH_ERR("Could not find HID config for device.\n"); goto error; } /* Allocate mem for the send control buffer, 32bit aligned */ adapter->send_control_type = WIIUSB_SC_NONE; adapter->send_control_buffer = memalign(32, 128); if (!adapter->send_control_buffer) { RARCH_ERR("Error creating send control buffer.\n"); goto error; } /* Sent the pad name as dummy, we don't know the * control name until we get its interface */ adapter->slot = pad_connection_pad_init(hid->connections, "hid", desc.idVendor, desc.idProduct, adapter, &wiiusb_hid); if (adapter->slot == -1) goto error; if (!pad_connection_has_interface(hid->connections, adapter->slot)) { RARCH_ERR(" Interface not found.\n"); goto error; } adapter->data = memalign(32, 128); adapter->hid = hid; adapter->next = hid->adapters_head; hid->adapters_head = adapter; /* Get the name from the interface */ device_name = wiiusb_hid_joypad_name(hid, adapter->slot); RARCH_LOG("Interface found: [%s].\n", device_name); RARCH_LOG("Device 0x%p attached (VID/PID: %04x:%04x).\n", adapter->device_id, desc.idVendor, desc.idProduct); wiiusb_hid_device_add_autodetect(adapter->slot, device_name, wiiusb_hid.ident, desc.idVendor, desc.idProduct); USB_FreeDescriptors(&desc); USB_DeviceRemovalNotifyAsync(adapter->handle, wiiusb_hid_removal_cb, adapter); return 0; error: if (adapter->send_control_buffer) free(adapter->send_control_buffer); if (adapter) free(adapter); USB_FreeDescriptors(&desc); USB_CloseDevice(&adapter->handle); return -1; }
static int add_adapter(void *data, usb_device_entry *dev) { int rc; usb_devdesc desc; const char *device_name = NULL; struct wiiusb_adapter *old_head = NULL; struct wiiusb_hid *hid = (struct wiiusb_hid*)data; struct wiiusb_adapter *adapter = (struct wiiusb_adapter*) calloc(1, sizeof(struct wiiusb_adapter)); (void)rc; if (!adapter) return -1; if (!hid) { free(adapter); RARCH_ERR("Allocation of adapter failed.\n"); return -1; } if (USB_OpenDevice(dev->device_id, dev->vid, dev->pid, &adapter->handle) < 0) { RARCH_ERR("Error opening device 0x%p (VID/PID: %04x:%04x).\n", (void*)&adapter->device, dev->vid, dev->pid); free(adapter); return -1; } adapter->device = *dev; USB_GetDescriptors(adapter->handle, &desc); wiiusb_get_description(&adapter->device, adapter, &desc); if (adapter->endpoint_in == 0) { RARCH_ERR("Could not find HID config for device.\n"); goto error; } if (desc.iManufacturer) { USB_GetAsciiString(adapter->handle, desc.iManufacturer, 0, sizeof(adapter->manufacturer_name), adapter->manufacturer_name); #if 0 RARCH_ERR(" Adapter Manufacturer name: %s\n", adapter->manufacturer_name); #endif } if (desc.iProduct) { USB_GetAsciiString(adapter->handle, desc.iProduct, 0, sizeof(adapter->name), adapter->name); #if 0 RARCH_ERR(" Adapter name: %s\n", adapter->name); #endif } device_name = (const char *)adapter->name; adapter->send_control_lock = slock_new(); adapter->send_control_buffer = fifo_new(4096); if (!adapter->send_control_lock || !adapter->send_control_buffer) { RARCH_ERR("Error creating send control buffer.\n"); goto error; } adapter->slot = pad_connection_pad_init(hid->slots, device_name, desc.idVendor, desc.idProduct, adapter, &wiiusb_hid_device_send_control); if (adapter->slot == -1) goto error; if (!pad_connection_has_interface(hid->slots, adapter->slot)) { RARCH_ERR(" Interface not found (%s).\n", adapter->name); goto error; } RARCH_LOG("Interface found: [%s].\n", adapter->name); RARCH_LOG("Device 0x%p attached (VID/PID: %04x:%04x).\n", adapter->device, desc.idVendor, desc.idProduct); wiiusb_hid_device_add_autodetect(adapter->slot, device_name, wiiusb_hid.ident, desc.idVendor, desc.idProduct); adapter->hid = hid; adapter->thread = sthread_create(adapter_thread, adapter); if (!adapter->thread) { RARCH_ERR("Error initializing adapter thread.\n"); goto error; } adapter->data = memalign(32, 2048); old_head = adapters.next; adapters.next = adapter; adapter->next = old_head; USB_FreeDescriptors(&desc); USB_DeviceRemovalNotifyAsync(adapter->handle, wiiusb_hid_removalnotify_cb, (void *)hid); return 0; error: if (adapter->thread) sthread_join(adapter->thread); if (adapter->send_control_lock) slock_free(adapter->send_control_lock); if (adapter->send_control_buffer) fifo_free(adapter->send_control_buffer); if (adapter) free(adapter); USB_FreeDescriptors(&desc); USB_CloseDevice(&adapter->handle); return -1; }
s32 USBStorage_Open(usbstorage_handle *dev, s32 device_id, u16 vid, u16 pid) { s32 retval = -1; u8 conf = -1; u8 *max_lun; u32 iConf, iInterface, iEp; usb_devdesc udd; usb_configurationdesc *ucd; usb_interfacedesc *uid; usb_endpointdesc *ued; bool reset_flag = false; max_lun = __lwp_heap_allocate(&__heap, 1); if (!max_lun) return IPC_ENOMEM; memset(dev, 0, sizeof(*dev)); dev->usb_fd = -1; dev->tag = TAG_START; usb_log("USBStorage_Open id: %i\n",device_id); if (LWP_MutexInit(&dev->lock, false) < 0) goto free_and_return; if (SYS_CreateAlarm(&dev->alarm) < 0) goto free_and_return; retry_init: retval = USB_OpenDevice(device_id, vid, pid, &dev->usb_fd); usb_log("USB_OpenDevice, ret: %i\n",retval); if (retval < 0) goto free_and_return; retval = USB_GetDescriptors(dev->usb_fd, &udd); if (retval < 0) { usb_log("USB_GetDescriptors error, ret: %i\n",retval); goto free_and_return; } else usb_log("USB_GetDescriptors OK, configurations: %i\n",udd.bNumConfigurations); for (iConf = 0; iConf < udd.bNumConfigurations; iConf++) { ucd = &udd.configurations[iConf]; usb_log(" Configuration: %i\n",iConf); for (iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++) { uid = &ucd->interfaces[iInterface]; usb_log(" Interface: %i InterfaceClass: 0x%x numendpoints: %i\n",iInterface,uid->bInterfaceClass,uid->bNumEndpoints); if(uid->bInterfaceClass == USB_CLASS_MASS_STORAGE && /* (uid->bInterfaceSubClass == MASS_STORAGE_SCSI_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_RBC_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_ATA_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_QIC_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_UFI_COMMANDS || uid->bInterfaceSubClass == MASS_STORAGE_SFF8070_COMMANDS) &&*/ uid->bInterfaceProtocol == MASS_STORAGE_BULK_ONLY) { if (uid->bNumEndpoints < 2) continue; dev->ep_in = dev->ep_out = 0; for (iEp = 0; iEp < uid->bNumEndpoints; iEp++) { ued = &uid->endpoints[iEp]; if (ued->bmAttributes != USB_ENDPOINT_BULK) continue; usb_log(" USB_ENDPOINT_BULK found: 0x%x MaxPacketSize: %i\n",ued->bEndpointAddress,ued->wMaxPacketSize); if (ued->bEndpointAddress & USB_ENDPOINT_IN) { dev->ep_in = ued->bEndpointAddress; } else { dev->ep_out = ued->bEndpointAddress; if(ued->wMaxPacketSize > 64 && (dev->usb_fd>=0x20 || dev->usb_fd<-1)) usb2_mode=true; else usb2_mode=false; } } if (dev->ep_in != 0 && dev->ep_out != 0) { dev->configuration = ucd->bConfigurationValue; dev->interface = uid->bInterfaceNumber; dev->altInterface = uid->bAlternateSetting; goto found; } } } } usb_log("Device not found device\n"); USB_FreeDescriptors(&udd); retval = USBSTORAGE_ENOINTERFACE; goto free_and_return; found: usb_log("Found device, InterfaceSubClass: %i\n",uid->bInterfaceSubClass); dev->bInterfaceSubClass = uid->bInterfaceSubClass; USB_FreeDescriptors(&udd); retval = USBSTORAGE_EINIT; // some devices return an error, ignore it USB_GetConfiguration(dev->usb_fd, &conf); if(method==0 || method==4) { if (conf != dev->configuration && USB_SetConfiguration(dev->usb_fd, dev->configuration) < 0) { usb_log("Error USB_SetConfiguration\n"); goto free_and_return; } else usb_log("USB_SetConfiguration ok\n"); if (dev->altInterface !=0 && USB_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface) < 0) { usb_log("Error USB_SetAlternativeInterface, alt: %i int: %i\n",dev->altInterface,dev->interface); goto free_and_return; } else usb_log("USB_SetAlternativeInterface ok, alt: %i int: %i\n",dev->altInterface,dev->interface); dev->suspended = 0; if(!reset_flag && dev->bInterfaceSubClass == MASS_STORAGE_SCSI_COMMANDS) { reset_flag = true; retval = USBStorage_Reset(dev); usb_log("USBStorage_Open, USBStorage_Reset: %i\n",retval); if (retval < 0) { USB_CloseDevice(&dev->usb_fd); goto retry_init; } } } else if(method==1) { if ( USB_SetConfiguration(dev->usb_fd, dev->configuration) < 0) { usb_log("Error USB_SetConfiguration\n"); } else usb_log("USB_SetConfiguration ok\n"); if (USB_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface) < 0) { usb_log("Error USB_SetAlternativeInterface, alt: %i int: %i\n",dev->altInterface,dev->interface); } else usb_log("USB_SetAlternativeInterface ok, alt: %i int: %i\n",dev->altInterface,dev->interface); dev->suspended = 0; } else if(method==2) { if ( USB_SetConfiguration(dev->usb_fd, dev->configuration) < 0) { usb_log("Error USB_SetConfiguration\n"); } else usb_log("USB_SetConfiguration ok\n"); if (USB_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface) < 0) { usb_log("Error USB_SetAlternativeInterface, alt: %i int: %i\n",dev->altInterface,dev->interface); } else usb_log("USB_SetAlternativeInterface ok, alt: %i int: %i\n",dev->altInterface,dev->interface); dev->suspended = 0; if(!reset_flag && dev->bInterfaceSubClass == MASS_STORAGE_SCSI_COMMANDS) { reset_flag = true; retval = USBStorage_Reset(dev); usb_log("USBStorage_Open, USBStorage_Reset: %i\n",retval); if (retval < 0) { USB_CloseDevice(&dev->usb_fd); goto retry_init; } } } else if(method==3) { if (USB_SetConfiguration(dev->usb_fd, dev->configuration) < 0) { usb_log("Error USB_SetConfiguration\n"); goto free_and_return; } else usb_log("USB_SetConfiguration ok\n"); if (dev->altInterface !=0 && USB_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface) < 0) { usb_log("Error USB_SetAlternativeInterface, alt: %i int: %i\n",dev->altInterface,dev->interface); goto free_and_return; } else usb_log("USB_SetAlternativeInterface ok, alt: %i int: %i\n",dev->altInterface,dev->interface); dev->suspended = 0; retval = USBStorage_Reset(dev); usb_log("USBStorage_Open, USBStorage_Reset: %i\n",retval); } else if(method==5 || method==6) { if (conf != dev->configuration && USB_SetConfiguration(dev->usb_fd, dev->configuration) < 0) { usb_log("Error USB_SetConfiguration\n"); //goto free_and_return; } else usb_log("USB_SetConfiguration ok\n"); if (dev->altInterface !=0 && USB_SetAlternativeInterface(dev->usb_fd, dev->interface, dev->altInterface) < 0) { usb_log("Error USB_SetAlternativeInterface, alt: %i int: %i\n",dev->altInterface,dev->interface); //goto free_and_return; } else usb_log("USB_SetAlternativeInterface ok, alt: %i int: %i\n",dev->altInterface,dev->interface); if(!usb2_mode) { retval = USBStorage_Reset(dev); usb_log("USBStorage_Open, USBStorage_Reset: %i\n",retval); } dev->suspended = 0; } if(method==6) { dev->max_lun = 2; usb_log("No get max lun call, max_lun: %i\n",dev->max_lun); } else { LWP_MutexLock(dev->lock); retval = __USB_CtrlMsgTimeout(dev, (USB_CTRLTYPE_DIR_DEVICE2HOST | USB_CTRLTYPE_TYPE_CLASS | USB_CTRLTYPE_REC_INTERFACE), USBSTORAGE_GET_MAX_LUN, 0, dev->interface, 1, max_lun); LWP_MutexUnlock(dev->lock); if (retval < 0) dev->max_lun = 1; else dev->max_lun = *max_lun + 1; usb_log("USBSTORAGE_GET_MAX_LUN, ret: %i max_lun: %i\n",retval,dev->max_lun); } if (retval == USBSTORAGE_ETIMEDOUT) goto free_and_return; retval = USBSTORAGE_OK; dev->sector_size = (u32 *) calloc(dev->max_lun, sizeof(u32)); if(!dev->sector_size) { retval = IPC_ENOMEM; goto free_and_return; } /* taken from linux usbstorage module (drivers/usb/storage/transport.c) * * Some devices (i.e. Iomega Zip100) need this -- apparently * the bulk pipes get STALLed when the GetMaxLUN request is * processed. This is, in theory, harmless to all other devices * (regardless of if they stall or not). * * 8/9/10: If anyone wants to actually use a Zip100, they can add this back. * But for now, it seems to be breaking things more than it is helping. */ //USB_ClearHalt(dev->usb_fd, dev->ep_in); //USB_ClearHalt(dev->usb_fd, dev->ep_out); if(!dev->buffer) dev->buffer = __lwp_heap_allocate(&__heap, MAX_TRANSFER_SIZE_V5); if(!dev->buffer) { retval = IPC_ENOMEM; } else { USB_DeviceRemovalNotifyAsync(dev->usb_fd,__usb_deviceremoved_cb,dev); retval = USBSTORAGE_OK; } free_and_return: if (max_lun) __lwp_heap_free(&__heap, max_lun); if (retval < 0) { USBStorage_Close(dev); return retval; } return 0; }