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/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/smp_lock.h>
29 #include <linux/poll.h>
30 #include <linux/ioctl.h>
31 #include <linux/wait.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
41 #define dprintk if (debug) printk
43 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
44 const u8
*src
, size_t len
)
53 free
= dvb_ringbuffer_free(buf
);
55 dprintk("dmxdev: buffer overflow\n");
59 return dvb_ringbuffer_write(buf
, src
, len
);
62 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
63 int non_blocking
, char __user
*buf
,
64 size_t count
, loff_t
*ppos
)
75 dvb_ringbuffer_flush(src
);
79 for (todo
= count
; todo
> 0; todo
-= ret
) {
80 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
85 ret
= wait_event_interruptible(src
->queue
,
86 !dvb_ringbuffer_empty(src
) ||
93 dvb_ringbuffer_flush(src
);
97 avail
= dvb_ringbuffer_avail(src
);
101 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
108 return (count
- todo
) ? (count
- todo
) : ret
;
111 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
113 struct list_head
*head
, *pos
;
115 head
= demux
->get_frontends(demux
);
118 list_for_each(pos
, head
)
119 if (DMX_FE_ENTRY(pos
)->source
== type
)
120 return DMX_FE_ENTRY(pos
);
125 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
127 struct dvb_device
*dvbdev
= file
->private_data
;
128 struct dmxdev
*dmxdev
= dvbdev
->priv
;
129 struct dmx_frontend
*front
;
131 dprintk("function : %s\n", __func__
);
133 if (mutex_lock_interruptible(&dmxdev
->mutex
))
137 mutex_unlock(&dmxdev
->mutex
);
141 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
142 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
143 mutex_unlock(&dmxdev
->mutex
);
148 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
150 if (!dvbdev
->readers
) {
151 mutex_unlock(&dmxdev
->mutex
);
154 mem
= vmalloc(DVR_BUFFER_SIZE
);
156 mutex_unlock(&dmxdev
->mutex
);
159 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
163 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
164 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
166 if (!dmxdev
->demux
->write
) {
167 mutex_unlock(&dmxdev
->mutex
);
171 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
174 mutex_unlock(&dmxdev
->mutex
);
177 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
178 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
181 mutex_unlock(&dmxdev
->mutex
);
185 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
187 struct dvb_device
*dvbdev
= file
->private_data
;
188 struct dmxdev
*dmxdev
= dvbdev
->priv
;
190 mutex_lock(&dmxdev
->mutex
);
192 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
193 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
194 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
195 dmxdev
->dvr_orig_fe
);
197 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
199 if (dmxdev
->dvr_buffer
.data
) {
200 void *mem
= dmxdev
->dvr_buffer
.data
;
202 spin_lock_irq(&dmxdev
->lock
);
203 dmxdev
->dvr_buffer
.data
= NULL
;
204 spin_unlock_irq(&dmxdev
->lock
);
210 if(dvbdev
->users
==-1 && dmxdev
->exit
==1) {
211 fops_put(file
->f_op
);
213 mutex_unlock(&dmxdev
->mutex
);
214 wake_up(&dvbdev
->wait_queue
);
216 mutex_unlock(&dmxdev
->mutex
);
221 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
222 size_t count
, loff_t
*ppos
)
224 struct dvb_device
*dvbdev
= file
->private_data
;
225 struct dmxdev
*dmxdev
= dvbdev
->priv
;
228 if (!dmxdev
->demux
->write
)
230 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
232 if (mutex_lock_interruptible(&dmxdev
->mutex
))
236 mutex_unlock(&dmxdev
->mutex
);
239 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
240 mutex_unlock(&dmxdev
->mutex
);
244 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
247 struct dvb_device
*dvbdev
= file
->private_data
;
248 struct dmxdev
*dmxdev
= dvbdev
->priv
;
253 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
254 file
->f_flags
& O_NONBLOCK
,
258 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
261 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
265 dprintk("function : %s\n", __func__
);
267 if (buf
->size
== size
)
272 newmem
= vmalloc(size
);
278 spin_lock_irq(&dmxdev
->lock
);
282 /* reset and not flush in case the buffer shrinks */
283 dvb_ringbuffer_reset(buf
);
284 spin_unlock_irq(&dmxdev
->lock
);
291 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
292 *dmxdevfilter
, int state
)
294 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
295 dmxdevfilter
->state
= state
;
296 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
299 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
302 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
306 if (buf
->size
== size
)
310 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
313 newmem
= vmalloc(size
);
319 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
323 /* reset and not flush in case the buffer shrinks */
324 dvb_ringbuffer_reset(buf
);
325 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
332 static void dvb_dmxdev_filter_timeout(unsigned long data
)
334 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
336 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
337 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
338 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
339 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
340 wake_up(&dmxdevfilter
->buffer
.queue
);
343 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
345 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
347 del_timer(&dmxdevfilter
->timer
);
349 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
350 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
351 dmxdevfilter
->timer
.expires
=
352 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
353 add_timer(&dmxdevfilter
->timer
);
357 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
358 const u8
*buffer2
, size_t buffer2_len
,
359 struct dmx_section_filter
*filter
,
360 enum dmx_success success
)
362 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
365 if (dmxdevfilter
->buffer
.error
) {
366 wake_up(&dmxdevfilter
->buffer
.queue
);
369 spin_lock(&dmxdevfilter
->dev
->lock
);
370 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
371 spin_unlock(&dmxdevfilter
->dev
->lock
);
374 del_timer(&dmxdevfilter
->timer
);
375 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
376 buffer1
[0], buffer1
[1],
377 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
378 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
380 if (ret
== buffer1_len
) {
381 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
385 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
386 dmxdevfilter
->buffer
.error
= ret
;
388 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
389 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
390 spin_unlock(&dmxdevfilter
->dev
->lock
);
391 wake_up(&dmxdevfilter
->buffer
.queue
);
395 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
396 const u8
*buffer2
, size_t buffer2_len
,
397 struct dmx_ts_feed
*feed
,
398 enum dmx_success success
)
400 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
401 struct dvb_ringbuffer
*buffer
;
404 spin_lock(&dmxdevfilter
->dev
->lock
);
405 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
406 spin_unlock(&dmxdevfilter
->dev
->lock
);
410 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
411 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
412 buffer
= &dmxdevfilter
->buffer
;
414 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
416 spin_unlock(&dmxdevfilter
->dev
->lock
);
417 wake_up(&buffer
->queue
);
420 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
421 if (ret
== buffer1_len
)
422 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
424 dvb_ringbuffer_flush(buffer
);
427 spin_unlock(&dmxdevfilter
->dev
->lock
);
428 wake_up(&buffer
->queue
);
432 /* stop feed but only mark the specified filter as stopped (state set) */
433 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
435 struct dmxdev_feed
*feed
;
437 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
439 switch (dmxdevfilter
->type
) {
440 case DMXDEV_TYPE_SEC
:
441 del_timer(&dmxdevfilter
->timer
);
442 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
444 case DMXDEV_TYPE_PES
:
445 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
446 feed
->ts
->stop_filtering(feed
->ts
);
454 /* start feed associated with the specified filter */
455 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
457 struct dmxdev_feed
*feed
;
460 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
462 switch (filter
->type
) {
463 case DMXDEV_TYPE_SEC
:
464 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
465 case DMXDEV_TYPE_PES
:
466 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
467 ret
= feed
->ts
->start_filtering(feed
->ts
);
469 dvb_dmxdev_feed_stop(filter
);
481 /* restart section feed if it has filters left associated with it,
482 otherwise release the feed */
483 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
486 struct dmxdev
*dmxdev
= filter
->dev
;
487 u16 pid
= filter
->params
.sec
.pid
;
489 for (i
= 0; i
< dmxdev
->filternum
; i
++)
490 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
491 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
492 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
493 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
497 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
503 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
505 struct dmxdev_feed
*feed
;
506 struct dmx_demux
*demux
;
508 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
511 switch (dmxdevfilter
->type
) {
512 case DMXDEV_TYPE_SEC
:
513 if (!dmxdevfilter
->feed
.sec
)
515 dvb_dmxdev_feed_stop(dmxdevfilter
);
516 if (dmxdevfilter
->filter
.sec
)
517 dmxdevfilter
->feed
.sec
->
518 release_filter(dmxdevfilter
->feed
.sec
,
519 dmxdevfilter
->filter
.sec
);
520 dvb_dmxdev_feed_restart(dmxdevfilter
);
521 dmxdevfilter
->feed
.sec
= NULL
;
523 case DMXDEV_TYPE_PES
:
524 dvb_dmxdev_feed_stop(dmxdevfilter
);
525 demux
= dmxdevfilter
->dev
->demux
;
526 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
527 demux
->release_ts_feed(demux
, feed
->ts
);
532 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
537 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
541 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
543 struct dmxdev_feed
*feed
, *tmp
;
545 /* delete all PIDs */
546 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
547 list_del(&feed
->next
);
551 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
554 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
556 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
559 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
560 dvb_dmxdev_delete_pids(dmxdevfilter
);
562 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
563 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
567 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
568 struct dmxdev_filter
*filter
,
569 struct dmxdev_feed
*feed
)
571 struct timespec timeout
= { 0 };
572 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
576 enum dmx_ts_pes ts_pes
;
577 struct dmx_ts_feed
*tsfeed
;
580 otype
= para
->output
;
582 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
584 if (ts_pes
< DMX_PES_OTHER
)
585 ts_type
= TS_DECODER
;
589 if (otype
== DMX_OUT_TS_TAP
)
590 ts_type
|= TS_PACKET
;
591 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
592 ts_type
|= TS_PACKET
| TS_DEMUX
;
593 else if (otype
== DMX_OUT_TAP
)
594 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
596 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
597 dvb_dmxdev_ts_callback
);
602 tsfeed
->priv
= filter
;
604 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, 32768, timeout
);
606 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
610 ret
= tsfeed
->start_filtering(tsfeed
);
612 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
619 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
621 struct dmxdev
*dmxdev
= filter
->dev
;
622 struct dmxdev_feed
*feed
;
626 if (filter
->state
< DMXDEV_STATE_SET
)
629 if (filter
->state
>= DMXDEV_STATE_GO
)
630 dvb_dmxdev_filter_stop(filter
);
632 if (!filter
->buffer
.data
) {
633 mem
= vmalloc(filter
->buffer
.size
);
636 spin_lock_irq(&filter
->dev
->lock
);
637 filter
->buffer
.data
= mem
;
638 spin_unlock_irq(&filter
->dev
->lock
);
641 dvb_ringbuffer_flush(&filter
->buffer
);
643 switch (filter
->type
) {
644 case DMXDEV_TYPE_SEC
:
646 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
647 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
648 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
654 /* find active filter/feed with same PID */
655 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
656 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
657 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
658 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
659 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
664 /* if no feed found, try to allocate new one */
666 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
668 dvb_dmxdev_section_callback
);
670 printk("DVB (%s): could not alloc feed\n",
675 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
676 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
678 printk("DVB (%s): could not set feed\n",
680 dvb_dmxdev_feed_restart(filter
);
684 dvb_dmxdev_feed_stop(filter
);
687 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
689 dvb_dmxdev_feed_restart(filter
);
690 filter
->feed
.sec
->start_filtering(*secfeed
);
691 dprintk("could not get filter\n");
695 (*secfilter
)->priv
= filter
;
697 memcpy(&((*secfilter
)->filter_value
[3]),
698 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
699 memcpy(&(*secfilter
)->filter_mask
[3],
700 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
701 memcpy(&(*secfilter
)->filter_mode
[3],
702 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
704 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
705 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
706 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
707 (*secfilter
)->filter_mask
[1] = 0;
708 (*secfilter
)->filter_mask
[2] = 0;
712 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
716 dvb_dmxdev_filter_timer(filter
);
719 case DMXDEV_TYPE_PES
:
720 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
721 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
723 dvb_dmxdev_filter_stop(filter
);
732 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
736 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
738 struct dvb_device
*dvbdev
= file
->private_data
;
739 struct dmxdev
*dmxdev
= dvbdev
->priv
;
741 struct dmxdev_filter
*dmxdevfilter
;
746 if (mutex_lock_interruptible(&dmxdev
->mutex
))
749 for (i
= 0; i
< dmxdev
->filternum
; i
++)
750 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
753 if (i
== dmxdev
->filternum
) {
754 mutex_unlock(&dmxdev
->mutex
);
758 dmxdevfilter
= &dmxdev
->filter
[i
];
759 mutex_init(&dmxdevfilter
->mutex
);
760 file
->private_data
= dmxdevfilter
;
762 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
763 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
764 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
765 init_timer(&dmxdevfilter
->timer
);
769 mutex_unlock(&dmxdev
->mutex
);
773 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
774 struct dmxdev_filter
*dmxdevfilter
)
776 mutex_lock(&dmxdev
->mutex
);
777 mutex_lock(&dmxdevfilter
->mutex
);
779 dvb_dmxdev_filter_stop(dmxdevfilter
);
780 dvb_dmxdev_filter_reset(dmxdevfilter
);
782 if (dmxdevfilter
->buffer
.data
) {
783 void *mem
= dmxdevfilter
->buffer
.data
;
785 spin_lock_irq(&dmxdev
->lock
);
786 dmxdevfilter
->buffer
.data
= NULL
;
787 spin_unlock_irq(&dmxdev
->lock
);
791 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
792 wake_up(&dmxdevfilter
->buffer
.queue
);
793 mutex_unlock(&dmxdevfilter
->mutex
);
794 mutex_unlock(&dmxdev
->mutex
);
798 static inline void invert_mode(dmx_filter_t
*filter
)
802 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
803 filter
->mode
[i
] ^= 0xff;
806 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
807 struct dmxdev_filter
*filter
, u16 pid
)
809 struct dmxdev_feed
*feed
;
811 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
812 (filter
->state
< DMXDEV_STATE_SET
))
815 /* only TS packet filters may have multiple PIDs */
816 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
817 (!list_empty(&filter
->feed
.ts
)))
820 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
825 list_add(&feed
->next
, &filter
->feed
.ts
);
827 if (filter
->state
>= DMXDEV_STATE_GO
)
828 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
833 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
834 struct dmxdev_filter
*filter
, u16 pid
)
836 struct dmxdev_feed
*feed
, *tmp
;
838 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
839 (filter
->state
< DMXDEV_STATE_SET
))
842 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
843 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
844 feed
->ts
->stop_filtering(feed
->ts
);
845 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
847 list_del(&feed
->next
);
855 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
856 struct dmxdev_filter
*dmxdevfilter
,
857 struct dmx_sct_filter_params
*params
)
859 dprintk("function : %s\n", __func__
);
861 dvb_dmxdev_filter_stop(dmxdevfilter
);
863 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
864 memcpy(&dmxdevfilter
->params
.sec
,
865 params
, sizeof(struct dmx_sct_filter_params
));
866 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
867 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
869 if (params
->flags
& DMX_IMMEDIATE_START
)
870 return dvb_dmxdev_filter_start(dmxdevfilter
);
875 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
876 struct dmxdev_filter
*dmxdevfilter
,
877 struct dmx_pes_filter_params
*params
)
881 dvb_dmxdev_filter_stop(dmxdevfilter
);
882 dvb_dmxdev_filter_reset(dmxdevfilter
);
884 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
887 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
888 memcpy(&dmxdevfilter
->params
, params
,
889 sizeof(struct dmx_pes_filter_params
));
890 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
892 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
894 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
895 dmxdevfilter
->params
.pes
.pid
);
899 if (params
->flags
& DMX_IMMEDIATE_START
)
900 return dvb_dmxdev_filter_start(dmxdevfilter
);
905 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
906 struct file
*file
, char __user
*buf
,
907 size_t count
, loff_t
*ppos
)
912 if (dfil
->todo
<= 0) {
913 hcount
= 3 + dfil
->todo
;
916 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
917 file
->f_flags
& O_NONBLOCK
,
923 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
928 dfil
->todo
-= result
;
931 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
935 if (count
> dfil
->todo
)
937 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
938 file
->f_flags
& O_NONBLOCK
,
942 dfil
->todo
-= result
;
943 return (result
+ done
);
947 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
950 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
953 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
956 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
957 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
959 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
960 file
->f_flags
& O_NONBLOCK
,
963 mutex_unlock(&dmxdevfilter
->mutex
);
967 static int dvb_demux_do_ioctl(struct file
*file
,
968 unsigned int cmd
, void *parg
)
970 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
971 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
972 unsigned long arg
= (unsigned long)parg
;
975 if (mutex_lock_interruptible(&dmxdev
->mutex
))
980 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
981 mutex_unlock(&dmxdev
->mutex
);
984 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
987 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
988 mutex_unlock(&dmxdevfilter
->mutex
);
992 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
993 mutex_unlock(&dmxdev
->mutex
);
996 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
997 mutex_unlock(&dmxdevfilter
->mutex
);
1000 case DMX_SET_FILTER
:
1001 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1002 mutex_unlock(&dmxdev
->mutex
);
1003 return -ERESTARTSYS
;
1005 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1006 mutex_unlock(&dmxdevfilter
->mutex
);
1009 case DMX_SET_PES_FILTER
:
1010 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1011 mutex_unlock(&dmxdev
->mutex
);
1012 return -ERESTARTSYS
;
1014 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1015 mutex_unlock(&dmxdevfilter
->mutex
);
1018 case DMX_SET_BUFFER_SIZE
:
1019 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1020 mutex_unlock(&dmxdev
->mutex
);
1021 return -ERESTARTSYS
;
1023 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1024 mutex_unlock(&dmxdevfilter
->mutex
);
1027 case DMX_GET_PES_PIDS
:
1028 if (!dmxdev
->demux
->get_pes_pids
) {
1032 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1036 if (!dmxdev
->demux
->get_caps
) {
1040 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
1043 case DMX_SET_SOURCE
:
1044 if (!dmxdev
->demux
->set_source
) {
1048 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
1052 if (!dmxdev
->demux
->get_stc
) {
1056 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1057 ((struct dmx_stc
*)parg
)->num
,
1058 &((struct dmx_stc
*)parg
)->stc
,
1059 &((struct dmx_stc
*)parg
)->base
);
1063 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1067 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1068 mutex_unlock(&dmxdevfilter
->mutex
);
1071 case DMX_REMOVE_PID
:
1072 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1076 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1077 mutex_unlock(&dmxdevfilter
->mutex
);
1084 mutex_unlock(&dmxdev
->mutex
);
1088 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1094 ret
= dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1100 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1102 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1103 unsigned int mask
= 0;
1108 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1110 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1111 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1112 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1115 if (dmxdevfilter
->buffer
.error
)
1116 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1118 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1119 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1124 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1126 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1127 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1131 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1133 mutex_lock(&dmxdev
->mutex
);
1134 dmxdev
->dvbdev
->users
--;
1135 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1136 fops_put(file
->f_op
);
1138 mutex_unlock(&dmxdev
->mutex
);
1139 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1141 mutex_unlock(&dmxdev
->mutex
);
1146 static const struct file_operations dvb_demux_fops
= {
1147 .owner
= THIS_MODULE
,
1148 .read
= dvb_demux_read
,
1149 .unlocked_ioctl
= dvb_demux_ioctl
,
1150 .open
= dvb_demux_open
,
1151 .release
= dvb_demux_release
,
1152 .poll
= dvb_demux_poll
,
1155 static struct dvb_device dvbdev_demux
= {
1159 .fops
= &dvb_demux_fops
1162 static int dvb_dvr_do_ioctl(struct file
*file
,
1163 unsigned int cmd
, void *parg
)
1165 struct dvb_device
*dvbdev
= file
->private_data
;
1166 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1167 unsigned long arg
= (unsigned long)parg
;
1170 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1171 return -ERESTARTSYS
;
1174 case DMX_SET_BUFFER_SIZE
:
1175 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1182 mutex_unlock(&dmxdev
->mutex
);
1186 static long dvb_dvr_ioctl(struct file
*file
,
1187 unsigned int cmd
, unsigned long arg
)
1192 ret
= dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1198 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1200 struct dvb_device
*dvbdev
= file
->private_data
;
1201 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1202 unsigned int mask
= 0;
1204 dprintk("function : %s\n", __func__
);
1206 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1208 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1209 if (dmxdev
->dvr_buffer
.error
)
1210 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1212 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1213 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1215 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1220 static const struct file_operations dvb_dvr_fops
= {
1221 .owner
= THIS_MODULE
,
1222 .read
= dvb_dvr_read
,
1223 .write
= dvb_dvr_write
,
1224 .unlocked_ioctl
= dvb_dvr_ioctl
,
1225 .open
= dvb_dvr_open
,
1226 .release
= dvb_dvr_release
,
1227 .poll
= dvb_dvr_poll
,
1230 static struct dvb_device dvbdev_dvr
= {
1234 .fops
= &dvb_dvr_fops
1237 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1241 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1244 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1245 if (!dmxdev
->filter
)
1248 mutex_init(&dmxdev
->mutex
);
1249 spin_lock_init(&dmxdev
->lock
);
1250 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1251 dmxdev
->filter
[i
].dev
= dmxdev
;
1252 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1253 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1257 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1259 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1260 dmxdev
, DVB_DEVICE_DVR
);
1262 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1267 EXPORT_SYMBOL(dvb_dmxdev_init
);
1269 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1272 if (dmxdev
->dvbdev
->users
> 1) {
1273 wait_event(dmxdev
->dvbdev
->wait_queue
,
1274 dmxdev
->dvbdev
->users
==1);
1276 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1277 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1278 dmxdev
->dvr_dvbdev
->users
==1);
1281 dvb_unregister_device(dmxdev
->dvbdev
);
1282 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1284 vfree(dmxdev
->filter
);
1285 dmxdev
->filter
= NULL
;
1286 dmxdev
->demux
->close(dmxdev
->demux
);
1289 EXPORT_SYMBOL(dvb_dmxdev_release
);