blob: 0270405691803339f143879cd6a1a7eb1b933141 [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>
Jonathan Cameron7026ea42009-08-18 18:06:24 +010014
Jonathan Cameronf2a96242011-09-21 11:15:55 +010015#ifdef CONFIG_IIO_BUFFER
Jonathan Cameron26620512010-07-11 16:39:14 +010016
Jonathan Cameron14555b12011-09-21 11:15:57 +010017struct iio_buffer;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010018
19/**
Jonathan Cameron14555b12011-09-21 11:15:57 +010020 * struct iio_buffer_access_funcs - access functions for buffers.
Jonathan Cameron14555b12011-09-21 11:15:57 +010021 * @store_to: actually store stuff to the buffer
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +010022 * @read_first_n: try to get a specified number of bytes (must exist)
Jonathan Cameron7026ea42009-08-18 18:06:24 +010023 * @request_update: if a parameter change has been marked, update underlying
24 * storage.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010025 * @get_bytes_per_datum:get current bytes per datum
26 * @set_bytes_per_datum:set number of bytes per datum
Jonathan Cameron14555b12011-09-21 11:15:57 +010027 * @get_length: get number of datums in buffer
28 * @set_length: set number of datums in buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +010029 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010030 * The purpose of this structure is to make the buffer element
Jonathan Cameron7026ea42009-08-18 18:06:24 +010031 * modular as event for a given driver, different usecases may require
Jonathan Cameron14555b12011-09-21 11:15:57 +010032 * different buffer designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010033 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010034 * It is worth noting that a given buffer implementation may only support a
35 * small proportion of these functions. The core code 'should' cope fine with
36 * any of them not existing.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010037 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +010038struct iio_buffer_access_funcs {
Lars-Peter Clausence56ade2012-09-04 13:38:00 +010039 int (*store_to)(struct iio_buffer *buffer, u8 *data);
Jonathan Cameron14555b12011-09-21 11:15:57 +010040 int (*read_first_n)(struct iio_buffer *buffer,
Jonathan Cameronb4281732011-04-15 18:55:55 +010041 size_t n,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010042 char __user *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010043
Jonathan Cameron14555b12011-09-21 11:15:57 +010044 int (*request_update)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010045
Jonathan Cameron14555b12011-09-21 11:15:57 +010046 int (*get_bytes_per_datum)(struct iio_buffer *buffer);
47 int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
48 int (*get_length)(struct iio_buffer *buffer);
49 int (*set_length)(struct iio_buffer *buffer, int length);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010050};
51
Jonathan Cameron6446e9c2011-08-30 12:41:13 +010052/**
Jonathan Cameron14555b12011-09-21 11:15:57 +010053 * struct iio_buffer - general buffer structure
Jonathan Cameron14555b12011-09-21 11:15:57 +010054 * @length: [DEVICE] number of datums in buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010055 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
Manuel Stahlbf329632010-08-31 11:32:52 +020056 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
57 * control method is used
Manuel Stahlbf329632010-08-31 11:32:52 +020058 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
59 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
Jonathan Cameron14555b12011-09-21 11:15:57 +010060 * @access: [DRIVER] buffer access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +010061 * implementation.
Jonathan Cameron5f070a32011-12-05 22:18:30 +000062 * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
63 * @scan_el_group: [DRIVER] attribute group for those attributes not
64 * created from the iio_chan_info array.
65 * @pollq: [INTERN] wait queue to allow for polling on the buffer.
66 * @stufftoread: [INTERN] flag to indicate new data.
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000067 * @demux_list: [INTERN] list of operations required to demux the scan.
68 * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010069 * @buffer_list: [INTERN] entry in the devices list of current buffers.
70 */
Jonathan Cameron14555b12011-09-21 11:15:57 +010071struct iio_buffer {
Jonathan Cameron8d213f22011-05-18 14:42:34 +010072 int length;
73 int bytes_per_datum;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010074 struct attribute_group *scan_el_attrs;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010075 long *scan_mask;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010076 bool scan_timestamp;
Jonathan Cameron14555b12011-09-21 11:15:57 +010077 const struct iio_buffer_access_funcs *access;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010078 struct list_head scan_el_dev_attr_list;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +010079 struct attribute_group scan_el_group;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010080 wait_queue_head_t pollq;
81 bool stufftoread;
Jonathan Cameron1aa04272011-08-30 12:32:47 +010082 const struct attribute_group *attrs;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000083 struct list_head demux_list;
84 unsigned char *demux_bounce;
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010085 struct list_head buffer_list;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010086};
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010087
88/**
Jonathan Cameron84b36ce2012-06-30 20:06:00 +010089 * iio_update_buffers() - add or remove buffer from active list
90 * @indio_dev: device to add buffer to
91 * @insert_buffer: buffer to insert
92 * @remove_buffer: buffer_to_remove
93 *
94 * Note this will tear down the all buffering and build it up again
95 */
96int iio_update_buffers(struct iio_dev *indio_dev,
97 struct iio_buffer *insert_buffer,
98 struct iio_buffer *remove_buffer);
99
100/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100101 * iio_buffer_init() - Initialize the buffer structure
Peter Meerwald3bdff932012-07-01 00:47:43 +0200102 * @buffer: buffer to be initialized
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100103 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000104void iio_buffer_init(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100105
106/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100107 * __iio_update_buffer() - update common elements of buffers
108 * @buffer: buffer that is the event source
Randy Dunlap4c572602009-10-04 19:34:02 -0700109 * @bytes_per_datum: size of individual datum including timestamp
Jonathan Cameron14555b12011-09-21 11:15:57 +0100110 * @length: number of datums in buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100111 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100112static inline void __iio_update_buffer(struct iio_buffer *buffer,
113 int bytes_per_datum, int length)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100114{
Jonathan Cameron14555b12011-09-21 11:15:57 +0100115 buffer->bytes_per_datum = bytes_per_datum;
116 buffer->length = length;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100117}
118
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000119int iio_scan_mask_query(struct iio_dev *indio_dev,
120 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200121
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100122/**
123 * iio_scan_mask_set() - set particular bit in the scan mask
Peter Meerwald3bdff932012-07-01 00:47:43 +0200124 * @indio_dev IIO device structure
125 * @buffer: the buffer whose scan mask we are interested in
126 * @bit: the bit to be set.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100127 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000128int iio_scan_mask_set(struct iio_dev *indio_dev,
129 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200130
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100131/**
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100132 * iio_push_to_buffers() - push to a registered buffer.
133 * @indio_dev: iio_dev structure for device.
134 * @data: Full scan.
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000135 */
Jonathan Cameron84b36ce2012-06-30 20:06:00 +0100136int iio_push_to_buffers(struct iio_dev *indio_dev, unsigned char *data);
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000137
138int iio_update_demux(struct iio_dev *indio_dev);
139
140/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100141 * iio_buffer_register() - register the buffer with IIO core
Peter Meerwald3bdff932012-07-01 00:47:43 +0200142 * @indio_dev: device with the buffer to be registered
143 * @channels: the channel descriptions used to construct buffer
144 * @num_channels: the number of channels
Jonathan Cameron1d892712011-05-18 14:40:51 +0100145 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100146int iio_buffer_register(struct iio_dev *indio_dev,
147 const struct iio_chan_spec *channels,
148 int num_channels);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100149
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100150/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100151 * iio_buffer_unregister() - unregister the buffer from IIO core
Peter Meerwald3bdff932012-07-01 00:47:43 +0200152 * @indio_dev: the device with the buffer to be unregistered
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100153 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100154void iio_buffer_unregister(struct iio_dev *indio_dev);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100155
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100156/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100157 * iio_buffer_read_length() - attr func to get number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100158 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100159ssize_t iio_buffer_read_length(struct device *dev,
160 struct device_attribute *attr,
161 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100162/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100163 * iio_buffer_write_length() - attr func to set number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100164 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100165ssize_t iio_buffer_write_length(struct device *dev,
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100166 struct device_attribute *attr,
167 const char *buf,
168 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100169/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100170 * iio_buffer_store_enable() - attr to turn the buffer on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100171 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100172ssize_t iio_buffer_store_enable(struct device *dev,
173 struct device_attribute *attr,
174 const char *buf,
175 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100176/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100177 * iio_buffer_show_enable() - attr to see if the buffer is on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100178 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100179ssize_t iio_buffer_show_enable(struct device *dev,
180 struct device_attribute *attr,
181 char *buf);
182#define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
183 iio_buffer_read_length, \
184 iio_buffer_write_length)
Jonathan Cameron5565a452011-05-18 14:42:24 +0100185
Jonathan Cameron14555b12011-09-21 11:15:57 +0100186#define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
187 iio_buffer_show_enable, \
188 iio_buffer_store_enable)
189
190int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100191
Lars-Peter Clausen81636632012-07-09 10:00:00 +0100192bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
193 const unsigned long *mask);
194
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100195#else /* CONFIG_IIO_BUFFER */
Jonathan Cameron1d892712011-05-18 14:40:51 +0100196
Jonathan Cameron14555b12011-09-21 11:15:57 +0100197static inline int iio_buffer_register(struct iio_dev *indio_dev,
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100198 struct iio_chan_spec *channels,
199 int num_channels)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100200{
201 return 0;
202}
203
Jonathan Cameron14555b12011-09-21 11:15:57 +0100204static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
Peter Meerwald19ea47522012-06-18 20:33:03 +0200205{}
Jonathan Cameron26620512010-07-11 16:39:14 +0100206
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100207#endif /* CONFIG_IIO_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100208
Jonathan Cameron3811cd62011-09-21 11:15:56 +0100209#endif /* _IIO_BUFFER_GENERIC_H_ */