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