1 /* rc-core.c - handle IR scancode->keycode tables
3 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
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.
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.
15 #include <media/ir-core.h>
16 #include <linux/spinlock.h>
17 #include <linux/delay.h>
18 #include <linux/input.h>
19 #include <linux/slab.h>
20 #include <linux/device.h>
21 #include "rc-core-priv.h"
23 #define IRRCV_NUM_DEVICES 256
25 /* bit array to represent IR sysfs device number */
26 static unsigned long ir_core_dev_number;
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
32 /* FIXME: IR_KEYPRESS_TIMEOUT should be protocol specific */
33 #define IR_KEYPRESS_TIMEOUT 250
35 /* Used to keep track of known keymaps */
36 static LIST_HEAD(rc_map_list);
37 static DEFINE_SPINLOCK(rc_map_lock);
39 /* Forward declarations */
40 static int ir_register_class(struct input_dev *input_dev);
41 static void ir_unregister_class(struct input_dev *input_dev);
42 static int ir_register_input(struct input_dev *input_dev);
45 static struct rc_keymap *seek_rc_map(const char *name)
47 struct rc_keymap *map = NULL;
49 spin_lock(&rc_map_lock);
50 list_for_each_entry(map, &rc_map_list, list) {
51 if (!strcmp(name, map->map.name)) {
52 spin_unlock(&rc_map_lock);
56 spin_unlock(&rc_map_lock);
61 struct ir_scancode_table *get_rc_map(const char *name)
64 struct rc_keymap *map;
66 map = seek_rc_map(name);
69 int rc = request_module(name);
71 printk(KERN_ERR "Couldn't load IR keymap %s\n", name);
74 msleep(20); /* Give some time for IR to register */
76 map = seek_rc_map(name);
80 printk(KERN_ERR "IR keymap %s not found\n", name);
84 printk(KERN_INFO "Registered IR keymap %s\n", map->map.name);
88 EXPORT_SYMBOL_GPL(get_rc_map);
90 int ir_register_map(struct rc_keymap *map)
92 spin_lock(&rc_map_lock);
93 list_add_tail(&map->list, &rc_map_list);
94 spin_unlock(&rc_map_lock);
97 EXPORT_SYMBOL_GPL(ir_register_map);
99 void ir_unregister_map(struct rc_keymap *map)
101 spin_lock(&rc_map_lock);
102 list_del(&map->list);
103 spin_unlock(&rc_map_lock);
105 EXPORT_SYMBOL_GPL(ir_unregister_map);
108 static struct ir_scancode empty[] = {
109 { 0x2a, KEY_COFFEE },
112 static struct rc_keymap empty_map = {
115 .size = ARRAY_SIZE(empty),
116 .ir_type = IR_TYPE_UNKNOWN, /* Legacy IR type */
117 .name = RC_MAP_EMPTY,
122 * ir_create_table() - initializes a scancode table
123 * @rc_tab: the ir_scancode_table to initialize
124 * @name: name to assign to the table
125 * @ir_type: ir type to assign to the new table
126 * @size: initial size of the table
127 * @return: zero on success or a negative error code
129 * This routine will initialize the ir_scancode_table and will allocate
130 * memory to hold at least the specified number elements.
132 static int ir_create_table(struct ir_scancode_table *rc_tab,
133 const char *name, u64 ir_type, size_t size)
136 rc_tab->ir_type = ir_type;
137 rc_tab->alloc = roundup_pow_of_two(size * sizeof(struct ir_scancode));
138 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
139 rc_tab->scan = kmalloc(rc_tab->alloc, GFP_KERNEL);
143 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
144 rc_tab->size, rc_tab->alloc);
149 * ir_free_table() - frees memory allocated by a scancode table
150 * @rc_tab: the table whose mappings need to be freed
152 * This routine will free memory alloctaed for key mappings used by given
155 static void ir_free_table(struct ir_scancode_table *rc_tab)
163 * ir_resize_table() - resizes a scancode table if necessary
164 * @rc_tab: the ir_scancode_table to resize
165 * @gfp_flags: gfp flags to use when allocating memory
166 * @return: zero on success or a negative error code
168 * This routine will shrink the ir_scancode_table if it has lots of
169 * unused entries and grow it if it is full.
171 static int ir_resize_table(struct ir_scancode_table *rc_tab, gfp_t gfp_flags)
173 unsigned int oldalloc = rc_tab->alloc;
174 unsigned int newalloc = oldalloc;
175 struct ir_scancode *oldscan = rc_tab->scan;
176 struct ir_scancode *newscan;
178 if (rc_tab->size == rc_tab->len) {
179 /* All entries in use -> grow keytable */
180 if (rc_tab->alloc >= IR_TAB_MAX_SIZE)
184 IR_dprintk(1, "Growing table to %u bytes\n", newalloc);
187 if ((rc_tab->len * 3 < rc_tab->size) && (oldalloc > IR_TAB_MIN_SIZE)) {
188 /* Less than 1/3 of entries in use -> shrink keytable */
190 IR_dprintk(1, "Shrinking table to %u bytes\n", newalloc);
193 if (newalloc == oldalloc)
196 newscan = kmalloc(newalloc, gfp_flags);
198 IR_dprintk(1, "Failed to kmalloc %u bytes\n", newalloc);
202 memcpy(newscan, rc_tab->scan, rc_tab->len * sizeof(struct ir_scancode));
203 rc_tab->scan = newscan;
204 rc_tab->alloc = newalloc;
205 rc_tab->size = rc_tab->alloc / sizeof(struct ir_scancode);
211 * ir_update_mapping() - set a keycode in the scancode->keycode table
212 * @dev: the struct input_dev device descriptor
213 * @rc_tab: scancode table to be adjusted
214 * @index: index of the mapping that needs to be updated
215 * @keycode: the desired keycode
216 * @return: previous keycode assigned to the mapping
218 * This routine is used to update scancode->keycopde mapping at given
221 static unsigned int ir_update_mapping(struct input_dev *dev,
222 struct ir_scancode_table *rc_tab,
224 unsigned int new_keycode)
226 int old_keycode = rc_tab->scan[index].keycode;
229 /* Did the user wish to remove the mapping? */
230 if (new_keycode == KEY_RESERVED || new_keycode == KEY_UNKNOWN) {
231 IR_dprintk(1, "#%d: Deleting scan 0x%04x\n",
232 index, rc_tab->scan[index].scancode);
234 memmove(&rc_tab->scan[index], &rc_tab->scan[index+ 1],
235 (rc_tab->len - index) * sizeof(struct ir_scancode));
237 IR_dprintk(1, "#%d: %s scan 0x%04x with key 0x%04x\n",
239 old_keycode == KEY_RESERVED ? "New" : "Replacing",
240 rc_tab->scan[index].scancode, new_keycode);
241 rc_tab->scan[index].keycode = new_keycode;
242 __set_bit(new_keycode, dev->keybit);
245 if (old_keycode != KEY_RESERVED) {
246 /* A previous mapping was updated... */
247 __clear_bit(old_keycode, dev->keybit);
248 /* ... but another scancode might use the same keycode */
249 for (i = 0; i < rc_tab->len; i++) {
250 if (rc_tab->scan[i].keycode == old_keycode) {
251 __set_bit(old_keycode, dev->keybit);
256 /* Possibly shrink the keytable, failure is not a problem */
257 ir_resize_table(rc_tab, GFP_ATOMIC);
264 * ir_establish_scancode() - set a keycode in the scancode->keycode table
265 * @ir_dev: the struct ir_input_dev device descriptor
266 * @rc_tab: scancode table to be searched
267 * @scancode: the desired scancode
268 * @resize: controls whether we allowed to resize the table to
269 * accomodate not yet present scancodes
270 * @return: index of the mapping containing scancode in question
271 * or -1U in case of failure.
273 * This routine is used to locate given scancode in ir_scancode_table.
274 * If scancode is not yet present the routine will allocate a new slot
277 static unsigned int ir_establish_scancode(struct ir_input_dev *ir_dev,
278 struct ir_scancode_table *rc_tab,
279 unsigned int scancode,
285 * Unfortunately, some hardware-based IR decoders don't provide
286 * all bits for the complete IR code. In general, they provide only
287 * the command part of the IR code. Yet, as it is possible to replace
288 * the provided IR with another one, it is needed to allow loading
289 * IR tables from other remotes. So,
291 if (ir_dev->props && ir_dev->props->scanmask)
292 scancode &= ir_dev->props->scanmask;
294 /* First check if we already have a mapping for this ir command */
295 for (i = 0; i < rc_tab->len; i++) {
296 if (rc_tab->scan[i].scancode == scancode)
299 /* Keytable is sorted from lowest to highest scancode */
300 if (rc_tab->scan[i].scancode >= scancode)
304 /* No previous mapping found, we might need to grow the table */
305 if (rc_tab->size == rc_tab->len) {
306 if (!resize || ir_resize_table(rc_tab, GFP_ATOMIC))
310 /* i is the proper index to insert our new keycode */
312 memmove(&rc_tab->scan[i + 1], &rc_tab->scan[i],
313 (rc_tab->len - i) * sizeof(struct ir_scancode));
314 rc_tab->scan[i].scancode = scancode;
315 rc_tab->scan[i].keycode = KEY_RESERVED;
322 * ir_setkeycode() - set a keycode in the scancode->keycode table
323 * @dev: the struct input_dev device descriptor
324 * @scancode: the desired scancode
326 * @return: -EINVAL if the keycode could not be inserted, otherwise zero.
328 * This routine is used to handle evdev EVIOCSKEY ioctl.
330 static int ir_setkeycode(struct input_dev *dev,
331 const struct input_keymap_entry *ke,
332 unsigned int *old_keycode)
334 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
335 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
337 unsigned int scancode;
341 spin_lock_irqsave(&rc_tab->lock, flags);
343 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
345 if (index >= rc_tab->len) {
350 retval = input_scancode_to_scalar(ke, &scancode);
354 index = ir_establish_scancode(ir_dev, rc_tab, scancode, true);
355 if (index >= rc_tab->len) {
361 *old_keycode = ir_update_mapping(dev, rc_tab, index, ke->keycode);
364 spin_unlock_irqrestore(&rc_tab->lock, flags);
369 * ir_setkeytable() - sets several entries in the scancode->keycode table
370 * @dev: the struct input_dev device descriptor
371 * @to: the struct ir_scancode_table to copy entries to
372 * @from: the struct ir_scancode_table to copy entries from
373 * @return: -ENOMEM if all keycodes could not be inserted, otherwise zero.
375 * This routine is used to handle table initialization.
377 static int ir_setkeytable(struct ir_input_dev *ir_dev,
378 const struct ir_scancode_table *from)
380 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
381 unsigned int i, index;
384 rc = ir_create_table(&ir_dev->rc_tab,
385 from->name, from->ir_type, from->size);
389 IR_dprintk(1, "Allocated space for %u keycode entries (%u bytes)\n",
390 rc_tab->size, rc_tab->alloc);
392 for (i = 0; i < from->size; i++) {
393 index = ir_establish_scancode(ir_dev, rc_tab,
394 from->scan[i].scancode, false);
395 if (index >= rc_tab->len) {
400 ir_update_mapping(ir_dev->input_dev, rc_tab, index,
401 from->scan[i].keycode);
405 ir_free_table(rc_tab);
411 * ir_lookup_by_scancode() - locate mapping by scancode
412 * @rc_tab: the &struct ir_scancode_table to search
413 * @scancode: scancode to look for in the table
414 * @return: index in the table, -1U if not found
416 * This routine performs binary search in RC keykeymap table for
419 static unsigned int ir_lookup_by_scancode(const struct ir_scancode_table *rc_tab,
420 unsigned int scancode)
423 int end = rc_tab->len - 1;
426 while (start <= end) {
427 mid = (start + end) / 2;
428 if (rc_tab->scan[mid].scancode < scancode)
430 else if (rc_tab->scan[mid].scancode > scancode)
440 * ir_getkeycode() - get a keycode from the scancode->keycode table
441 * @dev: the struct input_dev device descriptor
442 * @scancode: the desired scancode
443 * @keycode: used to return the keycode, if found, or KEY_RESERVED
444 * @return: always returns zero.
446 * This routine is used to handle evdev EVIOCGKEY ioctl.
448 static int ir_getkeycode(struct input_dev *dev,
449 struct input_keymap_entry *ke)
451 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
452 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
453 struct ir_scancode *entry;
456 unsigned int scancode;
459 spin_lock_irqsave(&rc_tab->lock, flags);
461 if (ke->flags & INPUT_KEYMAP_BY_INDEX) {
464 retval = input_scancode_to_scalar(ke, &scancode);
468 index = ir_lookup_by_scancode(rc_tab, scancode);
471 if (index >= rc_tab->len) {
472 if (!(ke->flags & INPUT_KEYMAP_BY_INDEX))
473 IR_dprintk(1, "unknown key for scancode 0x%04x\n",
479 entry = &rc_tab->scan[index];
482 ke->keycode = entry->keycode;
483 ke->len = sizeof(entry->scancode);
484 memcpy(ke->scancode, &entry->scancode, sizeof(entry->scancode));
489 spin_unlock_irqrestore(&rc_tab->lock, flags);
494 * ir_g_keycode_from_table() - gets the keycode that corresponds to a scancode
495 * @input_dev: the struct input_dev descriptor of the device
496 * @scancode: the scancode that we're seeking
498 * This routine is used by the input routines when a key is pressed at the
499 * IR. The scancode is received and needs to be converted into a keycode.
500 * If the key is not found, it returns KEY_RESERVED. Otherwise, returns the
501 * corresponding keycode from the table.
503 u32 ir_g_keycode_from_table(struct input_dev *dev, u32 scancode)
505 struct ir_input_dev *ir_dev = input_get_drvdata(dev);
506 struct ir_scancode_table *rc_tab = &ir_dev->rc_tab;
507 unsigned int keycode;
511 spin_lock_irqsave(&rc_tab->lock, flags);
513 index = ir_lookup_by_scancode(rc_tab, scancode);
514 keycode = index < rc_tab->len ?
515 rc_tab->scan[index].keycode : KEY_RESERVED;
517 spin_unlock_irqrestore(&rc_tab->lock, flags);
519 if (keycode != KEY_RESERVED)
520 IR_dprintk(1, "%s: scancode 0x%04x keycode 0x%02x\n",
521 dev->name, scancode, keycode);
525 EXPORT_SYMBOL_GPL(ir_g_keycode_from_table);
528 * ir_do_keyup() - internal function to signal the release of a keypress
529 * @ir: the struct ir_input_dev descriptor of the device
531 * This function is used internally to release a keypress, it must be
532 * called with keylock held.
534 static void ir_do_keyup(struct ir_input_dev *ir)
539 IR_dprintk(1, "keyup key 0x%04x\n", ir->last_keycode);
540 input_report_key(ir->input_dev, ir->last_keycode, 0);
541 input_sync(ir->input_dev);
542 ir->keypressed = false;
546 * ir_keyup() - generates input event to signal the release of a keypress
547 * @dev: the struct input_dev descriptor of the device
549 * This routine is used to signal that a key has been released on the
552 void ir_keyup(struct input_dev *dev)
555 struct ir_input_dev *ir = input_get_drvdata(dev);
557 spin_lock_irqsave(&ir->keylock, flags);
559 spin_unlock_irqrestore(&ir->keylock, flags);
561 EXPORT_SYMBOL_GPL(ir_keyup);
564 * ir_timer_keyup() - generates a keyup event after a timeout
565 * @cookie: a pointer to struct ir_input_dev passed to setup_timer()
567 * This routine will generate a keyup event some time after a keydown event
568 * is generated when no further activity has been detected.
570 static void ir_timer_keyup(unsigned long cookie)
572 struct ir_input_dev *ir = (struct ir_input_dev *)cookie;
576 * ir->keyup_jiffies is used to prevent a race condition if a
577 * hardware interrupt occurs at this point and the keyup timer
578 * event is moved further into the future as a result.
580 * The timer will then be reactivated and this function called
581 * again in the future. We need to exit gracefully in that case
582 * to allow the input subsystem to do its auto-repeat magic or
583 * a keyup event might follow immediately after the keydown.
585 spin_lock_irqsave(&ir->keylock, flags);
586 if (time_is_before_eq_jiffies(ir->keyup_jiffies))
588 spin_unlock_irqrestore(&ir->keylock, flags);
592 * ir_repeat() - notifies the IR core that a key is still pressed
593 * @dev: the struct input_dev descriptor of the device
595 * This routine is used by IR decoders when a repeat message which does
596 * not include the necessary bits to reproduce the scancode has been
599 void ir_repeat(struct input_dev *dev)
602 struct ir_input_dev *ir = input_get_drvdata(dev);
604 spin_lock_irqsave(&ir->keylock, flags);
606 input_event(dev, EV_MSC, MSC_SCAN, ir->last_scancode);
611 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
612 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
615 spin_unlock_irqrestore(&ir->keylock, flags);
617 EXPORT_SYMBOL_GPL(ir_repeat);
620 * ir_do_keydown() - internal function to process a keypress
621 * @dev: the struct input_dev descriptor of the device
622 * @scancode: the scancode of the keypress
623 * @keycode: the keycode of the keypress
624 * @toggle: the toggle value of the keypress
626 * This function is used internally to register a keypress, it must be
627 * called with keylock held.
629 static void ir_do_keydown(struct input_dev *dev, int scancode,
630 u32 keycode, u8 toggle)
632 struct ir_input_dev *ir = input_get_drvdata(dev);
634 input_event(dev, EV_MSC, MSC_SCAN, scancode);
637 if (ir->keypressed &&
638 ir->last_scancode == scancode &&
639 ir->last_toggle == toggle)
642 /* Release old keypress */
645 ir->last_scancode = scancode;
646 ir->last_toggle = toggle;
647 ir->last_keycode = keycode;
649 if (keycode == KEY_RESERVED)
652 /* Register a keypress */
653 ir->keypressed = true;
654 IR_dprintk(1, "%s: key down event, key 0x%04x, scancode 0x%04x\n",
655 dev->name, keycode, scancode);
656 input_report_key(dev, ir->last_keycode, 1);
661 * ir_keydown() - generates input event for a key press
662 * @dev: the struct input_dev descriptor of the device
663 * @scancode: the scancode that we're seeking
664 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
665 * support toggle values, this should be set to zero)
667 * This routine is used by the input routines when a key is pressed at the
668 * IR. It gets the keycode for a scancode and reports an input event via
669 * input_report_key().
671 void ir_keydown(struct input_dev *dev, int scancode, u8 toggle)
674 struct ir_input_dev *ir = input_get_drvdata(dev);
675 u32 keycode = ir_g_keycode_from_table(dev, scancode);
677 spin_lock_irqsave(&ir->keylock, flags);
678 ir_do_keydown(dev, scancode, keycode, toggle);
680 if (ir->keypressed) {
681 ir->keyup_jiffies = jiffies + msecs_to_jiffies(IR_KEYPRESS_TIMEOUT);
682 mod_timer(&ir->timer_keyup, ir->keyup_jiffies);
684 spin_unlock_irqrestore(&ir->keylock, flags);
686 EXPORT_SYMBOL_GPL(ir_keydown);
689 * ir_keydown_notimeout() - generates input event for a key press without
690 * an automatic keyup event at a later time
691 * @dev: the struct input_dev descriptor of the device
692 * @scancode: the scancode that we're seeking
693 * @toggle: the toggle value (protocol dependent, if the protocol doesn't
694 * support toggle values, this should be set to zero)
696 * This routine is used by the input routines when a key is pressed at the
697 * IR. It gets the keycode for a scancode and reports an input event via
698 * input_report_key(). The driver must manually call ir_keyup() at a later
701 void ir_keydown_notimeout(struct input_dev *dev, int scancode, u8 toggle)
704 struct ir_input_dev *ir = input_get_drvdata(dev);
705 u32 keycode = ir_g_keycode_from_table(dev, scancode);
707 spin_lock_irqsave(&ir->keylock, flags);
708 ir_do_keydown(dev, scancode, keycode, toggle);
709 spin_unlock_irqrestore(&ir->keylock, flags);
711 EXPORT_SYMBOL_GPL(ir_keydown_notimeout);
713 static int ir_open(struct input_dev *input_dev)
715 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
717 return ir_dev->props->open(ir_dev->props->priv);
720 static void ir_close(struct input_dev *input_dev)
722 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
724 ir_dev->props->close(ir_dev->props->priv);
728 * __ir_input_register() - sets the IR keycode table and add the handlers
729 * for keymap table get/set
730 * @input_dev: the struct input_dev descriptor of the device
731 * @rc_tab: the struct ir_scancode_table table of scancode/keymap
733 * This routine is used to initialize the input infrastructure
734 * to work with an IR.
735 * It will register the input/evdev interface for the device and
736 * register the syfs code for IR class
738 int __ir_input_register(struct input_dev *input_dev,
739 const struct ir_scancode_table *rc_tab,
740 struct ir_dev_props *props,
741 const char *driver_name)
743 struct ir_input_dev *ir_dev;
746 if (rc_tab->scan == NULL || !rc_tab->size)
749 ir_dev = kzalloc(sizeof(*ir_dev), GFP_KERNEL);
753 ir_dev->driver_name = kasprintf(GFP_KERNEL, "%s", driver_name);
754 if (!ir_dev->driver_name) {
759 input_dev->getkeycode_new = ir_getkeycode;
760 input_dev->setkeycode_new = ir_setkeycode;
761 input_set_drvdata(input_dev, ir_dev);
762 ir_dev->input_dev = input_dev;
764 spin_lock_init(&ir_dev->rc_tab.lock);
765 spin_lock_init(&ir_dev->keylock);
766 setup_timer(&ir_dev->timer_keyup, ir_timer_keyup, (unsigned long)ir_dev);
769 ir_dev->props = props;
771 input_dev->open = ir_open;
773 input_dev->close = ir_close;
776 set_bit(EV_KEY, input_dev->evbit);
777 set_bit(EV_REP, input_dev->evbit);
778 set_bit(EV_MSC, input_dev->evbit);
779 set_bit(MSC_SCAN, input_dev->mscbit);
781 rc = ir_setkeytable(ir_dev, rc_tab);
785 rc = ir_register_class(input_dev);
790 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW) {
791 rc = ir_raw_event_register(input_dev);
796 rc = ir_register_input(input_dev);
800 IR_dprintk(1, "Registered input device on %s for %s remote%s.\n",
801 driver_name, rc_tab->name,
802 (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_IR_RAW) ?
803 " in raw mode" : "");
806 * Default delay of 250ms is too short for some protocols, expecially
807 * since the timeout is currently set to 250ms. Increase it to 500ms,
808 * to avoid wrong repetition of the keycodes.
810 input_dev->rep[REP_DELAY] = 500;
815 ir_unregister_class(input_dev);
817 ir_free_table(&ir_dev->rc_tab);
819 kfree(ir_dev->driver_name);
824 EXPORT_SYMBOL_GPL(__ir_input_register);
827 * ir_input_unregister() - unregisters IR and frees resources
828 * @input_dev: the struct input_dev descriptor of the device
830 * This routine is used to free memory and de-register interfaces.
832 void ir_input_unregister(struct input_dev *input_dev)
834 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
839 IR_dprintk(1, "Freed keycode table\n");
841 del_timer_sync(&ir_dev->timer_keyup);
843 if (ir_dev->props->driver_type == RC_DRIVER_IR_RAW)
844 ir_raw_event_unregister(input_dev);
846 ir_free_table(&ir_dev->rc_tab);
848 ir_unregister_class(input_dev);
850 kfree(ir_dev->driver_name);
853 EXPORT_SYMBOL_GPL(ir_input_unregister);
855 /* class for /sys/class/rc */
856 static char *ir_devnode(struct device *dev, mode_t *mode)
858 return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
861 static struct class ir_input_class = {
863 .devnode = ir_devnode,
870 { IR_TYPE_UNKNOWN, "unknown" },
871 { IR_TYPE_RC5, "rc-5" },
872 { IR_TYPE_NEC, "nec" },
873 { IR_TYPE_RC6, "rc-6" },
874 { IR_TYPE_JVC, "jvc" },
875 { IR_TYPE_SONY, "sony" },
876 { IR_TYPE_RC5_SZ, "rc-5-sz" },
877 { IR_TYPE_LIRC, "lirc" },
880 #define PROTO_NONE "none"
883 * show_protocols() - shows the current IR protocol(s)
884 * @d: the device descriptor
885 * @mattr: the device attribute struct (unused)
886 * @buf: a pointer to the output buffer
888 * This routine is a callback routine for input read the IR protocol type(s).
889 * it is trigged by reading /sys/class/rc/rc?/protocols.
890 * It returns the protocol names of supported protocols.
891 * Enabled protocols are printed in brackets.
893 static ssize_t show_protocols(struct device *d,
894 struct device_attribute *mattr, char *buf)
896 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
897 u64 allowed, enabled;
901 /* Device is being removed */
905 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
906 enabled = ir_dev->rc_tab.ir_type;
907 allowed = ir_dev->props->allowed_protos;
908 } else if (ir_dev->raw) {
909 enabled = ir_dev->raw->enabled_protocols;
910 allowed = ir_raw_get_allowed_protocols();
912 return sprintf(tmp, "[builtin]\n");
914 IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
918 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
919 if (allowed & enabled & proto_names[i].type)
920 tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
921 else if (allowed & proto_names[i].type)
922 tmp += sprintf(tmp, "%s ", proto_names[i].name);
928 return tmp + 1 - buf;
932 * store_protocols() - changes the current IR protocol(s)
933 * @d: the device descriptor
934 * @mattr: the device attribute struct (unused)
935 * @buf: a pointer to the input buffer
936 * @len: length of the input buffer
938 * This routine is a callback routine for changing the IR protocol type.
939 * It is trigged by writing to /sys/class/rc/rc?/protocols.
940 * Writing "+proto" will add a protocol to the list of enabled protocols.
941 * Writing "-proto" will remove a protocol from the list of enabled protocols.
942 * Writing "proto" will enable only "proto".
943 * Writing "none" will disable all protocols.
944 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
945 * is used, otherwise @len.
947 static ssize_t store_protocols(struct device *d,
948 struct device_attribute *mattr,
952 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
953 bool enable, disable;
957 int rc, i, count = 0;
960 /* Device is being removed */
964 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
965 type = ir_dev->rc_tab.ir_type;
966 else if (ir_dev->raw)
967 type = ir_dev->raw->enabled_protocols;
969 IR_dprintk(1, "Protocol switching not supported\n");
973 while ((tmp = strsep((char **) &data, " \n")) != NULL) {
981 } else if (*tmp == '-') {
990 if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
991 tmp += sizeof(PROTO_NONE);
995 for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
996 if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
997 tmp += strlen(proto_names[i].name);
998 mask = proto_names[i].type;
1002 if (i == ARRAY_SIZE(proto_names)) {
1003 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
1018 IR_dprintk(1, "Protocol not specified\n");
1022 if (ir_dev->props && ir_dev->props->change_protocol) {
1023 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
1026 IR_dprintk(1, "Error setting protocols to 0x%llx\n",
1032 if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
1033 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
1034 ir_dev->rc_tab.ir_type = type;
1035 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
1037 ir_dev->raw->enabled_protocols = type;
1040 IR_dprintk(1, "Current protocol(s): 0x%llx\n",
1046 #define ADD_HOTPLUG_VAR(fmt, val...) \
1048 int err = add_uevent_var(env, fmt, val); \
1053 static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
1055 struct ir_input_dev *ir_dev = dev_get_drvdata(device);
1057 if (ir_dev->rc_tab.name)
1058 ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
1059 if (ir_dev->driver_name)
1060 ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
1066 * Static device attribute struct with the sysfs attributes for IR's
1068 static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
1069 show_protocols, store_protocols);
1071 static struct attribute *rc_dev_attrs[] = {
1072 &dev_attr_protocols.attr,
1076 static struct attribute_group rc_dev_attr_grp = {
1077 .attrs = rc_dev_attrs,
1080 static const struct attribute_group *rc_dev_attr_groups[] = {
1085 static struct device_type rc_dev_type = {
1086 .groups = rc_dev_attr_groups,
1087 .uevent = rc_dev_uevent,
1091 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
1092 * @input_dev: the struct input_dev descriptor of the device
1094 * This routine is used to register the syfs code for IR class
1096 static int ir_register_class(struct input_dev *input_dev)
1098 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1099 int devno = find_first_zero_bit(&ir_core_dev_number,
1102 if (unlikely(devno < 0))
1105 ir_dev->dev.type = &rc_dev_type;
1106 ir_dev->devno = devno;
1108 ir_dev->dev.class = &ir_input_class;
1109 ir_dev->dev.parent = input_dev->dev.parent;
1110 input_dev->dev.parent = &ir_dev->dev;
1111 dev_set_name(&ir_dev->dev, "rc%d", devno);
1112 dev_set_drvdata(&ir_dev->dev, ir_dev);
1113 return device_register(&ir_dev->dev);
1117 * ir_register_input - registers ir input device with input subsystem
1118 * @input_dev: the struct input_dev descriptor of the device
1121 static int ir_register_input(struct input_dev *input_dev)
1123 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1128 rc = input_register_device(input_dev);
1130 device_del(&ir_dev->dev);
1134 __module_get(THIS_MODULE);
1136 path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
1137 printk(KERN_INFO "%s: %s as %s\n",
1138 dev_name(&ir_dev->dev),
1139 input_dev->name ? input_dev->name : "Unspecified device",
1140 path ? path : "N/A");
1143 set_bit(ir_dev->devno, &ir_core_dev_number);
1148 * ir_unregister_class() - removes the sysfs for sysfs for
1150 * @input_dev: the struct input_dev descriptor of the device
1152 * This routine is used to unregister the syfs code for IR class
1154 static void ir_unregister_class(struct input_dev *input_dev)
1156 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
1158 input_set_drvdata(input_dev, NULL);
1159 clear_bit(ir_dev->devno, &ir_core_dev_number);
1160 input_unregister_device(input_dev);
1161 device_del(&ir_dev->dev);
1163 module_put(THIS_MODULE);
1167 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
1170 static int __init ir_core_init(void)
1172 int rc = class_register(&ir_input_class);
1174 printk(KERN_ERR "ir_core: unable to register rc class\n");
1178 /* Initialize/load the decoders/keymap code that will be used */
1180 ir_register_map(&empty_map);
1185 static void __exit ir_core_exit(void)
1187 class_unregister(&ir_input_class);
1188 ir_unregister_map(&empty_map);
1191 module_init(ir_core_init);
1192 module_exit(ir_core_exit);
1194 int ir_core_debug; /* ir_debug level (0,1,2) */
1195 EXPORT_SYMBOL_GPL(ir_core_debug);
1196 module_param_named(debug, ir_core_debug, int, 0644);
1198 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
1199 MODULE_LICENSE("GPL");