industrialio-buffer-cb.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_cb_set_buffer_watermark(struct iio_cb_buffer *cb_buff,
  92. size_t watermark)
  93. {
  94. if (!watermark)
  95. return -EINVAL;
  96. cb_buff->buffer.watermark = watermark;
  97. return 0;
  98. }
  99. EXPORT_SYMBOL_GPL(iio_channel_cb_set_buffer_watermark);
  100. int iio_channel_start_all_cb(struct iio_cb_buffer *cb_buff)
  101. {
  102. return iio_update_buffers(cb_buff->indio_dev, &cb_buff->buffer,
  103. NULL);
  104. }
  105. EXPORT_SYMBOL_GPL(iio_channel_start_all_cb);
  106. void iio_channel_stop_all_cb(struct iio_cb_buffer *cb_buff)
  107. {
  108. iio_update_buffers(cb_buff->indio_dev, NULL, &cb_buff->buffer);
  109. }
  110. EXPORT_SYMBOL_GPL(iio_channel_stop_all_cb);
  111. void iio_channel_release_all_cb(struct iio_cb_buffer *cb_buff)
  112. {
  113. iio_channel_release_all(cb_buff->channels);
  114. iio_buffer_put(&cb_buff->buffer);
  115. }
  116. EXPORT_SYMBOL_GPL(iio_channel_release_all_cb);
  117. struct iio_channel
  118. *iio_channel_cb_get_channels(const struct iio_cb_buffer *cb_buffer)
  119. {
  120. return cb_buffer->channels;
  121. }
  122. EXPORT_SYMBOL_GPL(iio_channel_cb_get_channels);
  123. struct iio_dev
  124. *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer)
  125. {
  126. return cb_buffer->indio_dev;
  127. }
  128. EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev);
  129. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  130. MODULE_DESCRIPTION("Industrial I/O callback buffer");
  131. MODULE_LICENSE("GPL");