Initial commit
[kernel/linux-3.0.git] / include / compat / linux / kfifo.h
1 #include <linux/version.h>
2 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36))
3 #include_next <linux/kfifo.h>
4 #else
5 /*
6  * A generic kernel FIFO implementation
7  *
8  * Copyright (C) 2009/2010 Stefani Seibold <stefani@seibold.net>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  *
24  */
25
26 #ifndef _LINUX_KFIFO_H
27 #define _LINUX_KFIFO_H
28
29 /*
30  * How to porting drivers to the new generic FIFO API:
31  *
32  * - Modify the declaration of the "struct kfifo *" object into a
33  *   in-place "struct kfifo" object
34  * - Init the in-place object with kfifo_alloc() or kfifo_init()
35  *   Note: The address of the in-place "struct kfifo" object must be
36  *   passed as the first argument to this functions
37  * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
38  *   into kfifo_out
39  * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get
40  *   into kfifo_out_spinlocked
41  *   Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
42  *   must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked
43  *   as the last parameter
44  * - The formerly __kfifo_* functions are renamed into kfifo_*
45  */
46
47 /*
48  * Note about locking : There is no locking required until only * one reader
49  * and one writer is using the fifo and no kfifo_reset() will be * called
50  *  kfifo_reset_out() can be safely used, until it will be only called
51  * in the reader thread.
52  *  For multiple writer and one reader there is only a need to lock the writer.
53  * And vice versa for only one writer and multiple reader there is only a need
54  * to lock the reader.
55  */
56
57 #include <linux/kernel.h>
58 #include <linux/spinlock.h>
59 #include <linux/stddef.h>
60 #include <linux/scatterlist.h>
61
62 struct __kfifo {
63         unsigned int    in;
64         unsigned int    out;
65         unsigned int    mask;
66         unsigned int    esize;
67         void            *data;
68 };
69
70 #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \
71         union { \
72                 struct __kfifo  kfifo; \
73                 datatype        *type; \
74                 char            (*rectype)[recsize]; \
75                 ptrtype         *ptr; \
76                 const ptrtype   *ptr_const; \
77         }
78
79 #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \
80 { \
81         __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
82         type            buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \
83 }
84
85 #define STRUCT_KFIFO(type, size) \
86         struct __STRUCT_KFIFO(type, size, 0, type)
87
88 #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \
89 { \
90         __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \
91         type            buf[0]; \
92 }
93
94 #define STRUCT_KFIFO_PTR(type) \
95         struct __STRUCT_KFIFO_PTR(type, 0, type)
96
97 /*
98  * define compatibility "struct kfifo" for dynamic allocated fifos
99  */
100 struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void);
101
102 #define STRUCT_KFIFO_REC_1(size) \
103         struct __STRUCT_KFIFO(unsigned char, size, 1, void)
104
105 #define STRUCT_KFIFO_REC_2(size) \
106         struct __STRUCT_KFIFO(unsigned char, size, 2, void)
107
108 /*
109  * define kfifo_rec types
110  */
111 struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void);
112 struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void);
113
114 /*
115  * helper macro to distinguish between real in place fifo where the fifo
116  * array is a part of the structure and the fifo type where the array is
117  * outside of the fifo structure.
118  */
119 #define __is_kfifo_ptr(fifo)    (sizeof(*fifo) == sizeof(struct __kfifo))
120
121 /**
122  * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object
123  * @fifo: name of the declared fifo
124  * @type: type of the fifo elements
125  */
126 #define DECLARE_KFIFO_PTR(fifo, type)   STRUCT_KFIFO_PTR(type) fifo
127
128 /**
129  * DECLARE_KFIFO - macro to declare a fifo object
130  * @fifo: name of the declared fifo
131  * @type: type of the fifo elements
132  * @size: the number of elements in the fifo, this must be a power of 2
133  */
134 #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo
135
136 /**
137  * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO
138  * @fifo: name of the declared fifo datatype
139  */
140 #define INIT_KFIFO(fifo) \
141 (void)({ \
142         typeof(&(fifo)) __tmp = &(fifo); \
143         struct __kfifo *__kfifo = &__tmp->kfifo; \
144         __kfifo->in = 0; \
145         __kfifo->out = 0; \
146         __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\
147         __kfifo->esize = sizeof(*__tmp->buf); \
148         __kfifo->data = __is_kfifo_ptr(__tmp) ?  NULL : __tmp->buf; \
149 })
150
151 /**
152  * DEFINE_KFIFO - macro to define and initialize a fifo
153  * @fifo: name of the declared fifo datatype
154  * @type: type of the fifo elements
155  * @size: the number of elements in the fifo, this must be a power of 2
156  *
157  * Note: the macro can be used for global and local fifo data type variables.
158  */
159 #define DEFINE_KFIFO(fifo, type, size) \
160         DECLARE_KFIFO(fifo, type, size) = \
161         (typeof(fifo)) { \
162                 { \
163                         { \
164                         .in     = 0, \
165                         .out    = 0, \
166                         .mask   = __is_kfifo_ptr(&(fifo)) ? \
167                                   0 : \
168                                   ARRAY_SIZE((fifo).buf) - 1, \
169                         .esize  = sizeof(*(fifo).buf), \
170                         .data   = __is_kfifo_ptr(&(fifo)) ? \
171                                 NULL : \
172                                 (fifo).buf, \
173                         } \
174                 } \
175         }
176
177
178 static inline unsigned int __must_check
179 __kfifo_uint_must_check_helper(unsigned int val)
180 {
181         return val;
182 }
183
184 static inline int __must_check
185 __kfifo_int_must_check_helper(int val)
186 {
187         return val;
188 }
189
190 /**
191  * kfifo_initialized - Check if the fifo is initialized
192  * @fifo: address of the fifo to check
193  *
194  * Return %true if fifo is initialized, otherwise %false.
195  * Assumes the fifo was 0 before.
196  */
197 #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
198
199 /**
200  * kfifo_esize - returns the size of the element managed by the fifo
201  * @fifo: address of the fifo to be used
202  */
203 #define kfifo_esize(fifo)       ((fifo)->kfifo.esize)
204
205 /**
206  * kfifo_recsize - returns the size of the record length field
207  * @fifo: address of the fifo to be used
208  */
209 #define kfifo_recsize(fifo)     (sizeof(*(fifo)->rectype))
210
211 /**
212  * kfifo_size - returns the size of the fifo in elements
213  * @fifo: address of the fifo to be used
214  */
215 #define kfifo_size(fifo)        ((fifo)->kfifo.mask + 1)
216
217 /**
218  * kfifo_reset - removes the entire fifo content
219  * @fifo: address of the fifo to be used
220  *
221  * Note: usage of kfifo_reset() is dangerous. It should be only called when the
222  * fifo is exclusived locked or when it is secured that no other thread is
223  * accessing the fifo.
224  */
225 #define kfifo_reset(fifo) \
226 (void)({ \
227         typeof((fifo) + 1) __tmp = (fifo); \
228         __tmp->kfifo.in = __tmp->kfifo.out = 0; \
229 })
230
231 /**
232  * kfifo_reset_out - skip fifo content
233  * @fifo: address of the fifo to be used
234  *
235  * Note: The usage of kfifo_reset_out() is safe until it will be only called
236  * from the reader thread and there is only one concurrent reader. Otherwise
237  * it is dangerous and must be handled in the same way as kfifo_reset().
238  */
239 #define kfifo_reset_out(fifo)   \
240 (void)({ \
241         typeof((fifo) + 1) __tmp = (fifo); \
242         __tmp->kfifo.out = __tmp->kfifo.in; \
243 })
244
245 /**
246  * kfifo_len - returns the number of used elements in the fifo
247  * @fifo: address of the fifo to be used
248  */
249 #define kfifo_len(fifo) \
250 ({ \
251         typeof((fifo) + 1) __tmpl = (fifo); \
252         __tmpl->kfifo.in - __tmpl->kfifo.out; \
253 })
254
255 /**
256  * kfifo_is_empty - returns true if the fifo is empty
257  * @fifo: address of the fifo to be used
258  */
259 #define kfifo_is_empty(fifo) \
260 ({ \
261         typeof((fifo) + 1) __tmpq = (fifo); \
262         __tmpq->kfifo.in == __tmpq->kfifo.out; \
263 })
264
265 /**
266  * kfifo_is_full - returns true if the fifo is full
267  * @fifo: address of the fifo to be used
268  */
269 #define kfifo_is_full(fifo) \
270 ({ \
271         typeof((fifo) + 1) __tmpq = (fifo); \
272         kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
273 })
274
275 /**
276  * kfifo_avail - returns the number of unused elements in the fifo
277  * @fifo: address of the fifo to be used
278  */
279 #define kfifo_avail(fifo) \
280 __kfifo_uint_must_check_helper( \
281 ({ \
282         typeof((fifo) + 1) __tmpq = (fifo); \
283         const size_t __recsize = sizeof(*__tmpq->rectype); \
284         unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
285         (__recsize) ? ((__avail <= __recsize) ? 0 : \
286         __kfifo_max_r(__avail - __recsize, __recsize)) : \
287         __avail; \
288 }) \
289 )
290
291 /**
292  * kfifo_skip - skip output data
293  * @fifo: address of the fifo to be used
294  */
295 #define kfifo_skip(fifo) \
296 (void)({ \
297         typeof((fifo) + 1) __tmp = (fifo); \
298         const size_t __recsize = sizeof(*__tmp->rectype); \
299         struct __kfifo *__kfifo = &__tmp->kfifo; \
300         if (__recsize) \
301                 __kfifo_skip_r(__kfifo, __recsize); \
302         else \
303                 __kfifo->out++; \
304 })
305
306 /**
307  * kfifo_peek_len - gets the size of the next fifo record
308  * @fifo: address of the fifo to be used
309  *
310  * This function returns the size of the next fifo record in number of bytes.
311  */
312 #define kfifo_peek_len(fifo) \
313 __kfifo_uint_must_check_helper( \
314 ({ \
315         typeof((fifo) + 1) __tmp = (fifo); \
316         const size_t __recsize = sizeof(*__tmp->rectype); \
317         struct __kfifo *__kfifo = &__tmp->kfifo; \
318         (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
319         __kfifo_len_r(__kfifo, __recsize); \
320 }) \
321 )
322
323 /**
324  * kfifo_alloc - dynamically allocates a new fifo buffer
325  * @fifo: pointer to the fifo
326  * @size: the number of elements in the fifo, this must be a power of 2
327  * @gfp_mask: get_free_pages mask, passed to kmalloc()
328  *
329  * This macro dynamically allocates a new fifo buffer.
330  *
331  * The numer of elements will be rounded-up to a power of 2.
332  * The fifo will be release with kfifo_free().
333  * Return 0 if no error, otherwise an error code.
334  */
335 #define kfifo_alloc(fifo, size, gfp_mask) \
336 __kfifo_int_must_check_helper( \
337 ({ \
338         typeof((fifo) + 1) __tmp = (fifo); \
339         struct __kfifo *__kfifo = &__tmp->kfifo; \
340         __is_kfifo_ptr(__tmp) ? \
341         __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
342         -EINVAL; \
343 }) \
344 )
345
346 /**
347  * kfifo_free - frees the fifo
348  * @fifo: the fifo to be freed
349  */
350 #define kfifo_free(fifo) \
351 ({ \
352         typeof((fifo) + 1) __tmp = (fifo); \
353         struct __kfifo *__kfifo = &__tmp->kfifo; \
354         if (__is_kfifo_ptr(__tmp)) \
355                 __kfifo_free(__kfifo); \
356 })
357
358 /**
359  * kfifo_init - initialize a fifo using a preallocated buffer
360  * @fifo: the fifo to assign the buffer
361  * @buffer: the preallocated buffer to be used
362  * @size: the size of the internal buffer, this have to be a power of 2
363  *
364  * This macro initialize a fifo using a preallocated buffer.
365  *
366  * The numer of elements will be rounded-up to a power of 2.
367  * Return 0 if no error, otherwise an error code.
368  */
369 #define kfifo_init(fifo, buffer, size) \
370 ({ \
371         typeof((fifo) + 1) __tmp = (fifo); \
372         struct __kfifo *__kfifo = &__tmp->kfifo; \
373         __is_kfifo_ptr(__tmp) ? \
374         __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
375         -EINVAL; \
376 })
377
378 /**
379  * kfifo_put - put data into the fifo
380  * @fifo: address of the fifo to be used
381  * @val: the data to be added
382  *
383  * This macro copies the given value into the fifo.
384  * It returns 0 if the fifo was full. Otherwise it returns the number
385  * processed elements.
386  *
387  * Note that with only one concurrent reader and one concurrent
388  * writer, you don't need extra locking to use these macro.
389  */
390 #define kfifo_put(fifo, val) \
391 ({ \
392         typeof((fifo) + 1) __tmp = (fifo); \
393         typeof((val) + 1) __val = (val); \
394         unsigned int __ret; \
395         const size_t __recsize = sizeof(*__tmp->rectype); \
396         struct __kfifo *__kfifo = &__tmp->kfifo; \
397         if (0) { \
398                 typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
399                 __dummy = (typeof(__val))NULL; \
400         } \
401         if (__recsize) \
402                 __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \
403                         __recsize); \
404         else { \
405                 __ret = !kfifo_is_full(__tmp); \
406                 if (__ret) { \
407                         (__is_kfifo_ptr(__tmp) ? \
408                         ((typeof(__tmp->type))__kfifo->data) : \
409                         (__tmp->buf) \
410                         )[__kfifo->in & __tmp->kfifo.mask] = \
411                                 *(typeof(__tmp->type))__val; \
412                         smp_wmb(); \
413                         __kfifo->in++; \
414                 } \
415         } \
416         __ret; \
417 })
418
419 /**
420  * kfifo_get - get data from the fifo
421  * @fifo: address of the fifo to be used
422  * @val: the var where to store the data to be added
423  *
424  * This macro reads the data from the fifo.
425  * It returns 0 if the fifo was empty. Otherwise it returns the number
426  * processed elements.
427  *
428  * Note that with only one concurrent reader and one concurrent
429  * writer, you don't need extra locking to use these macro.
430  */
431 #define kfifo_get(fifo, val) \
432 __kfifo_uint_must_check_helper( \
433 ({ \
434         typeof((fifo) + 1) __tmp = (fifo); \
435         typeof((val) + 1) __val = (val); \
436         unsigned int __ret; \
437         const size_t __recsize = sizeof(*__tmp->rectype); \
438         struct __kfifo *__kfifo = &__tmp->kfifo; \
439         if (0) \
440                 __val = (typeof(__tmp->ptr))0; \
441         if (__recsize) \
442                 __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
443                         __recsize); \
444         else { \
445                 __ret = !kfifo_is_empty(__tmp); \
446                 if (__ret) { \
447                         *(typeof(__tmp->type))__val = \
448                                 (__is_kfifo_ptr(__tmp) ? \
449                                 ((typeof(__tmp->type))__kfifo->data) : \
450                                 (__tmp->buf) \
451                                 )[__kfifo->out & __tmp->kfifo.mask]; \
452                         smp_wmb(); \
453                         __kfifo->out++; \
454                 } \
455         } \
456         __ret; \
457 }) \
458 )
459
460 /**
461  * kfifo_peek - get data from the fifo without removing
462  * @fifo: address of the fifo to be used
463  * @val: the var where to store the data to be added
464  *
465  * This reads the data from the fifo without removing it from the fifo.
466  * It returns 0 if the fifo was empty. Otherwise it returns the number
467  * processed elements.
468  *
469  * Note that with only one concurrent reader and one concurrent
470  * writer, you don't need extra locking to use these macro.
471  */
472 #define kfifo_peek(fifo, val) \
473 __kfifo_uint_must_check_helper( \
474 ({ \
475         typeof((fifo) + 1) __tmp = (fifo); \
476         typeof((val) + 1) __val = (val); \
477         unsigned int __ret; \
478         const size_t __recsize = sizeof(*__tmp->rectype); \
479         struct __kfifo *__kfifo = &__tmp->kfifo; \
480         if (0) \
481                 __val = (typeof(__tmp->ptr))NULL; \
482         if (__recsize) \
483                 __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
484                         __recsize); \
485         else { \
486                 __ret = !kfifo_is_empty(__tmp); \
487                 if (__ret) { \
488                         *(typeof(__tmp->type))__val = \
489                                 (__is_kfifo_ptr(__tmp) ? \
490                                 ((typeof(__tmp->type))__kfifo->data) : \
491                                 (__tmp->buf) \
492                                 )[__kfifo->out & __tmp->kfifo.mask]; \
493                         smp_wmb(); \
494                 } \
495         } \
496         __ret; \
497 }) \
498 )
499
500 /**
501  * kfifo_in - put data into the fifo
502  * @fifo: address of the fifo to be used
503  * @buf: the data to be added
504  * @n: number of elements to be added
505  *
506  * This macro copies the given buffer into the fifo and returns the
507  * number of copied elements.
508  *
509  * Note that with only one concurrent reader and one concurrent
510  * writer, you don't need extra locking to use these macro.
511  */
512 #define kfifo_in(fifo, buf, n) \
513 ({ \
514         typeof((fifo) + 1) __tmp = (fifo); \
515         typeof((buf) + 1) __buf = (buf); \
516         unsigned long __n = (n); \
517         const size_t __recsize = sizeof(*__tmp->rectype); \
518         struct __kfifo *__kfifo = &__tmp->kfifo; \
519         if (0) { \
520                 typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
521                 __dummy = (typeof(__buf))NULL; \
522         } \
523         (__recsize) ?\
524         __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
525         __kfifo_in(__kfifo, __buf, __n); \
526 })
527
528 /**
529  * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
530  * @fifo: address of the fifo to be used
531  * @buf: the data to be added
532  * @n: number of elements to be added
533  * @lock: pointer to the spinlock to use for locking
534  *
535  * This macro copies the given values buffer into the fifo and returns the
536  * number of copied elements.
537  */
538 #define kfifo_in_spinlocked(fifo, buf, n, lock) \
539 ({ \
540         unsigned long __flags; \
541         unsigned int __ret; \
542         spin_lock_irqsave(lock, __flags); \
543         __ret = kfifo_in(fifo, buf, n); \
544         spin_unlock_irqrestore(lock, __flags); \
545         __ret; \
546 })
547
548 /* alias for kfifo_in_spinlocked, will be removed in a future release */
549 #define kfifo_in_locked(fifo, buf, n, lock) \
550                 kfifo_in_spinlocked(fifo, buf, n, lock)
551
552 /**
553  * kfifo_out - get data from the fifo
554  * @fifo: address of the fifo to be used
555  * @buf: pointer to the storage buffer
556  * @n: max. number of elements to get
557  *
558  * This macro get some data from the fifo and return the numbers of elements
559  * copied.
560  *
561  * Note that with only one concurrent reader and one concurrent
562  * writer, you don't need extra locking to use these macro.
563  */
564 #define kfifo_out(fifo, buf, n) \
565 __kfifo_uint_must_check_helper( \
566 ({ \
567         typeof((fifo) + 1) __tmp = (fifo); \
568         typeof((buf) + 1) __buf = (buf); \
569         unsigned long __n = (n); \
570         const size_t __recsize = sizeof(*__tmp->rectype); \
571         struct __kfifo *__kfifo = &__tmp->kfifo; \
572         if (0) { \
573                 typeof(__tmp->ptr) __dummy = NULL; \
574                 __buf = __dummy; \
575         } \
576         (__recsize) ?\
577         __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
578         __kfifo_out(__kfifo, __buf, __n); \
579 }) \
580 )
581
582 /**
583  * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
584  * @fifo: address of the fifo to be used
585  * @buf: pointer to the storage buffer
586  * @n: max. number of elements to get
587  * @lock: pointer to the spinlock to use for locking
588  *
589  * This macro get the data from the fifo and return the numbers of elements
590  * copied.
591  */
592 #define kfifo_out_spinlocked(fifo, buf, n, lock) \
593 __kfifo_uint_must_check_helper( \
594 ({ \
595         unsigned long __flags; \
596         unsigned int __ret; \
597         spin_lock_irqsave(lock, __flags); \
598         __ret = kfifo_out(fifo, buf, n); \
599         spin_unlock_irqrestore(lock, __flags); \
600         __ret; \
601 }) \
602 )
603
604 /* alias for kfifo_out_spinlocked, will be removed in a future release */
605 #define kfifo_out_locked(fifo, buf, n, lock) \
606                 kfifo_out_spinlocked(fifo, buf, n, lock)
607
608 /**
609  * kfifo_from_user - puts some data from user space into the fifo
610  * @fifo: address of the fifo to be used
611  * @from: pointer to the data to be added
612  * @len: the length of the data to be added
613  * @copied: pointer to output variable to store the number of copied bytes
614  *
615  * This macro copies at most @len bytes from the @from into the
616  * fifo, depending of the available space and returns -EFAULT/0.
617  *
618  * Note that with only one concurrent reader and one concurrent
619  * writer, you don't need extra locking to use these macro.
620  */
621 #define kfifo_from_user(fifo, from, len, copied) \
622 __kfifo_uint_must_check_helper( \
623 ({ \
624         typeof((fifo) + 1) __tmp = (fifo); \
625         const void __user *__from = (from); \
626         unsigned int __len = (len); \
627         unsigned int *__copied = (copied); \
628         const size_t __recsize = sizeof(*__tmp->rectype); \
629         struct __kfifo *__kfifo = &__tmp->kfifo; \
630         (__recsize) ? \
631         __kfifo_from_user_r(__kfifo, __from, __len,  __copied, __recsize) : \
632         __kfifo_from_user(__kfifo, __from, __len, __copied); \
633 }) \
634 )
635
636 /**
637  * kfifo_to_user - copies data from the fifo into user space
638  * @fifo: address of the fifo to be used
639  * @to: where the data must be copied
640  * @len: the size of the destination buffer
641  * @copied: pointer to output variable to store the number of copied bytes
642  *
643  * This macro copies at most @len bytes from the fifo into the
644  * @to buffer and returns -EFAULT/0.
645  *
646  * Note that with only one concurrent reader and one concurrent
647  * writer, you don't need extra locking to use these macro.
648  */
649 #define kfifo_to_user(fifo, to, len, copied) \
650 __kfifo_uint_must_check_helper( \
651 ({ \
652         typeof((fifo) + 1) __tmp = (fifo); \
653         void __user *__to = (to); \
654         unsigned int __len = (len); \
655         unsigned int *__copied = (copied); \
656         const size_t __recsize = sizeof(*__tmp->rectype); \
657         struct __kfifo *__kfifo = &__tmp->kfifo; \
658         (__recsize) ? \
659         __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
660         __kfifo_to_user(__kfifo, __to, __len, __copied); \
661 }) \
662 )
663
664 /**
665  * kfifo_dma_in_prepare - setup a scatterlist for DMA input
666  * @fifo: address of the fifo to be used
667  * @sgl: pointer to the scatterlist array
668  * @nents: number of entries in the scatterlist array
669  * @len: number of elements to transfer
670  *
671  * This macro fills a scatterlist for DMA input.
672  * It returns the number entries in the scatterlist array.
673  *
674  * Note that with only one concurrent reader and one concurrent
675  * writer, you don't need extra locking to use these macros.
676  */
677 #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
678 ({ \
679         typeof((fifo) + 1) __tmp = (fifo); \
680         struct scatterlist *__sgl = (sgl); \
681         int __nents = (nents); \
682         unsigned int __len = (len); \
683         const size_t __recsize = sizeof(*__tmp->rectype); \
684         struct __kfifo *__kfifo = &__tmp->kfifo; \
685         (__recsize) ? \
686         __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
687         __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
688 })
689
690 /**
691  * kfifo_dma_in_finish - finish a DMA IN operation
692  * @fifo: address of the fifo to be used
693  * @len: number of bytes to received
694  *
695  * This macro finish a DMA IN operation. The in counter will be updated by
696  * the len parameter. No error checking will be done.
697  *
698  * Note that with only one concurrent reader and one concurrent
699  * writer, you don't need extra locking to use these macros.
700  */
701 #define kfifo_dma_in_finish(fifo, len) \
702 (void)({ \
703         typeof((fifo) + 1) __tmp = (fifo); \
704         unsigned int __len = (len); \
705         const size_t __recsize = sizeof(*__tmp->rectype); \
706         struct __kfifo *__kfifo = &__tmp->kfifo; \
707         if (__recsize) \
708                 __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
709         else \
710                 __kfifo->in += __len / sizeof(*__tmp->type); \
711 })
712
713 /**
714  * kfifo_dma_out_prepare - setup a scatterlist for DMA output
715  * @fifo: address of the fifo to be used
716  * @sgl: pointer to the scatterlist array
717  * @nents: number of entries in the scatterlist array
718  * @len: number of elements to transfer
719  *
720  * This macro fills a scatterlist for DMA output which at most @len bytes
721  * to transfer.
722  * It returns the number entries in the scatterlist array.
723  * A zero means there is no space available and the scatterlist is not filled.
724  *
725  * Note that with only one concurrent reader and one concurrent
726  * writer, you don't need extra locking to use these macros.
727  */
728 #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
729 ({ \
730         typeof((fifo) + 1) __tmp = (fifo);  \
731         struct scatterlist *__sgl = (sgl); \
732         int __nents = (nents); \
733         unsigned int __len = (len); \
734         const size_t __recsize = sizeof(*__tmp->rectype); \
735         struct __kfifo *__kfifo = &__tmp->kfifo; \
736         (__recsize) ? \
737         __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
738         __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
739 })
740
741 /**
742  * kfifo_dma_out_finish - finish a DMA OUT operation
743  * @fifo: address of the fifo to be used
744  * @len: number of bytes transferd
745  *
746  * This macro finish a DMA OUT operation. The out counter will be updated by
747  * the len parameter. No error checking will be done.
748  *
749  * Note that with only one concurrent reader and one concurrent
750  * writer, you don't need extra locking to use these macros.
751  */
752 #define kfifo_dma_out_finish(fifo, len) \
753 (void)({ \
754         typeof((fifo) + 1) __tmp = (fifo); \
755         unsigned int __len = (len); \
756         const size_t __recsize = sizeof(*__tmp->rectype); \
757         struct __kfifo *__kfifo = &__tmp->kfifo; \
758         if (__recsize) \
759                 __kfifo_dma_out_finish_r(__kfifo, __recsize); \
760         else \
761                 __kfifo->out += __len / sizeof(*__tmp->type); \
762 })
763
764 /**
765  * kfifo_out_peek - gets some data from the fifo
766  * @fifo: address of the fifo to be used
767  * @buf: pointer to the storage buffer
768  * @n: max. number of elements to get
769  *
770  * This macro get the data from the fifo and return the numbers of elements
771  * copied. The data is not removed from the fifo.
772  *
773  * Note that with only one concurrent reader and one concurrent
774  * writer, you don't need extra locking to use these macro.
775  */
776 #define kfifo_out_peek(fifo, buf, n) \
777 __kfifo_uint_must_check_helper( \
778 ({ \
779         typeof((fifo) + 1) __tmp = (fifo); \
780         typeof((buf) + 1) __buf = (buf); \
781         unsigned long __n = (n); \
782         const size_t __recsize = sizeof(*__tmp->rectype); \
783         struct __kfifo *__kfifo = &__tmp->kfifo; \
784         if (0) { \
785                 typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \
786                 __buf = __dummy; \
787         } \
788         (__recsize) ? \
789         __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
790         __kfifo_out_peek(__kfifo, __buf, __n); \
791 }) \
792 )
793
794 extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
795         size_t esize, gfp_t gfp_mask);
796
797 extern void __kfifo_free(struct __kfifo *fifo);
798
799 extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
800         unsigned int size, size_t esize);
801
802 extern unsigned int __kfifo_in(struct __kfifo *fifo,
803         const void *buf, unsigned int len);
804
805 extern unsigned int __kfifo_out(struct __kfifo *fifo,
806         void *buf, unsigned int len);
807
808 extern int __kfifo_from_user(struct __kfifo *fifo,
809         const void __user *from, unsigned long len, unsigned int *copied);
810
811 extern int __kfifo_to_user(struct __kfifo *fifo,
812         void __user *to, unsigned long len, unsigned int *copied);
813
814 extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
815         struct scatterlist *sgl, int nents, unsigned int len);
816
817 extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
818         struct scatterlist *sgl, int nents, unsigned int len);
819
820 extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
821         void *buf, unsigned int len);
822
823 extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
824         const void *buf, unsigned int len, size_t recsize);
825
826 extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
827         void *buf, unsigned int len, size_t recsize);
828
829 extern int __kfifo_from_user_r(struct __kfifo *fifo,
830         const void __user *from, unsigned long len, unsigned int *copied,
831         size_t recsize);
832
833 extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
834         unsigned long len, unsigned int *copied, size_t recsize);
835
836 extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
837         struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
838
839 extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
840         unsigned int len, size_t recsize);
841
842 extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
843         struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
844
845 extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
846
847 extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
848
849 extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
850
851 extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
852         void *buf, unsigned int len, size_t recsize);
853
854 extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
855
856 #endif
857 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)) */