---
layout: post
title: USB鼠标驱动简单示例
subtitle: USB鼠标移动产生中断执行定义好的函数
date: 2019-9-2
author: Jerry Chen
header-img: img/post-bg-debug.png
catalog: true
tags:
- 应用
- USB鼠标
---
### 开始
首先从内核中**去掉**鼠标驱动:
路径:Device Drivers ---> HID Devices ---> USB Human Interface Device (full HID) support
#### 编写USB鼠标驱动
首先关于匹配信息:采用协议的方式匹配,也就是不按照厂商id和产品id进行匹配,而是只要使用该协议的设备都可以匹配成功。
![](https://raw.githubusercontent.com/jvfan/jvfan.github.io/master/img/post_img/20190902225501.png)
编写usb_demo_drv.c
```
#include
#include
#include
#include
#define DRIVER_VERSION "v1.6"
#define DRIVER_AUTHOR "Vojtech Pavlik "
#define DRIVER_DESC "USB HID Boot Protocol mouse driver"
#define DRIVER_LICENSE "GPL"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);
static int pipe,maxp,count;
static signed char *data;
static struct urb *mouse_urb;
static dma_addr_t data_dma;
//下面是usb鼠标中断执行的函数
static void check_usb_data(struct urb *urb){
printk("count is %d time!\n",count++);
//每个usb_submit_urb检测一次,故在执行函数中提交可多次检测
usb_submit_urb(mouse_urb, GFP_KERNEL);
}
static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(intf);
struct usb_endpoint_descriptor *endpoint;
printk("usb mouse probe!\n");
//[可选]打印一些描述信息(一般�