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