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