2 * dvb_demux.c - DVB kernel demux API
4 * Copyright (C) 2000-2001 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 * for convergence integrated media GmbH
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/string.h>
30 #include <linux/crc32.h>
31 #include <asm/uaccess.h>
33 #include "dvb_demux.h"
37 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
39 // #define DVB_DEMUX_SECTION_LOSS_LOG
42 static LIST_HEAD(dmx_muxs);
45 static int dmx_register_demux(struct dmx_demux *demux)
48 list_add(&demux->reg_list, &dmx_muxs);
52 static int dmx_unregister_demux(struct dmx_demux* demux)
54 struct list_head *pos, *n, *head=&dmx_muxs;
56 list_for_each_safe (pos, n, head) {
57 if (DMX_DIR_ENTRY(pos) == demux) {
69 /******************************************************************************
70 * static inlined helper functions
71 ******************************************************************************/
74 static inline u16 section_length(const u8 *buf)
76 return 3+((buf[1]&0x0f)<<8)+buf[2];
80 static inline u16 ts_pid(const u8 *buf)
82 return ((buf[1]&0x1f)<<8)+buf[2];
86 static inline u8 payload(const u8 *tsp)
88 if (!(tsp[3] & 0x10)) // no payload?
90 if (tsp[3] & 0x20) { // adaptation field?
91 if (tsp[4] > 183) // corrupted data?
100 static u32 dvb_dmx_crc32 (struct dvb_demux_feed *f, const u8 *src, size_t len)
102 return (f->feed.sec.crc_val = crc32_be (f->feed.sec.crc_val, src, len));
106 static void dvb_dmx_memcopy (struct dvb_demux_feed *f, u8 *d, const u8 *s, size_t len)
112 /******************************************************************************
113 * Software filter functions
114 ******************************************************************************/
116 static inline int dvb_dmx_swfilter_payload (struct dvb_demux_feed *feed, const u8 *buf)
118 int count = payload(buf);
130 ccok=((dvbdmxfeed->cc+1)&0x0f)==cc ? 1 : 0;
133 printk("missed packet!\n");
136 if (buf[1] & 0x40) // PUSI ?
137 feed->peslen = 0xfffa;
139 feed->peslen += count;
141 return feed->cb.ts (&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
145 static int dvb_dmx_swfilter_sectionfilter (struct dvb_demux_feed *feed,
146 struct dvb_demux_filter *f)
151 for (i=0; i<DVB_DEMUX_MASK_MAX; i++) {
152 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
154 if (f->maskandmode[i] & xor)
157 neq |= f->maskandnotmode[i] & xor;
160 if (f->doneq && !neq)
163 return feed->cb.sec (feed->feed.sec.secbuf, feed->feed.sec.seclen,
164 NULL, 0, &f->filter, DMX_OK);
168 static inline int dvb_dmx_swfilter_section_feed (struct dvb_demux_feed *feed)
170 struct dvb_demux *demux = feed->demux;
171 struct dvb_demux_filter *f = feed->filter;
172 struct dmx_section_feed *sec = &feed->feed.sec;
173 int section_syntax_indicator;
175 if (!sec->is_filtering)
181 if (sec->check_crc) {
182 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
183 if (section_syntax_indicator &&
184 demux->check_crc32(feed, sec->secbuf, sec->seclen))
189 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
191 } while ((f = f->next) && sec->is_filtering);
199 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
201 struct dmx_section_feed *sec = &feed->feed.sec;
203 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
204 if(sec->secbufp < sec->tsfeedp)
206 int i, n = sec->tsfeedp - sec->secbufp;
208 /* section padding is done with 0xff bytes entirely.
209 ** due to speed reasons, we won't check all of them
210 ** but just first and last
212 if(sec->secbuf[0] != 0xff || sec->secbuf[n-1] != 0xff)
214 printk("dvb_demux.c section ts padding loss: %d/%d\n",
216 printk("dvb_demux.c pad data:");
217 for(i = 0; i < n; i++)
218 printk(" %02x", sec->secbuf[i]);
224 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
225 sec->secbuf = sec->secbuf_base;
229 ** Losless Section Demux 1.4.1 by Emard
230 ** Valsecchi Patrick:
231 ** - middle of section A (no PUSI)
232 ** - end of section A and start of section B
233 ** (with PUSI pointing to the start of the second section)
235 ** In this case, without feed->pusi_seen you'll receive a garbage section
236 ** consisting of the end of section A. Basically because tsfeedp
237 ** is incemented and the use=0 condition is not raised
238 ** when the second packet arrives.
241 ** when demux is started, let feed->pusi_seen = 0 to
242 ** prevent initial feeding of garbage from the end of
243 ** previous section. When you for the first time see PUSI=1
244 ** then set feed->pusi_seen = 1
246 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed, const u8 *buf, u8 len)
248 struct dvb_demux *demux = feed->demux;
249 struct dmx_section_feed *sec = &feed->feed.sec;
250 u16 limit, seclen, n;
252 if(sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
255 if(sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE)
257 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
258 printk("dvb_demux.c section buffer full loss: %d/%d\n",
259 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE, DMX_MAX_SECFEED_SIZE);
261 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
267 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
270 /* -----------------------------------------------------
271 ** Dump all the sections we can find in the data (Emard)
274 limit = sec->tsfeedp;
275 if(limit > DMX_MAX_SECFEED_SIZE)
276 return -1; /* internal error should never happen */
278 /* to be sure always set secbuf */
279 sec->secbuf = sec->secbuf_base + sec->secbufp;
281 for(n = 0; sec->secbufp + 2 < limit; n++)
283 seclen = section_length(sec->secbuf);
284 if(seclen <= 0 || seclen > DMX_MAX_SECFEED_SIZE
285 || seclen + sec->secbufp > limit)
287 sec->seclen = seclen;
289 /* dump [secbuf .. secbuf+seclen) */
291 dvb_dmx_swfilter_section_feed(feed);
292 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
294 printk("dvb_demux.c pusi not seen, discarding section data\n");
296 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
297 sec->secbuf += seclen; /* redundand but saves pointer arithmetic */
304 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed, const u8 *buf)
310 count = payload(buf);
312 if (count == 0) /* count == 0 if no payload or out of range */
315 p = 188 - count; /* payload start */
318 ccok = ((feed->cc + 1) & 0x0f) == cc;
322 /* adaption field present, check for discontinuity_indicator */
323 if ((buf[4] > 0) && (buf[5] & 0x80))
328 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
329 printk("dvb_demux.c discontinuity detected %d bytes lost\n", count);
330 /* those bytes under sume circumstances will again be reported
331 ** in the following dvb_dmx_swfilter_section_new
334 /* Discontinuity detected. Reset pusi_seen = 0 to
335 ** stop feeding of suspicious data until next PUSI=1 arrives
338 dvb_dmx_swfilter_section_new(feed);
343 // PUSI=1 (is set), section boundary is here
344 if (count > 1 && buf[p] < count) {
345 const u8 *before = buf+p+1;
346 u8 before_len = buf[p];
347 const u8 *after = before+before_len;
348 u8 after_len = count-1-before_len;
350 dvb_dmx_swfilter_section_copy_dump(feed, before, before_len);
351 /* before start of new section, set pusi_seen = 1 */
353 dvb_dmx_swfilter_section_new(feed);
354 dvb_dmx_swfilter_section_copy_dump(feed, after, after_len);
356 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
359 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
362 // PUSI=0 (is not set), no section boundary
363 const u8 *entire = buf+p;
364 u8 entire_len = count;
366 dvb_dmx_swfilter_section_copy_dump(feed, entire, entire_len);
372 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed, const u8 *buf)
376 if (!feed->feed.ts.is_filtering)
378 if (feed->ts_type & TS_PACKET) {
379 if (feed->ts_type & TS_PAYLOAD_ONLY)
380 dvb_dmx_swfilter_payload(feed, buf);
382 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
384 if (feed->ts_type & TS_DECODER)
385 if (feed->demux->write_to_decoder)
386 feed->demux->write_to_decoder(feed, buf, 188);
390 if (!feed->feed.sec.is_filtering)
392 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
393 feed->feed.sec.seclen = feed->feed.sec.secbufp=0;
401 #define DVR_FEED(f) \
402 (((f)->type == DMX_TYPE_TS) && \
403 ((f)->feed.ts.is_filtering) && \
404 (((f)->ts_type & (TS_PACKET|TS_PAYLOAD_ONLY)) == TS_PACKET))
406 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
408 struct dvb_demux_feed *feed;
409 struct list_head *pos, *head=&demux->feed_list;
410 u16 pid = ts_pid(buf);
413 list_for_each(pos, head) {
414 feed = list_entry(pos, struct dvb_demux_feed, list_head);
416 if ((feed->pid != pid) && (feed->pid != 0x2000))
419 /* copy each packet only once to the dvr device, even
420 * if a PID is in multiple filters (e.g. video + PCR) */
421 if ((DVR_FEED(feed)) && (dvr_done++))
424 if (feed->pid == pid) {
425 dvb_dmx_swfilter_packet_type(feed, buf);
430 if (feed->pid == 0x2000)
431 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
435 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, size_t count)
437 spin_lock(&demux->lock);
441 dvb_dmx_swfilter_packet(demux, buf);
446 spin_unlock(&demux->lock);
448 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
451 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
455 spin_lock(&demux->lock);
457 if ((i = demux->tsbufp)) {
458 if (count < (j=188-i)) {
459 memcpy(&demux->tsbuf[i], buf, count);
460 demux->tsbufp += count;
463 memcpy(&demux->tsbuf[i], buf, j);
464 if (demux->tsbuf[0] == 0x47)
465 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
471 if (buf[p] == 0x47) {
472 if (count-p >= 188) {
473 dvb_dmx_swfilter_packet(demux, buf+p);
477 memcpy(demux->tsbuf, buf+p, i);
486 spin_unlock(&demux->lock);
488 EXPORT_SYMBOL(dvb_dmx_swfilter);
490 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
494 spin_lock(&demux->lock);
496 if ((i = demux->tsbufp)) {
497 if (count < (j=204-i)) {
498 memcpy(&demux->tsbuf[i], buf, count);
499 demux->tsbufp += count;
502 memcpy(&demux->tsbuf[i], buf, j);
503 if ((demux->tsbuf[0] == 0x47)|(demux->tsbuf[0]==0xB8)) {
504 memcpy(tmppack, demux->tsbuf, 188);
505 if (tmppack[0] == 0xB8) tmppack[0] = 0x47;
506 dvb_dmx_swfilter_packet(demux, tmppack);
513 if ((buf[p] == 0x47)|(buf[p] == 0xB8)) {
514 if (count-p >= 204) {
515 memcpy(tmppack, buf+p, 188);
516 if (tmppack[0] == 0xB8) tmppack[0] = 0x47;
517 dvb_dmx_swfilter_packet(demux, tmppack);
521 memcpy(demux->tsbuf, buf+p, i);
531 spin_unlock(&demux->lock);
533 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
536 static struct dvb_demux_filter * dvb_dmx_filter_alloc(struct dvb_demux *demux)
540 for (i=0; i<demux->filternum; i++)
541 if (demux->filter[i].state == DMX_STATE_FREE)
544 if (i == demux->filternum)
547 demux->filter[i].state = DMX_STATE_ALLOCATED;
549 return &demux->filter[i];
552 static struct dvb_demux_feed * dvb_dmx_feed_alloc(struct dvb_demux *demux)
556 for (i=0; i<demux->feednum; i++)
557 if (demux->feed[i].state == DMX_STATE_FREE)
560 if (i == demux->feednum)
563 demux->feed[i].state = DMX_STATE_ALLOCATED;
565 return &demux->feed[i];
568 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
570 struct dvb_demux_feed *entry;
572 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
579 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
581 spin_lock_irq(&feed->demux->lock);
582 if (dvb_demux_feed_find(feed)) {
583 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
584 __FUNCTION__, feed->type, feed->state, feed->pid);
588 list_add(&feed->list_head, &feed->demux->feed_list);
590 spin_unlock_irq(&feed->demux->lock);
593 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
595 spin_lock_irq(&feed->demux->lock);
596 if (!(dvb_demux_feed_find(feed))) {
597 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
598 __FUNCTION__, feed->type, feed->state, feed->pid);
602 list_del(&feed->list_head);
604 spin_unlock_irq(&feed->demux->lock);
607 static int dmx_ts_feed_set (struct dmx_ts_feed* ts_feed, u16 pid, int ts_type,
608 enum dmx_ts_pes pes_type, size_t callback_length,
609 size_t circular_buffer_size, int descramble,
610 struct timespec timeout)
612 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
613 struct dvb_demux *demux = feed->demux;
615 if (pid > DMX_MAX_PID)
618 if (down_interruptible (&demux->mutex))
621 if (ts_type & TS_DECODER) {
622 if (pes_type >= DMX_TS_PES_OTHER) {
627 if (demux->pesfilter[pes_type] &&
628 demux->pesfilter[pes_type] != feed) {
633 demux->pesfilter[pes_type] = feed;
634 demux->pids[pes_type] = pid;
637 dvb_demux_feed_add(feed);
640 feed->buffer_size = circular_buffer_size;
641 feed->descramble = descramble;
642 feed->timeout = timeout;
643 feed->cb_length = callback_length;
644 feed->ts_type = ts_type;
645 feed->pes_type = pes_type;
647 if (feed->descramble) {
652 if (feed->buffer_size) {
656 feed->buffer = vmalloc(feed->buffer_size);
664 feed->state = DMX_STATE_READY;
671 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed* ts_feed)
673 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
674 struct dvb_demux *demux = feed->demux;
677 if (down_interruptible (&demux->mutex))
680 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
685 if (!demux->start_feed) {
690 if ((ret = demux->start_feed(feed)) < 0) {
695 spin_lock_irq(&demux->lock);
696 ts_feed->is_filtering = 1;
697 feed->state = DMX_STATE_GO;
698 spin_unlock_irq(&demux->lock);
704 static int dmx_ts_feed_stop_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 (down_interruptible (&demux->mutex))
713 if (feed->state < DMX_STATE_GO) {
718 if (!demux->stop_feed) {
723 ret = demux->stop_feed(feed);
725 spin_lock_irq(&demux->lock);
726 ts_feed->is_filtering = 0;
727 feed->state = DMX_STATE_ALLOCATED;
728 spin_unlock_irq(&demux->lock);
734 static int dvbdmx_allocate_ts_feed (struct dmx_demux *dmx, struct dmx_ts_feed **ts_feed,
737 struct dvb_demux *demux = (struct dvb_demux *) dmx;
738 struct dvb_demux_feed *feed;
740 if (down_interruptible (&demux->mutex))
743 if (!(feed = dvb_dmx_feed_alloc(demux))) {
748 feed->type = DMX_TYPE_TS;
749 feed->cb.ts = callback;
752 feed->peslen = 0xfffa;
755 (*ts_feed) = &feed->feed.ts;
756 (*ts_feed)->parent = dmx;
757 (*ts_feed)->priv = NULL;
758 (*ts_feed)->is_filtering = 0;
759 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
760 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
761 (*ts_feed)->set = dmx_ts_feed_set;
764 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
765 feed->state = DMX_STATE_FREE;
770 feed->filter->type = DMX_TYPE_TS;
771 feed->filter->feed = feed;
772 feed->filter->state = DMX_STATE_READY;
779 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx, struct dmx_ts_feed *ts_feed)
781 struct dvb_demux *demux = (struct dvb_demux *) dmx;
782 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
784 if (down_interruptible (&demux->mutex))
787 if (feed->state == DMX_STATE_FREE) {
797 feed->state = DMX_STATE_FREE;
798 feed->filter->state = DMX_STATE_FREE;
800 dvb_demux_feed_del(feed);
804 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
805 demux->pesfilter[feed->pes_type] = NULL;
812 /******************************************************************************
813 * dmx_section_feed API calls
814 ******************************************************************************/
816 static int dmx_section_feed_allocate_filter(struct dmx_section_feed* feed,
817 struct dmx_section_filter** filter)
819 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
820 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
821 struct dvb_demux_filter *dvbdmxfilter;
823 if (down_interruptible (&dvbdemux->mutex))
826 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
828 up(&dvbdemux->mutex);
832 spin_lock_irq(&dvbdemux->lock);
833 *filter = &dvbdmxfilter->filter;
834 (*filter)->parent = feed;
835 (*filter)->priv = NULL;
836 dvbdmxfilter->feed = dvbdmxfeed;
837 dvbdmxfilter->type = DMX_TYPE_SEC;
838 dvbdmxfilter->state = DMX_STATE_READY;
839 dvbdmxfilter->next = dvbdmxfeed->filter;
840 dvbdmxfeed->filter = dvbdmxfilter;
841 spin_unlock_irq(&dvbdemux->lock);
843 up(&dvbdemux->mutex);
848 static int dmx_section_feed_set(struct dmx_section_feed* feed,
849 u16 pid, size_t circular_buffer_size,
850 int descramble, int check_crc)
852 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
853 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
858 if (down_interruptible (&dvbdmx->mutex))
861 dvb_demux_feed_add(dvbdmxfeed);
863 dvbdmxfeed->pid = pid;
864 dvbdmxfeed->buffer_size = circular_buffer_size;
865 dvbdmxfeed->descramble = descramble;
866 if (dvbdmxfeed->descramble) {
871 dvbdmxfeed->feed.sec.check_crc = check_crc;
874 dvbdmxfeed->buffer = NULL;
876 dvbdmxfeed->buffer=vmalloc(dvbdmxfeed->buffer_size);
877 if (!dvbdmxfeed->buffer) {
883 dvbdmxfeed->state = DMX_STATE_READY;
889 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
892 struct dvb_demux_filter *f;
893 struct dmx_section_filter *sf;
894 u8 mask, mode, doneq;
896 if (!(f=dvbdmxfeed->filter))
901 for (i=0; i<DVB_DEMUX_MASK_MAX; i++) {
902 mode = sf->filter_mode[i];
903 mask = sf->filter_mask[i];
904 f->maskandmode[i] = mask & mode;
905 doneq |= f->maskandnotmode[i] = mask & ~mode;
907 f->doneq = doneq ? 1 : 0;
908 } while ((f = f->next));
912 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
914 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
915 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
918 if (down_interruptible (&dvbdmx->mutex))
921 if (feed->is_filtering) {
926 if (!dvbdmxfeed->filter) {
931 dvbdmxfeed->feed.sec.tsfeedp = 0;
932 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
933 dvbdmxfeed->feed.sec.secbufp = 0;
934 dvbdmxfeed->feed.sec.seclen = 0;
936 if (!dvbdmx->start_feed) {
941 prepare_secfilters(dvbdmxfeed);
943 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
948 spin_lock_irq(&dvbdmx->lock);
949 feed->is_filtering = 1;
950 dvbdmxfeed->state = DMX_STATE_GO;
951 spin_unlock_irq(&dvbdmx->lock);
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 if (down_interruptible (&dvbdmx->mutex))
967 if (!dvbdmx->stop_feed) {
972 ret = dvbdmx->stop_feed(dvbdmxfeed);
974 spin_lock_irq(&dvbdmx->lock);
975 dvbdmxfeed->state = DMX_STATE_READY;
976 feed->is_filtering = 0;
977 spin_unlock_irq(&dvbdmx->lock);
984 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
985 struct dmx_section_filter* filter)
987 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *) filter, *f;
988 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
989 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
991 if (down_interruptible (&dvbdmx->mutex))
994 if (dvbdmxfilter->feed != dvbdmxfeed) {
999 if (feed->is_filtering)
1000 feed->stop_filtering(feed);
1002 spin_lock_irq(&dvbdmx->lock);
1003 f = dvbdmxfeed->filter;
1005 if (f == dvbdmxfilter) {
1006 dvbdmxfeed->filter = dvbdmxfilter->next;
1008 while(f->next != dvbdmxfilter)
1010 f->next = f->next->next;
1013 dvbdmxfilter->state = DMX_STATE_FREE;
1014 spin_unlock_irq(&dvbdmx->lock);
1019 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1020 struct dmx_section_feed **feed,
1021 dmx_section_cb callback)
1023 struct dvb_demux *dvbdmx = (struct dvb_demux *) demux;
1024 struct dvb_demux_feed *dvbdmxfeed;
1026 if (down_interruptible (&dvbdmx->mutex))
1027 return -ERESTARTSYS;
1029 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1034 dvbdmxfeed->type = DMX_TYPE_SEC;
1035 dvbdmxfeed->cb.sec = callback;
1036 dvbdmxfeed->demux = dvbdmx;
1037 dvbdmxfeed->pid = 0xffff;
1038 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1039 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1040 dvbdmxfeed->feed.sec.tsfeedp = 0;
1041 dvbdmxfeed->filter = NULL;
1042 dvbdmxfeed->buffer = NULL;
1044 (*feed)=&dvbdmxfeed->feed.sec;
1045 (*feed)->is_filtering = 0;
1046 (*feed)->parent = demux;
1047 (*feed)->priv = NULL;
1049 (*feed)->set = dmx_section_feed_set;
1050 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1051 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1052 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1053 (*feed)->release_filter = dmx_section_feed_release_filter;
1059 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1060 struct dmx_section_feed *feed)
1062 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
1063 struct dvb_demux *dvbdmx = (struct dvb_demux *) demux;
1065 if (down_interruptible (&dvbdmx->mutex))
1066 return -ERESTARTSYS;
1068 if (dvbdmxfeed->state==DMX_STATE_FREE) {
1073 vfree(dvbdmxfeed->buffer);
1074 dvbdmxfeed->buffer=0;
1076 dvbdmxfeed->state=DMX_STATE_FREE;
1078 dvb_demux_feed_del(dvbdmxfeed);
1080 dvbdmxfeed->pid = 0xffff;
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)
1103 static int dvbdmx_close(struct dmx_demux *demux)
1105 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1107 if (dvbdemux->users == 0)
1111 //FIXME: release any unneeded resources if users==0
1116 static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
1118 struct dvb_demux *dvbdemux=(struct dvb_demux *) demux;
1120 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1123 if (down_interruptible (&dvbdemux->mutex))
1124 return -ERESTARTSYS;
1125 dvb_dmx_swfilter(dvbdemux, buf, count);
1126 up(&dvbdemux->mutex);
1128 if (signal_pending(current))
1134 static int dvbdmx_add_frontend(struct dmx_demux *demux, 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);
1145 static int dvbdmx_remove_frontend(struct dmx_demux *demux, 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) {
1161 static struct list_head * dvbdmx_get_frontends(struct dmx_demux *demux)
1163 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1165 if (list_empty(&dvbdemux->frontend_list))
1167 return &dvbdemux->frontend_list;
1171 static int dvbdmx_connect_frontend(struct dmx_demux *demux, struct dmx_frontend *frontend)
1173 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1175 if (demux->frontend)
1178 if (down_interruptible (&dvbdemux->mutex))
1179 return -ERESTARTSYS;
1181 demux->frontend = frontend;
1182 up(&dvbdemux->mutex);
1187 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1189 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1191 if (down_interruptible (&dvbdemux->mutex))
1192 return -ERESTARTSYS;
1194 demux->frontend = NULL;
1195 up(&dvbdemux->mutex);
1200 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 *pids)
1202 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1204 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->users = 0;
1215 dvbdemux->filter = vmalloc(dvbdemux->filternum*sizeof(struct dvb_demux_filter));
1217 if (!dvbdemux->filter)
1220 dvbdemux->feed = vmalloc(dvbdemux->feednum*sizeof(struct dvb_demux_feed));
1221 if (!dvbdemux->feed) {
1222 vfree(dvbdemux->filter);
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;
1233 dvbdemux->frontend_list.next=
1234 dvbdemux->frontend_list.prev=
1235 &dvbdemux->frontend_list;
1236 for (i=0; i<DMX_TS_PES_OTHER; i++) {
1237 dvbdemux->pesfilter[i] = NULL;
1238 dvbdemux->pids[i] = 0xffff;
1241 INIT_LIST_HEAD(&dvbdemux->feed_list);
1243 dvbdemux->playing = 0;
1244 dvbdemux->recording = 0;
1245 dvbdemux->tsbufp = 0;
1247 if (!dvbdemux->check_crc32)
1248 dvbdemux->check_crc32 = dvb_dmx_crc32;
1250 if (!dvbdemux->memcopy)
1251 dvbdemux->memcopy = dvb_dmx_memcopy;
1253 dmx->frontend = NULL;
1254 dmx->reg_list.prev = dmx->reg_list.next = &dmx->reg_list;
1255 dmx->priv = (void *) 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->descramble_mac_address = NULL;
1265 dmx->descramble_section_payload = NULL;
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 sema_init(&dvbdemux->mutex, 1);
1275 spin_lock_init(&dvbdemux->lock);
1277 if ((err = dmx_register_demux(dmx)) < 0)
1282 EXPORT_SYMBOL(dvb_dmx_init);
1285 int dvb_dmx_release(struct dvb_demux *dvbdemux)
1287 struct dmx_demux *dmx = &dvbdemux->dmx;
1289 dmx_unregister_demux(dmx);
1290 vfree(dvbdemux->filter);
1291 vfree(dvbdemux->feed);
1294 EXPORT_SYMBOL(dvb_dmx_release);