thinkpad-acpi: handle HKEY 0x4010, 0x4011 events
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / industrialio-core.c
blob94d3bfaa061d8e3b0ee3002f0d85ce10bfbcda95
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_TIMESTAMP] = "timestamp",
48 [IIO_ACCEL] = "accel",
49 [IIO_IN] = "in",
50 [IIO_CURRENT] = "current",
51 [IIO_POWER] = "power",
52 [IIO_IN_DIFF] = "in-in",
53 [IIO_GYRO] = "gyro",
54 [IIO_TEMP] = "temp",
55 [IIO_MAGN] = "magn",
56 [IIO_INCLI] = "incli",
57 [IIO_ROT] = "rot",
58 [IIO_INTENSITY] = "intensity",
59 [IIO_LIGHT] = "illuminance",
60 [IIO_ANGL] = "angl",
63 static const char * const iio_chan_type_name_spec_complex[] = {
64 [IIO_IN_DIFF] = "in%d-in%d",
67 static const char * const iio_modifier_names_light[] = {
68 [IIO_MOD_LIGHT_BOTH] = "both",
69 [IIO_MOD_LIGHT_IR] = "ir",
72 static const char * const iio_modifier_names_axial[] = {
73 [IIO_MOD_X] = "x",
74 [IIO_MOD_Y] = "y",
75 [IIO_MOD_Z] = "z",
78 /* relies on pairs of these shared then separate */
79 static 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",
84 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
85 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
88 int iio_push_event(struct iio_dev *dev_info,
89 int ev_line,
90 int ev_code,
91 s64 timestamp)
93 struct iio_event_interface *ev_int
94 = &dev_info->event_interfaces[ev_line];
95 struct iio_detected_event_list *ev;
96 int ret = 0;
98 /* Does anyone care? */
99 mutex_lock(&ev_int->event_list_lock);
100 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
101 if (ev_int->current_events == ev_int->max_events) {
102 mutex_unlock(&ev_int->event_list_lock);
103 return 0;
105 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
106 if (ev == NULL) {
107 ret = -ENOMEM;
108 mutex_unlock(&ev_int->event_list_lock);
109 goto error_ret;
111 ev->ev.id = ev_code;
112 ev->ev.timestamp = timestamp;
114 list_add_tail(&ev->list, &ev_int->det_events);
115 ev_int->current_events++;
116 mutex_unlock(&ev_int->event_list_lock);
117 wake_up_interruptible(&ev_int->wait);
118 } else
119 mutex_unlock(&ev_int->event_list_lock);
121 error_ret:
122 return ret;
124 EXPORT_SYMBOL(iio_push_event);
127 /* This turns up an awful lot */
128 ssize_t iio_read_const_attr(struct device *dev,
129 struct device_attribute *attr,
130 char *buf)
132 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
134 EXPORT_SYMBOL(iio_read_const_attr);
137 static ssize_t iio_event_chrdev_read(struct file *filep,
138 char __user *buf,
139 size_t count,
140 loff_t *f_ps)
142 struct iio_event_interface *ev_int = filep->private_data;
143 struct iio_detected_event_list *el;
144 int ret;
145 size_t len;
147 mutex_lock(&ev_int->event_list_lock);
148 if (list_empty(&ev_int->det_events)) {
149 if (filep->f_flags & O_NONBLOCK) {
150 ret = -EAGAIN;
151 goto error_mutex_unlock;
153 mutex_unlock(&ev_int->event_list_lock);
154 /* Blocking on device; waiting for something to be there */
155 ret = wait_event_interruptible(ev_int->wait,
156 !list_empty(&ev_int
157 ->det_events));
158 if (ret)
159 goto error_ret;
160 /* Single access device so no one else can get the data */
161 mutex_lock(&ev_int->event_list_lock);
164 el = list_first_entry(&ev_int->det_events,
165 struct iio_detected_event_list,
166 list);
167 len = sizeof el->ev;
168 if (copy_to_user(buf, &(el->ev), len)) {
169 ret = -EFAULT;
170 goto error_mutex_unlock;
172 list_del(&el->list);
173 ev_int->current_events--;
174 mutex_unlock(&ev_int->event_list_lock);
175 kfree(el);
177 return len;
179 error_mutex_unlock:
180 mutex_unlock(&ev_int->event_list_lock);
181 error_ret:
183 return ret;
186 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
188 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
189 struct iio_event_interface *ev_int = hand->private;
190 struct iio_detected_event_list *el, *t;
192 mutex_lock(&ev_int->event_list_lock);
193 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
195 * In order to maintain a clean state for reopening,
196 * clear out any awaiting events. The mask will prevent
197 * any new __iio_push_event calls running.
199 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
200 list_del(&el->list);
201 kfree(el);
203 mutex_unlock(&ev_int->event_list_lock);
205 return 0;
208 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
210 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
211 struct iio_event_interface *ev_int = hand->private;
213 mutex_lock(&ev_int->event_list_lock);
214 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
215 fops_put(filep->f_op);
216 mutex_unlock(&ev_int->event_list_lock);
217 return -EBUSY;
219 filep->private_data = hand->private;
220 mutex_unlock(&ev_int->event_list_lock);
222 return 0;
225 static const struct file_operations iio_event_chrdev_fileops = {
226 .read = iio_event_chrdev_read,
227 .release = iio_event_chrdev_release,
228 .open = iio_event_chrdev_open,
229 .owner = THIS_MODULE,
230 .llseek = noop_llseek,
233 static void iio_event_dev_release(struct device *dev)
235 struct iio_event_interface *ev_int
236 = container_of(dev, struct iio_event_interface, dev);
237 cdev_del(&ev_int->handler.chrdev);
238 iio_device_free_chrdev_minor(MINOR(dev->devt));
241 static struct device_type iio_event_type = {
242 .release = iio_event_dev_release,
245 int iio_device_get_chrdev_minor(void)
247 int ret, val;
249 ida_again:
250 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
251 return -ENOMEM;
252 spin_lock(&iio_ida_lock);
253 ret = ida_get_new(&iio_chrdev_ida, &val);
254 spin_unlock(&iio_ida_lock);
255 if (unlikely(ret == -EAGAIN))
256 goto ida_again;
257 else if (unlikely(ret))
258 return ret;
259 if (val > IIO_DEV_MAX)
260 return -ENOMEM;
261 return val;
264 void iio_device_free_chrdev_minor(int val)
266 spin_lock(&iio_ida_lock);
267 ida_remove(&iio_chrdev_ida, val);
268 spin_unlock(&iio_ida_lock);
271 static int iio_setup_ev_int(struct iio_event_interface *ev_int,
272 const char *dev_name,
273 int index,
274 struct module *owner,
275 struct device *dev)
277 int ret, minor;
279 ev_int->dev.bus = &iio_bus_type;
280 ev_int->dev.parent = dev;
281 ev_int->dev.type = &iio_event_type;
282 device_initialize(&ev_int->dev);
284 minor = iio_device_get_chrdev_minor();
285 if (minor < 0) {
286 ret = minor;
287 goto error_device_put;
289 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
290 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
292 ret = device_add(&ev_int->dev);
293 if (ret)
294 goto error_free_minor;
296 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
297 ev_int->handler.chrdev.owner = owner;
299 mutex_init(&ev_int->event_list_lock);
300 /* discussion point - make this variable? */
301 ev_int->max_events = 10;
302 ev_int->current_events = 0;
303 INIT_LIST_HEAD(&ev_int->det_events);
304 init_waitqueue_head(&ev_int->wait);
305 ev_int->handler.private = ev_int;
306 ev_int->handler.flags = 0;
308 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
309 if (ret)
310 goto error_unreg_device;
312 return 0;
314 error_unreg_device:
315 device_unregister(&ev_int->dev);
316 error_free_minor:
317 iio_device_free_chrdev_minor(minor);
318 error_device_put:
319 put_device(&ev_int->dev);
321 return ret;
324 static void iio_free_ev_int(struct iio_event_interface *ev_int)
326 device_unregister(&ev_int->dev);
327 put_device(&ev_int->dev);
330 static int __init iio_dev_init(void)
332 int err;
334 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
335 if (err < 0)
336 printk(KERN_ERR "%s: failed to allocate char dev region\n",
337 __FILE__);
339 return err;
342 static void __exit iio_dev_exit(void)
344 if (iio_devt)
345 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
348 static int __init iio_init(void)
350 int ret;
352 /* Register sysfs bus */
353 ret = bus_register(&iio_bus_type);
354 if (ret < 0) {
355 printk(KERN_ERR
356 "%s could not register bus type\n",
357 __FILE__);
358 goto error_nothing;
361 ret = iio_dev_init();
362 if (ret < 0)
363 goto error_unregister_bus_type;
365 return 0;
367 error_unregister_bus_type:
368 bus_unregister(&iio_bus_type);
369 error_nothing:
370 return ret;
373 static void __exit iio_exit(void)
375 iio_dev_exit();
376 bus_unregister(&iio_bus_type);
379 static ssize_t iio_read_channel_info(struct device *dev,
380 struct device_attribute *attr,
381 char *buf)
383 struct iio_dev *indio_dev = dev_get_drvdata(dev);
384 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
385 int val, val2;
386 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
387 &val, &val2, this_attr->address);
389 if (ret < 0)
390 return ret;
392 if (ret == IIO_VAL_INT)
393 return sprintf(buf, "%d\n", val);
394 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
395 if (val2 < 0)
396 return sprintf(buf, "-%d.%06u\n", val, -val2);
397 else
398 return sprintf(buf, "%d.%06u\n", val, val2);
399 } else
400 return 0;
403 static ssize_t iio_write_channel_info(struct device *dev,
404 struct device_attribute *attr,
405 const char *buf,
406 size_t len)
408 struct iio_dev *indio_dev = dev_get_drvdata(dev);
409 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
410 int ret, integer = 0, micro = 0, micro_mult = 100000;
411 bool integer_part = true, negative = false;
413 /* Assumes decimal - precision based on number of digits */
414 if (!indio_dev->info->write_raw)
415 return -EINVAL;
416 if (buf[0] == '-') {
417 negative = true;
418 buf++;
420 while (*buf) {
421 if ('0' <= *buf && *buf <= '9') {
422 if (integer_part)
423 integer = integer*10 + *buf - '0';
424 else {
425 micro += micro_mult*(*buf - '0');
426 if (micro_mult == 1)
427 break;
428 micro_mult /= 10;
430 } else if (*buf == '\n') {
431 if (*(buf + 1) == '\0')
432 break;
433 else
434 return -EINVAL;
435 } else if (*buf == '.') {
436 integer_part = false;
437 } else {
438 return -EINVAL;
440 buf++;
442 if (negative) {
443 if (integer)
444 integer = -integer;
445 else
446 micro = -micro;
449 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
450 integer, micro, this_attr->address);
451 if (ret)
452 return ret;
454 return len;
457 static int __iio_build_postfix(struct iio_chan_spec const *chan,
458 bool generic,
459 const char *postfix,
460 char **result)
462 char *all_post;
463 /* 3 options - generic, extend_name, modified - if generic, extend_name
464 * and modified cannot apply.*/
466 if (generic || (!chan->modified && !chan->extend_name)) {
467 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
468 } else if (chan->modified) {
469 const char *intermediate;
470 switch (chan->type) {
471 case IIO_INTENSITY:
472 intermediate
473 = iio_modifier_names_light[chan->channel2];
474 break;
475 case IIO_ACCEL:
476 case IIO_GYRO:
477 case IIO_MAGN:
478 case IIO_INCLI:
479 case IIO_ROT:
480 case IIO_ANGL:
481 intermediate
482 = iio_modifier_names_axial[chan->channel2];
483 break;
484 default:
485 return -EINVAL;
487 if (chan->extend_name)
488 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
489 intermediate,
490 chan->extend_name,
491 postfix);
492 else
493 all_post = kasprintf(GFP_KERNEL, "%s_%s",
494 intermediate,
495 postfix);
496 } else
497 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
498 postfix);
499 if (all_post == NULL)
500 return -ENOMEM;
501 *result = all_post;
502 return 0;
505 int __iio_device_attr_init(struct device_attribute *dev_attr,
506 const char *postfix,
507 struct iio_chan_spec const *chan,
508 ssize_t (*readfunc)(struct device *dev,
509 struct device_attribute *attr,
510 char *buf),
511 ssize_t (*writefunc)(struct device *dev,
512 struct device_attribute *attr,
513 const char *buf,
514 size_t len),
515 bool generic)
517 int ret;
518 char *name_format, *full_postfix;
519 sysfs_attr_init(&dev_attr->attr);
520 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
521 if (ret)
522 goto error_ret;
524 /* Special case for types that uses both channel numbers in naming */
525 if (chan->type == IIO_IN_DIFF && !generic)
526 name_format
527 = kasprintf(GFP_KERNEL, "%s_%s",
528 iio_chan_type_name_spec_complex[chan->type],
529 full_postfix);
530 else if (generic || !chan->indexed)
531 name_format
532 = kasprintf(GFP_KERNEL, "%s_%s",
533 iio_chan_type_name_spec_shared[chan->type],
534 full_postfix);
535 else
536 name_format
537 = kasprintf(GFP_KERNEL, "%s%d_%s",
538 iio_chan_type_name_spec_shared[chan->type],
539 chan->channel,
540 full_postfix);
542 if (name_format == NULL) {
543 ret = -ENOMEM;
544 goto error_free_full_postfix;
546 dev_attr->attr.name = kasprintf(GFP_KERNEL,
547 name_format,
548 chan->channel,
549 chan->channel2);
550 if (dev_attr->attr.name == NULL) {
551 ret = -ENOMEM;
552 goto error_free_name_format;
555 if (readfunc) {
556 dev_attr->attr.mode |= S_IRUGO;
557 dev_attr->show = readfunc;
560 if (writefunc) {
561 dev_attr->attr.mode |= S_IWUSR;
562 dev_attr->store = writefunc;
564 kfree(name_format);
565 kfree(full_postfix);
567 return 0;
569 error_free_name_format:
570 kfree(name_format);
571 error_free_full_postfix:
572 kfree(full_postfix);
573 error_ret:
574 return ret;
577 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
579 kfree(dev_attr->attr.name);
582 int __iio_add_chan_devattr(const char *postfix,
583 const char *group,
584 struct iio_chan_spec const *chan,
585 ssize_t (*readfunc)(struct device *dev,
586 struct device_attribute *attr,
587 char *buf),
588 ssize_t (*writefunc)(struct device *dev,
589 struct device_attribute *attr,
590 const char *buf,
591 size_t len),
592 int mask,
593 bool generic,
594 struct device *dev,
595 struct list_head *attr_list)
597 int ret;
598 struct iio_dev_attr *iio_attr, *t;
600 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
601 if (iio_attr == NULL) {
602 ret = -ENOMEM;
603 goto error_ret;
605 ret = __iio_device_attr_init(&iio_attr->dev_attr,
606 postfix, chan,
607 readfunc, writefunc, generic);
608 if (ret)
609 goto error_iio_dev_attr_free;
610 iio_attr->c = chan;
611 iio_attr->address = mask;
612 list_for_each_entry(t, attr_list, l)
613 if (strcmp(t->dev_attr.attr.name,
614 iio_attr->dev_attr.attr.name) == 0) {
615 if (!generic)
616 dev_err(dev, "tried to double register : %s\n",
617 t->dev_attr.attr.name);
618 ret = -EBUSY;
619 goto error_device_attr_deinit;
622 ret = sysfs_add_file_to_group(&dev->kobj,
623 &iio_attr->dev_attr.attr, group);
624 if (ret < 0)
625 goto error_device_attr_deinit;
627 list_add(&iio_attr->l, attr_list);
629 return 0;
631 error_device_attr_deinit:
632 __iio_device_attr_deinit(&iio_attr->dev_attr);
633 error_iio_dev_attr_free:
634 kfree(iio_attr);
635 error_ret:
636 return ret;
639 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
640 struct iio_chan_spec const *chan)
642 int ret, i;
645 if (chan->channel < 0)
646 return 0;
647 if (chan->processed_val)
648 ret = __iio_add_chan_devattr("input", NULL, chan,
649 &iio_read_channel_info,
650 NULL,
653 &dev_info->dev,
654 &dev_info->channel_attr_list);
655 else
656 ret = __iio_add_chan_devattr("raw", NULL, chan,
657 &iio_read_channel_info,
658 NULL,
661 &dev_info->dev,
662 &dev_info->channel_attr_list);
663 if (ret)
664 goto error_ret;
666 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
667 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
668 NULL, chan,
669 &iio_read_channel_info,
670 &iio_write_channel_info,
671 (1 << i),
672 !(i%2),
673 &dev_info->dev,
674 &dev_info->channel_attr_list);
675 if (ret == -EBUSY && (i%2 == 0)) {
676 ret = 0;
677 continue;
679 if (ret < 0)
680 goto error_ret;
682 error_ret:
683 return ret;
686 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
687 struct iio_dev_attr *p)
689 sysfs_remove_file_from_group(&dev_info->dev.kobj,
690 &p->dev_attr.attr, NULL);
691 kfree(p->dev_attr.attr.name);
692 kfree(p);
695 static ssize_t iio_show_dev_name(struct device *dev,
696 struct device_attribute *attr,
697 char *buf)
699 struct iio_dev *indio_dev = dev_get_drvdata(dev);
700 return sprintf(buf, "%s\n", indio_dev->name);
703 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
705 static int iio_device_register_sysfs(struct iio_dev *dev_info)
707 int i, ret = 0;
708 struct iio_dev_attr *p, *n;
710 if (dev_info->info->attrs) {
711 ret = sysfs_create_group(&dev_info->dev.kobj,
712 dev_info->info->attrs);
713 if (ret) {
714 dev_err(dev_info->dev.parent,
715 "Failed to register sysfs hooks\n");
716 goto error_ret;
721 * New channel registration method - relies on the fact a group does
722 * not need to be initialized if it is name is NULL.
724 INIT_LIST_HEAD(&dev_info->channel_attr_list);
725 if (dev_info->channels)
726 for (i = 0; i < dev_info->num_channels; i++) {
727 ret = iio_device_add_channel_sysfs(dev_info,
728 &dev_info
729 ->channels[i]);
730 if (ret < 0)
731 goto error_clear_attrs;
733 if (dev_info->name) {
734 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
735 &dev_attr_name.attr,
736 NULL);
737 if (ret)
738 goto error_clear_attrs;
740 return 0;
742 error_clear_attrs:
743 list_for_each_entry_safe(p, n,
744 &dev_info->channel_attr_list, l) {
745 list_del(&p->l);
746 iio_device_remove_and_free_read_attr(dev_info, p);
748 if (dev_info->info->attrs)
749 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
750 error_ret:
751 return ret;
755 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
758 struct iio_dev_attr *p, *n;
759 if (dev_info->name)
760 sysfs_remove_file_from_group(&dev_info->dev.kobj,
761 &dev_attr_name.attr,
762 NULL);
763 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
764 list_del(&p->l);
765 iio_device_remove_and_free_read_attr(dev_info, p);
768 if (dev_info->info->attrs)
769 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
772 /* Return a negative errno on failure */
773 int iio_get_new_ida_val(struct ida *this_ida)
775 int ret;
776 int val;
778 ida_again:
779 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
780 return -ENOMEM;
782 spin_lock(&iio_ida_lock);
783 ret = ida_get_new(this_ida, &val);
784 spin_unlock(&iio_ida_lock);
785 if (unlikely(ret == -EAGAIN))
786 goto ida_again;
787 else if (unlikely(ret))
788 return ret;
790 return val;
792 EXPORT_SYMBOL(iio_get_new_ida_val);
794 void iio_free_ida_val(struct ida *this_ida, int id)
796 spin_lock(&iio_ida_lock);
797 ida_remove(this_ida, id);
798 spin_unlock(&iio_ida_lock);
800 EXPORT_SYMBOL(iio_free_ida_val);
802 static const char * const iio_ev_type_text[] = {
803 [IIO_EV_TYPE_THRESH] = "thresh",
804 [IIO_EV_TYPE_MAG] = "mag",
805 [IIO_EV_TYPE_ROC] = "roc"
808 static const char * const iio_ev_dir_text[] = {
809 [IIO_EV_DIR_EITHER] = "either",
810 [IIO_EV_DIR_RISING] = "rising",
811 [IIO_EV_DIR_FALLING] = "falling"
814 static ssize_t iio_ev_state_store(struct device *dev,
815 struct device_attribute *attr,
816 const char *buf,
817 size_t len)
819 struct iio_dev *indio_dev = dev_get_drvdata(dev);
820 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
821 int ret;
822 bool val;
824 ret = strtobool(buf, &val);
825 if (ret < 0)
826 return ret;
828 ret = indio_dev->info->write_event_config(indio_dev,
829 this_attr->address,
830 val);
831 return (ret < 0) ? ret : len;
834 static ssize_t iio_ev_state_show(struct device *dev,
835 struct device_attribute *attr,
836 char *buf)
838 struct iio_dev *indio_dev = dev_get_drvdata(dev);
839 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
840 int val = indio_dev->info->read_event_config(indio_dev,
841 this_attr->address);
843 if (val < 0)
844 return val;
845 else
846 return sprintf(buf, "%d\n", val);
849 static ssize_t iio_ev_value_show(struct device *dev,
850 struct device_attribute *attr,
851 char *buf)
853 struct iio_dev *indio_dev = dev_get_drvdata(dev);
854 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
855 int val, ret;
857 ret = indio_dev->info->read_event_value(indio_dev,
858 this_attr->address, &val);
859 if (ret < 0)
860 return ret;
862 return sprintf(buf, "%d\n", val);
865 static ssize_t iio_ev_value_store(struct device *dev,
866 struct device_attribute *attr,
867 const char *buf,
868 size_t len)
870 struct iio_dev *indio_dev = dev_get_drvdata(dev);
871 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
872 unsigned long val;
873 int ret;
875 ret = strict_strtoul(buf, 10, &val);
876 if (ret)
877 return ret;
879 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
880 val);
881 if (ret < 0)
882 return ret;
884 return len;
887 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
888 struct iio_chan_spec const *chan)
891 int ret = 0, i, mask;
892 char *postfix;
893 if (!chan->event_mask)
894 return 0;
896 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
897 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
898 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
899 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
900 if (postfix == NULL) {
901 ret = -ENOMEM;
902 goto error_ret;
904 switch (chan->type) {
905 /* Switch this to a table at some point */
906 case IIO_IN:
907 mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
908 i/IIO_EV_TYPE_MAX,
909 i%IIO_EV_TYPE_MAX);
910 break;
911 case IIO_ACCEL:
912 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
913 i/IIO_EV_TYPE_MAX,
914 i%IIO_EV_TYPE_MAX);
915 break;
916 case IIO_IN_DIFF:
917 mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
918 chan->channel2,
919 i/IIO_EV_TYPE_MAX,
920 i%IIO_EV_TYPE_MAX);
921 break;
922 default:
923 printk(KERN_INFO "currently unhandled type of event\n");
925 ret = __iio_add_chan_devattr(postfix,
926 NULL,
927 chan,
928 &iio_ev_state_show,
929 iio_ev_state_store,
930 mask,
931 /*HACK. - limits us to one
932 event interface - fix by
933 extending the bitmask - but
934 how far*/
936 &dev_info->event_interfaces[0].dev,
937 &dev_info->event_interfaces[0].
938 dev_attr_list);
939 kfree(postfix);
940 if (ret)
941 goto error_ret;
943 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
944 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
945 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
946 if (postfix == NULL) {
947 ret = -ENOMEM;
948 goto error_ret;
950 ret = __iio_add_chan_devattr(postfix, NULL, chan,
951 iio_ev_value_show,
952 iio_ev_value_store,
953 mask,
955 &dev_info->event_interfaces[0]
956 .dev,
957 &dev_info->event_interfaces[0]
958 .dev_attr_list);
959 kfree(postfix);
960 if (ret)
961 goto error_ret;
965 error_ret:
966 return ret;
969 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
970 const char *groupname,
971 int num)
973 struct iio_dev_attr *p, *n;
974 list_for_each_entry_safe(p, n,
975 &dev_info->event_interfaces[num].
976 dev_attr_list, l) {
977 sysfs_remove_file_from_group(&dev_info
978 ->event_interfaces[num].dev.kobj,
979 &p->dev_attr.attr,
980 groupname);
981 kfree(p->dev_attr.attr.name);
982 kfree(p);
986 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
988 int j;
989 int ret;
990 INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
991 /* Dynically created from the channels array */
992 if (dev_info->channels) {
993 for (j = 0; j < dev_info->num_channels; j++) {
994 ret = iio_device_add_event_sysfs(dev_info,
995 &dev_info
996 ->channels[j]);
997 if (ret)
998 goto error_clear_attrs;
1001 return 0;
1003 error_clear_attrs:
1004 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1006 return ret;
1009 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1010 int i)
1012 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1013 return 0;
1016 static int iio_device_register_eventset(struct iio_dev *dev_info)
1018 int ret = 0, i, j;
1020 if (dev_info->info->num_interrupt_lines == 0)
1021 return 0;
1023 dev_info->event_interfaces =
1024 kzalloc(sizeof(struct iio_event_interface)
1025 *dev_info->info->num_interrupt_lines,
1026 GFP_KERNEL);
1027 if (dev_info->event_interfaces == NULL) {
1028 ret = -ENOMEM;
1029 goto error_ret;
1032 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1033 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1034 dev_name(&dev_info->dev),
1036 dev_info->info->driver_module,
1037 &dev_info->dev);
1038 if (ret) {
1039 dev_err(&dev_info->dev,
1040 "Could not get chrdev interface\n");
1041 goto error_free_setup_ev_ints;
1044 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1045 (void *)dev_info);
1047 if (dev_info->info->event_attrs != NULL)
1048 ret = sysfs_create_group(&dev_info
1049 ->event_interfaces[i]
1050 .dev.kobj,
1051 &dev_info->info
1052 ->event_attrs[i]);
1054 if (ret) {
1055 dev_err(&dev_info->dev,
1056 "Failed to register sysfs for event attrs");
1057 goto error_remove_sysfs_interfaces;
1061 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1062 ret = __iio_add_event_config_attrs(dev_info, i);
1063 if (ret)
1064 goto error_unregister_config_attrs;
1067 return 0;
1069 error_unregister_config_attrs:
1070 for (j = 0; j < i; j++)
1071 __iio_remove_event_config_attrs(dev_info, i);
1072 i = dev_info->info->num_interrupt_lines - 1;
1073 error_remove_sysfs_interfaces:
1074 for (j = 0; j < i; j++)
1075 if (dev_info->info->event_attrs != NULL)
1076 sysfs_remove_group(&dev_info
1077 ->event_interfaces[j].dev.kobj,
1078 &dev_info->info->event_attrs[j]);
1079 error_free_setup_ev_ints:
1080 for (j = 0; j < i; j++)
1081 iio_free_ev_int(&dev_info->event_interfaces[j]);
1082 kfree(dev_info->event_interfaces);
1083 error_ret:
1085 return ret;
1088 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1090 int i;
1092 if (dev_info->info->num_interrupt_lines == 0)
1093 return;
1094 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1095 __iio_remove_event_config_attrs(dev_info, i);
1096 if (dev_info->info->event_attrs != NULL)
1097 sysfs_remove_group(&dev_info
1098 ->event_interfaces[i].dev.kobj,
1099 &dev_info->info->event_attrs[i]);
1102 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
1103 iio_free_ev_int(&dev_info->event_interfaces[i]);
1104 kfree(dev_info->event_interfaces);
1107 static void iio_dev_release(struct device *device)
1109 iio_put();
1110 kfree(to_iio_dev(device));
1113 static struct device_type iio_dev_type = {
1114 .name = "iio_device",
1115 .release = iio_dev_release,
1118 struct iio_dev *iio_allocate_device(int sizeof_priv)
1120 struct iio_dev *dev;
1121 size_t alloc_size;
1123 alloc_size = sizeof(struct iio_dev);
1124 if (sizeof_priv) {
1125 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1126 alloc_size += sizeof_priv;
1128 /* ensure 32-byte alignment of whole construct ? */
1129 alloc_size += IIO_ALIGN - 1;
1131 dev = kzalloc(alloc_size, GFP_KERNEL);
1133 if (dev) {
1134 dev->dev.type = &iio_dev_type;
1135 dev->dev.bus = &iio_bus_type;
1136 device_initialize(&dev->dev);
1137 dev_set_drvdata(&dev->dev, (void *)dev);
1138 mutex_init(&dev->mlock);
1139 iio_get();
1142 return dev;
1144 EXPORT_SYMBOL(iio_allocate_device);
1146 void iio_free_device(struct iio_dev *dev)
1148 if (dev)
1149 iio_put_device(dev);
1151 EXPORT_SYMBOL(iio_free_device);
1153 int iio_device_register(struct iio_dev *dev_info)
1155 int ret;
1157 dev_info->id = iio_get_new_ida_val(&iio_ida);
1158 if (dev_info->id < 0) {
1159 ret = dev_info->id;
1160 dev_err(&dev_info->dev, "Failed to get id\n");
1161 goto error_ret;
1163 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1165 ret = device_add(&dev_info->dev);
1166 if (ret)
1167 goto error_free_ida;
1168 ret = iio_device_register_sysfs(dev_info);
1169 if (ret) {
1170 dev_err(dev_info->dev.parent,
1171 "Failed to register sysfs interfaces\n");
1172 goto error_del_device;
1174 ret = iio_device_register_eventset(dev_info);
1175 if (ret) {
1176 dev_err(dev_info->dev.parent,
1177 "Failed to register event set\n");
1178 goto error_free_sysfs;
1180 if (dev_info->modes & INDIO_RING_TRIGGERED)
1181 iio_device_register_trigger_consumer(dev_info);
1183 return 0;
1185 error_free_sysfs:
1186 iio_device_unregister_sysfs(dev_info);
1187 error_del_device:
1188 device_del(&dev_info->dev);
1189 error_free_ida:
1190 iio_free_ida_val(&iio_ida, dev_info->id);
1191 error_ret:
1192 return ret;
1194 EXPORT_SYMBOL(iio_device_register);
1196 void iio_device_unregister(struct iio_dev *dev_info)
1198 if (dev_info->modes & INDIO_RING_TRIGGERED)
1199 iio_device_unregister_trigger_consumer(dev_info);
1200 iio_device_unregister_eventset(dev_info);
1201 iio_device_unregister_sysfs(dev_info);
1202 iio_free_ida_val(&iio_ida, dev_info->id);
1203 device_unregister(&dev_info->dev);
1205 EXPORT_SYMBOL(iio_device_unregister);
1207 void iio_put(void)
1209 module_put(THIS_MODULE);
1212 void iio_get(void)
1214 __module_get(THIS_MODULE);
1217 subsys_initcall(iio_init);
1218 module_exit(iio_exit);
1220 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1221 MODULE_DESCRIPTION("Industrial I/O core");
1222 MODULE_LICENSE("GPL");