kfifo.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /*
  50. * Macros for declaration and initialization of the kfifo datatype
  51. */
  52. /* helper macro */
  53. #define __kfifo_initializer(s, b) \
  54. (struct kfifo) { \
  55. .size = s, \
  56. .in = 0, \
  57. .out = 0, \
  58. .buffer = b \
  59. }
  60. /**
  61. * DECLARE_KFIFO - macro to declare a kfifo and the associated buffer
  62. * @name: name of the declared kfifo datatype
  63. * @size: size of the fifo buffer
  64. *
  65. * Note: the macro can be used inside struct or union declaration
  66. * Note: the macro creates two objects:
  67. * A kfifo object with the given name and a buffer for the kfifo
  68. * object named name##kfifo_buffer
  69. */
  70. #define DECLARE_KFIFO(name, size) \
  71. union { \
  72. struct kfifo name; \
  73. unsigned char name##kfifo_buffer[size + sizeof(struct kfifo)]; \
  74. }
  75. /**
  76. * INIT_KFIFO - Initialize a kfifo declared by DECLARED_KFIFO
  77. * @name: name of the declared kfifo datatype
  78. * @size: size of the fifo buffer
  79. */
  80. #define INIT_KFIFO(name) \
  81. name = __kfifo_initializer(sizeof(name##kfifo_buffer) - \
  82. sizeof(struct kfifo), name##kfifo_buffer)
  83. /**
  84. * DEFINE_KFIFO - macro to define and initialize a kfifo
  85. * @name: name of the declared kfifo datatype
  86. * @size: size of the fifo buffer
  87. *
  88. * Note: the macro can be used for global and local kfifo data type variables
  89. * Note: the macro creates two objects:
  90. * A kfifo object with the given name and a buffer for the kfifo
  91. * object named name##kfifo_buffer
  92. */
  93. #define DEFINE_KFIFO(name, size) \
  94. unsigned char name##kfifo_buffer[size]; \
  95. struct kfifo name = __kfifo_initializer(size, name##kfifo_buffer)
  96. #undef __kfifo_initializer
  97. extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  98. unsigned int size);
  99. extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
  100. gfp_t gfp_mask);
  101. extern void kfifo_free(struct kfifo *fifo);
  102. extern unsigned int kfifo_in(struct kfifo *fifo,
  103. const unsigned char *from, unsigned int len);
  104. extern __must_check unsigned int kfifo_out(struct kfifo *fifo,
  105. unsigned char *to, unsigned int len);
  106. /**
  107. * kfifo_reset - removes the entire FIFO contents
  108. * @fifo: the fifo to be emptied.
  109. */
  110. static inline void kfifo_reset(struct kfifo *fifo)
  111. {
  112. fifo->in = fifo->out = 0;
  113. }
  114. /**
  115. * kfifo_size - returns the size of the fifo in bytes
  116. * @fifo: the fifo to be used.
  117. */
  118. static inline __must_check unsigned int kfifo_size(struct kfifo *fifo)
  119. {
  120. return fifo->size;
  121. }
  122. /**
  123. * kfifo_len - returns the number of used bytes in the FIFO
  124. * @fifo: the fifo to be used.
  125. */
  126. static inline unsigned int kfifo_len(struct kfifo *fifo)
  127. {
  128. register unsigned int out;
  129. out = fifo->out;
  130. smp_rmb();
  131. return fifo->in - out;
  132. }
  133. /**
  134. * kfifo_is_empty - returns true if the fifo is empty
  135. * @fifo: the fifo to be used.
  136. */
  137. static inline __must_check int kfifo_is_empty(struct kfifo *fifo)
  138. {
  139. return fifo->in == fifo->out;
  140. }
  141. /**
  142. * kfifo_is_full - returns true if the fifo is full
  143. * @fifo: the fifo to be used.
  144. */
  145. static inline __must_check int kfifo_is_full(struct kfifo *fifo)
  146. {
  147. return kfifo_len(fifo) == kfifo_size(fifo);
  148. }
  149. /**
  150. * kfifo_avail - returns the number of bytes available in the FIFO
  151. * @fifo: the fifo to be used.
  152. */
  153. static inline __must_check unsigned int kfifo_avail(struct kfifo *fifo)
  154. {
  155. return kfifo_size(fifo) - kfifo_len(fifo);
  156. }
  157. /**
  158. * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking
  159. * @fifo: the fifo to be used.
  160. * @from: the data to be added.
  161. * @n: the length of the data to be added.
  162. * @lock: pointer to the spinlock to use for locking.
  163. *
  164. * This function copies at most @len bytes from the @from buffer into
  165. * the FIFO depending on the free space, and returns the number of
  166. * bytes copied.
  167. */
  168. static inline unsigned int kfifo_in_locked(struct kfifo *fifo,
  169. const unsigned char *from, unsigned int n, spinlock_t *lock)
  170. {
  171. unsigned long flags;
  172. unsigned int ret;
  173. spin_lock_irqsave(lock, flags);
  174. ret = kfifo_in(fifo, from, n);
  175. spin_unlock_irqrestore(lock, flags);
  176. return ret;
  177. }
  178. /**
  179. * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking
  180. * @fifo: the fifo to be used.
  181. * @to: where the data must be copied.
  182. * @n: the size of the destination buffer.
  183. * @lock: pointer to the spinlock to use for locking.
  184. *
  185. * This function copies at most @len bytes from the FIFO into the
  186. * @to buffer and returns the number of copied bytes.
  187. */
  188. static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo,
  189. unsigned char *to, unsigned int n, spinlock_t *lock)
  190. {
  191. unsigned long flags;
  192. unsigned int ret;
  193. spin_lock_irqsave(lock, flags);
  194. ret = kfifo_out(fifo, to, n);
  195. /*
  196. * optimization: if the FIFO is empty, set the indices to 0
  197. * so we don't wrap the next time
  198. */
  199. if (kfifo_is_empty(fifo))
  200. kfifo_reset(fifo);
  201. spin_unlock_irqrestore(lock, flags);
  202. return ret;
  203. }
  204. #endif