Staging: android: lowmemorykiller.c
[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
1e8dfcc6
JC
45static const char * const iio_data_type_name[] = {
46 [IIO_RAW] = "raw",
47 [IIO_PROCESSED] = "input",
48};
49
c6fc8062
JC
50static const char * const iio_direction[] = {
51 [0] = "in",
52 [1] = "out",
53};
54
ade7ef7b 55static const char * const iio_chan_type_name_spec[] = {
c6fc8062 56 [IIO_VOLTAGE] = "voltage",
faf290e8
MH
57 [IIO_CURRENT] = "current",
58 [IIO_POWER] = "power",
9bff02f8 59 [IIO_ACCEL] = "accel",
41ea040c 60 [IIO_ANGL_VEL] = "anglvel",
1d892719 61 [IIO_MAGN] = "magn",
9bff02f8
BF
62 [IIO_LIGHT] = "illuminance",
63 [IIO_INTENSITY] = "intensity",
f09f2c81 64 [IIO_PROXIMITY] = "proximity",
9bff02f8 65 [IIO_TEMP] = "temp",
1d892719
JC
66 [IIO_INCLI] = "incli",
67 [IIO_ROT] = "rot",
1d892719 68 [IIO_ANGL] = "angl",
9bff02f8 69 [IIO_TIMESTAMP] = "timestamp",
66dbe704 70 [IIO_CAPACITANCE] = "capacitance",
1d892719
JC
71};
72
330c6c57 73static const char * const iio_modifier_names[] = {
1d892719
JC
74 [IIO_MOD_X] = "x",
75 [IIO_MOD_Y] = "y",
76 [IIO_MOD_Z] = "z",
330c6c57
JC
77 [IIO_MOD_LIGHT_BOTH] = "both",
78 [IIO_MOD_LIGHT_IR] = "ir",
1d892719
JC
79};
80
81/* relies on pairs of these shared then separate */
82static const char * const iio_chan_info_postfix[] = {
c8a9f805
JC
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",
df94aba8
JC
91 [IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY]
92 = "filter_low_pass_3db_frequency",
1d892719
JC
93};
94
5fb21c82
JC
95const struct iio_chan_spec
96*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
97{
98 int i;
99
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;
104}
105
847ec80b
JC
106/* This turns up an awful lot */
107ssize_t iio_read_const_attr(struct device *dev,
108 struct device_attribute *attr,
109 char *buf)
110{
111 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
112}
113EXPORT_SYMBOL(iio_read_const_attr);
114
847ec80b
JC
115static int __init iio_init(void)
116{
117 int ret;
118
5aaaeba8
JC
119 /* Register sysfs bus */
120 ret = bus_register(&iio_bus_type);
847ec80b
JC
121 if (ret < 0) {
122 printk(KERN_ERR
5aaaeba8 123 "%s could not register bus type\n",
847ec80b
JC
124 __FILE__);
125 goto error_nothing;
126 }
127
9aa1a167
JC
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__);
5aaaeba8 132 goto error_unregister_bus_type;
9aa1a167 133 }
847ec80b 134
e553f182
MH
135 iio_debugfs_dentry = debugfs_create_dir("iio", NULL);
136
847ec80b
JC
137 return 0;
138
5aaaeba8
JC
139error_unregister_bus_type:
140 bus_unregister(&iio_bus_type);
847ec80b
JC
141error_nothing:
142 return ret;
143}
144
145static void __exit iio_exit(void)
146{
9aa1a167
JC
147 if (iio_devt)
148 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
5aaaeba8 149 bus_unregister(&iio_bus_type);
e553f182
MH
150 debugfs_remove(iio_debugfs_dentry);
151}
152
153#if defined(CONFIG_DEBUG_FS)
154static int iio_debugfs_open(struct inode *inode, struct file *file)
155{
156 if (inode->i_private)
157 file->private_data = inode->i_private;
158
159 return 0;
160}
161
162static ssize_t iio_debugfs_read_reg(struct file *file, char __user *userbuf,
163 size_t count, loff_t *ppos)
164{
165 struct iio_dev *indio_dev = file->private_data;
166 char buf[20];
167 unsigned val = 0;
168 ssize_t len;
169 int ret;
170
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__);
176
177 len = snprintf(buf, sizeof(buf), "0x%X\n", val);
178
179 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
180}
181
182static ssize_t iio_debugfs_write_reg(struct file *file,
183 const char __user *userbuf, size_t count, loff_t *ppos)
184{
185 struct iio_dev *indio_dev = file->private_data;
186 unsigned reg, val;
187 char buf[80];
188 int ret;
189
190 count = min_t(size_t, count, (sizeof(buf)-1));
191 if (copy_from_user(buf, userbuf, count))
192 return -EFAULT;
193
194 buf[count] = 0;
195
196 ret = sscanf(buf, "%i %i", &reg, &val);
197
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;
210 }
211 break;
212 default:
213 return -EINVAL;
214 }
215
216 return count;
217}
218
219static 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,
223};
224
225static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
226{
227 debugfs_remove_recursive(indio_dev->debugfs_dentry);
847ec80b
JC
228}
229
5f420b42
LPC
230static ssize_t iio_read_channel_ext_info(struct device *dev,
231 struct device_attribute *attr,
232 char *buf)
233{
234 struct iio_dev *indio_dev = dev_get_drvdata(dev);
235 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
236 const struct iio_chan_spec_ext_info *ext_info;
237
238 ext_info = &this_attr->c->ext_info[this_attr->address];
239
240 return ext_info->read(indio_dev, this_attr->c, buf);
241}
242
243static ssize_t iio_write_channel_ext_info(struct device *dev,
244 struct device_attribute *attr,
245 const char *buf,
246 size_t len)
247{
248 struct iio_dev *indio_dev = dev_get_drvdata(dev);
249 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
250 const struct iio_chan_spec_ext_info *ext_info;
251
252 ext_info = &this_attr->c->ext_info[this_attr->address];
253
254 return ext_info->write(indio_dev, this_attr->c, buf, len);
255}
256
e553f182
MH
257static int iio_device_register_debugfs(struct iio_dev *indio_dev)
258{
259 struct dentry *d;
260
261 if (indio_dev->info->debugfs_reg_access == NULL)
262 return 0;
263
264 if (IS_ERR(iio_debugfs_dentry))
265 return 0;
266
267 indio_dev->debugfs_dentry =
268 debugfs_create_dir(dev_name(&indio_dev->dev),
269 iio_debugfs_dentry);
270 if (IS_ERR(indio_dev->debugfs_dentry))
271 return PTR_ERR(indio_dev->debugfs_dentry);
272
273 if (indio_dev->debugfs_dentry == NULL) {
274 dev_warn(indio_dev->dev.parent,
275 "Failed to create debugfs directory\n");
276 return -EFAULT;
277 }
278
279 d = debugfs_create_file("direct_reg_access", 0644,
280 indio_dev->debugfs_dentry,
281 indio_dev, &iio_debugfs_reg_fops);
282 if (!d) {
283 iio_device_unregister_debugfs(indio_dev);
284 return -ENOMEM;
285 }
286
287 return 0;
288}
289#else
290static int iio_device_register_debugfs(struct iio_dev *indio_dev)
291{
292 return 0;
293}
294
295static void iio_device_unregister_debugfs(struct iio_dev *indio_dev)
296{
297}
298#endif /* CONFIG_DEBUG_FS */
299
1d892719
JC
300static ssize_t iio_read_channel_info(struct device *dev,
301 struct device_attribute *attr,
302 char *buf)
847ec80b 303{
1d892719
JC
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;
6fe8135f
JC
307 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
308 &val, &val2, this_attr->address);
1d892719
JC
309
310 if (ret < 0)
311 return ret;
847ec80b 312
1d892719
JC
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);
71646e2c
MH
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);
1d892719
JC
325 } else
326 return 0;
327}
328
329static ssize_t iio_write_channel_info(struct device *dev,
330 struct device_attribute *attr,
331 const char *buf,
332 size_t len)
333{
334 struct iio_dev *indio_dev = dev_get_drvdata(dev);
335 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
5c04af04 336 int ret, integer = 0, fract = 0, fract_mult = 100000;
1d892719
JC
337 bool integer_part = true, negative = false;
338
339 /* Assumes decimal - precision based on number of digits */
6fe8135f 340 if (!indio_dev->info->write_raw)
1d892719 341 return -EINVAL;
5c04af04
MH
342
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;
354 }
355
1d892719
JC
356 if (buf[0] == '-') {
357 negative = true;
358 buf++;
359 }
5c04af04 360
1d892719
JC
361 while (*buf) {
362 if ('0' <= *buf && *buf <= '9') {
363 if (integer_part)
364 integer = integer*10 + *buf - '0';
365 else {
5c04af04
MH
366 fract += fract_mult*(*buf - '0');
367 if (fract_mult == 1)
1d892719 368 break;
5c04af04 369 fract_mult /= 10;
1d892719
JC
370 }
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;
380 }
381 buf++;
382 }
383 if (negative) {
384 if (integer)
385 integer = -integer;
386 else
5c04af04 387 fract = -fract;
1d892719
JC
388 }
389
6fe8135f 390 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
5c04af04 391 integer, fract, this_attr->address);
1d892719
JC
392 if (ret)
393 return ret;
394
395 return len;
396}
397
df9c1c42 398static
1d892719
JC
399int __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)
410{
411 int ret;
412 char *name_format, *full_postfix;
413 sysfs_attr_init(&dev_attr->attr);
1d892719 414
ade7ef7b 415 /* Build up postfix of <extend_name>_<modifier>_postfix */
0403e0d6 416 if (chan->modified && !generic) {
ade7ef7b
JC
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);
436 }
437 if (full_postfix == NULL) {
438 ret = -ENOMEM;
439 goto error_ret;
440 }
1d892719 441
4abf6f8b 442 if (chan->differential) { /* Differential can not have modifier */
ade7ef7b
JC
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;
463 }
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);
484 }
1d892719
JC
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
512error_free_name_format:
513 kfree(name_format);
514error_free_full_postfix:
515 kfree(full_postfix);
516error_ret:
517 return ret;
518}
519
df9c1c42 520static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1d892719
JC
521{
522 kfree(dev_attr->attr.name);
523}
524
525int __iio_add_chan_devattr(const char *postfix,
1d892719
JC
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),
e614a54b 534 u64 mask,
1d892719
JC
535 bool generic,
536 struct device *dev,
537 struct list_head *attr_list)
538{
539 int ret;
540 struct iio_dev_attr *iio_attr, *t;
541
542 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
543 if (iio_attr == NULL) {
544 ret = -ENOMEM;
545 goto error_ret;
546 }
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;
562 }
1d892719
JC
563 list_add(&iio_attr->l, attr_list);
564
565 return 0;
566
567error_device_attr_deinit:
568 __iio_device_attr_deinit(&iio_attr->dev_attr);
569error_iio_dev_attr_free:
570 kfree(iio_attr);
571error_ret:
572 return ret;
573}
574
f8c6f4e9 575static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1d892719
JC
576 struct iio_chan_spec const *chan)
577{
26d25ae3 578 int ret, i, attrcount = 0;
5f420b42 579 const struct iio_chan_spec_ext_info *ext_info;
1d892719 580
1d892719
JC
581 if (chan->channel < 0)
582 return 0;
1e8dfcc6
JC
583
584 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
26d25ae3 585 chan,
1e8dfcc6 586 &iio_read_channel_info,
c6fc8062 587 (chan->output ?
1e8dfcc6
JC
588 &iio_write_channel_info : NULL),
589 0,
590 0,
f8c6f4e9
JC
591 &indio_dev->dev,
592 &indio_dev->channel_attr_list);
1d892719
JC
593 if (ret)
594 goto error_ret;
26d25ae3 595 attrcount++;
1d892719
JC
596
597 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
598 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
26d25ae3 599 chan,
1d892719
JC
600 &iio_read_channel_info,
601 &iio_write_channel_info,
c8a9f805 602 i/2,
1d892719 603 !(i%2),
f8c6f4e9
JC
604 &indio_dev->dev,
605 &indio_dev->channel_attr_list);
1d892719
JC
606 if (ret == -EBUSY && (i%2 == 0)) {
607 ret = 0;
608 continue;
609 }
610 if (ret < 0)
611 goto error_ret;
26d25ae3 612 attrcount++;
1d892719 613 }
5f420b42
LPC
614
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,
624 i,
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;
631
632 if (ret)
633 goto error_ret;
634
635 attrcount++;
636 }
637 }
638
26d25ae3 639 ret = attrcount;
1d892719
JC
640error_ret:
641 return ret;
642}
643
f8c6f4e9 644static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
1d892719
JC
645 struct iio_dev_attr *p)
646{
1d892719
JC
647 kfree(p->dev_attr.attr.name);
648 kfree(p);
649}
650
1b732888
JC
651static ssize_t iio_show_dev_name(struct device *dev,
652 struct device_attribute *attr,
653 char *buf)
654{
655 struct iio_dev *indio_dev = dev_get_drvdata(dev);
656 return sprintf(buf, "%s\n", indio_dev->name);
657}
658
659static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
660
f8c6f4e9 661static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1d892719 662{
26d25ae3 663 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1d892719 664 struct iio_dev_attr *p, *n;
26d25ae3 665 struct attribute **attr;
1d892719 666
26d25ae3 667 /* First count elements in any existing group */
f8c6f4e9
JC
668 if (indio_dev->info->attrs) {
669 attr = indio_dev->info->attrs->attrs;
26d25ae3
JC
670 while (*attr++ != NULL)
671 attrcount_orig++;
847ec80b 672 }
26d25ae3 673 attrcount = attrcount_orig;
1d892719
JC
674 /*
675 * New channel registration method - relies on the fact a group does
4abf6f8b 676 * not need to be initialized if it is name is NULL.
1d892719 677 */
f8c6f4e9
JC
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
1d892719
JC
683 ->channels[i]);
684 if (ret < 0)
685 goto error_clear_attrs;
26d25ae3 686 attrcount += ret;
1d892719 687 }
26d25ae3 688
f8c6f4e9 689 if (indio_dev->name)
26d25ae3
JC
690 attrcount++;
691
d83fb184
TM
692 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
693 sizeof(indio_dev->chan_attr_group.attrs[0]),
694 GFP_KERNEL);
f8c6f4e9 695 if (indio_dev->chan_attr_group.attrs == NULL) {
26d25ae3
JC
696 ret = -ENOMEM;
697 goto error_clear_attrs;
1b732888 698 }
26d25ae3 699 /* Copy across original attributes */
f8c6f4e9
JC
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])
26d25ae3
JC
704 *attrcount_orig);
705 attrn = attrcount_orig;
706 /* Add all elements from the list. */
f8c6f4e9
JC
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;
26d25ae3 711
f8c6f4e9
JC
712 indio_dev->groups[indio_dev->groupcounter++] =
713 &indio_dev->chan_attr_group;
26d25ae3 714
1d892719 715 return 0;
1b732888 716
1d892719
JC
717error_clear_attrs:
718 list_for_each_entry_safe(p, n,
f8c6f4e9 719 &indio_dev->channel_attr_list, l) {
1d892719 720 list_del(&p->l);
f8c6f4e9 721 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 722 }
1d892719 723
26d25ae3 724 return ret;
847ec80b
JC
725}
726
f8c6f4e9 727static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
847ec80b 728{
1d892719
JC
729
730 struct iio_dev_attr *p, *n;
26d25ae3 731
f8c6f4e9 732 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
1d892719 733 list_del(&p->l);
f8c6f4e9 734 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 735 }
f8c6f4e9 736 kfree(indio_dev->chan_attr_group.attrs);
847ec80b
JC
737}
738
847ec80b
JC
739static void iio_dev_release(struct device *device)
740{
f8c6f4e9
JC
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);
e553f182 747 iio_device_unregister_debugfs(indio_dev);
847ec80b
JC
748}
749
750static struct device_type iio_dev_type = {
751 .name = "iio_device",
752 .release = iio_dev_release,
753};
754
6f7c8ee5 755struct iio_dev *iio_allocate_device(int sizeof_priv)
847ec80b 756{
6f7c8ee5
JC
757 struct iio_dev *dev;
758 size_t alloc_size;
759
760 alloc_size = sizeof(struct iio_dev);
761 if (sizeof_priv) {
762 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
763 alloc_size += sizeof_priv;
764 }
765 /* ensure 32-byte alignment of whole construct ? */
766 alloc_size += IIO_ALIGN - 1;
767
768 dev = kzalloc(alloc_size, GFP_KERNEL);
847ec80b
JC
769
770 if (dev) {
26d25ae3 771 dev->dev.groups = dev->groups;
847ec80b 772 dev->dev.type = &iio_dev_type;
5aaaeba8 773 dev->dev.bus = &iio_bus_type;
847ec80b
JC
774 device_initialize(&dev->dev);
775 dev_set_drvdata(&dev->dev, (void *)dev);
776 mutex_init(&dev->mlock);
ac917a81 777 mutex_init(&dev->info_exist_lock);
a9e39f9e
JC
778
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;
785 }
786 dev_set_name(&dev->dev, "iio:device%d", dev->id);
847ec80b
JC
787 }
788
789 return dev;
790}
791EXPORT_SYMBOL(iio_allocate_device);
792
793void iio_free_device(struct iio_dev *dev)
794{
a9e39f9e
JC
795 if (dev) {
796 ida_simple_remove(&iio_ida, dev->id);
1aa04278 797 kfree(dev);
a9e39f9e 798 }
847ec80b
JC
799}
800EXPORT_SYMBOL(iio_free_device);
801
1aa04278 802/**
14555b14 803 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1aa04278
JC
804 **/
805static int iio_chrdev_open(struct inode *inode, struct file *filp)
806{
f8c6f4e9 807 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1aa04278 808 struct iio_dev, chrdev);
bb01443e
LPC
809
810 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
811 return -EBUSY;
812
f8c6f4e9 813 filp->private_data = indio_dev;
30eb82f0 814
79335140 815 return 0;
1aa04278
JC
816}
817
818/**
14555b14 819 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1aa04278
JC
820 **/
821static int iio_chrdev_release(struct inode *inode, struct file *filp)
822{
bb01443e
LPC
823 struct iio_dev *indio_dev = container_of(inode->i_cdev,
824 struct iio_dev, chrdev);
bb01443e 825 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1aa04278
JC
826 return 0;
827}
828
829/* Somewhat of a cross file organization violation - ioctls here are actually
830 * event related */
831static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
832{
833 struct iio_dev *indio_dev = filp->private_data;
834 int __user *ip = (int __user *)arg;
835 int fd;
836
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;
842 }
843 return -EINVAL;
844}
845
14555b14
JC
846static const struct file_operations iio_buffer_fileops = {
847 .read = iio_buffer_read_first_n_outer_addr,
1aa04278
JC
848 .release = iio_chrdev_release,
849 .open = iio_chrdev_open,
14555b14 850 .poll = iio_buffer_poll_addr,
1aa04278
JC
851 .owner = THIS_MODULE,
852 .llseek = noop_llseek,
853 .unlocked_ioctl = iio_ioctl,
854 .compat_ioctl = iio_ioctl,
855};
856
0f1acee5
MH
857static const struct iio_buffer_setup_ops noop_ring_setup_ops;
858
f8c6f4e9 859int iio_device_register(struct iio_dev *indio_dev)
847ec80b
JC
860{
861 int ret;
862
1aa04278 863 /* configure elements for the chrdev */
f8c6f4e9 864 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
847ec80b 865
e553f182
MH
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;
871 }
f8c6f4e9 872 ret = iio_device_register_sysfs(indio_dev);
847ec80b 873 if (ret) {
f8c6f4e9 874 dev_err(indio_dev->dev.parent,
847ec80b 875 "Failed to register sysfs interfaces\n");
e553f182 876 goto error_unreg_debugfs;
847ec80b 877 }
f8c6f4e9 878 ret = iio_device_register_eventset(indio_dev);
847ec80b 879 if (ret) {
f8c6f4e9 880 dev_err(indio_dev->dev.parent,
c849d253 881 "Failed to register event set\n");
847ec80b
JC
882 goto error_free_sysfs;
883 }
f8c6f4e9
JC
884 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
885 iio_device_register_trigger_consumer(indio_dev);
847ec80b 886
0f1acee5
MH
887 if ((indio_dev->modes & INDIO_ALL_BUFFER_MODES) &&
888 indio_dev->setup_ops == NULL)
889 indio_dev->setup_ops = &noop_ring_setup_ops;
890
f8c6f4e9 891 ret = device_add(&indio_dev->dev);
26d25ae3
JC
892 if (ret < 0)
893 goto error_unreg_eventset;
f8c6f4e9
JC
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);
26d25ae3
JC
897 if (ret < 0)
898 goto error_del_device;
847ec80b
JC
899 return 0;
900
847ec80b 901error_del_device:
f8c6f4e9 902 device_del(&indio_dev->dev);
26d25ae3 903error_unreg_eventset:
f8c6f4e9 904 iio_device_unregister_eventset(indio_dev);
26d25ae3 905error_free_sysfs:
f8c6f4e9 906 iio_device_unregister_sysfs(indio_dev);
e553f182
MH
907error_unreg_debugfs:
908 iio_device_unregister_debugfs(indio_dev);
847ec80b
JC
909error_ret:
910 return ret;
911}
912EXPORT_SYMBOL(iio_device_register);
913
f8c6f4e9 914void iio_device_unregister(struct iio_dev *indio_dev)
847ec80b 915{
ac917a81
JC
916 mutex_lock(&indio_dev->info_exist_lock);
917 indio_dev->info = NULL;
918 mutex_unlock(&indio_dev->info_exist_lock);
f8c6f4e9 919 device_unregister(&indio_dev->dev);
847ec80b
JC
920}
921EXPORT_SYMBOL(iio_device_unregister);
847ec80b
JC
922subsys_initcall(iio_init);
923module_exit(iio_exit);
924
925MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
926MODULE_DESCRIPTION("Industrial I/O core");
927MODULE_LICENSE("GPL");
This page took 0.502855 seconds and 5 git commands to generate.