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