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 struct dmxdev_filter
*
46 dvb_dmxdev_file_to_filter(struct file
*file
)
48 return (struct dmxdev_filter
*) file
->private_data
;
51 static inline void dvb_dmxdev_buffer_init(struct dmxdev_buffer
*buffer
)
58 init_waitqueue_head(&buffer
->queue
);
61 static inline int dvb_dmxdev_buffer_write(struct dmxdev_buffer
*buf
, const u8
*src
, int len
)
72 free
=buf
->pread
-buf
->pwrite
;
76 split
=buf
->size
-buf
->pwrite
;
79 dprintk("dmxdev: buffer overflow\n");
86 memcpy(buf
->data
+ buf
->pwrite
, src
, split
);
90 memcpy(buf
->data
+ buf
->pwrite
, src
+split
, todo
);
91 buf
->pwrite
=(buf
->pwrite
+todo
)%buf
->size
;
95 static ssize_t
dvb_dmxdev_buffer_read(struct dmxdev_buffer
*src
,
96 int non_blocking
, char __user
*buf
, size_t count
, loff_t
*ppos
)
98 unsigned long todo
=count
;
99 int split
, avail
, error
;
104 if ((error
=src
->error
)) {
105 src
->pwrite
=src
->pread
;
110 if (non_blocking
&& (src
->pwrite
==src
->pread
))
114 if (non_blocking
&& (src
->pwrite
==src
->pread
))
115 return (count
-todo
) ? (count
-todo
) : -EWOULDBLOCK
;
117 if (wait_event_interruptible(src
->queue
,
118 (src
->pread
!=src
->pwrite
) ||
122 if ((error
=src
->error
)) {
123 src
->pwrite
=src
->pread
;
129 avail
=src
->pwrite
- src
->pread
;
132 split
=src
->size
- src
->pread
;
137 if (copy_to_user(buf
, src
->data
+src
->pread
, split
))
145 if (copy_to_user(buf
, src
->data
+src
->pread
, avail
))
147 src
->pread
= (src
->pread
+ avail
) % src
->size
;
155 static struct dmx_frontend
* get_fe(struct dmx_demux
*demux
, int type
)
157 struct list_head
*head
, *pos
;
159 head
=demux
->get_frontends(demux
);
162 list_for_each(pos
, head
)
163 if (DMX_FE_ENTRY(pos
)->source
==type
)
164 return DMX_FE_ENTRY(pos
);
169 static inline void dvb_dmxdev_dvr_state_set(struct dmxdev_dvr
*dmxdevdvr
, int state
)
171 spin_lock_irq(&dmxdevdvr
->dev
->lock
);
172 dmxdevdvr
->state
=state
;
173 spin_unlock_irq(&dmxdevdvr
->dev
->lock
);
176 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
178 struct dvb_device
*dvbdev
= file
->private_data
;
179 struct dmxdev
*dmxdev
= dvbdev
->priv
;
180 struct dmx_frontend
*front
;
182 dprintk ("function : %s\n", __FUNCTION__
);
184 if (down_interruptible (&dmxdev
->mutex
))
187 if ((file
->f_flags
&O_ACCMODE
)==O_RDWR
) {
188 if (!(dmxdev
->capabilities
&DMXDEV_CAP_DUPLEX
)) {
194 if ((file
->f_flags
&O_ACCMODE
)==O_RDONLY
) {
195 dvb_dmxdev_buffer_init(&dmxdev
->dvr_buffer
);
196 dmxdev
->dvr_buffer
.size
=DVR_BUFFER_SIZE
;
197 dmxdev
->dvr_buffer
.data
=vmalloc(DVR_BUFFER_SIZE
);
198 if (!dmxdev
->dvr_buffer
.data
) {
204 if ((file
->f_flags
&O_ACCMODE
)==O_WRONLY
) {
205 dmxdev
->dvr_orig_fe
=dmxdev
->demux
->frontend
;
207 if (!dmxdev
->demux
->write
) {
212 front
=get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
218 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
219 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
225 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
227 struct dvb_device
*dvbdev
= file
->private_data
;
228 struct dmxdev
*dmxdev
= dvbdev
->priv
;
230 if (down_interruptible (&dmxdev
->mutex
))
233 if ((file
->f_flags
&O_ACCMODE
)==O_WRONLY
) {
234 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
235 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
236 dmxdev
->dvr_orig_fe
);
238 if ((file
->f_flags
&O_ACCMODE
)==O_RDONLY
) {
239 if (dmxdev
->dvr_buffer
.data
) {
240 void *mem
=dmxdev
->dvr_buffer
.data
;
242 spin_lock_irq(&dmxdev
->lock
);
243 dmxdev
->dvr_buffer
.data
=NULL
;
244 spin_unlock_irq(&dmxdev
->lock
);
252 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
253 size_t count
, loff_t
*ppos
)
255 struct dvb_device
*dvbdev
= file
->private_data
;
256 struct dmxdev
*dmxdev
= dvbdev
->priv
;
259 if (!dmxdev
->demux
->write
)
261 if ((file
->f_flags
&O_ACCMODE
)!=O_WRONLY
)
263 if (down_interruptible (&dmxdev
->mutex
))
265 ret
=dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
270 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
273 struct dvb_device
*dvbdev
= file
->private_data
;
274 struct dmxdev
*dmxdev
= dvbdev
->priv
;
277 //down(&dmxdev->mutex);
278 ret
= dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
279 file
->f_flags
&O_NONBLOCK
,
281 //up(&dmxdev->mutex);
285 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
*dmxdevfilter
, int state
)
287 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
288 dmxdevfilter
->state
=state
;
289 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
292 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
, unsigned long size
)
294 struct dmxdev_buffer
*buf
=&dmxdevfilter
->buffer
;
299 if (dmxdevfilter
->state
>=DMXDEV_STATE_GO
)
301 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
305 buf
->pwrite
=buf
->pread
=0;
306 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
310 mem
=vmalloc(dmxdevfilter
->buffer
.size
);
313 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
315 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
320 static void dvb_dmxdev_filter_timeout(unsigned long data
)
322 struct dmxdev_filter
*dmxdevfilter
=(struct dmxdev_filter
*)data
;
324 dmxdevfilter
->buffer
.error
=-ETIMEDOUT
;
325 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
326 dmxdevfilter
->state
=DMXDEV_STATE_TIMEDOUT
;
327 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
328 wake_up(&dmxdevfilter
->buffer
.queue
);
331 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
333 struct dmx_sct_filter_params
*para
=&dmxdevfilter
->params
.sec
;
335 del_timer(&dmxdevfilter
->timer
);
337 dmxdevfilter
->timer
.function
=dvb_dmxdev_filter_timeout
;
338 dmxdevfilter
->timer
.data
=(unsigned long) dmxdevfilter
;
339 dmxdevfilter
->timer
.expires
=jiffies
+1+(HZ
/2+HZ
*para
->timeout
)/1000;
340 add_timer(&dmxdevfilter
->timer
);
344 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
345 const u8
*buffer2
, size_t buffer2_len
,
346 struct dmx_section_filter
*filter
, enum dmx_success success
)
348 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
351 if (dmxdevfilter
->buffer
.error
) {
352 wake_up(&dmxdevfilter
->buffer
.queue
);
355 spin_lock(&dmxdevfilter
->dev
->lock
);
356 if (dmxdevfilter
->state
!=DMXDEV_STATE_GO
) {
357 spin_unlock(&dmxdevfilter
->dev
->lock
);
360 del_timer(&dmxdevfilter
->timer
);
361 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
362 buffer1
[0], buffer1
[1],
363 buffer1
[2], buffer1
[3],
364 buffer1
[4], buffer1
[5]);
365 ret
=dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
, buffer1_len
);
366 if (ret
==buffer1_len
) {
367 ret
=dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
, buffer2_len
);
370 dmxdevfilter
->buffer
.pwrite
=dmxdevfilter
->buffer
.pread
;
371 dmxdevfilter
->buffer
.error
=-EOVERFLOW
;
373 if (dmxdevfilter
->params
.sec
.flags
&DMX_ONESHOT
)
374 dmxdevfilter
->state
=DMXDEV_STATE_DONE
;
375 spin_unlock(&dmxdevfilter
->dev
->lock
);
376 wake_up(&dmxdevfilter
->buffer
.queue
);
380 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
381 const u8
*buffer2
, size_t buffer2_len
,
382 struct dmx_ts_feed
*feed
, enum dmx_success success
)
384 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
385 struct dmxdev_buffer
*buffer
;
388 spin_lock(&dmxdevfilter
->dev
->lock
);
389 if (dmxdevfilter
->params
.pes
.output
==DMX_OUT_DECODER
) {
390 spin_unlock(&dmxdevfilter
->dev
->lock
);
394 if (dmxdevfilter
->params
.pes
.output
==DMX_OUT_TAP
)
395 buffer
=&dmxdevfilter
->buffer
;
397 buffer
=&dmxdevfilter
->dev
->dvr_buffer
;
399 spin_unlock(&dmxdevfilter
->dev
->lock
);
400 wake_up(&buffer
->queue
);
403 ret
=dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
404 if (ret
==buffer1_len
)
405 ret
=dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
407 buffer
->pwrite
=buffer
->pread
;
408 buffer
->error
=-EOVERFLOW
;
410 spin_unlock(&dmxdevfilter
->dev
->lock
);
411 wake_up(&buffer
->queue
);
416 /* stop feed but only mark the specified filter as stopped (state set) */
418 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
420 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
422 switch (dmxdevfilter
->type
) {
423 case DMXDEV_TYPE_SEC
:
424 del_timer(&dmxdevfilter
->timer
);
425 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
427 case DMXDEV_TYPE_PES
:
428 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
437 /* start feed associated with the specified filter */
439 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
441 dvb_dmxdev_filter_state_set (filter
, DMXDEV_STATE_GO
);
443 switch (filter
->type
) {
444 case DMXDEV_TYPE_SEC
:
445 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
447 case DMXDEV_TYPE_PES
:
448 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
458 /* restart section feed if it has filters left associated with it,
459 otherwise release the feed */
461 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
464 struct dmxdev
*dmxdev
= filter
->dev
;
465 u16 pid
= filter
->params
.sec
.pid
;
467 for (i
=0; i
<dmxdev
->filternum
; i
++)
468 if (dmxdev
->filter
[i
].state
>=DMXDEV_STATE_GO
&&
469 dmxdev
->filter
[i
].type
==DMXDEV_TYPE_SEC
&&
470 dmxdev
->filter
[i
].pid
==pid
) {
471 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
475 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
, filter
->feed
.sec
);
480 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
482 if (dmxdevfilter
->state
<DMXDEV_STATE_GO
)
485 switch (dmxdevfilter
->type
) {
486 case DMXDEV_TYPE_SEC
:
487 if (!dmxdevfilter
->feed
.sec
)
489 dvb_dmxdev_feed_stop(dmxdevfilter
);
490 if (dmxdevfilter
->filter
.sec
)
491 dmxdevfilter
->feed
.sec
->
492 release_filter(dmxdevfilter
->feed
.sec
,
493 dmxdevfilter
->filter
.sec
);
494 dvb_dmxdev_feed_restart(dmxdevfilter
);
495 dmxdevfilter
->feed
.sec
=NULL
;
497 case DMXDEV_TYPE_PES
:
498 if (!dmxdevfilter
->feed
.ts
)
500 dvb_dmxdev_feed_stop(dmxdevfilter
);
501 dmxdevfilter
->dev
->demux
->
502 release_ts_feed(dmxdevfilter
->dev
->demux
,
503 dmxdevfilter
->feed
.ts
);
504 dmxdevfilter
->feed
.ts
=NULL
;
507 if (dmxdevfilter
->state
==DMXDEV_STATE_ALLOCATED
)
511 dmxdevfilter
->buffer
.pwrite
=dmxdevfilter
->buffer
.pread
=0;
515 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
517 if (dmxdevfilter
->state
<DMXDEV_STATE_SET
)
520 dmxdevfilter
->type
=DMXDEV_TYPE_NONE
;
521 dmxdevfilter
->pid
=0xffff;
522 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
526 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
528 struct dmxdev
*dmxdev
= filter
->dev
;
532 if (filter
->state
< DMXDEV_STATE_SET
)
535 if (filter
->state
>= DMXDEV_STATE_GO
)
536 dvb_dmxdev_filter_stop(filter
);
538 if (!(mem
= filter
->buffer
.data
)) {
539 mem
= vmalloc(filter
->buffer
.size
);
540 spin_lock_irq(&filter
->dev
->lock
);
541 filter
->buffer
.data
=mem
;
542 spin_unlock_irq(&filter
->dev
->lock
);
543 if (!filter
->buffer
.data
)
547 filter
->buffer
.pwrite
= filter
->buffer
.pread
= 0;
549 switch (filter
->type
) {
550 case DMXDEV_TYPE_SEC
:
552 struct dmx_sct_filter_params
*para
=&filter
->params
.sec
;
553 struct dmx_section_filter
**secfilter
=&filter
->filter
.sec
;
554 struct dmx_section_feed
**secfeed
=&filter
->feed
.sec
;
559 /* find active filter/feed with same PID */
560 for (i
=0; i
<dmxdev
->filternum
; i
++) {
561 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
562 dmxdev
->filter
[i
].pid
== para
->pid
&&
563 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
) {
564 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
569 /* if no feed found, try to allocate new one */
571 ret
=dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
573 dvb_dmxdev_section_callback
);
575 printk ("DVB (%s): could not alloc feed\n",
580 ret
=(*secfeed
)->set(*secfeed
, para
->pid
, 32768, 0,
581 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
584 printk ("DVB (%s): could not set feed\n",
586 dvb_dmxdev_feed_restart(filter
);
590 dvb_dmxdev_feed_stop(filter
);
593 ret
=(*secfeed
)->allocate_filter(*secfeed
, secfilter
);
596 dvb_dmxdev_feed_restart(filter
);
597 filter
->feed
.sec
->start_filtering(*secfeed
);
598 dprintk ("could not get filter\n");
602 (*secfilter
)->priv
= filter
;
604 memcpy(&((*secfilter
)->filter_value
[3]),
605 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
-1);
606 memcpy(&(*secfilter
)->filter_mask
[3],
607 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
-1);
608 memcpy(&(*secfilter
)->filter_mode
[3],
609 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
-1);
611 (*secfilter
)->filter_value
[0]=para
->filter
.filter
[0];
612 (*secfilter
)->filter_mask
[0]=para
->filter
.mask
[0];
613 (*secfilter
)->filter_mode
[0]=para
->filter
.mode
[0];
614 (*secfilter
)->filter_mask
[1]=0;
615 (*secfilter
)->filter_mask
[2]=0;
619 ret
= filter
->feed
.sec
->start_filtering (filter
->feed
.sec
);
624 dvb_dmxdev_filter_timer(filter
);
628 case DMXDEV_TYPE_PES
:
630 struct timespec timeout
= { 0 };
631 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
635 enum dmx_ts_pes ts_pes
;
636 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
638 filter
->feed
.ts
= NULL
;
641 ts_pes
=(enum dmx_ts_pes
) para
->pes_type
;
643 if (ts_pes
<DMX_PES_OTHER
)
648 if (otype
== DMX_OUT_TS_TAP
)
649 ts_type
|= TS_PACKET
;
651 if (otype
== DMX_OUT_TAP
)
652 ts_type
|= TS_PAYLOAD_ONLY
|TS_PACKET
;
654 ret
=dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
656 dvb_dmxdev_ts_callback
);
660 (*tsfeed
)->priv
= (void *) filter
;
662 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
663 188, 32768, 0, timeout
);
666 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, *tsfeed
);
670 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
681 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
685 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
687 struct dvb_device
*dvbdev
= file
->private_data
;
688 struct dmxdev
*dmxdev
= dvbdev
->priv
;
690 struct dmxdev_filter
*dmxdevfilter
;
695 if (down_interruptible(&dmxdev
->mutex
))
698 for (i
=0; i
<dmxdev
->filternum
; i
++)
699 if (dmxdev
->filter
[i
].state
==DMXDEV_STATE_FREE
)
702 if (i
==dmxdev
->filternum
) {
707 dmxdevfilter
=&dmxdev
->filter
[i
];
708 sema_init(&dmxdevfilter
->mutex
, 1);
709 dmxdevfilter
->dvbdev
=dmxdev
->dvbdev
;
710 file
->private_data
=dmxdevfilter
;
712 dvb_dmxdev_buffer_init(&dmxdevfilter
->buffer
);
713 dmxdevfilter
->type
=DMXDEV_TYPE_NONE
;
714 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
715 dmxdevfilter
->feed
.ts
=NULL
;
716 init_timer(&dmxdevfilter
->timer
);
723 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
, struct dmxdev_filter
*dmxdevfilter
)
725 if (down_interruptible(&dmxdev
->mutex
))
728 if (down_interruptible(&dmxdevfilter
->mutex
)) {
733 dvb_dmxdev_filter_stop(dmxdevfilter
);
734 dvb_dmxdev_filter_reset(dmxdevfilter
);
736 if (dmxdevfilter
->buffer
.data
) {
737 void *mem
=dmxdevfilter
->buffer
.data
;
739 spin_lock_irq(&dmxdev
->lock
);
740 dmxdevfilter
->buffer
.data
=NULL
;
741 spin_unlock_irq(&dmxdev
->lock
);
745 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
746 wake_up(&dmxdevfilter
->buffer
.queue
);
747 up(&dmxdevfilter
->mutex
);
752 static inline void invert_mode(dmx_filter_t
*filter
)
756 for (i
=0; i
<DMX_FILTER_SIZE
; i
++)
757 filter
->mode
[i
]^=0xff;
761 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
762 struct dmxdev_filter
*dmxdevfilter
,
763 struct dmx_sct_filter_params
*params
)
765 dprintk ("function : %s\n", __FUNCTION__
);
767 dvb_dmxdev_filter_stop(dmxdevfilter
);
769 dmxdevfilter
->type
=DMXDEV_TYPE_SEC
;
770 dmxdevfilter
->pid
=params
->pid
;
771 memcpy(&dmxdevfilter
->params
.sec
,
772 params
, sizeof(struct dmx_sct_filter_params
));
773 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
774 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
776 if (params
->flags
&DMX_IMMEDIATE_START
)
777 return dvb_dmxdev_filter_start(dmxdevfilter
);
782 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
783 struct dmxdev_filter
*dmxdevfilter
,
784 struct dmx_pes_filter_params
*params
)
786 dvb_dmxdev_filter_stop(dmxdevfilter
);
788 if (params
->pes_type
>DMX_PES_OTHER
|| params
->pes_type
<0)
791 dmxdevfilter
->type
=DMXDEV_TYPE_PES
;
792 dmxdevfilter
->pid
=params
->pid
;
793 memcpy(&dmxdevfilter
->params
, params
, sizeof(struct dmx_pes_filter_params
));
795 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
797 if (params
->flags
&DMX_IMMEDIATE_START
)
798 return dvb_dmxdev_filter_start(dmxdevfilter
);
803 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
804 struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
813 result
=dvb_dmxdev_buffer_read(&dfil
->buffer
, file
->f_flags
&O_NONBLOCK
,
819 if (copy_from_user(dfil
->secheader
-dfil
->todo
, buf
, result
))
827 dfil
->todo
=((dfil
->secheader
[1]<<8)|dfil
->secheader
[2])&0xfff;
831 if (count
>dfil
->todo
)
833 result
=dvb_dmxdev_buffer_read(&dfil
->buffer
, file
->f_flags
&O_NONBLOCK
,
838 return (result
+done
);
843 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
845 struct dmxdev_filter
*dmxdevfilter
=dvb_dmxdev_file_to_filter(file
);
848 if (down_interruptible(&dmxdevfilter
->mutex
))
851 if (dmxdevfilter
->type
==DMXDEV_TYPE_SEC
)
852 ret
=dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
854 ret
=dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
855 file
->f_flags
&O_NONBLOCK
,
858 up(&dmxdevfilter
->mutex
);
863 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
864 unsigned int cmd
, void *parg
)
866 struct dmxdev_filter
*dmxdevfilter
=dvb_dmxdev_file_to_filter(file
);
867 struct dmxdev
*dmxdev
=dmxdevfilter
->dev
;
868 unsigned long arg
=(unsigned long) parg
;
871 if (down_interruptible (&dmxdev
->mutex
))
876 if (down_interruptible(&dmxdevfilter
->mutex
)) {
880 if (dmxdevfilter
->state
<DMXDEV_STATE_SET
)
883 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
884 up(&dmxdevfilter
->mutex
);
888 if (down_interruptible(&dmxdevfilter
->mutex
)) {
892 ret
=dvb_dmxdev_filter_stop(dmxdevfilter
);
893 up(&dmxdevfilter
->mutex
);
897 if (down_interruptible(&dmxdevfilter
->mutex
)) {
901 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
,
902 (struct dmx_sct_filter_params
*)parg
);
903 up(&dmxdevfilter
->mutex
);
906 case DMX_SET_PES_FILTER
:
907 if (down_interruptible(&dmxdevfilter
->mutex
)) {
911 ret
=dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
,
912 (struct dmx_pes_filter_params
*)parg
);
913 up(&dmxdevfilter
->mutex
);
916 case DMX_SET_BUFFER_SIZE
:
917 if (down_interruptible(&dmxdevfilter
->mutex
)) {
921 ret
=dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
922 up(&dmxdevfilter
->mutex
);
928 case DMX_GET_PES_PIDS
:
929 if (!dmxdev
->demux
->get_pes_pids
) {
933 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, (u16
*)parg
);
937 if (!dmxdev
->demux
->get_stc
) {
941 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
942 ((struct dmx_stc
*)parg
)->num
,
943 &((struct dmx_stc
*)parg
)->stc
,
944 &((struct dmx_stc
*)parg
)->base
);
954 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
955 unsigned int cmd
, unsigned long arg
)
957 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
961 static unsigned int dvb_demux_poll (struct file
*file
, poll_table
*wait
)
963 struct dmxdev_filter
*dmxdevfilter
= dvb_dmxdev_file_to_filter(file
);
964 unsigned int mask
= 0;
969 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
971 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
972 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
973 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
976 if (dmxdevfilter
->buffer
.error
)
977 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
979 if (dmxdevfilter
->buffer
.pread
!= dmxdevfilter
->buffer
.pwrite
)
980 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
986 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
988 struct dmxdev_filter
*dmxdevfilter
= dvb_dmxdev_file_to_filter(file
);
989 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
991 return dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
995 static struct file_operations dvb_demux_fops
= {
996 .owner
= THIS_MODULE
,
997 .read
= dvb_demux_read
,
998 .ioctl
= dvb_demux_ioctl
,
999 .open
= dvb_demux_open
,
1000 .release
= dvb_demux_release
,
1001 .poll
= dvb_demux_poll
,
1005 static struct dvb_device dvbdev_demux
= {
1009 .fops
= &dvb_demux_fops
1013 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
1014 unsigned int cmd
, void *parg
)
1016 struct dvb_device
*dvbdev
= file
->private_data
;
1017 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1021 if (down_interruptible (&dmxdev
->mutex
))
1022 return -ERESTARTSYS
;
1025 case DMX_SET_BUFFER_SIZE
:
1038 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1039 unsigned int cmd
, unsigned long arg
)
1041 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1045 static unsigned int dvb_dvr_poll (struct file
*file
, poll_table
*wait
)
1047 struct dvb_device
*dvbdev
= file
->private_data
;
1048 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1049 unsigned int mask
= 0;
1051 dprintk ("function : %s\n", __FUNCTION__
);
1053 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1055 if ((file
->f_flags
&O_ACCMODE
) == O_RDONLY
) {
1056 if (dmxdev
->dvr_buffer
.error
)
1057 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1059 if (dmxdev
->dvr_buffer
.pread
!=dmxdev
->dvr_buffer
.pwrite
)
1060 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1062 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1068 static struct file_operations dvb_dvr_fops
= {
1069 .owner
= THIS_MODULE
,
1070 .read
= dvb_dvr_read
,
1071 .write
= dvb_dvr_write
,
1072 .ioctl
= dvb_dvr_ioctl
,
1073 .open
= dvb_dvr_open
,
1074 .release
= dvb_dvr_release
,
1075 .poll
= dvb_dvr_poll
,
1078 static struct dvb_device dvbdev_dvr
= {
1082 .fops
= &dvb_dvr_fops
1086 dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1090 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1093 dmxdev
->filter
= vmalloc(dmxdev
->filternum
*sizeof(struct dmxdev_filter
));
1094 if (!dmxdev
->filter
)
1097 dmxdev
->dvr
= vmalloc(dmxdev
->filternum
*sizeof(struct dmxdev_dvr
));
1099 vfree(dmxdev
->filter
);
1100 dmxdev
->filter
= NULL
;
1104 sema_init(&dmxdev
->mutex
, 1);
1105 spin_lock_init(&dmxdev
->lock
);
1106 for (i
=0; i
<dmxdev
->filternum
; i
++) {
1107 dmxdev
->filter
[i
].dev
=dmxdev
;
1108 dmxdev
->filter
[i
].buffer
.data
=NULL
;
1109 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
], DMXDEV_STATE_FREE
);
1110 dmxdev
->dvr
[i
].dev
=dmxdev
;
1111 dmxdev
->dvr
[i
].buffer
.data
=NULL
;
1112 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
], DMXDEV_STATE_FREE
);
1113 dvb_dmxdev_dvr_state_set(&dmxdev
->dvr
[i
], DMXDEV_STATE_FREE
);
1116 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
, DVB_DEVICE_DEMUX
);
1117 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
, dmxdev
, DVB_DEVICE_DVR
);
1119 dvb_dmxdev_buffer_init(&dmxdev
->dvr_buffer
);
1123 EXPORT_SYMBOL(dvb_dmxdev_init
);
1126 dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1128 dvb_unregister_device(dmxdev
->dvbdev
);
1129 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1131 vfree(dmxdev
->filter
);
1132 dmxdev
->filter
=NULL
;
1135 dmxdev
->demux
->close(dmxdev
->demux
);
1137 EXPORT_SYMBOL(dvb_dmxdev_release
);