键盘驱动

一、前言 #

本节讲解键盘输入在linux里面的处理,包括键值定义等

二、中断 #

键盘中断是1,在Linux源码里面注册如下

 1// drivers/input/serio/i8042-io.h
 2/*
 3 * IRQs.
 4 */
 5
 6#ifdef __alpha__
 7# define I8042_KBD_IRQ	1
 8# define I8042_AUX_IRQ	(RTC_PORT(0) == 0x170 ? 9 : 12)	/* Jensen is special */
 9#elif defined(__arm__)
10/* defined in include/asm-arm/arch-xxx/irqs.h */
11#include <asm/irq.h>
12#elif defined(CONFIG_PPC)
13extern int of_i8042_kbd_irq;
14extern int of_i8042_aux_irq;
15# define I8042_KBD_IRQ  of_i8042_kbd_irq
16# define I8042_AUX_IRQ  of_i8042_aux_irq
17#else
18# define I8042_KBD_IRQ	1
19# define I8042_AUX_IRQ	12
20#endif
21
22// drivers/input/serio/i8042.c
23static int i8042_setup_kbd(void)
24{
25	int error;
26
27	error = i8042_create_kbd_port();
28	if (error)
29		return error;
30
31	error = request_irq(I8042_KBD_IRQ, i8042_interrupt, IRQF_SHARED,
32			    "i8042", i8042_platform_device);
33	if (error)
34		goto err_free_port;
35
36	error = i8042_enable_kbd_port();
37	if (error)
38		goto err_free_irq;
39
40	i8042_kbd_irq_registered = true;
41	return 0;
42
43 err_free_irq:
44	free_irq(I8042_KBD_IRQ, i8042_platform_device);
45 err_free_port:
46	i8042_free_kbd_port();
47	return error;
48}

三、键值解析 #

键值的宏定义如下

1// include/uapi/linux/input-event-codes.h
2#define KEY_RESERVED		0
3#define KEY_ESC			1
4#define KEY_1			2
5#define KEY_2			3
6#define KEY_3			4
7#define KEY_4			5
8...