2 * dmxdev.c - DVB demultiplexer device
4 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 for convergence integrated media GmbH
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/moduleparam.h>
29 #include <linux/sched.h>
30 #include <linux/poll.h>
31 #include <linux/ioctl.h>
32 #include <linux/wait.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
40 module_param(debug
, int, 0644);
41 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
43 #define dprintk if (debug) printk
45 static inline void dvb_dmxdev_buffer_init(struct dmxdev_buffer
*buffer
)
52 init_waitqueue_head(&buffer
->queue
);
55 static inline int dvb_dmxdev_buffer_write(struct dmxdev_buffer
*buf
, const u8
*src
, int len
)
66 free
=buf
->pread
-buf
->pwrite
;
70 split
=buf
->size
-buf
->pwrite
;
73 dprintk("dmxdev: buffer overflow\n");
80 memcpy(buf
->data
+ buf
->pwrite
, src
, split
);
84 memcpy(buf
->data
+ buf
->pwrite
, src
+split
, todo
);
85 buf
->pwrite
=(buf
->pwrite
+todo
)%buf
->size
;
89 static ssize_t
dvb_dmxdev_buffer_read(struct dmxdev_buffer
*src
,
90 int non_blocking
, char __user
*buf
, size_t count
, loff_t
*ppos
)
92 unsigned long todo
=count
;
93 int split
, avail
, error
;
98 if ((error
=src
->error
)) {
99 src
->pwrite
=src
->pread
;
104 if (non_blocking
&& (src
->pwrite
==src
->pread
))
108 if (non_blocking
&& (src
->pwrite
==src
->pread
))
109 return (count
-todo
) ? (count
-todo
) : -EWOULDBLOCK
;
111 if (wait_event_interruptible(src
->queue
,
112 (src
->pread
!=src
->pwrite
) ||
116 if ((error
=src
->error
)) {
117 src
->pwrite
=src
->pread
;
123 avail
=src
->pwrite
- src
->pread
;
126 split
=src
->size
- src
->pread
;
131 if (copy_to_user(buf
, src
->data
+src
->pread
, split
))
139 if (copy_to_user(buf
, src
->data
+src
->pread
, avail
))
141 src
->pread
= (src
->pread
+ avail
) % src
->size
;
149 static struct dmx_frontend
* get_fe(struct dmx_demux
*demux
, int type
)
151 struct list_head
*head
, *pos
;
153 head
=demux
->get_frontends(demux
);
156 list_for_each(pos
, head
)
157 if (DMX_FE_ENTRY(pos
)->source
==type
)
158 return DMX_FE_ENTRY(pos
);
163 static inline void dvb_dmxdev_dvr_state_set(struct dmxdev_dvr
*dmxdevdvr
, int state
)
165 spin_lock_irq(&dmxdevdvr
->dev
->lock
);
166 dmxdevdvr
->state
=state
;
167 spin_unlock_irq(&dmxdevdvr
->dev
->lock
);
170 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
172 struct dvb_device
*dvbdev
= file
->private_data
;
173 struct dmxdev
*dmxdev
= dvbdev
->priv
;
174 struct dmx_frontend
*front
;
176 dprintk ("function : %s\n", __FUNCTION__
);
178 if (down_interruptible (&dmxdev
->mutex
))
181 if ((file
->f_flags
&O_ACCMODE
)==O_RDWR
) {
182 if (!(dmxdev
->capabilities
&DMXDEV_CAP_DUPLEX
)) {
188 if ((file
->f_flags
&O_ACCMODE
)==O_RDONLY
) {
189 dvb_dmxdev_buffer_init(&dmxdev
->dvr_buffer
);
190 dmxdev
->dvr_buffer
.size
=DVR_BUFFER_SIZE
;
191 dmxdev
->dvr_buffer
.data
=vmalloc(DVR_BUFFER_SIZE
);
192 if (!dmxdev
->dvr_buffer
.data
) {
198 if ((file
->f_flags
&O_ACCMODE
)==O_WRONLY
) {
199 dmxdev
->dvr_orig_fe
=dmxdev
->demux
->frontend
;
201 if (!dmxdev
->demux
->write
) {
206 front
=get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
212 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
213 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
219 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
221 struct dvb_device
*dvbdev
= file
->private_data
;
222 struct dmxdev
*dmxdev
= dvbdev
->priv
;
224 if (down_interruptible (&dmxdev
->mutex
))
227 if ((file
->f_flags
&O_ACCMODE
)==O_WRONLY
) {
228 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
229 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
230 dmxdev
->dvr_orig_fe
);
232 if ((file
->f_flags
&O_ACCMODE
)==O_RDONLY
) {
233 if (dmxdev
->dvr_buffer
.data
) {
234 void *mem
=dmxdev
->dvr_buffer
.data
;
236 spin_lock_irq(&dmxdev
->lock
);
237 dmxdev
->dvr_buffer
.data
=NULL
;
238 spin_unlock_irq(&dmxdev
->lock
);
246 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
247 size_t count
, loff_t
*ppos
)
249 struct dvb_device
*dvbdev
= file
->private_data
;
250 struct dmxdev
*dmxdev
= dvbdev
->priv
;
253 if (!dmxdev
->demux
->write
)
255 if ((file
->f_flags
&O_ACCMODE
)!=O_WRONLY
)
257 if (down_interruptible (&dmxdev
->mutex
))
259 ret
=dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
264 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
267 struct dvb_device
*dvbdev
= file
->private_data
;
268 struct dmxdev
*dmxdev
= dvbdev
->priv
;
271 //down(&dmxdev->mutex);
272 ret
= dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
273 file
->f_flags
&O_NONBLOCK
,
275 //up(&dmxdev->mutex);
279 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
*dmxdevfilter
, int state
)
281 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
282 dmxdevfilter
->state
=state
;
283 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
286 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
, unsigned long size
)
288 struct dmxdev_buffer
*buf
=&dmxdevfilter
->buffer
;
293 if (dmxdevfilter
->state
>=DMXDEV_STATE_GO
)
295 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
299 buf
->pwrite
=buf
->pread
=0;
300 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
304 mem
=vmalloc(dmxdevfilter
->buffer
.size
);
307 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
309 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
314 static void dvb_dmxdev_filter_timeout(unsigned long data
)
316 struct dmxdev_filter
*dmxdevfilter
=(struct dmxdev_filter
*)data
;
318 dmxdevfilter
->buffer
.error
=-ETIMEDOUT
;
319 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
320 dmxdevfilter
->state
=DMXDEV_STATE_TIMEDOUT
;
321 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
322 wake_up(&dmxdevfilter
->buffer
.queue
);
325 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
327 struct dmx_sct_filter_params
*para
=&dmxdevfilter
->params
.sec
;
329 del_timer(&dmxdevfilter
->timer
);
331 dmxdevfilter
->timer
.function
=dvb_dmxdev_filter_timeout
;
332 dmxdevfilter
->timer
.data
=(unsigned long) dmxdevfilter
;
333 dmxdevfilter
->timer
.expires
=jiffies
+1+(HZ
/2+HZ
*para
->timeout
)/1000;
334 add_timer(&dmxdevfilter
->timer
);
338 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
339 const u8
*buffer2
, size_t buffer2_len
,
340 struct dmx_section_filter
*filter
, enum dmx_success success
)
342 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
345 if (dmxdevfilter
->buffer
.error
) {
346 wake_up(&dmxdevfilter
->buffer
.queue
);
349 spin_lock(&dmxdevfilter
->dev
->lock
);
350 if (dmxdevfilter
->state
!=DMXDEV_STATE_GO
) {
351 spin_unlock(&dmxdevfilter
->dev
->lock
);
354 del_timer(&dmxdevfilter
->timer
);
355 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
356 buffer1
[0], buffer1
[1],
357 buffer1
[2], buffer1
[3],
358 buffer1
[4], buffer1
[5]);
359 ret
=dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
, buffer1_len
);
360 if (ret
==buffer1_len
) {
361 ret
=dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
, buffer2_len
);
364 dmxdevfilter
->buffer
.pwrite
=dmxdevfilter
->buffer
.pread
;
365 dmxdevfilter
->buffer
.error
=-EOVERFLOW
;
367 if (dmxdevfilter
->params
.sec
.flags
&DMX_ONESHOT
)
368 dmxdevfilter
->state
=DMXDEV_STATE_DONE
;
369 spin_unlock(&dmxdevfilter
->dev
->lock
);
370 wake_up(&dmxdevfilter
->buffer
.queue
);
374 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
375 const u8
*buffer2
, size_t buffer2_len
,
376 struct dmx_ts_feed
*feed
, enum dmx_success success
)
378 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
379 struct dmxdev_buffer
*buffer
;
382 spin_lock(&dmxdevfilter
->dev
->lock
);
383 if (dmxdevfilter
->params
.pes
.output
==DMX_OUT_DECODER
) {
384 spin_unlock(&dmxdevfilter
->dev
->lock
);
388 if (dmxdevfilter
->params
.pes
.output
==DMX_OUT_TAP
)
389 buffer
=&dmxdevfilter
->buffer
;
391 buffer
=&dmxdevfilter
->dev
->dvr_buffer
;
393 spin_unlock(&dmxdevfilter
->dev
->lock
);
394 wake_up(&buffer
->queue
);
397 ret
=dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
398 if (ret
==buffer1_len
)
399 ret
=dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
401 buffer
->pwrite
=buffer
->pread
;
402 buffer
->error
=-EOVERFLOW
;
404 spin_unlock(&dmxdevfilter
->dev
->lock
);
405 wake_up(&buffer
->queue
);
410 /* stop feed but only mark the specified filter as stopped (state set) */
412 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
414 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
416 switch (dmxdevfilter
->type
) {
417 case DMXDEV_TYPE_SEC
:
418 del_timer(&dmxdevfilter
->timer
);
419 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
421 case DMXDEV_TYPE_PES
:
422 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
431 /* start feed associated with the specified filter */
433 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
435 dvb_dmxdev_filter_state_set (filter
, DMXDEV_STATE_GO
);
437 switch (filter
->type
) {
438 case DMXDEV_TYPE_SEC
:
439 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
441 case DMXDEV_TYPE_PES
:
442 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
452 /* restart section feed if it has filters left associated with it,
453 otherwise release the feed */
455 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
458 struct dmxdev
*dmxdev
= filter
->dev
;
459 u16 pid
= filter
->params
.sec
.pid
;
461 for (i
=0; i
<dmxdev
->filternum
; i
++)
462 if (dmxdev
->filter
[i
].state
>=DMXDEV_STATE_GO
&&
463 dmxdev
->filter
[i
].type
==DMXDEV_TYPE_SEC
&&
464 dmxdev
->filter
[i
].pid
==pid
) {
465 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
469 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
, filter
->feed
.sec
);
474 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
476 if (dmxdevfilter
->state
<DMXDEV_STATE_GO
)
479 switch (dmxdevfilter
->type
) {
480 case DMXDEV_TYPE_SEC
:
481 if (!dmxdevfilter
->feed
.sec
)
483 dvb_dmxdev_feed_stop(dmxdevfilter
);
484 if (dmxdevfilter
->filter
.sec
)
485 dmxdevfilter
->feed
.sec
->
486 release_filter(dmxdevfilter
->feed
.sec
,
487 dmxdevfilter
->filter
.sec
);
488 dvb_dmxdev_feed_restart(dmxdevfilter
);
489 dmxdevfilter
->feed
.sec
=NULL
;
491 case DMXDEV_TYPE_PES
:
492 if (!dmxdevfilter
->feed
.ts
)
494 dvb_dmxdev_feed_stop(dmxdevfilter
);
495 dmxdevfilter
->dev
->demux
->
496 release_ts_feed(dmxdevfilter
->dev
->demux
,
497 dmxdevfilter
->feed
.ts
);
498 dmxdevfilter
->feed
.ts
=NULL
;
501 if (dmxdevfilter
->state
==DMXDEV_STATE_ALLOCATED
)
505 dmxdevfilter
->buffer
.pwrite
=dmxdevfilter
->buffer
.pread
=0;
509 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
511 if (dmxdevfilter
->state
<DMXDEV_STATE_SET
)
514 dmxdevfilter
->type
=DMXDEV_TYPE_NONE
;
515 dmxdevfilter
->pid
=0xffff;
516 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
520 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
522 struct dmxdev
*dmxdev
= filter
->dev
;
526 if (filter
->state
< DMXDEV_STATE_SET
)
529 if (filter
->state
>= DMXDEV_STATE_GO
)
530 dvb_dmxdev_filter_stop(filter
);
532 if (!(mem
= filter
->buffer
.data
)) {
533 mem
= vmalloc(filter
->buffer
.size
);
534 spin_lock_irq(&filter
->dev
->lock
);
535 filter
->buffer
.data
=mem
;
536 spin_unlock_irq(&filter
->dev
->lock
);
537 if (!filter
->buffer
.data
)
541 filter
->buffer
.pwrite
= filter
->buffer
.pread
= 0;
543 switch (filter
->type
) {
544 case DMXDEV_TYPE_SEC
:
546 struct dmx_sct_filter_params
*para
=&filter
->params
.sec
;
547 struct dmx_section_filter
**secfilter
=&filter
->filter
.sec
;
548 struct dmx_section_feed
**secfeed
=&filter
->feed
.sec
;
553 /* find active filter/feed with same PID */
554 for (i
=0; i
<dmxdev
->filternum
; i
++) {
555 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
556 dmxdev
->filter
[i
].pid
== para
->pid
&&
557 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
) {
558 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
563 /* if no feed found, try to allocate new one */
565 ret
=dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
567 dvb_dmxdev_section_callback
);
569 printk ("DVB (%s): could not alloc feed\n",
574 ret
=(*secfeed
)->set(*secfeed
, para
->pid
, 32768,
575 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
578 printk ("DVB (%s): could not set feed\n",
580 dvb_dmxdev_feed_restart(filter
);
584 dvb_dmxdev_feed_stop(filter
);
587 ret
=(*secfeed
)->allocate_filter(*secfeed
, secfilter
);
590 dvb_dmxdev_feed_restart(filter
);
591 filter
->feed
.sec
->start_filtering(*secfeed
);
592 dprintk ("could not get filter\n");
596 (*secfilter
)->priv
= filter
;
598 memcpy(&((*secfilter
)->filter_value
[3]),
599 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
-1);
600 memcpy(&(*secfilter
)->filter_mask
[3],
601 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
-1);
602 memcpy(&(*secfilter
)->filter_mode
[3],
603 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
-1);
605 (*secfilter
)->filter_value
[0]=para
->filter
.filter
[0];
606 (*secfilter
)->filter_mask
[0]=para
->filter
.mask
[0];
607 (*secfilter
)->filter_mode
[0]=para
->filter
.mode
[0];
608 (*secfilter
)->filter_mask
[1]=0;
609 (*secfilter
)->filter_mask
[2]=0;
613 ret
= filter
->feed
.sec
->start_filtering (filter
->feed
.sec
);
618 dvb_dmxdev_filter_timer(filter
);
622 case DMXDEV_TYPE_PES
:
624 struct timespec timeout
= { 0 };
625 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
629 enum dmx_ts_pes ts_pes
;
630 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
632 filter
->feed
.ts
= NULL
;
635 ts_pes
=(enum dmx_ts_pes
) para
->pes_type
;
637 if (ts_pes
<DMX_PES_OTHER
)
642 if (otype
== DMX_OUT_TS_TAP
)
643 ts_type
|= TS_PACKET
;
645 if (otype
== DMX_OUT_TAP
)
646 ts_type
|= TS_PAYLOAD_ONLY
|TS_PACKET
;
648 ret
=dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
650 dvb_dmxdev_ts_callback
);
654 (*tsfeed
)->priv
= (void *) filter
;
656 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
660 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, *tsfeed
);
664 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
667 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, *tsfeed
);
677 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
681 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
683 struct dvb_device
*dvbdev
= file
->private_data
;
684 struct dmxdev
*dmxdev
= dvbdev
->priv
;
686 struct dmxdev_filter
*dmxdevfilter
;
691 if (down_interruptible(&dmxdev
->mutex
))
694 for (i
=0; i
<dmxdev
->filternum
; i
++)
695 if (dmxdev
->filter
[i
].state
==DMXDEV_STATE_FREE
)
698 if (i
==dmxdev
->filternum
) {
703 dmxdevfilter
=&dmxdev
->filter
[i
];
704 sema_init(&dmxdevfilter
->mutex
, 1);
705 dmxdevfilter
->dvbdev
=dmxdev
->dvbdev
;
706 file
->private_data
=dmxdevfilter
;
708 dvb_dmxdev_buffer_init(&dmxdevfilter
->buffer
);
709 dmxdevfilter
->type
=DMXDEV_TYPE_NONE
;
710 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
711 dmxdevfilter
->feed
.ts
=NULL
;
712 init_timer(&dmxdevfilter
->timer
);
719 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
, struct dmxdev_filter
*dmxdevfilter
)
721 if (down_interruptible(&dmxdev
->mutex
))
724 if (down_interruptible(&dmxdevfilter
->mutex
)) {
729 dvb_dmxdev_filter_stop(dmxdevfilter
);
730 dvb_dmxdev_filter_reset(dmxdevfilter
);
732 if (dmxdevfilter
->buffer
.data
) {
733 void *mem
=dmxdevfilter
->buffer
.data
;
735 spin_lock_irq(&dmxdev
->lock
);
736 dmxdevfilter
->buffer
.data
=NULL
;
737 spin_unlock_irq(&dmxdev
->lock
);
741 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
742 wake_up(&dmxdevfilter
->buffer
.queue
);
743 up(&dmxdevfilter
->mutex
);
748 static inline void invert_mode(dmx_filter_t
*filter
)
752 for (i
=0; i
<DMX_FILTER_SIZE
; i
++)
753 filter
->mode
[i
]^=0xff;
757 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
758 struct dmxdev_filter
*dmxdevfilter
,
759 struct dmx_sct_filter_params
*params
)
761 dprintk ("function : %s\n", __FUNCTION__
);
763 dvb_dmxdev_filter_stop(dmxdevfilter
);
765 dmxdevfilter
->type
=DMXDEV_TYPE_SEC
;
766 dmxdevfilter
->pid
=params
->pid
;
767 memcpy(&dmxdevfilter
->params
.sec
,
768 params
, sizeof(struct dmx_sct_filter_params
));
769 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
770 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
772 if (params
->flags
&DMX_IMMEDIATE_START
)
773 return dvb_dmxdev_filter_start(dmxdevfilter
);
778 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
779 struct dmxdev_filter
*dmxdevfilter
,
780 struct dmx_pes_filter_params
*params
)
782 dvb_dmxdev_filter_stop(dmxdevfilter
);
784 if (params
->pes_type
>DMX_PES_OTHER
|| params
->pes_type
<0)
787 dmxdevfilter
->type
=DMXDEV_TYPE_PES
;
788 dmxdevfilter
->pid
=params
->pid
;
789 memcpy(&dmxdevfilter
->params
, params
, sizeof(struct dmx_pes_filter_params
));
791 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
793 if (params
->flags
&DMX_IMMEDIATE_START
)
794 return dvb_dmxdev_filter_start(dmxdevfilter
);
799 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
800 struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
809 result
=dvb_dmxdev_buffer_read(&dfil
->buffer
, file
->f_flags
&O_NONBLOCK
,
815 if (copy_from_user(dfil
->secheader
-dfil
->todo
, buf
, result
))
823 dfil
->todo
=((dfil
->secheader
[1]<<8)|dfil
->secheader
[2])&0xfff;
827 if (count
>dfil
->todo
)
829 result
=dvb_dmxdev_buffer_read(&dfil
->buffer
, file
->f_flags
&O_NONBLOCK
,
834 return (result
+done
);
839 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
841 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
844 if (down_interruptible(&dmxdevfilter
->mutex
))
847 if (dmxdevfilter
->type
==DMXDEV_TYPE_SEC
)
848 ret
=dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
850 ret
=dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
851 file
->f_flags
&O_NONBLOCK
,
854 up(&dmxdevfilter
->mutex
);
859 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
860 unsigned int cmd
, void *parg
)
862 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
863 struct dmxdev
*dmxdev
=dmxdevfilter
->dev
;
864 unsigned long arg
=(unsigned long) parg
;
867 if (down_interruptible (&dmxdev
->mutex
))
872 if (down_interruptible(&dmxdevfilter
->mutex
)) {
876 if (dmxdevfilter
->state
<DMXDEV_STATE_SET
)
879 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
880 up(&dmxdevfilter
->mutex
);
884 if (down_interruptible(&dmxdevfilter
->mutex
)) {
888 ret
=dvb_dmxdev_filter_stop(dmxdevfilter
);
889 up(&dmxdevfilter
->mutex
);
893 if (down_interruptible(&dmxdevfilter
->mutex
)) {
897 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
,
898 (struct dmx_sct_filter_params
*)parg
);
899 up(&dmxdevfilter
->mutex
);
902 case DMX_SET_PES_FILTER
:
903 if (down_interruptible(&dmxdevfilter
->mutex
)) {
907 ret
=dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
,
908 (struct dmx_pes_filter_params
*)parg
);
909 up(&dmxdevfilter
->mutex
);
912 case DMX_SET_BUFFER_SIZE
:
913 if (down_interruptible(&dmxdevfilter
->mutex
)) {
917 ret
=dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
918 up(&dmxdevfilter
->mutex
);
924 case DMX_GET_PES_PIDS
:
925 if (!dmxdev
->demux
->get_pes_pids
) {
929 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, (u16
*)parg
);
933 if (!dmxdev
->demux
->get_caps
) {
937 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
941 if (!dmxdev
->demux
->set_source
) {
945 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
949 if (!dmxdev
->demux
->get_stc
) {
953 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
954 ((struct dmx_stc
*)parg
)->num
,
955 &((struct dmx_stc
*)parg
)->stc
,
956 &((struct dmx_stc
*)parg
)->base
);
966 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
967 unsigned int cmd
, unsigned long arg
)
969 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
973 static unsigned int dvb_demux_poll (struct file
*file
, poll_table
*wait
)
975 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
976 unsigned int mask
= 0;
981 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
983 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
984 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
985 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
988 if (dmxdevfilter
->buffer
.error
)
989 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
991 if (dmxdevfilter
->buffer
.pread
!= dmxdevfilter
->buffer
.pwrite
)
992 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
998 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1000 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1001 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1003 return dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1007 static struct file_operations dvb_demux_fops
= {
1008 .owner
= THIS_MODULE
,
1009 .read
= dvb_demux_read
,
1010 .ioctl
= dvb_demux_ioctl
,
1011 .open
= dvb_demux_open
,
1012 .release
= dvb_demux_release
,
1013 .poll
= dvb_demux_poll
,
1017 static struct dvb_device dvbdev_demux
= {
1021 .fops
= &dvb_demux_fops
1025 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
1026 unsigned int cmd
, void *parg
)
1028 struct dvb_device
*dvbdev
= file
->private_data
;
1029 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1033 if (down_interruptible (&dmxdev
->mutex
))
1034 return -ERESTARTSYS
;
1037 case DMX_SET_BUFFER_SIZE
:
1050 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1051 unsigned int cmd
, unsigned long arg
)
1053 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1057 static unsigned int dvb_dvr_poll (struct file
*file
, poll_table
*wait
)
1059 struct dvb_device
*dvbdev
= file
->private_data
;
1060 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1061 unsigned int mask
= 0;
1063 dprintk ("function : %s\n", __FUNCTION__
);
1065 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1067 if ((file
->f_flags
&O_ACCMODE
) == O_RDONLY
) {
1068 if (dmxdev
->dvr_buffer
.error
)
1069 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1071 if (dmxdev
->dvr_buffer
.pread
!=dmxdev
->dvr_buffer
.pwrite
)
1072 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1074 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1080 static struct file_operations dvb_dvr_fops
= {
1081 .owner
= THIS_MODULE
,
1082 .read
= dvb_dvr_read
,
1083 .write
= dvb_dvr_write
,
1084 .ioctl
= dvb_dvr_ioctl
,
1085 .open
= dvb_dvr_open
,
1086 .release
= dvb_dvr_release
,
1087 .poll
= dvb_dvr_poll
,
1090 static struct dvb_device dvbdev_dvr
= {
1094 .fops
= &dvb_dvr_fops
1098 dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1102 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1105 dmxdev
->filter
= vmalloc(dmxdev
->filternum
*sizeof(struct dmxdev_filter
));
1106 if (!dmxdev
->filter
)
1109 dmxdev
->dvr
= vmalloc(dmxdev
->filternum
*sizeof(struct dmxdev_dvr
));
1111 vfree(dmxdev
->filter
);
1112 dmxdev
->filter
= NULL
;
1116 sema_init(&dmxdev
->mutex
, 1);
1117 spin_lock_init(&dmxdev
->lock
);
1118 for (i
=0; i
<dmxdev
->filternum
; i
++) {
1119 dmxdev
->filter
[i
].dev
=dmxdev
;
1120 dmxdev
->filter
[i
].buffer
.data
=NULL
;
1121 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
], DMXDEV_STATE_FREE
);
1122 dmxdev
->dvr
[i
].dev
=dmxdev
;
1123 dmxdev
->dvr
[i
].buffer
.data
=NULL
;
1124 dvb_dmxdev_dvr_state_set(&dmxdev
->dvr
[i
], DMXDEV_STATE_FREE
);
1127 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
, DVB_DEVICE_DEMUX
);
1128 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
, dmxdev
, DVB_DEVICE_DVR
);
1130 dvb_dmxdev_buffer_init(&dmxdev
->dvr_buffer
);
1134 EXPORT_SYMBOL(dvb_dmxdev_init
);
1137 dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1139 dvb_unregister_device(dmxdev
->dvbdev
);
1140 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1142 vfree(dmxdev
->filter
);
1143 dmxdev
->filter
=NULL
;
1146 dmxdev
->demux
->close(dmxdev
->demux
);
1148 EXPORT_SYMBOL(dvb_dmxdev_release
);