iio_simple_dummy_buffer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * Copyright (c) 2011 Jonathan Cameron
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * Buffer handling elements of industrial I/O reference driver.
  9. * Uses the kfifo buffer.
  10. *
  11. * To test without hardware use the sysfs trigger.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/export.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/bitmap.h>
  19. #include <linux/iio/iio.h>
  20. #include <linux/iio/trigger_consumer.h>
  21. #include <linux/iio/kfifo_buf.h>
  22. #include "iio_simple_dummy.h"
  23. /* Some fake data */
  24. static const s16 fakedata[] = {
  25. [DUMMY_INDEX_VOLTAGE_0] = 7,
  26. [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
  27. [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
  28. [DUMMY_INDEX_ACCELX] = 344,
  29. };
  30. /**
  31. * iio_simple_dummy_trigger_h() - the trigger handler function
  32. * @irq: the interrupt number
  33. * @p: private data - always a pointer to the poll func.
  34. *
  35. * This is the guts of buffered capture. On a trigger event occurring,
  36. * if the pollfunc is attached then this handler is called as a threaded
  37. * interrupt (and hence may sleep). It is responsible for grabbing data
  38. * from the device and pushing it into the associated buffer.
  39. */
  40. static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
  41. {
  42. struct iio_poll_func *pf = p;
  43. struct iio_dev *indio_dev = pf->indio_dev;
  44. int len = 0;
  45. u16 *data;
  46. data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  47. if (!data)
  48. goto done;
  49. if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
  50. /*
  51. * Three common options here:
  52. * hardware scans: certain combinations of channels make
  53. * up a fast read. The capture will consist of all of them.
  54. * Hence we just call the grab data function and fill the
  55. * buffer without processing.
  56. * software scans: can be considered to be random access
  57. * so efficient reading is just a case of minimal bus
  58. * transactions.
  59. * software culled hardware scans:
  60. * occasionally a driver may process the nearest hardware
  61. * scan to avoid storing elements that are not desired. This
  62. * is the fiddliest option by far.
  63. * Here let's pretend we have random access. And the values are
  64. * in the constant table fakedata.
  65. */
  66. int i, j;
  67. for (i = 0, j = 0;
  68. i < bitmap_weight(indio_dev->active_scan_mask,
  69. indio_dev->masklength);
  70. i++, j++) {
  71. j = find_next_bit(indio_dev->active_scan_mask,
  72. indio_dev->masklength, j);
  73. /* random access read from the 'device' */
  74. data[i] = fakedata[j];
  75. len += 2;
  76. }
  77. }
  78. iio_push_to_buffers_with_timestamp(indio_dev, data,
  79. iio_get_time_ns(indio_dev));
  80. kfree(data);
  81. done:
  82. /*
  83. * Tell the core we are done with this trigger and ready for the
  84. * next one.
  85. */
  86. iio_trigger_notify_done(indio_dev->trig);
  87. return IRQ_HANDLED;
  88. }
  89. static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
  90. /*
  91. * iio_triggered_buffer_postenable:
  92. * Generic function that simply attaches the pollfunc to the trigger.
  93. * Replace this to mess with hardware state before we attach the
  94. * trigger.
  95. */
  96. .postenable = &iio_triggered_buffer_postenable,
  97. /*
  98. * iio_triggered_buffer_predisable:
  99. * Generic function that simple detaches the pollfunc from the trigger.
  100. * Replace this to put hardware state back again after the trigger is
  101. * detached but before userspace knows we have disabled the ring.
  102. */
  103. .predisable = &iio_triggered_buffer_predisable,
  104. };
  105. int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
  106. {
  107. int ret;
  108. struct iio_buffer *buffer;
  109. /* Allocate a buffer to use - here a kfifo */
  110. buffer = iio_kfifo_allocate();
  111. if (!buffer) {
  112. ret = -ENOMEM;
  113. goto error_ret;
  114. }
  115. iio_device_attach_buffer(indio_dev, buffer);
  116. /* Enable timestamps by default */
  117. buffer->scan_timestamp = true;
  118. /*
  119. * Tell the core what device type specific functions should
  120. * be run on either side of buffer capture enable / disable.
  121. */
  122. indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
  123. /*
  124. * Configure a polling function.
  125. * When a trigger event with this polling function connected
  126. * occurs, this function is run. Typically this grabs data
  127. * from the device.
  128. *
  129. * NULL for the bottom half. This is normally implemented only if we
  130. * either want to ping a capture now pin (no sleeping) or grab
  131. * a timestamp as close as possible to a data ready trigger firing.
  132. *
  133. * IRQF_ONESHOT ensures irqs are masked such that only one instance
  134. * of the handler can run at a time.
  135. *
  136. * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
  137. * as seen under /proc/interrupts. Remaining parameters as per printk.
  138. */
  139. indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
  140. &iio_simple_dummy_trigger_h,
  141. IRQF_ONESHOT,
  142. indio_dev,
  143. "iio_simple_dummy_consumer%d",
  144. indio_dev->id);
  145. if (!indio_dev->pollfunc) {
  146. ret = -ENOMEM;
  147. goto error_free_buffer;
  148. }
  149. /*
  150. * Notify the core that this device is capable of buffered capture
  151. * driven by a trigger.
  152. */
  153. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  154. return 0;
  155. error_free_buffer:
  156. iio_kfifo_free(indio_dev->buffer);
  157. error_ret:
  158. return ret;
  159. }
  160. /**
  161. * iio_simple_dummy_unconfigure_buffer() - release buffer resources
  162. * @indo_dev: device instance state
  163. */
  164. void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
  165. {
  166. iio_dealloc_pollfunc(indio_dev->pollfunc);
  167. iio_kfifo_free(indio_dev->buffer);
  168. }