xref: /linux/drivers/media/dvb-core/dvb_demux.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * dvb_demux.c - DVB kernel demux API
3  *
4  * Copyright (C) 2000-2001 Ralph  Metzler <ralph@convergence.de>
5  *		       & Marcus Metzler <marcus@convergence.de>
6  *			 for convergence integrated media GmbH
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23 
24 #include <linux/sched.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/string.h>
31 #include <linux/crc32.h>
32 #include <asm/uaccess.h>
33 #include <asm/div64.h>
34 
35 #include "dvb_demux.h"
36 
37 #define NOBUFS
38 /*
39 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
40 */
41 // #define DVB_DEMUX_SECTION_LOSS_LOG
42 
43 static int dvb_demux_tscheck;
44 module_param(dvb_demux_tscheck, int, 0644);
45 MODULE_PARM_DESC(dvb_demux_tscheck,
46 		"enable transport stream continuity and TEI check");
47 
48 static int dvb_demux_speedcheck;
49 module_param(dvb_demux_speedcheck, int, 0644);
50 MODULE_PARM_DESC(dvb_demux_speedcheck,
51 		"enable transport stream speed check");
52 
53 static int dvb_demux_feed_err_pkts = 1;
54 module_param(dvb_demux_feed_err_pkts, int, 0644);
55 MODULE_PARM_DESC(dvb_demux_feed_err_pkts,
56 		 "when set to 0, drop packets with the TEI bit set (1 by default)");
57 
58 #define dprintk_tscheck(x...) do {                              \
59 		if (dvb_demux_tscheck && printk_ratelimit())    \
60 			printk(x);                              \
61 	} while (0)
62 
63 /******************************************************************************
64  * static inlined helper functions
65  ******************************************************************************/
66 
67 static inline u16 section_length(const u8 *buf)
68 {
69 	return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
70 }
71 
72 static inline u16 ts_pid(const u8 *buf)
73 {
74 	return ((buf[1] & 0x1f) << 8) + buf[2];
75 }
76 
77 static inline u8 payload(const u8 *tsp)
78 {
79 	if (!(tsp[3] & 0x10))	// no payload?
80 		return 0;
81 
82 	if (tsp[3] & 0x20) {	// adaptation field?
83 		if (tsp[4] > 183)	// corrupted data?
84 			return 0;
85 		else
86 			return 184 - 1 - tsp[4];
87 	}
88 
89 	return 184;
90 }
91 
92 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
93 {
94 	return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
95 }
96 
97 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
98 			    size_t len)
99 {
100 	memcpy(d, s, len);
101 }
102 
103 /******************************************************************************
104  * Software filter functions
105  ******************************************************************************/
106 
107 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
108 					   const u8 *buf)
109 {
110 	int count = payload(buf);
111 	int p;
112 	//int ccok;
113 	//u8 cc;
114 
115 	if (count == 0)
116 		return -1;
117 
118 	p = 188 - count;
119 
120 	/*
121 	cc = buf[3] & 0x0f;
122 	ccok = ((feed->cc + 1) & 0x0f) == cc;
123 	feed->cc = cc;
124 	if (!ccok)
125 		printk("missed packet!\n");
126 	*/
127 
128 	if (buf[1] & 0x40)	// PUSI ?
129 		feed->peslen = 0xfffa;
130 
131 	feed->peslen += count;
132 
133 	return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
134 }
135 
136 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
137 					  struct dvb_demux_filter *f)
138 {
139 	u8 neq = 0;
140 	int i;
141 
142 	for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
143 		u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
144 
145 		if (f->maskandmode[i] & xor)
146 			return 0;
147 
148 		neq |= f->maskandnotmode[i] & xor;
149 	}
150 
151 	if (f->doneq && !neq)
152 		return 0;
153 
154 	return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
155 			    NULL, 0, &f->filter, DMX_OK);
156 }
157 
158 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
159 {
160 	struct dvb_demux *demux = feed->demux;
161 	struct dvb_demux_filter *f = feed->filter;
162 	struct dmx_section_feed *sec = &feed->feed.sec;
163 	int section_syntax_indicator;
164 
165 	if (!sec->is_filtering)
166 		return 0;
167 
168 	if (!f)
169 		return 0;
170 
171 	if (sec->check_crc) {
172 		section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
173 		if (section_syntax_indicator &&
174 		    demux->check_crc32(feed, sec->secbuf, sec->seclen))
175 			return -1;
176 	}
177 
178 	do {
179 		if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
180 			return -1;
181 	} while ((f = f->next) && sec->is_filtering);
182 
183 	sec->seclen = 0;
184 
185 	return 0;
186 }
187 
188 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
189 {
190 	struct dmx_section_feed *sec = &feed->feed.sec;
191 
192 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
193 	if (sec->secbufp < sec->tsfeedp) {
194 		int i, n = sec->tsfeedp - sec->secbufp;
195 
196 		/*
197 		 * Section padding is done with 0xff bytes entirely.
198 		 * Due to speed reasons, we won't check all of them
199 		 * but just first and last.
200 		 */
201 		if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
202 			printk("dvb_demux.c section ts padding loss: %d/%d\n",
203 			       n, sec->tsfeedp);
204 			printk("dvb_demux.c pad data:");
205 			for (i = 0; i < n; i++)
206 				printk(" %02x", sec->secbuf[i]);
207 			printk("\n");
208 		}
209 	}
210 #endif
211 
212 	sec->tsfeedp = sec->secbufp = sec->seclen = 0;
213 	sec->secbuf = sec->secbuf_base;
214 }
215 
216 /*
217  * Losless Section Demux 1.4.1 by Emard
218  * Valsecchi Patrick:
219  *  - middle of section A  (no PUSI)
220  *  - end of section A and start of section B
221  *    (with PUSI pointing to the start of the second section)
222  *
223  *  In this case, without feed->pusi_seen you'll receive a garbage section
224  *  consisting of the end of section A. Basically because tsfeedp
225  *  is incemented and the use=0 condition is not raised
226  *  when the second packet arrives.
227  *
228  * Fix:
229  * when demux is started, let feed->pusi_seen = 0 to
230  * prevent initial feeding of garbage from the end of
231  * previous section. When you for the first time see PUSI=1
232  * then set feed->pusi_seen = 1
233  */
234 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
235 					      const u8 *buf, u8 len)
236 {
237 	struct dvb_demux *demux = feed->demux;
238 	struct dmx_section_feed *sec = &feed->feed.sec;
239 	u16 limit, seclen, n;
240 
241 	if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
242 		return 0;
243 
244 	if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
245 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
246 		printk("dvb_demux.c section buffer full loss: %d/%d\n",
247 		       sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
248 		       DMX_MAX_SECFEED_SIZE);
249 #endif
250 		len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
251 	}
252 
253 	if (len <= 0)
254 		return 0;
255 
256 	demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
257 	sec->tsfeedp += len;
258 
259 	/*
260 	 * Dump all the sections we can find in the data (Emard)
261 	 */
262 	limit = sec->tsfeedp;
263 	if (limit > DMX_MAX_SECFEED_SIZE)
264 		return -1;	/* internal error should never happen */
265 
266 	/* to be sure always set secbuf */
267 	sec->secbuf = sec->secbuf_base + sec->secbufp;
268 
269 	for (n = 0; sec->secbufp + 2 < limit; n++) {
270 		seclen = section_length(sec->secbuf);
271 		if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
272 		    || seclen + sec->secbufp > limit)
273 			return 0;
274 		sec->seclen = seclen;
275 		sec->crc_val = ~0;
276 		/* dump [secbuf .. secbuf+seclen) */
277 		if (feed->pusi_seen)
278 			dvb_dmx_swfilter_section_feed(feed);
279 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
280 		else
281 			printk("dvb_demux.c pusi not seen, discarding section data\n");
282 #endif
283 		sec->secbufp += seclen;	/* secbufp and secbuf moving together is */
284 		sec->secbuf += seclen;	/* redundant but saves pointer arithmetic */
285 	}
286 
287 	return 0;
288 }
289 
290 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
291 					   const u8 *buf)
292 {
293 	u8 p, count;
294 	int ccok, dc_i = 0;
295 	u8 cc;
296 
297 	count = payload(buf);
298 
299 	if (count == 0)		/* count == 0 if no payload or out of range */
300 		return -1;
301 
302 	p = 188 - count;	/* payload start */
303 
304 	cc = buf[3] & 0x0f;
305 	ccok = ((feed->cc + 1) & 0x0f) == cc;
306 	feed->cc = cc;
307 
308 	if (buf[3] & 0x20) {
309 		/* adaption field present, check for discontinuity_indicator */
310 		if ((buf[4] > 0) && (buf[5] & 0x80))
311 			dc_i = 1;
312 	}
313 
314 	if (!ccok || dc_i) {
315 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
316 		printk("dvb_demux.c discontinuity detected %d bytes lost\n",
317 		       count);
318 		/*
319 		 * those bytes under sume circumstances will again be reported
320 		 * in the following dvb_dmx_swfilter_section_new
321 		 */
322 #endif
323 		/*
324 		 * Discontinuity detected. Reset pusi_seen = 0 to
325 		 * stop feeding of suspicious data until next PUSI=1 arrives
326 		 */
327 		feed->pusi_seen = 0;
328 		dvb_dmx_swfilter_section_new(feed);
329 	}
330 
331 	if (buf[1] & 0x40) {
332 		/* PUSI=1 (is set), section boundary is here */
333 		if (count > 1 && buf[p] < count) {
334 			const u8 *before = &buf[p + 1];
335 			u8 before_len = buf[p];
336 			const u8 *after = &before[before_len];
337 			u8 after_len = count - 1 - before_len;
338 
339 			dvb_dmx_swfilter_section_copy_dump(feed, before,
340 							   before_len);
341 			/* before start of new section, set pusi_seen = 1 */
342 			feed->pusi_seen = 1;
343 			dvb_dmx_swfilter_section_new(feed);
344 			dvb_dmx_swfilter_section_copy_dump(feed, after,
345 							   after_len);
346 		}
347 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
348 		else if (count > 0)
349 			printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
350 #endif
351 	} else {
352 		/* PUSI=0 (is not set), no section boundary */
353 		dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
354 	}
355 
356 	return 0;
357 }
358 
359 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
360 						const u8 *buf)
361 {
362 	switch (feed->type) {
363 	case DMX_TYPE_TS:
364 		if (!feed->feed.ts.is_filtering)
365 			break;
366 		if (feed->ts_type & TS_PACKET) {
367 			if (feed->ts_type & TS_PAYLOAD_ONLY)
368 				dvb_dmx_swfilter_payload(feed, buf);
369 			else
370 				feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
371 					    DMX_OK);
372 		}
373 		if (feed->ts_type & TS_DECODER)
374 			if (feed->demux->write_to_decoder)
375 				feed->demux->write_to_decoder(feed, buf, 188);
376 		break;
377 
378 	case DMX_TYPE_SEC:
379 		if (!feed->feed.sec.is_filtering)
380 			break;
381 		if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
382 			feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
383 		break;
384 
385 	default:
386 		break;
387 	}
388 }
389 
390 #define DVR_FEED(f)							\
391 	(((f)->type == DMX_TYPE_TS) &&					\
392 	((f)->feed.ts.is_filtering) &&					\
393 	(((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
394 
395 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
396 {
397 	struct dvb_demux_feed *feed;
398 	u16 pid = ts_pid(buf);
399 	int dvr_done = 0;
400 
401 	if (dvb_demux_speedcheck) {
402 		struct timespec cur_time, delta_time;
403 		u64 speed_bytes, speed_timedelta;
404 
405 		demux->speed_pkts_cnt++;
406 
407 		/* show speed every SPEED_PKTS_INTERVAL packets */
408 		if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
409 			cur_time = current_kernel_time();
410 
411 			if (demux->speed_last_time.tv_sec != 0 &&
412 					demux->speed_last_time.tv_nsec != 0) {
413 				delta_time = timespec_sub(cur_time,
414 						demux->speed_last_time);
415 				speed_bytes = (u64)demux->speed_pkts_cnt
416 					* 188 * 8;
417 				/* convert to 1024 basis */
418 				speed_bytes = 1000 * div64_u64(speed_bytes,
419 						1024);
420 				speed_timedelta =
421 					(u64)timespec_to_ns(&delta_time);
422 				speed_timedelta = div64_u64(speed_timedelta,
423 						1000000); /* nsec -> usec */
424 				printk(KERN_INFO "TS speed %llu Kbits/sec \n",
425 						div64_u64(speed_bytes,
426 							speed_timedelta));
427 			}
428 
429 			demux->speed_last_time = cur_time;
430 			demux->speed_pkts_cnt = 0;
431 		}
432 	}
433 
434 	if (buf[1] & 0x80) {
435 		dprintk_tscheck("TEI detected. "
436 				"PID=0x%x data1=0x%x\n",
437 				pid, buf[1]);
438 		/* data in this packet can't be trusted - drop it unless
439 		 * module option dvb_demux_feed_err_pkts is set */
440 		if (!dvb_demux_feed_err_pkts)
441 			return;
442 	} else /* if TEI bit is set, pid may be wrong- skip pkt counter */
443 		if (demux->cnt_storage && dvb_demux_tscheck) {
444 			/* check pkt counter */
445 			if (pid < MAX_PID) {
446 				if (buf[3] & 0x10)
447 					demux->cnt_storage[pid] =
448 						(demux->cnt_storage[pid] + 1) & 0xf;
449 
450 				if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
451 					dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
452 						pid, demux->cnt_storage[pid],
453 						buf[3] & 0xf);
454 					demux->cnt_storage[pid] = buf[3] & 0xf;
455 				}
456 			}
457 			/* end check */
458 		}
459 
460 	list_for_each_entry(feed, &demux->feed_list, list_head) {
461 		if ((feed->pid != pid) && (feed->pid != 0x2000))
462 			continue;
463 
464 		/* copy each packet only once to the dvr device, even
465 		 * if a PID is in multiple filters (e.g. video + PCR) */
466 		if ((DVR_FEED(feed)) && (dvr_done++))
467 			continue;
468 
469 		if (feed->pid == pid)
470 			dvb_dmx_swfilter_packet_type(feed, buf);
471 		else if (feed->pid == 0x2000)
472 			feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
473 	}
474 }
475 
476 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
477 			      size_t count)
478 {
479 	unsigned long flags;
480 
481 	spin_lock_irqsave(&demux->lock, flags);
482 
483 	while (count--) {
484 		if (buf[0] == 0x47)
485 			dvb_dmx_swfilter_packet(demux, buf);
486 		buf += 188;
487 	}
488 
489 	spin_unlock_irqrestore(&demux->lock, flags);
490 }
491 
492 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
493 
494 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
495 				   const int pktsize)
496 {
497 	int start = pos, lost;
498 
499 	while (pos < count) {
500 		if (buf[pos] == 0x47 ||
501 		    (pktsize == 204 && buf[pos] == 0xB8))
502 			break;
503 		pos++;
504 	}
505 
506 	lost = pos - start;
507 	if (lost) {
508 		/* This garbage is part of a valid packet? */
509 		int backtrack = pos - pktsize;
510 		if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
511 		    (pktsize == 204 && buf[backtrack] == 0xB8)))
512 			return backtrack;
513 	}
514 
515 	return pos;
516 }
517 
518 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
519 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
520 		size_t count, const int pktsize)
521 {
522 	int p = 0, i, j;
523 	const u8 *q;
524 	unsigned long flags;
525 
526 	spin_lock_irqsave(&demux->lock, flags);
527 
528 	if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
529 		i = demux->tsbufp;
530 		j = pktsize - i;
531 		if (count < j) {
532 			memcpy(&demux->tsbuf[i], buf, count);
533 			demux->tsbufp += count;
534 			goto bailout;
535 		}
536 		memcpy(&demux->tsbuf[i], buf, j);
537 		if (demux->tsbuf[0] == 0x47) /* double check */
538 			dvb_dmx_swfilter_packet(demux, demux->tsbuf);
539 		demux->tsbufp = 0;
540 		p += j;
541 	}
542 
543 	while (1) {
544 		p = find_next_packet(buf, p, count, pktsize);
545 		if (p >= count)
546 			break;
547 		if (count - p < pktsize)
548 			break;
549 
550 		q = &buf[p];
551 
552 		if (pktsize == 204 && (*q == 0xB8)) {
553 			memcpy(demux->tsbuf, q, 188);
554 			demux->tsbuf[0] = 0x47;
555 			q = demux->tsbuf;
556 		}
557 		dvb_dmx_swfilter_packet(demux, q);
558 		p += pktsize;
559 	}
560 
561 	i = count - p;
562 	if (i) {
563 		memcpy(demux->tsbuf, &buf[p], i);
564 		demux->tsbufp = i;
565 		if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
566 			demux->tsbuf[0] = 0x47;
567 	}
568 
569 bailout:
570 	spin_unlock_irqrestore(&demux->lock, flags);
571 }
572 
573 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
574 {
575 	_dvb_dmx_swfilter(demux, buf, count, 188);
576 }
577 EXPORT_SYMBOL(dvb_dmx_swfilter);
578 
579 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
580 {
581 	_dvb_dmx_swfilter(demux, buf, count, 204);
582 }
583 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
584 
585 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
586 {
587 	unsigned long flags;
588 
589 	spin_lock_irqsave(&demux->lock, flags);
590 
591 	demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts, DMX_OK);
592 
593 	spin_unlock_irqrestore(&demux->lock, flags);
594 }
595 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
596 
597 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
598 {
599 	int i;
600 
601 	for (i = 0; i < demux->filternum; i++)
602 		if (demux->filter[i].state == DMX_STATE_FREE)
603 			break;
604 
605 	if (i == demux->filternum)
606 		return NULL;
607 
608 	demux->filter[i].state = DMX_STATE_ALLOCATED;
609 
610 	return &demux->filter[i];
611 }
612 
613 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
614 {
615 	int i;
616 
617 	for (i = 0; i < demux->feednum; i++)
618 		if (demux->feed[i].state == DMX_STATE_FREE)
619 			break;
620 
621 	if (i == demux->feednum)
622 		return NULL;
623 
624 	demux->feed[i].state = DMX_STATE_ALLOCATED;
625 
626 	return &demux->feed[i];
627 }
628 
629 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
630 {
631 	struct dvb_demux_feed *entry;
632 
633 	list_for_each_entry(entry, &feed->demux->feed_list, list_head)
634 		if (entry == feed)
635 			return 1;
636 
637 	return 0;
638 }
639 
640 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
641 {
642 	spin_lock_irq(&feed->demux->lock);
643 	if (dvb_demux_feed_find(feed)) {
644 		printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
645 		       __func__, feed->type, feed->state, feed->pid);
646 		goto out;
647 	}
648 
649 	list_add(&feed->list_head, &feed->demux->feed_list);
650 out:
651 	spin_unlock_irq(&feed->demux->lock);
652 }
653 
654 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
655 {
656 	spin_lock_irq(&feed->demux->lock);
657 	if (!(dvb_demux_feed_find(feed))) {
658 		printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
659 		       __func__, feed->type, feed->state, feed->pid);
660 		goto out;
661 	}
662 
663 	list_del(&feed->list_head);
664 out:
665 	spin_unlock_irq(&feed->demux->lock);
666 }
667 
668 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
669 			   enum dmx_ts_pes pes_type,
670 			   size_t circular_buffer_size, struct timespec timeout)
671 {
672 	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
673 	struct dvb_demux *demux = feed->demux;
674 
675 	if (pid > DMX_MAX_PID)
676 		return -EINVAL;
677 
678 	if (mutex_lock_interruptible(&demux->mutex))
679 		return -ERESTARTSYS;
680 
681 	if (ts_type & TS_DECODER) {
682 		if (pes_type >= DMX_PES_OTHER) {
683 			mutex_unlock(&demux->mutex);
684 			return -EINVAL;
685 		}
686 
687 		if (demux->pesfilter[pes_type] &&
688 		    demux->pesfilter[pes_type] != feed) {
689 			mutex_unlock(&demux->mutex);
690 			return -EINVAL;
691 		}
692 
693 		demux->pesfilter[pes_type] = feed;
694 		demux->pids[pes_type] = pid;
695 	}
696 
697 	dvb_demux_feed_add(feed);
698 
699 	feed->pid = pid;
700 	feed->buffer_size = circular_buffer_size;
701 	feed->timeout = timeout;
702 	feed->ts_type = ts_type;
703 	feed->pes_type = pes_type;
704 
705 	if (feed->buffer_size) {
706 #ifdef NOBUFS
707 		feed->buffer = NULL;
708 #else
709 		feed->buffer = vmalloc(feed->buffer_size);
710 		if (!feed->buffer) {
711 			mutex_unlock(&demux->mutex);
712 			return -ENOMEM;
713 		}
714 #endif
715 	}
716 
717 	feed->state = DMX_STATE_READY;
718 	mutex_unlock(&demux->mutex);
719 
720 	return 0;
721 }
722 
723 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
724 {
725 	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
726 	struct dvb_demux *demux = feed->demux;
727 	int ret;
728 
729 	if (mutex_lock_interruptible(&demux->mutex))
730 		return -ERESTARTSYS;
731 
732 	if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
733 		mutex_unlock(&demux->mutex);
734 		return -EINVAL;
735 	}
736 
737 	if (!demux->start_feed) {
738 		mutex_unlock(&demux->mutex);
739 		return -ENODEV;
740 	}
741 
742 	if ((ret = demux->start_feed(feed)) < 0) {
743 		mutex_unlock(&demux->mutex);
744 		return ret;
745 	}
746 
747 	spin_lock_irq(&demux->lock);
748 	ts_feed->is_filtering = 1;
749 	feed->state = DMX_STATE_GO;
750 	spin_unlock_irq(&demux->lock);
751 	mutex_unlock(&demux->mutex);
752 
753 	return 0;
754 }
755 
756 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
757 {
758 	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
759 	struct dvb_demux *demux = feed->demux;
760 	int ret;
761 
762 	mutex_lock(&demux->mutex);
763 
764 	if (feed->state < DMX_STATE_GO) {
765 		mutex_unlock(&demux->mutex);
766 		return -EINVAL;
767 	}
768 
769 	if (!demux->stop_feed) {
770 		mutex_unlock(&demux->mutex);
771 		return -ENODEV;
772 	}
773 
774 	ret = demux->stop_feed(feed);
775 
776 	spin_lock_irq(&demux->lock);
777 	ts_feed->is_filtering = 0;
778 	feed->state = DMX_STATE_ALLOCATED;
779 	spin_unlock_irq(&demux->lock);
780 	mutex_unlock(&demux->mutex);
781 
782 	return ret;
783 }
784 
785 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
786 				   struct dmx_ts_feed **ts_feed,
787 				   dmx_ts_cb callback)
788 {
789 	struct dvb_demux *demux = (struct dvb_demux *)dmx;
790 	struct dvb_demux_feed *feed;
791 
792 	if (mutex_lock_interruptible(&demux->mutex))
793 		return -ERESTARTSYS;
794 
795 	if (!(feed = dvb_dmx_feed_alloc(demux))) {
796 		mutex_unlock(&demux->mutex);
797 		return -EBUSY;
798 	}
799 
800 	feed->type = DMX_TYPE_TS;
801 	feed->cb.ts = callback;
802 	feed->demux = demux;
803 	feed->pid = 0xffff;
804 	feed->peslen = 0xfffa;
805 	feed->buffer = NULL;
806 
807 	(*ts_feed) = &feed->feed.ts;
808 	(*ts_feed)->parent = dmx;
809 	(*ts_feed)->priv = NULL;
810 	(*ts_feed)->is_filtering = 0;
811 	(*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
812 	(*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
813 	(*ts_feed)->set = dmx_ts_feed_set;
814 
815 	if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
816 		feed->state = DMX_STATE_FREE;
817 		mutex_unlock(&demux->mutex);
818 		return -EBUSY;
819 	}
820 
821 	feed->filter->type = DMX_TYPE_TS;
822 	feed->filter->feed = feed;
823 	feed->filter->state = DMX_STATE_READY;
824 
825 	mutex_unlock(&demux->mutex);
826 
827 	return 0;
828 }
829 
830 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
831 				  struct dmx_ts_feed *ts_feed)
832 {
833 	struct dvb_demux *demux = (struct dvb_demux *)dmx;
834 	struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
835 
836 	mutex_lock(&demux->mutex);
837 
838 	if (feed->state == DMX_STATE_FREE) {
839 		mutex_unlock(&demux->mutex);
840 		return -EINVAL;
841 	}
842 #ifndef NOBUFS
843 	vfree(feed->buffer);
844 	feed->buffer = NULL;
845 #endif
846 
847 	feed->state = DMX_STATE_FREE;
848 	feed->filter->state = DMX_STATE_FREE;
849 
850 	dvb_demux_feed_del(feed);
851 
852 	feed->pid = 0xffff;
853 
854 	if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
855 		demux->pesfilter[feed->pes_type] = NULL;
856 
857 	mutex_unlock(&demux->mutex);
858 	return 0;
859 }
860 
861 /******************************************************************************
862  * dmx_section_feed API calls
863  ******************************************************************************/
864 
865 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
866 					    struct dmx_section_filter **filter)
867 {
868 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
869 	struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
870 	struct dvb_demux_filter *dvbdmxfilter;
871 
872 	if (mutex_lock_interruptible(&dvbdemux->mutex))
873 		return -ERESTARTSYS;
874 
875 	dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
876 	if (!dvbdmxfilter) {
877 		mutex_unlock(&dvbdemux->mutex);
878 		return -EBUSY;
879 	}
880 
881 	spin_lock_irq(&dvbdemux->lock);
882 	*filter = &dvbdmxfilter->filter;
883 	(*filter)->parent = feed;
884 	(*filter)->priv = NULL;
885 	dvbdmxfilter->feed = dvbdmxfeed;
886 	dvbdmxfilter->type = DMX_TYPE_SEC;
887 	dvbdmxfilter->state = DMX_STATE_READY;
888 	dvbdmxfilter->next = dvbdmxfeed->filter;
889 	dvbdmxfeed->filter = dvbdmxfilter;
890 	spin_unlock_irq(&dvbdemux->lock);
891 
892 	mutex_unlock(&dvbdemux->mutex);
893 	return 0;
894 }
895 
896 static int dmx_section_feed_set(struct dmx_section_feed *feed,
897 				u16 pid, size_t circular_buffer_size,
898 				int check_crc)
899 {
900 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
901 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
902 
903 	if (pid > 0x1fff)
904 		return -EINVAL;
905 
906 	if (mutex_lock_interruptible(&dvbdmx->mutex))
907 		return -ERESTARTSYS;
908 
909 	dvb_demux_feed_add(dvbdmxfeed);
910 
911 	dvbdmxfeed->pid = pid;
912 	dvbdmxfeed->buffer_size = circular_buffer_size;
913 	dvbdmxfeed->feed.sec.check_crc = check_crc;
914 
915 #ifdef NOBUFS
916 	dvbdmxfeed->buffer = NULL;
917 #else
918 	dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
919 	if (!dvbdmxfeed->buffer) {
920 		mutex_unlock(&dvbdmx->mutex);
921 		return -ENOMEM;
922 	}
923 #endif
924 
925 	dvbdmxfeed->state = DMX_STATE_READY;
926 	mutex_unlock(&dvbdmx->mutex);
927 	return 0;
928 }
929 
930 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
931 {
932 	int i;
933 	struct dvb_demux_filter *f;
934 	struct dmx_section_filter *sf;
935 	u8 mask, mode, doneq;
936 
937 	if (!(f = dvbdmxfeed->filter))
938 		return;
939 	do {
940 		sf = &f->filter;
941 		doneq = 0;
942 		for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
943 			mode = sf->filter_mode[i];
944 			mask = sf->filter_mask[i];
945 			f->maskandmode[i] = mask & mode;
946 			doneq |= f->maskandnotmode[i] = mask & ~mode;
947 		}
948 		f->doneq = doneq ? 1 : 0;
949 	} while ((f = f->next));
950 }
951 
952 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
953 {
954 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
955 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
956 	int ret;
957 
958 	if (mutex_lock_interruptible(&dvbdmx->mutex))
959 		return -ERESTARTSYS;
960 
961 	if (feed->is_filtering) {
962 		mutex_unlock(&dvbdmx->mutex);
963 		return -EBUSY;
964 	}
965 
966 	if (!dvbdmxfeed->filter) {
967 		mutex_unlock(&dvbdmx->mutex);
968 		return -EINVAL;
969 	}
970 
971 	dvbdmxfeed->feed.sec.tsfeedp = 0;
972 	dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
973 	dvbdmxfeed->feed.sec.secbufp = 0;
974 	dvbdmxfeed->feed.sec.seclen = 0;
975 
976 	if (!dvbdmx->start_feed) {
977 		mutex_unlock(&dvbdmx->mutex);
978 		return -ENODEV;
979 	}
980 
981 	prepare_secfilters(dvbdmxfeed);
982 
983 	if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
984 		mutex_unlock(&dvbdmx->mutex);
985 		return ret;
986 	}
987 
988 	spin_lock_irq(&dvbdmx->lock);
989 	feed->is_filtering = 1;
990 	dvbdmxfeed->state = DMX_STATE_GO;
991 	spin_unlock_irq(&dvbdmx->lock);
992 
993 	mutex_unlock(&dvbdmx->mutex);
994 	return 0;
995 }
996 
997 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
998 {
999 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1000 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1001 	int ret;
1002 
1003 	mutex_lock(&dvbdmx->mutex);
1004 
1005 	if (!dvbdmx->stop_feed) {
1006 		mutex_unlock(&dvbdmx->mutex);
1007 		return -ENODEV;
1008 	}
1009 
1010 	ret = dvbdmx->stop_feed(dvbdmxfeed);
1011 
1012 	spin_lock_irq(&dvbdmx->lock);
1013 	dvbdmxfeed->state = DMX_STATE_READY;
1014 	feed->is_filtering = 0;
1015 	spin_unlock_irq(&dvbdmx->lock);
1016 
1017 	mutex_unlock(&dvbdmx->mutex);
1018 	return ret;
1019 }
1020 
1021 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
1022 					   struct dmx_section_filter *filter)
1023 {
1024 	struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
1025 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1026 	struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1027 
1028 	mutex_lock(&dvbdmx->mutex);
1029 
1030 	if (dvbdmxfilter->feed != dvbdmxfeed) {
1031 		mutex_unlock(&dvbdmx->mutex);
1032 		return -EINVAL;
1033 	}
1034 
1035 	if (feed->is_filtering) {
1036 		/* release dvbdmx->mutex as far as it is
1037 		   acquired by stop_filtering() itself */
1038 		mutex_unlock(&dvbdmx->mutex);
1039 		feed->stop_filtering(feed);
1040 		mutex_lock(&dvbdmx->mutex);
1041 	}
1042 
1043 	spin_lock_irq(&dvbdmx->lock);
1044 	f = dvbdmxfeed->filter;
1045 
1046 	if (f == dvbdmxfilter) {
1047 		dvbdmxfeed->filter = dvbdmxfilter->next;
1048 	} else {
1049 		while (f->next != dvbdmxfilter)
1050 			f = f->next;
1051 		f->next = f->next->next;
1052 	}
1053 
1054 	dvbdmxfilter->state = DMX_STATE_FREE;
1055 	spin_unlock_irq(&dvbdmx->lock);
1056 	mutex_unlock(&dvbdmx->mutex);
1057 	return 0;
1058 }
1059 
1060 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1061 					struct dmx_section_feed **feed,
1062 					dmx_section_cb callback)
1063 {
1064 	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1065 	struct dvb_demux_feed *dvbdmxfeed;
1066 
1067 	if (mutex_lock_interruptible(&dvbdmx->mutex))
1068 		return -ERESTARTSYS;
1069 
1070 	if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1071 		mutex_unlock(&dvbdmx->mutex);
1072 		return -EBUSY;
1073 	}
1074 
1075 	dvbdmxfeed->type = DMX_TYPE_SEC;
1076 	dvbdmxfeed->cb.sec = callback;
1077 	dvbdmxfeed->demux = dvbdmx;
1078 	dvbdmxfeed->pid = 0xffff;
1079 	dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1080 	dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1081 	dvbdmxfeed->feed.sec.tsfeedp = 0;
1082 	dvbdmxfeed->filter = NULL;
1083 	dvbdmxfeed->buffer = NULL;
1084 
1085 	(*feed) = &dvbdmxfeed->feed.sec;
1086 	(*feed)->is_filtering = 0;
1087 	(*feed)->parent = demux;
1088 	(*feed)->priv = NULL;
1089 
1090 	(*feed)->set = dmx_section_feed_set;
1091 	(*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1092 	(*feed)->start_filtering = dmx_section_feed_start_filtering;
1093 	(*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1094 	(*feed)->release_filter = dmx_section_feed_release_filter;
1095 
1096 	mutex_unlock(&dvbdmx->mutex);
1097 	return 0;
1098 }
1099 
1100 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1101 				       struct dmx_section_feed *feed)
1102 {
1103 	struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1104 	struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1105 
1106 	mutex_lock(&dvbdmx->mutex);
1107 
1108 	if (dvbdmxfeed->state == DMX_STATE_FREE) {
1109 		mutex_unlock(&dvbdmx->mutex);
1110 		return -EINVAL;
1111 	}
1112 #ifndef NOBUFS
1113 	vfree(dvbdmxfeed->buffer);
1114 	dvbdmxfeed->buffer = NULL;
1115 #endif
1116 	dvbdmxfeed->state = DMX_STATE_FREE;
1117 
1118 	dvb_demux_feed_del(dvbdmxfeed);
1119 
1120 	dvbdmxfeed->pid = 0xffff;
1121 
1122 	mutex_unlock(&dvbdmx->mutex);
1123 	return 0;
1124 }
1125 
1126 /******************************************************************************
1127  * dvb_demux kernel data API calls
1128  ******************************************************************************/
1129 
1130 static int dvbdmx_open(struct dmx_demux *demux)
1131 {
1132 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1133 
1134 	if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1135 		return -EUSERS;
1136 
1137 	dvbdemux->users++;
1138 	return 0;
1139 }
1140 
1141 static int dvbdmx_close(struct dmx_demux *demux)
1142 {
1143 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1144 
1145 	if (dvbdemux->users == 0)
1146 		return -ENODEV;
1147 
1148 	dvbdemux->users--;
1149 	//FIXME: release any unneeded resources if users==0
1150 	return 0;
1151 }
1152 
1153 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1154 {
1155 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1156 	void *p;
1157 
1158 	if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1159 		return -EINVAL;
1160 
1161 	p = memdup_user(buf, count);
1162 	if (IS_ERR(p))
1163 		return PTR_ERR(p);
1164 	if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1165 		kfree(p);
1166 		return -ERESTARTSYS;
1167 	}
1168 	dvb_dmx_swfilter(dvbdemux, p, count);
1169 	kfree(p);
1170 	mutex_unlock(&dvbdemux->mutex);
1171 
1172 	if (signal_pending(current))
1173 		return -EINTR;
1174 	return count;
1175 }
1176 
1177 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1178 			       struct dmx_frontend *frontend)
1179 {
1180 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1181 	struct list_head *head = &dvbdemux->frontend_list;
1182 
1183 	list_add(&(frontend->connectivity_list), head);
1184 
1185 	return 0;
1186 }
1187 
1188 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1189 				  struct dmx_frontend *frontend)
1190 {
1191 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1192 	struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1193 
1194 	list_for_each_safe(pos, n, head) {
1195 		if (DMX_FE_ENTRY(pos) == frontend) {
1196 			list_del(pos);
1197 			return 0;
1198 		}
1199 	}
1200 
1201 	return -ENODEV;
1202 }
1203 
1204 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1205 {
1206 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1207 
1208 	if (list_empty(&dvbdemux->frontend_list))
1209 		return NULL;
1210 
1211 	return &dvbdemux->frontend_list;
1212 }
1213 
1214 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1215 				   struct dmx_frontend *frontend)
1216 {
1217 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1218 
1219 	if (demux->frontend)
1220 		return -EINVAL;
1221 
1222 	mutex_lock(&dvbdemux->mutex);
1223 
1224 	demux->frontend = frontend;
1225 	mutex_unlock(&dvbdemux->mutex);
1226 	return 0;
1227 }
1228 
1229 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1230 {
1231 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1232 
1233 	mutex_lock(&dvbdemux->mutex);
1234 
1235 	demux->frontend = NULL;
1236 	mutex_unlock(&dvbdemux->mutex);
1237 	return 0;
1238 }
1239 
1240 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1241 {
1242 	struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1243 
1244 	memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1245 	return 0;
1246 }
1247 
1248 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1249 {
1250 	int i;
1251 	struct dmx_demux *dmx = &dvbdemux->dmx;
1252 
1253 	dvbdemux->cnt_storage = NULL;
1254 	dvbdemux->users = 0;
1255 	dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1256 
1257 	if (!dvbdemux->filter)
1258 		return -ENOMEM;
1259 
1260 	dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1261 	if (!dvbdemux->feed) {
1262 		vfree(dvbdemux->filter);
1263 		dvbdemux->filter = NULL;
1264 		return -ENOMEM;
1265 	}
1266 	for (i = 0; i < dvbdemux->filternum; i++) {
1267 		dvbdemux->filter[i].state = DMX_STATE_FREE;
1268 		dvbdemux->filter[i].index = i;
1269 	}
1270 	for (i = 0; i < dvbdemux->feednum; i++) {
1271 		dvbdemux->feed[i].state = DMX_STATE_FREE;
1272 		dvbdemux->feed[i].index = i;
1273 	}
1274 
1275 	dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1276 	if (!dvbdemux->cnt_storage)
1277 		printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
1278 
1279 	INIT_LIST_HEAD(&dvbdemux->frontend_list);
1280 
1281 	for (i = 0; i < DMX_PES_OTHER; i++) {
1282 		dvbdemux->pesfilter[i] = NULL;
1283 		dvbdemux->pids[i] = 0xffff;
1284 	}
1285 
1286 	INIT_LIST_HEAD(&dvbdemux->feed_list);
1287 
1288 	dvbdemux->playing = 0;
1289 	dvbdemux->recording = 0;
1290 	dvbdemux->tsbufp = 0;
1291 
1292 	if (!dvbdemux->check_crc32)
1293 		dvbdemux->check_crc32 = dvb_dmx_crc32;
1294 
1295 	if (!dvbdemux->memcopy)
1296 		dvbdemux->memcopy = dvb_dmx_memcopy;
1297 
1298 	dmx->frontend = NULL;
1299 	dmx->priv = dvbdemux;
1300 	dmx->open = dvbdmx_open;
1301 	dmx->close = dvbdmx_close;
1302 	dmx->write = dvbdmx_write;
1303 	dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1304 	dmx->release_ts_feed = dvbdmx_release_ts_feed;
1305 	dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1306 	dmx->release_section_feed = dvbdmx_release_section_feed;
1307 
1308 	dmx->add_frontend = dvbdmx_add_frontend;
1309 	dmx->remove_frontend = dvbdmx_remove_frontend;
1310 	dmx->get_frontends = dvbdmx_get_frontends;
1311 	dmx->connect_frontend = dvbdmx_connect_frontend;
1312 	dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1313 	dmx->get_pes_pids = dvbdmx_get_pes_pids;
1314 
1315 	mutex_init(&dvbdemux->mutex);
1316 	spin_lock_init(&dvbdemux->lock);
1317 
1318 	return 0;
1319 }
1320 
1321 EXPORT_SYMBOL(dvb_dmx_init);
1322 
1323 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1324 {
1325 	vfree(dvbdemux->cnt_storage);
1326 	vfree(dvbdemux->filter);
1327 	vfree(dvbdemux->feed);
1328 }
1329 
1330 EXPORT_SYMBOL(dvb_dmx_release);
1331