]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/media/dvb/dvb-core/dvb_demux.c
V4L/DVB: dvb_demux: Don't use vmalloc at dvb_dmx_swfilter_packet
[linux-2.6.git] / drivers / media / dvb / dvb-core / dvb_demux.c
1 /*
2  * dvb_demux.c - DVB kernel demux API
3  *
4  * Copyright (C) 2000-2001 Ralph  Metzler <ralph@convergence.de>
5  *                     & Marcus Metzler <marcus@convergence.de>
6  *                       for convergence integrated media GmbH
7  *
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.
12  *
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.
17  *
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.
21  *
22  */
23
24 #include <linux/sched.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/vmalloc.h>
28 #include <linux/module.h>
29 #include <linux/poll.h>
30 #include <linux/string.h>
31 #include <linux/crc32.h>
32 #include <asm/uaccess.h>
33 #include <asm/div64.h>
34
35 #include "dvb_demux.h"
36
37 #define NOBUFS
38 /*
39 ** #define DVB_DEMUX_SECTION_LOSS_LOG to monitor payload loss in the syslog
40 */
41 // #define DVB_DEMUX_SECTION_LOSS_LOG
42
43 static int dvb_demux_tscheck;
44 module_param(dvb_demux_tscheck, int, 0644);
45 MODULE_PARM_DESC(dvb_demux_tscheck,
46                 "enable transport stream continuity and TEI check");
47
48 static int dvb_demux_speedcheck;
49 module_param(dvb_demux_speedcheck, int, 0644);
50 MODULE_PARM_DESC(dvb_demux_speedcheck,
51                 "enable transport stream speed check");
52
53 #define dprintk_tscheck(x...) do {                              \
54                 if (dvb_demux_tscheck && printk_ratelimit())    \
55                         printk(x);                              \
56         } while (0)
57
58 /******************************************************************************
59  * static inlined helper functions
60  ******************************************************************************/
61
62 static inline u16 section_length(const u8 *buf)
63 {
64         return 3 + ((buf[1] & 0x0f) << 8) + buf[2];
65 }
66
67 static inline u16 ts_pid(const u8 *buf)
68 {
69         return ((buf[1] & 0x1f) << 8) + buf[2];
70 }
71
72 static inline u8 payload(const u8 *tsp)
73 {
74         if (!(tsp[3] & 0x10))   // no payload?
75                 return 0;
76
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];
82         }
83
84         return 184;
85 }
86
87 static u32 dvb_dmx_crc32(struct dvb_demux_feed *f, const u8 *src, size_t len)
88 {
89         return (f->feed.sec.crc_val = crc32_be(f->feed.sec.crc_val, src, len));
90 }
91
92 static void dvb_dmx_memcopy(struct dvb_demux_feed *f, u8 *d, const u8 *s,
93                             size_t len)
94 {
95         memcpy(d, s, len);
96 }
97
98 /******************************************************************************
99  * Software filter functions
100  ******************************************************************************/
101
102 static inline int dvb_dmx_swfilter_payload(struct dvb_demux_feed *feed,
103                                            const u8 *buf)
104 {
105         int count = payload(buf);
106         int p;
107         //int ccok;
108         //u8 cc;
109
110         if (count == 0)
111                 return -1;
112
113         p = 188 - count;
114
115         /*
116         cc = buf[3] & 0x0f;
117         ccok = ((feed->cc + 1) & 0x0f) == cc;
118         feed->cc = cc;
119         if (!ccok)
120                 printk("missed packet!\n");
121         */
122
123         if (buf[1] & 0x40)      // PUSI ?
124                 feed->peslen = 0xfffa;
125
126         feed->peslen += count;
127
128         return feed->cb.ts(&buf[p], count, NULL, 0, &feed->feed.ts, DMX_OK);
129 }
130
131 static int dvb_dmx_swfilter_sectionfilter(struct dvb_demux_feed *feed,
132                                           struct dvb_demux_filter *f)
133 {
134         u8 neq = 0;
135         int i;
136
137         for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
138                 u8 xor = f->filter.filter_value[i] ^ feed->feed.sec.secbuf[i];
139
140                 if (f->maskandmode[i] & xor)
141                         return 0;
142
143                 neq |= f->maskandnotmode[i] & xor;
144         }
145
146         if (f->doneq && !neq)
147                 return 0;
148
149         return feed->cb.sec(feed->feed.sec.secbuf, feed->feed.sec.seclen,
150                             NULL, 0, &f->filter, DMX_OK);
151 }
152
153 static inline int dvb_dmx_swfilter_section_feed(struct dvb_demux_feed *feed)
154 {
155         struct dvb_demux *demux = feed->demux;
156         struct dvb_demux_filter *f = feed->filter;
157         struct dmx_section_feed *sec = &feed->feed.sec;
158         int section_syntax_indicator;
159
160         if (!sec->is_filtering)
161                 return 0;
162
163         if (!f)
164                 return 0;
165
166         if (sec->check_crc) {
167                 section_syntax_indicator = ((sec->secbuf[1] & 0x80) != 0);
168                 if (section_syntax_indicator &&
169                     demux->check_crc32(feed, sec->secbuf, sec->seclen))
170                         return -1;
171         }
172
173         do {
174                 if (dvb_dmx_swfilter_sectionfilter(feed, f) < 0)
175                         return -1;
176         } while ((f = f->next) && sec->is_filtering);
177
178         sec->seclen = 0;
179
180         return 0;
181 }
182
183 static void dvb_dmx_swfilter_section_new(struct dvb_demux_feed *feed)
184 {
185         struct dmx_section_feed *sec = &feed->feed.sec;
186
187 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
188         if (sec->secbufp < sec->tsfeedp) {
189                 int i, n = sec->tsfeedp - sec->secbufp;
190
191                 /*
192                  * Section padding is done with 0xff bytes entirely.
193                  * Due to speed reasons, we won't check all of them
194                  * but just first and last.
195                  */
196                 if (sec->secbuf[0] != 0xff || sec->secbuf[n - 1] != 0xff) {
197                         printk("dvb_demux.c section ts padding loss: %d/%d\n",
198                                n, sec->tsfeedp);
199                         printk("dvb_demux.c pad data:");
200                         for (i = 0; i < n; i++)
201                                 printk(" %02x", sec->secbuf[i]);
202                         printk("\n");
203                 }
204         }
205 #endif
206
207         sec->tsfeedp = sec->secbufp = sec->seclen = 0;
208         sec->secbuf = sec->secbuf_base;
209 }
210
211 /*
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)
217  *
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.
222  *
223  * Fix:
224  * when demux is started, let feed->pusi_seen = 0 to
225  * prevent initial feeding of garbage from the end of
226  * previous section. When you for the first time see PUSI=1
227  * then set feed->pusi_seen = 1
228  */
229 static int dvb_dmx_swfilter_section_copy_dump(struct dvb_demux_feed *feed,
230                                               const u8 *buf, u8 len)
231 {
232         struct dvb_demux *demux = feed->demux;
233         struct dmx_section_feed *sec = &feed->feed.sec;
234         u16 limit, seclen, n;
235
236         if (sec->tsfeedp >= DMX_MAX_SECFEED_SIZE)
237                 return 0;
238
239         if (sec->tsfeedp + len > DMX_MAX_SECFEED_SIZE) {
240 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
241                 printk("dvb_demux.c section buffer full loss: %d/%d\n",
242                        sec->tsfeedp + len - DMX_MAX_SECFEED_SIZE,
243                        DMX_MAX_SECFEED_SIZE);
244 #endif
245                 len = DMX_MAX_SECFEED_SIZE - sec->tsfeedp;
246         }
247
248         if (len <= 0)
249                 return 0;
250
251         demux->memcopy(feed, sec->secbuf_base + sec->tsfeedp, buf, len);
252         sec->tsfeedp += len;
253
254         /*
255          * Dump all the sections we can find in the data (Emard)
256          */
257         limit = sec->tsfeedp;
258         if (limit > DMX_MAX_SECFEED_SIZE)
259                 return -1;      /* internal error should never happen */
260
261         /* to be sure always set secbuf */
262         sec->secbuf = sec->secbuf_base + sec->secbufp;
263
264         for (n = 0; sec->secbufp + 2 < limit; n++) {
265                 seclen = section_length(sec->secbuf);
266                 if (seclen <= 0 || seclen > DMX_MAX_SECTION_SIZE
267                     || seclen + sec->secbufp > limit)
268                         return 0;
269                 sec->seclen = seclen;
270                 sec->crc_val = ~0;
271                 /* dump [secbuf .. secbuf+seclen) */
272                 if (feed->pusi_seen)
273                         dvb_dmx_swfilter_section_feed(feed);
274 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
275                 else
276                         printk("dvb_demux.c pusi not seen, discarding section data\n");
277 #endif
278                 sec->secbufp += seclen; /* secbufp and secbuf moving together is */
279                 sec->secbuf += seclen;  /* redundant but saves pointer arithmetic */
280         }
281
282         return 0;
283 }
284
285 static int dvb_dmx_swfilter_section_packet(struct dvb_demux_feed *feed,
286                                            const u8 *buf)
287 {
288         u8 p, count;
289         int ccok, dc_i = 0;
290         u8 cc;
291
292         count = payload(buf);
293
294         if (count == 0)         /* count == 0 if no payload or out of range */
295                 return -1;
296
297         p = 188 - count;        /* payload start */
298
299         cc = buf[3] & 0x0f;
300         ccok = ((feed->cc + 1) & 0x0f) == cc;
301         feed->cc = cc;
302
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;
307         }
308
309         if (!ccok || dc_i) {
310 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
311                 printk("dvb_demux.c discontinuity detected %d bytes lost\n",
312                        count);
313                 /*
314                  * those bytes under sume circumstances will again be reported
315                  * in the following dvb_dmx_swfilter_section_new
316                  */
317 #endif
318                 /*
319                  * Discontinuity detected. Reset pusi_seen = 0 to
320                  * stop feeding of suspicious data until next PUSI=1 arrives
321                  */
322                 feed->pusi_seen = 0;
323                 dvb_dmx_swfilter_section_new(feed);
324         }
325
326         if (buf[1] & 0x40) {
327                 /* PUSI=1 (is set), section boundary is here */
328                 if (count > 1 && buf[p] < count) {
329                         const u8 *before = &buf[p + 1];
330                         u8 before_len = buf[p];
331                         const u8 *after = &before[before_len];
332                         u8 after_len = count - 1 - before_len;
333
334                         dvb_dmx_swfilter_section_copy_dump(feed, before,
335                                                            before_len);
336                         /* before start of new section, set pusi_seen = 1 */
337                         feed->pusi_seen = 1;
338                         dvb_dmx_swfilter_section_new(feed);
339                         dvb_dmx_swfilter_section_copy_dump(feed, after,
340                                                            after_len);
341                 }
342 #ifdef DVB_DEMUX_SECTION_LOSS_LOG
343                 else if (count > 0)
344                         printk("dvb_demux.c PUSI=1 but %d bytes lost\n", count);
345 #endif
346         } else {
347                 /* PUSI=0 (is not set), no section boundary */
348                 dvb_dmx_swfilter_section_copy_dump(feed, &buf[p], count);
349         }
350
351         return 0;
352 }
353
354 static inline void dvb_dmx_swfilter_packet_type(struct dvb_demux_feed *feed,
355                                                 const u8 *buf)
356 {
357         switch (feed->type) {
358         case DMX_TYPE_TS:
359                 if (!feed->feed.ts.is_filtering)
360                         break;
361                 if (feed->ts_type & TS_PACKET) {
362                         if (feed->ts_type & TS_PAYLOAD_ONLY)
363                                 dvb_dmx_swfilter_payload(feed, buf);
364                         else
365                                 feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts,
366                                             DMX_OK);
367                 }
368                 if (feed->ts_type & TS_DECODER)
369                         if (feed->demux->write_to_decoder)
370                                 feed->demux->write_to_decoder(feed, buf, 188);
371                 break;
372
373         case DMX_TYPE_SEC:
374                 if (!feed->feed.sec.is_filtering)
375                         break;
376                 if (dvb_dmx_swfilter_section_packet(feed, buf) < 0)
377                         feed->feed.sec.seclen = feed->feed.sec.secbufp = 0;
378                 break;
379
380         default:
381                 break;
382         }
383 }
384
385 #define DVR_FEED(f)                                                     \
386         (((f)->type == DMX_TYPE_TS) &&                                  \
387         ((f)->feed.ts.is_filtering) &&                                  \
388         (((f)->ts_type & (TS_PACKET | TS_DEMUX)) == TS_PACKET))
389
390 static void dvb_dmx_swfilter_packet(struct dvb_demux *demux, const u8 *buf)
391 {
392         struct dvb_demux_feed *feed;
393         u16 pid = ts_pid(buf);
394         int dvr_done = 0;
395
396         if (dvb_demux_speedcheck) {
397                 struct timespec cur_time, delta_time;
398                 u64 speed_bytes, speed_timedelta;
399
400                 demux->speed_pkts_cnt++;
401
402                 /* show speed every SPEED_PKTS_INTERVAL packets */
403                 if (!(demux->speed_pkts_cnt % SPEED_PKTS_INTERVAL)) {
404                         cur_time = current_kernel_time();
405
406                         if (demux->speed_last_time.tv_sec != 0 &&
407                                         demux->speed_last_time.tv_nsec != 0) {
408                                 delta_time = timespec_sub(cur_time,
409                                                 demux->speed_last_time);
410                                 speed_bytes = (u64)demux->speed_pkts_cnt
411                                         * 188 * 8;
412                                 /* convert to 1024 basis */
413                                 speed_bytes = 1000 * div64_u64(speed_bytes,
414                                                 1024);
415                                 speed_timedelta =
416                                         (u64)timespec_to_ns(&delta_time);
417                                 speed_timedelta = div64_u64(speed_timedelta,
418                                                 1000000); /* nsec -> usec */
419                                 printk(KERN_INFO "TS speed %llu Kbits/sec \n",
420                                                 div64_u64(speed_bytes,
421                                                         speed_timedelta));
422                         };
423
424                         demux->speed_last_time = cur_time;
425                         demux->speed_pkts_cnt = 0;
426                 };
427         };
428
429         if (demux->cnt_storage) {
430                 /* check pkt counter */
431                 if (pid < MAX_PID) {
432                         if (buf[1] & 0x80)
433                                 dprintk_tscheck("TEI detected. "
434                                                 "PID=0x%x data1=0x%x\n",
435                                                 pid, buf[1]);
436
437                         if ((buf[3] & 0xf) != demux->cnt_storage[pid])
438                                 dprintk_tscheck("TS packet counter mismatch. "
439                                                 "PID=0x%x expected 0x%x "
440                                                 "got 0x%x\n",
441                                                 pid, demux->cnt_storage[pid],
442                                                 buf[3] & 0xf);
443
444                         demux->cnt_storage[pid] = ((buf[3] & 0xf) + 1)&0xf;
445                 };
446                 /* end check */
447         };
448
449         list_for_each_entry(feed, &demux->feed_list, list_head) {
450                 if ((feed->pid != pid) && (feed->pid != 0x2000))
451                         continue;
452
453                 /* copy each packet only once to the dvr device, even
454                  * if a PID is in multiple filters (e.g. video + PCR) */
455                 if ((DVR_FEED(feed)) && (dvr_done++))
456                         continue;
457
458                 if (feed->pid == pid)
459                         dvb_dmx_swfilter_packet_type(feed, buf);
460                 else if (feed->pid == 0x2000)
461                         feed->cb.ts(buf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
462         }
463 }
464
465 void dvb_dmx_swfilter_packets(struct dvb_demux *demux, const u8 *buf,
466                               size_t count)
467 {
468         spin_lock(&demux->lock);
469
470         while (count--) {
471                 if (buf[0] == 0x47)
472                         dvb_dmx_swfilter_packet(demux, buf);
473                 buf += 188;
474         }
475
476         spin_unlock(&demux->lock);
477 }
478
479 EXPORT_SYMBOL(dvb_dmx_swfilter_packets);
480
481 void dvb_dmx_swfilter(struct dvb_demux *demux, const u8 *buf, size_t count)
482 {
483         int p = 0, i, j;
484
485         spin_lock(&demux->lock);
486
487         if (demux->tsbufp) {
488                 i = demux->tsbufp;
489                 j = 188 - i;
490                 if (count < j) {
491                         memcpy(&demux->tsbuf[i], buf, count);
492                         demux->tsbufp += count;
493                         goto bailout;
494                 }
495                 memcpy(&demux->tsbuf[i], buf, j);
496                 if (demux->tsbuf[0] == 0x47)
497                         dvb_dmx_swfilter_packet(demux, demux->tsbuf);
498                 demux->tsbufp = 0;
499                 p += j;
500         }
501
502         while (p < count) {
503                 if (buf[p] == 0x47) {
504                         if (count - p >= 188) {
505                                 dvb_dmx_swfilter_packet(demux, &buf[p]);
506                                 p += 188;
507                         } else {
508                                 i = count - p;
509                                 memcpy(demux->tsbuf, &buf[p], i);
510                                 demux->tsbufp = i;
511                                 goto bailout;
512                         }
513                 } else
514                         p++;
515         }
516
517 bailout:
518         spin_unlock(&demux->lock);
519 }
520
521 EXPORT_SYMBOL(dvb_dmx_swfilter);
522
523 void dvb_dmx_swfilter_204(struct dvb_demux *demux, const u8 *buf, size_t count)
524 {
525         int p = 0, i, j;
526         u8 tmppack[188];
527
528         spin_lock(&demux->lock);
529
530         if (demux->tsbufp) {
531                 i = demux->tsbufp;
532                 j = 204 - i;
533                 if (count < j) {
534                         memcpy(&demux->tsbuf[i], buf, count);
535                         demux->tsbufp += count;
536                         goto bailout;
537                 }
538                 memcpy(&demux->tsbuf[i], buf, j);
539                 if ((demux->tsbuf[0] == 0x47) || (demux->tsbuf[0] == 0xB8)) {
540                         memcpy(tmppack, demux->tsbuf, 188);
541                         if (tmppack[0] == 0xB8)
542                                 tmppack[0] = 0x47;
543                         dvb_dmx_swfilter_packet(demux, tmppack);
544                 }
545                 demux->tsbufp = 0;
546                 p += j;
547         }
548
549         while (p < count) {
550                 if ((buf[p] == 0x47) || (buf[p] == 0xB8)) {
551                         if (count - p >= 204) {
552                                 memcpy(tmppack, &buf[p], 188);
553                                 if (tmppack[0] == 0xB8)
554                                         tmppack[0] = 0x47;
555                                 dvb_dmx_swfilter_packet(demux, tmppack);
556                                 p += 204;
557                         } else {
558                                 i = count - p;
559                                 memcpy(demux->tsbuf, &buf[p], i);
560                                 demux->tsbufp = i;
561                                 goto bailout;
562                         }
563                 } else {
564                         p++;
565                 }
566         }
567
568 bailout:
569         spin_unlock(&demux->lock);
570 }
571
572 EXPORT_SYMBOL(dvb_dmx_swfilter_204);
573
574 static struct dvb_demux_filter *dvb_dmx_filter_alloc(struct dvb_demux *demux)
575 {
576         int i;
577
578         for (i = 0; i < demux->filternum; i++)
579                 if (demux->filter[i].state == DMX_STATE_FREE)
580                         break;
581
582         if (i == demux->filternum)
583                 return NULL;
584
585         demux->filter[i].state = DMX_STATE_ALLOCATED;
586
587         return &demux->filter[i];
588 }
589
590 static struct dvb_demux_feed *dvb_dmx_feed_alloc(struct dvb_demux *demux)
591 {
592         int i;
593
594         for (i = 0; i < demux->feednum; i++)
595                 if (demux->feed[i].state == DMX_STATE_FREE)
596                         break;
597
598         if (i == demux->feednum)
599                 return NULL;
600
601         demux->feed[i].state = DMX_STATE_ALLOCATED;
602
603         return &demux->feed[i];
604 }
605
606 static int dvb_demux_feed_find(struct dvb_demux_feed *feed)
607 {
608         struct dvb_demux_feed *entry;
609
610         list_for_each_entry(entry, &feed->demux->feed_list, list_head)
611                 if (entry == feed)
612                         return 1;
613
614         return 0;
615 }
616
617 static void dvb_demux_feed_add(struct dvb_demux_feed *feed)
618 {
619         spin_lock_irq(&feed->demux->lock);
620         if (dvb_demux_feed_find(feed)) {
621                 printk(KERN_ERR "%s: feed already in list (type=%x state=%x pid=%x)\n",
622                        __func__, feed->type, feed->state, feed->pid);
623                 goto out;
624         }
625
626         list_add(&feed->list_head, &feed->demux->feed_list);
627 out:
628         spin_unlock_irq(&feed->demux->lock);
629 }
630
631 static void dvb_demux_feed_del(struct dvb_demux_feed *feed)
632 {
633         spin_lock_irq(&feed->demux->lock);
634         if (!(dvb_demux_feed_find(feed))) {
635                 printk(KERN_ERR "%s: feed not in list (type=%x state=%x pid=%x)\n",
636                        __func__, feed->type, feed->state, feed->pid);
637                 goto out;
638         }
639
640         list_del(&feed->list_head);
641 out:
642         spin_unlock_irq(&feed->demux->lock);
643 }
644
645 static int dmx_ts_feed_set(struct dmx_ts_feed *ts_feed, u16 pid, int ts_type,
646                            enum dmx_ts_pes pes_type,
647                            size_t circular_buffer_size, struct timespec timeout)
648 {
649         struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
650         struct dvb_demux *demux = feed->demux;
651
652         if (pid > DMX_MAX_PID)
653                 return -EINVAL;
654
655         if (mutex_lock_interruptible(&demux->mutex))
656                 return -ERESTARTSYS;
657
658         if (ts_type & TS_DECODER) {
659                 if (pes_type >= DMX_TS_PES_OTHER) {
660                         mutex_unlock(&demux->mutex);
661                         return -EINVAL;
662                 }
663
664                 if (demux->pesfilter[pes_type] &&
665                     demux->pesfilter[pes_type] != feed) {
666                         mutex_unlock(&demux->mutex);
667                         return -EINVAL;
668                 }
669
670                 demux->pesfilter[pes_type] = feed;
671                 demux->pids[pes_type] = pid;
672         }
673
674         dvb_demux_feed_add(feed);
675
676         feed->pid = pid;
677         feed->buffer_size = circular_buffer_size;
678         feed->timeout = timeout;
679         feed->ts_type = ts_type;
680         feed->pes_type = pes_type;
681
682         if (feed->buffer_size) {
683 #ifdef NOBUFS
684                 feed->buffer = NULL;
685 #else
686                 feed->buffer = vmalloc(feed->buffer_size);
687                 if (!feed->buffer) {
688                         mutex_unlock(&demux->mutex);
689                         return -ENOMEM;
690                 }
691 #endif
692         }
693
694         feed->state = DMX_STATE_READY;
695         mutex_unlock(&demux->mutex);
696
697         return 0;
698 }
699
700 static int dmx_ts_feed_start_filtering(struct dmx_ts_feed *ts_feed)
701 {
702         struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
703         struct dvb_demux *demux = feed->demux;
704         int ret;
705
706         if (mutex_lock_interruptible(&demux->mutex))
707                 return -ERESTARTSYS;
708
709         if (feed->state != DMX_STATE_READY || feed->type != DMX_TYPE_TS) {
710                 mutex_unlock(&demux->mutex);
711                 return -EINVAL;
712         }
713
714         if (!demux->start_feed) {
715                 mutex_unlock(&demux->mutex);
716                 return -ENODEV;
717         }
718
719         if ((ret = demux->start_feed(feed)) < 0) {
720                 mutex_unlock(&demux->mutex);
721                 return ret;
722         }
723
724         spin_lock_irq(&demux->lock);
725         ts_feed->is_filtering = 1;
726         feed->state = DMX_STATE_GO;
727         spin_unlock_irq(&demux->lock);
728         mutex_unlock(&demux->mutex);
729
730         return 0;
731 }
732
733 static int dmx_ts_feed_stop_filtering(struct dmx_ts_feed *ts_feed)
734 {
735         struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
736         struct dvb_demux *demux = feed->demux;
737         int ret;
738
739         mutex_lock(&demux->mutex);
740
741         if (feed->state < DMX_STATE_GO) {
742                 mutex_unlock(&demux->mutex);
743                 return -EINVAL;
744         }
745
746         if (!demux->stop_feed) {
747                 mutex_unlock(&demux->mutex);
748                 return -ENODEV;
749         }
750
751         ret = demux->stop_feed(feed);
752
753         spin_lock_irq(&demux->lock);
754         ts_feed->is_filtering = 0;
755         feed->state = DMX_STATE_ALLOCATED;
756         spin_unlock_irq(&demux->lock);
757         mutex_unlock(&demux->mutex);
758
759         return ret;
760 }
761
762 static int dvbdmx_allocate_ts_feed(struct dmx_demux *dmx,
763                                    struct dmx_ts_feed **ts_feed,
764                                    dmx_ts_cb callback)
765 {
766         struct dvb_demux *demux = (struct dvb_demux *)dmx;
767         struct dvb_demux_feed *feed;
768
769         if (mutex_lock_interruptible(&demux->mutex))
770                 return -ERESTARTSYS;
771
772         if (!(feed = dvb_dmx_feed_alloc(demux))) {
773                 mutex_unlock(&demux->mutex);
774                 return -EBUSY;
775         }
776
777         feed->type = DMX_TYPE_TS;
778         feed->cb.ts = callback;
779         feed->demux = demux;
780         feed->pid = 0xffff;
781         feed->peslen = 0xfffa;
782         feed->buffer = NULL;
783
784         (*ts_feed) = &feed->feed.ts;
785         (*ts_feed)->parent = dmx;
786         (*ts_feed)->priv = NULL;
787         (*ts_feed)->is_filtering = 0;
788         (*ts_feed)->start_filtering = dmx_ts_feed_start_filtering;
789         (*ts_feed)->stop_filtering = dmx_ts_feed_stop_filtering;
790         (*ts_feed)->set = dmx_ts_feed_set;
791
792         if (!(feed->filter = dvb_dmx_filter_alloc(demux))) {
793                 feed->state = DMX_STATE_FREE;
794                 mutex_unlock(&demux->mutex);
795                 return -EBUSY;
796         }
797
798         feed->filter->type = DMX_TYPE_TS;
799         feed->filter->feed = feed;
800         feed->filter->state = DMX_STATE_READY;
801
802         mutex_unlock(&demux->mutex);
803
804         return 0;
805 }
806
807 static int dvbdmx_release_ts_feed(struct dmx_demux *dmx,
808                                   struct dmx_ts_feed *ts_feed)
809 {
810         struct dvb_demux *demux = (struct dvb_demux *)dmx;
811         struct dvb_demux_feed *feed = (struct dvb_demux_feed *)ts_feed;
812
813         mutex_lock(&demux->mutex);
814
815         if (feed->state == DMX_STATE_FREE) {
816                 mutex_unlock(&demux->mutex);
817                 return -EINVAL;
818         }
819 #ifndef NOBUFS
820         vfree(feed->buffer);
821         feed->buffer = NULL;
822 #endif
823
824         feed->state = DMX_STATE_FREE;
825         feed->filter->state = DMX_STATE_FREE;
826
827         dvb_demux_feed_del(feed);
828
829         feed->pid = 0xffff;
830
831         if (feed->ts_type & TS_DECODER && feed->pes_type < DMX_TS_PES_OTHER)
832                 demux->pesfilter[feed->pes_type] = NULL;
833
834         mutex_unlock(&demux->mutex);
835         return 0;
836 }
837
838 /******************************************************************************
839  * dmx_section_feed API calls
840  ******************************************************************************/
841
842 static int dmx_section_feed_allocate_filter(struct dmx_section_feed *feed,
843                                             struct dmx_section_filter **filter)
844 {
845         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
846         struct dvb_demux *dvbdemux = dvbdmxfeed->demux;
847         struct dvb_demux_filter *dvbdmxfilter;
848
849         if (mutex_lock_interruptible(&dvbdemux->mutex))
850                 return -ERESTARTSYS;
851
852         dvbdmxfilter = dvb_dmx_filter_alloc(dvbdemux);
853         if (!dvbdmxfilter) {
854                 mutex_unlock(&dvbdemux->mutex);
855                 return -EBUSY;
856         }
857
858         spin_lock_irq(&dvbdemux->lock);
859         *filter = &dvbdmxfilter->filter;
860         (*filter)->parent = feed;
861         (*filter)->priv = NULL;
862         dvbdmxfilter->feed = dvbdmxfeed;
863         dvbdmxfilter->type = DMX_TYPE_SEC;
864         dvbdmxfilter->state = DMX_STATE_READY;
865         dvbdmxfilter->next = dvbdmxfeed->filter;
866         dvbdmxfeed->filter = dvbdmxfilter;
867         spin_unlock_irq(&dvbdemux->lock);
868
869         mutex_unlock(&dvbdemux->mutex);
870         return 0;
871 }
872
873 static int dmx_section_feed_set(struct dmx_section_feed *feed,
874                                 u16 pid, size_t circular_buffer_size,
875                                 int check_crc)
876 {
877         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
878         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
879
880         if (pid > 0x1fff)
881                 return -EINVAL;
882
883         if (mutex_lock_interruptible(&dvbdmx->mutex))
884                 return -ERESTARTSYS;
885
886         dvb_demux_feed_add(dvbdmxfeed);
887
888         dvbdmxfeed->pid = pid;
889         dvbdmxfeed->buffer_size = circular_buffer_size;
890         dvbdmxfeed->feed.sec.check_crc = check_crc;
891
892 #ifdef NOBUFS
893         dvbdmxfeed->buffer = NULL;
894 #else
895         dvbdmxfeed->buffer = vmalloc(dvbdmxfeed->buffer_size);
896         if (!dvbdmxfeed->buffer) {
897                 mutex_unlock(&dvbdmx->mutex);
898                 return -ENOMEM;
899         }
900 #endif
901
902         dvbdmxfeed->state = DMX_STATE_READY;
903         mutex_unlock(&dvbdmx->mutex);
904         return 0;
905 }
906
907 static void prepare_secfilters(struct dvb_demux_feed *dvbdmxfeed)
908 {
909         int i;
910         struct dvb_demux_filter *f;
911         struct dmx_section_filter *sf;
912         u8 mask, mode, doneq;
913
914         if (!(f = dvbdmxfeed->filter))
915                 return;
916         do {
917                 sf = &f->filter;
918                 doneq = 0;
919                 for (i = 0; i < DVB_DEMUX_MASK_MAX; i++) {
920                         mode = sf->filter_mode[i];
921                         mask = sf->filter_mask[i];
922                         f->maskandmode[i] = mask & mode;
923                         doneq |= f->maskandnotmode[i] = mask & ~mode;
924                 }
925                 f->doneq = doneq ? 1 : 0;
926         } while ((f = f->next));
927 }
928
929 static int dmx_section_feed_start_filtering(struct dmx_section_feed *feed)
930 {
931         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
932         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
933         int ret;
934
935         if (mutex_lock_interruptible(&dvbdmx->mutex))
936                 return -ERESTARTSYS;
937
938         if (feed->is_filtering) {
939                 mutex_unlock(&dvbdmx->mutex);
940                 return -EBUSY;
941         }
942
943         if (!dvbdmxfeed->filter) {
944                 mutex_unlock(&dvbdmx->mutex);
945                 return -EINVAL;
946         }
947
948         dvbdmxfeed->feed.sec.tsfeedp = 0;
949         dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
950         dvbdmxfeed->feed.sec.secbufp = 0;
951         dvbdmxfeed->feed.sec.seclen = 0;
952
953         if (!dvbdmx->start_feed) {
954                 mutex_unlock(&dvbdmx->mutex);
955                 return -ENODEV;
956         }
957
958         prepare_secfilters(dvbdmxfeed);
959
960         if ((ret = dvbdmx->start_feed(dvbdmxfeed)) < 0) {
961                 mutex_unlock(&dvbdmx->mutex);
962                 return ret;
963         }
964
965         spin_lock_irq(&dvbdmx->lock);
966         feed->is_filtering = 1;
967         dvbdmxfeed->state = DMX_STATE_GO;
968         spin_unlock_irq(&dvbdmx->lock);
969
970         mutex_unlock(&dvbdmx->mutex);
971         return 0;
972 }
973
974 static int dmx_section_feed_stop_filtering(struct dmx_section_feed *feed)
975 {
976         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
977         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
978         int ret;
979
980         mutex_lock(&dvbdmx->mutex);
981
982         if (!dvbdmx->stop_feed) {
983                 mutex_unlock(&dvbdmx->mutex);
984                 return -ENODEV;
985         }
986
987         ret = dvbdmx->stop_feed(dvbdmxfeed);
988
989         spin_lock_irq(&dvbdmx->lock);
990         dvbdmxfeed->state = DMX_STATE_READY;
991         feed->is_filtering = 0;
992         spin_unlock_irq(&dvbdmx->lock);
993
994         mutex_unlock(&dvbdmx->mutex);
995         return ret;
996 }
997
998 static int dmx_section_feed_release_filter(struct dmx_section_feed *feed,
999                                            struct dmx_section_filter *filter)
1000 {
1001         struct dvb_demux_filter *dvbdmxfilter = (struct dvb_demux_filter *)filter, *f;
1002         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1003         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
1004
1005         mutex_lock(&dvbdmx->mutex);
1006
1007         if (dvbdmxfilter->feed != dvbdmxfeed) {
1008                 mutex_unlock(&dvbdmx->mutex);
1009                 return -EINVAL;
1010         }
1011
1012         if (feed->is_filtering)
1013                 feed->stop_filtering(feed);
1014
1015         spin_lock_irq(&dvbdmx->lock);
1016         f = dvbdmxfeed->filter;
1017
1018         if (f == dvbdmxfilter) {
1019                 dvbdmxfeed->filter = dvbdmxfilter->next;
1020         } else {
1021                 while (f->next != dvbdmxfilter)
1022                         f = f->next;
1023                 f->next = f->next->next;
1024         }
1025
1026         dvbdmxfilter->state = DMX_STATE_FREE;
1027         spin_unlock_irq(&dvbdmx->lock);
1028         mutex_unlock(&dvbdmx->mutex);
1029         return 0;
1030 }
1031
1032 static int dvbdmx_allocate_section_feed(struct dmx_demux *demux,
1033                                         struct dmx_section_feed **feed,
1034                                         dmx_section_cb callback)
1035 {
1036         struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1037         struct dvb_demux_feed *dvbdmxfeed;
1038
1039         if (mutex_lock_interruptible(&dvbdmx->mutex))
1040                 return -ERESTARTSYS;
1041
1042         if (!(dvbdmxfeed = dvb_dmx_feed_alloc(dvbdmx))) {
1043                 mutex_unlock(&dvbdmx->mutex);
1044                 return -EBUSY;
1045         }
1046
1047         dvbdmxfeed->type = DMX_TYPE_SEC;
1048         dvbdmxfeed->cb.sec = callback;
1049         dvbdmxfeed->demux = dvbdmx;
1050         dvbdmxfeed->pid = 0xffff;
1051         dvbdmxfeed->feed.sec.secbuf = dvbdmxfeed->feed.sec.secbuf_base;
1052         dvbdmxfeed->feed.sec.secbufp = dvbdmxfeed->feed.sec.seclen = 0;
1053         dvbdmxfeed->feed.sec.tsfeedp = 0;
1054         dvbdmxfeed->filter = NULL;
1055         dvbdmxfeed->buffer = NULL;
1056
1057         (*feed) = &dvbdmxfeed->feed.sec;
1058         (*feed)->is_filtering = 0;
1059         (*feed)->parent = demux;
1060         (*feed)->priv = NULL;
1061
1062         (*feed)->set = dmx_section_feed_set;
1063         (*feed)->allocate_filter = dmx_section_feed_allocate_filter;
1064         (*feed)->start_filtering = dmx_section_feed_start_filtering;
1065         (*feed)->stop_filtering = dmx_section_feed_stop_filtering;
1066         (*feed)->release_filter = dmx_section_feed_release_filter;
1067
1068         mutex_unlock(&dvbdmx->mutex);
1069         return 0;
1070 }
1071
1072 static int dvbdmx_release_section_feed(struct dmx_demux *demux,
1073                                        struct dmx_section_feed *feed)
1074 {
1075         struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *)feed;
1076         struct dvb_demux *dvbdmx = (struct dvb_demux *)demux;
1077
1078         mutex_lock(&dvbdmx->mutex);
1079
1080         if (dvbdmxfeed->state == DMX_STATE_FREE) {
1081                 mutex_unlock(&dvbdmx->mutex);
1082                 return -EINVAL;
1083         }
1084 #ifndef NOBUFS
1085         vfree(dvbdmxfeed->buffer);
1086         dvbdmxfeed->buffer = NULL;
1087 #endif
1088         dvbdmxfeed->state = DMX_STATE_FREE;
1089
1090         dvb_demux_feed_del(dvbdmxfeed);
1091
1092         dvbdmxfeed->pid = 0xffff;
1093
1094         mutex_unlock(&dvbdmx->mutex);
1095         return 0;
1096 }
1097
1098 /******************************************************************************
1099  * dvb_demux kernel data API calls
1100  ******************************************************************************/
1101
1102 static int dvbdmx_open(struct dmx_demux *demux)
1103 {
1104         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1105
1106         if (dvbdemux->users >= MAX_DVB_DEMUX_USERS)
1107                 return -EUSERS;
1108
1109         dvbdemux->users++;
1110         return 0;
1111 }
1112
1113 static int dvbdmx_close(struct dmx_demux *demux)
1114 {
1115         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1116
1117         if (dvbdemux->users == 0)
1118                 return -ENODEV;
1119
1120         dvbdemux->users--;
1121         //FIXME: release any unneeded resources if users==0
1122         return 0;
1123 }
1124
1125 static int dvbdmx_write(struct dmx_demux *demux, const char __user *buf, size_t count)
1126 {
1127         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1128         void *p;
1129
1130         if ((!demux->frontend) || (demux->frontend->source != DMX_MEMORY_FE))
1131                 return -EINVAL;
1132
1133         p = kmalloc(count, GFP_USER);
1134         if (!p)
1135                 return -ENOMEM;
1136         if (copy_from_user(p, buf, count)) {
1137                 kfree(p);
1138                 return -EFAULT;
1139         }
1140         if (mutex_lock_interruptible(&dvbdemux->mutex)) {
1141                 kfree(p);
1142                 return -ERESTARTSYS;
1143         }
1144         dvb_dmx_swfilter(dvbdemux, p, count);
1145         kfree(p);
1146         mutex_unlock(&dvbdemux->mutex);
1147
1148         if (signal_pending(current))
1149                 return -EINTR;
1150         return count;
1151 }
1152
1153 static int dvbdmx_add_frontend(struct dmx_demux *demux,
1154                                struct dmx_frontend *frontend)
1155 {
1156         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1157         struct list_head *head = &dvbdemux->frontend_list;
1158
1159         list_add(&(frontend->connectivity_list), head);
1160
1161         return 0;
1162 }
1163
1164 static int dvbdmx_remove_frontend(struct dmx_demux *demux,
1165                                   struct dmx_frontend *frontend)
1166 {
1167         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1168         struct list_head *pos, *n, *head = &dvbdemux->frontend_list;
1169
1170         list_for_each_safe(pos, n, head) {
1171                 if (DMX_FE_ENTRY(pos) == frontend) {
1172                         list_del(pos);
1173                         return 0;
1174                 }
1175         }
1176
1177         return -ENODEV;
1178 }
1179
1180 static struct list_head *dvbdmx_get_frontends(struct dmx_demux *demux)
1181 {
1182         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1183
1184         if (list_empty(&dvbdemux->frontend_list))
1185                 return NULL;
1186
1187         return &dvbdemux->frontend_list;
1188 }
1189
1190 static int dvbdmx_connect_frontend(struct dmx_demux *demux,
1191                                    struct dmx_frontend *frontend)
1192 {
1193         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1194
1195         if (demux->frontend)
1196                 return -EINVAL;
1197
1198         mutex_lock(&dvbdemux->mutex);
1199
1200         demux->frontend = frontend;
1201         mutex_unlock(&dvbdemux->mutex);
1202         return 0;
1203 }
1204
1205 static int dvbdmx_disconnect_frontend(struct dmx_demux *demux)
1206 {
1207         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1208
1209         mutex_lock(&dvbdemux->mutex);
1210
1211         demux->frontend = NULL;
1212         mutex_unlock(&dvbdemux->mutex);
1213         return 0;
1214 }
1215
1216 static int dvbdmx_get_pes_pids(struct dmx_demux *demux, u16 * pids)
1217 {
1218         struct dvb_demux *dvbdemux = (struct dvb_demux *)demux;
1219
1220         memcpy(pids, dvbdemux->pids, 5 * sizeof(u16));
1221         return 0;
1222 }
1223
1224 int dvb_dmx_init(struct dvb_demux *dvbdemux)
1225 {
1226         int i;
1227         struct dmx_demux *dmx = &dvbdemux->dmx;
1228
1229         dvbdemux->cnt_storage = NULL;
1230         dvbdemux->users = 0;
1231         dvbdemux->filter = vmalloc(dvbdemux->filternum * sizeof(struct dvb_demux_filter));
1232
1233         if (!dvbdemux->filter)
1234                 return -ENOMEM;
1235
1236         dvbdemux->feed = vmalloc(dvbdemux->feednum * sizeof(struct dvb_demux_feed));
1237         if (!dvbdemux->feed) {
1238                 vfree(dvbdemux->filter);
1239                 dvbdemux->filter = NULL;
1240                 return -ENOMEM;
1241         }
1242         for (i = 0; i < dvbdemux->filternum; i++) {
1243                 dvbdemux->filter[i].state = DMX_STATE_FREE;
1244                 dvbdemux->filter[i].index = i;
1245         }
1246         for (i = 0; i < dvbdemux->feednum; i++) {
1247                 dvbdemux->feed[i].state = DMX_STATE_FREE;
1248                 dvbdemux->feed[i].index = i;
1249         }
1250
1251         if (dvb_demux_tscheck) {
1252                 dvbdemux->cnt_storage = vmalloc(MAX_PID + 1);
1253
1254                 if (!dvbdemux->cnt_storage)
1255                         printk(KERN_WARNING "Couldn't allocate memory for TS/TEI check. Disabling it\n");
1256         }
1257
1258         INIT_LIST_HEAD(&dvbdemux->frontend_list);
1259
1260         for (i = 0; i < DMX_TS_PES_OTHER; i++) {
1261                 dvbdemux->pesfilter[i] = NULL;
1262                 dvbdemux->pids[i] = 0xffff;
1263         }
1264
1265         INIT_LIST_HEAD(&dvbdemux->feed_list);
1266
1267         dvbdemux->playing = 0;
1268         dvbdemux->recording = 0;
1269         dvbdemux->tsbufp = 0;
1270
1271         if (!dvbdemux->check_crc32)
1272                 dvbdemux->check_crc32 = dvb_dmx_crc32;
1273
1274         if (!dvbdemux->memcopy)
1275                 dvbdemux->memcopy = dvb_dmx_memcopy;
1276
1277         dmx->frontend = NULL;
1278         dmx->priv = dvbdemux;
1279         dmx->open = dvbdmx_open;
1280         dmx->close = dvbdmx_close;
1281         dmx->write = dvbdmx_write;
1282         dmx->allocate_ts_feed = dvbdmx_allocate_ts_feed;
1283         dmx->release_ts_feed = dvbdmx_release_ts_feed;
1284         dmx->allocate_section_feed = dvbdmx_allocate_section_feed;
1285         dmx->release_section_feed = dvbdmx_release_section_feed;
1286
1287         dmx->add_frontend = dvbdmx_add_frontend;
1288         dmx->remove_frontend = dvbdmx_remove_frontend;
1289         dmx->get_frontends = dvbdmx_get_frontends;
1290         dmx->connect_frontend = dvbdmx_connect_frontend;
1291         dmx->disconnect_frontend = dvbdmx_disconnect_frontend;
1292         dmx->get_pes_pids = dvbdmx_get_pes_pids;
1293
1294         mutex_init(&dvbdemux->mutex);
1295         spin_lock_init(&dvbdemux->lock);
1296
1297         return 0;
1298 }
1299
1300 EXPORT_SYMBOL(dvb_dmx_init);
1301
1302 void dvb_dmx_release(struct dvb_demux *dvbdemux)
1303 {
1304         vfree(dvbdemux->cnt_storage);
1305         vfree(dvbdemux->filter);
1306         vfree(dvbdemux->feed);
1307 }
1308
1309 EXPORT_SYMBOL(dvb_dmx_release);