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/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
36 module_param(debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
39 #define dprintk if (debug) printk
41 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
42 const u8
*src
, size_t len
)
51 free
= dvb_ringbuffer_free(buf
);
53 dprintk("dmxdev: buffer overflow\n");
57 return dvb_ringbuffer_write(buf
, src
, len
);
60 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
61 int non_blocking
, char __user
*buf
,
62 size_t count
, loff_t
*ppos
)
73 dvb_ringbuffer_flush(src
);
77 for (todo
= count
; todo
> 0; todo
-= ret
) {
78 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
83 ret
= wait_event_interruptible(src
->queue
,
84 !dvb_ringbuffer_empty(src
) ||
91 dvb_ringbuffer_flush(src
);
95 avail
= dvb_ringbuffer_avail(src
);
99 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
106 return (count
- todo
) ? (count
- todo
) : ret
;
109 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
111 struct list_head
*head
, *pos
;
113 head
= demux
->get_frontends(demux
);
116 list_for_each(pos
, head
)
117 if (DMX_FE_ENTRY(pos
)->source
== type
)
118 return DMX_FE_ENTRY(pos
);
123 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
125 struct dvb_device
*dvbdev
= file
->private_data
;
126 struct dmxdev
*dmxdev
= dvbdev
->priv
;
127 struct dmx_frontend
*front
;
129 dprintk("function : %s\n", __func__
);
131 if (mutex_lock_interruptible(&dmxdev
->mutex
))
135 mutex_unlock(&dmxdev
->mutex
);
139 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
140 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
141 mutex_unlock(&dmxdev
->mutex
);
146 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
148 if (!dvbdev
->readers
) {
149 mutex_unlock(&dmxdev
->mutex
);
152 mem
= vmalloc(DVR_BUFFER_SIZE
);
154 mutex_unlock(&dmxdev
->mutex
);
157 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
161 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
162 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
164 if (!dmxdev
->demux
->write
) {
165 mutex_unlock(&dmxdev
->mutex
);
169 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
172 mutex_unlock(&dmxdev
->mutex
);
175 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
176 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
179 mutex_unlock(&dmxdev
->mutex
);
183 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
185 struct dvb_device
*dvbdev
= file
->private_data
;
186 struct dmxdev
*dmxdev
= dvbdev
->priv
;
188 mutex_lock(&dmxdev
->mutex
);
190 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
191 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
192 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
193 dmxdev
->dvr_orig_fe
);
195 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
197 if (dmxdev
->dvr_buffer
.data
) {
198 void *mem
= dmxdev
->dvr_buffer
.data
;
200 spin_lock_irq(&dmxdev
->lock
);
201 dmxdev
->dvr_buffer
.data
= NULL
;
202 spin_unlock_irq(&dmxdev
->lock
);
208 if (dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
209 mutex_unlock(&dmxdev
->mutex
);
210 wake_up(&dvbdev
->wait_queue
);
212 mutex_unlock(&dmxdev
->mutex
);
217 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
218 size_t count
, loff_t
*ppos
)
220 struct dvb_device
*dvbdev
= file
->private_data
;
221 struct dmxdev
*dmxdev
= dvbdev
->priv
;
224 if (!dmxdev
->demux
->write
)
226 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
228 if (mutex_lock_interruptible(&dmxdev
->mutex
))
232 mutex_unlock(&dmxdev
->mutex
);
235 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
236 mutex_unlock(&dmxdev
->mutex
);
240 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
243 struct dvb_device
*dvbdev
= file
->private_data
;
244 struct dmxdev
*dmxdev
= dvbdev
->priv
;
249 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
250 file
->f_flags
& O_NONBLOCK
,
254 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
257 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
261 dprintk("function : %s\n", __func__
);
263 if (buf
->size
== size
)
268 newmem
= vmalloc(size
);
274 spin_lock_irq(&dmxdev
->lock
);
278 /* reset and not flush in case the buffer shrinks */
279 dvb_ringbuffer_reset(buf
);
280 spin_unlock_irq(&dmxdev
->lock
);
287 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
288 *dmxdevfilter
, int state
)
290 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
291 dmxdevfilter
->state
= state
;
292 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
295 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
298 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
302 if (buf
->size
== size
)
306 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
309 newmem
= vmalloc(size
);
315 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
319 /* reset and not flush in case the buffer shrinks */
320 dvb_ringbuffer_reset(buf
);
321 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
328 static void dvb_dmxdev_filter_timeout(unsigned long data
)
330 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
332 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
333 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
334 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
335 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
336 wake_up(&dmxdevfilter
->buffer
.queue
);
339 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
341 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
343 del_timer(&dmxdevfilter
->timer
);
345 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
346 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
347 dmxdevfilter
->timer
.expires
=
348 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
349 add_timer(&dmxdevfilter
->timer
);
353 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
354 const u8
*buffer2
, size_t buffer2_len
,
355 struct dmx_section_filter
*filter
,
356 enum dmx_success success
)
358 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
361 if (dmxdevfilter
->buffer
.error
) {
362 wake_up(&dmxdevfilter
->buffer
.queue
);
365 spin_lock(&dmxdevfilter
->dev
->lock
);
366 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
367 spin_unlock(&dmxdevfilter
->dev
->lock
);
370 del_timer(&dmxdevfilter
->timer
);
371 dprintk("dmxdev: section callback %*ph\n", 6, buffer1
);
372 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
374 if (ret
== buffer1_len
) {
375 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
379 dmxdevfilter
->buffer
.error
= ret
;
380 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
381 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
382 spin_unlock(&dmxdevfilter
->dev
->lock
);
383 wake_up(&dmxdevfilter
->buffer
.queue
);
387 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
388 const u8
*buffer2
, size_t buffer2_len
,
389 struct dmx_ts_feed
*feed
,
390 enum dmx_success success
)
392 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
393 struct dvb_ringbuffer
*buffer
;
396 spin_lock(&dmxdevfilter
->dev
->lock
);
397 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
398 spin_unlock(&dmxdevfilter
->dev
->lock
);
402 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
403 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
404 buffer
= &dmxdevfilter
->buffer
;
406 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
408 spin_unlock(&dmxdevfilter
->dev
->lock
);
409 wake_up(&buffer
->queue
);
412 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
413 if (ret
== buffer1_len
)
414 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
417 spin_unlock(&dmxdevfilter
->dev
->lock
);
418 wake_up(&buffer
->queue
);
422 /* stop feed but only mark the specified filter as stopped (state set) */
423 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
425 struct dmxdev_feed
*feed
;
427 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
429 switch (dmxdevfilter
->type
) {
430 case DMXDEV_TYPE_SEC
:
431 del_timer(&dmxdevfilter
->timer
);
432 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
434 case DMXDEV_TYPE_PES
:
435 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
436 feed
->ts
->stop_filtering(feed
->ts
);
444 /* start feed associated with the specified filter */
445 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
447 struct dmxdev_feed
*feed
;
450 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
452 switch (filter
->type
) {
453 case DMXDEV_TYPE_SEC
:
454 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
455 case DMXDEV_TYPE_PES
:
456 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
457 ret
= feed
->ts
->start_filtering(feed
->ts
);
459 dvb_dmxdev_feed_stop(filter
);
471 /* restart section feed if it has filters left associated with it,
472 otherwise release the feed */
473 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
476 struct dmxdev
*dmxdev
= filter
->dev
;
477 u16 pid
= filter
->params
.sec
.pid
;
479 for (i
= 0; i
< dmxdev
->filternum
; i
++)
480 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
481 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
482 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
483 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
487 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
493 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
495 struct dmxdev_feed
*feed
;
496 struct dmx_demux
*demux
;
498 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
501 switch (dmxdevfilter
->type
) {
502 case DMXDEV_TYPE_SEC
:
503 if (!dmxdevfilter
->feed
.sec
)
505 dvb_dmxdev_feed_stop(dmxdevfilter
);
506 if (dmxdevfilter
->filter
.sec
)
507 dmxdevfilter
->feed
.sec
->
508 release_filter(dmxdevfilter
->feed
.sec
,
509 dmxdevfilter
->filter
.sec
);
510 dvb_dmxdev_feed_restart(dmxdevfilter
);
511 dmxdevfilter
->feed
.sec
= NULL
;
513 case DMXDEV_TYPE_PES
:
514 dvb_dmxdev_feed_stop(dmxdevfilter
);
515 demux
= dmxdevfilter
->dev
->demux
;
516 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
517 demux
->release_ts_feed(demux
, feed
->ts
);
522 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
527 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
531 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
533 struct dmxdev_feed
*feed
, *tmp
;
535 /* delete all PIDs */
536 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
537 list_del(&feed
->next
);
541 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
544 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
546 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
549 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
550 dvb_dmxdev_delete_pids(dmxdevfilter
);
552 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
553 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
557 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
558 struct dmxdev_filter
*filter
,
559 struct dmxdev_feed
*feed
)
561 struct timespec timeout
= { 0 };
562 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
566 enum dmx_ts_pes ts_pes
;
567 struct dmx_ts_feed
*tsfeed
;
570 otype
= para
->output
;
572 ts_pes
= para
->pes_type
;
574 if (ts_pes
< DMX_PES_OTHER
)
575 ts_type
= TS_DECODER
;
579 if (otype
== DMX_OUT_TS_TAP
)
580 ts_type
|= TS_PACKET
;
581 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
582 ts_type
|= TS_PACKET
| TS_DEMUX
;
583 else if (otype
== DMX_OUT_TAP
)
584 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
586 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
587 dvb_dmxdev_ts_callback
);
592 tsfeed
->priv
= filter
;
594 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, 32768, timeout
);
596 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
600 ret
= tsfeed
->start_filtering(tsfeed
);
602 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
609 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
611 struct dmxdev
*dmxdev
= filter
->dev
;
612 struct dmxdev_feed
*feed
;
616 if (filter
->state
< DMXDEV_STATE_SET
)
619 if (filter
->state
>= DMXDEV_STATE_GO
)
620 dvb_dmxdev_filter_stop(filter
);
622 if (!filter
->buffer
.data
) {
623 mem
= vmalloc(filter
->buffer
.size
);
626 spin_lock_irq(&filter
->dev
->lock
);
627 filter
->buffer
.data
= mem
;
628 spin_unlock_irq(&filter
->dev
->lock
);
631 dvb_ringbuffer_flush(&filter
->buffer
);
633 switch (filter
->type
) {
634 case DMXDEV_TYPE_SEC
:
636 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
637 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
638 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
644 /* find active filter/feed with same PID */
645 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
646 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
647 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
648 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
649 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
654 /* if no feed found, try to allocate new one */
656 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
658 dvb_dmxdev_section_callback
);
660 printk("DVB (%s): could not alloc feed\n",
665 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
666 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
668 printk("DVB (%s): could not set feed\n",
670 dvb_dmxdev_feed_restart(filter
);
674 dvb_dmxdev_feed_stop(filter
);
677 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
679 dvb_dmxdev_feed_restart(filter
);
680 filter
->feed
.sec
->start_filtering(*secfeed
);
681 dprintk("could not get filter\n");
685 (*secfilter
)->priv
= filter
;
687 memcpy(&((*secfilter
)->filter_value
[3]),
688 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
689 memcpy(&(*secfilter
)->filter_mask
[3],
690 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
691 memcpy(&(*secfilter
)->filter_mode
[3],
692 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
694 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
695 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
696 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
697 (*secfilter
)->filter_mask
[1] = 0;
698 (*secfilter
)->filter_mask
[2] = 0;
702 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
706 dvb_dmxdev_filter_timer(filter
);
709 case DMXDEV_TYPE_PES
:
710 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
711 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
713 dvb_dmxdev_filter_stop(filter
);
722 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
726 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
728 struct dvb_device
*dvbdev
= file
->private_data
;
729 struct dmxdev
*dmxdev
= dvbdev
->priv
;
731 struct dmxdev_filter
*dmxdevfilter
;
736 if (mutex_lock_interruptible(&dmxdev
->mutex
))
739 for (i
= 0; i
< dmxdev
->filternum
; i
++)
740 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
743 if (i
== dmxdev
->filternum
) {
744 mutex_unlock(&dmxdev
->mutex
);
748 dmxdevfilter
= &dmxdev
->filter
[i
];
749 mutex_init(&dmxdevfilter
->mutex
);
750 file
->private_data
= dmxdevfilter
;
752 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
753 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
754 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
755 init_timer(&dmxdevfilter
->timer
);
759 mutex_unlock(&dmxdev
->mutex
);
763 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
764 struct dmxdev_filter
*dmxdevfilter
)
766 mutex_lock(&dmxdev
->mutex
);
767 mutex_lock(&dmxdevfilter
->mutex
);
769 dvb_dmxdev_filter_stop(dmxdevfilter
);
770 dvb_dmxdev_filter_reset(dmxdevfilter
);
772 if (dmxdevfilter
->buffer
.data
) {
773 void *mem
= dmxdevfilter
->buffer
.data
;
775 spin_lock_irq(&dmxdev
->lock
);
776 dmxdevfilter
->buffer
.data
= NULL
;
777 spin_unlock_irq(&dmxdev
->lock
);
781 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
782 wake_up(&dmxdevfilter
->buffer
.queue
);
783 mutex_unlock(&dmxdevfilter
->mutex
);
784 mutex_unlock(&dmxdev
->mutex
);
788 static inline void invert_mode(dmx_filter_t
*filter
)
792 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
793 filter
->mode
[i
] ^= 0xff;
796 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
797 struct dmxdev_filter
*filter
, u16 pid
)
799 struct dmxdev_feed
*feed
;
801 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
802 (filter
->state
< DMXDEV_STATE_SET
))
805 /* only TS packet filters may have multiple PIDs */
806 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
807 (!list_empty(&filter
->feed
.ts
)))
810 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
815 list_add(&feed
->next
, &filter
->feed
.ts
);
817 if (filter
->state
>= DMXDEV_STATE_GO
)
818 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
823 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
824 struct dmxdev_filter
*filter
, u16 pid
)
826 struct dmxdev_feed
*feed
, *tmp
;
828 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
829 (filter
->state
< DMXDEV_STATE_SET
))
832 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
833 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
834 feed
->ts
->stop_filtering(feed
->ts
);
835 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
837 list_del(&feed
->next
);
845 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
846 struct dmxdev_filter
*dmxdevfilter
,
847 struct dmx_sct_filter_params
*params
)
849 dprintk("function : %s, PID=0x%04x, flags=%02x, timeout=%d\n",
850 __func__
, params
->pid
, params
->flags
, params
->timeout
);
852 dvb_dmxdev_filter_stop(dmxdevfilter
);
854 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
855 memcpy(&dmxdevfilter
->params
.sec
,
856 params
, sizeof(struct dmx_sct_filter_params
));
857 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
858 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
860 if (params
->flags
& DMX_IMMEDIATE_START
)
861 return dvb_dmxdev_filter_start(dmxdevfilter
);
866 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
867 struct dmxdev_filter
*dmxdevfilter
,
868 struct dmx_pes_filter_params
*params
)
872 dvb_dmxdev_filter_stop(dmxdevfilter
);
873 dvb_dmxdev_filter_reset(dmxdevfilter
);
875 if ((unsigned)params
->pes_type
> DMX_PES_OTHER
)
878 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
879 memcpy(&dmxdevfilter
->params
, params
,
880 sizeof(struct dmx_pes_filter_params
));
881 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
883 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
885 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
886 dmxdevfilter
->params
.pes
.pid
);
890 if (params
->flags
& DMX_IMMEDIATE_START
)
891 return dvb_dmxdev_filter_start(dmxdevfilter
);
896 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
897 struct file
*file
, char __user
*buf
,
898 size_t count
, loff_t
*ppos
)
903 if (dfil
->todo
<= 0) {
904 hcount
= 3 + dfil
->todo
;
907 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
908 file
->f_flags
& O_NONBLOCK
,
914 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
919 dfil
->todo
-= result
;
922 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
926 if (count
> dfil
->todo
)
928 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
929 file
->f_flags
& O_NONBLOCK
,
933 dfil
->todo
-= result
;
934 return (result
+ done
);
938 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
941 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
944 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
947 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
948 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
950 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
951 file
->f_flags
& O_NONBLOCK
,
954 mutex_unlock(&dmxdevfilter
->mutex
);
958 static int dvb_demux_do_ioctl(struct file
*file
,
959 unsigned int cmd
, void *parg
)
961 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
962 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
963 unsigned long arg
= (unsigned long)parg
;
966 if (mutex_lock_interruptible(&dmxdev
->mutex
))
971 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
972 mutex_unlock(&dmxdev
->mutex
);
975 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
978 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
979 mutex_unlock(&dmxdevfilter
->mutex
);
983 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
984 mutex_unlock(&dmxdev
->mutex
);
987 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
988 mutex_unlock(&dmxdevfilter
->mutex
);
992 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
993 mutex_unlock(&dmxdev
->mutex
);
996 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
997 mutex_unlock(&dmxdevfilter
->mutex
);
1000 case DMX_SET_PES_FILTER
:
1001 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1002 mutex_unlock(&dmxdev
->mutex
);
1003 return -ERESTARTSYS
;
1005 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1006 mutex_unlock(&dmxdevfilter
->mutex
);
1009 case DMX_SET_BUFFER_SIZE
:
1010 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1011 mutex_unlock(&dmxdev
->mutex
);
1012 return -ERESTARTSYS
;
1014 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1015 mutex_unlock(&dmxdevfilter
->mutex
);
1018 case DMX_GET_PES_PIDS
:
1019 if (!dmxdev
->demux
->get_pes_pids
) {
1023 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1027 if (!dmxdev
->demux
->get_caps
) {
1031 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
1034 case DMX_SET_SOURCE
:
1035 if (!dmxdev
->demux
->set_source
) {
1039 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
1043 if (!dmxdev
->demux
->get_stc
) {
1047 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1048 ((struct dmx_stc
*)parg
)->num
,
1049 &((struct dmx_stc
*)parg
)->stc
,
1050 &((struct dmx_stc
*)parg
)->base
);
1054 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1058 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1059 mutex_unlock(&dmxdevfilter
->mutex
);
1062 case DMX_REMOVE_PID
:
1063 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1067 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1068 mutex_unlock(&dmxdevfilter
->mutex
);
1075 mutex_unlock(&dmxdev
->mutex
);
1079 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1082 return dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1085 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1087 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1088 unsigned int mask
= 0;
1090 if ((!dmxdevfilter
) || dmxdevfilter
->dev
->exit
)
1093 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1095 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1096 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1097 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1100 if (dmxdevfilter
->buffer
.error
)
1101 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1103 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1104 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1109 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1111 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1112 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1116 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1118 mutex_lock(&dmxdev
->mutex
);
1119 dmxdev
->dvbdev
->users
--;
1120 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1121 mutex_unlock(&dmxdev
->mutex
);
1122 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1124 mutex_unlock(&dmxdev
->mutex
);
1129 static const struct file_operations dvb_demux_fops
= {
1130 .owner
= THIS_MODULE
,
1131 .read
= dvb_demux_read
,
1132 .unlocked_ioctl
= dvb_demux_ioctl
,
1133 .open
= dvb_demux_open
,
1134 .release
= dvb_demux_release
,
1135 .poll
= dvb_demux_poll
,
1136 .llseek
= default_llseek
,
1139 static const struct dvb_device dvbdev_demux
= {
1143 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1144 .name
= "dvb-demux",
1146 .fops
= &dvb_demux_fops
1149 static int dvb_dvr_do_ioctl(struct file
*file
,
1150 unsigned int cmd
, void *parg
)
1152 struct dvb_device
*dvbdev
= file
->private_data
;
1153 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1154 unsigned long arg
= (unsigned long)parg
;
1157 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1158 return -ERESTARTSYS
;
1161 case DMX_SET_BUFFER_SIZE
:
1162 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1169 mutex_unlock(&dmxdev
->mutex
);
1173 static long dvb_dvr_ioctl(struct file
*file
,
1174 unsigned int cmd
, unsigned long arg
)
1176 return dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1179 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1181 struct dvb_device
*dvbdev
= file
->private_data
;
1182 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1183 unsigned int mask
= 0;
1185 dprintk("function : %s\n", __func__
);
1190 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1192 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1193 if (dmxdev
->dvr_buffer
.error
)
1194 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1196 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1197 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1199 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1204 static const struct file_operations dvb_dvr_fops
= {
1205 .owner
= THIS_MODULE
,
1206 .read
= dvb_dvr_read
,
1207 .write
= dvb_dvr_write
,
1208 .unlocked_ioctl
= dvb_dvr_ioctl
,
1209 .open
= dvb_dvr_open
,
1210 .release
= dvb_dvr_release
,
1211 .poll
= dvb_dvr_poll
,
1212 .llseek
= default_llseek
,
1215 static const struct dvb_device dvbdev_dvr
= {
1219 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1222 .fops
= &dvb_dvr_fops
1224 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1228 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1231 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1232 if (!dmxdev
->filter
)
1235 mutex_init(&dmxdev
->mutex
);
1236 spin_lock_init(&dmxdev
->lock
);
1237 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1238 dmxdev
->filter
[i
].dev
= dmxdev
;
1239 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1240 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1244 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1246 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1247 dmxdev
, DVB_DEVICE_DVR
);
1249 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1254 EXPORT_SYMBOL(dvb_dmxdev_init
);
1256 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1259 if (dmxdev
->dvbdev
->users
> 1) {
1260 wait_event(dmxdev
->dvbdev
->wait_queue
,
1261 dmxdev
->dvbdev
->users
==1);
1263 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1264 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1265 dmxdev
->dvr_dvbdev
->users
==1);
1268 dvb_unregister_device(dmxdev
->dvbdev
);
1269 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1271 vfree(dmxdev
->filter
);
1272 dmxdev
->filter
= NULL
;
1273 dmxdev
->demux
->close(dmxdev
->demux
);
1276 EXPORT_SYMBOL(dvb_dmxdev_release
);