2 * dmxdev.c - DVB demultiplexer device
4 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
5 * for convergence integrated media GmbH
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.
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.
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.
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
37 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
40 #define dprintk if (debug) printk
42 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
43 const u8 *src, size_t len)
52 free = dvb_ringbuffer_free(buf);
54 dprintk("dmxdev: buffer overflow\n");
58 return dvb_ringbuffer_write(buf, src, len);
61 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
62 int non_blocking, char __user *buf,
63 size_t count, loff_t *ppos)
74 dvb_ringbuffer_flush(src);
78 for (todo = count; todo > 0; todo -= ret) {
79 if (non_blocking && dvb_ringbuffer_empty(src)) {
84 ret = wait_event_interruptible(src->queue,
85 !dvb_ringbuffer_empty(src) ||
92 dvb_ringbuffer_flush(src);
96 avail = dvb_ringbuffer_avail(src);
100 ret = dvb_ringbuffer_read(src, buf, avail, 1);
107 return (count - todo) ? (count - todo) : ret;
110 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
112 struct list_head *head, *pos;
114 head = demux->get_frontends(demux);
117 list_for_each(pos, head)
118 if (DMX_FE_ENTRY(pos)->source == type)
119 return DMX_FE_ENTRY(pos);
124 static int dvb_dvr_open(struct inode *inode, struct file *file)
126 struct dvb_device *dvbdev = file->private_data;
127 struct dmxdev *dmxdev = dvbdev->priv;
128 struct dmx_frontend *front;
130 dprintk("function : %s\n", __FUNCTION__);
132 if (mutex_lock_interruptible(&dmxdev->mutex))
136 mutex_unlock(&dmxdev->mutex);
140 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
141 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
142 mutex_unlock(&dmxdev->mutex);
147 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
149 if (!dvbdev->readers) {
150 mutex_unlock(&dmxdev->mutex);
153 mem = vmalloc(DVR_BUFFER_SIZE);
155 mutex_unlock(&dmxdev->mutex);
158 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
162 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
163 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
165 if (!dmxdev->demux->write) {
166 mutex_unlock(&dmxdev->mutex);
170 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
173 mutex_unlock(&dmxdev->mutex);
176 dmxdev->demux->disconnect_frontend(dmxdev->demux);
177 dmxdev->demux->connect_frontend(dmxdev->demux, front);
180 mutex_unlock(&dmxdev->mutex);
184 static int dvb_dvr_release(struct inode *inode, struct file *file)
186 struct dvb_device *dvbdev = file->private_data;
187 struct dmxdev *dmxdev = dvbdev->priv;
189 mutex_lock(&dmxdev->mutex);
191 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
192 dmxdev->demux->disconnect_frontend(dmxdev->demux);
193 dmxdev->demux->connect_frontend(dmxdev->demux,
194 dmxdev->dvr_orig_fe);
196 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
198 if (dmxdev->dvr_buffer.data) {
199 void *mem = dmxdev->dvr_buffer.data;
201 spin_lock_irq(&dmxdev->lock);
202 dmxdev->dvr_buffer.data = NULL;
203 spin_unlock_irq(&dmxdev->lock);
209 if(dvbdev->users==-1 && dmxdev->exit==1) {
210 fops_put(file->f_op);
212 mutex_unlock(&dmxdev->mutex);
213 wake_up(&dvbdev->wait_queue);
215 mutex_unlock(&dmxdev->mutex);
220 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
221 size_t count, loff_t *ppos)
223 struct dvb_device *dvbdev = file->private_data;
224 struct dmxdev *dmxdev = dvbdev->priv;
227 if (!dmxdev->demux->write)
229 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
231 if (mutex_lock_interruptible(&dmxdev->mutex))
235 mutex_unlock(&dmxdev->mutex);
238 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
239 mutex_unlock(&dmxdev->mutex);
243 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
246 struct dvb_device *dvbdev = file->private_data;
247 struct dmxdev *dmxdev = dvbdev->priv;
251 mutex_unlock(&dmxdev->mutex);
255 //mutex_lock(&dmxdev->mutex);
256 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
257 file->f_flags & O_NONBLOCK,
259 //mutex_unlock(&dmxdev->mutex);
263 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
264 *dmxdevfilter, int state)
266 spin_lock_irq(&dmxdevfilter->dev->lock);
267 dmxdevfilter->state = state;
268 spin_unlock_irq(&dmxdevfilter->dev->lock);
271 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
274 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
277 if (buf->size == size)
279 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
281 spin_lock_irq(&dmxdevfilter->dev->lock);
285 dvb_ringbuffer_flush(buf);
286 spin_unlock_irq(&dmxdevfilter->dev->lock);
290 mem = vmalloc(dmxdevfilter->buffer.size);
293 spin_lock_irq(&dmxdevfilter->dev->lock);
295 spin_unlock_irq(&dmxdevfilter->dev->lock);
300 static void dvb_dmxdev_filter_timeout(unsigned long data)
302 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
304 dmxdevfilter->buffer.error = -ETIMEDOUT;
305 spin_lock_irq(&dmxdevfilter->dev->lock);
306 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
307 spin_unlock_irq(&dmxdevfilter->dev->lock);
308 wake_up(&dmxdevfilter->buffer.queue);
311 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
313 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
315 del_timer(&dmxdevfilter->timer);
317 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
318 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
319 dmxdevfilter->timer.expires =
320 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
321 add_timer(&dmxdevfilter->timer);
325 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
326 const u8 *buffer2, size_t buffer2_len,
327 struct dmx_section_filter *filter,
328 enum dmx_success success)
330 struct dmxdev_filter *dmxdevfilter = filter->priv;
333 if (dmxdevfilter->buffer.error) {
334 wake_up(&dmxdevfilter->buffer.queue);
337 spin_lock(&dmxdevfilter->dev->lock);
338 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
339 spin_unlock(&dmxdevfilter->dev->lock);
342 del_timer(&dmxdevfilter->timer);
343 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
344 buffer1[0], buffer1[1],
345 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
346 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
348 if (ret == buffer1_len) {
349 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
353 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
354 dmxdevfilter->buffer.error = ret;
356 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
357 dmxdevfilter->state = DMXDEV_STATE_DONE;
358 spin_unlock(&dmxdevfilter->dev->lock);
359 wake_up(&dmxdevfilter->buffer.queue);
363 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
364 const u8 *buffer2, size_t buffer2_len,
365 struct dmx_ts_feed *feed,
366 enum dmx_success success)
368 struct dmxdev_filter *dmxdevfilter = feed->priv;
369 struct dvb_ringbuffer *buffer;
372 spin_lock(&dmxdevfilter->dev->lock);
373 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
374 spin_unlock(&dmxdevfilter->dev->lock);
378 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
379 buffer = &dmxdevfilter->buffer;
381 buffer = &dmxdevfilter->dev->dvr_buffer;
383 spin_unlock(&dmxdevfilter->dev->lock);
384 wake_up(&buffer->queue);
387 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
388 if (ret == buffer1_len)
389 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
391 dvb_ringbuffer_flush(buffer);
394 spin_unlock(&dmxdevfilter->dev->lock);
395 wake_up(&buffer->queue);
399 /* stop feed but only mark the specified filter as stopped (state set) */
400 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
402 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
404 switch (dmxdevfilter->type) {
405 case DMXDEV_TYPE_SEC:
406 del_timer(&dmxdevfilter->timer);
407 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
409 case DMXDEV_TYPE_PES:
410 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
418 /* start feed associated with the specified filter */
419 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
421 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
423 switch (filter->type) {
424 case DMXDEV_TYPE_SEC:
425 return filter->feed.sec->start_filtering(filter->feed.sec);
426 case DMXDEV_TYPE_PES:
427 return filter->feed.ts->start_filtering(filter->feed.ts);
435 /* restart section feed if it has filters left associated with it,
436 otherwise release the feed */
437 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
440 struct dmxdev *dmxdev = filter->dev;
441 u16 pid = filter->params.sec.pid;
443 for (i = 0; i < dmxdev->filternum; i++)
444 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
445 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
446 dmxdev->filter[i].params.sec.pid == pid) {
447 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
451 filter->dev->demux->release_section_feed(dmxdev->demux,
457 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
459 if (dmxdevfilter->state < DMXDEV_STATE_GO)
462 switch (dmxdevfilter->type) {
463 case DMXDEV_TYPE_SEC:
464 if (!dmxdevfilter->feed.sec)
466 dvb_dmxdev_feed_stop(dmxdevfilter);
467 if (dmxdevfilter->filter.sec)
468 dmxdevfilter->feed.sec->
469 release_filter(dmxdevfilter->feed.sec,
470 dmxdevfilter->filter.sec);
471 dvb_dmxdev_feed_restart(dmxdevfilter);
472 dmxdevfilter->feed.sec = NULL;
474 case DMXDEV_TYPE_PES:
475 if (!dmxdevfilter->feed.ts)
477 dvb_dmxdev_feed_stop(dmxdevfilter);
478 dmxdevfilter->dev->demux->
479 release_ts_feed(dmxdevfilter->dev->demux,
480 dmxdevfilter->feed.ts);
481 dmxdevfilter->feed.ts = NULL;
484 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
489 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
493 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
495 if (dmxdevfilter->state < DMXDEV_STATE_SET)
498 dmxdevfilter->type = DMXDEV_TYPE_NONE;
499 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
503 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
505 struct dmxdev *dmxdev = filter->dev;
509 if (filter->state < DMXDEV_STATE_SET)
512 if (filter->state >= DMXDEV_STATE_GO)
513 dvb_dmxdev_filter_stop(filter);
515 if (!filter->buffer.data) {
516 mem = vmalloc(filter->buffer.size);
519 spin_lock_irq(&filter->dev->lock);
520 filter->buffer.data = mem;
521 spin_unlock_irq(&filter->dev->lock);
524 dvb_ringbuffer_flush(&filter->buffer);
526 switch (filter->type) {
527 case DMXDEV_TYPE_SEC:
529 struct dmx_sct_filter_params *para = &filter->params.sec;
530 struct dmx_section_filter **secfilter = &filter->filter.sec;
531 struct dmx_section_feed **secfeed = &filter->feed.sec;
537 /* find active filter/feed with same PID */
538 for (i = 0; i < dmxdev->filternum; i++) {
539 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
540 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
541 dmxdev->filter[i].params.sec.pid == para->pid) {
542 *secfeed = dmxdev->filter[i].feed.sec;
547 /* if no feed found, try to allocate new one */
549 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
551 dvb_dmxdev_section_callback);
553 printk("DVB (%s): could not alloc feed\n",
558 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
559 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
561 printk("DVB (%s): could not set feed\n",
563 dvb_dmxdev_feed_restart(filter);
567 dvb_dmxdev_feed_stop(filter);
570 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
572 dvb_dmxdev_feed_restart(filter);
573 filter->feed.sec->start_filtering(*secfeed);
574 dprintk("could not get filter\n");
578 (*secfilter)->priv = filter;
580 memcpy(&((*secfilter)->filter_value[3]),
581 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
582 memcpy(&(*secfilter)->filter_mask[3],
583 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
584 memcpy(&(*secfilter)->filter_mode[3],
585 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
587 (*secfilter)->filter_value[0] = para->filter.filter[0];
588 (*secfilter)->filter_mask[0] = para->filter.mask[0];
589 (*secfilter)->filter_mode[0] = para->filter.mode[0];
590 (*secfilter)->filter_mask[1] = 0;
591 (*secfilter)->filter_mask[2] = 0;
595 ret = filter->feed.sec->start_filtering(filter->feed.sec);
599 dvb_dmxdev_filter_timer(filter);
602 case DMXDEV_TYPE_PES:
604 struct timespec timeout = { 0 };
605 struct dmx_pes_filter_params *para = &filter->params.pes;
609 enum dmx_ts_pes ts_pes;
610 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
612 filter->feed.ts = NULL;
613 otype = para->output;
615 ts_pes = (enum dmx_ts_pes)para->pes_type;
617 if (ts_pes < DMX_PES_OTHER)
618 ts_type = TS_DECODER;
622 if (otype == DMX_OUT_TS_TAP)
623 ts_type |= TS_PACKET;
625 if (otype == DMX_OUT_TAP)
626 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
628 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
630 dvb_dmxdev_ts_callback);
634 (*tsfeed)->priv = filter;
636 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
639 dmxdev->demux->release_ts_feed(dmxdev->demux,
644 ret = filter->feed.ts->start_filtering(filter->feed.ts);
646 dmxdev->demux->release_ts_feed(dmxdev->demux,
657 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
661 static int dvb_demux_open(struct inode *inode, struct file *file)
663 struct dvb_device *dvbdev = file->private_data;
664 struct dmxdev *dmxdev = dvbdev->priv;
666 struct dmxdev_filter *dmxdevfilter;
671 if (mutex_lock_interruptible(&dmxdev->mutex))
674 for (i = 0; i < dmxdev->filternum; i++)
675 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
678 if (i == dmxdev->filternum) {
679 mutex_unlock(&dmxdev->mutex);
683 dmxdevfilter = &dmxdev->filter[i];
684 mutex_init(&dmxdevfilter->mutex);
685 file->private_data = dmxdevfilter;
687 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
688 dmxdevfilter->type = DMXDEV_TYPE_NONE;
689 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
690 dmxdevfilter->feed.ts = NULL;
691 init_timer(&dmxdevfilter->timer);
695 mutex_unlock(&dmxdev->mutex);
699 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
700 struct dmxdev_filter *dmxdevfilter)
702 mutex_lock(&dmxdev->mutex);
703 mutex_lock(&dmxdevfilter->mutex);
705 dvb_dmxdev_filter_stop(dmxdevfilter);
706 dvb_dmxdev_filter_reset(dmxdevfilter);
708 if (dmxdevfilter->buffer.data) {
709 void *mem = dmxdevfilter->buffer.data;
711 spin_lock_irq(&dmxdev->lock);
712 dmxdevfilter->buffer.data = NULL;
713 spin_unlock_irq(&dmxdev->lock);
717 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
718 wake_up(&dmxdevfilter->buffer.queue);
719 mutex_unlock(&dmxdevfilter->mutex);
720 mutex_unlock(&dmxdev->mutex);
724 static inline void invert_mode(dmx_filter_t *filter)
728 for (i = 0; i < DMX_FILTER_SIZE; i++)
729 filter->mode[i] ^= 0xff;
732 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
733 struct dmxdev_filter *dmxdevfilter,
734 struct dmx_sct_filter_params *params)
736 dprintk("function : %s\n", __FUNCTION__);
738 dvb_dmxdev_filter_stop(dmxdevfilter);
740 dmxdevfilter->type = DMXDEV_TYPE_SEC;
741 memcpy(&dmxdevfilter->params.sec,
742 params, sizeof(struct dmx_sct_filter_params));
743 invert_mode(&dmxdevfilter->params.sec.filter);
744 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
746 if (params->flags & DMX_IMMEDIATE_START)
747 return dvb_dmxdev_filter_start(dmxdevfilter);
752 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
753 struct dmxdev_filter *dmxdevfilter,
754 struct dmx_pes_filter_params *params)
756 dvb_dmxdev_filter_stop(dmxdevfilter);
758 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
761 dmxdevfilter->type = DMXDEV_TYPE_PES;
762 memcpy(&dmxdevfilter->params, params,
763 sizeof(struct dmx_pes_filter_params));
765 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
767 if (params->flags & DMX_IMMEDIATE_START)
768 return dvb_dmxdev_filter_start(dmxdevfilter);
773 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
774 struct file *file, char __user *buf,
775 size_t count, loff_t *ppos)
780 if (dfil->todo <= 0) {
781 hcount = 3 + dfil->todo;
784 result = dvb_dmxdev_buffer_read(&dfil->buffer,
785 file->f_flags & O_NONBLOCK,
791 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
796 dfil->todo -= result;
799 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
803 if (count > dfil->todo)
805 result = dvb_dmxdev_buffer_read(&dfil->buffer,
806 file->f_flags & O_NONBLOCK,
810 dfil->todo -= result;
811 return (result + done);
815 dvb_demux_read(struct file *file, char __user *buf, size_t count,
818 struct dmxdev_filter *dmxdevfilter = file->private_data;
821 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
824 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
825 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
827 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
828 file->f_flags & O_NONBLOCK,
831 mutex_unlock(&dmxdevfilter->mutex);
835 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
836 unsigned int cmd, void *parg)
838 struct dmxdev_filter *dmxdevfilter = file->private_data;
839 struct dmxdev *dmxdev = dmxdevfilter->dev;
840 unsigned long arg = (unsigned long)parg;
843 if (mutex_lock_interruptible(&dmxdev->mutex))
848 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
849 mutex_unlock(&dmxdev->mutex);
852 if (dmxdevfilter->state < DMXDEV_STATE_SET)
855 ret = dvb_dmxdev_filter_start(dmxdevfilter);
856 mutex_unlock(&dmxdevfilter->mutex);
860 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861 mutex_unlock(&dmxdev->mutex);
864 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
865 mutex_unlock(&dmxdevfilter->mutex);
869 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
870 mutex_unlock(&dmxdev->mutex);
873 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
874 mutex_unlock(&dmxdevfilter->mutex);
877 case DMX_SET_PES_FILTER:
878 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
879 mutex_unlock(&dmxdev->mutex);
882 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
883 mutex_unlock(&dmxdevfilter->mutex);
886 case DMX_SET_BUFFER_SIZE:
887 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
888 mutex_unlock(&dmxdev->mutex);
891 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
892 mutex_unlock(&dmxdevfilter->mutex);
895 case DMX_GET_PES_PIDS:
896 if (!dmxdev->demux->get_pes_pids) {
900 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
904 if (!dmxdev->demux->get_caps) {
908 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
912 if (!dmxdev->demux->set_source) {
916 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
920 if (!dmxdev->demux->get_stc) {
924 ret = dmxdev->demux->get_stc(dmxdev->demux,
925 ((struct dmx_stc *)parg)->num,
926 &((struct dmx_stc *)parg)->stc,
927 &((struct dmx_stc *)parg)->base);
934 mutex_unlock(&dmxdev->mutex);
938 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
939 unsigned int cmd, unsigned long arg)
941 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
944 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
946 struct dmxdev_filter *dmxdevfilter = file->private_data;
947 unsigned int mask = 0;
952 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
954 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
955 dmxdevfilter->state != DMXDEV_STATE_DONE &&
956 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
959 if (dmxdevfilter->buffer.error)
960 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
962 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
963 mask |= (POLLIN | POLLRDNORM | POLLPRI);
968 static int dvb_demux_release(struct inode *inode, struct file *file)
970 struct dmxdev_filter *dmxdevfilter = file->private_data;
971 struct dmxdev *dmxdev = dmxdevfilter->dev;
975 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
977 mutex_lock(&dmxdev->mutex);
978 dmxdev->dvbdev->users--;
979 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
980 fops_put(file->f_op);
982 mutex_unlock(&dmxdev->mutex);
983 wake_up(&dmxdev->dvbdev->wait_queue);
985 mutex_unlock(&dmxdev->mutex);
990 static struct file_operations dvb_demux_fops = {
991 .owner = THIS_MODULE,
992 .read = dvb_demux_read,
993 .ioctl = dvb_demux_ioctl,
994 .open = dvb_demux_open,
995 .release = dvb_demux_release,
996 .poll = dvb_demux_poll,
999 static struct dvb_device dvbdev_demux = {
1003 .fops = &dvb_demux_fops
1006 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1007 unsigned int cmd, void *parg)
1009 struct dvb_device *dvbdev = file->private_data;
1010 struct dmxdev *dmxdev = dvbdev->priv;
1013 if (mutex_lock_interruptible(&dmxdev->mutex))
1014 return -ERESTARTSYS;
1017 case DMX_SET_BUFFER_SIZE:
1026 mutex_unlock(&dmxdev->mutex);
1030 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1031 unsigned int cmd, unsigned long arg)
1033 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1036 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1038 struct dvb_device *dvbdev = file->private_data;
1039 struct dmxdev *dmxdev = dvbdev->priv;
1040 unsigned int mask = 0;
1042 dprintk("function : %s\n", __FUNCTION__);
1044 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1046 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1047 if (dmxdev->dvr_buffer.error)
1048 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1050 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1051 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1053 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1058 static struct file_operations dvb_dvr_fops = {
1059 .owner = THIS_MODULE,
1060 .read = dvb_dvr_read,
1061 .write = dvb_dvr_write,
1062 .ioctl = dvb_dvr_ioctl,
1063 .open = dvb_dvr_open,
1064 .release = dvb_dvr_release,
1065 .poll = dvb_dvr_poll,
1068 static struct dvb_device dvbdev_dvr = {
1072 .fops = &dvb_dvr_fops
1075 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1079 if (dmxdev->demux->open(dmxdev->demux) < 0)
1082 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1083 if (!dmxdev->filter)
1086 mutex_init(&dmxdev->mutex);
1087 spin_lock_init(&dmxdev->lock);
1088 for (i = 0; i < dmxdev->filternum; i++) {
1089 dmxdev->filter[i].dev = dmxdev;
1090 dmxdev->filter[i].buffer.data = NULL;
1091 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1095 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1097 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1098 dmxdev, DVB_DEVICE_DVR);
1100 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1105 EXPORT_SYMBOL(dvb_dmxdev_init);
1107 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1110 if (dmxdev->dvbdev->users > 1) {
1111 wait_event(dmxdev->dvbdev->wait_queue,
1112 dmxdev->dvbdev->users==1);
1114 if (dmxdev->dvr_dvbdev->users > 1) {
1115 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1116 dmxdev->dvr_dvbdev->users==1);
1119 dvb_unregister_device(dmxdev->dvbdev);
1120 dvb_unregister_device(dmxdev->dvr_dvbdev);
1122 vfree(dmxdev->filter);
1123 dmxdev->filter = NULL;
1124 dmxdev->demux->close(dmxdev->demux);
1127 EXPORT_SYMBOL(dvb_dmxdev_release);