kfifo_buf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/slab.h>
  3. #include <linux/kernel.h>
  4. #include <linux/module.h>
  5. #include <linux/device.h>
  6. #include <linux/workqueue.h>
  7. #include <linux/kfifo.h>
  8. #include <linux/mutex.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/iio/buffer.h>
  11. #include <linux/iio/kfifo_buf.h>
  12. #include <linux/iio/buffer_impl.h>
  13. #include <linux/sched.h>
  14. #include <linux/poll.h>
  15. struct iio_kfifo {
  16. struct iio_buffer buffer;
  17. struct kfifo kf;
  18. struct mutex user_lock;
  19. int update_needed;
  20. };
  21. #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
  22. static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
  23. size_t bytes_per_datum, unsigned int length)
  24. {
  25. if ((length == 0) || (bytes_per_datum == 0))
  26. return -EINVAL;
  27. /*
  28. * Make sure we don't overflow an unsigned int after kfifo rounds up to
  29. * the next power of 2.
  30. */
  31. if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
  32. return -EINVAL;
  33. return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
  34. bytes_per_datum, GFP_KERNEL);
  35. }
  36. static int iio_request_update_kfifo(struct iio_buffer *r)
  37. {
  38. int ret = 0;
  39. struct iio_kfifo *buf = iio_to_kfifo(r);
  40. mutex_lock(&buf->user_lock);
  41. if (buf->update_needed) {
  42. kfifo_free(&buf->kf);
  43. ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
  44. buf->buffer.length);
  45. if (ret >= 0)
  46. buf->update_needed = false;
  47. } else {
  48. kfifo_reset_out(&buf->kf);
  49. }
  50. mutex_unlock(&buf->user_lock);
  51. return ret;
  52. }
  53. static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
  54. {
  55. struct iio_kfifo *kf = iio_to_kfifo(r);
  56. kf->update_needed = true;
  57. return 0;
  58. }
  59. static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
  60. {
  61. if (r->bytes_per_datum != bpd) {
  62. r->bytes_per_datum = bpd;
  63. iio_mark_update_needed_kfifo(r);
  64. }
  65. return 0;
  66. }
  67. static int iio_set_length_kfifo(struct iio_buffer *r, unsigned int length)
  68. {
  69. /* Avoid an invalid state */
  70. if (length < 2)
  71. length = 2;
  72. if (r->length != length) {
  73. r->length = length;
  74. iio_mark_update_needed_kfifo(r);
  75. }
  76. return 0;
  77. }
  78. static int iio_store_to_kfifo(struct iio_buffer *r,
  79. const void *data)
  80. {
  81. int ret;
  82. struct iio_kfifo *kf = iio_to_kfifo(r);
  83. ret = kfifo_in(&kf->kf, data, 1);
  84. if (ret != 1)
  85. return -EBUSY;
  86. return 0;
  87. }
  88. static int iio_read_kfifo(struct iio_buffer *r, 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 size_t iio_kfifo_buf_space_available(struct iio_buffer *r)
  120. {
  121. struct iio_kfifo *kf = iio_to_kfifo(r);
  122. size_t avail;
  123. mutex_lock(&kf->user_lock);
  124. avail = kfifo_avail(&kf->kf);
  125. mutex_unlock(&kf->user_lock);
  126. return avail;
  127. }
  128. static int iio_kfifo_remove_from(struct iio_buffer *r, void *data)
  129. {
  130. int ret;
  131. struct iio_kfifo *kf = iio_to_kfifo(r);
  132. if (kfifo_size(&kf->kf) < 1)
  133. return -EBUSY;
  134. ret = kfifo_out(&kf->kf, data, 1);
  135. if (ret != 1)
  136. return -EBUSY;
  137. wake_up_interruptible_poll(&r->pollq, EPOLLOUT | EPOLLWRNORM);
  138. return 0;
  139. }
  140. static int iio_kfifo_write(struct iio_buffer *r, size_t n,
  141. const char __user *buf)
  142. {
  143. struct iio_kfifo *kf = iio_to_kfifo(r);
  144. int ret, copied;
  145. mutex_lock(&kf->user_lock);
  146. if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
  147. ret = -EINVAL;
  148. else
  149. ret = kfifo_from_user(&kf->kf, buf, n, &copied);
  150. mutex_unlock(&kf->user_lock);
  151. if (ret)
  152. return ret;
  153. return copied;
  154. }
  155. static const struct iio_buffer_access_funcs kfifo_access_funcs = {
  156. .store_to = &iio_store_to_kfifo,
  157. .read = &iio_read_kfifo,
  158. .data_available = iio_kfifo_buf_data_available,
  159. .remove_from = &iio_kfifo_remove_from,
  160. .write = &iio_kfifo_write,
  161. .space_available = &iio_kfifo_buf_space_available,
  162. .request_update = &iio_request_update_kfifo,
  163. .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
  164. .set_length = &iio_set_length_kfifo,
  165. .release = &iio_kfifo_buffer_release,
  166. .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
  167. };
  168. struct iio_buffer *iio_kfifo_allocate(void)
  169. {
  170. struct iio_kfifo *kf;
  171. kf = kzalloc(sizeof(*kf), GFP_KERNEL);
  172. if (!kf)
  173. return NULL;
  174. kf->update_needed = true;
  175. iio_buffer_init(&kf->buffer);
  176. kf->buffer.access = &kfifo_access_funcs;
  177. kf->buffer.length = 2;
  178. mutex_init(&kf->user_lock);
  179. return &kf->buffer;
  180. }
  181. EXPORT_SYMBOL(iio_kfifo_allocate);
  182. void iio_kfifo_free(struct iio_buffer *r)
  183. {
  184. iio_buffer_put(r);
  185. }
  186. EXPORT_SYMBOL(iio_kfifo_free);
  187. static void devm_iio_kfifo_release(struct device *dev, void *res)
  188. {
  189. iio_kfifo_free(*(struct iio_buffer **)res);
  190. }
  191. /**
  192. * devm_iio_kfifo_allocate - Resource-managed iio_kfifo_allocate()
  193. * @dev: Device to allocate kfifo buffer for
  194. *
  195. * RETURNS:
  196. * Pointer to allocated iio_buffer on success, NULL on failure.
  197. */
  198. static struct iio_buffer *devm_iio_kfifo_allocate(struct device *dev)
  199. {
  200. struct iio_buffer **ptr, *r;
  201. ptr = devres_alloc(devm_iio_kfifo_release, sizeof(*ptr), GFP_KERNEL);
  202. if (!ptr)
  203. return NULL;
  204. r = iio_kfifo_allocate();
  205. if (r) {
  206. *ptr = r;
  207. devres_add(dev, ptr);
  208. } else {
  209. devres_free(ptr);
  210. }
  211. return r;
  212. }
  213. /**
  214. * devm_iio_kfifo_buffer_setup_ext - Allocate a kfifo buffer & attach it to an IIO device
  215. * @dev: Device object to which to attach the life-time of this kfifo buffer
  216. * @indio_dev: The device the buffer should be attached to
  217. * @setup_ops: The setup_ops required to configure the HW part of the buffer (optional)
  218. * @buffer_attrs: Extra sysfs buffer attributes for this IIO buffer
  219. *
  220. * This function allocates a kfifo buffer via devm_iio_kfifo_allocate() and
  221. * attaches it to the IIO device via iio_device_attach_buffer().
  222. * This is meant to be a bit of a short-hand/helper function as there are a few
  223. * drivers that seem to do this.
  224. */
  225. int devm_iio_kfifo_buffer_setup_ext(struct device *dev,
  226. struct iio_dev *indio_dev,
  227. const struct iio_buffer_setup_ops *setup_ops,
  228. const struct attribute **buffer_attrs)
  229. {
  230. struct iio_buffer *buffer;
  231. buffer = devm_iio_kfifo_allocate(dev);
  232. if (!buffer)
  233. return -ENOMEM;
  234. indio_dev->modes |= INDIO_BUFFER_SOFTWARE;
  235. indio_dev->setup_ops = setup_ops;
  236. buffer->attrs = buffer_attrs;
  237. return iio_device_attach_buffer(indio_dev, buffer);
  238. }
  239. EXPORT_SYMBOL_GPL(devm_iio_kfifo_buffer_setup_ext);
  240. MODULE_LICENSE("GPL");