Commit | Line | Data |
---|---|---|
7026ea4b JC |
1 | /* The industrial I/O core - generic ring buffer interfaces. |
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 | ||
10 | #ifndef _IIO_RING_GENERIC_H_ | |
11 | #define _IIO_RING_GENERIC_H_ | |
12 | #include "iio.h" | |
7ae8cf62 | 13 | #include "chrdev.h" |
7026ea4b | 14 | |
2662051e JC |
15 | #ifdef CONFIG_IIO_RING_BUFFER |
16 | ||
7026ea4b | 17 | struct iio_ring_buffer; |
7026ea4b | 18 | |
7026ea4b JC |
19 | /** |
20 | * struct iio_ring_access_funcs - access functions for ring buffers. | |
7026ea4b JC |
21 | * @mark_in_use: reference counting, typically to prevent module removal |
22 | * @unmark_in_use: reduce reference count when no longer using ring buffer | |
23 | * @store_to: actually store stuff to the ring buffer | |
24 | * @read_last: get the last element stored | |
b4281733 | 25 | * @read_first_n: try to get a specified number of elements (must exist) |
7026ea4b JC |
26 | * @mark_param_change: notify ring that some relevant parameter has changed |
27 | * Often this means the underlying storage may need to | |
28 | * change. | |
29 | * @request_update: if a parameter change has been marked, update underlying | |
30 | * storage. | |
c3e5d410 JC |
31 | * @get_bytes_per_datum:get current bytes per datum |
32 | * @set_bytes_per_datum:set number of bytes per datum | |
7026ea4b JC |
33 | * @get_length: get number of datums in ring |
34 | * @set_length: set number of datums in ring | |
35 | * @is_enabled: query if ring is currently being used | |
36 | * @enable: enable the ring | |
37 | * | |
38 | * The purpose of this structure is to make the ring buffer element | |
39 | * modular as event for a given driver, different usecases may require | |
4c572605 | 40 | * different ring designs (space efficiency vs speed for example). |
7026ea4b JC |
41 | * |
42 | * It is worth noting that a given ring implementation may only support a small | |
43 | * proportion of these functions. The core code 'should' cope fine with any of | |
44 | * them not existing. | |
45 | **/ | |
46 | struct iio_ring_access_funcs { | |
47 | void (*mark_in_use)(struct iio_ring_buffer *ring); | |
48 | void (*unmark_in_use)(struct iio_ring_buffer *ring); | |
49 | ||
50 | int (*store_to)(struct iio_ring_buffer *ring, u8 *data, s64 timestamp); | |
51 | int (*read_last)(struct iio_ring_buffer *ring, u8 *data); | |
b4281733 JC |
52 | int (*read_first_n)(struct iio_ring_buffer *ring, |
53 | size_t n, | |
b26a2188 | 54 | char __user *buf); |
7026ea4b JC |
55 | |
56 | int (*mark_param_change)(struct iio_ring_buffer *ring); | |
57 | int (*request_update)(struct iio_ring_buffer *ring); | |
58 | ||
ffcab07a MS |
59 | int (*get_bytes_per_datum)(struct iio_ring_buffer *ring); |
60 | int (*set_bytes_per_datum)(struct iio_ring_buffer *ring, size_t bpd); | |
7026ea4b JC |
61 | int (*get_length)(struct iio_ring_buffer *ring); |
62 | int (*set_length)(struct iio_ring_buffer *ring, int length); | |
63 | ||
64 | int (*is_enabled)(struct iio_ring_buffer *ring); | |
65 | int (*enable)(struct iio_ring_buffer *ring); | |
66 | }; | |
67 | ||
6446e9cd JC |
68 | /** |
69 | * struct iio_ring_setup_ops - buffer setup related callbacks | |
70 | * @preenable: [DRIVER] function to run prior to marking ring enabled | |
71 | * @postenable: [DRIVER] function to run after marking ring enabled | |
72 | * @predisable: [DRIVER] function to run prior to marking ring disabled | |
73 | * @postdisable: [DRIVER] function to run after marking ring disabled | |
74 | */ | |
5565a450 JC |
75 | struct iio_ring_setup_ops { |
76 | int (*preenable)(struct iio_dev *); | |
77 | int (*postenable)(struct iio_dev *); | |
78 | int (*predisable)(struct iio_dev *); | |
79 | int (*postdisable)(struct iio_dev *); | |
80 | }; | |
81 | ||
7026ea4b JC |
82 | /** |
83 | * struct iio_ring_buffer - general ring buffer structure | |
4c572605 | 84 | * @dev: ring buffer device struct |
4c572605 RD |
85 | * @indio_dev: industrial I/O device structure |
86 | * @owner: module that owns the ring buffer (for ref counting) | |
4c572605 | 87 | * @length: [DEVICE] number of datums in ring |
c3e5d410 | 88 | * @bytes_per_datum: [DEVICE] size of individual datum including timestamp |
ad577f8d | 89 | * @bpe: [DEVICE] size of individual channel value |
bf32963c MS |
90 | * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode |
91 | * control method is used | |
92 | * @scan_count: [INTERN] the number of elements in the current scan mode | |
93 | * @scan_mask: [INTERN] bitmask used in masking scan mode elements | |
94 | * @scan_timestamp: [INTERN] does the scan mode include a timestamp | |
4c572605 | 95 | * @access: [DRIVER] ring access functions associated with the |
7026ea4b | 96 | * implementation. |
6356463c | 97 | * @flags: [INTERN] file ops related flags including busy flag. |
8d213f24 | 98 | **/ |
7026ea4b | 99 | struct iio_ring_buffer { |
8d213f24 JC |
100 | struct iio_dev *indio_dev; |
101 | struct module *owner; | |
102 | int length; | |
103 | int bytes_per_datum; | |
104 | int bpe; | |
105 | struct attribute_group *scan_el_attrs; | |
106 | int scan_count; | |
32b5eeca | 107 | long *scan_mask; |
8d213f24 | 108 | bool scan_timestamp; |
5565a450 | 109 | const struct iio_ring_access_funcs *access; |
8d213f24 JC |
110 | const struct iio_ring_setup_ops *setup_ops; |
111 | struct list_head scan_el_dev_attr_list; | |
a7348347 | 112 | |
8d213f24 JC |
113 | wait_queue_head_t pollq; |
114 | bool stufftoread; | |
6356463c | 115 | unsigned long flags; |
1aa04278 | 116 | const struct attribute_group *attrs; |
7026ea4b | 117 | }; |
c3e5d410 JC |
118 | |
119 | /** | |
120 | * iio_ring_buffer_init() - Initialize the buffer structure | |
121 | * @ring: buffer to be initialized | |
122 | * @dev_info: the iio device the buffer is assocated with | |
123 | **/ | |
7026ea4b JC |
124 | void iio_ring_buffer_init(struct iio_ring_buffer *ring, |
125 | struct iio_dev *dev_info); | |
126 | ||
32b5eeca JC |
127 | void iio_ring_buffer_deinit(struct iio_ring_buffer *ring); |
128 | ||
7026ea4b | 129 | /** |
6f2dfb31 | 130 | * __iio_update_ring_buffer() - update common elements of ring buffers |
4c572605 RD |
131 | * @ring: ring buffer that is the event source |
132 | * @bytes_per_datum: size of individual datum including timestamp | |
133 | * @length: number of datums in ring | |
7026ea4b | 134 | **/ |
6f2dfb31 JC |
135 | static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring, |
136 | int bytes_per_datum, int length) | |
7026ea4b | 137 | { |
ffcab07a | 138 | ring->bytes_per_datum = bytes_per_datum; |
7026ea4b | 139 | ring->length = length; |
7026ea4b JC |
140 | } |
141 | ||
32b5eeca | 142 | int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit); |
bf32963c | 143 | |
c3e5d410 JC |
144 | /** |
145 | * iio_scan_mask_set() - set particular bit in the scan mask | |
146 | * @ring: the ring buffer whose scan mask we are interested in | |
147 | * @bit: the bit to be set. | |
148 | **/ | |
32b5eeca | 149 | int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit); |
bf32963c | 150 | |
8d213f24 | 151 | #define to_iio_ring_buffer(d) \ |
7026ea4b | 152 | container_of(d, struct iio_ring_buffer, dev) |
c3e5d410 JC |
153 | |
154 | /** | |
c009f7e4 | 155 | * iio_ring_buffer_register() - register the buffer with IIO core |
1aa04278 | 156 | * @indio_dev: device with the buffer to be registered |
1d892719 | 157 | **/ |
c009f7e4 JC |
158 | int iio_ring_buffer_register(struct iio_dev *indio_dev, |
159 | const struct iio_chan_spec *channels, | |
160 | int num_channels); | |
1d892719 | 161 | |
c3e5d410 JC |
162 | /** |
163 | * iio_ring_buffer_unregister() - unregister the buffer from IIO core | |
1aa04278 | 164 | * @indio_dev: the device with the buffer to be unregistered |
c3e5d410 | 165 | **/ |
1aa04278 | 166 | void iio_ring_buffer_unregister(struct iio_dev *indio_dev); |
7026ea4b | 167 | |
c3e5d410 JC |
168 | /** |
169 | * iio_read_ring_length() - attr func to get number of datums in the buffer | |
170 | **/ | |
7026ea4b JC |
171 | ssize_t iio_read_ring_length(struct device *dev, |
172 | struct device_attribute *attr, | |
173 | char *buf); | |
c3e5d410 JC |
174 | /** |
175 | * iio_write_ring_length() - attr func to set number of datums in the buffer | |
176 | **/ | |
7026ea4b JC |
177 | ssize_t iio_write_ring_length(struct device *dev, |
178 | struct device_attribute *attr, | |
179 | const char *buf, | |
180 | size_t len); | |
c3e5d410 JC |
181 | /** |
182 | * iio_read_ring_bytes_per_datum() - attr for number of bytes in whole datum | |
183 | **/ | |
ffcab07a | 184 | ssize_t iio_read_ring_bytes_per_datum(struct device *dev, |
7026ea4b JC |
185 | struct device_attribute *attr, |
186 | char *buf); | |
c3e5d410 JC |
187 | /** |
188 | * iio_store_ring_enable() - attr to turn the buffer on | |
189 | **/ | |
7026ea4b JC |
190 | ssize_t iio_store_ring_enable(struct device *dev, |
191 | struct device_attribute *attr, | |
192 | const char *buf, | |
193 | size_t len); | |
c3e5d410 JC |
194 | /** |
195 | * iio_show_ring_enable() - attr to see if the buffer is on | |
196 | **/ | |
7026ea4b JC |
197 | ssize_t iio_show_ring_enable(struct device *dev, |
198 | struct device_attribute *attr, | |
199 | char *buf); | |
200 | #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \ | |
201 | iio_read_ring_length, \ | |
202 | iio_write_ring_length) | |
ffcab07a MS |
203 | #define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR, \ |
204 | iio_read_ring_bytes_per_datum, NULL) | |
205 | #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \ | |
7026ea4b JC |
206 | iio_show_ring_enable, \ |
207 | iio_store_ring_enable) | |
5565a450 JC |
208 | |
209 | int iio_sw_ring_preenable(struct iio_dev *indio_dev); | |
210 | ||
2662051e | 211 | #else /* CONFIG_IIO_RING_BUFFER */ |
1d892719 | 212 | |
c009f7e4 JC |
213 | static inline int iio_ring_buffer_register(struct iio_dev *indio_dev, |
214 | struct iio_chan_spec *channels, | |
215 | int num_channels) | |
1d892719 JC |
216 | { |
217 | return 0; | |
218 | } | |
219 | ||
1aa04278 | 220 | static inline void iio_ring_buffer_unregister(struct iio_dev *indio_dev) |
2662051e JC |
221 | {}; |
222 | ||
223 | #endif /* CONFIG_IIO_RING_BUFFER */ | |
7026ea4b JC |
224 | |
225 | #endif /* _IIO_RING_GENERIC_H_ */ |