int init(void) { struct usb_device *dev; dev = find_device(); if (dev == NULL) { printf("not found\n"); goto cleanup; } handle = usb_open(dev); if (handle == NULL) goto cleanup; usb_detach_kernel_driver_np(handle, 0); if (usb_set_configuration(handle, 1) < 0) goto cleanup; if (usb_claim_interface(handle, 0) < 0) goto cleanup; //if (usb_set_altinterface(handle, 2) < 0) //goto cleanup; return 0; cleanup: assert(0); return 0; }
static usb_dev_handle *get_ir(void) { static usb_dev_handle *ir = NULL; if (!ir) { usb_init(); usb_find_busses(); usb_find_devices(); ir = find_ir(); if (!ir) { fprintf(stderr, "IR receiver not found, quitting\n"); exit(1); } /* interface is normally handled by hiddev */ usb_detach_kernel_driver_np(ir, 0); if (usb_claim_interface(ir, 0)) { fprintf(stderr, "error claiming interface, are you root?\n"); exit(2); } //usb_reset(ir); //usb_set_configuration(ir, 0); } return ir; }
static int open_interface(struct rf2500_transport *tr, struct usb_device *dev, int ino) { printc("Trying to open interface %d on %s\n", ino, dev->filename); tr->int_number = ino; tr->handle = usb_open(dev); if (!tr->handle) { pr_error("rf2500: can't open device"); return -1; } #if defined(__linux__) if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0) pr_error("rf2500: warning: can't " "detach kernel driver"); #endif #ifdef WIN32 if (usb_set_configuration(tr->handle, 1) < 0) { pr_error("rf2500: can't set configuration 1"); usb_close(tr->handle); return -1; } #endif if (usb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error("rf2500: can't claim interface"); usb_close(tr->handle); return -1; } return 0; }
int main(int argc, char **argv) { usb_dev_handle *handle = NULL; const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; char buffer[50] = ""; unsigned char request; int vid, pid, nBytes; usb_init(); if(argc < 2){ /* we need at least one argument */ printf("Arguments Missing"); exit(1); } request = atoi(argv[1]); /* compute VID/PID from usbconfig.h so that there is a central source of information */ vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; /* The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } /* Since we use only control endpoint 0, we don't need to choose a * configuration and interface. Reading device descriptor and setting a * configuration and interface is done through endpoint 0 after all. * However, newer versions of Linux require that we claim an interface * even for endpoint 0. Enable the following code if your operating system * needs it: */ #if 0 int retries = 1, usbConfiguration = 1, usbInterface = 0; if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } /* now try to claim the interface and detach the kernel HID driver on * Linux and other operating systems which support the call. */ while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); } #endif } #endif nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, request, 0, 0, (char *)buffer, sizeof(buffer), 5000); if(nBytes < 0) printf("error in USB control transfer: %s\n", usb_strerror()); usb_close(handle); return 0; }
int detach_driver(struct usb_dev_handle *handle, int interface_number) { int r; r = usb_detach_kernel_driver_np(handle, interface_number); r = usb_return(((r == -61)? 0 : r), "detach_driver: usb_detach_kernel_driver_np"); return r; }
int usbClaimInterface (UsbDevice *device, unsigned char interface) { UsbDeviceExtension *devx = device->extension; int detached = 0; int result; logMessage(LOG_CATEGORY(USB_IO), "claiming interface: %u", interface); while (1) { char driver[0X100]; result = usb_claim_interface(devx->handle, interface); if (result >= 0) return 1; if (result != -EBUSY) break; if (detached) break; #ifdef LIBUSB_HAS_GET_DRIVER_NP result = usb_get_driver_np(devx->handle, interface, driver, sizeof(driver)); if (result < 0) #endif /* LIBUSB_HAS_GET_DRIVER_NP */ { strcpy(driver, "unknown"); } logMessage(LOG_WARNING, "USB interface in use: %u (%s)", interface, driver); if (strcmp(driver, "usbfs") == 0) { result = -EBUSY; break; } #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP logMessage(LOG_CATEGORY(USB_IO), "detaching kernel driver: %u (%s)", interface, driver); result = usb_detach_kernel_driver_np(devx->handle, interface); if (result >= 0) { logMessage(LOG_CATEGORY(USB_IO), "detached kernel driver: %u (%s)", interface, driver); detached = 1; continue; } result = -EBUSY; #endif /* LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP */ break; } errno = -result; logSystemError("USB interface claim"); return 0; }
static int gp_port_usb_open (GPPort *port) { int ret; char name[64]; gp_log (GP_LOG_DEBUG,"libusb","gp_port_usb_open()"); if (!port || !port->pl->d) return GP_ERROR_BAD_PARAMETERS; /* * Open the device using the previous usb_handle returned by * find_device */ port->pl->dh = usb_open (port->pl->d); if (!port->pl->dh) { gp_port_set_error (port, _("Could not open USB device (%m).")); return GP_ERROR_IO; } #if defined(LIBUSB_HAS_GET_DRIVER_NP) && defined(LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP) memset(name,0,sizeof(name)); ret = usb_get_driver_np (port->pl->dh, port->settings.usb.interface, name, sizeof(name) ); if (strstr(name,"usbfs") || strstr(name,"storage")) { /* other gphoto instance most likely * Also let mass storage be served by regular means. */ gp_port_set_error (port, _("Camera is already in use.")); return GP_ERROR_IO_LOCK; } if (ret >= 0) { gp_log (GP_LOG_DEBUG,"libusb",_("Device has driver '%s' attached, detaching it now."), name); ret = usb_detach_kernel_driver_np (port->pl->dh, port->settings.usb.interface); if (ret < 0) gp_port_set_error (port, _("Could not detach kernel driver '%s' of camera device."),name); else port->pl->detached = 1; } else { if (errno != ENODATA) /* ENODATA - just no driver there */ gp_port_set_error (port, _("Could not query kernel driver of device.")); } #endif gp_log (GP_LOG_DEBUG,"libusb","claiming interface %d", port->settings.usb.interface); ret = usb_claim_interface (port->pl->dh, port->settings.usb.interface); if (ret < 0) { gp_port_set_error (port, _("Could not claim " "interface %d (%m). Make sure no other program " "or kernel module (such as %s) is using the device " "and you have read/write access to the device."), port->settings.usb.interface, "sdc2xx, stv680, spca50x"); return GP_ERROR_IO_USB_CLAIM; } return GP_OK; }
usb_dev_handle *m4Init() { struct usb_bus *bus; struct usb_device *dev; usb_init(); if (usb_find_busses() < 0) { return NULL; } if (usb_find_devices() < 0) { return NULL; } bus = usb_get_busses(); while (bus) { dev = bus->devices; while (dev) { if (dev->descriptor.idVendor == VENDOR && dev->descriptor.idProduct == PRODUCT) { usb_dev_handle *handle = usb_open(dev); if (handle) { #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP /* Linux usually claims HID devices for its usbhid driver. */ usb_detach_kernel_driver_np(handle, 0); #endif if (usb_set_configuration(handle, 1) >= 0) { if (usb_claim_interface(handle, 0) >= 0) { if (usb_set_altinterface(handle, 0) < 0) { usb_close(handle); return NULL; } } else { usb_close(handle); return NULL; } } else { usb_close(handle); return NULL; } return handle; } } dev = dev->next; } bus = bus->next; } return NULL; }
void write_usb_device(struct usb_device* dev, int interface, int endpoint) { struct usb_dev_handle* handle = usb_open(dev); if (!handle) { std::cout << "Error opening usb device" << std::endl; } else { if (usb_claim_interface(handle, interface) != 0) { std::cout << "Error claiming the interface: " << usb_strerror() << std::endl; if (usb_detach_kernel_driver_np(handle, interface) < 0) { std::cout << "Failure to kick kernel driver: " << usb_strerror() << std::endl; exit(EXIT_FAILURE); } if (usb_claim_interface(handle, interface) != 0) { std::cout << "Error claiming the interface: " << usb_strerror() << std::endl; exit(EXIT_FAILURE); } } bool quit = false; while(!quit) { uint8_t data[32]; if (1) { int ret = fread(data, sizeof(char), sizeof(data), stdin); std::cout << ret << std::endl; usb_interrupt_write(handle, endpoint, (char*)data, ret, 0); } else { int ret = sizeof(data); for(int i = 0; i < ret ; ++i) { data[i] = int(127 * sin(float(i) / ret * M_PI*2)) + 127; std::cout << ret << std::endl; } std::cout << ret << std::endl; usb_interrupt_write(handle, endpoint, (char*)data, ret, 0); } } } }
int send_command(usb_dev_handle *handle, cmdstruct command ) { if (command.numCmds == 0) { printf( "send_command: Empty command provided! Not sending anything...\n"); return 0; } int stat; stat = usb_detach_kernel_driver_np(handle, 0); if ((stat < 0 ) || verbose_flag) perror("Detach kernel driver"); stat = usb_claim_interface( handle, 0 ); if ( (stat < 0) || verbose_flag) perror("Claiming USB interface"); int transferred = 0; // send all command strings provided in command int cmdCount; for (cmdCount=0; cmdCount < command.numCmds; cmdCount++) { if (verbose_flag) { char raw_string[255]; print_cmd(raw_string, command.cmds[cmdCount]); printf("\tSending string: \"%s\"\n", raw_string); } stat = usb_interrupt_write( handle, 1, (const char*)command.cmds[cmdCount], sizeof( command.cmds[cmdCount] ), TRANSFER_WAIT_TIMEOUT_MS ); transferred = (stat>0)?stat:0; if ( (stat < 0) || verbose_flag) perror("Sending USB command"); } /* In case the command just sent caused the device to switch from restricted mode to native mode * the following two commands will fail due to invalid device handle (because the device changed * its pid on the USB bus). * So it is not possible anymore to release the interface and re-attach kernel driver. * I am not sure if this produces a memory leak within libusb, but i do not think there is another * solution possible... */ stat = usb_release_interface(handle, 0 ); /*FIXME: if (stat != usb_ERROR_NO_DEVICE) { // silently ignore "No such device" error due to reasons explained above. if ( (stat < 0) || verbose_flag) { perror("Releasing USB interface."); } } */ //FIXME: Not portable?! stat = usb_attach_kernel_driver_np( handle, 0); /*FIXME: if (stat != usb_ERROR_NO_DEVICE) { // silently ignore "No such device" error due to reasons explained above. if ( (stat < 0) || verbose_flag) { perror("Reattaching kernel driver"); } } */ return 0; }
static int open_interface(struct cp210x_transport *tr, struct usb_device *dev, int ino, int baud_rate) { #if defined(__linux__) int drv; char drName[256]; #endif printc_dbg(__FILE__": Trying to open interface %d on %s\n", ino, dev->filename); tr->int_number = ino; tr->handle = usb_open(dev); if (!tr->handle) { pr_error(__FILE__": can't open device"); return -1; } #if defined(__linux__) drv = usb_get_driver_np(tr->handle, tr->int_number, drName, sizeof(drName)); printc(__FILE__" : driver %d\n", drv); if (drv >= 0) { if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0) pr_error(__FILE__": warning: can't detach " "kernel driver"); } #endif #ifdef __Windows__ if (usb_set_configuration(tr->handle, 1) < 0) { pr_error(__FILE__": can't set configuration 1"); usb_close(tr->handle); return -1; } #endif if (usb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error(__FILE__": can't claim interface"); usb_close(tr->handle); return -1; } if (configure_port(tr, baud_rate) < 0) { printc_err("Failed to configure for V1 device\n"); usb_close(tr->handle); return -1; } return 0; }
/* USB::DevHandle#usb_detach_kernel_driver_np(interface) */ static VALUE rusb_detach_kernel_driver_np( VALUE v, VALUE vinterface) { usb_dev_handle *p = get_usb_devhandle(v); int interface = NUM2INT(vinterface); int ret; ret = usb_detach_kernel_driver_np(p, interface); check_usb_error("usb_detach_kernel_driver_np", ret); return Qnil; }
usb_dev_handle * open_usb_device(int vid, int pid) { struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *h; char buf[128]; int r; usb_init(); usb_find_busses(); usb_find_devices(); //printf_verbose("\nSearching for USB device:\n"); for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { //printf_verbose("bus \"%s\", device \"%s\" vid=%04X, pid=%04X\n", // bus->dirname, dev->filename, // dev->descriptor.idVendor, // dev->descriptor.idProduct //); if (dev->descriptor.idVendor != vid) continue; if (dev->descriptor.idProduct != pid) continue; h = usb_open(dev); if (!h) { printf_verbose("Found device but unable to open"); continue; } #ifdef LIBUSB_HAS_GET_DRIVER_NP r = usb_get_driver_np(h, 0, buf, sizeof(buf)); if (r >= 0) { r = usb_detach_kernel_driver_np(h, 0); if (r < 0) { usb_close(h); printf_verbose("Device is in use by \"%s\" driver", buf); continue; } } #endif // Mac OS-X - removing this call to usb_claim_interface() might allow // this to work, even though it is a clear misuse of the libusb API. // normally Apple's IOKit should be used on Mac OS-X r = usb_claim_interface(h, 0); if (r < 0) { usb_close(h); printf_verbose("Unable to claim interface, check USB permissions"); continue; } return h; } } return NULL; }
/* * detaches the kernel from a particular USB interface. The kernel tends to * claim HID devices it recognises, so if you want to utilitize the device * directly through libusb you must first detach the kernel. This stop the * device from acting like a regular linux mouse or keyboard and let you utilize * it directly */ VALUE interface_detach_kernel(VALUE self) { if(interface_is_claimable(self) == Qtrue) return Qnil; struct usb_interface_descriptor* interface; Data_Get_Struct(self, struct usb_interface_descriptor, interface); ENSURE_OPEN_BEGIN(rb_iv_get(self, "@device")); int result = usb_detach_kernel_driver_np(handle, interface->bInterfaceNumber); ENSURE_OPEN_END; if(result < 0) { raise_usb_error(); } return Qnil; }
void *_ykusb_open_device(int vendor_id, int *product_ids, size_t pids_len) { struct usb_bus *bus; struct usb_device *yk_device; struct usb_dev_handle *h = NULL; int rc = YK_EUSBERR; int found = 0; for (bus = usb_get_busses(); bus; bus = bus->next) { struct usb_device *dev; rc = YK_ENOKEY; for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == vendor_id) { size_t j; for (j = 0; j < pids_len; j++) { if (dev->descriptor.idProduct == product_ids[j]) { if(found == 0) { yk_device = dev; found = 1; break; } else { rc = YK_EMORETHANONE; goto done; } } } } } } if(found == 1) { rc = YK_EUSBERR; h = usb_open(yk_device); #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if (h != NULL) usb_detach_kernel_driver_np(h, 0); #endif /* This is needed for yubikey-personalization to work inside virtualbox virtualization. */ if (h != NULL) usb_set_configuration(h, 1); goto done; } done: if (h == NULL) yk_errno = rc; return h; }
static int handle_device(int dev_id) { int r, i; struct usb_device *dev = g_devices[dev_id].usb_dev; usb_dev_handle *hdev = g_devices[dev_id].hdev; usb_detach_kernel_driver_np(hdev, 0); if( 0 != (r = usb_set_configuration(hdev, dev->config[0].bConfigurationValue)) ) { printf("usb_set_configuration returns %d (%s)\n", r, usb_strerror()); return -1; } if((r = usb_claim_interface(hdev, 0)) < 0) { printf("Interface cannot be claimed: %d\n", r); return r; } int nep = dev->config->interface->altsetting->bNumEndpoints; for(i=0; i<nep; i++) { int ep = dev->config->interface->altsetting->endpoint[i].bEndpointAddress; if(ep&(1<<7)) g_devices[dev_id].epin = ep; else g_devices[dev_id].epout = ep; } // Set baudrate int baudrate = 250000; r = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_ENDPOINT_OUT, CP210X_IFC_ENABLE, UART_ENABLE, 0, NULL, 0, 500); r = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_ENDPOINT_OUT, CP210X_SET_BAUDRATE, 0, 0, (char *)&baudrate, sizeof(baudrate), 500); r = usb_control_msg(hdev, USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_ENDPOINT_OUT, CP210X_IFC_ENABLE, UART_DISABLE, 0, NULL, 0, 500); // read/write main loop io_loop(dev_id); usb_release_interface(hdev, 0); return 0; }
int main(int argc, char **argv) { unsigned int count = 0U, removed = 0U; struct usb_bus *bus; struct usb_device *dev; static usb_dev_handle *handle; char name[256]; usb_init(); usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { for (dev = bus->devices; dev != NULL; dev = dev->next) { if (dev->descriptor.idVendor == C108_VENDOR_ID && (dev->descriptor.idProduct == C108_PRODUCT_ID || dev->descriptor.idProduct == C108AH_PRODUCT_ID || dev->descriptor.idProduct == C119_PRODUCT_ID)) { count++; if ((handle = usb_open(dev)) == NULL) { fprintf(stderr, "rmuridrv: cannot open the USB device: %s\n", usb_strerror()); continue; } memset(name, 0, 256); if (usb_get_driver_np(handle, CM108_INTERFACE, name, 256) == 0) { if (strcmp(name, "usbhid") == 0) { if (usb_detach_kernel_driver_np(handle, CM108_INTERFACE) == 0) removed++; else fprintf(stderr, "rmuridrv: could not disconnect from usbhid: %s\n", usb_strerror()); } } usb_close(handle); } } } fprintf(stdout, "rmuridrv: found %u CM108 devices and removed %u from the usbhid driver\n", count, removed); return 0; }
void read_usb_device(struct usb_device* dev, int interface, int endpoint) { struct usb_dev_handle* handle = usb_open(dev); if (!handle) { std::cout << "Error opening usb device" << std::endl; } else { if (usb_claim_interface(handle, interface) != 0) { std::cout << "Error claiming the interface: " << usb_strerror() << std::endl; if (usb_detach_kernel_driver_np(handle, interface) < 0) { std::cout << "Failure to kick kernel driver: " << usb_strerror() << std::endl; exit(EXIT_FAILURE); } if (usb_claim_interface(handle, interface) != 0) { std::cout << "Error claiming the interface: " << usb_strerror() << std::endl; exit(EXIT_FAILURE); } } bool quit = false; while(!quit) { uint8_t data[8192]; int ret = usb_interrupt_read(handle, endpoint, (char*)data, sizeof(data), 0); if (ret < 0) { std::cerr << "USBError: " << ret << "\n" << usb_strerror() << std::endl; std::cerr << "Shutting down" << std::endl; quit = true; } fwrite(data, sizeof(char), ret, stdout); } } }
static int open_device(struct bslhid_transport *tr, struct usb_device *dev) { if (find_interface(tr, dev) < 0) return -1; printc_dbg("Opening interface %d (config %d)...\n", tr->int_number, tr->cfg_number); if (find_endpoints(tr, dev) < 0) return -1; printc_dbg("Found endpoints: IN: 0x%02x, OUT: 0x%02x\n", tr->in_ep, tr->out_ep); tr->handle = usb_open(dev); if (!tr->handle) { pr_error("bslhid: can't open device"); return -1; } #ifdef __Windows__ if (usb_set_configuration(tr->handle, tr->cfg_number) < 0) pr_error("warning: bslhid: can't set configuration"); #endif #ifdef __linux__ if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0) pr_error("warning: bslhid: can't detach kernel driver"); #endif if (usb_claim_interface(tr->handle, tr->int_number) < 0) { pr_error("bslhid: can't claim interface"); usb_close(tr->handle); return -1; } /* Save the bus path for a future suspend/resume */ strncpy(tr->bus_name, dev->bus->dirname, sizeof(tr->bus_name)); tr->bus_name[sizeof(tr->bus_name) - 1] = 0; return 0; }
MyDriver::MyDriver() { const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID}; char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; int vid, pid; handle=NULL; usb_init(); // compute VID/PID from usbconfig.h so that there is a central source of information vid = rawVid[1] * 256 + rawVid[0]; pid = rawPid[1] * 256 + rawPid[0]; // The following function is in opendevice.c: */ if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){ fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } // Since we use only control endpoint 0, we don't need to choose a // configuration and interface. Reading device descriptor and setting a // configuration and interface is done through endpoint 0 after all. // However, newer versions of Linux require that we claim an interface // even for endpoint 0. Enable the following code if your operating system // needs it #if 0 int retries = 1, usbConfiguration = 1, usbInterface = 0; if(usb_set_configuration(handle, usbConfiguration) && showWarnings){ fprintf(stderr, "Warning: could not set configuration: %s\n", usb_strerror()); } // now try to claim the interface and detach the kernel HID driver on // Linux and other operating systems which support the call. while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){ #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){ fprintf(stderr, "Warning: could not detach kernel driver: %s\n", usb_strerror()); } #endif } #endif }
int main(void){ // printf("SizeOF %ld\n", sizeof(struct usb_dev_handle*)); libusb_init(); libusb_set_debug(2); libusb_find_busses(); libusb_find_devices(); struct usb_dev_handle* green = malloc(256); green = get_green(bus, dev); printf("Detach: %d\n", usb_detach_kernel_driver_np(green, 0)); printf("Halt: %d\n", usb_clear_halt(green, 0x81)); printf("Interface: %d\n", usb_claim_interface(green, 0)); libusb_alloc_transfer(0); // printf("Buffer: %x\n", buffer); return 0; }
void usb_detach(usb_dev_handle *lvr_winusb, int iInterface) { int ret; ret = usb_detach_kernel_driver_np(lvr_winusb, iInterface); if(ret) { if(errno == ENODATA) { if(debug) { printf("Device already detached\n"); } } else { if(debug) { printf("Detach failed: %s[%d]\n", strerror(errno), errno); printf("Continuing anyway\n"); } } } else { if(debug) { printf("detach successful\n"); } } }
void process_device(int argc, char **argv, struct usb_device *dev, struct usb_config_descriptor *cfg, int itfnum) { int mac[6]; usb_dev_handle *devh = usb_open(dev); if ( ! devh ) fatal("usb_open"); usb_detach_kernel_driver_np(devh, itfnum); int res = usb_claim_interface(devh, itfnum); if ( res < 0 ) fatal("usb_claim_interface"); show_master(devh, itfnum); if ( argc >= 2 ) { if ( sscanf(argv[1], "%x:%x:%x:%x:%x:%x", &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) { printf("usage: %s [<bd_addr of master>]\n", argv[0]); exit(1); } } else { FILE *f = popen("hcitool dev", "r"); if ( !f || fscanf(f, "%*s\n%*s %x:%x:%x:%x:%x:%x", &mac[0],&mac[1],&mac[2],&mac[3],&mac[4],&mac[5]) != 6 ) { printf("Unable to retrieve local bd_addr from `hcitool dev`.\n"); printf("Please enable Bluetooth or specify an address manually.\n"); exit(1); } pclose(f); } set_master(devh, itfnum, mac); usb_reset(devh); usb_release_interface(devh, itfnum); usb_close(devh); }
void *_ykusb_open_device(int vendor_id, int product_id) { struct usb_bus *bus; struct usb_device *dev; struct usb_dev_handle *h = NULL; int rc = YK_EUSBERR; for (bus = usb_get_busses(); bus; bus = bus->next) { rc = YK_ENOKEY; for (dev = bus->devices; dev; dev = dev->next) if (dev->descriptor.idVendor == YUBICO_VID && dev->descriptor.idProduct == YUBIKEY_PID) { rc = YK_EUSBERR; h = usb_open(dev); if (h != NULL) usb_detach_kernel_driver_np(h, 0); goto done; } } done: if (h == NULL) yk_errno = rc; return h; }
usb_dev_handle* OpenProxmark(int verbose) { int ret; usb_dev_handle *handle = NULL; unsigned int iface; handle = findProxmark(verbose, &iface); if (!handle) return NULL; #ifdef __linux__ /* detach kernel driver first */ ret = usb_detach_kernel_driver_np(handle, iface); /* don't complain if no driver attached */ if (ret<0 && ret != -61 && verbose) fprintf(stderr, "detach kernel driver failed: (%d) %s!\n", ret, usb_strerror()); #endif // Needed for Windows. Optional for Mac OS and Linux ret = usb_set_configuration(handle, 1); if (ret < 0) { if (verbose) fprintf(stderr, "configuration set failed: %s!\n", usb_strerror()); return NULL; } ret = usb_claim_interface(handle, iface); if (ret < 0) { if (verbose) fprintf(stderr, "claim failed: %s!\n", usb_strerror()); return NULL; } claimed_iface = iface; devh = handle; return handle; }
usb_dev_handle* setup_libusb_access() { usb_dev_handle *lvr_winusb; usb_set_debug(0); usb_init(); usb_find_busses(); usb_find_devices(); if(!(lvr_winusb = find_lvr_winusb())) { return NULL; } /* Linux*/ usb_detach_kernel_driver_np(lvr_winusb,0); if (usb_set_configuration(lvr_winusb, 1) < 0) { printf("Could not set configuration 1 : \n"); return NULL; } if (usb_claim_interface(lvr_winusb, INTFACE) < 0) { printf("Could not claim interface: \n"); return NULL; } return lvr_winusb; }
static int drv_MOGX_open(void) { struct usb_bus *busses, *bus; struct usb_device *dev; char driver[1024]; char product[1024]; char manufacturer[1024]; char serialnumber[1024]; int ret; lcd_dev = NULL; info("%s: scanning for Matrix Orbital GX Series LCD...", Name); usb_set_debug(0); usb_init(); usb_find_busses(); usb_find_devices(); busses = usb_get_busses(); for (bus = busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if ((dev->descriptor.idVendor == MatrixOrbitalGX_VENDOR) && ((dev->descriptor.idProduct == MatrixOrbitalGX_DEVICE_1) || (dev->descriptor.idProduct == MatrixOrbitalGX_DEVICE_2) || (dev->descriptor.idProduct == MatrixOrbitalGX_DEVICE_3))) { /* At the moment, I have information for only this LCD */ if (dev->descriptor.idProduct == MatrixOrbitalGX_DEVICE_2) backlight_RGB = 0; info("%s: found Matrix Orbital GX Series LCD on bus %s device %s", Name, bus->dirname, dev->filename); lcd_dev = usb_open(dev); ret = usb_get_driver_np(lcd_dev, 0, driver, sizeof(driver)); if (ret == 0) { info("%s: interface 0 already claimed by '%s'", Name, driver); info("%s: attempting to detach driver...", Name); if (usb_detach_kernel_driver_np(lcd_dev, 0) < 0) { error("%s: usb_detach_kernel_driver_np() failed!", Name); return -1; } } usb_set_configuration(lcd_dev, 1); usleep(100); if (usb_claim_interface(lcd_dev, 0) < 0) { error("%s: usb_claim_interface() failed!", Name); return -1; } usb_set_altinterface(lcd_dev, 0); usb_get_string_simple(lcd_dev, dev->descriptor.iProduct, product, sizeof(product)); usb_get_string_simple(lcd_dev, dev->descriptor.iManufacturer, manufacturer, sizeof(manufacturer)); usb_get_string_simple(lcd_dev, dev->descriptor.iSerialNumber, serialnumber, sizeof(serialnumber)); info("%s: Manufacturer='%s' Product='%s' SerialNumber='%s'", Name, manufacturer, product, serialnumber); return 0; } } } error("%s: could not find a Matrix Orbital GX Series LCD", Name); return -1; }
int main(int argc, char *argv[]) { struct usb_bus *bus; struct usb_device *dev, *mydev; usb_dev_handle *handle; unsigned char bytes[64]; int ret; int i; for (ret = 0; ret < 64; ret++) { bytes[ret] = 0; } bytes[0] = 0; bytes[1] = 0; bytes[2] = 0; bytes[3] = 0; bytes[4] = 0; bytes[5] = 0; bytes[6] = 0x01; bytes[7] = 0xbb; usb_init(); usb_find_busses(); usb_find_devices(); for (bus = usb_busses; bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (dev->descriptor.idVendor == 0x1130 && dev->descriptor.idProduct == 0x6807) { printf("find my device\n"); mydev = dev; break; } } } handle = usb_open(mydev); printf("handle = %d\n", handle); //usb_get_driver_np(handle, 0, bytes, 64); //fprintf(stderr, "return usb_get_driver name is %s\n", bytes); #if 1 ret = usb_detach_kernel_driver_np(handle, 1); fprintf(stderr, "usb_detach_kernel_driver_np return %d\n", ret); ret = usb_claim_interface(handle, 1); printf("claim interface return %d\n", ret); #endif #if 1 ret = usb_detach_kernel_driver_np(handle, 0); fprintf(stderr, "return usb_detach_kernel_driver_np is %d\n", ret); ret = usb_claim_interface(handle, 0); printf("claim interface return %d\n", ret); #endif ret = usb_control_msg(handle, 0x21, 0x09, 0x0200, 1, bytes, 0x0008, 1000); printf("usb_control_msg return %d\n", ret); sleep(1); ret = usb_interrupt_read(handle, 3, bytes, 64, 1000); printf("usb_interrupt_read return %d\n", ret); for (i = 0; i < 64; i++) { printf("0x%02x ", bytes[i]); } printf("\n"); usb_release_interface(handle, 0); usb_release_interface(handle, 1); usb_close(handle); return 0; }
/** * API: Initialize glcd2usb connection type. */ int glcd2usb_init(Driver *drvthis) { PrivateData *p = (PrivateData *)drvthis->private_data; CT_glcd2usb_data *ctd; static int didUsbInit = 0; struct usb_bus *bus; struct usb_device *dev; usb_dev_handle *handle = NULL; int err = 0; int rval, retries = 3; int len; /* Set up connection type low-level functions */ p->glcd_functions->blit = glcd2usb_blit; p->glcd_functions->close = glcd2usb_close; p->glcd_functions->set_backlight = glcd2usb_backlight; p->glcd_functions->poll_keys = glcd2usb_poll_keys; /* Allocate memory structures */ ctd = (CT_glcd2usb_data *) calloc(1, sizeof(CT_glcd2usb_data)); if (ctd == NULL) { report(RPT_ERR, "%s/glcd2usb: error allocating connection data", drvthis->name); return -1; } p->ct_data = ctd; /* * Try to find and open a device. Only the first device found will be * recognized. */ if (!didUsbInit) { usb_init(); didUsbInit = 1; } usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { for (dev = bus->devices; dev != NULL; dev = dev->next) { if (dev->descriptor.idVendor == GLCD2USB_VID && dev->descriptor.idProduct == GLCD2USB_PID) { handle = usb_open(dev); if (!handle) { report(RPT_WARNING, "%s/glcd2usb: cannot open USB device: %s", drvthis->name, usb_strerror()); continue; } else { goto found_dev; } } } } found_dev: if (handle) { debug(RPT_DEBUG, "%s/glcd2usb: opening device succeeded", drvthis->name); } else { report(RPT_ERR, "%s/glcd2usb: no GLCD2USB device found", drvthis->name); goto err_out; } if (usb_set_configuration(handle, 1)) report(RPT_WARNING, "%s/glcd2usb: could not set configuration: %s", drvthis->name, usb_strerror()); /* * now try to claim the interface and detach the kernel HID driver on * Linux and other operating systems which support the call. */ while ((rval = usb_claim_interface(handle, 0)) != 0 && retries-- > 0) { #ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP if (usb_detach_kernel_driver_np(handle, 0) < 0) { report(RPT_WARNING, "%s/glcd2usb: could not detach kernel HID driver: %s", drvthis->name, usb_strerror()); } #endif } if (rval != 0) report(RPT_WARNING, "%s/glcd2usb: could not claim interface", drvthis->name); /* * Continue anyway, even if we could not claim the interface. Control * transfers should still work. */ ctd->device = handle; /* Query device */ memset(&(ctd->tx_buffer), 0, sizeof(ctd->tx_buffer)); len = sizeof(display_info_t); if ((err = usbGetReport(ctd->device, USB_HID_REPORT_TYPE_FEATURE, GLCD2USB_RID_GET_INFO, ctd->tx_buffer.bytes, &len)) != 0) { report(RPT_ERR, "%s/glcd2usb: query display parameters: %s", drvthis->name, usbErrorMessage(err)); goto err_out; } if (len < (int)sizeof(ctd->tx_buffer.display_info)) { report(RPT_ERR, "%s/glcd2usb: incomplete display info report (%d instead of %d)", drvthis->name, len, (int)sizeof(ctd->tx_buffer.display_info)); goto err_out; } if (!(ctd->tx_buffer.display_info.flags & FLAG_VERTICAL_UNITS)) { report(RPT_ERR, "%s/glcd2usb: unsupported display layout", drvthis->name); goto err_out; } if (ctd->tx_buffer.display_info.width > GLCD_MAX_WIDTH || ctd->tx_buffer.display_info.width <= 0 || ctd->tx_buffer.display_info.height > GLCD_MAX_HEIGHT || ctd->tx_buffer.display_info.height <= 0) { report(RPT_ERR, "%s/glcd2usb: display size out of range: %dx%d", drvthis->name, ctd->tx_buffer.display_info.width, ctd->tx_buffer.display_info.height); goto err_out; } p->framebuf.layout = FB_TYPE_VPAGED; p->framebuf.px_width = ctd->tx_buffer.display_info.width; p->framebuf.px_height = ctd->tx_buffer.display_info.height; p->framebuf.size = (p->framebuf.px_height + 7) / 8 * p->framebuf.px_width; report(RPT_INFO, "%s/glcd2usb: using display size %dx%d", drvthis->name, ctd->tx_buffer.display_info.width, ctd->tx_buffer.display_info.height); ctd->paged_buffer = malloc(p->framebuf.size); if (ctd->paged_buffer == NULL) { report(RPT_ERR, "%s/glcd2usb: cannot allocate memory", drvthis->name); goto err_out; } memset(ctd->paged_buffer, 0x55, p->framebuf.size); ctd->dirty_buffer = malloc(p->framebuf.size); if (ctd->dirty_buffer == NULL) { report(RPT_ERR, "%s/glcd2usb: cannot allocate memory", drvthis->name); goto err_out; } /* Allocate the display (turn off the 'whirl') */ ctd->tx_buffer.bytes[0] = GLCD2USB_RID_SET_ALLOC; ctd->tx_buffer.bytes[1] = 1; if ((err = usbSetReport(ctd->device, USB_HID_REPORT_TYPE_FEATURE, ctd->tx_buffer.bytes, 2)) != 0) { report(RPT_ERR, "%s/glcd2usb: Error allocating display: %s", drvthis->name, usbErrorMessage(err)); goto err_out; } return 0; err_out: glcd2usb_close(p); return -1; }
static int switch_dell(struct device_info *devinfo) { char report[] = { 0x7f, 0x13, 0x00, 0x00 }; struct usb_dev_handle *handle; int err; handle = usb_open(devinfo->dev); if (handle) { usb_claim_interface(handle, 0); usb_detach_kernel_driver_np(handle, 0); } err = usb_control_msg(handle, USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0x09, 0x7f | (0x03 << 8), 0, report, sizeof(report), 10000); if (err == 0) { err = -1; errno = EALREADY; } else { if (errno == ETIMEDOUT) err = 0; } usb_close(handle); return err; }