buffer.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. * struct iio_buffer_access_funcs - access functions for buffers.
  18. * @store_to: actually store stuff to the buffer
  19. * @read_first_n: try to get a specified number of bytes (must exist)
  20. * @data_available: indicates whether data for reading from the buffer is
  21. * available.
  22. * @request_update: if a parameter change has been marked, update underlying
  23. * storage.
  24. * @get_bytes_per_datum:get current bytes per datum
  25. * @set_bytes_per_datum:set number of bytes per datum
  26. * @get_length: get number of datums in buffer
  27. * @set_length: set number of datums in buffer
  28. * @release: called when the last reference to the buffer is dropped,
  29. * should free all resources allocated by the buffer.
  30. *
  31. * The purpose of this structure is to make the buffer element
  32. * modular as event for a given driver, different usecases may require
  33. * different buffer designs (space efficiency vs speed for example).
  34. *
  35. * It is worth noting that a given buffer implementation may only support a
  36. * small proportion of these functions. The core code 'should' cope fine with
  37. * any of them not existing.
  38. **/
  39. struct iio_buffer_access_funcs {
  40. int (*store_to)(struct iio_buffer *buffer, const void *data);
  41. int (*read_first_n)(struct iio_buffer *buffer,
  42. size_t n,
  43. char __user *buf);
  44. bool (*data_available)(struct iio_buffer *buffer);
  45. int (*request_update)(struct iio_buffer *buffer);
  46. int (*get_bytes_per_datum)(struct iio_buffer *buffer);
  47. int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
  48. int (*get_length)(struct iio_buffer *buffer);
  49. int (*set_length)(struct iio_buffer *buffer, int length);
  50. void (*release)(struct iio_buffer *buffer);
  51. };
  52. /**
  53. * struct iio_buffer - general buffer structure
  54. * @length: [DEVICE] number of datums in buffer
  55. * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
  56. * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
  57. * control method is used
  58. * @scan_mask: [INTERN] bitmask used in masking scan mode elements
  59. * @scan_timestamp: [INTERN] does the scan mode include a timestamp
  60. * @access: [DRIVER] buffer access functions associated with the
  61. * implementation.
  62. * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
  63. * @scan_el_group: [DRIVER] attribute group for those attributes not
  64. * created from the iio_chan_info array.
  65. * @pollq: [INTERN] wait queue to allow for polling on the buffer.
  66. * @stufftoread: [INTERN] flag to indicate new data.
  67. * @demux_list: [INTERN] list of operations required to demux the scan.
  68. * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
  69. * @buffer_list: [INTERN] entry in the devices list of current buffers.
  70. * @ref: [INTERN] reference count of the buffer.
  71. */
  72. struct iio_buffer {
  73. int length;
  74. int bytes_per_datum;
  75. struct attribute_group *scan_el_attrs;
  76. long *scan_mask;
  77. bool scan_timestamp;
  78. const struct iio_buffer_access_funcs *access;
  79. struct list_head scan_el_dev_attr_list;
  80. struct attribute_group scan_el_group;
  81. wait_queue_head_t pollq;
  82. bool stufftoread;
  83. const struct attribute_group *attrs;
  84. struct list_head demux_list;
  85. void *demux_bounce;
  86. struct list_head buffer_list;
  87. struct kref ref;
  88. };
  89. /**
  90. * iio_update_buffers() - add or remove buffer from active list
  91. * @indio_dev: device to add buffer to
  92. * @insert_buffer: buffer to insert
  93. * @remove_buffer: buffer_to_remove
  94. *
  95. * Note this will tear down the all buffering and build it up again
  96. */
  97. int iio_update_buffers(struct iio_dev *indio_dev,
  98. struct iio_buffer *insert_buffer,
  99. struct iio_buffer *remove_buffer);
  100. /**
  101. * iio_buffer_init() - Initialize the buffer structure
  102. * @buffer: buffer to be initialized
  103. **/
  104. void iio_buffer_init(struct iio_buffer *buffer);
  105. int iio_scan_mask_query(struct iio_dev *indio_dev,
  106. struct iio_buffer *buffer, int bit);
  107. /**
  108. * iio_scan_mask_set() - set particular bit in the scan mask
  109. * @indio_dev IIO device structure
  110. * @buffer: the buffer whose scan mask we are interested in
  111. * @bit: the bit to be set.
  112. **/
  113. int iio_scan_mask_set(struct iio_dev *indio_dev,
  114. struct iio_buffer *buffer, int bit);
  115. /**
  116. * iio_push_to_buffers() - push to a registered buffer.
  117. * @indio_dev: iio_dev structure for device.
  118. * @data: Full scan.
  119. */
  120. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
  121. /*
  122. * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
  123. * @indio_dev: iio_dev structure for device.
  124. * @data: sample data
  125. * @timestamp: timestamp for the sample data
  126. *
  127. * Pushes data to the IIO device's buffers. If timestamps are enabled for the
  128. * device the function will store the supplied timestamp as the last element in
  129. * the sample data buffer before pushing it to the device buffers. The sample
  130. * data buffer needs to be large enough to hold the additional timestamp
  131. * (usually the buffer should be indio->scan_bytes bytes large).
  132. *
  133. * Returns 0 on success, a negative error code otherwise.
  134. */
  135. static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
  136. void *data, int64_t timestamp)
  137. {
  138. if (indio_dev->scan_timestamp) {
  139. size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
  140. ((int64_t *)data)[ts_offset] = timestamp;
  141. }
  142. return iio_push_to_buffers(indio_dev, data);
  143. }
  144. int iio_update_demux(struct iio_dev *indio_dev);
  145. /**
  146. * iio_buffer_register() - register the buffer with IIO core
  147. * @indio_dev: device with the buffer to be registered
  148. * @channels: the channel descriptions used to construct buffer
  149. * @num_channels: the number of channels
  150. **/
  151. int iio_buffer_register(struct iio_dev *indio_dev,
  152. const struct iio_chan_spec *channels,
  153. int num_channels);
  154. /**
  155. * iio_buffer_unregister() - unregister the buffer from IIO core
  156. * @indio_dev: the device with the buffer to be unregistered
  157. **/
  158. void iio_buffer_unregister(struct iio_dev *indio_dev);
  159. /**
  160. * iio_buffer_read_length() - attr func to get number of datums in the buffer
  161. **/
  162. ssize_t iio_buffer_read_length(struct device *dev,
  163. struct device_attribute *attr,
  164. char *buf);
  165. /**
  166. * iio_buffer_write_length() - attr func to set number of datums in the buffer
  167. **/
  168. ssize_t iio_buffer_write_length(struct device *dev,
  169. struct device_attribute *attr,
  170. const char *buf,
  171. size_t len);
  172. /**
  173. * iio_buffer_store_enable() - attr to turn the buffer on
  174. **/
  175. ssize_t iio_buffer_store_enable(struct device *dev,
  176. struct device_attribute *attr,
  177. const char *buf,
  178. size_t len);
  179. /**
  180. * iio_buffer_show_enable() - attr to see if the buffer is on
  181. **/
  182. ssize_t iio_buffer_show_enable(struct device *dev,
  183. struct device_attribute *attr,
  184. char *buf);
  185. #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
  186. iio_buffer_read_length, \
  187. iio_buffer_write_length)
  188. #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
  189. iio_buffer_show_enable, \
  190. iio_buffer_store_enable)
  191. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  192. const unsigned long *mask);
  193. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
  194. void iio_buffer_put(struct iio_buffer *buffer);
  195. /**
  196. * iio_device_attach_buffer - Attach a buffer to a IIO device
  197. * @indio_dev: The device the buffer should be attached to
  198. * @buffer: The buffer to attach to the device
  199. *
  200. * This function attaches a buffer to a IIO device. The buffer stays attached to
  201. * the device until the device is freed. The function should only be called at
  202. * most once per device.
  203. */
  204. static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
  205. struct iio_buffer *buffer)
  206. {
  207. indio_dev->buffer = iio_buffer_get(buffer);
  208. }
  209. #else /* CONFIG_IIO_BUFFER */
  210. static inline int iio_buffer_register(struct iio_dev *indio_dev,
  211. const struct iio_chan_spec *channels,
  212. int num_channels)
  213. {
  214. return 0;
  215. }
  216. static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
  217. {}
  218. static inline void iio_buffer_get(struct iio_buffer *buffer) {}
  219. static inline void iio_buffer_put(struct iio_buffer *buffer) {}
  220. #endif /* CONFIG_IIO_BUFFER */
  221. #endif /* _IIO_BUFFER_GENERIC_H_ */