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