/kernel/2.6.32_froyo_photon_nightly/drivers/staging/iio/ring_sw.h

http://photon-android.googlecode.com/ · C++ Header · 189 lines · 57 code · 31 blank · 101 comment · 1 complexity · 5bca2dc32152c62f9f2fdc39638de825 MD5 · raw file

  1. /* The industrial I/O simple minimally locked ring buffer.
  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. * This code is deliberately kept separate from the main industrialio I/O core
  10. * as it is intended that in the future a number of different software ring
  11. * buffer implementations will exist with different characteristics to suit
  12. * different applications.
  13. *
  14. * This particular one was designed for a data capture application where it was
  15. * particularly important that no userspace reads would interrupt the capture
  16. * process. To this end the ring is not locked during a read.
  17. *
  18. * Comments on this buffer design welcomed. It's far from efficient and some of
  19. * my understanding of the effects of scheduling on this are somewhat limited.
  20. * Frankly, to my mind, this is the current weak point in the industrial I/O
  21. * patch set.
  22. */
  23. #ifndef _IIO_RING_SW_H_
  24. #define _IIO_RING_SW_H_
  25. /* NEEDS COMMENTS */
  26. /* The intention is that this should be a separate module from the iio core.
  27. * This is a bit like supporting algorithms dependent on what the device
  28. * driver requests - some may support multiple options */
  29. #include <linux/autoconf.h>
  30. #include "iio.h"
  31. #include "ring_generic.h"
  32. #if defined CONFIG_IIO_SW_RING || defined CONFIG_IIO_SW_RING_MODULE
  33. /**
  34. * iio_create_sw_rb() software ring buffer allocation
  35. * @r: pointer to ring buffer pointer
  36. **/
  37. int iio_create_sw_rb(struct iio_ring_buffer **r);
  38. /**
  39. * iio_init_sw_rb() initialize the software ring buffer
  40. * @r: pointer to a software ring buffer created by an
  41. * iio_create_sw_rb call.
  42. **/
  43. int iio_init_sw_rb(struct iio_ring_buffer *r, struct iio_dev *indio_dev);
  44. /**
  45. * iio_exit_sw_rb() reverse what was done in iio_init_sw_rb
  46. **/
  47. void iio_exit_sw_rb(struct iio_ring_buffer *r);
  48. /**
  49. * iio_free_sw_rb() free memory occupied by the core ring buffer struct
  50. **/
  51. void iio_free_sw_rb(struct iio_ring_buffer *r);
  52. /**
  53. * iio_mark_sw_rb_in_use() reference counting to prevent incorrect chances
  54. **/
  55. void iio_mark_sw_rb_in_use(struct iio_ring_buffer *r);
  56. /**
  57. * iio_unmark_sw_rb_in_use() notify the ring buffer that we don't care anymore
  58. **/
  59. void iio_unmark_sw_rb_in_use(struct iio_ring_buffer *r);
  60. /**
  61. * iio_read_last_from_sw_rb() attempt to read the last stored datum from the rb
  62. **/
  63. int iio_read_last_from_sw_rb(struct iio_ring_buffer *r, u8 *data);
  64. /**
  65. * iio_store_to_sw_rb() store a new datum to the ring buffer
  66. * @rb: pointer to ring buffer instance
  67. * @data: the datum to be stored including timestamp if relevant.
  68. * @timestamp: timestamp which will be attached to buffer events if relevant.
  69. **/
  70. int iio_store_to_sw_rb(struct iio_ring_buffer *r, u8 *data, s64 timestamp);
  71. /**
  72. * iio_rip_sw_rb() attempt to read data from the ring buffer
  73. * @r: ring buffer instance
  74. * @count: number of datum's to try and read
  75. * @data: where the data will be stored.
  76. * @dead_offset: how much of the stored data was possibly invalidated by
  77. * the end of the copy.
  78. **/
  79. int iio_rip_sw_rb(struct iio_ring_buffer *r,
  80. size_t count,
  81. u8 **data,
  82. int *dead_offset);
  83. /**
  84. * iio_request_update_sw_rb() update params if update needed
  85. **/
  86. int iio_request_update_sw_rb(struct iio_ring_buffer *r);
  87. /**
  88. * iio_mark_update_needed_sw_rb() tell the ring buffer it needs a param update
  89. **/
  90. int iio_mark_update_needed_sw_rb(struct iio_ring_buffer *r);
  91. /**
  92. * iio_get_bpd_sw_rb() get the datum size in bytes
  93. **/
  94. int iio_get_bpd_sw_rb(struct iio_ring_buffer *r);
  95. /**
  96. * iio_set_bpd_sw_rb() set the datum size in bytes
  97. **/
  98. int iio_set_bpd_sw_rb(struct iio_ring_buffer *r, size_t bpd);
  99. /**
  100. * iio_get_length_sw_rb() get how many datums the rb may contain
  101. **/
  102. int iio_get_length_sw_rb(struct iio_ring_buffer *r);
  103. /**
  104. * iio_set_length_sw_rb() set how many datums the rb may contain
  105. **/
  106. int iio_set_length_sw_rb(struct iio_ring_buffer *r, int length);
  107. /**
  108. * iio_ring_sw_register_funcs() helper function to set up rb access
  109. **/
  110. static inline void iio_ring_sw_register_funcs(struct iio_ring_access_funcs *ra)
  111. {
  112. ra->mark_in_use = &iio_mark_sw_rb_in_use;
  113. ra->unmark_in_use = &iio_unmark_sw_rb_in_use;
  114. ra->store_to = &iio_store_to_sw_rb;
  115. ra->read_last = &iio_read_last_from_sw_rb;
  116. ra->rip_lots = &iio_rip_sw_rb;
  117. ra->mark_param_change = &iio_mark_update_needed_sw_rb;
  118. ra->request_update = &iio_request_update_sw_rb;
  119. ra->get_bpd = &iio_get_bpd_sw_rb;
  120. ra->set_bpd = &iio_set_bpd_sw_rb;
  121. ra->get_length = &iio_get_length_sw_rb;
  122. ra->set_length = &iio_set_length_sw_rb;
  123. };
  124. /**
  125. * struct iio_sw_ring_buffer - software ring buffer
  126. * @buf: generic ring buffer elements
  127. * @data: the ring buffer memory
  128. * @read_p: read pointer (oldest available)
  129. * @write_p: write pointer
  130. * @last_written_p: read pointer (newest available)
  131. * @half_p: half buffer length behind write_p (event generation)
  132. * @use_count: reference count to prevent resizing when in use
  133. * @update_needed: flag to indicated change in size requested
  134. * @use_lock: lock to prevent change in size when in use
  135. *
  136. * Note that the first element of all ring buffers must be a
  137. * struct iio_ring_buffer.
  138. **/
  139. struct iio_sw_ring_buffer {
  140. struct iio_ring_buffer buf;
  141. unsigned char *data;
  142. unsigned char *read_p;
  143. unsigned char *write_p;
  144. unsigned char *last_written_p;
  145. /* used to act as a point at which to signal an event */
  146. unsigned char *half_p;
  147. int use_count;
  148. int update_needed;
  149. spinlock_t use_lock;
  150. };
  151. #define iio_to_sw_ring(r) container_of(r, struct iio_sw_ring_buffer, buf)
  152. struct iio_ring_buffer *iio_sw_rb_allocate(struct iio_dev *indio_dev);
  153. void iio_sw_rb_free(struct iio_ring_buffer *ring);
  154. #else /* CONFIG_IIO_RING_BUFFER*/
  155. static inline void iio_ring_sw_register_funcs(struct iio_ring_access_funcs *ra)
  156. {};
  157. #endif /* !CONFIG_IIO_RING_BUFFER */
  158. #endif /* _IIO_RING_SW_H_ */