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 14#include <linux/kernel.h> 15#include <linux/export.h> 16#include <linux/slab.h> 17#include <linux/interrupt.h> 18#include <linux/irq.h> 19#include <linux/bitmap.h> 20 21#include <linux/iio/iio.h> 22#include <linux/iio/trigger_consumer.h> 23#include <linux/iio/kfifo_buf.h> 24 25#include "iio_simple_dummy.h" 26 27/* Some fake data */ 28 29static const s16 fakedata[] = { 30 [voltage0] = 7, 31 [diffvoltage1m2] = -33, 32 [diffvoltage3m4] = -2, 33 [accelx] = 344, 34}; 35/** 36 * iio_simple_dummy_trigger_h() - the trigger handler function 37 * @irq: the interrupt number 38 * @p: private data - always a pointer to the poll func. 39 * 40 * This is the guts of buffered capture. On a trigger event occurring, 41 * if the pollfunc is attached then this handler is called as a threaded 42 * interrupt (and hence may sleep). It is responsible for grabbing data 43 * from the device and pushing it into the associated buffer. 44 */ 45static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p) 46{ 47 struct iio_poll_func *pf = p; 48 struct iio_dev *indio_dev = pf->indio_dev; 49 int len = 0; 50 u16 *data; 51 52 data = kmalloc(indio_dev->scan_bytes, GFP_KERNEL); 53 if (data == NULL) 54 goto done; 55 56 if (!bitmap_empty(indio_dev->active_scan_mask, indio_dev->masklength)) { 57 /* 58 * Three common options here: 59 * hardware scans: certain combinations of channels make 60 * up a fast read. The capture will consist of all of them. 61 * Hence we just call the grab data function and fill the 62 * buffer without processing. 63 * software scans: can be considered to be random access 64 * so efficient reading is just a case of minimal bus 65 * transactions. 66 * software culled hardware scans: 67 * occasionally a driver may process the nearest hardware 68 * scan to avoid storing elements that are not desired. This 69 * is the fiddliest option by far. 70 * Here let's pretend we have random access. And the values are 71 * in the constant table fakedata. 72 */ 73 int i, j; 74 for (i = 0, j = 0; 75 i < bitmap_weight(indio_dev->active_scan_mask, 76 indio_dev->masklength); 77 i++, j++) { 78 j = find_next_bit(indio_dev->active_scan_mask, 79 indio_dev->masklength, j); 80 /* random access read from the 'device' */ 81 data[i] = fakedata[j]; 82 len += 2; 83 } 84 } 85 /* Store the timestamp at an 8 byte aligned offset */ 86 if (indio_dev->scan_timestamp) 87 *(s64 *)((u8 *)data + ALIGN(len, sizeof(s64))) 88 = iio_get_time_ns(); 89 iio_push_to_buffers(indio_dev, (u8 *)data); 90 91 kfree(data); 92 93done: 94 /* 95 * Tell the core we are done with this trigger and ready for the 96 * next one. 97 */ 98 iio_trigger_notify_done(indio_dev->trig); 99 100 return IRQ_HANDLED; 101} 102 103static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = { 104 /* 105 * iio_sw_buffer_preenable: 106 * Generic function for equal sized ring elements + 64 bit timestamp 107 * Assumes that any combination of channels can be enabled. 108 * Typically replaced to implement restrictions on what combinations 109 * can be captured (hardware scan modes). 110 */ 111 .preenable = &iio_sw_buffer_preenable, 112 /* 113 * iio_triggered_buffer_postenable: 114 * Generic function that simply attaches the pollfunc to the trigger. 115 * Replace this to mess with hardware state before we attach the 116 * trigger. 117 */ 118 .postenable = &iio_triggered_buffer_postenable, 119 /* 120 * iio_triggered_buffer_predisable: 121 * Generic function that simple detaches the pollfunc from the trigger. 122 * Replace this to put hardware state back again after the trigger is 123 * detached but before userspace knows we have disabled the ring. 124 */ 125 .predisable = &iio_triggered_buffer_predisable, 126}; 127 128int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev, 129 const struct iio_chan_spec *channels, unsigned int num_channels) 130{ 131 int ret; 132 struct iio_buffer *buffer; 133 134 /* Allocate a buffer to use - here a kfifo */ 135 buffer = iio_kfifo_allocate(indio_dev); 136 if (buffer == NULL) { 137 ret = -ENOMEM; 138 goto error_ret; 139 } 140 141 indio_dev->buffer = buffer; 142 143 /* Enable timestamps by default */ 144 buffer->scan_timestamp = true; 145 146 /* 147 * Tell the core what device type specific functions should 148 * be run on either side of buffer capture enable / disable. 149 */ 150 indio_dev->setup_ops = &iio_simple_dummy_buffer_setup_ops; 151 152 /* 153 * Configure a polling function. 154 * When a trigger event with this polling function connected 155 * occurs, this function is run. Typically this grabs data 156 * from the device. 157 * 158 * NULL for the bottom half. This is normally implemented only if we 159 * either want to ping a capture now pin (no sleeping) or grab 160 * a timestamp as close as possible to a data ready trigger firing. 161 * 162 * IRQF_ONESHOT ensures irqs are masked such that only one instance 163 * of the handler can run at a time. 164 * 165 * "iio_simple_dummy_consumer%d" formatting string for the irq 'name' 166 * as seen under /proc/interrupts. Remaining parameters as per printk. 167 */ 168 indio_dev->pollfunc = iio_alloc_pollfunc(NULL, 169 &iio_simple_dummy_trigger_h, 170 IRQF_ONESHOT, 171 indio_dev, 172 "iio_simple_dummy_consumer%d", 173 indio_dev->id); 174 175 if (indio_dev->pollfunc == NULL) { 176 ret = -ENOMEM; 177 goto error_free_buffer; 178 } 179 180 /* 181 * Notify the core that this device is capable of buffered capture 182 * driven by a trigger. 183 */ 184 indio_dev->modes |= INDIO_BUFFER_TRIGGERED; 185 186 ret = iio_buffer_register(indio_dev, channels, num_channels); 187 if (ret) 188 goto error_dealloc_pollfunc; 189 190 return 0; 191 192error_dealloc_pollfunc: 193 iio_dealloc_pollfunc(indio_dev->pollfunc); 194error_free_buffer: 195 iio_kfifo_free(indio_dev->buffer); 196error_ret: 197 return ret; 198 199} 200 201/** 202 * iio_simple_dummy_unconfigure_buffer() - release buffer resources 203 * @indo_dev: device instance state 204 */ 205void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev) 206{ 207 iio_buffer_unregister(indio_dev); 208 iio_dealloc_pollfunc(indio_dev->pollfunc); 209 iio_kfifo_free(indio_dev->buffer); 210} 211