Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright 2013-2015 Analog Devices Inc.
0004  *  Author: Lars-Peter Clausen <lars@metafoo.de>
0005  */
0006 
0007 #ifndef __INDUSTRIALIO_DMA_BUFFER_H__
0008 #define __INDUSTRIALIO_DMA_BUFFER_H__
0009 
0010 #include <linux/list.h>
0011 #include <linux/kref.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/mutex.h>
0014 #include <linux/iio/buffer_impl.h>
0015 
0016 struct iio_dma_buffer_queue;
0017 struct iio_dma_buffer_ops;
0018 struct device;
0019 
0020 /**
0021  * enum iio_block_state - State of a struct iio_dma_buffer_block
0022  * @IIO_BLOCK_STATE_DEQUEUED: Block is not queued
0023  * @IIO_BLOCK_STATE_QUEUED: Block is on the incoming queue
0024  * @IIO_BLOCK_STATE_ACTIVE: Block is currently being processed by the DMA
0025  * @IIO_BLOCK_STATE_DONE: Block is on the outgoing queue
0026  * @IIO_BLOCK_STATE_DEAD: Block has been marked as to be freed
0027  */
0028 enum iio_block_state {
0029     IIO_BLOCK_STATE_DEQUEUED,
0030     IIO_BLOCK_STATE_QUEUED,
0031     IIO_BLOCK_STATE_ACTIVE,
0032     IIO_BLOCK_STATE_DONE,
0033     IIO_BLOCK_STATE_DEAD,
0034 };
0035 
0036 /**
0037  * struct iio_dma_buffer_block - IIO buffer block
0038  * @head: List head
0039  * @size: Total size of the block in bytes
0040  * @bytes_used: Number of bytes that contain valid data
0041  * @vaddr: Virutal address of the blocks memory
0042  * @phys_addr: Physical address of the blocks memory
0043  * @queue: Parent DMA buffer queue
0044  * @kref: kref used to manage the lifetime of block
0045  * @state: Current state of the block
0046  */
0047 struct iio_dma_buffer_block {
0048     /* May only be accessed by the owner of the block */
0049     struct list_head head;
0050     size_t bytes_used;
0051 
0052     /*
0053      * Set during allocation, constant thereafter. May be accessed read-only
0054      * by anybody holding a reference to the block.
0055      */
0056     void *vaddr;
0057     dma_addr_t phys_addr;
0058     size_t size;
0059     struct iio_dma_buffer_queue *queue;
0060 
0061     /* Must not be accessed outside the core. */
0062     struct kref kref;
0063     /*
0064      * Must not be accessed outside the core. Access needs to hold
0065      * queue->list_lock if the block is not owned by the core.
0066      */
0067     enum iio_block_state state;
0068 };
0069 
0070 /**
0071  * struct iio_dma_buffer_queue_fileio - FileIO state for the DMA buffer
0072  * @blocks: Buffer blocks used for fileio
0073  * @active_block: Block being used in read()
0074  * @pos: Read offset in the active block
0075  * @block_size: Size of each block
0076  */
0077 struct iio_dma_buffer_queue_fileio {
0078     struct iio_dma_buffer_block *blocks[2];
0079     struct iio_dma_buffer_block *active_block;
0080     size_t pos;
0081     size_t block_size;
0082 };
0083 
0084 /**
0085  * struct iio_dma_buffer_queue - DMA buffer base structure
0086  * @buffer: IIO buffer base structure
0087  * @dev: Parent device
0088  * @ops: DMA buffer callbacks
0089  * @lock: Protects the incoming list, active and the fields in the fileio
0090  *   substruct
0091  * @list_lock: Protects lists that contain blocks which can be modified in
0092  *   atomic context as well as blocks on those lists. This is the outgoing queue
0093  *   list and typically also a list of active blocks in the part that handles
0094  *   the DMA controller
0095  * @incoming: List of buffers on the incoming queue
0096  * @outgoing: List of buffers on the outgoing queue
0097  * @active: Whether the buffer is currently active
0098  * @fileio: FileIO state
0099  */
0100 struct iio_dma_buffer_queue {
0101     struct iio_buffer buffer;
0102     struct device *dev;
0103     const struct iio_dma_buffer_ops *ops;
0104 
0105     struct mutex lock;
0106     spinlock_t list_lock;
0107     struct list_head incoming;
0108     struct list_head outgoing;
0109 
0110     bool active;
0111 
0112     struct iio_dma_buffer_queue_fileio fileio;
0113 };
0114 
0115 /**
0116  * struct iio_dma_buffer_ops - DMA buffer callback operations
0117  * @submit: Called when a block is submitted to the DMA controller
0118  * @abort: Should abort all pending transfers
0119  */
0120 struct iio_dma_buffer_ops {
0121     int (*submit)(struct iio_dma_buffer_queue *queue,
0122         struct iio_dma_buffer_block *block);
0123     void (*abort)(struct iio_dma_buffer_queue *queue);
0124 };
0125 
0126 void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block);
0127 void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
0128     struct list_head *list);
0129 
0130 int iio_dma_buffer_enable(struct iio_buffer *buffer,
0131     struct iio_dev *indio_dev);
0132 int iio_dma_buffer_disable(struct iio_buffer *buffer,
0133     struct iio_dev *indio_dev);
0134 int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
0135     char __user *user_buffer);
0136 size_t iio_dma_buffer_data_available(struct iio_buffer *buffer);
0137 int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd);
0138 int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length);
0139 int iio_dma_buffer_request_update(struct iio_buffer *buffer);
0140 
0141 int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
0142     struct device *dma_dev, const struct iio_dma_buffer_ops *ops);
0143 void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue);
0144 void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue);
0145 
0146 #endif