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