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
;
131 bool need_ringbuffer
= false;
133 dprintk("%s\n", __func__
);
135 if (mutex_lock_interruptible(&dmxdev
->mutex
))
139 mutex_unlock(&dmxdev
->mutex
);
143 dmxdev
->may_do_mmap
= 0;
146 * The logic here is a little tricky due to the ifdef.
148 * The ringbuffer is used for both read and mmap.
150 * It is not needed, however, on two situations:
151 * - Write devices (access with O_WRONLY);
152 * - For duplex device nodes, opened with O_RDWR.
155 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
)
156 need_ringbuffer
= true;
157 else if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
158 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
159 #ifdef CONFIG_DVB_MMAP
160 dmxdev
->may_do_mmap
= 1;
161 need_ringbuffer
= true;
163 mutex_unlock(&dmxdev
->mutex
);
169 if (need_ringbuffer
) {
172 if (!dvbdev
->readers
) {
173 mutex_unlock(&dmxdev
->mutex
);
176 mem
= vmalloc(DVR_BUFFER_SIZE
);
178 mutex_unlock(&dmxdev
->mutex
);
181 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
182 if (dmxdev
->may_do_mmap
)
183 dvb_vb2_init(&dmxdev
->dvr_vb2_ctx
, "dvr",
184 file
->f_flags
& O_NONBLOCK
);
188 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
189 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
191 if (!dmxdev
->demux
->write
) {
192 mutex_unlock(&dmxdev
->mutex
);
196 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
199 mutex_unlock(&dmxdev
->mutex
);
202 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
203 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
206 mutex_unlock(&dmxdev
->mutex
);
210 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
212 struct dvb_device
*dvbdev
= file
->private_data
;
213 struct dmxdev
*dmxdev
= dvbdev
->priv
;
215 mutex_lock(&dmxdev
->mutex
);
217 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
218 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
219 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
220 dmxdev
->dvr_orig_fe
);
223 if (((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) ||
224 dmxdev
->may_do_mmap
) {
225 if (dmxdev
->may_do_mmap
) {
226 if (dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
227 dvb_vb2_stream_off(&dmxdev
->dvr_vb2_ctx
);
228 dvb_vb2_release(&dmxdev
->dvr_vb2_ctx
);
231 if (dmxdev
->dvr_buffer
.data
) {
232 void *mem
= dmxdev
->dvr_buffer
.data
;
235 spin_lock_irq(&dmxdev
->lock
);
236 dmxdev
->dvr_buffer
.data
= NULL
;
237 spin_unlock_irq(&dmxdev
->lock
);
243 if (dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
244 mutex_unlock(&dmxdev
->mutex
);
245 wake_up(&dvbdev
->wait_queue
);
247 mutex_unlock(&dmxdev
->mutex
);
252 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
253 size_t count
, loff_t
*ppos
)
255 struct dvb_device
*dvbdev
= file
->private_data
;
256 struct dmxdev
*dmxdev
= dvbdev
->priv
;
259 if (!dmxdev
->demux
->write
)
261 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
263 if (mutex_lock_interruptible(&dmxdev
->mutex
))
267 mutex_unlock(&dmxdev
->mutex
);
270 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
271 mutex_unlock(&dmxdev
->mutex
);
275 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
278 struct dvb_device
*dvbdev
= file
->private_data
;
279 struct dmxdev
*dmxdev
= dvbdev
->priv
;
284 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
285 file
->f_flags
& O_NONBLOCK
,
289 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
292 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
296 dprintk("%s\n", __func__
);
298 if (buf
->size
== size
)
303 newmem
= vmalloc(size
);
309 spin_lock_irq(&dmxdev
->lock
);
313 /* reset and not flush in case the buffer shrinks */
314 dvb_ringbuffer_reset(buf
);
315 spin_unlock_irq(&dmxdev
->lock
);
322 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
323 *dmxdevfilter
, int state
)
325 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
326 dmxdevfilter
->state
= state
;
327 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
330 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
333 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
337 if (buf
->size
== size
)
341 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
344 newmem
= vmalloc(size
);
350 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
354 /* reset and not flush in case the buffer shrinks */
355 dvb_ringbuffer_reset(buf
);
356 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
363 static void dvb_dmxdev_filter_timeout(struct timer_list
*t
)
365 struct dmxdev_filter
*dmxdevfilter
= from_timer(dmxdevfilter
, t
, timer
);
367 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
368 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
369 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
370 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
371 wake_up(&dmxdevfilter
->buffer
.queue
);
374 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
376 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
378 del_timer(&dmxdevfilter
->timer
);
380 dmxdevfilter
->timer
.expires
=
381 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
382 add_timer(&dmxdevfilter
->timer
);
386 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
387 const u8
*buffer2
, size_t buffer2_len
,
388 struct dmx_section_filter
*filter
,
391 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
394 if (!dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
) &&
395 dmxdevfilter
->buffer
.error
) {
396 wake_up(&dmxdevfilter
->buffer
.queue
);
399 spin_lock(&dmxdevfilter
->dev
->lock
);
400 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
401 spin_unlock(&dmxdevfilter
->dev
->lock
);
404 del_timer(&dmxdevfilter
->timer
);
405 dprintk("section callback %*ph\n", 6, buffer1
);
406 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
)) {
407 ret
= dvb_vb2_fill_buffer(&dmxdevfilter
->vb2_ctx
,
408 buffer1
, buffer1_len
,
410 if (ret
== buffer1_len
)
411 ret
= dvb_vb2_fill_buffer(&dmxdevfilter
->vb2_ctx
,
412 buffer2
, buffer2_len
,
415 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
,
416 buffer1
, buffer1_len
);
417 if (ret
== buffer1_len
) {
418 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
,
419 buffer2
, buffer2_len
);
423 dmxdevfilter
->buffer
.error
= ret
;
424 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
425 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
426 spin_unlock(&dmxdevfilter
->dev
->lock
);
427 wake_up(&dmxdevfilter
->buffer
.queue
);
431 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
432 const u8
*buffer2
, size_t buffer2_len
,
433 struct dmx_ts_feed
*feed
,
436 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
437 struct dvb_ringbuffer
*buffer
;
438 #ifdef CONFIG_DVB_MMAP
439 struct dvb_vb2_ctx
*ctx
;
443 spin_lock(&dmxdevfilter
->dev
->lock
);
444 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
445 spin_unlock(&dmxdevfilter
->dev
->lock
);
449 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
||
450 dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
) {
451 buffer
= &dmxdevfilter
->buffer
;
452 #ifdef CONFIG_DVB_MMAP
453 ctx
= &dmxdevfilter
->vb2_ctx
;
456 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
457 #ifdef CONFIG_DVB_MMAP
458 ctx
= &dmxdevfilter
->dev
->dvr_vb2_ctx
;
462 if (dvb_vb2_is_streaming(ctx
)) {
463 ret
= dvb_vb2_fill_buffer(ctx
, buffer1
, buffer1_len
,
465 if (ret
== buffer1_len
)
466 ret
= dvb_vb2_fill_buffer(ctx
, buffer2
, buffer2_len
,
470 spin_unlock(&dmxdevfilter
->dev
->lock
);
471 wake_up(&buffer
->queue
);
474 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
475 if (ret
== buffer1_len
)
476 ret
= dvb_dmxdev_buffer_write(buffer
,
477 buffer2
, buffer2_len
);
481 spin_unlock(&dmxdevfilter
->dev
->lock
);
482 wake_up(&buffer
->queue
);
486 /* stop feed but only mark the specified filter as stopped (state set) */
487 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
489 struct dmxdev_feed
*feed
;
491 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
493 switch (dmxdevfilter
->type
) {
494 case DMXDEV_TYPE_SEC
:
495 del_timer(&dmxdevfilter
->timer
);
496 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
498 case DMXDEV_TYPE_PES
:
499 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
500 feed
->ts
->stop_filtering(feed
->ts
);
508 /* start feed associated with the specified filter */
509 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
511 struct dmxdev_feed
*feed
;
514 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
516 switch (filter
->type
) {
517 case DMXDEV_TYPE_SEC
:
518 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
519 case DMXDEV_TYPE_PES
:
520 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
521 ret
= feed
->ts
->start_filtering(feed
->ts
);
523 dvb_dmxdev_feed_stop(filter
);
535 /* restart section feed if it has filters left associated with it,
536 otherwise release the feed */
537 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
540 struct dmxdev
*dmxdev
= filter
->dev
;
541 u16 pid
= filter
->params
.sec
.pid
;
543 for (i
= 0; i
< dmxdev
->filternum
; i
++)
544 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
545 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
546 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
547 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
551 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
557 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
559 struct dmxdev_feed
*feed
;
560 struct dmx_demux
*demux
;
562 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
565 switch (dmxdevfilter
->type
) {
566 case DMXDEV_TYPE_SEC
:
567 if (!dmxdevfilter
->feed
.sec
)
569 dvb_dmxdev_feed_stop(dmxdevfilter
);
570 if (dmxdevfilter
->filter
.sec
)
571 dmxdevfilter
->feed
.sec
->
572 release_filter(dmxdevfilter
->feed
.sec
,
573 dmxdevfilter
->filter
.sec
);
574 dvb_dmxdev_feed_restart(dmxdevfilter
);
575 dmxdevfilter
->feed
.sec
= NULL
;
577 case DMXDEV_TYPE_PES
:
578 dvb_dmxdev_feed_stop(dmxdevfilter
);
579 demux
= dmxdevfilter
->dev
->demux
;
580 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
581 demux
->release_ts_feed(demux
, feed
->ts
);
586 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
591 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
595 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
597 struct dmxdev_feed
*feed
, *tmp
;
599 /* delete all PIDs */
600 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
601 list_del(&feed
->next
);
605 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
608 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
610 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
613 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
614 dvb_dmxdev_delete_pids(dmxdevfilter
);
616 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
617 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
621 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
622 struct dmxdev_filter
*filter
,
623 struct dmxdev_feed
*feed
)
626 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
627 enum dmx_output otype
;
630 enum dmx_ts_pes ts_pes
;
631 struct dmx_ts_feed
*tsfeed
;
634 otype
= para
->output
;
636 ts_pes
= para
->pes_type
;
638 if (ts_pes
< DMX_PES_OTHER
)
639 ts_type
= TS_DECODER
;
643 if (otype
== DMX_OUT_TS_TAP
)
644 ts_type
|= TS_PACKET
;
645 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
646 ts_type
|= TS_PACKET
| TS_DEMUX
;
647 else if (otype
== DMX_OUT_TAP
)
648 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
650 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
651 dvb_dmxdev_ts_callback
);
656 tsfeed
->priv
= filter
;
658 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, timeout
);
660 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
664 ret
= tsfeed
->start_filtering(tsfeed
);
666 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
673 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
675 struct dmxdev
*dmxdev
= filter
->dev
;
676 struct dmxdev_feed
*feed
;
680 if (filter
->state
< DMXDEV_STATE_SET
)
683 if (filter
->state
>= DMXDEV_STATE_GO
)
684 dvb_dmxdev_filter_stop(filter
);
686 if (!filter
->buffer
.data
) {
687 mem
= vmalloc(filter
->buffer
.size
);
690 spin_lock_irq(&filter
->dev
->lock
);
691 filter
->buffer
.data
= mem
;
692 spin_unlock_irq(&filter
->dev
->lock
);
695 dvb_ringbuffer_flush(&filter
->buffer
);
697 switch (filter
->type
) {
698 case DMXDEV_TYPE_SEC
:
700 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
701 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
702 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
708 /* find active filter/feed with same PID */
709 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
710 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
711 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
712 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
713 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
718 /* if no feed found, try to allocate new one */
720 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
722 dvb_dmxdev_section_callback
);
724 pr_err("DVB (%s): could not alloc feed\n",
729 ret
= (*secfeed
)->set(*secfeed
, para
->pid
,
730 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
732 pr_err("DVB (%s): could not set feed\n",
734 dvb_dmxdev_feed_restart(filter
);
738 dvb_dmxdev_feed_stop(filter
);
741 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
743 dvb_dmxdev_feed_restart(filter
);
744 filter
->feed
.sec
->start_filtering(*secfeed
);
745 dprintk("could not get filter\n");
749 (*secfilter
)->priv
= filter
;
751 memcpy(&((*secfilter
)->filter_value
[3]),
752 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
753 memcpy(&(*secfilter
)->filter_mask
[3],
754 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
755 memcpy(&(*secfilter
)->filter_mode
[3],
756 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
758 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
759 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
760 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
761 (*secfilter
)->filter_mask
[1] = 0;
762 (*secfilter
)->filter_mask
[2] = 0;
766 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
770 dvb_dmxdev_filter_timer(filter
);
773 case DMXDEV_TYPE_PES
:
774 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
775 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
777 dvb_dmxdev_filter_stop(filter
);
786 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
790 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
792 struct dvb_device
*dvbdev
= file
->private_data
;
793 struct dmxdev
*dmxdev
= dvbdev
->priv
;
795 struct dmxdev_filter
*dmxdevfilter
;
800 if (mutex_lock_interruptible(&dmxdev
->mutex
))
803 for (i
= 0; i
< dmxdev
->filternum
; i
++)
804 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
807 if (i
== dmxdev
->filternum
) {
808 mutex_unlock(&dmxdev
->mutex
);
812 dmxdevfilter
= &dmxdev
->filter
[i
];
813 mutex_init(&dmxdevfilter
->mutex
);
814 file
->private_data
= dmxdevfilter
;
816 #ifdef CONFIG_DVB_MMAP
817 dmxdev
->may_do_mmap
= 1;
819 dmxdev
->may_do_mmap
= 0;
822 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
823 dvb_vb2_init(&dmxdevfilter
->vb2_ctx
, "demux_filter",
824 file
->f_flags
& O_NONBLOCK
);
825 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
826 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
827 timer_setup(&dmxdevfilter
->timer
, dvb_dmxdev_filter_timeout
, 0);
831 mutex_unlock(&dmxdev
->mutex
);
835 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
836 struct dmxdev_filter
*dmxdevfilter
)
838 mutex_lock(&dmxdev
->mutex
);
839 mutex_lock(&dmxdevfilter
->mutex
);
840 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
841 dvb_vb2_stream_off(&dmxdevfilter
->vb2_ctx
);
842 dvb_vb2_release(&dmxdevfilter
->vb2_ctx
);
845 dvb_dmxdev_filter_stop(dmxdevfilter
);
846 dvb_dmxdev_filter_reset(dmxdevfilter
);
848 if (dmxdevfilter
->buffer
.data
) {
849 void *mem
= dmxdevfilter
->buffer
.data
;
851 spin_lock_irq(&dmxdev
->lock
);
852 dmxdevfilter
->buffer
.data
= NULL
;
853 spin_unlock_irq(&dmxdev
->lock
);
857 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
858 wake_up(&dmxdevfilter
->buffer
.queue
);
859 mutex_unlock(&dmxdevfilter
->mutex
);
860 mutex_unlock(&dmxdev
->mutex
);
864 static inline void invert_mode(struct dmx_filter
*filter
)
868 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
869 filter
->mode
[i
] ^= 0xff;
872 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
873 struct dmxdev_filter
*filter
, u16 pid
)
875 struct dmxdev_feed
*feed
;
877 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
878 (filter
->state
< DMXDEV_STATE_SET
))
881 /* only TS packet filters may have multiple PIDs */
882 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
883 (!list_empty(&filter
->feed
.ts
)))
886 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
891 list_add(&feed
->next
, &filter
->feed
.ts
);
893 if (filter
->state
>= DMXDEV_STATE_GO
)
894 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
899 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
900 struct dmxdev_filter
*filter
, u16 pid
)
902 struct dmxdev_feed
*feed
, *tmp
;
904 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
905 (filter
->state
< DMXDEV_STATE_SET
))
908 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
909 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
910 feed
->ts
->stop_filtering(feed
->ts
);
911 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
913 list_del(&feed
->next
);
921 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
922 struct dmxdev_filter
*dmxdevfilter
,
923 struct dmx_sct_filter_params
*params
)
925 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
926 __func__
, params
->pid
, params
->flags
, params
->timeout
);
928 dvb_dmxdev_filter_stop(dmxdevfilter
);
930 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
931 memcpy(&dmxdevfilter
->params
.sec
,
932 params
, sizeof(struct dmx_sct_filter_params
));
933 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
934 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
936 if (params
->flags
& DMX_IMMEDIATE_START
)
937 return dvb_dmxdev_filter_start(dmxdevfilter
);
942 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
943 struct dmxdev_filter
*dmxdevfilter
,
944 struct dmx_pes_filter_params
*params
)
948 dvb_dmxdev_filter_stop(dmxdevfilter
);
949 dvb_dmxdev_filter_reset(dmxdevfilter
);
951 if ((unsigned int)params
->pes_type
> DMX_PES_OTHER
)
954 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
955 memcpy(&dmxdevfilter
->params
, params
,
956 sizeof(struct dmx_pes_filter_params
));
957 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
959 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
961 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
962 dmxdevfilter
->params
.pes
.pid
);
966 if (params
->flags
& DMX_IMMEDIATE_START
)
967 return dvb_dmxdev_filter_start(dmxdevfilter
);
972 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
973 struct file
*file
, char __user
*buf
,
974 size_t count
, loff_t
*ppos
)
979 if (dfil
->todo
<= 0) {
980 hcount
= 3 + dfil
->todo
;
983 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
984 file
->f_flags
& O_NONBLOCK
,
990 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
995 dfil
->todo
-= result
;
998 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
1002 if (count
> dfil
->todo
)
1004 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
1005 file
->f_flags
& O_NONBLOCK
,
1009 dfil
->todo
-= result
;
1010 return (result
+ done
);
1014 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
1017 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1020 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
1021 return -ERESTARTSYS
;
1023 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
1024 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
1026 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
1027 file
->f_flags
& O_NONBLOCK
,
1030 mutex_unlock(&dmxdevfilter
->mutex
);
1034 static int dvb_demux_do_ioctl(struct file
*file
,
1035 unsigned int cmd
, void *parg
)
1037 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1038 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1039 unsigned long arg
= (unsigned long)parg
;
1042 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1043 return -ERESTARTSYS
;
1047 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1048 mutex_unlock(&dmxdev
->mutex
);
1049 return -ERESTARTSYS
;
1051 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
1054 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
1055 mutex_unlock(&dmxdevfilter
->mutex
);
1059 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1060 mutex_unlock(&dmxdev
->mutex
);
1061 return -ERESTARTSYS
;
1063 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
1064 mutex_unlock(&dmxdevfilter
->mutex
);
1067 case DMX_SET_FILTER
:
1068 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1069 mutex_unlock(&dmxdev
->mutex
);
1070 return -ERESTARTSYS
;
1072 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1073 mutex_unlock(&dmxdevfilter
->mutex
);
1076 case DMX_SET_PES_FILTER
:
1077 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1078 mutex_unlock(&dmxdev
->mutex
);
1079 return -ERESTARTSYS
;
1081 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1082 mutex_unlock(&dmxdevfilter
->mutex
);
1085 case DMX_SET_BUFFER_SIZE
:
1086 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1087 mutex_unlock(&dmxdev
->mutex
);
1088 return -ERESTARTSYS
;
1090 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1091 mutex_unlock(&dmxdevfilter
->mutex
);
1094 case DMX_GET_PES_PIDS
:
1095 if (!dmxdev
->demux
->get_pes_pids
) {
1099 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1103 if (!dmxdev
->demux
->get_stc
) {
1107 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1108 ((struct dmx_stc
*)parg
)->num
,
1109 &((struct dmx_stc
*)parg
)->stc
,
1110 &((struct dmx_stc
*)parg
)->base
);
1114 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1118 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1119 mutex_unlock(&dmxdevfilter
->mutex
);
1122 case DMX_REMOVE_PID
:
1123 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1127 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1128 mutex_unlock(&dmxdevfilter
->mutex
);
1131 #ifdef CONFIG_DVB_MMAP
1133 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1134 mutex_unlock(&dmxdev
->mutex
);
1135 return -ERESTARTSYS
;
1137 ret
= dvb_vb2_reqbufs(&dmxdevfilter
->vb2_ctx
, parg
);
1138 mutex_unlock(&dmxdevfilter
->mutex
);
1142 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1143 mutex_unlock(&dmxdev
->mutex
);
1144 return -ERESTARTSYS
;
1146 ret
= dvb_vb2_querybuf(&dmxdevfilter
->vb2_ctx
, parg
);
1147 mutex_unlock(&dmxdevfilter
->mutex
);
1151 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1152 mutex_unlock(&dmxdev
->mutex
);
1153 return -ERESTARTSYS
;
1155 ret
= dvb_vb2_expbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1156 mutex_unlock(&dmxdevfilter
->mutex
);
1160 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1161 mutex_unlock(&dmxdev
->mutex
);
1162 return -ERESTARTSYS
;
1164 ret
= dvb_vb2_qbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1165 if (ret
== 0 && !dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
1166 ret
= dvb_vb2_stream_on(&dmxdevfilter
->vb2_ctx
);
1167 mutex_unlock(&dmxdevfilter
->mutex
);
1171 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1172 mutex_unlock(&dmxdev
->mutex
);
1173 return -ERESTARTSYS
;
1175 ret
= dvb_vb2_dqbuf(&dmxdevfilter
->vb2_ctx
, parg
);
1176 mutex_unlock(&dmxdevfilter
->mutex
);
1183 mutex_unlock(&dmxdev
->mutex
);
1187 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1190 return dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1193 static __poll_t
dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1195 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1198 if ((!dmxdevfilter
) || dmxdevfilter
->dev
->exit
)
1200 if (dvb_vb2_is_streaming(&dmxdevfilter
->vb2_ctx
))
1201 return dvb_vb2_poll(&dmxdevfilter
->vb2_ctx
, file
, wait
);
1203 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1205 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1206 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1207 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1210 if (dmxdevfilter
->buffer
.error
)
1211 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
| EPOLLERR
);
1213 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1214 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
);
1219 #ifdef CONFIG_DVB_MMAP
1220 static int dvb_demux_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1222 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1223 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1226 if (!dmxdev
->may_do_mmap
)
1229 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1230 return -ERESTARTSYS
;
1232 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1233 mutex_unlock(&dmxdev
->mutex
);
1234 return -ERESTARTSYS
;
1236 ret
= dvb_vb2_mmap(&dmxdevfilter
->vb2_ctx
, vma
);
1238 mutex_unlock(&dmxdevfilter
->mutex
);
1239 mutex_unlock(&dmxdev
->mutex
);
1245 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1247 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1248 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1251 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1253 mutex_lock(&dmxdev
->mutex
);
1254 dmxdev
->dvbdev
->users
--;
1255 if (dmxdev
->dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
1256 mutex_unlock(&dmxdev
->mutex
);
1257 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1259 mutex_unlock(&dmxdev
->mutex
);
1264 static const struct file_operations dvb_demux_fops
= {
1265 .owner
= THIS_MODULE
,
1266 .read
= dvb_demux_read
,
1267 .unlocked_ioctl
= dvb_demux_ioctl
,
1268 .open
= dvb_demux_open
,
1269 .release
= dvb_demux_release
,
1270 .poll
= dvb_demux_poll
,
1271 .llseek
= default_llseek
,
1272 #ifdef CONFIG_DVB_MMAP
1273 .mmap
= dvb_demux_mmap
,
1277 static const struct dvb_device dvbdev_demux
= {
1281 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1282 .name
= "dvb-demux",
1284 .fops
= &dvb_demux_fops
1287 static int dvb_dvr_do_ioctl(struct file
*file
,
1288 unsigned int cmd
, void *parg
)
1290 struct dvb_device
*dvbdev
= file
->private_data
;
1291 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1292 unsigned long arg
= (unsigned long)parg
;
1295 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1296 return -ERESTARTSYS
;
1299 case DMX_SET_BUFFER_SIZE
:
1300 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1303 #ifdef CONFIG_DVB_MMAP
1305 ret
= dvb_vb2_reqbufs(&dmxdev
->dvr_vb2_ctx
, parg
);
1309 ret
= dvb_vb2_querybuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1313 ret
= dvb_vb2_expbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1317 ret
= dvb_vb2_qbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1318 if (ret
== 0 && !dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
1319 ret
= dvb_vb2_stream_on(&dmxdev
->dvr_vb2_ctx
);
1323 ret
= dvb_vb2_dqbuf(&dmxdev
->dvr_vb2_ctx
, parg
);
1330 mutex_unlock(&dmxdev
->mutex
);
1334 static long dvb_dvr_ioctl(struct file
*file
,
1335 unsigned int cmd
, unsigned long arg
)
1337 return dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1340 static __poll_t
dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1342 struct dvb_device
*dvbdev
= file
->private_data
;
1343 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1346 dprintk("%s\n", __func__
);
1350 if (dvb_vb2_is_streaming(&dmxdev
->dvr_vb2_ctx
))
1351 return dvb_vb2_poll(&dmxdev
->dvr_vb2_ctx
, file
, wait
);
1353 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1355 if (((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) ||
1356 dmxdev
->may_do_mmap
) {
1357 if (dmxdev
->dvr_buffer
.error
)
1358 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
| EPOLLERR
);
1360 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1361 mask
|= (EPOLLIN
| EPOLLRDNORM
| EPOLLPRI
);
1363 mask
|= (EPOLLOUT
| EPOLLWRNORM
| EPOLLPRI
);
1368 #ifdef CONFIG_DVB_MMAP
1369 static int dvb_dvr_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1371 struct dvb_device
*dvbdev
= file
->private_data
;
1372 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1375 if (!dmxdev
->may_do_mmap
)
1381 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1382 return -ERESTARTSYS
;
1384 ret
= dvb_vb2_mmap(&dmxdev
->dvr_vb2_ctx
, vma
);
1385 mutex_unlock(&dmxdev
->mutex
);
1390 static const struct file_operations dvb_dvr_fops
= {
1391 .owner
= THIS_MODULE
,
1392 .read
= dvb_dvr_read
,
1393 .write
= dvb_dvr_write
,
1394 .unlocked_ioctl
= dvb_dvr_ioctl
,
1395 .open
= dvb_dvr_open
,
1396 .release
= dvb_dvr_release
,
1397 .poll
= dvb_dvr_poll
,
1398 .llseek
= default_llseek
,
1399 #ifdef CONFIG_DVB_MMAP
1400 .mmap
= dvb_dvr_mmap
,
1404 static const struct dvb_device dvbdev_dvr
= {
1408 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1411 .fops
= &dvb_dvr_fops
1413 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1417 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1420 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1421 if (!dmxdev
->filter
)
1424 mutex_init(&dmxdev
->mutex
);
1425 spin_lock_init(&dmxdev
->lock
);
1426 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1427 dmxdev
->filter
[i
].dev
= dmxdev
;
1428 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1429 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1433 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1434 DVB_DEVICE_DEMUX
, dmxdev
->filternum
);
1435 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1436 dmxdev
, DVB_DEVICE_DVR
, dmxdev
->filternum
);
1438 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1443 EXPORT_SYMBOL(dvb_dmxdev_init
);
1445 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1448 if (dmxdev
->dvbdev
->users
> 1) {
1449 wait_event(dmxdev
->dvbdev
->wait_queue
,
1450 dmxdev
->dvbdev
->users
== 1);
1452 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1453 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1454 dmxdev
->dvr_dvbdev
->users
== 1);
1457 dvb_unregister_device(dmxdev
->dvbdev
);
1458 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1460 vfree(dmxdev
->filter
);
1461 dmxdev
->filter
= NULL
;
1462 dmxdev
->demux
->close(dmxdev
->demux
);
1465 EXPORT_SYMBOL(dvb_dmxdev_release
);