Merge branch 'stable/xen-swiotlb-0.8.6' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.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_bpd:            get current bytes per datum
56  * @set_bpd:            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_bpd)(struct iio_ring_buffer *ring);
85         int (*set_bpd)(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  * @bpd:                [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  * @access_handler:     [INTERN] chrdev access handling
106  * @ev_int:             [INTERN] chrdev interface for the event chrdev
107  * @shared_ev_pointer:  [INTERN] the shared event pointer to allow escalation of
108  *                      events
109  * @access:             [DRIVER] ring access functions associated with the
110  *                      implementation.
111  * @preenable:          [DRIVER] function to run prior to marking ring enabled
112  * @postenable:         [DRIVER] function to run after marking ring enabled
113  * @predisable:         [DRIVER] function to run prior to marking ring disabled
114  * @postdisable:        [DRIVER] function to run after marking ring disabled
115   **/
116 struct iio_ring_buffer {
117         struct device dev;
118         struct device access_dev;
119         struct iio_dev *indio_dev;
120         struct module *owner;
121         int                             id;
122         int                             access_id;
123         int                             length;
124         int                             bpd;
125         int                             bpe;
126         int                             loopcount;
127         struct iio_handler              access_handler;
128         struct iio_event_interface      ev_int;
129         struct iio_shared_ev_pointer    shared_ev_pointer;
130         struct iio_ring_access_funcs    access;
131         int                             (*preenable)(struct iio_dev *);
132         int                             (*postenable)(struct iio_dev *);
133         int                             (*predisable)(struct iio_dev *);
134         int                             (*postdisable)(struct iio_dev *);
135
136 };
137 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
138                           struct iio_dev *dev_info);
139
140 /**
141  * __iio_update_ring_buffer() - update common elements of ring buffers
142  * @ring:               ring buffer that is the event source
143  * @bytes_per_datum:    size of individual datum including timestamp
144  * @length:             number of datums in ring
145  **/
146 static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
147                                             int bytes_per_datum, int length)
148 {
149         ring->bpd = bytes_per_datum;
150         ring->length = length;
151         ring->loopcount = 0;
152 }
153
154 /**
155  * struct iio_scan_el - an individual element of a scan
156  * @dev_attr:           control attribute (if directly controllable)
157  * @number:             unique identifier of element (used for bit mask)
158  * @bit_count:          number of bits in scan element
159  * @label:              useful data for the scan el (often reg address)
160  * @set_state:          for some devices datardy signals are generated
161  *                      for any enabled lines.  This allows unwanted lines
162  *                      to be disabled and hence not get in the way.
163  **/
164 struct iio_scan_el {
165         struct device_attribute         dev_attr;
166         unsigned int                    number;
167         int                             bit_count;
168         unsigned int                    label;
169
170         int (*set_state)(struct iio_scan_el *scanel,
171                          struct iio_dev *dev_info,
172                          bool state);
173 };
174
175 #define to_iio_scan_el(_dev_attr)                               \
176         container_of(_dev_attr, struct iio_scan_el, dev_attr);
177
178 /**
179  * iio_scan_el_store() - sysfs scan element selection interface
180  * @dev: the target device
181  * @attr: the device attribute that is being processed
182  * @buf: input from userspace
183  * @len: length of input
184  *
185  * A generic function used to enable various scan elements.  In some
186  * devices explicit read commands for each channel mean this is merely
187  * a software switch.  In others this must actively disable the channel.
188  * Complexities occur when this interacts with data ready type triggers
189  * which may not reset unless every channel that is enabled is explicitly
190  * read.
191  **/
192 ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
193                           const char *buf, size_t len);
194 /**
195  * iio_scal_el_show() - sysfs interface to query whether a scan element is
196  *                      is enabled or not
197  * @dev: the target device
198  * @attr: the device attribute that is being processed
199  * @buf: output buffer
200  **/
201 ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
202                          char *buf);
203
204 ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
205                              const char *buf, size_t len);
206
207 ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
208                             char *buf);
209 /**
210  * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
211  *
212  * @_name:      identifying name. Resulting struct is iio_scan_el_##_name,
213  *              sysfs element, _name##_en.
214  * @_number:    unique id number for the scan element.
215  * @_bits:      number of bits in the scan element result (used in mixed bit
216  *              length devices).
217  * @_label:     indentification variable used by drivers.  Often a reg address.
218  * @_controlfunc: function used to notify hardware of whether state changes
219  **/
220 #define __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)    \
221         struct iio_scan_el iio_scan_el_##_name = {                      \
222                 .dev_attr = __ATTR(_number##_##_name##_en,              \
223                                    S_IRUGO | S_IWUSR,                   \
224                                    iio_scan_el_show,                    \
225                                    iio_scan_el_store),                  \
226                 .number =  _number,                                     \
227                 .bit_count = _bits,                                     \
228                 .label = _label,                                        \
229                 .set_state = _controlfunc,                              \
230         }
231
232 #define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)      \
233         __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)
234
235 #define __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
236         struct iio_scan_el iio_scan_el_##_name = {                      \
237                 .dev_attr = __ATTR(_number##_##_string##_en,            \
238                                    S_IRUGO | S_IWUSR,                   \
239                                    iio_scan_el_show,                    \
240                                    iio_scan_el_store),                  \
241                 .number =  _number,                                     \
242                 .bit_count = _bits,                                     \
243                 .label = _label,                                        \
244                 .set_state = _cf,                                       \
245         }
246 #define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
247         __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf)
248 /**
249  * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
250  *
251  * Odd one out. Handled slightly differently from other scan elements.
252  **/
253 #define IIO_SCAN_EL_TIMESTAMP(number)                           \
254         struct iio_scan_el iio_scan_el_timestamp = {            \
255                 .dev_attr = __ATTR(number##_timestamp_en,       \
256                                    S_IRUGO | S_IWUSR,           \
257                                    iio_scan_el_ts_show,         \
258                                    iio_scan_el_ts_store),       \
259         }
260
261 static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
262 {
263         put_device(&ring->dev);
264 };
265
266 #define to_iio_ring_buffer(d)                   \
267         container_of(d, struct iio_ring_buffer, dev)
268 #define access_dev_to_iio_ring_buffer(d)                        \
269         container_of(d, struct iio_ring_buffer, access_dev)
270 int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
271 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
272
273 ssize_t iio_read_ring_length(struct device *dev,
274                              struct device_attribute *attr,
275                              char *buf);
276 ssize_t iio_write_ring_length(struct device *dev,
277                               struct device_attribute *attr,
278                               const char *buf,
279                               size_t len);
280 ssize_t iio_read_ring_bps(struct device *dev,
281                           struct device_attribute *attr,
282                           char *buf);
283 ssize_t iio_store_ring_enable(struct device *dev,
284                               struct device_attribute *attr,
285                               const char *buf,
286                               size_t len);
287 ssize_t iio_show_ring_enable(struct device *dev,
288                              struct device_attribute *attr,
289                              char *buf);
290 #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR,     \
291                                          iio_read_ring_length,          \
292                                          iio_write_ring_length)
293 #define IIO_RING_BPS_ATTR DEVICE_ATTR(bps, S_IRUGO | S_IWUSR,   \
294                                       iio_read_ring_bps, NULL)
295 #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(ring_enable, S_IRUGO | S_IWUSR, \
296                                          iio_show_ring_enable,          \
297                                          iio_store_ring_enable)
298 #else /* CONFIG_IIO_RING_BUFFER */
299 static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
300 {
301         return 0;
302 };
303 static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
304 {};
305
306 #endif /* CONFIG_IIO_RING_BUFFER */
307
308 #endif /* _IIO_RING_GENERIC_H_ */