/* usb-try-discon -- try to claim a USB mass-storage interface */ #include #include #include #include #include #include #include #define USBDEVFS_TRY_DISCONNECT _IO('U', 26) int main(int argc, char **argv) { const char *filename; unsigned intf; int fd; int rc; int c; struct usbdevfs_ioctl uioctl; if (argc != 3) { fprintf(stderr, "Usage: usb-try-discon devpath intf-num\n"); return 1; } filename = argv[1]; intf = atoi(argv[2]); fd = open(filename, O_WRONLY); if (fd < 0) { perror("Error opening device file"); return 1; } printf("Trying to disconnect driver of %s interface %d\n", filename, intf); uioctl.ifno = intf; uioctl.ioctl_code = USBDEVFS_TRY_DISCONNECT; uioctl.data = NULL; rc = ioctl(fd, USBDEVFS_IOCTL, &uioctl); if (rc < 0 && errno == ENODATA) { printf("No driver was connected.\n"); } else if (rc < 0) { perror("Error in ioctl"); return 1; } else { printf("Disconnect successful\n"); } printf("Trying to claim the interface\n"); rc = ioctl(fd, USBDEVFS_CLAIMINTERFACE, &intf); if (rc < 0) { perror("Error in ioctl"); return 1; } printf("Claim successful\n"); printf("Press Enter to continue..."); do { c = getc(stdin); } while (c != '\n' && c != EOF); printf("Releasing interface\n"); rc = ioctl(fd, USBDEVFS_RELEASEINTERFACE, &intf); if (rc < 0) { perror("Error in ioctl"); return 1; } printf("Release successful\n"); close(fd); return 0; }