Created
June 19, 2011 16:47
-
-
Save giannitedesco/1034468 to your computer and use it in GitHub Desktop.
Detach a USB device from kernel driver using libusb-1.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <libusb.h> | |
#include <stdint.h> | |
#include <inttypes.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
static libusb_context *ctx; | |
static int do_init(void) | |
{ | |
if ( NULL == ctx && libusb_init(&ctx) ) | |
return 0; | |
return 1; | |
} | |
static int do_device(libusb_device *dev) | |
{ | |
struct libusb_device_descriptor d; | |
struct libusb_config_descriptor *conf; | |
libusb_device_handle *handle; | |
unsigned int i; | |
if ( libusb_get_device_descriptor(dev, &d) ) | |
return 0; | |
printf("%03d.%03d = %04x:%04x\n", | |
libusb_get_bus_number(dev), | |
libusb_get_device_address(dev), | |
d.idVendor, d.idProduct); | |
if ( d.idVendor != 0x19d2 || | |
d.idProduct != 0x1007 ) | |
return 1; | |
if ( libusb_get_active_config_descriptor(dev, &conf) ) | |
return 0; | |
if ( libusb_open(dev, &handle) ) | |
return 0; | |
for(i = 0; i < conf->bNumInterfaces; i++) { | |
if ( libusb_detach_kernel_driver(handle, i) ) { | |
libusb_close(handle); | |
libusb_free_config_descriptor(conf); | |
return 0; | |
} | |
printf(" - detached interface %u\n", i); | |
} | |
//libusb_reset_device(handle); | |
libusb_close(handle); | |
libusb_free_config_descriptor(conf); | |
return 1; | |
} | |
static int do_all_devices(void) | |
{ | |
libusb_device **devlist; | |
ssize_t numdev, i; | |
int ret = 0; | |
if ( !do_init() ) | |
goto out; | |
numdev = libusb_get_device_list(ctx, &devlist); | |
if ( numdev <= 0 ) | |
goto out; | |
for(ret = 1, i = 0; i < numdev; i++) { | |
if ( !do_device(devlist[i]) ) | |
ret = 0; | |
} | |
//out_free: | |
libusb_free_device_list(devlist, 1); | |
out: | |
return ret; | |
} | |
int main(int argc, char **argv) | |
{ | |
do_all_devices(); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment