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
41 /******************************************************************************
42 * static inlined helper functions
43 ******************************************************************************/
45 static inline u16 section_length(const u8 *buf)
47 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
50 static inline u16 ts_pid(const u8 *buf)
52 return ((buf[1] & 0x1f) << 8) + buf[2];
55 static inline u8 payload(const u8 *tsp)
57 if (!(tsp[3] & 0x10)) // no payload?
60 if (tsp[3] & 0x20) { // adaptation field?
61 if (tsp[4] > 183) // corrupted data?
64 return 184 - 1 - tsp[4];
70 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
72 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
75 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
81 /******************************************************************************
82 * Software filter functions
83 ******************************************************************************/
85 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
88 int count = payload(buf);
100 ccok = ((feed->cc + 1) & 0x0f) == cc;
103 printk("missed packet!\n");
106 if (buf[1] & 0x40) // PUSI ?
107 feed->peslen = 0xfffa;
109 feed->peslen += count;
111 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
114 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
115 struct dvb_demux_filter *f)
120 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
121 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
123 if (f->maskandmode[i] & xor)
126 neq |= f->maskandnotmode[i] & xor;
129 if (f->doneq && !neq)
132 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
133 NULL, 0, &f->filter, DMX_OK);
136 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
138 struct dvb_demux *demux = feed->demux;
139 struct dvb_demux_filter *f = feed->filter;
140 struct dmx_section_feed *sec = &feed->feed.sec;
141 int section_syntax_indicator;
143 if (!sec->is_filtering)
149 if (sec->check_crc) {
150 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
151 if (section_syntax_indicator &&
152 demux->check_crc32(feed, sec->secbuf, sec->seclen))
157 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
159 } while ((f = f->next) && sec->is_filtering);
166 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
168 struct dmx_section_feed *sec = &feed->feed.sec;
170 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
171 if (sec->secbufp < sec->tsfeedp) {
172 int i, n = sec->tsfeedp - sec->secbufp;
175 * Section padding is done with 0xff bytes entirely.
176 * Due to speed reasons, we won't check all of them
177 * but just first and last.
179 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
180 printk("dvb_demux.c section ts padding loss: %d/%d\n",
182 printk("dvb_demux.c pad data:");
183 for (i = 0; i < n; i++)
184 printk(" %02x", sec->secbuf[i]);
190 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
191 sec->secbuf = sec->secbuf_base;
195 * Losless Section Demux 1.4.1 by Emard
197 * - middle of section A (no PUSI)
198 * - end of section A and start of section B
199 * (with PUSI pointing to the start of the second section)
201 * In this case, without feed->pusi_seen you'll receive a garbage section
202 * consisting of the end of section A. Basically because tsfeedp
203 * is incemented and the use=0 condition is not raised
204 * when the second packet arrives.
207 * when demux is started, let feed->pusi_seen = 0 to
208 * prevent initial feeding of garbage from the end of
209 * previous section. When you for the first time see PUSI=1
210 * then set feed->pusi_seen = 1
212 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
213 const u8 *buf, u8 len)
215 struct dvb_demux *demux = feed->demux;
216 struct dmx_section_feed *sec = &feed->feed.sec;
217 u16 limit, seclen, n;
219 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
222 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
223 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
224 printk("dvb_demux.c section buffer full loss: %d/%d\n",
225 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
226 DMX_MAX_SECFEED_SIZE);
228 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
234 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
238 * Dump all the sections we can find in the data (Emard)
240 limit = sec->tsfeedp;
241 if (limit > DMX_MAX_SECFEED_SIZE)
242 return -1; /* internal error should never happen */
244 /* to be sure always set secbuf */
245 sec->secbuf = sec->secbuf_base + sec->secbufp;
247 for (n = 0; sec->secbufp + 2 < limit; n++) {
248 seclen = section_length(sec->secbuf);
249 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
250 || seclen + sec->secbufp > limit)
252 sec->seclen = seclen;
254 /* dump [secbuf .. secbuf+seclen) */
256 dvb_dmx_swfilter_section_feed(feed);
257 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
259 printk("dvb_demux.c pusi not seen, discarding section data\n");
261 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
262 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
268 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
275 count = payload(buf);
277 if (count == 0) /* count == 0 if no payload or out of range */
280 p = 188 - count; /* payload start */
283 ccok = ((feed->cc + 1) & 0x0f) == cc;
287 /* adaption field present, check for discontinuity_indicator */
288 if ((buf[4] > 0) && (buf[5] & 0x80))
293 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
294 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
297 * those bytes under sume circumstances will again be reported
298 * in the following dvb_dmx_swfilter_section_new
302 * Discontinuity detected. Reset pusi_seen = 0 to
303 * stop feeding of suspicious data until next PUSI=1 arrives
306 dvb_dmx_swfilter_section_new(feed);
310 /* PUSI=1 (is set), section boundary is here */
311 if (count > 1 && buf[p] < count) {
312 const u8 *before = &buf[p + 1];
313 u8 before_len = buf[p];
314 const u8 *after = &before[before_len];
315 u8 after_len = count - 1 - before_len;
317 dvb_dmx_swfilter_section_copy_dump(feed, before,
319 /* before start of new section, set pusi_seen = 1 */
321 dvb_dmx_swfilter_section_new(feed);
322 dvb_dmx_swfilter_section_copy_dump(feed, after,
325 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
327 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
330 /* PUSI=0 (is not set), no section boundary */
331 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
337 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
340 switch (feed->type) {
342 if (!feed->feed.ts.is_filtering)
344 if (feed->ts_type & TS_PACKET) {
345 if (feed->ts_type & TS_PAYLOAD_ONLY)
346 dvb_dmx_swfilter_payload(feed, buf);
348 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
351 if (feed->ts_type & TS_DECODER)
352 if (feed->demux->write_to_decoder)
353 feed->demux->write_to_decoder(feed, buf, 188);
357 if (!feed->feed.sec.is_filtering)
359 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
360 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
368 #define DVR_FEED(f) \
369 (((f)->type == DMX_TYPE_TS) && \
370 ((f)->feed.ts.is_filtering) && \
371 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
373 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
375 struct dvb_demux_feed *feed;
376 u16 pid = ts_pid(buf);
379 list_for_each_entry(feed, &demux->feed_list, list_head) {
380 if ((feed->pid != pid) && (feed->pid != 0x2000))
383 /* copy each packet only once to the dvr device, even
384 * if a PID is in multiple filters (e.g. video + PCR) */
385 if ((DVR_FEED(feed)) && (dvr_done++))
388 if (feed->pid == pid) {
389 dvb_dmx_swfilter_packet_type(feed, buf);
394 if (feed->pid == 0x2000)
395 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
399 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
404 spin_lock_irqsave(&demux->lock, flags);
408 dvb_dmx_swfilter_packet(demux, buf);
412 spin_unlock_irqrestore(&demux->lock, flags);
415 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
417 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
422 spin_lock_irqsave(&demux->lock, flags);
428 memcpy(&demux->tsbuf[i], buf, count);
429 demux->tsbufp += count;
432 memcpy(&demux->tsbuf[i], buf, j);
433 if (demux->tsbuf[0] == 0x47)
434 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
440 if (buf[p] == 0x47) {
441 if (count - p >= 188) {
442 dvb_dmx_swfilter_packet(demux, &buf[p]);
446 memcpy(demux->tsbuf, &buf[p], i);
455 spin_unlock_irqrestore(&demux->lock, flags);
458 EXPORT_SYMBOL(dvb_dmx_swfilter);
460 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
466 spin_lock_irqsave(&demux->lock, flags);
472 memcpy(&demux->tsbuf[i], buf, count);
473 demux->tsbufp += count;
476 memcpy(&demux->tsbuf[i], buf, j);
477 if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
478 memcpy(tmppack, demux->tsbuf, 188);
479 if (tmppack[0] == 0xB8)
481 dvb_dmx_swfilter_packet(demux, tmppack);
488 if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
489 if (count - p >= 204) {
490 memcpy(tmppack, &buf[p], 188);
491 if (tmppack[0] == 0xB8)
493 dvb_dmx_swfilter_packet(demux, tmppack);
497 memcpy(demux->tsbuf, &buf[p], i);
507 spin_unlock_irqrestore(&demux->lock, flags);
510 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
512 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
516 for (i = 0; i < demux->filternum; i++)
517 if (demux->filter[i].state == DMX_STATE_FREE)
520 if (i == demux->filternum)
523 demux->filter[i].state = DMX_STATE_ALLOCATED;
525 return &demux->filter[i];
528 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
532 for (i = 0; i < demux->feednum; i++)
533 if (demux->feed[i].state == DMX_STATE_FREE)
536 if (i == demux->feednum)
539 demux->feed[i].state = DMX_STATE_ALLOCATED;
541 return &demux->feed[i];
544 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
546 struct dvb_demux_feed *entry;
548 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
555 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
557 spin_lock_irq(&feed->demux->lock);
558 if (dvb_demux_feed_find(feed)) {
559 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
560 __func__, feed->type, feed->state, feed->pid);
564 list_add(&feed->list_head, &feed->demux->feed_list);
566 spin_unlock_irq(&feed->demux->lock);
569 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
571 spin_lock_irq(&feed->demux->lock);
572 if (!(dvb_demux_feed_find(feed))) {
573 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
574 __func__, feed->type, feed->state, feed->pid);
578 list_del(&feed->list_head);
580 spin_unlock_irq(&feed->demux->lock);
583 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
584 enum dmx_ts_pes pes_type,
585 size_t circular_buffer_size, struct timespec timeout)
587 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
588 struct dvb_demux *demux = feed->demux;
590 if (pid > DMX_MAX_PID)
593 if (mutex_lock_interruptible(&demux->mutex))
596 if (ts_type & TS_DECODER) {
597 if (pes_type >= DMX_TS_PES_OTHER) {
598 mutex_unlock(&demux->mutex);
602 if (demux->pesfilter[pes_type] &&
603 demux->pesfilter[pes_type] != feed) {
604 mutex_unlock(&demux->mutex);
608 demux->pesfilter[pes_type] = feed;
609 demux->pids[pes_type] = pid;
612 dvb_demux_feed_add(feed);
615 feed->buffer_size = circular_buffer_size;
616 feed->timeout = timeout;
617 feed->ts_type = ts_type;
618 feed->pes_type = pes_type;
620 if (feed->buffer_size) {
624 feed->buffer = vmalloc(feed->buffer_size);
626 mutex_unlock(&demux->mutex);
632 feed->state = DMX_STATE_READY;
633 mutex_unlock(&demux->mutex);
638 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
640 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
641 struct dvb_demux *demux = feed->demux;
644 if (mutex_lock_interruptible(&demux->mutex))
647 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
648 mutex_unlock(&demux->mutex);
652 if (!demux->start_feed) {
653 mutex_unlock(&demux->mutex);
657 if ((ret = demux->start_feed(feed)) < 0) {
658 mutex_unlock(&demux->mutex);
662 spin_lock_irq(&demux->lock);
663 ts_feed->is_filtering = 1;
664 feed->state = DMX_STATE_GO;
665 spin_unlock_irq(&demux->lock);
666 mutex_unlock(&demux->mutex);
671 static int dmx_ts_feed_stop_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 mutex_lock(&demux->mutex);
679 if (feed->state < DMX_STATE_GO) {
680 mutex_unlock(&demux->mutex);
684 if (!demux->stop_feed) {
685 mutex_unlock(&demux->mutex);
689 ret = demux->stop_feed(feed);
691 spin_lock_irq(&demux->lock);
692 ts_feed->is_filtering = 0;
693 feed->state = DMX_STATE_ALLOCATED;
694 spin_unlock_irq(&demux->lock);
695 mutex_unlock(&demux->mutex);
700 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
701 struct dmx_ts_feed **ts_feed,
704 struct dvb_demux *demux = (struct dvb_demux *)dmx;
705 struct dvb_demux_feed *feed;
707 if (mutex_lock_interruptible(&demux->mutex))
710 if (!(feed = dvb_dmx_feed_alloc(demux))) {
711 mutex_unlock(&demux->mutex);
715 feed->type = DMX_TYPE_TS;
716 feed->cb.ts = callback;
719 feed->peslen = 0xfffa;
722 (*ts_feed) = &feed->feed.ts;
723 (*ts_feed)->parent = dmx;
724 (*ts_feed)->priv = NULL;
725 (*ts_feed)->is_filtering = 0;
726 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
727 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
728 (*ts_feed)->set = dmx_ts_feed_set;
730 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
731 feed->state = DMX_STATE_FREE;
732 mutex_unlock(&demux->mutex);
736 feed->filter->type = DMX_TYPE_TS;
737 feed->filter->feed = feed;
738 feed->filter->state = DMX_STATE_READY;
740 mutex_unlock(&demux->mutex);
745 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
746 struct dmx_ts_feed *ts_feed)
748 struct dvb_demux *demux = (struct dvb_demux *)dmx;
749 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
751 mutex_lock(&demux->mutex);
753 if (feed->state == DMX_STATE_FREE) {
754 mutex_unlock(&demux->mutex);
762 feed->state = DMX_STATE_FREE;
763 feed->filter->state = DMX_STATE_FREE;
765 dvb_demux_feed_del(feed);
769 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
770 demux->pesfilter[feed->pes_type] = NULL;
772 mutex_unlock(&demux->mutex);
776 /******************************************************************************
777 * dmx_section_feed API calls
778 ******************************************************************************/
780 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
781 struct dmx_section_filter **filter)
783 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
784 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
785 struct dvb_demux_filter *dvbdmxfilter;
787 if (mutex_lock_interruptible(&dvbdemux->mutex))
790 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
792 mutex_unlock(&dvbdemux->mutex);
796 spin_lock_irq(&dvbdemux->lock);
797 *filter = &dvbdmxfilter->filter;
798 (*filter)->parent = feed;
799 (*filter)->priv = NULL;
800 dvbdmxfilter->feed = dvbdmxfeed;
801 dvbdmxfilter->type = DMX_TYPE_SEC;
802 dvbdmxfilter->state = DMX_STATE_READY;
803 dvbdmxfilter->next = dvbdmxfeed->filter;
804 dvbdmxfeed->filter = dvbdmxfilter;
805 spin_unlock_irq(&dvbdemux->lock);
807 mutex_unlock(&dvbdemux->mutex);
811 static int dmx_section_feed_set(struct dmx_section_feed *feed,
812 u16 pid, size_t circular_buffer_size,
815 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
816 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
821 if (mutex_lock_interruptible(&dvbdmx->mutex))
824 dvb_demux_feed_add(dvbdmxfeed);
826 dvbdmxfeed->pid = pid;
827 dvbdmxfeed->buffer_size = circular_buffer_size;
828 dvbdmxfeed->feed.sec.check_crc = check_crc;
831 dvbdmxfeed->buffer = NULL;
833 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
834 if (!dvbdmxfeed->buffer) {
835 mutex_unlock(&dvbdmx->mutex);
840 dvbdmxfeed->state = DMX_STATE_READY;
841 mutex_unlock(&dvbdmx->mutex);
845 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
848 struct dvb_demux_filter *f;
849 struct dmx_section_filter *sf;
850 u8 mask, mode, doneq;
852 if (!(f = dvbdmxfeed->filter))
857 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
858 mode = sf->filter_mode[i];
859 mask = sf->filter_mask[i];
860 f->maskandmode[i] = mask & mode;
861 doneq |= f->maskandnotmode[i] = mask & ~mode;
863 f->doneq = doneq ? 1 : 0;
864 } while ((f = f->next));
867 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
869 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
870 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
873 if (mutex_lock_interruptible(&dvbdmx->mutex))
876 if (feed->is_filtering) {
877 mutex_unlock(&dvbdmx->mutex);
881 if (!dvbdmxfeed->filter) {
882 mutex_unlock(&dvbdmx->mutex);
886 dvbdmxfeed->feed.sec.tsfeedp = 0;
887 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
888 dvbdmxfeed->feed.sec.secbufp = 0;
889 dvbdmxfeed->feed.sec.seclen = 0;
891 if (!dvbdmx->start_feed) {
892 mutex_unlock(&dvbdmx->mutex);
896 prepare_secfilters(dvbdmxfeed);
898 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
899 mutex_unlock(&dvbdmx->mutex);
903 spin_lock_irq(&dvbdmx->lock);
904 feed->is_filtering = 1;
905 dvbdmxfeed->state = DMX_STATE_GO;
906 spin_unlock_irq(&dvbdmx->lock);
908 mutex_unlock(&dvbdmx->mutex);
912 static int dmx_section_feed_stop_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 mutex_lock(&dvbdmx->mutex);
920 if (!dvbdmx->stop_feed) {
921 mutex_unlock(&dvbdmx->mutex);
925 ret = dvbdmx->stop_feed(dvbdmxfeed);
927 spin_lock_irq(&dvbdmx->lock);
928 dvbdmxfeed->state = DMX_STATE_READY;
929 feed->is_filtering = 0;
930 spin_unlock_irq(&dvbdmx->lock);
932 mutex_unlock(&dvbdmx->mutex);
936 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
937 struct dmx_section_filter *filter)
939 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
940 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
941 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
943 mutex_lock(&dvbdmx->mutex);
945 if (dvbdmxfilter->feed != dvbdmxfeed) {
946 mutex_unlock(&dvbdmx->mutex);
950 if (feed->is_filtering)
951 feed->stop_filtering(feed);
953 spin_lock_irq(&dvbdmx->lock);
954 f = dvbdmxfeed->filter;
956 if (f == dvbdmxfilter) {
957 dvbdmxfeed->filter = dvbdmxfilter->next;
959 while (f->next != dvbdmxfilter)
961 f->next = f->next->next;
964 dvbdmxfilter->state = DMX_STATE_FREE;
965 spin_unlock_irq(&dvbdmx->lock);
966 mutex_unlock(&dvbdmx->mutex);
970 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
971 struct dmx_section_feed **feed,
972 dmx_section_cb callback)
974 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
975 struct dvb_demux_feed *dvbdmxfeed;
977 if (mutex_lock_interruptible(&dvbdmx->mutex))
980 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
981 mutex_unlock(&dvbdmx->mutex);
985 dvbdmxfeed->type = DMX_TYPE_SEC;
986 dvbdmxfeed->cb.sec = callback;
987 dvbdmxfeed->demux = dvbdmx;
988 dvbdmxfeed->pid = 0xffff;
989 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
990 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
991 dvbdmxfeed->feed.sec.tsfeedp = 0;
992 dvbdmxfeed->filter = NULL;
993 dvbdmxfeed->buffer = NULL;
995 (*feed) = &dvbdmxfeed->feed.sec;
996 (*feed)->is_filtering = 0;
997 (*feed)->parent = demux;
998 (*feed)->priv = NULL;
1000 (*feed)->set = dmx_section_feed_set;
1001 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1002 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1003 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1004 (*feed)->release_filter = dmx_section_feed_release_filter;
1006 mutex_unlock(&dvbdmx->mutex);
1010 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1011 struct dmx_section_feed *feed)
1013 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1014 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1016 mutex_lock(&dvbdmx->mutex);
1018 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1019 mutex_unlock(&dvbdmx->mutex);
1023 vfree(dvbdmxfeed->buffer);
1024 dvbdmxfeed->buffer = NULL;
1026 dvbdmxfeed->state = DMX_STATE_FREE;
1028 dvb_demux_feed_del(dvbdmxfeed);
1030 dvbdmxfeed->pid = 0xffff;
1032 mutex_unlock(&dvbdmx->mutex);
1036 /******************************************************************************
1037 * dvb_demux kernel data API calls
1038 ******************************************************************************/
1040 static int dvbdmx_open(struct dmx_demux *demux)
1042 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1044 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1051 static int dvbdmx_close(struct dmx_demux *demux)
1053 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1055 if (dvbdemux->users == 0)
1059 //FIXME: release any unneeded resources if users==0
1063 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1065 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1068 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1071 p = kmalloc(count, GFP_USER);
1074 if (copy_from_user(p, buf, count)) {
1078 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1080 return -ERESTARTSYS;
1082 dvb_dmx_swfilter(dvbdemux, p, count);
1084 mutex_unlock(&dvbdemux->mutex);
1086 if (signal_pending(current))
1091 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1092 struct dmx_frontend *frontend)
1094 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1095 struct list_head *head = &dvbdemux->frontend_list;
1097 list_add(&(frontend->connectivity_list), head);
1102 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1103 struct dmx_frontend *frontend)
1105 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1106 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1108 list_for_each_safe(pos, n, head) {
1109 if (DMX_FE_ENTRY(pos) == frontend) {
1118 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1120 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1122 if (list_empty(&dvbdemux->frontend_list))
1125 return &dvbdemux->frontend_list;
1128 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1129 struct dmx_frontend *frontend)
1131 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1133 if (demux->frontend)
1136 mutex_lock(&dvbdemux->mutex);
1138 demux->frontend = frontend;
1139 mutex_unlock(&dvbdemux->mutex);
1143 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1145 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1147 mutex_lock(&dvbdemux->mutex);
1149 demux->frontend = NULL;
1150 mutex_unlock(&dvbdemux->mutex);
1154 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1156 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1158 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1162 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1165 struct dmx_demux *dmx = &dvbdemux->dmx;
1167 dvbdemux->users = 0;
1168 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1170 if (!dvbdemux->filter)
1173 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1174 if (!dvbdemux->feed) {
1175 vfree(dvbdemux->filter);
1178 for (i = 0; i < dvbdemux->filternum; i++) {
1179 dvbdemux->filter[i].state = DMX_STATE_FREE;
1180 dvbdemux->filter[i].index = i;
1182 for (i = 0; i < dvbdemux->feednum; i++) {
1183 dvbdemux->feed[i].state = DMX_STATE_FREE;
1184 dvbdemux->feed[i].index = i;
1187 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1189 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1190 dvbdemux->pesfilter[i] = NULL;
1191 dvbdemux->pids[i] = 0xffff;
1194 INIT_LIST_HEAD(&dvbdemux->feed_list);
1196 dvbdemux->playing = 0;
1197 dvbdemux->recording = 0;
1198 dvbdemux->tsbufp = 0;
1200 if (!dvbdemux->check_crc32)
1201 dvbdemux->check_crc32 = dvb_dmx_crc32;
1203 if (!dvbdemux->memcopy)
1204 dvbdemux->memcopy = dvb_dmx_memcopy;
1206 dmx->frontend = NULL;
1207 dmx->priv = dvbdemux;
1208 dmx->open = dvbdmx_open;
1209 dmx->close = dvbdmx_close;
1210 dmx->write = dvbdmx_write;
1211 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1212 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1213 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1214 dmx->release_section_feed = dvbdmx_release_section_feed;
1216 dmx->add_frontend = dvbdmx_add_frontend;
1217 dmx->remove_frontend = dvbdmx_remove_frontend;
1218 dmx->get_frontends = dvbdmx_get_frontends;
1219 dmx->connect_frontend = dvbdmx_connect_frontend;
1220 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1221 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1223 mutex_init(&dvbdemux->mutex);
1224 spin_lock_init(&dvbdemux->lock);
1229 EXPORT_SYMBOL(dvb_dmx_init);
1231 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1233 vfree(dvbdemux->filter);
1234 vfree(dvbdemux->feed);
1237 EXPORT_SYMBOL(dvb_dmx_release);