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