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,
136 .llseek = noop_llseek,
140 * __iio_request_ring_buffer_event_chrdev() allocate ring event chrdev
141 * @buf: ring buffer whose event chrdev we are allocating
142 * @owner: the module who owns the ring buffer (for ref counting)
143 * @dev: device with which the chrdev is associated
146 __iio_request_ring_buffer_event_chrdev(struct iio_ring_buffer *buf,
148 struct module *owner,
153 snprintf(buf->ev_int._name, sizeof(buf->ev_int._name),
157 ret = iio_setup_ev_int(&(buf->ev_int),
170 __iio_free_ring_buffer_event_chrdev(struct iio_ring_buffer *buf)
172 iio_free_ev_int(&(buf->ev_int));
175 static void iio_ring_access_release(struct device *dev)
177 struct iio_ring_buffer *buf
178 = access_dev_to_iio_ring_buffer(dev);
179 cdev_del(&buf->access_handler.chrdev);
180 iio_device_free_chrdev_minor(MINOR(dev->devt));
183 static struct device_type iio_ring_access_type = {
184 .release = iio_ring_access_release,
188 __iio_request_ring_buffer_access_chrdev(struct iio_ring_buffer *buf,
190 struct module *owner)
194 buf->access_handler.flags = 0;
196 buf->access_dev.parent = &buf->dev;
197 buf->access_dev.bus = &iio_bus_type;
198 buf->access_dev.type = &iio_ring_access_type;
199 device_initialize(&buf->access_dev);
201 minor = iio_device_get_chrdev_minor();
204 goto error_device_put;
206 buf->access_dev.devt = MKDEV(MAJOR(iio_devt), minor);
211 dev_set_name(&buf->access_dev, "%s:access%d",
214 ret = device_add(&buf->access_dev);
216 printk(KERN_ERR "failed to add the ring access dev\n");
217 goto error_device_put;
220 cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops);
221 buf->access_handler.chrdev.owner = owner;
223 ret = cdev_add(&buf->access_handler.chrdev, buf->access_dev.devt, 1);
225 printk(KERN_ERR "failed to allocate ring access chrdev\n");
226 goto error_device_unregister;
230 error_device_unregister:
231 device_unregister(&buf->access_dev);
233 put_device(&buf->access_dev);
238 static void __iio_free_ring_buffer_access_chrdev(struct iio_ring_buffer *buf)
240 device_unregister(&buf->access_dev);
243 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
244 struct iio_dev *dev_info)
246 if (ring->access.mark_param_change)
247 ring->access.mark_param_change(ring);
248 ring->indio_dev = dev_info;
249 ring->ev_int.private = ring;
250 ring->access_handler.private = ring;
251 ring->shared_ev_pointer.ev_p = NULL;
252 spin_lock_init(&ring->shared_ev_pointer.lock);
254 EXPORT_SYMBOL(iio_ring_buffer_init);
256 int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
262 dev_set_name(&ring->dev, "%s:buffer%d",
263 dev_name(ring->dev.parent),
265 ret = device_add(&ring->dev);
269 ret = __iio_request_ring_buffer_event_chrdev(ring,
274 goto error_remove_device;
276 ret = __iio_request_ring_buffer_access_chrdev(ring,
281 goto error_free_ring_buffer_event_chrdev;
284 error_free_ring_buffer_event_chrdev:
285 __iio_free_ring_buffer_event_chrdev(ring);
287 device_del(&ring->dev);
291 EXPORT_SYMBOL(iio_ring_buffer_register);
293 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
295 __iio_free_ring_buffer_access_chrdev(ring);
296 __iio_free_ring_buffer_event_chrdev(ring);
297 device_del(&ring->dev);
299 EXPORT_SYMBOL(iio_ring_buffer_unregister);
301 ssize_t iio_read_ring_length(struct device *dev,
302 struct device_attribute *attr,
306 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
308 if (ring->access.get_length)
309 len = sprintf(buf, "%d\n",
310 ring->access.get_length(ring));
314 EXPORT_SYMBOL(iio_read_ring_length);
316 ssize_t iio_write_ring_length(struct device *dev,
317 struct device_attribute *attr,
323 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
324 ret = strict_strtoul(buf, 10, &val);
328 if (ring->access.get_length)
329 if (val == ring->access.get_length(ring))
332 if (ring->access.set_length) {
333 ring->access.set_length(ring, val);
334 if (ring->access.mark_param_change)
335 ring->access.mark_param_change(ring);
340 EXPORT_SYMBOL(iio_write_ring_length);
342 ssize_t iio_read_ring_bps(struct device *dev,
343 struct device_attribute *attr,
347 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
349 if (ring->access.get_bpd)
350 len = sprintf(buf, "%d\n",
351 ring->access.get_bpd(ring));
355 EXPORT_SYMBOL(iio_read_ring_bps);
357 ssize_t iio_store_ring_enable(struct device *dev,
358 struct device_attribute *attr,
363 bool requested_state, current_state;
365 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
366 struct iio_dev *dev_info = ring->indio_dev;
368 mutex_lock(&dev_info->mlock);
369 previous_mode = dev_info->currentmode;
370 requested_state = !(buf[0] == '0');
371 current_state = !!(previous_mode & INDIO_ALL_RING_MODES);
372 if (current_state == requested_state) {
373 printk(KERN_INFO "iio-ring, current state requested again\n");
376 if (requested_state) {
377 if (ring->preenable) {
378 ret = ring->preenable(dev_info);
381 "Buffer not started:"
382 "ring preenable failed\n");
386 if (ring->access.request_update) {
387 ret = ring->access.request_update(ring);
390 "Buffer not started:"
391 "ring parameter update failed\n");
395 if (ring->access.mark_in_use)
396 ring->access.mark_in_use(ring);
397 /* Definitely possible for devices to support both of these.*/
398 if (dev_info->modes & INDIO_RING_TRIGGERED) {
399 if (!dev_info->trig) {
401 "Buffer not started: no trigger\n");
403 if (ring->access.unmark_in_use)
404 ring->access.unmark_in_use(ring);
407 dev_info->currentmode = INDIO_RING_TRIGGERED;
408 } else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER)
409 dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER;
410 else { /* should never be reached */
415 if (ring->postenable) {
417 ret = ring->postenable(dev_info);
420 "Buffer not started:"
421 "postenable failed\n");
422 if (ring->access.unmark_in_use)
423 ring->access.unmark_in_use(ring);
424 dev_info->currentmode = previous_mode;
425 if (ring->postdisable)
426 ring->postdisable(dev_info);
431 if (ring->predisable) {
432 ret = ring->predisable(dev_info);
436 if (ring->access.unmark_in_use)
437 ring->access.unmark_in_use(ring);
438 dev_info->currentmode = INDIO_DIRECT_MODE;
439 if (ring->postdisable) {
440 ret = ring->postdisable(dev_info);
446 mutex_unlock(&dev_info->mlock);
450 mutex_unlock(&dev_info->mlock);
453 EXPORT_SYMBOL(iio_store_ring_enable);
454 ssize_t iio_show_ring_enable(struct device *dev,
455 struct device_attribute *attr,
458 struct iio_ring_buffer *ring = dev_get_drvdata(dev);
459 return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode
460 & INDIO_ALL_RING_MODES));
462 EXPORT_SYMBOL(iio_show_ring_enable);
464 ssize_t iio_scan_el_show(struct device *dev,
465 struct device_attribute *attr,
469 struct iio_dev *indio_dev = dev_get_drvdata(dev);
470 struct iio_scan_el *this_el = to_iio_scan_el(attr);
472 ret = iio_scan_mask_query(indio_dev, this_el->number);
475 return sprintf(buf, "%d\n", ret);
477 EXPORT_SYMBOL(iio_scan_el_show);
479 ssize_t iio_scan_el_store(struct device *dev,
480 struct device_attribute *attr,
486 struct iio_dev *indio_dev = dev_get_drvdata(dev);
487 struct iio_scan_el *this_el = to_iio_scan_el(attr);
489 state = !(buf[0] == '0');
490 mutex_lock(&indio_dev->mlock);
491 if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
495 ret = iio_scan_mask_query(indio_dev, this_el->number);
499 ret = iio_scan_mask_clear(indio_dev, this_el->number);
502 indio_dev->scan_count--;
503 } else if (state && !ret) {
504 ret = iio_scan_mask_set(indio_dev, this_el->number);
507 indio_dev->scan_count++;
509 if (this_el->set_state)
510 ret = this_el->set_state(this_el, indio_dev, state);
512 mutex_unlock(&indio_dev->mlock);
514 return ret ? ret : len;
517 EXPORT_SYMBOL(iio_scan_el_store);
519 ssize_t iio_scan_el_ts_show(struct device *dev,
520 struct device_attribute *attr,
523 struct iio_dev *indio_dev = dev_get_drvdata(dev);
524 return sprintf(buf, "%d\n", indio_dev->scan_timestamp);
526 EXPORT_SYMBOL(iio_scan_el_ts_show);
528 ssize_t iio_scan_el_ts_store(struct device *dev,
529 struct device_attribute *attr,
534 struct iio_dev *indio_dev = dev_get_drvdata(dev);
536 state = !(buf[0] == '0');
537 mutex_lock(&indio_dev->mlock);
538 if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
542 indio_dev->scan_timestamp = state;
544 mutex_unlock(&indio_dev->mlock);
546 return ret ? ret : len;
548 EXPORT_SYMBOL(iio_scan_el_ts_store);