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. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Lesser General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | #ifndef _DVB_RINGBUFFER_H_ |
| 28 | #define _DVB_RINGBUFFER_H_ |
| 29 | |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/wait.h> |
| 32 | |
| 33 | struct dvb_ringbuffer { |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 34 | u8 *data; |
| 35 | ssize_t size; |
| 36 | ssize_t pread; |
| 37 | ssize_t pwrite; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 39 | wait_queue_head_t queue; |
| 40 | spinlock_t lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | #define DVB_RINGBUFFER_PKTHDRSIZE 3 |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | ** Notes: |
| 48 | ** ------ |
| 49 | ** (1) For performance reasons read and write routines don't check buffer sizes |
| 50 | ** and/or number of bytes free/available. This has to be done before these |
| 51 | ** routines are called. For example: |
| 52 | ** |
| 53 | ** *** write <buflen> bytes *** |
| 54 | ** free = dvb_ringbuffer_free(rbuf); |
| 55 | ** if (free >= buflen) |
| 56 | ** count = dvb_ringbuffer_write(rbuf, buffer, buflen); |
| 57 | ** else |
| 58 | ** ... |
| 59 | ** |
| 60 | ** *** read min. 1000, max. <bufsize> bytes *** |
| 61 | ** avail = dvb_ringbuffer_avail(rbuf); |
| 62 | ** if (avail >= 1000) |
| 63 | ** count = dvb_ringbuffer_read(rbuf, buffer, min(avail, bufsize), 0); |
| 64 | ** else |
| 65 | ** ... |
| 66 | ** |
| 67 | ** (2) If there is exactly one reader and one writer, there is no need |
| 68 | ** to lock read or write operations. |
| 69 | ** Two or more readers must be locked against each other. |
| 70 | ** Flushing the buffer counts as a read operation. |
| 71 | ** Two or more writers must be locked against each other. |
| 72 | */ |
| 73 | |
| 74 | /* initialize ring buffer, lock and queue */ |
| 75 | extern void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, size_t len); |
| 76 | |
| 77 | /* test whether buffer is empty */ |
| 78 | extern int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf); |
| 79 | |
| 80 | /* return the number of free bytes in the buffer */ |
| 81 | extern ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf); |
| 82 | |
| 83 | /* return the number of bytes waiting in the buffer */ |
| 84 | extern ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf); |
| 85 | |
| 86 | |
| 87 | /* read routines & macros */ |
| 88 | /* ---------------------- */ |
| 89 | /* flush buffer */ |
| 90 | extern void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf); |
| 91 | |
| 92 | /* flush buffer protected by spinlock and wake-up waiting task(s) */ |
| 93 | extern void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf); |
| 94 | |
| 95 | /* peek at byte <offs> in the buffer */ |
| 96 | #define DVB_RINGBUFFER_PEEK(rbuf,offs) \ |
| 97 | (rbuf)->data[((rbuf)->pread+(offs))%(rbuf)->size] |
| 98 | |
| 99 | /* advance read ptr by <num> bytes */ |
| 100 | #define DVB_RINGBUFFER_SKIP(rbuf,num) \ |
| 101 | (rbuf)->pread=((rbuf)->pread+(num))%(rbuf)->size |
| 102 | |
| 103 | /* |
| 104 | ** read <len> bytes from ring buffer into <buf> |
| 105 | ** <usermem> specifies whether <buf> resides in user space |
| 106 | ** returns number of bytes transferred or -EFAULT |
| 107 | */ |
| 108 | extern ssize_t dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, u8 *buf, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 109 | size_t len, int usermem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
| 111 | |
| 112 | /* write routines & macros */ |
| 113 | /* ----------------------- */ |
| 114 | /* write single byte to ring buffer */ |
| 115 | #define DVB_RINGBUFFER_WRITE_BYTE(rbuf,byte) \ |
| 116 | { (rbuf)->data[(rbuf)->pwrite]=(byte); \ |
| 117 | (rbuf)->pwrite=((rbuf)->pwrite+1)%(rbuf)->size; } |
| 118 | /* |
| 119 | ** write <len> bytes to ring buffer |
| 120 | ** <usermem> specifies whether <buf> resides in user space |
| 121 | ** returns number of bytes transferred or -EFAULT |
| 122 | */ |
| 123 | extern ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 124 | size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | |
| 127 | /** |
| 128 | * Write a packet into the ringbuffer. |
| 129 | * |
| 130 | * <rbuf> Ringbuffer to write to. |
| 131 | * <buf> Buffer to write. |
| 132 | * <len> Length of buffer (currently limited to 65535 bytes max). |
| 133 | * returns Number of bytes written, or -EFAULT, -ENOMEM, -EVINAL. |
| 134 | */ |
| 135 | extern ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8* buf, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 136 | size_t len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Read from a packet in the ringbuffer. Note: unlike dvb_ringbuffer_read(), this |
| 140 | * does NOT update the read pointer in the ringbuffer. You must use |
| 141 | * dvb_ringbuffer_pkt_dispose() to mark a packet as no longer required. |
| 142 | * |
| 143 | * <rbuf> Ringbuffer concerned. |
| 144 | * <idx> Packet index as returned by dvb_ringbuffer_pkt_next(). |
| 145 | * <offset> Offset into packet to read from. |
| 146 | * <buf> Destination buffer for data. |
| 147 | * <len> Size of destination buffer. |
| 148 | * <usermem> Set to 1 if <buf> is in userspace. |
| 149 | * returns Number of bytes read, or -EFAULT. |
| 150 | */ |
| 151 | extern ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx, |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame^] | 152 | int offset, u8* buf, size_t len, int usermem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Dispose of a packet in the ring buffer. |
| 156 | * |
| 157 | * <rbuf> Ring buffer concerned. |
| 158 | * <idx> Packet index as returned by dvb_ringbuffer_pkt_next(). |
| 159 | */ |
| 160 | extern void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx); |
| 161 | |
| 162 | /** |
| 163 | * Get the index of the next packet in a ringbuffer. |
| 164 | * |
| 165 | * <rbuf> Ringbuffer concerned. |
| 166 | * <idx> Previous packet index, or -1 to return the first packet index. |
| 167 | * <pktlen> On success, will be updated to contain the length of the packet in bytes. |
| 168 | * returns Packet index (if >=0), or -1 if no packets available. |
| 169 | */ |
| 170 | extern ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, size_t idx, size_t* pktlen); |
| 171 | |
| 172 | |
| 173 | #endif /* _DVB_RINGBUFFER_H_ */ |