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