Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob73970cd97af1356e1fdeaea9dd733aab94a5f5c7
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/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
32 #include "dmxdev.h"
34 static int debug;
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)
44 ssize_t free;
46 if (!len)
47 return 0;
48 if (!buf->data)
49 return 0;
51 free = dvb_ringbuffer_free(buf);
52 if (len > free) {
53 dprintk("dmxdev: buffer overflow\n");
54 return -EOVERFLOW;
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)
64 size_t todo;
65 ssize_t avail;
66 ssize_t ret = 0;
68 if (!src->data)
69 return 0;
71 if (src->error) {
72 ret = src->error;
73 dvb_ringbuffer_flush(src);
74 return ret;
77 for (todo = count; todo > 0; todo -= ret) {
78 if (non_blocking && dvb_ringbuffer_empty(src)) {
79 ret = -EWOULDBLOCK;
80 break;
83 ret = wait_event_interruptible(src->queue,
84 !dvb_ringbuffer_empty(src) ||
85 (src->error != 0));
86 if (ret < 0)
87 break;
89 if (src->error) {
90 ret = src->error;
91 dvb_ringbuffer_flush(src);
92 break;
95 avail = dvb_ringbuffer_avail(src);
96 if (avail > todo)
97 avail = todo;
99 ret = dvb_ringbuffer_read_user(src, buf, avail);
100 if (ret < 0)
101 break;
103 buf += ret;
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);
114 if (!head)
115 return NULL;
116 list_for_each(pos, head)
117 if (DMX_FE_ENTRY(pos)->source == type)
118 return DMX_FE_ENTRY(pos);
120 return NULL;
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))
132 return -ERESTARTSYS;
134 if (dmxdev->exit) {
135 mutex_unlock(&dmxdev->mutex);
136 return -ENODEV;
139 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
141 mutex_unlock(&dmxdev->mutex);
142 return -EOPNOTSUPP;
146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
147 void *mem;
148 if (!dvbdev->readers) {
149 mutex_unlock(&dmxdev->mutex);
150 return -EBUSY;
152 mem = vmalloc(DVR_BUFFER_SIZE);
153 if (!mem) {
154 mutex_unlock(&dmxdev->mutex);
155 return -ENOMEM;
157 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
158 dvbdev->readers--;
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);
166 return -EOPNOTSUPP;
169 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
171 if (!front) {
172 mutex_unlock(&dmxdev->mutex);
173 return -EINVAL;
175 dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 dmxdev->demux->connect_frontend(dmxdev->demux, front);
178 dvbdev->users++;
179 mutex_unlock(&dmxdev->mutex);
180 return 0;
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) {
196 dvbdev->readers++;
197 if (dmxdev->dvr_buffer.data) {
198 void *mem = dmxdev->dvr_buffer.data;
199 mb();
200 spin_lock_irq(&dmxdev->lock);
201 dmxdev->dvr_buffer.data = NULL;
202 spin_unlock_irq(&dmxdev->lock);
203 vfree(mem);
206 /* TODO */
207 dvbdev->users--;
208 if (dvbdev->users == 1 && dmxdev->exit == 1) {
209 fops_put(file->f_op);
210 file->f_op = NULL;
211 mutex_unlock(&dmxdev->mutex);
212 wake_up(&dvbdev->wait_queue);
213 } else
214 mutex_unlock(&dmxdev->mutex);
216 return 0;
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;
224 int ret;
226 if (!dmxdev->demux->write)
227 return -EOPNOTSUPP;
228 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
229 return -EINVAL;
230 if (mutex_lock_interruptible(&dmxdev->mutex))
231 return -ERESTARTSYS;
233 if (dmxdev->exit) {
234 mutex_unlock(&dmxdev->mutex);
235 return -ENODEV;
237 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
238 mutex_unlock(&dmxdev->mutex);
239 return ret;
242 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
243 loff_t *ppos)
245 struct dvb_device *dvbdev = file->private_data;
246 struct dmxdev *dmxdev = dvbdev->priv;
248 if (dmxdev->exit)
249 return -ENODEV;
251 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
252 file->f_flags & O_NONBLOCK,
253 buf, count, ppos);
256 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
257 unsigned long size)
259 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
260 void *newmem;
261 void *oldmem;
263 dprintk("function : %s\n", __func__);
265 if (buf->size == size)
266 return 0;
267 if (!size)
268 return -EINVAL;
270 newmem = vmalloc(size);
271 if (!newmem)
272 return -ENOMEM;
274 oldmem = buf->data;
276 spin_lock_irq(&dmxdev->lock);
277 buf->data = newmem;
278 buf->size = size;
280 /* reset and not flush in case the buffer shrinks */
281 dvb_ringbuffer_reset(buf);
282 spin_unlock_irq(&dmxdev->lock);
284 vfree(oldmem);
286 return 0;
289 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 *dmxdevfilter, int state)
292 spin_lock_irq(&dmxdevfilter->dev->lock);
293 dmxdevfilter->state = state;
294 spin_unlock_irq(&dmxdevfilter->dev->lock);
297 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
298 unsigned long size)
300 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
301 void *newmem;
302 void *oldmem;
304 if (buf->size == size)
305 return 0;
306 if (!size)
307 return -EINVAL;
308 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
309 return -EBUSY;
311 newmem = vmalloc(size);
312 if (!newmem)
313 return -ENOMEM;
315 oldmem = buf->data;
317 spin_lock_irq(&dmxdevfilter->dev->lock);
318 buf->data = newmem;
319 buf->size = size;
321 /* reset and not flush in case the buffer shrinks */
322 dvb_ringbuffer_reset(buf);
323 spin_unlock_irq(&dmxdevfilter->dev->lock);
325 vfree(oldmem);
327 return 0;
330 static void dvb_dmxdev_filter_timeout(unsigned long data)
332 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
334 dmxdevfilter->buffer.error = -ETIMEDOUT;
335 spin_lock_irq(&dmxdevfilter->dev->lock);
336 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
337 spin_unlock_irq(&dmxdevfilter->dev->lock);
338 wake_up(&dmxdevfilter->buffer.queue);
341 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
343 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
345 del_timer(&dmxdevfilter->timer);
346 if (para->timeout) {
347 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
348 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
349 dmxdevfilter->timer.expires =
350 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
351 add_timer(&dmxdevfilter->timer);
355 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
356 const u8 *buffer2, size_t buffer2_len,
357 struct dmx_section_filter *filter,
358 enum dmx_success success)
360 struct dmxdev_filter *dmxdevfilter = filter->priv;
361 int ret;
363 if (dmxdevfilter->buffer.error) {
364 wake_up(&dmxdevfilter->buffer.queue);
365 return 0;
367 spin_lock(&dmxdevfilter->dev->lock);
368 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
369 spin_unlock(&dmxdevfilter->dev->lock);
370 return 0;
372 del_timer(&dmxdevfilter->timer);
373 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
374 buffer1[0], buffer1[1],
375 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
376 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
377 buffer1_len);
378 if (ret == buffer1_len) {
379 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
380 buffer2_len);
382 if (ret < 0) {
383 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
384 dmxdevfilter->buffer.error = ret;
386 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
387 dmxdevfilter->state = DMXDEV_STATE_DONE;
388 spin_unlock(&dmxdevfilter->dev->lock);
389 wake_up(&dmxdevfilter->buffer.queue);
390 return 0;
393 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
394 const u8 *buffer2, size_t buffer2_len,
395 struct dmx_ts_feed *feed,
396 enum dmx_success success)
398 struct dmxdev_filter *dmxdevfilter = feed->priv;
399 struct dvb_ringbuffer *buffer;
400 int ret;
402 spin_lock(&dmxdevfilter->dev->lock);
403 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
404 spin_unlock(&dmxdevfilter->dev->lock);
405 return 0;
408 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
409 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
410 buffer = &dmxdevfilter->buffer;
411 else
412 buffer = &dmxdevfilter->dev->dvr_buffer;
413 if (buffer->error) {
414 spin_unlock(&dmxdevfilter->dev->lock);
415 wake_up(&buffer->queue);
416 return 0;
418 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
419 if (ret == buffer1_len)
420 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
421 if (ret < 0) {
422 dvb_ringbuffer_flush(buffer);
423 buffer->error = ret;
425 spin_unlock(&dmxdevfilter->dev->lock);
426 wake_up(&buffer->queue);
427 return 0;
430 /* stop feed but only mark the specified filter as stopped (state set) */
431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
433 struct dmxdev_feed *feed;
435 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
437 switch (dmxdevfilter->type) {
438 case DMXDEV_TYPE_SEC:
439 del_timer(&dmxdevfilter->timer);
440 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
441 break;
442 case DMXDEV_TYPE_PES:
443 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
444 feed->ts->stop_filtering(feed->ts);
445 break;
446 default:
447 return -EINVAL;
449 return 0;
452 /* start feed associated with the specified filter */
453 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
455 struct dmxdev_feed *feed;
456 int ret;
458 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
460 switch (filter->type) {
461 case DMXDEV_TYPE_SEC:
462 return filter->feed.sec->start_filtering(filter->feed.sec);
463 case DMXDEV_TYPE_PES:
464 list_for_each_entry(feed, &filter->feed.ts, next) {
465 ret = feed->ts->start_filtering(feed->ts);
466 if (ret < 0) {
467 dvb_dmxdev_feed_stop(filter);
468 return ret;
471 break;
472 default:
473 return -EINVAL;
476 return 0;
479 /* restart section feed if it has filters left associated with it,
480 otherwise release the feed */
481 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
483 int i;
484 struct dmxdev *dmxdev = filter->dev;
485 u16 pid = filter->params.sec.pid;
487 for (i = 0; i < dmxdev->filternum; i++)
488 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
489 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
490 dmxdev->filter[i].params.sec.pid == pid) {
491 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
492 return 0;
495 filter->dev->demux->release_section_feed(dmxdev->demux,
496 filter->feed.sec);
498 return 0;
501 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
503 struct dmxdev_feed *feed;
504 struct dmx_demux *demux;
506 if (dmxdevfilter->state < DMXDEV_STATE_GO)
507 return 0;
509 switch (dmxdevfilter->type) {
510 case DMXDEV_TYPE_SEC:
511 if (!dmxdevfilter->feed.sec)
512 break;
513 dvb_dmxdev_feed_stop(dmxdevfilter);
514 if (dmxdevfilter->filter.sec)
515 dmxdevfilter->feed.sec->
516 release_filter(dmxdevfilter->feed.sec,
517 dmxdevfilter->filter.sec);
518 dvb_dmxdev_feed_restart(dmxdevfilter);
519 dmxdevfilter->feed.sec = NULL;
520 break;
521 case DMXDEV_TYPE_PES:
522 dvb_dmxdev_feed_stop(dmxdevfilter);
523 demux = dmxdevfilter->dev->demux;
524 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
525 demux->release_ts_feed(demux, feed->ts);
526 feed->ts = NULL;
528 break;
529 default:
530 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
531 return 0;
532 return -EINVAL;
535 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
536 return 0;
539 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
541 struct dmxdev_feed *feed, *tmp;
543 /* delete all PIDs */
544 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
545 list_del(&feed->next);
546 kfree(feed);
549 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
552 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
554 if (dmxdevfilter->state < DMXDEV_STATE_SET)
555 return 0;
557 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
558 dvb_dmxdev_delete_pids(dmxdevfilter);
560 dmxdevfilter->type = DMXDEV_TYPE_NONE;
561 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
562 return 0;
565 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
566 struct dmxdev_filter *filter,
567 struct dmxdev_feed *feed)
569 struct timespec timeout = { 0 };
570 struct dmx_pes_filter_params *para = &filter->params.pes;
571 dmx_output_t otype;
572 int ret;
573 int ts_type;
574 dmx_pes_type_t ts_pes;
575 struct dmx_ts_feed *tsfeed;
577 feed->ts = NULL;
578 otype = para->output;
580 ts_pes = para->pes_type;
582 if (ts_pes < DMX_PES_OTHER)
583 ts_type = TS_DECODER;
584 else
585 ts_type = 0;
587 if (otype == DMX_OUT_TS_TAP)
588 ts_type |= TS_PACKET;
589 else if (otype == DMX_OUT_TSDEMUX_TAP)
590 ts_type |= TS_PACKET | TS_DEMUX;
591 else if (otype == DMX_OUT_TAP)
592 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
594 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
595 dvb_dmxdev_ts_callback);
596 if (ret < 0)
597 return ret;
599 tsfeed = feed->ts;
600 tsfeed->priv = filter;
602 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
603 if (ret < 0) {
604 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
605 return ret;
608 ret = tsfeed->start_filtering(tsfeed);
609 if (ret < 0) {
610 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
611 return ret;
614 return 0;
617 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
619 struct dmxdev *dmxdev = filter->dev;
620 struct dmxdev_feed *feed;
621 void *mem;
622 int ret, i;
624 if (filter->state < DMXDEV_STATE_SET)
625 return -EINVAL;
627 if (filter->state >= DMXDEV_STATE_GO)
628 dvb_dmxdev_filter_stop(filter);
630 if (!filter->buffer.data) {
631 mem = vmalloc(filter->buffer.size);
632 if (!mem)
633 return -ENOMEM;
634 spin_lock_irq(&filter->dev->lock);
635 filter->buffer.data = mem;
636 spin_unlock_irq(&filter->dev->lock);
639 dvb_ringbuffer_flush(&filter->buffer);
641 switch (filter->type) {
642 case DMXDEV_TYPE_SEC:
644 struct dmx_sct_filter_params *para = &filter->params.sec;
645 struct dmx_section_filter **secfilter = &filter->filter.sec;
646 struct dmx_section_feed **secfeed = &filter->feed.sec;
648 *secfilter = NULL;
649 *secfeed = NULL;
652 /* find active filter/feed with same PID */
653 for (i = 0; i < dmxdev->filternum; i++) {
654 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
655 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
656 dmxdev->filter[i].params.sec.pid == para->pid) {
657 *secfeed = dmxdev->filter[i].feed.sec;
658 break;
662 /* if no feed found, try to allocate new one */
663 if (!*secfeed) {
664 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
665 secfeed,
666 dvb_dmxdev_section_callback);
667 if (ret < 0) {
668 printk("DVB (%s): could not alloc feed\n",
669 __func__);
670 return ret;
673 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
674 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
675 if (ret < 0) {
676 printk("DVB (%s): could not set feed\n",
677 __func__);
678 dvb_dmxdev_feed_restart(filter);
679 return ret;
681 } else {
682 dvb_dmxdev_feed_stop(filter);
685 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
686 if (ret < 0) {
687 dvb_dmxdev_feed_restart(filter);
688 filter->feed.sec->start_filtering(*secfeed);
689 dprintk("could not get filter\n");
690 return ret;
693 (*secfilter)->priv = filter;
695 memcpy(&((*secfilter)->filter_value[3]),
696 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
697 memcpy(&(*secfilter)->filter_mask[3],
698 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
699 memcpy(&(*secfilter)->filter_mode[3],
700 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
702 (*secfilter)->filter_value[0] = para->filter.filter[0];
703 (*secfilter)->filter_mask[0] = para->filter.mask[0];
704 (*secfilter)->filter_mode[0] = para->filter.mode[0];
705 (*secfilter)->filter_mask[1] = 0;
706 (*secfilter)->filter_mask[2] = 0;
708 filter->todo = 0;
710 ret = filter->feed.sec->start_filtering(filter->feed.sec);
711 if (ret < 0)
712 return ret;
714 dvb_dmxdev_filter_timer(filter);
715 break;
717 case DMXDEV_TYPE_PES:
718 list_for_each_entry(feed, &filter->feed.ts, next) {
719 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
720 if (ret < 0) {
721 dvb_dmxdev_filter_stop(filter);
722 return ret;
725 break;
726 default:
727 return -EINVAL;
730 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
731 return 0;
734 static int dvb_demux_open(struct inode *inode, struct file *file)
736 struct dvb_device *dvbdev = file->private_data;
737 struct dmxdev *dmxdev = dvbdev->priv;
738 int i;
739 struct dmxdev_filter *dmxdevfilter;
741 if (!dmxdev->filter)
742 return -EINVAL;
744 if (mutex_lock_interruptible(&dmxdev->mutex))
745 return -ERESTARTSYS;
747 for (i = 0; i < dmxdev->filternum; i++)
748 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
749 break;
751 if (i == dmxdev->filternum) {
752 mutex_unlock(&dmxdev->mutex);
753 return -EMFILE;
756 dmxdevfilter = &dmxdev->filter[i];
757 mutex_init(&dmxdevfilter->mutex);
758 file->private_data = dmxdevfilter;
760 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
761 dmxdevfilter->type = DMXDEV_TYPE_NONE;
762 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
763 init_timer(&dmxdevfilter->timer);
765 dvbdev->users++;
767 mutex_unlock(&dmxdev->mutex);
768 return 0;
771 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
772 struct dmxdev_filter *dmxdevfilter)
774 mutex_lock(&dmxdev->mutex);
775 mutex_lock(&dmxdevfilter->mutex);
777 dvb_dmxdev_filter_stop(dmxdevfilter);
778 dvb_dmxdev_filter_reset(dmxdevfilter);
780 if (dmxdevfilter->buffer.data) {
781 void *mem = dmxdevfilter->buffer.data;
783 spin_lock_irq(&dmxdev->lock);
784 dmxdevfilter->buffer.data = NULL;
785 spin_unlock_irq(&dmxdev->lock);
786 vfree(mem);
789 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
790 wake_up(&dmxdevfilter->buffer.queue);
791 mutex_unlock(&dmxdevfilter->mutex);
792 mutex_unlock(&dmxdev->mutex);
793 return 0;
796 static inline void invert_mode(dmx_filter_t *filter)
798 int i;
800 for (i = 0; i < DMX_FILTER_SIZE; i++)
801 filter->mode[i] ^= 0xff;
804 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
805 struct dmxdev_filter *filter, u16 pid)
807 struct dmxdev_feed *feed;
809 if ((filter->type != DMXDEV_TYPE_PES) ||
810 (filter->state < DMXDEV_STATE_SET))
811 return -EINVAL;
813 /* only TS packet filters may have multiple PIDs */
814 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
815 (!list_empty(&filter->feed.ts)))
816 return -EINVAL;
818 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
819 if (feed == NULL)
820 return -ENOMEM;
822 feed->pid = pid;
823 list_add(&feed->next, &filter->feed.ts);
825 if (filter->state >= DMXDEV_STATE_GO)
826 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
828 return 0;
831 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
832 struct dmxdev_filter *filter, u16 pid)
834 struct dmxdev_feed *feed, *tmp;
836 if ((filter->type != DMXDEV_TYPE_PES) ||
837 (filter->state < DMXDEV_STATE_SET))
838 return -EINVAL;
840 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
841 if ((feed->pid == pid) && (feed->ts != NULL)) {
842 feed->ts->stop_filtering(feed->ts);
843 filter->dev->demux->release_ts_feed(filter->dev->demux,
844 feed->ts);
845 list_del(&feed->next);
846 kfree(feed);
850 return 0;
853 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
854 struct dmxdev_filter *dmxdevfilter,
855 struct dmx_sct_filter_params *params)
857 dprintk("function : %s\n", __func__);
859 dvb_dmxdev_filter_stop(dmxdevfilter);
861 dmxdevfilter->type = DMXDEV_TYPE_SEC;
862 memcpy(&dmxdevfilter->params.sec,
863 params, sizeof(struct dmx_sct_filter_params));
864 invert_mode(&dmxdevfilter->params.sec.filter);
865 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
867 if (params->flags & DMX_IMMEDIATE_START)
868 return dvb_dmxdev_filter_start(dmxdevfilter);
870 return 0;
873 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
874 struct dmxdev_filter *dmxdevfilter,
875 struct dmx_pes_filter_params *params)
877 int ret;
879 dvb_dmxdev_filter_stop(dmxdevfilter);
880 dvb_dmxdev_filter_reset(dmxdevfilter);
882 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
883 return -EINVAL;
885 dmxdevfilter->type = DMXDEV_TYPE_PES;
886 memcpy(&dmxdevfilter->params, params,
887 sizeof(struct dmx_pes_filter_params));
888 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
890 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
892 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
893 dmxdevfilter->params.pes.pid);
894 if (ret < 0)
895 return ret;
897 if (params->flags & DMX_IMMEDIATE_START)
898 return dvb_dmxdev_filter_start(dmxdevfilter);
900 return 0;
903 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
904 struct file *file, char __user *buf,
905 size_t count, loff_t *ppos)
907 int result, hcount;
908 int done = 0;
910 if (dfil->todo <= 0) {
911 hcount = 3 + dfil->todo;
912 if (hcount > count)
913 hcount = count;
914 result = dvb_dmxdev_buffer_read(&dfil->buffer,
915 file->f_flags & O_NONBLOCK,
916 buf, hcount, ppos);
917 if (result < 0) {
918 dfil->todo = 0;
919 return result;
921 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
922 return -EFAULT;
923 buf += result;
924 done = result;
925 count -= result;
926 dfil->todo -= result;
927 if (dfil->todo > -3)
928 return done;
929 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
930 if (!count)
931 return done;
933 if (count > dfil->todo)
934 count = dfil->todo;
935 result = dvb_dmxdev_buffer_read(&dfil->buffer,
936 file->f_flags & O_NONBLOCK,
937 buf, count, ppos);
938 if (result < 0)
939 return result;
940 dfil->todo -= result;
941 return (result + done);
944 static ssize_t
945 dvb_demux_read(struct file *file, char __user *buf, size_t count,
946 loff_t *ppos)
948 struct dmxdev_filter *dmxdevfilter = file->private_data;
949 int ret;
951 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
952 return -ERESTARTSYS;
954 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
955 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
956 else
957 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
958 file->f_flags & O_NONBLOCK,
959 buf, count, ppos);
961 mutex_unlock(&dmxdevfilter->mutex);
962 return ret;
965 static int dvb_demux_do_ioctl(struct file *file,
966 unsigned int cmd, void *parg)
968 struct dmxdev_filter *dmxdevfilter = file->private_data;
969 struct dmxdev *dmxdev = dmxdevfilter->dev;
970 unsigned long arg = (unsigned long)parg;
971 int ret = 0;
973 if (mutex_lock_interruptible(&dmxdev->mutex))
974 return -ERESTARTSYS;
976 switch (cmd) {
977 case DMX_START:
978 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
979 mutex_unlock(&dmxdev->mutex);
980 return -ERESTARTSYS;
982 if (dmxdevfilter->state < DMXDEV_STATE_SET)
983 ret = -EINVAL;
984 else
985 ret = dvb_dmxdev_filter_start(dmxdevfilter);
986 mutex_unlock(&dmxdevfilter->mutex);
987 break;
989 case DMX_STOP:
990 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
991 mutex_unlock(&dmxdev->mutex);
992 return -ERESTARTSYS;
994 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
995 mutex_unlock(&dmxdevfilter->mutex);
996 break;
998 case DMX_SET_FILTER:
999 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1000 mutex_unlock(&dmxdev->mutex);
1001 return -ERESTARTSYS;
1003 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1004 mutex_unlock(&dmxdevfilter->mutex);
1005 break;
1007 case DMX_SET_PES_FILTER:
1008 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1009 mutex_unlock(&dmxdev->mutex);
1010 return -ERESTARTSYS;
1012 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1013 mutex_unlock(&dmxdevfilter->mutex);
1014 break;
1016 case DMX_SET_BUFFER_SIZE:
1017 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1018 mutex_unlock(&dmxdev->mutex);
1019 return -ERESTARTSYS;
1021 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1022 mutex_unlock(&dmxdevfilter->mutex);
1023 break;
1025 case DMX_GET_PES_PIDS:
1026 if (!dmxdev->demux->get_pes_pids) {
1027 ret = -EINVAL;
1028 break;
1030 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1031 break;
1033 case DMX_GET_CAPS:
1034 if (!dmxdev->demux->get_caps) {
1035 ret = -EINVAL;
1036 break;
1038 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
1039 break;
1041 case DMX_SET_SOURCE:
1042 if (!dmxdev->demux->set_source) {
1043 ret = -EINVAL;
1044 break;
1046 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
1047 break;
1049 case DMX_GET_STC:
1050 if (!dmxdev->demux->get_stc) {
1051 ret = -EINVAL;
1052 break;
1054 ret = dmxdev->demux->get_stc(dmxdev->demux,
1055 ((struct dmx_stc *)parg)->num,
1056 &((struct dmx_stc *)parg)->stc,
1057 &((struct dmx_stc *)parg)->base);
1058 break;
1060 case DMX_ADD_PID:
1061 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1062 ret = -ERESTARTSYS;
1063 break;
1065 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1066 mutex_unlock(&dmxdevfilter->mutex);
1067 break;
1069 case DMX_REMOVE_PID:
1070 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1071 ret = -ERESTARTSYS;
1072 break;
1074 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1075 mutex_unlock(&dmxdevfilter->mutex);
1076 break;
1078 default:
1079 ret = -EINVAL;
1080 break;
1082 mutex_unlock(&dmxdev->mutex);
1083 return ret;
1086 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1087 unsigned long arg)
1089 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1092 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
1094 struct dmxdev_filter *dmxdevfilter = file->private_data;
1095 unsigned int mask = 0;
1097 if (!dmxdevfilter)
1098 return -EINVAL;
1100 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1102 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1103 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1104 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1105 return 0;
1107 if (dmxdevfilter->buffer.error)
1108 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1110 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1111 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1113 return mask;
1116 static int dvb_demux_release(struct inode *inode, struct file *file)
1118 struct dmxdev_filter *dmxdevfilter = file->private_data;
1119 struct dmxdev *dmxdev = dmxdevfilter->dev;
1121 int ret;
1123 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1125 mutex_lock(&dmxdev->mutex);
1126 dmxdev->dvbdev->users--;
1127 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1128 fops_put(file->f_op);
1129 file->f_op = NULL;
1130 mutex_unlock(&dmxdev->mutex);
1131 wake_up(&dmxdev->dvbdev->wait_queue);
1132 } else
1133 mutex_unlock(&dmxdev->mutex);
1135 return ret;
1138 static const struct file_operations dvb_demux_fops = {
1139 .owner = THIS_MODULE,
1140 .read = dvb_demux_read,
1141 .unlocked_ioctl = dvb_demux_ioctl,
1142 .open = dvb_demux_open,
1143 .release = dvb_demux_release,
1144 .poll = dvb_demux_poll,
1145 .llseek = default_llseek,
1148 static struct dvb_device dvbdev_demux = {
1149 .priv = NULL,
1150 .users = 1,
1151 .writers = 1,
1152 .fops = &dvb_demux_fops
1155 static int dvb_dvr_do_ioctl(struct file *file,
1156 unsigned int cmd, void *parg)
1158 struct dvb_device *dvbdev = file->private_data;
1159 struct dmxdev *dmxdev = dvbdev->priv;
1160 unsigned long arg = (unsigned long)parg;
1161 int ret;
1163 if (mutex_lock_interruptible(&dmxdev->mutex))
1164 return -ERESTARTSYS;
1166 switch (cmd) {
1167 case DMX_SET_BUFFER_SIZE:
1168 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1169 break;
1171 default:
1172 ret = -EINVAL;
1173 break;
1175 mutex_unlock(&dmxdev->mutex);
1176 return ret;
1179 static long dvb_dvr_ioctl(struct file *file,
1180 unsigned int cmd, unsigned long arg)
1182 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1185 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1187 struct dvb_device *dvbdev = file->private_data;
1188 struct dmxdev *dmxdev = dvbdev->priv;
1189 unsigned int mask = 0;
1191 dprintk("function : %s\n", __func__);
1193 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1195 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1196 if (dmxdev->dvr_buffer.error)
1197 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1199 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1200 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1201 } else
1202 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1204 return mask;
1207 static const struct file_operations dvb_dvr_fops = {
1208 .owner = THIS_MODULE,
1209 .read = dvb_dvr_read,
1210 .write = dvb_dvr_write,
1211 .unlocked_ioctl = dvb_dvr_ioctl,
1212 .open = dvb_dvr_open,
1213 .release = dvb_dvr_release,
1214 .poll = dvb_dvr_poll,
1215 .llseek = default_llseek,
1218 static struct dvb_device dvbdev_dvr = {
1219 .priv = NULL,
1220 .readers = 1,
1221 .users = 1,
1222 .fops = &dvb_dvr_fops
1225 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1227 int i;
1229 if (dmxdev->demux->open(dmxdev->demux) < 0)
1230 return -EUSERS;
1232 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1233 if (!dmxdev->filter)
1234 return -ENOMEM;
1236 mutex_init(&dmxdev->mutex);
1237 spin_lock_init(&dmxdev->lock);
1238 for (i = 0; i < dmxdev->filternum; i++) {
1239 dmxdev->filter[i].dev = dmxdev;
1240 dmxdev->filter[i].buffer.data = NULL;
1241 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1242 DMXDEV_STATE_FREE);
1245 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1246 DVB_DEVICE_DEMUX);
1247 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1248 dmxdev, DVB_DEVICE_DVR);
1250 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1252 return 0;
1255 EXPORT_SYMBOL(dvb_dmxdev_init);
1257 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1259 dmxdev->exit=1;
1260 if (dmxdev->dvbdev->users > 1) {
1261 wait_event(dmxdev->dvbdev->wait_queue,
1262 dmxdev->dvbdev->users==1);
1264 if (dmxdev->dvr_dvbdev->users > 1) {
1265 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1266 dmxdev->dvr_dvbdev->users==1);
1269 dvb_unregister_device(dmxdev->dvbdev);
1270 dvb_unregister_device(dmxdev->dvr_dvbdev);
1272 vfree(dmxdev->filter);
1273 dmxdev->filter = NULL;
1274 dmxdev->demux->close(dmxdev->demux);
1277 EXPORT_SYMBOL(dvb_dmxdev_release);