blob: d73589ad55e6acf5b8f9103d5b6f8c63df71cb61 [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
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030021/* bit array to represent IR sysfs device number */
22static unsigned long ir_core_dev_number;
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030023
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030024/* class for /sys/class/irrcv */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -030025static struct class *ir_input_class;
26
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030027/**
28 * show_protocol() - shows the current IR protocol
29 * @d: the device descriptor
30 * @mattr: the device attribute struct (unused)
31 * @buf: a pointer to the output buffer
32 *
33 * This routine is a callback routine for input read the IR protocol type.
34 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
35 * It returns the protocol name, as understood by the driver.
36 */
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -030037static ssize_t show_protocol(struct device *d,
38 struct device_attribute *mattr, char *buf)
39{
40 char *s;
41 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
42 enum ir_type ir_type = ir_dev->rc_tab.ir_type;
43
44 IR_dprintk(1, "Current protocol is %ld\n", ir_type);
45
46 /* FIXME: doesn't support multiple protocols at the same time */
47 if (ir_type == IR_TYPE_UNKNOWN)
48 s = "Unknown";
49 else if (ir_type == IR_TYPE_RC5)
50 s = "RC-5";
51 else if (ir_type == IR_TYPE_PD)
52 s = "Pulse/distance";
53 else if (ir_type == IR_TYPE_NEC)
54 s = "NEC";
55 else
56 s = "Other";
57
58 return sprintf(buf, "%s\n", s);
59}
60
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -030061/**
62 * store_protocol() - shows the current IR protocol
63 * @d: the device descriptor
64 * @mattr: the device attribute struct (unused)
65 * @buf: a pointer to the input buffer
66 * @len: length of the input buffer
67 *
68 * This routine is a callback routine for changing the IR protocol type.
69 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
70 * It changes the IR the protocol name, if the IR type is recognized
71 * by the driver.
72 * If an unknown protocol name is used, returns -EINVAL.
73 */
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030074static ssize_t store_protocol(struct device *d,
75 struct device_attribute *mattr,
76 const char *data,
77 size_t len)
78{
79 struct ir_input_dev *ir_dev = dev_get_drvdata(d);
80 enum ir_type ir_type = IR_TYPE_UNKNOWN;
81 int rc = -EINVAL;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -030082 unsigned long flags;
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -030083 char *buf;
84
85 buf = strsep((char **) &data, "\n");
86
87 if (!strcasecmp(buf, "rc-5"))
88 ir_type = IR_TYPE_RC5;
89 else if (!strcasecmp(buf, "pd"))
90 ir_type = IR_TYPE_PD;
91 else if (!strcasecmp(buf, "nec"))
92 ir_type = IR_TYPE_NEC;
93
94 if (ir_type == IR_TYPE_UNKNOWN) {
95 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
96 return -EINVAL;
97 }
98
99 if (ir_dev->props->change_protocol)
100 rc = ir_dev->props->change_protocol(ir_dev->props->priv,
101 ir_type);
102
103 if (rc < 0) {
104 IR_dprintk(1, "Error setting protocol to %ld\n", ir_type);
105 return -EINVAL;
106 }
107
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300108 spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300109 ir_dev->rc_tab.ir_type = ir_type;
Mauro Carvalho Chehabeecee322009-12-14 02:56:15 -0300110 spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300111
112 IR_dprintk(1, "Current protocol is %ld\n", ir_type);
113
114 return len;
115}
116
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300117/*
118 * Static device attribute struct with the sysfs attributes for IR's
119 */
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -0300120static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
Mauro Carvalho Chehab09b01b92009-12-14 02:46:42 -0300121 show_protocol, store_protocol);
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300122
123static struct attribute *ir_dev_attrs[] = {
Mauro Carvalho Chehab53f87022009-12-14 02:16:36 -0300124 &dev_attr_current_protocol.attr,
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300125};
126
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300127/**
128 * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
129 * @input_dev: the struct input_dev descriptor of the device
130 *
131 * This routine is used to register the syfs code for IR class
132 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300133int ir_register_class(struct input_dev *input_dev)
134{
135 int rc;
136 struct kobject *kobj;
137
138 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
139 int devno = find_first_zero_bit(&ir_core_dev_number,
140 IRRCV_NUM_DEVICES);
141
142 if (unlikely(devno < 0))
143 return devno;
144
145 ir_dev->attr.attrs = ir_dev_attrs;
146 ir_dev->class_dev = device_create(ir_input_class, NULL,
147 input_dev->dev.devt, ir_dev,
148 "irrcv%d", devno);
149 kobj = &ir_dev->class_dev->kobj;
150
151 printk(KERN_WARNING "Creating IR device %s\n", kobject_name(kobj));
152 rc = sysfs_create_group(kobj, &ir_dev->attr);
153 if (unlikely(rc < 0)) {
154 device_destroy(ir_input_class, input_dev->dev.devt);
155 return -ENOMEM;
156 }
157
158 ir_dev->devno = devno;
159 set_bit(devno, &ir_core_dev_number);
160
161 return 0;
162};
163
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300164/**
165 * ir_unregister_class() - removes the sysfs for sysfs for
166 * /sys/class/irrcv/irrcv?
167 * @input_dev: the struct input_dev descriptor of the device
168 *
169 * This routine is used to unregister the syfs code for IR class
170 */
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300171void ir_unregister_class(struct input_dev *input_dev)
172{
173 struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
174 struct kobject *kobj;
175
176 clear_bit(ir_dev->devno, &ir_core_dev_number);
177
178 kobj = &ir_dev->class_dev->kobj;
179
180 sysfs_remove_group(kobj, &ir_dev->attr);
181 device_destroy(ir_input_class, input_dev->dev.devt);
182
183 kfree(ir_dev->attr.name);
184}
185
Mauro Carvalho Chehabd4b778d2009-12-14 02:55:03 -0300186/*
187 * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
188 */
189
Mauro Carvalho Chehab4714eda2009-12-13 16:00:08 -0300190static int __init ir_core_init(void)
191{
192 ir_input_class = class_create(THIS_MODULE, "irrcv");
193 if (IS_ERR(ir_input_class)) {
194 printk(KERN_ERR "ir_core: unable to register irrcv class\n");
195 return PTR_ERR(ir_input_class);
196 }
197
198 return 0;
199}
200
201static void __exit ir_core_exit(void)
202{
203 class_destroy(ir_input_class);
204}
205
206module_init(ir_core_init);
207module_exit(ir_core_exit);