buffer-dma.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2013-2015 Analog Devices Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * Licensed under the GPL-2.
  6. */
  7. #ifndef __INDUSTRIALIO_DMA_BUFFER_H__
  8. #define __INDUSTRIALIO_DMA_BUFFER_H__
  9. #include <linux/list.h>
  10. #include <linux/kref.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/mutex.h>
  13. #include <linux/iio/buffer.h>
  14. struct iio_dma_buffer_queue;
  15. struct iio_dma_buffer_ops;
  16. struct device;
  17. struct iio_buffer_block {
  18. u32 size;
  19. u32 bytes_used;
  20. };
  21. /**
  22. * enum iio_block_state - State of a struct iio_dma_buffer_block
  23. * @IIO_BLOCK_STATE_DEQUEUED: Block is not queued
  24. * @IIO_BLOCK_STATE_QUEUED: Block is on the incoming queue
  25. * @IIO_BLOCK_STATE_ACTIVE: Block is currently being processed by the DMA
  26. * @IIO_BLOCK_STATE_DONE: Block is on the outgoing queue
  27. * @IIO_BLOCK_STATE_DEAD: Block has been marked as to be freed
  28. */
  29. enum iio_block_state {
  30. IIO_BLOCK_STATE_DEQUEUED,
  31. IIO_BLOCK_STATE_QUEUED,
  32. IIO_BLOCK_STATE_ACTIVE,
  33. IIO_BLOCK_STATE_DONE,
  34. IIO_BLOCK_STATE_DEAD,
  35. };
  36. /**
  37. * struct iio_dma_buffer_block - IIO buffer block
  38. * @head: List head
  39. * @size: Total size of the block in bytes
  40. * @bytes_used: Number of bytes that contain valid data
  41. * @vaddr: Virutal address of the blocks memory
  42. * @phys_addr: Physical address of the blocks memory
  43. * @queue: Parent DMA buffer queue
  44. * @kref: kref used to manage the lifetime of block
  45. * @state: Current state of the block
  46. */
  47. struct iio_dma_buffer_block {
  48. /* May only be accessed by the owner of the block */
  49. struct list_head head;
  50. size_t bytes_used;
  51. /*
  52. * Set during allocation, constant thereafter. May be accessed read-only
  53. * by anybody holding a reference to the block.
  54. */
  55. void *vaddr;
  56. dma_addr_t phys_addr;
  57. size_t size;
  58. struct iio_dma_buffer_queue *queue;
  59. /* Must not be accessed outside the core. */
  60. struct kref kref;
  61. /*
  62. * Must not be accessed outside the core. Access needs to hold
  63. * queue->list_lock if the block is not owned by the core.
  64. */
  65. enum iio_block_state state;
  66. };
  67. /**
  68. * struct iio_dma_buffer_queue_fileio - FileIO state for the DMA buffer
  69. * @blocks: Buffer blocks used for fileio
  70. * @active_block: Block being used in read()
  71. * @pos: Read offset in the active block
  72. * @block_size: Size of each block
  73. */
  74. struct iio_dma_buffer_queue_fileio {
  75. struct iio_dma_buffer_block *blocks[2];
  76. struct iio_dma_buffer_block *active_block;
  77. size_t pos;
  78. size_t block_size;
  79. };
  80. /**
  81. * struct iio_dma_buffer_queue - DMA buffer base structure
  82. * @buffer: IIO buffer base structure
  83. * @dev: Parent device
  84. * @ops: DMA buffer callbacks
  85. * @lock: Protects the incoming list, active and the fields in the fileio
  86. * substruct
  87. * @list_lock: Protects lists that contain blocks which can be modified in
  88. * atomic context as well as blocks on those lists. This is the outgoing queue
  89. * list and typically also a list of active blocks in the part that handles
  90. * the DMA controller
  91. * @incoming: List of buffers on the incoming queue
  92. * @outgoing: List of buffers on the outgoing queue
  93. * @active: Whether the buffer is currently active
  94. * @fileio: FileIO state
  95. */
  96. struct iio_dma_buffer_queue {
  97. struct iio_buffer buffer;
  98. struct device *dev;
  99. const struct iio_dma_buffer_ops *ops;
  100. struct mutex lock;
  101. spinlock_t list_lock;
  102. struct list_head incoming;
  103. struct list_head outgoing;
  104. bool active;
  105. struct iio_dma_buffer_queue_fileio fileio;
  106. };
  107. /**
  108. * struct iio_dma_buffer_ops - DMA buffer callback operations
  109. * @submit: Called when a block is submitted to the DMA controller
  110. * @abort: Should abort all pending transfers
  111. */
  112. struct iio_dma_buffer_ops {
  113. int (*submit)(struct iio_dma_buffer_queue *queue,
  114. struct iio_dma_buffer_block *block);
  115. void (*abort)(struct iio_dma_buffer_queue *queue);
  116. };
  117. void iio_dma_buffer_block_done(struct iio_dma_buffer_block *block);
  118. void iio_dma_buffer_block_list_abort(struct iio_dma_buffer_queue *queue,
  119. struct list_head *list);
  120. int iio_dma_buffer_enable(struct iio_buffer *buffer,
  121. struct iio_dev *indio_dev);
  122. int iio_dma_buffer_disable(struct iio_buffer *buffer,
  123. struct iio_dev *indio_dev);
  124. int iio_dma_buffer_read(struct iio_buffer *buffer, size_t n,
  125. char __user *user_buffer);
  126. size_t iio_dma_buffer_data_available(struct iio_buffer *buffer);
  127. int iio_dma_buffer_set_bytes_per_datum(struct iio_buffer *buffer, size_t bpd);
  128. int iio_dma_buffer_set_length(struct iio_buffer *buffer, int length);
  129. int iio_dma_buffer_request_update(struct iio_buffer *buffer);
  130. int iio_dma_buffer_init(struct iio_dma_buffer_queue *queue,
  131. struct device *dma_dev, const struct iio_dma_buffer_ops *ops);
  132. void iio_dma_buffer_exit(struct iio_dma_buffer_queue *queue);
  133. void iio_dma_buffer_release(struct iio_dma_buffer_queue *queue);
  134. #endif