1 简介
输入设备(如按键、键盘、触摸屏、鼠标等)是典型的字符设备,其一般的工作机理是底层在按键、触摸等动作发送时产生一个中断(或驱动通过 timer 定时查询),然后 CPU 通过 SPI、 I2C或外部存储器总线读取键值、坐标等数据,放入一个缓冲区,字符设备驱动管理该缓冲区,而驱动的 read()接口让用户可以读取键值、坐标等数据。
显然,在这些工作中,只是中断、读键值/坐标值是设备相关的,而输入事件的缓冲区管理以及字符设备驱动的 file_operations 接口则对输入设备是通用的。基于此,内核设计了输入子系统。
2 源码流程
2.1 源码
(1)初始化
内核deivers/input中,初始化
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file,
};
static int __init input_init(void)
{
...
err = class_register(&input_class);//在/sys/class 里创建一个input类
...
err = register_chrdev(INPUT_MAJOR, "input", &input_fops); //注册
...
}
input_init中还没有注册驱动设备,需要在具体的设备中注册。在input_fops中只有open函数。
static int input_open_file(struct inode *inode, struct file *file)
{
struct input_handler *handler;
const struct file_operations *old_fops, *new_fops = NULL;
int err;
lock_kernel();
/* No load-on-demand here? */
handler = input_table[iminor(inode) >> 5]; //取子设备号,除以32
if (!handler || !(new_fops = fops_get(handler->fops))) {
err = -ENODEV;
goto out;
}
if (!new_fops->open) {
fops_put(new_fops);
err = -ENODEV;
goto out;
}
old_fops = file->f_op;
file->f_op = new_fops;//新加载的驱动
err = new_fops->open(inode, file);//新加载驱动的open
if (err) {
fops_put(file->f_op);
file->f_op = fops_get(old_fops);
}
fops_put(old_fops);
out:
unlock_kernel();
return err;
}
从input_table中找到handler,并找到对应驱动的file_operations结构体,并调用open。
input_table在哪构造:input_register_handler中赋值。
input_register_handler由谁调用:搜索,得到
evdev_init in evdev.c (drivers\input) : return input_register_handler(&evdev_handler);
joydev_init in joydev.c (drivers\input) : return input_register_handler(&joydev_handler);
kbd_init in keyboard.c (drivers\char) : error = input_register_handler(&kbd_handler);
mousedev_init in mousedev.c (drivers\input) : error = input_register_handler(&mousedev_handler);
evdev.c——事件设备,joydev.c——joystick操作杆设备,keyboard.c——键盘设备,mousedev.c——鼠标设备。
(2)注册
关系:
handler是纯软件,device是设备相关。
int input_register_device(struct input_dev *dev)
{
...
list_add_tail(&dev->node, &input_dev_list); //放入链表中
...
list_for_each_entry(handler, &input_handler_list, node) //遍历input_handler_list链表里的每一项
input_attach_handler(dev, handler); //根据input_handler的id_table判断能否支持这个input_dev
...
}
input_attach_handler对handler和设备进行匹配。
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
...
id = input_match_device(handler->id_table, dev);
if (!id)
return -ENODEV;
error = handler->connect(handler, dev, id);
...
}
若匹配,则调用handler的connect;否则,退出。在input_register_handler中也会调用input_attach_handler。
int input_register_handler(struct input_handler *handler)
{
...
list_add_tail(&handler->node, &input_handler_list);
list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);
...
}
(3)connect
以evdev.c为例。
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
const struct input_device_id *id)
{
struct evdev *evdev;
int minor;
int error;
for (minor = 0; minor < EVDEV_MINORS; minor++)//查找可用的子设备号
if (!evdev_table[minor])
break;
if (minor == EVDEV_MINORS) {//最多32个
printk(KERN_ERR "evdev: no more free evdev devices\n");
return -ENFILE;
}
evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
if (!evdev)
return -ENOMEM;
INIT_LIST_HEAD(&evdev->client_list);
spin_lock_init(&evdev->client_lock);
mutex_init(&evdev->mutex);
init_waitqueue_head(&evdev->wait);
dev_set_name(&evdev->dev, "event%d", minor);//设备名称
evdev->exist = 1;
evdev->minor = minor;
evdev->handle.dev = input_get_device(dev);
evdev->handle.name = dev_name(&evdev->dev);
evdev->handle.handler = handler;
evdev->handle.private = evdev;
evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
evdev->dev.class = &input_class;
evdev->dev.parent = &dev->dev;
evdev->dev.release = evdev_free;
device_initialize(&evdev->dev);
error = input_register_handle(&evdev->handle);//注册handle
if (error)
goto err_free_evdev;
error = evdev_install_chrdev(evdev);
if (error)
goto err_unregister_handle;
error = device_add(&evdev->dev);//添加设备
if (error)
goto err_cleanup_evdev;
return 0;
err_cleanup_evdev:
evdev_cleanup(evdev);
err_unregister_handle:
input_unregister_handle(&evdev->handle);
err_free_evdev:
put_device(&evdev->dev);
return error;
}
input_register_handle进行注册
int input_register_handle(struct input_handle *handle)
{
struct input_handler *handler = handle->handler;
struct input_dev *dev = handle->dev;
int error;
error = mutex_lock_interruptible(&dev->mutex);
if (error)
return error;
list_add_tail_rcu(&handle->d_node, &dev->h_list);
mutex_unlock(&dev->mutex);
list_add_tail(&handle->h_node, &handler->h_list);
if (handler->start)
handler->start(handle);
return 0;
}
handle->d_node加入dev->h_list,handle->h_node加入handler->h_list。这样就可以从设备找到handle,再从handle找到handler。
(4)read
当应用层read时,会调用evdev_read
static ssize_t evdev_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
...
if (client->head == client->tail && evdev->exist &&
(file->f_flags & O_NONBLOCK)) //非阻塞情况下
return -EAGAIN;
retval = wait_event_interruptible(evdev->wait,
client->head != client->tail || !evdev->exist);//没有数据时,阻塞
if (retval)
return retval;
if (!evdev->exist)
return -ENODEV;
while (retval + input_event_size() <= count &&
evdev_fetch_next_event(client, &event)) {
if (input_event_to_user(buffer + retval, &event))
return -EFAULT;
retval += input_event_size();
}
return retval;
}
非阻塞时,若没有数据,直接返回;阻塞时,若没有数据,休眠。
若进入了休眠,如何唤醒:在evdev_event中
static void evdev_event(struct input_handle *handle,
unsigned int type, unsigned int code, int value)
{
...
wake_up_interruptible(&evdev->wait);
}
谁调用evdev_event:底层驱动,流程如下
input_event --->
input_handle_event --->
input_pass_event --->
handler->event() ---> // 最终会调用到handler 中的event函数
evdev_pass_event --->
client->buffer[client->head++] = *event; // 会将input输入事件数据存放在evdev_client结构体中的缓冲去中
当被唤醒后,读取数据,流程
evdev_read --->
evdev_fetch_next_event --->
*event = client->buffer[client->tail++] // 将evdev_client->buffer中的数据取走
input_event_to_user --->
copy_to_user // 拷贝到用户空间
3 例程
以MINI2440开发板中的4个按键,分别对应l、s、enter、leftshift。
//在 linux-2.6.32.2/arch/arm/mach-s3c2410/include/mach 目录下
#include<mach/regs-gpio.h> // 和GPIO相关的宏定义
#include<mach/hardware.h> //S3C2410_gpio_cfgpin
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/sched.h>
#include <linux/input.h>
//在 linux-2.6.32.2/arch/arm/include/asm 目录下
#include<asm/irq.h>
#include<asm/uaccess.h>
#include<asm/atomic.h>
#include<asm/unistd.h>
#include<asm/gpio.h>
#include<asm/io.h>
struct pin_desc {
int irq;
char *name;
unsigned int pin;
unsigned int key_val;
};
static struct pin_desc pins_desc[] = {
{IRQ_EINT8, "KEY1", S3C2410_GPG(0), KEY_L}, /* K1 */
{IRQ_EINT11, "KEY2", S3C2410_GPG(3), KEY_S}, /* K2 */
{IRQ_EINT13, "KEY3", S3C2410_GPG(5), KEY_ENTER}, /* K3 */
{IRQ_EINT14, "KEY4", S3C2410_GPG(6), KEY_LEFTSHIFT}, /* K4 */
};
static struct input_dev *buttons_dev;
static struct pin_desc *irq_pd;
static struct timer_list buttons_timer;
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
irq_pd = (struct pin_desc*)dev_id;
mod_timer(&buttons_timer, jiffies+HZ/100);
return IRQ_RETVAL(IRQ_HANDLED);
}
static void buttons_timer_function(unsigned long data)
{
struct pin_desc *pindesc = irq_pd;
unsigned int pinval;
if (!pindesc)
return;
pinval = s3c2410_gpio_getpin(pindesc->pin);
if (pinval)
{
input_event(buttons_dev, EV_KEY, pindesc->key_val, 0);
input_sync(buttons_dev);
}
else
{
input_event(buttons_dev, EV_KEY, pindesc->key_val, 1);
input_sync(buttons_dev);
}
}
static int __init s3c24xx_buttons_init(void)
{
int i;
/* 1. 分配一个input_dev结构体 */
buttons_dev = input_allocate_device();;
/* 2. 设置 */
/* 2.1 能产生哪类事件 */
set_bit(EV_KEY, buttons_dev->evbit); //按键事件
set_bit(EV_REP, buttons_dev->evbit); //Repeat
/* 2.2 能产生这类操作里的哪些事件: L,S,ENTER,LEFTSHIT */
set_bit(KEY_L, buttons_dev->keybit);
set_bit(KEY_S, buttons_dev->keybit);
set_bit(KEY_ENTER, buttons_dev->keybit);
set_bit(KEY_LEFTSHIFT, buttons_dev->keybit);
/* 3. 注册 */
buttons_dev->name = "button_dev";
input_register_device(buttons_dev);
/* 4. 硬件相关的操作 */
init_timer(&buttons_timer);
buttons_timer.function = buttons_timer_function;
add_timer(&buttons_timer);
for (i = 0; i < 4; i++)
{
request_irq(pins_desc[i].irq, buttons_interrupt, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, pins_desc[i].name, &pins_desc[i]);
}
return 0;
}
static void __exit s3c24xx_buttons_exit(void)
{
int i;
for (i = 0; i < 4; i++)
{
free_irq(pins_desc[i].irq, &pins_desc[i]);
}
del_timer(&buttons_timer);
input_unregister_device(buttons_dev);
input_free_device(buttons_dev);
}
module_init(s3c24xx_buttons_init);
module_exit(s3c24xx_buttons_exit);
MODULE_LICENSE("GPL");
4 测试
添加内核event interface。
make menuconfig
Device Drivers --->
| //核心层:drivers/input/input.c
Input device support --->
| //数据处理层:drivers/input/evdev.c
<*> Event interface
将编译好的模块下载到开发板中,执行insmod。
执行exec 0</dev/tty1,将/dev/tty1挂载到-sh进程描述符0下,此时的键盘驱动就会直接打印在tty1终端上。