xref: /openbmc/linux/drivers/comedi/comedi_buf.c (revision df0e68c1)
18ffdff6aSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
28ffdff6aSGreg Kroah-Hartman /*
38ffdff6aSGreg Kroah-Hartman  * comedi_buf.c
48ffdff6aSGreg Kroah-Hartman  *
58ffdff6aSGreg Kroah-Hartman  * COMEDI - Linux Control and Measurement Device Interface
68ffdff6aSGreg Kroah-Hartman  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
78ffdff6aSGreg Kroah-Hartman  * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
88ffdff6aSGreg Kroah-Hartman  */
98ffdff6aSGreg Kroah-Hartman 
108ffdff6aSGreg Kroah-Hartman #include <linux/vmalloc.h>
118ffdff6aSGreg Kroah-Hartman #include <linux/slab.h>
12*df0e68c1SIan Abbott #include <linux/comedi/comedidev.h>
138ffdff6aSGreg Kroah-Hartman #include "comedi_internal.h"
148ffdff6aSGreg Kroah-Hartman 
158ffdff6aSGreg Kroah-Hartman #ifdef PAGE_KERNEL_NOCACHE
168ffdff6aSGreg Kroah-Hartman #define COMEDI_PAGE_PROTECTION		PAGE_KERNEL_NOCACHE
178ffdff6aSGreg Kroah-Hartman #else
188ffdff6aSGreg Kroah-Hartman #define COMEDI_PAGE_PROTECTION		PAGE_KERNEL
198ffdff6aSGreg Kroah-Hartman #endif
208ffdff6aSGreg Kroah-Hartman 
comedi_buf_map_kref_release(struct kref * kref)218ffdff6aSGreg Kroah-Hartman static void comedi_buf_map_kref_release(struct kref *kref)
228ffdff6aSGreg Kroah-Hartman {
238ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm =
248ffdff6aSGreg Kroah-Hartman 		container_of(kref, struct comedi_buf_map, refcount);
258ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_page *buf;
268ffdff6aSGreg Kroah-Hartman 	unsigned int i;
278ffdff6aSGreg Kroah-Hartman 
288ffdff6aSGreg Kroah-Hartman 	if (bm->page_list) {
298ffdff6aSGreg Kroah-Hartman 		if (bm->dma_dir != DMA_NONE) {
308ffdff6aSGreg Kroah-Hartman 			/*
318ffdff6aSGreg Kroah-Hartman 			 * DMA buffer was allocated as a single block.
328ffdff6aSGreg Kroah-Hartman 			 * Address is in page_list[0].
338ffdff6aSGreg Kroah-Hartman 			 */
348ffdff6aSGreg Kroah-Hartman 			buf = &bm->page_list[0];
358ffdff6aSGreg Kroah-Hartman 			dma_free_coherent(bm->dma_hw_dev,
368ffdff6aSGreg Kroah-Hartman 					  PAGE_SIZE * bm->n_pages,
378ffdff6aSGreg Kroah-Hartman 					  buf->virt_addr, buf->dma_addr);
388ffdff6aSGreg Kroah-Hartman 		} else {
398ffdff6aSGreg Kroah-Hartman 			for (i = 0; i < bm->n_pages; i++) {
408ffdff6aSGreg Kroah-Hartman 				buf = &bm->page_list[i];
418ffdff6aSGreg Kroah-Hartman 				ClearPageReserved(virt_to_page(buf->virt_addr));
428ffdff6aSGreg Kroah-Hartman 				free_page((unsigned long)buf->virt_addr);
438ffdff6aSGreg Kroah-Hartman 			}
448ffdff6aSGreg Kroah-Hartman 		}
458ffdff6aSGreg Kroah-Hartman 		vfree(bm->page_list);
468ffdff6aSGreg Kroah-Hartman 	}
478ffdff6aSGreg Kroah-Hartman 	if (bm->dma_dir != DMA_NONE)
488ffdff6aSGreg Kroah-Hartman 		put_device(bm->dma_hw_dev);
498ffdff6aSGreg Kroah-Hartman 	kfree(bm);
508ffdff6aSGreg Kroah-Hartman }
518ffdff6aSGreg Kroah-Hartman 
__comedi_buf_free(struct comedi_device * dev,struct comedi_subdevice * s)528ffdff6aSGreg Kroah-Hartman static void __comedi_buf_free(struct comedi_device *dev,
538ffdff6aSGreg Kroah-Hartman 			      struct comedi_subdevice *s)
548ffdff6aSGreg Kroah-Hartman {
558ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
568ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm;
578ffdff6aSGreg Kroah-Hartman 	unsigned long flags;
588ffdff6aSGreg Kroah-Hartman 
598ffdff6aSGreg Kroah-Hartman 	if (async->prealloc_buf) {
608ffdff6aSGreg Kroah-Hartman 		if (s->async_dma_dir == DMA_NONE)
618ffdff6aSGreg Kroah-Hartman 			vunmap(async->prealloc_buf);
628ffdff6aSGreg Kroah-Hartman 		async->prealloc_buf = NULL;
638ffdff6aSGreg Kroah-Hartman 		async->prealloc_bufsz = 0;
648ffdff6aSGreg Kroah-Hartman 	}
658ffdff6aSGreg Kroah-Hartman 
668ffdff6aSGreg Kroah-Hartman 	spin_lock_irqsave(&s->spin_lock, flags);
678ffdff6aSGreg Kroah-Hartman 	bm = async->buf_map;
688ffdff6aSGreg Kroah-Hartman 	async->buf_map = NULL;
698ffdff6aSGreg Kroah-Hartman 	spin_unlock_irqrestore(&s->spin_lock, flags);
708ffdff6aSGreg Kroah-Hartman 	comedi_buf_map_put(bm);
718ffdff6aSGreg Kroah-Hartman }
728ffdff6aSGreg Kroah-Hartman 
738ffdff6aSGreg Kroah-Hartman static struct comedi_buf_map *
comedi_buf_map_alloc(struct comedi_device * dev,enum dma_data_direction dma_dir,unsigned int n_pages)748ffdff6aSGreg Kroah-Hartman comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir,
758ffdff6aSGreg Kroah-Hartman 		     unsigned int n_pages)
768ffdff6aSGreg Kroah-Hartman {
778ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm;
788ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_page *buf;
798ffdff6aSGreg Kroah-Hartman 	unsigned int i;
808ffdff6aSGreg Kroah-Hartman 
818ffdff6aSGreg Kroah-Hartman 	bm = kzalloc(sizeof(*bm), GFP_KERNEL);
828ffdff6aSGreg Kroah-Hartman 	if (!bm)
838ffdff6aSGreg Kroah-Hartman 		return NULL;
848ffdff6aSGreg Kroah-Hartman 
858ffdff6aSGreg Kroah-Hartman 	kref_init(&bm->refcount);
868ffdff6aSGreg Kroah-Hartman 	bm->dma_dir = dma_dir;
878ffdff6aSGreg Kroah-Hartman 	if (bm->dma_dir != DMA_NONE) {
888ffdff6aSGreg Kroah-Hartman 		/* Need ref to hardware device to free buffer later. */
898ffdff6aSGreg Kroah-Hartman 		bm->dma_hw_dev = get_device(dev->hw_dev);
908ffdff6aSGreg Kroah-Hartman 	}
918ffdff6aSGreg Kroah-Hartman 
928ffdff6aSGreg Kroah-Hartman 	bm->page_list = vzalloc(sizeof(*buf) * n_pages);
938ffdff6aSGreg Kroah-Hartman 	if (!bm->page_list)
948ffdff6aSGreg Kroah-Hartman 		goto err;
958ffdff6aSGreg Kroah-Hartman 
968ffdff6aSGreg Kroah-Hartman 	if (bm->dma_dir != DMA_NONE) {
978ffdff6aSGreg Kroah-Hartman 		void *virt_addr;
988ffdff6aSGreg Kroah-Hartman 		dma_addr_t dma_addr;
998ffdff6aSGreg Kroah-Hartman 
1008ffdff6aSGreg Kroah-Hartman 		/*
1018ffdff6aSGreg Kroah-Hartman 		 * Currently, the DMA buffer needs to be allocated as a
1028ffdff6aSGreg Kroah-Hartman 		 * single block so that it can be mmap()'ed.
1038ffdff6aSGreg Kroah-Hartman 		 */
1048ffdff6aSGreg Kroah-Hartman 		virt_addr = dma_alloc_coherent(bm->dma_hw_dev,
1058ffdff6aSGreg Kroah-Hartman 					       PAGE_SIZE * n_pages, &dma_addr,
1068ffdff6aSGreg Kroah-Hartman 					       GFP_KERNEL);
1078ffdff6aSGreg Kroah-Hartman 		if (!virt_addr)
1088ffdff6aSGreg Kroah-Hartman 			goto err;
1098ffdff6aSGreg Kroah-Hartman 
1108ffdff6aSGreg Kroah-Hartman 		for (i = 0; i < n_pages; i++) {
1118ffdff6aSGreg Kroah-Hartman 			buf = &bm->page_list[i];
1128ffdff6aSGreg Kroah-Hartman 			buf->virt_addr = virt_addr + (i << PAGE_SHIFT);
1138ffdff6aSGreg Kroah-Hartman 			buf->dma_addr = dma_addr + (i << PAGE_SHIFT);
1148ffdff6aSGreg Kroah-Hartman 		}
1158ffdff6aSGreg Kroah-Hartman 
1168ffdff6aSGreg Kroah-Hartman 		bm->n_pages = i;
1178ffdff6aSGreg Kroah-Hartman 	} else {
1188ffdff6aSGreg Kroah-Hartman 		for (i = 0; i < n_pages; i++) {
1198ffdff6aSGreg Kroah-Hartman 			buf = &bm->page_list[i];
1208ffdff6aSGreg Kroah-Hartman 			buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
1218ffdff6aSGreg Kroah-Hartman 			if (!buf->virt_addr)
1228ffdff6aSGreg Kroah-Hartman 				break;
1238ffdff6aSGreg Kroah-Hartman 
1248ffdff6aSGreg Kroah-Hartman 			SetPageReserved(virt_to_page(buf->virt_addr));
1258ffdff6aSGreg Kroah-Hartman 		}
1268ffdff6aSGreg Kroah-Hartman 
1278ffdff6aSGreg Kroah-Hartman 		bm->n_pages = i;
1288ffdff6aSGreg Kroah-Hartman 		if (i < n_pages)
1298ffdff6aSGreg Kroah-Hartman 			goto err;
1308ffdff6aSGreg Kroah-Hartman 	}
1318ffdff6aSGreg Kroah-Hartman 
1328ffdff6aSGreg Kroah-Hartman 	return bm;
1338ffdff6aSGreg Kroah-Hartman 
1348ffdff6aSGreg Kroah-Hartman err:
1358ffdff6aSGreg Kroah-Hartman 	comedi_buf_map_put(bm);
1368ffdff6aSGreg Kroah-Hartman 	return NULL;
1378ffdff6aSGreg Kroah-Hartman }
1388ffdff6aSGreg Kroah-Hartman 
__comedi_buf_alloc(struct comedi_device * dev,struct comedi_subdevice * s,unsigned int n_pages)1398ffdff6aSGreg Kroah-Hartman static void __comedi_buf_alloc(struct comedi_device *dev,
1408ffdff6aSGreg Kroah-Hartman 			       struct comedi_subdevice *s,
1418ffdff6aSGreg Kroah-Hartman 			       unsigned int n_pages)
1428ffdff6aSGreg Kroah-Hartman {
1438ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
1448ffdff6aSGreg Kroah-Hartman 	struct page **pages = NULL;
1458ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm;
1468ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_page *buf;
1478ffdff6aSGreg Kroah-Hartman 	unsigned long flags;
1488ffdff6aSGreg Kroah-Hartman 	unsigned int i;
1498ffdff6aSGreg Kroah-Hartman 
1508ffdff6aSGreg Kroah-Hartman 	if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
1518ffdff6aSGreg Kroah-Hartman 		dev_err(dev->class_dev,
1528ffdff6aSGreg Kroah-Hartman 			"dma buffer allocation not supported\n");
1538ffdff6aSGreg Kroah-Hartman 		return;
1548ffdff6aSGreg Kroah-Hartman 	}
1558ffdff6aSGreg Kroah-Hartman 
1568ffdff6aSGreg Kroah-Hartman 	bm = comedi_buf_map_alloc(dev, s->async_dma_dir, n_pages);
1578ffdff6aSGreg Kroah-Hartman 	if (!bm)
1588ffdff6aSGreg Kroah-Hartman 		return;
1598ffdff6aSGreg Kroah-Hartman 
1608ffdff6aSGreg Kroah-Hartman 	spin_lock_irqsave(&s->spin_lock, flags);
1618ffdff6aSGreg Kroah-Hartman 	async->buf_map = bm;
1628ffdff6aSGreg Kroah-Hartman 	spin_unlock_irqrestore(&s->spin_lock, flags);
1638ffdff6aSGreg Kroah-Hartman 
1648ffdff6aSGreg Kroah-Hartman 	if (bm->dma_dir != DMA_NONE) {
1658ffdff6aSGreg Kroah-Hartman 		/*
1668ffdff6aSGreg Kroah-Hartman 		 * DMA buffer was allocated as a single block.
1678ffdff6aSGreg Kroah-Hartman 		 * Address is in page_list[0].
1688ffdff6aSGreg Kroah-Hartman 		 */
1698ffdff6aSGreg Kroah-Hartman 		buf = &bm->page_list[0];
1708ffdff6aSGreg Kroah-Hartman 		async->prealloc_buf = buf->virt_addr;
1718ffdff6aSGreg Kroah-Hartman 	} else {
1728ffdff6aSGreg Kroah-Hartman 		pages = vmalloc(sizeof(struct page *) * n_pages);
1738ffdff6aSGreg Kroah-Hartman 		if (!pages)
1748ffdff6aSGreg Kroah-Hartman 			return;
1758ffdff6aSGreg Kroah-Hartman 
1768ffdff6aSGreg Kroah-Hartman 		for (i = 0; i < n_pages; i++) {
1778ffdff6aSGreg Kroah-Hartman 			buf = &bm->page_list[i];
1788ffdff6aSGreg Kroah-Hartman 			pages[i] = virt_to_page(buf->virt_addr);
1798ffdff6aSGreg Kroah-Hartman 		}
1808ffdff6aSGreg Kroah-Hartman 
1818ffdff6aSGreg Kroah-Hartman 		/* vmap the pages to prealloc_buf */
1828ffdff6aSGreg Kroah-Hartman 		async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
1838ffdff6aSGreg Kroah-Hartman 					   COMEDI_PAGE_PROTECTION);
1848ffdff6aSGreg Kroah-Hartman 
1858ffdff6aSGreg Kroah-Hartman 		vfree(pages);
1868ffdff6aSGreg Kroah-Hartman 	}
1878ffdff6aSGreg Kroah-Hartman }
1888ffdff6aSGreg Kroah-Hartman 
comedi_buf_map_get(struct comedi_buf_map * bm)1898ffdff6aSGreg Kroah-Hartman void comedi_buf_map_get(struct comedi_buf_map *bm)
1908ffdff6aSGreg Kroah-Hartman {
1918ffdff6aSGreg Kroah-Hartman 	if (bm)
1928ffdff6aSGreg Kroah-Hartman 		kref_get(&bm->refcount);
1938ffdff6aSGreg Kroah-Hartman }
1948ffdff6aSGreg Kroah-Hartman 
comedi_buf_map_put(struct comedi_buf_map * bm)1958ffdff6aSGreg Kroah-Hartman int comedi_buf_map_put(struct comedi_buf_map *bm)
1968ffdff6aSGreg Kroah-Hartman {
1978ffdff6aSGreg Kroah-Hartman 	if (bm)
1988ffdff6aSGreg Kroah-Hartman 		return kref_put(&bm->refcount, comedi_buf_map_kref_release);
1998ffdff6aSGreg Kroah-Hartman 	return 1;
2008ffdff6aSGreg Kroah-Hartman }
2018ffdff6aSGreg Kroah-Hartman 
2028ffdff6aSGreg Kroah-Hartman /* helper for "access" vm operation */
comedi_buf_map_access(struct comedi_buf_map * bm,unsigned long offset,void * buf,int len,int write)2038ffdff6aSGreg Kroah-Hartman int comedi_buf_map_access(struct comedi_buf_map *bm, unsigned long offset,
2048ffdff6aSGreg Kroah-Hartman 			  void *buf, int len, int write)
2058ffdff6aSGreg Kroah-Hartman {
2068ffdff6aSGreg Kroah-Hartman 	unsigned int pgoff = offset_in_page(offset);
2078ffdff6aSGreg Kroah-Hartman 	unsigned long pg = offset >> PAGE_SHIFT;
2088ffdff6aSGreg Kroah-Hartman 	int done = 0;
2098ffdff6aSGreg Kroah-Hartman 
2108ffdff6aSGreg Kroah-Hartman 	while (done < len && pg < bm->n_pages) {
2118ffdff6aSGreg Kroah-Hartman 		int l = min_t(int, len - done, PAGE_SIZE - pgoff);
2128ffdff6aSGreg Kroah-Hartman 		void *b = bm->page_list[pg].virt_addr + pgoff;
2138ffdff6aSGreg Kroah-Hartman 
2148ffdff6aSGreg Kroah-Hartman 		if (write)
2158ffdff6aSGreg Kroah-Hartman 			memcpy(b, buf, l);
2168ffdff6aSGreg Kroah-Hartman 		else
2178ffdff6aSGreg Kroah-Hartman 			memcpy(buf, b, l);
2188ffdff6aSGreg Kroah-Hartman 		buf += l;
2198ffdff6aSGreg Kroah-Hartman 		done += l;
2208ffdff6aSGreg Kroah-Hartman 		pg++;
2218ffdff6aSGreg Kroah-Hartman 		pgoff = 0;
2228ffdff6aSGreg Kroah-Hartman 	}
2238ffdff6aSGreg Kroah-Hartman 	return done;
2248ffdff6aSGreg Kroah-Hartman }
2258ffdff6aSGreg Kroah-Hartman 
2268ffdff6aSGreg Kroah-Hartman /* returns s->async->buf_map and increments its kref refcount */
2278ffdff6aSGreg Kroah-Hartman struct comedi_buf_map *
comedi_buf_map_from_subdev_get(struct comedi_subdevice * s)2288ffdff6aSGreg Kroah-Hartman comedi_buf_map_from_subdev_get(struct comedi_subdevice *s)
2298ffdff6aSGreg Kroah-Hartman {
2308ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
2318ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm = NULL;
2328ffdff6aSGreg Kroah-Hartman 	unsigned long flags;
2338ffdff6aSGreg Kroah-Hartman 
2348ffdff6aSGreg Kroah-Hartman 	if (!async)
2358ffdff6aSGreg Kroah-Hartman 		return NULL;
2368ffdff6aSGreg Kroah-Hartman 
2378ffdff6aSGreg Kroah-Hartman 	spin_lock_irqsave(&s->spin_lock, flags);
2388ffdff6aSGreg Kroah-Hartman 	bm = async->buf_map;
2398ffdff6aSGreg Kroah-Hartman 	/* only want it if buffer pages allocated */
2408ffdff6aSGreg Kroah-Hartman 	if (bm && bm->n_pages)
2418ffdff6aSGreg Kroah-Hartman 		comedi_buf_map_get(bm);
2428ffdff6aSGreg Kroah-Hartman 	else
2438ffdff6aSGreg Kroah-Hartman 		bm = NULL;
2448ffdff6aSGreg Kroah-Hartman 	spin_unlock_irqrestore(&s->spin_lock, flags);
2458ffdff6aSGreg Kroah-Hartman 
2468ffdff6aSGreg Kroah-Hartman 	return bm;
2478ffdff6aSGreg Kroah-Hartman }
2488ffdff6aSGreg Kroah-Hartman 
comedi_buf_is_mmapped(struct comedi_subdevice * s)2498ffdff6aSGreg Kroah-Hartman bool comedi_buf_is_mmapped(struct comedi_subdevice *s)
2508ffdff6aSGreg Kroah-Hartman {
2518ffdff6aSGreg Kroah-Hartman 	struct comedi_buf_map *bm = s->async->buf_map;
2528ffdff6aSGreg Kroah-Hartman 
2538ffdff6aSGreg Kroah-Hartman 	return bm && (kref_read(&bm->refcount) > 1);
2548ffdff6aSGreg Kroah-Hartman }
2558ffdff6aSGreg Kroah-Hartman 
comedi_buf_alloc(struct comedi_device * dev,struct comedi_subdevice * s,unsigned long new_size)2568ffdff6aSGreg Kroah-Hartman int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
2578ffdff6aSGreg Kroah-Hartman 		     unsigned long new_size)
2588ffdff6aSGreg Kroah-Hartman {
2598ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
2608ffdff6aSGreg Kroah-Hartman 
2618ffdff6aSGreg Kroah-Hartman 	lockdep_assert_held(&dev->mutex);
2628ffdff6aSGreg Kroah-Hartman 
2638ffdff6aSGreg Kroah-Hartman 	/* Round up new_size to multiple of PAGE_SIZE */
2648ffdff6aSGreg Kroah-Hartman 	new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
2658ffdff6aSGreg Kroah-Hartman 
2668ffdff6aSGreg Kroah-Hartman 	/* if no change is required, do nothing */
2678ffdff6aSGreg Kroah-Hartman 	if (async->prealloc_buf && async->prealloc_bufsz == new_size)
2688ffdff6aSGreg Kroah-Hartman 		return 0;
2698ffdff6aSGreg Kroah-Hartman 
2708ffdff6aSGreg Kroah-Hartman 	/* deallocate old buffer */
2718ffdff6aSGreg Kroah-Hartman 	__comedi_buf_free(dev, s);
2728ffdff6aSGreg Kroah-Hartman 
2738ffdff6aSGreg Kroah-Hartman 	/* allocate new buffer */
2748ffdff6aSGreg Kroah-Hartman 	if (new_size) {
2758ffdff6aSGreg Kroah-Hartman 		unsigned int n_pages = new_size >> PAGE_SHIFT;
2768ffdff6aSGreg Kroah-Hartman 
2778ffdff6aSGreg Kroah-Hartman 		__comedi_buf_alloc(dev, s, n_pages);
2788ffdff6aSGreg Kroah-Hartman 
2798ffdff6aSGreg Kroah-Hartman 		if (!async->prealloc_buf) {
2808ffdff6aSGreg Kroah-Hartman 			/* allocation failed */
2818ffdff6aSGreg Kroah-Hartman 			__comedi_buf_free(dev, s);
2828ffdff6aSGreg Kroah-Hartman 			return -ENOMEM;
2838ffdff6aSGreg Kroah-Hartman 		}
2848ffdff6aSGreg Kroah-Hartman 	}
2858ffdff6aSGreg Kroah-Hartman 	async->prealloc_bufsz = new_size;
2868ffdff6aSGreg Kroah-Hartman 
2878ffdff6aSGreg Kroah-Hartman 	return 0;
2888ffdff6aSGreg Kroah-Hartman }
2898ffdff6aSGreg Kroah-Hartman 
comedi_buf_reset(struct comedi_subdevice * s)2908ffdff6aSGreg Kroah-Hartman void comedi_buf_reset(struct comedi_subdevice *s)
2918ffdff6aSGreg Kroah-Hartman {
2928ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
2938ffdff6aSGreg Kroah-Hartman 
2948ffdff6aSGreg Kroah-Hartman 	async->buf_write_alloc_count = 0;
2958ffdff6aSGreg Kroah-Hartman 	async->buf_write_count = 0;
2968ffdff6aSGreg Kroah-Hartman 	async->buf_read_alloc_count = 0;
2978ffdff6aSGreg Kroah-Hartman 	async->buf_read_count = 0;
2988ffdff6aSGreg Kroah-Hartman 
2998ffdff6aSGreg Kroah-Hartman 	async->buf_write_ptr = 0;
3008ffdff6aSGreg Kroah-Hartman 	async->buf_read_ptr = 0;
3018ffdff6aSGreg Kroah-Hartman 
3028ffdff6aSGreg Kroah-Hartman 	async->cur_chan = 0;
3038ffdff6aSGreg Kroah-Hartman 	async->scans_done = 0;
3048ffdff6aSGreg Kroah-Hartman 	async->scan_progress = 0;
3058ffdff6aSGreg Kroah-Hartman 	async->munge_chan = 0;
3068ffdff6aSGreg Kroah-Hartman 	async->munge_count = 0;
3078ffdff6aSGreg Kroah-Hartman 	async->munge_ptr = 0;
3088ffdff6aSGreg Kroah-Hartman 
3098ffdff6aSGreg Kroah-Hartman 	async->events = 0;
3108ffdff6aSGreg Kroah-Hartman }
3118ffdff6aSGreg Kroah-Hartman 
comedi_buf_write_n_unalloc(struct comedi_subdevice * s)3128ffdff6aSGreg Kroah-Hartman static unsigned int comedi_buf_write_n_unalloc(struct comedi_subdevice *s)
3138ffdff6aSGreg Kroah-Hartman {
3148ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
3158ffdff6aSGreg Kroah-Hartman 	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
3168ffdff6aSGreg Kroah-Hartman 
3178ffdff6aSGreg Kroah-Hartman 	return free_end - async->buf_write_alloc_count;
3188ffdff6aSGreg Kroah-Hartman }
3198ffdff6aSGreg Kroah-Hartman 
comedi_buf_write_n_available(struct comedi_subdevice * s)3208ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_write_n_available(struct comedi_subdevice *s)
3218ffdff6aSGreg Kroah-Hartman {
3228ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
3238ffdff6aSGreg Kroah-Hartman 	unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
3248ffdff6aSGreg Kroah-Hartman 
3258ffdff6aSGreg Kroah-Hartman 	return free_end - async->buf_write_count;
3268ffdff6aSGreg Kroah-Hartman }
3278ffdff6aSGreg Kroah-Hartman 
3288ffdff6aSGreg Kroah-Hartman /**
3298ffdff6aSGreg Kroah-Hartman  * comedi_buf_write_alloc() - Reserve buffer space for writing
3308ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
3318ffdff6aSGreg Kroah-Hartman  * @nbytes: Maximum space to reserve in bytes.
3328ffdff6aSGreg Kroah-Hartman  *
3338ffdff6aSGreg Kroah-Hartman  * Reserve up to @nbytes bytes of space to be written in the COMEDI acquisition
3348ffdff6aSGreg Kroah-Hartman  * data buffer associated with the subdevice.  The amount reserved is limited
3358ffdff6aSGreg Kroah-Hartman  * by the space available.
3368ffdff6aSGreg Kroah-Hartman  *
3378ffdff6aSGreg Kroah-Hartman  * Return: The amount of space reserved in bytes.
3388ffdff6aSGreg Kroah-Hartman  */
comedi_buf_write_alloc(struct comedi_subdevice * s,unsigned int nbytes)3398ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s,
3408ffdff6aSGreg Kroah-Hartman 				    unsigned int nbytes)
3418ffdff6aSGreg Kroah-Hartman {
3428ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
3438ffdff6aSGreg Kroah-Hartman 	unsigned int unalloc = comedi_buf_write_n_unalloc(s);
3448ffdff6aSGreg Kroah-Hartman 
3458ffdff6aSGreg Kroah-Hartman 	if (nbytes > unalloc)
3468ffdff6aSGreg Kroah-Hartman 		nbytes = unalloc;
3478ffdff6aSGreg Kroah-Hartman 
3488ffdff6aSGreg Kroah-Hartman 	async->buf_write_alloc_count += nbytes;
3498ffdff6aSGreg Kroah-Hartman 
3508ffdff6aSGreg Kroah-Hartman 	/*
3518ffdff6aSGreg Kroah-Hartman 	 * ensure the async buffer 'counts' are read and updated
3528ffdff6aSGreg Kroah-Hartman 	 * before we write data to the write-alloc'ed buffer space
3538ffdff6aSGreg Kroah-Hartman 	 */
3548ffdff6aSGreg Kroah-Hartman 	smp_mb();
3558ffdff6aSGreg Kroah-Hartman 
3568ffdff6aSGreg Kroah-Hartman 	return nbytes;
3578ffdff6aSGreg Kroah-Hartman }
3588ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
3598ffdff6aSGreg Kroah-Hartman 
3608ffdff6aSGreg Kroah-Hartman /*
3618ffdff6aSGreg Kroah-Hartman  * munging is applied to data by core as it passes between user
3628ffdff6aSGreg Kroah-Hartman  * and kernel space
3638ffdff6aSGreg Kroah-Hartman  */
comedi_buf_munge(struct comedi_subdevice * s,unsigned int num_bytes)3648ffdff6aSGreg Kroah-Hartman static unsigned int comedi_buf_munge(struct comedi_subdevice *s,
3658ffdff6aSGreg Kroah-Hartman 				     unsigned int num_bytes)
3668ffdff6aSGreg Kroah-Hartman {
3678ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
3688ffdff6aSGreg Kroah-Hartman 	unsigned int count = 0;
3698ffdff6aSGreg Kroah-Hartman 	const unsigned int num_sample_bytes = comedi_bytes_per_sample(s);
3708ffdff6aSGreg Kroah-Hartman 
3718ffdff6aSGreg Kroah-Hartman 	if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
3728ffdff6aSGreg Kroah-Hartman 		async->munge_count += num_bytes;
3738ffdff6aSGreg Kroah-Hartman 		return num_bytes;
3748ffdff6aSGreg Kroah-Hartman 	}
3758ffdff6aSGreg Kroah-Hartman 
3768ffdff6aSGreg Kroah-Hartman 	/* don't munge partial samples */
3778ffdff6aSGreg Kroah-Hartman 	num_bytes -= num_bytes % num_sample_bytes;
3788ffdff6aSGreg Kroah-Hartman 	while (count < num_bytes) {
3798ffdff6aSGreg Kroah-Hartman 		int block_size = num_bytes - count;
3808ffdff6aSGreg Kroah-Hartman 		unsigned int buf_end;
3818ffdff6aSGreg Kroah-Hartman 
3828ffdff6aSGreg Kroah-Hartman 		buf_end = async->prealloc_bufsz - async->munge_ptr;
3838ffdff6aSGreg Kroah-Hartman 		if (block_size > buf_end)
3848ffdff6aSGreg Kroah-Hartman 			block_size = buf_end;
3858ffdff6aSGreg Kroah-Hartman 
3868ffdff6aSGreg Kroah-Hartman 		s->munge(s->device, s,
3878ffdff6aSGreg Kroah-Hartman 			 async->prealloc_buf + async->munge_ptr,
3888ffdff6aSGreg Kroah-Hartman 			 block_size, async->munge_chan);
3898ffdff6aSGreg Kroah-Hartman 
3908ffdff6aSGreg Kroah-Hartman 		/*
3918ffdff6aSGreg Kroah-Hartman 		 * ensure data is munged in buffer before the
3928ffdff6aSGreg Kroah-Hartman 		 * async buffer munge_count is incremented
3938ffdff6aSGreg Kroah-Hartman 		 */
3948ffdff6aSGreg Kroah-Hartman 		smp_wmb();
3958ffdff6aSGreg Kroah-Hartman 
3968ffdff6aSGreg Kroah-Hartman 		async->munge_chan += block_size / num_sample_bytes;
3978ffdff6aSGreg Kroah-Hartman 		async->munge_chan %= async->cmd.chanlist_len;
3988ffdff6aSGreg Kroah-Hartman 		async->munge_count += block_size;
3998ffdff6aSGreg Kroah-Hartman 		async->munge_ptr += block_size;
4008ffdff6aSGreg Kroah-Hartman 		async->munge_ptr %= async->prealloc_bufsz;
4018ffdff6aSGreg Kroah-Hartman 		count += block_size;
4028ffdff6aSGreg Kroah-Hartman 	}
4038ffdff6aSGreg Kroah-Hartman 
4048ffdff6aSGreg Kroah-Hartman 	return count;
4058ffdff6aSGreg Kroah-Hartman }
4068ffdff6aSGreg Kroah-Hartman 
comedi_buf_write_n_allocated(struct comedi_subdevice * s)4078ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_write_n_allocated(struct comedi_subdevice *s)
4088ffdff6aSGreg Kroah-Hartman {
4098ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
4108ffdff6aSGreg Kroah-Hartman 
4118ffdff6aSGreg Kroah-Hartman 	return async->buf_write_alloc_count - async->buf_write_count;
4128ffdff6aSGreg Kroah-Hartman }
4138ffdff6aSGreg Kroah-Hartman 
4148ffdff6aSGreg Kroah-Hartman /**
4158ffdff6aSGreg Kroah-Hartman  * comedi_buf_write_free() - Free buffer space after it is written
4168ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
4178ffdff6aSGreg Kroah-Hartman  * @nbytes: Maximum space to free in bytes.
4188ffdff6aSGreg Kroah-Hartman  *
4198ffdff6aSGreg Kroah-Hartman  * Free up to @nbytes bytes of space previously reserved for writing in the
4208ffdff6aSGreg Kroah-Hartman  * COMEDI acquisition data buffer associated with the subdevice.  The amount of
4218ffdff6aSGreg Kroah-Hartman  * space freed is limited to the amount that was reserved.  The freed space is
4228ffdff6aSGreg Kroah-Hartman  * assumed to have been filled with sample data by the writer.
4238ffdff6aSGreg Kroah-Hartman  *
4248ffdff6aSGreg Kroah-Hartman  * If the samples in the freed space need to be "munged", do so here.  The
4258ffdff6aSGreg Kroah-Hartman  * freed space becomes available for allocation by the reader.
4268ffdff6aSGreg Kroah-Hartman  *
4278ffdff6aSGreg Kroah-Hartman  * Return: The amount of space freed in bytes.
4288ffdff6aSGreg Kroah-Hartman  */
comedi_buf_write_free(struct comedi_subdevice * s,unsigned int nbytes)4298ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_write_free(struct comedi_subdevice *s,
4308ffdff6aSGreg Kroah-Hartman 				   unsigned int nbytes)
4318ffdff6aSGreg Kroah-Hartman {
4328ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
4338ffdff6aSGreg Kroah-Hartman 	unsigned int allocated = comedi_buf_write_n_allocated(s);
4348ffdff6aSGreg Kroah-Hartman 
4358ffdff6aSGreg Kroah-Hartman 	if (nbytes > allocated)
4368ffdff6aSGreg Kroah-Hartman 		nbytes = allocated;
4378ffdff6aSGreg Kroah-Hartman 
4388ffdff6aSGreg Kroah-Hartman 	async->buf_write_count += nbytes;
4398ffdff6aSGreg Kroah-Hartman 	async->buf_write_ptr += nbytes;
4408ffdff6aSGreg Kroah-Hartman 	comedi_buf_munge(s, async->buf_write_count - async->munge_count);
4418ffdff6aSGreg Kroah-Hartman 	if (async->buf_write_ptr >= async->prealloc_bufsz)
4428ffdff6aSGreg Kroah-Hartman 		async->buf_write_ptr %= async->prealloc_bufsz;
4438ffdff6aSGreg Kroah-Hartman 
4448ffdff6aSGreg Kroah-Hartman 	return nbytes;
4458ffdff6aSGreg Kroah-Hartman }
4468ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_write_free);
4478ffdff6aSGreg Kroah-Hartman 
4488ffdff6aSGreg Kroah-Hartman /**
4498ffdff6aSGreg Kroah-Hartman  * comedi_buf_read_n_available() - Determine amount of readable buffer space
4508ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
4518ffdff6aSGreg Kroah-Hartman  *
4528ffdff6aSGreg Kroah-Hartman  * Determine the amount of readable buffer space in the COMEDI acquisition data
4538ffdff6aSGreg Kroah-Hartman  * buffer associated with the subdevice.  The readable buffer space is that
4548ffdff6aSGreg Kroah-Hartman  * which has been freed by the writer and "munged" to the sample data format
4558ffdff6aSGreg Kroah-Hartman  * expected by COMEDI if necessary.
4568ffdff6aSGreg Kroah-Hartman  *
4578ffdff6aSGreg Kroah-Hartman  * Return: The amount of readable buffer space.
4588ffdff6aSGreg Kroah-Hartman  */
comedi_buf_read_n_available(struct comedi_subdevice * s)4598ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s)
4608ffdff6aSGreg Kroah-Hartman {
4618ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
4628ffdff6aSGreg Kroah-Hartman 	unsigned int num_bytes;
4638ffdff6aSGreg Kroah-Hartman 
4648ffdff6aSGreg Kroah-Hartman 	if (!async)
4658ffdff6aSGreg Kroah-Hartman 		return 0;
4668ffdff6aSGreg Kroah-Hartman 
4678ffdff6aSGreg Kroah-Hartman 	num_bytes = async->munge_count - async->buf_read_count;
4688ffdff6aSGreg Kroah-Hartman 
4698ffdff6aSGreg Kroah-Hartman 	/*
4708ffdff6aSGreg Kroah-Hartman 	 * ensure the async buffer 'counts' are read before we
4718ffdff6aSGreg Kroah-Hartman 	 * attempt to read data from the buffer
4728ffdff6aSGreg Kroah-Hartman 	 */
4738ffdff6aSGreg Kroah-Hartman 	smp_rmb();
4748ffdff6aSGreg Kroah-Hartman 
4758ffdff6aSGreg Kroah-Hartman 	return num_bytes;
4768ffdff6aSGreg Kroah-Hartman }
4778ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
4788ffdff6aSGreg Kroah-Hartman 
4798ffdff6aSGreg Kroah-Hartman /**
4808ffdff6aSGreg Kroah-Hartman  * comedi_buf_read_alloc() - Reserve buffer space for reading
4818ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
4828ffdff6aSGreg Kroah-Hartman  * @nbytes: Maximum space to reserve in bytes.
4838ffdff6aSGreg Kroah-Hartman  *
4848ffdff6aSGreg Kroah-Hartman  * Reserve up to @nbytes bytes of previously written and "munged" buffer space
4858ffdff6aSGreg Kroah-Hartman  * for reading in the COMEDI acquisition data buffer associated with the
4868ffdff6aSGreg Kroah-Hartman  * subdevice.  The amount reserved is limited to the space available.  The
4878ffdff6aSGreg Kroah-Hartman  * reader can read from the reserved space and then free it.  A reader is also
4888ffdff6aSGreg Kroah-Hartman  * allowed to read from the space before reserving it as long as it determines
4898ffdff6aSGreg Kroah-Hartman  * the amount of readable data available, but the space needs to be marked as
4908ffdff6aSGreg Kroah-Hartman  * reserved before it can be freed.
4918ffdff6aSGreg Kroah-Hartman  *
4928ffdff6aSGreg Kroah-Hartman  * Return: The amount of space reserved in bytes.
4938ffdff6aSGreg Kroah-Hartman  */
comedi_buf_read_alloc(struct comedi_subdevice * s,unsigned int nbytes)4948ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s,
4958ffdff6aSGreg Kroah-Hartman 				   unsigned int nbytes)
4968ffdff6aSGreg Kroah-Hartman {
4978ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
4988ffdff6aSGreg Kroah-Hartman 	unsigned int available;
4998ffdff6aSGreg Kroah-Hartman 
5008ffdff6aSGreg Kroah-Hartman 	available = async->munge_count - async->buf_read_alloc_count;
5018ffdff6aSGreg Kroah-Hartman 	if (nbytes > available)
5028ffdff6aSGreg Kroah-Hartman 		nbytes = available;
5038ffdff6aSGreg Kroah-Hartman 
5048ffdff6aSGreg Kroah-Hartman 	async->buf_read_alloc_count += nbytes;
5058ffdff6aSGreg Kroah-Hartman 
5068ffdff6aSGreg Kroah-Hartman 	/*
5078ffdff6aSGreg Kroah-Hartman 	 * ensure the async buffer 'counts' are read before we
5088ffdff6aSGreg Kroah-Hartman 	 * attempt to read data from the read-alloc'ed buffer space
5098ffdff6aSGreg Kroah-Hartman 	 */
5108ffdff6aSGreg Kroah-Hartman 	smp_rmb();
5118ffdff6aSGreg Kroah-Hartman 
5128ffdff6aSGreg Kroah-Hartman 	return nbytes;
5138ffdff6aSGreg Kroah-Hartman }
5148ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
5158ffdff6aSGreg Kroah-Hartman 
comedi_buf_read_n_allocated(struct comedi_async * async)5168ffdff6aSGreg Kroah-Hartman static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
5178ffdff6aSGreg Kroah-Hartman {
5188ffdff6aSGreg Kroah-Hartman 	return async->buf_read_alloc_count - async->buf_read_count;
5198ffdff6aSGreg Kroah-Hartman }
5208ffdff6aSGreg Kroah-Hartman 
5218ffdff6aSGreg Kroah-Hartman /**
5228ffdff6aSGreg Kroah-Hartman  * comedi_buf_read_free() - Free buffer space after it has been read
5238ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
5248ffdff6aSGreg Kroah-Hartman  * @nbytes: Maximum space to free in bytes.
5258ffdff6aSGreg Kroah-Hartman  *
5268ffdff6aSGreg Kroah-Hartman  * Free up to @nbytes bytes of buffer space previously reserved for reading in
5278ffdff6aSGreg Kroah-Hartman  * the COMEDI acquisition data buffer associated with the subdevice.  The
5288ffdff6aSGreg Kroah-Hartman  * amount of space freed is limited to the amount that was reserved.
5298ffdff6aSGreg Kroah-Hartman  *
5308ffdff6aSGreg Kroah-Hartman  * The freed space becomes available for allocation by the writer.
5318ffdff6aSGreg Kroah-Hartman  *
5328ffdff6aSGreg Kroah-Hartman  * Return: The amount of space freed in bytes.
5338ffdff6aSGreg Kroah-Hartman  */
comedi_buf_read_free(struct comedi_subdevice * s,unsigned int nbytes)5348ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_read_free(struct comedi_subdevice *s,
5358ffdff6aSGreg Kroah-Hartman 				  unsigned int nbytes)
5368ffdff6aSGreg Kroah-Hartman {
5378ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
5388ffdff6aSGreg Kroah-Hartman 	unsigned int allocated;
5398ffdff6aSGreg Kroah-Hartman 
5408ffdff6aSGreg Kroah-Hartman 	/*
5418ffdff6aSGreg Kroah-Hartman 	 * ensure data has been read out of buffer before
5428ffdff6aSGreg Kroah-Hartman 	 * the async read count is incremented
5438ffdff6aSGreg Kroah-Hartman 	 */
5448ffdff6aSGreg Kroah-Hartman 	smp_mb();
5458ffdff6aSGreg Kroah-Hartman 
5468ffdff6aSGreg Kroah-Hartman 	allocated = comedi_buf_read_n_allocated(async);
5478ffdff6aSGreg Kroah-Hartman 	if (nbytes > allocated)
5488ffdff6aSGreg Kroah-Hartman 		nbytes = allocated;
5498ffdff6aSGreg Kroah-Hartman 
5508ffdff6aSGreg Kroah-Hartman 	async->buf_read_count += nbytes;
5518ffdff6aSGreg Kroah-Hartman 	async->buf_read_ptr += nbytes;
5528ffdff6aSGreg Kroah-Hartman 	async->buf_read_ptr %= async->prealloc_bufsz;
5538ffdff6aSGreg Kroah-Hartman 	return nbytes;
5548ffdff6aSGreg Kroah-Hartman }
5558ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_read_free);
5568ffdff6aSGreg Kroah-Hartman 
comedi_buf_memcpy_to(struct comedi_subdevice * s,const void * data,unsigned int num_bytes)5578ffdff6aSGreg Kroah-Hartman static void comedi_buf_memcpy_to(struct comedi_subdevice *s,
5588ffdff6aSGreg Kroah-Hartman 				 const void *data, unsigned int num_bytes)
5598ffdff6aSGreg Kroah-Hartman {
5608ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
5618ffdff6aSGreg Kroah-Hartman 	unsigned int write_ptr = async->buf_write_ptr;
5628ffdff6aSGreg Kroah-Hartman 
5638ffdff6aSGreg Kroah-Hartman 	while (num_bytes) {
5648ffdff6aSGreg Kroah-Hartman 		unsigned int block_size;
5658ffdff6aSGreg Kroah-Hartman 
5668ffdff6aSGreg Kroah-Hartman 		if (write_ptr + num_bytes > async->prealloc_bufsz)
5678ffdff6aSGreg Kroah-Hartman 			block_size = async->prealloc_bufsz - write_ptr;
5688ffdff6aSGreg Kroah-Hartman 		else
5698ffdff6aSGreg Kroah-Hartman 			block_size = num_bytes;
5708ffdff6aSGreg Kroah-Hartman 
5718ffdff6aSGreg Kroah-Hartman 		memcpy(async->prealloc_buf + write_ptr, data, block_size);
5728ffdff6aSGreg Kroah-Hartman 
5738ffdff6aSGreg Kroah-Hartman 		data += block_size;
5748ffdff6aSGreg Kroah-Hartman 		num_bytes -= block_size;
5758ffdff6aSGreg Kroah-Hartman 
5768ffdff6aSGreg Kroah-Hartman 		write_ptr = 0;
5778ffdff6aSGreg Kroah-Hartman 	}
5788ffdff6aSGreg Kroah-Hartman }
5798ffdff6aSGreg Kroah-Hartman 
comedi_buf_memcpy_from(struct comedi_subdevice * s,void * dest,unsigned int nbytes)5808ffdff6aSGreg Kroah-Hartman static void comedi_buf_memcpy_from(struct comedi_subdevice *s,
5818ffdff6aSGreg Kroah-Hartman 				   void *dest, unsigned int nbytes)
5828ffdff6aSGreg Kroah-Hartman {
5838ffdff6aSGreg Kroah-Hartman 	void *src;
5848ffdff6aSGreg Kroah-Hartman 	struct comedi_async *async = s->async;
5858ffdff6aSGreg Kroah-Hartman 	unsigned int read_ptr = async->buf_read_ptr;
5868ffdff6aSGreg Kroah-Hartman 
5878ffdff6aSGreg Kroah-Hartman 	while (nbytes) {
5888ffdff6aSGreg Kroah-Hartman 		unsigned int block_size;
5898ffdff6aSGreg Kroah-Hartman 
5908ffdff6aSGreg Kroah-Hartman 		src = async->prealloc_buf + read_ptr;
5918ffdff6aSGreg Kroah-Hartman 
5928ffdff6aSGreg Kroah-Hartman 		if (nbytes >= async->prealloc_bufsz - read_ptr)
5938ffdff6aSGreg Kroah-Hartman 			block_size = async->prealloc_bufsz - read_ptr;
5948ffdff6aSGreg Kroah-Hartman 		else
5958ffdff6aSGreg Kroah-Hartman 			block_size = nbytes;
5968ffdff6aSGreg Kroah-Hartman 
5978ffdff6aSGreg Kroah-Hartman 		memcpy(dest, src, block_size);
5988ffdff6aSGreg Kroah-Hartman 		nbytes -= block_size;
5998ffdff6aSGreg Kroah-Hartman 		dest += block_size;
6008ffdff6aSGreg Kroah-Hartman 		read_ptr = 0;
6018ffdff6aSGreg Kroah-Hartman 	}
6028ffdff6aSGreg Kroah-Hartman }
6038ffdff6aSGreg Kroah-Hartman 
6048ffdff6aSGreg Kroah-Hartman /**
6058ffdff6aSGreg Kroah-Hartman  * comedi_buf_write_samples() - Write sample data to COMEDI buffer
6068ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
6078ffdff6aSGreg Kroah-Hartman  * @data: Pointer to source samples.
6088ffdff6aSGreg Kroah-Hartman  * @nsamples: Number of samples to write.
6098ffdff6aSGreg Kroah-Hartman  *
6108ffdff6aSGreg Kroah-Hartman  * Write up to @nsamples samples to the COMEDI acquisition data buffer
6118ffdff6aSGreg Kroah-Hartman  * associated with the subdevice, mark it as written and update the
6128ffdff6aSGreg Kroah-Hartman  * acquisition scan progress.  If there is not enough room for the specified
6138ffdff6aSGreg Kroah-Hartman  * number of samples, the number of samples written is limited to the number
6148ffdff6aSGreg Kroah-Hartman  * that will fit and the %COMEDI_CB_OVERFLOW event flag is set to cause the
6158ffdff6aSGreg Kroah-Hartman  * acquisition to terminate with an overrun error.  Set the %COMEDI_CB_BLOCK
6168ffdff6aSGreg Kroah-Hartman  * event flag if any samples are written to cause waiting tasks to be woken
6178ffdff6aSGreg Kroah-Hartman  * when the event flags are processed.
6188ffdff6aSGreg Kroah-Hartman  *
6198ffdff6aSGreg Kroah-Hartman  * Return: The amount of data written in bytes.
6208ffdff6aSGreg Kroah-Hartman  */
comedi_buf_write_samples(struct comedi_subdevice * s,const void * data,unsigned int nsamples)6218ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
6228ffdff6aSGreg Kroah-Hartman 				      const void *data, unsigned int nsamples)
6238ffdff6aSGreg Kroah-Hartman {
6248ffdff6aSGreg Kroah-Hartman 	unsigned int max_samples;
6258ffdff6aSGreg Kroah-Hartman 	unsigned int nbytes;
6268ffdff6aSGreg Kroah-Hartman 
6278ffdff6aSGreg Kroah-Hartman 	/*
6288ffdff6aSGreg Kroah-Hartman 	 * Make sure there is enough room in the buffer for all the samples.
6298ffdff6aSGreg Kroah-Hartman 	 * If not, clamp the nsamples to the number that will fit, flag the
6308ffdff6aSGreg Kroah-Hartman 	 * buffer overrun and add the samples that fit.
6318ffdff6aSGreg Kroah-Hartman 	 */
6328ffdff6aSGreg Kroah-Hartman 	max_samples = comedi_bytes_to_samples(s, comedi_buf_write_n_unalloc(s));
6338ffdff6aSGreg Kroah-Hartman 	if (nsamples > max_samples) {
6348ffdff6aSGreg Kroah-Hartman 		dev_warn(s->device->class_dev, "buffer overrun\n");
6358ffdff6aSGreg Kroah-Hartman 		s->async->events |= COMEDI_CB_OVERFLOW;
6368ffdff6aSGreg Kroah-Hartman 		nsamples = max_samples;
6378ffdff6aSGreg Kroah-Hartman 	}
6388ffdff6aSGreg Kroah-Hartman 
6398ffdff6aSGreg Kroah-Hartman 	if (nsamples == 0)
6408ffdff6aSGreg Kroah-Hartman 		return 0;
6418ffdff6aSGreg Kroah-Hartman 
6428ffdff6aSGreg Kroah-Hartman 	nbytes = comedi_buf_write_alloc(s,
6438ffdff6aSGreg Kroah-Hartman 					comedi_samples_to_bytes(s, nsamples));
6448ffdff6aSGreg Kroah-Hartman 	comedi_buf_memcpy_to(s, data, nbytes);
6458ffdff6aSGreg Kroah-Hartman 	comedi_buf_write_free(s, nbytes);
6468ffdff6aSGreg Kroah-Hartman 	comedi_inc_scan_progress(s, nbytes);
6478ffdff6aSGreg Kroah-Hartman 	s->async->events |= COMEDI_CB_BLOCK;
6488ffdff6aSGreg Kroah-Hartman 
6498ffdff6aSGreg Kroah-Hartman 	return nbytes;
6508ffdff6aSGreg Kroah-Hartman }
6518ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_write_samples);
6528ffdff6aSGreg Kroah-Hartman 
6538ffdff6aSGreg Kroah-Hartman /**
6548ffdff6aSGreg Kroah-Hartman  * comedi_buf_read_samples() - Read sample data from COMEDI buffer
6558ffdff6aSGreg Kroah-Hartman  * @s: COMEDI subdevice.
6568ffdff6aSGreg Kroah-Hartman  * @data: Pointer to destination.
6578ffdff6aSGreg Kroah-Hartman  * @nsamples: Maximum number of samples to read.
6588ffdff6aSGreg Kroah-Hartman  *
6598ffdff6aSGreg Kroah-Hartman  * Read up to @nsamples samples from the COMEDI acquisition data buffer
6608ffdff6aSGreg Kroah-Hartman  * associated with the subdevice, mark it as read and update the acquisition
6618ffdff6aSGreg Kroah-Hartman  * scan progress.  Limit the number of samples read to the number available.
6628ffdff6aSGreg Kroah-Hartman  * Set the %COMEDI_CB_BLOCK event flag if any samples are read to cause waiting
6638ffdff6aSGreg Kroah-Hartman  * tasks to be woken when the event flags are processed.
6648ffdff6aSGreg Kroah-Hartman  *
6658ffdff6aSGreg Kroah-Hartman  * Return: The amount of data read in bytes.
6668ffdff6aSGreg Kroah-Hartman  */
comedi_buf_read_samples(struct comedi_subdevice * s,void * data,unsigned int nsamples)6678ffdff6aSGreg Kroah-Hartman unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
6688ffdff6aSGreg Kroah-Hartman 				     void *data, unsigned int nsamples)
6698ffdff6aSGreg Kroah-Hartman {
6708ffdff6aSGreg Kroah-Hartman 	unsigned int max_samples;
6718ffdff6aSGreg Kroah-Hartman 	unsigned int nbytes;
6728ffdff6aSGreg Kroah-Hartman 
6738ffdff6aSGreg Kroah-Hartman 	/* clamp nsamples to the number of full samples available */
6748ffdff6aSGreg Kroah-Hartman 	max_samples = comedi_bytes_to_samples(s,
6758ffdff6aSGreg Kroah-Hartman 					      comedi_buf_read_n_available(s));
6768ffdff6aSGreg Kroah-Hartman 	if (nsamples > max_samples)
6778ffdff6aSGreg Kroah-Hartman 		nsamples = max_samples;
6788ffdff6aSGreg Kroah-Hartman 
6798ffdff6aSGreg Kroah-Hartman 	if (nsamples == 0)
6808ffdff6aSGreg Kroah-Hartman 		return 0;
6818ffdff6aSGreg Kroah-Hartman 
6828ffdff6aSGreg Kroah-Hartman 	nbytes = comedi_buf_read_alloc(s,
6838ffdff6aSGreg Kroah-Hartman 				       comedi_samples_to_bytes(s, nsamples));
6848ffdff6aSGreg Kroah-Hartman 	comedi_buf_memcpy_from(s, data, nbytes);
6858ffdff6aSGreg Kroah-Hartman 	comedi_buf_read_free(s, nbytes);
6868ffdff6aSGreg Kroah-Hartman 	comedi_inc_scan_progress(s, nbytes);
6878ffdff6aSGreg Kroah-Hartman 	s->async->events |= COMEDI_CB_BLOCK;
6888ffdff6aSGreg Kroah-Hartman 
6898ffdff6aSGreg Kroah-Hartman 	return nbytes;
6908ffdff6aSGreg Kroah-Hartman }
6918ffdff6aSGreg Kroah-Hartman EXPORT_SYMBOL_GPL(comedi_buf_read_samples);
692