linux系统下USB键盘驱动源码+使用文档说明
如何编写Linux下的USB键盘驱动
1. 指定USB键盘驱动所需的头文件:
#include /*内核头文件,含有内核一些常用函数的原型定义*/
#include /*定义内存分配的一些函数*/
#include /*模块编译必须的头文件*/
#include /*输入设备相关函数的头文件*/
#include /*linux初始化模块函数定义*/
#include /*USB设备相关函数定义*/
2. 定义键盘码表数组:
/*使用第一套键盘扫描码表:A-1E;B-30;C-2E…*/
static unsigned char usb_kbd_keycode[256] = {
0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
150,158,159,128,136,177,178,176,142,152,173,140
};
3. 编写设备ID表:
static struct usb_device_id usb_kbd_id_table [] = {
{ USB_INTERFACE_INFO(3, 1, 1) },/*3,1,1分别表示接口类,接口子类,接口协议;3,1,1为键盘接口类;鼠标为3,1,2*/
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);/*指定设备ID表*/
4. 定义USB键盘结构体:
struct usb_kbd {
struct input_dev *dev; /*定义一个输入设备*/
struct usb_device *usbdev;/*定义一个usb设备*/
unsigned char old[8]; /*按键离开时所用之数据缓冲区*/
struct urb *irq/*usb键盘之中断请求块*/, *led/*usb键盘之指示灯请求块*/;
unsigned char newleds;/*目标指定灯状态*/
char name[128];/*存放厂商名字及产品名字*/
char phys[64]