industrialio-triggered-buffer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2012 Analog Devices, Inc.
  3. * Author: Lars-Peter Clausen <lars@metafoo.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/module.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/buffer.h>
  14. #include <linux/iio/kfifo_buf.h>
  15. #include <linux/iio/triggered_buffer.h>
  16. #include <linux/iio/trigger_consumer.h>
  17. static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
  18. .preenable = &iio_sw_buffer_preenable,
  19. .postenable = &iio_triggered_buffer_postenable,
  20. .predisable = &iio_triggered_buffer_predisable,
  21. };
  22. /**
  23. * iio_triggered_buffer_setup() - Setup triggered buffer and pollfunc
  24. * @indio_dev: IIO device structure
  25. * @pollfunc_bh: Function which will be used as pollfunc bottom half
  26. * @pollfunc_th: Function which will be used as pollfunc top half
  27. * @setup_ops: Buffer setup functions to use for this device.
  28. * If NULL the default setup functions for triggered
  29. * buffers will be used.
  30. *
  31. * This function combines some common tasks which will normally be performed
  32. * when setting up a triggered buffer. It will allocate the buffer and the
  33. * pollfunc, as well as register the buffer with the IIO core.
  34. *
  35. * Before calling this function the indio_dev structure should already be
  36. * completely initialized, but not yet registered. In practice this means that
  37. * this function should be called right before iio_device_register().
  38. *
  39. * To free the resources allocated by this function call
  40. * iio_triggered_buffer_cleanup().
  41. */
  42. int iio_triggered_buffer_setup(struct iio_dev *indio_dev,
  43. irqreturn_t (*pollfunc_bh)(int irq, void *p),
  44. irqreturn_t (*pollfunc_th)(int irq, void *p),
  45. const struct iio_buffer_setup_ops *setup_ops)
  46. {
  47. int ret;
  48. indio_dev->buffer = iio_kfifo_allocate(indio_dev);
  49. if (!indio_dev->buffer) {
  50. ret = -ENOMEM;
  51. goto error_ret;
  52. }
  53. indio_dev->pollfunc = iio_alloc_pollfunc(pollfunc_bh,
  54. pollfunc_th,
  55. IRQF_ONESHOT,
  56. indio_dev,
  57. "%s_consumer%d",
  58. indio_dev->name,
  59. indio_dev->id);
  60. if (indio_dev->pollfunc == NULL) {
  61. ret = -ENOMEM;
  62. goto error_kfifo_free;
  63. }
  64. /* Ring buffer functions - here trigger setup related */
  65. if (setup_ops)
  66. indio_dev->setup_ops = setup_ops;
  67. else
  68. indio_dev->setup_ops = &iio_triggered_buffer_setup_ops;
  69. /* Flag that polled ring buffering is possible */
  70. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  71. ret = iio_buffer_register(indio_dev,
  72. indio_dev->channels,
  73. indio_dev->num_channels);
  74. if (ret)
  75. goto error_dealloc_pollfunc;
  76. return 0;
  77. error_dealloc_pollfunc:
  78. iio_dealloc_pollfunc(indio_dev->pollfunc);
  79. error_kfifo_free:
  80. iio_kfifo_free(indio_dev->buffer);
  81. error_ret:
  82. return ret;
  83. }
  84. EXPORT_SYMBOL(iio_triggered_buffer_setup);
  85. /**
  86. * iio_triggered_buffer_cleanup() - Free resources allocated by iio_triggered_buffer_setup()
  87. * @indio_dev: IIO device structure
  88. */
  89. void iio_triggered_buffer_cleanup(struct iio_dev *indio_dev)
  90. {
  91. iio_buffer_unregister(indio_dev);
  92. iio_dealloc_pollfunc(indio_dev->pollfunc);
  93. iio_kfifo_free(indio_dev->buffer);
  94. }
  95. EXPORT_SYMBOL(iio_triggered_buffer_cleanup);
  96. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  97. MODULE_DESCRIPTION("IIO helper functions for setting up triggered buffers");
  98. MODULE_LICENSE("GPL");