PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / media / dvb / dvb-core / dmxdev.c
blobdf5bef6a2517821c013c1aedbcae843ff7b278fa
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/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
28 #include <linux/ioctl.h>
29 #include <linux/wait.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
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(src, (u8 *)buf, avail, 1);
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;
247 int ret;
249 if (dmxdev->exit) {
250 mutex_unlock(&dmxdev->mutex);
251 return -ENODEV;
254 //mutex_lock(&dmxdev->mutex);
255 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
256 file->f_flags & O_NONBLOCK,
257 buf, count, ppos);
258 //mutex_unlock(&dmxdev->mutex);
259 return ret;
262 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
263 unsigned long size)
265 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
266 void *newmem;
267 void *oldmem;
269 dprintk("function : %s\n", __func__);
271 if (buf->size == size)
272 return 0;
273 if (!size)
274 return -EINVAL;
276 newmem = vmalloc(size);
277 if (!newmem)
278 return -ENOMEM;
280 oldmem = buf->data;
282 spin_lock_irq(&dmxdev->lock);
283 buf->data = newmem;
284 buf->size = size;
286 /* reset and not flush in case the buffer shrinks */
287 dvb_ringbuffer_reset(buf);
288 spin_unlock_irq(&dmxdev->lock);
290 vfree(oldmem);
292 return 0;
295 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
296 *dmxdevfilter, int state)
298 spin_lock_irq(&dmxdevfilter->dev->lock);
299 dmxdevfilter->state = state;
300 spin_unlock_irq(&dmxdevfilter->dev->lock);
303 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
304 unsigned long size)
306 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
307 void *newmem;
308 void *oldmem;
310 if (buf->size == size)
311 return 0;
312 if (!size)
313 return -EINVAL;
314 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
315 return -EBUSY;
317 newmem = vmalloc(size);
318 if (!newmem)
319 return -ENOMEM;
321 oldmem = buf->data;
323 spin_lock_irq(&dmxdevfilter->dev->lock);
324 buf->data = newmem;
325 buf->size = size;
327 /* reset and not flush in case the buffer shrinks */
328 dvb_ringbuffer_reset(buf);
329 spin_unlock_irq(&dmxdevfilter->dev->lock);
331 vfree(oldmem);
333 return 0;
336 static void dvb_dmxdev_filter_timeout(unsigned long data)
338 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
340 dmxdevfilter->buffer.error = -ETIMEDOUT;
341 spin_lock_irq(&dmxdevfilter->dev->lock);
342 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
343 spin_unlock_irq(&dmxdevfilter->dev->lock);
344 wake_up(&dmxdevfilter->buffer.queue);
347 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
349 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
351 del_timer(&dmxdevfilter->timer);
352 if (para->timeout) {
353 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
354 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
355 dmxdevfilter->timer.expires =
356 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
357 add_timer(&dmxdevfilter->timer);
361 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
362 const u8 *buffer2, size_t buffer2_len,
363 struct dmx_section_filter *filter,
364 enum dmx_success success)
366 struct dmxdev_filter *dmxdevfilter = filter->priv;
367 int ret;
369 if (dmxdevfilter->buffer.error) {
370 wake_up(&dmxdevfilter->buffer.queue);
371 return 0;
373 spin_lock(&dmxdevfilter->dev->lock);
374 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
375 spin_unlock(&dmxdevfilter->dev->lock);
376 return 0;
378 del_timer(&dmxdevfilter->timer);
379 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
380 buffer1[0], buffer1[1],
381 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
382 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
383 buffer1_len);
384 if (ret == buffer1_len) {
385 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
386 buffer2_len);
388 if (ret < 0) {
389 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
390 dmxdevfilter->buffer.error = ret;
392 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
393 dmxdevfilter->state = DMXDEV_STATE_DONE;
394 spin_unlock(&dmxdevfilter->dev->lock);
395 wake_up(&dmxdevfilter->buffer.queue);
396 return 0;
399 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
400 const u8 *buffer2, size_t buffer2_len,
401 struct dmx_ts_feed *feed,
402 enum dmx_success success)
404 struct dmxdev_filter *dmxdevfilter = feed->priv;
405 struct dvb_ringbuffer *buffer;
406 int ret;
408 spin_lock(&dmxdevfilter->dev->lock);
409 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
410 spin_unlock(&dmxdevfilter->dev->lock);
411 return 0;
414 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
415 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
416 buffer = &dmxdevfilter->buffer;
417 else
418 buffer = &dmxdevfilter->dev->dvr_buffer;
419 if (buffer->error) {
420 spin_unlock(&dmxdevfilter->dev->lock);
421 wake_up(&buffer->queue);
422 return 0;
424 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
425 if (ret == buffer1_len)
426 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
427 if (ret < 0) {
428 dvb_ringbuffer_flush(buffer);
429 buffer->error = ret;
431 spin_unlock(&dmxdevfilter->dev->lock);
432 wake_up(&buffer->queue);
433 return 0;
436 /* stop feed but only mark the specified filter as stopped (state set) */
437 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
439 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
441 switch (dmxdevfilter->type) {
442 case DMXDEV_TYPE_SEC:
443 del_timer(&dmxdevfilter->timer);
444 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
445 break;
446 case DMXDEV_TYPE_PES:
447 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
448 break;
449 default:
450 return -EINVAL;
452 return 0;
455 /* start feed associated with the specified filter */
456 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
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 return filter->feed.ts->start_filtering(filter->feed.ts);
465 default:
466 return -EINVAL;
469 return 0;
472 /* restart section feed if it has filters left associated with it,
473 otherwise release the feed */
474 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
476 int i;
477 struct dmxdev *dmxdev = filter->dev;
478 u16 pid = filter->params.sec.pid;
480 for (i = 0; i < dmxdev->filternum; i++)
481 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
482 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
483 dmxdev->filter[i].params.sec.pid == pid) {
484 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
485 return 0;
488 filter->dev->demux->release_section_feed(dmxdev->demux,
489 filter->feed.sec);
491 return 0;
494 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
496 if (dmxdevfilter->state < DMXDEV_STATE_GO)
497 return 0;
499 switch (dmxdevfilter->type) {
500 case DMXDEV_TYPE_SEC:
501 if (!dmxdevfilter->feed.sec)
502 break;
503 dvb_dmxdev_feed_stop(dmxdevfilter);
504 if (dmxdevfilter->filter.sec)
505 dmxdevfilter->feed.sec->
506 release_filter(dmxdevfilter->feed.sec,
507 dmxdevfilter->filter.sec);
508 dvb_dmxdev_feed_restart(dmxdevfilter);
509 dmxdevfilter->feed.sec = NULL;
510 break;
511 case DMXDEV_TYPE_PES:
512 if (!dmxdevfilter->feed.ts)
513 break;
514 dvb_dmxdev_feed_stop(dmxdevfilter);
515 dmxdevfilter->dev->demux->
516 release_ts_feed(dmxdevfilter->dev->demux,
517 dmxdevfilter->feed.ts);
518 dmxdevfilter->feed.ts = NULL;
519 break;
520 default:
521 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
522 return 0;
523 return -EINVAL;
526 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
527 return 0;
530 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
532 if (dmxdevfilter->state < DMXDEV_STATE_SET)
533 return 0;
535 dmxdevfilter->type = DMXDEV_TYPE_NONE;
536 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
537 return 0;
540 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
542 struct dmxdev *dmxdev = filter->dev;
543 void *mem;
544 int ret, i;
546 if (filter->state < DMXDEV_STATE_SET)
547 return -EINVAL;
549 if (filter->state >= DMXDEV_STATE_GO)
550 dvb_dmxdev_filter_stop(filter);
552 if (!filter->buffer.data) {
553 mem = vmalloc(filter->buffer.size);
554 if (!mem)
555 return -ENOMEM;
556 spin_lock_irq(&filter->dev->lock);
557 filter->buffer.data = mem;
558 spin_unlock_irq(&filter->dev->lock);
561 dvb_ringbuffer_flush(&filter->buffer);
563 switch (filter->type) {
564 case DMXDEV_TYPE_SEC:
566 struct dmx_sct_filter_params *para = &filter->params.sec;
567 struct dmx_section_filter **secfilter = &filter->filter.sec;
568 struct dmx_section_feed **secfeed = &filter->feed.sec;
570 *secfilter = NULL;
571 *secfeed = NULL;
574 /* find active filter/feed with same PID */
575 for (i = 0; i < dmxdev->filternum; i++) {
576 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
577 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
578 dmxdev->filter[i].params.sec.pid == para->pid) {
579 *secfeed = dmxdev->filter[i].feed.sec;
580 break;
584 /* if no feed found, try to allocate new one */
585 if (!*secfeed) {
586 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
587 secfeed,
588 dvb_dmxdev_section_callback);
589 if (ret < 0) {
590 printk("DVB (%s): could not alloc feed\n",
591 __func__);
592 return ret;
595 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
596 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
597 if (ret < 0) {
598 printk("DVB (%s): could not set feed\n",
599 __func__);
600 dvb_dmxdev_feed_restart(filter);
601 return ret;
603 } else {
604 dvb_dmxdev_feed_stop(filter);
607 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
608 if (ret < 0) {
609 dvb_dmxdev_feed_restart(filter);
610 filter->feed.sec->start_filtering(*secfeed);
611 dprintk("could not get filter\n");
612 return ret;
615 (*secfilter)->priv = filter;
617 memcpy(&((*secfilter)->filter_value[3]),
618 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
619 memcpy(&(*secfilter)->filter_mask[3],
620 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
621 memcpy(&(*secfilter)->filter_mode[3],
622 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
624 (*secfilter)->filter_value[0] = para->filter.filter[0];
625 (*secfilter)->filter_mask[0] = para->filter.mask[0];
626 (*secfilter)->filter_mode[0] = para->filter.mode[0];
627 (*secfilter)->filter_mask[1] = 0;
628 (*secfilter)->filter_mask[2] = 0;
630 filter->todo = 0;
632 ret = filter->feed.sec->start_filtering(filter->feed.sec);
633 if (ret < 0)
634 return ret;
636 dvb_dmxdev_filter_timer(filter);
637 break;
639 case DMXDEV_TYPE_PES:
641 struct timespec timeout = { 0 };
642 struct dmx_pes_filter_params *para = &filter->params.pes;
643 dmx_output_t otype;
644 int ret;
645 int ts_type;
646 enum dmx_ts_pes ts_pes;
647 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
649 filter->feed.ts = NULL;
650 otype = para->output;
652 ts_pes = (enum dmx_ts_pes)para->pes_type;
654 if (ts_pes < DMX_PES_OTHER)
655 ts_type = TS_DECODER;
656 else
657 ts_type = 0;
659 if (otype == DMX_OUT_TS_TAP)
660 ts_type |= TS_PACKET;
661 else if (otype == DMX_OUT_TSDEMUX_TAP)
662 ts_type |= TS_PACKET | TS_DEMUX;
663 else if (otype == DMX_OUT_TAP)
664 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
666 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
667 tsfeed,
668 dvb_dmxdev_ts_callback);
669 if (ret < 0)
670 return ret;
672 (*tsfeed)->priv = filter;
674 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
675 32768, timeout);
676 if (ret < 0) {
677 dmxdev->demux->release_ts_feed(dmxdev->demux,
678 *tsfeed);
679 return ret;
682 ret = filter->feed.ts->start_filtering(filter->feed.ts);
683 if (ret < 0) {
684 dmxdev->demux->release_ts_feed(dmxdev->demux,
685 *tsfeed);
686 return ret;
689 break;
691 default:
692 return -EINVAL;
695 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
696 return 0;
699 static int dvb_demux_open(struct inode *inode, struct file *file)
701 struct dvb_device *dvbdev = file->private_data;
702 struct dmxdev *dmxdev = dvbdev->priv;
703 int i;
704 struct dmxdev_filter *dmxdevfilter;
706 if (!dmxdev->filter)
707 return -EINVAL;
709 if (mutex_lock_interruptible(&dmxdev->mutex))
710 return -ERESTARTSYS;
712 for (i = 0; i < dmxdev->filternum; i++)
713 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
714 break;
716 if (i == dmxdev->filternum) {
717 mutex_unlock(&dmxdev->mutex);
718 return -EMFILE;
721 dmxdevfilter = &dmxdev->filter[i];
722 mutex_init(&dmxdevfilter->mutex);
723 file->private_data = dmxdevfilter;
725 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
726 dmxdevfilter->type = DMXDEV_TYPE_NONE;
727 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
728 dmxdevfilter->feed.ts = NULL;
729 init_timer(&dmxdevfilter->timer);
731 dvbdev->users++;
733 mutex_unlock(&dmxdev->mutex);
734 return 0;
737 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
738 struct dmxdev_filter *dmxdevfilter)
740 mutex_lock(&dmxdev->mutex);
741 mutex_lock(&dmxdevfilter->mutex);
743 dvb_dmxdev_filter_stop(dmxdevfilter);
744 dvb_dmxdev_filter_reset(dmxdevfilter);
746 if (dmxdevfilter->buffer.data) {
747 void *mem = dmxdevfilter->buffer.data;
749 spin_lock_irq(&dmxdev->lock);
750 dmxdevfilter->buffer.data = NULL;
751 spin_unlock_irq(&dmxdev->lock);
752 vfree(mem);
755 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
756 wake_up(&dmxdevfilter->buffer.queue);
757 mutex_unlock(&dmxdevfilter->mutex);
758 mutex_unlock(&dmxdev->mutex);
759 return 0;
762 static inline void invert_mode(dmx_filter_t *filter)
764 int i;
766 for (i = 0; i < DMX_FILTER_SIZE; i++)
767 filter->mode[i] ^= 0xff;
770 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
771 struct dmxdev_filter *dmxdevfilter,
772 struct dmx_sct_filter_params *params)
774 dprintk("function : %s\n", __func__);
776 dvb_dmxdev_filter_stop(dmxdevfilter);
778 dmxdevfilter->type = DMXDEV_TYPE_SEC;
779 memcpy(&dmxdevfilter->params.sec,
780 params, sizeof(struct dmx_sct_filter_params));
781 invert_mode(&dmxdevfilter->params.sec.filter);
782 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
784 if (params->flags & DMX_IMMEDIATE_START)
785 return dvb_dmxdev_filter_start(dmxdevfilter);
787 return 0;
790 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
791 struct dmxdev_filter *dmxdevfilter,
792 struct dmx_pes_filter_params *params)
794 dvb_dmxdev_filter_stop(dmxdevfilter);
796 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
797 return -EINVAL;
799 dmxdevfilter->type = DMXDEV_TYPE_PES;
800 memcpy(&dmxdevfilter->params, params,
801 sizeof(struct dmx_pes_filter_params));
803 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
805 if (params->flags & DMX_IMMEDIATE_START)
806 return dvb_dmxdev_filter_start(dmxdevfilter);
808 return 0;
811 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
812 struct file *file, char __user *buf,
813 size_t count, loff_t *ppos)
815 int result, hcount;
816 int done = 0;
818 if (dfil->todo <= 0) {
819 hcount = 3 + dfil->todo;
820 if (hcount > count)
821 hcount = count;
822 result = dvb_dmxdev_buffer_read(&dfil->buffer,
823 file->f_flags & O_NONBLOCK,
824 buf, hcount, ppos);
825 if (result < 0) {
826 dfil->todo = 0;
827 return result;
829 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
830 return -EFAULT;
831 buf += result;
832 done = result;
833 count -= result;
834 dfil->todo -= result;
835 if (dfil->todo > -3)
836 return done;
837 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
838 if (!count)
839 return done;
841 if (count > dfil->todo)
842 count = dfil->todo;
843 result = dvb_dmxdev_buffer_read(&dfil->buffer,
844 file->f_flags & O_NONBLOCK,
845 buf, count, ppos);
846 if (result < 0)
847 return result;
848 dfil->todo -= result;
849 return (result + done);
852 static ssize_t
853 dvb_demux_read(struct file *file, char __user *buf, size_t count,
854 loff_t *ppos)
856 struct dmxdev_filter *dmxdevfilter = file->private_data;
857 int ret;
859 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
860 return -ERESTARTSYS;
862 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
863 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
864 else
865 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
866 file->f_flags & O_NONBLOCK,
867 buf, count, ppos);
869 mutex_unlock(&dmxdevfilter->mutex);
870 return ret;
873 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
874 unsigned int cmd, void *parg)
876 struct dmxdev_filter *dmxdevfilter = file->private_data;
877 struct dmxdev *dmxdev = dmxdevfilter->dev;
878 unsigned long arg = (unsigned long)parg;
879 int ret = 0;
881 if (mutex_lock_interruptible(&dmxdev->mutex))
882 return -ERESTARTSYS;
884 switch (cmd) {
885 case DMX_START:
886 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
887 mutex_unlock(&dmxdev->mutex);
888 return -ERESTARTSYS;
890 if (dmxdevfilter->state < DMXDEV_STATE_SET)
891 ret = -EINVAL;
892 else
893 ret = dvb_dmxdev_filter_start(dmxdevfilter);
894 mutex_unlock(&dmxdevfilter->mutex);
895 break;
897 case DMX_STOP:
898 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
899 mutex_unlock(&dmxdev->mutex);
900 return -ERESTARTSYS;
902 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
903 mutex_unlock(&dmxdevfilter->mutex);
904 break;
906 case DMX_SET_FILTER:
907 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
908 mutex_unlock(&dmxdev->mutex);
909 return -ERESTARTSYS;
911 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
912 mutex_unlock(&dmxdevfilter->mutex);
913 break;
915 case DMX_SET_PES_FILTER:
916 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
917 mutex_unlock(&dmxdev->mutex);
918 return -ERESTARTSYS;
920 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
921 mutex_unlock(&dmxdevfilter->mutex);
922 break;
924 case DMX_SET_BUFFER_SIZE:
925 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
926 mutex_unlock(&dmxdev->mutex);
927 return -ERESTARTSYS;
929 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
930 mutex_unlock(&dmxdevfilter->mutex);
931 break;
933 case DMX_GET_PES_PIDS:
934 if (!dmxdev->demux->get_pes_pids) {
935 ret = -EINVAL;
936 break;
938 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
939 break;
941 case DMX_GET_CAPS:
942 if (!dmxdev->demux->get_caps) {
943 ret = -EINVAL;
944 break;
946 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
947 break;
949 case DMX_SET_SOURCE:
950 if (!dmxdev->demux->set_source) {
951 ret = -EINVAL;
952 break;
954 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
955 break;
957 case DMX_GET_STC:
958 if (!dmxdev->demux->get_stc) {
959 ret = -EINVAL;
960 break;
962 ret = dmxdev->demux->get_stc(dmxdev->demux,
963 ((struct dmx_stc *)parg)->num,
964 &((struct dmx_stc *)parg)->stc,
965 &((struct dmx_stc *)parg)->base);
966 break;
968 default:
969 ret = -EINVAL;
970 break;
972 mutex_unlock(&dmxdev->mutex);
973 return ret;
976 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
977 unsigned int cmd, unsigned long arg)
979 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
982 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
984 struct dmxdev_filter *dmxdevfilter = file->private_data;
985 unsigned int mask = 0;
987 if (!dmxdevfilter)
988 return -EINVAL;
990 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
992 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
993 dmxdevfilter->state != DMXDEV_STATE_DONE &&
994 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
995 return 0;
997 if (dmxdevfilter->buffer.error)
998 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1000 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1001 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1003 return mask;
1006 static int dvb_demux_release(struct inode *inode, struct file *file)
1008 struct dmxdev_filter *dmxdevfilter = file->private_data;
1009 struct dmxdev *dmxdev = dmxdevfilter->dev;
1011 int ret;
1013 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1015 mutex_lock(&dmxdev->mutex);
1016 dmxdev->dvbdev->users--;
1017 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1018 fops_put(file->f_op);
1019 file->f_op = NULL;
1020 mutex_unlock(&dmxdev->mutex);
1021 wake_up(&dmxdev->dvbdev->wait_queue);
1022 } else
1023 mutex_unlock(&dmxdev->mutex);
1025 return ret;
1028 static struct file_operations dvb_demux_fops = {
1029 .owner = THIS_MODULE,
1030 .read = dvb_demux_read,
1031 .ioctl = dvb_demux_ioctl,
1032 .open = dvb_demux_open,
1033 .release = dvb_demux_release,
1034 .poll = dvb_demux_poll,
1037 static struct dvb_device dvbdev_demux = {
1038 .priv = NULL,
1039 .users = 1,
1040 .writers = 1,
1041 .fops = &dvb_demux_fops
1044 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1045 unsigned int cmd, void *parg)
1047 struct dvb_device *dvbdev = file->private_data;
1048 struct dmxdev *dmxdev = dvbdev->priv;
1049 unsigned long arg = (unsigned long)parg;
1050 int ret;
1052 if (mutex_lock_interruptible(&dmxdev->mutex))
1053 return -ERESTARTSYS;
1055 switch (cmd) {
1056 case DMX_SET_BUFFER_SIZE:
1057 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1058 break;
1060 default:
1061 ret = -EINVAL;
1062 break;
1064 mutex_unlock(&dmxdev->mutex);
1065 return ret;
1068 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1069 unsigned int cmd, unsigned long arg)
1071 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1074 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1076 struct dvb_device *dvbdev = file->private_data;
1077 struct dmxdev *dmxdev = dvbdev->priv;
1078 unsigned int mask = 0;
1080 dprintk("function : %s\n", __func__);
1082 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1084 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1085 if (dmxdev->dvr_buffer.error)
1086 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1088 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1089 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1090 } else
1091 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1093 return mask;
1096 static struct file_operations dvb_dvr_fops = {
1097 .owner = THIS_MODULE,
1098 .read = dvb_dvr_read,
1099 .write = dvb_dvr_write,
1100 .ioctl = dvb_dvr_ioctl,
1101 .open = dvb_dvr_open,
1102 .release = dvb_dvr_release,
1103 .poll = dvb_dvr_poll,
1106 static struct dvb_device dvbdev_dvr = {
1107 .priv = NULL,
1108 .readers = 1,
1109 .users = 1,
1110 .fops = &dvb_dvr_fops
1113 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1115 int i;
1117 if (dmxdev->demux->open(dmxdev->demux) < 0)
1118 return -EUSERS;
1120 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1121 if (!dmxdev->filter)
1122 return -ENOMEM;
1124 mutex_init(&dmxdev->mutex);
1125 spin_lock_init(&dmxdev->lock);
1126 for (i = 0; i < dmxdev->filternum; i++) {
1127 dmxdev->filter[i].dev = dmxdev;
1128 dmxdev->filter[i].buffer.data = NULL;
1129 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1130 DMXDEV_STATE_FREE);
1133 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1134 DVB_DEVICE_DEMUX);
1135 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1136 dmxdev, DVB_DEVICE_DVR);
1138 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1140 return 0;
1143 EXPORT_SYMBOL(dvb_dmxdev_init);
1145 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1147 dmxdev->exit=1;
1148 if (dmxdev->dvbdev->users > 1) {
1149 wait_event(dmxdev->dvbdev->wait_queue,
1150 dmxdev->dvbdev->users==1);
1152 if (dmxdev->dvr_dvbdev->users > 1) {
1153 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1154 dmxdev->dvr_dvbdev->users==1);
1157 dvb_unregister_device(dmxdev->dvbdev);
1158 dvb_unregister_device(dmxdev->dvr_dvbdev);
1160 vfree(dmxdev->filter);
1161 dmxdev->filter = NULL;
1162 dmxdev->demux->close(dmxdev->demux);
1165 EXPORT_SYMBOL(dvb_dmxdev_release);