2 * dmxdev.c - DVB demultiplexer device
4 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
5 * for convergence integrated media GmbH
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License
9 * as published by the Free Software Foundation; either version 2.1
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <linux/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
28 #include <linux/ioctl.h>
29 #include <linux/wait.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.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(src
, (u8
*)buf
, avail
, 1);
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 fops_put(file
->f_op
);
211 mutex_unlock(&dmxdev
->mutex
);
212 wake_up(&dvbdev
->wait_queue
);
214 mutex_unlock(&dmxdev
->mutex
);
219 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
220 size_t count
, loff_t
*ppos
)
222 struct dvb_device
*dvbdev
= file
->private_data
;
223 struct dmxdev
*dmxdev
= dvbdev
->priv
;
226 if (!dmxdev
->demux
->write
)
228 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
230 if (mutex_lock_interruptible(&dmxdev
->mutex
))
234 mutex_unlock(&dmxdev
->mutex
);
237 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
238 mutex_unlock(&dmxdev
->mutex
);
242 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
245 struct dvb_device
*dvbdev
= file
->private_data
;
246 struct dmxdev
*dmxdev
= dvbdev
->priv
;
250 mutex_unlock(&dmxdev
->mutex
);
254 //mutex_lock(&dmxdev->mutex);
255 ret
= dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
256 file
->f_flags
& O_NONBLOCK
,
258 //mutex_unlock(&dmxdev->mutex);
262 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
265 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
269 dprintk("function : %s\n", __func__
);
271 if (buf
->size
== size
)
276 newmem
= vmalloc(size
);
282 spin_lock_irq(&dmxdev
->lock
);
286 /* reset and not flush in case the buffer shrinks */
287 dvb_ringbuffer_reset(buf
);
288 spin_unlock_irq(&dmxdev
->lock
);
295 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
296 *dmxdevfilter
, int state
)
298 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
299 dmxdevfilter
->state
= state
;
300 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
303 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
306 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
310 if (buf
->size
== size
)
314 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
317 newmem
= vmalloc(size
);
323 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
327 /* reset and not flush in case the buffer shrinks */
328 dvb_ringbuffer_reset(buf
);
329 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
336 static void dvb_dmxdev_filter_timeout(unsigned long data
)
338 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
340 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
341 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
342 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
343 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
344 wake_up(&dmxdevfilter
->buffer
.queue
);
347 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
349 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
351 del_timer(&dmxdevfilter
->timer
);
353 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
354 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
355 dmxdevfilter
->timer
.expires
=
356 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
357 add_timer(&dmxdevfilter
->timer
);
361 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
362 const u8
*buffer2
, size_t buffer2_len
,
363 struct dmx_section_filter
*filter
,
364 enum dmx_success success
)
366 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
369 if (dmxdevfilter
->buffer
.error
) {
370 wake_up(&dmxdevfilter
->buffer
.queue
);
373 spin_lock(&dmxdevfilter
->dev
->lock
);
374 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
375 spin_unlock(&dmxdevfilter
->dev
->lock
);
378 del_timer(&dmxdevfilter
->timer
);
379 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
380 buffer1
[0], buffer1
[1],
381 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
382 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
384 if (ret
== buffer1_len
) {
385 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
389 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
390 dmxdevfilter
->buffer
.error
= ret
;
392 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
393 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
394 spin_unlock(&dmxdevfilter
->dev
->lock
);
395 wake_up(&dmxdevfilter
->buffer
.queue
);
399 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
400 const u8
*buffer2
, size_t buffer2_len
,
401 struct dmx_ts_feed
*feed
,
402 enum dmx_success success
)
404 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
405 struct dvb_ringbuffer
*buffer
;
408 spin_lock(&dmxdevfilter
->dev
->lock
);
409 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
410 spin_unlock(&dmxdevfilter
->dev
->lock
);
414 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
415 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
416 buffer
= &dmxdevfilter
->buffer
;
418 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
420 spin_unlock(&dmxdevfilter
->dev
->lock
);
421 wake_up(&buffer
->queue
);
424 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
425 if (ret
== buffer1_len
)
426 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
428 dvb_ringbuffer_flush(buffer
);
431 spin_unlock(&dmxdevfilter
->dev
->lock
);
432 wake_up(&buffer
->queue
);
436 /* stop feed but only mark the specified filter as stopped (state set) */
437 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
439 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
441 switch (dmxdevfilter
->type
) {
442 case DMXDEV_TYPE_SEC
:
443 del_timer(&dmxdevfilter
->timer
);
444 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
446 case DMXDEV_TYPE_PES
:
447 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
455 /* start feed associated with the specified filter */
456 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
458 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
460 switch (filter
->type
) {
461 case DMXDEV_TYPE_SEC
:
462 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
463 case DMXDEV_TYPE_PES
:
464 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
472 /* restart section feed if it has filters left associated with it,
473 otherwise release the feed */
474 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
477 struct dmxdev
*dmxdev
= filter
->dev
;
478 u16 pid
= filter
->params
.sec
.pid
;
480 for (i
= 0; i
< dmxdev
->filternum
; i
++)
481 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
482 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
483 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
484 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
488 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
494 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
496 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
499 switch (dmxdevfilter
->type
) {
500 case DMXDEV_TYPE_SEC
:
501 if (!dmxdevfilter
->feed
.sec
)
503 dvb_dmxdev_feed_stop(dmxdevfilter
);
504 if (dmxdevfilter
->filter
.sec
)
505 dmxdevfilter
->feed
.sec
->
506 release_filter(dmxdevfilter
->feed
.sec
,
507 dmxdevfilter
->filter
.sec
);
508 dvb_dmxdev_feed_restart(dmxdevfilter
);
509 dmxdevfilter
->feed
.sec
= NULL
;
511 case DMXDEV_TYPE_PES
:
512 if (!dmxdevfilter
->feed
.ts
)
514 dvb_dmxdev_feed_stop(dmxdevfilter
);
515 dmxdevfilter
->dev
->demux
->
516 release_ts_feed(dmxdevfilter
->dev
->demux
,
517 dmxdevfilter
->feed
.ts
);
518 dmxdevfilter
->feed
.ts
= NULL
;
521 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
526 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
530 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
532 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
535 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
536 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
540 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
542 struct dmxdev
*dmxdev
= filter
->dev
;
546 if (filter
->state
< DMXDEV_STATE_SET
)
549 if (filter
->state
>= DMXDEV_STATE_GO
)
550 dvb_dmxdev_filter_stop(filter
);
552 if (!filter
->buffer
.data
) {
553 mem
= vmalloc(filter
->buffer
.size
);
556 spin_lock_irq(&filter
->dev
->lock
);
557 filter
->buffer
.data
= mem
;
558 spin_unlock_irq(&filter
->dev
->lock
);
561 dvb_ringbuffer_flush(&filter
->buffer
);
563 switch (filter
->type
) {
564 case DMXDEV_TYPE_SEC
:
566 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
567 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
568 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
574 /* find active filter/feed with same PID */
575 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
576 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
577 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
578 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
579 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
584 /* if no feed found, try to allocate new one */
586 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
588 dvb_dmxdev_section_callback
);
590 printk("DVB (%s): could not alloc feed\n",
595 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
596 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
598 printk("DVB (%s): could not set feed\n",
600 dvb_dmxdev_feed_restart(filter
);
604 dvb_dmxdev_feed_stop(filter
);
607 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
609 dvb_dmxdev_feed_restart(filter
);
610 filter
->feed
.sec
->start_filtering(*secfeed
);
611 dprintk("could not get filter\n");
615 (*secfilter
)->priv
= filter
;
617 memcpy(&((*secfilter
)->filter_value
[3]),
618 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
619 memcpy(&(*secfilter
)->filter_mask
[3],
620 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
621 memcpy(&(*secfilter
)->filter_mode
[3],
622 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
624 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
625 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
626 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
627 (*secfilter
)->filter_mask
[1] = 0;
628 (*secfilter
)->filter_mask
[2] = 0;
632 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
636 dvb_dmxdev_filter_timer(filter
);
639 case DMXDEV_TYPE_PES
:
641 struct timespec timeout
= { 0 };
642 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
646 enum dmx_ts_pes ts_pes
;
647 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
649 filter
->feed
.ts
= NULL
;
650 otype
= para
->output
;
652 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
654 if (ts_pes
< DMX_PES_OTHER
)
655 ts_type
= TS_DECODER
;
659 if (otype
== DMX_OUT_TS_TAP
)
660 ts_type
|= TS_PACKET
;
661 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
662 ts_type
|= TS_PACKET
| TS_DEMUX
;
663 else if (otype
== DMX_OUT_TAP
)
664 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
666 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
668 dvb_dmxdev_ts_callback
);
672 (*tsfeed
)->priv
= filter
;
674 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
677 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
682 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
684 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
695 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
699 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
701 struct dvb_device
*dvbdev
= file
->private_data
;
702 struct dmxdev
*dmxdev
= dvbdev
->priv
;
704 struct dmxdev_filter
*dmxdevfilter
;
709 if (mutex_lock_interruptible(&dmxdev
->mutex
))
712 for (i
= 0; i
< dmxdev
->filternum
; i
++)
713 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
716 if (i
== dmxdev
->filternum
) {
717 mutex_unlock(&dmxdev
->mutex
);
721 dmxdevfilter
= &dmxdev
->filter
[i
];
722 mutex_init(&dmxdevfilter
->mutex
);
723 file
->private_data
= dmxdevfilter
;
725 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
726 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
727 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
728 dmxdevfilter
->feed
.ts
= NULL
;
729 init_timer(&dmxdevfilter
->timer
);
733 mutex_unlock(&dmxdev
->mutex
);
737 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
738 struct dmxdev_filter
*dmxdevfilter
)
740 mutex_lock(&dmxdev
->mutex
);
741 mutex_lock(&dmxdevfilter
->mutex
);
743 dvb_dmxdev_filter_stop(dmxdevfilter
);
744 dvb_dmxdev_filter_reset(dmxdevfilter
);
746 if (dmxdevfilter
->buffer
.data
) {
747 void *mem
= dmxdevfilter
->buffer
.data
;
749 spin_lock_irq(&dmxdev
->lock
);
750 dmxdevfilter
->buffer
.data
= NULL
;
751 spin_unlock_irq(&dmxdev
->lock
);
755 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
756 wake_up(&dmxdevfilter
->buffer
.queue
);
757 mutex_unlock(&dmxdevfilter
->mutex
);
758 mutex_unlock(&dmxdev
->mutex
);
762 static inline void invert_mode(dmx_filter_t
*filter
)
766 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
767 filter
->mode
[i
] ^= 0xff;
770 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
771 struct dmxdev_filter
*dmxdevfilter
,
772 struct dmx_sct_filter_params
*params
)
774 dprintk("function : %s\n", __func__
);
776 dvb_dmxdev_filter_stop(dmxdevfilter
);
778 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
779 memcpy(&dmxdevfilter
->params
.sec
,
780 params
, sizeof(struct dmx_sct_filter_params
));
781 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
782 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
784 if (params
->flags
& DMX_IMMEDIATE_START
)
785 return dvb_dmxdev_filter_start(dmxdevfilter
);
790 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
791 struct dmxdev_filter
*dmxdevfilter
,
792 struct dmx_pes_filter_params
*params
)
794 dvb_dmxdev_filter_stop(dmxdevfilter
);
796 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
799 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
800 memcpy(&dmxdevfilter
->params
, params
,
801 sizeof(struct dmx_pes_filter_params
));
803 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
805 if (params
->flags
& DMX_IMMEDIATE_START
)
806 return dvb_dmxdev_filter_start(dmxdevfilter
);
811 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
812 struct file
*file
, char __user
*buf
,
813 size_t count
, loff_t
*ppos
)
818 if (dfil
->todo
<= 0) {
819 hcount
= 3 + dfil
->todo
;
822 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
823 file
->f_flags
& O_NONBLOCK
,
829 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
834 dfil
->todo
-= result
;
837 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
841 if (count
> dfil
->todo
)
843 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
844 file
->f_flags
& O_NONBLOCK
,
848 dfil
->todo
-= result
;
849 return (result
+ done
);
853 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
856 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
859 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
862 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
863 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
865 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
866 file
->f_flags
& O_NONBLOCK
,
869 mutex_unlock(&dmxdevfilter
->mutex
);
873 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
874 unsigned int cmd
, void *parg
)
876 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
877 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
878 unsigned long arg
= (unsigned long)parg
;
881 if (mutex_lock_interruptible(&dmxdev
->mutex
))
886 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
887 mutex_unlock(&dmxdev
->mutex
);
890 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
893 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
894 mutex_unlock(&dmxdevfilter
->mutex
);
898 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
899 mutex_unlock(&dmxdev
->mutex
);
902 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
903 mutex_unlock(&dmxdevfilter
->mutex
);
907 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
908 mutex_unlock(&dmxdev
->mutex
);
911 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
912 mutex_unlock(&dmxdevfilter
->mutex
);
915 case DMX_SET_PES_FILTER
:
916 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
917 mutex_unlock(&dmxdev
->mutex
);
920 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
921 mutex_unlock(&dmxdevfilter
->mutex
);
924 case DMX_SET_BUFFER_SIZE
:
925 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
926 mutex_unlock(&dmxdev
->mutex
);
929 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
930 mutex_unlock(&dmxdevfilter
->mutex
);
933 case DMX_GET_PES_PIDS
:
934 if (!dmxdev
->demux
->get_pes_pids
) {
938 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
942 if (!dmxdev
->demux
->get_caps
) {
946 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
950 if (!dmxdev
->demux
->set_source
) {
954 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
958 if (!dmxdev
->demux
->get_stc
) {
962 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
963 ((struct dmx_stc
*)parg
)->num
,
964 &((struct dmx_stc
*)parg
)->stc
,
965 &((struct dmx_stc
*)parg
)->base
);
972 mutex_unlock(&dmxdev
->mutex
);
976 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
977 unsigned int cmd
, unsigned long arg
)
979 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
982 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
984 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
985 unsigned int mask
= 0;
990 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
992 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
993 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
994 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
997 if (dmxdevfilter
->buffer
.error
)
998 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1000 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1001 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1006 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1008 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1009 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1013 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1015 mutex_lock(&dmxdev
->mutex
);
1016 dmxdev
->dvbdev
->users
--;
1017 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1018 fops_put(file
->f_op
);
1020 mutex_unlock(&dmxdev
->mutex
);
1021 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1023 mutex_unlock(&dmxdev
->mutex
);
1028 static struct file_operations dvb_demux_fops
= {
1029 .owner
= THIS_MODULE
,
1030 .read
= dvb_demux_read
,
1031 .ioctl
= dvb_demux_ioctl
,
1032 .open
= dvb_demux_open
,
1033 .release
= dvb_demux_release
,
1034 .poll
= dvb_demux_poll
,
1037 static struct dvb_device dvbdev_demux
= {
1041 .fops
= &dvb_demux_fops
1044 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
1045 unsigned int cmd
, void *parg
)
1047 struct dvb_device
*dvbdev
= file
->private_data
;
1048 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1049 unsigned long arg
= (unsigned long)parg
;
1052 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1053 return -ERESTARTSYS
;
1056 case DMX_SET_BUFFER_SIZE
:
1057 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1064 mutex_unlock(&dmxdev
->mutex
);
1068 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1069 unsigned int cmd
, unsigned long arg
)
1071 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1074 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1076 struct dvb_device
*dvbdev
= file
->private_data
;
1077 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1078 unsigned int mask
= 0;
1080 dprintk("function : %s\n", __func__
);
1082 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1084 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1085 if (dmxdev
->dvr_buffer
.error
)
1086 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1088 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1089 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1091 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1096 static struct file_operations dvb_dvr_fops
= {
1097 .owner
= THIS_MODULE
,
1098 .read
= dvb_dvr_read
,
1099 .write
= dvb_dvr_write
,
1100 .ioctl
= dvb_dvr_ioctl
,
1101 .open
= dvb_dvr_open
,
1102 .release
= dvb_dvr_release
,
1103 .poll
= dvb_dvr_poll
,
1106 static struct dvb_device dvbdev_dvr
= {
1110 .fops
= &dvb_dvr_fops
1113 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1117 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1120 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1121 if (!dmxdev
->filter
)
1124 mutex_init(&dmxdev
->mutex
);
1125 spin_lock_init(&dmxdev
->lock
);
1126 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1127 dmxdev
->filter
[i
].dev
= dmxdev
;
1128 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1129 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1133 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1135 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1136 dmxdev
, DVB_DEVICE_DVR
);
1138 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1143 EXPORT_SYMBOL(dvb_dmxdev_init
);
1145 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1148 if (dmxdev
->dvbdev
->users
> 1) {
1149 wait_event(dmxdev
->dvbdev
->wait_queue
,
1150 dmxdev
->dvbdev
->users
==1);
1152 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1153 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1154 dmxdev
->dvr_dvbdev
->users
==1);
1157 dvb_unregister_device(dmxdev
->dvbdev
);
1158 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1160 vfree(dmxdev
->filter
);
1161 dmxdev
->filter
= NULL
;
1162 dmxdev
->demux
->close(dmxdev
->demux
);
1165 EXPORT_SYMBOL(dvb_dmxdev_release
);