一 、 总体架构 图
上层是图形界面和应用程序,通过监听设备节点,获取用户相应的输入事件,根据输入事件来做出相应的反应;eventX (X从0开始)表示 按键事件,mice 表示鼠标事件
Input core --- input 核心
Input event drivers --- input事件驱动(mousedev 、 evdev、keyboard)
Input device drivers --- input设备驱动(触摸屏、键盘、鼠标)
三个重要的结构体:
代码路径: include\linux\input.h (基于kernel4.0)
struct input_dev { ///代表 input 设备
const char *name; /// 设备名称
const char *phys; ////设备在系统结构中的物理路径
const char *uniq; ///设备的唯一标识符
struct input_id id; ///描述硬件设备属性的 ID unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];/// INPUT_PROP_CNT--0x20 ,一个long包含32bit,所以 propbit[1] unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; ///设备支持的事件类型
unsigned long keybit[BITS_TO_LONGS(KEY_CNT)];
unsigned long relbit[BITS_TO_LONGS(REL_CNT)];
unsigned long absbit[BITS_TO_LONGS(ABS_CNT)];
unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)];
unsigned long ledbit[BITS_TO_LONGS(LED_CNT)];
unsigned long sndbit[BITS_TO_LONGS(SND_CNT)];
unsigned long ffbit[BITS_TO_LONGS(FF_CNT)];
unsigned long swbit[BITS_TO_LONGS(SW_CNT)]; unsigned int hint_events_per_packet; unsigned int keycodemax;
unsigned int keycodesize;
void *keycode; int (*setkeycode)(struct input_dev *dev,
const struct input_keymap_entry *ke,
unsigned int *old_keycode);
int (*getkeycode)(struct input_dev *dev,
struct input_keymap_entry *ke); struct ff_device *ff; unsigned int repeat_key;
struct timer_list timer; int rep[REP_CNT]; struct input_mt *mt; struct input_absinfo *absinfo; unsigned long key[BITS_TO_LONGS(KEY_CNT)];
unsigned long led[BITS_TO_LONGS(LED_CNT)];
unsigned long snd[BITS_TO_LONGS(SND_CNT)];
unsigned long sw[BITS_TO_LONGS(SW_CNT)]; int (*open)(struct input_dev *dev);///打开设备
void (*close)(struct input_dev *dev);
int (*flush)(struct input_dev *dev, struct file *file);
int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); struct input_handle __rcu *grab; spinlock_t event_lock;
struct mutex mutex; unsigned int users;
bool going_away; struct device dev;///每个设备的基类 struct list_head h_list;
struct list_head node; unsigned int num_vals;
unsigned int max_vals;
struct input_value *vals; bool devres_managed;
};
struct input_handler {///代表输入设备的处理方法 void *private; void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
void (*events)(struct input_handle *handle,
const struct input_value *vals, unsigned int count);
bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);///
bool (*match)(struct input_handler *handler, struct input_dev *dev);
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); bool legacy_minors; ///是否遗留次设备号
int minor;///次设备号
const char *name;///名称 const struct input_device_id *id_table;/// input_device 和 input_handle 通过id_table 进行匹配 struct list_head h_list;
struct list_head node;
};
Match :在设备id 和handler 的id_table 匹配成功的时候调用; (called after comparing device's id with handler's id_table to perform fine-grained matching between device and handler)
Connect : 当一个handle 匹配到一个device 的时候执行(called when attaching a handler to an input device)
Start :当input 核心执行完 connect 以后调用(called by input core right after connect() method)
struct input_handle { //// 用来关联input_dev 和 input_handler (links input device with an input handler) 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;
};
如图,横轴表示一系列的input 设备,纵轴表示事件驱动event,中间的紫色圆点代表input_handle,通过input_handle来关联设备驱动和事件驱动
(注:以上图片均来自麦子学院 金鑫老师的课程,在此对其辛勤付出和无私分享表示真挚的感谢!)