在此文章之前,我们讲解的都是简单的字符驱动,涉及的内容有字符驱动的框架、自动创建设备节点、linux中断、poll机制、异步通知、同步互斥、非阻塞、定时器去抖动。
上一节文章链接:http://blog.csdn.net/lwj103862095/article/details/17589311
在这一节里,我们要引入linux的分离分层的概念,linux输入子系统是一个很好的代表,在讲解如何编写input子系统的驱动之前,我们理所当然的要先好好认识一下input子系统的框架。
一、linux输入子系统的框架(摘自作者:刘洪涛,华清远见嵌入式学院讲师。)
下图是input输入子系统框架,输入子系统由输入子系统核心层(Input Core),驱动层和事件处理层(Event Handler)
三部份组成。一个输入事件,如鼠标移动,键盘按键按下,joystick的移动等等通过
input driver -> Input core -> Event handler -> userspace 到达用户空间传给应用程序。
二、drivers/input/input.c:
入口函数input_init > err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- <span style="font-size:14px;">static int __init input_init(void)
- {
- int err;
- ...
- /* 创建类 */
- err = class_register(&input_class);
- ...
- /* 注册一个字符驱动,主设备号为13 */
- err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- ...
- return 0;
- }</span>
- <span style="font-size:14px;">static const struct file_operations input_fops = {
- .owner = THIS_MODULE,
- .open = input_open_file,
- };</span>
input_open_file函数
- <span style="font-size:14px;">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;
- ...
- /* 以次设备号为下标,在input_table数组找到一项handler */
- handler = input_table[iminor(inode) >> 5];
- /* 通过handler找到一个新的fops */
- new_fops = fops_get(handler->fops);
- ...
- old_fops = file->f_op;
- /* 从此file->f_op = new_fops */
- file->f_op = new_fops;
- ...
- /* 用新的new_fops的打开函数 */
- err = new_fops->open(inode, file);
- ...
- return err;
- }</span>
- <span style="font-size:14px;">struct input_handler {
- void *private;
- void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
- int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
- void (*disconnect)(struct input_handle *handle);
- void (*start)(struct input_handle *handle);
- const struct file_operations *fops;
- int minor;
- const char *name;
- const struct input_device_id *id_table;
- const struct input_device_id *blacklist;
- struct list_head h_list;
- struct list_head node;
- };</span>
APP:read > ... > file->f_op->read
问:input_table数组由谁构造?
答:input_register_handler
三、input_register_handler函数(注册input_handler)
- <span style="font-size:14px;">int input_register_handler(struct input_handler *handler)
- {
- struct input_dev *dev;
- ...
- INIT_LIST_HEAD(&handler->h_list);
- ...
- /* 将handler放入input_table数组 */
- input_table[handler->minor >> 5] = handler;
- ...
- /* 将handler放入input_handler_list链表 */
- list_add_tail(&handler->node, &input_handler_list);
- ...
- /* 对于每个input_dev,调用input_attach_handler
- * 根据input_handler的id_table判断能否支持这个input_dev
- */
- list_for_each_entry(dev, &input_dev_list, node)
- input_attach_handler(dev, handler);
- ...
- }</span>
- <span style="font-size:14px;">int input_register_device(struct input_dev *dev)
- {
- ...
- struct input_handler *handler;
- ...
- device_add(&dev->dev);
- ...
- /* 把input_dev放入input_dev_list链表 */
- list_add_tail(&dev->node, &input_dev_list);
- ...
- /* 对于每一个input_handler,都调用input_attach_handler
- * 根据input_handler的id_table判断能否支持这个input_dev
- */
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- ...
- }</span>
五、input_attach_handler函数
- <span style="font-size:14px;">static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
- {
- const struct input_device_id *id;
- ...
- /* 根据input_handler的id_table判断能否支持这个input_dev */
- input_match_device(handler->id_table, dev);
- ...
- /* 若支持,则调用handler的connect函数,建立连接 */
- handler->connect(handler, dev, id);
- ...
- }</span>
注册input_dev或input_handler时,会两两比较左边的input_dev和右边的input_handler,根据input_handler的id_table判断这个input_handler能否支持这个input_dev,如果能支持,则调用input_handler的connect函数建立"连接"。
问:如何建立连接connect?
答:举例,evdev_connect函数
- <span style="font-size:14px;">static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
- const struct input_device_id *id)
- {
- struct evdev *evdev;
- ...
- /* 分配一个input_handle */
- evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
- ...
- snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
- evdev->exist = 1;
- evdev->minor = minor;
- evdev->handle.dev = input_get_device(dev); // 指向左边的input_dev
- evdev->handle.name = evdev->name;
- evdev->handle.handler = handler; // 指向右边的input_handler
- evdev->handle.private = evdev;
- /* 设置dev结构体成员 */
- dev_set_name(&evdev->dev, evdev->name);
- 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);
- /* 注册 */
- input_register_handle(&evdev->handle);
- ...
- }</span>
input_handle结构体成员
- <span style="font-size:14px;">struct input_handle {
- void *private;
- int open;
- const char *name;
- struct input_dev *dev;
- struct input_handler *handler;
- struct list_head d_node;
- struct list_head h_node;
- };</span>
- <span style="font-size:14px;">int input_register_handle(struct input_handle *handle)
- {
- struct input_handler *handler = handle->handler;
- struct input_dev *dev = handle->dev;
- ...
- /* 把handle->d_node添加到dev->h_list
- * 这样,就可以从dev->h_list找到handle,进而找到handler
- */
- list_add_tail_rcu(&handle->d_node, &dev->h_list);
- ...
- /* 把handle->h_node添加到handler->h_list
- * 这样,就可以从handler->h_list找到handle,进而找到dev
- */
- list_add_tail(&handle->h_node, &handler->h_list);
- ...
- return 0;
- }</span>
怎么建立连接connect?
1. 分配一个input_handle结构体
2.
input_handle.dev = input_dev; // 指向左边的input_dev
input_handle.handler = input_handler; // 指向右边的input_handler
3. 注册:
input_handler->h_list = &input_handle;
inpu_dev->h_list = &input_handle;
六、怎么读按键?
答:举例,evdev_read
- <span style="font-size:14px;">static ssize_t evdev_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ppos)
- {
- struct evdev_client *client = file->private_data;
- struct evdev *evdev = client->evdev;
- struct input_event event;
- ...
- /* 无数据并且是非阻塞方式打开,则立刻返回 */
- 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);
- ...
- }</span>
搜索evdev->wait发现是evdev_event唤醒的
- <span style="font-size:14px;">static void evdev_event(struct input_handle *handle,
- unsigned int type, unsigned int code, int value)
- {
- struct evdev *evdev = handle->private;
- struct evdev_client *client;
- struct input_event event;
- ...
- /* 唤醒 */
- wake_up_interruptible(&evdev->wait);
- }</span>
答:应该是硬件相关的代码,input_dev那层调用的在设备的中断服务程序里,确定事件是什么,然后调用相应的input_handler的event处理函数。
举例,在drivers/input/keyboard/gpio_keys.c里的gpio_keys_isr函数
- <span style="font-size:14px;">static irqreturn_t gpio_keys_isr(int irq, void *dev_id)
- {
- struct gpio_button_data *bdata = dev_id;
- struct gpio_keys_button *button = bdata->button;
- ...
- /* 上报事件 */
- gpio_keys_report_event(bdata);
- return IRQ_HANDLED;
- }</span>
gpio_keys_report_event函数
- <span style="font-size:14px;">static void gpio_keys_report_event(struct gpio_button_data *bdata)
- {
- struct gpio_keys_button *button = bdata->button;
- struct input_dev *input = bdata->input;
- unsigned int type = button->type ?: EV_KEY;
- int state = (gpio_get_value(button->gpio) ? 1 : 0) ^ button->active_low;
- /* 上报事件 */
- input_event(input, type, button->code, !!state);
- input_sync(input);
- }</span>
问:input_event函数如何上报事件
答:
- input_event-->input_handle_event-->input_pass_event
- list_for_each_entry_rcu(handle, &dev->h_list, d_node)
- if (handle->open)
- handle->handler->event(handle,
- type, code, value);
怎么写符合输入子系统框架的驱动程序?
1. 分配一个input_dev结构体
2. 设置
3. 注册
4. 硬件相关的代码,比如在中断服务程序里上报事件