首页 > 其他 > 详细

这是我的第一篇微博(2.13.1)————input子系统

时间:2019-01-19 17:02:05      阅读:188      评论:0      收藏:0      [点我收藏+]

自己写按键(input)驱动的步骤如下:
a.确定主设备号(major);
b.构造一个file_operations结构体{.open;  .read;  .write; ...}
c.register_chrdev函数注册这个字符设备驱动程序(其实就是将file_operations里的东西以及主次设备号向内核注册);
d.入口函数和出口函数;

but,在liux内核中有现成的input子系统的驱动程序,  现在我们就来看看系统的输入子系统的驱动框架。
我们来到drivers/input/目录下,有个input.c文件,这里的驱动代码便是输入子系统驱动框架的核心层。

先看他的入口函数:
static int __init input_init(void)
{
  int err;
  err = class_register(&input_class);
  if (err) {
     pr_err("unable to register input_dev class\n");
    return err;

}
err = input_proc_init();
if (err)
  goto fail1;
err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0);

INPUT_MAX_CHAR_DEVICES, "input");
if (err) {
  pr_err("unable to register char major %d", INPUT_MAJOR);
   goto fail2;

}
  return 0;
  fail2:    input_proc_exit();
  fail1:    class_unregister(&input_class);
  return err;

}

这里有一个疑问,以上代码是linux-4.14-74版本的代码,采用的是register_chrdev_region新接口函数;而课程中用的老接口函数register_chrdev是可以经得起分析的:

register_chrdev(INPUT_MAJOR, "input", &input_fops);

&input_fops--> struct file_operations input_fops
{.open = input_open_file,};

input_open_file-->函数input_open_file() {
struct input_handler *handle = input_table[iminor](inode) >> 5;
struct file_operations *old_fops, *new_fops;
new_fops = fops_get(handler->fops);

file->f_op = new_fops;

new_fops->open(inode, file);
}

所以应用层app:read > ... > file->f_op->read!

input_table数组由谁构造:input_register_handle函数        这个函数被谁调用:evdev.c, keyboarb.c, joydec等

插入图片:

device与handler如何连接起来:evdev举例:
struct input_handler evdev_handler = {
.event = evdev_event,
.connect = evdev_connect,
.disconnect = evdev_disconnect,

.fops = &evdev_fops,
.minor = ..,
.name = "evdev",
.id_table = evdev_ids,
};

若比较device和hanler后能对应起来,则调用以上的connect函数(猜测); .id_table表示能支持什么device

注册输入函数:
函数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_attach_handler(dev, handler);//对于每一个input_handler,都调用input_attach_handler;  根据input_handler的id_table判断能否支持这个input_dev
}

注册input_handler:
函数input_register_handler(struct input_handler *handler){
input_table[handler->minor >> 5] = handler;//放入数组
list_add_tail(&handler->node, &input_handler_list);//放入链表


list_for_each_entry(dev, &input_dev_list, node)
input_attach_handler(dev, handler);  //对于每个input_dev,调用input_attach_handler; 根据input_handler的id_table判断能否支持这个input_dev
}

函数input_attach_handler(struct input_dev *dev, struct input_handler *handler){

input_match_device(handler->id_table, dev);
//match成功后

handler->connect(handler, dev, id);
}

小结:注册input_dev或input_handler时,会两两比较; 根据input_handler的id_table判断这个input_handler能否支持这个input_dev;  如果支持,调用input_handler的connect函数建立“连接”。

举例evdev.c里如何建立连接:
.connect = evdev_connect,
函数static int evdev_connect(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id){

evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);//分配一个input_hanle
evdev->handle.dev = dev;//指向左边的input_dev
evdev->handle.name = evdev->name;
evdev->handle.handler = handler;//指向右边的input_handler
evdev->handle.private = evdev;
input_register_handle(&evdev->handle);

}

1.分配一个input_handle结构体
2.evdev->handle.dev = input_dev;//指向左边的input_dev          evdev->handle.handler = input_handler;//指向右边的input_handler
3.注册: input_handler->h_list = &input_handler;    input_dev->h_list = &input_handler;


怎么读按键?
app:read一下-----------(evdev,或其他.c)input_handler里的fops里面的读函数(.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->exit && (file->f_flags & O_NONBLOCK))    
    return -EAGAIN; //无数据并且是非阻塞方式打开,则立即返回

retval = wait_event_interruptible(evdev->wait, client->head != client->tail || !evdev->exist); //否则休眠

}

谁来唤醒?-------函数evdev_evevt()----wake_up_interruptible(&evdev->wait);
evdev_event被谁调用?------猜测应该是被硬件相关的代码,input_dev那层调用

在设备的中断服务程序里,确定事件是什么,然后调用相应的input_handler的event处理函数,,gpio_keys.c中:

gpio_keys_isr(){

input_event(input, type, button->code, !!state);
input_sync(input);


}

函数input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) {

struct input_handle *handle;
list_for_each_entry(handle, &dev->h_list, d_node)
if(handle->open)

  handle->handler->event(handle, type, code, value)

}

这是我的第一篇微博(2.13.1)————input子系统

原文:https://www.cnblogs.com/ayaya-123/p/10292154.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!