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