dvb_ringbuffer.c 7.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/*
 *
 * dvb_ringbuffer.c: ring buffer implementation for the dvb driver
 *
 * Copyright (C) 2003 Oliver Endriss
 * Copyright (C) 2004 Andrew de Quincey
 *
 * based on code originally found in av7110.c & dvb_ci.c:
 * Copyright (C) 1999-2003 Ralph  Metzler
 *                       & Marcus Metzler for convergence integrated media GmbH
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */



#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <asm/uaccess.h>

#include "dvb_ringbuffer.h"

#define PKT_READY 0
#define PKT_DISPOSED 1


void dvb_ringbuffer_init(struct dvb_ringbuffer *rbuf, void *data, size_t len)
{
44 45 46
	rbuf->pread=rbuf->pwrite=0;
	rbuf->data=data;
	rbuf->size=len;
47
	rbuf->error=0;
L
Linus Torvalds 已提交
48

49
	init_waitqueue_head(&rbuf->queue);
L
Linus Torvalds 已提交
50

51
	spin_lock_init(&(rbuf->lock));
L
Linus Torvalds 已提交
52 53 54 55 56 57
}



int dvb_ringbuffer_empty(struct dvb_ringbuffer *rbuf)
{
58
	return (rbuf->pread==rbuf->pwrite);
L
Linus Torvalds 已提交
59 60 61 62 63 64
}



ssize_t dvb_ringbuffer_free(struct dvb_ringbuffer *rbuf)
{
65
	ssize_t free;
L
Linus Torvalds 已提交
66

67 68 69 70
	free = rbuf->pread - rbuf->pwrite;
	if (free <= 0)
		free += rbuf->size;
	return free-1;
L
Linus Torvalds 已提交
71 72 73 74 75 76
}



ssize_t dvb_ringbuffer_avail(struct dvb_ringbuffer *rbuf)
{
77
	ssize_t avail;
L
Linus Torvalds 已提交
78

79 80 81 82
	avail = rbuf->pwrite - rbuf->pread;
	if (avail < 0)
		avail += rbuf->size;
	return avail;
L
Linus Torvalds 已提交
83 84 85 86 87 88
}



void dvb_ringbuffer_flush(struct dvb_ringbuffer *rbuf)
{
89
	rbuf->pread = rbuf->pwrite;
90
	rbuf->error = 0;
L
Linus Torvalds 已提交
91
}
92
EXPORT_SYMBOL(dvb_ringbuffer_flush);
L
Linus Torvalds 已提交
93

94 95 96 97 98
void dvb_ringbuffer_reset(struct dvb_ringbuffer *rbuf)
{
	rbuf->pread = rbuf->pwrite = 0;
	rbuf->error = 0;
}
L
Linus Torvalds 已提交
99 100 101

void dvb_ringbuffer_flush_spinlock_wakeup(struct dvb_ringbuffer *rbuf)
{
102
	unsigned long flags;
L
Linus Torvalds 已提交
103

104 105 106
	spin_lock_irqsave(&rbuf->lock, flags);
	dvb_ringbuffer_flush(rbuf);
	spin_unlock_irqrestore(&rbuf->lock, flags);
L
Linus Torvalds 已提交
107

108
	wake_up(&rbuf->queue);
L
Linus Torvalds 已提交
109 110
}

111
ssize_t dvb_ringbuffer_read_user(struct dvb_ringbuffer *rbuf, u8 __user *buf, size_t len)
L
Linus Torvalds 已提交
112
{
113 114 115 116 117
	size_t todo = len;
	size_t split;

	split = (rbuf->pread + len > rbuf->size) ? rbuf->size - rbuf->pread : 0;
	if (split > 0) {
118 119
		if (copy_to_user(buf, rbuf->data+rbuf->pread, split))
			return -EFAULT;
120 121 122 123
		buf += split;
		todo -= split;
		rbuf->pread = 0;
	}
124 125
	if (copy_to_user(buf, rbuf->data+rbuf->pread, todo))
		return -EFAULT;
126 127 128 129

	rbuf->pread = (rbuf->pread + todo) % rbuf->size;

	return len;
L
Linus Torvalds 已提交
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
void dvb_ringbuffer_read(struct dvb_ringbuffer *rbuf, u8 *buf, size_t len)
{
	size_t todo = len;
	size_t split;

	split = (rbuf->pread + len > rbuf->size) ? rbuf->size - rbuf->pread : 0;
	if (split > 0) {
		memcpy(buf, rbuf->data+rbuf->pread, split);
		buf += split;
		todo -= split;
		rbuf->pread = 0;
	}
	memcpy(buf, rbuf->data+rbuf->pread, todo);

	rbuf->pread = (rbuf->pread + todo) % rbuf->size;
}
L
Linus Torvalds 已提交
148 149 150 151


ssize_t dvb_ringbuffer_write(struct dvb_ringbuffer *rbuf, const u8 *buf, size_t len)
{
152 153
	size_t todo = len;
	size_t split;
L
Linus Torvalds 已提交
154

155
	split = (rbuf->pwrite + len > rbuf->size) ? rbuf->size - rbuf->pwrite : 0;
L
Linus Torvalds 已提交
156

157 158 159 160 161 162 163 164
	if (split > 0) {
		memcpy(rbuf->data+rbuf->pwrite, buf, split);
		buf += split;
		todo -= split;
		rbuf->pwrite = 0;
	}
	memcpy(rbuf->data+rbuf->pwrite, buf, todo);
	rbuf->pwrite = (rbuf->pwrite + todo) % rbuf->size;
L
Linus Torvalds 已提交
165

166
	return len;
L
Linus Torvalds 已提交
167 168 169 170
}

ssize_t dvb_ringbuffer_pkt_write(struct dvb_ringbuffer *rbuf, u8* buf, size_t len)
{
171 172
	int status;
	ssize_t oldpwrite = rbuf->pwrite;
L
Linus Torvalds 已提交
173

174 175 176 177
	DVB_RINGBUFFER_WRITE_BYTE(rbuf, len >> 8);
	DVB_RINGBUFFER_WRITE_BYTE(rbuf, len & 0xff);
	DVB_RINGBUFFER_WRITE_BYTE(rbuf, PKT_READY);
	status = dvb_ringbuffer_write(rbuf, buf, len);
L
Linus Torvalds 已提交
178

179 180
	if (status < 0) rbuf->pwrite = oldpwrite;
	return status;
L
Linus Torvalds 已提交
181 182
}

183 184
ssize_t dvb_ringbuffer_pkt_read_user(struct dvb_ringbuffer *rbuf, size_t idx,
				int offset, u8 __user *buf, size_t len)
L
Linus Torvalds 已提交
185
{
186 187 188 189 190 191 192 193 194 195 196 197 198
	size_t todo;
	size_t split;
	size_t pktlen;

	pktlen = rbuf->data[idx] << 8;
	pktlen |= rbuf->data[(idx + 1) % rbuf->size];
	if (offset > pktlen) return -EINVAL;
	if ((offset + len) > pktlen) len = pktlen - offset;

	idx = (idx + DVB_RINGBUFFER_PKTHDRSIZE + offset) % rbuf->size;
	todo = len;
	split = ((idx + len) > rbuf->size) ? rbuf->size - idx : 0;
	if (split > 0) {
199 200
		if (copy_to_user(buf, rbuf->data+idx, split))
			return -EFAULT;
201 202 203 204
		buf += split;
		todo -= split;
		idx = 0;
	}
205 206 207 208 209
	if (copy_to_user(buf, rbuf->data+idx, todo))
		return -EFAULT;

	return len;
}
210

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
ssize_t dvb_ringbuffer_pkt_read(struct dvb_ringbuffer *rbuf, size_t idx,
				int offset, u8* buf, size_t len)
{
	size_t todo;
	size_t split;
	size_t pktlen;

	pktlen = rbuf->data[idx] << 8;
	pktlen |= rbuf->data[(idx + 1) % rbuf->size];
	if (offset > pktlen) return -EINVAL;
	if ((offset + len) > pktlen) len = pktlen - offset;

	idx = (idx + DVB_RINGBUFFER_PKTHDRSIZE + offset) % rbuf->size;
	todo = len;
	split = ((idx + len) > rbuf->size) ? rbuf->size - idx : 0;
	if (split > 0) {
		memcpy(buf, rbuf->data+idx, split);
		buf += split;
		todo -= split;
		idx = 0;
	}
	memcpy(buf, rbuf->data+idx, todo);
233
	return len;
L
Linus Torvalds 已提交
234 235 236 237
}

void dvb_ringbuffer_pkt_dispose(struct dvb_ringbuffer *rbuf, size_t idx)
{
238 239 240 241 242 243 244
	size_t pktlen;

	rbuf->data[(idx + 2) % rbuf->size] = PKT_DISPOSED;

	// clean up disposed packets
	while(dvb_ringbuffer_avail(rbuf) > DVB_RINGBUFFER_PKTHDRSIZE) {
		if (DVB_RINGBUFFER_PEEK(rbuf, 2) == PKT_DISPOSED) {
245 246 247
			pktlen = DVB_RINGBUFFER_PEEK(rbuf, 0) << 8;
			pktlen |= DVB_RINGBUFFER_PEEK(rbuf, 1);
			DVB_RINGBUFFER_SKIP(rbuf, pktlen + DVB_RINGBUFFER_PKTHDRSIZE);
248
		} else {
249 250
			// first packet is not disposed, so we stop cleaning now
			break;
251 252
		}
	}
L
Linus Torvalds 已提交
253 254 255 256
}

ssize_t dvb_ringbuffer_pkt_next(struct dvb_ringbuffer *rbuf, size_t idx, size_t* pktlen)
{
257 258 259
	int consumed;
	int curpktlen;
	int curpktstatus;
L
Linus Torvalds 已提交
260

261
	if (idx == -1) {
L
Linus Torvalds 已提交
262 263
	       idx = rbuf->pread;
	} else {
264 265 266
		curpktlen = rbuf->data[idx] << 8;
		curpktlen |= rbuf->data[(idx + 1) % rbuf->size];
		idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size;
L
Linus Torvalds 已提交
267 268
	}

269
	consumed = (idx - rbuf->pread) % rbuf->size;
L
Linus Torvalds 已提交
270

271
	while((dvb_ringbuffer_avail(rbuf) - consumed) > DVB_RINGBUFFER_PKTHDRSIZE) {
L
Linus Torvalds 已提交
272

273 274 275
		curpktlen = rbuf->data[idx] << 8;
		curpktlen |= rbuf->data[(idx + 1) % rbuf->size];
		curpktstatus = rbuf->data[(idx + 2) % rbuf->size];
L
Linus Torvalds 已提交
276

277
		if (curpktstatus == PKT_READY) {
278 279
			*pktlen = curpktlen;
			return idx;
280
		}
L
Linus Torvalds 已提交
281

282 283 284
		consumed += curpktlen + DVB_RINGBUFFER_PKTHDRSIZE;
		idx = (idx + curpktlen + DVB_RINGBUFFER_PKTHDRSIZE) % rbuf->size;
	}
L
Linus Torvalds 已提交
285

286 287
	// no packets available
	return -1;
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296
}



EXPORT_SYMBOL(dvb_ringbuffer_init);
EXPORT_SYMBOL(dvb_ringbuffer_empty);
EXPORT_SYMBOL(dvb_ringbuffer_free);
EXPORT_SYMBOL(dvb_ringbuffer_avail);
EXPORT_SYMBOL(dvb_ringbuffer_flush_spinlock_wakeup);
297
EXPORT_SYMBOL(dvb_ringbuffer_read_user);
L
Linus Torvalds 已提交
298 299
EXPORT_SYMBOL(dvb_ringbuffer_read);
EXPORT_SYMBOL(dvb_ringbuffer_write);