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