kfifo-new.h 25 KB

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