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);
342 // PUSI=1 (is set), section boundary is here
343 if (count > 1 && buf[p] < count) {
344 const u8 *before = buf+p+1;
345 u8 before_len = buf[p];
346 const u8 *after = before+before_len;
347 u8 after_len = count-1-before_len;
349 dvb_dmx_swfilter_section_copy_dump(feed, before, before_len);
350 /* before start of new section, set pusi_seen = 1 */
352 dvb_dmx_swfilter_section_new(feed);
353 dvb_dmx_swfilter_section_copy_dump(feed, after, after_len);
355 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
358 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
361 // PUSI=0 (is not set), no section boundary
362 const u8 *entire = buf+p;
363 u8 entire_len = count;
365 dvb_dmx_swfilter_section_copy_dump(feed, entire, entire_len);
371 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed, const u8 *buf)
375 if (!feed->feed.ts.is_filtering)
377 if (feed->ts_type & TS_PACKET) {
378 if (feed->ts_type & TS_PAYLOAD_ONLY)
379 dvb_dmx_swfilter_payload(feed, buf);
381 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
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_PAYLOAD_ONLY)) == TS_PACKET))
405 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
407 struct dvb_demux_feed *feed;
408 struct list_head *pos, *head=&demux->feed_list;
409 u16 pid = ts_pid(buf);
412 list_for_each(pos, head) {
413 feed = list_entry(pos, struct dvb_demux_feed, list_head);
415 if ((feed->pid != pid) && (feed->pid != 0x2000))
418 /* copy each packet only once to the dvr device, even
419 * if a PID is in multiple filters (e.g. video + PCR) */
420 if ((DVR_FEED(feed)) && (dvr_done++))
423 if (feed->pid == pid) {
424 dvb_dmx_swfilter_packet_type(feed, buf);
429 if (feed->pid == 0x2000)
430 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
434 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf, size_t count)
436 spin_lock(&demux->lock);
440 dvb_dmx_swfilter_packet(demux, buf);
445 spin_unlock(&demux->lock);
447 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
450 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
454 spin_lock(&demux->lock);
456 if ((i = demux->tsbufp)) {
457 if (count < (j=188-i)) {
458 memcpy(&demux->tsbuf[i], buf, count);
459 demux->tsbufp += count;
462 memcpy(&demux->tsbuf[i], buf, j);
463 if (demux->tsbuf[0] == 0x47)
464 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
470 if (buf[p] == 0x47) {
471 if (count-p >= 188) {
472 dvb_dmx_swfilter_packet(demux, buf+p);
476 memcpy(demux->tsbuf, buf+p, i);
485 spin_unlock(&demux->lock);
487 EXPORT_SYMBOL(dvb_dmx_swfilter);
489 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
493 spin_lock(&demux->lock);
495 if ((i = demux->tsbufp)) {
496 if (count < (j=204-i)) {
497 memcpy(&demux->tsbuf[i], buf, count);
498 demux->tsbufp += count;
501 memcpy(&demux->tsbuf[i], buf, j);
502 if ((demux->tsbuf[0] == 0x47)|(demux->tsbuf[0]==0xB8)) {
503 memcpy(tmppack, demux->tsbuf, 188);
504 if (tmppack[0] == 0xB8) tmppack[0] = 0x47;
505 dvb_dmx_swfilter_packet(demux, tmppack);
512 if ((buf[p] == 0x47)|(buf[p] == 0xB8)) {
513 if (count-p >= 204) {
514 memcpy(tmppack, buf+p, 188);
515 if (tmppack[0] == 0xB8) tmppack[0] = 0x47;
516 dvb_dmx_swfilter_packet(demux, tmppack);
520 memcpy(demux->tsbuf, buf+p, i);
530 spin_unlock(&demux->lock);
532 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
535 static struct dvb_demux_filter * dvb_dmx_filter_alloc(struct dvb_demux *demux)
539 for (i=0; i<demux->filternum; i++)
540 if (demux->filter[i].state == DMX_STATE_FREE)
543 if (i == demux->filternum)
546 demux->filter[i].state = DMX_STATE_ALLOCATED;
548 return &demux->filter[i];
551 static struct dvb_demux_feed * dvb_dmx_feed_alloc(struct dvb_demux *demux)
555 for (i=0; i<demux->feednum; i++)
556 if (demux->feed[i].state == DMX_STATE_FREE)
559 if (i == demux->feednum)
562 demux->feed[i].state = DMX_STATE_ALLOCATED;
564 return &demux->feed[i];
567 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
569 struct dvb_demux_feed *entry;
571 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
578 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
580 spin_lock_irq(&feed->demux->lock);
581 if (dvb_demux_feed_find(feed)) {
582 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
583 __FUNCTION__, feed->type, feed->state, feed->pid);
587 list_add(&feed->list_head, &feed->demux->feed_list);
589 spin_unlock_irq(&feed->demux->lock);
592 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
594 spin_lock_irq(&feed->demux->lock);
595 if (!(dvb_demux_feed_find(feed))) {
596 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
597 __FUNCTION__, feed->type, feed->state, feed->pid);
601 list_del(&feed->list_head);
603 spin_unlock_irq(&feed->demux->lock);
606 static int dmx_ts_feed_set (struct dmx_ts_feed* ts_feed, u16 pid, int ts_type,
607 enum dmx_ts_pes pes_type, size_t callback_length,
608 size_t circular_buffer_size, int descramble,
609 struct timespec timeout)
611 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
612 struct dvb_demux *demux = feed->demux;
614 if (pid > DMX_MAX_PID)
617 if (down_interruptible (&demux->mutex))
620 if (ts_type & TS_DECODER) {
621 if (pes_type >= DMX_TS_PES_OTHER) {
626 if (demux->pesfilter[pes_type] &&
627 demux->pesfilter[pes_type] != feed) {
632 demux->pesfilter[pes_type] = feed;
633 demux->pids[pes_type] = pid;
636 dvb_demux_feed_add(feed);
639 feed->buffer_size = circular_buffer_size;
640 feed->descramble = descramble;
641 feed->timeout = timeout;
642 feed->cb_length = callback_length;
643 feed->ts_type = ts_type;
644 feed->pes_type = pes_type;
646 if (feed->descramble) {
651 if (feed->buffer_size) {
655 feed->buffer = vmalloc(feed->buffer_size);
663 feed->state = DMX_STATE_READY;
670 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed* ts_feed)
672 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
673 struct dvb_demux *demux = feed->demux;
676 if (down_interruptible (&demux->mutex))
679 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
684 if (!demux->start_feed) {
689 if ((ret = demux->start_feed(feed)) < 0) {
694 spin_lock_irq(&demux->lock);
695 ts_feed->is_filtering = 1;
696 feed->state = DMX_STATE_GO;
697 spin_unlock_irq(&demux->lock);
703 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed* ts_feed)
705 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
706 struct dvb_demux *demux = feed->demux;
709 if (down_interruptible (&demux->mutex))
712 if (feed->state < DMX_STATE_GO) {
717 if (!demux->stop_feed) {
722 ret = demux->stop_feed(feed);
724 spin_lock_irq(&demux->lock);
725 ts_feed->is_filtering = 0;
726 feed->state = DMX_STATE_ALLOCATED;
727 spin_unlock_irq(&demux->lock);
733 static int dvbdmx_allocate_ts_feed (struct dmx_demux *dmx, struct dmx_ts_feed **ts_feed,
736 struct dvb_demux *demux = (struct dvb_demux *) dmx;
737 struct dvb_demux_feed *feed;
739 if (down_interruptible (&demux->mutex))
742 if (!(feed = dvb_dmx_feed_alloc(demux))) {
747 feed->type = DMX_TYPE_TS;
748 feed->cb.ts = callback;
751 feed->peslen = 0xfffa;
754 (*ts_feed) = &feed->feed.ts;
755 (*ts_feed)->parent = dmx;
756 (*ts_feed)->priv = NULL;
757 (*ts_feed)->is_filtering = 0;
758 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
759 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
760 (*ts_feed)->set = dmx_ts_feed_set;
763 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
764 feed->state = DMX_STATE_FREE;
769 feed->filter->type = DMX_TYPE_TS;
770 feed->filter->feed = feed;
771 feed->filter->state = DMX_STATE_READY;
778 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx, struct dmx_ts_feed *ts_feed)
780 struct dvb_demux *demux = (struct dvb_demux *) dmx;
781 struct dvb_demux_feed *feed = (struct dvb_demux_feed *) ts_feed;
783 if (down_interruptible (&demux->mutex))
786 if (feed->state == DMX_STATE_FREE) {
796 feed->state = DMX_STATE_FREE;
797 feed->filter->state = DMX_STATE_FREE;
799 dvb_demux_feed_del(feed);
803 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
804 demux->pesfilter[feed->pes_type] = NULL;
811 /******************************************************************************
812 * dmx_section_feed API calls
813 ******************************************************************************/
815 static int dmx_section_feed_allocate_filter(struct dmx_section_feed* feed,
816 struct dmx_section_filter** filter)
818 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
819 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
820 struct dvb_demux_filter *dvbdmxfilter;
822 if (down_interruptible (&dvbdemux->mutex))
825 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
827 up(&dvbdemux->mutex);
831 spin_lock_irq(&dvbdemux->lock);
832 *filter = &dvbdmxfilter->filter;
833 (*filter)->parent = feed;
834 (*filter)->priv = NULL;
835 dvbdmxfilter->feed = dvbdmxfeed;
836 dvbdmxfilter->type = DMX_TYPE_SEC;
837 dvbdmxfilter->state = DMX_STATE_READY;
838 dvbdmxfilter->next = dvbdmxfeed->filter;
839 dvbdmxfeed->filter = dvbdmxfilter;
840 spin_unlock_irq(&dvbdemux->lock);
842 up(&dvbdemux->mutex);
847 static int dmx_section_feed_set(struct dmx_section_feed* feed,
848 u16 pid, size_t circular_buffer_size,
849 int descramble, int check_crc)
851 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
852 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
857 if (down_interruptible (&dvbdmx->mutex))
860 dvb_demux_feed_add(dvbdmxfeed);
862 dvbdmxfeed->pid = pid;
863 dvbdmxfeed->buffer_size = circular_buffer_size;
864 dvbdmxfeed->descramble = descramble;
865 if (dvbdmxfeed->descramble) {
870 dvbdmxfeed->feed.sec.check_crc = check_crc;
873 dvbdmxfeed->buffer = NULL;
875 dvbdmxfeed->buffer=vmalloc(dvbdmxfeed->buffer_size);
876 if (!dvbdmxfeed->buffer) {
882 dvbdmxfeed->state = DMX_STATE_READY;
888 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
891 struct dvb_demux_filter *f;
892 struct dmx_section_filter *sf;
893 u8 mask, mode, doneq;
895 if (!(f=dvbdmxfeed->filter))
900 for (i=0; i<DVB_DEMUX_MASK_MAX; i++) {
901 mode = sf->filter_mode[i];
902 mask = sf->filter_mask[i];
903 f->maskandmode[i] = mask & mode;
904 doneq |= f->maskandnotmode[i] = mask & ~mode;
906 f->doneq = doneq ? 1 : 0;
907 } while ((f = f->next));
911 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
913 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
914 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
917 if (down_interruptible (&dvbdmx->mutex))
920 if (feed->is_filtering) {
925 if (!dvbdmxfeed->filter) {
930 dvbdmxfeed->feed.sec.tsfeedp = 0;
931 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
932 dvbdmxfeed->feed.sec.secbufp = 0;
933 dvbdmxfeed->feed.sec.seclen = 0;
935 if (!dvbdmx->start_feed) {
940 prepare_secfilters(dvbdmxfeed);
942 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
947 spin_lock_irq(&dvbdmx->lock);
948 feed->is_filtering = 1;
949 dvbdmxfeed->state = DMX_STATE_GO;
950 spin_unlock_irq(&dvbdmx->lock);
957 static int dmx_section_feed_stop_filtering(struct dmx_section_feed* feed)
959 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
960 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
963 if (down_interruptible (&dvbdmx->mutex))
966 if (!dvbdmx->stop_feed) {
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);
983 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
984 struct dmx_section_filter* filter)
986 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *) filter, *f;
987 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
988 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
990 if (down_interruptible (&dvbdmx->mutex))
993 if (dvbdmxfilter->feed != dvbdmxfeed) {
998 if (feed->is_filtering)
999 feed->stop_filtering(feed);
1001 spin_lock_irq(&dvbdmx->lock);
1002 f = dvbdmxfeed->filter;
1004 if (f == dvbdmxfilter) {
1005 dvbdmxfeed->filter = dvbdmxfilter->next;
1007 while(f->next != dvbdmxfilter)
1009 f->next = f->next->next;
1012 dvbdmxfilter->state = DMX_STATE_FREE;
1013 spin_unlock_irq(&dvbdmx->lock);
1018 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1019 struct dmx_section_feed **feed,
1020 dmx_section_cb callback)
1022 struct dvb_demux *dvbdmx = (struct dvb_demux *) demux;
1023 struct dvb_demux_feed *dvbdmxfeed;
1025 if (down_interruptible (&dvbdmx->mutex))
1026 return -ERESTARTSYS;
1028 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1033 dvbdmxfeed->type = DMX_TYPE_SEC;
1034 dvbdmxfeed->cb.sec = callback;
1035 dvbdmxfeed->demux = dvbdmx;
1036 dvbdmxfeed->pid = 0xffff;
1037 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1038 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1039 dvbdmxfeed->feed.sec.tsfeedp = 0;
1040 dvbdmxfeed->filter = NULL;
1041 dvbdmxfeed->buffer = NULL;
1043 (*feed)=&dvbdmxfeed->feed.sec;
1044 (*feed)->is_filtering = 0;
1045 (*feed)->parent = demux;
1046 (*feed)->priv = NULL;
1048 (*feed)->set = dmx_section_feed_set;
1049 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1050 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1051 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1052 (*feed)->release_filter = dmx_section_feed_release_filter;
1058 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1059 struct dmx_section_feed *feed)
1061 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) feed;
1062 struct dvb_demux *dvbdmx = (struct dvb_demux *) demux;
1064 if (down_interruptible (&dvbdmx->mutex))
1065 return -ERESTARTSYS;
1067 if (dvbdmxfeed->state==DMX_STATE_FREE) {
1072 vfree(dvbdmxfeed->buffer);
1073 dvbdmxfeed->buffer=0;
1075 dvbdmxfeed->state=DMX_STATE_FREE;
1077 dvb_demux_feed_del(dvbdmxfeed);
1079 dvbdmxfeed->pid = 0xffff;
1086 /******************************************************************************
1087 * dvb_demux kernel data API calls
1088 ******************************************************************************/
1090 static int dvbdmx_open(struct dmx_demux *demux)
1092 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1094 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1102 static int dvbdmx_close(struct dmx_demux *demux)
1104 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1106 if (dvbdemux->users == 0)
1110 //FIXME: release any unneeded resources if users==0
1115 static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
1117 struct dvb_demux *dvbdemux=(struct dvb_demux *) demux;
1119 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1122 if (down_interruptible (&dvbdemux->mutex))
1123 return -ERESTARTSYS;
1124 dvb_dmx_swfilter(dvbdemux, buf, count);
1125 up(&dvbdemux->mutex);
1127 if (signal_pending(current))
1133 static int dvbdmx_add_frontend(struct dmx_demux *demux, struct dmx_frontend *frontend)
1135 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1136 struct list_head *head = &dvbdemux->frontend_list;
1138 list_add(&(frontend->connectivity_list), head);
1144 static int dvbdmx_remove_frontend(struct dmx_demux *demux, struct dmx_frontend *frontend)
1146 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1147 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1149 list_for_each_safe (pos, n, head) {
1150 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))
1166 return &dvbdemux->frontend_list;
1170 static int dvbdmx_connect_frontend(struct dmx_demux *demux, struct dmx_frontend *frontend)
1172 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1174 if (demux->frontend)
1177 if (down_interruptible (&dvbdemux->mutex))
1178 return -ERESTARTSYS;
1180 demux->frontend = frontend;
1181 up(&dvbdemux->mutex);
1186 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1188 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1190 if (down_interruptible (&dvbdemux->mutex))
1191 return -ERESTARTSYS;
1193 demux->frontend = NULL;
1194 up(&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));
1208 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1211 struct dmx_demux *dmx = &dvbdemux->dmx;
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);
1224 for (i=0; i<dvbdemux->filternum; i++) {
1225 dvbdemux->filter[i].state = DMX_STATE_FREE;
1226 dvbdemux->filter[i].index = i;
1228 for (i=0; i<dvbdemux->feednum; i++) {
1229 dvbdemux->feed[i].state = DMX_STATE_FREE;
1230 dvbdemux->feed[i].index = i;
1232 dvbdemux->frontend_list.next=
1233 dvbdemux->frontend_list.prev=
1234 &dvbdemux->frontend_list;
1235 for (i=0; i<DMX_TS_PES_OTHER; i++) {
1236 dvbdemux->pesfilter[i] = NULL;
1237 dvbdemux->pids[i] = 0xffff;
1240 INIT_LIST_HEAD(&dvbdemux->feed_list);
1242 dvbdemux->playing = 0;
1243 dvbdemux->recording = 0;
1244 dvbdemux->tsbufp = 0;
1246 if (!dvbdemux->check_crc32)
1247 dvbdemux->check_crc32 = dvb_dmx_crc32;
1249 if (!dvbdemux->memcopy)
1250 dvbdemux->memcopy = dvb_dmx_memcopy;
1252 dmx->frontend = NULL;
1253 dmx->reg_list.prev = dmx->reg_list.next = &dmx->reg_list;
1254 dmx->priv = (void *) dvbdemux;
1255 dmx->open = dvbdmx_open;
1256 dmx->close = dvbdmx_close;
1257 dmx->write = dvbdmx_write;
1258 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1259 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1260 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1261 dmx->release_section_feed = dvbdmx_release_section_feed;
1263 dmx->descramble_mac_address = NULL;
1264 dmx->descramble_section_payload = NULL;
1266 dmx->add_frontend = dvbdmx_add_frontend;
1267 dmx->remove_frontend = dvbdmx_remove_frontend;
1268 dmx->get_frontends = dvbdmx_get_frontends;
1269 dmx->connect_frontend = dvbdmx_connect_frontend;
1270 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1271 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1273 sema_init(&dvbdemux->mutex, 1);
1274 spin_lock_init(&dvbdemux->lock);
1276 if ((err = dmx_register_demux(dmx)) < 0)
1281 EXPORT_SYMBOL(dvb_dmx_init);
1284 int dvb_dmx_release(struct dvb_demux *dvbdemux)
1286 struct dmx_demux *dmx = &dvbdemux->dmx;
1288 dmx_unregister_demux(dmx);
1289 vfree(dvbdemux->filter);
1290 vfree(dvbdemux->feed);
1293 EXPORT_SYMBOL(dvb_dmx_release);