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