我有一个非常简单的 USB 键盘驱动程序:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
#include <linux/usb/input.h>
#include <linux/hid.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("TEST_USER");
MODULE_DESCRIPTION("A USB Keyboard Driver Kernel Module");
static struct usb_device_id usb_kbd_id_table[] = {
{ USB_INTERFACE_INFO(
USB_INTERFACE_CLASS_HID,
USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_KEYBOARD) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, usb_kbd_id_table);
static int kbd_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
pr_info("USB keyboard probe function called\n");
return 0;
}
static void kbd_disconnect(struct usb_interface *interface)
{
pr_info("USB keyboard disconnect function called\n");
}
static struct usb_driver kbd_driver = {
.name = "usbkbd",
.probe = kbd_probe,
.disconnect = kbd_disconnect,
.id_table = usb_kbd_id_table,
};
static int __init kbd_init(void)
{
int res = 0;
res = usb_register(&kbd_driver);
if (res)
pr_err("usb_register failed with error %d", res);
return res;
}
static void __exit kbd_exit(void)
{
pr_debug("USB Keyboard Removed..\n");
usb_deregister(&kbd_driver);
return;
}
module_init(kbd_init);
module_exit(kbd_exit);
我命名它kbtest.ko
并加载它insmod kbtest.ko
,它已成功并通过 进行验证lsmod
。具体来说:
lsmod | grep "usb"
usbhid 31758 0
hid 69153 2 hid_generic,usbhid
usbcore 115019 6 ohci_hcd,ohci_pci,ehci_hcd,ehci_pci,kbtest,usbhid
usb_common 12484 1 usbcore
所以我插入 USB 键盘但无法执行驱动程序。具体来说,没有调用probe函数。这是检测键盘后我的 dmesg:
[ 1219.695553] usb 1-2: new full-speed USB device number 6 using ohci-pci
[ 1219.996010] usb 1-2: New USB device found, idVendor=1267, idProduct=0103
[ 1220.016126] usb 1-2: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[ 1220.100005] input: HID 1267:0103 as /devices/pci0000:00/0000:00:06.0/usb1/1-2/1-2:1.0/0003:1267:0103.0008/input/input14
[ 1220.126349] hid-generic 0003:1267:0103.0008: input,hidraw1: USB HID v1.10 Keyboard [HID 1267:0103] on usb-0000:00:06.0-2/input0
[ 1220.206453] input: HID 1267:0103 as /devices/pci0000:00/0000:00:06.0/usb1/1-2/1-2:1.1/0003:1267:0103.0009/input/input15
[ 1220.224334] hid-generic 0003:1267:0103.0009: input,hidraw2: USB HID v1.10 Device [HID 1267:0103] on usb-0000:00:06.0-2/input1
信息到这里就结束了。这里到底有什么问题呢?我在 Virtual Box 中运行 Debian 机器。
答案1
我在 ubuntu 14.04 机器上遇到了同样的问题。问题可能是默认usbhid
驱动程序阻止您的驱动程序正常运行。这可能是probe
热插拔时未调用的原因。尝试删除usbhid
模块。检查udev rules
您的发行版的定义,usbhid
即使您卸载它,它也会在热插拔上自动加载默认驱动程序模块。