David Härdeman | dd3f616 | 2010-04-15 18:46:35 -0300 | [diff] [blame] | 1 | /* ir-keytable.c - handle IR scancode->keycode tables |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 2 | * |
| 3 | * Copyright (C) 2009 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 | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 15 | |
Mauro Carvalho Chehab | 882ead3 | 2009-12-29 10:37:38 -0300 | [diff] [blame] | 16 | #include <linux/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Mauro Carvalho Chehab | 3f113e3 | 2010-04-08 15:10:27 -0300 | [diff] [blame] | 18 | #include "ir-core-priv.h" |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 19 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 20 | /* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */ |
| 21 | #define IR_TAB_MIN_SIZE 256 |
| 22 | #define IR_TAB_MAX_SIZE 8192 |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 23 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 24 | /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */ |
| 25 | #define IR_KEYPRESS_TIMEOUT 250 |
| 26 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 27 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 28 | * ir_create_table() - initializes a scancode table |
| 29 | * @rc_tab: the ir_scancode_table to initialize |
| 30 | * @name: name to assign to the table |
| 31 | * @ir_type: ir type to assign to the new table |
| 32 | * @size: initial size of the table |
| 33 | * @return: zero on success or a negative error code |
| 34 | * |
| 35 | * This routine will initialize the ir_scancode_table and will allocate |
| 36 | * memory to hold at least the specified number elements. |
| 37 | */ |
| 38 | static int ir_create_table(struct ir_scancode_table *rc_tab, |
| 39 | const char *name, u64 ir_type, size_t size) |
| 40 | { |
| 41 | rc_tab->name = name; |
| 42 | rc_tab->ir_type = ir_type; |
| 43 | rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode)); |
| 44 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 45 | rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL); |
| 46 | if (!rc_tab->scan) |
| 47 | return -ENOMEM; |
| 48 | |
| 49 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 50 | rc_tab->size, rc_tab->alloc); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * ir_free_table() - frees memory allocated by a scancode table |
| 56 | * @rc_tab: the table whose mappings need to be freed |
| 57 | * |
| 58 | * This routine will free memory alloctaed for key mappings used by given |
| 59 | * scancode table. |
| 60 | */ |
| 61 | static void ir_free_table(struct ir_scancode_table *rc_tab) |
| 62 | { |
| 63 | rc_tab->size = 0; |
| 64 | kfree(rc_tab->scan); |
| 65 | rc_tab->scan = NULL; |
| 66 | } |
| 67 | |
| 68 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 69 | * ir_resize_table() - resizes a scancode table if necessary |
| 70 | * @rc_tab: the ir_scancode_table to resize |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 71 | * @gfp_flags: gfp flags to use when allocating memory |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 72 | * @return: zero on success or a negative error code |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 73 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 74 | * This routine will shrink the ir_scancode_table if it has lots of |
| 75 | * unused entries and grow it if it is full. |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 76 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 77 | 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] | 78 | { |
| 79 | unsigned int oldalloc = rc_tab->alloc; |
| 80 | unsigned int newalloc = oldalloc; |
| 81 | struct ir_scancode *oldscan = rc_tab->scan; |
| 82 | struct ir_scancode *newscan; |
| 83 | |
| 84 | if (rc_tab->size == rc_tab->len) { |
| 85 | /* All entries in use -> grow keytable */ |
| 86 | if (rc_tab->alloc >= IR_TAB_MAX_SIZE) |
| 87 | return -ENOMEM; |
| 88 | |
| 89 | newalloc *= 2; |
| 90 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); |
| 91 | } |
| 92 | |
| 93 | if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
| 94 | /* Less than 1/3 of entries in use -> shrink keytable */ |
| 95 | newalloc /= 2; |
| 96 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); |
| 97 | } |
| 98 | |
| 99 | if (newalloc == oldalloc) |
| 100 | return 0; |
| 101 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 102 | newscan = kmalloc(newalloc, gfp_flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 103 | if (!newscan) { |
| 104 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); |
| 105 | return -ENOMEM; |
| 106 | } |
| 107 | |
| 108 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
| 109 | rc_tab->scan = newscan; |
| 110 | rc_tab->alloc = newalloc; |
| 111 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 112 | kfree(oldscan); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 117 | * ir_update_mapping() - set a keycode in the scancode->keycode table |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 118 | * @dev: the struct input_dev device descriptor |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 119 | * @rc_tab: scancode table to be adjusted |
| 120 | * @index: index of the mapping that needs to be updated |
| 121 | * @keycode: the desired keycode |
| 122 | * @return: previous keycode assigned to the mapping |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 123 | * |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 124 | * This routine is used to update scancode->keycopde mapping at given |
| 125 | * position. |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 126 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 127 | static unsigned int ir_update_mapping(struct input_dev *dev, |
| 128 | struct ir_scancode_table *rc_tab, |
| 129 | unsigned int index, |
| 130 | unsigned int new_keycode) |
| 131 | { |
| 132 | int old_keycode = rc_tab->scan[index].keycode; |
| 133 | int i; |
| 134 | |
| 135 | /* Did the user wish to remove the mapping? */ |
| 136 | if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) { |
| 137 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", |
| 138 | index, rc_tab->scan[index].scancode); |
| 139 | rc_tab->len--; |
| 140 | memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1], |
| 141 | (rc_tab->len - index) * sizeof(struct ir_scancode)); |
| 142 | } else { |
| 143 | IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n", |
| 144 | index, |
| 145 | old_keycode == KEY_RESERVED ? "New" : "Replacing", |
| 146 | rc_tab->scan[index].scancode, new_keycode); |
| 147 | rc_tab->scan[index].keycode = new_keycode; |
| 148 | __set_bit(new_keycode, dev->keybit); |
| 149 | } |
| 150 | |
| 151 | if (old_keycode != KEY_RESERVED) { |
| 152 | /* A previous mapping was updated... */ |
| 153 | __clear_bit(old_keycode, dev->keybit); |
| 154 | /* ... but another scancode might use the same keycode */ |
| 155 | for (i = 0; i < rc_tab->len; i++) { |
| 156 | if (rc_tab->scan[i].keycode == old_keycode) { |
| 157 | __set_bit(old_keycode, dev->keybit); |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* Possibly shrink the keytable, failure is not a problem */ |
| 163 | ir_resize_table(rc_tab, GFP_ATOMIC); |
| 164 | } |
| 165 | |
| 166 | return old_keycode; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * ir_locate_scancode() - set a keycode in the scancode->keycode table |
| 171 | * @ir_dev: the struct ir_input_dev device descriptor |
| 172 | * @rc_tab: scancode table to be searched |
| 173 | * @scancode: the desired scancode |
| 174 | * @resize: controls whether we allowed to resize the table to |
| 175 | * accomodate not yet present scancodes |
| 176 | * @return: index of the mapping containing scancode in question |
| 177 | * or -1U in case of failure. |
| 178 | * |
| 179 | * This routine is used to locate given scancode in ir_scancode_table. |
| 180 | * If scancode is not yet present the routine will allocate a new slot |
| 181 | * for it. |
| 182 | */ |
| 183 | static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev, |
| 184 | struct ir_scancode_table *rc_tab, |
| 185 | unsigned int scancode, |
| 186 | bool resize) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 187 | { |
| 188 | unsigned int i; |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 189 | |
| 190 | /* |
| 191 | * Unfortunately, some hardware-based IR decoders don't provide |
| 192 | * all bits for the complete IR code. In general, they provide only |
| 193 | * the command part of the IR code. Yet, as it is possible to replace |
| 194 | * the provided IR with another one, it is needed to allow loading |
| 195 | * IR tables from other remotes. So, |
| 196 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 197 | if (ir_dev->props && ir_dev->props->scanmask) |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 198 | scancode &= ir_dev->props->scanmask; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 199 | |
| 200 | /* First check if we already have a mapping for this ir command */ |
| 201 | for (i = 0; i < rc_tab->len; i++) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 202 | if (rc_tab->scan[i].scancode == scancode) |
| 203 | return i; |
| 204 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 205 | /* Keytable is sorted from lowest to highest scancode */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 206 | if (rc_tab->scan[i].scancode >= scancode) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 207 | break; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 208 | } |
| 209 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 210 | /* No previous mapping found, we might need to grow the table */ |
| 211 | if (rc_tab->size == rc_tab->len) { |
| 212 | if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC)) |
| 213 | return -1U; |
| 214 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 215 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 216 | /* i is the proper index to insert our new keycode */ |
| 217 | if (i < rc_tab->len) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 218 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], |
| 219 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 220 | rc_tab->scan[i].scancode = scancode; |
| 221 | rc_tab->scan[i].keycode = KEY_RESERVED; |
| 222 | rc_tab->len++; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 223 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 224 | return i; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
| 228 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
| 229 | * @dev: the struct input_dev device descriptor |
| 230 | * @scancode: the desired scancode |
| 231 | * @keycode: result |
| 232 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 233 | * |
| 234 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 235 | */ |
| 236 | static int ir_setkeycode(struct input_dev *dev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 237 | const struct input_keymap_entry *ke, |
| 238 | unsigned int *old_keycode) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 239 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 240 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 241 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 242 | unsigned int index; |
| 243 | unsigned int scancode; |
| 244 | int retval; |
| 245 | unsigned long flags; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 246 | |
| 247 | spin_lock_irqsave(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 248 | |
| 249 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 250 | index = ke->index; |
| 251 | if (index >= rc_tab->len) { |
| 252 | retval = -EINVAL; |
| 253 | goto out; |
| 254 | } |
| 255 | } else { |
| 256 | retval = input_scancode_to_scalar(ke, &scancode); |
| 257 | if (retval) |
| 258 | goto out; |
| 259 | |
| 260 | index = ir_establish_scancode(ir_dev, rc_tab, scancode, true); |
| 261 | if (index >= rc_tab->len) { |
| 262 | retval = -ENOMEM; |
| 263 | goto out; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode); |
| 268 | |
| 269 | out: |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 270 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 271 | return retval; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 275 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
| 276 | * @dev: the struct input_dev device descriptor |
| 277 | * @to: the struct ir_scancode_table to copy entries to |
| 278 | * @from: the struct ir_scancode_table to copy entries from |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 279 | * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 280 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 281 | * This routine is used to handle table initialization. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 282 | */ |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 283 | static int ir_setkeytable(struct ir_input_dev *ir_dev, |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 284 | const struct ir_scancode_table *from) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 285 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 286 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 287 | unsigned int i, index; |
| 288 | int rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 289 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 290 | rc = ir_create_table(&ir_dev->rc_tab, |
| 291 | from->name, from->ir_type, from->size); |
| 292 | if (rc) |
| 293 | return rc; |
| 294 | |
| 295 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 296 | rc_tab->size, rc_tab->alloc); |
| 297 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 298 | for (i = 0; i < from->size; i++) { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 299 | index = ir_establish_scancode(ir_dev, rc_tab, |
| 300 | from->scan[i].scancode, false); |
| 301 | if (index >= rc_tab->len) { |
| 302 | rc = -ENOMEM; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 303 | break; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | ir_update_mapping(ir_dev->input_dev, rc_tab, index, |
| 307 | from->scan[i].keycode); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 308 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 309 | |
| 310 | if (rc) |
| 311 | ir_free_table(rc_tab); |
| 312 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 313 | return rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 314 | } |
| 315 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 316 | /** |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 317 | * ir_lookup_by_scancode() - locate mapping by scancode |
| 318 | * @rc_tab: the &struct ir_scancode_table to search |
| 319 | * @scancode: scancode to look for in the table |
| 320 | * @return: index in the table, -1U if not found |
| 321 | * |
| 322 | * This routine performs binary search in RC keykeymap table for |
| 323 | * given scancode. |
| 324 | */ |
| 325 | static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab, |
| 326 | unsigned int scancode) |
| 327 | { |
David Härdeman | 0d07025 | 2010-10-30 22:17:44 +0200 | [diff] [blame^] | 328 | int start = 0; |
| 329 | int end = rc_tab->len - 1; |
| 330 | int mid; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 331 | |
| 332 | while (start <= end) { |
| 333 | mid = (start + end) / 2; |
| 334 | if (rc_tab->scan[mid].scancode < scancode) |
| 335 | start = mid + 1; |
| 336 | else if (rc_tab->scan[mid].scancode > scancode) |
| 337 | end = mid - 1; |
| 338 | else |
| 339 | return mid; |
| 340 | } |
| 341 | |
| 342 | return -1U; |
| 343 | } |
| 344 | |
| 345 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 346 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 347 | * @dev: the struct input_dev device descriptor |
| 348 | * @scancode: the desired scancode |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 349 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
| 350 | * @return: always returns zero. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 351 | * |
| 352 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 353 | */ |
| 354 | static int ir_getkeycode(struct input_dev *dev, |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 355 | struct input_keymap_entry *ke) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 356 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 357 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 358 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 359 | struct ir_scancode *entry; |
| 360 | unsigned long flags; |
| 361 | unsigned int index; |
| 362 | unsigned int scancode; |
| 363 | int retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 364 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 365 | spin_lock_irqsave(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 366 | |
| 367 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 368 | index = ke->index; |
| 369 | } else { |
| 370 | retval = input_scancode_to_scalar(ke, &scancode); |
| 371 | if (retval) |
| 372 | goto out; |
| 373 | |
| 374 | index = ir_lookup_by_scancode(rc_tab, scancode); |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 375 | } |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 376 | |
| 377 | if (index >= rc_tab->len) { |
| 378 | if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) |
| 379 | IR_dprintk(1, "unknown key for scancode 0x%04x\n", |
| 380 | scancode); |
| 381 | retval = -EINVAL; |
| 382 | goto out; |
| 383 | } |
| 384 | |
| 385 | entry = &rc_tab->scan[index]; |
| 386 | |
| 387 | ke->index = index; |
| 388 | ke->keycode = entry->keycode; |
| 389 | ke->len = sizeof(entry->scancode); |
| 390 | memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode)); |
| 391 | |
| 392 | out: |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 393 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 394 | return retval; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | /** |
| 398 | * 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] | 399 | * @input_dev: the struct input_dev descriptor of the device |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 400 | * @scancode: the scancode that we're seeking |
| 401 | * |
| 402 | * This routine is used by the input routines when a key is pressed at the |
| 403 | * 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] | 404 | * 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] | 405 | * corresponding keycode from the table. |
| 406 | */ |
| 407 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 408 | { |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 409 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 410 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
| 411 | unsigned int keycode; |
| 412 | unsigned int index; |
| 413 | unsigned long flags; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 414 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 415 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 416 | |
| 417 | index = ir_lookup_by_scancode(rc_tab, scancode); |
| 418 | keycode = index < rc_tab->len ? |
| 419 | rc_tab->scan[index].keycode : KEY_RESERVED; |
| 420 | |
| 421 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 422 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 423 | if (keycode != KEY_RESERVED) |
| 424 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 425 | dev->name, scancode, keycode); |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 426 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 427 | return keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 428 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 429 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 430 | |
| 431 | /** |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 432 | * ir_keyup() - generates input event to cleanup a key press |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 433 | * @ir: the struct ir_input_dev descriptor of the device |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 434 | * |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 435 | * This routine is used to signal that a key has been released on the |
| 436 | * remote control. It reports a keyup input event via input_report_key(). |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 437 | */ |
Jarod Wilson | ee08940 | 2010-09-15 15:31:12 -0300 | [diff] [blame] | 438 | void ir_keyup(struct ir_input_dev *ir) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 439 | { |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 440 | if (!ir->keypressed) |
| 441 | return; |
| 442 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 443 | IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode); |
| 444 | input_report_key(ir->input_dev, ir->last_keycode, 0); |
| 445 | input_sync(ir->input_dev); |
| 446 | ir->keypressed = false; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 447 | } |
Jarod Wilson | ee08940 | 2010-09-15 15:31:12 -0300 | [diff] [blame] | 448 | EXPORT_SYMBOL_GPL(ir_keyup); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * ir_timer_keyup() - generates a keyup event after a timeout |
| 452 | * @cookie: a pointer to struct ir_input_dev passed to setup_timer() |
| 453 | * |
| 454 | * This routine will generate a keyup event some time after a keydown event |
| 455 | * is generated when no further activity has been detected. |
| 456 | */ |
| 457 | static void ir_timer_keyup(unsigned long cookie) |
| 458 | { |
| 459 | struct ir_input_dev *ir = (struct ir_input_dev *)cookie; |
| 460 | unsigned long flags; |
| 461 | |
| 462 | /* |
| 463 | * ir->keyup_jiffies is used to prevent a race condition if a |
| 464 | * hardware interrupt occurs at this point and the keyup timer |
| 465 | * event is moved further into the future as a result. |
| 466 | * |
| 467 | * The timer will then be reactivated and this function called |
| 468 | * again in the future. We need to exit gracefully in that case |
| 469 | * to allow the input subsystem to do its auto-repeat magic or |
| 470 | * a keyup event might follow immediately after the keydown. |
| 471 | */ |
| 472 | spin_lock_irqsave(&ir->keylock, flags); |
Maxim Levitsky | e0172fd | 2010-09-06 18:26:09 -0300 | [diff] [blame] | 473 | if (time_is_before_eq_jiffies(ir->keyup_jiffies)) |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 474 | ir_keyup(ir); |
| 475 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * ir_repeat() - notifies the IR core that a key is still pressed |
| 480 | * @dev: the struct input_dev descriptor of the device |
| 481 | * |
| 482 | * This routine is used by IR decoders when a repeat message which does |
| 483 | * not include the necessary bits to reproduce the scancode has been |
| 484 | * received. |
| 485 | */ |
| 486 | void ir_repeat(struct input_dev *dev) |
| 487 | { |
| 488 | unsigned long flags; |
| 489 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 490 | |
| 491 | spin_lock_irqsave(&ir->keylock, flags); |
| 492 | |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 493 | input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode); |
| 494 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 495 | if (!ir->keypressed) |
| 496 | goto out; |
| 497 | |
| 498 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 499 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 500 | |
| 501 | out: |
| 502 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 503 | } |
| 504 | EXPORT_SYMBOL_GPL(ir_repeat); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 505 | |
| 506 | /** |
| 507 | * ir_keydown() - generates input event for a key press |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 508 | * @dev: the struct input_dev descriptor of the device |
| 509 | * @scancode: the scancode that we're seeking |
| 510 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 511 | * support toggle values, this should be set to zero) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 512 | * |
| 513 | * This routine is used by the input routines when a key is pressed at the |
| 514 | * IR. It gets the keycode for a scancode and reports an input event via |
| 515 | * input_report_key(). |
| 516 | */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 517 | void ir_keydown(struct input_dev *dev, int scancode, u8 toggle) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 518 | { |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 519 | unsigned long flags; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 520 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 521 | |
| 522 | u32 keycode = ir_g_keycode_from_table(dev, scancode); |
| 523 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 524 | spin_lock_irqsave(&ir->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 525 | |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 526 | input_event(dev, EV_MSC, MSC_SCAN, scancode); |
| 527 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 528 | /* Repeat event? */ |
| 529 | if (ir->keypressed && |
| 530 | ir->last_scancode == scancode && |
| 531 | ir->last_toggle == toggle) |
| 532 | goto set_timer; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 533 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 534 | /* Release old keypress */ |
| 535 | ir_keyup(ir); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 536 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 537 | ir->last_scancode = scancode; |
| 538 | ir->last_toggle = toggle; |
| 539 | ir->last_keycode = keycode; |
| 540 | |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 541 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 542 | if (keycode == KEY_RESERVED) |
| 543 | goto out; |
| 544 | |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 545 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 546 | /* Register a keypress */ |
| 547 | ir->keypressed = true; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 548 | IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n", |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 549 | dev->name, keycode, scancode); |
| 550 | input_report_key(dev, ir->last_keycode, 1); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 551 | input_sync(dev); |
| 552 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 553 | set_timer: |
| 554 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 555 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 556 | out: |
| 557 | spin_unlock_irqrestore(&ir->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 558 | } |
| 559 | EXPORT_SYMBOL_GPL(ir_keydown); |
| 560 | |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 561 | static int ir_open(struct input_dev *input_dev) |
| 562 | { |
| 563 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 564 | |
| 565 | return ir_dev->props->open(ir_dev->props->priv); |
| 566 | } |
| 567 | |
| 568 | static void ir_close(struct input_dev *input_dev) |
| 569 | { |
| 570 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 571 | |
| 572 | ir_dev->props->close(ir_dev->props->priv); |
| 573 | } |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 574 | |
| 575 | /** |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 576 | * __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] | 577 | * for keymap table get/set |
| 578 | * @input_dev: the struct input_dev descriptor of the device |
| 579 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 580 | * |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 581 | * This routine is used to initialize the input infrastructure |
| 582 | * to work with an IR. |
| 583 | * It will register the input/evdev interface for the device and |
| 584 | * register the syfs code for IR class |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 585 | */ |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 586 | int __ir_input_register(struct input_dev *input_dev, |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 587 | const struct ir_scancode_table *rc_tab, |
Maxim Levitsky | 4a702eb | 2010-07-31 11:59:22 -0300 | [diff] [blame] | 588 | struct ir_dev_props *props, |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 589 | const char *driver_name) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 590 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 591 | struct ir_input_dev *ir_dev; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 592 | int rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 593 | |
| 594 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 595 | return -EINVAL; |
| 596 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 597 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
| 598 | if (!ir_dev) |
| 599 | return -ENOMEM; |
| 600 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 601 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
| 602 | if (!ir_dev->driver_name) { |
| 603 | rc = -ENOMEM; |
| 604 | goto out_dev; |
Alexander Beregalov | 8231152 | 2010-01-09 13:51:14 -0300 | [diff] [blame] | 605 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 606 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 607 | input_dev->getkeycode_new = ir_getkeycode; |
| 608 | input_dev->setkeycode_new = ir_setkeycode; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 609 | input_set_drvdata(input_dev, ir_dev); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 610 | ir_dev->input_dev = input_dev; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 611 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 612 | spin_lock_init(&ir_dev->rc_tab.lock); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 613 | spin_lock_init(&ir_dev->keylock); |
| 614 | setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev); |
| 615 | |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 616 | if (props) { |
| 617 | ir_dev->props = props; |
| 618 | if (props->open) |
| 619 | input_dev->open = ir_open; |
| 620 | if (props->close) |
| 621 | input_dev->close = ir_close; |
| 622 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 623 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 624 | set_bit(EV_KEY, input_dev->evbit); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 625 | set_bit(EV_REP, input_dev->evbit); |
Maxim Levitsky | ed4d387 | 2010-07-31 11:59:24 -0300 | [diff] [blame] | 626 | set_bit(EV_MSC, input_dev->evbit); |
| 627 | set_bit(MSC_SCAN, input_dev->mscbit); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 628 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 629 | rc = ir_setkeytable(ir_dev, rc_tab); |
| 630 | if (rc) |
| 631 | goto out_name; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 632 | |
Mauro Carvalho Chehab | 945cdfa | 2010-03-11 12:41:56 -0300 | [diff] [blame] | 633 | rc = ir_register_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 634 | if (rc < 0) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 635 | goto out_table; |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 636 | |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 637 | if (ir_dev->props) |
| 638 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) { |
| 639 | rc = ir_raw_event_register(input_dev); |
| 640 | if (rc < 0) |
| 641 | goto out_event; |
| 642 | } |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 643 | |
Maxim Levitsky | 58b3dd4 | 2010-09-06 18:26:07 -0300 | [diff] [blame] | 644 | rc = ir_register_input(input_dev); |
Mauro Carvalho Chehab | 991369e | 2010-10-14 17:49:33 -0300 | [diff] [blame] | 645 | if (rc < 0) |
| 646 | goto out_event; |
Maxim Levitsky | 58b3dd4 | 2010-09-06 18:26:07 -0300 | [diff] [blame] | 647 | |
Mauro Carvalho Chehab | 844a9e9 | 2010-08-01 17:19:29 -0300 | [diff] [blame] | 648 | IR_dprintk(1, "Registered input device on %s for %s remote%s.\n", |
| 649 | driver_name, rc_tab->name, |
Dan Carpenter | ede67a3 | 2010-08-06 03:31:00 -0300 | [diff] [blame] | 650 | (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ? |
| 651 | " in raw mode" : ""); |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 652 | |
Mauro Carvalho Chehab | 04cab131 | 2010-09-08 12:58:12 -0300 | [diff] [blame] | 653 | /* |
| 654 | * Default delay of 250ms is too short for some protocols, expecially |
| 655 | * since the timeout is currently set to 250ms. Increase it to 500ms, |
| 656 | * to avoid wrong repetition of the keycodes. |
| 657 | */ |
| 658 | input_dev->rep[REP_DELAY] = 500; |
| 659 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 660 | return 0; |
| 661 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 662 | out_event: |
| 663 | ir_unregister_class(input_dev); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 664 | out_table: |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 665 | ir_free_table(&ir_dev->rc_tab); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 666 | out_name: |
| 667 | kfree(ir_dev->driver_name); |
| 668 | out_dev: |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 669 | kfree(ir_dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 670 | return rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 671 | } |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 672 | EXPORT_SYMBOL_GPL(__ir_input_register); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 673 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 674 | /** |
| 675 | * ir_input_unregister() - unregisters IR and frees resources |
| 676 | * @input_dev: the struct input_dev descriptor of the device |
| 677 | |
| 678 | * This routine is used to free memory and de-register interfaces. |
| 679 | */ |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 680 | void ir_input_unregister(struct input_dev *input_dev) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 681 | { |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 682 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 683 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 684 | if (!ir_dev) |
Mauro Carvalho Chehab | 05395a3 | 2009-12-06 08:32:49 -0300 | [diff] [blame] | 685 | return; |
| 686 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 687 | IR_dprintk(1, "Freed keycode table\n"); |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 688 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 689 | del_timer_sync(&ir_dev->timer_keyup); |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 690 | if (ir_dev->props) |
| 691 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) |
| 692 | ir_raw_event_unregister(input_dev); |
| 693 | |
Dmitry Torokhov | 9f47009 | 2010-09-09 21:59:11 -0700 | [diff] [blame] | 694 | ir_free_table(&ir_dev->rc_tab); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 695 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 696 | ir_unregister_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 697 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 698 | kfree(ir_dev->driver_name); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 699 | kfree(ir_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 700 | } |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 701 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 702 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 703 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
| 704 | EXPORT_SYMBOL_GPL(ir_core_debug); |
| 705 | module_param_named(debug, ir_core_debug, int, 0644); |
| 706 | |
| 707 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 708 | MODULE_LICENSE("GPL"); |