kfifo_buf.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <linux/slab.h>
  2. #include <linux/kernel.h>
  3. #include <linux/module.h>
  4. #include <linux/device.h>
  5. #include <linux/workqueue.h>
  6. #include <linux/kfifo.h>
  7. #include <linux/mutex.h>
  8. #include <linux/iio/iio.h>
  9. #include <linux/iio/buffer.h>
  10. #include <linux/iio/kfifo_buf.h>
  11. #include <linux/iio/buffer_impl.h>
  12. #include <linux/sched.h>
  13. #include <linux/poll.h>
  14. struct iio_kfifo {
  15. struct iio_buffer buffer;
  16. struct kfifo kf;
  17. struct mutex user_lock;
  18. int update_needed;
  19. };
  20. #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
  21. static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
  22. int bytes_per_datum, int length)
  23. {
  24. if ((length == 0) || (bytes_per_datum == 0))
  25. return -EINVAL;
  26. return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
  27. bytes_per_datum, GFP_KERNEL);
  28. }
  29. static int iio_request_update_kfifo(struct iio_buffer *r)
  30. {
  31. int ret = 0;
  32. struct iio_kfifo *buf = iio_to_kfifo(r);
  33. mutex_lock(&buf->user_lock);
  34. if (buf->update_needed) {
  35. kfifo_free(&buf->kf);
  36. ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
  37. buf->buffer.length);
  38. if (ret >= 0)
  39. buf->update_needed = false;
  40. } else {
  41. kfifo_reset_out(&buf->kf);
  42. }
  43. mutex_unlock(&buf->user_lock);
  44. return ret;
  45. }
  46. static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
  47. {
  48. struct iio_kfifo *kf = iio_to_kfifo(r);
  49. kf->update_needed = true;
  50. return 0;
  51. }
  52. static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
  53. {
  54. if (r->bytes_per_datum != bpd) {
  55. r->bytes_per_datum = bpd;
  56. iio_mark_update_needed_kfifo(r);
  57. }
  58. return 0;
  59. }
  60. static int iio_set_length_kfifo(struct iio_buffer *r, int length)
  61. {
  62. /* Avoid an invalid state */
  63. if (length < 2)
  64. length = 2;
  65. if (r->length != length) {
  66. r->length = length;
  67. iio_mark_update_needed_kfifo(r);
  68. }
  69. return 0;
  70. }
  71. static int iio_store_to_kfifo(struct iio_buffer *r,
  72. const void *data)
  73. {
  74. int ret;
  75. struct iio_kfifo *kf = iio_to_kfifo(r);
  76. ret = kfifo_in(&kf->kf, data, 1);
  77. if (ret != 1)
  78. return -EBUSY;
  79. return 0;
  80. }
  81. static int iio_read_first_n_kfifo(struct iio_buffer *r,
  82. size_t n, char __user *buf)
  83. {
  84. int ret, copied;
  85. struct iio_kfifo *kf = iio_to_kfifo(r);
  86. if (mutex_lock_interruptible(&kf->user_lock))
  87. return -ERESTARTSYS;
  88. if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
  89. ret = -EINVAL;
  90. else
  91. ret = kfifo_to_user(&kf->kf, buf, n, &copied);
  92. mutex_unlock(&kf->user_lock);
  93. if (ret < 0)
  94. return ret;
  95. return copied;
  96. }
  97. static size_t iio_kfifo_buf_data_available(struct iio_buffer *r)
  98. {
  99. struct iio_kfifo *kf = iio_to_kfifo(r);
  100. size_t samples;
  101. mutex_lock(&kf->user_lock);
  102. samples = kfifo_len(&kf->kf);
  103. mutex_unlock(&kf->user_lock);
  104. return samples;
  105. }
  106. static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
  107. {
  108. struct iio_kfifo *kf = iio_to_kfifo(buffer);
  109. mutex_destroy(&kf->user_lock);
  110. kfifo_free(&kf->kf);
  111. kfree(kf);
  112. }
  113. static const struct iio_buffer_access_funcs kfifo_access_funcs = {
  114. .store_to = &iio_store_to_kfifo,
  115. .read_first_n = &iio_read_first_n_kfifo,
  116. .data_available = iio_kfifo_buf_data_available,
  117. .request_update = &iio_request_update_kfifo,
  118. .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
  119. .set_length = &iio_set_length_kfifo,
  120. .release = &iio_kfifo_buffer_release,
  121. .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
  122. };
  123. struct iio_buffer *iio_kfifo_allocate(void)
  124. {
  125. struct iio_kfifo *kf;
  126. kf = kzalloc(sizeof(*kf), GFP_KERNEL);
  127. if (!kf)
  128. return NULL;
  129. kf->update_needed = true;
  130. iio_buffer_init(&kf->buffer);
  131. kf->buffer.access = &kfifo_access_funcs;
  132. kf->buffer.length = 2;
  133. mutex_init(&kf->user_lock);
  134. return &kf->buffer;
  135. }
  136. EXPORT_SYMBOL(iio_kfifo_allocate);
  137. void iio_kfifo_free(struct iio_buffer *r)
  138. {
  139. iio_buffer_put(r);
  140. }
  141. EXPORT_SYMBOL(iio_kfifo_free);
  142. static void devm_iio_kfifo_release(struct device *dev, void *res)
  143. {
  144. iio_kfifo_free(*(struct iio_buffer **)res);
  145. }
  146. static int devm_iio_kfifo_match(struct device *dev, void *res, void *data)
  147. {
  148. struct iio_buffer **r = res;
  149. if (WARN_ON(!r || !*r))
  150. return 0;
  151. return *r == data;
  152. }
  153. /**
  154. * devm_iio_fifo_allocate - Resource-managed iio_kfifo_allocate()
  155. * @dev: Device to allocate kfifo buffer for
  156. *
  157. * RETURNS:
  158. * Pointer to allocated iio_buffer on success, NULL on failure.
  159. */
  160. struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
  161. {
  162. struct iio_buffer **ptr, *r;
  163. ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
  164. if (!ptr)
  165. return NULL;
  166. r = iio_kfifo_allocate();
  167. if (r) {
  168. *ptr = r;
  169. devres_add(dev, ptr);
  170. } else {
  171. devres_free(ptr);
  172. }
  173. return r;
  174. }
  175. EXPORT_SYMBOL(devm_iio_kfifo_allocate);
  176. /**
  177. * devm_iio_fifo_free - Resource-managed iio_kfifo_free()
  178. * @dev: Device the buffer belongs to
  179. * @r: The buffer associated with the device
  180. */
  181. void devm_iio_kfifo_free(struct device *dev, struct iio_buffer *r)
  182. {
  183. WARN_ON(devres_release(dev, devm_iio_kfifo_release,
  184. devm_iio_kfifo_match, r));
  185. }
  186. EXPORT_SYMBOL(devm_iio_kfifo_free);
  187. MODULE_LICENSE("GPL");