Input: fix formatting to better follow CodingStyle
[linux-2.6.git] / drivers / input / input.c
index db52ba0..7570a3f 100644 (file)
@@ -29,6 +29,7 @@ MODULE_DESCRIPTION("Input core");
 MODULE_LICENSE("GPL");
 
 EXPORT_SYMBOL(input_allocate_device);
+EXPORT_SYMBOL(input_free_device);
 EXPORT_SYMBOL(input_register_device);
 EXPORT_SYMBOL(input_unregister_device);
 EXPORT_SYMBOL(input_register_handler);
@@ -63,11 +64,13 @@ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, in
                case EV_SYN:
                        switch (code) {
                                case SYN_CONFIG:
-                                       if (dev->event) dev->event(dev, type, code, value);
+                                       if (dev->event)
+                                               dev->event(dev, type, code, value);
                                        break;
 
                                case SYN_REPORT:
-                                       if (dev->sync) return;
+                                       if (dev->sync)
+                                               return;
                                        dev->sync = 1;
                                        break;
                        }
@@ -136,7 +139,8 @@ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, in
                        if (code > MSC_MAX || !test_bit(code, dev->mscbit))
                                return;
 
-                       if (dev->event) dev->event(dev, type, code, value);
+                       if (dev->event)
+                               dev->event(dev, type, code, value);
 
                        break;
 
@@ -146,7 +150,9 @@ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, in
                                return;
 
                        change_bit(code, dev->led);
-                       if (dev->event) dev->event(dev, type, code, value);
+
+                       if (dev->event)
+                               dev->event(dev, type, code, value);
 
                        break;
 
@@ -155,21 +161,28 @@ void input_event(struct input_dev *dev, unsigned int type, unsigned int code, in
                        if (code > SND_MAX || !test_bit(code, dev->sndbit))
                                return;
 
-                       if (dev->event) dev->event(dev, type, code, value);
+                       if (!!test_bit(code, dev->snd) != !!value)
+                               change_bit(code, dev->snd);
+
+                       if (dev->event)
+                               dev->event(dev, type, code, value);
 
                        break;
 
                case EV_REP:
 
-                       if (code > REP_MAX || value < 0 || dev->rep[code] == value) return;
+                       if (code > REP_MAX || value < 0 || dev->rep[code] == value)
+                               return;
 
                        dev->rep[code] = value;
-                       if (dev->event) dev->event(dev, type, code, value);
+                       if (dev->event)
+                               dev->event(dev, type, code, value);
 
                        break;
 
                case EV_FF:
-                       if (dev->event) dev->event(dev, type, code, value);
+                       if (dev->event)
+                               dev->event(dev, type, code, value);
                        break;
        }
 
@@ -286,19 +299,19 @@ static struct input_device_id *input_match_device(struct input_device_id *id, st
        for (; id->flags || id->driver_info; id++) {
 
                if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
-                       if (id->id.bustype != dev->id.bustype)
+                       if (id->bustype != dev->id.bustype)
                                continue;
 
                if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
-                       if (id->id.vendor != dev->id.vendor)
+                       if (id->vendor != dev->id.vendor)
                                continue;
 
                if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
-                       if (id->id.product != dev->id.product)
+                       if (id->product != dev->id.product)
                                continue;
 
                if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
-                       if (id->id.version != dev->id.version)
+                       if (id->version != dev->id.version)
                                continue;
 
                MATCH_BIT(evbit,  EV_MAX);
@@ -332,9 +345,11 @@ static inline void input_wakeup_procfs_readers(void)
 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
 {
        int state = input_devices_state;
+
        poll_wait(file, &input_devices_poll_wait, wait);
        if (state != input_devices_state)
                return POLLIN | POLLRDNORM;
+
        return 0;
 }
 
@@ -571,15 +586,16 @@ INPUT_DEV_STRING_ATTR_SHOW(name);
 INPUT_DEV_STRING_ATTR_SHOW(phys);
 INPUT_DEV_STRING_ATTR_SHOW(uniq);
 
-static int print_modalias_bits(char *buf, int size, char prefix, unsigned long *arr,
-                              unsigned int min, unsigned int max)
+static int input_print_modalias_bits(char *buf, int size,
+                                    char name, unsigned long *bm,
+                                    unsigned int min_bit, unsigned int max_bit)
 {
-       int len, i;
+       int len = 0, i;
 
-       len = snprintf(buf, size, "%c", prefix);
-       for (i = min; i < max; i++)
-               if (arr[LONG(i)] & BIT(i))
-                       len += snprintf(buf + len, size - len, "%X,", i);
+       len += snprintf(buf, max(size, 0), "%c", name);
+       for (i = min_bit; i < max_bit; i++)
+               if (bm[LONG(i)] & BIT(i))
+                       len += snprintf(buf + len, max(size - len, 0), "%X,", i);
        return len;
 }
 
@@ -588,33 +604,32 @@ static int input_print_modalias(char *buf, int size, struct input_dev *id,
 {
        int len;
 
-       len = snprintf(buf, size, "input:b%04Xv%04Xp%04Xe%04X-",
-                      id->id.bustype,
-                      id->id.vendor,
-                      id->id.product,
-                      id->id.version);
-
-       len += print_modalias_bits(buf + len, size - len, 'e', id->evbit,
-                                  0, EV_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'k', id->keybit,
-                                  KEY_MIN_INTERESTING, KEY_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'r', id->relbit,
-                                  0, REL_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'a', id->absbit,
-                                  0, ABS_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'm', id->mscbit,
-                                  0, MSC_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'l', id->ledbit,
-                                  0, LED_MAX);
-       len += print_modalias_bits(buf + len, size - len, 's', id->sndbit,
-                                  0, SND_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'f', id->ffbit,
-                                  0, FF_MAX);
-       len += print_modalias_bits(buf + len, size - len, 'w', id->swbit,
-                                  0, SW_MAX);
+       len = snprintf(buf, max(size, 0),
+                      "input:b%04Xv%04Xp%04Xe%04X-",
+                      id->id.bustype, id->id.vendor,
+                      id->id.product, id->id.version);
+
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'e', id->evbit, 0, EV_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'r', id->relbit, 0, REL_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'a', id->absbit, 0, ABS_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'm', id->mscbit, 0, MSC_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'l', id->ledbit, 0, LED_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               's', id->sndbit, 0, SND_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'f', id->ffbit, 0, FF_MAX);
+       len += input_print_modalias_bits(buf + len, size - len,
+                               'w', id->swbit, 0, SW_MAX);
 
        if (add_cr)
-               len += snprintf(buf + len, size - len, "\n");
+               len += snprintf(buf + len, max(size - len, 0), "\n");
 
        return len;
 }
@@ -626,7 +641,7 @@ static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
 
        len = input_print_modalias(buf, PAGE_SIZE, id, 1);
 
-       return max_t(int, len, PAGE_SIZE);
+       return min_t(int, len, PAGE_SIZE);
 }
 static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
 
@@ -739,8 +754,8 @@ static void input_dev_release(struct class_device *class_dev)
  * device bitfields.
  */
 static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
-                                   char *buffer, int buffer_size, int *cur_len,
-                                   const char *name, unsigned long *bitmap, int max)
+                                  char *buffer, int buffer_size, int *cur_len,
+                                  const char *name, unsigned long *bitmap, int max)
 {
        if (*cur_index >= num_envp - 1)
                return -ENOMEM;
@@ -748,7 +763,7 @@ static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
        envp[*cur_index] = buffer + *cur_len;
 
        *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
-       if (*cur_len > buffer_size)
+       if (*cur_len >= buffer_size)
                return -ENOMEM;
 
        *cur_len += input_print_bitmap(buffer + *cur_len,
@@ -761,9 +776,33 @@ static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
        return 0;
 }
 
+static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
+                                        char *buffer, int buffer_size, int *cur_len,
+                                        struct input_dev *dev)
+{
+       if (*cur_index >= num_envp - 1)
+               return -ENOMEM;
+
+       envp[*cur_index] = buffer + *cur_len;
+
+       *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
+                            "MODALIAS=");
+       if (*cur_len >= buffer_size)
+               return -ENOMEM;
+
+       *cur_len += input_print_modalias(buffer + *cur_len,
+                                        max(buffer_size - *cur_len, 0),
+                                        dev, 0) + 1;
+       if (*cur_len > buffer_size)
+               return -ENOMEM;
+
+       (*cur_index)++;
+       return 0;
+}
+
 #define INPUT_ADD_HOTPLUG_VAR(fmt, val...)                             \
        do {                                                            \
-               int err = add_uevent_var(envp, num_envp, &i,    \
+               int err = add_uevent_var(envp, num_envp, &i,            \
                                        buffer, buffer_size, &len,      \
                                        fmt, val);                      \
                if (err)                                                \
@@ -779,6 +818,16 @@ static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
                        return err;                                     \
        } while (0)
 
+#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev)                            \
+       do {                                                            \
+               int err = input_add_uevent_modalias_var(envp,           \
+                                       num_envp, &i,                   \
+                                       buffer, buffer_size, &len,      \
+                                       dev);                           \
+               if (err)                                                \
+                       return err;                                     \
+       } while (0)
+
 static int input_dev_uevent(struct class_device *cdev, char **envp,
                            int num_envp, char *buffer, int buffer_size)
 {
@@ -814,9 +863,7 @@ static int input_dev_uevent(struct class_device *cdev, char **envp,
        if (test_bit(EV_SW, dev->evbit))
                INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
 
-       envp[i++] = buffer + len;
-       len += snprintf(buffer + len, buffer_size - len, "MODALIAS=");
-       len += input_print_modalias(buffer + len, buffer_size - len, dev, 0) + 1;
+       INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
 
        envp[i] = NULL;
        return 0;
@@ -837,6 +884,7 @@ struct input_dev *input_allocate_device(void)
                dev->dynalloc = 1;
                dev->cdev.class = &input_class;
                class_device_initialize(&dev->cdev);
+               mutex_init(&dev->mutex);
                INIT_LIST_HEAD(&dev->h_list);
                INIT_LIST_HEAD(&dev->node);
        }
@@ -844,6 +892,18 @@ struct input_dev *input_allocate_device(void)
        return dev;
 }
 
+void input_free_device(struct input_dev *dev)
+{
+       if (dev) {
+
+               mutex_lock(&dev->mutex);
+               dev->name = dev->phys = dev->uniq = NULL;
+               mutex_unlock(&dev->mutex);
+
+               input_put_device(dev);
+       }
+}
+
 int input_register_device(struct input_dev *dev)
 {
        static atomic_t input_no = ATOMIC_INIT(0);
@@ -860,7 +920,6 @@ int input_register_device(struct input_dev *dev)
                return -EINVAL;
        }
 
-       mutex_init(&dev->mutex);
        set_bit(EV_SYN, dev->evbit);
 
        /*
@@ -924,9 +983,10 @@ int input_register_device(struct input_dev *dev)
 
 void input_unregister_device(struct input_dev *dev)
 {
-       struct list_head * node, * next;
+       struct list_head *node, *next;
 
-       if (!dev) return;
+       if (!dev)
+               return;
 
        del_timer_sync(&dev->timer);
 
@@ -944,6 +1004,10 @@ void input_unregister_device(struct input_dev *dev)
        sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group);
        class_device_unregister(&dev->cdev);
 
+       mutex_lock(&dev->mutex);
+       dev->name = dev->phys = dev->uniq = NULL;
+       mutex_unlock(&dev->mutex);
+
        input_wakeup_procfs_readers();
 }
 
@@ -953,7 +1017,8 @@ void input_register_handler(struct input_handler *handler)
        struct input_handle *handle;
        struct input_device_id *id;
 
-       if (!handler) return;
+       if (!handler)
+               return;
 
        INIT_LIST_HEAD(&handler->h_list);
 
@@ -973,7 +1038,7 @@ void input_register_handler(struct input_handler *handler)
 
 void input_unregister_handler(struct input_handler *handler)
 {
-       struct list_head * node, * next;
+       struct list_head *node, *next;
 
        list_for_each_safe(node, next, &handler->h_list) {
                struct input_handle * handle = to_handle_h(node);