blob: e92d519f93b13e2721f25fb6e1b5d6cbf2af0125 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefani Seibold45465482009-12-21 14:37:26 -08002 * A generic kernel FIFO implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Stefani Seibold45465482009-12-21 14:37:26 -08004 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
6 *
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.
11 *
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.
16 *
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.
20 *
21 */
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/slab.h>
26#include <linux/err.h>
27#include <linux/kfifo.h>
vignesh babuf84d5a72007-07-15 23:41:34 -070028#include <linux/log2.h>
Stefani Seibolda121f242009-12-21 14:37:31 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Stefani Seibold45465482009-12-21 14:37:26 -080031static void _kfifo_init(struct kfifo *fifo, unsigned char *buffer,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080032 unsigned int size)
Stefani Seibold45465482009-12-21 14:37:26 -080033{
34 fifo->buffer = buffer;
35 fifo->size = size;
Stefani Seibold45465482009-12-21 14:37:26 -080036
37 kfifo_reset(fifo);
38}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/**
Stefani Seibold45465482009-12-21 14:37:26 -080041 * kfifo_init - initialize a FIFO using a preallocated buffer
42 * @fifo: the fifo to assign the buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * @buffer: the preallocated buffer to be used.
44 * @size: the size of the internal buffer, this have to be a power of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080047void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 /* size must be a power of 2 */
vignesh babuf84d5a72007-07-15 23:41:34 -070050 BUG_ON(!is_power_of_2(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080052 _kfifo_init(fifo, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54EXPORT_SYMBOL(kfifo_init);
55
56/**
Stefani Seibold45465482009-12-21 14:37:26 -080057 * kfifo_alloc - allocates a new FIFO internal buffer
58 * @fifo: the fifo to assign then new buffer
59 * @size: the size of the buffer to be allocated, this have to be a power of 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * @gfp_mask: get_free_pages mask, passed to kmalloc()
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
Stefani Seibold45465482009-12-21 14:37:26 -080062 * This function dynamically allocates a new fifo internal buffer
63 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * The size will be rounded-up to a power of 2.
Stefani Seibold45465482009-12-21 14:37:26 -080065 * The buffer will be release with kfifo_free().
66 * Return 0 if no error, otherwise the an error code
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080068int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 unsigned char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /*
73 * round up to the next power of 2, since our 'let the indices
Robert P. J. Dayb33112d2009-06-16 15:33:34 -070074 * wrap' technique works only in this case.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
Robert P. J. Dayb33112d2009-06-16 15:33:34 -070076 if (!is_power_of_2(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 BUG_ON(size > 0x80000000);
78 size = roundup_pow_of_two(size);
79 }
80
81 buffer = kmalloc(size, gfp_mask);
Stefani Seibold45465482009-12-21 14:37:26 -080082 if (!buffer) {
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080083 _kfifo_init(fifo, 0, 0);
Stefani Seibold45465482009-12-21 14:37:26 -080084 return -ENOMEM;
85 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080087 _kfifo_init(fifo, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Stefani Seibold45465482009-12-21 14:37:26 -080089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91EXPORT_SYMBOL(kfifo_alloc);
92
93/**
Stefani Seibold45465482009-12-21 14:37:26 -080094 * kfifo_free - frees the FIFO internal buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * @fifo: the fifo to be freed.
96 */
97void kfifo_free(struct kfifo *fifo)
98{
99 kfree(fifo->buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101EXPORT_SYMBOL(kfifo_free);
102
103/**
Stefani Seibolda121f242009-12-21 14:37:31 -0800104 * kfifo_skip - skip output data
105 * @fifo: the fifo to be used.
106 * @len: number of bytes to skip
107 */
108void kfifo_skip(struct kfifo *fifo, unsigned int len)
109{
110 if (len < kfifo_len(fifo)) {
111 __kfifo_add_out(fifo, len);
112 return;
113 }
114 kfifo_reset_out(fifo);
115}
116EXPORT_SYMBOL(kfifo_skip);
117
Stefani Seibold86d48802009-12-21 14:37:32 -0800118static inline void __kfifo_in_data(struct kfifo *fifo,
119 const void *from, unsigned int len, unsigned int off)
120{
121 unsigned int l;
122
123 /*
124 * Ensure that we sample the fifo->out index -before- we
125 * start putting bytes into the kfifo.
126 */
127
128 smp_mb();
129
130 off = __kfifo_off(fifo, fifo->in + off);
131
132 /* first put the data starting from fifo->in to buffer end */
133 l = min(len, fifo->size - off);
134 memcpy(fifo->buffer + off, from, l);
135
136 /* then put the rest (if any) at the beginning of the buffer */
137 memcpy(fifo->buffer, from + l, len - l);
138}
139
140static inline void __kfifo_out_data(struct kfifo *fifo,
141 void *to, unsigned int len, unsigned int off)
142{
143 unsigned int l;
144
145 /*
146 * Ensure that we sample the fifo->in index -before- we
147 * start removing bytes from the kfifo.
148 */
149
150 smp_rmb();
151
152 off = __kfifo_off(fifo, fifo->out + off);
153
154 /* first get the data from fifo->out until the end of the buffer */
155 l = min(len, fifo->size - off);
156 memcpy(to, fifo->buffer + off, l);
157
158 /* then get the rest (if any) from the beginning of the buffer */
159 memcpy(to + l, fifo->buffer, len - l);
160}
161
162static inline unsigned int __kfifo_from_user_data(struct kfifo *fifo,
163 const void __user *from, unsigned int len, unsigned int off)
164{
165 unsigned int l;
166 int ret;
167
168 /*
169 * Ensure that we sample the fifo->out index -before- we
170 * start putting bytes into the kfifo.
171 */
172
173 smp_mb();
174
175 off = __kfifo_off(fifo, fifo->in + off);
176
177 /* first put the data starting from fifo->in to buffer end */
178 l = min(len, fifo->size - off);
179 ret = copy_from_user(fifo->buffer + off, from, l);
180
181 if (unlikely(ret))
182 return ret + len - l;
183
184 /* then put the rest (if any) at the beginning of the buffer */
185 return copy_from_user(fifo->buffer, from + l, len - l);
186}
187
188static inline unsigned int __kfifo_to_user_data(struct kfifo *fifo,
189 void __user *to, unsigned int len, unsigned int off)
190{
191 unsigned int l;
192 int ret;
193
194 /*
195 * Ensure that we sample the fifo->in index -before- we
196 * start removing bytes from the kfifo.
197 */
198
199 smp_rmb();
200
201 off = __kfifo_off(fifo, fifo->out + off);
202
203 /* first get the data from fifo->out until the end of the buffer */
204 l = min(len, fifo->size - off);
205 ret = copy_to_user(to, fifo->buffer + off, l);
206
207 if (unlikely(ret))
208 return ret + len - l;
209
210 /* then get the rest (if any) from the beginning of the buffer */
211 return copy_to_user(to + l, fifo->buffer, len - l);
212}
213
214unsigned int __kfifo_in_n(struct kfifo *fifo,
215 const void *from, unsigned int len, unsigned int recsize)
216{
217 if (kfifo_avail(fifo) < len + recsize)
218 return len + 1;
219
220 __kfifo_in_data(fifo, from, len, recsize);
221 return 0;
222}
223EXPORT_SYMBOL(__kfifo_in_n);
224
Stefani Seibolda121f242009-12-21 14:37:31 -0800225/**
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800226 * kfifo_in - puts some data into the FIFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * @fifo: the fifo to be used.
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800228 * @from: the data to be added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * @len: the length of the data to be added.
230 *
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800231 * This function copies at most @len bytes from the @from buffer into
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * the FIFO depending on the free space, and returns the number of
233 * bytes copied.
234 *
235 * Note that with only one concurrent reader and one concurrent
236 * writer, you don't need extra locking to use these functions.
237 */
Stefani Seibold86d48802009-12-21 14:37:32 -0800238unsigned int kfifo_in(struct kfifo *fifo, const unsigned char *from,
239 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Stefani Seibold86d48802009-12-21 14:37:32 -0800241 len = min(kfifo_avail(fifo), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Stefani Seibold86d48802009-12-21 14:37:32 -0800243 __kfifo_in_data(fifo, from, len, 0);
Stefani Seibolda121f242009-12-21 14:37:31 -0800244 __kfifo_add_in(fifo, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return len;
246}
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800247EXPORT_SYMBOL(kfifo_in);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Stefani Seibold86d48802009-12-21 14:37:32 -0800249unsigned int __kfifo_in_generic(struct kfifo *fifo,
250 const void *from, unsigned int len, unsigned int recsize)
251{
252 return __kfifo_in_rec(fifo, from, len, recsize);
253}
254EXPORT_SYMBOL(__kfifo_in_generic);
255
256unsigned int __kfifo_out_n(struct kfifo *fifo,
257 void *to, unsigned int len, unsigned int recsize)
258{
259 if (kfifo_len(fifo) < len + recsize)
260 return len;
261
262 __kfifo_out_data(fifo, to, len, recsize);
263 __kfifo_add_out(fifo, len + recsize);
264 return 0;
265}
266EXPORT_SYMBOL(__kfifo_out_n);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/**
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800269 * kfifo_out - gets some data from the FIFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * @fifo: the fifo to be used.
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800271 * @to: where the data must be copied.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * @len: the size of the destination buffer.
273 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800274 * This function copies at most @len bytes from the FIFO into the
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800275 * @to buffer and returns the number of copied bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 *
277 * Note that with only one concurrent reader and one concurrent
278 * writer, you don't need extra locking to use these functions.
279 */
Stefani Seibold86d48802009-12-21 14:37:32 -0800280unsigned int kfifo_out(struct kfifo *fifo, unsigned char *to, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Stefani Seibold86d48802009-12-21 14:37:32 -0800282 len = min(kfifo_len(fifo), len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Stefani Seibold86d48802009-12-21 14:37:32 -0800284 __kfifo_out_data(fifo, to, len, 0);
Stefani Seibolda121f242009-12-21 14:37:31 -0800285 __kfifo_add_out(fifo, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return len;
288}
Stefani Seibold7acd72e2009-12-21 14:37:28 -0800289EXPORT_SYMBOL(kfifo_out);
Stefani Seibolda121f242009-12-21 14:37:31 -0800290
Stefani Seibold86d48802009-12-21 14:37:32 -0800291unsigned int __kfifo_out_generic(struct kfifo *fifo,
292 void *to, unsigned int len, unsigned int recsize,
293 unsigned int *total)
294{
295 return __kfifo_out_rec(fifo, to, len, recsize, total);
296}
297EXPORT_SYMBOL(__kfifo_out_generic);
298
299unsigned int __kfifo_from_user_n(struct kfifo *fifo,
300 const void __user *from, unsigned int len, unsigned int recsize)
301{
302 if (kfifo_avail(fifo) < len + recsize)
303 return len + 1;
304
305 return __kfifo_from_user_data(fifo, from, len, recsize);
306}
307EXPORT_SYMBOL(__kfifo_from_user_n);
308
Stefani Seibolda121f242009-12-21 14:37:31 -0800309/**
310 * kfifo_from_user - puts some data from user space into the FIFO
311 * @fifo: the fifo to be used.
312 * @from: pointer to the data to be added.
313 * @len: the length of the data to be added.
314 *
315 * This function copies at most @len bytes from the @from into the
316 * FIFO depending and returns the number of copied bytes.
317 *
318 * Note that with only one concurrent reader and one concurrent
319 * writer, you don't need extra locking to use these functions.
320 */
321unsigned int kfifo_from_user(struct kfifo *fifo,
322 const void __user *from, unsigned int len)
323{
Stefani Seibold86d48802009-12-21 14:37:32 -0800324 len = min(kfifo_avail(fifo), len);
325 len -= __kfifo_from_user_data(fifo, from, len, 0);
Stefani Seibolda121f242009-12-21 14:37:31 -0800326 __kfifo_add_in(fifo, len);
Stefani Seibolda121f242009-12-21 14:37:31 -0800327 return len;
328}
329EXPORT_SYMBOL(kfifo_from_user);
330
Stefani Seibold86d48802009-12-21 14:37:32 -0800331unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
332 const void __user *from, unsigned int len, unsigned int recsize)
333{
334 return __kfifo_from_user_rec(fifo, from, len, recsize);
335}
336EXPORT_SYMBOL(__kfifo_from_user_generic);
337
338unsigned int __kfifo_to_user_n(struct kfifo *fifo,
339 void __user *to, unsigned int len, unsigned int reclen,
340 unsigned int recsize)
341{
342 unsigned int ret;
343
344 if (kfifo_len(fifo) < reclen + recsize)
345 return len;
346
347 ret = __kfifo_to_user_data(fifo, to, reclen, recsize);
348
349 if (likely(ret == 0))
350 __kfifo_add_out(fifo, reclen + recsize);
351
352 return ret;
353}
354EXPORT_SYMBOL(__kfifo_to_user_n);
355
Stefani Seibolda121f242009-12-21 14:37:31 -0800356/**
357 * kfifo_to_user - gets data from the FIFO and write it to user space
358 * @fifo: the fifo to be used.
359 * @to: where the data must be copied.
360 * @len: the size of the destination buffer.
361 *
362 * This function copies at most @len bytes from the FIFO into the
363 * @to buffer and returns the number of copied bytes.
364 *
365 * Note that with only one concurrent reader and one concurrent
366 * writer, you don't need extra locking to use these functions.
367 */
368unsigned int kfifo_to_user(struct kfifo *fifo,
369 void __user *to, unsigned int len)
370{
Stefani Seibold86d48802009-12-21 14:37:32 -0800371 len = min(kfifo_len(fifo), len);
372 len -= __kfifo_to_user_data(fifo, to, len, 0);
Stefani Seibolda121f242009-12-21 14:37:31 -0800373 __kfifo_add_out(fifo, len);
Stefani Seibolda121f242009-12-21 14:37:31 -0800374 return len;
375}
376EXPORT_SYMBOL(kfifo_to_user);
377
Stefani Seibold86d48802009-12-21 14:37:32 -0800378unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
379 void __user *to, unsigned int len, unsigned int recsize,
380 unsigned int *total)
381{
382 return __kfifo_to_user_rec(fifo, to, len, recsize, total);
383}
384EXPORT_SYMBOL(__kfifo_to_user_generic);
385
386unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize)
387{
388 if (recsize == 0)
389 return kfifo_avail(fifo);
390
391 return __kfifo_peek_n(fifo, recsize);
392}
393EXPORT_SYMBOL(__kfifo_peek_generic);
394
395void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize)
396{
397 __kfifo_skip_rec(fifo, recsize);
398}
399EXPORT_SYMBOL(__kfifo_skip_generic);
400