4 * COMEDI - Linux Control and Measurement Device Interface
5 * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include "comedidev.h"
19 #include "comedi_internal.h"
21 #ifdef PAGE_KERNEL_NOCACHE
22 #define COMEDI_PAGE_PROTECTION PAGE_KERNEL_NOCACHE
24 #define COMEDI_PAGE_PROTECTION PAGE_KERNEL
27 static void __comedi_buf_free(struct comedi_device
*dev
,
28 struct comedi_subdevice
*s
,
31 struct comedi_async
*async
= s
->async
;
32 struct comedi_buf_page
*buf
;
35 if (async
->prealloc_buf
) {
36 vunmap(async
->prealloc_buf
);
37 async
->prealloc_buf
= NULL
;
38 async
->prealloc_bufsz
= 0;
41 if (!async
->buf_page_list
)
44 for (i
= 0; i
< n_pages
; ++i
) {
45 buf
= &async
->buf_page_list
[i
];
47 clear_bit(PG_reserved
,
48 &(virt_to_page(buf
->virt_addr
)->flags
));
49 if (s
->async_dma_dir
!= DMA_NONE
) {
51 dma_free_coherent(dev
->hw_dev
,
57 free_page((unsigned long)buf
->virt_addr
);
61 vfree(async
->buf_page_list
);
62 async
->buf_page_list
= NULL
;
63 async
->n_buf_pages
= 0;
66 static void __comedi_buf_alloc(struct comedi_device
*dev
,
67 struct comedi_subdevice
*s
,
70 struct comedi_async
*async
= s
->async
;
71 struct page
**pages
= NULL
;
72 struct comedi_buf_page
*buf
;
75 if (!IS_ENABLED(CONFIG_HAS_DMA
) && s
->async_dma_dir
!= DMA_NONE
) {
76 dev_err(dev
->class_dev
,
77 "dma buffer allocation not supported\n");
81 async
->buf_page_list
= vzalloc(sizeof(*buf
) * n_pages
);
82 if (async
->buf_page_list
)
83 pages
= vmalloc(sizeof(struct page
*) * n_pages
);
88 for (i
= 0; i
< n_pages
; i
++) {
89 buf
= &async
->buf_page_list
[i
];
90 if (s
->async_dma_dir
!= DMA_NONE
)
92 buf
->virt_addr
= dma_alloc_coherent(dev
->hw_dev
,
101 buf
->virt_addr
= (void *)get_zeroed_page(GFP_KERNEL
);
105 set_bit(PG_reserved
, &(virt_to_page(buf
->virt_addr
)->flags
));
107 pages
[i
] = virt_to_page(buf
->virt_addr
);
110 /* vmap the prealloc_buf if all the pages were allocated */
112 async
->prealloc_buf
= vmap(pages
, n_pages
, VM_MAP
,
113 COMEDI_PAGE_PROTECTION
);
118 int comedi_buf_alloc(struct comedi_device
*dev
, struct comedi_subdevice
*s
,
119 unsigned long new_size
)
121 struct comedi_async
*async
= s
->async
;
123 /* Round up new_size to multiple of PAGE_SIZE */
124 new_size
= (new_size
+ PAGE_SIZE
- 1) & PAGE_MASK
;
126 /* if no change is required, do nothing */
127 if (async
->prealloc_buf
&& async
->prealloc_bufsz
== new_size
)
130 /* deallocate old buffer */
131 __comedi_buf_free(dev
, s
, async
->n_buf_pages
);
133 /* allocate new buffer */
135 unsigned n_pages
= new_size
>> PAGE_SHIFT
;
137 __comedi_buf_alloc(dev
, s
, n_pages
);
139 if (!async
->prealloc_buf
) {
140 /* allocation failed */
141 __comedi_buf_free(dev
, s
, n_pages
);
144 async
->n_buf_pages
= n_pages
;
146 async
->prealloc_bufsz
= new_size
;
151 void comedi_buf_reset(struct comedi_async
*async
)
153 async
->buf_write_alloc_count
= 0;
154 async
->buf_write_count
= 0;
155 async
->buf_read_alloc_count
= 0;
156 async
->buf_read_count
= 0;
158 async
->buf_write_ptr
= 0;
159 async
->buf_read_ptr
= 0;
162 async
->scan_progress
= 0;
163 async
->munge_chan
= 0;
164 async
->munge_count
= 0;
165 async
->munge_ptr
= 0;
170 static unsigned int comedi_buf_write_n_available(struct comedi_async
*async
)
172 unsigned int free_end
= async
->buf_read_count
+ async
->prealloc_bufsz
;
174 return free_end
- async
->buf_write_alloc_count
;
177 static unsigned int __comedi_buf_write_alloc(struct comedi_async
*async
,
181 unsigned int available
= comedi_buf_write_n_available(async
);
183 if (nbytes
> available
)
184 nbytes
= strict
? 0 : available
;
186 async
->buf_write_alloc_count
+= nbytes
;
189 * ensure the async buffer 'counts' are read and updated
190 * before we write data to the write-alloc'ed buffer space
197 /* allocates chunk for the writer from free buffer space */
198 unsigned int comedi_buf_write_alloc(struct comedi_async
*async
,
201 return __comedi_buf_write_alloc(async
, nbytes
, 0);
203 EXPORT_SYMBOL_GPL(comedi_buf_write_alloc
);
206 * munging is applied to data by core as it passes between user
209 static unsigned int comedi_buf_munge(struct comedi_async
*async
,
210 unsigned int num_bytes
)
212 struct comedi_subdevice
*s
= async
->subdevice
;
213 unsigned int count
= 0;
214 const unsigned num_sample_bytes
= bytes_per_sample(s
);
216 if (!s
->munge
|| (async
->cmd
.flags
& CMDF_RAWDATA
)) {
217 async
->munge_count
+= num_bytes
;
220 /* don't munge partial samples */
221 num_bytes
-= num_bytes
% num_sample_bytes
;
222 while (count
< num_bytes
) {
223 int block_size
= num_bytes
- count
;
224 unsigned int buf_end
;
226 buf_end
= async
->prealloc_bufsz
- async
->munge_ptr
;
227 if (block_size
> buf_end
)
228 block_size
= buf_end
;
230 s
->munge(s
->device
, s
,
231 async
->prealloc_buf
+ async
->munge_ptr
,
232 block_size
, async
->munge_chan
);
235 * ensure data is munged in buffer before the
236 * async buffer munge_count is incremented
240 async
->munge_chan
+= block_size
/ num_sample_bytes
;
241 async
->munge_chan
%= async
->cmd
.chanlist_len
;
242 async
->munge_count
+= block_size
;
243 async
->munge_ptr
+= block_size
;
244 async
->munge_ptr
%= async
->prealloc_bufsz
;
252 unsigned int comedi_buf_write_n_allocated(struct comedi_async
*async
)
254 return async
->buf_write_alloc_count
- async
->buf_write_count
;
257 /* transfers a chunk from writer to filled buffer space */
258 unsigned int comedi_buf_write_free(struct comedi_async
*async
,
261 unsigned int allocated
= comedi_buf_write_n_allocated(async
);
263 if (nbytes
> allocated
)
266 async
->buf_write_count
+= nbytes
;
267 async
->buf_write_ptr
+= nbytes
;
268 comedi_buf_munge(async
, async
->buf_write_count
- async
->munge_count
);
269 if (async
->buf_write_ptr
>= async
->prealloc_bufsz
)
270 async
->buf_write_ptr
%= async
->prealloc_bufsz
;
274 EXPORT_SYMBOL_GPL(comedi_buf_write_free
);
276 unsigned int comedi_buf_read_n_available(struct comedi_async
*async
)
283 num_bytes
= async
->munge_count
- async
->buf_read_count
;
286 * ensure the async buffer 'counts' are read before we
287 * attempt to read data from the buffer
293 EXPORT_SYMBOL_GPL(comedi_buf_read_n_available
);
295 /* allocates a chunk for the reader from filled (and munged) buffer space */
296 unsigned int comedi_buf_read_alloc(struct comedi_async
*async
,
299 unsigned int available
;
301 available
= async
->munge_count
- async
->buf_read_alloc_count
;
302 if (nbytes
> available
)
305 async
->buf_read_alloc_count
+= nbytes
;
308 * ensure the async buffer 'counts' are read before we
309 * attempt to read data from the read-alloc'ed buffer space
315 EXPORT_SYMBOL_GPL(comedi_buf_read_alloc
);
317 static unsigned int comedi_buf_read_n_allocated(struct comedi_async
*async
)
319 return async
->buf_read_alloc_count
- async
->buf_read_count
;
322 /* transfers control of a chunk from reader to free buffer space */
323 unsigned int comedi_buf_read_free(struct comedi_async
*async
,
326 unsigned int allocated
;
329 * ensure data has been read out of buffer before
330 * the async read count is incremented
334 allocated
= comedi_buf_read_n_allocated(async
);
335 if (nbytes
> allocated
)
338 async
->buf_read_count
+= nbytes
;
339 async
->buf_read_ptr
+= nbytes
;
340 async
->buf_read_ptr
%= async
->prealloc_bufsz
;
343 EXPORT_SYMBOL_GPL(comedi_buf_read_free
);
345 int comedi_buf_put(struct comedi_async
*async
, short x
)
347 unsigned int n
= __comedi_buf_write_alloc(async
, sizeof(short), 1);
349 if (n
< sizeof(short)) {
350 async
->events
|= COMEDI_CB_ERROR
;
353 *(short *)(async
->prealloc_buf
+ async
->buf_write_ptr
) = x
;
354 comedi_buf_write_free(async
, sizeof(short));
357 EXPORT_SYMBOL_GPL(comedi_buf_put
);
359 int comedi_buf_get(struct comedi_async
*async
, short *x
)
361 unsigned int n
= comedi_buf_read_n_available(async
);
363 if (n
< sizeof(short))
365 comedi_buf_read_alloc(async
, sizeof(short));
366 *x
= *(short *)(async
->prealloc_buf
+ async
->buf_read_ptr
);
367 comedi_buf_read_free(async
, sizeof(short));
370 EXPORT_SYMBOL_GPL(comedi_buf_get
);
372 void comedi_buf_memcpy_to(struct comedi_async
*async
, unsigned int offset
,
373 const void *data
, unsigned int num_bytes
)
375 unsigned int write_ptr
= async
->buf_write_ptr
+ offset
;
377 if (write_ptr
>= async
->prealloc_bufsz
)
378 write_ptr
%= async
->prealloc_bufsz
;
381 unsigned int block_size
;
383 if (write_ptr
+ num_bytes
> async
->prealloc_bufsz
)
384 block_size
= async
->prealloc_bufsz
- write_ptr
;
386 block_size
= num_bytes
;
388 memcpy(async
->prealloc_buf
+ write_ptr
, data
, block_size
);
391 num_bytes
-= block_size
;
396 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_to
);
398 void comedi_buf_memcpy_from(struct comedi_async
*async
, unsigned int offset
,
399 void *dest
, unsigned int nbytes
)
402 unsigned int read_ptr
= async
->buf_read_ptr
+ offset
;
404 if (read_ptr
>= async
->prealloc_bufsz
)
405 read_ptr
%= async
->prealloc_bufsz
;
408 unsigned int block_size
;
410 src
= async
->prealloc_buf
+ read_ptr
;
412 if (nbytes
>= async
->prealloc_bufsz
- read_ptr
)
413 block_size
= async
->prealloc_bufsz
- read_ptr
;
417 memcpy(dest
, src
, block_size
);
418 nbytes
-= block_size
;
423 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_from
);