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", __FUNCTION__
);
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 inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
263 *dmxdevfilter
, int state
)
265 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
266 dmxdevfilter
->state
= state
;
267 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
270 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
273 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
276 if (buf
->size
== size
)
278 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
280 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
284 dvb_ringbuffer_flush(buf
);
285 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
289 mem
= vmalloc(dmxdevfilter
->buffer
.size
);
292 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
294 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
299 static void dvb_dmxdev_filter_timeout(unsigned long data
)
301 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
303 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
304 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
305 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
306 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
307 wake_up(&dmxdevfilter
->buffer
.queue
);
310 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
312 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
314 del_timer(&dmxdevfilter
->timer
);
316 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
317 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
318 dmxdevfilter
->timer
.expires
=
319 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
320 add_timer(&dmxdevfilter
->timer
);
324 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
325 const u8
*buffer2
, size_t buffer2_len
,
326 struct dmx_section_filter
*filter
,
327 enum dmx_success success
)
329 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
332 if (dmxdevfilter
->buffer
.error
) {
333 wake_up(&dmxdevfilter
->buffer
.queue
);
336 spin_lock(&dmxdevfilter
->dev
->lock
);
337 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
338 spin_unlock(&dmxdevfilter
->dev
->lock
);
341 del_timer(&dmxdevfilter
->timer
);
342 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
343 buffer1
[0], buffer1
[1],
344 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
345 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
347 if (ret
== buffer1_len
) {
348 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
352 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
353 dmxdevfilter
->buffer
.error
= ret
;
355 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
356 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
357 spin_unlock(&dmxdevfilter
->dev
->lock
);
358 wake_up(&dmxdevfilter
->buffer
.queue
);
362 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
363 const u8
*buffer2
, size_t buffer2_len
,
364 struct dmx_ts_feed
*feed
,
365 enum dmx_success success
)
367 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
368 struct dvb_ringbuffer
*buffer
;
371 spin_lock(&dmxdevfilter
->dev
->lock
);
372 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
373 spin_unlock(&dmxdevfilter
->dev
->lock
);
377 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
)
378 buffer
= &dmxdevfilter
->buffer
;
380 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
382 spin_unlock(&dmxdevfilter
->dev
->lock
);
383 wake_up(&buffer
->queue
);
386 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
387 if (ret
== buffer1_len
)
388 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
390 dvb_ringbuffer_flush(buffer
);
393 spin_unlock(&dmxdevfilter
->dev
->lock
);
394 wake_up(&buffer
->queue
);
398 /* stop feed but only mark the specified filter as stopped (state set) */
399 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
401 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
403 switch (dmxdevfilter
->type
) {
404 case DMXDEV_TYPE_SEC
:
405 del_timer(&dmxdevfilter
->timer
);
406 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
408 case DMXDEV_TYPE_PES
:
409 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
417 /* start feed associated with the specified filter */
418 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
420 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
422 switch (filter
->type
) {
423 case DMXDEV_TYPE_SEC
:
424 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
425 case DMXDEV_TYPE_PES
:
426 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
434 /* restart section feed if it has filters left associated with it,
435 otherwise release the feed */
436 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
439 struct dmxdev
*dmxdev
= filter
->dev
;
440 u16 pid
= filter
->params
.sec
.pid
;
442 for (i
= 0; i
< dmxdev
->filternum
; i
++)
443 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
444 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
445 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
446 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
450 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
456 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
458 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
461 switch (dmxdevfilter
->type
) {
462 case DMXDEV_TYPE_SEC
:
463 if (!dmxdevfilter
->feed
.sec
)
465 dvb_dmxdev_feed_stop(dmxdevfilter
);
466 if (dmxdevfilter
->filter
.sec
)
467 dmxdevfilter
->feed
.sec
->
468 release_filter(dmxdevfilter
->feed
.sec
,
469 dmxdevfilter
->filter
.sec
);
470 dvb_dmxdev_feed_restart(dmxdevfilter
);
471 dmxdevfilter
->feed
.sec
= NULL
;
473 case DMXDEV_TYPE_PES
:
474 if (!dmxdevfilter
->feed
.ts
)
476 dvb_dmxdev_feed_stop(dmxdevfilter
);
477 dmxdevfilter
->dev
->demux
->
478 release_ts_feed(dmxdevfilter
->dev
->demux
,
479 dmxdevfilter
->feed
.ts
);
480 dmxdevfilter
->feed
.ts
= NULL
;
483 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
488 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
492 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
494 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
497 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
498 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
502 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
504 struct dmxdev
*dmxdev
= filter
->dev
;
508 if (filter
->state
< DMXDEV_STATE_SET
)
511 if (filter
->state
>= DMXDEV_STATE_GO
)
512 dvb_dmxdev_filter_stop(filter
);
514 if (!filter
->buffer
.data
) {
515 mem
= vmalloc(filter
->buffer
.size
);
518 spin_lock_irq(&filter
->dev
->lock
);
519 filter
->buffer
.data
= mem
;
520 spin_unlock_irq(&filter
->dev
->lock
);
523 dvb_ringbuffer_flush(&filter
->buffer
);
525 switch (filter
->type
) {
526 case DMXDEV_TYPE_SEC
:
528 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
529 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
530 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
536 /* find active filter/feed with same PID */
537 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
538 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
539 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
540 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
541 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
546 /* if no feed found, try to allocate new one */
548 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
550 dvb_dmxdev_section_callback
);
552 printk("DVB (%s): could not alloc feed\n",
557 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
558 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
560 printk("DVB (%s): could not set feed\n",
562 dvb_dmxdev_feed_restart(filter
);
566 dvb_dmxdev_feed_stop(filter
);
569 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
571 dvb_dmxdev_feed_restart(filter
);
572 filter
->feed
.sec
->start_filtering(*secfeed
);
573 dprintk("could not get filter\n");
577 (*secfilter
)->priv
= filter
;
579 memcpy(&((*secfilter
)->filter_value
[3]),
580 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
581 memcpy(&(*secfilter
)->filter_mask
[3],
582 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
583 memcpy(&(*secfilter
)->filter_mode
[3],
584 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
586 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
587 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
588 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
589 (*secfilter
)->filter_mask
[1] = 0;
590 (*secfilter
)->filter_mask
[2] = 0;
594 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
598 dvb_dmxdev_filter_timer(filter
);
601 case DMXDEV_TYPE_PES
:
603 struct timespec timeout
= { 0 };
604 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
608 enum dmx_ts_pes ts_pes
;
609 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
611 filter
->feed
.ts
= NULL
;
612 otype
= para
->output
;
614 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
616 if (ts_pes
< DMX_PES_OTHER
)
617 ts_type
= TS_DECODER
;
621 if (otype
== DMX_OUT_TS_TAP
)
622 ts_type
|= TS_PACKET
;
624 if (otype
== DMX_OUT_TAP
)
625 ts_type
|= TS_PAYLOAD_ONLY
| TS_PACKET
;
627 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
629 dvb_dmxdev_ts_callback
);
633 (*tsfeed
)->priv
= filter
;
635 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
638 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
643 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
645 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
656 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
660 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
662 struct dvb_device
*dvbdev
= file
->private_data
;
663 struct dmxdev
*dmxdev
= dvbdev
->priv
;
665 struct dmxdev_filter
*dmxdevfilter
;
670 if (mutex_lock_interruptible(&dmxdev
->mutex
))
673 for (i
= 0; i
< dmxdev
->filternum
; i
++)
674 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
677 if (i
== dmxdev
->filternum
) {
678 mutex_unlock(&dmxdev
->mutex
);
682 dmxdevfilter
= &dmxdev
->filter
[i
];
683 mutex_init(&dmxdevfilter
->mutex
);
684 file
->private_data
= dmxdevfilter
;
686 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
687 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
688 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
689 dmxdevfilter
->feed
.ts
= NULL
;
690 init_timer(&dmxdevfilter
->timer
);
694 mutex_unlock(&dmxdev
->mutex
);
698 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
699 struct dmxdev_filter
*dmxdevfilter
)
701 mutex_lock(&dmxdev
->mutex
);
702 mutex_lock(&dmxdevfilter
->mutex
);
704 dvb_dmxdev_filter_stop(dmxdevfilter
);
705 dvb_dmxdev_filter_reset(dmxdevfilter
);
707 if (dmxdevfilter
->buffer
.data
) {
708 void *mem
= dmxdevfilter
->buffer
.data
;
710 spin_lock_irq(&dmxdev
->lock
);
711 dmxdevfilter
->buffer
.data
= NULL
;
712 spin_unlock_irq(&dmxdev
->lock
);
716 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
717 wake_up(&dmxdevfilter
->buffer
.queue
);
718 mutex_unlock(&dmxdevfilter
->mutex
);
719 mutex_unlock(&dmxdev
->mutex
);
723 static inline void invert_mode(dmx_filter_t
*filter
)
727 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
728 filter
->mode
[i
] ^= 0xff;
731 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
732 struct dmxdev_filter
*dmxdevfilter
,
733 struct dmx_sct_filter_params
*params
)
735 dprintk("function : %s\n", __FUNCTION__
);
737 dvb_dmxdev_filter_stop(dmxdevfilter
);
739 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
740 memcpy(&dmxdevfilter
->params
.sec
,
741 params
, sizeof(struct dmx_sct_filter_params
));
742 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
743 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
745 if (params
->flags
& DMX_IMMEDIATE_START
)
746 return dvb_dmxdev_filter_start(dmxdevfilter
);
751 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
752 struct dmxdev_filter
*dmxdevfilter
,
753 struct dmx_pes_filter_params
*params
)
755 dvb_dmxdev_filter_stop(dmxdevfilter
);
757 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
760 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
761 memcpy(&dmxdevfilter
->params
, params
,
762 sizeof(struct dmx_pes_filter_params
));
764 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
766 if (params
->flags
& DMX_IMMEDIATE_START
)
767 return dvb_dmxdev_filter_start(dmxdevfilter
);
772 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
773 struct file
*file
, char __user
*buf
,
774 size_t count
, loff_t
*ppos
)
779 if (dfil
->todo
<= 0) {
780 hcount
= 3 + dfil
->todo
;
783 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
784 file
->f_flags
& O_NONBLOCK
,
790 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
795 dfil
->todo
-= result
;
798 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
802 if (count
> dfil
->todo
)
804 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
805 file
->f_flags
& O_NONBLOCK
,
809 dfil
->todo
-= result
;
810 return (result
+ done
);
814 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
817 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
820 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
823 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
824 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
826 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
827 file
->f_flags
& O_NONBLOCK
,
830 mutex_unlock(&dmxdevfilter
->mutex
);
834 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
835 unsigned int cmd
, void *parg
)
837 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
838 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
839 unsigned long arg
= (unsigned long)parg
;
842 if (mutex_lock_interruptible(&dmxdev
->mutex
))
847 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
848 mutex_unlock(&dmxdev
->mutex
);
851 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
854 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
855 mutex_unlock(&dmxdevfilter
->mutex
);
859 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
860 mutex_unlock(&dmxdev
->mutex
);
863 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
864 mutex_unlock(&dmxdevfilter
->mutex
);
868 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
869 mutex_unlock(&dmxdev
->mutex
);
872 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
873 mutex_unlock(&dmxdevfilter
->mutex
);
876 case DMX_SET_PES_FILTER
:
877 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
878 mutex_unlock(&dmxdev
->mutex
);
881 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
882 mutex_unlock(&dmxdevfilter
->mutex
);
885 case DMX_SET_BUFFER_SIZE
:
886 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
887 mutex_unlock(&dmxdev
->mutex
);
890 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
891 mutex_unlock(&dmxdevfilter
->mutex
);
894 case DMX_GET_PES_PIDS
:
895 if (!dmxdev
->demux
->get_pes_pids
) {
899 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
903 if (!dmxdev
->demux
->get_caps
) {
907 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
911 if (!dmxdev
->demux
->set_source
) {
915 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
919 if (!dmxdev
->demux
->get_stc
) {
923 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
924 ((struct dmx_stc
*)parg
)->num
,
925 &((struct dmx_stc
*)parg
)->stc
,
926 &((struct dmx_stc
*)parg
)->base
);
933 mutex_unlock(&dmxdev
->mutex
);
937 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
938 unsigned int cmd
, unsigned long arg
)
940 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
943 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
945 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
946 unsigned int mask
= 0;
951 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
953 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
954 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
955 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
958 if (dmxdevfilter
->buffer
.error
)
959 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
961 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
962 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
967 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
969 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
970 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
974 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
976 mutex_lock(&dmxdev
->mutex
);
977 dmxdev
->dvbdev
->users
--;
978 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
979 fops_put(file
->f_op
);
981 mutex_unlock(&dmxdev
->mutex
);
982 wake_up(&dmxdev
->dvbdev
->wait_queue
);
984 mutex_unlock(&dmxdev
->mutex
);
989 static struct file_operations dvb_demux_fops
= {
990 .owner
= THIS_MODULE
,
991 .read
= dvb_demux_read
,
992 .ioctl
= dvb_demux_ioctl
,
993 .open
= dvb_demux_open
,
994 .release
= dvb_demux_release
,
995 .poll
= dvb_demux_poll
,
998 static struct dvb_device dvbdev_demux
= {
1002 .fops
= &dvb_demux_fops
1005 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
1006 unsigned int cmd
, void *parg
)
1008 struct dvb_device
*dvbdev
= file
->private_data
;
1009 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1012 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1013 return -ERESTARTSYS
;
1016 case DMX_SET_BUFFER_SIZE
:
1025 mutex_unlock(&dmxdev
->mutex
);
1029 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1030 unsigned int cmd
, unsigned long arg
)
1032 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1035 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1037 struct dvb_device
*dvbdev
= file
->private_data
;
1038 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1039 unsigned int mask
= 0;
1041 dprintk("function : %s\n", __FUNCTION__
);
1043 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1045 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1046 if (dmxdev
->dvr_buffer
.error
)
1047 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1049 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1050 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1052 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1057 static struct file_operations dvb_dvr_fops
= {
1058 .owner
= THIS_MODULE
,
1059 .read
= dvb_dvr_read
,
1060 .write
= dvb_dvr_write
,
1061 .ioctl
= dvb_dvr_ioctl
,
1062 .open
= dvb_dvr_open
,
1063 .release
= dvb_dvr_release
,
1064 .poll
= dvb_dvr_poll
,
1067 static struct dvb_device dvbdev_dvr
= {
1071 .fops
= &dvb_dvr_fops
1074 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1078 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1081 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1082 if (!dmxdev
->filter
)
1085 mutex_init(&dmxdev
->mutex
);
1086 spin_lock_init(&dmxdev
->lock
);
1087 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1088 dmxdev
->filter
[i
].dev
= dmxdev
;
1089 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1090 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1094 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1096 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1097 dmxdev
, DVB_DEVICE_DVR
);
1099 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1104 EXPORT_SYMBOL(dvb_dmxdev_init
);
1106 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1109 if (dmxdev
->dvbdev
->users
> 1) {
1110 wait_event(dmxdev
->dvbdev
->wait_queue
,
1111 dmxdev
->dvbdev
->users
==1);
1113 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1114 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1115 dmxdev
->dvr_dvbdev
->users
==1);
1118 dvb_unregister_device(dmxdev
->dvbdev
);
1119 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1121 vfree(dmxdev
->filter
);
1122 dmxdev
->filter
= NULL
;
1123 dmxdev
->demux
->close(dmxdev
->demux
);
1126 EXPORT_SYMBOL(dvb_dmxdev_release
);