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