iio_simple_dummy_buffer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, iio_get_time_ns());
  79. kfree(data);
  80. done:
  81. /*
  82. * Tell the core we are done with this trigger and ready for the
  83. * next one.
  84. */
  85. iio_trigger_notify_done(indio_dev->trig);
  86. return IRQ_HANDLED;
  87. }
  88. static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
  89. /*
  90. * iio_triggered_buffer_postenable:
  91. * Generic function that simply attaches the pollfunc to the trigger.
  92. * Replace this to mess with hardware state before we attach the
  93. * trigger.
  94. */
  95. .postenable = &iio_triggered_buffer_postenable,
  96. /*
  97. * iio_triggered_buffer_predisable:
  98. * Generic function that simple detaches the pollfunc from the trigger.
  99. * Replace this to put hardware state back again after the trigger is
  100. * detached but before userspace knows we have disabled the ring.
  101. */
  102. .predisable = &iio_triggered_buffer_predisable,
  103. };
  104. int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
  105. {
  106. int ret;
  107. struct iio_buffer *buffer;
  108. /* Allocate a buffer to use - here a kfifo */
  109. buffer = iio_kfifo_allocate();
  110. if (!buffer) {
  111. ret = -ENOMEM;
  112. goto error_ret;
  113. }
  114. iio_device_attach_buffer(indio_dev, buffer);
  115. /* Enable timestamps by default */
  116. buffer->scan_timestamp = true;
  117. /*
  118. * Tell the core what device type specific functions should
  119. * be run on either side of buffer capture enable / disable.
  120. */
  121. indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops;
  122. /*
  123. * Configure a polling function.
  124. * When a trigger event with this polling function connected
  125. * occurs, this function is run. Typically this grabs data
  126. * from the device.
  127. *
  128. * NULL for the bottom half. This is normally implemented only if we
  129. * either want to ping a capture now pin (no sleeping) or grab
  130. * a timestamp as close as possible to a data ready trigger firing.
  131. *
  132. * IRQF_ONESHOT ensures irqs are masked such that only one instance
  133. * of the handler can run at a time.
  134. *
  135. * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
  136. * as seen under /proc/interrupts. Remaining parameters as per printk.
  137. */
  138. indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
  139. &iio_simple_dummy_trigger_h,
  140. IRQF_ONESHOT,
  141. indio_dev,
  142. "iio_simple_dummy_consumer%d",
  143. indio_dev->id);
  144. if (!indio_dev->pollfunc) {
  145. ret = -ENOMEM;
  146. goto error_free_buffer;
  147. }
  148. /*
  149. * Notify the core that this device is capable of buffered capture
  150. * driven by a trigger.
  151. */
  152. indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
  153. return 0;
  154. error_free_buffer:
  155. iio_kfifo_free(indio_dev->buffer);
  156. error_ret:
  157. return ret;
  158. }
  159. /**
  160. * iio_simple_dummy_unconfigure_buffer() - release buffer resources
  161. * @indo_dev: device instance state
  162. */
  163. void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
  164. {
  165. iio_dealloc_pollfunc(indio_dev->pollfunc);
  166. iio_kfifo_free(indio_dev->buffer);
  167. }