blob: bbe94873d44d314a120acdf821dd160d6e06eae7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * dvb_ringbuffer.h: ring buffer implementation for the dvb driver
4 *
5 * Copyright (C) 2003 Oliver Endriss
6 * Copyright (C) 2004 Andrew de Quincey
7 *
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
11 *
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.
16 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 */
22
23#ifndef _DVB_RINGBUFFER_H_
24#define _DVB_RINGBUFFER_H_
25
26#include <linux/spinlock.h>
27#include <linux/wait.h>
28
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030029/**
30 * struct dvb_ringbuffer - Describes a ring buffer used at DVB framework
31 *
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
38 * was filled
39 * @lock: Spinlock used to protect the ringbuffer
40 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041struct dvb_ringbuffer {
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080042 u8 *data;
43 ssize_t size;
44 ssize_t pread;
45 ssize_t pwrite;
Andreas Oberritter9d9d6ba2006-03-13 13:14:34 -030046 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080048 wait_queue_head_t queue;
49 spinlock_t lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
52#define DVB_RINGBUFFER_PKTHDRSIZE 3
53
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030054/**
55 * dvb_ringbuffer_init - initialize ring buffer, lock and queue
56 *
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
60 */
61extern void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data,
62 size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030064/**
65 * dvb_ringbuffer_empty - test whether buffer is empty
66 *
67 * @rbuf: pointer to struct dvb_ringbuffer
68 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069extern int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf);
70
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030071/**
72 * dvb_ringbuffer_free - returns the number of free bytes in the buffer
73 *
74 * @rbuf: pointer to struct dvb_ringbuffer
75 *
76 * Return: number of free bytes in the buffer
77 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078extern ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf);
79
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030080/**
81 * dvb_ringbuffer_avail - returns the number of bytes waiting in the buffer
82 *
83 * @rbuf: pointer to struct dvb_ringbuffer
84 *
85 * Return: number of bytes waiting in the buffer
86 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087extern ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf);
88
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -030089/**
90 * dvb_ringbuffer_reset - resets the ringbuffer to initial state
91 *
92 * @rbuf: pointer to struct dvb_ringbuffer
93 *
94 * Resets the read and write pointers to zero and flush the buffer.
95 *
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -030096 * This counts as a read and write operation
97 */
Andrea Odetti48c01a92008-04-20 18:37:45 -030098extern void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf);
99
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300100/*
101 * read routines & macros
102 */
103
104/**
105 * dvb_ringbuffer_flush - flush buffer
106 *
107 * @rbuf: pointer to struct dvb_ringbuffer
108 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109extern void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf);
110
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300111/**
112 * dvb_ringbuffer_flush_spinlock_wakeup- flush buffer protected by spinlock
113 * and wake-up waiting task(s)
114 *
115 * @rbuf: pointer to struct dvb_ringbuffer
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117extern void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf);
118
Mauro Carvalho Chehab9aa9d922016-08-29 10:01:06 -0300119/**
120 * DVB_RINGBUFFER_PEEK - peek at byte @offs in the buffer
121 *
122 * @rbuf: pointer to struct dvb_ringbuffer
123 * @offs: offset inside the ringbuffer
124 */
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300125#define DVB_RINGBUFFER_PEEK(rbuf, offs) \
Mauro Carvalho Chehab9aa9d922016-08-29 10:01:06 -0300126 ((rbuf)->data[((rbuf)->pread + (offs)) % (rbuf)->size])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Mauro Carvalho Chehab9aa9d922016-08-29 10:01:06 -0300128/**
129 * DVB_RINGBUFFER_SKIP - advance read ptr by @num bytes
130 *
131 * @rbuf: pointer to struct dvb_ringbuffer
132 * @num: number of bytes to advance
133 */
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300134#define DVB_RINGBUFFER_SKIP(rbuf, num) {\
135 (rbuf)->pread = ((rbuf)->pread + (num)) % (rbuf)->size;\
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300138/**
139 * dvb_ringbuffer_read_user - Reads a buffer into an user pointer
140 *
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
144 *
145 * This variant assumes that the buffer is a memory at the userspace. So,
146 * it will internally call copy_to_user().
147 *
148 * Return: number of bytes transferred or -EFAULT
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300149 */
Al Virob0ba0e32008-06-22 14:20:29 -0300150extern ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf,
151 u8 __user *buf, size_t len);
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300152
153/**
154 * dvb_ringbuffer_read - Reads a buffer into a pointer
155 *
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
159 *
160 * This variant assumes that the buffer is a memory at the Kernel space
161 *
162 * Return: number of bytes transferred or -EFAULT
163 */
Al Virob0ba0e32008-06-22 14:20:29 -0300164extern void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf,
165 u8 *buf, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300168 * write routines & macros
169 */
170
Mauro Carvalho Chehab9aa9d922016-08-29 10:01:06 -0300171/**
172 * DVB_RINGBUFFER_WRITE_BYTE - write single byte to ring buffer
173 *
174 * @rbuf: pointer to struct dvb_ringbuffer
175 * @byte: byte to write
176 */
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300177#define DVB_RINGBUFFER_WRITE_BYTE(rbuf, byte) \
178 { (rbuf)->data[(rbuf)->pwrite] = (byte); \
179 (rbuf)->pwrite = ((rbuf)->pwrite + 1) % (rbuf)->size; }
180
181/**
182 * dvb_ringbuffer_write - Writes a buffer into the ringbuffer
183 *
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
187 *
188 * This variant assumes that the buffer is a memory at the Kernel space
189 *
190 * return: number of bytes transferred or -EFAULT
191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192extern ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200193 size_t len);
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300194
195/**
Mauro Carvalho Chehabe383ce02016-09-22 07:59:03 -0300196 * dvb_ringbuffer_write_user - Writes a buffer received via an user pointer
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300197 *
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
201 *
202 * This variant assumes that the buffer is a memory at the userspace. So,
203 * it will internally call copy_from_user().
204 *
205 * Return: number of bytes transferred or -EFAULT
206 */
Mauro Carvalho Chehab04da2da2014-09-03 20:44:04 -0300207extern ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf,
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300208 const u8 __user *buf, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/**
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300211 * dvb_ringbuffer_pkt_write - Write a packet into the ringbuffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300213 * @rbuf: Ringbuffer to write to.
214 * @buf: Buffer to write.
215 * @len: Length of buffer (currently limited to 65535 bytes max).
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300216 *
217 * Return: Number of bytes written, or -EFAULT, -ENOMEM, -EVINAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300219extern ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8 *buf,
Michael Krufky50c25ff2006-01-09 15:25:34 -0200220 size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222/**
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300223 * dvb_ringbuffer_pkt_read_user - Read from a packet in the ringbuffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300225 * @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.
230 *
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300231 * Return: Number of bytes read, or -EFAULT.
Mauro Carvalho Chehab7a23c252016-07-22 13:58:09 -0300232 *
233 * .. note::
234 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
Mauro Carvalho Chehab7a23c252016-07-22 13:58:09 -0300239extern ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf,
240 size_t idx,
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300241 int offset, u8 __user *buf,
Mauro Carvalho Chehab7a23c252016-07-22 13:58:09 -0300242 size_t len);
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300243
244/**
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
247 * in the ringbuffer.
248 *
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.
254 *
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300255 * Return: Number of bytes read, or -EFAULT.
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257extern ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx,
Al Virob0ba0e32008-06-22 14:20:29 -0300258 int offset, u8 *buf, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260/**
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300261 * dvb_ringbuffer_pkt_dispose - Dispose of a packet in the ring buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 *
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300263 * @rbuf: Ring buffer concerned.
264 * @idx: Packet index as returned by dvb_ringbuffer_pkt_next().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266extern void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx);
267
268/**
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300269 * dvb_ringbuffer_pkt_next - Get the index of the next packet in a ringbuffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Mauro Carvalho Chehab2a86e372015-08-22 07:38:51 -0300271 * @rbuf: Ringbuffer concerned.
272 * @idx: Previous packet index, or -1 to return the first packet index.
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300273 * @pktlen: On success, will be updated to contain the length of the packet
274 * in bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * returns Packet index (if >=0), or -1 if no packets available.
276 */
Mauro Carvalho Chehabff54c192016-08-29 08:38:01 -0300277extern ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf,
278 size_t idx, size_t *pktlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif /* _DVB_RINGBUFFER_H_ */