Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2986385
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: LINUX

2014-09-12 21:26:24

原文地址:

这节结合even handler来分析设备的注册和打开的过程,在设备注册之前,必须先初始化INPUT子系统,由input_init()函数来完成

相关阅读:Linux Input子系统(上)--概述 

  1. static int __init input_init(void)  
  2. {  
  3.     int err;  
  4.   
  5.     input_init_abs_bypass();  
  6.   
  7.     err = class_register(&input_class);//注册input类   
  8.     if (err) {  
  9.         printk(KERN_ERR "input: unable to register input_dev class\n");  
  10.         return err;  
  11.     }  
  12.   
  13.     err = input_proc_init();//创建/proc中的项   
  14.     if (err)  
  15.         goto fail1;  
  16.   
  17.     /*注册设备,设备号为INPUT_MAJOR(13),后面注册的输入设备都使用该主设备号*/  
  18.     err = register_chrdev(INPUT_MAJOR, "input", &input_fops);  
  19.     if (err) {  
  20.         printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);  
  21.         goto fail2;  
  22.     }  
  23.   
  24.     return 0;  
  25.   
  26.  fail2: input_proc_exit();  
  27.  fail1: class_unregister(&input_class);  
  28.     return err;  
  29. }  

input_fops中只定义了open函数。

  1. static const struct file_operations input_fops = {  
  2.     .owner = THIS_MODULE,  
  3.     .open = input_open_file,  
  4. };  

我们需要在设备驱动层中完成输入设备的注册,通过调用input_register_device()函数来完成,该函数的一个重要任务就是完成设备与事件驱动的匹配。

  1. int input_register_device(struct input_dev *dev)  
  2. {  
  3.     static atomic_t input_no = ATOMIC_INIT(0);  
  4.     struct input_handler *handler;  
  5.     const char *path;  
  6.     int error;  
  7.   
  8.     __set_bit(EV_SYN, dev->evbit);  
  9.   
  10.     /* 
  11.      * If delay and period are pre-set by the driver, then autorepeating 
  12.      * is handled by the driver itself and we don't do it in input.c. 
  13.      */  
  14.   
  15.     init_timer(&dev->timer);  
  16.     if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {  
  17.         dev->timer.data = (long) dev;  
  18.         dev->timer.function = input_repeat_key;  
  19.         dev->rep[REP_DELAY] = 250;  
  20.         dev->rep[REP_PERIOD] = 33;  
  21.     }  
  22.   
  23.     /*没有定义设备的getkeycode函数,则使用默认的获取键值函数*/  
  24.     if (!dev->getkeycode)  
  25.         dev->getkeycode = input_default_getkeycode;  
  26.   
  27.     /*没有定义设备的setkeycode函数,则使用默认的设定键值函数*/  
  28.     if (!dev->setkeycode)  
  29.         dev->setkeycode = input_default_setkeycode;  
  30.   
  31.     /*设定dev的名字*/  
  32.     dev_set_name(&dev->dev, "input%ld",  
  33.              (unsigned long) atomic_inc_return(&input_no) - 1);  
  34.   
  35.     /*添加设备*/  
  36.     error = device_add(&dev->dev);  
  37.     if (error)  
  38.         return error;  
  39.   
  40.     path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);  
  41.     printk(KERN_INFO "input: %s as %s\n",  
  42.         dev->name ? dev->name : "Unspecified device", path ? path : "N/A");  
  43.     kfree(path);  
  44.   
  45.     error = mutex_lock_interruptible(&input_mutex);  
  46.     if (error) {  
  47.         device_del(&dev->dev);  
  48.         return error;  
  49.     }  
  50.   
  51.     /*将设备添加到input_dev_list设备链表*/  
  52.     list_add_tail(&dev->node, &input_dev_list);  
  53.   
  54.     /*遍历input_handler_list,试图与每一个handler进行匹配*/  
  55.     list_for_each_entry(handler, &input_handler_list, node)  
  56.         input_attach_handler(dev, handler);  
  57.   
  58.     input_wakeup_procfs_readers();  
  59.   
  60.     mutex_unlock(&input_mutex);  
  61.   
  62.     return 0;  
  63. }

 

  1. static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)  
  2. {  
  3.     const struct input_device_id *id;  
  4.     int error;  
  5.   
  6.     /*如果定义了handler的黑名单,并且设备和黑名单中的id匹配了,则该设备不能和handler匹配*/  
  7.     if (handler->blacklist && input_match_device(handler->blacklist, dev))  
  8.         return -ENODEV;  
  9.   
  10.     id = input_match_device(handler->id_table, dev);  
  11.     if (!id)  
  12.         return -ENODEV;  
  13.   
  14.     /*执行handler的connect,建立handler与设备之间的联系*/  
  15.     error = handler->connect(handler, dev, id);  
  16.     if (error && error != -ENODEV)  
  17.         printk(KERN_ERR  
  18.             "input: failed to attach handler %s to device %s, "  
  19.             "error: %d\n",  
  20.             handler->name, kobject_name(&dev->dev.kobj), error);  
  21.   
  22.     return error;  
  23. }  

匹配的具体过程:

  1. static const struct input_device_id *input_match_device(const struct input_device_id *id,  
  2.                             struct input_dev *dev)  
  3. {  
  4.     int i;  
  5.   
  6.     /*遍历handler的id_table与device进行匹配*/  
  7.     for (; id->flags || id->driver_info; id++) {  
  8.   
  9.         /*根据flags的标志位,按需要匹配相应的字段*/  
  10.         if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)  
  11.             if (id->bustype != dev->id.bustype)//总线类型不匹配   
  12.                 continue;  
  13.   
  14.         if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)//生产厂商不匹配   
  15.             if (id->vendor != dev->id.vendor)  
  16.                 continue;  
  17.   
  18.         if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)//产品不匹配   
  19.             if (id->product != dev->id.product)  
  20.                 continue;  
  21.   
  22.         if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)//版本不匹配   
  23.             if (id->version != dev->id.version)  
  24.                 continue;  
  25.           
  26.         MATCH_BIT(evbit,  EV_MAX);//匹配所有的事件类型   
  27.   
  28.         /*匹配所有事件的子事件*/  
  29.         MATCH_BIT(keybit, KEY_MAX);  
  30.         MATCH_BIT(relbit, REL_MAX);  
  31.         MATCH_BIT(absbit, ABS_MAX);  
  32.         MATCH_BIT(mscbit, MSC_MAX);  
  33.         MATCH_BIT(ledbit, LED_MAX);  
  34.         MATCH_BIT(sndbit, SND_MAX);  
  35.         MATCH_BIT(ffbit,  FF_MAX);  
  36.         MATCH_BIT(swbit,  SW_MAX);  
  37.   
  38.         return id;//匹配成功,返回id   
  39.     }  
  40.   
  41.     return NULL;  
  42. }  

MATCH_BIT是将device的相应字段和handler的相应字段逐位对比,都一样的话表示成功,否则continue

  1. #define MATCH_BIT(bit, max) \   
  2.         for (i = 0; i < BITS_TO_LONGS(max); i++) \  
  3.             if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \  
  4.                 break; \  
  5.         if (i != BITS_TO_LONGS(max)) \  
  6.             continue;  

以event handler为例,看connect函数做了什么:

  1. static int evdev_connect(struct input_handler *handler, struct input_dev *dev,  
  2.              const struct input_device_id *id)  
  3. {  
  4.     struct evdev *evdev;  
  5.     int minor;  
  6.     int error;  
  7.   
  8.     /*在evdev_table中找到空闲块,其对应的数组下标再加上EVDEV_MINOR_BASE(64)就是次设备号 
  9.       每个handler都拥有32个此设备号,也就是最多可以对应32个设备*/  
  10.     for (minor = 0; minor < EVDEV_MINORS; minor++)  
  11.         if (!evdev_table[minor])  
  12.             break;  
  13.   
  14.     if (minor == EVDEV_MINORS) {  
  15.         printk(KERN_ERR "evdev: no more free evdev devices\n");  
  16.         return -ENFILE;  
  17.     }  
  18.   
  19.     evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);  
  20.     if (!evdev)  
  21.         return -ENOMEM;  
  22.   
  23.     INIT_LIST_HEAD(&evdev->client_list);  
  24.     spin_lock_init(&evdev->client_lock);  
  25.     mutex_init(&evdev->mutex);  
  26.     init_waitqueue_head(&evdev->wait);  
  27.   
  28.     /*初始化evdev的内部变量*/  
  29.     snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);  
  30.     evdev->exist = 1;  
  31.     evdev->minor = minor;  
  32.   
  33.     /*初始化handle,每个evdev中都有一个handle*/  
  34.     evdev->handle.dev = input_get_device(dev);  
  35.     evdev->handle.name = evdev->name;  
  36.     evdev->handle.handler = handler;  
  37.     evdev->handle.private = evdev;  
  38.   
  39.     /*初始化evdev中的内嵌device*/  
  40.     dev_set_name(&evdev->dev, evdev->name);  
  41.     evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);  
  42.     evdev->dev.class = &input_class;  
  43.     evdev->dev.parent = &dev->dev;  
  44.     evdev->dev.release = evdev_free;  
  45.     device_initialize(&evdev->dev);  
  46.   
  47.     /*注册handle,主要将handle链接到input_dev和handler的h_list链表中去*/  
  48.     error = input_register_handle(&evdev->handle);  
  49.     if (error)  
  50.         goto err_free_evdev;  
  51.   
  52.     /*将evdev存入evdev_table数组*/  
  53.     error = evdev_install_chrdev(evdev);  
  54.     if (error)  
  55.         goto err_unregister_handle;  
  56.   
  57.     error = device_add(&evdev->dev);  
  58.     if (error)  
  59.         goto err_cleanup_evdev;  
  60.   
  61.     return 0;  
  62.   
  63.  err_cleanup_evdev:  
  64.     evdev_cleanup(evdev);  
  65.  err_unregister_handle:  
  66.     input_unregister_handle(&evdev->handle);  
  67.  err_free_evdev:  
  68.     put_device(&evdev->dev);  
  69.     return error;  
  70. }  

至此设备的注册完成!对应event handler,在/dev中将多出一个event(x)设备文件,对应一个evdev实例,应用程序打开它的话也就意味着通过event handler来和设备驱动层传递事件。

再来看打开设备的过程,还是以event handler为例,假如打开一个event(x),则先执行:

  1. static int input_open_file(struct inode *inode, struct file *file)  
  2. {  
  3.     struct input_handler *handler;  
  4.     const struct file_operations *old_fops, *new_fops = NULL;  
  5.     int err;  
  6.   
  7.     lock_kernel();  
  8.     /* No load-on-demand here? */  
  9.       
  10.     /*32个设备是共用一个handler的,通过此设备号得到设备对应的handler*/  
  11.     handler = input_table[iminor(inode) >> 5];  
  12.     if (!handler || !(new_fops = fops_get(handler->fops))) {  
  13.         err = -ENODEV;  
  14.         goto out;  
  15.     }  
  16.   
  17.     /* 
  18.      * That's _really_ odd. Usually NULL ->open means "nothing special", 
  19.      * not "no device". Oh, well... 
  20.      */  
  21.     if (!new_fops->open) {  
  22.         fops_put(new_fops);  
  23.         err = -ENODEV;  
  24.         goto out;  
  25.     }  
  26.     old_fops = file->f_op;  
  27.     /*定位fops*/  
  28.     file->f_op = new_fops;  
  29.   
  30.     err = new_fops->open(inode, file);  
  31.   
  32.     if (err) {  
  33.         fops_put(file->f_op);  
  34.         file->f_op = fops_get(old_fops);  
  35.     }  
  36.     fops_put(old_fops);  
  37. out:  
  38.     unlock_kernel();  
  39.     return err;  
  40. }  

通过此设备号所在的组(0~31),(32~63),(64~95)……就可以找到相应的handler,所有的handler都保存在input_table中,对于次设备号在64~95范围的设备,将定位到下标为2的handler,,也就是event handler,然后将用handler中的open函数替代之前的open函数,并执行新的open函数,这样就以handler本身定义的open来打开设备完成相应的初始化了。

event handler中的open函数:

  1. static int evdev_open(struct inode *inode, struct file *file)  
  2. {  
  3.     struct evdev *evdev;  
  4.     struct evdev_client *client;  
  5.     int i = iminor(inode) - EVDEV_MINOR_BASE;  
  6.     int error;  
  7.   
  8.     if (i >= EVDEV_MINORS)  
  9.         return -ENODEV;  
  10.   
  11.     error = mutex_lock_interruptible(&evdev_table_mutex);  
  12.     if (error)  
  13.         return error;  
  14.     /*从evdev_table中取出和此设备号对应的evdev实例*/  
  15.     evdev = evdev_table[i];  
  16.     if (evdev)  
  17.         get_device(&evdev->dev);  
  18.     mutex_unlock(&evdev_table_mutex);  
  19.   
  20.     if (!evdev)  
  21.         return -ENODEV;  
  22.   
  23.     /*每当一个应用程序打开该文件都会生成一个client*/  
  24.     client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL);  
  25.     if (!client) {  
  26.         error = -ENOMEM;  
  27.         goto err_put_evdev;  
  28.     }  
  29.   
  30.     spin_lock_init(&client->buffer_lock);  
  31.     client->evdev = evdev;  
  32.     /*将client链入evdev的client_list中*/  
  33.     evdev_attach_client(evdev, client);  
  34.   
  35.     error = evdev_open_device(evdev);  
  36.     if (error)  
  37.         goto err_free_client;  
  38.   
  39.     file->private_data = client;  
  40.     return 0;  
  41.   
  42.  err_free_client:  
  43.     evdev_detach_client(evdev, client);  
  44.     kfree(client);  
  45.  err_put_evdev:  
  46.     put_device(&evdev->dev);  
  47.     return error;  
  48. }  
  1. static int evdev_open_device(struct evdev *evdev)  
  2. {  
  3.     int retval;  
  4.   
  5.     retval = mutex_lock_interruptible(&evdev->mutex);  
  6.     if (retval)  
  7.         return retval;  
  8.   
  9.     if (!evdev->exist)  
  10.         retval = -ENODEV;  
  11.     else if (!evdev->open++) {/*如果是第一次打开该设备,则要执行设备中定义的open*/  
  12.         retval = input_open_device(&evdev->handle);  
  13.         if (retval)  
  14.             evdev->open--;  
  15.     }  
  16.   
  17.     mutex_unlock(&evdev->mutex);  
  18.     return retval;  
  19. }  
  1. int input_open_device(struct input_handle *handle)  
  2. {  
  3.     struct input_dev *dev = handle->dev;  
  4.     int retval;  
  5.   
  6.     retval = mutex_lock_interruptible(&dev->mutex);  
  7.     if (retval)  
  8.         return retval;  
  9.   
  10.     if (dev->going_away) {  
  11.         retval = -ENODEV;  
  12.         goto out;  
  13.     }  
  14.   
  15.     handle->open++;  
  16.   
  17.     if (!dev->users++ && dev->open)  
  18.         retval = dev->open(dev);//执行input_dev中定义的open,完成设备的初始化   
  19.   
  20.     if (retval) {  
  21.         dev->users--;  
  22.         if (!--handle->open) {  
  23.             /* 
  24.              * Make sure we are not delivering any more events 
  25.              * through this handle 
  26.              */  
  27.             synchronize_rcu();  
  28.         }  
  29.     }  
  30.   
  31.  out:  
  32.     mutex_unlock(&dev->mutex);  
  33.     return retval;  
  34. }  

阅读(1007) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~