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