linux内核中usb鼠标使用了哪些驱动? [英] Which drivers are used by usb mouse in linux kernel?

查看:18
本文介绍了linux内核中usb鼠标使用了哪些驱动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 LDD3 第 14 章中阅读了有关热插拔驱动程序的内容.我需要编写一个 USB 鼠标驱动程序,该驱动程序在我插入硬件时加载.现在,做一些实验,我知道有一个名为hid-generic"的驱动程序,它在拔出插头时调用.

I read from LDD3 chapter 14 about hotplug drivers.I need to write a usb mouse driver which load when I plug the hardware. Now, doing some experiment I come to know that there is a driver named "hid-generic" which is called when plug-unplug.

[ 6654.232046] usb 3-1: new low-speed USB device number 3 using uhci_hcd
[ 6654.462061] usb 3-1: New USB device found, idVendor=093a, idProduct=2510
[ 6654.462067] usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 6654.462071] usb 3-1: Product: USB OPTICAL MOUSE
[ 6654.462074] usb 3-1: Manufacturer: PIXART
[ 6654.489316] input: PIXART USB OPTICAL MOUSE as /devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0/input/input12
[ 6654.489445] hid-generic 0003:093A:2510.0004: input,hidraw0: USB HID v1.10 Mouse [PIXART USB OPTICAL MOUSE] on usb-0000:00:1d.1-1/input0

还有 lsmod 显示,

Also lsmod shows,

Module                  Size  Used by
hid_generic            12541  0 
usbhid                 47259  0 
hid                   105241  2 hid_generic,usbhid
psmouse               102541  0 

我的疑惑如下,

1) 为了在插入鼠标时加载我的模块(热插拔),我必须在内核中禁用这 3 个驱动程序,并使用 id_table 中的供应商和设备 ID 使用我的驱动程序构建整个内核.对吗?

1) To make my module load (hotplug) when this mouse plugs in, I have to disable these 3 drivers in kernel and build whole kernel with my driver with vendor and device ID in id_table. Right?

2) 我还阅读了 USB 核心驱动程序和 USB 设备驱动程序.那么这些 HID 驱动是核心驱动还是设备驱动?

2) I also read about USB core drivers and USB device drivers. So these HID drivers are core drivers or device drivers?

3) USB 鼠标的核心驱动程序和设备驱动程序分别是哪些?我在哪里可以在内核源代码中找到它们?

3) Which are core drivers and device driver in case of USB mouse? And where can I find them in kernel source?

谢谢,苏尼尔.

推荐答案

我会尽量一一回答你的问题:

I'll try to answer your questions one by one :

1) 为了在鼠标插入时加载我的模块(热插拔),我必须在内核中禁用这 3 个驱动程序,并使用 id_table 中的供应商和设备 ID 使用我的驱动程序构建整个内核.对吗?

是的,但您还需要注意一些其他事项.首先了解如何加载特定模块(驱动程序).关键是 MODULE_DEVICE_TABLE(usb, &my_id_table); 每当安装"特定模块时(使用 make modules_install),根据 id 表,一个条目传入 MODULE_DEVICE_TABLE/lib/modules//modules.usbmap/lib/modules//modules.dep 中创建 文件(在文件中搜索字符串usbhid").每当检测到新的 USB 设备时,内核都会读取这些文件以查找匹配的参数.如果找到,则从保存信息的 /lib/modules//modules.dep 中找到的相应路径加载以下模块.关于驱动程序所在的路径及其依赖项.

Yes, but there are some additional things you need to take care of. First understand how a particular module(driver) gets loaded. The key to this is MODULE_DEVICE_TABLE(usb, &my_id_table); Whenever a particular module is "installed" (using make modules_install), an entry, according to the id table passed in MODULE_DEVICE_TABLE gets created in /lib/modules/<your_kernel>/modules.usbmap and /lib/modules/<your_kernel>/modules.dep file(search for the string "usbhid" in the files). Whenever a new usb device is detected, the kernel reads these files to find the matching parameters. If it is found, the following module is loaded from the corresponding path found in /lib/modules/<your_kernel>/modules.dep which holds the info. about the path where the driver is located and also its dependencies.

所以,现在即使您从内核中卸载(rmmod) usbhid,当您重新插入鼠标时,它也会再次加载.为了避免这种情况发生,您需要修改这些文件,即从文件中删除条目.为此,请将 usbhid 驱动程序从其原始路径(通常位于 /lib/modules//kernel/drivers/hid/usbhid/usbhid.ko 到一个安全的地方.现在重建依赖项,以便从依赖项文件中删除条目.

So, now even if you unload(rmmod) usbhid from the kernel, it will be loaded again when you re-insert your mouse. To avoid this from happening you need to modify those files, i.e. remove the entries from the files. To do so, "move" the usbhid driver from its original path(generally located at /lib/modules/<your_kernel>/kernel/drivers/hid/usbhid/usbhid.ko to a safe place. Now rebuild the dependencies such that the entries would be removed from the dependency files.

现在您需要创建驱动程序条目.只需安装驱动程序即可!

Now you need to create entries of your driver. Just install your driver and you are good to go!

总结一下:

$ sudo rmmod usbhid                                      # Unload the usb mouse driver
$ cd /lib/modules/$(uname -r)/                           # Move to your current kernel
$ vim modules.usbmap                                     # Check for the "usbhid" string
$ vim modules.dep                                        # Check for "usbhid.ko:" string
$ sudo mv kernel/drivers/hid/usbhid/usbhid.ko ~/Desktop  # Take backup of your current 
                                                           usb mouse driver
$ sudo depmod -a                                         # Rebuild the dependency files

现在再次检查字符串usbhid"的依赖文件.它不应该在那里!

Now check the dependency files for the string "usbhid" again. It shouldn't be there!

$ cd /path/to/your/driver
$ sudo make modules_install  # Install your driver into /lib/modules/$(uname -r)/extra
$ sudo depmod -a             # Rebuild the dependency files

这一步之后,在依赖文件中搜索你的模块对应的字符串,应该就有了!从这一刻起,每当您插入鼠标(或从引导本身)时,您的驱动程序将被加载,而不是原始驱动程序.

After this step, search for the string corresponding to your module in the dependency files, and it should be there! From this moment on, whenever you insert the mouse(or from boot itself) your driver will be loaded, instead of the original.

使用完驱动程序后,您可以将原始 usbhid 文件复制回其原始目的地并重建依赖文件 (sudo depmod -a)

Once your are done playing with your driver, you may copy back the original usbhid file to its original destination and rebuild the dependency files (sudo depmod -a)

现在我还看到您正在尝试使用供应商和设备 ID 来匹配您的设备,在这种情况下,驱动程序将仅适用于您的鼠标.推荐的方法是使用类 ID,这使您的驱动程序适用于任何 USB 鼠标.

Now I also see that you are trying to use vendor and device id to match your device, in which case, the driver would work only for your mouse. The recommended way is to use class ids, which makes your driver work for any usb mouse.

2) 我还阅读了 USB 核心驱动程序和 USB 设备驱动程序.那么这些 HID 驱动程序是核心驱动程序还是设备驱动程序?

usbhid 基本上是一个设备驱动程序".驱动程序的分类可以简述为:核心驱动程序、主机控制器驱动程序和设备驱动程序:

usbhid is basically a "device driver". The classification of drivers could be briefed out as : core drivers, host controller drivers and device drivers :

设备驱动程序:这是用于控制设备的软件.例如 USB 鼠标、基于 pci 的以太网卡、USB 随身碟、基于 i2c 的加速度计.

Device Drivers : This is the software used to control the devices. For example usb mouse, pci based ethernet card, usb pendrive, i2c based accelerometer.

主机控制器驱动程序: 这是为控制总线控制器而编写的软件.例如 USB 主机控制器(EHCI、UHCI、OHCI 等)、PCI 主机控制器、I2C 主机等.

Host Controller Drivers : This is the software written to control the bus controller. For example USB Host Controllers(EHCI, UHCI, OHCI, etc.), PCI Host Controller, I2C Masters, etc.

核心驱动程序:这些实际上将上述讨论的驱动程序粘合在一起.例如 USB 核心、PCI 核心等.核心驱动程序提供了辅助程序(API),以便设备和主机控制器驱动程序可以使用它们(模块堆栈的概念!).这些是将正确的设备绑定到其驱动程序的那些.核心驱动程序还提供许多其他服务.

Core Drivers : These actually glues up the above discussed drivers. Examples are USB core, PCI core, etc. Core drivers provides helper routines(APIs) such that the device and host-controller driver could make use of them(concept of module stacking!). These are the ones, which bind the correct device to its driver. There are many other services provided by the core drivers.

USB 设备驱动程序示例代码:

Example code for USB Device Driver :

http://lxr.free-electrons.com/source/drivers/hid/usbhid/usbmouse.c

您可以在以下位置找到 USB 主机控制器驱动程序:

You may find the USB Host Controller Drivers under :

http://lxr.free-electrons.com/source/drivers/usb/host/

USB 核心位于此处:http://lxr.free-electrons.com/source/drivers/usb/core/

USB Core resides here : http://lxr.free-electrons.com/source/drivers/usb/core/

我认为这也回答了你的第三个问题!

I think this also answers your third question!

希望这有帮助.

这篇关于linux内核中usb鼠标使用了哪些驱动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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