ir-sysfs.c 8.5 KB
Newer Older
1
/* ir-sysfs.c - sysfs interface for RC devices (/sys/class/rc)
2
 *
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
static struct {
	u64	type;
	char	*name;
} proto_names[] = {
	{ IR_TYPE_UNKNOWN,	"unknown"	},
41
	{ IR_TYPE_RC5,		"rc-5"		},
42
	{ IR_TYPE_NEC,		"nec"		},
43
	{ IR_TYPE_RC6,		"rc-6"		},
44 45
	{ IR_TYPE_JVC,		"jvc"		},
	{ IR_TYPE_SONY,		"sony"		},
46
	{ IR_TYPE_LIRC,		"lirc"		},
47 48
};

49 50
#define PROTO_NONE	"none"

51
/**
52
 * show_protocols() - shows the current IR protocol(s)
53 54 55 56
 * @d:		the device descriptor
 * @mattr:	the device attribute struct (unused)
 * @buf:	a pointer to the output buffer
 *
57 58 59 60
 * This routine is a callback routine for input read the IR protocol type(s).
 * it is trigged by reading /sys/class/rc/rc?/protocols.
 * It returns the protocol names of supported protocols.
 * Enabled protocols are printed in brackets.
61
 */
62 63
static ssize_t show_protocols(struct device *d,
			      struct device_attribute *mattr, char *buf)
64 65
{
	struct ir_input_dev *ir_dev = dev_get_drvdata(d);
66 67
	u64 allowed, enabled;
	char *tmp = buf;
68
	int i;
69

70
	if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
71 72
		enabled = ir_dev->rc_tab.ir_type;
		allowed = ir_dev->props->allowed_protos;
73
	} else if (ir_dev->raw) {
74 75
		enabled = ir_dev->raw->enabled_protocols;
		allowed = ir_raw_get_allowed_protocols();
76 77
	} else
		return sprintf(tmp, "[builtin]\n");
78

79 80 81 82
	IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n",
		   (long long)allowed,
		   (long long)enabled);

83 84 85 86 87 88
	for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
		if (allowed & enabled & proto_names[i].type)
			tmp += sprintf(tmp, "[%s] ", proto_names[i].name);
		else if (allowed & proto_names[i].type)
			tmp += sprintf(tmp, "%s ", proto_names[i].name);
	}
89 90 91 92 93

	if (tmp != buf)
		tmp--;
	*tmp = '\n';
	return tmp + 1 - buf;
94 95
}

96
/**
97
 * store_protocols() - changes the current IR protocol(s)
98 99 100 101 102 103
 * @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.
104 105 106 107
 * It is trigged by writing to /sys/class/rc/rc?/protocols.
 * Writing "+proto" will add a protocol to the list of enabled protocols.
 * Writing "-proto" will remove a protocol from the list of enabled protocols.
 * Writing "proto" will enable only "proto".
108
 * Writing "none" will disable all protocols.
109 110
 * Returns -EINVAL if an invalid protocol combination or unknown protocol name
 * is used, otherwise @len.
111
 */
112 113 114 115
static ssize_t store_protocols(struct device *d,
			       struct device_attribute *mattr,
			       const char *data,
			       size_t len)
116 117
{
	struct ir_input_dev *ir_dev = dev_get_drvdata(d);
118 119 120 121
	bool enable, disable;
	const char *tmp;
	u64 type;
	u64 mask;
122
	int rc, i, count = 0;
123
	unsigned long flags;
124

125
	if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE)
126
		type = ir_dev->rc_tab.ir_type;
127
	else if (ir_dev->raw)
128
		type = ir_dev->raw->enabled_protocols;
129 130 131 132
	else {
		IR_dprintk(1, "Protocol switching not supported\n");
		return -EINVAL;
	}
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	while ((tmp = strsep((char **) &data, " \n")) != NULL) {
		if (!*tmp)
			break;

		if (*tmp == '+') {
			enable = true;
			disable = false;
			tmp++;
		} else if (*tmp == '-') {
			enable = false;
			disable = true;
			tmp++;
		} else {
			enable = false;
			disable = false;
		}
150

151 152 153 154 155 156 157 158 159 160 161
		if (!enable && !disable && !strncasecmp(tmp, PROTO_NONE, sizeof(PROTO_NONE))) {
			tmp += sizeof(PROTO_NONE);
			mask = 0;
			count++;
		} else {
			for (i = 0; i < ARRAY_SIZE(proto_names); i++) {
				if (!strncasecmp(tmp, proto_names[i].name, strlen(proto_names[i].name))) {
					tmp += strlen(proto_names[i].name);
					mask = proto_names[i].type;
					break;
				}
162
			}
163 164 165 166 167
			if (i == ARRAY_SIZE(proto_names)) {
				IR_dprintk(1, "Unknown protocol: '%s'\n", tmp);
				return -EINVAL;
			}
			count++;
168
		}
169 170 171 172 173 174 175

		if (enable)
			type |= mask;
		else if (disable)
			type &= ~mask;
		else
			type = mask;
176 177
	}

178 179
	if (!count) {
		IR_dprintk(1, "Protocol not specified\n");
180 181 182
		return -EINVAL;
	}

183 184 185 186 187 188 189 190 191
	if (ir_dev->props && ir_dev->props->change_protocol) {
		rc = ir_dev->props->change_protocol(ir_dev->props->priv,
						    type);
		if (rc < 0) {
			IR_dprintk(1, "Error setting protocols to 0x%llx\n",
				   (long long)type);
			return -EINVAL;
		}
	}
192

193
	if (ir_dev->props && ir_dev->props->driver_type == RC_DRIVER_SCANCODE) {
194 195 196 197 198 199
		spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
		ir_dev->rc_tab.ir_type = type;
		spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
	} else {
		ir_dev->raw->enabled_protocols = type;
	}
200

201 202
	IR_dprintk(1, "Current protocol(s): 0x%llx\n",
		   (long long)type);
203

204
	return len;
205
}
206 207 208 209 210 211 212 213

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

214
static int rc_dev_uevent(struct device *device, struct kobj_uevent_env *env)
215 216 217 218
{
	struct ir_input_dev *ir_dev = dev_get_drvdata(device);

	if (ir_dev->rc_tab.name)
219
		ADD_HOTPLUG_VAR("NAME=%s", ir_dev->rc_tab.name);
220
	if (ir_dev->driver_name)
221
		ADD_HOTPLUG_VAR("DRV_NAME=%s", ir_dev->driver_name);
222 223 224 225

	return 0;
}

226 227 228
/*
 * Static device attribute struct with the sysfs attributes for IR's
 */
229 230
static DEVICE_ATTR(protocols, S_IRUGO | S_IWUSR,
		   show_protocols, store_protocols);
231

232 233
static struct attribute *rc_dev_attrs[] = {
	&dev_attr_protocols.attr,
234
	NULL,
235 236
};

237 238
static struct attribute_group rc_dev_attr_grp = {
	.attrs	= rc_dev_attrs,
239 240
};

241 242
static const struct attribute_group *rc_dev_attr_groups[] = {
	&rc_dev_attr_grp,
243 244 245
	NULL
};

246
static struct device_type rc_dev_type = {
247 248
	.groups		= rc_dev_attr_groups,
	.uevent		= rc_dev_uevent,
249 250
};

251
/**
252
 * ir_register_class() - creates the sysfs for /sys/class/rc/rc?
253 254 255 256
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to register the syfs code for IR class
 */
257 258 259
int ir_register_class(struct input_dev *input_dev)
{
	int rc;
260
	const char *path;
261 262 263 264 265 266 267
	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;

268
	ir_dev->dev.type = &rc_dev_type;
269

270 271
	ir_dev->dev.class = &ir_input_class;
	ir_dev->dev.parent = input_dev->dev.parent;
272
	dev_set_name(&ir_dev->dev, "rc%d", devno);
273
	dev_set_drvdata(&ir_dev->dev, ir_dev);
274 275 276 277 278 279 280 281 282 283
	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;
284 285
	}

286 287
	__module_get(THIS_MODULE);

288 289
	path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
	printk(KERN_INFO "%s: %s as %s\n",
290 291 292 293 294
		dev_name(&ir_dev->dev),
		input_dev->name ? input_dev->name : "Unspecified device",
		path ? path : "N/A");
	kfree(path);

295 296 297 298 299 300
	ir_dev->devno = devno;
	set_bit(devno, &ir_core_dev_number);

	return 0;
};

301 302
/**
 * ir_unregister_class() - removes the sysfs for sysfs for
303
 *			   /sys/class/rc/rc?
304 305 306 307
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to unregister the syfs code for IR class
 */
308 309 310 311 312
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);
313 314
	input_unregister_device(input_dev);
	device_del(&ir_dev->dev);
315

316
	module_put(THIS_MODULE);
317 318
}

319
/*
320
 * Init/exit code for the module. Basically, creates/removes /sys/class/rc
321 322
 */

323 324
static int __init ir_core_init(void)
{
325 326
	int rc = class_register(&ir_input_class);
	if (rc) {
327
		printk(KERN_ERR "ir_core: unable to register rc class\n");
328
		return rc;
329 330
	}

331
	/* Initialize/load the decoders/keymap code that will be used */
332
	ir_raw_init();
333
	ir_rcmap_init();
334

335 336 337 338 339
	return 0;
}

static void __exit ir_core_exit(void)
{
340
	class_unregister(&ir_input_class);
341
	ir_rcmap_cleanup();
342 343 344 345
}

module_init(ir_core_init);
module_exit(ir_core_exit);