kfifo_buf.c 3.6 KB

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