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