industrialio-buffer-cb.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* The industrial I/O callback buffer
  2. *
  3. * This program is free software; you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License version 2 as published by
  5. * the Free Software Foundation.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/err.h>
  11. #include <linux/export.h>
  12. #include <linux/iio/iio.h>
  13. #include <linux/iio/buffer_impl.h>
  14. #include <linux/iio/consumer.h>
  15. struct iio_cb_buffer {
  16. struct iio_buffer buffer;
  17. int (*cb)(const void *data, void *private);
  18. void *private;
  19. struct iio_channel *channels;
  20. struct iio_dev *indio_dev;
  21. };
  22. static struct iio_cb_buffer *buffer_to_cb_buffer(struct iio_buffer *buffer)
  23. {
  24. return container_of(buffer, struct iio_cb_buffer, buffer);
  25. }
  26. static int iio_buffer_cb_store_to(struct iio_buffer *buffer, const void *data)
  27. {
  28. struct iio_cb_buffer *cb_buff = buffer_to_cb_buffer(buffer);
  29. return cb_buff->cb(data, cb_buff->private);
  30. }
  31. static void iio_buffer_cb_release(struct iio_buffer *buffer)
  32. {
  33. struct iio_cb_buffer *cb_buff = buffer_to_cb_buffer(buffer);
  34. kfree(cb_buff->buffer.scan_mask);
  35. kfree(cb_buff);
  36. }
  37. static const struct iio_buffer_access_funcs iio_cb_access = {
  38. .store_to = &iio_buffer_cb_store_to,
  39. .release = &iio_buffer_cb_release,
  40. .modes = INDIO_BUFFER_SOFTWARE | INDIO_BUFFER_TRIGGERED,
  41. };
  42. struct iio_cb_buffer *iio_channel_get_all_cb(struct device *dev,
  43. int (*cb)(const void *data,
  44. void *private),
  45. void *private)
  46. {
  47. int ret;
  48. struct iio_cb_buffer *cb_buff;
  49. struct iio_channel *chan;
  50. cb_buff = kzalloc(sizeof(*cb_buff), GFP_KERNEL);
  51. if (cb_buff == NULL)
  52. return ERR_PTR(-ENOMEM);
  53. iio_buffer_init(&cb_buff->buffer);
  54. cb_buff->private = private;
  55. cb_buff->cb = cb;
  56. cb_buff->buffer.access = &iio_cb_access;
  57. INIT_LIST_HEAD(&cb_buff->buffer.demux_list);
  58. cb_buff->channels = iio_channel_get_all(dev);
  59. if (IS_ERR(cb_buff->channels)) {
  60. ret = PTR_ERR(cb_buff->channels);
  61. goto error_free_cb_buff;
  62. }
  63. cb_buff->indio_dev = cb_buff->channels[0].indio_dev;
  64. cb_buff->buffer.scan_mask
  65. = kcalloc(BITS_TO_LONGS(cb_buff->indio_dev->masklength),
  66. sizeof(long), GFP_KERNEL);
  67. if (cb_buff->buffer.scan_mask == NULL) {
  68. ret = -ENOMEM;
  69. goto error_release_channels;
  70. }
  71. chan = &cb_buff->channels[0];
  72. while (chan->indio_dev) {
  73. if (chan->indio_dev != cb_buff->indio_dev) {
  74. ret = -EINVAL;
  75. goto error_free_scan_mask;
  76. }
  77. set_bit(chan->channel->scan_index,
  78. cb_buff->buffer.scan_mask);
  79. chan++;
  80. }
  81. return cb_buff;
  82. error_free_scan_mask:
  83. kfree(cb_buff->buffer.scan_mask);
  84. error_release_channels:
  85. iio_channel_release_all(cb_buff->channels);
  86. error_free_cb_buff:
  87. kfree(cb_buff);
  88. return ERR_PTR(ret);
  89. }
  90. EXPORT_SYMBOL_GPL(iio_channel_get_all_cb);
  91. int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff)
  92. {
  93. return iio_update_buffers(cb_buff->indio_dev, &cb_buff->buffer,
  94. NULL);
  95. }
  96. EXPORT_SYMBOL_GPL(iio_channel_start_all_cb);
  97. void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff)
  98. {
  99. iio_update_buffers(cb_buff->indio_dev, NULL, &cb_buff->buffer);
  100. }
  101. EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb);
  102. void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buff)
  103. {
  104. iio_channel_release_all(cb_buff->channels);
  105. iio_buffer_put(&cb_buff->buffer);
  106. }
  107. EXPORT_SYMBOL_GPL(iio_channel_release_all_cb);
  108. struct iio_channel
  109. *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer)
  110. {
  111. return cb_buffer->channels;
  112. }
  113. EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels);
  114. struct iio_dev
  115. *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer)
  116. {
  117. return cb_buffer->indio_dev;
  118. }
  119. EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev);
  120. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  121. MODULE_DESCRIPTION("Industrial I/O callback buffer");
  122. MODULE_LICENSE("GPL");