diff options
Diffstat (limited to 'drivers/input/input.c')
| -rw-r--r-- | drivers/input/input.c | 899 | 
1 files changed, 560 insertions, 339 deletions
diff --git a/drivers/input/input.c b/drivers/input/input.c index 7f26ca6ecf7..29ca0bb4f56 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c @@ -10,9 +10,12 @@   * the Free Software Foundation.   */ +#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt +  #include <linux/init.h>  #include <linux/types.h> -#include <linux/input.h> +#include <linux/idr.h> +#include <linux/input/mt.h>  #include <linux/module.h>  #include <linux/slab.h>  #include <linux/random.h> @@ -24,14 +27,15 @@  #include <linux/device.h>  #include <linux/mutex.h>  #include <linux/rcupdate.h> -#include <linux/smp_lock.h>  #include "input-compat.h"  MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");  MODULE_DESCRIPTION("Input core");  MODULE_LICENSE("GPL"); -#define INPUT_DEVICES	256 +#define INPUT_MAX_CHAR_DEVICES		1024 +#define INPUT_FIRST_DYNAMIC_DEV		256 +static DEFINE_IDA(input_ida);  static LIST_HEAD(input_dev_list);  static LIST_HEAD(input_handler_list); @@ -44,7 +48,7 @@ static LIST_HEAD(input_handler_list);   */  static DEFINE_MUTEX(input_mutex); -static struct input_handler *input_table[8]; +static const struct input_value input_value_sync = { EV_SYN, SYN_REPORT, 1 };  static inline int is_event_supported(unsigned int code,  				     unsigned long *bm, unsigned int max) @@ -68,52 +72,102 @@ static int input_defuzz_abs_event(int value, int old_val, int fuzz)  	return value;  } +static void input_start_autorepeat(struct input_dev *dev, int code) +{ +	if (test_bit(EV_REP, dev->evbit) && +	    dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && +	    dev->timer.data) { +		dev->repeat_key = code; +		mod_timer(&dev->timer, +			  jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); +	} +} + +static void input_stop_autorepeat(struct input_dev *dev) +{ +	del_timer(&dev->timer); +} +  /*   * Pass event first through all filters and then, if event has not been   * filtered out, through all open handles. This function is called with   * dev->event_lock held and interrupts disabled.   */ -static void input_pass_event(struct input_dev *dev, -			     struct input_handler *src_handler, -			     unsigned int type, unsigned int code, int value) +static unsigned int input_to_handler(struct input_handle *handle, +			struct input_value *vals, unsigned int count)  { -	struct input_handler *handler; -	struct input_handle *handle; +	struct input_handler *handler = handle->handler; +	struct input_value *end = vals; +	struct input_value *v; -	rcu_read_lock(); +	for (v = vals; v != vals + count; v++) { +		if (handler->filter && +		    handler->filter(handle, v->type, v->code, v->value)) +			continue; +		if (end != v) +			*end = *v; +		end++; +	} -	handle = rcu_dereference(dev->grab); -	if (handle) -		handle->handler->event(handle, type, code, value); -	else { -		bool filtered = false; +	count = end - vals; +	if (!count) +		return 0; -		list_for_each_entry_rcu(handle, &dev->h_list, d_node) { -			if (!handle->open) -				continue; +	if (handler->events) +		handler->events(handle, vals, count); +	else if (handler->event) +		for (v = vals; v != end; v++) +			handler->event(handle, v->type, v->code, v->value); -			handler = handle->handler; +	return count; +} -			/* -			 * If this is the handler that injected this -			 * particular event we want to skip it to avoid -			 * filters firing again and again. -			 */ -			if (handler == src_handler) -				continue; +/* + * Pass values first through all filters and then, if event has not been + * filtered out, through all open handles. This function is called with + * dev->event_lock held and interrupts disabled. + */ +static void input_pass_values(struct input_dev *dev, +			      struct input_value *vals, unsigned int count) +{ +	struct input_handle *handle; +	struct input_value *v; -			if (!handler->filter) { -				if (filtered) -					break; +	if (!count) +		return; -				handler->event(handle, type, code, value); +	rcu_read_lock(); -			} else if (handler->filter(handle, type, code, value)) -				filtered = true; -		} +	handle = rcu_dereference(dev->grab); +	if (handle) { +		count = input_to_handler(handle, vals, count); +	} else { +		list_for_each_entry_rcu(handle, &dev->h_list, d_node) +			if (handle->open) +				count = input_to_handler(handle, vals, count);  	}  	rcu_read_unlock(); + +	add_input_randomness(vals->type, vals->code, vals->value); + +	/* trigger auto repeat for key events */ +	for (v = vals; v != vals + count; v++) { +		if (v->type == EV_KEY && v->value != 2) { +			if (v->value) +				input_start_autorepeat(dev, v->code); +			else +				input_stop_autorepeat(dev); +		} +	} +} + +static void input_pass_event(struct input_dev *dev, +			     unsigned int type, unsigned int code, int value) +{ +	struct input_value vals[] = { { type, code, value } }; + +	input_pass_values(dev, vals, ARRAY_SIZE(vals));  }  /* @@ -130,18 +184,12 @@ static void input_repeat_key(unsigned long data)  	if (test_bit(dev->repeat_key, dev->key) &&  	    is_event_supported(dev->repeat_key, dev->keybit, KEY_MAX)) { +		struct input_value vals[] =  { +			{ EV_KEY, dev->repeat_key, 2 }, +			input_value_sync +		}; -		input_pass_event(dev, NULL, EV_KEY, dev->repeat_key, 2); - -		if (dev->sync) { -			/* -			 * Only send SYN_REPORT if we are not in a middle -			 * of driver parsing a new hardware packet. -			 * Otherwise assume that the driver will send -			 * SYN_REPORT once it's done. -			 */ -			input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); -		} +		input_pass_values(dev, vals, ARRAY_SIZE(vals));  		if (dev->rep[REP_PERIOD])  			mod_timer(&dev->timer, jiffies + @@ -151,31 +199,17 @@ static void input_repeat_key(unsigned long data)  	spin_unlock_irqrestore(&dev->event_lock, flags);  } -static void input_start_autorepeat(struct input_dev *dev, int code) -{ -	if (test_bit(EV_REP, dev->evbit) && -	    dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && -	    dev->timer.data) { -		dev->repeat_key = code; -		mod_timer(&dev->timer, -			  jiffies + msecs_to_jiffies(dev->rep[REP_DELAY])); -	} -} - -static void input_stop_autorepeat(struct input_dev *dev) -{ -	del_timer(&dev->timer); -} -  #define INPUT_IGNORE_EVENT	0  #define INPUT_PASS_TO_HANDLERS	1  #define INPUT_PASS_TO_DEVICE	2 +#define INPUT_SLOT		4 +#define INPUT_FLUSH		8  #define INPUT_PASS_TO_ALL	(INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)  static int input_handle_abs_event(struct input_dev *dev, -				  struct input_handler *src_handler,  				  unsigned int code, int *pval)  { +	struct input_mt *mt = dev->mt;  	bool is_mt_event;  	int *pold; @@ -184,19 +218,18 @@ static int input_handle_abs_event(struct input_dev *dev,  		 * "Stage" the event; we'll flush it later, when we  		 * get actual touch data.  		 */ -		if (*pval >= 0 && *pval < dev->mtsize) -			dev->slot = *pval; +		if (mt && *pval >= 0 && *pval < mt->num_slots) +			mt->slot = *pval;  		return INPUT_IGNORE_EVENT;  	} -	is_mt_event = code >= ABS_MT_FIRST && code <= ABS_MT_LAST; +	is_mt_event = input_is_mt_value(code);  	if (!is_mt_event) {  		pold = &dev->absinfo[code].value; -	} else if (dev->mt) { -		struct input_mt_slot *mtslot = &dev->mt[dev->slot]; -		pold = &mtslot->abs[code - ABS_MT_FIRST]; +	} else if (mt) { +		pold = &mt->slots[mt->slot].abs[code - ABS_MT_FIRST];  	} else {  		/*  		 * Bypass filtering for multi-touch events when @@ -215,20 +248,19 @@ static int input_handle_abs_event(struct input_dev *dev,  	}  	/* Flush pending "slot" event */ -	if (is_mt_event && dev->slot != input_abs_get_val(dev, ABS_MT_SLOT)) { -		input_abs_set_val(dev, ABS_MT_SLOT, dev->slot); -		input_pass_event(dev, src_handler, -				 EV_ABS, ABS_MT_SLOT, dev->slot); +	if (is_mt_event && mt && mt->slot != input_abs_get_val(dev, ABS_MT_SLOT)) { +		input_abs_set_val(dev, ABS_MT_SLOT, mt->slot); +		return INPUT_PASS_TO_HANDLERS | INPUT_SLOT;  	}  	return INPUT_PASS_TO_HANDLERS;  } -static void input_handle_event(struct input_dev *dev, -			       struct input_handler *src_handler, -			       unsigned int type, unsigned int code, int value) +static int input_get_disposition(struct input_dev *dev, +			  unsigned int type, unsigned int code, int *pval)  {  	int disposition = INPUT_IGNORE_EVENT; +	int value = *pval;  	switch (type) { @@ -239,37 +271,34 @@ static void input_handle_event(struct input_dev *dev,  			break;  		case SYN_REPORT: -			if (!dev->sync) { -				dev->sync = true; -				disposition = INPUT_PASS_TO_HANDLERS; -			} +			disposition = INPUT_PASS_TO_HANDLERS | INPUT_FLUSH;  			break;  		case SYN_MT_REPORT: -			dev->sync = false;  			disposition = INPUT_PASS_TO_HANDLERS;  			break;  		}  		break;  	case EV_KEY: -		if (is_event_supported(code, dev->keybit, KEY_MAX) && -		    !!test_bit(code, dev->key) != value) { +		if (is_event_supported(code, dev->keybit, KEY_MAX)) { -			if (value != 2) { -				__change_bit(code, dev->key); -				if (value) -					input_start_autorepeat(dev, code); -				else -					input_stop_autorepeat(dev); +			/* auto-repeat bypasses state updates */ +			if (value == 2) { +				disposition = INPUT_PASS_TO_HANDLERS; +				break;  			} -			disposition = INPUT_PASS_TO_HANDLERS; +			if (!!test_bit(code, dev->key) != !!value) { + +				__change_bit(code, dev->key); +				disposition = INPUT_PASS_TO_HANDLERS; +			}  		}  		break;  	case EV_SW:  		if (is_event_supported(code, dev->swbit, SW_MAX) && -		    !!test_bit(code, dev->sw) != value) { +		    !!test_bit(code, dev->sw) != !!value) {  			__change_bit(code, dev->sw);  			disposition = INPUT_PASS_TO_HANDLERS; @@ -278,8 +307,7 @@ static void input_handle_event(struct input_dev *dev,  	case EV_ABS:  		if (is_event_supported(code, dev->absbit, ABS_MAX)) -			disposition = input_handle_abs_event(dev, src_handler, -							     code, &value); +			disposition = input_handle_abs_event(dev, code, &value);  		break; @@ -297,7 +325,7 @@ static void input_handle_event(struct input_dev *dev,  	case EV_LED:  		if (is_event_supported(code, dev->ledbit, LED_MAX) && -		    !!test_bit(code, dev->led) != value) { +		    !!test_bit(code, dev->led) != !!value) {  			__change_bit(code, dev->led);  			disposition = INPUT_PASS_TO_ALL; @@ -330,14 +358,49 @@ static void input_handle_event(struct input_dev *dev,  		break;  	} -	if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN) -		dev->sync = false; +	*pval = value; +	return disposition; +} + +static void input_handle_event(struct input_dev *dev, +			       unsigned int type, unsigned int code, int value) +{ +	int disposition; + +	disposition = input_get_disposition(dev, type, code, &value);  	if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)  		dev->event(dev, type, code, value); -	if (disposition & INPUT_PASS_TO_HANDLERS) -		input_pass_event(dev, src_handler, type, code, value); +	if (!dev->vals) +		return; + +	if (disposition & INPUT_PASS_TO_HANDLERS) { +		struct input_value *v; + +		if (disposition & INPUT_SLOT) { +			v = &dev->vals[dev->num_vals++]; +			v->type = EV_ABS; +			v->code = ABS_MT_SLOT; +			v->value = dev->mt->slot; +		} + +		v = &dev->vals[dev->num_vals++]; +		v->type = type; +		v->code = code; +		v->value = value; +	} + +	if (disposition & INPUT_FLUSH) { +		if (dev->num_vals >= 2) +			input_pass_values(dev, dev->vals, dev->num_vals); +		dev->num_vals = 0; +	} else if (dev->num_vals >= dev->max_vals - 2) { +		dev->vals[dev->num_vals++] = input_value_sync; +		input_pass_values(dev, dev->vals, dev->num_vals); +		dev->num_vals = 0; +	} +  }  /** @@ -365,8 +428,7 @@ void input_event(struct input_dev *dev,  	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, NULL, type, code, value); +		input_handle_event(dev, type, code, value);  		spin_unlock_irqrestore(&dev->event_lock, flags);  	}  } @@ -396,8 +458,7 @@ void input_inject_event(struct input_handle *handle,  		rcu_read_lock();  		grab = rcu_dereference(dev->grab);  		if (!grab || grab == handle) -			input_handle_event(dev, handle->handler, -					   type, code, value); +			input_handle_event(dev, type, code, value);  		rcu_read_unlock();  		spin_unlock_irqrestore(&dev->event_lock, flags); @@ -465,7 +526,6 @@ int input_grab_device(struct input_handle *handle)  	}  	rcu_assign_pointer(dev->grab, handle); -	synchronize_rcu();   out:  	mutex_unlock(&dev->mutex); @@ -476,8 +536,11 @@ EXPORT_SYMBOL(input_grab_device);  static void __input_release_device(struct input_handle *handle)  {  	struct input_dev *dev = handle->dev; +	struct input_handle *grabber; -	if (dev->grab == handle) { +	grabber = rcu_dereference_protected(dev->grab, +					    lockdep_is_held(&dev->mutex)); +	if (grabber == handle) {  		rcu_assign_pointer(dev->grab, NULL);  		/* Make sure input_pass_event() notices that grab is gone */  		synchronize_rcu(); @@ -610,10 +673,10 @@ static void input_dev_release_keys(struct input_dev *dev)  		for (code = 0; code <= KEY_MAX; code++) {  			if (is_event_supported(code, dev->keybit, KEY_MAX) &&  			    __test_and_clear_bit(code, dev->key)) { -				input_pass_event(dev, NULL, EV_KEY, code, 0); +				input_pass_event(dev, EV_KEY, code, 0);  			}  		} -		input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); +		input_pass_event(dev, EV_SYN, SYN_REPORT, 1);  	}  } @@ -753,7 +816,7 @@ static int input_default_setkeycode(struct input_dev *dev,  	if (index >= dev->keycodemax)  		return -EINVAL; -	if (dev->keycodesize < sizeof(dev->keycode) && +	if (dev->keycodesize < sizeof(ke->keycode) &&  			(ke->keycode >> (dev->keycodesize * 8)))  		return -EINVAL; @@ -805,22 +868,9 @@ int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)  	int retval;  	spin_lock_irqsave(&dev->event_lock, flags); - -	if (dev->getkeycode) { -		/* -		 * Support for legacy drivers, that don't implement the new -		 * ioctls -		 */ -		u32 scancode = ke->index; - -		memcpy(ke->scancode, &scancode, sizeof(scancode)); -		ke->len = sizeof(scancode); -		retval = dev->getkeycode(dev, scancode, &ke->keycode); -	} else { -		retval = dev->getkeycode_new(dev, ke); -	} - +	retval = dev->getkeycode(dev, ke);  	spin_unlock_irqrestore(&dev->event_lock, flags); +  	return retval;  }  EXPORT_SYMBOL(input_get_keycode); @@ -845,35 +895,7 @@ int input_set_keycode(struct input_dev *dev,  	spin_lock_irqsave(&dev->event_lock, flags); -	if (dev->setkeycode) { -		/* -		 * Support for legacy drivers, that don't implement the new -		 * ioctls -		 */ -		unsigned int scancode; - -		retval = input_scancode_to_scalar(ke, &scancode); -		if (retval) -			goto out; - -		/* -		 * We need to know the old scancode, in order to generate a -		 * keyup effect, if the set operation happens successfully -		 */ -		if (!dev->getkeycode) { -			retval = -EINVAL; -			goto out; -		} - -		retval = dev->getkeycode(dev, scancode, &old_keycode); -		if (retval) -			goto out; - -		retval = dev->setkeycode(dev, scancode, ke->keycode); -	} else { -		retval = dev->setkeycode_new(dev, ke, &old_keycode); -	} - +	retval = dev->setkeycode(dev, ke, &old_keycode);  	if (retval)  		goto out; @@ -887,10 +909,12 @@ int input_set_keycode(struct input_dev *dev,  	if (test_bit(EV_KEY, dev->evbit) &&  	    !is_event_supported(old_keycode, dev->keybit, KEY_MAX) &&  	    __test_and_clear_bit(old_keycode, dev->key)) { +		struct input_value vals[] =  { +			{ EV_KEY, old_keycode, 0 }, +			input_value_sync +		}; -		input_pass_event(dev, NULL, EV_KEY, old_keycode, 0); -		if (dev->sync) -			input_pass_event(dev, NULL, EV_SYN, SYN_REPORT, 1); +		input_pass_values(dev, vals, ARRAY_SIZE(vals));  	}   out: @@ -900,18 +924,10 @@ int input_set_keycode(struct input_dev *dev,  }  EXPORT_SYMBOL(input_set_keycode); -#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; -  static const struct input_device_id *input_match_device(struct input_handler *handler,  							struct input_dev *dev)  {  	const struct input_device_id *id; -	int i;  	for (id = handler->id_table; id->flags || id->driver_info; id++) { @@ -931,15 +947,32 @@ static const struct input_device_id *input_match_device(struct input_handler *ha  			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); +		if (!bitmap_subset(id->evbit, dev->evbit, EV_MAX)) +			continue; + +		if (!bitmap_subset(id->keybit, dev->keybit, KEY_MAX)) +			continue; + +		if (!bitmap_subset(id->relbit, dev->relbit, REL_MAX)) +			continue; + +		if (!bitmap_subset(id->absbit, dev->absbit, ABS_MAX)) +			continue; + +		if (!bitmap_subset(id->mscbit, dev->mscbit, MSC_MAX)) +			continue; + +		if (!bitmap_subset(id->ledbit, dev->ledbit, LED_MAX)) +			continue; + +		if (!bitmap_subset(id->sndbit, dev->sndbit, SND_MAX)) +			continue; + +		if (!bitmap_subset(id->ffbit, dev->ffbit, FF_MAX)) +			continue; + +		if (!bitmap_subset(id->swbit, dev->swbit, SW_MAX)) +			continue;  		if (!handler->match || handler->match(handler, dev))  			return id; @@ -959,10 +992,8 @@ static int input_attach_handler(struct input_dev *dev, struct input_handler *han  	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); +		pr_err("failed to attach handler %s to device %s, error: %d\n", +		       handler->name, kobject_name(&dev->dev.kobj), error);  	return error;  } @@ -1110,6 +1141,8 @@ static int input_devices_seq_show(struct seq_file *seq, void *v)  		seq_printf(seq, "%s ", handle->name);  	seq_putc(seq, '\n'); +	input_seq_print_bitmap(seq, "PROP", dev->propbit, INPUT_PROP_MAX); +  	input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);  	if (test_bit(EV_KEY, dev->evbit))  		input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX); @@ -1191,7 +1224,7 @@ static int input_handlers_seq_show(struct seq_file *seq, void *v)  	seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);  	if (handler->filter)  		seq_puts(seq, " (filter)"); -	if (handler->fops) +	if (handler->legacy_minors)  		seq_printf(seq, " Minor=%d", handler->minor);  	seq_putc(seq, '\n'); @@ -1333,11 +1366,26 @@ static ssize_t input_dev_show_modalias(struct device *dev,  }  static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL); +static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap, +			      int max, int add_cr); + +static ssize_t input_dev_show_properties(struct device *dev, +					 struct device_attribute *attr, +					 char *buf) +{ +	struct input_dev *input_dev = to_input_dev(dev); +	int len = input_print_bitmap(buf, PAGE_SIZE, input_dev->propbit, +				     INPUT_PROP_MAX, true); +	return min_t(int, len, PAGE_SIZE); +} +static DEVICE_ATTR(properties, S_IRUGO, input_dev_show_properties, NULL); +  static struct attribute *input_dev_attrs[] = {  	&dev_attr_name.attr,  	&dev_attr_phys.attr,  	&dev_attr_uniq.attr,  	&dev_attr_modalias.attr, +	&dev_attr_properties.attr,  	NULL  }; @@ -1457,6 +1505,7 @@ static void input_dev_release(struct device *device)  	input_ff_destroy(dev);  	input_mt_destroy_slots(dev);  	kfree(dev->absinfo); +	kfree(dev->vals);  	kfree(dev);  	module_put(THIS_MODULE); @@ -1471,7 +1520,7 @@ static int input_add_uevent_bm_var(struct kobj_uevent_env *env,  {  	int len; -	if (add_uevent_var(env, "%s=", name)) +	if (add_uevent_var(env, "%s", name))  		return -ENOMEM;  	len = input_print_bitmap(&env->buf[env->buflen - 1], @@ -1537,6 +1586,8 @@ static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)  	if (dev->uniq)  		INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq); +	INPUT_ADD_HOTPLUG_BM_VAR("PROP=", dev->propbit, INPUT_PROP_MAX); +  	INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);  	if (test_bit(EV_KEY, dev->evbit))  		INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX); @@ -1604,35 +1655,36 @@ static void input_dev_toggle(struct input_dev *dev, bool activate)   */  void input_reset_device(struct input_dev *dev)  { -	mutex_lock(&dev->mutex); +	unsigned long flags; -	if (dev->users) { -		input_dev_toggle(dev, true); +	mutex_lock(&dev->mutex); +	spin_lock_irqsave(&dev->event_lock, flags); -		/* -		 * Keys that have been pressed at suspend time are unlikely -		 * to be still pressed when we resume. -		 */ -		spin_lock_irq(&dev->event_lock); -		input_dev_release_keys(dev); -		spin_unlock_irq(&dev->event_lock); -	} +	input_dev_toggle(dev, true); +	input_dev_release_keys(dev); +	spin_unlock_irqrestore(&dev->event_lock, flags);  	mutex_unlock(&dev->mutex);  }  EXPORT_SYMBOL(input_reset_device); -#ifdef CONFIG_PM +#ifdef CONFIG_PM_SLEEP  static int input_dev_suspend(struct device *dev)  {  	struct input_dev *input_dev = to_input_dev(dev); -	mutex_lock(&input_dev->mutex); +	spin_lock_irq(&input_dev->event_lock); -	if (input_dev->users) -		input_dev_toggle(input_dev, false); +	/* +	 * Keys that are pressed now are unlikely to be +	 * still pressed when we resume. +	 */ +	input_dev_release_keys(input_dev); -	mutex_unlock(&input_dev->mutex); +	/* Turn off LEDs and sounds, if any are active. */ +	input_dev_toggle(input_dev, false); + +	spin_unlock_irq(&input_dev->event_lock);  	return 0;  } @@ -1641,7 +1693,43 @@ static int input_dev_resume(struct device *dev)  {  	struct input_dev *input_dev = to_input_dev(dev); -	input_reset_device(input_dev); +	spin_lock_irq(&input_dev->event_lock); + +	/* Restore state of LEDs and sounds, if any were active. */ +	input_dev_toggle(input_dev, true); + +	spin_unlock_irq(&input_dev->event_lock); + +	return 0; +} + +static int input_dev_freeze(struct device *dev) +{ +	struct input_dev *input_dev = to_input_dev(dev); + +	spin_lock_irq(&input_dev->event_lock); + +	/* +	 * Keys that are pressed now are unlikely to be +	 * still pressed when we resume. +	 */ +	input_dev_release_keys(input_dev); + +	spin_unlock_irq(&input_dev->event_lock); + +	return 0; +} + +static int input_dev_poweroff(struct device *dev) +{ +	struct input_dev *input_dev = to_input_dev(dev); + +	spin_lock_irq(&input_dev->event_lock); + +	/* Turn off LEDs and sounds, if any are active. */ +	input_dev_toggle(input_dev, false); + +	spin_unlock_irq(&input_dev->event_lock);  	return 0;  } @@ -1649,7 +1737,8 @@ static int input_dev_resume(struct device *dev)  static const struct dev_pm_ops input_dev_pm_ops = {  	.suspend	= input_dev_suspend,  	.resume		= input_dev_resume, -	.poweroff	= input_dev_suspend, +	.freeze		= input_dev_freeze, +	.poweroff	= input_dev_poweroff,  	.restore	= input_dev_resume,  };  #endif /* CONFIG_PM */ @@ -1658,12 +1747,12 @@ static struct device_type input_dev_type = {  	.groups		= input_dev_attr_groups,  	.release	= input_dev_release,  	.uevent		= input_dev_uevent, -#ifdef CONFIG_PM +#ifdef CONFIG_PM_SLEEP  	.pm		= &input_dev_pm_ops,  #endif  }; -static char *input_devnode(struct device *dev, mode_t *mode) +static char *input_devnode(struct device *dev, umode_t *mode)  {  	return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));  } @@ -1677,7 +1766,7 @@ EXPORT_SYMBOL_GPL(input_class);  /**   * input_allocate_device - allocate memory for new input device   * - * Returns prepared struct input_dev or NULL. + * Returns prepared struct input_dev or %NULL.   *   * NOTE: Use input_free_device() to free devices that have not been   * registered; input_unregister_device() should be used for already @@ -1685,6 +1774,7 @@ EXPORT_SYMBOL_GPL(input_class);   */  struct input_dev *input_allocate_device(void)  { +	static atomic_t input_no = ATOMIC_INIT(0);  	struct input_dev *dev;  	dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); @@ -1694,9 +1784,13 @@ struct input_dev *input_allocate_device(void)  		device_initialize(&dev->dev);  		mutex_init(&dev->mutex);  		spin_lock_init(&dev->event_lock); +		init_timer(&dev->timer);  		INIT_LIST_HEAD(&dev->h_list);  		INIT_LIST_HEAD(&dev->node); +		dev_set_name(&dev->dev, "input%ld", +			     (unsigned long) atomic_inc_return(&input_no) - 1); +  		__module_get(THIS_MODULE);  	} @@ -1704,72 +1798,97 @@ struct input_dev *input_allocate_device(void)  }  EXPORT_SYMBOL(input_allocate_device); -/** - * input_free_device - free memory occupied by input_dev structure - * @dev: input device to free - * - * This function should only be used if input_register_device() - * was not called yet or if it failed. Once device was registered - * use input_unregister_device() and memory will be freed once last - * reference to the device is dropped. - * - * Device should be allocated by input_allocate_device(). - * - * NOTE: If there are references to the input device then memory - * will not be freed until last reference is dropped. - */ -void input_free_device(struct input_dev *dev) +struct input_devres { +	struct input_dev *input; +}; + +static int devm_input_device_match(struct device *dev, void *res, void *data)  { -	if (dev) -		input_put_device(dev); +	struct input_devres *devres = res; + +	return devres->input == data; +} + +static void devm_input_device_release(struct device *dev, void *res) +{ +	struct input_devres *devres = res; +	struct input_dev *input = devres->input; + +	dev_dbg(dev, "%s: dropping reference to %s\n", +		__func__, dev_name(&input->dev)); +	input_put_device(input);  } -EXPORT_SYMBOL(input_free_device);  /** - * input_mt_create_slots() - create MT input slots - * @dev: input device supporting MT events and finger tracking - * @num_slots: number of slots used by the device + * devm_input_allocate_device - allocate managed input device + * @dev: device owning the input device being created + * + * Returns prepared struct input_dev or %NULL. + * + * Managed input devices do not need to be explicitly unregistered or + * freed as it will be done automatically when owner device unbinds from + * its driver (or binding fails). Once managed input device is allocated, + * it is ready to be set up and registered in the same fashion as regular + * input device. There are no special devm_input_device_[un]register() + * variants, regular ones work with both managed and unmanaged devices, + * should you need them. In most cases however, managed input device need + * not be explicitly unregistered or freed.   * - * This function allocates all necessary memory for MT slot handling in the - * input device, and adds ABS_MT_SLOT to the device capabilities. All slots - * are initially marked as unused by setting ABS_MT_TRACKING_ID to -1. + * NOTE: the owner device is set up as parent of input device and users + * should not override it.   */ -int input_mt_create_slots(struct input_dev *dev, unsigned int num_slots) +struct input_dev *devm_input_allocate_device(struct device *dev)  { -	int i; +	struct input_dev *input; +	struct input_devres *devres; -	if (!num_slots) -		return 0; +	devres = devres_alloc(devm_input_device_release, +			      sizeof(struct input_devres), GFP_KERNEL); +	if (!devres) +		return NULL; -	dev->mt = kcalloc(num_slots, sizeof(struct input_mt_slot), GFP_KERNEL); -	if (!dev->mt) -		return -ENOMEM; +	input = input_allocate_device(); +	if (!input) { +		devres_free(devres); +		return NULL; +	} -	dev->mtsize = num_slots; -	input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0); +	input->dev.parent = dev; +	input->devres_managed = true; -	/* Mark slots as 'unused' */ -	for (i = 0; i < num_slots; i++) -		dev->mt[i].abs[ABS_MT_TRACKING_ID - ABS_MT_FIRST] = -1; +	devres->input = input; +	devres_add(dev, devres); -	return 0; +	return input;  } -EXPORT_SYMBOL(input_mt_create_slots); +EXPORT_SYMBOL(devm_input_allocate_device);  /** - * input_mt_destroy_slots() - frees the MT slots of the input device - * @dev: input device with allocated MT slots + * input_free_device - free memory occupied by input_dev structure + * @dev: input device to free   * - * This function is only needed in error path as the input core will - * automatically free the MT slots when the device is destroyed. + * This function should only be used if input_register_device() + * was not called yet or if it failed. Once device was registered + * use input_unregister_device() and memory will be freed once last + * reference to the device is dropped. + * + * Device should be allocated by input_allocate_device(). + * + * NOTE: If there are references to the input device then memory + * will not be freed until last reference is dropped.   */ -void input_mt_destroy_slots(struct input_dev *dev) +void input_free_device(struct input_dev *dev)  { -	kfree(dev->mt); -	dev->mt = NULL; -	dev->mtsize = 0; +	if (dev) { +		if (dev->devres_managed) +			WARN_ON(devres_destroy(dev->dev.parent, +						devm_input_device_release, +						devm_input_device_match, +						dev)); +		input_put_device(dev); +	}  } -EXPORT_SYMBOL(input_mt_destroy_slots); +EXPORT_SYMBOL(input_free_device);  /**   * input_set_capability - mark device as capable of a certain event @@ -1792,6 +1911,10 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int  		break;  	case EV_ABS: +		input_alloc_absinfo(dev); +		if (!dev->absinfo) +			return; +  		__set_bit(code, dev->absbit);  		break; @@ -1820,9 +1943,8 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int  		break;  	default: -		printk(KERN_ERR -			"input_set_capability: unknown type %u (code %u)\n", -			type, code); +		pr_err("input_set_capability: unknown type %u (code %u)\n", +		       type, code);  		dump_stack();  		return;  	} @@ -1831,6 +1953,45 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int  }  EXPORT_SYMBOL(input_set_capability); +static unsigned int input_estimate_events_per_packet(struct input_dev *dev) +{ +	int mt_slots; +	int i; +	unsigned int events; + +	if (dev->mt) { +		mt_slots = dev->mt->num_slots; +	} else if (test_bit(ABS_MT_TRACKING_ID, dev->absbit)) { +		mt_slots = dev->absinfo[ABS_MT_TRACKING_ID].maximum - +			   dev->absinfo[ABS_MT_TRACKING_ID].minimum + 1, +		mt_slots = clamp(mt_slots, 2, 32); +	} else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) { +		mt_slots = 2; +	} else { +		mt_slots = 0; +	} + +	events = mt_slots + 1; /* count SYN_MT_REPORT and SYN_REPORT */ + +	for (i = 0; i < ABS_CNT; i++) { +		if (test_bit(i, dev->absbit)) { +			if (input_is_mt_axis(i)) +				events += mt_slots; +			else +				events++; +		} +	} + +	for (i = 0; i < REL_CNT; i++) +		if (test_bit(i, dev->relbit)) +			events++; + +	/* Make room for KEY and MSC events */ +	events += 7; + +	return events; +} +  #define INPUT_CLEANSE_BITMASK(dev, type, bits)				\  	do {								\  		if (!test_bit(EV_##type, dev->evbit))			\ @@ -1850,6 +2011,38 @@ static void input_cleanse_bitmasks(struct input_dev *dev)  	INPUT_CLEANSE_BITMASK(dev, SW, sw);  } +static void __input_unregister_device(struct input_dev *dev) +{ +	struct input_handle *handle, *next; + +	input_disconnect_device(dev); + +	mutex_lock(&input_mutex); + +	list_for_each_entry_safe(handle, next, &dev->h_list, d_node) +		handle->handler->disconnect(handle); +	WARN_ON(!list_empty(&dev->h_list)); + +	del_timer_sync(&dev->timer); +	list_del_init(&dev->node); + +	input_wakeup_procfs_readers(); + +	mutex_unlock(&input_mutex); + +	device_del(&dev->dev); +} + +static void devm_input_device_unregister(struct device *dev, void *res) +{ +	struct input_devres *devres = res; +	struct input_dev *input = devres->input; + +	dev_dbg(dev, "%s: unregistering device %s\n", +		__func__, dev_name(&input->dev)); +	__input_unregister_device(input); +} +  /**   * input_register_device - register device with input core   * @dev: device to be registered @@ -1861,14 +2054,35 @@ static void input_cleanse_bitmasks(struct input_dev *dev)   * Once device has been successfully registered it can be unregistered   * with input_unregister_device(); input_free_device() should not be   * called in this case. + * + * Note that this function is also used to register managed input devices + * (ones allocated with devm_input_allocate_device()). Such managed input + * devices need not be explicitly unregistered or freed, their tear down + * is controlled by the devres infrastructure. It is also worth noting + * that tear down of managed input devices is internally a 2-step process: + * registered managed input device is first unregistered, but stays in + * memory and can still handle input_event() calls (although events will + * not be delivered anywhere). The freeing of managed input device will + * happen later, when devres stack is unwound to the point where device + * allocation was made.   */  int input_register_device(struct input_dev *dev)  { -	static atomic_t input_no = ATOMIC_INIT(0); +	struct input_devres *devres = NULL;  	struct input_handler *handler; +	unsigned int packet_size;  	const char *path;  	int error; +	if (dev->devres_managed) { +		devres = devres_alloc(devm_input_device_unregister, +				      sizeof(struct input_devres), GFP_KERNEL); +		if (!devres) +			return -ENOMEM; + +		devres->input = dev; +	} +  	/* Every input device generates EV_SYN/SYN_REPORT events. */  	__set_bit(EV_SYN, dev->evbit); @@ -1878,11 +2092,21 @@ int input_register_device(struct input_dev *dev)  	/* Make sure that bitmasks not mentioned in dev->evbit are clean. */  	input_cleanse_bitmasks(dev); +	packet_size = input_estimate_events_per_packet(dev); +	if (dev->hint_events_per_packet < packet_size) +		dev->hint_events_per_packet = packet_size; + +	dev->max_vals = dev->hint_events_per_packet + 2; +	dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL); +	if (!dev->vals) { +		error = -ENOMEM; +		goto err_devres_free; +	} +  	/*  	 * 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; @@ -1890,29 +2114,25 @@ int input_register_device(struct input_dev *dev)  		dev->rep[REP_PERIOD] = 33;  	} -	if (!dev->getkeycode && !dev->getkeycode_new) -		dev->getkeycode_new = input_default_getkeycode; - -	if (!dev->setkeycode && !dev->setkeycode_new) -		dev->setkeycode_new = input_default_setkeycode; +	if (!dev->getkeycode) +		dev->getkeycode = input_default_getkeycode; -	dev_set_name(&dev->dev, "input%ld", -		     (unsigned long) atomic_inc_return(&input_no) - 1); +	if (!dev->setkeycode) +		dev->setkeycode = input_default_setkeycode;  	error = device_add(&dev->dev);  	if (error) -		return error; +		goto err_free_vals;  	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"); +	pr_info("%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; -	} +	if (error) +		goto err_device_del;  	list_add_tail(&dev->node, &input_dev_list); @@ -1923,7 +2143,21 @@ int input_register_device(struct input_dev *dev)  	mutex_unlock(&input_mutex); +	if (dev->devres_managed) { +		dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n", +			__func__, dev_name(&dev->dev)); +		devres_add(dev->dev.parent, devres); +	}  	return 0; + +err_device_del: +	device_del(&dev->dev); +err_free_vals: +	kfree(dev->vals); +	dev->vals = NULL; +err_devres_free: +	devres_free(devres); +	return error;  }  EXPORT_SYMBOL(input_register_device); @@ -1936,24 +2170,20 @@ EXPORT_SYMBOL(input_register_device);   */  void input_unregister_device(struct input_dev *dev)  { -	struct input_handle *handle, *next; - -	input_disconnect_device(dev); - -	mutex_lock(&input_mutex); - -	list_for_each_entry_safe(handle, next, &dev->h_list, d_node) -		handle->handler->disconnect(handle); -	WARN_ON(!list_empty(&dev->h_list)); - -	del_timer_sync(&dev->timer); -	list_del_init(&dev->node); - -	input_wakeup_procfs_readers(); - -	mutex_unlock(&input_mutex); - -	device_unregister(&dev->dev); +	if (dev->devres_managed) { +		WARN_ON(devres_destroy(dev->dev.parent, +					devm_input_device_unregister, +					devm_input_device_match, +					dev)); +		__input_unregister_device(dev); +		/* +		 * We do not do input_put_device() here because it will be done +		 * when 2nd devres fires up. +		 */ +	} else { +		__input_unregister_device(dev); +		input_put_device(dev); +	}  }  EXPORT_SYMBOL(input_unregister_device); @@ -1968,22 +2198,14 @@ EXPORT_SYMBOL(input_unregister_device);  int input_register_handler(struct input_handler *handler)  {  	struct input_dev *dev; -	int retval; +	int error; -	retval = mutex_lock_interruptible(&input_mutex); -	if (retval) -		return retval; +	error = mutex_lock_interruptible(&input_mutex); +	if (error) +		return error;  	INIT_LIST_HEAD(&handler->h_list); -	if (handler->fops != NULL) { -		if (input_table[handler->minor >> 5]) { -			retval = -EBUSY; -			goto out; -		} -		input_table[handler->minor >> 5] = handler; -	} -  	list_add_tail(&handler->node, &input_handler_list);  	list_for_each_entry(dev, &input_dev_list, node) @@ -1991,9 +2213,8 @@ int input_register_handler(struct input_handler *handler)  	input_wakeup_procfs_readers(); - out:  	mutex_unlock(&input_mutex); -	return retval; +	return 0;  }  EXPORT_SYMBOL(input_register_handler); @@ -2016,9 +2237,6 @@ void input_unregister_handler(struct input_handler *handler)  	list_del_init(&handler->node); -	if (handler->fops != NULL) -		input_table[handler->minor >> 5] = NULL; -  	input_wakeup_procfs_readers();  	mutex_unlock(&input_mutex); @@ -2135,51 +2353,52 @@ void input_unregister_handle(struct input_handle *handle)  }  EXPORT_SYMBOL(input_unregister_handle); -static int input_open_file(struct inode *inode, struct file *file) +/** + * input_get_new_minor - allocates a new input minor number + * @legacy_base: beginning or the legacy range to be searched + * @legacy_num: size of legacy range + * @allow_dynamic: whether we can also take ID from the dynamic range + * + * This function allocates a new device minor for from input major namespace. + * Caller can request legacy minor by specifying @legacy_base and @legacy_num + * parameters and whether ID can be allocated from dynamic range if there are + * no free IDs in legacy range. + */ +int input_get_new_minor(int legacy_base, unsigned int legacy_num, +			bool allow_dynamic)  { -	struct input_handler *handler; -	const struct file_operations *old_fops, *new_fops = NULL; -	int err; - -	err = mutex_lock_interruptible(&input_mutex); -	if (err) -		return err; - -	/* No load-on-demand here? */ -	handler = input_table[iminor(inode) >> 5]; -	if (handler) -		new_fops = fops_get(handler->fops); - -	mutex_unlock(&input_mutex); -  	/* -	 * That's _really_ odd. Usually NULL ->open means "nothing special", -	 * not "no device". Oh, well... +	 * This function should be called from input handler's ->connect() +	 * methods, which are serialized with input_mutex, so no additional +	 * locking is needed here.  	 */ -	if (!new_fops || !new_fops->open) { -		fops_put(new_fops); -		err = -ENODEV; -		goto out; +	if (legacy_base >= 0) { +		int minor = ida_simple_get(&input_ida, +					   legacy_base, +					   legacy_base + legacy_num, +					   GFP_KERNEL); +		if (minor >= 0 || !allow_dynamic) +			return minor;  	} -	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: -	return err; +	return ida_simple_get(&input_ida, +			      INPUT_FIRST_DYNAMIC_DEV, INPUT_MAX_CHAR_DEVICES, +			      GFP_KERNEL);  } +EXPORT_SYMBOL(input_get_new_minor); -static const struct file_operations input_fops = { -	.owner = THIS_MODULE, -	.open = input_open_file, -	.llseek = noop_llseek, -}; +/** + * input_free_minor - release previously allocated minor + * @minor: minor to be released + * + * This function releases previously allocated input minor so that it can be + * reused later. + */ +void input_free_minor(unsigned int minor) +{ +	ida_simple_remove(&input_ida, minor); +} +EXPORT_SYMBOL(input_free_minor);  static int __init input_init(void)  { @@ -2187,7 +2406,7 @@ static int __init input_init(void)  	err = class_register(&input_class);  	if (err) { -		printk(KERN_ERR "input: unable to register input_dev class\n"); +		pr_err("unable to register input_dev class\n");  		return err;  	} @@ -2195,9 +2414,10 @@ static int __init input_init(void)  	if (err)  		goto fail1; -	err = register_chrdev(INPUT_MAJOR, "input", &input_fops); +	err = register_chrdev_region(MKDEV(INPUT_MAJOR, 0), +				     INPUT_MAX_CHAR_DEVICES, "input");  	if (err) { -		printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR); +		pr_err("unable to register char major %d", INPUT_MAJOR);  		goto fail2;  	} @@ -2211,7 +2431,8 @@ static int __init input_init(void)  static void __exit input_exit(void)  {  	input_proc_exit(); -	unregister_chrdev(INPUT_MAJOR, "input"); +	unregister_chrdev_region(MKDEV(INPUT_MAJOR, 0), +				 INPUT_MAX_CHAR_DEVICES);  	class_unregister(&input_class);  }  | 
