Merge branch 'for-linus' of git://oss.sgi.com/xfs/xfs
[linux/fpc-iii.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob425862ffb2856b1e3b9019816ef6b003e7053f3f
1 /*
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/smp_lock.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>
34 #include "dmxdev.h"
36 static int debug;
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)
46 ssize_t free;
48 if (!len)
49 return 0;
50 if (!buf->data)
51 return 0;
53 free = dvb_ringbuffer_free(buf);
54 if (len > free) {
55 dprintk("dmxdev: buffer overflow\n");
56 return -EOVERFLOW;
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)
66 size_t todo;
67 ssize_t avail;
68 ssize_t ret = 0;
70 if (!src->data)
71 return 0;
73 if (src->error) {
74 ret = src->error;
75 dvb_ringbuffer_flush(src);
76 return ret;
79 for (todo = count; todo > 0; todo -= ret) {
80 if (non_blocking && dvb_ringbuffer_empty(src)) {
81 ret = -EWOULDBLOCK;
82 break;
85 ret = wait_event_interruptible(src->queue,
86 !dvb_ringbuffer_empty(src) ||
87 (src->error != 0));
88 if (ret < 0)
89 break;
91 if (src->error) {
92 ret = src->error;
93 dvb_ringbuffer_flush(src);
94 break;
97 avail = dvb_ringbuffer_avail(src);
98 if (avail > todo)
99 avail = todo;
101 ret = dvb_ringbuffer_read_user(src, buf, avail);
102 if (ret < 0)
103 break;
105 buf += ret;
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);
116 if (!head)
117 return NULL;
118 list_for_each(pos, head)
119 if (DMX_FE_ENTRY(pos)->source == type)
120 return DMX_FE_ENTRY(pos);
122 return NULL;
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", __func__);
133 if (mutex_lock_interruptible(&dmxdev->mutex))
134 return -ERESTARTSYS;
136 if (dmxdev->exit) {
137 mutex_unlock(&dmxdev->mutex);
138 return -ENODEV;
141 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
142 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
143 mutex_unlock(&dmxdev->mutex);
144 return -EOPNOTSUPP;
148 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
149 void *mem;
150 if (!dvbdev->readers) {
151 mutex_unlock(&dmxdev->mutex);
152 return -EBUSY;
154 mem = vmalloc(DVR_BUFFER_SIZE);
155 if (!mem) {
156 mutex_unlock(&dmxdev->mutex);
157 return -ENOMEM;
159 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
160 dvbdev->readers--;
163 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
164 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
166 if (!dmxdev->demux->write) {
167 mutex_unlock(&dmxdev->mutex);
168 return -EOPNOTSUPP;
171 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
173 if (!front) {
174 mutex_unlock(&dmxdev->mutex);
175 return -EINVAL;
177 dmxdev->demux->disconnect_frontend(dmxdev->demux);
178 dmxdev->demux->connect_frontend(dmxdev->demux, front);
180 dvbdev->users++;
181 mutex_unlock(&dmxdev->mutex);
182 return 0;
185 static int dvb_dvr_release(struct inode *inode, struct file *file)
187 struct dvb_device *dvbdev = file->private_data;
188 struct dmxdev *dmxdev = dvbdev->priv;
190 mutex_lock(&dmxdev->mutex);
192 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
193 dmxdev->demux->disconnect_frontend(dmxdev->demux);
194 dmxdev->demux->connect_frontend(dmxdev->demux,
195 dmxdev->dvr_orig_fe);
197 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
198 dvbdev->readers++;
199 if (dmxdev->dvr_buffer.data) {
200 void *mem = dmxdev->dvr_buffer.data;
201 mb();
202 spin_lock_irq(&dmxdev->lock);
203 dmxdev->dvr_buffer.data = NULL;
204 spin_unlock_irq(&dmxdev->lock);
205 vfree(mem);
208 /* TODO */
209 dvbdev->users--;
210 if(dvbdev->users==-1 && dmxdev->exit==1) {
211 fops_put(file->f_op);
212 file->f_op = NULL;
213 mutex_unlock(&dmxdev->mutex);
214 wake_up(&dvbdev->wait_queue);
215 } else
216 mutex_unlock(&dmxdev->mutex);
218 return 0;
221 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
222 size_t count, loff_t *ppos)
224 struct dvb_device *dvbdev = file->private_data;
225 struct dmxdev *dmxdev = dvbdev->priv;
226 int ret;
228 if (!dmxdev->demux->write)
229 return -EOPNOTSUPP;
230 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
231 return -EINVAL;
232 if (mutex_lock_interruptible(&dmxdev->mutex))
233 return -ERESTARTSYS;
235 if (dmxdev->exit) {
236 mutex_unlock(&dmxdev->mutex);
237 return -ENODEV;
239 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
240 mutex_unlock(&dmxdev->mutex);
241 return ret;
244 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
245 loff_t *ppos)
247 struct dvb_device *dvbdev = file->private_data;
248 struct dmxdev *dmxdev = dvbdev->priv;
250 if (dmxdev->exit)
251 return -ENODEV;
253 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
254 file->f_flags & O_NONBLOCK,
255 buf, count, ppos);
258 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
259 unsigned long size)
261 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
262 void *newmem;
263 void *oldmem;
265 dprintk("function : %s\n", __func__);
267 if (buf->size == size)
268 return 0;
269 if (!size)
270 return -EINVAL;
272 newmem = vmalloc(size);
273 if (!newmem)
274 return -ENOMEM;
276 oldmem = buf->data;
278 spin_lock_irq(&dmxdev->lock);
279 buf->data = newmem;
280 buf->size = size;
282 /* reset and not flush in case the buffer shrinks */
283 dvb_ringbuffer_reset(buf);
284 spin_unlock_irq(&dmxdev->lock);
286 vfree(oldmem);
288 return 0;
291 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
292 *dmxdevfilter, int state)
294 spin_lock_irq(&dmxdevfilter->dev->lock);
295 dmxdevfilter->state = state;
296 spin_unlock_irq(&dmxdevfilter->dev->lock);
299 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
300 unsigned long size)
302 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
303 void *newmem;
304 void *oldmem;
306 if (buf->size == size)
307 return 0;
308 if (!size)
309 return -EINVAL;
310 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
311 return -EBUSY;
313 newmem = vmalloc(size);
314 if (!newmem)
315 return -ENOMEM;
317 oldmem = buf->data;
319 spin_lock_irq(&dmxdevfilter->dev->lock);
320 buf->data = newmem;
321 buf->size = size;
323 /* reset and not flush in case the buffer shrinks */
324 dvb_ringbuffer_reset(buf);
325 spin_unlock_irq(&dmxdevfilter->dev->lock);
327 vfree(oldmem);
329 return 0;
332 static void dvb_dmxdev_filter_timeout(unsigned long data)
334 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
336 dmxdevfilter->buffer.error = -ETIMEDOUT;
337 spin_lock_irq(&dmxdevfilter->dev->lock);
338 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
339 spin_unlock_irq(&dmxdevfilter->dev->lock);
340 wake_up(&dmxdevfilter->buffer.queue);
343 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
345 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
347 del_timer(&dmxdevfilter->timer);
348 if (para->timeout) {
349 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
350 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
351 dmxdevfilter->timer.expires =
352 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
353 add_timer(&dmxdevfilter->timer);
357 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
358 const u8 *buffer2, size_t buffer2_len,
359 struct dmx_section_filter *filter,
360 enum dmx_success success)
362 struct dmxdev_filter *dmxdevfilter = filter->priv;
363 int ret;
365 if (dmxdevfilter->buffer.error) {
366 wake_up(&dmxdevfilter->buffer.queue);
367 return 0;
369 spin_lock(&dmxdevfilter->dev->lock);
370 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
371 spin_unlock(&dmxdevfilter->dev->lock);
372 return 0;
374 del_timer(&dmxdevfilter->timer);
375 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
376 buffer1[0], buffer1[1],
377 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
378 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
379 buffer1_len);
380 if (ret == buffer1_len) {
381 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
382 buffer2_len);
384 if (ret < 0) {
385 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
386 dmxdevfilter->buffer.error = ret;
388 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
389 dmxdevfilter->state = DMXDEV_STATE_DONE;
390 spin_unlock(&dmxdevfilter->dev->lock);
391 wake_up(&dmxdevfilter->buffer.queue);
392 return 0;
395 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
396 const u8 *buffer2, size_t buffer2_len,
397 struct dmx_ts_feed *feed,
398 enum dmx_success success)
400 struct dmxdev_filter *dmxdevfilter = feed->priv;
401 struct dvb_ringbuffer *buffer;
402 int ret;
404 spin_lock(&dmxdevfilter->dev->lock);
405 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
406 spin_unlock(&dmxdevfilter->dev->lock);
407 return 0;
410 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
411 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
412 buffer = &dmxdevfilter->buffer;
413 else
414 buffer = &dmxdevfilter->dev->dvr_buffer;
415 if (buffer->error) {
416 spin_unlock(&dmxdevfilter->dev->lock);
417 wake_up(&buffer->queue);
418 return 0;
420 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
421 if (ret == buffer1_len)
422 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
423 if (ret < 0) {
424 dvb_ringbuffer_flush(buffer);
425 buffer->error = ret;
427 spin_unlock(&dmxdevfilter->dev->lock);
428 wake_up(&buffer->queue);
429 return 0;
432 /* stop feed but only mark the specified filter as stopped (state set) */
433 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
435 struct dmxdev_feed *feed;
437 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
439 switch (dmxdevfilter->type) {
440 case DMXDEV_TYPE_SEC:
441 del_timer(&dmxdevfilter->timer);
442 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
443 break;
444 case DMXDEV_TYPE_PES:
445 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
446 feed->ts->stop_filtering(feed->ts);
447 break;
448 default:
449 return -EINVAL;
451 return 0;
454 /* start feed associated with the specified filter */
455 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
457 struct dmxdev_feed *feed;
458 int ret;
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 list_for_each_entry(feed, &filter->feed.ts, next) {
467 ret = feed->ts->start_filtering(feed->ts);
468 if (ret < 0) {
469 dvb_dmxdev_feed_stop(filter);
470 return ret;
473 break;
474 default:
475 return -EINVAL;
478 return 0;
481 /* restart section feed if it has filters left associated with it,
482 otherwise release the feed */
483 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
485 int i;
486 struct dmxdev *dmxdev = filter->dev;
487 u16 pid = filter->params.sec.pid;
489 for (i = 0; i < dmxdev->filternum; i++)
490 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
491 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
492 dmxdev->filter[i].params.sec.pid == pid) {
493 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
494 return 0;
497 filter->dev->demux->release_section_feed(dmxdev->demux,
498 filter->feed.sec);
500 return 0;
503 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
505 struct dmxdev_feed *feed;
506 struct dmx_demux *demux;
508 if (dmxdevfilter->state < DMXDEV_STATE_GO)
509 return 0;
511 switch (dmxdevfilter->type) {
512 case DMXDEV_TYPE_SEC:
513 if (!dmxdevfilter->feed.sec)
514 break;
515 dvb_dmxdev_feed_stop(dmxdevfilter);
516 if (dmxdevfilter->filter.sec)
517 dmxdevfilter->feed.sec->
518 release_filter(dmxdevfilter->feed.sec,
519 dmxdevfilter->filter.sec);
520 dvb_dmxdev_feed_restart(dmxdevfilter);
521 dmxdevfilter->feed.sec = NULL;
522 break;
523 case DMXDEV_TYPE_PES:
524 dvb_dmxdev_feed_stop(dmxdevfilter);
525 demux = dmxdevfilter->dev->demux;
526 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
527 demux->release_ts_feed(demux, feed->ts);
528 feed->ts = NULL;
530 break;
531 default:
532 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
533 return 0;
534 return -EINVAL;
537 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
538 return 0;
541 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
543 struct dmxdev_feed *feed, *tmp;
545 /* delete all PIDs */
546 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
547 list_del(&feed->next);
548 kfree(feed);
551 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
554 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
556 if (dmxdevfilter->state < DMXDEV_STATE_SET)
557 return 0;
559 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
560 dvb_dmxdev_delete_pids(dmxdevfilter);
562 dmxdevfilter->type = DMXDEV_TYPE_NONE;
563 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
564 return 0;
567 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
568 struct dmxdev_filter *filter,
569 struct dmxdev_feed *feed)
571 struct timespec timeout = { 0 };
572 struct dmx_pes_filter_params *para = &filter->params.pes;
573 dmx_output_t otype;
574 int ret;
575 int ts_type;
576 enum dmx_ts_pes ts_pes;
577 struct dmx_ts_feed *tsfeed;
579 feed->ts = NULL;
580 otype = para->output;
582 ts_pes = (enum dmx_ts_pes)para->pes_type;
584 if (ts_pes < DMX_PES_OTHER)
585 ts_type = TS_DECODER;
586 else
587 ts_type = 0;
589 if (otype == DMX_OUT_TS_TAP)
590 ts_type |= TS_PACKET;
591 else if (otype == DMX_OUT_TSDEMUX_TAP)
592 ts_type |= TS_PACKET | TS_DEMUX;
593 else if (otype == DMX_OUT_TAP)
594 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
596 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
597 dvb_dmxdev_ts_callback);
598 if (ret < 0)
599 return ret;
601 tsfeed = feed->ts;
602 tsfeed->priv = filter;
604 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
605 if (ret < 0) {
606 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
607 return ret;
610 ret = tsfeed->start_filtering(tsfeed);
611 if (ret < 0) {
612 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
613 return ret;
616 return 0;
619 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
621 struct dmxdev *dmxdev = filter->dev;
622 struct dmxdev_feed *feed;
623 void *mem;
624 int ret, i;
626 if (filter->state < DMXDEV_STATE_SET)
627 return -EINVAL;
629 if (filter->state >= DMXDEV_STATE_GO)
630 dvb_dmxdev_filter_stop(filter);
632 if (!filter->buffer.data) {
633 mem = vmalloc(filter->buffer.size);
634 if (!mem)
635 return -ENOMEM;
636 spin_lock_irq(&filter->dev->lock);
637 filter->buffer.data = mem;
638 spin_unlock_irq(&filter->dev->lock);
641 dvb_ringbuffer_flush(&filter->buffer);
643 switch (filter->type) {
644 case DMXDEV_TYPE_SEC:
646 struct dmx_sct_filter_params *para = &filter->params.sec;
647 struct dmx_section_filter **secfilter = &filter->filter.sec;
648 struct dmx_section_feed **secfeed = &filter->feed.sec;
650 *secfilter = NULL;
651 *secfeed = NULL;
654 /* find active filter/feed with same PID */
655 for (i = 0; i < dmxdev->filternum; i++) {
656 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
657 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
658 dmxdev->filter[i].params.sec.pid == para->pid) {
659 *secfeed = dmxdev->filter[i].feed.sec;
660 break;
664 /* if no feed found, try to allocate new one */
665 if (!*secfeed) {
666 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
667 secfeed,
668 dvb_dmxdev_section_callback);
669 if (ret < 0) {
670 printk("DVB (%s): could not alloc feed\n",
671 __func__);
672 return ret;
675 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
676 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
677 if (ret < 0) {
678 printk("DVB (%s): could not set feed\n",
679 __func__);
680 dvb_dmxdev_feed_restart(filter);
681 return ret;
683 } else {
684 dvb_dmxdev_feed_stop(filter);
687 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
688 if (ret < 0) {
689 dvb_dmxdev_feed_restart(filter);
690 filter->feed.sec->start_filtering(*secfeed);
691 dprintk("could not get filter\n");
692 return ret;
695 (*secfilter)->priv = filter;
697 memcpy(&((*secfilter)->filter_value[3]),
698 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
699 memcpy(&(*secfilter)->filter_mask[3],
700 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
701 memcpy(&(*secfilter)->filter_mode[3],
702 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
704 (*secfilter)->filter_value[0] = para->filter.filter[0];
705 (*secfilter)->filter_mask[0] = para->filter.mask[0];
706 (*secfilter)->filter_mode[0] = para->filter.mode[0];
707 (*secfilter)->filter_mask[1] = 0;
708 (*secfilter)->filter_mask[2] = 0;
710 filter->todo = 0;
712 ret = filter->feed.sec->start_filtering(filter->feed.sec);
713 if (ret < 0)
714 return ret;
716 dvb_dmxdev_filter_timer(filter);
717 break;
719 case DMXDEV_TYPE_PES:
720 list_for_each_entry(feed, &filter->feed.ts, next) {
721 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
722 if (ret < 0) {
723 dvb_dmxdev_filter_stop(filter);
724 return ret;
727 break;
728 default:
729 return -EINVAL;
732 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
733 return 0;
736 static int dvb_demux_open(struct inode *inode, struct file *file)
738 struct dvb_device *dvbdev = file->private_data;
739 struct dmxdev *dmxdev = dvbdev->priv;
740 int i;
741 struct dmxdev_filter *dmxdevfilter;
743 if (!dmxdev->filter)
744 return -EINVAL;
746 if (mutex_lock_interruptible(&dmxdev->mutex))
747 return -ERESTARTSYS;
749 for (i = 0; i < dmxdev->filternum; i++)
750 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
751 break;
753 if (i == dmxdev->filternum) {
754 mutex_unlock(&dmxdev->mutex);
755 return -EMFILE;
758 dmxdevfilter = &dmxdev->filter[i];
759 mutex_init(&dmxdevfilter->mutex);
760 file->private_data = dmxdevfilter;
762 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
763 dmxdevfilter->type = DMXDEV_TYPE_NONE;
764 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
765 init_timer(&dmxdevfilter->timer);
767 dvbdev->users++;
769 mutex_unlock(&dmxdev->mutex);
770 return 0;
773 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
774 struct dmxdev_filter *dmxdevfilter)
776 mutex_lock(&dmxdev->mutex);
777 mutex_lock(&dmxdevfilter->mutex);
779 dvb_dmxdev_filter_stop(dmxdevfilter);
780 dvb_dmxdev_filter_reset(dmxdevfilter);
782 if (dmxdevfilter->buffer.data) {
783 void *mem = dmxdevfilter->buffer.data;
785 spin_lock_irq(&dmxdev->lock);
786 dmxdevfilter->buffer.data = NULL;
787 spin_unlock_irq(&dmxdev->lock);
788 vfree(mem);
791 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
792 wake_up(&dmxdevfilter->buffer.queue);
793 mutex_unlock(&dmxdevfilter->mutex);
794 mutex_unlock(&dmxdev->mutex);
795 return 0;
798 static inline void invert_mode(dmx_filter_t *filter)
800 int i;
802 for (i = 0; i < DMX_FILTER_SIZE; i++)
803 filter->mode[i] ^= 0xff;
806 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
807 struct dmxdev_filter *filter, u16 pid)
809 struct dmxdev_feed *feed;
811 if ((filter->type != DMXDEV_TYPE_PES) ||
812 (filter->state < DMXDEV_STATE_SET))
813 return -EINVAL;
815 /* only TS packet filters may have multiple PIDs */
816 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
817 (!list_empty(&filter->feed.ts)))
818 return -EINVAL;
820 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
821 if (feed == NULL)
822 return -ENOMEM;
824 feed->pid = pid;
825 list_add(&feed->next, &filter->feed.ts);
827 if (filter->state >= DMXDEV_STATE_GO)
828 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
830 return 0;
833 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
834 struct dmxdev_filter *filter, u16 pid)
836 struct dmxdev_feed *feed, *tmp;
838 if ((filter->type != DMXDEV_TYPE_PES) ||
839 (filter->state < DMXDEV_STATE_SET))
840 return -EINVAL;
842 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
843 if ((feed->pid == pid) && (feed->ts != NULL)) {
844 feed->ts->stop_filtering(feed->ts);
845 filter->dev->demux->release_ts_feed(filter->dev->demux,
846 feed->ts);
847 list_del(&feed->next);
848 kfree(feed);
852 return 0;
855 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
856 struct dmxdev_filter *dmxdevfilter,
857 struct dmx_sct_filter_params *params)
859 dprintk("function : %s\n", __func__);
861 dvb_dmxdev_filter_stop(dmxdevfilter);
863 dmxdevfilter->type = DMXDEV_TYPE_SEC;
864 memcpy(&dmxdevfilter->params.sec,
865 params, sizeof(struct dmx_sct_filter_params));
866 invert_mode(&dmxdevfilter->params.sec.filter);
867 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
869 if (params->flags & DMX_IMMEDIATE_START)
870 return dvb_dmxdev_filter_start(dmxdevfilter);
872 return 0;
875 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
876 struct dmxdev_filter *dmxdevfilter,
877 struct dmx_pes_filter_params *params)
879 int ret;
881 dvb_dmxdev_filter_stop(dmxdevfilter);
882 dvb_dmxdev_filter_reset(dmxdevfilter);
884 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
885 return -EINVAL;
887 dmxdevfilter->type = DMXDEV_TYPE_PES;
888 memcpy(&dmxdevfilter->params, params,
889 sizeof(struct dmx_pes_filter_params));
890 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
892 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
894 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
895 dmxdevfilter->params.pes.pid);
896 if (ret < 0)
897 return ret;
899 if (params->flags & DMX_IMMEDIATE_START)
900 return dvb_dmxdev_filter_start(dmxdevfilter);
902 return 0;
905 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
906 struct file *file, char __user *buf,
907 size_t count, loff_t *ppos)
909 int result, hcount;
910 int done = 0;
912 if (dfil->todo <= 0) {
913 hcount = 3 + dfil->todo;
914 if (hcount > count)
915 hcount = count;
916 result = dvb_dmxdev_buffer_read(&dfil->buffer,
917 file->f_flags & O_NONBLOCK,
918 buf, hcount, ppos);
919 if (result < 0) {
920 dfil->todo = 0;
921 return result;
923 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
924 return -EFAULT;
925 buf += result;
926 done = result;
927 count -= result;
928 dfil->todo -= result;
929 if (dfil->todo > -3)
930 return done;
931 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
932 if (!count)
933 return done;
935 if (count > dfil->todo)
936 count = dfil->todo;
937 result = dvb_dmxdev_buffer_read(&dfil->buffer,
938 file->f_flags & O_NONBLOCK,
939 buf, count, ppos);
940 if (result < 0)
941 return result;
942 dfil->todo -= result;
943 return (result + done);
946 static ssize_t
947 dvb_demux_read(struct file *file, char __user *buf, size_t count,
948 loff_t *ppos)
950 struct dmxdev_filter *dmxdevfilter = file->private_data;
951 int ret;
953 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
954 return -ERESTARTSYS;
956 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
957 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
958 else
959 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
960 file->f_flags & O_NONBLOCK,
961 buf, count, ppos);
963 mutex_unlock(&dmxdevfilter->mutex);
964 return ret;
967 static int dvb_demux_do_ioctl(struct file *file,
968 unsigned int cmd, void *parg)
970 struct dmxdev_filter *dmxdevfilter = file->private_data;
971 struct dmxdev *dmxdev = dmxdevfilter->dev;
972 unsigned long arg = (unsigned long)parg;
973 int ret = 0;
975 if (mutex_lock_interruptible(&dmxdev->mutex))
976 return -ERESTARTSYS;
978 switch (cmd) {
979 case DMX_START:
980 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
981 mutex_unlock(&dmxdev->mutex);
982 return -ERESTARTSYS;
984 if (dmxdevfilter->state < DMXDEV_STATE_SET)
985 ret = -EINVAL;
986 else
987 ret = dvb_dmxdev_filter_start(dmxdevfilter);
988 mutex_unlock(&dmxdevfilter->mutex);
989 break;
991 case DMX_STOP:
992 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
993 mutex_unlock(&dmxdev->mutex);
994 return -ERESTARTSYS;
996 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
997 mutex_unlock(&dmxdevfilter->mutex);
998 break;
1000 case DMX_SET_FILTER:
1001 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1002 mutex_unlock(&dmxdev->mutex);
1003 return -ERESTARTSYS;
1005 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1006 mutex_unlock(&dmxdevfilter->mutex);
1007 break;
1009 case DMX_SET_PES_FILTER:
1010 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1011 mutex_unlock(&dmxdev->mutex);
1012 return -ERESTARTSYS;
1014 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1015 mutex_unlock(&dmxdevfilter->mutex);
1016 break;
1018 case DMX_SET_BUFFER_SIZE:
1019 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1020 mutex_unlock(&dmxdev->mutex);
1021 return -ERESTARTSYS;
1023 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1024 mutex_unlock(&dmxdevfilter->mutex);
1025 break;
1027 case DMX_GET_PES_PIDS:
1028 if (!dmxdev->demux->get_pes_pids) {
1029 ret = -EINVAL;
1030 break;
1032 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1033 break;
1035 case DMX_GET_CAPS:
1036 if (!dmxdev->demux->get_caps) {
1037 ret = -EINVAL;
1038 break;
1040 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
1041 break;
1043 case DMX_SET_SOURCE:
1044 if (!dmxdev->demux->set_source) {
1045 ret = -EINVAL;
1046 break;
1048 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
1049 break;
1051 case DMX_GET_STC:
1052 if (!dmxdev->demux->get_stc) {
1053 ret = -EINVAL;
1054 break;
1056 ret = dmxdev->demux->get_stc(dmxdev->demux,
1057 ((struct dmx_stc *)parg)->num,
1058 &((struct dmx_stc *)parg)->stc,
1059 &((struct dmx_stc *)parg)->base);
1060 break;
1062 case DMX_ADD_PID:
1063 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1064 ret = -ERESTARTSYS;
1065 break;
1067 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1068 mutex_unlock(&dmxdevfilter->mutex);
1069 break;
1071 case DMX_REMOVE_PID:
1072 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1073 ret = -ERESTARTSYS;
1074 break;
1076 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1077 mutex_unlock(&dmxdevfilter->mutex);
1078 break;
1080 default:
1081 ret = -EINVAL;
1082 break;
1084 mutex_unlock(&dmxdev->mutex);
1085 return ret;
1088 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1089 unsigned long arg)
1091 int ret;
1093 lock_kernel();
1094 ret = dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1095 unlock_kernel();
1097 return ret;
1100 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
1102 struct dmxdev_filter *dmxdevfilter = file->private_data;
1103 unsigned int mask = 0;
1105 if (!dmxdevfilter)
1106 return -EINVAL;
1108 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1110 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1111 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1112 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1113 return 0;
1115 if (dmxdevfilter->buffer.error)
1116 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1118 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1119 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1121 return mask;
1124 static int dvb_demux_release(struct inode *inode, struct file *file)
1126 struct dmxdev_filter *dmxdevfilter = file->private_data;
1127 struct dmxdev *dmxdev = dmxdevfilter->dev;
1129 int ret;
1131 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1133 mutex_lock(&dmxdev->mutex);
1134 dmxdev->dvbdev->users--;
1135 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1136 fops_put(file->f_op);
1137 file->f_op = NULL;
1138 mutex_unlock(&dmxdev->mutex);
1139 wake_up(&dmxdev->dvbdev->wait_queue);
1140 } else
1141 mutex_unlock(&dmxdev->mutex);
1143 return ret;
1146 static const struct file_operations dvb_demux_fops = {
1147 .owner = THIS_MODULE,
1148 .read = dvb_demux_read,
1149 .unlocked_ioctl = dvb_demux_ioctl,
1150 .open = dvb_demux_open,
1151 .release = dvb_demux_release,
1152 .poll = dvb_demux_poll,
1155 static struct dvb_device dvbdev_demux = {
1156 .priv = NULL,
1157 .users = 1,
1158 .writers = 1,
1159 .fops = &dvb_demux_fops
1162 static int dvb_dvr_do_ioctl(struct file *file,
1163 unsigned int cmd, void *parg)
1165 struct dvb_device *dvbdev = file->private_data;
1166 struct dmxdev *dmxdev = dvbdev->priv;
1167 unsigned long arg = (unsigned long)parg;
1168 int ret;
1170 if (mutex_lock_interruptible(&dmxdev->mutex))
1171 return -ERESTARTSYS;
1173 switch (cmd) {
1174 case DMX_SET_BUFFER_SIZE:
1175 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1176 break;
1178 default:
1179 ret = -EINVAL;
1180 break;
1182 mutex_unlock(&dmxdev->mutex);
1183 return ret;
1186 static long dvb_dvr_ioctl(struct file *file,
1187 unsigned int cmd, unsigned long arg)
1189 int ret;
1191 lock_kernel();
1192 ret = dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1193 unlock_kernel();
1195 return ret;
1198 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1200 struct dvb_device *dvbdev = file->private_data;
1201 struct dmxdev *dmxdev = dvbdev->priv;
1202 unsigned int mask = 0;
1204 dprintk("function : %s\n", __func__);
1206 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1208 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1209 if (dmxdev->dvr_buffer.error)
1210 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1212 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1213 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1214 } else
1215 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1217 return mask;
1220 static const struct file_operations dvb_dvr_fops = {
1221 .owner = THIS_MODULE,
1222 .read = dvb_dvr_read,
1223 .write = dvb_dvr_write,
1224 .unlocked_ioctl = dvb_dvr_ioctl,
1225 .open = dvb_dvr_open,
1226 .release = dvb_dvr_release,
1227 .poll = dvb_dvr_poll,
1230 static struct dvb_device dvbdev_dvr = {
1231 .priv = NULL,
1232 .readers = 1,
1233 .users = 1,
1234 .fops = &dvb_dvr_fops
1237 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1239 int i;
1241 if (dmxdev->demux->open(dmxdev->demux) < 0)
1242 return -EUSERS;
1244 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1245 if (!dmxdev->filter)
1246 return -ENOMEM;
1248 mutex_init(&dmxdev->mutex);
1249 spin_lock_init(&dmxdev->lock);
1250 for (i = 0; i < dmxdev->filternum; i++) {
1251 dmxdev->filter[i].dev = dmxdev;
1252 dmxdev->filter[i].buffer.data = NULL;
1253 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1254 DMXDEV_STATE_FREE);
1257 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1258 DVB_DEVICE_DEMUX);
1259 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1260 dmxdev, DVB_DEVICE_DVR);
1262 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1264 return 0;
1267 EXPORT_SYMBOL(dvb_dmxdev_init);
1269 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1271 dmxdev->exit=1;
1272 if (dmxdev->dvbdev->users > 1) {
1273 wait_event(dmxdev->dvbdev->wait_queue,
1274 dmxdev->dvbdev->users==1);
1276 if (dmxdev->dvr_dvbdev->users > 1) {
1277 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1278 dmxdev->dvr_dvbdev->users==1);
1281 dvb_unregister_device(dmxdev->dvbdev);
1282 dvb_unregister_device(dmxdev->dvr_dvbdev);
1284 vfree(dmxdev->filter);
1285 dmxdev->filter = NULL;
1286 dmxdev->demux->close(dmxdev->demux);
1289 EXPORT_SYMBOL(dvb_dmxdev_release);