ir-sysfs.c 6.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* ir-register.c - handle IR scancode->keycode tables
 *
 * Copyright (C) 2009 by Mauro Carvalho Chehab <mchehab@redhat.com>
 *
 * 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 18 19 20 21
#include <linux/input.h>
#include <linux/device.h>
#include <media/ir-core.h>

#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/irrcv */
26 27 28 29 30 31 32 33 34
static char *ir_devnode(struct device *dev, mode_t *mode)
{
	return kasprintf(GFP_KERNEL, "irrcv/%s", dev_name(dev));
}

struct class ir_input_class = {
	.name		= "irrcv",
	.devnode	= ir_devnode,
};
35

36 37 38 39 40 41 42 43 44 45
/**
 * 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.
 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
 * 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 59 60 61 62 63 64 65 66 67 68 69

	/* 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)
		s = "RC-5";
	else if (ir_type == IR_TYPE_PD)
		s = "Pulse/distance";
	else if (ir_type == IR_TYPE_NEC)
		s = "NEC";
	else
		s = "Other";

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

70 71 72 73 74 75 76 77 78 79 80 81 82
/**
 * 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.
 * it is trigged by reading /sys/class/irrcv/irrcv?/current_protocol.
 * 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.
 */
83 84 85 86 87 88
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);
89
	u64 ir_type = IR_TYPE_UNKNOWN;
90
	int rc = -EINVAL;
91
	unsigned long flags;
92 93 94 95 96 97 98 99 100 101 102 103
	char *buf;

	buf = strsep((char **) &data, "\n");

	if (!strcasecmp(buf, "rc-5"))
		ir_type = IR_TYPE_RC5;
	else if (!strcasecmp(buf, "pd"))
		ir_type = IR_TYPE_PD;
	else if (!strcasecmp(buf, "nec"))
		ir_type = IR_TYPE_NEC;

	if (ir_type == IR_TYPE_UNKNOWN) {
104 105
		IR_dprintk(1, "Error setting protocol to %lld\n",
			   (long long)ir_type);
106 107 108
		return -EINVAL;
	}

109
	if (ir_dev->props && ir_dev->props->change_protocol)
110 111 112 113
		rc = ir_dev->props->change_protocol(ir_dev->props->priv,
						    ir_type);

	if (rc < 0) {
114 115
		IR_dprintk(1, "Error setting protocol to %lld\n",
			   (long long)ir_type);
116 117 118
		return -EINVAL;
	}

119
	spin_lock_irqsave(&ir_dev->rc_tab.lock, flags);
120
	ir_dev->rc_tab.ir_type = ir_type;
121
	spin_unlock_irqrestore(&ir_dev->rc_tab.lock, flags);
122

123 124
	IR_dprintk(1, "Current protocol is %lld\n",
		   (long long)ir_type);
125 126 127 128

	return len;
}

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

#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)
		ADD_HOTPLUG_VAR("NAME=\"%s\"", ir_dev->rc_tab.name);

	return 0;
}

147 148 149
/*
 * Static device attribute struct with the sysfs attributes for IR's
 */
150
static DEVICE_ATTR(current_protocol, S_IRUGO | S_IWUSR,
151
		   show_protocol, store_protocol);
152 153

static struct attribute *ir_dev_attrs[] = {
154
	&dev_attr_current_protocol.attr,
155
	NULL,
156 157
};

158 159 160 161 162 163 164 165 166 167 168
static struct attribute_group ir_dev_attr_grp = {
	.attrs	= ir_dev_attrs,
};

static const struct attribute_group *ir_dev_attr_groups[] = {
	&ir_dev_attr_grp,
	NULL
};

static struct device_type ir_dev_type = {
	.groups		= ir_dev_attr_groups,
169
	.uevent		= ir_dev_uevent,
170 171
};

172 173 174 175 176 177
/**
 * ir_register_class() - creates the sysfs for /sys/class/irrcv/irrcv?
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to register the syfs code for IR class
 */
178 179 180
int ir_register_class(struct input_dev *input_dev)
{
	int rc;
181
	const char *path;
182 183 184 185 186 187 188 189

	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;

190 191 192 193
	ir_dev->dev.type = &ir_dev_type;
	ir_dev->dev.class = &ir_input_class;
	ir_dev->dev.parent = input_dev->dev.parent;
	dev_set_name(&ir_dev->dev, "irrcv%d", devno);
194
	dev_set_drvdata(&ir_dev->dev, ir_dev);
195 196 197 198 199 200 201 202 203 204
	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;
205 206
	}

207 208
	__module_get(THIS_MODULE);

209 210
	path = kobject_get_path(&ir_dev->dev.kobj, GFP_KERNEL);
	printk(KERN_INFO "%s: %s as %s\n",
211 212 213 214 215
		dev_name(&ir_dev->dev),
		input_dev->name ? input_dev->name : "Unspecified device",
		path ? path : "N/A");
	kfree(path);

216 217 218 219 220 221
	ir_dev->devno = devno;
	set_bit(devno, &ir_core_dev_number);

	return 0;
};

222 223 224 225 226 227 228
/**
 * ir_unregister_class() - removes the sysfs for sysfs for
 *			   /sys/class/irrcv/irrcv?
 * @input_dev:	the struct input_dev descriptor of the device
 *
 * This routine is used to unregister the syfs code for IR class
 */
229 230 231 232 233
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);
234 235
	input_unregister_device(input_dev);
	device_del(&ir_dev->dev);
236

237
	module_put(THIS_MODULE);
238 239
}

240 241 242 243
/*
 * Init/exit code for the module. Basically, creates/removes /sys/class/irrcv
 */

244 245
static int __init ir_core_init(void)
{
246 247
	int rc = class_register(&ir_input_class);
	if (rc) {
248
		printk(KERN_ERR "ir_core: unable to register irrcv class\n");
249
		return rc;
250 251 252 253 254 255 256
	}

	return 0;
}

static void __exit ir_core_exit(void)
{
257
	class_unregister(&ir_input_class);
258 259 260 261
}

module_init(ir_core_init);
module_exit(ir_core_exit);