替换内核USB鼠标驱动

(一)开发环境介绍
1.使用win7_64的笔记本搭建Virtualbox虚拟机,安装Ubuntu16.04系统作为编译环境。
为了减少内核版本和应用程序的影响,此章采用《Linux内核学习(3)最小系统制作》中搭建的最小系统linux4.15.0+rootfs(Busybox)环境,使用虚拟机启动此最小系统。

(二)屏蔽内核驱动
1.这里必须明白内核如何使用USB鼠标驱动,在内核的“\linux-source-4.15.0\drivers\hid\usbhid\Kconfig”文件中有如下描述

config USB_HID
	tristate "USB HID transport layer"
	default y
	depends on USB && INPUT
	select HID
	---help---
	  Say Y here if you want to connect USB keyboards,
	  mice, joysticks, graphic tablets, or any other HID based devices
	  to your computer via USB, as well as Uninterruptible Power Supply
	  (UPS) and monitor control devices.

	  You can't use this driver and the HIDBP (Boot Protocol) keyboard
	  and mouse drivers at the same time. More information is available:
	  <file:Documentation/input/input.txt>.

	  If unsure, say Y.

	  To compile this driver as a module, choose M here: the
	  module will be called usbhid.

comment "Input core support is needed for USB HID input layer or HIDBP support"
	depends on USB_HID && INPUT=n

这段内容表面,如果要使用自定义的USB鼠标驱动,必须不配置USB_HID,注意这里不能选择HID-GENERIC,即使不编译HID-GENERIC内核也能正常识别USB鼠标。
另外内核中有写好的USB鼠标驱动,代码位置为“\linux-source-4.15.0\drivers\hid\usbhid\usbmouse.c”,但是默认情况下usbmouse并不会编译,在menuconfig中也不会显示,在kconfig中有如下描述:

menu "USB HID Boot Protocol drivers"
	depends on USB!=n && USB_HID!=y && EXPERT

config USB_KBD
	tristate "USB HIDBP Keyboard (simple Boot) support"
	depends on USB && INPUT
	---help---
	  Say Y here only if you are absolutely sure that you don't want
	  to use the generic HID driver for your USB keyboard and prefer
	  to use the keyboard in its limited Boot Protocol mode instead.

	  This is almost certainly not what you want.  This is mostly
	  useful for embedded applications or simple keyboards.

	  To compile this driver as a module, choose M here: the
	  module will be called usbkbd.

	  If even remotely unsure, say N.

config USB_MOUSE
	tristate "USB HIDBP Mouse (simple Boot) support"
	depends on USB && INPUT
	---help---
	  Say Y here only if you are absolutely sure that you don't want
	  to use the generic HID driver for your USB mouse and prefer
	  to use the mouse in its limited Boot Protocol mode instead.

	  This is almost certainly not what you want.  This is mostly
	  useful for embedded applications or simple mice.

	  To compile this driver as a module, choose M here: the
	  module will be called usbmouse.

	  If even remotely unsure, say N.

endmenu

这段配置说明,如果要显示USB HID Boot Protocol drivers,USB配置不为n,USB_HID不为y,必须要有EXPERT配置。在原来配置情况下,显示为:
Linux驱动学习(2) 从usb驱动到input子系统4
Linux驱动学习(2) 从usb驱动到input子系统4
所以如果要显示此菜单的话,需要删掉EXPERT,使配置为:

menu "USB HID Boot Protocol drivers"
	depends on USB!=n && USB_HID!=y

然后选择USB_HID配置为模块,然后menuconfig配置如下:
Linux驱动学习(2) 从usb驱动到input子系统4Linux驱动学习(2) 从usb驱动到input子系统4

这样内核就会将鼠标编译进内核,我们可以测试下,在每个函数下打个log,例如
Linux驱动学习(2) 从usb驱动到input子系统4
这样编译完内核后就可以重启了,在virtualbox中选择设备-USB-指定USB鼠标,会有如下打印。
Linux驱动学习(2) 从usb驱动到input子系统4
可以看到probe函数已经被调用,表面鼠标驱动的确载入成功了。

相关文章: