V4L/DVB (6715): ivtv: Remove unnecessary register update
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dvb_demux.c
blob7959020f9317d2ee5f1f0824370ab730542f09c3
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
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?
58 return 0;
60 if (tsp[3] & 0x20) { // adaptation field?
61 if (tsp[4] > 183) // corrupted data?
62 return 0;
63 else
64 return 184 - 1 - tsp[4];
67 return 184;
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,
76 size_t len)
78 memcpy(d, s, len);
81 /******************************************************************************
82 * Software filter functions
83 ******************************************************************************/
85 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
86 const u8 *buf)
88 int count = payload(buf);
89 int p;
90 //int ccok;
91 //u8 cc;
93 if (count == 0)
94 return -1;
96 p = 188 - count;
99 cc = buf[3] & 0x0f;
100 ccok = ((feed->cc + 1) & 0x0f) == cc;
101 feed->cc = cc;
102 if (!ccok)
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)
117 u8 neq = 0;
118 int i;
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)
124 return 0;
126 neq |= f->maskandnotmode[i] & xor;
129 if (f->doneq && !neq)
130 return 0;
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)
144 return 0;
146 if (!f)
147 return 0;
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))
153 return -1;
156 do {
157 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
158 return -1;
159 } while ((f = f->next) && sec->is_filtering);
161 sec->seclen = 0;
163 return 0;
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",
181 n, sec->tsfeedp);
182 printk("dvb_demux.c pad data:");
183 for (i = 0; i < n; i++)
184 printk(" %02x", sec->secbuf[i]);
185 printk("\n");
188 #endif
190 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
191 sec->secbuf = sec->secbuf_base;
195 * Losless Section Demux 1.4.1 by Emard
196 * Valsecchi Patrick:
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.
206 * Fix:
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)
220 return 0;
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);
227 #endif
228 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
231 if (len <= 0)
232 return 0;
234 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
235 sec->tsfeedp += 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)
251 return 0;
252 sec->seclen = seclen;
253 sec->crc_val = ~0;
254 /* dump [secbuf .. secbuf+seclen) */
255 if (feed->pusi_seen)
256 dvb_dmx_swfilter_section_feed(feed);
257 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
258 else
259 printk("dvb_demux.c pusi not seen, discarding section data\n");
260 #endif
261 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
262 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
265 return 0;
268 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
269 const u8 *buf)
271 u8 p, count;
272 int ccok, dc_i = 0;
273 u8 cc;
275 count = payload(buf);
277 if (count == 0) /* count == 0 if no payload or out of range */
278 return -1;
280 p = 188 - count; /* payload start */
282 cc = buf[3] & 0x0f;
283 ccok = ((feed->cc + 1) & 0x0f) == cc;
284 feed->cc = cc;
286 if (buf[3] & 0x20) {
287 /* adaption field present, check for discontinuity_indicator */
288 if ((buf[4] > 0) && (buf[5] & 0x80))
289 dc_i = 1;
292 if (!ccok || dc_i) {
293 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
294 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
295 count);
297 * those bytes under sume circumstances will again be reported
298 * in the following dvb_dmx_swfilter_section_new
300 #endif
302 * Discontinuity detected. Reset pusi_seen = 0 to
303 * stop feeding of suspicious data until next PUSI=1 arrives
305 feed->pusi_seen = 0;
306 dvb_dmx_swfilter_section_new(feed);
309 if (buf[1] & 0x40) {
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,
318 before_len);
319 /* before start of new section, set pusi_seen = 1 */
320 feed->pusi_seen = 1;
321 dvb_dmx_swfilter_section_new(feed);
322 dvb_dmx_swfilter_section_copy_dump(feed, after,
323 after_len);
325 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
326 else if (count > 0)
327 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
328 #endif
329 } else {
330 /* PUSI=0 (is not set), no section boundary */
331 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
334 return 0;
337 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
338 const u8 *buf)
340 switch (feed->type) {
341 case DMX_TYPE_TS:
342 if (!feed->feed.ts.is_filtering)
343 break;
344 if (feed->ts_type & TS_PACKET) {
345 if (feed->ts_type & TS_PAYLOAD_ONLY)
346 dvb_dmx_swfilter_payload(feed, buf);
347 else
348 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
349 DMX_OK);
351 if (feed->ts_type & TS_DECODER)
352 if (feed->demux->write_to_decoder)
353 feed->demux->write_to_decoder(feed, buf, 188);
354 break;
356 case DMX_TYPE_SEC:
357 if (!feed->feed.sec.is_filtering)
358 break;
359 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
360 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
361 break;
363 default:
364 break;
368 #define DVR_FEED(f) \
369 (((f)->type == DMX_TYPE_TS) && \
370 ((f)->feed.ts.is_filtering) && \
371 (((f)->ts_type & (TS_PACKET|TS_PAYLOAD_ONLY)) == 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);
377 int dvr_done = 0;
379 list_for_each_entry(feed, &demux->feed_list, list_head) {
380 if ((feed->pid != pid) && (feed->pid != 0x2000))
381 continue;
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++))
386 continue;
388 if (feed->pid == pid) {
389 dvb_dmx_swfilter_packet_type(feed, buf);
390 if (DVR_FEED(feed))
391 continue;
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,
400 size_t count)
402 spin_lock(&demux->lock);
404 while (count--) {
405 if (buf[0] == 0x47)
406 dvb_dmx_swfilter_packet(demux, buf);
407 buf += 188;
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)
417 int p = 0, i, j;
419 spin_lock(&demux->lock);
421 if (demux->tsbufp) {
422 i = demux->tsbufp;
423 j = 188 - i;
424 if (count < j) {
425 memcpy(&demux->tsbuf[i], buf, count);
426 demux->tsbufp += count;
427 goto bailout;
429 memcpy(&demux->tsbuf[i], buf, j);
430 if (demux->tsbuf[0] == 0x47)
431 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
432 demux->tsbufp = 0;
433 p += j;
436 while (p < count) {
437 if (buf[p] == 0x47) {
438 if (count - p >= 188) {
439 dvb_dmx_swfilter_packet(demux, &buf[p]);
440 p += 188;
441 } else {
442 i = count - p;
443 memcpy(demux->tsbuf, &buf[p], i);
444 demux->tsbufp = i;
445 goto bailout;
447 } else
448 p++;
451 bailout:
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)
459 int p = 0, i, j;
460 u8 tmppack[188];
462 spin_lock(&demux->lock);
464 if (demux->tsbufp) {
465 i = demux->tsbufp;
466 j = 204 - i;
467 if (count < j) {
468 memcpy(&demux->tsbuf[i], buf, count);
469 demux->tsbufp += count;
470 goto bailout;
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)
476 tmppack[0] = 0x47;
477 dvb_dmx_swfilter_packet(demux, tmppack);
479 demux->tsbufp = 0;
480 p += j;
483 while (p < count) {
484 if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
485 if (count - p >= 204) {
486 memcpy(tmppack, &buf[p], 188);
487 if (tmppack[0] == 0xB8)
488 tmppack[0] = 0x47;
489 dvb_dmx_swfilter_packet(demux, tmppack);
490 p += 204;
491 } else {
492 i = count - p;
493 memcpy(demux->tsbuf, &buf[p], i);
494 demux->tsbufp = i;
495 goto bailout;
497 } else {
498 p++;
502 bailout:
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)
510 int i;
512 for (i = 0; i < demux->filternum; i++)
513 if (demux->filter[i].state == DMX_STATE_FREE)
514 break;
516 if (i == demux->filternum)
517 return NULL;
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)
526 int i;
528 for (i = 0; i < demux->feednum; i++)
529 if (demux->feed[i].state == DMX_STATE_FREE)
530 break;
532 if (i == demux->feednum)
533 return NULL;
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)
545 if (entry == feed)
546 return 1;
548 return 0;
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 __FUNCTION__, feed->type, feed->state, feed->pid);
557 goto out;
560 list_add(&feed->list_head, &feed->demux->feed_list);
561 out:
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 __FUNCTION__, feed->type, feed->state, feed->pid);
571 goto out;
574 list_del(&feed->list_head);
575 out:
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)
587 return -EINVAL;
589 if (mutex_lock_interruptible(&demux->mutex))
590 return -ERESTARTSYS;
592 if (ts_type & TS_DECODER) {
593 if (pes_type >= DMX_TS_PES_OTHER) {
594 mutex_unlock(&demux->mutex);
595 return -EINVAL;
598 if (demux->pesfilter[pes_type] &&
599 demux->pesfilter[pes_type] != feed) {
600 mutex_unlock(&demux->mutex);
601 return -EINVAL;
604 demux->pesfilter[pes_type] = feed;
605 demux->pids[pes_type] = pid;
608 dvb_demux_feed_add(feed);
610 feed->pid = pid;
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) {
617 #ifdef NOBUFS
618 feed->buffer = NULL;
619 #else
620 feed->buffer = vmalloc(feed->buffer_size);
621 if (!feed->buffer) {
622 mutex_unlock(&demux->mutex);
623 return -ENOMEM;
625 #endif
628 feed->state = DMX_STATE_READY;
629 mutex_unlock(&demux->mutex);
631 return 0;
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;
638 int ret;
640 if (mutex_lock_interruptible(&demux->mutex))
641 return -ERESTARTSYS;
643 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
644 mutex_unlock(&demux->mutex);
645 return -EINVAL;
648 if (!demux->start_feed) {
649 mutex_unlock(&demux->mutex);
650 return -ENODEV;
653 if ((ret = demux->start_feed(feed)) < 0) {
654 mutex_unlock(&demux->mutex);
655 return ret;
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);
664 return 0;
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;
671 int ret;
673 mutex_lock(&demux->mutex);
675 if (feed->state < DMX_STATE_GO) {
676 mutex_unlock(&demux->mutex);
677 return -EINVAL;
680 if (!demux->stop_feed) {
681 mutex_unlock(&demux->mutex);
682 return -ENODEV;
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);
693 return ret;
696 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
697 struct dmx_ts_feed **ts_feed,
698 dmx_ts_cb callback)
700 struct dvb_demux *demux = (struct dvb_demux *)dmx;
701 struct dvb_demux_feed *feed;
703 if (mutex_lock_interruptible(&demux->mutex))
704 return -ERESTARTSYS;
706 if (!(feed = dvb_dmx_feed_alloc(demux))) {
707 mutex_unlock(&demux->mutex);
708 return -EBUSY;
711 feed->type = DMX_TYPE_TS;
712 feed->cb.ts = callback;
713 feed->demux = demux;
714 feed->pid = 0xffff;
715 feed->peslen = 0xfffa;
716 feed->buffer = NULL;
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);
729 return -EBUSY;
732 feed->filter->type = DMX_TYPE_TS;
733 feed->filter->feed = feed;
734 feed->filter->state = DMX_STATE_READY;
736 mutex_unlock(&demux->mutex);
738 return 0;
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);
751 return -EINVAL;
753 #ifndef NOBUFS
754 vfree(feed->buffer);
755 feed->buffer = NULL;
756 #endif
758 feed->state = DMX_STATE_FREE;
759 feed->filter->state = DMX_STATE_FREE;
761 dvb_demux_feed_del(feed);
763 feed->pid = 0xffff;
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);
769 return 0;
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))
784 return -ERESTARTSYS;
786 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
787 if (!dvbdmxfilter) {
788 mutex_unlock(&dvbdemux->mutex);
789 return -EBUSY;
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);
804 return 0;
807 static int dmx_section_feed_set(struct dmx_section_feed *feed,
808 u16 pid, size_t circular_buffer_size,
809 int check_crc)
811 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
812 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
814 if (pid > 0x1fff)
815 return -EINVAL;
817 if (mutex_lock_interruptible(&dvbdmx->mutex))
818 return -ERESTARTSYS;
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;
826 #ifdef NOBUFS
827 dvbdmxfeed->buffer = NULL;
828 #else
829 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
830 if (!dvbdmxfeed->buffer) {
831 mutex_unlock(&dvbdmx->mutex);
832 return -ENOMEM;
834 #endif
836 dvbdmxfeed->state = DMX_STATE_READY;
837 mutex_unlock(&dvbdmx->mutex);
838 return 0;
841 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
843 int i;
844 struct dvb_demux_filter *f;
845 struct dmx_section_filter *sf;
846 u8 mask, mode, doneq;
848 if (!(f = dvbdmxfeed->filter))
849 return;
850 do {
851 sf = &f->filter;
852 doneq = 0;
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;
867 int ret;
869 if (mutex_lock_interruptible(&dvbdmx->mutex))
870 return -ERESTARTSYS;
872 if (feed->is_filtering) {
873 mutex_unlock(&dvbdmx->mutex);
874 return -EBUSY;
877 if (!dvbdmxfeed->filter) {
878 mutex_unlock(&dvbdmx->mutex);
879 return -EINVAL;
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);
889 return -ENODEV;
892 prepare_secfilters(dvbdmxfeed);
894 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
895 mutex_unlock(&dvbdmx->mutex);
896 return ret;
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);
905 return 0;
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;
912 int ret;
914 mutex_lock(&dvbdmx->mutex);
916 if (!dvbdmx->stop_feed) {
917 mutex_unlock(&dvbdmx->mutex);
918 return -ENODEV;
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);
929 return ret;
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);
943 return -EINVAL;
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;
954 } else {
955 while (f->next != dvbdmxfilter)
956 f = f->next;
957 f->next = f->next->next;
960 dvbdmxfilter->state = DMX_STATE_FREE;
961 spin_unlock_irq(&dvbdmx->lock);
962 mutex_unlock(&dvbdmx->mutex);
963 return 0;
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))
974 return -ERESTARTSYS;
976 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
977 mutex_unlock(&dvbdmx->mutex);
978 return -EBUSY;
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);
1003 return 0;
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);
1016 return -EINVAL;
1018 #ifndef NOBUFS
1019 vfree(dvbdmxfeed->buffer);
1020 dvbdmxfeed->buffer = NULL;
1021 #endif
1022 dvbdmxfeed->state = DMX_STATE_FREE;
1024 dvb_demux_feed_del(dvbdmxfeed);
1026 dvbdmxfeed->pid = 0xffff;
1028 mutex_unlock(&dvbdmx->mutex);
1029 return 0;
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)
1041 return -EUSERS;
1043 dvbdemux->users++;
1044 return 0;
1047 static int dvbdmx_close(struct dmx_demux *demux)
1049 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1051 if (dvbdemux->users == 0)
1052 return -ENODEV;
1054 dvbdemux->users--;
1055 //FIXME: release any unneeded resources if users==0
1056 return 0;
1059 static int dvbdmx_write(struct dmx_demux *demux, const char *buf, size_t count)
1061 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1063 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1064 return -EINVAL;
1066 if (mutex_lock_interruptible(&dvbdemux->mutex))
1067 return -ERESTARTSYS;
1068 dvb_dmx_swfilter(dvbdemux, (u8 *)buf, count);
1069 mutex_unlock(&dvbdemux->mutex);
1071 if (signal_pending(current))
1072 return -EINTR;
1073 return count;
1076 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1077 struct dmx_frontend *frontend)
1079 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1080 struct list_head *head = &dvbdemux->frontend_list;
1082 list_add(&(frontend->connectivity_list), head);
1084 return 0;
1087 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1088 struct dmx_frontend *frontend)
1090 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1091 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1093 list_for_each_safe(pos, n, head) {
1094 if (DMX_FE_ENTRY(pos) == frontend) {
1095 list_del(pos);
1096 return 0;
1100 return -ENODEV;
1103 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1105 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1107 if (list_empty(&dvbdemux->frontend_list))
1108 return NULL;
1110 return &dvbdemux->frontend_list;
1113 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1114 struct dmx_frontend *frontend)
1116 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1118 if (demux->frontend)
1119 return -EINVAL;
1121 mutex_lock(&dvbdemux->mutex);
1123 demux->frontend = frontend;
1124 mutex_unlock(&dvbdemux->mutex);
1125 return 0;
1128 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1130 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1132 mutex_lock(&dvbdemux->mutex);
1134 demux->frontend = NULL;
1135 mutex_unlock(&dvbdemux->mutex);
1136 return 0;
1139 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1141 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1143 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1144 return 0;
1147 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1149 int i;
1150 struct dmx_demux *dmx = &dvbdemux->dmx;
1152 dvbdemux->users = 0;
1153 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1155 if (!dvbdemux->filter)
1156 return -ENOMEM;
1158 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1159 if (!dvbdemux->feed) {
1160 vfree(dvbdemux->filter);
1161 return -ENOMEM;
1163 for (i = 0; i < dvbdemux->filternum; i++) {
1164 dvbdemux->filter[i].state = DMX_STATE_FREE;
1165 dvbdemux->filter[i].index = i;
1167 for (i = 0; i < dvbdemux->feednum; i++) {
1168 dvbdemux->feed[i].state = DMX_STATE_FREE;
1169 dvbdemux->feed[i].index = i;
1172 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1174 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1175 dvbdemux->pesfilter[i] = NULL;
1176 dvbdemux->pids[i] = 0xffff;
1179 INIT_LIST_HEAD(&dvbdemux->feed_list);
1181 dvbdemux->playing = 0;
1182 dvbdemux->recording = 0;
1183 dvbdemux->tsbufp = 0;
1185 if (!dvbdemux->check_crc32)
1186 dvbdemux->check_crc32 = dvb_dmx_crc32;
1188 if (!dvbdemux->memcopy)
1189 dvbdemux->memcopy = dvb_dmx_memcopy;
1191 dmx->frontend = NULL;
1192 dmx->priv = dvbdemux;
1193 dmx->open = dvbdmx_open;
1194 dmx->close = dvbdmx_close;
1195 dmx->write = dvbdmx_write;
1196 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1197 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1198 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1199 dmx->release_section_feed = dvbdmx_release_section_feed;
1201 dmx->add_frontend = dvbdmx_add_frontend;
1202 dmx->remove_frontend = dvbdmx_remove_frontend;
1203 dmx->get_frontends = dvbdmx_get_frontends;
1204 dmx->connect_frontend = dvbdmx_connect_frontend;
1205 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1206 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1208 mutex_init(&dvbdemux->mutex);
1209 spin_lock_init(&dvbdemux->lock);
1211 return 0;
1214 EXPORT_SYMBOL(dvb_dmx_init);
1216 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1218 vfree(dvbdemux->filter);
1219 vfree(dvbdemux->feed);
1222 EXPORT_SYMBOL(dvb_dmx_release);