Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright 2013-2015 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *  Author: Lars-Peter Clausen <lars@metafoo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/iio/buffer_impl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/iio/buffer-dma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * For DMA buffers the storage is sub-divided into so called blocks. Each block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * has its own memory buffer. The size of the block is the granularity at which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * memory is exchanged between the hardware and the application. Increasing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * basic unit of data exchange from one sample to one block decreases the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * management overhead that is associated with each sample. E.g. if we say the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * management overhead for one exchange is x and the unit of exchange is one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * sample the overhead will be x for each sample. Whereas when using a block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * which contains n samples the overhead per sample is reduced to x/n. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * allows to achieve much higher samplerates than what can be sustained with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * the one sample approach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * Blocks are exchanged between the DMA controller and the application via the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * means of two queues. The incoming queue and the outgoing queue. Blocks on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * incoming queue are waiting for the DMA controller to pick them up and fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * them with data. Block on the outgoing queue have been filled with data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * are waiting for the application to dequeue them and read the data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * A block can be in one of the following states:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *  * Owned by the application. In this state the application can read data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *    the block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *  * On the incoming list: Blocks on the incoming list are queued up to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *    processed by the DMA controller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *  * Owned by the DMA controller: The DMA controller is processing the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *    and filling it with data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *  * On the outgoing list: Blocks on the outgoing list have been successfully
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *    processed by the DMA controller and contain data. They can be dequeued by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *    the application.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *  * Dead: A block that is dead has been marked as to be freed. It might still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *    be owned by either the application or the DMA controller at the moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *    But once they are done processing it instead of going to either the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *    incoming or outgoing queue the block will be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * In addition to this blocks are reference counted and the memory associated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * with both the block structure as well as the storage memory for the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * will be freed when the last reference to the block is dropped. This means a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * block must not be accessed without holding a reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * The iio_dma_buffer implementation provides a generic infrastructure for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * managing the blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * A driver for a specific piece of hardware that has DMA capabilities need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * implement the submit() callback from the iio_dma_buffer_ops structure. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * callback is supposed to initiate the DMA transfer copying data from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  * converter to the memory region of the block. Once the DMA transfer has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * completed the driver must call iio_dma_buffer_block_done() for the completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * Prior to this it must set the bytes_used field of the block contains
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * the actual number of bytes in the buffer. Typically this will be equal to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * size of the block, but if the DMA hardware has certain alignment requirements
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * for the transfer length it might choose to use less than the full size. In
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * either case it is expected that bytes_used is a multiple of the bytes per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * datum, i.e. the block must not contain partial samples.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * The driver must call iio_dma_buffer_block_done() for each block it has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * received through its submit_block() callback, even if it does not actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * perform a DMA transfer for the block, e.g. because the buffer was disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  * before the block transfer was started. In this case it should set bytes_used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * to 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * In addition it is recommended that a driver implements the abort() callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * It will be called when the buffer is disabled and can be used to cancel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * pending and stop active transfers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * The specific driver implementation should use the default callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * implementations provided by this module for the iio_buffer_access_funcs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * struct. It may overload some callbacks with custom variants if the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * has special requirements that are not handled by the generic functions. If a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * driver chooses to overload a callback it has to ensure that the generic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  * callback is called from within the custom callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static void iio_buffer_block_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct iio_dma_buffer_block *block = container_of(kref,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		struct iio_dma_buffer_block, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	WARN_ON(block->state != IIO_BLOCK_STATE_DEAD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	dma_free_coherent(block->queue->dev, PAGE_ALIGN(block->size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 					block->vaddr, block->phys_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	iio_buffer_put(&block->queue->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	kfree(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void iio_buffer_block_get(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	kref_get(&block->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static void iio_buffer_block_put(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	kref_put(&block->kref, iio_buffer_block_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  * dma_free_coherent can sleep, hence we need to take some special care to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * able to drop a reference from an atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static LIST_HEAD(iio_dma_buffer_dead_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static DEFINE_SPINLOCK(iio_dma_buffer_dead_blocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static void iio_dma_buffer_cleanup_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct iio_dma_buffer_block *block, *_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	LIST_HEAD(block_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	spin_lock_irq(&iio_dma_buffer_dead_blocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	list_splice_tail_init(&iio_dma_buffer_dead_blocks, &block_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	spin_unlock_irq(&iio_dma_buffer_dead_blocks_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	list_for_each_entry_safe(block, _block, &block_list, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		iio_buffer_block_release(&block->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static DECLARE_WORK(iio_dma_buffer_cleanup_work, iio_dma_buffer_cleanup_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) static void iio_buffer_block_release_atomic(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	block = container_of(kref, struct iio_dma_buffer_block, kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	spin_lock_irqsave(&iio_dma_buffer_dead_blocks_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	list_add_tail(&block->head, &iio_dma_buffer_dead_blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	spin_unlock_irqrestore(&iio_dma_buffer_dead_blocks_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	schedule_work(&iio_dma_buffer_cleanup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * Version of iio_buffer_block_put() that can be called from atomic context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void iio_buffer_block_put_atomic(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	kref_put(&block->kref, iio_buffer_block_release_atomic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static struct iio_dma_buffer_queue *iio_buffer_to_queue(struct iio_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return container_of(buf, struct iio_dma_buffer_queue, buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static struct iio_dma_buffer_block *iio_dma_buffer_alloc_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	struct iio_dma_buffer_queue *queue, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	block = kzalloc(sizeof(*block), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if (!block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	block->vaddr = dma_alloc_coherent(queue->dev, PAGE_ALIGN(size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		&block->phys_addr, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (!block->vaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		kfree(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	block->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	block->state = IIO_BLOCK_STATE_DEQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	block->queue = queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	INIT_LIST_HEAD(&block->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	kref_init(&block->kref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	iio_buffer_get(&queue->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	return block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void _iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	struct iio_dma_buffer_queue *queue = block->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	 * The buffer has already been freed by the application, just drop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	 * reference.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	if (block->state != IIO_BLOCK_STATE_DEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		block->state = IIO_BLOCK_STATE_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		list_add_tail(&block->head, &queue->outgoing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  * iio_dma_buffer_block_done() - Indicate that a block has been completed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)  * @block: The completed block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * Should be called when the DMA controller has finished handling the block to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * pass back ownership of the block to the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct iio_dma_buffer_queue *queue = block->queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	spin_lock_irqsave(&queue->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	_iio_dma_buffer_block_done(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	spin_unlock_irqrestore(&queue->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	iio_buffer_block_put_atomic(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) EXPORT_SYMBOL_GPL(iio_dma_buffer_block_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * iio_dma_buffer_block_list_abort() - Indicate that a list block has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  *   aborted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * @queue: Queue for which to complete blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * @list: List of aborted blocks. All blocks in this list must be from @queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * Typically called from the abort() callback after the DMA controller has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  * stopped. This will set bytes_used to 0 for each block in the list and then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * hand the blocks back to the queue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	struct iio_dma_buffer_block *block, *_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	spin_lock_irqsave(&queue->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	list_for_each_entry_safe(block, _block, list, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		list_del(&block->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		block->bytes_used = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		_iio_dma_buffer_block_done(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		iio_buffer_block_put_atomic(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	spin_unlock_irqrestore(&queue->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	wake_up_interruptible_poll(&queue->buffer.pollq, EPOLLIN | EPOLLRDNORM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) EXPORT_SYMBOL_GPL(iio_dma_buffer_block_list_abort);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) static bool iio_dma_block_reusable(struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	 * If the core owns the block it can be re-used. This should be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	 * default case when enabling the buffer, unless the DMA controller does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	 * not support abort and has not given back the block yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	switch (block->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	case IIO_BLOCK_STATE_DEQUEUED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	case IIO_BLOCK_STATE_QUEUED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case IIO_BLOCK_STATE_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  * iio_dma_buffer_request_update() - DMA buffer request_update callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)  * @buffer: The buffer which to request an update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  * Should be used as the iio_dma_buffer_request_update() callback for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  * iio_buffer_access_ops struct for DMA buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int iio_dma_buffer_request_update(struct iio_buffer *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	bool try_reuse = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * Split the buffer into two even parts. This is used as a double
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * buffering scheme with usually one block at a time being used by the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * DMA and the other one by the application.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	size = DIV_ROUND_UP(queue->buffer.bytes_per_datum *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		queue->buffer.length, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	/* Allocations are page aligned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (PAGE_ALIGN(queue->fileio.block_size) == PAGE_ALIGN(size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		try_reuse = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	queue->fileio.block_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	queue->fileio.active_block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	spin_lock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		block = queue->fileio.blocks[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		/* If we can't re-use it free it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (block && (!iio_dma_block_reusable(block) || !try_reuse))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			block->state = IIO_BLOCK_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	 * At this point all blocks are either owned by the core or marked as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	 * dead. This means we can reset the lists without having to fear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	 * corrution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	INIT_LIST_HEAD(&queue->outgoing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	spin_unlock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	INIT_LIST_HEAD(&queue->incoming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		if (queue->fileio.blocks[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 			block = queue->fileio.blocks[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			if (block->state == IIO_BLOCK_STATE_DEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 				/* Could not reuse it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 				iio_buffer_block_put(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 				block->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (!block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			block = iio_dma_buffer_alloc_block(queue, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			if (!block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 				goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			queue->fileio.blocks[i] = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		block->state = IIO_BLOCK_STATE_QUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		list_add_tail(&block->head, &queue->incoming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) EXPORT_SYMBOL_GPL(iio_dma_buffer_request_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static void iio_dma_buffer_submit_block(struct iio_dma_buffer_queue *queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 * If the hardware has already been removed we put the block into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * limbo. It will neither be on the incoming nor outgoing list, nor will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * it ever complete. It will just wait to be freed eventually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if (!queue->ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	block->state = IIO_BLOCK_STATE_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	iio_buffer_block_get(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	ret = queue->ops->submit(queue, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		 * This is a bit of a problem and there is not much we can do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		 * other then wait for the buffer to be disabled and re-enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		 * and try again. But it should not really happen unless we run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		 * out of memory or something similar.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		 * TODO: Implement support in the IIO core to allow buffers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		 * notify consumers that something went wrong and the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		 * should be disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		iio_buffer_block_put(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  * iio_dma_buffer_enable() - Enable DMA buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * @buffer: IIO buffer to enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  * @indio_dev: IIO device the buffer is attached to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  * Needs to be called when the device that the buffer is attached to starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * sampling. Typically should be the iio_buffer_access_ops enable callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  * This will allocate the DMA buffers and start the DMA transfers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int iio_dma_buffer_enable(struct iio_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct iio_dma_buffer_block *block, *_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	queue->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	list_for_each_entry_safe(block, _block, &queue->incoming, head) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		list_del(&block->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		iio_dma_buffer_submit_block(queue, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) EXPORT_SYMBOL_GPL(iio_dma_buffer_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * iio_dma_buffer_disable() - Disable DMA buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * @buffer: IIO DMA buffer to disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * @indio_dev: IIO device the buffer is attached to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  * Needs to be called when the device that the buffer is attached to stops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * sampling. Typically should be the iio_buffer_access_ops disable callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) int iio_dma_buffer_disable(struct iio_buffer *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	struct iio_dev *indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	queue->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (queue->ops && queue->ops->abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		queue->ops->abort(queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) EXPORT_SYMBOL_GPL(iio_dma_buffer_disable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) static void iio_dma_buffer_enqueue(struct iio_dma_buffer_queue *queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct iio_dma_buffer_block *block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (block->state == IIO_BLOCK_STATE_DEAD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		iio_buffer_block_put(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	} else if (queue->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		iio_dma_buffer_submit_block(queue, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		block->state = IIO_BLOCK_STATE_QUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		list_add_tail(&block->head, &queue->incoming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static struct iio_dma_buffer_block *iio_dma_buffer_dequeue(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct iio_dma_buffer_queue *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	spin_lock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	block = list_first_entry_or_null(&queue->outgoing, struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		iio_dma_buffer_block, head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (block != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		list_del(&block->head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		block->state = IIO_BLOCK_STATE_DEQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	spin_unlock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	return block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)  * iio_dma_buffer_read() - DMA buffer read callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)  * @buffer: Buffer to read form
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)  * @n: Number of bytes to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)  * @user_buffer: Userspace buffer to copy the data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)  * Should be used as the read callback for iio_buffer_access_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)  * struct for DMA buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	char __user *user_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (n < buffer->bytes_per_datum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (!queue->fileio.active_block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		block = iio_dma_buffer_dequeue(queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		if (block == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		queue->fileio.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 		queue->fileio.active_block = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		block = queue->fileio.active_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	n = rounddown(n, buffer->bytes_per_datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	if (n > block->bytes_used - queue->fileio.pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		n = block->bytes_used - queue->fileio.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	if (copy_to_user(user_buffer, block->vaddr + queue->fileio.pos, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	queue->fileio.pos += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (queue->fileio.pos == block->bytes_used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		queue->fileio.active_block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		iio_dma_buffer_enqueue(queue, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	ret = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) EXPORT_SYMBOL_GPL(iio_dma_buffer_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)  * iio_dma_buffer_data_available() - DMA buffer data_available callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)  * @buf: Buffer to check for data availability
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  * Should be used as the data_available callback for iio_buffer_access_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  * struct for DMA buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) size_t iio_dma_buffer_data_available(struct iio_buffer *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	struct iio_dma_buffer_queue *queue = iio_buffer_to_queue(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	struct iio_dma_buffer_block *block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	size_t data_available = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	 * For counting the available bytes we'll use the size of the block not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	 * the number of actual bytes available in the block. Otherwise it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	 * possible that we end up with a value that is lower than the watermark
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	 * but won't increase since all blocks are in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (queue->fileio.active_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		data_available += queue->fileio.active_block->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	spin_lock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	list_for_each_entry(block, &queue->outgoing, head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		data_available += block->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	spin_unlock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	return data_available;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) EXPORT_SYMBOL_GPL(iio_dma_buffer_data_available);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  * iio_dma_buffer_set_bytes_per_datum() - DMA buffer set_bytes_per_datum callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  * @buffer: Buffer to set the bytes-per-datum for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)  * @bpd: The new bytes-per-datum value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)  * Should be used as the set_bytes_per_datum callback for iio_buffer_access_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)  * struct for DMA buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	buffer->bytes_per_datum = bpd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) EXPORT_SYMBOL_GPL(iio_dma_buffer_set_bytes_per_datum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581)  * iio_dma_buffer_set_length - DMA buffer set_length callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582)  * @buffer: Buffer to set the length for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)  * @length: The new buffer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * Should be used as the set_length callback for iio_buffer_access_ops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  * struct for DMA buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) int iio_dma_buffer_set_length(struct iio_buffer *buffer, unsigned int length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	/* Avoid an invalid state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (length < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		length = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	buffer->length = length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	buffer->watermark = length / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) EXPORT_SYMBOL_GPL(iio_dma_buffer_set_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)  * iio_dma_buffer_init() - Initialize DMA buffer queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)  * @queue: Buffer to initialize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)  * @dev: DMA device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)  * @ops: DMA buffer queue callback operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)  * The DMA device will be used by the queue to do DMA memory allocations. So it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)  * should refer to the device that will perform the DMA to ensure that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)  * allocations are done from a memory region that can be accessed by the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	struct device *dev, const struct iio_dma_buffer_ops *ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	iio_buffer_init(&queue->buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	queue->buffer.length = PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	queue->buffer.watermark = queue->buffer.length / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	queue->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	queue->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	INIT_LIST_HEAD(&queue->incoming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	INIT_LIST_HEAD(&queue->outgoing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	mutex_init(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	spin_lock_init(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) EXPORT_SYMBOL_GPL(iio_dma_buffer_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * iio_dma_buffer_exit() - Cleanup DMA buffer queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  * @queue: Buffer to cleanup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  * After this function has completed it is safe to free any resources that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * associated with the buffer and are accessed inside the callback operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	mutex_lock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	spin_lock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		if (!queue->fileio.blocks[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		queue->fileio.blocks[i]->state = IIO_BLOCK_STATE_DEAD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	INIT_LIST_HEAD(&queue->outgoing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	spin_unlock_irq(&queue->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	INIT_LIST_HEAD(&queue->incoming);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	for (i = 0; i < ARRAY_SIZE(queue->fileio.blocks); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		if (!queue->fileio.blocks[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		iio_buffer_block_put(queue->fileio.blocks[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		queue->fileio.blocks[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	queue->fileio.active_block = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	queue->ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	mutex_unlock(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) EXPORT_SYMBOL_GPL(iio_dma_buffer_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667)  * iio_dma_buffer_release() - Release final buffer resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668)  * @queue: Buffer to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)  * Frees resources that can't yet be freed in iio_dma_buffer_exit(). Should be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671)  * called in the buffers release callback implementation right before freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)  * the memory associated with the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	mutex_destroy(&queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) EXPORT_SYMBOL_GPL(iio_dma_buffer_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) MODULE_DESCRIPTION("DMA buffer for the IIO framework");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) MODULE_LICENSE("GPL v2");