blob: b6cd67ad55d177f80898c6b1d4c34c766b6300d2 [file] [log] [blame]
H Hartley Sweetenea082fb2013-01-09 13:25:06 -07001/*
2 * comedi_buf.c
3 *
4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "comedidev.h"
23#include "comedi_internal.h"
24
H Hartley Sweeten6bd76452013-01-09 13:26:26 -070025#ifdef PAGE_KERNEL_NOCACHE
26#define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
27#else
28#define COMEDI_PAGE_PROTECTION PAGE_KERNEL
29#endif
30
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070031static void __comedi_buf_free(struct comedi_device *dev,
32 struct comedi_subdevice *s,
33 unsigned n_pages)
34{
35 struct comedi_async *async = s->async;
36 struct comedi_buf_page *buf;
37 unsigned i;
38
39 if (async->prealloc_buf) {
40 vunmap(async->prealloc_buf);
41 async->prealloc_buf = NULL;
42 async->prealloc_bufsz = 0;
43 }
44
45 if (!async->buf_page_list)
46 return;
47
48 for (i = 0; i < n_pages; ++i) {
49 buf = &async->buf_page_list[i];
50 if (buf->virt_addr) {
51 clear_bit(PG_reserved,
52 &(virt_to_page(buf->virt_addr)->flags));
53 if (s->async_dma_dir != DMA_NONE) {
Ian Abbott4efc4bb2013-05-10 14:07:15 +010054#ifdef CONFIG_HAS_DMA
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070055 dma_free_coherent(dev->hw_dev,
56 PAGE_SIZE,
57 buf->virt_addr,
58 buf->dma_addr);
Ian Abbott4efc4bb2013-05-10 14:07:15 +010059#endif
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070060 } else {
61 free_page((unsigned long)buf->virt_addr);
62 }
63 }
64 }
65 vfree(async->buf_page_list);
66 async->buf_page_list = NULL;
67 async->n_buf_pages = 0;
68}
69
H Hartley Sweeten6bd76452013-01-09 13:26:26 -070070static void __comedi_buf_alloc(struct comedi_device *dev,
71 struct comedi_subdevice *s,
72 unsigned n_pages)
73{
74 struct comedi_async *async = s->async;
75 struct page **pages = NULL;
76 struct comedi_buf_page *buf;
77 unsigned i;
78
79 async->buf_page_list = vzalloc(sizeof(*buf) * n_pages);
80 if (async->buf_page_list)
81 pages = vmalloc(sizeof(struct page *) * n_pages);
82
83 if (!pages)
84 return;
85
86 for (i = 0; i < n_pages; i++) {
87 buf = &async->buf_page_list[i];
88 if (s->async_dma_dir != DMA_NONE)
Ian Abbott4efc4bb2013-05-10 14:07:15 +010089#ifdef CONFIG_HAS_DMA
H Hartley Sweeten6bd76452013-01-09 13:26:26 -070090 buf->virt_addr = dma_alloc_coherent(dev->hw_dev,
91 PAGE_SIZE,
92 &buf->dma_addr,
93 GFP_KERNEL |
94 __GFP_COMP);
Ian Abbott4efc4bb2013-05-10 14:07:15 +010095#else
96 break;
97#endif
H Hartley Sweeten6bd76452013-01-09 13:26:26 -070098 else
99 buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
100 if (!buf->virt_addr)
101 break;
102
103 set_bit(PG_reserved, &(virt_to_page(buf->virt_addr)->flags));
104
105 pages[i] = virt_to_page(buf->virt_addr);
106 }
107
108 /* vmap the prealloc_buf if all the pages were allocated */
109 if (i == n_pages)
110 async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
111 COMEDI_PAGE_PROTECTION);
112
113 vfree(pages);
114}
115
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700116int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
117 unsigned long new_size)
118{
119 struct comedi_async *async = s->async;
120
121 /* Round up new_size to multiple of PAGE_SIZE */
122 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
123
124 /* if no change is required, do nothing */
125 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
126 return 0;
127
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700128 /* deallocate old buffer */
129 __comedi_buf_free(dev, s, async->n_buf_pages);
130
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700131 /* allocate new buffer */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700132 if (new_size) {
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700133 unsigned n_pages = new_size >> PAGE_SHIFT;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700134
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700135 __comedi_buf_alloc(dev, s, n_pages);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700136
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700137 if (!async->prealloc_buf) {
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700138 /* allocation failed */
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700139 __comedi_buf_free(dev, s, n_pages);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700140 return -ENOMEM;
141 }
142 async->n_buf_pages = n_pages;
143 }
144 async->prealloc_bufsz = new_size;
145
146 return 0;
147}
148
H Hartley Sweeten61c9fb02013-01-09 13:27:07 -0700149void comedi_buf_reset(struct comedi_async *async)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700150{
151 async->buf_write_alloc_count = 0;
152 async->buf_write_count = 0;
153 async->buf_read_alloc_count = 0;
154 async->buf_read_count = 0;
155
156 async->buf_write_ptr = 0;
157 async->buf_read_ptr = 0;
158
159 async->cur_chan = 0;
160 async->scan_progress = 0;
161 async->munge_chan = 0;
162 async->munge_count = 0;
163 async->munge_ptr = 0;
164
165 async->events = 0;
166}
167
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700168static unsigned int comedi_buf_write_n_available(struct comedi_async *async)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700169{
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700170 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700171
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700172 return free_end - async->buf_write_alloc_count;
173}
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700174
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700175static unsigned int __comedi_buf_write_alloc(struct comedi_async *async,
176 unsigned int nbytes,
177 int strict)
178{
179 unsigned int available = comedi_buf_write_n_available(async);
180
181 if (nbytes > available)
182 nbytes = strict ? 0 : available;
183
184 async->buf_write_alloc_count += nbytes;
185
186 /*
187 * ensure the async buffer 'counts' are read and updated
188 * before we write data to the write-alloc'ed buffer space
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700189 */
190 smp_mb();
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700191
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700192 return nbytes;
193}
194
195/* allocates chunk for the writer from free buffer space */
196unsigned int comedi_buf_write_alloc(struct comedi_async *async,
197 unsigned int nbytes)
198{
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700199 return __comedi_buf_write_alloc(async, nbytes, 0);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700200}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700201EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700202
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700203/*
204 * munging is applied to data by core as it passes between user
205 * and kernel space
206 */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700207static unsigned int comedi_buf_munge(struct comedi_async *async,
208 unsigned int num_bytes)
209{
210 struct comedi_subdevice *s = async->subdevice;
211 unsigned int count = 0;
212 const unsigned num_sample_bytes = bytes_per_sample(s);
213
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700214 if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700215 async->munge_count += num_bytes;
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700216 count = num_bytes;
217 } else {
218 /* don't munge partial samples */
219 num_bytes -= num_bytes % num_sample_bytes;
220 while (count < num_bytes) {
221 int block_size = num_bytes - count;
222 unsigned int buf_end;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700223
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700224 buf_end = async->prealloc_bufsz - async->munge_ptr;
225 if (block_size > buf_end)
226 block_size = buf_end;
227
228 s->munge(s->device, s,
229 async->prealloc_buf + async->munge_ptr,
230 block_size, async->munge_chan);
231
232 /*
233 * ensure data is munged in buffer before the
234 * async buffer munge_count is incremented
235 */
236 smp_wmb();
237
238 async->munge_chan += block_size / num_sample_bytes;
239 async->munge_chan %= async->cmd.chanlist_len;
240 async->munge_count += block_size;
241 async->munge_ptr += block_size;
242 async->munge_ptr %= async->prealloc_bufsz;
243 count += block_size;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700244 }
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700245 }
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700246
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700247 return count;
248}
249
H Hartley Sweeten8bd650f2013-01-09 13:32:18 -0700250unsigned int comedi_buf_write_n_allocated(struct comedi_async *async)
251{
252 return async->buf_write_alloc_count - async->buf_write_count;
253}
254
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700255/* transfers a chunk from writer to filled buffer space */
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700256unsigned int comedi_buf_write_free(struct comedi_async *async,
257 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700258{
H Hartley Sweetend21af4c2013-01-09 13:29:53 -0700259 unsigned int allocated = comedi_buf_write_n_allocated(async);
260
H Hartley Sweeten6166ce82013-01-10 10:37:56 -0700261 if (nbytes > allocated)
H Hartley Sweetend21af4c2013-01-09 13:29:53 -0700262 nbytes = allocated;
H Hartley Sweeten6166ce82013-01-10 10:37:56 -0700263
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700264 async->buf_write_count += nbytes;
265 async->buf_write_ptr += nbytes;
266 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
267 if (async->buf_write_ptr >= async->prealloc_bufsz)
268 async->buf_write_ptr %= async->prealloc_bufsz;
269
270 return nbytes;
271}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700272EXPORT_SYMBOL_GPL(comedi_buf_write_free);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700273
274unsigned int comedi_buf_read_n_available(struct comedi_async *async)
275{
276 unsigned num_bytes;
277
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700278 if (!async)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700279 return 0;
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700280
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700281 num_bytes = async->munge_count - async->buf_read_count;
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700282
283 /*
284 * ensure the async buffer 'counts' are read before we
285 * attempt to read data from the buffer
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700286 */
287 smp_rmb();
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700288
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700289 return num_bytes;
290}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700291EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700292
293/* allocates a chunk for the reader from filled (and munged) buffer space */
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700294unsigned int comedi_buf_read_alloc(struct comedi_async *async,
295 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700296{
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700297 unsigned int available;
298
299 available = async->munge_count - async->buf_read_alloc_count;
300 if (nbytes > available)
301 nbytes = available;
302
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700303 async->buf_read_alloc_count += nbytes;
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700304
305 /*
306 * ensure the async buffer 'counts' are read before we
307 * attempt to read data from the read-alloc'ed buffer space
308 */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700309 smp_rmb();
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700310
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700311 return nbytes;
312}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700313EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700314
H Hartley Sweeten5b2b64b2013-01-09 13:31:46 -0700315static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
316{
317 return async->buf_read_alloc_count - async->buf_read_count;
318}
319
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700320/* transfers control of a chunk from reader to free buffer space */
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700321unsigned int comedi_buf_read_free(struct comedi_async *async,
322 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700323{
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700324 unsigned int allocated;
325
326 /*
327 * ensure data has been read out of buffer before
328 * the async read count is incremented
329 */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700330 smp_mb();
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700331
332 allocated = comedi_buf_read_n_allocated(async);
H Hartley Sweeten215040e2013-01-10 10:38:20 -0700333 if (nbytes > allocated)
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700334 nbytes = allocated;
H Hartley Sweeten215040e2013-01-10 10:38:20 -0700335
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700336 async->buf_read_count += nbytes;
337 async->buf_read_ptr += nbytes;
338 async->buf_read_ptr %= async->prealloc_bufsz;
339 return nbytes;
340}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700341EXPORT_SYMBOL_GPL(comedi_buf_read_free);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700342
343int comedi_buf_put(struct comedi_async *async, short x)
344{
H Hartley Sweeten47181ea2013-01-09 13:28:20 -0700345 unsigned int n = __comedi_buf_write_alloc(async, sizeof(short), 1);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700346
347 if (n < sizeof(short)) {
348 async->events |= COMEDI_CB_ERROR;
349 return 0;
350 }
351 *(short *)(async->prealloc_buf + async->buf_write_ptr) = x;
352 comedi_buf_write_free(async, sizeof(short));
353 return 1;
354}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700355EXPORT_SYMBOL_GPL(comedi_buf_put);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700356
357int comedi_buf_get(struct comedi_async *async, short *x)
358{
359 unsigned int n = comedi_buf_read_n_available(async);
360
361 if (n < sizeof(short))
362 return 0;
363 comedi_buf_read_alloc(async, sizeof(short));
364 *x = *(short *)(async->prealloc_buf + async->buf_read_ptr);
365 comedi_buf_read_free(async, sizeof(short));
366 return 1;
367}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700368EXPORT_SYMBOL_GPL(comedi_buf_get);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700369
370void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
371 const void *data, unsigned int num_bytes)
372{
373 unsigned int write_ptr = async->buf_write_ptr + offset;
374
375 if (write_ptr >= async->prealloc_bufsz)
376 write_ptr %= async->prealloc_bufsz;
377
378 while (num_bytes) {
379 unsigned int block_size;
380
381 if (write_ptr + num_bytes > async->prealloc_bufsz)
382 block_size = async->prealloc_bufsz - write_ptr;
383 else
384 block_size = num_bytes;
385
386 memcpy(async->prealloc_buf + write_ptr, data, block_size);
387
388 data += block_size;
389 num_bytes -= block_size;
390
391 write_ptr = 0;
392 }
393}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700394EXPORT_SYMBOL_GPL(comedi_buf_memcpy_to);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700395
396void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
397 void *dest, unsigned int nbytes)
398{
399 void *src;
400 unsigned int read_ptr = async->buf_read_ptr + offset;
401
402 if (read_ptr >= async->prealloc_bufsz)
403 read_ptr %= async->prealloc_bufsz;
404
405 while (nbytes) {
406 unsigned int block_size;
407
408 src = async->prealloc_buf + read_ptr;
409
410 if (nbytes >= async->prealloc_bufsz - read_ptr)
411 block_size = async->prealloc_bufsz - read_ptr;
412 else
413 block_size = nbytes;
414
415 memcpy(dest, src, block_size);
416 nbytes -= block_size;
417 dest += block_size;
418 read_ptr = 0;
419 }
420}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700421EXPORT_SYMBOL_GPL(comedi_buf_memcpy_from);