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