Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 2 | * A generic kernel FIFO implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 4 | * Copyright (C) 2013 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 21 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 22 | #ifndef _LINUX_KFIFO_H |
| 23 | #define _LINUX_KFIFO_H |
| 24 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 25 | /* |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 26 | * How to porting drivers to the new generic FIFO API: |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 27 | * |
| 28 | * - Modify the declaration of the "struct kfifo *" object into a |
| 29 | * in-place "struct kfifo" object |
| 30 | * - Init the in-place object with kfifo_alloc() or kfifo_init() |
| 31 | * Note: The address of the in-place "struct kfifo" object must be |
| 32 | * passed as the first argument to this functions |
| 33 | * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get |
| 34 | * into kfifo_out |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 35 | * - Replace the use of kfifo_put into kfifo_in_spinlocked and kfifo_get |
| 36 | * into kfifo_out_spinlocked |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 37 | * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 38 | * must be passed now to the kfifo_in_spinlocked and kfifo_out_spinlocked |
| 39 | * as the last parameter |
| 40 | * - The formerly __kfifo_* functions are renamed into kfifo_* |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 41 | */ |
| 42 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 43 | /* |
| 44 | * Note about locking : There is no locking required until only * one reader |
| 45 | * and one writer is using the fifo and no kfifo_reset() will be * called |
| 46 | * kfifo_reset_out() can be safely used, until it will be only called |
| 47 | * in the reader thread. |
| 48 | * For multiple writer and one reader there is only a need to lock the writer. |
| 49 | * And vice versa for only one writer and multiple reader there is only a need |
| 50 | * to lock the reader. |
| 51 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <linux/kernel.h> |
| 54 | #include <linux/spinlock.h> |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 55 | #include <linux/stddef.h> |
| 56 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 58 | struct __kfifo { |
| 59 | unsigned int in; |
| 60 | unsigned int out; |
| 61 | unsigned int mask; |
| 62 | unsigned int esize; |
| 63 | void *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 66 | #define __STRUCT_KFIFO_COMMON(datatype, recsize, ptrtype) \ |
| 67 | union { \ |
| 68 | struct __kfifo kfifo; \ |
| 69 | datatype *type; \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 70 | const datatype *const_type; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 71 | char (*rectype)[recsize]; \ |
| 72 | ptrtype *ptr; \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 73 | ptrtype const *ptr_const; \ |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 76 | #define __STRUCT_KFIFO(type, size, recsize, ptrtype) \ |
| 77 | { \ |
| 78 | __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ |
| 79 | type buf[((size < 2) || (size & (size - 1))) ? -1 : size]; \ |
| 80 | } |
| 81 | |
| 82 | #define STRUCT_KFIFO(type, size) \ |
| 83 | struct __STRUCT_KFIFO(type, size, 0, type) |
| 84 | |
| 85 | #define __STRUCT_KFIFO_PTR(type, recsize, ptrtype) \ |
| 86 | { \ |
| 87 | __STRUCT_KFIFO_COMMON(type, recsize, ptrtype); \ |
| 88 | type buf[0]; \ |
| 89 | } |
| 90 | |
| 91 | #define STRUCT_KFIFO_PTR(type) \ |
| 92 | struct __STRUCT_KFIFO_PTR(type, 0, type) |
| 93 | |
| 94 | /* |
| 95 | * define compatibility "struct kfifo" for dynamic allocated fifos |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 96 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 97 | struct kfifo __STRUCT_KFIFO_PTR(unsigned char, 0, void); |
| 98 | |
| 99 | #define STRUCT_KFIFO_REC_1(size) \ |
| 100 | struct __STRUCT_KFIFO(unsigned char, size, 1, void) |
| 101 | |
| 102 | #define STRUCT_KFIFO_REC_2(size) \ |
| 103 | struct __STRUCT_KFIFO(unsigned char, size, 2, void) |
| 104 | |
| 105 | /* |
| 106 | * define kfifo_rec types |
| 107 | */ |
| 108 | struct kfifo_rec_ptr_1 __STRUCT_KFIFO_PTR(unsigned char, 1, void); |
| 109 | struct kfifo_rec_ptr_2 __STRUCT_KFIFO_PTR(unsigned char, 2, void); |
| 110 | |
| 111 | /* |
| 112 | * helper macro to distinguish between real in place fifo where the fifo |
| 113 | * array is a part of the structure and the fifo type where the array is |
| 114 | * outside of the fifo structure. |
| 115 | */ |
| 116 | #define __is_kfifo_ptr(fifo) (sizeof(*fifo) == sizeof(struct __kfifo)) |
| 117 | |
| 118 | /** |
| 119 | * DECLARE_KFIFO_PTR - macro to declare a fifo pointer object |
| 120 | * @fifo: name of the declared fifo |
| 121 | * @type: type of the fifo elements |
| 122 | */ |
| 123 | #define DECLARE_KFIFO_PTR(fifo, type) STRUCT_KFIFO_PTR(type) fifo |
| 124 | |
| 125 | /** |
| 126 | * DECLARE_KFIFO - macro to declare a fifo object |
| 127 | * @fifo: name of the declared fifo |
| 128 | * @type: type of the fifo elements |
| 129 | * @size: the number of elements in the fifo, this must be a power of 2 |
| 130 | */ |
| 131 | #define DECLARE_KFIFO(fifo, type, size) STRUCT_KFIFO(type, size) fifo |
| 132 | |
| 133 | /** |
| 134 | * INIT_KFIFO - Initialize a fifo declared by DECLARE_KFIFO |
| 135 | * @fifo: name of the declared fifo datatype |
| 136 | */ |
| 137 | #define INIT_KFIFO(fifo) \ |
| 138 | (void)({ \ |
| 139 | typeof(&(fifo)) __tmp = &(fifo); \ |
| 140 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 141 | __kfifo->in = 0; \ |
| 142 | __kfifo->out = 0; \ |
| 143 | __kfifo->mask = __is_kfifo_ptr(__tmp) ? 0 : ARRAY_SIZE(__tmp->buf) - 1;\ |
| 144 | __kfifo->esize = sizeof(*__tmp->buf); \ |
| 145 | __kfifo->data = __is_kfifo_ptr(__tmp) ? NULL : __tmp->buf; \ |
| 146 | }) |
| 147 | |
| 148 | /** |
| 149 | * DEFINE_KFIFO - macro to define and initialize a fifo |
| 150 | * @fifo: name of the declared fifo datatype |
| 151 | * @type: type of the fifo elements |
| 152 | * @size: the number of elements in the fifo, this must be a power of 2 |
| 153 | * |
| 154 | * Note: the macro can be used for global and local fifo data type variables. |
| 155 | */ |
| 156 | #define DEFINE_KFIFO(fifo, type, size) \ |
| 157 | DECLARE_KFIFO(fifo, type, size) = \ |
| 158 | (typeof(fifo)) { \ |
| 159 | { \ |
| 160 | { \ |
| 161 | .in = 0, \ |
| 162 | .out = 0, \ |
| 163 | .mask = __is_kfifo_ptr(&(fifo)) ? \ |
| 164 | 0 : \ |
| 165 | ARRAY_SIZE((fifo).buf) - 1, \ |
| 166 | .esize = sizeof(*(fifo).buf), \ |
| 167 | .data = __is_kfifo_ptr(&(fifo)) ? \ |
| 168 | NULL : \ |
| 169 | (fifo).buf, \ |
| 170 | } \ |
| 171 | } \ |
| 172 | } |
| 173 | |
| 174 | |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 175 | static inline unsigned int __must_check |
| 176 | __kfifo_uint_must_check_helper(unsigned int val) |
| 177 | { |
| 178 | return val; |
| 179 | } |
| 180 | |
| 181 | static inline int __must_check |
| 182 | __kfifo_int_must_check_helper(int val) |
| 183 | { |
| 184 | return val; |
| 185 | } |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 186 | |
| 187 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 188 | * kfifo_initialized - Check if the fifo is initialized |
| 189 | * @fifo: address of the fifo to check |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 190 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 191 | * Return %true if fifo is initialized, otherwise %false. |
Andi Kleen | d994ffc | 2010-01-15 17:01:17 -0800 | [diff] [blame] | 192 | * Assumes the fifo was 0 before. |
| 193 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 194 | #define kfifo_initialized(fifo) ((fifo)->kfifo.mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 197 | * kfifo_esize - returns the size of the element managed by the fifo |
| 198 | * @fifo: address of the fifo to be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 200 | #define kfifo_esize(fifo) ((fifo)->kfifo.esize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 203 | * kfifo_recsize - returns the size of the record length field |
| 204 | * @fifo: address of the fifo to be used |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 205 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 206 | #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype)) |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 207 | |
| 208 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 209 | * kfifo_size - returns the size of the fifo in elements |
| 210 | * @fifo: address of the fifo to be used |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 211 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 212 | #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1) |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 213 | |
| 214 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 215 | * kfifo_reset - removes the entire fifo content |
| 216 | * @fifo: address of the fifo to be used |
| 217 | * |
| 218 | * Note: usage of kfifo_reset() is dangerous. It should be only called when the |
| 219 | * fifo is exclusived locked or when it is secured that no other thread is |
| 220 | * accessing the fifo. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 221 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 222 | #define kfifo_reset(fifo) \ |
| 223 | (void)({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 224 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 225 | __tmp->kfifo.in = __tmp->kfifo.out = 0; \ |
| 226 | }) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 227 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 228 | /** |
| 229 | * kfifo_reset_out - skip fifo content |
| 230 | * @fifo: address of the fifo to be used |
| 231 | * |
| 232 | * Note: The usage of kfifo_reset_out() is safe until it will be only called |
| 233 | * from the reader thread and there is only one concurrent reader. Otherwise |
| 234 | * it is dangerous and must be handled in the same way as kfifo_reset(). |
| 235 | */ |
| 236 | #define kfifo_reset_out(fifo) \ |
| 237 | (void)({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 238 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 239 | __tmp->kfifo.out = __tmp->kfifo.in; \ |
| 240 | }) |
| 241 | |
| 242 | /** |
| 243 | * kfifo_len - returns the number of used elements in the fifo |
| 244 | * @fifo: address of the fifo to be used |
| 245 | */ |
| 246 | #define kfifo_len(fifo) \ |
| 247 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 248 | typeof((fifo) + 1) __tmpl = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 249 | __tmpl->kfifo.in - __tmpl->kfifo.out; \ |
| 250 | }) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 251 | |
| 252 | /** |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 253 | * kfifo_is_empty - returns true if the fifo is empty |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 254 | * @fifo: address of the fifo to be used |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 255 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 256 | #define kfifo_is_empty(fifo) \ |
| 257 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 258 | typeof((fifo) + 1) __tmpq = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 259 | __tmpq->kfifo.in == __tmpq->kfifo.out; \ |
| 260 | }) |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * kfifo_is_full - returns true if the fifo is full |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 264 | * @fifo: address of the fifo to be used |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 265 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 266 | #define kfifo_is_full(fifo) \ |
| 267 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 268 | typeof((fifo) + 1) __tmpq = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 269 | kfifo_len(__tmpq) > __tmpq->kfifo.mask; \ |
| 270 | }) |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 271 | |
| 272 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 273 | * kfifo_avail - returns the number of unused elements in the fifo |
| 274 | * @fifo: address of the fifo to be used |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 275 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 276 | #define kfifo_avail(fifo) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 277 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 278 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 279 | typeof((fifo) + 1) __tmpq = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 280 | const size_t __recsize = sizeof(*__tmpq->rectype); \ |
| 281 | unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \ |
| 282 | (__recsize) ? ((__avail <= __recsize) ? 0 : \ |
| 283 | __kfifo_max_r(__avail - __recsize, __recsize)) : \ |
| 284 | __avail; \ |
| 285 | }) \ |
| 286 | ) |
Stefani Seibold | 37bdfbb | 2009-12-21 14:37:30 -0800 | [diff] [blame] | 287 | |
| 288 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 289 | * kfifo_skip - skip output data |
| 290 | * @fifo: address of the fifo to be used |
| 291 | */ |
| 292 | #define kfifo_skip(fifo) \ |
| 293 | (void)({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 294 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 295 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 296 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 297 | if (__recsize) \ |
| 298 | __kfifo_skip_r(__kfifo, __recsize); \ |
| 299 | else \ |
| 300 | __kfifo->out++; \ |
| 301 | }) |
| 302 | |
| 303 | /** |
| 304 | * kfifo_peek_len - gets the size of the next fifo record |
| 305 | * @fifo: address of the fifo to be used |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 307 | * This function returns the size of the next fifo record in number of bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 309 | #define kfifo_peek_len(fifo) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 310 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 311 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 312 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 313 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 314 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 315 | (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \ |
| 316 | __kfifo_len_r(__kfifo, __recsize); \ |
| 317 | }) \ |
| 318 | ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 321 | * kfifo_alloc - dynamically allocates a new fifo buffer |
| 322 | * @fifo: pointer to the fifo |
| 323 | * @size: the number of elements in the fifo, this must be a power of 2 |
| 324 | * @gfp_mask: get_free_pages mask, passed to kmalloc() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 326 | * This macro dynamically allocates a new fifo buffer. |
| 327 | * |
| 328 | * The numer of elements will be rounded-up to a power of 2. |
| 329 | * The fifo will be release with kfifo_free(). |
| 330 | * Return 0 if no error, otherwise an error code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 332 | #define kfifo_alloc(fifo, size, gfp_mask) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 333 | __kfifo_int_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 334 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 335 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 336 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 337 | __is_kfifo_ptr(__tmp) ? \ |
| 338 | __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \ |
| 339 | -EINVAL; \ |
| 340 | }) \ |
| 341 | ) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 342 | |
| 343 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 344 | * kfifo_free - frees the fifo |
| 345 | * @fifo: the fifo to be freed |
| 346 | */ |
| 347 | #define kfifo_free(fifo) \ |
| 348 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 349 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 350 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 351 | if (__is_kfifo_ptr(__tmp)) \ |
| 352 | __kfifo_free(__kfifo); \ |
| 353 | }) |
| 354 | |
| 355 | /** |
| 356 | * kfifo_init - initialize a fifo using a preallocated buffer |
| 357 | * @fifo: the fifo to assign the buffer |
| 358 | * @buffer: the preallocated buffer to be used |
| 359 | * @size: the size of the internal buffer, this have to be a power of 2 |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 360 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 361 | * This macro initialize a fifo using a preallocated buffer. |
| 362 | * |
| 363 | * The numer of elements will be rounded-up to a power of 2. |
| 364 | * Return 0 if no error, otherwise an error code. |
| 365 | */ |
| 366 | #define kfifo_init(fifo, buffer, size) \ |
| 367 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 368 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 369 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 370 | __is_kfifo_ptr(__tmp) ? \ |
| 371 | __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \ |
| 372 | -EINVAL; \ |
| 373 | }) |
| 374 | |
| 375 | /** |
| 376 | * kfifo_put - put data into the fifo |
| 377 | * @fifo: address of the fifo to be used |
| 378 | * @val: the data to be added |
| 379 | * |
| 380 | * This macro copies the given value into the fifo. |
| 381 | * It returns 0 if the fifo was full. Otherwise it returns the number |
| 382 | * processed elements. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 383 | * |
| 384 | * Note that with only one concurrent reader and one concurrent |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 385 | * writer, you don't need extra locking to use these macro. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 386 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 387 | #define kfifo_put(fifo, val) \ |
| 388 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 389 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 390 | typeof(*__tmp->const_type) __val = (val); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 391 | unsigned int __ret; \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 392 | size_t __recsize = sizeof(*__tmp->rectype); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 393 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 394 | if (__recsize) \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 395 | __ret = __kfifo_in_r(__kfifo, &__val, sizeof(__val), \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 396 | __recsize); \ |
| 397 | else { \ |
| 398 | __ret = !kfifo_is_full(__tmp); \ |
| 399 | if (__ret) { \ |
| 400 | (__is_kfifo_ptr(__tmp) ? \ |
| 401 | ((typeof(__tmp->type))__kfifo->data) : \ |
| 402 | (__tmp->buf) \ |
| 403 | )[__kfifo->in & __tmp->kfifo.mask] = \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 404 | (typeof(*__tmp->type))__val; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 405 | smp_wmb(); \ |
| 406 | __kfifo->in++; \ |
| 407 | } \ |
| 408 | } \ |
| 409 | __ret; \ |
| 410 | }) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 411 | |
| 412 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 413 | * kfifo_get - get data from the fifo |
| 414 | * @fifo: address of the fifo to be used |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 415 | * @val: address where to store the data |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 416 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 417 | * This macro reads the data from the fifo. |
| 418 | * It returns 0 if the fifo was empty. Otherwise it returns the number |
| 419 | * processed elements. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 420 | * |
| 421 | * Note that with only one concurrent reader and one concurrent |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 422 | * writer, you don't need extra locking to use these macro. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 423 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 424 | #define kfifo_get(fifo, val) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 425 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 426 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 427 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 428 | typeof(__tmp->ptr) __val = (val); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 429 | unsigned int __ret; \ |
| 430 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 431 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 432 | if (__recsize) \ |
| 433 | __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \ |
| 434 | __recsize); \ |
| 435 | else { \ |
| 436 | __ret = !kfifo_is_empty(__tmp); \ |
| 437 | if (__ret) { \ |
| 438 | *(typeof(__tmp->type))__val = \ |
| 439 | (__is_kfifo_ptr(__tmp) ? \ |
| 440 | ((typeof(__tmp->type))__kfifo->data) : \ |
| 441 | (__tmp->buf) \ |
| 442 | )[__kfifo->out & __tmp->kfifo.mask]; \ |
| 443 | smp_wmb(); \ |
| 444 | __kfifo->out++; \ |
| 445 | } \ |
| 446 | } \ |
| 447 | __ret; \ |
| 448 | }) \ |
| 449 | ) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 450 | |
| 451 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 452 | * kfifo_peek - get data from the fifo without removing |
| 453 | * @fifo: address of the fifo to be used |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 454 | * @val: address where to store the data |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 455 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 456 | * This reads the data from the fifo without removing it from the fifo. |
| 457 | * It returns 0 if the fifo was empty. Otherwise it returns the number |
| 458 | * processed elements. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 459 | * |
| 460 | * Note that with only one concurrent reader and one concurrent |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 461 | * writer, you don't need extra locking to use these macro. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 462 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 463 | #define kfifo_peek(fifo, val) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 464 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 465 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 466 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 467 | typeof(__tmp->ptr) __val = (val); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 468 | unsigned int __ret; \ |
| 469 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 470 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 471 | if (__recsize) \ |
| 472 | __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \ |
| 473 | __recsize); \ |
| 474 | else { \ |
| 475 | __ret = !kfifo_is_empty(__tmp); \ |
| 476 | if (__ret) { \ |
| 477 | *(typeof(__tmp->type))__val = \ |
| 478 | (__is_kfifo_ptr(__tmp) ? \ |
| 479 | ((typeof(__tmp->type))__kfifo->data) : \ |
| 480 | (__tmp->buf) \ |
| 481 | )[__kfifo->out & __tmp->kfifo.mask]; \ |
| 482 | smp_wmb(); \ |
| 483 | } \ |
| 484 | } \ |
| 485 | __ret; \ |
| 486 | }) \ |
| 487 | ) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 488 | |
| 489 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 490 | * kfifo_in - put data into the fifo |
| 491 | * @fifo: address of the fifo to be used |
| 492 | * @buf: the data to be added |
| 493 | * @n: number of elements to be added |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 494 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 495 | * This macro copies the given buffer into the fifo and returns the |
| 496 | * number of copied elements. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 497 | * |
| 498 | * Note that with only one concurrent reader and one concurrent |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 499 | * writer, you don't need extra locking to use these macro. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 500 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 501 | #define kfifo_in(fifo, buf, n) \ |
| 502 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 503 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 504 | typeof(__tmp->ptr_const) __buf = (buf); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 505 | unsigned long __n = (n); \ |
| 506 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 507 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 508 | (__recsize) ?\ |
| 509 | __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \ |
| 510 | __kfifo_in(__kfifo, __buf, __n); \ |
| 511 | }) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 512 | |
| 513 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 514 | * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking |
| 515 | * @fifo: address of the fifo to be used |
| 516 | * @buf: the data to be added |
| 517 | * @n: number of elements to be added |
| 518 | * @lock: pointer to the spinlock to use for locking |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 519 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 520 | * This macro copies the given values buffer into the fifo and returns the |
| 521 | * number of copied elements. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 522 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 523 | #define kfifo_in_spinlocked(fifo, buf, n, lock) \ |
| 524 | ({ \ |
| 525 | unsigned long __flags; \ |
| 526 | unsigned int __ret; \ |
| 527 | spin_lock_irqsave(lock, __flags); \ |
| 528 | __ret = kfifo_in(fifo, buf, n); \ |
| 529 | spin_unlock_irqrestore(lock, __flags); \ |
| 530 | __ret; \ |
| 531 | }) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 532 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 533 | /* alias for kfifo_in_spinlocked, will be removed in a future release */ |
| 534 | #define kfifo_in_locked(fifo, buf, n, lock) \ |
| 535 | kfifo_in_spinlocked(fifo, buf, n, lock) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 536 | |
| 537 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 538 | * kfifo_out - get data from the fifo |
| 539 | * @fifo: address of the fifo to be used |
| 540 | * @buf: pointer to the storage buffer |
| 541 | * @n: max. number of elements to get |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 542 | * |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 543 | * This macro get some data from the fifo and return the numbers of elements |
| 544 | * copied. |
| 545 | * |
| 546 | * Note that with only one concurrent reader and one concurrent |
| 547 | * writer, you don't need extra locking to use these macro. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 548 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 549 | #define kfifo_out(fifo, buf, n) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 550 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 551 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 552 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 553 | typeof(__tmp->ptr) __buf = (buf); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 554 | unsigned long __n = (n); \ |
| 555 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 556 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 557 | (__recsize) ?\ |
| 558 | __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \ |
| 559 | __kfifo_out(__kfifo, __buf, __n); \ |
| 560 | }) \ |
| 561 | ) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 562 | |
| 563 | /** |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 564 | * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking |
| 565 | * @fifo: address of the fifo to be used |
| 566 | * @buf: pointer to the storage buffer |
| 567 | * @n: max. number of elements to get |
| 568 | * @lock: pointer to the spinlock to use for locking |
| 569 | * |
| 570 | * This macro get the data from the fifo and return the numbers of elements |
| 571 | * copied. |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 572 | */ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 573 | #define kfifo_out_spinlocked(fifo, buf, n, lock) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 574 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 575 | ({ \ |
| 576 | unsigned long __flags; \ |
| 577 | unsigned int __ret; \ |
| 578 | spin_lock_irqsave(lock, __flags); \ |
| 579 | __ret = kfifo_out(fifo, buf, n); \ |
| 580 | spin_unlock_irqrestore(lock, __flags); \ |
| 581 | __ret; \ |
| 582 | }) \ |
| 583 | ) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 584 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 585 | /* alias for kfifo_out_spinlocked, will be removed in a future release */ |
| 586 | #define kfifo_out_locked(fifo, buf, n, lock) \ |
| 587 | kfifo_out_spinlocked(fifo, buf, n, lock) |
| 588 | |
| 589 | /** |
| 590 | * kfifo_from_user - puts some data from user space into the fifo |
| 591 | * @fifo: address of the fifo to be used |
| 592 | * @from: pointer to the data to be added |
| 593 | * @len: the length of the data to be added |
| 594 | * @copied: pointer to output variable to store the number of copied bytes |
| 595 | * |
| 596 | * This macro copies at most @len bytes from the @from into the |
| 597 | * fifo, depending of the available space and returns -EFAULT/0. |
| 598 | * |
| 599 | * Note that with only one concurrent reader and one concurrent |
| 600 | * writer, you don't need extra locking to use these macro. |
| 601 | */ |
| 602 | #define kfifo_from_user(fifo, from, len, copied) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 603 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 604 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 605 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 606 | const void __user *__from = (from); \ |
| 607 | unsigned int __len = (len); \ |
| 608 | unsigned int *__copied = (copied); \ |
| 609 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 610 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 611 | (__recsize) ? \ |
| 612 | __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \ |
| 613 | __kfifo_from_user(__kfifo, __from, __len, __copied); \ |
| 614 | }) \ |
| 615 | ) |
| 616 | |
| 617 | /** |
| 618 | * kfifo_to_user - copies data from the fifo into user space |
| 619 | * @fifo: address of the fifo to be used |
| 620 | * @to: where the data must be copied |
| 621 | * @len: the size of the destination buffer |
| 622 | * @copied: pointer to output variable to store the number of copied bytes |
| 623 | * |
| 624 | * This macro copies at most @len bytes from the fifo into the |
| 625 | * @to buffer and returns -EFAULT/0. |
| 626 | * |
| 627 | * Note that with only one concurrent reader and one concurrent |
| 628 | * writer, you don't need extra locking to use these macro. |
| 629 | */ |
| 630 | #define kfifo_to_user(fifo, to, len, copied) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 631 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 632 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 633 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 634 | void __user *__to = (to); \ |
| 635 | unsigned int __len = (len); \ |
| 636 | unsigned int *__copied = (copied); \ |
| 637 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 638 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 639 | (__recsize) ? \ |
| 640 | __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \ |
| 641 | __kfifo_to_user(__kfifo, __to, __len, __copied); \ |
| 642 | }) \ |
| 643 | ) |
| 644 | |
| 645 | /** |
| 646 | * kfifo_dma_in_prepare - setup a scatterlist for DMA input |
| 647 | * @fifo: address of the fifo to be used |
| 648 | * @sgl: pointer to the scatterlist array |
| 649 | * @nents: number of entries in the scatterlist array |
| 650 | * @len: number of elements to transfer |
| 651 | * |
| 652 | * This macro fills a scatterlist for DMA input. |
| 653 | * It returns the number entries in the scatterlist array. |
| 654 | * |
| 655 | * Note that with only one concurrent reader and one concurrent |
| 656 | * writer, you don't need extra locking to use these macros. |
| 657 | */ |
| 658 | #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \ |
| 659 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 660 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 661 | struct scatterlist *__sgl = (sgl); \ |
| 662 | int __nents = (nents); \ |
| 663 | unsigned int __len = (len); \ |
| 664 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 665 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 666 | (__recsize) ? \ |
| 667 | __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ |
| 668 | __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \ |
| 669 | }) |
| 670 | |
| 671 | /** |
| 672 | * kfifo_dma_in_finish - finish a DMA IN operation |
| 673 | * @fifo: address of the fifo to be used |
| 674 | * @len: number of bytes to received |
| 675 | * |
| 676 | * This macro finish a DMA IN operation. The in counter will be updated by |
| 677 | * the len parameter. No error checking will be done. |
| 678 | * |
| 679 | * Note that with only one concurrent reader and one concurrent |
| 680 | * writer, you don't need extra locking to use these macros. |
| 681 | */ |
| 682 | #define kfifo_dma_in_finish(fifo, len) \ |
| 683 | (void)({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 684 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 685 | unsigned int __len = (len); \ |
| 686 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 687 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 688 | if (__recsize) \ |
| 689 | __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \ |
| 690 | else \ |
| 691 | __kfifo->in += __len / sizeof(*__tmp->type); \ |
| 692 | }) |
| 693 | |
| 694 | /** |
| 695 | * kfifo_dma_out_prepare - setup a scatterlist for DMA output |
| 696 | * @fifo: address of the fifo to be used |
| 697 | * @sgl: pointer to the scatterlist array |
| 698 | * @nents: number of entries in the scatterlist array |
| 699 | * @len: number of elements to transfer |
| 700 | * |
| 701 | * This macro fills a scatterlist for DMA output which at most @len bytes |
| 702 | * to transfer. |
| 703 | * It returns the number entries in the scatterlist array. |
| 704 | * A zero means there is no space available and the scatterlist is not filled. |
| 705 | * |
| 706 | * Note that with only one concurrent reader and one concurrent |
| 707 | * writer, you don't need extra locking to use these macros. |
| 708 | */ |
| 709 | #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \ |
| 710 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 711 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 712 | struct scatterlist *__sgl = (sgl); \ |
| 713 | int __nents = (nents); \ |
| 714 | unsigned int __len = (len); \ |
| 715 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 716 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 717 | (__recsize) ? \ |
| 718 | __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \ |
| 719 | __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \ |
| 720 | }) |
| 721 | |
| 722 | /** |
| 723 | * kfifo_dma_out_finish - finish a DMA OUT operation |
| 724 | * @fifo: address of the fifo to be used |
Masanari Iida | da3dae5 | 2014-09-09 01:27:23 +0900 | [diff] [blame] | 725 | * @len: number of bytes transferred |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 726 | * |
| 727 | * This macro finish a DMA OUT operation. The out counter will be updated by |
| 728 | * the len parameter. No error checking will be done. |
| 729 | * |
| 730 | * Note that with only one concurrent reader and one concurrent |
| 731 | * writer, you don't need extra locking to use these macros. |
| 732 | */ |
| 733 | #define kfifo_dma_out_finish(fifo, len) \ |
| 734 | (void)({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 735 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 736 | unsigned int __len = (len); \ |
| 737 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 738 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
| 739 | if (__recsize) \ |
| 740 | __kfifo_dma_out_finish_r(__kfifo, __recsize); \ |
| 741 | else \ |
| 742 | __kfifo->out += __len / sizeof(*__tmp->type); \ |
| 743 | }) |
| 744 | |
| 745 | /** |
| 746 | * kfifo_out_peek - gets some data from the fifo |
| 747 | * @fifo: address of the fifo to be used |
| 748 | * @buf: pointer to the storage buffer |
| 749 | * @n: max. number of elements to get |
| 750 | * |
| 751 | * This macro get the data from the fifo and return the numbers of elements |
| 752 | * copied. The data is not removed from the fifo. |
| 753 | * |
| 754 | * Note that with only one concurrent reader and one concurrent |
| 755 | * writer, you don't need extra locking to use these macro. |
| 756 | */ |
| 757 | #define kfifo_out_peek(fifo, buf, n) \ |
Stefani Seibold | 144ecf3 | 2010-10-27 15:34:50 -0700 | [diff] [blame] | 758 | __kfifo_uint_must_check_helper( \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 759 | ({ \ |
Huang Ying | e0bf1024 | 2010-09-09 16:37:26 -0700 | [diff] [blame] | 760 | typeof((fifo) + 1) __tmp = (fifo); \ |
Stefani Seibold | 498d319 | 2013-11-14 14:32:17 -0800 | [diff] [blame] | 761 | typeof(__tmp->ptr) __buf = (buf); \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 762 | unsigned long __n = (n); \ |
| 763 | const size_t __recsize = sizeof(*__tmp->rectype); \ |
| 764 | struct __kfifo *__kfifo = &__tmp->kfifo; \ |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 765 | (__recsize) ? \ |
| 766 | __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \ |
| 767 | __kfifo_out_peek(__kfifo, __buf, __n); \ |
| 768 | }) \ |
| 769 | ) |
| 770 | |
| 771 | extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size, |
| 772 | size_t esize, gfp_t gfp_mask); |
| 773 | |
| 774 | extern void __kfifo_free(struct __kfifo *fifo); |
| 775 | |
| 776 | extern int __kfifo_init(struct __kfifo *fifo, void *buffer, |
| 777 | unsigned int size, size_t esize); |
| 778 | |
| 779 | extern unsigned int __kfifo_in(struct __kfifo *fifo, |
| 780 | const void *buf, unsigned int len); |
| 781 | |
| 782 | extern unsigned int __kfifo_out(struct __kfifo *fifo, |
| 783 | void *buf, unsigned int len); |
| 784 | |
| 785 | extern int __kfifo_from_user(struct __kfifo *fifo, |
| 786 | const void __user *from, unsigned long len, unsigned int *copied); |
| 787 | |
| 788 | extern int __kfifo_to_user(struct __kfifo *fifo, |
| 789 | void __user *to, unsigned long len, unsigned int *copied); |
| 790 | |
| 791 | extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo, |
| 792 | struct scatterlist *sgl, int nents, unsigned int len); |
| 793 | |
| 794 | extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo, |
| 795 | struct scatterlist *sgl, int nents, unsigned int len); |
| 796 | |
| 797 | extern unsigned int __kfifo_out_peek(struct __kfifo *fifo, |
| 798 | void *buf, unsigned int len); |
| 799 | |
| 800 | extern unsigned int __kfifo_in_r(struct __kfifo *fifo, |
| 801 | const void *buf, unsigned int len, size_t recsize); |
| 802 | |
| 803 | extern unsigned int __kfifo_out_r(struct __kfifo *fifo, |
| 804 | void *buf, unsigned int len, size_t recsize); |
| 805 | |
| 806 | extern int __kfifo_from_user_r(struct __kfifo *fifo, |
| 807 | const void __user *from, unsigned long len, unsigned int *copied, |
| 808 | size_t recsize); |
| 809 | |
| 810 | extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to, |
| 811 | unsigned long len, unsigned int *copied, size_t recsize); |
| 812 | |
| 813 | extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo, |
| 814 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); |
| 815 | |
| 816 | extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo, |
| 817 | unsigned int len, size_t recsize); |
| 818 | |
| 819 | extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo, |
| 820 | struct scatterlist *sgl, int nents, unsigned int len, size_t recsize); |
| 821 | |
| 822 | extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize); |
| 823 | |
| 824 | extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize); |
| 825 | |
Andrea Righi | b35de43 | 2010-08-19 14:13:27 -0700 | [diff] [blame] | 826 | extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize); |
| 827 | |
Stefani Seibold | 2e956fb | 2010-08-10 18:03:38 -0700 | [diff] [blame] | 828 | extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo, |
| 829 | void *buf, unsigned int len, size_t recsize); |
| 830 | |
| 831 | extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 832 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | #endif |