kfifo_buf.c 3.6 KB

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