From: Kumar, S. <san...@hp...> - 2013-06-20 06:26:25
|
#include <unistd.h> #include <errno.h> #include <stdio.h> #include<stdlib.h> #include <string.h> #include <libusb-1.0/libusb.h> #include <error.h> #include <syslog.h> #define LIBUSB_TIMEOUT 30000 /* milliseconds */ #define LIBUSB_CONTROL_REQ_TIMEOUT 5000 #define DBG(args...) printf(args) #define INFO(args...) printf(args) #define BUG(args...) printf(args) static int get_ep(libusb_device *dev, int config, int interface, int altset, enum libusb_transfer_type type, enum libusb_endpoint_direction epdir) { struct libusb_config_descriptor *confptr = NULL; const struct libusb_interface_descriptor *pi; int i, endpoint = -1; if (libusb_get_config_descriptor(dev, config, &confptr) != 0) goto bugout; if (confptr == NULL || confptr->interface == NULL || confptr->interface[interface].altsetting == NULL) goto bugout; pi = &(confptr->interface[interface].altsetting[altset]); for (i=0; i<pi->bNumEndpoints; i++) { if (pi->endpoint == NULL) goto bugout; if (pi->endpoint[i].bmAttributes == type) { if (epdir == LIBUSB_ENDPOINT_IN) { if (pi->endpoint[i].bEndpointAddress & LIBUSB_ENDPOINT_IN) { endpoint = pi->endpoint[i].bEndpointAddress; break; } } else if (epdir == LIBUSB_ENDPOINT_OUT) { if (!(pi->endpoint[i].bEndpointAddress & LIBUSB_ENDPOINT_IN)) { endpoint = pi->endpoint[i].bEndpointAddress; break; } } } } bugout: libusb_free_config_descriptor(confptr); if (endpoint == -1) DBG("get_ep: ERROR! returning -1\n"); return endpoint; /* no endpoint found */ } static int get_out_ep(libusb_device *dev, int config, int interface, int altset, enum libusb_transfer_type type) { return get_ep(dev, config, interface, altset, type, LIBUSB_ENDPOINT_OUT); } /* Detach any kernel module that may have claimed specified inteface. */ static int detach(libusb_device_handle *hd, int interface) { int ret ; /* If any kernel module has claimed this interface, detach it. */ ret = libusb_kernel_driver_active (hd, interface); DBG("Active kernel driver on interface=%d ret=%d (%m)\n", interface, ret); if (ret == 1) { ret = libusb_detach_kernel_driver(hd, interface); DBG("Detaching kernel driver on interface=%d ret=%d (%m)\n", interface, ret); if (ret < 0) BUG("could not remove kernel driver interface=%d, retvalue = %d: %m\n", interface,ret); } ret = libusb_kernel_driver_active (hd, interface); DBG("Varify Active kernel driver on interface=%d ret=%d (%m)...............\n", interface, ret); return 0; } int main(int argc, char **argv) { libusb_context *ctx = NULL; libusb_device **list; /*List of connected USB devices */ libusb_device *dev = NULL; /* Current device */ struct libusb_device_descriptor devdesc; /* Current device descriptor */ struct libusb_config_descriptor *confptr = NULL; /* Pointer to current configuration */ libusb_device_handle *hd = NULL; int i, j = 0,r, conf, numdevs = 0,status; /* number of connected devices */ char imanufact[128] = {0,} , iproduct[128] = {0,}, iserial[128] = {0,}; int num = 0; char string[128] = {0,}; char buffer[1024]={0}; int maxSize = 1024,size = 1024, rlen=-4, interfacenum=0; libusb_init(&ctx); libusb_set_debug(ctx,2); numdevs = libusb_get_device_list(ctx, &list); if (numdevs <= 0) goto bugout; for (i = 0; i < numdevs; i++) { dev = list[i]; libusb_get_device_descriptor (dev, &devdesc); if (!devdesc.bNumConfigurations || !devdesc.idVendor || !devdesc.idProduct) continue; if(devdesc.idVendor != 0x3f0) /*Not a HP device */ continue; for (conf = 0; conf < devdesc.bNumConfigurations; conf++) { if (libusb_get_config_descriptor (dev, conf, &confptr) < 0) continue; if (confptr->interface[0].altsetting[0].bInterfaceClass != LIBUSB_CLASS_MASS_STORAGE) { libusb_open(dev, &hd); if (hd == NULL) { BUG("Invalid usb_open: %m\n"); continue; } detach(hd, 0); DBG("\n"); rlen = libusb_claim_interface(hd, 0); if (rlen) DBG("invalid claim_interface %d: (%m)\n",0); else DBG("claimed interface : %d (%m)\n",interfacenum); rlen = libusb_control_transfer(hd, LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE , /* bmRequestType */ LIBUSB_REQUEST_GET_STATUS, /* bRequest */ 0, /* wValue */ 0, /* wIndex interface */ /* note firmware does not follow the USB Printer Class specification for wIndex */ buffer, maxSize, LIBUSB_CONTROL_REQ_TIMEOUT); if (rlen < 0) BUG("invalid deviceid ret=%d: (%m)\n\n", rlen); else BUG("Valid deviceid ret=%d: (%m)\n\n", rlen); rlen = ntohs(*(short *)buffer); if (rlen > (size-1)) rlen = size-1; /* leave byte for zero termination */ if (rlen > 2) rlen -= 2; memcpy(buffer, buffer+2, rlen); /* remove length */ buffer[rlen]=0; DBG("read actual device_id successfully fd=%d len=%d (%m)\n\n", 1, rlen); BUG("device ID:%s: \n\n", buffer); libusb_free_config_descriptor(confptr); confptr = NULL; } } } bugout: if (hd) libusb_close(hd); if (confptr) libusb_free_config_descriptor(confptr); libusb_free_device_list(list, 1); libusb_exit(ctx); return 0; } |