root/drivers/media/dvb-core/dvb_demux.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. section_length
  2. ts_pid
  3. payload
  4. dvb_dmx_crc32
  5. dvb_dmx_memcopy
  6. dvb_dmx_swfilter_payload
  7. dvb_dmx_swfilter_sectionfilter
  8. dvb_dmx_swfilter_section_feed
  9. dvb_dmx_swfilter_section_new
  10. dvb_dmx_swfilter_section_copy_dump
  11. dvb_dmx_swfilter_section_packet
  12. dvb_dmx_swfilter_packet_type
  13. dvb_dmx_swfilter_packet
  14. dvb_dmx_swfilter_packets
  15. find_next_packet
  16. _dvb_dmx_swfilter
  17. dvb_dmx_swfilter
  18. dvb_dmx_swfilter_204
  19. dvb_dmx_swfilter_raw
  20. dvb_dmx_filter_alloc
  21. dvb_dmx_feed_alloc
  22. dvb_demux_feed_find
  23. dvb_demux_feed_add
  24. dvb_demux_feed_del
  25. dmx_ts_feed_set
  26. dmx_ts_feed_start_filtering
  27. dmx_ts_feed_stop_filtering
  28. dvbdmx_allocate_ts_feed
  29. dvbdmx_release_ts_feed
  30. dmx_section_feed_allocate_filter
  31. dmx_section_feed_set
  32. prepare_secfilters
  33. dmx_section_feed_start_filtering
  34. dmx_section_feed_stop_filtering
  35. dmx_section_feed_release_filter
  36. dvbdmx_allocate_section_feed
  37. dvbdmx_release_section_feed
  38. dvbdmx_open
  39. dvbdmx_close
  40. dvbdmx_write
  41. dvbdmx_add_frontend
  42. dvbdmx_remove_frontend
  43. dvbdmx_get_frontends
  44. dvbdmx_connect_frontend
  45. dvbdmx_disconnect_frontend
  46. dvbdmx_get_pes_pids
  47. dvb_dmx_init
  48. dvb_dmx_release

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

/* [<][>][^][v][top][bottom][index][help] */