Lines Matching +full:async +full:- +full:enum
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() local
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()
74 comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir, in comedi_buf_map_alloc()
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() local
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() local
234 if (!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() local
261 lockdep_assert_held(&dev->mutex); 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() local
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() local
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() local
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
342 struct comedi_async *async = s->async; in comedi_buf_write_alloc() local
348 async->buf_write_alloc_count += nbytes; in comedi_buf_write_alloc()
351 * ensure the async buffer 'counts' are read and updated 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() local
371 if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) { in comedi_buf_munge()
372 async->munge_count += num_bytes; 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()
392 * async buffer munge_count is incremented 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() local
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
432 struct comedi_async *async = s->async; in comedi_buf_write_free() local
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() local
464 if (!async) in comedi_buf_read_n_available()
467 num_bytes = async->munge_count - async->buf_read_count; in comedi_buf_read_n_available()
470 * ensure the async buffer 'counts' are read before we in comedi_buf_read_n_available()
480 * comedi_buf_read_alloc() - Reserve buffer space for reading
497 struct comedi_async *async = s->async; in comedi_buf_read_alloc() local
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()
507 * ensure the async buffer 'counts' are read before we in comedi_buf_read_alloc()
508 * attempt to read data from the read-alloc'ed buffer space in comedi_buf_read_alloc()
516 static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async) in comedi_buf_read_n_allocated() argument
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
537 struct comedi_async *async = s->async; in comedi_buf_read_free() local
542 * the async read count is incremented in comedi_buf_read_free()
546 allocated = comedi_buf_read_n_allocated(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() local
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() local
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
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
687 s->async->events |= COMEDI_CB_BLOCK; in comedi_buf_read_samples()