blob: 821345dbe7c9d1c548bd85cf5330dc3972547020 [file] [log] [blame]
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -03001/* ir-register.c - handle IR scancode->keycode tables
2 *
3 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
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#include <linux/input.h>
16#include <linux/device.h>
17#include <media/ir-core.h>
18
19#define IRRCV_NUM_DEVICES 256
20
21unsigned long ir_core_dev_number;
22
23static struct class *ir_input_class;
24
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030025
26static ssize_t show_protocol(struct device *d,
27 struct device_attribute *mattr, char *buf)
28{
29 char *s;
30 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
31 enum ir_type ir_type = ir_dev->rc_tab.ir_type;
32
33 IR_dprintk(1, "Current protocol is %ld\n", ir_type);
34
35 /* FIXME: doesn't support multiple protocols at the same time */
36 if (ir_type == IR_TYPE_UNKNOWN)
37 s = "Unknown";
38 else if (ir_type == IR_TYPE_RC5)
39 s = "RC-5";
40 else if (ir_type == IR_TYPE_PD)
41 s = "Pulse/distance";
42 else if (ir_type == IR_TYPE_NEC)
43 s = "NEC";
44 else
45 s = "Other";
46
47 return sprintf(buf, "%s\n", s);
48}
49
50static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
51 show_protocol, NULL);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030052
53static struct attribute *ir_dev_attrs[] = {
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030054 &dev_attr_current_protocol.attr,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030055};
56
57int ir_register_class(struct input_dev *input_dev)
58{
59 int rc;
60 struct kobject *kobj;
61
62 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
63 int devno = find_first_zero_bit(&ir_core_dev_number,
64 IRRCV_NUM_DEVICES);
65
66 if (unlikely(devno < 0))
67 return devno;
68
69 ir_dev->attr.attrs = ir_dev_attrs;
70 ir_dev->class_dev = device_create(ir_input_class, NULL,
71 input_dev->dev.devt, ir_dev,
72 "irrcv%d", devno);
73 kobj = &ir_dev->class_dev->kobj;
74
75 printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
76 rc = sysfs_create_group(kobj, &ir_dev->attr);
77 if (unlikely(rc < 0)) {
78 device_destroy(ir_input_class, input_dev->dev.devt);
79 return -ENOMEM;
80 }
81
82 ir_dev->devno = devno;
83 set_bit(devno, &ir_core_dev_number);
84
85 return 0;
86};
87
88void ir_unregister_class(struct input_dev *input_dev)
89{
90 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
91 struct kobject *kobj;
92
93 clear_bit(ir_dev->devno, &ir_core_dev_number);
94
95 kobj = &ir_dev->class_dev->kobj;
96
97 sysfs_remove_group(kobj, &ir_dev->attr);
98 device_destroy(ir_input_class, input_dev->dev.devt);
99
100 kfree(ir_dev->attr.name);
101}
102
103static int __init ir_core_init(void)
104{
105 ir_input_class = class_create(THIS_MODULE, "irrcv");
106 if (IS_ERR(ir_input_class)) {
107 printk(KERN_ERR "ir_core: unable to register irrcv class\n");
108 return PTR_ERR(ir_input_class);
109 }
110
111 return 0;
112}
113
114static void __exit ir_core_exit(void)
115{
116 class_destroy(ir_input_class);
117}
118
119module_init(ir_core_init);
120module_exit(ir_core_exit);