Linux Input子系统(2)--设备的注册和打开

水平有限,描述不当之处还请指出,转载请注明出处http://blog.csdn.net/vanbreaker/article/details/7718158
        这节结合even handler来分析设备的注册和打开的过程,在设备注册之前,必须先初始化INPUT子系统,由input_init()函数来完成

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

	input_init_abs_bypass();

	err = class_register(&input_class);//注册input类
	if (err) {
		printk(KERN_ERR "input: unable to register input_dev class\n");
		return err;
	}

	err = input_proc_init();//创建/proc中的项
	if (err)
		goto fail1;

	/*注册设备,设备号为INPUT_MAJOR(13),后面注册的输入设备都使用该主设备号*/
	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;
}



input_fops中只定义了open函数。

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

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

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

	/*没有定义设备的getkeycode函数,则使用默认的获取键值函数*/
	if (!dev->getkeycode)
		dev->getkeycode = input_default_getkeycode;

	/*没有定义设备的setkeycode函数,则使用默认的设定键值函数*/
	if (!dev->setkeycode)
		dev->setkeycode = input_default_setkeycode;

	/*设定dev的名字*/
	dev_set_name(&dev->dev, "input%ld",
		     (unsigned long) atomic_inc_return(&input_no) - 1);

	/*添加设备*/
	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_list设备链表*/
	list_add_tail(&dev->node, &input_dev_list);

	/*遍历input_handler_list,试图与每一个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;
}

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

	/*如果定义了handler的黑名单,并且设备和黑名单中的id匹配了,则该设备不能和handler匹配*/
	if (handler->blacklist && input_match_device(handler->blacklist, dev))
		return -ENODEV;

	id = input_match_device(handler->id_table, dev);
	if (!id)
		return -ENODEV;

	/*执行handler的connect,建立handler与设备之间的联系*/
	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;
}


匹配的具体过程:

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

	/*遍历handler的id_table与device进行匹配*/
	for (; id->flags || id->driver_info; id++) {

		/*根据flags的标志位,按需要匹配相应的字段*/
		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;//匹配成功,返回id
	}

	return NULL;
}


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
#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;


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

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

	/*在evdev_table中找到空闲块,其对应的数组下标再加上EVDEV_MINOR_BASE(64)就是次设备号
	  每个handler都拥有32个此设备号,也就是最多可以对应32个设备*/
	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);

	/*初始化evdev的内部变量*/
	snprintf(evdev->name, sizeof(evdev->name), "event%d", minor);
	evdev->exist = 1;
	evdev->minor = minor;

	/*初始化handle,每个evdev中都有一个handle*/
	evdev->handle.dev = input_get_device(dev);
	evdev->handle.name = evdev->name;
	evdev->handle.handler = handler;
	evdev->handle.private = evdev;

	/*初始化evdev中的内嵌device*/
	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);

	/*注册handle,主要将handle链接到input_dev和handler的h_list链表中去*/
	error = input_register_handle(&evdev->handle);
	if (error)
		goto err_free_evdev;

	/*将evdev存入evdev_table数组*/
	error = evdev_install_chrdev(evdev);
	if (error)
		goto err_unregister_handle;

	error = device_add(&evdev->dev);
	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;
}


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


通过此设备号所在的组(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.     struct evdev *evdev; 
  3.     struct evdev_client *client; 
  4.     int i = iminor(inode) - EVDEV_MINOR_BASE; 
  5.     int error; 
  6.  
  7.     if (i >= EVDEV_MINORS) 
  8.         return -ENODEV; 
  9.  
  10.     error = mutex_lock_interruptible(&evdev_table_mutex); 
  11.     if (error) 
  12.         return error; 
  13.     /*从evdev_table中取出和此设备号对应的evdev实例*/ 
  14.     evdev = evdev_table[i]; 
  15.     if (evdev) 
  16.         get_device(&evdev->dev); 
  17.     mutex_unlock(&evdev_table_mutex); 
  18.  
  19.     if (!evdev) 
  20.         return -ENODEV; 
  21.  
  22.     /*每当一个应用程序打开该文件都会生成一个client*/ 
  23.     client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); 
  24.     if (!client) { 
  25.         error = -ENOMEM; 
  26.         goto err_put_evdev; 
  27.     } 
  28.  
  29.     spin_lock_init(&client->buffer_lock); 
  30.     client->evdev = evdev; 
  31.     /*将client链入evdev的client_list中*/ 
  32.     evdev_attach_client(evdev, client); 
  33.  
  34.     error = evdev_open_device(evdev); 
  35.     if (error) 
  36.         goto err_free_client; 
  37.  
  38.     file->private_data = client; 
  39.     return 0; 
  40.  
  41. err_free_client: 
  42.     evdev_detach_client(evdev, client); 
  43.     kfree(client); 
  44. err_put_evdev: 
  45.     put_device(&evdev->dev); 
  46.     return error; 
static int evdev_open(struct inode *inode, struct file *file)
{
	struct evdev *evdev;
	struct evdev_client *client;
	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中取出和此设备号对应的evdev实例*/
	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);
	client->evdev = evdev;
	/*将client链入evdev的client_list中*/
	evdev_attach_client(evdev, client);

	error = evdev_open_device(evdev);
	if (error)
		goto err_free_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;
}


  1. static int evdev_open_device(struct evdev *evdev) 
  2.     int retval; 
  3.  
  4.     retval = mutex_lock_interruptible(&evdev->mutex); 
  5.     if (retval) 
  6.         return retval; 
  7.  
  8.     if (!evdev->exist) 
  9.         retval = -ENODEV; 
  10.     else if (!evdev->open++) {/*如果是第一次打开该设备,则要执行设备中定义的open*/ 
  11.         retval = input_open_device(&evdev->handle); 
  12.         if (retval) 
  13.             evdev->open--; 
  14.     } 
  15.  
  16.     mutex_unlock(&evdev->mutex); 
  17.     return retval; 
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;
	else if (!evdev->open++) {/*如果是第一次打开该设备,则要执行设备中定义的open*/
		retval = input_open_device(&evdev->handle);
		if (retval)
			evdev->open--;
	}

	mutex_unlock(&evdev->mutex);
	return retval;
}


  1. int input_open_device(struct input_handle *handle) 
  2.     struct input_dev *dev = handle->dev; 
  3.     int retval; 
  4.  
  5.     retval = mutex_lock_interruptible(&dev->mutex); 
  6.     if (retval) 
  7.         return retval; 
  8.  
  9.     if (dev->going_away) { 
  10.         retval = -ENODEV; 
  11.         goto out; 
  12.     } 
  13.  
  14.     handle->open++; 
  15.  
  16.     if (!dev->users++ && dev->open) 
  17.         retval = dev->open(dev);//执行input_dev中定义的open,完成设备的初始化 
  18.  
  19.     if (retval) { 
  20.         dev->users--; 
  21.         if (!--handle->open) { 
  22.             /*
  23.              * Make sure we are not delivering any more events
  24.              * through this handle
  25.              */ 
  26.             synchronize_rcu(); 
  27.         } 
  28.     } 
  29.  
  30. out: 
  31.     mutex_unlock(&dev->mutex); 
  32.     return retval; 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值