Merge branch 'upstream/jump-label-noearly' of git://git.kernel.org/pub/scm/linux...
[pandora-kernel.git] / drivers / staging / iio / buffer_generic.h
1 /* The industrial I/O core - generic 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_BUFFER_GENERIC_H_
11 #define _IIO_BUFFER_GENERIC_H_
12 #include <linux/sysfs.h>
13 #include "iio.h"
14 #include "chrdev.h"
15
16 #ifdef CONFIG_IIO_BUFFER
17
18 struct iio_buffer;
19
20 /**
21  * struct iio_buffer_access_funcs - access functions for buffers.
22  * @mark_in_use:        reference counting, typically to prevent module removal
23  * @unmark_in_use:      reduce reference count when no longer using buffer
24  * @store_to:           actually store stuff to the buffer
25  * @read_last:          get the last element stored
26  * @read_first_n:       try to get a specified number of elements (must exist)
27  * @mark_param_change:  notify buffer 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.
32  * @get_bytes_per_datum:get current bytes per datum
33  * @set_bytes_per_datum:set number of bytes per datum
34  * @get_length:         get number of datums in buffer
35  * @set_length:         set number of datums in buffer
36  * @is_enabled:         query if buffer is currently being used
37  * @enable:             enable the buffer
38  *
39  * The purpose of this structure is to make the buffer element
40  * modular as event for a given driver, different usecases may require
41  * different buffer designs (space efficiency vs speed for example).
42  *
43  * It is worth noting that a given buffer implementation may only support a
44  * small proportion of these functions.  The core code 'should' cope fine with
45  * any of them not existing.
46  **/
47 struct iio_buffer_access_funcs {
48         void (*mark_in_use)(struct iio_buffer *buffer);
49         void (*unmark_in_use)(struct iio_buffer *buffer);
50
51         int (*store_to)(struct iio_buffer *buffer, u8 *data, s64 timestamp);
52         int (*read_last)(struct iio_buffer *buffer, u8 *data);
53         int (*read_first_n)(struct iio_buffer *buffer,
54                             size_t n,
55                             char __user *buf);
56
57         int (*mark_param_change)(struct iio_buffer *buffer);
58         int (*request_update)(struct iio_buffer *buffer);
59
60         int (*get_bytes_per_datum)(struct iio_buffer *buffer);
61         int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
62         int (*get_length)(struct iio_buffer *buffer);
63         int (*set_length)(struct iio_buffer *buffer, int length);
64
65         int (*is_enabled)(struct iio_buffer *buffer);
66         int (*enable)(struct iio_buffer *buffer);
67 };
68
69 /**
70  * struct iio_buffer_setup_ops - buffer setup related callbacks
71  * @preenable:          [DRIVER] function to run prior to marking buffer enabled
72  * @postenable:         [DRIVER] function to run after marking buffer enabled
73  * @predisable:         [DRIVER] function to run prior to marking buffer
74  *                      disabled
75  * @postdisable:        [DRIVER] function to run after marking buffer disabled
76  */
77 struct iio_buffer_setup_ops {
78         int                             (*preenable)(struct iio_dev *);
79         int                             (*postenable)(struct iio_dev *);
80         int                             (*predisable)(struct iio_dev *);
81         int                             (*postdisable)(struct iio_dev *);
82 };
83
84 /**
85  * struct iio_buffer - general buffer structure
86  * @indio_dev:          industrial I/O device structure
87  * @owner:              module that owns the buffer (for ref counting)
88  * @length:             [DEVICE] number of datums in buffer
89  * @bytes_per_datum:    [DEVICE] size of individual datum including timestamp
90  * @bpe:                [DEVICE] size of individual channel value
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
96  * @access:             [DRIVER] buffer access functions associated with the
97  *                      implementation.
98  * @flags:              [INTERN] file ops related flags including busy flag.
99  **/
100 struct iio_buffer {
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;
108         long                                    *scan_mask;
109         bool                                    scan_timestamp;
110         const struct iio_buffer_access_funcs    *access;
111         const struct iio_buffer_setup_ops               *setup_ops;
112         struct list_head                        scan_el_dev_attr_list;
113         struct attribute_group                  scan_el_group;
114         wait_queue_head_t                       pollq;
115         bool                                    stufftoread;
116         unsigned long                           flags;
117         const struct attribute_group *attrs;
118 };
119
120 /**
121  * iio_buffer_init() - Initialize the buffer structure
122  * @buffer: buffer to be initialized
123  * @indio_dev: the iio device the buffer is assocated with
124  **/
125 void iio_buffer_init(struct iio_buffer *buffer,
126                           struct iio_dev *indio_dev);
127
128 void iio_buffer_deinit(struct iio_buffer *buffer);
129
130 /**
131  * __iio_update_buffer() - update common elements of buffers
132  * @buffer:             buffer that is the event source
133  * @bytes_per_datum:    size of individual datum including timestamp
134  * @length:             number of datums in buffer
135  **/
136 static inline void __iio_update_buffer(struct iio_buffer *buffer,
137                                        int bytes_per_datum, int length)
138 {
139         buffer->bytes_per_datum = bytes_per_datum;
140         buffer->length = length;
141 }
142
143 int iio_scan_mask_query(struct iio_buffer *buffer, int bit);
144
145 /**
146  * iio_scan_mask_set() - set particular bit in the scan mask
147  * @buffer: the buffer whose scan mask we are interested in
148  * @bit: the bit to be set.
149  **/
150 int iio_scan_mask_set(struct iio_buffer *buffer, int bit);
151
152 #define to_iio_buffer(d)                                \
153         container_of(d, struct iio_buffer, dev)
154
155 /**
156  * iio_buffer_register() - register the buffer with IIO core
157  * @indio_dev: device with the buffer to be registered
158  **/
159 int iio_buffer_register(struct iio_dev *indio_dev,
160                         const struct iio_chan_spec *channels,
161                         int num_channels);
162
163 /**
164  * iio_buffer_unregister() - unregister the buffer from IIO core
165  * @indio_dev: the device with the buffer to be unregistered
166  **/
167 void iio_buffer_unregister(struct iio_dev *indio_dev);
168
169 /**
170  * iio_buffer_read_length() - attr func to get number of datums in the buffer
171  **/
172 ssize_t iio_buffer_read_length(struct device *dev,
173                                struct device_attribute *attr,
174                                char *buf);
175 /**
176  * iio_buffer_write_length() - attr func to set number of datums in the buffer
177  **/
178 ssize_t iio_buffer_write_length(struct device *dev,
179                               struct device_attribute *attr,
180                               const char *buf,
181                               size_t len);
182 /**
183  * iio_buffer_read_bytes_per_datum() - attr for number of bytes in whole datum
184  **/
185 ssize_t iio_buffer_read_bytes_per_datum(struct device *dev,
186                                         struct device_attribute *attr,
187                                         char *buf);
188 /**
189  * iio_buffer_store_enable() - attr to turn the buffer on
190  **/
191 ssize_t iio_buffer_store_enable(struct device *dev,
192                                 struct device_attribute *attr,
193                                 const char *buf,
194                                 size_t len);
195 /**
196  * iio_buffer_show_enable() - attr to see if the buffer is on
197  **/
198 ssize_t iio_buffer_show_enable(struct device *dev,
199                                struct device_attribute *attr,
200                                char *buf);
201 #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR,   \
202                                            iio_buffer_read_length,      \
203                                            iio_buffer_write_length)
204 #define IIO_BUFFER_BYTES_PER_DATUM_ATTR                                 \
205         DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR,                 \
206                     iio_buffer_read_bytes_per_datum, NULL)
207
208 #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,   \
209                                            iio_buffer_show_enable,      \
210                                            iio_buffer_store_enable)
211
212 int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
213
214 #else /* CONFIG_IIO_BUFFER */
215
216 static inline int iio_buffer_register(struct iio_dev *indio_dev,
217                                            struct iio_chan_spec *channels,
218                                            int num_channels)
219 {
220         return 0;
221 }
222
223 static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
224 {};
225
226 #endif /* CONFIG_IIO_BUFFER */
227
228 #endif /* _IIO_BUFFER_GENERIC_H_ */