Commit | Line | Data |
---|---|---|
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> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/poll.h> | |
ffc18afa | 21 | #include <linux/sched.h> |
4439c935 | 22 | #include <linux/wait.h> |
847ec80b | 23 | #include <linux/cdev.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
847ec80b JC |
25 | #include "iio.h" |
26 | #include "trigger_consumer.h" | |
27 | ||
28 | #define IIO_ID_PREFIX "device" | |
29 | #define IIO_ID_FORMAT IIO_ID_PREFIX "%d" | |
30 | ||
31 | /* IDR to assign each registered device a unique id*/ | |
b156cf70 | 32 | static DEFINE_IDA(iio_ida); |
847ec80b | 33 | /* IDR to allocate character device minor numbers */ |
b156cf70 | 34 | static DEFINE_IDA(iio_chrdev_ida); |
847ec80b | 35 | /* Lock used to protect both of the above */ |
b156cf70 | 36 | static DEFINE_SPINLOCK(iio_ida_lock); |
847ec80b JC |
37 | |
38 | dev_t iio_devt; | |
39 | EXPORT_SYMBOL(iio_devt); | |
40 | ||
41 | #define IIO_DEV_MAX 256 | |
5aaaeba8 | 42 | struct bus_type iio_bus_type = { |
847ec80b | 43 | .name = "iio", |
847ec80b | 44 | }; |
5aaaeba8 | 45 | EXPORT_SYMBOL(iio_bus_type); |
847ec80b | 46 | |
1d892719 JC |
47 | static const char * const iio_chan_type_name_spec_shared[] = { |
48 | [IIO_TIMESTAMP] = "timestamp", | |
49 | [IIO_ACCEL] = "accel", | |
50 | [IIO_IN] = "in", | |
51 | [IIO_IN_DIFF] = "in-in", | |
52 | [IIO_GYRO] = "gyro", | |
53 | [IIO_TEMP] = "temp", | |
54 | [IIO_MAGN] = "magn", | |
55 | [IIO_INCLI] = "incli", | |
56 | [IIO_ROT] = "rot", | |
57 | [IIO_INTENSITY] = "intensity", | |
58 | [IIO_LIGHT] = "illuminance", | |
59 | [IIO_ANGL] = "angl", | |
60 | }; | |
61 | ||
62 | static const char * const iio_chan_type_name_spec_complex[] = { | |
63 | [IIO_IN_DIFF] = "in%d-in%d", | |
64 | }; | |
65 | ||
66 | static const char * const iio_modifier_names_light[] = { | |
67 | [IIO_MOD_LIGHT_BOTH] = "both", | |
68 | [IIO_MOD_LIGHT_IR] = "ir", | |
69 | }; | |
70 | ||
71 | static const char * const iio_modifier_names_axial[] = { | |
72 | [IIO_MOD_X] = "x", | |
73 | [IIO_MOD_Y] = "y", | |
74 | [IIO_MOD_Z] = "z", | |
75 | }; | |
76 | ||
77 | /* relies on pairs of these shared then separate */ | |
78 | static const char * const iio_chan_info_postfix[] = { | |
79 | [IIO_CHAN_INFO_SCALE_SHARED/2] = "scale", | |
80 | [IIO_CHAN_INFO_OFFSET_SHARED/2] = "offset", | |
81 | [IIO_CHAN_INFO_CALIBSCALE_SHARED/2] = "calibscale", | |
82 | [IIO_CHAN_INFO_CALIBBIAS_SHARED/2] = "calibbias", | |
83 | }; | |
84 | ||
aaf370db JC |
85 | int iio_push_event(struct iio_dev *dev_info, |
86 | int ev_line, | |
87 | int ev_code, | |
88 | s64 timestamp) | |
847ec80b | 89 | { |
aaf370db JC |
90 | struct iio_event_interface *ev_int |
91 | = &dev_info->event_interfaces[ev_line]; | |
847ec80b JC |
92 | struct iio_detected_event_list *ev; |
93 | int ret = 0; | |
94 | ||
95 | /* Does anyone care? */ | |
96 | mutex_lock(&ev_int->event_list_lock); | |
97 | if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) { | |
75c80753 JC |
98 | if (ev_int->current_events == ev_int->max_events) { |
99 | mutex_unlock(&ev_int->event_list_lock); | |
847ec80b | 100 | return 0; |
75c80753 | 101 | } |
847ec80b JC |
102 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
103 | if (ev == NULL) { | |
104 | ret = -ENOMEM; | |
75c80753 | 105 | mutex_unlock(&ev_int->event_list_lock); |
847ec80b JC |
106 | goto error_ret; |
107 | } | |
108 | ev->ev.id = ev_code; | |
109 | ev->ev.timestamp = timestamp; | |
847ec80b JC |
110 | |
111 | list_add_tail(&ev->list, &ev_int->det_events.list); | |
112 | ev_int->current_events++; | |
113 | mutex_unlock(&ev_int->event_list_lock); | |
114 | wake_up_interruptible(&ev_int->wait); | |
115 | } else | |
116 | mutex_unlock(&ev_int->event_list_lock); | |
117 | ||
118 | error_ret: | |
119 | return ret; | |
120 | } | |
847ec80b JC |
121 | EXPORT_SYMBOL(iio_push_event); |
122 | ||
847ec80b JC |
123 | |
124 | /* This turns up an awful lot */ | |
125 | ssize_t iio_read_const_attr(struct device *dev, | |
126 | struct device_attribute *attr, | |
127 | char *buf) | |
128 | { | |
129 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); | |
130 | } | |
131 | EXPORT_SYMBOL(iio_read_const_attr); | |
132 | ||
847ec80b | 133 | |
77712e5f MB |
134 | static ssize_t iio_event_chrdev_read(struct file *filep, |
135 | char __user *buf, | |
136 | size_t count, | |
137 | loff_t *f_ps) | |
847ec80b JC |
138 | { |
139 | struct iio_event_interface *ev_int = filep->private_data; | |
140 | struct iio_detected_event_list *el; | |
141 | int ret; | |
142 | size_t len; | |
143 | ||
144 | mutex_lock(&ev_int->event_list_lock); | |
145 | if (list_empty(&ev_int->det_events.list)) { | |
146 | if (filep->f_flags & O_NONBLOCK) { | |
147 | ret = -EAGAIN; | |
148 | goto error_mutex_unlock; | |
149 | } | |
150 | mutex_unlock(&ev_int->event_list_lock); | |
151 | /* Blocking on device; waiting for something to be there */ | |
152 | ret = wait_event_interruptible(ev_int->wait, | |
153 | !list_empty(&ev_int | |
154 | ->det_events.list)); | |
155 | if (ret) | |
156 | goto error_ret; | |
25985edc | 157 | /* Single access device so no one else can get the data */ |
847ec80b JC |
158 | mutex_lock(&ev_int->event_list_lock); |
159 | } | |
160 | ||
161 | el = list_first_entry(&ev_int->det_events.list, | |
162 | struct iio_detected_event_list, | |
163 | list); | |
164 | len = sizeof el->ev; | |
165 | if (copy_to_user(buf, &(el->ev), len)) { | |
166 | ret = -EFAULT; | |
167 | goto error_mutex_unlock; | |
168 | } | |
169 | list_del(&el->list); | |
170 | ev_int->current_events--; | |
171 | mutex_unlock(&ev_int->event_list_lock); | |
847ec80b JC |
172 | kfree(el); |
173 | ||
174 | return len; | |
175 | ||
176 | error_mutex_unlock: | |
177 | mutex_unlock(&ev_int->event_list_lock); | |
178 | error_ret: | |
179 | ||
180 | return ret; | |
181 | } | |
182 | ||
77712e5f | 183 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
847ec80b JC |
184 | { |
185 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); | |
186 | struct iio_event_interface *ev_int = hand->private; | |
187 | struct iio_detected_event_list *el, *t; | |
188 | ||
189 | mutex_lock(&ev_int->event_list_lock); | |
190 | clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags); | |
191 | /* | |
192 | * In order to maintain a clean state for reopening, | |
193 | * clear out any awaiting events. The mask will prevent | |
194 | * any new __iio_push_event calls running. | |
195 | */ | |
196 | list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) { | |
197 | list_del(&el->list); | |
198 | kfree(el); | |
199 | } | |
200 | mutex_unlock(&ev_int->event_list_lock); | |
201 | ||
202 | return 0; | |
203 | } | |
204 | ||
77712e5f | 205 | static int iio_event_chrdev_open(struct inode *inode, struct file *filep) |
847ec80b JC |
206 | { |
207 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); | |
208 | struct iio_event_interface *ev_int = hand->private; | |
209 | ||
210 | mutex_lock(&ev_int->event_list_lock); | |
211 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) { | |
212 | fops_put(filep->f_op); | |
213 | mutex_unlock(&ev_int->event_list_lock); | |
214 | return -EBUSY; | |
215 | } | |
216 | filep->private_data = hand->private; | |
217 | mutex_unlock(&ev_int->event_list_lock); | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
222 | static const struct file_operations iio_event_chrdev_fileops = { | |
223 | .read = iio_event_chrdev_read, | |
224 | .release = iio_event_chrdev_release, | |
225 | .open = iio_event_chrdev_open, | |
226 | .owner = THIS_MODULE, | |
6038f373 | 227 | .llseek = noop_llseek, |
847ec80b JC |
228 | }; |
229 | ||
230 | static void iio_event_dev_release(struct device *dev) | |
231 | { | |
232 | struct iio_event_interface *ev_int | |
233 | = container_of(dev, struct iio_event_interface, dev); | |
234 | cdev_del(&ev_int->handler.chrdev); | |
235 | iio_device_free_chrdev_minor(MINOR(dev->devt)); | |
236 | }; | |
237 | ||
238 | static struct device_type iio_event_type = { | |
239 | .release = iio_event_dev_release, | |
240 | }; | |
241 | ||
242 | int iio_device_get_chrdev_minor(void) | |
243 | { | |
244 | int ret, val; | |
245 | ||
b156cf70 JC |
246 | ida_again: |
247 | if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0)) | |
847ec80b | 248 | return -ENOMEM; |
b156cf70 JC |
249 | spin_lock(&iio_ida_lock); |
250 | ret = ida_get_new(&iio_chrdev_ida, &val); | |
251 | spin_unlock(&iio_ida_lock); | |
847ec80b | 252 | if (unlikely(ret == -EAGAIN)) |
b156cf70 | 253 | goto ida_again; |
847ec80b JC |
254 | else if (unlikely(ret)) |
255 | return ret; | |
256 | if (val > IIO_DEV_MAX) | |
257 | return -ENOMEM; | |
258 | return val; | |
259 | } | |
260 | ||
261 | void iio_device_free_chrdev_minor(int val) | |
262 | { | |
b156cf70 JC |
263 | spin_lock(&iio_ida_lock); |
264 | ida_remove(&iio_chrdev_ida, val); | |
265 | spin_unlock(&iio_ida_lock); | |
847ec80b JC |
266 | } |
267 | ||
b9d40a9d | 268 | static int iio_setup_ev_int(struct iio_event_interface *ev_int, |
847ec80b JC |
269 | const char *name, |
270 | struct module *owner, | |
271 | struct device *dev) | |
272 | { | |
273 | int ret, minor; | |
274 | ||
5aaaeba8 | 275 | ev_int->dev.bus = &iio_bus_type; |
847ec80b JC |
276 | ev_int->dev.parent = dev; |
277 | ev_int->dev.type = &iio_event_type; | |
278 | device_initialize(&ev_int->dev); | |
279 | ||
280 | minor = iio_device_get_chrdev_minor(); | |
281 | if (minor < 0) { | |
282 | ret = minor; | |
283 | goto error_device_put; | |
284 | } | |
285 | ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor); | |
286 | dev_set_name(&ev_int->dev, "%s", name); | |
287 | ||
288 | ret = device_add(&ev_int->dev); | |
289 | if (ret) | |
290 | goto error_free_minor; | |
291 | ||
292 | cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops); | |
293 | ev_int->handler.chrdev.owner = owner; | |
294 | ||
295 | mutex_init(&ev_int->event_list_lock); | |
296 | /* discussion point - make this variable? */ | |
297 | ev_int->max_events = 10; | |
298 | ev_int->current_events = 0; | |
299 | INIT_LIST_HEAD(&ev_int->det_events.list); | |
300 | init_waitqueue_head(&ev_int->wait); | |
301 | ev_int->handler.private = ev_int; | |
302 | ev_int->handler.flags = 0; | |
303 | ||
304 | ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1); | |
305 | if (ret) | |
306 | goto error_unreg_device; | |
307 | ||
308 | return 0; | |
309 | ||
310 | error_unreg_device: | |
311 | device_unregister(&ev_int->dev); | |
312 | error_free_minor: | |
313 | iio_device_free_chrdev_minor(minor); | |
314 | error_device_put: | |
315 | put_device(&ev_int->dev); | |
316 | ||
317 | return ret; | |
318 | } | |
319 | ||
b9d40a9d | 320 | static void iio_free_ev_int(struct iio_event_interface *ev_int) |
847ec80b JC |
321 | { |
322 | device_unregister(&ev_int->dev); | |
323 | put_device(&ev_int->dev); | |
324 | } | |
325 | ||
326 | static int __init iio_dev_init(void) | |
327 | { | |
328 | int err; | |
329 | ||
330 | err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); | |
331 | if (err < 0) | |
332 | printk(KERN_ERR "%s: failed to allocate char dev region\n", | |
333 | __FILE__); | |
334 | ||
335 | return err; | |
336 | } | |
337 | ||
338 | static void __exit iio_dev_exit(void) | |
339 | { | |
340 | if (iio_devt) | |
341 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); | |
342 | } | |
343 | ||
344 | static int __init iio_init(void) | |
345 | { | |
346 | int ret; | |
347 | ||
5aaaeba8 JC |
348 | /* Register sysfs bus */ |
349 | ret = bus_register(&iio_bus_type); | |
847ec80b JC |
350 | if (ret < 0) { |
351 | printk(KERN_ERR | |
5aaaeba8 | 352 | "%s could not register bus type\n", |
847ec80b JC |
353 | __FILE__); |
354 | goto error_nothing; | |
355 | } | |
356 | ||
357 | ret = iio_dev_init(); | |
358 | if (ret < 0) | |
5aaaeba8 | 359 | goto error_unregister_bus_type; |
847ec80b JC |
360 | |
361 | return 0; | |
362 | ||
5aaaeba8 JC |
363 | error_unregister_bus_type: |
364 | bus_unregister(&iio_bus_type); | |
847ec80b JC |
365 | error_nothing: |
366 | return ret; | |
367 | } | |
368 | ||
369 | static void __exit iio_exit(void) | |
370 | { | |
371 | iio_dev_exit(); | |
5aaaeba8 | 372 | bus_unregister(&iio_bus_type); |
847ec80b JC |
373 | } |
374 | ||
1d892719 JC |
375 | static ssize_t iio_read_channel_info(struct device *dev, |
376 | struct device_attribute *attr, | |
377 | char *buf) | |
847ec80b | 378 | { |
1d892719 JC |
379 | struct iio_dev *indio_dev = dev_get_drvdata(dev); |
380 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
381 | int val, val2; | |
382 | int ret = indio_dev->read_raw(indio_dev, this_attr->c, | |
383 | &val, &val2, this_attr->address); | |
384 | ||
385 | if (ret < 0) | |
386 | return ret; | |
847ec80b | 387 | |
1d892719 JC |
388 | if (ret == IIO_VAL_INT) |
389 | return sprintf(buf, "%d\n", val); | |
390 | else if (ret == IIO_VAL_INT_PLUS_MICRO) { | |
391 | if (val2 < 0) | |
392 | return sprintf(buf, "-%d.%06u\n", val, -val2); | |
393 | else | |
394 | return sprintf(buf, "%d.%06u\n", val, val2); | |
395 | } else | |
396 | return 0; | |
397 | } | |
398 | ||
399 | static ssize_t iio_write_channel_info(struct device *dev, | |
400 | struct device_attribute *attr, | |
401 | const char *buf, | |
402 | size_t len) | |
403 | { | |
404 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
405 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
406 | int ret, integer = 0, micro = 0, micro_mult = 100000; | |
407 | bool integer_part = true, negative = false; | |
408 | ||
409 | /* Assumes decimal - precision based on number of digits */ | |
410 | if (!indio_dev->write_raw) | |
411 | return -EINVAL; | |
412 | if (buf[0] == '-') { | |
413 | negative = true; | |
414 | buf++; | |
415 | } | |
416 | while (*buf) { | |
417 | if ('0' <= *buf && *buf <= '9') { | |
418 | if (integer_part) | |
419 | integer = integer*10 + *buf - '0'; | |
420 | else { | |
421 | micro += micro_mult*(*buf - '0'); | |
422 | if (micro_mult == 1) | |
423 | break; | |
424 | micro_mult /= 10; | |
425 | } | |
426 | } else if (*buf == '\n') { | |
427 | if (*(buf + 1) == '\0') | |
428 | break; | |
429 | else | |
430 | return -EINVAL; | |
431 | } else if (*buf == '.') { | |
432 | integer_part = false; | |
433 | } else { | |
434 | return -EINVAL; | |
435 | } | |
436 | buf++; | |
437 | } | |
438 | if (negative) { | |
439 | if (integer) | |
440 | integer = -integer; | |
441 | else | |
442 | micro = -micro; | |
443 | } | |
444 | ||
445 | ret = indio_dev->write_raw(indio_dev, this_attr->c, | |
446 | integer, micro, this_attr->address); | |
447 | if (ret) | |
448 | return ret; | |
449 | ||
450 | return len; | |
451 | } | |
452 | ||
453 | static int __iio_build_postfix(struct iio_chan_spec const *chan, | |
454 | bool generic, | |
455 | const char *postfix, | |
456 | char **result) | |
457 | { | |
458 | char *all_post; | |
459 | /* 3 options - generic, extend_name, modified - if generic, extend_name | |
460 | * and modified cannot apply.*/ | |
461 | ||
462 | if (generic || (!chan->modified && !chan->extend_name)) { | |
463 | all_post = kasprintf(GFP_KERNEL, "%s", postfix); | |
464 | } else if (chan->modified) { | |
465 | const char *intermediate; | |
466 | switch (chan->type) { | |
467 | case IIO_INTENSITY: | |
468 | intermediate | |
469 | = iio_modifier_names_light[chan->channel2]; | |
470 | break; | |
471 | case IIO_ACCEL: | |
472 | case IIO_GYRO: | |
473 | case IIO_MAGN: | |
474 | case IIO_INCLI: | |
475 | case IIO_ROT: | |
476 | case IIO_ANGL: | |
477 | intermediate | |
478 | = iio_modifier_names_axial[chan->channel2]; | |
479 | break; | |
480 | default: | |
481 | return -EINVAL; | |
482 | } | |
483 | if (chan->extend_name) | |
484 | all_post = kasprintf(GFP_KERNEL, "%s_%s_%s", | |
485 | intermediate, | |
486 | chan->extend_name, | |
487 | postfix); | |
488 | else | |
489 | all_post = kasprintf(GFP_KERNEL, "%s_%s", | |
490 | intermediate, | |
491 | postfix); | |
492 | } else | |
493 | all_post = kasprintf(GFP_KERNEL, "%s_%s", chan->extend_name, | |
494 | postfix); | |
495 | if (all_post == NULL) | |
496 | return -ENOMEM; | |
497 | *result = all_post; | |
498 | return 0; | |
499 | } | |
500 | ||
501 | int __iio_device_attr_init(struct device_attribute *dev_attr, | |
502 | const char *postfix, | |
503 | struct iio_chan_spec const *chan, | |
504 | ssize_t (*readfunc)(struct device *dev, | |
505 | struct device_attribute *attr, | |
506 | char *buf), | |
507 | ssize_t (*writefunc)(struct device *dev, | |
508 | struct device_attribute *attr, | |
509 | const char *buf, | |
510 | size_t len), | |
511 | bool generic) | |
512 | { | |
513 | int ret; | |
514 | char *name_format, *full_postfix; | |
515 | sysfs_attr_init(&dev_attr->attr); | |
516 | ret = __iio_build_postfix(chan, generic, postfix, &full_postfix); | |
517 | if (ret) | |
847ec80b | 518 | goto error_ret; |
1d892719 JC |
519 | |
520 | /* Special case for types that uses both channel numbers in naming */ | |
521 | if (chan->type == IIO_IN_DIFF && !generic) | |
522 | name_format | |
523 | = kasprintf(GFP_KERNEL, "%s_%s", | |
524 | iio_chan_type_name_spec_complex[chan->type], | |
525 | full_postfix); | |
526 | else if (generic || !chan->indexed) | |
527 | name_format | |
528 | = kasprintf(GFP_KERNEL, "%s_%s", | |
529 | iio_chan_type_name_spec_shared[chan->type], | |
530 | full_postfix); | |
531 | else | |
532 | name_format | |
533 | = kasprintf(GFP_KERNEL, "%s%d_%s", | |
534 | iio_chan_type_name_spec_shared[chan->type], | |
535 | chan->channel, | |
536 | full_postfix); | |
537 | ||
538 | if (name_format == NULL) { | |
539 | ret = -ENOMEM; | |
540 | goto error_free_full_postfix; | |
541 | } | |
542 | dev_attr->attr.name = kasprintf(GFP_KERNEL, | |
543 | name_format, | |
544 | chan->channel, | |
545 | chan->channel2); | |
546 | if (dev_attr->attr.name == NULL) { | |
547 | ret = -ENOMEM; | |
548 | goto error_free_name_format; | |
549 | } | |
550 | ||
551 | if (readfunc) { | |
552 | dev_attr->attr.mode |= S_IRUGO; | |
553 | dev_attr->show = readfunc; | |
554 | } | |
555 | ||
556 | if (writefunc) { | |
557 | dev_attr->attr.mode |= S_IWUSR; | |
558 | dev_attr->store = writefunc; | |
559 | } | |
560 | kfree(name_format); | |
561 | kfree(full_postfix); | |
562 | ||
563 | return 0; | |
564 | ||
565 | error_free_name_format: | |
566 | kfree(name_format); | |
567 | error_free_full_postfix: | |
568 | kfree(full_postfix); | |
569 | error_ret: | |
570 | return ret; | |
571 | } | |
572 | ||
573 | void __iio_device_attr_deinit(struct device_attribute *dev_attr) | |
574 | { | |
575 | kfree(dev_attr->attr.name); | |
576 | } | |
577 | ||
578 | int __iio_add_chan_devattr(const char *postfix, | |
579 | const char *group, | |
580 | struct iio_chan_spec const *chan, | |
581 | ssize_t (*readfunc)(struct device *dev, | |
582 | struct device_attribute *attr, | |
583 | char *buf), | |
584 | ssize_t (*writefunc)(struct device *dev, | |
585 | struct device_attribute *attr, | |
586 | const char *buf, | |
587 | size_t len), | |
588 | int mask, | |
589 | bool generic, | |
590 | struct device *dev, | |
591 | struct list_head *attr_list) | |
592 | { | |
593 | int ret; | |
594 | struct iio_dev_attr *iio_attr, *t; | |
595 | ||
596 | iio_attr = kzalloc(sizeof *iio_attr, GFP_KERNEL); | |
597 | if (iio_attr == NULL) { | |
598 | ret = -ENOMEM; | |
599 | goto error_ret; | |
600 | } | |
601 | ret = __iio_device_attr_init(&iio_attr->dev_attr, | |
602 | postfix, chan, | |
603 | readfunc, writefunc, generic); | |
604 | if (ret) | |
605 | goto error_iio_dev_attr_free; | |
606 | iio_attr->c = chan; | |
607 | iio_attr->address = mask; | |
608 | list_for_each_entry(t, attr_list, l) | |
609 | if (strcmp(t->dev_attr.attr.name, | |
610 | iio_attr->dev_attr.attr.name) == 0) { | |
611 | if (!generic) | |
612 | dev_err(dev, "tried to double register : %s\n", | |
613 | t->dev_attr.attr.name); | |
614 | ret = -EBUSY; | |
615 | goto error_device_attr_deinit; | |
616 | } | |
617 | ||
618 | ret = sysfs_add_file_to_group(&dev->kobj, | |
619 | &iio_attr->dev_attr.attr, group); | |
620 | if (ret < 0) | |
621 | goto error_device_attr_deinit; | |
622 | ||
623 | list_add(&iio_attr->l, attr_list); | |
624 | ||
625 | return 0; | |
626 | ||
627 | error_device_attr_deinit: | |
628 | __iio_device_attr_deinit(&iio_attr->dev_attr); | |
629 | error_iio_dev_attr_free: | |
630 | kfree(iio_attr); | |
631 | error_ret: | |
632 | return ret; | |
633 | } | |
634 | ||
635 | static int iio_device_add_channel_sysfs(struct iio_dev *dev_info, | |
636 | struct iio_chan_spec const *chan) | |
637 | { | |
638 | int ret, i; | |
639 | ||
640 | ||
641 | if (chan->channel < 0) | |
642 | return 0; | |
643 | if (chan->processed_val) | |
644 | ret = __iio_add_chan_devattr("input", NULL, chan, | |
645 | &iio_read_channel_info, | |
646 | NULL, | |
647 | 0, | |
648 | 0, | |
649 | &dev_info->dev, | |
650 | &dev_info->channel_attr_list); | |
651 | else | |
652 | ret = __iio_add_chan_devattr("raw", NULL, chan, | |
653 | &iio_read_channel_info, | |
654 | NULL, | |
655 | 0, | |
656 | 0, | |
657 | &dev_info->dev, | |
658 | &dev_info->channel_attr_list); | |
659 | if (ret) | |
660 | goto error_ret; | |
661 | ||
662 | for_each_set_bit(i, &chan->info_mask, sizeof(long)*8) { | |
663 | ret = __iio_add_chan_devattr(iio_chan_info_postfix[i/2], | |
664 | NULL, chan, | |
665 | &iio_read_channel_info, | |
666 | &iio_write_channel_info, | |
667 | (1 << i), | |
668 | !(i%2), | |
669 | &dev_info->dev, | |
670 | &dev_info->channel_attr_list); | |
671 | if (ret == -EBUSY && (i%2 == 0)) { | |
672 | ret = 0; | |
673 | continue; | |
674 | } | |
675 | if (ret < 0) | |
676 | goto error_ret; | |
677 | } | |
678 | error_ret: | |
679 | return ret; | |
680 | } | |
681 | ||
682 | static void iio_device_remove_and_free_read_attr(struct iio_dev *dev_info, | |
683 | struct iio_dev_attr *p) | |
684 | { | |
685 | sysfs_remove_file_from_group(&dev_info->dev.kobj, | |
686 | &p->dev_attr.attr, NULL); | |
687 | kfree(p->dev_attr.attr.name); | |
688 | kfree(p); | |
689 | } | |
690 | ||
691 | static int iio_device_register_sysfs(struct iio_dev *dev_info) | |
692 | { | |
693 | int i, ret = 0; | |
694 | struct iio_dev_attr *p, *n; | |
695 | ||
696 | if (dev_info->attrs) { | |
697 | ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs); | |
698 | if (ret) { | |
699 | dev_err(dev_info->dev.parent, | |
700 | "Failed to register sysfs hooks\n"); | |
701 | goto error_ret; | |
702 | } | |
847ec80b JC |
703 | } |
704 | ||
1d892719 JC |
705 | /* |
706 | * New channel registration method - relies on the fact a group does | |
707 | * not need to be initialized if it is name is NULL. | |
708 | */ | |
709 | INIT_LIST_HEAD(&dev_info->channel_attr_list); | |
710 | if (dev_info->channels) | |
711 | for (i = 0; i < dev_info->num_channels; i++) { | |
712 | ret = iio_device_add_channel_sysfs(dev_info, | |
713 | &dev_info | |
714 | ->channels[i]); | |
715 | if (ret < 0) | |
716 | goto error_clear_attrs; | |
717 | } | |
718 | ||
719 | return 0; | |
720 | error_clear_attrs: | |
721 | list_for_each_entry_safe(p, n, | |
722 | &dev_info->channel_attr_list, l) { | |
723 | list_del(&p->l); | |
724 | iio_device_remove_and_free_read_attr(dev_info, p); | |
725 | } | |
726 | if (dev_info->attrs) | |
727 | sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs); | |
847ec80b JC |
728 | error_ret: |
729 | return ret; | |
1d892719 | 730 | |
847ec80b JC |
731 | } |
732 | ||
733 | static void iio_device_unregister_sysfs(struct iio_dev *dev_info) | |
734 | { | |
1d892719 JC |
735 | |
736 | struct iio_dev_attr *p, *n; | |
737 | list_for_each_entry_safe(p, n, &dev_info->channel_attr_list, l) { | |
738 | list_del(&p->l); | |
739 | iio_device_remove_and_free_read_attr(dev_info, p); | |
740 | } | |
741 | ||
742 | if (dev_info->attrs) | |
743 | sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs); | |
847ec80b JC |
744 | } |
745 | ||
f3cdc285 | 746 | /* Return a negative errno on failure */ |
b156cf70 | 747 | int iio_get_new_ida_val(struct ida *this_ida) |
847ec80b JC |
748 | { |
749 | int ret; | |
750 | int val; | |
751 | ||
b156cf70 JC |
752 | ida_again: |
753 | if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0)) | |
847ec80b JC |
754 | return -ENOMEM; |
755 | ||
b156cf70 JC |
756 | spin_lock(&iio_ida_lock); |
757 | ret = ida_get_new(this_ida, &val); | |
758 | spin_unlock(&iio_ida_lock); | |
847ec80b | 759 | if (unlikely(ret == -EAGAIN)) |
b156cf70 | 760 | goto ida_again; |
847ec80b JC |
761 | else if (unlikely(ret)) |
762 | return ret; | |
763 | ||
764 | return val; | |
765 | } | |
b156cf70 | 766 | EXPORT_SYMBOL(iio_get_new_ida_val); |
847ec80b | 767 | |
b156cf70 | 768 | void iio_free_ida_val(struct ida *this_ida, int id) |
847ec80b | 769 | { |
b156cf70 JC |
770 | spin_lock(&iio_ida_lock); |
771 | ida_remove(this_ida, id); | |
772 | spin_unlock(&iio_ida_lock); | |
847ec80b | 773 | } |
b156cf70 | 774 | EXPORT_SYMBOL(iio_free_ida_val); |
847ec80b JC |
775 | |
776 | static int iio_device_register_id(struct iio_dev *dev_info, | |
b156cf70 | 777 | struct ida *this_ida) |
847ec80b | 778 | { |
b156cf70 | 779 | dev_info->id = iio_get_new_ida_val(&iio_ida); |
847ec80b JC |
780 | if (dev_info->id < 0) |
781 | return dev_info->id; | |
782 | return 0; | |
783 | } | |
784 | ||
785 | static void iio_device_unregister_id(struct iio_dev *dev_info) | |
786 | { | |
b156cf70 | 787 | iio_free_ida_val(&iio_ida, dev_info->id); |
847ec80b JC |
788 | } |
789 | ||
1d892719 JC |
790 | static const char * const iio_ev_type_text[] = { |
791 | [IIO_EV_TYPE_THRESH] = "thresh", | |
792 | [IIO_EV_TYPE_MAG] = "mag", | |
793 | [IIO_EV_TYPE_ROC] = "roc" | |
794 | }; | |
795 | ||
796 | static const char * const iio_ev_dir_text[] = { | |
797 | [IIO_EV_DIR_EITHER] = "either", | |
798 | [IIO_EV_DIR_RISING] = "rising", | |
799 | [IIO_EV_DIR_FALLING] = "falling" | |
800 | }; | |
801 | ||
802 | static ssize_t iio_ev_state_store(struct device *dev, | |
803 | struct device_attribute *attr, | |
804 | const char *buf, | |
805 | size_t len) | |
806 | { | |
807 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
aaf370db | 808 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
1d892719 JC |
809 | int ret; |
810 | unsigned long val; | |
811 | ret = strict_strtoul(buf, 10, &val); | |
812 | if (ret || val < 0 || val > 1) | |
813 | return -EINVAL; | |
814 | ||
aaf370db | 815 | ret = indio_dev->write_event_config(indio_dev, this_attr->address, |
1d892719 JC |
816 | val); |
817 | return (ret < 0) ? ret : len; | |
818 | } | |
819 | ||
820 | static ssize_t iio_ev_state_show(struct device *dev, | |
821 | struct device_attribute *attr, | |
822 | char *buf) | |
823 | { | |
824 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
aaf370db JC |
825 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
826 | int val = indio_dev->read_event_config(indio_dev, this_attr->address); | |
1d892719 JC |
827 | |
828 | if (val < 0) | |
829 | return val; | |
830 | else | |
831 | return sprintf(buf, "%d\n", val); | |
832 | } | |
833 | ||
834 | static ssize_t iio_ev_value_show(struct device *dev, | |
835 | struct device_attribute *attr, | |
836 | char *buf) | |
837 | { | |
838 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
839 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
840 | int val, ret; | |
841 | ||
842 | ret = indio_dev->read_event_value(indio_dev, | |
843 | this_attr->address, &val); | |
844 | if (ret < 0) | |
845 | return ret; | |
846 | ||
847 | return sprintf(buf, "%d\n", val); | |
848 | } | |
849 | ||
850 | static ssize_t iio_ev_value_store(struct device *dev, | |
851 | struct device_attribute *attr, | |
852 | const char *buf, | |
853 | size_t len) | |
854 | { | |
855 | struct iio_dev *indio_dev = dev_get_drvdata(dev); | |
856 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); | |
857 | unsigned long val; | |
858 | int ret; | |
859 | ||
860 | ret = strict_strtoul(buf, 10, &val); | |
861 | if (ret) | |
862 | return ret; | |
863 | ||
864 | ret = indio_dev->write_event_value(indio_dev, this_attr->address, | |
865 | val); | |
866 | if (ret < 0) | |
867 | return ret; | |
868 | ||
869 | return len; | |
870 | } | |
871 | ||
1d892719 JC |
872 | static int iio_device_add_event_sysfs(struct iio_dev *dev_info, |
873 | struct iio_chan_spec const *chan) | |
874 | { | |
875 | ||
876 | int ret = 0, i, mask; | |
877 | char *postfix; | |
878 | if (!chan->event_mask) | |
879 | return 0; | |
880 | ||
881 | for_each_set_bit(i, &chan->event_mask, sizeof(chan->event_mask)*8) { | |
882 | postfix = kasprintf(GFP_KERNEL, "%s_%s_en", | |
883 | iio_ev_type_text[i/IIO_EV_TYPE_MAX], | |
884 | iio_ev_dir_text[i%IIO_EV_TYPE_MAX]); | |
885 | if (postfix == NULL) { | |
886 | ret = -ENOMEM; | |
887 | goto error_ret; | |
888 | } | |
889 | switch (chan->type) { | |
890 | /* Switch this to a table at some point */ | |
891 | case IIO_IN: | |
892 | mask = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, | |
893 | i/IIO_EV_TYPE_MAX, | |
894 | i%IIO_EV_TYPE_MAX); | |
895 | break; | |
896 | case IIO_ACCEL: | |
897 | mask = IIO_MOD_EVENT_CODE(chan->type, 0, chan->channel, | |
898 | i/IIO_EV_TYPE_MAX, | |
899 | i%IIO_EV_TYPE_MAX); | |
900 | break; | |
901 | case IIO_IN_DIFF: | |
902 | mask = IIO_MOD_EVENT_CODE(chan->type, chan->channel, | |
903 | chan->channel2, | |
904 | i/IIO_EV_TYPE_MAX, | |
905 | i%IIO_EV_TYPE_MAX); | |
906 | break; | |
907 | default: | |
908 | printk(KERN_INFO "currently unhandled type of event\n"); | |
909 | } | |
aaf370db JC |
910 | ret = __iio_add_chan_devattr(postfix, |
911 | NULL, | |
912 | chan, | |
913 | &iio_ev_state_show, | |
914 | iio_ev_state_store, | |
915 | mask, | |
916 | /*HACK. - limits us to one | |
917 | event interface - fix by | |
918 | extending the bitmask - but | |
919 | how far*/ | |
920 | 0, | |
921 | &dev_info->event_interfaces[0] | |
922 | .dev, | |
923 | &dev_info->event_interfaces[0]. | |
924 | dev_attr_list); | |
1d892719 JC |
925 | kfree(postfix); |
926 | if (ret) | |
927 | goto error_ret; | |
928 | ||
929 | postfix = kasprintf(GFP_KERNEL, "%s_%s_value", | |
930 | iio_ev_type_text[i/IIO_EV_TYPE_MAX], | |
931 | iio_ev_dir_text[i%IIO_EV_TYPE_MAX]); | |
932 | if (postfix == NULL) { | |
933 | ret = -ENOMEM; | |
934 | goto error_ret; | |
935 | } | |
936 | ret = __iio_add_chan_devattr(postfix, NULL, chan, | |
937 | iio_ev_value_show, | |
938 | iio_ev_value_store, | |
939 | mask, | |
940 | 0, | |
941 | &dev_info->event_interfaces[0] | |
942 | .dev, | |
943 | &dev_info->event_interfaces[0] | |
944 | .dev_attr_list); | |
945 | kfree(postfix); | |
946 | if (ret) | |
947 | goto error_ret; | |
948 | ||
949 | } | |
950 | ||
951 | error_ret: | |
952 | return ret; | |
953 | } | |
954 | ||
955 | static inline void __iio_remove_all_event_sysfs(struct iio_dev *dev_info, | |
956 | const char *groupname, | |
957 | int num) | |
958 | { | |
959 | struct iio_dev_attr *p, *n; | |
1d892719 JC |
960 | list_for_each_entry_safe(p, n, |
961 | &dev_info->event_interfaces[num]. | |
962 | dev_attr_list, l) { | |
963 | sysfs_remove_file_from_group(&dev_info | |
964 | ->event_interfaces[num].dev.kobj, | |
965 | &p->dev_attr.attr, | |
966 | groupname); | |
967 | kfree(p->dev_attr.attr.name); | |
968 | kfree(p); | |
969 | } | |
1d892719 JC |
970 | } |
971 | ||
847ec80b JC |
972 | static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i) |
973 | { | |
1d892719 | 974 | int j; |
847ec80b | 975 | int ret; |
847ec80b JC |
976 | struct attribute **attrp, **attrq; |
977 | ||
978 | if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) { | |
979 | attrp = dev_info->event_conf_attrs[i].attrs; | |
980 | while (*attrp) { | |
1d892719 JC |
981 | ret = sysfs_add_file_to_group(&dev_info |
982 | ->event_interfaces[0] | |
983 | .dev.kobj, | |
847ec80b | 984 | *attrp, |
1d892719 | 985 | NULL); |
847ec80b JC |
986 | if (ret) |
987 | goto error_ret; | |
988 | attrp++; | |
989 | } | |
990 | } | |
1d892719 JC |
991 | INIT_LIST_HEAD(&dev_info->event_interfaces[0].dev_attr_list); |
992 | /* Dynically created from the channels array */ | |
993 | if (dev_info->channels) { | |
994 | for (j = 0; j < dev_info->num_channels; j++) { | |
995 | ret = iio_device_add_event_sysfs(dev_info, | |
996 | &dev_info | |
997 | ->channels[j]); | |
998 | if (ret) | |
999 | goto error_clear_attrs; | |
1000 | } | |
1001 | } | |
847ec80b JC |
1002 | return 0; |
1003 | ||
1d892719 JC |
1004 | error_clear_attrs: |
1005 | __iio_remove_all_event_sysfs(dev_info, | |
1006 | NULL, | |
1007 | i); | |
847ec80b JC |
1008 | error_ret: |
1009 | attrq = dev_info->event_conf_attrs[i].attrs; | |
1010 | while (attrq != attrp) { | |
1d892719 JC |
1011 | sysfs_remove_file_from_group(&dev_info |
1012 | ->event_interfaces[0] | |
1013 | .dev.kobj, | |
1014 | *attrq, | |
1015 | NULL); | |
847ec80b JC |
1016 | attrq++; |
1017 | } | |
1018 | ||
1019 | return ret; | |
1020 | } | |
1021 | ||
1022 | static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info, | |
1023 | int i) | |
1024 | { | |
1025 | struct attribute **attrq; | |
1d892719 JC |
1026 | __iio_remove_all_event_sysfs(dev_info, |
1027 | NULL, | |
1028 | i); | |
847ec80b JC |
1029 | if (dev_info->event_conf_attrs |
1030 | && dev_info->event_conf_attrs[i].attrs) { | |
1031 | attrq = dev_info->event_conf_attrs[i].attrs; | |
1032 | while (*attrq) { | |
1d892719 JC |
1033 | sysfs_remove_file_from_group(&dev_info |
1034 | ->event_interfaces[0] | |
1035 | .dev.kobj, | |
847ec80b | 1036 | *attrq, |
1d892719 | 1037 | NULL); |
847ec80b JC |
1038 | attrq++; |
1039 | } | |
1040 | } | |
1041 | ||
1042 | return 0; | |
1043 | } | |
1044 | ||
1045 | static int iio_device_register_eventset(struct iio_dev *dev_info) | |
1046 | { | |
1047 | int ret = 0, i, j; | |
1048 | ||
1049 | if (dev_info->num_interrupt_lines == 0) | |
1050 | return 0; | |
1051 | ||
1052 | dev_info->event_interfaces = | |
1053 | kzalloc(sizeof(struct iio_event_interface) | |
1054 | *dev_info->num_interrupt_lines, | |
1055 | GFP_KERNEL); | |
1056 | if (dev_info->event_interfaces == NULL) { | |
1057 | ret = -ENOMEM; | |
1058 | goto error_ret; | |
1059 | } | |
1060 | ||
847ec80b JC |
1061 | for (i = 0; i < dev_info->num_interrupt_lines; i++) { |
1062 | dev_info->event_interfaces[i].owner = dev_info->driver_module; | |
847ec80b JC |
1063 | |
1064 | snprintf(dev_info->event_interfaces[i]._name, 20, | |
ba5c6fba JC |
1065 | "%s:event%d", |
1066 | dev_name(&dev_info->dev), | |
3d550fba | 1067 | i); |
847ec80b JC |
1068 | |
1069 | ret = iio_setup_ev_int(&dev_info->event_interfaces[i], | |
1070 | (const char *)(dev_info | |
1071 | ->event_interfaces[i] | |
1072 | ._name), | |
1073 | dev_info->driver_module, | |
1074 | &dev_info->dev); | |
1075 | if (ret) { | |
1076 | dev_err(&dev_info->dev, | |
1077 | "Could not get chrdev interface\n"); | |
847ec80b JC |
1078 | goto error_free_setup_ev_ints; |
1079 | } | |
847ec80b | 1080 | |
5cba220b JC |
1081 | dev_set_drvdata(&dev_info->event_interfaces[i].dev, |
1082 | (void *)dev_info); | |
1d892719 JC |
1083 | |
1084 | if (dev_info->event_attrs != NULL) | |
1085 | ret = sysfs_create_group(&dev_info | |
1086 | ->event_interfaces[i] | |
1087 | .dev.kobj, | |
1088 | &dev_info->event_attrs[i]); | |
5cba220b | 1089 | |
847ec80b JC |
1090 | if (ret) { |
1091 | dev_err(&dev_info->dev, | |
1092 | "Failed to register sysfs for event attrs"); | |
1093 | goto error_remove_sysfs_interfaces; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | for (i = 0; i < dev_info->num_interrupt_lines; i++) { | |
1098 | ret = __iio_add_event_config_attrs(dev_info, i); | |
1099 | if (ret) | |
1100 | goto error_unregister_config_attrs; | |
1101 | } | |
1102 | ||
1103 | return 0; | |
1104 | ||
1105 | error_unregister_config_attrs: | |
1106 | for (j = 0; j < i; j++) | |
1107 | __iio_remove_event_config_attrs(dev_info, i); | |
1108 | i = dev_info->num_interrupt_lines - 1; | |
1109 | error_remove_sysfs_interfaces: | |
1110 | for (j = 0; j < i; j++) | |
1d892719 JC |
1111 | if (dev_info->event_attrs != NULL) |
1112 | sysfs_remove_group(&dev_info | |
5cba220b | 1113 | ->event_interfaces[j].dev.kobj, |
847ec80b | 1114 | &dev_info->event_attrs[j]); |
847ec80b | 1115 | error_free_setup_ev_ints: |
3d550fba | 1116 | for (j = 0; j < i; j++) |
847ec80b | 1117 | iio_free_ev_int(&dev_info->event_interfaces[j]); |
847ec80b JC |
1118 | kfree(dev_info->event_interfaces); |
1119 | error_ret: | |
1120 | ||
1121 | return ret; | |
1122 | } | |
1123 | ||
1124 | static void iio_device_unregister_eventset(struct iio_dev *dev_info) | |
1125 | { | |
1126 | int i; | |
1127 | ||
1128 | if (dev_info->num_interrupt_lines == 0) | |
1129 | return; | |
1d892719 JC |
1130 | for (i = 0; i < dev_info->num_interrupt_lines; i++) { |
1131 | __iio_remove_event_config_attrs(dev_info, i); | |
1132 | if (dev_info->event_attrs != NULL) | |
1133 | sysfs_remove_group(&dev_info | |
1134 | ->event_interfaces[i].dev.kobj, | |
1135 | &dev_info->event_attrs[i]); | |
1136 | } | |
847ec80b | 1137 | |
3d550fba | 1138 | for (i = 0; i < dev_info->num_interrupt_lines; i++) |
847ec80b | 1139 | iio_free_ev_int(&dev_info->event_interfaces[i]); |
847ec80b JC |
1140 | kfree(dev_info->event_interfaces); |
1141 | } | |
1142 | ||
1143 | static void iio_dev_release(struct device *device) | |
1144 | { | |
1145 | struct iio_dev *dev = to_iio_dev(device); | |
1146 | ||
1147 | iio_put(); | |
1148 | kfree(dev); | |
1149 | } | |
1150 | ||
1151 | static struct device_type iio_dev_type = { | |
1152 | .name = "iio_device", | |
1153 | .release = iio_dev_release, | |
1154 | }; | |
1155 | ||
6f7c8ee5 | 1156 | struct iio_dev *iio_allocate_device(int sizeof_priv) |
847ec80b | 1157 | { |
6f7c8ee5 JC |
1158 | struct iio_dev *dev; |
1159 | size_t alloc_size; | |
1160 | ||
1161 | alloc_size = sizeof(struct iio_dev); | |
1162 | if (sizeof_priv) { | |
1163 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); | |
1164 | alloc_size += sizeof_priv; | |
1165 | } | |
1166 | /* ensure 32-byte alignment of whole construct ? */ | |
1167 | alloc_size += IIO_ALIGN - 1; | |
1168 | ||
1169 | dev = kzalloc(alloc_size, GFP_KERNEL); | |
847ec80b JC |
1170 | |
1171 | if (dev) { | |
1172 | dev->dev.type = &iio_dev_type; | |
5aaaeba8 | 1173 | dev->dev.bus = &iio_bus_type; |
847ec80b JC |
1174 | device_initialize(&dev->dev); |
1175 | dev_set_drvdata(&dev->dev, (void *)dev); | |
1176 | mutex_init(&dev->mlock); | |
1177 | iio_get(); | |
1178 | } | |
1179 | ||
1180 | return dev; | |
1181 | } | |
1182 | EXPORT_SYMBOL(iio_allocate_device); | |
1183 | ||
1184 | void iio_free_device(struct iio_dev *dev) | |
1185 | { | |
1186 | if (dev) | |
1187 | iio_put_device(dev); | |
1188 | } | |
1189 | EXPORT_SYMBOL(iio_free_device); | |
1190 | ||
1191 | int iio_device_register(struct iio_dev *dev_info) | |
1192 | { | |
1193 | int ret; | |
1194 | ||
b156cf70 | 1195 | ret = iio_device_register_id(dev_info, &iio_ida); |
847ec80b JC |
1196 | if (ret) { |
1197 | dev_err(&dev_info->dev, "Failed to get id\n"); | |
1198 | goto error_ret; | |
1199 | } | |
1200 | dev_set_name(&dev_info->dev, "device%d", dev_info->id); | |
1201 | ||
1202 | ret = device_add(&dev_info->dev); | |
1203 | if (ret) | |
b156cf70 | 1204 | goto error_free_ida; |
847ec80b JC |
1205 | ret = iio_device_register_sysfs(dev_info); |
1206 | if (ret) { | |
1207 | dev_err(dev_info->dev.parent, | |
1208 | "Failed to register sysfs interfaces\n"); | |
1209 | goto error_del_device; | |
1210 | } | |
1211 | ret = iio_device_register_eventset(dev_info); | |
1212 | if (ret) { | |
1213 | dev_err(dev_info->dev.parent, | |
c849d253 | 1214 | "Failed to register event set\n"); |
847ec80b JC |
1215 | goto error_free_sysfs; |
1216 | } | |
1217 | if (dev_info->modes & INDIO_RING_TRIGGERED) | |
1218 | iio_device_register_trigger_consumer(dev_info); | |
1219 | ||
1220 | return 0; | |
1221 | ||
1222 | error_free_sysfs: | |
1223 | iio_device_unregister_sysfs(dev_info); | |
1224 | error_del_device: | |
1225 | device_del(&dev_info->dev); | |
b156cf70 | 1226 | error_free_ida: |
847ec80b JC |
1227 | iio_device_unregister_id(dev_info); |
1228 | error_ret: | |
1229 | return ret; | |
1230 | } | |
1231 | EXPORT_SYMBOL(iio_device_register); | |
1232 | ||
1233 | void iio_device_unregister(struct iio_dev *dev_info) | |
1234 | { | |
1235 | if (dev_info->modes & INDIO_RING_TRIGGERED) | |
1236 | iio_device_unregister_trigger_consumer(dev_info); | |
1237 | iio_device_unregister_eventset(dev_info); | |
1238 | iio_device_unregister_sysfs(dev_info); | |
1239 | iio_device_unregister_id(dev_info); | |
1240 | device_unregister(&dev_info->dev); | |
1241 | } | |
1242 | EXPORT_SYMBOL(iio_device_unregister); | |
1243 | ||
1244 | void iio_put(void) | |
1245 | { | |
1246 | module_put(THIS_MODULE); | |
1247 | } | |
1248 | ||
1249 | void iio_get(void) | |
1250 | { | |
1251 | __module_get(THIS_MODULE); | |
1252 | } | |
1253 | ||
1254 | subsys_initcall(iio_init); | |
1255 | module_exit(iio_exit); | |
1256 | ||
1257 | MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>"); | |
1258 | MODULE_DESCRIPTION("Industrial I/O core"); | |
1259 | MODULE_LICENSE("GPL"); |