[media] media: rc: if protocols can't be changed, don't be writable
[linux-2.6-block.git] / drivers / media / rc / rc-main.c
CommitLineData
829ba9fe 1/* rc-main.c - Remote Controller core module
ef53a115 2 *
37e59f87 3 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab
446e4a64
MCC
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.
ef53a115
MCC
13 */
14
d3d96820
MCC
15#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
6bda9644 17#include <media/rc-core.h>
631493ec
MCC
18#include <linux/spinlock.h>
19#include <linux/delay.h>
882ead32 20#include <linux/input.h>
153a60bb 21#include <linux/leds.h>
5a0e3ad6 22#include <linux/slab.h>
fcb13097 23#include <linux/idr.h>
bc2a6c57 24#include <linux/device.h>
7a707b89 25#include <linux/module.h>
f62de675 26#include "rc-core-priv.h"
ef53a115 27
b3074c0a
DH
28/* Sizes are in bytes, 256 bytes allows for 32 entries on x64 */
29#define IR_TAB_MIN_SIZE 256
30#define IR_TAB_MAX_SIZE 8192
fcb13097 31#define RC_DEV_MAX 256
f6fc5049 32
d57ea877
SY
33static const struct {
34 const char *name;
35 unsigned int repeat_period;
36 unsigned int scancode_bits;
37} protocols[] = {
6d741bfe
SY
38 [RC_PROTO_UNKNOWN] = { .name = "unknown", .repeat_period = 250 },
39 [RC_PROTO_OTHER] = { .name = "other", .repeat_period = 250 },
40 [RC_PROTO_RC5] = { .name = "rc-5",
d57ea877 41 .scancode_bits = 0x1f7f, .repeat_period = 164 },
6d741bfe 42 [RC_PROTO_RC5X_20] = { .name = "rc-5x-20",
d57ea877 43 .scancode_bits = 0x1f7f3f, .repeat_period = 164 },
6d741bfe 44 [RC_PROTO_RC5_SZ] = { .name = "rc-5-sz",
d57ea877 45 .scancode_bits = 0x2fff, .repeat_period = 164 },
6d741bfe 46 [RC_PROTO_JVC] = { .name = "jvc",
d57ea877 47 .scancode_bits = 0xffff, .repeat_period = 250 },
6d741bfe 48 [RC_PROTO_SONY12] = { .name = "sony-12",
d57ea877 49 .scancode_bits = 0x1f007f, .repeat_period = 100 },
6d741bfe 50 [RC_PROTO_SONY15] = { .name = "sony-15",
d57ea877 51 .scancode_bits = 0xff007f, .repeat_period = 100 },
6d741bfe 52 [RC_PROTO_SONY20] = { .name = "sony-20",
d57ea877 53 .scancode_bits = 0x1fff7f, .repeat_period = 100 },
6d741bfe 54 [RC_PROTO_NEC] = { .name = "nec",
d57ea877 55 .scancode_bits = 0xffff, .repeat_period = 160 },
6d741bfe 56 [RC_PROTO_NECX] = { .name = "nec-x",
d57ea877 57 .scancode_bits = 0xffffff, .repeat_period = 160 },
6d741bfe 58 [RC_PROTO_NEC32] = { .name = "nec-32",
d57ea877 59 .scancode_bits = 0xffffffff, .repeat_period = 160 },
6d741bfe 60 [RC_PROTO_SANYO] = { .name = "sanyo",
d57ea877 61 .scancode_bits = 0x1fffff, .repeat_period = 250 },
6d741bfe 62 [RC_PROTO_MCIR2_KBD] = { .name = "mcir2-kbd",
d57ea877 63 .scancode_bits = 0xffff, .repeat_period = 150 },
6d741bfe 64 [RC_PROTO_MCIR2_MSE] = { .name = "mcir2-mse",
d57ea877 65 .scancode_bits = 0x1fffff, .repeat_period = 150 },
6d741bfe 66 [RC_PROTO_RC6_0] = { .name = "rc-6-0",
d57ea877 67 .scancode_bits = 0xffff, .repeat_period = 164 },
6d741bfe 68 [RC_PROTO_RC6_6A_20] = { .name = "rc-6-6a-20",
d57ea877 69 .scancode_bits = 0xfffff, .repeat_period = 164 },
6d741bfe 70 [RC_PROTO_RC6_6A_24] = { .name = "rc-6-6a-24",
d57ea877 71 .scancode_bits = 0xffffff, .repeat_period = 164 },
6d741bfe 72 [RC_PROTO_RC6_6A_32] = { .name = "rc-6-6a-32",
d57ea877 73 .scancode_bits = 0xffffffff, .repeat_period = 164 },
6d741bfe 74 [RC_PROTO_RC6_MCE] = { .name = "rc-6-mce",
d57ea877 75 .scancode_bits = 0xffff7fff, .repeat_period = 164 },
6d741bfe 76 [RC_PROTO_SHARP] = { .name = "sharp",
d57ea877 77 .scancode_bits = 0x1fff, .repeat_period = 250 },
6d741bfe
SY
78 [RC_PROTO_XMP] = { .name = "xmp", .repeat_period = 250 },
79 [RC_PROTO_CEC] = { .name = "cec", .repeat_period = 550 },
d57ea877 80};
a374fef4 81
4c7b355d 82/* Used to keep track of known keymaps */
631493ec
MCC
83static LIST_HEAD(rc_map_list);
84static DEFINE_SPINLOCK(rc_map_lock);
153a60bb 85static struct led_trigger *led_feedback;
631493ec 86
fcb13097
DH
87/* Used to keep track of rc devices */
88static DEFINE_IDA(rc_ida);
89
d100e659 90static struct rc_map_list *seek_rc_map(const char *name)
631493ec 91{
d100e659 92 struct rc_map_list *map = NULL;
631493ec
MCC
93
94 spin_lock(&rc_map_lock);
95 list_for_each_entry(map, &rc_map_list, list) {
96 if (!strcmp(name, map->map.name)) {
97 spin_unlock(&rc_map_lock);
98 return map;
99 }
100 }
101 spin_unlock(&rc_map_lock);
102
103 return NULL;
104}
105
d100e659 106struct rc_map *rc_map_get(const char *name)
631493ec
MCC
107{
108
d100e659 109 struct rc_map_list *map;
631493ec
MCC
110
111 map = seek_rc_map(name);
2ff56fad 112#ifdef CONFIG_MODULES
631493ec 113 if (!map) {
8ea5488a 114 int rc = request_module("%s", name);
631493ec 115 if (rc < 0) {
d3d96820 116 pr_err("Couldn't load IR keymap %s\n", name);
631493ec
MCC
117 return NULL;
118 }
119 msleep(20); /* Give some time for IR to register */
120
121 map = seek_rc_map(name);
122 }
123#endif
124 if (!map) {
d3d96820 125 pr_err("IR keymap %s not found\n", name);
631493ec
MCC
126 return NULL;
127 }
128
129 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
130
131 return &map->map;
132}
d100e659 133EXPORT_SYMBOL_GPL(rc_map_get);
631493ec 134
d100e659 135int rc_map_register(struct rc_map_list *map)
631493ec
MCC
136{
137 spin_lock(&rc_map_lock);
138 list_add_tail(&map->list, &rc_map_list);
139 spin_unlock(&rc_map_lock);
140 return 0;
141}
d100e659 142EXPORT_SYMBOL_GPL(rc_map_register);
631493ec 143
d100e659 144void rc_map_unregister(struct rc_map_list *map)
631493ec
MCC
145{
146 spin_lock(&rc_map_lock);
147 list_del(&map->list);
148 spin_unlock(&rc_map_lock);
149}
d100e659 150EXPORT_SYMBOL_GPL(rc_map_unregister);
631493ec
MCC
151
152
2f4f58d6 153static struct rc_map_table empty[] = {
631493ec
MCC
154 { 0x2a, KEY_COFFEE },
155};
156
d100e659 157static struct rc_map_list empty_map = {
631493ec 158 .map = {
6d741bfe
SY
159 .scan = empty,
160 .size = ARRAY_SIZE(empty),
161 .rc_proto = RC_PROTO_UNKNOWN, /* Legacy IR type */
162 .name = RC_MAP_EMPTY,
631493ec
MCC
163 }
164};
165
9f470095
DT
166/**
167 * ir_create_table() - initializes a scancode table
b088ba65 168 * @rc_map: the rc_map to initialize
9f470095 169 * @name: name to assign to the table
6d741bfe 170 * @rc_proto: ir type to assign to the new table
9f470095
DT
171 * @size: initial size of the table
172 * @return: zero on success or a negative error code
173 *
b088ba65 174 * This routine will initialize the rc_map and will allocate
d8b4b582 175 * memory to hold at least the specified number of elements.
9f470095 176 */
b088ba65 177static int ir_create_table(struct rc_map *rc_map,
6d741bfe 178 const char *name, u64 rc_proto, size_t size)
9f470095 179{
d54fc3bb
HV
180 rc_map->name = kstrdup(name, GFP_KERNEL);
181 if (!rc_map->name)
182 return -ENOMEM;
6d741bfe 183 rc_map->rc_proto = rc_proto;
2f4f58d6
MCC
184 rc_map->alloc = roundup_pow_of_two(size * sizeof(struct rc_map_table));
185 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
b088ba65 186 rc_map->scan = kmalloc(rc_map->alloc, GFP_KERNEL);
d54fc3bb
HV
187 if (!rc_map->scan) {
188 kfree(rc_map->name);
189 rc_map->name = NULL;
9f470095 190 return -ENOMEM;
d54fc3bb 191 }
9f470095
DT
192
193 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
b088ba65 194 rc_map->size, rc_map->alloc);
9f470095
DT
195 return 0;
196}
197
198/**
199 * ir_free_table() - frees memory allocated by a scancode table
b088ba65 200 * @rc_map: the table whose mappings need to be freed
9f470095
DT
201 *
202 * This routine will free memory alloctaed for key mappings used by given
203 * scancode table.
204 */
b088ba65 205static void ir_free_table(struct rc_map *rc_map)
9f470095 206{
b088ba65 207 rc_map->size = 0;
d54fc3bb 208 kfree(rc_map->name);
c183d358 209 rc_map->name = NULL;
b088ba65
MCC
210 kfree(rc_map->scan);
211 rc_map->scan = NULL;
9f470095
DT
212}
213
7fee03e4 214/**
b3074c0a 215 * ir_resize_table() - resizes a scancode table if necessary
b088ba65 216 * @rc_map: the rc_map to resize
9f470095 217 * @gfp_flags: gfp flags to use when allocating memory
b3074c0a 218 * @return: zero on success or a negative error code
7fee03e4 219 *
b088ba65 220 * This routine will shrink the rc_map if it has lots of
b3074c0a 221 * unused entries and grow it if it is full.
7fee03e4 222 */
b088ba65 223static int ir_resize_table(struct rc_map *rc_map, gfp_t gfp_flags)
7fee03e4 224{
b088ba65 225 unsigned int oldalloc = rc_map->alloc;
b3074c0a 226 unsigned int newalloc = oldalloc;
2f4f58d6
MCC
227 struct rc_map_table *oldscan = rc_map->scan;
228 struct rc_map_table *newscan;
b3074c0a 229
b088ba65 230 if (rc_map->size == rc_map->len) {
b3074c0a 231 /* All entries in use -> grow keytable */
b088ba65 232 if (rc_map->alloc >= IR_TAB_MAX_SIZE)
b3074c0a 233 return -ENOMEM;
7fee03e4 234
b3074c0a
DH
235 newalloc *= 2;
236 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
237 }
7fee03e4 238
b088ba65 239 if ((rc_map->len * 3 < rc_map->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
b3074c0a
DH
240 /* Less than 1/3 of entries in use -> shrink keytable */
241 newalloc /= 2;
242 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
243 }
7fee03e4 244
b3074c0a
DH
245 if (newalloc == oldalloc)
246 return 0;
7fee03e4 247
9f470095 248 newscan = kmalloc(newalloc, gfp_flags);
b3074c0a
DH
249 if (!newscan) {
250 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
251 return -ENOMEM;
252 }
7fee03e4 253
2f4f58d6 254 memcpy(newscan, rc_map->scan, rc_map->len * sizeof(struct rc_map_table));
b088ba65
MCC
255 rc_map->scan = newscan;
256 rc_map->alloc = newalloc;
2f4f58d6 257 rc_map->size = rc_map->alloc / sizeof(struct rc_map_table);
b3074c0a
DH
258 kfree(oldscan);
259 return 0;
7fee03e4
MCC
260}
261
f6fc5049 262/**
9f470095 263 * ir_update_mapping() - set a keycode in the scancode->keycode table
d8b4b582 264 * @dev: the struct rc_dev device descriptor
b088ba65 265 * @rc_map: scancode table to be adjusted
9f470095
DT
266 * @index: index of the mapping that needs to be updated
267 * @keycode: the desired keycode
268 * @return: previous keycode assigned to the mapping
269 *
d8b4b582 270 * This routine is used to update scancode->keycode mapping at given
9f470095
DT
271 * position.
272 */
d8b4b582 273static unsigned int ir_update_mapping(struct rc_dev *dev,
b088ba65 274 struct rc_map *rc_map,
9f470095
DT
275 unsigned int index,
276 unsigned int new_keycode)
277{
b088ba65 278 int old_keycode = rc_map->scan[index].keycode;
9f470095
DT
279 int i;
280
281 /* Did the user wish to remove the mapping? */
282 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
283 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
b088ba65
MCC
284 index, rc_map->scan[index].scancode);
285 rc_map->len--;
286 memmove(&rc_map->scan[index], &rc_map->scan[index+ 1],
2f4f58d6 287 (rc_map->len - index) * sizeof(struct rc_map_table));
9f470095
DT
288 } else {
289 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
290 index,
291 old_keycode == KEY_RESERVED ? "New" : "Replacing",
b088ba65
MCC
292 rc_map->scan[index].scancode, new_keycode);
293 rc_map->scan[index].keycode = new_keycode;
d8b4b582 294 __set_bit(new_keycode, dev->input_dev->keybit);
9f470095
DT
295 }
296
297 if (old_keycode != KEY_RESERVED) {
298 /* A previous mapping was updated... */
d8b4b582 299 __clear_bit(old_keycode, dev->input_dev->keybit);
9f470095 300 /* ... but another scancode might use the same keycode */
b088ba65
MCC
301 for (i = 0; i < rc_map->len; i++) {
302 if (rc_map->scan[i].keycode == old_keycode) {
d8b4b582 303 __set_bit(old_keycode, dev->input_dev->keybit);
9f470095
DT
304 break;
305 }
306 }
307
308 /* Possibly shrink the keytable, failure is not a problem */
b088ba65 309 ir_resize_table(rc_map, GFP_ATOMIC);
9f470095
DT
310 }
311
312 return old_keycode;
313}
314
315/**
4c7b355d 316 * ir_establish_scancode() - set a keycode in the scancode->keycode table
d8b4b582 317 * @dev: the struct rc_dev device descriptor
b088ba65 318 * @rc_map: scancode table to be searched
9f470095
DT
319 * @scancode: the desired scancode
320 * @resize: controls whether we allowed to resize the table to
25985edc 321 * accommodate not yet present scancodes
9f470095
DT
322 * @return: index of the mapping containing scancode in question
323 * or -1U in case of failure.
f6fc5049 324 *
b088ba65 325 * This routine is used to locate given scancode in rc_map.
9f470095
DT
326 * If scancode is not yet present the routine will allocate a new slot
327 * for it.
f6fc5049 328 */
d8b4b582 329static unsigned int ir_establish_scancode(struct rc_dev *dev,
b088ba65 330 struct rc_map *rc_map,
9f470095
DT
331 unsigned int scancode,
332 bool resize)
f6fc5049 333{
b3074c0a 334 unsigned int i;
9dfe4e83
MCC
335
336 /*
337 * Unfortunately, some hardware-based IR decoders don't provide
338 * all bits for the complete IR code. In general, they provide only
339 * the command part of the IR code. Yet, as it is possible to replace
340 * the provided IR with another one, it is needed to allow loading
d8b4b582
DH
341 * IR tables from other remotes. So, we support specifying a mask to
342 * indicate the valid bits of the scancodes.
9dfe4e83 343 */
9d2f1d3c
DH
344 if (dev->scancode_mask)
345 scancode &= dev->scancode_mask;
b3074c0a
DH
346
347 /* First check if we already have a mapping for this ir command */
b088ba65
MCC
348 for (i = 0; i < rc_map->len; i++) {
349 if (rc_map->scan[i].scancode == scancode)
9f470095
DT
350 return i;
351
b3074c0a 352 /* Keytable is sorted from lowest to highest scancode */
b088ba65 353 if (rc_map->scan[i].scancode >= scancode)
b3074c0a 354 break;
b3074c0a 355 }
f6fc5049 356
9f470095 357 /* No previous mapping found, we might need to grow the table */
b088ba65
MCC
358 if (rc_map->size == rc_map->len) {
359 if (!resize || ir_resize_table(rc_map, GFP_ATOMIC))
9f470095
DT
360 return -1U;
361 }
35438946 362
9f470095 363 /* i is the proper index to insert our new keycode */
b088ba65
MCC
364 if (i < rc_map->len)
365 memmove(&rc_map->scan[i + 1], &rc_map->scan[i],
2f4f58d6 366 (rc_map->len - i) * sizeof(struct rc_map_table));
b088ba65
MCC
367 rc_map->scan[i].scancode = scancode;
368 rc_map->scan[i].keycode = KEY_RESERVED;
369 rc_map->len++;
f6fc5049 370
9f470095 371 return i;
f6fc5049
MCC
372}
373
ef53a115 374/**
b3074c0a 375 * ir_setkeycode() - set a keycode in the scancode->keycode table
d8b4b582 376 * @idev: the struct input_dev device descriptor
ef53a115 377 * @scancode: the desired scancode
b3074c0a
DH
378 * @keycode: result
379 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
ef53a115 380 *
b3074c0a 381 * This routine is used to handle evdev EVIOCSKEY ioctl.
ef53a115 382 */
d8b4b582 383static int ir_setkeycode(struct input_dev *idev,
9f470095
DT
384 const struct input_keymap_entry *ke,
385 unsigned int *old_keycode)
ef53a115 386{
d8b4b582 387 struct rc_dev *rdev = input_get_drvdata(idev);
b088ba65 388 struct rc_map *rc_map = &rdev->rc_map;
9f470095
DT
389 unsigned int index;
390 unsigned int scancode;
dea8a39f 391 int retval = 0;
9f470095 392 unsigned long flags;
ef53a115 393
b088ba65 394 spin_lock_irqsave(&rc_map->lock, flags);
9f470095
DT
395
396 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
397 index = ke->index;
b088ba65 398 if (index >= rc_map->len) {
9f470095
DT
399 retval = -EINVAL;
400 goto out;
401 }
402 } else {
403 retval = input_scancode_to_scalar(ke, &scancode);
404 if (retval)
405 goto out;
406
b088ba65
MCC
407 index = ir_establish_scancode(rdev, rc_map, scancode, true);
408 if (index >= rc_map->len) {
9f470095
DT
409 retval = -ENOMEM;
410 goto out;
411 }
412 }
413
b088ba65 414 *old_keycode = ir_update_mapping(rdev, rc_map, index, ke->keycode);
9f470095
DT
415
416out:
b088ba65 417 spin_unlock_irqrestore(&rc_map->lock, flags);
9f470095 418 return retval;
e97f4677
MCC
419}
420
421/**
b3074c0a 422 * ir_setkeytable() - sets several entries in the scancode->keycode table
d8b4b582 423 * @dev: the struct rc_dev device descriptor
b088ba65
MCC
424 * @to: the struct rc_map to copy entries to
425 * @from: the struct rc_map to copy entries from
9f470095 426 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
e97f4677 427 *
b3074c0a 428 * This routine is used to handle table initialization.
e97f4677 429 */
d8b4b582 430static int ir_setkeytable(struct rc_dev *dev,
b088ba65 431 const struct rc_map *from)
e97f4677 432{
b088ba65 433 struct rc_map *rc_map = &dev->rc_map;
9f470095
DT
434 unsigned int i, index;
435 int rc;
436
b088ba65 437 rc = ir_create_table(rc_map, from->name,
6d741bfe 438 from->rc_proto, from->size);
9f470095
DT
439 if (rc)
440 return rc;
441
442 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
b088ba65 443 rc_map->size, rc_map->alloc);
e97f4677 444
b3074c0a 445 for (i = 0; i < from->size; i++) {
b088ba65 446 index = ir_establish_scancode(dev, rc_map,
9f470095 447 from->scan[i].scancode, false);
b088ba65 448 if (index >= rc_map->len) {
9f470095 449 rc = -ENOMEM;
b3074c0a 450 break;
9f470095
DT
451 }
452
b088ba65 453 ir_update_mapping(dev, rc_map, index,
9f470095 454 from->scan[i].keycode);
e97f4677 455 }
9f470095
DT
456
457 if (rc)
b088ba65 458 ir_free_table(rc_map);
9f470095 459
b3074c0a 460 return rc;
ef53a115
MCC
461}
462
9f470095
DT
463/**
464 * ir_lookup_by_scancode() - locate mapping by scancode
b088ba65 465 * @rc_map: the struct rc_map to search
9f470095
DT
466 * @scancode: scancode to look for in the table
467 * @return: index in the table, -1U if not found
468 *
469 * This routine performs binary search in RC keykeymap table for
470 * given scancode.
471 */
b088ba65 472static unsigned int ir_lookup_by_scancode(const struct rc_map *rc_map,
9f470095
DT
473 unsigned int scancode)
474{
0d07025e 475 int start = 0;
b088ba65 476 int end = rc_map->len - 1;
0d07025e 477 int mid;
9f470095
DT
478
479 while (start <= end) {
480 mid = (start + end) / 2;
b088ba65 481 if (rc_map->scan[mid].scancode < scancode)
9f470095 482 start = mid + 1;
b088ba65 483 else if (rc_map->scan[mid].scancode > scancode)
9f470095
DT
484 end = mid - 1;
485 else
486 return mid;
487 }
488
489 return -1U;
490}
491
ef53a115 492/**
b3074c0a 493 * ir_getkeycode() - get a keycode from the scancode->keycode table
d8b4b582 494 * @idev: the struct input_dev device descriptor
ef53a115 495 * @scancode: the desired scancode
b3074c0a
DH
496 * @keycode: used to return the keycode, if found, or KEY_RESERVED
497 * @return: always returns zero.
ef53a115 498 *
b3074c0a 499 * This routine is used to handle evdev EVIOCGKEY ioctl.
ef53a115 500 */
d8b4b582 501static int ir_getkeycode(struct input_dev *idev,
9f470095 502 struct input_keymap_entry *ke)
ef53a115 503{
d8b4b582 504 struct rc_dev *rdev = input_get_drvdata(idev);
b088ba65 505 struct rc_map *rc_map = &rdev->rc_map;
2f4f58d6 506 struct rc_map_table *entry;
9f470095
DT
507 unsigned long flags;
508 unsigned int index;
509 unsigned int scancode;
510 int retval;
ef53a115 511
b088ba65 512 spin_lock_irqsave(&rc_map->lock, flags);
9f470095
DT
513
514 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
515 index = ke->index;
516 } else {
517 retval = input_scancode_to_scalar(ke, &scancode);
518 if (retval)
519 goto out;
520
b088ba65 521 index = ir_lookup_by_scancode(rc_map, scancode);
9f470095
DT
522 }
523
54e74b87
DT
524 if (index < rc_map->len) {
525 entry = &rc_map->scan[index];
526
527 ke->index = index;
528 ke->keycode = entry->keycode;
529 ke->len = sizeof(entry->scancode);
530 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
531
532 } else if (!(ke->flags & INPUT_KEYMAP_BY_INDEX)) {
533 /*
534 * We do not really know the valid range of scancodes
535 * so let's respond with KEY_RESERVED to anything we
536 * do not have mapping for [yet].
537 */
538 ke->index = index;
539 ke->keycode = KEY_RESERVED;
540 } else {
9f470095
DT
541 retval = -EINVAL;
542 goto out;
e97f4677
MCC
543 }
544
47c5ba53
DT
545 retval = 0;
546
9f470095 547out:
b088ba65 548 spin_unlock_irqrestore(&rc_map->lock, flags);
9f470095 549 return retval;
ef53a115
MCC
550}
551
552/**
ca86674b 553 * rc_g_keycode_from_table() - gets the keycode that corresponds to a scancode
d8b4b582
DH
554 * @dev: the struct rc_dev descriptor of the device
555 * @scancode: the scancode to look for
556 * @return: the corresponding keycode, or KEY_RESERVED
ef53a115 557 *
d8b4b582
DH
558 * This routine is used by drivers which need to convert a scancode to a
559 * keycode. Normally it should not be used since drivers should have no
560 * interest in keycodes.
ef53a115 561 */
ca86674b 562u32 rc_g_keycode_from_table(struct rc_dev *dev, u32 scancode)
ef53a115 563{
b088ba65 564 struct rc_map *rc_map = &dev->rc_map;
9f470095
DT
565 unsigned int keycode;
566 unsigned int index;
567 unsigned long flags;
568
b088ba65 569 spin_lock_irqsave(&rc_map->lock, flags);
9f470095 570
b088ba65
MCC
571 index = ir_lookup_by_scancode(rc_map, scancode);
572 keycode = index < rc_map->len ?
573 rc_map->scan[index].keycode : KEY_RESERVED;
9f470095 574
b088ba65 575 spin_unlock_irqrestore(&rc_map->lock, flags);
ef53a115 576
35438946
MCC
577 if (keycode != KEY_RESERVED)
578 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
518f4b26 579 dev->device_name, scancode, keycode);
9f470095 580
b3074c0a 581 return keycode;
ef53a115 582}
ca86674b 583EXPORT_SYMBOL_GPL(rc_g_keycode_from_table);
ef53a115 584
6660de56 585/**
62c65031 586 * ir_do_keyup() - internal function to signal the release of a keypress
d8b4b582 587 * @dev: the struct rc_dev descriptor of the device
98c32bcd 588 * @sync: whether or not to call input_sync
6660de56 589 *
62c65031
DH
590 * This function is used internally to release a keypress, it must be
591 * called with keylock held.
a374fef4 592 */
98c32bcd 593static void ir_do_keyup(struct rc_dev *dev, bool sync)
a374fef4 594{
d8b4b582 595 if (!dev->keypressed)
a374fef4
DH
596 return;
597
d8b4b582
DH
598 IR_dprintk(1, "keyup key 0x%04x\n", dev->last_keycode);
599 input_report_key(dev->input_dev, dev->last_keycode, 0);
153a60bb 600 led_trigger_event(led_feedback, LED_OFF);
98c32bcd
JW
601 if (sync)
602 input_sync(dev->input_dev);
d8b4b582 603 dev->keypressed = false;
a374fef4 604}
62c65031
DH
605
606/**
ca86674b 607 * rc_keyup() - signals the release of a keypress
d8b4b582 608 * @dev: the struct rc_dev descriptor of the device
62c65031
DH
609 *
610 * This routine is used to signal that a key has been released on the
611 * remote control.
612 */
ca86674b 613void rc_keyup(struct rc_dev *dev)
62c65031
DH
614{
615 unsigned long flags;
62c65031 616
d8b4b582 617 spin_lock_irqsave(&dev->keylock, flags);
98c32bcd 618 ir_do_keyup(dev, true);
d8b4b582 619 spin_unlock_irqrestore(&dev->keylock, flags);
62c65031 620}
ca86674b 621EXPORT_SYMBOL_GPL(rc_keyup);
a374fef4
DH
622
623/**
624 * ir_timer_keyup() - generates a keyup event after a timeout
d8b4b582 625 * @cookie: a pointer to the struct rc_dev for the device
a374fef4
DH
626 *
627 * This routine will generate a keyup event some time after a keydown event
628 * is generated when no further activity has been detected.
6660de56 629 */
a374fef4 630static void ir_timer_keyup(unsigned long cookie)
6660de56 631{
d8b4b582 632 struct rc_dev *dev = (struct rc_dev *)cookie;
a374fef4
DH
633 unsigned long flags;
634
635 /*
636 * ir->keyup_jiffies is used to prevent a race condition if a
637 * hardware interrupt occurs at this point and the keyup timer
638 * event is moved further into the future as a result.
639 *
640 * The timer will then be reactivated and this function called
641 * again in the future. We need to exit gracefully in that case
642 * to allow the input subsystem to do its auto-repeat magic or
643 * a keyup event might follow immediately after the keydown.
644 */
d8b4b582
DH
645 spin_lock_irqsave(&dev->keylock, flags);
646 if (time_is_before_eq_jiffies(dev->keyup_jiffies))
98c32bcd 647 ir_do_keyup(dev, true);
d8b4b582 648 spin_unlock_irqrestore(&dev->keylock, flags);
a374fef4
DH
649}
650
651/**
ca86674b 652 * rc_repeat() - signals that a key is still pressed
d8b4b582 653 * @dev: the struct rc_dev descriptor of the device
a374fef4
DH
654 *
655 * This routine is used by IR decoders when a repeat message which does
656 * not include the necessary bits to reproduce the scancode has been
657 * received.
658 */
ca86674b 659void rc_repeat(struct rc_dev *dev)
a374fef4
DH
660{
661 unsigned long flags;
d57ea877 662 unsigned int timeout = protocols[dev->last_protocol].repeat_period;
6660de56 663
d8b4b582 664 spin_lock_irqsave(&dev->keylock, flags);
a374fef4 665
d8b4b582 666 if (!dev->keypressed)
a374fef4 667 goto out;
6660de56 668
265a2988
DH
669 input_event(dev->input_dev, EV_MSC, MSC_SCAN, dev->last_scancode);
670 input_sync(dev->input_dev);
671
d57ea877 672 dev->keyup_jiffies = jiffies + msecs_to_jiffies(timeout);
d8b4b582 673 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
a374fef4
DH
674
675out:
d8b4b582 676 spin_unlock_irqrestore(&dev->keylock, flags);
6660de56 677}
ca86674b 678EXPORT_SYMBOL_GPL(rc_repeat);
6660de56
MCC
679
680/**
62c65031 681 * ir_do_keydown() - internal function to process a keypress
d8b4b582 682 * @dev: the struct rc_dev descriptor of the device
120703f9 683 * @protocol: the protocol of the keypress
62c65031
DH
684 * @scancode: the scancode of the keypress
685 * @keycode: the keycode of the keypress
686 * @toggle: the toggle value of the keypress
6660de56 687 *
62c65031
DH
688 * This function is used internally to register a keypress, it must be
689 * called with keylock held.
6660de56 690 */
6d741bfe 691static void ir_do_keydown(struct rc_dev *dev, enum rc_proto protocol,
120703f9 692 u32 scancode, u32 keycode, u8 toggle)
6660de56 693{
99b0f3c9 694 bool new_event = (!dev->keypressed ||
120703f9 695 dev->last_protocol != protocol ||
99b0f3c9 696 dev->last_scancode != scancode ||
120703f9 697 dev->last_toggle != toggle);
6660de56 698
98c32bcd
JW
699 if (new_event && dev->keypressed)
700 ir_do_keyup(dev, false);
6660de56 701
98c32bcd 702 input_event(dev->input_dev, EV_MSC, MSC_SCAN, scancode);
a374fef4 703
98c32bcd
JW
704 if (new_event && keycode != KEY_RESERVED) {
705 /* Register a keypress */
706 dev->keypressed = true;
120703f9 707 dev->last_protocol = protocol;
98c32bcd
JW
708 dev->last_scancode = scancode;
709 dev->last_toggle = toggle;
710 dev->last_keycode = keycode;
711
25ec587c 712 IR_dprintk(1, "%s: key down event, key 0x%04x, protocol 0x%04x, scancode 0x%08x\n",
518f4b26 713 dev->device_name, keycode, protocol, scancode);
98c32bcd 714 input_report_key(dev->input_dev, keycode, 1);
70a2f912
JH
715
716 led_trigger_event(led_feedback, LED_FULL);
98c32bcd 717 }
ed4d3876 718
d8b4b582 719 input_sync(dev->input_dev);
62c65031 720}
6660de56 721
62c65031 722/**
ca86674b 723 * rc_keydown() - generates input event for a key press
d8b4b582 724 * @dev: the struct rc_dev descriptor of the device
120703f9
DH
725 * @protocol: the protocol for the keypress
726 * @scancode: the scancode for the keypress
62c65031
DH
727 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
728 * support toggle values, this should be set to zero)
729 *
d8b4b582
DH
730 * This routine is used to signal that a key has been pressed on the
731 * remote control.
62c65031 732 */
6d741bfe
SY
733void rc_keydown(struct rc_dev *dev, enum rc_proto protocol, u32 scancode,
734 u8 toggle)
62c65031
DH
735{
736 unsigned long flags;
ca86674b 737 u32 keycode = rc_g_keycode_from_table(dev, scancode);
62c65031 738
d8b4b582 739 spin_lock_irqsave(&dev->keylock, flags);
120703f9 740 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
62c65031 741
d8b4b582 742 if (dev->keypressed) {
d57ea877
SY
743 dev->keyup_jiffies = jiffies +
744 msecs_to_jiffies(protocols[protocol].repeat_period);
d8b4b582 745 mod_timer(&dev->timer_keyup, dev->keyup_jiffies);
62c65031 746 }
d8b4b582 747 spin_unlock_irqrestore(&dev->keylock, flags);
6660de56 748}
ca86674b 749EXPORT_SYMBOL_GPL(rc_keydown);
6660de56 750
62c65031 751/**
ca86674b 752 * rc_keydown_notimeout() - generates input event for a key press without
62c65031 753 * an automatic keyup event at a later time
d8b4b582 754 * @dev: the struct rc_dev descriptor of the device
120703f9
DH
755 * @protocol: the protocol for the keypress
756 * @scancode: the scancode for the keypress
62c65031
DH
757 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
758 * support toggle values, this should be set to zero)
759 *
d8b4b582 760 * This routine is used to signal that a key has been pressed on the
ca86674b 761 * remote control. The driver must manually call rc_keyup() at a later stage.
62c65031 762 */
6d741bfe 763void rc_keydown_notimeout(struct rc_dev *dev, enum rc_proto protocol,
120703f9 764 u32 scancode, u8 toggle)
62c65031
DH
765{
766 unsigned long flags;
ca86674b 767 u32 keycode = rc_g_keycode_from_table(dev, scancode);
62c65031 768
d8b4b582 769 spin_lock_irqsave(&dev->keylock, flags);
120703f9 770 ir_do_keydown(dev, protocol, scancode, keycode, toggle);
d8b4b582 771 spin_unlock_irqrestore(&dev->keylock, flags);
62c65031 772}
ca86674b 773EXPORT_SYMBOL_GPL(rc_keydown_notimeout);
62c65031 774
b590c0bf
SY
775/**
776 * rc_validate_filter() - checks that the scancode and mask are valid and
777 * provides sensible defaults
f423ccc1 778 * @dev: the struct rc_dev descriptor of the device
b590c0bf
SY
779 * @filter: the scancode and mask
780 * @return: 0 or -EINVAL if the filter is not valid
781 */
f423ccc1 782static int rc_validate_filter(struct rc_dev *dev,
b590c0bf
SY
783 struct rc_scancode_filter *filter)
784{
d57ea877 785 u32 mask, s = filter->data;
6d741bfe 786 enum rc_proto protocol = dev->wakeup_protocol;
b590c0bf 787
d57ea877 788 if (protocol >= ARRAY_SIZE(protocols))
2168b416
SY
789 return -EINVAL;
790
d57ea877
SY
791 mask = protocols[protocol].scancode_bits;
792
b590c0bf 793 switch (protocol) {
6d741bfe 794 case RC_PROTO_NECX:
b590c0bf
SY
795 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
796 return -EINVAL;
797 break;
6d741bfe 798 case RC_PROTO_NEC32:
b590c0bf
SY
799 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
800 return -EINVAL;
801 break;
6d741bfe 802 case RC_PROTO_RC6_MCE:
b590c0bf
SY
803 if ((s & 0xffff0000) != 0x800f0000)
804 return -EINVAL;
805 break;
6d741bfe 806 case RC_PROTO_RC6_6A_32:
b590c0bf
SY
807 if ((s & 0xffff0000) == 0x800f0000)
808 return -EINVAL;
809 break;
810 default:
811 break;
812 }
813
d57ea877
SY
814 filter->data &= mask;
815 filter->mask &= mask;
b590c0bf 816
f423ccc1
JH
817 /*
818 * If we have to raw encode the IR for wakeup, we cannot have a mask
819 */
d57ea877 820 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
f423ccc1
JH
821 return -EINVAL;
822
b590c0bf
SY
823 return 0;
824}
825
8b2ff320
SK
826int rc_open(struct rc_dev *rdev)
827{
828 int rval = 0;
829
830 if (!rdev)
831 return -EINVAL;
832
833 mutex_lock(&rdev->lock);
c73bbaa4 834
f02dcdd1 835 if (!rdev->users++ && rdev->open != NULL)
8b2ff320
SK
836 rval = rdev->open(rdev);
837
838 if (rval)
839 rdev->users--;
840
841 mutex_unlock(&rdev->lock);
842
843 return rval;
844}
845EXPORT_SYMBOL_GPL(rc_open);
846
d8b4b582 847static int ir_open(struct input_dev *idev)
ef53a115 848{
d8b4b582 849 struct rc_dev *rdev = input_get_drvdata(idev);
75543cce 850
8b2ff320
SK
851 return rc_open(rdev);
852}
853
854void rc_close(struct rc_dev *rdev)
855{
856 if (rdev) {
857 mutex_lock(&rdev->lock);
858
81b7d14e 859 if (!--rdev->users && rdev->close != NULL)
8b2ff320
SK
860 rdev->close(rdev);
861
862 mutex_unlock(&rdev->lock);
863 }
ef53a115 864}
8b2ff320 865EXPORT_SYMBOL_GPL(rc_close);
d4b778d3 866
d8b4b582 867static void ir_close(struct input_dev *idev)
f6fc5049 868{
d8b4b582 869 struct rc_dev *rdev = input_get_drvdata(idev);
8b2ff320 870 rc_close(rdev);
f6fc5049 871}
f6fc5049 872
bc2a6c57 873/* class for /sys/class/rc */
40fc5325 874static char *rc_devnode(struct device *dev, umode_t *mode)
bc2a6c57
MCC
875{
876 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
877}
878
40fc5325 879static struct class rc_class = {
bc2a6c57 880 .name = "rc",
40fc5325 881 .devnode = rc_devnode,
bc2a6c57
MCC
882};
883
c003ab1b
DH
884/*
885 * These are the protocol textual descriptions that are
886 * used by the sysfs protocols file. Note that the order
887 * of the entries is relevant.
888 */
53df8777 889static const struct {
bc2a6c57 890 u64 type;
53df8777 891 const char *name;
9f0bf366 892 const char *module_name;
bc2a6c57 893} proto_names[] = {
6d741bfe
SY
894 { RC_PROTO_BIT_NONE, "none", NULL },
895 { RC_PROTO_BIT_OTHER, "other", NULL },
896 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
897 { RC_PROTO_BIT_RC5 |
898 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
899 { RC_PROTO_BIT_NEC |
900 RC_PROTO_BIT_NECX |
901 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
902 { RC_PROTO_BIT_RC6_0 |
903 RC_PROTO_BIT_RC6_6A_20 |
904 RC_PROTO_BIT_RC6_6A_24 |
905 RC_PROTO_BIT_RC6_6A_32 |
906 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
907 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
908 { RC_PROTO_BIT_SONY12 |
909 RC_PROTO_BIT_SONY15 |
910 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
911 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
912 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
913 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
914 { RC_PROTO_BIT_MCIR2_KBD |
915 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
916 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
917 { RC_PROTO_BIT_CEC, "cec", NULL },
bc2a6c57
MCC
918};
919
bc2a6c57 920/**
ab88c66d
JH
921 * struct rc_filter_attribute - Device attribute relating to a filter type.
922 * @attr: Device attribute.
923 * @type: Filter type.
924 * @mask: false for filter value, true for filter mask.
925 */
926struct rc_filter_attribute {
927 struct device_attribute attr;
928 enum rc_filter_type type;
929 bool mask;
930};
931#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
932
ab88c66d
JH
933#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
934 struct rc_filter_attribute dev_attr_##_name = { \
935 .attr = __ATTR(_name, _mode, _show, _store), \
936 .type = (_type), \
937 .mask = (_mask), \
938 }
939
dd6ff6a0
DH
940static bool lirc_is_present(void)
941{
942#if defined(CONFIG_LIRC_MODULE)
943 struct module *lirc;
944
945 mutex_lock(&module_mutex);
946 lirc = find_module("lirc_dev");
947 mutex_unlock(&module_mutex);
948
949 return lirc ? true : false;
950#elif defined(CONFIG_LIRC)
951 return true;
952#else
953 return false;
954#endif
955}
956
ab88c66d 957/**
0751d33c 958 * show_protocols() - shows the current IR protocol(s)
d8b4b582 959 * @device: the device descriptor
da6e162d 960 * @mattr: the device attribute struct
bc2a6c57
MCC
961 * @buf: a pointer to the output buffer
962 *
963 * This routine is a callback routine for input read the IR protocol type(s).
0751d33c 964 * it is trigged by reading /sys/class/rc/rc?/protocols.
bc2a6c57
MCC
965 * It returns the protocol names of supported protocols.
966 * Enabled protocols are printed in brackets.
08aeb7c9 967 *
18726a34
DH
968 * dev->lock is taken to guard against races between
969 * store_protocols and show_protocols.
bc2a6c57 970 */
d8b4b582 971static ssize_t show_protocols(struct device *device,
bc2a6c57
MCC
972 struct device_attribute *mattr, char *buf)
973{
d8b4b582 974 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57
MCC
975 u64 allowed, enabled;
976 char *tmp = buf;
977 int i;
978
08aeb7c9
JW
979 mutex_lock(&dev->lock);
980
0751d33c
SY
981 enabled = dev->enabled_protocols;
982 allowed = dev->allowed_protocols;
983 if (dev->raw && !allowed)
984 allowed = ir_raw_get_allowed_protocols();
bc2a6c57 985
da6e162d
DH
986 mutex_unlock(&dev->lock);
987
988 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
989 __func__, (long long)allowed, (long long)enabled);
bc2a6c57
MCC
990
991 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
992 if (allowed & enabled & proto_names[i].type)
993 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
994 else if (allowed & proto_names[i].type)
995 tmp += sprintf(tmp, "%s ", proto_names[i].name);
c003ab1b
DH
996
997 if (allowed & proto_names[i].type)
998 allowed &= ~proto_names[i].type;
bc2a6c57
MCC
999 }
1000
dd6ff6a0 1001 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
275ddb40
DH
1002 tmp += sprintf(tmp, "[lirc] ");
1003
bc2a6c57
MCC
1004 if (tmp != buf)
1005 tmp--;
1006 *tmp = '\n';
08aeb7c9 1007
bc2a6c57
MCC
1008 return tmp + 1 - buf;
1009}
1010
1011/**
da6e162d
DH
1012 * parse_protocol_change() - parses a protocol change request
1013 * @protocols: pointer to the bitmask of current protocols
1014 * @buf: pointer to the buffer with a list of changes
bc2a6c57 1015 *
da6e162d
DH
1016 * Writing "+proto" will add a protocol to the protocol mask.
1017 * Writing "-proto" will remove a protocol from protocol mask.
bc2a6c57
MCC
1018 * Writing "proto" will enable only "proto".
1019 * Writing "none" will disable all protocols.
da6e162d 1020 * Returns the number of changes performed or a negative error code.
bc2a6c57 1021 */
da6e162d 1022static int parse_protocol_change(u64 *protocols, const char *buf)
bc2a6c57 1023{
bc2a6c57 1024 const char *tmp;
da6e162d
DH
1025 unsigned count = 0;
1026 bool enable, disable;
bc2a6c57 1027 u64 mask;
da6e162d 1028 int i;
bc2a6c57 1029
da6e162d 1030 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
bc2a6c57
MCC
1031 if (!*tmp)
1032 break;
1033
1034 if (*tmp == '+') {
1035 enable = true;
1036 disable = false;
1037 tmp++;
1038 } else if (*tmp == '-') {
1039 enable = false;
1040 disable = true;
1041 tmp++;
1042 } else {
1043 enable = false;
1044 disable = false;
1045 }
1046
c003ab1b
DH
1047 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1048 if (!strcasecmp(tmp, proto_names[i].name)) {
1049 mask = proto_names[i].type;
1050 break;
bc2a6c57 1051 }
bc2a6c57
MCC
1052 }
1053
c003ab1b 1054 if (i == ARRAY_SIZE(proto_names)) {
275ddb40
DH
1055 if (!strcasecmp(tmp, "lirc"))
1056 mask = 0;
1057 else {
1058 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1059 return -EINVAL;
1060 }
c003ab1b
DH
1061 }
1062
1063 count++;
1064
bc2a6c57 1065 if (enable)
da6e162d 1066 *protocols |= mask;
bc2a6c57 1067 else if (disable)
da6e162d 1068 *protocols &= ~mask;
bc2a6c57 1069 else
da6e162d 1070 *protocols = mask;
bc2a6c57
MCC
1071 }
1072
1073 if (!count) {
1074 IR_dprintk(1, "Protocol not specified\n");
da6e162d
DH
1075 return -EINVAL;
1076 }
1077
1078 return count;
1079}
1080
9f0bf366 1081static void ir_raw_load_modules(u64 *protocols)
9f0bf366
HK
1082{
1083 u64 available;
1084 int i, ret;
1085
1086 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
6d741bfe
SY
1087 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1088 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1089 RC_PROTO_BIT_UNKNOWN))
9f0bf366
HK
1090 continue;
1091
1092 available = ir_raw_get_allowed_protocols();
1093 if (!(*protocols & proto_names[i].type & ~available))
1094 continue;
1095
1096 if (!proto_names[i].module_name) {
1097 pr_err("Can't enable IR protocol %s\n",
1098 proto_names[i].name);
1099 *protocols &= ~proto_names[i].type;
1100 continue;
1101 }
1102
1103 ret = request_module("%s", proto_names[i].module_name);
1104 if (ret < 0) {
1105 pr_err("Couldn't load IR protocol module %s\n",
1106 proto_names[i].module_name);
1107 *protocols &= ~proto_names[i].type;
1108 continue;
1109 }
1110 msleep(20);
1111 available = ir_raw_get_allowed_protocols();
1112 if (!(*protocols & proto_names[i].type & ~available))
1113 continue;
1114
8caebcdc 1115 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
9f0bf366
HK
1116 proto_names[i].module_name,
1117 proto_names[i].name);
1118 *protocols &= ~proto_names[i].type;
1119 }
1120}
1121
da6e162d
DH
1122/**
1123 * store_protocols() - changes the current/wakeup IR protocol(s)
1124 * @device: the device descriptor
1125 * @mattr: the device attribute struct
1126 * @buf: a pointer to the input buffer
1127 * @len: length of the input buffer
1128 *
1129 * This routine is for changing the IR protocol type.
1130 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1131 * See parse_protocol_change() for the valid commands.
1132 * Returns @len on success or a negative error code.
1133 *
18726a34
DH
1134 * dev->lock is taken to guard against races between
1135 * store_protocols and show_protocols.
da6e162d
DH
1136 */
1137static ssize_t store_protocols(struct device *device,
1138 struct device_attribute *mattr,
1139 const char *buf, size_t len)
1140{
1141 struct rc_dev *dev = to_rc_dev(device);
da6e162d 1142 u64 *current_protocols;
da6e162d 1143 struct rc_scancode_filter *filter;
da6e162d
DH
1144 u64 old_protocols, new_protocols;
1145 ssize_t rc;
1146
0751d33c
SY
1147 IR_dprintk(1, "Normal protocol change requested\n");
1148 current_protocols = &dev->enabled_protocols;
1149 filter = &dev->scancode_filter;
da6e162d 1150
0751d33c 1151 if (!dev->change_protocol) {
da6e162d
DH
1152 IR_dprintk(1, "Protocol switching not supported\n");
1153 return -EINVAL;
1154 }
1155
1156 mutex_lock(&dev->lock);
1157
1158 old_protocols = *current_protocols;
1159 new_protocols = old_protocols;
1160 rc = parse_protocol_change(&new_protocols, buf);
1161 if (rc < 0)
1162 goto out;
1163
0751d33c 1164 rc = dev->change_protocol(dev, &new_protocols);
da6e162d
DH
1165 if (rc < 0) {
1166 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1167 (long long)new_protocols);
08aeb7c9 1168 goto out;
bc2a6c57
MCC
1169 }
1170
9f0bf366
HK
1171 if (dev->driver_type == RC_DRIVER_IR_RAW)
1172 ir_raw_load_modules(&new_protocols);
1173
983c5bd2
JH
1174 if (new_protocols != old_protocols) {
1175 *current_protocols = new_protocols;
1176 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1177 (long long)new_protocols);
bc2a6c57
MCC
1178 }
1179
6bea25af 1180 /*
983c5bd2
JH
1181 * If a protocol change was attempted the filter may need updating, even
1182 * if the actual protocol mask hasn't changed (since the driver may have
1183 * cleared the filter).
6bea25af
JH
1184 * Try setting the same filter with the new protocol (if any).
1185 * Fall back to clearing the filter.
1186 */
0751d33c 1187 if (dev->s_filter && filter->mask) {
da6e162d 1188 if (new_protocols)
0751d33c 1189 rc = dev->s_filter(dev, filter);
da6e162d
DH
1190 else
1191 rc = -1;
6bea25af 1192
da6e162d
DH
1193 if (rc < 0) {
1194 filter->data = 0;
1195 filter->mask = 0;
0751d33c 1196 dev->s_filter(dev, filter);
da6e162d 1197 }
6bea25af
JH
1198 }
1199
da6e162d 1200 rc = len;
08aeb7c9
JW
1201
1202out:
1203 mutex_unlock(&dev->lock);
da6e162d 1204 return rc;
bc2a6c57
MCC
1205}
1206
00942d1a
JH
1207/**
1208 * show_filter() - shows the current scancode filter value or mask
1209 * @device: the device descriptor
1210 * @attr: the device attribute struct
1211 * @buf: a pointer to the output buffer
1212 *
1213 * This routine is a callback routine to read a scancode filter value or mask.
1214 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1215 * It prints the current scancode filter value or mask of the appropriate filter
1216 * type in hexadecimal into @buf and returns the size of the buffer.
1217 *
1218 * Bits of the filter value corresponding to set bits in the filter mask are
1219 * compared against input scancodes and non-matching scancodes are discarded.
1220 *
18726a34 1221 * dev->lock is taken to guard against races between
00942d1a
JH
1222 * store_filter and show_filter.
1223 */
1224static ssize_t show_filter(struct device *device,
1225 struct device_attribute *attr,
1226 char *buf)
1227{
1228 struct rc_dev *dev = to_rc_dev(device);
1229 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1230 struct rc_scancode_filter *filter;
00942d1a
JH
1231 u32 val;
1232
c73bbaa4 1233 mutex_lock(&dev->lock);
c73bbaa4 1234
da6e162d 1235 if (fattr->type == RC_FILTER_NORMAL)
c5540fbb 1236 filter = &dev->scancode_filter;
da6e162d 1237 else
c5540fbb 1238 filter = &dev->scancode_wakeup_filter;
da6e162d 1239
da6e162d
DH
1240 if (fattr->mask)
1241 val = filter->mask;
00942d1a 1242 else
da6e162d 1243 val = filter->data;
00942d1a
JH
1244 mutex_unlock(&dev->lock);
1245
1246 return sprintf(buf, "%#x\n", val);
1247}
1248
1249/**
1250 * store_filter() - changes the scancode filter value
1251 * @device: the device descriptor
1252 * @attr: the device attribute struct
1253 * @buf: a pointer to the input buffer
1254 * @len: length of the input buffer
1255 *
1256 * This routine is for changing a scancode filter value or mask.
1257 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1258 * Returns -EINVAL if an invalid filter value for the current protocol was
1259 * specified or if scancode filtering is not supported by the driver, otherwise
1260 * returns @len.
1261 *
1262 * Bits of the filter value corresponding to set bits in the filter mask are
1263 * compared against input scancodes and non-matching scancodes are discarded.
1264 *
18726a34 1265 * dev->lock is taken to guard against races between
00942d1a
JH
1266 * store_filter and show_filter.
1267 */
1268static ssize_t store_filter(struct device *device,
1269 struct device_attribute *attr,
da6e162d 1270 const char *buf, size_t len)
00942d1a
JH
1271{
1272 struct rc_dev *dev = to_rc_dev(device);
1273 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1274 struct rc_scancode_filter new_filter, *filter;
00942d1a
JH
1275 int ret;
1276 unsigned long val;
23c843b5 1277 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
00942d1a 1278
00942d1a
JH
1279 ret = kstrtoul(buf, 0, &val);
1280 if (ret < 0)
1281 return ret;
1282
da6e162d
DH
1283 if (fattr->type == RC_FILTER_NORMAL) {
1284 set_filter = dev->s_filter;
c5540fbb 1285 filter = &dev->scancode_filter;
da6e162d
DH
1286 } else {
1287 set_filter = dev->s_wakeup_filter;
c5540fbb 1288 filter = &dev->scancode_wakeup_filter;
da6e162d
DH
1289 }
1290
99b0f3c9
DH
1291 if (!set_filter)
1292 return -EINVAL;
00942d1a
JH
1293
1294 mutex_lock(&dev->lock);
1295
da6e162d 1296 new_filter = *filter;
00942d1a 1297 if (fattr->mask)
da6e162d 1298 new_filter.mask = val;
00942d1a 1299 else
da6e162d 1300 new_filter.data = val;
23c843b5 1301
0751d33c 1302 if (fattr->type == RC_FILTER_WAKEUP) {
b590c0bf
SY
1303 /*
1304 * Refuse to set a filter unless a protocol is enabled
1305 * and the filter is valid for that protocol
1306 */
6d741bfe 1307 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
f423ccc1 1308 ret = rc_validate_filter(dev, &new_filter);
b590c0bf 1309 else
0751d33c 1310 ret = -EINVAL;
b590c0bf
SY
1311
1312 if (ret != 0)
0751d33c 1313 goto unlock;
0751d33c
SY
1314 }
1315
1316 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1317 val) {
6bea25af
JH
1318 /* refuse to set a filter unless a protocol is enabled */
1319 ret = -EINVAL;
1320 goto unlock;
1321 }
23c843b5 1322
da6e162d 1323 ret = set_filter(dev, &new_filter);
99b0f3c9
DH
1324 if (ret < 0)
1325 goto unlock;
00942d1a 1326
da6e162d 1327 *filter = new_filter;
00942d1a
JH
1328
1329unlock:
1330 mutex_unlock(&dev->lock);
da6e162d 1331 return (ret < 0) ? ret : len;
00942d1a
JH
1332}
1333
0751d33c
SY
1334/**
1335 * show_wakeup_protocols() - shows the wakeup IR protocol
1336 * @device: the device descriptor
1337 * @mattr: the device attribute struct
1338 * @buf: a pointer to the output buffer
1339 *
1340 * This routine is a callback routine for input read the IR protocol type(s).
1341 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1342 * It returns the protocol names of supported protocols.
1343 * The enabled protocols are printed in brackets.
1344 *
18726a34
DH
1345 * dev->lock is taken to guard against races between
1346 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1347 */
1348static ssize_t show_wakeup_protocols(struct device *device,
1349 struct device_attribute *mattr,
1350 char *buf)
1351{
1352 struct rc_dev *dev = to_rc_dev(device);
1353 u64 allowed;
6d741bfe 1354 enum rc_proto enabled;
0751d33c
SY
1355 char *tmp = buf;
1356 int i;
1357
0751d33c
SY
1358 mutex_lock(&dev->lock);
1359
1360 allowed = dev->allowed_wakeup_protocols;
1361 enabled = dev->wakeup_protocol;
1362
1363 mutex_unlock(&dev->lock);
1364
1365 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1366 __func__, (long long)allowed, enabled);
1367
d57ea877 1368 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
0751d33c
SY
1369 if (allowed & (1ULL << i)) {
1370 if (i == enabled)
d57ea877 1371 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
0751d33c 1372 else
d57ea877 1373 tmp += sprintf(tmp, "%s ", protocols[i].name);
0751d33c
SY
1374 }
1375 }
1376
1377 if (tmp != buf)
1378 tmp--;
1379 *tmp = '\n';
1380
1381 return tmp + 1 - buf;
1382}
1383
1384/**
1385 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1386 * @device: the device descriptor
1387 * @mattr: the device attribute struct
1388 * @buf: a pointer to the input buffer
1389 * @len: length of the input buffer
1390 *
1391 * This routine is for changing the IR protocol type.
1392 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1393 * Returns @len on success or a negative error code.
1394 *
18726a34
DH
1395 * dev->lock is taken to guard against races between
1396 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1397 */
1398static ssize_t store_wakeup_protocols(struct device *device,
1399 struct device_attribute *mattr,
1400 const char *buf, size_t len)
1401{
1402 struct rc_dev *dev = to_rc_dev(device);
6d741bfe 1403 enum rc_proto protocol;
0751d33c
SY
1404 ssize_t rc;
1405 u64 allowed;
1406 int i;
1407
0751d33c
SY
1408 mutex_lock(&dev->lock);
1409
1410 allowed = dev->allowed_wakeup_protocols;
1411
1412 if (sysfs_streq(buf, "none")) {
6d741bfe 1413 protocol = RC_PROTO_UNKNOWN;
0751d33c 1414 } else {
d57ea877 1415 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
0751d33c 1416 if ((allowed & (1ULL << i)) &&
d57ea877 1417 sysfs_streq(buf, protocols[i].name)) {
0751d33c
SY
1418 protocol = i;
1419 break;
1420 }
1421 }
1422
d57ea877 1423 if (i == ARRAY_SIZE(protocols)) {
0751d33c
SY
1424 rc = -EINVAL;
1425 goto out;
1426 }
f423ccc1
JH
1427
1428 if (dev->encode_wakeup) {
1429 u64 mask = 1ULL << protocol;
1430
1431 ir_raw_load_modules(&mask);
1432 if (!mask) {
1433 rc = -EINVAL;
1434 goto out;
1435 }
1436 }
0751d33c
SY
1437 }
1438
1439 if (dev->wakeup_protocol != protocol) {
1440 dev->wakeup_protocol = protocol;
1441 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1442
6d741bfe 1443 if (protocol == RC_PROTO_RC6_MCE)
0751d33c
SY
1444 dev->scancode_wakeup_filter.data = 0x800f0000;
1445 else
1446 dev->scancode_wakeup_filter.data = 0;
1447 dev->scancode_wakeup_filter.mask = 0;
1448
1449 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1450 if (rc == 0)
1451 rc = len;
1452 } else {
1453 rc = len;
1454 }
1455
1456out:
1457 mutex_unlock(&dev->lock);
1458 return rc;
1459}
1460
d8b4b582
DH
1461static void rc_dev_release(struct device *device)
1462{
47cae1e1
MK
1463 struct rc_dev *dev = to_rc_dev(device);
1464
1465 kfree(dev);
d8b4b582
DH
1466}
1467
bc2a6c57
MCC
1468#define ADD_HOTPLUG_VAR(fmt, val...) \
1469 do { \
1470 int err = add_uevent_var(env, fmt, val); \
1471 if (err) \
1472 return err; \
1473 } while (0)
1474
1475static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1476{
d8b4b582 1477 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57 1478
b088ba65
MCC
1479 if (dev->rc_map.name)
1480 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
d8b4b582
DH
1481 if (dev->driver_name)
1482 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
bc2a6c57
MCC
1483
1484 return 0;
1485}
1486
1487/*
1488 * Static device attribute struct with the sysfs attributes for IR's
1489 */
6d75db30
SY
1490static struct device_attribute dev_attr_ro_protocols =
1491__ATTR(protocols, 0444, show_protocols, NULL);
1492static struct device_attribute dev_attr_rw_protocols =
1493__ATTR(protocols, 0644, show_protocols, store_protocols);
0751d33c
SY
1494static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1495 store_wakeup_protocols);
00942d1a
JH
1496static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1497 show_filter, store_filter, RC_FILTER_NORMAL, false);
1498static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1499 show_filter, store_filter, RC_FILTER_NORMAL, true);
1500static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1501 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1502static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1503 show_filter, store_filter, RC_FILTER_WAKEUP, true);
bc2a6c57 1504
6d75db30
SY
1505static struct attribute *rc_dev_rw_protocol_attrs[] = {
1506 &dev_attr_rw_protocols.attr,
99b0f3c9
DH
1507 NULL,
1508};
1509
6d75db30
SY
1510static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1511 .attrs = rc_dev_rw_protocol_attrs,
1512};
1513
1514static struct attribute *rc_dev_ro_protocol_attrs[] = {
1515 &dev_attr_ro_protocols.attr,
1516 NULL,
1517};
1518
1519static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1520 .attrs = rc_dev_ro_protocol_attrs,
99b0f3c9
DH
1521};
1522
99b0f3c9 1523static struct attribute *rc_dev_filter_attrs[] = {
00942d1a
JH
1524 &dev_attr_filter.attr.attr,
1525 &dev_attr_filter_mask.attr.attr,
bc2a6c57
MCC
1526 NULL,
1527};
1528
db68102c 1529static const struct attribute_group rc_dev_filter_attr_grp = {
99b0f3c9 1530 .attrs = rc_dev_filter_attrs,
bc2a6c57
MCC
1531};
1532
99b0f3c9
DH
1533static struct attribute *rc_dev_wakeup_filter_attrs[] = {
1534 &dev_attr_wakeup_filter.attr.attr,
1535 &dev_attr_wakeup_filter_mask.attr.attr,
0751d33c 1536 &dev_attr_wakeup_protocols.attr,
99b0f3c9
DH
1537 NULL,
1538};
1539
db68102c 1540static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
99b0f3c9 1541 .attrs = rc_dev_wakeup_filter_attrs,
bc2a6c57
MCC
1542};
1543
f03f02f9 1544static const struct device_type rc_dev_type = {
d8b4b582 1545 .release = rc_dev_release,
bc2a6c57
MCC
1546 .uevent = rc_dev_uevent,
1547};
1548
0f7499fd 1549struct rc_dev *rc_allocate_device(enum rc_driver_type type)
bc2a6c57 1550{
d8b4b582 1551 struct rc_dev *dev;
bc2a6c57 1552
d8b4b582
DH
1553 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1554 if (!dev)
1555 return NULL;
1556
d34aee10
AS
1557 if (type != RC_DRIVER_IR_RAW_TX) {
1558 dev->input_dev = input_allocate_device();
1559 if (!dev->input_dev) {
1560 kfree(dev);
1561 return NULL;
1562 }
1563
1564 dev->input_dev->getkeycode = ir_getkeycode;
1565 dev->input_dev->setkeycode = ir_setkeycode;
1566 input_set_drvdata(dev->input_dev, dev);
d8b4b582 1567
d34aee10
AS
1568 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1569 (unsigned long)dev);
d8b4b582 1570
d34aee10
AS
1571 spin_lock_init(&dev->rc_map.lock);
1572 spin_lock_init(&dev->keylock);
1573 }
08aeb7c9 1574 mutex_init(&dev->lock);
bc2a6c57 1575
d8b4b582 1576 dev->dev.type = &rc_dev_type;
40fc5325 1577 dev->dev.class = &rc_class;
d8b4b582
DH
1578 device_initialize(&dev->dev);
1579
0f7499fd
AS
1580 dev->driver_type = type;
1581
d8b4b582
DH
1582 __module_get(THIS_MODULE);
1583 return dev;
1584}
1585EXPORT_SYMBOL_GPL(rc_allocate_device);
1586
1587void rc_free_device(struct rc_dev *dev)
bc2a6c57 1588{
b05681b9
MCC
1589 if (!dev)
1590 return;
1591
3dd94f00 1592 input_free_device(dev->input_dev);
b05681b9
MCC
1593
1594 put_device(&dev->dev);
1595
47cae1e1
MK
1596 /* kfree(dev) will be called by the callback function
1597 rc_dev_release() */
1598
b05681b9 1599 module_put(THIS_MODULE);
d8b4b582
DH
1600}
1601EXPORT_SYMBOL_GPL(rc_free_device);
1602
ddbf7d5a
HK
1603static void devm_rc_alloc_release(struct device *dev, void *res)
1604{
1605 rc_free_device(*(struct rc_dev **)res);
1606}
1607
0f7499fd
AS
1608struct rc_dev *devm_rc_allocate_device(struct device *dev,
1609 enum rc_driver_type type)
ddbf7d5a
HK
1610{
1611 struct rc_dev **dr, *rc;
1612
1613 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1614 if (!dr)
1615 return NULL;
1616
0f7499fd 1617 rc = rc_allocate_device(type);
ddbf7d5a
HK
1618 if (!rc) {
1619 devres_free(dr);
1620 return NULL;
1621 }
1622
1623 rc->dev.parent = dev;
1624 rc->managed_alloc = true;
1625 *dr = rc;
1626 devres_add(dev, dr);
1627
1628 return rc;
1629}
1630EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1631
f56928ab 1632static int rc_prepare_rx_device(struct rc_dev *dev)
d8b4b582 1633{
fcb13097 1634 int rc;
7ff2c2bc 1635 struct rc_map *rc_map;
6d741bfe 1636 u64 rc_proto;
bc2a6c57 1637
7ff2c2bc 1638 if (!dev->map_name)
d8b4b582 1639 return -EINVAL;
bc2a6c57 1640
d100e659 1641 rc_map = rc_map_get(dev->map_name);
b088ba65 1642 if (!rc_map)
d100e659 1643 rc_map = rc_map_get(RC_MAP_EMPTY);
b088ba65 1644 if (!rc_map || !rc_map->scan || rc_map->size == 0)
d8b4b582
DH
1645 return -EINVAL;
1646
7ff2c2bc
AS
1647 rc = ir_setkeytable(dev, rc_map);
1648 if (rc)
1649 return rc;
1650
6d741bfe 1651 rc_proto = BIT_ULL(rc_map->rc_proto);
7ff2c2bc 1652
831c4c81
SY
1653 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1654 dev->enabled_protocols = dev->allowed_protocols;
1655
41380868 1656 if (dev->change_protocol) {
6d741bfe 1657 rc = dev->change_protocol(dev, &rc_proto);
7ff2c2bc
AS
1658 if (rc < 0)
1659 goto out_table;
6d741bfe 1660 dev->enabled_protocols = rc_proto;
7ff2c2bc
AS
1661 }
1662
41380868 1663 if (dev->driver_type == RC_DRIVER_IR_RAW)
6d741bfe 1664 ir_raw_load_modules(&rc_proto);
41380868 1665
d8b4b582
DH
1666 set_bit(EV_KEY, dev->input_dev->evbit);
1667 set_bit(EV_REP, dev->input_dev->evbit);
1668 set_bit(EV_MSC, dev->input_dev->evbit);
1669 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1670 if (dev->open)
1671 dev->input_dev->open = ir_open;
1672 if (dev->close)
1673 dev->input_dev->close = ir_close;
1674
b2aceb73
DH
1675 dev->input_dev->dev.parent = &dev->dev;
1676 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1677 dev->input_dev->phys = dev->input_phys;
518f4b26 1678 dev->input_dev->name = dev->device_name;
b2aceb73 1679
f56928ab
DH
1680 return 0;
1681
1682out_table:
1683 ir_free_table(&dev->rc_map);
1684
1685 return rc;
1686}
1687
1688static int rc_setup_rx_device(struct rc_dev *dev)
1689{
1690 int rc;
1691
b2aceb73
DH
1692 /* rc_open will be called here */
1693 rc = input_register_device(dev->input_dev);
1694 if (rc)
f56928ab 1695 return rc;
b2aceb73 1696
7ff2c2bc
AS
1697 /*
1698 * Default delay of 250ms is too short for some protocols, especially
1699 * since the timeout is currently set to 250ms. Increase it to 500ms,
1700 * to avoid wrong repetition of the keycodes. Note that this must be
1701 * set after the call to input_register_device().
1702 */
1703 dev->input_dev->rep[REP_DELAY] = 500;
1704
1705 /*
1706 * As a repeat event on protocols like RC-5 and NEC take as long as
1707 * 110/114ms, using 33ms as a repeat period is not the right thing
1708 * to do.
1709 */
1710 dev->input_dev->rep[REP_PERIOD] = 125;
1711
7ff2c2bc 1712 return 0;
7ff2c2bc
AS
1713}
1714
1715static void rc_free_rx_device(struct rc_dev *dev)
1716{
f56928ab 1717 if (!dev)
7ff2c2bc
AS
1718 return;
1719
f56928ab
DH
1720 if (dev->input_dev) {
1721 input_unregister_device(dev->input_dev);
1722 dev->input_dev = NULL;
1723 }
7ff2c2bc 1724
f56928ab 1725 ir_free_table(&dev->rc_map);
7ff2c2bc
AS
1726}
1727
1728int rc_register_device(struct rc_dev *dev)
1729{
7ff2c2bc
AS
1730 const char *path;
1731 int attr = 0;
1732 int minor;
1733 int rc;
1734
1735 if (!dev)
1736 return -EINVAL;
1737
fcb13097
DH
1738 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1739 if (minor < 0)
1740 return minor;
1741
1742 dev->minor = minor;
1743 dev_set_name(&dev->dev, "rc%u", dev->minor);
1744 dev_set_drvdata(&dev->dev, dev);
587d1b06 1745
99b0f3c9 1746 dev->dev.groups = dev->sysfs_groups;
6d75db30
SY
1747 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1748 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1749 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1750 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
99b0f3c9 1751 if (dev->s_filter)
120703f9 1752 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
99b0f3c9
DH
1753 if (dev->s_wakeup_filter)
1754 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
99b0f3c9
DH
1755 dev->sysfs_groups[attr++] = NULL;
1756
f56928ab
DH
1757 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1758 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1759 rc = ir_raw_event_prepare(dev);
1760 if (rc < 0)
1761 goto out_minor;
1762 }
1763
1764 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1765 rc = rc_prepare_rx_device(dev);
1766 if (rc)
1767 goto out_raw;
1768 }
1769
d8b4b582
DH
1770 rc = device_add(&dev->dev);
1771 if (rc)
f56928ab 1772 goto out_rx_free;
bc2a6c57 1773
d8b4b582 1774 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
4dc0e908 1775 dev_info(&dev->dev, "%s as %s\n",
518f4b26 1776 dev->device_name ?: "Unspecified device", path ?: "N/A");
bc2a6c57
MCC
1777 kfree(path);
1778
f56928ab
DH
1779 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1780 rc = rc_setup_rx_device(dev);
1781 if (rc)
1782 goto out_dev;
1783 }
1784
d34aee10
AS
1785 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1786 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
d8b4b582
DH
1787 rc = ir_raw_event_register(dev);
1788 if (rc < 0)
f56928ab 1789 goto out_rx;
d8b4b582
DH
1790 }
1791
7ff2c2bc 1792 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
fcb13097 1793 dev->minor,
7ff2c2bc 1794 dev->driver_name ? dev->driver_name : "unknown");
d8b4b582 1795
bc2a6c57 1796 return 0;
d8b4b582 1797
f56928ab
DH
1798out_rx:
1799 rc_free_rx_device(dev);
d8b4b582
DH
1800out_dev:
1801 device_del(&dev->dev);
f56928ab
DH
1802out_rx_free:
1803 ir_free_table(&dev->rc_map);
1804out_raw:
1805 ir_raw_event_free(dev);
1806out_minor:
fcb13097 1807 ida_simple_remove(&rc_ida, minor);
d8b4b582 1808 return rc;
bc2a6c57 1809}
d8b4b582 1810EXPORT_SYMBOL_GPL(rc_register_device);
bc2a6c57 1811
ddbf7d5a
HK
1812static void devm_rc_release(struct device *dev, void *res)
1813{
1814 rc_unregister_device(*(struct rc_dev **)res);
1815}
1816
1817int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1818{
1819 struct rc_dev **dr;
1820 int ret;
1821
1822 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1823 if (!dr)
1824 return -ENOMEM;
1825
1826 ret = rc_register_device(dev);
1827 if (ret) {
1828 devres_free(dr);
1829 return ret;
1830 }
1831
1832 *dr = dev;
1833 devres_add(parent, dr);
1834
1835 return 0;
1836}
1837EXPORT_SYMBOL_GPL(devm_rc_register_device);
1838
d8b4b582 1839void rc_unregister_device(struct rc_dev *dev)
bc2a6c57 1840{
d8b4b582
DH
1841 if (!dev)
1842 return;
bc2a6c57 1843
d8b4b582 1844 del_timer_sync(&dev->timer_keyup);
bc2a6c57 1845
d8b4b582
DH
1846 if (dev->driver_type == RC_DRIVER_IR_RAW)
1847 ir_raw_event_unregister(dev);
1848
7ff2c2bc 1849 rc_free_rx_device(dev);
d8b4b582 1850
b05681b9 1851 device_del(&dev->dev);
d8b4b582 1852
fcb13097
DH
1853 ida_simple_remove(&rc_ida, dev->minor);
1854
ddbf7d5a
HK
1855 if (!dev->managed_alloc)
1856 rc_free_device(dev);
bc2a6c57 1857}
b05681b9 1858
d8b4b582 1859EXPORT_SYMBOL_GPL(rc_unregister_device);
bc2a6c57
MCC
1860
1861/*
1862 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1863 */
1864
6bda9644 1865static int __init rc_core_init(void)
bc2a6c57 1866{
40fc5325 1867 int rc = class_register(&rc_class);
bc2a6c57 1868 if (rc) {
d3d96820 1869 pr_err("rc_core: unable to register rc class\n");
bc2a6c57
MCC
1870 return rc;
1871 }
1872
153a60bb 1873 led_trigger_register_simple("rc-feedback", &led_feedback);
d100e659 1874 rc_map_register(&empty_map);
bc2a6c57
MCC
1875
1876 return 0;
1877}
1878
6bda9644 1879static void __exit rc_core_exit(void)
bc2a6c57 1880{
40fc5325 1881 class_unregister(&rc_class);
153a60bb 1882 led_trigger_unregister_simple(led_feedback);
d100e659 1883 rc_map_unregister(&empty_map);
bc2a6c57
MCC
1884}
1885
e76d4ce4 1886subsys_initcall(rc_core_init);
6bda9644 1887module_exit(rc_core_exit);
bc2a6c57 1888
6bda9644
MCC
1889int rc_core_debug; /* ir_debug level (0,1,2) */
1890EXPORT_SYMBOL_GPL(rc_core_debug);
1891module_param_named(debug, rc_core_debug, int, 0644);
446e4a64 1892
37e59f87 1893MODULE_AUTHOR("Mauro Carvalho Chehab");
446e4a64 1894MODULE_LICENSE("GPL");