kfifo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * A generic kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
  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. * Howto porting drivers to the new generic fifo API:
  24. *
  25. * - Modify the declaration of the "struct kfifo *" object into a
  26. * in-place "struct kfifo" object
  27. * - Init the in-place object with kfifo_alloc() or kfifo_init()
  28. * Note: The address of the in-place "struct kfifo" object must be
  29. * passed as the first argument to this functions
  30. * - Replace the use of __kfifo_put into kfifo_in and __kfifo_get
  31. * into kfifo_out
  32. * - Replace the use of kfifo_put into kfifo_in_locked and kfifo_get
  33. * into kfifo_out_locked
  34. * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc
  35. * must be passed now to the kfifo_in_locked and kfifo_out_locked
  36. * as the last parameter.
  37. * - All formerly name __kfifo_* functions has been renamed into kfifo_*
  38. */
  39. #ifndef _LINUX_KFIFO_H
  40. #define _LINUX_KFIFO_H
  41. #include <linux/kernel.h>
  42. #include <linux/spinlock.h>
  43. struct kfifo {
  44. unsigned char *buffer; /* the buffer holding the data */
  45. unsigned int size; /* the size of the allocated buffer */
  46. unsigned int in; /* data is added at offset (in % size) */
  47. unsigned int out; /* data is extracted from off. (out % size) */
  48. };
  49. extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  50. unsigned int size);
  51. extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
  52. gfp_t gfp_mask);
  53. extern void kfifo_free(struct kfifo *fifo);
  54. extern unsigned int kfifo_in(struct kfifo *fifo,
  55. const unsigned char *from, unsigned int len);
  56. extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
  57. unsigned char *to, unsigned int len);
  58. /**
  59. * kfifo_reset - removes the entire FIFO contents
  60. * @fifo: the fifo to be emptied.
  61. */
  62. static inline void kfifo_reset(struct kfifo *fifo)
  63. {
  64. fifo->in = fifo->out = 0;
  65. }
  66. /**
  67. * kfifo_len - returns the number of used bytes in the FIFO
  68. * @fifo: the fifo to be used.
  69. */
  70. static inline unsigned int kfifo_len(struct kfifo *fifo)
  71. {
  72. register unsigned int out;
  73. out = fifo->out;
  74. smp_rmb();
  75. return fifo->in - out;
  76. }
  77. /**
  78. * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
  79. * @fifo: the fifo to be used.
  80. * @from: the data to be added.
  81. * @n: the length of the data to be added.
  82. * @lock: pointer to the spinlock to use for locking.
  83. *
  84. * This function copies at most @len bytes from the @from buffer into
  85. * the FIFO depending on the free space, and returns the number of
  86. * bytes copied.
  87. */
  88. static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
  89. const unsigned char *from, unsigned int n, spinlock_t *lock)
  90. {
  91. unsigned long flags;
  92. unsigned int ret;
  93. spin_lock_irqsave(lock, flags);
  94. ret = kfifo_in(fifo, from, n);
  95. spin_unlock_irqrestore(lock, flags);
  96. return ret;
  97. }
  98. /**
  99. * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
  100. * @fifo: the fifo to be used.
  101. * @to: where the data must be copied.
  102. * @n: the size of the destination buffer.
  103. * @lock: pointer to the spinlock to use for locking.
  104. *
  105. * This function copies at most @len bytes from the FIFO into the
  106. * @to buffer and returns the number of copied bytes.
  107. */
  108. static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
  109. unsigned char *to, unsigned int n, spinlock_t *lock)
  110. {
  111. unsigned long flags;
  112. unsigned int ret;
  113. spin_lock_irqsave(lock, flags);
  114. ret = kfifo_out(fifo, to, n);
  115. /*
  116. * optimization: if the FIFO is empty, set the indices to 0
  117. * so we don't wrap the next time
  118. */
  119. if (fifo->in == fifo->out)
  120. fifo->in = fifo->out = 0;
  121. spin_unlock_irqrestore(lock, flags);
  122. return ret;
  123. }
  124. #endif