MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / nozomi / kfifo.c
blob59e5eb0711e823e957de9979955d60e525b125e6
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 <asm/uaccess.h>
26 #include <linux/err.h>
27 #include "kfifo.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 unsigned int gfp_mask, void *lock)
43 struct kfifo *fifo;
45 /* size must be a power of 2 */
46 BUG_ON(size & (size - 1));
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;
56 return fifo;
59 /**
60 * kfifo_alloc - allocates a new FIFO and its internal buffer
61 * @size: the size of the internal buffer to be allocated.
62 * @gfp_mask: get_free_pages mask, passed to kmalloc()
63 * @lock: the lock to be used to protect the fifo buffer
65 * The size will be rounded-up to a power of 2.
67 struct kfifo *kfifo_alloc(unsigned int size, unsigned int gfp_mask, void *lock)
69 unsigned char *buffer;
70 struct kfifo *ret;
73 * round up to the next power of 2, since our 'let the indices
74 * wrap' tachnique works only in this case.
76 if (size & (size - 1)) {
77 BUG_ON(size > 0x80000000);
78 printk("Do not support no power of two!\n");
79 //size = roundup_pow_of_two(size);
82 buffer = kmalloc(size, gfp_mask);
83 if (!buffer)
84 return ERR_PTR(-ENOMEM);
86 ret = kfifo_init(buffer, size, gfp_mask, lock);
88 if (IS_ERR(ret))
89 kfree(buffer);
91 return ret;
94 /**
95 * kfifo_free - frees the FIFO
96 * @fifo: the fifo to be freed.
98 void kfifo_free(struct kfifo *fifo)
100 kfree(fifo->buffer);
101 kfree(fifo);
105 * __kfifo_put - puts some data into the FIFO, no locking version
106 * @fifo: the fifo to be used.
107 * @buffer: the data to be added.
108 * @len: the length of the data to be added.
110 * This function copies at most 'len' bytes from the 'buffer' into
111 * the FIFO depending on the free space, and returns the number of
112 * bytes copied.
114 * Note that with only one concurrent reader and one concurrent
115 * writer, you don't need extra locking to use these functions.
117 unsigned int __kfifo_put(struct kfifo *fifo,
118 unsigned char *buffer, unsigned int len)
120 unsigned int l;
122 len = min(len, fifo->size - fifo->in + fifo->out);
124 /* first put the data starting from fifo->in to buffer end */
125 l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
126 memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
128 /* then put the rest (if any) at the beginning of the buffer */
129 memcpy(fifo->buffer, buffer + l, len - l);
131 fifo->in += len;
133 return len;
136 /** __kfifio_put_user works like __kfifo_put, but copies data from
137 * user space.
140 unsigned int __kfifo_put_user(struct kfifo *fifo,
141 unsigned char *buffer, unsigned int len)
143 unsigned int l;
145 len = min(len, fifo->size - fifo->in + fifo->out);
147 /* first put the data starting from fifo->in to buffer end */
148 l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
149 copy_from_user(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
151 /* then put the rest (if any) at the beginning of the buffer */
152 copy_from_user(fifo->buffer, buffer + l, len - l);
154 fifo->in += len;
156 return len;
162 * __kfifo_get - gets some data from the FIFO, no locking version
163 * @fifo: the fifo to be used.
164 * @buffer: where the data must be copied.
165 * @len: the size of the destination buffer.
167 * This function copies at most 'len' bytes from the FIFO into the
168 * 'buffer' and returns the number of copied bytes.
170 * Note that with only one concurrent reader and one concurrent
171 * writer, you don't need extra locking to use these functions.
173 unsigned int __kfifo_get(struct kfifo *fifo,
174 unsigned char *buffer, unsigned int len)
176 unsigned int l;
178 len = min(len, fifo->in - fifo->out);
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);
187 fifo->out += len;
189 return len;