/* call at unbound state */ static int bind_to_usbip(char *busid) { int configvalue = 0; int ninterface = 0; int i; int failed = 0; configvalue = read_bConfigurationValue(busid); ninterface = read_bNumInterfaces(busid); if (configvalue < 0 || ninterface < 0) { dbg("read config and ninf value, removed?"); return -1; } for (i = 0; i < ninterface; i++) { int ret; ret = bind_interface(busid, configvalue, i, USBIP_HOST_DRV_NAME); if (ret < 0) { dbg("bind usbip at %s:%d.%d, failed", busid, configvalue, i); failed = 1; /* need to contine binding at other interfaces */ } } if (failed) return -1; else return 0; }
static int use_device_by_other(char *busid) { int ret; int config; /* read and write the same config value to kick probing */ config = read_bConfigurationValue(busid); if (config < 0) { g_warning("read bConfigurationValue of %s, failed", busid); return -1; } ret = modify_match_busid(busid, 0); if (ret < 0) { g_warning("del %s to match_busid, failed", busid); return -1; } ret = write_bConfigurationValue(busid, config); if (ret < 0) { g_warning("read bConfigurationValue of %s, failed", busid); return -1; } g_message("bind %s to other drivers than usbip, complete!", busid); return 0; }
static int unbind(char *busid) { int configvalue = 0; int ninterface = 0; int devclass = 0; int i; int failed = 0; configvalue = read_bConfigurationValue(busid); ninterface = read_bNumInterfaces(busid); devclass = read_bDeviceClass(busid); if (configvalue < 0 || ninterface < 0 || devclass < 0) { dbg("read config and ninf value, removed?"); return -1; } if (devclass == 0x09) { dbg("skip unbinding of hub"); return -1; } for (i = 0; i < ninterface; i++) { char driver[PATH_MAX]; int ret; memset(&driver, 0, sizeof(driver)); getdriver(busid, configvalue, i, driver, PATH_MAX-1); dbg(" %s:%d.%d -> %s ", busid, configvalue, i, driver); if (!strncmp("none", driver, PATH_MAX)) continue; /* unbound interface */ #if 0 if (!strncmp("usbip", driver, PATH_MAX)) continue; /* already bound to usbip */ #endif /* unbinding */ ret = unbind_interface(busid, configvalue, i); if (ret < 0) { dbg("unbind driver at %s:%d.%d failed", busid, configvalue, i); failed = 1; } } if (failed) return -1; else return 0; }
static int allusbip(void) { DIR *dir; dir = opendir("/sys/bus/usb/devices/"); if (!dir) g_error("opendir: %s", strerror(errno)); for (;;) { struct dirent *dirent; char *busid; dirent = readdir(dir); if (!dirent) break; busid = dirent->d_name; if (!is_usb_device(busid)) continue; { char name[PATH_MAX]; int conf, ninf = 0; int i; int be_local = 0; conf = read_bConfigurationValue(busid); ninf = read_bNumInterfaces(busid); getdevicename(busid, name, sizeof(name)); for (i = 0; i < ninf; i++) { char driver[PATH_MAX]; getdriver(busid, conf, i, driver, sizeof(driver)); #if 0 if (strncmp(driver, "usbhid", 6) == 0 || strncmp(driver, "usb-storage", 11) == 0) { be_local = 1; break; } #endif } if (be_local == 0) use_device_by_usbip(busid); } } closedir(dir); return 0; }
static int show_devices_script(void) { DIR *dir; dir = opendir("/sys/bus/usb/devices/"); if (!dir) g_error("opendir: %s", strerror(errno)); for (;;) { struct dirent *dirent; char *busid; dirent = readdir(dir); if (!dirent) break; busid = dirent->d_name; if (is_usb_device(busid)) { char name[100] = {'\0'}; char driver[100] = {'\0'}; int conf, ninf = 0; int i; int islocal = 0; conf = read_bConfigurationValue(busid); ninf = read_bNumInterfaces(busid); getdevicename(busid, name, sizeof(name)); for (i = 0; i < ninf; i++) { getdriver(busid, conf, i, driver, sizeof(driver)); if (strncmp(driver, "usbhid", 6) == 0 || strncmp(driver, "usb-storage", 11) == 0) { islocal = 1; break; } } if (islocal == 0) printf("%s|%s|%s\n", busid, driver, name); } } closedir(dir); return 0; }
static int show_devices(void) { DIR *dir; dir = opendir("/sys/bus/usb/devices/"); if (!dir) g_error("opendir: %s", strerror(errno)); printf("List USB devices\n"); for (;;) { struct dirent *dirent; char *busid; dirent = readdir(dir); if (!dirent) break; busid = dirent->d_name; if (is_usb_device(busid)) { char name[100] = {'\0'}; char driver[100] = {'\0'}; int conf, ninf = 0; int i; conf = read_bConfigurationValue(busid); ninf = read_bNumInterfaces(busid); getdevicename(busid, name, sizeof(name)); printf(" - busid %s (%s)\n", busid, name); for (i = 0; i < ninf; i++) { getdriver(busid, conf, i, driver, sizeof(driver)); printf(" %s:%d.%d -> %s\n", busid, conf, i, driver); } printf("\n"); } } closedir(dir); return 0; }