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