对输入子系统分析总结

来源:互联网 发布:网络教育文凭出国留学 编辑:程序博客网 时间:2024/06/06 02:54
 原文链接   http://100ask.net/forum/showtopic-3567.aspx

在drivers/input/input.c中:   

 进入模块入口函数input_init :    
对输入子系统分析总结

  1.     err= register_chrdev(INPUT_MAJOR, "input",&input_fops);
复制代码
    而input_fops只有open和llseek函数:    
  1.  static const struct file_operations input_fops ={
  2.     .owner= THIS_MODULE,
  3.     .open= input_open_file,
  4.     .llseek= noop_llseek,
  5.     };
复制代码
     那么没有read函数的话该怎么读数据呢?    进入 input_open_file函数:        
  1.   struct input_handler*handler;//定义一个input_handler指针
  2.         handler= input_table[iminor(inode) >>5];//根据传入文件的次设备号从
  3.         //input_table中提取出一个input_handler
  4.         if(handler)
  5.         new_fops=fops_get(handler->fops);//从input_handler中提取出new_fops
  6.         file->f_op= new_fops;//将new_fops赋值给当前文件的file_operations
  7.         err= new_fops->open(inode, file);
复制代码
   经过这些操作后,当app再来调用read,write,open等函数时,最终都会调用到file->f_op 
中的read,write,open等函数。
  那么input_open_file函数中的input_table 又是从何而来的呢?    
搜索代码可知,    
在input.c的input_register_handler函数中构造了input_table:       
  1.   if(handler->fops != NULL) {
  2.             if(input_table[handler->minor>> 5]) {
  3.                 retval= -EBUSY;
  4.                 gotoout;
  5.             }
  6.             //给input_table中的第handler->minor >>5项赋值
  7.             input_table[handler->minor>> 5] = handler;
  8.         }
复制代码
     又是谁在调用input_register_handler呢?    搜索内核可知:    evdev.c,tsdev.c,joydev.c,
keyboard.c,mousedev.c等文件调用了    input_register_handler,我们以evdev.c为例 
    在drivers/input/evdev.c中:
     进入模块入口函数evdev_init:    
  1.      returninput_register_handler(&evdev_handler);
  2.     evdev_handler的定义如下:
  3.         staticstruct input_handler evdev_handler = {
  4.         .event        =evdev_event,
  5.         .connect    =evdev_connect,
  6.         .disconnect    =evdev_disconnect,
  7.         .fops        =&evdev_fops,//前面所用到的 new_fops指的就是这里定义的的fops
  8.         .minor        =EVDEV_MINOR_BASE,
  9.         .name        ="evdev",
  10.         .id_table    =evdev_ids,
  11.         };
复制代码
     这里需要插入的讲解一下输入子系统的架构!如图:    通过input_register_device注册一个
input_dev设备或者通过  input_register_handler注册一个input_handler时,input_dev与
input_handler    会进行匹配,如何匹配?   
input_handler中有一个id_table,里面包含的就是这个input_handler能处理的input_dev,
当input_handler与input_dev匹配成功的话,则会调用    input_handler里    的connect函数。  
     
 下面我们来看下input_register_device和input_register_handler分别做了什么:  
  1.    input_register_device
  2.         //将刚注册的input_dev放入input_dev_list链表(1)
  3.         list_add_tail(&dev->node,&input_dev_list);
  4.         
  5.             
  6.         list_for_each_entry(handler,&input_handler_list,node)
  7.         input_attach_handler(dev,handler);
  8.         
  9.     input_register_handler
  10.         //将刚注册的input_handler放入input_table中(1)
  11.         input_table[handler->minor>> 5] = handler;
  12.     
  13.         //将刚注册的input_handler放入input_handler_list链表中 (2)
  14.         list_add_tail(&handler->node,&input_handler_list);

  15.         
  16.         list_for_each_entry(dev,&input_dev_list,node)
  17.         input_attach_handler(dev,handler);
复制代码
     由此可以看出,无论是先注册input_handler还是先注册input_dev最终都会调用来
input_attach_handler(dev,handler);
来进行两两匹配。现在我们来看看input_attach_handler是如何将input_handler和input_dev进行
匹配的。    
在input_attach_handler函数中:       
id = input_match_device(handler, dev);          
        
 error = handler->connect(handler,dev, id);        
我们以evdev.c中的 input_handler结构中的connect函数为例,分析connect函数做了些什么。    
evdev_connect函数中:
  1.  
  2.         
  3.         evdev= kzalloc(sizeof(struct evdev), GFP_KERNEL);
  4.         
  5.         
  6.         evdev->handle.dev= input_get_device(dev);
  7.         evdev->handle.name= dev_name(&evdev->dev);
  8.         evdev->handle.handler= handler;
  9.         evdev->handle.private= evdev;
  10.       
  11.         
  12.         error=input_register_handle(&evdev->handle);
  13.      
复制代码
            下面我们来看注册input_handle做了些什么:       
  1.       
  2.             
  3.        list_add_tail_rcu(&handle->d_node,&dev->h_list);
  4.             
  5.             
  6.             list_add_tail_rcu(&handle->h_node,&handler->h_list);
复制代码
     对于输入设备,我们知道一般需要读取其数据,那么经过上述的一些列动作后又是如何读取到数据的呢?
我们来看下evdev.c中的input_handler中的fops中的read函数:   
 在evdev_read函数中:       
 //如果没有数据且nonblock的话,则EAGAIN
  1.        if( client->head == client->tail&& evdev->exist&&
  2.         (file->f_flags & O_NONBLOCK))
  3.         return-EAGAIN;
  4.         //否则,睡眠
  5.         retval=wait_event_interruptible(evdev->wait,client->head!= client->tail ||!        evdev->exist);
复制代码
     既然有睡眠,那么何时被唤醒,搜索代码。    
evdev_event函数中:       
 //唤醒  wake_up_interruptible(&evdev->wait);    
evdev_event是input_handler中的成员,当有数据可读
(如触摸屏被按下,按键    被按下)时,event函数会被调用。
而event函数是怎么被调用到的?这就得看设备层了,
设备层的驱动做了如下工作:   
<1>在中断函数中确定判断发生了什么事件,并且相应的值是多少   
<2>通过input_event()函数上报事件    //input.c input_report_key会调用 input_event
<3>通过input_sync()函数表明上报结束   

分析input_event函数我们就可以知道input_handler中的event函数是如何被调用到的了。   
input_event中,调用了input_handle_event函数,在input_handle_event函数中调用了
input_pass_event函数; 
   
在input_pass_event函数中:
input_report_key ——> input_event——> input_handle_event ——> input_pass_event ——>
handler->event


  1.  
  2.         structinput_handler *handler;
  3.         
  4.         list_for_each_entry_rcu(handle,&dev->h_list, d_node) {
  5.             if (!handle->open)
  6.                 continue;
  7.             //如果该input_handle被打开,则该input_handle->input_handler即为可            //处理该input_dev的handler
  8.             handler= handle->handler;
  9.             if(!handler->filter) {
  10.                 if(filtered)
  11.                     break;
  12.                 
  13.                 handler->event(handle,type, code, value);
  14.             }else if (handler->filter(handle, type, code,value))
  15.                 filtered= true;
  16.         }
复制代码
 最后,回归到如何写符合输入子系统框架的驱动程序,四个步骤:
 <1> 分配一个input_dev结构体
 <2> 设置input_dev
 <3>    注册input_dev        
 <4>  在中断函数中上报事件