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 #define pr_fmt(fmt) "dvb_demux: " fmt
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/module.h>
31 #include <linux/poll.h>
32 #include <linux/string.h>
33 #include <linux/crc32.h>
34 #include <linux/uaccess.h>
35 #include <asm/div64.h>
37 #include "dvb_demux.h"
39 static int dvb_demux_tscheck
;
40 module_param(dvb_demux_tscheck
, int, 0644);
41 MODULE_PARM_DESC(dvb_demux_tscheck
,
42 "enable transport stream continuity and TEI check");
44 static int dvb_demux_speedcheck
;
45 module_param(dvb_demux_speedcheck
, int, 0644);
46 MODULE_PARM_DESC(dvb_demux_speedcheck
,
47 "enable transport stream speed check");
49 static int dvb_demux_feed_err_pkts
= 1;
50 module_param(dvb_demux_feed_err_pkts
, int, 0644);
51 MODULE_PARM_DESC(dvb_demux_feed_err_pkts
,
52 "when set to 0, drop packets with the TEI bit set (1 by default)");
54 #define dprintk(fmt, arg...) \
55 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
57 #define dprintk_tscheck(x...) do { \
58 if (dvb_demux_tscheck && printk_ratelimit()) \
62 /******************************************************************************
63 * static inlined helper functions
64 ******************************************************************************/
66 static inline u16
section_length(const u8
*buf
)
68 return 3 + ((buf
[1] & 0x0f) << 8) + buf
[2];
71 static inline u16
ts_pid(const u8
*buf
)
73 return ((buf
[1] & 0x1f) << 8) + buf
[2];
76 static inline u8
payload(const u8
*tsp
)
78 if (!(tsp
[3] & 0x10)) // no payload?
81 if (tsp
[3] & 0x20) { // adaptation field?
82 if (tsp
[4] > 183) // corrupted data?
85 return 184 - 1 - tsp
[4];
91 static u32
dvb_dmx_crc32(struct dvb_demux_feed
*f
, const u8
*src
, size_t len
)
93 return (f
->feed
.sec
.crc_val
= crc32_be(f
->feed
.sec
.crc_val
, src
, len
));
96 static void dvb_dmx_memcopy(struct dvb_demux_feed
*f
, u8
*d
, const u8
*s
,
102 /******************************************************************************
103 * Software filter functions
104 ******************************************************************************/
106 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed
*feed
,
109 int count
= payload(buf
);
111 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
121 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
123 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
126 dprintk("missed packet!\n");
129 if (buf
[1] & 0x40) // PUSI ?
130 feed
->peslen
= 0xfffa;
132 feed
->peslen
+= count
;
134 return feed
->cb
.ts(&buf
[p
], count
, NULL
, 0, &feed
->feed
.ts
);
137 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed
*feed
,
138 struct dvb_demux_filter
*f
)
143 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
144 u8
xor = f
->filter
.filter_value
[i
] ^ feed
->feed
.sec
.secbuf
[i
];
146 if (f
->maskandmode
[i
] & xor)
149 neq
|= f
->maskandnotmode
[i
] & xor;
152 if (f
->doneq
&& !neq
)
155 return feed
->cb
.sec(feed
->feed
.sec
.secbuf
, feed
->feed
.sec
.seclen
,
156 NULL
, 0, &f
->filter
);
159 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed
*feed
)
161 struct dvb_demux
*demux
= feed
->demux
;
162 struct dvb_demux_filter
*f
= feed
->filter
;
163 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
164 int section_syntax_indicator
;
166 if (!sec
->is_filtering
)
172 if (sec
->check_crc
) {
173 section_syntax_indicator
= ((sec
->secbuf
[1] & 0x80) != 0);
174 if (section_syntax_indicator
&&
175 demux
->check_crc32(feed
, sec
->secbuf
, sec
->seclen
))
180 if (dvb_dmx_swfilter_sectionfilter(feed
, f
) < 0)
182 } while ((f
= f
->next
) && sec
->is_filtering
);
189 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed
*feed
)
191 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
193 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
194 if (sec
->secbufp
< sec
->tsfeedp
) {
195 int i
, n
= sec
->tsfeedp
- sec
->secbufp
;
198 * Section padding is done with 0xff bytes entirely.
199 * Due to speed reasons, we won't check all of them
200 * but just first and last.
202 if (sec
->secbuf
[0] != 0xff || sec
->secbuf
[n
- 1] != 0xff) {
203 dprintk("dvb_demux.c section ts padding loss: %d/%d\n",
205 dprintk("dvb_demux.c pad data:");
206 for (i
= 0; i
< n
; i
++)
207 pr_cont(" %02x", sec
->secbuf
[i
]);
213 sec
->tsfeedp
= sec
->secbufp
= sec
->seclen
= 0;
214 sec
->secbuf
= sec
->secbuf_base
;
218 * Losless Section Demux 1.4.1 by Emard
220 * - middle of section A (no PUSI)
221 * - end of section A and start of section B
222 * (with PUSI pointing to the start of the second section)
224 * In this case, without feed->pusi_seen you'll receive a garbage section
225 * consisting of the end of section A. Basically because tsfeedp
226 * is incemented and the use=0 condition is not raised
227 * when the second packet arrives.
230 * when demux is started, let feed->pusi_seen = 0 to
231 * prevent initial feeding of garbage from the end of
232 * previous section. When you for the first time see PUSI=1
233 * then set feed->pusi_seen = 1
235 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed
*feed
,
236 const u8
*buf
, u8 len
)
238 struct dvb_demux
*demux
= feed
->demux
;
239 struct dmx_section_feed
*sec
= &feed
->feed
.sec
;
240 u16 limit
, seclen
, n
;
242 if (sec
->tsfeedp
>= DMX_MAX_SECFEED_SIZE
)
245 if (sec
->tsfeedp
+ len
> DMX_MAX_SECFEED_SIZE
) {
246 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
247 dprintk("dvb_demux.c section buffer full loss: %d/%d\n",
248 sec
->tsfeedp
+ len
- DMX_MAX_SECFEED_SIZE
,
249 DMX_MAX_SECFEED_SIZE
);
251 len
= DMX_MAX_SECFEED_SIZE
- sec
->tsfeedp
;
257 demux
->memcopy(feed
, sec
->secbuf_base
+ sec
->tsfeedp
, buf
, len
);
261 * Dump all the sections we can find in the data (Emard)
263 limit
= sec
->tsfeedp
;
264 if (limit
> DMX_MAX_SECFEED_SIZE
)
265 return -1; /* internal error should never happen */
267 /* to be sure always set secbuf */
268 sec
->secbuf
= sec
->secbuf_base
+ sec
->secbufp
;
270 for (n
= 0; sec
->secbufp
+ 2 < limit
; n
++) {
271 seclen
= section_length(sec
->secbuf
);
272 if (seclen
<= 0 || seclen
> DMX_MAX_SECTION_SIZE
273 || seclen
+ sec
->secbufp
> limit
)
275 sec
->seclen
= seclen
;
277 /* dump [secbuf .. secbuf+seclen) */
279 dvb_dmx_swfilter_section_feed(feed
);
280 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
282 dprintk("dvb_demux.c pusi not seen, discarding section data\n");
284 sec
->secbufp
+= seclen
; /* secbufp and secbuf moving together is */
285 sec
->secbuf
+= seclen
; /* redundant but saves pointer arithmetic */
291 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed
*feed
,
298 count
= payload(buf
);
300 if (count
== 0) /* count == 0 if no payload or out of range */
303 p
= 188 - count
; /* payload start */
306 ccok
= ((feed
->cc
+ 1) & 0x0f) == cc
;
310 /* adaption field present, check for discontinuity_indicator */
311 if ((buf
[4] > 0) && (buf
[5] & 0x80))
316 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
317 dprintk("dvb_demux.c discontinuity detected %d bytes lost\n",
320 * those bytes under sume circumstances will again be reported
321 * in the following dvb_dmx_swfilter_section_new
325 * Discontinuity detected. Reset pusi_seen = 0 to
326 * stop feeding of suspicious data until next PUSI=1 arrives
329 dvb_dmx_swfilter_section_new(feed
);
333 /* PUSI=1 (is set), section boundary is here */
334 if (count
> 1 && buf
[p
] < count
) {
335 const u8
*before
= &buf
[p
+ 1];
336 u8 before_len
= buf
[p
];
337 const u8
*after
= &before
[before_len
];
338 u8 after_len
= count
- 1 - before_len
;
340 dvb_dmx_swfilter_section_copy_dump(feed
, before
,
342 /* before start of new section, set pusi_seen = 1 */
344 dvb_dmx_swfilter_section_new(feed
);
345 dvb_dmx_swfilter_section_copy_dump(feed
, after
,
348 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
350 dprintk("dvb_demux.c PUSI=1 but %d bytes lost\n",
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 if (feed
->ts_type
& TS_DECODER
)
375 if (feed
->demux
->write_to_decoder
)
376 feed
->demux
->write_to_decoder(feed
, buf
, 188);
380 if (!feed
->feed
.sec
.is_filtering
)
382 if (dvb_dmx_swfilter_section_packet(feed
, buf
) < 0)
383 feed
->feed
.sec
.seclen
= feed
->feed
.sec
.secbufp
= 0;
391 #define DVR_FEED(f) \
392 (((f)->type == DMX_TYPE_TS) && \
393 ((f)->feed.ts.is_filtering) && \
394 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
396 static void dvb_dmx_swfilter_packet(struct dvb_demux
*demux
, const u8
*buf
)
398 struct dvb_demux_feed
*feed
;
399 u16 pid
= ts_pid(buf
);
402 if (dvb_demux_speedcheck
) {
404 u64 speed_bytes
, speed_timedelta
;
406 demux
->speed_pkts_cnt
++;
408 /* show speed every SPEED_PKTS_INTERVAL packets */
409 if (!(demux
->speed_pkts_cnt
% SPEED_PKTS_INTERVAL
)) {
410 cur_time
= ktime_get();
412 if (ktime_to_ns(demux
->speed_last_time
) != 0) {
413 speed_bytes
= (u64
)demux
->speed_pkts_cnt
415 /* convert to 1024 basis */
416 speed_bytes
= 1000 * div64_u64(speed_bytes
,
418 speed_timedelta
= ktime_ms_delta(cur_time
,
419 demux
->speed_last_time
);
420 dprintk("TS speed %llu Kbits/sec \n",
421 div64_u64(speed_bytes
,
425 demux
->speed_last_time
= cur_time
;
426 demux
->speed_pkts_cnt
= 0;
431 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
433 /* data in this packet can't be trusted - drop it unless
434 * module option dvb_demux_feed_err_pkts is set */
435 if (!dvb_demux_feed_err_pkts
)
437 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
438 if (demux
->cnt_storage
&& dvb_demux_tscheck
) {
439 /* check pkt counter */
442 demux
->cnt_storage
[pid
] =
443 (demux
->cnt_storage
[pid
] + 1) & 0xf;
445 if ((buf
[3] & 0xf) != demux
->cnt_storage
[pid
]) {
446 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
447 pid
, demux
->cnt_storage
[pid
],
449 demux
->cnt_storage
[pid
] = buf
[3] & 0xf;
455 list_for_each_entry(feed
, &demux
->feed_list
, list_head
) {
456 if ((feed
->pid
!= pid
) && (feed
->pid
!= 0x2000))
459 /* copy each packet only once to the dvr device, even
460 * if a PID is in multiple filters (e.g. video + PCR) */
461 if ((DVR_FEED(feed
)) && (dvr_done
++))
464 if (feed
->pid
== pid
)
465 dvb_dmx_swfilter_packet_type(feed
, buf
);
466 else if (feed
->pid
== 0x2000)
467 feed
->cb
.ts(buf
, 188, NULL
, 0, &feed
->feed
.ts
);
471 void dvb_dmx_swfilter_packets(struct dvb_demux
*demux
, const u8
*buf
,
476 spin_lock_irqsave(&demux
->lock
, flags
);
480 dvb_dmx_swfilter_packet(demux
, buf
);
484 spin_unlock_irqrestore(&demux
->lock
, flags
);
487 EXPORT_SYMBOL(dvb_dmx_swfilter_packets
);
489 static inline int find_next_packet(const u8
*buf
, int pos
, size_t count
,
492 int start
= pos
, lost
;
494 while (pos
< count
) {
495 if (buf
[pos
] == 0x47 ||
496 (pktsize
== 204 && buf
[pos
] == 0xB8))
503 /* This garbage is part of a valid packet? */
504 int backtrack
= pos
- pktsize
;
505 if (backtrack
>= 0 && (buf
[backtrack
] == 0x47 ||
506 (pktsize
== 204 && buf
[backtrack
] == 0xB8)))
513 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
514 static inline void _dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
,
515 size_t count
, const int pktsize
)
521 spin_lock_irqsave(&demux
->lock
, flags
);
523 if (demux
->tsbufp
) { /* tsbuf[0] is now 0x47. */
527 memcpy(&demux
->tsbuf
[i
], buf
, count
);
528 demux
->tsbufp
+= count
;
531 memcpy(&demux
->tsbuf
[i
], buf
, j
);
532 if (demux
->tsbuf
[0] == 0x47) /* double check */
533 dvb_dmx_swfilter_packet(demux
, demux
->tsbuf
);
539 p
= find_next_packet(buf
, p
, count
, pktsize
);
542 if (count
- p
< pktsize
)
547 if (pktsize
== 204 && (*q
== 0xB8)) {
548 memcpy(demux
->tsbuf
, q
, 188);
549 demux
->tsbuf
[0] = 0x47;
552 dvb_dmx_swfilter_packet(demux
, q
);
558 memcpy(demux
->tsbuf
, &buf
[p
], i
);
560 if (pktsize
== 204 && demux
->tsbuf
[0] == 0xB8)
561 demux
->tsbuf
[0] = 0x47;
565 spin_unlock_irqrestore(&demux
->lock
, flags
);
568 void dvb_dmx_swfilter(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
570 _dvb_dmx_swfilter(demux
, buf
, count
, 188);
572 EXPORT_SYMBOL(dvb_dmx_swfilter
);
574 void dvb_dmx_swfilter_204(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
576 _dvb_dmx_swfilter(demux
, buf
, count
, 204);
578 EXPORT_SYMBOL(dvb_dmx_swfilter_204
);
580 void dvb_dmx_swfilter_raw(struct dvb_demux
*demux
, const u8
*buf
, size_t count
)
584 spin_lock_irqsave(&demux
->lock
, flags
);
586 demux
->feed
->cb
.ts(buf
, count
, NULL
, 0, &demux
->feed
->feed
.ts
);
588 spin_unlock_irqrestore(&demux
->lock
, flags
);
590 EXPORT_SYMBOL(dvb_dmx_swfilter_raw
);
592 static struct dvb_demux_filter
*dvb_dmx_filter_alloc(struct dvb_demux
*demux
)
596 for (i
= 0; i
< demux
->filternum
; i
++)
597 if (demux
->filter
[i
].state
== DMX_STATE_FREE
)
600 if (i
== demux
->filternum
)
603 demux
->filter
[i
].state
= DMX_STATE_ALLOCATED
;
605 return &demux
->filter
[i
];
608 static struct dvb_demux_feed
*dvb_dmx_feed_alloc(struct dvb_demux
*demux
)
612 for (i
= 0; i
< demux
->feednum
; i
++)
613 if (demux
->feed
[i
].state
== DMX_STATE_FREE
)
616 if (i
== demux
->feednum
)
619 demux
->feed
[i
].state
= DMX_STATE_ALLOCATED
;
621 return &demux
->feed
[i
];
624 static int dvb_demux_feed_find(struct dvb_demux_feed
*feed
)
626 struct dvb_demux_feed
*entry
;
628 list_for_each_entry(entry
, &feed
->demux
->feed_list
, list_head
)
635 static void dvb_demux_feed_add(struct dvb_demux_feed
*feed
)
637 spin_lock_irq(&feed
->demux
->lock
);
638 if (dvb_demux_feed_find(feed
)) {
639 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
640 __func__
, feed
->type
, feed
->state
, feed
->pid
);
644 list_add(&feed
->list_head
, &feed
->demux
->feed_list
);
646 spin_unlock_irq(&feed
->demux
->lock
);
649 static void dvb_demux_feed_del(struct dvb_demux_feed
*feed
)
651 spin_lock_irq(&feed
->demux
->lock
);
652 if (!(dvb_demux_feed_find(feed
))) {
653 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
654 __func__
, feed
->type
, feed
->state
, feed
->pid
);
658 list_del(&feed
->list_head
);
660 spin_unlock_irq(&feed
->demux
->lock
);
663 static int dmx_ts_feed_set(struct dmx_ts_feed
*ts_feed
, u16 pid
, int ts_type
,
664 enum dmx_ts_pes pes_type
, ktime_t timeout
)
666 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
667 struct dvb_demux
*demux
= feed
->demux
;
669 if (pid
> DMX_MAX_PID
)
672 if (mutex_lock_interruptible(&demux
->mutex
))
675 if (ts_type
& TS_DECODER
) {
676 if (pes_type
>= DMX_PES_OTHER
) {
677 mutex_unlock(&demux
->mutex
);
681 if (demux
->pesfilter
[pes_type
] &&
682 demux
->pesfilter
[pes_type
] != feed
) {
683 mutex_unlock(&demux
->mutex
);
687 demux
->pesfilter
[pes_type
] = feed
;
688 demux
->pids
[pes_type
] = pid
;
691 dvb_demux_feed_add(feed
);
694 feed
->timeout
= timeout
;
695 feed
->ts_type
= ts_type
;
696 feed
->pes_type
= pes_type
;
698 feed
->state
= DMX_STATE_READY
;
699 mutex_unlock(&demux
->mutex
);
704 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed
*ts_feed
)
706 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
707 struct dvb_demux
*demux
= feed
->demux
;
710 if (mutex_lock_interruptible(&demux
->mutex
))
713 if (feed
->state
!= DMX_STATE_READY
|| feed
->type
!= DMX_TYPE_TS
) {
714 mutex_unlock(&demux
->mutex
);
718 if (!demux
->start_feed
) {
719 mutex_unlock(&demux
->mutex
);
723 if ((ret
= demux
->start_feed(feed
)) < 0) {
724 mutex_unlock(&demux
->mutex
);
728 spin_lock_irq(&demux
->lock
);
729 ts_feed
->is_filtering
= 1;
730 feed
->state
= DMX_STATE_GO
;
731 spin_unlock_irq(&demux
->lock
);
732 mutex_unlock(&demux
->mutex
);
737 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed
*ts_feed
)
739 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
740 struct dvb_demux
*demux
= feed
->demux
;
743 mutex_lock(&demux
->mutex
);
745 if (feed
->state
< DMX_STATE_GO
) {
746 mutex_unlock(&demux
->mutex
);
750 if (!demux
->stop_feed
) {
751 mutex_unlock(&demux
->mutex
);
755 ret
= demux
->stop_feed(feed
);
757 spin_lock_irq(&demux
->lock
);
758 ts_feed
->is_filtering
= 0;
759 feed
->state
= DMX_STATE_ALLOCATED
;
760 spin_unlock_irq(&demux
->lock
);
761 mutex_unlock(&demux
->mutex
);
766 static int dvbdmx_allocate_ts_feed(struct dmx_demux
*dmx
,
767 struct dmx_ts_feed
**ts_feed
,
770 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
771 struct dvb_demux_feed
*feed
;
773 if (mutex_lock_interruptible(&demux
->mutex
))
776 if (!(feed
= dvb_dmx_feed_alloc(demux
))) {
777 mutex_unlock(&demux
->mutex
);
781 feed
->type
= DMX_TYPE_TS
;
782 feed
->cb
.ts
= callback
;
785 feed
->peslen
= 0xfffa;
787 (*ts_feed
) = &feed
->feed
.ts
;
788 (*ts_feed
)->parent
= dmx
;
789 (*ts_feed
)->priv
= NULL
;
790 (*ts_feed
)->is_filtering
= 0;
791 (*ts_feed
)->start_filtering
= dmx_ts_feed_start_filtering
;
792 (*ts_feed
)->stop_filtering
= dmx_ts_feed_stop_filtering
;
793 (*ts_feed
)->set
= dmx_ts_feed_set
;
795 if (!(feed
->filter
= dvb_dmx_filter_alloc(demux
))) {
796 feed
->state
= DMX_STATE_FREE
;
797 mutex_unlock(&demux
->mutex
);
801 feed
->filter
->type
= DMX_TYPE_TS
;
802 feed
->filter
->feed
= feed
;
803 feed
->filter
->state
= DMX_STATE_READY
;
805 mutex_unlock(&demux
->mutex
);
810 static int dvbdmx_release_ts_feed(struct dmx_demux
*dmx
,
811 struct dmx_ts_feed
*ts_feed
)
813 struct dvb_demux
*demux
= (struct dvb_demux
*)dmx
;
814 struct dvb_demux_feed
*feed
= (struct dvb_demux_feed
*)ts_feed
;
816 mutex_lock(&demux
->mutex
);
818 if (feed
->state
== DMX_STATE_FREE
) {
819 mutex_unlock(&demux
->mutex
);
823 feed
->state
= DMX_STATE_FREE
;
824 feed
->filter
->state
= DMX_STATE_FREE
;
826 dvb_demux_feed_del(feed
);
830 if (feed
->ts_type
& TS_DECODER
&& feed
->pes_type
< DMX_PES_OTHER
)
831 demux
->pesfilter
[feed
->pes_type
] = NULL
;
833 mutex_unlock(&demux
->mutex
);
837 /******************************************************************************
838 * dmx_section_feed API calls
839 ******************************************************************************/
841 static int dmx_section_feed_allocate_filter(struct dmx_section_feed
*feed
,
842 struct dmx_section_filter
**filter
)
844 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
845 struct dvb_demux
*dvbdemux
= dvbdmxfeed
->demux
;
846 struct dvb_demux_filter
*dvbdmxfilter
;
848 if (mutex_lock_interruptible(&dvbdemux
->mutex
))
851 dvbdmxfilter
= dvb_dmx_filter_alloc(dvbdemux
);
853 mutex_unlock(&dvbdemux
->mutex
);
857 spin_lock_irq(&dvbdemux
->lock
);
858 *filter
= &dvbdmxfilter
->filter
;
859 (*filter
)->parent
= feed
;
860 (*filter
)->priv
= NULL
;
861 dvbdmxfilter
->feed
= dvbdmxfeed
;
862 dvbdmxfilter
->type
= DMX_TYPE_SEC
;
863 dvbdmxfilter
->state
= DMX_STATE_READY
;
864 dvbdmxfilter
->next
= dvbdmxfeed
->filter
;
865 dvbdmxfeed
->filter
= dvbdmxfilter
;
866 spin_unlock_irq(&dvbdemux
->lock
);
868 mutex_unlock(&dvbdemux
->mutex
);
872 static int dmx_section_feed_set(struct dmx_section_feed
*feed
,
873 u16 pid
, int check_crc
)
875 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
876 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
881 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
884 dvb_demux_feed_add(dvbdmxfeed
);
886 dvbdmxfeed
->pid
= pid
;
887 dvbdmxfeed
->feed
.sec
.check_crc
= check_crc
;
889 dvbdmxfeed
->state
= DMX_STATE_READY
;
890 mutex_unlock(&dvbdmx
->mutex
);
894 static void prepare_secfilters(struct dvb_demux_feed
*dvbdmxfeed
)
897 struct dvb_demux_filter
*f
;
898 struct dmx_section_filter
*sf
;
899 u8 mask
, mode
, doneq
;
901 if (!(f
= dvbdmxfeed
->filter
))
906 for (i
= 0; i
< DVB_DEMUX_MASK_MAX
; i
++) {
907 mode
= sf
->filter_mode
[i
];
908 mask
= sf
->filter_mask
[i
];
909 f
->maskandmode
[i
] = mask
& mode
;
910 doneq
|= f
->maskandnotmode
[i
] = mask
& ~mode
;
912 f
->doneq
= doneq
? 1 : 0;
913 } while ((f
= f
->next
));
916 static int dmx_section_feed_start_filtering(struct dmx_section_feed
*feed
)
918 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
919 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
922 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
925 if (feed
->is_filtering
) {
926 mutex_unlock(&dvbdmx
->mutex
);
930 if (!dvbdmxfeed
->filter
) {
931 mutex_unlock(&dvbdmx
->mutex
);
935 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
936 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
937 dvbdmxfeed
->feed
.sec
.secbufp
= 0;
938 dvbdmxfeed
->feed
.sec
.seclen
= 0;
940 if (!dvbdmx
->start_feed
) {
941 mutex_unlock(&dvbdmx
->mutex
);
945 prepare_secfilters(dvbdmxfeed
);
947 if ((ret
= dvbdmx
->start_feed(dvbdmxfeed
)) < 0) {
948 mutex_unlock(&dvbdmx
->mutex
);
952 spin_lock_irq(&dvbdmx
->lock
);
953 feed
->is_filtering
= 1;
954 dvbdmxfeed
->state
= DMX_STATE_GO
;
955 spin_unlock_irq(&dvbdmx
->lock
);
957 mutex_unlock(&dvbdmx
->mutex
);
961 static int dmx_section_feed_stop_filtering(struct dmx_section_feed
*feed
)
963 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
964 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
967 mutex_lock(&dvbdmx
->mutex
);
969 if (!dvbdmx
->stop_feed
) {
970 mutex_unlock(&dvbdmx
->mutex
);
974 ret
= dvbdmx
->stop_feed(dvbdmxfeed
);
976 spin_lock_irq(&dvbdmx
->lock
);
977 dvbdmxfeed
->state
= DMX_STATE_READY
;
978 feed
->is_filtering
= 0;
979 spin_unlock_irq(&dvbdmx
->lock
);
981 mutex_unlock(&dvbdmx
->mutex
);
985 static int dmx_section_feed_release_filter(struct dmx_section_feed
*feed
,
986 struct dmx_section_filter
*filter
)
988 struct dvb_demux_filter
*dvbdmxfilter
= (struct dvb_demux_filter
*)filter
, *f
;
989 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
990 struct dvb_demux
*dvbdmx
= dvbdmxfeed
->demux
;
992 mutex_lock(&dvbdmx
->mutex
);
994 if (dvbdmxfilter
->feed
!= dvbdmxfeed
) {
995 mutex_unlock(&dvbdmx
->mutex
);
999 if (feed
->is_filtering
) {
1000 /* release dvbdmx->mutex as far as it is
1001 acquired by stop_filtering() itself */
1002 mutex_unlock(&dvbdmx
->mutex
);
1003 feed
->stop_filtering(feed
);
1004 mutex_lock(&dvbdmx
->mutex
);
1007 spin_lock_irq(&dvbdmx
->lock
);
1008 f
= dvbdmxfeed
->filter
;
1010 if (f
== dvbdmxfilter
) {
1011 dvbdmxfeed
->filter
= dvbdmxfilter
->next
;
1013 while (f
->next
!= dvbdmxfilter
)
1015 f
->next
= f
->next
->next
;
1018 dvbdmxfilter
->state
= DMX_STATE_FREE
;
1019 spin_unlock_irq(&dvbdmx
->lock
);
1020 mutex_unlock(&dvbdmx
->mutex
);
1024 static int dvbdmx_allocate_section_feed(struct dmx_demux
*demux
,
1025 struct dmx_section_feed
**feed
,
1026 dmx_section_cb callback
)
1028 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1029 struct dvb_demux_feed
*dvbdmxfeed
;
1031 if (mutex_lock_interruptible(&dvbdmx
->mutex
))
1032 return -ERESTARTSYS
;
1034 if (!(dvbdmxfeed
= dvb_dmx_feed_alloc(dvbdmx
))) {
1035 mutex_unlock(&dvbdmx
->mutex
);
1039 dvbdmxfeed
->type
= DMX_TYPE_SEC
;
1040 dvbdmxfeed
->cb
.sec
= callback
;
1041 dvbdmxfeed
->demux
= dvbdmx
;
1042 dvbdmxfeed
->pid
= 0xffff;
1043 dvbdmxfeed
->feed
.sec
.secbuf
= dvbdmxfeed
->feed
.sec
.secbuf_base
;
1044 dvbdmxfeed
->feed
.sec
.secbufp
= dvbdmxfeed
->feed
.sec
.seclen
= 0;
1045 dvbdmxfeed
->feed
.sec
.tsfeedp
= 0;
1046 dvbdmxfeed
->filter
= NULL
;
1048 (*feed
) = &dvbdmxfeed
->feed
.sec
;
1049 (*feed
)->is_filtering
= 0;
1050 (*feed
)->parent
= demux
;
1051 (*feed
)->priv
= NULL
;
1053 (*feed
)->set
= dmx_section_feed_set
;
1054 (*feed
)->allocate_filter
= dmx_section_feed_allocate_filter
;
1055 (*feed
)->start_filtering
= dmx_section_feed_start_filtering
;
1056 (*feed
)->stop_filtering
= dmx_section_feed_stop_filtering
;
1057 (*feed
)->release_filter
= dmx_section_feed_release_filter
;
1059 mutex_unlock(&dvbdmx
->mutex
);
1063 static int dvbdmx_release_section_feed(struct dmx_demux
*demux
,
1064 struct dmx_section_feed
*feed
)
1066 struct dvb_demux_feed
*dvbdmxfeed
= (struct dvb_demux_feed
*)feed
;
1067 struct dvb_demux
*dvbdmx
= (struct dvb_demux
*)demux
;
1069 mutex_lock(&dvbdmx
->mutex
);
1071 if (dvbdmxfeed
->state
== DMX_STATE_FREE
) {
1072 mutex_unlock(&dvbdmx
->mutex
);
1075 dvbdmxfeed
->state
= DMX_STATE_FREE
;
1077 dvb_demux_feed_del(dvbdmxfeed
);
1079 dvbdmxfeed
->pid
= 0xffff;
1081 mutex_unlock(&dvbdmx
->mutex
);
1085 /******************************************************************************
1086 * dvb_demux kernel data API calls
1087 ******************************************************************************/
1089 static int dvbdmx_open(struct dmx_demux
*demux
)
1091 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1093 if (dvbdemux
->users
>= MAX_DVB_DEMUX_USERS
)
1100 static int dvbdmx_close(struct dmx_demux
*demux
)
1102 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1104 if (dvbdemux
->users
== 0)
1108 //FIXME: release any unneeded resources if users==0
1112 static int dvbdmx_write(struct dmx_demux
*demux
, const char __user
*buf
, size_t count
)
1114 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1117 if ((!demux
->frontend
) || (demux
->frontend
->source
!= DMX_MEMORY_FE
))
1120 p
= memdup_user(buf
, count
);
1123 if (mutex_lock_interruptible(&dvbdemux
->mutex
)) {
1125 return -ERESTARTSYS
;
1127 dvb_dmx_swfilter(dvbdemux
, p
, count
);
1129 mutex_unlock(&dvbdemux
->mutex
);
1131 if (signal_pending(current
))
1136 static int dvbdmx_add_frontend(struct dmx_demux
*demux
,
1137 struct dmx_frontend
*frontend
)
1139 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1140 struct list_head
*head
= &dvbdemux
->frontend_list
;
1142 list_add(&(frontend
->connectivity_list
), head
);
1147 static int dvbdmx_remove_frontend(struct dmx_demux
*demux
,
1148 struct dmx_frontend
*frontend
)
1150 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1151 struct list_head
*pos
, *n
, *head
= &dvbdemux
->frontend_list
;
1153 list_for_each_safe(pos
, n
, head
) {
1154 if (DMX_FE_ENTRY(pos
) == frontend
) {
1163 static struct list_head
*dvbdmx_get_frontends(struct dmx_demux
*demux
)
1165 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1167 if (list_empty(&dvbdemux
->frontend_list
))
1170 return &dvbdemux
->frontend_list
;
1173 static int dvbdmx_connect_frontend(struct dmx_demux
*demux
,
1174 struct dmx_frontend
*frontend
)
1176 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1178 if (demux
->frontend
)
1181 mutex_lock(&dvbdemux
->mutex
);
1183 demux
->frontend
= frontend
;
1184 mutex_unlock(&dvbdemux
->mutex
);
1188 static int dvbdmx_disconnect_frontend(struct dmx_demux
*demux
)
1190 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1192 mutex_lock(&dvbdemux
->mutex
);
1194 demux
->frontend
= NULL
;
1195 mutex_unlock(&dvbdemux
->mutex
);
1199 static int dvbdmx_get_pes_pids(struct dmx_demux
*demux
, u16
* pids
)
1201 struct dvb_demux
*dvbdemux
= (struct dvb_demux
*)demux
;
1203 memcpy(pids
, dvbdemux
->pids
, 5 * sizeof(u16
));
1207 int dvb_dmx_init(struct dvb_demux
*dvbdemux
)
1210 struct dmx_demux
*dmx
= &dvbdemux
->dmx
;
1212 dvbdemux
->cnt_storage
= NULL
;
1213 dvbdemux
->users
= 0;
1214 dvbdemux
->filter
= vmalloc(dvbdemux
->filternum
* sizeof(struct dvb_demux_filter
));
1216 if (!dvbdemux
->filter
)
1219 dvbdemux
->feed
= vmalloc(dvbdemux
->feednum
* sizeof(struct dvb_demux_feed
));
1220 if (!dvbdemux
->feed
) {
1221 vfree(dvbdemux
->filter
);
1222 dvbdemux
->filter
= NULL
;
1225 for (i
= 0; i
< dvbdemux
->filternum
; i
++) {
1226 dvbdemux
->filter
[i
].state
= DMX_STATE_FREE
;
1227 dvbdemux
->filter
[i
].index
= i
;
1229 for (i
= 0; i
< dvbdemux
->feednum
; i
++) {
1230 dvbdemux
->feed
[i
].state
= DMX_STATE_FREE
;
1231 dvbdemux
->feed
[i
].index
= i
;
1234 dvbdemux
->cnt_storage
= vmalloc(MAX_PID
+ 1);
1235 if (!dvbdemux
->cnt_storage
)
1236 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1238 INIT_LIST_HEAD(&dvbdemux
->frontend_list
);
1240 for (i
= 0; i
< DMX_PES_OTHER
; i
++) {
1241 dvbdemux
->pesfilter
[i
] = NULL
;
1242 dvbdemux
->pids
[i
] = 0xffff;
1245 INIT_LIST_HEAD(&dvbdemux
->feed_list
);
1247 dvbdemux
->playing
= 0;
1248 dvbdemux
->recording
= 0;
1249 dvbdemux
->tsbufp
= 0;
1251 if (!dvbdemux
->check_crc32
)
1252 dvbdemux
->check_crc32
= dvb_dmx_crc32
;
1254 if (!dvbdemux
->memcopy
)
1255 dvbdemux
->memcopy
= dvb_dmx_memcopy
;
1257 dmx
->frontend
= NULL
;
1258 dmx
->priv
= dvbdemux
;
1259 dmx
->open
= dvbdmx_open
;
1260 dmx
->close
= dvbdmx_close
;
1261 dmx
->write
= dvbdmx_write
;
1262 dmx
->allocate_ts_feed
= dvbdmx_allocate_ts_feed
;
1263 dmx
->release_ts_feed
= dvbdmx_release_ts_feed
;
1264 dmx
->allocate_section_feed
= dvbdmx_allocate_section_feed
;
1265 dmx
->release_section_feed
= dvbdmx_release_section_feed
;
1267 dmx
->add_frontend
= dvbdmx_add_frontend
;
1268 dmx
->remove_frontend
= dvbdmx_remove_frontend
;
1269 dmx
->get_frontends
= dvbdmx_get_frontends
;
1270 dmx
->connect_frontend
= dvbdmx_connect_frontend
;
1271 dmx
->disconnect_frontend
= dvbdmx_disconnect_frontend
;
1272 dmx
->get_pes_pids
= dvbdmx_get_pes_pids
;
1274 mutex_init(&dvbdemux
->mutex
);
1275 spin_lock_init(&dvbdemux
->lock
);
1280 EXPORT_SYMBOL(dvb_dmx_init
);
1282 void dvb_dmx_release(struct dvb_demux
*dvbdemux
)
1284 vfree(dvbdemux
->cnt_storage
);
1285 vfree(dvbdemux
->filter
);
1286 vfree(dvbdemux
->feed
);
1289 EXPORT_SYMBOL(dvb_dmx_release
);