static struct usbip_imported_device *imported_device_init(struct usbip_vhci_driver *driver, struct usbip_imported_device *idev, char *busid) { struct sysfs_device *sudev; sudev = sysfs_open_device("usb", busid); if(!sudev) { err("sysfs_open_device %s", busid); goto err; } read_usb_device(sudev, &idev->udev); sysfs_close_device(sudev); /* add class devices of this imported device */ struct class_device *cdev; dlist_for_each_data(driver->cdev_list, cdev, struct class_device) { if(!strncmp(cdev->devpath, idev->udev.path, strlen(idev->udev.path))) { struct class_device *new_cdev; /* alloc and copy because dlist is linked from only one list */ new_cdev = calloc(1, sizeof(*new_cdev)); if(!new_cdev) goto err; memcpy(new_cdev, cdev, sizeof(*new_cdev)); dlist_unshift(idev->cdev_list, (void*) new_cdev); } } return idev; err: return NULL; }
static struct usbip_imported_device *usbip_imported_device_new(struct usbip_vhci_driver *driver, char *busid) { struct usbip_imported_device *idev = NULL; struct sysfs_device *sudev; idev = (struct usbip_imported_device *) calloc(1, sizeof(*idev)); if(!idev) return NULL; /* * The members of idev->cdev_list are also linked from * driver->cdev_list. They are freed by destroy of driver->cdev_list. * */ idev->cdev_list = dlist_new(sizeof(struct class_device)); if(!idev->cdev_list) goto err; sudev = sysfs_open_device("usb", busid); if(!sudev) { err("sysfs_open_device %s", busid); goto err; } read_usb_device(sudev, &idev->udev); sysfs_close_device(sudev); /* add class devices of this imported device */ struct class_device *cdev; dlist_for_each_data(driver->cdev_list, cdev, struct class_device) { if(!strncmp(cdev->devpath, idev->udev.path, strlen(idev->udev.path))) { struct class_device *new_cdev; new_cdev = calloc(1, sizeof(*new_cdev)); if(!new_cdev) goto err; memcpy(new_cdev, cdev, sizeof(*new_cdev)); dlist_unshift(idev->cdev_list, (void*) new_cdev); } } return idev; err: if(idev->cdev_list) dlist_destroy(idev->cdev_list); if(idev) free(idev); return NULL; }
static struct usbip_exported_device *usbip_exported_device_new(char *sdevpath) { struct usbip_exported_device *edev = NULL; size_t size; int i; edev = calloc(1, sizeof(*edev)); if (!edev) { dbg("calloc failed"); return NULL; } edev->sudev = sysfs_open_device_path(sdevpath); if (!edev->sudev) { dbg("sysfs_open_device_path failed: %s", sdevpath); goto err; } read_usb_device(edev->sudev, &edev->udev); edev->status = read_attr_usbip_status(&edev->udev); if (edev->status < 0) goto err; /* reallocate buffer to include usb interface data */ size = sizeof(*edev) + edev->udev.bNumInterfaces * sizeof(struct usbip_usb_interface); edev = realloc(edev, size); if (!edev) { dbg("realloc failed"); goto err; } for (i = 0; i < edev->udev.bNumInterfaces; i++) read_usb_interface(&edev->udev, i, &edev->uinf[i]); return edev; err: if (edev && edev->sudev) sysfs_close_device(edev->sudev); if (edev) free(edev); return NULL; }
static struct usbip_imported_device * imported_device_init(struct usbip_imported_device *idev, char *busid) { struct udev_device *sudev; sudev = udev_device_new_from_subsystem_sysname(udev_context, "usb", busid); if (!sudev) { dbg("udev_device_new_from_subsystem_sysname failed: %s", busid); goto err; } read_usb_device(sudev, &idev->udev); udev_device_unref(sudev); return idev; err: return NULL; }
int main(int argc, char** argv) { srand(time(NULL)); if (argc == 2 && strcmp("list", argv[1]) == 0) { usb_init(); usb_find_busses(); usb_find_devices(); list_usb_devices(); } else if ((argc == 4 || argc == 5 || argc == 6) && (strcmp("cat", argv[1]) == 0 || strcmp("read", argv[1]) == 0 || strcmp("write", argv[1]) == 0)) { uint16_t idVendor; uint16_t idProduct; int interface = 0; int endpoint = 1; if (sscanf(argv[2], "0x%hx", &idVendor) == 1 && sscanf(argv[3], "0x%hx", &idProduct) == 1) { if (argc >= 5) interface = atoi(argv[4]); if (argc == 6) endpoint = atoi(argv[5]); usb_init(); usb_find_busses(); usb_find_devices(); struct usb_device* dev = find_usb_device(idVendor, idProduct); if (!dev) { std::cout << "Error: Device (" << boost::format("idVendor: 0x%04hx, idProduct: 0x%04hx") % idVendor % idProduct << ") not found" << std::endl; } else { if (strcmp("cat", argv[1]) == 0) { std::cout << "Reading data from: " << dev << " Interface: " << interface << " Endpoint: " << endpoint << std::endl; cat_usb_device(dev, interface, endpoint); } else if (strcmp("read", argv[1]) == 0) { read_usb_device(dev, interface, endpoint); } else if (strcmp("write", argv[1]) == 0) { write_usb_device(dev, interface, endpoint); } } } else { std::cout << "Error: Expected IDVENDOR IDPRODUCT" << std::endl; } } else { std::cout << "Usage: " << argv[0] << " list\n" << " " << argv[0] << " cat IDVENDOR IDPRODUCT [INTERFACE] [ENDPOINT]" << std::endl; } }