buffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. **/
  63. struct iio_buffer {
  64. int length;
  65. int bytes_per_datum;
  66. struct attribute_group *scan_el_attrs;
  67. long *scan_mask;
  68. bool scan_timestamp;
  69. const struct iio_buffer_access_funcs *access;
  70. struct list_head scan_el_dev_attr_list;
  71. struct attribute_group scan_el_group;
  72. wait_queue_head_t pollq;
  73. bool stufftoread;
  74. const struct attribute_group *attrs;
  75. struct list_head demux_list;
  76. unsigned char *demux_bounce;
  77. };
  78. /**
  79. * iio_buffer_init() - Initialize the buffer structure
  80. * @buffer: buffer to be initialized
  81. **/
  82. void iio_buffer_init(struct iio_buffer *buffer);
  83. /**
  84. * __iio_update_buffer() - update common elements of buffers
  85. * @buffer: buffer that is the event source
  86. * @bytes_per_datum: size of individual datum including timestamp
  87. * @length: number of datums in buffer
  88. **/
  89. static inline void __iio_update_buffer(struct iio_buffer *buffer,
  90. int bytes_per_datum, int length)
  91. {
  92. buffer->bytes_per_datum = bytes_per_datum;
  93. buffer->length = length;
  94. }
  95. int iio_scan_mask_query(struct iio_dev *indio_dev,
  96. struct iio_buffer *buffer, int bit);
  97. /**
  98. * iio_scan_mask_set() - set particular bit in the scan mask
  99. * @indio_dev IIO device structure
  100. * @buffer: the buffer whose scan mask we are interested in
  101. * @bit: the bit to be set.
  102. **/
  103. int iio_scan_mask_set(struct iio_dev *indio_dev,
  104. struct iio_buffer *buffer, int bit);
  105. /**
  106. * iio_push_to_buffer() - push to a registered buffer.
  107. * @buffer: IIO buffer structure for device
  108. * @data: the data to push to the buffer
  109. */
  110. int iio_push_to_buffer(struct iio_buffer *buffer, unsigned char *data);
  111. int iio_update_demux(struct iio_dev *indio_dev);
  112. /**
  113. * iio_buffer_register() - register the buffer with IIO core
  114. * @indio_dev: device with the buffer to be registered
  115. * @channels: the channel descriptions used to construct buffer
  116. * @num_channels: the number of channels
  117. **/
  118. int iio_buffer_register(struct iio_dev *indio_dev,
  119. const struct iio_chan_spec *channels,
  120. int num_channels);
  121. /**
  122. * iio_buffer_unregister() - unregister the buffer from IIO core
  123. * @indio_dev: the device with the buffer to be unregistered
  124. **/
  125. void iio_buffer_unregister(struct iio_dev *indio_dev);
  126. /**
  127. * iio_buffer_read_length() - attr func to get number of datums in the buffer
  128. **/
  129. ssize_t iio_buffer_read_length(struct device *dev,
  130. struct device_attribute *attr,
  131. char *buf);
  132. /**
  133. * iio_buffer_write_length() - attr func to set number of datums in the buffer
  134. **/
  135. ssize_t iio_buffer_write_length(struct device *dev,
  136. struct device_attribute *attr,
  137. const char *buf,
  138. size_t len);
  139. /**
  140. * iio_buffer_store_enable() - attr to turn the buffer on
  141. **/
  142. ssize_t iio_buffer_store_enable(struct device *dev,
  143. struct device_attribute *attr,
  144. const char *buf,
  145. size_t len);
  146. /**
  147. * iio_buffer_show_enable() - attr to see if the buffer is on
  148. **/
  149. ssize_t iio_buffer_show_enable(struct device *dev,
  150. struct device_attribute *attr,
  151. char *buf);
  152. #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
  153. iio_buffer_read_length, \
  154. iio_buffer_write_length)
  155. #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
  156. iio_buffer_show_enable, \
  157. iio_buffer_store_enable)
  158. int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
  159. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  160. const unsigned long *mask);
  161. #else /* CONFIG_IIO_BUFFER */
  162. static inline int iio_buffer_register(struct iio_dev *indio_dev,
  163. struct iio_chan_spec *channels,
  164. int num_channels)
  165. {
  166. return 0;
  167. }
  168. static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
  169. {}
  170. #endif /* CONFIG_IIO_BUFFER */
  171. #endif /* _IIO_BUFFER_GENERIC_H_ */