iio: industrialio-core: Add IIO_OUT type
[deliverable/linux.git] / drivers / staging / iio / industrialio-core.c
1 /* 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>
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"
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*/
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);
36
37 dev_t iio_devt;
38 EXPORT_SYMBOL(iio_devt);
39
40 #define IIO_DEV_MAX 256
41 struct bus_type iio_bus_type = {
42 .name = "iio",
43 };
44 EXPORT_SYMBOL(iio_bus_type);
45
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_OUT] = "out",
51 [IIO_CURRENT] = "current",
52 [IIO_POWER] = "power",
53 [IIO_IN_DIFF] = "in-in",
54 [IIO_GYRO] = "gyro",
55 [IIO_TEMP] = "temp",
56 [IIO_MAGN] = "magn",
57 [IIO_INCLI] = "incli",
58 [IIO_ROT] = "rot",
59 [IIO_INTENSITY] = "intensity",
60 [IIO_LIGHT] = "illuminance",
61 [IIO_ANGL] = "angl",
62 };
63
64 static const char * const iio_chan_type_name_spec_complex[] = {
65 [IIO_IN_DIFF] = "in%d-in%d",
66 };
67
68 static const char * const iio_modifier_names_light[] = {
69 [IIO_MOD_LIGHT_BOTH] = "both",
70 [IIO_MOD_LIGHT_IR] = "ir",
71 };
72
73 static const char * const iio_modifier_names_axial[] = {
74 [IIO_MOD_X] = "x",
75 [IIO_MOD_Y] = "y",
76 [IIO_MOD_Z] = "z",
77 };
78
79 /* relies on pairs of these shared then separate */
80 static const char * const iio_chan_info_postfix[] = {
81 [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale",
82 [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset",
83 [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale",
84 [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias",
85 [IIO_CHAN_INFO_PEAK_SHARED/2] = "peak_raw",
86 [IIO_CHAN_INFO_PEAK_SCALE_SHARED/2] = "peak_scale",
87 };
88
89 int iio_push_event(struct iio_dev *dev_info,
90 int ev_line,
91 int ev_code,
92 s64 timestamp)
93 {
94 struct iio_event_interface *ev_int
95 = &dev_info->event_interfaces[ev_line];
96 struct iio_detected_event_list *ev;
97 int ret = 0;
98
99 /* Does anyone care? */
100 mutex_lock(&ev_int->event_list_lock);
101 if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) {
102 if (ev_int->current_events == ev_int->max_events) {
103 mutex_unlock(&ev_int->event_list_lock);
104 return 0;
105 }
106 ev = kmalloc(sizeof(*ev), GFP_KERNEL);
107 if (ev == NULL) {
108 ret = -ENOMEM;
109 mutex_unlock(&ev_int->event_list_lock);
110 goto error_ret;
111 }
112 ev->ev.id = ev_code;
113 ev->ev.timestamp = timestamp;
114
115 list_add_tail(&ev->list, &ev_int->det_events);
116 ev_int->current_events++;
117 mutex_unlock(&ev_int->event_list_lock);
118 wake_up_interruptible(&ev_int->wait);
119 } else
120 mutex_unlock(&ev_int->event_list_lock);
121
122 error_ret:
123 return ret;
124 }
125 EXPORT_SYMBOL(iio_push_event);
126
127
128 /* This turns up an awful lot */
129 ssize_t iio_read_const_attr(struct device *dev,
130 struct device_attribute *attr,
131 char *buf)
132 {
133 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
134 }
135 EXPORT_SYMBOL(iio_read_const_attr);
136
137
138 static ssize_t iio_event_chrdev_read(struct file *filep,
139 char __user *buf,
140 size_t count,
141 loff_t *f_ps)
142 {
143 struct iio_event_interface *ev_int = filep->private_data;
144 struct iio_detected_event_list *el;
145 int ret;
146 size_t len;
147
148 mutex_lock(&ev_int->event_list_lock);
149 if (list_empty(&ev_int->det_events)) {
150 if (filep->f_flags & O_NONBLOCK) {
151 ret = -EAGAIN;
152 goto error_mutex_unlock;
153 }
154 mutex_unlock(&ev_int->event_list_lock);
155 /* Blocking on device; waiting for something to be there */
156 ret = wait_event_interruptible(ev_int->wait,
157 !list_empty(&ev_int
158 ->det_events));
159 if (ret)
160 goto error_ret;
161 /* Single access device so no one else can get the data */
162 mutex_lock(&ev_int->event_list_lock);
163 }
164
165 el = list_first_entry(&ev_int->det_events,
166 struct iio_detected_event_list,
167 list);
168 len = sizeof el->ev;
169 if (copy_to_user(buf, &(el->ev), len)) {
170 ret = -EFAULT;
171 goto error_mutex_unlock;
172 }
173 list_del(&el->list);
174 ev_int->current_events--;
175 mutex_unlock(&ev_int->event_list_lock);
176 kfree(el);
177
178 return len;
179
180 error_mutex_unlock:
181 mutex_unlock(&ev_int->event_list_lock);
182 error_ret:
183
184 return ret;
185 }
186
187 static int iio_event_chrdev_release(struct inode *inode, struct file *filep)
188 {
189 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
190 struct iio_event_interface *ev_int = hand->private;
191 struct iio_detected_event_list *el, *t;
192
193 mutex_lock(&ev_int->event_list_lock);
194 clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags);
195 /*
196 * In order to maintain a clean state for reopening,
197 * clear out any awaiting events. The mask will prevent
198 * any new __iio_push_event calls running.
199 */
200 list_for_each_entry_safe(el, t, &ev_int->det_events, list) {
201 list_del(&el->list);
202 kfree(el);
203 }
204 mutex_unlock(&ev_int->event_list_lock);
205
206 return 0;
207 }
208
209 static int iio_event_chrdev_open(struct inode *inode, struct file *filep)
210 {
211 struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev);
212 struct iio_event_interface *ev_int = hand->private;
213
214 mutex_lock(&ev_int->event_list_lock);
215 if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) {
216 fops_put(filep->f_op);
217 mutex_unlock(&ev_int->event_list_lock);
218 return -EBUSY;
219 }
220 filep->private_data = hand->private;
221 mutex_unlock(&ev_int->event_list_lock);
222
223 return 0;
224 }
225
226 static const struct file_operations iio_event_chrdev_fileops = {
227 .read = iio_event_chrdev_read,
228 .release = iio_event_chrdev_release,
229 .open = iio_event_chrdev_open,
230 .owner = THIS_MODULE,
231 .llseek = noop_llseek,
232 };
233
234 static void iio_event_dev_release(struct device *dev)
235 {
236 struct iio_event_interface *ev_int
237 = container_of(dev, struct iio_event_interface, dev);
238 cdev_del(&ev_int->handler.chrdev);
239 iio_device_free_chrdev_minor(MINOR(dev->devt));
240 };
241
242 static struct device_type iio_event_type = {
243 .release = iio_event_dev_release,
244 };
245
246 int iio_device_get_chrdev_minor(void)
247 {
248 int ret, val;
249
250 ida_again:
251 if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0))
252 return -ENOMEM;
253 spin_lock(&iio_ida_lock);
254 ret = ida_get_new(&iio_chrdev_ida, &val);
255 spin_unlock(&iio_ida_lock);
256 if (unlikely(ret == -EAGAIN))
257 goto ida_again;
258 else if (unlikely(ret))
259 return ret;
260 if (val > IIO_DEV_MAX)
261 return -ENOMEM;
262 return val;
263 }
264
265 void iio_device_free_chrdev_minor(int val)
266 {
267 spin_lock(&iio_ida_lock);
268 ida_remove(&iio_chrdev_ida, val);
269 spin_unlock(&iio_ida_lock);
270 }
271
272 static int iio_setup_ev_int(struct iio_event_interface *ev_int,
273 const char *dev_name,
274 int index,
275 struct module *owner,
276 struct device *dev)
277 {
278 int ret, minor;
279
280 ev_int->dev.bus = &iio_bus_type;
281 ev_int->dev.parent = dev;
282 ev_int->dev.type = &iio_event_type;
283 device_initialize(&ev_int->dev);
284
285 minor = iio_device_get_chrdev_minor();
286 if (minor < 0) {
287 ret = minor;
288 goto error_device_put;
289 }
290 ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor);
291 dev_set_name(&ev_int->dev, "%s:event%d", dev_name, index);
292
293 ret = device_add(&ev_int->dev);
294 if (ret)
295 goto error_free_minor;
296
297 cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops);
298 ev_int->handler.chrdev.owner = owner;
299
300 mutex_init(&ev_int->event_list_lock);
301 /* discussion point - make this variable? */
302 ev_int->max_events = 10;
303 ev_int->current_events = 0;
304 INIT_LIST_HEAD(&ev_int->det_events);
305 init_waitqueue_head(&ev_int->wait);
306 ev_int->handler.private = ev_int;
307 ev_int->handler.flags = 0;
308
309 ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1);
310 if (ret)
311 goto error_unreg_device;
312
313 return 0;
314
315 error_unreg_device:
316 device_unregister(&ev_int->dev);
317 error_free_minor:
318 iio_device_free_chrdev_minor(minor);
319 error_device_put:
320 put_device(&ev_int->dev);
321
322 return ret;
323 }
324
325 static void iio_free_ev_int(struct iio_event_interface *ev_int)
326 {
327 device_unregister(&ev_int->dev);
328 put_device(&ev_int->dev);
329 }
330
331 static int __init iio_dev_init(void)
332 {
333 int err;
334
335 err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
336 if (err < 0)
337 printk(KERN_ERR "%s: failed to allocate char dev region\n",
338 __FILE__);
339
340 return err;
341 }
342
343 static void __exit iio_dev_exit(void)
344 {
345 if (iio_devt)
346 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
347 }
348
349 static int __init iio_init(void)
350 {
351 int ret;
352
353 /* Register sysfs bus */
354 ret = bus_register(&iio_bus_type);
355 if (ret < 0) {
356 printk(KERN_ERR
357 "%s could not register bus type\n",
358 __FILE__);
359 goto error_nothing;
360 }
361
362 ret = iio_dev_init();
363 if (ret < 0)
364 goto error_unregister_bus_type;
365
366 return 0;
367
368 error_unregister_bus_type:
369 bus_unregister(&iio_bus_type);
370 error_nothing:
371 return ret;
372 }
373
374 static void __exit iio_exit(void)
375 {
376 iio_dev_exit();
377 bus_unregister(&iio_bus_type);
378 }
379
380 static ssize_t iio_read_channel_info(struct device *dev,
381 struct device_attribute *attr,
382 char *buf)
383 {
384 struct iio_dev *indio_dev = dev_get_drvdata(dev);
385 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
386 int val, val2;
387 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
388 &val, &val2, this_attr->address);
389
390 if (ret < 0)
391 return ret;
392
393 if (ret == IIO_VAL_INT)
394 return sprintf(buf, "%d\n", val);
395 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
396 if (val2 < 0)
397 return sprintf(buf, "-%d.%06u\n", val, -val2);
398 else
399 return sprintf(buf, "%d.%06u\n", val, val2);
400 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
401 if (val2 < 0)
402 return sprintf(buf, "-%d.%09u\n", val, -val2);
403 else
404 return sprintf(buf, "%d.%09u\n", val, val2);
405 } else
406 return 0;
407 }
408
409 static ssize_t iio_write_channel_info(struct device *dev,
410 struct device_attribute *attr,
411 const char *buf,
412 size_t len)
413 {
414 struct iio_dev *indio_dev = dev_get_drvdata(dev);
415 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
416 int ret, integer = 0, fract = 0, fract_mult = 100000;
417 bool integer_part = true, negative = false;
418
419 /* Assumes decimal - precision based on number of digits */
420 if (!indio_dev->info->write_raw)
421 return -EINVAL;
422
423 if (indio_dev->info->write_raw_get_fmt)
424 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
425 this_attr->c, this_attr->address)) {
426 case IIO_VAL_INT_PLUS_MICRO:
427 fract_mult = 100000;
428 break;
429 case IIO_VAL_INT_PLUS_NANO:
430 fract_mult = 100000000;
431 break;
432 default:
433 return -EINVAL;
434 }
435
436 if (buf[0] == '-') {
437 negative = true;
438 buf++;
439 }
440
441 while (*buf) {
442 if ('0' <= *buf && *buf <= '9') {
443 if (integer_part)
444 integer = integer*10 + *buf - '0';
445 else {
446 fract += fract_mult*(*buf - '0');
447 if (fract_mult == 1)
448 break;
449 fract_mult /= 10;
450 }
451 } else if (*buf == '\n') {
452 if (*(buf + 1) == '\0')
453 break;
454 else
455 return -EINVAL;
456 } else if (*buf == '.') {
457 integer_part = false;
458 } else {
459 return -EINVAL;
460 }
461 buf++;
462 }
463 if (negative) {
464 if (integer)
465 integer = -integer;
466 else
467 fract = -fract;
468 }
469
470 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
471 integer, fract, this_attr->address);
472 if (ret)
473 return ret;
474
475 return len;
476 }
477
478 static int __iio_build_postfix(struct iio_chan_spec const *chan,
479 bool generic,
480 const char *postfix,
481 char **result)
482 {
483 char *all_post;
484 /* 3 options - generic, extend_name, modified - if generic, extend_name
485 * and modified cannot apply.*/
486
487 if (generic || (!chan->modified && !chan->extend_name)) {
488 all_post = kasprintf(GFP_KERNEL, "%s", postfix);
489 } else if (chan->modified) {
490 const char *intermediate;
491 switch (chan->type) {
492 case IIO_INTENSITY:
493 intermediate
494 = iio_modifier_names_light[chan->channel2];
495 break;
496 case IIO_ACCEL:
497 case IIO_GYRO:
498 case IIO_MAGN:
499 case IIO_INCLI:
500 case IIO_ROT:
501 case IIO_ANGL:
502 intermediate
503 = iio_modifier_names_axial[chan->channel2];
504 break;
505 default:
506 return -EINVAL;
507 }
508 if (chan->extend_name)
509 all_post = kasprintf(GFP_KERNEL, "%s_%s_%s",
510 intermediate,
511 chan->extend_name,
512 postfix);
513 else
514 all_post = kasprintf(GFP_KERNEL, "%s_%s",
515 intermediate,
516 postfix);
517 } else
518 all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name,
519 postfix);
520 if (all_post == NULL)
521 return -ENOMEM;
522 *result = all_post;
523 return 0;
524 }
525
526 int __iio_device_attr_init(struct device_attribute *dev_attr,
527 const char *postfix,
528 struct iio_chan_spec const *chan,
529 ssize_t (*readfunc)(struct device *dev,
530 struct device_attribute *attr,
531 char *buf),
532 ssize_t (*writefunc)(struct device *dev,
533 struct device_attribute *attr,
534 const char *buf,
535 size_t len),
536 bool generic)
537 {
538 int ret;
539 char *name_format, *full_postfix;
540 sysfs_attr_init(&dev_attr->attr);
541 ret = __iio_build_postfix(chan, generic, postfix, &full_postfix);
542 if (ret)
543 goto error_ret;
544
545 /* Special case for types that uses both channel numbers in naming */
546 if (chan->type == IIO_IN_DIFF && !generic)
547 name_format
548 = kasprintf(GFP_KERNEL, "%s_%s",
549 iio_chan_type_name_spec_complex[chan->type],
550 full_postfix);
551 else if (generic || !chan->indexed)
552 name_format
553 = kasprintf(GFP_KERNEL, "%s_%s",
554 iio_chan_type_name_spec_shared[chan->type],
555 full_postfix);
556 else
557 name_format
558 = kasprintf(GFP_KERNEL, "%s%d_%s",
559 iio_chan_type_name_spec_shared[chan->type],
560 chan->channel,
561 full_postfix);
562
563 if (name_format == NULL) {
564 ret = -ENOMEM;
565 goto error_free_full_postfix;
566 }
567 dev_attr->attr.name = kasprintf(GFP_KERNEL,
568 name_format,
569 chan->channel,
570 chan->channel2);
571 if (dev_attr->attr.name == NULL) {
572 ret = -ENOMEM;
573 goto error_free_name_format;
574 }
575
576 if (readfunc) {
577 dev_attr->attr.mode |= S_IRUGO;
578 dev_attr->show = readfunc;
579 }
580
581 if (writefunc) {
582 dev_attr->attr.mode |= S_IWUSR;
583 dev_attr->store = writefunc;
584 }
585 kfree(name_format);
586 kfree(full_postfix);
587
588 return 0;
589
590 error_free_name_format:
591 kfree(name_format);
592 error_free_full_postfix:
593 kfree(full_postfix);
594 error_ret:
595 return ret;
596 }
597
598 void __iio_device_attr_deinit(struct device_attribute *dev_attr)
599 {
600 kfree(dev_attr->attr.name);
601 }
602
603 int __iio_add_chan_devattr(const char *postfix,
604 const char *group,
605 struct iio_chan_spec const *chan,
606 ssize_t (*readfunc)(struct device *dev,
607 struct device_attribute *attr,
608 char *buf),
609 ssize_t (*writefunc)(struct device *dev,
610 struct device_attribute *attr,
611 const char *buf,
612 size_t len),
613 int mask,
614 bool generic,
615 struct device *dev,
616 struct list_head *attr_list)
617 {
618 int ret;
619 struct iio_dev_attr *iio_attr, *t;
620
621 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
622 if (iio_attr == NULL) {
623 ret = -ENOMEM;
624 goto error_ret;
625 }
626 ret = __iio_device_attr_init(&iio_attr->dev_attr,
627 postfix, chan,
628 readfunc, writefunc, generic);
629 if (ret)
630 goto error_iio_dev_attr_free;
631 iio_attr->c = chan;
632 iio_attr->address = mask;
633 list_for_each_entry(t, attr_list, l)
634 if (strcmp(t->dev_attr.attr.name,
635 iio_attr->dev_attr.attr.name) == 0) {
636 if (!generic)
637 dev_err(dev, "tried to double register : %s\n",
638 t->dev_attr.attr.name);
639 ret = -EBUSY;
640 goto error_device_attr_deinit;
641 }
642
643 ret = sysfs_add_file_to_group(&dev->kobj,
644 &iio_attr->dev_attr.attr, group);
645 if (ret < 0)
646 goto error_device_attr_deinit;
647
648 list_add(&iio_attr->l, attr_list);
649
650 return 0;
651
652 error_device_attr_deinit:
653 __iio_device_attr_deinit(&iio_attr->dev_attr);
654 error_iio_dev_attr_free:
655 kfree(iio_attr);
656 error_ret:
657 return ret;
658 }
659
660 static int iio_device_add_channel_sysfs(struct iio_dev *dev_info,
661 struct iio_chan_spec const *chan)
662 {
663 int ret, i;
664
665
666 if (chan->channel < 0)
667 return 0;
668 if (chan->processed_val)
669 ret = __iio_add_chan_devattr("input", NULL, chan,
670 &iio_read_channel_info,
671 NULL,
672 0,
673 0,
674 &dev_info->dev,
675 &dev_info->channel_attr_list);
676 else
677 ret = __iio_add_chan_devattr("raw", NULL, chan,
678 &iio_read_channel_info,
679 (chan->type == IIO_OUT ?
680 &iio_write_channel_info : NULL),
681 0,
682 0,
683 &dev_info->dev,
684 &dev_info->channel_attr_list);
685 if (ret)
686 goto error_ret;
687
688 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
689 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
690 NULL, chan,
691 &iio_read_channel_info,
692 &iio_write_channel_info,
693 (1 << i),
694 !(i%2),
695 &dev_info->dev,
696 &dev_info->channel_attr_list);
697 if (ret == -EBUSY && (i%2 == 0)) {
698 ret = 0;
699 continue;
700 }
701 if (ret < 0)
702 goto error_ret;
703 }
704 error_ret:
705 return ret;
706 }
707
708 static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info,
709 struct iio_dev_attr *p)
710 {
711 sysfs_remove_file_from_group(&dev_info->dev.kobj,
712 &p->dev_attr.attr, NULL);
713 kfree(p->dev_attr.attr.name);
714 kfree(p);
715 }
716
717 static ssize_t iio_show_dev_name(struct device *dev,
718 struct device_attribute *attr,
719 char *buf)
720 {
721 struct iio_dev *indio_dev = dev_get_drvdata(dev);
722 return sprintf(buf, "%s\n", indio_dev->name);
723 }
724
725 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
726
727 static int iio_device_register_sysfs(struct iio_dev *dev_info)
728 {
729 int i, ret = 0;
730 struct iio_dev_attr *p, *n;
731
732 if (dev_info->info->attrs) {
733 ret = sysfs_create_group(&dev_info->dev.kobj,
734 dev_info->info->attrs);
735 if (ret) {
736 dev_err(dev_info->dev.parent,
737 "Failed to register sysfs hooks\n");
738 goto error_ret;
739 }
740 }
741
742 /*
743 * New channel registration method - relies on the fact a group does
744 * not need to be initialized if it is name is NULL.
745 */
746 INIT_LIST_HEAD(&dev_info->channel_attr_list);
747 if (dev_info->channels)
748 for (i = 0; i < dev_info->num_channels; i++) {
749 ret = iio_device_add_channel_sysfs(dev_info,
750 &dev_info
751 ->channels[i]);
752 if (ret < 0)
753 goto error_clear_attrs;
754 }
755 if (dev_info->name) {
756 ret = sysfs_add_file_to_group(&dev_info->dev.kobj,
757 &dev_attr_name.attr,
758 NULL);
759 if (ret)
760 goto error_clear_attrs;
761 }
762 return 0;
763
764 error_clear_attrs:
765 list_for_each_entry_safe(p, n,
766 &dev_info->channel_attr_list, l) {
767 list_del(&p->l);
768 iio_device_remove_and_free_read_attr(dev_info, p);
769 }
770 if (dev_info->info->attrs)
771 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
772 error_ret:
773 return ret;
774
775 }
776
777 static void iio_device_unregister_sysfs(struct iio_dev *dev_info)
778 {
779
780 struct iio_dev_attr *p, *n;
781 if (dev_info->name)
782 sysfs_remove_file_from_group(&dev_info->dev.kobj,
783 &dev_attr_name.attr,
784 NULL);
785 list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) {
786 list_del(&p->l);
787 iio_device_remove_and_free_read_attr(dev_info, p);
788 }
789
790 if (dev_info->info->attrs)
791 sysfs_remove_group(&dev_info->dev.kobj, dev_info->info->attrs);
792 }
793
794 /* Return a negative errno on failure */
795 int iio_get_new_ida_val(struct ida *this_ida)
796 {
797 int ret;
798 int val;
799
800 ida_again:
801 if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0))
802 return -ENOMEM;
803
804 spin_lock(&iio_ida_lock);
805 ret = ida_get_new(this_ida, &val);
806 spin_unlock(&iio_ida_lock);
807 if (unlikely(ret == -EAGAIN))
808 goto ida_again;
809 else if (unlikely(ret))
810 return ret;
811
812 return val;
813 }
814 EXPORT_SYMBOL(iio_get_new_ida_val);
815
816 void iio_free_ida_val(struct ida *this_ida, int id)
817 {
818 spin_lock(&iio_ida_lock);
819 ida_remove(this_ida, id);
820 spin_unlock(&iio_ida_lock);
821 }
822 EXPORT_SYMBOL(iio_free_ida_val);
823
824 static const char * const iio_ev_type_text[] = {
825 [IIO_EV_TYPE_THRESH] = "thresh",
826 [IIO_EV_TYPE_MAG] = "mag",
827 [IIO_EV_TYPE_ROC] = "roc"
828 };
829
830 static const char * const iio_ev_dir_text[] = {
831 [IIO_EV_DIR_EITHER] = "either",
832 [IIO_EV_DIR_RISING] = "rising",
833 [IIO_EV_DIR_FALLING] = "falling"
834 };
835
836 static ssize_t iio_ev_state_store(struct device *dev,
837 struct device_attribute *attr,
838 const char *buf,
839 size_t len)
840 {
841 struct iio_dev *indio_dev = dev_get_drvdata(dev);
842 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
843 int ret;
844 bool val;
845
846 ret = strtobool(buf, &val);
847 if (ret < 0)
848 return ret;
849
850 ret = indio_dev->info->write_event_config(indio_dev,
851 this_attr->address,
852 val);
853 return (ret < 0) ? ret : len;
854 }
855
856 static ssize_t iio_ev_state_show(struct device *dev,
857 struct device_attribute *attr,
858 char *buf)
859 {
860 struct iio_dev *indio_dev = dev_get_drvdata(dev);
861 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
862 int val = indio_dev->info->read_event_config(indio_dev,
863 this_attr->address);
864
865 if (val < 0)
866 return val;
867 else
868 return sprintf(buf, "%d\n", val);
869 }
870
871 static ssize_t iio_ev_value_show(struct device *dev,
872 struct device_attribute *attr,
873 char *buf)
874 {
875 struct iio_dev *indio_dev = dev_get_drvdata(dev);
876 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
877 int val, ret;
878
879 ret = indio_dev->info->read_event_value(indio_dev,
880 this_attr->address, &val);
881 if (ret < 0)
882 return ret;
883
884 return sprintf(buf, "%d\n", val);
885 }
886
887 static ssize_t iio_ev_value_store(struct device *dev,
888 struct device_attribute *attr,
889 const char *buf,
890 size_t len)
891 {
892 struct iio_dev *indio_dev = dev_get_drvdata(dev);
893 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
894 unsigned long val;
895 int ret;
896
897 ret = strict_strtoul(buf, 10, &val);
898 if (ret)
899 return ret;
900
901 ret = indio_dev->info->write_event_value(indio_dev, this_attr->address,
902 val);
903 if (ret < 0)
904 return ret;
905
906 return len;
907 }
908
909 static int iio_device_add_event_sysfs(struct iio_dev *dev_info,
910 struct iio_chan_spec const *chan)
911 {
912
913 int ret = 0, i, mask;
914 char *postfix;
915 if (!chan->event_mask)
916 return 0;
917
918 for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) {
919 postfix = kasprintf(GFP_KERNEL, "%s_%s_en",
920 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
921 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
922 if (postfix == NULL) {
923 ret = -ENOMEM;
924 goto error_ret;
925 }
926 switch (chan->type) {
927 /* Switch this to a table at some point */
928 case IIO_IN:
929 mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel,
930 i/IIO_EV_TYPE_MAX,
931 i%IIO_EV_TYPE_MAX);
932 break;
933 case IIO_ACCEL:
934 mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel,
935 i/IIO_EV_TYPE_MAX,
936 i%IIO_EV_TYPE_MAX);
937 break;
938 case IIO_IN_DIFF:
939 mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel,
940 chan->channel2,
941 i/IIO_EV_TYPE_MAX,
942 i%IIO_EV_TYPE_MAX);
943 break;
944 default:
945 printk(KERN_INFO "currently unhandled type of event\n");
946 }
947 ret = __iio_add_chan_devattr(postfix,
948 NULL,
949 chan,
950 &iio_ev_state_show,
951 iio_ev_state_store,
952 mask,
953 /*HACK. - limits us to one
954 event interface - fix by
955 extending the bitmask - but
956 how far*/
957 0,
958 &dev_info->event_interfaces[0].dev,
959 &dev_info->event_interfaces[0].
960 dev_attr_list);
961 kfree(postfix);
962 if (ret)
963 goto error_ret;
964
965 postfix = kasprintf(GFP_KERNEL, "%s_%s_value",
966 iio_ev_type_text[i/IIO_EV_TYPE_MAX],
967 iio_ev_dir_text[i%IIO_EV_TYPE_MAX]);
968 if (postfix == NULL) {
969 ret = -ENOMEM;
970 goto error_ret;
971 }
972 ret = __iio_add_chan_devattr(postfix, NULL, chan,
973 iio_ev_value_show,
974 iio_ev_value_store,
975 mask,
976 0,
977 &dev_info->event_interfaces[0]
978 .dev,
979 &dev_info->event_interfaces[0]
980 .dev_attr_list);
981 kfree(postfix);
982 if (ret)
983 goto error_ret;
984
985 }
986
987 error_ret:
988 return ret;
989 }
990
991 static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info,
992 const char *groupname,
993 int num)
994 {
995 struct iio_dev_attr *p, *n;
996 list_for_each_entry_safe(p, n,
997 &dev_info->event_interfaces[num].
998 dev_attr_list, l) {
999 sysfs_remove_file_from_group(&dev_info
1000 ->event_interfaces[num].dev.kobj,
1001 &p->dev_attr.attr,
1002 groupname);
1003 kfree(p->dev_attr.attr.name);
1004 kfree(p);
1005 }
1006 }
1007
1008 static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i)
1009 {
1010 int j;
1011 int ret;
1012 INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list);
1013 /* Dynically created from the channels array */
1014 if (dev_info->channels) {
1015 for (j = 0; j < dev_info->num_channels; j++) {
1016 ret = iio_device_add_event_sysfs(dev_info,
1017 &dev_info
1018 ->channels[j]);
1019 if (ret)
1020 goto error_clear_attrs;
1021 }
1022 }
1023 return 0;
1024
1025 error_clear_attrs:
1026 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1027
1028 return ret;
1029 }
1030
1031 static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info,
1032 int i)
1033 {
1034 __iio_remove_all_event_sysfs(dev_info, NULL, i);
1035 return 0;
1036 }
1037
1038 static int iio_device_register_eventset(struct iio_dev *dev_info)
1039 {
1040 int ret = 0, i, j;
1041
1042 if (dev_info->info->num_interrupt_lines == 0)
1043 return 0;
1044
1045 dev_info->event_interfaces =
1046 kzalloc(sizeof(struct iio_event_interface)
1047 *dev_info->info->num_interrupt_lines,
1048 GFP_KERNEL);
1049 if (dev_info->event_interfaces == NULL) {
1050 ret = -ENOMEM;
1051 goto error_ret;
1052 }
1053
1054 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1055 ret = iio_setup_ev_int(&dev_info->event_interfaces[i],
1056 dev_name(&dev_info->dev),
1057 i,
1058 dev_info->info->driver_module,
1059 &dev_info->dev);
1060 if (ret) {
1061 dev_err(&dev_info->dev,
1062 "Could not get chrdev interface\n");
1063 goto error_free_setup_ev_ints;
1064 }
1065
1066 dev_set_drvdata(&dev_info->event_interfaces[i].dev,
1067 (void *)dev_info);
1068
1069 if (dev_info->info->event_attrs != NULL)
1070 ret = sysfs_create_group(&dev_info
1071 ->event_interfaces[i]
1072 .dev.kobj,
1073 &dev_info->info
1074 ->event_attrs[i]);
1075
1076 if (ret) {
1077 dev_err(&dev_info->dev,
1078 "Failed to register sysfs for event attrs");
1079 goto error_remove_sysfs_interfaces;
1080 }
1081 }
1082
1083 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1084 ret = __iio_add_event_config_attrs(dev_info, i);
1085 if (ret)
1086 goto error_unregister_config_attrs;
1087 }
1088
1089 return 0;
1090
1091 error_unregister_config_attrs:
1092 for (j = 0; j < i; j++)
1093 __iio_remove_event_config_attrs(dev_info, i);
1094 i = dev_info->info->num_interrupt_lines - 1;
1095 error_remove_sysfs_interfaces:
1096 for (j = 0; j < i; j++)
1097 if (dev_info->info->event_attrs != NULL)
1098 sysfs_remove_group(&dev_info
1099 ->event_interfaces[j].dev.kobj,
1100 &dev_info->info->event_attrs[j]);
1101 error_free_setup_ev_ints:
1102 for (j = 0; j < i; j++)
1103 iio_free_ev_int(&dev_info->event_interfaces[j]);
1104 kfree(dev_info->event_interfaces);
1105 error_ret:
1106
1107 return ret;
1108 }
1109
1110 static void iio_device_unregister_eventset(struct iio_dev *dev_info)
1111 {
1112 int i;
1113
1114 if (dev_info->info->num_interrupt_lines == 0)
1115 return;
1116 for (i = 0; i < dev_info->info->num_interrupt_lines; i++) {
1117 __iio_remove_event_config_attrs(dev_info, i);
1118 if (dev_info->info->event_attrs != NULL)
1119 sysfs_remove_group(&dev_info
1120 ->event_interfaces[i].dev.kobj,
1121 &dev_info->info->event_attrs[i]);
1122 }
1123
1124 for (i = 0; i < dev_info->info->num_interrupt_lines; i++)
1125 iio_free_ev_int(&dev_info->event_interfaces[i]);
1126 kfree(dev_info->event_interfaces);
1127 }
1128
1129 static void iio_dev_release(struct device *device)
1130 {
1131 iio_put();
1132 kfree(to_iio_dev(device));
1133 }
1134
1135 static struct device_type iio_dev_type = {
1136 .name = "iio_device",
1137 .release = iio_dev_release,
1138 };
1139
1140 struct iio_dev *iio_allocate_device(int sizeof_priv)
1141 {
1142 struct iio_dev *dev;
1143 size_t alloc_size;
1144
1145 alloc_size = sizeof(struct iio_dev);
1146 if (sizeof_priv) {
1147 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
1148 alloc_size += sizeof_priv;
1149 }
1150 /* ensure 32-byte alignment of whole construct ? */
1151 alloc_size += IIO_ALIGN - 1;
1152
1153 dev = kzalloc(alloc_size, GFP_KERNEL);
1154
1155 if (dev) {
1156 dev->dev.type = &iio_dev_type;
1157 dev->dev.bus = &iio_bus_type;
1158 device_initialize(&dev->dev);
1159 dev_set_drvdata(&dev->dev, (void *)dev);
1160 mutex_init(&dev->mlock);
1161 iio_get();
1162 }
1163
1164 return dev;
1165 }
1166 EXPORT_SYMBOL(iio_allocate_device);
1167
1168 void iio_free_device(struct iio_dev *dev)
1169 {
1170 if (dev)
1171 iio_put_device(dev);
1172 }
1173 EXPORT_SYMBOL(iio_free_device);
1174
1175 int iio_device_register(struct iio_dev *dev_info)
1176 {
1177 int ret;
1178
1179 dev_info->id = iio_get_new_ida_val(&iio_ida);
1180 if (dev_info->id < 0) {
1181 ret = dev_info->id;
1182 dev_err(&dev_info->dev, "Failed to get id\n");
1183 goto error_ret;
1184 }
1185 dev_set_name(&dev_info->dev, "device%d", dev_info->id);
1186
1187 ret = device_add(&dev_info->dev);
1188 if (ret)
1189 goto error_free_ida;
1190 ret = iio_device_register_sysfs(dev_info);
1191 if (ret) {
1192 dev_err(dev_info->dev.parent,
1193 "Failed to register sysfs interfaces\n");
1194 goto error_del_device;
1195 }
1196 ret = iio_device_register_eventset(dev_info);
1197 if (ret) {
1198 dev_err(dev_info->dev.parent,
1199 "Failed to register event set\n");
1200 goto error_free_sysfs;
1201 }
1202 if (dev_info->modes & INDIO_RING_TRIGGERED)
1203 iio_device_register_trigger_consumer(dev_info);
1204
1205 return 0;
1206
1207 error_free_sysfs:
1208 iio_device_unregister_sysfs(dev_info);
1209 error_del_device:
1210 device_del(&dev_info->dev);
1211 error_free_ida:
1212 iio_free_ida_val(&iio_ida, dev_info->id);
1213 error_ret:
1214 return ret;
1215 }
1216 EXPORT_SYMBOL(iio_device_register);
1217
1218 void iio_device_unregister(struct iio_dev *dev_info)
1219 {
1220 if (dev_info->modes & INDIO_RING_TRIGGERED)
1221 iio_device_unregister_trigger_consumer(dev_info);
1222 iio_device_unregister_eventset(dev_info);
1223 iio_device_unregister_sysfs(dev_info);
1224 iio_free_ida_val(&iio_ida, dev_info->id);
1225 device_unregister(&dev_info->dev);
1226 }
1227 EXPORT_SYMBOL(iio_device_unregister);
1228
1229 void iio_put(void)
1230 {
1231 module_put(THIS_MODULE);
1232 }
1233
1234 void iio_get(void)
1235 {
1236 __module_get(THIS_MODULE);
1237 }
1238
1239 subsys_initcall(iio_init);
1240 module_exit(iio_exit);
1241
1242 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
1243 MODULE_DESCRIPTION("Industrial I/O core");
1244 MODULE_LICENSE("GPL");
This page took 0.118049 seconds and 5 git commands to generate.