industrialio-triggered-buffer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012 Analog Devices, Inc.
  4. * Author: Lars-Peter Clausen <lars@metafoo.de>
  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 iio_dev_attr **buffer_attrs)
  43. {
  44. struct iio_buffer *buffer;
  45. int ret;
  46. /*
  47. * iio_triggered_buffer_cleanup() assumes that the buffer allocated here
  48. * is assigned to indio_dev->buffer but this is only the case if this
  49. * function is the first caller to iio_device_attach_buffer(). If
  50. * indio_dev->buffer is already set then we can't proceed otherwise the
  51. * cleanup function will try to free a buffer that was not allocated here.
  52. */
  53. if (indio_dev->buffer)
  54. return -EADDRINUSE;
  55. buffer = iio_kfifo_allocate();
  56. if (!buffer) {
  57. ret = -ENOMEM;
  58. goto error_ret;
  59. }
  60. indio_dev->pollfunc = iio_alloc_pollfunc(h,
  61. thread,
  62. IRQF_ONESHOT,
  63. indio_dev,
  64. "%s_consumer%d",
  65. indio_dev->name,
  66. iio_device_id(indio_dev));
  67. if (indio_dev->pollfunc == NULL) {
  68. ret = -ENOMEM;
  69. goto error_kfifo_free;
  70. }
  71. /* Ring buffer functions - here trigger setup related */
  72. indio_dev->setup_ops = setup_ops;
  73. /* Flag that polled ring buffering is possible */
  74. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  75. buffer->direction = direction;
  76. buffer->attrs = buffer_attrs;
  77. ret = iio_device_attach_buffer(indio_dev, buffer);
  78. if (ret < 0)
  79. goto error_dealloc_pollfunc;
  80. return 0;
  81. error_dealloc_pollfunc:
  82. iio_dealloc_pollfunc(indio_dev->pollfunc);
  83. error_kfifo_free:
  84. iio_kfifo_free(buffer);
  85. error_ret:
  86. return ret;
  87. }
  88. EXPORT_SYMBOL(iio_triggered_buffer_setup_ext);
  89. /**
  90. * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup_ext()
  91. * @indio_dev: IIO device structure
  92. */
  93. void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
  94. {
  95. iio_dealloc_pollfunc(indio_dev->pollfunc);
  96. iio_kfifo_free(indio_dev->buffer);
  97. }
  98. EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
  99. static void devm_iio_triggered_buffer_clean(void *indio_dev)
  100. {
  101. iio_triggered_buffer_cleanup(indio_dev);
  102. }
  103. int devm_iio_triggered_buffer_setup_ext(struct device *dev,
  104. struct iio_dev *indio_dev,
  105. irqreturn_t (*h)(int irq, void *p),
  106. irqreturn_t (*thread)(int irq, void *p),
  107. enum iio_buffer_direction direction,
  108. const struct iio_buffer_setup_ops *ops,
  109. const struct iio_dev_attr **buffer_attrs)
  110. {
  111. int ret;
  112. ret = iio_triggered_buffer_setup_ext(indio_dev, h, thread, direction,
  113. ops, buffer_attrs);
  114. if (ret)
  115. return ret;
  116. return devm_add_action_or_reset(dev, devm_iio_triggered_buffer_clean,
  117. indio_dev);
  118. }
  119. EXPORT_SYMBOL_GPL(devm_iio_triggered_buffer_setup_ext);
  120. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  121. MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
  122. MODULE_LICENSE("GPL");