Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / media / dvb / dvb-core / dmxdev.c
blobe4b5c03ae516a4900c2aef889eeae40677e222e6
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 <asm/system.h>
33 #include "dmxdev.h"
35 static int debug;
37 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
40 #define dprintk if (debug) printk
42 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
43 const u8 *src, size_t len)
45 ssize_t free;
47 if (!len)
48 return 0;
49 if (!buf->data)
50 return 0;
52 free = dvb_ringbuffer_free(buf);
53 if (len > free) {
54 dprintk("dmxdev: buffer overflow\n");
55 return -EOVERFLOW;
58 return dvb_ringbuffer_write(buf, src, len);
61 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
62 int non_blocking, char __user *buf,
63 size_t count, loff_t *ppos)
65 size_t todo;
66 ssize_t avail;
67 ssize_t ret = 0;
69 if (!src->data)
70 return 0;
72 if (src->error) {
73 ret = src->error;
74 dvb_ringbuffer_flush(src);
75 return ret;
78 for (todo = count; todo > 0; todo -= ret) {
79 if (non_blocking && dvb_ringbuffer_empty(src)) {
80 ret = -EWOULDBLOCK;
81 break;
84 ret = wait_event_interruptible(src->queue,
85 !dvb_ringbuffer_empty(src) ||
86 (src->error != 0));
87 if (ret < 0)
88 break;
90 if (src->error) {
91 ret = src->error;
92 dvb_ringbuffer_flush(src);
93 break;
96 avail = dvb_ringbuffer_avail(src);
97 if (avail > todo)
98 avail = todo;
100 ret = dvb_ringbuffer_read_user(src, buf, avail);
101 if (ret < 0)
102 break;
104 buf += ret;
107 return (count - todo) ? (count - todo) : ret;
110 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
112 struct list_head *head, *pos;
114 head = demux->get_frontends(demux);
115 if (!head)
116 return NULL;
117 list_for_each(pos, head)
118 if (DMX_FE_ENTRY(pos)->source == type)
119 return DMX_FE_ENTRY(pos);
121 return NULL;
124 static int dvb_dvr_open(struct inode *inode, struct file *file)
126 struct dvb_device *dvbdev = file->private_data;
127 struct dmxdev *dmxdev = dvbdev->priv;
128 struct dmx_frontend *front;
130 dprintk("function : %s\n", __func__);
132 if (mutex_lock_interruptible(&dmxdev->mutex))
133 return -ERESTARTSYS;
135 if (dmxdev->exit) {
136 mutex_unlock(&dmxdev->mutex);
137 return -ENODEV;
140 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
141 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
142 mutex_unlock(&dmxdev->mutex);
143 return -EOPNOTSUPP;
147 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148 void *mem;
149 if (!dvbdev->readers) {
150 mutex_unlock(&dmxdev->mutex);
151 return -EBUSY;
153 mem = vmalloc(DVR_BUFFER_SIZE);
154 if (!mem) {
155 mutex_unlock(&dmxdev->mutex);
156 return -ENOMEM;
158 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
159 dvbdev->readers--;
162 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
163 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
165 if (!dmxdev->demux->write) {
166 mutex_unlock(&dmxdev->mutex);
167 return -EOPNOTSUPP;
170 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
172 if (!front) {
173 mutex_unlock(&dmxdev->mutex);
174 return -EINVAL;
176 dmxdev->demux->disconnect_frontend(dmxdev->demux);
177 dmxdev->demux->connect_frontend(dmxdev->demux, front);
179 dvbdev->users++;
180 mutex_unlock(&dmxdev->mutex);
181 return 0;
184 static int dvb_dvr_release(struct inode *inode, struct file *file)
186 struct dvb_device *dvbdev = file->private_data;
187 struct dmxdev *dmxdev = dvbdev->priv;
189 mutex_lock(&dmxdev->mutex);
191 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
192 dmxdev->demux->disconnect_frontend(dmxdev->demux);
193 dmxdev->demux->connect_frontend(dmxdev->demux,
194 dmxdev->dvr_orig_fe);
196 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
197 dvbdev->readers++;
198 if (dmxdev->dvr_buffer.data) {
199 void *mem = dmxdev->dvr_buffer.data;
200 mb();
201 spin_lock_irq(&dmxdev->lock);
202 dmxdev->dvr_buffer.data = NULL;
203 spin_unlock_irq(&dmxdev->lock);
204 vfree(mem);
207 /* TODO */
208 dvbdev->users--;
209 if (dvbdev->users == 1 && dmxdev->exit == 1) {
210 fops_put(file->f_op);
211 file->f_op = NULL;
212 mutex_unlock(&dmxdev->mutex);
213 wake_up(&dvbdev->wait_queue);
214 } else
215 mutex_unlock(&dmxdev->mutex);
217 return 0;
220 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
221 size_t count, loff_t *ppos)
223 struct dvb_device *dvbdev = file->private_data;
224 struct dmxdev *dmxdev = dvbdev->priv;
225 int ret;
227 if (!dmxdev->demux->write)
228 return -EOPNOTSUPP;
229 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
230 return -EINVAL;
231 if (mutex_lock_interruptible(&dmxdev->mutex))
232 return -ERESTARTSYS;
234 if (dmxdev->exit) {
235 mutex_unlock(&dmxdev->mutex);
236 return -ENODEV;
238 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
239 mutex_unlock(&dmxdev->mutex);
240 return ret;
243 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
244 loff_t *ppos)
246 struct dvb_device *dvbdev = file->private_data;
247 struct dmxdev *dmxdev = dvbdev->priv;
249 if (dmxdev->exit)
250 return -ENODEV;
252 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
253 file->f_flags & O_NONBLOCK,
254 buf, count, ppos);
257 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
258 unsigned long size)
260 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
261 void *newmem;
262 void *oldmem;
264 dprintk("function : %s\n", __func__);
266 if (buf->size == size)
267 return 0;
268 if (!size)
269 return -EINVAL;
271 newmem = vmalloc(size);
272 if (!newmem)
273 return -ENOMEM;
275 oldmem = buf->data;
277 spin_lock_irq(&dmxdev->lock);
278 buf->data = newmem;
279 buf->size = size;
281 /* reset and not flush in case the buffer shrinks */
282 dvb_ringbuffer_reset(buf);
283 spin_unlock_irq(&dmxdev->lock);
285 vfree(oldmem);
287 return 0;
290 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
291 *dmxdevfilter, int state)
293 spin_lock_irq(&dmxdevfilter->dev->lock);
294 dmxdevfilter->state = state;
295 spin_unlock_irq(&dmxdevfilter->dev->lock);
298 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
299 unsigned long size)
301 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
302 void *newmem;
303 void *oldmem;
305 if (buf->size == size)
306 return 0;
307 if (!size)
308 return -EINVAL;
309 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
310 return -EBUSY;
312 newmem = vmalloc(size);
313 if (!newmem)
314 return -ENOMEM;
316 oldmem = buf->data;
318 spin_lock_irq(&dmxdevfilter->dev->lock);
319 buf->data = newmem;
320 buf->size = size;
322 /* reset and not flush in case the buffer shrinks */
323 dvb_ringbuffer_reset(buf);
324 spin_unlock_irq(&dmxdevfilter->dev->lock);
326 vfree(oldmem);
328 return 0;
331 static void dvb_dmxdev_filter_timeout(unsigned long data)
333 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
335 dmxdevfilter->buffer.error = -ETIMEDOUT;
336 spin_lock_irq(&dmxdevfilter->dev->lock);
337 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
338 spin_unlock_irq(&dmxdevfilter->dev->lock);
339 wake_up(&dmxdevfilter->buffer.queue);
342 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
344 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
346 del_timer(&dmxdevfilter->timer);
347 if (para->timeout) {
348 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
349 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
350 dmxdevfilter->timer.expires =
351 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
352 add_timer(&dmxdevfilter->timer);
356 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
357 const u8 *buffer2, size_t buffer2_len,
358 struct dmx_section_filter *filter,
359 enum dmx_success success)
361 struct dmxdev_filter *dmxdevfilter = filter->priv;
362 int ret;
364 if (dmxdevfilter->buffer.error) {
365 wake_up(&dmxdevfilter->buffer.queue);
366 return 0;
368 spin_lock(&dmxdevfilter->dev->lock);
369 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
370 spin_unlock(&dmxdevfilter->dev->lock);
371 return 0;
373 del_timer(&dmxdevfilter->timer);
374 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
375 buffer1[0], buffer1[1],
376 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
377 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
378 buffer1_len);
379 if (ret == buffer1_len) {
380 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
381 buffer2_len);
383 if (ret < 0) {
384 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
385 dmxdevfilter->buffer.error = ret;
387 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
388 dmxdevfilter->state = DMXDEV_STATE_DONE;
389 spin_unlock(&dmxdevfilter->dev->lock);
390 wake_up(&dmxdevfilter->buffer.queue);
391 return 0;
394 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
395 const u8 *buffer2, size_t buffer2_len,
396 struct dmx_ts_feed *feed,
397 enum dmx_success success)
399 struct dmxdev_filter *dmxdevfilter = feed->priv;
400 struct dvb_ringbuffer *buffer;
401 int ret;
403 spin_lock(&dmxdevfilter->dev->lock);
404 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
405 spin_unlock(&dmxdevfilter->dev->lock);
406 return 0;
409 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
410 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
411 buffer = &dmxdevfilter->buffer;
412 else
413 buffer = &dmxdevfilter->dev->dvr_buffer;
414 if (buffer->error) {
415 spin_unlock(&dmxdevfilter->dev->lock);
416 wake_up(&buffer->queue);
417 return 0;
419 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
420 if (ret == buffer1_len)
421 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
422 if (ret < 0) {
423 dvb_ringbuffer_flush(buffer);
424 buffer->error = ret;
426 spin_unlock(&dmxdevfilter->dev->lock);
427 wake_up(&buffer->queue);
428 return 0;
431 /* stop feed but only mark the specified filter as stopped (state set) */
432 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
434 struct dmxdev_feed *feed;
436 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
438 switch (dmxdevfilter->type) {
439 case DMXDEV_TYPE_SEC:
440 del_timer(&dmxdevfilter->timer);
441 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
442 break;
443 case DMXDEV_TYPE_PES:
444 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
445 feed->ts->stop_filtering(feed->ts);
446 break;
447 default:
448 return -EINVAL;
450 return 0;
453 /* start feed associated with the specified filter */
454 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
456 struct dmxdev_feed *feed;
457 int ret;
459 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
461 switch (filter->type) {
462 case DMXDEV_TYPE_SEC:
463 return filter->feed.sec->start_filtering(filter->feed.sec);
464 case DMXDEV_TYPE_PES:
465 list_for_each_entry(feed, &filter->feed.ts, next) {
466 ret = feed->ts->start_filtering(feed->ts);
467 if (ret < 0) {
468 dvb_dmxdev_feed_stop(filter);
469 return ret;
472 break;
473 default:
474 return -EINVAL;
477 return 0;
480 /* restart section feed if it has filters left associated with it,
481 otherwise release the feed */
482 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
484 int i;
485 struct dmxdev *dmxdev = filter->dev;
486 u16 pid = filter->params.sec.pid;
488 for (i = 0; i < dmxdev->filternum; i++)
489 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
490 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
491 dmxdev->filter[i].params.sec.pid == pid) {
492 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
493 return 0;
496 filter->dev->demux->release_section_feed(dmxdev->demux,
497 filter->feed.sec);
499 return 0;
502 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
504 struct dmxdev_feed *feed;
505 struct dmx_demux *demux;
507 if (dmxdevfilter->state < DMXDEV_STATE_GO)
508 return 0;
510 switch (dmxdevfilter->type) {
511 case DMXDEV_TYPE_SEC:
512 if (!dmxdevfilter->feed.sec)
513 break;
514 dvb_dmxdev_feed_stop(dmxdevfilter);
515 if (dmxdevfilter->filter.sec)
516 dmxdevfilter->feed.sec->
517 release_filter(dmxdevfilter->feed.sec,
518 dmxdevfilter->filter.sec);
519 dvb_dmxdev_feed_restart(dmxdevfilter);
520 dmxdevfilter->feed.sec = NULL;
521 break;
522 case DMXDEV_TYPE_PES:
523 dvb_dmxdev_feed_stop(dmxdevfilter);
524 demux = dmxdevfilter->dev->demux;
525 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
526 demux->release_ts_feed(demux, feed->ts);
527 feed->ts = NULL;
529 break;
530 default:
531 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
532 return 0;
533 return -EINVAL;
536 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
537 return 0;
540 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
542 struct dmxdev_feed *feed, *tmp;
544 /* delete all PIDs */
545 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
546 list_del(&feed->next);
547 kfree(feed);
550 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
553 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
555 if (dmxdevfilter->state < DMXDEV_STATE_SET)
556 return 0;
558 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
559 dvb_dmxdev_delete_pids(dmxdevfilter);
561 dmxdevfilter->type = DMXDEV_TYPE_NONE;
562 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
563 return 0;
566 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
567 struct dmxdev_filter *filter,
568 struct dmxdev_feed *feed)
570 struct timespec timeout = { 0 };
571 struct dmx_pes_filter_params *para = &filter->params.pes;
572 dmx_output_t otype;
573 int ret;
574 int ts_type;
575 dmx_pes_type_t ts_pes;
576 struct dmx_ts_feed *tsfeed;
578 feed->ts = NULL;
579 otype = para->output;
581 ts_pes = para->pes_type;
583 if (ts_pes < DMX_PES_OTHER)
584 ts_type = TS_DECODER;
585 else
586 ts_type = 0;
588 if (otype == DMX_OUT_TS_TAP)
589 ts_type |= TS_PACKET;
590 else if (otype == DMX_OUT_TSDEMUX_TAP)
591 ts_type |= TS_PACKET | TS_DEMUX;
592 else if (otype == DMX_OUT_TAP)
593 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
595 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
596 dvb_dmxdev_ts_callback);
597 if (ret < 0)
598 return ret;
600 tsfeed = feed->ts;
601 tsfeed->priv = filter;
603 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
604 if (ret < 0) {
605 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
606 return ret;
609 ret = tsfeed->start_filtering(tsfeed);
610 if (ret < 0) {
611 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
612 return ret;
615 return 0;
618 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
620 struct dmxdev *dmxdev = filter->dev;
621 struct dmxdev_feed *feed;
622 void *mem;
623 int ret, i;
625 if (filter->state < DMXDEV_STATE_SET)
626 return -EINVAL;
628 if (filter->state >= DMXDEV_STATE_GO)
629 dvb_dmxdev_filter_stop(filter);
631 if (!filter->buffer.data) {
632 mem = vmalloc(filter->buffer.size);
633 if (!mem)
634 return -ENOMEM;
635 spin_lock_irq(&filter->dev->lock);
636 filter->buffer.data = mem;
637 spin_unlock_irq(&filter->dev->lock);
640 dvb_ringbuffer_flush(&filter->buffer);
642 switch (filter->type) {
643 case DMXDEV_TYPE_SEC:
645 struct dmx_sct_filter_params *para = &filter->params.sec;
646 struct dmx_section_filter **secfilter = &filter->filter.sec;
647 struct dmx_section_feed **secfeed = &filter->feed.sec;
649 *secfilter = NULL;
650 *secfeed = NULL;
653 /* find active filter/feed with same PID */
654 for (i = 0; i < dmxdev->filternum; i++) {
655 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
656 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
657 dmxdev->filter[i].params.sec.pid == para->pid) {
658 *secfeed = dmxdev->filter[i].feed.sec;
659 break;
663 /* if no feed found, try to allocate new one */
664 if (!*secfeed) {
665 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
666 secfeed,
667 dvb_dmxdev_section_callback);
668 if (ret < 0) {
669 printk("DVB (%s): could not alloc feed\n",
670 __func__);
671 return ret;
674 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
675 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
676 if (ret < 0) {
677 printk("DVB (%s): could not set feed\n",
678 __func__);
679 dvb_dmxdev_feed_restart(filter);
680 return ret;
682 } else {
683 dvb_dmxdev_feed_stop(filter);
686 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
687 if (ret < 0) {
688 dvb_dmxdev_feed_restart(filter);
689 filter->feed.sec->start_filtering(*secfeed);
690 dprintk("could not get filter\n");
691 return ret;
694 (*secfilter)->priv = filter;
696 memcpy(&((*secfilter)->filter_value[3]),
697 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
698 memcpy(&(*secfilter)->filter_mask[3],
699 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
700 memcpy(&(*secfilter)->filter_mode[3],
701 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
703 (*secfilter)->filter_value[0] = para->filter.filter[0];
704 (*secfilter)->filter_mask[0] = para->filter.mask[0];
705 (*secfilter)->filter_mode[0] = para->filter.mode[0];
706 (*secfilter)->filter_mask[1] = 0;
707 (*secfilter)->filter_mask[2] = 0;
709 filter->todo = 0;
711 ret = filter->feed.sec->start_filtering(filter->feed.sec);
712 if (ret < 0)
713 return ret;
715 dvb_dmxdev_filter_timer(filter);
716 break;
718 case DMXDEV_TYPE_PES:
719 list_for_each_entry(feed, &filter->feed.ts, next) {
720 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
721 if (ret < 0) {
722 dvb_dmxdev_filter_stop(filter);
723 return ret;
726 break;
727 default:
728 return -EINVAL;
731 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
732 return 0;
735 static int dvb_demux_open(struct inode *inode, struct file *file)
737 struct dvb_device *dvbdev = file->private_data;
738 struct dmxdev *dmxdev = dvbdev->priv;
739 int i;
740 struct dmxdev_filter *dmxdevfilter;
742 if (!dmxdev->filter)
743 return -EINVAL;
745 if (mutex_lock_interruptible(&dmxdev->mutex))
746 return -ERESTARTSYS;
748 for (i = 0; i < dmxdev->filternum; i++)
749 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
750 break;
752 if (i == dmxdev->filternum) {
753 mutex_unlock(&dmxdev->mutex);
754 return -EMFILE;
757 dmxdevfilter = &dmxdev->filter[i];
758 mutex_init(&dmxdevfilter->mutex);
759 file->private_data = dmxdevfilter;
761 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
762 dmxdevfilter->type = DMXDEV_TYPE_NONE;
763 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
764 init_timer(&dmxdevfilter->timer);
766 dvbdev->users++;
768 mutex_unlock(&dmxdev->mutex);
769 return 0;
772 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
773 struct dmxdev_filter *dmxdevfilter)
775 mutex_lock(&dmxdev->mutex);
776 mutex_lock(&dmxdevfilter->mutex);
778 dvb_dmxdev_filter_stop(dmxdevfilter);
779 dvb_dmxdev_filter_reset(dmxdevfilter);
781 if (dmxdevfilter->buffer.data) {
782 void *mem = dmxdevfilter->buffer.data;
784 spin_lock_irq(&dmxdev->lock);
785 dmxdevfilter->buffer.data = NULL;
786 spin_unlock_irq(&dmxdev->lock);
787 vfree(mem);
790 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
791 wake_up(&dmxdevfilter->buffer.queue);
792 mutex_unlock(&dmxdevfilter->mutex);
793 mutex_unlock(&dmxdev->mutex);
794 return 0;
797 static inline void invert_mode(dmx_filter_t *filter)
799 int i;
801 for (i = 0; i < DMX_FILTER_SIZE; i++)
802 filter->mode[i] ^= 0xff;
805 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
806 struct dmxdev_filter *filter, u16 pid)
808 struct dmxdev_feed *feed;
810 if ((filter->type != DMXDEV_TYPE_PES) ||
811 (filter->state < DMXDEV_STATE_SET))
812 return -EINVAL;
814 /* only TS packet filters may have multiple PIDs */
815 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
816 (!list_empty(&filter->feed.ts)))
817 return -EINVAL;
819 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
820 if (feed == NULL)
821 return -ENOMEM;
823 feed->pid = pid;
824 list_add(&feed->next, &filter->feed.ts);
826 if (filter->state >= DMXDEV_STATE_GO)
827 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
829 return 0;
832 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
833 struct dmxdev_filter *filter, u16 pid)
835 struct dmxdev_feed *feed, *tmp;
837 if ((filter->type != DMXDEV_TYPE_PES) ||
838 (filter->state < DMXDEV_STATE_SET))
839 return -EINVAL;
841 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
842 if ((feed->pid == pid) && (feed->ts != NULL)) {
843 feed->ts->stop_filtering(feed->ts);
844 filter->dev->demux->release_ts_feed(filter->dev->demux,
845 feed->ts);
846 list_del(&feed->next);
847 kfree(feed);
851 return 0;
854 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
855 struct dmxdev_filter *dmxdevfilter,
856 struct dmx_sct_filter_params *params)
858 dprintk("function : %s\n", __func__);
860 dvb_dmxdev_filter_stop(dmxdevfilter);
862 dmxdevfilter->type = DMXDEV_TYPE_SEC;
863 memcpy(&dmxdevfilter->params.sec,
864 params, sizeof(struct dmx_sct_filter_params));
865 invert_mode(&dmxdevfilter->params.sec.filter);
866 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
868 if (params->flags & DMX_IMMEDIATE_START)
869 return dvb_dmxdev_filter_start(dmxdevfilter);
871 return 0;
874 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
875 struct dmxdev_filter *dmxdevfilter,
876 struct dmx_pes_filter_params *params)
878 int ret;
880 dvb_dmxdev_filter_stop(dmxdevfilter);
881 dvb_dmxdev_filter_reset(dmxdevfilter);
883 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
884 return -EINVAL;
886 dmxdevfilter->type = DMXDEV_TYPE_PES;
887 memcpy(&dmxdevfilter->params, params,
888 sizeof(struct dmx_pes_filter_params));
889 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
891 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
893 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
894 dmxdevfilter->params.pes.pid);
895 if (ret < 0)
896 return ret;
898 if (params->flags & DMX_IMMEDIATE_START)
899 return dvb_dmxdev_filter_start(dmxdevfilter);
901 return 0;
904 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
905 struct file *file, char __user *buf,
906 size_t count, loff_t *ppos)
908 int result, hcount;
909 int done = 0;
911 if (dfil->todo <= 0) {
912 hcount = 3 + dfil->todo;
913 if (hcount > count)
914 hcount = count;
915 result = dvb_dmxdev_buffer_read(&dfil->buffer,
916 file->f_flags & O_NONBLOCK,
917 buf, hcount, ppos);
918 if (result < 0) {
919 dfil->todo = 0;
920 return result;
922 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
923 return -EFAULT;
924 buf += result;
925 done = result;
926 count -= result;
927 dfil->todo -= result;
928 if (dfil->todo > -3)
929 return done;
930 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
931 if (!count)
932 return done;
934 if (count > dfil->todo)
935 count = dfil->todo;
936 result = dvb_dmxdev_buffer_read(&dfil->buffer,
937 file->f_flags & O_NONBLOCK,
938 buf, count, ppos);
939 if (result < 0)
940 return result;
941 dfil->todo -= result;
942 return (result + done);
945 static ssize_t
946 dvb_demux_read(struct file *file, char __user *buf, size_t count,
947 loff_t *ppos)
949 struct dmxdev_filter *dmxdevfilter = file->private_data;
950 int ret;
952 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
953 return -ERESTARTSYS;
955 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
956 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
957 else
958 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
959 file->f_flags & O_NONBLOCK,
960 buf, count, ppos);
962 mutex_unlock(&dmxdevfilter->mutex);
963 return ret;
966 static int dvb_demux_do_ioctl(struct file *file,
967 unsigned int cmd, void *parg)
969 struct dmxdev_filter *dmxdevfilter = file->private_data;
970 struct dmxdev *dmxdev = dmxdevfilter->dev;
971 unsigned long arg = (unsigned long)parg;
972 int ret = 0;
974 if (mutex_lock_interruptible(&dmxdev->mutex))
975 return -ERESTARTSYS;
977 switch (cmd) {
978 case DMX_START:
979 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
980 mutex_unlock(&dmxdev->mutex);
981 return -ERESTARTSYS;
983 if (dmxdevfilter->state < DMXDEV_STATE_SET)
984 ret = -EINVAL;
985 else
986 ret = dvb_dmxdev_filter_start(dmxdevfilter);
987 mutex_unlock(&dmxdevfilter->mutex);
988 break;
990 case DMX_STOP:
991 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
992 mutex_unlock(&dmxdev->mutex);
993 return -ERESTARTSYS;
995 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
996 mutex_unlock(&dmxdevfilter->mutex);
997 break;
999 case DMX_SET_FILTER:
1000 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1001 mutex_unlock(&dmxdev->mutex);
1002 return -ERESTARTSYS;
1004 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1005 mutex_unlock(&dmxdevfilter->mutex);
1006 break;
1008 case DMX_SET_PES_FILTER:
1009 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1010 mutex_unlock(&dmxdev->mutex);
1011 return -ERESTARTSYS;
1013 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1014 mutex_unlock(&dmxdevfilter->mutex);
1015 break;
1017 case DMX_SET_BUFFER_SIZE:
1018 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1019 mutex_unlock(&dmxdev->mutex);
1020 return -ERESTARTSYS;
1022 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1023 mutex_unlock(&dmxdevfilter->mutex);
1024 break;
1026 case DMX_GET_PES_PIDS:
1027 if (!dmxdev->demux->get_pes_pids) {
1028 ret = -EINVAL;
1029 break;
1031 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1032 break;
1034 case DMX_GET_CAPS:
1035 if (!dmxdev->demux->get_caps) {
1036 ret = -EINVAL;
1037 break;
1039 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
1040 break;
1042 case DMX_SET_SOURCE:
1043 if (!dmxdev->demux->set_source) {
1044 ret = -EINVAL;
1045 break;
1047 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
1048 break;
1050 case DMX_GET_STC:
1051 if (!dmxdev->demux->get_stc) {
1052 ret = -EINVAL;
1053 break;
1055 ret = dmxdev->demux->get_stc(dmxdev->demux,
1056 ((struct dmx_stc *)parg)->num,
1057 &((struct dmx_stc *)parg)->stc,
1058 &((struct dmx_stc *)parg)->base);
1059 break;
1061 case DMX_ADD_PID:
1062 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1063 ret = -ERESTARTSYS;
1064 break;
1066 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1067 mutex_unlock(&dmxdevfilter->mutex);
1068 break;
1070 case DMX_REMOVE_PID:
1071 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1072 ret = -ERESTARTSYS;
1073 break;
1075 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1076 mutex_unlock(&dmxdevfilter->mutex);
1077 break;
1079 default:
1080 ret = -EINVAL;
1081 break;
1083 mutex_unlock(&dmxdev->mutex);
1084 return ret;
1087 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1088 unsigned long arg)
1090 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1093 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
1095 struct dmxdev_filter *dmxdevfilter = file->private_data;
1096 unsigned int mask = 0;
1098 if (!dmxdevfilter)
1099 return -EINVAL;
1101 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1103 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1104 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1105 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1106 return 0;
1108 if (dmxdevfilter->buffer.error)
1109 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1111 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1112 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1114 return mask;
1117 static int dvb_demux_release(struct inode *inode, struct file *file)
1119 struct dmxdev_filter *dmxdevfilter = file->private_data;
1120 struct dmxdev *dmxdev = dmxdevfilter->dev;
1122 int ret;
1124 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1126 mutex_lock(&dmxdev->mutex);
1127 dmxdev->dvbdev->users--;
1128 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1129 fops_put(file->f_op);
1130 file->f_op = NULL;
1131 mutex_unlock(&dmxdev->mutex);
1132 wake_up(&dmxdev->dvbdev->wait_queue);
1133 } else
1134 mutex_unlock(&dmxdev->mutex);
1136 return ret;
1139 static const struct file_operations dvb_demux_fops = {
1140 .owner = THIS_MODULE,
1141 .read = dvb_demux_read,
1142 .unlocked_ioctl = dvb_demux_ioctl,
1143 .open = dvb_demux_open,
1144 .release = dvb_demux_release,
1145 .poll = dvb_demux_poll,
1146 .llseek = default_llseek,
1149 static struct dvb_device dvbdev_demux = {
1150 .priv = NULL,
1151 .users = 1,
1152 .writers = 1,
1153 .fops = &dvb_demux_fops
1156 static int dvb_dvr_do_ioctl(struct file *file,
1157 unsigned int cmd, void *parg)
1159 struct dvb_device *dvbdev = file->private_data;
1160 struct dmxdev *dmxdev = dvbdev->priv;
1161 unsigned long arg = (unsigned long)parg;
1162 int ret;
1164 if (mutex_lock_interruptible(&dmxdev->mutex))
1165 return -ERESTARTSYS;
1167 switch (cmd) {
1168 case DMX_SET_BUFFER_SIZE:
1169 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1170 break;
1172 default:
1173 ret = -EINVAL;
1174 break;
1176 mutex_unlock(&dmxdev->mutex);
1177 return ret;
1180 static long dvb_dvr_ioctl(struct file *file,
1181 unsigned int cmd, unsigned long arg)
1183 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1186 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1188 struct dvb_device *dvbdev = file->private_data;
1189 struct dmxdev *dmxdev = dvbdev->priv;
1190 unsigned int mask = 0;
1192 dprintk("function : %s\n", __func__);
1194 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1196 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1197 if (dmxdev->dvr_buffer.error)
1198 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1200 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1201 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1202 } else
1203 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1205 return mask;
1208 static const struct file_operations dvb_dvr_fops = {
1209 .owner = THIS_MODULE,
1210 .read = dvb_dvr_read,
1211 .write = dvb_dvr_write,
1212 .unlocked_ioctl = dvb_dvr_ioctl,
1213 .open = dvb_dvr_open,
1214 .release = dvb_dvr_release,
1215 .poll = dvb_dvr_poll,
1216 .llseek = default_llseek,
1219 static struct dvb_device dvbdev_dvr = {
1220 .priv = NULL,
1221 .readers = 1,
1222 .users = 1,
1223 .fops = &dvb_dvr_fops
1226 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1228 int i;
1230 if (dmxdev->demux->open(dmxdev->demux) < 0)
1231 return -EUSERS;
1233 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1234 if (!dmxdev->filter)
1235 return -ENOMEM;
1237 mutex_init(&dmxdev->mutex);
1238 spin_lock_init(&dmxdev->lock);
1239 for (i = 0; i < dmxdev->filternum; i++) {
1240 dmxdev->filter[i].dev = dmxdev;
1241 dmxdev->filter[i].buffer.data = NULL;
1242 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1243 DMXDEV_STATE_FREE);
1246 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1247 DVB_DEVICE_DEMUX);
1248 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1249 dmxdev, DVB_DEVICE_DVR);
1251 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1253 return 0;
1256 EXPORT_SYMBOL(dvb_dmxdev_init);
1258 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1260 dmxdev->exit=1;
1261 if (dmxdev->dvbdev->users > 1) {
1262 wait_event(dmxdev->dvbdev->wait_queue,
1263 dmxdev->dvbdev->users==1);
1265 if (dmxdev->dvr_dvbdev->users > 1) {
1266 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1267 dmxdev->dvr_dvbdev->users==1);
1270 dvb_unregister_device(dmxdev->dvbdev);
1271 dvb_unregister_device(dmxdev->dvr_dvbdev);
1273 vfree(dmxdev->filter);
1274 dmxdev->filter = NULL;
1275 dmxdev->demux->close(dmxdev->demux);
1278 EXPORT_SYMBOL(dvb_dmxdev_release);