buffer.h 8.3 KB

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