kfifo_buf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. int bytes_per_datum, int length)
  20. {
  21. if ((length == 0) || (bytes_per_datum == 0))
  22. return -EINVAL;
  23. return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
  24. bytes_per_datum, GFP_KERNEL);
  25. }
  26. static int iio_request_update_kfifo(struct iio_buffer *r)
  27. {
  28. int ret = 0;
  29. struct iio_kfifo *buf = iio_to_kfifo(r);
  30. mutex_lock(&buf->user_lock);
  31. if (buf->update_needed) {
  32. kfifo_free(&buf->kf);
  33. ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
  34. buf->buffer.length);
  35. buf->update_needed = false;
  36. } else {
  37. kfifo_reset_out(&buf->kf);
  38. }
  39. r->stufftoread = false;
  40. mutex_unlock(&buf->user_lock);
  41. return ret;
  42. }
  43. static int iio_get_length_kfifo(struct iio_buffer *r)
  44. {
  45. return r->length;
  46. }
  47. static IIO_BUFFER_ENABLE_ATTR;
  48. static IIO_BUFFER_LENGTH_ATTR;
  49. static struct attribute *iio_kfifo_attributes[] = {
  50. &dev_attr_length.attr,
  51. &dev_attr_enable.attr,
  52. NULL,
  53. };
  54. static struct attribute_group iio_kfifo_attribute_group = {
  55. .attrs = iio_kfifo_attributes,
  56. .name = "buffer",
  57. };
  58. static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
  59. {
  60. return r->bytes_per_datum;
  61. }
  62. static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
  63. {
  64. struct iio_kfifo *kf = iio_to_kfifo(r);
  65. kf->update_needed = true;
  66. return 0;
  67. }
  68. static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
  69. {
  70. if (r->bytes_per_datum != bpd) {
  71. r->bytes_per_datum = bpd;
  72. iio_mark_update_needed_kfifo(r);
  73. }
  74. return 0;
  75. }
  76. static int iio_set_length_kfifo(struct iio_buffer *r, int length)
  77. {
  78. /* Avoid an invalid state */
  79. if (length < 2)
  80. length = 2;
  81. if (r->length != length) {
  82. r->length = length;
  83. iio_mark_update_needed_kfifo(r);
  84. }
  85. return 0;
  86. }
  87. static int iio_store_to_kfifo(struct iio_buffer *r,
  88. const void *data)
  89. {
  90. int ret;
  91. struct iio_kfifo *kf = iio_to_kfifo(r);
  92. ret = kfifo_in(&kf->kf, data, 1);
  93. if (ret != 1)
  94. return -EBUSY;
  95. r->stufftoread = true;
  96. wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
  97. return 0;
  98. }
  99. static int iio_read_first_n_kfifo(struct iio_buffer *r,
  100. size_t n, char __user *buf)
  101. {
  102. int ret, copied;
  103. struct iio_kfifo *kf = iio_to_kfifo(r);
  104. if (mutex_lock_interruptible(&kf->user_lock))
  105. return -ERESTARTSYS;
  106. if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf))
  107. ret = -EINVAL;
  108. else
  109. ret = kfifo_to_user(&kf->kf, buf, n, &copied);
  110. if (kfifo_is_empty(&kf->kf))
  111. r->stufftoread = false;
  112. /* verify it is still empty to avoid race */
  113. if (!kfifo_is_empty(&kf->kf))
  114. r->stufftoread = true;
  115. mutex_unlock(&kf->user_lock);
  116. if (ret < 0)
  117. return ret;
  118. return copied;
  119. }
  120. static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
  121. {
  122. struct iio_kfifo *kf = iio_to_kfifo(buffer);
  123. mutex_destroy(&kf->user_lock);
  124. kfifo_free(&kf->kf);
  125. kfree(kf);
  126. }
  127. static const struct iio_buffer_access_funcs kfifo_access_funcs = {
  128. .store_to = &iio_store_to_kfifo,
  129. .read_first_n = &iio_read_first_n_kfifo,
  130. .request_update = &iio_request_update_kfifo,
  131. .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
  132. .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
  133. .get_length = &iio_get_length_kfifo,
  134. .set_length = &iio_set_length_kfifo,
  135. .release = &iio_kfifo_buffer_release,
  136. };
  137. struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
  138. {
  139. struct iio_kfifo *kf;
  140. kf = kzalloc(sizeof *kf, GFP_KERNEL);
  141. if (!kf)
  142. return NULL;
  143. kf->update_needed = true;
  144. iio_buffer_init(&kf->buffer);
  145. kf->buffer.attrs = &iio_kfifo_attribute_group;
  146. kf->buffer.access = &kfifo_access_funcs;
  147. kf->buffer.length = 2;
  148. mutex_init(&kf->user_lock);
  149. return &kf->buffer;
  150. }
  151. EXPORT_SYMBOL(iio_kfifo_allocate);
  152. void iio_kfifo_free(struct iio_buffer *r)
  153. {
  154. iio_buffer_put(r);
  155. }
  156. EXPORT_SYMBOL(iio_kfifo_free);
  157. MODULE_LICENSE("GPL");