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/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/sched.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(src
, buf
, avail
, 1);
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", __FUNCTION__
);
133 if (mutex_lock_interruptible(&dmxdev
->mutex
))
136 if ((file
->f_flags
& O_ACCMODE
) == O_RDWR
) {
137 if (!(dmxdev
->capabilities
& DMXDEV_CAP_DUPLEX
)) {
138 mutex_unlock(&dmxdev
->mutex
);
143 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
145 if (!dvbdev
->readers
) {
146 mutex_unlock(&dmxdev
->mutex
);
149 mem
= vmalloc(DVR_BUFFER_SIZE
);
151 mutex_unlock(&dmxdev
->mutex
);
154 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
158 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
159 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
161 if (!dmxdev
->demux
->write
) {
162 mutex_unlock(&dmxdev
->mutex
);
166 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
169 mutex_unlock(&dmxdev
->mutex
);
172 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
173 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
175 mutex_unlock(&dmxdev
->mutex
);
179 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
181 struct dvb_device
*dvbdev
= file
->private_data
;
182 struct dmxdev
*dmxdev
= dvbdev
->priv
;
184 if (mutex_lock_interruptible(&dmxdev
->mutex
))
187 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
188 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
189 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
190 dmxdev
->dvr_orig_fe
);
192 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
194 if (dmxdev
->dvr_buffer
.data
) {
195 void *mem
= dmxdev
->dvr_buffer
.data
;
197 spin_lock_irq(&dmxdev
->lock
);
198 dmxdev
->dvr_buffer
.data
= NULL
;
199 spin_unlock_irq(&dmxdev
->lock
);
203 mutex_unlock(&dmxdev
->mutex
);
207 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
208 size_t count
, loff_t
*ppos
)
210 struct dvb_device
*dvbdev
= file
->private_data
;
211 struct dmxdev
*dmxdev
= dvbdev
->priv
;
214 if (!dmxdev
->demux
->write
)
216 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
218 if (mutex_lock_interruptible(&dmxdev
->mutex
))
220 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
221 mutex_unlock(&dmxdev
->mutex
);
225 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
228 struct dvb_device
*dvbdev
= file
->private_data
;
229 struct dmxdev
*dmxdev
= dvbdev
->priv
;
232 //mutex_lock(&dmxdev->mutex);
233 ret
= dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
234 file
->f_flags
& O_NONBLOCK
,
236 //mutex_unlock(&dmxdev->mutex);
240 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
241 *dmxdevfilter
, int state
)
243 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
244 dmxdevfilter
->state
= state
;
245 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
248 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
251 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
254 if (buf
->size
== size
)
256 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
258 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
262 dvb_ringbuffer_flush(buf
);
263 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
267 mem
= vmalloc(dmxdevfilter
->buffer
.size
);
270 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
272 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
277 static void dvb_dmxdev_filter_timeout(unsigned long data
)
279 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
281 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
282 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
283 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
284 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
285 wake_up(&dmxdevfilter
->buffer
.queue
);
288 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
290 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
292 del_timer(&dmxdevfilter
->timer
);
294 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
295 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
296 dmxdevfilter
->timer
.expires
=
297 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
298 add_timer(&dmxdevfilter
->timer
);
302 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
303 const u8
*buffer2
, size_t buffer2_len
,
304 struct dmx_section_filter
*filter
,
305 enum dmx_success success
)
307 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
310 if (dmxdevfilter
->buffer
.error
) {
311 wake_up(&dmxdevfilter
->buffer
.queue
);
314 spin_lock(&dmxdevfilter
->dev
->lock
);
315 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
316 spin_unlock(&dmxdevfilter
->dev
->lock
);
319 del_timer(&dmxdevfilter
->timer
);
320 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
321 buffer1
[0], buffer1
[1],
322 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
323 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
325 if (ret
== buffer1_len
) {
326 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
330 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
331 dmxdevfilter
->buffer
.error
= ret
;
333 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
334 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
335 spin_unlock(&dmxdevfilter
->dev
->lock
);
336 wake_up(&dmxdevfilter
->buffer
.queue
);
340 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
341 const u8
*buffer2
, size_t buffer2_len
,
342 struct dmx_ts_feed
*feed
,
343 enum dmx_success success
)
345 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
346 struct dvb_ringbuffer
*buffer
;
349 spin_lock(&dmxdevfilter
->dev
->lock
);
350 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
351 spin_unlock(&dmxdevfilter
->dev
->lock
);
355 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
)
356 buffer
= &dmxdevfilter
->buffer
;
358 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
360 spin_unlock(&dmxdevfilter
->dev
->lock
);
361 wake_up(&buffer
->queue
);
364 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
365 if (ret
== buffer1_len
)
366 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
368 dvb_ringbuffer_flush(buffer
);
371 spin_unlock(&dmxdevfilter
->dev
->lock
);
372 wake_up(&buffer
->queue
);
376 /* stop feed but only mark the specified filter as stopped (state set) */
377 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
379 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
381 switch (dmxdevfilter
->type
) {
382 case DMXDEV_TYPE_SEC
:
383 del_timer(&dmxdevfilter
->timer
);
384 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
386 case DMXDEV_TYPE_PES
:
387 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
395 /* start feed associated with the specified filter */
396 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
398 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
400 switch (filter
->type
) {
401 case DMXDEV_TYPE_SEC
:
402 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
403 case DMXDEV_TYPE_PES
:
404 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
412 /* restart section feed if it has filters left associated with it,
413 otherwise release the feed */
414 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
417 struct dmxdev
*dmxdev
= filter
->dev
;
418 u16 pid
= filter
->params
.sec
.pid
;
420 for (i
= 0; i
< dmxdev
->filternum
; i
++)
421 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
422 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
423 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
424 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
428 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
434 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
436 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
439 switch (dmxdevfilter
->type
) {
440 case DMXDEV_TYPE_SEC
:
441 if (!dmxdevfilter
->feed
.sec
)
443 dvb_dmxdev_feed_stop(dmxdevfilter
);
444 if (dmxdevfilter
->filter
.sec
)
445 dmxdevfilter
->feed
.sec
->
446 release_filter(dmxdevfilter
->feed
.sec
,
447 dmxdevfilter
->filter
.sec
);
448 dvb_dmxdev_feed_restart(dmxdevfilter
);
449 dmxdevfilter
->feed
.sec
= NULL
;
451 case DMXDEV_TYPE_PES
:
452 if (!dmxdevfilter
->feed
.ts
)
454 dvb_dmxdev_feed_stop(dmxdevfilter
);
455 dmxdevfilter
->dev
->demux
->
456 release_ts_feed(dmxdevfilter
->dev
->demux
,
457 dmxdevfilter
->feed
.ts
);
458 dmxdevfilter
->feed
.ts
= NULL
;
461 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
466 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
470 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
472 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
475 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
476 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
480 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
482 struct dmxdev
*dmxdev
= filter
->dev
;
486 if (filter
->state
< DMXDEV_STATE_SET
)
489 if (filter
->state
>= DMXDEV_STATE_GO
)
490 dvb_dmxdev_filter_stop(filter
);
492 if (!filter
->buffer
.data
) {
493 mem
= vmalloc(filter
->buffer
.size
);
496 spin_lock_irq(&filter
->dev
->lock
);
497 filter
->buffer
.data
= mem
;
498 spin_unlock_irq(&filter
->dev
->lock
);
501 dvb_ringbuffer_flush(&filter
->buffer
);
503 switch (filter
->type
) {
504 case DMXDEV_TYPE_SEC
:
506 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
507 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
508 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
514 /* find active filter/feed with same PID */
515 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
516 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
517 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
518 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
519 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
524 /* if no feed found, try to allocate new one */
526 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
528 dvb_dmxdev_section_callback
);
530 printk("DVB (%s): could not alloc feed\n",
535 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
536 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
538 printk("DVB (%s): could not set feed\n",
540 dvb_dmxdev_feed_restart(filter
);
544 dvb_dmxdev_feed_stop(filter
);
547 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
549 dvb_dmxdev_feed_restart(filter
);
550 filter
->feed
.sec
->start_filtering(*secfeed
);
551 dprintk("could not get filter\n");
555 (*secfilter
)->priv
= filter
;
557 memcpy(&((*secfilter
)->filter_value
[3]),
558 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
559 memcpy(&(*secfilter
)->filter_mask
[3],
560 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
561 memcpy(&(*secfilter
)->filter_mode
[3],
562 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
564 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
565 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
566 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
567 (*secfilter
)->filter_mask
[1] = 0;
568 (*secfilter
)->filter_mask
[2] = 0;
572 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
576 dvb_dmxdev_filter_timer(filter
);
579 case DMXDEV_TYPE_PES
:
581 struct timespec timeout
= { 0 };
582 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
586 enum dmx_ts_pes ts_pes
;
587 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
589 filter
->feed
.ts
= NULL
;
590 otype
= para
->output
;
592 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
594 if (ts_pes
< DMX_PES_OTHER
)
595 ts_type
= TS_DECODER
;
599 if (otype
== DMX_OUT_TS_TAP
)
600 ts_type
|= TS_PACKET
;
602 if (otype
== DMX_OUT_TAP
)
603 ts_type
|= TS_PAYLOAD_ONLY
| TS_PACKET
;
605 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
607 dvb_dmxdev_ts_callback
);
611 (*tsfeed
)->priv
= filter
;
613 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
616 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
621 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
623 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
634 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
638 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
640 struct dvb_device
*dvbdev
= file
->private_data
;
641 struct dmxdev
*dmxdev
= dvbdev
->priv
;
643 struct dmxdev_filter
*dmxdevfilter
;
648 if (mutex_lock_interruptible(&dmxdev
->mutex
))
651 for (i
= 0; i
< dmxdev
->filternum
; i
++)
652 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
655 if (i
== dmxdev
->filternum
) {
656 mutex_unlock(&dmxdev
->mutex
);
660 dmxdevfilter
= &dmxdev
->filter
[i
];
661 mutex_init(&dmxdevfilter
->mutex
);
662 file
->private_data
= dmxdevfilter
;
664 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
665 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
666 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
667 dmxdevfilter
->feed
.ts
= NULL
;
668 init_timer(&dmxdevfilter
->timer
);
670 mutex_unlock(&dmxdev
->mutex
);
674 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
675 struct dmxdev_filter
*dmxdevfilter
)
677 if (mutex_lock_interruptible(&dmxdev
->mutex
))
680 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
681 mutex_unlock(&dmxdev
->mutex
);
685 dvb_dmxdev_filter_stop(dmxdevfilter
);
686 dvb_dmxdev_filter_reset(dmxdevfilter
);
688 if (dmxdevfilter
->buffer
.data
) {
689 void *mem
= dmxdevfilter
->buffer
.data
;
691 spin_lock_irq(&dmxdev
->lock
);
692 dmxdevfilter
->buffer
.data
= NULL
;
693 spin_unlock_irq(&dmxdev
->lock
);
697 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
698 wake_up(&dmxdevfilter
->buffer
.queue
);
699 mutex_unlock(&dmxdevfilter
->mutex
);
700 mutex_unlock(&dmxdev
->mutex
);
704 static inline void invert_mode(dmx_filter_t
*filter
)
708 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
709 filter
->mode
[i
] ^= 0xff;
712 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
713 struct dmxdev_filter
*dmxdevfilter
,
714 struct dmx_sct_filter_params
*params
)
716 dprintk("function : %s\n", __FUNCTION__
);
718 dvb_dmxdev_filter_stop(dmxdevfilter
);
720 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
721 memcpy(&dmxdevfilter
->params
.sec
,
722 params
, sizeof(struct dmx_sct_filter_params
));
723 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
724 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
726 if (params
->flags
& DMX_IMMEDIATE_START
)
727 return dvb_dmxdev_filter_start(dmxdevfilter
);
732 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
733 struct dmxdev_filter
*dmxdevfilter
,
734 struct dmx_pes_filter_params
*params
)
736 dvb_dmxdev_filter_stop(dmxdevfilter
);
738 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
741 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
742 memcpy(&dmxdevfilter
->params
, params
,
743 sizeof(struct dmx_pes_filter_params
));
745 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
747 if (params
->flags
& DMX_IMMEDIATE_START
)
748 return dvb_dmxdev_filter_start(dmxdevfilter
);
753 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
754 struct file
*file
, char __user
*buf
,
755 size_t count
, loff_t
*ppos
)
760 if (dfil
->todo
<= 0) {
761 hcount
= 3 + dfil
->todo
;
764 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
765 file
->f_flags
& O_NONBLOCK
,
771 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
776 dfil
->todo
-= result
;
779 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
783 if (count
> dfil
->todo
)
785 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
786 file
->f_flags
& O_NONBLOCK
,
790 dfil
->todo
-= result
;
791 return (result
+ done
);
795 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
798 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
801 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
804 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
805 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
807 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
808 file
->f_flags
& O_NONBLOCK
,
811 mutex_unlock(&dmxdevfilter
->mutex
);
815 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
816 unsigned int cmd
, void *parg
)
818 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
819 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
820 unsigned long arg
= (unsigned long)parg
;
823 if (mutex_lock_interruptible(&dmxdev
->mutex
))
828 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
829 mutex_unlock(&dmxdev
->mutex
);
832 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
835 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
836 mutex_unlock(&dmxdevfilter
->mutex
);
840 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
841 mutex_unlock(&dmxdev
->mutex
);
844 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
845 mutex_unlock(&dmxdevfilter
->mutex
);
849 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
850 mutex_unlock(&dmxdev
->mutex
);
853 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
854 mutex_unlock(&dmxdevfilter
->mutex
);
857 case DMX_SET_PES_FILTER
:
858 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
859 mutex_unlock(&dmxdev
->mutex
);
862 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
863 mutex_unlock(&dmxdevfilter
->mutex
);
866 case DMX_SET_BUFFER_SIZE
:
867 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
868 mutex_unlock(&dmxdev
->mutex
);
871 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
872 mutex_unlock(&dmxdevfilter
->mutex
);
878 case DMX_GET_PES_PIDS
:
879 if (!dmxdev
->demux
->get_pes_pids
) {
883 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
887 if (!dmxdev
->demux
->get_caps
) {
891 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
895 if (!dmxdev
->demux
->set_source
) {
899 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
903 if (!dmxdev
->demux
->get_stc
) {
907 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
908 ((struct dmx_stc
*)parg
)->num
,
909 &((struct dmx_stc
*)parg
)->stc
,
910 &((struct dmx_stc
*)parg
)->base
);
917 mutex_unlock(&dmxdev
->mutex
);
921 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
922 unsigned int cmd
, unsigned long arg
)
924 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
927 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
929 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
930 unsigned int mask
= 0;
935 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
937 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
938 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
939 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
942 if (dmxdevfilter
->buffer
.error
)
943 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
945 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
946 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
951 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
953 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
954 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
956 return dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
959 static struct file_operations dvb_demux_fops
= {
960 .owner
= THIS_MODULE
,
961 .read
= dvb_demux_read
,
962 .ioctl
= dvb_demux_ioctl
,
963 .open
= dvb_demux_open
,
964 .release
= dvb_demux_release
,
965 .poll
= dvb_demux_poll
,
968 static struct dvb_device dvbdev_demux
= {
972 .fops
= &dvb_demux_fops
975 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
976 unsigned int cmd
, void *parg
)
978 struct dvb_device
*dvbdev
= file
->private_data
;
979 struct dmxdev
*dmxdev
= dvbdev
->priv
;
982 if (mutex_lock_interruptible(&dmxdev
->mutex
))
986 case DMX_SET_BUFFER_SIZE
:
995 mutex_unlock(&dmxdev
->mutex
);
999 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
1000 unsigned int cmd
, unsigned long arg
)
1002 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
1005 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1007 struct dvb_device
*dvbdev
= file
->private_data
;
1008 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1009 unsigned int mask
= 0;
1011 dprintk("function : %s\n", __FUNCTION__
);
1013 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1015 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1016 if (dmxdev
->dvr_buffer
.error
)
1017 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1019 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1020 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1022 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1027 static struct file_operations dvb_dvr_fops
= {
1028 .owner
= THIS_MODULE
,
1029 .read
= dvb_dvr_read
,
1030 .write
= dvb_dvr_write
,
1031 .ioctl
= dvb_dvr_ioctl
,
1032 .open
= dvb_dvr_open
,
1033 .release
= dvb_dvr_release
,
1034 .poll
= dvb_dvr_poll
,
1037 static struct dvb_device dvbdev_dvr
= {
1040 .fops
= &dvb_dvr_fops
1043 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1047 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1050 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1051 if (!dmxdev
->filter
)
1054 mutex_init(&dmxdev
->mutex
);
1055 spin_lock_init(&dmxdev
->lock
);
1056 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1057 dmxdev
->filter
[i
].dev
= dmxdev
;
1058 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1059 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1063 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1065 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1066 dmxdev
, DVB_DEVICE_DVR
);
1068 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1073 EXPORT_SYMBOL(dvb_dmxdev_init
);
1075 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1077 dvb_unregister_device(dmxdev
->dvbdev
);
1078 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1080 vfree(dmxdev
->filter
);
1081 dmxdev
->filter
= NULL
;
1082 dmxdev
->demux
->close(dmxdev
->demux
);
1085 EXPORT_SYMBOL(dvb_dmxdev_release
);