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