1 /* The industrial I/O core
3 * Copyright (c) 2008 Jonathan Cameron
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.
9 * Handling of ring allocation / resizing.
12 * Things to look at here.
13 * - Better memory allocation techniques?
14 * - Alternative access techniques?
16 #include <linux/kernel.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
20 #include <linux/poll.h>
21 #include <linux/module.h>
22 #include <linux/cdev.h>
23 #include <linux/slab.h>
26 #include "ring_generic.h"
28 int iio_push_ring_event(struct iio_ring_buffer
*ring_buf
,
32 return __iio_push_event(&ring_buf
->ev_int
,
35 &ring_buf
->shared_ev_pointer
);
37 EXPORT_SYMBOL(iio_push_ring_event
);
39 int iio_push_or_escallate_ring_event(struct iio_ring_buffer
*ring_buf
,
43 if (ring_buf
->shared_ev_pointer
.ev_p
)
44 __iio_change_event(ring_buf
->shared_ev_pointer
.ev_p
,
48 return iio_push_ring_event(ring_buf
,
53 EXPORT_SYMBOL(iio_push_or_escallate_ring_event
);
56 * iio_ring_open() chrdev file open for ring buffer access
58 * This function relies on all ring buffer implementations having an
59 * iio_ring_buffer as their first element.
61 static int iio_ring_open(struct inode
*inode
, struct file
*filp
)
63 struct iio_handler
*hand
64 = container_of(inode
->i_cdev
, struct iio_handler
, chrdev
);
65 struct iio_ring_buffer
*rb
= hand
->private;
67 filp
->private_data
= hand
->private;
68 if (rb
->access
.mark_in_use
)
69 rb
->access
.mark_in_use(rb
);
75 * iio_ring_release() -chrdev file close ring buffer access
77 * This function relies on all ring buffer implementations having an
78 * iio_ring_buffer as their first element.
80 static int iio_ring_release(struct inode
*inode
, struct file
*filp
)
82 struct cdev
*cd
= inode
->i_cdev
;
83 struct iio_handler
*hand
= iio_cdev_to_handler(cd
);
84 struct iio_ring_buffer
*rb
= hand
->private;
86 clear_bit(IIO_BUSY_BIT_POS
, &rb
->access_handler
.flags
);
87 if (rb
->access
.unmark_in_use
)
88 rb
->access
.unmark_in_use(rb
);
94 * iio_ring_rip_outer() chrdev read for ring buffer access
96 * This function relies on all ring buffer implementations having an
97 * iio_ring _bufer as their first element.
99 static ssize_t
iio_ring_rip_outer(struct file
*filp
, char __user
*buf
,
100 size_t count
, loff_t
*f_ps
)
102 struct iio_ring_buffer
*rb
= filp
->private_data
;
103 int ret
, dead_offset
, copied
;
105 /* rip lots must exist. */
106 if (!rb
->access
.rip_lots
)
108 copied
= rb
->access
.rip_lots(rb
, count
, &data
, &dead_offset
);
114 if (copy_to_user(buf
, data
+ dead_offset
, copied
)) {
116 goto error_free_data_cpy
;
118 /* In clever ring buffer designs this may not need to be freed.
119 * When such a design exists I'll add this to ring access funcs.
131 static const struct file_operations iio_ring_fileops
= {
132 .read
= iio_ring_rip_outer
,
133 .release
= iio_ring_release
,
134 .open
= iio_ring_open
,
135 .owner
= THIS_MODULE
,
139 * __iio_request_ring_buffer_event_chrdev() allocate ring event chrdev
140 * @buf: ring buffer whose event chrdev we are allocating
141 * @owner: the module who owns the ring buffer (for ref counting)
142 * @dev: device with which the chrdev is associated
145 __iio_request_ring_buffer_event_chrdev(struct iio_ring_buffer
*buf
,
147 struct module
*owner
,
152 snprintf(buf
->ev_int
._name
, sizeof(buf
->ev_int
._name
),
156 ret
= iio_setup_ev_int(&(buf
->ev_int
),
169 __iio_free_ring_buffer_event_chrdev(struct iio_ring_buffer
*buf
)
171 iio_free_ev_int(&(buf
->ev_int
));
174 static void iio_ring_access_release(struct device
*dev
)
176 struct iio_ring_buffer
*buf
177 = access_dev_to_iio_ring_buffer(dev
);
178 cdev_del(&buf
->access_handler
.chrdev
);
179 iio_device_free_chrdev_minor(MINOR(dev
->devt
));
182 static struct device_type iio_ring_access_type
= {
183 .release
= iio_ring_access_release
,
187 __iio_request_ring_buffer_access_chrdev(struct iio_ring_buffer
*buf
,
189 struct module
*owner
)
193 buf
->access_handler
.flags
= 0;
195 buf
->access_dev
.parent
= &buf
->dev
;
196 buf
->access_dev
.bus
= &iio_bus_type
;
197 buf
->access_dev
.type
= &iio_ring_access_type
;
198 device_initialize(&buf
->access_dev
);
200 minor
= iio_device_get_chrdev_minor();
203 goto error_device_put
;
205 buf
->access_dev
.devt
= MKDEV(MAJOR(iio_devt
), minor
);
210 dev_set_name(&buf
->access_dev
, "%s:access%d",
213 ret
= device_add(&buf
->access_dev
);
215 printk(KERN_ERR
"failed to add the ring access dev\n");
216 goto error_device_put
;
219 cdev_init(&buf
->access_handler
.chrdev
, &iio_ring_fileops
);
220 buf
->access_handler
.chrdev
.owner
= owner
;
222 ret
= cdev_add(&buf
->access_handler
.chrdev
, buf
->access_dev
.devt
, 1);
224 printk(KERN_ERR
"failed to allocate ring access chrdev\n");
225 goto error_device_unregister
;
229 error_device_unregister
:
230 device_unregister(&buf
->access_dev
);
232 put_device(&buf
->access_dev
);
237 static void __iio_free_ring_buffer_access_chrdev(struct iio_ring_buffer
*buf
)
239 device_unregister(&buf
->access_dev
);
242 void iio_ring_buffer_init(struct iio_ring_buffer
*ring
,
243 struct iio_dev
*dev_info
)
245 if (ring
->access
.mark_param_change
)
246 ring
->access
.mark_param_change(ring
);
247 ring
->indio_dev
= dev_info
;
248 ring
->ev_int
.private = ring
;
249 ring
->access_handler
.private = ring
;
250 ring
->shared_ev_pointer
.ev_p
= NULL
;
251 spin_lock_init(&ring
->shared_ev_pointer
.lock
);
253 EXPORT_SYMBOL(iio_ring_buffer_init
);
255 int iio_ring_buffer_register(struct iio_ring_buffer
*ring
, int id
)
261 dev_set_name(&ring
->dev
, "%s:buffer%d",
262 dev_name(ring
->dev
.parent
),
264 ret
= device_add(&ring
->dev
);
268 ret
= __iio_request_ring_buffer_event_chrdev(ring
,
273 goto error_remove_device
;
275 ret
= __iio_request_ring_buffer_access_chrdev(ring
,
280 goto error_free_ring_buffer_event_chrdev
;
283 error_free_ring_buffer_event_chrdev
:
284 __iio_free_ring_buffer_event_chrdev(ring
);
286 device_del(&ring
->dev
);
290 EXPORT_SYMBOL(iio_ring_buffer_register
);
292 void iio_ring_buffer_unregister(struct iio_ring_buffer
*ring
)
294 __iio_free_ring_buffer_access_chrdev(ring
);
295 __iio_free_ring_buffer_event_chrdev(ring
);
296 device_del(&ring
->dev
);
298 EXPORT_SYMBOL(iio_ring_buffer_unregister
);
300 ssize_t
iio_read_ring_length(struct device
*dev
,
301 struct device_attribute
*attr
,
305 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
307 if (ring
->access
.get_length
)
308 len
= sprintf(buf
, "%d\n",
309 ring
->access
.get_length(ring
));
313 EXPORT_SYMBOL(iio_read_ring_length
);
315 ssize_t
iio_write_ring_length(struct device
*dev
,
316 struct device_attribute
*attr
,
322 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
323 ret
= strict_strtoul(buf
, 10, &val
);
327 if (ring
->access
.get_length
)
328 if (val
== ring
->access
.get_length(ring
))
331 if (ring
->access
.set_length
) {
332 ring
->access
.set_length(ring
, val
);
333 if (ring
->access
.mark_param_change
)
334 ring
->access
.mark_param_change(ring
);
339 EXPORT_SYMBOL(iio_write_ring_length
);
341 ssize_t
iio_read_ring_bps(struct device
*dev
,
342 struct device_attribute
*attr
,
346 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
348 if (ring
->access
.get_bpd
)
349 len
= sprintf(buf
, "%d\n",
350 ring
->access
.get_bpd(ring
));
354 EXPORT_SYMBOL(iio_read_ring_bps
);
356 ssize_t
iio_store_ring_enable(struct device
*dev
,
357 struct device_attribute
*attr
,
362 bool requested_state
, current_state
;
364 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
365 struct iio_dev
*dev_info
= ring
->indio_dev
;
367 mutex_lock(&dev_info
->mlock
);
368 previous_mode
= dev_info
->currentmode
;
369 requested_state
= !(buf
[0] == '0');
370 current_state
= !!(previous_mode
& INDIO_ALL_RING_MODES
);
371 if (current_state
== requested_state
) {
372 printk(KERN_INFO
"iio-ring, current state requested again\n");
375 if (requested_state
) {
376 if (ring
->preenable
) {
377 ret
= ring
->preenable(dev_info
);
380 "Buffer not started:"
381 "ring preenable failed\n");
385 if (ring
->access
.request_update
) {
386 ret
= ring
->access
.request_update(ring
);
389 "Buffer not started:"
390 "ring parameter update failed\n");
394 if (ring
->access
.mark_in_use
)
395 ring
->access
.mark_in_use(ring
);
396 /* Definitely possible for devices to support both of these.*/
397 if (dev_info
->modes
& INDIO_RING_TRIGGERED
) {
398 if (!dev_info
->trig
) {
400 "Buffer not started: no trigger\n");
402 if (ring
->access
.unmark_in_use
)
403 ring
->access
.unmark_in_use(ring
);
406 dev_info
->currentmode
= INDIO_RING_TRIGGERED
;
407 } else if (dev_info
->modes
& INDIO_RING_HARDWARE_BUFFER
)
408 dev_info
->currentmode
= INDIO_RING_HARDWARE_BUFFER
;
409 else { /* should never be reached */
414 if (ring
->postenable
) {
416 ret
= ring
->postenable(dev_info
);
419 "Buffer not started:"
420 "postenable failed\n");
421 if (ring
->access
.unmark_in_use
)
422 ring
->access
.unmark_in_use(ring
);
423 dev_info
->currentmode
= previous_mode
;
424 if (ring
->postdisable
)
425 ring
->postdisable(dev_info
);
430 if (ring
->predisable
) {
431 ret
= ring
->predisable(dev_info
);
435 if (ring
->access
.unmark_in_use
)
436 ring
->access
.unmark_in_use(ring
);
437 dev_info
->currentmode
= INDIO_DIRECT_MODE
;
438 if (ring
->postdisable
) {
439 ret
= ring
->postdisable(dev_info
);
445 mutex_unlock(&dev_info
->mlock
);
449 mutex_unlock(&dev_info
->mlock
);
452 EXPORT_SYMBOL(iio_store_ring_enable
);
453 ssize_t
iio_show_ring_enable(struct device
*dev
,
454 struct device_attribute
*attr
,
457 struct iio_ring_buffer
*ring
= dev_get_drvdata(dev
);
458 return sprintf(buf
, "%d\n", !!(ring
->indio_dev
->currentmode
459 & INDIO_ALL_RING_MODES
));
461 EXPORT_SYMBOL(iio_show_ring_enable
);
463 ssize_t
iio_scan_el_show(struct device
*dev
,
464 struct device_attribute
*attr
,
468 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
469 struct iio_scan_el
*this_el
= to_iio_scan_el(attr
);
471 ret
= iio_scan_mask_query(indio_dev
, this_el
->number
);
474 return sprintf(buf
, "%d\n", ret
);
476 EXPORT_SYMBOL(iio_scan_el_show
);
478 ssize_t
iio_scan_el_store(struct device
*dev
,
479 struct device_attribute
*attr
,
485 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
486 struct iio_scan_el
*this_el
= to_iio_scan_el(attr
);
488 state
= !(buf
[0] == '0');
489 mutex_lock(&indio_dev
->mlock
);
490 if (indio_dev
->currentmode
== INDIO_RING_TRIGGERED
) {
494 ret
= iio_scan_mask_query(indio_dev
, this_el
->number
);
498 ret
= iio_scan_mask_clear(indio_dev
, this_el
->number
);
501 indio_dev
->scan_count
--;
502 } else if (state
&& !ret
) {
503 ret
= iio_scan_mask_set(indio_dev
, this_el
->number
);
506 indio_dev
->scan_count
++;
508 if (this_el
->set_state
)
509 ret
= this_el
->set_state(this_el
, indio_dev
, state
);
511 mutex_unlock(&indio_dev
->mlock
);
513 return ret
? ret
: len
;
516 EXPORT_SYMBOL(iio_scan_el_store
);
518 ssize_t
iio_scan_el_ts_show(struct device
*dev
,
519 struct device_attribute
*attr
,
522 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
523 return sprintf(buf
, "%d\n", indio_dev
->scan_timestamp
);
525 EXPORT_SYMBOL(iio_scan_el_ts_show
);
527 ssize_t
iio_scan_el_ts_store(struct device
*dev
,
528 struct device_attribute
*attr
,
533 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
535 state
= !(buf
[0] == '0');
536 mutex_lock(&indio_dev
->mlock
);
537 if (indio_dev
->currentmode
== INDIO_RING_TRIGGERED
) {
541 indio_dev
->scan_timestamp
= state
;
543 mutex_unlock(&indio_dev
->mlock
);
545 return ret
? ret
: len
;
547 EXPORT_SYMBOL(iio_scan_el_ts_store
);