buffer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* The industrial I/O core - generic buffer interfaces.
  2. *
  3. * Copyright (c) 2008 Jonathan Cameron
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #ifndef _IIO_BUFFER_GENERIC_H_
  10. #define _IIO_BUFFER_GENERIC_H_
  11. #include <linux/sysfs.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/kref.h>
  14. #ifdef CONFIG_IIO_BUFFER
  15. struct iio_buffer;
  16. void iio_buffer_set_attrs(struct iio_buffer *buffer,
  17. const struct attribute **attrs);
  18. /**
  19. * INDIO_BUFFER_FLAG_FIXED_WATERMARK - Watermark level of the buffer can not be
  20. * configured. It has a fixed value which will be buffer specific.
  21. */
  22. #define INDIO_BUFFER_FLAG_FIXED_WATERMARK BIT(0)
  23. /**
  24. * struct iio_buffer_access_funcs - access functions for buffers.
  25. * @store_to: actually store stuff to the buffer
  26. * @read_first_n: try to get a specified number of bytes (must exist)
  27. * @data_available: indicates how much data is available for reading from
  28. * the buffer.
  29. * @request_update: if a parameter change has been marked, update underlying
  30. * storage.
  31. * @set_bytes_per_datum:set number of bytes per datum
  32. * @set_length: set number of datums in buffer
  33. * @enable: called if the buffer is attached to a device and the
  34. * device starts sampling. Calls are balanced with
  35. * @disable.
  36. * @disable: called if the buffer is attached to a device and the
  37. * device stops sampling. Calles are balanced with @enable.
  38. * @release: called when the last reference to the buffer is dropped,
  39. * should free all resources allocated by the buffer.
  40. * @modes: Supported operating modes by this buffer type
  41. * @flags: A bitmask combination of INDIO_BUFFER_FLAG_*
  42. *
  43. * The purpose of this structure is to make the buffer element
  44. * modular as event for a given driver, different usecases may require
  45. * different buffer designs (space efficiency vs speed for example).
  46. *
  47. * It is worth noting that a given buffer implementation may only support a
  48. * small proportion of these functions. The core code 'should' cope fine with
  49. * any of them not existing.
  50. **/
  51. struct iio_buffer_access_funcs {
  52. int (*store_to)(struct iio_buffer *buffer, const void *data);
  53. int (*read_first_n)(struct iio_buffer *buffer,
  54. size_t n,
  55. char __user *buf);
  56. size_t (*data_available)(struct iio_buffer *buffer);
  57. int (*request_update)(struct iio_buffer *buffer);
  58. int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
  59. int (*set_length)(struct iio_buffer *buffer, int length);
  60. int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
  61. int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
  62. void (*release)(struct iio_buffer *buffer);
  63. unsigned int modes;
  64. unsigned int flags;
  65. };
  66. /**
  67. * struct iio_buffer - general buffer structure
  68. *
  69. * Note that the internals of this structure should only be of interest to
  70. * those writing new buffer implementations.
  71. */
  72. struct iio_buffer {
  73. /** @length: Number of datums in buffer. */
  74. int length;
  75. /** @bytes_per_datum: Size of individual datum including timestamp. */
  76. int bytes_per_datum;
  77. /**
  78. * @access: Buffer access functions associated with the
  79. * implementation.
  80. */
  81. const struct iio_buffer_access_funcs *access;
  82. /** @scan_mask: Bitmask used in masking scan mode elements. */
  83. long *scan_mask;
  84. /** @demux_list: List of operations required to demux the scan. */
  85. struct list_head demux_list;
  86. /** @pollq: Wait queue to allow for polling on the buffer. */
  87. wait_queue_head_t pollq;
  88. /** @watermark: Number of datums to wait for poll/read. */
  89. unsigned int watermark;
  90. /* private: */
  91. /*
  92. * @scan_el_attrs: Control of scan elements if that scan mode
  93. * control method is used.
  94. */
  95. struct attribute_group *scan_el_attrs;
  96. /* @scan_timestamp: Does the scan mode include a timestamp. */
  97. bool scan_timestamp;
  98. /* @scan_el_dev_attr_list: List of scan element related attributes. */
  99. struct list_head scan_el_dev_attr_list;
  100. /* @buffer_group: Attributes of the buffer group. */
  101. struct attribute_group buffer_group;
  102. /*
  103. * @scan_el_group: Attribute group for those attributes not
  104. * created from the iio_chan_info array.
  105. */
  106. struct attribute_group scan_el_group;
  107. /* @stufftoread: Flag to indicate new data. */
  108. bool stufftoread;
  109. /* @attrs: Standard attributes of the buffer. */
  110. const struct attribute **attrs;
  111. /* @demux_bounce: Buffer for doing gather from incoming scan. */
  112. void *demux_bounce;
  113. /* @buffer_list: Entry in the devices list of current buffers. */
  114. struct list_head buffer_list;
  115. /* @ref: Reference count of the buffer. */
  116. struct kref ref;
  117. };
  118. /**
  119. * iio_update_buffers() - add or remove buffer from active list
  120. * @indio_dev: device to add buffer to
  121. * @insert_buffer: buffer to insert
  122. * @remove_buffer: buffer_to_remove
  123. *
  124. * Note this will tear down the all buffering and build it up again
  125. */
  126. int iio_update_buffers(struct iio_dev *indio_dev,
  127. struct iio_buffer *insert_buffer,
  128. struct iio_buffer *remove_buffer);
  129. /**
  130. * iio_buffer_init() - Initialize the buffer structure
  131. * @buffer: buffer to be initialized
  132. **/
  133. void iio_buffer_init(struct iio_buffer *buffer);
  134. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
  135. /*
  136. * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
  137. * @indio_dev: iio_dev structure for device.
  138. * @data: sample data
  139. * @timestamp: timestamp for the sample data
  140. *
  141. * Pushes data to the IIO device's buffers. If timestamps are enabled for the
  142. * device the function will store the supplied timestamp as the last element in
  143. * the sample data buffer before pushing it to the device buffers. The sample
  144. * data buffer needs to be large enough to hold the additional timestamp
  145. * (usually the buffer should be indio->scan_bytes bytes large).
  146. *
  147. * Returns 0 on success, a negative error code otherwise.
  148. */
  149. static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
  150. void *data, int64_t timestamp)
  151. {
  152. if (indio_dev->scan_timestamp) {
  153. size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
  154. ((int64_t *)data)[ts_offset] = timestamp;
  155. }
  156. return iio_push_to_buffers(indio_dev, data);
  157. }
  158. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  159. const unsigned long *mask);
  160. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
  161. void iio_buffer_put(struct iio_buffer *buffer);
  162. /**
  163. * iio_device_attach_buffer - Attach a buffer to a IIO device
  164. * @indio_dev: The device the buffer should be attached to
  165. * @buffer: The buffer to attach to the device
  166. *
  167. * This function attaches a buffer to a IIO device. The buffer stays attached to
  168. * the device until the device is freed. The function should only be called at
  169. * most once per device.
  170. */
  171. static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
  172. struct iio_buffer *buffer)
  173. {
  174. indio_dev->buffer = iio_buffer_get(buffer);
  175. }
  176. #else /* CONFIG_IIO_BUFFER */
  177. static inline void iio_buffer_get(struct iio_buffer *buffer) {}
  178. static inline void iio_buffer_put(struct iio_buffer *buffer) {}
  179. #endif /* CONFIG_IIO_BUFFER */
  180. #endif /* _IIO_BUFFER_GENERIC_H_ */