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