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.
20 #define pr_fmt(fmt) "dvb_demux: " fmt
22 #include <linux/sched/signal.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
28 #include <linux/string.h>
29 #include <linux/crc32.h>
30 #include <linux/uaccess.h>
31 #include <asm/div64.h>
33 #include <media/dvb_demux.h>
35 static int dvb_demux_tscheck
;
36 module_param(dvb_demux_tscheck
, int, 0644);
37 MODULE_PARM_DESC(dvb_demux_tscheck
,
38 "enable transport stream continuity and TEI check");
40 static int dvb_demux_speedcheck
;
41 module_param(dvb_demux_speedcheck
, int, 0644);
42 MODULE_PARM_DESC(dvb_demux_speedcheck
,
43 "enable transport stream speed check");
45 static int dvb_demux_feed_err_pkts
= 1;
46 module_param(dvb_demux_feed_err_pkts
, int, 0644);
47 MODULE_PARM_DESC(dvb_demux_feed_err_pkts
,
48 "when set to 0, drop packets with the TEI bit set (1 by default)");
50 #define dprintk(fmt, arg...) \
51 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
53 #define dprintk_tscheck(x...) do { \
54 if (dvb_demux_tscheck && printk_ratelimit()) \
58 /******************************************************************************
59 * static inlined helper functions
60 ******************************************************************************/
62 static inline u16
section_length(const u8
*buf
)
64 return 3 + ((buf
[1] & 0x0f) << 8) + buf
[2];
67 static inline u16
ts_pid(const u8
*buf
)
69 return ((buf
[1] & 0x1f) << 8) + buf
[2];
72 static inline u8
payload(const u8
*tsp
)
74 if (!(tsp
[3] & 0x10)) // no payload?
77 if (tsp
[3] & 0x20) { // adaptation field?
78 if (tsp
[4] > 183) // corrupted data?
81 return 184 - 1 - tsp
[4];
87 static u32
dvb_dmx_crc32(struct dvb_demux_feed
*f
, const u8
*src
, size_t len
)
89 return (f
->feed
.sec
.crc_val
= crc32_be(f
->feed
.sec
.crc_val
, src
, len
));
92 static void dvb_dmx_memcopy(struct dvb_demux_feed
*f
, u8
*d
, const u8
*s
,
98 /******************************************************************************
99 * Software filter functions
100 ******************************************************************************/
102 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed
*feed
,
105 int count
= payload(buf
);
107 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
117 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
119 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
122 dprintk("missed packet: %d instead of %d!\n",
123 cc
, (feed
->cc
+ 1) & 0x0f);
126 if (buf
[1] & 0x40) // PUSI ?
127 feed
->peslen
= 0xfffa;
129 feed
->peslen
+= count
;
131 return feed
->cb
.ts(&buf
[p
], count
, NULL
, 0, &feed
->feed
.ts
);
134 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed
*feed
,
135 struct dvb_demux_filter
*f
)
140 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
141 u8
xor = f
->filter
.filter_value
[i
] ^ feed
->feed
.sec
.secbuf
[i
];
143 if (f
->maskandmode
[i
] & xor)
146 neq
|= f
->maskandnotmode
[i
] & xor;
149 if (f
->doneq
&& !neq
)
152 return feed
->cb
.sec(feed
->feed
.sec
.secbuf
, feed
->feed
.sec
.seclen
,
153 NULL
, 0, &f
->filter
);
156 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed
*feed
)
158 struct dvb_demux
*demux
= feed
->demux
;
159 struct dvb_demux_filter
*f
= feed
->filter
;
160 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
161 int section_syntax_indicator
;
163 if (!sec
->is_filtering
)
169 if (sec
->check_crc
) {
170 section_syntax_indicator
= ((sec
->secbuf
[1] & 0x80) != 0);
171 if (section_syntax_indicator
&&
172 demux
->check_crc32(feed
, sec
->secbuf
, sec
->seclen
))
177 if (dvb_dmx_swfilter_sectionfilter(feed
, f
) < 0)
179 } while ((f
= f
->next
) && sec
->is_filtering
);
186 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed
*feed
)
188 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
190 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
191 if (sec
->secbufp
< sec
->tsfeedp
) {
192 int n
= sec
->tsfeedp
- sec
->secbufp
;
195 * Section padding is done with 0xff bytes entirely.
196 * Due to speed reasons, we won't check all of them
197 * but just first and last.
199 if (sec
->secbuf
[0] != 0xff || sec
->secbuf
[n
- 1] != 0xff) {
200 dprintk("section ts padding loss: %d/%d\n",
202 dprintk("pad data: %*ph\n", n
, sec
->secbuf
);
207 sec
->tsfeedp
= sec
->secbufp
= sec
->seclen
= 0;
208 sec
->secbuf
= sec
->secbuf_base
;
212 * Losless Section Demux 1.4.1 by Emard
214 * - middle of section A (no PUSI)
215 * - end of section A and start of section B
216 * (with PUSI pointing to the start of the second section)
218 * In this case, without feed->pusi_seen you'll receive a garbage section
219 * consisting of the end of section A. Basically because tsfeedp
220 * is incemented and the use=0 condition is not raised
221 * when the second packet arrives.
224 * when demux is started, let feed->pusi_seen = false to
225 * prevent initial feeding of garbage from the end of
226 * previous section. When you for the first time see PUSI=1
227 * then set feed->pusi_seen = true
229 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed
*feed
,
230 const u8
*buf
, u8 len
)
232 struct dvb_demux
*demux
= feed
->demux
;
233 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
234 u16 limit
, seclen
, n
;
236 if (sec
->tsfeedp
>= DMX_MAX_SECFEED_SIZE
)
239 if (sec
->tsfeedp
+ len
> DMX_MAX_SECFEED_SIZE
) {
240 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
241 dprintk("section buffer full loss: %d/%d\n",
242 sec
->tsfeedp
+ len
- DMX_MAX_SECFEED_SIZE
,
243 DMX_MAX_SECFEED_SIZE
);
245 len
= DMX_MAX_SECFEED_SIZE
- sec
->tsfeedp
;
251 demux
->memcopy(feed
, sec
->secbuf_base
+ sec
->tsfeedp
, buf
, len
);
255 * Dump all the sections we can find in the data (Emard)
257 limit
= sec
->tsfeedp
;
258 if (limit
> DMX_MAX_SECFEED_SIZE
)
259 return -1; /* internal error should never happen */
261 /* to be sure always set secbuf */
262 sec
->secbuf
= sec
->secbuf_base
+ sec
->secbufp
;
264 for (n
= 0; sec
->secbufp
+ 2 < limit
; n
++) {
265 seclen
= section_length(sec
->secbuf
);
266 if (seclen
<= 0 || seclen
> DMX_MAX_SECTION_SIZE
267 || seclen
+ sec
->secbufp
> limit
)
269 sec
->seclen
= seclen
;
271 /* dump [secbuf .. secbuf+seclen) */
273 dvb_dmx_swfilter_section_feed(feed
);
274 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
276 dprintk("pusi not seen, discarding section data\n");
278 sec
->secbufp
+= seclen
; /* secbufp and secbuf moving together is */
279 sec
->secbuf
+= seclen
; /* redundant but saves pointer arithmetic */
285 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed
*feed
,
292 count
= payload(buf
);
294 if (count
== 0) /* count == 0 if no payload or out of range */
297 p
= 188 - count
; /* payload start */
300 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
304 /* adaption field present, check for discontinuity_indicator */
305 if ((buf
[4] > 0) && (buf
[5] & 0x80))
310 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
312 dprintk("%d frame with disconnect indicator\n",
315 dprintk("discontinuity: %d instead of %d. %d bytes lost\n",
316 cc
, (feed
->cc
+ 1) & 0x0f, count
+ 4);
318 * those bytes under sume circumstances will again be reported
319 * in the following dvb_dmx_swfilter_section_new
323 * Discontinuity detected. Reset pusi_seen to
324 * stop feeding of suspicious data until next PUSI=1 arrives
326 * FIXME: does it make sense if the MPEG-TS is the one
327 * reporting discontinuity?
329 feed
->pusi_seen
= false;
330 dvb_dmx_swfilter_section_new(feed
);
334 /* PUSI=1 (is set), section boundary is here */
335 if (count
> 1 && buf
[p
] < count
) {
336 const u8
*before
= &buf
[p
+ 1];
337 u8 before_len
= buf
[p
];
338 const u8
*after
= &before
[before_len
];
339 u8 after_len
= count
- 1 - before_len
;
341 dvb_dmx_swfilter_section_copy_dump(feed
, before
,
343 /* before start of new section, set pusi_seen */
344 feed
->pusi_seen
= true;
345 dvb_dmx_swfilter_section_new(feed
);
346 dvb_dmx_swfilter_section_copy_dump(feed
, after
,
349 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
351 dprintk("PUSI=1 but %d bytes lost\n", count
);
354 /* PUSI=0 (is not set), no section boundary */
355 dvb_dmx_swfilter_section_copy_dump(feed
, &buf
[p
], count
);
361 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed
*feed
,
364 switch (feed
->type
) {
366 if (!feed
->feed
.ts
.is_filtering
)
368 if (feed
->ts_type
& TS_PACKET
) {
369 if (feed
->ts_type
& TS_PAYLOAD_ONLY
)
370 dvb_dmx_swfilter_payload(feed
, buf
);
372 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
);
374 /* Used only on full-featured devices */
375 if (feed
->ts_type
& TS_DECODER
)
376 if (feed
->demux
->write_to_decoder
)
377 feed
->demux
->write_to_decoder(feed
, buf
, 188);
381 if (!feed
->feed
.sec
.is_filtering
)
383 if (dvb_dmx_swfilter_section_packet(feed
, buf
) < 0)
384 feed
->feed
.sec
.seclen
= feed
->feed
.sec
.secbufp
= 0;
392 #define DVR_FEED(f) \
393 (((f)->type == DMX_TYPE_TS) && \
394 ((f)->feed.ts.is_filtering) && \
395 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
397 static void dvb_dmx_swfilter_packet(struct dvb_demux
*demux
, const u8
*buf
)
399 struct dvb_demux_feed
*feed
;
400 u16 pid
= ts_pid(buf
);
403 if (dvb_demux_speedcheck
) {
405 u64 speed_bytes
, speed_timedelta
;
407 demux
->speed_pkts_cnt
++;
409 /* show speed every SPEED_PKTS_INTERVAL packets */
410 if (!(demux
->speed_pkts_cnt
% SPEED_PKTS_INTERVAL
)) {
411 cur_time
= ktime_get();
413 if (ktime_to_ns(demux
->speed_last_time
) != 0) {
414 speed_bytes
= (u64
)demux
->speed_pkts_cnt
416 /* convert to 1024 basis */
417 speed_bytes
= 1000 * div64_u64(speed_bytes
,
419 speed_timedelta
= ktime_ms_delta(cur_time
,
420 demux
->speed_last_time
);
422 dprintk("TS speed %llu Kbits/sec \n",
423 div64_u64(speed_bytes
,
427 demux
->speed_last_time
= cur_time
;
428 demux
->speed_pkts_cnt
= 0;
433 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
435 /* data in this packet can't be trusted - drop it unless
436 * module option dvb_demux_feed_err_pkts is set */
437 if (!dvb_demux_feed_err_pkts
)
439 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
440 if (demux
->cnt_storage
&& dvb_demux_tscheck
) {
441 /* check pkt counter */
444 demux
->cnt_storage
[pid
] =
445 (demux
->cnt_storage
[pid
] + 1) & 0xf;
447 if ((buf
[3] & 0xf) != demux
->cnt_storage
[pid
]) {
448 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
449 pid
, demux
->cnt_storage
[pid
],
451 demux
->cnt_storage
[pid
] = buf
[3] & 0xf;
457 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
458 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
461 /* copy each packet only once to the dvr device, even
462 * if a PID is in multiple filters (e.g. video + PCR) */
463 if ((DVR_FEED(feed
)) && (dvr_done
++))
466 if (feed
->pid
== pid
)
467 dvb_dmx_swfilter_packet_type(feed
, buf
);
468 else if (feed
->pid
== 0x2000)
469 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
);
473 void dvb_dmx_swfilter_packets(struct dvb_demux
*demux
, const u8
*buf
,
478 spin_lock_irqsave(&demux
->lock
, flags
);
482 dvb_dmx_swfilter_packet(demux
, buf
);
486 spin_unlock_irqrestore(&demux
->lock
, flags
);
489 EXPORT_SYMBOL(dvb_dmx_swfilter_packets
);
491 static inline int find_next_packet(const u8
*buf
, int pos
, size_t count
,
494 int start
= pos
, lost
;
496 while (pos
< count
) {
497 if (buf
[pos
] == 0x47 ||
498 (pktsize
== 204 && buf
[pos
] == 0xB8))
505 /* This garbage is part of a valid packet? */
506 int backtrack
= pos
- pktsize
;
507 if (backtrack
>= 0 && (buf
[backtrack
] == 0x47 ||
508 (pktsize
== 204 && buf
[backtrack
] == 0xB8)))
515 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
516 static inline void _dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
,
517 size_t count
, const int pktsize
)
523 spin_lock_irqsave(&demux
->lock
, flags
);
525 if (demux
->tsbufp
) { /* tsbuf[0] is now 0x47. */
529 memcpy(&demux
->tsbuf
[i
], buf
, count
);
530 demux
->tsbufp
+= count
;
533 memcpy(&demux
->tsbuf
[i
], buf
, j
);
534 if (demux
->tsbuf
[0] == 0x47) /* double check */
535 dvb_dmx_swfilter_packet(demux
, demux
->tsbuf
);
541 p
= find_next_packet(buf
, p
, count
, pktsize
);
544 if (count
- p
< pktsize
)
549 if (pktsize
== 204 && (*q
== 0xB8)) {
550 memcpy(demux
->tsbuf
, q
, 188);
551 demux
->tsbuf
[0] = 0x47;
554 dvb_dmx_swfilter_packet(demux
, q
);
560 memcpy(demux
->tsbuf
, &buf
[p
], i
);
562 if (pktsize
== 204 && demux
->tsbuf
[0] == 0xB8)
563 demux
->tsbuf
[0] = 0x47;
567 spin_unlock_irqrestore(&demux
->lock
, flags
);
570 void dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
572 _dvb_dmx_swfilter(demux
, buf
, count
, 188);
574 EXPORT_SYMBOL(dvb_dmx_swfilter
);
576 void dvb_dmx_swfilter_204(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
578 _dvb_dmx_swfilter(demux
, buf
, count
, 204);
580 EXPORT_SYMBOL(dvb_dmx_swfilter_204
);
582 void dvb_dmx_swfilter_raw(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
586 spin_lock_irqsave(&demux
->lock
, flags
);
588 demux
->feed
->cb
.ts(buf
, count
, NULL
, 0, &demux
->feed
->feed
.ts
);
590 spin_unlock_irqrestore(&demux
->lock
, flags
);
592 EXPORT_SYMBOL(dvb_dmx_swfilter_raw
);
594 static struct dvb_demux_filter
*dvb_dmx_filter_alloc(struct dvb_demux
*demux
)
598 for (i
= 0; i
< demux
->filternum
; i
++)
599 if (demux
->filter
[i
].state
== DMX_STATE_FREE
)
602 if (i
== demux
->filternum
)
605 demux
->filter
[i
].state
= DMX_STATE_ALLOCATED
;
607 return &demux
->filter
[i
];
610 static struct dvb_demux_feed
*dvb_dmx_feed_alloc(struct dvb_demux
*demux
)
614 for (i
= 0; i
< demux
->feednum
; i
++)
615 if (demux
->feed
[i
].state
== DMX_STATE_FREE
)
618 if (i
== demux
->feednum
)
621 demux
->feed
[i
].state
= DMX_STATE_ALLOCATED
;
623 return &demux
->feed
[i
];
626 static int dvb_demux_feed_find(struct dvb_demux_feed
*feed
)
628 struct dvb_demux_feed
*entry
;
630 list_for_each_entry(entry
, &feed
->demux
->feed_list
, list_head
)
637 static void dvb_demux_feed_add(struct dvb_demux_feed
*feed
)
639 spin_lock_irq(&feed
->demux
->lock
);
640 if (dvb_demux_feed_find(feed
)) {
641 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
642 __func__
, feed
->type
, feed
->state
, feed
->pid
);
646 list_add(&feed
->list_head
, &feed
->demux
->feed_list
);
648 spin_unlock_irq(&feed
->demux
->lock
);
651 static void dvb_demux_feed_del(struct dvb_demux_feed
*feed
)
653 spin_lock_irq(&feed
->demux
->lock
);
654 if (!(dvb_demux_feed_find(feed
))) {
655 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
656 __func__
, feed
->type
, feed
->state
, feed
->pid
);
660 list_del(&feed
->list_head
);
662 spin_unlock_irq(&feed
->demux
->lock
);
665 static int dmx_ts_feed_set(struct dmx_ts_feed
*ts_feed
, u16 pid
, int ts_type
,
666 enum dmx_ts_pes pes_type
, ktime_t timeout
)
668 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
669 struct dvb_demux
*demux
= feed
->demux
;
671 if (pid
> DMX_MAX_PID
)
674 if (mutex_lock_interruptible(&demux
->mutex
))
677 if (ts_type
& TS_DECODER
) {
678 if (pes_type
>= DMX_PES_OTHER
) {
679 mutex_unlock(&demux
->mutex
);
683 if (demux
->pesfilter
[pes_type
] &&
684 demux
->pesfilter
[pes_type
] != feed
) {
685 mutex_unlock(&demux
->mutex
);
689 demux
->pesfilter
[pes_type
] = feed
;
690 demux
->pids
[pes_type
] = pid
;
693 dvb_demux_feed_add(feed
);
696 feed
->timeout
= timeout
;
697 feed
->ts_type
= ts_type
;
698 feed
->pes_type
= pes_type
;
700 feed
->state
= DMX_STATE_READY
;
701 mutex_unlock(&demux
->mutex
);
706 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed
*ts_feed
)
708 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
709 struct dvb_demux
*demux
= feed
->demux
;
712 if (mutex_lock_interruptible(&demux
->mutex
))
715 if (feed
->state
!= DMX_STATE_READY
|| feed
->type
!= DMX_TYPE_TS
) {
716 mutex_unlock(&demux
->mutex
);
720 if (!demux
->start_feed
) {
721 mutex_unlock(&demux
->mutex
);
725 if ((ret
= demux
->start_feed(feed
)) < 0) {
726 mutex_unlock(&demux
->mutex
);
730 spin_lock_irq(&demux
->lock
);
731 ts_feed
->is_filtering
= 1;
732 feed
->state
= DMX_STATE_GO
;
733 spin_unlock_irq(&demux
->lock
);
734 mutex_unlock(&demux
->mutex
);
739 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed
*ts_feed
)
741 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
742 struct dvb_demux
*demux
= feed
->demux
;
745 mutex_lock(&demux
->mutex
);
747 if (feed
->state
< DMX_STATE_GO
) {
748 mutex_unlock(&demux
->mutex
);
752 if (!demux
->stop_feed
) {
753 mutex_unlock(&demux
->mutex
);
757 ret
= demux
->stop_feed(feed
);
759 spin_lock_irq(&demux
->lock
);
760 ts_feed
->is_filtering
= 0;
761 feed
->state
= DMX_STATE_ALLOCATED
;
762 spin_unlock_irq(&demux
->lock
);
763 mutex_unlock(&demux
->mutex
);
768 static int dvbdmx_allocate_ts_feed(struct dmx_demux
*dmx
,
769 struct dmx_ts_feed
**ts_feed
,
772 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
773 struct dvb_demux_feed
*feed
;
775 if (mutex_lock_interruptible(&demux
->mutex
))
778 if (!(feed
= dvb_dmx_feed_alloc(demux
))) {
779 mutex_unlock(&demux
->mutex
);
783 feed
->type
= DMX_TYPE_TS
;
784 feed
->cb
.ts
= callback
;
787 feed
->peslen
= 0xfffa;
789 (*ts_feed
) = &feed
->feed
.ts
;
790 (*ts_feed
)->parent
= dmx
;
791 (*ts_feed
)->priv
= NULL
;
792 (*ts_feed
)->is_filtering
= 0;
793 (*ts_feed
)->start_filtering
= dmx_ts_feed_start_filtering
;
794 (*ts_feed
)->stop_filtering
= dmx_ts_feed_stop_filtering
;
795 (*ts_feed
)->set
= dmx_ts_feed_set
;
797 if (!(feed
->filter
= dvb_dmx_filter_alloc(demux
))) {
798 feed
->state
= DMX_STATE_FREE
;
799 mutex_unlock(&demux
->mutex
);
803 feed
->filter
->type
= DMX_TYPE_TS
;
804 feed
->filter
->feed
= feed
;
805 feed
->filter
->state
= DMX_STATE_READY
;
807 mutex_unlock(&demux
->mutex
);
812 static int dvbdmx_release_ts_feed(struct dmx_demux
*dmx
,
813 struct dmx_ts_feed
*ts_feed
)
815 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
816 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
818 mutex_lock(&demux
->mutex
);
820 if (feed
->state
== DMX_STATE_FREE
) {
821 mutex_unlock(&demux
->mutex
);
825 feed
->state
= DMX_STATE_FREE
;
826 feed
->filter
->state
= DMX_STATE_FREE
;
828 dvb_demux_feed_del(feed
);
832 if (feed
->ts_type
& TS_DECODER
&& feed
->pes_type
< DMX_PES_OTHER
)
833 demux
->pesfilter
[feed
->pes_type
] = NULL
;
835 mutex_unlock(&demux
->mutex
);
839 /******************************************************************************
840 * dmx_section_feed API calls
841 ******************************************************************************/
843 static int dmx_section_feed_allocate_filter(struct dmx_section_feed
*feed
,
844 struct dmx_section_filter
**filter
)
846 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
847 struct dvb_demux
*dvbdemux
= dvbdmxfeed
->demux
;
848 struct dvb_demux_filter
*dvbdmxfilter
;
850 if (mutex_lock_interruptible(&dvbdemux
->mutex
))
853 dvbdmxfilter
= dvb_dmx_filter_alloc(dvbdemux
);
855 mutex_unlock(&dvbdemux
->mutex
);
859 spin_lock_irq(&dvbdemux
->lock
);
860 *filter
= &dvbdmxfilter
->filter
;
861 (*filter
)->parent
= feed
;
862 (*filter
)->priv
= NULL
;
863 dvbdmxfilter
->feed
= dvbdmxfeed
;
864 dvbdmxfilter
->type
= DMX_TYPE_SEC
;
865 dvbdmxfilter
->state
= DMX_STATE_READY
;
866 dvbdmxfilter
->next
= dvbdmxfeed
->filter
;
867 dvbdmxfeed
->filter
= dvbdmxfilter
;
868 spin_unlock_irq(&dvbdemux
->lock
);
870 mutex_unlock(&dvbdemux
->mutex
);
874 static int dmx_section_feed_set(struct dmx_section_feed
*feed
,
875 u16 pid
, int check_crc
)
877 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
878 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
883 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
886 dvb_demux_feed_add(dvbdmxfeed
);
888 dvbdmxfeed
->pid
= pid
;
889 dvbdmxfeed
->feed
.sec
.check_crc
= check_crc
;
891 dvbdmxfeed
->state
= DMX_STATE_READY
;
892 mutex_unlock(&dvbdmx
->mutex
);
896 static void prepare_secfilters(struct dvb_demux_feed
*dvbdmxfeed
)
899 struct dvb_demux_filter
*f
;
900 struct dmx_section_filter
*sf
;
901 u8 mask
, mode
, doneq
;
903 if (!(f
= dvbdmxfeed
->filter
))
908 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
909 mode
= sf
->filter_mode
[i
];
910 mask
= sf
->filter_mask
[i
];
911 f
->maskandmode
[i
] = mask
& mode
;
912 doneq
|= f
->maskandnotmode
[i
] = mask
& ~mode
;
914 f
->doneq
= doneq
? true : false;
915 } while ((f
= f
->next
));
918 static int dmx_section_feed_start_filtering(struct dmx_section_feed
*feed
)
920 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
921 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
924 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
927 if (feed
->is_filtering
) {
928 mutex_unlock(&dvbdmx
->mutex
);
932 if (!dvbdmxfeed
->filter
) {
933 mutex_unlock(&dvbdmx
->mutex
);
937 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
938 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
939 dvbdmxfeed
->feed
.sec
.secbufp
= 0;
940 dvbdmxfeed
->feed
.sec
.seclen
= 0;
942 if (!dvbdmx
->start_feed
) {
943 mutex_unlock(&dvbdmx
->mutex
);
947 prepare_secfilters(dvbdmxfeed
);
949 if ((ret
= dvbdmx
->start_feed(dvbdmxfeed
)) < 0) {
950 mutex_unlock(&dvbdmx
->mutex
);
954 spin_lock_irq(&dvbdmx
->lock
);
955 feed
->is_filtering
= 1;
956 dvbdmxfeed
->state
= DMX_STATE_GO
;
957 spin_unlock_irq(&dvbdmx
->lock
);
959 mutex_unlock(&dvbdmx
->mutex
);
963 static int dmx_section_feed_stop_filtering(struct dmx_section_feed
*feed
)
965 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
966 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
969 mutex_lock(&dvbdmx
->mutex
);
971 if (!dvbdmx
->stop_feed
) {
972 mutex_unlock(&dvbdmx
->mutex
);
976 ret
= dvbdmx
->stop_feed(dvbdmxfeed
);
978 spin_lock_irq(&dvbdmx
->lock
);
979 dvbdmxfeed
->state
= DMX_STATE_READY
;
980 feed
->is_filtering
= 0;
981 spin_unlock_irq(&dvbdmx
->lock
);
983 mutex_unlock(&dvbdmx
->mutex
);
987 static int dmx_section_feed_release_filter(struct dmx_section_feed
*feed
,
988 struct dmx_section_filter
*filter
)
990 struct dvb_demux_filter
*dvbdmxfilter
= (struct dvb_demux_filter
*)filter
, *f
;
991 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
992 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
994 mutex_lock(&dvbdmx
->mutex
);
996 if (dvbdmxfilter
->feed
!= dvbdmxfeed
) {
997 mutex_unlock(&dvbdmx
->mutex
);
1001 if (feed
->is_filtering
) {
1002 /* release dvbdmx->mutex as far as it is
1003 acquired by stop_filtering() itself */
1004 mutex_unlock(&dvbdmx
->mutex
);
1005 feed
->stop_filtering(feed
);
1006 mutex_lock(&dvbdmx
->mutex
);
1009 spin_lock_irq(&dvbdmx
->lock
);
1010 f
= dvbdmxfeed
->filter
;
1012 if (f
== dvbdmxfilter
) {
1013 dvbdmxfeed
->filter
= dvbdmxfilter
->next
;
1015 while (f
->next
!= dvbdmxfilter
)
1017 f
->next
= f
->next
->next
;
1020 dvbdmxfilter
->state
= DMX_STATE_FREE
;
1021 spin_unlock_irq(&dvbdmx
->lock
);
1022 mutex_unlock(&dvbdmx
->mutex
);
1026 static int dvbdmx_allocate_section_feed(struct dmx_demux
*demux
,
1027 struct dmx_section_feed
**feed
,
1028 dmx_section_cb callback
)
1030 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1031 struct dvb_demux_feed
*dvbdmxfeed
;
1033 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
1034 return -ERESTARTSYS
;
1036 if (!(dvbdmxfeed
= dvb_dmx_feed_alloc(dvbdmx
))) {
1037 mutex_unlock(&dvbdmx
->mutex
);
1041 dvbdmxfeed
->type
= DMX_TYPE_SEC
;
1042 dvbdmxfeed
->cb
.sec
= callback
;
1043 dvbdmxfeed
->demux
= dvbdmx
;
1044 dvbdmxfeed
->pid
= 0xffff;
1045 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
1046 dvbdmxfeed
->feed
.sec
.secbufp
= dvbdmxfeed
->feed
.sec
.seclen
= 0;
1047 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
1048 dvbdmxfeed
->filter
= NULL
;
1050 (*feed
) = &dvbdmxfeed
->feed
.sec
;
1051 (*feed
)->is_filtering
= 0;
1052 (*feed
)->parent
= demux
;
1053 (*feed
)->priv
= NULL
;
1055 (*feed
)->set
= dmx_section_feed_set
;
1056 (*feed
)->allocate_filter
= dmx_section_feed_allocate_filter
;
1057 (*feed
)->start_filtering
= dmx_section_feed_start_filtering
;
1058 (*feed
)->stop_filtering
= dmx_section_feed_stop_filtering
;
1059 (*feed
)->release_filter
= dmx_section_feed_release_filter
;
1061 mutex_unlock(&dvbdmx
->mutex
);
1065 static int dvbdmx_release_section_feed(struct dmx_demux
*demux
,
1066 struct dmx_section_feed
*feed
)
1068 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
1069 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1071 mutex_lock(&dvbdmx
->mutex
);
1073 if (dvbdmxfeed
->state
== DMX_STATE_FREE
) {
1074 mutex_unlock(&dvbdmx
->mutex
);
1077 dvbdmxfeed
->state
= DMX_STATE_FREE
;
1079 dvb_demux_feed_del(dvbdmxfeed
);
1081 dvbdmxfeed
->pid
= 0xffff;
1083 mutex_unlock(&dvbdmx
->mutex
);
1087 /******************************************************************************
1088 * dvb_demux kernel data API calls
1089 ******************************************************************************/
1091 static int dvbdmx_open(struct dmx_demux
*demux
)
1093 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1095 if (dvbdemux
->users
>= MAX_DVB_DEMUX_USERS
)
1102 static int dvbdmx_close(struct dmx_demux
*demux
)
1104 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1106 if (dvbdemux
->users
== 0)
1110 //FIXME: release any unneeded resources if users==0
1114 static int dvbdmx_write(struct dmx_demux
*demux
, const char __user
*buf
, size_t count
)
1116 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1119 if ((!demux
->frontend
) || (demux
->frontend
->source
!= DMX_MEMORY_FE
))
1122 p
= memdup_user(buf
, count
);
1125 if (mutex_lock_interruptible(&dvbdemux
->mutex
)) {
1127 return -ERESTARTSYS
;
1129 dvb_dmx_swfilter(dvbdemux
, p
, count
);
1131 mutex_unlock(&dvbdemux
->mutex
);
1133 if (signal_pending(current
))
1138 static int dvbdmx_add_frontend(struct dmx_demux
*demux
,
1139 struct dmx_frontend
*frontend
)
1141 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1142 struct list_head
*head
= &dvbdemux
->frontend_list
;
1144 list_add(&(frontend
->connectivity_list
), head
);
1149 static int dvbdmx_remove_frontend(struct dmx_demux
*demux
,
1150 struct dmx_frontend
*frontend
)
1152 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1153 struct list_head
*pos
, *n
, *head
= &dvbdemux
->frontend_list
;
1155 list_for_each_safe(pos
, n
, head
) {
1156 if (DMX_FE_ENTRY(pos
) == frontend
) {
1165 static struct list_head
*dvbdmx_get_frontends(struct dmx_demux
*demux
)
1167 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1169 if (list_empty(&dvbdemux
->frontend_list
))
1172 return &dvbdemux
->frontend_list
;
1175 static int dvbdmx_connect_frontend(struct dmx_demux
*demux
,
1176 struct dmx_frontend
*frontend
)
1178 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1180 if (demux
->frontend
)
1183 mutex_lock(&dvbdemux
->mutex
);
1185 demux
->frontend
= frontend
;
1186 mutex_unlock(&dvbdemux
->mutex
);
1190 static int dvbdmx_disconnect_frontend(struct dmx_demux
*demux
)
1192 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1194 mutex_lock(&dvbdemux
->mutex
);
1196 demux
->frontend
= NULL
;
1197 mutex_unlock(&dvbdemux
->mutex
);
1201 static int dvbdmx_get_pes_pids(struct dmx_demux
*demux
, u16
* pids
)
1203 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1205 memcpy(pids
, dvbdemux
->pids
, 5 * sizeof(u16
));
1209 int dvb_dmx_init(struct dvb_demux
*dvbdemux
)
1212 struct dmx_demux
*dmx
= &dvbdemux
->dmx
;
1214 dvbdemux
->cnt_storage
= NULL
;
1215 dvbdemux
->users
= 0;
1216 dvbdemux
->filter
= vmalloc(dvbdemux
->filternum
* sizeof(struct dvb_demux_filter
));
1218 if (!dvbdemux
->filter
)
1221 dvbdemux
->feed
= vmalloc(dvbdemux
->feednum
* sizeof(struct dvb_demux_feed
));
1222 if (!dvbdemux
->feed
) {
1223 vfree(dvbdemux
->filter
);
1224 dvbdemux
->filter
= NULL
;
1227 for (i
= 0; i
< dvbdemux
->filternum
; i
++) {
1228 dvbdemux
->filter
[i
].state
= DMX_STATE_FREE
;
1229 dvbdemux
->filter
[i
].index
= i
;
1231 for (i
= 0; i
< dvbdemux
->feednum
; i
++) {
1232 dvbdemux
->feed
[i
].state
= DMX_STATE_FREE
;
1233 dvbdemux
->feed
[i
].index
= i
;
1236 dvbdemux
->cnt_storage
= vmalloc(MAX_PID
+ 1);
1237 if (!dvbdemux
->cnt_storage
)
1238 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1240 INIT_LIST_HEAD(&dvbdemux
->frontend_list
);
1242 for (i
= 0; i
< DMX_PES_OTHER
; i
++) {
1243 dvbdemux
->pesfilter
[i
] = NULL
;
1244 dvbdemux
->pids
[i
] = 0xffff;
1247 INIT_LIST_HEAD(&dvbdemux
->feed_list
);
1249 dvbdemux
->playing
= 0;
1250 dvbdemux
->recording
= 0;
1251 dvbdemux
->tsbufp
= 0;
1253 if (!dvbdemux
->check_crc32
)
1254 dvbdemux
->check_crc32
= dvb_dmx_crc32
;
1256 if (!dvbdemux
->memcopy
)
1257 dvbdemux
->memcopy
= dvb_dmx_memcopy
;
1259 dmx
->frontend
= NULL
;
1260 dmx
->priv
= dvbdemux
;
1261 dmx
->open
= dvbdmx_open
;
1262 dmx
->close
= dvbdmx_close
;
1263 dmx
->write
= dvbdmx_write
;
1264 dmx
->allocate_ts_feed
= dvbdmx_allocate_ts_feed
;
1265 dmx
->release_ts_feed
= dvbdmx_release_ts_feed
;
1266 dmx
->allocate_section_feed
= dvbdmx_allocate_section_feed
;
1267 dmx
->release_section_feed
= dvbdmx_release_section_feed
;
1269 dmx
->add_frontend
= dvbdmx_add_frontend
;
1270 dmx
->remove_frontend
= dvbdmx_remove_frontend
;
1271 dmx
->get_frontends
= dvbdmx_get_frontends
;
1272 dmx
->connect_frontend
= dvbdmx_connect_frontend
;
1273 dmx
->disconnect_frontend
= dvbdmx_disconnect_frontend
;
1274 dmx
->get_pes_pids
= dvbdmx_get_pes_pids
;
1276 mutex_init(&dvbdemux
->mutex
);
1277 spin_lock_init(&dvbdemux
->lock
);
1282 EXPORT_SYMBOL(dvb_dmx_init
);
1284 void dvb_dmx_release(struct dvb_demux
*dvbdemux
)
1286 vfree(dvbdemux
->cnt_storage
);
1287 vfree(dvbdemux
->filter
);
1288 vfree(dvbdemux
->feed
);
1291 EXPORT_SYMBOL(dvb_dmx_release
);