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); |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 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); |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 42 | extern unsigned int kfifo_get(struct kfifo *fifo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | unsigned char *buffer, unsigned int len); |
| 44 | |
| 45 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | * kfifo_reset - removes the entire FIFO contents |
| 47 | * @fifo: the fifo to be emptied. |
| 48 | */ |
| 49 | static inline void kfifo_reset(struct kfifo *fifo) |
| 50 | { |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 51 | fifo->in = fifo->out = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 55 | * kfifo_len - returns the number of used bytes in the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 57 | */ |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 58 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 59 | { |
| 60 | register unsigned int out; |
| 61 | |
| 62 | out = fifo->out; |
| 63 | smp_rmb(); |
| 64 | return fifo->in - out; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * kfifo_put_locked - puts some data into the FIFO using a spinlock for locking |
| 69 | * @fifo: the fifo to be used. |
| 70 | * @from: the data to be added. |
| 71 | * @n: the length of the data to be added. |
| 72 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 74 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * the FIFO depending on the free space, and returns the number of |
| 76 | * bytes copied. |
| 77 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 78 | static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo, |
| 79 | const unsigned char *from, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
| 81 | unsigned long flags; |
| 82 | unsigned int ret; |
| 83 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 84 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 86 | ret = kfifo_put(fifo, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 88 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | return ret; |
| 91 | } |
| 92 | |
| 93 | /** |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 94 | * 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] | 95 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 96 | * @to: where the data must be copied. |
| 97 | * @n: the size of the destination buffer. |
| 98 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 100 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 101 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | */ |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 103 | static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo, |
| 104 | unsigned char *to, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | unsigned long flags; |
| 107 | unsigned int ret; |
| 108 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 109 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame^] | 111 | ret = kfifo_get(fifo, to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * optimization: if the FIFO is empty, set the indices to 0 |
| 115 | * so we don't wrap the next time |
| 116 | */ |
| 117 | if (fifo->in == fifo->out) |
| 118 | fifo->in = fifo->out = 0; |
| 119 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 120 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | #endif |