buffer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, u8 *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. unsigned char *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. /**
  97. * __iio_update_buffer() - update common elements of buffers
  98. * @buffer: buffer that is the event source
  99. * @bytes_per_datum: size of individual datum including timestamp
  100. * @length: number of datums in buffer
  101. **/
  102. static inline void __iio_update_buffer(struct iio_buffer *buffer,
  103. int bytes_per_datum, int length)
  104. {
  105. buffer->bytes_per_datum = bytes_per_datum;
  106. buffer->length = length;
  107. }
  108. int iio_scan_mask_query(struct iio_dev *indio_dev,
  109. struct iio_buffer *buffer, int bit);
  110. /**
  111. * iio_scan_mask_set() - set particular bit in the scan mask
  112. * @indio_dev IIO device structure
  113. * @buffer: the buffer whose scan mask we are interested in
  114. * @bit: the bit to be set.
  115. **/
  116. int iio_scan_mask_set(struct iio_dev *indio_dev,
  117. struct iio_buffer *buffer, int bit);
  118. /**
  119. * iio_push_to_buffers() - push to a registered buffer.
  120. * @indio_dev: iio_dev structure for device.
  121. * @data: Full scan.
  122. */
  123. int iio_push_to_buffers(struct iio_dev *indio_dev, unsigned char *data);
  124. int iio_update_demux(struct iio_dev *indio_dev);
  125. /**
  126. * iio_buffer_register() - register the buffer with IIO core
  127. * @indio_dev: device with the buffer to be registered
  128. * @channels: the channel descriptions used to construct buffer
  129. * @num_channels: the number of channels
  130. **/
  131. int iio_buffer_register(struct iio_dev *indio_dev,
  132. const struct iio_chan_spec *channels,
  133. int num_channels);
  134. /**
  135. * iio_buffer_unregister() - unregister the buffer from IIO core
  136. * @indio_dev: the device with the buffer to be unregistered
  137. **/
  138. void iio_buffer_unregister(struct iio_dev *indio_dev);
  139. /**
  140. * iio_buffer_read_length() - attr func to get number of datums in the buffer
  141. **/
  142. ssize_t iio_buffer_read_length(struct device *dev,
  143. struct device_attribute *attr,
  144. char *buf);
  145. /**
  146. * iio_buffer_write_length() - attr func to set number of datums in the buffer
  147. **/
  148. ssize_t iio_buffer_write_length(struct device *dev,
  149. struct device_attribute *attr,
  150. const char *buf,
  151. size_t len);
  152. /**
  153. * iio_buffer_store_enable() - attr to turn the buffer on
  154. **/
  155. ssize_t iio_buffer_store_enable(struct device *dev,
  156. struct device_attribute *attr,
  157. const char *buf,
  158. size_t len);
  159. /**
  160. * iio_buffer_show_enable() - attr to see if the buffer is on
  161. **/
  162. ssize_t iio_buffer_show_enable(struct device *dev,
  163. struct device_attribute *attr,
  164. char *buf);
  165. #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
  166. iio_buffer_read_length, \
  167. iio_buffer_write_length)
  168. #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
  169. iio_buffer_show_enable, \
  170. iio_buffer_store_enable)
  171. int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
  172. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  173. const unsigned long *mask);
  174. #else /* CONFIG_IIO_BUFFER */
  175. static inline int iio_buffer_register(struct iio_dev *indio_dev,
  176. struct iio_chan_spec *channels,
  177. int num_channels)
  178. {
  179. return 0;
  180. }
  181. static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
  182. {}
  183. #endif /* CONFIG_IIO_BUFFER */
  184. #endif /* _IIO_BUFFER_GENERIC_H_ */