dmxdev.c 30.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * dmxdev.c - DVB demultiplexer device
 *
4 5
 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
 *		      for convergence integrated media GmbH
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 *
 */

23 24
#define pr_fmt(fmt) "dmxdev: " fmt

25
#include <linux/sched.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/ioctl.h>
#include <linux/wait.h>
#include <asm/uaccess.h>
#include "dmxdev.h"

static int debug;

module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");

41 42 43 44 45
#define dprintk(fmt, arg...) do {					\
	if (debug)							\
		printk(KERN_DEBUG pr_fmt("%s: " fmt),			\
			__func__, ##arg);				\
} while (0)
L
Linus Torvalds 已提交
46

47 48
static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
				   const u8 *src, size_t len)
L
Linus Torvalds 已提交
49
{
50
	ssize_t free;
L
Linus Torvalds 已提交
51 52 53 54 55 56

	if (!len)
		return 0;
	if (!buf->data)
		return 0;

57 58
	free = dvb_ringbuffer_free(buf);
	if (len > free) {
59
		dprintk("buffer overflow\n");
60
		return -EOVERFLOW;
L
Linus Torvalds 已提交
61
	}
62 63

	return dvb_ringbuffer_write(buf, src, len);
L
Linus Torvalds 已提交
64 65
}

66
static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
67 68
				      int non_blocking, char __user *buf,
				      size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
69
{
70 71 72
	size_t todo;
	ssize_t avail;
	ssize_t ret = 0;
L
Linus Torvalds 已提交
73 74 75 76

	if (!src->data)
		return 0;

77 78 79 80
	if (src->error) {
		ret = src->error;
		dvb_ringbuffer_flush(src);
		return ret;
L
Linus Torvalds 已提交
81 82
	}

83 84 85 86 87
	for (todo = count; todo > 0; todo -= ret) {
		if (non_blocking && dvb_ringbuffer_empty(src)) {
			ret = -EWOULDBLOCK;
			break;
		}
L
Linus Torvalds 已提交
88

89 90 91 92 93
		ret = wait_event_interruptible(src->queue,
					       !dvb_ringbuffer_empty(src) ||
					       (src->error != 0));
		if (ret < 0)
			break;
L
Linus Torvalds 已提交
94

95 96 97 98
		if (src->error) {
			ret = src->error;
			dvb_ringbuffer_flush(src);
			break;
L
Linus Torvalds 已提交
99 100
		}

101
		avail = dvb_ringbuffer_avail(src);
102 103
		if (avail > todo)
			avail = todo;
104

105
		ret = dvb_ringbuffer_read_user(src, buf, avail);
106 107 108 109
		if (ret < 0)
			break;

		buf += ret;
L
Linus Torvalds 已提交
110
	}
111 112

	return (count - todo) ? (count - todo) : ret;
L
Linus Torvalds 已提交
113 114
}

115
static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
L
Linus Torvalds 已提交
116 117 118
{
	struct list_head *head, *pos;

119
	head = demux->get_frontends(demux);
L
Linus Torvalds 已提交
120 121 122
	if (!head)
		return NULL;
	list_for_each(pos, head)
123
		if (DMX_FE_ENTRY(pos)->source == type)
L
Linus Torvalds 已提交
124 125 126 127 128 129 130
			return DMX_FE_ENTRY(pos);

	return NULL;
}

static int dvb_dvr_open(struct inode *inode, struct file *file)
{
131 132
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
133 134
	struct dmx_frontend *front;

135
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
136

137
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
138 139
		return -ERESTARTSYS;

140 141 142 143 144
	if (dmxdev->exit) {
		mutex_unlock(&dmxdev->mutex);
		return -ENODEV;
	}

145 146
	if ((file->f_flags & O_ACCMODE) == O_RDWR) {
		if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
147
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
148 149 150 151
			return -EOPNOTSUPP;
		}
	}

152
	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
153 154 155 156 157 158
		void *mem;
		if (!dvbdev->readers) {
			mutex_unlock(&dmxdev->mutex);
			return -EBUSY;
		}
		mem = vmalloc(DVR_BUFFER_SIZE);
159
		if (!mem) {
160 161 162
			mutex_unlock(&dmxdev->mutex);
			return -ENOMEM;
		}
163
		dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
164
		dvbdev->readers--;
L
Linus Torvalds 已提交
165 166
	}

167 168
	if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
		dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
L
Linus Torvalds 已提交
169 170

		if (!dmxdev->demux->write) {
171
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
172 173 174
			return -EOPNOTSUPP;
		}

175
		front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
L
Linus Torvalds 已提交
176 177

		if (!front) {
178
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
179 180 181 182 183
			return -EINVAL;
		}
		dmxdev->demux->disconnect_frontend(dmxdev->demux);
		dmxdev->demux->connect_frontend(dmxdev->demux, front);
	}
184
	dvbdev->users++;
185
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
186 187 188 189 190
	return 0;
}

static int dvb_dvr_release(struct inode *inode, struct file *file)
{
191 192
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
193

194
	mutex_lock(&dmxdev->mutex);
L
Linus Torvalds 已提交
195

196
	if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
L
Linus Torvalds 已提交
197 198 199 200
		dmxdev->demux->disconnect_frontend(dmxdev->demux);
		dmxdev->demux->connect_frontend(dmxdev->demux,
						dmxdev->dvr_orig_fe);
	}
201
	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
202
		dvbdev->readers++;
L
Linus Torvalds 已提交
203
		if (dmxdev->dvr_buffer.data) {
204
			void *mem = dmxdev->dvr_buffer.data;
L
Linus Torvalds 已提交
205 206
			mb();
			spin_lock_irq(&dmxdev->lock);
207
			dmxdev->dvr_buffer.data = NULL;
L
Linus Torvalds 已提交
208 209 210 211
			spin_unlock_irq(&dmxdev->lock);
			vfree(mem);
		}
	}
212 213
	/* TODO */
	dvbdev->users--;
214
	if (dvbdev->users == 1 && dmxdev->exit == 1) {
215 216 217 218 219
		mutex_unlock(&dmxdev->mutex);
		wake_up(&dvbdev->wait_queue);
	} else
		mutex_unlock(&dmxdev->mutex);

L
Linus Torvalds 已提交
220 221 222 223
	return 0;
}

static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
224
			     size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
225
{
226 227
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
228 229 230 231
	int ret;

	if (!dmxdev->demux->write)
		return -EOPNOTSUPP;
232
	if ((file->f_flags & O_ACCMODE) != O_WRONLY)
L
Linus Torvalds 已提交
233
		return -EINVAL;
234
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
235
		return -ERESTARTSYS;
236 237 238 239 240

	if (dmxdev->exit) {
		mutex_unlock(&dmxdev->mutex);
		return -ENODEV;
	}
241
	ret = dmxdev->demux->write(dmxdev->demux, buf, count);
242
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
243 244 245 246
	return ret;
}

static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
247
			    loff_t *ppos)
L
Linus Torvalds 已提交
248
{
249 250
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
251

252
	if (dmxdev->exit)
253 254
		return -ENODEV;

255 256 257
	return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
				      file->f_flags & O_NONBLOCK,
				      buf, count, ppos);
L
Linus Torvalds 已提交
258 259
}

260 261 262 263 264 265 266
static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
				      unsigned long size)
{
	struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
	void *newmem;
	void *oldmem;

267
	dprintk("%s\n", __func__);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

	if (buf->size == size)
		return 0;
	if (!size)
		return -EINVAL;

	newmem = vmalloc(size);
	if (!newmem)
		return -ENOMEM;

	oldmem = buf->data;

	spin_lock_irq(&dmxdev->lock);
	buf->data = newmem;
	buf->size = size;

	/* reset and not flush in case the buffer shrinks */
	dvb_ringbuffer_reset(buf);
	spin_unlock_irq(&dmxdev->lock);

	vfree(oldmem);

	return 0;
}

293 294
static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
					       *dmxdevfilter, int state)
L
Linus Torvalds 已提交
295 296
{
	spin_lock_irq(&dmxdevfilter->dev->lock);
297
	dmxdevfilter->state = state;
L
Linus Torvalds 已提交
298 299 300
	spin_unlock_irq(&dmxdevfilter->dev->lock);
}

301 302
static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
				      unsigned long size)
L
Linus Torvalds 已提交
303
{
304
	struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
305 306
	void *newmem;
	void *oldmem;
L
Linus Torvalds 已提交
307

308
	if (buf->size == size)
L
Linus Torvalds 已提交
309
		return 0;
310 311
	if (!size)
		return -EINVAL;
312
	if (dmxdevfilter->state >= DMXDEV_STATE_GO)
L
Linus Torvalds 已提交
313
		return -EBUSY;
314 315 316 317 318 319 320

	newmem = vmalloc(size);
	if (!newmem)
		return -ENOMEM;

	oldmem = buf->data;

L
Linus Torvalds 已提交
321
	spin_lock_irq(&dmxdevfilter->dev->lock);
322
	buf->data = newmem;
323
	buf->size = size;
324 325 326

	/* reset and not flush in case the buffer shrinks */
	dvb_ringbuffer_reset(buf);
L
Linus Torvalds 已提交
327 328
	spin_unlock_irq(&dmxdevfilter->dev->lock);

329 330
	vfree(oldmem);

L
Linus Torvalds 已提交
331 332 333 334 335
	return 0;
}

static void dvb_dmxdev_filter_timeout(unsigned long data)
{
336
	struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
L
Linus Torvalds 已提交
337

338
	dmxdevfilter->buffer.error = -ETIMEDOUT;
L
Linus Torvalds 已提交
339
	spin_lock_irq(&dmxdevfilter->dev->lock);
340
	dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
L
Linus Torvalds 已提交
341 342 343 344 345 346
	spin_unlock_irq(&dmxdevfilter->dev->lock);
	wake_up(&dmxdevfilter->buffer.queue);
}

static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
{
347
	struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
L
Linus Torvalds 已提交
348 349 350

	del_timer(&dmxdevfilter->timer);
	if (para->timeout) {
351 352 353 354
		dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
		dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
		dmxdevfilter->timer.expires =
		    jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
L
Linus Torvalds 已提交
355 356 357 358 359
		add_timer(&dmxdevfilter->timer);
	}
}

static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
360
				       const u8 *buffer2, size_t buffer2_len,
361
				       struct dmx_section_filter *filter)
L
Linus Torvalds 已提交
362
{
363
	struct dmxdev_filter *dmxdevfilter = filter->priv;
L
Linus Torvalds 已提交
364 365 366 367 368 369
	int ret;

	if (dmxdevfilter->buffer.error) {
		wake_up(&dmxdevfilter->buffer.queue);
		return 0;
	}
370
	spin_lock(&dmxdevfilter->dev->lock);
371
	if (dmxdevfilter->state != DMXDEV_STATE_GO) {
372
		spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
373 374 375
		return 0;
	}
	del_timer(&dmxdevfilter->timer);
376
	dprintk("section callback %*ph\n", 6, buffer1);
377 378 379 380 381
	ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
				      buffer1_len);
	if (ret == buffer1_len) {
		ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
					      buffer2_len);
L
Linus Torvalds 已提交
382
	}
383
	if (ret < 0)
384
		dmxdevfilter->buffer.error = ret;
385 386
	if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
		dmxdevfilter->state = DMXDEV_STATE_DONE;
387
	spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
388 389 390 391 392
	wake_up(&dmxdevfilter->buffer.queue);
	return 0;
}

static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
393
				  const u8 *buffer2, size_t buffer2_len,
394
				  struct dmx_ts_feed *feed)
L
Linus Torvalds 已提交
395
{
396
	struct dmxdev_filter *dmxdevfilter = feed->priv;
397
	struct dvb_ringbuffer *buffer;
L
Linus Torvalds 已提交
398 399
	int ret;

400
	spin_lock(&dmxdevfilter->dev->lock);
401
	if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
402
		spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
403 404 405
		return 0;
	}

406 407
	if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
	    || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
408
		buffer = &dmxdevfilter->buffer;
L
Linus Torvalds 已提交
409
	else
410
		buffer = &dmxdevfilter->dev->dvr_buffer;
L
Linus Torvalds 已提交
411
	if (buffer->error) {
412
		spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
413 414 415
		wake_up(&buffer->queue);
		return 0;
	}
416 417 418
	ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
	if (ret == buffer1_len)
		ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
419
	if (ret < 0)
420
		buffer->error = ret;
421
	spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
422 423 424 425 426 427 428
	wake_up(&buffer->queue);
	return 0;
}

/* stop feed but only mark the specified filter as stopped (state set) */
static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
{
429 430
	struct dmxdev_feed *feed;

L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);

	switch (dmxdevfilter->type) {
	case DMXDEV_TYPE_SEC:
		del_timer(&dmxdevfilter->timer);
		dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
		break;
	case DMXDEV_TYPE_PES:
439 440
		list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
			feed->ts->stop_filtering(feed->ts);
L
Linus Torvalds 已提交
441 442 443 444 445 446 447 448 449 450
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/* start feed associated with the specified filter */
static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
{
451 452 453
	struct dmxdev_feed *feed;
	int ret;

454
	dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
L
Linus Torvalds 已提交
455 456 457 458 459

	switch (filter->type) {
	case DMXDEV_TYPE_SEC:
		return filter->feed.sec->start_filtering(filter->feed.sec);
	case DMXDEV_TYPE_PES:
460 461 462 463 464 465 466 467
		list_for_each_entry(feed, &filter->feed.ts, next) {
			ret = feed->ts->start_filtering(feed->ts);
			if (ret < 0) {
				dvb_dmxdev_feed_stop(filter);
				return ret;
			}
		}
		break;
L
Linus Torvalds 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
	default:
		return -EINVAL;
	}

	return 0;
}

/* restart section feed if it has filters left associated with it,
   otherwise release the feed */
static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
{
	int i;
	struct dmxdev *dmxdev = filter->dev;
	u16 pid = filter->params.sec.pid;

483 484 485 486
	for (i = 0; i < dmxdev->filternum; i++)
		if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
		    dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
		    dmxdev->filter[i].params.sec.pid == pid) {
L
Linus Torvalds 已提交
487 488 489 490
			dvb_dmxdev_feed_start(&dmxdev->filter[i]);
			return 0;
		}

491 492
	filter->dev->demux->release_section_feed(dmxdev->demux,
						 filter->feed.sec);
L
Linus Torvalds 已提交
493 494 495 496 497 498

	return 0;
}

static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
{
499 500 501
	struct dmxdev_feed *feed;
	struct dmx_demux *demux;

502
	if (dmxdevfilter->state < DMXDEV_STATE_GO)
L
Linus Torvalds 已提交
503 504 505 506 507 508 509 510 511
		return 0;

	switch (dmxdevfilter->type) {
	case DMXDEV_TYPE_SEC:
		if (!dmxdevfilter->feed.sec)
			break;
		dvb_dmxdev_feed_stop(dmxdevfilter);
		if (dmxdevfilter->filter.sec)
			dmxdevfilter->feed.sec->
512 513
			    release_filter(dmxdevfilter->feed.sec,
					   dmxdevfilter->filter.sec);
L
Linus Torvalds 已提交
514
		dvb_dmxdev_feed_restart(dmxdevfilter);
515
		dmxdevfilter->feed.sec = NULL;
L
Linus Torvalds 已提交
516 517 518
		break;
	case DMXDEV_TYPE_PES:
		dvb_dmxdev_feed_stop(dmxdevfilter);
519 520 521 522 523
		demux = dmxdevfilter->dev->demux;
		list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
			demux->release_ts_feed(demux, feed->ts);
			feed->ts = NULL;
		}
L
Linus Torvalds 已提交
524 525
		break;
	default:
526
		if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
L
Linus Torvalds 已提交
527 528 529
			return 0;
		return -EINVAL;
	}
530 531

	dvb_ringbuffer_flush(&dmxdevfilter->buffer);
L
Linus Torvalds 已提交
532 533 534
	return 0;
}

535 536 537 538 539 540 541 542 543 544 545 546 547
static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
{
	struct dmxdev_feed *feed, *tmp;

	/* delete all PIDs */
	list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
		list_del(&feed->next);
		kfree(feed);
	}

	BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
}

L
Linus Torvalds 已提交
548 549
static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
{
550
	if (dmxdevfilter->state < DMXDEV_STATE_SET)
L
Linus Torvalds 已提交
551 552
		return 0;

553 554 555
	if (dmxdevfilter->type == DMXDEV_TYPE_PES)
		dvb_dmxdev_delete_pids(dmxdevfilter);

556
	dmxdevfilter->type = DMXDEV_TYPE_NONE;
L
Linus Torvalds 已提交
557 558 559 560
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
	return 0;
}

561 562 563 564
static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
				 struct dmxdev_filter *filter,
				 struct dmxdev_feed *feed)
{
565
	ktime_t timeout = ktime_set(0, 0);
566 567 568 569
	struct dmx_pes_filter_params *para = &filter->params.pes;
	dmx_output_t otype;
	int ret;
	int ts_type;
570
	enum dmx_ts_pes ts_pes;
571 572 573 574 575
	struct dmx_ts_feed *tsfeed;

	feed->ts = NULL;
	otype = para->output;

576
	ts_pes = para->pes_type;
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

	if (ts_pes < DMX_PES_OTHER)
		ts_type = TS_DECODER;
	else
		ts_type = 0;

	if (otype == DMX_OUT_TS_TAP)
		ts_type |= TS_PACKET;
	else if (otype == DMX_OUT_TSDEMUX_TAP)
		ts_type |= TS_PACKET | TS_DEMUX;
	else if (otype == DMX_OUT_TAP)
		ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;

	ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
					      dvb_dmxdev_ts_callback);
	if (ret < 0)
		return ret;

	tsfeed = feed->ts;
	tsfeed->priv = filter;

	ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
	if (ret < 0) {
		dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
		return ret;
	}

	ret = tsfeed->start_filtering(tsfeed);
	if (ret < 0) {
		dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
		return ret;
	}

	return 0;
}

L
Linus Torvalds 已提交
613 614 615
static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
{
	struct dmxdev *dmxdev = filter->dev;
616
	struct dmxdev_feed *feed;
L
Linus Torvalds 已提交
617 618 619 620 621 622 623 624 625
	void *mem;
	int ret, i;

	if (filter->state < DMXDEV_STATE_SET)
		return -EINVAL;

	if (filter->state >= DMXDEV_STATE_GO)
		dvb_dmxdev_filter_stop(filter);

626
	if (!filter->buffer.data) {
L
Linus Torvalds 已提交
627
		mem = vmalloc(filter->buffer.size);
628 629
		if (!mem)
			return -ENOMEM;
L
Linus Torvalds 已提交
630
		spin_lock_irq(&filter->dev->lock);
631
		filter->buffer.data = mem;
L
Linus Torvalds 已提交
632 633 634
		spin_unlock_irq(&filter->dev->lock);
	}

635
	dvb_ringbuffer_flush(&filter->buffer);
L
Linus Torvalds 已提交
636 637 638 639

	switch (filter->type) {
	case DMXDEV_TYPE_SEC:
	{
640 641 642 643 644 645
		struct dmx_sct_filter_params *para = &filter->params.sec;
		struct dmx_section_filter **secfilter = &filter->filter.sec;
		struct dmx_section_feed **secfeed = &filter->feed.sec;

		*secfilter = NULL;
		*secfeed = NULL;
L
Linus Torvalds 已提交
646 647 648


		/* find active filter/feed with same PID */
649
		for (i = 0; i < dmxdev->filternum; i++) {
L
Linus Torvalds 已提交
650
			if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
651 652
			    dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
			    dmxdev->filter[i].params.sec.pid == para->pid) {
L
Linus Torvalds 已提交
653 654 655 656 657 658 659
				*secfeed = dmxdev->filter[i].feed.sec;
				break;
			}
		}

		/* if no feed found, try to allocate new one */
		if (!*secfeed) {
660 661 662 663
			ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
								   secfeed,
								   dvb_dmxdev_section_callback);
			if (ret < 0) {
664
				pr_err("DVB (%s): could not alloc feed\n",
665
				       __func__);
L
Linus Torvalds 已提交
666 667 668
				return ret;
			}

669 670 671
			ret = (*secfeed)->set(*secfeed, para->pid, 32768,
					      (para->flags & DMX_CHECK_CRC) ? 1 : 0);
			if (ret < 0) {
672
				pr_err("DVB (%s): could not set feed\n",
673
				       __func__);
L
Linus Torvalds 已提交
674 675 676 677 678 679 680
				dvb_dmxdev_feed_restart(filter);
				return ret;
			}
		} else {
			dvb_dmxdev_feed_stop(filter);
		}

681
		ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
L
Linus Torvalds 已提交
682 683 684
		if (ret < 0) {
			dvb_dmxdev_feed_restart(filter);
			filter->feed.sec->start_filtering(*secfeed);
685
			dprintk("could not get filter\n");
L
Linus Torvalds 已提交
686 687 688 689 690 691
			return ret;
		}

		(*secfilter)->priv = filter;

		memcpy(&((*secfilter)->filter_value[3]),
692
		       &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
693
		memcpy(&(*secfilter)->filter_mask[3],
694
		       &para->filter.mask[1], DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
695
		memcpy(&(*secfilter)->filter_mode[3],
696
		       &para->filter.mode[1], DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
697

698 699 700 701 702
		(*secfilter)->filter_value[0] = para->filter.filter[0];
		(*secfilter)->filter_mask[0] = para->filter.mask[0];
		(*secfilter)->filter_mode[0] = para->filter.mode[0];
		(*secfilter)->filter_mask[1] = 0;
		(*secfilter)->filter_mask[2] = 0;
L
Linus Torvalds 已提交
703 704 705

		filter->todo = 0;

706
		ret = filter->feed.sec->start_filtering(filter->feed.sec);
L
Linus Torvalds 已提交
707 708 709 710 711 712 713
		if (ret < 0)
			return ret;

		dvb_dmxdev_filter_timer(filter);
		break;
	}
	case DMXDEV_TYPE_PES:
714 715 716 717 718 719
		list_for_each_entry(feed, &filter->feed.ts, next) {
			ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
			if (ret < 0) {
				dvb_dmxdev_filter_stop(filter);
				return ret;
			}
720
		}
L
Linus Torvalds 已提交
721 722 723 724 725 726 727 728 729 730 731
		break;
	default:
		return -EINVAL;
	}

	dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
	return 0;
}

static int dvb_demux_open(struct inode *inode, struct file *file)
{
732 733
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
734 735 736 737 738 739
	int i;
	struct dmxdev_filter *dmxdevfilter;

	if (!dmxdev->filter)
		return -EINVAL;

740
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
741 742
		return -ERESTARTSYS;

743 744
	for (i = 0; i < dmxdev->filternum; i++)
		if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
L
Linus Torvalds 已提交
745 746
			break;

747
	if (i == dmxdev->filternum) {
748
		mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
749 750 751
		return -EMFILE;
	}

752
	dmxdevfilter = &dmxdev->filter[i];
753
	mutex_init(&dmxdevfilter->mutex);
754
	file->private_data = dmxdevfilter;
L
Linus Torvalds 已提交
755

756
	dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
757
	dmxdevfilter->type = DMXDEV_TYPE_NONE;
L
Linus Torvalds 已提交
758 759 760
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
	init_timer(&dmxdevfilter->timer);

761 762
	dvbdev->users++;

763
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
764 765 766
	return 0;
}

767 768
static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
				  struct dmxdev_filter *dmxdevfilter)
L
Linus Torvalds 已提交
769
{
770 771
	mutex_lock(&dmxdev->mutex);
	mutex_lock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
772 773 774 775 776

	dvb_dmxdev_filter_stop(dmxdevfilter);
	dvb_dmxdev_filter_reset(dmxdevfilter);

	if (dmxdevfilter->buffer.data) {
777
		void *mem = dmxdevfilter->buffer.data;
L
Linus Torvalds 已提交
778 779

		spin_lock_irq(&dmxdev->lock);
780
		dmxdevfilter->buffer.data = NULL;
L
Linus Torvalds 已提交
781 782 783 784 785 786
		spin_unlock_irq(&dmxdev->lock);
		vfree(mem);
	}

	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
	wake_up(&dmxdevfilter->buffer.queue);
787 788
	mutex_unlock(&dmxdevfilter->mutex);
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
789 790 791 792 793 794 795
	return 0;
}

static inline void invert_mode(dmx_filter_t *filter)
{
	int i;

796 797
	for (i = 0; i < DMX_FILTER_SIZE; i++)
		filter->mode[i] ^= 0xff;
L
Linus Torvalds 已提交
798 799
}

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
			      struct dmxdev_filter *filter, u16 pid)
{
	struct dmxdev_feed *feed;

	if ((filter->type != DMXDEV_TYPE_PES) ||
	    (filter->state < DMXDEV_STATE_SET))
		return -EINVAL;

	/* only TS packet filters may have multiple PIDs */
	if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
	    (!list_empty(&filter->feed.ts)))
		return -EINVAL;

	feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
	if (feed == NULL)
		return -ENOMEM;

	feed->pid = pid;
	list_add(&feed->next, &filter->feed.ts);

	if (filter->state >= DMXDEV_STATE_GO)
		return dvb_dmxdev_start_feed(dmxdev, filter, feed);

	return 0;
}

static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
				  struct dmxdev_filter *filter, u16 pid)
{
	struct dmxdev_feed *feed, *tmp;

	if ((filter->type != DMXDEV_TYPE_PES) ||
	    (filter->state < DMXDEV_STATE_SET))
		return -EINVAL;

	list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
		if ((feed->pid == pid) && (feed->ts != NULL)) {
			feed->ts->stop_filtering(feed->ts);
			filter->dev->demux->release_ts_feed(filter->dev->demux,
							    feed->ts);
			list_del(&feed->next);
			kfree(feed);
		}
	}

	return 0;
}

L
Linus Torvalds 已提交
849
static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
850 851
				 struct dmxdev_filter *dmxdevfilter,
				 struct dmx_sct_filter_params *params)
L
Linus Torvalds 已提交
852
{
853
	dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
854
		__func__, params->pid, params->flags, params->timeout);
L
Linus Torvalds 已提交
855 856 857

	dvb_dmxdev_filter_stop(dmxdevfilter);

858
	dmxdevfilter->type = DMXDEV_TYPE_SEC;
L
Linus Torvalds 已提交
859 860 861 862 863
	memcpy(&dmxdevfilter->params.sec,
	       params, sizeof(struct dmx_sct_filter_params));
	invert_mode(&dmxdevfilter->params.sec.filter);
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);

864
	if (params->flags & DMX_IMMEDIATE_START)
L
Linus Torvalds 已提交
865 866 867 868 869 870
		return dvb_dmxdev_filter_start(dmxdevfilter);

	return 0;
}

static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
871 872
				     struct dmxdev_filter *dmxdevfilter,
				     struct dmx_pes_filter_params *params)
L
Linus Torvalds 已提交
873
{
874 875
	int ret;

L
Linus Torvalds 已提交
876
	dvb_dmxdev_filter_stop(dmxdevfilter);
877
	dvb_dmxdev_filter_reset(dmxdevfilter);
L
Linus Torvalds 已提交
878

879
	if ((unsigned)params->pes_type > DMX_PES_OTHER)
L
Linus Torvalds 已提交
880 881
		return -EINVAL;

882 883 884
	dmxdevfilter->type = DMXDEV_TYPE_PES;
	memcpy(&dmxdevfilter->params, params,
	       sizeof(struct dmx_pes_filter_params));
885
	INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
L
Linus Torvalds 已提交
886 887 888

	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);

889 890 891 892 893
	ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
				 dmxdevfilter->params.pes.pid);
	if (ret < 0)
		return ret;

894
	if (params->flags & DMX_IMMEDIATE_START)
L
Linus Torvalds 已提交
895 896 897 898 899 900
		return dvb_dmxdev_filter_start(dmxdevfilter);

	return 0;
}

static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
901 902
				   struct file *file, char __user *buf,
				   size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
903 904
{
	int result, hcount;
905 906 907 908 909 910 911 912 913 914 915
	int done = 0;

	if (dfil->todo <= 0) {
		hcount = 3 + dfil->todo;
		if (hcount > count)
			hcount = count;
		result = dvb_dmxdev_buffer_read(&dfil->buffer,
						file->f_flags & O_NONBLOCK,
						buf, hcount, ppos);
		if (result < 0) {
			dfil->todo = 0;
L
Linus Torvalds 已提交
916 917
			return result;
		}
918
		if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
L
Linus Torvalds 已提交
919
			return -EFAULT;
920 921 922 923 924
		buf += result;
		done = result;
		count -= result;
		dfil->todo -= result;
		if (dfil->todo > -3)
L
Linus Torvalds 已提交
925
			return done;
926
		dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
L
Linus Torvalds 已提交
927 928 929
		if (!count)
			return done;
	}
930 931 932 933 934 935
	if (count > dfil->todo)
		count = dfil->todo;
	result = dvb_dmxdev_buffer_read(&dfil->buffer,
					file->f_flags & O_NONBLOCK,
					buf, count, ppos);
	if (result < 0)
L
Linus Torvalds 已提交
936
		return result;
937 938
	dfil->todo -= result;
	return (result + done);
L
Linus Torvalds 已提交
939 940 941
}

static ssize_t
942 943
dvb_demux_read(struct file *file, char __user *buf, size_t count,
	       loff_t *ppos)
L
Linus Torvalds 已提交
944
{
945 946
	struct dmxdev_filter *dmxdevfilter = file->private_data;
	int ret;
L
Linus Torvalds 已提交
947

948
	if (mutex_lock_interruptible(&dmxdevfilter->mutex))
L
Linus Torvalds 已提交
949 950
		return -ERESTARTSYS;

951 952
	if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
		ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
L
Linus Torvalds 已提交
953
	else
954 955 956
		ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
					     file->f_flags & O_NONBLOCK,
					     buf, count, ppos);
L
Linus Torvalds 已提交
957

958
	mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
959 960 961
	return ret;
}

962
static int dvb_demux_do_ioctl(struct file *file,
L
Linus Torvalds 已提交
963 964
			      unsigned int cmd, void *parg)
{
965
	struct dmxdev_filter *dmxdevfilter = file->private_data;
966 967 968
	struct dmxdev *dmxdev = dmxdevfilter->dev;
	unsigned long arg = (unsigned long)parg;
	int ret = 0;
L
Linus Torvalds 已提交
969

970
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
971 972 973 974
		return -ERESTARTSYS;

	switch (cmd) {
	case DMX_START:
975 976
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
977 978
			return -ERESTARTSYS;
		}
979
		if (dmxdevfilter->state < DMXDEV_STATE_SET)
L
Linus Torvalds 已提交
980 981 982
			ret = -EINVAL;
		else
			ret = dvb_dmxdev_filter_start(dmxdevfilter);
983
		mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
984 985 986
		break;

	case DMX_STOP:
987 988
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
989 990
			return -ERESTARTSYS;
		}
991
		ret = dvb_dmxdev_filter_stop(dmxdevfilter);
992
		mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
993 994 995
		break;

	case DMX_SET_FILTER:
996 997
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
998 999
			return -ERESTARTSYS;
		}
1000
		ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1001
		mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
1002 1003 1004
		break;

	case DMX_SET_PES_FILTER:
1005 1006
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1007 1008
			return -ERESTARTSYS;
		}
1009
		ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1010
		mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
1011 1012 1013
		break;

	case DMX_SET_BUFFER_SIZE:
1014 1015
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1016 1017
			return -ERESTARTSYS;
		}
1018
		ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1019
		mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
1020 1021 1022 1023
		break;

	case DMX_GET_PES_PIDS:
		if (!dmxdev->demux->get_pes_pids) {
1024
			ret = -EINVAL;
L
Linus Torvalds 已提交
1025 1026
			break;
		}
1027
		dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
L
Linus Torvalds 已提交
1028 1029
		break;

1030 1031 1032
#if 0
	/* Not used upstream and never documented */

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	case DMX_GET_CAPS:
		if (!dmxdev->demux->get_caps) {
			ret = -EINVAL;
			break;
		}
		ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
		break;

	case DMX_SET_SOURCE:
		if (!dmxdev->demux->set_source) {
			ret = -EINVAL;
			break;
		}
		ret = dmxdev->demux->set_source(dmxdev->demux, parg);
		break;
1048
#endif
1049

L
Linus Torvalds 已提交
1050 1051
	case DMX_GET_STC:
		if (!dmxdev->demux->get_stc) {
1052
			ret = -EINVAL;
L
Linus Torvalds 已提交
1053 1054 1055
			break;
		}
		ret = dmxdev->demux->get_stc(dmxdev->demux,
1056 1057 1058
					     ((struct dmx_stc *)parg)->num,
					     &((struct dmx_stc *)parg)->stc,
					     &((struct dmx_stc *)parg)->base);
L
Linus Torvalds 已提交
1059 1060
		break;

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
	case DMX_ADD_PID:
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			ret = -ERESTARTSYS;
			break;
		}
		ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
		mutex_unlock(&dmxdevfilter->mutex);
		break;

	case DMX_REMOVE_PID:
		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
			ret = -ERESTARTSYS;
			break;
		}
		ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
		mutex_unlock(&dmxdevfilter->mutex);
		break;

L
Linus Torvalds 已提交
1079
	default:
1080 1081
		ret = -EINVAL;
		break;
L
Linus Torvalds 已提交
1082
	}
1083
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1084 1085 1086
	return ret;
}

1087 1088
static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
			    unsigned long arg)
L
Linus Torvalds 已提交
1089
{
A
Arnd Bergmann 已提交
1090
	return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
L
Linus Torvalds 已提交
1091 1092
}

1093
static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
1094
{
1095
	struct dmxdev_filter *dmxdevfilter = file->private_data;
L
Linus Torvalds 已提交
1096 1097
	unsigned int mask = 0;

1098
	if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
1099
		return POLLERR;
L
Linus Torvalds 已提交
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

	poll_wait(file, &dmxdevfilter->buffer.queue, wait);

	if (dmxdevfilter->state != DMXDEV_STATE_GO &&
	    dmxdevfilter->state != DMXDEV_STATE_DONE &&
	    dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
		return 0;

	if (dmxdevfilter->buffer.error)
		mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);

1111
	if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118
		mask |= (POLLIN | POLLRDNORM | POLLPRI);

	return mask;
}

static int dvb_demux_release(struct inode *inode, struct file *file)
{
1119
	struct dmxdev_filter *dmxdevfilter = file->private_data;
L
Linus Torvalds 已提交
1120 1121
	struct dmxdev *dmxdev = dmxdevfilter->dev;

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	int ret;

	ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);

	mutex_lock(&dmxdev->mutex);
	dmxdev->dvbdev->users--;
	if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
		mutex_unlock(&dmxdev->mutex);
		wake_up(&dmxdev->dvbdev->wait_queue);
	} else
		mutex_unlock(&dmxdev->mutex);

	return ret;
L
Linus Torvalds 已提交
1135 1136
}

1137
static const struct file_operations dvb_demux_fops = {
1138 1139
	.owner = THIS_MODULE,
	.read = dvb_demux_read,
1140
	.unlocked_ioctl = dvb_demux_ioctl,
1141 1142 1143
	.open = dvb_demux_open,
	.release = dvb_demux_release,
	.poll = dvb_demux_poll,
1144
	.llseek = default_llseek,
L
Linus Torvalds 已提交
1145 1146
};

1147
static const struct dvb_device dvbdev_demux = {
1148 1149 1150
	.priv = NULL,
	.users = 1,
	.writers = 1,
1151
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1152
	.name = "dvb-demux",
1153
#endif
1154
	.fops = &dvb_demux_fops
L
Linus Torvalds 已提交
1155 1156
};

1157
static int dvb_dvr_do_ioctl(struct file *file,
1158
			    unsigned int cmd, void *parg)
L
Linus Torvalds 已提交
1159
{
1160 1161
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
1162
	unsigned long arg = (unsigned long)parg;
1163
	int ret;
L
Linus Torvalds 已提交
1164

1165
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
1166 1167 1168 1169
		return -ERESTARTSYS;

	switch (cmd) {
	case DMX_SET_BUFFER_SIZE:
1170
		ret = dvb_dvr_set_buffer_size(dmxdev, arg);
L
Linus Torvalds 已提交
1171 1172 1173
		break;

	default:
1174 1175
		ret = -EINVAL;
		break;
L
Linus Torvalds 已提交
1176
	}
1177
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1178 1179 1180
	return ret;
}

1181
static long dvb_dvr_ioctl(struct file *file,
1182
			 unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1183
{
A
Arnd Bergmann 已提交
1184
	return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
L
Linus Torvalds 已提交
1185 1186
}

1187
static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
1188
{
1189 1190
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
1191 1192
	unsigned int mask = 0;

1193
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1194

1195 1196 1197
	if (dmxdev->exit)
		return POLLERR;

L
Linus Torvalds 已提交
1198 1199
	poll_wait(file, &dmxdev->dvr_buffer.queue, wait);

1200
	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
L
Linus Torvalds 已提交
1201 1202 1203
		if (dmxdev->dvr_buffer.error)
			mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);

1204
		if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211
			mask |= (POLLIN | POLLRDNORM | POLLPRI);
	} else
		mask |= (POLLOUT | POLLWRNORM | POLLPRI);

	return mask;
}

1212
static const struct file_operations dvb_dvr_fops = {
1213 1214 1215
	.owner = THIS_MODULE,
	.read = dvb_dvr_read,
	.write = dvb_dvr_write,
1216
	.unlocked_ioctl = dvb_dvr_ioctl,
1217 1218 1219
	.open = dvb_dvr_open,
	.release = dvb_dvr_release,
	.poll = dvb_dvr_poll,
1220
	.llseek = default_llseek,
L
Linus Torvalds 已提交
1221 1222
};

1223
static const struct dvb_device dvbdev_dvr = {
1224
	.priv = NULL,
1225
	.readers = 1,
1226
	.users = 1,
1227
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1228
	.name = "dvb-dvr",
1229
#endif
1230
	.fops = &dvb_dvr_fops
L
Linus Torvalds 已提交
1231
};
1232
int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
L
Linus Torvalds 已提交
1233 1234 1235 1236 1237 1238
{
	int i;

	if (dmxdev->demux->open(dmxdev->demux) < 0)
		return -EUSERS;

1239
	dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
L
Linus Torvalds 已提交
1240 1241 1242
	if (!dmxdev->filter)
		return -ENOMEM;

1243
	mutex_init(&dmxdev->mutex);
L
Linus Torvalds 已提交
1244
	spin_lock_init(&dmxdev->lock);
1245 1246 1247 1248 1249
	for (i = 0; i < dmxdev->filternum; i++) {
		dmxdev->filter[i].dev = dmxdev;
		dmxdev->filter[i].buffer.data = NULL;
		dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
					    DMXDEV_STATE_FREE);
L
Linus Torvalds 已提交
1250 1251
	}

1252
	dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1253
			    DVB_DEVICE_DEMUX, dmxdev->filternum);
1254
	dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1255
			    dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
L
Linus Torvalds 已提交
1256

1257
	dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
L
Linus Torvalds 已提交
1258 1259 1260

	return 0;
}
1261

L
Linus Torvalds 已提交
1262 1263
EXPORT_SYMBOL(dvb_dmxdev_init);

1264
void dvb_dmxdev_release(struct dmxdev *dmxdev)
L
Linus Torvalds 已提交
1265
{
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	dmxdev->exit=1;
	if (dmxdev->dvbdev->users > 1) {
		wait_event(dmxdev->dvbdev->wait_queue,
				dmxdev->dvbdev->users==1);
	}
	if (dmxdev->dvr_dvbdev->users > 1) {
		wait_event(dmxdev->dvr_dvbdev->wait_queue,
				dmxdev->dvr_dvbdev->users==1);
	}

L
Linus Torvalds 已提交
1276 1277 1278 1279
	dvb_unregister_device(dmxdev->dvbdev);
	dvb_unregister_device(dmxdev->dvr_dvbdev);

	vfree(dmxdev->filter);
1280
	dmxdev->filter = NULL;
L
Linus Torvalds 已提交
1281 1282
	dmxdev->demux->close(dmxdev->demux);
}
1283

L
Linus Torvalds 已提交
1284
EXPORT_SYMBOL(dvb_dmxdev_release);