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/poll.h>
28 #include <linux/ioctl.h>
29 #include <linux/wait.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
39 #define dprintk if (debug) printk
41 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
42 const u8 *src, size_t len)
51 free = dvb_ringbuffer_free(buf);
53 dprintk("dmxdev: buffer overflow\n");
57 return dvb_ringbuffer_write(buf, src, len);
60 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
61 int non_blocking, char __user *buf,
62 size_t count, loff_t *ppos)
73 dvb_ringbuffer_flush(src);
77 for (todo = count; todo > 0; todo -= ret) {
78 if (non_blocking && dvb_ringbuffer_empty(src)) {
83 ret = wait_event_interruptible(src->queue,
84 !dvb_ringbuffer_empty(src) ||
91 dvb_ringbuffer_flush(src);
95 avail = dvb_ringbuffer_avail(src);
99 ret = dvb_ringbuffer_read_user(src, buf, avail);
106 return (count - todo) ? (count - todo) : ret;
109 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
111 struct list_head *head, *pos;
113 head = demux->get_frontends(demux);
116 list_for_each(pos, head)
117 if (DMX_FE_ENTRY(pos)->source == type)
118 return DMX_FE_ENTRY(pos);
123 static int dvb_dvr_open(struct inode *inode, struct file *file)
125 struct dvb_device *dvbdev = file->private_data;
126 struct dmxdev *dmxdev = dvbdev->priv;
127 struct dmx_frontend *front;
129 dprintk("function : %s\n", __func__);
131 if (mutex_lock_interruptible(&dmxdev->mutex))
135 mutex_unlock(&dmxdev->mutex);
139 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
141 mutex_unlock(&dmxdev->mutex);
146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148 if (!dvbdev->readers) {
149 mutex_unlock(&dmxdev->mutex);
152 mem = vmalloc(DVR_BUFFER_SIZE);
154 mutex_unlock(&dmxdev->mutex);
157 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
161 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
162 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
164 if (!dmxdev->demux->write) {
165 mutex_unlock(&dmxdev->mutex);
169 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
172 mutex_unlock(&dmxdev->mutex);
175 dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 dmxdev->demux->connect_frontend(dmxdev->demux, front);
179 mutex_unlock(&dmxdev->mutex);
183 static int dvb_dvr_release(struct inode *inode, struct file *file)
185 struct dvb_device *dvbdev = file->private_data;
186 struct dmxdev *dmxdev = dvbdev->priv;
188 mutex_lock(&dmxdev->mutex);
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);
195 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
197 if (dmxdev->dvr_buffer.data) {
198 void *mem = dmxdev->dvr_buffer.data;
200 spin_lock_irq(&dmxdev->lock);
201 dmxdev->dvr_buffer.data = NULL;
202 spin_unlock_irq(&dmxdev->lock);
208 if(dvbdev->users==-1 && dmxdev->exit==1) {
209 fops_put(file->f_op);
211 mutex_unlock(&dmxdev->mutex);
212 wake_up(&dvbdev->wait_queue);
214 mutex_unlock(&dmxdev->mutex);
219 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
220 size_t count, loff_t *ppos)
222 struct dvb_device *dvbdev = file->private_data;
223 struct dmxdev *dmxdev = dvbdev->priv;
226 if (!dmxdev->demux->write)
228 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
230 if (mutex_lock_interruptible(&dmxdev->mutex))
234 mutex_unlock(&dmxdev->mutex);
237 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
238 mutex_unlock(&dmxdev->mutex);
242 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
245 struct dvb_device *dvbdev = file->private_data;
246 struct dmxdev *dmxdev = dvbdev->priv;
250 mutex_unlock(&dmxdev->mutex);
254 //mutex_lock(&dmxdev->mutex);
255 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
256 file->f_flags & O_NONBLOCK,
258 //mutex_unlock(&dmxdev->mutex);
262 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
265 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
269 dprintk("function : %s\n", __func__);
271 if (buf->size == size)
276 newmem = vmalloc(size);
282 spin_lock_irq(&dmxdev->lock);
286 /* reset and not flush in case the buffer shrinks */
287 dvb_ringbuffer_reset(buf);
288 spin_unlock_irq(&dmxdev->lock);
295 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
296 *dmxdevfilter, int state)
298 spin_lock_irq(&dmxdevfilter->dev->lock);
299 dmxdevfilter->state = state;
300 spin_unlock_irq(&dmxdevfilter->dev->lock);
303 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
306 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
310 if (buf->size == size)
314 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
317 newmem = vmalloc(size);
323 spin_lock_irq(&dmxdevfilter->dev->lock);
327 /* reset and not flush in case the buffer shrinks */
328 dvb_ringbuffer_reset(buf);
329 spin_unlock_irq(&dmxdevfilter->dev->lock);
336 static void dvb_dmxdev_filter_timeout(unsigned long data)
338 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
340 dmxdevfilter->buffer.error = -ETIMEDOUT;
341 spin_lock_irq(&dmxdevfilter->dev->lock);
342 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
343 spin_unlock_irq(&dmxdevfilter->dev->lock);
344 wake_up(&dmxdevfilter->buffer.queue);
347 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
349 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
351 del_timer(&dmxdevfilter->timer);
353 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
354 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
355 dmxdevfilter->timer.expires =
356 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
357 add_timer(&dmxdevfilter->timer);
361 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
362 const u8 *buffer2, size_t buffer2_len,
363 struct dmx_section_filter *filter,
364 enum dmx_success success)
366 struct dmxdev_filter *dmxdevfilter = filter->priv;
369 if (dmxdevfilter->buffer.error) {
370 wake_up(&dmxdevfilter->buffer.queue);
373 spin_lock(&dmxdevfilter->dev->lock);
374 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
375 spin_unlock(&dmxdevfilter->dev->lock);
378 del_timer(&dmxdevfilter->timer);
379 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
380 buffer1[0], buffer1[1],
381 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
382 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
384 if (ret == buffer1_len) {
385 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
389 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
390 dmxdevfilter->buffer.error = ret;
392 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
393 dmxdevfilter->state = DMXDEV_STATE_DONE;
394 spin_unlock(&dmxdevfilter->dev->lock);
395 wake_up(&dmxdevfilter->buffer.queue);
399 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
400 const u8 *buffer2, size_t buffer2_len,
401 struct dmx_ts_feed *feed,
402 enum dmx_success success)
404 struct dmxdev_filter *dmxdevfilter = feed->priv;
405 struct dvb_ringbuffer *buffer;
408 spin_lock(&dmxdevfilter->dev->lock);
409 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
410 spin_unlock(&dmxdevfilter->dev->lock);
414 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
415 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
416 buffer = &dmxdevfilter->buffer;
418 buffer = &dmxdevfilter->dev->dvr_buffer;
420 spin_unlock(&dmxdevfilter->dev->lock);
421 wake_up(&buffer->queue);
424 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
425 if (ret == buffer1_len)
426 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
428 dvb_ringbuffer_flush(buffer);
431 spin_unlock(&dmxdevfilter->dev->lock);
432 wake_up(&buffer->queue);
436 /* stop feed but only mark the specified filter as stopped (state set) */
437 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
439 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
441 switch (dmxdevfilter->type) {
442 case DMXDEV_TYPE_SEC:
443 del_timer(&dmxdevfilter->timer);
444 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
446 case DMXDEV_TYPE_PES:
447 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
455 /* start feed associated with the specified filter */
456 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
458 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
460 switch (filter->type) {
461 case DMXDEV_TYPE_SEC:
462 return filter->feed.sec->start_filtering(filter->feed.sec);
463 case DMXDEV_TYPE_PES:
464 return filter->feed.ts->start_filtering(filter->feed.ts);
472 /* restart section feed if it has filters left associated with it,
473 otherwise release the feed */
474 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
477 struct dmxdev *dmxdev = filter->dev;
478 u16 pid = filter->params.sec.pid;
480 for (i = 0; i < dmxdev->filternum; i++)
481 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
482 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
483 dmxdev->filter[i].params.sec.pid == pid) {
484 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
488 filter->dev->demux->release_section_feed(dmxdev->demux,
494 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
496 if (dmxdevfilter->state < DMXDEV_STATE_GO)
499 switch (dmxdevfilter->type) {
500 case DMXDEV_TYPE_SEC:
501 if (!dmxdevfilter->feed.sec)
503 dvb_dmxdev_feed_stop(dmxdevfilter);
504 if (dmxdevfilter->filter.sec)
505 dmxdevfilter->feed.sec->
506 release_filter(dmxdevfilter->feed.sec,
507 dmxdevfilter->filter.sec);
508 dvb_dmxdev_feed_restart(dmxdevfilter);
509 dmxdevfilter->feed.sec = NULL;
511 case DMXDEV_TYPE_PES:
512 if (!dmxdevfilter->feed.ts)
514 dvb_dmxdev_feed_stop(dmxdevfilter);
515 dmxdevfilter->dev->demux->
516 release_ts_feed(dmxdevfilter->dev->demux,
517 dmxdevfilter->feed.ts);
518 dmxdevfilter->feed.ts = NULL;
521 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
526 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
530 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
532 if (dmxdevfilter->state < DMXDEV_STATE_SET)
535 dmxdevfilter->type = DMXDEV_TYPE_NONE;
536 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
540 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
542 struct dmxdev *dmxdev = filter->dev;
546 if (filter->state < DMXDEV_STATE_SET)
549 if (filter->state >= DMXDEV_STATE_GO)
550 dvb_dmxdev_filter_stop(filter);
552 if (!filter->buffer.data) {
553 mem = vmalloc(filter->buffer.size);
556 spin_lock_irq(&filter->dev->lock);
557 filter->buffer.data = mem;
558 spin_unlock_irq(&filter->dev->lock);
561 dvb_ringbuffer_flush(&filter->buffer);
563 switch (filter->type) {
564 case DMXDEV_TYPE_SEC:
566 struct dmx_sct_filter_params *para = &filter->params.sec;
567 struct dmx_section_filter **secfilter = &filter->filter.sec;
568 struct dmx_section_feed **secfeed = &filter->feed.sec;
574 /* find active filter/feed with same PID */
575 for (i = 0; i < dmxdev->filternum; i++) {
576 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
577 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
578 dmxdev->filter[i].params.sec.pid == para->pid) {
579 *secfeed = dmxdev->filter[i].feed.sec;
584 /* if no feed found, try to allocate new one */
586 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
588 dvb_dmxdev_section_callback);
590 printk("DVB (%s): could not alloc feed\n",
595 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
596 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
598 printk("DVB (%s): could not set feed\n",
600 dvb_dmxdev_feed_restart(filter);
604 dvb_dmxdev_feed_stop(filter);
607 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
609 dvb_dmxdev_feed_restart(filter);
610 filter->feed.sec->start_filtering(*secfeed);
611 dprintk("could not get filter\n");
615 (*secfilter)->priv = filter;
617 memcpy(&((*secfilter)->filter_value[3]),
618 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
619 memcpy(&(*secfilter)->filter_mask[3],
620 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
621 memcpy(&(*secfilter)->filter_mode[3],
622 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
624 (*secfilter)->filter_value[0] = para->filter.filter[0];
625 (*secfilter)->filter_mask[0] = para->filter.mask[0];
626 (*secfilter)->filter_mode[0] = para->filter.mode[0];
627 (*secfilter)->filter_mask[1] = 0;
628 (*secfilter)->filter_mask[2] = 0;
632 ret = filter->feed.sec->start_filtering(filter->feed.sec);
636 dvb_dmxdev_filter_timer(filter);
639 case DMXDEV_TYPE_PES:
641 struct timespec timeout = { 0 };
642 struct dmx_pes_filter_params *para = &filter->params.pes;
645 enum dmx_ts_pes ts_pes;
646 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
648 filter->feed.ts = NULL;
649 otype = para->output;
651 ts_pes = (enum dmx_ts_pes)para->pes_type;
653 if (ts_pes < DMX_PES_OTHER)
654 ts_type = TS_DECODER;
658 if (otype == DMX_OUT_TS_TAP)
659 ts_type |= TS_PACKET;
660 else if (otype == DMX_OUT_TSDEMUX_TAP)
661 ts_type |= TS_PACKET | TS_DEMUX;
662 else if (otype == DMX_OUT_TAP)
663 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
665 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
667 dvb_dmxdev_ts_callback);
671 (*tsfeed)->priv = filter;
673 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
676 dmxdev->demux->release_ts_feed(dmxdev->demux,
681 ret = filter->feed.ts->start_filtering(filter->feed.ts);
683 dmxdev->demux->release_ts_feed(dmxdev->demux,
694 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
698 static int dvb_demux_open(struct inode *inode, struct file *file)
700 struct dvb_device *dvbdev = file->private_data;
701 struct dmxdev *dmxdev = dvbdev->priv;
703 struct dmxdev_filter *dmxdevfilter;
708 if (mutex_lock_interruptible(&dmxdev->mutex))
711 for (i = 0; i < dmxdev->filternum; i++)
712 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
715 if (i == dmxdev->filternum) {
716 mutex_unlock(&dmxdev->mutex);
720 dmxdevfilter = &dmxdev->filter[i];
721 mutex_init(&dmxdevfilter->mutex);
722 file->private_data = dmxdevfilter;
724 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
725 dmxdevfilter->type = DMXDEV_TYPE_NONE;
726 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
727 dmxdevfilter->feed.ts = NULL;
728 init_timer(&dmxdevfilter->timer);
732 mutex_unlock(&dmxdev->mutex);
736 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
737 struct dmxdev_filter *dmxdevfilter)
739 mutex_lock(&dmxdev->mutex);
740 mutex_lock(&dmxdevfilter->mutex);
742 dvb_dmxdev_filter_stop(dmxdevfilter);
743 dvb_dmxdev_filter_reset(dmxdevfilter);
745 if (dmxdevfilter->buffer.data) {
746 void *mem = dmxdevfilter->buffer.data;
748 spin_lock_irq(&dmxdev->lock);
749 dmxdevfilter->buffer.data = NULL;
750 spin_unlock_irq(&dmxdev->lock);
754 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
755 wake_up(&dmxdevfilter->buffer.queue);
756 mutex_unlock(&dmxdevfilter->mutex);
757 mutex_unlock(&dmxdev->mutex);
761 static inline void invert_mode(dmx_filter_t *filter)
765 for (i = 0; i < DMX_FILTER_SIZE; i++)
766 filter->mode[i] ^= 0xff;
769 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
770 struct dmxdev_filter *dmxdevfilter,
771 struct dmx_sct_filter_params *params)
773 dprintk("function : %s\n", __func__);
775 dvb_dmxdev_filter_stop(dmxdevfilter);
777 dmxdevfilter->type = DMXDEV_TYPE_SEC;
778 memcpy(&dmxdevfilter->params.sec,
779 params, sizeof(struct dmx_sct_filter_params));
780 invert_mode(&dmxdevfilter->params.sec.filter);
781 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
783 if (params->flags & DMX_IMMEDIATE_START)
784 return dvb_dmxdev_filter_start(dmxdevfilter);
789 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
790 struct dmxdev_filter *dmxdevfilter,
791 struct dmx_pes_filter_params *params)
793 dvb_dmxdev_filter_stop(dmxdevfilter);
795 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
798 dmxdevfilter->type = DMXDEV_TYPE_PES;
799 memcpy(&dmxdevfilter->params, params,
800 sizeof(struct dmx_pes_filter_params));
802 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
804 if (params->flags & DMX_IMMEDIATE_START)
805 return dvb_dmxdev_filter_start(dmxdevfilter);
810 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
811 struct file *file, char __user *buf,
812 size_t count, loff_t *ppos)
817 if (dfil->todo <= 0) {
818 hcount = 3 + dfil->todo;
821 result = dvb_dmxdev_buffer_read(&dfil->buffer,
822 file->f_flags & O_NONBLOCK,
828 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
833 dfil->todo -= result;
836 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
840 if (count > dfil->todo)
842 result = dvb_dmxdev_buffer_read(&dfil->buffer,
843 file->f_flags & O_NONBLOCK,
847 dfil->todo -= result;
848 return (result + done);
852 dvb_demux_read(struct file *file, char __user *buf, size_t count,
855 struct dmxdev_filter *dmxdevfilter = file->private_data;
858 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
861 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
862 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
864 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
865 file->f_flags & O_NONBLOCK,
868 mutex_unlock(&dmxdevfilter->mutex);
872 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
873 unsigned int cmd, void *parg)
875 struct dmxdev_filter *dmxdevfilter = file->private_data;
876 struct dmxdev *dmxdev = dmxdevfilter->dev;
877 unsigned long arg = (unsigned long)parg;
880 if (mutex_lock_interruptible(&dmxdev->mutex))
885 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
886 mutex_unlock(&dmxdev->mutex);
889 if (dmxdevfilter->state < DMXDEV_STATE_SET)
892 ret = dvb_dmxdev_filter_start(dmxdevfilter);
893 mutex_unlock(&dmxdevfilter->mutex);
897 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
898 mutex_unlock(&dmxdev->mutex);
901 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
902 mutex_unlock(&dmxdevfilter->mutex);
906 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
907 mutex_unlock(&dmxdev->mutex);
910 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
911 mutex_unlock(&dmxdevfilter->mutex);
914 case DMX_SET_PES_FILTER:
915 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
916 mutex_unlock(&dmxdev->mutex);
919 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
920 mutex_unlock(&dmxdevfilter->mutex);
923 case DMX_SET_BUFFER_SIZE:
924 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
925 mutex_unlock(&dmxdev->mutex);
928 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
929 mutex_unlock(&dmxdevfilter->mutex);
932 case DMX_GET_PES_PIDS:
933 if (!dmxdev->demux->get_pes_pids) {
937 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
941 if (!dmxdev->demux->get_caps) {
945 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
949 if (!dmxdev->demux->set_source) {
953 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
957 if (!dmxdev->demux->get_stc) {
961 ret = dmxdev->demux->get_stc(dmxdev->demux,
962 ((struct dmx_stc *)parg)->num,
963 &((struct dmx_stc *)parg)->stc,
964 &((struct dmx_stc *)parg)->base);
971 mutex_unlock(&dmxdev->mutex);
975 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
976 unsigned int cmd, unsigned long arg)
978 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
981 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
983 struct dmxdev_filter *dmxdevfilter = file->private_data;
984 unsigned int mask = 0;
989 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
991 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
992 dmxdevfilter->state != DMXDEV_STATE_DONE &&
993 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
996 if (dmxdevfilter->buffer.error)
997 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
999 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1000 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1005 static int dvb_demux_release(struct inode *inode, struct file *file)
1007 struct dmxdev_filter *dmxdevfilter = file->private_data;
1008 struct dmxdev *dmxdev = dmxdevfilter->dev;
1012 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1014 mutex_lock(&dmxdev->mutex);
1015 dmxdev->dvbdev->users--;
1016 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1017 fops_put(file->f_op);
1019 mutex_unlock(&dmxdev->mutex);
1020 wake_up(&dmxdev->dvbdev->wait_queue);
1022 mutex_unlock(&dmxdev->mutex);
1027 static const struct file_operations dvb_demux_fops = {
1028 .owner = THIS_MODULE,
1029 .read = dvb_demux_read,
1030 .ioctl = dvb_demux_ioctl,
1031 .open = dvb_demux_open,
1032 .release = dvb_demux_release,
1033 .poll = dvb_demux_poll,
1036 static struct dvb_device dvbdev_demux = {
1040 .fops = &dvb_demux_fops
1043 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1044 unsigned int cmd, void *parg)
1046 struct dvb_device *dvbdev = file->private_data;
1047 struct dmxdev *dmxdev = dvbdev->priv;
1048 unsigned long arg = (unsigned long)parg;
1051 if (mutex_lock_interruptible(&dmxdev->mutex))
1052 return -ERESTARTSYS;
1055 case DMX_SET_BUFFER_SIZE:
1056 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1063 mutex_unlock(&dmxdev->mutex);
1067 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1068 unsigned int cmd, unsigned long arg)
1070 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1073 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1075 struct dvb_device *dvbdev = file->private_data;
1076 struct dmxdev *dmxdev = dvbdev->priv;
1077 unsigned int mask = 0;
1079 dprintk("function : %s\n", __func__);
1081 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1083 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1084 if (dmxdev->dvr_buffer.error)
1085 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1087 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1088 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1090 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1095 static struct file_operations dvb_dvr_fops = {
1096 .owner = THIS_MODULE,
1097 .read = dvb_dvr_read,
1098 .write = dvb_dvr_write,
1099 .ioctl = dvb_dvr_ioctl,
1100 .open = dvb_dvr_open,
1101 .release = dvb_dvr_release,
1102 .poll = dvb_dvr_poll,
1105 static struct dvb_device dvbdev_dvr = {
1109 .fops = &dvb_dvr_fops
1112 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1116 if (dmxdev->demux->open(dmxdev->demux) < 0)
1119 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1120 if (!dmxdev->filter)
1123 mutex_init(&dmxdev->mutex);
1124 spin_lock_init(&dmxdev->lock);
1125 for (i = 0; i < dmxdev->filternum; i++) {
1126 dmxdev->filter[i].dev = dmxdev;
1127 dmxdev->filter[i].buffer.data = NULL;
1128 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1132 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1134 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1135 dmxdev, DVB_DEVICE_DVR);
1137 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1142 EXPORT_SYMBOL(dvb_dmxdev_init);
1144 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1147 if (dmxdev->dvbdev->users > 1) {
1148 wait_event(dmxdev->dvbdev->wait_queue,
1149 dmxdev->dvbdev->users==1);
1151 if (dmxdev->dvr_dvbdev->users > 1) {
1152 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1153 dmxdev->dvr_dvbdev->users==1);
1156 dvb_unregister_device(dmxdev->dvbdev);
1157 dvb_unregister_device(dmxdev->dvr_dvbdev);
1159 vfree(dmxdev->filter);
1160 dmxdev->filter = NULL;
1161 dmxdev->demux->close(dmxdev->demux);
1164 EXPORT_SYMBOL(dvb_dmxdev_release);