http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/
分类:
2012-12-14 18:17:23
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终端(不包括串口终端)的输入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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上 }; |
如 下图代表了input_dev,input_handler,input_handle,3者之间的关系。一类handler可以和多个硬件设备相关联, 一个硬件设备可以和多个handler相关联。例如:一个触摸屏设备可以作为一个event设备,作为一个鼠标设备,也可以作为一个触摸设备,所以一个设 备需要与多个平台驱动进行连接。而一个平台驱动也不只为一个设备服务,一个触摸平台驱动可能要为A,B,C3个触摸设备提供上层驱动,所以需要这样一对多 的连接。
下面来看看input字符设备注册过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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); |
下面来看input子系统的file_operations,这里只有一个打开函数input_open_file,这个在事件传递部分讲解。
1 2 3 4 | static const struct file_operations input_fops = { .owner = THIS_MODULE, .open = input_open_file, }; |
下边来看input_dev设备的注册:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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; } |
跟踪程序,来看看input_attach_handler的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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; } |
下边来看看这个匹配函数:如果id->flags存在,并且相应的标志为被设定则进行比较。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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; |
Input_dev和input_handler匹配后调用input_handler的connect。以evdev_handler为例:如果匹配上了就会创建一个evdev,它里边封装了一个handle,会把input_dev和input_handler关联到一起。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /* * 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; 。。。。。。。。。。 } |
input子系统最重要的部分就是向上层report了。这里还是先介绍几个数据结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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_MINOR_BASE(64),也就是说evdev_handler所表示的设备文件范围(13,64)~(13,64+32)。
如下一个结构体:evdev_handler匹配所有设备。
1 2 3 4 | static const struct input_device_id evdev_ids[] = { { .driver_info = 1 }, /* Matches all devices */ { }, /* Terminating zero entry */ }; |
看一下这张图会对上边的结构有清楚的认知了:
这个是evdev_handler是fops,下面的讲解中会用到其中的open,read函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 }; |
在驱动程序中我们会调用input_report_abs等函数:
set_bit(EV_KEY, input_dev->keybit); //EV_KEY事件支持的事件码
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)
跟踪input_event如下:
1 2 3 4 5 6 7 8 9 10 | 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); } } |
跟踪input_handle_event如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 device来完成,就会将disposition设置成INPUT_PASS_TO_DEVICE,如果需要input
handler来完成,就会将disposition设置成INPUT_PASS_TO_DEVICE,如果需要两者都参与,则将disposition
设置成INPUT_PASS_TO_ALL。
跟踪input_pass_event如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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(); } |
比如下边的evdev_handler的evdev_event:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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; } |
这里还是假设handler是evdev_handler。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | 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; } |
下面是用户进程读取event的底层实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | 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; } |
这
里总结一下:如果两个进程打开同一个文件,每个进程在打开时都会生成一个evdev_client,evdev_client被挂在evdev的
client_list,在handle收到一个事件的时候,会把事件copy到挂在client_list上的所有evdev_client的
buffer中。这样所有打开同一个设备的进程都会收到这个消息而唤醒。