我一直在尝试编写一个简单的设备驱动程序,假设我可以通过编程方式获得供应商ID和产品ID。遍历了几乎所有必需的头文件后,我得出的结论是,我可以通过以下结构访问USB设备的供应商ID,产品ID和制造商详细信息:struct usb_device{},该成员具有struct usb_device_descriptor{}成员。此嵌套结构具有idVendor, idProductiManufacturer以及其他一些成员。

但是由于某种原因,由于某种原因,我无法访问这些成员,因此,在插入模块后执行dmesg时,它会打印一些垃圾值。我很高兴收到帮助或提示或任何答复。以下是我到目前为止编写的代码:

附注:已包含必要的内容。

遍历了几乎所有必需的头文件后,我知道可以通过以下结构访问USB设备的供应商ID,产品ID和制造商详细信息:struct usb_device{},该成员具有struct usb_device_descriptor{}成员。此嵌套结构具有idVendor, idProductiManufacturer以及其他一些成员。

//*******************************************

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

最佳答案

我想,以上是您的内核模块的完整代码。无论如何,只要您使用正确的结构和供应商ID,设备ID就会在设备描述符中可用。 Refer有关描述符的更多详细信息。

我建议您引用内核代码here

更新1:

以下程序将为您提供有关系统中可用HUB的信息。 3.2.0内核版本不支持usb_hub_for_each_child宏,而最新的3.7.x版本则支持usb_hub_for_each_child宏。
usb_bus_list#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);

输出为
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

关于c - 以编程方式获取Linux平台上USB设备的供应商ID,产品ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14722392/

10-13 07:31
查看更多