blob: 6e428d96d57025eb155a55c275ac9789104ff79f [file] [log] [blame]
Jonathan Cameron14555b12011-09-21 11:15:57 +01001/* The industrial I/O core - generic buffer interfaces.
Jonathan Cameron7026ea42009-08-18 18:06:24 +01002 *
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
Jonathan Cameron3811cd62011-09-21 11:15:56 +010010#ifndef _IIO_BUFFER_GENERIC_H_
11#define _IIO_BUFFER_GENERIC_H_
Jonathan Cameron26d25ae2011-09-02 17:14:40 +010012#include <linux/sysfs.h>
Jonathan Cameron06458e22012-04-25 15:54:58 +010013#include <linux/iio/iio.h>
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010014#include <linux/kref.h>
Jonathan Cameron7026ea42009-08-18 18:06:24 +010015
Jonathan Cameronf2a96242011-09-21 11:15:55 +010016#ifdef CONFIG_IIO_BUFFER
Jonathan Cameron26620512010-07-11 16:39:14 +010017
Jonathan Cameron14555b12011-09-21 11:15:57 +010018struct iio_buffer;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010019
20/**
Jonathan Cameron14555b12011-09-21 11:15:57 +010021 * struct iio_buffer_access_funcs - access functions for buffers.
Jonathan Cameron14555b12011-09-21 11:15:57 +010022 * @store_to: actually store stuff to the buffer
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +010023 * @read_first_n: try to get a specified number of bytes (must exist)
Jonathan Cameron7026ea42009-08-18 18:06:24 +010024 * @request_update: if a parameter change has been marked, update underlying
25 * storage.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010026 * @get_bytes_per_datum:get current bytes per datum
27 * @set_bytes_per_datum:set number of bytes per datum
Jonathan Cameron14555b12011-09-21 11:15:57 +010028 * @get_length: get number of datums in buffer
29 * @set_length: set number of datums in buffer
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010030 * @release: called when the last reference to the buffer is dropped,
31 * should free all resources allocated by the buffer.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010032 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010033 * The purpose of this structure is to make the buffer element
Jonathan Cameron7026ea42009-08-18 18:06:24 +010034 * modular as event for a given driver, different usecases may require
Jonathan Cameron14555b12011-09-21 11:15:57 +010035 * different buffer designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010036 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010037 * It is worth noting that a given buffer implementation may only support a
38 * small proportion of these functions. The core code 'should' cope fine with
39 * any of them not existing.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010040 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +010041struct iio_buffer_access_funcs {
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010042 int (*store_to)(struct iio_buffer *buffer, const void *data);
Jonathan Cameron14555b12011-09-21 11:15:57 +010043 int (*read_first_n)(struct iio_buffer *buffer,
Jonathan Cameronb4281732011-04-15 18:55:55 +010044 size_t n,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010045 char __user *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010046
Jonathan Cameron14555b12011-09-21 11:15:57 +010047 int (*request_update)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010048
Jonathan Cameron14555b12011-09-21 11:15:57 +010049 int (*get_bytes_per_datum)(struct iio_buffer *buffer);
50 int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
51 int (*get_length)(struct iio_buffer *buffer);
52 int (*set_length)(struct iio_buffer *buffer, int length);
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010053
54 void (*release)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010055};
56
Jonathan Cameron6446e9c2011-08-30 12:41:13 +010057/**
Jonathan Cameron14555b12011-09-21 11:15:57 +010058 * struct iio_buffer - general buffer structure
Jonathan Cameron14555b12011-09-21 11:15:57 +010059 * @length: [DEVICE] number of datums in buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010060 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
Manuel Stahlbf329632010-08-31 11:32:52 +020061 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
62 * control method is used
Manuel Stahlbf329632010-08-31 11:32:52 +020063 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
64 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
Jonathan Cameron14555b12011-09-21 11:15:57 +010065 * @access: [DRIVER] buffer access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +010066 * implementation.
Jonathan Cameron5f070a32011-12-05 22:18:30 +000067 * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
68 * @scan_el_group: [DRIVER] attribute group for those attributes not
69 * created from the iio_chan_info array.
70 * @pollq: [INTERN] wait queue to allow for polling on the buffer.
71 * @stufftoread: [INTERN] flag to indicate new data.
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000072 * @demux_list: [INTERN] list of operations required to demux the scan.
73 * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010074 * @buffer_list: [INTERN] entry in the devices list of current buffers.
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010075 * @ref: [INTERN] reference count of the buffer.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010076 */
Jonathan Cameron14555b12011-09-21 11:15:57 +010077struct iio_buffer {
Jonathan Cameron8d213f22011-05-18 14:42:34 +010078 int length;
79 int bytes_per_datum;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010080 struct attribute_group *scan_el_attrs;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010081 long *scan_mask;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010082 bool scan_timestamp;
Jonathan Cameron14555b12011-09-21 11:15:57 +010083 const struct iio_buffer_access_funcs *access;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010084 struct list_head scan_el_dev_attr_list;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +010085 struct attribute_group scan_el_group;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010086 wait_queue_head_t pollq;
87 bool stufftoread;
Jonathan Cameron1aa04272011-08-30 12:32:47 +010088 const struct attribute_group *attrs;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000089 struct list_head demux_list;
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +010090 void *demux_bounce;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010091 struct list_head buffer_list;
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +010092 struct kref ref;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010093};
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010094
95/**
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010096 * iio_update_buffers() - add or remove buffer from active list
97 * @indio_dev: device to add buffer to
98 * @insert_buffer: buffer to insert
99 * @remove_buffer: buffer_to_remove
100 *
101 * Note this will tear down the all buffering and build it up again
102 */
103int iio_update_buffers(struct iio_dev *indio_dev,
104 struct iio_buffer *insert_buffer,
105 struct iio_buffer *remove_buffer);
106
107/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100108 * iio_buffer_init() - Initialize the buffer structure
Peter Meerwald3bdff932012-07-01 00:47:43 +0200109 * @buffer: buffer to be initialized
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100110 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000111void iio_buffer_init(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100112
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000113int iio_scan_mask_query(struct iio_dev *indio_dev,
114 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200115
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100116/**
117 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald3bdff932012-07-01 00:47:43 +0200118 * @indio_dev IIO device structure
119 * @buffer: the buffer whose scan mask we are interested in
120 * @bit: the bit to be set.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100121 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000122int iio_scan_mask_set(struct iio_dev *indio_dev,
123 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200124
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100125/**
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100126 * iio_push_to_buffers() - push to a registered buffer.
127 * @indio_dev: iio_dev structure for device.
128 * @data: Full scan.
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000129 */
Lars-Peter Clausen5d65d922013-09-15 17:50:00 +0100130int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000131
Lars-Peter Clausend2c3d072013-09-19 13:59:00 +0100132/*
133 * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
134 * @indio_dev: iio_dev structure for device.
135 * @data: sample data
136 * @timestamp: timestamp for the sample data
137 *
138 * Pushes data to the IIO device's buffers. If timestamps are enabled for the
139 * device the function will store the supplied timestamp as the last element in
140 * the sample data buffer before pushing it to the device buffers. The sample
141 * data buffer needs to be large enough to hold the additional timestamp
142 * (usually the buffer should be indio->scan_bytes bytes large).
143 *
144 * Returns 0 on success, a negative error code otherwise.
145 */
146static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
147 void *data, int64_t timestamp)
148{
149 if (indio_dev->scan_timestamp) {
150 size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
151 ((int64_t *)data)[ts_offset] = timestamp;
152 }
153
154 return iio_push_to_buffers(indio_dev, data);
155}
156
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000157int iio_update_demux(struct iio_dev *indio_dev);
158
159/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100160 * iio_buffer_register() - register the buffer with IIO core
Peter Meerwald3bdff932012-07-01 00:47:43 +0200161 * @indio_dev: device with the buffer to be registered
162 * @channels: the channel descriptions used to construct buffer
163 * @num_channels: the number of channels
Jonathan Cameron1d892712011-05-18 14:40:51 +0100164 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100165int iio_buffer_register(struct iio_dev *indio_dev,
166 const struct iio_chan_spec *channels,
167 int num_channels);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100168
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100169/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100170 * iio_buffer_unregister() - unregister the buffer from IIO core
Peter Meerwald3bdff932012-07-01 00:47:43 +0200171 * @indio_dev: the device with the buffer to be unregistered
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100172 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100173void iio_buffer_unregister(struct iio_dev *indio_dev);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100174
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100175/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100176 * iio_buffer_read_length() - attr func to get number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100177 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100178ssize_t iio_buffer_read_length(struct device *dev,
179 struct device_attribute *attr,
180 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100181/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 * iio_buffer_write_length() - attr func to set number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100183 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100184ssize_t iio_buffer_write_length(struct device *dev,
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100185 struct device_attribute *attr,
186 const char *buf,
187 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100188/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100189 * iio_buffer_store_enable() - attr to turn the buffer on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100190 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100191ssize_t iio_buffer_store_enable(struct device *dev,
192 struct device_attribute *attr,
193 const char *buf,
194 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100195/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100196 * iio_buffer_show_enable() - attr to see if the buffer is on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100197 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100198ssize_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)
Jonathan Cameron5565a452011-05-18 14:42:24 +0100204
Jonathan Cameron14555b12011-09-21 11:15:57 +0100205#define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
206 iio_buffer_show_enable, \
207 iio_buffer_store_enable)
208
209int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100210
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100211bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
212 const unsigned long *mask);
213
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100214struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
215void iio_buffer_put(struct iio_buffer *buffer);
216
217/**
218 * iio_device_attach_buffer - Attach a buffer to a IIO device
219 * @indio_dev: The device the buffer should be attached to
220 * @buffer: The buffer to attach to the device
221 *
222 * This function attaches a buffer to a IIO device. The buffer stays attached to
223 * the device until the device is freed. The function should only be called at
224 * most once per device.
225 */
226static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
227 struct iio_buffer *buffer)
228{
229 indio_dev->buffer = iio_buffer_get(buffer);
230}
231
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100232#else /* CONFIG_IIO_BUFFER */
Jonathan Cameron1d892712011-05-18 14:40:51 +0100233
Jonathan Cameron14555b12011-09-21 11:15:57 +0100234static inline int iio_buffer_register(struct iio_dev *indio_dev,
Lars-Peter Clausen50d69b52012-11-13 11:48:00 +0000235 const struct iio_chan_spec *channels,
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100236 int num_channels)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100237{
238 return 0;
239}
240
Jonathan Cameron14555b12011-09-21 11:15:57 +0100241static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
Peter Meerwald19ea47522012-06-18 20:33:03 +0200242{}
Jonathan Cameron26620512010-07-11 16:39:14 +0100243
Lars-Peter Clausen9e69c932013-10-04 12:06:00 +0100244static inline void iio_buffer_get(struct iio_buffer *buffer) {}
245static inline void iio_buffer_put(struct iio_buffer *buffer) {}
246
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100247#endif /* CONFIG_IIO_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100248
Jonathan Cameron3811cd62011-09-21 11:15:56 +0100249#endif /* _IIO_BUFFER_GENERIC_H_ */