Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 2 | * A generic kernel FIFO implementation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/kfifo.h> |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 28 | #include <linux/log2.h> |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 29 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 31 | static void _kfifo_init(struct kfifo *fifo, void *buffer, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 32 | unsigned int size) |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 33 | { |
| 34 | fifo->buffer = buffer; |
| 35 | fifo->size = size; |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 36 | |
| 37 | kfifo_reset(fifo); |
| 38 | } |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 41 | * kfifo_init - initialize a FIFO using a preallocated buffer |
| 42 | * @fifo: the fifo to assign the buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | * @buffer: the preallocated buffer to be used. |
| 44 | * @size: the size of the internal buffer, this have to be a power of 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | */ |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 47 | void kfifo_init(struct kfifo *fifo, void *buffer, unsigned int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* size must be a power of 2 */ |
vignesh babu | f84d5a7 | 2007-07-15 23:41:34 -0700 | [diff] [blame] | 50 | BUG_ON(!is_power_of_2(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 52 | _kfifo_init(fifo, buffer, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | EXPORT_SYMBOL(kfifo_init); |
| 55 | |
| 56 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 57 | * kfifo_alloc - allocates a new FIFO internal buffer |
| 58 | * @fifo: the fifo to assign then new buffer |
| 59 | * @size: the size of the buffer to be allocated, this have to be a power of 2. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * @gfp_mask: get_free_pages mask, passed to kmalloc() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 62 | * This function dynamically allocates a new fifo internal buffer |
| 63 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * The size will be rounded-up to a power of 2. |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 65 | * The buffer will be release with kfifo_free(). |
| 66 | * Return 0 if no error, otherwise the an error code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 68 | int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | { |
| 70 | unsigned char *buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * round up to the next power of 2, since our 'let the indices |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 74 | * wrap' technique works only in this case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | */ |
Robert P. J. Day | b33112d | 2009-06-16 15:33:34 -0700 | [diff] [blame] | 76 | if (!is_power_of_2(size)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | BUG_ON(size > 0x80000000); |
| 78 | size = roundup_pow_of_two(size); |
| 79 | } |
| 80 | |
| 81 | buffer = kmalloc(size, gfp_mask); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 82 | if (!buffer) { |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 83 | _kfifo_init(fifo, 0, 0); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 84 | return -ENOMEM; |
| 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 87 | _kfifo_init(fifo, buffer, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 89 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | EXPORT_SYMBOL(kfifo_alloc); |
| 92 | |
| 93 | /** |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 94 | * kfifo_free - frees the FIFO internal buffer |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * @fifo: the fifo to be freed. |
| 96 | */ |
| 97 | void kfifo_free(struct kfifo *fifo) |
| 98 | { |
| 99 | kfree(fifo->buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | EXPORT_SYMBOL(kfifo_free); |
| 102 | |
| 103 | /** |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 104 | * kfifo_skip - skip output data |
| 105 | * @fifo: the fifo to be used. |
| 106 | * @len: number of bytes to skip |
| 107 | */ |
| 108 | void kfifo_skip(struct kfifo *fifo, unsigned int len) |
| 109 | { |
| 110 | if (len < kfifo_len(fifo)) { |
| 111 | __kfifo_add_out(fifo, len); |
| 112 | return; |
| 113 | } |
| 114 | kfifo_reset_out(fifo); |
| 115 | } |
| 116 | EXPORT_SYMBOL(kfifo_skip); |
| 117 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 118 | static inline void __kfifo_in_data(struct kfifo *fifo, |
| 119 | const void *from, unsigned int len, unsigned int off) |
| 120 | { |
| 121 | unsigned int l; |
| 122 | |
| 123 | /* |
| 124 | * Ensure that we sample the fifo->out index -before- we |
| 125 | * start putting bytes into the kfifo. |
| 126 | */ |
| 127 | |
| 128 | smp_mb(); |
| 129 | |
| 130 | off = __kfifo_off(fifo, fifo->in + off); |
| 131 | |
| 132 | /* first put the data starting from fifo->in to buffer end */ |
| 133 | l = min(len, fifo->size - off); |
| 134 | memcpy(fifo->buffer + off, from, l); |
| 135 | |
| 136 | /* then put the rest (if any) at the beginning of the buffer */ |
| 137 | memcpy(fifo->buffer, from + l, len - l); |
| 138 | } |
| 139 | |
| 140 | static inline void __kfifo_out_data(struct kfifo *fifo, |
| 141 | void *to, unsigned int len, unsigned int off) |
| 142 | { |
| 143 | unsigned int l; |
| 144 | |
| 145 | /* |
| 146 | * Ensure that we sample the fifo->in index -before- we |
| 147 | * start removing bytes from the kfifo. |
| 148 | */ |
| 149 | |
| 150 | smp_rmb(); |
| 151 | |
| 152 | off = __kfifo_off(fifo, fifo->out + off); |
| 153 | |
| 154 | /* first get the data from fifo->out until the end of the buffer */ |
| 155 | l = min(len, fifo->size - off); |
| 156 | memcpy(to, fifo->buffer + off, l); |
| 157 | |
| 158 | /* then get the rest (if any) from the beginning of the buffer */ |
| 159 | memcpy(to + l, fifo->buffer, len - l); |
| 160 | } |
| 161 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 162 | static inline int __kfifo_from_user_data(struct kfifo *fifo, |
| 163 | const void __user *from, unsigned int len, unsigned int off, |
| 164 | unsigned *lenout) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 165 | { |
| 166 | unsigned int l; |
| 167 | int ret; |
| 168 | |
| 169 | /* |
| 170 | * Ensure that we sample the fifo->out index -before- we |
| 171 | * start putting bytes into the kfifo. |
| 172 | */ |
| 173 | |
| 174 | smp_mb(); |
| 175 | |
| 176 | off = __kfifo_off(fifo, fifo->in + off); |
| 177 | |
| 178 | /* first put the data starting from fifo->in to buffer end */ |
| 179 | l = min(len, fifo->size - off); |
| 180 | ret = copy_from_user(fifo->buffer + off, from, l); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 181 | if (unlikely(ret)) { |
| 182 | *lenout = ret; |
| 183 | return -EFAULT; |
| 184 | } |
| 185 | *lenout = l; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 186 | |
| 187 | /* then put the rest (if any) at the beginning of the buffer */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 188 | ret = copy_from_user(fifo->buffer, from + l, len - l); |
| 189 | *lenout += ret ? ret : len - l; |
| 190 | return ret ? -EFAULT : 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 193 | static inline int __kfifo_to_user_data(struct kfifo *fifo, |
| 194 | void __user *to, unsigned int len, unsigned int off, unsigned *lenout) |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 195 | { |
| 196 | unsigned int l; |
| 197 | int ret; |
| 198 | |
| 199 | /* |
| 200 | * Ensure that we sample the fifo->in index -before- we |
| 201 | * start removing bytes from the kfifo. |
| 202 | */ |
| 203 | |
| 204 | smp_rmb(); |
| 205 | |
| 206 | off = __kfifo_off(fifo, fifo->out + off); |
| 207 | |
| 208 | /* first get the data from fifo->out until the end of the buffer */ |
| 209 | l = min(len, fifo->size - off); |
| 210 | ret = copy_to_user(to, fifo->buffer + off, l); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 211 | *lenout = l; |
| 212 | if (unlikely(ret)) { |
| 213 | *lenout -= ret; |
| 214 | return -EFAULT; |
| 215 | } |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 216 | |
| 217 | /* then get the rest (if any) from the beginning of the buffer */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 218 | len -= l; |
| 219 | ret = copy_to_user(to + l, fifo->buffer, len); |
| 220 | if (unlikely(ret)) { |
| 221 | *lenout += len - ret; |
| 222 | return -EFAULT; |
| 223 | } |
| 224 | *lenout += len; |
| 225 | return 0; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | unsigned int __kfifo_in_n(struct kfifo *fifo, |
| 229 | const void *from, unsigned int len, unsigned int recsize) |
| 230 | { |
| 231 | if (kfifo_avail(fifo) < len + recsize) |
| 232 | return len + 1; |
| 233 | |
| 234 | __kfifo_in_data(fifo, from, len, recsize); |
| 235 | return 0; |
| 236 | } |
| 237 | EXPORT_SYMBOL(__kfifo_in_n); |
| 238 | |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 239 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 240 | * kfifo_in - puts some data into the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | * @fifo: the fifo to be used. |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 242 | * @from: the data to be added. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | * @len: the length of the data to be added. |
| 244 | * |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 245 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | * the FIFO depending on the free space, and returns the number of |
| 247 | * bytes copied. |
| 248 | * |
| 249 | * Note that with only one concurrent reader and one concurrent |
| 250 | * writer, you don't need extra locking to use these functions. |
| 251 | */ |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 252 | unsigned int kfifo_in(struct kfifo *fifo, const void *from, |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 253 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 255 | len = min(kfifo_avail(fifo), len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 257 | __kfifo_in_data(fifo, from, len, 0); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 258 | __kfifo_add_in(fifo, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return len; |
| 260 | } |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 261 | EXPORT_SYMBOL(kfifo_in); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 263 | unsigned int __kfifo_in_generic(struct kfifo *fifo, |
| 264 | const void *from, unsigned int len, unsigned int recsize) |
| 265 | { |
| 266 | return __kfifo_in_rec(fifo, from, len, recsize); |
| 267 | } |
| 268 | EXPORT_SYMBOL(__kfifo_in_generic); |
| 269 | |
| 270 | unsigned int __kfifo_out_n(struct kfifo *fifo, |
| 271 | void *to, unsigned int len, unsigned int recsize) |
| 272 | { |
| 273 | if (kfifo_len(fifo) < len + recsize) |
| 274 | return len; |
| 275 | |
| 276 | __kfifo_out_data(fifo, to, len, recsize); |
| 277 | __kfifo_add_out(fifo, len + recsize); |
| 278 | return 0; |
| 279 | } |
| 280 | EXPORT_SYMBOL(__kfifo_out_n); |
| 281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 283 | * kfifo_out - gets some data from the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | * @fifo: the fifo to be used. |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 285 | * @to: where the data must be copied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | * @len: the size of the destination buffer. |
| 287 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 288 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 289 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | * |
| 291 | * Note that with only one concurrent reader and one concurrent |
| 292 | * writer, you don't need extra locking to use these functions. |
| 293 | */ |
Andi Kleen | 8ecc295 | 2010-01-15 17:01:12 -0800 | [diff] [blame] | 294 | unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 296 | len = min(kfifo_len(fifo), len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 298 | __kfifo_out_data(fifo, to, len, 0); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 299 | __kfifo_add_out(fifo, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | return len; |
| 302 | } |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 303 | EXPORT_SYMBOL(kfifo_out); |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 304 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 305 | unsigned int __kfifo_out_generic(struct kfifo *fifo, |
| 306 | void *to, unsigned int len, unsigned int recsize, |
| 307 | unsigned int *total) |
| 308 | { |
| 309 | return __kfifo_out_rec(fifo, to, len, recsize, total); |
| 310 | } |
| 311 | EXPORT_SYMBOL(__kfifo_out_generic); |
| 312 | |
| 313 | unsigned int __kfifo_from_user_n(struct kfifo *fifo, |
| 314 | const void __user *from, unsigned int len, unsigned int recsize) |
| 315 | { |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 316 | unsigned total; |
| 317 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 318 | if (kfifo_avail(fifo) < len + recsize) |
| 319 | return len + 1; |
| 320 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 321 | __kfifo_from_user_data(fifo, from, len, recsize, &total); |
| 322 | return total; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 323 | } |
| 324 | EXPORT_SYMBOL(__kfifo_from_user_n); |
| 325 | |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 326 | /** |
| 327 | * kfifo_from_user - puts some data from user space into the FIFO |
| 328 | * @fifo: the fifo to be used. |
| 329 | * @from: pointer to the data to be added. |
| 330 | * @len: the length of the data to be added. |
| 331 | * |
| 332 | * This function copies at most @len bytes from the @from into the |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 333 | * FIFO depending and returns -EFAULT/0. |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 334 | * |
| 335 | * Note that with only one concurrent reader and one concurrent |
| 336 | * writer, you don't need extra locking to use these functions. |
| 337 | */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 338 | int kfifo_from_user(struct kfifo *fifo, |
| 339 | const void __user *from, unsigned int len, unsigned *total) |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 340 | { |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 341 | int ret; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 342 | len = min(kfifo_avail(fifo), len); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 343 | ret = __kfifo_from_user_data(fifo, from, len, 0, total); |
| 344 | if (ret) |
| 345 | return ret; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 346 | __kfifo_add_in(fifo, len); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 347 | return 0; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 348 | } |
| 349 | EXPORT_SYMBOL(kfifo_from_user); |
| 350 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 351 | unsigned int __kfifo_from_user_generic(struct kfifo *fifo, |
| 352 | const void __user *from, unsigned int len, unsigned int recsize) |
| 353 | { |
| 354 | return __kfifo_from_user_rec(fifo, from, len, recsize); |
| 355 | } |
| 356 | EXPORT_SYMBOL(__kfifo_from_user_generic); |
| 357 | |
| 358 | unsigned int __kfifo_to_user_n(struct kfifo *fifo, |
| 359 | void __user *to, unsigned int len, unsigned int reclen, |
| 360 | unsigned int recsize) |
| 361 | { |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 362 | unsigned int ret, total; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 363 | |
| 364 | if (kfifo_len(fifo) < reclen + recsize) |
| 365 | return len; |
| 366 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 367 | ret = __kfifo_to_user_data(fifo, to, reclen, recsize, &total); |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 368 | |
| 369 | if (likely(ret == 0)) |
| 370 | __kfifo_add_out(fifo, reclen + recsize); |
| 371 | |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 372 | return total; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 373 | } |
| 374 | EXPORT_SYMBOL(__kfifo_to_user_n); |
| 375 | |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 376 | /** |
| 377 | * kfifo_to_user - gets data from the FIFO and write it to user space |
| 378 | * @fifo: the fifo to be used. |
| 379 | * @to: where the data must be copied. |
| 380 | * @len: the size of the destination buffer. |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 381 | @ @lenout: pointer to output variable with copied data |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 382 | * |
| 383 | * This function copies at most @len bytes from the FIFO into the |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 384 | * @to buffer and 0 or -EFAULT. |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 385 | * |
| 386 | * Note that with only one concurrent reader and one concurrent |
| 387 | * writer, you don't need extra locking to use these functions. |
| 388 | */ |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 389 | int kfifo_to_user(struct kfifo *fifo, |
| 390 | void __user *to, unsigned int len, unsigned *lenout) |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 391 | { |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 392 | int ret; |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 393 | len = min(kfifo_len(fifo), len); |
Andi Kleen | 64ce103 | 2010-01-15 17:01:15 -0800 | [diff] [blame^] | 394 | ret = __kfifo_to_user_data(fifo, to, len, 0, lenout); |
| 395 | __kfifo_add_out(fifo, *lenout); |
| 396 | return ret; |
Stefani Seibold | a121f24 | 2009-12-21 14:37:31 -0800 | [diff] [blame] | 397 | } |
| 398 | EXPORT_SYMBOL(kfifo_to_user); |
| 399 | |
Stefani Seibold | 86d4880 | 2009-12-21 14:37:32 -0800 | [diff] [blame] | 400 | unsigned int __kfifo_to_user_generic(struct kfifo *fifo, |
| 401 | void __user *to, unsigned int len, unsigned int recsize, |
| 402 | unsigned int *total) |
| 403 | { |
| 404 | return __kfifo_to_user_rec(fifo, to, len, recsize, total); |
| 405 | } |
| 406 | EXPORT_SYMBOL(__kfifo_to_user_generic); |
| 407 | |
| 408 | unsigned int __kfifo_peek_generic(struct kfifo *fifo, unsigned int recsize) |
| 409 | { |
| 410 | if (recsize == 0) |
| 411 | return kfifo_avail(fifo); |
| 412 | |
| 413 | return __kfifo_peek_n(fifo, recsize); |
| 414 | } |
| 415 | EXPORT_SYMBOL(__kfifo_peek_generic); |
| 416 | |
| 417 | void __kfifo_skip_generic(struct kfifo *fifo, unsigned int recsize) |
| 418 | { |
| 419 | __kfifo_skip_rec(fifo, recsize); |
| 420 | } |
| 421 | EXPORT_SYMBOL(__kfifo_skip_generic); |
| 422 | |