Saturday 1 February 2014

Programmatically obtaining the vendor ID, product ID of a USB device on a Linux platform

I have been trying to write a simple device driver, in which I am suppossed to get the Vendor ID and Product ID programmatically. Having gone through almost all the necessary header files, I have come to a conclusion that I can access the vendor ID, product ID, and manufacturer details of the USB device through a structure: struct usb_device{} which has a member struct usb_device_descriptor{}. This nested structure has idVendor, idProduct andiManufacturer and some other members.
But somehow, for some reason I am unable to access these members, so when I do a dmesg after inserting my module, it prints some garbage values. I would be glad to receive help or hints or any response. Following is the code that I have written so far:
P.S.: Necessary inclusions have been made.
Having gone through almost all the necessary header files, I know that I can access the vendor ID, product ID, and manufacturer details of the USB device through a structure: struct usb_device{}which has a member struct usb_device_descriptor{}. This nested structure has idVendor, idProduct and iManufacturer and some other members.
//***********************
struct usb_device udev;

struct usb_bus *bus;
ssize_t ret;

static int __init usb_fun_init(void)
{
int result;
__le16 idVendor
= 0;
__le16 idProduct
= 0;
__u8 iManufacturer
= 0;

printk
(KERN_INFO "\n************************************ in init\n");
list_for_each_entry
(bus, &usb_bus_list, bus_list){

printk
(KERN_INFO "***************** Begins ****************");
printk
(KERN_INFO "\nVendor ID = %d", udev.descriptor.idVendor);
printk
(KERN_INFO "\nProduct ID = %d", udev.descriptor.idProduct);
printk
(KERN_INFO "\nManufacturer = %s", udev.descriptor.iManufacturer);

return 0;
}

static int __exit usb_fun_exit(void)
{
printk
(KERN_INFO "\n************************************ in exit\n");
}

module_init
(usb_fun_init);
module_exit
(usb_fun_exit);

MODULE_LICENSE
("GPL");

I guess, the above is the full code for your kernel module. Anyway you are using correct structs and vendor ID, Device ID will be available in device descriptor. Refer for more details about descriptors.


I suggest you to refer kernel code here.


Update 1:




The following program will give you information about HUBs available in system. usb_hub_for_each_child macro is not supported in 3.2.0 kernel version, but supported in latest 3.7.x versions.


usb_bus_list is declared in #include <linux/usb/hcd.h>.

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/list.h>

MODULE_LICENSE
("GPL");

int ourinitmodule(void)
{

int chix = 0;
struct usb_device *dev, *childdev = NULL;
struct usb_bus *bus = NULL;

list_for_each_entry
(bus, &usb_bus_list, bus_list)
{
printk
("\n USB Bus : %d", bus->busnum);

dev
= bus->root_hub;

printk
("\n Vendor Id:%x, Product Id:%x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
#if 0 //usb_hub_for_each_child macro not supported in 3.2.0, so trying with 3.7.6.
usb_hub_for_each_child
(dev, chix, childdev)
{
if(childdev)
{
printk
("\n Vendor Id:%x, Product Id:%x\n", childdev->descriptor.idVendor, childdev->descriptor.idProduct);
}
}
#endif

}

printk
(KERN_ALERT "\n Hello Jay, Welcome to sample application.... \n");

return 0;
}

void ourcleanupmodule(void)
{
printk
(KERN_ALERT "\n Hello Jay, Thanks....Exiting Application. \n");
return;
}

module_init
(ourinitmodule);
module_exit
(ourcleanupmodule);
Output is
USB Bus :4
Vendor Id:1d6B, Product Id:3
USB
Bus :3
Vendor Id:1d6B, Product Id:2
USB
Bus :2
Vendor Id:1d6B, Product Id:2
USB
Bus :1
Vendor Id:1d6B, Product Id:2

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More