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