2011-09-21 18:15:57 +08:00
|
|
|
/* The industrial I/O core - generic buffer interfaces.
|
2009-08-19 01:06:24 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2008 Jonathan Cameron
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 as published by
|
|
|
|
* the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
2011-09-21 18:15:56 +08:00
|
|
|
#ifndef _IIO_BUFFER_GENERIC_H_
|
|
|
|
#define _IIO_BUFFER_GENERIC_H_
|
2011-09-03 00:14:40 +08:00
|
|
|
#include <linux/sysfs.h>
|
2012-04-25 22:54:58 +08:00
|
|
|
#include <linux/iio/iio.h>
|
2013-10-04 19:06:00 +08:00
|
|
|
#include <linux/kref.h>
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2011-09-21 18:15:55 +08:00
|
|
|
#ifdef CONFIG_IIO_BUFFER
|
2010-07-11 23:39:14 +08:00
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
struct iio_buffer;
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2017-01-03 03:28:26 +08:00
|
|
|
void iio_buffer_set_attrs(struct iio_buffer *buffer,
|
|
|
|
const struct attribute **attrs);
|
2015-10-14 00:10:26 +08:00
|
|
|
/**
|
|
|
|
* INDIO_BUFFER_FLAG_FIXED_WATERMARK - Watermark level of the buffer can not be
|
|
|
|
* configured. It has a fixed value which will be buffer specific.
|
|
|
|
*/
|
|
|
|
#define INDIO_BUFFER_FLAG_FIXED_WATERMARK BIT(0)
|
|
|
|
|
2009-08-19 01:06:24 +08:00
|
|
|
/**
|
2011-09-21 18:15:57 +08:00
|
|
|
* struct iio_buffer_access_funcs - access functions for buffers.
|
|
|
|
* @store_to: actually store stuff to the buffer
|
2011-12-12 16:33:14 +08:00
|
|
|
* @read_first_n: try to get a specified number of bytes (must exist)
|
2015-03-23 02:33:38 +08:00
|
|
|
* @data_available: indicates how much data is available for reading from
|
|
|
|
* the buffer.
|
2009-08-19 01:06:24 +08:00
|
|
|
* @request_update: if a parameter change has been marked, update underlying
|
|
|
|
* storage.
|
2010-09-05 00:54:45 +08:00
|
|
|
* @set_bytes_per_datum:set number of bytes per datum
|
2011-09-21 18:15:57 +08:00
|
|
|
* @set_length: set number of datums in buffer
|
2015-10-14 00:10:27 +08:00
|
|
|
* @enable: called if the buffer is attached to a device and the
|
|
|
|
* device starts sampling. Calls are balanced with
|
|
|
|
* @disable.
|
|
|
|
* @disable: called if the buffer is attached to a device and the
|
|
|
|
* device stops sampling. Calles are balanced with @enable.
|
2013-10-04 19:06:00 +08:00
|
|
|
* @release: called when the last reference to the buffer is dropped,
|
|
|
|
* should free all resources allocated by the buffer.
|
2015-05-30 00:14:21 +08:00
|
|
|
* @modes: Supported operating modes by this buffer type
|
2015-10-14 00:10:26 +08:00
|
|
|
* @flags: A bitmask combination of INDIO_BUFFER_FLAG_*
|
2009-08-19 01:06:24 +08:00
|
|
|
*
|
2011-09-21 18:15:57 +08:00
|
|
|
* The purpose of this structure is to make the buffer element
|
2009-08-19 01:06:24 +08:00
|
|
|
* modular as event for a given driver, different usecases may require
|
2011-09-21 18:15:57 +08:00
|
|
|
* different buffer designs (space efficiency vs speed for example).
|
2009-08-19 01:06:24 +08:00
|
|
|
*
|
2011-09-21 18:15:57 +08:00
|
|
|
* It is worth noting that a given buffer implementation may only support a
|
|
|
|
* small proportion of these functions. The core code 'should' cope fine with
|
|
|
|
* any of them not existing.
|
2009-08-19 01:06:24 +08:00
|
|
|
**/
|
2011-09-21 18:15:57 +08:00
|
|
|
struct iio_buffer_access_funcs {
|
2013-09-16 00:50:00 +08:00
|
|
|
int (*store_to)(struct iio_buffer *buffer, const void *data);
|
2011-09-21 18:15:57 +08:00
|
|
|
int (*read_first_n)(struct iio_buffer *buffer,
|
2011-04-16 01:55:55 +08:00
|
|
|
size_t n,
|
2011-05-18 21:41:02 +08:00
|
|
|
char __user *buf);
|
2015-03-23 02:33:38 +08:00
|
|
|
size_t (*data_available)(struct iio_buffer *buffer);
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
int (*request_update)(struct iio_buffer *buffer);
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
|
|
|
|
int (*set_length)(struct iio_buffer *buffer, int length);
|
2013-10-04 19:06:00 +08:00
|
|
|
|
2015-10-14 00:10:27 +08:00
|
|
|
int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
|
|
|
|
int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
|
|
|
|
|
2013-10-04 19:06:00 +08:00
|
|
|
void (*release)(struct iio_buffer *buffer);
|
2015-05-30 00:14:21 +08:00
|
|
|
|
|
|
|
unsigned int modes;
|
2015-10-14 00:10:26 +08:00
|
|
|
unsigned int flags;
|
2009-08-19 01:06:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-09-21 18:15:57 +08:00
|
|
|
* struct iio_buffer - general buffer structure
|
2017-01-03 03:28:25 +08:00
|
|
|
*
|
|
|
|
* Note that the internals of this structure should only be of interest to
|
|
|
|
* those writing new buffer implementations.
|
2012-07-01 03:06:00 +08:00
|
|
|
*/
|
2011-09-21 18:15:57 +08:00
|
|
|
struct iio_buffer {
|
2017-01-03 03:28:25 +08:00
|
|
|
/** @length: Number of datums in buffer. */
|
|
|
|
int length;
|
|
|
|
|
|
|
|
/** @bytes_per_datum: Size of individual datum including timestamp. */
|
|
|
|
int bytes_per_datum;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @access: Buffer access functions associated with the
|
|
|
|
* implementation.
|
|
|
|
*/
|
|
|
|
const struct iio_buffer_access_funcs *access;
|
|
|
|
|
|
|
|
/** @scan_mask: Bitmask used in masking scan mode elements. */
|
|
|
|
long *scan_mask;
|
|
|
|
|
|
|
|
/** @demux_list: List of operations required to demux the scan. */
|
|
|
|
struct list_head demux_list;
|
|
|
|
|
|
|
|
/** @pollq: Wait queue to allow for polling on the buffer. */
|
|
|
|
wait_queue_head_t pollq;
|
|
|
|
|
|
|
|
/** @watermark: Number of datums to wait for poll/read. */
|
|
|
|
unsigned int watermark;
|
|
|
|
|
|
|
|
/* private: */
|
|
|
|
/*
|
|
|
|
* @scan_el_attrs: Control of scan elements if that scan mode
|
|
|
|
* control method is used.
|
|
|
|
*/
|
|
|
|
struct attribute_group *scan_el_attrs;
|
|
|
|
|
|
|
|
/* @scan_timestamp: Does the scan mode include a timestamp. */
|
|
|
|
bool scan_timestamp;
|
|
|
|
|
|
|
|
/* @scan_el_dev_attr_list: List of scan element related attributes. */
|
|
|
|
struct list_head scan_el_dev_attr_list;
|
|
|
|
|
|
|
|
/* @buffer_group: Attributes of the buffer group. */
|
|
|
|
struct attribute_group buffer_group;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @scan_el_group: Attribute group for those attributes not
|
|
|
|
* created from the iio_chan_info array.
|
|
|
|
*/
|
|
|
|
struct attribute_group scan_el_group;
|
|
|
|
|
|
|
|
/* @stufftoread: Flag to indicate new data. */
|
|
|
|
bool stufftoread;
|
|
|
|
|
|
|
|
/* @attrs: Standard attributes of the buffer. */
|
|
|
|
const struct attribute **attrs;
|
|
|
|
|
|
|
|
/* @demux_bounce: Buffer for doing gather from incoming scan. */
|
|
|
|
void *demux_bounce;
|
|
|
|
|
|
|
|
/* @buffer_list: Entry in the devices list of current buffers. */
|
|
|
|
struct list_head buffer_list;
|
|
|
|
|
|
|
|
/* @ref: Reference count of the buffer. */
|
|
|
|
struct kref ref;
|
2009-08-19 01:06:24 +08:00
|
|
|
};
|
2010-09-05 00:54:45 +08:00
|
|
|
|
2012-07-01 03:06:00 +08:00
|
|
|
/**
|
|
|
|
* iio_update_buffers() - add or remove buffer from active list
|
|
|
|
* @indio_dev: device to add buffer to
|
|
|
|
* @insert_buffer: buffer to insert
|
|
|
|
* @remove_buffer: buffer_to_remove
|
|
|
|
*
|
|
|
|
* Note this will tear down the all buffering and build it up again
|
|
|
|
*/
|
|
|
|
int iio_update_buffers(struct iio_dev *indio_dev,
|
|
|
|
struct iio_buffer *insert_buffer,
|
|
|
|
struct iio_buffer *remove_buffer);
|
|
|
|
|
2010-09-05 00:54:45 +08:00
|
|
|
/**
|
2011-09-21 18:15:57 +08:00
|
|
|
* iio_buffer_init() - Initialize the buffer structure
|
2012-07-01 06:47:43 +08:00
|
|
|
* @buffer: buffer to be initialized
|
2010-09-05 00:54:45 +08:00
|
|
|
**/
|
2011-12-06 06:18:29 +08:00
|
|
|
void iio_buffer_init(struct iio_buffer *buffer);
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2013-09-16 00:50:00 +08:00
|
|
|
int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
|
2011-12-06 05:37:14 +08:00
|
|
|
|
2017-01-03 03:28:29 +08:00
|
|
|
/**
|
2013-09-19 20:59:00 +08:00
|
|
|
* iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
|
|
|
|
* @indio_dev: iio_dev structure for device.
|
|
|
|
* @data: sample data
|
|
|
|
* @timestamp: timestamp for the sample data
|
|
|
|
*
|
|
|
|
* Pushes data to the IIO device's buffers. If timestamps are enabled for the
|
|
|
|
* device the function will store the supplied timestamp as the last element in
|
|
|
|
* the sample data buffer before pushing it to the device buffers. The sample
|
|
|
|
* data buffer needs to be large enough to hold the additional timestamp
|
|
|
|
* (usually the buffer should be indio->scan_bytes bytes large).
|
|
|
|
*
|
|
|
|
* Returns 0 on success, a negative error code otherwise.
|
|
|
|
*/
|
|
|
|
static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
|
|
|
|
void *data, int64_t timestamp)
|
|
|
|
{
|
|
|
|
if (indio_dev->scan_timestamp) {
|
|
|
|
size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
|
|
|
|
((int64_t *)data)[ts_offset] = timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iio_push_to_buffers(indio_dev, data);
|
|
|
|
}
|
|
|
|
|
2012-07-09 17:00:00 +08:00
|
|
|
bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
|
|
|
|
const unsigned long *mask);
|
|
|
|
|
2013-10-04 19:06:00 +08:00
|
|
|
struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
|
|
|
|
void iio_buffer_put(struct iio_buffer *buffer);
|
|
|
|
|
2017-01-03 03:28:32 +08:00
|
|
|
void iio_device_attach_buffer(struct iio_dev *indio_dev,
|
|
|
|
struct iio_buffer *buffer);
|
2013-10-04 19:06:00 +08:00
|
|
|
|
2011-09-21 18:15:55 +08:00
|
|
|
#else /* CONFIG_IIO_BUFFER */
|
2011-05-18 21:40:51 +08:00
|
|
|
|
2013-10-04 19:06:00 +08:00
|
|
|
static inline void iio_buffer_get(struct iio_buffer *buffer) {}
|
|
|
|
static inline void iio_buffer_put(struct iio_buffer *buffer) {}
|
|
|
|
|
2011-09-21 18:15:55 +08:00
|
|
|
#endif /* CONFIG_IIO_BUFFER */
|
2009-08-19 01:06:24 +08:00
|
|
|
|
2011-09-21 18:15:56 +08:00
|
|
|
#endif /* _IIO_BUFFER_GENERIC_H_ */
|