kfifo_buf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. struct iio_kfifo {
  10. struct iio_buffer buffer;
  11. struct kfifo kf;
  12. int update_needed;
  13. };
  14. #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
  15. static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
  16. int bytes_per_datum, int length)
  17. {
  18. if ((length == 0) || (bytes_per_datum == 0))
  19. return -EINVAL;
  20. __iio_update_buffer(&buf->buffer, bytes_per_datum, length);
  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. error_ret:
  34. return ret;
  35. }
  36. static int iio_get_length_kfifo(struct iio_buffer *r)
  37. {
  38. return r->length;
  39. }
  40. static IIO_BUFFER_ENABLE_ATTR;
  41. static IIO_BUFFER_LENGTH_ATTR;
  42. static struct attribute *iio_kfifo_attributes[] = {
  43. &dev_attr_length.attr,
  44. &dev_attr_enable.attr,
  45. NULL,
  46. };
  47. static struct attribute_group iio_kfifo_attribute_group = {
  48. .attrs = iio_kfifo_attributes,
  49. .name = "buffer",
  50. };
  51. static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
  52. {
  53. return r->bytes_per_datum;
  54. }
  55. static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
  56. {
  57. struct iio_kfifo *kf = iio_to_kfifo(r);
  58. kf->update_needed = true;
  59. return 0;
  60. }
  61. static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
  62. {
  63. if (r->bytes_per_datum != bpd) {
  64. r->bytes_per_datum = bpd;
  65. iio_mark_update_needed_kfifo(r);
  66. }
  67. return 0;
  68. }
  69. static int iio_set_length_kfifo(struct iio_buffer *r, int length)
  70. {
  71. if (r->length != length) {
  72. r->length = length;
  73. iio_mark_update_needed_kfifo(r);
  74. }
  75. return 0;
  76. }
  77. static int iio_store_to_kfifo(struct iio_buffer *r,
  78. u8 *data,
  79. s64 timestamp)
  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_first_n_kfifo(struct iio_buffer *r,
  89. size_t n, char __user *buf)
  90. {
  91. int ret, copied;
  92. struct iio_kfifo *kf = iio_to_kfifo(r);
  93. if (n < r->bytes_per_datum)
  94. return -EINVAL;
  95. ret = kfifo_to_user(&kf->kf, buf, n, &copied);
  96. return copied;
  97. }
  98. static const struct iio_buffer_access_funcs kfifo_access_funcs = {
  99. .store_to = &iio_store_to_kfifo,
  100. .read_first_n = &iio_read_first_n_kfifo,
  101. .request_update = &iio_request_update_kfifo,
  102. .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
  103. .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
  104. .get_length = &iio_get_length_kfifo,
  105. .set_length = &iio_set_length_kfifo,
  106. };
  107. struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
  108. {
  109. struct iio_kfifo *kf;
  110. kf = kzalloc(sizeof *kf, GFP_KERNEL);
  111. if (!kf)
  112. return NULL;
  113. kf->update_needed = true;
  114. iio_buffer_init(&kf->buffer);
  115. kf->buffer.attrs = &iio_kfifo_attribute_group;
  116. kf->buffer.access = &kfifo_access_funcs;
  117. return &kf->buffer;
  118. }
  119. EXPORT_SYMBOL(iio_kfifo_allocate);
  120. void iio_kfifo_free(struct iio_buffer *r)
  121. {
  122. kfree(iio_to_kfifo(r));
  123. }
  124. EXPORT_SYMBOL(iio_kfifo_free);
  125. MODULE_LICENSE("GPL");