kfifo_buf.c 5.0 KB

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