powerpc: use consistent types in mktree
[zen-stable.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob6d6121eb5d592ea9157f0a8610069abc8ab4d8c9
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_user(src, buf, avail);
100 if (ret < 0)
101 break;
103 buf += ret;
106 return (count - todo) ? (count - todo) : ret;
109 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
111 struct list_head *head, *pos;
113 head = demux->get_frontends(demux);
114 if (!head)
115 return NULL;
116 list_for_each(pos, head)
117 if (DMX_FE_ENTRY(pos)->source == type)
118 return DMX_FE_ENTRY(pos);
120 return NULL;
123 static int dvb_dvr_open(struct inode *inode, struct file *file)
125 struct dvb_device *dvbdev = file->private_data;
126 struct dmxdev *dmxdev = dvbdev->priv;
127 struct dmx_frontend *front;
129 dprintk("function : %s\n", __func__);
131 if (mutex_lock_interruptible(&dmxdev->mutex))
132 return -ERESTARTSYS;
134 if (dmxdev->exit) {
135 mutex_unlock(&dmxdev->mutex);
136 return -ENODEV;
139 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
141 mutex_unlock(&dmxdev->mutex);
142 return -EOPNOTSUPP;
146 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
147 void *mem;
148 if (!dvbdev->readers) {
149 mutex_unlock(&dmxdev->mutex);
150 return -EBUSY;
152 mem = vmalloc(DVR_BUFFER_SIZE);
153 if (!mem) {
154 mutex_unlock(&dmxdev->mutex);
155 return -ENOMEM;
157 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
158 dvbdev->readers--;
161 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
162 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
164 if (!dmxdev->demux->write) {
165 mutex_unlock(&dmxdev->mutex);
166 return -EOPNOTSUPP;
169 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
171 if (!front) {
172 mutex_unlock(&dmxdev->mutex);
173 return -EINVAL;
175 dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 dmxdev->demux->connect_frontend(dmxdev->demux, front);
178 dvbdev->users++;
179 mutex_unlock(&dmxdev->mutex);
180 return 0;
183 static int dvb_dvr_release(struct inode *inode, struct file *file)
185 struct dvb_device *dvbdev = file->private_data;
186 struct dmxdev *dmxdev = dvbdev->priv;
188 mutex_lock(&dmxdev->mutex);
190 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
191 dmxdev->demux->disconnect_frontend(dmxdev->demux);
192 dmxdev->demux->connect_frontend(dmxdev->demux,
193 dmxdev->dvr_orig_fe);
195 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
196 dvbdev->readers++;
197 if (dmxdev->dvr_buffer.data) {
198 void *mem = dmxdev->dvr_buffer.data;
199 mb();
200 spin_lock_irq(&dmxdev->lock);
201 dmxdev->dvr_buffer.data = NULL;
202 spin_unlock_irq(&dmxdev->lock);
203 vfree(mem);
206 /* TODO */
207 dvbdev->users--;
208 if(dvbdev->users==-1 && dmxdev->exit==1) {
209 fops_put(file->f_op);
210 file->f_op = NULL;
211 mutex_unlock(&dmxdev->mutex);
212 wake_up(&dvbdev->wait_queue);
213 } else
214 mutex_unlock(&dmxdev->mutex);
216 return 0;
219 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
220 size_t count, loff_t *ppos)
222 struct dvb_device *dvbdev = file->private_data;
223 struct dmxdev *dmxdev = dvbdev->priv;
224 int ret;
226 if (!dmxdev->demux->write)
227 return -EOPNOTSUPP;
228 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
229 return -EINVAL;
230 if (mutex_lock_interruptible(&dmxdev->mutex))
231 return -ERESTARTSYS;
233 if (dmxdev->exit) {
234 mutex_unlock(&dmxdev->mutex);
235 return -ENODEV;
237 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
238 mutex_unlock(&dmxdev->mutex);
239 return ret;
242 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
243 loff_t *ppos)
245 struct dvb_device *dvbdev = file->private_data;
246 struct dmxdev *dmxdev = dvbdev->priv;
248 if (dmxdev->exit)
249 return -ENODEV;
251 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
252 file->f_flags & O_NONBLOCK,
253 buf, count, ppos);
256 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
257 unsigned long size)
259 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
260 void *newmem;
261 void *oldmem;
263 dprintk("function : %s\n", __func__);
265 if (buf->size == size)
266 return 0;
267 if (!size)
268 return -EINVAL;
270 newmem = vmalloc(size);
271 if (!newmem)
272 return -ENOMEM;
274 oldmem = buf->data;
276 spin_lock_irq(&dmxdev->lock);
277 buf->data = newmem;
278 buf->size = size;
280 /* reset and not flush in case the buffer shrinks */
281 dvb_ringbuffer_reset(buf);
282 spin_unlock_irq(&dmxdev->lock);
284 vfree(oldmem);
286 return 0;
289 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 *dmxdevfilter, int state)
292 spin_lock_irq(&dmxdevfilter->dev->lock);
293 dmxdevfilter->state = state;
294 spin_unlock_irq(&dmxdevfilter->dev->lock);
297 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
298 unsigned long size)
300 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
301 void *newmem;
302 void *oldmem;
304 if (buf->size == size)
305 return 0;
306 if (!size)
307 return -EINVAL;
308 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
309 return -EBUSY;
311 newmem = vmalloc(size);
312 if (!newmem)
313 return -ENOMEM;
315 oldmem = buf->data;
317 spin_lock_irq(&dmxdevfilter->dev->lock);
318 buf->data = newmem;
319 buf->size = size;
321 /* reset and not flush in case the buffer shrinks */
322 dvb_ringbuffer_reset(buf);
323 spin_unlock_irq(&dmxdevfilter->dev->lock);
325 vfree(oldmem);
327 return 0;
330 static void dvb_dmxdev_filter_timeout(unsigned long data)
332 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
334 dmxdevfilter->buffer.error = -ETIMEDOUT;
335 spin_lock_irq(&dmxdevfilter->dev->lock);
336 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
337 spin_unlock_irq(&dmxdevfilter->dev->lock);
338 wake_up(&dmxdevfilter->buffer.queue);
341 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
343 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
345 del_timer(&dmxdevfilter->timer);
346 if (para->timeout) {
347 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
348 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
349 dmxdevfilter->timer.expires =
350 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
351 add_timer(&dmxdevfilter->timer);
355 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
356 const u8 *buffer2, size_t buffer2_len,
357 struct dmx_section_filter *filter,
358 enum dmx_success success)
360 struct dmxdev_filter *dmxdevfilter = filter->priv;
361 int ret;
363 if (dmxdevfilter->buffer.error) {
364 wake_up(&dmxdevfilter->buffer.queue);
365 return 0;
367 spin_lock(&dmxdevfilter->dev->lock);
368 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
369 spin_unlock(&dmxdevfilter->dev->lock);
370 return 0;
372 del_timer(&dmxdevfilter->timer);
373 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
374 buffer1[0], buffer1[1],
375 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
376 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
377 buffer1_len);
378 if (ret == buffer1_len) {
379 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
380 buffer2_len);
382 if (ret < 0) {
383 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
384 dmxdevfilter->buffer.error = ret;
386 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
387 dmxdevfilter->state = DMXDEV_STATE_DONE;
388 spin_unlock(&dmxdevfilter->dev->lock);
389 wake_up(&dmxdevfilter->buffer.queue);
390 return 0;
393 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
394 const u8 *buffer2, size_t buffer2_len,
395 struct dmx_ts_feed *feed,
396 enum dmx_success success)
398 struct dmxdev_filter *dmxdevfilter = feed->priv;
399 struct dvb_ringbuffer *buffer;
400 int ret;
402 spin_lock(&dmxdevfilter->dev->lock);
403 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
404 spin_unlock(&dmxdevfilter->dev->lock);
405 return 0;
408 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
409 || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
410 buffer = &dmxdevfilter->buffer;
411 else
412 buffer = &dmxdevfilter->dev->dvr_buffer;
413 if (buffer->error) {
414 spin_unlock(&dmxdevfilter->dev->lock);
415 wake_up(&buffer->queue);
416 return 0;
418 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
419 if (ret == buffer1_len)
420 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
421 if (ret < 0) {
422 dvb_ringbuffer_flush(buffer);
423 buffer->error = ret;
425 spin_unlock(&dmxdevfilter->dev->lock);
426 wake_up(&buffer->queue);
427 return 0;
430 /* stop feed but only mark the specified filter as stopped (state set) */
431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
433 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
435 switch (dmxdevfilter->type) {
436 case DMXDEV_TYPE_SEC:
437 del_timer(&dmxdevfilter->timer);
438 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
439 break;
440 case DMXDEV_TYPE_PES:
441 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
442 break;
443 default:
444 return -EINVAL;
446 return 0;
449 /* start feed associated with the specified filter */
450 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
452 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
454 switch (filter->type) {
455 case DMXDEV_TYPE_SEC:
456 return filter->feed.sec->start_filtering(filter->feed.sec);
457 case DMXDEV_TYPE_PES:
458 return filter->feed.ts->start_filtering(filter->feed.ts);
459 default:
460 return -EINVAL;
463 return 0;
466 /* restart section feed if it has filters left associated with it,
467 otherwise release the feed */
468 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
470 int i;
471 struct dmxdev *dmxdev = filter->dev;
472 u16 pid = filter->params.sec.pid;
474 for (i = 0; i < dmxdev->filternum; i++)
475 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
476 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
477 dmxdev->filter[i].params.sec.pid == pid) {
478 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
479 return 0;
482 filter->dev->demux->release_section_feed(dmxdev->demux,
483 filter->feed.sec);
485 return 0;
488 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
490 if (dmxdevfilter->state < DMXDEV_STATE_GO)
491 return 0;
493 switch (dmxdevfilter->type) {
494 case DMXDEV_TYPE_SEC:
495 if (!dmxdevfilter->feed.sec)
496 break;
497 dvb_dmxdev_feed_stop(dmxdevfilter);
498 if (dmxdevfilter->filter.sec)
499 dmxdevfilter->feed.sec->
500 release_filter(dmxdevfilter->feed.sec,
501 dmxdevfilter->filter.sec);
502 dvb_dmxdev_feed_restart(dmxdevfilter);
503 dmxdevfilter->feed.sec = NULL;
504 break;
505 case DMXDEV_TYPE_PES:
506 if (!dmxdevfilter->feed.ts)
507 break;
508 dvb_dmxdev_feed_stop(dmxdevfilter);
509 dmxdevfilter->dev->demux->
510 release_ts_feed(dmxdevfilter->dev->demux,
511 dmxdevfilter->feed.ts);
512 dmxdevfilter->feed.ts = NULL;
513 break;
514 default:
515 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
516 return 0;
517 return -EINVAL;
520 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
521 return 0;
524 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
526 if (dmxdevfilter->state < DMXDEV_STATE_SET)
527 return 0;
529 dmxdevfilter->type = DMXDEV_TYPE_NONE;
530 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
531 return 0;
534 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
536 struct dmxdev *dmxdev = filter->dev;
537 void *mem;
538 int ret, i;
540 if (filter->state < DMXDEV_STATE_SET)
541 return -EINVAL;
543 if (filter->state >= DMXDEV_STATE_GO)
544 dvb_dmxdev_filter_stop(filter);
546 if (!filter->buffer.data) {
547 mem = vmalloc(filter->buffer.size);
548 if (!mem)
549 return -ENOMEM;
550 spin_lock_irq(&filter->dev->lock);
551 filter->buffer.data = mem;
552 spin_unlock_irq(&filter->dev->lock);
555 dvb_ringbuffer_flush(&filter->buffer);
557 switch (filter->type) {
558 case DMXDEV_TYPE_SEC:
560 struct dmx_sct_filter_params *para = &filter->params.sec;
561 struct dmx_section_filter **secfilter = &filter->filter.sec;
562 struct dmx_section_feed **secfeed = &filter->feed.sec;
564 *secfilter = NULL;
565 *secfeed = NULL;
568 /* find active filter/feed with same PID */
569 for (i = 0; i < dmxdev->filternum; i++) {
570 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
571 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
572 dmxdev->filter[i].params.sec.pid == para->pid) {
573 *secfeed = dmxdev->filter[i].feed.sec;
574 break;
578 /* if no feed found, try to allocate new one */
579 if (!*secfeed) {
580 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
581 secfeed,
582 dvb_dmxdev_section_callback);
583 if (ret < 0) {
584 printk("DVB (%s): could not alloc feed\n",
585 __func__);
586 return ret;
589 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
590 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
591 if (ret < 0) {
592 printk("DVB (%s): could not set feed\n",
593 __func__);
594 dvb_dmxdev_feed_restart(filter);
595 return ret;
597 } else {
598 dvb_dmxdev_feed_stop(filter);
601 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
602 if (ret < 0) {
603 dvb_dmxdev_feed_restart(filter);
604 filter->feed.sec->start_filtering(*secfeed);
605 dprintk("could not get filter\n");
606 return ret;
609 (*secfilter)->priv = filter;
611 memcpy(&((*secfilter)->filter_value[3]),
612 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
613 memcpy(&(*secfilter)->filter_mask[3],
614 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
615 memcpy(&(*secfilter)->filter_mode[3],
616 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
618 (*secfilter)->filter_value[0] = para->filter.filter[0];
619 (*secfilter)->filter_mask[0] = para->filter.mask[0];
620 (*secfilter)->filter_mode[0] = para->filter.mode[0];
621 (*secfilter)->filter_mask[1] = 0;
622 (*secfilter)->filter_mask[2] = 0;
624 filter->todo = 0;
626 ret = filter->feed.sec->start_filtering(filter->feed.sec);
627 if (ret < 0)
628 return ret;
630 dvb_dmxdev_filter_timer(filter);
631 break;
633 case DMXDEV_TYPE_PES:
635 struct timespec timeout = { 0 };
636 struct dmx_pes_filter_params *para = &filter->params.pes;
637 dmx_output_t otype;
638 int ts_type;
639 enum dmx_ts_pes ts_pes;
640 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
642 filter->feed.ts = NULL;
643 otype = para->output;
645 ts_pes = (enum dmx_ts_pes)para->pes_type;
647 if (ts_pes < DMX_PES_OTHER)
648 ts_type = TS_DECODER;
649 else
650 ts_type = 0;
652 if (otype == DMX_OUT_TS_TAP)
653 ts_type |= TS_PACKET;
654 else if (otype == DMX_OUT_TSDEMUX_TAP)
655 ts_type |= TS_PACKET | TS_DEMUX;
656 else if (otype == DMX_OUT_TAP)
657 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
659 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
660 tsfeed,
661 dvb_dmxdev_ts_callback);
662 if (ret < 0)
663 return ret;
665 (*tsfeed)->priv = filter;
667 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
668 32768, timeout);
669 if (ret < 0) {
670 dmxdev->demux->release_ts_feed(dmxdev->demux,
671 *tsfeed);
672 return ret;
675 ret = filter->feed.ts->start_filtering(filter->feed.ts);
676 if (ret < 0) {
677 dmxdev->demux->release_ts_feed(dmxdev->demux,
678 *tsfeed);
679 return ret;
682 break;
684 default:
685 return -EINVAL;
688 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
689 return 0;
692 static int dvb_demux_open(struct inode *inode, struct file *file)
694 struct dvb_device *dvbdev = file->private_data;
695 struct dmxdev *dmxdev = dvbdev->priv;
696 int i;
697 struct dmxdev_filter *dmxdevfilter;
699 if (!dmxdev->filter)
700 return -EINVAL;
702 if (mutex_lock_interruptible(&dmxdev->mutex))
703 return -ERESTARTSYS;
705 for (i = 0; i < dmxdev->filternum; i++)
706 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
707 break;
709 if (i == dmxdev->filternum) {
710 mutex_unlock(&dmxdev->mutex);
711 return -EMFILE;
714 dmxdevfilter = &dmxdev->filter[i];
715 mutex_init(&dmxdevfilter->mutex);
716 file->private_data = dmxdevfilter;
718 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
719 dmxdevfilter->type = DMXDEV_TYPE_NONE;
720 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
721 dmxdevfilter->feed.ts = NULL;
722 init_timer(&dmxdevfilter->timer);
724 dvbdev->users++;
726 mutex_unlock(&dmxdev->mutex);
727 return 0;
730 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
731 struct dmxdev_filter *dmxdevfilter)
733 mutex_lock(&dmxdev->mutex);
734 mutex_lock(&dmxdevfilter->mutex);
736 dvb_dmxdev_filter_stop(dmxdevfilter);
737 dvb_dmxdev_filter_reset(dmxdevfilter);
739 if (dmxdevfilter->buffer.data) {
740 void *mem = dmxdevfilter->buffer.data;
742 spin_lock_irq(&dmxdev->lock);
743 dmxdevfilter->buffer.data = NULL;
744 spin_unlock_irq(&dmxdev->lock);
745 vfree(mem);
748 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
749 wake_up(&dmxdevfilter->buffer.queue);
750 mutex_unlock(&dmxdevfilter->mutex);
751 mutex_unlock(&dmxdev->mutex);
752 return 0;
755 static inline void invert_mode(dmx_filter_t *filter)
757 int i;
759 for (i = 0; i < DMX_FILTER_SIZE; i++)
760 filter->mode[i] ^= 0xff;
763 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
764 struct dmxdev_filter *dmxdevfilter,
765 struct dmx_sct_filter_params *params)
767 dprintk("function : %s\n", __func__);
769 dvb_dmxdev_filter_stop(dmxdevfilter);
771 dmxdevfilter->type = DMXDEV_TYPE_SEC;
772 memcpy(&dmxdevfilter->params.sec,
773 params, sizeof(struct dmx_sct_filter_params));
774 invert_mode(&dmxdevfilter->params.sec.filter);
775 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
777 if (params->flags & DMX_IMMEDIATE_START)
778 return dvb_dmxdev_filter_start(dmxdevfilter);
780 return 0;
783 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
784 struct dmxdev_filter *dmxdevfilter,
785 struct dmx_pes_filter_params *params)
787 dvb_dmxdev_filter_stop(dmxdevfilter);
789 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
790 return -EINVAL;
792 dmxdevfilter->type = DMXDEV_TYPE_PES;
793 memcpy(&dmxdevfilter->params, params,
794 sizeof(struct dmx_pes_filter_params));
796 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
798 if (params->flags & DMX_IMMEDIATE_START)
799 return dvb_dmxdev_filter_start(dmxdevfilter);
801 return 0;
804 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
805 struct file *file, char __user *buf,
806 size_t count, loff_t *ppos)
808 int result, hcount;
809 int done = 0;
811 if (dfil->todo <= 0) {
812 hcount = 3 + dfil->todo;
813 if (hcount > count)
814 hcount = count;
815 result = dvb_dmxdev_buffer_read(&dfil->buffer,
816 file->f_flags & O_NONBLOCK,
817 buf, hcount, ppos);
818 if (result < 0) {
819 dfil->todo = 0;
820 return result;
822 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
823 return -EFAULT;
824 buf += result;
825 done = result;
826 count -= result;
827 dfil->todo -= result;
828 if (dfil->todo > -3)
829 return done;
830 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
831 if (!count)
832 return done;
834 if (count > dfil->todo)
835 count = dfil->todo;
836 result = dvb_dmxdev_buffer_read(&dfil->buffer,
837 file->f_flags & O_NONBLOCK,
838 buf, count, ppos);
839 if (result < 0)
840 return result;
841 dfil->todo -= result;
842 return (result + done);
845 static ssize_t
846 dvb_demux_read(struct file *file, char __user *buf, size_t count,
847 loff_t *ppos)
849 struct dmxdev_filter *dmxdevfilter = file->private_data;
850 int ret;
852 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
853 return -ERESTARTSYS;
855 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
856 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
857 else
858 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
859 file->f_flags & O_NONBLOCK,
860 buf, count, ppos);
862 mutex_unlock(&dmxdevfilter->mutex);
863 return ret;
866 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
867 unsigned int cmd, void *parg)
869 struct dmxdev_filter *dmxdevfilter = file->private_data;
870 struct dmxdev *dmxdev = dmxdevfilter->dev;
871 unsigned long arg = (unsigned long)parg;
872 int ret = 0;
874 if (mutex_lock_interruptible(&dmxdev->mutex))
875 return -ERESTARTSYS;
877 switch (cmd) {
878 case DMX_START:
879 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
880 mutex_unlock(&dmxdev->mutex);
881 return -ERESTARTSYS;
883 if (dmxdevfilter->state < DMXDEV_STATE_SET)
884 ret = -EINVAL;
885 else
886 ret = dvb_dmxdev_filter_start(dmxdevfilter);
887 mutex_unlock(&dmxdevfilter->mutex);
888 break;
890 case DMX_STOP:
891 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
892 mutex_unlock(&dmxdev->mutex);
893 return -ERESTARTSYS;
895 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
896 mutex_unlock(&dmxdevfilter->mutex);
897 break;
899 case DMX_SET_FILTER:
900 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
901 mutex_unlock(&dmxdev->mutex);
902 return -ERESTARTSYS;
904 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
905 mutex_unlock(&dmxdevfilter->mutex);
906 break;
908 case DMX_SET_PES_FILTER:
909 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
910 mutex_unlock(&dmxdev->mutex);
911 return -ERESTARTSYS;
913 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
914 mutex_unlock(&dmxdevfilter->mutex);
915 break;
917 case DMX_SET_BUFFER_SIZE:
918 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
919 mutex_unlock(&dmxdev->mutex);
920 return -ERESTARTSYS;
922 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
923 mutex_unlock(&dmxdevfilter->mutex);
924 break;
926 case DMX_GET_PES_PIDS:
927 if (!dmxdev->demux->get_pes_pids) {
928 ret = -EINVAL;
929 break;
931 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
932 break;
934 case DMX_GET_CAPS:
935 if (!dmxdev->demux->get_caps) {
936 ret = -EINVAL;
937 break;
939 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
940 break;
942 case DMX_SET_SOURCE:
943 if (!dmxdev->demux->set_source) {
944 ret = -EINVAL;
945 break;
947 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
948 break;
950 case DMX_GET_STC:
951 if (!dmxdev->demux->get_stc) {
952 ret = -EINVAL;
953 break;
955 ret = dmxdev->demux->get_stc(dmxdev->demux,
956 ((struct dmx_stc *)parg)->num,
957 &((struct dmx_stc *)parg)->stc,
958 &((struct dmx_stc *)parg)->base);
959 break;
961 default:
962 ret = -EINVAL;
963 break;
965 mutex_unlock(&dmxdev->mutex);
966 return ret;
969 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
970 unsigned int cmd, unsigned long arg)
972 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
975 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
977 struct dmxdev_filter *dmxdevfilter = file->private_data;
978 unsigned int mask = 0;
980 if (!dmxdevfilter)
981 return -EINVAL;
983 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
985 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
986 dmxdevfilter->state != DMXDEV_STATE_DONE &&
987 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
988 return 0;
990 if (dmxdevfilter->buffer.error)
991 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
993 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
994 mask |= (POLLIN | POLLRDNORM | POLLPRI);
996 return mask;
999 static int dvb_demux_release(struct inode *inode, struct file *file)
1001 struct dmxdev_filter *dmxdevfilter = file->private_data;
1002 struct dmxdev *dmxdev = dmxdevfilter->dev;
1004 int ret;
1006 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1008 mutex_lock(&dmxdev->mutex);
1009 dmxdev->dvbdev->users--;
1010 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1011 fops_put(file->f_op);
1012 file->f_op = NULL;
1013 mutex_unlock(&dmxdev->mutex);
1014 wake_up(&dmxdev->dvbdev->wait_queue);
1015 } else
1016 mutex_unlock(&dmxdev->mutex);
1018 return ret;
1021 static const struct file_operations dvb_demux_fops = {
1022 .owner = THIS_MODULE,
1023 .read = dvb_demux_read,
1024 .ioctl = dvb_demux_ioctl,
1025 .open = dvb_demux_open,
1026 .release = dvb_demux_release,
1027 .poll = dvb_demux_poll,
1030 static struct dvb_device dvbdev_demux = {
1031 .priv = NULL,
1032 .users = 1,
1033 .writers = 1,
1034 .fops = &dvb_demux_fops
1037 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1038 unsigned int cmd, void *parg)
1040 struct dvb_device *dvbdev = file->private_data;
1041 struct dmxdev *dmxdev = dvbdev->priv;
1042 unsigned long arg = (unsigned long)parg;
1043 int ret;
1045 if (mutex_lock_interruptible(&dmxdev->mutex))
1046 return -ERESTARTSYS;
1048 switch (cmd) {
1049 case DMX_SET_BUFFER_SIZE:
1050 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1051 break;
1053 default:
1054 ret = -EINVAL;
1055 break;
1057 mutex_unlock(&dmxdev->mutex);
1058 return ret;
1061 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1062 unsigned int cmd, unsigned long arg)
1064 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1067 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1069 struct dvb_device *dvbdev = file->private_data;
1070 struct dmxdev *dmxdev = dvbdev->priv;
1071 unsigned int mask = 0;
1073 dprintk("function : %s\n", __func__);
1075 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1077 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1078 if (dmxdev->dvr_buffer.error)
1079 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1081 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1082 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1083 } else
1084 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1086 return mask;
1089 static struct file_operations dvb_dvr_fops = {
1090 .owner = THIS_MODULE,
1091 .read = dvb_dvr_read,
1092 .write = dvb_dvr_write,
1093 .ioctl = dvb_dvr_ioctl,
1094 .open = dvb_dvr_open,
1095 .release = dvb_dvr_release,
1096 .poll = dvb_dvr_poll,
1099 static struct dvb_device dvbdev_dvr = {
1100 .priv = NULL,
1101 .readers = 1,
1102 .users = 1,
1103 .fops = &dvb_dvr_fops
1106 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1108 int i;
1110 if (dmxdev->demux->open(dmxdev->demux) < 0)
1111 return -EUSERS;
1113 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1114 if (!dmxdev->filter)
1115 return -ENOMEM;
1117 mutex_init(&dmxdev->mutex);
1118 spin_lock_init(&dmxdev->lock);
1119 for (i = 0; i < dmxdev->filternum; i++) {
1120 dmxdev->filter[i].dev = dmxdev;
1121 dmxdev->filter[i].buffer.data = NULL;
1122 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1123 DMXDEV_STATE_FREE);
1126 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1127 DVB_DEVICE_DEMUX);
1128 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1129 dmxdev, DVB_DEVICE_DVR);
1131 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1133 return 0;
1136 EXPORT_SYMBOL(dvb_dmxdev_init);
1138 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1140 dmxdev->exit=1;
1141 if (dmxdev->dvbdev->users > 1) {
1142 wait_event(dmxdev->dvbdev->wait_queue,
1143 dmxdev->dvbdev->users==1);
1145 if (dmxdev->dvr_dvbdev->users > 1) {
1146 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1147 dmxdev->dvr_dvbdev->users==1);
1150 dvb_unregister_device(dmxdev->dvbdev);
1151 dvb_unregister_device(dmxdev->dvr_dvbdev);
1153 vfree(dmxdev->filter);
1154 dmxdev->filter = NULL;
1155 dmxdev->demux->close(dmxdev->demux);
1158 EXPORT_SYMBOL(dvb_dmxdev_release);