staging: iio: adc: max1363: prevent buffer overflow
[deliverable/linux.git] / drivers / staging / iio / adc / max1363_ring.c
CommitLineData
0a1231df
JC
1/*
2 * Copyright (C) 2008 Jonathan Cameron
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * max1363_ring.c
9 */
10
11#include <linux/interrupt.h>
12#include <linux/gpio.h>
13#include <linux/workqueue.h>
14#include <linux/device.h>
5a0e3ad6 15#include <linux/slab.h>
0a1231df
JC
16#include <linux/kernel.h>
17#include <linux/sysfs.h>
18#include <linux/list.h>
19#include <linux/i2c.h>
82020b0e 20#include <linux/bitops.h>
0a1231df
JC
21
22#include "../iio.h"
23#include "../ring_generic.h"
24#include "../ring_sw.h"
25#include "../trigger.h"
26#include "../sysfs.h"
27
28#include "max1363.h"
29
82020b0e
JC
30/* Todo: test this */
31int max1363_single_channel_from_ring(long mask, struct max1363_state *st)
0a1231df 32{
bf32963c 33 struct iio_ring_buffer *ring = st->indio_dev->ring;
82020b0e
JC
34 unsigned long numvals;
35 int count = 0, ret;
36 u8 *ring_data;
37 if (!(st->current_mode->modemask & mask)) {
38 ret = -EBUSY;
39 goto error_ret;
40 }
0a1231df 41
e1517c00 42 ring_data = kmalloc(ring->access.get_bytes_per_datum(ring), GFP_KERNEL);
0a1231df
JC
43 if (ring_data == NULL) {
44 ret = -ENOMEM;
45 goto error_ret;
46 }
bf32963c 47 ret = ring->access.read_last(ring, ring_data);
0a1231df
JC
48 if (ret)
49 goto error_free_ring_data;
82020b0e
JC
50 /* Need a count of channels prior to this one */
51 mask >>= 1;
52 while (mask) {
81b77f94 53 if (mask & st->current_mode->modemask)
82020b0e
JC
54 count++;
55 mask >>= 1;
56 }
3bf877c1 57 if (st->chip_info->bits != 8)
81b77f94 58 ret = ((int)(ring_data[count*2 + 0] & 0x0F) << 8)
3bf877c1
JC
59 + (int)(ring_data[count*2 + 1]);
60 else
81b77f94 61 ret = ring_data[count];
0a1231df
JC
62
63error_free_ring_data:
64 kfree(ring_data);
65error_ret:
66 return ret;
67}
68
69/**
c40ab874 70 * max1363_ring_preenable() - setup the parameters of the ring before enabling
0a1231df
JC
71 *
72 * The complex nature of the setting of the nuber of bytes per datum is due
73 * to this driver currently ensuring that the timestamp is stored at an 8
74 * byte boundary.
75 **/
76static int max1363_ring_preenable(struct iio_dev *indio_dev)
77{
78 struct max1363_state *st = indio_dev->dev_data;
bf32963c 79 struct iio_ring_buffer *ring = indio_dev->ring;
0a1231df 80 size_t d_size;
82020b0e
JC
81 unsigned long numvals;
82
83 /*
84 * Need to figure out the current mode based upon the requested
85 * scan mask in iio_dev
86 */
bf32963c 87 st->current_mode = max1363_match_mode(ring->scan_mask,
82020b0e
JC
88 st->chip_info);
89 if (!st->current_mode)
90 return -EINVAL;
91
92 max1363_set_scan_mode(st);
0a1231df 93
82020b0e 94 numvals = hweight_long(st->current_mode->modemask);
bf32963c 95 if (ring->access.set_bytes_per_datum) {
3bf877c1
JC
96 if (st->chip_info->bits != 8)
97 d_size = numvals*2 + sizeof(s64);
98 else
99 d_size = numvals + sizeof(s64);
0a1231df
JC
100 if (d_size % 8)
101 d_size += 8 - (d_size % 8);
bf32963c 102 ring->access.set_bytes_per_datum(ring, d_size);
0a1231df
JC
103 }
104
105 return 0;
106}
107
0a1231df
JC
108
109/**
c40ab874 110 * max1363_poll_func_th() - th of trigger launched polling to ring buffer
0a1231df
JC
111 *
112 * As sampling only occurs on i2c comms occuring, leave timestamping until
113 * then. Some triggers will generate their own time stamp. Currently
114 * there is no way of notifying them when no one cares.
115 **/
7b2c33b1 116static void max1363_poll_func_th(struct iio_dev *indio_dev, s64 time)
0a1231df
JC
117{
118 struct max1363_state *st = indio_dev->dev_data;
119
120 schedule_work(&st->poll_work);
121
122 return;
123}
124/**
c40ab874 125 * max1363_poll_bh_to_ring() - bh of trigger launched polling to ring buffer
0a1231df
JC
126 * @work_s: the work struct through which this was scheduled
127 *
128 * Currently there is no option in this driver to disable the saving of
129 * timestamps within the ring.
130 * I think the one copy of this at a time was to avoid problems if the
131 * trigger was set far too high and the reads then locked up the computer.
132 **/
133static void max1363_poll_bh_to_ring(struct work_struct *work_s)
134{
135 struct max1363_state *st = container_of(work_s, struct max1363_state,
136 poll_work);
137 struct iio_dev *indio_dev = st->indio_dev;
bf32963c 138 struct iio_sw_ring_buffer *sw_ring = iio_to_sw_ring(indio_dev->ring);
0a1231df
JC
139 s64 time_ns;
140 __u8 *rxbuf;
141 int b_sent;
142 size_t d_size;
82020b0e 143 unsigned long numvals = hweight_long(st->current_mode->modemask);
0a1231df
JC
144
145 /* Ensure the timestamp is 8 byte aligned */
3bf877c1
JC
146 if (st->chip_info->bits != 8)
147 d_size = numvals*2 + sizeof(s64);
148 else
149 d_size = numvals + sizeof(s64);
0a1231df
JC
150 if (d_size % sizeof(s64))
151 d_size += sizeof(s64) - (d_size % sizeof(s64));
152
153 /* Ensure only one copy of this function running at a time */
154 if (atomic_inc_return(&st->protect_ring) > 1)
155 return;
156
157 /* Monitor mode prevents reading. Whilst not currently implemented
158 * might as well have this test in here in the meantime as it does
159 * no harm.
160 */
82020b0e 161 if (numvals == 0)
0a1231df
JC
162 return;
163
164 rxbuf = kmalloc(d_size, GFP_KERNEL);
165 if (rxbuf == NULL)
166 return;
3bf877c1
JC
167 if (st->chip_info->bits != 8)
168 b_sent = i2c_master_recv(st->client, rxbuf, numvals*2);
169 else
170 b_sent = i2c_master_recv(st->client, rxbuf, numvals);
0a1231df
JC
171 if (b_sent < 0)
172 goto done;
173
174 time_ns = iio_get_time_ns();
175
176 memcpy(rxbuf + d_size - sizeof(s64), &time_ns, sizeof(time_ns));
177
bf32963c 178 indio_dev->ring->access.store_to(&sw_ring->buf, rxbuf, time_ns);
0a1231df
JC
179done:
180 kfree(rxbuf);
181 atomic_dec(&st->protect_ring);
182}
183
184
185int max1363_register_ring_funcs_and_init(struct iio_dev *indio_dev)
186{
187 struct max1363_state *st = indio_dev->dev_data;
188 int ret = 0;
189
190 indio_dev->ring = iio_sw_rb_allocate(indio_dev);
191 if (!indio_dev->ring) {
192 ret = -ENOMEM;
193 goto error_ret;
194 }
195 /* Effectively select the ring buffer implementation */
bf32963c 196 iio_ring_sw_register_funcs(&indio_dev->ring->access);
15744090
JC
197 ret = iio_alloc_pollfunc(indio_dev, NULL, &max1363_poll_func_th);
198 if (ret)
0a1231df 199 goto error_deallocate_sw_rb;
0a1231df
JC
200
201 /* Ring buffer functions - here trigger setup related */
bf32963c 202 indio_dev->ring->scan_el_attrs = st->chip_info->scan_attrs;
c3db00cc 203 indio_dev->ring->postenable = &iio_triggered_ring_postenable;
0a1231df 204 indio_dev->ring->preenable = &max1363_ring_preenable;
c3db00cc 205 indio_dev->ring->predisable = &iio_triggered_ring_predisable;
0a1231df
JC
206 INIT_WORK(&st->poll_work, &max1363_poll_bh_to_ring);
207
208 /* Flag that polled ring buffering is possible */
209 indio_dev->modes |= INDIO_RING_TRIGGERED;
210 return 0;
211error_deallocate_sw_rb:
212 iio_sw_rb_free(indio_dev->ring);
213error_ret:
214 return ret;
215}
216
217void max1363_ring_cleanup(struct iio_dev *indio_dev)
218{
219 /* ensure that the trigger has been detached */
220 if (indio_dev->trig) {
221 iio_put_trigger(indio_dev->trig);
222 iio_trigger_dettach_poll_func(indio_dev->trig,
223 indio_dev->pollfunc);
224 }
225 kfree(indio_dev->pollfunc);
226 iio_sw_rb_free(indio_dev->ring);
227}
This page took 0.131368 seconds and 5 git commands to generate.