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