Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / iio / industrialio-event.c
CommitLineData
0a769a95
LPC
1/* Industrial I/O event handling
2 *
3 * Copyright (c) 2008 Jonathan Cameron
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * Based on elements of hwmon and input subsystems.
10 */
11
12#include <linux/anon_inodes.h>
13#include <linux/device.h>
14#include <linux/fs.h>
15#include <linux/kernel.h>
2c00193f 16#include <linux/kfifo.h>
0a769a95 17#include <linux/module.h>
e18045ed 18#include <linux/poll.h>
0a769a95
LPC
19#include <linux/sched.h>
20#include <linux/slab.h>
21#include <linux/uaccess.h>
22#include <linux/wait.h>
06458e27 23#include <linux/iio/iio.h>
0a769a95 24#include "iio_core.h"
06458e27
JC
25#include <linux/iio/sysfs.h>
26#include <linux/iio/events.h>
0a769a95 27
0a769a95
LPC
28/**
29 * struct iio_event_interface - chrdev interface for an event line
30 * @wait: wait queue to allow blocking reads of events
0a769a95 31 * @det_events: list of detected events
0a769a95
LPC
32 * @dev_attr_list: list of event interface sysfs attribute
33 * @flags: file operations related flags including busy flag.
34 * @group: event interface sysfs attribute group
35 */
36struct iio_event_interface {
37 wait_queue_head_t wait;
6fa3eb70 38 struct mutex read_lock;
2c00193f
LPC
39 DECLARE_KFIFO(det_events, struct iio_event_data, 16);
40
0a769a95
LPC
41 struct list_head dev_attr_list;
42 unsigned long flags;
43 struct attribute_group group;
44};
45
46int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
47{
48 struct iio_event_interface *ev_int = indio_dev->event_interface;
2c00193f 49 struct iio_event_data ev;
1b9dc91e 50 unsigned long flags;
2c00193f 51 int copied;
0a769a95
LPC
52
53 /* Does anyone care? */
1b9dc91e 54 spin_lock_irqsave(&ev_int->wait.lock, flags);
0a769a95 55 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
0a769a95 56
2c00193f
LPC
57 ev.id = ev_code;
58 ev.timestamp = timestamp;
59
60 copied = kfifo_put(&ev_int->det_events, &ev);
2c00193f 61 if (copied != 0)
e18045ed 62 wake_up_locked_poll(&ev_int->wait, POLLIN);
43ba1100 63 }
1b9dc91e 64 spin_unlock_irqrestore(&ev_int->wait.lock, flags);
0a769a95 65
2c00193f 66 return 0;
0a769a95
LPC
67}
68EXPORT_SYMBOL(iio_push_event);
69
e18045ed
LPC
70/**
71 * iio_event_poll() - poll the event queue to find out if it has data
72 */
73static unsigned int iio_event_poll(struct file *filep,
74 struct poll_table_struct *wait)
75{
76 struct iio_event_interface *ev_int = filep->private_data;
77 unsigned int events = 0;
78
79 poll_wait(filep, &ev_int->wait, wait);
80
1b9dc91e 81 spin_lock_irq(&ev_int->wait.lock);
e18045ed
LPC
82 if (!kfifo_is_empty(&ev_int->det_events))
83 events = POLLIN | POLLRDNORM;
1b9dc91e 84 spin_unlock_irq(&ev_int->wait.lock);
e18045ed
LPC
85
86 return events;
87}
88
0a769a95
LPC
89static ssize_t iio_event_chrdev_read(struct file *filep,
90 char __user *buf,
91 size_t count,
92 loff_t *f_ps)
93{
94 struct iio_event_interface *ev_int = filep->private_data;
2c00193f 95 unsigned int copied;
0a769a95
LPC
96 int ret;
97
2c00193f 98 if (count < sizeof(struct iio_event_data))
0a769a95
LPC
99 return -EINVAL;
100
6fa3eb70
S
101 if (mutex_lock_interruptible(&ev_int->read_lock))
102 return -ERESTARTSYS;
103
2c00193f 104 if (kfifo_is_empty(&ev_int->det_events)) {
0a769a95
LPC
105 if (filep->f_flags & O_NONBLOCK) {
106 ret = -EAGAIN;
43ba1100 107 goto error_unlock;
0a769a95 108 }
0a769a95 109 /* Blocking on device; waiting for something to be there */
6fa3eb70 110 ret = wait_event_interruptible(ev_int->wait,
2c00193f 111 !kfifo_is_empty(&ev_int->det_events));
0a769a95 112 if (ret)
43ba1100 113 goto error_unlock;
0a769a95 114 /* Single access device so no one else can get the data */
0a769a95
LPC
115 }
116
2c00193f 117 ret = kfifo_to_user(&ev_int->det_events, buf, count, &copied);
0a769a95 118
43ba1100 119error_unlock:
6fa3eb70 120 mutex_unlock(&ev_int->read_lock);
43ba1100 121
2c00193f 122 return ret ? ret : copied;
0a769a95
LPC
123}
124
125static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
126{
127 struct iio_event_interface *ev_int = filep->private_data;
0a769a95 128
1b9dc91e 129 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 130 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
0a769a95
LPC
131 /*
132 * In order to maintain a clean state for reopening,
133 * clear out any awaiting events. The mask will prevent
134 * any new __iio_push_event calls running.
135 */
2c00193f 136 kfifo_reset_out(&ev_int->det_events);
1b9dc91e 137 spin_unlock_irq(&ev_int->wait.lock);
0a769a95
LPC
138
139 return 0;
140}
141
142static const struct file_operations iio_event_chrdev_fileops = {
143 .read = iio_event_chrdev_read,
e18045ed 144 .poll = iio_event_poll,
0a769a95
LPC
145 .release = iio_event_chrdev_release,
146 .owner = THIS_MODULE,
147 .llseek = noop_llseek,
148};
149
150int iio_event_getfd(struct iio_dev *indio_dev)
151{
152 struct iio_event_interface *ev_int = indio_dev->event_interface;
153 int fd;
154
155 if (ev_int == NULL)
156 return -ENODEV;
157
1b9dc91e 158 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 159 if (__test_and_set_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
1b9dc91e 160 spin_unlock_irq(&ev_int->wait.lock);
0a769a95
LPC
161 return -EBUSY;
162 }
1b9dc91e 163 spin_unlock_irq(&ev_int->wait.lock);
0a769a95
LPC
164 fd = anon_inode_getfd("iio:event",
165 &iio_event_chrdev_fileops, ev_int, O_RDONLY);
166 if (fd < 0) {
1b9dc91e 167 spin_lock_irq(&ev_int->wait.lock);
a046c1e8 168 __clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
1b9dc91e 169 spin_unlock_irq(&ev_int->wait.lock);
0a769a95
LPC
170 }
171 return fd;
172}
173
174static const char * const iio_ev_type_text[] = {
175 [IIO_EV_TYPE_THRESH] = "thresh",
176 [IIO_EV_TYPE_MAG] = "mag",
177 [IIO_EV_TYPE_ROC] = "roc",
178 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
179 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
180};
181
182static const char * const iio_ev_dir_text[] = {
183 [IIO_EV_DIR_EITHER] = "either",
184 [IIO_EV_DIR_RISING] = "rising",
185 [IIO_EV_DIR_FALLING] = "falling"
186};
187
188static ssize_t iio_ev_state_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf,
191 size_t len)
192{
e53f5ac5 193 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
194 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
195 int ret;
196 bool val;
197
198 ret = strtobool(buf, &val);
199 if (ret < 0)
200 return ret;
201
202 ret = indio_dev->info->write_event_config(indio_dev,
203 this_attr->address,
204 val);
205 return (ret < 0) ? ret : len;
206}
207
208static ssize_t iio_ev_state_show(struct device *dev,
209 struct device_attribute *attr,
210 char *buf)
211{
e53f5ac5 212 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
213 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
214 int val = indio_dev->info->read_event_config(indio_dev,
215 this_attr->address);
216
217 if (val < 0)
218 return val;
219 else
220 return sprintf(buf, "%d\n", val);
221}
222
223static ssize_t iio_ev_value_show(struct device *dev,
224 struct device_attribute *attr,
225 char *buf)
226{
e53f5ac5 227 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95
LPC
228 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
229 int val, ret;
230
231 ret = indio_dev->info->read_event_value(indio_dev,
232 this_attr->address, &val);
233 if (ret < 0)
234 return ret;
235
236 return sprintf(buf, "%d\n", val);
237}
238
239static ssize_t iio_ev_value_store(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf,
242 size_t len)
243{
e53f5ac5 244 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
0a769a95 245 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
948ad205 246 int val;
0a769a95
LPC
247 int ret;
248
249 if (!indio_dev->info->write_event_value)
250 return -EINVAL;
251
948ad205 252 ret = kstrtoint(buf, 10, &val);
0a769a95
LPC
253 if (ret)
254 return ret;
255
256 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
257 val);
258 if (ret < 0)
259 return ret;
260
261 return len;
262}
263
264static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
265 struct iio_chan_spec const *chan)
266{
267 int ret = 0, i, attrcount = 0;
268 u64 mask = 0;
269 char *postfix;
270 if (!chan->event_mask)
271 return 0;
272
273 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
274 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
275 iio_ev_type_text[i/IIO_EV_DIR_MAX],
276 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
277 if (postfix == NULL) {
278 ret = -ENOMEM;
279 goto error_ret;
280 }
281 if (chan->modified)
282 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
283 i/IIO_EV_DIR_MAX,
284 i%IIO_EV_DIR_MAX);
285 else if (chan->differential)
286 mask = IIO_EVENT_CODE(chan->type,
287 0, 0,
288 i%IIO_EV_DIR_MAX,
289 i/IIO_EV_DIR_MAX,
290 0,
291 chan->channel,
292 chan->channel2);
293 else
294 mask = IIO_UNMOD_EVENT_CODE(chan->type,
295 chan->channel,
296 i/IIO_EV_DIR_MAX,
297 i%IIO_EV_DIR_MAX);
298
299 ret = __iio_add_chan_devattr(postfix,
300 chan,
301 &iio_ev_state_show,
302 iio_ev_state_store,
303 mask,
304 0,
305 &indio_dev->dev,
306 &indio_dev->event_interface->
307 dev_attr_list);
308 kfree(postfix);
309 if (ret)
310 goto error_ret;
311 attrcount++;
312 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
313 iio_ev_type_text[i/IIO_EV_DIR_MAX],
314 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
315 if (postfix == NULL) {
316 ret = -ENOMEM;
317 goto error_ret;
318 }
319 ret = __iio_add_chan_devattr(postfix, chan,
320 iio_ev_value_show,
321 iio_ev_value_store,
322 mask,
323 0,
324 &indio_dev->dev,
325 &indio_dev->event_interface->
326 dev_attr_list);
327 kfree(postfix);
328 if (ret)
329 goto error_ret;
330 attrcount++;
331 }
332 ret = attrcount;
333error_ret:
334 return ret;
335}
336
337static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
338{
339 struct iio_dev_attr *p, *n;
340 list_for_each_entry_safe(p, n,
341 &indio_dev->event_interface->
342 dev_attr_list, l) {
343 kfree(p->dev_attr.attr.name);
344 kfree(p);
345 }
346}
347
348static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
349{
350 int j, ret, attrcount = 0;
351
0a769a95
LPC
352 /* Dynically created from the channels array */
353 for (j = 0; j < indio_dev->num_channels; j++) {
354 ret = iio_device_add_event_sysfs(indio_dev,
355 &indio_dev->channels[j]);
356 if (ret < 0)
e3db9ef6 357 return ret;
0a769a95
LPC
358 attrcount += ret;
359 }
360 return attrcount;
0a769a95
LPC
361}
362
363static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
364{
365 int j;
366
367 for (j = 0; j < indio_dev->num_channels; j++)
368 if (indio_dev->channels[j].event_mask != 0)
369 return true;
370 return false;
371}
372
373static void iio_setup_ev_int(struct iio_event_interface *ev_int)
374{
2c00193f 375 INIT_KFIFO(ev_int->det_events);
0a769a95 376 init_waitqueue_head(&ev_int->wait);
6fa3eb70 377 mutex_init(&ev_int->read_lock);
0a769a95
LPC
378}
379
380static const char *iio_event_group_name = "events";
381int iio_device_register_eventset(struct iio_dev *indio_dev)
382{
383 struct iio_dev_attr *p;
384 int ret = 0, attrcount_orig = 0, attrcount, attrn;
385 struct attribute **attr;
386
387 if (!(indio_dev->info->event_attrs ||
388 iio_check_for_dynamic_events(indio_dev)))
389 return 0;
390
391 indio_dev->event_interface =
392 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
393 if (indio_dev->event_interface == NULL) {
394 ret = -ENOMEM;
395 goto error_ret;
396 }
397
46b24311
SH
398 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
399
0a769a95
LPC
400 iio_setup_ev_int(indio_dev->event_interface);
401 if (indio_dev->info->event_attrs != NULL) {
402 attr = indio_dev->info->event_attrs->attrs;
403 while (*attr++ != NULL)
404 attrcount_orig++;
405 }
406 attrcount = attrcount_orig;
407 if (indio_dev->channels) {
408 ret = __iio_add_event_config_attrs(indio_dev);
409 if (ret < 0)
410 goto error_free_setup_event_lines;
411 attrcount += ret;
412 }
413
414 indio_dev->event_interface->group.name = iio_event_group_name;
415 indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
416 sizeof(indio_dev->event_interface->group.attrs[0]),
417 GFP_KERNEL);
418 if (indio_dev->event_interface->group.attrs == NULL) {
419 ret = -ENOMEM;
420 goto error_free_setup_event_lines;
421 }
422 if (indio_dev->info->event_attrs)
423 memcpy(indio_dev->event_interface->group.attrs,
424 indio_dev->info->event_attrs->attrs,
425 sizeof(indio_dev->event_interface->group.attrs[0])
426 *attrcount_orig);
427 attrn = attrcount_orig;
428 /* Add all elements from the list. */
429 list_for_each_entry(p,
430 &indio_dev->event_interface->dev_attr_list,
431 l)
432 indio_dev->event_interface->group.attrs[attrn++] =
433 &p->dev_attr.attr;
434 indio_dev->groups[indio_dev->groupcounter++] =
435 &indio_dev->event_interface->group;
436
437 return 0;
438
439error_free_setup_event_lines:
440 __iio_remove_event_config_attrs(indio_dev);
6fa3eb70 441 mutex_destroy(&indio_dev->event_interface->read_lock);
0a769a95
LPC
442 kfree(indio_dev->event_interface);
443error_ret:
444
445 return ret;
446}
447
448void iio_device_unregister_eventset(struct iio_dev *indio_dev)
449{
450 if (indio_dev->event_interface == NULL)
451 return;
452 __iio_remove_event_config_attrs(indio_dev);
453 kfree(indio_dev->event_interface->group.attrs);
6fa3eb70 454 mutex_destroy(&indio_dev->event_interface->read_lock);
0a769a95
LPC
455 kfree(indio_dev->event_interface);
456}