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 | |
| 15 | #include <linux/usb/input.h> |
| 16 | |
| 17 | #include <media/ir-common.h> |
| 18 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 19 | #define IR_TAB_MIN_SIZE 32 |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 20 | #define IR_TAB_MAX_SIZE 1024 |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 21 | |
| 22 | /** |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 23 | * ir_seek_table() - returns the element order on the table |
| 24 | * @rc_tab: the ir_scancode_table with the keymap to be used |
| 25 | * @scancode: the scancode that we're seeking |
| 26 | * |
| 27 | * This routine is used by the input routines when a key is pressed at the |
| 28 | * IR. The scancode is received and needs to be converted into a keycode. |
| 29 | * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the |
| 30 | * corresponding keycode from the table. |
| 31 | */ |
| 32 | static int ir_seek_table(struct ir_scancode_table *rc_tab, u32 scancode) |
| 33 | { |
| 34 | int rc; |
| 35 | unsigned long flags; |
| 36 | struct ir_scancode *keymap = rc_tab->scan; |
| 37 | |
| 38 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 39 | |
| 40 | /* FIXME: replace it by a binary search */ |
| 41 | |
| 42 | for (rc = 0; rc < rc_tab->size; rc++) |
| 43 | if (keymap[rc].scancode == scancode) |
| 44 | goto exit; |
| 45 | |
| 46 | /* Not found */ |
| 47 | rc = -EINVAL; |
| 48 | |
| 49 | exit: |
| 50 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 51 | return rc; |
| 52 | } |
| 53 | |
| 54 | /** |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 55 | * ir_roundup_tablesize() - gets an optimum value for the table size |
| 56 | * @n_elems: minimum number of entries to store keycodes |
| 57 | * |
| 58 | * This routine is used to choose the keycode table size. |
| 59 | * |
| 60 | * In order to have some empty space for new keycodes, |
| 61 | * and knowing in advance that kmalloc allocates only power of two |
| 62 | * segments, it optimizes the allocated space to have some spare space |
| 63 | * for those new keycodes by using the maximum number of entries that |
| 64 | * will be effectively be allocated by kmalloc. |
| 65 | * In order to reduce the quantity of table resizes, it has a minimum |
| 66 | * table size of IR_TAB_MIN_SIZE. |
| 67 | */ |
| 68 | int ir_roundup_tablesize(int n_elems) |
| 69 | { |
| 70 | size_t size; |
| 71 | |
| 72 | if (n_elems < IR_TAB_MIN_SIZE) |
| 73 | n_elems = IR_TAB_MIN_SIZE; |
| 74 | |
| 75 | /* |
| 76 | * As kmalloc only allocates sizes of power of two, get as |
| 77 | * much entries as possible for the allocated memory segment |
| 78 | */ |
| 79 | size = roundup_pow_of_two(n_elems * sizeof(struct ir_scancode)); |
| 80 | n_elems = size / sizeof(struct ir_scancode); |
| 81 | |
| 82 | return n_elems; |
| 83 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 84 | EXPORT_SYMBOL_GPL(ir_roundup_tablesize); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * ir_copy_table() - copies a keytable, discarding the unused entries |
| 88 | * @destin: destin table |
| 89 | * @origin: origin table |
| 90 | * |
| 91 | * Copies all entries where the keycode is not KEY_UNKNOWN/KEY_RESERVED |
| 92 | */ |
| 93 | |
| 94 | int ir_copy_table(struct ir_scancode_table *destin, |
| 95 | const struct ir_scancode_table *origin) |
| 96 | { |
| 97 | int i, j = 0; |
| 98 | |
| 99 | for (i = 0; i < origin->size; i++) { |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 100 | if (origin->scan[i].keycode == KEY_UNKNOWN || |
| 101 | origin->scan[i].keycode == KEY_RESERVED) |
| 102 | continue; |
| 103 | |
| 104 | memcpy(&destin->scan[j], &origin->scan[i], sizeof(struct ir_scancode)); |
| 105 | j++; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 106 | } |
| 107 | destin->size = j; |
| 108 | |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 109 | IR_dprintk(1, "Copied %d scancodes to the new keycode table\n", destin->size); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 113 | EXPORT_SYMBOL_GPL(ir_copy_table); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 114 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 115 | /** |
| 116 | * ir_getkeycode() - get a keycode at the evdev scancode ->keycode table |
| 117 | * @dev: the struct input_dev device descriptor |
| 118 | * @scancode: the desired scancode |
| 119 | * @keycode: the keycode to be retorned. |
| 120 | * |
| 121 | * This routine is used to handle evdev EVIOCGKEY ioctl. |
| 122 | * If the key is not found, returns -EINVAL, otherwise, returns 0. |
| 123 | */ |
| 124 | static int ir_getkeycode(struct input_dev *dev, |
| 125 | int scancode, int *keycode) |
| 126 | { |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 127 | int elem; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 128 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 129 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 130 | |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 131 | elem = ir_seek_table(rc_tab, scancode); |
| 132 | if (elem >= 0) { |
| 133 | *keycode = rc_tab->scan[elem].keycode; |
| 134 | return 0; |
| 135 | } |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 136 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 137 | /* |
| 138 | * Scancode not found and table can't be expanded |
| 139 | */ |
| 140 | if (elem < 0 && rc_tab->size == IR_TAB_MAX_SIZE) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | /* |
| 144 | * If is there extra space, returns KEY_RESERVED, |
| 145 | * otherwise, input core won't let ir_setkeycode to work |
| 146 | */ |
| 147 | *keycode = KEY_RESERVED; |
| 148 | return 0; |
| 149 | } |
| 150 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 151 | /** |
| 152 | * ir_is_resize_needed() - Check if the table needs rezise |
| 153 | * @table: keycode table that may need to resize |
| 154 | * @n_elems: minimum number of entries to store keycodes |
| 155 | * |
| 156 | * Considering that kmalloc uses power of two storage areas, this |
| 157 | * routine detects if the real alloced size will change. If not, it |
| 158 | * just returns without doing nothing. Otherwise, it will extend or |
| 159 | * reduce the table size to meet the new needs. |
| 160 | * |
| 161 | * It returns 0 if no resize is needed, 1 otherwise. |
| 162 | */ |
| 163 | static int ir_is_resize_needed(struct ir_scancode_table *table, int n_elems) |
| 164 | { |
| 165 | int cur_size = ir_roundup_tablesize(table->size); |
| 166 | int new_size = ir_roundup_tablesize(n_elems); |
| 167 | |
| 168 | if (cur_size == new_size) |
| 169 | return 0; |
| 170 | |
| 171 | /* Resize is needed */ |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * ir_delete_key() - remove a keycode from the table |
| 177 | * @rc_tab: keycode table |
| 178 | * @elem: element to be removed |
| 179 | * |
| 180 | */ |
| 181 | static void ir_delete_key(struct ir_scancode_table *rc_tab, int elem) |
| 182 | { |
| 183 | unsigned long flags = 0; |
| 184 | int newsize = rc_tab->size - 1; |
| 185 | int resize = ir_is_resize_needed(rc_tab, newsize); |
| 186 | struct ir_scancode *oldkeymap = rc_tab->scan; |
| 187 | struct ir_scancode *newkeymap; |
| 188 | |
| 189 | if (resize) { |
| 190 | newkeymap = kzalloc(ir_roundup_tablesize(newsize) * |
| 191 | sizeof(*newkeymap), GFP_ATOMIC); |
| 192 | |
| 193 | /* There's no memory for resize. Keep the old table */ |
| 194 | if (!newkeymap) |
| 195 | resize = 0; |
| 196 | } |
| 197 | |
| 198 | if (!resize) { |
| 199 | newkeymap = oldkeymap; |
| 200 | |
| 201 | /* We'll modify the live table. Lock it */ |
| 202 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Copy the elements before the one that will be deleted |
| 207 | * if (!resize), both oldkeymap and newkeymap points |
| 208 | * to the same place, so, there's no need to copy |
| 209 | */ |
| 210 | if (resize && elem > 0) |
| 211 | memcpy(newkeymap, oldkeymap, |
| 212 | elem * sizeof(*newkeymap)); |
| 213 | |
| 214 | /* |
| 215 | * Copy the other elements overwriting the element to be removed |
| 216 | * This operation applies to both resize and non-resize case |
| 217 | */ |
| 218 | if (elem < newsize) |
| 219 | memcpy(&newkeymap[elem], &oldkeymap[elem + 1], |
| 220 | (newsize - elem) * sizeof(*newkeymap)); |
| 221 | |
| 222 | if (resize) { |
| 223 | /* |
| 224 | * As the copy happened to a temporary table, only here |
| 225 | * it needs to lock while replacing the table pointers |
| 226 | * to use the new table |
| 227 | */ |
| 228 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 229 | rc_tab->size = newsize; |
| 230 | rc_tab->scan = newkeymap; |
| 231 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 232 | |
| 233 | /* Frees the old keytable */ |
| 234 | kfree(oldkeymap); |
| 235 | } else { |
| 236 | rc_tab->size = newsize; |
| 237 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * ir_insert_key() - insert a keycode at the table |
| 243 | * @rc_tab: keycode table |
| 244 | * @scancode: the desired scancode |
| 245 | * @keycode: the keycode to be retorned. |
| 246 | * |
| 247 | */ |
| 248 | static int ir_insert_key(struct ir_scancode_table *rc_tab, |
| 249 | int scancode, int keycode) |
| 250 | { |
| 251 | unsigned long flags; |
| 252 | int elem = rc_tab->size; |
| 253 | int newsize = rc_tab->size + 1; |
| 254 | int resize = ir_is_resize_needed(rc_tab, newsize); |
| 255 | struct ir_scancode *oldkeymap = rc_tab->scan; |
| 256 | struct ir_scancode *newkeymap; |
| 257 | |
| 258 | if (resize) { |
| 259 | newkeymap = kzalloc(ir_roundup_tablesize(newsize) * |
| 260 | sizeof(*newkeymap), GFP_ATOMIC); |
| 261 | if (!newkeymap) |
| 262 | return -ENOMEM; |
| 263 | |
| 264 | memcpy(newkeymap, oldkeymap, |
| 265 | rc_tab->size * sizeof(*newkeymap)); |
| 266 | } else |
| 267 | newkeymap = oldkeymap; |
| 268 | |
| 269 | /* Stores the new code at the table */ |
| 270 | IR_dprintk(1, "#%d: New scan 0x%04x with key 0x%04x\n", |
| 271 | rc_tab->size, scancode, keycode); |
| 272 | |
| 273 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 274 | rc_tab->size = newsize; |
| 275 | if (resize) { |
| 276 | rc_tab->scan = newkeymap; |
| 277 | kfree(oldkeymap); |
| 278 | } |
| 279 | newkeymap[elem].scancode = scancode; |
| 280 | newkeymap[elem].keycode = keycode; |
| 281 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 282 | |
| 283 | return 0; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * ir_setkeycode() - set a keycode at the evdev scancode ->keycode table |
| 288 | * @dev: the struct input_dev device descriptor |
| 289 | * @scancode: the desired scancode |
| 290 | * @keycode: the keycode to be retorned. |
| 291 | * |
| 292 | * This routine is used to handle evdev EVIOCSKEY ioctl. |
| 293 | * There's one caveat here: how can we increase the size of the table? |
| 294 | * If the key is not found, returns -EINVAL, otherwise, returns 0. |
| 295 | */ |
| 296 | static int ir_setkeycode(struct input_dev *dev, |
| 297 | int scancode, int keycode) |
| 298 | { |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 299 | int rc = 0; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 300 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 301 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 302 | struct ir_scancode *keymap = rc_tab->scan; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 303 | unsigned long flags; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 304 | |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 305 | /* |
| 306 | * Handle keycode table deletions |
| 307 | * |
| 308 | * If userspace is adding a KEY_UNKNOWN or KEY_RESERVED, |
| 309 | * deal as a trial to remove an existing scancode attribution |
| 310 | * if table become too big, reduce it to save space |
| 311 | */ |
| 312 | if (keycode == KEY_UNKNOWN || keycode == KEY_RESERVED) { |
| 313 | rc = ir_seek_table(rc_tab, scancode); |
| 314 | if (rc < 0) |
| 315 | return 0; |
| 316 | |
| 317 | IR_dprintk(1, "#%d: Deleting scan 0x%04x\n", rc, scancode); |
| 318 | clear_bit(keymap[rc].keycode, dev->keybit); |
| 319 | ir_delete_key(rc_tab, rc); |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | /* |
| 325 | * Handle keycode replacements |
| 326 | * |
| 327 | * If the scancode exists, just replace by the new value |
| 328 | */ |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 329 | rc = ir_seek_table(rc_tab, scancode); |
Mauro Carvalho Chehab | e97f467 | 2009-12-04 17:17:47 -0300 | [diff] [blame] | 330 | if (rc >= 0) { |
| 331 | IR_dprintk(1, "#%d: Replacing scan 0x%04x with key 0x%04x\n", |
| 332 | rc, scancode, keycode); |
| 333 | |
| 334 | clear_bit(keymap[rc].keycode, dev->keybit); |
| 335 | |
| 336 | spin_lock_irqsave(&rc_tab->lock, flags); |
| 337 | keymap[rc].keycode = keycode; |
| 338 | spin_unlock_irqrestore(&rc_tab->lock, flags); |
| 339 | |
| 340 | set_bit(keycode, dev->keybit); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Handle new scancode inserts |
| 347 | * |
| 348 | * reallocate table if needed and insert a new keycode |
| 349 | */ |
| 350 | |
| 351 | /* Avoid growing the table indefinitely */ |
| 352 | if (rc_tab->size + 1 > IR_TAB_MAX_SIZE) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | rc = ir_insert_key(rc_tab, scancode, keycode); |
| 356 | if (rc < 0) |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 357 | return rc; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 358 | set_bit(keycode, dev->keybit); |
| 359 | |
| 360 | return 0; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | /** |
| 364 | * 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] | 365 | * @input_dev: the struct input_dev descriptor of the device |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 366 | * @scancode: the scancode that we're seeking |
| 367 | * |
| 368 | * This routine is used by the input routines when a key is pressed at the |
| 369 | * IR. The scancode is received and needs to be converted into a keycode. |
| 370 | * If the key is not found, it returns KEY_UNKNOWN. Otherwise, returns the |
| 371 | * corresponding keycode from the table. |
| 372 | */ |
| 373 | u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode) |
| 374 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 375 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 376 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 377 | struct ir_scancode *keymap = rc_tab->scan; |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 378 | int elem; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 379 | |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 380 | elem = ir_seek_table(rc_tab, scancode); |
| 381 | if (elem >= 0) { |
| 382 | IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n", |
| 383 | dev->name, scancode, keymap[elem].keycode); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 384 | |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 385 | return rc_tab->scan[elem].keycode; |
| 386 | } |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 387 | |
| 388 | printk(KERN_INFO "%s: unknown key for scancode 0x%04x\n", |
| 389 | dev->name, scancode); |
| 390 | |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 391 | /* Reports userspace that an unknown keycode were got */ |
| 392 | return KEY_RESERVED; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 393 | } |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 394 | EXPORT_SYMBOL_GPL(ir_g_keycode_from_table); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 395 | |
| 396 | /** |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 397 | * 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] | 398 | * for keymap table get/set |
| 399 | * @input_dev: the struct input_dev descriptor of the device |
| 400 | * @rc_tab: the struct ir_scancode_table table of scancode/keymap |
| 401 | * |
| 402 | * This routine is used to initialize the input infrastructure to work with |
Mauro Carvalho Chehab | 7fee03e | 2009-12-02 15:56:47 -0300 | [diff] [blame] | 403 | * an IR. |
| 404 | * It should be called before registering the IR device. |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 405 | */ |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 406 | int ir_input_register(struct input_dev *input_dev, |
| 407 | struct ir_scancode_table *rc_tab) |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 408 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 409 | struct ir_input_dev *ir_dev; |
| 410 | struct ir_scancode *keymap = rc_tab->scan; |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 411 | int i; |
| 412 | |
| 413 | if (rc_tab->scan == NULL || !rc_tab->size) |
| 414 | return -EINVAL; |
| 415 | |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 416 | ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL); |
| 417 | if (!ir_dev) |
| 418 | return -ENOMEM; |
| 419 | |
| 420 | spin_lock_init(&rc_tab->lock); |
| 421 | |
| 422 | ir_dev->rc_tab.size = ir_roundup_tablesize(rc_tab->size); |
| 423 | ir_dev->rc_tab.scan = kzalloc(ir_dev->rc_tab.size * |
| 424 | sizeof(struct ir_scancode), GFP_KERNEL); |
| 425 | if (!ir_dev->rc_tab.scan) |
| 426 | return -ENOMEM; |
| 427 | |
| 428 | IR_dprintk(1, "Allocated space for %d keycode entries (%zd bytes)\n", |
| 429 | ir_dev->rc_tab.size, |
| 430 | ir_dev->rc_tab.size * sizeof(ir_dev->rc_tab.scan)); |
| 431 | |
| 432 | ir_copy_table(&ir_dev->rc_tab, rc_tab); |
| 433 | |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 434 | /* set the bits for the keys */ |
| 435 | IR_dprintk(1, "key map size: %d\n", rc_tab->size); |
| 436 | for (i = 0; i < rc_tab->size; i++) { |
| 437 | IR_dprintk(1, "#%d: setting bit for keycode 0x%04x\n", |
| 438 | i, keymap[i].keycode); |
| 439 | set_bit(keymap[i].keycode, input_dev->keybit); |
| 440 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 441 | clear_bit(0, input_dev->keybit); |
| 442 | |
| 443 | set_bit(EV_KEY, input_dev->evbit); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 444 | |
| 445 | input_dev->getkeycode = ir_getkeycode; |
| 446 | input_dev->setkeycode = ir_setkeycode; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 447 | input_set_drvdata(input_dev, ir_dev); |
Mauro Carvalho Chehab | ef53a11 | 2009-11-27 22:01:23 -0300 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 451 | EXPORT_SYMBOL_GPL(ir_input_register); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 452 | |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame^] | 453 | void ir_input_unregister(struct input_dev *dev) |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 454 | { |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 455 | struct ir_input_dev *ir_dev = input_get_drvdata(dev); |
| 456 | struct ir_scancode_table *rc_tab = &ir_dev->rc_tab; |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 457 | |
Mauro Carvalho Chehab | 05395a3 | 2009-12-06 08:32:49 -0300 | [diff] [blame] | 458 | if (!rc_tab) |
| 459 | return; |
| 460 | |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 461 | IR_dprintk(1, "Freed keycode table\n"); |
| 462 | |
| 463 | rc_tab->size = 0; |
| 464 | kfree(rc_tab->scan); |
| 465 | rc_tab->scan = NULL; |
Mauro Carvalho Chehab | 75543cc | 2009-12-11 09:44:23 -0300 | [diff] [blame] | 466 | |
| 467 | kfree(ir_dev); |
| 468 | input_set_drvdata(dev, NULL); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 469 | } |
Mauro Carvalho Chehab | 38ef6aa | 2009-12-11 09:47:42 -0300 | [diff] [blame^] | 470 | EXPORT_SYMBOL_GPL(ir_input_unregister); |
Mauro Carvalho Chehab | f6fc504 | 2009-11-29 11:08:02 -0300 | [diff] [blame] | 471 | |
Mauro Carvalho Chehab | 446e4a6 | 2009-12-11 08:34:07 -0300 | [diff] [blame] | 472 | int ir_core_debug; /* ir_debug level (0,1,2) */ |
| 473 | EXPORT_SYMBOL_GPL(ir_core_debug); |
| 474 | module_param_named(debug, ir_core_debug, int, 0644); |
| 475 | |
| 476 | MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>"); |
| 477 | MODULE_LICENSE("GPL"); |