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.
19 #define pr_fmt(fmt) "dmxdev: " fmt
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/poll.h>
27 #include <linux/ioctl.h>
28 #include <linux/wait.h>
29 #include <linux/uaccess.h>
30 #include <media/dmxdev.h>
31 #include <media/dvb_vb2.h>
35 module_param(debug
, int, 0644);
36 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
38 #define dprintk(fmt, arg...) do { \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
44 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
45 const u8
*src
, size_t len
)
54 free
= dvb_ringbuffer_free(buf
);
56 dprintk("buffer overflow\n");
60 return dvb_ringbuffer_write(buf
, src
, len
);
63 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
64 int non_blocking
, char __user
*buf
,
65 size_t count
, loff_t
*ppos
)
76 dvb_ringbuffer_flush(src
);
80 for (todo
= count
; todo
> 0; todo
-= ret
) {
81 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
86 ret
= wait_event_interruptible(src
->queue
,
87 !dvb_ringbuffer_empty(src
) ||
94 dvb_ringbuffer_flush(src
);
98 avail
= dvb_ringbuffer_avail(src
);
102 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
109 return (count
- todo
) ? (count
- todo
) : ret
;
112 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
114 struct list_head
*head
, *pos
;
116 head
= demux
->get_frontends(demux
);
119 list_for_each(pos
, head
)
120 if (DMX_FE_ENTRY(pos
)->source
== type
)
121 return DMX_FE_ENTRY(pos
);
126 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
128 struct dvb_device
*dvbdev
= file
->private_data
;
129 struct dmxdev
*dmxdev
= dvbdev
->priv
;
130 struct dmx_frontend
*front
;
132 bool need_ringbuffer
= false;
134 const bool need_ringbuffer
= true;
137 dprintk("%s\n", __func__
);
139 if (mutex_lock_interruptible(&dmxdev
->mutex
))
143 mutex_unlock(&dmxdev
->mutex
);
148 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
149 need_ringbuffer
= true;
151 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
152 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
153 mutex_unlock(&dmxdev
->mutex
);
159 if (need_ringbuffer
) {
162 if (!dvbdev
->readers
) {
163 mutex_unlock(&dmxdev
->mutex
);
166 mem
= vmalloc(DVR_BUFFER_SIZE
);
168 mutex_unlock(&dmxdev
->mutex
);
171 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
172 dvb_vb2_init(&dmxdev
->dvr_vb2_ctx
, "dvr",
173 file
->f_flags
& O_NONBLOCK
);
177 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
178 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
180 if (!dmxdev
->demux
->write
) {
181 mutex_unlock(&dmxdev
->mutex
);
185 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
188 mutex_unlock(&dmxdev
->mutex
);
191 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
192 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
195 mutex_unlock(&dmxdev
->mutex
);
199 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
201 struct dvb_device
*dvbdev
= file
->private_data
;
202 struct dmxdev
*dmxdev
= dvbdev
->priv
;
204 bool need_ringbuffer
= false;
206 const bool need_ringbuffer
= true;
209 mutex_lock(&dmxdev
->mutex
);
211 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
212 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
213 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
214 dmxdev
->dvr_orig_fe
);
217 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
218 need_ringbuffer
= true;
221 if (need_ringbuffer
) {
222 if (dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
223 dvb_vb2_stream_off(&dmxdev
->dvr_vb2_ctx
);
224 dvb_vb2_release(&dmxdev
->dvr_vb2_ctx
);
226 if (dmxdev
->dvr_buffer
.data
) {
227 void *mem
= dmxdev
->dvr_buffer
.data
;
230 spin_lock_irq(&dmxdev
->lock
);
231 dmxdev
->dvr_buffer
.data
= NULL
;
232 spin_unlock_irq(&dmxdev
->lock
);
238 if (dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
239 mutex_unlock(&dmxdev
->mutex
);
240 wake_up(&dvbdev
->wait_queue
);
242 mutex_unlock(&dmxdev
->mutex
);
247 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
248 size_t count
, loff_t
*ppos
)
250 struct dvb_device
*dvbdev
= file
->private_data
;
251 struct dmxdev
*dmxdev
= dvbdev
->priv
;
254 if (!dmxdev
->demux
->write
)
256 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
258 if (mutex_lock_interruptible(&dmxdev
->mutex
))
262 mutex_unlock(&dmxdev
->mutex
);
265 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
266 mutex_unlock(&dmxdev
->mutex
);
270 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
273 struct dvb_device
*dvbdev
= file
->private_data
;
274 struct dmxdev
*dmxdev
= dvbdev
->priv
;
279 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
280 file
->f_flags
& O_NONBLOCK
,
284 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
287 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
291 dprintk("%s\n", __func__
);
293 if (buf
->size
== size
)
298 newmem
= vmalloc(size
);
304 spin_lock_irq(&dmxdev
->lock
);
308 /* reset and not flush in case the buffer shrinks */
309 dvb_ringbuffer_reset(buf
);
310 spin_unlock_irq(&dmxdev
->lock
);
317 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
318 *dmxdevfilter
, int state
)
320 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
321 dmxdevfilter
->state
= state
;
322 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
325 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
328 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
332 if (buf
->size
== size
)
336 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
339 newmem
= vmalloc(size
);
345 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
349 /* reset and not flush in case the buffer shrinks */
350 dvb_ringbuffer_reset(buf
);
351 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
358 static void dvb_dmxdev_filter_timeout(struct timer_list
*t
)
360 struct dmxdev_filter
*dmxdevfilter
= from_timer(dmxdevfilter
, t
, timer
);
362 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
363 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
364 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
365 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
366 wake_up(&dmxdevfilter
->buffer
.queue
);
369 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
371 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
373 del_timer(&dmxdevfilter
->timer
);
375 dmxdevfilter
->timer
.expires
=
376 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
377 add_timer(&dmxdevfilter
->timer
);
381 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
382 const u8
*buffer2
, size_t buffer2_len
,
383 struct dmx_section_filter
*filter
)
385 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
388 if (!dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
) &&
389 dmxdevfilter
->buffer
.error
) {
390 wake_up(&dmxdevfilter
->buffer
.queue
);
393 spin_lock(&dmxdevfilter
->dev
->lock
);
394 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
395 spin_unlock(&dmxdevfilter
->dev
->lock
);
398 del_timer(&dmxdevfilter
->timer
);
399 dprintk("section callback %*ph\n", 6, buffer1
);
400 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
)) {
401 ret
= dvb_vb2_fill_buffer(&dmxdevfilter
->vb2_ctx
,
402 buffer1
, buffer1_len
);
403 if (ret
== buffer1_len
)
404 ret
= dvb_vb2_fill_buffer(&dmxdevfilter
->vb2_ctx
,
405 buffer2
, buffer2_len
);
407 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
,
408 buffer1
, buffer1_len
);
409 if (ret
== buffer1_len
) {
410 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
,
411 buffer2
, buffer2_len
);
415 dmxdevfilter
->buffer
.error
= ret
;
416 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
417 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
418 spin_unlock(&dmxdevfilter
->dev
->lock
);
419 wake_up(&dmxdevfilter
->buffer
.queue
);
423 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
424 const u8
*buffer2
, size_t buffer2_len
,
425 struct dmx_ts_feed
*feed
)
427 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
428 struct dvb_ringbuffer
*buffer
;
430 struct dvb_vb2_ctx
*ctx
;
434 spin_lock(&dmxdevfilter
->dev
->lock
);
435 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
436 spin_unlock(&dmxdevfilter
->dev
->lock
);
440 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
||
441 dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
) {
442 buffer
= &dmxdevfilter
->buffer
;
444 ctx
= &dmxdevfilter
->vb2_ctx
;
447 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
449 ctx
= &dmxdevfilter
->dev
->dvr_vb2_ctx
;
453 if (dvb_vb2_is_streaming(ctx
)) {
454 ret
= dvb_vb2_fill_buffer(ctx
, buffer1
, buffer1_len
);
455 if (ret
== buffer1_len
)
456 ret
= dvb_vb2_fill_buffer(ctx
, buffer2
, buffer2_len
);
459 spin_unlock(&dmxdevfilter
->dev
->lock
);
460 wake_up(&buffer
->queue
);
463 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
464 if (ret
== buffer1_len
)
465 ret
= dvb_dmxdev_buffer_write(buffer
,
466 buffer2
, buffer2_len
);
470 spin_unlock(&dmxdevfilter
->dev
->lock
);
471 wake_up(&buffer
->queue
);
475 /* stop feed but only mark the specified filter as stopped (state set) */
476 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
478 struct dmxdev_feed
*feed
;
480 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
482 switch (dmxdevfilter
->type
) {
483 case DMXDEV_TYPE_SEC
:
484 del_timer(&dmxdevfilter
->timer
);
485 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
487 case DMXDEV_TYPE_PES
:
488 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
489 feed
->ts
->stop_filtering(feed
->ts
);
497 /* start feed associated with the specified filter */
498 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
500 struct dmxdev_feed
*feed
;
503 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
505 switch (filter
->type
) {
506 case DMXDEV_TYPE_SEC
:
507 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
508 case DMXDEV_TYPE_PES
:
509 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
510 ret
= feed
->ts
->start_filtering(feed
->ts
);
512 dvb_dmxdev_feed_stop(filter
);
524 /* restart section feed if it has filters left associated with it,
525 otherwise release the feed */
526 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
529 struct dmxdev
*dmxdev
= filter
->dev
;
530 u16 pid
= filter
->params
.sec
.pid
;
532 for (i
= 0; i
< dmxdev
->filternum
; i
++)
533 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
534 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
535 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
536 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
540 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
546 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
548 struct dmxdev_feed
*feed
;
549 struct dmx_demux
*demux
;
551 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
554 switch (dmxdevfilter
->type
) {
555 case DMXDEV_TYPE_SEC
:
556 if (!dmxdevfilter
->feed
.sec
)
558 dvb_dmxdev_feed_stop(dmxdevfilter
);
559 if (dmxdevfilter
->filter
.sec
)
560 dmxdevfilter
->feed
.sec
->
561 release_filter(dmxdevfilter
->feed
.sec
,
562 dmxdevfilter
->filter
.sec
);
563 dvb_dmxdev_feed_restart(dmxdevfilter
);
564 dmxdevfilter
->feed
.sec
= NULL
;
566 case DMXDEV_TYPE_PES
:
567 dvb_dmxdev_feed_stop(dmxdevfilter
);
568 demux
= dmxdevfilter
->dev
->demux
;
569 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
570 demux
->release_ts_feed(demux
, feed
->ts
);
575 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
580 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
584 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
586 struct dmxdev_feed
*feed
, *tmp
;
588 /* delete all PIDs */
589 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
590 list_del(&feed
->next
);
594 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
597 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
599 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
602 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
603 dvb_dmxdev_delete_pids(dmxdevfilter
);
605 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
606 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
610 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
611 struct dmxdev_filter
*filter
,
612 struct dmxdev_feed
*feed
)
615 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
616 enum dmx_output otype
;
619 enum dmx_ts_pes ts_pes
;
620 struct dmx_ts_feed
*tsfeed
;
623 otype
= para
->output
;
625 ts_pes
= para
->pes_type
;
627 if (ts_pes
< DMX_PES_OTHER
)
628 ts_type
= TS_DECODER
;
632 if (otype
== DMX_OUT_TS_TAP
)
633 ts_type
|= TS_PACKET
;
634 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
635 ts_type
|= TS_PACKET
| TS_DEMUX
;
636 else if (otype
== DMX_OUT_TAP
)
637 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
639 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
640 dvb_dmxdev_ts_callback
);
645 tsfeed
->priv
= filter
;
647 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, timeout
);
649 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
653 ret
= tsfeed
->start_filtering(tsfeed
);
655 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
662 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
664 struct dmxdev
*dmxdev
= filter
->dev
;
665 struct dmxdev_feed
*feed
;
669 if (filter
->state
< DMXDEV_STATE_SET
)
672 if (filter
->state
>= DMXDEV_STATE_GO
)
673 dvb_dmxdev_filter_stop(filter
);
675 if (!filter
->buffer
.data
) {
676 mem
= vmalloc(filter
->buffer
.size
);
679 spin_lock_irq(&filter
->dev
->lock
);
680 filter
->buffer
.data
= mem
;
681 spin_unlock_irq(&filter
->dev
->lock
);
684 dvb_ringbuffer_flush(&filter
->buffer
);
686 switch (filter
->type
) {
687 case DMXDEV_TYPE_SEC
:
689 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
690 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
691 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
697 /* find active filter/feed with same PID */
698 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
699 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
700 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
701 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
702 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
707 /* if no feed found, try to allocate new one */
709 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
711 dvb_dmxdev_section_callback
);
713 pr_err("DVB (%s): could not alloc feed\n",
718 ret
= (*secfeed
)->set(*secfeed
, para
->pid
,
719 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
721 pr_err("DVB (%s): could not set feed\n",
723 dvb_dmxdev_feed_restart(filter
);
727 dvb_dmxdev_feed_stop(filter
);
730 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
732 dvb_dmxdev_feed_restart(filter
);
733 filter
->feed
.sec
->start_filtering(*secfeed
);
734 dprintk("could not get filter\n");
738 (*secfilter
)->priv
= filter
;
740 memcpy(&((*secfilter
)->filter_value
[3]),
741 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
742 memcpy(&(*secfilter
)->filter_mask
[3],
743 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
744 memcpy(&(*secfilter
)->filter_mode
[3],
745 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
747 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
748 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
749 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
750 (*secfilter
)->filter_mask
[1] = 0;
751 (*secfilter
)->filter_mask
[2] = 0;
755 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
759 dvb_dmxdev_filter_timer(filter
);
762 case DMXDEV_TYPE_PES
:
763 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
764 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
766 dvb_dmxdev_filter_stop(filter
);
775 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
779 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
781 struct dvb_device
*dvbdev
= file
->private_data
;
782 struct dmxdev
*dmxdev
= dvbdev
->priv
;
784 struct dmxdev_filter
*dmxdevfilter
;
789 if (mutex_lock_interruptible(&dmxdev
->mutex
))
792 for (i
= 0; i
< dmxdev
->filternum
; i
++)
793 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
796 if (i
== dmxdev
->filternum
) {
797 mutex_unlock(&dmxdev
->mutex
);
801 dmxdevfilter
= &dmxdev
->filter
[i
];
802 mutex_init(&dmxdevfilter
->mutex
);
803 file
->private_data
= dmxdevfilter
;
805 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
806 dvb_vb2_init(&dmxdevfilter
->vb2_ctx
, "demux_filter",
807 file
->f_flags
& O_NONBLOCK
);
808 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
809 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
810 timer_setup(&dmxdevfilter
->timer
, dvb_dmxdev_filter_timeout
, 0);
814 mutex_unlock(&dmxdev
->mutex
);
818 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
819 struct dmxdev_filter
*dmxdevfilter
)
821 mutex_lock(&dmxdev
->mutex
);
822 mutex_lock(&dmxdevfilter
->mutex
);
823 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
824 dvb_vb2_stream_off(&dmxdevfilter
->vb2_ctx
);
825 dvb_vb2_release(&dmxdevfilter
->vb2_ctx
);
828 dvb_dmxdev_filter_stop(dmxdevfilter
);
829 dvb_dmxdev_filter_reset(dmxdevfilter
);
831 if (dmxdevfilter
->buffer
.data
) {
832 void *mem
= dmxdevfilter
->buffer
.data
;
834 spin_lock_irq(&dmxdev
->lock
);
835 dmxdevfilter
->buffer
.data
= NULL
;
836 spin_unlock_irq(&dmxdev
->lock
);
840 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
841 wake_up(&dmxdevfilter
->buffer
.queue
);
842 mutex_unlock(&dmxdevfilter
->mutex
);
843 mutex_unlock(&dmxdev
->mutex
);
847 static inline void invert_mode(struct dmx_filter
*filter
)
851 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
852 filter
->mode
[i
] ^= 0xff;
855 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
856 struct dmxdev_filter
*filter
, u16 pid
)
858 struct dmxdev_feed
*feed
;
860 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
861 (filter
->state
< DMXDEV_STATE_SET
))
864 /* only TS packet filters may have multiple PIDs */
865 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
866 (!list_empty(&filter
->feed
.ts
)))
869 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
874 list_add(&feed
->next
, &filter
->feed
.ts
);
876 if (filter
->state
>= DMXDEV_STATE_GO
)
877 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
882 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
883 struct dmxdev_filter
*filter
, u16 pid
)
885 struct dmxdev_feed
*feed
, *tmp
;
887 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
888 (filter
->state
< DMXDEV_STATE_SET
))
891 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
892 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
893 feed
->ts
->stop_filtering(feed
->ts
);
894 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
896 list_del(&feed
->next
);
904 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
905 struct dmxdev_filter
*dmxdevfilter
,
906 struct dmx_sct_filter_params
*params
)
908 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
909 __func__
, params
->pid
, params
->flags
, params
->timeout
);
911 dvb_dmxdev_filter_stop(dmxdevfilter
);
913 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
914 memcpy(&dmxdevfilter
->params
.sec
,
915 params
, sizeof(struct dmx_sct_filter_params
));
916 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
917 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
919 if (params
->flags
& DMX_IMMEDIATE_START
)
920 return dvb_dmxdev_filter_start(dmxdevfilter
);
925 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
926 struct dmxdev_filter
*dmxdevfilter
,
927 struct dmx_pes_filter_params
*params
)
931 dvb_dmxdev_filter_stop(dmxdevfilter
);
932 dvb_dmxdev_filter_reset(dmxdevfilter
);
934 if ((unsigned int)params
->pes_type
> DMX_PES_OTHER
)
937 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
938 memcpy(&dmxdevfilter
->params
, params
,
939 sizeof(struct dmx_pes_filter_params
));
940 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
942 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
944 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
945 dmxdevfilter
->params
.pes
.pid
);
949 if (params
->flags
& DMX_IMMEDIATE_START
)
950 return dvb_dmxdev_filter_start(dmxdevfilter
);
955 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
956 struct file
*file
, char __user
*buf
,
957 size_t count
, loff_t
*ppos
)
962 if (dfil
->todo
<= 0) {
963 hcount
= 3 + dfil
->todo
;
966 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
967 file
->f_flags
& O_NONBLOCK
,
973 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
978 dfil
->todo
-= result
;
981 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
985 if (count
> dfil
->todo
)
987 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
988 file
->f_flags
& O_NONBLOCK
,
992 dfil
->todo
-= result
;
993 return (result
+ done
);
997 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
1000 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1003 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
1004 return -ERESTARTSYS
;
1006 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
1007 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
1009 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
1010 file
->f_flags
& O_NONBLOCK
,
1013 mutex_unlock(&dmxdevfilter
->mutex
);
1017 static int dvb_demux_do_ioctl(struct file
*file
,
1018 unsigned int cmd
, void *parg
)
1020 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1021 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1022 unsigned long arg
= (unsigned long)parg
;
1025 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1026 return -ERESTARTSYS
;
1030 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1031 mutex_unlock(&dmxdev
->mutex
);
1032 return -ERESTARTSYS
;
1034 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
1037 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
1038 mutex_unlock(&dmxdevfilter
->mutex
);
1042 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1043 mutex_unlock(&dmxdev
->mutex
);
1044 return -ERESTARTSYS
;
1046 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
1047 mutex_unlock(&dmxdevfilter
->mutex
);
1050 case DMX_SET_FILTER
:
1051 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1052 mutex_unlock(&dmxdev
->mutex
);
1053 return -ERESTARTSYS
;
1055 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1056 mutex_unlock(&dmxdevfilter
->mutex
);
1059 case DMX_SET_PES_FILTER
:
1060 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1061 mutex_unlock(&dmxdev
->mutex
);
1062 return -ERESTARTSYS
;
1064 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1065 mutex_unlock(&dmxdevfilter
->mutex
);
1068 case DMX_SET_BUFFER_SIZE
:
1069 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1070 mutex_unlock(&dmxdev
->mutex
);
1071 return -ERESTARTSYS
;
1073 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1074 mutex_unlock(&dmxdevfilter
->mutex
);
1077 case DMX_GET_PES_PIDS
:
1078 if (!dmxdev
->demux
->get_pes_pids
) {
1082 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1086 if (!dmxdev
->demux
->get_stc
) {
1090 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1091 ((struct dmx_stc
*)parg
)->num
,
1092 &((struct dmx_stc
*)parg
)->stc
,
1093 &((struct dmx_stc
*)parg
)->base
);
1097 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1101 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1102 mutex_unlock(&dmxdevfilter
->mutex
);
1105 case DMX_REMOVE_PID
:
1106 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1110 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1111 mutex_unlock(&dmxdevfilter
->mutex
);
1116 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1117 mutex_unlock(&dmxdev
->mutex
);
1118 return -ERESTARTSYS
;
1120 ret
= dvb_vb2_reqbufs(&dmxdevfilter
->vb2_ctx
, parg
);
1121 mutex_unlock(&dmxdevfilter
->mutex
);
1125 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1126 mutex_unlock(&dmxdev
->mutex
);
1127 return -ERESTARTSYS
;
1129 ret
= dvb_vb2_querybuf(&dmxdevfilter
->vb2_ctx
, parg
);
1130 mutex_unlock(&dmxdevfilter
->mutex
);
1134 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1135 mutex_unlock(&dmxdev
->mutex
);
1136 return -ERESTARTSYS
;
1138 ret
= dvb_vb2_expbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1139 mutex_unlock(&dmxdevfilter
->mutex
);
1143 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1144 mutex_unlock(&dmxdev
->mutex
);
1145 return -ERESTARTSYS
;
1147 ret
= dvb_vb2_qbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1148 if (ret
== 0 && !dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
1149 ret
= dvb_vb2_stream_on(&dmxdevfilter
->vb2_ctx
);
1150 mutex_unlock(&dmxdevfilter
->mutex
);
1154 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1155 mutex_unlock(&dmxdev
->mutex
);
1156 return -ERESTARTSYS
;
1158 ret
= dvb_vb2_dqbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1159 mutex_unlock(&dmxdevfilter
->mutex
);
1166 mutex_unlock(&dmxdev
->mutex
);
1170 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1173 return dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1176 static __poll_t
dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1178 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1181 if ((!dmxdevfilter
) || dmxdevfilter
->dev
->exit
)
1183 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
1184 return dvb_vb2_poll(&dmxdevfilter
->vb2_ctx
, file
, wait
);
1186 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1188 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1189 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1190 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1193 if (dmxdevfilter
->buffer
.error
)
1194 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
| EPOLLERR
);
1196 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1197 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
);
1203 static int dvb_demux_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1205 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1206 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1209 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1210 return -ERESTARTSYS
;
1212 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1213 mutex_unlock(&dmxdev
->mutex
);
1214 return -ERESTARTSYS
;
1216 ret
= dvb_vb2_mmap(&dmxdevfilter
->vb2_ctx
, vma
);
1218 mutex_unlock(&dmxdevfilter
->mutex
);
1219 mutex_unlock(&dmxdev
->mutex
);
1225 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1227 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1228 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1231 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1233 mutex_lock(&dmxdev
->mutex
);
1234 dmxdev
->dvbdev
->users
--;
1235 if (dmxdev
->dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
1236 mutex_unlock(&dmxdev
->mutex
);
1237 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1239 mutex_unlock(&dmxdev
->mutex
);
1244 static const struct file_operations dvb_demux_fops
= {
1245 .owner
= THIS_MODULE
,
1246 .read
= dvb_demux_read
,
1247 .unlocked_ioctl
= dvb_demux_ioctl
,
1248 .open
= dvb_demux_open
,
1249 .release
= dvb_demux_release
,
1250 .poll
= dvb_demux_poll
,
1251 .llseek
= default_llseek
,
1253 .mmap
= dvb_demux_mmap
,
1257 static const struct dvb_device dvbdev_demux
= {
1261 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1262 .name
= "dvb-demux",
1264 .fops
= &dvb_demux_fops
1267 static int dvb_dvr_do_ioctl(struct file
*file
,
1268 unsigned int cmd
, void *parg
)
1270 struct dvb_device
*dvbdev
= file
->private_data
;
1271 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1272 unsigned long arg
= (unsigned long)parg
;
1275 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1276 return -ERESTARTSYS
;
1279 case DMX_SET_BUFFER_SIZE
:
1280 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1285 ret
= dvb_vb2_reqbufs(&dmxdev
->dvr_vb2_ctx
, parg
);
1289 ret
= dvb_vb2_querybuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1293 ret
= dvb_vb2_expbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1297 ret
= dvb_vb2_qbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1298 if (ret
== 0 && !dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
1299 ret
= dvb_vb2_stream_on(&dmxdev
->dvr_vb2_ctx
);
1303 ret
= dvb_vb2_dqbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1310 mutex_unlock(&dmxdev
->mutex
);
1314 static long dvb_dvr_ioctl(struct file
*file
,
1315 unsigned int cmd
, unsigned long arg
)
1317 return dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1320 static __poll_t
dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1322 struct dvb_device
*dvbdev
= file
->private_data
;
1323 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1326 bool need_ringbuffer
= false;
1328 const bool need_ringbuffer
= true;
1331 dprintk("%s\n", __func__
);
1335 if (dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
1336 return dvb_vb2_poll(&dmxdev
->dvr_vb2_ctx
, file
, wait
);
1338 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1341 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
1342 need_ringbuffer
= true;
1344 if (need_ringbuffer
) {
1345 if (dmxdev
->dvr_buffer
.error
)
1346 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
| EPOLLERR
);
1348 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1349 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
);
1351 mask
|= (EPOLLOUT
| EPOLLWRNORM
| EPOLLPRI
);
1357 static int dvb_dvr_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1359 struct dvb_device
*dvbdev
= file
->private_data
;
1360 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1366 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1367 return -ERESTARTSYS
;
1369 ret
= dvb_vb2_mmap(&dmxdev
->dvr_vb2_ctx
, vma
);
1370 mutex_unlock(&dmxdev
->mutex
);
1375 static const struct file_operations dvb_dvr_fops
= {
1376 .owner
= THIS_MODULE
,
1377 .read
= dvb_dvr_read
,
1378 .write
= dvb_dvr_write
,
1379 .unlocked_ioctl
= dvb_dvr_ioctl
,
1380 .open
= dvb_dvr_open
,
1381 .release
= dvb_dvr_release
,
1382 .poll
= dvb_dvr_poll
,
1383 .llseek
= default_llseek
,
1385 .mmap
= dvb_dvr_mmap
,
1389 static const struct dvb_device dvbdev_dvr
= {
1393 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1396 .fops
= &dvb_dvr_fops
1398 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1402 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1405 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1406 if (!dmxdev
->filter
)
1409 mutex_init(&dmxdev
->mutex
);
1410 spin_lock_init(&dmxdev
->lock
);
1411 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1412 dmxdev
->filter
[i
].dev
= dmxdev
;
1413 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1414 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1418 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1419 DVB_DEVICE_DEMUX
, dmxdev
->filternum
);
1420 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1421 dmxdev
, DVB_DEVICE_DVR
, dmxdev
->filternum
);
1423 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1428 EXPORT_SYMBOL(dvb_dmxdev_init
);
1430 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1433 if (dmxdev
->dvbdev
->users
> 1) {
1434 wait_event(dmxdev
->dvbdev
->wait_queue
,
1435 dmxdev
->dvbdev
->users
== 1);
1437 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1438 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1439 dmxdev
->dvr_dvbdev
->users
== 1);
1442 dvb_unregister_device(dmxdev
->dvbdev
);
1443 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1445 vfree(dmxdev
->filter
);
1446 dmxdev
->filter
= NULL
;
1447 dmxdev
->demux
->close(dmxdev
->demux
);
1450 EXPORT_SYMBOL(dvb_dmxdev_release
);