我一直在尝试编写一个简单的设备驱动程序,我在其中以编程方式获取供应商ID和产品ID.经过几乎所有必要的头文件后,我得出结论,我可以通过以下结构访问
USB设备的供应商ID,产品ID和制造商详细信息:struct usb_device {},其中包含成员结构usb_device_descriptor {} .这个嵌套结构有idVendor,idProduct和iManufacturer以及其他一些成员.
但不知何故,由于某种原因,我无法访问这些成员,所以当我插入我的模块后执行dmesg时,它会打印一些垃圾值.我很乐意收到帮助或提示或任何回复.以下是我到目前为止编写的代码:
P.S.:已经制作了必要的夹杂物.
经过几乎所有必要的头文件后,我知道我可以通过以下结构访问USB设备的供应商ID,其中包含成员结构usb_device_descriptor {}.这个嵌套结构有idVendor,idProduct和iManufacturer以及其他一些成员.
- //*******************************************
- 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有关描述符的更多详细信息.
更新1:
以下程序将为您提供有关系统中可用的HUB的信息. 3.2.0内核版本不支持usb_hub_for_each_child宏,但最新3.7.x版本支持.
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,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,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