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 JC |
46 | |
47 | void __iio_change_event(struct iio_detected_event_list *ev, | |
48 | int ev_code, | |
49 | s64 timestamp) | |
50 | { | |
51 | ev->ev.id = ev_code; | |
52 | ev->ev.timestamp = timestamp; | |
53 | } | |
54 | EXPORT_SYMBOL(__iio_change_event); | |
55 | ||
56 | /* Used both in the interrupt line put events and the ring buffer ones */ | |
57 | ||
58 | /* Note that in it's current form someone has to be listening before events | |
59 | * are queued. Hence a client MUST open the chrdev before the ring buffer is | |
60 | * switched on. | |
61 | */ | |
0abd2428 | 62 | int __iio_push_event(struct iio_event_interface *ev_int, |
847ec80b JC |
63 | int ev_code, |
64 | s64 timestamp, | |
65 | struct iio_shared_ev_pointer * | |
66 | shared_pointer_p) | |
67 | { | |
68 | struct iio_detected_event_list *ev; | |
69 | int ret = 0; | |
70 | ||
71 | /* Does anyone care? */ | |
72 | mutex_lock(&ev_int->event_list_lock); | |
73 | if (test_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags)) { | |
75c80753 JC |
74 | if (ev_int->current_events == ev_int->max_events) { |
75 | mutex_unlock(&ev_int->event_list_lock); | |
847ec80b | 76 | return 0; |
75c80753 | 77 | } |
847ec80b JC |
78 | ev = kmalloc(sizeof(*ev), GFP_KERNEL); |
79 | if (ev == NULL) { | |
80 | ret = -ENOMEM; | |
75c80753 | 81 | mutex_unlock(&ev_int->event_list_lock); |
847ec80b JC |
82 | goto error_ret; |
83 | } | |
84 | ev->ev.id = ev_code; | |
85 | ev->ev.timestamp = timestamp; | |
86 | ev->shared_pointer = shared_pointer_p; | |
87 | if (ev->shared_pointer) | |
88 | shared_pointer_p->ev_p = ev; | |
89 | ||
90 | list_add_tail(&ev->list, &ev_int->det_events.list); | |
91 | ev_int->current_events++; | |
92 | mutex_unlock(&ev_int->event_list_lock); | |
93 | wake_up_interruptible(&ev_int->wait); | |
94 | } else | |
95 | mutex_unlock(&ev_int->event_list_lock); | |
96 | ||
97 | error_ret: | |
98 | return ret; | |
99 | } | |
100 | EXPORT_SYMBOL(__iio_push_event); | |
101 | ||
102 | int iio_push_event(struct iio_dev *dev_info, | |
103 | int ev_line, | |
104 | int ev_code, | |
105 | s64 timestamp) | |
106 | { | |
107 | return __iio_push_event(&dev_info->event_interfaces[ev_line], | |
108 | ev_code, timestamp, NULL); | |
109 | } | |
110 | EXPORT_SYMBOL(iio_push_event); | |
111 | ||
112 | /* Generic interrupt line interrupt handler */ | |
77712e5f | 113 | static irqreturn_t iio_interrupt_handler(int irq, void *_int_info) |
847ec80b JC |
114 | { |
115 | struct iio_interrupt *int_info = _int_info; | |
116 | struct iio_dev *dev_info = int_info->dev_info; | |
117 | struct iio_event_handler_list *p; | |
118 | s64 time_ns; | |
119 | unsigned long flags; | |
120 | ||
121 | spin_lock_irqsave(&int_info->ev_list_lock, flags); | |
122 | if (list_empty(&int_info->ev_list)) { | |
123 | spin_unlock_irqrestore(&int_info->ev_list_lock, flags); | |
124 | return IRQ_NONE; | |
125 | } | |
126 | ||
127 | time_ns = iio_get_time_ns(); | |
b6e5d69a | 128 | list_for_each_entry(p, &int_info->ev_list, list) { |
847ec80b | 129 | disable_irq_nosync(irq); |
847ec80b | 130 | p->handler(dev_info, 1, time_ns, !(p->refcount > 1)); |
b6e5d69a | 131 | } |
847ec80b JC |
132 | spin_unlock_irqrestore(&int_info->ev_list_lock, flags); |
133 | ||
134 | return IRQ_HANDLED; | |
135 | } | |
136 | ||
137 | static struct iio_interrupt *iio_allocate_interrupt(void) | |
138 | { | |
139 | struct iio_interrupt *i = kmalloc(sizeof *i, GFP_KERNEL); | |
140 | if (i) { | |
141 | spin_lock_init(&i->ev_list_lock); | |
142 | INIT_LIST_HEAD(&i->ev_list); | |
143 | } | |
144 | return i; | |
145 | } | |
146 | ||
147 | /* Confirming the validity of supplied irq is left to drivers.*/ | |
148 | int iio_register_interrupt_line(unsigned int irq, | |
149 | struct iio_dev *dev_info, | |
150 | int line_number, | |
151 | unsigned long type, | |
152 | const char *name) | |
153 | { | |
154 | int ret; | |
155 | ||
156 | dev_info->interrupts[line_number] = iio_allocate_interrupt(); | |
157 | if (dev_info->interrupts[line_number] == NULL) { | |
158 | ret = -ENOMEM; | |
159 | goto error_ret; | |
160 | } | |
161 | dev_info->interrupts[line_number]->line_number = line_number; | |
162 | dev_info->interrupts[line_number]->irq = irq; | |
163 | dev_info->interrupts[line_number]->dev_info = dev_info; | |
164 | ||
165 | /* Possibly only request on demand? | |
166 | * Can see this may complicate the handling of interrupts. | |
167 | * However, with this approach we might end up handling lots of | |
168 | * events no-one cares about.*/ | |
169 | ret = request_irq(irq, | |
170 | &iio_interrupt_handler, | |
171 | type, | |
172 | name, | |
173 | dev_info->interrupts[line_number]); | |
174 | ||
175 | error_ret: | |
176 | return ret; | |
177 | } | |
178 | EXPORT_SYMBOL(iio_register_interrupt_line); | |
179 | ||
180 | /* This turns up an awful lot */ | |
181 | ssize_t iio_read_const_attr(struct device *dev, | |
182 | struct device_attribute *attr, | |
183 | char *buf) | |
184 | { | |
185 | return sprintf(buf, "%s\n", to_iio_const_attr(attr)->string); | |
186 | } | |
187 | EXPORT_SYMBOL(iio_read_const_attr); | |
188 | ||
189 | /* Before this runs the interrupt generator must have been disabled */ | |
190 | void iio_unregister_interrupt_line(struct iio_dev *dev_info, int line_number) | |
191 | { | |
192 | /* make sure the interrupt handlers are all done */ | |
193 | flush_scheduled_work(); | |
194 | free_irq(dev_info->interrupts[line_number]->irq, | |
195 | dev_info->interrupts[line_number]); | |
196 | kfree(dev_info->interrupts[line_number]); | |
197 | } | |
198 | EXPORT_SYMBOL(iio_unregister_interrupt_line); | |
199 | ||
200 | /* Reference counted add and remove */ | |
201 | void iio_add_event_to_list(struct iio_event_handler_list *el, | |
202 | struct list_head *head) | |
203 | { | |
204 | unsigned long flags; | |
205 | struct iio_interrupt *inter = to_iio_interrupt(head); | |
206 | ||
207 | /* take mutex to protect this element */ | |
208 | mutex_lock(&el->exist_lock); | |
209 | if (el->refcount == 0) { | |
210 | /* Take the event list spin lock */ | |
211 | spin_lock_irqsave(&inter->ev_list_lock, flags); | |
212 | list_add(&el->list, head); | |
213 | spin_unlock_irqrestore(&inter->ev_list_lock, flags); | |
214 | } | |
215 | el->refcount++; | |
216 | mutex_unlock(&el->exist_lock); | |
217 | } | |
218 | EXPORT_SYMBOL(iio_add_event_to_list); | |
219 | ||
220 | void iio_remove_event_from_list(struct iio_event_handler_list *el, | |
221 | struct list_head *head) | |
222 | { | |
223 | unsigned long flags; | |
224 | struct iio_interrupt *inter = to_iio_interrupt(head); | |
225 | ||
226 | mutex_lock(&el->exist_lock); | |
227 | el->refcount--; | |
228 | if (el->refcount == 0) { | |
229 | /* Take the event list spin lock */ | |
230 | spin_lock_irqsave(&inter->ev_list_lock, flags); | |
231 | list_del_init(&el->list); | |
232 | spin_unlock_irqrestore(&inter->ev_list_lock, flags); | |
233 | } | |
234 | mutex_unlock(&el->exist_lock); | |
235 | } | |
236 | EXPORT_SYMBOL(iio_remove_event_from_list); | |
237 | ||
77712e5f MB |
238 | static ssize_t iio_event_chrdev_read(struct file *filep, |
239 | char __user *buf, | |
240 | size_t count, | |
241 | loff_t *f_ps) | |
847ec80b JC |
242 | { |
243 | struct iio_event_interface *ev_int = filep->private_data; | |
244 | struct iio_detected_event_list *el; | |
245 | int ret; | |
246 | size_t len; | |
247 | ||
248 | mutex_lock(&ev_int->event_list_lock); | |
249 | if (list_empty(&ev_int->det_events.list)) { | |
250 | if (filep->f_flags & O_NONBLOCK) { | |
251 | ret = -EAGAIN; | |
252 | goto error_mutex_unlock; | |
253 | } | |
254 | mutex_unlock(&ev_int->event_list_lock); | |
255 | /* Blocking on device; waiting for something to be there */ | |
256 | ret = wait_event_interruptible(ev_int->wait, | |
257 | !list_empty(&ev_int | |
258 | ->det_events.list)); | |
259 | if (ret) | |
260 | goto error_ret; | |
25985edc | 261 | /* Single access device so no one else can get the data */ |
847ec80b JC |
262 | mutex_lock(&ev_int->event_list_lock); |
263 | } | |
264 | ||
265 | el = list_first_entry(&ev_int->det_events.list, | |
266 | struct iio_detected_event_list, | |
267 | list); | |
268 | len = sizeof el->ev; | |
269 | if (copy_to_user(buf, &(el->ev), len)) { | |
270 | ret = -EFAULT; | |
271 | goto error_mutex_unlock; | |
272 | } | |
273 | list_del(&el->list); | |
274 | ev_int->current_events--; | |
275 | mutex_unlock(&ev_int->event_list_lock); | |
276 | /* | |
277 | * Possible concurency issue if an update of this event is on its way | |
75b16013 JC |
278 | * through. May lead to new event being removed whilst the reported |
279 | * event was the unescalated event. In typical use case this is not a | |
280 | * problem as userspace will say read half the buffer due to a 50% | |
281 | * full event which would make the correct 100% full incorrect anyway. | |
847ec80b | 282 | */ |
75b16013 JC |
283 | if (el->shared_pointer) { |
284 | spin_lock(&el->shared_pointer->lock); | |
847ec80b | 285 | (el->shared_pointer->ev_p) = NULL; |
75b16013 JC |
286 | spin_unlock(&el->shared_pointer->lock); |
287 | } | |
847ec80b JC |
288 | kfree(el); |
289 | ||
290 | return len; | |
291 | ||
292 | error_mutex_unlock: | |
293 | mutex_unlock(&ev_int->event_list_lock); | |
294 | error_ret: | |
295 | ||
296 | return ret; | |
297 | } | |
298 | ||
77712e5f | 299 | static int iio_event_chrdev_release(struct inode *inode, struct file *filep) |
847ec80b JC |
300 | { |
301 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); | |
302 | struct iio_event_interface *ev_int = hand->private; | |
303 | struct iio_detected_event_list *el, *t; | |
304 | ||
305 | mutex_lock(&ev_int->event_list_lock); | |
306 | clear_bit(IIO_BUSY_BIT_POS, &ev_int->handler.flags); | |
307 | /* | |
308 | * In order to maintain a clean state for reopening, | |
309 | * clear out any awaiting events. The mask will prevent | |
310 | * any new __iio_push_event calls running. | |
311 | */ | |
312 | list_for_each_entry_safe(el, t, &ev_int->det_events.list, list) { | |
313 | list_del(&el->list); | |
314 | kfree(el); | |
315 | } | |
316 | mutex_unlock(&ev_int->event_list_lock); | |
317 | ||
318 | return 0; | |
319 | } | |
320 | ||
77712e5f | 321 | static int iio_event_chrdev_open(struct inode *inode, struct file *filep) |
847ec80b JC |
322 | { |
323 | struct iio_handler *hand = iio_cdev_to_handler(inode->i_cdev); | |
324 | struct iio_event_interface *ev_int = hand->private; | |
325 | ||
326 | mutex_lock(&ev_int->event_list_lock); | |
327 | if (test_and_set_bit(IIO_BUSY_BIT_POS, &hand->flags)) { | |
328 | fops_put(filep->f_op); | |
329 | mutex_unlock(&ev_int->event_list_lock); | |
330 | return -EBUSY; | |
331 | } | |
332 | filep->private_data = hand->private; | |
333 | mutex_unlock(&ev_int->event_list_lock); | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | static const struct file_operations iio_event_chrdev_fileops = { | |
339 | .read = iio_event_chrdev_read, | |
340 | .release = iio_event_chrdev_release, | |
341 | .open = iio_event_chrdev_open, | |
342 | .owner = THIS_MODULE, | |
6038f373 | 343 | .llseek = noop_llseek, |
847ec80b JC |
344 | }; |
345 | ||
346 | static void iio_event_dev_release(struct device *dev) | |
347 | { | |
348 | struct iio_event_interface *ev_int | |
349 | = container_of(dev, struct iio_event_interface, dev); | |
350 | cdev_del(&ev_int->handler.chrdev); | |
351 | iio_device_free_chrdev_minor(MINOR(dev->devt)); | |
352 | }; | |
353 | ||
354 | static struct device_type iio_event_type = { | |
355 | .release = iio_event_dev_release, | |
356 | }; | |
357 | ||
358 | int iio_device_get_chrdev_minor(void) | |
359 | { | |
360 | int ret, val; | |
361 | ||
b156cf70 JC |
362 | ida_again: |
363 | if (unlikely(ida_pre_get(&iio_chrdev_ida, GFP_KERNEL) == 0)) | |
847ec80b | 364 | return -ENOMEM; |
b156cf70 JC |
365 | spin_lock(&iio_ida_lock); |
366 | ret = ida_get_new(&iio_chrdev_ida, &val); | |
367 | spin_unlock(&iio_ida_lock); | |
847ec80b | 368 | if (unlikely(ret == -EAGAIN)) |
b156cf70 | 369 | goto ida_again; |
847ec80b JC |
370 | else if (unlikely(ret)) |
371 | return ret; | |
372 | if (val > IIO_DEV_MAX) | |
373 | return -ENOMEM; | |
374 | return val; | |
375 | } | |
376 | ||
377 | void iio_device_free_chrdev_minor(int val) | |
378 | { | |
b156cf70 JC |
379 | spin_lock(&iio_ida_lock); |
380 | ida_remove(&iio_chrdev_ida, val); | |
381 | spin_unlock(&iio_ida_lock); | |
847ec80b JC |
382 | } |
383 | ||
384 | int iio_setup_ev_int(struct iio_event_interface *ev_int, | |
385 | const char *name, | |
386 | struct module *owner, | |
387 | struct device *dev) | |
388 | { | |
389 | int ret, minor; | |
390 | ||
5aaaeba8 | 391 | ev_int->dev.bus = &iio_bus_type; |
847ec80b JC |
392 | ev_int->dev.parent = dev; |
393 | ev_int->dev.type = &iio_event_type; | |
394 | device_initialize(&ev_int->dev); | |
395 | ||
396 | minor = iio_device_get_chrdev_minor(); | |
397 | if (minor < 0) { | |
398 | ret = minor; | |
399 | goto error_device_put; | |
400 | } | |
401 | ev_int->dev.devt = MKDEV(MAJOR(iio_devt), minor); | |
402 | dev_set_name(&ev_int->dev, "%s", name); | |
403 | ||
404 | ret = device_add(&ev_int->dev); | |
405 | if (ret) | |
406 | goto error_free_minor; | |
407 | ||
408 | cdev_init(&ev_int->handler.chrdev, &iio_event_chrdev_fileops); | |
409 | ev_int->handler.chrdev.owner = owner; | |
410 | ||
411 | mutex_init(&ev_int->event_list_lock); | |
412 | /* discussion point - make this variable? */ | |
413 | ev_int->max_events = 10; | |
414 | ev_int->current_events = 0; | |
415 | INIT_LIST_HEAD(&ev_int->det_events.list); | |
416 | init_waitqueue_head(&ev_int->wait); | |
417 | ev_int->handler.private = ev_int; | |
418 | ev_int->handler.flags = 0; | |
419 | ||
420 | ret = cdev_add(&ev_int->handler.chrdev, ev_int->dev.devt, 1); | |
421 | if (ret) | |
422 | goto error_unreg_device; | |
423 | ||
424 | return 0; | |
425 | ||
426 | error_unreg_device: | |
427 | device_unregister(&ev_int->dev); | |
428 | error_free_minor: | |
429 | iio_device_free_chrdev_minor(minor); | |
430 | error_device_put: | |
431 | put_device(&ev_int->dev); | |
432 | ||
433 | return ret; | |
434 | } | |
435 | ||
436 | void iio_free_ev_int(struct iio_event_interface *ev_int) | |
437 | { | |
438 | device_unregister(&ev_int->dev); | |
439 | put_device(&ev_int->dev); | |
440 | } | |
441 | ||
442 | static int __init iio_dev_init(void) | |
443 | { | |
444 | int err; | |
445 | ||
446 | err = alloc_chrdev_region(&iio_devt, 0, IIO_DEV_MAX, "iio"); | |
447 | if (err < 0) | |
448 | printk(KERN_ERR "%s: failed to allocate char dev region\n", | |
449 | __FILE__); | |
450 | ||
451 | return err; | |
452 | } | |
453 | ||
454 | static void __exit iio_dev_exit(void) | |
455 | { | |
456 | if (iio_devt) | |
457 | unregister_chrdev_region(iio_devt, IIO_DEV_MAX); | |
458 | } | |
459 | ||
460 | static int __init iio_init(void) | |
461 | { | |
462 | int ret; | |
463 | ||
5aaaeba8 JC |
464 | /* Register sysfs bus */ |
465 | ret = bus_register(&iio_bus_type); | |
847ec80b JC |
466 | if (ret < 0) { |
467 | printk(KERN_ERR | |
5aaaeba8 | 468 | "%s could not register bus type\n", |
847ec80b JC |
469 | __FILE__); |
470 | goto error_nothing; | |
471 | } | |
472 | ||
473 | ret = iio_dev_init(); | |
474 | if (ret < 0) | |
5aaaeba8 | 475 | goto error_unregister_bus_type; |
847ec80b JC |
476 | |
477 | return 0; | |
478 | ||
5aaaeba8 JC |
479 | error_unregister_bus_type: |
480 | bus_unregister(&iio_bus_type); | |
847ec80b JC |
481 | error_nothing: |
482 | return ret; | |
483 | } | |
484 | ||
485 | static void __exit iio_exit(void) | |
486 | { | |
487 | iio_dev_exit(); | |
5aaaeba8 | 488 | bus_unregister(&iio_bus_type); |
847ec80b JC |
489 | } |
490 | ||
491 | static int iio_device_register_sysfs(struct iio_dev *dev_info) | |
492 | { | |
493 | int ret = 0; | |
494 | ||
495 | ret = sysfs_create_group(&dev_info->dev.kobj, dev_info->attrs); | |
496 | if (ret) { | |
497 | dev_err(dev_info->dev.parent, | |
498 | "Failed to register sysfs hooks\n"); | |
499 | goto error_ret; | |
500 | } | |
501 | ||
847ec80b JC |
502 | error_ret: |
503 | return ret; | |
504 | } | |
505 | ||
506 | static void iio_device_unregister_sysfs(struct iio_dev *dev_info) | |
507 | { | |
847ec80b JC |
508 | sysfs_remove_group(&dev_info->dev.kobj, dev_info->attrs); |
509 | } | |
510 | ||
f3cdc285 | 511 | /* Return a negative errno on failure */ |
b156cf70 | 512 | int iio_get_new_ida_val(struct ida *this_ida) |
847ec80b JC |
513 | { |
514 | int ret; | |
515 | int val; | |
516 | ||
b156cf70 JC |
517 | ida_again: |
518 | if (unlikely(ida_pre_get(this_ida, GFP_KERNEL) == 0)) | |
847ec80b JC |
519 | return -ENOMEM; |
520 | ||
b156cf70 JC |
521 | spin_lock(&iio_ida_lock); |
522 | ret = ida_get_new(this_ida, &val); | |
523 | spin_unlock(&iio_ida_lock); | |
847ec80b | 524 | if (unlikely(ret == -EAGAIN)) |
b156cf70 | 525 | goto ida_again; |
847ec80b JC |
526 | else if (unlikely(ret)) |
527 | return ret; | |
528 | ||
529 | return val; | |
530 | } | |
b156cf70 | 531 | EXPORT_SYMBOL(iio_get_new_ida_val); |
847ec80b | 532 | |
b156cf70 | 533 | void iio_free_ida_val(struct ida *this_ida, int id) |
847ec80b | 534 | { |
b156cf70 JC |
535 | spin_lock(&iio_ida_lock); |
536 | ida_remove(this_ida, id); | |
537 | spin_unlock(&iio_ida_lock); | |
847ec80b | 538 | } |
b156cf70 | 539 | EXPORT_SYMBOL(iio_free_ida_val); |
847ec80b JC |
540 | |
541 | static int iio_device_register_id(struct iio_dev *dev_info, | |
b156cf70 | 542 | struct ida *this_ida) |
847ec80b | 543 | { |
b156cf70 | 544 | dev_info->id = iio_get_new_ida_val(&iio_ida); |
847ec80b JC |
545 | if (dev_info->id < 0) |
546 | return dev_info->id; | |
547 | return 0; | |
548 | } | |
549 | ||
550 | static void iio_device_unregister_id(struct iio_dev *dev_info) | |
551 | { | |
b156cf70 | 552 | iio_free_ida_val(&iio_ida, dev_info->id); |
847ec80b JC |
553 | } |
554 | ||
555 | static inline int __iio_add_event_config_attrs(struct iio_dev *dev_info, int i) | |
556 | { | |
557 | int ret; | |
558 | /*p for adding, q for removing */ | |
559 | struct attribute **attrp, **attrq; | |
560 | ||
561 | if (dev_info->event_conf_attrs && dev_info->event_conf_attrs[i].attrs) { | |
562 | attrp = dev_info->event_conf_attrs[i].attrs; | |
563 | while (*attrp) { | |
564 | ret = sysfs_add_file_to_group(&dev_info->dev.kobj, | |
565 | *attrp, | |
566 | dev_info | |
567 | ->event_attrs[i].name); | |
568 | if (ret) | |
569 | goto error_ret; | |
570 | attrp++; | |
571 | } | |
572 | } | |
573 | return 0; | |
574 | ||
575 | error_ret: | |
576 | attrq = dev_info->event_conf_attrs[i].attrs; | |
577 | while (attrq != attrp) { | |
578 | sysfs_remove_file_from_group(&dev_info->dev.kobj, | |
579 | *attrq, | |
580 | dev_info->event_attrs[i].name); | |
581 | attrq++; | |
582 | } | |
583 | ||
584 | return ret; | |
585 | } | |
586 | ||
587 | static inline int __iio_remove_event_config_attrs(struct iio_dev *dev_info, | |
588 | int i) | |
589 | { | |
590 | struct attribute **attrq; | |
591 | ||
592 | if (dev_info->event_conf_attrs | |
593 | && dev_info->event_conf_attrs[i].attrs) { | |
594 | attrq = dev_info->event_conf_attrs[i].attrs; | |
595 | while (*attrq) { | |
596 | sysfs_remove_file_from_group(&dev_info->dev.kobj, | |
597 | *attrq, | |
598 | dev_info | |
599 | ->event_attrs[i].name); | |
600 | attrq++; | |
601 | } | |
602 | } | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | static int iio_device_register_eventset(struct iio_dev *dev_info) | |
608 | { | |
609 | int ret = 0, i, j; | |
610 | ||
611 | if (dev_info->num_interrupt_lines == 0) | |
612 | return 0; | |
613 | ||
614 | dev_info->event_interfaces = | |
615 | kzalloc(sizeof(struct iio_event_interface) | |
616 | *dev_info->num_interrupt_lines, | |
617 | GFP_KERNEL); | |
618 | if (dev_info->event_interfaces == NULL) { | |
619 | ret = -ENOMEM; | |
620 | goto error_ret; | |
621 | } | |
622 | ||
623 | dev_info->interrupts = kzalloc(sizeof(struct iio_interrupt *) | |
624 | *dev_info->num_interrupt_lines, | |
625 | GFP_KERNEL); | |
626 | if (dev_info->interrupts == NULL) { | |
627 | ret = -ENOMEM; | |
628 | goto error_free_event_interfaces; | |
629 | } | |
630 | ||
631 | for (i = 0; i < dev_info->num_interrupt_lines; i++) { | |
632 | dev_info->event_interfaces[i].owner = dev_info->driver_module; | |
847ec80b JC |
633 | |
634 | snprintf(dev_info->event_interfaces[i]._name, 20, | |
ba5c6fba JC |
635 | "%s:event%d", |
636 | dev_name(&dev_info->dev), | |
3d550fba | 637 | i); |
847ec80b JC |
638 | |
639 | ret = iio_setup_ev_int(&dev_info->event_interfaces[i], | |
640 | (const char *)(dev_info | |
641 | ->event_interfaces[i] | |
642 | ._name), | |
643 | dev_info->driver_module, | |
644 | &dev_info->dev); | |
645 | if (ret) { | |
646 | dev_err(&dev_info->dev, | |
647 | "Could not get chrdev interface\n"); | |
847ec80b JC |
648 | goto error_free_setup_ev_ints; |
649 | } | |
847ec80b | 650 | |
5cba220b JC |
651 | dev_set_drvdata(&dev_info->event_interfaces[i].dev, |
652 | (void *)dev_info); | |
653 | ret = sysfs_create_group(&dev_info | |
654 | ->event_interfaces[i] | |
655 | .dev.kobj, | |
656 | &dev_info->event_attrs[i]); | |
657 | ||
847ec80b JC |
658 | if (ret) { |
659 | dev_err(&dev_info->dev, | |
660 | "Failed to register sysfs for event attrs"); | |
661 | goto error_remove_sysfs_interfaces; | |
662 | } | |
663 | } | |
664 | ||
665 | for (i = 0; i < dev_info->num_interrupt_lines; i++) { | |
666 | ret = __iio_add_event_config_attrs(dev_info, i); | |
667 | if (ret) | |
668 | goto error_unregister_config_attrs; | |
669 | } | |
670 | ||
671 | return 0; | |
672 | ||
673 | error_unregister_config_attrs: | |
674 | for (j = 0; j < i; j++) | |
675 | __iio_remove_event_config_attrs(dev_info, i); | |
676 | i = dev_info->num_interrupt_lines - 1; | |
677 | error_remove_sysfs_interfaces: | |
678 | for (j = 0; j < i; j++) | |
5cba220b JC |
679 | sysfs_remove_group(&dev_info |
680 | ->event_interfaces[j].dev.kobj, | |
847ec80b | 681 | &dev_info->event_attrs[j]); |
847ec80b | 682 | error_free_setup_ev_ints: |
3d550fba | 683 | for (j = 0; j < i; j++) |
847ec80b | 684 | iio_free_ev_int(&dev_info->event_interfaces[j]); |
847ec80b JC |
685 | kfree(dev_info->interrupts); |
686 | error_free_event_interfaces: | |
687 | kfree(dev_info->event_interfaces); | |
688 | error_ret: | |
689 | ||
690 | return ret; | |
691 | } | |
692 | ||
693 | static void iio_device_unregister_eventset(struct iio_dev *dev_info) | |
694 | { | |
695 | int i; | |
696 | ||
697 | if (dev_info->num_interrupt_lines == 0) | |
698 | return; | |
699 | for (i = 0; i < dev_info->num_interrupt_lines; i++) | |
5cba220b JC |
700 | sysfs_remove_group(&dev_info |
701 | ->event_interfaces[i].dev.kobj, | |
847ec80b JC |
702 | &dev_info->event_attrs[i]); |
703 | ||
3d550fba | 704 | for (i = 0; i < dev_info->num_interrupt_lines; i++) |
847ec80b | 705 | iio_free_ev_int(&dev_info->event_interfaces[i]); |
847ec80b JC |
706 | kfree(dev_info->interrupts); |
707 | kfree(dev_info->event_interfaces); | |
708 | } | |
709 | ||
710 | static void iio_dev_release(struct device *device) | |
711 | { | |
712 | struct iio_dev *dev = to_iio_dev(device); | |
713 | ||
714 | iio_put(); | |
715 | kfree(dev); | |
716 | } | |
717 | ||
718 | static struct device_type iio_dev_type = { | |
719 | .name = "iio_device", | |
720 | .release = iio_dev_release, | |
721 | }; | |
722 | ||
6f7c8ee5 | 723 | struct iio_dev *iio_allocate_device(int sizeof_priv) |
847ec80b | 724 | { |
6f7c8ee5 JC |
725 | struct iio_dev *dev; |
726 | size_t alloc_size; | |
727 | ||
728 | alloc_size = sizeof(struct iio_dev); | |
729 | if (sizeof_priv) { | |
730 | alloc_size = ALIGN(alloc_size, IIO_ALIGN); | |
731 | alloc_size += sizeof_priv; | |
732 | } | |
733 | /* ensure 32-byte alignment of whole construct ? */ | |
734 | alloc_size += IIO_ALIGN - 1; | |
735 | ||
736 | dev = kzalloc(alloc_size, GFP_KERNEL); | |
847ec80b JC |
737 | |
738 | if (dev) { | |
739 | dev->dev.type = &iio_dev_type; | |
5aaaeba8 | 740 | dev->dev.bus = &iio_bus_type; |
847ec80b JC |
741 | device_initialize(&dev->dev); |
742 | dev_set_drvdata(&dev->dev, (void *)dev); | |
743 | mutex_init(&dev->mlock); | |
744 | iio_get(); | |
745 | } | |
746 | ||
747 | return dev; | |
748 | } | |
749 | EXPORT_SYMBOL(iio_allocate_device); | |
750 | ||
751 | void iio_free_device(struct iio_dev *dev) | |
752 | { | |
753 | if (dev) | |
754 | iio_put_device(dev); | |
755 | } | |
756 | EXPORT_SYMBOL(iio_free_device); | |
757 | ||
758 | int iio_device_register(struct iio_dev *dev_info) | |
759 | { | |
760 | int ret; | |
761 | ||
b156cf70 | 762 | ret = iio_device_register_id(dev_info, &iio_ida); |
847ec80b JC |
763 | if (ret) { |
764 | dev_err(&dev_info->dev, "Failed to get id\n"); | |
765 | goto error_ret; | |
766 | } | |
767 | dev_set_name(&dev_info->dev, "device%d", dev_info->id); | |
768 | ||
769 | ret = device_add(&dev_info->dev); | |
770 | if (ret) | |
b156cf70 | 771 | goto error_free_ida; |
847ec80b JC |
772 | ret = iio_device_register_sysfs(dev_info); |
773 | if (ret) { | |
774 | dev_err(dev_info->dev.parent, | |
775 | "Failed to register sysfs interfaces\n"); | |
776 | goto error_del_device; | |
777 | } | |
778 | ret = iio_device_register_eventset(dev_info); | |
779 | if (ret) { | |
780 | dev_err(dev_info->dev.parent, | |
c849d253 | 781 | "Failed to register event set\n"); |
847ec80b JC |
782 | goto error_free_sysfs; |
783 | } | |
784 | if (dev_info->modes & INDIO_RING_TRIGGERED) | |
785 | iio_device_register_trigger_consumer(dev_info); | |
786 | ||
787 | return 0; | |
788 | ||
789 | error_free_sysfs: | |
790 | iio_device_unregister_sysfs(dev_info); | |
791 | error_del_device: | |
792 | device_del(&dev_info->dev); | |
b156cf70 | 793 | error_free_ida: |
847ec80b JC |
794 | iio_device_unregister_id(dev_info); |
795 | error_ret: | |
796 | return ret; | |
797 | } | |
798 | EXPORT_SYMBOL(iio_device_register); | |
799 | ||
800 | void iio_device_unregister(struct iio_dev *dev_info) | |
801 | { | |
802 | if (dev_info->modes & INDIO_RING_TRIGGERED) | |
803 | iio_device_unregister_trigger_consumer(dev_info); | |
804 | iio_device_unregister_eventset(dev_info); | |
805 | iio_device_unregister_sysfs(dev_info); | |
806 | iio_device_unregister_id(dev_info); | |
807 | device_unregister(&dev_info->dev); | |
808 | } | |
809 | EXPORT_SYMBOL(iio_device_unregister); | |
810 | ||
811 | void iio_put(void) | |
812 | { | |
813 | module_put(THIS_MODULE); | |
814 | } | |
815 | ||
816 | void iio_get(void) | |
817 | { | |
818 | __module_get(THIS_MODULE); | |
819 | } | |
820 | ||
821 | subsys_initcall(iio_init); | |
822 | module_exit(iio_exit); | |
823 | ||
824 | MODULE_AUTHOR("Jonathan Cameron <jic23@cam.ac.uk>"); | |
825 | MODULE_DESCRIPTION("Industrial I/O core"); | |
826 | MODULE_LICENSE("GPL"); |