void dump_usbinfo(PTP_USB *ptp_usb) { struct usb_device *dev; #ifdef LIBUSB_HAS_GET_DRIVER_NP char devname[0x10]; int res; devname[0] = '\0'; res = usb_get_driver_np(ptp_usb->handle, (int) ptp_usb->interface, devname, sizeof(devname)); if (devname[0] != '\0') { printf(" Using kernel interface \"%s\"\n", devname); } #endif dev = usb_device(ptp_usb->handle); printf(" bcdUSB: %d\n", dev->descriptor.bcdUSB); printf(" bDeviceClass: %d\n", dev->descriptor.bDeviceClass); printf(" bDeviceSubClass: %d\n", dev->descriptor.bDeviceSubClass); printf(" bDeviceProtocol: %d\n", dev->descriptor.bDeviceProtocol); printf(" idVendor: %04x\n", dev->descriptor.idVendor); printf(" idProduct: %04x\n", dev->descriptor.idProduct); printf(" IN endpoint maxpacket: %d bytes\n", ptp_usb->inep_maxpacket); printf(" OUT endpoint maxpacket: %d bytes\n", ptp_usb->outep_maxpacket); printf(" Raw device info:\n"); printf(" Bus location: %d\n", ptp_usb->rawdevice.bus_location); printf(" Device number: %d\n", ptp_usb->rawdevice.devnum); printf(" Device entry info:\n"); printf(" Vendor: %s\n", ptp_usb->rawdevice.device_entry.vendor); printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.vendor_id); printf(" Product: %s\n", ptp_usb->rawdevice.device_entry.product); printf(" Vendor id: 0x%04x\n", ptp_usb->rawdevice.device_entry.product_id); printf(" Device flags: 0x%08x\n", ptp_usb->rawdevice.device_entry.device_flags); (void) probe_device_descriptor(dev, stdout); }
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; }
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 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; }
int Interface::driverName(std::string &driver) { int retval; char tmpString[256]; retval = usb_get_driver_np(m_parent->handle(), m_interfaceNumber, tmpString, sizeof(tmpString)); if (retval == 0) { std::string buf(tmpString); driver = buf; } return retval; }
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; }
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; }
/* USB::DevHandle#usb_get_driver_np(interface, name) */ static VALUE rusb_get_driver_np( VALUE v, VALUE vinterface, VALUE vname) { usb_dev_handle *p = get_usb_devhandle(v); int interface = NUM2INT(vinterface); char *name; int namelen; int ret; StringValue(vname); rb_str_modify(vname); name = RSTRING_PTR(vname); namelen = RSTRING_LEN(vname); ret = usb_get_driver_np(p, interface, name, namelen); check_usb_error("usb_get_driver_np", ret); return Qnil; }
static int read_one_sensor (struct usb_device *dev, uint16_t &value) { int ret; struct usb_dev_handle *devh; char driver_name[DRIVER_NAME_LEN] = ""; char usb_io_buf[USB_BUF_LEN] = "\x40\x68\x2a\x54" "\x52\x0a\x40\x40" "\x40\x40\x40\x40" "\x40\x40\x40\x40"; /* Open USB device. */ devh = usb_open (dev); if (! dev) { fprintf (stderr, "Failed to usb_open(%p)\n", (void *) dev); ret = -1; goto out; } /* Ensure that the device isn't claimed. */ ret = usb_get_driver_np (devh, 0/*intrf*/, driver_name, sizeof (driver_name)); if (! ret) { _log.Log(LOG_ERROR,"Voltcraft CO-20: Warning: device is claimed by driver %s, trying to unbind it.", driver_name); ret = usb_detach_kernel_driver_np (devh, 0/*intrf*/); if (ret) { _log.Log(LOG_ERROR,"Voltcraft CO-20: Warning: Failed to detatch kernel driver."); ret = -2; goto out; } } /* Claim device. */ ret = usb_claim_interface (devh, 0/*intrf*/); if (ret) { _log.Log(LOG_ERROR,"Voltcraft CO-20: usb_claim_interface() failed with error %d=%s", ret, strerror (-ret)); ret = -3; goto out; } /* Send query command. */ ret = usb_interrupt_write(devh, 0x0002/*endpoint*/, usb_io_buf, 0x10/*len*/, 1000/*msec*/); if (ret < 0) { _log.Log(LOG_ERROR,"Voltcraft CO-20: Failed to usb_interrupt_write() the initial buffer, ret = %d", ret); ret = -4; goto out_unlock; } /* Read answer. */ ret = usb_interrupt_read(devh, 0x0081/*endpoint*/, usb_io_buf, 0x10/*len*/, 1000/*msec*/); if (ret < 0) { _log.Log(LOG_ERROR,"Voltcraft CO-20: Failed to usb_interrupt_read() #1"); ret = -5; goto out_unlock; } /* On empty read, read again. */ if (ret == 0) { ret = usb_interrupt_read(devh, 0x0081/*endpoint*/, usb_io_buf, 0x10/*len*/, 1000/*msec*/); if (ret == 0) { //give up! goto out_unlock; } } /* Prepare value from first read. */ value = ((unsigned char *)usb_io_buf)[3] << 8 | ((unsigned char *)usb_io_buf)[2] << 0; /* Dummy read. */ usb_interrupt_read(devh, 0x0081/*endpoint*/, usb_io_buf, 0x10/*len*/, 1000/*msec*/); out_unlock: ret = usb_release_interface(devh, 0/*intrf*/); if (ret) { fprintf(stderr, "Failed to usb_release_interface()\n"); ret = -5; } out: if (devh) usb_close (devh); return ret; }
int main(int argc, char *argv[]) { int ret, vendor, product, debug, counter, one_read; int print_voc_only; struct usb_device *dev; char buf[1000]; // char str[5]; debug = 0; print_voc_only = 0; one_read = 0; vendor = 0x03eb; product = 0x2013; dev = NULL; while ((argc > 1) && (argv[1][0] == '-')) { switch (argv[1][1]) { case 'd': debug = 1; break; case 'v': print_voc_only = 1; break; case 'o': one_read = 1; break; case 'h': help(); } ++argv; --argc; } if (debug == 1) { printout("DEBUG: Active", 0); } if (debug == 1) { printout("DEBUG: Init USB", 0); } usb_init(); counter = 0; do { usb_set_debug(0); usb_find_busses(); usb_find_devices(); dev = find_device(vendor, product); sleep(1); if (dev == NULL) if (debug == 1) printout("DEBUG: No device found, wait 10sec...", 0); sleep(10); ++counter; if (counter == 10) { printout("Error: Device not found", 0); exit(1); } } while (dev == NULL); assert(dev); if (debug == 1) printout("DEBUG: USB device found", 0); devh = usb_open(dev); assert(devh); signal(SIGTERM, release_usb_device); ret = usb_get_driver_np(devh, 0, buf, sizeof(buf)); if (ret == 0) { ret = usb_detach_kernel_driver_np(devh, 0); } ret = usb_claim_interface(devh, 0); if (ret != 0) { printout("Error: claim failed with error: ", ret); exit(1); } unsigned short iresult=0; unsigned short voc=0; if (debug == 1) printout("DEBUG: Read any remaining data from USB", 0); ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000010, 1000); if (debug == 1) printout("DEBUG: Return code from USB read: ", ret); while(0==0) { time_t t = time(NULL); struct tm tm = *localtime(&t); // USB COMMAND TO REQUEST DATA // @h*TR if (debug == 1) printout("DEBUG: Write data to device", 0); memcpy(buf, "\x40\x68\x2a\x54\x52\x0a\x40\x40\x40\x40\x40\x40\x40\x40\x40\x40", 0x0000010); ret = usb_interrupt_write(devh, 0x00000002, buf, 0x0000010, 1000); if (debug == 1) printout("DEBUG: Return code from USB write: ", ret); if (debug == 1) printout("DEBUG: Read USB", 0); ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000010, 1000); if (debug == 1) printout("DEBUG: Return code from USB read: ", ret); if ( !((ret == 0) || (ret == 16))) { if (print_voc_only == 1) { printf("0\n"); } else { printout("ERROR: Invalid result code: ", ret); } } if (ret == 0) { if (debug == 1) printout("DEBUG: Read USB", 0); sleep(1); ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000010, 1000); if (debug == 1) printout("DEBUG: Return code from USB read: ", ret); } memcpy(&iresult,buf+2,2); voc = __le16_to_cpu(iresult); sleep(1); if (debug == 1) { printout("DEBUG: Read USB [flush]", 0); } ret = usb_interrupt_read(devh, 0x00000081, buf, 0x0000010, 1000); if (debug == 1) printout("DEBUG: Return code from USB read: ", ret); // According to AppliedSensor specifications the output range is between 450 and 2000 // So only printout values between this range if ( voc >= 450 && voc <= 6001) { if (print_voc_only == 1) { printf("%d\n", voc); } else { printf("%04d-%02d-%02d %02d:%02d:%02d, ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); printf("VOC: %d, RESULT: OK\n", voc); } } else { if (print_voc_only == 1) { printf("0\n"); } else { printf("%04d-%02d-%02d %02d:%02d:%02d, ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); printf("VOC: %d, RESULT: Error value out of range\n", voc); } } // If one read, then exit if (one_read == 1) exit(0); // Wait for next request for data sleep(10); } }
// rawhid_open - open 1 or more devices // // Inputs: // max = maximum number of devices to open // vid = Vendor ID, or -1 if any // pid = Product ID, or -1 if any // usage_page = top level usage page, or -1 if any // usage = top level usage number, or -1 if any // Output: // actual number of devices opened // int rawhid_open(int max, int vid, int pid, int usage_page, int usage) { struct usb_bus *bus; struct usb_device *dev; struct usb_interface *iface; struct usb_interface_descriptor *desc; struct usb_endpoint_descriptor *ep; usb_dev_handle *u; uint8_t buf[1024], *p; int i, n, len, tag, ep_in, ep_out, count=0, claimed; uint32_t val=0, parsed_usage, parsed_usage_page; hid_t *hid; if (first_hid) free_all_hid(); printf("rawhid_open, max=%d\n", max); if (max < 1) return 0; usb_init(); usb_find_busses(); usb_find_devices(); for (bus = usb_get_busses(); bus; bus = bus->next) { for (dev = bus->devices; dev; dev = dev->next) { if (vid > 0 && dev->descriptor.idVendor != vid) continue; if (pid > 0 && dev->descriptor.idProduct != pid) continue; if (!dev->config) continue; if (dev->config->bNumInterfaces < 1) continue; printf("device: vid=%04X, pic=%04X, with %d iface\n", dev->descriptor.idVendor, dev->descriptor.idProduct, dev->config->bNumInterfaces); iface = dev->config->interface; u = NULL; claimed = 0; for (i=0; i<dev->config->bNumInterfaces && iface; i++, iface++) { desc = iface->altsetting; if (!desc) continue; printf(" type %d, %d, %d\n", desc->bInterfaceClass, desc->bInterfaceSubClass, desc->bInterfaceProtocol); if (desc->bInterfaceClass != 3) continue; if (desc->bInterfaceSubClass != 0) continue; if (desc->bInterfaceProtocol != 0) continue; ep = desc->endpoint; ep_in = ep_out = 0; for (n = 0; n < desc->bNumEndpoints; n++, ep++) { if (ep->bEndpointAddress & 0x80) { if (!ep_in) ep_in = ep->bEndpointAddress & 0x7F; printf(" IN endpoint %d\n", ep_in); } else { if (!ep_out) ep_out = ep->bEndpointAddress; printf(" OUT endpoint %d\n", ep_out); } } if (!ep_in) continue; if (!u) { u = usb_open(dev); if (!u) { printf(" unable to open device\n"); break; } } printf(" hid interface (generic)\n"); if (usb_get_driver_np(u, i, (char *)buf, sizeof(buf)) >= 0) { printf(" in use by driver \"%s\"\n", buf); if (usb_detach_kernel_driver_np(u, i) < 0) { printf(" unable to detach from kernel\n"); continue; } } if (usb_claim_interface(u, i) < 0) { printf(" unable claim interface %d\n", i); continue; } len = usb_control_msg(u, 0x81, 6, 0x2200, i, (char *)buf, sizeof(buf), 250); printf(" descriptor, len=%d\n", len); if (len < 2) { usb_release_interface(u, i); continue; } p = buf; parsed_usage_page = parsed_usage = 0; while ((tag = hid_parse_item(&val, &p, buf + len)) >= 0) { printf(" tag: %X, val %X\n", tag, val); if (tag == 4) parsed_usage_page = val; if (tag == 8) parsed_usage = val; if (parsed_usage_page && parsed_usage) break; } if ((!parsed_usage_page) || (!parsed_usage) || (usage_page > 0 && parsed_usage_page != usage_page) || (usage > 0 && parsed_usage != usage)) { usb_release_interface(u, i); continue; } hid = (struct hid_struct *)malloc(sizeof(struct hid_struct)); if (!hid) { usb_release_interface(u, i); continue; } hid->usb = u; hid->iface = i; hid->ep_in = ep_in; hid->ep_out = ep_out; hid->open = 1; add_hid(hid); claimed++; count++; if (count >= max) return count; } if (u && !claimed) usb_close(u); } } return count; }
int main(void) { struct usb_device *dev = NULL; usb_dev_handle *devh = NULL; bool is_detached = false; bool is_claimed = false; usb_init(); signal(SIGINT, dummy_sigint_handler); signal(SIGTERM, dummy_sigint_handler); signal(SIGHUP, dummy_sigint_handler); signal(SIGUSR1, dummy_sigint_handler); signal(SIGUSR2, dummy_sigint_handler); while (dummy_is_running) { const int read_buf_size = 32; char read_buf[read_buf_size]; int read_size; usleep(DUMMY_LOOP_INTERVAL); usb_find_busses(); usb_find_devices(); if (!dev) { dev = dummy_find_smartboard(); continue; } if (!devh) { devh = usb_open(dev); continue; } if (!is_detached) { const int driver_name_size = 256; char driver_name[driver_name_size]; /* Assume the driver is detached if the driver's * name cannot be found. */ is_detached = usb_get_driver_np(devh, DUMMY_SB_IFACE, driver_name, driver_name_size); is_detached = is_detached || !usb_detach_kernel_driver_np(devh, DUMMY_SB_IFACE); if (!is_detached) fprintf(stderr, "error: libusb: %s\n", usb_strerror()); continue; } if (!is_claimed) { is_claimed = !usb_claim_interface(devh, DUMMY_SB_IFACE); if (!is_claimed) fprintf(stderr, "error: libusb: %s\n", usb_strerror()); continue; } do { read_size = usb_interrupt_read(devh, DUMMY_SB_EP_READ, read_buf, read_buf_size, DUMMY_SB_TIMEOUT_READ); if (read_size == -ETIMEDOUT) { break; } else if (read_size < 0) { fprintf(stderr, "error: libusb: %d %s\n", read_size, usb_strerror()); break; } else { char *hexstr; hexstr = dummy_aget_hexstr(read_buf, read_size); printf("IN: %s\n", hexstr); free(hexstr); } } while (read_size >= 0); } if (devh && is_claimed && usb_release_interface(devh, DUMMY_SB_IFACE)) fprintf(stderr, "error: libusb: %s\n", usb_strerror()); if (devh && usb_close(devh)) fprintf(stderr, "error: libusb: %s\n", usb_strerror()); printf("GracefulShutdown™!\n"); return 0; }
void garmin_usb_start(struct usb_device *dev, libusb_unit_data *lud) { int i; if (udev) return; udev = usb_open(dev); atexit(gusb_atexit_teardown); if (!udev) { fatal("usb_open failed: %s\n", usb_strerror()); } /* * Hrmph. No iManufacturer or iProduct headers.... */ #if 0 if (usb_set_configuration(udev, 1) < 0) { #if __linux__ char drvnm[128]; drvnm[0] = 0; /* * Most Linux distributions ship a slightly broken * kernel driver that bonds with the hardware. */ usb_get_driver_np(udev, 0, drvnm, sizeof(drvnm)-1); fatal("usb_set_configuration failed, probably because kernel driver '%s'\n is blocking our access to the USB device.\n" "For more information see http://www.gpsbabel.org/os/Linux_Hotplug.html\n", drvnm); #else fatal("usb_set_configuration failed: %s\n", usb_strerror()); #endif } #endif if (usb_claim_interface(udev, 0) < 0) { fatal("Claim interfaced failed: %s\n", usb_strerror()); } libusb_llops.max_tx_size = dev->descriptor.bMaxPacketSize0; /* * About 5% of the time on OS/X (Observed on 10.5.4 on Intel Imac * with Venture HC) we get a dev with a valid vendor ID, but no * associated configuration. I was unable to see a single instance * of this on a 276, a 60CS, a 60CSx, an SP310, or an Edge 305, leading * me to think this is some kind of bug in the Venture HC. * * Rather than crash, we at least print * a nastygram. Experiments with retrying various USB ops brought * no joy, so just call fatal and move on. */ if (!dev->config) { fatal("Found USB device with no configuration.\n"); } for (i = 0; i < dev->config->interface->altsetting->bNumEndpoints; i++) { struct usb_endpoint_descriptor * ep; ep = &dev->config->interface->altsetting->endpoint[i]; switch (ep->bmAttributes & USB_ENDPOINT_TYPE_MASK) { #define EA(x) x & USB_ENDPOINT_ADDRESS_MASK case USB_ENDPOINT_TYPE_BULK: if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) gusb_bulk_in_ep = EA(ep->bEndpointAddress); else gusb_bulk_out_ep = EA(ep->bEndpointAddress); break; case USB_ENDPOINT_TYPE_INTERRUPT: if (ep->bEndpointAddress & USB_ENDPOINT_DIR_MASK) gusb_intr_in_ep = EA(ep->bEndpointAddress); break; } } /* * Zero is the configuration endpoint, so if we made it through * that loop without non-zero values for all three, we're hosed. */ if (gusb_intr_in_ep && gusb_bulk_in_ep && gusb_bulk_out_ep) { lud->product_id = gusb_reset_toggles(); switch (lud->product_id) { // Search for "Venture HC" for more on this siliness.. // It's a case instead of an 'if' becuase I have a // feeling there are more affected models either // on the market or on the way. case 695: break; default: gusb_syncup(); } return; } fatal("Could not identify endpoints on USB device.\n" "Found endpoints Intr In 0x%x Bulk Out 0x%x Bulk In %0xx\n", gusb_intr_in_ep, gusb_bulk_out_ep, gusb_bulk_in_ep); }
pickit_dev *usbPickitOpen(int unitIndex, char *unitID) { int result, unitNumber; struct usb_device *device; struct usb_device *usb_devices; struct usb_bus *bus; usb_dev_handle *d = NULL; char unitIDSerial[64]; byte retData[reqLen + 1]; #ifdef LINUX int retval; char dname[32] = {0}; #ifndef USE_DETACH char syscmd[64]; #endif #endif #ifndef USB_HOTPLUG if (geteuid() != 0) return NULL; #endif #ifdef LINUX if (usbdebug & USB_DEBUG_DRIVER) usb_set_debug(255); #endif if (verbose) { printf("\nLocating USB Microchip PICkit2 (vendor 0x%04x/product 0x%04x)\n", pickit_vendorID, pickit_productID); fflush(stdout); } usb_init(); result = usb_find_busses(); if ((result < 0) && verbose) { printf("Error finding USB busses: %d\n", result); fflush(stdout); } result = usb_find_devices(); if ((result < 0) && verbose) { printf("Error finding USB devices: %d\n", result); fflush(stdout); } unitNumber = 0; for (bus = usb_get_busses(); bus != NULL; bus = bus->next) { usb_devices = bus->devices; for (device = usb_devices; device != NULL; device = device->next) { if (device->descriptor.idVendor == pickit_vendorID &&device->descriptor.idProduct == pickit_productID) { if (unitIndex == unitNumber) { if (verbose) { printf( "Found USB PICkit as device '%s' on USB bus %s\n", device->filename, device->bus->dirname); fflush(stdout); } unitID[0] = '-'; unitID[1] = 0; d = usb_open(device); deviceHandle = d; if (device->descriptor.iSerialNumber > 0) { usb_get_string_simple(d, device->descriptor.iSerialNumber, unitIDSerial, 64); if (unitIDSerial[0] && (unitIDSerial[0] != 9)) { strcpy(unitID, unitIDSerial); unitID[14] = 0; // ensure termination after 14 characters } } if (d) { // This is our device #ifdef LINUX { retval = usb_get_driver_np(d, 0, dname, 31); #ifndef USE_DETACH if (!retval) { strcpy(syscmd, "rmmod "); strcat(syscmd, dname); // printf("removing driver module: %s\n", syscmd); system(syscmd); } #else if (!retval) usb_detach_kernel_driver_np(d, 0); #endif } #endif #ifdef CLAIM_USB if (usb_set_configuration(d, CONFIG_VENDOR) < 0) // if config fails with CONFIG_VENDOR, { if (usb_set_configuration(d, CONFIG_HID) < 0) // it may be in bootloader, try CONFIG_HID { if (verbose) { printf("Error setting USB configuration.\n"); fflush(stdout); } return NULL; } } if (usb_claim_interface(d, pickit_interface)) { if (verbose) { printf("Claim failed-- the USB PICkit2 is in use by another driver.\n" "Do a `dmesg` to see which kernel driver has claimed it--\n" "You may need to `rmmod hid` or patch your kernel's hid driver.\n"); fflush(stdout); } return NULL; } #endif cmd[0] = GETVERSION; sendPickitCmd(d, cmd, 1); recvUSB(d, 8, retData); if (retData[5] == 'B') { if (verbose) { printf("Communication established. PICkit2 bootloader firmware version is %d.%d\n\n", (int) retData[6], (int) retData[7]); fflush(stdout); } pickit2mode = BOOTLOAD_MODE; pickit2firmware = (((int) retData[6]) << 8) | (((int) retData[7]) & 0xff); } else { if (verbose) { printf("Communication established. PICkit2 firmware version is %d.%d.%d\n\n", (int) retData[0], (int) retData[1], (int) retData[2]); fflush(stdout); } pickit2mode = NORMAL_MODE; pickit2firmware = (((int) retData[0]) << 16) | ((((int) retData[1]) << 8) & 0xff00) | (((int) retData[2]) & 0xff); } return d; } else { if (verbose) { printf("Open failed for USB device\n"); fflush(stdout); } return NULL; } } else { // not the unit we're looking for unitNumber++; } } // else some other vendor's device -- keep looking... } } if (verbose) { printf("Could not find PICkit2 programmer--\n" "you might try lsusb to see if it's actually there.\n"); fflush(stdout); } return NULL; }
static int open_interface(struct olimex_transport *tr, struct usb_device *dev, int ino) { #if defined(__linux__) int drv; char drName[256]; #endif printc(__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 WIN32 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; } int ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_IFC_ENABLE, 0x1, 0, NULL, 0, 300); #ifdef DEBUG_OLIMEX printc(__FILE__": %s : Sending control message ret %d\n", __FUNCTION__, ret); #endif /* Set the baud rate to 500000 bps */ ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_SET_BAUDDIV, 0x7, 0, NULL, 0, 300); #ifdef DEBUG_OLIMEX printc(__FILE__": %s : Sending control message ret %d\n", __FUNCTION__, ret); #endif /* Set the modem control settings. * Set RTS, DTR and WRITE_DTR, WRITE_RTS */ ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE, CP210X_SET_MHS, 0x303, 0, NULL, 0, 300); #ifdef DEBUG_OLIMEX printc(__FILE__": %s : Sending control message ret %d\n", __FUNCTION__, ret); #endif return 0; }
static int probe_device_descriptor(struct usb_device *dev, FILE *dumpfile) { usb_dev_handle *devh; unsigned char buf[1024], cmd; int i; int ret; if (dev->descriptor.bDeviceClass == USB_CLASS_HUB) { return 0; } devh = usb_open(dev); if (devh == NULL) { return 0; } if (dev->config) { for (i = 0; i < dev->descriptor.bNumConfigurations; i++) { uint8_t j; for (j = 0; j < dev->config[i].bNumInterfaces; j++) { int k; for (k = 0; k < dev->config[i].interface[j].num_altsetting; k++) { struct usb_interface_descriptor *intf = &dev->config[i].interface[j].altsetting[k]; buf[0] = '\0'; ret = usb_get_string_simple(devh, dev->config[i].interface[j].altsetting[k].iInterface, (char *) buf, 1024); if (ret < 3) continue; if (strcmp((char *) buf, "MTP") == 0) { if (dumpfile != NULL) { fprintf(dumpfile, "Configuration %d, interface %d, altsetting %d:\n", i, j, k); fprintf(dumpfile, " Interface description contains the string \"MTP\"\n"); fprintf(dumpfile, " Device recognized as MTP, no further probing.\n"); } usb_close(devh); return 1; } #ifdef LIBUSB_HAS_GET_DRIVER_NP { char devname[0x10]; devname[0] = '\0'; ret = usb_get_driver_np(devh, dev->config[i].interface[j].altsetting[k].iInterface, devname, sizeof(devname)); if (devname[0] != '\0' && strcmp(devname, "usb-storage")) { printf("avoid probing device using kernel interface \"%s\"\n", devname); return 0; } } #endif } } } } else { if (dev->descriptor.bNumConfigurations) printf("dev->config is NULL in probe_device_descriptor yet dev->descriptor.bNumConfigurations > 0\n"); } ret = usb_get_descriptor(devh, 0x03, 0xee, buf, sizeof(buf)); if (dumpfile != NULL && ret > 0) { fprintf(dumpfile, "Microsoft device descriptor 0xee:\n"); data_dump_ascii(dumpfile, buf, ret, 16); } if (ret < 10) { usb_close(devh); return 0; } if (!((buf[2] == 'M') && (buf[4] == 'S') && (buf[6] == 'F') && (buf[8] == 'T'))) { usb_close(devh); return 0; } cmd = buf[16]; ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, cmd, 0, 4, (char *) buf, sizeof(buf), USB_TIMEOUT_DEFAULT); if (dumpfile != NULL && ret > 0) { fprintf(dumpfile, "Microsoft device response to control message 1, CMD 0x%02x:\n", cmd); data_dump_ascii(dumpfile, buf, ret, 16); } if (ret <= 0x15) { usb_close(devh); return 0; } if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { usb_close(devh); return 0; } ret = usb_control_msg (devh, USB_ENDPOINT_IN|USB_RECIP_DEVICE|USB_TYPE_VENDOR, cmd, 0, 5, (char *) buf, sizeof(buf), USB_TIMEOUT_DEFAULT); if (dumpfile != NULL && ret > 0) { fprintf(dumpfile, "Microsoft device response to control message 2, CMD 0x%02x:\n", cmd); data_dump_ascii(dumpfile, buf, ret, 16); } if (ret == -1) { fprintf(stderr, "Potential MTP Device with VendorID:%04x and " "ProductID:%04x encountered an error responding to " "control message 2.\n" "Problems may arrise but continuing\n", dev->descriptor.idVendor, dev->descriptor.idProduct); } else if (ret <= 0x15) { fprintf(stderr, "Potential MTP Device with VendorID:%04x and " "ProductID:%04x responded to control message 2 with a " "response that was too short. Problems may arrise but " "continuing\n", dev->descriptor.idVendor, dev->descriptor.idProduct); } else if ((buf[0x12] != 'M') || (buf[0x13] != 'T') || (buf[0x14] != 'P')) { fprintf(stderr, "Potential MTP Device with VendorID:%04x and " "ProductID:%04x encountered an error responding to " "control message 2\n" "Problems may arrise but continuing\n", dev->descriptor.idVendor, dev->descriptor.idProduct); } usb_close(devh); return 1; }
/** * initialize hardware */ static NftResult _usb_init(void *privdata, const char *id) { Niftylino *n = privdata; struct usb_bus *bus; struct usb_device *dev; struct usb_dev_handle *h; /* save id */ strncpy(n->id, id, sizeof(n->id)); /* get our chain */ LedChain *chain = led_hardware_get_chain(n->hw); /* pixel-format of our chain */ LedPixelFormat *format = led_chain_get_format(chain); /* pixelformat supported? */ NiftylinoValueWidth vw; switch (led_pixel_format_get_bytes_per_pixel(format) / led_pixel_format_get_n_components(format)) { /* 8 bit values */ case 1: { vw = NIFTYLINO_8BIT_VALUES; break; } /* 16 bit values */ case 2: { vw = NIFTYLINO_16BIT_VALUES; break; } /* unsupported format */ default: { NFT_LOG(L_ERROR, "Unsupported format requested: %d bytes-per-pixel, %d components-per-pixel", led_pixel_format_get_bytes_per_pixel(format), led_pixel_format_get_n_components(format)); return NFT_FAILURE; } } /* find (new) busses */ usb_find_busses(); /* find (new) devices */ usb_find_devices(); /* open niftylino usb-device */ char serial[255]; /* walk all busses */ for(bus = usb_get_busses(); bus; bus = bus->next) { /* walk all devices on bus */ for(dev = bus->devices; dev; dev = dev->next) { /* found niftylino? */ if((dev->descriptor.idVendor != VENDOR_ID) || (dev->descriptor.idProduct != PRODUCT_ID)) { continue; } /* try to open */ if(!(h = usb_open(dev))) /* device allready open or other error */ continue; /* interface already claimed by driver? */ char driver[1024]; if(!(usb_get_driver_np(h, 0, driver, sizeof(driver)))) { // NFT_LOG(L_ERROR, "Device already claimed by // \"%s\"", driver); continue; } /* reset device */ usb_reset(h); usb_close(h); // ~ if(usb_reset(h) < 0) // ~ { // ~ /* reset failed */ // ~ usb_close(h); // ~ continue; // ~ } /* re-open */ if(!(h = usb_open(dev))) /* device allready open or other error */ continue; /* clear any previous halt status */ // usb_clear_halt(h, 0); /* claim interface */ if(usb_claim_interface(h, 0) < 0) { /* device claim failed */ usb_close(h); continue; } /* receive string-descriptor (serial number) */ if(usb_get_string_simple(h, 3, serial, sizeof(serial)) < 0) { usb_release_interface(h, 0); usb_close(h); continue; } /* device id == requested id? (or wildcard id * requested? */ if(strlen(n->id) == 0 || strncmp(n->id, serial, sizeof(serial)) == 0 || strncmp(n->id, "*", 1) == 0) { /* serial-number... */ strncpy(n->id, serial, sizeof(n->id)); /* usb device handle */ n->usb_handle = h; /* set format */ NFT_LOG(L_INFO, "Setting bitwidth to %d bit", (vw == NIFTYLINO_8BIT_VALUES ? 8 : 16)); if(!_set_format(privdata, vw)) { NFT_LOG(L_ERROR, "Failed to set greyscale format to %s.", vw == NIFTYLINO_8BIT_VALUES ? "u8" : "u16"); return NFT_FAILURE; } return NFT_SUCCESS; } /* close this adapter */ usb_release_interface(h, 0); usb_close(h); } } return NFT_FAILURE; }
/** * API: Initialize the connection type driver. * \param drvthis Pointer to driver structure. * \retval 0 Success. * \retval <0 Error. */ int glcd_picolcdgfx_init(Driver *drvthis) { PrivateData *p = (PrivateData *) drvthis->private_data; CT_picolcdgfx_data *ct_data; struct usb_bus *busses, *bus; struct usb_device *dev; char driver[1024]; char product[1024]; char manufacturer[1024]; char serialnumber[1024]; int ret; report(RPT_INFO, "GLCD/picolcdgfx: intializing"); /* Set up connection type low-level functions */ p->glcd_functions->blit = glcd_picolcdgfx_blit; p->glcd_functions->close = glcd_picolcdgfx_close; p->glcd_functions->poll_keys = glcd_picolcdgfx_pollkeys; p->glcd_functions->set_backlight = glcd_picolcdgfx_set_backlight; p->glcd_functions->set_contrast = glcd_picolcdgfx_set_contrast; /* Allocate memory structures */ ct_data = (CT_picolcdgfx_data *) calloc(1, sizeof(CT_picolcdgfx_data)); if (ct_data == NULL) { report(RPT_ERR, "GLCD/picolcdgfx: error allocating connection data"); return -1; } p->ct_data = ct_data; /* Fix display size to 256x64 */ p->framebuf.layout = FB_TYPE_VPAGED; p->framebuf.px_width = PICOLCDGFX_WIDTH; p->framebuf.px_height = PICOLCDGFX_HEIGHT; /* Since the display is fixed to 256x64 we have to recalculate. */ p->framebuf.size = (PICOLCDGFX_HEIGHT / 8) * PICOLCDGFX_WIDTH; ct_data->backingstore = malloc(p->framebuf.size); if (ct_data->backingstore == NULL) { report(RPT_ERR, "GLCD/picolcdgfx: unable to allocate backing store"); return -1; } /* framebuf is initialized with 0x00 so initialize the backingstore with * 0xFF so the first call to _blit will draw the entire screen. * */ memset(ct_data->backingstore, 0xFF, p->framebuf.size); /* Get key timeout */ ct_data->keytimeout = drvthis->config_get_int(drvthis->name, "picolcdgfx_KeyTimeout", 0, PICOLCDGFX_DEF_KEYTIMEOUT); /* Get inverted option */ if (drvthis->config_get_bool(drvthis->name, "picolcdgfx_Inverted", 0, PICOLCDGFX_DEF_INVERTED)) ct_data->inverted = 0xFF; else ct_data->inverted = 0; ct_data->lcd = NULL; report(RPT_DEBUG, "GLCD/picolcdgfx: scanning for picoLCD 256x64..."); 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 == picoLCDGfx_VENDOR) && (dev->descriptor.idProduct == picoLCDGfx_DEVICE)) { report(RPT_DEBUG, "GLCD/picolcdgfx: found picoLCDGraphics on bus %s device %s", bus->dirname, dev->filename); ct_data->lcd = usb_open(dev); ret = usb_get_driver_np(ct_data->lcd, 0, driver, sizeof(driver)); if (ret == 0) { report(RPT_DEBUG, "GLCD/picolcdgfx: interface 0 already claimed by '%s'", driver); report(RPT_DEBUG, "GLCD/picolcdgfx: attempting to detach driver..."); if (usb_detach_kernel_driver_np(ct_data->lcd, 0) < 0) { report(RPT_ERR, "GLCD/picolcdgfx: usb_detach_kernel_driver_np() failed!"); return -1; } } usb_set_configuration(ct_data->lcd, 1); usleep(100); if (usb_claim_interface(ct_data->lcd, 0) < 0) { report(RPT_ERR, "GLCD/picolcdgfx: usb_claim_interface() failed!"); return -1; } usb_set_altinterface(ct_data->lcd, 0); usb_get_string_simple(ct_data->lcd, dev->descriptor.iProduct, product, sizeof(product)); usb_get_string_simple(ct_data->lcd, dev->descriptor.iManufacturer, manufacturer, sizeof(manufacturer)); usb_get_string_simple(ct_data->lcd, dev->descriptor.iSerialNumber, serialnumber, sizeof(serialnumber)); report(RPT_INFO, "GLCD/picolcdgfx: Manufacturer='%s' Product='%s' SerialNumber='%s'", manufacturer, product, serialnumber); debug(RPT_DEBUG, "GLCD/picolcdgfx: init() done"); return 0; } } } report(RPT_ERR, "GLCD/picolcdgfx: could not find a picoLCDGraphics"); return -1; }
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 device_init(void) { struct usb_bus *usb_bus; struct usb_device *dev; usb_init(); int n=0; usb_find_busses(); usb_find_devices(); ret = hid_init(); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_init failed with return code %d \n", ret); return 1; } for (usb_bus = usb_busses; usb_bus; usb_bus = usb_bus->next) { for (dev = usb_bus->devices; dev; dev = dev->next) { if ((dev->descriptor.idVendor == VENDOR_ID) && (dev->descriptor.idProduct == PRODUCT_ID)) { /////////////////////////////////////////////// n++; usb_handle = usb_open(dev); int drstatus = usb_get_driver_np(usb_handle, 0, kdname,sizeof(kdname)); if (kdname != NULL && strlen(kdname) > 0) usb_detach_kernel_driver_np(usb_handle, 0); usb_reset(usb_handle); usb_close(usb_handle); HIDInterfaceMatcher matcher = { VENDOR_ID, PRODUCT_ID, NULL, NULL, 0 }; if (n==1) { hid1 = hid_new_HIDInterface(); if (hid1 != 0) { ret = hid_force_open(hid1, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); } } ////////////////////////////////////////////// } else // n=2 { hid2 = hid_new_HIDInterface(); if (hid2 != 0) { ret = hid_force_open(hid2, 0, &matcher, 3); if (ret != HID_RET_SUCCESS) { fprintf(stderr, "hid_force_open failed with return code %d\n", ret); } ////////////////////////////////////////////// } } } } } return 0; }