PRCM: OMAP3: Fix to wrongly modified omap2_clk_wait_ready
[linux-ginger.git] / kernel / kfifo.c
blob6d3e9999cf763e54e3b27665f7ab086053952771
1 /*
2 * A simple kernel FIFO implementation.
4 * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
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.
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.
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.
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/kfifo.h>
27 #include <linux/log2.h>
28 #include <linux/uaccess.h>
30 /**
31 * kfifo_init - allocates a new FIFO using a preallocated buffer
32 * @buffer: the preallocated buffer to be used.
33 * @size: the size of the internal buffer, this have to be a power of 2.
34 * @gfp_mask: get_free_pages mask, passed to kmalloc()
35 * @lock: the lock to be used to protect the fifo buffer
37 * Do NOT pass the kfifo to kfifo_free() after use! Simply free the
38 * &struct kfifo with kfree().
40 struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
41 gfp_t gfp_mask, spinlock_t *lock)
43 struct kfifo *fifo;
45 /* size must be a power of 2 */
46 BUG_ON(!is_power_of_2(size));
48 fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
49 if (!fifo)
50 return ERR_PTR(-ENOMEM);
52 fifo->buffer = buffer;
53 fifo->size = size;
54 fifo->in = fifo->out = 0;
55 fifo->lock = lock;
57 return fifo;
59 EXPORT_SYMBOL(kfifo_init);
61 /**
62 * kfifo_alloc - allocates a new FIFO and its internal buffer
63 * @size: the size of the internal buffer to be allocated.
64 * @gfp_mask: get_free_pages mask, passed to kmalloc()
65 * @lock: the lock to be used to protect the fifo buffer
67 * The size will be rounded-up to a power of 2.
69 struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock)
71 unsigned char *buffer;
72 struct kfifo *ret;
75 * round up to the next power of 2, since our 'let the indices
76 * wrap' tachnique works only in this case.
78 if (size & (size - 1)) {
79 BUG_ON(size > 0x80000000);
80 size = roundup_pow_of_two(size);
83 buffer = kmalloc(size, gfp_mask);
84 if (!buffer)
85 return ERR_PTR(-ENOMEM);
87 ret = kfifo_init(buffer, size, gfp_mask, lock);
89 if (IS_ERR(ret))
90 kfree(buffer);
92 return ret;
94 EXPORT_SYMBOL(kfifo_alloc);
96 /**
97 * kfifo_free - frees the FIFO
98 * @fifo: the fifo to be freed.
100 void kfifo_free(struct kfifo *fifo)
102 kfree(fifo->buffer);
103 kfree(fifo);
105 EXPORT_SYMBOL(kfifo_free);
108 * __kfifo_put - puts some data into the FIFO, no locking version
109 * @fifo: the fifo to be used.
110 * @buffer: the data to be added.
111 * @len: the length of the data to be added.
113 * This function copies at most @len bytes from the @buffer into
114 * the FIFO depending on the free space, and returns the number of
115 * bytes copied.
117 * Note that with only one concurrent reader and one concurrent
118 * writer, you don't need extra locking to use these functions.
120 unsigned int __kfifo_put(struct kfifo *fifo,
121 unsigned char *buffer, unsigned int len)
123 unsigned int l;
125 len = min(len, fifo->size - fifo->in + fifo->out);
128 * Ensure that we sample the fifo->out index -before- we
129 * start putting bytes into the kfifo.
132 smp_mb();
134 /* first put the data starting from fifo->in to buffer end */
135 l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
136 memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
138 /* then put the rest (if any) at the beginning of the buffer */
139 memcpy(fifo->buffer, buffer + l, len - l);
142 * Ensure that we add the bytes to the kfifo -before-
143 * we update the fifo->in index.
146 smp_wmb();
148 fifo->in += len;
150 return len;
152 EXPORT_SYMBOL(__kfifo_put);
155 * __kfifo_get - gets some data from the FIFO, no locking version
156 * @fifo: the fifo to be used.
157 * @buffer: where the data must be copied.
158 * @len: the size of the destination buffer.
160 * This function copies at most @len bytes from the FIFO into the
161 * @buffer and returns the number of copied bytes.
163 * Note that with only one concurrent reader and one concurrent
164 * writer, you don't need extra locking to use these functions.
166 unsigned int __kfifo_get(struct kfifo *fifo,
167 unsigned char *buffer, unsigned int len)
169 unsigned int l;
171 len = min(len, fifo->in - fifo->out);
174 * Ensure that we sample the fifo->in index -before- we
175 * start removing bytes from the kfifo.
178 smp_rmb();
180 /* first get the data from fifo->out until the end of the buffer */
181 l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
182 memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
184 /* then get the rest (if any) from the beginning of the buffer */
185 memcpy(buffer + l, fifo->buffer, len - l);
188 * Ensure that we remove the bytes from the kfifo -before-
189 * we update the fifo->out index.
192 smp_mb();
194 fifo->out += len;
196 return len;
198 EXPORT_SYMBOL(__kfifo_get);
201 * __kfifo_get_to_user - gets some data from the FIFO, no locking version
202 * @fifo: the fifo to be used.
203 * @buffer: where the data must be copied. user buffer.
204 * @len: the size of the destination buffer.
206 * This function copies at most @len bytes from the FIFO into the
207 * user @buffer and returns the number of copied bytes.
209 * Note that with only one concurrent reader and one concurrent
210 * writer, you don't need extra locking to use these functions.
212 unsigned int __kfifo_get_to_user(struct kfifo *fifo,
213 unsigned char __user *buffer,
214 unsigned int len)
216 unsigned int n1, n2;
217 int ret;
219 len = min(len, fifo->in - fifo->out);
222 * Ensure that we sample the fifo->in index -before- we
223 * start removing bytes from the kfifo.
226 smp_rmb();
228 /* first get the data from fifo->out until the end of the buffer */
229 n1 = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
230 n2 = len -n1;
231 ret = copy_to_user(buffer,
232 fifo->buffer + (fifo->out & (fifo->size - 1)), n1);
233 if (ret) {
234 len = n1 - ret;
235 goto out;
238 /* then get the rest (if any) from the beginning of the buffer */
239 ret = copy_to_user(buffer + n1, fifo->buffer, n2);
240 if (ret)
241 len = n1 + n2 - ret;
244 * Ensure that we remove the bytes from the kfifo -before-
245 * we update the fifo->out index.
247 out:
248 smp_mb();
250 fifo->out += len;
252 return len;
254 EXPORT_SYMBOL(__kfifo_get_to_user);