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
) {
144 void *mem
= vmalloc(DVR_BUFFER_SIZE
);
146 mutex_unlock(&dmxdev
->mutex
);
149 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, mem
, DVR_BUFFER_SIZE
);
152 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
153 dmxdev
->dvr_orig_fe
= dmxdev
->demux
->frontend
;
155 if (!dmxdev
->demux
->write
) {
156 mutex_unlock(&dmxdev
->mutex
);
160 front
= get_fe(dmxdev
->demux
, DMX_MEMORY_FE
);
163 mutex_unlock(&dmxdev
->mutex
);
166 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
167 dmxdev
->demux
->connect_frontend(dmxdev
->demux
, front
);
169 mutex_unlock(&dmxdev
->mutex
);
173 static int dvb_dvr_release(struct inode
*inode
, struct file
*file
)
175 struct dvb_device
*dvbdev
= file
->private_data
;
176 struct dmxdev
*dmxdev
= dvbdev
->priv
;
178 if (mutex_lock_interruptible(&dmxdev
->mutex
))
181 if ((file
->f_flags
& O_ACCMODE
) == O_WRONLY
) {
182 dmxdev
->demux
->disconnect_frontend(dmxdev
->demux
);
183 dmxdev
->demux
->connect_frontend(dmxdev
->demux
,
184 dmxdev
->dvr_orig_fe
);
186 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
187 if (dmxdev
->dvr_buffer
.data
) {
188 void *mem
= dmxdev
->dvr_buffer
.data
;
190 spin_lock_irq(&dmxdev
->lock
);
191 dmxdev
->dvr_buffer
.data
= NULL
;
192 spin_unlock_irq(&dmxdev
->lock
);
196 mutex_unlock(&dmxdev
->mutex
);
200 static ssize_t
dvb_dvr_write(struct file
*file
, const char __user
*buf
,
201 size_t count
, loff_t
*ppos
)
203 struct dvb_device
*dvbdev
= file
->private_data
;
204 struct dmxdev
*dmxdev
= dvbdev
->priv
;
207 if (!dmxdev
->demux
->write
)
209 if ((file
->f_flags
& O_ACCMODE
) != O_WRONLY
)
211 if (mutex_lock_interruptible(&dmxdev
->mutex
))
213 ret
= dmxdev
->demux
->write(dmxdev
->demux
, buf
, count
);
214 mutex_unlock(&dmxdev
->mutex
);
218 static ssize_t
dvb_dvr_read(struct file
*file
, char __user
*buf
, size_t count
,
221 struct dvb_device
*dvbdev
= file
->private_data
;
222 struct dmxdev
*dmxdev
= dvbdev
->priv
;
225 //mutex_lock(&dmxdev->mutex);
226 ret
= dvb_dmxdev_buffer_read(&dmxdev
->dvr_buffer
,
227 file
->f_flags
& O_NONBLOCK
,
229 //mutex_unlock(&dmxdev->mutex);
233 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
234 *dmxdevfilter
, int state
)
236 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
237 dmxdevfilter
->state
= state
;
238 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
241 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter
*dmxdevfilter
,
244 struct dvb_ringbuffer
*buf
= &dmxdevfilter
->buffer
;
247 if (buf
->size
== size
)
249 if (dmxdevfilter
->state
>= DMXDEV_STATE_GO
)
251 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
255 dvb_ringbuffer_flush(buf
);
256 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
260 mem
= vmalloc(dmxdevfilter
->buffer
.size
);
263 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
265 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
270 static void dvb_dmxdev_filter_timeout(unsigned long data
)
272 struct dmxdev_filter
*dmxdevfilter
= (struct dmxdev_filter
*)data
;
274 dmxdevfilter
->buffer
.error
= -ETIMEDOUT
;
275 spin_lock_irq(&dmxdevfilter
->dev
->lock
);
276 dmxdevfilter
->state
= DMXDEV_STATE_TIMEDOUT
;
277 spin_unlock_irq(&dmxdevfilter
->dev
->lock
);
278 wake_up(&dmxdevfilter
->buffer
.queue
);
281 static void dvb_dmxdev_filter_timer(struct dmxdev_filter
*dmxdevfilter
)
283 struct dmx_sct_filter_params
*para
= &dmxdevfilter
->params
.sec
;
285 del_timer(&dmxdevfilter
->timer
);
287 dmxdevfilter
->timer
.function
= dvb_dmxdev_filter_timeout
;
288 dmxdevfilter
->timer
.data
= (unsigned long)dmxdevfilter
;
289 dmxdevfilter
->timer
.expires
=
290 jiffies
+ 1 + (HZ
/ 2 + HZ
* para
->timeout
) / 1000;
291 add_timer(&dmxdevfilter
->timer
);
295 static int dvb_dmxdev_section_callback(const u8
*buffer1
, size_t buffer1_len
,
296 const u8
*buffer2
, size_t buffer2_len
,
297 struct dmx_section_filter
*filter
,
298 enum dmx_success success
)
300 struct dmxdev_filter
*dmxdevfilter
= filter
->priv
;
303 if (dmxdevfilter
->buffer
.error
) {
304 wake_up(&dmxdevfilter
->buffer
.queue
);
307 spin_lock(&dmxdevfilter
->dev
->lock
);
308 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
) {
309 spin_unlock(&dmxdevfilter
->dev
->lock
);
312 del_timer(&dmxdevfilter
->timer
);
313 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
314 buffer1
[0], buffer1
[1],
315 buffer1
[2], buffer1
[3], buffer1
[4], buffer1
[5]);
316 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer1
,
318 if (ret
== buffer1_len
) {
319 ret
= dvb_dmxdev_buffer_write(&dmxdevfilter
->buffer
, buffer2
,
323 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
324 dmxdevfilter
->buffer
.error
= ret
;
326 if (dmxdevfilter
->params
.sec
.flags
& DMX_ONESHOT
)
327 dmxdevfilter
->state
= DMXDEV_STATE_DONE
;
328 spin_unlock(&dmxdevfilter
->dev
->lock
);
329 wake_up(&dmxdevfilter
->buffer
.queue
);
333 static int dvb_dmxdev_ts_callback(const u8
*buffer1
, size_t buffer1_len
,
334 const u8
*buffer2
, size_t buffer2_len
,
335 struct dmx_ts_feed
*feed
,
336 enum dmx_success success
)
338 struct dmxdev_filter
*dmxdevfilter
= feed
->priv
;
339 struct dvb_ringbuffer
*buffer
;
342 spin_lock(&dmxdevfilter
->dev
->lock
);
343 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_DECODER
) {
344 spin_unlock(&dmxdevfilter
->dev
->lock
);
348 if (dmxdevfilter
->params
.pes
.output
== DMX_OUT_TAP
)
349 buffer
= &dmxdevfilter
->buffer
;
351 buffer
= &dmxdevfilter
->dev
->dvr_buffer
;
353 spin_unlock(&dmxdevfilter
->dev
->lock
);
354 wake_up(&buffer
->queue
);
357 ret
= dvb_dmxdev_buffer_write(buffer
, buffer1
, buffer1_len
);
358 if (ret
== buffer1_len
)
359 ret
= dvb_dmxdev_buffer_write(buffer
, buffer2
, buffer2_len
);
361 dvb_ringbuffer_flush(buffer
);
364 spin_unlock(&dmxdevfilter
->dev
->lock
);
365 wake_up(&buffer
->queue
);
369 /* stop feed but only mark the specified filter as stopped (state set) */
370 static int dvb_dmxdev_feed_stop(struct dmxdev_filter
*dmxdevfilter
)
372 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
374 switch (dmxdevfilter
->type
) {
375 case DMXDEV_TYPE_SEC
:
376 del_timer(&dmxdevfilter
->timer
);
377 dmxdevfilter
->feed
.sec
->stop_filtering(dmxdevfilter
->feed
.sec
);
379 case DMXDEV_TYPE_PES
:
380 dmxdevfilter
->feed
.ts
->stop_filtering(dmxdevfilter
->feed
.ts
);
388 /* start feed associated with the specified filter */
389 static int dvb_dmxdev_feed_start(struct dmxdev_filter
*filter
)
391 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
393 switch (filter
->type
) {
394 case DMXDEV_TYPE_SEC
:
395 return filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
396 case DMXDEV_TYPE_PES
:
397 return filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
405 /* restart section feed if it has filters left associated with it,
406 otherwise release the feed */
407 static int dvb_dmxdev_feed_restart(struct dmxdev_filter
*filter
)
410 struct dmxdev
*dmxdev
= filter
->dev
;
411 u16 pid
= filter
->params
.sec
.pid
;
413 for (i
= 0; i
< dmxdev
->filternum
; i
++)
414 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
415 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
416 dmxdev
->filter
[i
].params
.sec
.pid
== pid
) {
417 dvb_dmxdev_feed_start(&dmxdev
->filter
[i
]);
421 filter
->dev
->demux
->release_section_feed(dmxdev
->demux
,
427 static int dvb_dmxdev_filter_stop(struct dmxdev_filter
*dmxdevfilter
)
429 if (dmxdevfilter
->state
< DMXDEV_STATE_GO
)
432 switch (dmxdevfilter
->type
) {
433 case DMXDEV_TYPE_SEC
:
434 if (!dmxdevfilter
->feed
.sec
)
436 dvb_dmxdev_feed_stop(dmxdevfilter
);
437 if (dmxdevfilter
->filter
.sec
)
438 dmxdevfilter
->feed
.sec
->
439 release_filter(dmxdevfilter
->feed
.sec
,
440 dmxdevfilter
->filter
.sec
);
441 dvb_dmxdev_feed_restart(dmxdevfilter
);
442 dmxdevfilter
->feed
.sec
= NULL
;
444 case DMXDEV_TYPE_PES
:
445 if (!dmxdevfilter
->feed
.ts
)
447 dvb_dmxdev_feed_stop(dmxdevfilter
);
448 dmxdevfilter
->dev
->demux
->
449 release_ts_feed(dmxdevfilter
->dev
->demux
,
450 dmxdevfilter
->feed
.ts
);
451 dmxdevfilter
->feed
.ts
= NULL
;
454 if (dmxdevfilter
->state
== DMXDEV_STATE_ALLOCATED
)
459 dvb_ringbuffer_flush(&dmxdevfilter
->buffer
);
463 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter
*dmxdevfilter
)
465 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
468 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
469 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
473 static int dvb_dmxdev_filter_start(struct dmxdev_filter
*filter
)
475 struct dmxdev
*dmxdev
= filter
->dev
;
479 if (filter
->state
< DMXDEV_STATE_SET
)
482 if (filter
->state
>= DMXDEV_STATE_GO
)
483 dvb_dmxdev_filter_stop(filter
);
485 if (!filter
->buffer
.data
) {
486 mem
= vmalloc(filter
->buffer
.size
);
489 spin_lock_irq(&filter
->dev
->lock
);
490 filter
->buffer
.data
= mem
;
491 spin_unlock_irq(&filter
->dev
->lock
);
494 dvb_ringbuffer_flush(&filter
->buffer
);
496 switch (filter
->type
) {
497 case DMXDEV_TYPE_SEC
:
499 struct dmx_sct_filter_params
*para
= &filter
->params
.sec
;
500 struct dmx_section_filter
**secfilter
= &filter
->filter
.sec
;
501 struct dmx_section_feed
**secfeed
= &filter
->feed
.sec
;
507 /* find active filter/feed with same PID */
508 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
509 if (dmxdev
->filter
[i
].state
>= DMXDEV_STATE_GO
&&
510 dmxdev
->filter
[i
].type
== DMXDEV_TYPE_SEC
&&
511 dmxdev
->filter
[i
].params
.sec
.pid
== para
->pid
) {
512 *secfeed
= dmxdev
->filter
[i
].feed
.sec
;
517 /* if no feed found, try to allocate new one */
519 ret
= dmxdev
->demux
->allocate_section_feed(dmxdev
->demux
,
521 dvb_dmxdev_section_callback
);
523 printk("DVB (%s): could not alloc feed\n",
528 ret
= (*secfeed
)->set(*secfeed
, para
->pid
, 32768,
529 (para
->flags
& DMX_CHECK_CRC
) ? 1 : 0);
531 printk("DVB (%s): could not set feed\n",
533 dvb_dmxdev_feed_restart(filter
);
537 dvb_dmxdev_feed_stop(filter
);
540 ret
= (*secfeed
)->allocate_filter(*secfeed
, secfilter
);
542 dvb_dmxdev_feed_restart(filter
);
543 filter
->feed
.sec
->start_filtering(*secfeed
);
544 dprintk("could not get filter\n");
548 (*secfilter
)->priv
= filter
;
550 memcpy(&((*secfilter
)->filter_value
[3]),
551 &(para
->filter
.filter
[1]), DMX_FILTER_SIZE
- 1);
552 memcpy(&(*secfilter
)->filter_mask
[3],
553 ¶
->filter
.mask
[1], DMX_FILTER_SIZE
- 1);
554 memcpy(&(*secfilter
)->filter_mode
[3],
555 ¶
->filter
.mode
[1], DMX_FILTER_SIZE
- 1);
557 (*secfilter
)->filter_value
[0] = para
->filter
.filter
[0];
558 (*secfilter
)->filter_mask
[0] = para
->filter
.mask
[0];
559 (*secfilter
)->filter_mode
[0] = para
->filter
.mode
[0];
560 (*secfilter
)->filter_mask
[1] = 0;
561 (*secfilter
)->filter_mask
[2] = 0;
565 ret
= filter
->feed
.sec
->start_filtering(filter
->feed
.sec
);
569 dvb_dmxdev_filter_timer(filter
);
572 case DMXDEV_TYPE_PES
:
574 struct timespec timeout
= { 0 };
575 struct dmx_pes_filter_params
*para
= &filter
->params
.pes
;
579 enum dmx_ts_pes ts_pes
;
580 struct dmx_ts_feed
**tsfeed
= &filter
->feed
.ts
;
582 filter
->feed
.ts
= NULL
;
583 otype
= para
->output
;
585 ts_pes
= (enum dmx_ts_pes
)para
->pes_type
;
587 if (ts_pes
< DMX_PES_OTHER
)
588 ts_type
= TS_DECODER
;
592 if (otype
== DMX_OUT_TS_TAP
)
593 ts_type
|= TS_PACKET
;
595 if (otype
== DMX_OUT_TAP
)
596 ts_type
|= TS_PAYLOAD_ONLY
| TS_PACKET
;
598 ret
= dmxdev
->demux
->allocate_ts_feed(dmxdev
->demux
,
600 dvb_dmxdev_ts_callback
);
604 (*tsfeed
)->priv
= filter
;
606 ret
= (*tsfeed
)->set(*tsfeed
, para
->pid
, ts_type
, ts_pes
,
609 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
614 ret
= filter
->feed
.ts
->start_filtering(filter
->feed
.ts
);
616 dmxdev
->demux
->release_ts_feed(dmxdev
->demux
,
627 dvb_dmxdev_filter_state_set(filter
, DMXDEV_STATE_GO
);
631 static int dvb_demux_open(struct inode
*inode
, struct file
*file
)
633 struct dvb_device
*dvbdev
= file
->private_data
;
634 struct dmxdev
*dmxdev
= dvbdev
->priv
;
636 struct dmxdev_filter
*dmxdevfilter
;
641 if (mutex_lock_interruptible(&dmxdev
->mutex
))
644 for (i
= 0; i
< dmxdev
->filternum
; i
++)
645 if (dmxdev
->filter
[i
].state
== DMXDEV_STATE_FREE
)
648 if (i
== dmxdev
->filternum
) {
649 mutex_unlock(&dmxdev
->mutex
);
653 dmxdevfilter
= &dmxdev
->filter
[i
];
654 mutex_init(&dmxdevfilter
->mutex
);
655 file
->private_data
= dmxdevfilter
;
657 dvb_ringbuffer_init(&dmxdevfilter
->buffer
, NULL
, 8192);
658 dmxdevfilter
->type
= DMXDEV_TYPE_NONE
;
659 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_ALLOCATED
);
660 dmxdevfilter
->feed
.ts
= NULL
;
661 init_timer(&dmxdevfilter
->timer
);
663 mutex_unlock(&dmxdev
->mutex
);
667 static int dvb_dmxdev_filter_free(struct dmxdev
*dmxdev
,
668 struct dmxdev_filter
*dmxdevfilter
)
670 if (mutex_lock_interruptible(&dmxdev
->mutex
))
673 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
674 mutex_unlock(&dmxdev
->mutex
);
678 dvb_dmxdev_filter_stop(dmxdevfilter
);
679 dvb_dmxdev_filter_reset(dmxdevfilter
);
681 if (dmxdevfilter
->buffer
.data
) {
682 void *mem
= dmxdevfilter
->buffer
.data
;
684 spin_lock_irq(&dmxdev
->lock
);
685 dmxdevfilter
->buffer
.data
= NULL
;
686 spin_unlock_irq(&dmxdev
->lock
);
690 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_FREE
);
691 wake_up(&dmxdevfilter
->buffer
.queue
);
692 mutex_unlock(&dmxdevfilter
->mutex
);
693 mutex_unlock(&dmxdev
->mutex
);
697 static inline void invert_mode(dmx_filter_t
*filter
)
701 for (i
= 0; i
< DMX_FILTER_SIZE
; i
++)
702 filter
->mode
[i
] ^= 0xff;
705 static int dvb_dmxdev_filter_set(struct dmxdev
*dmxdev
,
706 struct dmxdev_filter
*dmxdevfilter
,
707 struct dmx_sct_filter_params
*params
)
709 dprintk("function : %s\n", __FUNCTION__
);
711 dvb_dmxdev_filter_stop(dmxdevfilter
);
713 dmxdevfilter
->type
= DMXDEV_TYPE_SEC
;
714 memcpy(&dmxdevfilter
->params
.sec
,
715 params
, sizeof(struct dmx_sct_filter_params
));
716 invert_mode(&dmxdevfilter
->params
.sec
.filter
);
717 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
719 if (params
->flags
& DMX_IMMEDIATE_START
)
720 return dvb_dmxdev_filter_start(dmxdevfilter
);
725 static int dvb_dmxdev_pes_filter_set(struct dmxdev
*dmxdev
,
726 struct dmxdev_filter
*dmxdevfilter
,
727 struct dmx_pes_filter_params
*params
)
729 dvb_dmxdev_filter_stop(dmxdevfilter
);
731 if (params
->pes_type
> DMX_PES_OTHER
|| params
->pes_type
< 0)
734 dmxdevfilter
->type
= DMXDEV_TYPE_PES
;
735 memcpy(&dmxdevfilter
->params
, params
,
736 sizeof(struct dmx_pes_filter_params
));
738 dvb_dmxdev_filter_state_set(dmxdevfilter
, DMXDEV_STATE_SET
);
740 if (params
->flags
& DMX_IMMEDIATE_START
)
741 return dvb_dmxdev_filter_start(dmxdevfilter
);
746 static ssize_t
dvb_dmxdev_read_sec(struct dmxdev_filter
*dfil
,
747 struct file
*file
, char __user
*buf
,
748 size_t count
, loff_t
*ppos
)
753 if (dfil
->todo
<= 0) {
754 hcount
= 3 + dfil
->todo
;
757 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
758 file
->f_flags
& O_NONBLOCK
,
764 if (copy_from_user(dfil
->secheader
- dfil
->todo
, buf
, result
))
769 dfil
->todo
-= result
;
772 dfil
->todo
= ((dfil
->secheader
[1] << 8) | dfil
->secheader
[2]) & 0xfff;
776 if (count
> dfil
->todo
)
778 result
= dvb_dmxdev_buffer_read(&dfil
->buffer
,
779 file
->f_flags
& O_NONBLOCK
,
783 dfil
->todo
-= result
;
784 return (result
+ done
);
788 dvb_demux_read(struct file
*file
, char __user
*buf
, size_t count
,
791 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
794 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
))
797 if (dmxdevfilter
->type
== DMXDEV_TYPE_SEC
)
798 ret
= dvb_dmxdev_read_sec(dmxdevfilter
, file
, buf
, count
, ppos
);
800 ret
= dvb_dmxdev_buffer_read(&dmxdevfilter
->buffer
,
801 file
->f_flags
& O_NONBLOCK
,
804 mutex_unlock(&dmxdevfilter
->mutex
);
808 static int dvb_demux_do_ioctl(struct inode
*inode
, struct file
*file
,
809 unsigned int cmd
, void *parg
)
811 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
812 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
813 unsigned long arg
= (unsigned long)parg
;
816 if (mutex_lock_interruptible(&dmxdev
->mutex
))
821 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
822 mutex_unlock(&dmxdev
->mutex
);
825 if (dmxdevfilter
->state
< DMXDEV_STATE_SET
)
828 ret
= dvb_dmxdev_filter_start(dmxdevfilter
);
829 mutex_unlock(&dmxdevfilter
->mutex
);
833 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
834 mutex_unlock(&dmxdev
->mutex
);
837 ret
= dvb_dmxdev_filter_stop(dmxdevfilter
);
838 mutex_unlock(&dmxdevfilter
->mutex
);
842 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
843 mutex_unlock(&dmxdev
->mutex
);
846 ret
= dvb_dmxdev_filter_set(dmxdev
, dmxdevfilter
, parg
);
847 mutex_unlock(&dmxdevfilter
->mutex
);
850 case DMX_SET_PES_FILTER
:
851 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
852 mutex_unlock(&dmxdev
->mutex
);
855 ret
= dvb_dmxdev_pes_filter_set(dmxdev
, dmxdevfilter
, parg
);
856 mutex_unlock(&dmxdevfilter
->mutex
);
859 case DMX_SET_BUFFER_SIZE
:
860 if (mutex_lock_interruptible(&dmxdevfilter
->mutex
)) {
861 mutex_unlock(&dmxdev
->mutex
);
864 ret
= dvb_dmxdev_set_buffer_size(dmxdevfilter
, arg
);
865 mutex_unlock(&dmxdevfilter
->mutex
);
871 case DMX_GET_PES_PIDS
:
872 if (!dmxdev
->demux
->get_pes_pids
) {
876 dmxdev
->demux
->get_pes_pids(dmxdev
->demux
, parg
);
880 if (!dmxdev
->demux
->get_caps
) {
884 ret
= dmxdev
->demux
->get_caps(dmxdev
->demux
, parg
);
888 if (!dmxdev
->demux
->set_source
) {
892 ret
= dmxdev
->demux
->set_source(dmxdev
->demux
, parg
);
896 if (!dmxdev
->demux
->get_stc
) {
900 ret
= dmxdev
->demux
->get_stc(dmxdev
->demux
,
901 ((struct dmx_stc
*)parg
)->num
,
902 &((struct dmx_stc
*)parg
)->stc
,
903 &((struct dmx_stc
*)parg
)->base
);
910 mutex_unlock(&dmxdev
->mutex
);
914 static int dvb_demux_ioctl(struct inode
*inode
, struct file
*file
,
915 unsigned int cmd
, unsigned long arg
)
917 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_demux_do_ioctl
);
920 static unsigned int dvb_demux_poll(struct file
*file
, poll_table
*wait
)
922 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
923 unsigned int mask
= 0;
928 poll_wait(file
, &dmxdevfilter
->buffer
.queue
, wait
);
930 if (dmxdevfilter
->state
!= DMXDEV_STATE_GO
&&
931 dmxdevfilter
->state
!= DMXDEV_STATE_DONE
&&
932 dmxdevfilter
->state
!= DMXDEV_STATE_TIMEDOUT
)
935 if (dmxdevfilter
->buffer
.error
)
936 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
938 if (!dvb_ringbuffer_empty(&dmxdevfilter
->buffer
))
939 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
944 static int dvb_demux_release(struct inode
*inode
, struct file
*file
)
946 struct dmxdev_filter
*dmxdevfilter
= file
->private_data
;
947 struct dmxdev
*dmxdev
= dmxdevfilter
->dev
;
949 return dvb_dmxdev_filter_free(dmxdev
, dmxdevfilter
);
952 static struct file_operations dvb_demux_fops
= {
953 .owner
= THIS_MODULE
,
954 .read
= dvb_demux_read
,
955 .ioctl
= dvb_demux_ioctl
,
956 .open
= dvb_demux_open
,
957 .release
= dvb_demux_release
,
958 .poll
= dvb_demux_poll
,
961 static struct dvb_device dvbdev_demux
= {
965 .fops
= &dvb_demux_fops
968 static int dvb_dvr_do_ioctl(struct inode
*inode
, struct file
*file
,
969 unsigned int cmd
, void *parg
)
971 struct dvb_device
*dvbdev
= file
->private_data
;
972 struct dmxdev
*dmxdev
= dvbdev
->priv
;
975 if (mutex_lock_interruptible(&dmxdev
->mutex
))
979 case DMX_SET_BUFFER_SIZE
:
988 mutex_unlock(&dmxdev
->mutex
);
992 static int dvb_dvr_ioctl(struct inode
*inode
, struct file
*file
,
993 unsigned int cmd
, unsigned long arg
)
995 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_dvr_do_ioctl
);
998 static unsigned int dvb_dvr_poll(struct file
*file
, poll_table
*wait
)
1000 struct dvb_device
*dvbdev
= file
->private_data
;
1001 struct dmxdev
*dmxdev
= dvbdev
->priv
;
1002 unsigned int mask
= 0;
1004 dprintk("function : %s\n", __FUNCTION__
);
1006 poll_wait(file
, &dmxdev
->dvr_buffer
.queue
, wait
);
1008 if ((file
->f_flags
& O_ACCMODE
) == O_RDONLY
) {
1009 if (dmxdev
->dvr_buffer
.error
)
1010 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLERR
);
1012 if (!dvb_ringbuffer_empty(&dmxdev
->dvr_buffer
))
1013 mask
|= (POLLIN
| POLLRDNORM
| POLLPRI
);
1015 mask
|= (POLLOUT
| POLLWRNORM
| POLLPRI
);
1020 static struct file_operations dvb_dvr_fops
= {
1021 .owner
= THIS_MODULE
,
1022 .read
= dvb_dvr_read
,
1023 .write
= dvb_dvr_write
,
1024 .ioctl
= dvb_dvr_ioctl
,
1025 .open
= dvb_dvr_open
,
1026 .release
= dvb_dvr_release
,
1027 .poll
= dvb_dvr_poll
,
1030 static struct dvb_device dvbdev_dvr
= {
1034 .fops
= &dvb_dvr_fops
1037 int dvb_dmxdev_init(struct dmxdev
*dmxdev
, struct dvb_adapter
*dvb_adapter
)
1041 if (dmxdev
->demux
->open(dmxdev
->demux
) < 0)
1044 dmxdev
->filter
= vmalloc(dmxdev
->filternum
* sizeof(struct dmxdev_filter
));
1045 if (!dmxdev
->filter
)
1048 mutex_init(&dmxdev
->mutex
);
1049 spin_lock_init(&dmxdev
->lock
);
1050 for (i
= 0; i
< dmxdev
->filternum
; i
++) {
1051 dmxdev
->filter
[i
].dev
= dmxdev
;
1052 dmxdev
->filter
[i
].buffer
.data
= NULL
;
1053 dvb_dmxdev_filter_state_set(&dmxdev
->filter
[i
],
1057 dvb_register_device(dvb_adapter
, &dmxdev
->dvbdev
, &dvbdev_demux
, dmxdev
,
1059 dvb_register_device(dvb_adapter
, &dmxdev
->dvr_dvbdev
, &dvbdev_dvr
,
1060 dmxdev
, DVB_DEVICE_DVR
);
1062 dvb_ringbuffer_init(&dmxdev
->dvr_buffer
, NULL
, 8192);
1067 EXPORT_SYMBOL(dvb_dmxdev_init
);
1069 void dvb_dmxdev_release(struct dmxdev
*dmxdev
)
1071 dvb_unregister_device(dmxdev
->dvbdev
);
1072 dvb_unregister_device(dmxdev
->dvr_dvbdev
);
1074 vfree(dmxdev
->filter
);
1075 dmxdev
->filter
= NULL
;
1076 dmxdev
->demux
->close(dmxdev
->demux
);
1079 EXPORT_SYMBOL(dvb_dmxdev_release
);