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