sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-core / dvb_demux.c
blobbbbff72bbb2af53c2fe255b7e0bc2836558edeed
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 #define pr_fmt(fmt) "dvb_demux: " fmt
26 #include <linux/sched.h>
27 #include <linux/spinlock.h>
28 #include <linux/slab.h>
29 #include <linux/vmalloc.h>
30 #include <linux/module.h>
31 #include <linux/poll.h>
32 #include <linux/string.h>
33 #include <linux/crc32.h>
34 #include <linux/uaccess.h>
35 #include <asm/div64.h>
37 #include "dvb_demux.h"
39 static int dvb_demux_tscheck;
40 module_param(dvb_demux_tscheck, int, 0644);
41 MODULE_PARM_DESC(dvb_demux_tscheck,
42 "enable transport stream continuity and TEI check");
44 static int dvb_demux_speedcheck;
45 module_param(dvb_demux_speedcheck, int, 0644);
46 MODULE_PARM_DESC(dvb_demux_speedcheck,
47 "enable transport stream speed check");
49 static int dvb_demux_feed_err_pkts = 1;
50 module_param(dvb_demux_feed_err_pkts, int, 0644);
51 MODULE_PARM_DESC(dvb_demux_feed_err_pkts,
52 "when set to 0, drop packets with the TEI bit set (1 by default)");
54 #define dprintk(fmt, arg...) \
55 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
57 #define dprintk_tscheck(x...) do { \
58 if (dvb_demux_tscheck && printk_ratelimit()) \
59 dprintk(x); \
60 } while (0)
62 /******************************************************************************
63 * static inlined helper functions
64 ******************************************************************************/
66 static inline u16 section_length(const u8 *buf)
68 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
71 static inline u16 ts_pid(const u8 *buf)
73 return ((buf[1] & 0x1f) << 8) + buf[2];
76 static inline u8 payload(const u8 *tsp)
78 if (!(tsp[3] & 0x10)) // no payload?
79 return 0;
81 if (tsp[3] & 0x20) { // adaptation field?
82 if (tsp[4] > 183) // corrupted data?
83 return 0;
84 else
85 return 184 - 1 - tsp[4];
88 return 184;
91 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
93 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
96 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
97 size_t len)
99 memcpy(d, s, len);
102 /******************************************************************************
103 * Software filter functions
104 ******************************************************************************/
106 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
107 const u8 *buf)
109 int count = payload(buf);
110 int p;
111 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
112 int ccok;
113 u8 cc;
114 #endif
116 if (count == 0)
117 return -1;
119 p = 188 - count;
121 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
122 cc = buf[3] & 0x0f;
123 ccok = ((feed->cc + 1) & 0x0f) == cc;
124 feed->cc = cc;
125 if (!ccok)
126 dprintk("missed packet!\n");
127 #endif
129 if (buf[1] & 0x40) // PUSI ?
130 feed->peslen = 0xfffa;
132 feed->peslen += count;
134 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts);
137 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
138 struct dvb_demux_filter *f)
140 u8 neq = 0;
141 int i;
143 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
144 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
146 if (f->maskandmode[i] & xor)
147 return 0;
149 neq |= f->maskandnotmode[i] & xor;
152 if (f->doneq && !neq)
153 return 0;
155 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
156 NULL, 0, &f->filter);
159 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
161 struct dvb_demux *demux = feed->demux;
162 struct dvb_demux_filter *f = feed->filter;
163 struct dmx_section_feed *sec = &feed->feed.sec;
164 int section_syntax_indicator;
166 if (!sec->is_filtering)
167 return 0;
169 if (!f)
170 return 0;
172 if (sec->check_crc) {
173 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
174 if (section_syntax_indicator &&
175 demux->check_crc32(feed, sec->secbuf, sec->seclen))
176 return -1;
179 do {
180 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
181 return -1;
182 } while ((f = f->next) && sec->is_filtering);
184 sec->seclen = 0;
186 return 0;
189 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
191 struct dmx_section_feed *sec = &feed->feed.sec;
193 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
194 if (sec->secbufp < sec->tsfeedp) {
195 int i, n = sec->tsfeedp - sec->secbufp;
198 * Section padding is done with 0xff bytes entirely.
199 * Due to speed reasons, we won't check all of them
200 * but just first and last.
202 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
203 dprintk("dvb_demux.c section ts padding loss: %d/%d\n",
204 n, sec->tsfeedp);
205 dprintk("dvb_demux.c pad data:");
206 for (i = 0; i < n; i++)
207 pr_cont(" %02x", sec->secbuf[i]);
208 pr_cont("\n");
211 #endif
213 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
214 sec->secbuf = sec->secbuf_base;
218 * Losless Section Demux 1.4.1 by Emard
219 * Valsecchi Patrick:
220 * - middle of section A (no PUSI)
221 * - end of section A and start of section B
222 * (with PUSI pointing to the start of the second section)
224 * In this case, without feed->pusi_seen you'll receive a garbage section
225 * consisting of the end of section A. Basically because tsfeedp
226 * is incemented and the use=0 condition is not raised
227 * when the second packet arrives.
229 * Fix:
230 * when demux is started, let feed->pusi_seen = 0 to
231 * prevent initial feeding of garbage from the end of
232 * previous section. When you for the first time see PUSI=1
233 * then set feed->pusi_seen = 1
235 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
236 const u8 *buf, u8 len)
238 struct dvb_demux *demux = feed->demux;
239 struct dmx_section_feed *sec = &feed->feed.sec;
240 u16 limit, seclen, n;
242 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
243 return 0;
245 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
246 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
247 dprintk("dvb_demux.c section buffer full loss: %d/%d\n",
248 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
249 DMX_MAX_SECFEED_SIZE);
250 #endif
251 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
254 if (len <= 0)
255 return 0;
257 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
258 sec->tsfeedp += len;
261 * Dump all the sections we can find in the data (Emard)
263 limit = sec->tsfeedp;
264 if (limit > DMX_MAX_SECFEED_SIZE)
265 return -1; /* internal error should never happen */
267 /* to be sure always set secbuf */
268 sec->secbuf = sec->secbuf_base + sec->secbufp;
270 for (n = 0; sec->secbufp + 2 < limit; n++) {
271 seclen = section_length(sec->secbuf);
272 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
273 || seclen + sec->secbufp > limit)
274 return 0;
275 sec->seclen = seclen;
276 sec->crc_val = ~0;
277 /* dump [secbuf .. secbuf+seclen) */
278 if (feed->pusi_seen)
279 dvb_dmx_swfilter_section_feed(feed);
280 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
281 else
282 dprintk("dvb_demux.c pusi not seen, discarding section data\n");
283 #endif
284 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
285 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
288 return 0;
291 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
292 const u8 *buf)
294 u8 p, count;
295 int ccok, dc_i = 0;
296 u8 cc;
298 count = payload(buf);
300 if (count == 0) /* count == 0 if no payload or out of range */
301 return -1;
303 p = 188 - count; /* payload start */
305 cc = buf[3] & 0x0f;
306 ccok = ((feed->cc + 1) & 0x0f) == cc;
307 feed->cc = cc;
309 if (buf[3] & 0x20) {
310 /* adaption field present, check for discontinuity_indicator */
311 if ((buf[4] > 0) && (buf[5] & 0x80))
312 dc_i = 1;
315 if (!ccok || dc_i) {
316 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
317 dprintk("dvb_demux.c discontinuity detected %d bytes lost\n",
318 count);
320 * those bytes under sume circumstances will again be reported
321 * in the following dvb_dmx_swfilter_section_new
323 #endif
325 * Discontinuity detected. Reset pusi_seen = 0 to
326 * stop feeding of suspicious data until next PUSI=1 arrives
328 feed->pusi_seen = 0;
329 dvb_dmx_swfilter_section_new(feed);
332 if (buf[1] & 0x40) {
333 /* PUSI=1 (is set), section boundary is here */
334 if (count > 1 && buf[p] < count) {
335 const u8 *before = &buf[p + 1];
336 u8 before_len = buf[p];
337 const u8 *after = &before[before_len];
338 u8 after_len = count - 1 - before_len;
340 dvb_dmx_swfilter_section_copy_dump(feed, before,
341 before_len);
342 /* before start of new section, set pusi_seen = 1 */
343 feed->pusi_seen = 1;
344 dvb_dmx_swfilter_section_new(feed);
345 dvb_dmx_swfilter_section_copy_dump(feed, after,
346 after_len);
348 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
349 else if (count > 0)
350 dprintk("dvb_demux.c PUSI=1 but %d bytes lost\n",
351 count);
352 #endif
353 } else {
354 /* PUSI=0 (is not set), no section boundary */
355 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
358 return 0;
361 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
362 const u8 *buf)
364 switch (feed->type) {
365 case DMX_TYPE_TS:
366 if (!feed->feed.ts.is_filtering)
367 break;
368 if (feed->ts_type & TS_PACKET) {
369 if (feed->ts_type & TS_PAYLOAD_ONLY)
370 dvb_dmx_swfilter_payload(feed, buf);
371 else
372 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts);
374 if (feed->ts_type & TS_DECODER)
375 if (feed->demux->write_to_decoder)
376 feed->demux->write_to_decoder(feed, buf, 188);
377 break;
379 case DMX_TYPE_SEC:
380 if (!feed->feed.sec.is_filtering)
381 break;
382 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
383 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
384 break;
386 default:
387 break;
391 #define DVR_FEED(f) \
392 (((f)->type == DMX_TYPE_TS) && \
393 ((f)->feed.ts.is_filtering) && \
394 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
396 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
398 struct dvb_demux_feed *feed;
399 u16 pid = ts_pid(buf);
400 int dvr_done = 0;
402 if (dvb_demux_speedcheck) {
403 ktime_t cur_time;
404 u64 speed_bytes, speed_timedelta;
406 demux->speed_pkts_cnt++;
408 /* show speed every SPEED_PKTS_INTERVAL packets */
409 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
410 cur_time = ktime_get();
412 if (ktime_to_ns(demux->speed_last_time) != 0) {
413 speed_bytes = (u64)demux->speed_pkts_cnt
414 * 188 * 8;
415 /* convert to 1024 basis */
416 speed_bytes = 1000 * div64_u64(speed_bytes,
417 1024);
418 speed_timedelta = ktime_ms_delta(cur_time,
419 demux->speed_last_time);
420 dprintk("TS speed %llu Kbits/sec \n",
421 div64_u64(speed_bytes,
422 speed_timedelta));
425 demux->speed_last_time = cur_time;
426 demux->speed_pkts_cnt = 0;
430 if (buf[1] & 0x80) {
431 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
432 pid, buf[1]);
433 /* data in this packet can't be trusted - drop it unless
434 * module option dvb_demux_feed_err_pkts is set */
435 if (!dvb_demux_feed_err_pkts)
436 return;
437 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
438 if (demux->cnt_storage && dvb_demux_tscheck) {
439 /* check pkt counter */
440 if (pid < MAX_PID) {
441 if (buf[3] & 0x10)
442 demux->cnt_storage[pid] =
443 (demux->cnt_storage[pid] + 1) & 0xf;
445 if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
446 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
447 pid, demux->cnt_storage[pid],
448 buf[3] & 0xf);
449 demux->cnt_storage[pid] = buf[3] & 0xf;
452 /* end check */
455 list_for_each_entry(feed, &demux->feed_list, list_head) {
456 if ((feed->pid != pid) && (feed->pid != 0x2000))
457 continue;
459 /* copy each packet only once to the dvr device, even
460 * if a PID is in multiple filters (e.g. video + PCR) */
461 if ((DVR_FEED(feed)) && (dvr_done++))
462 continue;
464 if (feed->pid == pid)
465 dvb_dmx_swfilter_packet_type(feed, buf);
466 else if (feed->pid == 0x2000)
467 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts);
471 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
472 size_t count)
474 unsigned long flags;
476 spin_lock_irqsave(&demux->lock, flags);
478 while (count--) {
479 if (buf[0] == 0x47)
480 dvb_dmx_swfilter_packet(demux, buf);
481 buf += 188;
484 spin_unlock_irqrestore(&demux->lock, flags);
487 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
489 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
490 const int pktsize)
492 int start = pos, lost;
494 while (pos < count) {
495 if (buf[pos] == 0x47 ||
496 (pktsize == 204 && buf[pos] == 0xB8))
497 break;
498 pos++;
501 lost = pos - start;
502 if (lost) {
503 /* This garbage is part of a valid packet? */
504 int backtrack = pos - pktsize;
505 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
506 (pktsize == 204 && buf[backtrack] == 0xB8)))
507 return backtrack;
510 return pos;
513 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
514 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
515 size_t count, const int pktsize)
517 int p = 0, i, j;
518 const u8 *q;
519 unsigned long flags;
521 spin_lock_irqsave(&demux->lock, flags);
523 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
524 i = demux->tsbufp;
525 j = pktsize - i;
526 if (count < j) {
527 memcpy(&demux->tsbuf[i], buf, count);
528 demux->tsbufp += count;
529 goto bailout;
531 memcpy(&demux->tsbuf[i], buf, j);
532 if (demux->tsbuf[0] == 0x47) /* double check */
533 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
534 demux->tsbufp = 0;
535 p += j;
538 while (1) {
539 p = find_next_packet(buf, p, count, pktsize);
540 if (p >= count)
541 break;
542 if (count - p < pktsize)
543 break;
545 q = &buf[p];
547 if (pktsize == 204 && (*q == 0xB8)) {
548 memcpy(demux->tsbuf, q, 188);
549 demux->tsbuf[0] = 0x47;
550 q = demux->tsbuf;
552 dvb_dmx_swfilter_packet(demux, q);
553 p += pktsize;
556 i = count - p;
557 if (i) {
558 memcpy(demux->tsbuf, &buf[p], i);
559 demux->tsbufp = i;
560 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
561 demux->tsbuf[0] = 0x47;
564 bailout:
565 spin_unlock_irqrestore(&demux->lock, flags);
568 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
570 _dvb_dmx_swfilter(demux, buf, count, 188);
572 EXPORT_SYMBOL(dvb_dmx_swfilter);
574 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
576 _dvb_dmx_swfilter(demux, buf, count, 204);
578 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
580 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
582 unsigned long flags;
584 spin_lock_irqsave(&demux->lock, flags);
586 demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts);
588 spin_unlock_irqrestore(&demux->lock, flags);
590 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
592 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
594 int i;
596 for (i = 0; i < demux->filternum; i++)
597 if (demux->filter[i].state == DMX_STATE_FREE)
598 break;
600 if (i == demux->filternum)
601 return NULL;
603 demux->filter[i].state = DMX_STATE_ALLOCATED;
605 return &demux->filter[i];
608 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
610 int i;
612 for (i = 0; i < demux->feednum; i++)
613 if (demux->feed[i].state == DMX_STATE_FREE)
614 break;
616 if (i == demux->feednum)
617 return NULL;
619 demux->feed[i].state = DMX_STATE_ALLOCATED;
621 return &demux->feed[i];
624 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
626 struct dvb_demux_feed *entry;
628 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
629 if (entry == feed)
630 return 1;
632 return 0;
635 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
637 spin_lock_irq(&feed->demux->lock);
638 if (dvb_demux_feed_find(feed)) {
639 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
640 __func__, feed->type, feed->state, feed->pid);
641 goto out;
644 list_add(&feed->list_head, &feed->demux->feed_list);
645 out:
646 spin_unlock_irq(&feed->demux->lock);
649 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
651 spin_lock_irq(&feed->demux->lock);
652 if (!(dvb_demux_feed_find(feed))) {
653 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
654 __func__, feed->type, feed->state, feed->pid);
655 goto out;
658 list_del(&feed->list_head);
659 out:
660 spin_unlock_irq(&feed->demux->lock);
663 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
664 enum dmx_ts_pes pes_type, ktime_t timeout)
666 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
667 struct dvb_demux *demux = feed->demux;
669 if (pid > DMX_MAX_PID)
670 return -EINVAL;
672 if (mutex_lock_interruptible(&demux->mutex))
673 return -ERESTARTSYS;
675 if (ts_type & TS_DECODER) {
676 if (pes_type >= DMX_PES_OTHER) {
677 mutex_unlock(&demux->mutex);
678 return -EINVAL;
681 if (demux->pesfilter[pes_type] &&
682 demux->pesfilter[pes_type] != feed) {
683 mutex_unlock(&demux->mutex);
684 return -EINVAL;
687 demux->pesfilter[pes_type] = feed;
688 demux->pids[pes_type] = pid;
691 dvb_demux_feed_add(feed);
693 feed->pid = pid;
694 feed->timeout = timeout;
695 feed->ts_type = ts_type;
696 feed->pes_type = pes_type;
698 feed->state = DMX_STATE_READY;
699 mutex_unlock(&demux->mutex);
701 return 0;
704 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
706 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
707 struct dvb_demux *demux = feed->demux;
708 int ret;
710 if (mutex_lock_interruptible(&demux->mutex))
711 return -ERESTARTSYS;
713 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
714 mutex_unlock(&demux->mutex);
715 return -EINVAL;
718 if (!demux->start_feed) {
719 mutex_unlock(&demux->mutex);
720 return -ENODEV;
723 if ((ret = demux->start_feed(feed)) < 0) {
724 mutex_unlock(&demux->mutex);
725 return ret;
728 spin_lock_irq(&demux->lock);
729 ts_feed->is_filtering = 1;
730 feed->state = DMX_STATE_GO;
731 spin_unlock_irq(&demux->lock);
732 mutex_unlock(&demux->mutex);
734 return 0;
737 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
739 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
740 struct dvb_demux *demux = feed->demux;
741 int ret;
743 mutex_lock(&demux->mutex);
745 if (feed->state < DMX_STATE_GO) {
746 mutex_unlock(&demux->mutex);
747 return -EINVAL;
750 if (!demux->stop_feed) {
751 mutex_unlock(&demux->mutex);
752 return -ENODEV;
755 ret = demux->stop_feed(feed);
757 spin_lock_irq(&demux->lock);
758 ts_feed->is_filtering = 0;
759 feed->state = DMX_STATE_ALLOCATED;
760 spin_unlock_irq(&demux->lock);
761 mutex_unlock(&demux->mutex);
763 return ret;
766 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
767 struct dmx_ts_feed **ts_feed,
768 dmx_ts_cb callback)
770 struct dvb_demux *demux = (struct dvb_demux *)dmx;
771 struct dvb_demux_feed *feed;
773 if (mutex_lock_interruptible(&demux->mutex))
774 return -ERESTARTSYS;
776 if (!(feed = dvb_dmx_feed_alloc(demux))) {
777 mutex_unlock(&demux->mutex);
778 return -EBUSY;
781 feed->type = DMX_TYPE_TS;
782 feed->cb.ts = callback;
783 feed->demux = demux;
784 feed->pid = 0xffff;
785 feed->peslen = 0xfffa;
787 (*ts_feed) = &feed->feed.ts;
788 (*ts_feed)->parent = dmx;
789 (*ts_feed)->priv = NULL;
790 (*ts_feed)->is_filtering = 0;
791 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
792 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
793 (*ts_feed)->set = dmx_ts_feed_set;
795 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
796 feed->state = DMX_STATE_FREE;
797 mutex_unlock(&demux->mutex);
798 return -EBUSY;
801 feed->filter->type = DMX_TYPE_TS;
802 feed->filter->feed = feed;
803 feed->filter->state = DMX_STATE_READY;
805 mutex_unlock(&demux->mutex);
807 return 0;
810 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
811 struct dmx_ts_feed *ts_feed)
813 struct dvb_demux *demux = (struct dvb_demux *)dmx;
814 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
816 mutex_lock(&demux->mutex);
818 if (feed->state == DMX_STATE_FREE) {
819 mutex_unlock(&demux->mutex);
820 return -EINVAL;
823 feed->state = DMX_STATE_FREE;
824 feed->filter->state = DMX_STATE_FREE;
826 dvb_demux_feed_del(feed);
828 feed->pid = 0xffff;
830 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
831 demux->pesfilter[feed->pes_type] = NULL;
833 mutex_unlock(&demux->mutex);
834 return 0;
837 /******************************************************************************
838 * dmx_section_feed API calls
839 ******************************************************************************/
841 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
842 struct dmx_section_filter **filter)
844 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
845 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
846 struct dvb_demux_filter *dvbdmxfilter;
848 if (mutex_lock_interruptible(&dvbdemux->mutex))
849 return -ERESTARTSYS;
851 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
852 if (!dvbdmxfilter) {
853 mutex_unlock(&dvbdemux->mutex);
854 return -EBUSY;
857 spin_lock_irq(&dvbdemux->lock);
858 *filter = &dvbdmxfilter->filter;
859 (*filter)->parent = feed;
860 (*filter)->priv = NULL;
861 dvbdmxfilter->feed = dvbdmxfeed;
862 dvbdmxfilter->type = DMX_TYPE_SEC;
863 dvbdmxfilter->state = DMX_STATE_READY;
864 dvbdmxfilter->next = dvbdmxfeed->filter;
865 dvbdmxfeed->filter = dvbdmxfilter;
866 spin_unlock_irq(&dvbdemux->lock);
868 mutex_unlock(&dvbdemux->mutex);
869 return 0;
872 static int dmx_section_feed_set(struct dmx_section_feed *feed,
873 u16 pid, int check_crc)
875 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
876 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
878 if (pid > 0x1fff)
879 return -EINVAL;
881 if (mutex_lock_interruptible(&dvbdmx->mutex))
882 return -ERESTARTSYS;
884 dvb_demux_feed_add(dvbdmxfeed);
886 dvbdmxfeed->pid = pid;
887 dvbdmxfeed->feed.sec.check_crc = check_crc;
889 dvbdmxfeed->state = DMX_STATE_READY;
890 mutex_unlock(&dvbdmx->mutex);
891 return 0;
894 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
896 int i;
897 struct dvb_demux_filter *f;
898 struct dmx_section_filter *sf;
899 u8 mask, mode, doneq;
901 if (!(f = dvbdmxfeed->filter))
902 return;
903 do {
904 sf = &f->filter;
905 doneq = 0;
906 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
907 mode = sf->filter_mode[i];
908 mask = sf->filter_mask[i];
909 f->maskandmode[i] = mask & mode;
910 doneq |= f->maskandnotmode[i] = mask & ~mode;
912 f->doneq = doneq ? 1 : 0;
913 } while ((f = f->next));
916 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
918 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
919 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
920 int ret;
922 if (mutex_lock_interruptible(&dvbdmx->mutex))
923 return -ERESTARTSYS;
925 if (feed->is_filtering) {
926 mutex_unlock(&dvbdmx->mutex);
927 return -EBUSY;
930 if (!dvbdmxfeed->filter) {
931 mutex_unlock(&dvbdmx->mutex);
932 return -EINVAL;
935 dvbdmxfeed->feed.sec.tsfeedp = 0;
936 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
937 dvbdmxfeed->feed.sec.secbufp = 0;
938 dvbdmxfeed->feed.sec.seclen = 0;
940 if (!dvbdmx->start_feed) {
941 mutex_unlock(&dvbdmx->mutex);
942 return -ENODEV;
945 prepare_secfilters(dvbdmxfeed);
947 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
948 mutex_unlock(&dvbdmx->mutex);
949 return ret;
952 spin_lock_irq(&dvbdmx->lock);
953 feed->is_filtering = 1;
954 dvbdmxfeed->state = DMX_STATE_GO;
955 spin_unlock_irq(&dvbdmx->lock);
957 mutex_unlock(&dvbdmx->mutex);
958 return 0;
961 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
963 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
964 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
965 int ret;
967 mutex_lock(&dvbdmx->mutex);
969 if (!dvbdmx->stop_feed) {
970 mutex_unlock(&dvbdmx->mutex);
971 return -ENODEV;
974 ret = dvbdmx->stop_feed(dvbdmxfeed);
976 spin_lock_irq(&dvbdmx->lock);
977 dvbdmxfeed->state = DMX_STATE_READY;
978 feed->is_filtering = 0;
979 spin_unlock_irq(&dvbdmx->lock);
981 mutex_unlock(&dvbdmx->mutex);
982 return ret;
985 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
986 struct dmx_section_filter *filter)
988 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
989 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
990 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
992 mutex_lock(&dvbdmx->mutex);
994 if (dvbdmxfilter->feed != dvbdmxfeed) {
995 mutex_unlock(&dvbdmx->mutex);
996 return -EINVAL;
999 if (feed->is_filtering) {
1000 /* release dvbdmx->mutex as far as it is
1001 acquired by stop_filtering() itself */
1002 mutex_unlock(&dvbdmx->mutex);
1003 feed->stop_filtering(feed);
1004 mutex_lock(&dvbdmx->mutex);
1007 spin_lock_irq(&dvbdmx->lock);
1008 f = dvbdmxfeed->filter;
1010 if (f == dvbdmxfilter) {
1011 dvbdmxfeed->filter = dvbdmxfilter->next;
1012 } else {
1013 while (f->next != dvbdmxfilter)
1014 f = f->next;
1015 f->next = f->next->next;
1018 dvbdmxfilter->state = DMX_STATE_FREE;
1019 spin_unlock_irq(&dvbdmx->lock);
1020 mutex_unlock(&dvbdmx->mutex);
1021 return 0;
1024 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1025 struct dmx_section_feed **feed,
1026 dmx_section_cb callback)
1028 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1029 struct dvb_demux_feed *dvbdmxfeed;
1031 if (mutex_lock_interruptible(&dvbdmx->mutex))
1032 return -ERESTARTSYS;
1034 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1035 mutex_unlock(&dvbdmx->mutex);
1036 return -EBUSY;
1039 dvbdmxfeed->type = DMX_TYPE_SEC;
1040 dvbdmxfeed->cb.sec = callback;
1041 dvbdmxfeed->demux = dvbdmx;
1042 dvbdmxfeed->pid = 0xffff;
1043 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1044 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1045 dvbdmxfeed->feed.sec.tsfeedp = 0;
1046 dvbdmxfeed->filter = NULL;
1048 (*feed) = &dvbdmxfeed->feed.sec;
1049 (*feed)->is_filtering = 0;
1050 (*feed)->parent = demux;
1051 (*feed)->priv = NULL;
1053 (*feed)->set = dmx_section_feed_set;
1054 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1055 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1056 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1057 (*feed)->release_filter = dmx_section_feed_release_filter;
1059 mutex_unlock(&dvbdmx->mutex);
1060 return 0;
1063 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1064 struct dmx_section_feed *feed)
1066 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1067 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1069 mutex_lock(&dvbdmx->mutex);
1071 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1072 mutex_unlock(&dvbdmx->mutex);
1073 return -EINVAL;
1075 dvbdmxfeed->state = DMX_STATE_FREE;
1077 dvb_demux_feed_del(dvbdmxfeed);
1079 dvbdmxfeed->pid = 0xffff;
1081 mutex_unlock(&dvbdmx->mutex);
1082 return 0;
1085 /******************************************************************************
1086 * dvb_demux kernel data API calls
1087 ******************************************************************************/
1089 static int dvbdmx_open(struct dmx_demux *demux)
1091 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1093 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1094 return -EUSERS;
1096 dvbdemux->users++;
1097 return 0;
1100 static int dvbdmx_close(struct dmx_demux *demux)
1102 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1104 if (dvbdemux->users == 0)
1105 return -ENODEV;
1107 dvbdemux->users--;
1108 //FIXME: release any unneeded resources if users==0
1109 return 0;
1112 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1114 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1115 void *p;
1117 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1118 return -EINVAL;
1120 p = memdup_user(buf, count);
1121 if (IS_ERR(p))
1122 return PTR_ERR(p);
1123 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1124 kfree(p);
1125 return -ERESTARTSYS;
1127 dvb_dmx_swfilter(dvbdemux, p, count);
1128 kfree(p);
1129 mutex_unlock(&dvbdemux->mutex);
1131 if (signal_pending(current))
1132 return -EINTR;
1133 return count;
1136 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1137 struct dmx_frontend *frontend)
1139 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1140 struct list_head *head = &dvbdemux->frontend_list;
1142 list_add(&(frontend->connectivity_list), head);
1144 return 0;
1147 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1148 struct dmx_frontend *frontend)
1150 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1151 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1153 list_for_each_safe(pos, n, head) {
1154 if (DMX_FE_ENTRY(pos) == frontend) {
1155 list_del(pos);
1156 return 0;
1160 return -ENODEV;
1163 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1165 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1167 if (list_empty(&dvbdemux->frontend_list))
1168 return NULL;
1170 return &dvbdemux->frontend_list;
1173 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1174 struct dmx_frontend *frontend)
1176 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1178 if (demux->frontend)
1179 return -EINVAL;
1181 mutex_lock(&dvbdemux->mutex);
1183 demux->frontend = frontend;
1184 mutex_unlock(&dvbdemux->mutex);
1185 return 0;
1188 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1190 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1192 mutex_lock(&dvbdemux->mutex);
1194 demux->frontend = NULL;
1195 mutex_unlock(&dvbdemux->mutex);
1196 return 0;
1199 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1201 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1203 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1204 return 0;
1207 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1209 int i;
1210 struct dmx_demux *dmx = &dvbdemux->dmx;
1212 dvbdemux->cnt_storage = NULL;
1213 dvbdemux->users = 0;
1214 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1216 if (!dvbdemux->filter)
1217 return -ENOMEM;
1219 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1220 if (!dvbdemux->feed) {
1221 vfree(dvbdemux->filter);
1222 dvbdemux->filter = NULL;
1223 return -ENOMEM;
1225 for (i = 0; i < dvbdemux->filternum; i++) {
1226 dvbdemux->filter[i].state = DMX_STATE_FREE;
1227 dvbdemux->filter[i].index = i;
1229 for (i = 0; i < dvbdemux->feednum; i++) {
1230 dvbdemux->feed[i].state = DMX_STATE_FREE;
1231 dvbdemux->feed[i].index = i;
1234 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1235 if (!dvbdemux->cnt_storage)
1236 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1238 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1240 for (i = 0; i < DMX_PES_OTHER; i++) {
1241 dvbdemux->pesfilter[i] = NULL;
1242 dvbdemux->pids[i] = 0xffff;
1245 INIT_LIST_HEAD(&dvbdemux->feed_list);
1247 dvbdemux->playing = 0;
1248 dvbdemux->recording = 0;
1249 dvbdemux->tsbufp = 0;
1251 if (!dvbdemux->check_crc32)
1252 dvbdemux->check_crc32 = dvb_dmx_crc32;
1254 if (!dvbdemux->memcopy)
1255 dvbdemux->memcopy = dvb_dmx_memcopy;
1257 dmx->frontend = NULL;
1258 dmx->priv = dvbdemux;
1259 dmx->open = dvbdmx_open;
1260 dmx->close = dvbdmx_close;
1261 dmx->write = dvbdmx_write;
1262 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1263 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1264 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1265 dmx->release_section_feed = dvbdmx_release_section_feed;
1267 dmx->add_frontend = dvbdmx_add_frontend;
1268 dmx->remove_frontend = dvbdmx_remove_frontend;
1269 dmx->get_frontends = dvbdmx_get_frontends;
1270 dmx->connect_frontend = dvbdmx_connect_frontend;
1271 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1272 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1274 mutex_init(&dvbdemux->mutex);
1275 spin_lock_init(&dvbdemux->lock);
1277 return 0;
1280 EXPORT_SYMBOL(dvb_dmx_init);
1282 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1284 vfree(dvbdemux->cnt_storage);
1285 vfree(dvbdemux->filter);
1286 vfree(dvbdemux->feed);
1289 EXPORT_SYMBOL(dvb_dmx_release);