blob: e0f5c9d4197d47a65ae6d7def2b199aff95f94f1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Stefani Seibold45465482009-12-21 14:37:26 -08002 * A generic kernel FIFO implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Stefani Seibold45465482009-12-21 14:37:26 -08004 * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/spinlock.h>
27
28struct 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 Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Stefani Seibold45465482009-12-21 14:37:26 -080035extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080036 unsigned int size);
Stefani Seibold45465482009-12-21 14:37:26 -080037extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080038 gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039extern void kfifo_free(struct kfifo *fifo);
40extern unsigned int __kfifo_put(struct kfifo *fifo,
Alan Coxf8a7c1a2009-09-19 13:13:17 -070041 const unsigned char *buffer, unsigned int len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042extern 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 */
49static 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 */
58static inline void kfifo_reset(struct kfifo *fifo)
59{
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 __kfifo_reset(fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
63/**
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080064 * __kfifo_len - returns the number of bytes available in the FIFO
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080066 */
67static 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 Torvalds1da177e2005-04-16 15:20:36 -070082 *
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080083 * This function copies at most @len bytes from the @from buffer into
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * the FIFO depending on the free space, and returns the number of
85 * bytes copied.
86 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080087static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo,
88 const unsigned char *from, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 unsigned long flags;
91 unsigned int ret;
92
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080093 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080095 ret = __kfifo_put(fifo, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080097 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 return ret;
100}
101
102/**
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800103 * kfifo_get_locked - gets some data from the FIFO using a spinlock for locking
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800105 * @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 Torvalds1da177e2005-04-16 15:20:36 -0700108 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800109 * This function copies at most @len bytes from the FIFO into the
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800110 * @to buffer and returns the number of copied bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800112static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo,
113 unsigned char *to, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 unsigned long flags;
116 unsigned int ret;
117
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800118 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800120 ret = __kfifo_get(fifo, to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
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 Seiboldc1e13f22009-12-21 14:37:27 -0800129 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 return ret;
132}
133
134/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * kfifo_len - returns the number of bytes available in the FIFO
136 * @fifo: the fifo to be used.
137 */
138static inline unsigned int kfifo_len(struct kfifo *fifo)
139{
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800140 return __kfifo_len(fifo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143#endif