kfifo.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/err.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/log2.h>
  28. #include <linux/uaccess.h>
  29. static void _kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  30. unsigned int size)
  31. {
  32. fifo->buffer = buffer;
  33. fifo->size = size;
  34. kfifo_reset(fifo);
  35. }
  36. /**
  37. * kfifo_init - initialize a FIFO using a preallocated buffer
  38. * @fifo: the fifo to assign the buffer
  39. * @buffer: the preallocated buffer to be used.
  40. * @size: the size of the internal buffer, this have to be a power of 2.
  41. *
  42. */
  43. void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size)
  44. {
  45. /* size must be a power of 2 */
  46. BUG_ON(!is_power_of_2(size));
  47. _kfifo_init(fifo, buffer, size);
  48. }
  49. EXPORT_SYMBOL(kfifo_init);
  50. /**
  51. * kfifo_alloc - allocates a new FIFO internal buffer
  52. * @fifo: the fifo to assign then new buffer
  53. * @size: the size of the buffer to be allocated, this have to be a power of 2.
  54. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  55. *
  56. * This function dynamically allocates a new fifo internal buffer
  57. *
  58. * The size will be rounded-up to a power of 2.
  59. * The buffer will be release with kfifo_free().
  60. * Return 0 if no error, otherwise the an error code
  61. */
  62. int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
  63. {
  64. unsigned char *buffer;
  65. /*
  66. * round up to the next power of 2, since our 'let the indices
  67. * wrap' technique works only in this case.
  68. */
  69. if (!is_power_of_2(size)) {
  70. BUG_ON(size > 0x80000000);
  71. size = roundup_pow_of_two(size);
  72. }
  73. buffer = kmalloc(size, gfp_mask);
  74. if (!buffer) {
  75. _kfifo_init(fifo, 0, 0);
  76. return -ENOMEM;
  77. }
  78. _kfifo_init(fifo, buffer, size);
  79. return 0;
  80. }
  81. EXPORT_SYMBOL(kfifo_alloc);
  82. /**
  83. * kfifo_free - frees the FIFO internal buffer
  84. * @fifo: the fifo to be freed.
  85. */
  86. void kfifo_free(struct kfifo *fifo)
  87. {
  88. kfree(fifo->buffer);
  89. }
  90. EXPORT_SYMBOL(kfifo_free);
  91. /**
  92. * kfifo_skip - skip output data
  93. * @fifo: the fifo to be used.
  94. * @len: number of bytes to skip
  95. */
  96. void kfifo_skip(struct kfifo *fifo, unsigned int len)
  97. {
  98. if (len < kfifo_len(fifo)) {
  99. __kfifo_add_out(fifo, len);
  100. return;
  101. }
  102. kfifo_reset_out(fifo);
  103. }
  104. EXPORT_SYMBOL(kfifo_skip);
  105. /**
  106. * kfifo_in - puts some data into the FIFO
  107. * @fifo: the fifo to be used.
  108. * @from: the data to be added.
  109. * @len: the length of the data to be added.
  110. *
  111. * This function copies at most @len bytes from the @from buffer into
  112. * the FIFO depending on the free space, and returns the number of
  113. * bytes copied.
  114. *
  115. * Note that with only one concurrent reader and one concurrent
  116. * writer, you don't need extra locking to use these functions.
  117. */
  118. unsigned int kfifo_in(struct kfifo *fifo,
  119. const unsigned char *from, unsigned int len)
  120. {
  121. unsigned int off;
  122. unsigned int l;
  123. len = min(len, fifo->size - fifo->in + fifo->out);
  124. /*
  125. * Ensure that we sample the fifo->out index -before- we
  126. * start putting bytes into the kfifo.
  127. */
  128. smp_mb();
  129. off = __kfifo_off(fifo, fifo->in);
  130. /* first put the data starting from fifo->in to buffer end */
  131. l = min(len, fifo->size - off);
  132. memcpy(fifo->buffer + off, from, l);
  133. /* then put the rest (if any) at the beginning of the buffer */
  134. memcpy(fifo->buffer, from + l, len - l);
  135. __kfifo_add_in(fifo, len);
  136. return len;
  137. }
  138. EXPORT_SYMBOL(kfifo_in);
  139. /**
  140. * kfifo_out - gets some data from the FIFO
  141. * @fifo: the fifo to be used.
  142. * @to: where the data must be copied.
  143. * @len: the size of the destination buffer.
  144. *
  145. * This function copies at most @len bytes from the FIFO into the
  146. * @to buffer and returns the number of copied bytes.
  147. *
  148. * Note that with only one concurrent reader and one concurrent
  149. * writer, you don't need extra locking to use these functions.
  150. */
  151. unsigned int kfifo_out(struct kfifo *fifo,
  152. unsigned char *to, unsigned int len)
  153. {
  154. unsigned int off;
  155. unsigned int l;
  156. len = min(len, fifo->in - fifo->out);
  157. /*
  158. * Ensure that we sample the fifo->in index -before- we
  159. * start removing bytes from the kfifo.
  160. */
  161. smp_rmb();
  162. off = __kfifo_off(fifo, fifo->out);
  163. /* first get the data from fifo->out until the end of the buffer */
  164. l = min(len, fifo->size - off);
  165. memcpy(to, fifo->buffer + off, l);
  166. /* then get the rest (if any) from the beginning of the buffer */
  167. memcpy(to + l, fifo->buffer, len - l);
  168. __kfifo_add_out(fifo, len);
  169. return len;
  170. }
  171. EXPORT_SYMBOL(kfifo_out);
  172. /**
  173. * kfifo_from_user - puts some data from user space into the FIFO
  174. * @fifo: the fifo to be used.
  175. * @from: pointer to the data to be added.
  176. * @len: the length of the data to be added.
  177. *
  178. * This function copies at most @len bytes from the @from into the
  179. * FIFO depending and returns the number of copied bytes.
  180. *
  181. * Note that with only one concurrent reader and one concurrent
  182. * writer, you don't need extra locking to use these functions.
  183. */
  184. unsigned int kfifo_from_user(struct kfifo *fifo,
  185. const void __user *from, unsigned int len)
  186. {
  187. unsigned int off;
  188. unsigned int l;
  189. int ret;
  190. len = min(len, fifo->size - fifo->in + fifo->out);
  191. /*
  192. * Ensure that we sample the fifo->out index -before- we
  193. * start putting bytes into the kfifo.
  194. */
  195. smp_mb();
  196. off = __kfifo_off(fifo, fifo->in);
  197. /* first put the data starting from fifo->in to buffer end */
  198. l = min(len, fifo->size - off);
  199. ret = copy_from_user(fifo->buffer + off, from, l);
  200. if (unlikely(ret))
  201. return l - ret;
  202. /* then put the rest (if any) at the beginning of the buffer */
  203. ret = copy_from_user(fifo->buffer, from + l, len - l);
  204. if (unlikely(ret))
  205. return len - ret;
  206. __kfifo_add_in(fifo, len);
  207. return len;
  208. }
  209. EXPORT_SYMBOL(kfifo_from_user);
  210. /**
  211. * kfifo_to_user - gets data from the FIFO and write it to user space
  212. * @fifo: the fifo to be used.
  213. * @to: where the data must be copied.
  214. * @len: the size of the destination buffer.
  215. *
  216. * This function copies at most @len bytes from the FIFO into the
  217. * @to buffer and returns the number of copied bytes.
  218. *
  219. * Note that with only one concurrent reader and one concurrent
  220. * writer, you don't need extra locking to use these functions.
  221. */
  222. unsigned int kfifo_to_user(struct kfifo *fifo,
  223. void __user *to, unsigned int len)
  224. {
  225. unsigned int off;
  226. unsigned int l;
  227. int ret;
  228. len = min(len, fifo->in - fifo->out);
  229. /*
  230. * Ensure that we sample the fifo->in index -before- we
  231. * start removing bytes from the kfifo.
  232. */
  233. smp_rmb();
  234. off = __kfifo_off(fifo, fifo->out);
  235. /* first get the data from fifo->out until the end of the buffer */
  236. l = min(len, fifo->size - off);
  237. ret = copy_to_user(to, fifo->buffer + off, l);
  238. if (unlikely(ret))
  239. return l - ret;
  240. /* then get the rest (if any) from the beginning of the buffer */
  241. ret = copy_to_user(to + l, fifo->buffer, len - l);
  242. if (unlikely(ret))
  243. return len - ret;
  244. __kfifo_add_out(fifo, len);
  245. return len;
  246. }
  247. EXPORT_SYMBOL(kfifo_to_user);