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