Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / media / dvb / dvb-core / dvb_demux.c
blobd82469f842e2e023a85c88f8917081b5bce2c776
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>
33 #include <asm/div64.h>
35 #include "dvb_demux.h"
37 #define NOBUFS
39 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
41 // #define DVB_DEMUX_SECTION_LOSS_LOG
43 static int dvb_demux_tscheck;
44 module_param(dvb_demux_tscheck, int, 0644);
45 MODULE_PARM_DESC(dvb_demux_tscheck,
46 "enable transport stream continuity and TEI check");
48 static int dvb_demux_speedcheck;
49 module_param(dvb_demux_speedcheck, int, 0644);
50 MODULE_PARM_DESC(dvb_demux_speedcheck,
51 "enable transport stream speed check");
53 #define dprintk_tscheck(x...) do { \
54 if (dvb_demux_tscheck && printk_ratelimit()) \
55 printk(x); \
56 } while (0)
58 /******************************************************************************
59 * static inlined helper functions
60 ******************************************************************************/
62 static inline u16 section_length(const u8 *buf)
64 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
67 static inline u16 ts_pid(const u8 *buf)
69 return ((buf[1] & 0x1f) << 8) + buf[2];
72 static inline u8 payload(const u8 *tsp)
74 if (!(tsp[3] & 0x10)) // no payload?
75 return 0;
77 if (tsp[3] & 0x20) { // adaptation field?
78 if (tsp[4] > 183) // corrupted data?
79 return 0;
80 else
81 return 184 - 1 - tsp[4];
84 return 184;
87 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
89 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
92 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
93 size_t len)
95 memcpy(d, s, len);
98 /******************************************************************************
99 * Software filter functions
100 ******************************************************************************/
102 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
103 const u8 *buf)
105 int count = payload(buf);
106 int p;
107 //int ccok;
108 //u8 cc;
110 if (count == 0)
111 return -1;
113 p = 188 - count;
116 cc = buf[3] & 0x0f;
117 ccok = ((feed->cc + 1) & 0x0f) == cc;
118 feed->cc = cc;
119 if (!ccok)
120 printk("missed packet!\n");
123 if (buf[1] & 0x40) // PUSI ?
124 feed->peslen = 0xfffa;
126 feed->peslen += count;
128 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
131 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
132 struct dvb_demux_filter *f)
134 u8 neq = 0;
135 int i;
137 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
138 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
140 if (f->maskandmode[i] & xor)
141 return 0;
143 neq |= f->maskandnotmode[i] & xor;
146 if (f->doneq && !neq)
147 return 0;
149 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
150 NULL, 0, &f->filter, DMX_OK);
153 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
155 struct dvb_demux *demux = feed->demux;
156 struct dvb_demux_filter *f = feed->filter;
157 struct dmx_section_feed *sec = &feed->feed.sec;
158 int section_syntax_indicator;
160 if (!sec->is_filtering)
161 return 0;
163 if (!f)
164 return 0;
166 if (sec->check_crc) {
167 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
168 if (section_syntax_indicator &&
169 demux->check_crc32(feed, sec->secbuf, sec->seclen))
170 return -1;
173 do {
174 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
175 return -1;
176 } while ((f = f->next) && sec->is_filtering);
178 sec->seclen = 0;
180 return 0;
183 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
185 struct dmx_section_feed *sec = &feed->feed.sec;
187 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
188 if (sec->secbufp < sec->tsfeedp) {
189 int i, n = sec->tsfeedp - sec->secbufp;
192 * Section padding is done with 0xff bytes entirely.
193 * Due to speed reasons, we won't check all of them
194 * but just first and last.
196 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
197 printk("dvb_demux.c section ts padding loss: %d/%d\n",
198 n, sec->tsfeedp);
199 printk("dvb_demux.c pad data:");
200 for (i = 0; i < n; i++)
201 printk(" %02x", sec->secbuf[i]);
202 printk("\n");
205 #endif
207 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
208 sec->secbuf = sec->secbuf_base;
212 * Losless Section Demux 1.4.1 by Emard
213 * Valsecchi Patrick:
214 * - middle of section A (no PUSI)
215 * - end of section A and start of section B
216 * (with PUSI pointing to the start of the second section)
218 * In this case, without feed->pusi_seen you'll receive a garbage section
219 * consisting of the end of section A. Basically because tsfeedp
220 * is incemented and the use=0 condition is not raised
221 * when the second packet arrives.
223 * Fix:
224 * when demux is started, let feed->pusi_seen = 0 to
225 * prevent initial feeding of garbage from the end of
226 * previous section. When you for the first time see PUSI=1
227 * then set feed->pusi_seen = 1
229 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
230 const u8 *buf, u8 len)
232 struct dvb_demux *demux = feed->demux;
233 struct dmx_section_feed *sec = &feed->feed.sec;
234 u16 limit, seclen, n;
236 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
237 return 0;
239 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
240 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
241 printk("dvb_demux.c section buffer full loss: %d/%d\n",
242 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
243 DMX_MAX_SECFEED_SIZE);
244 #endif
245 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
248 if (len <= 0)
249 return 0;
251 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
252 sec->tsfeedp += len;
255 * Dump all the sections we can find in the data (Emard)
257 limit = sec->tsfeedp;
258 if (limit > DMX_MAX_SECFEED_SIZE)
259 return -1; /* internal error should never happen */
261 /* to be sure always set secbuf */
262 sec->secbuf = sec->secbuf_base + sec->secbufp;
264 for (n = 0; sec->secbufp + 2 < limit; n++) {
265 seclen = section_length(sec->secbuf);
266 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
267 || seclen + sec->secbufp > limit)
268 return 0;
269 sec->seclen = seclen;
270 sec->crc_val = ~0;
271 /* dump [secbuf .. secbuf+seclen) */
272 if (feed->pusi_seen)
273 dvb_dmx_swfilter_section_feed(feed);
274 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
275 else
276 printk("dvb_demux.c pusi not seen, discarding section data\n");
277 #endif
278 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
279 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
282 return 0;
285 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
286 const u8 *buf)
288 u8 p, count;
289 int ccok, dc_i = 0;
290 u8 cc;
292 count = payload(buf);
294 if (count == 0) /* count == 0 if no payload or out of range */
295 return -1;
297 p = 188 - count; /* payload start */
299 cc = buf[3] & 0x0f;
300 ccok = ((feed->cc + 1) & 0x0f) == cc;
301 feed->cc = cc;
303 if (buf[3] & 0x20) {
304 /* adaption field present, check for discontinuity_indicator */
305 if ((buf[4] > 0) && (buf[5] & 0x80))
306 dc_i = 1;
309 if (!ccok || dc_i) {
310 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
311 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
312 count);
314 * those bytes under sume circumstances will again be reported
315 * in the following dvb_dmx_swfilter_section_new
317 #endif
319 * Discontinuity detected. Reset pusi_seen = 0 to
320 * stop feeding of suspicious data until next PUSI=1 arrives
322 feed->pusi_seen = 0;
323 dvb_dmx_swfilter_section_new(feed);
326 if (buf[1] & 0x40) {
327 /* PUSI=1 (is set), section boundary is here */
328 if (count > 1 && buf[p] < count) {
329 const u8 *before = &buf[p + 1];
330 u8 before_len = buf[p];
331 const u8 *after = &before[before_len];
332 u8 after_len = count - 1 - before_len;
334 dvb_dmx_swfilter_section_copy_dump(feed, before,
335 before_len);
336 /* before start of new section, set pusi_seen = 1 */
337 feed->pusi_seen = 1;
338 dvb_dmx_swfilter_section_new(feed);
339 dvb_dmx_swfilter_section_copy_dump(feed, after,
340 after_len);
342 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
343 else if (count > 0)
344 printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
345 #endif
346 } else {
347 /* PUSI=0 (is not set), no section boundary */
348 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
351 return 0;
354 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
355 const u8 *buf)
357 switch (feed->type) {
358 case DMX_TYPE_TS:
359 if (!feed->feed.ts.is_filtering)
360 break;
361 if (feed->ts_type & TS_PACKET) {
362 if (feed->ts_type & TS_PAYLOAD_ONLY)
363 dvb_dmx_swfilter_payload(feed, buf);
364 else
365 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
366 DMX_OK);
368 if (feed->ts_type & TS_DECODER)
369 if (feed->demux->write_to_decoder)
370 feed->demux->write_to_decoder(feed, buf, 188);
371 break;
373 case DMX_TYPE_SEC:
374 if (!feed->feed.sec.is_filtering)
375 break;
376 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
377 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
378 break;
380 default:
381 break;
385 #define DVR_FEED(f) \
386 (((f)->type == DMX_TYPE_TS) && \
387 ((f)->feed.ts.is_filtering) && \
388 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
390 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
392 struct dvb_demux_feed *feed;
393 u16 pid = ts_pid(buf);
394 int dvr_done = 0;
396 if (dvb_demux_speedcheck) {
397 struct timespec cur_time, delta_time;
398 u64 speed_bytes, speed_timedelta;
400 demux->speed_pkts_cnt++;
402 /* show speed every SPEED_PKTS_INTERVAL packets */
403 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
404 cur_time = current_kernel_time();
406 if (demux->speed_last_time.tv_sec != 0 &&
407 demux->speed_last_time.tv_nsec != 0) {
408 delta_time = timespec_sub(cur_time,
409 demux->speed_last_time);
410 speed_bytes = (u64)demux->speed_pkts_cnt
411 * 188 * 8;
412 /* convert to 1024 basis */
413 speed_bytes = 1000 * div64_u64(speed_bytes,
414 1024);
415 speed_timedelta =
416 (u64)timespec_to_ns(&delta_time);
417 speed_timedelta = div64_u64(speed_timedelta,
418 1000000); /* nsec -> usec */
419 printk(KERN_INFO "TS speed %llu Kbits/sec \n",
420 div64_u64(speed_bytes,
421 speed_timedelta));
424 demux->speed_last_time = cur_time;
425 demux->speed_pkts_cnt = 0;
429 if (demux->cnt_storage && dvb_demux_tscheck) {
430 /* check pkt counter */
431 if (pid < MAX_PID) {
432 if (buf[1] & 0x80)
433 dprintk_tscheck("TEI detected. "
434 "PID=0x%x data1=0x%x\n",
435 pid, buf[1]);
437 if ((buf[3] & 0xf) != demux->cnt_storage[pid])
438 dprintk_tscheck("TS packet counter mismatch. "
439 "PID=0x%x expected 0x%x "
440 "got 0x%x\n",
441 pid, demux->cnt_storage[pid],
442 buf[3] & 0xf);
444 demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf;
446 /* end check */
449 list_for_each_entry(feed, &demux->feed_list, list_head) {
450 if ((feed->pid != pid) && (feed->pid != 0x2000))
451 continue;
453 /* copy each packet only once to the dvr device, even
454 * if a PID is in multiple filters (e.g. video + PCR) */
455 if ((DVR_FEED(feed)) && (dvr_done++))
456 continue;
458 if (feed->pid == pid)
459 dvb_dmx_swfilter_packet_type(feed, buf);
460 else if (feed->pid == 0x2000)
461 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
465 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
466 size_t count)
468 spin_lock(&demux->lock);
470 while (count--) {
471 if (buf[0] == 0x47)
472 dvb_dmx_swfilter_packet(demux, buf);
473 buf += 188;
476 spin_unlock(&demux->lock);
479 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
481 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
482 const int pktsize)
484 int start = pos, lost;
486 while (pos < count) {
487 if (buf[pos] == 0x47 ||
488 (pktsize == 204 && buf[pos] == 0xB8))
489 break;
490 pos++;
493 lost = pos - start;
494 if (lost) {
495 /* This garbage is part of a valid packet? */
496 int backtrack = pos - pktsize;
497 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
498 (pktsize == 204 && buf[backtrack] == 0xB8)))
499 return backtrack;
502 return pos;
505 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
506 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
507 size_t count, const int pktsize)
509 int p = 0, i, j;
510 const u8 *q;
512 spin_lock(&demux->lock);
514 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
515 i = demux->tsbufp;
516 j = pktsize - i;
517 if (count < j) {
518 memcpy(&demux->tsbuf[i], buf, count);
519 demux->tsbufp += count;
520 goto bailout;
522 memcpy(&demux->tsbuf[i], buf, j);
523 if (demux->tsbuf[0] == 0x47) /* double check */
524 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
525 demux->tsbufp = 0;
526 p += j;
529 while (1) {
530 p = find_next_packet(buf, p, count, pktsize);
531 if (p >= count)
532 break;
533 if (count - p < pktsize)
534 break;
536 q = &buf[p];
538 if (pktsize == 204 && (*q == 0xB8)) {
539 memcpy(demux->tsbuf, q, 188);
540 demux->tsbuf[0] = 0x47;
541 q = demux->tsbuf;
543 dvb_dmx_swfilter_packet(demux, q);
544 p += pktsize;
547 i = count - p;
548 if (i) {
549 memcpy(demux->tsbuf, &buf[p], i);
550 demux->tsbufp = i;
551 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
552 demux->tsbuf[0] = 0x47;
555 bailout:
556 spin_unlock(&demux->lock);
559 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
561 _dvb_dmx_swfilter(demux, buf, count, 188);
563 EXPORT_SYMBOL(dvb_dmx_swfilter);
565 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
567 _dvb_dmx_swfilter(demux, buf, count, 204);
569 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
571 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
573 spin_lock(&demux->lock);
575 demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts, DMX_OK);
577 spin_unlock(&demux->lock);
579 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
581 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
583 int i;
585 for (i = 0; i < demux->filternum; i++)
586 if (demux->filter[i].state == DMX_STATE_FREE)
587 break;
589 if (i == demux->filternum)
590 return NULL;
592 demux->filter[i].state = DMX_STATE_ALLOCATED;
594 return &demux->filter[i];
597 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
599 int i;
601 for (i = 0; i < demux->feednum; i++)
602 if (demux->feed[i].state == DMX_STATE_FREE)
603 break;
605 if (i == demux->feednum)
606 return NULL;
608 demux->feed[i].state = DMX_STATE_ALLOCATED;
610 return &demux->feed[i];
613 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
615 struct dvb_demux_feed *entry;
617 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
618 if (entry == feed)
619 return 1;
621 return 0;
624 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
626 spin_lock_irq(&feed->demux->lock);
627 if (dvb_demux_feed_find(feed)) {
628 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
629 __func__, feed->type, feed->state, feed->pid);
630 goto out;
633 list_add(&feed->list_head, &feed->demux->feed_list);
634 out:
635 spin_unlock_irq(&feed->demux->lock);
638 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
640 spin_lock_irq(&feed->demux->lock);
641 if (!(dvb_demux_feed_find(feed))) {
642 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
643 __func__, feed->type, feed->state, feed->pid);
644 goto out;
647 list_del(&feed->list_head);
648 out:
649 spin_unlock_irq(&feed->demux->lock);
652 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
653 enum dmx_ts_pes pes_type,
654 size_t circular_buffer_size, struct timespec timeout)
656 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
657 struct dvb_demux *demux = feed->demux;
659 if (pid > DMX_MAX_PID)
660 return -EINVAL;
662 if (mutex_lock_interruptible(&demux->mutex))
663 return -ERESTARTSYS;
665 if (ts_type & TS_DECODER) {
666 if (pes_type >= DMX_TS_PES_OTHER) {
667 mutex_unlock(&demux->mutex);
668 return -EINVAL;
671 if (demux->pesfilter[pes_type] &&
672 demux->pesfilter[pes_type] != feed) {
673 mutex_unlock(&demux->mutex);
674 return -EINVAL;
677 demux->pesfilter[pes_type] = feed;
678 demux->pids[pes_type] = pid;
681 dvb_demux_feed_add(feed);
683 feed->pid = pid;
684 feed->buffer_size = circular_buffer_size;
685 feed->timeout = timeout;
686 feed->ts_type = ts_type;
687 feed->pes_type = pes_type;
689 if (feed->buffer_size) {
690 #ifdef NOBUFS
691 feed->buffer = NULL;
692 #else
693 feed->buffer = vmalloc(feed->buffer_size);
694 if (!feed->buffer) {
695 mutex_unlock(&demux->mutex);
696 return -ENOMEM;
698 #endif
701 feed->state = DMX_STATE_READY;
702 mutex_unlock(&demux->mutex);
704 return 0;
707 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
709 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
710 struct dvb_demux *demux = feed->demux;
711 int ret;
713 if (mutex_lock_interruptible(&demux->mutex))
714 return -ERESTARTSYS;
716 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
717 mutex_unlock(&demux->mutex);
718 return -EINVAL;
721 if (!demux->start_feed) {
722 mutex_unlock(&demux->mutex);
723 return -ENODEV;
726 if ((ret = demux->start_feed(feed)) < 0) {
727 mutex_unlock(&demux->mutex);
728 return ret;
731 spin_lock_irq(&demux->lock);
732 ts_feed->is_filtering = 1;
733 feed->state = DMX_STATE_GO;
734 spin_unlock_irq(&demux->lock);
735 mutex_unlock(&demux->mutex);
737 return 0;
740 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
742 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
743 struct dvb_demux *demux = feed->demux;
744 int ret;
746 mutex_lock(&demux->mutex);
748 if (feed->state < DMX_STATE_GO) {
749 mutex_unlock(&demux->mutex);
750 return -EINVAL;
753 if (!demux->stop_feed) {
754 mutex_unlock(&demux->mutex);
755 return -ENODEV;
758 ret = demux->stop_feed(feed);
760 spin_lock_irq(&demux->lock);
761 ts_feed->is_filtering = 0;
762 feed->state = DMX_STATE_ALLOCATED;
763 spin_unlock_irq(&demux->lock);
764 mutex_unlock(&demux->mutex);
766 return ret;
769 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
770 struct dmx_ts_feed **ts_feed,
771 dmx_ts_cb callback)
773 struct dvb_demux *demux = (struct dvb_demux *)dmx;
774 struct dvb_demux_feed *feed;
776 if (mutex_lock_interruptible(&demux->mutex))
777 return -ERESTARTSYS;
779 if (!(feed = dvb_dmx_feed_alloc(demux))) {
780 mutex_unlock(&demux->mutex);
781 return -EBUSY;
784 feed->type = DMX_TYPE_TS;
785 feed->cb.ts = callback;
786 feed->demux = demux;
787 feed->pid = 0xffff;
788 feed->peslen = 0xfffa;
789 feed->buffer = NULL;
791 (*ts_feed) = &feed->feed.ts;
792 (*ts_feed)->parent = dmx;
793 (*ts_feed)->priv = NULL;
794 (*ts_feed)->is_filtering = 0;
795 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
796 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
797 (*ts_feed)->set = dmx_ts_feed_set;
799 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
800 feed->state = DMX_STATE_FREE;
801 mutex_unlock(&demux->mutex);
802 return -EBUSY;
805 feed->filter->type = DMX_TYPE_TS;
806 feed->filter->feed = feed;
807 feed->filter->state = DMX_STATE_READY;
809 mutex_unlock(&demux->mutex);
811 return 0;
814 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
815 struct dmx_ts_feed *ts_feed)
817 struct dvb_demux *demux = (struct dvb_demux *)dmx;
818 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
820 mutex_lock(&demux->mutex);
822 if (feed->state == DMX_STATE_FREE) {
823 mutex_unlock(&demux->mutex);
824 return -EINVAL;
826 #ifndef NOBUFS
827 vfree(feed->buffer);
828 feed->buffer = NULL;
829 #endif
831 feed->state = DMX_STATE_FREE;
832 feed->filter->state = DMX_STATE_FREE;
834 dvb_demux_feed_del(feed);
836 feed->pid = 0xffff;
838 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
839 demux->pesfilter[feed->pes_type] = NULL;
841 mutex_unlock(&demux->mutex);
842 return 0;
845 /******************************************************************************
846 * dmx_section_feed API calls
847 ******************************************************************************/
849 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
850 struct dmx_section_filter **filter)
852 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
853 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
854 struct dvb_demux_filter *dvbdmxfilter;
856 if (mutex_lock_interruptible(&dvbdemux->mutex))
857 return -ERESTARTSYS;
859 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
860 if (!dvbdmxfilter) {
861 mutex_unlock(&dvbdemux->mutex);
862 return -EBUSY;
865 spin_lock_irq(&dvbdemux->lock);
866 *filter = &dvbdmxfilter->filter;
867 (*filter)->parent = feed;
868 (*filter)->priv = NULL;
869 dvbdmxfilter->feed = dvbdmxfeed;
870 dvbdmxfilter->type = DMX_TYPE_SEC;
871 dvbdmxfilter->state = DMX_STATE_READY;
872 dvbdmxfilter->next = dvbdmxfeed->filter;
873 dvbdmxfeed->filter = dvbdmxfilter;
874 spin_unlock_irq(&dvbdemux->lock);
876 mutex_unlock(&dvbdemux->mutex);
877 return 0;
880 static int dmx_section_feed_set(struct dmx_section_feed *feed,
881 u16 pid, size_t circular_buffer_size,
882 int check_crc)
884 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
885 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
887 if (pid > 0x1fff)
888 return -EINVAL;
890 if (mutex_lock_interruptible(&dvbdmx->mutex))
891 return -ERESTARTSYS;
893 dvb_demux_feed_add(dvbdmxfeed);
895 dvbdmxfeed->pid = pid;
896 dvbdmxfeed->buffer_size = circular_buffer_size;
897 dvbdmxfeed->feed.sec.check_crc = check_crc;
899 #ifdef NOBUFS
900 dvbdmxfeed->buffer = NULL;
901 #else
902 dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
903 if (!dvbdmxfeed->buffer) {
904 mutex_unlock(&dvbdmx->mutex);
905 return -ENOMEM;
907 #endif
909 dvbdmxfeed->state = DMX_STATE_READY;
910 mutex_unlock(&dvbdmx->mutex);
911 return 0;
914 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
916 int i;
917 struct dvb_demux_filter *f;
918 struct dmx_section_filter *sf;
919 u8 mask, mode, doneq;
921 if (!(f = dvbdmxfeed->filter))
922 return;
923 do {
924 sf = &f->filter;
925 doneq = 0;
926 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
927 mode = sf->filter_mode[i];
928 mask = sf->filter_mask[i];
929 f->maskandmode[i] = mask & mode;
930 doneq |= f->maskandnotmode[i] = mask & ~mode;
932 f->doneq = doneq ? 1 : 0;
933 } while ((f = f->next));
936 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
938 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
939 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
940 int ret;
942 if (mutex_lock_interruptible(&dvbdmx->mutex))
943 return -ERESTARTSYS;
945 if (feed->is_filtering) {
946 mutex_unlock(&dvbdmx->mutex);
947 return -EBUSY;
950 if (!dvbdmxfeed->filter) {
951 mutex_unlock(&dvbdmx->mutex);
952 return -EINVAL;
955 dvbdmxfeed->feed.sec.tsfeedp = 0;
956 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
957 dvbdmxfeed->feed.sec.secbufp = 0;
958 dvbdmxfeed->feed.sec.seclen = 0;
960 if (!dvbdmx->start_feed) {
961 mutex_unlock(&dvbdmx->mutex);
962 return -ENODEV;
965 prepare_secfilters(dvbdmxfeed);
967 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
968 mutex_unlock(&dvbdmx->mutex);
969 return ret;
972 spin_lock_irq(&dvbdmx->lock);
973 feed->is_filtering = 1;
974 dvbdmxfeed->state = DMX_STATE_GO;
975 spin_unlock_irq(&dvbdmx->lock);
977 mutex_unlock(&dvbdmx->mutex);
978 return 0;
981 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
983 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
984 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
985 int ret;
987 mutex_lock(&dvbdmx->mutex);
989 if (!dvbdmx->stop_feed) {
990 mutex_unlock(&dvbdmx->mutex);
991 return -ENODEV;
994 ret = dvbdmx->stop_feed(dvbdmxfeed);
996 spin_lock_irq(&dvbdmx->lock);
997 dvbdmxfeed->state = DMX_STATE_READY;
998 feed->is_filtering = 0;
999 spin_unlock_irq(&dvbdmx->lock);
1001 mutex_unlock(&dvbdmx->mutex);
1002 return ret;
1005 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
1006 struct dmx_section_filter *filter)
1008 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
1009 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1010 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1012 mutex_lock(&dvbdmx->mutex);
1014 if (dvbdmxfilter->feed != dvbdmxfeed) {
1015 mutex_unlock(&dvbdmx->mutex);
1016 return -EINVAL;
1019 if (feed->is_filtering)
1020 feed->stop_filtering(feed);
1022 spin_lock_irq(&dvbdmx->lock);
1023 f = dvbdmxfeed->filter;
1025 if (f == dvbdmxfilter) {
1026 dvbdmxfeed->filter = dvbdmxfilter->next;
1027 } else {
1028 while (f->next != dvbdmxfilter)
1029 f = f->next;
1030 f->next = f->next->next;
1033 dvbdmxfilter->state = DMX_STATE_FREE;
1034 spin_unlock_irq(&dvbdmx->lock);
1035 mutex_unlock(&dvbdmx->mutex);
1036 return 0;
1039 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1040 struct dmx_section_feed **feed,
1041 dmx_section_cb callback)
1043 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1044 struct dvb_demux_feed *dvbdmxfeed;
1046 if (mutex_lock_interruptible(&dvbdmx->mutex))
1047 return -ERESTARTSYS;
1049 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1050 mutex_unlock(&dvbdmx->mutex);
1051 return -EBUSY;
1054 dvbdmxfeed->type = DMX_TYPE_SEC;
1055 dvbdmxfeed->cb.sec = callback;
1056 dvbdmxfeed->demux = dvbdmx;
1057 dvbdmxfeed->pid = 0xffff;
1058 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1059 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1060 dvbdmxfeed->feed.sec.tsfeedp = 0;
1061 dvbdmxfeed->filter = NULL;
1062 dvbdmxfeed->buffer = NULL;
1064 (*feed) = &dvbdmxfeed->feed.sec;
1065 (*feed)->is_filtering = 0;
1066 (*feed)->parent = demux;
1067 (*feed)->priv = NULL;
1069 (*feed)->set = dmx_section_feed_set;
1070 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1071 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1072 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1073 (*feed)->release_filter = dmx_section_feed_release_filter;
1075 mutex_unlock(&dvbdmx->mutex);
1076 return 0;
1079 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1080 struct dmx_section_feed *feed)
1082 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1083 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1085 mutex_lock(&dvbdmx->mutex);
1087 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1088 mutex_unlock(&dvbdmx->mutex);
1089 return -EINVAL;
1091 #ifndef NOBUFS
1092 vfree(dvbdmxfeed->buffer);
1093 dvbdmxfeed->buffer = NULL;
1094 #endif
1095 dvbdmxfeed->state = DMX_STATE_FREE;
1097 dvb_demux_feed_del(dvbdmxfeed);
1099 dvbdmxfeed->pid = 0xffff;
1101 mutex_unlock(&dvbdmx->mutex);
1102 return 0;
1105 /******************************************************************************
1106 * dvb_demux kernel data API calls
1107 ******************************************************************************/
1109 static int dvbdmx_open(struct dmx_demux *demux)
1111 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1113 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1114 return -EUSERS;
1116 dvbdemux->users++;
1117 return 0;
1120 static int dvbdmx_close(struct dmx_demux *demux)
1122 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1124 if (dvbdemux->users == 0)
1125 return -ENODEV;
1127 dvbdemux->users--;
1128 //FIXME: release any unneeded resources if users==0
1129 return 0;
1132 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1134 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1135 void *p;
1137 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1138 return -EINVAL;
1140 p = memdup_user(buf, count);
1141 if (IS_ERR(p))
1142 return PTR_ERR(p);
1143 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1144 kfree(p);
1145 return -ERESTARTSYS;
1147 dvb_dmx_swfilter(dvbdemux, p, count);
1148 kfree(p);
1149 mutex_unlock(&dvbdemux->mutex);
1151 if (signal_pending(current))
1152 return -EINTR;
1153 return count;
1156 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1157 struct dmx_frontend *frontend)
1159 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1160 struct list_head *head = &dvbdemux->frontend_list;
1162 list_add(&(frontend->connectivity_list), head);
1164 return 0;
1167 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1168 struct dmx_frontend *frontend)
1170 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1171 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1173 list_for_each_safe(pos, n, head) {
1174 if (DMX_FE_ENTRY(pos) == frontend) {
1175 list_del(pos);
1176 return 0;
1180 return -ENODEV;
1183 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1185 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1187 if (list_empty(&dvbdemux->frontend_list))
1188 return NULL;
1190 return &dvbdemux->frontend_list;
1193 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1194 struct dmx_frontend *frontend)
1196 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1198 if (demux->frontend)
1199 return -EINVAL;
1201 mutex_lock(&dvbdemux->mutex);
1203 demux->frontend = frontend;
1204 mutex_unlock(&dvbdemux->mutex);
1205 return 0;
1208 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1210 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1212 mutex_lock(&dvbdemux->mutex);
1214 demux->frontend = NULL;
1215 mutex_unlock(&dvbdemux->mutex);
1216 return 0;
1219 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1221 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1223 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1224 return 0;
1227 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1229 int i;
1230 struct dmx_demux *dmx = &dvbdemux->dmx;
1232 dvbdemux->cnt_storage = NULL;
1233 dvbdemux->users = 0;
1234 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1236 if (!dvbdemux->filter)
1237 return -ENOMEM;
1239 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1240 if (!dvbdemux->feed) {
1241 vfree(dvbdemux->filter);
1242 dvbdemux->filter = NULL;
1243 return -ENOMEM;
1245 for (i = 0; i < dvbdemux->filternum; i++) {
1246 dvbdemux->filter[i].state = DMX_STATE_FREE;
1247 dvbdemux->filter[i].index = i;
1249 for (i = 0; i < dvbdemux->feednum; i++) {
1250 dvbdemux->feed[i].state = DMX_STATE_FREE;
1251 dvbdemux->feed[i].index = i;
1254 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1255 if (!dvbdemux->cnt_storage)
1256 printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
1258 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1260 for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1261 dvbdemux->pesfilter[i] = NULL;
1262 dvbdemux->pids[i] = 0xffff;
1265 INIT_LIST_HEAD(&dvbdemux->feed_list);
1267 dvbdemux->playing = 0;
1268 dvbdemux->recording = 0;
1269 dvbdemux->tsbufp = 0;
1271 if (!dvbdemux->check_crc32)
1272 dvbdemux->check_crc32 = dvb_dmx_crc32;
1274 if (!dvbdemux->memcopy)
1275 dvbdemux->memcopy = dvb_dmx_memcopy;
1277 dmx->frontend = NULL;
1278 dmx->priv = dvbdemux;
1279 dmx->open = dvbdmx_open;
1280 dmx->close = dvbdmx_close;
1281 dmx->write = dvbdmx_write;
1282 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1283 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1284 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1285 dmx->release_section_feed = dvbdmx_release_section_feed;
1287 dmx->add_frontend = dvbdmx_add_frontend;
1288 dmx->remove_frontend = dvbdmx_remove_frontend;
1289 dmx->get_frontends = dvbdmx_get_frontends;
1290 dmx->connect_frontend = dvbdmx_connect_frontend;
1291 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1292 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1294 mutex_init(&dvbdemux->mutex);
1295 spin_lock_init(&dvbdemux->lock);
1297 return 0;
1300 EXPORT_SYMBOL(dvb_dmx_init);
1302 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1304 vfree(dvbdemux->cnt_storage);
1305 vfree(dvbdemux->filter);
1306 vfree(dvbdemux->feed);
1309 EXPORT_SYMBOL(dvb_dmx_release);