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 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Howto 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_locked and kfifo_get |
| 34 | * into kfifo_out_locked |
| 35 | * Note: the spinlock pointer formerly passed to kfifo_init/kfifo_alloc |
| 36 | * must be passed now to the kfifo_in_locked and kfifo_out_locked |
| 37 | * as the last parameter. |
| 38 | * - All formerly name __kfifo_* functions has been renamed into kfifo_* |
| 39 | */ |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #ifndef _LINUX_KFIFO_H |
| 42 | #define _LINUX_KFIFO_H |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <linux/kernel.h> |
| 45 | #include <linux/spinlock.h> |
| 46 | |
| 47 | struct kfifo { |
| 48 | unsigned char *buffer; /* the buffer holding the data */ |
| 49 | unsigned int size; /* the size of the allocated buffer */ |
| 50 | unsigned int in; /* data is added at offset (in % size) */ |
| 51 | unsigned int out; /* data is extracted from off. (out % size) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | }; |
| 53 | |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 54 | extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 55 | unsigned int size); |
Stefani Seibold | 4546548 | 2009-12-21 14:37:26 -0800 | [diff] [blame] | 56 | extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 57 | gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | extern void kfifo_free(struct kfifo *fifo); |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame^] | 59 | extern unsigned int kfifo_in(struct kfifo *fifo, |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 60 | const unsigned char *from, unsigned int len); |
| 61 | extern __must_check unsigned int kfifo_out(struct kfifo *fifo, |
| 62 | unsigned char *to, unsigned int len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | * kfifo_reset - removes the entire FIFO contents |
| 66 | * @fifo: the fifo to be emptied. |
| 67 | */ |
| 68 | static inline void kfifo_reset(struct kfifo *fifo) |
| 69 | { |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 70 | fifo->in = fifo->out = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 74 | * kfifo_len - returns the number of used bytes in the FIFO |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 76 | */ |
Stefani Seibold | e64c026 | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 77 | static inline unsigned int kfifo_len(struct kfifo *fifo) |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 78 | { |
| 79 | register unsigned int out; |
| 80 | |
| 81 | out = fifo->out; |
| 82 | smp_rmb(); |
| 83 | return fifo->in - out; |
| 84 | } |
| 85 | |
| 86 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 87 | * kfifo_in_locked - puts some data into the FIFO using a spinlock for locking |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 88 | * @fifo: the fifo to be used. |
| 89 | * @from: the data to be added. |
| 90 | * @n: the length of the data to be added. |
| 91 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 93 | * This function copies at most @len bytes from the @from buffer into |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | * the FIFO depending on the free space, and returns the number of |
| 95 | * bytes copied. |
| 96 | */ |
Stefani Seibold | 9842c38 | 2009-12-21 14:37:29 -0800 | [diff] [blame^] | 97 | static inline unsigned int kfifo_in_locked(struct kfifo *fifo, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 98 | const unsigned char *from, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { |
| 100 | unsigned long flags; |
| 101 | unsigned int ret; |
| 102 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 103 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 105 | ret = kfifo_in(fifo, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 107 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | /** |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 113 | * kfifo_out_locked - gets some data from the FIFO using a spinlock for locking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * @fifo: the fifo to be used. |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 115 | * @to: where the data must be copied. |
| 116 | * @n: the size of the destination buffer. |
| 117 | * @lock: pointer to the spinlock to use for locking. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | * |
Robert P. J. Day | 72fd4a3 | 2007-02-10 01:45:59 -0800 | [diff] [blame] | 119 | * This function copies at most @len bytes from the FIFO into the |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 120 | * @to buffer and returns the number of copied bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | */ |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 122 | static inline __must_check unsigned int kfifo_out_locked(struct kfifo *fifo, |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 123 | unsigned char *to, unsigned int n, spinlock_t *lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned long flags; |
| 126 | unsigned int ret; |
| 127 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 128 | spin_lock_irqsave(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Stefani Seibold | 7acd72e | 2009-12-21 14:37:28 -0800 | [diff] [blame] | 130 | ret = kfifo_out(fifo, to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | * optimization: if the FIFO is empty, set the indices to 0 |
| 134 | * so we don't wrap the next time |
| 135 | */ |
| 136 | if (fifo->in == fifo->out) |
| 137 | fifo->in = fifo->out = 0; |
| 138 | |
Stefani Seibold | c1e13f2 | 2009-12-21 14:37:27 -0800 | [diff] [blame] | 139 | spin_unlock_irqrestore(lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | #endif |