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