kfifo: use void * pointers for user buffers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / kfifo.c
blobab615e6950525c839ac84e1616945b3e6b946835
1 /*
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.
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>
28 #include <linux/log2.h>
29 #include <linux/uaccess.h>
31 static void _kfifo_init(struct kfifo *fifo, void *buffer,
32 unsigned int size)
34 fifo->buffer = buffer;
35 fifo->size = size;
37 kfifo_reset(fifo);
40 /**
41 * kfifo_init - initialize a FIFO using a preallocated buffer
42 * @fifo: the fifo to assign the buffer
43 * @buffer: the preallocated buffer to be used.
44 * @size: the size of the internal buffer, this have to be a power of 2.
47 void kfifo_init(struct kfifo *fifo, void *buffer, unsigned int size)
49 /* size must be a power of 2 */
50 BUG_ON(!is_power_of_2(size));
52 _kfifo_init(fifo, buffer, size);
54 EXPORT_SYMBOL(kfifo_init);
56 /**
57 * 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.
60 * @gfp_mask: get_free_pages mask, passed to kmalloc()
62 * This function dynamically allocates a new fifo internal buffer
64 * The size will be rounded-up to a power of 2.
65 * The buffer will be release with kfifo_free().
66 * Return 0 if no error, otherwise the an error code
68 int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
70 unsigned char *buffer;
73 * round up to the next power of 2, since our 'let the indices
74 * wrap' technique works only in this case.
76 if (!is_power_of_2(size)) {
77 BUG_ON(size > 0x80000000);
78 size = roundup_pow_of_two(size);
81 buffer = kmalloc(size, gfp_mask);
82 if (!buffer) {
83 _kfifo_init(fifo, 0, 0);
84 return -ENOMEM;
87 _kfifo_init(fifo, buffer, size);
89 return 0;
91 EXPORT_SYMBOL(kfifo_alloc);
93 /**
94 * kfifo_free - frees the FIFO internal buffer
95 * @fifo: the fifo to be freed.
97 void kfifo_free(struct kfifo *fifo)
99 kfree(fifo->buffer);
101 EXPORT_SYMBOL(kfifo_free);
104 * kfifo_skip - skip output data
105 * @fifo: the fifo to be used.
106 * @len: number of bytes to skip
108 void kfifo_skip(struct kfifo *fifo, unsigned int len)
110 if (len < kfifo_len(fifo)) {
111 __kfifo_add_out(fifo, len);
112 return;
114 kfifo_reset_out(fifo);
116 EXPORT_SYMBOL(kfifo_skip);
118 static inline void __kfifo_in_data(struct kfifo *fifo,
119 const void *from, unsigned int len, unsigned int off)
121 unsigned int l;
124 * Ensure that we sample the fifo->out index -before- we
125 * start putting bytes into the kfifo.
128 smp_mb();
130 off = __kfifo_off(fifo, fifo->in + off);
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);
136 /* then put the rest (if any) at the beginning of the buffer */
137 memcpy(fifo->buffer, from + l, len - l);
140 static inline void __kfifo_out_data(struct kfifo *fifo,
141 void *to, unsigned int len, unsigned int off)
143 unsigned int l;
146 * Ensure that we sample the fifo->in index -before- we
147 * start removing bytes from the kfifo.
150 smp_rmb();
152 off = __kfifo_off(fifo, fifo->out + off);
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);
158 /* then get the rest (if any) from the beginning of the buffer */
159 memcpy(to + l, fifo->buffer, len - l);
162 static inline unsigned int __kfifo_from_user_data(struct kfifo *fifo,
163 const void __user *from, unsigned int len, unsigned int off)
165 unsigned int l;
166 int ret;
169 * Ensure that we sample the fifo->out index -before- we
170 * start putting bytes into the kfifo.
173 smp_mb();
175 off = __kfifo_off(fifo, fifo->in + off);
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);
181 if (unlikely(ret))
182 return ret + len - l;
184 /* then put the rest (if any) at the beginning of the buffer */
185 return copy_from_user(fifo->buffer, from + l, len - l);
188 static inline unsigned int __kfifo_to_user_data(struct kfifo *fifo,
189 void __user *to, unsigned int len, unsigned int off)
191 unsigned int l;
192 int ret;
195 * Ensure that we sample the fifo->in index -before- we
196 * start removing bytes from the kfifo.
199 smp_rmb();
201 off = __kfifo_off(fifo, fifo->out + off);
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);
207 if (unlikely(ret))
208 return ret + len - l;
210 /* then get the rest (if any) from the beginning of the buffer */
211 return copy_to_user(to + l, fifo->buffer, len - l);
214 unsigned int __kfifo_in_n(struct kfifo *fifo,
215 const void *from, unsigned int len, unsigned int recsize)
217 if (kfifo_avail(fifo) < len + recsize)
218 return len + 1;
220 __kfifo_in_data(fifo, from, len, recsize);
221 return 0;
223 EXPORT_SYMBOL(__kfifo_in_n);
226 * kfifo_in - puts some data into the FIFO
227 * @fifo: the fifo to be used.
228 * @from: the data to be added.
229 * @len: the length of the data to be added.
231 * This function copies at most @len bytes from the @from buffer into
232 * the FIFO depending on the free space, and returns the number of
233 * bytes copied.
235 * Note that with only one concurrent reader and one concurrent
236 * writer, you don't need extra locking to use these functions.
238 unsigned int kfifo_in(struct kfifo *fifo, const void *from,
239 unsigned int len)
241 len = min(kfifo_avail(fifo), len);
243 __kfifo_in_data(fifo, from, len, 0);
244 __kfifo_add_in(fifo, len);
245 return len;
247 EXPORT_SYMBOL(kfifo_in);
249 unsigned int __kfifo_in_generic(struct kfifo *fifo,
250 const void *from, unsigned int len, unsigned int recsize)
252 return __kfifo_in_rec(fifo, from, len, recsize);
254 EXPORT_SYMBOL(__kfifo_in_generic);
256 unsigned int __kfifo_out_n(struct kfifo *fifo,
257 void *to, unsigned int len, unsigned int recsize)
259 if (kfifo_len(fifo) < len + recsize)
260 return len;
262 __kfifo_out_data(fifo, to, len, recsize);
263 __kfifo_add_out(fifo, len + recsize);
264 return 0;
266 EXPORT_SYMBOL(__kfifo_out_n);
269 * kfifo_out - gets some data from the FIFO
270 * @fifo: the fifo to be used.
271 * @to: where the data must be copied.
272 * @len: the size of the destination buffer.
274 * This function copies at most @len bytes from the FIFO into the
275 * @to buffer and returns the number of copied bytes.
277 * Note that with only one concurrent reader and one concurrent
278 * writer, you don't need extra locking to use these functions.
280 unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len)
282 len = min(kfifo_len(fifo), len);
284 __kfifo_out_data(fifo, to, len, 0);
285 __kfifo_add_out(fifo, len);
287 return len;
289 EXPORT_SYMBOL(kfifo_out);
291 unsigned int __kfifo_out_generic(struct kfifo *fifo,
292 void *to, unsigned int len, unsigned int recsize,
293 unsigned int *total)
295 return __kfifo_out_rec(fifo, to, len, recsize, total);
297 EXPORT_SYMBOL(__kfifo_out_generic);
299 unsigned int __kfifo_from_user_n(struct kfifo *fifo,
300 const void __user *from, unsigned int len, unsigned int recsize)
302 if (kfifo_avail(fifo) < len + recsize)
303 return len + 1;
305 return __kfifo_from_user_data(fifo, from, len, recsize);
307 EXPORT_SYMBOL(__kfifo_from_user_n);
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.
315 * This function copies at most @len bytes from the @from into the
316 * FIFO depending and returns the number of copied bytes.
318 * Note that with only one concurrent reader and one concurrent
319 * writer, you don't need extra locking to use these functions.
321 unsigned int kfifo_from_user(struct kfifo *fifo,
322 const void __user *from, unsigned int len)
324 len = min(kfifo_avail(fifo), len);
325 len -= __kfifo_from_user_data(fifo, from, len, 0);
326 __kfifo_add_in(fifo, len);
327 return len;
329 EXPORT_SYMBOL(kfifo_from_user);
331 unsigned int __kfifo_from_user_generic(struct kfifo *fifo,
332 const void __user *from, unsigned int len, unsigned int recsize)
334 return __kfifo_from_user_rec(fifo, from, len, recsize);
336 EXPORT_SYMBOL(__kfifo_from_user_generic);
338 unsigned int __kfifo_to_user_n(struct kfifo *fifo,
339 void __user *to, unsigned int len, unsigned int reclen,
340 unsigned int recsize)
342 unsigned int ret;
344 if (kfifo_len(fifo) < reclen + recsize)
345 return len;
347 ret = __kfifo_to_user_data(fifo, to, reclen, recsize);
349 if (likely(ret == 0))
350 __kfifo_add_out(fifo, reclen + recsize);
352 return ret;
354 EXPORT_SYMBOL(__kfifo_to_user_n);
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.
362 * This function copies at most @len bytes from the FIFO into the
363 * @to buffer and returns the number of copied bytes.
365 * Note that with only one concurrent reader and one concurrent
366 * writer, you don't need extra locking to use these functions.
368 unsigned int kfifo_to_user(struct kfifo *fifo,
369 void __user *to, unsigned int len)
371 len = min(kfifo_len(fifo), len);
372 len -= __kfifo_to_user_data(fifo, to, len, 0);
373 __kfifo_add_out(fifo, len);
374 return len;
376 EXPORT_SYMBOL(kfifo_to_user);
378 unsigned int __kfifo_to_user_generic(struct kfifo *fifo,
379 void __user *to, unsigned int len, unsigned int recsize,
380 unsigned int *total)
382 return __kfifo_to_user_rec(fifo, to, len, recsize, total);
384 EXPORT_SYMBOL(__kfifo_to_user_generic);
386 unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize)
388 if (recsize == 0)
389 return kfifo_avail(fifo);
391 return __kfifo_peek_n(fifo, recsize);
393 EXPORT_SYMBOL(__kfifo_peek_generic);
395 void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize)
397 __kfifo_skip_rec(fifo, recsize);
399 EXPORT_SYMBOL(__kfifo_skip_generic);