/*
* dmxdev.c - DVB demultiplexer device
*
* Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
* & Marcus Metzler <marcus@convergence.de>
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 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/spinlock.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/ioctl.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include "dmxdev.h"
static int debug;
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
#define dprintk if (debug) printk
static inline void dvb_dmxdev_buffer_init(struct dmxdev_buffer *buffer)
{
buffer->data=NULL;
buffer->size=8192;
buffer->pread=0;
buffer->pwrite=0;
buffer->error=0;
init_waitqueue_head(&buffer->queue);
}
static inline int dvb_dmxdev_buffer_write(struct dmxdev_buffer *buf, const u8 *src, int len)
{
int split;
int free;
int todo;
if (!len)
return 0;
if (!buf->data)
return 0;
free=buf->pread-buf->pwrite;
split=0;
if (free<=0) {
free+=buf->size;
split=buf->size-buf->pwrite;
}
if (len>=free) {
dprintk("dmxdev: buffer overflow\n");
return -1;
}
if (split>=len)
split=0;
todo=len;
if (split) {
memcpy(buf->data + buf->pwrite, src, split);
todo-=split;
buf->pwrite=0;
}
memcpy(buf->data + buf->pwrite, src+split, todo);
buf->pwrite=(buf->pwrite+todo)%buf->size;
return len;
}
static ssize_t dvb_dmxdev_buffer_read(struct dmxdev_buffer *src,
int non_blocking, char __user *buf, size_t count, loff_t *ppos)
{
unsigned long todo=count;
int split, avail, error;
if (!src->data)
return 0;
if ((error=src->error)) {
src->pwrite=src->pread;
src->error=0;
return error;
}
if (non_blocking && (src->pwrite==src->pread))
return -EWOULDBLOCK;
while (todo>0) {
if (non_blocking && (src->pwrite==src->pread))
return (count-todo) ? (count-todo) : -EWOULDBLOCK;
if (wait_event_interruptible(src->queue,
(src->pread!=src->pwrite) ||
(src->error))<0)
return count-todo;
if ((error=src->error)) {
src->pwrite=src->pread;
src->error=0;
return error;
}