blob: 44593b28b36fbb0f86abbe9b3319ed91ce89e7f3 [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 Cameron7026ea42009-08-18 18:06:24 +010013#include "iio.h"
14
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 Cameron7026ea42009-08-18 18:06:24 +010021 * @mark_in_use: reference counting, typically to prevent module removal
Jonathan Cameron14555b12011-09-21 11:15:57 +010022 * @unmark_in_use: reduce reference count when no longer using buffer
23 * @store_to: actually store stuff to the buffer
Lars-Peter Clausen8fe64952011-12-12 09:33:14 +010024 * @read_first_n: try to get a specified number of bytes (must exist)
Jonathan Cameron14555b12011-09-21 11:15:57 +010025 * @mark_param_change: notify buffer that some relevant parameter has changed
Jonathan Cameron7026ea42009-08-18 18:06:24 +010026 * Often this means the underlying storage may need to
27 * change.
28 * @request_update: if a parameter change has been marked, update underlying
29 * storage.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010030 * @get_bytes_per_datum:get current bytes per datum
31 * @set_bytes_per_datum:set number of bytes per datum
Jonathan Cameron14555b12011-09-21 11:15:57 +010032 * @get_length: get number of datums in buffer
33 * @set_length: set number of datums in buffer
34 * @is_enabled: query if buffer is currently being used
35 * @enable: enable the buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +010036 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010037 * The purpose of this structure is to make the buffer element
Jonathan Cameron7026ea42009-08-18 18:06:24 +010038 * modular as event for a given driver, different usecases may require
Jonathan Cameron14555b12011-09-21 11:15:57 +010039 * different buffer designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010040 *
Jonathan Cameron14555b12011-09-21 11:15:57 +010041 * It is worth noting that a given buffer implementation may only support a
42 * small proportion of these functions. The core code 'should' cope fine with
43 * any of them not existing.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010044 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +010045struct iio_buffer_access_funcs {
46 void (*mark_in_use)(struct iio_buffer *buffer);
47 void (*unmark_in_use)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010048
Jonathan Cameron14555b12011-09-21 11:15:57 +010049 int (*store_to)(struct iio_buffer *buffer, u8 *data, s64 timestamp);
Jonathan Cameron14555b12011-09-21 11:15:57 +010050 int (*read_first_n)(struct iio_buffer *buffer,
Jonathan Cameronb4281732011-04-15 18:55:55 +010051 size_t n,
Jonathan Cameronb26a2182011-05-18 14:41:02 +010052 char __user *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010053
Jonathan Cameron14555b12011-09-21 11:15:57 +010054 int (*mark_param_change)(struct iio_buffer *buffer);
55 int (*request_update)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010056
Jonathan Cameron14555b12011-09-21 11:15:57 +010057 int (*get_bytes_per_datum)(struct iio_buffer *buffer);
58 int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
59 int (*get_length)(struct iio_buffer *buffer);
60 int (*set_length)(struct iio_buffer *buffer, int length);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010061
Jonathan Cameron14555b12011-09-21 11:15:57 +010062 int (*is_enabled)(struct iio_buffer *buffer);
63 int (*enable)(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010064};
65
Jonathan Cameron6446e9c2011-08-30 12:41:13 +010066/**
Jonathan Cameron14555b12011-09-21 11:15:57 +010067 * struct iio_buffer - general buffer structure
Jonathan Cameron14555b12011-09-21 11:15:57 +010068 * @length: [DEVICE] number of datums in buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010069 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
Manuel Stahlbf329632010-08-31 11:32:52 +020070 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
71 * control method is used
Manuel Stahlbf329632010-08-31 11:32:52 +020072 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
Jonathan Cameron5f070a32011-12-05 22:18:30 +000073 * @scan_index_timestamp:[INTERN] cache of the index to the timestamp
Manuel Stahlbf329632010-08-31 11:32:52 +020074 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
Jonathan Cameron14555b12011-09-21 11:15:57 +010075 * @access: [DRIVER] buffer access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +010076 * implementation.
Jonathan Cameron5f070a32011-12-05 22:18:30 +000077 * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
78 * @scan_el_group: [DRIVER] attribute group for those attributes not
79 * created from the iio_chan_info array.
80 * @pollq: [INTERN] wait queue to allow for polling on the buffer.
81 * @stufftoread: [INTERN] flag to indicate new data.
Jonathan Cameron63564632011-08-30 12:32:46 +010082 * @flags: [INTERN] file ops related flags including busy flag.
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +000083 * @demux_list: [INTERN] list of operations required to demux the scan.
84 * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
Jonathan Cameron8d213f22011-05-18 14:42:34 +010085 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +010086struct iio_buffer {
Jonathan Cameron8d213f22011-05-18 14:42:34 +010087 int length;
88 int bytes_per_datum;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010089 struct attribute_group *scan_el_attrs;
Jonathan Cameron32b5eec2011-09-02 17:14:38 +010090 long *scan_mask;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010091 bool scan_timestamp;
Jonathan Cameronbeb80602011-12-05 21:37:11 +000092 unsigned scan_index_timestamp;
Jonathan Cameron14555b12011-09-21 11:15:57 +010093 const struct iio_buffer_access_funcs *access;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010094 struct list_head scan_el_dev_attr_list;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +010095 struct attribute_group scan_el_group;
Jonathan Cameron8d213f22011-05-18 14:42:34 +010096 wait_queue_head_t pollq;
97 bool stufftoread;
Jonathan Cameron63564632011-08-30 12:32:46 +010098 unsigned long flags;
Jonathan Cameron1aa04272011-08-30 12:32:47 +010099 const struct attribute_group *attrs;
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000100 struct list_head demux_list;
101 unsigned char *demux_bounce;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100102};
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100103
104/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100105 * iio_buffer_init() - Initialize the buffer structure
106 * @buffer: buffer to be initialized
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100107 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000108void iio_buffer_init(struct iio_buffer *buffer);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100109
Jonathan Cameron14555b12011-09-21 11:15:57 +0100110void iio_buffer_deinit(struct iio_buffer *buffer);
Jonathan Cameron32b5eec2011-09-02 17:14:38 +0100111
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100112/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100113 * __iio_update_buffer() - update common elements of buffers
114 * @buffer: buffer that is the event source
Randy Dunlap4c572602009-10-04 19:34:02 -0700115 * @bytes_per_datum: size of individual datum including timestamp
Jonathan Cameron14555b12011-09-21 11:15:57 +0100116 * @length: number of datums in buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100117 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100118static inline void __iio_update_buffer(struct iio_buffer *buffer,
119 int bytes_per_datum, int length)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100120{
Jonathan Cameron14555b12011-09-21 11:15:57 +0100121 buffer->bytes_per_datum = bytes_per_datum;
122 buffer->length = length;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100123}
124
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000125int iio_scan_mask_query(struct iio_dev *indio_dev,
126 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200127
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100128/**
129 * iio_scan_mask_set() - set particular bit in the scan mask
Jonathan Cameron14555b12011-09-21 11:15:57 +0100130 * @buffer: the buffer whose scan mask we are interested in
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100131 * @bit: the bit to be set.
132 **/
Jonathan Cameronf79a9092011-12-05 22:18:29 +0000133int iio_scan_mask_set(struct iio_dev *indio_dev,
134 struct iio_buffer *buffer, int bit);
Manuel Stahlbf329632010-08-31 11:32:52 +0200135
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100136/**
Jonathan Cameron5ada4ea2011-12-05 21:37:14 +0000137 * iio_push_to_buffer() - push to a registered buffer.
138 * @buffer: IIO buffer structure for device
139 * @scan: Full scan.
140 * @timestamp:
141 */
142int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data,
143 s64 timestamp);
144
145int iio_update_demux(struct iio_dev *indio_dev);
146
147/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100148 * iio_buffer_register() - register the buffer with IIO core
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100149 * @indio_dev: device with the buffer to be registered
Jonathan Cameron1d892712011-05-18 14:40:51 +0100150 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100151int iio_buffer_register(struct iio_dev *indio_dev,
152 const struct iio_chan_spec *channels,
153 int num_channels);
Jonathan Cameron1d892712011-05-18 14:40:51 +0100154
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100155/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100156 * iio_buffer_unregister() - unregister the buffer from IIO core
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100157 * @indio_dev: the device with the buffer to be unregistered
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100158 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100159void iio_buffer_unregister(struct iio_dev *indio_dev);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100160
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100161/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100162 * iio_buffer_read_length() - attr func to get number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100163 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100164ssize_t iio_buffer_read_length(struct device *dev,
165 struct device_attribute *attr,
166 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100167/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100168 * iio_buffer_write_length() - attr func to set number of datums in the buffer
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100169 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100170ssize_t iio_buffer_write_length(struct device *dev,
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100171 struct device_attribute *attr,
172 const char *buf,
173 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100174/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100175 * iio_buffer_store_enable() - attr to turn the buffer on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100176 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100177ssize_t iio_buffer_store_enable(struct device *dev,
178 struct device_attribute *attr,
179 const char *buf,
180 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100181/**
Jonathan Cameron14555b12011-09-21 11:15:57 +0100182 * iio_buffer_show_enable() - attr to see if the buffer is on
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100183 **/
Jonathan Cameron14555b12011-09-21 11:15:57 +0100184ssize_t iio_buffer_show_enable(struct device *dev,
185 struct device_attribute *attr,
186 char *buf);
187#define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
188 iio_buffer_read_length, \
189 iio_buffer_write_length)
Jonathan Cameron5565a452011-05-18 14:42:24 +0100190
Jonathan Cameron14555b12011-09-21 11:15:57 +0100191#define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
192 iio_buffer_show_enable, \
193 iio_buffer_store_enable)
194
195int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
Jonathan Cameron5565a452011-05-18 14:42:24 +0100196
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100197#else /* CONFIG_IIO_BUFFER */
Jonathan Cameron1d892712011-05-18 14:40:51 +0100198
Jonathan Cameron14555b12011-09-21 11:15:57 +0100199static inline int iio_buffer_register(struct iio_dev *indio_dev,
Jonathan Cameronc009f7e2011-08-30 12:41:14 +0100200 struct iio_chan_spec *channels,
201 int num_channels)
Jonathan Cameron1d892712011-05-18 14:40:51 +0100202{
203 return 0;
204}
205
Jonathan Cameron14555b12011-09-21 11:15:57 +0100206static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
Jonathan Cameron26620512010-07-11 16:39:14 +0100207{};
208
Jonathan Cameronf2a96242011-09-21 11:15:55 +0100209#endif /* CONFIG_IIO_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100210
Jonathan Cameron3811cd62011-09-21 11:15:56 +0100211#endif /* _IIO_BUFFER_GENERIC_H_ */