buffer.h 7.4 KB

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