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