2 * A generic kernel FIFO implementation
4 * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
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.
22 #ifndef _LINUX_KFIFO_H
23 #define _LINUX_KFIFO_H
26 * How to porting drivers to the new generic FIFO API:
28 * - Modify the declaration of the "struct kfifo *" object into a
29 * in-place "struct kfifo" object
30 * - Init the in-place object with kfifo_alloc() or kfifo_init()
31 * Note: The address of the in-place "struct kfifo" object must be
32 * passed as the first argument to this functions
33 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
35 * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
36 * into kfifo_out_spinlocked
37 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
38 * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
39 * as the last parameter
40 * - The formerly __kfifo_* functions are renamed into kfifo_*
44 * Note about locking : There is no locking required until only * one reader
45 * and one writer is using the fifo and no kfifo_reset() will be * called
46 * kfifo_reset_out() can be safely used, until it will be only called
47 * in the reader thread.
48 * For multiple writer and one reader there is only a need to lock the writer.
49 * And vice versa for only one writer and multiple reader there is only a need
53 #include <linux/kernel.h>
54 #include <linux/spinlock.h>
55 #include <linux/stddef.h>
56 #include <linux/scatterlist.h>
66 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
68 struct __kfifo kfifo; \
70 char (*rectype)[recsize]; \
72 const ptrtype *ptr_const; \
75 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
77 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
78 type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
81 #define STRUCT_KFIFO(type, size) \
82 struct __STRUCT_KFIFO(type, size, 0, type)
84 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
86 __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
90 #define STRUCT_KFIFO_PTR(type) \
91 struct __STRUCT_KFIFO_PTR(type, 0, type)
94 * define compatibility "struct kfifo" for dynamic allocated fifos
96 struct kfifo
__STRUCT_KFIFO_PTR(unsigned char, 0, void);
98 #define STRUCT_KFIFO_REC_1(size) \
99 struct __STRUCT_KFIFO(unsigned char, size, 1, void)
101 #define STRUCT_KFIFO_REC_2(size) \
102 struct __STRUCT_KFIFO(unsigned char, size, 2, void)
105 * define kfifo_rec types
107 struct kfifo_rec_ptr_1
__STRUCT_KFIFO_PTR(unsigned char, 1, void);
108 struct kfifo_rec_ptr_2
__STRUCT_KFIFO_PTR(unsigned char, 2, void);
111 * helper macro to distinguish between real in place fifo where the fifo
112 * array is a part of the structure and the fifo type where the array is
113 * outside of the fifo structure.
115 #define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo))
118 * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
119 * @fifo: name of the declared fifo
120 * @type: type of the fifo elements
122 #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo
125 * DECLARE_KFIFO - macro to declare a fifo object
126 * @fifo: name of the declared fifo
127 * @type: type of the fifo elements
128 * @size: the number of elements in the fifo, this must be a power of 2
130 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
133 * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
134 * @fifo: name of the declared fifo datatype
136 #define INIT_KFIFO(fifo) \
138 typeof(&(fifo)) __tmp = &(fifo); \
139 struct __kfifo *__kfifo = &__tmp->kfifo; \
142 __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
143 __kfifo->esize = sizeof(*__tmp->buf); \
144 __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \
148 * DEFINE_KFIFO - macro to define and initialize a fifo
149 * @fifo: name of the declared fifo datatype
150 * @type: type of the fifo elements
151 * @size: the number of elements in the fifo, this must be a power of 2
153 * Note: the macro can be used for global and local fifo data type variables.
155 #define DEFINE_KFIFO(fifo, type, size) \
156 DECLARE_KFIFO(fifo, type, size) = \
162 .mask = __is_kfifo_ptr(&(fifo)) ? \
164 ARRAY_SIZE((fifo).buf) - 1, \
165 .esize = sizeof(*(fifo).buf), \
166 .data = __is_kfifo_ptr(&(fifo)) ? \
174 /* __kfifo_must_check_helper() is temporarily disabled because it was faulty */
175 #define __kfifo_must_check_helper(x) (x)
178 * kfifo_initialized - Check if the fifo is initialized
179 * @fifo: address of the fifo to check
181 * Return %true if fifo is initialized, otherwise %false.
182 * Assumes the fifo was 0 before.
184 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
187 * kfifo_esize - returns the size of the element managed by the fifo
188 * @fifo: address of the fifo to be used
190 #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
193 * kfifo_recsize - returns the size of the record length field
194 * @fifo: address of the fifo to be used
196 #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
199 * kfifo_size - returns the size of the fifo in elements
200 * @fifo: address of the fifo to be used
202 #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
205 * kfifo_reset - removes the entire fifo content
206 * @fifo: address of the fifo to be used
208 * Note: usage of kfifo_reset() is dangerous. It should be only called when the
209 * fifo is exclusived locked or when it is secured that no other thread is
210 * accessing the fifo.
212 #define kfifo_reset(fifo) \
214 typeof((fifo) + 1) __tmp = (fifo); \
215 __tmp->kfifo.in = __tmp->kfifo.out = 0; \
219 * kfifo_reset_out - skip fifo content
220 * @fifo: address of the fifo to be used
222 * Note: The usage of kfifo_reset_out() is safe until it will be only called
223 * from the reader thread and there is only one concurrent reader. Otherwise
224 * it is dangerous and must be handled in the same way as kfifo_reset().
226 #define kfifo_reset_out(fifo) \
228 typeof((fifo) + 1) __tmp = (fifo); \
229 __tmp->kfifo.out = __tmp->kfifo.in; \
233 * kfifo_len - returns the number of used elements in the fifo
234 * @fifo: address of the fifo to be used
236 #define kfifo_len(fifo) \
238 typeof((fifo) + 1) __tmpl = (fifo); \
239 __tmpl->kfifo.in - __tmpl->kfifo.out; \
243 * kfifo_is_empty - returns true if the fifo is empty
244 * @fifo: address of the fifo to be used
246 #define kfifo_is_empty(fifo) \
248 typeof((fifo) + 1) __tmpq = (fifo); \
249 __tmpq->kfifo.in == __tmpq->kfifo.out; \
253 * kfifo_is_full - returns true if the fifo is full
254 * @fifo: address of the fifo to be used
256 #define kfifo_is_full(fifo) \
258 typeof((fifo) + 1) __tmpq = (fifo); \
259 kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
263 * kfifo_avail - returns the number of unused elements in the fifo
264 * @fifo: address of the fifo to be used
266 #define kfifo_avail(fifo) \
267 __kfifo_must_check_helper( \
269 typeof((fifo) + 1) __tmpq = (fifo); \
270 const size_t __recsize = sizeof(*__tmpq->rectype); \
271 unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
272 (__recsize) ? ((__avail <= __recsize) ? 0 : \
273 __kfifo_max_r(__avail - __recsize, __recsize)) : \
279 * kfifo_skip - skip output data
280 * @fifo: address of the fifo to be used
282 #define kfifo_skip(fifo) \
284 typeof((fifo) + 1) __tmp = (fifo); \
285 const size_t __recsize = sizeof(*__tmp->rectype); \
286 struct __kfifo *__kfifo = &__tmp->kfifo; \
288 __kfifo_skip_r(__kfifo, __recsize); \
294 * kfifo_peek_len - gets the size of the next fifo record
295 * @fifo: address of the fifo to be used
297 * This function returns the size of the next fifo record in number of bytes.
299 #define kfifo_peek_len(fifo) \
300 __kfifo_must_check_helper( \
302 typeof((fifo) + 1) __tmp = (fifo); \
303 const size_t __recsize = sizeof(*__tmp->rectype); \
304 struct __kfifo *__kfifo = &__tmp->kfifo; \
305 (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
306 __kfifo_len_r(__kfifo, __recsize); \
311 * kfifo_alloc - dynamically allocates a new fifo buffer
312 * @fifo: pointer to the fifo
313 * @size: the number of elements in the fifo, this must be a power of 2
314 * @gfp_mask: get_free_pages mask, passed to kmalloc()
316 * This macro dynamically allocates a new fifo buffer.
318 * The numer of elements will be rounded-up to a power of 2.
319 * The fifo will be release with kfifo_free().
320 * Return 0 if no error, otherwise an error code.
322 #define kfifo_alloc(fifo, size, gfp_mask) \
323 __kfifo_must_check_helper( \
325 typeof((fifo) + 1) __tmp = (fifo); \
326 struct __kfifo *__kfifo = &__tmp->kfifo; \
327 __is_kfifo_ptr(__tmp) ? \
328 __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
334 * kfifo_free - frees the fifo
335 * @fifo: the fifo to be freed
337 #define kfifo_free(fifo) \
339 typeof((fifo) + 1) __tmp = (fifo); \
340 struct __kfifo *__kfifo = &__tmp->kfifo; \
341 if (__is_kfifo_ptr(__tmp)) \
342 __kfifo_free(__kfifo); \
346 * kfifo_init - initialize a fifo using a preallocated buffer
347 * @fifo: the fifo to assign the buffer
348 * @buffer: the preallocated buffer to be used
349 * @size: the size of the internal buffer, this have to be a power of 2
351 * This macro initialize a fifo using a preallocated buffer.
353 * The numer of elements will be rounded-up to a power of 2.
354 * Return 0 if no error, otherwise an error code.
356 #define kfifo_init(fifo, buffer, size) \
358 typeof((fifo) + 1) __tmp = (fifo); \
359 struct __kfifo *__kfifo = &__tmp->kfifo; \
360 __is_kfifo_ptr(__tmp) ? \
361 __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
366 * kfifo_put - put data into the fifo
367 * @fifo: address of the fifo to be used
368 * @val: the data to be added
370 * This macro copies the given value into the fifo.
371 * It returns 0 if the fifo was full. Otherwise it returns the number
372 * processed elements.
374 * Note that with only one concurrent reader and one concurrent
375 * writer, you don't need extra locking to use these macro.
377 #define kfifo_put(fifo, val) \
379 typeof((fifo) + 1) __tmp = (fifo); \
380 typeof((val) + 1) __val = (val); \
381 unsigned int __ret; \
382 const size_t __recsize = sizeof(*__tmp->rectype); \
383 struct __kfifo *__kfifo = &__tmp->kfifo; \
385 typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
386 __dummy = (typeof(__val))NULL; \
389 __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \
392 __ret = !kfifo_is_full(__tmp); \
394 (__is_kfifo_ptr(__tmp) ? \
395 ((typeof(__tmp->type))__kfifo->data) : \
397 )[__kfifo->in & __tmp->kfifo.mask] = \
398 *(typeof(__tmp->type))__val; \
407 * kfifo_get - get data from the fifo
408 * @fifo: address of the fifo to be used
409 * @val: the var where to store the data to be added
411 * This macro reads the data from the fifo.
412 * It returns 0 if the fifo was empty. Otherwise it returns the number
413 * processed elements.
415 * Note that with only one concurrent reader and one concurrent
416 * writer, you don't need extra locking to use these macro.
418 #define kfifo_get(fifo, val) \
419 __kfifo_must_check_helper( \
421 typeof((fifo) + 1) __tmp = (fifo); \
422 typeof((val) + 1) __val = (val); \
423 unsigned int __ret; \
424 const size_t __recsize = sizeof(*__tmp->rectype); \
425 struct __kfifo *__kfifo = &__tmp->kfifo; \
427 __val = (typeof(__tmp->ptr))0; \
429 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
432 __ret = !kfifo_is_empty(__tmp); \
434 *(typeof(__tmp->type))__val = \
435 (__is_kfifo_ptr(__tmp) ? \
436 ((typeof(__tmp->type))__kfifo->data) : \
438 )[__kfifo->out & __tmp->kfifo.mask]; \
448 * kfifo_peek - get data from the fifo without removing
449 * @fifo: address of the fifo to be used
450 * @val: the var where to store the data to be added
452 * This reads the data from the fifo without removing it from the fifo.
453 * It returns 0 if the fifo was empty. Otherwise it returns the number
454 * processed elements.
456 * Note that with only one concurrent reader and one concurrent
457 * writer, you don't need extra locking to use these macro.
459 #define kfifo_peek(fifo, val) \
460 __kfifo_must_check_helper( \
462 typeof((fifo) + 1) __tmp = (fifo); \
463 typeof((val) + 1) __val = (val); \
464 unsigned int __ret; \
465 const size_t __recsize = sizeof(*__tmp->rectype); \
466 struct __kfifo *__kfifo = &__tmp->kfifo; \
468 __val = (typeof(__tmp->ptr))NULL; \
470 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
473 __ret = !kfifo_is_empty(__tmp); \
475 *(typeof(__tmp->type))__val = \
476 (__is_kfifo_ptr(__tmp) ? \
477 ((typeof(__tmp->type))__kfifo->data) : \
479 )[__kfifo->out & __tmp->kfifo.mask]; \
488 * kfifo_in - put data into the fifo
489 * @fifo: address of the fifo to be used
490 * @buf: the data to be added
491 * @n: number of elements to be added
493 * This macro copies the given buffer into the fifo and returns the
494 * number of copied elements.
496 * Note that with only one concurrent reader and one concurrent
497 * writer, you don't need extra locking to use these macro.
499 #define kfifo_in(fifo, buf, n) \
501 typeof((fifo) + 1) __tmp = (fifo); \
502 typeof((buf) + 1) __buf = (buf); \
503 unsigned long __n = (n); \
504 const size_t __recsize = sizeof(*__tmp->rectype); \
505 struct __kfifo *__kfifo = &__tmp->kfifo; \
507 typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
508 __dummy = (typeof(__buf))NULL; \
511 __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
512 __kfifo_in(__kfifo, __buf, __n); \
516 * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
517 * @fifo: address of the fifo to be used
518 * @buf: the data to be added
519 * @n: number of elements to be added
520 * @lock: pointer to the spinlock to use for locking
522 * This macro copies the given values buffer into the fifo and returns the
523 * number of copied elements.
525 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
527 unsigned long __flags; \
528 unsigned int __ret; \
529 spin_lock_irqsave(lock, __flags); \
530 __ret = kfifo_in(fifo, buf, n); \
531 spin_unlock_irqrestore(lock, __flags); \
535 /* alias for kfifo_in_spinlocked, will be removed in a future release */
536 #define kfifo_in_locked(fifo, buf, n, lock) \
537 kfifo_in_spinlocked(fifo, buf, n, lock)
540 * kfifo_out - get data from the fifo
541 * @fifo: address of the fifo to be used
542 * @buf: pointer to the storage buffer
543 * @n: max. number of elements to get
545 * This macro get some data from the fifo and return the numbers of elements
548 * Note that with only one concurrent reader and one concurrent
549 * writer, you don't need extra locking to use these macro.
551 #define kfifo_out(fifo, buf, n) \
552 __kfifo_must_check_helper( \
554 typeof((fifo) + 1) __tmp = (fifo); \
555 typeof((buf) + 1) __buf = (buf); \
556 unsigned long __n = (n); \
557 const size_t __recsize = sizeof(*__tmp->rectype); \
558 struct __kfifo *__kfifo = &__tmp->kfifo; \
560 typeof(__tmp->ptr) __dummy = NULL; \
564 __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
565 __kfifo_out(__kfifo, __buf, __n); \
570 * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
571 * @fifo: address of the fifo to be used
572 * @buf: pointer to the storage buffer
573 * @n: max. number of elements to get
574 * @lock: pointer to the spinlock to use for locking
576 * This macro get the data from the fifo and return the numbers of elements
579 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
580 __kfifo_must_check_helper( \
582 unsigned long __flags; \
583 unsigned int __ret; \
584 spin_lock_irqsave(lock, __flags); \
585 __ret = kfifo_out(fifo, buf, n); \
586 spin_unlock_irqrestore(lock, __flags); \
591 /* alias for kfifo_out_spinlocked, will be removed in a future release */
592 #define kfifo_out_locked(fifo, buf, n, lock) \
593 kfifo_out_spinlocked(fifo, buf, n, lock)
596 * kfifo_from_user - puts some data from user space into the fifo
597 * @fifo: address of the fifo to be used
598 * @from: pointer to the data to be added
599 * @len: the length of the data to be added
600 * @copied: pointer to output variable to store the number of copied bytes
602 * This macro copies at most @len bytes from the @from into the
603 * fifo, depending of the available space and returns -EFAULT/0.
605 * Note that with only one concurrent reader and one concurrent
606 * writer, you don't need extra locking to use these macro.
608 #define kfifo_from_user(fifo, from, len, copied) \
609 __kfifo_must_check_helper( \
611 typeof((fifo) + 1) __tmp = (fifo); \
612 const void __user *__from = (from); \
613 unsigned int __len = (len); \
614 unsigned int *__copied = (copied); \
615 const size_t __recsize = sizeof(*__tmp->rectype); \
616 struct __kfifo *__kfifo = &__tmp->kfifo; \
618 __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
619 __kfifo_from_user(__kfifo, __from, __len, __copied); \
624 * kfifo_to_user - copies data from the fifo into user space
625 * @fifo: address of the fifo to be used
626 * @to: where the data must be copied
627 * @len: the size of the destination buffer
628 * @copied: pointer to output variable to store the number of copied bytes
630 * This macro copies at most @len bytes from the fifo into the
631 * @to buffer and returns -EFAULT/0.
633 * Note that with only one concurrent reader and one concurrent
634 * writer, you don't need extra locking to use these macro.
636 #define kfifo_to_user(fifo, to, len, copied) \
637 __kfifo_must_check_helper( \
639 typeof((fifo) + 1) __tmp = (fifo); \
640 void __user *__to = (to); \
641 unsigned int __len = (len); \
642 unsigned int *__copied = (copied); \
643 const size_t __recsize = sizeof(*__tmp->rectype); \
644 struct __kfifo *__kfifo = &__tmp->kfifo; \
646 __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
647 __kfifo_to_user(__kfifo, __to, __len, __copied); \
652 * kfifo_dma_in_prepare - setup a scatterlist for DMA input
653 * @fifo: address of the fifo to be used
654 * @sgl: pointer to the scatterlist array
655 * @nents: number of entries in the scatterlist array
656 * @len: number of elements to transfer
658 * This macro fills a scatterlist for DMA input.
659 * It returns the number entries in the scatterlist array.
661 * Note that with only one concurrent reader and one concurrent
662 * writer, you don't need extra locking to use these macros.
664 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
666 typeof((fifo) + 1) __tmp = (fifo); \
667 struct scatterlist *__sgl = (sgl); \
668 int __nents = (nents); \
669 unsigned int __len = (len); \
670 const size_t __recsize = sizeof(*__tmp->rectype); \
671 struct __kfifo *__kfifo = &__tmp->kfifo; \
673 __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
674 __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
678 * kfifo_dma_in_finish - finish a DMA IN operation
679 * @fifo: address of the fifo to be used
680 * @len: number of bytes to received
682 * This macro finish a DMA IN operation. The in counter will be updated by
683 * the len parameter. No error checking will be done.
685 * Note that with only one concurrent reader and one concurrent
686 * writer, you don't need extra locking to use these macros.
688 #define kfifo_dma_in_finish(fifo, len) \
690 typeof((fifo) + 1) __tmp = (fifo); \
691 unsigned int __len = (len); \
692 const size_t __recsize = sizeof(*__tmp->rectype); \
693 struct __kfifo *__kfifo = &__tmp->kfifo; \
695 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
697 __kfifo->in += __len / sizeof(*__tmp->type); \
701 * kfifo_dma_out_prepare - setup a scatterlist for DMA output
702 * @fifo: address of the fifo to be used
703 * @sgl: pointer to the scatterlist array
704 * @nents: number of entries in the scatterlist array
705 * @len: number of elements to transfer
707 * This macro fills a scatterlist for DMA output which at most @len bytes
709 * It returns the number entries in the scatterlist array.
710 * A zero means there is no space available and the scatterlist is not filled.
712 * Note that with only one concurrent reader and one concurrent
713 * writer, you don't need extra locking to use these macros.
715 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
717 typeof((fifo) + 1) __tmp = (fifo); \
718 struct scatterlist *__sgl = (sgl); \
719 int __nents = (nents); \
720 unsigned int __len = (len); \
721 const size_t __recsize = sizeof(*__tmp->rectype); \
722 struct __kfifo *__kfifo = &__tmp->kfifo; \
724 __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
725 __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
729 * kfifo_dma_out_finish - finish a DMA OUT operation
730 * @fifo: address of the fifo to be used
731 * @len: number of bytes transferd
733 * This macro finish a DMA OUT operation. The out counter will be updated by
734 * the len parameter. No error checking will be done.
736 * Note that with only one concurrent reader and one concurrent
737 * writer, you don't need extra locking to use these macros.
739 #define kfifo_dma_out_finish(fifo, len) \
741 typeof((fifo) + 1) __tmp = (fifo); \
742 unsigned int __len = (len); \
743 const size_t __recsize = sizeof(*__tmp->rectype); \
744 struct __kfifo *__kfifo = &__tmp->kfifo; \
746 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
748 __kfifo->out += __len / sizeof(*__tmp->type); \
752 * kfifo_out_peek - gets some data from the fifo
753 * @fifo: address of the fifo to be used
754 * @buf: pointer to the storage buffer
755 * @n: max. number of elements to get
757 * This macro get the data from the fifo and return the numbers of elements
758 * copied. The data is not removed from the fifo.
760 * Note that with only one concurrent reader and one concurrent
761 * writer, you don't need extra locking to use these macro.
763 #define kfifo_out_peek(fifo, buf, n) \
764 __kfifo_must_check_helper( \
766 typeof((fifo) + 1) __tmp = (fifo); \
767 typeof((buf) + 1) __buf = (buf); \
768 unsigned long __n = (n); \
769 const size_t __recsize = sizeof(*__tmp->rectype); \
770 struct __kfifo *__kfifo = &__tmp->kfifo; \
772 typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \
776 __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
777 __kfifo_out_peek(__kfifo, __buf, __n); \
781 extern int __kfifo_alloc(struct __kfifo
*fifo
, unsigned int size
,
782 size_t esize
, gfp_t gfp_mask
);
784 extern void __kfifo_free(struct __kfifo
*fifo
);
786 extern int __kfifo_init(struct __kfifo
*fifo
, void *buffer
,
787 unsigned int size
, size_t esize
);
789 extern unsigned int __kfifo_in(struct __kfifo
*fifo
,
790 const void *buf
, unsigned int len
);
792 extern unsigned int __kfifo_out(struct __kfifo
*fifo
,
793 void *buf
, unsigned int len
);
795 extern int __kfifo_from_user(struct __kfifo
*fifo
,
796 const void __user
*from
, unsigned long len
, unsigned int *copied
);
798 extern int __kfifo_to_user(struct __kfifo
*fifo
,
799 void __user
*to
, unsigned long len
, unsigned int *copied
);
801 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo
*fifo
,
802 struct scatterlist
*sgl
, int nents
, unsigned int len
);
804 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo
*fifo
,
805 struct scatterlist
*sgl
, int nents
, unsigned int len
);
807 extern unsigned int __kfifo_out_peek(struct __kfifo
*fifo
,
808 void *buf
, unsigned int len
);
810 extern unsigned int __kfifo_in_r(struct __kfifo
*fifo
,
811 const void *buf
, unsigned int len
, size_t recsize
);
813 extern unsigned int __kfifo_out_r(struct __kfifo
*fifo
,
814 void *buf
, unsigned int len
, size_t recsize
);
816 extern int __kfifo_from_user_r(struct __kfifo
*fifo
,
817 const void __user
*from
, unsigned long len
, unsigned int *copied
,
820 extern int __kfifo_to_user_r(struct __kfifo
*fifo
, void __user
*to
,
821 unsigned long len
, unsigned int *copied
, size_t recsize
);
823 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo
*fifo
,
824 struct scatterlist
*sgl
, int nents
, unsigned int len
, size_t recsize
);
826 extern void __kfifo_dma_in_finish_r(struct __kfifo
*fifo
,
827 unsigned int len
, size_t recsize
);
829 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo
*fifo
,
830 struct scatterlist
*sgl
, int nents
, unsigned int len
, size_t recsize
);
832 extern void __kfifo_dma_out_finish_r(struct __kfifo
*fifo
, size_t recsize
);
834 extern unsigned int __kfifo_len_r(struct __kfifo
*fifo
, size_t recsize
);
836 extern void __kfifo_skip_r(struct __kfifo
*fifo
, size_t recsize
);
838 extern unsigned int __kfifo_out_peek_r(struct __kfifo
*fifo
,
839 void *buf
, unsigned int len
, size_t recsize
);
841 extern unsigned int __kfifo_max_r(unsigned int len
, size_t recsize
);