2 * A generic kernel FIFO implementation.
4 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
5 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Howto porting drivers to the new generic fifo API:
26 * - Modify the declaration of the "struct kfifo *" object into a
27 * in-place "struct kfifo" object
28 * - Init the in-place object with kfifo_alloc() or kfifo_init()
29 * Note: The address of the in-place "struct kfifo" object must be
30 * passed as the first argument to this functions
31 * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
33 * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get
34 * into kfifo_out_locked
35 * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
36 * must be passed now to the kfifo_in_locked and kfifo_out_locked
37 * as the last parameter.
38 * - All formerly name __kfifo_* functions has been renamed into kfifo_*
41 #ifndef _LINUX_KFIFO_H
42 #define _LINUX_KFIFO_H
44 #include <linux/kernel.h>
45 #include <linux/spinlock.h>
48 unsigned char *buffer; /* the buffer holding the data */
49 unsigned int size; /* the size of the allocated buffer */
50 unsigned int in; /* data is added at offset (in % size) */
51 unsigned int out; /* data is extracted from off. (out % size) */
55 * Macros for declaration and initialization of the kfifo datatype
59 #define __kfifo_initializer(s, b) \
68 * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
69 * @name: name of the declared kfifo datatype
70 * @size: size of the fifo buffer
72 * Note: the macro can be used inside struct or union declaration
73 * Note: the macro creates two objects:
74 * A kfifo object with the given name and a buffer for the kfifo
75 * object named name##kfifo_buffer
77 #define DECLARE_KFIFO(name, size) \
80 unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
84 * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO
85 * @name: name of the declared kfifo datatype
86 * @size: size of the fifo buffer
88 #define INIT_KFIFO(name) \
89 name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
90 sizeof(struct kfifo), name##kfifo_buffer)
93 * DEFINE_KFIFO - macro to define and initialize a kfifo
94 * @name: name of the declared kfifo datatype
95 * @size: size of the fifo buffer
97 * Note: the macro can be used for global and local kfifo data type variables
98 * Note: the macro creates two objects:
99 * A kfifo object with the given name and a buffer for the kfifo
100 * object named name##kfifo_buffer
102 #define DEFINE_KFIFO(name, size) \
103 unsigned char name##kfifo_buffer[size]; \
104 struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
106 #undef __kfifo_initializer
108 extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
110 extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
112 extern void kfifo_free(struct kfifo *fifo);
113 extern unsigned int kfifo_in(struct kfifo *fifo,
114 const unsigned char *from, unsigned int len);
115 extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
116 unsigned char *to, unsigned int len);
119 * kfifo_reset - removes the entire FIFO contents
120 * @fifo: the fifo to be emptied.
122 static inline void kfifo_reset(struct kfifo *fifo)
124 fifo->in = fifo->out = 0;
128 * kfifo_size - returns the size of the fifo in bytes
129 * @fifo: the fifo to be used.
131 static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
137 * kfifo_len - returns the number of used bytes in the FIFO
138 * @fifo: the fifo to be used.
140 static inline unsigned int kfifo_len(struct kfifo *fifo)
142 register unsigned int out;
146 return fifo->in - out;
150 * kfifo_is_empty - returns true if the fifo is empty
151 * @fifo: the fifo to be used.
153 static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
155 return fifo->in == fifo->out;
159 * kfifo_is_full - returns true if the fifo is full
160 * @fifo: the fifo to be used.
162 static inline __must_check int kfifo_is_full(struct kfifo *fifo)
164 return kfifo_len(fifo) == kfifo_size(fifo);
168 * kfifo_avail - returns the number of bytes available in the FIFO
169 * @fifo: the fifo to be used.
171 static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
173 return kfifo_size(fifo) - kfifo_len(fifo);
177 * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
178 * @fifo: the fifo to be used.
179 * @from: the data to be added.
180 * @n: the length of the data to be added.
181 * @lock: pointer to the spinlock to use for locking.
183 * This function copies at most @len bytes from the @from buffer into
184 * the FIFO depending on the free space, and returns the number of
187 static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
188 const unsigned char *from, unsigned int n, spinlock_t *lock)
193 spin_lock_irqsave(lock, flags);
195 ret = kfifo_in(fifo, from, n);
197 spin_unlock_irqrestore(lock, flags);
203 * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
204 * @fifo: the fifo to be used.
205 * @to: where the data must be copied.
206 * @n: the size of the destination buffer.
207 * @lock: pointer to the spinlock to use for locking.
209 * This function copies at most @len bytes from the FIFO into the
210 * @to buffer and returns the number of copied bytes.
212 static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
213 unsigned char *to, unsigned int n, spinlock_t *lock)
218 spin_lock_irqsave(lock, flags);
220 ret = kfifo_out(fifo, to, n);
223 * optimization: if the FIFO is empty, set the indices to 0
224 * so we don't wrap the next time
226 if (kfifo_is_empty(fifo))
229 spin_unlock_irqrestore(lock, flags);