濰坊做網(wǎng)站的那家好網(wǎng)絡(luò)推廣公司名字大全
目錄
- 一、input子系統(tǒng)
- 二、關(guān)鍵數(shù)據(jù)結(jié)構(gòu)和api
- 2.1 數(shù)據(jù)結(jié)構(gòu)
- 2.1.1 input_dev
- 2.1.2 input_handler
- 2.1.3 input_event
- 2.1.4 input_handle
- 2.2 api接口
- 2.2.1 input_device 相關(guān)接口
- input_device 注冊(cè)流程
- 事件上報(bào)
- 2.2.2 input handle 相關(guān)接口
- 注冊(cè) handle
- 指定 handle
- 2.2.3 input handler 相關(guān)接口
- 注冊(cè)handler
- 三、input handler
- 3.1 evdev handler
- 3.1.1 handler 注冊(cè)
- 3.1.2 evdev_connect
- 3.1.3 evdev_events
- 3.1.4 file_operations
- 3.2 mousedev handler
一、input子系統(tǒng)
input子系統(tǒng)處理Linux下的輸入事件。
驅(qū)動(dòng)層:輸入設(shè)備的驅(qū)動(dòng)程序,負(fù)責(zé)檢測(cè)和接收輸入設(shè)備的輸入事件,將輸入事件上報(bào)給核心層;
核心層:提供設(shè)備驅(qū)動(dòng)、事件 handler 注冊(cè)和操作的接口;接收驅(qū)動(dòng)層的輸入事件并上報(bào)給事件處理層;
事件處理層:通過(guò)提供 sysfs 接口等方式和用戶(hù)空間交互,例如用戶(hù)空間打開(kāi)特定設(shè)備,當(dāng)有輸入數(shù)據(jù)時(shí)就會(huì)上傳給用戶(hù)空間。
input子系統(tǒng)框架結(jié)構(gòu)圖(總結(jié)來(lái)自這里):
input driver 接收到硬件的輸入事件 ==> 發(fā)送到input core,input core 根據(jù)事件類(lèi)型 ==> 將事件交給對(duì)應(yīng)的input handler處理 ==> input handler 上報(bào)用戶(hù)空間,用戶(hù)空間收收到事件后進(jìn)行對(duì)應(yīng)的處理。
二、關(guān)鍵數(shù)據(jù)結(jié)構(gòu)和api
2.1 數(shù)據(jù)結(jié)構(gòu)
2.1.1 input_dev
input_dev 描述輸入設(shè)備,結(jié)構(gòu)體中的多個(gè) bitmap 描述了輸入設(shè)備的類(lèi)型和支持的輸入事件。這些事件類(lèi)型相關(guān)的宏定義在 input-event-codes.h 頭文件中。
struct input_dev {const char *name;const char *phys;const char *uniq;struct input_id id;unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; // 設(shè)備支持的事件類(lèi)型的bitmapunsigned long keybit[BITS_TO_LONGS(KEY_CNT)]; // 設(shè)備支持的按鍵類(lèi)型unsigned long relbit[BITS_TO_LONGS(REL_CNT)]; // 設(shè)備支持的相對(duì)坐標(biāo)事件unsigned long absbit[BITS_TO_LONGS(ABS_CNT)]; // 設(shè)備支持的絕對(duì)坐標(biāo)事件unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)]; // 設(shè)備支持的雜項(xiàng)事件unsigned long ledbit[BITS_TO_LONGS(LED_CNT)]; // ledunsigned long sndbit[BITS_TO_LONGS(SND_CNT)]; // 聲音unsigned long ffbit[BITS_TO_LONGS(FF_CNT)]; // 壓力反饋事件unsigned long swbit[BITS_TO_LONGS(SW_CNT)]; // 開(kāi)關(guān)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;struct input_dev_poller *poller;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;ktime_t timestamp[INPUT_CLK_MAX];
};
2.1.2 input_handler
input_handler 提供了對(duì)一類(lèi)設(shè)備輸入事件處理的接口。
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;struct list_head h_list;struct list_head node;
};
以 evdev handler 為例,
connect 接口,通過(guò) input_register_handle 接口,實(shí)現(xiàn)將 input_dev 和 input_handler 綁定,并創(chuàng)建對(duì)應(yīng)輸入設(shè)備的字符設(shè)備;
event/events 接口,將設(shè)備的輸入拷貝到 buffer 中,當(dāng)用戶(hù)空間調(diào)用字符設(shè)備的 read 接口時(shí),就可以從 buffer 中讀取輸入信息;
2.1.3 input_event
handler 上報(bào)事件到用戶(hù)層的時(shí)候,以 input_event 格式進(jìn)行上報(bào)。
struct input_event {struct timeval time; // 事件發(fā)生事件__u16 type; // 事件類(lèi)型,例如 EV_KEY 按鍵類(lèi)型__u16 code; // 事件編碼,例如 KEY_0 按鍵__s32 value; // 事件值
};
2.1.4 input_handle
input_handle 實(shí)現(xiàn)將 input_device 和 input_handler 綁定的功能,上面已經(jīng)介紹到,evdev handler 的 connect 接口中,會(huì)調(diào)用 input_register_handle 接口,實(shí)現(xiàn)將 input_dev 和 input_handler 綁定。
struct input_handle {void *private;int open; // 當(dāng)前handle是否openconst char *name;struct input_dev *dev;struct input_handler *handler;struct list_head d_node;struct list_head h_node;
};
open 記錄了當(dāng)前 handle 是否被 open,以 evdev 為例,當(dāng)用戶(hù)空間 open 字符設(shè)備的時(shí)候,會(huì)調(diào)用到input_open_device 接口,接口內(nèi)部實(shí)現(xiàn) input_handle->open++。
2.2 api接口
2.2.1 input_device 相關(guān)接口
input核心層提供了如下一系列input device相關(guān)的接口,事件input device的注冊(cè)、事件的上報(bào)等功能:
// 申請(qǐng)
struct input_dev *input_allocate_device(void);
struct input_dev *devm_input_allocate_device(struct device *dev);// 設(shè)置支持的事件類(lèi)型
void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code);// 注冊(cè)、注銷(xiāo)
int input_register_device(struct input_dev *dev);
void input_unregister_device(struct input_dev *dev);// 釋放
void input_free_device(struct input_dev *dev);// 事件上報(bào)
void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value);
void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value);
static inline void input_report_key(struct input_dev *dev, unsigned int code, int value);
static inline void input_report_rel(struct input_dev *dev, unsigned int code, int value);
static inline void input_report_abs(struct input_dev *dev, unsigned int code, int value)
static inline void input_report_ff_status(struct input_dev *dev, unsigned int code, int value);
static inline void input_report_switch(struct input_dev *dev, unsigned int code, int value);
static inline void input_sync(struct input_dev *dev); // 同步通知事件發(fā)送完成
static inline void input_mt_sync(struct input_dev *dev);
input_device 注冊(cè)流程
注冊(cè)接口中主要做了以下動(dòng)作:
- 檢查 bitmap 等參數(shù)設(shè)置是否正確;
- 將 device 添加到 input_device_list 鏈表中;
- 對(duì)于 input_handler_list 中的每一個(gè) handler,調(diào)用 input_attach_handler 接口嘗試將 device 和 handler 綁定,在接口內(nèi)部會(huì)檢查 device 和 handler 是否 match,match 的話則調(diào)用 handler 的 connect 接口完成綁定動(dòng)作。
int input_register_device(struct input_dev *dev)
{// 檢查bitmap等參數(shù)、配置input_dev部分參數(shù)/* Every input device generates EV_SYN/SYN_REPORT events. */__set_bit(EV_SYN, dev->evbit);/* KEY_RESERVED is not supposed to be transmitted to userspace. */__clear_bit(KEY_RESERVED, dev->keybit);/* Make sure that bitmasks not mentioned in dev->evbit are clean. */input_cleanse_bitmasks(dev);dev->max_vals = dev->hint_events_per_packet + 2;dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);// device_adderror = device_add(&dev->dev);// device 和 handler綁定error = mutex_lock_interruptible(&input_mutex);list_add_tail(&dev->node, &input_dev_list);list_for_each_entry(handler, &input_handler_list, node)input_attach_handler(dev, handler);input_wakeup_procfs_readers();mutex_unlock(&input_mutex);if (dev->devres_managed) {dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",__func__, dev_name(&dev->dev));devres_add(dev->dev.parent, devres);}return 0;
}
EXPORT_SYMBOL(input_register_device);
事件上報(bào)
input 子系統(tǒng)中封裝了針對(duì)不同類(lèi)型事件的上報(bào)接口,例如 input_report_key\input_report_abs 等,這些接口實(shí)際都是調(diào)用 input_event 接口完成事件上報(bào),只不過(guò)接口參數(shù)中的 type 類(lèi)型不同,以 input_report_key 為例:
input_report_key(struct input_dev *dev, unsigned int code, int value)-> input_event(dev, EV_KEY, code, !!value);-> input_handle_event(dev, type, code, value);-> input_get_disposition(dev, type, code, &value); // 獲取事件類(lèi)型-> input_pass_values(dev, dev->vals, dev->num_vals); -> input_to_handler(handle, vals, count);-> handler->events(handle, vals, count); // 通知handler處理事件
在 input_handle_event 接口中,會(huì)將事件緩存在 dev->vals 中,并記錄事件數(shù)目到 dev-num_vals,當(dāng)檢測(cè)到 dev->num_vals >= dev->max_vals - 2 或者 input_sync 事件時(shí),將所有緩存事件通知 handler 處理。
static void input_handle_event(struct input_dev *dev,unsigned int type, unsigned int code, int value)
{// 獲取事件類(lèi)型int disposition = input_get_disposition(dev, type, code, &value);if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)add_input_randomness(type, code, value);// 如果 INPUT_PASS_TO_DEVICE并且device實(shí)現(xiàn)了event,則通知deviceif ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)dev->event(dev, type, code, value);if (!dev->vals)return;// 記錄要通知給handler的事件if (disposition & INPUT_PASS_TO_HANDLERS) {struct input_value *v;if (disposition & INPUT_SLOT) {v = &dev->vals[dev->num_vals++];v->type = EV_ABS;v->code = ABS_MT_SLOT;v->value = dev->mt->slot;}v = &dev->vals[dev->num_vals++];v->type = type;v->code = code;v->value = value;}// sync事件或者要超出緩存,則將緩存的vals flush到handlerif (disposition & INPUT_FLUSH) {if (dev->num_vals >= 2)input_pass_values(dev, dev->vals, dev->num_vals);dev->num_vals = 0;dev->timestamp[INPUT_CLK_MONO] = ktime_set(0, 0);} else if (dev->num_vals >= dev->max_vals - 2) {dev->vals[dev->num_vals++] = input_value_sync;input_pass_values(dev, dev->vals, dev->num_vals);dev->num_vals = 0;}}
2.2.2 input handle 相關(guān)接口
int input_register_handle(struct input_handle *handle);
void input_unregister_handle(struct input_handle *handle);
注冊(cè) handle
input_register_handle 接口實(shí)現(xiàn)注冊(cè)一個(gè)input_handle,將 device 和 handler 綁定,例如在 evdev handler 的 connect 接口中,就調(diào)用了 input_register_handle 接口。
接口流程:
int input_register_handle(struct input_handle *handle)
{// 將 handle->d_node 加入到 dev->h_list,實(shí)現(xiàn)遍歷dev->h_list就能找到所有關(guān)聯(lián)的input_handle,進(jìn)而找到input_handlerif (handler->filter)list_add_rcu(&handle->d_node, &dev->h_list);elselist_add_tail_rcu(&handle->d_node, &dev->h_list);// 將 handle->h_node 加入到 handler->h_list,實(shí)現(xiàn)遍歷handler->h_list就能找到所有關(guān)聯(lián)的input_handler,進(jìn)而找到input_devicelist_add_tail_rcu(&handle->h_node, &handler->h_list);if (handler->start)handler->start(handle);return 0;
}
在 input_register_handle 接口中,會(huì)將 handle->d_node 加入到 dev->h_list,實(shí)現(xiàn)遍歷dev->h_list就能找到所有關(guān)聯(lián)的input_handle,進(jìn)而找到input_handler。
實(shí)際上在 input_pass_values 中,如果未指定 input_device 的 input_handle, 就是通過(guò)遍歷列表的方式,將事件通過(guò)所有關(guān)聯(lián)的 input_handle 發(fā)送到 input_handler 中。
也就是說(shuō)默認(rèn)input_event的事件上報(bào)是一個(gè)廣播行為:
handle = rcu_dereference(dev->grab);if (handle) {count = input_to_handler(handle, vals, count); // 指定handle} else {list_for_each_entry_rcu(handle, &dev->h_list, d_node) // 廣播if (handle->open) {count = input_to_handler(handle, vals, count);if (!count)break;}}
指定 handle
在 input_grab_device 接口中,實(shí)現(xiàn)了 dev->grab 與 handle 的綁定:
int input_grab_device(struct input_handle *handle)
{if (dev->grab) {retval = -EBUSY;goto out;}rcu_assign_pointer(dev->grab, handle);
}
以 evdev handler 為例,ioctl 中實(shí)現(xiàn)了 EVIOCGRAB,用于 input_device 指定 input_handle:
// ioctl接口中,調(diào)用evdev_grab或evdev_ungrab事件綁定和解綁:case EVIOCGRAB:if (p)return evdev_grab(evdev, client);elsereturn evdev_ungrab(evdev, client);// evcev_grab中調(diào)用 input_grab_device 實(shí)現(xiàn) dev->grab 與 handle 的綁定
static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
{int error;if (evdev->grab)return -EBUSY;error = input_grab_device(&evdev->handle);if (error)return error;rcu_assign_pointer(evdev->grab, client);return 0;
}
2.2.3 input handler 相關(guān)接口
// 注冊(cè)、注銷(xiāo)
int input_register_handler(struct input_handler *handler);
void input_unregister_handler(struct input_handler *handler);
注冊(cè)handler
input_register_handler 接口中,將 handler 添加到 input_handler_list 中,遍歷 input_dev_list,執(zhí)行input_attach_handler(dev, handler):
int input_register_handler(struct input_handler *handler)
{INIT_LIST_HEAD(&handler->h_list);list_add_tail(&handler->node, &input_handler_list);list_for_each_entry(dev, &input_dev_list, node)input_attach_handler(dev, handler);return 0;
}
三、input handler
3.1 evdev handler
3.1.1 handler 注冊(cè)
在evdev_init中調(diào)用 input_register_handler 實(shí)現(xiàn) handler 的注冊(cè)。
static struct input_handler evdev_handler = {.event = evdev_event,.events = evdev_events,.connect = evdev_connect,.disconnect = evdev_disconnect,.legacy_minors = true,.minor = EVDEV_MINOR_BASE,.name = "evdev",.id_table = evdev_ids,
};static int __init evdev_init(void)
{return input_register_handler(&evdev_handler);
}
3.1.2 evdev_connect
evdev_connect 接口在 input_attach_handler 中被調(diào)用,接口實(shí)現(xiàn)以下功能:
- 以 evdev_fops 為 file_operations 創(chuàng)建 cdev,設(shè)備名稱(chēng)為 event%d;
- 調(diào)用 input_register_handle 實(shí)現(xiàn) input_device 與 input_handler 的綁定;
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,const struct input_device_id *id)
{minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);INIT_LIST_HEAD(&evdev->client_list);dev_no = minor;dev_set_name(&evdev->dev, "event%d", dev_no);evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);evdev->dev.class = &input_class;evdev->dev.parent = &dev->dev;evdev->dev.release = evdev_free;device_initialize(&evdev->dev);error = input_register_handle(&evdev->handle);cdev_init(&evdev->cdev, &evdev_fops);error = cdev_device_add(&evdev->cdev, &evdev->dev);}
3.1.3 evdev_events
evdev_events 接口負(fù)責(zé)處理 input_device 上報(bào)的事件,并上報(bào)給用戶(hù)層:
handler->events(handle, vals, count); // evdev_events,讀時(shí)間--> evdev_pass_values(client, vals, count, ev_time); // 組input_event--> __pass_event(client, &event); // 將event存在evdev_client的buffer中--> kill_fasync(&client->fasync, SIGIO, POLL_IN); // 異步信號(hào)通知用戶(hù)層--> wake_up_interruptible(&evdev->wait); // 喚醒等待隊(duì)列
3.1.4 file_operations
evdev handler 的 file_operations 提供了 fasync\poll\read 等接口,供用戶(hù)層讀取 input event。
static const struct file_operations evdev_fops = {.owner = THIS_MODULE,.read = evdev_read,.write = evdev_write,.poll = evdev_poll,.open = evdev_open,.release = evdev_release,.unlocked_ioctl = evdev_ioctl,
#ifdef CONFIG_COMPAT.compat_ioctl = evdev_ioctl_compat,
#endif.fasync = evdev_fasync,.flush = evdev_flush,.llseek = no_llseek,
};
evdev_fasync
接口實(shí)現(xiàn)異步通知處理函數(shù),當(dāng)有input_event事件時(shí),在evdev_events接口中,最終會(huì)調(diào)用 kill_fasync
實(shí)現(xiàn)發(fā)送異步通知信號(hào),用戶(hù)層接收到狀態(tài)變化后,可知曉有input_event事件需要處理。
evdev_read
接口為用戶(hù)空間提供了讀input_event事件的接口,實(shí)際是將evdev_events接口中緩存在buffer中的數(shù)據(jù)copy到用戶(hù)空間。當(dāng)緩存中沒(méi)有數(shù)據(jù)是,調(diào)用wait_event_interruptible
等待 evdev_events 喚醒。
3.2 mousedev handler
TODO
參考鏈接:https://www.cnblogs.com/arnoldlu/p/17952329