]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - include/linux/kfifo.h
Fix n800 build - only register HW keyboard on i2c board if CONFIG_MACH_NOKIA_N810
[linux-2.6-omap-h63xx.git] / include / linux / kfifo.h
1 /*
2  * A simple kernel FIFO implementation.
3  *
4  * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
5  *
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.
10  *
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.
15  *
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.
19  *
20  */
21 #ifndef _LINUX_KFIFO_H
22 #define _LINUX_KFIFO_H
23
24 #ifdef __KERNEL__
25
26 #include <linux/kernel.h>
27 #include <linux/spinlock.h>
28
29 struct kfifo {
30         unsigned char *buffer;  /* the buffer holding the data */
31         unsigned int size;      /* the size of the allocated buffer */
32         unsigned int in;        /* data is added at offset (in % size) */
33         unsigned int out;       /* data is extracted from off. (out % size) */
34         spinlock_t *lock;       /* protects concurrent modifications */
35 };
36
37 extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
38                                 gfp_t gfp_mask, spinlock_t *lock);
39 extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
40                                  spinlock_t *lock);
41 extern void kfifo_free(struct kfifo *fifo);
42 extern unsigned int __kfifo_put(struct kfifo *fifo,
43                                 unsigned char *buffer, unsigned int len);
44 extern unsigned int __kfifo_get(struct kfifo *fifo,
45                                 unsigned char *buffer, unsigned int len);
46 extern unsigned int __kfifo_get_to_user(struct kfifo *fifo,
47                                         unsigned char __user *buffer,
48                                         unsigned int len);
49
50 /**
51  * __kfifo_reset - removes the entire FIFO contents, no locking version
52  * @fifo: the fifo to be emptied.
53  */
54 static inline void __kfifo_reset(struct kfifo *fifo)
55 {
56         fifo->in = fifo->out = 0;
57 }
58
59 /**
60  * kfifo_reset - removes the entire FIFO contents
61  * @fifo: the fifo to be emptied.
62  */
63 static inline void kfifo_reset(struct kfifo *fifo)
64 {
65         unsigned long flags;
66
67         spin_lock_irqsave(fifo->lock, flags);
68
69         __kfifo_reset(fifo);
70
71         spin_unlock_irqrestore(fifo->lock, flags);
72 }
73
74 /**
75  * kfifo_put - puts some data into the FIFO
76  * @fifo: the fifo to be used.
77  * @buffer: the data to be added.
78  * @len: the length of the data to be added.
79  *
80  * This function copies at most @len bytes from the @buffer into
81  * the FIFO depending on the free space, and returns the number of
82  * bytes copied.
83  */
84 static inline unsigned int kfifo_put(struct kfifo *fifo,
85                                      unsigned char *buffer, unsigned int len)
86 {
87         unsigned long flags;
88         unsigned int ret;
89
90         spin_lock_irqsave(fifo->lock, flags);
91
92         ret = __kfifo_put(fifo, buffer, len);
93
94         spin_unlock_irqrestore(fifo->lock, flags);
95
96         return ret;
97 }
98
99 /**
100  * kfifo_get - gets some data from the FIFO
101  * @fifo: the fifo to be used.
102  * @buffer: where the data must be copied.
103  * @len: the size of the destination buffer.
104  *
105  * This function copies at most @len bytes from the FIFO into the
106  * @buffer and returns the number of copied bytes.
107  */
108 static inline unsigned int kfifo_get(struct kfifo *fifo,
109                                      unsigned char *buffer, unsigned int len)
110 {
111         unsigned long flags;
112         unsigned int ret;
113
114         spin_lock_irqsave(fifo->lock, flags);
115
116         ret = __kfifo_get(fifo, buffer, len);
117
118         /*
119          * optimization: if the FIFO is empty, set the indices to 0
120          * so we don't wrap the next time
121          */
122         if (fifo->in == fifo->out)
123                 fifo->in = fifo->out = 0;
124
125         spin_unlock_irqrestore(fifo->lock, flags);
126
127         return ret;
128 }
129
130 /**
131  * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
132  * @fifo: the fifo to be used.
133  */
134 static inline unsigned int __kfifo_len(struct kfifo *fifo)
135 {
136         return fifo->in - fifo->out;
137 }
138
139 /**
140  * kfifo_len - returns the number of bytes available in the FIFO
141  * @fifo: the fifo to be used.
142  */
143 static inline unsigned int kfifo_len(struct kfifo *fifo)
144 {
145         unsigned long flags;
146         unsigned int ret;
147
148         spin_lock_irqsave(fifo->lock, flags);
149
150         ret = __kfifo_len(fifo);
151
152         spin_unlock_irqrestore(fifo->lock, flags);
153
154         return ret;
155 }
156
157 /**
158  * kfifo_get_to_user - gets some data from the FIFO
159  * @fifo: the fifo to be used.
160  * @buffer: where the data must be copied. user buffer
161  * @len: the size of the destination buffer.
162  *
163  * This function copies at most @len bytes from the FIFO into the
164  * user @buffer and returns the number of copied bytes.
165  */
166 static inline unsigned int kfifo_get_to_user(struct kfifo *fifo,
167                                              unsigned char __user *buffer,
168                                              unsigned int len)
169 {
170         unsigned long flags;
171         unsigned int ret;
172
173         spin_lock_irqsave(fifo->lock, flags);
174
175         ret = __kfifo_get_to_user(fifo, buffer, len);
176
177         /*
178          * optimization: if the FIFO is empty, set the indices to 0
179          * so we don't wrap the next time
180          */
181         if (fifo->in == fifo->out)
182                 fifo->in = fifo->out = 0;
183
184         spin_unlock_irqrestore(fifo->lock, flags);
185
186         return ret;
187 }
188
189 #else
190 #warning "don't include kernel headers in userspace"
191 #endif /* __KERNEL__ */
192 #endif