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