media: rc: saa7134: raw decoder can support any protocol
[linux-2.6-block.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",
518f4b26 533 dev->device_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",
518f4b26 666 dev->device_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{
2168b416 736 static const u32 masks[] = {
b590c0bf
SY
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 759
2168b416
SY
760 if (protocol >= ARRAY_SIZE(masks))
761 return -EINVAL;
762
b590c0bf
SY
763 switch (protocol) {
764 case RC_TYPE_NECX:
765 if ((((s >> 16) ^ ~(s >> 8)) & 0xff) == 0)
766 return -EINVAL;
767 break;
768 case RC_TYPE_NEC32:
769 if ((((s >> 24) ^ ~(s >> 16)) & 0xff) == 0)
770 return -EINVAL;
771 break;
772 case RC_TYPE_RC6_MCE:
773 if ((s & 0xffff0000) != 0x800f0000)
774 return -EINVAL;
775 break;
776 case RC_TYPE_RC6_6A_32:
777 if ((s & 0xffff0000) == 0x800f0000)
778 return -EINVAL;
779 break;
780 default:
781 break;
782 }
783
784 filter->data &= masks[protocol];
785 filter->mask &= masks[protocol];
786
f423ccc1
JH
787 /*
788 * If we have to raw encode the IR for wakeup, we cannot have a mask
789 */
790 if (dev->encode_wakeup &&
791 filter->mask != 0 && filter->mask != masks[protocol])
792 return -EINVAL;
793
b590c0bf
SY
794 return 0;
795}
796
8b2ff320
SK
797int rc_open(struct rc_dev *rdev)
798{
799 int rval = 0;
800
801 if (!rdev)
802 return -EINVAL;
803
804 mutex_lock(&rdev->lock);
c73bbaa4 805
f02dcdd1 806 if (!rdev->users++ && rdev->open != NULL)
8b2ff320
SK
807 rval = rdev->open(rdev);
808
809 if (rval)
810 rdev->users--;
811
812 mutex_unlock(&rdev->lock);
813
814 return rval;
815}
816EXPORT_SYMBOL_GPL(rc_open);
817
d8b4b582 818static int ir_open(struct input_dev *idev)
ef53a115 819{
d8b4b582 820 struct rc_dev *rdev = input_get_drvdata(idev);
75543cce 821
8b2ff320
SK
822 return rc_open(rdev);
823}
824
825void rc_close(struct rc_dev *rdev)
826{
827 if (rdev) {
828 mutex_lock(&rdev->lock);
829
81b7d14e 830 if (!--rdev->users && rdev->close != NULL)
8b2ff320
SK
831 rdev->close(rdev);
832
833 mutex_unlock(&rdev->lock);
834 }
ef53a115 835}
8b2ff320 836EXPORT_SYMBOL_GPL(rc_close);
d4b778d3 837
d8b4b582 838static void ir_close(struct input_dev *idev)
f6fc5049 839{
d8b4b582 840 struct rc_dev *rdev = input_get_drvdata(idev);
8b2ff320 841 rc_close(rdev);
f6fc5049 842}
f6fc5049 843
bc2a6c57 844/* class for /sys/class/rc */
40fc5325 845static char *rc_devnode(struct device *dev, umode_t *mode)
bc2a6c57
MCC
846{
847 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
848}
849
40fc5325 850static struct class rc_class = {
bc2a6c57 851 .name = "rc",
40fc5325 852 .devnode = rc_devnode,
bc2a6c57
MCC
853};
854
c003ab1b
DH
855/*
856 * These are the protocol textual descriptions that are
857 * used by the sysfs protocols file. Note that the order
858 * of the entries is relevant.
859 */
53df8777 860static const struct {
bc2a6c57 861 u64 type;
53df8777 862 const char *name;
9f0bf366 863 const char *module_name;
bc2a6c57 864} proto_names[] = {
9f0bf366
HK
865 { RC_BIT_NONE, "none", NULL },
866 { RC_BIT_OTHER, "other", NULL },
867 { RC_BIT_UNKNOWN, "unknown", NULL },
c003ab1b 868 { RC_BIT_RC5 |
0fcd3f0a 869 RC_BIT_RC5X_20, "rc-5", "ir-rc5-decoder" },
2ceeca04
SY
870 { RC_BIT_NEC |
871 RC_BIT_NECX |
872 RC_BIT_NEC32, "nec", "ir-nec-decoder" },
c003ab1b
DH
873 { RC_BIT_RC6_0 |
874 RC_BIT_RC6_6A_20 |
875 RC_BIT_RC6_6A_24 |
876 RC_BIT_RC6_6A_32 |
9f0bf366
HK
877 RC_BIT_RC6_MCE, "rc-6", "ir-rc6-decoder" },
878 { RC_BIT_JVC, "jvc", "ir-jvc-decoder" },
c003ab1b
DH
879 { RC_BIT_SONY12 |
880 RC_BIT_SONY15 |
9f0bf366
HK
881 RC_BIT_SONY20, "sony", "ir-sony-decoder" },
882 { RC_BIT_RC5_SZ, "rc-5-sz", "ir-rc5-decoder" },
883 { RC_BIT_SANYO, "sanyo", "ir-sanyo-decoder" },
884 { RC_BIT_SHARP, "sharp", "ir-sharp-decoder" },
b73bc16d
SY
885 { RC_BIT_MCIR2_KBD |
886 RC_BIT_MCIR2_MSE, "mce_kbd", "ir-mce_kbd-decoder" },
9f0bf366 887 { RC_BIT_XMP, "xmp", "ir-xmp-decoder" },
ff42c8aa 888 { RC_BIT_CEC, "cec", NULL },
bc2a6c57
MCC
889};
890
bc2a6c57 891/**
ab88c66d
JH
892 * struct rc_filter_attribute - Device attribute relating to a filter type.
893 * @attr: Device attribute.
894 * @type: Filter type.
895 * @mask: false for filter value, true for filter mask.
896 */
897struct rc_filter_attribute {
898 struct device_attribute attr;
899 enum rc_filter_type type;
900 bool mask;
901};
902#define to_rc_filter_attr(a) container_of(a, struct rc_filter_attribute, attr)
903
ab88c66d
JH
904#define RC_FILTER_ATTR(_name, _mode, _show, _store, _type, _mask) \
905 struct rc_filter_attribute dev_attr_##_name = { \
906 .attr = __ATTR(_name, _mode, _show, _store), \
907 .type = (_type), \
908 .mask = (_mask), \
909 }
910
dd6ff6a0
DH
911static bool lirc_is_present(void)
912{
913#if defined(CONFIG_LIRC_MODULE)
914 struct module *lirc;
915
916 mutex_lock(&module_mutex);
917 lirc = find_module("lirc_dev");
918 mutex_unlock(&module_mutex);
919
920 return lirc ? true : false;
921#elif defined(CONFIG_LIRC)
922 return true;
923#else
924 return false;
925#endif
926}
927
ab88c66d 928/**
0751d33c 929 * show_protocols() - shows the current IR protocol(s)
d8b4b582 930 * @device: the device descriptor
da6e162d 931 * @mattr: the device attribute struct
bc2a6c57
MCC
932 * @buf: a pointer to the output buffer
933 *
934 * This routine is a callback routine for input read the IR protocol type(s).
0751d33c 935 * it is trigged by reading /sys/class/rc/rc?/protocols.
bc2a6c57
MCC
936 * It returns the protocol names of supported protocols.
937 * Enabled protocols are printed in brackets.
08aeb7c9 938 *
18726a34
DH
939 * dev->lock is taken to guard against races between
940 * store_protocols and show_protocols.
bc2a6c57 941 */
d8b4b582 942static ssize_t show_protocols(struct device *device,
bc2a6c57
MCC
943 struct device_attribute *mattr, char *buf)
944{
d8b4b582 945 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57
MCC
946 u64 allowed, enabled;
947 char *tmp = buf;
948 int i;
949
08aeb7c9
JW
950 mutex_lock(&dev->lock);
951
0751d33c
SY
952 enabled = dev->enabled_protocols;
953 allowed = dev->allowed_protocols;
954 if (dev->raw && !allowed)
955 allowed = ir_raw_get_allowed_protocols();
bc2a6c57 956
da6e162d
DH
957 mutex_unlock(&dev->lock);
958
959 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n",
960 __func__, (long long)allowed, (long long)enabled);
bc2a6c57
MCC
961
962 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
963 if (allowed & enabled & proto_names[i].type)
964 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
965 else if (allowed & proto_names[i].type)
966 tmp += sprintf(tmp, "%s ", proto_names[i].name);
c003ab1b
DH
967
968 if (allowed & proto_names[i].type)
969 allowed &= ~proto_names[i].type;
bc2a6c57
MCC
970 }
971
dd6ff6a0 972 if (dev->driver_type == RC_DRIVER_IR_RAW && lirc_is_present())
275ddb40
DH
973 tmp += sprintf(tmp, "[lirc] ");
974
bc2a6c57
MCC
975 if (tmp != buf)
976 tmp--;
977 *tmp = '\n';
08aeb7c9 978
bc2a6c57
MCC
979 return tmp + 1 - buf;
980}
981
982/**
da6e162d
DH
983 * parse_protocol_change() - parses a protocol change request
984 * @protocols: pointer to the bitmask of current protocols
985 * @buf: pointer to the buffer with a list of changes
bc2a6c57 986 *
da6e162d
DH
987 * Writing "+proto" will add a protocol to the protocol mask.
988 * Writing "-proto" will remove a protocol from protocol mask.
bc2a6c57
MCC
989 * Writing "proto" will enable only "proto".
990 * Writing "none" will disable all protocols.
da6e162d 991 * Returns the number of changes performed or a negative error code.
bc2a6c57 992 */
da6e162d 993static int parse_protocol_change(u64 *protocols, const char *buf)
bc2a6c57 994{
bc2a6c57 995 const char *tmp;
da6e162d
DH
996 unsigned count = 0;
997 bool enable, disable;
bc2a6c57 998 u64 mask;
da6e162d 999 int i;
bc2a6c57 1000
da6e162d 1001 while ((tmp = strsep((char **)&buf, " \n")) != NULL) {
bc2a6c57
MCC
1002 if (!*tmp)
1003 break;
1004
1005 if (*tmp == '+') {
1006 enable = true;
1007 disable = false;
1008 tmp++;
1009 } else if (*tmp == '-') {
1010 enable = false;
1011 disable = true;
1012 tmp++;
1013 } else {
1014 enable = false;
1015 disable = false;
1016 }
1017
c003ab1b
DH
1018 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1019 if (!strcasecmp(tmp, proto_names[i].name)) {
1020 mask = proto_names[i].type;
1021 break;
bc2a6c57 1022 }
bc2a6c57
MCC
1023 }
1024
c003ab1b 1025 if (i == ARRAY_SIZE(proto_names)) {
275ddb40
DH
1026 if (!strcasecmp(tmp, "lirc"))
1027 mask = 0;
1028 else {
1029 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1030 return -EINVAL;
1031 }
c003ab1b
DH
1032 }
1033
1034 count++;
1035
bc2a6c57 1036 if (enable)
da6e162d 1037 *protocols |= mask;
bc2a6c57 1038 else if (disable)
da6e162d 1039 *protocols &= ~mask;
bc2a6c57 1040 else
da6e162d 1041 *protocols = mask;
bc2a6c57
MCC
1042 }
1043
1044 if (!count) {
1045 IR_dprintk(1, "Protocol not specified\n");
da6e162d
DH
1046 return -EINVAL;
1047 }
1048
1049 return count;
1050}
1051
9f0bf366 1052static void ir_raw_load_modules(u64 *protocols)
9f0bf366
HK
1053{
1054 u64 available;
1055 int i, ret;
1056
1057 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
1058 if (proto_names[i].type == RC_BIT_NONE ||
1059 proto_names[i].type & (RC_BIT_OTHER | RC_BIT_UNKNOWN))
1060 continue;
1061
1062 available = ir_raw_get_allowed_protocols();
1063 if (!(*protocols & proto_names[i].type & ~available))
1064 continue;
1065
1066 if (!proto_names[i].module_name) {
1067 pr_err("Can't enable IR protocol %s\n",
1068 proto_names[i].name);
1069 *protocols &= ~proto_names[i].type;
1070 continue;
1071 }
1072
1073 ret = request_module("%s", proto_names[i].module_name);
1074 if (ret < 0) {
1075 pr_err("Couldn't load IR protocol module %s\n",
1076 proto_names[i].module_name);
1077 *protocols &= ~proto_names[i].type;
1078 continue;
1079 }
1080 msleep(20);
1081 available = ir_raw_get_allowed_protocols();
1082 if (!(*protocols & proto_names[i].type & ~available))
1083 continue;
1084
8caebcdc 1085 pr_err("Loaded IR protocol module %s, but protocol %s still not available\n",
9f0bf366
HK
1086 proto_names[i].module_name,
1087 proto_names[i].name);
1088 *protocols &= ~proto_names[i].type;
1089 }
1090}
1091
da6e162d
DH
1092/**
1093 * store_protocols() - changes the current/wakeup IR protocol(s)
1094 * @device: the device descriptor
1095 * @mattr: the device attribute struct
1096 * @buf: a pointer to the input buffer
1097 * @len: length of the input buffer
1098 *
1099 * This routine is for changing the IR protocol type.
1100 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols.
1101 * See parse_protocol_change() for the valid commands.
1102 * Returns @len on success or a negative error code.
1103 *
18726a34
DH
1104 * dev->lock is taken to guard against races between
1105 * store_protocols and show_protocols.
da6e162d
DH
1106 */
1107static ssize_t store_protocols(struct device *device,
1108 struct device_attribute *mattr,
1109 const char *buf, size_t len)
1110{
1111 struct rc_dev *dev = to_rc_dev(device);
da6e162d 1112 u64 *current_protocols;
da6e162d 1113 struct rc_scancode_filter *filter;
da6e162d
DH
1114 u64 old_protocols, new_protocols;
1115 ssize_t rc;
1116
0751d33c
SY
1117 IR_dprintk(1, "Normal protocol change requested\n");
1118 current_protocols = &dev->enabled_protocols;
1119 filter = &dev->scancode_filter;
da6e162d 1120
0751d33c 1121 if (!dev->change_protocol) {
da6e162d
DH
1122 IR_dprintk(1, "Protocol switching not supported\n");
1123 return -EINVAL;
1124 }
1125
1126 mutex_lock(&dev->lock);
1127
1128 old_protocols = *current_protocols;
1129 new_protocols = old_protocols;
1130 rc = parse_protocol_change(&new_protocols, buf);
1131 if (rc < 0)
1132 goto out;
1133
0751d33c 1134 rc = dev->change_protocol(dev, &new_protocols);
da6e162d
DH
1135 if (rc < 0) {
1136 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1137 (long long)new_protocols);
08aeb7c9 1138 goto out;
bc2a6c57
MCC
1139 }
1140
9f0bf366
HK
1141 if (dev->driver_type == RC_DRIVER_IR_RAW)
1142 ir_raw_load_modules(&new_protocols);
1143
983c5bd2
JH
1144 if (new_protocols != old_protocols) {
1145 *current_protocols = new_protocols;
1146 IR_dprintk(1, "Protocols changed to 0x%llx\n",
1147 (long long)new_protocols);
bc2a6c57
MCC
1148 }
1149
6bea25af 1150 /*
983c5bd2
JH
1151 * If a protocol change was attempted the filter may need updating, even
1152 * if the actual protocol mask hasn't changed (since the driver may have
1153 * cleared the filter).
6bea25af
JH
1154 * Try setting the same filter with the new protocol (if any).
1155 * Fall back to clearing the filter.
1156 */
0751d33c 1157 if (dev->s_filter && filter->mask) {
da6e162d 1158 if (new_protocols)
0751d33c 1159 rc = dev->s_filter(dev, filter);
da6e162d
DH
1160 else
1161 rc = -1;
6bea25af 1162
da6e162d
DH
1163 if (rc < 0) {
1164 filter->data = 0;
1165 filter->mask = 0;
0751d33c 1166 dev->s_filter(dev, filter);
da6e162d 1167 }
6bea25af
JH
1168 }
1169
da6e162d 1170 rc = len;
08aeb7c9
JW
1171
1172out:
1173 mutex_unlock(&dev->lock);
da6e162d 1174 return rc;
bc2a6c57
MCC
1175}
1176
00942d1a
JH
1177/**
1178 * show_filter() - shows the current scancode filter value or mask
1179 * @device: the device descriptor
1180 * @attr: the device attribute struct
1181 * @buf: a pointer to the output buffer
1182 *
1183 * This routine is a callback routine to read a scancode filter value or mask.
1184 * It is trigged by reading /sys/class/rc/rc?/[wakeup_]filter[_mask].
1185 * It prints the current scancode filter value or mask of the appropriate filter
1186 * type in hexadecimal into @buf and returns the size of the buffer.
1187 *
1188 * Bits of the filter value corresponding to set bits in the filter mask are
1189 * compared against input scancodes and non-matching scancodes are discarded.
1190 *
18726a34 1191 * dev->lock is taken to guard against races between
00942d1a
JH
1192 * store_filter and show_filter.
1193 */
1194static ssize_t show_filter(struct device *device,
1195 struct device_attribute *attr,
1196 char *buf)
1197{
1198 struct rc_dev *dev = to_rc_dev(device);
1199 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1200 struct rc_scancode_filter *filter;
00942d1a
JH
1201 u32 val;
1202
c73bbaa4 1203 mutex_lock(&dev->lock);
c73bbaa4 1204
da6e162d 1205 if (fattr->type == RC_FILTER_NORMAL)
c5540fbb 1206 filter = &dev->scancode_filter;
da6e162d 1207 else
c5540fbb 1208 filter = &dev->scancode_wakeup_filter;
da6e162d 1209
da6e162d
DH
1210 if (fattr->mask)
1211 val = filter->mask;
00942d1a 1212 else
da6e162d 1213 val = filter->data;
00942d1a
JH
1214 mutex_unlock(&dev->lock);
1215
1216 return sprintf(buf, "%#x\n", val);
1217}
1218
1219/**
1220 * store_filter() - changes the scancode filter value
1221 * @device: the device descriptor
1222 * @attr: the device attribute struct
1223 * @buf: a pointer to the input buffer
1224 * @len: length of the input buffer
1225 *
1226 * This routine is for changing a scancode filter value or mask.
1227 * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]filter[_mask].
1228 * Returns -EINVAL if an invalid filter value for the current protocol was
1229 * specified or if scancode filtering is not supported by the driver, otherwise
1230 * returns @len.
1231 *
1232 * Bits of the filter value corresponding to set bits in the filter mask are
1233 * compared against input scancodes and non-matching scancodes are discarded.
1234 *
18726a34 1235 * dev->lock is taken to guard against races between
00942d1a
JH
1236 * store_filter and show_filter.
1237 */
1238static ssize_t store_filter(struct device *device,
1239 struct device_attribute *attr,
da6e162d 1240 const char *buf, size_t len)
00942d1a
JH
1241{
1242 struct rc_dev *dev = to_rc_dev(device);
1243 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr);
da6e162d 1244 struct rc_scancode_filter new_filter, *filter;
00942d1a
JH
1245 int ret;
1246 unsigned long val;
23c843b5 1247 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter);
00942d1a 1248
00942d1a
JH
1249 ret = kstrtoul(buf, 0, &val);
1250 if (ret < 0)
1251 return ret;
1252
da6e162d
DH
1253 if (fattr->type == RC_FILTER_NORMAL) {
1254 set_filter = dev->s_filter;
c5540fbb 1255 filter = &dev->scancode_filter;
da6e162d
DH
1256 } else {
1257 set_filter = dev->s_wakeup_filter;
c5540fbb 1258 filter = &dev->scancode_wakeup_filter;
da6e162d
DH
1259 }
1260
99b0f3c9
DH
1261 if (!set_filter)
1262 return -EINVAL;
00942d1a
JH
1263
1264 mutex_lock(&dev->lock);
1265
da6e162d 1266 new_filter = *filter;
00942d1a 1267 if (fattr->mask)
da6e162d 1268 new_filter.mask = val;
00942d1a 1269 else
da6e162d 1270 new_filter.data = val;
23c843b5 1271
0751d33c 1272 if (fattr->type == RC_FILTER_WAKEUP) {
b590c0bf
SY
1273 /*
1274 * Refuse to set a filter unless a protocol is enabled
1275 * and the filter is valid for that protocol
1276 */
1277 if (dev->wakeup_protocol != RC_TYPE_UNKNOWN)
f423ccc1 1278 ret = rc_validate_filter(dev, &new_filter);
b590c0bf 1279 else
0751d33c 1280 ret = -EINVAL;
b590c0bf
SY
1281
1282 if (ret != 0)
0751d33c 1283 goto unlock;
0751d33c
SY
1284 }
1285
1286 if (fattr->type == RC_FILTER_NORMAL && !dev->enabled_protocols &&
1287 val) {
6bea25af
JH
1288 /* refuse to set a filter unless a protocol is enabled */
1289 ret = -EINVAL;
1290 goto unlock;
1291 }
23c843b5 1292
da6e162d 1293 ret = set_filter(dev, &new_filter);
99b0f3c9
DH
1294 if (ret < 0)
1295 goto unlock;
00942d1a 1296
da6e162d 1297 *filter = new_filter;
00942d1a
JH
1298
1299unlock:
1300 mutex_unlock(&dev->lock);
da6e162d 1301 return (ret < 0) ? ret : len;
00942d1a
JH
1302}
1303
0751d33c
SY
1304/*
1305 * This is the list of all variants of all protocols, which is used by
1306 * the wakeup_protocols sysfs entry. In the protocols sysfs entry some
1307 * some protocols are grouped together (e.g. nec = nec + necx + nec32).
1308 *
1309 * For wakeup we need to know the exact protocol variant so the hardware
1310 * can be programmed exactly what to expect.
1311 */
1312static const char * const proto_variant_names[] = {
1313 [RC_TYPE_UNKNOWN] = "unknown",
1314 [RC_TYPE_OTHER] = "other",
1315 [RC_TYPE_RC5] = "rc-5",
1316 [RC_TYPE_RC5X_20] = "rc-5x-20",
1317 [RC_TYPE_RC5_SZ] = "rc-5-sz",
1318 [RC_TYPE_JVC] = "jvc",
1319 [RC_TYPE_SONY12] = "sony-12",
1320 [RC_TYPE_SONY15] = "sony-15",
1321 [RC_TYPE_SONY20] = "sony-20",
1322 [RC_TYPE_NEC] = "nec",
1323 [RC_TYPE_NECX] = "nec-x",
1324 [RC_TYPE_NEC32] = "nec-32",
1325 [RC_TYPE_SANYO] = "sanyo",
b73bc16d
SY
1326 [RC_TYPE_MCIR2_KBD] = "mcir2-kbd",
1327 [RC_TYPE_MCIR2_MSE] = "mcir2-mse",
0751d33c
SY
1328 [RC_TYPE_RC6_0] = "rc-6-0",
1329 [RC_TYPE_RC6_6A_20] = "rc-6-6a-20",
1330 [RC_TYPE_RC6_6A_24] = "rc-6-6a-24",
1331 [RC_TYPE_RC6_6A_32] = "rc-6-6a-32",
1332 [RC_TYPE_RC6_MCE] = "rc-6-mce",
1333 [RC_TYPE_SHARP] = "sharp",
1334 [RC_TYPE_XMP] = "xmp",
1335 [RC_TYPE_CEC] = "cec",
1336};
1337
1338/**
1339 * show_wakeup_protocols() - shows the wakeup IR protocol
1340 * @device: the device descriptor
1341 * @mattr: the device attribute struct
1342 * @buf: a pointer to the output buffer
1343 *
1344 * This routine is a callback routine for input read the IR protocol type(s).
1345 * it is trigged by reading /sys/class/rc/rc?/wakeup_protocols.
1346 * It returns the protocol names of supported protocols.
1347 * The enabled protocols are printed in brackets.
1348 *
18726a34
DH
1349 * dev->lock is taken to guard against races between
1350 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1351 */
1352static ssize_t show_wakeup_protocols(struct device *device,
1353 struct device_attribute *mattr,
1354 char *buf)
1355{
1356 struct rc_dev *dev = to_rc_dev(device);
1357 u64 allowed;
1358 enum rc_type enabled;
1359 char *tmp = buf;
1360 int i;
1361
0751d33c
SY
1362 mutex_lock(&dev->lock);
1363
1364 allowed = dev->allowed_wakeup_protocols;
1365 enabled = dev->wakeup_protocol;
1366
1367 mutex_unlock(&dev->lock);
1368
1369 IR_dprintk(1, "%s: allowed - 0x%llx, enabled - %d\n",
1370 __func__, (long long)allowed, enabled);
1371
1372 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1373 if (allowed & (1ULL << i)) {
1374 if (i == enabled)
1375 tmp += sprintf(tmp, "[%s] ",
1376 proto_variant_names[i]);
1377 else
1378 tmp += sprintf(tmp, "%s ",
1379 proto_variant_names[i]);
1380 }
1381 }
1382
1383 if (tmp != buf)
1384 tmp--;
1385 *tmp = '\n';
1386
1387 return tmp + 1 - buf;
1388}
1389
1390/**
1391 * store_wakeup_protocols() - changes the wakeup IR protocol(s)
1392 * @device: the device descriptor
1393 * @mattr: the device attribute struct
1394 * @buf: a pointer to the input buffer
1395 * @len: length of the input buffer
1396 *
1397 * This routine is for changing the IR protocol type.
1398 * It is trigged by writing to /sys/class/rc/rc?/wakeup_protocols.
1399 * Returns @len on success or a negative error code.
1400 *
18726a34
DH
1401 * dev->lock is taken to guard against races between
1402 * store_wakeup_protocols and show_wakeup_protocols.
0751d33c
SY
1403 */
1404static ssize_t store_wakeup_protocols(struct device *device,
1405 struct device_attribute *mattr,
1406 const char *buf, size_t len)
1407{
1408 struct rc_dev *dev = to_rc_dev(device);
1409 enum rc_type protocol;
1410 ssize_t rc;
1411 u64 allowed;
1412 int i;
1413
0751d33c
SY
1414 mutex_lock(&dev->lock);
1415
1416 allowed = dev->allowed_wakeup_protocols;
1417
1418 if (sysfs_streq(buf, "none")) {
1419 protocol = RC_TYPE_UNKNOWN;
1420 } else {
1421 for (i = 0; i < ARRAY_SIZE(proto_variant_names); i++) {
1422 if ((allowed & (1ULL << i)) &&
1423 sysfs_streq(buf, proto_variant_names[i])) {
1424 protocol = i;
1425 break;
1426 }
1427 }
1428
1429 if (i == ARRAY_SIZE(proto_variant_names)) {
1430 rc = -EINVAL;
1431 goto out;
1432 }
f423ccc1
JH
1433
1434 if (dev->encode_wakeup) {
1435 u64 mask = 1ULL << protocol;
1436
1437 ir_raw_load_modules(&mask);
1438 if (!mask) {
1439 rc = -EINVAL;
1440 goto out;
1441 }
1442 }
0751d33c
SY
1443 }
1444
1445 if (dev->wakeup_protocol != protocol) {
1446 dev->wakeup_protocol = protocol;
1447 IR_dprintk(1, "Wakeup protocol changed to %d\n", protocol);
1448
1449 if (protocol == RC_TYPE_RC6_MCE)
1450 dev->scancode_wakeup_filter.data = 0x800f0000;
1451 else
1452 dev->scancode_wakeup_filter.data = 0;
1453 dev->scancode_wakeup_filter.mask = 0;
1454
1455 rc = dev->s_wakeup_filter(dev, &dev->scancode_wakeup_filter);
1456 if (rc == 0)
1457 rc = len;
1458 } else {
1459 rc = len;
1460 }
1461
1462out:
1463 mutex_unlock(&dev->lock);
1464 return rc;
1465}
1466
d8b4b582
DH
1467static void rc_dev_release(struct device *device)
1468{
47cae1e1
MK
1469 struct rc_dev *dev = to_rc_dev(device);
1470
1471 kfree(dev);
d8b4b582
DH
1472}
1473
bc2a6c57
MCC
1474#define ADD_HOTPLUG_VAR(fmt, val...) \
1475 do { \
1476 int err = add_uevent_var(env, fmt, val); \
1477 if (err) \
1478 return err; \
1479 } while (0)
1480
1481static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1482{
d8b4b582 1483 struct rc_dev *dev = to_rc_dev(device);
bc2a6c57 1484
b088ba65
MCC
1485 if (dev->rc_map.name)
1486 ADD_HOTPLUG_VAR("NAME=%s", dev->rc_map.name);
d8b4b582
DH
1487 if (dev->driver_name)
1488 ADD_HOTPLUG_VAR("DRV_NAME=%s", dev->driver_name);
bc2a6c57
MCC
1489
1490 return 0;
1491}
1492
1493/*
1494 * Static device attribute struct with the sysfs attributes for IR's
1495 */
0751d33c
SY
1496static DEVICE_ATTR(protocols, 0644, show_protocols, store_protocols);
1497static DEVICE_ATTR(wakeup_protocols, 0644, show_wakeup_protocols,
1498 store_wakeup_protocols);
00942d1a
JH
1499static RC_FILTER_ATTR(filter, S_IRUGO|S_IWUSR,
1500 show_filter, store_filter, RC_FILTER_NORMAL, false);
1501static RC_FILTER_ATTR(filter_mask, S_IRUGO|S_IWUSR,
1502 show_filter, store_filter, RC_FILTER_NORMAL, true);
1503static RC_FILTER_ATTR(wakeup_filter, S_IRUGO|S_IWUSR,
1504 show_filter, store_filter, RC_FILTER_WAKEUP, false);
1505static RC_FILTER_ATTR(wakeup_filter_mask, S_IRUGO|S_IWUSR,
1506 show_filter, store_filter, RC_FILTER_WAKEUP, true);
bc2a6c57 1507
99b0f3c9 1508static struct attribute *rc_dev_protocol_attrs[] = {
0751d33c 1509 &dev_attr_protocols.attr,
99b0f3c9
DH
1510 NULL,
1511};
1512
db68102c 1513static const struct attribute_group rc_dev_protocol_attr_grp = {
99b0f3c9
DH
1514 .attrs = rc_dev_protocol_attrs,
1515};
1516
99b0f3c9 1517static struct attribute *rc_dev_filter_attrs[] = {
00942d1a
JH
1518 &dev_attr_filter.attr.attr,
1519 &dev_attr_filter_mask.attr.attr,
bc2a6c57
MCC
1520 NULL,
1521};
1522
db68102c 1523static const struct attribute_group rc_dev_filter_attr_grp = {
99b0f3c9 1524 .attrs = rc_dev_filter_attrs,
bc2a6c57
MCC
1525};
1526
99b0f3c9
DH
1527static struct attribute *rc_dev_wakeup_filter_attrs[] = {
1528 &dev_attr_wakeup_filter.attr.attr,
1529 &dev_attr_wakeup_filter_mask.attr.attr,
0751d33c 1530 &dev_attr_wakeup_protocols.attr,
99b0f3c9
DH
1531 NULL,
1532};
1533
db68102c 1534static const struct attribute_group rc_dev_wakeup_filter_attr_grp = {
99b0f3c9 1535 .attrs = rc_dev_wakeup_filter_attrs,
bc2a6c57
MCC
1536};
1537
1538static struct device_type rc_dev_type = {
d8b4b582 1539 .release = rc_dev_release,
bc2a6c57
MCC
1540 .uevent = rc_dev_uevent,
1541};
1542
0f7499fd 1543struct rc_dev *rc_allocate_device(enum rc_driver_type type)
bc2a6c57 1544{
d8b4b582 1545 struct rc_dev *dev;
bc2a6c57 1546
d8b4b582
DH
1547 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1548 if (!dev)
1549 return NULL;
1550
d34aee10
AS
1551 if (type != RC_DRIVER_IR_RAW_TX) {
1552 dev->input_dev = input_allocate_device();
1553 if (!dev->input_dev) {
1554 kfree(dev);
1555 return NULL;
1556 }
1557
1558 dev->input_dev->getkeycode = ir_getkeycode;
1559 dev->input_dev->setkeycode = ir_setkeycode;
1560 input_set_drvdata(dev->input_dev, dev);
d8b4b582 1561
d34aee10
AS
1562 setup_timer(&dev->timer_keyup, ir_timer_keyup,
1563 (unsigned long)dev);
d8b4b582 1564
d34aee10
AS
1565 spin_lock_init(&dev->rc_map.lock);
1566 spin_lock_init(&dev->keylock);
1567 }
08aeb7c9 1568 mutex_init(&dev->lock);
bc2a6c57 1569
d8b4b582 1570 dev->dev.type = &rc_dev_type;
40fc5325 1571 dev->dev.class = &rc_class;
d8b4b582
DH
1572 device_initialize(&dev->dev);
1573
0f7499fd
AS
1574 dev->driver_type = type;
1575
d8b4b582
DH
1576 __module_get(THIS_MODULE);
1577 return dev;
1578}
1579EXPORT_SYMBOL_GPL(rc_allocate_device);
1580
1581void rc_free_device(struct rc_dev *dev)
bc2a6c57 1582{
b05681b9
MCC
1583 if (!dev)
1584 return;
1585
3dd94f00 1586 input_free_device(dev->input_dev);
b05681b9
MCC
1587
1588 put_device(&dev->dev);
1589
47cae1e1
MK
1590 /* kfree(dev) will be called by the callback function
1591 rc_dev_release() */
1592
b05681b9 1593 module_put(THIS_MODULE);
d8b4b582
DH
1594}
1595EXPORT_SYMBOL_GPL(rc_free_device);
1596
ddbf7d5a
HK
1597static void devm_rc_alloc_release(struct device *dev, void *res)
1598{
1599 rc_free_device(*(struct rc_dev **)res);
1600}
1601
0f7499fd
AS
1602struct rc_dev *devm_rc_allocate_device(struct device *dev,
1603 enum rc_driver_type type)
ddbf7d5a
HK
1604{
1605 struct rc_dev **dr, *rc;
1606
1607 dr = devres_alloc(devm_rc_alloc_release, sizeof(*dr), GFP_KERNEL);
1608 if (!dr)
1609 return NULL;
1610
0f7499fd 1611 rc = rc_allocate_device(type);
ddbf7d5a
HK
1612 if (!rc) {
1613 devres_free(dr);
1614 return NULL;
1615 }
1616
1617 rc->dev.parent = dev;
1618 rc->managed_alloc = true;
1619 *dr = rc;
1620 devres_add(dev, dr);
1621
1622 return rc;
1623}
1624EXPORT_SYMBOL_GPL(devm_rc_allocate_device);
1625
f56928ab 1626static int rc_prepare_rx_device(struct rc_dev *dev)
d8b4b582 1627{
fcb13097 1628 int rc;
7ff2c2bc 1629 struct rc_map *rc_map;
41380868 1630 u64 rc_type;
bc2a6c57 1631
7ff2c2bc 1632 if (!dev->map_name)
d8b4b582 1633 return -EINVAL;
bc2a6c57 1634
d100e659 1635 rc_map = rc_map_get(dev->map_name);
b088ba65 1636 if (!rc_map)
d100e659 1637 rc_map = rc_map_get(RC_MAP_EMPTY);
b088ba65 1638 if (!rc_map || !rc_map->scan || rc_map->size == 0)
d8b4b582
DH
1639 return -EINVAL;
1640
7ff2c2bc
AS
1641 rc = ir_setkeytable(dev, rc_map);
1642 if (rc)
1643 return rc;
1644
41380868 1645 rc_type = BIT_ULL(rc_map->rc_type);
7ff2c2bc 1646
41380868 1647 if (dev->change_protocol) {
7ff2c2bc
AS
1648 rc = dev->change_protocol(dev, &rc_type);
1649 if (rc < 0)
1650 goto out_table;
1651 dev->enabled_protocols = rc_type;
1652 }
1653
41380868
SY
1654 if (dev->driver_type == RC_DRIVER_IR_RAW)
1655 ir_raw_load_modules(&rc_type);
1656
d8b4b582
DH
1657 set_bit(EV_KEY, dev->input_dev->evbit);
1658 set_bit(EV_REP, dev->input_dev->evbit);
1659 set_bit(EV_MSC, dev->input_dev->evbit);
1660 set_bit(MSC_SCAN, dev->input_dev->mscbit);
1661 if (dev->open)
1662 dev->input_dev->open = ir_open;
1663 if (dev->close)
1664 dev->input_dev->close = ir_close;
1665
b2aceb73
DH
1666 dev->input_dev->dev.parent = &dev->dev;
1667 memcpy(&dev->input_dev->id, &dev->input_id, sizeof(dev->input_id));
1668 dev->input_dev->phys = dev->input_phys;
518f4b26 1669 dev->input_dev->name = dev->device_name;
b2aceb73 1670
f56928ab
DH
1671 return 0;
1672
1673out_table:
1674 ir_free_table(&dev->rc_map);
1675
1676 return rc;
1677}
1678
1679static int rc_setup_rx_device(struct rc_dev *dev)
1680{
1681 int rc;
1682
b2aceb73
DH
1683 /* rc_open will be called here */
1684 rc = input_register_device(dev->input_dev);
1685 if (rc)
f56928ab 1686 return rc;
b2aceb73 1687
7ff2c2bc
AS
1688 /*
1689 * Default delay of 250ms is too short for some protocols, especially
1690 * since the timeout is currently set to 250ms. Increase it to 500ms,
1691 * to avoid wrong repetition of the keycodes. Note that this must be
1692 * set after the call to input_register_device().
1693 */
1694 dev->input_dev->rep[REP_DELAY] = 500;
1695
1696 /*
1697 * As a repeat event on protocols like RC-5 and NEC take as long as
1698 * 110/114ms, using 33ms as a repeat period is not the right thing
1699 * to do.
1700 */
1701 dev->input_dev->rep[REP_PERIOD] = 125;
1702
7ff2c2bc 1703 return 0;
7ff2c2bc
AS
1704}
1705
1706static void rc_free_rx_device(struct rc_dev *dev)
1707{
f56928ab 1708 if (!dev)
7ff2c2bc
AS
1709 return;
1710
f56928ab
DH
1711 if (dev->input_dev) {
1712 input_unregister_device(dev->input_dev);
1713 dev->input_dev = NULL;
1714 }
7ff2c2bc 1715
f56928ab 1716 ir_free_table(&dev->rc_map);
7ff2c2bc
AS
1717}
1718
1719int rc_register_device(struct rc_dev *dev)
1720{
7ff2c2bc
AS
1721 const char *path;
1722 int attr = 0;
1723 int minor;
1724 int rc;
1725
1726 if (!dev)
1727 return -EINVAL;
1728
fcb13097
DH
1729 minor = ida_simple_get(&rc_ida, 0, RC_DEV_MAX, GFP_KERNEL);
1730 if (minor < 0)
1731 return minor;
1732
1733 dev->minor = minor;
1734 dev_set_name(&dev->dev, "rc%u", dev->minor);
1735 dev_set_drvdata(&dev->dev, dev);
587d1b06 1736
99b0f3c9 1737 dev->dev.groups = dev->sysfs_groups;
d34aee10
AS
1738 if (dev->driver_type != RC_DRIVER_IR_RAW_TX)
1739 dev->sysfs_groups[attr++] = &rc_dev_protocol_attr_grp;
99b0f3c9 1740 if (dev->s_filter)
120703f9 1741 dev->sysfs_groups[attr++] = &rc_dev_filter_attr_grp;
99b0f3c9
DH
1742 if (dev->s_wakeup_filter)
1743 dev->sysfs_groups[attr++] = &rc_dev_wakeup_filter_attr_grp;
99b0f3c9
DH
1744 dev->sysfs_groups[attr++] = NULL;
1745
f56928ab
DH
1746 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1747 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
1748 rc = ir_raw_event_prepare(dev);
1749 if (rc < 0)
1750 goto out_minor;
1751 }
1752
1753 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1754 rc = rc_prepare_rx_device(dev);
1755 if (rc)
1756 goto out_raw;
1757 }
1758
d8b4b582
DH
1759 rc = device_add(&dev->dev);
1760 if (rc)
f56928ab 1761 goto out_rx_free;
bc2a6c57 1762
d8b4b582 1763 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
4dc0e908 1764 dev_info(&dev->dev, "%s as %s\n",
518f4b26 1765 dev->device_name ?: "Unspecified device", path ?: "N/A");
bc2a6c57
MCC
1766 kfree(path);
1767
f56928ab
DH
1768 if (dev->driver_type != RC_DRIVER_IR_RAW_TX) {
1769 rc = rc_setup_rx_device(dev);
1770 if (rc)
1771 goto out_dev;
1772 }
1773
d34aee10
AS
1774 if (dev->driver_type == RC_DRIVER_IR_RAW ||
1775 dev->driver_type == RC_DRIVER_IR_RAW_TX) {
d8b4b582
DH
1776 rc = ir_raw_event_register(dev);
1777 if (rc < 0)
f56928ab 1778 goto out_rx;
d8b4b582
DH
1779 }
1780
7ff2c2bc 1781 IR_dprintk(1, "Registered rc%u (driver: %s)\n",
fcb13097 1782 dev->minor,
7ff2c2bc 1783 dev->driver_name ? dev->driver_name : "unknown");
d8b4b582 1784
bc2a6c57 1785 return 0;
d8b4b582 1786
f56928ab
DH
1787out_rx:
1788 rc_free_rx_device(dev);
d8b4b582
DH
1789out_dev:
1790 device_del(&dev->dev);
f56928ab
DH
1791out_rx_free:
1792 ir_free_table(&dev->rc_map);
1793out_raw:
1794 ir_raw_event_free(dev);
1795out_minor:
fcb13097 1796 ida_simple_remove(&rc_ida, minor);
d8b4b582 1797 return rc;
bc2a6c57 1798}
d8b4b582 1799EXPORT_SYMBOL_GPL(rc_register_device);
bc2a6c57 1800
ddbf7d5a
HK
1801static void devm_rc_release(struct device *dev, void *res)
1802{
1803 rc_unregister_device(*(struct rc_dev **)res);
1804}
1805
1806int devm_rc_register_device(struct device *parent, struct rc_dev *dev)
1807{
1808 struct rc_dev **dr;
1809 int ret;
1810
1811 dr = devres_alloc(devm_rc_release, sizeof(*dr), GFP_KERNEL);
1812 if (!dr)
1813 return -ENOMEM;
1814
1815 ret = rc_register_device(dev);
1816 if (ret) {
1817 devres_free(dr);
1818 return ret;
1819 }
1820
1821 *dr = dev;
1822 devres_add(parent, dr);
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(devm_rc_register_device);
1827
d8b4b582 1828void rc_unregister_device(struct rc_dev *dev)
bc2a6c57 1829{
d8b4b582
DH
1830 if (!dev)
1831 return;
bc2a6c57 1832
d8b4b582 1833 del_timer_sync(&dev->timer_keyup);
bc2a6c57 1834
d8b4b582
DH
1835 if (dev->driver_type == RC_DRIVER_IR_RAW)
1836 ir_raw_event_unregister(dev);
1837
7ff2c2bc 1838 rc_free_rx_device(dev);
d8b4b582 1839
b05681b9 1840 device_del(&dev->dev);
d8b4b582 1841
fcb13097
DH
1842 ida_simple_remove(&rc_ida, dev->minor);
1843
ddbf7d5a
HK
1844 if (!dev->managed_alloc)
1845 rc_free_device(dev);
bc2a6c57 1846}
b05681b9 1847
d8b4b582 1848EXPORT_SYMBOL_GPL(rc_unregister_device);
bc2a6c57
MCC
1849
1850/*
1851 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1852 */
1853
6bda9644 1854static int __init rc_core_init(void)
bc2a6c57 1855{
40fc5325 1856 int rc = class_register(&rc_class);
bc2a6c57 1857 if (rc) {
d3d96820 1858 pr_err("rc_core: unable to register rc class\n");
bc2a6c57
MCC
1859 return rc;
1860 }
1861
153a60bb 1862 led_trigger_register_simple("rc-feedback", &led_feedback);
d100e659 1863 rc_map_register(&empty_map);
bc2a6c57
MCC
1864
1865 return 0;
1866}
1867
6bda9644 1868static void __exit rc_core_exit(void)
bc2a6c57 1869{
40fc5325 1870 class_unregister(&rc_class);
153a60bb 1871 led_trigger_unregister_simple(led_feedback);
d100e659 1872 rc_map_unregister(&empty_map);
bc2a6c57
MCC
1873}
1874
e76d4ce4 1875subsys_initcall(rc_core_init);
6bda9644 1876module_exit(rc_core_exit);
bc2a6c57 1877
6bda9644
MCC
1878int rc_core_debug; /* ir_debug level (0,1,2) */
1879EXPORT_SYMBOL_GPL(rc_core_debug);
1880module_param_named(debug, rc_core_debug, int, 0644);
446e4a64 1881
37e59f87 1882MODULE_AUTHOR("Mauro Carvalho Chehab");
446e4a64 1883MODULE_LICENSE("GPL");