ir-sysfs.c 7.9 KB
Newer Older
1 2
/* ir-register.c - handle IR scancode->keycode tables
 *
3
 * Copyright (C) 2009-2010 by Mauro Carvalho Chehab <mchehab@redhat.com>
4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

15
#include <linux/slab.h>
16 17
#include <linux/input.h>
#include <linux/device.h>
18
#include "ir-core-priv.h"
19 20 21

#define IRRCV_NUM_DEVICES	256

22 23
/* bit array to represent IR sysfs device number */
static unsigned long ir_core_dev_number;
24

25
/* class for /sys/class/rc */
26 27
static char *ir_devnode(struct device *dev, mode_t *mode)
{
28
	return kasprintf(GFP_KERNEL, "rc/%s", dev_name(dev));
29 30
}

31
static struct class ir_input_class = {
32
	.name		= "rc",
33 34
	.devnode	= ir_devnode,
};
35

36 37 38 39 40 41 42
/**
 * show_protocol() - shows the current IR protocol
 * @d:		the device descriptor
 * @mattr:	the device attribute struct (unused)
 * @buf:	a pointer to the output buffer
 *
 * This routine is a callback routine for input read the IR protocol type.
43
 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
44 45
 * It returns the protocol name, as understood by the driver.
 */
46 47 48 49 50
static ssize_t show_protocol(struct device *d,
			     struct device_attribute *mattr, char *buf)
{
	char *s;
	struct ir_input_dev *ir_dev = dev_get_drvdata(d);
51
	u64 ir_type = ir_dev->rc_tab.ir_type;
52

53
	IR_dprintk(1, "Current protocol is %lld\n", (long long)ir_type);
54 55 56 57 58

	/* FIXME: doesn't support multiple protocols at the same time */
	if (ir_type == IR_TYPE_UNKNOWN)
		s = "Unknown";
	else if (ir_type == IR_TYPE_RC5)
59
		s = "rc-5";
60
	else if (ir_type == IR_TYPE_PD)
61
		s = "pulse-distance";
62
	else if (ir_type == IR_TYPE_NEC)
63
		s = "nec";
64 65
	else if (ir_type == IR_TYPE_RC6)
		s = "rc6";
66 67
	else if (ir_type == IR_TYPE_JVC)
		s = "jvc";
68 69
	else if (ir_type == IR_TYPE_SONY)
		s = "sony";
70
	else
71
		s = "other";
72 73 74 75

	return sprintf(buf, "%s\n", s);
}

76 77 78 79 80 81 82 83
/**
 * store_protocol() - shows the current IR protocol
 * @d:		the device descriptor
 * @mattr:	the device attribute struct (unused)
 * @buf:	a pointer to the input buffer
 * @len:	length of the input buffer
 *
 * This routine is a callback routine for changing the IR protocol type.
84
 * it is trigged by reading /sys/class/rc/rc?/current_protocol.
85 86 87 88
 * It changes the IR the protocol name, if the IR type is recognized
 * by the driver.
 * If an unknown protocol name is used, returns -EINVAL.
 */
89 90 91 92 93 94
static ssize_t store_protocol(struct device *d,
			      struct device_attribute *mattr,
			      const char *data,
			      size_t len)
{
	struct ir_input_dev *ir_dev = dev_get_drvdata(d);
95
	u64 ir_type = 0;
96
	int rc = -EINVAL;
97
	unsigned long flags;
98 99
	char *buf;

100
	while ((buf = strsep((char **) &data, " \n")) != NULL) {
101 102 103 104 105 106
		if (!strcasecmp(buf, "rc-5") || !strcasecmp(buf, "rc5"))
			ir_type |= IR_TYPE_RC5;
		if (!strcasecmp(buf, "pd") || !strcasecmp(buf, "pulse-distance"))
			ir_type |= IR_TYPE_PD;
		if (!strcasecmp(buf, "nec"))
			ir_type |= IR_TYPE_NEC;
107 108
		if (!strcasecmp(buf, "jvc"))
			ir_type |= IR_TYPE_JVC;
109 110
		if (!strcasecmp(buf, "sony"))
			ir_type |= IR_TYPE_SONY;
111
	}
112

113 114
	if (!ir_type) {
		IR_dprintk(1, "Unknown protocol\n");
115 116 117
		return -EINVAL;
	}

118
	if (ir_dev->props && ir_dev->props->change_protocol)
119 120 121 122
		rc = ir_dev->props->change_protocol(ir_dev->props->priv,
						    ir_type);

	if (rc < 0) {
123 124
		IR_dprintk(1, "Error setting protocol to %lld\n",
			   (long long)ir_type);
125 126 127
		return -EINVAL;
	}

128
	spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
129
	ir_dev->rc_tab.ir_type = ir_type;
130
	spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
131

132
	IR_dprintk(1, "Current protocol(s) is(are) %lld\n",
133
		   (long long)ir_type);
134 135 136 137

	return len;
}

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
static ssize_t show_supported_protocols(struct device *d,
			     struct device_attribute *mattr, char *buf)
{
	char *orgbuf = buf;
	struct ir_input_dev *ir_dev = dev_get_drvdata(d);

	/* FIXME: doesn't support multiple protocols at the same time */
	if (ir_dev->props->allowed_protos == IR_TYPE_UNKNOWN)
		buf += sprintf(buf, "unknown ");
	if (ir_dev->props->allowed_protos & IR_TYPE_RC5)
		buf += sprintf(buf, "rc-5 ");
	if (ir_dev->props->allowed_protos & IR_TYPE_PD)
		buf += sprintf(buf, "pulse-distance ");
	if (ir_dev->props->allowed_protos & IR_TYPE_NEC)
		buf += sprintf(buf, "nec ");
	if (buf == orgbuf)
		buf += sprintf(buf, "other ");

	buf += sprintf(buf - 1, "\n");

	return buf - orgbuf;
}
160 161 162 163 164 165 166 167 168 169 170 171 172

#define ADD_HOTPLUG_VAR(fmt, val...)					\
	do {								\
		int err = add_uevent_var(env, fmt, val);		\
		if (err)						\
			return err;					\
	} while (0)

static int ir_dev_uevent(struct device *device, struct kobj_uevent_env *env)
{
	struct ir_input_dev *ir_dev = dev_get_drvdata(device);

	if (ir_dev->rc_tab.name)
173
		ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
174
	if (ir_dev->driver_name)
175
		ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
176 177 178 179

	return 0;
}

180 181 182
/*
 * Static device attribute struct with the sysfs attributes for IR's
 */
183
static DEVICE_ATTR(protocol, S_IRUGO | S_IWUSR,
184
		   show_protocol, store_protocol);
185

186 187 188
static DEVICE_ATTR(supported_protocols, S_IRUGO | S_IWUSR,
		   show_supported_protocols, NULL);

189
static struct attribute *ir_hw_dev_attrs[] = {
190 191
	&dev_attr_protocol.attr,
	&dev_attr_supported_protocols.attr,
192
	NULL,
193 194
};

195 196
static struct attribute_group ir_hw_dev_attr_grp = {
	.attrs	= ir_hw_dev_attrs,
197 198
};

199 200
static const struct attribute_group *ir_hw_dev_attr_groups[] = {
	&ir_hw_dev_attr_grp,
201 202 203
	NULL
};

204 205 206 207 208 209
static struct device_type rc_dev_type = {
	.groups		= ir_hw_dev_attr_groups,
	.uevent		= ir_dev_uevent,
};

static struct device_type ir_raw_dev_type = {
210
	.uevent		= ir_dev_uevent,
211 212
};

213
/**
214
 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
215 216 217 218
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to register the syfs code for IR class
 */
219 220 221
int ir_register_class(struct input_dev *input_dev)
{
	int rc;
222
	const char *path;
223 224 225 226 227 228 229
	struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);
	int devno = find_first_zero_bit(&ir_core_dev_number,
					IRRCV_NUM_DEVICES);

	if (unlikely(devno < 0))
		return devno;

230 231 232 233 234
	if (ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
		ir_dev->dev.type = &rc_dev_type;
	else
		ir_dev->dev.type = &ir_raw_dev_type;

235 236
	ir_dev->dev.class = &ir_input_class;
	ir_dev->dev.parent = input_dev->dev.parent;
237
	dev_set_name(&ir_dev->dev, "rc%d", devno);
238
	dev_set_drvdata(&ir_dev->dev, ir_dev);
239 240 241 242 243 244 245 246 247 248
	rc = device_register(&ir_dev->dev);
	if (rc)
		return rc;


	input_dev->dev.parent = &ir_dev->dev;
	rc = input_register_device(input_dev);
	if (rc < 0) {
		device_del(&ir_dev->dev);
		return rc;
249 250
	}

251 252
	__module_get(THIS_MODULE);

253 254
	path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
	printk(KERN_INFO "%s: %s as %s\n",
255 256 257 258 259
		dev_name(&ir_dev->dev),
		input_dev->name ? input_dev->name : "Unspecified device",
		path ? path : "N/A");
	kfree(path);

260 261 262 263 264 265
	ir_dev->devno = devno;
	set_bit(devno, &ir_core_dev_number);

	return 0;
};

266 267
/**
 * ir_unregister_class() - removes the sysfs for sysfs for
268
 *			   /sys/class/rc/rc?
269 270 271 272
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to unregister the syfs code for IR class
 */
273 274 275 276 277
void ir_unregister_class(struct input_dev *input_dev)
{
	struct ir_input_dev *ir_dev = input_get_drvdata(input_dev);

	clear_bit(ir_dev->devno, &ir_core_dev_number);
278 279
	input_unregister_device(input_dev);
	device_del(&ir_dev->dev);
280

281
	module_put(THIS_MODULE);
282 283
}

284
/*
285
 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
286 287
 */

288 289
static int __init ir_core_init(void)
{
290 291
	int rc = class_register(&ir_input_class);
	if (rc) {
292
		printk(KERN_ERR "ir_core: unable to register rc class\n");
293
		return rc;
294 295
	}

296
	/* Initialize/load the decoders/keymap code that will be used */
297 298
	ir_raw_init();

299 300 301 302 303
	return 0;
}

static void __exit ir_core_exit(void)
{
304
	class_unregister(&ir_input_class);
305 306 307 308
}

module_init(ir_core_init);
module_exit(ir_core_exit);