注:内核版本为 kernel-2.6.30.4
一、input子系统初始化
整个input子系统是作为一个字符设备驱动,它们的主设备号是13,不同的设备次设备号不同,input驱动注册的时候提供的fops
里面只包括一个open函数.
static const struct file_operations input_fops = { .owner = THIS_MODULE, .open = input_open_file, }; static int __init input_init(void) { int err; input_init_abs_bypass(); err = class_register(&input_class);//新建类 if (err) { printk(KERN_ERR "input: unable to register input_dev class\n"); return err; } err = input_proc_init();//新建/proc入口 if (err) goto fail1; err = register_chrdev(INPUT_MAJOR, "input", &input_fops);//注册输入子系统,主设备号为13 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; } static int input_open_file(struct inode *inode, struct file *file) { struct input_handler *handler; /*static struct input_handler *input_table[8];input子系统最多维护8个事件处理方法 *每个事件处理方法可以处理32个次设备号 *调用input_register_handler的时候将input_handler按次设备号放在该数组中 */ handler = input_table[iminor(inode)>>5];//根据次设备号,获取该设备input_handler if (!handler || !(new_fops = fops_get(handler->fops))) { //提取handler里面的file_operations err = -ENODEV; goto out; } file->f_op = new_fops; //将设备的fops赋值给它的文件描述符的f_op err = new_fops->open(inode, file); //调用handler->fops->open实现文件的打开 }
二、设备驱动层设备链表建立过程
1.分配struct input_dev结构体
struct input_dev *input_allocate_device(void) { struct input_dev *dev;dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);//分配一个struct input_dev结构体 if (dev) {dev->dev.type = &input_dev_type; dev->dev.class = &input_class; device_initialize(&dev->dev); mutex_init(&dev->mutex); spin_lock_init(&dev->event_lock); INIT_LIST_HEAD(&dev->h_list);//初始该设备的input_handle链表头 INIT_LIST_HEAD(&dev->node); __module_get(THIS_MODULE); return dev; }
2.设置struct input_dev结构体
/* 能产生哪类事件 */ set_bit(EV_KEY, buttons_dev->evbit);//能产生按键类型事件 set_bit(EV_REP, buttons_dev->evbit);//能产生重复类型事件 /* 能产生该类操作里的哪些事件: L,S,ENTER,LEFTSHIT */ set_bit(KEY_L, buttons_dev->keybit);//能产生按键类型下的L键事件 set_bit(KEY_S, buttons_dev->keybit);//能产生按键类型下的S键事件 set_bit(KEY_ENTER, buttons_dev->keybit);//能产生按键类型下的ENTER键事件 set_bit(KEY_LEFTSHIFT, buttons_dev->keybit);//能产生按键类型下的SHIFT键事件
3.注册struct input_dev结构体
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); 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; } 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); error = device_add(&dev->dev);//将设备注册到设备模型中去,这样在/sys目录下将新建一个以目录 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; } list_add_tail(&dev->node, &input_dev_list);//将input_dev加入到input子系统维护的全局input_dev链表中去 list_for_each_entry(handler, &input_handler_list, node)//取出input子系统维护的全局input_handler链表的元素 input_attach_handler(dev, handler);//找出能处理新注册的input_dev的设备方法input_handler input_wakeup_procfs_readers(); mutex_unlock(&input_mutex); return 0; }4.input_dev和input_handler匹配过程
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) { const struct input_device_id *id; int error; if (handler->blacklist && input_match_device(handler->blacklist, dev))//input_dev在input_handler的黑名单中,就直接返回 return -ENODEV; id = input_match_device(handler->id_table, dev);//比较input_handler的id_table是否支持input_dev if (!id) return -ENODEV; error = handler->connect(handler, dev, id);//如果input_hander能支持该设备就调用它的connect函数 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; }
input_dev和input_handler匹配成功后将调用handler的connect函数,现在以内核中evdev提供的evdev_handler的connect为例
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; 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 = 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); error = input_register_handle(&evdev->handle);//新建一个input_handle,用于联系匹配的input_dev和input_handler if (error) goto err_free_evdev; error = evdev_install_chrdev(evdev); if (error) goto err_unregister_handle; error = device_add(&evdev->dev);//将该evdev加入到设备模型中去 if (error) goto err_cleanup_evdev; return 0; err_cleanup_evdev: evdev_cleanup(evdev); err_unregister_handle: input_unregister_handle(&evdev->handle); err_free_evdev: put_device(&evdev->dev); return error; }
input_register_handle用于将handle挂靠在dev和handler的h_list链表上
int input_register_handle(struct input_handle *handle) { struct input_handler *handler = handle->handler;//获得input_handle关联的input_handler struct input_dev *dev = handle->dev;//获得input_handle关联的input_dev int error; error = mutex_lock_interruptible(&dev->mutex); if (error) return error; list_add_tail_rcu(&handle->d_node, &dev->h_list);//将input_handle挂在input_dev的h_list链表上 mutex_unlock(&dev->mutex); list_add_tail(&handle->h_node, &handler->h_list);//将input_handle挂在input_handler的h_list链表上 if (handler->start) handler->start(handle); return 0; }
二、事件处理层的设备处理方法链表建立过程
内核自带很多的设备处理方法,当有新的设备,evdev.c里面注册的evdev_handler就可以处理任何的input_dev设备。
int input_register_handler(struct input_handler *handler) { struct input_dev *dev; int retval; retval = mutex_lock_interruptible(&input_mutex); if (retval) return retval; INIT_LIST_HEAD(&handler->h_list);//初始化input_handler的input_handle链表 if (handler->fops != NULL) { if (input_table[handler->minor >> 5]) { retval = -EBUSY; goto out; } input_table[handler->minor >> 5] = handler;//根据次设备号找到新handler存放位置 } list_add_tail(&handler->node, &input_handler_list);//将该新建的handler加入到input子系统维护的全局handler链表中 list_for_each_entry(dev, &input_dev_list, node)//遍历input子系统维护的全局dev链表 input_attach_handler(dev, handler);//找到和它匹配的input_dev input_wakeup_procfs_readers(); out: mutex_unlock(&input_mutex); return retval; }
三、应用程序访问input设备节点
1.打开设备节点
当应用程序调用open("/dev/eventxx",O_RDWR),会调用驱动层的input_open_file.该函数会根据设备的次设备号在 struct input_handler *input_table[8]找到它对应的input_handler,然后取出里面的fops赋值给文件描述符使用。
2.读写设备节点(evdev.c为例)
当应用程序调用read后将调用到input_handler的read函数,如果设备现在没有数据可读,那个调用read的进程将休眠等待
static ssize_t evdev_read(struct file *file, char __user *buffer,size_t count, loff_t *ppos) { 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; /*缓冲队列为空&&*evdev存在&&文件为非阻塞/ if (client->head == client->tail && evdev->exist &&(file->f_flags & O_NONBLOCK)) return -EAGAIN; /*将进程休眠等待在evdev->wait上,至到缓冲区有数据或者evdev不存在*/ retval = wait_event_interruptible(evdev->wait,client->head != client->tail || !evdev->exist); if (retval) return retval; if (!evdev->exist) return -ENODEV; while (retval + input_event_size() <= count &&evdev_fetch_next_event(client, &event)) { if (input_event_to_user(buffer + retval, &event)) return -EFAULT; retval += input_event_size(); } return retval; } 3.硬件设备有动作,唤醒读等待进程
当设备驱动层有数据的时候,就会调用input_event,这将导致input_handler的event函数被调用。(evdev.c为例)
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_pass_event(client, &event); else list_for_each_entry_rcu(client, &evdev->client_list, node) evdev_pass_event(client, &event); rcu_read_unlock(); //唤醒因读这个evdev设备而休眠的设备 wake_up_interruptible(&evdev->wait); }
参考文章:
http://www.cnblogs.com/myblesh/articles/2367648.html