kfifo_buf.c 5.0 KB

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