Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / media / dvb-core / dvb_demux.c
blob5fde1d38b3e3454a4fe87055c38ecd71e898d3df
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.
20 #define pr_fmt(fmt) "dvb_demux: " fmt
22 #include <linux/sched/signal.h>
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
28 #include <linux/string.h>
29 #include <linux/crc32.h>
30 #include <linux/uaccess.h>
31 #include <asm/div64.h>
33 #include <media/dvb_demux.h>
35 static int dvb_demux_tscheck;
36 module_param(dvb_demux_tscheck, int, 0644);
37 MODULE_PARM_DESC(dvb_demux_tscheck,
38 "enable transport stream continuity and TEI check");
40 static int dvb_demux_speedcheck;
41 module_param(dvb_demux_speedcheck, int, 0644);
42 MODULE_PARM_DESC(dvb_demux_speedcheck,
43 "enable transport stream speed check");
45 static int dvb_demux_feed_err_pkts = 1;
46 module_param(dvb_demux_feed_err_pkts, int, 0644);
47 MODULE_PARM_DESC(dvb_demux_feed_err_pkts,
48 "when set to 0, drop packets with the TEI bit set (1 by default)");
50 #define dprintk(fmt, arg...) \
51 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg)
53 #define dprintk_tscheck(x...) do { \
54 if (dvb_demux_tscheck && printk_ratelimit()) \
55 dprintk(x); \
56 } while (0)
58 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
59 # define dprintk_sect_loss(x...) dprintk(x)
60 #else
61 # define dprintk_sect_loss(x...)
62 #endif
64 #define set_buf_flags(__feed, __flag) \
65 do { \
66 (__feed)->buffer_flags |= (__flag); \
67 } while (0)
69 /******************************************************************************
70 * static inlined helper functions
71 ******************************************************************************/
73 static inline u16 section_length(const u8 *buf)
75 return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
78 static inline u16 ts_pid(const u8 *buf)
80 return ((buf[1] & 0x1f) << 8) + buf[2];
83 static inline u8 payload(const u8 *tsp)
85 if (!(tsp[3] & 0x10)) // no payload?
86 return 0;
88 if (tsp[3] & 0x20) { // adaptation field?
89 if (tsp[4] > 183) // corrupted data?
90 return 0;
91 else
92 return 184 - 1 - tsp[4];
95 return 184;
98 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
100 return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
103 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
104 size_t len)
106 memcpy(d, s, len);
109 /******************************************************************************
110 * Software filter functions
111 ******************************************************************************/
113 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
114 const u8 *buf)
116 int count = payload(buf);
117 int p;
118 int ccok;
119 u8 cc;
121 if (count == 0)
122 return -1;
124 p = 188 - count;
126 cc = buf[3] & 0x0f;
127 ccok = ((feed->cc + 1) & 0x0f) == cc;
128 feed->cc = cc;
129 if (!ccok) {
130 set_buf_flags(feed, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
131 dprintk_sect_loss("missed packet: %d instead of %d!\n",
132 cc, (feed->cc + 1) & 0x0f);
135 if (buf[1] & 0x40) // PUSI ?
136 feed->peslen = 0xfffa;
138 feed->peslen += count;
140 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts,
141 &feed->buffer_flags);
144 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
145 struct dvb_demux_filter *f)
147 u8 neq = 0;
148 int i;
150 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
151 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
153 if (f->maskandmode[i] & xor)
154 return 0;
156 neq |= f->maskandnotmode[i] & xor;
159 if (f->doneq && !neq)
160 return 0;
162 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
163 NULL, 0, &f->filter, &feed->buffer_flags);
166 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
168 struct dvb_demux *demux = feed->demux;
169 struct dvb_demux_filter *f = feed->filter;
170 struct dmx_section_feed *sec = &feed->feed.sec;
171 int section_syntax_indicator;
173 if (!sec->is_filtering)
174 return 0;
176 if (!f)
177 return 0;
179 if (sec->check_crc) {
180 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
181 if (section_syntax_indicator &&
182 demux->check_crc32(feed, sec->secbuf, sec->seclen)) {
183 set_buf_flags(feed, DMX_BUFFER_FLAG_HAD_CRC32_DISCARD);
184 return -1;
188 do {
189 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
190 return -1;
191 } while ((f = f->next) && sec->is_filtering);
193 sec->seclen = 0;
195 return 0;
198 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
200 struct dmx_section_feed *sec = &feed->feed.sec;
202 if (sec->secbufp < sec->tsfeedp) {
203 int n = sec->tsfeedp - sec->secbufp;
206 * Section padding is done with 0xff bytes entirely.
207 * Due to speed reasons, we won't check all of them
208 * but just first and last.
210 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
211 set_buf_flags(feed,
212 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
213 dprintk_sect_loss("section ts padding loss: %d/%d\n",
214 n, sec->tsfeedp);
215 dprintk_sect_loss("pad data: %*ph\n", n, sec->secbuf);
219 sec->tsfeedp = sec->secbufp = sec->seclen = 0;
220 sec->secbuf = sec->secbuf_base;
224 * Losless Section Demux 1.4.1 by Emard
225 * Valsecchi Patrick:
226 * - middle of section A (no PUSI)
227 * - end of section A and start of section B
228 * (with PUSI pointing to the start of the second section)
230 * In this case, without feed->pusi_seen you'll receive a garbage section
231 * consisting of the end of section A. Basically because tsfeedp
232 * is incemented and the use=0 condition is not raised
233 * when the second packet arrives.
235 * Fix:
236 * when demux is started, let feed->pusi_seen = false to
237 * prevent initial feeding of garbage from the end of
238 * previous section. When you for the first time see PUSI=1
239 * then set feed->pusi_seen = true
241 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
242 const u8 *buf, u8 len)
244 struct dvb_demux *demux = feed->demux;
245 struct dmx_section_feed *sec = &feed->feed.sec;
246 u16 limit, seclen, n;
248 if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
249 return 0;
251 if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
252 set_buf_flags(feed, DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
253 dprintk_sect_loss("section buffer full loss: %d/%d\n",
254 sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
255 DMX_MAX_SECFEED_SIZE);
256 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
259 if (len <= 0)
260 return 0;
262 demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
263 sec->tsfeedp += len;
266 * Dump all the sections we can find in the data (Emard)
268 limit = sec->tsfeedp;
269 if (limit > DMX_MAX_SECFEED_SIZE)
270 return -1; /* internal error should never happen */
272 /* to be sure always set secbuf */
273 sec->secbuf = sec->secbuf_base + sec->secbufp;
275 for (n = 0; sec->secbufp + 2 < limit; n++) {
276 seclen = section_length(sec->secbuf);
277 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
278 || seclen + sec->secbufp > limit)
279 return 0;
280 sec->seclen = seclen;
281 sec->crc_val = ~0;
282 /* dump [secbuf .. secbuf+seclen) */
283 if (feed->pusi_seen) {
284 dvb_dmx_swfilter_section_feed(feed);
285 } else {
286 set_buf_flags(feed,
287 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
288 dprintk_sect_loss("pusi not seen, discarding section data\n");
290 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
291 sec->secbuf += seclen; /* redundant but saves pointer arithmetic */
294 return 0;
297 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
298 const u8 *buf)
300 u8 p, count;
301 int ccok, dc_i = 0;
302 u8 cc;
304 count = payload(buf);
306 if (count == 0) /* count == 0 if no payload or out of range */
307 return -1;
309 p = 188 - count; /* payload start */
311 cc = buf[3] & 0x0f;
312 ccok = ((feed->cc + 1) & 0x0f) == cc;
313 feed->cc = cc;
315 if (buf[3] & 0x20) {
316 /* adaption field present, check for discontinuity_indicator */
317 if ((buf[4] > 0) && (buf[5] & 0x80))
318 dc_i = 1;
321 if (!ccok || dc_i) {
322 if (dc_i) {
323 set_buf_flags(feed,
324 DMX_BUFFER_FLAG_DISCONTINUITY_INDICATOR);
325 dprintk_sect_loss("%d frame with disconnect indicator\n",
326 cc);
327 } else {
328 set_buf_flags(feed,
329 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
330 dprintk_sect_loss("discontinuity: %d instead of %d. %d bytes lost\n",
331 cc, (feed->cc + 1) & 0x0f, count + 4);
334 * those bytes under some circumstances will again be reported
335 * in the following dvb_dmx_swfilter_section_new
339 * Discontinuity detected. Reset pusi_seen to
340 * stop feeding of suspicious data until next PUSI=1 arrives
342 * FIXME: does it make sense if the MPEG-TS is the one
343 * reporting discontinuity?
346 feed->pusi_seen = false;
347 dvb_dmx_swfilter_section_new(feed);
350 if (buf[1] & 0x40) {
351 /* PUSI=1 (is set), section boundary is here */
352 if (count > 1 && buf[p] < count) {
353 const u8 *before = &buf[p + 1];
354 u8 before_len = buf[p];
355 const u8 *after = &before[before_len];
356 u8 after_len = count - 1 - before_len;
358 dvb_dmx_swfilter_section_copy_dump(feed, before,
359 before_len);
360 /* before start of new section, set pusi_seen */
361 feed->pusi_seen = true;
362 dvb_dmx_swfilter_section_new(feed);
363 dvb_dmx_swfilter_section_copy_dump(feed, after,
364 after_len);
365 } else if (count > 0) {
366 set_buf_flags(feed,
367 DMX_BUFFER_FLAG_DISCONTINUITY_DETECTED);
368 dprintk_sect_loss("PUSI=1 but %d bytes lost\n", count);
370 } else {
371 /* PUSI=0 (is not set), no section boundary */
372 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
375 return 0;
378 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
379 const u8 *buf)
381 switch (feed->type) {
382 case DMX_TYPE_TS:
383 if (!feed->feed.ts.is_filtering)
384 break;
385 if (feed->ts_type & TS_PACKET) {
386 if (feed->ts_type & TS_PAYLOAD_ONLY)
387 dvb_dmx_swfilter_payload(feed, buf);
388 else
389 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
390 &feed->buffer_flags);
392 /* Used only on full-featured devices */
393 if (feed->ts_type & TS_DECODER)
394 if (feed->demux->write_to_decoder)
395 feed->demux->write_to_decoder(feed, buf, 188);
396 break;
398 case DMX_TYPE_SEC:
399 if (!feed->feed.sec.is_filtering)
400 break;
401 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
402 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
403 break;
405 default:
406 break;
410 #define DVR_FEED(f) \
411 (((f)->type == DMX_TYPE_TS) && \
412 ((f)->feed.ts.is_filtering) && \
413 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
415 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
417 struct dvb_demux_feed *feed;
418 u16 pid = ts_pid(buf);
419 int dvr_done = 0;
421 if (dvb_demux_speedcheck) {
422 ktime_t cur_time;
423 u64 speed_bytes, speed_timedelta;
425 demux->speed_pkts_cnt++;
427 /* show speed every SPEED_PKTS_INTERVAL packets */
428 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
429 cur_time = ktime_get();
431 if (ktime_to_ns(demux->speed_last_time) != 0) {
432 speed_bytes = (u64)demux->speed_pkts_cnt
433 * 188 * 8;
434 /* convert to 1024 basis */
435 speed_bytes = 1000 * div64_u64(speed_bytes,
436 1024);
437 speed_timedelta = ktime_ms_delta(cur_time,
438 demux->speed_last_time);
439 if (speed_timedelta)
440 dprintk("TS speed %llu Kbits/sec \n",
441 div64_u64(speed_bytes,
442 speed_timedelta));
445 demux->speed_last_time = cur_time;
446 demux->speed_pkts_cnt = 0;
450 if (buf[1] & 0x80) {
451 list_for_each_entry(feed, &demux->feed_list, list_head) {
452 if ((feed->pid != pid) && (feed->pid != 0x2000))
453 continue;
454 set_buf_flags(feed, DMX_BUFFER_FLAG_TEI);
456 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
457 pid, buf[1]);
458 /* data in this packet can't be trusted - drop it unless
459 * module option dvb_demux_feed_err_pkts is set */
460 if (!dvb_demux_feed_err_pkts)
461 return;
462 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
463 if (demux->cnt_storage && dvb_demux_tscheck) {
464 /* check pkt counter */
465 if (pid < MAX_PID) {
466 if (buf[3] & 0x10)
467 demux->cnt_storage[pid] =
468 (demux->cnt_storage[pid] + 1) & 0xf;
470 if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
471 list_for_each_entry(feed, &demux->feed_list, list_head) {
472 if ((feed->pid != pid) && (feed->pid != 0x2000))
473 continue;
474 set_buf_flags(feed,
475 DMX_BUFFER_PKT_COUNTER_MISMATCH);
478 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
479 pid, demux->cnt_storage[pid],
480 buf[3] & 0xf);
481 demux->cnt_storage[pid] = buf[3] & 0xf;
484 /* end check */
487 list_for_each_entry(feed, &demux->feed_list, list_head) {
488 if ((feed->pid != pid) && (feed->pid != 0x2000))
489 continue;
491 /* copy each packet only once to the dvr device, even
492 * if a PID is in multiple filters (e.g. video + PCR) */
493 if ((DVR_FEED(feed)) && (dvr_done++))
494 continue;
496 if (feed->pid == pid)
497 dvb_dmx_swfilter_packet_type(feed, buf);
498 else if (feed->pid == 0x2000)
499 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
500 &feed->buffer_flags);
504 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
505 size_t count)
507 unsigned long flags;
509 spin_lock_irqsave(&demux->lock, flags);
511 while (count--) {
512 if (buf[0] == 0x47)
513 dvb_dmx_swfilter_packet(demux, buf);
514 buf += 188;
517 spin_unlock_irqrestore(&demux->lock, flags);
520 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
522 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
523 const int pktsize)
525 int start = pos, lost;
527 while (pos < count) {
528 if (buf[pos] == 0x47 ||
529 (pktsize == 204 && buf[pos] == 0xB8))
530 break;
531 pos++;
534 lost = pos - start;
535 if (lost) {
536 /* This garbage is part of a valid packet? */
537 int backtrack = pos - pktsize;
538 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
539 (pktsize == 204 && buf[backtrack] == 0xB8)))
540 return backtrack;
543 return pos;
546 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
547 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
548 size_t count, const int pktsize)
550 int p = 0, i, j;
551 const u8 *q;
552 unsigned long flags;
554 spin_lock_irqsave(&demux->lock, flags);
556 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
557 i = demux->tsbufp;
558 j = pktsize - i;
559 if (count < j) {
560 memcpy(&demux->tsbuf[i], buf, count);
561 demux->tsbufp += count;
562 goto bailout;
564 memcpy(&demux->tsbuf[i], buf, j);
565 if (demux->tsbuf[0] == 0x47) /* double check */
566 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
567 demux->tsbufp = 0;
568 p += j;
571 while (1) {
572 p = find_next_packet(buf, p, count, pktsize);
573 if (p >= count)
574 break;
575 if (count - p < pktsize)
576 break;
578 q = &buf[p];
580 if (pktsize == 204 && (*q == 0xB8)) {
581 memcpy(demux->tsbuf, q, 188);
582 demux->tsbuf[0] = 0x47;
583 q = demux->tsbuf;
585 dvb_dmx_swfilter_packet(demux, q);
586 p += pktsize;
589 i = count - p;
590 if (i) {
591 memcpy(demux->tsbuf, &buf[p], i);
592 demux->tsbufp = i;
593 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
594 demux->tsbuf[0] = 0x47;
597 bailout:
598 spin_unlock_irqrestore(&demux->lock, flags);
601 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
603 _dvb_dmx_swfilter(demux, buf, count, 188);
605 EXPORT_SYMBOL(dvb_dmx_swfilter);
607 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
609 _dvb_dmx_swfilter(demux, buf, count, 204);
611 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
613 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
615 unsigned long flags;
617 spin_lock_irqsave(&demux->lock, flags);
619 demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts,
620 &demux->feed->buffer_flags);
622 spin_unlock_irqrestore(&demux->lock, flags);
624 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
626 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
628 int i;
630 for (i = 0; i < demux->filternum; i++)
631 if (demux->filter[i].state == DMX_STATE_FREE)
632 break;
634 if (i == demux->filternum)
635 return NULL;
637 demux->filter[i].state = DMX_STATE_ALLOCATED;
639 return &demux->filter[i];
642 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
644 int i;
646 for (i = 0; i < demux->feednum; i++)
647 if (demux->feed[i].state == DMX_STATE_FREE)
648 break;
650 if (i == demux->feednum)
651 return NULL;
653 demux->feed[i].state = DMX_STATE_ALLOCATED;
655 return &demux->feed[i];
658 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
660 struct dvb_demux_feed *entry;
662 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
663 if (entry == feed)
664 return 1;
666 return 0;
669 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
671 spin_lock_irq(&feed->demux->lock);
672 if (dvb_demux_feed_find(feed)) {
673 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
674 __func__, feed->type, feed->state, feed->pid);
675 goto out;
678 list_add(&feed->list_head, &feed->demux->feed_list);
679 out:
680 spin_unlock_irq(&feed->demux->lock);
683 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
685 spin_lock_irq(&feed->demux->lock);
686 if (!(dvb_demux_feed_find(feed))) {
687 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
688 __func__, feed->type, feed->state, feed->pid);
689 goto out;
692 list_del(&feed->list_head);
693 out:
694 spin_unlock_irq(&feed->demux->lock);
697 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
698 enum dmx_ts_pes pes_type, ktime_t timeout)
700 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
701 struct dvb_demux *demux = feed->demux;
703 if (pid > DMX_MAX_PID)
704 return -EINVAL;
706 if (mutex_lock_interruptible(&demux->mutex))
707 return -ERESTARTSYS;
709 if (ts_type & TS_DECODER) {
710 if (pes_type >= DMX_PES_OTHER) {
711 mutex_unlock(&demux->mutex);
712 return -EINVAL;
715 if (demux->pesfilter[pes_type] &&
716 demux->pesfilter[pes_type] != feed) {
717 mutex_unlock(&demux->mutex);
718 return -EINVAL;
721 demux->pesfilter[pes_type] = feed;
722 demux->pids[pes_type] = pid;
725 dvb_demux_feed_add(feed);
727 feed->pid = pid;
728 feed->timeout = timeout;
729 feed->ts_type = ts_type;
730 feed->pes_type = pes_type;
732 feed->state = DMX_STATE_READY;
733 mutex_unlock(&demux->mutex);
735 return 0;
738 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
740 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
741 struct dvb_demux *demux = feed->demux;
742 int ret;
744 if (mutex_lock_interruptible(&demux->mutex))
745 return -ERESTARTSYS;
747 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
748 mutex_unlock(&demux->mutex);
749 return -EINVAL;
752 if (!demux->start_feed) {
753 mutex_unlock(&demux->mutex);
754 return -ENODEV;
757 if ((ret = demux->start_feed(feed)) < 0) {
758 mutex_unlock(&demux->mutex);
759 return ret;
762 spin_lock_irq(&demux->lock);
763 ts_feed->is_filtering = 1;
764 feed->state = DMX_STATE_GO;
765 spin_unlock_irq(&demux->lock);
766 mutex_unlock(&demux->mutex);
768 return 0;
771 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
773 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
774 struct dvb_demux *demux = feed->demux;
775 int ret;
777 mutex_lock(&demux->mutex);
779 if (feed->state < DMX_STATE_GO) {
780 mutex_unlock(&demux->mutex);
781 return -EINVAL;
784 if (!demux->stop_feed) {
785 mutex_unlock(&demux->mutex);
786 return -ENODEV;
789 ret = demux->stop_feed(feed);
791 spin_lock_irq(&demux->lock);
792 ts_feed->is_filtering = 0;
793 feed->state = DMX_STATE_ALLOCATED;
794 spin_unlock_irq(&demux->lock);
795 mutex_unlock(&demux->mutex);
797 return ret;
800 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
801 struct dmx_ts_feed **ts_feed,
802 dmx_ts_cb callback)
804 struct dvb_demux *demux = (struct dvb_demux *)dmx;
805 struct dvb_demux_feed *feed;
807 if (mutex_lock_interruptible(&demux->mutex))
808 return -ERESTARTSYS;
810 if (!(feed = dvb_dmx_feed_alloc(demux))) {
811 mutex_unlock(&demux->mutex);
812 return -EBUSY;
815 feed->type = DMX_TYPE_TS;
816 feed->cb.ts = callback;
817 feed->demux = demux;
818 feed->pid = 0xffff;
819 feed->peslen = 0xfffa;
820 feed->buffer_flags = 0;
822 (*ts_feed) = &feed->feed.ts;
823 (*ts_feed)->parent = dmx;
824 (*ts_feed)->priv = NULL;
825 (*ts_feed)->is_filtering = 0;
826 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
827 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
828 (*ts_feed)->set = dmx_ts_feed_set;
830 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
831 feed->state = DMX_STATE_FREE;
832 mutex_unlock(&demux->mutex);
833 return -EBUSY;
836 feed->filter->type = DMX_TYPE_TS;
837 feed->filter->feed = feed;
838 feed->filter->state = DMX_STATE_READY;
840 mutex_unlock(&demux->mutex);
842 return 0;
845 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
846 struct dmx_ts_feed *ts_feed)
848 struct dvb_demux *demux = (struct dvb_demux *)dmx;
849 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
851 mutex_lock(&demux->mutex);
853 if (feed->state == DMX_STATE_FREE) {
854 mutex_unlock(&demux->mutex);
855 return -EINVAL;
858 feed->state = DMX_STATE_FREE;
859 feed->filter->state = DMX_STATE_FREE;
861 dvb_demux_feed_del(feed);
863 feed->pid = 0xffff;
865 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
866 demux->pesfilter[feed->pes_type] = NULL;
868 mutex_unlock(&demux->mutex);
869 return 0;
872 /******************************************************************************
873 * dmx_section_feed API calls
874 ******************************************************************************/
876 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
877 struct dmx_section_filter **filter)
879 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
880 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
881 struct dvb_demux_filter *dvbdmxfilter;
883 if (mutex_lock_interruptible(&dvbdemux->mutex))
884 return -ERESTARTSYS;
886 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
887 if (!dvbdmxfilter) {
888 mutex_unlock(&dvbdemux->mutex);
889 return -EBUSY;
892 spin_lock_irq(&dvbdemux->lock);
893 *filter = &dvbdmxfilter->filter;
894 (*filter)->parent = feed;
895 (*filter)->priv = NULL;
896 dvbdmxfilter->feed = dvbdmxfeed;
897 dvbdmxfilter->type = DMX_TYPE_SEC;
898 dvbdmxfilter->state = DMX_STATE_READY;
899 dvbdmxfilter->next = dvbdmxfeed->filter;
900 dvbdmxfeed->filter = dvbdmxfilter;
901 spin_unlock_irq(&dvbdemux->lock);
903 mutex_unlock(&dvbdemux->mutex);
904 return 0;
907 static int dmx_section_feed_set(struct dmx_section_feed *feed,
908 u16 pid, int check_crc)
910 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
911 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
913 if (pid > 0x1fff)
914 return -EINVAL;
916 if (mutex_lock_interruptible(&dvbdmx->mutex))
917 return -ERESTARTSYS;
919 dvb_demux_feed_add(dvbdmxfeed);
921 dvbdmxfeed->pid = pid;
922 dvbdmxfeed->feed.sec.check_crc = check_crc;
924 dvbdmxfeed->state = DMX_STATE_READY;
925 mutex_unlock(&dvbdmx->mutex);
926 return 0;
929 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
931 int i;
932 struct dvb_demux_filter *f;
933 struct dmx_section_filter *sf;
934 u8 mask, mode, doneq;
936 if (!(f = dvbdmxfeed->filter))
937 return;
938 do {
939 sf = &f->filter;
940 doneq = false;
941 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
942 mode = sf->filter_mode[i];
943 mask = sf->filter_mask[i];
944 f->maskandmode[i] = mask & mode;
945 doneq |= f->maskandnotmode[i] = mask & ~mode;
947 f->doneq = doneq ? true : false;
948 } while ((f = f->next));
951 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
953 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
954 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
955 int ret;
957 if (mutex_lock_interruptible(&dvbdmx->mutex))
958 return -ERESTARTSYS;
960 if (feed->is_filtering) {
961 mutex_unlock(&dvbdmx->mutex);
962 return -EBUSY;
965 if (!dvbdmxfeed->filter) {
966 mutex_unlock(&dvbdmx->mutex);
967 return -EINVAL;
970 dvbdmxfeed->feed.sec.tsfeedp = 0;
971 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
972 dvbdmxfeed->feed.sec.secbufp = 0;
973 dvbdmxfeed->feed.sec.seclen = 0;
974 dvbdmxfeed->pusi_seen = false;
976 if (!dvbdmx->start_feed) {
977 mutex_unlock(&dvbdmx->mutex);
978 return -ENODEV;
981 prepare_secfilters(dvbdmxfeed);
983 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
984 mutex_unlock(&dvbdmx->mutex);
985 return ret;
988 spin_lock_irq(&dvbdmx->lock);
989 feed->is_filtering = 1;
990 dvbdmxfeed->state = DMX_STATE_GO;
991 spin_unlock_irq(&dvbdmx->lock);
993 mutex_unlock(&dvbdmx->mutex);
994 return 0;
997 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
999 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1000 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1001 int ret;
1003 mutex_lock(&dvbdmx->mutex);
1005 if (!dvbdmx->stop_feed) {
1006 mutex_unlock(&dvbdmx->mutex);
1007 return -ENODEV;
1010 ret = dvbdmx->stop_feed(dvbdmxfeed);
1012 spin_lock_irq(&dvbdmx->lock);
1013 dvbdmxfeed->state = DMX_STATE_READY;
1014 feed->is_filtering = 0;
1015 spin_unlock_irq(&dvbdmx->lock);
1017 mutex_unlock(&dvbdmx->mutex);
1018 return ret;
1021 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
1022 struct dmx_section_filter *filter)
1024 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
1025 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1026 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1028 mutex_lock(&dvbdmx->mutex);
1030 if (dvbdmxfilter->feed != dvbdmxfeed) {
1031 mutex_unlock(&dvbdmx->mutex);
1032 return -EINVAL;
1035 if (feed->is_filtering) {
1036 /* release dvbdmx->mutex as far as it is
1037 acquired by stop_filtering() itself */
1038 mutex_unlock(&dvbdmx->mutex);
1039 feed->stop_filtering(feed);
1040 mutex_lock(&dvbdmx->mutex);
1043 spin_lock_irq(&dvbdmx->lock);
1044 f = dvbdmxfeed->filter;
1046 if (f == dvbdmxfilter) {
1047 dvbdmxfeed->filter = dvbdmxfilter->next;
1048 } else {
1049 while (f->next != dvbdmxfilter)
1050 f = f->next;
1051 f->next = f->next->next;
1054 dvbdmxfilter->state = DMX_STATE_FREE;
1055 spin_unlock_irq(&dvbdmx->lock);
1056 mutex_unlock(&dvbdmx->mutex);
1057 return 0;
1060 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1061 struct dmx_section_feed **feed,
1062 dmx_section_cb callback)
1064 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1065 struct dvb_demux_feed *dvbdmxfeed;
1067 if (mutex_lock_interruptible(&dvbdmx->mutex))
1068 return -ERESTARTSYS;
1070 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1071 mutex_unlock(&dvbdmx->mutex);
1072 return -EBUSY;
1075 dvbdmxfeed->type = DMX_TYPE_SEC;
1076 dvbdmxfeed->cb.sec = callback;
1077 dvbdmxfeed->demux = dvbdmx;
1078 dvbdmxfeed->pid = 0xffff;
1079 dvbdmxfeed->buffer_flags = 0;
1080 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1081 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1082 dvbdmxfeed->feed.sec.tsfeedp = 0;
1083 dvbdmxfeed->filter = NULL;
1085 (*feed) = &dvbdmxfeed->feed.sec;
1086 (*feed)->is_filtering = 0;
1087 (*feed)->parent = demux;
1088 (*feed)->priv = NULL;
1090 (*feed)->set = dmx_section_feed_set;
1091 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1092 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1093 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1094 (*feed)->release_filter = dmx_section_feed_release_filter;
1096 mutex_unlock(&dvbdmx->mutex);
1097 return 0;
1100 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1101 struct dmx_section_feed *feed)
1103 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1104 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1106 mutex_lock(&dvbdmx->mutex);
1108 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1109 mutex_unlock(&dvbdmx->mutex);
1110 return -EINVAL;
1112 dvbdmxfeed->state = DMX_STATE_FREE;
1114 dvb_demux_feed_del(dvbdmxfeed);
1116 dvbdmxfeed->pid = 0xffff;
1118 mutex_unlock(&dvbdmx->mutex);
1119 return 0;
1122 /******************************************************************************
1123 * dvb_demux kernel data API calls
1124 ******************************************************************************/
1126 static int dvbdmx_open(struct dmx_demux *demux)
1128 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1130 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1131 return -EUSERS;
1133 dvbdemux->users++;
1134 return 0;
1137 static int dvbdmx_close(struct dmx_demux *demux)
1139 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1141 if (dvbdemux->users == 0)
1142 return -ENODEV;
1144 dvbdemux->users--;
1145 //FIXME: release any unneeded resources if users==0
1146 return 0;
1149 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1151 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1152 void *p;
1154 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1155 return -EINVAL;
1157 p = memdup_user(buf, count);
1158 if (IS_ERR(p))
1159 return PTR_ERR(p);
1160 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1161 kfree(p);
1162 return -ERESTARTSYS;
1164 dvb_dmx_swfilter(dvbdemux, p, count);
1165 kfree(p);
1166 mutex_unlock(&dvbdemux->mutex);
1168 if (signal_pending(current))
1169 return -EINTR;
1170 return count;
1173 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1174 struct dmx_frontend *frontend)
1176 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1177 struct list_head *head = &dvbdemux->frontend_list;
1179 list_add(&(frontend->connectivity_list), head);
1181 return 0;
1184 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1185 struct dmx_frontend *frontend)
1187 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1188 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1190 list_for_each_safe(pos, n, head) {
1191 if (DMX_FE_ENTRY(pos) == frontend) {
1192 list_del(pos);
1193 return 0;
1197 return -ENODEV;
1200 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1202 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1204 if (list_empty(&dvbdemux->frontend_list))
1205 return NULL;
1207 return &dvbdemux->frontend_list;
1210 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1211 struct dmx_frontend *frontend)
1213 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1215 if (demux->frontend)
1216 return -EINVAL;
1218 mutex_lock(&dvbdemux->mutex);
1220 demux->frontend = frontend;
1221 mutex_unlock(&dvbdemux->mutex);
1222 return 0;
1225 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1227 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1229 mutex_lock(&dvbdemux->mutex);
1231 demux->frontend = NULL;
1232 mutex_unlock(&dvbdemux->mutex);
1233 return 0;
1236 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1238 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1240 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1241 return 0;
1244 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1246 int i;
1247 struct dmx_demux *dmx = &dvbdemux->dmx;
1249 dvbdemux->cnt_storage = NULL;
1250 dvbdemux->users = 0;
1251 dvbdemux->filter = vmalloc(array_size(sizeof(struct dvb_demux_filter),
1252 dvbdemux->filternum));
1254 if (!dvbdemux->filter)
1255 return -ENOMEM;
1257 dvbdemux->feed = vmalloc(array_size(sizeof(struct dvb_demux_feed),
1258 dvbdemux->feednum));
1259 if (!dvbdemux->feed) {
1260 vfree(dvbdemux->filter);
1261 dvbdemux->filter = NULL;
1262 return -ENOMEM;
1264 for (i = 0; i < dvbdemux->filternum; i++) {
1265 dvbdemux->filter[i].state = DMX_STATE_FREE;
1266 dvbdemux->filter[i].index = i;
1268 for (i = 0; i < dvbdemux->feednum; i++) {
1269 dvbdemux->feed[i].state = DMX_STATE_FREE;
1270 dvbdemux->feed[i].index = i;
1273 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1274 if (!dvbdemux->cnt_storage)
1275 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1277 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1279 for (i = 0; i < DMX_PES_OTHER; i++) {
1280 dvbdemux->pesfilter[i] = NULL;
1281 dvbdemux->pids[i] = 0xffff;
1284 INIT_LIST_HEAD(&dvbdemux->feed_list);
1286 dvbdemux->playing = 0;
1287 dvbdemux->recording = 0;
1288 dvbdemux->tsbufp = 0;
1290 if (!dvbdemux->check_crc32)
1291 dvbdemux->check_crc32 = dvb_dmx_crc32;
1293 if (!dvbdemux->memcopy)
1294 dvbdemux->memcopy = dvb_dmx_memcopy;
1296 dmx->frontend = NULL;
1297 dmx->priv = dvbdemux;
1298 dmx->open = dvbdmx_open;
1299 dmx->close = dvbdmx_close;
1300 dmx->write = dvbdmx_write;
1301 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1302 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1303 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1304 dmx->release_section_feed = dvbdmx_release_section_feed;
1306 dmx->add_frontend = dvbdmx_add_frontend;
1307 dmx->remove_frontend = dvbdmx_remove_frontend;
1308 dmx->get_frontends = dvbdmx_get_frontends;
1309 dmx->connect_frontend = dvbdmx_connect_frontend;
1310 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1311 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1313 mutex_init(&dvbdemux->mutex);
1314 spin_lock_init(&dvbdemux->lock);
1316 return 0;
1319 EXPORT_SYMBOL(dvb_dmx_init);
1321 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1323 vfree(dvbdemux->cnt_storage);
1324 vfree(dvbdemux->filter);
1325 vfree(dvbdemux->feed);
1328 EXPORT_SYMBOL(dvb_dmx_release);