kfifo.h 25 KB

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