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/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
36 module_param(debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
39 #define dprintk if (debug) printk
41 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
42 const u8
*src
, size_t len
)
51 free
= dvb_ringbuffer_free(buf
);
53 dprintk("dmxdev: buffer overflow\n");
57 return dvb_ringbuffer_write(buf
, src
, len
);
60 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
61 int non_blocking
, char __user
*buf
,
62 size_t count
, loff_t
*ppos
)
73 dvb_ringbuffer_flush(src
);
77 for (todo
= count
; todo
> 0; todo
-= ret
) {
78 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
83 ret
= wait_event_interruptible(src
->queue
,
84 !dvb_ringbuffer_empty(src
) ||
91 dvb_ringbuffer_flush(src
);
95 avail
= dvb_ringbuffer_avail(src
);
99 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
106 return (count
- todo
) ? (count
- todo
) : ret
;
109 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
111 struct list_head
*head
, *pos
;
113 head
= demux
->get_frontends(demux
);
116 list_for_each(pos
, head
)
117 if (DMX_FE_ENTRY(pos
)->source
== type
)
118 return DMX_FE_ENTRY(pos
);
123 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
125 struct dvb_device
*dvbdev
= file
->private_data
;
126 struct dmxdev
*dmxdev
= dvbdev
->priv
;
127 struct dmx_frontend
*front
;
129 dprintk("function : %s\n", __func__
);
131 if (mutex_lock_interruptible(&dmxdev
->mutex
))
135 mutex_unlock(&dmxdev
->mutex
);
139 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
140 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
141 mutex_unlock(&dmxdev
->mutex
);
146 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
148 if (!dvbdev
->readers
) {
149 mutex_unlock(&dmxdev
->mutex
);
152 mem
= vmalloc(DVR_BUFFER_SIZE
);
154 mutex_unlock(&dmxdev
->mutex
);
157 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
161 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
162 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
164 if (!dmxdev
->demux
->write
) {
165 mutex_unlock(&dmxdev
->mutex
);
169 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
172 mutex_unlock(&dmxdev
->mutex
);
175 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
176 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
179 mutex_unlock(&dmxdev
->mutex
);
183 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
185 struct dvb_device
*dvbdev
= file
->private_data
;
186 struct dmxdev
*dmxdev
= dvbdev
->priv
;
188 mutex_lock(&dmxdev
->mutex
);
190 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
191 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
192 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
193 dmxdev
->dvr_orig_fe
);
195 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
197 if (dmxdev
->dvr_buffer
.data
) {
198 void *mem
= dmxdev
->dvr_buffer
.data
;
200 spin_lock_irq(&dmxdev
->lock
);
201 dmxdev
->dvr_buffer
.data
= NULL
;
202 spin_unlock_irq(&dmxdev
->lock
);
208 if (dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
209 fops_put(file
->f_op
);
211 mutex_unlock(&dmxdev
->mutex
);
212 wake_up(&dvbdev
->wait_queue
);
214 mutex_unlock(&dmxdev
->mutex
);
219 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
220 size_t count
, loff_t
*ppos
)
222 struct dvb_device
*dvbdev
= file
->private_data
;
223 struct dmxdev
*dmxdev
= dvbdev
->priv
;
226 if (!dmxdev
->demux
->write
)
228 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
230 if (mutex_lock_interruptible(&dmxdev
->mutex
))
234 mutex_unlock(&dmxdev
->mutex
);
237 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
238 mutex_unlock(&dmxdev
->mutex
);
242 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
245 struct dvb_device
*dvbdev
= file
->private_data
;
246 struct dmxdev
*dmxdev
= dvbdev
->priv
;
251 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
252 file
->f_flags
& O_NONBLOCK
,
256 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
259 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
263 dprintk("function : %s\n", __func__
);
265 if (buf
->size
== size
)
270 newmem
= vmalloc(size
);
276 spin_lock_irq(&dmxdev
->lock
);
280 /* reset and not flush in case the buffer shrinks */
281 dvb_ringbuffer_reset(buf
);
282 spin_unlock_irq(&dmxdev
->lock
);
289 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 *dmxdevfilter
, int state
)
292 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
293 dmxdevfilter
->state
= state
;
294 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
297 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
300 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
304 if (buf
->size
== size
)
308 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
311 newmem
= vmalloc(size
);
317 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
321 /* reset and not flush in case the buffer shrinks */
322 dvb_ringbuffer_reset(buf
);
323 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
330 static void dvb_dmxdev_filter_timeout(unsigned long data
)
332 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
334 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
335 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
336 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
337 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
338 wake_up(&dmxdevfilter
->buffer
.queue
);
341 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
343 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
345 del_timer(&dmxdevfilter
->timer
);
347 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
348 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
349 dmxdevfilter
->timer
.expires
=
350 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
351 add_timer(&dmxdevfilter
->timer
);
355 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
356 const u8
*buffer2
, size_t buffer2_len
,
357 struct dmx_section_filter
*filter
,
358 enum dmx_success success
)
360 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
363 if (dmxdevfilter
->buffer
.error
) {
364 wake_up(&dmxdevfilter
->buffer
.queue
);
367 spin_lock(&dmxdevfilter
->dev
->lock
);
368 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
369 spin_unlock(&dmxdevfilter
->dev
->lock
);
372 del_timer(&dmxdevfilter
->timer
);
373 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
374 buffer1
[0], buffer1
[1],
375 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
376 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
378 if (ret
== buffer1_len
) {
379 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
383 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
384 dmxdevfilter
->buffer
.error
= ret
;
386 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
387 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
388 spin_unlock(&dmxdevfilter
->dev
->lock
);
389 wake_up(&dmxdevfilter
->buffer
.queue
);
393 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
394 const u8
*buffer2
, size_t buffer2_len
,
395 struct dmx_ts_feed
*feed
,
396 enum dmx_success success
)
398 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
399 struct dvb_ringbuffer
*buffer
;
402 spin_lock(&dmxdevfilter
->dev
->lock
);
403 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
404 spin_unlock(&dmxdevfilter
->dev
->lock
);
408 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
409 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
410 buffer
= &dmxdevfilter
->buffer
;
412 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
414 spin_unlock(&dmxdevfilter
->dev
->lock
);
415 wake_up(&buffer
->queue
);
418 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
419 if (ret
== buffer1_len
)
420 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
422 dvb_ringbuffer_flush(buffer
);
425 spin_unlock(&dmxdevfilter
->dev
->lock
);
426 wake_up(&buffer
->queue
);
430 /* stop feed but only mark the specified filter as stopped (state set) */
431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
433 struct dmxdev_feed
*feed
;
435 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
437 switch (dmxdevfilter
->type
) {
438 case DMXDEV_TYPE_SEC
:
439 del_timer(&dmxdevfilter
->timer
);
440 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
442 case DMXDEV_TYPE_PES
:
443 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
444 feed
->ts
->stop_filtering(feed
->ts
);
452 /* start feed associated with the specified filter */
453 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
455 struct dmxdev_feed
*feed
;
458 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
460 switch (filter
->type
) {
461 case DMXDEV_TYPE_SEC
:
462 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
463 case DMXDEV_TYPE_PES
:
464 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
465 ret
= feed
->ts
->start_filtering(feed
->ts
);
467 dvb_dmxdev_feed_stop(filter
);
479 /* restart section feed if it has filters left associated with it,
480 otherwise release the feed */
481 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
484 struct dmxdev
*dmxdev
= filter
->dev
;
485 u16 pid
= filter
->params
.sec
.pid
;
487 for (i
= 0; i
< dmxdev
->filternum
; i
++)
488 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
489 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
490 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
491 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
495 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
501 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
503 struct dmxdev_feed
*feed
;
504 struct dmx_demux
*demux
;
506 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
509 switch (dmxdevfilter
->type
) {
510 case DMXDEV_TYPE_SEC
:
511 if (!dmxdevfilter
->feed
.sec
)
513 dvb_dmxdev_feed_stop(dmxdevfilter
);
514 if (dmxdevfilter
->filter
.sec
)
515 dmxdevfilter
->feed
.sec
->
516 release_filter(dmxdevfilter
->feed
.sec
,
517 dmxdevfilter
->filter
.sec
);
518 dvb_dmxdev_feed_restart(dmxdevfilter
);
519 dmxdevfilter
->feed
.sec
= NULL
;
521 case DMXDEV_TYPE_PES
:
522 dvb_dmxdev_feed_stop(dmxdevfilter
);
523 demux
= dmxdevfilter
->dev
->demux
;
524 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
525 demux
->release_ts_feed(demux
, feed
->ts
);
530 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
535 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
539 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
541 struct dmxdev_feed
*feed
, *tmp
;
543 /* delete all PIDs */
544 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
545 list_del(&feed
->next
);
549 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
552 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
554 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
557 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
558 dvb_dmxdev_delete_pids(dmxdevfilter
);
560 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
561 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
565 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
566 struct dmxdev_filter
*filter
,
567 struct dmxdev_feed
*feed
)
569 struct timespec timeout
= { 0 };
570 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
574 dmx_pes_type_t ts_pes
;
575 struct dmx_ts_feed
*tsfeed
;
578 otype
= para
->output
;
580 ts_pes
= para
->pes_type
;
582 if (ts_pes
< DMX_PES_OTHER
)
583 ts_type
= TS_DECODER
;
587 if (otype
== DMX_OUT_TS_TAP
)
588 ts_type
|= TS_PACKET
;
589 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
590 ts_type
|= TS_PACKET
| TS_DEMUX
;
591 else if (otype
== DMX_OUT_TAP
)
592 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
594 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
595 dvb_dmxdev_ts_callback
);
600 tsfeed
->priv
= filter
;
602 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, 32768, timeout
);
604 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
608 ret
= tsfeed
->start_filtering(tsfeed
);
610 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
617 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
619 struct dmxdev
*dmxdev
= filter
->dev
;
620 struct dmxdev_feed
*feed
;
624 if (filter
->state
< DMXDEV_STATE_SET
)
627 if (filter
->state
>= DMXDEV_STATE_GO
)
628 dvb_dmxdev_filter_stop(filter
);
630 if (!filter
->buffer
.data
) {
631 mem
= vmalloc(filter
->buffer
.size
);
634 spin_lock_irq(&filter
->dev
->lock
);
635 filter
->buffer
.data
= mem
;
636 spin_unlock_irq(&filter
->dev
->lock
);
639 dvb_ringbuffer_flush(&filter
->buffer
);
641 switch (filter
->type
) {
642 case DMXDEV_TYPE_SEC
:
644 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
645 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
646 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
652 /* find active filter/feed with same PID */
653 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
654 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
655 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
656 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
657 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
662 /* if no feed found, try to allocate new one */
664 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
666 dvb_dmxdev_section_callback
);
668 printk("DVB (%s): could not alloc feed\n",
673 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
674 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
676 printk("DVB (%s): could not set feed\n",
678 dvb_dmxdev_feed_restart(filter
);
682 dvb_dmxdev_feed_stop(filter
);
685 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
687 dvb_dmxdev_feed_restart(filter
);
688 filter
->feed
.sec
->start_filtering(*secfeed
);
689 dprintk("could not get filter\n");
693 (*secfilter
)->priv
= filter
;
695 memcpy(&((*secfilter
)->filter_value
[3]),
696 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
697 memcpy(&(*secfilter
)->filter_mask
[3],
698 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
699 memcpy(&(*secfilter
)->filter_mode
[3],
700 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
702 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
703 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
704 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
705 (*secfilter
)->filter_mask
[1] = 0;
706 (*secfilter
)->filter_mask
[2] = 0;
710 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
714 dvb_dmxdev_filter_timer(filter
);
717 case DMXDEV_TYPE_PES
:
718 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
719 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
721 dvb_dmxdev_filter_stop(filter
);
730 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
734 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
736 struct dvb_device
*dvbdev
= file
->private_data
;
737 struct dmxdev
*dmxdev
= dvbdev
->priv
;
739 struct dmxdev_filter
*dmxdevfilter
;
744 if (mutex_lock_interruptible(&dmxdev
->mutex
))
747 for (i
= 0; i
< dmxdev
->filternum
; i
++)
748 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
751 if (i
== dmxdev
->filternum
) {
752 mutex_unlock(&dmxdev
->mutex
);
756 dmxdevfilter
= &dmxdev
->filter
[i
];
757 mutex_init(&dmxdevfilter
->mutex
);
758 file
->private_data
= dmxdevfilter
;
760 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
761 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
762 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
763 init_timer(&dmxdevfilter
->timer
);
767 mutex_unlock(&dmxdev
->mutex
);
771 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
772 struct dmxdev_filter
*dmxdevfilter
)
774 mutex_lock(&dmxdev
->mutex
);
775 mutex_lock(&dmxdevfilter
->mutex
);
777 dvb_dmxdev_filter_stop(dmxdevfilter
);
778 dvb_dmxdev_filter_reset(dmxdevfilter
);
780 if (dmxdevfilter
->buffer
.data
) {
781 void *mem
= dmxdevfilter
->buffer
.data
;
783 spin_lock_irq(&dmxdev
->lock
);
784 dmxdevfilter
->buffer
.data
= NULL
;
785 spin_unlock_irq(&dmxdev
->lock
);
789 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
790 wake_up(&dmxdevfilter
->buffer
.queue
);
791 mutex_unlock(&dmxdevfilter
->mutex
);
792 mutex_unlock(&dmxdev
->mutex
);
796 static inline void invert_mode(dmx_filter_t
*filter
)
800 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
801 filter
->mode
[i
] ^= 0xff;
804 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
805 struct dmxdev_filter
*filter
, u16 pid
)
807 struct dmxdev_feed
*feed
;
809 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
810 (filter
->state
< DMXDEV_STATE_SET
))
813 /* only TS packet filters may have multiple PIDs */
814 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
815 (!list_empty(&filter
->feed
.ts
)))
818 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
823 list_add(&feed
->next
, &filter
->feed
.ts
);
825 if (filter
->state
>= DMXDEV_STATE_GO
)
826 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
831 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
832 struct dmxdev_filter
*filter
, u16 pid
)
834 struct dmxdev_feed
*feed
, *tmp
;
836 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
837 (filter
->state
< DMXDEV_STATE_SET
))
840 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
841 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
842 feed
->ts
->stop_filtering(feed
->ts
);
843 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
845 list_del(&feed
->next
);
853 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
854 struct dmxdev_filter
*dmxdevfilter
,
855 struct dmx_sct_filter_params
*params
)
857 dprintk("function : %s\n", __func__
);
859 dvb_dmxdev_filter_stop(dmxdevfilter
);
861 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
862 memcpy(&dmxdevfilter
->params
.sec
,
863 params
, sizeof(struct dmx_sct_filter_params
));
864 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
865 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
867 if (params
->flags
& DMX_IMMEDIATE_START
)
868 return dvb_dmxdev_filter_start(dmxdevfilter
);
873 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
874 struct dmxdev_filter
*dmxdevfilter
,
875 struct dmx_pes_filter_params
*params
)
879 dvb_dmxdev_filter_stop(dmxdevfilter
);
880 dvb_dmxdev_filter_reset(dmxdevfilter
);
882 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
885 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
886 memcpy(&dmxdevfilter
->params
, params
,
887 sizeof(struct dmx_pes_filter_params
));
888 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
890 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
892 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
893 dmxdevfilter
->params
.pes
.pid
);
897 if (params
->flags
& DMX_IMMEDIATE_START
)
898 return dvb_dmxdev_filter_start(dmxdevfilter
);
903 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
904 struct file
*file
, char __user
*buf
,
905 size_t count
, loff_t
*ppos
)
910 if (dfil
->todo
<= 0) {
911 hcount
= 3 + dfil
->todo
;
914 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
915 file
->f_flags
& O_NONBLOCK
,
921 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
926 dfil
->todo
-= result
;
929 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
933 if (count
> dfil
->todo
)
935 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
936 file
->f_flags
& O_NONBLOCK
,
940 dfil
->todo
-= result
;
941 return (result
+ done
);
945 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
948 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
951 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
954 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
955 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
957 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
958 file
->f_flags
& O_NONBLOCK
,
961 mutex_unlock(&dmxdevfilter
->mutex
);
965 static int dvb_demux_do_ioctl(struct file
*file
,
966 unsigned int cmd
, void *parg
)
968 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
969 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
970 unsigned long arg
= (unsigned long)parg
;
973 if (mutex_lock_interruptible(&dmxdev
->mutex
))
978 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
979 mutex_unlock(&dmxdev
->mutex
);
982 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
985 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
986 mutex_unlock(&dmxdevfilter
->mutex
);
990 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
991 mutex_unlock(&dmxdev
->mutex
);
994 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
995 mutex_unlock(&dmxdevfilter
->mutex
);
999 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1000 mutex_unlock(&dmxdev
->mutex
);
1001 return -ERESTARTSYS
;
1003 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1004 mutex_unlock(&dmxdevfilter
->mutex
);
1007 case DMX_SET_PES_FILTER
:
1008 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1009 mutex_unlock(&dmxdev
->mutex
);
1010 return -ERESTARTSYS
;
1012 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1013 mutex_unlock(&dmxdevfilter
->mutex
);
1016 case DMX_SET_BUFFER_SIZE
:
1017 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1018 mutex_unlock(&dmxdev
->mutex
);
1019 return -ERESTARTSYS
;
1021 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1022 mutex_unlock(&dmxdevfilter
->mutex
);
1025 case DMX_GET_PES_PIDS
:
1026 if (!dmxdev
->demux
->get_pes_pids
) {
1030 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1034 if (!dmxdev
->demux
->get_caps
) {
1038 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
1041 case DMX_SET_SOURCE
:
1042 if (!dmxdev
->demux
->set_source
) {
1046 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
1050 if (!dmxdev
->demux
->get_stc
) {
1054 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1055 ((struct dmx_stc
*)parg
)->num
,
1056 &((struct dmx_stc
*)parg
)->stc
,
1057 &((struct dmx_stc
*)parg
)->base
);
1061 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1065 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1066 mutex_unlock(&dmxdevfilter
->mutex
);
1069 case DMX_REMOVE_PID
:
1070 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1074 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1075 mutex_unlock(&dmxdevfilter
->mutex
);
1082 mutex_unlock(&dmxdev
->mutex
);
1086 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1089 return dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1092 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1094 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1095 unsigned int mask
= 0;
1100 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1102 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1103 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1104 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1107 if (dmxdevfilter
->buffer
.error
)
1108 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1110 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1111 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1116 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1118 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1119 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1123 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1125 mutex_lock(&dmxdev
->mutex
);
1126 dmxdev
->dvbdev
->users
--;
1127 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1128 fops_put(file
->f_op
);
1130 mutex_unlock(&dmxdev
->mutex
);
1131 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1133 mutex_unlock(&dmxdev
->mutex
);
1138 static const struct file_operations dvb_demux_fops
= {
1139 .owner
= THIS_MODULE
,
1140 .read
= dvb_demux_read
,
1141 .unlocked_ioctl
= dvb_demux_ioctl
,
1142 .open
= dvb_demux_open
,
1143 .release
= dvb_demux_release
,
1144 .poll
= dvb_demux_poll
,
1145 .llseek
= default_llseek
,
1148 static struct dvb_device dvbdev_demux
= {
1152 .fops
= &dvb_demux_fops
1155 static int dvb_dvr_do_ioctl(struct file
*file
,
1156 unsigned int cmd
, void *parg
)
1158 struct dvb_device
*dvbdev
= file
->private_data
;
1159 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1160 unsigned long arg
= (unsigned long)parg
;
1163 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1164 return -ERESTARTSYS
;
1167 case DMX_SET_BUFFER_SIZE
:
1168 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1175 mutex_unlock(&dmxdev
->mutex
);
1179 static long dvb_dvr_ioctl(struct file
*file
,
1180 unsigned int cmd
, unsigned long arg
)
1182 return dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1185 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1187 struct dvb_device
*dvbdev
= file
->private_data
;
1188 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1189 unsigned int mask
= 0;
1191 dprintk("function : %s\n", __func__
);
1193 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1195 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1196 if (dmxdev
->dvr_buffer
.error
)
1197 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1199 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1200 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1202 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1207 static const struct file_operations dvb_dvr_fops
= {
1208 .owner
= THIS_MODULE
,
1209 .read
= dvb_dvr_read
,
1210 .write
= dvb_dvr_write
,
1211 .unlocked_ioctl
= dvb_dvr_ioctl
,
1212 .open
= dvb_dvr_open
,
1213 .release
= dvb_dvr_release
,
1214 .poll
= dvb_dvr_poll
,
1215 .llseek
= default_llseek
,
1218 static struct dvb_device dvbdev_dvr
= {
1222 .fops
= &dvb_dvr_fops
1225 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1229 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1232 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1233 if (!dmxdev
->filter
)
1236 mutex_init(&dmxdev
->mutex
);
1237 spin_lock_init(&dmxdev
->lock
);
1238 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1239 dmxdev
->filter
[i
].dev
= dmxdev
;
1240 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1241 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1245 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1247 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1248 dmxdev
, DVB_DEVICE_DVR
);
1250 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1255 EXPORT_SYMBOL(dvb_dmxdev_init
);
1257 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1260 if (dmxdev
->dvbdev
->users
> 1) {
1261 wait_event(dmxdev
->dvbdev
->wait_queue
,
1262 dmxdev
->dvbdev
->users
==1);
1264 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1265 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1266 dmxdev
->dvr_dvbdev
->users
==1);
1269 dvb_unregister_device(dmxdev
->dvbdev
);
1270 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1272 vfree(dmxdev
->filter
);
1273 dmxdev
->filter
= NULL
;
1274 dmxdev
->demux
->close(dmxdev
->demux
);
1277 EXPORT_SYMBOL(dvb_dmxdev_release
);