blob: a893acda3964429447c79442a1af5e3e0447cb2f [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);
Stefani Seibolde64c0262009-12-21 14:37:28 -080040extern unsigned int kfifo_put(struct kfifo *fifo,
Alan Coxf8a7c1a2009-09-19 13:13:17 -070041 const unsigned char *buffer, unsigned int len);
Stefani Seibolde64c0262009-12-21 14:37:28 -080042extern unsigned int kfifo_get(struct kfifo *fifo,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned char *buffer, unsigned int len);
44
45/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * kfifo_reset - removes the entire FIFO contents
47 * @fifo: the fifo to be emptied.
48 */
49static inline void kfifo_reset(struct kfifo *fifo)
50{
Stefani Seibolde64c0262009-12-21 14:37:28 -080051 fifo->in = fifo->out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
54/**
Stefani Seibolde64c0262009-12-21 14:37:28 -080055 * kfifo_len - returns the number of used bytes in the FIFO
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080057 */
Stefani Seibolde64c0262009-12-21 14:37:28 -080058static inline unsigned int kfifo_len(struct kfifo *fifo)
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080059{
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 Torvalds1da177e2005-04-16 15:20:36 -070073 *
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080074 * This function copies at most @len bytes from the @from buffer into
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * the FIFO depending on the free space, and returns the number of
76 * bytes copied.
77 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080078static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo,
79 const unsigned char *from, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 unsigned long flags;
82 unsigned int ret;
83
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080084 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Stefani Seibolde64c0262009-12-21 14:37:28 -080086 ret = kfifo_put(fifo, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080088 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 return ret;
91}
92
93/**
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080094 * kfifo_get_locked - gets some data from the FIFO using a spinlock for locking
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * @fifo: the fifo to be used.
Stefani Seiboldc1e13f22009-12-21 14:37:27 -080096 * @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 Torvalds1da177e2005-04-16 15:20:36 -070099 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800100 * This function copies at most @len bytes from the FIFO into the
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800101 * @to buffer and returns the number of copied bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800103static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo,
104 unsigned char *to, unsigned int n, spinlock_t *lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 unsigned long flags;
107 unsigned int ret;
108
Stefani Seiboldc1e13f22009-12-21 14:37:27 -0800109 spin_lock_irqsave(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Stefani Seibolde64c0262009-12-21 14:37:28 -0800111 ret = kfifo_get(fifo, to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
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 Seiboldc1e13f22009-12-21 14:37:27 -0800120 spin_unlock_irqrestore(lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 return ret;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125#endif