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 "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!\n");
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
);
133 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed
*feed
,
134 struct dvb_demux_filter
*f
)
139 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
140 u8
xor = f
->filter
.filter_value
[i
] ^ feed
->feed
.sec
.secbuf
[i
];
142 if (f
->maskandmode
[i
] & xor)
145 neq
|= f
->maskandnotmode
[i
] & xor;
148 if (f
->doneq
&& !neq
)
151 return feed
->cb
.sec(feed
->feed
.sec
.secbuf
, feed
->feed
.sec
.seclen
,
152 NULL
, 0, &f
->filter
);
155 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed
*feed
)
157 struct dvb_demux
*demux
= feed
->demux
;
158 struct dvb_demux_filter
*f
= feed
->filter
;
159 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
160 int section_syntax_indicator
;
162 if (!sec
->is_filtering
)
168 if (sec
->check_crc
) {
169 section_syntax_indicator
= ((sec
->secbuf
[1] & 0x80) != 0);
170 if (section_syntax_indicator
&&
171 demux
->check_crc32(feed
, sec
->secbuf
, sec
->seclen
))
176 if (dvb_dmx_swfilter_sectionfilter(feed
, f
) < 0)
178 } while ((f
= f
->next
) && sec
->is_filtering
);
185 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed
*feed
)
187 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
189 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
190 if (sec
->secbufp
< sec
->tsfeedp
) {
191 int i
, n
= sec
->tsfeedp
- sec
->secbufp
;
194 * Section padding is done with 0xff bytes entirely.
195 * Due to speed reasons, we won't check all of them
196 * but just first and last.
198 if (sec
->secbuf
[0] != 0xff || sec
->secbuf
[n
- 1] != 0xff) {
199 dprintk("dvb_demux.c section ts padding loss: %d/%d\n",
201 dprintk("dvb_demux.c pad data:");
202 for (i
= 0; i
< n
; i
++)
203 pr_cont(" %02x", sec
->secbuf
[i
]);
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
;
236 u16 limit
, seclen
, n
;
238 if (sec
->tsfeedp
>= DMX_MAX_SECFEED_SIZE
)
241 if (sec
->tsfeedp
+ len
> DMX_MAX_SECFEED_SIZE
) {
242 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
243 dprintk("dvb_demux.c section buffer full loss: %d/%d\n",
244 sec
->tsfeedp
+ len
- DMX_MAX_SECFEED_SIZE
,
245 DMX_MAX_SECFEED_SIZE
);
247 len
= DMX_MAX_SECFEED_SIZE
- sec
->tsfeedp
;
253 demux
->memcopy(feed
, sec
->secbuf_base
+ sec
->tsfeedp
, buf
, len
);
257 * Dump all the sections we can find in the data (Emard)
259 limit
= sec
->tsfeedp
;
260 if (limit
> DMX_MAX_SECFEED_SIZE
)
261 return -1; /* internal error should never happen */
263 /* to be sure always set secbuf */
264 sec
->secbuf
= sec
->secbuf_base
+ sec
->secbufp
;
266 for (n
= 0; sec
->secbufp
+ 2 < limit
; n
++) {
267 seclen
= section_length(sec
->secbuf
);
268 if (seclen
<= 0 || seclen
> DMX_MAX_SECTION_SIZE
269 || seclen
+ sec
->secbufp
> limit
)
271 sec
->seclen
= seclen
;
273 /* dump [secbuf .. secbuf+seclen) */
275 dvb_dmx_swfilter_section_feed(feed
);
276 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
278 dprintk("dvb_demux.c 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
;
306 /* adaption field present, check for discontinuity_indicator */
307 if ((buf
[4] > 0) && (buf
[5] & 0x80))
312 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
313 dprintk("dvb_demux.c discontinuity detected %d bytes lost\n",
316 * those bytes under sume circumstances will again be reported
317 * in the following dvb_dmx_swfilter_section_new
321 * Discontinuity detected. Reset pusi_seen to
322 * stop feeding of suspicious data until next PUSI=1 arrives
324 feed
->pusi_seen
= false;
325 dvb_dmx_swfilter_section_new(feed
);
329 /* PUSI=1 (is set), section boundary is here */
330 if (count
> 1 && buf
[p
] < count
) {
331 const u8
*before
= &buf
[p
+ 1];
332 u8 before_len
= buf
[p
];
333 const u8
*after
= &before
[before_len
];
334 u8 after_len
= count
- 1 - before_len
;
336 dvb_dmx_swfilter_section_copy_dump(feed
, before
,
338 /* before start of new section, set pusi_seen */
339 feed
->pusi_seen
= true;
340 dvb_dmx_swfilter_section_new(feed
);
341 dvb_dmx_swfilter_section_copy_dump(feed
, after
,
344 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
346 dprintk("dvb_demux.c PUSI=1 but %d bytes lost\n",
350 /* PUSI=0 (is not set), no section boundary */
351 dvb_dmx_swfilter_section_copy_dump(feed
, &buf
[p
], count
);
357 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed
*feed
,
360 switch (feed
->type
) {
362 if (!feed
->feed
.ts
.is_filtering
)
364 if (feed
->ts_type
& TS_PACKET
) {
365 if (feed
->ts_type
& TS_PAYLOAD_ONLY
)
366 dvb_dmx_swfilter_payload(feed
, buf
);
368 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
);
370 /* Used only on full-featured devices */
371 if (feed
->ts_type
& TS_DECODER
)
372 if (feed
->demux
->write_to_decoder
)
373 feed
->demux
->write_to_decoder(feed
, buf
, 188);
377 if (!feed
->feed
.sec
.is_filtering
)
379 if (dvb_dmx_swfilter_section_packet(feed
, buf
) < 0)
380 feed
->feed
.sec
.seclen
= feed
->feed
.sec
.secbufp
= 0;
388 #define DVR_FEED(f) \
389 (((f)->type == DMX_TYPE_TS) && \
390 ((f)->feed.ts.is_filtering) && \
391 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
393 static void dvb_dmx_swfilter_packet(struct dvb_demux
*demux
, const u8
*buf
)
395 struct dvb_demux_feed
*feed
;
396 u16 pid
= ts_pid(buf
);
399 if (dvb_demux_speedcheck
) {
401 u64 speed_bytes
, speed_timedelta
;
403 demux
->speed_pkts_cnt
++;
405 /* show speed every SPEED_PKTS_INTERVAL packets */
406 if (!(demux
->speed_pkts_cnt
% SPEED_PKTS_INTERVAL
)) {
407 cur_time
= ktime_get();
409 if (ktime_to_ns(demux
->speed_last_time
) != 0) {
410 speed_bytes
= (u64
)demux
->speed_pkts_cnt
412 /* convert to 1024 basis */
413 speed_bytes
= 1000 * div64_u64(speed_bytes
,
415 speed_timedelta
= ktime_ms_delta(cur_time
,
416 demux
->speed_last_time
);
417 dprintk("TS speed %llu Kbits/sec \n",
418 div64_u64(speed_bytes
,
422 demux
->speed_last_time
= cur_time
;
423 demux
->speed_pkts_cnt
= 0;
428 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
430 /* data in this packet can't be trusted - drop it unless
431 * module option dvb_demux_feed_err_pkts is set */
432 if (!dvb_demux_feed_err_pkts
)
434 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
435 if (demux
->cnt_storage
&& dvb_demux_tscheck
) {
436 /* check pkt counter */
439 demux
->cnt_storage
[pid
] =
440 (demux
->cnt_storage
[pid
] + 1) & 0xf;
442 if ((buf
[3] & 0xf) != demux
->cnt_storage
[pid
]) {
443 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
444 pid
, demux
->cnt_storage
[pid
],
446 demux
->cnt_storage
[pid
] = buf
[3] & 0xf;
452 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
453 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
456 /* copy each packet only once to the dvr device, even
457 * if a PID is in multiple filters (e.g. video + PCR) */
458 if ((DVR_FEED(feed
)) && (dvr_done
++))
461 if (feed
->pid
== pid
)
462 dvb_dmx_swfilter_packet_type(feed
, buf
);
463 else if (feed
->pid
== 0x2000)
464 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
);
468 void dvb_dmx_swfilter_packets(struct dvb_demux
*demux
, const u8
*buf
,
473 spin_lock_irqsave(&demux
->lock
, flags
);
477 dvb_dmx_swfilter_packet(demux
, buf
);
481 spin_unlock_irqrestore(&demux
->lock
, flags
);
484 EXPORT_SYMBOL(dvb_dmx_swfilter_packets
);
486 static inline int find_next_packet(const u8
*buf
, int pos
, size_t count
,
489 int start
= pos
, lost
;
491 while (pos
< count
) {
492 if (buf
[pos
] == 0x47 ||
493 (pktsize
== 204 && buf
[pos
] == 0xB8))
500 /* This garbage is part of a valid packet? */
501 int backtrack
= pos
- pktsize
;
502 if (backtrack
>= 0 && (buf
[backtrack
] == 0x47 ||
503 (pktsize
== 204 && buf
[backtrack
] == 0xB8)))
510 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
511 static inline void _dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
,
512 size_t count
, const int pktsize
)
518 spin_lock_irqsave(&demux
->lock
, flags
);
520 if (demux
->tsbufp
) { /* tsbuf[0] is now 0x47. */
524 memcpy(&demux
->tsbuf
[i
], buf
, count
);
525 demux
->tsbufp
+= count
;
528 memcpy(&demux
->tsbuf
[i
], buf
, j
);
529 if (demux
->tsbuf
[0] == 0x47) /* double check */
530 dvb_dmx_swfilter_packet(demux
, demux
->tsbuf
);
536 p
= find_next_packet(buf
, p
, count
, pktsize
);
539 if (count
- p
< pktsize
)
544 if (pktsize
== 204 && (*q
== 0xB8)) {
545 memcpy(demux
->tsbuf
, q
, 188);
546 demux
->tsbuf
[0] = 0x47;
549 dvb_dmx_swfilter_packet(demux
, q
);
555 memcpy(demux
->tsbuf
, &buf
[p
], i
);
557 if (pktsize
== 204 && demux
->tsbuf
[0] == 0xB8)
558 demux
->tsbuf
[0] = 0x47;
562 spin_unlock_irqrestore(&demux
->lock
, flags
);
565 void dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
567 _dvb_dmx_swfilter(demux
, buf
, count
, 188);
569 EXPORT_SYMBOL(dvb_dmx_swfilter
);
571 void dvb_dmx_swfilter_204(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
573 _dvb_dmx_swfilter(demux
, buf
, count
, 204);
575 EXPORT_SYMBOL(dvb_dmx_swfilter_204
);
577 void dvb_dmx_swfilter_raw(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
581 spin_lock_irqsave(&demux
->lock
, flags
);
583 demux
->feed
->cb
.ts(buf
, count
, NULL
, 0, &demux
->feed
->feed
.ts
);
585 spin_unlock_irqrestore(&demux
->lock
, flags
);
587 EXPORT_SYMBOL(dvb_dmx_swfilter_raw
);
589 static struct dvb_demux_filter
*dvb_dmx_filter_alloc(struct dvb_demux
*demux
)
593 for (i
= 0; i
< demux
->filternum
; i
++)
594 if (demux
->filter
[i
].state
== DMX_STATE_FREE
)
597 if (i
== demux
->filternum
)
600 demux
->filter
[i
].state
= DMX_STATE_ALLOCATED
;
602 return &demux
->filter
[i
];
605 static struct dvb_demux_feed
*dvb_dmx_feed_alloc(struct dvb_demux
*demux
)
609 for (i
= 0; i
< demux
->feednum
; i
++)
610 if (demux
->feed
[i
].state
== DMX_STATE_FREE
)
613 if (i
== demux
->feednum
)
616 demux
->feed
[i
].state
= DMX_STATE_ALLOCATED
;
618 return &demux
->feed
[i
];
621 static int dvb_demux_feed_find(struct dvb_demux_feed
*feed
)
623 struct dvb_demux_feed
*entry
;
625 list_for_each_entry(entry
, &feed
->demux
->feed_list
, list_head
)
632 static void dvb_demux_feed_add(struct dvb_demux_feed
*feed
)
634 spin_lock_irq(&feed
->demux
->lock
);
635 if (dvb_demux_feed_find(feed
)) {
636 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
637 __func__
, feed
->type
, feed
->state
, feed
->pid
);
641 list_add(&feed
->list_head
, &feed
->demux
->feed_list
);
643 spin_unlock_irq(&feed
->demux
->lock
);
646 static void dvb_demux_feed_del(struct dvb_demux_feed
*feed
)
648 spin_lock_irq(&feed
->demux
->lock
);
649 if (!(dvb_demux_feed_find(feed
))) {
650 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
651 __func__
, feed
->type
, feed
->state
, feed
->pid
);
655 list_del(&feed
->list_head
);
657 spin_unlock_irq(&feed
->demux
->lock
);
660 static int dmx_ts_feed_set(struct dmx_ts_feed
*ts_feed
, u16 pid
, int ts_type
,
661 enum dmx_ts_pes pes_type
, ktime_t timeout
)
663 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
664 struct dvb_demux
*demux
= feed
->demux
;
666 if (pid
> DMX_MAX_PID
)
669 if (mutex_lock_interruptible(&demux
->mutex
))
672 if (ts_type
& TS_DECODER
) {
673 if (pes_type
>= DMX_PES_OTHER
) {
674 mutex_unlock(&demux
->mutex
);
678 if (demux
->pesfilter
[pes_type
] &&
679 demux
->pesfilter
[pes_type
] != feed
) {
680 mutex_unlock(&demux
->mutex
);
684 demux
->pesfilter
[pes_type
] = feed
;
685 demux
->pids
[pes_type
] = pid
;
688 dvb_demux_feed_add(feed
);
691 feed
->timeout
= timeout
;
692 feed
->ts_type
= ts_type
;
693 feed
->pes_type
= pes_type
;
695 feed
->state
= DMX_STATE_READY
;
696 mutex_unlock(&demux
->mutex
);
701 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed
*ts_feed
)
703 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
704 struct dvb_demux
*demux
= feed
->demux
;
707 if (mutex_lock_interruptible(&demux
->mutex
))
710 if (feed
->state
!= DMX_STATE_READY
|| feed
->type
!= DMX_TYPE_TS
) {
711 mutex_unlock(&demux
->mutex
);
715 if (!demux
->start_feed
) {
716 mutex_unlock(&demux
->mutex
);
720 if ((ret
= demux
->start_feed(feed
)) < 0) {
721 mutex_unlock(&demux
->mutex
);
725 spin_lock_irq(&demux
->lock
);
726 ts_feed
->is_filtering
= 1;
727 feed
->state
= DMX_STATE_GO
;
728 spin_unlock_irq(&demux
->lock
);
729 mutex_unlock(&demux
->mutex
);
734 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed
*ts_feed
)
736 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
737 struct dvb_demux
*demux
= feed
->demux
;
740 mutex_lock(&demux
->mutex
);
742 if (feed
->state
< DMX_STATE_GO
) {
743 mutex_unlock(&demux
->mutex
);
747 if (!demux
->stop_feed
) {
748 mutex_unlock(&demux
->mutex
);
752 ret
= demux
->stop_feed(feed
);
754 spin_lock_irq(&demux
->lock
);
755 ts_feed
->is_filtering
= 0;
756 feed
->state
= DMX_STATE_ALLOCATED
;
757 spin_unlock_irq(&demux
->lock
);
758 mutex_unlock(&demux
->mutex
);
763 static int dvbdmx_allocate_ts_feed(struct dmx_demux
*dmx
,
764 struct dmx_ts_feed
**ts_feed
,
767 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
768 struct dvb_demux_feed
*feed
;
770 if (mutex_lock_interruptible(&demux
->mutex
))
773 if (!(feed
= dvb_dmx_feed_alloc(demux
))) {
774 mutex_unlock(&demux
->mutex
);
778 feed
->type
= DMX_TYPE_TS
;
779 feed
->cb
.ts
= callback
;
782 feed
->peslen
= 0xfffa;
784 (*ts_feed
) = &feed
->feed
.ts
;
785 (*ts_feed
)->parent
= dmx
;
786 (*ts_feed
)->priv
= NULL
;
787 (*ts_feed
)->is_filtering
= 0;
788 (*ts_feed
)->start_filtering
= dmx_ts_feed_start_filtering
;
789 (*ts_feed
)->stop_filtering
= dmx_ts_feed_stop_filtering
;
790 (*ts_feed
)->set
= dmx_ts_feed_set
;
792 if (!(feed
->filter
= dvb_dmx_filter_alloc(demux
))) {
793 feed
->state
= DMX_STATE_FREE
;
794 mutex_unlock(&demux
->mutex
);
798 feed
->filter
->type
= DMX_TYPE_TS
;
799 feed
->filter
->feed
= feed
;
800 feed
->filter
->state
= DMX_STATE_READY
;
802 mutex_unlock(&demux
->mutex
);
807 static int dvbdmx_release_ts_feed(struct dmx_demux
*dmx
,
808 struct dmx_ts_feed
*ts_feed
)
810 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
811 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
813 mutex_lock(&demux
->mutex
);
815 if (feed
->state
== DMX_STATE_FREE
) {
816 mutex_unlock(&demux
->mutex
);
820 feed
->state
= DMX_STATE_FREE
;
821 feed
->filter
->state
= DMX_STATE_FREE
;
823 dvb_demux_feed_del(feed
);
827 if (feed
->ts_type
& TS_DECODER
&& feed
->pes_type
< DMX_PES_OTHER
)
828 demux
->pesfilter
[feed
->pes_type
] = NULL
;
830 mutex_unlock(&demux
->mutex
);
834 /******************************************************************************
835 * dmx_section_feed API calls
836 ******************************************************************************/
838 static int dmx_section_feed_allocate_filter(struct dmx_section_feed
*feed
,
839 struct dmx_section_filter
**filter
)
841 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
842 struct dvb_demux
*dvbdemux
= dvbdmxfeed
->demux
;
843 struct dvb_demux_filter
*dvbdmxfilter
;
845 if (mutex_lock_interruptible(&dvbdemux
->mutex
))
848 dvbdmxfilter
= dvb_dmx_filter_alloc(dvbdemux
);
850 mutex_unlock(&dvbdemux
->mutex
);
854 spin_lock_irq(&dvbdemux
->lock
);
855 *filter
= &dvbdmxfilter
->filter
;
856 (*filter
)->parent
= feed
;
857 (*filter
)->priv
= NULL
;
858 dvbdmxfilter
->feed
= dvbdmxfeed
;
859 dvbdmxfilter
->type
= DMX_TYPE_SEC
;
860 dvbdmxfilter
->state
= DMX_STATE_READY
;
861 dvbdmxfilter
->next
= dvbdmxfeed
->filter
;
862 dvbdmxfeed
->filter
= dvbdmxfilter
;
863 spin_unlock_irq(&dvbdemux
->lock
);
865 mutex_unlock(&dvbdemux
->mutex
);
869 static int dmx_section_feed_set(struct dmx_section_feed
*feed
,
870 u16 pid
, int check_crc
)
872 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
873 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
878 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
881 dvb_demux_feed_add(dvbdmxfeed
);
883 dvbdmxfeed
->pid
= pid
;
884 dvbdmxfeed
->feed
.sec
.check_crc
= check_crc
;
886 dvbdmxfeed
->state
= DMX_STATE_READY
;
887 mutex_unlock(&dvbdmx
->mutex
);
891 static void prepare_secfilters(struct dvb_demux_feed
*dvbdmxfeed
)
894 struct dvb_demux_filter
*f
;
895 struct dmx_section_filter
*sf
;
896 u8 mask
, mode
, doneq
;
898 if (!(f
= dvbdmxfeed
->filter
))
903 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
904 mode
= sf
->filter_mode
[i
];
905 mask
= sf
->filter_mask
[i
];
906 f
->maskandmode
[i
] = mask
& mode
;
907 doneq
|= f
->maskandnotmode
[i
] = mask
& ~mode
;
909 f
->doneq
= doneq
? true : false;
910 } while ((f
= f
->next
));
913 static int dmx_section_feed_start_filtering(struct dmx_section_feed
*feed
)
915 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
916 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
919 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
922 if (feed
->is_filtering
) {
923 mutex_unlock(&dvbdmx
->mutex
);
927 if (!dvbdmxfeed
->filter
) {
928 mutex_unlock(&dvbdmx
->mutex
);
932 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
933 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
934 dvbdmxfeed
->feed
.sec
.secbufp
= 0;
935 dvbdmxfeed
->feed
.sec
.seclen
= 0;
937 if (!dvbdmx
->start_feed
) {
938 mutex_unlock(&dvbdmx
->mutex
);
942 prepare_secfilters(dvbdmxfeed
);
944 if ((ret
= dvbdmx
->start_feed(dvbdmxfeed
)) < 0) {
945 mutex_unlock(&dvbdmx
->mutex
);
949 spin_lock_irq(&dvbdmx
->lock
);
950 feed
->is_filtering
= 1;
951 dvbdmxfeed
->state
= DMX_STATE_GO
;
952 spin_unlock_irq(&dvbdmx
->lock
);
954 mutex_unlock(&dvbdmx
->mutex
);
958 static int dmx_section_feed_stop_filtering(struct dmx_section_feed
*feed
)
960 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
961 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
964 mutex_lock(&dvbdmx
->mutex
);
966 if (!dvbdmx
->stop_feed
) {
967 mutex_unlock(&dvbdmx
->mutex
);
971 ret
= dvbdmx
->stop_feed(dvbdmxfeed
);
973 spin_lock_irq(&dvbdmx
->lock
);
974 dvbdmxfeed
->state
= DMX_STATE_READY
;
975 feed
->is_filtering
= 0;
976 spin_unlock_irq(&dvbdmx
->lock
);
978 mutex_unlock(&dvbdmx
->mutex
);
982 static int dmx_section_feed_release_filter(struct dmx_section_feed
*feed
,
983 struct dmx_section_filter
*filter
)
985 struct dvb_demux_filter
*dvbdmxfilter
= (struct dvb_demux_filter
*)filter
, *f
;
986 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
987 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
989 mutex_lock(&dvbdmx
->mutex
);
991 if (dvbdmxfilter
->feed
!= dvbdmxfeed
) {
992 mutex_unlock(&dvbdmx
->mutex
);
996 if (feed
->is_filtering
) {
997 /* release dvbdmx->mutex as far as it is
998 acquired by stop_filtering() itself */
999 mutex_unlock(&dvbdmx
->mutex
);
1000 feed
->stop_filtering(feed
);
1001 mutex_lock(&dvbdmx
->mutex
);
1004 spin_lock_irq(&dvbdmx
->lock
);
1005 f
= dvbdmxfeed
->filter
;
1007 if (f
== dvbdmxfilter
) {
1008 dvbdmxfeed
->filter
= dvbdmxfilter
->next
;
1010 while (f
->next
!= dvbdmxfilter
)
1012 f
->next
= f
->next
->next
;
1015 dvbdmxfilter
->state
= DMX_STATE_FREE
;
1016 spin_unlock_irq(&dvbdmx
->lock
);
1017 mutex_unlock(&dvbdmx
->mutex
);
1021 static int dvbdmx_allocate_section_feed(struct dmx_demux
*demux
,
1022 struct dmx_section_feed
**feed
,
1023 dmx_section_cb callback
)
1025 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1026 struct dvb_demux_feed
*dvbdmxfeed
;
1028 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
1029 return -ERESTARTSYS
;
1031 if (!(dvbdmxfeed
= dvb_dmx_feed_alloc(dvbdmx
))) {
1032 mutex_unlock(&dvbdmx
->mutex
);
1036 dvbdmxfeed
->type
= DMX_TYPE_SEC
;
1037 dvbdmxfeed
->cb
.sec
= callback
;
1038 dvbdmxfeed
->demux
= dvbdmx
;
1039 dvbdmxfeed
->pid
= 0xffff;
1040 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
1041 dvbdmxfeed
->feed
.sec
.secbufp
= dvbdmxfeed
->feed
.sec
.seclen
= 0;
1042 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
1043 dvbdmxfeed
->filter
= NULL
;
1045 (*feed
) = &dvbdmxfeed
->feed
.sec
;
1046 (*feed
)->is_filtering
= 0;
1047 (*feed
)->parent
= demux
;
1048 (*feed
)->priv
= NULL
;
1050 (*feed
)->set
= dmx_section_feed_set
;
1051 (*feed
)->allocate_filter
= dmx_section_feed_allocate_filter
;
1052 (*feed
)->start_filtering
= dmx_section_feed_start_filtering
;
1053 (*feed
)->stop_filtering
= dmx_section_feed_stop_filtering
;
1054 (*feed
)->release_filter
= dmx_section_feed_release_filter
;
1056 mutex_unlock(&dvbdmx
->mutex
);
1060 static int dvbdmx_release_section_feed(struct dmx_demux
*demux
,
1061 struct dmx_section_feed
*feed
)
1063 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
1064 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1066 mutex_lock(&dvbdmx
->mutex
);
1068 if (dvbdmxfeed
->state
== DMX_STATE_FREE
) {
1069 mutex_unlock(&dvbdmx
->mutex
);
1072 dvbdmxfeed
->state
= DMX_STATE_FREE
;
1074 dvb_demux_feed_del(dvbdmxfeed
);
1076 dvbdmxfeed
->pid
= 0xffff;
1078 mutex_unlock(&dvbdmx
->mutex
);
1082 /******************************************************************************
1083 * dvb_demux kernel data API calls
1084 ******************************************************************************/
1086 static int dvbdmx_open(struct dmx_demux
*demux
)
1088 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1090 if (dvbdemux
->users
>= MAX_DVB_DEMUX_USERS
)
1097 static int dvbdmx_close(struct dmx_demux
*demux
)
1099 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1101 if (dvbdemux
->users
== 0)
1105 //FIXME: release any unneeded resources if users==0
1109 static int dvbdmx_write(struct dmx_demux
*demux
, const char __user
*buf
, size_t count
)
1111 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1114 if ((!demux
->frontend
) || (demux
->frontend
->source
!= DMX_MEMORY_FE
))
1117 p
= memdup_user(buf
, count
);
1120 if (mutex_lock_interruptible(&dvbdemux
->mutex
)) {
1122 return -ERESTARTSYS
;
1124 dvb_dmx_swfilter(dvbdemux
, p
, count
);
1126 mutex_unlock(&dvbdemux
->mutex
);
1128 if (signal_pending(current
))
1133 static int dvbdmx_add_frontend(struct dmx_demux
*demux
,
1134 struct dmx_frontend
*frontend
)
1136 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1137 struct list_head
*head
= &dvbdemux
->frontend_list
;
1139 list_add(&(frontend
->connectivity_list
), head
);
1144 static int dvbdmx_remove_frontend(struct dmx_demux
*demux
,
1145 struct dmx_frontend
*frontend
)
1147 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1148 struct list_head
*pos
, *n
, *head
= &dvbdemux
->frontend_list
;
1150 list_for_each_safe(pos
, n
, head
) {
1151 if (DMX_FE_ENTRY(pos
) == frontend
) {
1160 static struct list_head
*dvbdmx_get_frontends(struct dmx_demux
*demux
)
1162 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1164 if (list_empty(&dvbdemux
->frontend_list
))
1167 return &dvbdemux
->frontend_list
;
1170 static int dvbdmx_connect_frontend(struct dmx_demux
*demux
,
1171 struct dmx_frontend
*frontend
)
1173 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1175 if (demux
->frontend
)
1178 mutex_lock(&dvbdemux
->mutex
);
1180 demux
->frontend
= frontend
;
1181 mutex_unlock(&dvbdemux
->mutex
);
1185 static int dvbdmx_disconnect_frontend(struct dmx_demux
*demux
)
1187 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1189 mutex_lock(&dvbdemux
->mutex
);
1191 demux
->frontend
= NULL
;
1192 mutex_unlock(&dvbdemux
->mutex
);
1196 static int dvbdmx_get_pes_pids(struct dmx_demux
*demux
, u16
* pids
)
1198 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1200 memcpy(pids
, dvbdemux
->pids
, 5 * sizeof(u16
));
1204 int dvb_dmx_init(struct dvb_demux
*dvbdemux
)
1207 struct dmx_demux
*dmx
= &dvbdemux
->dmx
;
1209 dvbdemux
->cnt_storage
= NULL
;
1210 dvbdemux
->users
= 0;
1211 dvbdemux
->filter
= vmalloc(dvbdemux
->filternum
* sizeof(struct dvb_demux_filter
));
1213 if (!dvbdemux
->filter
)
1216 dvbdemux
->feed
= vmalloc(dvbdemux
->feednum
* sizeof(struct dvb_demux_feed
));
1217 if (!dvbdemux
->feed
) {
1218 vfree(dvbdemux
->filter
);
1219 dvbdemux
->filter
= NULL
;
1222 for (i
= 0; i
< dvbdemux
->filternum
; i
++) {
1223 dvbdemux
->filter
[i
].state
= DMX_STATE_FREE
;
1224 dvbdemux
->filter
[i
].index
= i
;
1226 for (i
= 0; i
< dvbdemux
->feednum
; i
++) {
1227 dvbdemux
->feed
[i
].state
= DMX_STATE_FREE
;
1228 dvbdemux
->feed
[i
].index
= i
;
1231 dvbdemux
->cnt_storage
= vmalloc(MAX_PID
+ 1);
1232 if (!dvbdemux
->cnt_storage
)
1233 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1235 INIT_LIST_HEAD(&dvbdemux
->frontend_list
);
1237 for (i
= 0; i
< DMX_PES_OTHER
; i
++) {
1238 dvbdemux
->pesfilter
[i
] = NULL
;
1239 dvbdemux
->pids
[i
] = 0xffff;
1242 INIT_LIST_HEAD(&dvbdemux
->feed_list
);
1244 dvbdemux
->playing
= 0;
1245 dvbdemux
->recording
= 0;
1246 dvbdemux
->tsbufp
= 0;
1248 if (!dvbdemux
->check_crc32
)
1249 dvbdemux
->check_crc32
= dvb_dmx_crc32
;
1251 if (!dvbdemux
->memcopy
)
1252 dvbdemux
->memcopy
= dvb_dmx_memcopy
;
1254 dmx
->frontend
= NULL
;
1255 dmx
->priv
= dvbdemux
;
1256 dmx
->open
= dvbdmx_open
;
1257 dmx
->close
= dvbdmx_close
;
1258 dmx
->write
= dvbdmx_write
;
1259 dmx
->allocate_ts_feed
= dvbdmx_allocate_ts_feed
;
1260 dmx
->release_ts_feed
= dvbdmx_release_ts_feed
;
1261 dmx
->allocate_section_feed
= dvbdmx_allocate_section_feed
;
1262 dmx
->release_section_feed
= dvbdmx_release_section_feed
;
1264 dmx
->add_frontend
= dvbdmx_add_frontend
;
1265 dmx
->remove_frontend
= dvbdmx_remove_frontend
;
1266 dmx
->get_frontends
= dvbdmx_get_frontends
;
1267 dmx
->connect_frontend
= dvbdmx_connect_frontend
;
1268 dmx
->disconnect_frontend
= dvbdmx_disconnect_frontend
;
1269 dmx
->get_pes_pids
= dvbdmx_get_pes_pids
;
1271 mutex_init(&dvbdemux
->mutex
);
1272 spin_lock_init(&dvbdemux
->lock
);
1277 EXPORT_SYMBOL(dvb_dmx_init
);
1279 void dvb_dmx_release(struct dvb_demux
*dvbdemux
)
1281 vfree(dvbdemux
->cnt_storage
);
1282 vfree(dvbdemux
->filter
);
1283 vfree(dvbdemux
->feed
);
1286 EXPORT_SYMBOL(dvb_dmx_release
);