/*
* 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;
}
split=src->size;
avail=src->pwrite - src->pread;
if (avail<0) {
avail+=src->size;
split=src->size - src->pread;
}
if (avail>todo)
avail=todo;
if (split<avail) {
if (copy_to_user(buf, src->data+src->pread, split))
return -EFAULT;
buf+=split;
src->pread=0;
todo-=split;
avail-=split;
}
if (avail) {
if (copy_to_user(buf, src->data+src->pread, avail))
return -EFAULT;
src->pread = (src->pread + avail) % src->size;
todo-=avail;
buf+=avail;
}
}
return count;
}
static struct dmx_frontend * get_fe(struct dmx_demux *demux, int type)
{
struct list_head *head, *pos;
head=demux->get_frontends(demux);
if (!head)
return NULL;
list_for_each(pos, head)
if (DMX_FE_ENTRY(pos)->source==type)
return DMX_FE_ENTRY(pos);
return NULL;
}
static inline void dvb_dmxdev_dvr_state_set(struct dmxdev_dvr *dmxdevdvr, int state)
{
spin_lock_irq(&dmxdevdvr->dev->lock);
dmxdevdvr->state=state;
spin_unlock_irq(&dmxdevdvr->dev->lock);
}
static int dvb_dvr_open(struct inode *inode, struct file *file)
{
struct dvb_device *dvbdev = file->private_data;
struct dmxdev *dmxdev = dvbdev->priv;
struct dmx_frontend *front;
dprintk ("function : %s\n", __FUNCTION__);
if (down_interruptible (&dmxdev->mutex))
return -ERESTARTSYS;
if ((file->f_flags&O_ACCMODE)==O_RDWR) {
if (!(dmxdev->capabilities&DMXDEV_CAP_DUPLEX)) {
up(&dmxdev->mutex);
return -EOPNOTSUPP;
}
}
if ((file->f_flags&O_ACCMODE)==O_RDONLY) {
dvb_dmxdev_buffer_init(&dmxdev->dvr_buffer);
dmxdev->dvr_buffer.size=DVR_BUFFER_SIZE;
dmxdev->dvr_buffer.data=vmalloc(DVR_BUFFER_SIZE);
if (!dmxdev->dvr_buffer.data) {
up(&dmxdev->mutex);
return -ENOMEM;
}
}
if ((file->f_flags&O_ACCMODE)==O_WRONLY) {
dmxdev->dvr_orig_fe=dmxdev->demux->frontend;
if (!dmxdev->demux->write) {
up(&dmxdev->mutex);
return -EOPNOTSUPP;
}
front=get_fe(dmxdev->demux, DMX_MEMORY_FE);
if
|