iio_simple_dummy_buffer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/buffer.h>
  22. #include <linux/iio/kfifo_buf.h>
  23. #include "iio_simple_dummy.h"
  24. /* Some fake data */
  25. static const s16 fakedata[] = {
  26. [DUMMY_INDEX_VOLTAGE_0] = 7,
  27. [DUMMY_INDEX_DIFFVOLTAGE_1M2] = -33,
  28. [DUMMY_INDEX_DIFFVOLTAGE_3M4] = -2,
  29. [DUMMY_INDEX_ACCELX] = 344,
  30. };
  31. /**
  32. * iio_simple_dummy_trigger_h() - the trigger handler function
  33. * @irq: the interrupt number
  34. * @p: private data - always a pointer to the poll func.
  35. *
  36. * This is the guts of buffered capture. On a trigger event occurring,
  37. * if the pollfunc is attached then this handler is called as a threaded
  38. * interrupt (and hence may sleep). It is responsible for grabbing data
  39. * from the device and pushing it into the associated buffer.
  40. */
  41. static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
  42. {
  43. struct iio_poll_func *pf = p;
  44. struct iio_dev *indio_dev = pf->indio_dev;
  45. int len = 0;
  46. u16 *data;
  47. data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
  48. if (!data)
  49. goto done;
  50. if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) {
  51. /*
  52. * Three common options here:
  53. * hardware scans: certain combinations of channels make
  54. * up a fast read. The capture will consist of all of them.
  55. * Hence we just call the grab data function and fill the
  56. * buffer without processing.
  57. * software scans: can be considered to be random access
  58. * so efficient reading is just a case of minimal bus
  59. * transactions.
  60. * software culled hardware scans:
  61. * occasionally a driver may process the nearest hardware
  62. * scan to avoid storing elements that are not desired. This
  63. * is the fiddliest option by far.
  64. * Here let's pretend we have random access. And the values are
  65. * in the constant table fakedata.
  66. */
  67. int i, j;
  68. for (i = 0, j = 0;
  69. i < bitmap_weight(indio_dev->active_scan_mask,
  70. indio_dev->masklength);
  71. i++, j++) {
  72. j = find_next_bit(indio_dev->active_scan_mask,
  73. indio_dev->masklength, j);
  74. /* random access read from the 'device' */
  75. data[i] = fakedata[j];
  76. len += 2;
  77. }
  78. }
  79. iio_push_to_buffers_with_timestamp(indio_dev, data,
  80. iio_get_time_ns(indio_dev));
  81. kfree(data);
  82. done:
  83. /*
  84. * Tell the core we are done with this trigger and ready for the
  85. * next one.
  86. */
  87. iio_trigger_notify_done(indio_dev->trig);
  88. return IRQ_HANDLED;
  89. }
  90. static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
  91. /*
  92. * iio_triggered_buffer_postenable:
  93. * Generic function that simply attaches the pollfunc to the trigger.
  94. * Replace this to mess with hardware state before we attach the
  95. * trigger.
  96. */
  97. .postenable = &iio_triggered_buffer_postenable,
  98. /*
  99. * iio_triggered_buffer_predisable:
  100. * Generic function that simple detaches the pollfunc from the trigger.
  101. * Replace this to put hardware state back again after the trigger is
  102. * detached but before userspace knows we have disabled the ring.
  103. */
  104. .predisable = &iio_triggered_buffer_predisable,
  105. };
  106. int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
  107. {
  108. int ret;
  109. struct iio_buffer *buffer;
  110. /* Allocate a buffer to use - here a kfifo */
  111. buffer = iio_kfifo_allocate();
  112. if (!buffer) {
  113. ret = -ENOMEM;
  114. goto error_ret;
  115. }
  116. iio_device_attach_buffer(indio_dev, buffer);
  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. }