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 | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 18 | #include <media/ir-common.h> |
| 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 | |
| 24 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 25 | * ir_resize_table() - resizes a scancode table if necessary |
| 26 | * @rc_tab: the ir_scancode_table to resize |
| 27 | * @return: zero on success or a negative error code |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 28 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 29 | * This routine will shrink the ir_scancode_table if it has lots of |
| 30 | * unused entries and grow it if it is full. |
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 | static int ir_resize_table(struct ir_scancode_table *rc_tab) |
| 33 | { |
| 34 | unsigned int oldalloc = rc_tab->alloc; |
| 35 | unsigned int newalloc = oldalloc; |
| 36 | struct ir_scancode *oldscan = rc_tab->scan; |
| 37 | struct ir_scancode *newscan; |
| 38 | |
| 39 | if (rc_tab->size == rc_tab->len) { |
| 40 | /* All entries in use -> grow keytable */ |
| 41 | if (rc_tab->alloc >= IR_TAB_MAX_SIZE) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | newalloc *= 2; |
| 45 | IR_dprintk(1, "Growing table to %u bytes\n", newalloc); |
| 46 | } |
| 47 | |
| 48 | if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) { |
| 49 | /* Less than 1/3 of entries in use -> shrink keytable */ |
| 50 | newalloc /= 2; |
| 51 | IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc); |
| 52 | } |
| 53 | |
| 54 | if (newalloc == oldalloc) |
| 55 | return 0; |
| 56 | |
| 57 | newscan = kmalloc(newalloc, GFP_ATOMIC); |
| 58 | if (!newscan) { |
| 59 | IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc); |
| 60 | return -ENOMEM; |
| 61 | } |
| 62 | |
| 63 | memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode)); |
| 64 | rc_tab->scan = newscan; |
| 65 | rc_tab->alloc = newalloc; |
| 66 | rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode); |
| 67 | kfree(oldscan); |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * ir_do_setkeycode() - internal function to set a keycode in the |
| 73 | * scancode->keycode table |
| 74 | * @dev: the struct input_dev device descriptor |
| 75 | * @rc_tab: the struct ir_scancode_table to set the keycode in |
| 76 | * @scancode: the scancode for the ir command |
| 77 | * @keycode: the keycode for the ir command |
| 78 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 79 | * |
| 80 | * This routine is used internally to manipulate the scancode->keycode table. |
| 81 | * The caller has to hold @rc_tab->lock. |
| 82 | */ |
| 83 | static int ir_do_setkeycode(struct input_dev *dev, |
| 84 | struct ir_scancode_table *rc_tab, |
| 85 | unsigned scancode, unsigned keycode) |
| 86 | { |
| 87 | unsigned int i; |
| 88 | int old_keycode = KEY_RESERVED; |
| 89 | |
| 90 | /* First check if we already have a mapping for this ir command */ |
| 91 | for (i = 0; i < rc_tab->len; i++) { |
| 92 | /* Keytable is sorted from lowest to highest scancode */ |
| 93 | if (rc_tab->scan[i].scancode > scancode) |
| 94 | break; |
| 95 | else if (rc_tab->scan[i].scancode < scancode) |
| 96 | continue; |
| 97 | |
| 98 | old_keycode = rc_tab->scan[i].keycode; |
| 99 | rc_tab->scan[i].keycode = keycode; |
| 100 | |
| 101 | /* Did the user wish to remove the mapping? */ |
| 102 | if (keycode == KEY_RESERVED || keycode == KEY_UNKNOWN) { |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 103 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", |
| 104 | i, scancode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 105 | rc_tab->len--; |
| 106 | memmove(&rc_tab->scan[i], &rc_tab->scan[i + 1], |
| 107 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
| 108 | } |
| 109 | |
| 110 | /* Possibly shrink the keytable, failure is not a problem */ |
| 111 | ir_resize_table(rc_tab); |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | if (old_keycode == KEY_RESERVED) { |
| 116 | /* No previous mapping found, we might need to grow the table */ |
| 117 | if (ir_resize_table(rc_tab)) |
| 118 | return -ENOMEM; |
| 119 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 120 | IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", |
| 121 | i, scancode, keycode); |
| 122 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 123 | /* i is the proper index to insert our new keycode */ |
| 124 | memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i], |
| 125 | (rc_tab->len - i) * sizeof(struct ir_scancode)); |
| 126 | rc_tab->scan[i].scancode = scancode; |
| 127 | rc_tab->scan[i].keycode = keycode; |
| 128 | rc_tab->len++; |
| 129 | set_bit(keycode, dev->keybit); |
| 130 | } else { |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 131 | IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n", |
| 132 | i, scancode, keycode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 133 | /* A previous mapping was updated... */ |
| 134 | clear_bit(old_keycode, dev->keybit); |
| 135 | /* ...but another scancode might use the same keycode */ |
| 136 | for (i = 0; i < rc_tab->len; i++) { |
| 137 | if (rc_tab->scan[i].keycode == old_keycode) { |
| 138 | set_bit(old_keycode, dev->keybit); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * ir_setkeycode() - set a keycode in the scancode->keycode table |
| 149 | * @dev: the struct input_dev device descriptor |
| 150 | * @scancode: the desired scancode |
| 151 | * @keycode: result |
| 152 | * @return: -EINVAL if the keycode could not be inserted, otherwise zero. |
| 153 | * |
| 154 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 155 | */ |
| 156 | static int ir_setkeycode(struct input_dev *dev, |
| 157 | unsigned int scancode, unsigned int keycode) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 158 | { |
| 159 | int rc; |
| 160 | unsigned long flags; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 161 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 162 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 163 | |
| 164 | spin_lock_irqsave(&rc_tab->lock, flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 165 | rc = ir_do_setkeycode(dev, rc_tab, scancode, keycode); |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 166 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 171 | * ir_setkeytable() - sets several entries in the scancode->keycode table |
| 172 | * @dev: the struct input_dev device descriptor |
| 173 | * @to: the struct ir_scancode_table to copy entries to |
| 174 | * @from: the struct ir_scancode_table to copy entries from |
| 175 | * @return: -EINVAL if all keycodes could not be inserted, otherwise zero. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 176 | * |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 177 | * This routine is used to handle table initialization. |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 178 | */ |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 179 | static int ir_setkeytable(struct input_dev *dev, |
| 180 | struct ir_scancode_table *to, |
| 181 | const struct ir_scancode_table *from) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 182 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 183 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 184 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
| 185 | unsigned long flags; |
| 186 | unsigned int i; |
| 187 | int rc = 0; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 188 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 189 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 190 | for (i = 0; i < from->size; i++) { |
| 191 | rc = ir_do_setkeycode(dev, to, from->scan[i].scancode, |
| 192 | from->scan[i].keycode); |
| 193 | if (rc) |
| 194 | break; |
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 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 197 | return rc; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 198 | } |
| 199 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 200 | /** |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 201 | * ir_getkeycode() - get a keycode from the scancode->keycode table |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 202 | * @dev: the struct input_dev device descriptor |
| 203 | * @scancode: the desired scancode |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 204 | * @keycode: used to return the keycode, if found, or KEY_RESERVED |
| 205 | * @return: always returns zero. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 206 | * |
| 207 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 208 | */ |
| 209 | static int ir_getkeycode(struct input_dev *dev, |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 210 | unsigned int scancode, unsigned int *keycode) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 211 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 212 | int start, end, mid; |
| 213 | unsigned long flags; |
| 214 | int key = KEY_RESERVED; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 215 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 216 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 217 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 218 | spin_lock_irqsave(&rc_tab->lock, flags); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 219 | start = 0; |
| 220 | end = rc_tab->len - 1; |
| 221 | while (start <= end) { |
| 222 | mid = (start + end) / 2; |
| 223 | if (rc_tab->scan[mid].scancode < scancode) |
| 224 | start = mid + 1; |
| 225 | else if (rc_tab->scan[mid].scancode > scancode) |
| 226 | end = mid - 1; |
| 227 | else { |
| 228 | key = rc_tab->scan[mid].keycode; |
| 229 | break; |
| 230 | } |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 231 | } |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 232 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 233 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 234 | if (key == KEY_RESERVED) |
| 235 | IR_dprintk(1, "unknown key for scancode 0x%04x\n", |
| 236 | scancode); |
| 237 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 238 | *keycode = key; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 239 | return 0; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * 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] | 244 | * @input_dev: the struct input_dev descriptor of the device |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 245 | * @scancode: the scancode that we're seeking |
| 246 | * |
| 247 | * This routine is used by the input routines when a key is pressed at the |
| 248 | * 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] | 249 | * 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] | 250 | * corresponding keycode from the table. |
| 251 | */ |
| 252 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 253 | { |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 254 | int keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 255 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 256 | ir_getkeycode(dev, scancode, &keycode); |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 257 | if (keycode != KEY_RESERVED) |
| 258 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 259 | dev->name, scancode, keycode); |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 260 | return keycode; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 261 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 263 | |
| 264 | /** |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 265 | * ir_keyup() - generates input event to cleanup a key press |
| 266 | * @input_dev: the struct input_dev descriptor of the device |
| 267 | * |
| 268 | * This routine is used by the input routines when a key is pressed at the |
| 269 | * IR. It reports a keyup input event via input_report_key(). |
| 270 | */ |
| 271 | void ir_keyup(struct input_dev *dev) |
| 272 | { |
| 273 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 274 | |
| 275 | if (!ir->keypressed) |
| 276 | return; |
| 277 | |
Mauro Carvalho Chehab | 9f15478 | 2010-03-21 13:00:55 -0300 | [diff] [blame] | 278 | IR_dprintk(1, "keyup key 0x%04x\n", ir->keycode); |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 279 | input_report_key(dev, ir->keycode, 0); |
| 280 | input_sync(dev); |
| 281 | ir->keypressed = 0; |
| 282 | } |
| 283 | EXPORT_SYMBOL_GPL(ir_keyup); |
| 284 | |
| 285 | /** |
| 286 | * ir_keydown() - generates input event for a key press |
| 287 | * @input_dev: the struct input_dev descriptor of the device |
| 288 | * @scancode: the scancode that we're seeking |
| 289 | * |
| 290 | * This routine is used by the input routines when a key is pressed at the |
| 291 | * IR. It gets the keycode for a scancode and reports an input event via |
| 292 | * input_report_key(). |
| 293 | */ |
| 294 | void ir_keydown(struct input_dev *dev, int scancode) |
| 295 | { |
| 296 | struct ir_input_dev *ir = input_get_drvdata(dev); |
| 297 | |
| 298 | u32 keycode = ir_g_keycode_from_table(dev, scancode); |
| 299 | |
| 300 | /* If already sent a keydown, do a keyup */ |
| 301 | if (ir->keypressed) |
| 302 | ir_keyup(dev); |
| 303 | |
| 304 | if (KEY_RESERVED == keycode) |
| 305 | return; |
| 306 | |
| 307 | ir->keycode = keycode; |
| 308 | ir->keypressed = 1; |
| 309 | |
| 310 | IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n", |
| 311 | dev->name, keycode, scancode); |
| 312 | |
| 313 | input_report_key(dev, ir->keycode, 1); |
| 314 | input_sync(dev); |
| 315 | |
| 316 | } |
| 317 | EXPORT_SYMBOL_GPL(ir_keydown); |
| 318 | |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 319 | static int ir_open(struct input_dev *input_dev) |
| 320 | { |
| 321 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 322 | |
| 323 | return ir_dev->props->open(ir_dev->props->priv); |
| 324 | } |
| 325 | |
| 326 | static void ir_close(struct input_dev *input_dev) |
| 327 | { |
| 328 | struct ir_input_dev *ir_dev = input_get_drvdata(input_dev); |
| 329 | |
| 330 | ir_dev->props->close(ir_dev->props->priv); |
| 331 | } |
Mauro Carvalho Chehab | 6660de5 | 2010-03-21 12:15:16 -0300 | [diff] [blame] | 332 | |
| 333 | /** |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 334 | * __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] | 335 | * for keymap table get/set |
| 336 | * @input_dev: the struct input_dev descriptor of the device |
| 337 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 338 | * |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 339 | * This routine is used to initialize the input infrastructure |
| 340 | * to work with an IR. |
| 341 | * It will register the input/evdev interface for the device and |
| 342 | * register the syfs code for IR class |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 343 | */ |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 344 | int __ir_input_register(struct input_dev *input_dev, |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 345 | const struct ir_scancode_table *rc_tab, |
Mauro Carvalho Chehab | 727e625 | 2010-03-12 21:18:14 -0300 | [diff] [blame] | 346 | const struct ir_dev_props *props, |
| 347 | const char *driver_name) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 348 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 349 | struct ir_input_dev *ir_dev; |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 350 | int rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 351 | |
| 352 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 353 | return -EINVAL; |
| 354 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 355 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
| 356 | if (!ir_dev) |
| 357 | return -ENOMEM; |
| 358 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 359 | ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name); |
| 360 | if (!ir_dev->driver_name) { |
| 361 | rc = -ENOMEM; |
| 362 | goto out_dev; |
Alexander Beregalov | 8231152 | 2010-01-09 13:51:14 -0300 | [diff] [blame] | 363 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 364 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 365 | input_dev->getkeycode = ir_getkeycode; |
| 366 | input_dev->setkeycode = ir_setkeycode; |
| 367 | input_set_drvdata(input_dev, ir_dev); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 368 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 369 | spin_lock_init(&ir_dev->rc_tab.lock); |
| 370 | ir_dev->rc_tab.name = rc_tab->name; |
| 371 | ir_dev->rc_tab.ir_type = rc_tab->ir_type; |
| 372 | ir_dev->rc_tab.alloc = roundup_pow_of_two(rc_tab->size * |
| 373 | sizeof(struct ir_scancode)); |
| 374 | ir_dev->rc_tab.scan = kmalloc(ir_dev->rc_tab.alloc, GFP_KERNEL); |
| 375 | ir_dev->rc_tab.size = ir_dev->rc_tab.alloc / sizeof(struct ir_scancode); |
| 376 | |
| 377 | if (!ir_dev->rc_tab.scan) { |
| 378 | rc = -ENOMEM; |
| 379 | goto out_name; |
| 380 | } |
| 381 | |
| 382 | IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n", |
| 383 | ir_dev->rc_tab.size, ir_dev->rc_tab.alloc); |
| 384 | |
| 385 | set_bit(EV_KEY, input_dev->evbit); |
| 386 | if (ir_setkeytable(input_dev, &ir_dev->rc_tab, rc_tab)) { |
| 387 | rc = -ENOMEM; |
| 388 | goto out_table; |
| 389 | } |
| 390 | |
Mauro Carvalho Chehab | e93854d | 2009-12-14 00:16:55 -0300 | [diff] [blame] | 391 | ir_dev->props = props; |
Mauro Carvalho Chehab | 716aab4 | 2010-03-31 14:40:35 -0300 | [diff] [blame] | 392 | if (props && props->open) |
| 393 | input_dev->open = ir_open; |
| 394 | if (props && props->close) |
| 395 | input_dev->close = ir_close; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 396 | |
Mauro Carvalho Chehab | 945cdfa | 2010-03-11 12:41:56 -0300 | [diff] [blame] | 397 | rc = ir_register_class(input_dev); |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 398 | if (rc < 0) |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 399 | goto out_table; |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 400 | |
Mauro Carvalho Chehab | 3543894 | 2010-04-03 16:53:16 -0300 | [diff] [blame^] | 401 | IR_dprintk(1, "Registered input device on %s for %s remote.\n", |
| 402 | driver_name, rc_tab->name); |
| 403 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 404 | return 0; |
| 405 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 406 | out_table: |
| 407 | kfree(ir_dev->rc_tab.scan); |
| 408 | out_name: |
| 409 | kfree(ir_dev->driver_name); |
| 410 | out_dev: |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 411 | kfree(ir_dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 412 | return rc; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 413 | } |
Mauro Carvalho Chehab | b2245ba | 2010-04-02 13:18:42 -0300 | [diff] [blame] | 414 | EXPORT_SYMBOL_GPL(__ir_input_register); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 415 | |
Mauro Carvalho Chehab | d4b778d | 2009-12-14 02:55:03 -0300 | [diff] [blame] | 416 | /** |
| 417 | * ir_input_unregister() - unregisters IR and frees resources |
| 418 | * @input_dev: the struct input_dev descriptor of the device |
| 419 | |
| 420 | * This routine is used to free memory and de-register interfaces. |
| 421 | */ |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 422 | void ir_input_unregister(struct input_dev *dev) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 423 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 424 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 425 | struct ir_scancode_table *rc_tab; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 426 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 427 | if (!ir_dev) |
Mauro Carvalho Chehab | 05395a3 | 2009-12-06 08:32:49 -0300 | [diff] [blame] | 428 | return; |
| 429 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 430 | IR_dprintk(1, "Freed keycode table\n"); |
| 431 | |
Mauro Carvalho Chehab | 579e7d6 | 2009-12-11 11:20:59 -0300 | [diff] [blame] | 432 | rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 433 | rc_tab->size = 0; |
| 434 | kfree(rc_tab->scan); |
| 435 | rc_tab->scan = NULL; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 436 | |
Mauro Carvalho Chehab | 4714eda | 2009-12-13 16:00:08 -0300 | [diff] [blame] | 437 | ir_unregister_class(dev); |
| 438 | |
David Härdeman | b3074c0 | 2010-04-02 15:58:28 -0300 | [diff] [blame] | 439 | kfree(ir_dev->driver_name); |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 440 | kfree(ir_dev); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 441 | } |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame] | 442 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 443 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 444 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
| 445 | EXPORT_SYMBOL_GPL(ir_core_debug); |
| 446 | module_param_named(debug, ir_core_debug, int, 0644); |
| 447 | |
| 448 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 449 | MODULE_LICENSE("GPL"); |