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 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 28 | * ir_resize_table() - resizes a scancode table if necessary |
| 29 | * @rc_tab: the ir_scancode_table to resize |
| 30 | * @return: zero on success or a negative error code |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 31 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 32 | * This routine will shrink the ir_scancode_table if it has lots of |
| 33 | * unused entries and grow it if it is full. |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 34 | */ |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 35 | static int ir_resize_table(struct ir_scancode_table *rc_tab) |
| 36 | { |
| 37 | unsigned int oldalloc = rc_tab->alloc; |
| 38 | unsigned int newalloc = oldalloc; |
| 39 | struct ir_scancode *oldscan = rc_tab->scan; |
| 40 | struct ir_scancode *newscan; |
| 41 | |
| 42 | if (rc_tab->size == rc_tab->len) { |
| 43 | /* All entries in use -> grow keytable */ |
| 44 | if (rc_tab->alloc >= IR_TAB_MAX_SIZE) |
| 45 | return -ENOMEM; |
| 46 | |
| 47 | newalloc *= 2; |
| 48 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); |
| 49 | } |
| 50 | |
| 51 | if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
| 52 | /* Less than 1/3 of entries in use -> shrink keytable */ |
| 53 | newalloc /= 2; |
| 54 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); |
| 55 | } |
| 56 | |
| 57 | if (newalloc == oldalloc) |
| 58 | return 0; |
| 59 | |
| 60 | newscan = kmalloc(newalloc, GFP_ATOMIC); |
| 61 | if (!newscan) { |
| 62 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); |
| 63 | return -ENOMEM; |
| 64 | } |
| 65 | |
| 66 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
| 67 | rc_tab->scan = newscan; |
| 68 | rc_tab->alloc = newalloc; |
| 69 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 70 | kfree(oldscan); |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * ir_do_setkeycode() - internal function to set a keycode in the |
| 76 | * scancode->keycode table |
| 77 | * @dev: the struct input_dev device descriptor |
| 78 | * @rc_tab: the struct ir_scancode_table to set the keycode in |
| 79 | * @scancode: the scancode for the ir command |
| 80 | * @keycode: the keycode for the ir command |
Mauro Carvalho Chehab | 42880cd | 2010-05-18 02:06:06 -0300 | [diff] [blame] | 81 | * @resize: whether the keytable may be shrunk |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 82 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 83 | * |
| 84 | * This routine is used internally to manipulate the scancode->keycode table. |
| 85 | * The caller has to hold @rc_tab->lock. |
| 86 | */ |
| 87 | static int ir_do_setkeycode(struct input_dev *dev, |
| 88 | struct ir_scancode_table *rc_tab, |
Mauro Carvalho Chehab | 42880cd | 2010-05-18 02:06:06 -0300 | [diff] [blame] | 89 | unsigned scancode, unsigned keycode, |
| 90 | bool resize) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 91 | { |
| 92 | unsigned int i; |
| 93 | int old_keycode = KEY_RESERVED; |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 94 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 95 | |
| 96 | /* |
| 97 | * Unfortunately, some hardware-based IR decoders don't provide |
| 98 | * all bits for the complete IR code. In general, they provide only |
| 99 | * the command part of the IR code. Yet, as it is possible to replace |
| 100 | * the provided IR with another one, it is needed to allow loading |
| 101 | * IR tables from other remotes. So, |
| 102 | */ |
| 103 | if (ir_dev->props && ir_dev->props->scanmask) { |
| 104 | scancode &= ir_dev->props->scanmask; |
| 105 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 106 | |
| 107 | /* First check if we already have a mapping for this ir command */ |
| 108 | for (i = 0; i < rc_tab->len; i++) { |
| 109 | /* Keytable is sorted from lowest to highest scancode */ |
| 110 | if (rc_tab->scan[i].scancode > scancode) |
| 111 | break; |
| 112 | else if (rc_tab->scan[i].scancode < scancode) |
| 113 | continue; |
| 114 | |
| 115 | old_keycode = rc_tab->scan[i].keycode; |
| 116 | rc_tab->scan[i].keycode = keycode; |
| 117 | |
| 118 | /* Did the user wish to remove the mapping? */ |
| 119 | if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) { |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 120 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", |
| 121 | i, scancode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 122 | rc_tab->len--; |
| 123 | memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1], |
| 124 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
| 125 | } |
| 126 | |
| 127 | /* Possibly shrink the keytable, failure is not a problem */ |
| 128 | ir_resize_table(rc_tab); |
| 129 | break; |
| 130 | } |
| 131 | |
Mauro Carvalho Chehab | 09bd00e | 2010-04-11 00:26:23 -0300 | [diff] [blame] | 132 | if (old_keycode == KEY_RESERVED && keycode != KEY_RESERVED) { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 133 | /* No previous mapping found, we might need to grow the table */ |
Mauro Carvalho Chehab | 42880cd | 2010-05-18 02:06:06 -0300 | [diff] [blame] | 134 | if (resize && ir_resize_table(rc_tab)) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 135 | return -ENOMEM; |
| 136 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 137 | IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", |
| 138 | i, scancode, keycode); |
| 139 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 140 | /* i is the proper index to insert our new keycode */ |
| 141 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], |
| 142 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
| 143 | rc_tab->scan[i].scancode = scancode; |
| 144 | rc_tab->scan[i].keycode = keycode; |
| 145 | rc_tab->len++; |
| 146 | set_bit(keycode, dev->keybit); |
| 147 | } else { |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 148 | IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n", |
| 149 | i, scancode, keycode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 150 | /* A previous mapping was updated... */ |
| 151 | clear_bit(old_keycode, dev->keybit); |
| 152 | /* ...but another scancode might use the same keycode */ |
| 153 | for (i = 0; i < rc_tab->len; i++) { |
| 154 | if (rc_tab->scan[i].keycode == old_keycode) { |
| 155 | set_bit(old_keycode, dev->keybit); |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
| 166 | * @dev: the struct input_dev device descriptor |
| 167 | * @scancode: the desired scancode |
| 168 | * @keycode: result |
| 169 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 170 | * |
| 171 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 172 | */ |
| 173 | static int ir_setkeycode(struct input_dev *dev, |
| 174 | unsigned int scancode, unsigned int keycode) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 175 | { |
| 176 | int rc; |
| 177 | unsigned long flags; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 178 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 179 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 180 | |
| 181 | spin_lock_irqsave(&rc_tab->lock, flags); |
Mauro Carvalho Chehab | 42880cd | 2010-05-18 02:06:06 -0300 | [diff] [blame] | 182 | rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode, true); |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 183 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 188 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
| 189 | * @dev: the struct input_dev device descriptor |
| 190 | * @to: the struct ir_scancode_table to copy entries to |
| 191 | * @from: the struct ir_scancode_table to copy entries from |
| 192 | * @return: -EINVAL if all keycodes could not be inserted, otherwise zero. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 193 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 194 | * This routine is used to handle table initialization. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 195 | */ |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 196 | static int ir_setkeytable(struct input_dev *dev, |
| 197 | struct ir_scancode_table *to, |
| 198 | const struct ir_scancode_table *from) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 199 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 200 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 201 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
| 202 | unsigned long flags; |
| 203 | unsigned int i; |
| 204 | int rc = 0; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 205 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 206 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 207 | for (i = 0; i < from->size; i++) { |
| 208 | rc = ir_do_setkeycode(dev, to, from->scan[i].scancode, |
Mauro Carvalho Chehab | 42880cd | 2010-05-18 02:06:06 -0300 | [diff] [blame] | 209 | from->scan[i].keycode, false); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 210 | if (rc) |
| 211 | break; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 212 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 213 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 214 | return rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 215 | } |
| 216 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 217 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 218 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 219 | * @dev: the struct input_dev device descriptor |
| 220 | * @scancode: the desired scancode |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 221 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
| 222 | * @return: always returns zero. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 223 | * |
| 224 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 225 | */ |
| 226 | static int ir_getkeycode(struct input_dev *dev, |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 227 | unsigned int scancode, unsigned int *keycode) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 228 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 229 | int start, end, mid; |
| 230 | unsigned long flags; |
| 231 | int key = KEY_RESERVED; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 232 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 233 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 234 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 235 | spin_lock_irqsave(&rc_tab->lock, flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 236 | start = 0; |
| 237 | end = rc_tab->len - 1; |
| 238 | while (start <= end) { |
| 239 | mid = (start + end) / 2; |
| 240 | if (rc_tab->scan[mid].scancode < scancode) |
| 241 | start = mid + 1; |
| 242 | else if (rc_tab->scan[mid].scancode > scancode) |
| 243 | end = mid - 1; |
| 244 | else { |
| 245 | key = rc_tab->scan[mid].keycode; |
| 246 | break; |
| 247 | } |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 248 | } |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 249 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 250 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 251 | if (key == KEY_RESERVED) |
| 252 | IR_dprintk(1, "unknown key for scancode 0x%04x\n", |
| 253 | scancode); |
| 254 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 255 | *keycode = key; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 256 | return 0; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * 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] | 261 | * @input_dev: the struct input_dev descriptor of the device |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 262 | * @scancode: the scancode that we're seeking |
| 263 | * |
| 264 | * This routine is used by the input routines when a key is pressed at the |
| 265 | * 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] | 266 | * 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] | 267 | * corresponding keycode from the table. |
| 268 | */ |
| 269 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 270 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 271 | int keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 272 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 273 | ir_getkeycode(dev, scancode, &keycode); |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 274 | if (keycode != KEY_RESERVED) |
| 275 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 276 | dev->name, scancode, keycode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 277 | return keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 278 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 279 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 280 | |
| 281 | /** |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 282 | * ir_keyup() - generates input event to cleanup a key press |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 283 | * @ir: the struct ir_input_dev descriptor of the device |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 284 | * |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 285 | * This routine is used to signal that a key has been released on the |
| 286 | * 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] | 287 | */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 288 | static void ir_keyup(struct ir_input_dev *ir) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 289 | { |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 290 | if (!ir->keypressed) |
| 291 | return; |
| 292 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 293 | IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode); |
| 294 | input_report_key(ir->input_dev, ir->last_keycode, 0); |
| 295 | input_sync(ir->input_dev); |
| 296 | ir->keypressed = false; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 297 | } |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 298 | |
| 299 | /** |
| 300 | * ir_timer_keyup() - generates a keyup event after a timeout |
| 301 | * @cookie: a pointer to struct ir_input_dev passed to setup_timer() |
| 302 | * |
| 303 | * This routine will generate a keyup event some time after a keydown event |
| 304 | * is generated when no further activity has been detected. |
| 305 | */ |
| 306 | static void ir_timer_keyup(unsigned long cookie) |
| 307 | { |
| 308 | struct ir_input_dev *ir = (struct ir_input_dev *)cookie; |
| 309 | unsigned long flags; |
| 310 | |
| 311 | /* |
| 312 | * ir->keyup_jiffies is used to prevent a race condition if a |
| 313 | * hardware interrupt occurs at this point and the keyup timer |
| 314 | * event is moved further into the future as a result. |
| 315 | * |
| 316 | * The timer will then be reactivated and this function called |
| 317 | * again in the future. We need to exit gracefully in that case |
| 318 | * to allow the input subsystem to do its auto-repeat magic or |
| 319 | * a keyup event might follow immediately after the keydown. |
| 320 | */ |
| 321 | spin_lock_irqsave(&ir->keylock, flags); |
| 322 | if (time_is_after_eq_jiffies(ir->keyup_jiffies)) |
| 323 | ir_keyup(ir); |
| 324 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * ir_repeat() - notifies the IR core that a key is still pressed |
| 329 | * @dev: the struct input_dev descriptor of the device |
| 330 | * |
| 331 | * This routine is used by IR decoders when a repeat message which does |
| 332 | * not include the necessary bits to reproduce the scancode has been |
| 333 | * received. |
| 334 | */ |
| 335 | void ir_repeat(struct input_dev *dev) |
| 336 | { |
| 337 | unsigned long flags; |
| 338 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 339 | |
| 340 | spin_lock_irqsave(&ir->keylock, flags); |
| 341 | |
| 342 | if (!ir->keypressed) |
| 343 | goto out; |
| 344 | |
| 345 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 346 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 347 | |
| 348 | out: |
| 349 | spin_unlock_irqrestore(&ir->keylock, flags); |
| 350 | } |
| 351 | EXPORT_SYMBOL_GPL(ir_repeat); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * ir_keydown() - generates input event for a key press |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 355 | * @dev: the struct input_dev descriptor of the device |
| 356 | * @scancode: the scancode that we're seeking |
| 357 | * @toggle: the toggle value (protocol dependent, if the protocol doesn't |
| 358 | * support toggle values, this should be set to zero) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 359 | * |
| 360 | * This routine is used by the input routines when a key is pressed at the |
| 361 | * IR. It gets the keycode for a scancode and reports an input event via |
| 362 | * input_report_key(). |
| 363 | */ |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 364 | void ir_keydown(struct input_dev *dev, int scancode, u8 toggle) |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 365 | { |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 366 | unsigned long flags; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 367 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 368 | |
| 369 | u32 keycode = ir_g_keycode_from_table(dev, scancode); |
| 370 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 371 | spin_lock_irqsave(&ir->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 372 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 373 | /* Repeat event? */ |
| 374 | if (ir->keypressed && |
| 375 | ir->last_scancode == scancode && |
| 376 | ir->last_toggle == toggle) |
| 377 | goto set_timer; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 378 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 379 | /* Release old keypress */ |
| 380 | ir_keyup(ir); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 381 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 382 | ir->last_scancode = scancode; |
| 383 | ir->last_toggle = toggle; |
| 384 | ir->last_keycode = keycode; |
| 385 | |
| 386 | if (keycode == KEY_RESERVED) |
| 387 | goto out; |
| 388 | |
| 389 | /* Register a keypress */ |
| 390 | ir->keypressed = true; |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 391 | 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] | 392 | dev->name, keycode, scancode); |
| 393 | input_report_key(dev, ir->last_keycode, 1); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 394 | input_sync(dev); |
| 395 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 396 | set_timer: |
| 397 | ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT); |
| 398 | mod_timer(&ir->timer_keyup, ir->keyup_jiffies); |
| 399 | out: |
| 400 | spin_unlock_irqrestore(&ir->keylock, flags); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 401 | } |
| 402 | EXPORT_SYMBOL_GPL(ir_keydown); |
| 403 | |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 404 | static int ir_open(struct input_dev *input_dev) |
| 405 | { |
| 406 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 407 | |
| 408 | return ir_dev->props->open(ir_dev->props->priv); |
| 409 | } |
| 410 | |
| 411 | static void ir_close(struct input_dev *input_dev) |
| 412 | { |
| 413 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 414 | |
| 415 | ir_dev->props->close(ir_dev->props->priv); |
| 416 | } |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 417 | |
| 418 | /** |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 419 | * __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] | 420 | * for keymap table get/set |
| 421 | * @input_dev: the struct input_dev descriptor of the device |
| 422 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 423 | * |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 424 | * This routine is used to initialize the input infrastructure |
| 425 | * to work with an IR. |
| 426 | * It will register the input/evdev interface for the device and |
| 427 | * register the syfs code for IR class |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 428 | */ |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 429 | int __ir_input_register(struct input_dev *input_dev, |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 430 | const struct ir_scancode_table *rc_tab, |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 431 | const struct ir_dev_props *props, |
| 432 | const char *driver_name) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 433 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 434 | struct ir_input_dev *ir_dev; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 435 | int rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 436 | |
| 437 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 438 | return -EINVAL; |
| 439 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 440 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
| 441 | if (!ir_dev) |
| 442 | return -ENOMEM; |
| 443 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 444 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
| 445 | if (!ir_dev->driver_name) { |
| 446 | rc = -ENOMEM; |
| 447 | goto out_dev; |
Alexander Beregalov | 8231152 | 2010-01-09 13:51:14 -0300 | [diff] [blame] | 448 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 449 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 450 | input_dev->getkeycode = ir_getkeycode; |
| 451 | input_dev->setkeycode = ir_setkeycode; |
| 452 | input_set_drvdata(input_dev, ir_dev); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 453 | ir_dev->input_dev = input_dev; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 454 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 455 | spin_lock_init(&ir_dev->rc_tab.lock); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 456 | spin_lock_init(&ir_dev->keylock); |
| 457 | setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev); |
| 458 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 459 | ir_dev->rc_tab.name = rc_tab->name; |
| 460 | ir_dev->rc_tab.ir_type = rc_tab->ir_type; |
| 461 | ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size * |
| 462 | sizeof(struct ir_scancode)); |
| 463 | ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL); |
| 464 | ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode); |
Mauro Carvalho Chehab | 9dfe4e8 | 2010-04-04 14:06:55 -0300 | [diff] [blame] | 465 | if (props) { |
| 466 | ir_dev->props = props; |
| 467 | if (props->open) |
| 468 | input_dev->open = ir_open; |
| 469 | if (props->close) |
| 470 | input_dev->close = ir_close; |
| 471 | } |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 472 | |
| 473 | if (!ir_dev->rc_tab.scan) { |
| 474 | rc = -ENOMEM; |
| 475 | goto out_name; |
| 476 | } |
| 477 | |
| 478 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 479 | ir_dev->rc_tab.size, ir_dev->rc_tab.alloc); |
| 480 | |
| 481 | set_bit(EV_KEY, input_dev->evbit); |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 482 | set_bit(EV_REP, input_dev->evbit); |
| 483 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 484 | if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) { |
| 485 | rc = -ENOMEM; |
| 486 | goto out_table; |
| 487 | } |
| 488 | |
Mauro Carvalho Chehab | 945cdfa | 2010-03-11 12:41:56 -0300 | [diff] [blame] | 489 | rc = ir_register_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 490 | if (rc < 0) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 491 | goto out_table; |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 492 | |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 493 | if (ir_dev->props) |
| 494 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) { |
| 495 | rc = ir_raw_event_register(input_dev); |
| 496 | if (rc < 0) |
| 497 | goto out_event; |
| 498 | } |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 499 | |
Mauro Carvalho Chehab | 844a9e9 | 2010-08-01 17:19:29 -0300 | [diff] [blame] | 500 | IR_dprintk(1, "Registered input device on %s for %s remote%s.\n", |
| 501 | driver_name, rc_tab->name, |
| 502 | ir_dev->props->driver_type == RC_DRIVER_IR_RAW ? " in raw mode" : ""); |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame] | 503 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 504 | return 0; |
| 505 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 506 | out_event: |
| 507 | ir_unregister_class(input_dev); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 508 | out_table: |
| 509 | kfree(ir_dev->rc_tab.scan); |
| 510 | out_name: |
| 511 | kfree(ir_dev->driver_name); |
| 512 | out_dev: |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 513 | kfree(ir_dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 514 | return rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 515 | } |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 516 | EXPORT_SYMBOL_GPL(__ir_input_register); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 517 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 518 | /** |
| 519 | * ir_input_unregister() - unregisters IR and frees resources |
| 520 | * @input_dev: the struct input_dev descriptor of the device |
| 521 | |
| 522 | * This routine is used to free memory and de-register interfaces. |
| 523 | */ |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 524 | void ir_input_unregister(struct input_dev *input_dev) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 525 | { |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 526 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 527 | struct ir_scancode_table *rc_tab; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 528 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 529 | if (!ir_dev) |
Mauro Carvalho Chehab | 05395a3 | 2009-12-06 08:32:49 -0300 | [diff] [blame] | 530 | return; |
| 531 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 532 | IR_dprintk(1, "Freed keycode table\n"); |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 533 | |
David Härdeman | a374fef | 2010-04-02 15:58:29 -0300 | [diff] [blame] | 534 | del_timer_sync(&ir_dev->timer_keyup); |
Igor M. Liplianin | 84b14f1 | 2010-05-26 23:31:21 -0300 | [diff] [blame] | 535 | if (ir_dev->props) |
| 536 | if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) |
| 537 | ir_raw_event_unregister(input_dev); |
| 538 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 539 | rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 540 | rc_tab->size = 0; |
| 541 | kfree(rc_tab->scan); |
| 542 | rc_tab->scan = NULL; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 543 | |
Mauro Carvalho Chehab | 626cf69 | 2010-04-06 23:21:46 -0300 | [diff] [blame] | 544 | ir_unregister_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 545 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 546 | kfree(ir_dev->driver_name); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 547 | kfree(ir_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 548 | } |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 549 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 550 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 551 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
| 552 | EXPORT_SYMBOL_GPL(ir_core_debug); |
| 553 | module_param_named(debug, ir_core_debug, int, 0644); |
| 554 | |
| 555 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 556 | MODULE_LICENSE("GPL"); |