]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/iio/ring_generic.h
6124353b2ed1ae1ef766c67dda40e1b3097b58c6
[karo-tx-linux.git] / drivers / staging / iio / ring_generic.h
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"
13
14 #ifdef CONFIG_IIO_RING_BUFFER
15
16 struct iio_handler;
17 struct iio_ring_buffer;
18 struct iio_dev;
19
20 /**
21  * iio_push_ring_event() - ring buffer specific push to event chrdev
22  * @ring_buf:           ring buffer that is the event source
23  * @event_code:         event indentification code
24  * @timestamp:          time of event
25  **/
26 int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
27                         int event_code,
28                         s64 timestamp);
29 /**
30  * iio_push_or_escallate_ring_event() - escalate or add as appropriate
31  * @ring_buf:           ring buffer that is the event source
32  * @event_code:         event indentification code
33  * @timestamp:          time of event
34  *
35  * Typical usecase is to escalate a 50% ring full to 75% full if noone has yet
36  * read the first event. Clearly the 50% full is no longer of interest in
37  * typical use case.
38  **/
39 int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
40                                      int event_code,
41                                      s64 timestamp);
42
43 /**
44  * struct iio_ring_access_funcs - access functions for ring buffers.
45  * @mark_in_use:        reference counting, typically to prevent module removal
46  * @unmark_in_use:      reduce reference count when no longer using ring buffer
47  * @store_to:           actually store stuff to the ring buffer
48  * @read_last:          get the last element stored
49  * @rip_lots:           try to get a specified number of elements (must exist)
50  * @mark_param_change:  notify ring that some relevant parameter has changed
51  *                      Often this means the underlying storage may need to
52  *                      change.
53  * @request_update:     if a parameter change has been marked, update underlying
54  *                      storage.
55  * @get_bytes_per_datum:                get current bytes per datum
56  * @set_bytes_per_datum:                set number of bytes per datum
57  * @get_length:         get number of datums in ring
58  * @set_length:         set number of datums in ring
59  * @is_enabled:         query if ring is currently being used
60  * @enable:             enable the ring
61  *
62  * The purpose of this structure is to make the ring buffer element
63  * modular as event for a given driver, different usecases may require
64  * different ring designs (space efficiency vs speed for example).
65  *
66  * It is worth noting that a given ring implementation may only support a small
67  * proportion of these functions.  The core code 'should' cope fine with any of
68  * them not existing.
69  **/
70 struct iio_ring_access_funcs {
71         void (*mark_in_use)(struct iio_ring_buffer *ring);
72         void (*unmark_in_use)(struct iio_ring_buffer *ring);
73
74         int (*store_to)(struct iio_ring_buffer *ring, u8 *data, s64 timestamp);
75         int (*read_last)(struct iio_ring_buffer *ring, u8 *data);
76         int (*rip_lots)(struct iio_ring_buffer *ring,
77                         size_t count,
78                         u8 **data,
79                         int *dead_offset);
80
81         int (*mark_param_change)(struct iio_ring_buffer *ring);
82         int (*request_update)(struct iio_ring_buffer *ring);
83
84         int (*get_bytes_per_datum)(struct iio_ring_buffer *ring);
85         int (*set_bytes_per_datum)(struct iio_ring_buffer *ring, size_t bpd);
86         int (*get_length)(struct iio_ring_buffer *ring);
87         int (*set_length)(struct iio_ring_buffer *ring, int length);
88
89         int (*is_enabled)(struct iio_ring_buffer *ring);
90         int (*enable)(struct iio_ring_buffer *ring);
91 };
92
93 /**
94  * struct iio_ring_buffer - general ring buffer structure
95  * @dev:                ring buffer device struct
96  * @access_dev:         system device struct for the chrdev
97  * @indio_dev:          industrial I/O device structure
98  * @owner:              module that owns the ring buffer (for ref counting)
99  * @id:                 unique id number
100  * @access_id:          device id number
101  * @length:             [DEVICE] number of datums in ring
102  * @bytes_per_datum     [DEVICE] size of individual datum including timestamp
103  * @bpe:                [DEVICE] size of individual channel value
104  * @loopcount:          [INTERN] number of times the ring has looped
105  * @scan_el_attrs:      [DRIVER] control of scan elements if that scan mode
106  *                      control method is used
107  * @scan_count: [INTERN] the number of elements in the current scan mode
108  * @scan_mask:          [INTERN] bitmask used in masking scan mode elements
109  * @scan_timestamp:     [INTERN] does the scan mode include a timestamp
110  * @access_handler:     [INTERN] chrdev access handling
111  * @ev_int:             [INTERN] chrdev interface for the event chrdev
112  * @shared_ev_pointer:  [INTERN] the shared event pointer to allow escalation of
113  *                      events
114  * @access:             [DRIVER] ring access functions associated with the
115  *                      implementation.
116  * @preenable:          [DRIVER] function to run prior to marking ring enabled
117  * @postenable:         [DRIVER] function to run after marking ring enabled
118  * @predisable:         [DRIVER] function to run prior to marking ring disabled
119  * @postdisable:        [DRIVER] function to run after marking ring disabled
120   **/
121 struct iio_ring_buffer {
122         struct device dev;
123         struct device access_dev;
124         struct iio_dev *indio_dev;
125         struct module *owner;
126         int                             id;
127         int                             access_id;
128         int                             length;
129         int                             bytes_per_datum;
130         int                             bpe;
131         int                             loopcount;
132         struct attribute_group          *scan_el_attrs;
133         int                             scan_count;
134         u32                             scan_mask;
135         bool                            scan_timestamp;
136         struct iio_handler              access_handler;
137         struct iio_event_interface      ev_int;
138         struct iio_shared_ev_pointer    shared_ev_pointer;
139         struct iio_ring_access_funcs    access;
140         int                             (*preenable)(struct iio_dev *);
141         int                             (*postenable)(struct iio_dev *);
142         int                             (*predisable)(struct iio_dev *);
143         int                             (*postdisable)(struct iio_dev *);
144
145 };
146 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
147                           struct iio_dev *dev_info);
148
149 /**
150  * __iio_update_ring_buffer() - update common elements of ring buffers
151  * @ring:               ring buffer that is the event source
152  * @bytes_per_datum:    size of individual datum including timestamp
153  * @length:             number of datums in ring
154  **/
155 static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
156                                             int bytes_per_datum, int length)
157 {
158         ring->bytes_per_datum = bytes_per_datum;
159         ring->length = length;
160         ring->loopcount = 0;
161 }
162
163 /**
164  * struct iio_scan_el - an individual element of a scan
165  * @dev_attr:           control attribute (if directly controllable)
166  * @number:             unique identifier of element (used for bit mask)
167  * @bit_count:          number of bits in scan element
168  * @label:              useful data for the scan el (often reg address)
169  * @set_state:          for some devices datardy signals are generated
170  *                      for any enabled lines.  This allows unwanted lines
171  *                      to be disabled and hence not get in the way.
172  **/
173 struct iio_scan_el {
174         struct device_attribute         dev_attr;
175         unsigned int                    number;
176         int                             bit_count;
177         unsigned int                    label;
178
179         int (*set_state)(struct iio_scan_el *scanel,
180                          struct iio_dev *dev_info,
181                          bool state);
182 };
183
184 #define to_iio_scan_el(_dev_attr)                               \
185         container_of(_dev_attr, struct iio_scan_el, dev_attr);
186
187 /**
188  * iio_scan_el_store() - sysfs scan element selection interface
189  * @dev: the target device
190  * @attr: the device attribute that is being processed
191  * @buf: input from userspace
192  * @len: length of input
193  *
194  * A generic function used to enable various scan elements.  In some
195  * devices explicit read commands for each channel mean this is merely
196  * a software switch.  In others this must actively disable the channel.
197  * Complexities occur when this interacts with data ready type triggers
198  * which may not reset unless every channel that is enabled is explicitly
199  * read.
200  **/
201 ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
202                           const char *buf, size_t len);
203 /**
204  * iio_scal_el_show() - sysfs interface to query whether a scan element is
205  *                      is enabled or not
206  * @dev: the target device
207  * @attr: the device attribute that is being processed
208  * @buf: output buffer
209  **/
210 ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
211                          char *buf);
212
213 ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
214                              const char *buf, size_t len);
215
216 ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
217                             char *buf);
218 /**
219  * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
220  *
221  * @_name:      identifying name. Resulting struct is iio_scan_el_##_name,
222  *              sysfs element, _name##_en.
223  * @_number:    unique id number for the scan element.
224  * @_bits:      number of bits in the scan element result (used in mixed bit
225  *              length devices).
226  * @_label:     indentification variable used by drivers.  Often a reg address.
227  * @_controlfunc: function used to notify hardware of whether state changes
228  **/
229 #define __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)    \
230         struct iio_scan_el iio_scan_el_##_name = {                      \
231                 .dev_attr = __ATTR(_number##_##_name##_en,              \
232                                    S_IRUGO | S_IWUSR,                   \
233                                    iio_scan_el_show,                    \
234                                    iio_scan_el_store),                  \
235                 .number =  _number,                                     \
236                 .bit_count = _bits,                                     \
237                 .label = _label,                                        \
238                 .set_state = _controlfunc,                              \
239         }
240
241 #define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)      \
242         __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)
243
244 #define __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
245         struct iio_scan_el iio_scan_el_##_name = {                      \
246                 .dev_attr = __ATTR(_number##_##_string##_en,            \
247                                    S_IRUGO | S_IWUSR,                   \
248                                    iio_scan_el_show,                    \
249                                    iio_scan_el_store),                  \
250                 .number =  _number,                                     \
251                 .bit_count = _bits,                                     \
252                 .label = _label,                                        \
253                 .set_state = _cf,                                       \
254         }
255 #define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
256         __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf)
257 /**
258  * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
259  *
260  * Odd one out. Handled slightly differently from other scan elements.
261  **/
262 #define IIO_SCAN_EL_TIMESTAMP(number)                           \
263         struct iio_scan_el iio_scan_el_timestamp = {            \
264                 .dev_attr = __ATTR(number##_timestamp_en,       \
265                                    S_IRUGO | S_IWUSR,           \
266                                    iio_scan_el_ts_show,         \
267                                    iio_scan_el_ts_store),       \
268         }
269
270 /*
271  * These are mainly provided to allow for a change of implementation if a device
272  * has a large number of scan elements
273  */
274 #define IIO_MAX_SCAN_LENGTH 31
275
276 /* note 0 used as error indicator as it doesn't make sense. */
277 static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
278 {
279         while (*av_masks) {
280                 if (!(~*av_masks & mask))
281                         return *av_masks;
282                 av_masks++;
283         }
284         return 0;
285 }
286
287 static inline int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit)
288 {
289         struct iio_dev *dev_info = ring->indio_dev;
290         u32 mask;
291
292         if (bit > IIO_MAX_SCAN_LENGTH)
293                 return -EINVAL;
294
295         if (!ring->scan_mask)
296                 return 0;
297
298         if (dev_info->available_scan_masks)
299                 mask = iio_scan_mask_match(dev_info->available_scan_masks,
300                                         ring->scan_mask);
301         else
302                 mask = ring->scan_mask;
303
304         if (!mask)
305                 return -EINVAL;
306
307         return !!(mask & (1 << bit));
308 };
309
310 static inline int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit)
311 {
312         struct iio_dev *dev_info = ring->indio_dev;
313         u32 mask;
314         u32 trialmask = ring->scan_mask | (1 << bit);
315
316         if (bit > IIO_MAX_SCAN_LENGTH)
317                 return -EINVAL;
318         if (dev_info->available_scan_masks) {
319                 mask = iio_scan_mask_match(dev_info->available_scan_masks,
320                                         trialmask);
321                 if (!mask)
322                         return -EINVAL;
323         }
324         ring->scan_mask = trialmask;
325         ring->scan_count++;
326
327         return 0;
328 };
329
330 static inline int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit)
331 {
332         if (bit > IIO_MAX_SCAN_LENGTH)
333                 return -EINVAL;
334         ring->scan_mask &= ~(1 << bit);
335         ring->scan_count--;
336         return 0;
337 };
338
339 /**
340  * iio_scan_mask_count_to_right() - how many scan elements occur before here
341  * @dev_info: the iio_device whose scan mode we are querying
342  * @bit: which number scan element is this
343  **/
344 static inline int iio_scan_mask_count_to_right(struct iio_ring_buffer *ring,
345                                                 int bit)
346 {
347         int count = 0;
348         int mask = (1 << bit);
349         if (bit > IIO_MAX_SCAN_LENGTH)
350                 return -EINVAL;
351         while (mask) {
352                 mask >>= 1;
353                 if (mask & ring->scan_mask)
354                         count++;
355         }
356
357         return count;
358 }
359
360
361 static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
362 {
363         put_device(&ring->dev);
364 };
365
366 #define to_iio_ring_buffer(d)                   \
367         container_of(d, struct iio_ring_buffer, dev)
368 #define access_dev_to_iio_ring_buffer(d)                        \
369         container_of(d, struct iio_ring_buffer, access_dev)
370 int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
371 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
372
373 ssize_t iio_read_ring_length(struct device *dev,
374                              struct device_attribute *attr,
375                              char *buf);
376 ssize_t iio_write_ring_length(struct device *dev,
377                               struct device_attribute *attr,
378                               const char *buf,
379                               size_t len);
380 ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
381                           struct device_attribute *attr,
382                           char *buf);
383 ssize_t iio_store_ring_enable(struct device *dev,
384                               struct device_attribute *attr,
385                               const char *buf,
386                               size_t len);
387 ssize_t iio_show_ring_enable(struct device *dev,
388                              struct device_attribute *attr,
389                              char *buf);
390 #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR,     \
391                                          iio_read_ring_length,          \
392                                          iio_write_ring_length)
393 #define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR,   \
394                                       iio_read_ring_bytes_per_datum, NULL)
395 #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
396                                          iio_show_ring_enable,          \
397                                          iio_store_ring_enable)
398 #else /* CONFIG_IIO_RING_BUFFER */
399 static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
400 {
401         return 0;
402 };
403 static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
404 {};
405
406 #endif /* CONFIG_IIO_RING_BUFFER */
407
408 #endif /* _IIO_RING_GENERIC_H_ */