industrialio-triggered-buffer.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012 Analog Devices, Inc.
  4. * Author: Lars-Peter Clausen <[email protected]>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/export.h>
  8. #include <linux/module.h>
  9. #include <linux/iio/iio.h>
  10. #include <linux/iio/buffer.h>
  11. #include <linux/iio/buffer_impl.h>
  12. #include <linux/iio/kfifo_buf.h>
  13. #include <linux/iio/triggered_buffer.h>
  14. #include <linux/iio/trigger_consumer.h>
  15. /**
  16. * iio_triggered_buffer_setup_ext() - Setup triggered buffer and pollfunc
  17. * @indio_dev: IIO device structure
  18. * @h: Function which will be used as pollfunc top half
  19. * @thread: Function which will be used as pollfunc bottom half
  20. * @direction: Direction of the data stream (in/out).
  21. * @setup_ops: Buffer setup functions to use for this device.
  22. * If NULL the default setup functions for triggered
  23. * buffers will be used.
  24. * @buffer_attrs: Extra sysfs buffer attributes for this IIO buffer
  25. *
  26. * This function combines some common tasks which will normally be performed
  27. * when setting up a triggered buffer. It will allocate the buffer and the
  28. * pollfunc.
  29. *
  30. * Before calling this function the indio_dev structure should already be
  31. * completely initialized, but not yet registered. In practice this means that
  32. * this function should be called right before iio_device_register().
  33. *
  34. * To free the resources allocated by this function call
  35. * iio_triggered_buffer_cleanup().
  36. */
  37. int iio_triggered_buffer_setup_ext(struct iio_dev *indio_dev,
  38. irqreturn_t (*h)(int irq, void *p),
  39. irqreturn_t (*thread)(int irq, void *p),
  40. enum iio_buffer_direction direction,
  41. const struct iio_buffer_setup_ops *setup_ops,
  42. const struct attribute **buffer_attrs)
  43. {
  44. struct iio_buffer *buffer;
  45. int ret;
  46. buffer = iio_kfifo_allocate();
  47. if (!buffer) {
  48. ret = -ENOMEM;
  49. goto error_ret;
  50. }
  51. indio_dev->pollfunc = iio_alloc_pollfunc(h,
  52. thread,
  53. IRQF_ONESHOT,
  54. indio_dev,
  55. "%s_consumer%d",
  56. indio_dev->name,
  57. iio_device_id(indio_dev));
  58. if (indio_dev->pollfunc == NULL) {
  59. ret = -ENOMEM;
  60. goto error_kfifo_free;
  61. }
  62. /* Ring buffer functions - here trigger setup related */
  63. indio_dev->setup_ops = setup_ops;
  64. /* Flag that polled ring buffering is possible */
  65. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  66. buffer->direction = direction;
  67. buffer->attrs = buffer_attrs;
  68. ret = iio_device_attach_buffer(indio_dev, buffer);
  69. if (ret < 0)
  70. goto error_dealloc_pollfunc;
  71. return 0;
  72. error_dealloc_pollfunc:
  73. iio_dealloc_pollfunc(indio_dev->pollfunc);
  74. error_kfifo_free:
  75. iio_kfifo_free(buffer);
  76. error_ret:
  77. return ret;
  78. }
  79. EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
  80. /**
  81. * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
  82. * @indio_dev: IIO device structure
  83. */
  84. void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
  85. {
  86. iio_dealloc_pollfunc(indio_dev->pollfunc);
  87. iio_kfifo_free(indio_dev->buffer);
  88. }
  89. EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
  90. static void devm_iio_triggered_buffer_clean(void *indio_dev)
  91. {
  92. iio_triggered_buffer_cleanup(indio_dev);
  93. }
  94. int devm_iio_triggered_buffer_setup_ext(struct device *dev,
  95. struct iio_dev *indio_dev,
  96. irqreturn_t (*h)(int irq, void *p),
  97. irqreturn_t (*thread)(int irq, void *p),
  98. enum iio_buffer_direction direction,
  99. const struct iio_buffer_setup_ops *ops,
  100. const struct attribute **buffer_attrs)
  101. {
  102. int ret;
  103. ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction,
  104. ops, buffer_attrs);
  105. if (ret)
  106. return ret;
  107. return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean,
  108. indio_dev);
  109. }
  110. EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
  111. MODULE_AUTHOR("Lars-Peter Clausen <[email protected]>");
  112. MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
  113. MODULE_LICENSE("GPL");