static void wiiusb_hid_scan_for_devices(wiiusb_hid_t *hid) { unsigned i; u8 count; usb_device_entry *dev_entries = (usb_device_entry *) calloc(MAX_USERS, sizeof(*dev_entries)); if (!dev_entries) goto error; if (USB_GetDeviceList(dev_entries, MAX_USERS, USB_CLASS_HID, &count) < 0) goto error; for (i = 0; i < count; i++) { /* first check the device is not already in our list */ if (!wiiusb_hid_new_device(hid, dev_entries[i].device_id)) continue; if (dev_entries[i].vid > 0 && dev_entries[i].pid > 0) wiiusb_hid_add_adapter(hid, &dev_entries[i]); } error: if (dev_entries) free(dev_entries); }
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; }
static void *wiiusb_hid_init(void) { unsigned i; u8 count; int ret; usb_device_entry *dev_entries; wiiusb_hid_t *hid = (wiiusb_hid_t*)calloc(1, sizeof(*hid)); (void)ret; if (!hid) goto error; hid->slots = pad_connection_init(MAX_USERS); if (!hid->slots) goto error; dev_entries = (usb_device_entry *)calloc(MAX_USERS, sizeof(*dev_entries)); if (!dev_entries) goto error; USB_Initialize(); if (USB_GetDeviceList(dev_entries, MAX_USERS, USB_CLASS_HID, &count) < 0) { free(dev_entries); goto error; } for (i = 0; i < count; i++) { if (dev_entries[i].vid > 0 && dev_entries[i].pid > 0) add_adapter(hid, &dev_entries[i]); } free(dev_entries); USB_DeviceChangeNotifyAsync(USB_CLASS_HID, wiiusb_hid_changenotify_cb, (void *)hid); return hid; error: wiiusb_hid_free(hid); return NULL; }
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 wii_find_mouse() { s32 fd=0; static u8 *buffer = 0; if (!buffer) { buffer = (u8*)memalign(32, DEVLIST_MAXSIZE << 3); } if(buffer == NULL) { return -1; } memset(buffer, 0, DEVLIST_MAXSIZE << 3); u8 dummy; u16 vid,pid; if (USB_GetDeviceList("/dev/usb/oh0", buffer, DEVLIST_MAXSIZE, 0, &dummy) < 0) { free(buffer); buffer =0; return -2; } u8 mouseep; u32 mouseep_size; int i; for(i = 0; i < DEVLIST_MAXSIZE; i++) { memcpy(&vid, (buffer + (i << 3) + 4), 2); memcpy(&pid, (buffer + (i << 3) + 6), 2); if ((vid==0)&&(pid==0)) continue; fd =0; int err = USB_OpenDevice("oh0",vid,pid,&fd); if (err<0) { continue; } else { //fprintf(stderr, "ok open %04x:%04x\n", vid, pid); //SDL_Delay(1000); } u32 iConf, iInterface, iEp; usb_devdesc udd; usb_configurationdesc *ucd; usb_interfacedesc *uid; usb_endpointdesc *ued; USB_GetDescriptors(fd, &udd); for(iConf = 0; iConf < udd.bNumConfigurations; iConf++) { ucd = &udd.configurations[iConf]; for(iInterface = 0; iInterface < ucd->bNumInterfaces; iInterface++) { uid = &ucd->interfaces[iInterface]; if ( (uid->bInterfaceClass == USB_CLASS_HID) && (uid->bInterfaceSubClass == USB_SUBCLASS_BOOT) && (uid->bInterfaceProtocol== USB_PROTOCOL_MOUSE)) { int iEp; for(iEp = 0; iEp < uid->bNumEndpoints; iEp++) { ued = &uid->endpoints[iEp]; mouse_vid = vid; mouse_pid = pid; mouseep = ued->bEndpointAddress; mouseep_size = ued->wMaxPacketSize; mouseconfiguration = ucd->bConfigurationValue; mouseinterface = uid->bInterfaceNumber; mousealtInterface = uid->bAlternateSetting; } break; } } } USB_FreeDescriptors(&udd); USB_CloseDevice(&fd); } if (mouse_pid!=0 || mouse_vid!=0) return 0; return -1; }
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; }