1 // SPDX-License-Identifier: LGPL-2.1-or-later
3 * dvb_demux.c - DVB kernel demux API
5 * Copyright (C) 2000-2001 Ralph Metzler <ralph@convergence.de>
6 * & Marcus Metzler <marcus@convergence.de>
7 * for convergence integrated media GmbH
10 #define pr_fmt(fmt) "dvb_demux: " fmt
12 #include <linux/sched/signal.h>
13 #include <linux/spinlock.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/module.h>
17 #include <linux/poll.h>
18 #include <linux/string.h>
19 #include <linux/crc32.h>
20 #include <linux/uaccess.h>
21 #include <asm/div64.h>
23 #include <media/dvb_demux.h>
25 static int dvb_demux_tscheck
;
26 module_param(dvb_demux_tscheck
, int, 0644);
27 MODULE_PARM_DESC(dvb_demux_tscheck
,
28 "enable transport stream continuity and TEI check");
30 static int dvb_demux_speedcheck
;
31 module_param(dvb_demux_speedcheck
, int, 0644);
32 MODULE_PARM_DESC(dvb_demux_speedcheck
,
33 "enable transport stream speed check");
35 static int dvb_demux_feed_err_pkts
= 1;
36 module_param(dvb_demux_feed_err_pkts
, int, 0644);
37 MODULE_PARM_DESC(dvb_demux_feed_err_pkts
,
38 "when set to 0, drop packets with the TEI bit set (1 by default)");
40 #define dprintk(fmt, arg...) \
41 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
43 #define dprintk_tscheck(x...) do { \
44 if (dvb_demux_tscheck && printk_ratelimit()) \
48 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
49 # define dprintk_sect_loss(x...) dprintk(x)
51 # define dprintk_sect_loss(x...)
54 #define set_buf_flags(__feed, __flag) \
56 (__feed)->buffer_flags |= (__flag); \
59 /******************************************************************************
60 * static inlined helper functions
61 ******************************************************************************/
63 static inline u16
section_length(const u8
*buf
)
65 return 3 + ((buf
[1] & 0x0f) << 8) + buf
[2];
68 static inline u16
ts_pid(const u8
*buf
)
70 return ((buf
[1] & 0x1f) << 8) + buf
[2];
73 static inline u8
payload(const u8
*tsp
)
75 if (!(tsp
[3] & 0x10)) // no payload?
78 if (tsp
[3] & 0x20) { // adaptation field?
79 if (tsp
[4] > 183) // corrupted data?
82 return 184 - 1 - tsp
[4];
88 static u32
dvb_dmx_crc32(struct dvb_demux_feed
*f
, const u8
*src
, size_t len
)
90 return (f
->feed
.sec
.crc_val
= crc32_be(f
->feed
.sec
.crc_val
, src
, len
));
93 static void dvb_dmx_memcopy(struct dvb_demux_feed
*f
, u8
*d
, const u8
*s
,
99 /******************************************************************************
100 * Software filter functions
101 ******************************************************************************/
103 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed
*feed
,
106 int count
= payload(buf
);
117 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
119 set_buf_flags(feed
, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
120 dprintk_sect_loss("missed packet: %d instead of %d!\n",
121 cc
, (feed
->cc
+ 1) & 0x0f);
125 if (buf
[1] & 0x40) // PUSI ?
126 feed
->peslen
= 0xfffa;
128 feed
->peslen
+= count
;
130 return feed
->cb
.ts(&buf
[p
], count
, NULL
, 0, &feed
->feed
.ts
,
131 &feed
->buffer_flags
);
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
, &feed
->buffer_flags
);
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
)) {
173 set_buf_flags(feed
, DMX_BUFFER_FLAG_HAD_CRC32_DISCARD
);
179 if (dvb_dmx_swfilter_sectionfilter(feed
, f
) < 0)
181 } while ((f
= f
->next
) && sec
->is_filtering
);
188 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed
*feed
)
190 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
192 if (sec
->secbufp
< sec
->tsfeedp
) {
193 int n
= sec
->tsfeedp
- sec
->secbufp
;
196 * Section padding is done with 0xff bytes entirely.
197 * Due to speed reasons, we won't check all of them
198 * but just first and last.
200 if (sec
->secbuf
[0] != 0xff || sec
->secbuf
[n
- 1] != 0xff) {
202 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
203 dprintk_sect_loss("section ts padding loss: %d/%d\n",
205 dprintk_sect_loss("pad data: %*ph\n", n
, sec
->secbuf
);
209 sec
->tsfeedp
= sec
->secbufp
= sec
->seclen
= 0;
210 sec
->secbuf
= sec
->secbuf_base
;
214 * Losless Section Demux 1.4.1 by Emard
216 * - middle of section A (no PUSI)
217 * - end of section A and start of section B
218 * (with PUSI pointing to the start of the second section)
220 * In this case, without feed->pusi_seen you'll receive a garbage section
221 * consisting of the end of section A. Basically because tsfeedp
222 * is incemented and the use=0 condition is not raised
223 * when the second packet arrives.
226 * when demux is started, let feed->pusi_seen = false to
227 * prevent initial feeding of garbage from the end of
228 * previous section. When you for the first time see PUSI=1
229 * then set feed->pusi_seen = true
231 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed
*feed
,
232 const u8
*buf
, u8 len
)
234 struct dvb_demux
*demux
= feed
->demux
;
235 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
238 if (sec
->tsfeedp
>= DMX_MAX_SECFEED_SIZE
)
241 if (sec
->tsfeedp
+ len
> DMX_MAX_SECFEED_SIZE
) {
242 set_buf_flags(feed
, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
243 dprintk_sect_loss("section buffer full loss: %d/%d\n",
244 sec
->tsfeedp
+ len
- DMX_MAX_SECFEED_SIZE
,
245 DMX_MAX_SECFEED_SIZE
);
246 len
= DMX_MAX_SECFEED_SIZE
- sec
->tsfeedp
;
252 demux
->memcopy(feed
, sec
->secbuf_base
+ sec
->tsfeedp
, buf
, len
);
256 * Dump all the sections we can find in the data (Emard)
258 limit
= sec
->tsfeedp
;
259 if (limit
> DMX_MAX_SECFEED_SIZE
)
260 return -1; /* internal error should never happen */
262 /* to be sure always set secbuf */
263 sec
->secbuf
= sec
->secbuf_base
+ sec
->secbufp
;
265 while (sec
->secbufp
+ 2 < limit
) {
266 seclen
= section_length(sec
->secbuf
);
267 if (seclen
<= 0 || seclen
> DMX_MAX_SECTION_SIZE
268 || seclen
+ sec
->secbufp
> limit
)
270 sec
->seclen
= seclen
;
272 /* dump [secbuf .. secbuf+seclen) */
273 if (feed
->pusi_seen
) {
274 dvb_dmx_swfilter_section_feed(feed
);
277 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
278 dprintk_sect_loss("pusi not seen, discarding section data\n");
280 sec
->secbufp
+= seclen
; /* secbufp and secbuf moving together is */
281 sec
->secbuf
+= seclen
; /* redundant but saves pointer arithmetic */
287 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed
*feed
,
294 count
= payload(buf
);
296 if (count
== 0) /* count == 0 if no payload or out of range */
299 p
= 188 - count
; /* payload start */
302 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
305 /* adaption field present, check for discontinuity_indicator */
306 if ((buf
[4] > 0) && (buf
[5] & 0x80))
313 DMX_BUFFER_FLAG_DISCONTINUITY_INDICATOR
);
314 dprintk_sect_loss("%d frame with disconnect indicator\n",
318 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
319 dprintk_sect_loss("discontinuity: %d instead of %d. %d bytes lost\n",
320 cc
, (feed
->cc
+ 1) & 0x0f, count
+ 4);
323 * those bytes under some circumstances will again be reported
324 * in the following dvb_dmx_swfilter_section_new
328 * Discontinuity detected. Reset pusi_seen to
329 * stop feeding of suspicious data until next PUSI=1 arrives
331 * FIXME: does it make sense if the MPEG-TS is the one
332 * reporting discontinuity?
335 feed
->pusi_seen
= false;
336 dvb_dmx_swfilter_section_new(feed
);
341 /* PUSI=1 (is set), section boundary is here */
342 if (count
> 1 && buf
[p
] < count
) {
343 const u8
*before
= &buf
[p
+ 1];
344 u8 before_len
= buf
[p
];
345 const u8
*after
= &before
[before_len
];
346 u8 after_len
= count
- 1 - before_len
;
348 dvb_dmx_swfilter_section_copy_dump(feed
, before
,
350 /* before start of new section, set pusi_seen */
351 feed
->pusi_seen
= true;
352 dvb_dmx_swfilter_section_new(feed
);
353 dvb_dmx_swfilter_section_copy_dump(feed
, after
,
355 } else if (count
> 0) {
357 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED
);
358 dprintk_sect_loss("PUSI=1 but %d bytes lost\n", count
);
361 /* PUSI=0 (is not set), no section boundary */
362 dvb_dmx_swfilter_section_copy_dump(feed
, &buf
[p
], count
);
368 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed
*feed
,
371 switch (feed
->type
) {
373 if (!feed
->feed
.ts
.is_filtering
)
375 if (feed
->ts_type
& TS_PACKET
) {
376 if (feed
->ts_type
& TS_PAYLOAD_ONLY
)
377 dvb_dmx_swfilter_payload(feed
, buf
);
379 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
,
380 &feed
->buffer_flags
);
382 /* Used only on full-featured devices */
383 if (feed
->ts_type
& TS_DECODER
)
384 if (feed
->demux
->write_to_decoder
)
385 feed
->demux
->write_to_decoder(feed
, buf
, 188);
389 if (!feed
->feed
.sec
.is_filtering
)
391 if (dvb_dmx_swfilter_section_packet(feed
, buf
) < 0)
392 feed
->feed
.sec
.seclen
= feed
->feed
.sec
.secbufp
= 0;
400 #define DVR_FEED(f) \
401 (((f)->type == DMX_TYPE_TS) && \
402 ((f)->feed.ts.is_filtering) && \
403 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
405 static void dvb_dmx_swfilter_packet(struct dvb_demux
*demux
, const u8
*buf
)
407 struct dvb_demux_feed
*feed
;
408 u16 pid
= ts_pid(buf
);
411 if (dvb_demux_speedcheck
) {
413 u64 speed_bytes
, speed_timedelta
;
415 demux
->speed_pkts_cnt
++;
417 /* show speed every SPEED_PKTS_INTERVAL packets */
418 if (!(demux
->speed_pkts_cnt
% SPEED_PKTS_INTERVAL
)) {
419 cur_time
= ktime_get();
421 if (ktime_to_ns(demux
->speed_last_time
) != 0) {
422 speed_bytes
= (u64
)demux
->speed_pkts_cnt
424 /* convert to 1024 basis */
425 speed_bytes
= 1000 * div64_u64(speed_bytes
,
427 speed_timedelta
= ktime_ms_delta(cur_time
,
428 demux
->speed_last_time
);
430 dprintk("TS speed %llu Kbits/sec \n",
431 div64_u64(speed_bytes
,
435 demux
->speed_last_time
= cur_time
;
436 demux
->speed_pkts_cnt
= 0;
441 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
442 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
444 set_buf_flags(feed
, DMX_BUFFER_FLAG_TEI
);
446 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
448 /* data in this packet can't be trusted - drop it unless
449 * module option dvb_demux_feed_err_pkts is set */
450 if (!dvb_demux_feed_err_pkts
)
452 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
453 if (demux
->cnt_storage
&& dvb_demux_tscheck
) {
454 /* check pkt counter */
457 demux
->cnt_storage
[pid
] =
458 (demux
->cnt_storage
[pid
] + 1) & 0xf;
460 if ((buf
[3] & 0xf) != demux
->cnt_storage
[pid
]) {
461 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
462 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
465 DMX_BUFFER_PKT_COUNTER_MISMATCH
);
468 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
469 pid
, demux
->cnt_storage
[pid
],
471 demux
->cnt_storage
[pid
] = buf
[3] & 0xf;
477 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
478 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
481 /* copy each packet only once to the dvr device, even
482 * if a PID is in multiple filters (e.g. video + PCR) */
483 if ((DVR_FEED(feed
)) && (dvr_done
++))
486 if (feed
->pid
== pid
)
487 dvb_dmx_swfilter_packet_type(feed
, buf
);
488 else if (feed
->pid
== 0x2000)
489 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
,
490 &feed
->buffer_flags
);
494 void dvb_dmx_swfilter_packets(struct dvb_demux
*demux
, const u8
*buf
,
499 spin_lock_irqsave(&demux
->lock
, flags
);
503 dvb_dmx_swfilter_packet(demux
, buf
);
507 spin_unlock_irqrestore(&demux
->lock
, flags
);
510 EXPORT_SYMBOL(dvb_dmx_swfilter_packets
);
512 static inline int find_next_packet(const u8
*buf
, int pos
, size_t count
,
515 int start
= pos
, lost
;
517 while (pos
< count
) {
518 if (buf
[pos
] == 0x47 ||
519 (pktsize
== 204 && buf
[pos
] == 0xB8))
526 /* This garbage is part of a valid packet? */
527 int backtrack
= pos
- pktsize
;
528 if (backtrack
>= 0 && (buf
[backtrack
] == 0x47 ||
529 (pktsize
== 204 && buf
[backtrack
] == 0xB8)))
536 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
537 static inline void _dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
,
538 size_t count
, const int pktsize
)
544 spin_lock_irqsave(&demux
->lock
, flags
);
546 if (demux
->tsbufp
) { /* tsbuf[0] is now 0x47. */
550 memcpy(&demux
->tsbuf
[i
], buf
, count
);
551 demux
->tsbufp
+= count
;
554 memcpy(&demux
->tsbuf
[i
], buf
, j
);
555 if (demux
->tsbuf
[0] == 0x47) /* double check */
556 dvb_dmx_swfilter_packet(demux
, demux
->tsbuf
);
562 p
= find_next_packet(buf
, p
, count
, pktsize
);
565 if (count
- p
< pktsize
)
570 if (pktsize
== 204 && (*q
== 0xB8)) {
571 memcpy(demux
->tsbuf
, q
, 188);
572 demux
->tsbuf
[0] = 0x47;
575 dvb_dmx_swfilter_packet(demux
, q
);
581 memcpy(demux
->tsbuf
, &buf
[p
], i
);
583 if (pktsize
== 204 && demux
->tsbuf
[0] == 0xB8)
584 demux
->tsbuf
[0] = 0x47;
588 spin_unlock_irqrestore(&demux
->lock
, flags
);
591 void dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
593 _dvb_dmx_swfilter(demux
, buf
, count
, 188);
595 EXPORT_SYMBOL(dvb_dmx_swfilter
);
597 void dvb_dmx_swfilter_204(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
599 _dvb_dmx_swfilter(demux
, buf
, count
, 204);
601 EXPORT_SYMBOL(dvb_dmx_swfilter_204
);
603 void dvb_dmx_swfilter_raw(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
607 spin_lock_irqsave(&demux
->lock
, flags
);
609 demux
->feed
->cb
.ts(buf
, count
, NULL
, 0, &demux
->feed
->feed
.ts
,
610 &demux
->feed
->buffer_flags
);
612 spin_unlock_irqrestore(&demux
->lock
, flags
);
614 EXPORT_SYMBOL(dvb_dmx_swfilter_raw
);
616 static struct dvb_demux_filter
*dvb_dmx_filter_alloc(struct dvb_demux
*demux
)
620 for (i
= 0; i
< demux
->filternum
; i
++)
621 if (demux
->filter
[i
].state
== DMX_STATE_FREE
)
624 if (i
== demux
->filternum
)
627 demux
->filter
[i
].state
= DMX_STATE_ALLOCATED
;
629 return &demux
->filter
[i
];
632 static struct dvb_demux_feed
*dvb_dmx_feed_alloc(struct dvb_demux
*demux
)
636 for (i
= 0; i
< demux
->feednum
; i
++)
637 if (demux
->feed
[i
].state
== DMX_STATE_FREE
)
640 if (i
== demux
->feednum
)
643 demux
->feed
[i
].state
= DMX_STATE_ALLOCATED
;
645 return &demux
->feed
[i
];
648 static int dvb_demux_feed_find(struct dvb_demux_feed
*feed
)
650 struct dvb_demux_feed
*entry
;
652 list_for_each_entry(entry
, &feed
->demux
->feed_list
, list_head
)
659 static void dvb_demux_feed_add(struct dvb_demux_feed
*feed
)
661 spin_lock_irq(&feed
->demux
->lock
);
662 if (dvb_demux_feed_find(feed
)) {
663 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
664 __func__
, feed
->type
, feed
->state
, feed
->pid
);
668 list_add(&feed
->list_head
, &feed
->demux
->feed_list
);
670 spin_unlock_irq(&feed
->demux
->lock
);
673 static void dvb_demux_feed_del(struct dvb_demux_feed
*feed
)
675 spin_lock_irq(&feed
->demux
->lock
);
676 if (!(dvb_demux_feed_find(feed
))) {
677 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
678 __func__
, feed
->type
, feed
->state
, feed
->pid
);
682 list_del(&feed
->list_head
);
684 spin_unlock_irq(&feed
->demux
->lock
);
687 static int dmx_ts_feed_set(struct dmx_ts_feed
*ts_feed
, u16 pid
, int ts_type
,
688 enum dmx_ts_pes pes_type
, ktime_t timeout
)
690 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
691 struct dvb_demux
*demux
= feed
->demux
;
693 if (pid
> DMX_MAX_PID
)
696 if (mutex_lock_interruptible(&demux
->mutex
))
699 if (ts_type
& TS_DECODER
) {
700 if (pes_type
>= DMX_PES_OTHER
) {
701 mutex_unlock(&demux
->mutex
);
705 if (demux
->pesfilter
[pes_type
] &&
706 demux
->pesfilter
[pes_type
] != feed
) {
707 mutex_unlock(&demux
->mutex
);
711 demux
->pesfilter
[pes_type
] = feed
;
712 demux
->pids
[pes_type
] = pid
;
715 dvb_demux_feed_add(feed
);
718 feed
->timeout
= timeout
;
719 feed
->ts_type
= ts_type
;
720 feed
->pes_type
= pes_type
;
722 feed
->state
= DMX_STATE_READY
;
723 mutex_unlock(&demux
->mutex
);
728 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed
*ts_feed
)
730 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
731 struct dvb_demux
*demux
= feed
->demux
;
734 if (mutex_lock_interruptible(&demux
->mutex
))
737 if (feed
->state
!= DMX_STATE_READY
|| feed
->type
!= DMX_TYPE_TS
) {
738 mutex_unlock(&demux
->mutex
);
742 if (!demux
->start_feed
) {
743 mutex_unlock(&demux
->mutex
);
747 if ((ret
= demux
->start_feed(feed
)) < 0) {
748 mutex_unlock(&demux
->mutex
);
752 spin_lock_irq(&demux
->lock
);
753 ts_feed
->is_filtering
= 1;
754 feed
->state
= DMX_STATE_GO
;
755 spin_unlock_irq(&demux
->lock
);
756 mutex_unlock(&demux
->mutex
);
761 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed
*ts_feed
)
763 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
764 struct dvb_demux
*demux
= feed
->demux
;
767 mutex_lock(&demux
->mutex
);
769 if (feed
->state
< DMX_STATE_GO
) {
770 mutex_unlock(&demux
->mutex
);
774 if (!demux
->stop_feed
) {
775 mutex_unlock(&demux
->mutex
);
779 ret
= demux
->stop_feed(feed
);
781 spin_lock_irq(&demux
->lock
);
782 ts_feed
->is_filtering
= 0;
783 feed
->state
= DMX_STATE_ALLOCATED
;
784 spin_unlock_irq(&demux
->lock
);
785 mutex_unlock(&demux
->mutex
);
790 static int dvbdmx_allocate_ts_feed(struct dmx_demux
*dmx
,
791 struct dmx_ts_feed
**ts_feed
,
794 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
795 struct dvb_demux_feed
*feed
;
797 if (mutex_lock_interruptible(&demux
->mutex
))
800 if (!(feed
= dvb_dmx_feed_alloc(demux
))) {
801 mutex_unlock(&demux
->mutex
);
805 feed
->type
= DMX_TYPE_TS
;
806 feed
->cb
.ts
= callback
;
809 feed
->peslen
= 0xfffa;
810 feed
->buffer_flags
= 0;
812 (*ts_feed
) = &feed
->feed
.ts
;
813 (*ts_feed
)->parent
= dmx
;
814 (*ts_feed
)->priv
= NULL
;
815 (*ts_feed
)->is_filtering
= 0;
816 (*ts_feed
)->start_filtering
= dmx_ts_feed_start_filtering
;
817 (*ts_feed
)->stop_filtering
= dmx_ts_feed_stop_filtering
;
818 (*ts_feed
)->set
= dmx_ts_feed_set
;
820 if (!(feed
->filter
= dvb_dmx_filter_alloc(demux
))) {
821 feed
->state
= DMX_STATE_FREE
;
822 mutex_unlock(&demux
->mutex
);
826 feed
->filter
->type
= DMX_TYPE_TS
;
827 feed
->filter
->feed
= feed
;
828 feed
->filter
->state
= DMX_STATE_READY
;
830 mutex_unlock(&demux
->mutex
);
835 static int dvbdmx_release_ts_feed(struct dmx_demux
*dmx
,
836 struct dmx_ts_feed
*ts_feed
)
838 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
839 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
841 mutex_lock(&demux
->mutex
);
843 if (feed
->state
== DMX_STATE_FREE
) {
844 mutex_unlock(&demux
->mutex
);
848 feed
->state
= DMX_STATE_FREE
;
849 feed
->filter
->state
= DMX_STATE_FREE
;
851 dvb_demux_feed_del(feed
);
855 if (feed
->ts_type
& TS_DECODER
&& feed
->pes_type
< DMX_PES_OTHER
)
856 demux
->pesfilter
[feed
->pes_type
] = NULL
;
858 mutex_unlock(&demux
->mutex
);
862 /******************************************************************************
863 * dmx_section_feed API calls
864 ******************************************************************************/
866 static int dmx_section_feed_allocate_filter(struct dmx_section_feed
*feed
,
867 struct dmx_section_filter
**filter
)
869 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
870 struct dvb_demux
*dvbdemux
= dvbdmxfeed
->demux
;
871 struct dvb_demux_filter
*dvbdmxfilter
;
873 if (mutex_lock_interruptible(&dvbdemux
->mutex
))
876 dvbdmxfilter
= dvb_dmx_filter_alloc(dvbdemux
);
878 mutex_unlock(&dvbdemux
->mutex
);
882 spin_lock_irq(&dvbdemux
->lock
);
883 *filter
= &dvbdmxfilter
->filter
;
884 (*filter
)->parent
= feed
;
885 (*filter
)->priv
= NULL
;
886 dvbdmxfilter
->feed
= dvbdmxfeed
;
887 dvbdmxfilter
->type
= DMX_TYPE_SEC
;
888 dvbdmxfilter
->state
= DMX_STATE_READY
;
889 dvbdmxfilter
->next
= dvbdmxfeed
->filter
;
890 dvbdmxfeed
->filter
= dvbdmxfilter
;
891 spin_unlock_irq(&dvbdemux
->lock
);
893 mutex_unlock(&dvbdemux
->mutex
);
897 static int dmx_section_feed_set(struct dmx_section_feed
*feed
,
898 u16 pid
, int check_crc
)
900 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
901 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
906 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
909 dvb_demux_feed_add(dvbdmxfeed
);
911 dvbdmxfeed
->pid
= pid
;
912 dvbdmxfeed
->feed
.sec
.check_crc
= check_crc
;
914 dvbdmxfeed
->state
= DMX_STATE_READY
;
915 mutex_unlock(&dvbdmx
->mutex
);
919 static void prepare_secfilters(struct dvb_demux_feed
*dvbdmxfeed
)
922 struct dvb_demux_filter
*f
;
923 struct dmx_section_filter
*sf
;
924 u8 mask
, mode
, doneq
;
926 if (!(f
= dvbdmxfeed
->filter
))
931 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
932 mode
= sf
->filter_mode
[i
];
933 mask
= sf
->filter_mask
[i
];
934 f
->maskandmode
[i
] = mask
& mode
;
935 doneq
|= f
->maskandnotmode
[i
] = mask
& ~mode
;
937 f
->doneq
= doneq
? true : false;
938 } while ((f
= f
->next
));
941 static int dmx_section_feed_start_filtering(struct dmx_section_feed
*feed
)
943 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
944 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
947 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
950 if (feed
->is_filtering
) {
951 mutex_unlock(&dvbdmx
->mutex
);
955 if (!dvbdmxfeed
->filter
) {
956 mutex_unlock(&dvbdmx
->mutex
);
960 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
961 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
962 dvbdmxfeed
->feed
.sec
.secbufp
= 0;
963 dvbdmxfeed
->feed
.sec
.seclen
= 0;
964 dvbdmxfeed
->pusi_seen
= false;
966 if (!dvbdmx
->start_feed
) {
967 mutex_unlock(&dvbdmx
->mutex
);
971 prepare_secfilters(dvbdmxfeed
);
973 if ((ret
= dvbdmx
->start_feed(dvbdmxfeed
)) < 0) {
974 mutex_unlock(&dvbdmx
->mutex
);
978 spin_lock_irq(&dvbdmx
->lock
);
979 feed
->is_filtering
= 1;
980 dvbdmxfeed
->state
= DMX_STATE_GO
;
981 spin_unlock_irq(&dvbdmx
->lock
);
983 mutex_unlock(&dvbdmx
->mutex
);
987 static int dmx_section_feed_stop_filtering(struct dmx_section_feed
*feed
)
989 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
990 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
993 mutex_lock(&dvbdmx
->mutex
);
995 if (!dvbdmx
->stop_feed
) {
996 mutex_unlock(&dvbdmx
->mutex
);
1000 ret
= dvbdmx
->stop_feed(dvbdmxfeed
);
1002 spin_lock_irq(&dvbdmx
->lock
);
1003 dvbdmxfeed
->state
= DMX_STATE_READY
;
1004 feed
->is_filtering
= 0;
1005 spin_unlock_irq(&dvbdmx
->lock
);
1007 mutex_unlock(&dvbdmx
->mutex
);
1011 static int dmx_section_feed_release_filter(struct dmx_section_feed
*feed
,
1012 struct dmx_section_filter
*filter
)
1014 struct dvb_demux_filter
*dvbdmxfilter
= (struct dvb_demux_filter
*)filter
, *f
;
1015 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
1016 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
1018 mutex_lock(&dvbdmx
->mutex
);
1020 if (dvbdmxfilter
->feed
!= dvbdmxfeed
) {
1021 mutex_unlock(&dvbdmx
->mutex
);
1025 if (feed
->is_filtering
) {
1026 /* release dvbdmx->mutex as far as it is
1027 acquired by stop_filtering() itself */
1028 mutex_unlock(&dvbdmx
->mutex
);
1029 feed
->stop_filtering(feed
);
1030 mutex_lock(&dvbdmx
->mutex
);
1033 spin_lock_irq(&dvbdmx
->lock
);
1034 f
= dvbdmxfeed
->filter
;
1036 if (f
== dvbdmxfilter
) {
1037 dvbdmxfeed
->filter
= dvbdmxfilter
->next
;
1039 while (f
->next
!= dvbdmxfilter
)
1041 f
->next
= f
->next
->next
;
1044 dvbdmxfilter
->state
= DMX_STATE_FREE
;
1045 spin_unlock_irq(&dvbdmx
->lock
);
1046 mutex_unlock(&dvbdmx
->mutex
);
1050 static int dvbdmx_allocate_section_feed(struct dmx_demux
*demux
,
1051 struct dmx_section_feed
**feed
,
1052 dmx_section_cb callback
)
1054 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1055 struct dvb_demux_feed
*dvbdmxfeed
;
1057 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
1058 return -ERESTARTSYS
;
1060 if (!(dvbdmxfeed
= dvb_dmx_feed_alloc(dvbdmx
))) {
1061 mutex_unlock(&dvbdmx
->mutex
);
1065 dvbdmxfeed
->type
= DMX_TYPE_SEC
;
1066 dvbdmxfeed
->cb
.sec
= callback
;
1067 dvbdmxfeed
->demux
= dvbdmx
;
1068 dvbdmxfeed
->pid
= 0xffff;
1069 dvbdmxfeed
->buffer_flags
= 0;
1070 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
1071 dvbdmxfeed
->feed
.sec
.secbufp
= dvbdmxfeed
->feed
.sec
.seclen
= 0;
1072 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
1073 dvbdmxfeed
->filter
= NULL
;
1075 (*feed
) = &dvbdmxfeed
->feed
.sec
;
1076 (*feed
)->is_filtering
= 0;
1077 (*feed
)->parent
= demux
;
1078 (*feed
)->priv
= NULL
;
1080 (*feed
)->set
= dmx_section_feed_set
;
1081 (*feed
)->allocate_filter
= dmx_section_feed_allocate_filter
;
1082 (*feed
)->start_filtering
= dmx_section_feed_start_filtering
;
1083 (*feed
)->stop_filtering
= dmx_section_feed_stop_filtering
;
1084 (*feed
)->release_filter
= dmx_section_feed_release_filter
;
1086 mutex_unlock(&dvbdmx
->mutex
);
1090 static int dvbdmx_release_section_feed(struct dmx_demux
*demux
,
1091 struct dmx_section_feed
*feed
)
1093 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
1094 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1096 mutex_lock(&dvbdmx
->mutex
);
1098 if (dvbdmxfeed
->state
== DMX_STATE_FREE
) {
1099 mutex_unlock(&dvbdmx
->mutex
);
1102 dvbdmxfeed
->state
= DMX_STATE_FREE
;
1104 dvb_demux_feed_del(dvbdmxfeed
);
1106 dvbdmxfeed
->pid
= 0xffff;
1108 mutex_unlock(&dvbdmx
->mutex
);
1112 /******************************************************************************
1113 * dvb_demux kernel data API calls
1114 ******************************************************************************/
1116 static int dvbdmx_open(struct dmx_demux
*demux
)
1118 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1120 if (dvbdemux
->users
>= MAX_DVB_DEMUX_USERS
)
1127 static int dvbdmx_close(struct dmx_demux
*demux
)
1129 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1131 if (dvbdemux
->users
== 0)
1135 //FIXME: release any unneeded resources if users==0
1139 static int dvbdmx_write(struct dmx_demux
*demux
, const char __user
*buf
, size_t count
)
1141 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1144 if ((!demux
->frontend
) || (demux
->frontend
->source
!= DMX_MEMORY_FE
))
1147 p
= memdup_user(buf
, count
);
1150 if (mutex_lock_interruptible(&dvbdemux
->mutex
)) {
1152 return -ERESTARTSYS
;
1154 dvb_dmx_swfilter(dvbdemux
, p
, count
);
1156 mutex_unlock(&dvbdemux
->mutex
);
1158 if (signal_pending(current
))
1163 static int dvbdmx_add_frontend(struct dmx_demux
*demux
,
1164 struct dmx_frontend
*frontend
)
1166 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1167 struct list_head
*head
= &dvbdemux
->frontend_list
;
1169 list_add(&(frontend
->connectivity_list
), head
);
1174 static int dvbdmx_remove_frontend(struct dmx_demux
*demux
,
1175 struct dmx_frontend
*frontend
)
1177 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1178 struct list_head
*pos
, *n
, *head
= &dvbdemux
->frontend_list
;
1180 list_for_each_safe(pos
, n
, head
) {
1181 if (DMX_FE_ENTRY(pos
) == frontend
) {
1190 static struct list_head
*dvbdmx_get_frontends(struct dmx_demux
*demux
)
1192 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1194 if (list_empty(&dvbdemux
->frontend_list
))
1197 return &dvbdemux
->frontend_list
;
1200 static int dvbdmx_connect_frontend(struct dmx_demux
*demux
,
1201 struct dmx_frontend
*frontend
)
1203 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1205 if (demux
->frontend
)
1208 mutex_lock(&dvbdemux
->mutex
);
1210 demux
->frontend
= frontend
;
1211 mutex_unlock(&dvbdemux
->mutex
);
1215 static int dvbdmx_disconnect_frontend(struct dmx_demux
*demux
)
1217 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1219 mutex_lock(&dvbdemux
->mutex
);
1221 demux
->frontend
= NULL
;
1222 mutex_unlock(&dvbdemux
->mutex
);
1226 static int dvbdmx_get_pes_pids(struct dmx_demux
*demux
, u16
* pids
)
1228 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1230 memcpy(pids
, dvbdemux
->pids
, 5 * sizeof(u16
));
1234 int dvb_dmx_init(struct dvb_demux
*dvbdemux
)
1237 struct dmx_demux
*dmx
= &dvbdemux
->dmx
;
1239 dvbdemux
->cnt_storage
= NULL
;
1240 dvbdemux
->users
= 0;
1241 dvbdemux
->filter
= vmalloc(array_size(sizeof(struct dvb_demux_filter
),
1242 dvbdemux
->filternum
));
1244 if (!dvbdemux
->filter
)
1247 dvbdemux
->feed
= vmalloc(array_size(sizeof(struct dvb_demux_feed
),
1248 dvbdemux
->feednum
));
1249 if (!dvbdemux
->feed
) {
1250 vfree(dvbdemux
->filter
);
1251 dvbdemux
->filter
= NULL
;
1254 for (i
= 0; i
< dvbdemux
->filternum
; i
++) {
1255 dvbdemux
->filter
[i
].state
= DMX_STATE_FREE
;
1256 dvbdemux
->filter
[i
].index
= i
;
1258 for (i
= 0; i
< dvbdemux
->feednum
; i
++) {
1259 dvbdemux
->feed
[i
].state
= DMX_STATE_FREE
;
1260 dvbdemux
->feed
[i
].index
= i
;
1263 dvbdemux
->cnt_storage
= vmalloc(MAX_PID
+ 1);
1264 if (!dvbdemux
->cnt_storage
)
1265 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1267 INIT_LIST_HEAD(&dvbdemux
->frontend_list
);
1269 for (i
= 0; i
< DMX_PES_OTHER
; i
++) {
1270 dvbdemux
->pesfilter
[i
] = NULL
;
1271 dvbdemux
->pids
[i
] = 0xffff;
1274 INIT_LIST_HEAD(&dvbdemux
->feed_list
);
1276 dvbdemux
->playing
= 0;
1277 dvbdemux
->recording
= 0;
1278 dvbdemux
->tsbufp
= 0;
1280 if (!dvbdemux
->check_crc32
)
1281 dvbdemux
->check_crc32
= dvb_dmx_crc32
;
1283 if (!dvbdemux
->memcopy
)
1284 dvbdemux
->memcopy
= dvb_dmx_memcopy
;
1286 dmx
->frontend
= NULL
;
1287 dmx
->priv
= dvbdemux
;
1288 dmx
->open
= dvbdmx_open
;
1289 dmx
->close
= dvbdmx_close
;
1290 dmx
->write
= dvbdmx_write
;
1291 dmx
->allocate_ts_feed
= dvbdmx_allocate_ts_feed
;
1292 dmx
->release_ts_feed
= dvbdmx_release_ts_feed
;
1293 dmx
->allocate_section_feed
= dvbdmx_allocate_section_feed
;
1294 dmx
->release_section_feed
= dvbdmx_release_section_feed
;
1296 dmx
->add_frontend
= dvbdmx_add_frontend
;
1297 dmx
->remove_frontend
= dvbdmx_remove_frontend
;
1298 dmx
->get_frontends
= dvbdmx_get_frontends
;
1299 dmx
->connect_frontend
= dvbdmx_connect_frontend
;
1300 dmx
->disconnect_frontend
= dvbdmx_disconnect_frontend
;
1301 dmx
->get_pes_pids
= dvbdmx_get_pes_pids
;
1303 mutex_init(&dvbdemux
->mutex
);
1304 spin_lock_init(&dvbdemux
->lock
);
1309 EXPORT_SYMBOL(dvb_dmx_init
);
1311 void dvb_dmx_release(struct dvb_demux
*dvbdemux
)
1313 vfree(dvbdemux
->cnt_storage
);
1314 vfree(dvbdemux
->filter
);
1315 vfree(dvbdemux
->feed
);
1318 EXPORT_SYMBOL(dvb_dmx_release
);