blob: 25aacf3b89331f67f42bee8e324dcc7aa3e81432 [file] [log] [blame]
Jonathan Cameron7026ea42009-08-18 18:06:24 +01001/* 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"
Jonathan Cameron7ae8cf62011-08-30 12:32:44 +010013#include "chrdev.h"
Jonathan Cameron7026ea42009-08-18 18:06:24 +010014
Jonathan Cameron26620512010-07-11 16:39:14 +010015#ifdef CONFIG_IIO_RING_BUFFER
16
Jonathan Cameron7026ea42009-08-18 18:06:24 +010017struct iio_ring_buffer;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010018
19/**
Jonathan Cameron7026ea42009-08-18 18:06:24 +010020 * struct iio_ring_access_funcs - access functions for ring buffers.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010021 * @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
Jonathan Cameronb4281732011-04-15 18:55:55 +010025 * @read_first_n: try to get a specified number of elements (must exist)
Jonathan Cameron7026ea42009-08-18 18:06:24 +010026 * @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.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010031 * @get_bytes_per_datum:get current bytes per datum
32 * @set_bytes_per_datum:set number of bytes per datum
Jonathan Cameron7026ea42009-08-18 18:06:24 +010033 * @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
Randy Dunlap4c572602009-10-04 19:34:02 -070040 * different ring designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010041 *
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 **/
46struct 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);
Jonathan Cameronb4281732011-04-15 18:55:55 +010052 int (*read_first_n)(struct iio_ring_buffer *ring,
53 size_t n,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010054 char __user *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010055
56 int (*mark_param_change)(struct iio_ring_buffer *ring);
57 int (*request_update)(struct iio_ring_buffer *ring);
58
Manuel Stahlffcab072010-08-31 11:32:50 +020059 int (*get_bytes_per_datum)(struct iio_ring_buffer *ring);
60 int (*set_bytes_per_datum)(struct iio_ring_buffer *ring, size_t bpd);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010061 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
Jonathan Cameron6446e9c2011-08-30 12:41:13 +010068/**
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 */
Jonathan Cameron5565a452011-05-18 14:42:24 +010075struct 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
Jonathan Cameron7026ea42009-08-18 18:06:24 +010082/**
83 * struct iio_ring_buffer - general ring buffer structure
Randy Dunlap4c572602009-10-04 19:34:02 -070084 * @dev: ring buffer device struct
Randy Dunlap4c572602009-10-04 19:34:02 -070085 * @indio_dev: industrial I/O device structure
86 * @owner: module that owns the ring buffer (for ref counting)
Randy Dunlap4c572602009-10-04 19:34:02 -070087 * @length: [DEVICE] number of datums in ring
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010088 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
Barry Songad577f82010-07-11 16:39:16 +010089 * @bpe: [DEVICE] size of individual channel value
Manuel Stahlbf329632010-08-31 11:32:52 +020090 * @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
Randy Dunlap4c572602009-10-04 19:34:02 -070095 * @access: [DRIVER] ring access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +010096 * implementation.
Jonathan Cameron63564632011-08-30 12:32:46 +010097 * @flags: [INTERN] file ops related flags including busy flag.
Jonathan Cameron8d213f22011-05-18 14:42:34 +010098 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +010099struct iio_ring_buffer {
Jonathan Cameron8d213f22011-05-18 14:42:34 +0100100 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;
107 unsigned long scan_mask;
108 bool scan_timestamp;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100109 const struct iio_ring_access_funcs *access;
Jonathan Cameron8d213f22011-05-18 14:42:34 +0100110 const struct iio_ring_setup_ops *setup_ops;
111 struct list_head scan_el_dev_attr_list;
Jonathan Camerona7348342011-05-18 14:40:55 +0100112
Jonathan Cameron8d213f22011-05-18 14:42:34 +0100113 wait_queue_head_t pollq;
114 bool stufftoread;
Jonathan Cameron63564632011-08-30 12:32:46 +0100115 unsigned long flags;
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100116 const struct attribute_group *attrs;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100117};
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100118
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 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100124void iio_ring_buffer_init(struct iio_ring_buffer *ring,
125 struct iio_dev *dev_info);
126
127/**
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000128 * __iio_update_ring_buffer() - update common elements of ring buffers
Randy Dunlap4c572602009-10-04 19:34:02 -0700129 * @ring: ring buffer that is the event source
130 * @bytes_per_datum: size of individual datum including timestamp
131 * @length: number of datums in ring
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100132 **/
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000133static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
134 int bytes_per_datum, int length)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100135{
Manuel Stahlffcab072010-08-31 11:32:50 +0200136 ring->bytes_per_datum = bytes_per_datum;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100137 ring->length = length;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100138}
139
Manuel Stahlbf329632010-08-31 11:32:52 +0200140/*
141 * These are mainly provided to allow for a change of implementation if a device
142 * has a large number of scan elements
143 */
144#define IIO_MAX_SCAN_LENGTH 31
145
146/* note 0 used as error indicator as it doesn't make sense. */
147static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
148{
149 while (*av_masks) {
150 if (!(~*av_masks & mask))
151 return *av_masks;
152 av_masks++;
153 }
154 return 0;
155}
156
157static inline int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit)
158{
159 struct iio_dev *dev_info = ring->indio_dev;
160 u32 mask;
161
162 if (bit > IIO_MAX_SCAN_LENGTH)
163 return -EINVAL;
164
165 if (!ring->scan_mask)
166 return 0;
167
168 if (dev_info->available_scan_masks)
169 mask = iio_scan_mask_match(dev_info->available_scan_masks,
170 ring->scan_mask);
171 else
172 mask = ring->scan_mask;
173
174 if (!mask)
175 return -EINVAL;
176
177 return !!(mask & (1 << bit));
178};
179
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100180/**
181 * iio_scan_mask_set() - set particular bit in the scan mask
182 * @ring: the ring buffer whose scan mask we are interested in
183 * @bit: the bit to be set.
184 **/
Manuel Stahlbf329632010-08-31 11:32:52 +0200185static inline int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit)
186{
187 struct iio_dev *dev_info = ring->indio_dev;
188 u32 mask;
189 u32 trialmask = ring->scan_mask | (1 << bit);
190
191 if (bit > IIO_MAX_SCAN_LENGTH)
192 return -EINVAL;
193 if (dev_info->available_scan_masks) {
194 mask = iio_scan_mask_match(dev_info->available_scan_masks,
195 trialmask);
196 if (!mask)
197 return -EINVAL;
198 }
199 ring->scan_mask = trialmask;
200 ring->scan_count++;
201
202 return 0;
203};
204
Jonathan Cameron8d213f22011-05-18 14:42:34 +0100205#define to_iio_ring_buffer(d) \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100206 container_of(d, struct iio_ring_buffer, dev)
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100207
208/**
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100209 * iio_ring_buffer_register() - register the buffer with IIO core
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100210 * @indio_dev: device with the buffer to be registered
Jonathan Cameron1d892712011-05-18 14:40:51 +0100211 **/
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100212int iio_ring_buffer_register(struct iio_dev *indio_dev,
213 const struct iio_chan_spec *channels,
214 int num_channels);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100215
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100216/**
217 * iio_ring_buffer_unregister() - unregister the buffer from IIO core
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100218 * @indio_dev: the device with the buffer to be unregistered
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100219 **/
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100220void iio_ring_buffer_unregister(struct iio_dev *indio_dev);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100221
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100222/**
223 * iio_read_ring_length() - attr func to get number of datums in the buffer
224 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100225ssize_t iio_read_ring_length(struct device *dev,
226 struct device_attribute *attr,
227 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100228/**
229 * iio_write_ring_length() - attr func to set number of datums in the buffer
230 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100231ssize_t iio_write_ring_length(struct device *dev,
232 struct device_attribute *attr,
233 const char *buf,
234 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100235/**
236 * iio_read_ring_bytes_per_datum() - attr for number of bytes in whole datum
237 **/
Manuel Stahlffcab072010-08-31 11:32:50 +0200238ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100239 struct device_attribute *attr,
240 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100241/**
242 * iio_store_ring_enable() - attr to turn the buffer on
243 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100244ssize_t iio_store_ring_enable(struct device *dev,
245 struct device_attribute *attr,
246 const char *buf,
247 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100248/**
249 * iio_show_ring_enable() - attr to see if the buffer is on
250 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100251ssize_t iio_show_ring_enable(struct device *dev,
252 struct device_attribute *attr,
253 char *buf);
254#define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
255 iio_read_ring_length, \
256 iio_write_ring_length)
Manuel Stahlffcab072010-08-31 11:32:50 +0200257#define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR, \
258 iio_read_ring_bytes_per_datum, NULL)
259#define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100260 iio_show_ring_enable, \
261 iio_store_ring_enable)
Jonathan Cameron5565a452011-05-18 14:42:24 +0100262
263int iio_sw_ring_preenable(struct iio_dev *indio_dev);
264
Jonathan Cameron26620512010-07-11 16:39:14 +0100265#else /* CONFIG_IIO_RING_BUFFER */
Jonathan Cameron1d892712011-05-18 14:40:51 +0100266
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100267static inline int iio_ring_buffer_register(struct iio_dev *indio_dev,
268 struct iio_chan_spec *channels,
269 int num_channels)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100270{
271 return 0;
272}
273
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100274static inline void iio_ring_buffer_unregister(struct iio_dev *indio_dev)
Jonathan Cameron26620512010-07-11 16:39:14 +0100275{};
276
277#endif /* CONFIG_IIO_RING_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100278
279#endif /* _IIO_RING_GENERIC_H_ */