]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/rc/rc-main.c
media: lirc: validate scancode for transmit
[mirror_ubuntu-jammy-kernel.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
49a4b36a
SY
778/**
779 * rc_validate_scancode() - checks that a scancode is valid for a protocol
780 * @proto: protocol
781 * @scancode: scancode
782 */
783bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
784{
785 switch (proto) {
786 case RC_PROTO_NECX:
787 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
788 return false;
789 break;
790 case RC_PROTO_NEC32:
791 if ((((scancode >> 24) ^ ~(scancode >> 16)) & 0xff) == 0)
792 return false;
793 break;
794 case RC_PROTO_RC6_MCE:
795 if ((scancode & 0xffff0000) != 0x800f0000)
796 return false;
797 break;
798 case RC_PROTO_RC6_6A_32:
799 if ((scancode & 0xffff0000) == 0x800f0000)
800 return false;
801 break;
802 default:
803 break;
804 }
805
806 return true;
807}
808
b590c0bf
SY
809/**
810 * rc_validate_filter() - checks that the scancode and mask are valid and
811 * provides sensible defaults
f423ccc1 812 * @dev: the struct rc_dev descriptor of the device
b590c0bf 813 * @filter: the scancode and mask
f67f366c
MCC
814 *
815 * return: 0 or -EINVAL if the filter is not valid
b590c0bf 816 */
f423ccc1 817static int rc_validate_filter(struct rc_dev *dev,
b590c0bf
SY
818 struct rc_scancode_filter *filter)
819{
d57ea877 820 u32 mask, s = filter->data;
6d741bfe 821 enum rc_proto protocol = dev->wakeup_protocol;
b590c0bf 822
d57ea877 823 if (protocol >= ARRAY_SIZE(protocols))
2168b416
SY
824 return -EINVAL;
825
d57ea877
SY
826 mask = protocols[protocol].scancode_bits;
827
49a4b36a
SY
828 if (!rc_validate_scancode(protocol, s))
829 return -EINVAL;
b590c0bf 830
d57ea877
SY
831 filter->data &= mask;
832 filter->mask &= mask;
b590c0bf 833
f423ccc1
JH
834 /*
835 * If we have to raw encode the IR for wakeup, we cannot have a mask
836 */
d57ea877 837 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
f423ccc1
JH
838 return -EINVAL;
839
b590c0bf
SY
840 return 0;
841}
842
8b2ff320
SK
843int rc_open(struct rc_dev *rdev)
844{
845 int rval = 0;
846
847 if (!rdev)
848 return -EINVAL;
849
850 mutex_lock(&rdev->lock);
c73bbaa4 851
f02dcdd1 852 if (!rdev->users++ && rdev->open != NULL)
8b2ff320
SK
853 rval = rdev->open(rdev);
854
855 if (rval)
856 rdev->users--;
857
858 mutex_unlock(&rdev->lock);
859
860 return rval;
861}
8b2ff320 862
d8b4b582 863static int ir_open(struct input_dev *idev)
ef53a115 864{
d8b4b582 865 struct rc_dev *rdev = input_get_drvdata(idev);
75543cce 866
8b2ff320
SK
867 return rc_open(rdev);
868}
869
870void rc_close(struct rc_dev *rdev)
871{
872 if (rdev) {
873 mutex_lock(&rdev->lock);
874
81b7d14e 875 if (!--rdev->users && rdev->close != NULL)
8b2ff320
SK
876 rdev->close(rdev);
877
878 mutex_unlock(&rdev->lock);
879 }
ef53a115 880}
d4b778d3 881
d8b4b582 882static void ir_close(struct input_dev *idev)
f6fc5049 883{
d8b4b582 884 struct rc_dev *rdev = input_get_drvdata(idev);
8b2ff320 885 rc_close(rdev);
f6fc5049 886}
f6fc5049 887
bc2a6c57 888/* class for /sys/class/rc */
40fc5325 889static char *rc_devnode(struct device *dev, umode_t *mode)
bc2a6c57
MCC
890{
891 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
892}
893
40fc5325 894static struct class rc_class = {
bc2a6c57 895 .name = "rc",
40fc5325 896 .devnode = rc_devnode,
bc2a6c57
MCC
897};
898
c003ab1b
DH
899/*
900 * These are the protocol textual descriptions that are
901 * used by the sysfs protocols file. Note that the order
902 * of the entries is relevant.
903 */
53df8777 904static const struct {
bc2a6c57 905 u64 type;
53df8777 906 const char *name;
9f0bf366 907 const char *module_name;
bc2a6c57 908} proto_names[] = {
6d741bfe
SY
909 { RC_PROTO_BIT_NONE, "none", NULL },
910 { RC_PROTO_BIT_OTHER, "other", NULL },
911 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
912 { RC_PROTO_BIT_RC5 |
913 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
914 { RC_PROTO_BIT_NEC |
915 RC_PROTO_BIT_NECX |
916 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
917 { RC_PROTO_BIT_RC6_0 |
918 RC_PROTO_BIT_RC6_6A_20 |
919 RC_PROTO_BIT_RC6_6A_24 |
920 RC_PROTO_BIT_RC6_6A_32 |
921 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
922 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
923 { RC_PROTO_BIT_SONY12 |
924 RC_PROTO_BIT_SONY15 |
925 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
926 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
927 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
928 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
929 { RC_PROTO_BIT_MCIR2_KBD |
930 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
931 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
932 { RC_PROTO_BIT_CEC, "cec", NULL },
bc2a6c57
MCC
933};
934
bc2a6c57 935/**
ab88c66d
JH
936 * struct rc_filter_attribute - Device attribute relating to a filter type.
937 * @attr: Device attribute.
938 * @type: Filter type.
939 * @mask: false for filter value, true for filter mask.
940 */
941struct rc_filter_attribute {
942 struct device_attribute attr;
943 enum rc_filter_type type;
944 bool mask;
945};
946#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
947
ab88c66d
JH
948#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
949 struct rc_filter_attribute dev_attr_##_name = { \
950 .attr = __ATTR(_name, _mode, _show, _store), \
951 .type = (_type), \
952 .mask = (_mask), \
953 }
954
955/**
0751d33c 956 * show_protocols() - shows the current IR protocol(s)
d8b4b582 957 * @device: the device descriptor
da6e162d 958 * @mattr: the device attribute struct
bc2a6c57
MCC
959 * @buf: a pointer to the output buffer
960 *
961 * This routine is a callback routine for input read the IR protocol type(s).
0751d33c 962 * it is trigged by reading /sys/class/rc/rc?/protocols.
bc2a6c57
MCC
963 * It returns the protocol names of supported protocols.
964 * Enabled protocols are printed in brackets.
08aeb7c9 965 *
18726a34
DH
966 * dev->lock is taken to guard against races between
967 * store_protocols and show_protocols.
bc2a6c57 968 */
d8b4b582 969static ssize_t show_protocols(struct device *device,
bc2a6c57
MCC
970 struct device_attribute *mattr, char *buf)
971{
d8b4b582 972 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57
MCC
973 u64 allowed, enabled;
974 char *tmp = buf;
975 int i;
976
08aeb7c9
JW
977 mutex_lock(&dev->lock);
978
0751d33c
SY
979 enabled = dev->enabled_protocols;
980 allowed = dev->allowed_protocols;
981 if (dev->raw && !allowed)
982 allowed = ir_raw_get_allowed_protocols();
bc2a6c57 983
da6e162d
DH
984 mutex_unlock(&dev->lock);
985
986 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
987 __func__, (long long)allowed, (long long)enabled);
bc2a6c57
MCC
988
989 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
990 if (allowed & enabled & proto_names[i].type)
991 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
992 else if (allowed & proto_names[i].type)
993 tmp += sprintf(tmp, "%s ", proto_names[i].name);
c003ab1b
DH
994
995 if (allowed & proto_names[i].type)
996 allowed &= ~proto_names[i].type;
bc2a6c57
MCC
997 }
998
a60d64b1
SY
999#ifdef CONFIG_LIRC
1000 if (dev->driver_type == RC_DRIVER_IR_RAW)
275ddb40 1001 tmp += sprintf(tmp, "[lirc] ");
a60d64b1 1002#endif
275ddb40 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
0d39ab0b 1081void 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);
b9f407e3
SY
1483 if (dev->device_name)
1484 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
bc2a6c57
MCC
1485
1486 return 0;
1487}
1488
1489/*
1490 * Static device attribute struct with the sysfs attributes for IR's
1491 */
6d75db30
SY
1492static struct device_attribute dev_attr_ro_protocols =
1493__ATTR(protocols, 0444, show_protocols, NULL);
1494static struct device_attribute dev_attr_rw_protocols =
1495__ATTR(protocols, 0644, show_protocols, store_protocols);
0751d33c
SY
1496static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1497 store_wakeup_protocols);
00942d1a
JH
1498static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1499 show_filter, store_filter, RC_FILTER_NORMAL, false);
1500static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1501 show_filter, store_filter, RC_FILTER_NORMAL, true);
1502static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1503 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1504static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1505 show_filter, store_filter, RC_FILTER_WAKEUP, true);
bc2a6c57 1506
6d75db30
SY
1507static struct attribute *rc_dev_rw_protocol_attrs[] = {
1508 &dev_attr_rw_protocols.attr,
99b0f3c9
DH
1509 NULL,
1510};
1511
6d75db30
SY
1512static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1513 .attrs = rc_dev_rw_protocol_attrs,
1514};
1515
1516static struct attribute *rc_dev_ro_protocol_attrs[] = {
1517 &dev_attr_ro_protocols.attr,
1518 NULL,
1519};
1520
1521static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1522 .attrs = rc_dev_ro_protocol_attrs,
99b0f3c9
DH
1523};
1524
99b0f3c9 1525static struct attribute *rc_dev_filter_attrs[] = {
00942d1a
JH
1526 &dev_attr_filter.attr.attr,
1527 &dev_attr_filter_mask.attr.attr,
bc2a6c57
MCC
1528 NULL,
1529};
1530
db68102c 1531static const struct attribute_group rc_dev_filter_attr_grp = {
99b0f3c9 1532 .attrs = rc_dev_filter_attrs,
bc2a6c57
MCC
1533};
1534
99b0f3c9
DH
1535static struct attribute *rc_dev_wakeup_filter_attrs[] = {
1536 &dev_attr_wakeup_filter.attr.attr,
1537 &dev_attr_wakeup_filter_mask.attr.attr,
0751d33c 1538 &dev_attr_wakeup_protocols.attr,
99b0f3c9
DH
1539 NULL,
1540};
1541
db68102c 1542static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
99b0f3c9 1543 .attrs = rc_dev_wakeup_filter_attrs,
bc2a6c57
MCC
1544};
1545
f03f02f9 1546static const struct device_type rc_dev_type = {
d8b4b582 1547 .release = rc_dev_release,
bc2a6c57
MCC
1548 .uevent = rc_dev_uevent,
1549};
1550
0f7499fd 1551struct rc_dev *rc_allocate_device(enum rc_driver_type type)
bc2a6c57 1552{
d8b4b582 1553 struct rc_dev *dev;
bc2a6c57 1554
d8b4b582
DH
1555 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1556 if (!dev)
1557 return NULL;
1558
d34aee10
AS
1559 if (type != RC_DRIVER_IR_RAW_TX) {
1560 dev->input_dev = input_allocate_device();
1561 if (!dev->input_dev) {
1562 kfree(dev);
1563 return NULL;
1564 }
1565
1566 dev->input_dev->getkeycode = ir_getkeycode;
1567 dev->input_dev->setkeycode = ir_setkeycode;
1568 input_set_drvdata(dev->input_dev, dev);
d8b4b582 1569
b17ec78a 1570 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
d8b4b582 1571
d34aee10
AS
1572 spin_lock_init(&dev->rc_map.lock);
1573 spin_lock_init(&dev->keylock);
1574 }
08aeb7c9 1575 mutex_init(&dev->lock);
bc2a6c57 1576
d8b4b582 1577 dev->dev.type = &rc_dev_type;
40fc5325 1578 dev->dev.class = &rc_class;
d8b4b582
DH
1579 device_initialize(&dev->dev);
1580
0f7499fd
AS
1581 dev->driver_type = type;
1582
d8b4b582
DH
1583 __module_get(THIS_MODULE);
1584 return dev;
1585}
1586EXPORT_SYMBOL_GPL(rc_allocate_device);
1587
1588void rc_free_device(struct rc_dev *dev)
bc2a6c57 1589{
b05681b9
MCC
1590 if (!dev)
1591 return;
1592
3dd94f00 1593 input_free_device(dev->input_dev);
b05681b9
MCC
1594
1595 put_device(&dev->dev);
1596
47cae1e1
MK
1597 /* kfree(dev) will be called by the callback function
1598 rc_dev_release() */
1599
b05681b9 1600 module_put(THIS_MODULE);
d8b4b582
DH
1601}
1602EXPORT_SYMBOL_GPL(rc_free_device);
1603
ddbf7d5a
HK
1604static void devm_rc_alloc_release(struct device *dev, void *res)
1605{
1606 rc_free_device(*(struct rc_dev **)res);
1607}
1608
0f7499fd
AS
1609struct rc_dev *devm_rc_allocate_device(struct device *dev,
1610 enum rc_driver_type type)
ddbf7d5a
HK
1611{
1612 struct rc_dev **dr, *rc;
1613
1614 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1615 if (!dr)
1616 return NULL;
1617
0f7499fd 1618 rc = rc_allocate_device(type);
ddbf7d5a
HK
1619 if (!rc) {
1620 devres_free(dr);
1621 return NULL;
1622 }
1623
1624 rc->dev.parent = dev;
1625 rc->managed_alloc = true;
1626 *dr = rc;
1627 devres_add(dev, dr);
1628
1629 return rc;
1630}
1631EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1632
f56928ab 1633static int rc_prepare_rx_device(struct rc_dev *dev)
d8b4b582 1634{
fcb13097 1635 int rc;
7ff2c2bc 1636 struct rc_map *rc_map;
6d741bfe 1637 u64 rc_proto;
bc2a6c57 1638
7ff2c2bc 1639 if (!dev->map_name)
d8b4b582 1640 return -EINVAL;
bc2a6c57 1641
d100e659 1642 rc_map = rc_map_get(dev->map_name);
b088ba65 1643 if (!rc_map)
d100e659 1644 rc_map = rc_map_get(RC_MAP_EMPTY);
b088ba65 1645 if (!rc_map || !rc_map->scan || rc_map->size == 0)
d8b4b582
DH
1646 return -EINVAL;
1647
7ff2c2bc
AS
1648 rc = ir_setkeytable(dev, rc_map);
1649 if (rc)
1650 return rc;
1651
6d741bfe 1652 rc_proto = BIT_ULL(rc_map->rc_proto);
7ff2c2bc 1653
831c4c81
SY
1654 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1655 dev->enabled_protocols = dev->allowed_protocols;
1656
41380868 1657 if (dev->change_protocol) {
6d741bfe 1658 rc = dev->change_protocol(dev, &rc_proto);
7ff2c2bc
AS
1659 if (rc < 0)
1660 goto out_table;
6d741bfe 1661 dev->enabled_protocols = rc_proto;
7ff2c2bc
AS
1662 }
1663
41380868 1664 if (dev->driver_type == RC_DRIVER_IR_RAW)
6d741bfe 1665 ir_raw_load_modules(&rc_proto);
41380868 1666
d8b4b582
DH
1667 set_bit(EV_KEY, dev->input_dev->evbit);
1668 set_bit(EV_REP, dev->input_dev->evbit);
1669 set_bit(EV_MSC, dev->input_dev->evbit);
1670 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1671 if (dev->open)
1672 dev->input_dev->open = ir_open;
1673 if (dev->close)
1674 dev->input_dev->close = ir_close;
1675
b2aceb73
DH
1676 dev->input_dev->dev.parent = &dev->dev;
1677 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1678 dev->input_dev->phys = dev->input_phys;
518f4b26 1679 dev->input_dev->name = dev->device_name;
b2aceb73 1680
f56928ab
DH
1681 return 0;
1682
1683out_table:
1684 ir_free_table(&dev->rc_map);
1685
1686 return rc;
1687}
1688
1689static int rc_setup_rx_device(struct rc_dev *dev)
1690{
1691 int rc;
1692
b2aceb73
DH
1693 /* rc_open will be called here */
1694 rc = input_register_device(dev->input_dev);
1695 if (rc)
f56928ab 1696 return rc;
b2aceb73 1697
7ff2c2bc
AS
1698 /*
1699 * Default delay of 250ms is too short for some protocols, especially
1700 * since the timeout is currently set to 250ms. Increase it to 500ms,
1701 * to avoid wrong repetition of the keycodes. Note that this must be
1702 * set after the call to input_register_device().
1703 */
1704 dev->input_dev->rep[REP_DELAY] = 500;
1705
1706 /*
1707 * As a repeat event on protocols like RC-5 and NEC take as long as
1708 * 110/114ms, using 33ms as a repeat period is not the right thing
1709 * to do.
1710 */
1711 dev->input_dev->rep[REP_PERIOD] = 125;
1712
7ff2c2bc 1713 return 0;
7ff2c2bc
AS
1714}
1715
1716static void rc_free_rx_device(struct rc_dev *dev)
1717{
f56928ab 1718 if (!dev)
7ff2c2bc
AS
1719 return;
1720
f56928ab
DH
1721 if (dev->input_dev) {
1722 input_unregister_device(dev->input_dev);
1723 dev->input_dev = NULL;
1724 }
7ff2c2bc 1725
f56928ab 1726 ir_free_table(&dev->rc_map);
7ff2c2bc
AS
1727}
1728
1729int rc_register_device(struct rc_dev *dev)
1730{
7ff2c2bc
AS
1731 const char *path;
1732 int attr = 0;
1733 int minor;
1734 int rc;
1735
1736 if (!dev)
1737 return -EINVAL;
1738
fcb13097
DH
1739 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1740 if (minor < 0)
1741 return minor;
1742
1743 dev->minor = minor;
1744 dev_set_name(&dev->dev, "rc%u", dev->minor);
1745 dev_set_drvdata(&dev->dev, dev);
587d1b06 1746
99b0f3c9 1747 dev->dev.groups = dev->sysfs_groups;
6d75db30
SY
1748 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1749 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1750 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1751 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
99b0f3c9 1752 if (dev->s_filter)
120703f9 1753 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
99b0f3c9
DH
1754 if (dev->s_wakeup_filter)
1755 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
99b0f3c9
DH
1756 dev->sysfs_groups[attr++] = NULL;
1757
a60d64b1 1758 if (dev->driver_type == RC_DRIVER_IR_RAW) {
f56928ab
DH
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
a60d64b1
SY
1785 /* Ensure that the lirc kfifo is setup before we start the thread */
1786 if (dev->driver_type != RC_DRIVER_SCANCODE) {
1787 rc = ir_lirc_register(dev);
d8b4b582 1788 if (rc < 0)
f56928ab 1789 goto out_rx;
d8b4b582
DH
1790 }
1791
a60d64b1
SY
1792 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1793 rc = ir_raw_event_register(dev);
1794 if (rc < 0)
1795 goto out_lirc;
1796 }
1797
7ff2c2bc 1798 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
fcb13097 1799 dev->minor,
7ff2c2bc 1800 dev->driver_name ? dev->driver_name : "unknown");
d8b4b582 1801
bc2a6c57 1802 return 0;
d8b4b582 1803
a60d64b1
SY
1804out_lirc:
1805 if (dev->driver_type != RC_DRIVER_SCANCODE)
1806 ir_lirc_unregister(dev);
f56928ab
DH
1807out_rx:
1808 rc_free_rx_device(dev);
d8b4b582
DH
1809out_dev:
1810 device_del(&dev->dev);
f56928ab
DH
1811out_rx_free:
1812 ir_free_table(&dev->rc_map);
1813out_raw:
1814 ir_raw_event_free(dev);
1815out_minor:
fcb13097 1816 ida_simple_remove(&rc_ida, minor);
d8b4b582 1817 return rc;
bc2a6c57 1818}
d8b4b582 1819EXPORT_SYMBOL_GPL(rc_register_device);
bc2a6c57 1820
ddbf7d5a
HK
1821static void devm_rc_release(struct device *dev, void *res)
1822{
1823 rc_unregister_device(*(struct rc_dev **)res);
1824}
1825
1826int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1827{
1828 struct rc_dev **dr;
1829 int ret;
1830
1831 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1832 if (!dr)
1833 return -ENOMEM;
1834
1835 ret = rc_register_device(dev);
1836 if (ret) {
1837 devres_free(dr);
1838 return ret;
1839 }
1840
1841 *dr = dev;
1842 devres_add(parent, dr);
1843
1844 return 0;
1845}
1846EXPORT_SYMBOL_GPL(devm_rc_register_device);
1847
d8b4b582 1848void rc_unregister_device(struct rc_dev *dev)
bc2a6c57 1849{
d8b4b582
DH
1850 if (!dev)
1851 return;
bc2a6c57 1852
d8b4b582 1853 del_timer_sync(&dev->timer_keyup);
bc2a6c57 1854
d8b4b582
DH
1855 if (dev->driver_type == RC_DRIVER_IR_RAW)
1856 ir_raw_event_unregister(dev);
1857
7ff2c2bc 1858 rc_free_rx_device(dev);
d8b4b582 1859
a60d64b1
SY
1860 if (dev->driver_type != RC_DRIVER_SCANCODE)
1861 ir_lirc_unregister(dev);
1862
b05681b9 1863 device_del(&dev->dev);
d8b4b582 1864
fcb13097
DH
1865 ida_simple_remove(&rc_ida, dev->minor);
1866
ddbf7d5a
HK
1867 if (!dev->managed_alloc)
1868 rc_free_device(dev);
bc2a6c57 1869}
b05681b9 1870
d8b4b582 1871EXPORT_SYMBOL_GPL(rc_unregister_device);
bc2a6c57
MCC
1872
1873/*
1874 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1875 */
1876
6bda9644 1877static int __init rc_core_init(void)
bc2a6c57 1878{
40fc5325 1879 int rc = class_register(&rc_class);
bc2a6c57 1880 if (rc) {
d3d96820 1881 pr_err("rc_core: unable to register rc class\n");
bc2a6c57
MCC
1882 return rc;
1883 }
1884
a60d64b1
SY
1885 rc = lirc_dev_init();
1886 if (rc) {
1887 pr_err("rc_core: unable to init lirc\n");
1888 class_unregister(&rc_class);
1889 return 0;
1890 }
1891
153a60bb 1892 led_trigger_register_simple("rc-feedback", &led_feedback);
d100e659 1893 rc_map_register(&empty_map);
bc2a6c57
MCC
1894
1895 return 0;
1896}
1897
6bda9644 1898static void __exit rc_core_exit(void)
bc2a6c57 1899{
a60d64b1 1900 lirc_dev_exit();
40fc5325 1901 class_unregister(&rc_class);
153a60bb 1902 led_trigger_unregister_simple(led_feedback);
d100e659 1903 rc_map_unregister(&empty_map);
bc2a6c57
MCC
1904}
1905
e76d4ce4 1906subsys_initcall(rc_core_init);
6bda9644 1907module_exit(rc_core_exit);
bc2a6c57 1908
6bda9644
MCC
1909int rc_core_debug; /* ir_debug level (0,1,2) */
1910EXPORT_SYMBOL_GPL(rc_core_debug);
1911module_param_named(debug, rc_core_debug, int, 0644);
446e4a64 1912
37e59f87 1913MODULE_AUTHOR("Mauro Carvalho Chehab");
20835280 1914MODULE_LICENSE("GPL v2");