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/poll.h>
28 #include <linux/ioctl.h>
29 #include <linux/wait.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
39 #define dprintk if (debug) printk
41 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
42 const u8 *src, size_t len)
51 free = dvb_ringbuffer_free(buf);
53 dprintk("dmxdev: buffer overflow\n");
57 return dvb_ringbuffer_write(buf, src, len);
60 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
61 int non_blocking, char __user *buf,
62 size_t count, loff_t *ppos)
73 dvb_ringbuffer_flush(src);
77 for (todo = count; todo > 0; todo -= ret) {
78 if (non_blocking && dvb_ringbuffer_empty(src)) {
83 ret = wait_event_interruptible(src->queue,
84 !dvb_ringbuffer_empty(src) ||
91 dvb_ringbuffer_flush(src);
95 avail = dvb_ringbuffer_avail(src);
99 ret = dvb_ringbuffer_read_user(src, buf, avail);
106 return (count - todo) ? (count - todo) : ret;
109 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
111 struct list_head *head, *pos;
113 head = demux->get_frontends(demux);
116 list_for_each(pos, head)
117 if (DMX_FE_ENTRY(pos)->source == type)
118 return DMX_FE_ENTRY(pos);
123 static int dvb_dvr_open(struct inode *inode, struct file *file)
125 struct dvb_device *dvbdev = file->private_data;
126 struct dmxdev *dmxdev = dvbdev->priv;
127 struct dmx_frontend *front;
129 dprintk("function : %s\n", __func__);
131 if (mutex_lock_interruptible(&dmxdev->mutex))
135 mutex_unlock(&dmxdev->mutex);
139 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
141 mutex_unlock(&dmxdev->mutex);
146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148 if (!dvbdev->readers) {
149 mutex_unlock(&dmxdev->mutex);
152 mem = vmalloc(DVR_BUFFER_SIZE);
154 mutex_unlock(&dmxdev->mutex);
157 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
161 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
162 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
164 if (!dmxdev->demux->write) {
165 mutex_unlock(&dmxdev->mutex);
169 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
172 mutex_unlock(&dmxdev->mutex);
175 dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 dmxdev->demux->connect_frontend(dmxdev->demux, front);
179 mutex_unlock(&dmxdev->mutex);
183 static int dvb_dvr_release(struct inode *inode, struct file *file)
185 struct dvb_device *dvbdev = file->private_data;
186 struct dmxdev *dmxdev = dvbdev->priv;
188 mutex_lock(&dmxdev->mutex);
190 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
191 dmxdev->demux->disconnect_frontend(dmxdev->demux);
192 dmxdev->demux->connect_frontend(dmxdev->demux,
193 dmxdev->dvr_orig_fe);
195 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
197 if (dmxdev->dvr_buffer.data) {
198 void *mem = dmxdev->dvr_buffer.data;
200 spin_lock_irq(&dmxdev->lock);
201 dmxdev->dvr_buffer.data = NULL;
202 spin_unlock_irq(&dmxdev->lock);
208 if(dvbdev->users==-1 && dmxdev->exit==1) {
209 fops_put(file->f_op);
211 mutex_unlock(&dmxdev->mutex);
212 wake_up(&dvbdev->wait_queue);
214 mutex_unlock(&dmxdev->mutex);
219 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
220 size_t count, loff_t *ppos)
222 struct dvb_device *dvbdev = file->private_data;
223 struct dmxdev *dmxdev = dvbdev->priv;
226 if (!dmxdev->demux->write)
228 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
230 if (mutex_lock_interruptible(&dmxdev->mutex))
234 mutex_unlock(&dmxdev->mutex);
237 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
238 mutex_unlock(&dmxdev->mutex);
242 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
245 struct dvb_device *dvbdev = file->private_data;
246 struct dmxdev *dmxdev = dvbdev->priv;
250 mutex_unlock(&dmxdev->mutex);
254 //mutex_lock(&dmxdev->mutex);
255 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
256 file->f_flags & O_NONBLOCK,
258 //mutex_unlock(&dmxdev->mutex);
262 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
265 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
269 dprintk("function : %s\n", __func__);
271 if (buf->size == size)
276 newmem = vmalloc(size);
282 spin_lock_irq(&dmxdev->lock);
286 /* reset and not flush in case the buffer shrinks */
287 dvb_ringbuffer_reset(buf);
288 spin_unlock_irq(&dmxdev->lock);
295 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
296 *dmxdevfilter, int state)
298 spin_lock_irq(&dmxdevfilter->dev->lock);
299 dmxdevfilter->state = state;
300 spin_unlock_irq(&dmxdevfilter->dev->lock);
303 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
306 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
310 if (buf->size == size)
314 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
317 newmem = vmalloc(size);
323 spin_lock_irq(&dmxdevfilter->dev->lock);
327 /* reset and not flush in case the buffer shrinks */
328 dvb_ringbuffer_reset(buf);
329 spin_unlock_irq(&dmxdevfilter->dev->lock);
336 static void dvb_dmxdev_filter_timeout(unsigned long data)
338 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
340 dmxdevfilter->buffer.error = -ETIMEDOUT;
341 spin_lock_irq(&dmxdevfilter->dev->lock);
342 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
343 spin_unlock_irq(&dmxdevfilter->dev->lock);
344 wake_up(&dmxdevfilter->buffer.queue);
347 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
349 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
351 del_timer(&dmxdevfilter->timer);
353 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
354 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
355 dmxdevfilter->timer.expires =
356 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
357 add_timer(&dmxdevfilter->timer);
361 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
362 const u8 *buffer2, size_t buffer2_len,
363 struct dmx_section_filter *filter,
364 enum dmx_success success)
366 struct dmxdev_filter *dmxdevfilter = filter->priv;
370 if (dmxdevfilter->buffer.error) {
371 wake_up(&dmxdevfilter->buffer.queue);
374 spin_lock_irqsave(&dmxdevfilter->dev->lock, flags);
375 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
376 spin_unlock_irqrestore(&dmxdevfilter->dev->lock, flags);
379 del_timer(&dmxdevfilter->timer);
380 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
381 buffer1[0], buffer1[1],
382 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
383 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
385 if (ret == buffer1_len) {
386 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
390 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
391 dmxdevfilter->buffer.error = ret;
393 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
394 dmxdevfilter->state = DMXDEV_STATE_DONE;
395 spin_unlock_irqrestore(&dmxdevfilter->dev->lock, flags);
396 wake_up(&dmxdevfilter->buffer.queue);
400 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
401 const u8 *buffer2, size_t buffer2_len,
402 struct dmx_ts_feed *feed,
403 enum dmx_success success)
405 struct dmxdev_filter *dmxdevfilter = feed->priv;
406 struct dvb_ringbuffer *buffer;
410 spin_lock_irqsave(&dmxdevfilter->dev->lock, flags);
411 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
412 spin_unlock_irqrestore(&dmxdevfilter->dev->lock, flags);
416 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
417 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
418 buffer = &dmxdevfilter->buffer;
420 buffer = &dmxdevfilter->dev->dvr_buffer;
422 spin_unlock_irqrestore(&dmxdevfilter->dev->lock, flags);
423 wake_up(&buffer->queue);
426 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
427 if (ret == buffer1_len)
428 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
430 dvb_ringbuffer_flush(buffer);
433 spin_unlock_irqrestore(&dmxdevfilter->dev->lock, flags);
434 wake_up(&buffer->queue);
438 /* stop feed but only mark the specified filter as stopped (state set) */
439 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
441 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
443 switch (dmxdevfilter->type) {
444 case DMXDEV_TYPE_SEC:
445 del_timer(&dmxdevfilter->timer);
446 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
448 case DMXDEV_TYPE_PES:
449 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
457 /* start feed associated with the specified filter */
458 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
460 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
462 switch (filter->type) {
463 case DMXDEV_TYPE_SEC:
464 return filter->feed.sec->start_filtering(filter->feed.sec);
465 case DMXDEV_TYPE_PES:
466 return filter->feed.ts->start_filtering(filter->feed.ts);
474 /* restart section feed if it has filters left associated with it,
475 otherwise release the feed */
476 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
479 struct dmxdev *dmxdev = filter->dev;
480 u16 pid = filter->params.sec.pid;
482 for (i = 0; i < dmxdev->filternum; i++)
483 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
484 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
485 dmxdev->filter[i].params.sec.pid == pid) {
486 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
490 filter->dev->demux->release_section_feed(dmxdev->demux,
496 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
498 if (dmxdevfilter->state < DMXDEV_STATE_GO)
501 switch (dmxdevfilter->type) {
502 case DMXDEV_TYPE_SEC:
503 if (!dmxdevfilter->feed.sec)
505 dvb_dmxdev_feed_stop(dmxdevfilter);
506 if (dmxdevfilter->filter.sec)
507 dmxdevfilter->feed.sec->
508 release_filter(dmxdevfilter->feed.sec,
509 dmxdevfilter->filter.sec);
510 dvb_dmxdev_feed_restart(dmxdevfilter);
511 dmxdevfilter->feed.sec = NULL;
513 case DMXDEV_TYPE_PES:
514 if (!dmxdevfilter->feed.ts)
516 dvb_dmxdev_feed_stop(dmxdevfilter);
517 dmxdevfilter->dev->demux->
518 release_ts_feed(dmxdevfilter->dev->demux,
519 dmxdevfilter->feed.ts);
520 dmxdevfilter->feed.ts = NULL;
523 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
528 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
532 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
534 if (dmxdevfilter->state < DMXDEV_STATE_SET)
537 dmxdevfilter->type = DMXDEV_TYPE_NONE;
538 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
542 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
544 struct dmxdev *dmxdev = filter->dev;
548 if (filter->state < DMXDEV_STATE_SET)
551 if (filter->state >= DMXDEV_STATE_GO)
552 dvb_dmxdev_filter_stop(filter);
554 if (!filter->buffer.data) {
555 mem = vmalloc(filter->buffer.size);
558 spin_lock_irq(&filter->dev->lock);
559 filter->buffer.data = mem;
560 spin_unlock_irq(&filter->dev->lock);
563 dvb_ringbuffer_flush(&filter->buffer);
565 switch (filter->type) {
566 case DMXDEV_TYPE_SEC:
568 struct dmx_sct_filter_params *para = &filter->params.sec;
569 struct dmx_section_filter **secfilter = &filter->filter.sec;
570 struct dmx_section_feed **secfeed = &filter->feed.sec;
576 /* find active filter/feed with same PID */
577 for (i = 0; i < dmxdev->filternum; i++) {
578 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
579 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
580 dmxdev->filter[i].params.sec.pid == para->pid) {
581 *secfeed = dmxdev->filter[i].feed.sec;
586 /* if no feed found, try to allocate new one */
588 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
590 dvb_dmxdev_section_callback);
592 printk("DVB (%s): could not alloc feed\n",
597 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
598 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
600 printk("DVB (%s): could not set feed\n",
602 dvb_dmxdev_feed_restart(filter);
606 dvb_dmxdev_feed_stop(filter);
609 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
611 dvb_dmxdev_feed_restart(filter);
612 filter->feed.sec->start_filtering(*secfeed);
613 dprintk("could not get filter\n");
617 (*secfilter)->priv = filter;
619 memcpy(&((*secfilter)->filter_value[3]),
620 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
621 memcpy(&(*secfilter)->filter_mask[3],
622 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
623 memcpy(&(*secfilter)->filter_mode[3],
624 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
626 (*secfilter)->filter_value[0] = para->filter.filter[0];
627 (*secfilter)->filter_mask[0] = para->filter.mask[0];
628 (*secfilter)->filter_mode[0] = para->filter.mode[0];
629 (*secfilter)->filter_mask[1] = 0;
630 (*secfilter)->filter_mask[2] = 0;
634 ret = filter->feed.sec->start_filtering(filter->feed.sec);
638 dvb_dmxdev_filter_timer(filter);
641 case DMXDEV_TYPE_PES:
643 struct timespec timeout = { 0 };
644 struct dmx_pes_filter_params *para = &filter->params.pes;
647 enum dmx_ts_pes ts_pes;
648 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
650 filter->feed.ts = NULL;
651 otype = para->output;
653 ts_pes = (enum dmx_ts_pes)para->pes_type;
655 if (ts_pes < DMX_PES_OTHER)
656 ts_type = TS_DECODER;
660 if (otype == DMX_OUT_TS_TAP)
661 ts_type |= TS_PACKET;
662 else if (otype == DMX_OUT_TSDEMUX_TAP)
663 ts_type |= TS_PACKET | TS_DEMUX;
664 else if (otype == DMX_OUT_TAP)
665 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
667 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
669 dvb_dmxdev_ts_callback);
673 (*tsfeed)->priv = filter;
675 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
678 dmxdev->demux->release_ts_feed(dmxdev->demux,
683 ret = filter->feed.ts->start_filtering(filter->feed.ts);
685 dmxdev->demux->release_ts_feed(dmxdev->demux,
696 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
700 static int dvb_demux_open(struct inode *inode, struct file *file)
702 struct dvb_device *dvbdev = file->private_data;
703 struct dmxdev *dmxdev = dvbdev->priv;
705 struct dmxdev_filter *dmxdevfilter;
710 if (mutex_lock_interruptible(&dmxdev->mutex))
713 for (i = 0; i < dmxdev->filternum; i++)
714 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
717 if (i == dmxdev->filternum) {
718 mutex_unlock(&dmxdev->mutex);
722 dmxdevfilter = &dmxdev->filter[i];
723 mutex_init(&dmxdevfilter->mutex);
724 file->private_data = dmxdevfilter;
726 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
727 dmxdevfilter->type = DMXDEV_TYPE_NONE;
728 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
729 dmxdevfilter->feed.ts = NULL;
730 init_timer(&dmxdevfilter->timer);
734 mutex_unlock(&dmxdev->mutex);
738 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
739 struct dmxdev_filter *dmxdevfilter)
741 mutex_lock(&dmxdev->mutex);
742 mutex_lock(&dmxdevfilter->mutex);
744 dvb_dmxdev_filter_stop(dmxdevfilter);
745 dvb_dmxdev_filter_reset(dmxdevfilter);
747 if (dmxdevfilter->buffer.data) {
748 void *mem = dmxdevfilter->buffer.data;
750 spin_lock_irq(&dmxdev->lock);
751 dmxdevfilter->buffer.data = NULL;
752 spin_unlock_irq(&dmxdev->lock);
756 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
757 wake_up(&dmxdevfilter->buffer.queue);
758 mutex_unlock(&dmxdevfilter->mutex);
759 mutex_unlock(&dmxdev->mutex);
763 static inline void invert_mode(dmx_filter_t *filter)
767 for (i = 0; i < DMX_FILTER_SIZE; i++)
768 filter->mode[i] ^= 0xff;
771 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
772 struct dmxdev_filter *dmxdevfilter,
773 struct dmx_sct_filter_params *params)
775 dprintk("function : %s\n", __func__);
777 dvb_dmxdev_filter_stop(dmxdevfilter);
779 dmxdevfilter->type = DMXDEV_TYPE_SEC;
780 memcpy(&dmxdevfilter->params.sec,
781 params, sizeof(struct dmx_sct_filter_params));
782 invert_mode(&dmxdevfilter->params.sec.filter);
783 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
785 if (params->flags & DMX_IMMEDIATE_START)
786 return dvb_dmxdev_filter_start(dmxdevfilter);
791 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
792 struct dmxdev_filter *dmxdevfilter,
793 struct dmx_pes_filter_params *params)
795 dvb_dmxdev_filter_stop(dmxdevfilter);
797 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
800 dmxdevfilter->type = DMXDEV_TYPE_PES;
801 memcpy(&dmxdevfilter->params, params,
802 sizeof(struct dmx_pes_filter_params));
804 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
806 if (params->flags & DMX_IMMEDIATE_START)
807 return dvb_dmxdev_filter_start(dmxdevfilter);
812 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
813 struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
819 if (dfil->todo <= 0) {
820 hcount = 3 + dfil->todo;
823 result = dvb_dmxdev_buffer_read(&dfil->buffer,
824 file->f_flags & O_NONBLOCK,
830 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
835 dfil->todo -= result;
838 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
842 if (count > dfil->todo)
844 result = dvb_dmxdev_buffer_read(&dfil->buffer,
845 file->f_flags & O_NONBLOCK,
849 dfil->todo -= result;
850 return (result + done);
854 dvb_demux_read(struct file *file, char __user *buf, size_t count,
857 struct dmxdev_filter *dmxdevfilter = file->private_data;
860 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
863 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
864 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
866 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
867 file->f_flags & O_NONBLOCK,
870 mutex_unlock(&dmxdevfilter->mutex);
874 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
875 unsigned int cmd, void *parg)
877 struct dmxdev_filter *dmxdevfilter = file->private_data;
878 struct dmxdev *dmxdev = dmxdevfilter->dev;
879 unsigned long arg = (unsigned long)parg;
882 if (mutex_lock_interruptible(&dmxdev->mutex))
887 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
888 mutex_unlock(&dmxdev->mutex);
891 if (dmxdevfilter->state < DMXDEV_STATE_SET)
894 ret = dvb_dmxdev_filter_start(dmxdevfilter);
895 mutex_unlock(&dmxdevfilter->mutex);
899 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
900 mutex_unlock(&dmxdev->mutex);
903 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
904 mutex_unlock(&dmxdevfilter->mutex);
908 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
909 mutex_unlock(&dmxdev->mutex);
912 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
913 mutex_unlock(&dmxdevfilter->mutex);
916 case DMX_SET_PES_FILTER:
917 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
918 mutex_unlock(&dmxdev->mutex);
921 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
922 mutex_unlock(&dmxdevfilter->mutex);
925 case DMX_SET_BUFFER_SIZE:
926 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
927 mutex_unlock(&dmxdev->mutex);
930 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
931 mutex_unlock(&dmxdevfilter->mutex);
934 case DMX_GET_PES_PIDS:
935 if (!dmxdev->demux->get_pes_pids) {
939 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
943 if (!dmxdev->demux->get_caps) {
947 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
951 if (!dmxdev->demux->set_source) {
955 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
959 if (!dmxdev->demux->get_stc) {
963 ret = dmxdev->demux->get_stc(dmxdev->demux,
964 ((struct dmx_stc *)parg)->num,
965 &((struct dmx_stc *)parg)->stc,
966 &((struct dmx_stc *)parg)->base);
973 mutex_unlock(&dmxdev->mutex);
977 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
978 unsigned int cmd, unsigned long arg)
980 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
983 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
985 struct dmxdev_filter *dmxdevfilter = file->private_data;
986 unsigned int mask = 0;
991 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
993 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
994 dmxdevfilter->state != DMXDEV_STATE_DONE &&
995 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
998 if (dmxdevfilter->buffer.error)
999 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1001 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1002 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1007 static int dvb_demux_release(struct inode *inode, struct file *file)
1009 struct dmxdev_filter *dmxdevfilter = file->private_data;
1010 struct dmxdev *dmxdev = dmxdevfilter->dev;
1014 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1016 mutex_lock(&dmxdev->mutex);
1017 dmxdev->dvbdev->users--;
1018 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1019 fops_put(file->f_op);
1021 mutex_unlock(&dmxdev->mutex);
1022 wake_up(&dmxdev->dvbdev->wait_queue);
1024 mutex_unlock(&dmxdev->mutex);
1029 static struct file_operations dvb_demux_fops = {
1030 .owner = THIS_MODULE,
1031 .read = dvb_demux_read,
1032 .ioctl = dvb_demux_ioctl,
1033 .open = dvb_demux_open,
1034 .release = dvb_demux_release,
1035 .poll = dvb_demux_poll,
1038 static struct dvb_device dvbdev_demux = {
1042 .fops = &dvb_demux_fops
1045 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1046 unsigned int cmd, void *parg)
1048 struct dvb_device *dvbdev = file->private_data;
1049 struct dmxdev *dmxdev = dvbdev->priv;
1050 unsigned long arg = (unsigned long)parg;
1053 if (mutex_lock_interruptible(&dmxdev->mutex))
1054 return -ERESTARTSYS;
1057 case DMX_SET_BUFFER_SIZE:
1058 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1065 mutex_unlock(&dmxdev->mutex);
1069 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1070 unsigned int cmd, unsigned long arg)
1072 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1075 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1077 struct dvb_device *dvbdev = file->private_data;
1078 struct dmxdev *dmxdev = dvbdev->priv;
1079 unsigned int mask = 0;
1081 dprintk("function : %s\n", __func__);
1083 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1085 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1086 if (dmxdev->dvr_buffer.error)
1087 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1089 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1090 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1092 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1097 static struct file_operations dvb_dvr_fops = {
1098 .owner = THIS_MODULE,
1099 .read = dvb_dvr_read,
1100 .write = dvb_dvr_write,
1101 .ioctl = dvb_dvr_ioctl,
1102 .open = dvb_dvr_open,
1103 .release = dvb_dvr_release,
1104 .poll = dvb_dvr_poll,
1107 static struct dvb_device dvbdev_dvr = {
1111 .fops = &dvb_dvr_fops
1114 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1118 if (dmxdev->demux->open(dmxdev->demux) < 0)
1121 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1122 if (!dmxdev->filter)
1125 mutex_init(&dmxdev->mutex);
1126 spin_lock_init(&dmxdev->lock);
1127 for (i = 0; i < dmxdev->filternum; i++) {
1128 dmxdev->filter[i].dev = dmxdev;
1129 dmxdev->filter[i].buffer.data = NULL;
1130 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1134 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1136 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1137 dmxdev, DVB_DEVICE_DVR);
1139 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1144 EXPORT_SYMBOL(dvb_dmxdev_init);
1146 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1149 if (dmxdev->dvbdev->users > 1) {
1150 wait_event(dmxdev->dvbdev->wait_queue,
1151 dmxdev->dvbdev->users==1);
1153 if (dmxdev->dvr_dvbdev->users > 1) {
1154 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1155 dmxdev->dvr_dvbdev->users==1);
1158 dvb_unregister_device(dmxdev->dvbdev);
1159 dvb_unregister_device(dmxdev->dvr_dvbdev);
1161 vfree(dmxdev->filter);
1162 dmxdev->filter = NULL;
1163 dmxdev->demux->close(dmxdev->demux);
1166 EXPORT_SYMBOL(dvb_dmxdev_release);