以编程方式获取Linux平台上USB设备的供应商ID,产品ID

前端之家收集整理的这篇文章主要介绍了以编程方式获取Linux平台上USB设备的供应商ID,产品ID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在尝试编写一个简单的设备驱动程序,我在其中以编程方式获取供应商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以及其他一些成员.

  1. //*******************************************
  2.  
  3. struct usb_device udev;
  4.  
  5. struct usb_bus *bus;
  6. ssize_t ret;
  7.  
  8. static int __init usb_fun_init(void)
  9. {
  10. int result;
  11. __le16 idVendor = 0;
  12. __le16 idProduct = 0;
  13. __u8 iManufacturer = 0;
  14.  
  15. printk(KERN_INFO "\n************************************ in init\n");
  16. list_for_each_entry(bus,&usb_bus_list,bus_list){
  17.  
  18. printk(KERN_INFO "***************** Begins ****************");
  19. printk(KERN_INFO "\nVendor ID = %d",udev.descriptor.idVendor);
  20. printk(KERN_INFO "\nProduct ID = %d",udev.descriptor.idProduct);
  21. printk(KERN_INFO "\nManufacturer = %s",udev.descriptor.iManufacturer);
  22.  
  23. return 0;
  24. }
  25.  
  26. static int __exit usb_fun_exit(void)
  27. {
  28. printk(KERN_INFO "\n************************************ in exit\n");
  29. }
  30.  
  31. module_init(usb_fun_init);
  32. module_exit(usb_fun_exit);
  33.  
  34. MODULE_LICENSE("GPL");

解决方法

我想,上面是你的内核模块的完整代码.无论如何,您使用的是正确的结构和供应商ID,设备描述符中将提供设备ID. Refer有关描述符的更多详细信息.

我建议你参考内核代码here.

更新1:

以下程序将为您提供有关系统中可用的HUB的信息. 3.2.0内核版本不支持usb_hub_for_each_child宏,但最新3.7.x版本支持.

usb_bus_list在#include< linux / usb / hcd.h>中声明.

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/usb.h>
  4. #include <linux/usb/hcd.h>
  5. #include <linux/list.h>
  6.  
  7. MODULE_LICENSE("GPL");
  8.  
  9. int ourinitmodule(void)
  10. {
  11.  
  12. int chix = 0;
  13. struct usb_device *dev,*childdev = NULL;
  14. struct usb_bus *bus = NULL;
  15.  
  16. list_for_each_entry(bus,bus_list)
  17. {
  18. printk("\n USB Bus : %d",bus->busnum);
  19.  
  20. dev = bus->root_hub;
  21.  
  22. printk("\n Vendor Id:%x,Product Id:%x\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
  23. #if 0 //usb_hub_for_each_child macro not supported in 3.2.0,so trying with 3.7.6.
  24. usb_hub_for_each_child(dev,chix,childdev)
  25. {
  26. if(childdev)
  27. {
  28. printk("\n Vendor Id:%x,childdev->descriptor.idVendor,childdev->descriptor.idProduct);
  29. }
  30. }
  31. #endif
  32.  
  33. }
  34.  
  35. printk(KERN_ALERT "\n Hello Jay,Welcome to sample application.... \n");
  36.  
  37. return 0;
  38. }
  39.  
  40. void ourcleanupmodule(void)
  41. {
  42. printk(KERN_ALERT "\n Hello Jay,Thanks....Exiting Application. \n");
  43. return;
  44. }
  45.  
  46. module_init(ourinitmodule);
  47. module_exit(ourcleanupmodule);

输出

  1. USB Bus :4
  2. Vendor Id:1d6B,Product Id:3
  3. USB Bus :3
  4. Vendor Id:1d6B,Product Id:2
  5. USB Bus :2
  6. Vendor Id:1d6B,Product Id:2
  7. USB Bus :1
  8. Vendor Id:1d6B,Product Id:2

猜你在找的Linux相关文章