blob: 06bfc859ab314d3670276b6512eefd01f91178a6 [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
H Hartley Sweetenea082fb2013-01-09 13:25:06 -07002/*
3 * comedi_buf.c
4 *
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
Ian Abbottb8d57652014-09-15 13:46:06 +01007 * Copyright (C) 2002 Frank Mori Hess <fmhess@users.sourceforge.net>
H Hartley Sweetenea082fb2013-01-09 13:25:06 -07008 */
9
H Hartley Sweeten76a1de32013-07-24 09:55:39 -070010#include <linux/vmalloc.h>
Ian Abbottaf93da32013-11-08 15:03:43 +000011#include <linux/slab.h>
H Hartley Sweeten76a1de32013-07-24 09:55:39 -070012
H Hartley Sweetenea082fb2013-01-09 13:25:06 -070013#include "comedidev.h"
14#include "comedi_internal.h"
15
H Hartley Sweeten6bd76452013-01-09 13:26:26 -070016#ifdef PAGE_KERNEL_NOCACHE
17#define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
18#else
19#define COMEDI_PAGE_PROTECTION PAGE_KERNEL
20#endif
21
Ian Abbottaf93da32013-11-08 15:03:43 +000022static void comedi_buf_map_kref_release(struct kref *kref)
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070023{
Ian Abbottaf93da32013-11-08 15:03:43 +000024 struct comedi_buf_map *bm =
25 container_of(kref, struct comedi_buf_map, refcount);
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070026 struct comedi_buf_page *buf;
Ian Abbottaf93da32013-11-08 15:03:43 +000027 unsigned int i;
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070028
Ian Abbottaf93da32013-11-08 15:03:43 +000029 if (bm->page_list) {
Ian Abbotte3647212019-06-25 12:26:59 +010030 if (bm->dma_dir != DMA_NONE) {
31 /*
32 * DMA buffer was allocated as a single block.
33 * Address is in page_list[0].
34 */
35 buf = &bm->page_list[0];
36 dma_free_coherent(bm->dma_hw_dev,
37 PAGE_SIZE * bm->n_pages,
38 buf->virt_addr, buf->dma_addr);
39 } else {
40 for (i = 0; i < bm->n_pages; i++) {
41 buf = &bm->page_list[i];
42 ClearPageReserved(virt_to_page(buf->virt_addr));
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070043 free_page((unsigned long)buf->virt_addr);
44 }
45 }
Ian Abbottaf93da32013-11-08 15:03:43 +000046 vfree(bm->page_list);
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070047 }
Ian Abbottaf93da32013-11-08 15:03:43 +000048 if (bm->dma_dir != DMA_NONE)
49 put_device(bm->dma_hw_dev);
50 kfree(bm);
51}
52
53static void __comedi_buf_free(struct comedi_device *dev,
54 struct comedi_subdevice *s)
55{
56 struct comedi_async *async = s->async;
Ian Abbottb34aa862014-04-10 19:41:57 +010057 struct comedi_buf_map *bm;
58 unsigned long flags;
Ian Abbottaf93da32013-11-08 15:03:43 +000059
60 if (async->prealloc_buf) {
Ian Abbotte3647212019-06-25 12:26:59 +010061 if (s->async_dma_dir == DMA_NONE)
62 vunmap(async->prealloc_buf);
Ian Abbottaf93da32013-11-08 15:03:43 +000063 async->prealloc_buf = NULL;
64 async->prealloc_bufsz = 0;
65 }
66
Ian Abbottb34aa862014-04-10 19:41:57 +010067 spin_lock_irqsave(&s->spin_lock, flags);
68 bm = async->buf_map;
Ian Abbottaf93da32013-11-08 15:03:43 +000069 async->buf_map = NULL;
Ian Abbottb34aa862014-04-10 19:41:57 +010070 spin_unlock_irqrestore(&s->spin_lock, flags);
71 comedi_buf_map_put(bm);
H Hartley Sweeten718c4d62013-01-09 13:25:54 -070072}
73
Ian Abbotte3647212019-06-25 12:26:59 +010074static struct comedi_buf_map *
75comedi_buf_map_alloc(struct comedi_device *dev, enum dma_data_direction dma_dir,
76 unsigned int n_pages)
77{
78 struct comedi_buf_map *bm;
79 struct comedi_buf_page *buf;
80 unsigned int i;
81
82 bm = kzalloc(sizeof(*bm), GFP_KERNEL);
83 if (!bm)
84 return NULL;
85
86 kref_init(&bm->refcount);
87 bm->dma_dir = dma_dir;
88 if (bm->dma_dir != DMA_NONE) {
89 /* Need ref to hardware device to free buffer later. */
90 bm->dma_hw_dev = get_device(dev->hw_dev);
91 }
92
93 bm->page_list = vzalloc(sizeof(*buf) * n_pages);
94 if (!bm->page_list)
95 goto err;
96
97 if (bm->dma_dir != DMA_NONE) {
98 void *virt_addr;
99 dma_addr_t dma_addr;
100
101 /*
102 * Currently, the DMA buffer needs to be allocated as a
103 * single block so that it can be mmap()'ed.
104 */
105 virt_addr = dma_alloc_coherent(bm->dma_hw_dev,
106 PAGE_SIZE * n_pages, &dma_addr,
107 GFP_KERNEL);
108 if (!virt_addr)
109 goto err;
110
111 for (i = 0; i < n_pages; i++) {
112 buf = &bm->page_list[i];
113 buf->virt_addr = virt_addr + (i << PAGE_SHIFT);
114 buf->dma_addr = dma_addr + (i << PAGE_SHIFT);
115 }
116
117 bm->n_pages = i;
118 } else {
119 for (i = 0; i < n_pages; i++) {
120 buf = &bm->page_list[i];
121 buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
122 if (!buf->virt_addr)
123 break;
124
125 SetPageReserved(virt_to_page(buf->virt_addr));
126 }
127
128 bm->n_pages = i;
129 if (i < n_pages)
130 goto err;
131 }
132
133 return bm;
134
135err:
136 comedi_buf_map_put(bm);
137 return NULL;
138}
139
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700140static void __comedi_buf_alloc(struct comedi_device *dev,
141 struct comedi_subdevice *s,
Leslie Klein938cae72016-03-20 20:26:12 -0400142 unsigned int n_pages)
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700143{
144 struct comedi_async *async = s->async;
145 struct page **pages = NULL;
Ian Abbottaf93da32013-11-08 15:03:43 +0000146 struct comedi_buf_map *bm;
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700147 struct comedi_buf_page *buf;
Ian Abbottb34aa862014-04-10 19:41:57 +0100148 unsigned long flags;
Leslie Klein938cae72016-03-20 20:26:12 -0400149 unsigned int i;
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700150
Ian Abbotte9166132013-05-10 14:07:16 +0100151 if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
152 dev_err(dev->class_dev,
153 "dma buffer allocation not supported\n");
154 return;
155 }
156
Ian Abbotte3647212019-06-25 12:26:59 +0100157 bm = comedi_buf_map_alloc(dev, s->async_dma_dir, n_pages);
Ian Abbottaf93da32013-11-08 15:03:43 +0000158 if (!bm)
159 return;
160
Ian Abbottb34aa862014-04-10 19:41:57 +0100161 spin_lock_irqsave(&s->spin_lock, flags);
162 async->buf_map = bm;
163 spin_unlock_irqrestore(&s->spin_lock, flags);
Ian Abbottaf93da32013-11-08 15:03:43 +0000164
Ian Abbotte3647212019-06-25 12:26:59 +0100165 if (bm->dma_dir != DMA_NONE) {
166 /*
167 * DMA buffer was allocated as a single block.
168 * Address is in page_list[0].
169 */
170 buf = &bm->page_list[0];
171 async->prealloc_buf = buf->virt_addr;
172 } else {
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700173 pages = vmalloc(sizeof(struct page *) * n_pages);
Ian Abbotte3647212019-06-25 12:26:59 +0100174 if (!pages)
175 return;
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700176
Ian Abbotte3647212019-06-25 12:26:59 +0100177 for (i = 0; i < n_pages; i++) {
178 buf = &bm->page_list[i];
179 pages[i] = virt_to_page(buf->virt_addr);
180 }
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700181
Ian Abbotte3647212019-06-25 12:26:59 +0100182 /* vmap the pages to prealloc_buf */
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700183 async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
184 COMEDI_PAGE_PROTECTION);
185
Ian Abbotte3647212019-06-25 12:26:59 +0100186 vfree(pages);
187 }
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700188}
189
Ian Abbottaf93da32013-11-08 15:03:43 +0000190void comedi_buf_map_get(struct comedi_buf_map *bm)
191{
192 if (bm)
193 kref_get(&bm->refcount);
194}
195
196int comedi_buf_map_put(struct comedi_buf_map *bm)
197{
198 if (bm)
199 return kref_put(&bm->refcount, comedi_buf_map_kref_release);
200 return 1;
201}
202
Ian Abbott255364f2017-04-20 19:05:14 +0100203/* helper for "access" vm operation */
204int comedi_buf_map_access(struct comedi_buf_map *bm, unsigned long offset,
205 void *buf, int len, int write)
206{
Amitoj Kaur Chawlaf47d8b12017-07-03 19:13:34 -0400207 unsigned int pgoff = offset_in_page(offset);
Ian Abbott255364f2017-04-20 19:05:14 +0100208 unsigned long pg = offset >> PAGE_SHIFT;
209 int done = 0;
210
211 while (done < len && pg < bm->n_pages) {
212 int l = min_t(int, len - done, PAGE_SIZE - pgoff);
213 void *b = bm->page_list[pg].virt_addr + pgoff;
214
215 if (write)
216 memcpy(b, buf, l);
217 else
218 memcpy(buf, b, l);
219 buf += l;
220 done += l;
221 pg++;
222 pgoff = 0;
223 }
224 return done;
225}
226
Ian Abbottb34aa862014-04-10 19:41:57 +0100227/* returns s->async->buf_map and increments its kref refcount */
228struct comedi_buf_map *
229comedi_buf_map_from_subdev_get(struct comedi_subdevice *s)
230{
231 struct comedi_async *async = s->async;
232 struct comedi_buf_map *bm = NULL;
233 unsigned long flags;
234
235 if (!async)
236 return NULL;
237
238 spin_lock_irqsave(&s->spin_lock, flags);
239 bm = async->buf_map;
240 /* only want it if buffer pages allocated */
241 if (bm && bm->n_pages)
242 comedi_buf_map_get(bm);
243 else
244 bm = NULL;
245 spin_unlock_irqrestore(&s->spin_lock, flags);
246
247 return bm;
248}
249
Ian Abbottd4526ab2014-05-06 13:12:11 +0100250bool comedi_buf_is_mmapped(struct comedi_subdevice *s)
Ian Abbottaf93da32013-11-08 15:03:43 +0000251{
Ian Abbottd4526ab2014-05-06 13:12:11 +0100252 struct comedi_buf_map *bm = s->async->buf_map;
Ian Abbottaf93da32013-11-08 15:03:43 +0000253
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100254 return bm && (kref_read(&bm->refcount) > 1);
Ian Abbottaf93da32013-11-08 15:03:43 +0000255}
256
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700257int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
258 unsigned long new_size)
259{
260 struct comedi_async *async = s->async;
261
Ian Abbott77c21b62019-04-17 15:39:29 +0100262 lockdep_assert_held(&dev->mutex);
263
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700264 /* Round up new_size to multiple of PAGE_SIZE */
265 new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
266
267 /* if no change is required, do nothing */
268 if (async->prealloc_buf && async->prealloc_bufsz == new_size)
269 return 0;
270
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700271 /* deallocate old buffer */
Ian Abbottaf93da32013-11-08 15:03:43 +0000272 __comedi_buf_free(dev, s);
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700273
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700274 /* allocate new buffer */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700275 if (new_size) {
Leslie Klein938cae72016-03-20 20:26:12 -0400276 unsigned int n_pages = new_size >> PAGE_SHIFT;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700277
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700278 __comedi_buf_alloc(dev, s, n_pages);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700279
H Hartley Sweeten718c4d62013-01-09 13:25:54 -0700280 if (!async->prealloc_buf) {
H Hartley Sweeten6bd76452013-01-09 13:26:26 -0700281 /* allocation failed */
Ian Abbottaf93da32013-11-08 15:03:43 +0000282 __comedi_buf_free(dev, s);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700283 return -ENOMEM;
284 }
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700285 }
286 async->prealloc_bufsz = new_size;
287
288 return 0;
289}
290
Ian Abbottfcc18a92014-05-06 13:12:10 +0100291void comedi_buf_reset(struct comedi_subdevice *s)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700292{
Ian Abbottfcc18a92014-05-06 13:12:10 +0100293 struct comedi_async *async = s->async;
294
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700295 async->buf_write_alloc_count = 0;
296 async->buf_write_count = 0;
297 async->buf_read_alloc_count = 0;
298 async->buf_read_count = 0;
299
300 async->buf_write_ptr = 0;
301 async->buf_read_ptr = 0;
302
303 async->cur_chan = 0;
H Hartley Sweeten1dacbe52014-11-05 10:20:52 -0700304 async->scans_done = 0;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700305 async->scan_progress = 0;
306 async->munge_chan = 0;
307 async->munge_count = 0;
308 async->munge_ptr = 0;
309
310 async->events = 0;
311}
312
Ian Abbott274ec5e2015-10-09 12:26:48 +0100313static unsigned int comedi_buf_write_n_unalloc(struct comedi_subdevice *s)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700314{
Ian Abbotta1c0e5f2014-05-06 13:12:14 +0100315 struct comedi_async *async = s->async;
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700316 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700317
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700318 return free_end - async->buf_write_alloc_count;
319}
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700320
Ian Abbott432fbde2015-10-09 12:26:49 +0100321unsigned int comedi_buf_write_n_available(struct comedi_subdevice *s)
322{
323 struct comedi_async *async = s->async;
324 unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
325
326 return free_end - async->buf_write_count;
327}
328
Ian Abbottc240e202015-09-22 18:02:39 +0100329/**
330 * comedi_buf_write_alloc() - Reserve buffer space for writing
331 * @s: COMEDI subdevice.
332 * @nbytes: Maximum space to reserve in bytes.
333 *
334 * Reserve up to @nbytes bytes of space to be written in the COMEDI acquisition
335 * data buffer associated with the subdevice. The amount reserved is limited
336 * by the space available.
337 *
338 * Return: The amount of space reserved in bytes.
339 */
H Hartley Sweeten0cf5efa2014-10-22 15:37:18 -0700340unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s,
341 unsigned int nbytes)
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700342{
Ian Abbott8ab4fe72014-05-06 13:12:13 +0100343 struct comedi_async *async = s->async;
Ian Abbott274ec5e2015-10-09 12:26:48 +0100344 unsigned int unalloc = comedi_buf_write_n_unalloc(s);
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700345
Ian Abbott274ec5e2015-10-09 12:26:48 +0100346 if (nbytes > unalloc)
347 nbytes = unalloc;
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700348
349 async->buf_write_alloc_count += nbytes;
350
351 /*
352 * ensure the async buffer 'counts' are read and updated
353 * before we write data to the write-alloc'ed buffer space
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700354 */
355 smp_mb();
H Hartley Sweetenf8f76e92013-01-09 13:27:48 -0700356
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700357 return nbytes;
358}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700359EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700360
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700361/*
362 * munging is applied to data by core as it passes between user
363 * and kernel space
364 */
Ian Abbott5b108582014-05-06 13:12:12 +0100365static unsigned int comedi_buf_munge(struct comedi_subdevice *s,
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700366 unsigned int num_bytes)
367{
Ian Abbott5b108582014-05-06 13:12:12 +0100368 struct comedi_async *async = s->async;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700369 unsigned int count = 0;
Leslie Klein938cae72016-03-20 20:26:12 -0400370 const unsigned int num_sample_bytes = comedi_bytes_per_sample(s);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700371
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700372 if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700373 async->munge_count += num_bytes;
Swen Kalskid70fb892021-03-25 14:35:20 +0100374 return num_bytes;
375 }
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700376
Swen Kalskid70fb892021-03-25 14:35:20 +0100377 /* don't munge partial samples */
378 num_bytes -= num_bytes % num_sample_bytes;
379 while (count < num_bytes) {
380 int block_size = num_bytes - count;
381 unsigned int buf_end;
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700382
Swen Kalskid70fb892021-03-25 14:35:20 +0100383 buf_end = async->prealloc_bufsz - async->munge_ptr;
384 if (block_size > buf_end)
385 block_size = buf_end;
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700386
Swen Kalskid70fb892021-03-25 14:35:20 +0100387 s->munge(s->device, s,
388 async->prealloc_buf + async->munge_ptr,
389 block_size, async->munge_chan);
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700390
Swen Kalskid70fb892021-03-25 14:35:20 +0100391 /*
392 * ensure data is munged in buffer before the
393 * async buffer munge_count is incremented
394 */
395 smp_wmb();
396
397 async->munge_chan += block_size / num_sample_bytes;
398 async->munge_chan %= async->cmd.chanlist_len;
399 async->munge_count += block_size;
400 async->munge_ptr += block_size;
401 async->munge_ptr %= async->prealloc_bufsz;
402 count += block_size;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700403 }
H Hartley Sweeten8d4be662013-01-09 13:29:19 -0700404
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700405 return count;
406}
407
Ian Abbott0f1f34e2014-05-06 13:12:06 +0100408unsigned int comedi_buf_write_n_allocated(struct comedi_subdevice *s)
H Hartley Sweeten8bd650f2013-01-09 13:32:18 -0700409{
Ian Abbott0f1f34e2014-05-06 13:12:06 +0100410 struct comedi_async *async = s->async;
411
H Hartley Sweeten8bd650f2013-01-09 13:32:18 -0700412 return async->buf_write_alloc_count - async->buf_write_count;
413}
414
Ian Abbottc240e202015-09-22 18:02:39 +0100415/**
416 * comedi_buf_write_free() - Free buffer space after it is written
417 * @s: COMEDI subdevice.
418 * @nbytes: Maximum space to free in bytes.
419 *
420 * Free up to @nbytes bytes of space previously reserved for writing in the
421 * COMEDI acquisition data buffer associated with the subdevice. The amount of
422 * space freed is limited to the amount that was reserved. The freed space is
423 * assumed to have been filled with sample data by the writer.
424 *
425 * If the samples in the freed space need to be "munged", do so here. The
426 * freed space becomes available for allocation by the reader.
427 *
428 * Return: The amount of space freed in bytes.
429 */
Ian Abbott940dd352014-05-06 13:12:05 +0100430unsigned int comedi_buf_write_free(struct comedi_subdevice *s,
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700431 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700432{
Ian Abbott940dd352014-05-06 13:12:05 +0100433 struct comedi_async *async = s->async;
Ian Abbott0f1f34e2014-05-06 13:12:06 +0100434 unsigned int allocated = comedi_buf_write_n_allocated(s);
H Hartley Sweetend21af4c2013-01-09 13:29:53 -0700435
H Hartley Sweeten6166ce82013-01-10 10:37:56 -0700436 if (nbytes > allocated)
H Hartley Sweetend21af4c2013-01-09 13:29:53 -0700437 nbytes = allocated;
H Hartley Sweeten6166ce82013-01-10 10:37:56 -0700438
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700439 async->buf_write_count += nbytes;
440 async->buf_write_ptr += nbytes;
Ian Abbott5b108582014-05-06 13:12:12 +0100441 comedi_buf_munge(s, async->buf_write_count - async->munge_count);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700442 if (async->buf_write_ptr >= async->prealloc_bufsz)
443 async->buf_write_ptr %= async->prealloc_bufsz;
444
445 return nbytes;
446}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700447EXPORT_SYMBOL_GPL(comedi_buf_write_free);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700448
Ian Abbottc240e202015-09-22 18:02:39 +0100449/**
450 * comedi_buf_read_n_available() - Determine amount of readable buffer space
451 * @s: COMEDI subdevice.
452 *
453 * Determine the amount of readable buffer space in the COMEDI acquisition data
454 * buffer associated with the subdevice. The readable buffer space is that
455 * which has been freed by the writer and "munged" to the sample data format
456 * expected by COMEDI if necessary.
457 *
458 * Return: The amount of readable buffer space.
459 */
Ian Abbotte9edef32014-05-06 13:12:09 +0100460unsigned int comedi_buf_read_n_available(struct comedi_subdevice *s)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700461{
Ian Abbotte9edef32014-05-06 13:12:09 +0100462 struct comedi_async *async = s->async;
Leslie Klein938cae72016-03-20 20:26:12 -0400463 unsigned int num_bytes;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700464
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700465 if (!async)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700466 return 0;
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700467
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700468 num_bytes = async->munge_count - async->buf_read_count;
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700469
470 /*
471 * ensure the async buffer 'counts' are read before we
472 * attempt to read data from the buffer
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700473 */
474 smp_rmb();
H Hartley Sweeten43f91372013-01-09 13:30:22 -0700475
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700476 return num_bytes;
477}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700478EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700479
Ian Abbottc240e202015-09-22 18:02:39 +0100480/**
481 * comedi_buf_read_alloc() - Reserve buffer space for reading
482 * @s: COMEDI subdevice.
483 * @nbytes: Maximum space to reserve in bytes.
484 *
485 * Reserve up to @nbytes bytes of previously written and "munged" buffer space
486 * for reading in the COMEDI acquisition data buffer associated with the
487 * subdevice. The amount reserved is limited to the space available. The
488 * reader can read from the reserved space and then free it. A reader is also
489 * allowed to read from the space before reserving it as long as it determines
490 * the amount of readable data available, but the space needs to be marked as
491 * reserved before it can be freed.
492 *
493 * Return: The amount of space reserved in bytes.
494 */
Ian Abbottd13be552014-05-06 13:12:07 +0100495unsigned int comedi_buf_read_alloc(struct comedi_subdevice *s,
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700496 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700497{
Ian Abbottd13be552014-05-06 13:12:07 +0100498 struct comedi_async *async = s->async;
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700499 unsigned int available;
500
501 available = async->munge_count - async->buf_read_alloc_count;
502 if (nbytes > available)
503 nbytes = available;
504
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700505 async->buf_read_alloc_count += nbytes;
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700506
507 /*
508 * ensure the async buffer 'counts' are read before we
509 * attempt to read data from the read-alloc'ed buffer space
510 */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700511 smp_rmb();
H Hartley Sweeten034cbd12013-01-09 13:30:49 -0700512
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700513 return nbytes;
514}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700515EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700516
H Hartley Sweeten5b2b64b2013-01-09 13:31:46 -0700517static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
518{
519 return async->buf_read_alloc_count - async->buf_read_count;
520}
521
Ian Abbottc240e202015-09-22 18:02:39 +0100522/**
523 * comedi_buf_read_free() - Free buffer space after it has been read
524 * @s: COMEDI subdevice.
525 * @nbytes: Maximum space to free in bytes.
526 *
527 * Free up to @nbytes bytes of buffer space previously reserved for reading in
528 * the COMEDI acquisition data buffer associated with the subdevice. The
529 * amount of space freed is limited to the amount that was reserved.
530 *
531 * The freed space becomes available for allocation by the writer.
532 *
533 * Return: The amount of space freed in bytes.
534 */
Ian Abbottf1df8662014-05-06 13:12:08 +0100535unsigned int comedi_buf_read_free(struct comedi_subdevice *s,
H Hartley Sweeten8ae560a2013-01-09 13:32:56 -0700536 unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700537{
Ian Abbottf1df8662014-05-06 13:12:08 +0100538 struct comedi_async *async = s->async;
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700539 unsigned int allocated;
540
541 /*
542 * ensure data has been read out of buffer before
543 * the async read count is incremented
544 */
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700545 smp_mb();
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700546
547 allocated = comedi_buf_read_n_allocated(async);
H Hartley Sweeten215040e2013-01-10 10:38:20 -0700548 if (nbytes > allocated)
H Hartley Sweeten3abfa102013-01-09 13:31:16 -0700549 nbytes = allocated;
H Hartley Sweeten215040e2013-01-10 10:38:20 -0700550
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700551 async->buf_read_count += nbytes;
552 async->buf_read_ptr += nbytes;
553 async->buf_read_ptr %= async->prealloc_bufsz;
554 return nbytes;
555}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700556EXPORT_SYMBOL_GPL(comedi_buf_read_free);
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700557
H Hartley Sweetenaa26e462014-10-22 14:36:49 -0700558static void comedi_buf_memcpy_to(struct comedi_subdevice *s,
H Hartley Sweetenaa26e462014-10-22 14:36:49 -0700559 const void *data, unsigned int num_bytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700560{
Ian Abbott00603a92014-05-06 13:12:01 +0100561 struct comedi_async *async = s->async;
H Hartley Sweeten319ad742014-10-22 14:36:50 -0700562 unsigned int write_ptr = async->buf_write_ptr;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700563
564 while (num_bytes) {
565 unsigned int block_size;
566
567 if (write_ptr + num_bytes > async->prealloc_bufsz)
568 block_size = async->prealloc_bufsz - write_ptr;
569 else
570 block_size = num_bytes;
571
572 memcpy(async->prealloc_buf + write_ptr, data, block_size);
573
574 data += block_size;
575 num_bytes -= block_size;
576
577 write_ptr = 0;
578 }
579}
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700580
H Hartley Sweeten9e2093d2014-10-22 14:36:48 -0700581static void comedi_buf_memcpy_from(struct comedi_subdevice *s,
H Hartley Sweeten9e2093d2014-10-22 14:36:48 -0700582 void *dest, unsigned int nbytes)
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700583{
584 void *src;
Ian Abbott2fadffc02014-05-06 13:12:02 +0100585 struct comedi_async *async = s->async;
H Hartley Sweeten0ca7f612014-10-22 14:36:51 -0700586 unsigned int read_ptr = async->buf_read_ptr;
H Hartley Sweetenea082fb2013-01-09 13:25:06 -0700587
588 while (nbytes) {
589 unsigned int block_size;
590
591 src = async->prealloc_buf + read_ptr;
592
593 if (nbytes >= async->prealloc_bufsz - read_ptr)
594 block_size = async->prealloc_bufsz - read_ptr;
595 else
596 block_size = nbytes;
597
598 memcpy(dest, src, block_size);
599 nbytes -= block_size;
600 dest += block_size;
601 read_ptr = 0;
602 }
603}
Ian Abbottea29c1d2014-09-15 13:46:03 +0100604
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700605/**
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100606 * comedi_buf_write_samples() - Write sample data to COMEDI buffer
607 * @s: COMEDI subdevice.
608 * @data: Pointer to source samples.
609 * @nsamples: Number of samples to write.
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700610 *
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100611 * Write up to @nsamples samples to the COMEDI acquisition data buffer
612 * associated with the subdevice, mark it as written and update the
613 * acquisition scan progress. If there is not enough room for the specified
614 * number of samples, the number of samples written is limited to the number
615 * that will fit and the %COMEDI_CB_OVERFLOW event flag is set to cause the
616 * acquisition to terminate with an overrun error. Set the %COMEDI_CB_BLOCK
617 * event flag if any samples are written to cause waiting tasks to be woken
618 * when the event flags are processed.
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700619 *
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100620 * Return: The amount of data written in bytes.
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700621 */
622unsigned int comedi_buf_write_samples(struct comedi_subdevice *s,
623 const void *data, unsigned int nsamples)
624{
625 unsigned int max_samples;
626 unsigned int nbytes;
627
H Hartley Sweeteneb3a1322014-10-30 11:10:50 -0700628 /*
629 * Make sure there is enough room in the buffer for all the samples.
630 * If not, clamp the nsamples to the number that will fit, flag the
631 * buffer overrun and add the samples that fit.
632 */
Ian Abbott274ec5e2015-10-09 12:26:48 +0100633 max_samples = comedi_bytes_to_samples(s, comedi_buf_write_n_unalloc(s));
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700634 if (nsamples > max_samples) {
635 dev_warn(s->device->class_dev, "buffer overrun\n");
636 s->async->events |= COMEDI_CB_OVERFLOW;
H Hartley Sweeteneb3a1322014-10-30 11:10:50 -0700637 nsamples = max_samples;
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700638 }
639
H Hartley Sweetene19a3c92014-10-22 15:37:17 -0700640 if (nsamples == 0)
641 return 0;
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700642
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700643 nbytes = comedi_buf_write_alloc(s,
644 comedi_samples_to_bytes(s, nsamples));
H Hartley Sweetene19a3c92014-10-22 15:37:17 -0700645 comedi_buf_memcpy_to(s, data, nbytes);
646 comedi_buf_write_free(s, nbytes);
647 comedi_inc_scan_progress(s, nbytes);
648 s->async->events |= COMEDI_CB_BLOCK;
649
650 return nbytes;
H Hartley Sweeten5438da82014-10-22 15:36:24 -0700651}
652EXPORT_SYMBOL_GPL(comedi_buf_write_samples);
653
654/**
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100655 * comedi_buf_read_samples() - Read sample data from COMEDI buffer
656 * @s: COMEDI subdevice.
657 * @data: Pointer to destination.
658 * @nsamples: Maximum number of samples to read.
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700659 *
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100660 * Read up to @nsamples samples from the COMEDI acquisition data buffer
661 * associated with the subdevice, mark it as read and update the acquisition
662 * scan progress. Limit the number of samples read to the number available.
663 * Set the %COMEDI_CB_BLOCK event flag if any samples are read to cause waiting
664 * tasks to be woken when the event flags are processed.
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700665 *
Ian Abbott67ecc3d2015-09-22 18:02:38 +0100666 * Return: The amount of data read in bytes.
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700667 */
668unsigned int comedi_buf_read_samples(struct comedi_subdevice *s,
669 void *data, unsigned int nsamples)
670{
671 unsigned int max_samples;
672 unsigned int nbytes;
673
H Hartley Sweeten8de27e72014-10-22 14:36:42 -0700674 /* clamp nsamples to the number of full samples available */
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700675 max_samples = comedi_bytes_to_samples(s,
676 comedi_buf_read_n_available(s));
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700677 if (nsamples > max_samples)
678 nsamples = max_samples;
679
H Hartley Sweeten8de27e72014-10-22 14:36:42 -0700680 if (nsamples == 0)
H Hartley Sweeten109bf062014-10-22 14:36:41 -0700681 return 0;
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700682
H Hartley Sweetenc39e0502014-10-31 12:04:28 -0700683 nbytes = comedi_buf_read_alloc(s,
684 comedi_samples_to_bytes(s, nsamples));
H Hartley Sweeten0ca7f612014-10-22 14:36:51 -0700685 comedi_buf_memcpy_from(s, data, nbytes);
H Hartley Sweeten109bf062014-10-22 14:36:41 -0700686 comedi_buf_read_free(s, nbytes);
687 comedi_inc_scan_progress(s, nbytes);
688 s->async->events |= COMEDI_CB_BLOCK;
689
690 return nbytes;
H Hartley Sweeten4455d7c2014-10-22 14:36:34 -0700691}
692EXPORT_SYMBOL_GPL(comedi_buf_read_samples);