我正在尝试学习如何为 Linux 系统编写模块和驱动程序。类似于this https://unix.stackexchange.com/questions/120839/usb-kernel-module-does-not-load-on-demand-but-works-fine-with-insmod-and-modprob问题 我正在尝试在 USB 键盘热插拔上运行一个简单的“Hello World”模块(代码如下)。即使通过命令初始化模块insmode
and modprobe
似乎有效(dmesg
显示调试消息),插入键盘时不会加载模块。
我做了什么:
- Run
make
生产hellomodule.ko
file.
- 复制
hellomodule.ko
文件到 /lib/modules/"my_kernel_version"/
- Run
depmod -a
comand.
经过这三个步骤后,我将模块添加到modules.alias
and modules.dep
文件。它仍然不起作用。
这是内核配置错误还是完全不同的东西?
System: Ubuntu 14.04 LTS;核心:3.14.0
你好模块.c:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/usb.h>
#include <linux/usb/input.h>
#include <linux/hid.h>
MODULE_AUTHOR("author");
MODULE_DESCRIPTION("helloworld module\n");
MODULE_LICENSE("GPL");
static struct usb_device_id hello_id_table [] = {
{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID,
USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_KEYBOARD) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, hello_id_table);
static int hello_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
pr_debug("HelloModule: USB keyboard probe function called\n");
return 0;
}
static void hello_disconnect(struct usb_interface *interface)
{
pr_debug("HelloModule: USB keyboard disconnect function called\n");
}
static struct usb_driver hello_driver = {
//.owner = THIS_MODULE,
.name = "hello_driver",
.probe = hello_probe,
.disconnect = hello_disconnect,
.id_table = hello_id_table
};
static int __init hello_init(void)
{
int retval = 0;
pr_debug("HelloModule: Hello World!\n");
retval = usb_register(&hello_driver);
if (retval)
pr_debug("HelloModule: usb_register failed. Error number %d", retval);
return 0;
}
static void __exit hello_exit(void)
{
usb_deregister(&hello_driver);
pr_debug("HelloModule: exit\n");
}
module_init(hello_init);
module_exit(hello_exit);
生成文件:
obj-m := hellomodule.o
CFLAGS_hellomodule.o := -DDEBUG
KDIR := /lib/modules/`uname -r`/build
default:
make -C $(KDIR) M=$(PWD) modules
clean:
make -C $(KDIR) M=$(PWD) clean