root/drivers/media/dvb-core/dmxdev.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dvb_dmxdev_buffer_write
  2. dvb_dmxdev_buffer_read
  3. get_fe
  4. dvb_dvr_open
  5. dvb_dvr_release
  6. dvb_dvr_write
  7. dvb_dvr_read
  8. dvb_dvr_set_buffer_size
  9. dvb_dmxdev_filter_state_set
  10. dvb_dmxdev_set_buffer_size
  11. dvb_dmxdev_filter_timeout
  12. dvb_dmxdev_filter_timer
  13. dvb_dmxdev_section_callback
  14. dvb_dmxdev_ts_callback
  15. dvb_dmxdev_feed_stop
  16. dvb_dmxdev_feed_start
  17. dvb_dmxdev_feed_restart
  18. dvb_dmxdev_filter_stop
  19. dvb_dmxdev_delete_pids
  20. dvb_dmxdev_filter_reset
  21. dvb_dmxdev_start_feed
  22. dvb_dmxdev_filter_start
  23. dvb_demux_open
  24. dvb_dmxdev_filter_free
  25. invert_mode
  26. dvb_dmxdev_add_pid
  27. dvb_dmxdev_remove_pid
  28. dvb_dmxdev_filter_set
  29. dvb_dmxdev_pes_filter_set
  30. dvb_dmxdev_read_sec
  31. dvb_demux_read
  32. dvb_demux_do_ioctl
  33. dvb_demux_ioctl
  34. dvb_demux_poll
  35. dvb_demux_mmap
  36. dvb_demux_release
  37. dvb_dvr_do_ioctl
  38. dvb_dvr_ioctl
  39. dvb_dvr_poll
  40. dvb_dvr_mmap
  41. dvb_dmxdev_init
  42. dvb_dmxdev_release

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

/* [<][>][^][v][top][bottom][index][help] */