blob: a94227c5555109bfcfc14ab9a750cd0761680470 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefani Seibold2e956fb2010-08-10 18:03:38 -07002 * A generic kernel FIFO implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Stefani Seibold2e956fb2010-08-10 18:03:38 -07004 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Stefani Seiboldd78a3ed2010-08-11 14:17:27 -070013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040023#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/slab.h>
25#include <linux/err.h>
vignesh babuf84d5a72007-07-15 23:41:34 -070026#include <linux/log2.h>
Stefani Seibolda121f242009-12-21 14:37:31 -080027#include <linux/uaccess.h>
Stefani Seibold2e956fb2010-08-10 18:03:38 -070028#include <linux/kfifo.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Stefani Seibold2e956fb2010-08-10 18:03:38 -070030/*
31 * internal helper to calculate the unused elements in a fifo
32 */
33static inline unsigned int kfifo_unused(struct __kfifo *fifo)
Stefani Seibold45465482009-12-21 14:37:26 -080034{
Stefani Seibold2e956fb2010-08-10 18:03:38 -070035 return (fifo->mask + 1) - (fifo->in - fifo->out);
Stefani Seibold45465482009-12-21 14:37:26 -080036}
37
Stefani Seibold2e956fb2010-08-10 18:03:38 -070038int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
39 size_t esize, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /*
Stefani Seibold2e956fb2010-08-10 18:03:38 -070042 * round down to the next power of 2, since our 'let the indices
Robert P. J. Dayb33112d2009-06-16 15:33:34 -070043 * wrap' technique works only in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
Stefani Seibolddfe2a772013-02-27 17:05:51 -080045 size = roundup_pow_of_two(size);
Stefani Seibold2e956fb2010-08-10 18:03:38 -070046
47 fifo->in = 0;
48 fifo->out = 0;
49 fifo->esize = esize;
50
51 if (size < 2) {
52 fifo->data = NULL;
53 fifo->mask = 0;
54 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56
Stefani Seibold2e956fb2010-08-10 18:03:38 -070057 fifo->data = kmalloc(size * esize, gfp_mask);
58
59 if (!fifo->data) {
60 fifo->mask = 0;
Stefani Seibold45465482009-12-21 14:37:26 -080061 return -ENOMEM;
62 }
Stefani Seibold2e956fb2010-08-10 18:03:38 -070063 fifo->mask = size - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Stefani Seibold45465482009-12-21 14:37:26 -080065 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
Stefani Seibold2e956fb2010-08-10 18:03:38 -070067EXPORT_SYMBOL(__kfifo_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Stefani Seibold2e956fb2010-08-10 18:03:38 -070069void __kfifo_free(struct __kfifo *fifo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Stefani Seibold2e956fb2010-08-10 18:03:38 -070071 kfree(fifo->data);
72 fifo->in = 0;
73 fifo->out = 0;
74 fifo->esize = 0;
75 fifo->data = NULL;
76 fifo->mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Stefani Seibold2e956fb2010-08-10 18:03:38 -070078EXPORT_SYMBOL(__kfifo_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Stefani Seibold2e956fb2010-08-10 18:03:38 -070080int __kfifo_init(struct __kfifo *fifo, void *buffer,
81 unsigned int size, size_t esize)
Stefani Seibolda121f242009-12-21 14:37:31 -080082{
Stefani Seibold2e956fb2010-08-10 18:03:38 -070083 size /= esize;
84
Linus Torvalds1d06b242019-08-30 18:47:15 -070085 if (!is_power_of_2(size))
86 size = rounddown_pow_of_two(size);
Stefani Seibold2e956fb2010-08-10 18:03:38 -070087
88 fifo->in = 0;
89 fifo->out = 0;
90 fifo->esize = esize;
91 fifo->data = buffer;
92
93 if (size < 2) {
94 fifo->mask = 0;
95 return -EINVAL;
Stefani Seibolda121f242009-12-21 14:37:31 -080096 }
Stefani Seibold2e956fb2010-08-10 18:03:38 -070097 fifo->mask = size - 1;
Stefani Seibolda121f242009-12-21 14:37:31 -080098
Andi Kleen64ce1032010-01-15 17:01:15 -080099 return 0;
Stefani Seibold86d48802009-12-21 14:37:32 -0800100}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700101EXPORT_SYMBOL(__kfifo_init);
Stefani Seibold86d48802009-12-21 14:37:32 -0800102
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700103static void kfifo_copy_in(struct __kfifo *fifo, const void *src,
104 unsigned int len, unsigned int off)
Stefani Seibold86d48802009-12-21 14:37:32 -0800105{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700106 unsigned int size = fifo->mask + 1;
107 unsigned int esize = fifo->esize;
108 unsigned int l;
Stefani Seibold86d48802009-12-21 14:37:32 -0800109
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700110 off &= fifo->mask;
111 if (esize != 1) {
112 off *= esize;
113 size *= esize;
114 len *= esize;
115 }
116 l = min(len, size - off);
117
118 memcpy(fifo->data + off, src, l);
119 memcpy(fifo->data, src + l, len - l);
120 /*
121 * make sure that the data in the fifo is up to date before
122 * incrementing the fifo->in index counter
123 */
124 smp_wmb();
Stefani Seibold86d48802009-12-21 14:37:32 -0800125}
Stefani Seibold86d48802009-12-21 14:37:32 -0800126
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700127unsigned int __kfifo_in(struct __kfifo *fifo,
128 const void *buf, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700130 unsigned int l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700132 l = kfifo_unused(fifo);
133 if (len > l)
134 len = l;
135
136 kfifo_copy_in(fifo, buf, len, fifo->in);
137 fifo->in += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return len;
139}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700140EXPORT_SYMBOL(__kfifo_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700142static void kfifo_copy_out(struct __kfifo *fifo, void *dst,
143 unsigned int len, unsigned int off)
Stefani Seibold86d48802009-12-21 14:37:32 -0800144{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700145 unsigned int size = fifo->mask + 1;
146 unsigned int esize = fifo->esize;
147 unsigned int l;
148
149 off &= fifo->mask;
150 if (esize != 1) {
151 off *= esize;
152 size *= esize;
153 len *= esize;
154 }
155 l = min(len, size - off);
156
157 memcpy(dst, fifo->data + off, l);
158 memcpy(dst + l, fifo->data, len - l);
159 /*
160 * make sure that the data is copied before
161 * incrementing the fifo->out index counter
162 */
163 smp_wmb();
Stefani Seibold86d48802009-12-21 14:37:32 -0800164}
Stefani Seibold86d48802009-12-21 14:37:32 -0800165
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700166unsigned int __kfifo_out_peek(struct __kfifo *fifo,
167 void *buf, unsigned int len)
Stefani Seibold86d48802009-12-21 14:37:32 -0800168{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700169 unsigned int l;
Stefani Seibold86d48802009-12-21 14:37:32 -0800170
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700171 l = fifo->in - fifo->out;
172 if (len > l)
173 len = l;
Stefani Seibold86d48802009-12-21 14:37:32 -0800174
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700175 kfifo_copy_out(fifo, buf, len, fifo->out);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 return len;
177}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700178EXPORT_SYMBOL(__kfifo_out_peek);
Stefani Seibolda121f242009-12-21 14:37:31 -0800179
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700180unsigned int __kfifo_out(struct __kfifo *fifo,
181 void *buf, unsigned int len)
Andi Kleena5b9e2c2010-01-15 17:01:16 -0800182{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700183 len = __kfifo_out_peek(fifo, buf, len);
184 fifo->out += len;
Andi Kleena5b9e2c2010-01-15 17:01:16 -0800185 return len;
186}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700187EXPORT_SYMBOL(__kfifo_out);
Andi Kleena5b9e2c2010-01-15 17:01:16 -0800188
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700189static unsigned long kfifo_copy_from_user(struct __kfifo *fifo,
190 const void __user *from, unsigned int len, unsigned int off,
191 unsigned int *copied)
Stefani Seibold86d48802009-12-21 14:37:32 -0800192{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700193 unsigned int size = fifo->mask + 1;
194 unsigned int esize = fifo->esize;
195 unsigned int l;
196 unsigned long ret;
Stefani Seibold86d48802009-12-21 14:37:32 -0800197
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700198 off &= fifo->mask;
199 if (esize != 1) {
200 off *= esize;
201 size *= esize;
202 len *= esize;
203 }
204 l = min(len, size - off);
Andi Kleen64ce1032010-01-15 17:01:15 -0800205
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700206 ret = copy_from_user(fifo->data + off, from, l);
207 if (unlikely(ret))
208 ret = DIV_ROUND_UP(ret + len - l, esize);
209 else {
210 ret = copy_from_user(fifo->data, from + l, len - l);
211 if (unlikely(ret))
212 ret = DIV_ROUND_UP(ret, esize);
213 }
214 /*
215 * make sure that the data in the fifo is up to date before
216 * incrementing the fifo->in index counter
217 */
218 smp_wmb();
Lars-Peter Clausena019e482013-11-14 14:32:16 -0800219 *copied = len - ret * esize;
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700220 /* return the number of elements which are not copied */
Andi Kleen64ce1032010-01-15 17:01:15 -0800221 return ret;
Stefani Seibolda121f242009-12-21 14:37:31 -0800222}
Stefani Seibolda121f242009-12-21 14:37:31 -0800223
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700224int __kfifo_from_user(struct __kfifo *fifo, const void __user *from,
225 unsigned long len, unsigned int *copied)
Stefani Seibold86d48802009-12-21 14:37:32 -0800226{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700227 unsigned int l;
228 unsigned long ret;
229 unsigned int esize = fifo->esize;
230 int err;
231
232 if (esize != 1)
233 len /= esize;
234
235 l = kfifo_unused(fifo);
236 if (len > l)
237 len = l;
238
239 ret = kfifo_copy_from_user(fifo, from, len, fifo->in, copied);
240 if (unlikely(ret)) {
241 len -= ret;
242 err = -EFAULT;
243 } else
244 err = 0;
245 fifo->in += len;
246 return err;
Stefani Seibold86d48802009-12-21 14:37:32 -0800247}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700248EXPORT_SYMBOL(__kfifo_from_user);
Stefani Seibold86d48802009-12-21 14:37:32 -0800249
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700250static unsigned long kfifo_copy_to_user(struct __kfifo *fifo, void __user *to,
251 unsigned int len, unsigned int off, unsigned int *copied)
Stefani Seibold86d48802009-12-21 14:37:32 -0800252{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700253 unsigned int l;
254 unsigned long ret;
255 unsigned int size = fifo->mask + 1;
256 unsigned int esize = fifo->esize;
Stefani Seibold86d48802009-12-21 14:37:32 -0800257
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700258 off &= fifo->mask;
259 if (esize != 1) {
260 off *= esize;
261 size *= esize;
262 len *= esize;
263 }
264 l = min(len, size - off);
265
266 ret = copy_to_user(to, fifo->data + off, l);
267 if (unlikely(ret))
268 ret = DIV_ROUND_UP(ret + len - l, esize);
269 else {
270 ret = copy_to_user(to + l, fifo->data, len - l);
271 if (unlikely(ret))
272 ret = DIV_ROUND_UP(ret, esize);
273 }
274 /*
275 * make sure that the data is copied before
276 * incrementing the fifo->out index counter
277 */
278 smp_wmb();
Lars-Peter Clausena019e482013-11-14 14:32:16 -0800279 *copied = len - ret * esize;
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700280 /* return the number of elements which are not copied */
281 return ret;
282}
283
284int __kfifo_to_user(struct __kfifo *fifo, void __user *to,
285 unsigned long len, unsigned int *copied)
286{
287 unsigned int l;
288 unsigned long ret;
289 unsigned int esize = fifo->esize;
290 int err;
291
292 if (esize != 1)
293 len /= esize;
294
295 l = fifo->in - fifo->out;
296 if (len > l)
297 len = l;
298 ret = kfifo_copy_to_user(fifo, to, len, fifo->out, copied);
299 if (unlikely(ret)) {
300 len -= ret;
301 err = -EFAULT;
302 } else
303 err = 0;
304 fifo->out += len;
305 return err;
306}
307EXPORT_SYMBOL(__kfifo_to_user);
308
309static int setup_sgl_buf(struct scatterlist *sgl, void *buf,
310 int nents, unsigned int len)
311{
312 int n;
313 unsigned int l;
314 unsigned int off;
315 struct page *page;
316
317 if (!nents)
318 return 0;
319
320 if (!len)
321 return 0;
322
323 n = 0;
324 page = virt_to_page(buf);
325 off = offset_in_page(buf);
326 l = 0;
327
328 while (len >= l + PAGE_SIZE - off) {
329 struct page *npage;
330
331 l += PAGE_SIZE;
332 buf += PAGE_SIZE;
333 npage = virt_to_page(buf);
334 if (page_to_phys(page) != page_to_phys(npage) - l) {
Stefani Seiboldd78a3ed2010-08-11 14:17:27 -0700335 sg_set_page(sgl, page, l - off, off);
336 sgl = sg_next(sgl);
337 if (++n == nents || sgl == NULL)
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700338 return n;
339 page = npage;
340 len -= l - off;
341 l = off = 0;
342 }
343 }
Stefani Seiboldd78a3ed2010-08-11 14:17:27 -0700344 sg_set_page(sgl, page, len, off);
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700345 return n + 1;
346}
347
348static unsigned int setup_sgl(struct __kfifo *fifo, struct scatterlist *sgl,
349 int nents, unsigned int len, unsigned int off)
350{
351 unsigned int size = fifo->mask + 1;
352 unsigned int esize = fifo->esize;
353 unsigned int l;
354 unsigned int n;
355
356 off &= fifo->mask;
357 if (esize != 1) {
358 off *= esize;
359 size *= esize;
360 len *= esize;
361 }
362 l = min(len, size - off);
363
Stefani Seiboldd78a3ed2010-08-11 14:17:27 -0700364 n = setup_sgl_buf(sgl, fifo->data + off, nents, l);
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700365 n += setup_sgl_buf(sgl + n, fifo->data, nents - n, len - l);
366
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700367 return n;
368}
369
370unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
371 struct scatterlist *sgl, int nents, unsigned int len)
372{
373 unsigned int l;
374
375 l = kfifo_unused(fifo);
376 if (len > l)
377 len = l;
378
379 return setup_sgl(fifo, sgl, nents, len, fifo->in);
380}
381EXPORT_SYMBOL(__kfifo_dma_in_prepare);
382
383unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
384 struct scatterlist *sgl, int nents, unsigned int len)
385{
386 unsigned int l;
387
388 l = fifo->in - fifo->out;
389 if (len > l)
390 len = l;
391
392 return setup_sgl(fifo, sgl, nents, len, fifo->out);
393}
394EXPORT_SYMBOL(__kfifo_dma_out_prepare);
395
396unsigned int __kfifo_max_r(unsigned int len, size_t recsize)
397{
398 unsigned int max = (1 << (recsize << 3)) - 1;
399
400 if (len > max)
401 return max;
402 return len;
403}
Srinivas Kandagatla30059d92012-04-11 05:01:56 -0300404EXPORT_SYMBOL(__kfifo_max_r);
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700405
406#define __KFIFO_PEEK(data, out, mask) \
407 ((data)[(out) & (mask)])
408/*
409 * __kfifo_peek_n internal helper function for determinate the length of
410 * the next record in the fifo
411 */
412static unsigned int __kfifo_peek_n(struct __kfifo *fifo, size_t recsize)
413{
414 unsigned int l;
415 unsigned int mask = fifo->mask;
416 unsigned char *data = fifo->data;
417
418 l = __KFIFO_PEEK(data, fifo->out, mask);
419
420 if (--recsize)
421 l |= __KFIFO_PEEK(data, fifo->out + 1, mask) << 8;
422
423 return l;
424}
425
426#define __KFIFO_POKE(data, in, mask, val) \
427 ( \
428 (data)[(in) & (mask)] = (unsigned char)(val) \
429 )
430
431/*
432 * __kfifo_poke_n internal helper function for storeing the length of
433 * the record into the fifo
434 */
435static void __kfifo_poke_n(struct __kfifo *fifo, unsigned int n, size_t recsize)
436{
437 unsigned int mask = fifo->mask;
438 unsigned char *data = fifo->data;
439
440 __KFIFO_POKE(data, fifo->in, mask, n);
441
442 if (recsize > 1)
443 __KFIFO_POKE(data, fifo->in + 1, mask, n >> 8);
444}
445
446unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize)
447{
Stefani Seibold86d48802009-12-21 14:37:32 -0800448 return __kfifo_peek_n(fifo, recsize);
449}
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700450EXPORT_SYMBOL(__kfifo_len_r);
Stefani Seibold86d48802009-12-21 14:37:32 -0800451
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700452unsigned int __kfifo_in_r(struct __kfifo *fifo, const void *buf,
453 unsigned int len, size_t recsize)
Stefani Seibold86d48802009-12-21 14:37:32 -0800454{
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700455 if (len + recsize > kfifo_unused(fifo))
456 return 0;
Stefani Seibold86d48802009-12-21 14:37:32 -0800457
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700458 __kfifo_poke_n(fifo, len, recsize);
459
460 kfifo_copy_in(fifo, buf, len, fifo->in + recsize);
461 fifo->in += len + recsize;
462 return len;
463}
464EXPORT_SYMBOL(__kfifo_in_r);
465
466static unsigned int kfifo_out_copy_r(struct __kfifo *fifo,
467 void *buf, unsigned int len, size_t recsize, unsigned int *n)
468{
469 *n = __kfifo_peek_n(fifo, recsize);
470
471 if (len > *n)
472 len = *n;
473
474 kfifo_copy_out(fifo, buf, len, fifo->out + recsize);
475 return len;
476}
477
478unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, void *buf,
479 unsigned int len, size_t recsize)
480{
481 unsigned int n;
482
483 if (fifo->in == fifo->out)
484 return 0;
485
486 return kfifo_out_copy_r(fifo, buf, len, recsize, &n);
487}
488EXPORT_SYMBOL(__kfifo_out_peek_r);
489
490unsigned int __kfifo_out_r(struct __kfifo *fifo, void *buf,
491 unsigned int len, size_t recsize)
492{
493 unsigned int n;
494
495 if (fifo->in == fifo->out)
496 return 0;
497
498 len = kfifo_out_copy_r(fifo, buf, len, recsize, &n);
499 fifo->out += n + recsize;
500 return len;
501}
502EXPORT_SYMBOL(__kfifo_out_r);
503
Andrea Righib35de432010-08-19 14:13:27 -0700504void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize)
505{
506 unsigned int n;
507
508 n = __kfifo_peek_n(fifo, recsize);
509 fifo->out += n + recsize;
510}
511EXPORT_SYMBOL(__kfifo_skip_r);
512
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700513int __kfifo_from_user_r(struct __kfifo *fifo, const void __user *from,
514 unsigned long len, unsigned int *copied, size_t recsize)
515{
516 unsigned long ret;
517
518 len = __kfifo_max_r(len, recsize);
519
520 if (len + recsize > kfifo_unused(fifo)) {
521 *copied = 0;
522 return 0;
523 }
524
525 __kfifo_poke_n(fifo, len, recsize);
526
527 ret = kfifo_copy_from_user(fifo, from, len, fifo->in + recsize, copied);
528 if (unlikely(ret)) {
529 *copied = 0;
530 return -EFAULT;
531 }
532 fifo->in += len + recsize;
533 return 0;
534}
535EXPORT_SYMBOL(__kfifo_from_user_r);
536
537int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
538 unsigned long len, unsigned int *copied, size_t recsize)
539{
540 unsigned long ret;
541 unsigned int n;
542
543 if (fifo->in == fifo->out) {
544 *copied = 0;
545 return 0;
546 }
547
548 n = __kfifo_peek_n(fifo, recsize);
549 if (len > n)
550 len = n;
551
552 ret = kfifo_copy_to_user(fifo, to, len, fifo->out + recsize, copied);
553 if (unlikely(ret)) {
554 *copied = 0;
555 return -EFAULT;
556 }
557 fifo->out += n + recsize;
558 return 0;
559}
560EXPORT_SYMBOL(__kfifo_to_user_r);
561
562unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
563 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
564{
Himangi Saraogi89b3ac62014-08-08 14:22:44 -0700565 BUG_ON(!nents);
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700566
567 len = __kfifo_max_r(len, recsize);
568
569 if (len + recsize > kfifo_unused(fifo))
570 return 0;
571
572 return setup_sgl(fifo, sgl, nents, len, fifo->in + recsize);
573}
574EXPORT_SYMBOL(__kfifo_dma_in_prepare_r);
575
576void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
577 unsigned int len, size_t recsize)
578{
579 len = __kfifo_max_r(len, recsize);
580 __kfifo_poke_n(fifo, len, recsize);
581 fifo->in += len + recsize;
582}
583EXPORT_SYMBOL(__kfifo_dma_in_finish_r);
584
585unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
586 struct scatterlist *sgl, int nents, unsigned int len, size_t recsize)
587{
Himangi Saraogi89b3ac62014-08-08 14:22:44 -0700588 BUG_ON(!nents);
Stefani Seibold2e956fb2010-08-10 18:03:38 -0700589
590 len = __kfifo_max_r(len, recsize);
591
592 if (len + recsize > fifo->in - fifo->out)
593 return 0;
594
595 return setup_sgl(fifo, sgl, nents, len, fifo->out + recsize);
596}
597EXPORT_SYMBOL(__kfifo_dma_out_prepare_r);
598
599void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize)
600{
601 unsigned int len;
602
603 len = __kfifo_peek_n(fifo, recsize);
604 fifo->out += len + recsize;
605}
606EXPORT_SYMBOL(__kfifo_dma_out_finish_r);