在键盘热插拔上加载模块 [英] Loading module on keyboard hotplug

查看:99
本文介绍了在键盘热插拔上加载模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何为Linux系统编写模块和驱动程序.类似于insmode和modprobe初始化模块似乎可以正常工作(dmesg显示调试消息),插入键盘后也不会加载该模块.

I am trying to learn how to write modules and drivers for Linux systems. Similarly to this question I am trying to run a simple "Hello World" module on USB keyboard hot-plug (code below). Even though initializing the module by commands insmode and modprobe seem to work (dmesg shows debugging messages), the module is not loaded upon plugging in the keyboard.

我做了什么:

  1. 运行make以生成hellomodule.ko文件.
  2. hellomodule.ko文件复制到/lib/modules/"my_kernel_version"/
  3. 运行depmod -a命令.
  1. Run make to produce hellomodule.ko file.
  2. Copy the hellomodule.ko file to /lib/modules/"my_kernel_version"/
  3. Run depmod -a comand.

经过这三个步骤,我将模块添加到了modules.aliasmodules.dep文件中.仍然不起作用.

After those three steps I have my module added to modules.alias and modules.dep files. It still does not work.

这是内核配置错误还是完全不同?

Is this kernels configuration fault or something entirely different?

系统: Ubuntu 14.04 LTS ;内核: 3.14.0

hellomodule.c:

hellomodule.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);

Makefile:

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

推荐答案

我遇到了同样的问题.就我而言,这是由于usbhid模块已经加载,因为我使用的是USB鼠标.

I had the same problem. In my case it was caused by the usbhid module being already loaded, since I was using a USB mouse.

如果我正确理解它,则在Ubuntu 14.04中,在连接新设备时加载正确模块的udev规则如下(位于/lib/udev/rules.d/80-drivers.rules中):

If I understand it correctly, in Ubuntu 14.04, the udev rule that loads the proper module(s) when a new device is attached is the following (located in /lib/udev/rules.d/80-drivers.rules):

DRIVER!="?*", ENV{MODALIAS}=="?*", RUN{builtin}="kmod load $env{MODALIAS}"

如您所见,kmod load仅在新设备​​没有驱动程序时才执行.但是,如果已经加载了usbhid,则刚刚连接的键盘已经具有驱动程序.因此,"hello world"模块未加载.

As you can see, kmod load is executed only if the new device has no driver. However, if usbhid is already loaded, the just-attached keyboard already has a driver. Therefore the "hello world" module is not loaded.

一种可能的解决方案是通过删除DRIVER!="?*"条件来修改/覆盖udev规则,从而将其变为:

A possible solution is to modify/override the udev rule by removing the DRIVER!="?*" condition, thus turning it into:

ENV{MODALIAS}=="?*", RUN{builtin}="kmod load $env{MODALIAS}"`.

另一种可能的解决方法是在连接键盘之前卸载usbhid模块.当然,这将导致所有USB键盘,鼠标和其他HID类设备停止工作,直到您安装新键盘为止.

Another possible workaround is to unload the usbhid module before attaching the keyboard. Of course, that will cause all USB keyboards, mice and other HID class devices to stop working until you attach the new keyboard.

这篇关于在键盘热插拔上加载模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆