dvb_demux.c 31.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 *
 */

24
#include <linux/sched.h>
L
Linus Torvalds 已提交
25 26 27 28 29 30 31 32
#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>
33
#include <asm/div64.h>
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42

#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

43 44 45 46 47
static int dvb_demux_tscheck;
module_param(dvb_demux_tscheck, int, 0644);
MODULE_PARM_DESC(dvb_demux_tscheck,
		"enable transport stream continuity and TEI check");

48 49 50 51 52
static int dvb_demux_speedcheck;
module_param(dvb_demux_speedcheck, int, 0644);
MODULE_PARM_DESC(dvb_demux_speedcheck,
		"enable transport stream speed check");

53 54 55 56 57
#define dprintk_tscheck(x...) do {                              \
		if (dvb_demux_tscheck && printk_ratelimit())    \
			printk(x);                              \
	} while (0)

L
Linus Torvalds 已提交
58 59 60 61 62 63
/******************************************************************************
 * static inlined helper functions
 ******************************************************************************/

static inline u16 section_length(const u8 *buf)
{
64
	return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
L
Linus Torvalds 已提交
65 66 67 68
}

static inline u16 ts_pid(const u8 *buf)
{
69
	return ((buf[1] & 0x1f) << 8) + buf[2];
L
Linus Torvalds 已提交
70 71 72 73
}

static inline u8 payload(const u8 *tsp)
{
74
	if (!(tsp[3] & 0x10))	// no payload?
L
Linus Torvalds 已提交
75
		return 0;
76 77 78

	if (tsp[3] & 0x20) {	// adaptation field?
		if (tsp[4] > 183)	// corrupted data?
L
Linus Torvalds 已提交
79 80
			return 0;
		else
81
			return 184 - 1 - tsp[4];
L
Linus Torvalds 已提交
82
	}
83

L
Linus Torvalds 已提交
84 85 86
	return 184;
}

87
static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
L
Linus Torvalds 已提交
88
{
89
	return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
L
Linus Torvalds 已提交
90 91
}

92 93
static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
			    size_t len)
L
Linus Torvalds 已提交
94
{
95
	memcpy(d, s, len);
L
Linus Torvalds 已提交
96 97 98 99 100 101
}

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

102 103
static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
					   const u8 *buf)
L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112
{
	int count = payload(buf);
	int p;
	//int ccok;
	//u8 cc;

	if (count == 0)
		return -1;

113
	p = 188 - count;
L
Linus Torvalds 已提交
114 115

	/*
116 117 118
	cc = buf[3] & 0x0f;
	ccok = ((feed->cc + 1) & 0x0f) == cc;
	feed->cc = cc;
L
Linus Torvalds 已提交
119 120 121 122
	if (!ccok)
		printk("missed packet!\n");
	*/

123
	if (buf[1] & 0x40)	// PUSI ?
L
Linus Torvalds 已提交
124 125 126 127
		feed->peslen = 0xfffa;

	feed->peslen += count;

128
	return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
L
Linus Torvalds 已提交
129 130
}

131 132
static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
					  struct dvb_demux_filter *f)
L
Linus Torvalds 已提交
133 134 135 136
{
	u8 neq = 0;
	int i;

137
	for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146 147 148
		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;

149 150
	return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
			    NULL, 0, &f->filter, DMX_OK);
L
Linus Torvalds 已提交
151 152
}

153
static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
L
Linus Torvalds 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
	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
188
	if (sec->secbufp < sec->tsfeedp) {
L
Linus Torvalds 已提交
189 190
		int i, n = sec->tsfeedp - sec->secbufp;

191 192 193 194 195 196
		/*
		 * 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) {
L
Linus Torvalds 已提交
197 198 199
			printk("dvb_demux.c section ts padding loss: %d/%d\n",
			       n, sec->tsfeedp);
			printk("dvb_demux.c pad data:");
200
			for (i = 0; i < n; i++)
L
Linus Torvalds 已提交
201 202 203 204 205 206 207 208 209 210 211
				printk(" %02x", sec->secbuf[i]);
			printk("\n");
		}
	}
#endif

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

/*
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
 * 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)
L
Linus Torvalds 已提交
231 232 233 234 235
{
	struct dvb_demux *demux = feed->demux;
	struct dmx_section_feed *sec = &feed->feed.sec;
	u16 limit, seclen, n;

236
	if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
L
Linus Torvalds 已提交
237 238
		return 0;

239
	if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
L
Linus Torvalds 已提交
240 241
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
		printk("dvb_demux.c section buffer full loss: %d/%d\n",
242 243
		       sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
		       DMX_MAX_SECFEED_SIZE);
L
Linus Torvalds 已提交
244 245 246 247
#endif
		len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
	}

248
	if (len <= 0)
L
Linus Torvalds 已提交
249 250 251 252 253
		return 0;

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

254 255 256
	/*
	 * Dump all the sections we can find in the data (Emard)
	 */
L
Linus Torvalds 已提交
257
	limit = sec->tsfeedp;
258 259
	if (limit > DMX_MAX_SECFEED_SIZE)
		return -1;	/* internal error should never happen */
L
Linus Torvalds 已提交
260 261 262 263

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

264
	for (n = 0; sec->secbufp + 2 < limit; n++) {
L
Linus Torvalds 已提交
265
		seclen = section_length(sec->secbuf);
266
		if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
267
		    || seclen + sec->secbufp > limit)
L
Linus Torvalds 已提交
268 269 270 271
			return 0;
		sec->seclen = seclen;
		sec->crc_val = ~0;
		/* dump [secbuf .. secbuf+seclen) */
272
		if (feed->pusi_seen)
L
Linus Torvalds 已提交
273 274 275 276 277
			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
278 279
		sec->secbufp += seclen;	/* secbufp and secbuf moving together is */
		sec->secbuf += seclen;	/* redundant but saves pointer arithmetic */
L
Linus Torvalds 已提交
280 281 282 283 284
	}

	return 0;
}

285 286
static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
					   const u8 *buf)
L
Linus Torvalds 已提交
287 288 289 290 291 292 293
{
	u8 p, count;
	int ccok, dc_i = 0;
	u8 cc;

	count = payload(buf);

294
	if (count == 0)		/* count == 0 if no payload or out of range */
L
Linus Torvalds 已提交
295 296
		return -1;

297
	p = 188 - count;	/* payload start */
L
Linus Torvalds 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310

	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
311 312 313 314 315 316
		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
		 */
L
Linus Torvalds 已提交
317
#endif
318 319 320 321
		/*
		 * Discontinuity detected. Reset pusi_seen = 0 to
		 * stop feeding of suspicious data until next PUSI=1 arrives
		 */
L
Linus Torvalds 已提交
322 323 324 325 326
		feed->pusi_seen = 0;
		dvb_dmx_swfilter_section_new(feed);
	}

	if (buf[1] & 0x40) {
327
		/* PUSI=1 (is set), section boundary is here */
L
Linus Torvalds 已提交
328
		if (count > 1 && buf[p] < count) {
329
			const u8 *before = &buf[p + 1];
L
Linus Torvalds 已提交
330
			u8 before_len = buf[p];
331 332
			const u8 *after = &before[before_len];
			u8 after_len = count - 1 - before_len;
L
Linus Torvalds 已提交
333

334 335
			dvb_dmx_swfilter_section_copy_dump(feed, before,
							   before_len);
L
Linus Torvalds 已提交
336 337 338
			/* before start of new section, set pusi_seen = 1 */
			feed->pusi_seen = 1;
			dvb_dmx_swfilter_section_new(feed);
339 340
			dvb_dmx_swfilter_section_copy_dump(feed, after,
							   after_len);
L
Linus Torvalds 已提交
341 342
		}
#ifdef DVB_DEMUX_SECTION_LOSS_LOG
343 344
		else if (count > 0)
			printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
L
Linus Torvalds 已提交
345 346
#endif
	} else {
347 348
		/* PUSI=0 (is not set), no section boundary */
		dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
L
Linus Torvalds 已提交
349
	}
350

L
Linus Torvalds 已提交
351 352 353
	return 0;
}

354 355
static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
						const u8 *buf)
L
Linus Torvalds 已提交
356
{
357
	switch (feed->type) {
L
Linus Torvalds 已提交
358 359 360 361 362 363 364
	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
365 366
				feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
					    DMX_OK);
L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376
		}
		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)
377
			feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387
		break;

	default:
		break;
	}
}

#define DVR_FEED(f)							\
	(((f)->type == DMX_TYPE_TS) &&					\
	((f)->feed.ts.is_filtering) &&					\
388
	(((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
L
Linus Torvalds 已提交
389 390 391 392 393 394 395

static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
{
	struct dvb_demux_feed *feed;
	u16 pid = ts_pid(buf);
	int dvr_done = 0;

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	if (dvb_demux_speedcheck) {
		struct timespec cur_time, delta_time;
		u64 speed_bytes, speed_timedelta;

		demux->speed_pkts_cnt++;

		/* show speed every SPEED_PKTS_INTERVAL packets */
		if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
			cur_time = current_kernel_time();

			if (demux->speed_last_time.tv_sec != 0 &&
					demux->speed_last_time.tv_nsec != 0) {
				delta_time = timespec_sub(cur_time,
						demux->speed_last_time);
				speed_bytes = (u64)demux->speed_pkts_cnt
					* 188 * 8;
				/* convert to 1024 basis */
				speed_bytes = 1000 * div64_u64(speed_bytes,
						1024);
				speed_timedelta =
					(u64)timespec_to_ns(&delta_time);
				speed_timedelta = div64_u64(speed_timedelta,
						1000000); /* nsec -> usec */
				printk(KERN_INFO "TS speed %llu Kbits/sec \n",
						div64_u64(speed_bytes,
							speed_timedelta));
			};

			demux->speed_last_time = cur_time;
			demux->speed_pkts_cnt = 0;
		};
	};

429
	if (demux->cnt_storage && dvb_demux_tscheck) {
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
		/* check pkt counter */
		if (pid < MAX_PID) {
			if (buf[1] & 0x80)
				dprintk_tscheck("TEI detected. "
						"PID=0x%x data1=0x%x\n",
						pid, buf[1]);

			if ((buf[3] & 0xf) != demux->cnt_storage[pid])
				dprintk_tscheck("TS packet counter mismatch. "
						"PID=0x%x expected 0x%x "
						"got 0x%x\n",
						pid, demux->cnt_storage[pid],
						buf[3] & 0xf);

			demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf;
		};
		/* end check */
	};

449
	list_for_each_entry(feed, &demux->feed_list, list_head) {
L
Linus Torvalds 已提交
450 451 452 453 454 455 456 457
		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;

458
		if (feed->pid == pid)
L
Linus Torvalds 已提交
459
			dvb_dmx_swfilter_packet_type(feed, buf);
460
		else if (feed->pid == 0x2000)
L
Linus Torvalds 已提交
461 462 463 464
			feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
	}
}

465 466
void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
			      size_t count)
L
Linus Torvalds 已提交
467
{
468
	spin_lock(&demux->lock);
L
Linus Torvalds 已提交
469 470

	while (count--) {
471 472
		if (buf[0] == 0x47)
			dvb_dmx_swfilter_packet(demux, buf);
L
Linus Torvalds 已提交
473 474 475
		buf += 188;
	}

476
	spin_unlock(&demux->lock);
L
Linus Torvalds 已提交
477 478
}

479
EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
L
Linus Torvalds 已提交
480

481 482
static inline int find_next_packet(const u8 *buf, int pos, size_t count,
				   const int pktsize)
L
Linus Torvalds 已提交
483
{
484
	int start = pos, lost;
L
Linus Torvalds 已提交
485

486 487 488 489 490
	while (pos < count) {
		if (buf[pos] == 0x47 ||
		    (pktsize == 204 && buf[pos] == 0xB8))
			break;
		pos++;
L
Linus Torvalds 已提交
491 492
	}

493 494 495 496 497 498 499
	lost = pos - start;
	if (lost) {
		/* This garbage is part of a valid packet? */
		int backtrack = pos - pktsize;
		if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
		    (pktsize == 204 && buf[backtrack] == 0xB8)))
			return backtrack;
L
Linus Torvalds 已提交
500 501
	}

502
	return pos;
L
Linus Torvalds 已提交
503
}
504

505 506 507
/* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
		size_t count, const int pktsize)
L
Linus Torvalds 已提交
508
{
509
	int p = 0, i, j;
510
	const u8 *q;
511

512
	spin_lock(&demux->lock);
L
Linus Torvalds 已提交
513

514
	if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
515
		i = demux->tsbufp;
516
		j = pktsize - i;
517
		if (count < j) {
L
Linus Torvalds 已提交
518 519 520 521 522
			memcpy(&demux->tsbuf[i], buf, count);
			demux->tsbufp += count;
			goto bailout;
		}
		memcpy(&demux->tsbuf[i], buf, j);
523 524
		if (demux->tsbuf[0] == 0x47) /* double check */
			dvb_dmx_swfilter_packet(demux, demux->tsbuf);
L
Linus Torvalds 已提交
525 526 527 528
		demux->tsbufp = 0;
		p += j;
	}

529 530 531 532 533 534 535 536 537 538 539 540 541
	while (1) {
		p = find_next_packet(buf, p, count, pktsize);
		if (p >= count)
			break;
		if (count - p < pktsize)
			break;

		q = &buf[p];

		if (pktsize == 204 && (*q == 0xB8)) {
			memcpy(demux->tsbuf, q, 188);
			demux->tsbuf[0] = 0x47;
			q = demux->tsbuf;
L
Linus Torvalds 已提交
542
		}
543 544 545 546 547 548 549 550 551 552
		dvb_dmx_swfilter_packet(demux, q);
		p += pktsize;
	}

	i = count - p;
	if (i) {
		memcpy(demux->tsbuf, &buf[p], i);
		demux->tsbufp = i;
		if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
			demux->tsbuf[0] = 0x47;
L
Linus Torvalds 已提交
553 554 555
	}

bailout:
556
	spin_unlock(&demux->lock);
L
Linus Torvalds 已提交
557 558
}

559 560 561 562 563 564 565 566 567 568
void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
{
	_dvb_dmx_swfilter(demux, buf, count, 188);
}
EXPORT_SYMBOL(dvb_dmx_swfilter);

void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
{
	_dvb_dmx_swfilter(demux, buf, count, 204);
}
569
EXPORT_SYMBOL(dvb_dmx_swfilter_204);
L
Linus Torvalds 已提交
570

571 572 573 574 575 576 577 578 579 580
void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
{
	spin_lock(&demux->lock);

	demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts, DMX_OK);

	spin_unlock(&demux->lock);
}
EXPORT_SYMBOL(dvb_dmx_swfilter_raw);

581
static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
L
Linus Torvalds 已提交
582 583 584
{
	int i;

585
	for (i = 0; i < demux->filternum; i++)
L
Linus Torvalds 已提交
586 587 588 589 590 591 592 593 594 595 596
		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];
}

597
static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
L
Linus Torvalds 已提交
598 599 600
{
	int i;

601
	for (i = 0; i < demux->feednum; i++)
L
Linus Torvalds 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		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",
629
		       __func__, feed->type, feed->state, feed->pid);
L
Linus Torvalds 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642
		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",
643
		       __func__, feed->type, feed->state, feed->pid);
L
Linus Torvalds 已提交
644 645 646 647 648 649 650 651
		goto out;
	}

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

652 653 654
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)
L
Linus Torvalds 已提交
655
{
656
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
L
Linus Torvalds 已提交
657 658 659 660 661
	struct dvb_demux *demux = feed->demux;

	if (pid > DMX_MAX_PID)
		return -EINVAL;

662
	if (mutex_lock_interruptible(&demux->mutex))
L
Linus Torvalds 已提交
663 664 665 666
		return -ERESTARTSYS;

	if (ts_type & TS_DECODER) {
		if (pes_type >= DMX_TS_PES_OTHER) {
667
			mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
668 669 670 671 672
			return -EINVAL;
		}

		if (demux->pesfilter[pes_type] &&
		    demux->pesfilter[pes_type] != feed) {
673
			mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
			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
691
		feed->buffer = NULL;
L
Linus Torvalds 已提交
692 693 694
#else
		feed->buffer = vmalloc(feed->buffer_size);
		if (!feed->buffer) {
695
			mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
696 697 698 699 700 701
			return -ENOMEM;
		}
#endif
	}

	feed->state = DMX_STATE_READY;
702
	mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
703 704 705 706

	return 0;
}

707
static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
L
Linus Torvalds 已提交
708
{
709
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
L
Linus Torvalds 已提交
710 711 712
	struct dvb_demux *demux = feed->demux;
	int ret;

713
	if (mutex_lock_interruptible(&demux->mutex))
L
Linus Torvalds 已提交
714 715 716
		return -ERESTARTSYS;

	if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
717
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
718 719 720 721
		return -EINVAL;
	}

	if (!demux->start_feed) {
722
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
723 724 725 726
		return -ENODEV;
	}

	if ((ret = demux->start_feed(feed)) < 0) {
727
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
728 729 730 731 732 733 734
		return ret;
	}

	spin_lock_irq(&demux->lock);
	ts_feed->is_filtering = 1;
	feed->state = DMX_STATE_GO;
	spin_unlock_irq(&demux->lock);
735
	mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
736 737 738 739

	return 0;
}

740
static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
L
Linus Torvalds 已提交
741
{
742
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
L
Linus Torvalds 已提交
743 744 745
	struct dvb_demux *demux = feed->demux;
	int ret;

746
	mutex_lock(&demux->mutex);
L
Linus Torvalds 已提交
747 748

	if (feed->state < DMX_STATE_GO) {
749
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
750 751 752 753
		return -EINVAL;
	}

	if (!demux->stop_feed) {
754
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
755 756 757 758 759 760 761 762 763
		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);
764
	mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
765 766 767 768

	return ret;
}

769 770 771
static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
				   struct dmx_ts_feed **ts_feed,
				   dmx_ts_cb callback)
L
Linus Torvalds 已提交
772
{
773
	struct dvb_demux *demux = (struct dvb_demux *)dmx;
L
Linus Torvalds 已提交
774 775
	struct dvb_demux_feed *feed;

776
	if (mutex_lock_interruptible(&demux->mutex))
L
Linus Torvalds 已提交
777 778 779
		return -ERESTARTSYS;

	if (!(feed = dvb_dmx_feed_alloc(demux))) {
780
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
		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;
801
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
802 803 804 805 806 807 808
		return -EBUSY;
	}

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

809
	mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
810 811 812 813

	return 0;
}

814 815
static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
				  struct dmx_ts_feed *ts_feed)
L
Linus Torvalds 已提交
816
{
817 818
	struct dvb_demux *demux = (struct dvb_demux *)dmx;
	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
L
Linus Torvalds 已提交
819

820
	mutex_lock(&demux->mutex);
L
Linus Torvalds 已提交
821 822

	if (feed->state == DMX_STATE_FREE) {
823
		mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
824 825 826 827
		return -EINVAL;
	}
#ifndef NOBUFS
	vfree(feed->buffer);
828
	feed->buffer = NULL;
L
Linus Torvalds 已提交
829 830 831 832 833 834 835 836 837 838 839 840
#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;

841
	mutex_unlock(&demux->mutex);
L
Linus Torvalds 已提交
842 843 844 845 846 847 848
	return 0;
}

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

849 850
static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
					    struct dmx_section_filter **filter)
L
Linus Torvalds 已提交
851
{
852
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
L
Linus Torvalds 已提交
853 854 855
	struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
	struct dvb_demux_filter *dvbdmxfilter;

856
	if (mutex_lock_interruptible(&dvbdemux->mutex))
L
Linus Torvalds 已提交
857 858 859 860
		return -ERESTARTSYS;

	dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
	if (!dvbdmxfilter) {
861
		mutex_unlock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
862 863 864 865 866 867 868 869 870 871 872 873 874 875
		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);

876
	mutex_unlock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
877 878 879
	return 0;
}

880 881 882
static int dmx_section_feed_set(struct dmx_section_feed *feed,
				u16 pid, size_t circular_buffer_size,
				int check_crc)
L
Linus Torvalds 已提交
883
{
884
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
L
Linus Torvalds 已提交
885 886 887 888 889
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;

	if (pid > 0x1fff)
		return -EINVAL;

890
	if (mutex_lock_interruptible(&dvbdmx->mutex))
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899 900 901
		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
902
	dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
L
Linus Torvalds 已提交
903
	if (!dvbdmxfeed->buffer) {
904
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
905 906 907 908 909
		return -ENOMEM;
	}
#endif

	dvbdmxfeed->state = DMX_STATE_READY;
910
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
911 912 913 914 915 916 917 918 919 920
	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;

921
	if (!(f = dvbdmxfeed->filter))
L
Linus Torvalds 已提交
922 923 924 925
		return;
	do {
		sf = &f->filter;
		doneq = 0;
926
		for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
L
Linus Torvalds 已提交
927 928 929 930 931 932 933 934 935 936 937
			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)
{
938
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
L
Linus Torvalds 已提交
939 940 941
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	int ret;

942
	if (mutex_lock_interruptible(&dvbdmx->mutex))
L
Linus Torvalds 已提交
943 944 945
		return -ERESTARTSYS;

	if (feed->is_filtering) {
946
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
947 948 949 950
		return -EBUSY;
	}

	if (!dvbdmxfeed->filter) {
951
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
952 953 954 955 956 957 958 959 960
		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) {
961
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
962 963 964 965 966 967
		return -ENODEV;
	}

	prepare_secfilters(dvbdmxfeed);

	if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
968
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
969 970 971 972 973 974 975 976
		return ret;
	}

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

977
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
978 979 980
	return 0;
}

981
static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
L
Linus Torvalds 已提交
982
{
983
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
L
Linus Torvalds 已提交
984 985 986
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
	int ret;

987
	mutex_lock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
988 989

	if (!dvbdmx->stop_feed) {
990
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
991 992 993 994 995 996 997 998 999 1000
		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);

1001
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1002 1003 1004 1005
	return ret;
}

static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
1006
					   struct dmx_section_filter *filter)
L
Linus Torvalds 已提交
1007
{
1008 1009
	struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
L
Linus Torvalds 已提交
1010 1011
	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;

1012
	mutex_lock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1013 1014

	if (dvbdmxfilter->feed != dvbdmxfeed) {
1015
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		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 {
1028
		while (f->next != dvbdmxfilter)
L
Linus Torvalds 已提交
1029 1030 1031 1032 1033 1034
			f = f->next;
		f->next = f->next->next;
	}

	dvbdmxfilter->state = DMX_STATE_FREE;
	spin_unlock_irq(&dvbdmx->lock);
1035
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1036 1037 1038 1039 1040 1041 1042
	return 0;
}

static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
					struct dmx_section_feed **feed,
					dmx_section_cb callback)
{
1043
	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1044 1045
	struct dvb_demux_feed *dvbdmxfeed;

1046
	if (mutex_lock_interruptible(&dvbdmx->mutex))
L
Linus Torvalds 已提交
1047 1048 1049
		return -ERESTARTSYS;

	if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1050
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
		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;

1064
	(*feed) = &dvbdmxfeed->feed.sec;
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	(*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;

1075
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081
	return 0;
}

static int dvbdmx_release_section_feed(struct dmx_demux *demux,
				       struct dmx_section_feed *feed)
{
1082 1083
	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1084

1085
	mutex_lock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1086

1087
	if (dvbdmxfeed->state == DMX_STATE_FREE) {
1088
		mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1089 1090 1091 1092
		return -EINVAL;
	}
#ifndef NOBUFS
	vfree(dvbdmxfeed->buffer);
1093
	dvbdmxfeed->buffer = NULL;
L
Linus Torvalds 已提交
1094
#endif
1095
	dvbdmxfeed->state = DMX_STATE_FREE;
L
Linus Torvalds 已提交
1096 1097 1098 1099 1100

	dvb_demux_feed_del(dvbdmxfeed);

	dvbdmxfeed->pid = 0xffff;

1101
	mutex_unlock(&dvbdmx->mutex);
L
Linus Torvalds 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110
	return 0;
}

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

static int dvbdmx_open(struct dmx_demux *demux)
{
1111
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

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

	dvbdemux->users++;
	return 0;
}

static int dvbdmx_close(struct dmx_demux *demux)
{
1122
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1123 1124 1125 1126 1127 1128 1129 1130 1131

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

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

1132
static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
L
Linus Torvalds 已提交
1133
{
1134
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1135
	void *p;
L
Linus Torvalds 已提交
1136 1137 1138 1139

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

1140 1141 1142
	p = memdup_user(buf, count);
	if (IS_ERR(p))
		return PTR_ERR(p);
1143 1144
	if (mutex_lock_interruptible(&dvbdemux->mutex)) {
		kfree(p);
L
Linus Torvalds 已提交
1145
		return -ERESTARTSYS;
1146 1147 1148
	}
	dvb_dmx_swfilter(dvbdemux, p, count);
	kfree(p);
1149
	mutex_unlock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1150 1151 1152 1153 1154 1155

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

1156 1157
static int dvbdmx_add_frontend(struct dmx_demux *demux,
			       struct dmx_frontend *frontend)
L
Linus Torvalds 已提交
1158
{
1159
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1160 1161 1162 1163 1164 1165 1166
	struct list_head *head = &dvbdemux->frontend_list;

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

	return 0;
}

1167 1168
static int dvbdmx_remove_frontend(struct dmx_demux *demux,
				  struct dmx_frontend *frontend)
L
Linus Torvalds 已提交
1169
{
1170
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1171 1172
	struct list_head *pos, *n, *head = &dvbdemux->frontend_list;

1173
	list_for_each_safe(pos, n, head) {
L
Linus Torvalds 已提交
1174 1175 1176 1177 1178 1179 1180 1181 1182
		if (DMX_FE_ENTRY(pos) == frontend) {
			list_del(pos);
			return 0;
		}
	}

	return -ENODEV;
}

1183
static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
L
Linus Torvalds 已提交
1184
{
1185
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1186 1187 1188

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

L
Linus Torvalds 已提交
1190 1191 1192
	return &dvbdemux->frontend_list;
}

1193 1194
static int dvbdmx_connect_frontend(struct dmx_demux *demux,
				   struct dmx_frontend *frontend)
L
Linus Torvalds 已提交
1195
{
1196
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1197 1198 1199 1200

	if (demux->frontend)
		return -EINVAL;

1201
	mutex_lock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1202 1203

	demux->frontend = frontend;
1204
	mutex_unlock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209
	return 0;
}

static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
{
1210
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1211

1212
	mutex_lock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1213 1214

	demux->frontend = NULL;
1215
	mutex_unlock(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1216 1217 1218
	return 0;
}

1219
static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
L
Linus Torvalds 已提交
1220
{
1221
	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
L
Linus Torvalds 已提交
1222

1223
	memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228
	return 0;
}

int dvb_dmx_init(struct dvb_demux *dvbdemux)
{
1229
	int i;
L
Linus Torvalds 已提交
1230 1231
	struct dmx_demux *dmx = &dvbdemux->dmx;

1232
	dvbdemux->cnt_storage = NULL;
L
Linus Torvalds 已提交
1233
	dvbdemux->users = 0;
1234
	dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
L
Linus Torvalds 已提交
1235 1236 1237 1238

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

1239
	dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
L
Linus Torvalds 已提交
1240 1241
	if (!dvbdemux->feed) {
		vfree(dvbdemux->filter);
1242
		dvbdemux->filter = NULL;
L
Linus Torvalds 已提交
1243 1244
		return -ENOMEM;
	}
1245
	for (i = 0; i < dvbdemux->filternum; i++) {
L
Linus Torvalds 已提交
1246 1247 1248
		dvbdemux->filter[i].state = DMX_STATE_FREE;
		dvbdemux->filter[i].index = i;
	}
1249
	for (i = 0; i < dvbdemux->feednum; i++) {
L
Linus Torvalds 已提交
1250 1251 1252
		dvbdemux->feed[i].state = DMX_STATE_FREE;
		dvbdemux->feed[i].index = i;
	}
1253

1254 1255 1256
	dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
	if (!dvbdemux->cnt_storage)
		printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
1257

1258 1259
	INIT_LIST_HEAD(&dvbdemux->frontend_list);

1260
	for (i = 0; i < DMX_TS_PES_OTHER; i++) {
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
		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;

1274 1275
	if (!dvbdemux->memcopy)
		dvbdemux->memcopy = dvb_dmx_memcopy;
L
Linus Torvalds 已提交
1276 1277

	dmx->frontend = NULL;
1278
	dmx->priv = dvbdemux;
L
Linus Torvalds 已提交
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	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;

1294
	mutex_init(&dvbdemux->mutex);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299
	spin_lock_init(&dvbdemux->lock);

	return 0;
}

1300
EXPORT_SYMBOL(dvb_dmx_init);
L
Linus Torvalds 已提交
1301

1302
void dvb_dmx_release(struct dvb_demux *dvbdemux)
L
Linus Torvalds 已提交
1303
{
1304
	vfree(dvbdemux->cnt_storage);
L
Linus Torvalds 已提交
1305 1306 1307
	vfree(dvbdemux->filter);
	vfree(dvbdemux->feed);
}
1308

L
Linus Torvalds 已提交
1309
EXPORT_SYMBOL(dvb_dmx_release);