內核的輸入子系統是對分散的,多種不同類別的輸入設備(如鍵盤,鼠標,跟蹤球,操縱桿,觸摸屏,加速計和手寫板)等字符設備進行統一處理的一層抽象,就是在字符設備驅動上抽象出的一層。輸入子系統包括兩類驅動程序:事件驅動程序和設備驅動程序。事件驅動程序負責和應用程序的接口,而設備驅動程序負責和底層輸入設備的通信。鼠標事件生成文件mousedev屬於事件驅動程序,而PS/2鼠標驅動程序是設備驅動程序。事件驅動程序是標準的,對所有的輸入類都是可用的,所以要實現的是設備驅動程序而不是事件驅動程序。設備驅動程序可以利用一個已經存在的,合適的事件驅動程序通過輸入核心和用戶應用程序接口。
輸入子系統帶來了如下好處:
1.統一了物理形態各異的相似的輸入設備的處理功能
2.提供了用於分發輸入報告給用戶應用程序的簡單的事件接口
3.抽取出了輸入驅動程序的通用部分,簡化了驅動,並引入了一致性
現在 Android、X windows、qt等衆多應用對於linux系統中鍵盤、鼠標、觸摸屏等輸入設備的支持都通過、或越來越傾向於標準的input輸入子系統。因爲input子系統已經完成了字符驅動的文件操作接口,所以編寫驅動的核心工作是完成input系統留出的接口,工作量不大。但如果你想更靈活的應用它,就需要好好的分析下input子系統了。
再對照下圖(input輸入子系統框架),很清楚的知道輸入子系統是由輸入子系統核心層( Input Core ),驅動層和事件處理層(Event Handler)三部份組成。一個輸入事件,如鼠標移動,鍵盤按鍵按下,joystick的移動等等通過 input driver -> Input core -> Event handler -> userspace 到達用戶空間傳給應用程序。
注意:keyboard.c不會在/dev/input下產生節點,而是作爲ttyn終端(不包括串口終端)的輸入。
現在瞭解了input子系統的基本思想,下面來看一下input子系統的3個基本的數據結構:
- struct input_dev {
- const char *name; //名稱
- const char *phys; //設備在系統中的物理路徑
- const char *uniq; //設備唯一識別符
- struct input_id id; //設備ID,包含總線ID(PCI、USB)、廠商ID,與input_handler匹配的時會用到
- 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)]; //支持的LED燈事件
- 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 keycodemax; //keycode表的大小
- unsigned int keycodesize; //keycode表中元素個數
- void *keycode; //設備的鍵盤表
- int (*setkeycode)(struct input_dev *dev, int scancode, int keycode);//配置keycode表
- int (*getkeycode)(struct input_dev *dev, int scancode, int *keycode);//獲取keycode表
- struct ff_device *ff;
- unsigned int repeat_key;//保存上一個鍵值
- struct timer_list timer;
- int sync;
- int abs[ABS_MAX + 1]; //絕對座標上報的當前值
- int rep[REP_MAX + 1]; //這個參數主要是處理重複按鍵,後面遇到再講
- 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 absmax[ABS_MAX + 1]; //絕對座標的最大值
- int absmin[ABS_MAX + 1]; //絕對座標的最小值
- int absfuzz[ABS_MAX + 1];
- int absflat[ABS_MAX + 1];
- //操作接口
- 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 *grab; //當前使用的handle
- spinlock_t event_lock;
- struct mutex mutex;
- unsigned int users;
- int going_away;
- struct device dev;
- struct list_head h_list; //h_list是一個鏈表頭,用來把handle掛載在這個上
- struct list_head node; //這個node是用來連到input_dev_list上的
- };
-
// input_dev->evbit表示設備支持的事件類型,可以是下列值的組合
#define EV_SYN 0x00 //同步事件
#define EV_KEY 0x01 //絕對二進制值,如鍵盤或按鈕
#define EV_REL 0x02 //絕對結果,如鼠標設備
#define EV_ABS 0x03 //絕對整數值,如操縱桿或書寫板
#define EV_MSC 0x04 //其它類
#define EV_SW 0x05 //開關事件
#define EV_LED 0x11 //LED或其它指示設備
#define EV_SND 0x12 //聲音輸出,如蜂鳴器
#define EV_REP 0x14 //允許按鍵自重複
#define EV_FF 0x15 //力反饋
#define EV_PWR 0x16 //電源管理事件
include/linux/input.h中定義了支持的類型 - struct input_handler {
- void *private;
- void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
- 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);
- const struct file_operations *fops;
- int minor; //次設備號
- const char *name;
- const struct input_device_id *id_table;
- const struct input_device_id *blacklist;
- struct list_head h_list; //h_list是一個鏈表頭,用來把handle掛載在這個上
- struct list_head node; //這個node是用來連到input_handler_list上的
- };
- struct input_handle {
- void *private;
- int open;
- const char *name;
- struct input_dev *dev; //指向input_dev
- struct input_handler *handler; //指向input_handler
- struct list_head d_node; //連到input_dev的h_list上
- struct list_head h_node; //連到input_handler的h_list上
- };
- static int __init input_init(void)
- {
- int err;
- input_init_abs_bypass();
- /*創建一個類input_class*/
- err = class_register(&input_class);
- if (err) {
- printk(KERN_ERR "input: unable to register input_dev class/n");
- return err;
- }
- /*在/proc下創建入口項*/
- err = input_proc_init();
- if (err)
- goto fail1;
- /*註冊設備號INPUT_MAJOR的設備,記住input子系統的設備的主設備號都是13,即INPUT_MAJOR爲13,並與input_fops相關聯*/
- err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
- if (err) {
- printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
- goto fail2;
- }
- return 0;
- fail2: input_proc_exit();
- fail1: class_unregister(&input_class);
- return err;
- }
- subsys_initcall(input_init);
- static const struct file_operations input_fops = {
- .owner = THIS_MODULE,
- .open = input_open_file,
- };
- int input_register_device(struct input_dev *dev)
- {
- static atomic_t input_no = ATOMIC_INIT(0);
- struct input_handler *handler;
- const char *path;
- int error;
- __set_bit(EV_SYN, dev->evbit);
- /*
- * If delay and period are pre-set by the driver, then autorepeating
- * is handled by the driver itself and we don't do it in input.c.
- */
- init_timer(&dev->timer);
- /*
- *rep主要是處理重複按鍵,如果沒有定義dev->rep[REP_DELAY]和dev->rep[REP_PERIOD],
- *則將其賦值爲默認值。dev->rep[REP_DELAY]是指第一次按下多久算一次,這裏是250ms,
- *dev->rep[REP_PERIOD]指如果按鍵沒有被擡起,每33ms算一次。
- */
- if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
- dev->timer.data = (long) dev;
- dev->timer.function = input_repeat_key;
- dev->rep[REP_DELAY] = 250;
- dev->rep[REP_PERIOD] = 33;
- }
- /*如果dev沒有定義getkeycode和setkeycode,則賦默認值。他們的作用一個是獲得鍵的掃描碼,一個是設置鍵的掃描碼*/
- if (!dev->getkeycode)
- dev->getkeycode = input_default_getkeycode;
- if (!dev->setkeycode)
- dev->setkeycode = input_default_setkeycode;
- dev_set_name(&dev->dev, "input%ld",
- (unsigned long) atomic_inc_return(&input_no) - 1);
- /*將input_dev封裝的dev註冊到sysfs*/
- error = device_add(&dev->dev);
- if (error)
- return error;
- path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
- printk(KERN_INFO "input: %s as %s/n",
- dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
- kfree(path);
- error = mutex_lock_interruptible(&input_mutex);
- if (error) {
- device_del(&dev->dev);
- return error;
- }
- /*將input_dev掛在input_dev_list上*/
- list_add_tail(&dev->node, &input_dev_list);
- /*匹配所有的input_handler,這個就是剛纔那幅圖裏的一個設備對應多個handler的由來*/
- list_for_each_entry(handler, &input_handler_list, node)
- input_attach_handler(dev, handler);
- input_wakeup_procfs_readers();
- mutex_unlock(&input_mutex);
- return 0;
- }
- static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
- {
- const struct input_device_id *id;
- int error;
- /*handler有一個黑名單,如果存在黑名單,並且這個id匹配就退出*/
- if (handler->blacklist && input_match_device(handler->blacklist, dev))
- return -ENODEV;
- /*匹配id,實現在下邊可以看到*/
- id = input_match_device(handler->id_table, dev);
- if (!id)
- return -ENODEV;
- /*如果匹配,則調用具體的handler的connect函數*/
- error = handler->connect(handler, dev, id);
- if (error && error != -ENODEV)
- printk(KERN_ERR
- "input: failed to attach handler %s to device %s, "
- "error: %d/n",
- handler->name, kobject_name(&dev->dev.kobj), error);
- return error;
- }
- static const struct input_device_id *input_match_device(const struct input_device_id *id,
- struct input_dev *dev)
- {
- int i;
- for (; id->flags || id->driver_info; id++) {
- if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
- if (id->bustype != dev->id.bustype)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
- if (id->vendor != dev->id.vendor)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
- if (id->product != dev->id.product)
- continue;
- if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
- if (id->version != dev->id.version)
- continue;
- MATCH_BIT(evbit, EV_MAX);
- MATCH_BIT(keybit, KEY_MAX);
- MATCH_BIT(relbit, REL_MAX);
- MATCH_BIT(absbit, ABS_MAX);
- MATCH_BIT(mscbit, MSC_MAX);
- MATCH_BIT(ledbit, LED_MAX);
- MATCH_BIT(sndbit, SND_MAX);
- MATCH_BIT(ffbit, FF_MAX);
- MATCH_BIT(swbit, SW_MAX);
- return id;
- }
- return NULL;
- }
- #define MATCH_BIT(bit, max) /
- for (i = 0; i < BITS_TO_LONGS(max); i++) /
- if ((id->bit[i] & dev->bit[i]) != id->bit[i]) /
- break; /
- if (i != BITS_TO_LONGS(max)) /
- continue;
- /*
- * Create new evdev device. Note that input core serializes calls
- * to connect and disconnect so we don't need to lock evdev_table here.
- */
- static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
- const struct input_device_id *id)
- {
- struct evdev *evdev;
- int minor;
- int error;
- /*
- *首先補充幾個知識點:
- *static struct input_handler *input_table[8];
- *#define INPUT_DEVICES 256
- *一共有8個input_handler,對應256個設備,所以一個handler對應32個設備。
- *這個問題在我參加的一次linux驅動的面試中被問到,當時真是汗啊!!!
- *static struct evdev *evdev_table[EVDEV_MINORS];
- *#define EVDEV_MINORS 32
- *evdev理論上可對應32個設備,其對應的設備節點一般位於/dev/input/event0~/dev/input/event4
- *下邊的for循環,在evdev_table數組中找一個未使用的地方
- */
- for (minor = 0; minor < EVDEV_MINORS; minor++)
- if (!evdev_table[minor])
- break;
- if (minor == EVDEV_MINORS) {
- printk(KERN_ERR "evdev: no more free evdev devices/n");
- return -ENFILE;
- }
- /*下邊的代碼是爲每一個匹配的設備分配一個evdev結構體,並對成員進行初始化*/
- evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
- if (!evdev)
- return -ENOMEM;
- INIT_LIST_HEAD(&evdev->client_list);
- spin_lock_init(&evdev->client_lock);
- mutex_init(&evdev->mutex);
- init_waitqueue_head(&evdev->wait);
- snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
- evdev->exist = 1;
- evdev->minor = minor;
- evdev->handle.dev = input_get_device(dev);
- evdev->handle.name = evdev->name;
- evdev->handle.handler = handler;
- evdev->handle.private = evdev;
- dev_set_name(&evdev->dev, evdev->name);
- evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
- evdev->dev.class = &input_class; /*調用函數創建字符設備節點*/
- evdev->dev.parent = &dev->dev;
- evdev->dev.release = evdev_free;
- /**/
- device_initialize(&evdev->dev);
- /*
- *input_register_handle完成的主要功能是:
- *list_add_tail_rcu(&handle->d_node, &dev->h_list);
- *list_add_tail(&handle->h_node, &handler->h_list);
- */
- error = input_register_handle(&evdev->handle);
- if (error)
- goto err_free_evdev;
- /*evdev_install_chrdev完成的功能是evdev_table[evdev->minor]=evdev;*/
- error = evdev_install_chrdev(evdev);
- if (error)
- goto err_unregister_handle;
- error = device_add(&evdev->dev);
- if (error)
- goto err_cleanup_evdev;
- return 0;
- 。。。。。。。。。。
- }
- struct input_event {
- struct timeval time; //事件發生的時間
- __u16 type; //事件類型
- __u16 code; //子事件
- __s32 value; //事件的value
- };
- struct evdev_client {
- struct input_event buffer[EVDEV_BUFFER_SIZE]; //可以同時管理EVDEV_BUFFER_SIZE(64)個事件
- int head; //存儲事件從head開始
- int tail; //取出事件從tail開始
- spinlock_t buffer_lock; /* protects access to buffer, head and tail */
- struct fasync_struct *fasync; //異步通知事件發生
- struct evdev *evdev; //指向本evdev_client歸屬的evdev
- struct list_head node; //用於掛載到evdev的鏈表頭client_list上
- };
- static struct input_handler evdev_handler = {
- .event = evdev_event, //向系統報告input事件,系統通過read方法讀取
- .connect = evdev_connect, //和input_dev匹配後調用connect構建
- .disconnect = evdev_disconnect,
- .fops = &evdev_fops, //event設備文件的操作方法
- .minor = EVDEV_MINOR_BASE,//次設備號基準值
- .name = "evdev",
- .id_table = evdev_ids, //匹配規則
- };
如下一個結構體:evdev_handler匹配所有設備。
- static const struct input_device_id evdev_ids[] = {
- { .driver_info = 1 }, /* Matches all devices */
- { }, /* Terminating zero entry */
- };
這個是evdev_handler是fops,下面的講解中會用到其中的open,read函數。
- 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
- };
設備驅動通過宏set_bit()告訴input子系統它支持哪些事件,如下所示
struct input_dev中有兩個成員,一個是unsigned long evbit,一個是unsigned long keybit,分別用來表示設備所支持的事件類型和按鍵類型。
用於報告EV_KEY、EV_REL、EV_ABS、EV_FF、EV_SW等事件的函數有:
void input_report_key(struct input_dev *dev, unsigned int code, int value)
void input_report_rel(struct input_dev *dev, unsigned int code, int value)
void input_report_abs(struct input_dev *dev, unsigned int code, int value)
void input_report_ff_status(struct input_dev *dev, unsigned int code, int value)
void input_report_switch(struct input_dev *dev, unsigned int code, int value)
如果你覺得麻煩,你也可以只記住1個函數(因爲上述函數都是通過它實現的)
void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
code:
void input_sync(struct input_dev *dev)
{
input_event(dev,EV_SYN,SYN_REPORT,0);
}
這一點在鼠標移動處理中很重要,因爲鼠標座標的X分量和Y分量是分開傳送的,需要利用input_sync函數來同步。
跟蹤input_event如下:
- void input_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- unsigned long flags;
- if (is_event_supported(type, dev->evbit, EV_MAX)) {
- spin_lock_irqsave(&dev->event_lock, flags);
- /*利用輸入值調正隨機數產生器*/
- add_input_randomness(type, code, value);
- input_handle_event(dev, type, code, value);
- spin_unlock_irqrestore(&dev->event_lock, flags);
- }
- }
- static void input_handle_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- int disposition = INPUT_IGNORE_EVENT;
- switch (type) {
- 。。。。。。。。。。。。。。。。
- if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
- dev->sync = 0;
- if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
- dev->event(dev, type, code, value);
- if (disposition & INPUT_PASS_TO_HANDLERS)
- input_pass_event(dev, type, code, value);
- }
跟蹤input_pass_event如下:
- static void input_pass_event(struct input_dev *dev,
- unsigned int type, unsigned int code, int value)
- {
- struct input_handle *handle;
- rcu_read_lock();
- /**/
- handle = rcu_dereference(dev->grab);
- if (handle)
- /*如果input_dev的grab指向了一個handle,就用這個handle關聯的handler的event,否則遍歷整個掛在input_dev的h_list上的handle關聯的handler*/
- handle->handler->event(handle, type, code, value);
- else
- list_for_each_entry_rcu(handle, &dev->h_list, d_node)
- if (handle->open)
- handle->handler->event(handle,
- type, code, value);
- rcu_read_unlock();
- }
- static void evdev_event(struct input_handle *handle,
- unsigned int type, unsigned int code, int value)
- {
- struct evdev *evdev = handle->private;
- struct evdev_client *client;
- struct input_event event;
- do_gettimeofday(&event.time);
- event.type = type;
- event.code = code;
- event.value = value;
- rcu_read_lock();
- client = rcu_dereference(evdev->grab);
- if (client)
- /*如果evdev->grab指向一個當前使用的client就將event放到這個client的buffer中,否則放到整個client_list上的client的鏈表中*/
- evdev_pass_event(client, &event);
- else
- list_for_each_entry_rcu(client, &evdev->client_list, node)
- evdev_pass_event(client, &event);
- rcu_read_unlock();
- wake_up_interruptible(&evdev->wait);
- }
- static void evdev_pass_event(struct evdev_client *client,
- struct input_event *event)
- {
- /*
- * Interrupts are disabled, just acquire the lock
- */
- spin_lock(&client->buffer_lock);
- /*將event裝入client的buffer中,buffer是一個環形緩存區*/
- client->buffer[client->head++] = *event;
- client->head &= EVDEV_BUFFER_SIZE - 1;
- spin_unlock(&client->buffer_lock);
- kill_fasync(&client->fasync, SIGIO, POLL_IN);
- }
這裏總結一下事件的傳遞過程:首先在驅動層中,調用inport_report_abs,然後他調用了input core層的input_event,input_event調用了input_handle_event對事件進行分派,調用input_pass_event,在這裏他會把事件傳遞給具體的handler層,然後在相應handler的event處理函數中,封裝一個event,然後把它投入evdev的那個client_list上的client的事件buffer中,等待用戶空間來讀取。
當用戶空間打開設備節點/dev/input/event0~/dev/input/event4的時候,會使用input_fops中的input_open_file()函數,input_open_file()->evdev_open()(如果handler是evdev的話)->evdev_open_device()->input_open_device()->dev->open()。也就是struct
file_operations input_fops提供了通用接口,最終會調用具體input_dev的open函數。下邊看一下用戶程序打開文件時的過程,首先調用了input_open_file:
- static int input_open_file(struct inode *inode, struct file *file)
- {
- struct input_handler *handler;
- const struct file_operations *old_fops, *new_fops = NULL;
- int err;
- lock_kernel();
- /* No load-on-demand here? */
- /*因爲32個input_dev公共一個handler所以低5位應該是相同的*/
- handler = input_table[iminor(inode) >> 5];
- if (!handler || !(new_fops = fops_get(handler->fops))) {
- err = -ENODEV;
- goto out;
- }
- /*
- * That's _really_ odd. Usually NULL ->open means "nothing special",
- * not "no device". Oh, well...
- */
- if (!new_fops->open) {
- fops_put(new_fops);
- err = -ENODEV;
- goto out;
- }
- /*保存以前的fops,使用相應的handler的fops*/
- old_fops = file->f_op;
- file->f_op = new_fops;
- err = new_fops->open(inode, file);
- if (err) {
- fops_put(file->f_op);
- file->f_op = fops_get(old_fops);
- }
- fops_put(old_fops);
- out:
- unlock_kernel();
- return err;
- }
- static int evdev_open(struct inode *inode, struct file *file)
- {
- struct evdev *evdev;
- struct evdev_client *client;
- /*因爲次設備號是從EVDEV_MINOR_BASE開始的*/
- int i = iminor(inode) - EVDEV_MINOR_BASE;
- int error;
- if (i >= EVDEV_MINORS)
- return -ENODEV;
- error = mutex_lock_interruptible(&evdev_table_mutex);
- if (error)
- return error;
- /*evdev_table一共可容納32個成員,找到次設備號對應的那個*/
- evdev = evdev_table[i];
- if (evdev)
- get_device(&evdev->dev);
- mutex_unlock(&evdev_table_mutex);
- if (!evdev)
- return -ENODEV;
- /*打開的時候創建一個client*/
- client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);
- if (!client) {
- error = -ENOMEM;
- goto err_put_evdev;
- }
- spin_lock_init(&client->buffer_lock);
- /*下邊兩句的作用就是將evdev和client綁定到一起*/
- client->evdev = evdev;
- evdev_attach_client(evdev, client);
- error = evdev_open_device(evdev);
- if (error)
- goto err_free_client;
- /*將file->private_data指向剛剛建的client,後邊會用到的*/
- file->private_data = client;
- return 0;
- err_free_client:
- evdev_detach_client(evdev, client);
- kfree(client);
- err_put_evdev:
- put_device(&evdev->dev);
- return error;
- }
- static int evdev_open_device(struct evdev *evdev)
- {
- int retval;
- retval = mutex_lock_interruptible(&evdev->mutex);
- if (retval)
- return retval;
- /*如果設備不存在,返回錯誤*/
- if (!evdev->exist)
- retval = -ENODEV;
- /*如果是被第一次打開,則調用input_open_device*/
- else if (!evdev->open++) {
- retval = input_open_device(&evdev->handle);
- if (retval)
- evdev->open--;
- }
- mutex_unlock(&evdev->mutex);
- return retval;
- }
- int input_open_device(struct input_handle *handle)
- {
- struct input_dev *dev = handle->dev;
- int retval;
- retval = mutex_lock_interruptible(&dev->mutex);
- if (retval)
- return retval;
- if (dev->going_away) {
- retval = -ENODEV;
- goto out;
- }
- handle->open++;
- if (!dev->users++ && dev->open)
- retval = dev->open(dev);
- if (retval) {
- dev->users--;
- if (!--handle->open) {
- /*
- * Make sure we are not delivering any more events
- * through this handle
- */
- synchronize_rcu();
- }
- }
- out:
- mutex_unlock(&dev->mutex);
- return retval;
- }
- static ssize_t evdev_read(struct file *file, char __user *buffer,
- size_t count, loff_t *ppos)
- {
- /*這個就是剛纔在open函數中*/
- struct evdev_client *client = file->private_data;
- struct evdev *evdev = client->evdev;
- struct input_event event;
- int retval;
- if (count < input_event_size())
- return -EINVAL;
- /*如果client的環形緩衝區中沒有數據並且是非阻塞的,那麼返回-EAGAIN,也就是try again*/
- if (client->head == client->tail && evdev->exist &&
- (file->f_flags & O_NONBLOCK))
- return -EAGAIN;
- /*如果沒有數據,並且是阻塞的,則在等待隊列上等待吧*/
- retval = wait_event_interruptible(evdev->wait,
- client->head != client->tail || !evdev->exist);
- if (retval)
- return retval;
- if (!evdev->exist)
- return -ENODEV;
- /*如果獲得了數據則取出來,調用evdev_fetch_next_event*/
- while (retval + input_event_size() <= count &&
- evdev_fetch_next_event(client, &event)) {
- /*input_event_to_user調用copy_to_user傳入用戶程序中,這樣讀取完成*/
- if (input_event_to_user(buffer + retval, &event))
- return -EFAULT;
- retval += input_event_size();
- }
- return retval;
- }
- static int evdev_fetch_next_event(struct evdev_client *client,
- struct input_event *event)
- {
- int have_event;
- spin_lock_irq(&client->buffer_lock);
- /*先判斷一下是否有數據*/
- have_event = client->head != client->tail;
- /*如果有就從環形緩衝區的取出來,記得是從head存儲,tail取出*/
- if (have_event) {
- *event = client->buffer[client->tail++];
- client->tail &= EVDEV_BUFFER_SIZE - 1;
- }
- spin_unlock_irq(&client->buffer_lock);
- return have_event;
- }
- int input_event_to_user(char __user *buffer,
- const struct input_event *event)
- {
- /*如果設置了標誌INPUT_COMPAT_TEST就將事件event包裝成結構體compat_event*/
- if (INPUT_COMPAT_TEST) {
- struct input_event_compat compat_event;
- compat_event.time.tv_sec = event->time.tv_sec;
- compat_event.time.tv_usec = event->time.tv_usec;
- compat_event.type = event->type;
- compat_event.code = event->code;
- compat_event.value = event->value;
- /*將包裝成的compat_event拷貝到用戶空間*/
- if (copy_to_user(buffer, &compat_event,
- sizeof(struct input_event_compat)))
- return -EFAULT;
- } else {
- /*否則,將event拷貝到用戶空間*/
- if (copy_to_user(buffer, event, sizeof(struct input_event)))
- return -EFAULT;
- }
- return 0;
- }