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_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 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
;
251 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
252 file
->f_flags
& O_NONBLOCK
,
256 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
259 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
263 dprintk("function : %s\n", __func__
);
265 if (buf
->size
== size
)
270 newmem
= vmalloc(size
);
276 spin_lock_irq(&dmxdev
->lock
);
280 /* reset and not flush in case the buffer shrinks */
281 dvb_ringbuffer_reset(buf
);
282 spin_unlock_irq(&dmxdev
->lock
);
289 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 *dmxdevfilter
, int state
)
292 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
293 dmxdevfilter
->state
= state
;
294 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
297 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
300 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
304 if (buf
->size
== size
)
308 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
311 newmem
= vmalloc(size
);
317 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
321 /* reset and not flush in case the buffer shrinks */
322 dvb_ringbuffer_reset(buf
);
323 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
330 static void dvb_dmxdev_filter_timeout(unsigned long data
)
332 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
334 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
335 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
336 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
337 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
338 wake_up(&dmxdevfilter
->buffer
.queue
);
341 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
343 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
345 del_timer(&dmxdevfilter
->timer
);
347 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
348 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
349 dmxdevfilter
->timer
.expires
=
350 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
351 add_timer(&dmxdevfilter
->timer
);
355 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
356 const u8
*buffer2
, size_t buffer2_len
,
357 struct dmx_section_filter
*filter
,
358 enum dmx_success success
)
360 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
363 if (dmxdevfilter
->buffer
.error
) {
364 wake_up(&dmxdevfilter
->buffer
.queue
);
367 spin_lock(&dmxdevfilter
->dev
->lock
);
368 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
369 spin_unlock(&dmxdevfilter
->dev
->lock
);
372 del_timer(&dmxdevfilter
->timer
);
373 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
374 buffer1
[0], buffer1
[1],
375 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
376 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
378 if (ret
== buffer1_len
) {
379 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
383 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
384 dmxdevfilter
->buffer
.error
= ret
;
386 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
387 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
388 spin_unlock(&dmxdevfilter
->dev
->lock
);
389 wake_up(&dmxdevfilter
->buffer
.queue
);
393 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
394 const u8
*buffer2
, size_t buffer2_len
,
395 struct dmx_ts_feed
*feed
,
396 enum dmx_success success
)
398 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
399 struct dvb_ringbuffer
*buffer
;
402 spin_lock(&dmxdevfilter
->dev
->lock
);
403 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
404 spin_unlock(&dmxdevfilter
->dev
->lock
);
408 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
409 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
410 buffer
= &dmxdevfilter
->buffer
;
412 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
414 spin_unlock(&dmxdevfilter
->dev
->lock
);
415 wake_up(&buffer
->queue
);
418 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
419 if (ret
== buffer1_len
)
420 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
422 dvb_ringbuffer_flush(buffer
);
425 spin_unlock(&dmxdevfilter
->dev
->lock
);
426 wake_up(&buffer
->queue
);
430 /* stop feed but only mark the specified filter as stopped (state set) */
431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
433 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
435 switch (dmxdevfilter
->type
) {
436 case DMXDEV_TYPE_SEC
:
437 del_timer(&dmxdevfilter
->timer
);
438 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
440 case DMXDEV_TYPE_PES
:
441 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
449 /* start feed associated with the specified filter */
450 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
452 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
454 switch (filter
->type
) {
455 case DMXDEV_TYPE_SEC
:
456 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
457 case DMXDEV_TYPE_PES
:
458 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
466 /* restart section feed if it has filters left associated with it,
467 otherwise release the feed */
468 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
471 struct dmxdev
*dmxdev
= filter
->dev
;
472 u16 pid
= filter
->params
.sec
.pid
;
474 for (i
= 0; i
< dmxdev
->filternum
; i
++)
475 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
476 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
477 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
478 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
482 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
488 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
490 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
493 switch (dmxdevfilter
->type
) {
494 case DMXDEV_TYPE_SEC
:
495 if (!dmxdevfilter
->feed
.sec
)
497 dvb_dmxdev_feed_stop(dmxdevfilter
);
498 if (dmxdevfilter
->filter
.sec
)
499 dmxdevfilter
->feed
.sec
->
500 release_filter(dmxdevfilter
->feed
.sec
,
501 dmxdevfilter
->filter
.sec
);
502 dvb_dmxdev_feed_restart(dmxdevfilter
);
503 dmxdevfilter
->feed
.sec
= NULL
;
505 case DMXDEV_TYPE_PES
:
506 if (!dmxdevfilter
->feed
.ts
)
508 dvb_dmxdev_feed_stop(dmxdevfilter
);
509 dmxdevfilter
->dev
->demux
->
510 release_ts_feed(dmxdevfilter
->dev
->demux
,
511 dmxdevfilter
->feed
.ts
);
512 dmxdevfilter
->feed
.ts
= NULL
;
515 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
520 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
524 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
526 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
529 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
530 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
534 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
536 struct dmxdev
*dmxdev
= filter
->dev
;
540 if (filter
->state
< DMXDEV_STATE_SET
)
543 if (filter
->state
>= DMXDEV_STATE_GO
)
544 dvb_dmxdev_filter_stop(filter
);
546 if (!filter
->buffer
.data
) {
547 mem
= vmalloc(filter
->buffer
.size
);
550 spin_lock_irq(&filter
->dev
->lock
);
551 filter
->buffer
.data
= mem
;
552 spin_unlock_irq(&filter
->dev
->lock
);
555 dvb_ringbuffer_flush(&filter
->buffer
);
557 switch (filter
->type
) {
558 case DMXDEV_TYPE_SEC
:
560 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
561 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
562 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
568 /* find active filter/feed with same PID */
569 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
570 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
571 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
572 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
573 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
578 /* if no feed found, try to allocate new one */
580 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
582 dvb_dmxdev_section_callback
);
584 printk("DVB (%s): could not alloc feed\n",
589 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
590 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
592 printk("DVB (%s): could not set feed\n",
594 dvb_dmxdev_feed_restart(filter
);
598 dvb_dmxdev_feed_stop(filter
);
601 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
603 dvb_dmxdev_feed_restart(filter
);
604 filter
->feed
.sec
->start_filtering(*secfeed
);
605 dprintk("could not get filter\n");
609 (*secfilter
)->priv
= filter
;
611 memcpy(&((*secfilter
)->filter_value
[3]),
612 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
613 memcpy(&(*secfilter
)->filter_mask
[3],
614 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
615 memcpy(&(*secfilter
)->filter_mode
[3],
616 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
618 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
619 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
620 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
621 (*secfilter
)->filter_mask
[1] = 0;
622 (*secfilter
)->filter_mask
[2] = 0;
626 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
630 dvb_dmxdev_filter_timer(filter
);
633 case DMXDEV_TYPE_PES
:
635 struct timespec timeout
= { 0 };
636 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
639 enum dmx_ts_pes ts_pes
;
640 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
642 filter
->feed
.ts
= NULL
;
643 otype
= para
->output
;
645 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
647 if (ts_pes
< DMX_PES_OTHER
)
648 ts_type
= TS_DECODER
;
652 if (otype
== DMX_OUT_TS_TAP
)
653 ts_type
|= TS_PACKET
;
654 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
655 ts_type
|= TS_PACKET
| TS_DEMUX
;
656 else if (otype
== DMX_OUT_TAP
)
657 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
659 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
661 dvb_dmxdev_ts_callback
);
665 (*tsfeed
)->priv
= filter
;
667 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
670 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
675 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
677 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
688 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
692 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
694 struct dvb_device
*dvbdev
= file
->private_data
;
695 struct dmxdev
*dmxdev
= dvbdev
->priv
;
697 struct dmxdev_filter
*dmxdevfilter
;
702 if (mutex_lock_interruptible(&dmxdev
->mutex
))
705 for (i
= 0; i
< dmxdev
->filternum
; i
++)
706 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
709 if (i
== dmxdev
->filternum
) {
710 mutex_unlock(&dmxdev
->mutex
);
714 dmxdevfilter
= &dmxdev
->filter
[i
];
715 mutex_init(&dmxdevfilter
->mutex
);
716 file
->private_data
= dmxdevfilter
;
718 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
719 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
720 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
721 dmxdevfilter
->feed
.ts
= NULL
;
722 init_timer(&dmxdevfilter
->timer
);
726 mutex_unlock(&dmxdev
->mutex
);
730 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
731 struct dmxdev_filter
*dmxdevfilter
)
733 mutex_lock(&dmxdev
->mutex
);
734 mutex_lock(&dmxdevfilter
->mutex
);
736 dvb_dmxdev_filter_stop(dmxdevfilter
);
737 dvb_dmxdev_filter_reset(dmxdevfilter
);
739 if (dmxdevfilter
->buffer
.data
) {
740 void *mem
= dmxdevfilter
->buffer
.data
;
742 spin_lock_irq(&dmxdev
->lock
);
743 dmxdevfilter
->buffer
.data
= NULL
;
744 spin_unlock_irq(&dmxdev
->lock
);
748 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
749 wake_up(&dmxdevfilter
->buffer
.queue
);
750 mutex_unlock(&dmxdevfilter
->mutex
);
751 mutex_unlock(&dmxdev
->mutex
);
755 static inline void invert_mode(dmx_filter_t
*filter
)
759 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
760 filter
->mode
[i
] ^= 0xff;
763 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
764 struct dmxdev_filter
*dmxdevfilter
,
765 struct dmx_sct_filter_params
*params
)
767 dprintk("function : %s\n", __func__
);
769 dvb_dmxdev_filter_stop(dmxdevfilter
);
771 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
772 memcpy(&dmxdevfilter
->params
.sec
,
773 params
, sizeof(struct dmx_sct_filter_params
));
774 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
775 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
777 if (params
->flags
& DMX_IMMEDIATE_START
)
778 return dvb_dmxdev_filter_start(dmxdevfilter
);
783 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
784 struct dmxdev_filter
*dmxdevfilter
,
785 struct dmx_pes_filter_params
*params
)
787 dvb_dmxdev_filter_stop(dmxdevfilter
);
789 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
792 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
793 memcpy(&dmxdevfilter
->params
, params
,
794 sizeof(struct dmx_pes_filter_params
));
796 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
798 if (params
->flags
& DMX_IMMEDIATE_START
)
799 return dvb_dmxdev_filter_start(dmxdevfilter
);
804 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
805 struct file
*file
, char __user
*buf
,
806 size_t count
, loff_t
*ppos
)
811 if (dfil
->todo
<= 0) {
812 hcount
= 3 + dfil
->todo
;
815 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
816 file
->f_flags
& O_NONBLOCK
,
822 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
827 dfil
->todo
-= result
;
830 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
834 if (count
> dfil
->todo
)
836 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
837 file
->f_flags
& O_NONBLOCK
,
841 dfil
->todo
-= result
;
842 return (result
+ done
);
846 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
849 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
852 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
855 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
856 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
858 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
859 file
->f_flags
& O_NONBLOCK
,
862 mutex_unlock(&dmxdevfilter
->mutex
);
866 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
867 unsigned int cmd
, void *parg
)
869 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
870 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
871 unsigned long arg
= (unsigned long)parg
;
874 if (mutex_lock_interruptible(&dmxdev
->mutex
))
879 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
880 mutex_unlock(&dmxdev
->mutex
);
883 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
886 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
887 mutex_unlock(&dmxdevfilter
->mutex
);
891 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
892 mutex_unlock(&dmxdev
->mutex
);
895 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
896 mutex_unlock(&dmxdevfilter
->mutex
);
900 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
901 mutex_unlock(&dmxdev
->mutex
);
904 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
905 mutex_unlock(&dmxdevfilter
->mutex
);
908 case DMX_SET_PES_FILTER
:
909 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
910 mutex_unlock(&dmxdev
->mutex
);
913 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
914 mutex_unlock(&dmxdevfilter
->mutex
);
917 case DMX_SET_BUFFER_SIZE
:
918 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
919 mutex_unlock(&dmxdev
->mutex
);
922 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
923 mutex_unlock(&dmxdevfilter
->mutex
);
926 case DMX_GET_PES_PIDS
:
927 if (!dmxdev
->demux
->get_pes_pids
) {
931 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
935 if (!dmxdev
->demux
->get_caps
) {
939 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
943 if (!dmxdev
->demux
->set_source
) {
947 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
951 if (!dmxdev
->demux
->get_stc
) {
955 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
956 ((struct dmx_stc
*)parg
)->num
,
957 &((struct dmx_stc
*)parg
)->stc
,
958 &((struct dmx_stc
*)parg
)->base
);
965 mutex_unlock(&dmxdev
->mutex
);
969 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
970 unsigned int cmd
, unsigned long arg
)
972 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
975 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
977 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
978 unsigned int mask
= 0;
983 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
985 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
986 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
987 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
990 if (dmxdevfilter
->buffer
.error
)
991 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
993 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
994 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
999 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1001 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1002 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1006 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1008 mutex_lock(&dmxdev
->mutex
);
1009 dmxdev
->dvbdev
->users
--;
1010 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1011 fops_put(file
->f_op
);
1013 mutex_unlock(&dmxdev
->mutex
);
1014 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1016 mutex_unlock(&dmxdev
->mutex
);
1021 static const struct file_operations dvb_demux_fops
= {
1022 .owner
= THIS_MODULE
,
1023 .read
= dvb_demux_read
,
1024 .ioctl
= dvb_demux_ioctl
,
1025 .open
= dvb_demux_open
,
1026 .release
= dvb_demux_release
,
1027 .poll
= dvb_demux_poll
,
1030 static struct dvb_device dvbdev_demux
= {
1034 .fops
= &dvb_demux_fops
1037 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
1038 unsigned int cmd
, void *parg
)
1040 struct dvb_device
*dvbdev
= file
->private_data
;
1041 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1042 unsigned long arg
= (unsigned long)parg
;
1045 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1046 return -ERESTARTSYS
;
1049 case DMX_SET_BUFFER_SIZE
:
1050 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1057 mutex_unlock(&dmxdev
->mutex
);
1061 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1062 unsigned int cmd
, unsigned long arg
)
1064 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1067 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1069 struct dvb_device
*dvbdev
= file
->private_data
;
1070 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1071 unsigned int mask
= 0;
1073 dprintk("function : %s\n", __func__
);
1075 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1077 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1078 if (dmxdev
->dvr_buffer
.error
)
1079 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1081 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1082 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1084 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1089 static struct file_operations dvb_dvr_fops
= {
1090 .owner
= THIS_MODULE
,
1091 .read
= dvb_dvr_read
,
1092 .write
= dvb_dvr_write
,
1093 .ioctl
= dvb_dvr_ioctl
,
1094 .open
= dvb_dvr_open
,
1095 .release
= dvb_dvr_release
,
1096 .poll
= dvb_dvr_poll
,
1099 static struct dvb_device dvbdev_dvr
= {
1103 .fops
= &dvb_dvr_fops
1106 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1110 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1113 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1114 if (!dmxdev
->filter
)
1117 mutex_init(&dmxdev
->mutex
);
1118 spin_lock_init(&dmxdev
->lock
);
1119 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1120 dmxdev
->filter
[i
].dev
= dmxdev
;
1121 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1122 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1126 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1128 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1129 dmxdev
, DVB_DEVICE_DVR
);
1131 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1136 EXPORT_SYMBOL(dvb_dmxdev_init
);
1138 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1141 if (dmxdev
->dvbdev
->users
> 1) {
1142 wait_event(dmxdev
->dvbdev
->wait_queue
,
1143 dmxdev
->dvbdev
->users
==1);
1145 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1146 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1147 dmxdev
->dvr_dvbdev
->users
==1);
1150 dvb_unregister_device(dmxdev
->dvbdev
);
1151 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1153 vfree(dmxdev
->filter
);
1154 dmxdev
->filter
= NULL
;
1155 dmxdev
->demux
->close(dmxdev
->demux
);
1158 EXPORT_SYMBOL(dvb_dmxdev_release
);