目的:把usb鼠标当作按键使用,左键相当按键l,右键相当按键s,中键相当enter
代码如下
-
/*
-
* 参考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 input_dev *uk_dev;
-
static char *usb_buf;
-
static dma_addr_t usb_buf_phys;
-
static int len;
-
static struct urb *uk_urb;
-
-
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 void usbmouse_as_key_irq(struct urb *urb)
-
{
-
static unsigned char pre_val;
-
-
/* USB鼠标数据含义
-
* data[0]: bit0-左键, 1-按下, 0-松开
-
* bit1-右键, 1-按下, 0-松开
-
* bit2-中键, 1-按下, 0-松开
-
*/
-
if ((pre_val & (1<<0)) != (usb_buf[0] & (1<<0)))
-
{
-
/* 左键发生了变化 */
-
input_event(uk_dev, EV_KEY, KEY_L, (usb_buf[0] & (1<<0)) ? 1 : 0);
-
input_sync(uk_dev);
-
}
-
-
if ((pre_val & (1<<1)) != (usb_buf[0] & (1<<1)))
-
{
-
/* 右键发生了变化 */
-
input_event(uk_dev, EV_KEY, KEY_S, (usb_buf[0] & (1<<1)) ? 1 : 0);
-
input_sync(uk_dev);
-
}
-
-
if ((pre_val & (1<<2)) != (usb_buf[0] & (1<<2)))
-
{
-
/* 中键发生了变化 */
-
input_event(uk_dev, EV_KEY, KEY_ENTER, (usb_buf[0] & (1<<2)) ? 1 : 0);
-
input_sync(uk_dev);
-
}
-
-
pre_val = usb_buf[0];
-
-
/* 重新提交urb */
-
usb_submit_urb(uk_urb, GFP_KERNEL);
-
}
-
-
static int usbmouse_as_key_probe(struct usb_interface *intf, const struct usb_device_id *id)
-
{
-
struct usb_device *dev = interface_to_usbdev(intf);
-
struct usb_host_interface *interface;
-
struct usb_endpoint_descriptor *endpoint;
-
int pipe;
-
-
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);
-
-
interface = intf->cur_altsetting;
-
endpoint = &interface->endpoint[0].desc;
-
-
/* a. 分配一个input_dev */
-
uk_dev = input_allocate_device();
-
-
/* b. 设置 */
-
/* b.1 能产生哪类事件 */
-
set_bit(EV_KEY, uk_dev->evbit);//按键类事件
-
set_bit(EV_REP, uk_dev->evbit);//重复类事件
-
-
/* b.2 能产生哪些事件 */
-
set_bit(KEY_L, uk_dev->keybit);//左键作为按键L
-
set_bit(KEY_S, uk_dev->keybit);//右键作为按键S
-
set_bit(KEY_ENTER, uk_dev->keybit);//中键作为按键ENTER
-
-
/* c. 注册 */
-
if( input_register_device(uk_dev) )
-
return -EFAULT;
-
-
/* d. 硬件相关操作 */
-
/* 数据传输3要素: 源,目的,长度 */
-
/* 源: USB设备的某个端点 */
-
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
-
-
/* 长度: */
-
len = endpoint->wMaxPacketSize;
-
-
/* 目的: */
-
usb_buf = usb_alloc_coherent(dev, len, GFP_ATOMIC, &usb_buf_phys);
-
-
/* 使用"3要素" */
-
/* 分配usb request block */
-
uk_urb = usb_alloc_urb(0, GFP_KERNEL);
-
/* 使用"3要素设置urb" */
-
usb_fill_int_urb(uk_urb, dev, pipe, usb_buf, len, usbmouse_as_key_irq, NULL, endpoint->bInterval);
-
uk_urb->transfer_dma = usb_buf_phys;
-
uk_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
-
-
/* 使用URB */
-
usb_submit_urb(uk_urb, GFP_KERNEL);
-
-
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");
阅读(1131) | 评论(0) | 转发(0) |