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

查看:154
本文介绍了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,

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的驱动程序来构建整个内核.对吧?

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?

是的,但是还需要注意一些其他事项.首先了解如何加载特定的模块(驱动程序).关键是MODULE_DEVICE_TABLE(usb, &my_id_table);每当安装"特定模块(使用make modules_install)时,就会根据MODULE_DEVICE_TABLE中传递的id表,在/lib/modules/<your_kernel>/modules.usbmap/lib/modules/<your_kernel>/modules.dep文件中创建一个条目(搜索文件中的字符串"usbhid").每当检测到新的USB设备时,内核就会读取这些文件以查找匹配的参数.如果找到,将从/lib/modules/<your_kernel>/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/<your_kernel>/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驱动程序是核心驱动程序还是设备驱动程序?

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

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 pendrive,基于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天全站免登陆