Lines Matching +full:up +full:- +full:samples
1 // SPDX-License-Identifier: GPL-2.0+
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
28 if (bm->page_list) { in comedi_buf_map_kref_release()
29 if (bm->dma_dir != DMA_NONE) { in comedi_buf_map_kref_release()
34 buf = &bm->page_list[0]; in comedi_buf_map_kref_release()
35 dma_free_coherent(bm->dma_hw_dev, in comedi_buf_map_kref_release()
36 PAGE_SIZE * bm->n_pages, in comedi_buf_map_kref_release()
37 buf->virt_addr, buf->dma_addr); in comedi_buf_map_kref_release()
39 for (i = 0; i < bm->n_pages; i++) { in comedi_buf_map_kref_release()
40 buf = &bm->page_list[i]; in comedi_buf_map_kref_release()
41 ClearPageReserved(virt_to_page(buf->virt_addr)); in comedi_buf_map_kref_release()
42 free_page((unsigned long)buf->virt_addr); in comedi_buf_map_kref_release()
45 vfree(bm->page_list); in comedi_buf_map_kref_release()
47 if (bm->dma_dir != DMA_NONE) in comedi_buf_map_kref_release()
48 put_device(bm->dma_hw_dev); in comedi_buf_map_kref_release()
55 struct comedi_async *async = s->async; in __comedi_buf_free()
59 if (async->prealloc_buf) { in __comedi_buf_free()
60 if (s->async_dma_dir == DMA_NONE) in __comedi_buf_free()
61 vunmap(async->prealloc_buf); in __comedi_buf_free()
62 async->prealloc_buf = NULL; in __comedi_buf_free()
63 async->prealloc_bufsz = 0; in __comedi_buf_free()
66 spin_lock_irqsave(&s->spin_lock, flags); in __comedi_buf_free()
67 bm = async->buf_map; in __comedi_buf_free()
68 async->buf_map = NULL; in __comedi_buf_free()
69 spin_unlock_irqrestore(&s->spin_lock, flags); in __comedi_buf_free()
85 kref_init(&bm->refcount); in comedi_buf_map_alloc()
86 bm->dma_dir = dma_dir; in comedi_buf_map_alloc()
87 if (bm->dma_dir != DMA_NONE) { in comedi_buf_map_alloc()
89 bm->dma_hw_dev = get_device(dev->hw_dev); in comedi_buf_map_alloc()
92 bm->page_list = vzalloc(sizeof(*buf) * n_pages); in comedi_buf_map_alloc()
93 if (!bm->page_list) in comedi_buf_map_alloc()
96 if (bm->dma_dir != DMA_NONE) { in comedi_buf_map_alloc()
104 virt_addr = dma_alloc_coherent(bm->dma_hw_dev, in comedi_buf_map_alloc()
111 buf = &bm->page_list[i]; in comedi_buf_map_alloc()
112 buf->virt_addr = virt_addr + (i << PAGE_SHIFT); in comedi_buf_map_alloc()
113 buf->dma_addr = dma_addr + (i << PAGE_SHIFT); in comedi_buf_map_alloc()
116 bm->n_pages = i; in comedi_buf_map_alloc()
119 buf = &bm->page_list[i]; in comedi_buf_map_alloc()
120 buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL); in comedi_buf_map_alloc()
121 if (!buf->virt_addr) in comedi_buf_map_alloc()
124 SetPageReserved(virt_to_page(buf->virt_addr)); in comedi_buf_map_alloc()
127 bm->n_pages = i; in comedi_buf_map_alloc()
143 struct comedi_async *async = s->async; in __comedi_buf_alloc()
150 if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) { in __comedi_buf_alloc()
151 dev_err(dev->class_dev, in __comedi_buf_alloc()
156 bm = comedi_buf_map_alloc(dev, s->async_dma_dir, n_pages); in __comedi_buf_alloc()
160 spin_lock_irqsave(&s->spin_lock, flags); in __comedi_buf_alloc()
161 async->buf_map = bm; in __comedi_buf_alloc()
162 spin_unlock_irqrestore(&s->spin_lock, flags); in __comedi_buf_alloc()
164 if (bm->dma_dir != DMA_NONE) { in __comedi_buf_alloc()
169 buf = &bm->page_list[0]; in __comedi_buf_alloc()
170 async->prealloc_buf = buf->virt_addr; in __comedi_buf_alloc()
177 buf = &bm->page_list[i]; in __comedi_buf_alloc()
178 pages[i] = virt_to_page(buf->virt_addr); in __comedi_buf_alloc()
182 async->prealloc_buf = vmap(pages, n_pages, VM_MAP, in __comedi_buf_alloc()
192 kref_get(&bm->refcount); in comedi_buf_map_get()
198 return kref_put(&bm->refcount, comedi_buf_map_kref_release); in comedi_buf_map_put()
210 while (done < len && pg < bm->n_pages) { in comedi_buf_map_access()
211 int l = min_t(int, len - done, PAGE_SIZE - pgoff); in comedi_buf_map_access()
212 void *b = bm->page_list[pg].virt_addr + pgoff; in comedi_buf_map_access()
226 /* returns s->async->buf_map and increments its kref refcount */
230 struct comedi_async *async = s->async; in comedi_buf_map_from_subdev_get()
237 spin_lock_irqsave(&s->spin_lock, flags); in comedi_buf_map_from_subdev_get()
238 bm = async->buf_map; in comedi_buf_map_from_subdev_get()
240 if (bm && bm->n_pages) in comedi_buf_map_from_subdev_get()
244 spin_unlock_irqrestore(&s->spin_lock, flags); in comedi_buf_map_from_subdev_get()
251 struct comedi_buf_map *bm = s->async->buf_map; in comedi_buf_is_mmapped()
253 return bm && (kref_read(&bm->refcount) > 1); in comedi_buf_is_mmapped()
259 struct comedi_async *async = s->async; in comedi_buf_alloc()
261 lockdep_assert_held(&dev->mutex); in comedi_buf_alloc()
263 /* Round up new_size to multiple of PAGE_SIZE */ in comedi_buf_alloc()
264 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK; in comedi_buf_alloc()
267 if (async->prealloc_buf && async->prealloc_bufsz == new_size) in comedi_buf_alloc()
279 if (!async->prealloc_buf) { in comedi_buf_alloc()
282 return -ENOMEM; in comedi_buf_alloc()
285 async->prealloc_bufsz = new_size; in comedi_buf_alloc()
292 struct comedi_async *async = s->async; in comedi_buf_reset()
294 async->buf_write_alloc_count = 0; in comedi_buf_reset()
295 async->buf_write_count = 0; in comedi_buf_reset()
296 async->buf_read_alloc_count = 0; in comedi_buf_reset()
297 async->buf_read_count = 0; in comedi_buf_reset()
299 async->buf_write_ptr = 0; in comedi_buf_reset()
300 async->buf_read_ptr = 0; in comedi_buf_reset()
302 async->cur_chan = 0; in comedi_buf_reset()
303 async->scans_done = 0; in comedi_buf_reset()
304 async->scan_progress = 0; in comedi_buf_reset()
305 async->munge_chan = 0; in comedi_buf_reset()
306 async->munge_count = 0; in comedi_buf_reset()
307 async->munge_ptr = 0; in comedi_buf_reset()
309 async->events = 0; in comedi_buf_reset()
314 struct comedi_async *async = s->async; in comedi_buf_write_n_unalloc()
315 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; in comedi_buf_write_n_unalloc()
317 return free_end - async->buf_write_alloc_count; in comedi_buf_write_n_unalloc()
322 struct comedi_async *async = s->async; in comedi_buf_write_n_available()
323 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz; in comedi_buf_write_n_available()
325 return free_end - async->buf_write_count; in comedi_buf_write_n_available()
329 * comedi_buf_write_alloc() - Reserve buffer space for writing
333 * Reserve up to @nbytes bytes of space to be written in the COMEDI acquisition
342 struct comedi_async *async = s->async; in comedi_buf_write_alloc()
348 async->buf_write_alloc_count += nbytes; in comedi_buf_write_alloc()
352 * before we write data to the write-alloc'ed buffer space in comedi_buf_write_alloc()
367 struct comedi_async *async = s->async; in comedi_buf_munge()
371 if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) { in comedi_buf_munge()
372 async->munge_count += num_bytes; in comedi_buf_munge()
376 /* don't munge partial samples */ in comedi_buf_munge()
377 num_bytes -= num_bytes % num_sample_bytes; in comedi_buf_munge()
379 int block_size = num_bytes - count; in comedi_buf_munge()
382 buf_end = async->prealloc_bufsz - async->munge_ptr; in comedi_buf_munge()
386 s->munge(s->device, s, in comedi_buf_munge()
387 async->prealloc_buf + async->munge_ptr, in comedi_buf_munge()
388 block_size, async->munge_chan); in comedi_buf_munge()
396 async->munge_chan += block_size / num_sample_bytes; in comedi_buf_munge()
397 async->munge_chan %= async->cmd.chanlist_len; in comedi_buf_munge()
398 async->munge_count += block_size; in comedi_buf_munge()
399 async->munge_ptr += block_size; in comedi_buf_munge()
400 async->munge_ptr %= async->prealloc_bufsz; in comedi_buf_munge()
409 struct comedi_async *async = s->async; in comedi_buf_write_n_allocated()
411 return async->buf_write_alloc_count - async->buf_write_count; in comedi_buf_write_n_allocated()
415 * comedi_buf_write_free() - Free buffer space after it is written
419 * Free up to @nbytes bytes of space previously reserved for writing in the
424 * If the samples in the freed space need to be "munged", do so here. The
432 struct comedi_async *async = s->async; in comedi_buf_write_free()
438 async->buf_write_count += nbytes; in comedi_buf_write_free()
439 async->buf_write_ptr += nbytes; in comedi_buf_write_free()
440 comedi_buf_munge(s, async->buf_write_count - async->munge_count); in comedi_buf_write_free()
441 if (async->buf_write_ptr >= async->prealloc_bufsz) in comedi_buf_write_free()
442 async->buf_write_ptr %= async->prealloc_bufsz; in comedi_buf_write_free()
449 * comedi_buf_read_n_available() - Determine amount of readable buffer space
461 struct comedi_async *async = s->async; in comedi_buf_read_n_available()
467 num_bytes = async->munge_count - async->buf_read_count; in comedi_buf_read_n_available()
480 * comedi_buf_read_alloc() - Reserve buffer space for reading
484 * Reserve up to @nbytes bytes of previously written and "munged" buffer space
497 struct comedi_async *async = s->async; in comedi_buf_read_alloc()
500 available = async->munge_count - async->buf_read_alloc_count; in comedi_buf_read_alloc()
504 async->buf_read_alloc_count += nbytes; in comedi_buf_read_alloc()
508 * attempt to read data from the read-alloc'ed buffer space in comedi_buf_read_alloc()
518 return async->buf_read_alloc_count - async->buf_read_count; in comedi_buf_read_n_allocated()
522 * comedi_buf_read_free() - Free buffer space after it has been read
526 * Free up to @nbytes bytes of buffer space previously reserved for reading in
537 struct comedi_async *async = s->async; in comedi_buf_read_free()
550 async->buf_read_count += nbytes; in comedi_buf_read_free()
551 async->buf_read_ptr += nbytes; in comedi_buf_read_free()
552 async->buf_read_ptr %= async->prealloc_bufsz; in comedi_buf_read_free()
560 struct comedi_async *async = s->async; in comedi_buf_memcpy_to()
561 unsigned int write_ptr = async->buf_write_ptr; in comedi_buf_memcpy_to()
566 if (write_ptr + num_bytes > async->prealloc_bufsz) in comedi_buf_memcpy_to()
567 block_size = async->prealloc_bufsz - write_ptr; in comedi_buf_memcpy_to()
571 memcpy(async->prealloc_buf + write_ptr, data, block_size); in comedi_buf_memcpy_to()
574 num_bytes -= block_size; in comedi_buf_memcpy_to()
584 struct comedi_async *async = s->async; in comedi_buf_memcpy_from()
585 unsigned int read_ptr = async->buf_read_ptr; in comedi_buf_memcpy_from()
590 src = async->prealloc_buf + read_ptr; in comedi_buf_memcpy_from()
592 if (nbytes >= async->prealloc_bufsz - read_ptr) in comedi_buf_memcpy_from()
593 block_size = async->prealloc_bufsz - read_ptr; in comedi_buf_memcpy_from()
598 nbytes -= block_size; in comedi_buf_memcpy_from()
605 * comedi_buf_write_samples() - Write sample data to COMEDI buffer
607 * @data: Pointer to source samples.
608 * @nsamples: Number of samples to write.
610 * Write up to @nsamples samples to the COMEDI acquisition data buffer
613 * number of samples, the number of samples written is limited to the number
616 * event flag if any samples are written to cause waiting tasks to be woken
628 * Make sure there is enough room in the buffer for all the samples. in comedi_buf_write_samples()
630 * buffer overrun and add the samples that fit. in comedi_buf_write_samples()
634 dev_warn(s->device->class_dev, "buffer overrun\n"); in comedi_buf_write_samples()
635 s->async->events |= COMEDI_CB_OVERFLOW; in comedi_buf_write_samples()
647 s->async->events |= COMEDI_CB_BLOCK; in comedi_buf_write_samples()
654 * comedi_buf_read_samples() - Read sample data from COMEDI buffer
657 * @nsamples: Maximum number of samples to read.
659 * Read up to @nsamples samples from the COMEDI acquisition data buffer
661 * scan progress. Limit the number of samples read to the number available.
662 * Set the %COMEDI_CB_BLOCK event flag if any samples are read to cause waiting
673 /* clamp nsamples to the number of full samples available */ in comedi_buf_read_samples()
687 s->async->events |= COMEDI_CB_BLOCK; in comedi_buf_read_samples()