staging:iio:Documentation Trivial typo fixes.
[deliverable/linux.git] / drivers / staging / iio / industrialio-core.c
CommitLineData
847ec80b
JC
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>
847ec80b 19#include <linux/poll.h>
ffc18afa 20#include <linux/sched.h>
4439c935 21#include <linux/wait.h>
847ec80b 22#include <linux/cdev.h>
5a0e3ad6 23#include <linux/slab.h>
8e7d9672 24#include <linux/anon_inodes.h>
e553f182 25#include <linux/debugfs.h>
847ec80b 26#include "iio.h"
df9c1c42 27#include "iio_core.h"
6aea1c36 28#include "iio_core_trigger.h"
9dd1cb30 29#include "sysfs.h"
af5046af 30#include "events.h"
9dd1cb30 31
47c24fdd 32/* IDA to assign each registered device a unique id*/
b156cf70 33static DEFINE_IDA(iio_ida);
847ec80b 34
f625cb97 35static dev_t iio_devt;
847ec80b
JC
36
37#define IIO_DEV_MAX 256
5aaaeba8 38struct bus_type iio_bus_type = {
847ec80b 39 .name = "iio",
847ec80b 40};
5aaaeba8 41EXPORT_SYMBOL(iio_bus_type);
847ec80b 42
e553f182
MH
43static struct dentry *iio_debugfs_dentry;
44
c6fc8062
JC
45static const char * const iio_direction[] = {
46 [0] = "in",
47 [1] = "out",
48};
49
ade7ef7b 50static const char * const iio_chan_type_name_spec[] = {
c6fc8062 51 [IIO_VOLTAGE] = "voltage",
faf290e8
MH
52 [IIO_CURRENT] = "current",
53 [IIO_POWER] = "power",
9bff02f8 54 [IIO_ACCEL] = "accel",
41ea040c 55 [IIO_ANGL_VEL] = "anglvel",
1d892719 56 [IIO_MAGN] = "magn",
9bff02f8
BF
57 [IIO_LIGHT] = "illuminance",
58 [IIO_INTENSITY] = "intensity",
f09f2c81 59 [IIO_PROXIMITY] = "proximity",
9bff02f8 60 [IIO_TEMP] = "temp",
1d892719
JC
61 [IIO_INCLI] = "incli",
62 [IIO_ROT] = "rot",
1d892719 63 [IIO_ANGL] = "angl",
9bff02f8 64 [IIO_TIMESTAMP] = "timestamp",
66dbe704 65 [IIO_CAPACITANCE] = "capacitance",
1d892719
JC
66};
67
330c6c57 68static const char * const iio_modifier_names[] = {
1d892719
JC
69 [IIO_MOD_X] = "x",
70 [IIO_MOD_Y] = "y",
71 [IIO_MOD_Z] = "z",
330c6c57
JC
72 [IIO_MOD_LIGHT_BOTH] = "both",
73 [IIO_MOD_LIGHT_IR] = "ir",
1d892719
JC
74};
75
76/* relies on pairs of these shared then separate */
77static const char * const iio_chan_info_postfix[] = {
75a973c7
JC
78 [IIO_CHAN_INFO_RAW] = "raw",
79 [IIO_CHAN_INFO_PROCESSED] = "input",
c8a9f805
JC
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",
df94aba8
JC
88 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
89 = "filter_low_pass_3db_frequency",
ce85a1cb 90 [IIO_CHAN_INFO_SAMP_FREQ] = "sampling_frequency",
1d892719
JC
91};
92
5fb21c82
JC
93const 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
847ec80b
JC
104/* This turns up an awful lot */
105ssize_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}
111EXPORT_SYMBOL(iio_read_const_attr);
112
847ec80b
JC
113static int __init iio_init(void)
114{
115 int ret;
116
5aaaeba8
JC
117 /* Register sysfs bus */
118 ret = bus_register(&iio_bus_type);
847ec80b
JC
119 if (ret < 0) {
120 printk(KERN_ERR
5aaaeba8 121 "%s could not register bus type\n",
847ec80b
JC
122 __FILE__);
123 goto error_nothing;
124 }
125
9aa1a167
JC
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__);
5aaaeba8 130 goto error_unregister_bus_type;
9aa1a167 131 }
847ec80b 132
e553f182
MH
133 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
134
847ec80b
JC
135 return 0;
136
5aaaeba8
JC
137error_unregister_bus_type:
138 bus_unregister(&iio_bus_type);
847ec80b
JC
139error_nothing:
140 return ret;
141}
142
143static void __exit iio_exit(void)
144{
9aa1a167
JC
145 if (iio_devt)
146 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
5aaaeba8 147 bus_unregister(&iio_bus_type);
e553f182
MH
148 debugfs_remove(iio_debugfs_dentry);
149}
150
151#if defined(CONFIG_DEBUG_FS)
152static 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
160static 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
180static 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
217static 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
223static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
224{
225 debugfs_remove_recursive(indio_dev->debugfs_dentry);
847ec80b
JC
226}
227
e553f182
MH
228static 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
261static int iio_device_register_debugfs(struct iio_dev *indio_dev)
262{
263 return 0;
264}
265
266static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
267{
268}
269#endif /* CONFIG_DEBUG_FS */
270
4fee7e16
LPC
271static 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
284static 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
1d892719
JC
298static ssize_t iio_read_channel_info(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
847ec80b 301{
1d892719
JC
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;
6fe8135f
JC
305 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
306 &val, &val2, this_attr->address);
1d892719
JC
307
308 if (ret < 0)
309 return ret;
847ec80b 310
1d892719
JC
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);
71646e2c
MH
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);
1d892719
JC
323 } else
324 return 0;
325}
326
327static 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);
5c04af04 334 int ret, integer = 0, fract = 0, fract_mult = 100000;
1d892719
JC
335 bool integer_part = true, negative = false;
336
337 /* Assumes decimal - precision based on number of digits */
6fe8135f 338 if (!indio_dev->info->write_raw)
1d892719 339 return -EINVAL;
5c04af04
MH
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
1d892719
JC
354 if (buf[0] == '-') {
355 negative = true;
356 buf++;
357 }
5c04af04 358
1d892719
JC
359 while (*buf) {
360 if ('0' <= *buf && *buf <= '9') {
361 if (integer_part)
362 integer = integer*10 + *buf - '0';
363 else {
5c04af04
MH
364 fract += fract_mult*(*buf - '0');
365 if (fract_mult == 1)
1d892719 366 break;
5c04af04 367 fract_mult /= 10;
1d892719
JC
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
5c04af04 385 fract = -fract;
1d892719
JC
386 }
387
6fe8135f 388 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
5c04af04 389 integer, fract, this_attr->address);
1d892719
JC
390 if (ret)
391 return ret;
392
393 return len;
394}
395
df9c1c42 396static
1d892719
JC
397int __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);
1d892719 412
ade7ef7b 413 /* Build up postfix of <extend_name>_<modifier>_postfix */
0403e0d6 414 if (chan->modified && !generic) {
ade7ef7b
JC
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 }
1d892719 439
4abf6f8b 440 if (chan->differential) { /* Differential can not have modifier */
ade7ef7b
JC
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 }
1d892719
JC
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
510error_free_name_format:
511 kfree(name_format);
512error_free_full_postfix:
513 kfree(full_postfix);
514error_ret:
515 return ret;
516}
517
df9c1c42 518static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1d892719
JC
519{
520 kfree(dev_attr->attr.name);
521}
522
523int __iio_add_chan_devattr(const char *postfix,
1d892719
JC
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),
e614a54b 532 u64 mask,
1d892719
JC
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 }
1d892719
JC
561 list_add(&iio_attr->l, attr_list);
562
563 return 0;
564
565error_device_attr_deinit:
566 __iio_device_attr_deinit(&iio_attr->dev_attr);
567error_iio_dev_attr_free:
568 kfree(iio_attr);
569error_ret:
570 return ret;
571}
572
f8c6f4e9 573static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1d892719
JC
574 struct iio_chan_spec const *chan)
575{
5ccb3adb 576 int ret, attrcount = 0;
75a973c7 577 int i;
5f420b42 578 const struct iio_chan_spec_ext_info *ext_info;
1d892719 579
1d892719
JC
580 if (chan->channel < 0)
581 return 0;
75a973c7 582 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
1d892719 583 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
26d25ae3 584 chan,
1d892719
JC
585 &iio_read_channel_info,
586 &iio_write_channel_info,
c8a9f805 587 i/2,
1d892719 588 !(i%2),
f8c6f4e9
JC
589 &indio_dev->dev,
590 &indio_dev->channel_attr_list);
1d892719
JC
591 if (ret == -EBUSY && (i%2 == 0)) {
592 ret = 0;
593 continue;
594 }
595 if (ret < 0)
596 goto error_ret;
26d25ae3 597 attrcount++;
1d892719 598 }
5f420b42
LPC
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
26d25ae3 624 ret = attrcount;
1d892719
JC
625error_ret:
626 return ret;
627}
628
f8c6f4e9 629static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
1d892719
JC
630 struct iio_dev_attr *p)
631{
1d892719
JC
632 kfree(p->dev_attr.attr.name);
633 kfree(p);
634}
635
1b732888
JC
636static 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
644static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
645
f8c6f4e9 646static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1d892719 647{
26d25ae3 648 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1d892719 649 struct iio_dev_attr *p, *n;
26d25ae3 650 struct attribute **attr;
1d892719 651
26d25ae3 652 /* First count elements in any existing group */
f8c6f4e9
JC
653 if (indio_dev->info->attrs) {
654 attr = indio_dev->info->attrs->attrs;
26d25ae3
JC
655 while (*attr++ != NULL)
656 attrcount_orig++;
847ec80b 657 }
26d25ae3 658 attrcount = attrcount_orig;
1d892719
JC
659 /*
660 * New channel registration method - relies on the fact a group does
4abf6f8b 661 * not need to be initialized if it is name is NULL.
1d892719 662 */
f8c6f4e9
JC
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
1d892719
JC
668 ->channels[i]);
669 if (ret < 0)
670 goto error_clear_attrs;
26d25ae3 671 attrcount += ret;
1d892719 672 }
26d25ae3 673
f8c6f4e9 674 if (indio_dev->name)
26d25ae3
JC
675 attrcount++;
676
d83fb184
TM
677 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
678 sizeof(indio_dev->chan_attr_group.attrs[0]),
679 GFP_KERNEL);
f8c6f4e9 680 if (indio_dev->chan_attr_group.attrs == NULL) {
26d25ae3
JC
681 ret = -ENOMEM;
682 goto error_clear_attrs;
1b732888 683 }
26d25ae3 684 /* Copy across original attributes */
f8c6f4e9
JC
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])
26d25ae3
JC
689 *attrcount_orig);
690 attrn = attrcount_orig;
691 /* Add all elements from the list. */
f8c6f4e9
JC
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;
26d25ae3 696
f8c6f4e9
JC
697 indio_dev->groups[indio_dev->groupcounter++] =
698 &indio_dev->chan_attr_group;
26d25ae3 699
1d892719 700 return 0;
1b732888 701
1d892719
JC
702error_clear_attrs:
703 list_for_each_entry_safe(p, n,
f8c6f4e9 704 &indio_dev->channel_attr_list, l) {
1d892719 705 list_del(&p->l);
f8c6f4e9 706 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 707 }
1d892719 708
26d25ae3 709 return ret;
847ec80b
JC
710}
711
f8c6f4e9 712static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
847ec80b 713{
1d892719
JC
714
715 struct iio_dev_attr *p, *n;
26d25ae3 716
f8c6f4e9 717 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
1d892719 718 list_del(&p->l);
f8c6f4e9 719 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 720 }
f8c6f4e9 721 kfree(indio_dev->chan_attr_group.attrs);
847ec80b
JC
722}
723
847ec80b
JC
724static void iio_dev_release(struct device *device)
725{
f8c6f4e9
JC
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);
e553f182 732 iio_device_unregister_debugfs(indio_dev);
847ec80b
JC
733}
734
735static struct device_type iio_dev_type = {
736 .name = "iio_device",
737 .release = iio_dev_release,
738};
739
6f7c8ee5 740struct iio_dev *iio_allocate_device(int sizeof_priv)
847ec80b 741{
6f7c8ee5
JC
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);
847ec80b
JC
754
755 if (dev) {
26d25ae3 756 dev->dev.groups = dev->groups;
847ec80b 757 dev->dev.type = &iio_dev_type;
5aaaeba8 758 dev->dev.bus = &iio_bus_type;
847ec80b
JC
759 device_initialize(&dev->dev);
760 dev_set_drvdata(&dev->dev, (void *)dev);
761 mutex_init(&dev->mlock);
ac917a81 762 mutex_init(&dev->info_exist_lock);
a9e39f9e
JC
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);
847ec80b
JC
772 }
773
774 return dev;
775}
776EXPORT_SYMBOL(iio_allocate_device);
777
778void iio_free_device(struct iio_dev *dev)
779{
a9e39f9e
JC
780 if (dev) {
781 ida_simple_remove(&iio_ida, dev->id);
1aa04278 782 kfree(dev);
a9e39f9e 783 }
847ec80b
JC
784}
785EXPORT_SYMBOL(iio_free_device);
786
1aa04278 787/**
14555b14 788 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1aa04278
JC
789 **/
790static int iio_chrdev_open(struct inode *inode, struct file *filp)
791{
f8c6f4e9 792 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1aa04278 793 struct iio_dev, chrdev);
bb01443e
LPC
794
795 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
796 return -EBUSY;
797
f8c6f4e9 798 filp->private_data = indio_dev;
30eb82f0 799
79335140 800 return 0;
1aa04278
JC
801}
802
803/**
14555b14 804 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1aa04278
JC
805 **/
806static int iio_chrdev_release(struct inode *inode, struct file *filp)
807{
bb01443e
LPC
808 struct iio_dev *indio_dev = container_of(inode->i_cdev,
809 struct iio_dev, chrdev);
bb01443e 810 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1aa04278
JC
811 return 0;
812}
813
814/* Somewhat of a cross file organization violation - ioctls here are actually
815 * event related */
816static 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
14555b14
JC
831static const struct file_operations iio_buffer_fileops = {
832 .read = iio_buffer_read_first_n_outer_addr,
1aa04278
JC
833 .release = iio_chrdev_release,
834 .open = iio_chrdev_open,
14555b14 835 .poll = iio_buffer_poll_addr,
1aa04278
JC
836 .owner = THIS_MODULE,
837 .llseek = noop_llseek,
838 .unlocked_ioctl = iio_ioctl,
839 .compat_ioctl = iio_ioctl,
840};
841
0f1acee5
MH
842static const struct iio_buffer_setup_ops noop_ring_setup_ops;
843
f8c6f4e9 844int iio_device_register(struct iio_dev *indio_dev)
847ec80b
JC
845{
846 int ret;
847
1aa04278 848 /* configure elements for the chrdev */
f8c6f4e9 849 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
847ec80b 850
e553f182
MH
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 }
f8c6f4e9 857 ret = iio_device_register_sysfs(indio_dev);
847ec80b 858 if (ret) {
f8c6f4e9 859 dev_err(indio_dev->dev.parent,
847ec80b 860 "Failed to register sysfs interfaces\n");
e553f182 861 goto error_unreg_debugfs;
847ec80b 862 }
f8c6f4e9 863 ret = iio_device_register_eventset(indio_dev);
847ec80b 864 if (ret) {
f8c6f4e9 865 dev_err(indio_dev->dev.parent,
c849d253 866 "Failed to register event set\n");
847ec80b
JC
867 goto error_free_sysfs;
868 }
f8c6f4e9
JC
869 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
870 iio_device_register_trigger_consumer(indio_dev);
847ec80b 871
0f1acee5
MH
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
f8c6f4e9 876 ret = device_add(&indio_dev->dev);
26d25ae3
JC
877 if (ret < 0)
878 goto error_unreg_eventset;
f8c6f4e9
JC
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);
26d25ae3
JC
882 if (ret < 0)
883 goto error_del_device;
847ec80b
JC
884 return 0;
885
847ec80b 886error_del_device:
f8c6f4e9 887 device_del(&indio_dev->dev);
26d25ae3 888error_unreg_eventset:
f8c6f4e9 889 iio_device_unregister_eventset(indio_dev);
26d25ae3 890error_free_sysfs:
f8c6f4e9 891 iio_device_unregister_sysfs(indio_dev);
e553f182
MH
892error_unreg_debugfs:
893 iio_device_unregister_debugfs(indio_dev);
847ec80b
JC
894error_ret:
895 return ret;
896}
897EXPORT_SYMBOL(iio_device_register);
898
f8c6f4e9 899void iio_device_unregister(struct iio_dev *indio_dev)
847ec80b 900{
ac917a81
JC
901 mutex_lock(&indio_dev->info_exist_lock);
902 indio_dev->info = NULL;
903 mutex_unlock(&indio_dev->info_exist_lock);
f8c6f4e9 904 device_unregister(&indio_dev->dev);
847ec80b
JC
905}
906EXPORT_SYMBOL(iio_device_unregister);
847ec80b
JC
907subsys_initcall(iio_init);
908module_exit(iio_exit);
909
910MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
911MODULE_DESCRIPTION("Industrial I/O core");
912MODULE_LICENSE("GPL");
This page took 0.366438 seconds and 5 git commands to generate.