memcg: always create memsw files if CONFIG_CGROUP_MEM_RES_CTLR_SWAP
[linux-2.6.git] / drivers / staging / iio / industrialio-core.c
blobd303bfbff27fb669b6efc83f86f4b4f920977e8b
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 <linux/anon_inodes.h>
25 #include <linux/debugfs.h>
26 #include "iio.h"
27 #include "iio_core.h"
28 #include "iio_core_trigger.h"
29 #include "sysfs.h"
30 #include "events.h"
32 /* IDA to assign each registered device a unique id*/
33 static DEFINE_IDA(iio_ida);
35 static dev_t iio_devt;
37 #define IIO_DEV_MAX 256
38 struct bus_type iio_bus_type = {
39 .name = "iio",
41 EXPORT_SYMBOL(iio_bus_type);
43 static struct dentry *iio_debugfs_dentry;
45 static const char * const iio_data_type_name[] = {
46 [IIO_RAW] = "raw",
47 [IIO_PROCESSED] = "input",
50 static const char * const iio_direction[] = {
51 [0] = "in",
52 [1] = "out",
55 static const char * const iio_chan_type_name_spec[] = {
56 [IIO_VOLTAGE] = "voltage",
57 [IIO_CURRENT] = "current",
58 [IIO_POWER] = "power",
59 [IIO_ACCEL] = "accel",
60 [IIO_ANGL_VEL] = "anglvel",
61 [IIO_MAGN] = "magn",
62 [IIO_LIGHT] = "illuminance",
63 [IIO_INTENSITY] = "intensity",
64 [IIO_PROXIMITY] = "proximity",
65 [IIO_TEMP] = "temp",
66 [IIO_INCLI] = "incli",
67 [IIO_ROT] = "rot",
68 [IIO_ANGL] = "angl",
69 [IIO_TIMESTAMP] = "timestamp",
70 [IIO_CAPACITANCE] = "capacitance",
73 static const char * const iio_modifier_names[] = {
74 [IIO_MOD_X] = "x",
75 [IIO_MOD_Y] = "y",
76 [IIO_MOD_Z] = "z",
77 [IIO_MOD_LIGHT_BOTH] = "both",
78 [IIO_MOD_LIGHT_IR] = "ir",
81 /* relies on pairs of these shared then separate */
82 static const char * const iio_chan_info_postfix[] = {
83 [IIO_CHAN_INFO_SCALE] = "scale",
84 [IIO_CHAN_INFO_OFFSET] = "offset",
85 [IIO_CHAN_INFO_CALIBSCALE] = "calibscale",
86 [IIO_CHAN_INFO_CALIBBIAS] = "calibbias",
87 [IIO_CHAN_INFO_PEAK] = "peak_raw",
88 [IIO_CHAN_INFO_PEAK_SCALE] = "peak_scale",
89 [IIO_CHAN_INFO_QUADRATURE_CORRECTION_RAW] = "quadrature_correction_raw",
90 [IIO_CHAN_INFO_AVERAGE_RAW] = "mean_raw",
91 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
92 = "filter_low_pass_3db_frequency",
95 const struct iio_chan_spec
96 *iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
98 int i;
100 for (i = 0; i < indio_dev->num_channels; i++)
101 if (indio_dev->channels[i].scan_index == si)
102 return &indio_dev->channels[i];
103 return NULL;
106 /* This turns up an awful lot */
107 ssize_t iio_read_const_attr(struct device *dev,
108 struct device_attribute *attr,
109 char *buf)
111 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
113 EXPORT_SYMBOL(iio_read_const_attr);
115 static int __init iio_init(void)
117 int ret;
119 /* Register sysfs bus */
120 ret = bus_register(&iio_bus_type);
121 if (ret < 0) {
122 printk(KERN_ERR
123 "%s could not register bus type\n",
124 __FILE__);
125 goto error_nothing;
128 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
129 if (ret < 0) {
130 printk(KERN_ERR "%s: failed to allocate char dev region\n",
131 __FILE__);
132 goto error_unregister_bus_type;
135 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
137 return 0;
139 error_unregister_bus_type:
140 bus_unregister(&iio_bus_type);
141 error_nothing:
142 return ret;
145 static void __exit iio_exit(void)
147 if (iio_devt)
148 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
149 bus_unregister(&iio_bus_type);
150 debugfs_remove(iio_debugfs_dentry);
153 #if defined(CONFIG_DEBUG_FS)
154 static int iio_debugfs_open(struct inode *inode, struct file *file)
156 if (inode->i_private)
157 file->private_data = inode->i_private;
159 return 0;
162 static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
163 size_t count, loff_t *ppos)
165 struct iio_dev *indio_dev = file->private_data;
166 char buf[20];
167 unsigned val = 0;
168 ssize_t len;
169 int ret;
171 ret = indio_dev->info->debugfs_reg_access(indio_dev,
172 indio_dev->cached_reg_addr,
173 0, &val);
174 if (ret)
175 dev_err(indio_dev->dev.parent, "%s: read failed\n", __func__);
177 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
179 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
182 static ssize_t iio_debugfs_write_reg(struct file *file,
183 const char __user *userbuf, size_t count, loff_t *ppos)
185 struct iio_dev *indio_dev = file->private_data;
186 unsigned reg, val;
187 char buf[80];
188 int ret;
190 count = min_t(size_t, count, (sizeof(buf)-1));
191 if (copy_from_user(buf, userbuf, count))
192 return -EFAULT;
194 buf[count] = 0;
196 ret = sscanf(buf, "%i %i", &reg, &val);
198 switch (ret) {
199 case 1:
200 indio_dev->cached_reg_addr = reg;
201 break;
202 case 2:
203 indio_dev->cached_reg_addr = reg;
204 ret = indio_dev->info->debugfs_reg_access(indio_dev, reg,
205 val, NULL);
206 if (ret) {
207 dev_err(indio_dev->dev.parent, "%s: write failed\n",
208 __func__);
209 return ret;
211 break;
212 default:
213 return -EINVAL;
216 return count;
219 static const struct file_operations iio_debugfs_reg_fops = {
220 .open = iio_debugfs_open,
221 .read = iio_debugfs_read_reg,
222 .write = iio_debugfs_write_reg,
225 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
227 debugfs_remove_recursive(indio_dev->debugfs_dentry);
230 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
232 struct dentry *d;
234 if (indio_dev->info->debugfs_reg_access == NULL)
235 return 0;
237 if (IS_ERR(iio_debugfs_dentry))
238 return 0;
240 indio_dev->debugfs_dentry =
241 debugfs_create_dir(dev_name(&indio_dev->dev),
242 iio_debugfs_dentry);
243 if (IS_ERR(indio_dev->debugfs_dentry))
244 return PTR_ERR(indio_dev->debugfs_dentry);
246 if (indio_dev->debugfs_dentry == NULL) {
247 dev_warn(indio_dev->dev.parent,
248 "Failed to create debugfs directory\n");
249 return -EFAULT;
252 d = debugfs_create_file("direct_reg_access", 0644,
253 indio_dev->debugfs_dentry,
254 indio_dev, &iio_debugfs_reg_fops);
255 if (!d) {
256 iio_device_unregister_debugfs(indio_dev);
257 return -ENOMEM;
260 return 0;
262 #else
263 static int iio_device_register_debugfs(struct iio_dev *indio_dev)
265 return 0;
268 static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
271 #endif /* CONFIG_DEBUG_FS */
273 static ssize_t iio_read_channel_ext_info(struct device *dev,
274 struct device_attribute *attr,
275 char *buf)
277 struct iio_dev *indio_dev = dev_get_drvdata(dev);
278 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
279 const struct iio_chan_spec_ext_info *ext_info;
281 ext_info = &this_attr->c->ext_info[this_attr->address];
283 return ext_info->read(indio_dev, this_attr->c, buf);
286 static ssize_t iio_write_channel_ext_info(struct device *dev,
287 struct device_attribute *attr,
288 const char *buf,
289 size_t len)
291 struct iio_dev *indio_dev = dev_get_drvdata(dev);
292 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
293 const struct iio_chan_spec_ext_info *ext_info;
295 ext_info = &this_attr->c->ext_info[this_attr->address];
297 return ext_info->write(indio_dev, this_attr->c, buf, len);
300 static ssize_t iio_read_channel_info(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
304 struct iio_dev *indio_dev = dev_get_drvdata(dev);
305 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
306 int val, val2;
307 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
308 &val, &val2, this_attr->address);
310 if (ret < 0)
311 return ret;
313 if (ret == IIO_VAL_INT)
314 return sprintf(buf, "%d\n", val);
315 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
316 if (val2 < 0)
317 return sprintf(buf, "-%d.%06u\n", val, -val2);
318 else
319 return sprintf(buf, "%d.%06u\n", val, val2);
320 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
321 if (val2 < 0)
322 return sprintf(buf, "-%d.%09u\n", val, -val2);
323 else
324 return sprintf(buf, "%d.%09u\n", val, val2);
325 } else
326 return 0;
329 static ssize_t iio_write_channel_info(struct device *dev,
330 struct device_attribute *attr,
331 const char *buf,
332 size_t len)
334 struct iio_dev *indio_dev = dev_get_drvdata(dev);
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
336 int ret, integer = 0, fract = 0, fract_mult = 100000;
337 bool integer_part = true, negative = false;
339 /* Assumes decimal - precision based on number of digits */
340 if (!indio_dev->info->write_raw)
341 return -EINVAL;
343 if (indio_dev->info->write_raw_get_fmt)
344 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
345 this_attr->c, this_attr->address)) {
346 case IIO_VAL_INT_PLUS_MICRO:
347 fract_mult = 100000;
348 break;
349 case IIO_VAL_INT_PLUS_NANO:
350 fract_mult = 100000000;
351 break;
352 default:
353 return -EINVAL;
356 if (buf[0] == '-') {
357 negative = true;
358 buf++;
361 while (*buf) {
362 if ('0' <= *buf && *buf <= '9') {
363 if (integer_part)
364 integer = integer*10 + *buf - '0';
365 else {
366 fract += fract_mult*(*buf - '0');
367 if (fract_mult == 1)
368 break;
369 fract_mult /= 10;
371 } else if (*buf == '\n') {
372 if (*(buf + 1) == '\0')
373 break;
374 else
375 return -EINVAL;
376 } else if (*buf == '.') {
377 integer_part = false;
378 } else {
379 return -EINVAL;
381 buf++;
383 if (negative) {
384 if (integer)
385 integer = -integer;
386 else
387 fract = -fract;
390 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
391 integer, fract, this_attr->address);
392 if (ret)
393 return ret;
395 return len;
398 static
399 int __iio_device_attr_init(struct device_attribute *dev_attr,
400 const char *postfix,
401 struct iio_chan_spec const *chan,
402 ssize_t (*readfunc)(struct device *dev,
403 struct device_attribute *attr,
404 char *buf),
405 ssize_t (*writefunc)(struct device *dev,
406 struct device_attribute *attr,
407 const char *buf,
408 size_t len),
409 bool generic)
411 int ret;
412 char *name_format, *full_postfix;
413 sysfs_attr_init(&dev_attr->attr);
415 /* Build up postfix of <extend_name>_<modifier>_postfix */
416 if (chan->modified && !generic) {
417 if (chan->extend_name)
418 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
419 iio_modifier_names[chan
420 ->channel2],
421 chan->extend_name,
422 postfix);
423 else
424 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
425 iio_modifier_names[chan
426 ->channel2],
427 postfix);
428 } else {
429 if (chan->extend_name == NULL)
430 full_postfix = kstrdup(postfix, GFP_KERNEL);
431 else
432 full_postfix = kasprintf(GFP_KERNEL,
433 "%s_%s",
434 chan->extend_name,
435 postfix);
437 if (full_postfix == NULL) {
438 ret = -ENOMEM;
439 goto error_ret;
442 if (chan->differential) { /* Differential can not have modifier */
443 if (generic)
444 name_format
445 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
446 iio_direction[chan->output],
447 iio_chan_type_name_spec[chan->type],
448 iio_chan_type_name_spec[chan->type],
449 full_postfix);
450 else if (chan->indexed)
451 name_format
452 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
453 iio_direction[chan->output],
454 iio_chan_type_name_spec[chan->type],
455 chan->channel,
456 iio_chan_type_name_spec[chan->type],
457 chan->channel2,
458 full_postfix);
459 else {
460 WARN_ON("Differential channels must be indexed\n");
461 ret = -EINVAL;
462 goto error_free_full_postfix;
464 } else { /* Single ended */
465 if (generic)
466 name_format
467 = kasprintf(GFP_KERNEL, "%s_%s_%s",
468 iio_direction[chan->output],
469 iio_chan_type_name_spec[chan->type],
470 full_postfix);
471 else if (chan->indexed)
472 name_format
473 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
474 iio_direction[chan->output],
475 iio_chan_type_name_spec[chan->type],
476 chan->channel,
477 full_postfix);
478 else
479 name_format
480 = kasprintf(GFP_KERNEL, "%s_%s_%s",
481 iio_direction[chan->output],
482 iio_chan_type_name_spec[chan->type],
483 full_postfix);
485 if (name_format == NULL) {
486 ret = -ENOMEM;
487 goto error_free_full_postfix;
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;
498 if (readfunc) {
499 dev_attr->attr.mode |= S_IRUGO;
500 dev_attr->show = readfunc;
503 if (writefunc) {
504 dev_attr->attr.mode |= S_IWUSR;
505 dev_attr->store = writefunc;
507 kfree(name_format);
508 kfree(full_postfix);
510 return 0;
512 error_free_name_format:
513 kfree(name_format);
514 error_free_full_postfix:
515 kfree(full_postfix);
516 error_ret:
517 return ret;
520 static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
522 kfree(dev_attr->attr.name);
525 int __iio_add_chan_devattr(const char *postfix,
526 struct iio_chan_spec const *chan,
527 ssize_t (*readfunc)(struct device *dev,
528 struct device_attribute *attr,
529 char *buf),
530 ssize_t (*writefunc)(struct device *dev,
531 struct device_attribute *attr,
532 const char *buf,
533 size_t len),
534 u64 mask,
535 bool generic,
536 struct device *dev,
537 struct list_head *attr_list)
539 int ret;
540 struct iio_dev_attr *iio_attr, *t;
542 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
543 if (iio_attr == NULL) {
544 ret = -ENOMEM;
545 goto error_ret;
547 ret = __iio_device_attr_init(&iio_attr->dev_attr,
548 postfix, chan,
549 readfunc, writefunc, generic);
550 if (ret)
551 goto error_iio_dev_attr_free;
552 iio_attr->c = chan;
553 iio_attr->address = mask;
554 list_for_each_entry(t, attr_list, l)
555 if (strcmp(t->dev_attr.attr.name,
556 iio_attr->dev_attr.attr.name) == 0) {
557 if (!generic)
558 dev_err(dev, "tried to double register : %s\n",
559 t->dev_attr.attr.name);
560 ret = -EBUSY;
561 goto error_device_attr_deinit;
563 list_add(&iio_attr->l, attr_list);
565 return 0;
567 error_device_attr_deinit:
568 __iio_device_attr_deinit(&iio_attr->dev_attr);
569 error_iio_dev_attr_free:
570 kfree(iio_attr);
571 error_ret:
572 return ret;
575 static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
576 struct iio_chan_spec const *chan)
578 int ret, i, attrcount = 0;
579 const struct iio_chan_spec_ext_info *ext_info;
581 if (chan->channel < 0)
582 return 0;
584 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
585 chan,
586 &iio_read_channel_info,
587 (chan->output ?
588 &iio_write_channel_info : NULL),
591 &indio_dev->dev,
592 &indio_dev->channel_attr_list);
593 if (ret)
594 goto error_ret;
595 attrcount++;
597 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
598 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
599 chan,
600 &iio_read_channel_info,
601 &iio_write_channel_info,
602 i/2,
603 !(i%2),
604 &indio_dev->dev,
605 &indio_dev->channel_attr_list);
606 if (ret == -EBUSY && (i%2 == 0)) {
607 ret = 0;
608 continue;
610 if (ret < 0)
611 goto error_ret;
612 attrcount++;
615 if (chan->ext_info) {
616 unsigned int i = 0;
617 for (ext_info = chan->ext_info; ext_info->name; ext_info++) {
618 ret = __iio_add_chan_devattr(ext_info->name,
619 chan,
620 ext_info->read ?
621 &iio_read_channel_ext_info : NULL,
622 ext_info->write ?
623 &iio_write_channel_ext_info : NULL,
625 ext_info->shared,
626 &indio_dev->dev,
627 &indio_dev->channel_attr_list);
628 i++;
629 if (ret == -EBUSY && ext_info->shared)
630 continue;
632 if (ret)
633 goto error_ret;
635 attrcount++;
639 ret = attrcount;
640 error_ret:
641 return ret;
644 static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
645 struct iio_dev_attr *p)
647 kfree(p->dev_attr.attr.name);
648 kfree(p);
651 static ssize_t iio_show_dev_name(struct device *dev,
652 struct device_attribute *attr,
653 char *buf)
655 struct iio_dev *indio_dev = dev_get_drvdata(dev);
656 return sprintf(buf, "%s\n", indio_dev->name);
659 static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
661 static int iio_device_register_sysfs(struct iio_dev *indio_dev)
663 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
664 struct iio_dev_attr *p, *n;
665 struct attribute **attr;
667 /* First count elements in any existing group */
668 if (indio_dev->info->attrs) {
669 attr = indio_dev->info->attrs->attrs;
670 while (*attr++ != NULL)
671 attrcount_orig++;
673 attrcount = attrcount_orig;
675 * New channel registration method - relies on the fact a group does
676 * not need to be initialized if it is name is NULL.
678 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
679 if (indio_dev->channels)
680 for (i = 0; i < indio_dev->num_channels; i++) {
681 ret = iio_device_add_channel_sysfs(indio_dev,
682 &indio_dev
683 ->channels[i]);
684 if (ret < 0)
685 goto error_clear_attrs;
686 attrcount += ret;
689 if (indio_dev->name)
690 attrcount++;
692 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
693 sizeof(indio_dev->chan_attr_group.attrs[0]),
694 GFP_KERNEL);
695 if (indio_dev->chan_attr_group.attrs == NULL) {
696 ret = -ENOMEM;
697 goto error_clear_attrs;
699 /* Copy across original attributes */
700 if (indio_dev->info->attrs)
701 memcpy(indio_dev->chan_attr_group.attrs,
702 indio_dev->info->attrs->attrs,
703 sizeof(indio_dev->chan_attr_group.attrs[0])
704 *attrcount_orig);
705 attrn = attrcount_orig;
706 /* Add all elements from the list. */
707 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
708 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
709 if (indio_dev->name)
710 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
712 indio_dev->groups[indio_dev->groupcounter++] =
713 &indio_dev->chan_attr_group;
715 return 0;
717 error_clear_attrs:
718 list_for_each_entry_safe(p, n,
719 &indio_dev->channel_attr_list, l) {
720 list_del(&p->l);
721 iio_device_remove_and_free_read_attr(indio_dev, p);
724 return ret;
727 static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
730 struct iio_dev_attr *p, *n;
732 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
733 list_del(&p->l);
734 iio_device_remove_and_free_read_attr(indio_dev, p);
736 kfree(indio_dev->chan_attr_group.attrs);
739 static void iio_dev_release(struct device *device)
741 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
742 cdev_del(&indio_dev->chrdev);
743 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
744 iio_device_unregister_trigger_consumer(indio_dev);
745 iio_device_unregister_eventset(indio_dev);
746 iio_device_unregister_sysfs(indio_dev);
747 iio_device_unregister_debugfs(indio_dev);
750 static struct device_type iio_dev_type = {
751 .name = "iio_device",
752 .release = iio_dev_release,
755 struct iio_dev *iio_allocate_device(int sizeof_priv)
757 struct iio_dev *dev;
758 size_t alloc_size;
760 alloc_size = sizeof(struct iio_dev);
761 if (sizeof_priv) {
762 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
763 alloc_size += sizeof_priv;
765 /* ensure 32-byte alignment of whole construct ? */
766 alloc_size += IIO_ALIGN - 1;
768 dev = kzalloc(alloc_size, GFP_KERNEL);
770 if (dev) {
771 dev->dev.groups = dev->groups;
772 dev->dev.type = &iio_dev_type;
773 dev->dev.bus = &iio_bus_type;
774 device_initialize(&dev->dev);
775 dev_set_drvdata(&dev->dev, (void *)dev);
776 mutex_init(&dev->mlock);
777 mutex_init(&dev->info_exist_lock);
779 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
780 if (dev->id < 0) {
781 /* cannot use a dev_err as the name isn't available */
782 printk(KERN_ERR "Failed to get id\n");
783 kfree(dev);
784 return NULL;
786 dev_set_name(&dev->dev, "iio:device%d", dev->id);
789 return dev;
791 EXPORT_SYMBOL(iio_allocate_device);
793 void iio_free_device(struct iio_dev *dev)
795 if (dev) {
796 ida_simple_remove(&iio_ida, dev->id);
797 kfree(dev);
800 EXPORT_SYMBOL(iio_free_device);
803 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
805 static int iio_chrdev_open(struct inode *inode, struct file *filp)
807 struct iio_dev *indio_dev = container_of(inode->i_cdev,
808 struct iio_dev, chrdev);
810 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
811 return -EBUSY;
813 filp->private_data = indio_dev;
815 return 0;
819 * iio_chrdev_release() - chrdev file close buffer access and ioctls
821 static int iio_chrdev_release(struct inode *inode, struct file *filp)
823 struct iio_dev *indio_dev = container_of(inode->i_cdev,
824 struct iio_dev, chrdev);
825 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
826 return 0;
829 /* Somewhat of a cross file organization violation - ioctls here are actually
830 * event related */
831 static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
833 struct iio_dev *indio_dev = filp->private_data;
834 int __user *ip = (int __user *)arg;
835 int fd;
837 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
838 fd = iio_event_getfd(indio_dev);
839 if (copy_to_user(ip, &fd, sizeof(fd)))
840 return -EFAULT;
841 return 0;
843 return -EINVAL;
846 static const struct file_operations iio_buffer_fileops = {
847 .read = iio_buffer_read_first_n_outer_addr,
848 .release = iio_chrdev_release,
849 .open = iio_chrdev_open,
850 .poll = iio_buffer_poll_addr,
851 .owner = THIS_MODULE,
852 .llseek = noop_llseek,
853 .unlocked_ioctl = iio_ioctl,
854 .compat_ioctl = iio_ioctl,
857 static const struct iio_buffer_setup_ops noop_ring_setup_ops;
859 int iio_device_register(struct iio_dev *indio_dev)
861 int ret;
863 /* configure elements for the chrdev */
864 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
866 ret = iio_device_register_debugfs(indio_dev);
867 if (ret) {
868 dev_err(indio_dev->dev.parent,
869 "Failed to register debugfs interfaces\n");
870 goto error_ret;
872 ret = iio_device_register_sysfs(indio_dev);
873 if (ret) {
874 dev_err(indio_dev->dev.parent,
875 "Failed to register sysfs interfaces\n");
876 goto error_unreg_debugfs;
878 ret = iio_device_register_eventset(indio_dev);
879 if (ret) {
880 dev_err(indio_dev->dev.parent,
881 "Failed to register event set\n");
882 goto error_free_sysfs;
884 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
885 iio_device_register_trigger_consumer(indio_dev);
887 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
888 indio_dev->setup_ops == NULL)
889 indio_dev->setup_ops = &noop_ring_setup_ops;
891 ret = device_add(&indio_dev->dev);
892 if (ret < 0)
893 goto error_unreg_eventset;
894 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
895 indio_dev->chrdev.owner = indio_dev->info->driver_module;
896 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
897 if (ret < 0)
898 goto error_del_device;
899 return 0;
901 error_del_device:
902 device_del(&indio_dev->dev);
903 error_unreg_eventset:
904 iio_device_unregister_eventset(indio_dev);
905 error_free_sysfs:
906 iio_device_unregister_sysfs(indio_dev);
907 error_unreg_debugfs:
908 iio_device_unregister_debugfs(indio_dev);
909 error_ret:
910 return ret;
912 EXPORT_SYMBOL(iio_device_register);
914 void iio_device_unregister(struct iio_dev *indio_dev)
916 mutex_lock(&indio_dev->info_exist_lock);
917 indio_dev->info = NULL;
918 mutex_unlock(&indio_dev->info_exist_lock);
919 device_unregister(&indio_dev->dev);
921 EXPORT_SYMBOL(iio_device_unregister);
922 subsys_initcall(iio_init);
923 module_exit(iio_exit);
925 MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
926 MODULE_DESCRIPTION("Industrial I/O core");
927 MODULE_LICENSE("GPL");