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