buffer.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. * @length: [DEVICE] number of datums in buffer
  67. * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
  68. * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
  69. * control method is used
  70. * @scan_mask: [INTERN] bitmask used in masking scan mode elements
  71. * @scan_timestamp: [INTERN] does the scan mode include a timestamp
  72. * @access: [DRIVER] buffer access functions associated with the
  73. * implementation.
  74. * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
  75. * @buffer_group: [INTERN] attributes of the buffer group
  76. * @scan_el_group: [DRIVER] attribute group for those attributes not
  77. * created from the iio_chan_info array.
  78. * @pollq: [INTERN] wait queue to allow for polling on the buffer.
  79. * @stufftoread: [INTERN] flag to indicate new data.
  80. * @attrs: [INTERN] standard attributes of the buffer
  81. * @demux_list: [INTERN] list of operations required to demux the scan.
  82. * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
  83. * @buffer_list: [INTERN] entry in the devices list of current buffers.
  84. * @ref: [INTERN] reference count of the buffer.
  85. * @watermark: [INTERN] number of datums to wait for poll/read.
  86. */
  87. struct iio_buffer {
  88. int length;
  89. int bytes_per_datum;
  90. struct attribute_group *scan_el_attrs;
  91. long *scan_mask;
  92. bool scan_timestamp;
  93. const struct iio_buffer_access_funcs *access;
  94. struct list_head scan_el_dev_attr_list;
  95. struct attribute_group buffer_group;
  96. struct attribute_group scan_el_group;
  97. wait_queue_head_t pollq;
  98. bool stufftoread;
  99. const struct attribute **attrs;
  100. struct list_head demux_list;
  101. void *demux_bounce;
  102. struct list_head buffer_list;
  103. struct kref ref;
  104. unsigned int watermark;
  105. };
  106. /**
  107. * iio_update_buffers() - add or remove buffer from active list
  108. * @indio_dev: device to add buffer to
  109. * @insert_buffer: buffer to insert
  110. * @remove_buffer: buffer_to_remove
  111. *
  112. * Note this will tear down the all buffering and build it up again
  113. */
  114. int iio_update_buffers(struct iio_dev *indio_dev,
  115. struct iio_buffer *insert_buffer,
  116. struct iio_buffer *remove_buffer);
  117. /**
  118. * iio_buffer_init() - Initialize the buffer structure
  119. * @buffer: buffer to be initialized
  120. **/
  121. void iio_buffer_init(struct iio_buffer *buffer);
  122. int iio_scan_mask_query(struct iio_dev *indio_dev,
  123. struct iio_buffer *buffer, int bit);
  124. /**
  125. * iio_push_to_buffers() - push to a registered buffer.
  126. * @indio_dev: iio_dev structure for device.
  127. * @data: Full scan.
  128. */
  129. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
  130. /*
  131. * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
  132. * @indio_dev: iio_dev structure for device.
  133. * @data: sample data
  134. * @timestamp: timestamp for the sample data
  135. *
  136. * Pushes data to the IIO device's buffers. If timestamps are enabled for the
  137. * device the function will store the supplied timestamp as the last element in
  138. * the sample data buffer before pushing it to the device buffers. The sample
  139. * data buffer needs to be large enough to hold the additional timestamp
  140. * (usually the buffer should be indio->scan_bytes bytes large).
  141. *
  142. * Returns 0 on success, a negative error code otherwise.
  143. */
  144. static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
  145. void *data, int64_t timestamp)
  146. {
  147. if (indio_dev->scan_timestamp) {
  148. size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
  149. ((int64_t *)data)[ts_offset] = timestamp;
  150. }
  151. return iio_push_to_buffers(indio_dev, data);
  152. }
  153. int iio_update_demux(struct iio_dev *indio_dev);
  154. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  155. const unsigned long *mask);
  156. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
  157. void iio_buffer_put(struct iio_buffer *buffer);
  158. /**
  159. * iio_device_attach_buffer - Attach a buffer to a IIO device
  160. * @indio_dev: The device the buffer should be attached to
  161. * @buffer: The buffer to attach to the device
  162. *
  163. * This function attaches a buffer to a IIO device. The buffer stays attached to
  164. * the device until the device is freed. The function should only be called at
  165. * most once per device.
  166. */
  167. static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
  168. struct iio_buffer *buffer)
  169. {
  170. indio_dev->buffer = iio_buffer_get(buffer);
  171. }
  172. #else /* CONFIG_IIO_BUFFER */
  173. static inline void iio_buffer_get(struct iio_buffer *buffer) {}
  174. static inline void iio_buffer_put(struct iio_buffer *buffer) {}
  175. #endif /* CONFIG_IIO_BUFFER */
  176. #endif /* _IIO_BUFFER_GENERIC_H_ */