sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / media / dvb-core / dmxdev.c
blob0c16bb21310109050f461979306408f7ca42866b
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 #define pr_fmt(fmt) "dmxdev: " fmt
25 #include <linux/sched.h>
26 #include <linux/spinlock.h>
27 #include <linux/slab.h>
28 #include <linux/vmalloc.h>
29 #include <linux/module.h>
30 #include <linux/poll.h>
31 #include <linux/ioctl.h>
32 #include <linux/wait.h>
33 #include <linux/uaccess.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(fmt, arg...) do { \
42 if (debug) \
43 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
44 __func__, ##arg); \
45 } while (0)
47 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
48 const u8 *src, size_t len)
50 ssize_t free;
52 if (!len)
53 return 0;
54 if (!buf->data)
55 return 0;
57 free = dvb_ringbuffer_free(buf);
58 if (len > free) {
59 dprintk("buffer overflow\n");
60 return -EOVERFLOW;
63 return dvb_ringbuffer_write(buf, src, len);
66 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
67 int non_blocking, char __user *buf,
68 size_t count, loff_t *ppos)
70 size_t todo;
71 ssize_t avail;
72 ssize_t ret = 0;
74 if (!src->data)
75 return 0;
77 if (src->error) {
78 ret = src->error;
79 dvb_ringbuffer_flush(src);
80 return ret;
83 for (todo = count; todo > 0; todo -= ret) {
84 if (non_blocking && dvb_ringbuffer_empty(src)) {
85 ret = -EWOULDBLOCK;
86 break;
89 ret = wait_event_interruptible(src->queue,
90 !dvb_ringbuffer_empty(src) ||
91 (src->error != 0));
92 if (ret < 0)
93 break;
95 if (src->error) {
96 ret = src->error;
97 dvb_ringbuffer_flush(src);
98 break;
101 avail = dvb_ringbuffer_avail(src);
102 if (avail > todo)
103 avail = todo;
105 ret = dvb_ringbuffer_read_user(src, buf, avail);
106 if (ret < 0)
107 break;
109 buf += ret;
112 return (count - todo) ? (count - todo) : ret;
115 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
117 struct list_head *head, *pos;
119 head = demux->get_frontends(demux);
120 if (!head)
121 return NULL;
122 list_for_each(pos, head)
123 if (DMX_FE_ENTRY(pos)->source == type)
124 return DMX_FE_ENTRY(pos);
126 return NULL;
129 static int dvb_dvr_open(struct inode *inode, struct file *file)
131 struct dvb_device *dvbdev = file->private_data;
132 struct dmxdev *dmxdev = dvbdev->priv;
133 struct dmx_frontend *front;
135 dprintk("%s\n", __func__);
137 if (mutex_lock_interruptible(&dmxdev->mutex))
138 return -ERESTARTSYS;
140 if (dmxdev->exit) {
141 mutex_unlock(&dmxdev->mutex);
142 return -ENODEV;
145 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
146 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
147 mutex_unlock(&dmxdev->mutex);
148 return -EOPNOTSUPP;
152 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
153 void *mem;
154 if (!dvbdev->readers) {
155 mutex_unlock(&dmxdev->mutex);
156 return -EBUSY;
158 mem = vmalloc(DVR_BUFFER_SIZE);
159 if (!mem) {
160 mutex_unlock(&dmxdev->mutex);
161 return -ENOMEM;
163 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
164 dvbdev->readers--;
167 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
168 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
170 if (!dmxdev->demux->write) {
171 mutex_unlock(&dmxdev->mutex);
172 return -EOPNOTSUPP;
175 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
177 if (!front) {
178 mutex_unlock(&dmxdev->mutex);
179 return -EINVAL;
181 dmxdev->demux->disconnect_frontend(dmxdev->demux);
182 dmxdev->demux->connect_frontend(dmxdev->demux, front);
184 dvbdev->users++;
185 mutex_unlock(&dmxdev->mutex);
186 return 0;
189 static int dvb_dvr_release(struct inode *inode, struct file *file)
191 struct dvb_device *dvbdev = file->private_data;
192 struct dmxdev *dmxdev = dvbdev->priv;
194 mutex_lock(&dmxdev->mutex);
196 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
197 dmxdev->demux->disconnect_frontend(dmxdev->demux);
198 dmxdev->demux->connect_frontend(dmxdev->demux,
199 dmxdev->dvr_orig_fe);
201 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
202 dvbdev->readers++;
203 if (dmxdev->dvr_buffer.data) {
204 void *mem = dmxdev->dvr_buffer.data;
205 mb();
206 spin_lock_irq(&dmxdev->lock);
207 dmxdev->dvr_buffer.data = NULL;
208 spin_unlock_irq(&dmxdev->lock);
209 vfree(mem);
212 /* TODO */
213 dvbdev->users--;
214 if (dvbdev->users == 1 && dmxdev->exit == 1) {
215 mutex_unlock(&dmxdev->mutex);
216 wake_up(&dvbdev->wait_queue);
217 } else
218 mutex_unlock(&dmxdev->mutex);
220 return 0;
223 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
224 size_t count, loff_t *ppos)
226 struct dvb_device *dvbdev = file->private_data;
227 struct dmxdev *dmxdev = dvbdev->priv;
228 int ret;
230 if (!dmxdev->demux->write)
231 return -EOPNOTSUPP;
232 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
233 return -EINVAL;
234 if (mutex_lock_interruptible(&dmxdev->mutex))
235 return -ERESTARTSYS;
237 if (dmxdev->exit) {
238 mutex_unlock(&dmxdev->mutex);
239 return -ENODEV;
241 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
242 mutex_unlock(&dmxdev->mutex);
243 return ret;
246 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
247 loff_t *ppos)
249 struct dvb_device *dvbdev = file->private_data;
250 struct dmxdev *dmxdev = dvbdev->priv;
252 if (dmxdev->exit)
253 return -ENODEV;
255 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
256 file->f_flags & O_NONBLOCK,
257 buf, count, ppos);
260 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
261 unsigned long size)
263 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
264 void *newmem;
265 void *oldmem;
267 dprintk("%s\n", __func__);
269 if (buf->size == size)
270 return 0;
271 if (!size)
272 return -EINVAL;
274 newmem = vmalloc(size);
275 if (!newmem)
276 return -ENOMEM;
278 oldmem = buf->data;
280 spin_lock_irq(&dmxdev->lock);
281 buf->data = newmem;
282 buf->size = size;
284 /* reset and not flush in case the buffer shrinks */
285 dvb_ringbuffer_reset(buf);
286 spin_unlock_irq(&dmxdev->lock);
288 vfree(oldmem);
290 return 0;
293 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
294 *dmxdevfilter, int state)
296 spin_lock_irq(&dmxdevfilter->dev->lock);
297 dmxdevfilter->state = state;
298 spin_unlock_irq(&dmxdevfilter->dev->lock);
301 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
302 unsigned long size)
304 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
305 void *newmem;
306 void *oldmem;
308 if (buf->size == size)
309 return 0;
310 if (!size)
311 return -EINVAL;
312 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
313 return -EBUSY;
315 newmem = vmalloc(size);
316 if (!newmem)
317 return -ENOMEM;
319 oldmem = buf->data;
321 spin_lock_irq(&dmxdevfilter->dev->lock);
322 buf->data = newmem;
323 buf->size = size;
325 /* reset and not flush in case the buffer shrinks */
326 dvb_ringbuffer_reset(buf);
327 spin_unlock_irq(&dmxdevfilter->dev->lock);
329 vfree(oldmem);
331 return 0;
334 static void dvb_dmxdev_filter_timeout(unsigned long data)
336 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
338 dmxdevfilter->buffer.error = -ETIMEDOUT;
339 spin_lock_irq(&dmxdevfilter->dev->lock);
340 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
341 spin_unlock_irq(&dmxdevfilter->dev->lock);
342 wake_up(&dmxdevfilter->buffer.queue);
345 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
347 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
349 del_timer(&dmxdevfilter->timer);
350 if (para->timeout) {
351 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
352 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
353 dmxdevfilter->timer.expires =
354 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
355 add_timer(&dmxdevfilter->timer);
359 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
360 const u8 *buffer2, size_t buffer2_len,
361 struct dmx_section_filter *filter)
363 struct dmxdev_filter *dmxdevfilter = filter->priv;
364 int ret;
366 if (dmxdevfilter->buffer.error) {
367 wake_up(&dmxdevfilter->buffer.queue);
368 return 0;
370 spin_lock(&dmxdevfilter->dev->lock);
371 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
372 spin_unlock(&dmxdevfilter->dev->lock);
373 return 0;
375 del_timer(&dmxdevfilter->timer);
376 dprintk("section callback %*ph\n", 6, buffer1);
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 dmxdevfilter->buffer.error = ret;
385 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
386 dmxdevfilter->state = DMXDEV_STATE_DONE;
387 spin_unlock(&dmxdevfilter->dev->lock);
388 wake_up(&dmxdevfilter->buffer.queue);
389 return 0;
392 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
393 const u8 *buffer2, size_t buffer2_len,
394 struct dmx_ts_feed *feed)
396 struct dmxdev_filter *dmxdevfilter = feed->priv;
397 struct dvb_ringbuffer *buffer;
398 int ret;
400 spin_lock(&dmxdevfilter->dev->lock);
401 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
402 spin_unlock(&dmxdevfilter->dev->lock);
403 return 0;
406 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
407 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
408 buffer = &dmxdevfilter->buffer;
409 else
410 buffer = &dmxdevfilter->dev->dvr_buffer;
411 if (buffer->error) {
412 spin_unlock(&dmxdevfilter->dev->lock);
413 wake_up(&buffer->queue);
414 return 0;
416 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
417 if (ret == buffer1_len)
418 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
419 if (ret < 0)
420 buffer->error = ret;
421 spin_unlock(&dmxdevfilter->dev->lock);
422 wake_up(&buffer->queue);
423 return 0;
426 /* stop feed but only mark the specified filter as stopped (state set) */
427 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
429 struct dmxdev_feed *feed;
431 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
433 switch (dmxdevfilter->type) {
434 case DMXDEV_TYPE_SEC:
435 del_timer(&dmxdevfilter->timer);
436 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
437 break;
438 case DMXDEV_TYPE_PES:
439 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
440 feed->ts->stop_filtering(feed->ts);
441 break;
442 default:
443 return -EINVAL;
445 return 0;
448 /* start feed associated with the specified filter */
449 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
451 struct dmxdev_feed *feed;
452 int ret;
454 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
456 switch (filter->type) {
457 case DMXDEV_TYPE_SEC:
458 return filter->feed.sec->start_filtering(filter->feed.sec);
459 case DMXDEV_TYPE_PES:
460 list_for_each_entry(feed, &filter->feed.ts, next) {
461 ret = feed->ts->start_filtering(feed->ts);
462 if (ret < 0) {
463 dvb_dmxdev_feed_stop(filter);
464 return ret;
467 break;
468 default:
469 return -EINVAL;
472 return 0;
475 /* restart section feed if it has filters left associated with it,
476 otherwise release the feed */
477 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
479 int i;
480 struct dmxdev *dmxdev = filter->dev;
481 u16 pid = filter->params.sec.pid;
483 for (i = 0; i < dmxdev->filternum; i++)
484 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
485 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
486 dmxdev->filter[i].params.sec.pid == pid) {
487 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
488 return 0;
491 filter->dev->demux->release_section_feed(dmxdev->demux,
492 filter->feed.sec);
494 return 0;
497 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
499 struct dmxdev_feed *feed;
500 struct dmx_demux *demux;
502 if (dmxdevfilter->state < DMXDEV_STATE_GO)
503 return 0;
505 switch (dmxdevfilter->type) {
506 case DMXDEV_TYPE_SEC:
507 if (!dmxdevfilter->feed.sec)
508 break;
509 dvb_dmxdev_feed_stop(dmxdevfilter);
510 if (dmxdevfilter->filter.sec)
511 dmxdevfilter->feed.sec->
512 release_filter(dmxdevfilter->feed.sec,
513 dmxdevfilter->filter.sec);
514 dvb_dmxdev_feed_restart(dmxdevfilter);
515 dmxdevfilter->feed.sec = NULL;
516 break;
517 case DMXDEV_TYPE_PES:
518 dvb_dmxdev_feed_stop(dmxdevfilter);
519 demux = dmxdevfilter->dev->demux;
520 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
521 demux->release_ts_feed(demux, feed->ts);
522 feed->ts = NULL;
524 break;
525 default:
526 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
527 return 0;
528 return -EINVAL;
531 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
532 return 0;
535 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
537 struct dmxdev_feed *feed, *tmp;
539 /* delete all PIDs */
540 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
541 list_del(&feed->next);
542 kfree(feed);
545 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
548 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
550 if (dmxdevfilter->state < DMXDEV_STATE_SET)
551 return 0;
553 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
554 dvb_dmxdev_delete_pids(dmxdevfilter);
556 dmxdevfilter->type = DMXDEV_TYPE_NONE;
557 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
558 return 0;
561 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
562 struct dmxdev_filter *filter,
563 struct dmxdev_feed *feed)
565 ktime_t timeout = 0;
566 struct dmx_pes_filter_params *para = &filter->params.pes;
567 dmx_output_t otype;
568 int ret;
569 int ts_type;
570 enum dmx_ts_pes ts_pes;
571 struct dmx_ts_feed *tsfeed;
573 feed->ts = NULL;
574 otype = para->output;
576 ts_pes = para->pes_type;
578 if (ts_pes < DMX_PES_OTHER)
579 ts_type = TS_DECODER;
580 else
581 ts_type = 0;
583 if (otype == DMX_OUT_TS_TAP)
584 ts_type |= TS_PACKET;
585 else if (otype == DMX_OUT_TSDEMUX_TAP)
586 ts_type |= TS_PACKET | TS_DEMUX;
587 else if (otype == DMX_OUT_TAP)
588 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
590 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
591 dvb_dmxdev_ts_callback);
592 if (ret < 0)
593 return ret;
595 tsfeed = feed->ts;
596 tsfeed->priv = filter;
598 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, timeout);
599 if (ret < 0) {
600 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
601 return ret;
604 ret = tsfeed->start_filtering(tsfeed);
605 if (ret < 0) {
606 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
607 return ret;
610 return 0;
613 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
615 struct dmxdev *dmxdev = filter->dev;
616 struct dmxdev_feed *feed;
617 void *mem;
618 int ret, i;
620 if (filter->state < DMXDEV_STATE_SET)
621 return -EINVAL;
623 if (filter->state >= DMXDEV_STATE_GO)
624 dvb_dmxdev_filter_stop(filter);
626 if (!filter->buffer.data) {
627 mem = vmalloc(filter->buffer.size);
628 if (!mem)
629 return -ENOMEM;
630 spin_lock_irq(&filter->dev->lock);
631 filter->buffer.data = mem;
632 spin_unlock_irq(&filter->dev->lock);
635 dvb_ringbuffer_flush(&filter->buffer);
637 switch (filter->type) {
638 case DMXDEV_TYPE_SEC:
640 struct dmx_sct_filter_params *para = &filter->params.sec;
641 struct dmx_section_filter **secfilter = &filter->filter.sec;
642 struct dmx_section_feed **secfeed = &filter->feed.sec;
644 *secfilter = NULL;
645 *secfeed = NULL;
648 /* find active filter/feed with same PID */
649 for (i = 0; i < dmxdev->filternum; i++) {
650 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
651 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
652 dmxdev->filter[i].params.sec.pid == para->pid) {
653 *secfeed = dmxdev->filter[i].feed.sec;
654 break;
658 /* if no feed found, try to allocate new one */
659 if (!*secfeed) {
660 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
661 secfeed,
662 dvb_dmxdev_section_callback);
663 if (ret < 0) {
664 pr_err("DVB (%s): could not alloc feed\n",
665 __func__);
666 return ret;
669 ret = (*secfeed)->set(*secfeed, para->pid,
670 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
671 if (ret < 0) {
672 pr_err("DVB (%s): could not set feed\n",
673 __func__);
674 dvb_dmxdev_feed_restart(filter);
675 return ret;
677 } else {
678 dvb_dmxdev_feed_stop(filter);
681 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
682 if (ret < 0) {
683 dvb_dmxdev_feed_restart(filter);
684 filter->feed.sec->start_filtering(*secfeed);
685 dprintk("could not get filter\n");
686 return ret;
689 (*secfilter)->priv = filter;
691 memcpy(&((*secfilter)->filter_value[3]),
692 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
693 memcpy(&(*secfilter)->filter_mask[3],
694 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
695 memcpy(&(*secfilter)->filter_mode[3],
696 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
698 (*secfilter)->filter_value[0] = para->filter.filter[0];
699 (*secfilter)->filter_mask[0] = para->filter.mask[0];
700 (*secfilter)->filter_mode[0] = para->filter.mode[0];
701 (*secfilter)->filter_mask[1] = 0;
702 (*secfilter)->filter_mask[2] = 0;
704 filter->todo = 0;
706 ret = filter->feed.sec->start_filtering(filter->feed.sec);
707 if (ret < 0)
708 return ret;
710 dvb_dmxdev_filter_timer(filter);
711 break;
713 case DMXDEV_TYPE_PES:
714 list_for_each_entry(feed, &filter->feed.ts, next) {
715 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
716 if (ret < 0) {
717 dvb_dmxdev_filter_stop(filter);
718 return ret;
721 break;
722 default:
723 return -EINVAL;
726 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
727 return 0;
730 static int dvb_demux_open(struct inode *inode, struct file *file)
732 struct dvb_device *dvbdev = file->private_data;
733 struct dmxdev *dmxdev = dvbdev->priv;
734 int i;
735 struct dmxdev_filter *dmxdevfilter;
737 if (!dmxdev->filter)
738 return -EINVAL;
740 if (mutex_lock_interruptible(&dmxdev->mutex))
741 return -ERESTARTSYS;
743 for (i = 0; i < dmxdev->filternum; i++)
744 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
745 break;
747 if (i == dmxdev->filternum) {
748 mutex_unlock(&dmxdev->mutex);
749 return -EMFILE;
752 dmxdevfilter = &dmxdev->filter[i];
753 mutex_init(&dmxdevfilter->mutex);
754 file->private_data = dmxdevfilter;
756 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
757 dmxdevfilter->type = DMXDEV_TYPE_NONE;
758 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
759 init_timer(&dmxdevfilter->timer);
761 dvbdev->users++;
763 mutex_unlock(&dmxdev->mutex);
764 return 0;
767 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
768 struct dmxdev_filter *dmxdevfilter)
770 mutex_lock(&dmxdev->mutex);
771 mutex_lock(&dmxdevfilter->mutex);
773 dvb_dmxdev_filter_stop(dmxdevfilter);
774 dvb_dmxdev_filter_reset(dmxdevfilter);
776 if (dmxdevfilter->buffer.data) {
777 void *mem = dmxdevfilter->buffer.data;
779 spin_lock_irq(&dmxdev->lock);
780 dmxdevfilter->buffer.data = NULL;
781 spin_unlock_irq(&dmxdev->lock);
782 vfree(mem);
785 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
786 wake_up(&dmxdevfilter->buffer.queue);
787 mutex_unlock(&dmxdevfilter->mutex);
788 mutex_unlock(&dmxdev->mutex);
789 return 0;
792 static inline void invert_mode(dmx_filter_t *filter)
794 int i;
796 for (i = 0; i < DMX_FILTER_SIZE; i++)
797 filter->mode[i] ^= 0xff;
800 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
801 struct dmxdev_filter *filter, u16 pid)
803 struct dmxdev_feed *feed;
805 if ((filter->type != DMXDEV_TYPE_PES) ||
806 (filter->state < DMXDEV_STATE_SET))
807 return -EINVAL;
809 /* only TS packet filters may have multiple PIDs */
810 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
811 (!list_empty(&filter->feed.ts)))
812 return -EINVAL;
814 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
815 if (feed == NULL)
816 return -ENOMEM;
818 feed->pid = pid;
819 list_add(&feed->next, &filter->feed.ts);
821 if (filter->state >= DMXDEV_STATE_GO)
822 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
824 return 0;
827 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
828 struct dmxdev_filter *filter, u16 pid)
830 struct dmxdev_feed *feed, *tmp;
832 if ((filter->type != DMXDEV_TYPE_PES) ||
833 (filter->state < DMXDEV_STATE_SET))
834 return -EINVAL;
836 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
837 if ((feed->pid == pid) && (feed->ts != NULL)) {
838 feed->ts->stop_filtering(feed->ts);
839 filter->dev->demux->release_ts_feed(filter->dev->demux,
840 feed->ts);
841 list_del(&feed->next);
842 kfree(feed);
846 return 0;
849 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
850 struct dmxdev_filter *dmxdevfilter,
851 struct dmx_sct_filter_params *params)
853 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
854 __func__, params->pid, params->flags, params->timeout);
856 dvb_dmxdev_filter_stop(dmxdevfilter);
858 dmxdevfilter->type = DMXDEV_TYPE_SEC;
859 memcpy(&dmxdevfilter->params.sec,
860 params, sizeof(struct dmx_sct_filter_params));
861 invert_mode(&dmxdevfilter->params.sec.filter);
862 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
864 if (params->flags & DMX_IMMEDIATE_START)
865 return dvb_dmxdev_filter_start(dmxdevfilter);
867 return 0;
870 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
871 struct dmxdev_filter *dmxdevfilter,
872 struct dmx_pes_filter_params *params)
874 int ret;
876 dvb_dmxdev_filter_stop(dmxdevfilter);
877 dvb_dmxdev_filter_reset(dmxdevfilter);
879 if ((unsigned)params->pes_type > DMX_PES_OTHER)
880 return -EINVAL;
882 dmxdevfilter->type = DMXDEV_TYPE_PES;
883 memcpy(&dmxdevfilter->params, params,
884 sizeof(struct dmx_pes_filter_params));
885 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
887 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
889 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
890 dmxdevfilter->params.pes.pid);
891 if (ret < 0)
892 return ret;
894 if (params->flags & DMX_IMMEDIATE_START)
895 return dvb_dmxdev_filter_start(dmxdevfilter);
897 return 0;
900 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
901 struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
904 int result, hcount;
905 int done = 0;
907 if (dfil->todo <= 0) {
908 hcount = 3 + dfil->todo;
909 if (hcount > count)
910 hcount = count;
911 result = dvb_dmxdev_buffer_read(&dfil->buffer,
912 file->f_flags & O_NONBLOCK,
913 buf, hcount, ppos);
914 if (result < 0) {
915 dfil->todo = 0;
916 return result;
918 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
919 return -EFAULT;
920 buf += result;
921 done = result;
922 count -= result;
923 dfil->todo -= result;
924 if (dfil->todo > -3)
925 return done;
926 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
927 if (!count)
928 return done;
930 if (count > dfil->todo)
931 count = dfil->todo;
932 result = dvb_dmxdev_buffer_read(&dfil->buffer,
933 file->f_flags & O_NONBLOCK,
934 buf, count, ppos);
935 if (result < 0)
936 return result;
937 dfil->todo -= result;
938 return (result + done);
941 static ssize_t
942 dvb_demux_read(struct file *file, char __user *buf, size_t count,
943 loff_t *ppos)
945 struct dmxdev_filter *dmxdevfilter = file->private_data;
946 int ret;
948 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
949 return -ERESTARTSYS;
951 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
952 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
953 else
954 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
955 file->f_flags & O_NONBLOCK,
956 buf, count, ppos);
958 mutex_unlock(&dmxdevfilter->mutex);
959 return ret;
962 static int dvb_demux_do_ioctl(struct file *file,
963 unsigned int cmd, void *parg)
965 struct dmxdev_filter *dmxdevfilter = file->private_data;
966 struct dmxdev *dmxdev = dmxdevfilter->dev;
967 unsigned long arg = (unsigned long)parg;
968 int ret = 0;
970 if (mutex_lock_interruptible(&dmxdev->mutex))
971 return -ERESTARTSYS;
973 switch (cmd) {
974 case DMX_START:
975 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
976 mutex_unlock(&dmxdev->mutex);
977 return -ERESTARTSYS;
979 if (dmxdevfilter->state < DMXDEV_STATE_SET)
980 ret = -EINVAL;
981 else
982 ret = dvb_dmxdev_filter_start(dmxdevfilter);
983 mutex_unlock(&dmxdevfilter->mutex);
984 break;
986 case DMX_STOP:
987 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
988 mutex_unlock(&dmxdev->mutex);
989 return -ERESTARTSYS;
991 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
992 mutex_unlock(&dmxdevfilter->mutex);
993 break;
995 case DMX_SET_FILTER:
996 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
997 mutex_unlock(&dmxdev->mutex);
998 return -ERESTARTSYS;
1000 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1001 mutex_unlock(&dmxdevfilter->mutex);
1002 break;
1004 case DMX_SET_PES_FILTER:
1005 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1006 mutex_unlock(&dmxdev->mutex);
1007 return -ERESTARTSYS;
1009 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1010 mutex_unlock(&dmxdevfilter->mutex);
1011 break;
1013 case DMX_SET_BUFFER_SIZE:
1014 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1015 mutex_unlock(&dmxdev->mutex);
1016 return -ERESTARTSYS;
1018 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1019 mutex_unlock(&dmxdevfilter->mutex);
1020 break;
1022 case DMX_GET_PES_PIDS:
1023 if (!dmxdev->demux->get_pes_pids) {
1024 ret = -EINVAL;
1025 break;
1027 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1028 break;
1030 #if 0
1031 /* Not used upstream and never documented */
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;
1048 #endif
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) || dmxdevfilter->dev->exit)
1099 return POLLERR;
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 mutex_unlock(&dmxdev->mutex);
1130 wake_up(&dmxdev->dvbdev->wait_queue);
1131 } else
1132 mutex_unlock(&dmxdev->mutex);
1134 return ret;
1137 static const struct file_operations dvb_demux_fops = {
1138 .owner = THIS_MODULE,
1139 .read = dvb_demux_read,
1140 .unlocked_ioctl = dvb_demux_ioctl,
1141 .open = dvb_demux_open,
1142 .release = dvb_demux_release,
1143 .poll = dvb_demux_poll,
1144 .llseek = default_llseek,
1147 static const struct dvb_device dvbdev_demux = {
1148 .priv = NULL,
1149 .users = 1,
1150 .writers = 1,
1151 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1152 .name = "dvb-demux",
1153 #endif
1154 .fops = &dvb_demux_fops
1157 static int dvb_dvr_do_ioctl(struct file *file,
1158 unsigned int cmd, void *parg)
1160 struct dvb_device *dvbdev = file->private_data;
1161 struct dmxdev *dmxdev = dvbdev->priv;
1162 unsigned long arg = (unsigned long)parg;
1163 int ret;
1165 if (mutex_lock_interruptible(&dmxdev->mutex))
1166 return -ERESTARTSYS;
1168 switch (cmd) {
1169 case DMX_SET_BUFFER_SIZE:
1170 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1171 break;
1173 default:
1174 ret = -EINVAL;
1175 break;
1177 mutex_unlock(&dmxdev->mutex);
1178 return ret;
1181 static long dvb_dvr_ioctl(struct file *file,
1182 unsigned int cmd, unsigned long arg)
1184 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1187 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1189 struct dvb_device *dvbdev = file->private_data;
1190 struct dmxdev *dmxdev = dvbdev->priv;
1191 unsigned int mask = 0;
1193 dprintk("%s\n", __func__);
1195 if (dmxdev->exit)
1196 return POLLERR;
1198 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1200 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1201 if (dmxdev->dvr_buffer.error)
1202 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1204 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1205 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1206 } else
1207 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1209 return mask;
1212 static const struct file_operations dvb_dvr_fops = {
1213 .owner = THIS_MODULE,
1214 .read = dvb_dvr_read,
1215 .write = dvb_dvr_write,
1216 .unlocked_ioctl = dvb_dvr_ioctl,
1217 .open = dvb_dvr_open,
1218 .release = dvb_dvr_release,
1219 .poll = dvb_dvr_poll,
1220 .llseek = default_llseek,
1223 static const struct dvb_device dvbdev_dvr = {
1224 .priv = NULL,
1225 .readers = 1,
1226 .users = 1,
1227 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1228 .name = "dvb-dvr",
1229 #endif
1230 .fops = &dvb_dvr_fops
1232 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1234 int i;
1236 if (dmxdev->demux->open(dmxdev->demux) < 0)
1237 return -EUSERS;
1239 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1240 if (!dmxdev->filter)
1241 return -ENOMEM;
1243 mutex_init(&dmxdev->mutex);
1244 spin_lock_init(&dmxdev->lock);
1245 for (i = 0; i < dmxdev->filternum; i++) {
1246 dmxdev->filter[i].dev = dmxdev;
1247 dmxdev->filter[i].buffer.data = NULL;
1248 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1249 DMXDEV_STATE_FREE);
1252 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1253 DVB_DEVICE_DEMUX, dmxdev->filternum);
1254 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1255 dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
1257 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1259 return 0;
1262 EXPORT_SYMBOL(dvb_dmxdev_init);
1264 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1266 dmxdev->exit=1;
1267 if (dmxdev->dvbdev->users > 1) {
1268 wait_event(dmxdev->dvbdev->wait_queue,
1269 dmxdev->dvbdev->users==1);
1271 if (dmxdev->dvr_dvbdev->users > 1) {
1272 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1273 dmxdev->dvr_dvbdev->users==1);
1276 dvb_unregister_device(dmxdev->dvbdev);
1277 dvb_unregister_device(dmxdev->dvr_dvbdev);
1279 vfree(dmxdev->filter);
1280 dmxdev->filter = NULL;
1281 dmxdev->demux->close(dmxdev->demux);
1284 EXPORT_SYMBOL(dvb_dmxdev_release);