Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

c - 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 and iManufacturer 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 "
************************************ in init
");
    list_for_each_entry(bus, &usb_bus_list, bus_list){

    printk(KERN_INFO "***************** Begins ****************");
    printk(KERN_INFO "
Vendor ID = %d", udev.descriptor.idVendor);
    printk(KERN_INFO "
Product ID = %d", udev.descriptor.idProduct);
    printk(KERN_INFO "
Manufacturer = %s", udev.descriptor.iManufacturer);

    return 0;
}

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

module_init(usb_fun_init);
module_exit(usb_fun_exit);

MODULE_LICENSE("GPL");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("
 USB Bus : %d", bus->busnum);

   dev = bus->root_hub;

   printk("
 Vendor Id:%x, Product Id:%x
", 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("
 Vendor Id:%x, Product Id:%x
", childdev->descriptor.idVendor, childdev->descriptor.idProduct);
        }
   }
#endif

}    

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

return 0;
}

void ourcleanupmodule(void)
{
printk(KERN_ALERT "
 Hello Jay, Thanks....Exiting Application. 
");
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...