dvb_demux.c 28.6 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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 44 45 46
/*
 * dvb_demux.c - DVB kernel demux API
 *
 * Copyright (C) 2000-2001 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/poll.h>
#include <linux/string.h>
#include <linux/crc32.h>
#include <asm/uaccess.h>

#include "dvb_demux.h"

#define NOBUFS
/*
** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
*/
// #define DVB_DEMUX_SECTION_LOSS_LOG

/******************************************************************************
 * static inlined helper functions
 ******************************************************************************/

static inline u16 section_length(const u8 *buf)
{
47
	return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
Linus Torvalds's avatar
Linus Torvalds committed
48 49 50 51
}

static inline u16 ts_pid(const u8 *buf)
{
52
	return ((buf[1] & 0x1f) << 8) + buf[2];
Linus Torvalds's avatar
Linus Torvalds committed
53 54 55 56
}

static inline u8 payload(const u8 *tsp)
{
57
	if (!(tsp[3] & 0x10))	// no payload?
Linus Torvalds's avatar
Linus Torvalds committed
58
		return 0;
59 60 61

	if (tsp[3] & 0x20) {	// adaptation field?
		if (tsp[4] > 183)	// corrupted data?
Linus Torvalds's avatar
Linus Torvalds committed
62 63
			return 0;
		else
64
			return 184 - 1 - tsp[4];
Linus Torvalds's avatar
Linus Torvalds committed
65
	}
66

Linus Torvalds's avatar
Linus Torvalds committed
67 68 69
	return 184;
}

70
static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
Linus Torvalds's avatar
Linus Torvalds committed
71
{
72
	return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
Linus Torvalds's avatar
Linus Torvalds committed
73 74
}

75 76
static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
			    size_t len)
Linus Torvalds's avatar
Linus Torvalds committed
77
{
78
	memcpy(d, s, len);
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82 83 84
}

/******************************************************************************
 * Software filter functions
 ******************************************************************************/

85 86
static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
					   const u8 *buf)
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90 91 92 93 94 95
{
	int count = payload(buf);
	int p;
	//int ccok;
	//u8 cc;

	if (count == 0)
		return -1;

96
	p = 188 - count;
Linus Torvalds's avatar
Linus Torvalds committed
97 98

	/*
99 100 101
	cc = buf[3] & 0x0f;
	ccok = ((feed->cc + 1) & 0x0f) == cc;
	feed->cc = cc;
Linus Torvalds's avatar
Linus Torvalds committed
102 103 104 105
	if (!ccok)
		printk("missed packet!\n");
	*/

106
	if (buf[1] & 0x40)	// PUSI ?
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110
		feed->peslen = 0xfffa;

	feed->peslen += count;

111
	return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
Linus Torvalds's avatar
Linus Torvalds committed
112 113
}

114 115
static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
					  struct dvb_demux_filter *f)
Linus Torvalds's avatar
Linus Torvalds committed
116 117 118 119
{
	u8 neq = 0;
	int i;

120
	for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
121 122 123 124 125 126 127 128 129 130 131
		u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];

		if (f->maskandmode[i] & xor)
			return 0;

		neq |= f->maskandnotmode[i] & xor;
	}

	if (f->doneq && !neq)
		return 0;

132 133
	return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
			    NULL, 0, &f->filter, DMX_OK);
Linus Torvalds's avatar
Linus Torvalds committed
134 135
}

136
static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
Linus Torvalds's avatar
Linus Torvalds committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
{
	struct dvb_demux *demux = feed->demux;
	struct dvb_demux_filter *f = feed->filter;
	struct dmx_section_feed *sec = &feed->feed.sec;
	int section_syntax_indicator;

	if (!sec->is_filtering)
		return 0;

	if (!f)
		return 0;

	if (sec->check_crc) {
		section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
		if (section_syntax_indicator &&
		    demux->check_crc32(feed, sec->secbuf, sec->seclen))
			return -1;
	}

	do {
		if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
			return -1;
	} while ((f = f->next) && sec->is_filtering);

	sec->seclen = 0;

	return 0;
}

static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
{
	struct dmx_section_feed *sec = &feed->feed.sec;

#ifdef DVB_DEMUX_SECTION_LOSS_LOG
171
	if (sec->secbufp < sec->tsfeedp) {
Linus Torvalds's avatar
Linus Torvalds committed
172 173
		int i, n = sec->tsfeedp - sec->secbufp;

174 175 176 177 178 179
		/*
		 * Section padding is done with 0xff bytes entirely.
		 * Due to speed reasons, we won't check all of them
		 * but just first and last.
		 */
		if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182
			printk("dvb_demux.c section ts padding loss: %d/%d\n",
			       n, sec->tsfeedp);
			printk("dvb_demux.c pad data:");
183
			for (i = 0; i < n; i++)
Linus Torvalds's avatar
Linus Torvalds committed
184 185 186 187 188 189 190 191 192 193 194
				printk(" %02x", sec->secbuf[i]);
			printk("\n");
		}
	}
#endif

	sec->tsfeedp = sec->secbufp = sec->seclen = 0;
	sec->secbuf = sec->secbuf_base;
}

/*
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
 * Losless Section Demux 1.4.1 by Emard
 * Valsecchi Patrick:
 *  - middle of section A  (no PUSI)
 *  - end of section A and start of section B
 *    (with PUSI pointing to the start of the second section)
 *
 *  In this case, without feed->pusi_seen you'll receive a garbage section
 *  consisting of the end of section A. Basically because tsfeedp
 *  is incemented and the use=0 condition is not raised
 *  when the second packet arrives.
 *
 * Fix:
 * when demux is started, let feed->pusi_seen = 0 to
 * prevent initial feeding of garbage from the end of
 * previous section. When you for the first time see PUSI=1
 * then set feed->pusi_seen = 1
 */
static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
					      const u8 *buf, u8 len)
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218
{
	struct dvb_demux *demux = feed->demux;
	struct dmx_section_feed *sec = &feed->feed.sec;
	u16 limit, seclen, n;

219
	if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
220 221
		return 0;

222
	if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
Linus Torvalds's avatar
Linus Torvalds committed
223 224
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
		printk("dvb_demux.c section buffer full loss: %d/%d\n",
225 226
		       sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
		       DMX_MAX_SECFEED_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
227 228 229 230
#endif
		len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
	}

231
	if (len <= 0)
Linus Torvalds's avatar
Linus Torvalds committed
232 233 234 235 236
		return 0;

	demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
	sec->tsfeedp += len;

237 238 239
	/*
	 * Dump all the sections we can find in the data (Emard)
	 */
Linus Torvalds's avatar
Linus Torvalds committed
240
	limit = sec->tsfeedp;
241 242
	if (limit > DMX_MAX_SECFEED_SIZE)
		return -1;	/* internal error should never happen */
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245 246

	/* to be sure always set secbuf */
	sec->secbuf = sec->secbuf_base + sec->secbufp;

247
	for (n = 0; sec->secbufp + 2 < limit; n++) {
Linus Torvalds's avatar
Linus Torvalds committed
248
		seclen = section_length(sec->secbuf);
249
		if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
250
		    || seclen + sec->secbufp > limit)
Linus Torvalds's avatar
Linus Torvalds committed
251 252 253 254
			return 0;
		sec->seclen = seclen;
		sec->crc_val = ~0;
		/* dump [secbuf .. secbuf+seclen) */
255
		if (feed->pusi_seen)
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260
			dvb_dmx_swfilter_section_feed(feed);
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
		else
			printk("dvb_demux.c pusi not seen, discarding section data\n");
#endif
261 262
		sec->secbufp += seclen;	/* secbufp and secbuf moving together is */
		sec->secbuf += seclen;	/* redundant but saves pointer arithmetic */
Linus Torvalds's avatar
Linus Torvalds committed
263 264 265 266 267
	}

	return 0;
}

268 269
static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
					   const u8 *buf)
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276
{
	u8 p, count;
	int ccok, dc_i = 0;
	u8 cc;

	count = payload(buf);

277
	if (count == 0)		/* count == 0 if no payload or out of range */
Linus Torvalds's avatar
Linus Torvalds committed
278 279
		return -1;

280
	p = 188 - count;	/* payload start */
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284 285 286 287 288 289 290 291 292 293

	cc = buf[3] & 0x0f;
	ccok = ((feed->cc + 1) & 0x0f) == cc;
	feed->cc = cc;

	if (buf[3] & 0x20) {
		/* adaption field present, check for discontinuity_indicator */
		if ((buf[4] > 0) && (buf[5] & 0x80))
			dc_i = 1;
	}

	if (!ccok || dc_i) {
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
294 295 296 297 298 299
		printk("dvb_demux.c discontinuity detected %d bytes lost\n",
		       count);
		/*
		 * those bytes under sume circumstances will again be reported
		 * in the following dvb_dmx_swfilter_section_new
		 */
Linus Torvalds's avatar
Linus Torvalds committed
300
#endif
301 302 303 304
		/*
		 * Discontinuity detected. Reset pusi_seen = 0 to
		 * stop feeding of suspicious data until next PUSI=1 arrives
		 */
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307 308 309
		feed->pusi_seen = 0;
		dvb_dmx_swfilter_section_new(feed);
	}

	if (buf[1] & 0x40) {
310
		/* PUSI=1 (is set), section boundary is here */
Linus Torvalds's avatar
Linus Torvalds committed
311
		if (count > 1 && buf[p] < count) {
312
			const u8 *before = &buf[p + 1];
Linus Torvalds's avatar
Linus Torvalds committed
313
			u8 before_len = buf[p];
314 315
			const u8 *after = &before[before_len];
			u8 after_len = count - 1 - before_len;
Linus Torvalds's avatar
Linus Torvalds committed
316

317 318
			dvb_dmx_swfilter_section_copy_dump(feed, before,
							   before_len);
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321
			/* before start of new section, set pusi_seen = 1 */
			feed->pusi_seen = 1;
			dvb_dmx_swfilter_section_new(feed);
322 323
			dvb_dmx_swfilter_section_copy_dump(feed, after,
							   after_len);
Linus Torvalds's avatar
Linus Torvalds committed
324 325
		}
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
326 327
		else if (count > 0)
			printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
Linus Torvalds's avatar
Linus Torvalds committed
328 329
#endif
	} else {
330 331
		/* PUSI=0 (is not set), no section boundary */
		dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
Linus Torvalds's avatar
Linus Torvalds committed
332
	}
333

Linus Torvalds's avatar
Linus Torvalds committed
334 335 336
	return 0;
}

337 338
static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
						const u8 *buf)
Linus Torvalds's avatar
Linus Torvalds committed
339
{
340
	switch (feed->type) {
Linus Torvalds's avatar
Linus Torvalds committed
341 342 343 344 345 346 347
	case DMX_TYPE_TS:
		if (!feed->feed.ts.is_filtering)
			break;
		if (feed->ts_type & TS_PACKET) {
			if (feed->ts_type & TS_PAYLOAD_ONLY)
				dvb_dmx_swfilter_payload(feed, buf);
			else
348 349
				feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
					    DMX_OK);
Linus Torvalds's avatar
Linus Torvalds committed
350 351 352 353 354 355 356 357 358 359
		}
		if (feed->ts_type & TS_DECODER)
			if (feed->demux->write_to_decoder)
				feed->demux->write_to_decoder(feed, buf, 188);
		break;

	case DMX_TYPE_SEC:
		if (!feed->feed.sec.is_filtering)
			break;
		if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
360
			feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
Linus Torvalds's avatar
Linus Torvalds committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
		break;

	default:
		break;
	}
}

#define DVR_FEED(f)							\
	(((f)->type == DMX_TYPE_TS) &&					\
	((f)->feed.ts.is_filtering) &&					\
	(((f)->ts_type & (TS_PACKET|TS_PAYLOAD_ONLY)) == TS_PACKET))

static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
{
	struct dvb_demux_feed *feed;
376
	struct list_head *pos, *head = &demux->feed_list;
Linus Torvalds's avatar
Linus Torvalds committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	u16 pid = ts_pid(buf);
	int dvr_done = 0;

	list_for_each(pos, head) {
		feed = list_entry(pos, struct dvb_demux_feed, list_head);

		if ((feed->pid != pid) && (feed->pid != 0x2000))
			continue;

		/* copy each packet only once to the dvr device, even
		 * if a PID is in multiple filters (e.g. video + PCR) */
		if ((DVR_FEED(feed)) && (dvr_done++))
			continue;

		if (feed->pid == pid) {
			dvb_dmx_swfilter_packet_type(feed, buf);
			if (DVR_FEED(feed))
				continue;
		}

		if (feed->pid == 0x2000)
			feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
	}
}

402 403
void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
			      size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
404 405 406 407
{
	spin_lock(&demux->lock);

	while (count--) {
408 409
		if (buf[0] == 0x47)
			dvb_dmx_swfilter_packet(demux, buf);
Linus Torvalds's avatar
Linus Torvalds committed
410 411 412 413 414 415
		buf += 188;
	}

	spin_unlock(&demux->lock);
}

416
EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
Linus Torvalds's avatar
Linus Torvalds committed
417 418 419 420 421 422 423

void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
{
	int p = 0, i, j;

	spin_lock(&demux->lock);

424 425 426 427
	if (demux->tsbufp) {
		i = demux->tsbufp;
		j = 188 - i;
		if (count < j) {
Linus Torvalds's avatar
Linus Torvalds committed
428 429 430 431 432 433 434 435 436 437 438 439 440
			memcpy(&demux->tsbuf[i], buf, count);
			demux->tsbufp += count;
			goto bailout;
		}
		memcpy(&demux->tsbuf[i], buf, j);
		if (demux->tsbuf[0] == 0x47)
			dvb_dmx_swfilter_packet(demux, demux->tsbuf);
		demux->tsbufp = 0;
		p += j;
	}

	while (p < count) {
		if (buf[p] == 0x47) {
441 442
			if (count - p >= 188) {
				dvb_dmx_swfilter_packet(demux, &buf[p]);
Linus Torvalds's avatar
Linus Torvalds committed
443 444
				p += 188;
			} else {
445 446 447
				i = count - p;
				memcpy(demux->tsbuf, &buf[p], i);
				demux->tsbufp = i;
Linus Torvalds's avatar
Linus Torvalds committed
448 449 450 451 452 453 454 455 456
				goto bailout;
			}
		} else
			p++;
	}

bailout:
	spin_unlock(&demux->lock);
}
457

Linus Torvalds's avatar
Linus Torvalds committed
458 459 460 461
EXPORT_SYMBOL(dvb_dmx_swfilter);

void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
{
462
	int p = 0, i, j;
Linus Torvalds's avatar
Linus Torvalds committed
463
	u8 tmppack[188];
464

Linus Torvalds's avatar
Linus Torvalds committed
465 466
	spin_lock(&demux->lock);

467 468 469 470
	if (demux->tsbufp) {
		i = demux->tsbufp;
		j = 204 - i;
		if (count < j) {
Linus Torvalds's avatar
Linus Torvalds committed
471 472 473 474 475
			memcpy(&demux->tsbuf[i], buf, count);
			demux->tsbufp += count;
			goto bailout;
		}
		memcpy(&demux->tsbuf[i], buf, j);
476
		if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
Linus Torvalds's avatar
Linus Torvalds committed
477
			memcpy(tmppack, demux->tsbuf, 188);
478 479
			if (tmppack[0] == 0xB8)
				tmppack[0] = 0x47;
Linus Torvalds's avatar
Linus Torvalds committed
480 481 482 483 484 485 486
			dvb_dmx_swfilter_packet(demux, tmppack);
		}
		demux->tsbufp = 0;
		p += j;
	}

	while (p < count) {
487
		if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
488 489 490 491
			if (count - p >= 204) {
				memcpy(tmppack, &buf[p], 188);
				if (tmppack[0] == 0xB8)
					tmppack[0] = 0x47;
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494
				dvb_dmx_swfilter_packet(demux, tmppack);
				p += 204;
			} else {
495 496 497
				i = count - p;
				memcpy(demux->tsbuf, &buf[p], i);
				demux->tsbufp = i;
Linus Torvalds's avatar
Linus Torvalds committed
498 499 500 501 502 503 504 505 506 507 508
				goto bailout;
			}
		} else {
			p++;
		}
	}

bailout:
	spin_unlock(&demux->lock);
}

509
EXPORT_SYMBOL(dvb_dmx_swfilter_204);
Linus Torvalds's avatar
Linus Torvalds committed
510

511
static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
Linus Torvalds's avatar
Linus Torvalds committed
512 513 514
{
	int i;

515
	for (i = 0; i < demux->filternum; i++)
Linus Torvalds's avatar
Linus Torvalds committed
516 517 518 519 520 521 522 523 524 525 526
		if (demux->filter[i].state == DMX_STATE_FREE)
			break;

	if (i == demux->filternum)
		return NULL;

	demux->filter[i].state = DMX_STATE_ALLOCATED;

	return &demux->filter[i];
}

527
static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
Linus Torvalds's avatar
Linus Torvalds committed
528 529 530
{
	int i;

531
	for (i = 0; i < demux->feednum; i++)
Linus Torvalds's avatar
Linus Torvalds committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		if (demux->feed[i].state == DMX_STATE_FREE)
			break;

	if (i == demux->feednum)
		return NULL;

	demux->feed[i].state = DMX_STATE_ALLOCATED;

	return &demux->feed[i];
}

static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
{
	struct dvb_demux_feed *entry;

	list_for_each_entry(entry, &feed->demux->feed_list, list_head)
		if (entry == feed)
			return 1;

	return 0;
}

static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
{
	spin_lock_irq(&feed->demux->lock);
	if (dvb_demux_feed_find(feed)) {
		printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
559
		       __FUNCTION__, feed->type, feed->state, feed->pid);
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564 565 566 567 568 569 570 571 572
		goto out;
	}

	list_add(&feed->list_head, &feed->demux->feed_list);
out:
	spin_unlock_irq(&feed->demux->lock);
}

static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
{
	spin_lock_irq(&feed->demux->lock);
	if (!(dvb_demux_feed_find(feed))) {
		printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
573
		       __FUNCTION__, feed->type, feed->state, feed->pid);
Linus Torvalds's avatar
Linus Torvalds committed
574 575 576 577 578 579 580 581
		goto out;
	}

	list_del(&feed->list_head);
out:
	spin_unlock_irq(&feed->demux->lock);
}

582 583 584
static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
			   enum dmx_ts_pes pes_type,
			   size_t circular_buffer_size, struct timespec timeout)
Linus Torvalds's avatar
Linus Torvalds committed
585
{
586
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds's avatar
Linus Torvalds committed
587 588 589 590 591
	struct dvb_demux *demux = feed->demux;

	if (pid > DMX_MAX_PID)
		return -EINVAL;

592
	if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
593 594 595 596
		return -ERESTARTSYS;

	if (ts_type & TS_DECODER) {
		if (pes_type >= DMX_TS_PES_OTHER) {
597
			mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
598 599 600 601 602
			return -EINVAL;
		}

		if (demux->pesfilter[pes_type] &&
		    demux->pesfilter[pes_type] != feed) {
603
			mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
			return -EINVAL;
		}

		demux->pesfilter[pes_type] = feed;
		demux->pids[pes_type] = pid;
	}

	dvb_demux_feed_add(feed);

	feed->pid = pid;
	feed->buffer_size = circular_buffer_size;
	feed->timeout = timeout;
	feed->ts_type = ts_type;
	feed->pes_type = pes_type;

	if (feed->buffer_size) {
#ifdef NOBUFS
621
		feed->buffer = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
622 623 624
#else
		feed->buffer = vmalloc(feed->buffer_size);
		if (!feed->buffer) {
625
			mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
626 627 628 629 630 631
			return -ENOMEM;
		}
#endif
	}

	feed->state = DMX_STATE_READY;
632
	mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
633 634 635 636

	return 0;
}

637
static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
Linus Torvalds's avatar
Linus Torvalds committed
638
{
639
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds's avatar
Linus Torvalds committed
640 641 642
	struct dvb_demux *demux = feed->demux;
	int ret;

643
	if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
644 645 646
		return -ERESTARTSYS;

	if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
647
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
648 649 650 651
		return -EINVAL;
	}

	if (!demux->start_feed) {
652
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
653 654 655 656
		return -ENODEV;
	}

	if ((ret = demux->start_feed(feed)) < 0) {
657
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
658 659 660 661 662 663 664
		return ret;
	}

	spin_lock_irq(&demux->lock);
	ts_feed->is_filtering = 1;
	feed->state = DMX_STATE_GO;
	spin_unlock_irq(&demux->lock);
665
	mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
666 667 668 669

	return 0;
}

670
static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
Linus Torvalds's avatar
Linus Torvalds committed
671
{
672
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds's avatar
Linus Torvalds committed
673 674 675
	struct dvb_demux *demux = feed->demux;
	int ret;

676
	mutex_lock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
677 678

	if (feed->state < DMX_STATE_GO) {
679
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
680 681 682 683
		return -EINVAL;
	}

	if (!demux->stop_feed) {
684
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
685 686 687 688 689 690 691 692 693
		return -ENODEV;
	}

	ret = demux->stop_feed(feed);

	spin_lock_irq(&demux->lock);
	ts_feed->is_filtering = 0;
	feed->state = DMX_STATE_ALLOCATED;
	spin_unlock_irq(&demux->lock);
694
	mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
695 696 697 698

	return ret;
}

699 700 701
static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
				   struct dmx_ts_feed **ts_feed,
				   dmx_ts_cb callback)
Linus Torvalds's avatar
Linus Torvalds committed
702
{
703
	struct dvb_demux *demux = (struct dvb_demux *)dmx;
Linus Torvalds's avatar
Linus Torvalds committed
704 705
	struct dvb_demux_feed *feed;

706
	if (mutex_lock_interruptible(&demux->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
707 708 709
		return -ERESTARTSYS;

	if (!(feed = dvb_dmx_feed_alloc(demux))) {
710
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
		return -EBUSY;
	}

	feed->type = DMX_TYPE_TS;
	feed->cb.ts = callback;
	feed->demux = demux;
	feed->pid = 0xffff;
	feed->peslen = 0xfffa;
	feed->buffer = NULL;

	(*ts_feed) = &feed->feed.ts;
	(*ts_feed)->parent = dmx;
	(*ts_feed)->priv = NULL;
	(*ts_feed)->is_filtering = 0;
	(*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
	(*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
	(*ts_feed)->set = dmx_ts_feed_set;

	if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
		feed->state = DMX_STATE_FREE;
731
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
732 733 734 735 736 737 738
		return -EBUSY;
	}

	feed->filter->type = DMX_TYPE_TS;
	feed->filter->feed = feed;
	feed->filter->state = DMX_STATE_READY;

739
	mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
740 741 742 743

	return 0;
}

744 745
static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
				  struct dmx_ts_feed *ts_feed)
Linus Torvalds's avatar
Linus Torvalds committed
746
{
747 748
	struct dvb_demux *demux = (struct dvb_demux *)dmx;
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
Linus Torvalds's avatar
Linus Torvalds committed
749

750
	mutex_lock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
751 752

	if (feed->state == DMX_STATE_FREE) {
753
		mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756 757
		return -EINVAL;
	}
#ifndef NOBUFS
	vfree(feed->buffer);
758
	feed->buffer = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
759 760 761 762 763 764 765 766 767 768 769 770
#endif

	feed->state = DMX_STATE_FREE;
	feed->filter->state = DMX_STATE_FREE;

	dvb_demux_feed_del(feed);

	feed->pid = 0xffff;

	if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
		demux->pesfilter[feed->pes_type] = NULL;

771
	mutex_unlock(&demux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
772 773 774 775 776 777 778
	return 0;
}

/******************************************************************************
 * dmx_section_feed API calls
 ******************************************************************************/

779 780
static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
					    struct dmx_section_filter **filter)
Linus Torvalds's avatar
Linus Torvalds committed
781
{
782
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785
	struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
	struct dvb_demux_filter *dvbdmxfilter;

786
	if (mutex_lock_interruptible(&dvbdemux->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
787 788 789 790
		return -ERESTARTSYS;

	dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
	if (!dvbdmxfilter) {
791
		mutex_unlock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
792 793 794 795 796 797 798 799 800 801 802 803 804 805
		return -EBUSY;
	}

	spin_lock_irq(&dvbdemux->lock);
	*filter = &dvbdmxfilter->filter;
	(*filter)->parent = feed;
	(*filter)->priv = NULL;
	dvbdmxfilter->feed = dvbdmxfeed;
	dvbdmxfilter->type = DMX_TYPE_SEC;
	dvbdmxfilter->state = DMX_STATE_READY;
	dvbdmxfilter->next = dvbdmxfeed->filter;
	dvbdmxfeed->filter = dvbdmxfilter;
	spin_unlock_irq(&dvbdemux->lock);

806
	mutex_unlock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
807 808 809
	return 0;
}

810 811 812
static int dmx_section_feed_set(struct dmx_section_feed *feed,
				u16 pid, size_t circular_buffer_size,
				int check_crc)
Linus Torvalds's avatar
Linus Torvalds committed
813
{
814
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;

	if (pid > 0x1fff)
		return -EINVAL;

820
	if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
821 822 823 824 825 826 827 828 829 830 831
		return -ERESTARTSYS;

	dvb_demux_feed_add(dvbdmxfeed);

	dvbdmxfeed->pid = pid;
	dvbdmxfeed->buffer_size = circular_buffer_size;
	dvbdmxfeed->feed.sec.check_crc = check_crc;

#ifdef NOBUFS
	dvbdmxfeed->buffer = NULL;
#else
832
	dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
Linus Torvalds's avatar
Linus Torvalds committed
833
	if (!dvbdmxfeed->buffer) {
834
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
835 836 837 838 839
		return -ENOMEM;
	}
#endif

	dvbdmxfeed->state = DMX_STATE_READY;
840
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
841 842 843 844 845 846 847 848 849 850
	return 0;
}

static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
{
	int i;
	struct dvb_demux_filter *f;
	struct dmx_section_filter *sf;
	u8 mask, mode, doneq;

851
	if (!(f = dvbdmxfeed->filter))
Linus Torvalds's avatar
Linus Torvalds committed
852 853 854 855
		return;
	do {
		sf = &f->filter;
		doneq = 0;
856
		for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
857 858 859 860 861 862 863 864 865 866 867
			mode = sf->filter_mode[i];
			mask = sf->filter_mask[i];
			f->maskandmode[i] = mask & mode;
			doneq |= f->maskandnotmode[i] = mask & ~mode;
		}
		f->doneq = doneq ? 1 : 0;
	} while ((f = f->next));
}

static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
{
868
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds's avatar
Linus Torvalds committed
869 870 871
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	int ret;

872
	if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875
		return -ERESTARTSYS;

	if (feed->is_filtering) {
876
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
877 878 879 880
		return -EBUSY;
	}

	if (!dvbdmxfeed->filter) {
881
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
882 883 884 885 886 887 888 889 890
		return -EINVAL;
	}

	dvbdmxfeed->feed.sec.tsfeedp = 0;
	dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
	dvbdmxfeed->feed.sec.secbufp = 0;
	dvbdmxfeed->feed.sec.seclen = 0;

	if (!dvbdmx->start_feed) {
891
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896 897
		return -ENODEV;
	}

	prepare_secfilters(dvbdmxfeed);

	if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
898
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
899 900 901 902 903 904 905 906
		return ret;
	}

	spin_lock_irq(&dvbdmx->lock);
	feed->is_filtering = 1;
	dvbdmxfeed->state = DMX_STATE_GO;
	spin_unlock_irq(&dvbdmx->lock);

907
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
908 909 910
	return 0;
}

911
static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
Linus Torvalds's avatar
Linus Torvalds committed
912
{
913
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	int ret;

917
	mutex_lock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
918 919

	if (!dvbdmx->stop_feed) {
920
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
921 922 923 924 925 926 927 928 929 930
		return -ENODEV;
	}

	ret = dvbdmx->stop_feed(dvbdmxfeed);

	spin_lock_irq(&dvbdmx->lock);
	dvbdmxfeed->state = DMX_STATE_READY;
	feed->is_filtering = 0;
	spin_unlock_irq(&dvbdmx->lock);

931
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
932 933 934 935
	return ret;
}

static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
936
					   struct dmx_section_filter *filter)
Linus Torvalds's avatar
Linus Torvalds committed
937
{
938 939
	struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
Linus Torvalds's avatar
Linus Torvalds committed
940 941
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;

942
	mutex_lock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
943 944

	if (dvbdmxfilter->feed != dvbdmxfeed) {
945
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
946 947 948 949 950 951 952 953 954 955 956 957
		return -EINVAL;
	}

	if (feed->is_filtering)
		feed->stop_filtering(feed);

	spin_lock_irq(&dvbdmx->lock);
	f = dvbdmxfeed->filter;

	if (f == dvbdmxfilter) {
		dvbdmxfeed->filter = dvbdmxfilter->next;
	} else {
958
		while (f->next != dvbdmxfilter)
Linus Torvalds's avatar
Linus Torvalds committed
959 960 961 962 963 964
			f = f->next;
		f->next = f->next->next;
	}

	dvbdmxfilter->state = DMX_STATE_FREE;
	spin_unlock_irq(&dvbdmx->lock);
965
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
966 967 968 969 970 971 972
	return 0;
}

static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
					struct dmx_section_feed **feed,
					dmx_section_cb callback)
{
973
	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
974 975
	struct dvb_demux_feed *dvbdmxfeed;

976
	if (mutex_lock_interruptible(&dvbdmx->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979
		return -ERESTARTSYS;

	if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
980
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
981 982 983 984 985 986 987 988 989 990 991 992 993
		return -EBUSY;
	}

	dvbdmxfeed->type = DMX_TYPE_SEC;
	dvbdmxfeed->cb.sec = callback;
	dvbdmxfeed->demux = dvbdmx;
	dvbdmxfeed->pid = 0xffff;
	dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
	dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
	dvbdmxfeed->feed.sec.tsfeedp = 0;
	dvbdmxfeed->filter = NULL;
	dvbdmxfeed->buffer = NULL;

994
	(*feed) = &dvbdmxfeed->feed.sec;
Linus Torvalds's avatar
Linus Torvalds committed
995 996 997 998 999 1000 1001 1002 1003 1004
	(*feed)->is_filtering = 0;
	(*feed)->parent = demux;
	(*feed)->priv = NULL;

	(*feed)->set = dmx_section_feed_set;
	(*feed)->allocate_filter = dmx_section_feed_allocate_filter;
	(*feed)->start_filtering = dmx_section_feed_start_filtering;
	(*feed)->stop_filtering = dmx_section_feed_stop_filtering;
	(*feed)->release_filter = dmx_section_feed_release_filter;

1005
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007 1008 1009 1010 1011
	return 0;
}

static int dvbdmx_release_section_feed(struct dmx_demux *demux,
				       struct dmx_section_feed *feed)
{
1012 1013
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1014

1015
	mutex_lock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1016

1017
	if (dvbdmxfeed->state == DMX_STATE_FREE) {
1018
		mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1019 1020 1021 1022
		return -EINVAL;
	}
#ifndef NOBUFS
	vfree(dvbdmxfeed->buffer);
1023
	dvbdmxfeed->buffer = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1024
#endif
1025
	dvbdmxfeed->state = DMX_STATE_FREE;
Linus Torvalds's avatar
Linus Torvalds committed
1026 1027 1028 1029 1030

	dvb_demux_feed_del(dvbdmxfeed);

	dvbdmxfeed->pid = 0xffff;

1031
	mutex_unlock(&dvbdmx->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033 1034 1035 1036 1037 1038 1039 1040
	return 0;
}

/******************************************************************************
 * dvb_demux kernel data API calls
 ******************************************************************************/

static int dvbdmx_open(struct dmx_demux *demux)
{
1041
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

	if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
		return -EUSERS;

	dvbdemux->users++;
	return 0;
}

static int dvbdmx_close(struct dmx_demux *demux)
{
1052
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063

	if (dvbdemux->users == 0)
		return -ENODEV;

	dvbdemux->users--;
	//FIXME: release any unneeded resources if users==0
	return 0;
}

static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
{
1064
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1065 1066 1067 1068

	if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
		return -EINVAL;

1069
	if (mutex_lock_interruptible(&dvbdemux->mutex))
Linus Torvalds's avatar
Linus Torvalds committed
1070 1071
		return -ERESTARTSYS;
	dvb_dmx_swfilter(dvbdemux, buf, count);
1072
	mutex_unlock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074 1075 1076 1077 1078

	if (signal_pending(current))
		return -EINTR;
	return count;
}

1079 1080
static int dvbdmx_add_frontend(struct dmx_demux *demux,
			       struct dmx_frontend *frontend)
Linus Torvalds's avatar
Linus Torvalds committed
1081
{
1082
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1083 1084 1085 1086 1087 1088 1089
	struct list_head *head = &dvbdemux->frontend_list;

	list_add(&(frontend->connectivity_list), head);

	return 0;
}

1090 1091
static int dvbdmx_remove_frontend(struct dmx_demux *demux,
				  struct dmx_frontend *frontend)
Linus Torvalds's avatar
Linus Torvalds committed
1092
{
1093
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1094 1095
	struct list_head *pos, *n, *head = &dvbdemux->frontend_list;

1096
	list_for_each_safe(pos, n, head) {
Linus Torvalds's avatar
Linus Torvalds committed
1097 1098 1099 1100 1101 1102 1103 1104 1105
		if (DMX_FE_ENTRY(pos) == frontend) {
			list_del(pos);
			return 0;
		}
	}

	return -ENODEV;
}

1106
static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
Linus Torvalds's avatar
Linus Torvalds committed
1107
{
1108
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1109 1110 1111

	if (list_empty(&dvbdemux->frontend_list))
		return NULL;
1112

Linus Torvalds's avatar
Linus Torvalds committed
1113 1114 1115
	return &dvbdemux->frontend_list;
}

1116 1117
static int dvbdmx_connect_frontend(struct dmx_demux *demux,
				   struct dmx_frontend *frontend)
Linus Torvalds's avatar
Linus Torvalds committed
1118
{
1119
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1120 1121 1122 1123

	if (demux->frontend)
		return -EINVAL;

1124
	mutex_lock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1125 1126

	demux->frontend = frontend;
1127
	mutex_unlock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1128 1129 1130 1131 1132
	return 0;
}

static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
{
1133
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1134

1135
	mutex_lock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1136 1137

	demux->frontend = NULL;
1138
	mutex_unlock(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1139 1140 1141
	return 0;
}

1142
static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
Linus Torvalds's avatar
Linus Torvalds committed
1143
{
1144
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
Linus Torvalds's avatar
Linus Torvalds committed
1145

1146
	memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
Linus Torvalds's avatar
Linus Torvalds committed
1147 1148 1149 1150 1151
	return 0;
}

int dvb_dmx_init(struct dvb_demux *dvbdemux)
{
1152
	int i;
Linus Torvalds's avatar
Linus Torvalds committed
1153 1154 1155
	struct dmx_demux *dmx = &dvbdemux->dmx;

	dvbdemux->users = 0;
1156
	dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
Linus Torvalds's avatar
Linus Torvalds committed
1157 1158 1159 1160

	if (!dvbdemux->filter)
		return -ENOMEM;

1161
	dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
Linus Torvalds's avatar
Linus Torvalds committed
1162 1163 1164 1165
	if (!dvbdemux->feed) {
		vfree(dvbdemux->filter);
		return -ENOMEM;
	}
1166
	for (i = 0; i < dvbdemux->filternum; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1167 1168 1169
		dvbdemux->filter[i].state = DMX_STATE_FREE;
		dvbdemux->filter[i].index = i;
	}
1170
	for (i = 0; i < dvbdemux->feednum; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1171 1172 1173
		dvbdemux->feed[i].state = DMX_STATE_FREE;
		dvbdemux->feed[i].index = i;
	}
1174 1175 1176

	INIT_LIST_HEAD(&dvbdemux->frontend_list);

1177
	for (i = 0; i < DMX_TS_PES_OTHER; i++) {
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
		dvbdemux->pesfilter[i] = NULL;
		dvbdemux->pids[i] = 0xffff;
	}

	INIT_LIST_HEAD(&dvbdemux->feed_list);

	dvbdemux->playing = 0;
	dvbdemux->recording = 0;
	dvbdemux->tsbufp = 0;

	if (!dvbdemux->check_crc32)
		dvbdemux->check_crc32 = dvb_dmx_crc32;

1191 1192
	if (!dvbdemux->memcopy)
		dvbdemux->memcopy = dvb_dmx_memcopy;
Linus Torvalds's avatar
Linus Torvalds committed
1193 1194

	dmx->frontend = NULL;
1195
	dmx->priv = dvbdemux;
Linus Torvalds's avatar
Linus Torvalds committed
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	dmx->open = dvbdmx_open;
	dmx->close = dvbdmx_close;
	dmx->write = dvbdmx_write;
	dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
	dmx->release_ts_feed = dvbdmx_release_ts_feed;
	dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
	dmx->release_section_feed = dvbdmx_release_section_feed;

	dmx->add_frontend = dvbdmx_add_frontend;
	dmx->remove_frontend = dvbdmx_remove_frontend;
	dmx->get_frontends = dvbdmx_get_frontends;
	dmx->connect_frontend = dvbdmx_connect_frontend;
	dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
	dmx->get_pes_pids = dvbdmx_get_pes_pids;

1211 1212
	init_waitqueue_head (&dvbdemux->wait_queue);

1213
	mutex_init(&dvbdemux->mutex);
Linus Torvalds's avatar
Linus Torvalds committed
1214 1215 1216 1217 1218
	spin_lock_init(&dvbdemux->lock);

	return 0;
}

1219
EXPORT_SYMBOL(dvb_dmx_init);
Linus Torvalds's avatar
Linus Torvalds committed
1220

1221
void dvb_dmx_release(struct dvb_demux *dvbdemux)
Linus Torvalds's avatar
Linus Torvalds committed
1222 1223 1224 1225
{
	vfree(dvbdemux->filter);
	vfree(dvbdemux->feed);
}
1226

Linus Torvalds's avatar
Linus Torvalds committed
1227
EXPORT_SYMBOL(dvb_dmx_release);