设备层:直接操作硬件,所有事件上报核心层或者通过核心层得到需要硬件执行什么操作
input子系统按键驱动实例:
1 /* 参考drivers\input\keyboard\gpio_keys.c */ 2 3 #include <linux/module.h> 4 #include <linux/version.h> 5 6 #include <linux/init.h> 7 #include <linux/fs.h> 8 #include <linux/interrupt.h> 9 #include <linux/irq.h> 10 #include <linux/sched.h> 11 #include <linux/pm.h> 12 #include <linux/sysctl.h> 13 #include <linux/proc_fs.h> 14 #include <linux/delay.h> 15 #include <linux/platform_device.h> 16 #include <linux/input.h> 17 #include <linux/irq.h> 18 19 #include <asm/gpio.h> 20 #include <asm/io.h> 21 #include <asm/arch/regs-gpio.h> 22 23 struct pin_desc{ 24 int irq; 25 char *name; 26 unsigned int pin; 27 unsigned int key_val; 28 }; 29 30 struct pin_desc pins_desc[4] = { 31 {IRQ_EINT8, "S2", S3C2410_GPG(0), KEY_L}, 32 {IRQ_EINT11, "S3", S3C2410_GPG(3), KEY_S}, 33 {IRQ_EINT13, "S4", S3C2410_GPG(5), KEY_ENTER}, 34 {IRQ_EINT14, "S5", S3C2410_GPG(6), KEY_LEFTSHIFT}, 35 }; 36 37 static struct input_dev *buttons_dev; 38 static struct pin_desc *irq_pd; 39 static struct timer_list buttons_timer; 40 41 static irqreturn_t buttons_irq(int irq, void *dev_id) 42 { 43 /* 10ms后启动定时器 */ 44 irq_pd = (struct pin_desc *)dev_id; 45 mod_timer(&buttons_timer, jiffies+HZ/100); 46 return IRQ_RETVAL(IRQ_HANDLED); 47 } 48 49 static void buttons_timer_function(unsigned long data) 50 { 51 struct pin_desc * pindesc = irq_pd; 52 unsigned int pinval; 53 54 if (!pindesc) 55 return; 56 57 pinval = s3c2410_gpio_getpin(pindesc->pin); 58 59 if (pinval) 60 { 61 /* 松开 : 最后一个参数: 0-松开, 1-按下 */ 62 input_event(buttons_dev, EV_KEY, pindesc->key_val, 0); 63 input_sync(buttons_dev); 64 } 65 else 66 { 67 /* 按下 */ 68 input_event(buttons_dev, EV_KEY, pindesc->key_val, 1); 69 input_sync(buttons_dev); 70 } 71 } 72 73 static int buttons_init(void) 74 { 75 int i; 76 77 /* 1. 分配一个input_dev结构体 */ 78 buttons_dev = input_allocate_device();; 79 80 /* 2. 设置 */ 81 /* 2.1 能产生哪类事件 */ 82 set_bit(EV_KEY, buttons_dev->evbit); 83 set_bit(EV_REP, buttons_dev->evbit); 84 85 /* 2.2 能产生这类操作里的哪些事件: L,S,ENTER,LEFTSHIT */ 86 set_bit(KEY_L, buttons_dev->keybit); 87 set_bit(KEY_S, buttons_dev->keybit); 88 set_bit(KEY_ENTER, buttons_dev->keybit); 89 set_bit(KEY_LEFTSHIFT, buttons_dev->keybit); 90 91 /* 3. 注册 */ 92 input_register_device(buttons_dev); 93 94 /* 4. 硬件相关的操作 */ 95 init_timer(&buttons_timer); 96 buttons_timer.function = buttons_timer_function; 97 add_timer(&buttons_timer); 98 99 for (i = 0; i < 4; i++) 100 { 101 request_irq(pins_desc[i].irq, buttons_irq, IRQ_TYPE_EDGE_BOTH, pins_desc[i].name, &pins_desc[i]); 102 } 103 104 return 0; 105 } 106 107 static void buttons_exit(void) 108 { 109 int i; 110 for (i = 0; i < 4; i++) 111 { 112 free_irq(pins_desc[i].irq, &pins_desc[i]); 113 } 114 115 del_timer(&buttons_timer); 116 input_unregister_device(buttons_dev); 117 input_free_device(buttons_dev); 118 } 119 120 module_init(buttons_init); 121 122 module_exit(buttons_exit); 123 124 MODULE_LICENSE("GPL");