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