Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 2 | * A generic kernel FIFO implementation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * 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 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Howto porting drivers to the new generic fifo API: |
| 25 | * |
| 26 | * - Modify the declaration of the "struct kfifo *" object into a |
| 27 | * in-place "struct kfifo" object |
| 28 | * - Init the in-place object with kfifo_alloc() or kfifo_init() |
| 29 | * Note: The address of the in-place "struct kfifo" object must be |
| 30 | * passed as the first argument to this functions |
| 31 | * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get |
| 32 | * into kfifo_out |
| 33 | * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get |
| 34 | * into kfifo_out_locked |
| 35 | * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc |
| 36 | * must be passed now to the kfifo_in_locked and kfifo_out_locked |
| 37 | * as the last parameter. |
| 38 | * - All formerly name __kfifo_* functions has been renamed into kfifo_* |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #ifndef _LINUX_KFIFO_H |
| 42 | #define _LINUX_KFIFO_H |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/kernel.h> |
| 45 | #include <linux/spinlock.h> |
| 46 | |
| 47 | struct kfifo { |
| 48 | unsigned char *buffer; /* the buffer holding the data */ |
| 49 | unsigned int size; /* the size of the allocated buffer */ |
| 50 | unsigned int in; /* data is added at offset (in % size) */ |
| 51 | unsigned int out; /* data is extracted from off. (out % size) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 54 | /* |
| 55 | * Macros for declaration and initialization of the kfifo datatype |
| 56 | */ |
| 57 | |
| 58 | /* helper macro */ |
| 59 | #define __kfifo_initializer(s, b) \ |
| 60 | (struct kfifo) { \ |
| 61 | .size = s, \ |
| 62 | .in = 0, \ |
| 63 | .out = 0, \ |
| 64 | .buffer = b \ |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer |
| 69 | * @name: name of the declared kfifo datatype |
| 70 | * @size: size of the fifo buffer |
| 71 | * |
| 72 | * Note: the macro can be used inside struct or union declaration |
| 73 | * Note: the macro creates two objects: |
| 74 | * A kfifo object with the given name and a buffer for the kfifo |
| 75 | * object named name##kfifo_buffer |
| 76 | */ |
| 77 | #define DECLARE_KFIFO(name, size) \ |
| 78 | union { \ |
| 79 | struct kfifo name; \ |
| 80 | unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \ |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO |
| 85 | * @name: name of the declared kfifo datatype |
| 86 | * @size: size of the fifo buffer |
| 87 | */ |
| 88 | #define INIT_KFIFO(name) \ |
| 89 | name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \ |
| 90 | sizeof(struct kfifo), name##kfifo_buffer) |
| 91 | |
| 92 | /** |
| 93 | * DEFINE_KFIFO - macro to define and initialize a kfifo |
| 94 | * @name: name of the declared kfifo datatype |
| 95 | * @size: size of the fifo buffer |
| 96 | * |
| 97 | * Note: the macro can be used for global and local kfifo data type variables |
| 98 | * Note: the macro creates two objects: |
| 99 | * A kfifo object with the given name and a buffer for the kfifo |
| 100 | * object named name##kfifo_buffer |
| 101 | */ |
| 102 | #define DEFINE_KFIFO(name, size) \ |
| 103 | unsigned char name##kfifo_buffer[size]; \ |
| 104 | struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer) |
| 105 | |
| 106 | #undef __kfifo_initializer |
| 107 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 108 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 109 | unsigned int size); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 110 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 111 | gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | extern void kfifo_free(struct kfifo *fifo); |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 113 | extern unsigned int kfifo_in(struct kfifo *fifo, |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 114 | const unsigned char *from, unsigned int len); |
| 115 | extern __must_check unsigned int kfifo_out(struct kfifo *fifo, |
| 116 | unsigned char *to, unsigned int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | * kfifo_reset - removes the entire FIFO contents |
| 120 | * @fifo: the fifo to be emptied. |
| 121 | */ |
| 122 | static inline void kfifo_reset(struct kfifo *fifo) |
| 123 | { |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 124 | fifo->in = fifo->out = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame^] | 128 | * kfifo_reset_out - skip FIFO contents |
| 129 | * @fifo: the fifo to be emptied. |
| 130 | */ |
| 131 | static inline void kfifo_reset_out(struct kfifo *fifo) |
| 132 | { |
| 133 | smp_mb(); |
| 134 | fifo->out = fifo->in; |
| 135 | } |
| 136 | |
| 137 | /** |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 138 | * kfifo_size - returns the size of the fifo in bytes |
| 139 | * @fifo: the fifo to be used. |
| 140 | */ |
| 141 | static inline __must_check unsigned int kfifo_size(struct kfifo *fifo) |
| 142 | { |
| 143 | return fifo->size; |
| 144 | } |
| 145 | |
| 146 | /** |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 147 | * kfifo_len - returns the number of used bytes in the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 149 | */ |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 150 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 151 | { |
| 152 | register unsigned int out; |
| 153 | |
| 154 | out = fifo->out; |
| 155 | smp_rmb(); |
| 156 | return fifo->in - out; |
| 157 | } |
| 158 | |
| 159 | /** |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 160 | * kfifo_is_empty - returns true if the fifo is empty |
| 161 | * @fifo: the fifo to be used. |
| 162 | */ |
| 163 | static inline __must_check int kfifo_is_empty(struct kfifo *fifo) |
| 164 | { |
| 165 | return fifo->in == fifo->out; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * kfifo_is_full - returns true if the fifo is full |
| 170 | * @fifo: the fifo to be used. |
| 171 | */ |
| 172 | static inline __must_check int kfifo_is_full(struct kfifo *fifo) |
| 173 | { |
| 174 | return kfifo_len(fifo) == kfifo_size(fifo); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * kfifo_avail - returns the number of bytes available in the FIFO |
| 179 | * @fifo: the fifo to be used. |
| 180 | */ |
| 181 | static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo) |
| 182 | { |
| 183 | return kfifo_size(fifo) - kfifo_len(fifo); |
| 184 | } |
| 185 | |
| 186 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 187 | * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 188 | * @fifo: the fifo to be used. |
| 189 | * @from: the data to be added. |
| 190 | * @n: the length of the data to be added. |
| 191 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | * |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 193 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * the FIFO depending on the free space, and returns the number of |
| 195 | * bytes copied. |
| 196 | */ |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 197 | static inline unsigned int kfifo_in_locked(struct kfifo *fifo, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 198 | const unsigned char *from, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
| 200 | unsigned long flags; |
| 201 | unsigned int ret; |
| 202 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 203 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 205 | ret = kfifo_in(fifo, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 207 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 213 | * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 215 | * @to: where the data must be copied. |
| 216 | * @n: the size of the destination buffer. |
| 217 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 219 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 220 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 222 | static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 223 | unsigned char *to, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | { |
| 225 | unsigned long flags; |
| 226 | unsigned int ret; |
| 227 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 228 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 230 | ret = kfifo_out(fifo, to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | /* |
| 233 | * optimization: if the FIFO is empty, set the indices to 0 |
| 234 | * so we don't wrap the next time |
| 235 | */ |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 236 | if (kfifo_is_empty(fifo)) |
| 237 | kfifo_reset(fifo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 239 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame^] | 244 | extern void kfifo_skip(struct kfifo *fifo, unsigned int len); |
| 245 | |
| 246 | extern __must_check unsigned int kfifo_from_user(struct kfifo *fifo, |
| 247 | const void __user *from, unsigned int n); |
| 248 | |
| 249 | extern __must_check unsigned int kfifo_to_user(struct kfifo *fifo, |
| 250 | void __user *to, unsigned int n); |
| 251 | |
| 252 | /** |
| 253 | * __kfifo_add_out internal helper function for updating the out offset |
| 254 | */ |
| 255 | static inline void __kfifo_add_out(struct kfifo *fifo, |
| 256 | unsigned int off) |
| 257 | { |
| 258 | smp_mb(); |
| 259 | fifo->out += off; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * __kfifo_add_in internal helper function for updating the in offset |
| 264 | */ |
| 265 | static inline void __kfifo_add_in(struct kfifo *fifo, |
| 266 | unsigned int off) |
| 267 | { |
| 268 | smp_wmb(); |
| 269 | fifo->in += off; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * __kfifo_off internal helper function for calculating the index of a |
| 274 | * given offeset |
| 275 | */ |
| 276 | static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off) |
| 277 | { |
| 278 | return off & (fifo->size - 1); |
| 279 | } |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | #endif |