blob: 1c53b9acae81029dd7b9c14d14ee17baae84e1be [file] [log] [blame]
Jonathan Cameron847ec802009-08-18 18:06:19 +01001/* The industrial I/O core
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/kernel.h>
13#include <linux/module.h>
14#include <linux/idr.h>
15#include <linux/kdev_t.h>
16#include <linux/err.h>
17#include <linux/device.h>
18#include <linux/fs.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010019#include <linux/poll.h>
Jonathan Cameronffc18af2009-10-12 19:18:09 +010020#include <linux/sched.h>
Jeff Mahoney4439c932009-10-12 17:10:34 -040021#include <linux/wait.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010022#include <linux/cdev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Jonathan Cameron8e7d9672011-08-30 12:32:45 +010024#include <linux/anon_inodes.h>
Jonathan Cameron847ec802009-08-18 18:06:19 +010025#include "iio.h"
Jonathan Camerondf9c1c42011-08-12 17:56:03 +010026#include "iio_core.h"
Jonathan Cameron6aea1c32011-08-24 17:28:38 +010027#include "iio_core_trigger.h"
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010028#include "sysfs.h"
Jonathan Cameronaf5046a2011-10-26 17:41:32 +010029#include "events.h"
Jonathan Cameron9dd1cb32011-08-30 12:41:15 +010030
Jonathan Cameron47c24fd2011-08-30 12:41:07 +010031/* IDA to assign each registered device a unique id*/
Jonathan Cameronb156cf72010-09-04 17:54:43 +010032static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010033
Jonathan Cameronf625cb92011-08-30 12:32:48 +010034static dev_t iio_devt;
Jonathan Cameron847ec802009-08-18 18:06:19 +010035
36#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010037struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010038 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010039};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010040EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010041
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +010042static const char * const iio_data_type_name[] = {
43 [IIO_RAW] = "raw",
44 [IIO_PROCESSED] = "input",
45};
46
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010047static const char * const iio_direction[] = {
48 [0] = "in",
49 [1] = "out",
50};
51
Jonathan Cameronade7ef72011-09-02 17:14:45 +010052static const char * const iio_chan_type_name_spec[] = {
Jonathan Cameronc6fc8062011-09-02 17:14:34 +010053 [IIO_VOLTAGE] = "voltage",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010054 [IIO_CURRENT] = "current",
55 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070056 [IIO_ACCEL] = "accel",
Jonathan Cameron41ea0402011-10-05 15:27:59 +010057 [IIO_ANGL_VEL] = "anglvel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010058 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070059 [IIO_LIGHT] = "illuminance",
60 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070061 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070062 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010063 [IIO_INCLI] = "incli",
64 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010065 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070066 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010067 [IIO_CAPACITANCE] = "capacitance",
Jonathan Cameron1d892712011-05-18 14:40:51 +010068};
69
Jonathan Cameron330c6c52011-09-02 17:14:39 +010070static const char * const iio_modifier_names[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010071 [IIO_MOD_X] = "x",
72 [IIO_MOD_Y] = "y",
73 [IIO_MOD_Z] = "z",
Jonathan Cameron330c6c52011-09-02 17:14:39 +010074 [IIO_MOD_LIGHT_BOTH] = "both",
75 [IIO_MOD_LIGHT_IR] = "ir",
Jonathan Cameron1d892712011-05-18 14:40:51 +010076};
77
78/* relies on pairs of these shared then separate */
79static const char * const iio_chan_info_postfix[] = {
80 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
81 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
82 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
83 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
Jonathan Cameroneb7fea52011-05-18 14:41:57 +010084 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
85 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
Jonathan Cameron7d438172011-08-12 17:47:53 +010086 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW_SHARED/2]
87 = "quadrature_correction_raw",
Jonathan Cameron66dbe702011-09-02 17:14:43 +010088 [IIO_CHAN_INFO_AVERAGE_RAW_SHARED/2] = "mean_raw",
Jonathan Cameron1d892712011-05-18 14:40:51 +010089};
90
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +010091/**
92 * struct iio_detected_event_list - list element for events that have occurred
93 * @list: linked list header
94 * @ev: the event itself
95 */
96struct iio_detected_event_list {
97 struct list_head list;
98 struct iio_event_data ev;
99};
100
101/**
102 * struct iio_event_interface - chrdev interface for an event line
103 * @dev: device assocated with event interface
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +0100104 * @wait: wait queue to allow blocking reads of events
105 * @event_list_lock: mutex to protect the list of detected events
106 * @det_events: list of detected events
107 * @max_events: maximum number of events before new ones are dropped
108 * @current_events: number of events in detected list
Jonathan Cameron63564632011-08-30 12:32:46 +0100109 * @flags: file operations related flags including busy flag.
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +0100110 */
111struct iio_event_interface {
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +0100112 wait_queue_head_t wait;
113 struct mutex event_list_lock;
114 struct list_head det_events;
115 int max_events;
116 int current_events;
117 struct list_head dev_attr_list;
Jonathan Cameron63564632011-08-30 12:32:46 +0100118 unsigned long flags;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100119 struct attribute_group group;
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +0100120};
121
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100122int iio_push_event(struct iio_dev *indio_dev, u64 ev_code, s64 timestamp)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100123{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100124 struct iio_event_interface *ev_int = indio_dev->event_interface;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100125 struct iio_detected_event_list *ev;
126 int ret = 0;
127
128 /* Does anyone care? */
129 mutex_lock(&ev_int->event_list_lock);
Jonathan Cameron63564632011-08-30 12:32:46 +0100130 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->flags)) {
Jonathan Cameron75c80752010-01-09 16:57:34 +0000131 if (ev_int->current_events == ev_int->max_events) {
132 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100133 return 0;
Jonathan Cameron75c80752010-01-09 16:57:34 +0000134 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100135 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
136 if (ev == NULL) {
137 ret = -ENOMEM;
Jonathan Cameron75c80752010-01-09 16:57:34 +0000138 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100139 goto error_ret;
140 }
141 ev->ev.id = ev_code;
142 ev->ev.timestamp = timestamp;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100143
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100144 list_add_tail(&ev->list, &ev_int->det_events);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100145 ev_int->current_events++;
146 mutex_unlock(&ev_int->event_list_lock);
147 wake_up_interruptible(&ev_int->wait);
148 } else
149 mutex_unlock(&ev_int->event_list_lock);
150
151error_ret:
152 return ret;
153}
Jonathan Cameron847ec802009-08-18 18:06:19 +0100154EXPORT_SYMBOL(iio_push_event);
155
Jonathan Cameron847ec802009-08-18 18:06:19 +0100156/* This turns up an awful lot */
157ssize_t iio_read_const_attr(struct device *dev,
158 struct device_attribute *attr,
159 char *buf)
160{
161 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
162}
163EXPORT_SYMBOL(iio_read_const_attr);
164
Mark Brown77712e52010-02-18 17:19:17 +0000165static ssize_t iio_event_chrdev_read(struct file *filep,
166 char __user *buf,
167 size_t count,
168 loff_t *f_ps)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100169{
170 struct iio_event_interface *ev_int = filep->private_data;
171 struct iio_detected_event_list *el;
Lars-Peter Clausendc8f5262011-10-26 17:27:43 +0100172 size_t len = sizeof(el->ev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100173 int ret;
Lars-Peter Clausendc8f5262011-10-26 17:27:43 +0100174
175 if (count < len)
176 return -EINVAL;
Jonathan Cameron63564632011-08-30 12:32:46 +0100177
Jonathan Cameron847ec802009-08-18 18:06:19 +0100178 mutex_lock(&ev_int->event_list_lock);
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100179 if (list_empty(&ev_int->det_events)) {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100180 if (filep->f_flags & O_NONBLOCK) {
181 ret = -EAGAIN;
182 goto error_mutex_unlock;
183 }
184 mutex_unlock(&ev_int->event_list_lock);
185 /* Blocking on device; waiting for something to be there */
186 ret = wait_event_interruptible(ev_int->wait,
187 !list_empty(&ev_int
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100188 ->det_events));
Jonathan Cameron847ec802009-08-18 18:06:19 +0100189 if (ret)
190 goto error_ret;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300191 /* Single access device so no one else can get the data */
Jonathan Cameron847ec802009-08-18 18:06:19 +0100192 mutex_lock(&ev_int->event_list_lock);
193 }
194
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100195 el = list_first_entry(&ev_int->det_events,
Jonathan Cameron847ec802009-08-18 18:06:19 +0100196 struct iio_detected_event_list,
197 list);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100198 if (copy_to_user(buf, &(el->ev), len)) {
199 ret = -EFAULT;
200 goto error_mutex_unlock;
201 }
202 list_del(&el->list);
203 ev_int->current_events--;
204 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100205 kfree(el);
206
207 return len;
208
209error_mutex_unlock:
210 mutex_unlock(&ev_int->event_list_lock);
211error_ret:
212
213 return ret;
214}
215
Mark Brown77712e52010-02-18 17:19:17 +0000216static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100217{
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100218 struct iio_event_interface *ev_int = filep->private_data;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100219 struct iio_detected_event_list *el, *t;
Jonathan Cameron63564632011-08-30 12:32:46 +0100220
Jonathan Cameron847ec802009-08-18 18:06:19 +0100221 mutex_lock(&ev_int->event_list_lock);
Jonathan Cameron63564632011-08-30 12:32:46 +0100222 clear_bit(IIO_BUSY_BIT_POS, &ev_int->flags);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100223 /*
224 * In order to maintain a clean state for reopening,
225 * clear out any awaiting events. The mask will prevent
226 * any new __iio_push_event calls running.
227 */
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100228 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100229 list_del(&el->list);
230 kfree(el);
231 }
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100232 ev_int->current_events = 0;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100233 mutex_unlock(&ev_int->event_list_lock);
234
235 return 0;
236}
237
238static const struct file_operations iio_event_chrdev_fileops = {
239 .read = iio_event_chrdev_read,
240 .release = iio_event_chrdev_release,
Jonathan Cameron847ec802009-08-18 18:06:19 +0100241 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200242 .llseek = noop_llseek,
Jonathan Cameron847ec802009-08-18 18:06:19 +0100243};
244
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100245static int iio_event_getfd(struct iio_dev *indio_dev)
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100246{
Al Virob4641332011-11-21 17:25:37 -0500247 int fd;
248
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100249 if (indio_dev->event_interface == NULL)
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100250 return -ENODEV;
251
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100252 mutex_lock(&indio_dev->event_interface->event_list_lock);
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100253 if (test_and_set_bit(IIO_BUSY_BIT_POS,
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100254 &indio_dev->event_interface->flags)) {
255 mutex_unlock(&indio_dev->event_interface->event_list_lock);
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100256 return -EBUSY;
257 }
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100258 mutex_unlock(&indio_dev->event_interface->event_list_lock);
Al Virob4641332011-11-21 17:25:37 -0500259 fd = anon_inode_getfd("iio:event",
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100260 &iio_event_chrdev_fileops,
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100261 indio_dev->event_interface, O_RDONLY);
Al Virob4641332011-11-21 17:25:37 -0500262 if (fd < 0) {
263 mutex_lock(&indio_dev->event_interface->event_list_lock);
Greg Kroah-Hartmanf791cec2011-11-26 16:31:16 -0800264 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->event_interface->flags);
Al Virob4641332011-11-21 17:25:37 -0500265 mutex_unlock(&indio_dev->event_interface->event_list_lock);
266 }
267 return fd;
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100268}
269
Jonathan Cameron847ec802009-08-18 18:06:19 +0100270static int __init iio_init(void)
271{
272 int ret;
273
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100274 /* Register sysfs bus */
275 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100276 if (ret < 0) {
277 printk(KERN_ERR
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100278 "%s could not register bus type\n",
Jonathan Cameron847ec802009-08-18 18:06:19 +0100279 __FILE__);
280 goto error_nothing;
281 }
282
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100283 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
284 if (ret < 0) {
285 printk(KERN_ERR "%s: failed to allocate char dev region\n",
286 __FILE__);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100287 goto error_unregister_bus_type;
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100288 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100289
290 return 0;
291
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100292error_unregister_bus_type:
293 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100294error_nothing:
295 return ret;
296}
297
298static void __exit iio_exit(void)
299{
Jonathan Cameron9aa1a162011-08-12 17:08:50 +0100300 if (iio_devt)
301 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100302 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100303}
304
Jonathan Cameron1d892712011-05-18 14:40:51 +0100305static ssize_t iio_read_channel_info(struct device *dev,
306 struct device_attribute *attr,
307 char *buf)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100308{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100309 struct iio_dev *indio_dev = dev_get_drvdata(dev);
310 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
311 int val, val2;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100312 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
313 &val, &val2, this_attr->address);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100314
Jonathan Cameron1d892712011-05-18 14:40:51 +0100315 if (ret < 0)
316 return ret;
317
318 if (ret == IIO_VAL_INT)
319 return sprintf(buf, "%d\n", val);
320 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
321 if (val2 < 0)
322 return sprintf(buf, "-%d.%06u\n", val, -val2);
323 else
324 return sprintf(buf, "%d.%06u\n", val, val2);
Michael Hennerich71646e22011-06-27 13:07:07 +0100325 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
326 if (val2 < 0)
327 return sprintf(buf, "-%d.%09u\n", val, -val2);
328 else
329 return sprintf(buf, "%d.%09u\n", val, val2);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100330 } else
331 return 0;
332}
333
334static ssize_t iio_write_channel_info(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf,
337 size_t len)
338{
339 struct iio_dev *indio_dev = dev_get_drvdata(dev);
340 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Michael Hennerich5c04af02011-06-27 13:07:10 +0100341 int ret, integer = 0, fract = 0, fract_mult = 100000;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100342 bool integer_part = true, negative = false;
343
344 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100345 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100346 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100347
348 if (indio_dev->info->write_raw_get_fmt)
349 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
350 this_attr->c, this_attr->address)) {
351 case IIO_VAL_INT_PLUS_MICRO:
352 fract_mult = 100000;
353 break;
354 case IIO_VAL_INT_PLUS_NANO:
355 fract_mult = 100000000;
356 break;
357 default:
358 return -EINVAL;
359 }
360
Jonathan Cameron1d892712011-05-18 14:40:51 +0100361 if (buf[0] == '-') {
362 negative = true;
363 buf++;
364 }
Michael Hennerich5c04af02011-06-27 13:07:10 +0100365
Jonathan Cameron1d892712011-05-18 14:40:51 +0100366 while (*buf) {
367 if ('0' <= *buf && *buf <= '9') {
368 if (integer_part)
369 integer = integer*10 + *buf - '0';
370 else {
Michael Hennerich5c04af02011-06-27 13:07:10 +0100371 fract += fract_mult*(*buf - '0');
372 if (fract_mult == 1)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100373 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100374 fract_mult /= 10;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100375 }
376 } else if (*buf == '\n') {
377 if (*(buf + 1) == '\0')
378 break;
379 else
380 return -EINVAL;
381 } else if (*buf == '.') {
382 integer_part = false;
383 } else {
384 return -EINVAL;
385 }
386 buf++;
387 }
388 if (negative) {
389 if (integer)
390 integer = -integer;
391 else
Michael Hennerich5c04af02011-06-27 13:07:10 +0100392 fract = -fract;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100393 }
394
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100395 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100396 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100397 if (ret)
398 return ret;
399
400 return len;
401}
402
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100403static
Jonathan Cameron1d892712011-05-18 14:40:51 +0100404int __iio_device_attr_init(struct device_attribute *dev_attr,
405 const char *postfix,
406 struct iio_chan_spec const *chan,
407 ssize_t (*readfunc)(struct device *dev,
408 struct device_attribute *attr,
409 char *buf),
410 ssize_t (*writefunc)(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t len),
414 bool generic)
415{
416 int ret;
417 char *name_format, *full_postfix;
418 sysfs_attr_init(&dev_attr->attr);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100419
420 /* Build up postfix of <extend_name>_<modifier>_postfix */
Jonathan Cameron0403e0d2011-10-26 17:27:42 +0100421 if (chan->modified && !generic) {
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100422 if (chan->extend_name)
423 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
424 iio_modifier_names[chan
425 ->channel2],
426 chan->extend_name,
427 postfix);
428 else
429 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
430 iio_modifier_names[chan
431 ->channel2],
432 postfix);
433 } else {
434 if (chan->extend_name == NULL)
435 full_postfix = kstrdup(postfix, GFP_KERNEL);
436 else
437 full_postfix = kasprintf(GFP_KERNEL,
438 "%s_%s",
439 chan->extend_name,
440 postfix);
441 }
442 if (full_postfix == NULL) {
443 ret = -ENOMEM;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100444 goto error_ret;
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100445 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100446
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100447 if (chan->differential) { /* Differential can not have modifier */
448 if (generic)
449 name_format
450 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
451 iio_direction[chan->output],
452 iio_chan_type_name_spec[chan->type],
453 iio_chan_type_name_spec[chan->type],
454 full_postfix);
455 else if (chan->indexed)
456 name_format
457 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
458 iio_direction[chan->output],
459 iio_chan_type_name_spec[chan->type],
460 chan->channel,
461 iio_chan_type_name_spec[chan->type],
462 chan->channel2,
463 full_postfix);
464 else {
465 WARN_ON("Differential channels must be indexed\n");
466 ret = -EINVAL;
467 goto error_free_full_postfix;
468 }
469 } else { /* Single ended */
470 if (generic)
471 name_format
472 = kasprintf(GFP_KERNEL, "%s_%s_%s",
473 iio_direction[chan->output],
474 iio_chan_type_name_spec[chan->type],
475 full_postfix);
476 else if (chan->indexed)
477 name_format
478 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
479 iio_direction[chan->output],
480 iio_chan_type_name_spec[chan->type],
481 chan->channel,
482 full_postfix);
483 else
484 name_format
485 = kasprintf(GFP_KERNEL, "%s_%s_%s",
486 iio_direction[chan->output],
487 iio_chan_type_name_spec[chan->type],
488 full_postfix);
489 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100490 if (name_format == NULL) {
491 ret = -ENOMEM;
492 goto error_free_full_postfix;
493 }
494 dev_attr->attr.name = kasprintf(GFP_KERNEL,
495 name_format,
496 chan->channel,
497 chan->channel2);
498 if (dev_attr->attr.name == NULL) {
499 ret = -ENOMEM;
500 goto error_free_name_format;
501 }
502
503 if (readfunc) {
504 dev_attr->attr.mode |= S_IRUGO;
505 dev_attr->show = readfunc;
506 }
507
508 if (writefunc) {
509 dev_attr->attr.mode |= S_IWUSR;
510 dev_attr->store = writefunc;
511 }
512 kfree(name_format);
513 kfree(full_postfix);
514
515 return 0;
516
517error_free_name_format:
518 kfree(name_format);
519error_free_full_postfix:
520 kfree(full_postfix);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100521error_ret:
522 return ret;
523}
524
Jonathan Camerondf9c1c42011-08-12 17:56:03 +0100525static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100526{
527 kfree(dev_attr->attr.name);
528}
529
530int __iio_add_chan_devattr(const char *postfix,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100531 struct iio_chan_spec const *chan,
532 ssize_t (*readfunc)(struct device *dev,
533 struct device_attribute *attr,
534 char *buf),
535 ssize_t (*writefunc)(struct device *dev,
536 struct device_attribute *attr,
537 const char *buf,
538 size_t len),
Jonathan Camerone614a542011-09-02 17:14:41 +0100539 u64 mask,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100540 bool generic,
541 struct device *dev,
542 struct list_head *attr_list)
543{
544 int ret;
545 struct iio_dev_attr *iio_attr, *t;
546
547 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
548 if (iio_attr == NULL) {
549 ret = -ENOMEM;
550 goto error_ret;
551 }
552 ret = __iio_device_attr_init(&iio_attr->dev_attr,
553 postfix, chan,
554 readfunc, writefunc, generic);
555 if (ret)
556 goto error_iio_dev_attr_free;
557 iio_attr->c = chan;
558 iio_attr->address = mask;
559 list_for_each_entry(t, attr_list, l)
560 if (strcmp(t->dev_attr.attr.name,
561 iio_attr->dev_attr.attr.name) == 0) {
562 if (!generic)
563 dev_err(dev, "tried to double register : %s\n",
564 t->dev_attr.attr.name);
565 ret = -EBUSY;
566 goto error_device_attr_deinit;
567 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100568 list_add(&iio_attr->l, attr_list);
569
570 return 0;
571
572error_device_attr_deinit:
573 __iio_device_attr_deinit(&iio_attr->dev_attr);
574error_iio_dev_attr_free:
575 kfree(iio_attr);
576error_ret:
577 return ret;
578}
579
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100580static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100581 struct iio_chan_spec const *chan)
582{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100583 int ret, i, attrcount = 0;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100584
Jonathan Cameron1d892712011-05-18 14:40:51 +0100585 if (chan->channel < 0)
586 return 0;
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100587
588 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100589 chan,
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100590 &iio_read_channel_info,
Jonathan Cameronc6fc8062011-09-02 17:14:34 +0100591 (chan->output ?
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100592 &iio_write_channel_info : NULL),
593 0,
594 0,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100595 &indio_dev->dev,
596 &indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100597 if (ret)
598 goto error_ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100599 attrcount++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100600
601 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
602 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100603 chan,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100604 &iio_read_channel_info,
605 &iio_write_channel_info,
606 (1 << i),
607 !(i%2),
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100608 &indio_dev->dev,
609 &indio_dev->channel_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100610 if (ret == -EBUSY && (i%2 == 0)) {
611 ret = 0;
612 continue;
613 }
614 if (ret < 0)
615 goto error_ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100616 attrcount++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100617 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100618 ret = attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100619error_ret:
620 return ret;
621}
622
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100623static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100624 struct iio_dev_attr *p)
625{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100626 kfree(p->dev_attr.attr.name);
627 kfree(p);
628}
629
Jonathan Cameron1b732882011-05-18 14:41:43 +0100630static ssize_t iio_show_dev_name(struct device *dev,
631 struct device_attribute *attr,
632 char *buf)
633{
634 struct iio_dev *indio_dev = dev_get_drvdata(dev);
635 return sprintf(buf, "%s\n", indio_dev->name);
636}
637
638static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
639
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100640static int iio_device_register_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100641{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100642 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100643 struct iio_dev_attr *p, *n;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100644 struct attribute **attr;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100645
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100646 /* First count elements in any existing group */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100647 if (indio_dev->info->attrs) {
648 attr = indio_dev->info->attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100649 while (*attr++ != NULL)
650 attrcount_orig++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100651 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100652 attrcount = attrcount_orig;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100653 /*
654 * New channel registration method - relies on the fact a group does
655 * not need to be initialized if it is name is NULL.
656 */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100657 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
658 if (indio_dev->channels)
659 for (i = 0; i < indio_dev->num_channels; i++) {
660 ret = iio_device_add_channel_sysfs(indio_dev,
661 &indio_dev
Jonathan Cameron1d892712011-05-18 14:40:51 +0100662 ->channels[i]);
663 if (ret < 0)
664 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100665 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100666 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100667
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100668 if (indio_dev->name)
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100669 attrcount++;
670
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100671 indio_dev->chan_attr_group.attrs
672 = kzalloc(sizeof(indio_dev->chan_attr_group.attrs[0])*
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100673 (attrcount + 1),
674 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100675 if (indio_dev->chan_attr_group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100676 ret = -ENOMEM;
677 goto error_clear_attrs;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100678 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100679 /* Copy across original attributes */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100680 if (indio_dev->info->attrs)
681 memcpy(indio_dev->chan_attr_group.attrs,
682 indio_dev->info->attrs->attrs,
683 sizeof(indio_dev->chan_attr_group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100684 *attrcount_orig);
685 attrn = attrcount_orig;
686 /* Add all elements from the list. */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100687 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
688 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
689 if (indio_dev->name)
690 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100691
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100692 indio_dev->groups[indio_dev->groupcounter++] =
693 &indio_dev->chan_attr_group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100694
Jonathan Cameron1d892712011-05-18 14:40:51 +0100695 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100696
Jonathan Cameron1d892712011-05-18 14:40:51 +0100697error_clear_attrs:
698 list_for_each_entry_safe(p, n,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100699 &indio_dev->channel_attr_list, l) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100700 list_del(&p->l);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100701 iio_device_remove_and_free_read_attr(indio_dev, p);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100702 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100703
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100704 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100705}
706
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100707static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100708{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100709
710 struct iio_dev_attr *p, *n;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100711
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100712 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100713 list_del(&p->l);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100714 iio_device_remove_and_free_read_attr(indio_dev, p);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100715 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100716 kfree(indio_dev->chan_attr_group.attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100717}
718
Jonathan Cameron1d892712011-05-18 14:40:51 +0100719static const char * const iio_ev_type_text[] = {
720 [IIO_EV_TYPE_THRESH] = "thresh",
721 [IIO_EV_TYPE_MAG] = "mag",
Jonathan Cameron8ce73752011-09-02 17:14:44 +0100722 [IIO_EV_TYPE_ROC] = "roc",
723 [IIO_EV_TYPE_THRESH_ADAPTIVE] = "thresh_adaptive",
724 [IIO_EV_TYPE_MAG_ADAPTIVE] = "mag_adaptive",
Jonathan Cameron1d892712011-05-18 14:40:51 +0100725};
726
727static const char * const iio_ev_dir_text[] = {
728 [IIO_EV_DIR_EITHER] = "either",
729 [IIO_EV_DIR_RISING] = "rising",
730 [IIO_EV_DIR_FALLING] = "falling"
731};
732
733static ssize_t iio_ev_state_store(struct device *dev,
734 struct device_attribute *attr,
735 const char *buf,
736 size_t len)
737{
738 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100739 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100740 int ret;
Jonathan Cameronc74b0de2011-05-18 14:42:33 +0100741 bool val;
742
743 ret = strtobool(buf, &val);
744 if (ret < 0)
745 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100746
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100747 ret = indio_dev->info->write_event_config(indio_dev,
748 this_attr->address,
749 val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100750 return (ret < 0) ? ret : len;
751}
752
753static ssize_t iio_ev_state_show(struct device *dev,
754 struct device_attribute *attr,
755 char *buf)
756{
757 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100758 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100759 int val = indio_dev->info->read_event_config(indio_dev,
760 this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100761
762 if (val < 0)
763 return val;
764 else
765 return sprintf(buf, "%d\n", val);
766}
767
768static ssize_t iio_ev_value_show(struct device *dev,
769 struct device_attribute *attr,
770 char *buf)
771{
772 struct iio_dev *indio_dev = dev_get_drvdata(dev);
773 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
774 int val, ret;
775
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100776 ret = indio_dev->info->read_event_value(indio_dev,
777 this_attr->address, &val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100778 if (ret < 0)
779 return ret;
780
781 return sprintf(buf, "%d\n", val);
782}
783
784static ssize_t iio_ev_value_store(struct device *dev,
785 struct device_attribute *attr,
786 const char *buf,
787 size_t len)
788{
789 struct iio_dev *indio_dev = dev_get_drvdata(dev);
790 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
791 unsigned long val;
792 int ret;
793
794 ret = strict_strtoul(buf, 10, &val);
795 if (ret)
796 return ret;
797
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100798 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
799 val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100800 if (ret < 0)
801 return ret;
802
803 return len;
804}
805
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100806static int iio_device_add_event_sysfs(struct iio_dev *indio_dev,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100807 struct iio_chan_spec const *chan)
808{
Jonathan Camerone614a542011-09-02 17:14:41 +0100809 int ret = 0, i, attrcount = 0;
810 u64 mask = 0;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100811 char *postfix;
812 if (!chan->event_mask)
813 return 0;
814
815 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
816 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
Jonathan Cameronc04ea8a2011-09-02 17:14:42 +0100817 iio_ev_type_text[i/IIO_EV_DIR_MAX],
818 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100819 if (postfix == NULL) {
820 ret = -ENOMEM;
821 goto error_ret;
822 }
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100823 if (chan->modified)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100824 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
Jonathan Cameronc04ea8a2011-09-02 17:14:42 +0100825 i/IIO_EV_DIR_MAX,
826 i%IIO_EV_DIR_MAX);
Jonathan Cameronade7ef72011-09-02 17:14:45 +0100827 else if (chan->differential)
828 mask = IIO_EVENT_CODE(chan->type,
829 0, 0,
830 i%IIO_EV_DIR_MAX,
831 i/IIO_EV_DIR_MAX,
832 0,
833 chan->channel,
834 chan->channel2);
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100835 else
836 mask = IIO_UNMOD_EVENT_CODE(chan->type,
837 chan->channel,
Jonathan Cameronc04ea8a2011-09-02 17:14:42 +0100838 i/IIO_EV_DIR_MAX,
839 i%IIO_EV_DIR_MAX);
Jonathan Cameron330c6c52011-09-02 17:14:39 +0100840
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100841 ret = __iio_add_chan_devattr(postfix,
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100842 chan,
843 &iio_ev_state_show,
844 iio_ev_state_store,
845 mask,
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100846 0,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100847 &indio_dev->dev,
848 &indio_dev->event_interface->
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100849 dev_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100850 kfree(postfix);
851 if (ret)
852 goto error_ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100853 attrcount++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100854 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
Jonathan Cameronc04ea8a2011-09-02 17:14:42 +0100855 iio_ev_type_text[i/IIO_EV_DIR_MAX],
856 iio_ev_dir_text[i%IIO_EV_DIR_MAX]);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100857 if (postfix == NULL) {
858 ret = -ENOMEM;
859 goto error_ret;
860 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100861 ret = __iio_add_chan_devattr(postfix, chan,
Jonathan Cameron1d892712011-05-18 14:40:51 +0100862 iio_ev_value_show,
863 iio_ev_value_store,
864 mask,
865 0,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100866 &indio_dev->dev,
867 &indio_dev->event_interface->
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100868 dev_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100869 kfree(postfix);
870 if (ret)
871 goto error_ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100872 attrcount++;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100873 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100874 ret = attrcount;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100875error_ret:
876 return ret;
877}
878
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100879static inline void __iio_remove_event_config_attrs(struct iio_dev *indio_dev)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100880{
881 struct iio_dev_attr *p, *n;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100882 list_for_each_entry_safe(p, n,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100883 &indio_dev->event_interface->
Jonathan Cameron1d892712011-05-18 14:40:51 +0100884 dev_attr_list, l) {
Jonathan Cameron1d892712011-05-18 14:40:51 +0100885 kfree(p->dev_attr.attr.name);
886 kfree(p);
887 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100888}
889
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100890static inline int __iio_add_event_config_attrs(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100891{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100892 int j, ret, attrcount = 0;
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100893
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100894 INIT_LIST_HEAD(&indio_dev->event_interface->dev_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100895 /* Dynically created from the channels array */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100896 for (j = 0; j < indio_dev->num_channels; j++) {
897 ret = iio_device_add_event_sysfs(indio_dev,
898 &indio_dev->channels[j]);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100899 if (ret < 0)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100900 goto error_clear_attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100901 attrcount += ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100902 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100903 return attrcount;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100904
Jonathan Cameron1d892712011-05-18 14:40:51 +0100905error_clear_attrs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100906 __iio_remove_event_config_attrs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100907
908 return ret;
909}
910
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100911static bool iio_check_for_dynamic_events(struct iio_dev *indio_dev)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100912{
913 int j;
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100914
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100915 for (j = 0; j < indio_dev->num_channels; j++)
916 if (indio_dev->channels[j].event_mask != 0)
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100917 return true;
918 return false;
919}
920
Jonathan Cameron1e8dfcc2011-08-30 12:41:11 +0100921static void iio_setup_ev_int(struct iio_event_interface *ev_int)
922{
923 mutex_init(&ev_int->event_list_lock);
924 /* discussion point - make this variable? */
925 ev_int->max_events = 10;
926 ev_int->current_events = 0;
927 INIT_LIST_HEAD(&ev_int->det_events);
928 init_waitqueue_head(&ev_int->wait);
929}
930
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100931static const char *iio_event_group_name = "events";
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100932static int iio_device_register_eventset(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100933{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100934 struct iio_dev_attr *p;
935 int ret = 0, attrcount_orig = 0, attrcount, attrn;
936 struct attribute **attr;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100937
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100938 if (!(indio_dev->info->event_attrs ||
939 iio_check_for_dynamic_events(indio_dev)))
Jonathan Cameron847ec802009-08-18 18:06:19 +0100940 return 0;
941
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100942 indio_dev->event_interface =
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100943 kzalloc(sizeof(struct iio_event_interface), GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100944 if (indio_dev->event_interface == NULL) {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100945 ret = -ENOMEM;
946 goto error_ret;
947 }
948
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100949 iio_setup_ev_int(indio_dev->event_interface);
950 if (indio_dev->info->event_attrs != NULL) {
951 attr = indio_dev->info->event_attrs->attrs;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100952 while (*attr++ != NULL)
953 attrcount_orig++;
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100954 }
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100955 attrcount = attrcount_orig;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100956 if (indio_dev->channels) {
957 ret = __iio_add_event_config_attrs(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100958 if (ret < 0)
Jonathan Cameroncc2439f2011-08-12 16:55:30 +0100959 goto error_free_setup_event_lines;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100960 attrcount += ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100961 }
962
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100963 indio_dev->event_interface->group.name = iio_event_group_name;
964 indio_dev->event_interface->group.attrs =
965 kzalloc(sizeof(indio_dev->event_interface->group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100966 *(attrcount + 1),
967 GFP_KERNEL);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100968 if (indio_dev->event_interface->group.attrs == NULL) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100969 ret = -ENOMEM;
970 goto error_free_setup_event_lines;
971 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100972 if (indio_dev->info->event_attrs)
973 memcpy(indio_dev->event_interface->group.attrs,
974 indio_dev->info->event_attrs->attrs,
975 sizeof(indio_dev->event_interface->group.attrs[0])
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100976 *attrcount_orig);
977 attrn = attrcount_orig;
978 /* Add all elements from the list. */
979 list_for_each_entry(p,
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100980 &indio_dev->event_interface->dev_attr_list,
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100981 l)
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100982 indio_dev->event_interface->group.attrs[attrn++] =
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100983 &p->dev_attr.attr;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100984 indio_dev->groups[indio_dev->groupcounter++] =
985 &indio_dev->event_interface->group;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100986
Jonathan Cameron847ec802009-08-18 18:06:19 +0100987 return 0;
988
Jonathan Cameroncc2439f2011-08-12 16:55:30 +0100989error_free_setup_event_lines:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100990 __iio_remove_event_config_attrs(indio_dev);
991 kfree(indio_dev->event_interface);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100992error_ret:
993
994 return ret;
995}
996
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100997static void iio_device_unregister_eventset(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100998{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +0100999 if (indio_dev->event_interface == NULL)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001000 return;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001001 __iio_remove_event_config_attrs(indio_dev);
1002 kfree(indio_dev->event_interface->group.attrs);
1003 kfree(indio_dev->event_interface);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001004}
1005
1006static void iio_dev_release(struct device *device)
1007{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001008 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
1009 cdev_del(&indio_dev->chrdev);
1010 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1011 iio_device_unregister_trigger_consumer(indio_dev);
1012 iio_device_unregister_eventset(indio_dev);
1013 iio_device_unregister_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001014}
1015
1016static struct device_type iio_dev_type = {
1017 .name = "iio_device",
1018 .release = iio_dev_release,
1019};
1020
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001021struct iio_dev *iio_allocate_device(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001022{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001023 struct iio_dev *dev;
1024 size_t alloc_size;
1025
1026 alloc_size = sizeof(struct iio_dev);
1027 if (sizeof_priv) {
1028 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1029 alloc_size += sizeof_priv;
1030 }
1031 /* ensure 32-byte alignment of whole construct ? */
1032 alloc_size += IIO_ALIGN - 1;
1033
1034 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001035
1036 if (dev) {
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001037 dev->dev.groups = dev->groups;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001038 dev->dev.type = &iio_dev_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001039 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001040 device_initialize(&dev->dev);
1041 dev_set_drvdata(&dev->dev, (void *)dev);
1042 mutex_init(&dev->mlock);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001043
1044 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
1045 if (dev->id < 0) {
1046 /* cannot use a dev_err as the name isn't available */
1047 printk(KERN_ERR "Failed to get id\n");
1048 kfree(dev);
1049 return NULL;
1050 }
1051 dev_set_name(&dev->dev, "iio:device%d", dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001052 }
1053
1054 return dev;
1055}
1056EXPORT_SYMBOL(iio_allocate_device);
1057
1058void iio_free_device(struct iio_dev *dev)
1059{
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001060 if (dev) {
1061 ida_simple_remove(&iio_ida, dev->id);
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001062 kfree(dev);
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001063 }
Jonathan Cameron847ec802009-08-18 18:06:19 +01001064}
1065EXPORT_SYMBOL(iio_free_device);
1066
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001067/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001068 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001069 **/
1070static int iio_chrdev_open(struct inode *inode, struct file *filp)
1071{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001072 struct iio_dev *indio_dev = container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001073 struct iio_dev, chrdev);
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001074 filp->private_data = indio_dev;
Jonathan Cameron30eb82f2011-09-21 11:16:02 +01001075
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001076 return iio_chrdev_buffer_open(indio_dev);
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001077}
1078
1079/**
Jonathan Cameron14555b12011-09-21 11:15:57 +01001080 * iio_chrdev_release() - chrdev file close buffer access and ioctls
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001081 **/
1082static int iio_chrdev_release(struct inode *inode, struct file *filp)
1083{
Jonathan Cameron14555b12011-09-21 11:15:57 +01001084 iio_chrdev_buffer_release(container_of(inode->i_cdev,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001085 struct iio_dev, chrdev));
1086 return 0;
1087}
1088
1089/* Somewhat of a cross file organization violation - ioctls here are actually
1090 * event related */
1091static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1092{
1093 struct iio_dev *indio_dev = filp->private_data;
1094 int __user *ip = (int __user *)arg;
1095 int fd;
1096
1097 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
1098 fd = iio_event_getfd(indio_dev);
1099 if (copy_to_user(ip, &fd, sizeof(fd)))
1100 return -EFAULT;
1101 return 0;
1102 }
1103 return -EINVAL;
1104}
1105
Jonathan Cameron14555b12011-09-21 11:15:57 +01001106static const struct file_operations iio_buffer_fileops = {
1107 .read = iio_buffer_read_first_n_outer_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001108 .release = iio_chrdev_release,
1109 .open = iio_chrdev_open,
Jonathan Cameron14555b12011-09-21 11:15:57 +01001110 .poll = iio_buffer_poll_addr,
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001111 .owner = THIS_MODULE,
1112 .llseek = noop_llseek,
1113 .unlocked_ioctl = iio_ioctl,
1114 .compat_ioctl = iio_ioctl,
1115};
1116
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001117int iio_device_register(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001118{
1119 int ret;
1120
Jonathan Cameron1aa04272011-08-30 12:32:47 +01001121 /* configure elements for the chrdev */
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001122 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001123
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001124 ret = iio_device_register_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001125 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001126 dev_err(indio_dev->dev.parent,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001127 "Failed to register sysfs interfaces\n");
Jonathan Camerona9e39f92011-09-14 13:01:25 +01001128 goto error_ret;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001129 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001130 ret = iio_device_register_eventset(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001131 if (ret) {
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001132 dev_err(indio_dev->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001133 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001134 goto error_free_sysfs;
1135 }
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001136 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
1137 iio_device_register_trigger_consumer(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001138
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001139 ret = device_add(&indio_dev->dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001140 if (ret < 0)
1141 goto error_unreg_eventset;
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001142 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
1143 indio_dev->chrdev.owner = indio_dev->info->driver_module;
1144 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001145 if (ret < 0)
1146 goto error_del_device;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001147 return 0;
1148
Jonathan Cameron847ec802009-08-18 18:06:19 +01001149error_del_device:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001150 device_del(&indio_dev->dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001151error_unreg_eventset:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001152 iio_device_unregister_eventset(indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +01001153error_free_sysfs:
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001154 iio_device_unregister_sysfs(indio_dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001155error_ret:
1156 return ret;
1157}
1158EXPORT_SYMBOL(iio_device_register);
1159
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001160void iio_device_unregister(struct iio_dev *indio_dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001161{
Jonathan Cameronf8c6f4e2011-10-06 17:14:35 +01001162 device_unregister(&indio_dev->dev);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001163}
1164EXPORT_SYMBOL(iio_device_unregister);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001165subsys_initcall(iio_init);
1166module_exit(iio_exit);
1167
1168MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1169MODULE_DESCRIPTION("Industrial I/O core");
1170MODULE_LICENSE("GPL");