USB设备的Linux平台上编程获得供应商ID,产品ID [英] Programmatically obtaining the vendor ID, product ID of a USB device on a Linux platform

查看:1900
本文介绍了USB设备的Linux平台上编程获得供应商ID,产品ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图写一个简单的设备驱动程序,其中我suppossed来获取供应商ID和产品ID编程。通过几乎所有必要的头文件已经走了,我来,我可以访问供应商ID一个结论,产品ID和的通过结构USB设备结构USB_DEVICE {} 里面有一个成员结构usb_device_descriptor {} 。此嵌套结构具有 idVendor,idProduct iManufacturer 和其他一些成员。

但不知何故,由于某种原因,我无法访问这些成员,所以当我做了的dmesg 插入我的模块后,它打印一些垃圾值。我会很高兴接受帮助或提示,或任何回应。以下是我迄今写的code:

P.S:必要的夹杂物而制定的。

已经通过的几乎所有必要的头文件了,我知道我可以通过一个结构访问供应商ID,产品ID,和USB设备制造商的详细信息:结构USB_DEVICE {} 里面有一个成员结构usb_device_descriptor {} 。此嵌套结构具有 idVendor,idProduct iManufacturer 和其他一些成员。

  // ************** *******结构USB_DEVICE udev的;结构usb_bus *总线;
ssiz​​e_t供RET;静态INT __init usb_fun_init(无效)
{
    INT结果;
    __le16 idVendor = 0;
    __le16 idProduct = 0;
    __u8 iManufacturer = 0;    printk的(KERN_INFO\\ n ************************************在初始化\\ n);
    list_for_each_entry(公交车,和放大器; usb_bus_list,bus_list){    printk的(KERN_INFO***************** ****************开始);
    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);    返回0;
}静态INT __exit usb_fun_exit(无效)
{
    printk的(KERN_INFO\\ n ************************************在退出\\ n);
}宏module_init(usb_fun_init);
宏module_exit(usb_fun_exit);MODULE_LICENSE(GPL);


解决方案

我猜,上面是满code你的内核模块。反正你使用正确的结构和供应商ID,设备ID将在设备描述符可用。 请参阅有关描述符的更多细节。

我建议你参考内核code 这里

更新1:


下面的程序将要给大家介绍的系统中可用的集线器信息。 usb_hub_for_each_child宏不是3.2.0内核版本的支持,但在最新的3.7.x版本的支持。

usb_bus_list 的#include&LT宣布; Linux的/ USB / hcd.h>

 的#include< Linux的/  -  module.h中GT;
#包括LT&;的Linux / kernel.h>
#包括LT&; Linux的/ usb.h>
#包括LT&; Linux的/ USB / hcd.h>
#包括LT&; Linux的/ list.h>MODULE_LICENSE(GPL);INT ourinitmodule(无效)
{INT CHIX = 0;
结构USB_DEVICE *开发,* childdev = NULL;
结构usb_bus *总线= NULL;list_for_each_entry(公交车,和放大器; usb_bus_list,bus_list)
{
   printk的(\\ n USB总线数:%d,&巴士─GT; BUSNUM);   开发=巴士─> root_hub;   printk的(\\ n供应商标识:%X,产品ID:%X \\ n的dev-> descriptor.idVendor,dev亡> descriptor.idProduct);
#如果0 // usb_hub_for_each_child宏3.2.0不支持,有这么3.7.6尝试。
   usb_hub_for_each_child(DEV,CHIX,childdev)
   {
        如果(childdev)
        {
           printk的(\\ n供应商标识:%X,产品ID:%X \\ n,childdev-> descriptor.idVendor,childdev-> descriptor.idProduct);
        }
   }
#万一}printk的(KERN_ALERT\\ n你好周杰伦,欢迎来样应用.... \\ n);返回0;
}无效ourcleanupmodule(无效)
{
printk的(KERN_ALERT。\\ n你好周杰伦,谢谢....退出程序\\ n);
返回;
}宏module_init(ourinitmodule);
宏module_exit(ourcleanupmodule);

输出为

  USB总线:4
供应商ID:1d6B,产品ID:3
USB总线:3
供应商ID:1d6B,产品编号:2
USB总线:2
供应商ID:1d6B,产品编号:2
USB总线:1
供应商ID:1d6B,产品编号:2

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

解决方案

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

这篇关于USB设备的Linux平台上编程获得供应商ID,产品ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆