]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/rc/rc-main.c
media: lirc: use kfifo rather than lirc_buffer for raw IR
[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 778/**
6b514c4a
SY
779 * rc_validate_scancode() - checks that a scancode is valid for a protocol.
780 * For nec, it should do the opposite of ir_nec_bytes_to_scancode()
49a4b36a
SY
781 * @proto: protocol
782 * @scancode: scancode
783 */
784bool rc_validate_scancode(enum rc_proto proto, u32 scancode)
785{
786 switch (proto) {
6b514c4a
SY
787 /*
788 * NECX has a 16-bit address; if the lower 8 bits match the upper
789 * 8 bits inverted, then the address would match regular nec.
790 */
49a4b36a
SY
791 case RC_PROTO_NECX:
792 if ((((scancode >> 16) ^ ~(scancode >> 8)) & 0xff) == 0)
793 return false;
794 break;
6b514c4a
SY
795 /*
796 * NEC32 has a 16 bit address and 16 bit command. If the lower 8 bits
797 * of the command match the upper 8 bits inverted, then it would
798 * be either NEC or NECX.
799 */
49a4b36a 800 case RC_PROTO_NEC32:
6b514c4a 801 if ((((scancode >> 8) ^ ~scancode) & 0xff) == 0)
49a4b36a
SY
802 return false;
803 break;
6b514c4a
SY
804 /*
805 * If the customer code (top 32-bit) is 0x800f, it is MCE else it
806 * is regular mode-6a 32 bit
807 */
49a4b36a
SY
808 case RC_PROTO_RC6_MCE:
809 if ((scancode & 0xffff0000) != 0x800f0000)
810 return false;
811 break;
812 case RC_PROTO_RC6_6A_32:
813 if ((scancode & 0xffff0000) == 0x800f0000)
814 return false;
815 break;
816 default:
817 break;
818 }
819
820 return true;
821}
822
b590c0bf
SY
823/**
824 * rc_validate_filter() - checks that the scancode and mask are valid and
825 * provides sensible defaults
f423ccc1 826 * @dev: the struct rc_dev descriptor of the device
b590c0bf 827 * @filter: the scancode and mask
f67f366c
MCC
828 *
829 * return: 0 or -EINVAL if the filter is not valid
b590c0bf 830 */
f423ccc1 831static int rc_validate_filter(struct rc_dev *dev,
b590c0bf
SY
832 struct rc_scancode_filter *filter)
833{
d57ea877 834 u32 mask, s = filter->data;
6d741bfe 835 enum rc_proto protocol = dev->wakeup_protocol;
b590c0bf 836
d57ea877 837 if (protocol >= ARRAY_SIZE(protocols))
2168b416
SY
838 return -EINVAL;
839
d57ea877
SY
840 mask = protocols[protocol].scancode_bits;
841
49a4b36a
SY
842 if (!rc_validate_scancode(protocol, s))
843 return -EINVAL;
b590c0bf 844
d57ea877
SY
845 filter->data &= mask;
846 filter->mask &= mask;
b590c0bf 847
f423ccc1
JH
848 /*
849 * If we have to raw encode the IR for wakeup, we cannot have a mask
850 */
d57ea877 851 if (dev->encode_wakeup && filter->mask != 0 && filter->mask != mask)
f423ccc1
JH
852 return -EINVAL;
853
b590c0bf
SY
854 return 0;
855}
856
8b2ff320
SK
857int rc_open(struct rc_dev *rdev)
858{
859 int rval = 0;
860
861 if (!rdev)
862 return -EINVAL;
863
864 mutex_lock(&rdev->lock);
c73bbaa4 865
f02dcdd1 866 if (!rdev->users++ && rdev->open != NULL)
8b2ff320
SK
867 rval = rdev->open(rdev);
868
869 if (rval)
870 rdev->users--;
871
872 mutex_unlock(&rdev->lock);
873
874 return rval;
875}
8b2ff320 876
d8b4b582 877static int ir_open(struct input_dev *idev)
ef53a115 878{
d8b4b582 879 struct rc_dev *rdev = input_get_drvdata(idev);
75543cce 880
8b2ff320
SK
881 return rc_open(rdev);
882}
883
884void rc_close(struct rc_dev *rdev)
885{
886 if (rdev) {
887 mutex_lock(&rdev->lock);
888
81b7d14e 889 if (!--rdev->users && rdev->close != NULL)
8b2ff320
SK
890 rdev->close(rdev);
891
892 mutex_unlock(&rdev->lock);
893 }
ef53a115 894}
d4b778d3 895
d8b4b582 896static void ir_close(struct input_dev *idev)
f6fc5049 897{
d8b4b582 898 struct rc_dev *rdev = input_get_drvdata(idev);
8b2ff320 899 rc_close(rdev);
f6fc5049 900}
f6fc5049 901
bc2a6c57 902/* class for /sys/class/rc */
40fc5325 903static char *rc_devnode(struct device *dev, umode_t *mode)
bc2a6c57
MCC
904{
905 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
906}
907
40fc5325 908static struct class rc_class = {
bc2a6c57 909 .name = "rc",
40fc5325 910 .devnode = rc_devnode,
bc2a6c57
MCC
911};
912
c003ab1b
DH
913/*
914 * These are the protocol textual descriptions that are
915 * used by the sysfs protocols file. Note that the order
916 * of the entries is relevant.
917 */
53df8777 918static const struct {
bc2a6c57 919 u64 type;
53df8777 920 const char *name;
9f0bf366 921 const char *module_name;
bc2a6c57 922} proto_names[] = {
6d741bfe
SY
923 { RC_PROTO_BIT_NONE, "none", NULL },
924 { RC_PROTO_BIT_OTHER, "other", NULL },
925 { RC_PROTO_BIT_UNKNOWN, "unknown", NULL },
926 { RC_PROTO_BIT_RC5 |
927 RC_PROTO_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
928 { RC_PROTO_BIT_NEC |
929 RC_PROTO_BIT_NECX |
930 RC_PROTO_BIT_NEC32, "nec", "ir-nec-decoder" },
931 { RC_PROTO_BIT_RC6_0 |
932 RC_PROTO_BIT_RC6_6A_20 |
933 RC_PROTO_BIT_RC6_6A_24 |
934 RC_PROTO_BIT_RC6_6A_32 |
935 RC_PROTO_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
936 { RC_PROTO_BIT_JVC, "jvc", "ir-jvc-decoder" },
937 { RC_PROTO_BIT_SONY12 |
938 RC_PROTO_BIT_SONY15 |
939 RC_PROTO_BIT_SONY20, "sony", "ir-sony-decoder" },
940 { RC_PROTO_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
941 { RC_PROTO_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
942 { RC_PROTO_BIT_SHARP, "sharp", "ir-sharp-decoder" },
943 { RC_PROTO_BIT_MCIR2_KBD |
944 RC_PROTO_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
945 { RC_PROTO_BIT_XMP, "xmp", "ir-xmp-decoder" },
946 { RC_PROTO_BIT_CEC, "cec", NULL },
bc2a6c57
MCC
947};
948
bc2a6c57 949/**
ab88c66d
JH
950 * struct rc_filter_attribute - Device attribute relating to a filter type.
951 * @attr: Device attribute.
952 * @type: Filter type.
953 * @mask: false for filter value, true for filter mask.
954 */
955struct rc_filter_attribute {
956 struct device_attribute attr;
957 enum rc_filter_type type;
958 bool mask;
959};
960#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
961
ab88c66d
JH
962#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
963 struct rc_filter_attribute dev_attr_##_name = { \
964 .attr = __ATTR(_name, _mode, _show, _store), \
965 .type = (_type), \
966 .mask = (_mask), \
967 }
968
969/**
0751d33c 970 * show_protocols() - shows the current IR protocol(s)
d8b4b582 971 * @device: the device descriptor
da6e162d 972 * @mattr: the device attribute struct
bc2a6c57
MCC
973 * @buf: a pointer to the output buffer
974 *
975 * This routine is a callback routine for input read the IR protocol type(s).
0751d33c 976 * it is trigged by reading /sys/class/rc/rc?/protocols.
bc2a6c57
MCC
977 * It returns the protocol names of supported protocols.
978 * Enabled protocols are printed in brackets.
08aeb7c9 979 *
18726a34
DH
980 * dev->lock is taken to guard against races between
981 * store_protocols and show_protocols.
bc2a6c57 982 */
d8b4b582 983static ssize_t show_protocols(struct device *device,
bc2a6c57
MCC
984 struct device_attribute *mattr, char *buf)
985{
d8b4b582 986 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57
MCC
987 u64 allowed, enabled;
988 char *tmp = buf;
989 int i;
990
08aeb7c9
JW
991 mutex_lock(&dev->lock);
992
0751d33c
SY
993 enabled = dev->enabled_protocols;
994 allowed = dev->allowed_protocols;
995 if (dev->raw && !allowed)
996 allowed = ir_raw_get_allowed_protocols();
bc2a6c57 997
da6e162d
DH
998 mutex_unlock(&dev->lock);
999
1000 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
1001 __func__, (long long)allowed, (long long)enabled);
bc2a6c57
MCC
1002
1003 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1004 if (allowed & enabled & proto_names[i].type)
1005 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
1006 else if (allowed & proto_names[i].type)
1007 tmp += sprintf(tmp, "%s ", proto_names[i].name);
c003ab1b
DH
1008
1009 if (allowed & proto_names[i].type)
1010 allowed &= ~proto_names[i].type;
bc2a6c57
MCC
1011 }
1012
a60d64b1
SY
1013#ifdef CONFIG_LIRC
1014 if (dev->driver_type == RC_DRIVER_IR_RAW)
275ddb40 1015 tmp += sprintf(tmp, "[lirc] ");
a60d64b1 1016#endif
275ddb40 1017
bc2a6c57
MCC
1018 if (tmp != buf)
1019 tmp--;
1020 *tmp = '\n';
08aeb7c9 1021
bc2a6c57
MCC
1022 return tmp + 1 - buf;
1023}
1024
1025/**
da6e162d
DH
1026 * parse_protocol_change() - parses a protocol change request
1027 * @protocols: pointer to the bitmask of current protocols
1028 * @buf: pointer to the buffer with a list of changes
bc2a6c57 1029 *
da6e162d
DH
1030 * Writing "+proto" will add a protocol to the protocol mask.
1031 * Writing "-proto" will remove a protocol from protocol mask.
bc2a6c57
MCC
1032 * Writing "proto" will enable only "proto".
1033 * Writing "none" will disable all protocols.
da6e162d 1034 * Returns the number of changes performed or a negative error code.
bc2a6c57 1035 */
da6e162d 1036static int parse_protocol_change(u64 *protocols, const char *buf)
bc2a6c57 1037{
bc2a6c57 1038 const char *tmp;
da6e162d
DH
1039 unsigned count = 0;
1040 bool enable, disable;
bc2a6c57 1041 u64 mask;
da6e162d 1042 int i;
bc2a6c57 1043
da6e162d 1044 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
bc2a6c57
MCC
1045 if (!*tmp)
1046 break;
1047
1048 if (*tmp == '+') {
1049 enable = true;
1050 disable = false;
1051 tmp++;
1052 } else if (*tmp == '-') {
1053 enable = false;
1054 disable = true;
1055 tmp++;
1056 } else {
1057 enable = false;
1058 disable = false;
1059 }
1060
c003ab1b
DH
1061 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1062 if (!strcasecmp(tmp, proto_names[i].name)) {
1063 mask = proto_names[i].type;
1064 break;
bc2a6c57 1065 }
bc2a6c57
MCC
1066 }
1067
c003ab1b 1068 if (i == ARRAY_SIZE(proto_names)) {
275ddb40
DH
1069 if (!strcasecmp(tmp, "lirc"))
1070 mask = 0;
1071 else {
1072 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1073 return -EINVAL;
1074 }
c003ab1b
DH
1075 }
1076
1077 count++;
1078
bc2a6c57 1079 if (enable)
da6e162d 1080 *protocols |= mask;
bc2a6c57 1081 else if (disable)
da6e162d 1082 *protocols &= ~mask;
bc2a6c57 1083 else
da6e162d 1084 *protocols = mask;
bc2a6c57
MCC
1085 }
1086
1087 if (!count) {
1088 IR_dprintk(1, "Protocol not specified\n");
da6e162d
DH
1089 return -EINVAL;
1090 }
1091
1092 return count;
1093}
1094
0d39ab0b 1095void ir_raw_load_modules(u64 *protocols)
9f0bf366
HK
1096{
1097 u64 available;
1098 int i, ret;
1099
1100 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
6d741bfe
SY
1101 if (proto_names[i].type == RC_PROTO_BIT_NONE ||
1102 proto_names[i].type & (RC_PROTO_BIT_OTHER |
1103 RC_PROTO_BIT_UNKNOWN))
9f0bf366
HK
1104 continue;
1105
1106 available = ir_raw_get_allowed_protocols();
1107 if (!(*protocols & proto_names[i].type & ~available))
1108 continue;
1109
1110 if (!proto_names[i].module_name) {
1111 pr_err("Can't enable IR protocol %s\n",
1112 proto_names[i].name);
1113 *protocols &= ~proto_names[i].type;
1114 continue;
1115 }
1116
1117 ret = request_module("%s", proto_names[i].module_name);
1118 if (ret < 0) {
1119 pr_err("Couldn't load IR protocol module %s\n",
1120 proto_names[i].module_name);
1121 *protocols &= ~proto_names[i].type;
1122 continue;
1123 }
1124 msleep(20);
1125 available = ir_raw_get_allowed_protocols();
1126 if (!(*protocols & proto_names[i].type & ~available))
1127 continue;
1128
8caebcdc 1129 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
9f0bf366
HK
1130 proto_names[i].module_name,
1131 proto_names[i].name);
1132 *protocols &= ~proto_names[i].type;
1133 }
1134}
1135
da6e162d
DH
1136/**
1137 * store_protocols() - changes the current/wakeup IR protocol(s)
1138 * @device: the device descriptor
1139 * @mattr: the device attribute struct
1140 * @buf: a pointer to the input buffer
1141 * @len: length of the input buffer
1142 *
1143 * This routine is for changing the IR protocol type.
1144 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1145 * See parse_protocol_change() for the valid commands.
1146 * Returns @len on success or a negative error code.
1147 *
18726a34
DH
1148 * dev->lock is taken to guard against races between
1149 * store_protocols and show_protocols.
da6e162d
DH
1150 */
1151static ssize_t store_protocols(struct device *device,
1152 struct device_attribute *mattr,
1153 const char *buf, size_t len)
1154{
1155 struct rc_dev *dev = to_rc_dev(device);
da6e162d 1156 u64 *current_protocols;
da6e162d 1157 struct rc_scancode_filter *filter;
da6e162d
DH
1158 u64 old_protocols, new_protocols;
1159 ssize_t rc;
1160
0751d33c
SY
1161 IR_dprintk(1, "Normal protocol change requested\n");
1162 current_protocols = &dev->enabled_protocols;
1163 filter = &dev->scancode_filter;
da6e162d 1164
0751d33c 1165 if (!dev->change_protocol) {
da6e162d
DH
1166 IR_dprintk(1, "Protocol switching not supported\n");
1167 return -EINVAL;
1168 }
1169
1170 mutex_lock(&dev->lock);
1171
1172 old_protocols = *current_protocols;
1173 new_protocols = old_protocols;
1174 rc = parse_protocol_change(&new_protocols, buf);
1175 if (rc < 0)
1176 goto out;
1177
0751d33c 1178 rc = dev->change_protocol(dev, &new_protocols);
da6e162d
DH
1179 if (rc < 0) {
1180 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1181 (long long)new_protocols);
08aeb7c9 1182 goto out;
bc2a6c57
MCC
1183 }
1184
9f0bf366
HK
1185 if (dev->driver_type == RC_DRIVER_IR_RAW)
1186 ir_raw_load_modules(&new_protocols);
1187
983c5bd2
JH
1188 if (new_protocols != old_protocols) {
1189 *current_protocols = new_protocols;
1190 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1191 (long long)new_protocols);
bc2a6c57
MCC
1192 }
1193
6bea25af 1194 /*
983c5bd2
JH
1195 * If a protocol change was attempted the filter may need updating, even
1196 * if the actual protocol mask hasn't changed (since the driver may have
1197 * cleared the filter).
6bea25af
JH
1198 * Try setting the same filter with the new protocol (if any).
1199 * Fall back to clearing the filter.
1200 */
0751d33c 1201 if (dev->s_filter && filter->mask) {
da6e162d 1202 if (new_protocols)
0751d33c 1203 rc = dev->s_filter(dev, filter);
da6e162d
DH
1204 else
1205 rc = -1;
6bea25af 1206
da6e162d
DH
1207 if (rc < 0) {
1208 filter->data = 0;
1209 filter->mask = 0;
0751d33c 1210 dev->s_filter(dev, filter);
da6e162d 1211 }
6bea25af
JH
1212 }
1213
da6e162d 1214 rc = len;
08aeb7c9
JW
1215
1216out:
1217 mutex_unlock(&dev->lock);
da6e162d 1218 return rc;
bc2a6c57
MCC
1219}
1220
00942d1a
JH
1221/**
1222 * show_filter() - shows the current scancode filter value or mask
1223 * @device: the device descriptor
1224 * @attr: the device attribute struct
1225 * @buf: a pointer to the output buffer
1226 *
1227 * This routine is a callback routine to read a scancode filter value or mask.
1228 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1229 * It prints the current scancode filter value or mask of the appropriate filter
1230 * type in hexadecimal into @buf and returns the size of the buffer.
1231 *
1232 * Bits of the filter value corresponding to set bits in the filter mask are
1233 * compared against input scancodes and non-matching scancodes are discarded.
1234 *
18726a34 1235 * dev->lock is taken to guard against races between
00942d1a
JH
1236 * store_filter and show_filter.
1237 */
1238static ssize_t show_filter(struct device *device,
1239 struct device_attribute *attr,
1240 char *buf)
1241{
1242 struct rc_dev *dev = to_rc_dev(device);
1243 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1244 struct rc_scancode_filter *filter;
00942d1a
JH
1245 u32 val;
1246
c73bbaa4 1247 mutex_lock(&dev->lock);
c73bbaa4 1248
da6e162d 1249 if (fattr->type == RC_FILTER_NORMAL)
c5540fbb 1250 filter = &dev->scancode_filter;
da6e162d 1251 else
c5540fbb 1252 filter = &dev->scancode_wakeup_filter;
da6e162d 1253
da6e162d
DH
1254 if (fattr->mask)
1255 val = filter->mask;
00942d1a 1256 else
da6e162d 1257 val = filter->data;
00942d1a
JH
1258 mutex_unlock(&dev->lock);
1259
1260 return sprintf(buf, "%#x\n", val);
1261}
1262
1263/**
1264 * store_filter() - changes the scancode filter value
1265 * @device: the device descriptor
1266 * @attr: the device attribute struct
1267 * @buf: a pointer to the input buffer
1268 * @len: length of the input buffer
1269 *
1270 * This routine is for changing a scancode filter value or mask.
1271 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1272 * Returns -EINVAL if an invalid filter value for the current protocol was
1273 * specified or if scancode filtering is not supported by the driver, otherwise
1274 * returns @len.
1275 *
1276 * Bits of the filter value corresponding to set bits in the filter mask are
1277 * compared against input scancodes and non-matching scancodes are discarded.
1278 *
18726a34 1279 * dev->lock is taken to guard against races between
00942d1a
JH
1280 * store_filter and show_filter.
1281 */
1282static ssize_t store_filter(struct device *device,
1283 struct device_attribute *attr,
da6e162d 1284 const char *buf, size_t len)
00942d1a
JH
1285{
1286 struct rc_dev *dev = to_rc_dev(device);
1287 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1288 struct rc_scancode_filter new_filter, *filter;
00942d1a
JH
1289 int ret;
1290 unsigned long val;
23c843b5 1291 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
00942d1a 1292
00942d1a
JH
1293 ret = kstrtoul(buf, 0, &val);
1294 if (ret < 0)
1295 return ret;
1296
da6e162d
DH
1297 if (fattr->type == RC_FILTER_NORMAL) {
1298 set_filter = dev->s_filter;
c5540fbb 1299 filter = &dev->scancode_filter;
da6e162d
DH
1300 } else {
1301 set_filter = dev->s_wakeup_filter;
c5540fbb 1302 filter = &dev->scancode_wakeup_filter;
da6e162d
DH
1303 }
1304
99b0f3c9
DH
1305 if (!set_filter)
1306 return -EINVAL;
00942d1a
JH
1307
1308 mutex_lock(&dev->lock);
1309
da6e162d 1310 new_filter = *filter;
00942d1a 1311 if (fattr->mask)
da6e162d 1312 new_filter.mask = val;
00942d1a 1313 else
da6e162d 1314 new_filter.data = val;
23c843b5 1315
0751d33c 1316 if (fattr->type == RC_FILTER_WAKEUP) {
b590c0bf
SY
1317 /*
1318 * Refuse to set a filter unless a protocol is enabled
1319 * and the filter is valid for that protocol
1320 */
6d741bfe 1321 if (dev->wakeup_protocol != RC_PROTO_UNKNOWN)
f423ccc1 1322 ret = rc_validate_filter(dev, &new_filter);
b590c0bf 1323 else
0751d33c 1324 ret = -EINVAL;
b590c0bf
SY
1325
1326 if (ret != 0)
0751d33c 1327 goto unlock;
0751d33c
SY
1328 }
1329
1330 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1331 val) {
6bea25af
JH
1332 /* refuse to set a filter unless a protocol is enabled */
1333 ret = -EINVAL;
1334 goto unlock;
1335 }
23c843b5 1336
da6e162d 1337 ret = set_filter(dev, &new_filter);
99b0f3c9
DH
1338 if (ret < 0)
1339 goto unlock;
00942d1a 1340
da6e162d 1341 *filter = new_filter;
00942d1a
JH
1342
1343unlock:
1344 mutex_unlock(&dev->lock);
da6e162d 1345 return (ret < 0) ? ret : len;
00942d1a
JH
1346}
1347
0751d33c
SY
1348/**
1349 * show_wakeup_protocols() - shows the wakeup IR protocol
1350 * @device: the device descriptor
1351 * @mattr: the device attribute struct
1352 * @buf: a pointer to the output buffer
1353 *
1354 * This routine is a callback routine for input read the IR protocol type(s).
1355 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1356 * It returns the protocol names of supported protocols.
1357 * The enabled protocols are printed in brackets.
1358 *
18726a34
DH
1359 * dev->lock is taken to guard against races between
1360 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1361 */
1362static ssize_t show_wakeup_protocols(struct device *device,
1363 struct device_attribute *mattr,
1364 char *buf)
1365{
1366 struct rc_dev *dev = to_rc_dev(device);
1367 u64 allowed;
6d741bfe 1368 enum rc_proto enabled;
0751d33c
SY
1369 char *tmp = buf;
1370 int i;
1371
0751d33c
SY
1372 mutex_lock(&dev->lock);
1373
1374 allowed = dev->allowed_wakeup_protocols;
1375 enabled = dev->wakeup_protocol;
1376
1377 mutex_unlock(&dev->lock);
1378
1379 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1380 __func__, (long long)allowed, enabled);
1381
d57ea877 1382 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
0751d33c
SY
1383 if (allowed & (1ULL << i)) {
1384 if (i == enabled)
d57ea877 1385 tmp += sprintf(tmp, "[%s] ", protocols[i].name);
0751d33c 1386 else
d57ea877 1387 tmp += sprintf(tmp, "%s ", protocols[i].name);
0751d33c
SY
1388 }
1389 }
1390
1391 if (tmp != buf)
1392 tmp--;
1393 *tmp = '\n';
1394
1395 return tmp + 1 - buf;
1396}
1397
1398/**
1399 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1400 * @device: the device descriptor
1401 * @mattr: the device attribute struct
1402 * @buf: a pointer to the input buffer
1403 * @len: length of the input buffer
1404 *
1405 * This routine is for changing the IR protocol type.
1406 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1407 * Returns @len on success or a negative error code.
1408 *
18726a34
DH
1409 * dev->lock is taken to guard against races between
1410 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1411 */
1412static ssize_t store_wakeup_protocols(struct device *device,
1413 struct device_attribute *mattr,
1414 const char *buf, size_t len)
1415{
1416 struct rc_dev *dev = to_rc_dev(device);
6d741bfe 1417 enum rc_proto protocol;
0751d33c
SY
1418 ssize_t rc;
1419 u64 allowed;
1420 int i;
1421
0751d33c
SY
1422 mutex_lock(&dev->lock);
1423
1424 allowed = dev->allowed_wakeup_protocols;
1425
1426 if (sysfs_streq(buf, "none")) {
6d741bfe 1427 protocol = RC_PROTO_UNKNOWN;
0751d33c 1428 } else {
d57ea877 1429 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
0751d33c 1430 if ((allowed & (1ULL << i)) &&
d57ea877 1431 sysfs_streq(buf, protocols[i].name)) {
0751d33c
SY
1432 protocol = i;
1433 break;
1434 }
1435 }
1436
d57ea877 1437 if (i == ARRAY_SIZE(protocols)) {
0751d33c
SY
1438 rc = -EINVAL;
1439 goto out;
1440 }
f423ccc1
JH
1441
1442 if (dev->encode_wakeup) {
1443 u64 mask = 1ULL << protocol;
1444
1445 ir_raw_load_modules(&mask);
1446 if (!mask) {
1447 rc = -EINVAL;
1448 goto out;
1449 }
1450 }
0751d33c
SY
1451 }
1452
1453 if (dev->wakeup_protocol != protocol) {
1454 dev->wakeup_protocol = protocol;
1455 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1456
6d741bfe 1457 if (protocol == RC_PROTO_RC6_MCE)
0751d33c
SY
1458 dev->scancode_wakeup_filter.data = 0x800f0000;
1459 else
1460 dev->scancode_wakeup_filter.data = 0;
1461 dev->scancode_wakeup_filter.mask = 0;
1462
1463 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1464 if (rc == 0)
1465 rc = len;
1466 } else {
1467 rc = len;
1468 }
1469
1470out:
1471 mutex_unlock(&dev->lock);
1472 return rc;
1473}
1474
d8b4b582
DH
1475static void rc_dev_release(struct device *device)
1476{
47cae1e1
MK
1477 struct rc_dev *dev = to_rc_dev(device);
1478
1479 kfree(dev);
d8b4b582
DH
1480}
1481
bc2a6c57
MCC
1482#define ADD_HOTPLUG_VAR(fmt, val...) \
1483 do { \
1484 int err = add_uevent_var(env, fmt, val); \
1485 if (err) \
1486 return err; \
1487 } while (0)
1488
1489static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1490{
d8b4b582 1491 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57 1492
b088ba65
MCC
1493 if (dev->rc_map.name)
1494 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
d8b4b582
DH
1495 if (dev->driver_name)
1496 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
b9f407e3
SY
1497 if (dev->device_name)
1498 ADD_HOTPLUG_VAR("DEV_NAME=%s", dev->device_name);
bc2a6c57
MCC
1499
1500 return 0;
1501}
1502
1503/*
1504 * Static device attribute struct with the sysfs attributes for IR's
1505 */
6d75db30
SY
1506static struct device_attribute dev_attr_ro_protocols =
1507__ATTR(protocols, 0444, show_protocols, NULL);
1508static struct device_attribute dev_attr_rw_protocols =
1509__ATTR(protocols, 0644, show_protocols, store_protocols);
0751d33c
SY
1510static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1511 store_wakeup_protocols);
00942d1a
JH
1512static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1513 show_filter, store_filter, RC_FILTER_NORMAL, false);
1514static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1515 show_filter, store_filter, RC_FILTER_NORMAL, true);
1516static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1517 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1518static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1519 show_filter, store_filter, RC_FILTER_WAKEUP, true);
bc2a6c57 1520
6d75db30
SY
1521static struct attribute *rc_dev_rw_protocol_attrs[] = {
1522 &dev_attr_rw_protocols.attr,
99b0f3c9
DH
1523 NULL,
1524};
1525
6d75db30
SY
1526static const struct attribute_group rc_dev_rw_protocol_attr_grp = {
1527 .attrs = rc_dev_rw_protocol_attrs,
1528};
1529
1530static struct attribute *rc_dev_ro_protocol_attrs[] = {
1531 &dev_attr_ro_protocols.attr,
1532 NULL,
1533};
1534
1535static const struct attribute_group rc_dev_ro_protocol_attr_grp = {
1536 .attrs = rc_dev_ro_protocol_attrs,
99b0f3c9
DH
1537};
1538
99b0f3c9 1539static struct attribute *rc_dev_filter_attrs[] = {
00942d1a
JH
1540 &dev_attr_filter.attr.attr,
1541 &dev_attr_filter_mask.attr.attr,
bc2a6c57
MCC
1542 NULL,
1543};
1544
db68102c 1545static const struct attribute_group rc_dev_filter_attr_grp = {
99b0f3c9 1546 .attrs = rc_dev_filter_attrs,
bc2a6c57
MCC
1547};
1548
99b0f3c9
DH
1549static struct attribute *rc_dev_wakeup_filter_attrs[] = {
1550 &dev_attr_wakeup_filter.attr.attr,
1551 &dev_attr_wakeup_filter_mask.attr.attr,
0751d33c 1552 &dev_attr_wakeup_protocols.attr,
99b0f3c9
DH
1553 NULL,
1554};
1555
db68102c 1556static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
99b0f3c9 1557 .attrs = rc_dev_wakeup_filter_attrs,
bc2a6c57
MCC
1558};
1559
f03f02f9 1560static const struct device_type rc_dev_type = {
d8b4b582 1561 .release = rc_dev_release,
bc2a6c57
MCC
1562 .uevent = rc_dev_uevent,
1563};
1564
0f7499fd 1565struct rc_dev *rc_allocate_device(enum rc_driver_type type)
bc2a6c57 1566{
d8b4b582 1567 struct rc_dev *dev;
bc2a6c57 1568
d8b4b582
DH
1569 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1570 if (!dev)
1571 return NULL;
1572
d34aee10
AS
1573 if (type != RC_DRIVER_IR_RAW_TX) {
1574 dev->input_dev = input_allocate_device();
1575 if (!dev->input_dev) {
1576 kfree(dev);
1577 return NULL;
1578 }
1579
1580 dev->input_dev->getkeycode = ir_getkeycode;
1581 dev->input_dev->setkeycode = ir_setkeycode;
1582 input_set_drvdata(dev->input_dev, dev);
d8b4b582 1583
b17ec78a 1584 timer_setup(&dev->timer_keyup, ir_timer_keyup, 0);
d8b4b582 1585
d34aee10
AS
1586 spin_lock_init(&dev->rc_map.lock);
1587 spin_lock_init(&dev->keylock);
1588 }
08aeb7c9 1589 mutex_init(&dev->lock);
bc2a6c57 1590
d8b4b582 1591 dev->dev.type = &rc_dev_type;
40fc5325 1592 dev->dev.class = &rc_class;
d8b4b582
DH
1593 device_initialize(&dev->dev);
1594
0f7499fd
AS
1595 dev->driver_type = type;
1596
d8b4b582
DH
1597 __module_get(THIS_MODULE);
1598 return dev;
1599}
1600EXPORT_SYMBOL_GPL(rc_allocate_device);
1601
1602void rc_free_device(struct rc_dev *dev)
bc2a6c57 1603{
b05681b9
MCC
1604 if (!dev)
1605 return;
1606
3dd94f00 1607 input_free_device(dev->input_dev);
b05681b9
MCC
1608
1609 put_device(&dev->dev);
1610
47cae1e1
MK
1611 /* kfree(dev) will be called by the callback function
1612 rc_dev_release() */
1613
b05681b9 1614 module_put(THIS_MODULE);
d8b4b582
DH
1615}
1616EXPORT_SYMBOL_GPL(rc_free_device);
1617
ddbf7d5a
HK
1618static void devm_rc_alloc_release(struct device *dev, void *res)
1619{
1620 rc_free_device(*(struct rc_dev **)res);
1621}
1622
0f7499fd
AS
1623struct rc_dev *devm_rc_allocate_device(struct device *dev,
1624 enum rc_driver_type type)
ddbf7d5a
HK
1625{
1626 struct rc_dev **dr, *rc;
1627
1628 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1629 if (!dr)
1630 return NULL;
1631
0f7499fd 1632 rc = rc_allocate_device(type);
ddbf7d5a
HK
1633 if (!rc) {
1634 devres_free(dr);
1635 return NULL;
1636 }
1637
1638 rc->dev.parent = dev;
1639 rc->managed_alloc = true;
1640 *dr = rc;
1641 devres_add(dev, dr);
1642
1643 return rc;
1644}
1645EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1646
f56928ab 1647static int rc_prepare_rx_device(struct rc_dev *dev)
d8b4b582 1648{
fcb13097 1649 int rc;
7ff2c2bc 1650 struct rc_map *rc_map;
6d741bfe 1651 u64 rc_proto;
bc2a6c57 1652
7ff2c2bc 1653 if (!dev->map_name)
d8b4b582 1654 return -EINVAL;
bc2a6c57 1655
d100e659 1656 rc_map = rc_map_get(dev->map_name);
b088ba65 1657 if (!rc_map)
d100e659 1658 rc_map = rc_map_get(RC_MAP_EMPTY);
b088ba65 1659 if (!rc_map || !rc_map->scan || rc_map->size == 0)
d8b4b582
DH
1660 return -EINVAL;
1661
7ff2c2bc
AS
1662 rc = ir_setkeytable(dev, rc_map);
1663 if (rc)
1664 return rc;
1665
6d741bfe 1666 rc_proto = BIT_ULL(rc_map->rc_proto);
7ff2c2bc 1667
831c4c81
SY
1668 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1669 dev->enabled_protocols = dev->allowed_protocols;
1670
41380868 1671 if (dev->change_protocol) {
6d741bfe 1672 rc = dev->change_protocol(dev, &rc_proto);
7ff2c2bc
AS
1673 if (rc < 0)
1674 goto out_table;
6d741bfe 1675 dev->enabled_protocols = rc_proto;
7ff2c2bc
AS
1676 }
1677
41380868 1678 if (dev->driver_type == RC_DRIVER_IR_RAW)
6d741bfe 1679 ir_raw_load_modules(&rc_proto);
41380868 1680
d8b4b582
DH
1681 set_bit(EV_KEY, dev->input_dev->evbit);
1682 set_bit(EV_REP, dev->input_dev->evbit);
1683 set_bit(EV_MSC, dev->input_dev->evbit);
1684 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1685 if (dev->open)
1686 dev->input_dev->open = ir_open;
1687 if (dev->close)
1688 dev->input_dev->close = ir_close;
1689
b2aceb73
DH
1690 dev->input_dev->dev.parent = &dev->dev;
1691 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1692 dev->input_dev->phys = dev->input_phys;
518f4b26 1693 dev->input_dev->name = dev->device_name;
b2aceb73 1694
f56928ab
DH
1695 return 0;
1696
1697out_table:
1698 ir_free_table(&dev->rc_map);
1699
1700 return rc;
1701}
1702
1703static int rc_setup_rx_device(struct rc_dev *dev)
1704{
1705 int rc;
1706
b2aceb73
DH
1707 /* rc_open will be called here */
1708 rc = input_register_device(dev->input_dev);
1709 if (rc)
f56928ab 1710 return rc;
b2aceb73 1711
7ff2c2bc
AS
1712 /*
1713 * Default delay of 250ms is too short for some protocols, especially
1714 * since the timeout is currently set to 250ms. Increase it to 500ms,
1715 * to avoid wrong repetition of the keycodes. Note that this must be
1716 * set after the call to input_register_device().
1717 */
1718 dev->input_dev->rep[REP_DELAY] = 500;
1719
1720 /*
1721 * As a repeat event on protocols like RC-5 and NEC take as long as
1722 * 110/114ms, using 33ms as a repeat period is not the right thing
1723 * to do.
1724 */
1725 dev->input_dev->rep[REP_PERIOD] = 125;
1726
7ff2c2bc 1727 return 0;
7ff2c2bc
AS
1728}
1729
1730static void rc_free_rx_device(struct rc_dev *dev)
1731{
f56928ab 1732 if (!dev)
7ff2c2bc
AS
1733 return;
1734
f56928ab
DH
1735 if (dev->input_dev) {
1736 input_unregister_device(dev->input_dev);
1737 dev->input_dev = NULL;
1738 }
7ff2c2bc 1739
f56928ab 1740 ir_free_table(&dev->rc_map);
7ff2c2bc
AS
1741}
1742
1743int rc_register_device(struct rc_dev *dev)
1744{
7ff2c2bc
AS
1745 const char *path;
1746 int attr = 0;
1747 int minor;
1748 int rc;
1749
1750 if (!dev)
1751 return -EINVAL;
1752
fcb13097
DH
1753 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1754 if (minor < 0)
1755 return minor;
1756
1757 dev->minor = minor;
1758 dev_set_name(&dev->dev, "rc%u", dev->minor);
1759 dev_set_drvdata(&dev->dev, dev);
587d1b06 1760
99b0f3c9 1761 dev->dev.groups = dev->sysfs_groups;
6d75db30
SY
1762 if (dev->driver_type == RC_DRIVER_SCANCODE && !dev->change_protocol)
1763 dev->sysfs_groups[attr++] = &rc_dev_ro_protocol_attr_grp;
1764 else if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1765 dev->sysfs_groups[attr++] = &rc_dev_rw_protocol_attr_grp;
99b0f3c9 1766 if (dev->s_filter)
120703f9 1767 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
99b0f3c9
DH
1768 if (dev->s_wakeup_filter)
1769 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
99b0f3c9
DH
1770 dev->sysfs_groups[attr++] = NULL;
1771
a60d64b1 1772 if (dev->driver_type == RC_DRIVER_IR_RAW) {
f56928ab
DH
1773 rc = ir_raw_event_prepare(dev);
1774 if (rc < 0)
1775 goto out_minor;
1776 }
1777
1778 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1779 rc = rc_prepare_rx_device(dev);
1780 if (rc)
1781 goto out_raw;
1782 }
1783
d8b4b582
DH
1784 rc = device_add(&dev->dev);
1785 if (rc)
f56928ab 1786 goto out_rx_free;
bc2a6c57 1787
d8b4b582 1788 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
4dc0e908 1789 dev_info(&dev->dev, "%s as %s\n",
518f4b26 1790 dev->device_name ?: "Unspecified device", path ?: "N/A");
bc2a6c57
MCC
1791 kfree(path);
1792
f56928ab
DH
1793 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1794 rc = rc_setup_rx_device(dev);
1795 if (rc)
1796 goto out_dev;
1797 }
1798
a60d64b1
SY
1799 /* Ensure that the lirc kfifo is setup before we start the thread */
1800 if (dev->driver_type != RC_DRIVER_SCANCODE) {
1801 rc = ir_lirc_register(dev);
d8b4b582 1802 if (rc < 0)
f56928ab 1803 goto out_rx;
d8b4b582
DH
1804 }
1805
a60d64b1
SY
1806 if (dev->driver_type == RC_DRIVER_IR_RAW) {
1807 rc = ir_raw_event_register(dev);
1808 if (rc < 0)
1809 goto out_lirc;
1810 }
1811
7ff2c2bc 1812 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
fcb13097 1813 dev->minor,
7ff2c2bc 1814 dev->driver_name ? dev->driver_name : "unknown");
d8b4b582 1815
bc2a6c57 1816 return 0;
d8b4b582 1817
a60d64b1
SY
1818out_lirc:
1819 if (dev->driver_type != RC_DRIVER_SCANCODE)
1820 ir_lirc_unregister(dev);
f56928ab
DH
1821out_rx:
1822 rc_free_rx_device(dev);
d8b4b582
DH
1823out_dev:
1824 device_del(&dev->dev);
f56928ab
DH
1825out_rx_free:
1826 ir_free_table(&dev->rc_map);
1827out_raw:
1828 ir_raw_event_free(dev);
1829out_minor:
fcb13097 1830 ida_simple_remove(&rc_ida, minor);
d8b4b582 1831 return rc;
bc2a6c57 1832}
d8b4b582 1833EXPORT_SYMBOL_GPL(rc_register_device);
bc2a6c57 1834
ddbf7d5a
HK
1835static void devm_rc_release(struct device *dev, void *res)
1836{
1837 rc_unregister_device(*(struct rc_dev **)res);
1838}
1839
1840int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1841{
1842 struct rc_dev **dr;
1843 int ret;
1844
1845 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1846 if (!dr)
1847 return -ENOMEM;
1848
1849 ret = rc_register_device(dev);
1850 if (ret) {
1851 devres_free(dr);
1852 return ret;
1853 }
1854
1855 *dr = dev;
1856 devres_add(parent, dr);
1857
1858 return 0;
1859}
1860EXPORT_SYMBOL_GPL(devm_rc_register_device);
1861
d8b4b582 1862void rc_unregister_device(struct rc_dev *dev)
bc2a6c57 1863{
d8b4b582
DH
1864 if (!dev)
1865 return;
bc2a6c57 1866
d8b4b582 1867 del_timer_sync(&dev->timer_keyup);
bc2a6c57 1868
d8b4b582
DH
1869 if (dev->driver_type == RC_DRIVER_IR_RAW)
1870 ir_raw_event_unregister(dev);
1871
7ff2c2bc 1872 rc_free_rx_device(dev);
d8b4b582 1873
a60d64b1
SY
1874 if (dev->driver_type != RC_DRIVER_SCANCODE)
1875 ir_lirc_unregister(dev);
1876
b05681b9 1877 device_del(&dev->dev);
d8b4b582 1878
fcb13097
DH
1879 ida_simple_remove(&rc_ida, dev->minor);
1880
ddbf7d5a
HK
1881 if (!dev->managed_alloc)
1882 rc_free_device(dev);
bc2a6c57 1883}
b05681b9 1884
d8b4b582 1885EXPORT_SYMBOL_GPL(rc_unregister_device);
bc2a6c57
MCC
1886
1887/*
1888 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1889 */
1890
6bda9644 1891static int __init rc_core_init(void)
bc2a6c57 1892{
40fc5325 1893 int rc = class_register(&rc_class);
bc2a6c57 1894 if (rc) {
d3d96820 1895 pr_err("rc_core: unable to register rc class\n");
bc2a6c57
MCC
1896 return rc;
1897 }
1898
a60d64b1
SY
1899 rc = lirc_dev_init();
1900 if (rc) {
1901 pr_err("rc_core: unable to init lirc\n");
1902 class_unregister(&rc_class);
1903 return 0;
1904 }
1905
153a60bb 1906 led_trigger_register_simple("rc-feedback", &led_feedback);
d100e659 1907 rc_map_register(&empty_map);
bc2a6c57
MCC
1908
1909 return 0;
1910}
1911
6bda9644 1912static void __exit rc_core_exit(void)
bc2a6c57 1913{
a60d64b1 1914 lirc_dev_exit();
40fc5325 1915 class_unregister(&rc_class);
153a60bb 1916 led_trigger_unregister_simple(led_feedback);
d100e659 1917 rc_map_unregister(&empty_map);
bc2a6c57
MCC
1918}
1919
e76d4ce4 1920subsys_initcall(rc_core_init);
6bda9644 1921module_exit(rc_core_exit);
bc2a6c57 1922
6bda9644
MCC
1923int rc_core_debug; /* ir_debug level (0,1,2) */
1924EXPORT_SYMBOL_GPL(rc_core_debug);
1925module_param_named(debug, rc_core_debug, int, 0644);
446e4a64 1926
37e59f87 1927MODULE_AUTHOR("Mauro Carvalho Chehab");
20835280 1928MODULE_LICENSE("GPL v2");