3 * dvb_ringbuffer.h: ring buffer implementation for the dvb driver
5 * Copyright (C) 2003 Oliver Endriss
6 * Copyright (C) 2004 Andrew de Quincey
8 * based on code originally found in av7110.c & dvb_ci.c:
9 * Copyright (C) 1999-2003 Ralph Metzler & Marcus Metzler
10 * for convergence integrated media GmbH
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
23 #ifndef _DVB_RINGBUFFER_H_
24 #define _DVB_RINGBUFFER_H_
26 #include <linux/spinlock.h>
27 #include <linux/wait.h>
30 * struct dvb_ringbuffer - Describes a ring buffer used at DVB framework
32 * @data: Area were the ringbuffer data is written
33 * @size: size of the ringbuffer
34 * @pread: next position to read
35 * @pwrite: next position to write
36 * @error: used by ringbuffer clients to indicate that an error happened.
37 * @queue: Wait queue used by ringbuffer clients to indicate when buffer
39 * @lock: Spinlock used to protect the ringbuffer
41 struct dvb_ringbuffer {
48 wait_queue_head_t queue;
52 #define DVB_RINGBUFFER_PKTHDRSIZE 3
55 * dvb_ringbuffer_init - initialize ring buffer, lock and queue
57 * @rbuf: pointer to struct dvb_ringbuffer
58 * @data: pointer to the buffer where the data will be stored
59 * @len: bytes from ring buffer into @buf
61 extern void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data,
65 * dvb_ringbuffer_empty - test whether buffer is empty
67 * @rbuf: pointer to struct dvb_ringbuffer
69 extern int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf);
72 * dvb_ringbuffer_free - returns the number of free bytes in the buffer
74 * @rbuf: pointer to struct dvb_ringbuffer
76 * Return: number of free bytes in the buffer
78 extern ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf);
81 * dvb_ringbuffer_avail - returns the number of bytes waiting in the buffer
83 * @rbuf: pointer to struct dvb_ringbuffer
85 * Return: number of bytes waiting in the buffer
87 extern ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf);
90 * dvb_ringbuffer_reset - resets the ringbuffer to initial state
92 * @rbuf: pointer to struct dvb_ringbuffer
94 * Resets the read and write pointers to zero and flush the buffer.
96 * This counts as a read and write operation
98 extern void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf);
101 * read routines & macros
105 * dvb_ringbuffer_flush - flush buffer
107 * @rbuf: pointer to struct dvb_ringbuffer
109 extern void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf);
112 * dvb_ringbuffer_flush_spinlock_wakeup- flush buffer protected by spinlock
113 * and wake-up waiting task(s)
115 * @rbuf: pointer to struct dvb_ringbuffer
117 extern void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf);
120 * DVB_RINGBUFFER_PEEK - peek at byte @offs in the buffer
122 * @rbuf: pointer to struct dvb_ringbuffer
123 * @offs: offset inside the ringbuffer
125 #define DVB_RINGBUFFER_PEEK(rbuf, offs) \
126 ((rbuf)->data[((rbuf)->pread + (offs)) % (rbuf)->size])
129 * DVB_RINGBUFFER_SKIP - advance read ptr by @num bytes
131 * @rbuf: pointer to struct dvb_ringbuffer
132 * @num: number of bytes to advance
134 #define DVB_RINGBUFFER_SKIP(rbuf, num) {\
135 (rbuf)->pread = ((rbuf)->pread + (num)) % (rbuf)->size;\
139 * dvb_ringbuffer_read_user - Reads a buffer into a user pointer
141 * @rbuf: pointer to struct dvb_ringbuffer
142 * @buf: pointer to the buffer where the data will be stored
143 * @len: bytes from ring buffer into @buf
145 * This variant assumes that the buffer is a memory at the userspace. So,
146 * it will internally call copy_to_user().
148 * Return: number of bytes transferred or -EFAULT
150 extern ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf,
151 u8 __user *buf, size_t len);
154 * dvb_ringbuffer_read - Reads a buffer into a pointer
156 * @rbuf: pointer to struct dvb_ringbuffer
157 * @buf: pointer to the buffer where the data will be stored
158 * @len: bytes from ring buffer into @buf
160 * This variant assumes that the buffer is a memory at the Kernel space
162 * Return: number of bytes transferred or -EFAULT
164 extern void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf,
165 u8 *buf, size_t len);
168 * write routines & macros
172 * DVB_RINGBUFFER_WRITE_BYTE - write single byte to ring buffer
174 * @rbuf: pointer to struct dvb_ringbuffer
175 * @byte: byte to write
177 #define DVB_RINGBUFFER_WRITE_BYTE(rbuf, byte) \
178 { (rbuf)->data[(rbuf)->pwrite] = (byte); \
179 (rbuf)->pwrite = ((rbuf)->pwrite + 1) % (rbuf)->size; }
182 * dvb_ringbuffer_write - Writes a buffer into the ringbuffer
184 * @rbuf: pointer to struct dvb_ringbuffer
185 * @buf: pointer to the buffer where the data will be read
186 * @len: bytes from ring buffer into @buf
188 * This variant assumes that the buffer is a memory at the Kernel space
190 * return: number of bytes transferred or -EFAULT
192 extern ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf,
196 * dvb_ringbuffer_write_user - Writes a buffer received via a user pointer
198 * @rbuf: pointer to struct dvb_ringbuffer
199 * @buf: pointer to the buffer where the data will be read
200 * @len: bytes from ring buffer into @buf
202 * This variant assumes that the buffer is a memory at the userspace. So,
203 * it will internally call copy_from_user().
205 * Return: number of bytes transferred or -EFAULT
207 extern ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf,
208 const u8 __user *buf, size_t len);
211 * dvb_ringbuffer_pkt_write - Write a packet into the ringbuffer.
213 * @rbuf: Ringbuffer to write to.
214 * @buf: Buffer to write.
215 * @len: Length of buffer (currently limited to 65535 bytes max).
217 * Return: Number of bytes written, or -EFAULT, -ENOMEM, -EVINAL.
219 extern ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8 *buf,
223 * dvb_ringbuffer_pkt_read_user - Read from a packet in the ringbuffer.
225 * @rbuf: Ringbuffer concerned.
226 * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
227 * @offset: Offset into packet to read from.
228 * @buf: Destination buffer for data.
229 * @len: Size of destination buffer.
231 * Return: Number of bytes read, or -EFAULT.
235 * unlike dvb_ringbuffer_read(), this does **NOT** update the read pointer
236 * in the ringbuffer. You must use dvb_ringbuffer_pkt_dispose() to mark a
237 * packet as no longer required.
239 extern ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf,
241 int offset, u8 __user *buf,
245 * dvb_ringbuffer_pkt_read - Read from a packet in the ringbuffer.
246 * Note: unlike dvb_ringbuffer_read_user(), this DOES update the read pointer
249 * @rbuf: Ringbuffer concerned.
250 * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
251 * @offset: Offset into packet to read from.
252 * @buf: Destination buffer for data.
253 * @len: Size of destination buffer.
255 * Return: Number of bytes read, or -EFAULT.
257 extern ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx,
258 int offset, u8 *buf, size_t len);
261 * dvb_ringbuffer_pkt_dispose - Dispose of a packet in the ring buffer.
263 * @rbuf: Ring buffer concerned.
264 * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
266 extern void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx);
269 * dvb_ringbuffer_pkt_next - Get the index of the next packet in a ringbuffer.
271 * @rbuf: Ringbuffer concerned.
272 * @idx: Previous packet index, or -1 to return the first packet index.
273 * @pktlen: On success, will be updated to contain the length of the packet
275 * returns Packet index (if >=0), or -1 if no packets available.
277 extern ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf,
278 size_t idx, size_t *pktlen);
280 #endif /* _DVB_RINGBUFFER_H_ */