industrialio-event.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Industrial I/O event handling
 *
 * Copyright (c) 2008 Jonathan Cameron
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * Based on elements of hwmon and input subsystems.
 */

#include <linux/anon_inodes.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/kernel.h>
16
#include <linux/kfifo.h>
17
#include <linux/module.h>
18
#include <linux/poll.h>
19 20 21 22
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/wait.h>
23
#include <linux/iio/iio.h>
24
#include "iio_core.h"
25 26
#include <linux/iio/sysfs.h>
#include <linux/iio/events.h>
27 28 29 30 31 32 33 34 35 36 37

/**
 * struct iio_event_interface - chrdev interface for an event line
 * @wait:		wait queue to allow blocking reads of events
 * @det_events:		list of detected events
 * @dev_attr_list:	list of event interface sysfs attribute
 * @flags:		file operations related flags including busy flag.
 * @group:		event interface sysfs attribute group
 */
struct iio_event_interface {
	wait_queue_head_t	wait;
38 39
	DECLARE_KFIFO(det_events, struct iio_event_data, 16);

40 41 42 43 44
	struct list_head	dev_attr_list;
	unsigned long		flags;
	struct attribute_group	group;
};

45 46 47 48 49 50
/**
 * iio_push_event() - try to add event to the list for userspace reading
 * @indio_dev:		IIO device structure
 * @ev_code:		What event
 * @timestamp:		When the event occurred
 **/
51 52 53
int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
{
	struct iio_event_interface *ev_int = indio_dev->event_interface;
54
	struct iio_event_data ev;
55
	unsigned long flags;
56
	int copied;
57 58

	/* Does anyone care? */
59
	spin_lock_irqsave(&ev_int->wait.lock, flags);
60 61
	if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {

62 63 64
		ev.id = ev_code;
		ev.timestamp = timestamp;

S
Stefani Seibold 已提交
65
		copied = kfifo_put(&ev_int->det_events, ev);
66
		if (copied != 0)
67
			wake_up_locked_poll(&ev_int->wait, POLLIN);
68
	}
69
	spin_unlock_irqrestore(&ev_int->wait.lock, flags);
70

71
	return 0;
72 73 74
}
EXPORT_SYMBOL(iio_push_event);

75 76 77 78 79 80
/**
 * iio_event_poll() - poll the event queue to find out if it has data
 */
static unsigned int iio_event_poll(struct file *filep,
			     struct poll_table_struct *wait)
{
81 82
	struct iio_dev *indio_dev = filep->private_data;
	struct iio_event_interface *ev_int = indio_dev->event_interface;
83 84
	unsigned int events = 0;

85 86 87
	if (!indio_dev->info)
		return -ENODEV;

88 89
	poll_wait(filep, &ev_int->wait, wait);

90
	spin_lock_irq(&ev_int->wait.lock);
91 92
	if (!kfifo_is_empty(&ev_int->det_events))
		events = POLLIN | POLLRDNORM;
93
	spin_unlock_irq(&ev_int->wait.lock);
94 95 96 97

	return events;
}

98 99 100 101 102
static ssize_t iio_event_chrdev_read(struct file *filep,
				     char __user *buf,
				     size_t count,
				     loff_t *f_ps)
{
103 104
	struct iio_dev *indio_dev = filep->private_data;
	struct iio_event_interface *ev_int = indio_dev->event_interface;
105
	unsigned int copied;
106 107
	int ret;

108 109 110
	if (!indio_dev->info)
		return -ENODEV;

111
	if (count < sizeof(struct iio_event_data))
112 113
		return -EINVAL;

114
	spin_lock_irq(&ev_int->wait.lock);
115
	if (kfifo_is_empty(&ev_int->det_events)) {
116 117
		if (filep->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
118
			goto error_unlock;
119 120
		}
		/* Blocking on device; waiting for something to be there */
121
		ret = wait_event_interruptible_locked_irq(ev_int->wait,
122 123
					!kfifo_is_empty(&ev_int->det_events) ||
					indio_dev->info == NULL);
124
		if (ret)
125
			goto error_unlock;
126 127 128 129
		if (indio_dev->info == NULL) {
			ret = -ENODEV;
			goto error_unlock;
		}
130 131 132
		/* Single access device so no one else can get the data */
	}

133
	ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
134

135
error_unlock:
136
	spin_unlock_irq(&ev_int->wait.lock);
137

138
	return ret ? ret : copied;
139 140 141 142
}

static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
{
143 144
	struct iio_dev *indio_dev = filep->private_data;
	struct iio_event_interface *ev_int = indio_dev->event_interface;
145

146
	spin_lock_irq(&ev_int->wait.lock);
147
	__clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
148 149 150 151 152
	/*
	 * In order to maintain a clean state for reopening,
	 * clear out any awaiting events. The mask will prevent
	 * any new __iio_push_event calls running.
	 */
153
	kfifo_reset_out(&ev_int->det_events);
154
	spin_unlock_irq(&ev_int->wait.lock);
155

156 157
	iio_device_put(indio_dev);

158 159 160 161 162
	return 0;
}

static const struct file_operations iio_event_chrdev_fileops = {
	.read =  iio_event_chrdev_read,
163
	.poll =  iio_event_poll,
164 165 166 167 168 169 170 171 172 173 174 175 176
	.release = iio_event_chrdev_release,
	.owner = THIS_MODULE,
	.llseek = noop_llseek,
};

int iio_event_getfd(struct iio_dev *indio_dev)
{
	struct iio_event_interface *ev_int = indio_dev->event_interface;
	int fd;

	if (ev_int == NULL)
		return -ENODEV;

177
	spin_lock_irq(&ev_int->wait.lock);
178
	if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
179
		spin_unlock_irq(&ev_int->wait.lock);
180 181
		return -EBUSY;
	}
182
	spin_unlock_irq(&ev_int->wait.lock);
183 184 185
	iio_device_get(indio_dev);

	fd = anon_inode_getfd("iio:event", &iio_event_chrdev_fileops,
186
				indio_dev, O_RDONLY | O_CLOEXEC);
187
	if (fd < 0) {
188
		spin_lock_irq(&ev_int->wait.lock);
189
		__clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
190
		spin_unlock_irq(&ev_int->wait.lock);
191
		iio_device_put(indio_dev);
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	}
	return fd;
}

static const char * const iio_ev_type_text[] = {
	[IIO_EV_TYPE_THRESH] = "thresh",
	[IIO_EV_TYPE_MAG] = "mag",
	[IIO_EV_TYPE_ROC] = "roc",
	[IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
	[IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
};

static const char * const iio_ev_dir_text[] = {
	[IIO_EV_DIR_EITHER] = "either",
	[IIO_EV_DIR_RISING] = "rising",
	[IIO_EV_DIR_FALLING] = "falling"
};

210 211 212
static const char * const iio_ev_info_text[] = {
	[IIO_EV_INFO_ENABLE] = "en",
	[IIO_EV_INFO_VALUE] = "value",
213
	[IIO_EV_INFO_HYSTERESIS] = "hysteresis",
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
};

static enum iio_event_direction iio_ev_attr_dir(struct iio_dev_attr *attr)
{
	return attr->c->event_spec[attr->address & 0xffff].dir;
}

static enum iio_event_type iio_ev_attr_type(struct iio_dev_attr *attr)
{
	return attr->c->event_spec[attr->address & 0xffff].type;
}

static enum iio_event_info iio_ev_attr_info(struct iio_dev_attr *attr)
{
	return (attr->address >> 16) & 0xffff;
}

231 232 233 234 235
static ssize_t iio_ev_state_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf,
				  size_t len)
{
L
Lars-Peter Clausen 已提交
236
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
237 238 239 240 241 242 243 244
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
	int ret;
	bool val;

	ret = strtobool(buf, &val);
	if (ret < 0)
		return ret;

245 246 247
	ret = indio_dev->info->write_event_config(indio_dev,
		this_attr->c, iio_ev_attr_type(this_attr),
		iio_ev_attr_dir(this_attr), val);
248

249 250 251 252 253 254 255
	return (ret < 0) ? ret : len;
}

static ssize_t iio_ev_state_show(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
L
Lars-Peter Clausen 已提交
256
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
257
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
258
	int val;
259

260 261 262
	val = indio_dev->info->read_event_config(indio_dev,
		this_attr->c, iio_ev_attr_type(this_attr),
		iio_ev_attr_dir(this_attr));
263 264 265 266 267 268 269 270 271 272
	if (val < 0)
		return val;
	else
		return sprintf(buf, "%d\n", val);
}

static ssize_t iio_ev_value_show(struct device *dev,
				 struct device_attribute *attr,
				 char *buf)
{
L
Lars-Peter Clausen 已提交
273
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
274
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
275 276
	int val, val2;
	int ret;
277

278 279 280 281 282 283 284
	ret = indio_dev->info->read_event_value(indio_dev,
		this_attr->c, iio_ev_attr_type(this_attr),
		iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
		&val, &val2);
	if (ret < 0)
		return ret;
	return iio_format_value(buf, ret, val, val2);
285 286 287 288 289 290 291
}

static ssize_t iio_ev_value_store(struct device *dev,
				  struct device_attribute *attr,
				  const char *buf,
				  size_t len)
{
L
Lars-Peter Clausen 已提交
292
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
293
	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
294
	int val, val2;
295 296
	int ret;

297
	if (!indio_dev->info->write_event_value)
298 299
		return -EINVAL;

300 301 302 303 304 305 306
	ret = iio_str_to_fixpoint(buf, 100000, &val, &val2);
	if (ret)
		return ret;
	ret = indio_dev->info->write_event_value(indio_dev,
		this_attr->c, iio_ev_attr_type(this_attr),
		iio_ev_attr_dir(this_attr), iio_ev_attr_info(this_attr),
		val, val2);
307 308 309 310 311 312
	if (ret < 0)
		return ret;

	return len;
}

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
static int iio_device_add_event(struct iio_dev *indio_dev,
	const struct iio_chan_spec *chan, unsigned int spec_index,
	enum iio_event_type type, enum iio_event_direction dir,
	enum iio_shared_by shared_by, const unsigned long *mask)
{
	ssize_t (*show)(struct device *, struct device_attribute *, char *);
	ssize_t (*store)(struct device *, struct device_attribute *,
		const char *, size_t);
	unsigned int attrcount = 0;
	unsigned int i;
	char *postfix;
	int ret;

	for_each_set_bit(i, mask, sizeof(*mask)) {
		postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
				iio_ev_type_text[type], iio_ev_dir_text[dir],
				iio_ev_info_text[i]);
		if (postfix == NULL)
			return -ENOMEM;

		if (i == IIO_EV_INFO_ENABLE) {
			show = iio_ev_state_show;
			store = iio_ev_state_store;
		} else {
			show = iio_ev_value_show;
			store = iio_ev_value_store;
		}

		ret = __iio_add_chan_devattr(postfix, chan, show, store,
			 (i << 16) | spec_index, shared_by, &indio_dev->dev,
			&indio_dev->event_interface->dev_attr_list);
		kfree(postfix);

		if (ret)
			return ret;

		attrcount++;
	}

	return attrcount;
}

355
static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
356 357 358 359 360 361 362 363 364 365 366 367 368
	struct iio_chan_spec const *chan)
{
	int ret = 0, i, attrcount = 0;
	enum iio_event_direction dir;
	enum iio_event_type type;

	for (i = 0; i < chan->num_event_specs; i++) {
		type = chan->event_spec[i].type;
		dir = chan->event_spec[i].dir;

		ret = iio_device_add_event(indio_dev, chan, i, type, dir,
			IIO_SEPARATE, &chan->event_spec[i].mask_separate);
		if (ret < 0)
369
			return ret;
370 371 372 373 374 375
		attrcount += ret;

		ret = iio_device_add_event(indio_dev, chan, i, type, dir,
			IIO_SHARED_BY_TYPE,
			&chan->event_spec[i].mask_shared_by_type);
		if (ret < 0)
376
			return ret;
377 378 379 380 381 382
		attrcount += ret;

		ret = iio_device_add_event(indio_dev, chan, i, type, dir,
			IIO_SHARED_BY_DIR,
			&chan->event_spec[i].mask_shared_by_dir);
		if (ret < 0)
383
			return ret;
384 385 386 387 388 389
		attrcount += ret;

		ret = iio_device_add_event(indio_dev, chan, i, type, dir,
			IIO_SHARED_BY_ALL,
			&chan->event_spec[i].mask_shared_by_all);
		if (ret < 0)
390
			return ret;
391 392 393 394 395 396
		attrcount += ret;
	}
	ret = attrcount;
	return ret;
}

397 398 399 400 401 402 403 404 405
static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
{
	int j, ret, attrcount = 0;

	/* Dynically created from the channels array */
	for (j = 0; j < indio_dev->num_channels; j++) {
		ret = iio_device_add_event_sysfs(indio_dev,
						 &indio_dev->channels[j]);
		if (ret < 0)
406
			return ret;
407 408 409 410 411 412 413 414 415
		attrcount += ret;
	}
	return attrcount;
}

static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
{
	int j;

416 417 418 419
	for (j = 0; j < indio_dev->num_channels; j++) {
		if (indio_dev->channels[j].num_event_specs != 0)
			return true;
	}
420 421 422 423 424
	return false;
}

static void iio_setup_ev_int(struct iio_event_interface *ev_int)
{
425
	INIT_KFIFO(ev_int->det_events);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	init_waitqueue_head(&ev_int->wait);
}

static const char *iio_event_group_name = "events";
int iio_device_register_eventset(struct iio_dev *indio_dev)
{
	struct iio_dev_attr *p;
	int ret = 0, attrcount_orig = 0, attrcount, attrn;
	struct attribute **attr;

	if (!(indio_dev->info->event_attrs ||
	      iio_check_for_dynamic_events(indio_dev)))
		return 0;

	indio_dev->event_interface =
		kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
442 443
	if (indio_dev->event_interface == NULL)
		return -ENOMEM;
444

S
Sascha Hauer 已提交
445 446
	INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
	iio_setup_ev_int(indio_dev->event_interface);
	if (indio_dev->info->event_attrs != NULL) {
		attr = indio_dev->info->event_attrs->attrs;
		while (*attr++ != NULL)
			attrcount_orig++;
	}
	attrcount = attrcount_orig;
	if (indio_dev->channels) {
		ret = __iio_add_event_config_attrs(indio_dev);
		if (ret < 0)
			goto error_free_setup_event_lines;
		attrcount += ret;
	}

	indio_dev->event_interface->group.name = iio_event_group_name;
	indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
							  sizeof(indio_dev->event_interface->group.attrs[0]),
							  GFP_KERNEL);
	if (indio_dev->event_interface->group.attrs == NULL) {
		ret = -ENOMEM;
		goto error_free_setup_event_lines;
	}
	if (indio_dev->info->event_attrs)
		memcpy(indio_dev->event_interface->group.attrs,
		       indio_dev->info->event_attrs->attrs,
		       sizeof(indio_dev->event_interface->group.attrs[0])
		       *attrcount_orig);
	attrn = attrcount_orig;
	/* Add all elements from the list. */
	list_for_each_entry(p,
			    &indio_dev->event_interface->dev_attr_list,
			    l)
		indio_dev->event_interface->group.attrs[attrn++] =
			&p->dev_attr.attr;
	indio_dev->groups[indio_dev->groupcounter++] =
		&indio_dev->event_interface->group;

	return 0;

error_free_setup_event_lines:
487
	iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
488 489 490 491
	kfree(indio_dev->event_interface);
	return ret;
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505
/**
 * iio_device_wakeup_eventset - Wakes up the event waitqueue
 * @indio_dev: The IIO device
 *
 * Wakes up the event waitqueue used for poll() and blocking read().
 * Should usually be called when the device is unregistered.
 */
void iio_device_wakeup_eventset(struct iio_dev *indio_dev)
{
	if (indio_dev->event_interface == NULL)
		return;
	wake_up(&indio_dev->event_interface->wait);
}

506 507 508 509
void iio_device_unregister_eventset(struct iio_dev *indio_dev)
{
	if (indio_dev->event_interface == NULL)
		return;
510
	iio_free_chan_devattr_list(&indio_dev->event_interface->dev_attr_list);
511 512 513
	kfree(indio_dev->event_interface->group.attrs);
	kfree(indio_dev->event_interface);
}