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 mutex_lock(&dmxdev->mutex);
185 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
186 dmxdev->demux->disconnect_frontend(dmxdev->demux);
187 dmxdev->demux->connect_frontend(dmxdev->demux,
188 dmxdev->dvr_orig_fe);
190 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
192 if (dmxdev->dvr_buffer.data) {
193 void *mem = dmxdev->dvr_buffer.data;
195 spin_lock_irq(&dmxdev->lock);
196 dmxdev->dvr_buffer.data = NULL;
197 spin_unlock_irq(&dmxdev->lock);
201 mutex_unlock(&dmxdev->mutex);
205 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
206 size_t count, loff_t *ppos)
208 struct dvb_device *dvbdev = file->private_data;
209 struct dmxdev *dmxdev = dvbdev->priv;
212 if (!dmxdev->demux->write)
214 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
216 if (mutex_lock_interruptible(&dmxdev->mutex))
218 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
219 mutex_unlock(&dmxdev->mutex);
223 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
226 struct dvb_device *dvbdev = file->private_data;
227 struct dmxdev *dmxdev = dvbdev->priv;
230 //mutex_lock(&dmxdev->mutex);
231 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
232 file->f_flags & O_NONBLOCK,
234 //mutex_unlock(&dmxdev->mutex);
238 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
239 *dmxdevfilter, int state)
241 spin_lock_irq(&dmxdevfilter->dev->lock);
242 dmxdevfilter->state = state;
243 spin_unlock_irq(&dmxdevfilter->dev->lock);
246 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
249 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
252 if (buf->size == size)
254 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
256 spin_lock_irq(&dmxdevfilter->dev->lock);
260 dvb_ringbuffer_flush(buf);
261 spin_unlock_irq(&dmxdevfilter->dev->lock);
265 mem = vmalloc(dmxdevfilter->buffer.size);
268 spin_lock_irq(&dmxdevfilter->dev->lock);
270 spin_unlock_irq(&dmxdevfilter->dev->lock);
275 static void dvb_dmxdev_filter_timeout(unsigned long data)
277 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
279 dmxdevfilter->buffer.error = -ETIMEDOUT;
280 spin_lock_irq(&dmxdevfilter->dev->lock);
281 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
282 spin_unlock_irq(&dmxdevfilter->dev->lock);
283 wake_up(&dmxdevfilter->buffer.queue);
286 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
288 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
290 del_timer(&dmxdevfilter->timer);
292 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
293 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
294 dmxdevfilter->timer.expires =
295 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
296 add_timer(&dmxdevfilter->timer);
300 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
301 const u8 *buffer2, size_t buffer2_len,
302 struct dmx_section_filter *filter,
303 enum dmx_success success)
305 struct dmxdev_filter *dmxdevfilter = filter->priv;
308 if (dmxdevfilter->buffer.error) {
309 wake_up(&dmxdevfilter->buffer.queue);
312 spin_lock(&dmxdevfilter->dev->lock);
313 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
314 spin_unlock(&dmxdevfilter->dev->lock);
317 del_timer(&dmxdevfilter->timer);
318 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
319 buffer1[0], buffer1[1],
320 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
321 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
323 if (ret == buffer1_len) {
324 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
328 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
329 dmxdevfilter->buffer.error = ret;
331 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
332 dmxdevfilter->state = DMXDEV_STATE_DONE;
333 spin_unlock(&dmxdevfilter->dev->lock);
334 wake_up(&dmxdevfilter->buffer.queue);
338 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
339 const u8 *buffer2, size_t buffer2_len,
340 struct dmx_ts_feed *feed,
341 enum dmx_success success)
343 struct dmxdev_filter *dmxdevfilter = feed->priv;
344 struct dvb_ringbuffer *buffer;
347 spin_lock(&dmxdevfilter->dev->lock);
348 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
349 spin_unlock(&dmxdevfilter->dev->lock);
353 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
354 buffer = &dmxdevfilter->buffer;
356 buffer = &dmxdevfilter->dev->dvr_buffer;
358 spin_unlock(&dmxdevfilter->dev->lock);
359 wake_up(&buffer->queue);
362 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
363 if (ret == buffer1_len)
364 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
366 dvb_ringbuffer_flush(buffer);
369 spin_unlock(&dmxdevfilter->dev->lock);
370 wake_up(&buffer->queue);
374 /* stop feed but only mark the specified filter as stopped (state set) */
375 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
377 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
379 switch (dmxdevfilter->type) {
380 case DMXDEV_TYPE_SEC:
381 del_timer(&dmxdevfilter->timer);
382 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
384 case DMXDEV_TYPE_PES:
385 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
393 /* start feed associated with the specified filter */
394 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
396 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
398 switch (filter->type) {
399 case DMXDEV_TYPE_SEC:
400 return filter->feed.sec->start_filtering(filter->feed.sec);
401 case DMXDEV_TYPE_PES:
402 return filter->feed.ts->start_filtering(filter->feed.ts);
410 /* restart section feed if it has filters left associated with it,
411 otherwise release the feed */
412 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
415 struct dmxdev *dmxdev = filter->dev;
416 u16 pid = filter->params.sec.pid;
418 for (i = 0; i < dmxdev->filternum; i++)
419 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
420 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
421 dmxdev->filter[i].params.sec.pid == pid) {
422 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
426 filter->dev->demux->release_section_feed(dmxdev->demux,
432 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
434 if (dmxdevfilter->state < DMXDEV_STATE_GO)
437 switch (dmxdevfilter->type) {
438 case DMXDEV_TYPE_SEC:
439 if (!dmxdevfilter->feed.sec)
441 dvb_dmxdev_feed_stop(dmxdevfilter);
442 if (dmxdevfilter->filter.sec)
443 dmxdevfilter->feed.sec->
444 release_filter(dmxdevfilter->feed.sec,
445 dmxdevfilter->filter.sec);
446 dvb_dmxdev_feed_restart(dmxdevfilter);
447 dmxdevfilter->feed.sec = NULL;
449 case DMXDEV_TYPE_PES:
450 if (!dmxdevfilter->feed.ts)
452 dvb_dmxdev_feed_stop(dmxdevfilter);
453 dmxdevfilter->dev->demux->
454 release_ts_feed(dmxdevfilter->dev->demux,
455 dmxdevfilter->feed.ts);
456 dmxdevfilter->feed.ts = NULL;
459 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
464 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
468 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
470 if (dmxdevfilter->state < DMXDEV_STATE_SET)
473 dmxdevfilter->type = DMXDEV_TYPE_NONE;
474 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
478 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
480 struct dmxdev *dmxdev = filter->dev;
484 if (filter->state < DMXDEV_STATE_SET)
487 if (filter->state >= DMXDEV_STATE_GO)
488 dvb_dmxdev_filter_stop(filter);
490 if (!filter->buffer.data) {
491 mem = vmalloc(filter->buffer.size);
494 spin_lock_irq(&filter->dev->lock);
495 filter->buffer.data = mem;
496 spin_unlock_irq(&filter->dev->lock);
499 dvb_ringbuffer_flush(&filter->buffer);
501 switch (filter->type) {
502 case DMXDEV_TYPE_SEC:
504 struct dmx_sct_filter_params *para = &filter->params.sec;
505 struct dmx_section_filter **secfilter = &filter->filter.sec;
506 struct dmx_section_feed **secfeed = &filter->feed.sec;
512 /* find active filter/feed with same PID */
513 for (i = 0; i < dmxdev->filternum; i++) {
514 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
515 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
516 dmxdev->filter[i].params.sec.pid == para->pid) {
517 *secfeed = dmxdev->filter[i].feed.sec;
522 /* if no feed found, try to allocate new one */
524 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
526 dvb_dmxdev_section_callback);
528 printk("DVB (%s): could not alloc feed\n",
533 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
534 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
536 printk("DVB (%s): could not set feed\n",
538 dvb_dmxdev_feed_restart(filter);
542 dvb_dmxdev_feed_stop(filter);
545 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
547 dvb_dmxdev_feed_restart(filter);
548 filter->feed.sec->start_filtering(*secfeed);
549 dprintk("could not get filter\n");
553 (*secfilter)->priv = filter;
555 memcpy(&((*secfilter)->filter_value[3]),
556 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
557 memcpy(&(*secfilter)->filter_mask[3],
558 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
559 memcpy(&(*secfilter)->filter_mode[3],
560 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
562 (*secfilter)->filter_value[0] = para->filter.filter[0];
563 (*secfilter)->filter_mask[0] = para->filter.mask[0];
564 (*secfilter)->filter_mode[0] = para->filter.mode[0];
565 (*secfilter)->filter_mask[1] = 0;
566 (*secfilter)->filter_mask[2] = 0;
570 ret = filter->feed.sec->start_filtering(filter->feed.sec);
574 dvb_dmxdev_filter_timer(filter);
577 case DMXDEV_TYPE_PES:
579 struct timespec timeout = { 0 };
580 struct dmx_pes_filter_params *para = &filter->params.pes;
584 enum dmx_ts_pes ts_pes;
585 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
587 filter->feed.ts = NULL;
588 otype = para->output;
590 ts_pes = (enum dmx_ts_pes)para->pes_type;
592 if (ts_pes < DMX_PES_OTHER)
593 ts_type = TS_DECODER;
597 if (otype == DMX_OUT_TS_TAP)
598 ts_type |= TS_PACKET;
600 if (otype == DMX_OUT_TAP)
601 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
603 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
605 dvb_dmxdev_ts_callback);
609 (*tsfeed)->priv = filter;
611 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
614 dmxdev->demux->release_ts_feed(dmxdev->demux,
619 ret = filter->feed.ts->start_filtering(filter->feed.ts);
621 dmxdev->demux->release_ts_feed(dmxdev->demux,
632 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
636 static int dvb_demux_open(struct inode *inode, struct file *file)
638 struct dvb_device *dvbdev = file->private_data;
639 struct dmxdev *dmxdev = dvbdev->priv;
641 struct dmxdev_filter *dmxdevfilter;
646 if (mutex_lock_interruptible(&dmxdev->mutex))
649 for (i = 0; i < dmxdev->filternum; i++)
650 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
653 if (i == dmxdev->filternum) {
654 mutex_unlock(&dmxdev->mutex);
658 dmxdevfilter = &dmxdev->filter[i];
659 mutex_init(&dmxdevfilter->mutex);
660 file->private_data = dmxdevfilter;
662 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
663 dmxdevfilter->type = DMXDEV_TYPE_NONE;
664 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
665 dmxdevfilter->feed.ts = NULL;
666 init_timer(&dmxdevfilter->timer);
668 mutex_unlock(&dmxdev->mutex);
672 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
673 struct dmxdev_filter *dmxdevfilter)
675 mutex_lock(&dmxdev->mutex);
676 mutex_lock(&dmxdevfilter->mutex);
678 dvb_dmxdev_filter_stop(dmxdevfilter);
679 dvb_dmxdev_filter_reset(dmxdevfilter);
681 if (dmxdevfilter->buffer.data) {
682 void *mem = dmxdevfilter->buffer.data;
684 spin_lock_irq(&dmxdev->lock);
685 dmxdevfilter->buffer.data = NULL;
686 spin_unlock_irq(&dmxdev->lock);
690 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
691 wake_up(&dmxdevfilter->buffer.queue);
692 mutex_unlock(&dmxdevfilter->mutex);
693 mutex_unlock(&dmxdev->mutex);
697 static inline void invert_mode(dmx_filter_t *filter)
701 for (i = 0; i < DMX_FILTER_SIZE; i++)
702 filter->mode[i] ^= 0xff;
705 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
706 struct dmxdev_filter *dmxdevfilter,
707 struct dmx_sct_filter_params *params)
709 dprintk("function : %s\n", __FUNCTION__);
711 dvb_dmxdev_filter_stop(dmxdevfilter);
713 dmxdevfilter->type = DMXDEV_TYPE_SEC;
714 memcpy(&dmxdevfilter->params.sec,
715 params, sizeof(struct dmx_sct_filter_params));
716 invert_mode(&dmxdevfilter->params.sec.filter);
717 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
719 if (params->flags & DMX_IMMEDIATE_START)
720 return dvb_dmxdev_filter_start(dmxdevfilter);
725 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
726 struct dmxdev_filter *dmxdevfilter,
727 struct dmx_pes_filter_params *params)
729 dvb_dmxdev_filter_stop(dmxdevfilter);
731 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
734 dmxdevfilter->type = DMXDEV_TYPE_PES;
735 memcpy(&dmxdevfilter->params, params,
736 sizeof(struct dmx_pes_filter_params));
738 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
740 if (params->flags & DMX_IMMEDIATE_START)
741 return dvb_dmxdev_filter_start(dmxdevfilter);
746 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
747 struct file *file, char __user *buf,
748 size_t count, loff_t *ppos)
753 if (dfil->todo <= 0) {
754 hcount = 3 + dfil->todo;
757 result = dvb_dmxdev_buffer_read(&dfil->buffer,
758 file->f_flags & O_NONBLOCK,
764 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
769 dfil->todo -= result;
772 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
776 if (count > dfil->todo)
778 result = dvb_dmxdev_buffer_read(&dfil->buffer,
779 file->f_flags & O_NONBLOCK,
783 dfil->todo -= result;
784 return (result + done);
788 dvb_demux_read(struct file *file, char __user *buf, size_t count,
791 struct dmxdev_filter *dmxdevfilter = file->private_data;
794 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
797 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
798 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
800 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
801 file->f_flags & O_NONBLOCK,
804 mutex_unlock(&dmxdevfilter->mutex);
808 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
809 unsigned int cmd, void *parg)
811 struct dmxdev_filter *dmxdevfilter = file->private_data;
812 struct dmxdev *dmxdev = dmxdevfilter->dev;
813 unsigned long arg = (unsigned long)parg;
816 if (mutex_lock_interruptible(&dmxdev->mutex))
821 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
822 mutex_unlock(&dmxdev->mutex);
825 if (dmxdevfilter->state < DMXDEV_STATE_SET)
828 ret = dvb_dmxdev_filter_start(dmxdevfilter);
829 mutex_unlock(&dmxdevfilter->mutex);
833 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
834 mutex_unlock(&dmxdev->mutex);
837 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
838 mutex_unlock(&dmxdevfilter->mutex);
842 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
843 mutex_unlock(&dmxdev->mutex);
846 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
847 mutex_unlock(&dmxdevfilter->mutex);
850 case DMX_SET_PES_FILTER:
851 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
852 mutex_unlock(&dmxdev->mutex);
855 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
856 mutex_unlock(&dmxdevfilter->mutex);
859 case DMX_SET_BUFFER_SIZE:
860 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861 mutex_unlock(&dmxdev->mutex);
864 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
865 mutex_unlock(&dmxdevfilter->mutex);
868 case DMX_GET_PES_PIDS:
869 if (!dmxdev->demux->get_pes_pids) {
873 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
877 if (!dmxdev->demux->get_caps) {
881 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
885 if (!dmxdev->demux->set_source) {
889 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
893 if (!dmxdev->demux->get_stc) {
897 ret = dmxdev->demux->get_stc(dmxdev->demux,
898 ((struct dmx_stc *)parg)->num,
899 &((struct dmx_stc *)parg)->stc,
900 &((struct dmx_stc *)parg)->base);
907 mutex_unlock(&dmxdev->mutex);
911 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
912 unsigned int cmd, unsigned long arg)
914 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
917 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
919 struct dmxdev_filter *dmxdevfilter = file->private_data;
920 unsigned int mask = 0;
925 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
927 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
928 dmxdevfilter->state != DMXDEV_STATE_DONE &&
929 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
932 if (dmxdevfilter->buffer.error)
933 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
935 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
936 mask |= (POLLIN | POLLRDNORM | POLLPRI);
941 static int dvb_demux_release(struct inode *inode, struct file *file)
943 struct dmxdev_filter *dmxdevfilter = file->private_data;
944 struct dmxdev *dmxdev = dmxdevfilter->dev;
946 return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
949 static struct file_operations dvb_demux_fops = {
950 .owner = THIS_MODULE,
951 .read = dvb_demux_read,
952 .ioctl = dvb_demux_ioctl,
953 .open = dvb_demux_open,
954 .release = dvb_demux_release,
955 .poll = dvb_demux_poll,
958 static struct dvb_device dvbdev_demux = {
962 .fops = &dvb_demux_fops
965 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
966 unsigned int cmd, void *parg)
968 struct dvb_device *dvbdev = file->private_data;
969 struct dmxdev *dmxdev = dvbdev->priv;
972 if (mutex_lock_interruptible(&dmxdev->mutex))
976 case DMX_SET_BUFFER_SIZE:
985 mutex_unlock(&dmxdev->mutex);
989 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
990 unsigned int cmd, unsigned long arg)
992 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
995 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
997 struct dvb_device *dvbdev = file->private_data;
998 struct dmxdev *dmxdev = dvbdev->priv;
999 unsigned int mask = 0;
1001 dprintk("function : %s\n", __FUNCTION__);
1003 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1005 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1006 if (dmxdev->dvr_buffer.error)
1007 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1009 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1010 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1012 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1017 static struct file_operations dvb_dvr_fops = {
1018 .owner = THIS_MODULE,
1019 .read = dvb_dvr_read,
1020 .write = dvb_dvr_write,
1021 .ioctl = dvb_dvr_ioctl,
1022 .open = dvb_dvr_open,
1023 .release = dvb_dvr_release,
1024 .poll = dvb_dvr_poll,
1027 static struct dvb_device dvbdev_dvr = {
1030 .fops = &dvb_dvr_fops
1033 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1037 if (dmxdev->demux->open(dmxdev->demux) < 0)
1040 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1041 if (!dmxdev->filter)
1044 mutex_init(&dmxdev->mutex);
1045 spin_lock_init(&dmxdev->lock);
1046 for (i = 0; i < dmxdev->filternum; i++) {
1047 dmxdev->filter[i].dev = dmxdev;
1048 dmxdev->filter[i].buffer.data = NULL;
1049 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1053 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1055 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1056 dmxdev, DVB_DEVICE_DVR);
1058 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1063 EXPORT_SYMBOL(dvb_dmxdev_init);
1065 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1067 dvb_unregister_device(dmxdev->dvbdev);
1068 dvb_unregister_device(dmxdev->dvr_dvbdev);
1070 vfree(dmxdev->filter);
1071 dmxdev->filter = NULL;
1072 dmxdev->demux->close(dmxdev->demux);
1075 EXPORT_SYMBOL(dvb_dmxdev_release);