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

后端 未结 2 1836
庸人自扰
庸人自扰 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:33

    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 .

    #include 
    #include 
    #include 
    #include 
    #include 
    
    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
    

提交回复
热议问题