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

后端 未结 2 1837
庸人自扰
庸人自扰 2020-12-10 07:19

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

2条回答
  •  温柔的废话
    2020-12-10 07:46

    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");  
    

提交回复
热议问题