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))
135 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
136 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
137 mutex_unlock(&dmxdev->mutex);
142 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
144 if (!dvbdev->readers) {
145 mutex_unlock(&dmxdev->mutex);
148 mem = vmalloc(DVR_BUFFER_SIZE);
150 mutex_unlock(&dmxdev->mutex);
153 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
157 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
158 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
160 if (!dmxdev->demux->write) {
161 mutex_unlock(&dmxdev->mutex);
165 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
168 mutex_unlock(&dmxdev->mutex);
171 dmxdev->demux->disconnect_frontend(dmxdev->demux);
172 dmxdev->demux->connect_frontend(dmxdev->demux, front);
174 mutex_unlock(&dmxdev->mutex);
178 static int dvb_dvr_release(struct inode *inode, struct file *file)
180 struct dvb_device *dvbdev = file->private_data;
181 struct dmxdev *dmxdev = dvbdev->priv;
183 if (mutex_lock_interruptible(&dmxdev->mutex))
186 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
187 dmxdev->demux->disconnect_frontend(dmxdev->demux);
188 dmxdev->demux->connect_frontend(dmxdev->demux,
189 dmxdev->dvr_orig_fe);
191 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
193 if (dmxdev->dvr_buffer.data) {
194 void *mem = dmxdev->dvr_buffer.data;
196 spin_lock_irq(&dmxdev->lock);
197 dmxdev->dvr_buffer.data = NULL;
198 spin_unlock_irq(&dmxdev->lock);
202 mutex_unlock(&dmxdev->mutex);
206 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
207 size_t count, loff_t *ppos)
209 struct dvb_device *dvbdev = file->private_data;
210 struct dmxdev *dmxdev = dvbdev->priv;
213 if (!dmxdev->demux->write)
215 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
217 if (mutex_lock_interruptible(&dmxdev->mutex))
219 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
220 mutex_unlock(&dmxdev->mutex);
224 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
227 struct dvb_device *dvbdev = file->private_data;
228 struct dmxdev *dmxdev = dvbdev->priv;
231 //mutex_lock(&dmxdev->mutex);
232 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
233 file->f_flags & O_NONBLOCK,
235 //mutex_unlock(&dmxdev->mutex);
239 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
240 *dmxdevfilter, int state)
242 spin_lock_irq(&dmxdevfilter->dev->lock);
243 dmxdevfilter->state = state;
244 spin_unlock_irq(&dmxdevfilter->dev->lock);
247 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
250 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
253 if (buf->size == size)
255 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
257 spin_lock_irq(&dmxdevfilter->dev->lock);
261 dvb_ringbuffer_flush(buf);
262 spin_unlock_irq(&dmxdevfilter->dev->lock);
266 mem = vmalloc(dmxdevfilter->buffer.size);
269 spin_lock_irq(&dmxdevfilter->dev->lock);
271 spin_unlock_irq(&dmxdevfilter->dev->lock);
276 static void dvb_dmxdev_filter_timeout(unsigned long data)
278 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
280 dmxdevfilter->buffer.error = -ETIMEDOUT;
281 spin_lock_irq(&dmxdevfilter->dev->lock);
282 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
283 spin_unlock_irq(&dmxdevfilter->dev->lock);
284 wake_up(&dmxdevfilter->buffer.queue);
287 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
289 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
291 del_timer(&dmxdevfilter->timer);
293 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
294 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
295 dmxdevfilter->timer.expires =
296 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
297 add_timer(&dmxdevfilter->timer);
301 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
302 const u8 *buffer2, size_t buffer2_len,
303 struct dmx_section_filter *filter,
304 enum dmx_success success)
306 struct dmxdev_filter *dmxdevfilter = filter->priv;
309 if (dmxdevfilter->buffer.error) {
310 wake_up(&dmxdevfilter->buffer.queue);
313 spin_lock(&dmxdevfilter->dev->lock);
314 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
315 spin_unlock(&dmxdevfilter->dev->lock);
318 del_timer(&dmxdevfilter->timer);
319 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
320 buffer1[0], buffer1[1],
321 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
322 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
324 if (ret == buffer1_len) {
325 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
329 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
330 dmxdevfilter->buffer.error = ret;
332 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
333 dmxdevfilter->state = DMXDEV_STATE_DONE;
334 spin_unlock(&dmxdevfilter->dev->lock);
335 wake_up(&dmxdevfilter->buffer.queue);
339 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
340 const u8 *buffer2, size_t buffer2_len,
341 struct dmx_ts_feed *feed,
342 enum dmx_success success)
344 struct dmxdev_filter *dmxdevfilter = feed->priv;
345 struct dvb_ringbuffer *buffer;
348 spin_lock(&dmxdevfilter->dev->lock);
349 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
350 spin_unlock(&dmxdevfilter->dev->lock);
354 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
355 buffer = &dmxdevfilter->buffer;
357 buffer = &dmxdevfilter->dev->dvr_buffer;
359 spin_unlock(&dmxdevfilter->dev->lock);
360 wake_up(&buffer->queue);
363 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
364 if (ret == buffer1_len)
365 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
367 dvb_ringbuffer_flush(buffer);
370 spin_unlock(&dmxdevfilter->dev->lock);
371 wake_up(&buffer->queue);
375 /* stop feed but only mark the specified filter as stopped (state set) */
376 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
378 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
380 switch (dmxdevfilter->type) {
381 case DMXDEV_TYPE_SEC:
382 del_timer(&dmxdevfilter->timer);
383 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
385 case DMXDEV_TYPE_PES:
386 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
394 /* start feed associated with the specified filter */
395 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
397 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
399 switch (filter->type) {
400 case DMXDEV_TYPE_SEC:
401 return filter->feed.sec->start_filtering(filter->feed.sec);
402 case DMXDEV_TYPE_PES:
403 return filter->feed.ts->start_filtering(filter->feed.ts);
411 /* restart section feed if it has filters left associated with it,
412 otherwise release the feed */
413 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
416 struct dmxdev *dmxdev = filter->dev;
417 u16 pid = filter->params.sec.pid;
419 for (i = 0; i < dmxdev->filternum; i++)
420 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
421 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
422 dmxdev->filter[i].params.sec.pid == pid) {
423 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
427 filter->dev->demux->release_section_feed(dmxdev->demux,
433 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
435 if (dmxdevfilter->state < DMXDEV_STATE_GO)
438 switch (dmxdevfilter->type) {
439 case DMXDEV_TYPE_SEC:
440 if (!dmxdevfilter->feed.sec)
442 dvb_dmxdev_feed_stop(dmxdevfilter);
443 if (dmxdevfilter->filter.sec)
444 dmxdevfilter->feed.sec->
445 release_filter(dmxdevfilter->feed.sec,
446 dmxdevfilter->filter.sec);
447 dvb_dmxdev_feed_restart(dmxdevfilter);
448 dmxdevfilter->feed.sec = NULL;
450 case DMXDEV_TYPE_PES:
451 if (!dmxdevfilter->feed.ts)
453 dvb_dmxdev_feed_stop(dmxdevfilter);
454 dmxdevfilter->dev->demux->
455 release_ts_feed(dmxdevfilter->dev->demux,
456 dmxdevfilter->feed.ts);
457 dmxdevfilter->feed.ts = NULL;
460 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
465 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
469 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
471 if (dmxdevfilter->state < DMXDEV_STATE_SET)
474 dmxdevfilter->type = DMXDEV_TYPE_NONE;
475 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
479 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
481 struct dmxdev *dmxdev = filter->dev;
485 if (filter->state < DMXDEV_STATE_SET)
488 if (filter->state >= DMXDEV_STATE_GO)
489 dvb_dmxdev_filter_stop(filter);
491 if (!filter->buffer.data) {
492 mem = vmalloc(filter->buffer.size);
495 spin_lock_irq(&filter->dev->lock);
496 filter->buffer.data = mem;
497 spin_unlock_irq(&filter->dev->lock);
500 dvb_ringbuffer_flush(&filter->buffer);
502 switch (filter->type) {
503 case DMXDEV_TYPE_SEC:
505 struct dmx_sct_filter_params *para = &filter->params.sec;
506 struct dmx_section_filter **secfilter = &filter->filter.sec;
507 struct dmx_section_feed **secfeed = &filter->feed.sec;
513 /* find active filter/feed with same PID */
514 for (i = 0; i < dmxdev->filternum; i++) {
515 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
516 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
517 dmxdev->filter[i].params.sec.pid == para->pid) {
518 *secfeed = dmxdev->filter[i].feed.sec;
523 /* if no feed found, try to allocate new one */
525 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
527 dvb_dmxdev_section_callback);
529 printk("DVB (%s): could not alloc feed\n",
534 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
535 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
537 printk("DVB (%s): could not set feed\n",
539 dvb_dmxdev_feed_restart(filter);
543 dvb_dmxdev_feed_stop(filter);
546 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
548 dvb_dmxdev_feed_restart(filter);
549 filter->feed.sec->start_filtering(*secfeed);
550 dprintk("could not get filter\n");
554 (*secfilter)->priv = filter;
556 memcpy(&((*secfilter)->filter_value[3]),
557 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
558 memcpy(&(*secfilter)->filter_mask[3],
559 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
560 memcpy(&(*secfilter)->filter_mode[3],
561 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
563 (*secfilter)->filter_value[0] = para->filter.filter[0];
564 (*secfilter)->filter_mask[0] = para->filter.mask[0];
565 (*secfilter)->filter_mode[0] = para->filter.mode[0];
566 (*secfilter)->filter_mask[1] = 0;
567 (*secfilter)->filter_mask[2] = 0;
571 ret = filter->feed.sec->start_filtering(filter->feed.sec);
575 dvb_dmxdev_filter_timer(filter);
578 case DMXDEV_TYPE_PES:
580 struct timespec timeout = { 0 };
581 struct dmx_pes_filter_params *para = &filter->params.pes;
585 enum dmx_ts_pes ts_pes;
586 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
588 filter->feed.ts = NULL;
589 otype = para->output;
591 ts_pes = (enum dmx_ts_pes)para->pes_type;
593 if (ts_pes < DMX_PES_OTHER)
594 ts_type = TS_DECODER;
598 if (otype == DMX_OUT_TS_TAP)
599 ts_type |= TS_PACKET;
601 if (otype == DMX_OUT_TAP)
602 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
604 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
606 dvb_dmxdev_ts_callback);
610 (*tsfeed)->priv = filter;
612 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
615 dmxdev->demux->release_ts_feed(dmxdev->demux,
620 ret = filter->feed.ts->start_filtering(filter->feed.ts);
622 dmxdev->demux->release_ts_feed(dmxdev->demux,
633 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
637 static int dvb_demux_open(struct inode *inode, struct file *file)
639 struct dvb_device *dvbdev = file->private_data;
640 struct dmxdev *dmxdev = dvbdev->priv;
642 struct dmxdev_filter *dmxdevfilter;
647 if (mutex_lock_interruptible(&dmxdev->mutex))
650 for (i = 0; i < dmxdev->filternum; i++)
651 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
654 if (i == dmxdev->filternum) {
655 mutex_unlock(&dmxdev->mutex);
659 dmxdevfilter = &dmxdev->filter[i];
660 mutex_init(&dmxdevfilter->mutex);
661 file->private_data = dmxdevfilter;
663 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
664 dmxdevfilter->type = DMXDEV_TYPE_NONE;
665 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
666 dmxdevfilter->feed.ts = NULL;
667 init_timer(&dmxdevfilter->timer);
669 mutex_unlock(&dmxdev->mutex);
673 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
674 struct dmxdev_filter *dmxdevfilter)
676 if (mutex_lock_interruptible(&dmxdev->mutex))
679 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
680 mutex_unlock(&dmxdev->mutex);
684 dvb_dmxdev_filter_stop(dmxdevfilter);
685 dvb_dmxdev_filter_reset(dmxdevfilter);
687 if (dmxdevfilter->buffer.data) {
688 void *mem = dmxdevfilter->buffer.data;
690 spin_lock_irq(&dmxdev->lock);
691 dmxdevfilter->buffer.data = NULL;
692 spin_unlock_irq(&dmxdev->lock);
696 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
697 wake_up(&dmxdevfilter->buffer.queue);
698 mutex_unlock(&dmxdevfilter->mutex);
699 mutex_unlock(&dmxdev->mutex);
703 static inline void invert_mode(dmx_filter_t *filter)
707 for (i = 0; i < DMX_FILTER_SIZE; i++)
708 filter->mode[i] ^= 0xff;
711 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
712 struct dmxdev_filter *dmxdevfilter,
713 struct dmx_sct_filter_params *params)
715 dprintk("function : %s\n", __FUNCTION__);
717 dvb_dmxdev_filter_stop(dmxdevfilter);
719 dmxdevfilter->type = DMXDEV_TYPE_SEC;
720 memcpy(&dmxdevfilter->params.sec,
721 params, sizeof(struct dmx_sct_filter_params));
722 invert_mode(&dmxdevfilter->params.sec.filter);
723 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
725 if (params->flags & DMX_IMMEDIATE_START)
726 return dvb_dmxdev_filter_start(dmxdevfilter);
731 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
732 struct dmxdev_filter *dmxdevfilter,
733 struct dmx_pes_filter_params *params)
735 dvb_dmxdev_filter_stop(dmxdevfilter);
737 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
740 dmxdevfilter->type = DMXDEV_TYPE_PES;
741 memcpy(&dmxdevfilter->params, params,
742 sizeof(struct dmx_pes_filter_params));
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 ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
753 struct file *file, char __user *buf,
754 size_t count, loff_t *ppos)
759 if (dfil->todo <= 0) {
760 hcount = 3 + dfil->todo;
763 result = dvb_dmxdev_buffer_read(&dfil->buffer,
764 file->f_flags & O_NONBLOCK,
770 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
775 dfil->todo -= result;
778 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
782 if (count > dfil->todo)
784 result = dvb_dmxdev_buffer_read(&dfil->buffer,
785 file->f_flags & O_NONBLOCK,
789 dfil->todo -= result;
790 return (result + done);
794 dvb_demux_read(struct file *file, char __user *buf, size_t count,
797 struct dmxdev_filter *dmxdevfilter = file->private_data;
800 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
803 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
804 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
806 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
807 file->f_flags & O_NONBLOCK,
810 mutex_unlock(&dmxdevfilter->mutex);
814 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
815 unsigned int cmd, void *parg)
817 struct dmxdev_filter *dmxdevfilter = file->private_data;
818 struct dmxdev *dmxdev = dmxdevfilter->dev;
819 unsigned long arg = (unsigned long)parg;
822 if (mutex_lock_interruptible(&dmxdev->mutex))
827 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
828 mutex_unlock(&dmxdev->mutex);
831 if (dmxdevfilter->state < DMXDEV_STATE_SET)
834 ret = dvb_dmxdev_filter_start(dmxdevfilter);
835 mutex_unlock(&dmxdevfilter->mutex);
839 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
840 mutex_unlock(&dmxdev->mutex);
843 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
844 mutex_unlock(&dmxdevfilter->mutex);
848 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
849 mutex_unlock(&dmxdev->mutex);
852 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
853 mutex_unlock(&dmxdevfilter->mutex);
856 case DMX_SET_PES_FILTER:
857 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
858 mutex_unlock(&dmxdev->mutex);
861 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
862 mutex_unlock(&dmxdevfilter->mutex);
865 case DMX_SET_BUFFER_SIZE:
866 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
867 mutex_unlock(&dmxdev->mutex);
870 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
871 mutex_unlock(&dmxdevfilter->mutex);
874 case DMX_GET_PES_PIDS:
875 if (!dmxdev->demux->get_pes_pids) {
879 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
883 if (!dmxdev->demux->get_caps) {
887 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
891 if (!dmxdev->demux->set_source) {
895 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
899 if (!dmxdev->demux->get_stc) {
903 ret = dmxdev->demux->get_stc(dmxdev->demux,
904 ((struct dmx_stc *)parg)->num,
905 &((struct dmx_stc *)parg)->stc,
906 &((struct dmx_stc *)parg)->base);
913 mutex_unlock(&dmxdev->mutex);
917 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
918 unsigned int cmd, unsigned long arg)
920 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
923 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
925 struct dmxdev_filter *dmxdevfilter = file->private_data;
926 unsigned int mask = 0;
931 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
933 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
934 dmxdevfilter->state != DMXDEV_STATE_DONE &&
935 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
938 if (dmxdevfilter->buffer.error)
939 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
941 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
942 mask |= (POLLIN | POLLRDNORM | POLLPRI);
947 static int dvb_demux_release(struct inode *inode, struct file *file)
949 struct dmxdev_filter *dmxdevfilter = file->private_data;
950 struct dmxdev *dmxdev = dmxdevfilter->dev;
952 return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
955 static struct file_operations dvb_demux_fops = {
956 .owner = THIS_MODULE,
957 .read = dvb_demux_read,
958 .ioctl = dvb_demux_ioctl,
959 .open = dvb_demux_open,
960 .release = dvb_demux_release,
961 .poll = dvb_demux_poll,
964 static struct dvb_device dvbdev_demux = {
968 .fops = &dvb_demux_fops
971 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
972 unsigned int cmd, void *parg)
974 struct dvb_device *dvbdev = file->private_data;
975 struct dmxdev *dmxdev = dvbdev->priv;
978 if (mutex_lock_interruptible(&dmxdev->mutex))
982 case DMX_SET_BUFFER_SIZE:
991 mutex_unlock(&dmxdev->mutex);
995 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
996 unsigned int cmd, unsigned long arg)
998 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1001 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1003 struct dvb_device *dvbdev = file->private_data;
1004 struct dmxdev *dmxdev = dvbdev->priv;
1005 unsigned int mask = 0;
1007 dprintk("function : %s\n", __FUNCTION__);
1009 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1011 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1012 if (dmxdev->dvr_buffer.error)
1013 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1015 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1016 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1018 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1023 static struct file_operations dvb_dvr_fops = {
1024 .owner = THIS_MODULE,
1025 .read = dvb_dvr_read,
1026 .write = dvb_dvr_write,
1027 .ioctl = dvb_dvr_ioctl,
1028 .open = dvb_dvr_open,
1029 .release = dvb_dvr_release,
1030 .poll = dvb_dvr_poll,
1033 static struct dvb_device dvbdev_dvr = {
1036 .fops = &dvb_dvr_fops
1039 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1043 if (dmxdev->demux->open(dmxdev->demux) < 0)
1046 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1047 if (!dmxdev->filter)
1050 mutex_init(&dmxdev->mutex);
1051 spin_lock_init(&dmxdev->lock);
1052 for (i = 0; i < dmxdev->filternum; i++) {
1053 dmxdev->filter[i].dev = dmxdev;
1054 dmxdev->filter[i].buffer.data = NULL;
1055 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1059 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1061 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1062 dmxdev, DVB_DEVICE_DVR);
1064 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1069 EXPORT_SYMBOL(dvb_dmxdev_init);
1071 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1073 dvb_unregister_device(dmxdev->dvbdev);
1074 dvb_unregister_device(dmxdev->dvr_dvbdev);
1076 vfree(dmxdev->filter);
1077 dmxdev->filter = NULL;
1078 dmxdev->demux->close(dmxdev->demux);
1081 EXPORT_SYMBOL(dvb_dmxdev_release);