Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6.git] / drivers / input / input.c
index a79c833..11905b6 100644 (file)
  * the Free Software Foundation.
  */
 
+#define pr_fmt(fmt) KBUILD_BASENAME ": " fmt
+
 #include <linux/init.h>
-#include <linux/input.h>
+#include <linux/types.h>
+#include <linux/input/mt.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 #include <linux/random.h>
 #include <linux/major.h>
 #include <linux/proc_fs.h>
+#include <linux/sched.h>
 #include <linux/seq_file.h>
 #include <linux/poll.h>
 #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");
@@ -65,12 +70,14 @@ static int input_defuzz_abs_event(int value, int old_val, int fuzz)
 }
 
 /*
- * Pass event through all open handles. This function is called with
+ * 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,
                             unsigned int type, unsigned int code, int value)
 {
+       struct input_handler *handler;
        struct input_handle *handle;
 
        rcu_read_lock();
@@ -78,11 +85,25 @@ static void input_pass_event(struct input_dev *dev,
        handle = rcu_dereference(dev->grab);
        if (handle)
                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);
+       else {
+               bool filtered = false;
+
+               list_for_each_entry_rcu(handle, &dev->h_list, d_node) {
+                       if (!handle->open)
+                               continue;
+
+                       handler = handle->handler;
+                       if (!handler->filter) {
+                               if (filtered)
+                                       break;
+
+                               handler->event(handle, type, code, value);
+
+                       } else if (handler->filter(handle, type, code, value))
+                               filtered = true;
+               }
+       }
+
        rcu_read_unlock();
 }
 
@@ -142,6 +163,56 @@ static void input_stop_autorepeat(struct input_dev *dev)
 #define INPUT_PASS_TO_DEVICE   2
 #define INPUT_PASS_TO_ALL      (INPUT_PASS_TO_HANDLERS | INPUT_PASS_TO_DEVICE)
 
+static int input_handle_abs_event(struct input_dev *dev,
+                                 unsigned int code, int *pval)
+{
+       bool is_mt_event;
+       int *pold;
+
+       if (code == ABS_MT_SLOT) {
+               /*
+                * "Stage" the event; we'll flush it later, when we
+                * get actual touch data.
+                */
+               if (*pval >= 0 && *pval < dev->mtsize)
+                       dev->slot = *pval;
+
+               return INPUT_IGNORE_EVENT;
+       }
+
+       is_mt_event = code >= ABS_MT_FIRST && code <= ABS_MT_LAST;
+
+       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 {
+               /*
+                * Bypass filtering for multi-touch events when
+                * not employing slots.
+                */
+               pold = NULL;
+       }
+
+       if (pold) {
+               *pval = input_defuzz_abs_event(*pval, *pold,
+                                               dev->absinfo[code].fuzz);
+               if (*pold == *pval)
+                       return INPUT_IGNORE_EVENT;
+
+               *pold = *pval;
+       }
+
+       /* 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, EV_ABS, ABS_MT_SLOT, dev->slot);
+       }
+
+       return INPUT_PASS_TO_HANDLERS;
+}
+
 static void input_handle_event(struct input_dev *dev,
                               unsigned int type, unsigned int code, int value)
 {
@@ -157,10 +228,14 @@ static void input_handle_event(struct input_dev *dev,
 
                case SYN_REPORT:
                        if (!dev->sync) {
-                               dev->sync = 1;
+                               dev->sync = true;
                                disposition = INPUT_PASS_TO_HANDLERS;
                        }
                        break;
+               case SYN_MT_REPORT:
+                       dev->sync = false;
+                       disposition = INPUT_PASS_TO_HANDLERS;
+                       break;
                }
                break;
 
@@ -190,16 +265,9 @@ static void input_handle_event(struct input_dev *dev,
                break;
 
        case EV_ABS:
-               if (is_event_supported(code, dev->absbit, ABS_MAX)) {
-
-                       value = input_defuzz_abs_event(value,
-                                       dev->abs[code], dev->absfuzz[code]);
+               if (is_event_supported(code, dev->absbit, ABS_MAX))
+                       disposition = input_handle_abs_event(dev, code, &value);
 
-                       if (dev->abs[code] != value) {
-                               dev->abs[code] = value;
-                               disposition = INPUT_PASS_TO_HANDLERS;
-                       }
-               }
                break;
 
        case EV_REL:
@@ -250,7 +318,7 @@ static void input_handle_event(struct input_dev *dev,
        }
 
        if (disposition != INPUT_IGNORE_EVENT && type != EV_SYN)
-               dev->sync = 0;
+               dev->sync = false;
 
        if ((disposition & INPUT_PASS_TO_DEVICE) && dev->event)
                dev->event(dev, type, code, value);
@@ -267,9 +335,15 @@ static void input_handle_event(struct input_dev *dev,
  * @value: value of the event
  *
  * This function should be used by drivers implementing various input
- * devices. See also input_inject_event().
+ * devices to report input events. See also input_inject_event().
+ *
+ * NOTE: input_event() may be safely used right after input device was
+ * allocated with input_allocate_device(), even before it is registered
+ * with input_register_device(), but the event will not reach any of the
+ * input handlers. Such early invocation of input_event() may be used
+ * to 'seed' initial state of a switch or initial position of absolute
+ * axis, etc.
  */
-
 void input_event(struct input_dev *dev,
                 unsigned int type, unsigned int code, int value)
 {
@@ -318,6 +392,43 @@ void input_inject_event(struct input_handle *handle,
 EXPORT_SYMBOL(input_inject_event);
 
 /**
+ * input_alloc_absinfo - allocates array of input_absinfo structs
+ * @dev: the input device emitting absolute events
+ *
+ * If the absinfo struct the caller asked for is already allocated, this
+ * functions will not do anything.
+ */
+void input_alloc_absinfo(struct input_dev *dev)
+{
+       if (!dev->absinfo)
+               dev->absinfo = kcalloc(ABS_CNT, sizeof(struct input_absinfo),
+                                       GFP_KERNEL);
+
+       WARN(!dev->absinfo, "%s(): kcalloc() failed?\n", __func__);
+}
+EXPORT_SYMBOL(input_alloc_absinfo);
+
+void input_set_abs_params(struct input_dev *dev, unsigned int axis,
+                         int min, int max, int fuzz, int flat)
+{
+       struct input_absinfo *absinfo;
+
+       input_alloc_absinfo(dev);
+       if (!dev->absinfo)
+               return;
+
+       absinfo = &dev->absinfo[axis];
+       absinfo->minimum = min;
+       absinfo->maximum = max;
+       absinfo->fuzz = fuzz;
+       absinfo->flat = flat;
+
+       dev->absbit[BIT_WORD(axis)] |= BIT_MASK(axis);
+}
+EXPORT_SYMBOL(input_set_abs_params);
+
+
+/**
  * input_grab_device - grabs device for exclusive use
  * @handle: input handle that wants to own the device
  *
@@ -474,12 +585,30 @@ void input_close_device(struct input_handle *handle)
 EXPORT_SYMBOL(input_close_device);
 
 /*
+ * Simulate keyup events for all keys that are marked as pressed.
+ * The function must be called with dev->event_lock held.
+ */
+static void input_dev_release_keys(struct input_dev *dev)
+{
+       int code;
+
+       if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
+               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, EV_KEY, code, 0);
+                       }
+               }
+               input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
+       }
+}
+
+/*
  * Prepare device for unregistering
  */
 static void input_disconnect_device(struct input_dev *dev)
 {
        struct input_handle *handle;
-       int code;
 
        /*
         * Mark device as going away. Note that we take dev->mutex here
@@ -487,7 +616,7 @@ static void input_disconnect_device(struct input_dev *dev)
         * that there are no threads in the middle of input_open_device()
         */
        mutex_lock(&dev->mutex);
-       dev->going_away = 1;
+       dev->going_away = true;
        mutex_unlock(&dev->mutex);
 
        spin_lock_irq(&dev->event_lock);
@@ -498,15 +627,7 @@ static void input_disconnect_device(struct input_dev *dev)
         * generate events even after we done here but they will not
         * reach any handlers.
         */
-       if (is_event_supported(EV_KEY, dev->evbit, EV_MAX)) {
-               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, EV_KEY, code, 0);
-                       }
-               }
-               input_pass_event(dev, EV_SYN, SYN_REPORT, 1);
-       }
+       input_dev_release_keys(dev);
 
        list_for_each_entry(handle, &dev->h_list, d_node)
                handle->open = 0;
@@ -514,76 +635,141 @@ static void input_disconnect_device(struct input_dev *dev)
        spin_unlock_irq(&dev->event_lock);
 }
 
-static int input_fetch_keycode(struct input_dev *dev, int scancode)
+/**
+ * input_scancode_to_scalar() - converts scancode in &struct input_keymap_entry
+ * @ke: keymap entry containing scancode to be converted.
+ * @scancode: pointer to the location where converted scancode should
+ *     be stored.
+ *
+ * This function is used to convert scancode stored in &struct keymap_entry
+ * into scalar form understood by legacy keymap handling methods. These
+ * methods expect scancodes to be represented as 'unsigned int'.
+ */
+int input_scancode_to_scalar(const struct input_keymap_entry *ke,
+                            unsigned int *scancode)
+{
+       switch (ke->len) {
+       case 1:
+               *scancode = *((u8 *)ke->scancode);
+               break;
+
+       case 2:
+               *scancode = *((u16 *)ke->scancode);
+               break;
+
+       case 4:
+               *scancode = *((u32 *)ke->scancode);
+               break;
+
+       default:
+               return -EINVAL;
+       }
+
+       return 0;
+}
+EXPORT_SYMBOL(input_scancode_to_scalar);
+
+/*
+ * Those routines handle the default case where no [gs]etkeycode() is
+ * defined. In this case, an array indexed by the scancode is used.
+ */
+
+static unsigned int input_fetch_keycode(struct input_dev *dev,
+                                       unsigned int index)
 {
        switch (dev->keycodesize) {
-               case 1:
-                       return ((u8 *)dev->keycode)[scancode];
+       case 1:
+               return ((u8 *)dev->keycode)[index];
 
-               case 2:
-                       return ((u16 *)dev->keycode)[scancode];
+       case 2:
+               return ((u16 *)dev->keycode)[index];
 
-               default:
-                       return ((u32 *)dev->keycode)[scancode];
+       default:
+               return ((u32 *)dev->keycode)[index];
        }
 }
 
 static int input_default_getkeycode(struct input_dev *dev,
-                                   int scancode, int *keycode)
+                                   struct input_keymap_entry *ke)
 {
+       unsigned int index;
+       int error;
+
        if (!dev->keycodesize)
                return -EINVAL;
 
-       if (scancode >= dev->keycodemax)
+       if (ke->flags & INPUT_KEYMAP_BY_INDEX)
+               index = ke->index;
+       else {
+               error = input_scancode_to_scalar(ke, &index);
+               if (error)
+                       return error;
+       }
+
+       if (index >= dev->keycodemax)
                return -EINVAL;
 
-       *keycode = input_fetch_keycode(dev, scancode);
+       ke->keycode = input_fetch_keycode(dev, index);
+       ke->index = index;
+       ke->len = sizeof(index);
+       memcpy(ke->scancode, &index, sizeof(index));
 
        return 0;
 }
 
 static int input_default_setkeycode(struct input_dev *dev,
-                                   int scancode, int keycode)
+                                   const struct input_keymap_entry *ke,
+                                   unsigned int *old_keycode)
 {
-       int old_keycode;
+       unsigned int index;
+       int error;
        int i;
 
-       if (scancode >= dev->keycodemax)
+       if (!dev->keycodesize)
                return -EINVAL;
 
-       if (!dev->keycodesize)
+       if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
+               index = ke->index;
+       } else {
+               error = input_scancode_to_scalar(ke, &index);
+               if (error)
+                       return error;
+       }
+
+       if (index >= dev->keycodemax)
                return -EINVAL;
 
-       if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
+       if (dev->keycodesize < sizeof(ke->keycode) &&
+                       (ke->keycode >> (dev->keycodesize * 8)))
                return -EINVAL;
 
        switch (dev->keycodesize) {
                case 1: {
                        u8 *k = (u8 *)dev->keycode;
-                       old_keycode = k[scancode];
-                       k[scancode] = keycode;
+                       *old_keycode = k[index];
+                       k[index] = ke->keycode;
                        break;
                }
                case 2: {
                        u16 *k = (u16 *)dev->keycode;
-                       old_keycode = k[scancode];
-                       k[scancode] = keycode;
+                       *old_keycode = k[index];
+                       k[index] = ke->keycode;
                        break;
                }
                default: {
                        u32 *k = (u32 *)dev->keycode;
-                       old_keycode = k[scancode];
-                       k[scancode] = keycode;
+                       *old_keycode = k[index];
+                       k[index] = ke->keycode;
                        break;
                }
        }
 
-       clear_bit(old_keycode, dev->keybit);
-       set_bit(keycode, dev->keybit);
+       __clear_bit(*old_keycode, dev->keybit);
+       __set_bit(ke->keycode, dev->keybit);
 
        for (i = 0; i < dev->keycodemax; i++) {
-               if (input_fetch_keycode(dev, i) == old_keycode) {
-                       set_bit(old_keycode, dev->keybit);
+               if (input_fetch_keycode(dev, i) == *old_keycode) {
+                       __set_bit(*old_keycode, dev->keybit);
                        break; /* Setting the bit twice is useless, so break */
                }
        }
@@ -594,53 +780,92 @@ static int input_default_setkeycode(struct input_dev *dev,
 /**
  * input_get_keycode - retrieve keycode currently mapped to a given scancode
  * @dev: input device which keymap is being queried
- * @scancode: scancode (or its equivalent for device in question) for which
- *     keycode is needed
- * @keycode: result
+ * @ke: keymap entry
  *
  * This function should be called by anyone interested in retrieving current
- * keymap. Presently keyboard and evdev handlers use it.
+ * keymap. Presently evdev handlers use it.
  */
-int input_get_keycode(struct input_dev *dev, int scancode, int *keycode)
+int input_get_keycode(struct input_dev *dev, struct input_keymap_entry *ke)
 {
-       if (scancode < 0)
-               return -EINVAL;
+       unsigned long flags;
+       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);
+       }
 
-       return dev->getkeycode(dev, scancode, keycode);
+       spin_unlock_irqrestore(&dev->event_lock, flags);
+       return retval;
 }
 EXPORT_SYMBOL(input_get_keycode);
 
 /**
- * input_get_keycode - assign new keycode to a given scancode
+ * input_set_keycode - attribute a keycode to a given scancode
  * @dev: input device which keymap is being updated
- * @scancode: scancode (or its equivalent for device in question)
- * @keycode: new keycode to be assigned to the scancode
+ * @ke: new keymap entry
  *
  * This function should be called by anyone needing to update current
  * keymap. Presently keyboard and evdev handlers use it.
  */
-int input_set_keycode(struct input_dev *dev, int scancode, int keycode)
+int input_set_keycode(struct input_dev *dev,
+                     const struct input_keymap_entry *ke)
 {
        unsigned long flags;
-       int old_keycode;
+       unsigned int old_keycode;
        int retval;
 
-       if (scancode < 0)
-               return -EINVAL;
-
-       if (keycode < 0 || keycode > KEY_MAX)
+       if (ke->keycode > KEY_MAX)
                return -EINVAL;
 
        spin_lock_irqsave(&dev->event_lock, flags);
 
-       retval = dev->getkeycode(dev, scancode, &old_keycode);
-       if (retval)
-               goto out;
+       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, scancode, keycode);
        if (retval)
                goto out;
 
+       /* Make sure KEY_RESERVED did not get enabled. */
+       __clear_bit(KEY_RESERVED, dev->keybit);
+
        /*
         * Simulate keyup event if keycode is not present
         * in the keymap anymore
@@ -668,12 +893,13 @@ EXPORT_SYMBOL(input_set_keycode);
                if (i != BITS_TO_LONGS(max)) \
                        continue;
 
-static const struct input_device_id *input_match_device(const struct input_device_id *id,
+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->flags || id->driver_info; id++) {
+       for (id = handler->id_table; id->flags || id->driver_info; id++) {
 
                if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
                        if (id->bustype != dev->id.bustype)
@@ -701,7 +927,8 @@ static const struct input_device_id *input_match_device(const struct input_devic
                MATCH_BIT(ffbit,  FF_MAX);
                MATCH_BIT(swbit,  SW_MAX);
 
-               return id;
+               if (!handler->match || handler->match(handler, dev))
+                       return id;
        }
 
        return NULL;
@@ -712,23 +939,52 @@ static int input_attach_handler(struct input_dev *dev, struct input_handler *han
        const struct input_device_id *id;
        int error;
 
-       if (handler->blacklist && input_match_device(handler->blacklist, dev))
-               return -ENODEV;
-
-       id = input_match_device(handler->id_table, dev);
+       id = input_match_device(handler, dev);
        if (!id)
                return -ENODEV;
 
        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;
 }
 
+#ifdef CONFIG_COMPAT
+
+static int input_bits_to_string(char *buf, int buf_size,
+                               unsigned long bits, bool skip_empty)
+{
+       int len = 0;
+
+       if (INPUT_COMPAT_TEST) {
+               u32 dword = bits >> 32;
+               if (dword || !skip_empty)
+                       len += snprintf(buf, buf_size, "%x ", dword);
+
+               dword = bits & 0xffffffffUL;
+               if (dword || !skip_empty || len)
+                       len += snprintf(buf + len, max(buf_size - len, 0),
+                                       "%x", dword);
+       } else {
+               if (bits || !skip_empty)
+                       len += snprintf(buf, buf_size, "%lx", bits);
+       }
+
+       return len;
+}
+
+#else /* !CONFIG_COMPAT */
+
+static int input_bits_to_string(char *buf, int buf_size,
+                               unsigned long bits, bool skip_empty)
+{
+       return bits || !skip_empty ?
+               snprintf(buf, buf_size, "%lx", bits) : 0;
+}
+
+#endif
 
 #ifdef CONFIG_PROC_FS
 
@@ -753,10 +1009,29 @@ static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
        return 0;
 }
 
+union input_seq_state {
+       struct {
+               unsigned short pos;
+               bool mutex_acquired;
+       };
+       void *p;
+};
+
 static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       if (mutex_lock_interruptible(&input_mutex))
-               return NULL;
+       union input_seq_state *state = (union input_seq_state *)&seq->private;
+       int error;
+
+       /* We need to fit into seq->private pointer */
+       BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
+
+       error = mutex_lock_interruptible(&input_mutex);
+       if (error) {
+               state->mutex_acquired = false;
+               return ERR_PTR(error);
+       }
+
+       state->mutex_acquired = true;
 
        return seq_list_start(&input_dev_list, *pos);
 }
@@ -766,23 +1041,37 @@ static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
        return seq_list_next(v, &input_dev_list, pos);
 }
 
-static void input_devices_seq_stop(struct seq_file *seq, void *v)
+static void input_seq_stop(struct seq_file *seq, void *v)
 {
-       mutex_unlock(&input_mutex);
+       union input_seq_state *state = (union input_seq_state *)&seq->private;
+
+       if (state->mutex_acquired)
+               mutex_unlock(&input_mutex);
 }
 
 static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
                                   unsigned long *bitmap, int max)
 {
        int i;
-
-       for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
-               if (bitmap[i])
-                       break;
+       bool skip_empty = true;
+       char buf[18];
 
        seq_printf(seq, "B: %s=", name);
-       for (; i >= 0; i--)
-               seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
+
+       for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
+               if (input_bits_to_string(buf, sizeof(buf),
+                                        bitmap[i], skip_empty)) {
+                       skip_empty = false;
+                       seq_printf(seq, "%s%s", buf, i > 0 ? " " : "");
+               }
+       }
+
+       /*
+        * If no output was produced print a single 0.
+        */
+       if (skip_empty)
+               seq_puts(seq, "0");
+
        seq_putc(seq, '\n');
 }
 
@@ -805,6 +1094,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);
@@ -832,7 +1123,7 @@ static int input_devices_seq_show(struct seq_file *seq, void *v)
 static const struct seq_operations input_devices_seq_ops = {
        .start  = input_devices_seq_start,
        .next   = input_devices_seq_next,
-       .stop   = input_devices_seq_stop,
+       .stop   = input_seq_stop,
        .show   = input_devices_seq_show,
 };
 
@@ -852,40 +1143,51 @@ static const struct file_operations input_devices_fileops = {
 
 static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
 {
-       if (mutex_lock_interruptible(&input_mutex))
-               return NULL;
+       union input_seq_state *state = (union input_seq_state *)&seq->private;
+       int error;
+
+       /* We need to fit into seq->private pointer */
+       BUILD_BUG_ON(sizeof(union input_seq_state) != sizeof(seq->private));
+
+       error = mutex_lock_interruptible(&input_mutex);
+       if (error) {
+               state->mutex_acquired = false;
+               return ERR_PTR(error);
+       }
+
+       state->mutex_acquired = true;
+       state->pos = *pos;
 
-       seq->private = (void *)(unsigned long)*pos;
        return seq_list_start(&input_handler_list, *pos);
 }
 
 static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
-       seq->private = (void *)(unsigned long)(*pos + 1);
-       return seq_list_next(v, &input_handler_list, pos);
-}
+       union input_seq_state *state = (union input_seq_state *)&seq->private;
 
-static void input_handlers_seq_stop(struct seq_file *seq, void *v)
-{
-       mutex_unlock(&input_mutex);
+       state->pos = *pos + 1;
+       return seq_list_next(v, &input_handler_list, pos);
 }
 
 static int input_handlers_seq_show(struct seq_file *seq, void *v)
 {
        struct input_handler *handler = container_of(v, struct input_handler, node);
+       union input_seq_state *state = (union input_seq_state *)&seq->private;
 
-       seq_printf(seq, "N: Number=%ld Name=%s",
-                  (unsigned long)seq->private, handler->name);
+       seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name);
+       if (handler->filter)
+               seq_puts(seq, " (filter)");
        if (handler->fops)
                seq_printf(seq, " Minor=%d", handler->minor);
        seq_putc(seq, '\n');
 
        return 0;
 }
+
 static const struct seq_operations input_handlers_seq_ops = {
        .start  = input_handlers_seq_start,
        .next   = input_handlers_seq_next,
-       .stop   = input_handlers_seq_stop,
+       .stop   = input_seq_stop,
        .show   = input_handlers_seq_show,
 };
 
@@ -910,8 +1212,6 @@ static int __init input_proc_init(void)
        if (!proc_bus_input_dir)
                return -ENOMEM;
 
-       proc_bus_input_dir->owner = THIS_MODULE;
-
        entry = proc_create("devices", 0, proc_bus_input_dir,
                            &input_devices_fileops);
        if (!entry)
@@ -1019,11 +1319,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
 };
 
@@ -1064,14 +1379,23 @@ static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
 {
        int i;
        int len = 0;
+       bool skip_empty = true;
+
+       for (i = BITS_TO_LONGS(max) - 1; i >= 0; i--) {
+               len += input_bits_to_string(buf + len, max(buf_size - len, 0),
+                                           bitmap[i], skip_empty);
+               if (len) {
+                       skip_empty = false;
+                       if (i > 0)
+                               len += snprintf(buf + len, max(buf_size - len, 0), " ");
+               }
+       }
 
-       for (i = BITS_TO_LONGS(max) - 1; i > 0; i--)
-               if (bitmap[i])
-                       break;
-
-       for (; i >= 0; i--)
-               len += snprintf(buf + len, max(buf_size - len, 0),
-                               "%lx%s", bitmap[i], i > 0 ? " " : "");
+       /*
+        * If no output was produced print a single 0.
+        */
+       if (len == 0)
+               len = snprintf(buf, buf_size, "%d", 0);
 
        if (add_cr)
                len += snprintf(buf + len, max(buf_size - len, 0), "\n");
@@ -1086,7 +1410,8 @@ static ssize_t input_dev_show_cap_##bm(struct device *dev,                \
 {                                                                      \
        struct input_dev *input_dev = to_input_dev(dev);                \
        int len = input_print_bitmap(buf, PAGE_SIZE,                    \
-                                    input_dev->bm##bit, ev##_MAX, 1);  \
+                                    input_dev->bm##bit, ev##_MAX,      \
+                                    true);                             \
        return min_t(int, len, PAGE_SIZE);                              \
 }                                                                      \
 static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
@@ -1119,7 +1444,7 @@ static struct attribute_group input_dev_caps_attr_group = {
        .attrs  = input_dev_caps_attrs,
 };
 
-static struct attribute_group *input_dev_attr_groups[] = {
+static const struct attribute_group *input_dev_attr_groups[] = {
        &input_dev_attr_group,
        &input_dev_id_attr_group,
        &input_dev_caps_attr_group,
@@ -1131,6 +1456,8 @@ static void input_dev_release(struct device *device)
        struct input_dev *dev = to_input_dev(device);
 
        input_ff_destroy(dev);
+       input_mt_destroy_slots(dev);
+       kfree(dev->absinfo);
        kfree(dev);
 
        module_put(THIS_MODULE);
@@ -1145,12 +1472,12 @@ 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],
                                 sizeof(env->buf) - env->buflen,
-                                bitmap, max, 0);
+                                bitmap, max, false);
        if (len >= (sizeof(env->buf) - env->buflen))
                return -ENOMEM;
 
@@ -1211,6 +1538,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);
@@ -1234,14 +1563,117 @@ static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
        return 0;
 }
 
+#define INPUT_DO_TOGGLE(dev, type, bits, on)                           \
+       do {                                                            \
+               int i;                                                  \
+               bool active;                                            \
+                                                                       \
+               if (!test_bit(EV_##type, dev->evbit))                   \
+                       break;                                          \
+                                                                       \
+               for (i = 0; i < type##_MAX; i++) {                      \
+                       if (!test_bit(i, dev->bits##bit))               \
+                               continue;                               \
+                                                                       \
+                       active = test_bit(i, dev->bits);                \
+                       if (!active && !on)                             \
+                               continue;                               \
+                                                                       \
+                       dev->event(dev, EV_##type, i, on ? active : 0); \
+               }                                                       \
+       } while (0)
+
+static void input_dev_toggle(struct input_dev *dev, bool activate)
+{
+       if (!dev->event)
+               return;
+
+       INPUT_DO_TOGGLE(dev, LED, led, activate);
+       INPUT_DO_TOGGLE(dev, SND, snd, activate);
+
+       if (activate && test_bit(EV_REP, dev->evbit)) {
+               dev->event(dev, EV_REP, REP_PERIOD, dev->rep[REP_PERIOD]);
+               dev->event(dev, EV_REP, REP_DELAY, dev->rep[REP_DELAY]);
+       }
+}
+
+/**
+ * input_reset_device() - reset/restore the state of input device
+ * @dev: input device whose state needs to be reset
+ *
+ * This function tries to reset the state of an opened input device and
+ * bring internal state and state if the hardware in sync with each other.
+ * We mark all keys as released, restore LED state, repeat rate, etc.
+ */
+void input_reset_device(struct input_dev *dev)
+{
+       mutex_lock(&dev->mutex);
+
+       if (dev->users) {
+               input_dev_toggle(dev, true);
+
+               /*
+                * 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);
+       }
+
+       mutex_unlock(&dev->mutex);
+}
+EXPORT_SYMBOL(input_reset_device);
+
+#ifdef CONFIG_PM
+static int input_dev_suspend(struct device *dev)
+{
+       struct input_dev *input_dev = to_input_dev(dev);
+
+       mutex_lock(&input_dev->mutex);
+
+       if (input_dev->users)
+               input_dev_toggle(input_dev, false);
+
+       mutex_unlock(&input_dev->mutex);
+
+       return 0;
+}
+
+static int input_dev_resume(struct device *dev)
+{
+       struct input_dev *input_dev = to_input_dev(dev);
+
+       input_reset_device(input_dev);
+
+       return 0;
+}
+
+static const struct dev_pm_ops input_dev_pm_ops = {
+       .suspend        = input_dev_suspend,
+       .resume         = input_dev_resume,
+       .poweroff       = input_dev_suspend,
+       .restore        = input_dev_resume,
+};
+#endif /* CONFIG_PM */
+
 static struct device_type input_dev_type = {
        .groups         = input_dev_attr_groups,
        .release        = input_dev_release,
        .uevent         = input_dev_uevent,
+#ifdef CONFIG_PM
+       .pm             = &input_dev_pm_ops,
+#endif
 };
 
+static char *input_devnode(struct device *dev, mode_t *mode)
+{
+       return kasprintf(GFP_KERNEL, "input/%s", dev_name(dev));
+}
+
 struct class input_class = {
        .name           = "input",
+       .devnode        = input_devnode,
 };
 EXPORT_SYMBOL_GPL(input_class);
 
@@ -1345,9 +1777,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;
        }
@@ -1356,6 +1787,25 @@ void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int
 }
 EXPORT_SYMBOL(input_set_capability);
 
+#define INPUT_CLEANSE_BITMASK(dev, type, bits)                         \
+       do {                                                            \
+               if (!test_bit(EV_##type, dev->evbit))                   \
+                       memset(dev->bits##bit, 0,                       \
+                               sizeof(dev->bits##bit));                \
+       } while (0)
+
+static void input_cleanse_bitmasks(struct input_dev *dev)
+{
+       INPUT_CLEANSE_BITMASK(dev, KEY, key);
+       INPUT_CLEANSE_BITMASK(dev, REL, rel);
+       INPUT_CLEANSE_BITMASK(dev, ABS, abs);
+       INPUT_CLEANSE_BITMASK(dev, MSC, msc);
+       INPUT_CLEANSE_BITMASK(dev, LED, led);
+       INPUT_CLEANSE_BITMASK(dev, SND, snd);
+       INPUT_CLEANSE_BITMASK(dev, FF, ff);
+       INPUT_CLEANSE_BITMASK(dev, SW, sw);
+}
+
 /**
  * input_register_device - register device with input core
  * @dev: device to be registered
@@ -1375,13 +1825,19 @@ int input_register_device(struct input_dev *dev)
        const char *path;
        int error;
 
+       /* Every input device generates EV_SYN/SYN_REPORT events. */
        __set_bit(EV_SYN, dev->evbit);
 
+       /* KEY_RESERVED is not supposed to be transmitted to userspace. */
+       __clear_bit(KEY_RESERVED, dev->keybit);
+
+       /* Make sure that bitmasks not mentioned in dev->evbit are clean. */
+       input_cleanse_bitmasks(dev);
+
        /*
         * 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;
@@ -1390,11 +1846,11 @@ int input_register_device(struct input_dev *dev)
                dev->rep[REP_PERIOD] = 33;
        }
 
-       if (!dev->getkeycode)
-               dev->getkeycode = input_default_getkeycode;
+       if (!dev->getkeycode && !dev->getkeycode_new)
+               dev->getkeycode_new = input_default_getkeycode;
 
-       if (!dev->setkeycode)
-               dev->setkeycode = input_default_setkeycode;
+       if (!dev->setkeycode && !dev->setkeycode_new)
+               dev->setkeycode_new = input_default_setkeycode;
 
        dev_set_name(&dev->dev, "input%ld",
                     (unsigned long) atomic_inc_return(&input_no) - 1);
@@ -1404,8 +1860,9 @@ int input_register_device(struct input_dev *dev)
                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");
+       pr_info("%s as %s\n",
+               dev->name ? dev->name : "Unspecified device",
+               path ? path : "N/A");
        kfree(path);
 
        error = mutex_lock_interruptible(&input_mutex);
@@ -1526,6 +1983,38 @@ void input_unregister_handler(struct input_handler *handler)
 EXPORT_SYMBOL(input_unregister_handler);
 
 /**
+ * input_handler_for_each_handle - handle iterator
+ * @handler: input handler to iterate
+ * @data: data for the callback
+ * @fn: function to be called for each handle
+ *
+ * Iterate over @bus's list of devices, and call @fn for each, passing
+ * it @data and stop when @fn returns a non-zero value. The function is
+ * using RCU to traverse the list and therefore may be usind in atonic
+ * contexts. The @fn callback is invoked from RCU critical section and
+ * thus must not sleep.
+ */
+int input_handler_for_each_handle(struct input_handler *handler, void *data,
+                                 int (*fn)(struct input_handle *, void *))
+{
+       struct input_handle *handle;
+       int retval = 0;
+
+       rcu_read_lock();
+
+       list_for_each_entry_rcu(handle, &handler->h_list, h_node) {
+               retval = fn(handle, data);
+               if (retval)
+                       break;
+       }
+
+       rcu_read_unlock();
+
+       return retval;
+}
+EXPORT_SYMBOL(input_handler_for_each_handle);
+
+/**
  * input_register_handle - register a new input handle
  * @handle: handle to register
  *
@@ -1549,7 +2038,16 @@ int input_register_handle(struct input_handle *handle)
        error = mutex_lock_interruptible(&dev->mutex);
        if (error)
                return error;
-       list_add_tail_rcu(&handle->d_node, &dev->h_list);
+
+       /*
+        * Filters go to the head of the list, normal handlers
+        * to the tail.
+        */
+       if (handler->filter)
+               list_add_rcu(&handle->d_node, &dev->h_list);
+       else
+               list_add_tail_rcu(&handle->d_node, &dev->h_list);
+
        mutex_unlock(&dev->mutex);
 
        /*
@@ -1558,7 +2056,7 @@ int input_register_handle(struct input_handle *handle)
         * we can't be racing with input_unregister_handle()
         * and so separate lock is not needed here.
         */
-       list_add_tail(&handle->h_node, &handler->h_list);
+       list_add_tail_rcu(&handle->h_node, &handler->h_list);
 
        if (handler->start)
                handler->start(handle);
@@ -1581,7 +2079,7 @@ void input_unregister_handle(struct input_handle *handle)
 {
        struct input_dev *dev = handle->dev;
 
-       list_del_init(&handle->h_node);
+       list_del_rcu(&handle->h_node);
 
        /*
         * Take dev->mutex to prevent race with input_release_device().
@@ -1589,6 +2087,7 @@ void input_unregister_handle(struct input_handle *handle)
        mutex_lock(&dev->mutex);
        list_del_rcu(&handle->d_node);
        mutex_unlock(&dev->mutex);
+
        synchronize_rcu();
 }
 EXPORT_SYMBOL(input_unregister_handle);
@@ -1599,41 +2098,44 @@ static int input_open_file(struct inode *inode, struct file *file)
        const struct file_operations *old_fops, *new_fops = NULL;
        int err;
 
-       lock_kernel();
+       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))) {
-               err = -ENODEV;
-               goto out;
-       }
+       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...
         */
-       if (!new_fops->open) {
+       if (!new_fops || !new_fops->open) {
                fops_put(new_fops);
                err = -ENODEV;
                goto out;
        }
+
        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;
 }
 
 static const struct file_operations input_fops = {
        .owner = THIS_MODULE,
        .open = input_open_file,
+       .llseek = noop_llseek,
 };
 
 static int __init input_init(void)
@@ -1642,7 +2144,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;
        }
 
@@ -1652,7 +2154,7 @@ static int __init input_init(void)
 
        err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
        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;
        }