Linux-2.6.12-rc2
[linux-2.6/next.git] / drivers / media / dvb / dvb-core / dvb_demux.c
blobac9889d222882bc6629d5a2c90b7405496d8f740
1 /*
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"
35 #define NOBUFS
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)
47 demux->users = 0;
48 list_add(&demux->reg_list, &dmx_muxs);
49 return 0;
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) {
58 if (demux->users>0)
59 return -EINVAL;
60 list_del(pos);
61 return 0;
65 return -ENODEV;
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?
89 return 0;
90 if (tsp[3] & 0x20) { // adaptation field?
91 if (tsp[4] > 183) // corrupted data?
92 return 0;
93 else
94 return 184-1-tsp[4];
96 return 184;
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)
108 memcpy (d, s, 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);
119 int p;
120 //int ccok;
121 //u8 cc;
123 if (count == 0)
124 return -1;
126 p = 188-count;
129 cc=buf[3]&0x0f;
130 ccok=((dvbdmxfeed->cc+1)&0x0f)==cc ? 1 : 0;
131 dvbdmxfeed->cc=cc;
132 if (!ccok)
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)
148 u8 neq = 0;
149 int i;
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)
155 return 0;
157 neq |= f->maskandnotmode[i] & xor;
160 if (f->doneq && !neq)
161 return 0;
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)
176 return 0;
178 if (!f)
179 return 0;
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))
185 return -1;
188 do {
189 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
190 return -1;
191 } while ((f = f->next) && sec->is_filtering);
193 sec->seclen = 0;
195 return 0;
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",
215 n, sec->tsfeedp);
216 printk("dvb_demux.c pad data:");
217 for(i = 0; i < n; i++)
218 printk(" %02x", sec->secbuf[i]);
219 printk("\n");
222 #endif
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.
240 ** Fix:
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)
253 return 0;
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);
260 #endif
261 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
264 if(len <= 0)
265 return 0;
267 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
268 sec->tsfeedp += 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)
286 return 0;
287 sec->seclen = seclen;
288 sec->crc_val = ~0;
289 /* dump [secbuf .. secbuf+seclen) */
290 if(feed->pusi_seen)
291 dvb_dmx_swfilter_section_feed(feed);
292 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
293 else
294 printk("dvb_demux.c pusi not seen, discarding section data\n");
295 #endif
296 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
297 sec->secbuf += seclen; /* redundand but saves pointer arithmetic */
300 return 0;
304 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed, const u8 *buf)
306 u8 p, count;
307 int ccok, dc_i = 0;
308 u8 cc;
310 count = payload(buf);
312 if (count == 0) /* count == 0 if no payload or out of range */
313 return -1;
315 p = 188 - count; /* payload start */
317 cc = buf[3] & 0x0f;
318 ccok = ((feed->cc + 1) & 0x0f) == cc;
319 feed->cc = cc;
321 if (buf[3] & 0x20) {
322 /* adaption field present, check for discontinuity_indicator */
323 if ((buf[4] > 0) && (buf[5] & 0x80))
324 dc_i = 1;
327 if (!ccok || dc_i) {
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
333 #endif
334 /* Discontinuity detected. Reset pusi_seen = 0 to
335 ** stop feeding of suspicious data until next PUSI=1 arrives
337 feed->pusi_seen = 0;
338 dvb_dmx_swfilter_section_new(feed);
339 return 0;
342 if (buf[1] & 0x40) {
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 */
352 feed->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
357 else
358 if (count > 0)
359 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
360 #endif
361 } else {
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);
368 return 0;
372 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed, const u8 *buf)
374 switch(feed->type) {
375 case DMX_TYPE_TS:
376 if (!feed->feed.ts.is_filtering)
377 break;
378 if (feed->ts_type & TS_PACKET) {
379 if (feed->ts_type & TS_PAYLOAD_ONLY)
380 dvb_dmx_swfilter_payload(feed, buf);
381 else
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);
387 break;
389 case DMX_TYPE_SEC:
390 if (!feed->feed.sec.is_filtering)
391 break;
392 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
393 feed->feed.sec.seclen = feed->feed.sec.secbufp=0;
394 break;
396 default:
397 break;
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);
411 int dvr_done = 0;
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))
417 continue;
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++))
422 continue;
424 if (feed->pid == pid) {
425 dvb_dmx_swfilter_packet_type(feed, buf);
426 if (DVR_FEED(feed))
427 continue;
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);
439 while (count--) {
440 if(buf[0] == 0x47) {
441 dvb_dmx_swfilter_packet(demux, buf);
443 buf += 188;
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)
453 int p = 0, i, j;
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;
461 goto bailout;
463 memcpy(&demux->tsbuf[i], buf, j);
464 if (demux->tsbuf[0] == 0x47)
465 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
466 demux->tsbufp = 0;
467 p += j;
470 while (p < count) {
471 if (buf[p] == 0x47) {
472 if (count-p >= 188) {
473 dvb_dmx_swfilter_packet(demux, buf+p);
474 p += 188;
475 } else {
476 i = count-p;
477 memcpy(demux->tsbuf, buf+p, i);
478 demux->tsbufp=i;
479 goto bailout;
481 } else
482 p++;
485 bailout:
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)
492 int p = 0,i, j;
493 u8 tmppack[188];
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;
500 goto bailout;
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);
508 demux->tsbufp = 0;
509 p += j;
512 while (p < count) {
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);
518 p += 204;
519 } else {
520 i = count-p;
521 memcpy(demux->tsbuf, buf+p, i);
522 demux->tsbufp=i;
523 goto bailout;
525 } else {
526 p++;
530 bailout:
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)
538 int i;
540 for (i=0; i<demux->filternum; i++)
541 if (demux->filter[i].state == DMX_STATE_FREE)
542 break;
544 if (i == demux->filternum)
545 return NULL;
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)
554 int i;
556 for (i=0; i<demux->feednum; i++)
557 if (demux->feed[i].state == DMX_STATE_FREE)
558 break;
560 if (i == demux->feednum)
561 return NULL;
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)
573 if (entry == feed)
574 return 1;
576 return 0;
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);
585 goto out;
588 list_add(&feed->list_head, &feed->demux->feed_list);
589 out:
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);
599 goto out;
602 list_del(&feed->list_head);
603 out:
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)
616 return -EINVAL;
618 if (down_interruptible (&demux->mutex))
619 return -ERESTARTSYS;
621 if (ts_type & TS_DECODER) {
622 if (pes_type >= DMX_TS_PES_OTHER) {
623 up(&demux->mutex);
624 return -EINVAL;
627 if (demux->pesfilter[pes_type] &&
628 demux->pesfilter[pes_type] != feed) {
629 up(&demux->mutex);
630 return -EINVAL;
633 demux->pesfilter[pes_type] = feed;
634 demux->pids[pes_type] = pid;
637 dvb_demux_feed_add(feed);
639 feed->pid = pid;
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) {
648 up(&demux->mutex);
649 return -ENOSYS;
652 if (feed->buffer_size) {
653 #ifdef NOBUFS
654 feed->buffer=NULL;
655 #else
656 feed->buffer = vmalloc(feed->buffer_size);
657 if (!feed->buffer) {
658 up(&demux->mutex);
659 return -ENOMEM;
661 #endif
664 feed->state = DMX_STATE_READY;
665 up(&demux->mutex);
667 return 0;
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;
675 int ret;
677 if (down_interruptible (&demux->mutex))
678 return -ERESTARTSYS;
680 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
681 up(&demux->mutex);
682 return -EINVAL;
685 if (!demux->start_feed) {
686 up(&demux->mutex);
687 return -ENODEV;
690 if ((ret = demux->start_feed(feed)) < 0) {
691 up(&demux->mutex);
692 return ret;
695 spin_lock_irq(&demux->lock);
696 ts_feed->is_filtering = 1;
697 feed->state = DMX_STATE_GO;
698 spin_unlock_irq(&demux->lock);
699 up(&demux->mutex);
701 return 0;
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;
708 int ret;
710 if (down_interruptible (&demux->mutex))
711 return -ERESTARTSYS;
713 if (feed->state < DMX_STATE_GO) {
714 up(&demux->mutex);
715 return -EINVAL;
718 if (!demux->stop_feed) {
719 up(&demux->mutex);
720 return -ENODEV;
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);
729 up(&demux->mutex);
731 return ret;
734 static int dvbdmx_allocate_ts_feed (struct dmx_demux *dmx, struct dmx_ts_feed **ts_feed,
735 dmx_ts_cb callback)
737 struct dvb_demux *demux = (struct dvb_demux *) dmx;
738 struct dvb_demux_feed *feed;
740 if (down_interruptible (&demux->mutex))
741 return -ERESTARTSYS;
743 if (!(feed = dvb_dmx_feed_alloc(demux))) {
744 up(&demux->mutex);
745 return -EBUSY;
748 feed->type = DMX_TYPE_TS;
749 feed->cb.ts = callback;
750 feed->demux = demux;
751 feed->pid = 0xffff;
752 feed->peslen = 0xfffa;
753 feed->buffer = NULL;
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;
766 up(&demux->mutex);
767 return -EBUSY;
770 feed->filter->type = DMX_TYPE_TS;
771 feed->filter->feed = feed;
772 feed->filter->state = DMX_STATE_READY;
774 up(&demux->mutex);
776 return 0;
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))
785 return -ERESTARTSYS;
787 if (feed->state == DMX_STATE_FREE) {
788 up(&demux->mutex);
789 return -EINVAL;
792 #ifndef NOBUFS
793 vfree(feed->buffer);
794 feed->buffer=0;
795 #endif
797 feed->state = DMX_STATE_FREE;
798 feed->filter->state = DMX_STATE_FREE;
800 dvb_demux_feed_del(feed);
802 feed->pid = 0xffff;
804 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
805 demux->pesfilter[feed->pes_type] = NULL;
807 up(&demux->mutex);
808 return 0;
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))
824 return -ERESTARTSYS;
826 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
827 if (!dvbdmxfilter) {
828 up(&dvbdemux->mutex);
829 return -EBUSY;
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);
844 return 0;
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;
855 if (pid > 0x1fff)
856 return -EINVAL;
858 if (down_interruptible (&dvbdmx->mutex))
859 return -ERESTARTSYS;
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) {
867 up(&dvbdmx->mutex);
868 return -ENOSYS;
871 dvbdmxfeed->feed.sec.check_crc = check_crc;
873 #ifdef NOBUFS
874 dvbdmxfeed->buffer = NULL;
875 #else
876 dvbdmxfeed->buffer=vmalloc(dvbdmxfeed->buffer_size);
877 if (!dvbdmxfeed->buffer) {
878 up(&dvbdmx->mutex);
879 return -ENOMEM;
881 #endif
883 dvbdmxfeed->state = DMX_STATE_READY;
884 up(&dvbdmx->mutex);
885 return 0;
889 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
891 int i;
892 struct dvb_demux_filter *f;
893 struct dmx_section_filter *sf;
894 u8 mask, mode, doneq;
896 if (!(f=dvbdmxfeed->filter))
897 return;
898 do {
899 sf = &f->filter;
900 doneq = 0;
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;
916 int ret;
918 if (down_interruptible (&dvbdmx->mutex))
919 return -ERESTARTSYS;
921 if (feed->is_filtering) {
922 up(&dvbdmx->mutex);
923 return -EBUSY;
926 if (!dvbdmxfeed->filter) {
927 up(&dvbdmx->mutex);
928 return -EINVAL;
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) {
937 up(&dvbdmx->mutex);
938 return -ENODEV;
941 prepare_secfilters(dvbdmxfeed);
943 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
944 up(&dvbdmx->mutex);
945 return ret;
948 spin_lock_irq(&dvbdmx->lock);
949 feed->is_filtering = 1;
950 dvbdmxfeed->state = DMX_STATE_GO;
951 spin_unlock_irq(&dvbdmx->lock);
953 up(&dvbdmx->mutex);
954 return 0;
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;
962 int ret;
964 if (down_interruptible (&dvbdmx->mutex))
965 return -ERESTARTSYS;
967 if (!dvbdmx->stop_feed) {
968 up(&dvbdmx->mutex);
969 return -ENODEV;
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);
979 up(&dvbdmx->mutex);
980 return ret;
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))
992 return -ERESTARTSYS;
994 if (dvbdmxfilter->feed != dvbdmxfeed) {
995 up(&dvbdmx->mutex);
996 return -EINVAL;
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;
1007 } else {
1008 while(f->next != dvbdmxfilter)
1009 f = f->next;
1010 f->next = f->next->next;
1013 dvbdmxfilter->state = DMX_STATE_FREE;
1014 spin_unlock_irq(&dvbdmx->lock);
1015 up(&dvbdmx->mutex);
1016 return 0;
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))) {
1030 up(&dvbdmx->mutex);
1031 return -EBUSY;
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;
1055 up(&dvbdmx->mutex);
1056 return 0;
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) {
1069 up(&dvbdmx->mutex);
1070 return -EINVAL;
1072 #ifndef NOBUFS
1073 vfree(dvbdmxfeed->buffer);
1074 dvbdmxfeed->buffer=0;
1075 #endif
1076 dvbdmxfeed->state=DMX_STATE_FREE;
1078 dvb_demux_feed_del(dvbdmxfeed);
1080 dvbdmxfeed->pid = 0xffff;
1082 up(&dvbdmx->mutex);
1083 return 0;
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)
1096 return -EUSERS;
1098 dvbdemux->users++;
1099 return 0;
1103 static int dvbdmx_close(struct dmx_demux *demux)
1105 struct dvb_demux *dvbdemux = (struct dvb_demux *) demux;
1107 if (dvbdemux->users == 0)
1108 return -ENODEV;
1110 dvbdemux->users--;
1111 //FIXME: release any unneeded resources if users==0
1112 return 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))
1121 return -EINVAL;
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))
1129 return -EINTR;
1130 return count;
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);
1141 return 0;
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) {
1152 list_del(pos);
1153 return 0;
1157 return -ENODEV;
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))
1166 return NULL;
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)
1176 return -EINVAL;
1178 if (down_interruptible (&dvbdemux->mutex))
1179 return -ERESTARTSYS;
1181 demux->frontend = frontend;
1182 up(&dvbdemux->mutex);
1183 return 0;
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);
1196 return 0;
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));
1205 return 0;
1209 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1211 int i, err;
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)
1218 return -ENOMEM;
1220 dvbdemux->feed = vmalloc(dvbdemux->feednum*sizeof(struct dvb_demux_feed));
1221 if (!dvbdemux->feed) {
1222 vfree(dvbdemux->filter);
1223 return -ENOMEM;
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)
1278 return err;
1280 return 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);
1292 return 0;
1294 EXPORT_SYMBOL(dvb_dmx_release);