blob: 19819e7578c6175f51a9c8b3b4793cbb4f0ab1b9 [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 Cameron847ec802009-08-18 18:06:19 +010024#include "iio.h"
25#include "trigger_consumer.h"
26
27#define IIO_ID_PREFIX "device"
28#define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
29
30/* IDR to assign each registered device a unique id*/
Jonathan Cameronb156cf72010-09-04 17:54:43 +010031static DEFINE_IDA(iio_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010032/* IDR to allocate character device minor numbers */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010033static DEFINE_IDA(iio_chrdev_ida);
Jonathan Cameron847ec802009-08-18 18:06:19 +010034/* Lock used to protect both of the above */
Jonathan Cameronb156cf72010-09-04 17:54:43 +010035static DEFINE_SPINLOCK(iio_ida_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +010036
37dev_t iio_devt;
38EXPORT_SYMBOL(iio_devt);
39
40#define IIO_DEV_MAX 256
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010041struct bus_type iio_bus_type = {
Jonathan Cameron847ec802009-08-18 18:06:19 +010042 .name = "iio",
Jonathan Cameron847ec802009-08-18 18:06:19 +010043};
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +010044EXPORT_SYMBOL(iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +010045
Jonathan Cameron1d892712011-05-18 14:40:51 +010046static const char * const iio_chan_type_name_spec_shared[] = {
Jonathan Cameron1d892712011-05-18 14:40:51 +010047 [IIO_IN] = "in",
Michael Hennerichae191782011-06-10 15:40:48 +020048 [IIO_OUT] = "out",
Michael Hennerichfaf290e2011-05-18 14:42:02 +010049 [IIO_CURRENT] = "current",
50 [IIO_POWER] = "power",
Bryan Freed9bff02f2011-07-07 12:01:54 -070051 [IIO_ACCEL] = "accel",
Jonathan Cameron1d892712011-05-18 14:40:51 +010052 [IIO_IN_DIFF] = "in-in",
53 [IIO_GYRO] = "gyro",
Jonathan Cameron1d892712011-05-18 14:40:51 +010054 [IIO_MAGN] = "magn",
Bryan Freed9bff02f2011-07-07 12:01:54 -070055 [IIO_LIGHT] = "illuminance",
56 [IIO_INTENSITY] = "intensity",
Bryan Freedf09f2c82011-07-07 12:01:55 -070057 [IIO_PROXIMITY] = "proximity",
Bryan Freed9bff02f2011-07-07 12:01:54 -070058 [IIO_TEMP] = "temp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010059 [IIO_INCLI] = "incli",
60 [IIO_ROT] = "rot",
Jonathan Cameron1d892712011-05-18 14:40:51 +010061 [IIO_ANGL] = "angl",
Bryan Freed9bff02f2011-07-07 12:01:54 -070062 [IIO_TIMESTAMP] = "timestamp",
Jonathan Cameron1d892712011-05-18 14:40:51 +010063};
64
65static const char * const iio_chan_type_name_spec_complex[] = {
66 [IIO_IN_DIFF] = "in%d-in%d",
67};
68
69static const char * const iio_modifier_names_light[] = {
70 [IIO_MOD_LIGHT_BOTH] = "both",
71 [IIO_MOD_LIGHT_IR] = "ir",
72};
73
74static const char * const iio_modifier_names_axial[] = {
75 [IIO_MOD_X] = "x",
76 [IIO_MOD_Y] = "y",
77 [IIO_MOD_Z] = "z",
78};
79
80/* relies on pairs of these shared then separate */
81static const char * const iio_chan_info_postfix[] = {
82 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
83 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
84 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
85 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
Jonathan Cameroneb7fea52011-05-18 14:41:57 +010086 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
87 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
Jonathan Cameron1d892712011-05-18 14:40:51 +010088};
89
Jonathan Cameronaaf370d2011-05-18 14:41:16 +010090int iio_push_event(struct iio_dev *dev_info,
91 int ev_line,
92 int ev_code,
93 s64 timestamp)
Jonathan Cameron847ec802009-08-18 18:06:19 +010094{
Jonathan Cameronaaf370d2011-05-18 14:41:16 +010095 struct iio_event_interface *ev_int
96 = &dev_info->event_interfaces[ev_line];
Jonathan Cameron847ec802009-08-18 18:06:19 +010097 struct iio_detected_event_list *ev;
98 int ret = 0;
99
100 /* Does anyone care? */
101 mutex_lock(&ev_int->event_list_lock);
102 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
Jonathan Cameron75c80752010-01-09 16:57:34 +0000103 if (ev_int->current_events == ev_int->max_events) {
104 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100105 return 0;
Jonathan Cameron75c80752010-01-09 16:57:34 +0000106 }
Jonathan Cameron847ec802009-08-18 18:06:19 +0100107 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
108 if (ev == NULL) {
109 ret = -ENOMEM;
Jonathan Cameron75c80752010-01-09 16:57:34 +0000110 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100111 goto error_ret;
112 }
113 ev->ev.id = ev_code;
114 ev->ev.timestamp = timestamp;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100115
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100116 list_add_tail(&ev->list, &ev_int->det_events);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100117 ev_int->current_events++;
118 mutex_unlock(&ev_int->event_list_lock);
119 wake_up_interruptible(&ev_int->wait);
120 } else
121 mutex_unlock(&ev_int->event_list_lock);
122
123error_ret:
124 return ret;
125}
Jonathan Cameron847ec802009-08-18 18:06:19 +0100126EXPORT_SYMBOL(iio_push_event);
127
Jonathan Cameron847ec802009-08-18 18:06:19 +0100128
129/* This turns up an awful lot */
130ssize_t iio_read_const_attr(struct device *dev,
131 struct device_attribute *attr,
132 char *buf)
133{
134 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
135}
136EXPORT_SYMBOL(iio_read_const_attr);
137
Jonathan Cameron847ec802009-08-18 18:06:19 +0100138
Mark Brown77712e52010-02-18 17:19:17 +0000139static ssize_t iio_event_chrdev_read(struct file *filep,
140 char __user *buf,
141 size_t count,
142 loff_t *f_ps)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100143{
144 struct iio_event_interface *ev_int = filep->private_data;
145 struct iio_detected_event_list *el;
146 int ret;
147 size_t len;
148
149 mutex_lock(&ev_int->event_list_lock);
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100150 if (list_empty(&ev_int->det_events)) {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100151 if (filep->f_flags & O_NONBLOCK) {
152 ret = -EAGAIN;
153 goto error_mutex_unlock;
154 }
155 mutex_unlock(&ev_int->event_list_lock);
156 /* Blocking on device; waiting for something to be there */
157 ret = wait_event_interruptible(ev_int->wait,
158 !list_empty(&ev_int
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100159 ->det_events));
Jonathan Cameron847ec802009-08-18 18:06:19 +0100160 if (ret)
161 goto error_ret;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300162 /* Single access device so no one else can get the data */
Jonathan Cameron847ec802009-08-18 18:06:19 +0100163 mutex_lock(&ev_int->event_list_lock);
164 }
165
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100166 el = list_first_entry(&ev_int->det_events,
Jonathan Cameron847ec802009-08-18 18:06:19 +0100167 struct iio_detected_event_list,
168 list);
169 len = sizeof el->ev;
170 if (copy_to_user(buf, &(el->ev), len)) {
171 ret = -EFAULT;
172 goto error_mutex_unlock;
173 }
174 list_del(&el->list);
175 ev_int->current_events--;
176 mutex_unlock(&ev_int->event_list_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100177 kfree(el);
178
179 return len;
180
181error_mutex_unlock:
182 mutex_unlock(&ev_int->event_list_lock);
183error_ret:
184
185 return ret;
186}
187
Mark Brown77712e52010-02-18 17:19:17 +0000188static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100189{
190 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
191 struct iio_event_interface *ev_int = hand->private;
192 struct iio_detected_event_list *el, *t;
193
194 mutex_lock(&ev_int->event_list_lock);
195 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
196 /*
197 * In order to maintain a clean state for reopening,
198 * clear out any awaiting events. The mask will prevent
199 * any new __iio_push_event calls running.
200 */
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100201 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
Jonathan Cameron847ec802009-08-18 18:06:19 +0100202 list_del(&el->list);
203 kfree(el);
204 }
205 mutex_unlock(&ev_int->event_list_lock);
206
207 return 0;
208}
209
Mark Brown77712e52010-02-18 17:19:17 +0000210static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100211{
212 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
213 struct iio_event_interface *ev_int = hand->private;
214
215 mutex_lock(&ev_int->event_list_lock);
216 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
217 fops_put(filep->f_op);
218 mutex_unlock(&ev_int->event_list_lock);
219 return -EBUSY;
220 }
221 filep->private_data = hand->private;
222 mutex_unlock(&ev_int->event_list_lock);
223
224 return 0;
225}
226
227static const struct file_operations iio_event_chrdev_fileops = {
228 .read = iio_event_chrdev_read,
229 .release = iio_event_chrdev_release,
230 .open = iio_event_chrdev_open,
231 .owner = THIS_MODULE,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200232 .llseek = noop_llseek,
Jonathan Cameron847ec802009-08-18 18:06:19 +0100233};
234
235static void iio_event_dev_release(struct device *dev)
236{
237 struct iio_event_interface *ev_int
238 = container_of(dev, struct iio_event_interface, dev);
239 cdev_del(&ev_int->handler.chrdev);
240 iio_device_free_chrdev_minor(MINOR(dev->devt));
241};
242
243static struct device_type iio_event_type = {
244 .release = iio_event_dev_release,
245};
246
247int iio_device_get_chrdev_minor(void)
248{
249 int ret, val;
250
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100251ida_again:
252 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
Jonathan Cameron847ec802009-08-18 18:06:19 +0100253 return -ENOMEM;
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100254 spin_lock(&iio_ida_lock);
255 ret = ida_get_new(&iio_chrdev_ida, &val);
256 spin_unlock(&iio_ida_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100257 if (unlikely(ret == -EAGAIN))
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100258 goto ida_again;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100259 else if (unlikely(ret))
260 return ret;
261 if (val > IIO_DEV_MAX)
262 return -ENOMEM;
263 return val;
264}
265
266void iio_device_free_chrdev_minor(int val)
267{
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100268 spin_lock(&iio_ida_lock);
269 ida_remove(&iio_chrdev_ida, val);
270 spin_unlock(&iio_ida_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100271}
272
Jonathan Cameronb9d40a92011-05-18 14:40:56 +0100273static int iio_setup_ev_int(struct iio_event_interface *ev_int,
Jonathan Cameronc74b0de2011-05-18 14:42:33 +0100274 const char *dev_name,
275 int index,
276 struct module *owner,
277 struct device *dev)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100278{
279 int ret, minor;
280
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100281 ev_int->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100282 ev_int->dev.parent = dev;
283 ev_int->dev.type = &iio_event_type;
284 device_initialize(&ev_int->dev);
285
286 minor = iio_device_get_chrdev_minor();
287 if (minor < 0) {
288 ret = minor;
289 goto error_device_put;
290 }
291 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
Jonathan Cameronc74b0de2011-05-18 14:42:33 +0100292 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100293
294 ret = device_add(&ev_int->dev);
295 if (ret)
296 goto error_free_minor;
297
298 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
299 ev_int->handler.chrdev.owner = owner;
300
301 mutex_init(&ev_int->event_list_lock);
302 /* discussion point - make this variable? */
303 ev_int->max_events = 10;
304 ev_int->current_events = 0;
Jonathan Cameron3b8ebfb2011-05-18 14:42:21 +0100305 INIT_LIST_HEAD(&ev_int->det_events);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100306 init_waitqueue_head(&ev_int->wait);
307 ev_int->handler.private = ev_int;
308 ev_int->handler.flags = 0;
309
310 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
311 if (ret)
312 goto error_unreg_device;
313
314 return 0;
315
316error_unreg_device:
317 device_unregister(&ev_int->dev);
318error_free_minor:
319 iio_device_free_chrdev_minor(minor);
320error_device_put:
321 put_device(&ev_int->dev);
322
323 return ret;
324}
325
Jonathan Cameronb9d40a92011-05-18 14:40:56 +0100326static void iio_free_ev_int(struct iio_event_interface *ev_int)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100327{
328 device_unregister(&ev_int->dev);
329 put_device(&ev_int->dev);
330}
331
332static int __init iio_dev_init(void)
333{
334 int err;
335
336 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
337 if (err < 0)
338 printk(KERN_ERR "%s: failed to allocate char dev region\n",
339 __FILE__);
340
341 return err;
342}
343
344static void __exit iio_dev_exit(void)
345{
346 if (iio_devt)
347 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
348}
349
350static int __init iio_init(void)
351{
352 int ret;
353
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100354 /* Register sysfs bus */
355 ret = bus_register(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100356 if (ret < 0) {
357 printk(KERN_ERR
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100358 "%s could not register bus type\n",
Jonathan Cameron847ec802009-08-18 18:06:19 +0100359 __FILE__);
360 goto error_nothing;
361 }
362
363 ret = iio_dev_init();
364 if (ret < 0)
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100365 goto error_unregister_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100366
367 return 0;
368
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100369error_unregister_bus_type:
370 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100371error_nothing:
372 return ret;
373}
374
375static void __exit iio_exit(void)
376{
377 iio_dev_exit();
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100378 bus_unregister(&iio_bus_type);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100379}
380
Jonathan Cameron1d892712011-05-18 14:40:51 +0100381static ssize_t iio_read_channel_info(struct device *dev,
382 struct device_attribute *attr,
383 char *buf)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100384{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100385 struct iio_dev *indio_dev = dev_get_drvdata(dev);
386 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
387 int val, val2;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100388 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
389 &val, &val2, this_attr->address);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100390
Jonathan Cameron1d892712011-05-18 14:40:51 +0100391 if (ret < 0)
392 return ret;
393
394 if (ret == IIO_VAL_INT)
395 return sprintf(buf, "%d\n", val);
396 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
397 if (val2 < 0)
398 return sprintf(buf, "-%d.%06u\n", val, -val2);
399 else
400 return sprintf(buf, "%d.%06u\n", val, val2);
Michael Hennerich71646e22011-06-27 13:07:07 +0100401 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
402 if (val2 < 0)
403 return sprintf(buf, "-%d.%09u\n", val, -val2);
404 else
405 return sprintf(buf, "%d.%09u\n", val, val2);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100406 } else
407 return 0;
408}
409
410static ssize_t iio_write_channel_info(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t len)
414{
415 struct iio_dev *indio_dev = dev_get_drvdata(dev);
416 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Michael Hennerich5c04af02011-06-27 13:07:10 +0100417 int ret, integer = 0, fract = 0, fract_mult = 100000;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100418 bool integer_part = true, negative = false;
419
420 /* Assumes decimal - precision based on number of digits */
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100421 if (!indio_dev->info->write_raw)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100422 return -EINVAL;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100423
424 if (indio_dev->info->write_raw_get_fmt)
425 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
426 this_attr->c, this_attr->address)) {
427 case IIO_VAL_INT_PLUS_MICRO:
428 fract_mult = 100000;
429 break;
430 case IIO_VAL_INT_PLUS_NANO:
431 fract_mult = 100000000;
432 break;
433 default:
434 return -EINVAL;
435 }
436
Jonathan Cameron1d892712011-05-18 14:40:51 +0100437 if (buf[0] == '-') {
438 negative = true;
439 buf++;
440 }
Michael Hennerich5c04af02011-06-27 13:07:10 +0100441
Jonathan Cameron1d892712011-05-18 14:40:51 +0100442 while (*buf) {
443 if ('0' <= *buf && *buf <= '9') {
444 if (integer_part)
445 integer = integer*10 + *buf - '0';
446 else {
Michael Hennerich5c04af02011-06-27 13:07:10 +0100447 fract += fract_mult*(*buf - '0');
448 if (fract_mult == 1)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100449 break;
Michael Hennerich5c04af02011-06-27 13:07:10 +0100450 fract_mult /= 10;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100451 }
452 } else if (*buf == '\n') {
453 if (*(buf + 1) == '\0')
454 break;
455 else
456 return -EINVAL;
457 } else if (*buf == '.') {
458 integer_part = false;
459 } else {
460 return -EINVAL;
461 }
462 buf++;
463 }
464 if (negative) {
465 if (integer)
466 integer = -integer;
467 else
Michael Hennerich5c04af02011-06-27 13:07:10 +0100468 fract = -fract;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100469 }
470
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100471 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
Michael Hennerich5c04af02011-06-27 13:07:10 +0100472 integer, fract, this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100473 if (ret)
474 return ret;
475
476 return len;
477}
478
479static int __iio_build_postfix(struct iio_chan_spec const *chan,
480 bool generic,
481 const char *postfix,
482 char **result)
483{
484 char *all_post;
485 /* 3 options - generic, extend_name, modified - if generic, extend_name
486 * and modified cannot apply.*/
487
488 if (generic || (!chan->modified && !chan->extend_name)) {
489 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
490 } else if (chan->modified) {
491 const char *intermediate;
492 switch (chan->type) {
493 case IIO_INTENSITY:
494 intermediate
495 = iio_modifier_names_light[chan->channel2];
496 break;
497 case IIO_ACCEL:
498 case IIO_GYRO:
499 case IIO_MAGN:
500 case IIO_INCLI:
501 case IIO_ROT:
502 case IIO_ANGL:
503 intermediate
504 = iio_modifier_names_axial[chan->channel2];
505 break;
506 default:
507 return -EINVAL;
508 }
509 if (chan->extend_name)
510 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
511 intermediate,
512 chan->extend_name,
513 postfix);
514 else
515 all_post = kasprintf(GFP_KERNEL, "%s_%s",
516 intermediate,
517 postfix);
518 } else
519 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
520 postfix);
521 if (all_post == NULL)
522 return -ENOMEM;
523 *result = all_post;
524 return 0;
525}
526
527int __iio_device_attr_init(struct device_attribute *dev_attr,
528 const char *postfix,
529 struct iio_chan_spec const *chan,
530 ssize_t (*readfunc)(struct device *dev,
531 struct device_attribute *attr,
532 char *buf),
533 ssize_t (*writefunc)(struct device *dev,
534 struct device_attribute *attr,
535 const char *buf,
536 size_t len),
537 bool generic)
538{
539 int ret;
540 char *name_format, *full_postfix;
541 sysfs_attr_init(&dev_attr->attr);
542 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
543 if (ret)
544 goto error_ret;
545
546 /* Special case for types that uses both channel numbers in naming */
547 if (chan->type == IIO_IN_DIFF && !generic)
548 name_format
549 = kasprintf(GFP_KERNEL, "%s_%s",
550 iio_chan_type_name_spec_complex[chan->type],
551 full_postfix);
552 else if (generic || !chan->indexed)
553 name_format
554 = kasprintf(GFP_KERNEL, "%s_%s",
555 iio_chan_type_name_spec_shared[chan->type],
556 full_postfix);
557 else
558 name_format
559 = kasprintf(GFP_KERNEL, "%s%d_%s",
560 iio_chan_type_name_spec_shared[chan->type],
561 chan->channel,
562 full_postfix);
563
564 if (name_format == NULL) {
565 ret = -ENOMEM;
566 goto error_free_full_postfix;
567 }
568 dev_attr->attr.name = kasprintf(GFP_KERNEL,
569 name_format,
570 chan->channel,
571 chan->channel2);
572 if (dev_attr->attr.name == NULL) {
573 ret = -ENOMEM;
574 goto error_free_name_format;
575 }
576
577 if (readfunc) {
578 dev_attr->attr.mode |= S_IRUGO;
579 dev_attr->show = readfunc;
580 }
581
582 if (writefunc) {
583 dev_attr->attr.mode |= S_IWUSR;
584 dev_attr->store = writefunc;
585 }
586 kfree(name_format);
587 kfree(full_postfix);
588
589 return 0;
590
591error_free_name_format:
592 kfree(name_format);
593error_free_full_postfix:
594 kfree(full_postfix);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100595error_ret:
596 return ret;
597}
598
Jonathan Cameron1d892712011-05-18 14:40:51 +0100599void __iio_device_attr_deinit(struct device_attribute *dev_attr)
600{
601 kfree(dev_attr->attr.name);
602}
603
604int __iio_add_chan_devattr(const char *postfix,
605 const char *group,
606 struct iio_chan_spec const *chan,
607 ssize_t (*readfunc)(struct device *dev,
608 struct device_attribute *attr,
609 char *buf),
610 ssize_t (*writefunc)(struct device *dev,
611 struct device_attribute *attr,
612 const char *buf,
613 size_t len),
614 int mask,
615 bool generic,
616 struct device *dev,
617 struct list_head *attr_list)
618{
619 int ret;
620 struct iio_dev_attr *iio_attr, *t;
621
622 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
623 if (iio_attr == NULL) {
624 ret = -ENOMEM;
625 goto error_ret;
626 }
627 ret = __iio_device_attr_init(&iio_attr->dev_attr,
628 postfix, chan,
629 readfunc, writefunc, generic);
630 if (ret)
631 goto error_iio_dev_attr_free;
632 iio_attr->c = chan;
633 iio_attr->address = mask;
634 list_for_each_entry(t, attr_list, l)
635 if (strcmp(t->dev_attr.attr.name,
636 iio_attr->dev_attr.attr.name) == 0) {
637 if (!generic)
638 dev_err(dev, "tried to double register : %s\n",
639 t->dev_attr.attr.name);
640 ret = -EBUSY;
641 goto error_device_attr_deinit;
642 }
643
644 ret = sysfs_add_file_to_group(&dev->kobj,
645 &iio_attr->dev_attr.attr, group);
646 if (ret < 0)
647 goto error_device_attr_deinit;
648
649 list_add(&iio_attr->l, attr_list);
650
651 return 0;
652
653error_device_attr_deinit:
654 __iio_device_attr_deinit(&iio_attr->dev_attr);
655error_iio_dev_attr_free:
656 kfree(iio_attr);
657error_ret:
658 return ret;
659}
660
661static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
662 struct iio_chan_spec const *chan)
663{
664 int ret, i;
665
666
667 if (chan->channel < 0)
668 return 0;
669 if (chan->processed_val)
670 ret = __iio_add_chan_devattr("input", NULL, chan,
671 &iio_read_channel_info,
672 NULL,
673 0,
674 0,
675 &dev_info->dev,
676 &dev_info->channel_attr_list);
677 else
678 ret = __iio_add_chan_devattr("raw", NULL, chan,
679 &iio_read_channel_info,
Michael Hennerichae191782011-06-10 15:40:48 +0200680 (chan->type == IIO_OUT ?
681 &iio_write_channel_info : NULL),
Jonathan Cameron1d892712011-05-18 14:40:51 +0100682 0,
683 0,
684 &dev_info->dev,
685 &dev_info->channel_attr_list);
686 if (ret)
687 goto error_ret;
688
689 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
690 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
691 NULL, chan,
692 &iio_read_channel_info,
693 &iio_write_channel_info,
694 (1 << i),
695 !(i%2),
696 &dev_info->dev,
697 &dev_info->channel_attr_list);
698 if (ret == -EBUSY && (i%2 == 0)) {
699 ret = 0;
700 continue;
701 }
702 if (ret < 0)
703 goto error_ret;
704 }
705error_ret:
706 return ret;
707}
708
709static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
710 struct iio_dev_attr *p)
711{
712 sysfs_remove_file_from_group(&dev_info->dev.kobj,
713 &p->dev_attr.attr, NULL);
714 kfree(p->dev_attr.attr.name);
715 kfree(p);
716}
717
Jonathan Cameron1b732882011-05-18 14:41:43 +0100718static ssize_t iio_show_dev_name(struct device *dev,
719 struct device_attribute *attr,
720 char *buf)
721{
722 struct iio_dev *indio_dev = dev_get_drvdata(dev);
723 return sprintf(buf, "%s\n", indio_dev->name);
724}
725
726static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
727
Jonathan Cameron1d892712011-05-18 14:40:51 +0100728static int iio_device_register_sysfs(struct iio_dev *dev_info)
729{
730 int i, ret = 0;
731 struct iio_dev_attr *p, *n;
732
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100733 if (dev_info->info->attrs) {
734 ret = sysfs_create_group(&dev_info->dev.kobj,
735 dev_info->info->attrs);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100736 if (ret) {
737 dev_err(dev_info->dev.parent,
738 "Failed to register sysfs hooks\n");
739 goto error_ret;
740 }
741 }
742
743 /*
744 * New channel registration method - relies on the fact a group does
745 * not need to be initialized if it is name is NULL.
746 */
747 INIT_LIST_HEAD(&dev_info->channel_attr_list);
748 if (dev_info->channels)
749 for (i = 0; i < dev_info->num_channels; i++) {
750 ret = iio_device_add_channel_sysfs(dev_info,
751 &dev_info
752 ->channels[i]);
753 if (ret < 0)
754 goto error_clear_attrs;
755 }
Jonathan Cameron1b732882011-05-18 14:41:43 +0100756 if (dev_info->name) {
757 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
758 &dev_attr_name.attr,
759 NULL);
760 if (ret)
761 goto error_clear_attrs;
762 }
Jonathan Cameron1d892712011-05-18 14:40:51 +0100763 return 0;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100764
Jonathan Cameron1d892712011-05-18 14:40:51 +0100765error_clear_attrs:
766 list_for_each_entry_safe(p, n,
767 &dev_info->channel_attr_list, l) {
768 list_del(&p->l);
769 iio_device_remove_and_free_read_attr(dev_info, p);
770 }
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100771 if (dev_info->info->attrs)
772 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100773error_ret:
774 return ret;
775
776}
777
Jonathan Cameron847ec802009-08-18 18:06:19 +0100778static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
779{
Jonathan Cameron1d892712011-05-18 14:40:51 +0100780
781 struct iio_dev_attr *p, *n;
Jonathan Cameron1b732882011-05-18 14:41:43 +0100782 if (dev_info->name)
783 sysfs_remove_file_from_group(&dev_info->dev.kobj,
784 &dev_attr_name.attr,
785 NULL);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100786 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
787 list_del(&p->l);
788 iio_device_remove_and_free_read_attr(dev_info, p);
789 }
790
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100791 if (dev_info->info->attrs)
792 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100793}
794
Andrew Mortonf3cdc282010-04-27 11:29:54 -0700795/* Return a negative errno on failure */
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100796int iio_get_new_ida_val(struct ida *this_ida)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100797{
798 int ret;
799 int val;
800
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100801ida_again:
802 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
Jonathan Cameron847ec802009-08-18 18:06:19 +0100803 return -ENOMEM;
804
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100805 spin_lock(&iio_ida_lock);
806 ret = ida_get_new(this_ida, &val);
807 spin_unlock(&iio_ida_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100808 if (unlikely(ret == -EAGAIN))
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100809 goto ida_again;
Jonathan Cameron847ec802009-08-18 18:06:19 +0100810 else if (unlikely(ret))
811 return ret;
812
813 return val;
814}
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100815EXPORT_SYMBOL(iio_get_new_ida_val);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100816
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100817void iio_free_ida_val(struct ida *this_ida, int id)
Jonathan Cameron847ec802009-08-18 18:06:19 +0100818{
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100819 spin_lock(&iio_ida_lock);
820 ida_remove(this_ida, id);
821 spin_unlock(&iio_ida_lock);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100822}
Jonathan Cameronb156cf72010-09-04 17:54:43 +0100823EXPORT_SYMBOL(iio_free_ida_val);
Jonathan Cameron847ec802009-08-18 18:06:19 +0100824
Jonathan Cameron1d892712011-05-18 14:40:51 +0100825static const char * const iio_ev_type_text[] = {
826 [IIO_EV_TYPE_THRESH] = "thresh",
827 [IIO_EV_TYPE_MAG] = "mag",
828 [IIO_EV_TYPE_ROC] = "roc"
829};
830
831static const char * const iio_ev_dir_text[] = {
832 [IIO_EV_DIR_EITHER] = "either",
833 [IIO_EV_DIR_RISING] = "rising",
834 [IIO_EV_DIR_FALLING] = "falling"
835};
836
837static ssize_t iio_ev_state_store(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf,
840 size_t len)
841{
842 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100843 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100844 int ret;
Jonathan Cameronc74b0de2011-05-18 14:42:33 +0100845 bool val;
846
847 ret = strtobool(buf, &val);
848 if (ret < 0)
849 return ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100850
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100851 ret = indio_dev->info->write_event_config(indio_dev,
852 this_attr->address,
853 val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100854 return (ret < 0) ? ret : len;
855}
856
857static ssize_t iio_ev_state_show(struct device *dev,
858 struct device_attribute *attr,
859 char *buf)
860{
861 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100862 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100863 int val = indio_dev->info->read_event_config(indio_dev,
864 this_attr->address);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100865
866 if (val < 0)
867 return val;
868 else
869 return sprintf(buf, "%d\n", val);
870}
871
872static ssize_t iio_ev_value_show(struct device *dev,
873 struct device_attribute *attr,
874 char *buf)
875{
876 struct iio_dev *indio_dev = dev_get_drvdata(dev);
877 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
878 int val, ret;
879
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100880 ret = indio_dev->info->read_event_value(indio_dev,
881 this_attr->address, &val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100882 if (ret < 0)
883 return ret;
884
885 return sprintf(buf, "%d\n", val);
886}
887
888static ssize_t iio_ev_value_store(struct device *dev,
889 struct device_attribute *attr,
890 const char *buf,
891 size_t len)
892{
893 struct iio_dev *indio_dev = dev_get_drvdata(dev);
894 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
895 unsigned long val;
896 int ret;
897
898 ret = strict_strtoul(buf, 10, &val);
899 if (ret)
900 return ret;
901
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100902 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
903 val);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100904 if (ret < 0)
905 return ret;
906
907 return len;
908}
909
Jonathan Cameron1d892712011-05-18 14:40:51 +0100910static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
911 struct iio_chan_spec const *chan)
912{
913
914 int ret = 0, i, mask;
915 char *postfix;
916 if (!chan->event_mask)
917 return 0;
918
919 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
920 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
921 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
922 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
923 if (postfix == NULL) {
924 ret = -ENOMEM;
925 goto error_ret;
926 }
927 switch (chan->type) {
928 /* Switch this to a table at some point */
929 case IIO_IN:
930 mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
931 i/IIO_EV_TYPE_MAX,
932 i%IIO_EV_TYPE_MAX);
933 break;
934 case IIO_ACCEL:
935 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
936 i/IIO_EV_TYPE_MAX,
937 i%IIO_EV_TYPE_MAX);
938 break;
939 case IIO_IN_DIFF:
940 mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
941 chan->channel2,
942 i/IIO_EV_TYPE_MAX,
943 i%IIO_EV_TYPE_MAX);
944 break;
945 default:
946 printk(KERN_INFO "currently unhandled type of event\n");
947 }
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100948 ret = __iio_add_chan_devattr(postfix,
949 NULL,
950 chan,
951 &iio_ev_state_show,
952 iio_ev_state_store,
953 mask,
954 /*HACK. - limits us to one
955 event interface - fix by
956 extending the bitmask - but
957 how far*/
958 0,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100959 &dev_info->event_interfaces[0].dev,
Jonathan Cameronaaf370d2011-05-18 14:41:16 +0100960 &dev_info->event_interfaces[0].
961 dev_attr_list);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100962 kfree(postfix);
963 if (ret)
964 goto error_ret;
965
966 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
967 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
968 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
969 if (postfix == NULL) {
970 ret = -ENOMEM;
971 goto error_ret;
972 }
973 ret = __iio_add_chan_devattr(postfix, NULL, chan,
974 iio_ev_value_show,
975 iio_ev_value_store,
976 mask,
977 0,
978 &dev_info->event_interfaces[0]
979 .dev,
980 &dev_info->event_interfaces[0]
981 .dev_attr_list);
982 kfree(postfix);
983 if (ret)
984 goto error_ret;
985
986 }
987
988error_ret:
989 return ret;
990}
991
992static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
993 const char *groupname,
994 int num)
995{
996 struct iio_dev_attr *p, *n;
Jonathan Cameron1d892712011-05-18 14:40:51 +0100997 list_for_each_entry_safe(p, n,
998 &dev_info->event_interfaces[num].
999 dev_attr_list, l) {
1000 sysfs_remove_file_from_group(&dev_info
1001 ->event_interfaces[num].dev.kobj,
1002 &p->dev_attr.attr,
1003 groupname);
1004 kfree(p->dev_attr.attr.name);
1005 kfree(p);
1006 }
Jonathan Cameron1d892712011-05-18 14:40:51 +01001007}
1008
Jonathan Cameron847ec802009-08-18 18:06:19 +01001009static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
1010{
Jonathan Cameron1d892712011-05-18 14:40:51 +01001011 int j;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001012 int ret;
Jonathan Cameron1d892712011-05-18 14:40:51 +01001013 INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
1014 /* Dynically created from the channels array */
1015 if (dev_info->channels) {
1016 for (j = 0; j < dev_info->num_channels; j++) {
1017 ret = iio_device_add_event_sysfs(dev_info,
1018 &dev_info
1019 ->channels[j]);
1020 if (ret)
1021 goto error_clear_attrs;
1022 }
1023 }
Jonathan Cameron847ec802009-08-18 18:06:19 +01001024 return 0;
1025
Jonathan Cameron1d892712011-05-18 14:40:51 +01001026error_clear_attrs:
Jonathan Cameron41c77522011-05-18 14:42:12 +01001027 __iio_remove_all_event_sysfs(dev_info, NULL, i);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001028
1029 return ret;
1030}
1031
1032static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1033 int i)
1034{
Jonathan Cameron41c77522011-05-18 14:42:12 +01001035 __iio_remove_all_event_sysfs(dev_info, NULL, i);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001036 return 0;
1037}
1038
1039static int iio_device_register_eventset(struct iio_dev *dev_info)
1040{
1041 int ret = 0, i, j;
1042
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001043 if (dev_info->info->num_interrupt_lines == 0)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001044 return 0;
1045
1046 dev_info->event_interfaces =
1047 kzalloc(sizeof(struct iio_event_interface)
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001048 *dev_info->info->num_interrupt_lines,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001049 GFP_KERNEL);
1050 if (dev_info->event_interfaces == NULL) {
1051 ret = -ENOMEM;
1052 goto error_ret;
1053 }
1054
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001055 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
Jonathan Cameron847ec802009-08-18 18:06:19 +01001056 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
Jonathan Cameronc74b0de2011-05-18 14:42:33 +01001057 dev_name(&dev_info->dev),
1058 i,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001059 dev_info->info->driver_module,
Jonathan Cameron847ec802009-08-18 18:06:19 +01001060 &dev_info->dev);
1061 if (ret) {
1062 dev_err(&dev_info->dev,
1063 "Could not get chrdev interface\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001064 goto error_free_setup_ev_ints;
1065 }
Jonathan Cameron847ec802009-08-18 18:06:19 +01001066
Jonathan Cameron5cba2202010-05-04 14:43:01 +01001067 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1068 (void *)dev_info);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001069
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001070 if (dev_info->info->event_attrs != NULL)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001071 ret = sysfs_create_group(&dev_info
1072 ->event_interfaces[i]
1073 .dev.kobj,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001074 &dev_info->info
1075 ->event_attrs[i]);
Jonathan Cameron5cba2202010-05-04 14:43:01 +01001076
Jonathan Cameron847ec802009-08-18 18:06:19 +01001077 if (ret) {
1078 dev_err(&dev_info->dev,
1079 "Failed to register sysfs for event attrs");
1080 goto error_remove_sysfs_interfaces;
1081 }
1082 }
1083
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001084 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
Jonathan Cameron847ec802009-08-18 18:06:19 +01001085 ret = __iio_add_event_config_attrs(dev_info, i);
1086 if (ret)
1087 goto error_unregister_config_attrs;
1088 }
1089
1090 return 0;
1091
1092error_unregister_config_attrs:
1093 for (j = 0; j < i; j++)
1094 __iio_remove_event_config_attrs(dev_info, i);
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001095 i = dev_info->info->num_interrupt_lines - 1;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001096error_remove_sysfs_interfaces:
1097 for (j = 0; j < i; j++)
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001098 if (dev_info->info->event_attrs != NULL)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001099 sysfs_remove_group(&dev_info
Jonathan Cameron5cba2202010-05-04 14:43:01 +01001100 ->event_interfaces[j].dev.kobj,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001101 &dev_info->info->event_attrs[j]);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001102error_free_setup_ev_ints:
Jonathan Cameron3d550fb2010-06-26 12:54:18 +01001103 for (j = 0; j < i; j++)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001104 iio_free_ev_int(&dev_info->event_interfaces[j]);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001105 kfree(dev_info->event_interfaces);
1106error_ret:
1107
1108 return ret;
1109}
1110
1111static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1112{
1113 int i;
1114
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001115 if (dev_info->info->num_interrupt_lines == 0)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001116 return;
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001117 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
Jonathan Cameron1d892712011-05-18 14:40:51 +01001118 __iio_remove_event_config_attrs(dev_info, i);
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001119 if (dev_info->info->event_attrs != NULL)
Jonathan Cameron1d892712011-05-18 14:40:51 +01001120 sysfs_remove_group(&dev_info
1121 ->event_interfaces[i].dev.kobj,
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001122 &dev_info->info->event_attrs[i]);
Jonathan Cameron1d892712011-05-18 14:40:51 +01001123 }
Jonathan Cameron847ec802009-08-18 18:06:19 +01001124
Jonathan Cameron6fe81352011-05-18 14:42:37 +01001125 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001126 iio_free_ev_int(&dev_info->event_interfaces[i]);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001127 kfree(dev_info->event_interfaces);
1128}
1129
1130static void iio_dev_release(struct device *device)
1131{
Jonathan Cameron847ec802009-08-18 18:06:19 +01001132 iio_put();
Jonathan Cameronc74b0de2011-05-18 14:42:33 +01001133 kfree(to_iio_dev(device));
Jonathan Cameron847ec802009-08-18 18:06:19 +01001134}
1135
1136static struct device_type iio_dev_type = {
1137 .name = "iio_device",
1138 .release = iio_dev_release,
1139};
1140
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001141struct iio_dev *iio_allocate_device(int sizeof_priv)
Jonathan Cameron847ec802009-08-18 18:06:19 +01001142{
Jonathan Cameron6f7c8ee2011-04-15 18:55:56 +01001143 struct iio_dev *dev;
1144 size_t alloc_size;
1145
1146 alloc_size = sizeof(struct iio_dev);
1147 if (sizeof_priv) {
1148 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1149 alloc_size += sizeof_priv;
1150 }
1151 /* ensure 32-byte alignment of whole construct ? */
1152 alloc_size += IIO_ALIGN - 1;
1153
1154 dev = kzalloc(alloc_size, GFP_KERNEL);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001155
1156 if (dev) {
1157 dev->dev.type = &iio_dev_type;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +01001158 dev->dev.bus = &iio_bus_type;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001159 device_initialize(&dev->dev);
1160 dev_set_drvdata(&dev->dev, (void *)dev);
1161 mutex_init(&dev->mlock);
1162 iio_get();
1163 }
1164
1165 return dev;
1166}
1167EXPORT_SYMBOL(iio_allocate_device);
1168
1169void iio_free_device(struct iio_dev *dev)
1170{
1171 if (dev)
1172 iio_put_device(dev);
1173}
1174EXPORT_SYMBOL(iio_free_device);
1175
1176int iio_device_register(struct iio_dev *dev_info)
1177{
1178 int ret;
1179
Jonathan Cameronc74b0de2011-05-18 14:42:33 +01001180 dev_info->id = iio_get_new_ida_val(&iio_ida);
1181 if (dev_info->id < 0) {
1182 ret = dev_info->id;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001183 dev_err(&dev_info->dev, "Failed to get id\n");
1184 goto error_ret;
1185 }
1186 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1187
1188 ret = device_add(&dev_info->dev);
1189 if (ret)
Jonathan Cameronb156cf72010-09-04 17:54:43 +01001190 goto error_free_ida;
Jonathan Cameron847ec802009-08-18 18:06:19 +01001191 ret = iio_device_register_sysfs(dev_info);
1192 if (ret) {
1193 dev_err(dev_info->dev.parent,
1194 "Failed to register sysfs interfaces\n");
1195 goto error_del_device;
1196 }
1197 ret = iio_device_register_eventset(dev_info);
1198 if (ret) {
1199 dev_err(dev_info->dev.parent,
Roel Van Nyenc849d252010-04-29 19:27:31 +02001200 "Failed to register event set\n");
Jonathan Cameron847ec802009-08-18 18:06:19 +01001201 goto error_free_sysfs;
1202 }
1203 if (dev_info->modes & INDIO_RING_TRIGGERED)
1204 iio_device_register_trigger_consumer(dev_info);
1205
1206 return 0;
1207
1208error_free_sysfs:
1209 iio_device_unregister_sysfs(dev_info);
1210error_del_device:
1211 device_del(&dev_info->dev);
Jonathan Cameronb156cf72010-09-04 17:54:43 +01001212error_free_ida:
Jonathan Cameronc74b0de2011-05-18 14:42:33 +01001213 iio_free_ida_val(&iio_ida, dev_info->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001214error_ret:
1215 return ret;
1216}
1217EXPORT_SYMBOL(iio_device_register);
1218
1219void iio_device_unregister(struct iio_dev *dev_info)
1220{
1221 if (dev_info->modes & INDIO_RING_TRIGGERED)
1222 iio_device_unregister_trigger_consumer(dev_info);
1223 iio_device_unregister_eventset(dev_info);
1224 iio_device_unregister_sysfs(dev_info);
Jonathan Cameronc74b0de2011-05-18 14:42:33 +01001225 iio_free_ida_val(&iio_ida, dev_info->id);
Jonathan Cameron847ec802009-08-18 18:06:19 +01001226 device_unregister(&dev_info->dev);
1227}
1228EXPORT_SYMBOL(iio_device_unregister);
1229
1230void iio_put(void)
1231{
1232 module_put(THIS_MODULE);
1233}
1234
1235void iio_get(void)
1236{
1237 __module_get(THIS_MODULE);
1238}
1239
1240subsys_initcall(iio_init);
1241module_exit(iio_exit);
1242
1243MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1244MODULE_DESCRIPTION("Industrial I/O core");
1245MODULE_LICENSE("GPL");