blob: aede78ddbace3f352f0697794df2b867e7d42c09 [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
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030050
51static ssize_t store_protocol(struct device *d,
52 struct device_attribute *mattr,
53 const char *data,
54 size_t len)
55{
56 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
57 enum ir_type ir_type = IR_TYPE_UNKNOWN;
58 int rc = -EINVAL;
59 char *buf;
60
61 buf = strsep((char **) &data, "\n");
62
63 if (!strcasecmp(buf, "rc-5"))
64 ir_type = IR_TYPE_RC5;
65 else if (!strcasecmp(buf, "pd"))
66 ir_type = IR_TYPE_PD;
67 else if (!strcasecmp(buf, "nec"))
68 ir_type = IR_TYPE_NEC;
69
70 if (ir_type == IR_TYPE_UNKNOWN) {
71 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
72 return -EINVAL;
73 }
74
75 if (ir_dev->props->change_protocol)
76 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
77 ir_type);
78
79 if (rc < 0) {
80 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
81 return -EINVAL;
82 }
83
84 ir_dev->rc_tab.ir_type = ir_type;
85
86 IR_dprintk(1, "Current protocol is %ld\n", ir_type);
87
88 return len;
89}
90
91
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030092static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030093 show_protocol, store_protocol);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030094
95static struct attribute *ir_dev_attrs[] = {
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030096 &dev_attr_current_protocol.attr,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030097};
98
99int ir_register_class(struct input_dev *input_dev)
100{
101 int rc;
102 struct kobject *kobj;
103
104 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
105 int devno = find_first_zero_bit(&ir_core_dev_number,
106 IRRCV_NUM_DEVICES);
107
108 if (unlikely(devno < 0))
109 return devno;
110
111 ir_dev->attr.attrs = ir_dev_attrs;
112 ir_dev->class_dev = device_create(ir_input_class, NULL,
113 input_dev->dev.devt, ir_dev,
114 "irrcv%d", devno);
115 kobj = &ir_dev->class_dev->kobj;
116
117 printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
118 rc = sysfs_create_group(kobj, &ir_dev->attr);
119 if (unlikely(rc < 0)) {
120 device_destroy(ir_input_class, input_dev->dev.devt);
121 return -ENOMEM;
122 }
123
124 ir_dev->devno = devno;
125 set_bit(devno, &ir_core_dev_number);
126
127 return 0;
128};
129
130void ir_unregister_class(struct input_dev *input_dev)
131{
132 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
133 struct kobject *kobj;
134
135 clear_bit(ir_dev->devno, &ir_core_dev_number);
136
137 kobj = &ir_dev->class_dev->kobj;
138
139 sysfs_remove_group(kobj, &ir_dev->attr);
140 device_destroy(ir_input_class, input_dev->dev.devt);
141
142 kfree(ir_dev->attr.name);
143}
144
145static int __init ir_core_init(void)
146{
147 ir_input_class = class_create(THIS_MODULE, "irrcv");
148 if (IS_ERR(ir_input_class)) {
149 printk(KERN_ERR "ir_core: unable to register irrcv class\n");
150 return PTR_ERR(ir_input_class);
151 }
152
153 return 0;
154}
155
156static void __exit ir_core_exit(void)
157{
158 class_destroy(ir_input_class);
159}
160
161module_init(ir_core_init);
162module_exit(ir_core_exit);