Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1 | /* rc-core.c - handle IR scancode->keycode tables |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 2 | * |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 3 | * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com> |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation version 2 of the License. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 13 | */ |
| 14 | |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 15 | #include <media/ir-core.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/delay.h> |
Mauro Carvalho Chehab | 882ead3 | 2009-12-29 10:37:38 -0300 | [diff] [blame] | 18 | #include <linux/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 20 | #include <linux/device.h> |
Mauro Carvalho Chehab | f62de67 | 2010-11-09 23:09:57 -0300 | [diff] [blame] | 21 | #include "rc-core-priv.h" |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 22 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 23 | #define IRRCV_NUM_DEVICES 256 |
| 24 | |
| 25 | /* bit array to represent IR sysfs device number */ |
| 26 | static unsigned long ir_core_dev_number; |
| 27 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 28 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ |
| 29 | #define IR_TAB_MIN_SIZE 256 |
| 30 | #define IR_TAB_MAX_SIZE 8192 |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 31 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 32 | /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */ |
| 33 | #define IR_KEYPRESS_TIMEOUT 250 |
| 34 | |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 35 | /* Used to keep track of known keymaps */ |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 36 | static LIST_HEAD(rc_map_list); |
| 37 | static DEFINE_SPINLOCK(rc_map_lock); |
| 38 | |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 39 | /* Forward declarations */ |
| 40 | static int ir_register_class(struct input_dev *input_dev); |
| 41 | static void ir_unregister_class(struct input_dev *input_dev); |
| 42 | static int ir_register_input(struct input_dev *input_dev); |
| 43 | |
| 44 | |
Mauro Carvalho Chehab | 631493e | 2010-11-09 23:44:27 -0300 | [diff] [blame] | 45 | static struct rc_keymap *seek_rc_map(const char *name) |
| 46 | { |
| 47 | struct rc_keymap *map = NULL; |
| 48 | |
| 49 | spin_lock(&rc_map_lock); |
| 50 | list_for_each_entry(map, &rc_map_list, list) { |
| 51 | if (!strcmp(name, map->map.name)) { |
| 52 | spin_unlock(&rc_map_lock); |
| 53 | return map; |
| 54 | } |
| 55 | } |
| 56 | spin_unlock(&rc_map_lock); |
| 57 | |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | struct ir_scancode_table *get_rc_map(const char *name) |
| 62 | { |
| 63 | |
| 64 | struct rc_keymap *map; |
| 65 | |
| 66 | map = seek_rc_map(name); |
| 67 | #ifdef MODULE |
| 68 | if (!map) { |
| 69 | int rc = request_module(name); |
| 70 | if (rc < 0) { |
| 71 | printk(KERN_ERR "Couldn't load IR keymap %s\n", name); |
| 72 | return NULL; |
| 73 | } |
| 74 | msleep(20); /* Give some time for IR to register */ |
| 75 | |
| 76 | map = seek_rc_map(name); |
| 77 | } |
| 78 | #endif |
| 79 | if (!map) { |
| 80 | printk(KERN_ERR "IR keymap %s not found\n", name); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | printk(KERN_INFO "Registered IR keymap %s\n", map->map.name); |
| 85 | |
| 86 | return &map->map; |
| 87 | } |
| 88 | EXPORT_SYMBOL_GPL(get_rc_map); |
| 89 | |
| 90 | int ir_register_map(struct rc_keymap *map) |
| 91 | { |
| 92 | spin_lock(&rc_map_lock); |
| 93 | list_add_tail(&map->list, &rc_map_list); |
| 94 | spin_unlock(&rc_map_lock); |
| 95 | return 0; |
| 96 | } |
| 97 | EXPORT_SYMBOL_GPL(ir_register_map); |
| 98 | |
| 99 | void ir_unregister_map(struct rc_keymap *map) |
| 100 | { |
| 101 | spin_lock(&rc_map_lock); |
| 102 | list_del(&map->list); |
| 103 | spin_unlock(&rc_map_lock); |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(ir_unregister_map); |
| 106 | |
| 107 | |
| 108 | static struct ir_scancode empty[] = { |
| 109 | { 0x2a, KEY_COFFEE }, |
| 110 | }; |
| 111 | |
| 112 | static struct rc_keymap empty_map = { |
| 113 | .map = { |
| 114 | .scan = empty, |
| 115 | .size = ARRAY_SIZE(empty), |
| 116 | .ir_type = IR_TYPE_UNKNOWN, /* Legacy IR type */ |
| 117 | .name = RC_MAP_EMPTY, |
| 118 | } |
| 119 | }; |
| 120 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 121 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 122 | * ir_create_table() - initializes a scancode table |
| 123 | * @rc_tab: the ir_scancode_table to initialize |
| 124 | * @name: name to assign to the table |
| 125 | * @ir_type: ir type to assign to the new table |
| 126 | * @size: initial size of the table |
| 127 | * @return: zero on success or a negative error code |
| 128 | * |
| 129 | * This routine will initialize the ir_scancode_table and will allocate |
| 130 | * memory to hold at least the specified number elements. |
| 131 | */ |
| 132 | static int ir_create_table(struct ir_scancode_table *rc_tab, |
| 133 | const char *name, u64 ir_type, size_t size) |
| 134 | { |
| 135 | rc_tab->name = name; |
| 136 | rc_tab->ir_type = ir_type; |
| 137 | rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode)); |
| 138 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 139 | rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL); |
| 140 | if (!rc_tab->scan) |
| 141 | return -ENOMEM; |
| 142 | |
| 143 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 144 | rc_tab->size, rc_tab->alloc); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * ir_free_table() - frees memory allocated by a scancode table |
| 150 | * @rc_tab: the table whose mappings need to be freed |
| 151 | * |
| 152 | * This routine will free memory alloctaed for key mappings used by given |
| 153 | * scancode table. |
| 154 | */ |
| 155 | static void ir_free_table(struct ir_scancode_table *rc_tab) |
| 156 | { |
| 157 | rc_tab->size = 0; |
| 158 | kfree(rc_tab->scan); |
| 159 | rc_tab->scan = NULL; |
| 160 | } |
| 161 | |
| 162 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 163 | * ir_resize_table() - resizes a scancode table if necessary |
| 164 | * @rc_tab: the ir_scancode_table to resize |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 165 | * @gfp_flags: gfp flags to use when allocating memory |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 166 | * @return: zero on success or a negative error code |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 167 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 168 | * This routine will shrink the ir_scancode_table if it has lots of |
| 169 | * unused entries and grow it if it is full. |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 170 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 171 | static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 172 | { |
| 173 | unsigned int oldalloc = rc_tab->alloc; |
| 174 | unsigned int newalloc = oldalloc; |
| 175 | struct ir_scancode *oldscan = rc_tab->scan; |
| 176 | struct ir_scancode *newscan; |
| 177 | |
| 178 | if (rc_tab->size == rc_tab->len) { |
| 179 | /* All entries in use -> grow keytable */ |
| 180 | if (rc_tab->alloc >= IR_TAB_MAX_SIZE) |
| 181 | return -ENOMEM; |
| 182 | |
| 183 | newalloc *= 2; |
| 184 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); |
| 185 | } |
| 186 | |
| 187 | if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
| 188 | /* Less than 1/3 of entries in use -> shrink keytable */ |
| 189 | newalloc /= 2; |
| 190 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); |
| 191 | } |
| 192 | |
| 193 | if (newalloc == oldalloc) |
| 194 | return 0; |
| 195 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 196 | newscan = kmalloc(newalloc, gfp_flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 197 | if (!newscan) { |
| 198 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); |
| 199 | return -ENOMEM; |
| 200 | } |
| 201 | |
| 202 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
| 203 | rc_tab->scan = newscan; |
| 204 | rc_tab->alloc = newalloc; |
| 205 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 206 | kfree(oldscan); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 211 | * ir_update_mapping() - set a keycode in the scancode->keycode table |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 212 | * @dev: the struct input_dev device descriptor |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 213 | * @rc_tab: scancode table to be adjusted |
| 214 | * @index: index of the mapping that needs to be updated |
| 215 | * @keycode: the desired keycode |
| 216 | * @return: previous keycode assigned to the mapping |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 217 | * |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 218 | * This routine is used to update scancode->keycopde mapping at given |
| 219 | * position. |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 220 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 221 | static unsigned int ir_update_mapping(struct input_dev *dev, |
| 222 | struct ir_scancode_table *rc_tab, |
| 223 | unsigned int index, |
| 224 | unsigned int new_keycode) |
| 225 | { |
| 226 | int old_keycode = rc_tab->scan[index].keycode; |
| 227 | int i; |
| 228 | |
| 229 | /* Did the user wish to remove the mapping? */ |
| 230 | if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) { |
| 231 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", |
| 232 | index, rc_tab->scan[index].scancode); |
| 233 | rc_tab->len--; |
| 234 | memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1], |
| 235 | (rc_tab->len - index) * sizeof(struct ir_scancode)); |
| 236 | } else { |
| 237 | IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n", |
| 238 | index, |
| 239 | old_keycode == KEY_RESERVED ? "New" : "Replacing", |
| 240 | rc_tab->scan[index].scancode, new_keycode); |
| 241 | rc_tab->scan[index].keycode = new_keycode; |
| 242 | __set_bit(new_keycode, dev->keybit); |
| 243 | } |
| 244 | |
| 245 | if (old_keycode != KEY_RESERVED) { |
| 246 | /* A previous mapping was updated... */ |
| 247 | __clear_bit(old_keycode, dev->keybit); |
| 248 | /* ... but another scancode might use the same keycode */ |
| 249 | for (i = 0; i < rc_tab->len; i++) { |
| 250 | if (rc_tab->scan[i].keycode == old_keycode) { |
| 251 | __set_bit(old_keycode, dev->keybit); |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* Possibly shrink the keytable, failure is not a problem */ |
| 257 | ir_resize_table(rc_tab, GFP_ATOMIC); |
| 258 | } |
| 259 | |
| 260 | return old_keycode; |
| 261 | } |
| 262 | |
| 263 | /** |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 264 | * ir_establish_scancode() - set a keycode in the scancode->keycode table |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 265 | * @ir_dev: the struct ir_input_dev device descriptor |
| 266 | * @rc_tab: scancode table to be searched |
| 267 | * @scancode: the desired scancode |
| 268 | * @resize: controls whether we allowed to resize the table to |
| 269 | * accomodate not yet present scancodes |
| 270 | * @return: index of the mapping containing scancode in question |
| 271 | * or -1U in case of failure. |
| 272 | * |
| 273 | * This routine is used to locate given scancode in ir_scancode_table. |
| 274 | * If scancode is not yet present the routine will allocate a new slot |
| 275 | * for it. |
| 276 | */ |
| 277 | static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev, |
| 278 | struct ir_scancode_table *rc_tab, |
| 279 | unsigned int scancode, |
| 280 | bool resize) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 281 | { |
| 282 | unsigned int i; |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 283 | |
| 284 | /* |
| 285 | * Unfortunately, some hardware-based IR decoders don't provide |
| 286 | * all bits for the complete IR code. In general, they provide only |
| 287 | * the command part of the IR code. Yet, as it is possible to replace |
| 288 | * the provided IR with another one, it is needed to allow loading |
| 289 | * IR tables from other remotes. So, |
| 290 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 291 | if (ir_dev->props && ir_dev->props->scanmask) |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 292 | scancode &= ir_dev->props->scanmask; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 293 | |
| 294 | /* First check if we already have a mapping for this ir command */ |
| 295 | for (i = 0; i < rc_tab->len; i++) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 296 | if (rc_tab->scan[i].scancode == scancode) |
| 297 | return i; |
| 298 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 299 | /* Keytable is sorted from lowest to highest scancode */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 300 | if (rc_tab->scan[i].scancode >= scancode) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 301 | break; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 302 | } |
| 303 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 304 | /* No previous mapping found, we might need to grow the table */ |
| 305 | if (rc_tab->size == rc_tab->len) { |
| 306 | if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC)) |
| 307 | return -1U; |
| 308 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 309 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 310 | /* i is the proper index to insert our new keycode */ |
| 311 | if (i < rc_tab->len) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 312 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], |
| 313 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 314 | rc_tab->scan[i].scancode = scancode; |
| 315 | rc_tab->scan[i].keycode = KEY_RESERVED; |
| 316 | rc_tab->len++; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 317 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 318 | return i; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | /** |
| 322 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
| 323 | * @dev: the struct input_dev device descriptor |
| 324 | * @scancode: the desired scancode |
| 325 | * @keycode: result |
| 326 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 327 | * |
| 328 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 329 | */ |
| 330 | static int ir_setkeycode(struct input_dev *dev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 331 | const struct input_keymap_entry *ke, |
| 332 | unsigned int *old_keycode) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 333 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 334 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 335 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 336 | unsigned int index; |
| 337 | unsigned int scancode; |
| 338 | int retval; |
| 339 | unsigned long flags; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 340 | |
| 341 | spin_lock_irqsave(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 342 | |
| 343 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 344 | index = ke->index; |
| 345 | if (index >= rc_tab->len) { |
| 346 | retval = -EINVAL; |
| 347 | goto out; |
| 348 | } |
| 349 | } else { |
| 350 | retval = input_scancode_to_scalar(ke, &scancode); |
| 351 | if (retval) |
| 352 | goto out; |
| 353 | |
| 354 | index = ir_establish_scancode(ir_dev, rc_tab, scancode, true); |
| 355 | if (index >= rc_tab->len) { |
| 356 | retval = -ENOMEM; |
| 357 | goto out; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode); |
| 362 | |
| 363 | out: |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 364 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 365 | return retval; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 369 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
| 370 | * @dev: the struct input_dev device descriptor |
| 371 | * @to: the struct ir_scancode_table to copy entries to |
| 372 | * @from: the struct ir_scancode_table to copy entries from |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 373 | * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 374 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 375 | * This routine is used to handle table initialization. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 376 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 377 | static int ir_setkeytable(struct ir_input_dev *ir_dev, |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 378 | const struct ir_scancode_table *from) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 379 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 380 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 381 | unsigned int i, index; |
| 382 | int rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 383 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 384 | rc = ir_create_table(&ir_dev->rc_tab, |
| 385 | from->name, from->ir_type, from->size); |
| 386 | if (rc) |
| 387 | return rc; |
| 388 | |
| 389 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 390 | rc_tab->size, rc_tab->alloc); |
| 391 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 392 | for (i = 0; i < from->size; i++) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 393 | index = ir_establish_scancode(ir_dev, rc_tab, |
| 394 | from->scan[i].scancode, false); |
| 395 | if (index >= rc_tab->len) { |
| 396 | rc = -ENOMEM; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 397 | break; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | ir_update_mapping(ir_dev->input_dev, rc_tab, index, |
| 401 | from->scan[i].keycode); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 402 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 403 | |
| 404 | if (rc) |
| 405 | ir_free_table(rc_tab); |
| 406 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 407 | return rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 408 | } |
| 409 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 410 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 411 | * ir_lookup_by_scancode() - locate mapping by scancode |
| 412 | * @rc_tab: the &struct ir_scancode_table to search |
| 413 | * @scancode: scancode to look for in the table |
| 414 | * @return: index in the table, -1U if not found |
| 415 | * |
| 416 | * This routine performs binary search in RC keykeymap table for |
| 417 | * given scancode. |
| 418 | */ |
| 419 | static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab, |
| 420 | unsigned int scancode) |
| 421 | { |
David Härdeman | 0d07025 | 2010-10-30 22:17:44 +0200 | [diff] [blame] | 422 | int start = 0; |
| 423 | int end = rc_tab->len - 1; |
| 424 | int mid; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 425 | |
| 426 | while (start <= end) { |
| 427 | mid = (start + end) / 2; |
| 428 | if (rc_tab->scan[mid].scancode < scancode) |
| 429 | start = mid + 1; |
| 430 | else if (rc_tab->scan[mid].scancode > scancode) |
| 431 | end = mid - 1; |
| 432 | else |
| 433 | return mid; |
| 434 | } |
| 435 | |
| 436 | return -1U; |
| 437 | } |
| 438 | |
| 439 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 440 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 441 | * @dev: the struct input_dev device descriptor |
| 442 | * @scancode: the desired scancode |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 443 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
| 444 | * @return: always returns zero. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 445 | * |
| 446 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 447 | */ |
| 448 | static int ir_getkeycode(struct input_dev *dev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 449 | struct input_keymap_entry *ke) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 450 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 451 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 452 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 453 | struct ir_scancode *entry; |
| 454 | unsigned long flags; |
| 455 | unsigned int index; |
| 456 | unsigned int scancode; |
| 457 | int retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 458 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 459 | spin_lock_irqsave(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 460 | |
| 461 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 462 | index = ke->index; |
| 463 | } else { |
| 464 | retval = input_scancode_to_scalar(ke, &scancode); |
| 465 | if (retval) |
| 466 | goto out; |
| 467 | |
| 468 | index = ir_lookup_by_scancode(rc_tab, scancode); |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 469 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 470 | |
| 471 | if (index >= rc_tab->len) { |
| 472 | if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) |
| 473 | IR_dprintk(1, "unknown key for scancode 0x%04x\n", |
| 474 | scancode); |
| 475 | retval = -EINVAL; |
| 476 | goto out; |
| 477 | } |
| 478 | |
| 479 | entry = &rc_tab->scan[index]; |
| 480 | |
| 481 | ke->index = index; |
| 482 | ke->keycode = entry->keycode; |
| 483 | ke->len = sizeof(entry->scancode); |
| 484 | memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode)); |
| 485 | |
Dmitry Torokhov | 47c5ba5 | 2010-10-31 15:18:42 -0700 | [diff] [blame] | 486 | retval = 0; |
| 487 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 488 | out: |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 489 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 490 | return retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | /** |
| 494 | * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 495 | * @input_dev: the struct input_dev descriptor of the device |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 496 | * @scancode: the scancode that we're seeking |
| 497 | * |
| 498 | * This routine is used by the input routines when a key is pressed at the |
| 499 | * IR. The scancode is received and needs to be converted into a keycode. |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 500 | * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 501 | * corresponding keycode from the table. |
| 502 | */ |
| 503 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 504 | { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 505 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 506 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
| 507 | unsigned int keycode; |
| 508 | unsigned int index; |
| 509 | unsigned long flags; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 510 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 511 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 512 | |
| 513 | index = ir_lookup_by_scancode(rc_tab, scancode); |
| 514 | keycode = index < rc_tab->len ? |
| 515 | rc_tab->scan[index].keycode : KEY_RESERVED; |
| 516 | |
| 517 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 518 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 519 | if (keycode != KEY_RESERVED) |
| 520 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 521 | dev->name, scancode, keycode); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 522 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 523 | return keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 524 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 525 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 526 | |
| 527 | /** |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 528 | * ir_do_keyup() - internal function to signal the release of a keypress |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 529 | * @ir: the struct ir_input_dev descriptor of the device |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 530 | * |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 531 | * This function is used internally to release a keypress, it must be |
| 532 | * called with keylock held. |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 533 | */ |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 534 | static void ir_do_keyup(struct ir_input_dev *ir) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 535 | { |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 536 | if (!ir->keypressed) |
| 537 | return; |
| 538 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 539 | IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode); |
| 540 | input_report_key(ir->input_dev, ir->last_keycode, 0); |
| 541 | input_sync(ir->input_dev); |
| 542 | ir->keypressed = false; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 543 | } |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 544 | |
| 545 | /** |
| 546 | * ir_keyup() - generates input event to signal the release of a keypress |
| 547 | * @dev: the struct input_dev descriptor of the device |
| 548 | * |
| 549 | * This routine is used to signal that a key has been released on the |
| 550 | * remote control. |
| 551 | */ |
| 552 | void ir_keyup(struct input_dev *dev) |
| 553 | { |
| 554 | unsigned long flags; |
| 555 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 556 | |
| 557 | spin_lock_irqsave(&ir->keylock, flags); |
| 558 | ir_do_keyup(ir); |
| 559 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 560 | } |
Jarod Wilson | ee08940 | 2010-09-15 15:31:12 -0300 | [diff] [blame] | 561 | EXPORT_SYMBOL_GPL(ir_keyup); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 562 | |
| 563 | /** |
| 564 | * ir_timer_keyup() - generates a keyup event after a timeout |
| 565 | * @cookie: a pointer to struct ir_input_dev passed to setup_timer() |
| 566 | * |
| 567 | * This routine will generate a keyup event some time after a keydown event |
| 568 | * is generated when no further activity has been detected. |
| 569 | */ |
| 570 | static void ir_timer_keyup(unsigned long cookie) |
| 571 | { |
| 572 | struct ir_input_dev *ir = (struct ir_input_dev *)cookie; |
| 573 | unsigned long flags; |
| 574 | |
| 575 | /* |
| 576 | * ir->keyup_jiffies is used to prevent a race condition if a |
| 577 | * hardware interrupt occurs at this point and the keyup timer |
| 578 | * event is moved further into the future as a result. |
| 579 | * |
| 580 | * The timer will then be reactivated and this function called |
| 581 | * again in the future. We need to exit gracefully in that case |
| 582 | * to allow the input subsystem to do its auto-repeat magic or |
| 583 | * a keyup event might follow immediately after the keydown. |
| 584 | */ |
| 585 | spin_lock_irqsave(&ir->keylock, flags); |
Maxim Levitsky | e0172fd | 2010-09-06 18:26:09 -0300 | [diff] [blame] | 586 | if (time_is_before_eq_jiffies(ir->keyup_jiffies)) |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 587 | ir_do_keyup(ir); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 588 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * ir_repeat() - notifies the IR core that a key is still pressed |
| 593 | * @dev: the struct input_dev descriptor of the device |
| 594 | * |
| 595 | * This routine is used by IR decoders when a repeat message which does |
| 596 | * not include the necessary bits to reproduce the scancode has been |
| 597 | * received. |
| 598 | */ |
| 599 | void ir_repeat(struct input_dev *dev) |
| 600 | { |
| 601 | unsigned long flags; |
| 602 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 603 | |
| 604 | spin_lock_irqsave(&ir->keylock, flags); |
| 605 | |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 606 | input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode); |
| 607 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 608 | if (!ir->keypressed) |
| 609 | goto out; |
| 610 | |
| 611 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 612 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 613 | |
| 614 | out: |
| 615 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 616 | } |
| 617 | EXPORT_SYMBOL_GPL(ir_repeat); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 618 | |
| 619 | /** |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 620 | * ir_do_keydown() - internal function to process a keypress |
| 621 | * @dev: the struct input_dev descriptor of the device |
| 622 | * @scancode: the scancode of the keypress |
| 623 | * @keycode: the keycode of the keypress |
| 624 | * @toggle: the toggle value of the keypress |
| 625 | * |
| 626 | * This function is used internally to register a keypress, it must be |
| 627 | * called with keylock held. |
| 628 | */ |
| 629 | static void ir_do_keydown(struct input_dev *dev, int scancode, |
| 630 | u32 keycode, u8 toggle) |
| 631 | { |
| 632 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 633 | |
| 634 | input_event(dev, EV_MSC, MSC_SCAN, scancode); |
| 635 | |
| 636 | /* Repeat event? */ |
| 637 | if (ir->keypressed && |
| 638 | ir->last_scancode == scancode && |
| 639 | ir->last_toggle == toggle) |
| 640 | return; |
| 641 | |
| 642 | /* Release old keypress */ |
| 643 | ir_do_keyup(ir); |
| 644 | |
| 645 | ir->last_scancode = scancode; |
| 646 | ir->last_toggle = toggle; |
| 647 | ir->last_keycode = keycode; |
| 648 | |
| 649 | if (keycode == KEY_RESERVED) |
| 650 | return; |
| 651 | |
| 652 | /* Register a keypress */ |
| 653 | ir->keypressed = true; |
| 654 | IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n", |
| 655 | dev->name, keycode, scancode); |
| 656 | input_report_key(dev, ir->last_keycode, 1); |
| 657 | input_sync(dev); |
| 658 | } |
| 659 | |
| 660 | /** |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 661 | * ir_keydown() - generates input event for a key press |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 662 | * @dev: the struct input_dev descriptor of the device |
| 663 | * @scancode: the scancode that we're seeking |
| 664 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 665 | * support toggle values, this should be set to zero) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 666 | * |
| 667 | * This routine is used by the input routines when a key is pressed at the |
| 668 | * IR. It gets the keycode for a scancode and reports an input event via |
| 669 | * input_report_key(). |
| 670 | */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 671 | void ir_keydown(struct input_dev *dev, int scancode, u8 toggle) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 672 | { |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 673 | unsigned long flags; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 674 | struct ir_input_dev *ir = input_get_drvdata(dev); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 675 | u32 keycode = ir_g_keycode_from_table(dev, scancode); |
| 676 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 677 | spin_lock_irqsave(&ir->keylock, flags); |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 678 | ir_do_keydown(dev, scancode, keycode, toggle); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 679 | |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 680 | if (ir->keypressed) { |
| 681 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 682 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 683 | } |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 684 | spin_unlock_irqrestore(&ir->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 685 | } |
| 686 | EXPORT_SYMBOL_GPL(ir_keydown); |
| 687 | |
David Härdeman | 62c6503 | 2010-10-29 16:08:07 -0300 | [diff] [blame] | 688 | /** |
| 689 | * ir_keydown_notimeout() - generates input event for a key press without |
| 690 | * an automatic keyup event at a later time |
| 691 | * @dev: the struct input_dev descriptor of the device |
| 692 | * @scancode: the scancode that we're seeking |
| 693 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 694 | * support toggle values, this should be set to zero) |
| 695 | * |
| 696 | * This routine is used by the input routines when a key is pressed at the |
| 697 | * IR. It gets the keycode for a scancode and reports an input event via |
| 698 | * input_report_key(). The driver must manually call ir_keyup() at a later |
| 699 | * stage. |
| 700 | */ |
| 701 | void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle) |
| 702 | { |
| 703 | unsigned long flags; |
| 704 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 705 | u32 keycode = ir_g_keycode_from_table(dev, scancode); |
| 706 | |
| 707 | spin_lock_irqsave(&ir->keylock, flags); |
| 708 | ir_do_keydown(dev, scancode, keycode, toggle); |
| 709 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 710 | } |
| 711 | EXPORT_SYMBOL_GPL(ir_keydown_notimeout); |
| 712 | |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 713 | static int ir_open(struct input_dev *input_dev) |
| 714 | { |
| 715 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 716 | |
| 717 | return ir_dev->props->open(ir_dev->props->priv); |
| 718 | } |
| 719 | |
| 720 | static void ir_close(struct input_dev *input_dev) |
| 721 | { |
| 722 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 723 | |
| 724 | ir_dev->props->close(ir_dev->props->priv); |
| 725 | } |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 726 | |
| 727 | /** |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 728 | * __ir_input_register() - sets the IR keycode table and add the handlers |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 729 | * for keymap table get/set |
| 730 | * @input_dev: the struct input_dev descriptor of the device |
| 731 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 732 | * |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 733 | * This routine is used to initialize the input infrastructure |
| 734 | * to work with an IR. |
| 735 | * It will register the input/evdev interface for the device and |
| 736 | * register the syfs code for IR class |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 737 | */ |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 738 | int __ir_input_register(struct input_dev *input_dev, |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 739 | const struct ir_scancode_table *rc_tab, |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 740 | struct ir_dev_props *props, |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 741 | const char *driver_name) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 742 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 743 | struct ir_input_dev *ir_dev; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 744 | int rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 745 | |
| 746 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 747 | return -EINVAL; |
| 748 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 749 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
| 750 | if (!ir_dev) |
| 751 | return -ENOMEM; |
| 752 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 753 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
| 754 | if (!ir_dev->driver_name) { |
| 755 | rc = -ENOMEM; |
| 756 | goto out_dev; |
Alexander Beregalov | 8231152 | 2010-01-09 13:51:14 -0300 | [diff] [blame] | 757 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 758 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 759 | input_dev->getkeycode_new = ir_getkeycode; |
| 760 | input_dev->setkeycode_new = ir_setkeycode; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 761 | input_set_drvdata(input_dev, ir_dev); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 762 | ir_dev->input_dev = input_dev; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 763 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 764 | spin_lock_init(&ir_dev->rc_tab.lock); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 765 | spin_lock_init(&ir_dev->keylock); |
| 766 | setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev); |
| 767 | |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 768 | if (props) { |
| 769 | ir_dev->props = props; |
| 770 | if (props->open) |
| 771 | input_dev->open = ir_open; |
| 772 | if (props->close) |
| 773 | input_dev->close = ir_close; |
| 774 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 775 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 776 | set_bit(EV_KEY, input_dev->evbit); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 777 | set_bit(EV_REP, input_dev->evbit); |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 778 | set_bit(EV_MSC, input_dev->evbit); |
| 779 | set_bit(MSC_SCAN, input_dev->mscbit); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 780 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 781 | rc = ir_setkeytable(ir_dev, rc_tab); |
| 782 | if (rc) |
| 783 | goto out_name; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 784 | |
Mauro Carvalho Chehab | 945cdfa | 2010-03-11 12:41:56 -0300 | [diff] [blame] | 785 | rc = ir_register_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 786 | if (rc < 0) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 787 | goto out_table; |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 788 | |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 789 | if (ir_dev->props) |
| 790 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) { |
| 791 | rc = ir_raw_event_register(input_dev); |
| 792 | if (rc < 0) |
| 793 | goto out_event; |
| 794 | } |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 795 | |
Maxim Levitsky | 58b3dd4 | 2010-09-06 18:26:07 -0300 | [diff] [blame] | 796 | rc = ir_register_input(input_dev); |
Mauro Carvalho Chehab | 991369e | 2010-10-14 17:49:33 -0300 | [diff] [blame] | 797 | if (rc < 0) |
| 798 | goto out_event; |
Maxim Levitsky | 58b3dd4 | 2010-09-06 18:26:07 -0300 | [diff] [blame] | 799 | |
Mauro Carvalho Chehab | 844a9e9 | 2010-08-01 17:19:29 -0300 | [diff] [blame] | 800 | IR_dprintk(1, "Registered input device on %s for %s remote%s.\n", |
| 801 | driver_name, rc_tab->name, |
Dan Carpenter | ede67a3 | 2010-08-06 03:31:00 -0300 | [diff] [blame] | 802 | (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ? |
| 803 | " in raw mode" : ""); |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 804 | |
Mauro Carvalho Chehab | 04cab131 | 2010-09-08 12:58:12 -0300 | [diff] [blame] | 805 | /* |
| 806 | * Default delay of 250ms is too short for some protocols, expecially |
| 807 | * since the timeout is currently set to 250ms. Increase it to 500ms, |
| 808 | * to avoid wrong repetition of the keycodes. |
| 809 | */ |
| 810 | input_dev->rep[REP_DELAY] = 500; |
| 811 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 812 | return 0; |
| 813 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 814 | out_event: |
| 815 | ir_unregister_class(input_dev); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 816 | out_table: |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 817 | ir_free_table(&ir_dev->rc_tab); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 818 | out_name: |
| 819 | kfree(ir_dev->driver_name); |
| 820 | out_dev: |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 821 | kfree(ir_dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 822 | return rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 823 | } |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 824 | EXPORT_SYMBOL_GPL(__ir_input_register); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 825 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 826 | /** |
| 827 | * ir_input_unregister() - unregisters IR and frees resources |
| 828 | * @input_dev: the struct input_dev descriptor of the device |
| 829 | |
| 830 | * This routine is used to free memory and de-register interfaces. |
| 831 | */ |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 832 | void ir_input_unregister(struct input_dev *input_dev) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 833 | { |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 834 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 835 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 836 | if (!ir_dev) |
Mauro Carvalho Chehab | 05395a3 | 2009-12-06 08:32:49 -0300 | [diff] [blame] | 837 | return; |
| 838 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 839 | IR_dprintk(1, "Freed keycode table\n"); |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 840 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 841 | del_timer_sync(&ir_dev->timer_keyup); |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 842 | if (ir_dev->props) |
| 843 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) |
| 844 | ir_raw_event_unregister(input_dev); |
| 845 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 846 | ir_free_table(&ir_dev->rc_tab); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 847 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 848 | ir_unregister_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 849 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 850 | kfree(ir_dev->driver_name); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 851 | kfree(ir_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 852 | } |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 853 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 854 | |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 855 | /* class for /sys/class/rc */ |
| 856 | static char *ir_devnode(struct device *dev, mode_t *mode) |
| 857 | { |
| 858 | return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev)); |
| 859 | } |
| 860 | |
| 861 | static struct class ir_input_class = { |
| 862 | .name = "rc", |
| 863 | .devnode = ir_devnode, |
| 864 | }; |
| 865 | |
| 866 | static struct { |
| 867 | u64 type; |
| 868 | char *name; |
| 869 | } proto_names[] = { |
| 870 | { IR_TYPE_UNKNOWN, "unknown" }, |
| 871 | { IR_TYPE_RC5, "rc-5" }, |
| 872 | { IR_TYPE_NEC, "nec" }, |
| 873 | { IR_TYPE_RC6, "rc-6" }, |
| 874 | { IR_TYPE_JVC, "jvc" }, |
| 875 | { IR_TYPE_SONY, "sony" }, |
| 876 | { IR_TYPE_RC5_SZ, "rc-5-sz" }, |
| 877 | { IR_TYPE_LIRC, "lirc" }, |
| 878 | }; |
| 879 | |
| 880 | #define PROTO_NONE "none" |
| 881 | |
| 882 | /** |
| 883 | * show_protocols() - shows the current IR protocol(s) |
| 884 | * @d: the device descriptor |
| 885 | * @mattr: the device attribute struct (unused) |
| 886 | * @buf: a pointer to the output buffer |
| 887 | * |
| 888 | * This routine is a callback routine for input read the IR protocol type(s). |
| 889 | * it is trigged by reading /sys/class/rc/rc?/protocols. |
| 890 | * It returns the protocol names of supported protocols. |
| 891 | * Enabled protocols are printed in brackets. |
| 892 | */ |
| 893 | static ssize_t show_protocols(struct device *d, |
| 894 | struct device_attribute *mattr, char *buf) |
| 895 | { |
| 896 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 897 | u64 allowed, enabled; |
| 898 | char *tmp = buf; |
| 899 | int i; |
| 900 | |
| 901 | /* Device is being removed */ |
| 902 | if (!ir_dev) |
| 903 | return -EINVAL; |
| 904 | |
| 905 | if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) { |
| 906 | enabled = ir_dev->rc_tab.ir_type; |
| 907 | allowed = ir_dev->props->allowed_protos; |
| 908 | } else if (ir_dev->raw) { |
| 909 | enabled = ir_dev->raw->enabled_protocols; |
| 910 | allowed = ir_raw_get_allowed_protocols(); |
| 911 | } else |
| 912 | return sprintf(tmp, "[builtin]\n"); |
| 913 | |
| 914 | IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n", |
| 915 | (long long)allowed, |
| 916 | (long long)enabled); |
| 917 | |
| 918 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
| 919 | if (allowed & enabled & proto_names[i].type) |
| 920 | tmp += sprintf(tmp, "[%s] ", proto_names[i].name); |
| 921 | else if (allowed & proto_names[i].type) |
| 922 | tmp += sprintf(tmp, "%s ", proto_names[i].name); |
| 923 | } |
| 924 | |
| 925 | if (tmp != buf) |
| 926 | tmp--; |
| 927 | *tmp = '\n'; |
| 928 | return tmp + 1 - buf; |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * store_protocols() - changes the current IR protocol(s) |
| 933 | * @d: the device descriptor |
| 934 | * @mattr: the device attribute struct (unused) |
| 935 | * @buf: a pointer to the input buffer |
| 936 | * @len: length of the input buffer |
| 937 | * |
| 938 | * This routine is a callback routine for changing the IR protocol type. |
| 939 | * It is trigged by writing to /sys/class/rc/rc?/protocols. |
| 940 | * Writing "+proto" will add a protocol to the list of enabled protocols. |
| 941 | * Writing "-proto" will remove a protocol from the list of enabled protocols. |
| 942 | * Writing "proto" will enable only "proto". |
| 943 | * Writing "none" will disable all protocols. |
| 944 | * Returns -EINVAL if an invalid protocol combination or unknown protocol name |
| 945 | * is used, otherwise @len. |
| 946 | */ |
| 947 | static ssize_t store_protocols(struct device *d, |
| 948 | struct device_attribute *mattr, |
| 949 | const char *data, |
| 950 | size_t len) |
| 951 | { |
| 952 | struct ir_input_dev *ir_dev = dev_get_drvdata(d); |
| 953 | bool enable, disable; |
| 954 | const char *tmp; |
| 955 | u64 type; |
| 956 | u64 mask; |
| 957 | int rc, i, count = 0; |
| 958 | unsigned long flags; |
| 959 | |
| 960 | /* Device is being removed */ |
| 961 | if (!ir_dev) |
| 962 | return -EINVAL; |
| 963 | |
| 964 | if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) |
| 965 | type = ir_dev->rc_tab.ir_type; |
| 966 | else if (ir_dev->raw) |
| 967 | type = ir_dev->raw->enabled_protocols; |
| 968 | else { |
| 969 | IR_dprintk(1, "Protocol switching not supported\n"); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
| 973 | while ((tmp = strsep((char **) &data, " \n")) != NULL) { |
| 974 | if (!*tmp) |
| 975 | break; |
| 976 | |
| 977 | if (*tmp == '+') { |
| 978 | enable = true; |
| 979 | disable = false; |
| 980 | tmp++; |
| 981 | } else if (*tmp == '-') { |
| 982 | enable = false; |
| 983 | disable = true; |
| 984 | tmp++; |
| 985 | } else { |
| 986 | enable = false; |
| 987 | disable = false; |
| 988 | } |
| 989 | |
| 990 | if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) { |
| 991 | tmp += sizeof(PROTO_NONE); |
| 992 | mask = 0; |
| 993 | count++; |
| 994 | } else { |
| 995 | for (i = 0; i < ARRAY_SIZE(proto_names); i++) { |
| 996 | if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) { |
| 997 | tmp += strlen(proto_names[i].name); |
| 998 | mask = proto_names[i].type; |
| 999 | break; |
| 1000 | } |
| 1001 | } |
| 1002 | if (i == ARRAY_SIZE(proto_names)) { |
| 1003 | IR_dprintk(1, "Unknown protocol: '%s'\n", tmp); |
| 1004 | return -EINVAL; |
| 1005 | } |
| 1006 | count++; |
| 1007 | } |
| 1008 | |
| 1009 | if (enable) |
| 1010 | type |= mask; |
| 1011 | else if (disable) |
| 1012 | type &= ~mask; |
| 1013 | else |
| 1014 | type = mask; |
| 1015 | } |
| 1016 | |
| 1017 | if (!count) { |
| 1018 | IR_dprintk(1, "Protocol not specified\n"); |
| 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | |
| 1022 | if (ir_dev->props && ir_dev->props->change_protocol) { |
| 1023 | rc = ir_dev->props->change_protocol(ir_dev->props->priv, |
| 1024 | type); |
| 1025 | if (rc < 0) { |
| 1026 | IR_dprintk(1, "Error setting protocols to 0x%llx\n", |
| 1027 | (long long)type); |
| 1028 | return -EINVAL; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) { |
| 1033 | spin_lock_irqsave(&ir_dev->rc_tab.lock, flags); |
| 1034 | ir_dev->rc_tab.ir_type = type; |
| 1035 | spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags); |
| 1036 | } else { |
| 1037 | ir_dev->raw->enabled_protocols = type; |
| 1038 | } |
| 1039 | |
| 1040 | IR_dprintk(1, "Current protocol(s): 0x%llx\n", |
| 1041 | (long long)type); |
| 1042 | |
| 1043 | return len; |
| 1044 | } |
| 1045 | |
| 1046 | #define ADD_HOTPLUG_VAR(fmt, val...) \ |
| 1047 | do { \ |
| 1048 | int err = add_uevent_var(env, fmt, val); \ |
| 1049 | if (err) \ |
| 1050 | return err; \ |
| 1051 | } while (0) |
| 1052 | |
| 1053 | static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env) |
| 1054 | { |
| 1055 | struct ir_input_dev *ir_dev = dev_get_drvdata(device); |
| 1056 | |
| 1057 | if (ir_dev->rc_tab.name) |
| 1058 | ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name); |
| 1059 | if (ir_dev->driver_name) |
| 1060 | ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name); |
| 1061 | |
| 1062 | return 0; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Static device attribute struct with the sysfs attributes for IR's |
| 1067 | */ |
| 1068 | static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR, |
| 1069 | show_protocols, store_protocols); |
| 1070 | |
| 1071 | static struct attribute *rc_dev_attrs[] = { |
| 1072 | &dev_attr_protocols.attr, |
| 1073 | NULL, |
| 1074 | }; |
| 1075 | |
| 1076 | static struct attribute_group rc_dev_attr_grp = { |
| 1077 | .attrs = rc_dev_attrs, |
| 1078 | }; |
| 1079 | |
| 1080 | static const struct attribute_group *rc_dev_attr_groups[] = { |
| 1081 | &rc_dev_attr_grp, |
| 1082 | NULL |
| 1083 | }; |
| 1084 | |
| 1085 | static struct device_type rc_dev_type = { |
| 1086 | .groups = rc_dev_attr_groups, |
| 1087 | .uevent = rc_dev_uevent, |
| 1088 | }; |
| 1089 | |
| 1090 | /** |
| 1091 | * ir_register_class() - creates the sysfs for /sys/class/rc/rc? |
| 1092 | * @input_dev: the struct input_dev descriptor of the device |
| 1093 | * |
| 1094 | * This routine is used to register the syfs code for IR class |
| 1095 | */ |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 1096 | static int ir_register_class(struct input_dev *input_dev) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1097 | { |
| 1098 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 1099 | int devno = find_first_zero_bit(&ir_core_dev_number, |
| 1100 | IRRCV_NUM_DEVICES); |
| 1101 | |
| 1102 | if (unlikely(devno < 0)) |
| 1103 | return devno; |
| 1104 | |
| 1105 | ir_dev->dev.type = &rc_dev_type; |
| 1106 | ir_dev->devno = devno; |
| 1107 | |
| 1108 | ir_dev->dev.class = &ir_input_class; |
| 1109 | ir_dev->dev.parent = input_dev->dev.parent; |
| 1110 | input_dev->dev.parent = &ir_dev->dev; |
| 1111 | dev_set_name(&ir_dev->dev, "rc%d", devno); |
| 1112 | dev_set_drvdata(&ir_dev->dev, ir_dev); |
| 1113 | return device_register(&ir_dev->dev); |
| 1114 | }; |
| 1115 | |
| 1116 | /** |
| 1117 | * ir_register_input - registers ir input device with input subsystem |
| 1118 | * @input_dev: the struct input_dev descriptor of the device |
| 1119 | */ |
| 1120 | |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 1121 | static int ir_register_input(struct input_dev *input_dev) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1122 | { |
| 1123 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 1124 | int rc; |
| 1125 | const char *path; |
| 1126 | |
| 1127 | |
| 1128 | rc = input_register_device(input_dev); |
| 1129 | if (rc < 0) { |
| 1130 | device_del(&ir_dev->dev); |
| 1131 | return rc; |
| 1132 | } |
| 1133 | |
| 1134 | __module_get(THIS_MODULE); |
| 1135 | |
| 1136 | path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL); |
| 1137 | printk(KERN_INFO "%s: %s as %s\n", |
| 1138 | dev_name(&ir_dev->dev), |
| 1139 | input_dev->name ? input_dev->name : "Unspecified device", |
| 1140 | path ? path : "N/A"); |
| 1141 | kfree(path); |
| 1142 | |
| 1143 | set_bit(ir_dev->devno, &ir_core_dev_number); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * ir_unregister_class() - removes the sysfs for sysfs for |
| 1149 | * /sys/class/rc/rc? |
| 1150 | * @input_dev: the struct input_dev descriptor of the device |
| 1151 | * |
| 1152 | * This routine is used to unregister the syfs code for IR class |
| 1153 | */ |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 1154 | static void ir_unregister_class(struct input_dev *input_dev) |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1155 | { |
| 1156 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 1157 | |
| 1158 | input_set_drvdata(input_dev, NULL); |
| 1159 | clear_bit(ir_dev->devno, &ir_core_dev_number); |
| 1160 | input_unregister_device(input_dev); |
| 1161 | device_del(&ir_dev->dev); |
| 1162 | |
| 1163 | module_put(THIS_MODULE); |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * Init/exit code for the module. Basically, creates/removes /sys/class/rc |
| 1168 | */ |
| 1169 | |
| 1170 | static int __init ir_core_init(void) |
| 1171 | { |
| 1172 | int rc = class_register(&ir_input_class); |
| 1173 | if (rc) { |
| 1174 | printk(KERN_ERR "ir_core: unable to register rc class\n"); |
| 1175 | return rc; |
| 1176 | } |
| 1177 | |
| 1178 | /* Initialize/load the decoders/keymap code that will be used */ |
| 1179 | ir_raw_init(); |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 1180 | ir_register_map(&empty_map); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static void __exit ir_core_exit(void) |
| 1186 | { |
| 1187 | class_unregister(&ir_input_class); |
David Härdeman | 4c7b355 | 2010-11-10 11:04:19 -0300 | [diff] [blame^] | 1188 | ir_unregister_map(&empty_map); |
Mauro Carvalho Chehab | bc2a6c5 | 2010-11-09 23:18:24 -0300 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | module_init(ir_core_init); |
| 1192 | module_exit(ir_core_exit); |
| 1193 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 1194 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
| 1195 | EXPORT_SYMBOL_GPL(ir_core_debug); |
| 1196 | module_param_named(debug, ir_core_debug, int, 0644); |
| 1197 | |
| 1198 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 1199 | MODULE_LICENSE("GPL"); |