驱动usb鼠标识别

废话不说,先贴代码:

#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/usb/input.h>
#include <linux/hid.h>
#define DRIVER_LICENSE "GPL"
#define DRIVER_AUTHOR "same"
#define DRIVER_DESC "u s b probe..."

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);
//#define DEVICE_FOR_SPECIAL
#define USB_MOUSE_VENDOR_ID    0x1c4f   //  根据具体的usb进行设置
#define USB_MOUSE_PRODUCT_ID    0x03    //  根据具体的usb进行设置

struct usb_device_id my_usb_table[] = {
	
	{ 
	#ifndef DEVICE_FOR_SPECIAL   /*仅和指定的设备类型相匹配 */
	  USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
	  USB_INTERFACE_PROTOCOL_MOUSE),
	#else  /*仅和指定的制造商和产品ID匹配,用于需要特定驱动的设备*/
	  USB_DEVICE(USB_MOUSE_VENDOR_ID,USB_MOUSE_PRODUCT_ID),	
	#endif
	},
	{}

};

static int my_usb_probe(struct usb_interface *intf,const struct usb_device_id *id){
	
	int retval = -ENOMEM;
	struct usb_device *dev = interface_to_usbdev(intf);
	printk("this is my_usb_probe,found device. \n");
	printk("bcdUSB:%x\t",dev->descriptor.bcdUSB);
	printk("VID : 0x%x \t ",dev->descriptor.idVendor);
	printk("PID : 0x%x \t",dev->descriptor.idProduct);
	return 0;

}

static void my_usb_disconnect (struct usb_interface *intf){
	printk("device %x disconnect ... \n",interface_to_usbdev(intf)->descriptor.idVendor);
}


static struct usb_driver my_usb_driver = {
	.name       =	"my_usb_driver_probe",//在/sys/中的驱动目录名字 
	.probe      =      my_usb_probe,
	.disconnect =	my_usb_disconnect,
	.id_table   =	my_usb_table,
};

static int __init my_usb_mouse_init(void){
	int result;

	/* register this driver with the USB subsystem */
	result = usb_register(&my_usb_driver);
	if (result)
		err("usb_register failed. Error number %d", result);

	return result;
}

static void __exit my_usb_mouse_exit(void){
	usb_deregister(&my_usb_driver);
}


module_init(my_usb_mouse_init);
module_exit(my_usb_mouse_exit);

这个usb的鼠标识别,它可以根据#define DEVICE_FOR_SPECIAL这个宏的是否设置, 从而进行是对特定的usb进行识别, 还是对于与设备类型进行识别.我的鼠标VENDOR_ID 是 0x1c4f , PRODUCT_ID是 0x03,不同的鼠标就需要修改其id了.那么怎么添加到板子上呢?首先,因为板子本身就是支持鼠标的,所以我们必修要把系统本身的usb鼠标识别给去掉: make menuconfig去掉原来的USB鼠标驱动-> Device Drivers   -> HID Devices  <> USB Human Interface Device (full HID) support  // 去掉这一项编译,烧写,insmod驱动前,插入鼠标, 提示:

usb 1-1: New USB device found, idVendor=1c4f, idProduct=0003
usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=0
usb 1-1: Product: Usb Mouse
usb 1-1: Manufacturer: SIGMACHIP
usb 1-1: configuration #1 chosen from 1 choice

/* 上面的 idVendor 和idProduct就是 我特定设备驱动的id  */

接着insmod驱动, 打印this is my_usb_probe,found device.bcdUSB:110      VID : 0x1c4f     PID : 0x3      bcdDevice : 0x110

到此, 我们的驱动识别到了我们的usb鼠标.后来我又做了猜想, 如果说我们同时把#define DEVICE_FOR_SPECIAL这个宏添加上, 再去掉, 编译出两个驱动(注,.name = "my_usb_driver_probe"需要修改),那么我都insmod到内核中, 那么插入usb鼠标, 它具体使用哪一个驱动呢???

一开始猜测, 因为我们其中一个是"指定的设备类型相匹配" , 而且刚刚好就是我这个usb鼠标, 所以应该是会使用这个的, 可是我发现实际上他这个的话, 是根据你先insmod哪个, 就先使用哪个驱动,  具体内核在操作的时候应该就是bus在发现有device加入的时候调用usb总线的match函数,而usb的match函数进行一个一个判断, 如果有合适的就直接跳出, 认定就是那个驱动了.
接下来你可以看:   简单的usb鼠标驱动分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值