Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
industrialio-triggered-buffer.c
Go to the documentation of this file.
1  /*
2  * Copyright (c) 2012 Analog Devices, Inc.
3  * Author: Lars-Peter Clausen <[email protected]>
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 
10 #include <linux/kernel.h>
11 #include <linux/export.h>
12 #include <linux/module.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/kfifo_buf.h>
18 
19 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
20  .preenable = &iio_sw_buffer_preenable,
21  .postenable = &iio_triggered_buffer_postenable,
22  .predisable = &iio_triggered_buffer_predisable,
23 };
24 
45 int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
46  irqreturn_t (*pollfunc_bh)(int irq, void *p),
47  irqreturn_t (*pollfunc_th)(int irq, void *p),
48  const struct iio_buffer_setup_ops *setup_ops)
49 {
50  int ret;
51 
52  indio_dev->buffer = iio_kfifo_allocate(indio_dev);
53  if (!indio_dev->buffer) {
54  ret = -ENOMEM;
55  goto error_ret;
56  }
57 
58  indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh,
59  pollfunc_th,
61  indio_dev,
62  "%s_consumer%d",
63  indio_dev->name,
64  indio_dev->id);
65  if (indio_dev->pollfunc == NULL) {
66  ret = -ENOMEM;
67  goto error_kfifo_free;
68  }
69 
70  /* Ring buffer functions - here trigger setup related */
71  if (setup_ops)
72  indio_dev->setup_ops = setup_ops;
73  else
74  indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
75 
76  /* Flag that polled ring buffering is possible */
77  indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
78 
79  ret = iio_buffer_register(indio_dev,
80  indio_dev->channels,
81  indio_dev->num_channels);
82  if (ret)
83  goto error_dealloc_pollfunc;
84 
85  return 0;
86 
87 error_dealloc_pollfunc:
88  iio_dealloc_pollfunc(indio_dev->pollfunc);
89 error_kfifo_free:
90  iio_kfifo_free(indio_dev->buffer);
91 error_ret:
92  return ret;
93 }
95 
100 void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
101 {
102  iio_buffer_unregister(indio_dev);
103  iio_dealloc_pollfunc(indio_dev->pollfunc);
104  iio_kfifo_free(indio_dev->buffer);
105 }
107 
108 MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
109 MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
110 MODULE_LICENSE("GPL");