Merge tag 'for-v3.13/clock-fixes-a' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / staging / comedi / comedi_buf.c
1 /*
2  * comedi_buf.c
3  *
4  * COMEDI - Linux Control and Measurement Device Interface
5  * Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
6  *
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.
11  *
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.
16  */
17
18 #include <linux/vmalloc.h>
19
20 #include "comedidev.h"
21 #include "comedi_internal.h"
22
23 #ifdef PAGE_KERNEL_NOCACHE
24 #define COMEDI_PAGE_PROTECTION          PAGE_KERNEL_NOCACHE
25 #else
26 #define COMEDI_PAGE_PROTECTION          PAGE_KERNEL
27 #endif
28
29 static void __comedi_buf_free(struct comedi_device *dev,
30                               struct comedi_subdevice *s,
31                               unsigned n_pages)
32 {
33         struct comedi_async *async = s->async;
34         struct comedi_buf_page *buf;
35         unsigned i;
36
37         if (async->prealloc_buf) {
38                 vunmap(async->prealloc_buf);
39                 async->prealloc_buf = NULL;
40                 async->prealloc_bufsz = 0;
41         }
42
43         if (!async->buf_page_list)
44                 return;
45
46         for (i = 0; i < n_pages; ++i) {
47                 buf = &async->buf_page_list[i];
48                 if (buf->virt_addr) {
49                         clear_bit(PG_reserved,
50                                   &(virt_to_page(buf->virt_addr)->flags));
51                         if (s->async_dma_dir != DMA_NONE) {
52 #ifdef CONFIG_HAS_DMA
53                                 dma_free_coherent(dev->hw_dev,
54                                                   PAGE_SIZE,
55                                                   buf->virt_addr,
56                                                   buf->dma_addr);
57 #endif
58                         } else {
59                                 free_page((unsigned long)buf->virt_addr);
60                         }
61                 }
62         }
63         vfree(async->buf_page_list);
64         async->buf_page_list = NULL;
65         async->n_buf_pages = 0;
66 }
67
68 static void __comedi_buf_alloc(struct comedi_device *dev,
69                                struct comedi_subdevice *s,
70                                unsigned n_pages)
71 {
72         struct comedi_async *async = s->async;
73         struct page **pages = NULL;
74         struct comedi_buf_page *buf;
75         unsigned i;
76
77         if (!IS_ENABLED(CONFIG_HAS_DMA) && s->async_dma_dir != DMA_NONE) {
78                 dev_err(dev->class_dev,
79                         "dma buffer allocation not supported\n");
80                 return;
81         }
82
83         async->buf_page_list = vzalloc(sizeof(*buf) * n_pages);
84         if (async->buf_page_list)
85                 pages = vmalloc(sizeof(struct page *) * n_pages);
86
87         if (!pages)
88                 return;
89
90         for (i = 0; i < n_pages; i++) {
91                 buf = &async->buf_page_list[i];
92                 if (s->async_dma_dir != DMA_NONE)
93 #ifdef CONFIG_HAS_DMA
94                         buf->virt_addr = dma_alloc_coherent(dev->hw_dev,
95                                                             PAGE_SIZE,
96                                                             &buf->dma_addr,
97                                                             GFP_KERNEL |
98                                                             __GFP_COMP);
99 #else
100                         break;
101 #endif
102                 else
103                         buf->virt_addr = (void *)get_zeroed_page(GFP_KERNEL);
104                 if (!buf->virt_addr)
105                         break;
106
107                 set_bit(PG_reserved, &(virt_to_page(buf->virt_addr)->flags));
108
109                 pages[i] = virt_to_page(buf->virt_addr);
110         }
111
112         /* vmap the prealloc_buf if all the pages were allocated */
113         if (i == n_pages)
114                 async->prealloc_buf = vmap(pages, n_pages, VM_MAP,
115                                            COMEDI_PAGE_PROTECTION);
116
117         vfree(pages);
118 }
119
120 int comedi_buf_alloc(struct comedi_device *dev, struct comedi_subdevice *s,
121                      unsigned long new_size)
122 {
123         struct comedi_async *async = s->async;
124
125         /* Round up new_size to multiple of PAGE_SIZE */
126         new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
127
128         /* if no change is required, do nothing */
129         if (async->prealloc_buf && async->prealloc_bufsz == new_size)
130                 return 0;
131
132         /* deallocate old buffer */
133         __comedi_buf_free(dev, s, async->n_buf_pages);
134
135         /* allocate new buffer */
136         if (new_size) {
137                 unsigned n_pages = new_size >> PAGE_SHIFT;
138
139                 __comedi_buf_alloc(dev, s, n_pages);
140
141                 if (!async->prealloc_buf) {
142                         /* allocation failed */
143                         __comedi_buf_free(dev, s, n_pages);
144                         return -ENOMEM;
145                 }
146                 async->n_buf_pages = n_pages;
147         }
148         async->prealloc_bufsz = new_size;
149
150         return 0;
151 }
152
153 void comedi_buf_reset(struct comedi_async *async)
154 {
155         async->buf_write_alloc_count = 0;
156         async->buf_write_count = 0;
157         async->buf_read_alloc_count = 0;
158         async->buf_read_count = 0;
159
160         async->buf_write_ptr = 0;
161         async->buf_read_ptr = 0;
162
163         async->cur_chan = 0;
164         async->scan_progress = 0;
165         async->munge_chan = 0;
166         async->munge_count = 0;
167         async->munge_ptr = 0;
168
169         async->events = 0;
170 }
171
172 static unsigned int comedi_buf_write_n_available(struct comedi_async *async)
173 {
174         unsigned int free_end = async->buf_read_count + async->prealloc_bufsz;
175
176         return free_end - async->buf_write_alloc_count;
177 }
178
179 static unsigned int __comedi_buf_write_alloc(struct comedi_async *async,
180                                              unsigned int nbytes,
181                                              int strict)
182 {
183         unsigned int available = comedi_buf_write_n_available(async);
184
185         if (nbytes > available)
186                 nbytes = strict ? 0 : available;
187
188         async->buf_write_alloc_count += nbytes;
189
190         /*
191          * ensure the async buffer 'counts' are read and updated
192          * before we write data to the write-alloc'ed buffer space
193          */
194         smp_mb();
195
196         return nbytes;
197 }
198
199 /* allocates chunk for the writer from free buffer space */
200 unsigned int comedi_buf_write_alloc(struct comedi_async *async,
201                                     unsigned int nbytes)
202 {
203         return __comedi_buf_write_alloc(async, nbytes, 0);
204 }
205 EXPORT_SYMBOL_GPL(comedi_buf_write_alloc);
206
207 /*
208  * munging is applied to data by core as it passes between user
209  * and kernel space
210  */
211 static unsigned int comedi_buf_munge(struct comedi_async *async,
212                                      unsigned int num_bytes)
213 {
214         struct comedi_subdevice *s = async->subdevice;
215         unsigned int count = 0;
216         const unsigned num_sample_bytes = bytes_per_sample(s);
217
218         if (!s->munge || (async->cmd.flags & CMDF_RAWDATA)) {
219                 async->munge_count += num_bytes;
220                 count = num_bytes;
221         } else {
222                 /* don't munge partial samples */
223                 num_bytes -= num_bytes % num_sample_bytes;
224                 while (count < num_bytes) {
225                         int block_size = num_bytes - count;
226                         unsigned int buf_end;
227
228                         buf_end = async->prealloc_bufsz - async->munge_ptr;
229                         if (block_size > buf_end)
230                                 block_size = buf_end;
231
232                         s->munge(s->device, s,
233                                  async->prealloc_buf + async->munge_ptr,
234                                  block_size, async->munge_chan);
235
236                         /*
237                          * ensure data is munged in buffer before the
238                          * async buffer munge_count is incremented
239                          */
240                         smp_wmb();
241
242                         async->munge_chan += block_size / num_sample_bytes;
243                         async->munge_chan %= async->cmd.chanlist_len;
244                         async->munge_count += block_size;
245                         async->munge_ptr += block_size;
246                         async->munge_ptr %= async->prealloc_bufsz;
247                         count += block_size;
248                 }
249         }
250
251         return count;
252 }
253
254 unsigned int comedi_buf_write_n_allocated(struct comedi_async *async)
255 {
256         return async->buf_write_alloc_count - async->buf_write_count;
257 }
258
259 /* transfers a chunk from writer to filled buffer space */
260 unsigned int comedi_buf_write_free(struct comedi_async *async,
261                                    unsigned int nbytes)
262 {
263         unsigned int allocated = comedi_buf_write_n_allocated(async);
264
265         if (nbytes > allocated)
266                 nbytes = allocated;
267
268         async->buf_write_count += nbytes;
269         async->buf_write_ptr += nbytes;
270         comedi_buf_munge(async, async->buf_write_count - async->munge_count);
271         if (async->buf_write_ptr >= async->prealloc_bufsz)
272                 async->buf_write_ptr %= async->prealloc_bufsz;
273
274         return nbytes;
275 }
276 EXPORT_SYMBOL_GPL(comedi_buf_write_free);
277
278 unsigned int comedi_buf_read_n_available(struct comedi_async *async)
279 {
280         unsigned num_bytes;
281
282         if (!async)
283                 return 0;
284
285         num_bytes = async->munge_count - async->buf_read_count;
286
287         /*
288          * ensure the async buffer 'counts' are read before we
289          * attempt to read data from the buffer
290          */
291         smp_rmb();
292
293         return num_bytes;
294 }
295 EXPORT_SYMBOL_GPL(comedi_buf_read_n_available);
296
297 /* allocates a chunk for the reader from filled (and munged) buffer space */
298 unsigned int comedi_buf_read_alloc(struct comedi_async *async,
299                                    unsigned int nbytes)
300 {
301         unsigned int available;
302
303         available = async->munge_count - async->buf_read_alloc_count;
304         if (nbytes > available)
305                 nbytes = available;
306
307         async->buf_read_alloc_count += nbytes;
308
309         /*
310          * ensure the async buffer 'counts' are read before we
311          * attempt to read data from the read-alloc'ed buffer space
312          */
313         smp_rmb();
314
315         return nbytes;
316 }
317 EXPORT_SYMBOL_GPL(comedi_buf_read_alloc);
318
319 static unsigned int comedi_buf_read_n_allocated(struct comedi_async *async)
320 {
321         return async->buf_read_alloc_count - async->buf_read_count;
322 }
323
324 /* transfers control of a chunk from reader to free buffer space */
325 unsigned int comedi_buf_read_free(struct comedi_async *async,
326                                   unsigned int nbytes)
327 {
328         unsigned int allocated;
329
330         /*
331          * ensure data has been read out of buffer before
332          * the async read count is incremented
333          */
334         smp_mb();
335
336         allocated = comedi_buf_read_n_allocated(async);
337         if (nbytes > allocated)
338                 nbytes = allocated;
339
340         async->buf_read_count += nbytes;
341         async->buf_read_ptr += nbytes;
342         async->buf_read_ptr %= async->prealloc_bufsz;
343         return nbytes;
344 }
345 EXPORT_SYMBOL_GPL(comedi_buf_read_free);
346
347 int comedi_buf_put(struct comedi_async *async, unsigned short x)
348 {
349         unsigned int n = __comedi_buf_write_alloc(async, sizeof(short), 1);
350
351         if (n < sizeof(short)) {
352                 async->events |= COMEDI_CB_ERROR;
353                 return 0;
354         }
355         *(unsigned short *)(async->prealloc_buf + async->buf_write_ptr) = x;
356         comedi_buf_write_free(async, sizeof(short));
357         return 1;
358 }
359 EXPORT_SYMBOL_GPL(comedi_buf_put);
360
361 int comedi_buf_get(struct comedi_async *async, unsigned short *x)
362 {
363         unsigned int n = comedi_buf_read_n_available(async);
364
365         if (n < sizeof(short))
366                 return 0;
367         comedi_buf_read_alloc(async, sizeof(short));
368         *x = *(unsigned short *)(async->prealloc_buf + async->buf_read_ptr);
369         comedi_buf_read_free(async, sizeof(short));
370         return 1;
371 }
372 EXPORT_SYMBOL_GPL(comedi_buf_get);
373
374 void comedi_buf_memcpy_to(struct comedi_async *async, unsigned int offset,
375                           const void *data, unsigned int num_bytes)
376 {
377         unsigned int write_ptr = async->buf_write_ptr + offset;
378
379         if (write_ptr >= async->prealloc_bufsz)
380                 write_ptr %= async->prealloc_bufsz;
381
382         while (num_bytes) {
383                 unsigned int block_size;
384
385                 if (write_ptr + num_bytes > async->prealloc_bufsz)
386                         block_size = async->prealloc_bufsz - write_ptr;
387                 else
388                         block_size = num_bytes;
389
390                 memcpy(async->prealloc_buf + write_ptr, data, block_size);
391
392                 data += block_size;
393                 num_bytes -= block_size;
394
395                 write_ptr = 0;
396         }
397 }
398 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_to);
399
400 void comedi_buf_memcpy_from(struct comedi_async *async, unsigned int offset,
401                             void *dest, unsigned int nbytes)
402 {
403         void *src;
404         unsigned int read_ptr = async->buf_read_ptr + offset;
405
406         if (read_ptr >= async->prealloc_bufsz)
407                 read_ptr %= async->prealloc_bufsz;
408
409         while (nbytes) {
410                 unsigned int block_size;
411
412                 src = async->prealloc_buf + read_ptr;
413
414                 if (nbytes >= async->prealloc_bufsz - read_ptr)
415                         block_size = async->prealloc_bufsz - read_ptr;
416                 else
417                         block_size = nbytes;
418
419                 memcpy(dest, src, block_size);
420                 nbytes -= block_size;
421                 dest += block_size;
422                 read_ptr = 0;
423         }
424 }
425 EXPORT_SYMBOL_GPL(comedi_buf_memcpy_from);