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