Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | */ |
| 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 Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 29 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | struct dvb_ringbuffer { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 42 | u8 *data; |
| 43 | ssize_t size; |
| 44 | ssize_t pread; |
| 45 | ssize_t pwrite; |
Andreas Oberritter | 9d9d6ba | 2006-03-13 13:14:34 -0300 | [diff] [blame] | 46 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 48 | wait_queue_head_t queue; |
| 49 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | #define DVB_RINGBUFFER_PKTHDRSIZE 3 |
| 53 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 54 | /** |
| 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 | */ |
| 61 | extern void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, |
| 62 | size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 64 | /** |
| 65 | * dvb_ringbuffer_empty - test whether buffer is empty |
| 66 | * |
| 67 | * @rbuf: pointer to struct dvb_ringbuffer |
| 68 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | extern int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf); |
| 70 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 71 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | extern ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf); |
| 79 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 80 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | extern ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf); |
| 88 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 89 | /** |
| 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 Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 96 | * This counts as a read and write operation |
| 97 | */ |
Andrea Odetti | 48c01a9 | 2008-04-20 18:37:45 -0300 | [diff] [blame] | 98 | extern void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf); |
| 99 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 100 | /* |
| 101 | * read routines & macros |
| 102 | */ |
| 103 | |
| 104 | /** |
| 105 | * dvb_ringbuffer_flush - flush buffer |
| 106 | * |
| 107 | * @rbuf: pointer to struct dvb_ringbuffer |
| 108 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | extern void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf); |
| 110 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 111 | /** |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | extern void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf); |
| 118 | |
Mauro Carvalho Chehab | 9aa9d92 | 2016-08-29 10:01:06 -0300 | [diff] [blame] | 119 | /** |
| 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 Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 125 | #define DVB_RINGBUFFER_PEEK(rbuf, offs) \ |
Mauro Carvalho Chehab | 9aa9d92 | 2016-08-29 10:01:06 -0300 | [diff] [blame] | 126 | ((rbuf)->data[((rbuf)->pread + (offs)) % (rbuf)->size]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Mauro Carvalho Chehab | 9aa9d92 | 2016-08-29 10:01:06 -0300 | [diff] [blame] | 128 | /** |
| 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 Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 134 | #define DVB_RINGBUFFER_SKIP(rbuf, num) {\ |
| 135 | (rbuf)->pread = ((rbuf)->pread + (num)) % (rbuf)->size;\ |
| 136 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 138 | /** |
Masahiro Yamada | 9332ef9 | 2017-02-27 14:28:47 -0800 | [diff] [blame] | 139 | * dvb_ringbuffer_read_user - Reads a buffer into a user pointer |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 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 Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 149 | */ |
Al Viro | b0ba0e3 | 2008-06-22 14:20:29 -0300 | [diff] [blame] | 150 | extern ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf, |
| 151 | u8 __user *buf, size_t len); |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 152 | |
| 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 Viro | b0ba0e3 | 2008-06-22 14:20:29 -0300 | [diff] [blame] | 164 | extern void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, |
| 165 | u8 *buf, size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 168 | * write routines & macros |
| 169 | */ |
| 170 | |
Mauro Carvalho Chehab | 9aa9d92 | 2016-08-29 10:01:06 -0300 | [diff] [blame] | 171 | /** |
| 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 Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 177 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | extern ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 193 | size_t len); |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 194 | |
| 195 | /** |
Masahiro Yamada | 9332ef9 | 2017-02-27 14:28:47 -0800 | [diff] [blame] | 196 | * dvb_ringbuffer_write_user - Writes a buffer received via a user pointer |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 197 | * |
| 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 Chehab | 04da2da | 2014-09-03 20:44:04 -0300 | [diff] [blame] | 207 | extern ssize_t dvb_ringbuffer_write_user(struct dvb_ringbuffer *rbuf, |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 208 | const u8 __user *buf, size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | /** |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 211 | * dvb_ringbuffer_pkt_write - Write a packet into the ringbuffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 213 | * @rbuf: Ringbuffer to write to. |
| 214 | * @buf: Buffer to write. |
| 215 | * @len: Length of buffer (currently limited to 65535 bytes max). |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 216 | * |
| 217 | * Return: Number of bytes written, or -EFAULT, -ENOMEM, -EVINAL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | */ |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 219 | extern ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8 *buf, |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 220 | size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | /** |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 223 | * dvb_ringbuffer_pkt_read_user - Read from a packet in the ringbuffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | * |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 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. |
| 230 | * |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 231 | * Return: Number of bytes read, or -EFAULT. |
Mauro Carvalho Chehab | 7a23c25 | 2016-07-22 13:58:09 -0300 | [diff] [blame] | 232 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | */ |
Mauro Carvalho Chehab | 7a23c25 | 2016-07-22 13:58:09 -0300 | [diff] [blame] | 239 | extern ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf, |
| 240 | size_t idx, |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 241 | int offset, u8 __user *buf, |
Mauro Carvalho Chehab | 7a23c25 | 2016-07-22 13:58:09 -0300 | [diff] [blame] | 242 | size_t len); |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 243 | |
| 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 Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 255 | * Return: Number of bytes read, or -EFAULT. |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 256 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | extern ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx, |
Al Viro | b0ba0e3 | 2008-06-22 14:20:29 -0300 | [diff] [blame] | 258 | int offset, u8 *buf, size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 261 | * dvb_ringbuffer_pkt_dispose - Dispose of a packet in the ring buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | * |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 263 | * @rbuf: Ring buffer concerned. |
| 264 | * @idx: Packet index as returned by dvb_ringbuffer_pkt_next(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | */ |
| 266 | extern void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx); |
| 267 | |
| 268 | /** |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 269 | * dvb_ringbuffer_pkt_next - Get the index of the next packet in a ringbuffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | * |
Mauro Carvalho Chehab | 2a86e37 | 2015-08-22 07:38:51 -0300 | [diff] [blame] | 271 | * @rbuf: Ringbuffer concerned. |
| 272 | * @idx: Previous packet index, or -1 to return the first packet index. |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 273 | * @pktlen: On success, will be updated to contain the length of the packet |
| 274 | * in bytes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | * returns Packet index (if >=0), or -1 if no packets available. |
| 276 | */ |
Mauro Carvalho Chehab | ff54c19 | 2016-08-29 08:38:01 -0300 | [diff] [blame] | 277 | extern ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, |
| 278 | size_t idx, size_t *pktlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | #endif /* _DVB_RINGBUFFER_H_ */ |