Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / dvb-core / dvb_demux.c
blob0cc5e935166ca8908a6fb79afbaf8b80613f2843
1 /*
2 * dvb_demux.c - DVB kernel demux API
4 * Copyright (C) 2000-2001 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 * for convergence integrated media GmbH
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.
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.
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.
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>
35 #include "dvb_demux.h"
37 #define NOBUFS
39 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
41 // #define DVB_DEMUX_SECTION_LOSS_LOG
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");
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");
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)");
58 #define dprintk_tscheck(x...) do { \
59 if (dvb_demux_tscheck && printk_ratelimit()) \
60 printk(x); \
61 } while (0)
63 /******************************************************************************
64 * static inlined helper functions
65 ******************************************************************************/
67 static inline u16 section_length(const u8 *buf)
69 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
72 static inline u16 ts_pid(const u8 *buf)
74 return ((buf[1] & 0x1f) << 8) + buf[2];
77 static inline u8 payload(const u8 *tsp)
79 if (!(tsp[3] & 0x10)) // no payload?
80 return 0;
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];
89 return 184;
92 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
94 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
97 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
98 size_t len)
100 memcpy(d, s, len);
103 /******************************************************************************
104 * Software filter functions
105 ******************************************************************************/
107 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
108 const u8 *buf)
110 int count = payload(buf);
111 int p;
112 //int ccok;
113 //u8 cc;
115 if (count == 0)
116 return -1;
118 p = 188 - count;
121 cc = buf[3] & 0x0f;
122 ccok = ((feed->cc + 1) & 0x0f) == cc;
123 feed->cc = cc;
124 if (!ccok)
125 printk("missed packet!\n");
128 if (buf[1] & 0x40) // PUSI ?
129 feed->peslen = 0xfffa;
131 feed->peslen += count;
133 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts);
136 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
137 struct dvb_demux_filter *f)
139 u8 neq = 0;
140 int i;
142 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
143 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
145 if (f->maskandmode[i] & xor)
146 return 0;
148 neq |= f->maskandnotmode[i] & xor;
151 if (f->doneq && !neq)
152 return 0;
154 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
155 NULL, 0, &f->filter);
158 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
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;
165 if (!sec->is_filtering)
166 return 0;
168 if (!f)
169 return 0;
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;
178 do {
179 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
180 return -1;
181 } while ((f = f->next) && sec->is_filtering);
183 sec->seclen = 0;
185 return 0;
188 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
190 struct dmx_section_feed *sec = &feed->feed.sec;
192 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
193 if (sec->secbufp < sec->tsfeedp) {
194 int i, n = sec->tsfeedp - sec->secbufp;
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.
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");
210 #endif
212 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
213 sec->secbuf = sec->secbuf_base;
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)
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.
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
234 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
235 const u8 *buf, u8 len)
237 struct dvb_demux *demux = feed->demux;
238 struct dmx_section_feed *sec = &feed->feed.sec;
239 u16 limit, seclen, n;
241 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
242 return 0;
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;
253 if (len <= 0)
254 return 0;
256 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
257 sec->tsfeedp += len;
260 * Dump all the sections we can find in the data (Emard)
262 limit = sec->tsfeedp;
263 if (limit > DMX_MAX_SECFEED_SIZE)
264 return -1; /* internal error should never happen */
266 /* to be sure always set secbuf */
267 sec->secbuf = sec->secbuf_base + sec->secbufp;
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 */
287 return 0;
290 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
291 const u8 *buf)
293 u8 p, count;
294 int ccok, dc_i = 0;
295 u8 cc;
297 count = payload(buf);
299 if (count == 0) /* count == 0 if no payload or out of range */
300 return -1;
302 p = 188 - count; /* payload start */
304 cc = buf[3] & 0x0f;
305 ccok = ((feed->cc + 1) & 0x0f) == cc;
306 feed->cc = cc;
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;
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);
319 * those bytes under sume circumstances will again be reported
320 * in the following dvb_dmx_swfilter_section_new
322 #endif
324 * Discontinuity detected. Reset pusi_seen = 0 to
325 * stop feeding of suspicious data until next PUSI=1 arrives
327 feed->pusi_seen = 0;
328 dvb_dmx_swfilter_section_new(feed);
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;
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);
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);
356 return 0;
359 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
360 const u8 *buf)
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);
372 if (feed->ts_type & TS_DECODER)
373 if (feed->demux->write_to_decoder)
374 feed->demux->write_to_decoder(feed, buf, 188);
375 break;
377 case DMX_TYPE_SEC:
378 if (!feed->feed.sec.is_filtering)
379 break;
380 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
381 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
382 break;
384 default:
385 break;
389 #define DVR_FEED(f) \
390 (((f)->type == DMX_TYPE_TS) && \
391 ((f)->feed.ts.is_filtering) && \
392 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
394 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
396 struct dvb_demux_feed *feed;
397 u16 pid = ts_pid(buf);
398 int dvr_done = 0;
400 if (dvb_demux_speedcheck) {
401 struct timespec cur_time, delta_time;
402 u64 speed_bytes, speed_timedelta;
404 demux->speed_pkts_cnt++;
406 /* show speed every SPEED_PKTS_INTERVAL packets */
407 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
408 cur_time = current_kernel_time();
410 if (demux->speed_last_time.tv_sec != 0 &&
411 demux->speed_last_time.tv_nsec != 0) {
412 delta_time = timespec_sub(cur_time,
413 demux->speed_last_time);
414 speed_bytes = (u64)demux->speed_pkts_cnt
415 * 188 * 8;
416 /* convert to 1024 basis */
417 speed_bytes = 1000 * div64_u64(speed_bytes,
418 1024);
419 speed_timedelta =
420 (u64)timespec_to_ns(&delta_time);
421 speed_timedelta = div64_u64(speed_timedelta,
422 1000000); /* nsec -> usec */
423 printk(KERN_INFO "TS speed %llu Kbits/sec \n",
424 div64_u64(speed_bytes,
425 speed_timedelta));
428 demux->speed_last_time = cur_time;
429 demux->speed_pkts_cnt = 0;
433 if (buf[1] & 0x80) {
434 dprintk_tscheck("TEI detected. "
435 "PID=0x%x data1=0x%x\n",
436 pid, buf[1]);
437 /* data in this packet can't be trusted - drop it unless
438 * module option dvb_demux_feed_err_pkts is set */
439 if (!dvb_demux_feed_err_pkts)
440 return;
441 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
442 if (demux->cnt_storage && dvb_demux_tscheck) {
443 /* check pkt counter */
444 if (pid < MAX_PID) {
445 if (buf[3] & 0x10)
446 demux->cnt_storage[pid] =
447 (demux->cnt_storage[pid] + 1) & 0xf;
449 if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
450 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
451 pid, demux->cnt_storage[pid],
452 buf[3] & 0xf);
453 demux->cnt_storage[pid] = buf[3] & 0xf;
456 /* end check */
459 list_for_each_entry(feed, &demux->feed_list, list_head) {
460 if ((feed->pid != pid) && (feed->pid != 0x2000))
461 continue;
463 /* copy each packet only once to the dvr device, even
464 * if a PID is in multiple filters (e.g. video + PCR) */
465 if ((DVR_FEED(feed)) && (dvr_done++))
466 continue;
468 if (feed->pid == pid)
469 dvb_dmx_swfilter_packet_type(feed, buf);
470 else if (feed->pid == 0x2000)
471 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts);
475 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
476 size_t count)
478 unsigned long flags;
480 spin_lock_irqsave(&demux->lock, flags);
482 while (count--) {
483 if (buf[0] == 0x47)
484 dvb_dmx_swfilter_packet(demux, buf);
485 buf += 188;
488 spin_unlock_irqrestore(&demux->lock, flags);
491 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
493 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
494 const int pktsize)
496 int start = pos, lost;
498 while (pos < count) {
499 if (buf[pos] == 0x47 ||
500 (pktsize == 204 && buf[pos] == 0xB8))
501 break;
502 pos++;
505 lost = pos - start;
506 if (lost) {
507 /* This garbage is part of a valid packet? */
508 int backtrack = pos - pktsize;
509 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
510 (pktsize == 204 && buf[backtrack] == 0xB8)))
511 return backtrack;
514 return pos;
517 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
518 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
519 size_t count, const int pktsize)
521 int p = 0, i, j;
522 const u8 *q;
523 unsigned long flags;
525 spin_lock_irqsave(&demux->lock, flags);
527 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
528 i = demux->tsbufp;
529 j = pktsize - i;
530 if (count < j) {
531 memcpy(&demux->tsbuf[i], buf, count);
532 demux->tsbufp += count;
533 goto bailout;
535 memcpy(&demux->tsbuf[i], buf, j);
536 if (demux->tsbuf[0] == 0x47) /* double check */
537 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
538 demux->tsbufp = 0;
539 p += j;
542 while (1) {
543 p = find_next_packet(buf, p, count, pktsize);
544 if (p >= count)
545 break;
546 if (count - p < pktsize)
547 break;
549 q = &buf[p];
551 if (pktsize == 204 && (*q == 0xB8)) {
552 memcpy(demux->tsbuf, q, 188);
553 demux->tsbuf[0] = 0x47;
554 q = demux->tsbuf;
556 dvb_dmx_swfilter_packet(demux, q);
557 p += pktsize;
560 i = count - p;
561 if (i) {
562 memcpy(demux->tsbuf, &buf[p], i);
563 demux->tsbufp = i;
564 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
565 demux->tsbuf[0] = 0x47;
568 bailout:
569 spin_unlock_irqrestore(&demux->lock, flags);
572 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
574 _dvb_dmx_swfilter(demux, buf, count, 188);
576 EXPORT_SYMBOL(dvb_dmx_swfilter);
578 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
580 _dvb_dmx_swfilter(demux, buf, count, 204);
582 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
584 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
586 unsigned long flags;
588 spin_lock_irqsave(&demux->lock, flags);
590 demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts);
592 spin_unlock_irqrestore(&demux->lock, flags);
594 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
596 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
598 int i;
600 for (i = 0; i < demux->filternum; i++)
601 if (demux->filter[i].state == DMX_STATE_FREE)
602 break;
604 if (i == demux->filternum)
605 return NULL;
607 demux->filter[i].state = DMX_STATE_ALLOCATED;
609 return &demux->filter[i];
612 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
614 int i;
616 for (i = 0; i < demux->feednum; i++)
617 if (demux->feed[i].state == DMX_STATE_FREE)
618 break;
620 if (i == demux->feednum)
621 return NULL;
623 demux->feed[i].state = DMX_STATE_ALLOCATED;
625 return &demux->feed[i];
628 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
630 struct dvb_demux_feed *entry;
632 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
633 if (entry == feed)
634 return 1;
636 return 0;
639 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
641 spin_lock_irq(&feed->demux->lock);
642 if (dvb_demux_feed_find(feed)) {
643 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
644 __func__, feed->type, feed->state, feed->pid);
645 goto out;
648 list_add(&feed->list_head, &feed->demux->feed_list);
649 out:
650 spin_unlock_irq(&feed->demux->lock);
653 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
655 spin_lock_irq(&feed->demux->lock);
656 if (!(dvb_demux_feed_find(feed))) {
657 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
658 __func__, feed->type, feed->state, feed->pid);
659 goto out;
662 list_del(&feed->list_head);
663 out:
664 spin_unlock_irq(&feed->demux->lock);
667 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
668 enum dmx_ts_pes pes_type,
669 size_t circular_buffer_size, struct timespec timeout)
671 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
672 struct dvb_demux *demux = feed->demux;
674 if (pid > DMX_MAX_PID)
675 return -EINVAL;
677 if (mutex_lock_interruptible(&demux->mutex))
678 return -ERESTARTSYS;
680 if (ts_type & TS_DECODER) {
681 if (pes_type >= DMX_PES_OTHER) {
682 mutex_unlock(&demux->mutex);
683 return -EINVAL;
686 if (demux->pesfilter[pes_type] &&
687 demux->pesfilter[pes_type] != feed) {
688 mutex_unlock(&demux->mutex);
689 return -EINVAL;
692 demux->pesfilter[pes_type] = feed;
693 demux->pids[pes_type] = pid;
696 dvb_demux_feed_add(feed);
698 feed->pid = pid;
699 feed->buffer_size = circular_buffer_size;
700 feed->timeout = timeout;
701 feed->ts_type = ts_type;
702 feed->pes_type = pes_type;
704 if (feed->buffer_size) {
705 #ifdef NOBUFS
706 feed->buffer = NULL;
707 #else
708 feed->buffer = vmalloc(feed->buffer_size);
709 if (!feed->buffer) {
710 mutex_unlock(&demux->mutex);
711 return -ENOMEM;
713 #endif
716 feed->state = DMX_STATE_READY;
717 mutex_unlock(&demux->mutex);
719 return 0;
722 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
724 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
725 struct dvb_demux *demux = feed->demux;
726 int ret;
728 if (mutex_lock_interruptible(&demux->mutex))
729 return -ERESTARTSYS;
731 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
732 mutex_unlock(&demux->mutex);
733 return -EINVAL;
736 if (!demux->start_feed) {
737 mutex_unlock(&demux->mutex);
738 return -ENODEV;
741 if ((ret = demux->start_feed(feed)) < 0) {
742 mutex_unlock(&demux->mutex);
743 return ret;
746 spin_lock_irq(&demux->lock);
747 ts_feed->is_filtering = 1;
748 feed->state = DMX_STATE_GO;
749 spin_unlock_irq(&demux->lock);
750 mutex_unlock(&demux->mutex);
752 return 0;
755 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
757 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
758 struct dvb_demux *demux = feed->demux;
759 int ret;
761 mutex_lock(&demux->mutex);
763 if (feed->state < DMX_STATE_GO) {
764 mutex_unlock(&demux->mutex);
765 return -EINVAL;
768 if (!demux->stop_feed) {
769 mutex_unlock(&demux->mutex);
770 return -ENODEV;
773 ret = demux->stop_feed(feed);
775 spin_lock_irq(&demux->lock);
776 ts_feed->is_filtering = 0;
777 feed->state = DMX_STATE_ALLOCATED;
778 spin_unlock_irq(&demux->lock);
779 mutex_unlock(&demux->mutex);
781 return ret;
784 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
785 struct dmx_ts_feed **ts_feed,
786 dmx_ts_cb callback)
788 struct dvb_demux *demux = (struct dvb_demux *)dmx;
789 struct dvb_demux_feed *feed;
791 if (mutex_lock_interruptible(&demux->mutex))
792 return -ERESTARTSYS;
794 if (!(feed = dvb_dmx_feed_alloc(demux))) {
795 mutex_unlock(&demux->mutex);
796 return -EBUSY;
799 feed->type = DMX_TYPE_TS;
800 feed->cb.ts = callback;
801 feed->demux = demux;
802 feed->pid = 0xffff;
803 feed->peslen = 0xfffa;
804 feed->buffer = NULL;
806 (*ts_feed) = &feed->feed.ts;
807 (*ts_feed)->parent = dmx;
808 (*ts_feed)->priv = NULL;
809 (*ts_feed)->is_filtering = 0;
810 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
811 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
812 (*ts_feed)->set = dmx_ts_feed_set;
814 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
815 feed->state = DMX_STATE_FREE;
816 mutex_unlock(&demux->mutex);
817 return -EBUSY;
820 feed->filter->type = DMX_TYPE_TS;
821 feed->filter->feed = feed;
822 feed->filter->state = DMX_STATE_READY;
824 mutex_unlock(&demux->mutex);
826 return 0;
829 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
830 struct dmx_ts_feed *ts_feed)
832 struct dvb_demux *demux = (struct dvb_demux *)dmx;
833 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
835 mutex_lock(&demux->mutex);
837 if (feed->state == DMX_STATE_FREE) {
838 mutex_unlock(&demux->mutex);
839 return -EINVAL;
841 #ifndef NOBUFS
842 vfree(feed->buffer);
843 feed->buffer = NULL;
844 #endif
846 feed->state = DMX_STATE_FREE;
847 feed->filter->state = DMX_STATE_FREE;
849 dvb_demux_feed_del(feed);
851 feed->pid = 0xffff;
853 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
854 demux->pesfilter[feed->pes_type] = NULL;
856 mutex_unlock(&demux->mutex);
857 return 0;
860 /******************************************************************************
861 * dmx_section_feed API calls
862 ******************************************************************************/
864 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
865 struct dmx_section_filter **filter)
867 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
868 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
869 struct dvb_demux_filter *dvbdmxfilter;
871 if (mutex_lock_interruptible(&dvbdemux->mutex))
872 return -ERESTARTSYS;
874 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
875 if (!dvbdmxfilter) {
876 mutex_unlock(&dvbdemux->mutex);
877 return -EBUSY;
880 spin_lock_irq(&dvbdemux->lock);
881 *filter = &dvbdmxfilter->filter;
882 (*filter)->parent = feed;
883 (*filter)->priv = NULL;
884 dvbdmxfilter->feed = dvbdmxfeed;
885 dvbdmxfilter->type = DMX_TYPE_SEC;
886 dvbdmxfilter->state = DMX_STATE_READY;
887 dvbdmxfilter->next = dvbdmxfeed->filter;
888 dvbdmxfeed->filter = dvbdmxfilter;
889 spin_unlock_irq(&dvbdemux->lock);
891 mutex_unlock(&dvbdemux->mutex);
892 return 0;
895 static int dmx_section_feed_set(struct dmx_section_feed *feed,
896 u16 pid, size_t circular_buffer_size,
897 int check_crc)
899 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
900 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
902 if (pid > 0x1fff)
903 return -EINVAL;
905 if (mutex_lock_interruptible(&dvbdmx->mutex))
906 return -ERESTARTSYS;
908 dvb_demux_feed_add(dvbdmxfeed);
910 dvbdmxfeed->pid = pid;
911 dvbdmxfeed->buffer_size = circular_buffer_size;
912 dvbdmxfeed->feed.sec.check_crc = check_crc;
914 #ifdef NOBUFS
915 dvbdmxfeed->buffer = NULL;
916 #else
917 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
918 if (!dvbdmxfeed->buffer) {
919 mutex_unlock(&dvbdmx->mutex);
920 return -ENOMEM;
922 #endif
924 dvbdmxfeed->state = DMX_STATE_READY;
925 mutex_unlock(&dvbdmx->mutex);
926 return 0;
929 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
931 int i;
932 struct dvb_demux_filter *f;
933 struct dmx_section_filter *sf;
934 u8 mask, mode, doneq;
936 if (!(f = dvbdmxfeed->filter))
937 return;
938 do {
939 sf = &f->filter;
940 doneq = 0;
941 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
942 mode = sf->filter_mode[i];
943 mask = sf->filter_mask[i];
944 f->maskandmode[i] = mask & mode;
945 doneq |= f->maskandnotmode[i] = mask & ~mode;
947 f->doneq = doneq ? 1 : 0;
948 } while ((f = f->next));
951 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
953 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
954 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
955 int ret;
957 if (mutex_lock_interruptible(&dvbdmx->mutex))
958 return -ERESTARTSYS;
960 if (feed->is_filtering) {
961 mutex_unlock(&dvbdmx->mutex);
962 return -EBUSY;
965 if (!dvbdmxfeed->filter) {
966 mutex_unlock(&dvbdmx->mutex);
967 return -EINVAL;
970 dvbdmxfeed->feed.sec.tsfeedp = 0;
971 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
972 dvbdmxfeed->feed.sec.secbufp = 0;
973 dvbdmxfeed->feed.sec.seclen = 0;
975 if (!dvbdmx->start_feed) {
976 mutex_unlock(&dvbdmx->mutex);
977 return -ENODEV;
980 prepare_secfilters(dvbdmxfeed);
982 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
983 mutex_unlock(&dvbdmx->mutex);
984 return ret;
987 spin_lock_irq(&dvbdmx->lock);
988 feed->is_filtering = 1;
989 dvbdmxfeed->state = DMX_STATE_GO;
990 spin_unlock_irq(&dvbdmx->lock);
992 mutex_unlock(&dvbdmx->mutex);
993 return 0;
996 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
998 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
999 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1000 int ret;
1002 mutex_lock(&dvbdmx->mutex);
1004 if (!dvbdmx->stop_feed) {
1005 mutex_unlock(&dvbdmx->mutex);
1006 return -ENODEV;
1009 ret = dvbdmx->stop_feed(dvbdmxfeed);
1011 spin_lock_irq(&dvbdmx->lock);
1012 dvbdmxfeed->state = DMX_STATE_READY;
1013 feed->is_filtering = 0;
1014 spin_unlock_irq(&dvbdmx->lock);
1016 mutex_unlock(&dvbdmx->mutex);
1017 return ret;
1020 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
1021 struct dmx_section_filter *filter)
1023 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
1024 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1025 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1027 mutex_lock(&dvbdmx->mutex);
1029 if (dvbdmxfilter->feed != dvbdmxfeed) {
1030 mutex_unlock(&dvbdmx->mutex);
1031 return -EINVAL;
1034 if (feed->is_filtering) {
1035 /* release dvbdmx->mutex as far as it is
1036 acquired by stop_filtering() itself */
1037 mutex_unlock(&dvbdmx->mutex);
1038 feed->stop_filtering(feed);
1039 mutex_lock(&dvbdmx->mutex);
1042 spin_lock_irq(&dvbdmx->lock);
1043 f = dvbdmxfeed->filter;
1045 if (f == dvbdmxfilter) {
1046 dvbdmxfeed->filter = dvbdmxfilter->next;
1047 } else {
1048 while (f->next != dvbdmxfilter)
1049 f = f->next;
1050 f->next = f->next->next;
1053 dvbdmxfilter->state = DMX_STATE_FREE;
1054 spin_unlock_irq(&dvbdmx->lock);
1055 mutex_unlock(&dvbdmx->mutex);
1056 return 0;
1059 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1060 struct dmx_section_feed **feed,
1061 dmx_section_cb callback)
1063 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1064 struct dvb_demux_feed *dvbdmxfeed;
1066 if (mutex_lock_interruptible(&dvbdmx->mutex))
1067 return -ERESTARTSYS;
1069 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1070 mutex_unlock(&dvbdmx->mutex);
1071 return -EBUSY;
1074 dvbdmxfeed->type = DMX_TYPE_SEC;
1075 dvbdmxfeed->cb.sec = callback;
1076 dvbdmxfeed->demux = dvbdmx;
1077 dvbdmxfeed->pid = 0xffff;
1078 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1079 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1080 dvbdmxfeed->feed.sec.tsfeedp = 0;
1081 dvbdmxfeed->filter = NULL;
1082 dvbdmxfeed->buffer = NULL;
1084 (*feed) = &dvbdmxfeed->feed.sec;
1085 (*feed)->is_filtering = 0;
1086 (*feed)->parent = demux;
1087 (*feed)->priv = NULL;
1089 (*feed)->set = dmx_section_feed_set;
1090 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1091 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1092 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1093 (*feed)->release_filter = dmx_section_feed_release_filter;
1095 mutex_unlock(&dvbdmx->mutex);
1096 return 0;
1099 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1100 struct dmx_section_feed *feed)
1102 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1103 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1105 mutex_lock(&dvbdmx->mutex);
1107 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1108 mutex_unlock(&dvbdmx->mutex);
1109 return -EINVAL;
1111 #ifndef NOBUFS
1112 vfree(dvbdmxfeed->buffer);
1113 dvbdmxfeed->buffer = NULL;
1114 #endif
1115 dvbdmxfeed->state = DMX_STATE_FREE;
1117 dvb_demux_feed_del(dvbdmxfeed);
1119 dvbdmxfeed->pid = 0xffff;
1121 mutex_unlock(&dvbdmx->mutex);
1122 return 0;
1125 /******************************************************************************
1126 * dvb_demux kernel data API calls
1127 ******************************************************************************/
1129 static int dvbdmx_open(struct dmx_demux *demux)
1131 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1133 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1134 return -EUSERS;
1136 dvbdemux->users++;
1137 return 0;
1140 static int dvbdmx_close(struct dmx_demux *demux)
1142 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1144 if (dvbdemux->users == 0)
1145 return -ENODEV;
1147 dvbdemux->users--;
1148 //FIXME: release any unneeded resources if users==0
1149 return 0;
1152 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1154 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1155 void *p;
1157 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1158 return -EINVAL;
1160 p = memdup_user(buf, count);
1161 if (IS_ERR(p))
1162 return PTR_ERR(p);
1163 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1164 kfree(p);
1165 return -ERESTARTSYS;
1167 dvb_dmx_swfilter(dvbdemux, p, count);
1168 kfree(p);
1169 mutex_unlock(&dvbdemux->mutex);
1171 if (signal_pending(current))
1172 return -EINTR;
1173 return count;
1176 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1177 struct dmx_frontend *frontend)
1179 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1180 struct list_head *head = &dvbdemux->frontend_list;
1182 list_add(&(frontend->connectivity_list), head);
1184 return 0;
1187 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1188 struct dmx_frontend *frontend)
1190 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1191 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1193 list_for_each_safe(pos, n, head) {
1194 if (DMX_FE_ENTRY(pos) == frontend) {
1195 list_del(pos);
1196 return 0;
1200 return -ENODEV;
1203 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1205 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1207 if (list_empty(&dvbdemux->frontend_list))
1208 return NULL;
1210 return &dvbdemux->frontend_list;
1213 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1214 struct dmx_frontend *frontend)
1216 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1218 if (demux->frontend)
1219 return -EINVAL;
1221 mutex_lock(&dvbdemux->mutex);
1223 demux->frontend = frontend;
1224 mutex_unlock(&dvbdemux->mutex);
1225 return 0;
1228 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1230 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1232 mutex_lock(&dvbdemux->mutex);
1234 demux->frontend = NULL;
1235 mutex_unlock(&dvbdemux->mutex);
1236 return 0;
1239 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1241 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1243 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1244 return 0;
1247 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1249 int i;
1250 struct dmx_demux *dmx = &dvbdemux->dmx;
1252 dvbdemux->cnt_storage = NULL;
1253 dvbdemux->users = 0;
1254 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1256 if (!dvbdemux->filter)
1257 return -ENOMEM;
1259 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1260 if (!dvbdemux->feed) {
1261 vfree(dvbdemux->filter);
1262 dvbdemux->filter = NULL;
1263 return -ENOMEM;
1265 for (i = 0; i < dvbdemux->filternum; i++) {
1266 dvbdemux->filter[i].state = DMX_STATE_FREE;
1267 dvbdemux->filter[i].index = i;
1269 for (i = 0; i < dvbdemux->feednum; i++) {
1270 dvbdemux->feed[i].state = DMX_STATE_FREE;
1271 dvbdemux->feed[i].index = i;
1274 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1275 if (!dvbdemux->cnt_storage)
1276 printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
1278 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1280 for (i = 0; i < DMX_PES_OTHER; i++) {
1281 dvbdemux->pesfilter[i] = NULL;
1282 dvbdemux->pids[i] = 0xffff;
1285 INIT_LIST_HEAD(&dvbdemux->feed_list);
1287 dvbdemux->playing = 0;
1288 dvbdemux->recording = 0;
1289 dvbdemux->tsbufp = 0;
1291 if (!dvbdemux->check_crc32)
1292 dvbdemux->check_crc32 = dvb_dmx_crc32;
1294 if (!dvbdemux->memcopy)
1295 dvbdemux->memcopy = dvb_dmx_memcopy;
1297 dmx->frontend = NULL;
1298 dmx->priv = dvbdemux;
1299 dmx->open = dvbdmx_open;
1300 dmx->close = dvbdmx_close;
1301 dmx->write = dvbdmx_write;
1302 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1303 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1304 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1305 dmx->release_section_feed = dvbdmx_release_section_feed;
1307 dmx->add_frontend = dvbdmx_add_frontend;
1308 dmx->remove_frontend = dvbdmx_remove_frontend;
1309 dmx->get_frontends = dvbdmx_get_frontends;
1310 dmx->connect_frontend = dvbdmx_connect_frontend;
1311 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1312 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1314 mutex_init(&dvbdemux->mutex);
1315 spin_lock_init(&dvbdemux->lock);
1317 return 0;
1320 EXPORT_SYMBOL(dvb_dmx_init);
1322 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1324 vfree(dvbdemux->cnt_storage);
1325 vfree(dvbdemux->filter);
1326 vfree(dvbdemux->feed);
1329 EXPORT_SYMBOL(dvb_dmx_release);