buffer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. * @data_available: indicates how much data is available for reading from
  21. * the buffer.
  22. * @request_update: if a parameter change has been marked, update underlying
  23. * storage.
  24. * @set_bytes_per_datum:set number of bytes per datum
  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. * @modes: Supported operating modes by this buffer type
  29. *
  30. * The purpose of this structure is to make the buffer element
  31. * modular as event for a given driver, different usecases may require
  32. * different buffer designs (space efficiency vs speed for example).
  33. *
  34. * It is worth noting that a given buffer implementation may only support a
  35. * small proportion of these functions. The core code 'should' cope fine with
  36. * any of them not existing.
  37. **/
  38. struct iio_buffer_access_funcs {
  39. int (*store_to)(struct iio_buffer *buffer, const void *data);
  40. int (*read_first_n)(struct iio_buffer *buffer,
  41. size_t n,
  42. char __user *buf);
  43. size_t (*data_available)(struct iio_buffer *buffer);
  44. int (*request_update)(struct iio_buffer *buffer);
  45. int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
  46. int (*set_length)(struct iio_buffer *buffer, unsigned int length);
  47. void (*release)(struct iio_buffer *buffer);
  48. unsigned int modes;
  49. };
  50. /**
  51. * struct iio_buffer - general buffer structure
  52. * @length: [DEVICE] number of datums in buffer
  53. * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
  54. * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
  55. * control method is used
  56. * @scan_mask: [INTERN] bitmask used in masking scan mode elements
  57. * @scan_timestamp: [INTERN] does the scan mode include a timestamp
  58. * @access: [DRIVER] buffer access functions associated with the
  59. * implementation.
  60. * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
  61. * @scan_el_group: [DRIVER] attribute group for those attributes not
  62. * created from the iio_chan_info array.
  63. * @pollq: [INTERN] wait queue to allow for polling on the buffer.
  64. * @stufftoread: [INTERN] flag to indicate new data.
  65. * @demux_list: [INTERN] list of operations required to demux the scan.
  66. * @demux_bounce: [INTERN] buffer for doing gather from incoming scan.
  67. * @buffer_list: [INTERN] entry in the devices list of current buffers.
  68. * @ref: [INTERN] reference count of the buffer.
  69. * @watermark: [INTERN] number of datums to wait for poll/read.
  70. */
  71. struct iio_buffer {
  72. unsigned int length;
  73. size_t bytes_per_datum;
  74. struct attribute_group *scan_el_attrs;
  75. long *scan_mask;
  76. bool scan_timestamp;
  77. const struct iio_buffer_access_funcs *access;
  78. struct list_head scan_el_dev_attr_list;
  79. struct attribute_group buffer_group;
  80. struct attribute_group scan_el_group;
  81. wait_queue_head_t pollq;
  82. bool stufftoread;
  83. const struct attribute **attrs;
  84. struct list_head demux_list;
  85. void *demux_bounce;
  86. struct list_head buffer_list;
  87. struct kref ref;
  88. unsigned int watermark;
  89. };
  90. /**
  91. * iio_update_buffers() - add or remove buffer from active list
  92. * @indio_dev: device to add buffer to
  93. * @insert_buffer: buffer to insert
  94. * @remove_buffer: buffer_to_remove
  95. *
  96. * Note this will tear down the all buffering and build it up again
  97. */
  98. int iio_update_buffers(struct iio_dev *indio_dev,
  99. struct iio_buffer *insert_buffer,
  100. struct iio_buffer *remove_buffer);
  101. /**
  102. * iio_buffer_init() - Initialize the buffer structure
  103. * @buffer: buffer to be initialized
  104. **/
  105. void iio_buffer_init(struct iio_buffer *buffer);
  106. int iio_scan_mask_query(struct iio_dev *indio_dev,
  107. struct iio_buffer *buffer, int bit);
  108. /**
  109. * iio_push_to_buffers() - push to a registered buffer.
  110. * @indio_dev: iio_dev structure for device.
  111. * @data: Full scan.
  112. */
  113. int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
  114. /*
  115. * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
  116. * @indio_dev: iio_dev structure for device.
  117. * @data: sample data
  118. * @timestamp: timestamp for the sample data
  119. *
  120. * Pushes data to the IIO device's buffers. If timestamps are enabled for the
  121. * device the function will store the supplied timestamp as the last element in
  122. * the sample data buffer before pushing it to the device buffers. The sample
  123. * data buffer needs to be large enough to hold the additional timestamp
  124. * (usually the buffer should be indio->scan_bytes bytes large).
  125. *
  126. * Returns 0 on success, a negative error code otherwise.
  127. */
  128. static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
  129. void *data, int64_t timestamp)
  130. {
  131. if (indio_dev->scan_timestamp) {
  132. size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
  133. ((int64_t *)data)[ts_offset] = timestamp;
  134. }
  135. return iio_push_to_buffers(indio_dev, data);
  136. }
  137. int iio_update_demux(struct iio_dev *indio_dev);
  138. bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
  139. const unsigned long *mask);
  140. struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
  141. void iio_buffer_put(struct iio_buffer *buffer);
  142. /**
  143. * iio_device_attach_buffer - Attach a buffer to a IIO device
  144. * @indio_dev: The device the buffer should be attached to
  145. * @buffer: The buffer to attach to the device
  146. *
  147. * This function attaches a buffer to a IIO device. The buffer stays attached to
  148. * the device until the device is freed. The function should only be called at
  149. * most once per device.
  150. */
  151. static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
  152. struct iio_buffer *buffer)
  153. {
  154. indio_dev->buffer = iio_buffer_get(buffer);
  155. }
  156. #else /* CONFIG_IIO_BUFFER */
  157. static inline void iio_buffer_get(struct iio_buffer *buffer) {}
  158. static inline void iio_buffer_put(struct iio_buffer *buffer) {}
  159. #endif /* CONFIG_IIO_BUFFER */
  160. #endif /* _IIO_BUFFER_GENERIC_H_ */