kfifo.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. static inline unsigned int __must_check
  154. __kfifo_uint_must_check_helper(unsigned int val)
  155. {
  156. return val;
  157. }
  158. static inline int __must_check
  159. __kfifo_int_must_check_helper(int val)
  160. {
  161. return val;
  162. }
  163. /**
  164. * kfifo_initialized - Check if the fifo is initialized
  165. * @fifo: address of the fifo to check
  166. *
  167. * Return %true if fifo is initialized, otherwise %false.
  168. * Assumes the fifo was 0 before.
  169. */
  170. #define kfifo_initialized(fifo) ((fifo)->kfifo.mask)
  171. /**
  172. * kfifo_esize - returns the size of the element managed by the fifo
  173. * @fifo: address of the fifo to be used
  174. */
  175. #define kfifo_esize(fifo) ((fifo)->kfifo.esize)
  176. /**
  177. * kfifo_recsize - returns the size of the record length field
  178. * @fifo: address of the fifo to be used
  179. */
  180. #define kfifo_recsize(fifo) (sizeof(*(fifo)->rectype))
  181. /**
  182. * kfifo_size - returns the size of the fifo in elements
  183. * @fifo: address of the fifo to be used
  184. */
  185. #define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
  186. /**
  187. * kfifo_reset - removes the entire fifo content
  188. * @fifo: address of the fifo to be used
  189. *
  190. * Note: usage of kfifo_reset() is dangerous. It should be only called when the
  191. * fifo is exclusived locked or when it is secured that no other thread is
  192. * accessing the fifo.
  193. */
  194. #define kfifo_reset(fifo) \
  195. (void)({ \
  196. typeof((fifo) + 1) __tmp = (fifo); \
  197. __tmp->kfifo.in = __tmp->kfifo.out = 0; \
  198. })
  199. /**
  200. * kfifo_reset_out - skip fifo content
  201. * @fifo: address of the fifo to be used
  202. *
  203. * Note: The usage of kfifo_reset_out() is safe until it will be only called
  204. * from the reader thread and there is only one concurrent reader. Otherwise
  205. * it is dangerous and must be handled in the same way as kfifo_reset().
  206. */
  207. #define kfifo_reset_out(fifo) \
  208. (void)({ \
  209. typeof((fifo) + 1) __tmp = (fifo); \
  210. __tmp->kfifo.out = __tmp->kfifo.in; \
  211. })
  212. /**
  213. * kfifo_len - returns the number of used elements in the fifo
  214. * @fifo: address of the fifo to be used
  215. */
  216. #define kfifo_len(fifo) \
  217. ({ \
  218. typeof((fifo) + 1) __tmpl = (fifo); \
  219. __tmpl->kfifo.in - __tmpl->kfifo.out; \
  220. })
  221. /**
  222. * kfifo_is_empty - returns true if the fifo is empty
  223. * @fifo: address of the fifo to be used
  224. */
  225. #define kfifo_is_empty(fifo) \
  226. ({ \
  227. typeof((fifo) + 1) __tmpq = (fifo); \
  228. __tmpq->kfifo.in == __tmpq->kfifo.out; \
  229. })
  230. /**
  231. * kfifo_is_full - returns true if the fifo is full
  232. * @fifo: address of the fifo to be used
  233. */
  234. #define kfifo_is_full(fifo) \
  235. ({ \
  236. typeof((fifo) + 1) __tmpq = (fifo); \
  237. kfifo_len(__tmpq) > __tmpq->kfifo.mask; \
  238. })
  239. /**
  240. * kfifo_avail - returns the number of unused elements in the fifo
  241. * @fifo: address of the fifo to be used
  242. */
  243. #define kfifo_avail(fifo) \
  244. __kfifo_uint_must_check_helper( \
  245. ({ \
  246. typeof((fifo) + 1) __tmpq = (fifo); \
  247. const size_t __recsize = sizeof(*__tmpq->rectype); \
  248. unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
  249. (__recsize) ? ((__avail <= __recsize) ? 0 : \
  250. __kfifo_max_r(__avail - __recsize, __recsize)) : \
  251. __avail; \
  252. }) \
  253. )
  254. /**
  255. * kfifo_skip - skip output data
  256. * @fifo: address of the fifo to be used
  257. */
  258. #define kfifo_skip(fifo) \
  259. (void)({ \
  260. typeof((fifo) + 1) __tmp = (fifo); \
  261. const size_t __recsize = sizeof(*__tmp->rectype); \
  262. struct __kfifo *__kfifo = &__tmp->kfifo; \
  263. if (__recsize) \
  264. __kfifo_skip_r(__kfifo, __recsize); \
  265. else \
  266. __kfifo->out++; \
  267. })
  268. /**
  269. * kfifo_peek_len - gets the size of the next fifo record
  270. * @fifo: address of the fifo to be used
  271. *
  272. * This function returns the size of the next fifo record in number of bytes.
  273. */
  274. #define kfifo_peek_len(fifo) \
  275. __kfifo_uint_must_check_helper( \
  276. ({ \
  277. typeof((fifo) + 1) __tmp = (fifo); \
  278. const size_t __recsize = sizeof(*__tmp->rectype); \
  279. struct __kfifo *__kfifo = &__tmp->kfifo; \
  280. (!__recsize) ? kfifo_len(__tmp) * sizeof(*__tmp->type) : \
  281. __kfifo_len_r(__kfifo, __recsize); \
  282. }) \
  283. )
  284. /**
  285. * kfifo_alloc - dynamically allocates a new fifo buffer
  286. * @fifo: pointer to the fifo
  287. * @size: the number of elements in the fifo, this must be a power of 2
  288. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  289. *
  290. * This macro dynamically allocates a new fifo buffer.
  291. *
  292. * The numer of elements will be rounded-up to a power of 2.
  293. * The fifo will be release with kfifo_free().
  294. * Return 0 if no error, otherwise an error code.
  295. */
  296. #define kfifo_alloc(fifo, size, gfp_mask) \
  297. __kfifo_int_must_check_helper( \
  298. ({ \
  299. typeof((fifo) + 1) __tmp = (fifo); \
  300. struct __kfifo *__kfifo = &__tmp->kfifo; \
  301. __is_kfifo_ptr(__tmp) ? \
  302. __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \
  303. -EINVAL; \
  304. }) \
  305. )
  306. /**
  307. * kfifo_free - frees the fifo
  308. * @fifo: the fifo to be freed
  309. */
  310. #define kfifo_free(fifo) \
  311. ({ \
  312. typeof((fifo) + 1) __tmp = (fifo); \
  313. struct __kfifo *__kfifo = &__tmp->kfifo; \
  314. if (__is_kfifo_ptr(__tmp)) \
  315. __kfifo_free(__kfifo); \
  316. })
  317. /**
  318. * kfifo_init - initialize a fifo using a preallocated buffer
  319. * @fifo: the fifo to assign the buffer
  320. * @buffer: the preallocated buffer to be used
  321. * @size: the size of the internal buffer, this have to be a power of 2
  322. *
  323. * This macro initialize a fifo using a preallocated buffer.
  324. *
  325. * The numer of elements will be rounded-up to a power of 2.
  326. * Return 0 if no error, otherwise an error code.
  327. */
  328. #define kfifo_init(fifo, buffer, size) \
  329. ({ \
  330. typeof((fifo) + 1) __tmp = (fifo); \
  331. struct __kfifo *__kfifo = &__tmp->kfifo; \
  332. __is_kfifo_ptr(__tmp) ? \
  333. __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
  334. -EINVAL; \
  335. })
  336. /**
  337. * kfifo_put - put data into the fifo
  338. * @fifo: address of the fifo to be used
  339. * @val: the data to be added
  340. *
  341. * This macro copies the given value into the fifo.
  342. * It returns 0 if the fifo was full. Otherwise it returns the number
  343. * processed elements.
  344. *
  345. * Note that with only one concurrent reader and one concurrent
  346. * writer, you don't need extra locking to use these macro.
  347. */
  348. #define kfifo_put(fifo, val) \
  349. ({ \
  350. typeof((fifo) + 1) __tmp = (fifo); \
  351. typeof((val) + 1) __val = (val); \
  352. unsigned int __ret; \
  353. const size_t __recsize = sizeof(*__tmp->rectype); \
  354. struct __kfifo *__kfifo = &__tmp->kfifo; \
  355. if (0) { \
  356. typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
  357. __dummy = (typeof(__val))NULL; \
  358. } \
  359. if (__recsize) \
  360. __ret = __kfifo_in_r(__kfifo, __val, sizeof(*__val), \
  361. __recsize); \
  362. else { \
  363. __ret = !kfifo_is_full(__tmp); \
  364. if (__ret) { \
  365. (__is_kfifo_ptr(__tmp) ? \
  366. ((typeof(__tmp->type))__kfifo->data) : \
  367. (__tmp->buf) \
  368. )[__kfifo->in & __tmp->kfifo.mask] = \
  369. *(typeof(__tmp->type))__val; \
  370. smp_wmb(); \
  371. __kfifo->in++; \
  372. } \
  373. } \
  374. __ret; \
  375. })
  376. /**
  377. * kfifo_get - get data from the fifo
  378. * @fifo: address of the fifo to be used
  379. * @val: the var where to store the data to be added
  380. *
  381. * This macro reads the data from the fifo.
  382. * It returns 0 if the fifo was empty. Otherwise it returns the number
  383. * processed elements.
  384. *
  385. * Note that with only one concurrent reader and one concurrent
  386. * writer, you don't need extra locking to use these macro.
  387. */
  388. #define kfifo_get(fifo, val) \
  389. __kfifo_uint_must_check_helper( \
  390. ({ \
  391. typeof((fifo) + 1) __tmp = (fifo); \
  392. typeof((val) + 1) __val = (val); \
  393. unsigned int __ret; \
  394. const size_t __recsize = sizeof(*__tmp->rectype); \
  395. struct __kfifo *__kfifo = &__tmp->kfifo; \
  396. if (0) \
  397. __val = (typeof(__tmp->ptr))0; \
  398. if (__recsize) \
  399. __ret = __kfifo_out_r(__kfifo, __val, sizeof(*__val), \
  400. __recsize); \
  401. else { \
  402. __ret = !kfifo_is_empty(__tmp); \
  403. if (__ret) { \
  404. *(typeof(__tmp->type))__val = \
  405. (__is_kfifo_ptr(__tmp) ? \
  406. ((typeof(__tmp->type))__kfifo->data) : \
  407. (__tmp->buf) \
  408. )[__kfifo->out & __tmp->kfifo.mask]; \
  409. smp_wmb(); \
  410. __kfifo->out++; \
  411. } \
  412. } \
  413. __ret; \
  414. }) \
  415. )
  416. /**
  417. * kfifo_peek - get data from the fifo without removing
  418. * @fifo: address of the fifo to be used
  419. * @val: the var where to store the data to be added
  420. *
  421. * This reads the data from the fifo without removing it from the fifo.
  422. * It returns 0 if the fifo was empty. Otherwise it returns the number
  423. * processed elements.
  424. *
  425. * Note that with only one concurrent reader and one concurrent
  426. * writer, you don't need extra locking to use these macro.
  427. */
  428. #define kfifo_peek(fifo, val) \
  429. __kfifo_uint_must_check_helper( \
  430. ({ \
  431. typeof((fifo) + 1) __tmp = (fifo); \
  432. typeof((val) + 1) __val = (val); \
  433. unsigned int __ret; \
  434. const size_t __recsize = sizeof(*__tmp->rectype); \
  435. struct __kfifo *__kfifo = &__tmp->kfifo; \
  436. if (0) \
  437. __val = (typeof(__tmp->ptr))NULL; \
  438. if (__recsize) \
  439. __ret = __kfifo_out_peek_r(__kfifo, __val, sizeof(*__val), \
  440. __recsize); \
  441. else { \
  442. __ret = !kfifo_is_empty(__tmp); \
  443. if (__ret) { \
  444. *(typeof(__tmp->type))__val = \
  445. (__is_kfifo_ptr(__tmp) ? \
  446. ((typeof(__tmp->type))__kfifo->data) : \
  447. (__tmp->buf) \
  448. )[__kfifo->out & __tmp->kfifo.mask]; \
  449. smp_wmb(); \
  450. } \
  451. } \
  452. __ret; \
  453. }) \
  454. )
  455. /**
  456. * kfifo_in - put data into the fifo
  457. * @fifo: address of the fifo to be used
  458. * @buf: the data to be added
  459. * @n: number of elements to be added
  460. *
  461. * This macro copies the given buffer into the fifo and returns the
  462. * number of copied elements.
  463. *
  464. * Note that with only one concurrent reader and one concurrent
  465. * writer, you don't need extra locking to use these macro.
  466. */
  467. #define kfifo_in(fifo, buf, n) \
  468. ({ \
  469. typeof((fifo) + 1) __tmp = (fifo); \
  470. typeof((buf) + 1) __buf = (buf); \
  471. unsigned long __n = (n); \
  472. const size_t __recsize = sizeof(*__tmp->rectype); \
  473. struct __kfifo *__kfifo = &__tmp->kfifo; \
  474. if (0) { \
  475. typeof(__tmp->ptr_const) __dummy __attribute__ ((unused)); \
  476. __dummy = (typeof(__buf))NULL; \
  477. } \
  478. (__recsize) ?\
  479. __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \
  480. __kfifo_in(__kfifo, __buf, __n); \
  481. })
  482. /**
  483. * kfifo_in_spinlocked - put data into the fifo using a spinlock for locking
  484. * @fifo: address of the fifo to be used
  485. * @buf: the data to be added
  486. * @n: number of elements to be added
  487. * @lock: pointer to the spinlock to use for locking
  488. *
  489. * This macro copies the given values buffer into the fifo and returns the
  490. * number of copied elements.
  491. */
  492. #define kfifo_in_spinlocked(fifo, buf, n, lock) \
  493. ({ \
  494. unsigned long __flags; \
  495. unsigned int __ret; \
  496. spin_lock_irqsave(lock, __flags); \
  497. __ret = kfifo_in(fifo, buf, n); \
  498. spin_unlock_irqrestore(lock, __flags); \
  499. __ret; \
  500. })
  501. /* alias for kfifo_in_spinlocked, will be removed in a future release */
  502. #define kfifo_in_locked(fifo, buf, n, lock) \
  503. kfifo_in_spinlocked(fifo, buf, n, lock)
  504. /**
  505. * kfifo_out - get data from the fifo
  506. * @fifo: address of the fifo to be used
  507. * @buf: pointer to the storage buffer
  508. * @n: max. number of elements to get
  509. *
  510. * This macro get some data from the fifo and return the numbers of elements
  511. * copied.
  512. *
  513. * Note that with only one concurrent reader and one concurrent
  514. * writer, you don't need extra locking to use these macro.
  515. */
  516. #define kfifo_out(fifo, buf, n) \
  517. __kfifo_uint_must_check_helper( \
  518. ({ \
  519. typeof((fifo) + 1) __tmp = (fifo); \
  520. typeof((buf) + 1) __buf = (buf); \
  521. unsigned long __n = (n); \
  522. const size_t __recsize = sizeof(*__tmp->rectype); \
  523. struct __kfifo *__kfifo = &__tmp->kfifo; \
  524. if (0) { \
  525. typeof(__tmp->ptr) __dummy = NULL; \
  526. __buf = __dummy; \
  527. } \
  528. (__recsize) ?\
  529. __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \
  530. __kfifo_out(__kfifo, __buf, __n); \
  531. }) \
  532. )
  533. /**
  534. * kfifo_out_spinlocked - get data from the fifo using a spinlock for locking
  535. * @fifo: address of the fifo to be used
  536. * @buf: pointer to the storage buffer
  537. * @n: max. number of elements to get
  538. * @lock: pointer to the spinlock to use for locking
  539. *
  540. * This macro get the data from the fifo and return the numbers of elements
  541. * copied.
  542. */
  543. #define kfifo_out_spinlocked(fifo, buf, n, lock) \
  544. __kfifo_uint_must_check_helper( \
  545. ({ \
  546. unsigned long __flags; \
  547. unsigned int __ret; \
  548. spin_lock_irqsave(lock, __flags); \
  549. __ret = kfifo_out(fifo, buf, n); \
  550. spin_unlock_irqrestore(lock, __flags); \
  551. __ret; \
  552. }) \
  553. )
  554. /* alias for kfifo_out_spinlocked, will be removed in a future release */
  555. #define kfifo_out_locked(fifo, buf, n, lock) \
  556. kfifo_out_spinlocked(fifo, buf, n, lock)
  557. /**
  558. * kfifo_from_user - puts some data from user space into the fifo
  559. * @fifo: address of the fifo to be used
  560. * @from: pointer to the data to be added
  561. * @len: the length of the data to be added
  562. * @copied: pointer to output variable to store the number of copied bytes
  563. *
  564. * This macro copies at most @len bytes from the @from into the
  565. * fifo, depending of the available space and returns -EFAULT/0.
  566. *
  567. * Note that with only one concurrent reader and one concurrent
  568. * writer, you don't need extra locking to use these macro.
  569. */
  570. #define kfifo_from_user(fifo, from, len, copied) \
  571. __kfifo_uint_must_check_helper( \
  572. ({ \
  573. typeof((fifo) + 1) __tmp = (fifo); \
  574. const void __user *__from = (from); \
  575. unsigned int __len = (len); \
  576. unsigned int *__copied = (copied); \
  577. const size_t __recsize = sizeof(*__tmp->rectype); \
  578. struct __kfifo *__kfifo = &__tmp->kfifo; \
  579. (__recsize) ? \
  580. __kfifo_from_user_r(__kfifo, __from, __len, __copied, __recsize) : \
  581. __kfifo_from_user(__kfifo, __from, __len, __copied); \
  582. }) \
  583. )
  584. /**
  585. * kfifo_to_user - copies data from the fifo into user space
  586. * @fifo: address of the fifo to be used
  587. * @to: where the data must be copied
  588. * @len: the size of the destination buffer
  589. * @copied: pointer to output variable to store the number of copied bytes
  590. *
  591. * This macro copies at most @len bytes from the fifo into the
  592. * @to buffer and returns -EFAULT/0.
  593. *
  594. * Note that with only one concurrent reader and one concurrent
  595. * writer, you don't need extra locking to use these macro.
  596. */
  597. #define kfifo_to_user(fifo, to, len, copied) \
  598. __kfifo_uint_must_check_helper( \
  599. ({ \
  600. typeof((fifo) + 1) __tmp = (fifo); \
  601. void __user *__to = (to); \
  602. unsigned int __len = (len); \
  603. unsigned int *__copied = (copied); \
  604. const size_t __recsize = sizeof(*__tmp->rectype); \
  605. struct __kfifo *__kfifo = &__tmp->kfifo; \
  606. (__recsize) ? \
  607. __kfifo_to_user_r(__kfifo, __to, __len, __copied, __recsize) : \
  608. __kfifo_to_user(__kfifo, __to, __len, __copied); \
  609. }) \
  610. )
  611. /**
  612. * kfifo_dma_in_prepare - setup a scatterlist for DMA input
  613. * @fifo: address of the fifo to be used
  614. * @sgl: pointer to the scatterlist array
  615. * @nents: number of entries in the scatterlist array
  616. * @len: number of elements to transfer
  617. *
  618. * This macro fills a scatterlist for DMA input.
  619. * It returns the number entries in the scatterlist array.
  620. *
  621. * Note that with only one concurrent reader and one concurrent
  622. * writer, you don't need extra locking to use these macros.
  623. */
  624. #define kfifo_dma_in_prepare(fifo, sgl, nents, len) \
  625. ({ \
  626. typeof((fifo) + 1) __tmp = (fifo); \
  627. struct scatterlist *__sgl = (sgl); \
  628. int __nents = (nents); \
  629. unsigned int __len = (len); \
  630. const size_t __recsize = sizeof(*__tmp->rectype); \
  631. struct __kfifo *__kfifo = &__tmp->kfifo; \
  632. (__recsize) ? \
  633. __kfifo_dma_in_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
  634. __kfifo_dma_in_prepare(__kfifo, __sgl, __nents, __len); \
  635. })
  636. /**
  637. * kfifo_dma_in_finish - finish a DMA IN operation
  638. * @fifo: address of the fifo to be used
  639. * @len: number of bytes to received
  640. *
  641. * This macro finish a DMA IN operation. The in counter will be updated by
  642. * the len parameter. No error checking will be done.
  643. *
  644. * Note that with only one concurrent reader and one concurrent
  645. * writer, you don't need extra locking to use these macros.
  646. */
  647. #define kfifo_dma_in_finish(fifo, len) \
  648. (void)({ \
  649. typeof((fifo) + 1) __tmp = (fifo); \
  650. unsigned int __len = (len); \
  651. const size_t __recsize = sizeof(*__tmp->rectype); \
  652. struct __kfifo *__kfifo = &__tmp->kfifo; \
  653. if (__recsize) \
  654. __kfifo_dma_in_finish_r(__kfifo, __len, __recsize); \
  655. else \
  656. __kfifo->in += __len / sizeof(*__tmp->type); \
  657. })
  658. /**
  659. * kfifo_dma_out_prepare - setup a scatterlist for DMA output
  660. * @fifo: address of the fifo to be used
  661. * @sgl: pointer to the scatterlist array
  662. * @nents: number of entries in the scatterlist array
  663. * @len: number of elements to transfer
  664. *
  665. * This macro fills a scatterlist for DMA output which at most @len bytes
  666. * to transfer.
  667. * It returns the number entries in the scatterlist array.
  668. * A zero means there is no space available and the scatterlist is not filled.
  669. *
  670. * Note that with only one concurrent reader and one concurrent
  671. * writer, you don't need extra locking to use these macros.
  672. */
  673. #define kfifo_dma_out_prepare(fifo, sgl, nents, len) \
  674. ({ \
  675. typeof((fifo) + 1) __tmp = (fifo); \
  676. struct scatterlist *__sgl = (sgl); \
  677. int __nents = (nents); \
  678. unsigned int __len = (len); \
  679. const size_t __recsize = sizeof(*__tmp->rectype); \
  680. struct __kfifo *__kfifo = &__tmp->kfifo; \
  681. (__recsize) ? \
  682. __kfifo_dma_out_prepare_r(__kfifo, __sgl, __nents, __len, __recsize) : \
  683. __kfifo_dma_out_prepare(__kfifo, __sgl, __nents, __len); \
  684. })
  685. /**
  686. * kfifo_dma_out_finish - finish a DMA OUT operation
  687. * @fifo: address of the fifo to be used
  688. * @len: number of bytes transferd
  689. *
  690. * This macro finish a DMA OUT operation. The out counter will be updated by
  691. * the len parameter. No error checking will be done.
  692. *
  693. * Note that with only one concurrent reader and one concurrent
  694. * writer, you don't need extra locking to use these macros.
  695. */
  696. #define kfifo_dma_out_finish(fifo, len) \
  697. (void)({ \
  698. typeof((fifo) + 1) __tmp = (fifo); \
  699. unsigned int __len = (len); \
  700. const size_t __recsize = sizeof(*__tmp->rectype); \
  701. struct __kfifo *__kfifo = &__tmp->kfifo; \
  702. if (__recsize) \
  703. __kfifo_dma_out_finish_r(__kfifo, __recsize); \
  704. else \
  705. __kfifo->out += __len / sizeof(*__tmp->type); \
  706. })
  707. /**
  708. * kfifo_out_peek - gets some data from the fifo
  709. * @fifo: address of the fifo to be used
  710. * @buf: pointer to the storage buffer
  711. * @n: max. number of elements to get
  712. *
  713. * This macro get the data from the fifo and return the numbers of elements
  714. * copied. The data is not removed from the fifo.
  715. *
  716. * Note that with only one concurrent reader and one concurrent
  717. * writer, you don't need extra locking to use these macro.
  718. */
  719. #define kfifo_out_peek(fifo, buf, n) \
  720. __kfifo_uint_must_check_helper( \
  721. ({ \
  722. typeof((fifo) + 1) __tmp = (fifo); \
  723. typeof((buf) + 1) __buf = (buf); \
  724. unsigned long __n = (n); \
  725. const size_t __recsize = sizeof(*__tmp->rectype); \
  726. struct __kfifo *__kfifo = &__tmp->kfifo; \
  727. if (0) { \
  728. typeof(__tmp->ptr) __dummy __attribute__ ((unused)) = NULL; \
  729. __buf = __dummy; \
  730. } \
  731. (__recsize) ? \
  732. __kfifo_out_peek_r(__kfifo, __buf, __n, __recsize) : \
  733. __kfifo_out_peek(__kfifo, __buf, __n); \
  734. }) \
  735. )
  736. extern int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,
  737. size_t esize, gfp_t gfp_mask);
  738. extern void __kfifo_free(struct __kfifo *fifo);
  739. extern int __kfifo_init(struct __kfifo *fifo, void *buffer,
  740. unsigned int size, size_t esize);
  741. extern unsigned int __kfifo_in(struct __kfifo *fifo,
  742. const void *buf, unsigned int len);
  743. extern unsigned int __kfifo_out(struct __kfifo *fifo,
  744. void *buf, unsigned int len);
  745. extern int __kfifo_from_user(struct __kfifo *fifo,
  746. const void __user *from, unsigned long len, unsigned int *copied);
  747. extern int __kfifo_to_user(struct __kfifo *fifo,
  748. void __user *to, unsigned long len, unsigned int *copied);
  749. extern unsigned int __kfifo_dma_in_prepare(struct __kfifo *fifo,
  750. struct scatterlist *sgl, int nents, unsigned int len);
  751. extern unsigned int __kfifo_dma_out_prepare(struct __kfifo *fifo,
  752. struct scatterlist *sgl, int nents, unsigned int len);
  753. extern unsigned int __kfifo_out_peek(struct __kfifo *fifo,
  754. void *buf, unsigned int len);
  755. extern unsigned int __kfifo_in_r(struct __kfifo *fifo,
  756. const void *buf, unsigned int len, size_t recsize);
  757. extern unsigned int __kfifo_out_r(struct __kfifo *fifo,
  758. void *buf, unsigned int len, size_t recsize);
  759. extern int __kfifo_from_user_r(struct __kfifo *fifo,
  760. const void __user *from, unsigned long len, unsigned int *copied,
  761. size_t recsize);
  762. extern int __kfifo_to_user_r(struct __kfifo *fifo, void __user *to,
  763. unsigned long len, unsigned int *copied, size_t recsize);
  764. extern unsigned int __kfifo_dma_in_prepare_r(struct __kfifo *fifo,
  765. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
  766. extern void __kfifo_dma_in_finish_r(struct __kfifo *fifo,
  767. unsigned int len, size_t recsize);
  768. extern unsigned int __kfifo_dma_out_prepare_r(struct __kfifo *fifo,
  769. struct scatterlist *sgl, int nents, unsigned int len, size_t recsize);
  770. extern void __kfifo_dma_out_finish_r(struct __kfifo *fifo, size_t recsize);
  771. extern unsigned int __kfifo_len_r(struct __kfifo *fifo, size_t recsize);
  772. extern void __kfifo_skip_r(struct __kfifo *fifo, size_t recsize);
  773. extern unsigned int __kfifo_out_peek_r(struct __kfifo *fifo,
  774. void *buf, unsigned int len, size_t recsize);
  775. extern unsigned int __kfifo_max_r(unsigned int len, size_t recsize);
  776. #endif