目的:只要在接入USB鼠标后打印"find usbmouse!\n"及设备编号等信息,移除后打印"disconnect usbmouse!\n"
可以参考内核目录下的drivers\hid\usbhid\usbmouse.c
步骤如下:
1. 分配/设置usb_driver结构体
.id_table
.probe
.disconnect
2. 注册
代码如下:
-
/*
-
* 参考drivers\hid\usbhid\usbmouse.c
-
*/
-
-
#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>
-
-
static struct usb_device_id usbmouse_as_key_id_table [] = {
-
{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,USB_INTERFACE_PROTOCOL_MOUSE) },
-
//只要USB设备的接口描述符里的类是HID,子类是BOOT,协议是MOUSE,那就支持该设备
-
-
//{USB_DEVICE(0x1234,0x5678)},
-
//只支持某个厂家生产的某个产品,厂家编号0x1234,设备编号0x5678
-
-
{ } /* Terminating entry */
-
};
-
-
static int usbmouse_as_key_probe(struct usb_interface *intf, const struct usb_device_id *id)
-
{
-
struct usb_device *dev = interface_to_usbdev(intf);
-
-
printk("find usbmouse!\n");
-
printk("bcdUSB = %x\n", dev->descriptor.bcdUSB);
-
printk("VID = 0x%x\n", dev->descriptor.idVendor);
-
printk("PID = 0x%x\n", dev->descriptor.idProduct);
-
-
return 0;
-
}
-
-
static void usbmouse_as_key_disconnect(struct usb_interface *intf)
-
{
-
printk("disconnect usbmouse!\n");
-
}
-
-
/* 1. 分配/设置usb_driver */
-
static struct usb_driver usbmouse_as_key_driver = {
-
.name = "usbmouse_as_key",
-
.probe = usbmouse_as_key_probe,
-
.disconnect = usbmouse_as_key_disconnect,
-
.id_table = usbmouse_as_key_id_table,
-
};
-
-
-
static int usbmouse_as_key_init(void)
-
{
-
/* 2. 注册 */
-
usb_register(&usbmouse_as_key_driver);
-
return 0;
-
}
-
-
static void usbmouse_as_key_exit(void)
-
{
-
usb_deregister(&usbmouse_as_key_driver);
-
}
-
-
module_init(usbmouse_as_key_init);
-
module_exit(usbmouse_as_key_exit);
-
-
MODULE_LICENSE("GPL");
测试:
先要去除内核的usb鼠标驱动 ,
make menuconfig去掉原来的USB鼠标驱动
-> Device Drivers
-> HID support
->USB HID support
< > USB HID transport layer
[ ] PID device support
用新内核启动,
接入USB鼠标后打印"find usbmouse!\n"及设备编号等信息,移除后打印"disconnect usbmouse!\n"
阅读(1154) | 评论(0) | 转发(0) |