input子系统一 主要数据结构

来源:互联网 发布:淘宝南风小铺抄袭 编辑:程序博客网 时间:2024/06/06 00:40

一、input子系统的分层结构

Linux系统实现了很多子系统架构,每个子系统都是分层的;input子系统也不例外,主要分为三层:

(1) 硬件驱动层:与硬件相关,是驱动开发的部分。

(2) 系统核心层:是连接其他两层的纽带,对应的代码为drivers/input/input.c、input-mt.c等。

(3) 事件处理层:真正的事件处理部分,为应用程序提供读写接口;对应的代码为drivers/input/evdev.c、mousedev.c等。

二、主要数据结构

[cpp] view plain copy
  1. struct input_value {  
  2.     __u16 type;  
  3.     __u16 code;  
  4.     __s32 value;  
  5. };  

input设备report数据最终调用的函数是void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);这个函数的参数最终会填到上述结构中。

[cpp] view plain copy
  1. struct input_dev {  
  2.     const char *name;  
  3.     const char *phys;  
  4.     const char *uniq;  
  5.     struct input_id id;  
  6.   
  7.     unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];  
  8.   
  9.     unsigned long evbit[BITS_TO_LONGS(EV_CNT)];  
  10.     unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];  
  11.     unsigned long relbit[BITS_TO_LONGS(REL_CNT)];  
  12.     unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];  
  13.     unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];  
  14.     unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];  
  15.     unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];  
  16.     unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];  
  17.     unsigned long swbit[BITS_TO_LONGS(SW_CNT)];  
  18.   
  19.     unsigned int hint_events_per_packet;  
  20.   
  21.     unsigned int keycodemax;  
  22.     unsigned int keycodesize;  
  23.     void *keycode;  
  24.   
  25.     int (*setkeycode)(struct input_dev *dev,  
  26.               const struct input_keymap_entry *ke,  
  27.               unsigned int *old_keycode);  
  28.     int (*getkeycode)(struct input_dev *dev,  
  29.               struct input_keymap_entry *ke);  
  30.   
  31.     struct ff_device *ff;  
  32.   
  33.     unsigned int repeat_key;  
  34.     struct timer_list timer;  
  35.   
  36.     int rep[REP_CNT];  
  37.   
  38.     struct input_mt *mt;  
  39.   
  40.     struct input_absinfo *absinfo;  
  41.   
  42.     unsigned long key[BITS_TO_LONGS(KEY_CNT)];  
  43.     unsigned long led[BITS_TO_LONGS(LED_CNT)];  
  44.     unsigned long snd[BITS_TO_LONGS(SND_CNT)];  
  45.     unsigned long sw[BITS_TO_LONGS(SW_CNT)];  
  46.   
  47.     int (*open)(struct input_dev *dev);  
  48.     void (*close)(struct input_dev *dev);  
  49.     int (*flush)(struct input_dev *dev, struct file *file);  
  50.     int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);  
  51.   
  52.     struct input_handle __rcu *grab;  
  53.   
  54.     spinlock_t event_lock;  
  55.     struct mutex mutex;  
  56.   
  57.     unsigned int users;  
  58.     bool going_away;  
  59.   
  60.     struct device dev;  
  61.   
  62.     struct list_head    h_list;  
  63.     struct list_head    node;  
  64.   
  65.     unsigned int num_vals;  
  66.     unsigned int max_vals;  
  67.     struct input_value *vals;  
  68.   
  69.     bool devres_managed;  
  70. };  

input_dev代表一个输入设备,每个输入设备的驱动文件中都能找到它,h_list用于挂handle,node用于挂自己到全局的list上。

[cpp] view plain copy
  1. struct input_id {  
  2.     __u16 bustype;  
  3.     __u16 vendor;  
  4.     __u16 product;  
  5.     __u16 version;  
  6. };  
[cpp] view plain copy
  1. struct input_handler {  
  2.   
  3.     void *private;  
  4.   
  5.     void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);  
  6.     void (*events)(struct input_handle *handle,  
  7.                const struct input_value *vals, unsigned int count);  
  8.     bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);  
  9.     bool (*match)(struct input_handler *handler, struct input_dev *dev);  
  10.     int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);  
  11.     void (*disconnect)(struct input_handle *handle);  
  12.     void (*start)(struct input_handle *handle);  
  13.   
  14.     bool legacy_minors;  
  15.     int minor;  
  16.     const char *name;  
  17.   
  18.     const struct input_device_id *id_table;  
  19.   
  20.     struct list_head    h_list;  
  21.     struct list_head    node;  
  22. };  

input_handler是一个事件处理,包含各种事件处理方法。h_list用于挂handle,node用于挂自己到全局的list上。

[cpp] view plain copy
  1. struct input_handle {  
  2.   
  3.     void *private;  
  4.   
  5.     int open;  
  6.     const char *name;  
  7.   
  8.     struct input_dev *dev;  
  9.     struct input_handler *handler;  
  10.   
  11.     struct list_head    d_node;  
  12.     struct list_head    h_node;  
  13. };  

input_handle是连接handler和dev的纽带。d_node用于挂dev,h_node用于挂handler。

在dev和handler匹配成功时,connect的时候会创建evdev->handle结构,handle会分别挂在已经匹配的dev和handler结构中。

[cpp] view plain copy
  1. struct input_event {  
  2.     struct timeval time;  
  3.     __u16 type;  
  4.     __u16 code;  
  5.     __s32 value;  
  6. };  
[cpp] view plain copy
  1. struct evdev {  
  2.     int open;  
  3.     struct input_handle handle;  
  4.     wait_queue_head_t wait;  
  5.     struct evdev_client __rcu *grab;  
  6.     struct list_head client_list;  
  7.     spinlock_t client_lock; /* protects client_list */  
  8.     struct mutex mutex;  
  9.     struct device dev;  
  10.     struct cdev cdev;  
  11.     bool exist;  
  12. };  
[cpp] view plain copy
  1. struct evdev_client {  
  2.     unsigned int head;  
  3.     unsigned int tail;  
  4.     unsigned int packet_head; /* [future] position of the first element of next packet */  
  5.     spinlock_t buffer_lock; /* protects access to buffer, head and tail */  
  6.     struct wake_lock wake_lock;  
  7.     bool use_wake_lock;  
  8.     char name[28];  
  9.     struct fasync_struct *fasync;  
  10.     struct evdev *evdev;  
  11.     struct list_head node;  
  12.     int clkid;  
  13.     unsigned int bufsize;  
  14.     struct input_event buffer[];  
  15. }; 
0 0