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 #define pr_fmt(fmt) "dmxdev: " fmt
25 #include <linux/sched.h>
26 #include <linux/spinlock.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/module.h>
30 #include <linux/poll.h>
31 #include <linux/ioctl.h>
32 #include <linux/wait.h>
33 #include <linux/uaccess.h>
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
41 #define dprintk(fmt, arg...) do { \
43 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
47 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
48 const u8
*src
, size_t len
)
57 free
= dvb_ringbuffer_free(buf
);
59 dprintk("buffer overflow\n");
63 return dvb_ringbuffer_write(buf
, src
, len
);
66 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
67 int non_blocking
, char __user
*buf
,
68 size_t count
, loff_t
*ppos
)
79 dvb_ringbuffer_flush(src
);
83 for (todo
= count
; todo
> 0; todo
-= ret
) {
84 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
89 ret
= wait_event_interruptible(src
->queue
,
90 !dvb_ringbuffer_empty(src
) ||
97 dvb_ringbuffer_flush(src
);
101 avail
= dvb_ringbuffer_avail(src
);
105 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
112 return (count
- todo
) ? (count
- todo
) : ret
;
115 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
117 struct list_head
*head
, *pos
;
119 head
= demux
->get_frontends(demux
);
122 list_for_each(pos
, head
)
123 if (DMX_FE_ENTRY(pos
)->source
== type
)
124 return DMX_FE_ENTRY(pos
);
129 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
131 struct dvb_device
*dvbdev
= file
->private_data
;
132 struct dmxdev
*dmxdev
= dvbdev
->priv
;
133 struct dmx_frontend
*front
;
135 dprintk("%s\n", __func__
);
137 if (mutex_lock_interruptible(&dmxdev
->mutex
))
141 mutex_unlock(&dmxdev
->mutex
);
145 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
146 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
147 mutex_unlock(&dmxdev
->mutex
);
152 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
154 if (!dvbdev
->readers
) {
155 mutex_unlock(&dmxdev
->mutex
);
158 mem
= vmalloc(DVR_BUFFER_SIZE
);
160 mutex_unlock(&dmxdev
->mutex
);
163 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
167 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
168 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
170 if (!dmxdev
->demux
->write
) {
171 mutex_unlock(&dmxdev
->mutex
);
175 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
178 mutex_unlock(&dmxdev
->mutex
);
181 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
182 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
185 mutex_unlock(&dmxdev
->mutex
);
189 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
191 struct dvb_device
*dvbdev
= file
->private_data
;
192 struct dmxdev
*dmxdev
= dvbdev
->priv
;
194 mutex_lock(&dmxdev
->mutex
);
196 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
197 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
198 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
199 dmxdev
->dvr_orig_fe
);
201 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
203 if (dmxdev
->dvr_buffer
.data
) {
204 void *mem
= dmxdev
->dvr_buffer
.data
;
206 spin_lock_irq(&dmxdev
->lock
);
207 dmxdev
->dvr_buffer
.data
= NULL
;
208 spin_unlock_irq(&dmxdev
->lock
);
214 if (dvbdev
->users
== 1 && dmxdev
->exit
== 1) {
215 mutex_unlock(&dmxdev
->mutex
);
216 wake_up(&dvbdev
->wait_queue
);
218 mutex_unlock(&dmxdev
->mutex
);
223 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
224 size_t count
, loff_t
*ppos
)
226 struct dvb_device
*dvbdev
= file
->private_data
;
227 struct dmxdev
*dmxdev
= dvbdev
->priv
;
230 if (!dmxdev
->demux
->write
)
232 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
234 if (mutex_lock_interruptible(&dmxdev
->mutex
))
238 mutex_unlock(&dmxdev
->mutex
);
241 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
242 mutex_unlock(&dmxdev
->mutex
);
246 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
249 struct dvb_device
*dvbdev
= file
->private_data
;
250 struct dmxdev
*dmxdev
= dvbdev
->priv
;
255 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
256 file
->f_flags
& O_NONBLOCK
,
260 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
263 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
267 dprintk("%s\n", __func__
);
269 if (buf
->size
== size
)
274 newmem
= vmalloc(size
);
280 spin_lock_irq(&dmxdev
->lock
);
284 /* reset and not flush in case the buffer shrinks */
285 dvb_ringbuffer_reset(buf
);
286 spin_unlock_irq(&dmxdev
->lock
);
293 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
294 *dmxdevfilter
, int state
)
296 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
297 dmxdevfilter
->state
= state
;
298 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
301 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
304 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
308 if (buf
->size
== size
)
312 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
315 newmem
= vmalloc(size
);
321 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
325 /* reset and not flush in case the buffer shrinks */
326 dvb_ringbuffer_reset(buf
);
327 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
334 static void dvb_dmxdev_filter_timeout(unsigned long data
)
336 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
338 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
339 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
340 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
341 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
342 wake_up(&dmxdevfilter
->buffer
.queue
);
345 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
347 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
349 del_timer(&dmxdevfilter
->timer
);
351 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
352 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
353 dmxdevfilter
->timer
.expires
=
354 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
355 add_timer(&dmxdevfilter
->timer
);
359 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
360 const u8
*buffer2
, size_t buffer2_len
,
361 struct dmx_section_filter
*filter
)
363 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
366 if (dmxdevfilter
->buffer
.error
) {
367 wake_up(&dmxdevfilter
->buffer
.queue
);
370 spin_lock(&dmxdevfilter
->dev
->lock
);
371 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
372 spin_unlock(&dmxdevfilter
->dev
->lock
);
375 del_timer(&dmxdevfilter
->timer
);
376 dprintk("section callback %*ph\n", 6, buffer1
);
377 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
379 if (ret
== buffer1_len
) {
380 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
384 dmxdevfilter
->buffer
.error
= ret
;
385 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
386 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
387 spin_unlock(&dmxdevfilter
->dev
->lock
);
388 wake_up(&dmxdevfilter
->buffer
.queue
);
392 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
393 const u8
*buffer2
, size_t buffer2_len
,
394 struct dmx_ts_feed
*feed
)
396 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
397 struct dvb_ringbuffer
*buffer
;
400 spin_lock(&dmxdevfilter
->dev
->lock
);
401 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
402 spin_unlock(&dmxdevfilter
->dev
->lock
);
406 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
407 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
408 buffer
= &dmxdevfilter
->buffer
;
410 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
412 spin_unlock(&dmxdevfilter
->dev
->lock
);
413 wake_up(&buffer
->queue
);
416 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
417 if (ret
== buffer1_len
)
418 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
421 spin_unlock(&dmxdevfilter
->dev
->lock
);
422 wake_up(&buffer
->queue
);
426 /* stop feed but only mark the specified filter as stopped (state set) */
427 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
429 struct dmxdev_feed
*feed
;
431 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
433 switch (dmxdevfilter
->type
) {
434 case DMXDEV_TYPE_SEC
:
435 del_timer(&dmxdevfilter
->timer
);
436 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
438 case DMXDEV_TYPE_PES
:
439 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
440 feed
->ts
->stop_filtering(feed
->ts
);
448 /* start feed associated with the specified filter */
449 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
451 struct dmxdev_feed
*feed
;
454 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
456 switch (filter
->type
) {
457 case DMXDEV_TYPE_SEC
:
458 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
459 case DMXDEV_TYPE_PES
:
460 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
461 ret
= feed
->ts
->start_filtering(feed
->ts
);
463 dvb_dmxdev_feed_stop(filter
);
475 /* restart section feed if it has filters left associated with it,
476 otherwise release the feed */
477 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
480 struct dmxdev
*dmxdev
= filter
->dev
;
481 u16 pid
= filter
->params
.sec
.pid
;
483 for (i
= 0; i
< dmxdev
->filternum
; i
++)
484 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
485 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
486 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
487 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
491 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
497 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
499 struct dmxdev_feed
*feed
;
500 struct dmx_demux
*demux
;
502 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
505 switch (dmxdevfilter
->type
) {
506 case DMXDEV_TYPE_SEC
:
507 if (!dmxdevfilter
->feed
.sec
)
509 dvb_dmxdev_feed_stop(dmxdevfilter
);
510 if (dmxdevfilter
->filter
.sec
)
511 dmxdevfilter
->feed
.sec
->
512 release_filter(dmxdevfilter
->feed
.sec
,
513 dmxdevfilter
->filter
.sec
);
514 dvb_dmxdev_feed_restart(dmxdevfilter
);
515 dmxdevfilter
->feed
.sec
= NULL
;
517 case DMXDEV_TYPE_PES
:
518 dvb_dmxdev_feed_stop(dmxdevfilter
);
519 demux
= dmxdevfilter
->dev
->demux
;
520 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
521 demux
->release_ts_feed(demux
, feed
->ts
);
526 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
531 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
535 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
537 struct dmxdev_feed
*feed
, *tmp
;
539 /* delete all PIDs */
540 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
541 list_del(&feed
->next
);
545 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
548 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
550 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
553 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
554 dvb_dmxdev_delete_pids(dmxdevfilter
);
556 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
557 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
561 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
562 struct dmxdev_filter
*filter
,
563 struct dmxdev_feed
*feed
)
566 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
570 enum dmx_ts_pes ts_pes
;
571 struct dmx_ts_feed
*tsfeed
;
574 otype
= para
->output
;
576 ts_pes
= para
->pes_type
;
578 if (ts_pes
< DMX_PES_OTHER
)
579 ts_type
= TS_DECODER
;
583 if (otype
== DMX_OUT_TS_TAP
)
584 ts_type
|= TS_PACKET
;
585 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
586 ts_type
|= TS_PACKET
| TS_DEMUX
;
587 else if (otype
== DMX_OUT_TAP
)
588 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
590 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
591 dvb_dmxdev_ts_callback
);
596 tsfeed
->priv
= filter
;
598 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, timeout
);
600 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
604 ret
= tsfeed
->start_filtering(tsfeed
);
606 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
613 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
615 struct dmxdev
*dmxdev
= filter
->dev
;
616 struct dmxdev_feed
*feed
;
620 if (filter
->state
< DMXDEV_STATE_SET
)
623 if (filter
->state
>= DMXDEV_STATE_GO
)
624 dvb_dmxdev_filter_stop(filter
);
626 if (!filter
->buffer
.data
) {
627 mem
= vmalloc(filter
->buffer
.size
);
630 spin_lock_irq(&filter
->dev
->lock
);
631 filter
->buffer
.data
= mem
;
632 spin_unlock_irq(&filter
->dev
->lock
);
635 dvb_ringbuffer_flush(&filter
->buffer
);
637 switch (filter
->type
) {
638 case DMXDEV_TYPE_SEC
:
640 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
641 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
642 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
648 /* find active filter/feed with same PID */
649 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
650 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
651 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
652 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
653 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
658 /* if no feed found, try to allocate new one */
660 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
662 dvb_dmxdev_section_callback
);
664 pr_err("DVB (%s): could not alloc feed\n",
669 ret
= (*secfeed
)->set(*secfeed
, para
->pid
,
670 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
672 pr_err("DVB (%s): could not set feed\n",
674 dvb_dmxdev_feed_restart(filter
);
678 dvb_dmxdev_feed_stop(filter
);
681 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
683 dvb_dmxdev_feed_restart(filter
);
684 filter
->feed
.sec
->start_filtering(*secfeed
);
685 dprintk("could not get filter\n");
689 (*secfilter
)->priv
= filter
;
691 memcpy(&((*secfilter
)->filter_value
[3]),
692 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
693 memcpy(&(*secfilter
)->filter_mask
[3],
694 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
695 memcpy(&(*secfilter
)->filter_mode
[3],
696 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
698 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
699 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
700 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
701 (*secfilter
)->filter_mask
[1] = 0;
702 (*secfilter
)->filter_mask
[2] = 0;
706 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
710 dvb_dmxdev_filter_timer(filter
);
713 case DMXDEV_TYPE_PES
:
714 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
715 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
717 dvb_dmxdev_filter_stop(filter
);
726 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
730 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
732 struct dvb_device
*dvbdev
= file
->private_data
;
733 struct dmxdev
*dmxdev
= dvbdev
->priv
;
735 struct dmxdev_filter
*dmxdevfilter
;
740 if (mutex_lock_interruptible(&dmxdev
->mutex
))
743 for (i
= 0; i
< dmxdev
->filternum
; i
++)
744 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
747 if (i
== dmxdev
->filternum
) {
748 mutex_unlock(&dmxdev
->mutex
);
752 dmxdevfilter
= &dmxdev
->filter
[i
];
753 mutex_init(&dmxdevfilter
->mutex
);
754 file
->private_data
= dmxdevfilter
;
756 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
757 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
758 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
759 init_timer(&dmxdevfilter
->timer
);
763 mutex_unlock(&dmxdev
->mutex
);
767 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
768 struct dmxdev_filter
*dmxdevfilter
)
770 mutex_lock(&dmxdev
->mutex
);
771 mutex_lock(&dmxdevfilter
->mutex
);
773 dvb_dmxdev_filter_stop(dmxdevfilter
);
774 dvb_dmxdev_filter_reset(dmxdevfilter
);
776 if (dmxdevfilter
->buffer
.data
) {
777 void *mem
= dmxdevfilter
->buffer
.data
;
779 spin_lock_irq(&dmxdev
->lock
);
780 dmxdevfilter
->buffer
.data
= NULL
;
781 spin_unlock_irq(&dmxdev
->lock
);
785 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
786 wake_up(&dmxdevfilter
->buffer
.queue
);
787 mutex_unlock(&dmxdevfilter
->mutex
);
788 mutex_unlock(&dmxdev
->mutex
);
792 static inline void invert_mode(dmx_filter_t
*filter
)
796 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
797 filter
->mode
[i
] ^= 0xff;
800 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
801 struct dmxdev_filter
*filter
, u16 pid
)
803 struct dmxdev_feed
*feed
;
805 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
806 (filter
->state
< DMXDEV_STATE_SET
))
809 /* only TS packet filters may have multiple PIDs */
810 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
811 (!list_empty(&filter
->feed
.ts
)))
814 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
819 list_add(&feed
->next
, &filter
->feed
.ts
);
821 if (filter
->state
>= DMXDEV_STATE_GO
)
822 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
827 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
828 struct dmxdev_filter
*filter
, u16 pid
)
830 struct dmxdev_feed
*feed
, *tmp
;
832 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
833 (filter
->state
< DMXDEV_STATE_SET
))
836 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
837 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
838 feed
->ts
->stop_filtering(feed
->ts
);
839 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
841 list_del(&feed
->next
);
849 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
850 struct dmxdev_filter
*dmxdevfilter
,
851 struct dmx_sct_filter_params
*params
)
853 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
854 __func__
, params
->pid
, params
->flags
, params
->timeout
);
856 dvb_dmxdev_filter_stop(dmxdevfilter
);
858 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
859 memcpy(&dmxdevfilter
->params
.sec
,
860 params
, sizeof(struct dmx_sct_filter_params
));
861 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
862 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
864 if (params
->flags
& DMX_IMMEDIATE_START
)
865 return dvb_dmxdev_filter_start(dmxdevfilter
);
870 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
871 struct dmxdev_filter
*dmxdevfilter
,
872 struct dmx_pes_filter_params
*params
)
876 dvb_dmxdev_filter_stop(dmxdevfilter
);
877 dvb_dmxdev_filter_reset(dmxdevfilter
);
879 if ((unsigned)params
->pes_type
> DMX_PES_OTHER
)
882 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
883 memcpy(&dmxdevfilter
->params
, params
,
884 sizeof(struct dmx_pes_filter_params
));
885 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
887 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
889 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
890 dmxdevfilter
->params
.pes
.pid
);
894 if (params
->flags
& DMX_IMMEDIATE_START
)
895 return dvb_dmxdev_filter_start(dmxdevfilter
);
900 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
901 struct file
*file
, char __user
*buf
,
902 size_t count
, loff_t
*ppos
)
907 if (dfil
->todo
<= 0) {
908 hcount
= 3 + dfil
->todo
;
911 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
912 file
->f_flags
& O_NONBLOCK
,
918 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
923 dfil
->todo
-= result
;
926 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
930 if (count
> dfil
->todo
)
932 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
933 file
->f_flags
& O_NONBLOCK
,
937 dfil
->todo
-= result
;
938 return (result
+ done
);
942 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
945 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
948 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
951 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
952 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
954 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
955 file
->f_flags
& O_NONBLOCK
,
958 mutex_unlock(&dmxdevfilter
->mutex
);
962 static int dvb_demux_do_ioctl(struct file
*file
,
963 unsigned int cmd
, void *parg
)
965 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
966 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
967 unsigned long arg
= (unsigned long)parg
;
970 if (mutex_lock_interruptible(&dmxdev
->mutex
))
975 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
976 mutex_unlock(&dmxdev
->mutex
);
979 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
982 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
983 mutex_unlock(&dmxdevfilter
->mutex
);
987 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
988 mutex_unlock(&dmxdev
->mutex
);
991 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
992 mutex_unlock(&dmxdevfilter
->mutex
);
996 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
997 mutex_unlock(&dmxdev
->mutex
);
1000 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1001 mutex_unlock(&dmxdevfilter
->mutex
);
1004 case DMX_SET_PES_FILTER
:
1005 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1006 mutex_unlock(&dmxdev
->mutex
);
1007 return -ERESTARTSYS
;
1009 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1010 mutex_unlock(&dmxdevfilter
->mutex
);
1013 case DMX_SET_BUFFER_SIZE
:
1014 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1015 mutex_unlock(&dmxdev
->mutex
);
1016 return -ERESTARTSYS
;
1018 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1019 mutex_unlock(&dmxdevfilter
->mutex
);
1022 case DMX_GET_PES_PIDS
:
1023 if (!dmxdev
->demux
->get_pes_pids
) {
1027 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1031 /* Not used upstream and never documented */
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
);
1051 if (!dmxdev
->demux
->get_stc
) {
1055 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
1056 ((struct dmx_stc
*)parg
)->num
,
1057 &((struct dmx_stc
*)parg
)->stc
,
1058 &((struct dmx_stc
*)parg
)->base
);
1062 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1066 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1067 mutex_unlock(&dmxdevfilter
->mutex
);
1070 case DMX_REMOVE_PID
:
1071 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1075 ret
= dvb_dmxdev_remove_pid(dmxdev
, dmxdevfilter
, *(u16
*)parg
);
1076 mutex_unlock(&dmxdevfilter
->mutex
);
1083 mutex_unlock(&dmxdev
->mutex
);
1087 static long dvb_demux_ioctl(struct file
*file
, unsigned int cmd
,
1090 return dvb_usercopy(file
, cmd
, arg
, dvb_demux_do_ioctl
);
1093 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
1095 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1096 unsigned int mask
= 0;
1098 if ((!dmxdevfilter
) || dmxdevfilter
->dev
->exit
)
1101 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
1103 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
1104 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
1105 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
1108 if (dmxdevfilter
->buffer
.error
)
1109 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1111 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
1112 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1117 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
1119 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
1120 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
1124 ret
= dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
1126 mutex_lock(&dmxdev
->mutex
);
1127 dmxdev
->dvbdev
->users
--;
1128 if(dmxdev
->dvbdev
->users
==1 && dmxdev
->exit
==1) {
1129 mutex_unlock(&dmxdev
->mutex
);
1130 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1132 mutex_unlock(&dmxdev
->mutex
);
1137 static const struct file_operations dvb_demux_fops
= {
1138 .owner
= THIS_MODULE
,
1139 .read
= dvb_demux_read
,
1140 .unlocked_ioctl
= dvb_demux_ioctl
,
1141 .open
= dvb_demux_open
,
1142 .release
= dvb_demux_release
,
1143 .poll
= dvb_demux_poll
,
1144 .llseek
= default_llseek
,
1147 static const struct dvb_device dvbdev_demux
= {
1151 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1152 .name
= "dvb-demux",
1154 .fops
= &dvb_demux_fops
1157 static int dvb_dvr_do_ioctl(struct file
*file
,
1158 unsigned int cmd
, void *parg
)
1160 struct dvb_device
*dvbdev
= file
->private_data
;
1161 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1162 unsigned long arg
= (unsigned long)parg
;
1165 if (mutex_lock_interruptible(&dmxdev
->mutex
))
1166 return -ERESTARTSYS
;
1169 case DMX_SET_BUFFER_SIZE
:
1170 ret
= dvb_dvr_set_buffer_size(dmxdev
, arg
);
1177 mutex_unlock(&dmxdev
->mutex
);
1181 static long dvb_dvr_ioctl(struct file
*file
,
1182 unsigned int cmd
, unsigned long arg
)
1184 return dvb_usercopy(file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1187 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1189 struct dvb_device
*dvbdev
= file
->private_data
;
1190 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1191 unsigned int mask
= 0;
1193 dprintk("%s\n", __func__
);
1198 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1200 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1201 if (dmxdev
->dvr_buffer
.error
)
1202 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1204 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1205 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1207 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1212 static const struct file_operations dvb_dvr_fops
= {
1213 .owner
= THIS_MODULE
,
1214 .read
= dvb_dvr_read
,
1215 .write
= dvb_dvr_write
,
1216 .unlocked_ioctl
= dvb_dvr_ioctl
,
1217 .open
= dvb_dvr_open
,
1218 .release
= dvb_dvr_release
,
1219 .poll
= dvb_dvr_poll
,
1220 .llseek
= default_llseek
,
1223 static const struct dvb_device dvbdev_dvr
= {
1227 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1230 .fops
= &dvb_dvr_fops
1232 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1236 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1239 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1240 if (!dmxdev
->filter
)
1243 mutex_init(&dmxdev
->mutex
);
1244 spin_lock_init(&dmxdev
->lock
);
1245 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1246 dmxdev
->filter
[i
].dev
= dmxdev
;
1247 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1248 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1252 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1253 DVB_DEVICE_DEMUX
, dmxdev
->filternum
);
1254 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1255 dmxdev
, DVB_DEVICE_DVR
, dmxdev
->filternum
);
1257 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1262 EXPORT_SYMBOL(dvb_dmxdev_init
);
1264 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1267 if (dmxdev
->dvbdev
->users
> 1) {
1268 wait_event(dmxdev
->dvbdev
->wait_queue
,
1269 dmxdev
->dvbdev
->users
==1);
1271 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1272 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1273 dmxdev
->dvr_dvbdev
->users
==1);
1276 dvb_unregister_device(dmxdev
->dvbdev
);
1277 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1279 vfree(dmxdev
->filter
);
1280 dmxdev
->filter
= NULL
;
1281 dmxdev
->demux
->close(dmxdev
->demux
);
1284 EXPORT_SYMBOL(dvb_dmxdev_release
);