buffer_impl.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _IIO_BUFFER_GENERIC_IMPL_H_
  2. #define _IIO_BUFFER_GENERIC_IMPL_H_
  3. #include <linux/sysfs.h>
  4. #include <linux/kref.h>
  5. #ifdef CONFIG_IIO_BUFFER
  6. struct iio_dev;
  7. struct iio_buffer;
  8. /**
  9. * INDIO_BUFFER_FLAG_FIXED_WATERMARK - Watermark level of the buffer can not be
  10. * configured. It has a fixed value which will be buffer specific.
  11. */
  12. #define INDIO_BUFFER_FLAG_FIXED_WATERMARK BIT(0)
  13. /**
  14. * struct iio_buffer_access_funcs - access functions for buffers.
  15. * @store_to: actually store stuff to the buffer
  16. * @read_first_n: try to get a specified number of bytes (must exist)
  17. * @data_available: indicates how much data is available for reading from
  18. * the buffer.
  19. * @request_update: if a parameter change has been marked, update underlying
  20. * storage.
  21. * @set_bytes_per_datum:set number of bytes per datum
  22. * @set_length: set number of datums in buffer
  23. * @enable: called if the buffer is attached to a device and the
  24. * device starts sampling. Calls are balanced with
  25. * @disable.
  26. * @disable: called if the buffer is attached to a device and the
  27. * device stops sampling. Calles are balanced with @enable.
  28. * @release: called when the last reference to the buffer is dropped,
  29. * should free all resources allocated by the buffer.
  30. * @modes: Supported operating modes by this buffer type
  31. * @flags: A bitmask combination of INDIO_BUFFER_FLAG_*
  32. *
  33. * The purpose of this structure is to make the buffer element
  34. * modular as event for a given driver, different usecases may require
  35. * different buffer designs (space efficiency vs speed for example).
  36. *
  37. * It is worth noting that a given buffer implementation may only support a
  38. * small proportion of these functions. The core code 'should' cope fine with
  39. * any of them not existing.
  40. **/
  41. struct iio_buffer_access_funcs {
  42. int (*store_to)(struct iio_buffer *buffer, const void *data);
  43. int (*read_first_n)(struct iio_buffer *buffer,
  44. size_t n,
  45. char __user *buf);
  46. size_t (*data_available)(struct iio_buffer *buffer);
  47. int (*request_update)(struct iio_buffer *buffer);
  48. int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
  49. int (*set_length)(struct iio_buffer *buffer, int length);
  50. int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
  51. int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
  52. void (*release)(struct iio_buffer *buffer);
  53. unsigned int modes;
  54. unsigned int flags;
  55. };
  56. /**
  57. * struct iio_buffer - general buffer structure
  58. *
  59. * Note that the internals of this structure should only be of interest to
  60. * those writing new buffer implementations.
  61. */
  62. struct iio_buffer {
  63. /** @length: Number of datums in buffer. */
  64. int length;
  65. /** @bytes_per_datum: Size of individual datum including timestamp. */
  66. int bytes_per_datum;
  67. /**
  68. * @access: Buffer access functions associated with the
  69. * implementation.
  70. */
  71. const struct iio_buffer_access_funcs *access;
  72. /** @scan_mask: Bitmask used in masking scan mode elements. */
  73. long *scan_mask;
  74. /** @demux_list: List of operations required to demux the scan. */
  75. struct list_head demux_list;
  76. /** @pollq: Wait queue to allow for polling on the buffer. */
  77. wait_queue_head_t pollq;
  78. /** @watermark: Number of datums to wait for poll/read. */
  79. unsigned int watermark;
  80. /* private: */
  81. /*
  82. * @scan_el_attrs: Control of scan elements if that scan mode
  83. * control method is used.
  84. */
  85. struct attribute_group *scan_el_attrs;
  86. /* @scan_timestamp: Does the scan mode include a timestamp. */
  87. bool scan_timestamp;
  88. /* @scan_el_dev_attr_list: List of scan element related attributes. */
  89. struct list_head scan_el_dev_attr_list;
  90. /* @buffer_group: Attributes of the buffer group. */
  91. struct attribute_group buffer_group;
  92. /*
  93. * @scan_el_group: Attribute group for those attributes not
  94. * created from the iio_chan_info array.
  95. */
  96. struct attribute_group scan_el_group;
  97. /* @stufftoread: Flag to indicate new data. */
  98. bool stufftoread;
  99. /* @attrs: Standard attributes of the buffer. */
  100. const struct attribute **attrs;
  101. /* @demux_bounce: Buffer for doing gather from incoming scan. */
  102. void *demux_bounce;
  103. /* @buffer_list: Entry in the devices list of current buffers. */
  104. struct list_head buffer_list;
  105. /* @ref: Reference count of the buffer. */
  106. struct kref ref;
  107. };
  108. /**
  109. * iio_update_buffers() - add or remove buffer from active list
  110. * @indio_dev: device to add buffer to
  111. * @insert_buffer: buffer to insert
  112. * @remove_buffer: buffer_to_remove
  113. *
  114. * Note this will tear down the all buffering and build it up again
  115. */
  116. int iio_update_buffers(struct iio_dev *indio_dev,
  117. struct iio_buffer *insert_buffer,
  118. struct iio_buffer *remove_buffer);
  119. /**
  120. * iio_buffer_init() - Initialize the buffer structure
  121. * @buffer: buffer to be initialized
  122. **/
  123. void iio_buffer_init(struct iio_buffer *buffer);
  124. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
  125. void iio_buffer_put(struct iio_buffer *buffer);
  126. #else /* CONFIG_IIO_BUFFER */
  127. static inline void iio_buffer_get(struct iio_buffer *buffer) {}
  128. static inline void iio_buffer_put(struct iio_buffer *buffer) {}
  129. #endif /* CONFIG_IIO_BUFFER */
  130. #endif /* _IIO_BUFFER_GENERIC_IMPL_H_ */