4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
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.
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.
18 #include <linux/vmalloc.h>
19 #include <linux/slab.h>
21 #include "comedidev.h"
22 #include "comedi_internal.h"
24 #ifdef PAGE_KERNEL_NOCACHE
25 #define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
27 #define COMEDI_PAGE_PROTECTION PAGE_KERNEL
30 static void comedi_buf_map_kref_release(struct kref *kref)
32 struct comedi_buf_map *bm =
33 container_of(kref, struct comedi_buf_map, refcount);
34 struct comedi_buf_page *buf;
38 for (i = 0; i < bm->n_pages; i++) {
39 buf = &bm->page_list[i];
40 clear_bit(PG_reserved,
41 &(virt_to_page(buf->virt_addr)->flags));
42 if (bm->dma_dir != DMA_NONE) {
44 dma_free_coherent(bm->dma_hw_dev,
50 free_page((unsigned long)buf->virt_addr);
55 if (bm->dma_dir != DMA_NONE)
56 put_device(bm->dma_hw_dev);
60 static void __comedi_buf_free(struct comedi_device *dev,
61 struct comedi_subdevice *s)
63 struct comedi_async *async = s->async;
64 struct comedi_buf_map *bm;
67 if (async->prealloc_buf) {
68 vunmap(async->prealloc_buf);
69 async->prealloc_buf = NULL;
70 async->prealloc_bufsz = 0;
73 spin_lock_irqsave(&s->spin_lock, flags);
75 async->buf_map = NULL;
76 spin_unlock_irqrestore(&s->spin_lock, flags);
77 comedi_buf_map_put(bm);
80 static void __comedi_buf_alloc(struct comedi_device *dev,
81 struct comedi_subdevice *s,
84 struct comedi_async *async = s->async;
85 struct page **pages = NULL;
86 struct comedi_buf_map *bm;
87 struct comedi_buf_page *buf;
91 if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
92 dev_err(dev->class_dev,
93 "dma buffer allocation not supported\n");
97 bm = kzalloc(sizeof(*async->buf_map), GFP_KERNEL);
101 kref_init(&bm->refcount);
102 spin_lock_irqsave(&s->spin_lock, flags);
104 spin_unlock_irqrestore(&s->spin_lock, flags);
105 bm->dma_dir = s->async_dma_dir;
106 if (bm->dma_dir != DMA_NONE)
107 /* Need ref to hardware device to free buffer later. */
108 bm->dma_hw_dev = get_device(dev->hw_dev);
110 bm->page_list = vzalloc(sizeof(*buf) * n_pages);
112 pages = vmalloc(sizeof(struct page *) * n_pages);
117 for (i = 0; i < n_pages; i++) {
118 buf = &bm->page_list[i];
119 if (bm->dma_dir != DMA_NONE)
120 #ifdef CONFIG_HAS_DMA
121 buf->virt_addr = dma_alloc_coherent(bm->dma_hw_dev,
130 buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
134 set_bit(PG_reserved, &(virt_to_page(buf->virt_addr)->flags));
136 pages[i] = virt_to_page(buf->virt_addr);
138 spin_lock_irqsave(&s->spin_lock, flags);
140 spin_unlock_irqrestore(&s->spin_lock, flags);
142 /* vmap the prealloc_buf if all the pages were allocated */
144 async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
145 COMEDI_PAGE_PROTECTION);
150 void comedi_buf_map_get(struct comedi_buf_map *bm)
153 kref_get(&bm->refcount);
156 int comedi_buf_map_put(struct comedi_buf_map *bm)
159 return kref_put(&bm->refcount, comedi_buf_map_kref_release);
163 /* returns s->async->buf_map and increments its kref refcount */
164 struct comedi_buf_map *
165 comedi_buf_map_from_subdev_get(struct comedi_subdevice *s)
167 struct comedi_async *async = s->async;
168 struct comedi_buf_map *bm = NULL;
174 spin_lock_irqsave(&s->spin_lock, flags);
176 /* only want it if buffer pages allocated */
177 if (bm && bm->n_pages)
178 comedi_buf_map_get(bm);
181 spin_unlock_irqrestore(&s->spin_lock, flags);
186 bool comedi_buf_is_mmapped(struct comedi_async *async)
188 struct comedi_buf_map *bm = async->buf_map;
190 return bm && (atomic_read(&bm->refcount.refcount) > 1);
193 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
194 unsigned long new_size)
196 struct comedi_async *async = s->async;
198 /* Round up new_size to multiple of PAGE_SIZE */
199 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
201 /* if no change is required, do nothing */
202 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
205 /* deallocate old buffer */
206 __comedi_buf_free(dev, s);
208 /* allocate new buffer */
210 unsigned n_pages = new_size >> PAGE_SHIFT;
212 __comedi_buf_alloc(dev, s, n_pages);
214 if (!async->prealloc_buf) {
215 /* allocation failed */
216 __comedi_buf_free(dev, s);
220 async->prealloc_bufsz = new_size;
225 void comedi_buf_reset(struct comedi_async *async)
227 async->buf_write_alloc_count = 0;
228 async->buf_write_count = 0;
229 async->buf_read_alloc_count = 0;
230 async->buf_read_count = 0;
232 async->buf_write_ptr = 0;
233 async->buf_read_ptr = 0;
236 async->scan_progress = 0;
237 async->munge_chan = 0;
238 async->munge_count = 0;
239 async->munge_ptr = 0;
244 static unsigned int comedi_buf_write_n_available(struct comedi_async *async)
246 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
248 return free_end - async->buf_write_alloc_count;
251 static unsigned int __comedi_buf_write_alloc(struct comedi_async *async,
255 unsigned int available = comedi_buf_write_n_available(async);
257 if (nbytes > available)
258 nbytes = strict ? 0 : available;
260 async->buf_write_alloc_count += nbytes;
263 * ensure the async buffer 'counts' are read and updated
264 * before we write data to the write-alloc'ed buffer space
271 /* allocates chunk for the writer from free buffer space */
272 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
275 return __comedi_buf_write_alloc(async, nbytes, 0);
277 EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
280 * munging is applied to data by core as it passes between user
283 static unsigned int comedi_buf_munge(struct comedi_async *async,
284 unsigned int num_bytes)
286 struct comedi_subdevice *s = async->subdevice;
287 unsigned int count = 0;
288 const unsigned num_sample_bytes = bytes_per_sample(s);
290 if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
291 async->munge_count += num_bytes;
294 /* don't munge partial samples */
295 num_bytes -= num_bytes % num_sample_bytes;
296 while (count < num_bytes) {
297 int block_size = num_bytes - count;
298 unsigned int buf_end;
300 buf_end = async->prealloc_bufsz - async->munge_ptr;
301 if (block_size > buf_end)
302 block_size = buf_end;
304 s->munge(s->device, s,
305 async->prealloc_buf + async->munge_ptr,
306 block_size, async->munge_chan);
309 * ensure data is munged in buffer before the
310 * async buffer munge_count is incremented
314 async->munge_chan += block_size / num_sample_bytes;
315 async->munge_chan %= async->cmd.chanlist_len;
316 async->munge_count += block_size;
317 async->munge_ptr += block_size;
318 async->munge_ptr %= async->prealloc_bufsz;
326 unsigned int comedi_buf_write_n_allocated(struct comedi_async *async)
328 return async->buf_write_alloc_count - async->buf_write_count;
331 /* transfers a chunk from writer to filled buffer space */
332 unsigned int comedi_buf_write_free(struct comedi_async *async,
335 unsigned int allocated = comedi_buf_write_n_allocated(async);
337 if (nbytes > allocated)
340 async->buf_write_count += nbytes;
341 async->buf_write_ptr += nbytes;
342 comedi_buf_munge(async, async->buf_write_count - async->munge_count);
343 if (async->buf_write_ptr >= async->prealloc_bufsz)
344 async->buf_write_ptr %= async->prealloc_bufsz;
348 EXPORT_SYMBOL_GPL(comedi_buf_write_free);
350 unsigned int comedi_buf_read_n_available(struct comedi_async *async)
357 num_bytes = async->munge_count - async->buf_read_count;
360 * ensure the async buffer 'counts' are read before we
361 * attempt to read data from the buffer
367 EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
369 /* allocates a chunk for the reader from filled (and munged) buffer space */
370 unsigned int comedi_buf_read_alloc(struct comedi_async *async,
373 unsigned int available;
375 available = async->munge_count - async->buf_read_alloc_count;
376 if (nbytes > available)
379 async->buf_read_alloc_count += nbytes;
382 * ensure the async buffer 'counts' are read before we
383 * attempt to read data from the read-alloc'ed buffer space
389 EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
391 static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
393 return async->buf_read_alloc_count - async->buf_read_count;
396 /* transfers control of a chunk from reader to free buffer space */
397 unsigned int comedi_buf_read_free(struct comedi_async *async,
400 unsigned int allocated;
403 * ensure data has been read out of buffer before
404 * the async read count is incremented
408 allocated = comedi_buf_read_n_allocated(async);
409 if (nbytes > allocated)
412 async->buf_read_count += nbytes;
413 async->buf_read_ptr += nbytes;
414 async->buf_read_ptr %= async->prealloc_bufsz;
417 EXPORT_SYMBOL_GPL(comedi_buf_read_free);
419 int comedi_buf_put(struct comedi_async *async, unsigned short x)
421 unsigned int n = __comedi_buf_write_alloc(async, sizeof(short), 1);
423 if (n < sizeof(short)) {
424 async->events |= COMEDI_CB_ERROR;
427 *(unsigned short *)(async->prealloc_buf + async->buf_write_ptr) = x;
428 comedi_buf_write_free(async, sizeof(short));
431 EXPORT_SYMBOL_GPL(comedi_buf_put);
433 int comedi_buf_get(struct comedi_async *async, unsigned short *x)
435 unsigned int n = comedi_buf_read_n_available(async);
437 if (n < sizeof(short))
439 comedi_buf_read_alloc(async, sizeof(short));
440 *x = *(unsigned short *)(async->prealloc_buf + async->buf_read_ptr);
441 comedi_buf_read_free(async, sizeof(short));
444 EXPORT_SYMBOL_GPL(comedi_buf_get);
446 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
447 const void *data, unsigned int num_bytes)
449 unsigned int write_ptr = async->buf_write_ptr + offset;
451 if (write_ptr >= async->prealloc_bufsz)
452 write_ptr %= async->prealloc_bufsz;
455 unsigned int block_size;
457 if (write_ptr + num_bytes > async->prealloc_bufsz)
458 block_size = async->prealloc_bufsz - write_ptr;
460 block_size = num_bytes;
462 memcpy(async->prealloc_buf + write_ptr, data, block_size);
465 num_bytes -= block_size;
470 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_to);
472 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
473 void *dest, unsigned int nbytes)
476 unsigned int read_ptr = async->buf_read_ptr + offset;
478 if (read_ptr >= async->prealloc_bufsz)
479 read_ptr %= async->prealloc_bufsz;
482 unsigned int block_size;
484 src = async->prealloc_buf + read_ptr;
486 if (nbytes >= async->prealloc_bufsz - read_ptr)
487 block_size = async->prealloc_bufsz - read_ptr;
491 memcpy(dest, src, block_size);
492 nbytes -= block_size;
497 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_from);