buffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. 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, unsigned char *data);
  112. int iio_update_demux(struct iio_dev *indio_dev);
  113. /**
  114. * iio_buffer_register() - register the buffer with IIO core
  115. * @indio_dev: device with the buffer to be registered
  116. * @channels: the channel descriptions used to construct buffer
  117. * @num_channels: the number of channels
  118. **/
  119. int iio_buffer_register(struct iio_dev *indio_dev,
  120. const struct iio_chan_spec *channels,
  121. int num_channels);
  122. /**
  123. * iio_buffer_unregister() - unregister the buffer from IIO core
  124. * @indio_dev: the device with the buffer to be unregistered
  125. **/
  126. void iio_buffer_unregister(struct iio_dev *indio_dev);
  127. /**
  128. * iio_buffer_read_length() - attr func to get number of datums in the buffer
  129. **/
  130. ssize_t iio_buffer_read_length(struct device *dev,
  131. struct device_attribute *attr,
  132. char *buf);
  133. /**
  134. * iio_buffer_write_length() - attr func to set number of datums in the buffer
  135. **/
  136. ssize_t iio_buffer_write_length(struct device *dev,
  137. struct device_attribute *attr,
  138. const char *buf,
  139. size_t len);
  140. /**
  141. * iio_buffer_store_enable() - attr to turn the buffer on
  142. **/
  143. ssize_t iio_buffer_store_enable(struct device *dev,
  144. struct device_attribute *attr,
  145. const char *buf,
  146. size_t len);
  147. /**
  148. * iio_buffer_show_enable() - attr to see if the buffer is on
  149. **/
  150. ssize_t iio_buffer_show_enable(struct device *dev,
  151. struct device_attribute *attr,
  152. char *buf);
  153. #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
  154. iio_buffer_read_length, \
  155. iio_buffer_write_length)
  156. #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
  157. iio_buffer_show_enable, \
  158. iio_buffer_store_enable)
  159. int iio_sw_buffer_preenable(struct iio_dev *indio_dev);
  160. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  161. const unsigned long *mask);
  162. #else /* CONFIG_IIO_BUFFER */
  163. static inline int iio_buffer_register(struct iio_dev *indio_dev,
  164. const struct iio_chan_spec *channels,
  165. int num_channels)
  166. {
  167. return 0;
  168. }
  169. static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
  170. {}
  171. #endif /* CONFIG_IIO_BUFFER */
  172. #endif /* _IIO_BUFFER_GENERIC_H_ */