1 /* 2 * dmxdev.c - DVB demultiplexer device 3 * 4 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler 5 * for convergence integrated media GmbH 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public License 9 * as published by the Free Software Foundation; either version 2.1 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 */ 18 19 #define pr_fmt(fmt) "dmxdev: " fmt 20 21 #include <linux/sched.h> 22 #include <linux/spinlock.h> 23 #include <linux/slab.h> 24 #include <linux/vmalloc.h> 25 #include <linux/module.h> 26 #include <linux/poll.h> 27 #include <linux/ioctl.h> 28 #include <linux/wait.h> 29 #include <linux/uaccess.h> 30 #include "dmxdev.h" 31 32 static int debug; 33 34 module_param(debug, int, 0644); 35 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off)."); 36 37 #define dprintk(fmt, arg...) do { \ 38 if (debug) \ 39 printk(KERN_DEBUG pr_fmt("%s: " fmt), \ 40 __func__, ##arg); \ 41 } while (0) 42 43 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf, 44 const u8 *src, size_t len) 45 { 46 ssize_t free; 47 48 if (!len) 49 return 0; 50 if (!buf->data) 51 return 0; 52 53 free = dvb_ringbuffer_free(buf); 54 if (len > free) { 55 dprintk("buffer overflow\n"); 56 return -EOVERFLOW; 57 } 58 59 return dvb_ringbuffer_write(buf, src, len); 60 } 61 62 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src, 63 int non_blocking, char __user *buf, 64 size_t count, loff_t *ppos) 65 { 66 size_t todo; 67 ssize_t avail; 68 ssize_t ret = 0; 69 70 if (!src->data) 71 return 0; 72 73 if (src->error) { 74 ret = src->error; 75 dvb_ringbuffer_flush(src); 76 return ret; 77 } 78 79 for (todo = count; todo > 0; todo -= ret) { 80 if (non_blocking && dvb_ringbuffer_empty(src)) { 81 ret = -EWOULDBLOCK; 82 break; 83 } 84 85 ret = wait_event_interruptible(src->queue, 86 !dvb_ringbuffer_empty(src) || 87 (src->error != 0)); 88 if (ret < 0) 89 break; 90 91 if (src->error) { 92 ret = src->error; 93 dvb_ringbuffer_flush(src); 94 break; 95 } 96 97 avail = dvb_ringbuffer_avail(src); 98 if (avail > todo) 99 avail = todo; 100 101 ret = dvb_ringbuffer_read_user(src, buf, avail); 102 if (ret < 0) 103 break; 104 105 buf += ret; 106 } 107 108 return (count - todo) ? (count - todo) : ret; 109 } 110 111 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type) 112 { 113 struct list_head *head, *pos; 114 115 head = demux->get_frontends(demux); 116 if (!head) 117 return NULL; 118 list_for_each(pos, head) 119 if (DMX_FE_ENTRY(pos)->source == type) 120 return DMX_FE_ENTRY(pos); 121 122 return NULL; 123 } 124 125 static int dvb_dvr_open(struct inode *inode, struct file *file) 126 { 127 struct dvb_device *dvbdev = file->private_data; 128 struct dmxdev *dmxdev = dvbdev->priv; 129 struct dmx_frontend *front; 130 131 dprintk("%s\n", __func__); 132 133 if (mutex_lock_interruptible(&dmxdev->mutex)) 134 return -ERESTARTSYS; 135 136 if (dmxdev->exit) { 137 mutex_unlock(&dmxdev->mutex); 138 return -ENODEV; 139 } 140 141 if ((file->f_flags & O_ACCMODE) == O_RDWR) { 142 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) { 143 mutex_unlock(&dmxdev->mutex); 144 return -EOPNOTSUPP; 145 } 146 } 147 148 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 149 void *mem; 150 151 if (!dvbdev->readers) { 152 mutex_unlock(&dmxdev->mutex); 153 return -EBUSY; 154 } 155 mem = vmalloc(DVR_BUFFER_SIZE); 156 if (!mem) { 157 mutex_unlock(&dmxdev->mutex); 158 return -ENOMEM; 159 } 160 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE); 161 dvbdev->readers--; 162 } 163 164 if ((file->f_flags & O_ACCMODE) == O_WRONLY) { 165 dmxdev->dvr_orig_fe = dmxdev->demux->frontend; 166 167 if (!dmxdev->demux->write) { 168 mutex_unlock(&dmxdev->mutex); 169 return -EOPNOTSUPP; 170 } 171 172 front = get_fe(dmxdev->demux, DMX_MEMORY_FE); 173 174 if (!front) { 175 mutex_unlock(&dmxdev->mutex); 176 return -EINVAL; 177 } 178 dmxdev->demux->disconnect_frontend(dmxdev->demux); 179 dmxdev->demux->connect_frontend(dmxdev->demux, front); 180 } 181 dvbdev->users++; 182 mutex_unlock(&dmxdev->mutex); 183 return 0; 184 } 185 186 static int dvb_dvr_release(struct inode *inode, struct file *file) 187 { 188 struct dvb_device *dvbdev = file->private_data; 189 struct dmxdev *dmxdev = dvbdev->priv; 190 191 mutex_lock(&dmxdev->mutex); 192 193 if ((file->f_flags & O_ACCMODE) == O_WRONLY) { 194 dmxdev->demux->disconnect_frontend(dmxdev->demux); 195 dmxdev->demux->connect_frontend(dmxdev->demux, 196 dmxdev->dvr_orig_fe); 197 } 198 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 199 dvbdev->readers++; 200 if (dmxdev->dvr_buffer.data) { 201 void *mem = dmxdev->dvr_buffer.data; 202 /*memory barrier*/ 203 mb(); 204 spin_lock_irq(&dmxdev->lock); 205 dmxdev->dvr_buffer.data = NULL; 206 spin_unlock_irq(&dmxdev->lock); 207 vfree(mem); 208 } 209 } 210 /* TODO */ 211 dvbdev->users--; 212 if (dvbdev->users == 1 && dmxdev->exit == 1) { 213 mutex_unlock(&dmxdev->mutex); 214 wake_up(&dvbdev->wait_queue); 215 } else 216 mutex_unlock(&dmxdev->mutex); 217 218 return 0; 219 } 220 221 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf, 222 size_t count, loff_t *ppos) 223 { 224 struct dvb_device *dvbdev = file->private_data; 225 struct dmxdev *dmxdev = dvbdev->priv; 226 int ret; 227 228 if (!dmxdev->demux->write) 229 return -EOPNOTSUPP; 230 if ((file->f_flags & O_ACCMODE) != O_WRONLY) 231 return -EINVAL; 232 if (mutex_lock_interruptible(&dmxdev->mutex)) 233 return -ERESTARTSYS; 234 235 if (dmxdev->exit) { 236 mutex_unlock(&dmxdev->mutex); 237 return -ENODEV; 238 } 239 ret = dmxdev->demux->write(dmxdev->demux, buf, count); 240 mutex_unlock(&dmxdev->mutex); 241 return ret; 242 } 243 244 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count, 245 loff_t *ppos) 246 { 247 struct dvb_device *dvbdev = file->private_data; 248 struct dmxdev *dmxdev = dvbdev->priv; 249 250 if (dmxdev->exit) 251 return -ENODEV; 252 253 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer, 254 file->f_flags & O_NONBLOCK, 255 buf, count, ppos); 256 } 257 258 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev, 259 unsigned long size) 260 { 261 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer; 262 void *newmem; 263 void *oldmem; 264 265 dprintk("%s\n", __func__); 266 267 if (buf->size == size) 268 return 0; 269 if (!size) 270 return -EINVAL; 271 272 newmem = vmalloc(size); 273 if (!newmem) 274 return -ENOMEM; 275 276 oldmem = buf->data; 277 278 spin_lock_irq(&dmxdev->lock); 279 buf->data = newmem; 280 buf->size = size; 281 282 /* reset and not flush in case the buffer shrinks */ 283 dvb_ringbuffer_reset(buf); 284 spin_unlock_irq(&dmxdev->lock); 285 286 vfree(oldmem); 287 288 return 0; 289 } 290 291 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter 292 *dmxdevfilter, int state) 293 { 294 spin_lock_irq(&dmxdevfilter->dev->lock); 295 dmxdevfilter->state = state; 296 spin_unlock_irq(&dmxdevfilter->dev->lock); 297 } 298 299 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter, 300 unsigned long size) 301 { 302 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer; 303 void *newmem; 304 void *oldmem; 305 306 if (buf->size == size) 307 return 0; 308 if (!size) 309 return -EINVAL; 310 if (dmxdevfilter->state >= DMXDEV_STATE_GO) 311 return -EBUSY; 312 313 newmem = vmalloc(size); 314 if (!newmem) 315 return -ENOMEM; 316 317 oldmem = buf->data; 318 319 spin_lock_irq(&dmxdevfilter->dev->lock); 320 buf->data = newmem; 321 buf->size = size; 322 323 /* reset and not flush in case the buffer shrinks */ 324 dvb_ringbuffer_reset(buf); 325 spin_unlock_irq(&dmxdevfilter->dev->lock); 326 327 vfree(oldmem); 328 329 return 0; 330 } 331 332 static void dvb_dmxdev_filter_timeout(unsigned long data) 333 { 334 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data; 335 336 dmxdevfilter->buffer.error = -ETIMEDOUT; 337 spin_lock_irq(&dmxdevfilter->dev->lock); 338 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT; 339 spin_unlock_irq(&dmxdevfilter->dev->lock); 340 wake_up(&dmxdevfilter->buffer.queue); 341 } 342 343 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter) 344 { 345 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec; 346 347 del_timer(&dmxdevfilter->timer); 348 if (para->timeout) { 349 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout; 350 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter; 351 dmxdevfilter->timer.expires = 352 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000; 353 add_timer(&dmxdevfilter->timer); 354 } 355 } 356 357 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len, 358 const u8 *buffer2, size_t buffer2_len, 359 struct dmx_section_filter *filter) 360 { 361 struct dmxdev_filter *dmxdevfilter = filter->priv; 362 int ret; 363 364 if (dmxdevfilter->buffer.error) { 365 wake_up(&dmxdevfilter->buffer.queue); 366 return 0; 367 } 368 spin_lock(&dmxdevfilter->dev->lock); 369 if (dmxdevfilter->state != DMXDEV_STATE_GO) { 370 spin_unlock(&dmxdevfilter->dev->lock); 371 return 0; 372 } 373 del_timer(&dmxdevfilter->timer); 374 dprintk("section callback %*ph\n", 6, buffer1); 375 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1, 376 buffer1_len); 377 if (ret == buffer1_len) { 378 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2, 379 buffer2_len); 380 } 381 if (ret < 0) 382 dmxdevfilter->buffer.error = ret; 383 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT) 384 dmxdevfilter->state = DMXDEV_STATE_DONE; 385 spin_unlock(&dmxdevfilter->dev->lock); 386 wake_up(&dmxdevfilter->buffer.queue); 387 return 0; 388 } 389 390 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len, 391 const u8 *buffer2, size_t buffer2_len, 392 struct dmx_ts_feed *feed) 393 { 394 struct dmxdev_filter *dmxdevfilter = feed->priv; 395 struct dvb_ringbuffer *buffer; 396 int ret; 397 398 spin_lock(&dmxdevfilter->dev->lock); 399 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) { 400 spin_unlock(&dmxdevfilter->dev->lock); 401 return 0; 402 } 403 404 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP 405 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) 406 buffer = &dmxdevfilter->buffer; 407 else 408 buffer = &dmxdevfilter->dev->dvr_buffer; 409 if (buffer->error) { 410 spin_unlock(&dmxdevfilter->dev->lock); 411 wake_up(&buffer->queue); 412 return 0; 413 } 414 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len); 415 if (ret == buffer1_len) 416 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len); 417 if (ret < 0) 418 buffer->error = ret; 419 spin_unlock(&dmxdevfilter->dev->lock); 420 wake_up(&buffer->queue); 421 return 0; 422 } 423 424 /* stop feed but only mark the specified filter as stopped (state set) */ 425 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter) 426 { 427 struct dmxdev_feed *feed; 428 429 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); 430 431 switch (dmxdevfilter->type) { 432 case DMXDEV_TYPE_SEC: 433 del_timer(&dmxdevfilter->timer); 434 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec); 435 break; 436 case DMXDEV_TYPE_PES: 437 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) 438 feed->ts->stop_filtering(feed->ts); 439 break; 440 default: 441 return -EINVAL; 442 } 443 return 0; 444 } 445 446 /* start feed associated with the specified filter */ 447 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter) 448 { 449 struct dmxdev_feed *feed; 450 int ret; 451 452 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); 453 454 switch (filter->type) { 455 case DMXDEV_TYPE_SEC: 456 return filter->feed.sec->start_filtering(filter->feed.sec); 457 case DMXDEV_TYPE_PES: 458 list_for_each_entry(feed, &filter->feed.ts, next) { 459 ret = feed->ts->start_filtering(feed->ts); 460 if (ret < 0) { 461 dvb_dmxdev_feed_stop(filter); 462 return ret; 463 } 464 } 465 break; 466 default: 467 return -EINVAL; 468 } 469 470 return 0; 471 } 472 473 /* restart section feed if it has filters left associated with it, 474 otherwise release the feed */ 475 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter) 476 { 477 int i; 478 struct dmxdev *dmxdev = filter->dev; 479 u16 pid = filter->params.sec.pid; 480 481 for (i = 0; i < dmxdev->filternum; i++) 482 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && 483 dmxdev->filter[i].type == DMXDEV_TYPE_SEC && 484 dmxdev->filter[i].params.sec.pid == pid) { 485 dvb_dmxdev_feed_start(&dmxdev->filter[i]); 486 return 0; 487 } 488 489 filter->dev->demux->release_section_feed(dmxdev->demux, 490 filter->feed.sec); 491 492 return 0; 493 } 494 495 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter) 496 { 497 struct dmxdev_feed *feed; 498 struct dmx_demux *demux; 499 500 if (dmxdevfilter->state < DMXDEV_STATE_GO) 501 return 0; 502 503 switch (dmxdevfilter->type) { 504 case DMXDEV_TYPE_SEC: 505 if (!dmxdevfilter->feed.sec) 506 break; 507 dvb_dmxdev_feed_stop(dmxdevfilter); 508 if (dmxdevfilter->filter.sec) 509 dmxdevfilter->feed.sec-> 510 release_filter(dmxdevfilter->feed.sec, 511 dmxdevfilter->filter.sec); 512 dvb_dmxdev_feed_restart(dmxdevfilter); 513 dmxdevfilter->feed.sec = NULL; 514 break; 515 case DMXDEV_TYPE_PES: 516 dvb_dmxdev_feed_stop(dmxdevfilter); 517 demux = dmxdevfilter->dev->demux; 518 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) { 519 demux->release_ts_feed(demux, feed->ts); 520 feed->ts = NULL; 521 } 522 break; 523 default: 524 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED) 525 return 0; 526 return -EINVAL; 527 } 528 529 dvb_ringbuffer_flush(&dmxdevfilter->buffer); 530 return 0; 531 } 532 533 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter) 534 { 535 struct dmxdev_feed *feed, *tmp; 536 537 /* delete all PIDs */ 538 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) { 539 list_del(&feed->next); 540 kfree(feed); 541 } 542 543 BUG_ON(!list_empty(&dmxdevfilter->feed.ts)); 544 } 545 546 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter) 547 { 548 if (dmxdevfilter->state < DMXDEV_STATE_SET) 549 return 0; 550 551 if (dmxdevfilter->type == DMXDEV_TYPE_PES) 552 dvb_dmxdev_delete_pids(dmxdevfilter); 553 554 dmxdevfilter->type = DMXDEV_TYPE_NONE; 555 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); 556 return 0; 557 } 558 559 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev, 560 struct dmxdev_filter *filter, 561 struct dmxdev_feed *feed) 562 { 563 ktime_t timeout = 0; 564 struct dmx_pes_filter_params *para = &filter->params.pes; 565 enum dmx_output otype; 566 int ret; 567 int ts_type; 568 enum dmx_ts_pes ts_pes; 569 struct dmx_ts_feed *tsfeed; 570 571 feed->ts = NULL; 572 otype = para->output; 573 574 ts_pes = para->pes_type; 575 576 if (ts_pes < DMX_PES_OTHER) 577 ts_type = TS_DECODER; 578 else 579 ts_type = 0; 580 581 if (otype == DMX_OUT_TS_TAP) 582 ts_type |= TS_PACKET; 583 else if (otype == DMX_OUT_TSDEMUX_TAP) 584 ts_type |= TS_PACKET | TS_DEMUX; 585 else if (otype == DMX_OUT_TAP) 586 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY; 587 588 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts, 589 dvb_dmxdev_ts_callback); 590 if (ret < 0) 591 return ret; 592 593 tsfeed = feed->ts; 594 tsfeed->priv = filter; 595 596 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, timeout); 597 if (ret < 0) { 598 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); 599 return ret; 600 } 601 602 ret = tsfeed->start_filtering(tsfeed); 603 if (ret < 0) { 604 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed); 605 return ret; 606 } 607 608 return 0; 609 } 610 611 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter) 612 { 613 struct dmxdev *dmxdev = filter->dev; 614 struct dmxdev_feed *feed; 615 void *mem; 616 int ret, i; 617 618 if (filter->state < DMXDEV_STATE_SET) 619 return -EINVAL; 620 621 if (filter->state >= DMXDEV_STATE_GO) 622 dvb_dmxdev_filter_stop(filter); 623 624 if (!filter->buffer.data) { 625 mem = vmalloc(filter->buffer.size); 626 if (!mem) 627 return -ENOMEM; 628 spin_lock_irq(&filter->dev->lock); 629 filter->buffer.data = mem; 630 spin_unlock_irq(&filter->dev->lock); 631 } 632 633 dvb_ringbuffer_flush(&filter->buffer); 634 635 switch (filter->type) { 636 case DMXDEV_TYPE_SEC: 637 { 638 struct dmx_sct_filter_params *para = &filter->params.sec; 639 struct dmx_section_filter **secfilter = &filter->filter.sec; 640 struct dmx_section_feed **secfeed = &filter->feed.sec; 641 642 *secfilter = NULL; 643 *secfeed = NULL; 644 645 646 /* find active filter/feed with same PID */ 647 for (i = 0; i < dmxdev->filternum; i++) { 648 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO && 649 dmxdev->filter[i].type == DMXDEV_TYPE_SEC && 650 dmxdev->filter[i].params.sec.pid == para->pid) { 651 *secfeed = dmxdev->filter[i].feed.sec; 652 break; 653 } 654 } 655 656 /* if no feed found, try to allocate new one */ 657 if (!*secfeed) { 658 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux, 659 secfeed, 660 dvb_dmxdev_section_callback); 661 if (ret < 0) { 662 pr_err("DVB (%s): could not alloc feed\n", 663 __func__); 664 return ret; 665 } 666 667 ret = (*secfeed)->set(*secfeed, para->pid, 668 (para->flags & DMX_CHECK_CRC) ? 1 : 0); 669 if (ret < 0) { 670 pr_err("DVB (%s): could not set feed\n", 671 __func__); 672 dvb_dmxdev_feed_restart(filter); 673 return ret; 674 } 675 } else { 676 dvb_dmxdev_feed_stop(filter); 677 } 678 679 ret = (*secfeed)->allocate_filter(*secfeed, secfilter); 680 if (ret < 0) { 681 dvb_dmxdev_feed_restart(filter); 682 filter->feed.sec->start_filtering(*secfeed); 683 dprintk("could not get filter\n"); 684 return ret; 685 } 686 687 (*secfilter)->priv = filter; 688 689 memcpy(&((*secfilter)->filter_value[3]), 690 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1); 691 memcpy(&(*secfilter)->filter_mask[3], 692 ¶->filter.mask[1], DMX_FILTER_SIZE - 1); 693 memcpy(&(*secfilter)->filter_mode[3], 694 ¶->filter.mode[1], DMX_FILTER_SIZE - 1); 695 696 (*secfilter)->filter_value[0] = para->filter.filter[0]; 697 (*secfilter)->filter_mask[0] = para->filter.mask[0]; 698 (*secfilter)->filter_mode[0] = para->filter.mode[0]; 699 (*secfilter)->filter_mask[1] = 0; 700 (*secfilter)->filter_mask[2] = 0; 701 702 filter->todo = 0; 703 704 ret = filter->feed.sec->start_filtering(filter->feed.sec); 705 if (ret < 0) 706 return ret; 707 708 dvb_dmxdev_filter_timer(filter); 709 break; 710 } 711 case DMXDEV_TYPE_PES: 712 list_for_each_entry(feed, &filter->feed.ts, next) { 713 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed); 714 if (ret < 0) { 715 dvb_dmxdev_filter_stop(filter); 716 return ret; 717 } 718 } 719 break; 720 default: 721 return -EINVAL; 722 } 723 724 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO); 725 return 0; 726 } 727 728 static int dvb_demux_open(struct inode *inode, struct file *file) 729 { 730 struct dvb_device *dvbdev = file->private_data; 731 struct dmxdev *dmxdev = dvbdev->priv; 732 int i; 733 struct dmxdev_filter *dmxdevfilter; 734 735 if (!dmxdev->filter) 736 return -EINVAL; 737 738 if (mutex_lock_interruptible(&dmxdev->mutex)) 739 return -ERESTARTSYS; 740 741 for (i = 0; i < dmxdev->filternum; i++) 742 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE) 743 break; 744 745 if (i == dmxdev->filternum) { 746 mutex_unlock(&dmxdev->mutex); 747 return -EMFILE; 748 } 749 750 dmxdevfilter = &dmxdev->filter[i]; 751 mutex_init(&dmxdevfilter->mutex); 752 file->private_data = dmxdevfilter; 753 754 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192); 755 dmxdevfilter->type = DMXDEV_TYPE_NONE; 756 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED); 757 init_timer(&dmxdevfilter->timer); 758 759 dvbdev->users++; 760 761 mutex_unlock(&dmxdev->mutex); 762 return 0; 763 } 764 765 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev, 766 struct dmxdev_filter *dmxdevfilter) 767 { 768 mutex_lock(&dmxdev->mutex); 769 mutex_lock(&dmxdevfilter->mutex); 770 771 dvb_dmxdev_filter_stop(dmxdevfilter); 772 dvb_dmxdev_filter_reset(dmxdevfilter); 773 774 if (dmxdevfilter->buffer.data) { 775 void *mem = dmxdevfilter->buffer.data; 776 777 spin_lock_irq(&dmxdev->lock); 778 dmxdevfilter->buffer.data = NULL; 779 spin_unlock_irq(&dmxdev->lock); 780 vfree(mem); 781 } 782 783 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE); 784 wake_up(&dmxdevfilter->buffer.queue); 785 mutex_unlock(&dmxdevfilter->mutex); 786 mutex_unlock(&dmxdev->mutex); 787 return 0; 788 } 789 790 static inline void invert_mode(struct dmx_filter *filter) 791 { 792 int i; 793 794 for (i = 0; i < DMX_FILTER_SIZE; i++) 795 filter->mode[i] ^= 0xff; 796 } 797 798 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev, 799 struct dmxdev_filter *filter, u16 pid) 800 { 801 struct dmxdev_feed *feed; 802 803 if ((filter->type != DMXDEV_TYPE_PES) || 804 (filter->state < DMXDEV_STATE_SET)) 805 return -EINVAL; 806 807 /* only TS packet filters may have multiple PIDs */ 808 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) && 809 (!list_empty(&filter->feed.ts))) 810 return -EINVAL; 811 812 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL); 813 if (feed == NULL) 814 return -ENOMEM; 815 816 feed->pid = pid; 817 list_add(&feed->next, &filter->feed.ts); 818 819 if (filter->state >= DMXDEV_STATE_GO) 820 return dvb_dmxdev_start_feed(dmxdev, filter, feed); 821 822 return 0; 823 } 824 825 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev, 826 struct dmxdev_filter *filter, u16 pid) 827 { 828 struct dmxdev_feed *feed, *tmp; 829 830 if ((filter->type != DMXDEV_TYPE_PES) || 831 (filter->state < DMXDEV_STATE_SET)) 832 return -EINVAL; 833 834 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) { 835 if ((feed->pid == pid) && (feed->ts != NULL)) { 836 feed->ts->stop_filtering(feed->ts); 837 filter->dev->demux->release_ts_feed(filter->dev->demux, 838 feed->ts); 839 list_del(&feed->next); 840 kfree(feed); 841 } 842 } 843 844 return 0; 845 } 846 847 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev, 848 struct dmxdev_filter *dmxdevfilter, 849 struct dmx_sct_filter_params *params) 850 { 851 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n", 852 __func__, params->pid, params->flags, params->timeout); 853 854 dvb_dmxdev_filter_stop(dmxdevfilter); 855 856 dmxdevfilter->type = DMXDEV_TYPE_SEC; 857 memcpy(&dmxdevfilter->params.sec, 858 params, sizeof(struct dmx_sct_filter_params)); 859 invert_mode(&dmxdevfilter->params.sec.filter); 860 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); 861 862 if (params->flags & DMX_IMMEDIATE_START) 863 return dvb_dmxdev_filter_start(dmxdevfilter); 864 865 return 0; 866 } 867 868 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev, 869 struct dmxdev_filter *dmxdevfilter, 870 struct dmx_pes_filter_params *params) 871 { 872 int ret; 873 874 dvb_dmxdev_filter_stop(dmxdevfilter); 875 dvb_dmxdev_filter_reset(dmxdevfilter); 876 877 if ((unsigned int)params->pes_type > DMX_PES_OTHER) 878 return -EINVAL; 879 880 dmxdevfilter->type = DMXDEV_TYPE_PES; 881 memcpy(&dmxdevfilter->params, params, 882 sizeof(struct dmx_pes_filter_params)); 883 INIT_LIST_HEAD(&dmxdevfilter->feed.ts); 884 885 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET); 886 887 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, 888 dmxdevfilter->params.pes.pid); 889 if (ret < 0) 890 return ret; 891 892 if (params->flags & DMX_IMMEDIATE_START) 893 return dvb_dmxdev_filter_start(dmxdevfilter); 894 895 return 0; 896 } 897 898 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil, 899 struct file *file, char __user *buf, 900 size_t count, loff_t *ppos) 901 { 902 int result, hcount; 903 int done = 0; 904 905 if (dfil->todo <= 0) { 906 hcount = 3 + dfil->todo; 907 if (hcount > count) 908 hcount = count; 909 result = dvb_dmxdev_buffer_read(&dfil->buffer, 910 file->f_flags & O_NONBLOCK, 911 buf, hcount, ppos); 912 if (result < 0) { 913 dfil->todo = 0; 914 return result; 915 } 916 if (copy_from_user(dfil->secheader - dfil->todo, buf, result)) 917 return -EFAULT; 918 buf += result; 919 done = result; 920 count -= result; 921 dfil->todo -= result; 922 if (dfil->todo > -3) 923 return done; 924 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff; 925 if (!count) 926 return done; 927 } 928 if (count > dfil->todo) 929 count = dfil->todo; 930 result = dvb_dmxdev_buffer_read(&dfil->buffer, 931 file->f_flags & O_NONBLOCK, 932 buf, count, ppos); 933 if (result < 0) 934 return result; 935 dfil->todo -= result; 936 return (result + done); 937 } 938 939 static ssize_t 940 dvb_demux_read(struct file *file, char __user *buf, size_t count, 941 loff_t *ppos) 942 { 943 struct dmxdev_filter *dmxdevfilter = file->private_data; 944 int ret; 945 946 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) 947 return -ERESTARTSYS; 948 949 if (dmxdevfilter->type == DMXDEV_TYPE_SEC) 950 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos); 951 else 952 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer, 953 file->f_flags & O_NONBLOCK, 954 buf, count, ppos); 955 956 mutex_unlock(&dmxdevfilter->mutex); 957 return ret; 958 } 959 960 static int dvb_demux_do_ioctl(struct file *file, 961 unsigned int cmd, void *parg) 962 { 963 struct dmxdev_filter *dmxdevfilter = file->private_data; 964 struct dmxdev *dmxdev = dmxdevfilter->dev; 965 unsigned long arg = (unsigned long)parg; 966 int ret = 0; 967 968 if (mutex_lock_interruptible(&dmxdev->mutex)) 969 return -ERESTARTSYS; 970 971 switch (cmd) { 972 case DMX_START: 973 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 974 mutex_unlock(&dmxdev->mutex); 975 return -ERESTARTSYS; 976 } 977 if (dmxdevfilter->state < DMXDEV_STATE_SET) 978 ret = -EINVAL; 979 else 980 ret = dvb_dmxdev_filter_start(dmxdevfilter); 981 mutex_unlock(&dmxdevfilter->mutex); 982 break; 983 984 case DMX_STOP: 985 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 986 mutex_unlock(&dmxdev->mutex); 987 return -ERESTARTSYS; 988 } 989 ret = dvb_dmxdev_filter_stop(dmxdevfilter); 990 mutex_unlock(&dmxdevfilter->mutex); 991 break; 992 993 case DMX_SET_FILTER: 994 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 995 mutex_unlock(&dmxdev->mutex); 996 return -ERESTARTSYS; 997 } 998 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg); 999 mutex_unlock(&dmxdevfilter->mutex); 1000 break; 1001 1002 case DMX_SET_PES_FILTER: 1003 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 1004 mutex_unlock(&dmxdev->mutex); 1005 return -ERESTARTSYS; 1006 } 1007 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg); 1008 mutex_unlock(&dmxdevfilter->mutex); 1009 break; 1010 1011 case DMX_SET_BUFFER_SIZE: 1012 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 1013 mutex_unlock(&dmxdev->mutex); 1014 return -ERESTARTSYS; 1015 } 1016 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg); 1017 mutex_unlock(&dmxdevfilter->mutex); 1018 break; 1019 1020 case DMX_GET_PES_PIDS: 1021 if (!dmxdev->demux->get_pes_pids) { 1022 ret = -EINVAL; 1023 break; 1024 } 1025 dmxdev->demux->get_pes_pids(dmxdev->demux, parg); 1026 break; 1027 1028 case DMX_GET_STC: 1029 if (!dmxdev->demux->get_stc) { 1030 ret = -EINVAL; 1031 break; 1032 } 1033 ret = dmxdev->demux->get_stc(dmxdev->demux, 1034 ((struct dmx_stc *)parg)->num, 1035 &((struct dmx_stc *)parg)->stc, 1036 &((struct dmx_stc *)parg)->base); 1037 break; 1038 1039 case DMX_ADD_PID: 1040 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 1041 ret = -ERESTARTSYS; 1042 break; 1043 } 1044 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg); 1045 mutex_unlock(&dmxdevfilter->mutex); 1046 break; 1047 1048 case DMX_REMOVE_PID: 1049 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) { 1050 ret = -ERESTARTSYS; 1051 break; 1052 } 1053 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg); 1054 mutex_unlock(&dmxdevfilter->mutex); 1055 break; 1056 1057 default: 1058 ret = -EINVAL; 1059 break; 1060 } 1061 mutex_unlock(&dmxdev->mutex); 1062 return ret; 1063 } 1064 1065 static long dvb_demux_ioctl(struct file *file, unsigned int cmd, 1066 unsigned long arg) 1067 { 1068 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl); 1069 } 1070 1071 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait) 1072 { 1073 struct dmxdev_filter *dmxdevfilter = file->private_data; 1074 unsigned int mask = 0; 1075 1076 if ((!dmxdevfilter) || dmxdevfilter->dev->exit) 1077 return POLLERR; 1078 1079 poll_wait(file, &dmxdevfilter->buffer.queue, wait); 1080 1081 if (dmxdevfilter->state != DMXDEV_STATE_GO && 1082 dmxdevfilter->state != DMXDEV_STATE_DONE && 1083 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT) 1084 return 0; 1085 1086 if (dmxdevfilter->buffer.error) 1087 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); 1088 1089 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer)) 1090 mask |= (POLLIN | POLLRDNORM | POLLPRI); 1091 1092 return mask; 1093 } 1094 1095 static int dvb_demux_release(struct inode *inode, struct file *file) 1096 { 1097 struct dmxdev_filter *dmxdevfilter = file->private_data; 1098 struct dmxdev *dmxdev = dmxdevfilter->dev; 1099 1100 int ret; 1101 1102 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter); 1103 1104 mutex_lock(&dmxdev->mutex); 1105 dmxdev->dvbdev->users--; 1106 if (dmxdev->dvbdev->users == 1 && dmxdev->exit == 1) { 1107 mutex_unlock(&dmxdev->mutex); 1108 wake_up(&dmxdev->dvbdev->wait_queue); 1109 } else 1110 mutex_unlock(&dmxdev->mutex); 1111 1112 return ret; 1113 } 1114 1115 static const struct file_operations dvb_demux_fops = { 1116 .owner = THIS_MODULE, 1117 .read = dvb_demux_read, 1118 .unlocked_ioctl = dvb_demux_ioctl, 1119 .open = dvb_demux_open, 1120 .release = dvb_demux_release, 1121 .poll = dvb_demux_poll, 1122 .llseek = default_llseek, 1123 }; 1124 1125 static const struct dvb_device dvbdev_demux = { 1126 .priv = NULL, 1127 .users = 1, 1128 .writers = 1, 1129 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 1130 .name = "dvb-demux", 1131 #endif 1132 .fops = &dvb_demux_fops 1133 }; 1134 1135 static int dvb_dvr_do_ioctl(struct file *file, 1136 unsigned int cmd, void *parg) 1137 { 1138 struct dvb_device *dvbdev = file->private_data; 1139 struct dmxdev *dmxdev = dvbdev->priv; 1140 unsigned long arg = (unsigned long)parg; 1141 int ret; 1142 1143 if (mutex_lock_interruptible(&dmxdev->mutex)) 1144 return -ERESTARTSYS; 1145 1146 switch (cmd) { 1147 case DMX_SET_BUFFER_SIZE: 1148 ret = dvb_dvr_set_buffer_size(dmxdev, arg); 1149 break; 1150 1151 default: 1152 ret = -EINVAL; 1153 break; 1154 } 1155 mutex_unlock(&dmxdev->mutex); 1156 return ret; 1157 } 1158 1159 static long dvb_dvr_ioctl(struct file *file, 1160 unsigned int cmd, unsigned long arg) 1161 { 1162 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl); 1163 } 1164 1165 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait) 1166 { 1167 struct dvb_device *dvbdev = file->private_data; 1168 struct dmxdev *dmxdev = dvbdev->priv; 1169 unsigned int mask = 0; 1170 1171 dprintk("%s\n", __func__); 1172 1173 if (dmxdev->exit) 1174 return POLLERR; 1175 1176 poll_wait(file, &dmxdev->dvr_buffer.queue, wait); 1177 1178 if ((file->f_flags & O_ACCMODE) == O_RDONLY) { 1179 if (dmxdev->dvr_buffer.error) 1180 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR); 1181 1182 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer)) 1183 mask |= (POLLIN | POLLRDNORM | POLLPRI); 1184 } else 1185 mask |= (POLLOUT | POLLWRNORM | POLLPRI); 1186 1187 return mask; 1188 } 1189 1190 static const struct file_operations dvb_dvr_fops = { 1191 .owner = THIS_MODULE, 1192 .read = dvb_dvr_read, 1193 .write = dvb_dvr_write, 1194 .unlocked_ioctl = dvb_dvr_ioctl, 1195 .open = dvb_dvr_open, 1196 .release = dvb_dvr_release, 1197 .poll = dvb_dvr_poll, 1198 .llseek = default_llseek, 1199 }; 1200 1201 static const struct dvb_device dvbdev_dvr = { 1202 .priv = NULL, 1203 .readers = 1, 1204 .users = 1, 1205 #if defined(CONFIG_MEDIA_CONTROLLER_DVB) 1206 .name = "dvb-dvr", 1207 #endif 1208 .fops = &dvb_dvr_fops 1209 }; 1210 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter) 1211 { 1212 int i; 1213 1214 if (dmxdev->demux->open(dmxdev->demux) < 0) 1215 return -EUSERS; 1216 1217 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter)); 1218 if (!dmxdev->filter) 1219 return -ENOMEM; 1220 1221 mutex_init(&dmxdev->mutex); 1222 spin_lock_init(&dmxdev->lock); 1223 for (i = 0; i < dmxdev->filternum; i++) { 1224 dmxdev->filter[i].dev = dmxdev; 1225 dmxdev->filter[i].buffer.data = NULL; 1226 dvb_dmxdev_filter_state_set(&dmxdev->filter[i], 1227 DMXDEV_STATE_FREE); 1228 } 1229 1230 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, 1231 DVB_DEVICE_DEMUX, dmxdev->filternum); 1232 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, 1233 dmxdev, DVB_DEVICE_DVR, dmxdev->filternum); 1234 1235 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192); 1236 1237 return 0; 1238 } 1239 1240 EXPORT_SYMBOL(dvb_dmxdev_init); 1241 1242 void dvb_dmxdev_release(struct dmxdev *dmxdev) 1243 { 1244 dmxdev->exit = 1; 1245 if (dmxdev->dvbdev->users > 1) { 1246 wait_event(dmxdev->dvbdev->wait_queue, 1247 dmxdev->dvbdev->users == 1); 1248 } 1249 if (dmxdev->dvr_dvbdev->users > 1) { 1250 wait_event(dmxdev->dvr_dvbdev->wait_queue, 1251 dmxdev->dvr_dvbdev->users == 1); 1252 } 1253 1254 dvb_unregister_device(dmxdev->dvbdev); 1255 dvb_unregister_device(dmxdev->dvr_dvbdev); 1256 1257 vfree(dmxdev->filter); 1258 dmxdev->filter = NULL; 1259 dmxdev->demux->close(dmxdev->demux); 1260 } 1261 1262 EXPORT_SYMBOL(dvb_dmxdev_release); 1263