staging:iio: Convert remaining drivers to module_spi_driver
[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>
847ec80b 25#include "iio.h"
df9c1c42 26#include "iio_core.h"
6aea1c36 27#include "iio_core_trigger.h"
9dd1cb30 28#include "sysfs.h"
af5046af 29#include "events.h"
9dd1cb30 30
47c24fdd 31/* IDA to assign each registered device a unique id*/
b156cf70 32static DEFINE_IDA(iio_ida);
847ec80b 33
f625cb97 34static dev_t iio_devt;
847ec80b
JC
35
36#define IIO_DEV_MAX 256
5aaaeba8 37struct bus_type iio_bus_type = {
847ec80b 38 .name = "iio",
847ec80b 39};
5aaaeba8 40EXPORT_SYMBOL(iio_bus_type);
847ec80b 41
1e8dfcc6
JC
42static const char * const iio_data_type_name[] = {
43 [IIO_RAW] = "raw",
44 [IIO_PROCESSED] = "input",
45};
46
c6fc8062
JC
47static const char * const iio_direction[] = {
48 [0] = "in",
49 [1] = "out",
50};
51
ade7ef7b 52static const char * const iio_chan_type_name_spec[] = {
c6fc8062 53 [IIO_VOLTAGE] = "voltage",
faf290e8
MH
54 [IIO_CURRENT] = "current",
55 [IIO_POWER] = "power",
9bff02f8 56 [IIO_ACCEL] = "accel",
41ea040c 57 [IIO_ANGL_VEL] = "anglvel",
1d892719 58 [IIO_MAGN] = "magn",
9bff02f8
BF
59 [IIO_LIGHT] = "illuminance",
60 [IIO_INTENSITY] = "intensity",
f09f2c81 61 [IIO_PROXIMITY] = "proximity",
9bff02f8 62 [IIO_TEMP] = "temp",
1d892719
JC
63 [IIO_INCLI] = "incli",
64 [IIO_ROT] = "rot",
1d892719 65 [IIO_ANGL] = "angl",
9bff02f8 66 [IIO_TIMESTAMP] = "timestamp",
66dbe704 67 [IIO_CAPACITANCE] = "capacitance",
1d892719
JC
68};
69
330c6c57 70static const char * const iio_modifier_names[] = {
1d892719
JC
71 [IIO_MOD_X] = "x",
72 [IIO_MOD_Y] = "y",
73 [IIO_MOD_Z] = "z",
330c6c57
JC
74 [IIO_MOD_LIGHT_BOTH] = "both",
75 [IIO_MOD_LIGHT_IR] = "ir",
1d892719
JC
76};
77
78/* relies on pairs of these shared then separate */
79static const char * const iio_chan_info_postfix[] = {
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",
1d892719
JC
90};
91
5fb21c82
JC
92const struct iio_chan_spec
93*iio_find_channel_from_si(struct iio_dev *indio_dev, int si)
94{
95 int i;
96
97 for (i = 0; i < indio_dev->num_channels; i++)
98 if (indio_dev->channels[i].scan_index == si)
99 return &indio_dev->channels[i];
100 return NULL;
101}
102
847ec80b
JC
103/* This turns up an awful lot */
104ssize_t iio_read_const_attr(struct device *dev,
105 struct device_attribute *attr,
106 char *buf)
107{
108 return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string);
109}
110EXPORT_SYMBOL(iio_read_const_attr);
111
847ec80b
JC
112static int __init iio_init(void)
113{
114 int ret;
115
5aaaeba8
JC
116 /* Register sysfs bus */
117 ret = bus_register(&iio_bus_type);
847ec80b
JC
118 if (ret < 0) {
119 printk(KERN_ERR
5aaaeba8 120 "%s could not register bus type\n",
847ec80b
JC
121 __FILE__);
122 goto error_nothing;
123 }
124
9aa1a167
JC
125 ret = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio");
126 if (ret < 0) {
127 printk(KERN_ERR "%s: failed to allocate char dev region\n",
128 __FILE__);
5aaaeba8 129 goto error_unregister_bus_type;
9aa1a167 130 }
847ec80b
JC
131
132 return 0;
133
5aaaeba8
JC
134error_unregister_bus_type:
135 bus_unregister(&iio_bus_type);
847ec80b
JC
136error_nothing:
137 return ret;
138}
139
140static void __exit iio_exit(void)
141{
9aa1a167
JC
142 if (iio_devt)
143 unregister_chrdev_region(iio_devt, IIO_DEV_MAX);
5aaaeba8 144 bus_unregister(&iio_bus_type);
847ec80b
JC
145}
146
1d892719
JC
147static ssize_t iio_read_channel_info(struct device *dev,
148 struct device_attribute *attr,
149 char *buf)
847ec80b 150{
1d892719
JC
151 struct iio_dev *indio_dev = dev_get_drvdata(dev);
152 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
153 int val, val2;
6fe8135f
JC
154 int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
155 &val, &val2, this_attr->address);
1d892719
JC
156
157 if (ret < 0)
158 return ret;
847ec80b 159
1d892719
JC
160 if (ret == IIO_VAL_INT)
161 return sprintf(buf, "%d\n", val);
162 else if (ret == IIO_VAL_INT_PLUS_MICRO) {
163 if (val2 < 0)
164 return sprintf(buf, "-%d.%06u\n", val, -val2);
165 else
166 return sprintf(buf, "%d.%06u\n", val, val2);
71646e2c
MH
167 } else if (ret == IIO_VAL_INT_PLUS_NANO) {
168 if (val2 < 0)
169 return sprintf(buf, "-%d.%09u\n", val, -val2);
170 else
171 return sprintf(buf, "%d.%09u\n", val, val2);
1d892719
JC
172 } else
173 return 0;
174}
175
176static ssize_t iio_write_channel_info(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf,
179 size_t len)
180{
181 struct iio_dev *indio_dev = dev_get_drvdata(dev);
182 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
5c04af04 183 int ret, integer = 0, fract = 0, fract_mult = 100000;
1d892719
JC
184 bool integer_part = true, negative = false;
185
186 /* Assumes decimal - precision based on number of digits */
6fe8135f 187 if (!indio_dev->info->write_raw)
1d892719 188 return -EINVAL;
5c04af04
MH
189
190 if (indio_dev->info->write_raw_get_fmt)
191 switch (indio_dev->info->write_raw_get_fmt(indio_dev,
192 this_attr->c, this_attr->address)) {
193 case IIO_VAL_INT_PLUS_MICRO:
194 fract_mult = 100000;
195 break;
196 case IIO_VAL_INT_PLUS_NANO:
197 fract_mult = 100000000;
198 break;
199 default:
200 return -EINVAL;
201 }
202
1d892719
JC
203 if (buf[0] == '-') {
204 negative = true;
205 buf++;
206 }
5c04af04 207
1d892719
JC
208 while (*buf) {
209 if ('0' <= *buf && *buf <= '9') {
210 if (integer_part)
211 integer = integer*10 + *buf - '0';
212 else {
5c04af04
MH
213 fract += fract_mult*(*buf - '0');
214 if (fract_mult == 1)
1d892719 215 break;
5c04af04 216 fract_mult /= 10;
1d892719
JC
217 }
218 } else if (*buf == '\n') {
219 if (*(buf + 1) == '\0')
220 break;
221 else
222 return -EINVAL;
223 } else if (*buf == '.') {
224 integer_part = false;
225 } else {
226 return -EINVAL;
227 }
228 buf++;
229 }
230 if (negative) {
231 if (integer)
232 integer = -integer;
233 else
5c04af04 234 fract = -fract;
1d892719
JC
235 }
236
6fe8135f 237 ret = indio_dev->info->write_raw(indio_dev, this_attr->c,
5c04af04 238 integer, fract, this_attr->address);
1d892719
JC
239 if (ret)
240 return ret;
241
242 return len;
243}
244
df9c1c42 245static
1d892719
JC
246int __iio_device_attr_init(struct device_attribute *dev_attr,
247 const char *postfix,
248 struct iio_chan_spec const *chan,
249 ssize_t (*readfunc)(struct device *dev,
250 struct device_attribute *attr,
251 char *buf),
252 ssize_t (*writefunc)(struct device *dev,
253 struct device_attribute *attr,
254 const char *buf,
255 size_t len),
256 bool generic)
257{
258 int ret;
259 char *name_format, *full_postfix;
260 sysfs_attr_init(&dev_attr->attr);
1d892719 261
ade7ef7b 262 /* Build up postfix of <extend_name>_<modifier>_postfix */
0403e0d6 263 if (chan->modified && !generic) {
ade7ef7b
JC
264 if (chan->extend_name)
265 full_postfix = kasprintf(GFP_KERNEL, "%s_%s_%s",
266 iio_modifier_names[chan
267 ->channel2],
268 chan->extend_name,
269 postfix);
270 else
271 full_postfix = kasprintf(GFP_KERNEL, "%s_%s",
272 iio_modifier_names[chan
273 ->channel2],
274 postfix);
275 } else {
276 if (chan->extend_name == NULL)
277 full_postfix = kstrdup(postfix, GFP_KERNEL);
278 else
279 full_postfix = kasprintf(GFP_KERNEL,
280 "%s_%s",
281 chan->extend_name,
282 postfix);
283 }
284 if (full_postfix == NULL) {
285 ret = -ENOMEM;
286 goto error_ret;
287 }
1d892719 288
ade7ef7b
JC
289 if (chan->differential) { /* Differential can not have modifier */
290 if (generic)
291 name_format
292 = kasprintf(GFP_KERNEL, "%s_%s-%s_%s",
293 iio_direction[chan->output],
294 iio_chan_type_name_spec[chan->type],
295 iio_chan_type_name_spec[chan->type],
296 full_postfix);
297 else if (chan->indexed)
298 name_format
299 = kasprintf(GFP_KERNEL, "%s_%s%d-%s%d_%s",
300 iio_direction[chan->output],
301 iio_chan_type_name_spec[chan->type],
302 chan->channel,
303 iio_chan_type_name_spec[chan->type],
304 chan->channel2,
305 full_postfix);
306 else {
307 WARN_ON("Differential channels must be indexed\n");
308 ret = -EINVAL;
309 goto error_free_full_postfix;
310 }
311 } else { /* Single ended */
312 if (generic)
313 name_format
314 = kasprintf(GFP_KERNEL, "%s_%s_%s",
315 iio_direction[chan->output],
316 iio_chan_type_name_spec[chan->type],
317 full_postfix);
318 else if (chan->indexed)
319 name_format
320 = kasprintf(GFP_KERNEL, "%s_%s%d_%s",
321 iio_direction[chan->output],
322 iio_chan_type_name_spec[chan->type],
323 chan->channel,
324 full_postfix);
325 else
326 name_format
327 = kasprintf(GFP_KERNEL, "%s_%s_%s",
328 iio_direction[chan->output],
329 iio_chan_type_name_spec[chan->type],
330 full_postfix);
331 }
1d892719
JC
332 if (name_format == NULL) {
333 ret = -ENOMEM;
334 goto error_free_full_postfix;
335 }
336 dev_attr->attr.name = kasprintf(GFP_KERNEL,
337 name_format,
338 chan->channel,
339 chan->channel2);
340 if (dev_attr->attr.name == NULL) {
341 ret = -ENOMEM;
342 goto error_free_name_format;
343 }
344
345 if (readfunc) {
346 dev_attr->attr.mode |= S_IRUGO;
347 dev_attr->show = readfunc;
348 }
349
350 if (writefunc) {
351 dev_attr->attr.mode |= S_IWUSR;
352 dev_attr->store = writefunc;
353 }
354 kfree(name_format);
355 kfree(full_postfix);
356
357 return 0;
358
359error_free_name_format:
360 kfree(name_format);
361error_free_full_postfix:
362 kfree(full_postfix);
363error_ret:
364 return ret;
365}
366
df9c1c42 367static void __iio_device_attr_deinit(struct device_attribute *dev_attr)
1d892719
JC
368{
369 kfree(dev_attr->attr.name);
370}
371
372int __iio_add_chan_devattr(const char *postfix,
1d892719
JC
373 struct iio_chan_spec const *chan,
374 ssize_t (*readfunc)(struct device *dev,
375 struct device_attribute *attr,
376 char *buf),
377 ssize_t (*writefunc)(struct device *dev,
378 struct device_attribute *attr,
379 const char *buf,
380 size_t len),
e614a54b 381 u64 mask,
1d892719
JC
382 bool generic,
383 struct device *dev,
384 struct list_head *attr_list)
385{
386 int ret;
387 struct iio_dev_attr *iio_attr, *t;
388
389 iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL);
390 if (iio_attr == NULL) {
391 ret = -ENOMEM;
392 goto error_ret;
393 }
394 ret = __iio_device_attr_init(&iio_attr->dev_attr,
395 postfix, chan,
396 readfunc, writefunc, generic);
397 if (ret)
398 goto error_iio_dev_attr_free;
399 iio_attr->c = chan;
400 iio_attr->address = mask;
401 list_for_each_entry(t, attr_list, l)
402 if (strcmp(t->dev_attr.attr.name,
403 iio_attr->dev_attr.attr.name) == 0) {
404 if (!generic)
405 dev_err(dev, "tried to double register : %s\n",
406 t->dev_attr.attr.name);
407 ret = -EBUSY;
408 goto error_device_attr_deinit;
409 }
1d892719
JC
410 list_add(&iio_attr->l, attr_list);
411
412 return 0;
413
414error_device_attr_deinit:
415 __iio_device_attr_deinit(&iio_attr->dev_attr);
416error_iio_dev_attr_free:
417 kfree(iio_attr);
418error_ret:
419 return ret;
420}
421
f8c6f4e9 422static int iio_device_add_channel_sysfs(struct iio_dev *indio_dev,
1d892719
JC
423 struct iio_chan_spec const *chan)
424{
26d25ae3 425 int ret, i, attrcount = 0;
1d892719 426
1d892719
JC
427 if (chan->channel < 0)
428 return 0;
1e8dfcc6
JC
429
430 ret = __iio_add_chan_devattr(iio_data_type_name[chan->processed_val],
26d25ae3 431 chan,
1e8dfcc6 432 &iio_read_channel_info,
c6fc8062 433 (chan->output ?
1e8dfcc6
JC
434 &iio_write_channel_info : NULL),
435 0,
436 0,
f8c6f4e9
JC
437 &indio_dev->dev,
438 &indio_dev->channel_attr_list);
1d892719
JC
439 if (ret)
440 goto error_ret;
26d25ae3 441 attrcount++;
1d892719
JC
442
443 for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) {
444 ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2],
26d25ae3 445 chan,
1d892719
JC
446 &iio_read_channel_info,
447 &iio_write_channel_info,
c8a9f805 448 i/2,
1d892719 449 !(i%2),
f8c6f4e9
JC
450 &indio_dev->dev,
451 &indio_dev->channel_attr_list);
1d892719
JC
452 if (ret == -EBUSY && (i%2 == 0)) {
453 ret = 0;
454 continue;
455 }
456 if (ret < 0)
457 goto error_ret;
26d25ae3 458 attrcount++;
1d892719 459 }
26d25ae3 460 ret = attrcount;
1d892719
JC
461error_ret:
462 return ret;
463}
464
f8c6f4e9 465static void iio_device_remove_and_free_read_attr(struct iio_dev *indio_dev,
1d892719
JC
466 struct iio_dev_attr *p)
467{
1d892719
JC
468 kfree(p->dev_attr.attr.name);
469 kfree(p);
470}
471
1b732888
JC
472static ssize_t iio_show_dev_name(struct device *dev,
473 struct device_attribute *attr,
474 char *buf)
475{
476 struct iio_dev *indio_dev = dev_get_drvdata(dev);
477 return sprintf(buf, "%s\n", indio_dev->name);
478}
479
480static DEVICE_ATTR(name, S_IRUGO, iio_show_dev_name, NULL);
481
f8c6f4e9 482static int iio_device_register_sysfs(struct iio_dev *indio_dev)
1d892719 483{
26d25ae3 484 int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
1d892719 485 struct iio_dev_attr *p, *n;
26d25ae3 486 struct attribute **attr;
1d892719 487
26d25ae3 488 /* First count elements in any existing group */
f8c6f4e9
JC
489 if (indio_dev->info->attrs) {
490 attr = indio_dev->info->attrs->attrs;
26d25ae3
JC
491 while (*attr++ != NULL)
492 attrcount_orig++;
847ec80b 493 }
26d25ae3 494 attrcount = attrcount_orig;
1d892719
JC
495 /*
496 * New channel registration method - relies on the fact a group does
497 * not need to be initialized if it is name is NULL.
498 */
f8c6f4e9
JC
499 INIT_LIST_HEAD(&indio_dev->channel_attr_list);
500 if (indio_dev->channels)
501 for (i = 0; i < indio_dev->num_channels; i++) {
502 ret = iio_device_add_channel_sysfs(indio_dev,
503 &indio_dev
1d892719
JC
504 ->channels[i]);
505 if (ret < 0)
506 goto error_clear_attrs;
26d25ae3 507 attrcount += ret;
1d892719 508 }
26d25ae3 509
f8c6f4e9 510 if (indio_dev->name)
26d25ae3
JC
511 attrcount++;
512
d83fb184
TM
513 indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
514 sizeof(indio_dev->chan_attr_group.attrs[0]),
515 GFP_KERNEL);
f8c6f4e9 516 if (indio_dev->chan_attr_group.attrs == NULL) {
26d25ae3
JC
517 ret = -ENOMEM;
518 goto error_clear_attrs;
1b732888 519 }
26d25ae3 520 /* Copy across original attributes */
f8c6f4e9
JC
521 if (indio_dev->info->attrs)
522 memcpy(indio_dev->chan_attr_group.attrs,
523 indio_dev->info->attrs->attrs,
524 sizeof(indio_dev->chan_attr_group.attrs[0])
26d25ae3
JC
525 *attrcount_orig);
526 attrn = attrcount_orig;
527 /* Add all elements from the list. */
f8c6f4e9
JC
528 list_for_each_entry(p, &indio_dev->channel_attr_list, l)
529 indio_dev->chan_attr_group.attrs[attrn++] = &p->dev_attr.attr;
530 if (indio_dev->name)
531 indio_dev->chan_attr_group.attrs[attrn++] = &dev_attr_name.attr;
26d25ae3 532
f8c6f4e9
JC
533 indio_dev->groups[indio_dev->groupcounter++] =
534 &indio_dev->chan_attr_group;
26d25ae3 535
1d892719 536 return 0;
1b732888 537
1d892719
JC
538error_clear_attrs:
539 list_for_each_entry_safe(p, n,
f8c6f4e9 540 &indio_dev->channel_attr_list, l) {
1d892719 541 list_del(&p->l);
f8c6f4e9 542 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 543 }
1d892719 544
26d25ae3 545 return ret;
847ec80b
JC
546}
547
f8c6f4e9 548static void iio_device_unregister_sysfs(struct iio_dev *indio_dev)
847ec80b 549{
1d892719
JC
550
551 struct iio_dev_attr *p, *n;
26d25ae3 552
f8c6f4e9 553 list_for_each_entry_safe(p, n, &indio_dev->channel_attr_list, l) {
1d892719 554 list_del(&p->l);
f8c6f4e9 555 iio_device_remove_and_free_read_attr(indio_dev, p);
1d892719 556 }
f8c6f4e9 557 kfree(indio_dev->chan_attr_group.attrs);
847ec80b
JC
558}
559
847ec80b
JC
560static void iio_dev_release(struct device *device)
561{
f8c6f4e9
JC
562 struct iio_dev *indio_dev = container_of(device, struct iio_dev, dev);
563 cdev_del(&indio_dev->chrdev);
564 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
565 iio_device_unregister_trigger_consumer(indio_dev);
566 iio_device_unregister_eventset(indio_dev);
567 iio_device_unregister_sysfs(indio_dev);
847ec80b
JC
568}
569
570static struct device_type iio_dev_type = {
571 .name = "iio_device",
572 .release = iio_dev_release,
573};
574
6f7c8ee5 575struct iio_dev *iio_allocate_device(int sizeof_priv)
847ec80b 576{
6f7c8ee5
JC
577 struct iio_dev *dev;
578 size_t alloc_size;
579
580 alloc_size = sizeof(struct iio_dev);
581 if (sizeof_priv) {
582 alloc_size = ALIGN(alloc_size, IIO_ALIGN);
583 alloc_size += sizeof_priv;
584 }
585 /* ensure 32-byte alignment of whole construct ? */
586 alloc_size += IIO_ALIGN - 1;
587
588 dev = kzalloc(alloc_size, GFP_KERNEL);
847ec80b
JC
589
590 if (dev) {
26d25ae3 591 dev->dev.groups = dev->groups;
847ec80b 592 dev->dev.type = &iio_dev_type;
5aaaeba8 593 dev->dev.bus = &iio_bus_type;
847ec80b
JC
594 device_initialize(&dev->dev);
595 dev_set_drvdata(&dev->dev, (void *)dev);
596 mutex_init(&dev->mlock);
ac917a81 597 mutex_init(&dev->info_exist_lock);
a9e39f9e
JC
598
599 dev->id = ida_simple_get(&iio_ida, 0, 0, GFP_KERNEL);
600 if (dev->id < 0) {
601 /* cannot use a dev_err as the name isn't available */
602 printk(KERN_ERR "Failed to get id\n");
603 kfree(dev);
604 return NULL;
605 }
606 dev_set_name(&dev->dev, "iio:device%d", dev->id);
847ec80b
JC
607 }
608
609 return dev;
610}
611EXPORT_SYMBOL(iio_allocate_device);
612
613void iio_free_device(struct iio_dev *dev)
614{
a9e39f9e
JC
615 if (dev) {
616 ida_simple_remove(&iio_ida, dev->id);
1aa04278 617 kfree(dev);
a9e39f9e 618 }
847ec80b
JC
619}
620EXPORT_SYMBOL(iio_free_device);
621
1aa04278 622/**
14555b14 623 * iio_chrdev_open() - chrdev file open for buffer access and ioctls
1aa04278
JC
624 **/
625static int iio_chrdev_open(struct inode *inode, struct file *filp)
626{
f8c6f4e9 627 struct iio_dev *indio_dev = container_of(inode->i_cdev,
1aa04278 628 struct iio_dev, chrdev);
bb01443e
LPC
629
630 if (test_and_set_bit(IIO_BUSY_BIT_POS, &indio_dev->flags))
631 return -EBUSY;
632
f8c6f4e9 633 filp->private_data = indio_dev;
30eb82f0 634
79335140 635 return 0;
1aa04278
JC
636}
637
638/**
14555b14 639 * iio_chrdev_release() - chrdev file close buffer access and ioctls
1aa04278
JC
640 **/
641static int iio_chrdev_release(struct inode *inode, struct file *filp)
642{
bb01443e
LPC
643 struct iio_dev *indio_dev = container_of(inode->i_cdev,
644 struct iio_dev, chrdev);
bb01443e 645 clear_bit(IIO_BUSY_BIT_POS, &indio_dev->flags);
1aa04278
JC
646 return 0;
647}
648
649/* Somewhat of a cross file organization violation - ioctls here are actually
650 * event related */
651static long iio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
652{
653 struct iio_dev *indio_dev = filp->private_data;
654 int __user *ip = (int __user *)arg;
655 int fd;
656
657 if (cmd == IIO_GET_EVENT_FD_IOCTL) {
658 fd = iio_event_getfd(indio_dev);
659 if (copy_to_user(ip, &fd, sizeof(fd)))
660 return -EFAULT;
661 return 0;
662 }
663 return -EINVAL;
664}
665
14555b14
JC
666static const struct file_operations iio_buffer_fileops = {
667 .read = iio_buffer_read_first_n_outer_addr,
1aa04278
JC
668 .release = iio_chrdev_release,
669 .open = iio_chrdev_open,
14555b14 670 .poll = iio_buffer_poll_addr,
1aa04278
JC
671 .owner = THIS_MODULE,
672 .llseek = noop_llseek,
673 .unlocked_ioctl = iio_ioctl,
674 .compat_ioctl = iio_ioctl,
675};
676
f8c6f4e9 677int iio_device_register(struct iio_dev *indio_dev)
847ec80b
JC
678{
679 int ret;
680
1aa04278 681 /* configure elements for the chrdev */
f8c6f4e9 682 indio_dev->dev.devt = MKDEV(MAJOR(iio_devt), indio_dev->id);
847ec80b 683
f8c6f4e9 684 ret = iio_device_register_sysfs(indio_dev);
847ec80b 685 if (ret) {
f8c6f4e9 686 dev_err(indio_dev->dev.parent,
847ec80b 687 "Failed to register sysfs interfaces\n");
a9e39f9e 688 goto error_ret;
847ec80b 689 }
f8c6f4e9 690 ret = iio_device_register_eventset(indio_dev);
847ec80b 691 if (ret) {
f8c6f4e9 692 dev_err(indio_dev->dev.parent,
c849d253 693 "Failed to register event set\n");
847ec80b
JC
694 goto error_free_sysfs;
695 }
f8c6f4e9
JC
696 if (indio_dev->modes & INDIO_BUFFER_TRIGGERED)
697 iio_device_register_trigger_consumer(indio_dev);
847ec80b 698
f8c6f4e9 699 ret = device_add(&indio_dev->dev);
26d25ae3
JC
700 if (ret < 0)
701 goto error_unreg_eventset;
f8c6f4e9
JC
702 cdev_init(&indio_dev->chrdev, &iio_buffer_fileops);
703 indio_dev->chrdev.owner = indio_dev->info->driver_module;
704 ret = cdev_add(&indio_dev->chrdev, indio_dev->dev.devt, 1);
26d25ae3
JC
705 if (ret < 0)
706 goto error_del_device;
847ec80b
JC
707 return 0;
708
847ec80b 709error_del_device:
f8c6f4e9 710 device_del(&indio_dev->dev);
26d25ae3 711error_unreg_eventset:
f8c6f4e9 712 iio_device_unregister_eventset(indio_dev);
26d25ae3 713error_free_sysfs:
f8c6f4e9 714 iio_device_unregister_sysfs(indio_dev);
847ec80b
JC
715error_ret:
716 return ret;
717}
718EXPORT_SYMBOL(iio_device_register);
719
f8c6f4e9 720void iio_device_unregister(struct iio_dev *indio_dev)
847ec80b 721{
ac917a81
JC
722 mutex_lock(&indio_dev->info_exist_lock);
723 indio_dev->info = NULL;
724 mutex_unlock(&indio_dev->info_exist_lock);
f8c6f4e9 725 device_unregister(&indio_dev->dev);
847ec80b
JC
726}
727EXPORT_SYMBOL(iio_device_unregister);
847ec80b
JC
728subsys_initcall(iio_init);
729module_exit(iio_exit);
730
731MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>");
732MODULE_DESCRIPTION("Industrial I/O core");
733MODULE_LICENSE("GPL");
This page took 0.313123 seconds and 5 git commands to generate.