Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 2 | * A generic kernel FIFO implementation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 4 | * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Copyright (C) 2004 Stelian Pop <stelian@popies.net> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | */ |
| 22 | #ifndef _LINUX_KFIFO_H |
| 23 | #define _LINUX_KFIFO_H |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/spinlock.h> |
| 27 | |
| 28 | struct kfifo { |
| 29 | unsigned char *buffer; /* the buffer holding the data */ |
| 30 | unsigned int size; /* the size of the allocated buffer */ |
| 31 | unsigned int in; /* data is added at offset (in % size) */ |
| 32 | unsigned int out; /* data is extracted from off. (out % size) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 35 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 36 | unsigned int size); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 37 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 38 | gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | extern void kfifo_free(struct kfifo *fifo); |
| 40 | extern unsigned int __kfifo_put(struct kfifo *fifo, |
Alan Cox | f8a7c1a | 2009-09-19 13:13:17 -0700 | [diff] [blame] | 41 | const unsigned char *buffer, unsigned int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | extern unsigned int __kfifo_get(struct kfifo *fifo, |
| 43 | unsigned char *buffer, unsigned int len); |
| 44 | |
| 45 | /** |
| 46 | * __kfifo_reset - removes the entire FIFO contents, no locking version |
| 47 | * @fifo: the fifo to be emptied. |
| 48 | */ |
| 49 | static inline void __kfifo_reset(struct kfifo *fifo) |
| 50 | { |
| 51 | fifo->in = fifo->out = 0; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * kfifo_reset - removes the entire FIFO contents |
| 56 | * @fifo: the fifo to be emptied. |
| 57 | */ |
| 58 | static inline void kfifo_reset(struct kfifo *fifo) |
| 59 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | __kfifo_reset(fifo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 64 | * __kfifo_len - returns the number of bytes available in the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 66 | */ |
| 67 | static inline unsigned int __kfifo_len(struct kfifo *fifo) |
| 68 | { |
| 69 | register unsigned int out; |
| 70 | |
| 71 | out = fifo->out; |
| 72 | smp_rmb(); |
| 73 | return fifo->in - out; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * kfifo_put_locked - puts some data into the FIFO using a spinlock for locking |
| 78 | * @fifo: the fifo to be used. |
| 79 | * @from: the data to be added. |
| 80 | * @n: the length of the data to be added. |
| 81 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | * |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 83 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | * the FIFO depending on the free space, and returns the number of |
| 85 | * bytes copied. |
| 86 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 87 | static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo, |
| 88 | const unsigned char *from, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | unsigned long flags; |
| 91 | unsigned int ret; |
| 92 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 93 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 95 | ret = __kfifo_put(fifo, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 97 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /** |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 103 | * kfifo_get_locked - gets some data from the FIFO using a spinlock for locking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 105 | * @to: where the data must be copied. |
| 106 | * @n: the size of the destination buffer. |
| 107 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 109 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 110 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 112 | static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo, |
| 113 | unsigned char *to, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | unsigned long flags; |
| 116 | unsigned int ret; |
| 117 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 118 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 120 | ret = __kfifo_get(fifo, to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | /* |
| 123 | * optimization: if the FIFO is empty, set the indices to 0 |
| 124 | * so we don't wrap the next time |
| 125 | */ |
| 126 | if (fifo->in == fifo->out) |
| 127 | fifo->in = fifo->out = 0; |
| 128 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 129 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | * kfifo_len - returns the number of bytes available in the FIFO |
| 136 | * @fifo: the fifo to be used. |
| 137 | */ |
| 138 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
| 139 | { |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame^] | 140 | return __kfifo_len(fifo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | #endif |