我正在为 LED 徽章编写一个小型简单的驱动器模块。我遵循了一些教程和参考资料来完成这项工作。所有这些似乎都暗示当设备插入时会自动加载该模块。但是,当我运行usb-devices
相应的设备时有 driver Driver=usbfs
。
我有另一个终端正在执行后续操作dmesg
,它根本不会吐出任何调试消息。当我尝试连接和断开连接时,会出现以下内容:
[36078.726962] usb 1-2: USB disconnect, device number 24
[36080.925619] usb 1-2: new full-speed USB device number 25 using xhci_hcd
[36081.067062] usb 1-2: New USB device found, idVendor=0483, idProduct=5750
[36081.067067] usb 1-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[36081.067071] usb 1-2: Product: CHW20160112 HID
[36081.067075] usb 1-2: Manufacturer: Chuanghongweitech1
[36081.068695] input: Chuanghongweitech1 CHW20160112 HID as /devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/0003:0483:5750.0018/input/input40
[36081.069072] hid-generic 0003:0483:5750.0018: input,hidraw2: USB HID v1.10 Device [Chuanghongweitech1 CHW20160112 HID] on usb-0000:00:14.0-2/input0
如果我向日志发出带有此输出的sudo insmod ledbadge_drv.ko
(用于卸载) ,我的模块就会卸载/加载:rmmod
[36216.554620] ledbadge:, Unloading module. Handler: ledbadge_exit
[36216.554620] ledbadge:, Attempting to deregister USB device.
[36216.554621] usbcore: deregistering interface driver ledbadge
[36216.554647] ledbadge:, Leaving module!
[36217.632615] ledbadge:, Loading module. Handler: ledbadge_init
[36217.632616] ledbadge:, Attempting to register USB device.
[36217.632636] usbcore: registered new interface driver ledbadge
该模块的代码如下:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/usb.h>
#define LEDBADGE_VENDOR_ID 0x0483
#define LEDBADGE_PRODUCT_ID 0x5750
#define LEDBADGE_MOD_NAME "ledbadge"
#define LEDBADGE_DRV_NAME "ledbadge"
/************* usb operations *************/
/*
ledbadge_probe
called when plugging the USB device */
static int ledbadge_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
printk(KERN_NOTICE "Led Badge driver (%04X: %04X) plugged \n", id->idVendor, id->idProduct);
return 0;
}
/*
ledbadge_disconnect
called when unplugging the USB device */
static void ledbadge_disconnect(struct usb_interface *interface)
{
printk(KERN_NOTICE "%s:, ledbage USB device removed", LEDBADGE_DRV_NAME);
}
/************* usb driver *************/
// information required to recognize the device
static struct usb_device_id ledbadge_table [] = {
{USB_DEVICE(LEDBADGE_VENDOR_ID, LEDBADGE_PRODUCT_ID)},
{ }, // terminating entry
};
MODULE_DEVICE_TABLE(usb, ledbadge_table);
static struct usb_driver ledbadge_driver = {
.name = LEDBADGE_DRV_NAME,
.id_table = ledbadge_table,
.probe = ledbadge_probe,
.disconnect = ledbadge_disconnect,
};
/************** module definition **************/
/* ledbadge_init
caled on module loading */
static int __init ledbadge_init(void)
{
int ret = -1;
printk(KERN_NOTICE "%s:, Loading module. Handler: %s",
LEDBADGE_MOD_NAME, __FUNCTION__);
printk(KERN_NOTICE "%s:, Attempting to register USB device.", LEDBADGE_MOD_NAME);
// attempt to register usb device
ret = usb_register(&ledbadge_driver);
// log dmesg of status
if(ret == 0) printk(KERN_NOTICE "%s:, Device registered!", LEDBADGE_MOD_NAME);
else printk(KERN_ERR "%s:, Device registration failed > %d", LEDBADGE_MOD_NAME, ret);
return ret;
}
/* ledbadge_exit
caled on module unloading */
static void __exit ledbadge_exit(void)
{
printk(KERN_NOTICE "%s:, Unloading module. Handler: %s",
LEDBADGE_MOD_NAME, __FUNCTION__);
printk(KERN_NOTICE "%s:, Attempting to deregister USB device.", LEDBADGE_MOD_NAME);
// attempt to deregister usb device
usb_deregister(&ledbadge_driver);
// log dmesg of status
printk(KERN_NOTICE "%s:, Leaving module!", LEDBADGE_MOD_NAME);
}
module_init(ledbadge_init);
module_exit(ledbadge_exit);
MODULE_LICENSE("GPL");
答案1
您是否将模块放入/lib/modules/<your kernel version>
目录树中并depmod
在之后运行?
如果是这样,那么/lib/modules/<your kernel version>/modules.alias
文件中应该有这样一行:
alias usb:v0483p5750d*dc*dsc*dp*ic*isc*ip*in* ledbadge
当内核看到一个新的 USB 设备时,它会读取其供应商和产品 ID 以及许多其他属性,并将这些属性用作系统创建的模块别名的组件,如上所示。然后内核会使用modprobe
该别名执行命令(实际机制在 Linux 2.6 及更高版本的历史中有所不同)。如果modprobe
找到与其匹配的别名定义,则加载该别名定义中列出的模块。
我认为匹配的方式是最具体的匹配获胜。目前看来该hid-generic
模块可能会胜过您的模块(假设您depmod
在安装模块后已运行),因此您的 ledbadge 设备可能是 USB HID 类设备。因此,您可能还需要/替代地声明 USB HID 设备别名。像这样的东西:
static const struct hid_device_id ledbadge_hid_table[] = {
{HID_USB_DEVICE(LEDBADGE_VENDOR_ID,LEDBADGE_PRODUCT_ID)},
{ } // terminating entry
};
MODULE_DEVICE_TABLE(hid, ledbadge_hid_table);
文件中的结果行modules.alias
应类似于:
alias hid:b0003g*v00000483p00005750 ledbadge