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