ath6kl: Remove auth type fall back in auto authentication mode
[linux-2.6/btrfs-unstable.git] / drivers / staging / iio / industrialio-core.c
blob19819e7578c6175f51a9c8b3b4793cbb4f0ab1b9
1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Based on elements of hwmon and input subsystems.
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>
19 #include <linux/poll.h>
20 #include <linux/sched.h>
21 #include <linux/wait.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
24 #include "iio.h"
25 #include "trigger_consumer.h"
27 #define IIO_ID_PREFIX "device"
28 #define IIO_ID_FORMAT IIO_ID_PREFIX "%d"
30 /* IDR to assign each registered device a unique id*/
31 static DEFINE_IDA(iio_ida);
32 /* IDR to allocate character device minor numbers */
33 static DEFINE_IDA(iio_chrdev_ida);
34 /* Lock used to protect both of the above */
35 static DEFINE_SPINLOCK(iio_ida_lock);
37 dev_t iio_devt;
38 EXPORT_SYMBOL(iio_devt);
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type = {
42 .name = "iio",
44 EXPORT_SYMBOL(iio_bus_type);
46 static const char * const iio_chan_type_name_spec_shared[] = {
47 [IIO_IN] = "in",
48 [IIO_OUT] = "out",
49 [IIO_CURRENT] = "current",
50 [IIO_POWER] = "power",
51 [IIO_ACCEL] = "accel",
52 [IIO_IN_DIFF] = "in-in",
53 [IIO_GYRO] = "gyro",
54 [IIO_MAGN] = "magn",
55 [IIO_LIGHT] = "illuminance",
56 [IIO_INTENSITY] = "intensity",
57 [IIO_PROXIMITY] = "proximity",
58 [IIO_TEMP] = "temp",
59 [IIO_INCLI] = "incli",
60 [IIO_ROT] = "rot",
61 [IIO_ANGL] = "angl",
62 [IIO_TIMESTAMP] = "timestamp",
65 static const char * const iio_chan_type_name_spec_complex[] = {
66 [IIO_IN_DIFF] = "in%d-in%d",
69 static const char * const iio_modifier_names_light[] = {
70 [IIO_MOD_LIGHT_BOTH] = "both",
71 [IIO_MOD_LIGHT_IR] = "ir",
74 static const char * const iio_modifier_names_axial[] = {
75 [IIO_MOD_X] = "x",
76 [IIO_MOD_Y] = "y",
77 [IIO_MOD_Z] = "z",
80 /* relies on pairs of these shared then separate */
81 static 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",
86 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
87 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
90 int iio_push_event(struct iio_dev *dev_info,
91 int ev_line,
92 int ev_code,
93 s64 timestamp)
95 struct iio_event_interface *ev_int
96 = &dev_info->event_interfaces[ev_line];
97 struct iio_detected_event_list *ev;
98 int ret = 0;
100 /* Does anyone care? */
101 mutex_lock(&ev_int->event_list_lock);
102 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
103 if (ev_int->current_events == ev_int->max_events) {
104 mutex_unlock(&ev_int->event_list_lock);
105 return 0;
107 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
108 if (ev == NULL) {
109 ret = -ENOMEM;
110 mutex_unlock(&ev_int->event_list_lock);
111 goto error_ret;
113 ev->ev.id = ev_code;
114 ev->ev.timestamp = timestamp;
116 list_add_tail(&ev->list, &ev_int->det_events);
117 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);
123 error_ret:
124 return ret;
126 EXPORT_SYMBOL(iio_push_event);
129 /* This turns up an awful lot */
130 ssize_t iio_read_const_attr(struct device *dev,
131 struct device_attribute *attr,
132 char *buf)
134 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
136 EXPORT_SYMBOL(iio_read_const_attr);
139 static ssize_t iio_event_chrdev_read(struct file *filep,
140 char __user *buf,
141 size_t count,
142 loff_t *f_ps)
144 struct iio_event_interface *ev_int = filep->private_data;
145 struct iio_detected_event_list *el;
146 int ret;
147 size_t len;
149 mutex_lock(&ev_int->event_list_lock);
150 if (list_empty(&ev_int->det_events)) {
151 if (filep->f_flags & O_NONBLOCK) {
152 ret = -EAGAIN;
153 goto error_mutex_unlock;
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
159 ->det_events));
160 if (ret)
161 goto error_ret;
162 /* Single access device so no one else can get the data */
163 mutex_lock(&ev_int->event_list_lock);
166 el = list_first_entry(&ev_int->det_events,
167 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;
174 list_del(&el->list);
175 ev_int->current_events--;
176 mutex_unlock(&ev_int->event_list_lock);
177 kfree(el);
179 return len;
181 error_mutex_unlock:
182 mutex_unlock(&ev_int->event_list_lock);
183 error_ret:
185 return ret;
188 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
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;
194 mutex_lock(&ev_int->event_list_lock);
195 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
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.
201 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
202 list_del(&el->list);
203 kfree(el);
205 mutex_unlock(&ev_int->event_list_lock);
207 return 0;
210 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
212 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
213 struct iio_event_interface *ev_int = hand->private;
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;
221 filep->private_data = hand->private;
222 mutex_unlock(&ev_int->event_list_lock);
224 return 0;
227 static 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,
232 .llseek = noop_llseek,
235 static void iio_event_dev_release(struct device *dev)
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));
243 static struct device_type iio_event_type = {
244 .release = iio_event_dev_release,
247 int iio_device_get_chrdev_minor(void)
249 int ret, val;
251 ida_again:
252 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
253 return -ENOMEM;
254 spin_lock(&iio_ida_lock);
255 ret = ida_get_new(&iio_chrdev_ida, &val);
256 spin_unlock(&iio_ida_lock);
257 if (unlikely(ret == -EAGAIN))
258 goto ida_again;
259 else if (unlikely(ret))
260 return ret;
261 if (val > IIO_DEV_MAX)
262 return -ENOMEM;
263 return val;
266 void iio_device_free_chrdev_minor(int val)
268 spin_lock(&iio_ida_lock);
269 ida_remove(&iio_chrdev_ida, val);
270 spin_unlock(&iio_ida_lock);
273 static int iio_setup_ev_int(struct iio_event_interface *ev_int,
274 const char *dev_name,
275 int index,
276 struct module *owner,
277 struct device *dev)
279 int ret, minor;
281 ev_int->dev.bus = &iio_bus_type;
282 ev_int->dev.parent = dev;
283 ev_int->dev.type = &iio_event_type;
284 device_initialize(&ev_int->dev);
286 minor = iio_device_get_chrdev_minor();
287 if (minor < 0) {
288 ret = minor;
289 goto error_device_put;
291 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
292 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
294 ret = device_add(&ev_int->dev);
295 if (ret)
296 goto error_free_minor;
298 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
299 ev_int->handler.chrdev.owner = owner;
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;
305 INIT_LIST_HEAD(&ev_int->det_events);
306 init_waitqueue_head(&ev_int->wait);
307 ev_int->handler.private = ev_int;
308 ev_int->handler.flags = 0;
310 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
311 if (ret)
312 goto error_unreg_device;
314 return 0;
316 error_unreg_device:
317 device_unregister(&ev_int->dev);
318 error_free_minor:
319 iio_device_free_chrdev_minor(minor);
320 error_device_put:
321 put_device(&ev_int->dev);
323 return ret;
326 static void iio_free_ev_int(struct iio_event_interface *ev_int)
328 device_unregister(&ev_int->dev);
329 put_device(&ev_int->dev);
332 static int __init iio_dev_init(void)
334 int err;
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__);
341 return err;
344 static void __exit iio_dev_exit(void)
346 if (iio_devt)
347 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
350 static int __init iio_init(void)
352 int ret;
354 /* Register sysfs bus */
355 ret = bus_register(&iio_bus_type);
356 if (ret < 0) {
357 printk(KERN_ERR
358 "%s could not register bus type\n",
359 __FILE__);
360 goto error_nothing;
363 ret = iio_dev_init();
364 if (ret < 0)
365 goto error_unregister_bus_type;
367 return 0;
369 error_unregister_bus_type:
370 bus_unregister(&iio_bus_type);
371 error_nothing:
372 return ret;
375 static void __exit iio_exit(void)
377 iio_dev_exit();
378 bus_unregister(&iio_bus_type);
381 static ssize_t iio_read_channel_info(struct device *dev,
382 struct device_attribute *attr,
383 char *buf)
385 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;
388 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
389 &val, &val2, this_attr->address);
391 if (ret < 0)
392 return ret;
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);
401 } 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);
406 } else
407 return 0;
410 static ssize_t iio_write_channel_info(struct device *dev,
411 struct device_attribute *attr,
412 const char *buf,
413 size_t len)
415 struct iio_dev *indio_dev = dev_get_drvdata(dev);
416 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
417 int ret, integer = 0, fract = 0, fract_mult = 100000;
418 bool integer_part = true, negative = false;
420 /* Assumes decimal - precision based on number of digits */
421 if (!indio_dev->info->write_raw)
422 return -EINVAL;
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;
437 if (buf[0] == '-') {
438 negative = true;
439 buf++;
442 while (*buf) {
443 if ('0' <= *buf && *buf <= '9') {
444 if (integer_part)
445 integer = integer*10 + *buf - '0';
446 else {
447 fract += fract_mult*(*buf - '0');
448 if (fract_mult == 1)
449 break;
450 fract_mult /= 10;
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;
462 buf++;
464 if (negative) {
465 if (integer)
466 integer = -integer;
467 else
468 fract = -fract;
471 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
472 integer, fract, this_attr->address);
473 if (ret)
474 return ret;
476 return len;
479 static int __iio_build_postfix(struct iio_chan_spec const *chan,
480 bool generic,
481 const char *postfix,
482 char **result)
484 char *all_post;
485 /* 3 options - generic, extend_name, modified - if generic, extend_name
486 * and modified cannot apply.*/
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;
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;
527 int __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)
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;
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);
564 if (name_format == NULL) {
565 ret = -ENOMEM;
566 goto error_free_full_postfix;
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;
577 if (readfunc) {
578 dev_attr->attr.mode |= S_IRUGO;
579 dev_attr->show = readfunc;
582 if (writefunc) {
583 dev_attr->attr.mode |= S_IWUSR;
584 dev_attr->store = writefunc;
586 kfree(name_format);
587 kfree(full_postfix);
589 return 0;
591 error_free_name_format:
592 kfree(name_format);
593 error_free_full_postfix:
594 kfree(full_postfix);
595 error_ret:
596 return ret;
599 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
601 kfree(dev_attr->attr.name);
604 int __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)
619 int ret;
620 struct iio_dev_attr *iio_attr, *t;
622 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
623 if (iio_attr == NULL) {
624 ret = -ENOMEM;
625 goto error_ret;
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;
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;
649 list_add(&iio_attr->l, attr_list);
651 return 0;
653 error_device_attr_deinit:
654 __iio_device_attr_deinit(&iio_attr->dev_attr);
655 error_iio_dev_attr_free:
656 kfree(iio_attr);
657 error_ret:
658 return ret;
661 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
662 struct iio_chan_spec const *chan)
664 int ret, i;
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,
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,
680 (chan->type == IIO_OUT ?
681 &iio_write_channel_info : NULL),
684 &dev_info->dev,
685 &dev_info->channel_attr_list);
686 if (ret)
687 goto error_ret;
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;
702 if (ret < 0)
703 goto error_ret;
705 error_ret:
706 return ret;
709 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
710 struct iio_dev_attr *p)
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);
718 static ssize_t iio_show_dev_name(struct device *dev,
719 struct device_attribute *attr,
720 char *buf)
722 struct iio_dev *indio_dev = dev_get_drvdata(dev);
723 return sprintf(buf, "%s\n", indio_dev->name);
726 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
728 static int iio_device_register_sysfs(struct iio_dev *dev_info)
730 int i, ret = 0;
731 struct iio_dev_attr *p, *n;
733 if (dev_info->info->attrs) {
734 ret = sysfs_create_group(&dev_info->dev.kobj,
735 dev_info->info->attrs);
736 if (ret) {
737 dev_err(dev_info->dev.parent,
738 "Failed to register sysfs hooks\n");
739 goto error_ret;
744 * New channel registration method - relies on the fact a group does
745 * not need to be initialized if it is name is NULL.
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;
756 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;
763 return 0;
765 error_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);
771 if (dev_info->info->attrs)
772 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
773 error_ret:
774 return ret;
778 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
781 struct iio_dev_attr *p, *n;
782 if (dev_info->name)
783 sysfs_remove_file_from_group(&dev_info->dev.kobj,
784 &dev_attr_name.attr,
785 NULL);
786 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);
791 if (dev_info->info->attrs)
792 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
795 /* Return a negative errno on failure */
796 int iio_get_new_ida_val(struct ida *this_ida)
798 int ret;
799 int val;
801 ida_again:
802 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
803 return -ENOMEM;
805 spin_lock(&iio_ida_lock);
806 ret = ida_get_new(this_ida, &val);
807 spin_unlock(&iio_ida_lock);
808 if (unlikely(ret == -EAGAIN))
809 goto ida_again;
810 else if (unlikely(ret))
811 return ret;
813 return val;
815 EXPORT_SYMBOL(iio_get_new_ida_val);
817 void iio_free_ida_val(struct ida *this_ida, int id)
819 spin_lock(&iio_ida_lock);
820 ida_remove(this_ida, id);
821 spin_unlock(&iio_ida_lock);
823 EXPORT_SYMBOL(iio_free_ida_val);
825 static 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"
831 static 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"
837 static ssize_t iio_ev_state_store(struct device *dev,
838 struct device_attribute *attr,
839 const char *buf,
840 size_t len)
842 struct iio_dev *indio_dev = dev_get_drvdata(dev);
843 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
844 int ret;
845 bool val;
847 ret = strtobool(buf, &val);
848 if (ret < 0)
849 return ret;
851 ret = indio_dev->info->write_event_config(indio_dev,
852 this_attr->address,
853 val);
854 return (ret < 0) ? ret : len;
857 static ssize_t iio_ev_state_show(struct device *dev,
858 struct device_attribute *attr,
859 char *buf)
861 struct iio_dev *indio_dev = dev_get_drvdata(dev);
862 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
863 int val = indio_dev->info->read_event_config(indio_dev,
864 this_attr->address);
866 if (val < 0)
867 return val;
868 else
869 return sprintf(buf, "%d\n", val);
872 static ssize_t iio_ev_value_show(struct device *dev,
873 struct device_attribute *attr,
874 char *buf)
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;
880 ret = indio_dev->info->read_event_value(indio_dev,
881 this_attr->address, &val);
882 if (ret < 0)
883 return ret;
885 return sprintf(buf, "%d\n", val);
888 static ssize_t iio_ev_value_store(struct device *dev,
889 struct device_attribute *attr,
890 const char *buf,
891 size_t len)
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;
898 ret = strict_strtoul(buf, 10, &val);
899 if (ret)
900 return ret;
902 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
903 val);
904 if (ret < 0)
905 return ret;
907 return len;
910 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
911 struct iio_chan_spec const *chan)
914 int ret = 0, i, mask;
915 char *postfix;
916 if (!chan->event_mask)
917 return 0;
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;
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");
948 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*/
959 &dev_info->event_interfaces[0].dev,
960 &dev_info->event_interfaces[0].
961 dev_attr_list);
962 kfree(postfix);
963 if (ret)
964 goto error_ret;
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;
973 ret = __iio_add_chan_devattr(postfix, NULL, chan,
974 iio_ev_value_show,
975 iio_ev_value_store,
976 mask,
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;
988 error_ret:
989 return ret;
992 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
993 const char *groupname,
994 int num)
996 struct iio_dev_attr *p, *n;
997 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);
1009 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
1011 int j;
1012 int ret;
1013 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;
1024 return 0;
1026 error_clear_attrs:
1027 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1029 return ret;
1032 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1033 int i)
1035 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1036 return 0;
1039 static int iio_device_register_eventset(struct iio_dev *dev_info)
1041 int ret = 0, i, j;
1043 if (dev_info->info->num_interrupt_lines == 0)
1044 return 0;
1046 dev_info->event_interfaces =
1047 kzalloc(sizeof(struct iio_event_interface)
1048 *dev_info->info->num_interrupt_lines,
1049 GFP_KERNEL);
1050 if (dev_info->event_interfaces == NULL) {
1051 ret = -ENOMEM;
1052 goto error_ret;
1055 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1056 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1057 dev_name(&dev_info->dev),
1059 dev_info->info->driver_module,
1060 &dev_info->dev);
1061 if (ret) {
1062 dev_err(&dev_info->dev,
1063 "Could not get chrdev interface\n");
1064 goto error_free_setup_ev_ints;
1067 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1068 (void *)dev_info);
1070 if (dev_info->info->event_attrs != NULL)
1071 ret = sysfs_create_group(&dev_info
1072 ->event_interfaces[i]
1073 .dev.kobj,
1074 &dev_info->info
1075 ->event_attrs[i]);
1077 if (ret) {
1078 dev_err(&dev_info->dev,
1079 "Failed to register sysfs for event attrs");
1080 goto error_remove_sysfs_interfaces;
1084 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1085 ret = __iio_add_event_config_attrs(dev_info, i);
1086 if (ret)
1087 goto error_unregister_config_attrs;
1090 return 0;
1092 error_unregister_config_attrs:
1093 for (j = 0; j < i; j++)
1094 __iio_remove_event_config_attrs(dev_info, i);
1095 i = dev_info->info->num_interrupt_lines - 1;
1096 error_remove_sysfs_interfaces:
1097 for (j = 0; j < i; j++)
1098 if (dev_info->info->event_attrs != NULL)
1099 sysfs_remove_group(&dev_info
1100 ->event_interfaces[j].dev.kobj,
1101 &dev_info->info->event_attrs[j]);
1102 error_free_setup_ev_ints:
1103 for (j = 0; j < i; j++)
1104 iio_free_ev_int(&dev_info->event_interfaces[j]);
1105 kfree(dev_info->event_interfaces);
1106 error_ret:
1108 return ret;
1111 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1113 int i;
1115 if (dev_info->info->num_interrupt_lines == 0)
1116 return;
1117 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1118 __iio_remove_event_config_attrs(dev_info, i);
1119 if (dev_info->info->event_attrs != NULL)
1120 sysfs_remove_group(&dev_info
1121 ->event_interfaces[i].dev.kobj,
1122 &dev_info->info->event_attrs[i]);
1125 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
1126 iio_free_ev_int(&dev_info->event_interfaces[i]);
1127 kfree(dev_info->event_interfaces);
1130 static void iio_dev_release(struct device *device)
1132 iio_put();
1133 kfree(to_iio_dev(device));
1136 static struct device_type iio_dev_type = {
1137 .name = "iio_device",
1138 .release = iio_dev_release,
1141 struct iio_dev *iio_allocate_device(int sizeof_priv)
1143 struct iio_dev *dev;
1144 size_t alloc_size;
1146 alloc_size = sizeof(struct iio_dev);
1147 if (sizeof_priv) {
1148 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1149 alloc_size += sizeof_priv;
1151 /* ensure 32-byte alignment of whole construct ? */
1152 alloc_size += IIO_ALIGN - 1;
1154 dev = kzalloc(alloc_size, GFP_KERNEL);
1156 if (dev) {
1157 dev->dev.type = &iio_dev_type;
1158 dev->dev.bus = &iio_bus_type;
1159 device_initialize(&dev->dev);
1160 dev_set_drvdata(&dev->dev, (void *)dev);
1161 mutex_init(&dev->mlock);
1162 iio_get();
1165 return dev;
1167 EXPORT_SYMBOL(iio_allocate_device);
1169 void iio_free_device(struct iio_dev *dev)
1171 if (dev)
1172 iio_put_device(dev);
1174 EXPORT_SYMBOL(iio_free_device);
1176 int iio_device_register(struct iio_dev *dev_info)
1178 int ret;
1180 dev_info->id = iio_get_new_ida_val(&iio_ida);
1181 if (dev_info->id < 0) {
1182 ret = dev_info->id;
1183 dev_err(&dev_info->dev, "Failed to get id\n");
1184 goto error_ret;
1186 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1188 ret = device_add(&dev_info->dev);
1189 if (ret)
1190 goto error_free_ida;
1191 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;
1197 ret = iio_device_register_eventset(dev_info);
1198 if (ret) {
1199 dev_err(dev_info->dev.parent,
1200 "Failed to register event set\n");
1201 goto error_free_sysfs;
1203 if (dev_info->modes & INDIO_RING_TRIGGERED)
1204 iio_device_register_trigger_consumer(dev_info);
1206 return 0;
1208 error_free_sysfs:
1209 iio_device_unregister_sysfs(dev_info);
1210 error_del_device:
1211 device_del(&dev_info->dev);
1212 error_free_ida:
1213 iio_free_ida_val(&iio_ida, dev_info->id);
1214 error_ret:
1215 return ret;
1217 EXPORT_SYMBOL(iio_device_register);
1219 void iio_device_unregister(struct iio_dev *dev_info)
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);
1225 iio_free_ida_val(&iio_ida, dev_info->id);
1226 device_unregister(&dev_info->dev);
1228 EXPORT_SYMBOL(iio_device_unregister);
1230 void iio_put(void)
1232 module_put(THIS_MODULE);
1235 void iio_get(void)
1237 __module_get(THIS_MODULE);
1240 subsys_initcall(iio_init);
1241 module_exit(iio_exit);
1243 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1244 MODULE_DESCRIPTION("Industrial I/O core");
1245 MODULE_LICENSE("GPL");