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 |
Andi Kleen | 5dab600 | 2010-01-15 17:01:17 -0800 | [diff] [blame] | 70 | * @size: size of the fifo buffer. Must be a power of two. |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 71 | * |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 72 | * Note1: the macro can be used inside struct or union declaration |
| 73 | * Note2: the macro creates two objects: |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 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 | /** |
Rolf Eike Beer | ed656d8 | 2009-12-26 17:58:11 +0100 | [diff] [blame] | 84 | * INIT_KFIFO - Initialize a kfifo declared by DECLARE_KFIFO |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 85 | * @name: name of the declared kfifo datatype |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 86 | */ |
| 87 | #define INIT_KFIFO(name) \ |
| 88 | name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \ |
| 89 | sizeof(struct kfifo), name##kfifo_buffer) |
| 90 | |
| 91 | /** |
| 92 | * DEFINE_KFIFO - macro to define and initialize a kfifo |
| 93 | * @name: name of the declared kfifo datatype |
Andi Kleen | 5dab600 | 2010-01-15 17:01:17 -0800 | [diff] [blame] | 94 | * @size: size of the fifo buffer. Must be a power of two. |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 95 | * |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 96 | * Note1: the macro can be used for global and local kfifo data type variables |
| 97 | * Note2: the macro creates two objects: |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 98 | * A kfifo object with the given name and a buffer for the kfifo |
| 99 | * object named name##kfifo_buffer |
| 100 | */ |
| 101 | #define DEFINE_KFIFO(name, size) \ |
| 102 | unsigned char name##kfifo_buffer[size]; \ |
| 103 | struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer) |
| 104 | |
| 105 | #undef __kfifo_initializer |
| 106 | |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 107 | extern void kfifo_init(struct kfifo *fifo, void *buffer, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 108 | unsigned int size); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 109 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 110 | gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | extern void kfifo_free(struct kfifo *fifo); |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 112 | extern unsigned int kfifo_in(struct kfifo *fifo, |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 113 | const void *from, unsigned int len); |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 114 | extern __must_check unsigned int kfifo_out(struct kfifo *fifo, |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 115 | void *to, unsigned int len); |
Andi Kleen | a5b9e2c | 2010-01-15 17:01:16 -0800 | [diff] [blame] | 116 | extern __must_check unsigned int kfifo_out_peek(struct kfifo *fifo, |
| 117 | void *to, unsigned int len, unsigned offset); |
| 118 | |
Andi Kleen | d994ffc | 2010-01-15 17:01:17 -0800 | [diff] [blame] | 119 | /** |
| 120 | * kfifo_initialized - Check if kfifo is initialized. |
| 121 | * @fifo: fifo to check |
| 122 | * Return %true if FIFO is initialized, otherwise %false. |
| 123 | * Assumes the fifo was 0 before. |
| 124 | */ |
| 125 | static inline bool kfifo_initialized(struct kfifo *fifo) |
| 126 | { |
Anton Vorontsov | 5a5e0f4 | 2010-01-27 17:09:38 +0300 | [diff] [blame] | 127 | return fifo->buffer != NULL; |
Andi Kleen | d994ffc | 2010-01-15 17:01:17 -0800 | [diff] [blame] | 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | * kfifo_reset - removes the entire FIFO contents |
| 132 | * @fifo: the fifo to be emptied. |
| 133 | */ |
| 134 | static inline void kfifo_reset(struct kfifo *fifo) |
| 135 | { |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 136 | fifo->in = fifo->out = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /** |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 140 | * kfifo_reset_out - skip FIFO contents |
| 141 | * @fifo: the fifo to be emptied. |
| 142 | */ |
| 143 | static inline void kfifo_reset_out(struct kfifo *fifo) |
| 144 | { |
| 145 | smp_mb(); |
| 146 | fifo->out = fifo->in; |
| 147 | } |
| 148 | |
| 149 | /** |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 150 | * kfifo_size - returns the size of the fifo in bytes |
| 151 | * @fifo: the fifo to be used. |
| 152 | */ |
| 153 | static inline __must_check unsigned int kfifo_size(struct kfifo *fifo) |
| 154 | { |
| 155 | return fifo->size; |
| 156 | } |
| 157 | |
| 158 | /** |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 159 | * kfifo_len - returns the number of used bytes in the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 161 | */ |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 162 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 163 | { |
| 164 | register unsigned int out; |
| 165 | |
| 166 | out = fifo->out; |
| 167 | smp_rmb(); |
| 168 | return fifo->in - out; |
| 169 | } |
| 170 | |
| 171 | /** |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 172 | * kfifo_is_empty - returns true if the fifo is empty |
| 173 | * @fifo: the fifo to be used. |
| 174 | */ |
| 175 | static inline __must_check int kfifo_is_empty(struct kfifo *fifo) |
| 176 | { |
| 177 | return fifo->in == fifo->out; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * kfifo_is_full - returns true if the fifo is full |
| 182 | * @fifo: the fifo to be used. |
| 183 | */ |
| 184 | static inline __must_check int kfifo_is_full(struct kfifo *fifo) |
| 185 | { |
| 186 | return kfifo_len(fifo) == kfifo_size(fifo); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * kfifo_avail - returns the number of bytes available in the FIFO |
| 191 | * @fifo: the fifo to be used. |
| 192 | */ |
| 193 | static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo) |
| 194 | { |
| 195 | return kfifo_size(fifo) - kfifo_len(fifo); |
| 196 | } |
| 197 | |
| 198 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 199 | * 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] | 200 | * @fifo: the fifo to be used. |
| 201 | * @from: the data to be added. |
| 202 | * @n: the length of the data to be added. |
| 203 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 205 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | * the FIFO depending on the free space, and returns the number of |
| 207 | * bytes copied. |
| 208 | */ |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame] | 209 | static inline unsigned int kfifo_in_locked(struct kfifo *fifo, |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 210 | const void *from, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | unsigned long flags; |
| 213 | unsigned int ret; |
| 214 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 215 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 217 | ret = kfifo_in(fifo, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 219 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 225 | * 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] | 226 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 227 | * @to: where the data must be copied. |
| 228 | * @n: the size of the destination buffer. |
| 229 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 231 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 232 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 234 | static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 235 | void *to, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | unsigned long flags; |
| 238 | unsigned int ret; |
| 239 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 240 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 242 | ret = kfifo_out(fifo, to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 244 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | return ret; |
| 247 | } |
| 248 | |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 249 | extern void kfifo_skip(struct kfifo *fifo, unsigned int len); |
| 250 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 251 | extern __must_check int kfifo_from_user(struct kfifo *fifo, |
| 252 | const void __user *from, unsigned int n, unsigned *lenout); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 253 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame] | 254 | extern __must_check int kfifo_to_user(struct kfifo *fifo, |
| 255 | void __user *to, unsigned int n, unsigned *lenout); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 256 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 257 | /* |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 258 | * __kfifo_add_out internal helper function for updating the out offset |
| 259 | */ |
| 260 | static inline void __kfifo_add_out(struct kfifo *fifo, |
| 261 | unsigned int off) |
| 262 | { |
| 263 | smp_mb(); |
| 264 | fifo->out += off; |
| 265 | } |
| 266 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 267 | /* |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 268 | * __kfifo_add_in internal helper function for updating the in offset |
| 269 | */ |
| 270 | static inline void __kfifo_add_in(struct kfifo *fifo, |
| 271 | unsigned int off) |
| 272 | { |
| 273 | smp_wmb(); |
| 274 | fifo->in += off; |
| 275 | } |
| 276 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 277 | /* |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 278 | * __kfifo_off internal helper function for calculating the index of a |
| 279 | * given offeset |
| 280 | */ |
| 281 | static inline unsigned int __kfifo_off(struct kfifo *fifo, unsigned int off) |
| 282 | { |
| 283 | return off & (fifo->size - 1); |
| 284 | } |
| 285 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 286 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 287 | * __kfifo_peek_n internal helper function for determinate the length of |
| 288 | * the next record in the fifo |
| 289 | */ |
| 290 | static inline unsigned int __kfifo_peek_n(struct kfifo *fifo, |
| 291 | unsigned int recsize) |
| 292 | { |
| 293 | #define __KFIFO_GET(fifo, off, shift) \ |
| 294 | ((fifo)->buffer[__kfifo_off((fifo), (fifo)->out+(off))] << (shift)) |
| 295 | |
| 296 | unsigned int l; |
| 297 | |
| 298 | l = __KFIFO_GET(fifo, 0, 0); |
| 299 | |
| 300 | if (--recsize) |
| 301 | l |= __KFIFO_GET(fifo, 1, 8); |
| 302 | |
| 303 | return l; |
| 304 | #undef __KFIFO_GET |
| 305 | } |
| 306 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 307 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 308 | * __kfifo_poke_n internal helper function for storing the length of |
| 309 | * the next record into the fifo |
| 310 | */ |
| 311 | static inline void __kfifo_poke_n(struct kfifo *fifo, |
| 312 | unsigned int recsize, unsigned int n) |
| 313 | { |
| 314 | #define __KFIFO_PUT(fifo, off, val, shift) \ |
| 315 | ( \ |
| 316 | (fifo)->buffer[__kfifo_off((fifo), (fifo)->in+(off))] = \ |
| 317 | (unsigned char)((val) >> (shift)) \ |
| 318 | ) |
| 319 | |
| 320 | __KFIFO_PUT(fifo, 0, n, 0); |
| 321 | |
| 322 | if (--recsize) |
| 323 | __KFIFO_PUT(fifo, 1, n, 8); |
| 324 | #undef __KFIFO_PUT |
| 325 | } |
| 326 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 327 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 328 | * __kfifo_in_... internal functions for put date into the fifo |
| 329 | * do not call it directly, use kfifo_in_rec() instead |
| 330 | */ |
| 331 | extern unsigned int __kfifo_in_n(struct kfifo *fifo, |
| 332 | const void *from, unsigned int n, unsigned int recsize); |
| 333 | |
| 334 | extern unsigned int __kfifo_in_generic(struct kfifo *fifo, |
| 335 | const void *from, unsigned int n, unsigned int recsize); |
| 336 | |
| 337 | static inline unsigned int __kfifo_in_rec(struct kfifo *fifo, |
| 338 | const void *from, unsigned int n, unsigned int recsize) |
| 339 | { |
| 340 | unsigned int ret; |
| 341 | |
| 342 | ret = __kfifo_in_n(fifo, from, n, recsize); |
| 343 | |
| 344 | if (likely(ret == 0)) { |
| 345 | if (recsize) |
| 346 | __kfifo_poke_n(fifo, recsize, n); |
| 347 | __kfifo_add_in(fifo, n + recsize); |
| 348 | } |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * kfifo_in_rec - puts some record data into the FIFO |
| 354 | * @fifo: the fifo to be used. |
| 355 | * @from: the data to be added. |
| 356 | * @n: the length of the data to be added. |
| 357 | * @recsize: size of record field |
| 358 | * |
| 359 | * This function copies @n bytes from the @from into the FIFO and returns |
| 360 | * the number of bytes which cannot be copied. |
| 361 | * A returned value greater than the @n value means that the record doesn't |
| 362 | * fit into the buffer. |
| 363 | * |
| 364 | * Note that with only one concurrent reader and one concurrent |
| 365 | * writer, you don't need extra locking to use these functions. |
| 366 | */ |
| 367 | static inline __must_check unsigned int kfifo_in_rec(struct kfifo *fifo, |
| 368 | void *from, unsigned int n, unsigned int recsize) |
| 369 | { |
| 370 | if (!__builtin_constant_p(recsize)) |
| 371 | return __kfifo_in_generic(fifo, from, n, recsize); |
| 372 | return __kfifo_in_rec(fifo, from, n, recsize); |
| 373 | } |
| 374 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 375 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 376 | * __kfifo_out_... internal functions for get date from the fifo |
| 377 | * do not call it directly, use kfifo_out_rec() instead |
| 378 | */ |
| 379 | extern unsigned int __kfifo_out_n(struct kfifo *fifo, |
| 380 | void *to, unsigned int reclen, unsigned int recsize); |
| 381 | |
| 382 | extern unsigned int __kfifo_out_generic(struct kfifo *fifo, |
| 383 | void *to, unsigned int n, |
| 384 | unsigned int recsize, unsigned int *total); |
| 385 | |
| 386 | static inline unsigned int __kfifo_out_rec(struct kfifo *fifo, |
| 387 | void *to, unsigned int n, unsigned int recsize, |
| 388 | unsigned int *total) |
| 389 | { |
| 390 | unsigned int l; |
| 391 | |
| 392 | if (!recsize) { |
| 393 | l = n; |
| 394 | if (total) |
| 395 | *total = l; |
| 396 | } else { |
| 397 | l = __kfifo_peek_n(fifo, recsize); |
| 398 | if (total) |
| 399 | *total = l; |
| 400 | if (n < l) |
| 401 | return l; |
| 402 | } |
| 403 | |
| 404 | return __kfifo_out_n(fifo, to, l, recsize); |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * kfifo_out_rec - gets some record data from the FIFO |
| 409 | * @fifo: the fifo to be used. |
| 410 | * @to: where the data must be copied. |
| 411 | * @n: the size of the destination buffer. |
| 412 | * @recsize: size of record field |
| 413 | * @total: pointer where the total number of to copied bytes should stored |
| 414 | * |
| 415 | * This function copies at most @n bytes from the FIFO to @to and returns the |
| 416 | * number of bytes which cannot be copied. |
| 417 | * A returned value greater than the @n value means that the record doesn't |
| 418 | * fit into the @to buffer. |
| 419 | * |
| 420 | * Note that with only one concurrent reader and one concurrent |
| 421 | * writer, you don't need extra locking to use these functions. |
| 422 | */ |
| 423 | static inline __must_check unsigned int kfifo_out_rec(struct kfifo *fifo, |
| 424 | void *to, unsigned int n, unsigned int recsize, |
| 425 | unsigned int *total) |
| 426 | |
| 427 | { |
| 428 | if (!__builtin_constant_p(recsize)) |
| 429 | return __kfifo_out_generic(fifo, to, n, recsize, total); |
| 430 | return __kfifo_out_rec(fifo, to, n, recsize, total); |
| 431 | } |
| 432 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 433 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 434 | * __kfifo_from_user_... internal functions for transfer from user space into |
| 435 | * the fifo. do not call it directly, use kfifo_from_user_rec() instead |
| 436 | */ |
| 437 | extern unsigned int __kfifo_from_user_n(struct kfifo *fifo, |
| 438 | const void __user *from, unsigned int n, unsigned int recsize); |
| 439 | |
| 440 | extern unsigned int __kfifo_from_user_generic(struct kfifo *fifo, |
| 441 | const void __user *from, unsigned int n, unsigned int recsize); |
| 442 | |
| 443 | static inline unsigned int __kfifo_from_user_rec(struct kfifo *fifo, |
| 444 | const void __user *from, unsigned int n, unsigned int recsize) |
| 445 | { |
| 446 | unsigned int ret; |
| 447 | |
| 448 | ret = __kfifo_from_user_n(fifo, from, n, recsize); |
| 449 | |
| 450 | if (likely(ret == 0)) { |
| 451 | if (recsize) |
| 452 | __kfifo_poke_n(fifo, recsize, n); |
| 453 | __kfifo_add_in(fifo, n + recsize); |
| 454 | } |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * kfifo_from_user_rec - puts some data from user space into the FIFO |
| 460 | * @fifo: the fifo to be used. |
| 461 | * @from: pointer to the data to be added. |
| 462 | * @n: the length of the data to be added. |
| 463 | * @recsize: size of record field |
| 464 | * |
| 465 | * This function copies @n bytes from the @from into the |
| 466 | * FIFO and returns the number of bytes which cannot be copied. |
| 467 | * |
| 468 | * If the returned value is equal or less the @n value, the copy_from_user() |
| 469 | * functions has failed. Otherwise the record doesn't fit into the buffer. |
| 470 | * |
| 471 | * Note that with only one concurrent reader and one concurrent |
| 472 | * writer, you don't need extra locking to use these functions. |
| 473 | */ |
| 474 | static inline __must_check unsigned int kfifo_from_user_rec(struct kfifo *fifo, |
| 475 | const void __user *from, unsigned int n, unsigned int recsize) |
| 476 | { |
| 477 | if (!__builtin_constant_p(recsize)) |
| 478 | return __kfifo_from_user_generic(fifo, from, n, recsize); |
| 479 | return __kfifo_from_user_rec(fifo, from, n, recsize); |
| 480 | } |
| 481 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 482 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 483 | * __kfifo_to_user_... internal functions for transfer fifo data into user space |
| 484 | * do not call it directly, use kfifo_to_user_rec() instead |
| 485 | */ |
| 486 | extern unsigned int __kfifo_to_user_n(struct kfifo *fifo, |
| 487 | void __user *to, unsigned int n, unsigned int reclen, |
| 488 | unsigned int recsize); |
| 489 | |
| 490 | extern unsigned int __kfifo_to_user_generic(struct kfifo *fifo, |
| 491 | void __user *to, unsigned int n, unsigned int recsize, |
| 492 | unsigned int *total); |
| 493 | |
| 494 | static inline unsigned int __kfifo_to_user_rec(struct kfifo *fifo, |
| 495 | void __user *to, unsigned int n, |
| 496 | unsigned int recsize, unsigned int *total) |
| 497 | { |
| 498 | unsigned int l; |
| 499 | |
| 500 | if (!recsize) { |
| 501 | l = n; |
| 502 | if (total) |
| 503 | *total = l; |
| 504 | } else { |
| 505 | l = __kfifo_peek_n(fifo, recsize); |
| 506 | if (total) |
| 507 | *total = l; |
| 508 | if (n < l) |
| 509 | return l; |
| 510 | } |
| 511 | |
| 512 | return __kfifo_to_user_n(fifo, to, n, l, recsize); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * kfifo_to_user_rec - gets data from the FIFO and write it to user space |
| 517 | * @fifo: the fifo to be used. |
| 518 | * @to: where the data must be copied. |
| 519 | * @n: the size of the destination buffer. |
| 520 | * @recsize: size of record field |
| 521 | * @total: pointer where the total number of to copied bytes should stored |
| 522 | * |
| 523 | * This function copies at most @n bytes from the FIFO to the @to. |
| 524 | * In case of an error, the function returns the number of bytes which cannot |
| 525 | * be copied. |
| 526 | * If the returned value is equal or less the @n value, the copy_to_user() |
| 527 | * functions has failed. Otherwise the record doesn't fit into the @to buffer. |
| 528 | * |
| 529 | * Note that with only one concurrent reader and one concurrent |
| 530 | * writer, you don't need extra locking to use these functions. |
| 531 | */ |
| 532 | static inline __must_check unsigned int kfifo_to_user_rec(struct kfifo *fifo, |
| 533 | void __user *to, unsigned int n, unsigned int recsize, |
| 534 | unsigned int *total) |
| 535 | { |
| 536 | if (!__builtin_constant_p(recsize)) |
| 537 | return __kfifo_to_user_generic(fifo, to, n, recsize, total); |
| 538 | return __kfifo_to_user_rec(fifo, to, n, recsize, total); |
| 539 | } |
| 540 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 541 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 542 | * __kfifo_peek_... internal functions for peek into the next fifo record |
| 543 | * do not call it directly, use kfifo_peek_rec() instead |
| 544 | */ |
| 545 | extern unsigned int __kfifo_peek_generic(struct kfifo *fifo, |
| 546 | unsigned int recsize); |
| 547 | |
| 548 | /** |
| 549 | * kfifo_peek_rec - gets the size of the next FIFO record data |
| 550 | * @fifo: the fifo to be used. |
| 551 | * @recsize: size of record field |
| 552 | * |
| 553 | * This function returns the size of the next FIFO record in number of bytes |
| 554 | */ |
| 555 | static inline __must_check unsigned int kfifo_peek_rec(struct kfifo *fifo, |
| 556 | unsigned int recsize) |
| 557 | { |
| 558 | if (!__builtin_constant_p(recsize)) |
| 559 | return __kfifo_peek_generic(fifo, recsize); |
| 560 | if (!recsize) |
| 561 | return kfifo_len(fifo); |
| 562 | return __kfifo_peek_n(fifo, recsize); |
| 563 | } |
| 564 | |
Randy Dunlap | 9c717de | 2009-12-23 09:23:33 -0800 | [diff] [blame] | 565 | /* |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 566 | * __kfifo_skip_... internal functions for skip the next fifo record |
| 567 | * do not call it directly, use kfifo_skip_rec() instead |
| 568 | */ |
| 569 | extern void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize); |
| 570 | |
| 571 | static inline void __kfifo_skip_rec(struct kfifo *fifo, |
| 572 | unsigned int recsize) |
| 573 | { |
| 574 | unsigned int l; |
| 575 | |
| 576 | if (recsize) { |
| 577 | l = __kfifo_peek_n(fifo, recsize); |
| 578 | |
| 579 | if (l + recsize <= kfifo_len(fifo)) { |
| 580 | __kfifo_add_out(fifo, l + recsize); |
| 581 | return; |
| 582 | } |
| 583 | } |
| 584 | kfifo_reset_out(fifo); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * kfifo_skip_rec - skip the next fifo out record |
| 589 | * @fifo: the fifo to be used. |
| 590 | * @recsize: size of record field |
| 591 | * |
| 592 | * This function skips the next FIFO record |
| 593 | */ |
| 594 | static inline void kfifo_skip_rec(struct kfifo *fifo, |
| 595 | unsigned int recsize) |
| 596 | { |
| 597 | if (!__builtin_constant_p(recsize)) |
| 598 | __kfifo_skip_generic(fifo, recsize); |
| 599 | else |
| 600 | __kfifo_skip_rec(fifo, recsize); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * kfifo_avail_rec - returns the number of bytes available in a record FIFO |
| 605 | * @fifo: the fifo to be used. |
| 606 | * @recsize: size of record field |
| 607 | */ |
| 608 | static inline __must_check unsigned int kfifo_avail_rec(struct kfifo *fifo, |
| 609 | unsigned int recsize) |
| 610 | { |
| 611 | unsigned int l = kfifo_size(fifo) - kfifo_len(fifo); |
| 612 | |
| 613 | return (l > recsize) ? l - recsize : 0; |
| 614 | } |
| 615 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | #endif |