First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / media / dvb / dvb-core / dvb_demux.c
blob91c537bca8adaac314f85c7ffc531c05f5d00620
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/sched.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/string.h>
31 #include <linux/crc32.h>
32 #include <asm/uaccess.h>
34 #include "dvb_demux.h"
36 #define NOBUFS
38 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
40 // #define DVB_DEMUX_SECTION_LOSS_LOG
42 static int dvb_demux_tscheck;
43 module_param(dvb_demux_tscheck, int, 0644);
44 MODULE_PARM_DESC(dvb_demux_tscheck,
45 "enable transport stream continuity and TEI check");
47 #define dprintk_tscheck(x...) do { \
48 if (dvb_demux_tscheck && printk_ratelimit()) \
49 printk(x); \
50 } while (0)
52 /******************************************************************************
53 * static inlined helper functions
54 ******************************************************************************/
56 static inline u16 section_length(const u8 *buf)
58 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
61 static inline u16 ts_pid(const u8 *buf)
63 return ((buf[1] & 0x1f) << 8) + buf[2];
66 static inline u8 payload(const u8 *tsp)
68 if (!(tsp[3] & 0x10)) // no payload?
69 return 0;
71 if (tsp[3] & 0x20) { // adaptation field?
72 if (tsp[4] > 183) // corrupted data?
73 return 0;
74 else
75 return 184 - 1 - tsp[4];
78 return 184;
81 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
83 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
86 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
87 size_t len)
89 memcpy(d, s, len);
92 /******************************************************************************
93 * Software filter functions
94 ******************************************************************************/
96 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
97 const u8 *buf)
99 int count = payload(buf);
100 int p;
101 //int ccok;
102 //u8 cc;
104 if (count == 0)
105 return -1;
107 p = 188 - count;
110 cc = buf[3] & 0x0f;
111 ccok = ((feed->cc + 1) & 0x0f) == cc;
112 feed->cc = cc;
113 if (!ccok)
114 printk("missed packet!\n");
117 if (buf[1] & 0x40) // PUSI ?
118 feed->peslen = 0xfffa;
120 feed->peslen += count;
122 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
125 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
126 struct dvb_demux_filter *f)
128 u8 neq = 0;
129 int i;
131 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
132 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
134 if (f->maskandmode[i] & xor)
135 return 0;
137 neq |= f->maskandnotmode[i] & xor;
140 if (f->doneq && !neq)
141 return 0;
143 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
144 NULL, 0, &f->filter, DMX_OK);
147 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
149 struct dvb_demux *demux = feed->demux;
150 struct dvb_demux_filter *f = feed->filter;
151 struct dmx_section_feed *sec = &feed->feed.sec;
152 int section_syntax_indicator;
154 if (!sec->is_filtering)
155 return 0;
157 if (!f)
158 return 0;
160 if (sec->check_crc) {
161 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
162 if (section_syntax_indicator &&
163 demux->check_crc32(feed, sec->secbuf, sec->seclen))
164 return -1;
167 do {
168 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
169 return -1;
170 } while ((f = f->next) && sec->is_filtering);
172 sec->seclen = 0;
174 return 0;
177 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
179 struct dmx_section_feed *sec = &feed->feed.sec;
181 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
182 if (sec->secbufp < sec->tsfeedp) {
183 int i, n = sec->tsfeedp - sec->secbufp;
186 * Section padding is done with 0xff bytes entirely.
187 * Due to speed reasons, we won't check all of them
188 * but just first and last.
190 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
191 printk("dvb_demux.c section ts padding loss: %d/%d\n",
192 n, sec->tsfeedp);
193 printk("dvb_demux.c pad data:");
194 for (i = 0; i < n; i++)
195 printk(" %02x", sec->secbuf[i]);
196 printk("\n");
199 #endif
201 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
202 sec->secbuf = sec->secbuf_base;
206 * Losless Section Demux 1.4.1 by Emard
207 * Valsecchi Patrick:
208 * - middle of section A (no PUSI)
209 * - end of section A and start of section B
210 * (with PUSI pointing to the start of the second section)
212 * In this case, without feed->pusi_seen you'll receive a garbage section
213 * consisting of the end of section A. Basically because tsfeedp
214 * is incemented and the use=0 condition is not raised
215 * when the second packet arrives.
217 * Fix:
218 * when demux is started, let feed->pusi_seen = 0 to
219 * prevent initial feeding of garbage from the end of
220 * previous section. When you for the first time see PUSI=1
221 * then set feed->pusi_seen = 1
223 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
224 const u8 *buf, u8 len)
226 struct dvb_demux *demux = feed->demux;
227 struct dmx_section_feed *sec = &feed->feed.sec;
228 u16 limit, seclen, n;
230 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
231 return 0;
233 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
234 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
235 printk("dvb_demux.c section buffer full loss: %d/%d\n",
236 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
237 DMX_MAX_SECFEED_SIZE);
238 #endif
239 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
242 if (len <= 0)
243 return 0;
245 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
246 sec->tsfeedp += len;
249 * Dump all the sections we can find in the data (Emard)
251 limit = sec->tsfeedp;
252 if (limit > DMX_MAX_SECFEED_SIZE)
253 return -1; /* internal error should never happen */
255 /* to be sure always set secbuf */
256 sec->secbuf = sec->secbuf_base + sec->secbufp;
258 for (n = 0; sec->secbufp + 2 < limit; n++) {
259 seclen = section_length(sec->secbuf);
260 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
261 || seclen + sec->secbufp > limit)
262 return 0;
263 sec->seclen = seclen;
264 sec->crc_val = ~0;
265 /* dump [secbuf .. secbuf+seclen) */
266 if (feed->pusi_seen)
267 dvb_dmx_swfilter_section_feed(feed);
268 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
269 else
270 printk("dvb_demux.c pusi not seen, discarding section data\n");
271 #endif
272 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
273 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
276 return 0;
279 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
280 const u8 *buf)
282 u8 p, count;
283 int ccok, dc_i = 0;
284 u8 cc;
286 count = payload(buf);
288 if (count == 0) /* count == 0 if no payload or out of range */
289 return -1;
291 p = 188 - count; /* payload start */
293 cc = buf[3] & 0x0f;
294 ccok = ((feed->cc + 1) & 0x0f) == cc;
295 feed->cc = cc;
297 if (buf[3] & 0x20) {
298 /* adaption field present, check for discontinuity_indicator */
299 if ((buf[4] > 0) && (buf[5] & 0x80))
300 dc_i = 1;
303 if (!ccok || dc_i) {
304 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
305 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
306 count);
308 * those bytes under sume circumstances will again be reported
309 * in the following dvb_dmx_swfilter_section_new
311 #endif
313 * Discontinuity detected. Reset pusi_seen = 0 to
314 * stop feeding of suspicious data until next PUSI=1 arrives
316 feed->pusi_seen = 0;
317 dvb_dmx_swfilter_section_new(feed);
320 if (buf[1] & 0x40) {
321 /* PUSI=1 (is set), section boundary is here */
322 if (count > 1 && buf[p] < count) {
323 const u8 *before = &buf[p + 1];
324 u8 before_len = buf[p];
325 const u8 *after = &before[before_len];
326 u8 after_len = count - 1 - before_len;
328 dvb_dmx_swfilter_section_copy_dump(feed, before,
329 before_len);
330 /* before start of new section, set pusi_seen = 1 */
331 feed->pusi_seen = 1;
332 dvb_dmx_swfilter_section_new(feed);
333 dvb_dmx_swfilter_section_copy_dump(feed, after,
334 after_len);
336 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
337 else if (count > 0)
338 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
339 #endif
340 } else {
341 /* PUSI=0 (is not set), no section boundary */
342 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
345 return 0;
348 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
349 const u8 *buf)
351 switch (feed->type) {
352 case DMX_TYPE_TS:
353 if (!feed->feed.ts.is_filtering)
354 break;
355 if (feed->ts_type & TS_PACKET) {
356 if (feed->ts_type & TS_PAYLOAD_ONLY)
357 dvb_dmx_swfilter_payload(feed, buf);
358 else
359 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
360 DMX_OK);
362 if (feed->ts_type & TS_DECODER)
363 if (feed->demux->write_to_decoder)
364 feed->demux->write_to_decoder(feed, buf, 188);
365 break;
367 case DMX_TYPE_SEC:
368 if (!feed->feed.sec.is_filtering)
369 break;
370 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
371 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
372 break;
374 default:
375 break;
379 #define DVR_FEED(f) \
380 (((f)->type == DMX_TYPE_TS) && \
381 ((f)->feed.ts.is_filtering) && \
382 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
384 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
386 struct dvb_demux_feed *feed;
387 u16 pid = ts_pid(buf);
388 int dvr_done = 0;
390 if (dvb_demux_tscheck) {
391 if (!demux->cnt_storage)
392 demux->cnt_storage = vmalloc(MAX_PID + 1);
394 if (!demux->cnt_storage) {
395 printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
396 dvb_demux_tscheck = 0;
397 goto no_dvb_demux_tscheck;
400 /* check pkt counter */
401 if (pid < MAX_PID) {
402 if (buf[1] & 0x80)
403 dprintk_tscheck("TEI detected. "
404 "PID=0x%x data1=0x%x\n",
405 pid, buf[1]);
407 if ((buf[3] & 0xf) != demux->cnt_storage[pid])
408 dprintk_tscheck("TS packet counter mismatch. "
409 "PID=0x%x expected 0x%x "
410 "got 0x%x\n",
411 pid, demux->cnt_storage[pid],
412 buf[3] & 0xf);
414 demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf;
416 /* end check */
418 no_dvb_demux_tscheck:
420 list_for_each_entry(feed, &demux->feed_list, list_head) {
421 if ((feed->pid != pid) && (feed->pid != 0x2000))
422 continue;
424 /* copy each packet only once to the dvr device, even
425 * if a PID is in multiple filters (e.g. video + PCR) */
426 if ((DVR_FEED(feed)) && (dvr_done++))
427 continue;
429 if (feed->pid == pid)
430 dvb_dmx_swfilter_packet_type(feed, buf);
431 else if (feed->pid == 0x2000)
432 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
436 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
437 size_t count)
439 spin_lock(&demux->lock);
441 while (count--) {
442 if (buf[0] == 0x47)
443 dvb_dmx_swfilter_packet(demux, buf);
444 buf += 188;
447 spin_unlock(&demux->lock);
450 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
452 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
454 int p = 0, i, j;
456 spin_lock(&demux->lock);
458 if (demux->tsbufp) {
459 i = demux->tsbufp;
460 j = 188 - i;
461 if (count < j) {
462 memcpy(&demux->tsbuf[i], buf, count);
463 demux->tsbufp += count;
464 goto bailout;
466 memcpy(&demux->tsbuf[i], buf, j);
467 if (demux->tsbuf[0] == 0x47)
468 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
469 demux->tsbufp = 0;
470 p += j;
473 while (p < count) {
474 if (buf[p] == 0x47) {
475 if (count - p >= 188) {
476 dvb_dmx_swfilter_packet(demux, &buf[p]);
477 p += 188;
478 } else {
479 i = count - p;
480 memcpy(demux->tsbuf, &buf[p], i);
481 demux->tsbufp = i;
482 goto bailout;
484 } else
485 p++;
488 bailout:
489 spin_unlock(&demux->lock);
492 EXPORT_SYMBOL(dvb_dmx_swfilter);
494 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
496 int p = 0, i, j;
497 u8 tmppack[188];
499 spin_lock(&demux->lock);
501 if (demux->tsbufp) {
502 i = demux->tsbufp;
503 j = 204 - i;
504 if (count < j) {
505 memcpy(&demux->tsbuf[i], buf, count);
506 demux->tsbufp += count;
507 goto bailout;
509 memcpy(&demux->tsbuf[i], buf, j);
510 if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
511 memcpy(tmppack, demux->tsbuf, 188);
512 if (tmppack[0] == 0xB8)
513 tmppack[0] = 0x47;
514 dvb_dmx_swfilter_packet(demux, tmppack);
516 demux->tsbufp = 0;
517 p += j;
520 while (p < count) {
521 if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
522 if (count - p >= 204) {
523 memcpy(tmppack, &buf[p], 188);
524 if (tmppack[0] == 0xB8)
525 tmppack[0] = 0x47;
526 dvb_dmx_swfilter_packet(demux, tmppack);
527 p += 204;
528 } else {
529 i = count - p;
530 memcpy(demux->tsbuf, &buf[p], i);
531 demux->tsbufp = i;
532 goto bailout;
534 } else {
535 p++;
539 bailout:
540 spin_unlock(&demux->lock);
543 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
545 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
547 int i;
549 for (i = 0; i < demux->filternum; i++)
550 if (demux->filter[i].state == DMX_STATE_FREE)
551 break;
553 if (i == demux->filternum)
554 return NULL;
556 demux->filter[i].state = DMX_STATE_ALLOCATED;
558 return &demux->filter[i];
561 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
563 int i;
565 for (i = 0; i < demux->feednum; i++)
566 if (demux->feed[i].state == DMX_STATE_FREE)
567 break;
569 if (i == demux->feednum)
570 return NULL;
572 demux->feed[i].state = DMX_STATE_ALLOCATED;
574 return &demux->feed[i];
577 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
579 struct dvb_demux_feed *entry;
581 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
582 if (entry == feed)
583 return 1;
585 return 0;
588 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
590 spin_lock_irq(&feed->demux->lock);
591 if (dvb_demux_feed_find(feed)) {
592 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
593 __func__, feed->type, feed->state, feed->pid);
594 goto out;
597 list_add(&feed->list_head, &feed->demux->feed_list);
598 out:
599 spin_unlock_irq(&feed->demux->lock);
602 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
604 spin_lock_irq(&feed->demux->lock);
605 if (!(dvb_demux_feed_find(feed))) {
606 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
607 __func__, feed->type, feed->state, feed->pid);
608 goto out;
611 list_del(&feed->list_head);
612 out:
613 spin_unlock_irq(&feed->demux->lock);
616 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
617 enum dmx_ts_pes pes_type,
618 size_t circular_buffer_size, struct timespec timeout)
620 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
621 struct dvb_demux *demux = feed->demux;
623 if (pid > DMX_MAX_PID)
624 return -EINVAL;
626 if (mutex_lock_interruptible(&demux->mutex))
627 return -ERESTARTSYS;
629 if (ts_type & TS_DECODER) {
630 if (pes_type >= DMX_TS_PES_OTHER) {
631 mutex_unlock(&demux->mutex);
632 return -EINVAL;
635 if (demux->pesfilter[pes_type] &&
636 demux->pesfilter[pes_type] != feed) {
637 mutex_unlock(&demux->mutex);
638 return -EINVAL;
641 demux->pesfilter[pes_type] = feed;
642 demux->pids[pes_type] = pid;
645 dvb_demux_feed_add(feed);
647 feed->pid = pid;
648 feed->buffer_size = circular_buffer_size;
649 feed->timeout = timeout;
650 feed->ts_type = ts_type;
651 feed->pes_type = pes_type;
653 if (feed->buffer_size) {
654 #ifdef NOBUFS
655 feed->buffer = NULL;
656 #else
657 feed->buffer = vmalloc(feed->buffer_size);
658 if (!feed->buffer) {
659 mutex_unlock(&demux->mutex);
660 return -ENOMEM;
662 #endif
665 feed->state = DMX_STATE_READY;
666 mutex_unlock(&demux->mutex);
668 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 (mutex_lock_interruptible(&demux->mutex))
678 return -ERESTARTSYS;
680 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
681 mutex_unlock(&demux->mutex);
682 return -EINVAL;
685 if (!demux->start_feed) {
686 mutex_unlock(&demux->mutex);
687 return -ENODEV;
690 if ((ret = demux->start_feed(feed)) < 0) {
691 mutex_unlock(&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 mutex_unlock(&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 mutex_lock(&demux->mutex);
712 if (feed->state < DMX_STATE_GO) {
713 mutex_unlock(&demux->mutex);
714 return -EINVAL;
717 if (!demux->stop_feed) {
718 mutex_unlock(&demux->mutex);
719 return -ENODEV;
722 ret = demux->stop_feed(feed);
724 spin_lock_irq(&demux->lock);
725 ts_feed->is_filtering = 0;
726 feed->state = DMX_STATE_ALLOCATED;
727 spin_unlock_irq(&demux->lock);
728 mutex_unlock(&demux->mutex);
730 return ret;
733 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
734 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 (mutex_lock_interruptible(&demux->mutex))
741 return -ERESTARTSYS;
743 if (!(feed = dvb_dmx_feed_alloc(demux))) {
744 mutex_unlock(&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;
763 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
764 feed->state = DMX_STATE_FREE;
765 mutex_unlock(&demux->mutex);
766 return -EBUSY;
769 feed->filter->type = DMX_TYPE_TS;
770 feed->filter->feed = feed;
771 feed->filter->state = DMX_STATE_READY;
773 mutex_unlock(&demux->mutex);
775 return 0;
778 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
779 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 mutex_lock(&demux->mutex);
786 if (feed->state == DMX_STATE_FREE) {
787 mutex_unlock(&demux->mutex);
788 return -EINVAL;
790 #ifndef NOBUFS
791 vfree(feed->buffer);
792 feed->buffer = NULL;
793 #endif
795 feed->state = DMX_STATE_FREE;
796 feed->filter->state = DMX_STATE_FREE;
798 dvb_demux_feed_del(feed);
800 feed->pid = 0xffff;
802 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
803 demux->pesfilter[feed->pes_type] = NULL;
805 mutex_unlock(&demux->mutex);
806 return 0;
809 /******************************************************************************
810 * dmx_section_feed API calls
811 ******************************************************************************/
813 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
814 struct dmx_section_filter **filter)
816 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
817 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
818 struct dvb_demux_filter *dvbdmxfilter;
820 if (mutex_lock_interruptible(&dvbdemux->mutex))
821 return -ERESTARTSYS;
823 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
824 if (!dvbdmxfilter) {
825 mutex_unlock(&dvbdemux->mutex);
826 return -EBUSY;
829 spin_lock_irq(&dvbdemux->lock);
830 *filter = &dvbdmxfilter->filter;
831 (*filter)->parent = feed;
832 (*filter)->priv = NULL;
833 dvbdmxfilter->feed = dvbdmxfeed;
834 dvbdmxfilter->type = DMX_TYPE_SEC;
835 dvbdmxfilter->state = DMX_STATE_READY;
836 dvbdmxfilter->next = dvbdmxfeed->filter;
837 dvbdmxfeed->filter = dvbdmxfilter;
838 spin_unlock_irq(&dvbdemux->lock);
840 mutex_unlock(&dvbdemux->mutex);
841 return 0;
844 static int dmx_section_feed_set(struct dmx_section_feed *feed,
845 u16 pid, size_t circular_buffer_size,
846 int check_crc)
848 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
849 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
851 if (pid > 0x1fff)
852 return -EINVAL;
854 if (mutex_lock_interruptible(&dvbdmx->mutex))
855 return -ERESTARTSYS;
857 dvb_demux_feed_add(dvbdmxfeed);
859 dvbdmxfeed->pid = pid;
860 dvbdmxfeed->buffer_size = circular_buffer_size;
861 dvbdmxfeed->feed.sec.check_crc = check_crc;
863 #ifdef NOBUFS
864 dvbdmxfeed->buffer = NULL;
865 #else
866 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
867 if (!dvbdmxfeed->buffer) {
868 mutex_unlock(&dvbdmx->mutex);
869 return -ENOMEM;
871 #endif
873 dvbdmxfeed->state = DMX_STATE_READY;
874 mutex_unlock(&dvbdmx->mutex);
875 return 0;
878 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
880 int i;
881 struct dvb_demux_filter *f;
882 struct dmx_section_filter *sf;
883 u8 mask, mode, doneq;
885 if (!(f = dvbdmxfeed->filter))
886 return;
887 do {
888 sf = &f->filter;
889 doneq = 0;
890 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
891 mode = sf->filter_mode[i];
892 mask = sf->filter_mask[i];
893 f->maskandmode[i] = mask & mode;
894 doneq |= f->maskandnotmode[i] = mask & ~mode;
896 f->doneq = doneq ? 1 : 0;
897 } while ((f = f->next));
900 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
902 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
903 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
904 int ret;
906 if (mutex_lock_interruptible(&dvbdmx->mutex))
907 return -ERESTARTSYS;
909 if (feed->is_filtering) {
910 mutex_unlock(&dvbdmx->mutex);
911 return -EBUSY;
914 if (!dvbdmxfeed->filter) {
915 mutex_unlock(&dvbdmx->mutex);
916 return -EINVAL;
919 dvbdmxfeed->feed.sec.tsfeedp = 0;
920 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
921 dvbdmxfeed->feed.sec.secbufp = 0;
922 dvbdmxfeed->feed.sec.seclen = 0;
924 if (!dvbdmx->start_feed) {
925 mutex_unlock(&dvbdmx->mutex);
926 return -ENODEV;
929 prepare_secfilters(dvbdmxfeed);
931 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
932 mutex_unlock(&dvbdmx->mutex);
933 return ret;
936 spin_lock_irq(&dvbdmx->lock);
937 feed->is_filtering = 1;
938 dvbdmxfeed->state = DMX_STATE_GO;
939 spin_unlock_irq(&dvbdmx->lock);
941 mutex_unlock(&dvbdmx->mutex);
942 return 0;
945 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
947 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
948 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
949 int ret;
951 mutex_lock(&dvbdmx->mutex);
953 if (!dvbdmx->stop_feed) {
954 mutex_unlock(&dvbdmx->mutex);
955 return -ENODEV;
958 ret = dvbdmx->stop_feed(dvbdmxfeed);
960 spin_lock_irq(&dvbdmx->lock);
961 dvbdmxfeed->state = DMX_STATE_READY;
962 feed->is_filtering = 0;
963 spin_unlock_irq(&dvbdmx->lock);
965 mutex_unlock(&dvbdmx->mutex);
966 return ret;
969 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
970 struct dmx_section_filter *filter)
972 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
973 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
974 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
976 mutex_lock(&dvbdmx->mutex);
978 if (dvbdmxfilter->feed != dvbdmxfeed) {
979 mutex_unlock(&dvbdmx->mutex);
980 return -EINVAL;
983 if (feed->is_filtering)
984 feed->stop_filtering(feed);
986 spin_lock_irq(&dvbdmx->lock);
987 f = dvbdmxfeed->filter;
989 if (f == dvbdmxfilter) {
990 dvbdmxfeed->filter = dvbdmxfilter->next;
991 } else {
992 while (f->next != dvbdmxfilter)
993 f = f->next;
994 f->next = f->next->next;
997 dvbdmxfilter->state = DMX_STATE_FREE;
998 spin_unlock_irq(&dvbdmx->lock);
999 mutex_unlock(&dvbdmx->mutex);
1000 return 0;
1003 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1004 struct dmx_section_feed **feed,
1005 dmx_section_cb callback)
1007 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1008 struct dvb_demux_feed *dvbdmxfeed;
1010 if (mutex_lock_interruptible(&dvbdmx->mutex))
1011 return -ERESTARTSYS;
1013 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1014 mutex_unlock(&dvbdmx->mutex);
1015 return -EBUSY;
1018 dvbdmxfeed->type = DMX_TYPE_SEC;
1019 dvbdmxfeed->cb.sec = callback;
1020 dvbdmxfeed->demux = dvbdmx;
1021 dvbdmxfeed->pid = 0xffff;
1022 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1023 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1024 dvbdmxfeed->feed.sec.tsfeedp = 0;
1025 dvbdmxfeed->filter = NULL;
1026 dvbdmxfeed->buffer = NULL;
1028 (*feed) = &dvbdmxfeed->feed.sec;
1029 (*feed)->is_filtering = 0;
1030 (*feed)->parent = demux;
1031 (*feed)->priv = NULL;
1033 (*feed)->set = dmx_section_feed_set;
1034 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1035 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1036 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1037 (*feed)->release_filter = dmx_section_feed_release_filter;
1039 mutex_unlock(&dvbdmx->mutex);
1040 return 0;
1043 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1044 struct dmx_section_feed *feed)
1046 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1047 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1049 mutex_lock(&dvbdmx->mutex);
1051 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1052 mutex_unlock(&dvbdmx->mutex);
1053 return -EINVAL;
1055 #ifndef NOBUFS
1056 vfree(dvbdmxfeed->buffer);
1057 dvbdmxfeed->buffer = NULL;
1058 #endif
1059 dvbdmxfeed->state = DMX_STATE_FREE;
1061 dvb_demux_feed_del(dvbdmxfeed);
1063 dvbdmxfeed->pid = 0xffff;
1065 mutex_unlock(&dvbdmx->mutex);
1066 return 0;
1069 /******************************************************************************
1070 * dvb_demux kernel data API calls
1071 ******************************************************************************/
1073 static int dvbdmx_open(struct dmx_demux *demux)
1075 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1077 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1078 return -EUSERS;
1080 dvbdemux->users++;
1081 return 0;
1084 static int dvbdmx_close(struct dmx_demux *demux)
1086 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1088 if (dvbdemux->users == 0)
1089 return -ENODEV;
1091 dvbdemux->users--;
1092 //FIXME: release any unneeded resources if users==0
1093 return 0;
1096 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1098 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1099 void *p;
1101 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1102 return -EINVAL;
1104 p = kmalloc(count, GFP_USER);
1105 if (!p)
1106 return -ENOMEM;
1107 if (copy_from_user(p, buf, count)) {
1108 kfree(p);
1109 return -EFAULT;
1111 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1112 kfree(p);
1113 return -ERESTARTSYS;
1115 dvb_dmx_swfilter(dvbdemux, p, count);
1116 kfree(p);
1117 mutex_unlock(&dvbdemux->mutex);
1119 if (signal_pending(current))
1120 return -EINTR;
1121 return count;
1124 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1125 struct dmx_frontend *frontend)
1127 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1128 struct list_head *head = &dvbdemux->frontend_list;
1130 list_add(&(frontend->connectivity_list), head);
1132 return 0;
1135 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1136 struct dmx_frontend *frontend)
1138 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1139 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1141 list_for_each_safe(pos, n, head) {
1142 if (DMX_FE_ENTRY(pos) == frontend) {
1143 list_del(pos);
1144 return 0;
1148 return -ENODEV;
1151 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1153 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1155 if (list_empty(&dvbdemux->frontend_list))
1156 return NULL;
1158 return &dvbdemux->frontend_list;
1161 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1162 struct dmx_frontend *frontend)
1164 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1166 if (demux->frontend)
1167 return -EINVAL;
1169 mutex_lock(&dvbdemux->mutex);
1171 demux->frontend = frontend;
1172 mutex_unlock(&dvbdemux->mutex);
1173 return 0;
1176 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1178 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1180 mutex_lock(&dvbdemux->mutex);
1182 demux->frontend = NULL;
1183 mutex_unlock(&dvbdemux->mutex);
1184 return 0;
1187 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1189 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1191 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1192 return 0;
1195 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1197 int i;
1198 struct dmx_demux *dmx = &dvbdemux->dmx;
1200 dvbdemux->cnt_storage = NULL;
1201 dvbdemux->users = 0;
1202 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1204 if (!dvbdemux->filter)
1205 return -ENOMEM;
1207 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1208 if (!dvbdemux->feed) {
1209 vfree(dvbdemux->filter);
1210 return -ENOMEM;
1212 for (i = 0; i < dvbdemux->filternum; i++) {
1213 dvbdemux->filter[i].state = DMX_STATE_FREE;
1214 dvbdemux->filter[i].index = i;
1216 for (i = 0; i < dvbdemux->feednum; i++) {
1217 dvbdemux->feed[i].state = DMX_STATE_FREE;
1218 dvbdemux->feed[i].index = i;
1221 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1223 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1224 dvbdemux->pesfilter[i] = NULL;
1225 dvbdemux->pids[i] = 0xffff;
1228 INIT_LIST_HEAD(&dvbdemux->feed_list);
1230 dvbdemux->playing = 0;
1231 dvbdemux->recording = 0;
1232 dvbdemux->tsbufp = 0;
1234 if (!dvbdemux->check_crc32)
1235 dvbdemux->check_crc32 = dvb_dmx_crc32;
1237 if (!dvbdemux->memcopy)
1238 dvbdemux->memcopy = dvb_dmx_memcopy;
1240 dmx->frontend = NULL;
1241 dmx->priv = dvbdemux;
1242 dmx->open = dvbdmx_open;
1243 dmx->close = dvbdmx_close;
1244 dmx->write = dvbdmx_write;
1245 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1246 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1247 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1248 dmx->release_section_feed = dvbdmx_release_section_feed;
1250 dmx->add_frontend = dvbdmx_add_frontend;
1251 dmx->remove_frontend = dvbdmx_remove_frontend;
1252 dmx->get_frontends = dvbdmx_get_frontends;
1253 dmx->connect_frontend = dvbdmx_connect_frontend;
1254 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1255 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1257 mutex_init(&dvbdemux->mutex);
1258 spin_lock_init(&dvbdemux->lock);
1260 return 0;
1263 EXPORT_SYMBOL(dvb_dmx_init);
1265 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1267 vfree(dvbdemux->cnt_storage);
1268 vfree(dvbdemux->filter);
1269 vfree(dvbdemux->feed);
1272 EXPORT_SYMBOL(dvb_dmx_release);