buffer.h 7.2 KB

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