dmxdev.c 29.8 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
 *
 * 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.
 *
 */

19 20
#define pr_fmt(fmt) "dmxdev: " fmt

21
#include <linux/sched.h>
L
Linus Torvalds 已提交
22 23 24 25 26 27 28
#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>
29
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
30 31 32 33 34 35 36
#include "dmxdev.h"

static int debug;

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

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

43 44
static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
				   const u8 *src, size_t len)
L
Linus Torvalds 已提交
45
{
46
	ssize_t free;
L
Linus Torvalds 已提交
47 48 49 50 51 52

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

53 54
	free = dvb_ringbuffer_free(buf);
	if (len > free) {
55
		dprintk("buffer overflow\n");
56
		return -EOVERFLOW;
L
Linus Torvalds 已提交
57
	}
58 59

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

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

	if (!src->data)
		return 0;

73 74 75 76
	if (src->error) {
		ret = src->error;
		dvb_ringbuffer_flush(src);
		return ret;
L
Linus Torvalds 已提交
77 78
	}

79 80 81 82 83
	for (todo = count; todo > 0; todo -= ret) {
		if (non_blocking && dvb_ringbuffer_empty(src)) {
			ret = -EWOULDBLOCK;
			break;
		}
L
Linus Torvalds 已提交
84

85 86 87 88 89
		ret = wait_event_interruptible(src->queue,
					       !dvb_ringbuffer_empty(src) ||
					       (src->error != 0));
		if (ret < 0)
			break;
L
Linus Torvalds 已提交
90

91 92 93 94
		if (src->error) {
			ret = src->error;
			dvb_ringbuffer_flush(src);
			break;
L
Linus Torvalds 已提交
95 96
		}

97
		avail = dvb_ringbuffer_avail(src);
98 99
		if (avail > todo)
			avail = todo;
100

101
		ret = dvb_ringbuffer_read_user(src, buf, avail);
102 103 104 105
		if (ret < 0)
			break;

		buf += ret;
L
Linus Torvalds 已提交
106
	}
107 108

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

111
static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
L
Linus Torvalds 已提交
112 113 114
{
	struct list_head *head, *pos;

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

	return NULL;
}

static int dvb_dvr_open(struct inode *inode, struct file *file)
{
127 128
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
129 130
	struct dmx_frontend *front;

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

133
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
134 135
		return -ERESTARTSYS;

136 137 138 139 140
	if (dmxdev->exit) {
		mutex_unlock(&dmxdev->mutex);
		return -ENODEV;
	}

141 142
	if ((file->f_flags & O_ACCMODE) == O_RDWR) {
		if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
143
			mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
144 145 146 147
			return -EOPNOTSUPP;
		}
	}

148
	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
149
		void *mem;
150

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

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

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

172
		front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
L
Linus Torvalds 已提交
173 174

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

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

191
	mutex_lock(&dmxdev->mutex);
L
Linus Torvalds 已提交
192

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

L
Linus Torvalds 已提交
218 219 220 221
	return 0;
}

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

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

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

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

250
	if (dmxdev->exit)
251 252
		return -ENODEV;

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

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

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

	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;
}

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

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

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

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

	oldmem = buf->data;

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

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

327 328
	vfree(oldmem);

L
Linus Torvalds 已提交
329 330 331
	return 0;
}

332
static void dvb_dmxdev_filter_timeout(struct timer_list *t)
L
Linus Torvalds 已提交
333
{
334
	struct dmxdev_filter *dmxdevfilter = from_timer(dmxdevfilter, t, timer);
L
Linus Torvalds 已提交
335

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

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

	del_timer(&dmxdevfilter->timer);
	if (para->timeout) {
349 350
		dmxdevfilter->timer.expires =
		    jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
L
Linus Torvalds 已提交
351 352 353 354 355
		add_timer(&dmxdevfilter->timer);
	}
}

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

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

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

396
	spin_lock(&dmxdevfilter->dev->lock);
397
	if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
398
		spin_unlock(&dmxdevfilter->dev->lock);
L
Linus Torvalds 已提交
399 400 401
		return 0;
	}

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

L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434
	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:
435 436
		list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
			feed->ts->stop_filtering(feed->ts);
L
Linus Torvalds 已提交
437 438 439 440 441 442 443 444 445 446
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/* start feed associated with the specified filter */
static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
{
447 448 449
	struct dmxdev_feed *feed;
	int ret;

450
	dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
L
Linus Torvalds 已提交
451 452 453 454 455

	switch (filter->type) {
	case DMXDEV_TYPE_SEC:
		return filter->feed.sec->start_filtering(filter->feed.sec);
	case DMXDEV_TYPE_PES:
456 457 458 459 460 461 462 463
		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 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	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;

479 480 481 482
	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 已提交
483 484 485 486
			dvb_dmxdev_feed_start(&dmxdev->filter[i]);
			return 0;
		}

487 488
	filter->dev->demux->release_section_feed(dmxdev->demux,
						 filter->feed.sec);
L
Linus Torvalds 已提交
489 490 491 492 493 494

	return 0;
}

static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
{
495 496 497
	struct dmxdev_feed *feed;
	struct dmx_demux *demux;

498
	if (dmxdevfilter->state < DMXDEV_STATE_GO)
L
Linus Torvalds 已提交
499 500 501 502 503 504 505 506 507
		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->
508 509
			    release_filter(dmxdevfilter->feed.sec,
					   dmxdevfilter->filter.sec);
L
Linus Torvalds 已提交
510
		dvb_dmxdev_feed_restart(dmxdevfilter);
511
		dmxdevfilter->feed.sec = NULL;
L
Linus Torvalds 已提交
512 513 514
		break;
	case DMXDEV_TYPE_PES:
		dvb_dmxdev_feed_stop(dmxdevfilter);
515 516 517 518 519
		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 已提交
520 521
		break;
	default:
522
		if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
L
Linus Torvalds 已提交
523 524 525
			return 0;
		return -EINVAL;
	}
526 527

	dvb_ringbuffer_flush(&dmxdevfilter->buffer);
L
Linus Torvalds 已提交
528 529 530
	return 0;
}

531 532 533 534 535 536 537 538 539 540 541 542 543
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 已提交
544 545
static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
{
546
	if (dmxdevfilter->state < DMXDEV_STATE_SET)
L
Linus Torvalds 已提交
547 548
		return 0;

549 550 551
	if (dmxdevfilter->type == DMXDEV_TYPE_PES)
		dvb_dmxdev_delete_pids(dmxdevfilter);

552
	dmxdevfilter->type = DMXDEV_TYPE_NONE;
L
Linus Torvalds 已提交
553 554 555 556
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
	return 0;
}

557 558 559 560
static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
				 struct dmxdev_filter *filter,
				 struct dmxdev_feed *feed)
{
T
Thomas Gleixner 已提交
561
	ktime_t timeout = 0;
562
	struct dmx_pes_filter_params *para = &filter->params.pes;
563
	enum dmx_output otype;
564 565
	int ret;
	int ts_type;
566
	enum dmx_ts_pes ts_pes;
567 568 569 570 571
	struct dmx_ts_feed *tsfeed;

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

572
	ts_pes = para->pes_type;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

	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;

594
	ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, timeout);
595 596 597 598 599 600 601 602 603 604 605 606 607 608
	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 已提交
609 610 611
static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
{
	struct dmxdev *dmxdev = filter->dev;
612
	struct dmxdev_feed *feed;
L
Linus Torvalds 已提交
613 614 615 616 617 618 619 620 621
	void *mem;
	int ret, i;

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

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

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

631
	dvb_ringbuffer_flush(&filter->buffer);
L
Linus Torvalds 已提交
632 633 634 635

	switch (filter->type) {
	case DMXDEV_TYPE_SEC:
	{
636 637 638 639 640 641
		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 已提交
642 643 644


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

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

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

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

		(*secfilter)->priv = filter;

		memcpy(&((*secfilter)->filter_value[3]),
688
		       &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
689
		memcpy(&(*secfilter)->filter_mask[3],
690
		       &para->filter.mask[1], DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
691
		memcpy(&(*secfilter)->filter_mode[3],
692
		       &para->filter.mode[1], DMX_FILTER_SIZE - 1);
L
Linus Torvalds 已提交
693

694 695 696 697 698
		(*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 已提交
699 700 701

		filter->todo = 0;

702
		ret = filter->feed.sec->start_filtering(filter->feed.sec);
L
Linus Torvalds 已提交
703 704 705 706 707 708 709
		if (ret < 0)
			return ret;

		dvb_dmxdev_filter_timer(filter);
		break;
	}
	case DMXDEV_TYPE_PES:
710 711 712 713 714 715
		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;
			}
716
		}
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727
		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)
{
728 729
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
730 731 732 733 734 735
	int i;
	struct dmxdev_filter *dmxdevfilter;

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

736
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
737 738
		return -ERESTARTSYS;

739 740
	for (i = 0; i < dmxdev->filternum; i++)
		if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
L
Linus Torvalds 已提交
741 742
			break;

743
	if (i == dmxdev->filternum) {
744
		mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
745 746 747
		return -EMFILE;
	}

748
	dmxdevfilter = &dmxdev->filter[i];
749
	mutex_init(&dmxdevfilter->mutex);
750
	file->private_data = dmxdevfilter;
L
Linus Torvalds 已提交
751

752
	dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
753
	dmxdevfilter->type = DMXDEV_TYPE_NONE;
L
Linus Torvalds 已提交
754
	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
755
	timer_setup(&dmxdevfilter->timer, dvb_dmxdev_filter_timeout, 0);
L
Linus Torvalds 已提交
756

757 758
	dvbdev->users++;

759
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
760 761 762
	return 0;
}

763 764
static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
				  struct dmxdev_filter *dmxdevfilter)
L
Linus Torvalds 已提交
765
{
766 767
	mutex_lock(&dmxdev->mutex);
	mutex_lock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
768 769 770 771 772

	dvb_dmxdev_filter_stop(dmxdevfilter);
	dvb_dmxdev_filter_reset(dmxdevfilter);

	if (dmxdevfilter->buffer.data) {
773
		void *mem = dmxdevfilter->buffer.data;
L
Linus Torvalds 已提交
774 775

		spin_lock_irq(&dmxdev->lock);
776
		dmxdevfilter->buffer.data = NULL;
L
Linus Torvalds 已提交
777 778 779 780 781 782
		spin_unlock_irq(&dmxdev->lock);
		vfree(mem);
	}

	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
	wake_up(&dmxdevfilter->buffer.queue);
783 784
	mutex_unlock(&dmxdevfilter->mutex);
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
785 786 787
	return 0;
}

788
static inline void invert_mode(struct dmx_filter *filter)
L
Linus Torvalds 已提交
789 790 791
{
	int i;

792 793
	for (i = 0; i < DMX_FILTER_SIZE; i++)
		filter->mode[i] ^= 0xff;
L
Linus Torvalds 已提交
794 795
}

796 797 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
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 已提交
845
static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
846 847
				 struct dmxdev_filter *dmxdevfilter,
				 struct dmx_sct_filter_params *params)
L
Linus Torvalds 已提交
848
{
849
	dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
850
		__func__, params->pid, params->flags, params->timeout);
L
Linus Torvalds 已提交
851 852 853

	dvb_dmxdev_filter_stop(dmxdevfilter);

854
	dmxdevfilter->type = DMXDEV_TYPE_SEC;
L
Linus Torvalds 已提交
855 856 857 858 859
	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);

860
	if (params->flags & DMX_IMMEDIATE_START)
L
Linus Torvalds 已提交
861 862 863 864 865 866
		return dvb_dmxdev_filter_start(dmxdevfilter);

	return 0;
}

static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
867 868
				     struct dmxdev_filter *dmxdevfilter,
				     struct dmx_pes_filter_params *params)
L
Linus Torvalds 已提交
869
{
870 871
	int ret;

L
Linus Torvalds 已提交
872
	dvb_dmxdev_filter_stop(dmxdevfilter);
873
	dvb_dmxdev_filter_reset(dmxdevfilter);
L
Linus Torvalds 已提交
874

875
	if ((unsigned int)params->pes_type > DMX_PES_OTHER)
L
Linus Torvalds 已提交
876 877
		return -EINVAL;

878 879 880
	dmxdevfilter->type = DMXDEV_TYPE_PES;
	memcpy(&dmxdevfilter->params, params,
	       sizeof(struct dmx_pes_filter_params));
881
	INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
L
Linus Torvalds 已提交
882 883 884

	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);

885 886 887 888 889
	ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
				 dmxdevfilter->params.pes.pid);
	if (ret < 0)
		return ret;

890
	if (params->flags & DMX_IMMEDIATE_START)
L
Linus Torvalds 已提交
891 892 893 894 895 896
		return dvb_dmxdev_filter_start(dmxdevfilter);

	return 0;
}

static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
897 898
				   struct file *file, char __user *buf,
				   size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
899 900
{
	int result, hcount;
901 902 903 904 905 906 907 908 909 910 911
	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 已提交
912 913
			return result;
		}
914
		if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
L
Linus Torvalds 已提交
915
			return -EFAULT;
916 917 918 919 920
		buf += result;
		done = result;
		count -= result;
		dfil->todo -= result;
		if (dfil->todo > -3)
L
Linus Torvalds 已提交
921
			return done;
922
		dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
L
Linus Torvalds 已提交
923 924 925
		if (!count)
			return done;
	}
926 927 928 929 930 931
	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 已提交
932
		return result;
933 934
	dfil->todo -= result;
	return (result + done);
L
Linus Torvalds 已提交
935 936 937
}

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

944
	if (mutex_lock_interruptible(&dmxdevfilter->mutex))
L
Linus Torvalds 已提交
945 946
		return -ERESTARTSYS;

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

954
	mutex_unlock(&dmxdevfilter->mutex);
L
Linus Torvalds 已提交
955 956 957
	return ret;
}

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

966
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
967 968 969 970
		return -ERESTARTSYS;

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

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

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

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

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

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

	case DMX_GET_STC:
		if (!dmxdev->demux->get_stc) {
1028
			ret = -EINVAL;
L
Linus Torvalds 已提交
1029 1030 1031
			break;
		}
		ret = dmxdev->demux->get_stc(dmxdev->demux,
1032 1033 1034
					     ((struct dmx_stc *)parg)->num,
					     &((struct dmx_stc *)parg)->stc,
					     &((struct dmx_stc *)parg)->base);
L
Linus Torvalds 已提交
1035 1036
		break;

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	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 已提交
1055
	default:
1056 1057
		ret = -EINVAL;
		break;
L
Linus Torvalds 已提交
1058
	}
1059
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1060 1061 1062
	return ret;
}

1063 1064
static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
			    unsigned long arg)
L
Linus Torvalds 已提交
1065
{
A
Arnd Bergmann 已提交
1066
	return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
L
Linus Torvalds 已提交
1067 1068
}

1069
static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
1070
{
1071
	struct dmxdev_filter *dmxdevfilter = file->private_data;
L
Linus Torvalds 已提交
1072 1073
	unsigned int mask = 0;

1074
	if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
1075
		return POLLERR;
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

	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);

1087
	if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093 1094
		mask |= (POLLIN | POLLRDNORM | POLLPRI);

	return mask;
}

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

1098 1099 1100 1101 1102 1103
	int ret;

	ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);

	mutex_lock(&dmxdev->mutex);
	dmxdev->dvbdev->users--;
1104
	if (dmxdev->dvbdev->users == 1 && dmxdev->exit == 1) {
1105 1106 1107 1108 1109 1110
		mutex_unlock(&dmxdev->mutex);
		wake_up(&dmxdev->dvbdev->wait_queue);
	} else
		mutex_unlock(&dmxdev->mutex);

	return ret;
L
Linus Torvalds 已提交
1111 1112
}

1113
static const struct file_operations dvb_demux_fops = {
1114 1115
	.owner = THIS_MODULE,
	.read = dvb_demux_read,
1116
	.unlocked_ioctl = dvb_demux_ioctl,
1117 1118 1119
	.open = dvb_demux_open,
	.release = dvb_demux_release,
	.poll = dvb_demux_poll,
1120
	.llseek = default_llseek,
L
Linus Torvalds 已提交
1121 1122
};

1123
static const struct dvb_device dvbdev_demux = {
1124 1125 1126
	.priv = NULL,
	.users = 1,
	.writers = 1,
1127
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1128
	.name = "dvb-demux",
1129
#endif
1130
	.fops = &dvb_demux_fops
L
Linus Torvalds 已提交
1131 1132
};

1133
static int dvb_dvr_do_ioctl(struct file *file,
1134
			    unsigned int cmd, void *parg)
L
Linus Torvalds 已提交
1135
{
1136 1137
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
1138
	unsigned long arg = (unsigned long)parg;
1139
	int ret;
L
Linus Torvalds 已提交
1140

1141
	if (mutex_lock_interruptible(&dmxdev->mutex))
L
Linus Torvalds 已提交
1142 1143 1144 1145
		return -ERESTARTSYS;

	switch (cmd) {
	case DMX_SET_BUFFER_SIZE:
1146
		ret = dvb_dvr_set_buffer_size(dmxdev, arg);
L
Linus Torvalds 已提交
1147 1148 1149
		break;

	default:
1150 1151
		ret = -EINVAL;
		break;
L
Linus Torvalds 已提交
1152
	}
1153
	mutex_unlock(&dmxdev->mutex);
L
Linus Torvalds 已提交
1154 1155 1156
	return ret;
}

1157
static long dvb_dvr_ioctl(struct file *file,
1158
			 unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1159
{
A
Arnd Bergmann 已提交
1160
	return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
L
Linus Torvalds 已提交
1161 1162
}

1163
static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
1164
{
1165 1166
	struct dvb_device *dvbdev = file->private_data;
	struct dmxdev *dmxdev = dvbdev->priv;
L
Linus Torvalds 已提交
1167 1168
	unsigned int mask = 0;

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

1171 1172 1173
	if (dmxdev->exit)
		return POLLERR;

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

1176
	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
L
Linus Torvalds 已提交
1177 1178 1179
		if (dmxdev->dvr_buffer.error)
			mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);

1180
		if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187
			mask |= (POLLIN | POLLRDNORM | POLLPRI);
	} else
		mask |= (POLLOUT | POLLWRNORM | POLLPRI);

	return mask;
}

1188
static const struct file_operations dvb_dvr_fops = {
1189 1190 1191
	.owner = THIS_MODULE,
	.read = dvb_dvr_read,
	.write = dvb_dvr_write,
1192
	.unlocked_ioctl = dvb_dvr_ioctl,
1193 1194 1195
	.open = dvb_dvr_open,
	.release = dvb_dvr_release,
	.poll = dvb_dvr_poll,
1196
	.llseek = default_llseek,
L
Linus Torvalds 已提交
1197 1198
};

1199
static const struct dvb_device dvbdev_dvr = {
1200
	.priv = NULL,
1201
	.readers = 1,
1202
	.users = 1,
1203
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1204
	.name = "dvb-dvr",
1205
#endif
1206
	.fops = &dvb_dvr_fops
L
Linus Torvalds 已提交
1207
};
1208
int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214
{
	int i;

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

1215
	dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
L
Linus Torvalds 已提交
1216 1217 1218
	if (!dmxdev->filter)
		return -ENOMEM;

1219
	mutex_init(&dmxdev->mutex);
L
Linus Torvalds 已提交
1220
	spin_lock_init(&dmxdev->lock);
1221 1222 1223 1224 1225
	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 已提交
1226 1227
	}

1228
	dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1229
			    DVB_DEVICE_DEMUX, dmxdev->filternum);
1230
	dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1231
			    dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
L
Linus Torvalds 已提交
1232

1233
	dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
L
Linus Torvalds 已提交
1234 1235 1236

	return 0;
}
1237

L
Linus Torvalds 已提交
1238 1239
EXPORT_SYMBOL(dvb_dmxdev_init);

1240
void dvb_dmxdev_release(struct dmxdev *dmxdev)
L
Linus Torvalds 已提交
1241
{
1242
	dmxdev->exit = 1;
1243 1244
	if (dmxdev->dvbdev->users > 1) {
		wait_event(dmxdev->dvbdev->wait_queue,
1245
				dmxdev->dvbdev->users == 1);
1246 1247 1248
	}
	if (dmxdev->dvr_dvbdev->users > 1) {
		wait_event(dmxdev->dvr_dvbdev->wait_queue,
1249
				dmxdev->dvr_dvbdev->users == 1);
1250 1251
	}

L
Linus Torvalds 已提交
1252 1253 1254 1255
	dvb_unregister_device(dmxdev->dvbdev);
	dvb_unregister_device(dmxdev->dvr_dvbdev);

	vfree(dmxdev->filter);
1256
	dmxdev->filter = NULL;
L
Linus Torvalds 已提交
1257 1258
	dmxdev->demux->close(dmxdev->demux);
}
1259

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