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,
402 spin_lock(&demux->lock);
406 dvb_dmx_swfilter_packet(demux, buf);
410 spin_unlock(&demux->lock);
413 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
415 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
419 spin_lock(&demux->lock);
425 memcpy(&demux->tsbuf[i], buf, count);
426 demux->tsbufp += count;
429 memcpy(&demux->tsbuf[i], buf, j);
430 if (demux->tsbuf[0] == 0x47)
431 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
437 if (buf[p] == 0x47) {
438 if (count - p >= 188) {
439 dvb_dmx_swfilter_packet(demux, &buf[p]);
443 memcpy(demux->tsbuf, &buf[p], i);
452 spin_unlock(&demux->lock);
455 EXPORT_SYMBOL(dvb_dmx_swfilter);
457 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
462 spin_lock(&demux->lock);
468 memcpy(&demux->tsbuf[i], buf, count);
469 demux->tsbufp += count;
472 memcpy(&demux->tsbuf[i], buf, j);
473 if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
474 memcpy(tmppack, demux->tsbuf, 188);
475 if (tmppack[0] == 0xB8)
477 dvb_dmx_swfilter_packet(demux, tmppack);
484 if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
485 if (count - p >= 204) {
486 memcpy(tmppack, &buf[p], 188);
487 if (tmppack[0] == 0xB8)
489 dvb_dmx_swfilter_packet(demux, tmppack);
493 memcpy(demux->tsbuf, &buf[p], i);
503 spin_unlock(&demux->lock);
506 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
508 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
512 for (i = 0; i < demux->filternum; i++)
513 if (demux->filter[i].state == DMX_STATE_FREE)
516 if (i == demux->filternum)
519 demux->filter[i].state = DMX_STATE_ALLOCATED;
521 return &demux->filter[i];
524 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
528 for (i = 0; i < demux->feednum; i++)
529 if (demux->feed[i].state == DMX_STATE_FREE)
532 if (i == demux->feednum)
535 demux->feed[i].state = DMX_STATE_ALLOCATED;
537 return &demux->feed[i];
540 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
542 struct dvb_demux_feed *entry;
544 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
551 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
553 spin_lock_irq(&feed->demux->lock);
554 if (dvb_demux_feed_find(feed)) {
555 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
556 __func__, feed->type, feed->state, feed->pid);
560 list_add(&feed->list_head, &feed->demux->feed_list);
562 spin_unlock_irq(&feed->demux->lock);
565 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
567 spin_lock_irq(&feed->demux->lock);
568 if (!(dvb_demux_feed_find(feed))) {
569 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
570 __func__, feed->type, feed->state, feed->pid);
574 list_del(&feed->list_head);
576 spin_unlock_irq(&feed->demux->lock);
579 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
580 enum dmx_ts_pes pes_type,
581 size_t circular_buffer_size, struct timespec timeout)
583 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
584 struct dvb_demux *demux = feed->demux;
586 if (pid > DMX_MAX_PID)
589 if (mutex_lock_interruptible(&demux->mutex))
592 if (ts_type & TS_DECODER) {
593 if (pes_type >= DMX_TS_PES_OTHER) {
594 mutex_unlock(&demux->mutex);
598 if (demux->pesfilter[pes_type] &&
599 demux->pesfilter[pes_type] != feed) {
600 mutex_unlock(&demux->mutex);
604 demux->pesfilter[pes_type] = feed;
605 demux->pids[pes_type] = pid;
608 dvb_demux_feed_add(feed);
611 feed->buffer_size = circular_buffer_size;
612 feed->timeout = timeout;
613 feed->ts_type = ts_type;
614 feed->pes_type = pes_type;
616 if (feed->buffer_size) {
620 feed->buffer = vmalloc(feed->buffer_size);
622 mutex_unlock(&demux->mutex);
628 feed->state = DMX_STATE_READY;
629 mutex_unlock(&demux->mutex);
634 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
636 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
637 struct dvb_demux *demux = feed->demux;
640 if (mutex_lock_interruptible(&demux->mutex))
643 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
644 mutex_unlock(&demux->mutex);
648 if (!demux->start_feed) {
649 mutex_unlock(&demux->mutex);
653 if ((ret = demux->start_feed(feed)) < 0) {
654 mutex_unlock(&demux->mutex);
658 spin_lock_irq(&demux->lock);
659 ts_feed->is_filtering = 1;
660 feed->state = DMX_STATE_GO;
661 spin_unlock_irq(&demux->lock);
662 mutex_unlock(&demux->mutex);
667 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
669 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
670 struct dvb_demux *demux = feed->demux;
673 mutex_lock(&demux->mutex);
675 if (feed->state < DMX_STATE_GO) {
676 mutex_unlock(&demux->mutex);
680 if (!demux->stop_feed) {
681 mutex_unlock(&demux->mutex);
685 ret = demux->stop_feed(feed);
687 spin_lock_irq(&demux->lock);
688 ts_feed->is_filtering = 0;
689 feed->state = DMX_STATE_ALLOCATED;
690 spin_unlock_irq(&demux->lock);
691 mutex_unlock(&demux->mutex);
696 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
697 struct dmx_ts_feed **ts_feed,
700 struct dvb_demux *demux = (struct dvb_demux *)dmx;
701 struct dvb_demux_feed *feed;
703 if (mutex_lock_interruptible(&demux->mutex))
706 if (!(feed = dvb_dmx_feed_alloc(demux))) {
707 mutex_unlock(&demux->mutex);
711 feed->type = DMX_TYPE_TS;
712 feed->cb.ts = callback;
715 feed->peslen = 0xfffa;
718 (*ts_feed) = &feed->feed.ts;
719 (*ts_feed)->parent = dmx;
720 (*ts_feed)->priv = NULL;
721 (*ts_feed)->is_filtering = 0;
722 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
723 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
724 (*ts_feed)->set = dmx_ts_feed_set;
726 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
727 feed->state = DMX_STATE_FREE;
728 mutex_unlock(&demux->mutex);
732 feed->filter->type = DMX_TYPE_TS;
733 feed->filter->feed = feed;
734 feed->filter->state = DMX_STATE_READY;
736 mutex_unlock(&demux->mutex);
741 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
742 struct dmx_ts_feed *ts_feed)
744 struct dvb_demux *demux = (struct dvb_demux *)dmx;
745 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
747 mutex_lock(&demux->mutex);
749 if (feed->state == DMX_STATE_FREE) {
750 mutex_unlock(&demux->mutex);
758 feed->state = DMX_STATE_FREE;
759 feed->filter->state = DMX_STATE_FREE;
761 dvb_demux_feed_del(feed);
765 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
766 demux->pesfilter[feed->pes_type] = NULL;
768 mutex_unlock(&demux->mutex);
772 /******************************************************************************
773 * dmx_section_feed API calls
774 ******************************************************************************/
776 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
777 struct dmx_section_filter **filter)
779 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
780 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
781 struct dvb_demux_filter *dvbdmxfilter;
783 if (mutex_lock_interruptible(&dvbdemux->mutex))
786 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
788 mutex_unlock(&dvbdemux->mutex);
792 spin_lock_irq(&dvbdemux->lock);
793 *filter = &dvbdmxfilter->filter;
794 (*filter)->parent = feed;
795 (*filter)->priv = NULL;
796 dvbdmxfilter->feed = dvbdmxfeed;
797 dvbdmxfilter->type = DMX_TYPE_SEC;
798 dvbdmxfilter->state = DMX_STATE_READY;
799 dvbdmxfilter->next = dvbdmxfeed->filter;
800 dvbdmxfeed->filter = dvbdmxfilter;
801 spin_unlock_irq(&dvbdemux->lock);
803 mutex_unlock(&dvbdemux->mutex);
807 static int dmx_section_feed_set(struct dmx_section_feed *feed,
808 u16 pid, size_t circular_buffer_size,
811 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
812 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
817 if (mutex_lock_interruptible(&dvbdmx->mutex))
820 dvb_demux_feed_add(dvbdmxfeed);
822 dvbdmxfeed->pid = pid;
823 dvbdmxfeed->buffer_size = circular_buffer_size;
824 dvbdmxfeed->feed.sec.check_crc = check_crc;
827 dvbdmxfeed->buffer = NULL;
829 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
830 if (!dvbdmxfeed->buffer) {
831 mutex_unlock(&dvbdmx->mutex);
836 dvbdmxfeed->state = DMX_STATE_READY;
837 mutex_unlock(&dvbdmx->mutex);
841 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
844 struct dvb_demux_filter *f;
845 struct dmx_section_filter *sf;
846 u8 mask, mode, doneq;
848 if (!(f = dvbdmxfeed->filter))
853 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
854 mode = sf->filter_mode[i];
855 mask = sf->filter_mask[i];
856 f->maskandmode[i] = mask & mode;
857 doneq |= f->maskandnotmode[i] = mask & ~mode;
859 f->doneq = doneq ? 1 : 0;
860 } while ((f = f->next));
863 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
865 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
866 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
869 if (mutex_lock_interruptible(&dvbdmx->mutex))
872 if (feed->is_filtering) {
873 mutex_unlock(&dvbdmx->mutex);
877 if (!dvbdmxfeed->filter) {
878 mutex_unlock(&dvbdmx->mutex);
882 dvbdmxfeed->feed.sec.tsfeedp = 0;
883 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
884 dvbdmxfeed->feed.sec.secbufp = 0;
885 dvbdmxfeed->feed.sec.seclen = 0;
887 if (!dvbdmx->start_feed) {
888 mutex_unlock(&dvbdmx->mutex);
892 prepare_secfilters(dvbdmxfeed);
894 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
895 mutex_unlock(&dvbdmx->mutex);
899 spin_lock_irq(&dvbdmx->lock);
900 feed->is_filtering = 1;
901 dvbdmxfeed->state = DMX_STATE_GO;
902 spin_unlock_irq(&dvbdmx->lock);
904 mutex_unlock(&dvbdmx->mutex);
908 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
910 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
911 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
914 mutex_lock(&dvbdmx->mutex);
916 if (!dvbdmx->stop_feed) {
917 mutex_unlock(&dvbdmx->mutex);
921 ret = dvbdmx->stop_feed(dvbdmxfeed);
923 spin_lock_irq(&dvbdmx->lock);
924 dvbdmxfeed->state = DMX_STATE_READY;
925 feed->is_filtering = 0;
926 spin_unlock_irq(&dvbdmx->lock);
928 mutex_unlock(&dvbdmx->mutex);
932 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
933 struct dmx_section_filter *filter)
935 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
936 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
937 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
939 mutex_lock(&dvbdmx->mutex);
941 if (dvbdmxfilter->feed != dvbdmxfeed) {
942 mutex_unlock(&dvbdmx->mutex);
946 if (feed->is_filtering)
947 feed->stop_filtering(feed);
949 spin_lock_irq(&dvbdmx->lock);
950 f = dvbdmxfeed->filter;
952 if (f == dvbdmxfilter) {
953 dvbdmxfeed->filter = dvbdmxfilter->next;
955 while (f->next != dvbdmxfilter)
957 f->next = f->next->next;
960 dvbdmxfilter->state = DMX_STATE_FREE;
961 spin_unlock_irq(&dvbdmx->lock);
962 mutex_unlock(&dvbdmx->mutex);
966 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
967 struct dmx_section_feed **feed,
968 dmx_section_cb callback)
970 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
971 struct dvb_demux_feed *dvbdmxfeed;
973 if (mutex_lock_interruptible(&dvbdmx->mutex))
976 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
977 mutex_unlock(&dvbdmx->mutex);
981 dvbdmxfeed->type = DMX_TYPE_SEC;
982 dvbdmxfeed->cb.sec = callback;
983 dvbdmxfeed->demux = dvbdmx;
984 dvbdmxfeed->pid = 0xffff;
985 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
986 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
987 dvbdmxfeed->feed.sec.tsfeedp = 0;
988 dvbdmxfeed->filter = NULL;
989 dvbdmxfeed->buffer = NULL;
991 (*feed) = &dvbdmxfeed->feed.sec;
992 (*feed)->is_filtering = 0;
993 (*feed)->parent = demux;
994 (*feed)->priv = NULL;
996 (*feed)->set = dmx_section_feed_set;
997 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
998 (*feed)->start_filtering = dmx_section_feed_start_filtering;
999 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1000 (*feed)->release_filter = dmx_section_feed_release_filter;
1002 mutex_unlock(&dvbdmx->mutex);
1006 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1007 struct dmx_section_feed *feed)
1009 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1010 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1012 mutex_lock(&dvbdmx->mutex);
1014 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1015 mutex_unlock(&dvbdmx->mutex);
1019 vfree(dvbdmxfeed->buffer);
1020 dvbdmxfeed->buffer = NULL;
1022 dvbdmxfeed->state = DMX_STATE_FREE;
1024 dvb_demux_feed_del(dvbdmxfeed);
1026 dvbdmxfeed->pid = 0xffff;
1028 mutex_unlock(&dvbdmx->mutex);
1032 /******************************************************************************
1033 * dvb_demux kernel data API calls
1034 ******************************************************************************/
1036 static int dvbdmx_open(struct dmx_demux *demux)
1038 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1040 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1047 static int dvbdmx_close(struct dmx_demux *demux)
1049 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1051 if (dvbdemux->users == 0)
1055 //FIXME: release any unneeded resources if users==0
1059 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1061 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1064 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1067 p = kmalloc(count, GFP_USER);
1070 if (copy_from_user(p, buf, count)) {
1074 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1076 return -ERESTARTSYS;
1078 dvb_dmx_swfilter(dvbdemux, p, count);
1080 mutex_unlock(&dvbdemux->mutex);
1082 if (signal_pending(current))
1087 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1088 struct dmx_frontend *frontend)
1090 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1091 struct list_head *head = &dvbdemux->frontend_list;
1093 list_add(&(frontend->connectivity_list), head);
1098 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1099 struct dmx_frontend *frontend)
1101 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1102 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1104 list_for_each_safe(pos, n, head) {
1105 if (DMX_FE_ENTRY(pos) == frontend) {
1114 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1116 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1118 if (list_empty(&dvbdemux->frontend_list))
1121 return &dvbdemux->frontend_list;
1124 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1125 struct dmx_frontend *frontend)
1127 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1129 if (demux->frontend)
1132 mutex_lock(&dvbdemux->mutex);
1134 demux->frontend = frontend;
1135 mutex_unlock(&dvbdemux->mutex);
1139 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1141 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1143 mutex_lock(&dvbdemux->mutex);
1145 demux->frontend = NULL;
1146 mutex_unlock(&dvbdemux->mutex);
1150 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1152 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1154 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1158 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1161 struct dmx_demux *dmx = &dvbdemux->dmx;
1163 dvbdemux->users = 0;
1164 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1166 if (!dvbdemux->filter)
1169 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1170 if (!dvbdemux->feed) {
1171 vfree(dvbdemux->filter);
1174 for (i = 0; i < dvbdemux->filternum; i++) {
1175 dvbdemux->filter[i].state = DMX_STATE_FREE;
1176 dvbdemux->filter[i].index = i;
1178 for (i = 0; i < dvbdemux->feednum; i++) {
1179 dvbdemux->feed[i].state = DMX_STATE_FREE;
1180 dvbdemux->feed[i].index = i;
1183 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1185 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1186 dvbdemux->pesfilter[i] = NULL;
1187 dvbdemux->pids[i] = 0xffff;
1190 INIT_LIST_HEAD(&dvbdemux->feed_list);
1192 dvbdemux->playing = 0;
1193 dvbdemux->recording = 0;
1194 dvbdemux->tsbufp = 0;
1196 if (!dvbdemux->check_crc32)
1197 dvbdemux->check_crc32 = dvb_dmx_crc32;
1199 if (!dvbdemux->memcopy)
1200 dvbdemux->memcopy = dvb_dmx_memcopy;
1202 dmx->frontend = NULL;
1203 dmx->priv = dvbdemux;
1204 dmx->open = dvbdmx_open;
1205 dmx->close = dvbdmx_close;
1206 dmx->write = dvbdmx_write;
1207 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1208 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1209 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1210 dmx->release_section_feed = dvbdmx_release_section_feed;
1212 dmx->add_frontend = dvbdmx_add_frontend;
1213 dmx->remove_frontend = dvbdmx_remove_frontend;
1214 dmx->get_frontends = dvbdmx_get_frontends;
1215 dmx->connect_frontend = dvbdmx_connect_frontend;
1216 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1217 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1219 mutex_init(&dvbdemux->mutex);
1220 spin_lock_init(&dvbdemux->lock);
1225 EXPORT_SYMBOL(dvb_dmx_init);
1227 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1229 vfree(dvbdemux->filter);
1230 vfree(dvbdemux->feed);
1233 EXPORT_SYMBOL(dvb_dmx_release);