diff options
Diffstat (limited to 'drivers/input/input.c')
-rw-r--r-- | drivers/input/input.c | 266 |
1 files changed, 205 insertions, 61 deletions
diff --git a/drivers/input/input.c b/drivers/input/input.c index a9a706f8fff9..915e9ab7cab0 100644 --- a/drivers/input/input.c +++ b/drivers/input/input.c | |||
@@ -299,12 +299,87 @@ void input_close_device(struct input_handle *handle) | |||
299 | } | 299 | } |
300 | EXPORT_SYMBOL(input_close_device); | 300 | EXPORT_SYMBOL(input_close_device); |
301 | 301 | ||
302 | static void input_link_handle(struct input_handle *handle) | 302 | static int input_fetch_keycode(struct input_dev *dev, int scancode) |
303 | { | 303 | { |
304 | list_add_tail(&handle->d_node, &handle->dev->h_list); | 304 | switch (dev->keycodesize) { |
305 | list_add_tail(&handle->h_node, &handle->handler->h_list); | 305 | case 1: |
306 | return ((u8 *)dev->keycode)[scancode]; | ||
307 | |||
308 | case 2: | ||
309 | return ((u16 *)dev->keycode)[scancode]; | ||
310 | |||
311 | default: | ||
312 | return ((u32 *)dev->keycode)[scancode]; | ||
313 | } | ||
314 | } | ||
315 | |||
316 | static int input_default_getkeycode(struct input_dev *dev, | ||
317 | int scancode, int *keycode) | ||
318 | { | ||
319 | if (!dev->keycodesize) | ||
320 | return -EINVAL; | ||
321 | |||
322 | if (scancode < 0 || scancode >= dev->keycodemax) | ||
323 | return -EINVAL; | ||
324 | |||
325 | *keycode = input_fetch_keycode(dev, scancode); | ||
326 | |||
327 | return 0; | ||
328 | } | ||
329 | |||
330 | static int input_default_setkeycode(struct input_dev *dev, | ||
331 | int scancode, int keycode) | ||
332 | { | ||
333 | int old_keycode; | ||
334 | int i; | ||
335 | |||
336 | if (scancode < 0 || scancode >= dev->keycodemax) | ||
337 | return -EINVAL; | ||
338 | |||
339 | if (keycode < 0 || keycode > KEY_MAX) | ||
340 | return -EINVAL; | ||
341 | |||
342 | if (!dev->keycodesize) | ||
343 | return -EINVAL; | ||
344 | |||
345 | if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8))) | ||
346 | return -EINVAL; | ||
347 | |||
348 | switch (dev->keycodesize) { | ||
349 | case 1: { | ||
350 | u8 *k = (u8 *)dev->keycode; | ||
351 | old_keycode = k[scancode]; | ||
352 | k[scancode] = keycode; | ||
353 | break; | ||
354 | } | ||
355 | case 2: { | ||
356 | u16 *k = (u16 *)dev->keycode; | ||
357 | old_keycode = k[scancode]; | ||
358 | k[scancode] = keycode; | ||
359 | break; | ||
360 | } | ||
361 | default: { | ||
362 | u32 *k = (u32 *)dev->keycode; | ||
363 | old_keycode = k[scancode]; | ||
364 | k[scancode] = keycode; | ||
365 | break; | ||
366 | } | ||
367 | } | ||
368 | |||
369 | clear_bit(old_keycode, dev->keybit); | ||
370 | set_bit(keycode, dev->keybit); | ||
371 | |||
372 | for (i = 0; i < dev->keycodemax; i++) { | ||
373 | if (input_fetch_keycode(dev, i) == old_keycode) { | ||
374 | set_bit(old_keycode, dev->keybit); | ||
375 | break; /* Setting the bit twice is useless, so break */ | ||
376 | } | ||
377 | } | ||
378 | |||
379 | return 0; | ||
306 | } | 380 | } |
307 | 381 | ||
382 | |||
308 | #define MATCH_BIT(bit, max) \ | 383 | #define MATCH_BIT(bit, max) \ |
309 | for (i = 0; i < NBITS(max); i++) \ | 384 | for (i = 0; i < NBITS(max); i++) \ |
310 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ | 385 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
@@ -351,6 +426,29 @@ static const struct input_device_id *input_match_device(const struct input_devic | |||
351 | return NULL; | 426 | return NULL; |
352 | } | 427 | } |
353 | 428 | ||
429 | static int input_attach_handler(struct input_dev *dev, struct input_handler *handler) | ||
430 | { | ||
431 | const struct input_device_id *id; | ||
432 | int error; | ||
433 | |||
434 | if (handler->blacklist && input_match_device(handler->blacklist, dev)) | ||
435 | return -ENODEV; | ||
436 | |||
437 | id = input_match_device(handler->id_table, dev); | ||
438 | if (!id) | ||
439 | return -ENODEV; | ||
440 | |||
441 | error = handler->connect(handler, dev, id); | ||
442 | if (error && error != -ENODEV) | ||
443 | printk(KERN_ERR | ||
444 | "input: failed to attach handler %s to device %s, " | ||
445 | "error: %d\n", | ||
446 | handler->name, kobject_name(&dev->cdev.kobj), error); | ||
447 | |||
448 | return error; | ||
449 | } | ||
450 | |||
451 | |||
354 | #ifdef CONFIG_PROC_FS | 452 | #ifdef CONFIG_PROC_FS |
355 | 453 | ||
356 | static struct proc_dir_entry *proc_bus_input_dir; | 454 | static struct proc_dir_entry *proc_bus_input_dir; |
@@ -439,6 +537,7 @@ static int input_devices_seq_show(struct seq_file *seq, void *v) | |||
439 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); | 537 | seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
440 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); | 538 | seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
441 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); | 539 | seq_printf(seq, "S: Sysfs=%s\n", path ? path : ""); |
540 | seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : ""); | ||
442 | seq_printf(seq, "H: Handlers="); | 541 | seq_printf(seq, "H: Handlers="); |
443 | 542 | ||
444 | list_for_each_entry(handle, &dev->h_list, d_node) | 543 | list_for_each_entry(handle, &dev->h_list, d_node) |
@@ -753,6 +852,13 @@ static struct attribute_group input_dev_caps_attr_group = { | |||
753 | .attrs = input_dev_caps_attrs, | 852 | .attrs = input_dev_caps_attrs, |
754 | }; | 853 | }; |
755 | 854 | ||
855 | static struct attribute_group *input_dev_attr_groups[] = { | ||
856 | &input_dev_attr_group, | ||
857 | &input_dev_id_attr_group, | ||
858 | &input_dev_caps_attr_group, | ||
859 | NULL | ||
860 | }; | ||
861 | |||
756 | static void input_dev_release(struct class_device *class_dev) | 862 | static void input_dev_release(struct class_device *class_dev) |
757 | { | 863 | { |
758 | struct input_dev *dev = to_input_dev(class_dev); | 864 | struct input_dev *dev = to_input_dev(class_dev); |
@@ -906,6 +1012,7 @@ struct input_dev *input_allocate_device(void) | |||
906 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); | 1012 | dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL); |
907 | if (dev) { | 1013 | if (dev) { |
908 | dev->cdev.class = &input_class; | 1014 | dev->cdev.class = &input_class; |
1015 | dev->cdev.groups = input_dev_attr_groups; | ||
909 | class_device_initialize(&dev->cdev); | 1016 | class_device_initialize(&dev->cdev); |
910 | mutex_init(&dev->mutex); | 1017 | mutex_init(&dev->mutex); |
911 | INIT_LIST_HEAD(&dev->h_list); | 1018 | INIT_LIST_HEAD(&dev->h_list); |
@@ -934,23 +1041,71 @@ EXPORT_SYMBOL(input_allocate_device); | |||
934 | */ | 1041 | */ |
935 | void input_free_device(struct input_dev *dev) | 1042 | void input_free_device(struct input_dev *dev) |
936 | { | 1043 | { |
937 | if (dev) { | 1044 | if (dev) |
938 | |||
939 | mutex_lock(&dev->mutex); | ||
940 | dev->name = dev->phys = dev->uniq = NULL; | ||
941 | mutex_unlock(&dev->mutex); | ||
942 | |||
943 | input_put_device(dev); | 1045 | input_put_device(dev); |
944 | } | ||
945 | } | 1046 | } |
946 | EXPORT_SYMBOL(input_free_device); | 1047 | EXPORT_SYMBOL(input_free_device); |
947 | 1048 | ||
1049 | /** | ||
1050 | * input_set_capability - mark device as capable of a certain event | ||
1051 | * @dev: device that is capable of emitting or accepting event | ||
1052 | * @type: type of the event (EV_KEY, EV_REL, etc...) | ||
1053 | * @code: event code | ||
1054 | * | ||
1055 | * In addition to setting up corresponding bit in appropriate capability | ||
1056 | * bitmap the function also adjusts dev->evbit. | ||
1057 | */ | ||
1058 | void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code) | ||
1059 | { | ||
1060 | switch (type) { | ||
1061 | case EV_KEY: | ||
1062 | __set_bit(code, dev->keybit); | ||
1063 | break; | ||
1064 | |||
1065 | case EV_REL: | ||
1066 | __set_bit(code, dev->relbit); | ||
1067 | break; | ||
1068 | |||
1069 | case EV_ABS: | ||
1070 | __set_bit(code, dev->absbit); | ||
1071 | break; | ||
1072 | |||
1073 | case EV_MSC: | ||
1074 | __set_bit(code, dev->mscbit); | ||
1075 | break; | ||
1076 | |||
1077 | case EV_SW: | ||
1078 | __set_bit(code, dev->swbit); | ||
1079 | break; | ||
1080 | |||
1081 | case EV_LED: | ||
1082 | __set_bit(code, dev->ledbit); | ||
1083 | break; | ||
1084 | |||
1085 | case EV_SND: | ||
1086 | __set_bit(code, dev->sndbit); | ||
1087 | break; | ||
1088 | |||
1089 | case EV_FF: | ||
1090 | __set_bit(code, dev->ffbit); | ||
1091 | break; | ||
1092 | |||
1093 | default: | ||
1094 | printk(KERN_ERR | ||
1095 | "input_set_capability: unknown type %u (code %u)\n", | ||
1096 | type, code); | ||
1097 | dump_stack(); | ||
1098 | return; | ||
1099 | } | ||
1100 | |||
1101 | __set_bit(type, dev->evbit); | ||
1102 | } | ||
1103 | EXPORT_SYMBOL(input_set_capability); | ||
1104 | |||
948 | int input_register_device(struct input_dev *dev) | 1105 | int input_register_device(struct input_dev *dev) |
949 | { | 1106 | { |
950 | static atomic_t input_no = ATOMIC_INIT(0); | 1107 | static atomic_t input_no = ATOMIC_INIT(0); |
951 | struct input_handle *handle; | ||
952 | struct input_handler *handler; | 1108 | struct input_handler *handler; |
953 | const struct input_device_id *id; | ||
954 | const char *path; | 1109 | const char *path; |
955 | int error; | 1110 | int error; |
956 | 1111 | ||
@@ -969,55 +1124,41 @@ int input_register_device(struct input_dev *dev) | |||
969 | dev->rep[REP_PERIOD] = 33; | 1124 | dev->rep[REP_PERIOD] = 33; |
970 | } | 1125 | } |
971 | 1126 | ||
1127 | if (!dev->getkeycode) | ||
1128 | dev->getkeycode = input_default_getkeycode; | ||
1129 | |||
1130 | if (!dev->setkeycode) | ||
1131 | dev->setkeycode = input_default_setkeycode; | ||
1132 | |||
972 | list_add_tail(&dev->node, &input_dev_list); | 1133 | list_add_tail(&dev->node, &input_dev_list); |
973 | 1134 | ||
974 | snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id), | 1135 | snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id), |
975 | "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); | 1136 | "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1); |
976 | 1137 | ||
1138 | if (!dev->cdev.dev) | ||
1139 | dev->cdev.dev = dev->dev.parent; | ||
1140 | |||
977 | error = class_device_add(&dev->cdev); | 1141 | error = class_device_add(&dev->cdev); |
978 | if (error) | 1142 | if (error) |
979 | return error; | 1143 | return error; |
980 | 1144 | ||
981 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_attr_group); | ||
982 | if (error) | ||
983 | goto fail1; | ||
984 | |||
985 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_id_attr_group); | ||
986 | if (error) | ||
987 | goto fail2; | ||
988 | |||
989 | error = sysfs_create_group(&dev->cdev.kobj, &input_dev_caps_attr_group); | ||
990 | if (error) | ||
991 | goto fail3; | ||
992 | |||
993 | path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); | 1145 | path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL); |
994 | printk(KERN_INFO "input: %s as %s\n", | 1146 | printk(KERN_INFO "input: %s as %s\n", |
995 | dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); | 1147 | dev->name ? dev->name : "Unspecified device", path ? path : "N/A"); |
996 | kfree(path); | 1148 | kfree(path); |
997 | 1149 | ||
998 | list_for_each_entry(handler, &input_handler_list, node) | 1150 | list_for_each_entry(handler, &input_handler_list, node) |
999 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) | 1151 | input_attach_handler(dev, handler); |
1000 | if ((id = input_match_device(handler->id_table, dev))) | ||
1001 | if ((handle = handler->connect(handler, dev, id))) { | ||
1002 | input_link_handle(handle); | ||
1003 | if (handler->start) | ||
1004 | handler->start(handle); | ||
1005 | } | ||
1006 | 1152 | ||
1007 | input_wakeup_procfs_readers(); | 1153 | input_wakeup_procfs_readers(); |
1008 | 1154 | ||
1009 | return 0; | 1155 | return 0; |
1010 | |||
1011 | fail3: sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group); | ||
1012 | fail2: sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group); | ||
1013 | fail1: class_device_del(&dev->cdev); | ||
1014 | return error; | ||
1015 | } | 1156 | } |
1016 | EXPORT_SYMBOL(input_register_device); | 1157 | EXPORT_SYMBOL(input_register_device); |
1017 | 1158 | ||
1018 | void input_unregister_device(struct input_dev *dev) | 1159 | void input_unregister_device(struct input_dev *dev) |
1019 | { | 1160 | { |
1020 | struct list_head *node, *next; | 1161 | struct input_handle *handle, *next; |
1021 | int code; | 1162 | int code; |
1022 | 1163 | ||
1023 | for (code = 0; code <= KEY_MAX; code++) | 1164 | for (code = 0; code <= KEY_MAX; code++) |
@@ -1027,19 +1168,12 @@ void input_unregister_device(struct input_dev *dev) | |||
1027 | 1168 | ||
1028 | del_timer_sync(&dev->timer); | 1169 | del_timer_sync(&dev->timer); |
1029 | 1170 | ||
1030 | list_for_each_safe(node, next, &dev->h_list) { | 1171 | list_for_each_entry_safe(handle, next, &dev->h_list, d_node) |
1031 | struct input_handle * handle = to_handle(node); | ||
1032 | list_del_init(&handle->d_node); | ||
1033 | list_del_init(&handle->h_node); | ||
1034 | handle->handler->disconnect(handle); | 1172 | handle->handler->disconnect(handle); |
1035 | } | 1173 | WARN_ON(!list_empty(&dev->h_list)); |
1036 | 1174 | ||
1037 | list_del_init(&dev->node); | 1175 | list_del_init(&dev->node); |
1038 | 1176 | ||
1039 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_caps_attr_group); | ||
1040 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_id_attr_group); | ||
1041 | sysfs_remove_group(&dev->cdev.kobj, &input_dev_attr_group); | ||
1042 | |||
1043 | class_device_unregister(&dev->cdev); | 1177 | class_device_unregister(&dev->cdev); |
1044 | 1178 | ||
1045 | input_wakeup_procfs_readers(); | 1179 | input_wakeup_procfs_readers(); |
@@ -1049,8 +1183,6 @@ EXPORT_SYMBOL(input_unregister_device); | |||
1049 | int input_register_handler(struct input_handler *handler) | 1183 | int input_register_handler(struct input_handler *handler) |
1050 | { | 1184 | { |
1051 | struct input_dev *dev; | 1185 | struct input_dev *dev; |
1052 | struct input_handle *handle; | ||
1053 | const struct input_device_id *id; | ||
1054 | 1186 | ||
1055 | INIT_LIST_HEAD(&handler->h_list); | 1187 | INIT_LIST_HEAD(&handler->h_list); |
1056 | 1188 | ||
@@ -1064,13 +1196,7 @@ int input_register_handler(struct input_handler *handler) | |||
1064 | list_add_tail(&handler->node, &input_handler_list); | 1196 | list_add_tail(&handler->node, &input_handler_list); |
1065 | 1197 | ||
1066 | list_for_each_entry(dev, &input_dev_list, node) | 1198 | list_for_each_entry(dev, &input_dev_list, node) |
1067 | if (!handler->blacklist || !input_match_device(handler->blacklist, dev)) | 1199 | input_attach_handler(dev, handler); |
1068 | if ((id = input_match_device(handler->id_table, dev))) | ||
1069 | if ((handle = handler->connect(handler, dev, id))) { | ||
1070 | input_link_handle(handle); | ||
1071 | if (handler->start) | ||
1072 | handler->start(handle); | ||
1073 | } | ||
1074 | 1200 | ||
1075 | input_wakeup_procfs_readers(); | 1201 | input_wakeup_procfs_readers(); |
1076 | return 0; | 1202 | return 0; |
@@ -1079,14 +1205,11 @@ EXPORT_SYMBOL(input_register_handler); | |||
1079 | 1205 | ||
1080 | void input_unregister_handler(struct input_handler *handler) | 1206 | void input_unregister_handler(struct input_handler *handler) |
1081 | { | 1207 | { |
1082 | struct list_head *node, *next; | 1208 | struct input_handle *handle, *next; |
1083 | 1209 | ||
1084 | list_for_each_safe(node, next, &handler->h_list) { | 1210 | list_for_each_entry_safe(handle, next, &handler->h_list, h_node) |
1085 | struct input_handle * handle = to_handle_h(node); | ||
1086 | list_del_init(&handle->h_node); | ||
1087 | list_del_init(&handle->d_node); | ||
1088 | handler->disconnect(handle); | 1211 | handler->disconnect(handle); |
1089 | } | 1212 | WARN_ON(!list_empty(&handler->h_list)); |
1090 | 1213 | ||
1091 | list_del_init(&handler->node); | 1214 | list_del_init(&handler->node); |
1092 | 1215 | ||
@@ -1097,6 +1220,27 @@ void input_unregister_handler(struct input_handler *handler) | |||
1097 | } | 1220 | } |
1098 | EXPORT_SYMBOL(input_unregister_handler); | 1221 | EXPORT_SYMBOL(input_unregister_handler); |
1099 | 1222 | ||
1223 | int input_register_handle(struct input_handle *handle) | ||
1224 | { | ||
1225 | struct input_handler *handler = handle->handler; | ||
1226 | |||
1227 | list_add_tail(&handle->d_node, &handle->dev->h_list); | ||
1228 | list_add_tail(&handle->h_node, &handler->h_list); | ||
1229 | |||
1230 | if (handler->start) | ||
1231 | handler->start(handle); | ||
1232 | |||
1233 | return 0; | ||
1234 | } | ||
1235 | EXPORT_SYMBOL(input_register_handle); | ||
1236 | |||
1237 | void input_unregister_handle(struct input_handle *handle) | ||
1238 | { | ||
1239 | list_del_init(&handle->h_node); | ||
1240 | list_del_init(&handle->d_node); | ||
1241 | } | ||
1242 | EXPORT_SYMBOL(input_unregister_handle); | ||
1243 | |||
1100 | static int input_open_file(struct inode *inode, struct file *file) | 1244 | static int input_open_file(struct inode *inode, struct file *file) |
1101 | { | 1245 | { |
1102 | struct input_handler *handler = input_table[iminor(inode) >> 5]; | 1246 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |