xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / media / dvb-core / dvb_demux.c
blob210eed0269b085d5b8c99d5f8941928cf0613a30
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 /******************************************************************************
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 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
108 int ccok;
109 u8 cc;
110 #endif
112 if (count == 0)
113 return -1;
115 p = 188 - count;
117 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
118 cc = buf[3] & 0x0f;
119 ccok = ((feed->cc + 1) & 0x0f) == cc;
120 feed->cc = cc;
121 if (!ccok)
122 dprintk("missed packet: %d instead of %d!\n",
123 cc, (feed->cc + 1) & 0x0f);
124 #endif
126 if (buf[1] & 0x40) // PUSI ?
127 feed->peslen = 0xfffa;
129 feed->peslen += count;
131 return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts);
134 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
135 struct dvb_demux_filter *f)
137 u8 neq = 0;
138 int i;
140 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
141 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
143 if (f->maskandmode[i] & xor)
144 return 0;
146 neq |= f->maskandnotmode[i] & xor;
149 if (f->doneq && !neq)
150 return 0;
152 return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
153 NULL, 0, &f->filter);
156 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
158 struct dvb_demux *demux = feed->demux;
159 struct dvb_demux_filter *f = feed->filter;
160 struct dmx_section_feed *sec = &feed->feed.sec;
161 int section_syntax_indicator;
163 if (!sec->is_filtering)
164 return 0;
166 if (!f)
167 return 0;
169 if (sec->check_crc) {
170 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
171 if (section_syntax_indicator &&
172 demux->check_crc32(feed, sec->secbuf, sec->seclen))
173 return -1;
176 do {
177 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
178 return -1;
179 } while ((f = f->next) && sec->is_filtering);
181 sec->seclen = 0;
183 return 0;
186 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
188 struct dmx_section_feed *sec = &feed->feed.sec;
190 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
191 if (sec->secbufp < sec->tsfeedp) {
192 int n = sec->tsfeedp - sec->secbufp;
195 * Section padding is done with 0xff bytes entirely.
196 * Due to speed reasons, we won't check all of them
197 * but just first and last.
199 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
200 dprintk("section ts padding loss: %d/%d\n",
201 n, sec->tsfeedp);
202 dprintk("pad data: %*ph\n", n, sec->secbuf);
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 = false 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 = true
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 CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
241 dprintk("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 CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
275 else
276 dprintk("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 CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
311 if (dc_i)
312 dprintk("%d frame with disconnect indicator\n",
313 cc);
314 else
315 dprintk("discontinuity: %d instead of %d. %d bytes lost\n",
316 cc, (feed->cc + 1) & 0x0f, count + 4);
318 * those bytes under sume circumstances will again be reported
319 * in the following dvb_dmx_swfilter_section_new
321 #endif
323 * Discontinuity detected. Reset pusi_seen to
324 * stop feeding of suspicious data until next PUSI=1 arrives
326 * FIXME: does it make sense if the MPEG-TS is the one
327 * reporting discontinuity?
329 feed->pusi_seen = false;
330 dvb_dmx_swfilter_section_new(feed);
333 if (buf[1] & 0x40) {
334 /* PUSI=1 (is set), section boundary is here */
335 if (count > 1 && buf[p] < count) {
336 const u8 *before = &buf[p + 1];
337 u8 before_len = buf[p];
338 const u8 *after = &before[before_len];
339 u8 after_len = count - 1 - before_len;
341 dvb_dmx_swfilter_section_copy_dump(feed, before,
342 before_len);
343 /* before start of new section, set pusi_seen */
344 feed->pusi_seen = true;
345 dvb_dmx_swfilter_section_new(feed);
346 dvb_dmx_swfilter_section_copy_dump(feed, after,
347 after_len);
349 #ifdef CONFIG_DVB_DEMUX_SECTION_LOSS_LOG
350 else if (count > 0)
351 dprintk("PUSI=1 but %d bytes lost\n", 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 /* Used only on full-featured devices */
375 if (feed->ts_type & TS_DECODER)
376 if (feed->demux->write_to_decoder)
377 feed->demux->write_to_decoder(feed, buf, 188);
378 break;
380 case DMX_TYPE_SEC:
381 if (!feed->feed.sec.is_filtering)
382 break;
383 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
384 feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
385 break;
387 default:
388 break;
392 #define DVR_FEED(f) \
393 (((f)->type == DMX_TYPE_TS) && \
394 ((f)->feed.ts.is_filtering) && \
395 (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
397 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
399 struct dvb_demux_feed *feed;
400 u16 pid = ts_pid(buf);
401 int dvr_done = 0;
403 if (dvb_demux_speedcheck) {
404 ktime_t cur_time;
405 u64 speed_bytes, speed_timedelta;
407 demux->speed_pkts_cnt++;
409 /* show speed every SPEED_PKTS_INTERVAL packets */
410 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
411 cur_time = ktime_get();
413 if (ktime_to_ns(demux->speed_last_time) != 0) {
414 speed_bytes = (u64)demux->speed_pkts_cnt
415 * 188 * 8;
416 /* convert to 1024 basis */
417 speed_bytes = 1000 * div64_u64(speed_bytes,
418 1024);
419 speed_timedelta = ktime_ms_delta(cur_time,
420 demux->speed_last_time);
421 if (speed_timedelta)
422 dprintk("TS speed %llu Kbits/sec \n",
423 div64_u64(speed_bytes,
424 speed_timedelta));
427 demux->speed_last_time = cur_time;
428 demux->speed_pkts_cnt = 0;
432 if (buf[1] & 0x80) {
433 dprintk_tscheck("TEI detected. PID=0x%x data1=0x%x\n",
434 pid, buf[1]);
435 /* data in this packet can't be trusted - drop it unless
436 * module option dvb_demux_feed_err_pkts is set */
437 if (!dvb_demux_feed_err_pkts)
438 return;
439 } else /* if TEI bit is set, pid may be wrong- skip pkt counter */
440 if (demux->cnt_storage && dvb_demux_tscheck) {
441 /* check pkt counter */
442 if (pid < MAX_PID) {
443 if (buf[3] & 0x10)
444 demux->cnt_storage[pid] =
445 (demux->cnt_storage[pid] + 1) & 0xf;
447 if ((buf[3] & 0xf) != demux->cnt_storage[pid]) {
448 dprintk_tscheck("TS packet counter mismatch. PID=0x%x expected 0x%x got 0x%x\n",
449 pid, demux->cnt_storage[pid],
450 buf[3] & 0xf);
451 demux->cnt_storage[pid] = buf[3] & 0xf;
454 /* end check */
457 list_for_each_entry(feed, &demux->feed_list, list_head) {
458 if ((feed->pid != pid) && (feed->pid != 0x2000))
459 continue;
461 /* copy each packet only once to the dvr device, even
462 * if a PID is in multiple filters (e.g. video + PCR) */
463 if ((DVR_FEED(feed)) && (dvr_done++))
464 continue;
466 if (feed->pid == pid)
467 dvb_dmx_swfilter_packet_type(feed, buf);
468 else if (feed->pid == 0x2000)
469 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts);
473 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
474 size_t count)
476 unsigned long flags;
478 spin_lock_irqsave(&demux->lock, flags);
480 while (count--) {
481 if (buf[0] == 0x47)
482 dvb_dmx_swfilter_packet(demux, buf);
483 buf += 188;
486 spin_unlock_irqrestore(&demux->lock, flags);
489 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
491 static inline int find_next_packet(const u8 *buf, int pos, size_t count,
492 const int pktsize)
494 int start = pos, lost;
496 while (pos < count) {
497 if (buf[pos] == 0x47 ||
498 (pktsize == 204 && buf[pos] == 0xB8))
499 break;
500 pos++;
503 lost = pos - start;
504 if (lost) {
505 /* This garbage is part of a valid packet? */
506 int backtrack = pos - pktsize;
507 if (backtrack >= 0 && (buf[backtrack] == 0x47 ||
508 (pktsize == 204 && buf[backtrack] == 0xB8)))
509 return backtrack;
512 return pos;
515 /* Filter all pktsize= 188 or 204 sized packets and skip garbage. */
516 static inline void _dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf,
517 size_t count, const int pktsize)
519 int p = 0, i, j;
520 const u8 *q;
521 unsigned long flags;
523 spin_lock_irqsave(&demux->lock, flags);
525 if (demux->tsbufp) { /* tsbuf[0] is now 0x47. */
526 i = demux->tsbufp;
527 j = pktsize - i;
528 if (count < j) {
529 memcpy(&demux->tsbuf[i], buf, count);
530 demux->tsbufp += count;
531 goto bailout;
533 memcpy(&demux->tsbuf[i], buf, j);
534 if (demux->tsbuf[0] == 0x47) /* double check */
535 dvb_dmx_swfilter_packet(demux, demux->tsbuf);
536 demux->tsbufp = 0;
537 p += j;
540 while (1) {
541 p = find_next_packet(buf, p, count, pktsize);
542 if (p >= count)
543 break;
544 if (count - p < pktsize)
545 break;
547 q = &buf[p];
549 if (pktsize == 204 && (*q == 0xB8)) {
550 memcpy(demux->tsbuf, q, 188);
551 demux->tsbuf[0] = 0x47;
552 q = demux->tsbuf;
554 dvb_dmx_swfilter_packet(demux, q);
555 p += pktsize;
558 i = count - p;
559 if (i) {
560 memcpy(demux->tsbuf, &buf[p], i);
561 demux->tsbufp = i;
562 if (pktsize == 204 && demux->tsbuf[0] == 0xB8)
563 demux->tsbuf[0] = 0x47;
566 bailout:
567 spin_unlock_irqrestore(&demux->lock, flags);
570 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
572 _dvb_dmx_swfilter(demux, buf, count, 188);
574 EXPORT_SYMBOL(dvb_dmx_swfilter);
576 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
578 _dvb_dmx_swfilter(demux, buf, count, 204);
580 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
582 void dvb_dmx_swfilter_raw(struct dvb_demux *demux, const u8 *buf, size_t count)
584 unsigned long flags;
586 spin_lock_irqsave(&demux->lock, flags);
588 demux->feed->cb.ts(buf, count, NULL, 0, &demux->feed->feed.ts);
590 spin_unlock_irqrestore(&demux->lock, flags);
592 EXPORT_SYMBOL(dvb_dmx_swfilter_raw);
594 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
596 int i;
598 for (i = 0; i < demux->filternum; i++)
599 if (demux->filter[i].state == DMX_STATE_FREE)
600 break;
602 if (i == demux->filternum)
603 return NULL;
605 demux->filter[i].state = DMX_STATE_ALLOCATED;
607 return &demux->filter[i];
610 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
612 int i;
614 for (i = 0; i < demux->feednum; i++)
615 if (demux->feed[i].state == DMX_STATE_FREE)
616 break;
618 if (i == demux->feednum)
619 return NULL;
621 demux->feed[i].state = DMX_STATE_ALLOCATED;
623 return &demux->feed[i];
626 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
628 struct dvb_demux_feed *entry;
630 list_for_each_entry(entry, &feed->demux->feed_list, list_head)
631 if (entry == feed)
632 return 1;
634 return 0;
637 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
639 spin_lock_irq(&feed->demux->lock);
640 if (dvb_demux_feed_find(feed)) {
641 pr_err("%s: feed already in list (type=%x state=%x pid=%x)\n",
642 __func__, feed->type, feed->state, feed->pid);
643 goto out;
646 list_add(&feed->list_head, &feed->demux->feed_list);
647 out:
648 spin_unlock_irq(&feed->demux->lock);
651 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
653 spin_lock_irq(&feed->demux->lock);
654 if (!(dvb_demux_feed_find(feed))) {
655 pr_err("%s: feed not in list (type=%x state=%x pid=%x)\n",
656 __func__, feed->type, feed->state, feed->pid);
657 goto out;
660 list_del(&feed->list_head);
661 out:
662 spin_unlock_irq(&feed->demux->lock);
665 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
666 enum dmx_ts_pes pes_type, ktime_t timeout)
668 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
669 struct dvb_demux *demux = feed->demux;
671 if (pid > DMX_MAX_PID)
672 return -EINVAL;
674 if (mutex_lock_interruptible(&demux->mutex))
675 return -ERESTARTSYS;
677 if (ts_type & TS_DECODER) {
678 if (pes_type >= DMX_PES_OTHER) {
679 mutex_unlock(&demux->mutex);
680 return -EINVAL;
683 if (demux->pesfilter[pes_type] &&
684 demux->pesfilter[pes_type] != feed) {
685 mutex_unlock(&demux->mutex);
686 return -EINVAL;
689 demux->pesfilter[pes_type] = feed;
690 demux->pids[pes_type] = pid;
693 dvb_demux_feed_add(feed);
695 feed->pid = pid;
696 feed->timeout = timeout;
697 feed->ts_type = ts_type;
698 feed->pes_type = pes_type;
700 feed->state = DMX_STATE_READY;
701 mutex_unlock(&demux->mutex);
703 return 0;
706 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
708 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
709 struct dvb_demux *demux = feed->demux;
710 int ret;
712 if (mutex_lock_interruptible(&demux->mutex))
713 return -ERESTARTSYS;
715 if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
716 mutex_unlock(&demux->mutex);
717 return -EINVAL;
720 if (!demux->start_feed) {
721 mutex_unlock(&demux->mutex);
722 return -ENODEV;
725 if ((ret = demux->start_feed(feed)) < 0) {
726 mutex_unlock(&demux->mutex);
727 return ret;
730 spin_lock_irq(&demux->lock);
731 ts_feed->is_filtering = 1;
732 feed->state = DMX_STATE_GO;
733 spin_unlock_irq(&demux->lock);
734 mutex_unlock(&demux->mutex);
736 return 0;
739 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
741 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
742 struct dvb_demux *demux = feed->demux;
743 int ret;
745 mutex_lock(&demux->mutex);
747 if (feed->state < DMX_STATE_GO) {
748 mutex_unlock(&demux->mutex);
749 return -EINVAL;
752 if (!demux->stop_feed) {
753 mutex_unlock(&demux->mutex);
754 return -ENODEV;
757 ret = demux->stop_feed(feed);
759 spin_lock_irq(&demux->lock);
760 ts_feed->is_filtering = 0;
761 feed->state = DMX_STATE_ALLOCATED;
762 spin_unlock_irq(&demux->lock);
763 mutex_unlock(&demux->mutex);
765 return ret;
768 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
769 struct dmx_ts_feed **ts_feed,
770 dmx_ts_cb callback)
772 struct dvb_demux *demux = (struct dvb_demux *)dmx;
773 struct dvb_demux_feed *feed;
775 if (mutex_lock_interruptible(&demux->mutex))
776 return -ERESTARTSYS;
778 if (!(feed = dvb_dmx_feed_alloc(demux))) {
779 mutex_unlock(&demux->mutex);
780 return -EBUSY;
783 feed->type = DMX_TYPE_TS;
784 feed->cb.ts = callback;
785 feed->demux = demux;
786 feed->pid = 0xffff;
787 feed->peslen = 0xfffa;
789 (*ts_feed) = &feed->feed.ts;
790 (*ts_feed)->parent = dmx;
791 (*ts_feed)->priv = NULL;
792 (*ts_feed)->is_filtering = 0;
793 (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
794 (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
795 (*ts_feed)->set = dmx_ts_feed_set;
797 if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
798 feed->state = DMX_STATE_FREE;
799 mutex_unlock(&demux->mutex);
800 return -EBUSY;
803 feed->filter->type = DMX_TYPE_TS;
804 feed->filter->feed = feed;
805 feed->filter->state = DMX_STATE_READY;
807 mutex_unlock(&demux->mutex);
809 return 0;
812 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
813 struct dmx_ts_feed *ts_feed)
815 struct dvb_demux *demux = (struct dvb_demux *)dmx;
816 struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
818 mutex_lock(&demux->mutex);
820 if (feed->state == DMX_STATE_FREE) {
821 mutex_unlock(&demux->mutex);
822 return -EINVAL;
825 feed->state = DMX_STATE_FREE;
826 feed->filter->state = DMX_STATE_FREE;
828 dvb_demux_feed_del(feed);
830 feed->pid = 0xffff;
832 if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_PES_OTHER)
833 demux->pesfilter[feed->pes_type] = NULL;
835 mutex_unlock(&demux->mutex);
836 return 0;
839 /******************************************************************************
840 * dmx_section_feed API calls
841 ******************************************************************************/
843 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
844 struct dmx_section_filter **filter)
846 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
847 struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
848 struct dvb_demux_filter *dvbdmxfilter;
850 if (mutex_lock_interruptible(&dvbdemux->mutex))
851 return -ERESTARTSYS;
853 dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
854 if (!dvbdmxfilter) {
855 mutex_unlock(&dvbdemux->mutex);
856 return -EBUSY;
859 spin_lock_irq(&dvbdemux->lock);
860 *filter = &dvbdmxfilter->filter;
861 (*filter)->parent = feed;
862 (*filter)->priv = NULL;
863 dvbdmxfilter->feed = dvbdmxfeed;
864 dvbdmxfilter->type = DMX_TYPE_SEC;
865 dvbdmxfilter->state = DMX_STATE_READY;
866 dvbdmxfilter->next = dvbdmxfeed->filter;
867 dvbdmxfeed->filter = dvbdmxfilter;
868 spin_unlock_irq(&dvbdemux->lock);
870 mutex_unlock(&dvbdemux->mutex);
871 return 0;
874 static int dmx_section_feed_set(struct dmx_section_feed *feed,
875 u16 pid, int check_crc)
877 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
878 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
880 if (pid > 0x1fff)
881 return -EINVAL;
883 if (mutex_lock_interruptible(&dvbdmx->mutex))
884 return -ERESTARTSYS;
886 dvb_demux_feed_add(dvbdmxfeed);
888 dvbdmxfeed->pid = pid;
889 dvbdmxfeed->feed.sec.check_crc = check_crc;
891 dvbdmxfeed->state = DMX_STATE_READY;
892 mutex_unlock(&dvbdmx->mutex);
893 return 0;
896 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
898 int i;
899 struct dvb_demux_filter *f;
900 struct dmx_section_filter *sf;
901 u8 mask, mode, doneq;
903 if (!(f = dvbdmxfeed->filter))
904 return;
905 do {
906 sf = &f->filter;
907 doneq = false;
908 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
909 mode = sf->filter_mode[i];
910 mask = sf->filter_mask[i];
911 f->maskandmode[i] = mask & mode;
912 doneq |= f->maskandnotmode[i] = mask & ~mode;
914 f->doneq = doneq ? true : false;
915 } while ((f = f->next));
918 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
920 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
921 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
922 int ret;
924 if (mutex_lock_interruptible(&dvbdmx->mutex))
925 return -ERESTARTSYS;
927 if (feed->is_filtering) {
928 mutex_unlock(&dvbdmx->mutex);
929 return -EBUSY;
932 if (!dvbdmxfeed->filter) {
933 mutex_unlock(&dvbdmx->mutex);
934 return -EINVAL;
937 dvbdmxfeed->feed.sec.tsfeedp = 0;
938 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
939 dvbdmxfeed->feed.sec.secbufp = 0;
940 dvbdmxfeed->feed.sec.seclen = 0;
942 if (!dvbdmx->start_feed) {
943 mutex_unlock(&dvbdmx->mutex);
944 return -ENODEV;
947 prepare_secfilters(dvbdmxfeed);
949 if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
950 mutex_unlock(&dvbdmx->mutex);
951 return ret;
954 spin_lock_irq(&dvbdmx->lock);
955 feed->is_filtering = 1;
956 dvbdmxfeed->state = DMX_STATE_GO;
957 spin_unlock_irq(&dvbdmx->lock);
959 mutex_unlock(&dvbdmx->mutex);
960 return 0;
963 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
965 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
966 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
967 int ret;
969 mutex_lock(&dvbdmx->mutex);
971 if (!dvbdmx->stop_feed) {
972 mutex_unlock(&dvbdmx->mutex);
973 return -ENODEV;
976 ret = dvbdmx->stop_feed(dvbdmxfeed);
978 spin_lock_irq(&dvbdmx->lock);
979 dvbdmxfeed->state = DMX_STATE_READY;
980 feed->is_filtering = 0;
981 spin_unlock_irq(&dvbdmx->lock);
983 mutex_unlock(&dvbdmx->mutex);
984 return ret;
987 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
988 struct dmx_section_filter *filter)
990 struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
991 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
992 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
994 mutex_lock(&dvbdmx->mutex);
996 if (dvbdmxfilter->feed != dvbdmxfeed) {
997 mutex_unlock(&dvbdmx->mutex);
998 return -EINVAL;
1001 if (feed->is_filtering) {
1002 /* release dvbdmx->mutex as far as it is
1003 acquired by stop_filtering() itself */
1004 mutex_unlock(&dvbdmx->mutex);
1005 feed->stop_filtering(feed);
1006 mutex_lock(&dvbdmx->mutex);
1009 spin_lock_irq(&dvbdmx->lock);
1010 f = dvbdmxfeed->filter;
1012 if (f == dvbdmxfilter) {
1013 dvbdmxfeed->filter = dvbdmxfilter->next;
1014 } else {
1015 while (f->next != dvbdmxfilter)
1016 f = f->next;
1017 f->next = f->next->next;
1020 dvbdmxfilter->state = DMX_STATE_FREE;
1021 spin_unlock_irq(&dvbdmx->lock);
1022 mutex_unlock(&dvbdmx->mutex);
1023 return 0;
1026 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1027 struct dmx_section_feed **feed,
1028 dmx_section_cb callback)
1030 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1031 struct dvb_demux_feed *dvbdmxfeed;
1033 if (mutex_lock_interruptible(&dvbdmx->mutex))
1034 return -ERESTARTSYS;
1036 if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1037 mutex_unlock(&dvbdmx->mutex);
1038 return -EBUSY;
1041 dvbdmxfeed->type = DMX_TYPE_SEC;
1042 dvbdmxfeed->cb.sec = callback;
1043 dvbdmxfeed->demux = dvbdmx;
1044 dvbdmxfeed->pid = 0xffff;
1045 dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1046 dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1047 dvbdmxfeed->feed.sec.tsfeedp = 0;
1048 dvbdmxfeed->filter = NULL;
1050 (*feed) = &dvbdmxfeed->feed.sec;
1051 (*feed)->is_filtering = 0;
1052 (*feed)->parent = demux;
1053 (*feed)->priv = NULL;
1055 (*feed)->set = dmx_section_feed_set;
1056 (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1057 (*feed)->start_filtering = dmx_section_feed_start_filtering;
1058 (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1059 (*feed)->release_filter = dmx_section_feed_release_filter;
1061 mutex_unlock(&dvbdmx->mutex);
1062 return 0;
1065 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1066 struct dmx_section_feed *feed)
1068 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1069 struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1071 mutex_lock(&dvbdmx->mutex);
1073 if (dvbdmxfeed->state == DMX_STATE_FREE) {
1074 mutex_unlock(&dvbdmx->mutex);
1075 return -EINVAL;
1077 dvbdmxfeed->state = DMX_STATE_FREE;
1079 dvb_demux_feed_del(dvbdmxfeed);
1081 dvbdmxfeed->pid = 0xffff;
1083 mutex_unlock(&dvbdmx->mutex);
1084 return 0;
1087 /******************************************************************************
1088 * dvb_demux kernel data API calls
1089 ******************************************************************************/
1091 static int dvbdmx_open(struct dmx_demux *demux)
1093 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1095 if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1096 return -EUSERS;
1098 dvbdemux->users++;
1099 return 0;
1102 static int dvbdmx_close(struct dmx_demux *demux)
1104 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1106 if (dvbdemux->users == 0)
1107 return -ENODEV;
1109 dvbdemux->users--;
1110 //FIXME: release any unneeded resources if users==0
1111 return 0;
1114 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1116 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1117 void *p;
1119 if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1120 return -EINVAL;
1122 p = memdup_user(buf, count);
1123 if (IS_ERR(p))
1124 return PTR_ERR(p);
1125 if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1126 kfree(p);
1127 return -ERESTARTSYS;
1129 dvb_dmx_swfilter(dvbdemux, p, count);
1130 kfree(p);
1131 mutex_unlock(&dvbdemux->mutex);
1133 if (signal_pending(current))
1134 return -EINTR;
1135 return count;
1138 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1139 struct dmx_frontend *frontend)
1141 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1142 struct list_head *head = &dvbdemux->frontend_list;
1144 list_add(&(frontend->connectivity_list), head);
1146 return 0;
1149 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1150 struct dmx_frontend *frontend)
1152 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1153 struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1155 list_for_each_safe(pos, n, head) {
1156 if (DMX_FE_ENTRY(pos) == frontend) {
1157 list_del(pos);
1158 return 0;
1162 return -ENODEV;
1165 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1167 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1169 if (list_empty(&dvbdemux->frontend_list))
1170 return NULL;
1172 return &dvbdemux->frontend_list;
1175 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1176 struct dmx_frontend *frontend)
1178 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1180 if (demux->frontend)
1181 return -EINVAL;
1183 mutex_lock(&dvbdemux->mutex);
1185 demux->frontend = frontend;
1186 mutex_unlock(&dvbdemux->mutex);
1187 return 0;
1190 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1192 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1194 mutex_lock(&dvbdemux->mutex);
1196 demux->frontend = NULL;
1197 mutex_unlock(&dvbdemux->mutex);
1198 return 0;
1201 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1203 struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1205 memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1206 return 0;
1209 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1211 int i;
1212 struct dmx_demux *dmx = &dvbdemux->dmx;
1214 dvbdemux->cnt_storage = NULL;
1215 dvbdemux->users = 0;
1216 dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1218 if (!dvbdemux->filter)
1219 return -ENOMEM;
1221 dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1222 if (!dvbdemux->feed) {
1223 vfree(dvbdemux->filter);
1224 dvbdemux->filter = NULL;
1225 return -ENOMEM;
1227 for (i = 0; i < dvbdemux->filternum; i++) {
1228 dvbdemux->filter[i].state = DMX_STATE_FREE;
1229 dvbdemux->filter[i].index = i;
1231 for (i = 0; i < dvbdemux->feednum; i++) {
1232 dvbdemux->feed[i].state = DMX_STATE_FREE;
1233 dvbdemux->feed[i].index = i;
1236 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1237 if (!dvbdemux->cnt_storage)
1238 pr_warn("Couldn't allocate memory for TS/TEI check. Disabling it\n");
1240 INIT_LIST_HEAD(&dvbdemux->frontend_list);
1242 for (i = 0; i < DMX_PES_OTHER; i++) {
1243 dvbdemux->pesfilter[i] = NULL;
1244 dvbdemux->pids[i] = 0xffff;
1247 INIT_LIST_HEAD(&dvbdemux->feed_list);
1249 dvbdemux->playing = 0;
1250 dvbdemux->recording = 0;
1251 dvbdemux->tsbufp = 0;
1253 if (!dvbdemux->check_crc32)
1254 dvbdemux->check_crc32 = dvb_dmx_crc32;
1256 if (!dvbdemux->memcopy)
1257 dvbdemux->memcopy = dvb_dmx_memcopy;
1259 dmx->frontend = NULL;
1260 dmx->priv = dvbdemux;
1261 dmx->open = dvbdmx_open;
1262 dmx->close = dvbdmx_close;
1263 dmx->write = dvbdmx_write;
1264 dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1265 dmx->release_ts_feed = dvbdmx_release_ts_feed;
1266 dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1267 dmx->release_section_feed = dvbdmx_release_section_feed;
1269 dmx->add_frontend = dvbdmx_add_frontend;
1270 dmx->remove_frontend = dvbdmx_remove_frontend;
1271 dmx->get_frontends = dvbdmx_get_frontends;
1272 dmx->connect_frontend = dvbdmx_connect_frontend;
1273 dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1274 dmx->get_pes_pids = dvbdmx_get_pes_pids;
1276 mutex_init(&dvbdemux->mutex);
1277 spin_lock_init(&dvbdemux->lock);
1279 return 0;
1282 EXPORT_SYMBOL(dvb_dmx_init);
1284 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1286 vfree(dvbdemux->cnt_storage);
1287 vfree(dvbdemux->filter);
1288 vfree(dvbdemux->feed);
1291 EXPORT_SYMBOL(dvb_dmx_release);