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 necessar
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
After spending a few time exploring header files, I got it to work. It was simple, just that I couldnt visualize what was happening and how it was happenning. I would love to thank all the people who posted/replied to it. Here is the updated sample code, so that someone like me who is newbie could just refer it. Its not at all a perfect reference though, I would love it if someone comes up with suggestions and modifications. Thank You!
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;
__u8 iSerialNumber = 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 "Vendor ID = %x", bus->root_hub->descriptor.idVendor);
printk(KERN_INFO "Product ID = %x", bus->root_hub->descriptor.idProduct);
printk(KERN_INFO "Serial Number = %x", bus->root_hub->descriptor.iSerialNumber);
//printk(KERN_INFO "Manu = %s", bus->root_hub->descriptor.iManufacturer);
printk(KERN_INFO "Manu = %s", bus->root_hub->manufacturer);
printk(KERN_INFO "Product = %s", bus->root_hub->product);
printk(KERN_INFO "Serial Number = %s", bus->root_hub->serial);
printk(KERN_INFO "\nManufacturer = %s", udev.bus.iManufacturer);
}
return 0;
}
static void __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");