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>
32 #include <asm/system.h>
37 module_param(debug
, int, 0644);
38 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
40 #define dprintk if (debug) printk
42 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer
*buf
,
43 const u8
*src
, size_t len
)
52 free
= dvb_ringbuffer_free(buf
);
54 dprintk("dmxdev: buffer overflow\n");
58 return dvb_ringbuffer_write(buf
, src
, len
);
61 static ssize_t
dvb_dmxdev_buffer_read(struct dvb_ringbuffer
*src
,
62 int non_blocking
, char __user
*buf
,
63 size_t count
, loff_t
*ppos
)
74 dvb_ringbuffer_flush(src
);
78 for (todo
= count
; todo
> 0; todo
-= ret
) {
79 if (non_blocking
&& dvb_ringbuffer_empty(src
)) {
84 ret
= wait_event_interruptible(src
->queue
,
85 !dvb_ringbuffer_empty(src
) ||
92 dvb_ringbuffer_flush(src
);
96 avail
= dvb_ringbuffer_avail(src
);
100 ret
= dvb_ringbuffer_read_user(src
, buf
, avail
);
107 return (count
- todo
) ? (count
- todo
) : ret
;
110 static struct dmx_frontend
*get_fe(struct dmx_demux
*demux
, int type
)
112 struct list_head
*head
, *pos
;
114 head
= demux
->get_frontends(demux
);
117 list_for_each(pos
, head
)
118 if (DMX_FE_ENTRY(pos
)->source
== type
)
119 return DMX_FE_ENTRY(pos
);
124 static int dvb_dvr_open(struct inode
*inode
, struct file
*file
)
126 struct dvb_device
*dvbdev
= file
->private_data
;
127 struct dmxdev
*dmxdev
= dvbdev
->priv
;
128 struct dmx_frontend
*front
;
130 dprintk("function : %s\n", __func__
);
132 if (mutex_lock_interruptible(&dmxdev
->mutex
))
136 mutex_unlock(&dmxdev
->mutex
);
140 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
141 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
142 mutex_unlock(&dmxdev
->mutex
);
147 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
149 if (!dvbdev
->readers
) {
150 mutex_unlock(&dmxdev
->mutex
);
153 mem
= vmalloc(DVR_BUFFER_SIZE
);
155 mutex_unlock(&dmxdev
->mutex
);
158 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
162 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
163 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
165 if (!dmxdev
->demux
->write
) {
166 mutex_unlock(&dmxdev
->mutex
);
170 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
173 mutex_unlock(&dmxdev
->mutex
);
176 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
177 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
180 mutex_unlock(&dmxdev
->mutex
);
184 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
186 struct dvb_device
*dvbdev
= file
->private_data
;
187 struct dmxdev
*dmxdev
= dvbdev
->priv
;
189 mutex_lock(&dmxdev
->mutex
);
191 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
192 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
193 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
194 dmxdev
->dvr_orig_fe
);
196 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
198 if (dmxdev
->dvr_buffer
.data
) {
199 void *mem
= dmxdev
->dvr_buffer
.data
;
201 spin_lock_irq(&dmxdev
->lock
);
202 dmxdev
->dvr_buffer
.data
= NULL
;
203 spin_unlock_irq(&dmxdev
->lock
);
209 if(dvbdev
->users
==-1 && dmxdev
->exit
==1) {
210 fops_put(file
->f_op
);
212 mutex_unlock(&dmxdev
->mutex
);
213 wake_up(&dvbdev
->wait_queue
);
215 mutex_unlock(&dmxdev
->mutex
);
220 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
221 size_t count
, loff_t
*ppos
)
223 struct dvb_device
*dvbdev
= file
->private_data
;
224 struct dmxdev
*dmxdev
= dvbdev
->priv
;
227 if (!dmxdev
->demux
->write
)
229 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
231 if (mutex_lock_interruptible(&dmxdev
->mutex
))
235 mutex_unlock(&dmxdev
->mutex
);
238 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
239 mutex_unlock(&dmxdev
->mutex
);
243 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
246 struct dvb_device
*dvbdev
= file
->private_data
;
247 struct dmxdev
*dmxdev
= dvbdev
->priv
;
252 return dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
253 file
->f_flags
& O_NONBLOCK
,
257 static int dvb_dvr_set_buffer_size(struct dmxdev
*dmxdev
,
260 struct dvb_ringbuffer
*buf
= &dmxdev
->dvr_buffer
;
264 dprintk("function : %s\n", __func__
);
266 if (buf
->size
== size
)
271 newmem
= vmalloc(size
);
277 spin_lock_irq(&dmxdev
->lock
);
281 /* reset and not flush in case the buffer shrinks */
282 dvb_ringbuffer_reset(buf
);
283 spin_unlock_irq(&dmxdev
->lock
);
290 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
291 *dmxdevfilter
, int state
)
293 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
294 dmxdevfilter
->state
= state
;
295 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
298 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
301 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
305 if (buf
->size
== size
)
309 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
312 newmem
= vmalloc(size
);
318 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
322 /* reset and not flush in case the buffer shrinks */
323 dvb_ringbuffer_reset(buf
);
324 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
331 static void dvb_dmxdev_filter_timeout(unsigned long data
)
333 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
335 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
336 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
337 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
338 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
339 wake_up(&dmxdevfilter
->buffer
.queue
);
342 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
344 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
346 del_timer(&dmxdevfilter
->timer
);
348 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
349 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
350 dmxdevfilter
->timer
.expires
=
351 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
352 add_timer(&dmxdevfilter
->timer
);
356 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
357 const u8
*buffer2
, size_t buffer2_len
,
358 struct dmx_section_filter
*filter
,
359 enum dmx_success success
)
361 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
364 if (dmxdevfilter
->buffer
.error
) {
365 wake_up(&dmxdevfilter
->buffer
.queue
);
368 spin_lock(&dmxdevfilter
->dev
->lock
);
369 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
370 spin_unlock(&dmxdevfilter
->dev
->lock
);
373 del_timer(&dmxdevfilter
->timer
);
374 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
375 buffer1
[0], buffer1
[1],
376 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
377 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
379 if (ret
== buffer1_len
) {
380 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
384 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
385 dmxdevfilter
->buffer
.error
= ret
;
387 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
388 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
389 spin_unlock(&dmxdevfilter
->dev
->lock
);
390 wake_up(&dmxdevfilter
->buffer
.queue
);
394 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
395 const u8
*buffer2
, size_t buffer2_len
,
396 struct dmx_ts_feed
*feed
,
397 enum dmx_success success
)
399 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
400 struct dvb_ringbuffer
*buffer
;
403 spin_lock(&dmxdevfilter
->dev
->lock
);
404 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
405 spin_unlock(&dmxdevfilter
->dev
->lock
);
409 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
410 || dmxdevfilter
->params
.pes
.output
== DMX_OUT_TSDEMUX_TAP
)
411 buffer
= &dmxdevfilter
->buffer
;
413 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
415 spin_unlock(&dmxdevfilter
->dev
->lock
);
416 wake_up(&buffer
->queue
);
419 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
420 if (ret
== buffer1_len
)
421 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
423 dvb_ringbuffer_flush(buffer
);
426 spin_unlock(&dmxdevfilter
->dev
->lock
);
427 wake_up(&buffer
->queue
);
431 /* stop feed but only mark the specified filter as stopped (state set) */
432 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
434 struct dmxdev_feed
*feed
;
436 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
438 switch (dmxdevfilter
->type
) {
439 case DMXDEV_TYPE_SEC
:
440 del_timer(&dmxdevfilter
->timer
);
441 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
443 case DMXDEV_TYPE_PES
:
444 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
)
445 feed
->ts
->stop_filtering(feed
->ts
);
453 /* start feed associated with the specified filter */
454 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
456 struct dmxdev_feed
*feed
;
459 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
461 switch (filter
->type
) {
462 case DMXDEV_TYPE_SEC
:
463 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
464 case DMXDEV_TYPE_PES
:
465 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
466 ret
= feed
->ts
->start_filtering(feed
->ts
);
468 dvb_dmxdev_feed_stop(filter
);
480 /* restart section feed if it has filters left associated with it,
481 otherwise release the feed */
482 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
485 struct dmxdev
*dmxdev
= filter
->dev
;
486 u16 pid
= filter
->params
.sec
.pid
;
488 for (i
= 0; i
< dmxdev
->filternum
; i
++)
489 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
490 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
491 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
492 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
496 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
502 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
504 struct dmxdev_feed
*feed
;
505 struct dmx_demux
*demux
;
507 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
510 switch (dmxdevfilter
->type
) {
511 case DMXDEV_TYPE_SEC
:
512 if (!dmxdevfilter
->feed
.sec
)
514 dvb_dmxdev_feed_stop(dmxdevfilter
);
515 if (dmxdevfilter
->filter
.sec
)
516 dmxdevfilter
->feed
.sec
->
517 release_filter(dmxdevfilter
->feed
.sec
,
518 dmxdevfilter
->filter
.sec
);
519 dvb_dmxdev_feed_restart(dmxdevfilter
);
520 dmxdevfilter
->feed
.sec
= NULL
;
522 case DMXDEV_TYPE_PES
:
523 dvb_dmxdev_feed_stop(dmxdevfilter
);
524 demux
= dmxdevfilter
->dev
->demux
;
525 list_for_each_entry(feed
, &dmxdevfilter
->feed
.ts
, next
) {
526 demux
->release_ts_feed(demux
, feed
->ts
);
531 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
536 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
540 static void dvb_dmxdev_delete_pids(struct dmxdev_filter
*dmxdevfilter
)
542 struct dmxdev_feed
*feed
, *tmp
;
544 /* delete all PIDs */
545 list_for_each_entry_safe(feed
, tmp
, &dmxdevfilter
->feed
.ts
, next
) {
546 list_del(&feed
->next
);
550 BUG_ON(!list_empty(&dmxdevfilter
->feed
.ts
));
553 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
555 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
558 if (dmxdevfilter
->type
== DMXDEV_TYPE_PES
)
559 dvb_dmxdev_delete_pids(dmxdevfilter
);
561 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
562 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
566 static int dvb_dmxdev_start_feed(struct dmxdev
*dmxdev
,
567 struct dmxdev_filter
*filter
,
568 struct dmxdev_feed
*feed
)
570 struct timespec timeout
= { 0 };
571 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
575 enum dmx_ts_pes ts_pes
;
576 struct dmx_ts_feed
*tsfeed
;
579 otype
= para
->output
;
581 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
583 if (ts_pes
< DMX_PES_OTHER
)
584 ts_type
= TS_DECODER
;
588 if (otype
== DMX_OUT_TS_TAP
)
589 ts_type
|= TS_PACKET
;
590 else if (otype
== DMX_OUT_TSDEMUX_TAP
)
591 ts_type
|= TS_PACKET
| TS_DEMUX
;
592 else if (otype
== DMX_OUT_TAP
)
593 ts_type
|= TS_PACKET
| TS_DEMUX
| TS_PAYLOAD_ONLY
;
595 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
, &feed
->ts
,
596 dvb_dmxdev_ts_callback
);
601 tsfeed
->priv
= filter
;
603 ret
= tsfeed
->set(tsfeed
, feed
->pid
, ts_type
, ts_pes
, 32768, timeout
);
605 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
609 ret
= tsfeed
->start_filtering(tsfeed
);
611 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
, tsfeed
);
618 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
620 struct dmxdev
*dmxdev
= filter
->dev
;
621 struct dmxdev_feed
*feed
;
625 if (filter
->state
< DMXDEV_STATE_SET
)
628 if (filter
->state
>= DMXDEV_STATE_GO
)
629 dvb_dmxdev_filter_stop(filter
);
631 if (!filter
->buffer
.data
) {
632 mem
= vmalloc(filter
->buffer
.size
);
635 spin_lock_irq(&filter
->dev
->lock
);
636 filter
->buffer
.data
= mem
;
637 spin_unlock_irq(&filter
->dev
->lock
);
640 dvb_ringbuffer_flush(&filter
->buffer
);
642 switch (filter
->type
) {
643 case DMXDEV_TYPE_SEC
:
645 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
646 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
647 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
653 /* find active filter/feed with same PID */
654 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
655 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
656 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
657 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
658 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
663 /* if no feed found, try to allocate new one */
665 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
667 dvb_dmxdev_section_callback
);
669 printk("DVB (%s): could not alloc feed\n",
674 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
675 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
677 printk("DVB (%s): could not set feed\n",
679 dvb_dmxdev_feed_restart(filter
);
683 dvb_dmxdev_feed_stop(filter
);
686 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
688 dvb_dmxdev_feed_restart(filter
);
689 filter
->feed
.sec
->start_filtering(*secfeed
);
690 dprintk("could not get filter\n");
694 (*secfilter
)->priv
= filter
;
696 memcpy(&((*secfilter
)->filter_value
[3]),
697 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
698 memcpy(&(*secfilter
)->filter_mask
[3],
699 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
700 memcpy(&(*secfilter
)->filter_mode
[3],
701 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
703 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
704 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
705 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
706 (*secfilter
)->filter_mask
[1] = 0;
707 (*secfilter
)->filter_mask
[2] = 0;
711 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
715 dvb_dmxdev_filter_timer(filter
);
718 case DMXDEV_TYPE_PES
:
719 list_for_each_entry(feed
, &filter
->feed
.ts
, next
) {
720 ret
= dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
722 dvb_dmxdev_filter_stop(filter
);
731 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
735 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
737 struct dvb_device
*dvbdev
= file
->private_data
;
738 struct dmxdev
*dmxdev
= dvbdev
->priv
;
740 struct dmxdev_filter
*dmxdevfilter
;
745 if (mutex_lock_interruptible(&dmxdev
->mutex
))
748 for (i
= 0; i
< dmxdev
->filternum
; i
++)
749 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
752 if (i
== dmxdev
->filternum
) {
753 mutex_unlock(&dmxdev
->mutex
);
757 dmxdevfilter
= &dmxdev
->filter
[i
];
758 mutex_init(&dmxdevfilter
->mutex
);
759 file
->private_data
= dmxdevfilter
;
761 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
762 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
763 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
764 init_timer(&dmxdevfilter
->timer
);
768 mutex_unlock(&dmxdev
->mutex
);
772 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
773 struct dmxdev_filter
*dmxdevfilter
)
775 mutex_lock(&dmxdev
->mutex
);
776 mutex_lock(&dmxdevfilter
->mutex
);
778 dvb_dmxdev_filter_stop(dmxdevfilter
);
779 dvb_dmxdev_filter_reset(dmxdevfilter
);
781 if (dmxdevfilter
->buffer
.data
) {
782 void *mem
= dmxdevfilter
->buffer
.data
;
784 spin_lock_irq(&dmxdev
->lock
);
785 dmxdevfilter
->buffer
.data
= NULL
;
786 spin_unlock_irq(&dmxdev
->lock
);
790 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
791 wake_up(&dmxdevfilter
->buffer
.queue
);
792 mutex_unlock(&dmxdevfilter
->mutex
);
793 mutex_unlock(&dmxdev
->mutex
);
797 static inline void invert_mode(dmx_filter_t
*filter
)
801 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
802 filter
->mode
[i
] ^= 0xff;
805 static int dvb_dmxdev_add_pid(struct dmxdev
*dmxdev
,
806 struct dmxdev_filter
*filter
, u16 pid
)
808 struct dmxdev_feed
*feed
;
810 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
811 (filter
->state
< DMXDEV_STATE_SET
))
814 /* only TS packet filters may have multiple PIDs */
815 if ((filter
->params
.pes
.output
!= DMX_OUT_TSDEMUX_TAP
) &&
816 (!list_empty(&filter
->feed
.ts
)))
819 feed
= kzalloc(sizeof(struct dmxdev_feed
), GFP_KERNEL
);
824 list_add(&feed
->next
, &filter
->feed
.ts
);
826 if (filter
->state
>= DMXDEV_STATE_GO
)
827 return dvb_dmxdev_start_feed(dmxdev
, filter
, feed
);
832 static int dvb_dmxdev_remove_pid(struct dmxdev
*dmxdev
,
833 struct dmxdev_filter
*filter
, u16 pid
)
835 struct dmxdev_feed
*feed
, *tmp
;
837 if ((filter
->type
!= DMXDEV_TYPE_PES
) ||
838 (filter
->state
< DMXDEV_STATE_SET
))
841 list_for_each_entry_safe(feed
, tmp
, &filter
->feed
.ts
, next
) {
842 if ((feed
->pid
== pid
) && (feed
->ts
!= NULL
)) {
843 feed
->ts
->stop_filtering(feed
->ts
);
844 filter
->dev
->demux
->release_ts_feed(filter
->dev
->demux
,
846 list_del(&feed
->next
);
854 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
855 struct dmxdev_filter
*dmxdevfilter
,
856 struct dmx_sct_filter_params
*params
)
858 dprintk("function : %s\n", __func__
);
860 dvb_dmxdev_filter_stop(dmxdevfilter
);
862 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
863 memcpy(&dmxdevfilter
->params
.sec
,
864 params
, sizeof(struct dmx_sct_filter_params
));
865 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
866 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
868 if (params
->flags
& DMX_IMMEDIATE_START
)
869 return dvb_dmxdev_filter_start(dmxdevfilter
);
874 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
875 struct dmxdev_filter
*dmxdevfilter
,
876 struct dmx_pes_filter_params
*params
)
880 dvb_dmxdev_filter_stop(dmxdevfilter
);
881 dvb_dmxdev_filter_reset(dmxdevfilter
);
883 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
886 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
887 memcpy(&dmxdevfilter
->params
, params
,
888 sizeof(struct dmx_pes_filter_params
));
889 INIT_LIST_HEAD(&dmxdevfilter
->feed
.ts
);
891 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
893 ret
= dvb_dmxdev_add_pid(dmxdev
, dmxdevfilter
,
894 dmxdevfilter
->params
.pes
.pid
);
898 if (params
->flags
& DMX_IMMEDIATE_START
)
899 return dvb_dmxdev_filter_start(dmxdevfilter
);
904 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
905 struct file
*file
, char __user
*buf
,
906 size_t count
, loff_t
*ppos
)
911 if (dfil
->todo
<= 0) {
912 hcount
= 3 + dfil
->todo
;
915 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
916 file
->f_flags
& O_NONBLOCK
,
922 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
927 dfil
->todo
-= result
;
930 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
934 if (count
> dfil
->todo
)
936 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
937 file
->f_flags
& O_NONBLOCK
,
941 dfil
->todo
-= result
;
942 return (result
+ done
);
946 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
949 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
952 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
955 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
956 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
958 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
959 file
->f_flags
& O_NONBLOCK
,
962 mutex_unlock(&dmxdevfilter
->mutex
);
966 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
967 unsigned int cmd
, void *parg
)
969 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
970 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
971 unsigned long arg
= (unsigned long)parg
;
974 if (mutex_lock_interruptible(&dmxdev
->mutex
))
979 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
980 mutex_unlock(&dmxdev
->mutex
);
983 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
986 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
987 mutex_unlock(&dmxdevfilter
->mutex
);
991 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
992 mutex_unlock(&dmxdev
->mutex
);
995 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
996 mutex_unlock(&dmxdevfilter
->mutex
);
1000 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1001 mutex_unlock(&dmxdev
->mutex
);
1002 return -ERESTARTSYS
;
1004 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
1005 mutex_unlock(&dmxdevfilter
->mutex
);
1008 case DMX_SET_PES_FILTER
:
1009 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1010 mutex_unlock(&dmxdev
->mutex
);
1011 return -ERESTARTSYS
;
1013 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
1014 mutex_unlock(&dmxdevfilter
->mutex
);
1017 case DMX_SET_BUFFER_SIZE
:
1018 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
1019 mutex_unlock(&dmxdev
->mutex
);
1020 return -ERESTARTSYS
;
1022 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
1023 mutex_unlock(&dmxdevfilter
->mutex
);
1026 case DMX_GET_PES_PIDS
:
1027 if (!dmxdev
->demux
->get_pes_pids
) {
1031 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
1035 if (!dmxdev
->demux
->get_caps
) {
1039 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
1042 case DMX_SET_SOURCE
:
1043 if (!dmxdev
->demux
->set_source
) {
1047 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 int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
1088 unsigned int cmd
, unsigned long arg
)
1090 return dvb_usercopy(inode
, 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;
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 fops_put(file
->f_op
);
1131 mutex_unlock(&dmxdev
->mutex
);
1132 wake_up(&dmxdev
->dvbdev
->wait_queue
);
1134 mutex_unlock(&dmxdev
->mutex
);
1139 static const struct file_operations dvb_demux_fops
= {
1140 .owner
= THIS_MODULE
,
1141 .read
= dvb_demux_read
,
1142 .ioctl
= dvb_demux_ioctl
,
1143 .open
= dvb_demux_open
,
1144 .release
= dvb_demux_release
,
1145 .poll
= dvb_demux_poll
,
1148 static struct dvb_device dvbdev_demux
= {
1152 .fops
= &dvb_demux_fops
1155 static int dvb_dvr_do_ioctl(struct inode
*inode
, 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 int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1180 unsigned int cmd
, unsigned long arg
)
1182 return dvb_usercopy(inode
, 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 .ioctl
= dvb_dvr_ioctl
,
1212 .open
= dvb_dvr_open
,
1213 .release
= dvb_dvr_release
,
1214 .poll
= dvb_dvr_poll
,
1217 static struct dvb_device dvbdev_dvr
= {
1221 .fops
= &dvb_dvr_fops
1224 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1228 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1231 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1232 if (!dmxdev
->filter
)
1235 mutex_init(&dmxdev
->mutex
);
1236 spin_lock_init(&dmxdev
->lock
);
1237 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1238 dmxdev
->filter
[i
].dev
= dmxdev
;
1239 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1240 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1244 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1246 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1247 dmxdev
, DVB_DEVICE_DVR
);
1249 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1254 EXPORT_SYMBOL(dvb_dmxdev_init
);
1256 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1259 if (dmxdev
->dvbdev
->users
> 1) {
1260 wait_event(dmxdev
->dvbdev
->wait_queue
,
1261 dmxdev
->dvbdev
->users
==1);
1263 if (dmxdev
->dvr_dvbdev
->users
> 1) {
1264 wait_event(dmxdev
->dvr_dvbdev
->wait_queue
,
1265 dmxdev
->dvr_dvbdev
->users
==1);
1268 dvb_unregister_device(dmxdev
->dvbdev
);
1269 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1271 vfree(dmxdev
->filter
);
1272 dmxdev
->filter
= NULL
;
1273 dmxdev
->demux
->close(dmxdev
->demux
);
1276 EXPORT_SYMBOL(dvb_dmxdev_release
);