[NETFILTER]: nf_conntrack: export hash allocation/destruction functions
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob275df65fde992680b82631f7ee1ca98750a6d256
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/moduleparam.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(src, buf, avail, 1);
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", __FUNCTION__);
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;
248 int ret;
250 if (dmxdev->exit) {
251 mutex_unlock(&dmxdev->mutex);
252 return -ENODEV;
255 //mutex_lock(&dmxdev->mutex);
256 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
257 file->f_flags & O_NONBLOCK,
258 buf, count, ppos);
259 //mutex_unlock(&dmxdev->mutex);
260 return ret;
263 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
264 *dmxdevfilter, int state)
266 spin_lock_irq(&dmxdevfilter->dev->lock);
267 dmxdevfilter->state = state;
268 spin_unlock_irq(&dmxdevfilter->dev->lock);
271 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
272 unsigned long size)
274 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
275 void *mem;
277 if (buf->size == size)
278 return 0;
279 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
280 return -EBUSY;
281 spin_lock_irq(&dmxdevfilter->dev->lock);
282 mem = buf->data;
283 buf->data = NULL;
284 buf->size = size;
285 dvb_ringbuffer_flush(buf);
286 spin_unlock_irq(&dmxdevfilter->dev->lock);
287 vfree(mem);
289 if (buf->size) {
290 mem = vmalloc(dmxdevfilter->buffer.size);
291 if (!mem)
292 return -ENOMEM;
293 spin_lock_irq(&dmxdevfilter->dev->lock);
294 buf->data = mem;
295 spin_unlock_irq(&dmxdevfilter->dev->lock);
297 return 0;
300 static void dvb_dmxdev_filter_timeout(unsigned long data)
302 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
304 dmxdevfilter->buffer.error = -ETIMEDOUT;
305 spin_lock_irq(&dmxdevfilter->dev->lock);
306 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
307 spin_unlock_irq(&dmxdevfilter->dev->lock);
308 wake_up(&dmxdevfilter->buffer.queue);
311 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
313 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
315 del_timer(&dmxdevfilter->timer);
316 if (para->timeout) {
317 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
318 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
319 dmxdevfilter->timer.expires =
320 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
321 add_timer(&dmxdevfilter->timer);
325 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
326 const u8 *buffer2, size_t buffer2_len,
327 struct dmx_section_filter *filter,
328 enum dmx_success success)
330 struct dmxdev_filter *dmxdevfilter = filter->priv;
331 int ret;
333 if (dmxdevfilter->buffer.error) {
334 wake_up(&dmxdevfilter->buffer.queue);
335 return 0;
337 spin_lock(&dmxdevfilter->dev->lock);
338 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
339 spin_unlock(&dmxdevfilter->dev->lock);
340 return 0;
342 del_timer(&dmxdevfilter->timer);
343 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
344 buffer1[0], buffer1[1],
345 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
346 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
347 buffer1_len);
348 if (ret == buffer1_len) {
349 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
350 buffer2_len);
352 if (ret < 0) {
353 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
354 dmxdevfilter->buffer.error = ret;
356 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
357 dmxdevfilter->state = DMXDEV_STATE_DONE;
358 spin_unlock(&dmxdevfilter->dev->lock);
359 wake_up(&dmxdevfilter->buffer.queue);
360 return 0;
363 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
364 const u8 *buffer2, size_t buffer2_len,
365 struct dmx_ts_feed *feed,
366 enum dmx_success success)
368 struct dmxdev_filter *dmxdevfilter = feed->priv;
369 struct dvb_ringbuffer *buffer;
370 int ret;
372 spin_lock(&dmxdevfilter->dev->lock);
373 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
374 spin_unlock(&dmxdevfilter->dev->lock);
375 return 0;
378 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
379 buffer = &dmxdevfilter->buffer;
380 else
381 buffer = &dmxdevfilter->dev->dvr_buffer;
382 if (buffer->error) {
383 spin_unlock(&dmxdevfilter->dev->lock);
384 wake_up(&buffer->queue);
385 return 0;
387 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
388 if (ret == buffer1_len)
389 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
390 if (ret < 0) {
391 dvb_ringbuffer_flush(buffer);
392 buffer->error = ret;
394 spin_unlock(&dmxdevfilter->dev->lock);
395 wake_up(&buffer->queue);
396 return 0;
399 /* stop feed but only mark the specified filter as stopped (state set) */
400 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
402 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
404 switch (dmxdevfilter->type) {
405 case DMXDEV_TYPE_SEC:
406 del_timer(&dmxdevfilter->timer);
407 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
408 break;
409 case DMXDEV_TYPE_PES:
410 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
411 break;
412 default:
413 return -EINVAL;
415 return 0;
418 /* start feed associated with the specified filter */
419 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
421 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
423 switch (filter->type) {
424 case DMXDEV_TYPE_SEC:
425 return filter->feed.sec->start_filtering(filter->feed.sec);
426 case DMXDEV_TYPE_PES:
427 return filter->feed.ts->start_filtering(filter->feed.ts);
428 default:
429 return -EINVAL;
432 return 0;
435 /* restart section feed if it has filters left associated with it,
436 otherwise release the feed */
437 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
439 int i;
440 struct dmxdev *dmxdev = filter->dev;
441 u16 pid = filter->params.sec.pid;
443 for (i = 0; i < dmxdev->filternum; i++)
444 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
445 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
446 dmxdev->filter[i].params.sec.pid == pid) {
447 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
448 return 0;
451 filter->dev->demux->release_section_feed(dmxdev->demux,
452 filter->feed.sec);
454 return 0;
457 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
459 if (dmxdevfilter->state < DMXDEV_STATE_GO)
460 return 0;
462 switch (dmxdevfilter->type) {
463 case DMXDEV_TYPE_SEC:
464 if (!dmxdevfilter->feed.sec)
465 break;
466 dvb_dmxdev_feed_stop(dmxdevfilter);
467 if (dmxdevfilter->filter.sec)
468 dmxdevfilter->feed.sec->
469 release_filter(dmxdevfilter->feed.sec,
470 dmxdevfilter->filter.sec);
471 dvb_dmxdev_feed_restart(dmxdevfilter);
472 dmxdevfilter->feed.sec = NULL;
473 break;
474 case DMXDEV_TYPE_PES:
475 if (!dmxdevfilter->feed.ts)
476 break;
477 dvb_dmxdev_feed_stop(dmxdevfilter);
478 dmxdevfilter->dev->demux->
479 release_ts_feed(dmxdevfilter->dev->demux,
480 dmxdevfilter->feed.ts);
481 dmxdevfilter->feed.ts = NULL;
482 break;
483 default:
484 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
485 return 0;
486 return -EINVAL;
489 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
490 return 0;
493 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
495 if (dmxdevfilter->state < DMXDEV_STATE_SET)
496 return 0;
498 dmxdevfilter->type = DMXDEV_TYPE_NONE;
499 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
500 return 0;
503 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
505 struct dmxdev *dmxdev = filter->dev;
506 void *mem;
507 int ret, i;
509 if (filter->state < DMXDEV_STATE_SET)
510 return -EINVAL;
512 if (filter->state >= DMXDEV_STATE_GO)
513 dvb_dmxdev_filter_stop(filter);
515 if (!filter->buffer.data) {
516 mem = vmalloc(filter->buffer.size);
517 if (!mem)
518 return -ENOMEM;
519 spin_lock_irq(&filter->dev->lock);
520 filter->buffer.data = mem;
521 spin_unlock_irq(&filter->dev->lock);
524 dvb_ringbuffer_flush(&filter->buffer);
526 switch (filter->type) {
527 case DMXDEV_TYPE_SEC:
529 struct dmx_sct_filter_params *para = &filter->params.sec;
530 struct dmx_section_filter **secfilter = &filter->filter.sec;
531 struct dmx_section_feed **secfeed = &filter->feed.sec;
533 *secfilter = NULL;
534 *secfeed = NULL;
537 /* find active filter/feed with same PID */
538 for (i = 0; i < dmxdev->filternum; i++) {
539 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
540 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
541 dmxdev->filter[i].params.sec.pid == para->pid) {
542 *secfeed = dmxdev->filter[i].feed.sec;
543 break;
547 /* if no feed found, try to allocate new one */
548 if (!*secfeed) {
549 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
550 secfeed,
551 dvb_dmxdev_section_callback);
552 if (ret < 0) {
553 printk("DVB (%s): could not alloc feed\n",
554 __FUNCTION__);
555 return ret;
558 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
559 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
560 if (ret < 0) {
561 printk("DVB (%s): could not set feed\n",
562 __FUNCTION__);
563 dvb_dmxdev_feed_restart(filter);
564 return ret;
566 } else {
567 dvb_dmxdev_feed_stop(filter);
570 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
571 if (ret < 0) {
572 dvb_dmxdev_feed_restart(filter);
573 filter->feed.sec->start_filtering(*secfeed);
574 dprintk("could not get filter\n");
575 return ret;
578 (*secfilter)->priv = filter;
580 memcpy(&((*secfilter)->filter_value[3]),
581 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
582 memcpy(&(*secfilter)->filter_mask[3],
583 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
584 memcpy(&(*secfilter)->filter_mode[3],
585 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
587 (*secfilter)->filter_value[0] = para->filter.filter[0];
588 (*secfilter)->filter_mask[0] = para->filter.mask[0];
589 (*secfilter)->filter_mode[0] = para->filter.mode[0];
590 (*secfilter)->filter_mask[1] = 0;
591 (*secfilter)->filter_mask[2] = 0;
593 filter->todo = 0;
595 ret = filter->feed.sec->start_filtering(filter->feed.sec);
596 if (ret < 0)
597 return ret;
599 dvb_dmxdev_filter_timer(filter);
600 break;
602 case DMXDEV_TYPE_PES:
604 struct timespec timeout = { 0 };
605 struct dmx_pes_filter_params *para = &filter->params.pes;
606 dmx_output_t otype;
607 int ret;
608 int ts_type;
609 enum dmx_ts_pes ts_pes;
610 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
612 filter->feed.ts = NULL;
613 otype = para->output;
615 ts_pes = (enum dmx_ts_pes)para->pes_type;
617 if (ts_pes < DMX_PES_OTHER)
618 ts_type = TS_DECODER;
619 else
620 ts_type = 0;
622 if (otype == DMX_OUT_TS_TAP)
623 ts_type |= TS_PACKET;
625 if (otype == DMX_OUT_TAP)
626 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
628 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
629 tsfeed,
630 dvb_dmxdev_ts_callback);
631 if (ret < 0)
632 return ret;
634 (*tsfeed)->priv = filter;
636 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
637 32768, timeout);
638 if (ret < 0) {
639 dmxdev->demux->release_ts_feed(dmxdev->demux,
640 *tsfeed);
641 return ret;
644 ret = filter->feed.ts->start_filtering(filter->feed.ts);
645 if (ret < 0) {
646 dmxdev->demux->release_ts_feed(dmxdev->demux,
647 *tsfeed);
648 return ret;
651 break;
653 default:
654 return -EINVAL;
657 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
658 return 0;
661 static int dvb_demux_open(struct inode *inode, struct file *file)
663 struct dvb_device *dvbdev = file->private_data;
664 struct dmxdev *dmxdev = dvbdev->priv;
665 int i;
666 struct dmxdev_filter *dmxdevfilter;
668 if (!dmxdev->filter)
669 return -EINVAL;
671 if (mutex_lock_interruptible(&dmxdev->mutex))
672 return -ERESTARTSYS;
674 for (i = 0; i < dmxdev->filternum; i++)
675 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
676 break;
678 if (i == dmxdev->filternum) {
679 mutex_unlock(&dmxdev->mutex);
680 return -EMFILE;
683 dmxdevfilter = &dmxdev->filter[i];
684 mutex_init(&dmxdevfilter->mutex);
685 file->private_data = dmxdevfilter;
687 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
688 dmxdevfilter->type = DMXDEV_TYPE_NONE;
689 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
690 dmxdevfilter->feed.ts = NULL;
691 init_timer(&dmxdevfilter->timer);
693 dvbdev->users++;
695 mutex_unlock(&dmxdev->mutex);
696 return 0;
699 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
700 struct dmxdev_filter *dmxdevfilter)
702 mutex_lock(&dmxdev->mutex);
703 mutex_lock(&dmxdevfilter->mutex);
705 dvb_dmxdev_filter_stop(dmxdevfilter);
706 dvb_dmxdev_filter_reset(dmxdevfilter);
708 if (dmxdevfilter->buffer.data) {
709 void *mem = dmxdevfilter->buffer.data;
711 spin_lock_irq(&dmxdev->lock);
712 dmxdevfilter->buffer.data = NULL;
713 spin_unlock_irq(&dmxdev->lock);
714 vfree(mem);
717 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
718 wake_up(&dmxdevfilter->buffer.queue);
719 mutex_unlock(&dmxdevfilter->mutex);
720 mutex_unlock(&dmxdev->mutex);
721 return 0;
724 static inline void invert_mode(dmx_filter_t *filter)
726 int i;
728 for (i = 0; i < DMX_FILTER_SIZE; i++)
729 filter->mode[i] ^= 0xff;
732 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
733 struct dmxdev_filter *dmxdevfilter,
734 struct dmx_sct_filter_params *params)
736 dprintk("function : %s\n", __FUNCTION__);
738 dvb_dmxdev_filter_stop(dmxdevfilter);
740 dmxdevfilter->type = DMXDEV_TYPE_SEC;
741 memcpy(&dmxdevfilter->params.sec,
742 params, sizeof(struct dmx_sct_filter_params));
743 invert_mode(&dmxdevfilter->params.sec.filter);
744 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
746 if (params->flags & DMX_IMMEDIATE_START)
747 return dvb_dmxdev_filter_start(dmxdevfilter);
749 return 0;
752 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
753 struct dmxdev_filter *dmxdevfilter,
754 struct dmx_pes_filter_params *params)
756 dvb_dmxdev_filter_stop(dmxdevfilter);
758 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
759 return -EINVAL;
761 dmxdevfilter->type = DMXDEV_TYPE_PES;
762 memcpy(&dmxdevfilter->params, params,
763 sizeof(struct dmx_pes_filter_params));
765 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
767 if (params->flags & DMX_IMMEDIATE_START)
768 return dvb_dmxdev_filter_start(dmxdevfilter);
770 return 0;
773 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
774 struct file *file, char __user *buf,
775 size_t count, loff_t *ppos)
777 int result, hcount;
778 int done = 0;
780 if (dfil->todo <= 0) {
781 hcount = 3 + dfil->todo;
782 if (hcount > count)
783 hcount = count;
784 result = dvb_dmxdev_buffer_read(&dfil->buffer,
785 file->f_flags & O_NONBLOCK,
786 buf, hcount, ppos);
787 if (result < 0) {
788 dfil->todo = 0;
789 return result;
791 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
792 return -EFAULT;
793 buf += result;
794 done = result;
795 count -= result;
796 dfil->todo -= result;
797 if (dfil->todo > -3)
798 return done;
799 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
800 if (!count)
801 return done;
803 if (count > dfil->todo)
804 count = dfil->todo;
805 result = dvb_dmxdev_buffer_read(&dfil->buffer,
806 file->f_flags & O_NONBLOCK,
807 buf, count, ppos);
808 if (result < 0)
809 return result;
810 dfil->todo -= result;
811 return (result + done);
814 static ssize_t
815 dvb_demux_read(struct file *file, char __user *buf, size_t count,
816 loff_t *ppos)
818 struct dmxdev_filter *dmxdevfilter = file->private_data;
819 int ret;
821 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
822 return -ERESTARTSYS;
824 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
825 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
826 else
827 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
828 file->f_flags & O_NONBLOCK,
829 buf, count, ppos);
831 mutex_unlock(&dmxdevfilter->mutex);
832 return ret;
835 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
836 unsigned int cmd, void *parg)
838 struct dmxdev_filter *dmxdevfilter = file->private_data;
839 struct dmxdev *dmxdev = dmxdevfilter->dev;
840 unsigned long arg = (unsigned long)parg;
841 int ret = 0;
843 if (mutex_lock_interruptible(&dmxdev->mutex))
844 return -ERESTARTSYS;
846 switch (cmd) {
847 case DMX_START:
848 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
849 mutex_unlock(&dmxdev->mutex);
850 return -ERESTARTSYS;
852 if (dmxdevfilter->state < DMXDEV_STATE_SET)
853 ret = -EINVAL;
854 else
855 ret = dvb_dmxdev_filter_start(dmxdevfilter);
856 mutex_unlock(&dmxdevfilter->mutex);
857 break;
859 case DMX_STOP:
860 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861 mutex_unlock(&dmxdev->mutex);
862 return -ERESTARTSYS;
864 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
865 mutex_unlock(&dmxdevfilter->mutex);
866 break;
868 case DMX_SET_FILTER:
869 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
870 mutex_unlock(&dmxdev->mutex);
871 return -ERESTARTSYS;
873 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
874 mutex_unlock(&dmxdevfilter->mutex);
875 break;
877 case DMX_SET_PES_FILTER:
878 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
879 mutex_unlock(&dmxdev->mutex);
880 return -ERESTARTSYS;
882 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
883 mutex_unlock(&dmxdevfilter->mutex);
884 break;
886 case DMX_SET_BUFFER_SIZE:
887 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
888 mutex_unlock(&dmxdev->mutex);
889 return -ERESTARTSYS;
891 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
892 mutex_unlock(&dmxdevfilter->mutex);
893 break;
895 case DMX_GET_PES_PIDS:
896 if (!dmxdev->demux->get_pes_pids) {
897 ret = -EINVAL;
898 break;
900 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
901 break;
903 case DMX_GET_CAPS:
904 if (!dmxdev->demux->get_caps) {
905 ret = -EINVAL;
906 break;
908 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
909 break;
911 case DMX_SET_SOURCE:
912 if (!dmxdev->demux->set_source) {
913 ret = -EINVAL;
914 break;
916 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
917 break;
919 case DMX_GET_STC:
920 if (!dmxdev->demux->get_stc) {
921 ret = -EINVAL;
922 break;
924 ret = dmxdev->demux->get_stc(dmxdev->demux,
925 ((struct dmx_stc *)parg)->num,
926 &((struct dmx_stc *)parg)->stc,
927 &((struct dmx_stc *)parg)->base);
928 break;
930 default:
931 ret = -EINVAL;
932 break;
934 mutex_unlock(&dmxdev->mutex);
935 return ret;
938 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
939 unsigned int cmd, unsigned long arg)
941 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
944 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
946 struct dmxdev_filter *dmxdevfilter = file->private_data;
947 unsigned int mask = 0;
949 if (!dmxdevfilter)
950 return -EINVAL;
952 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
954 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
955 dmxdevfilter->state != DMXDEV_STATE_DONE &&
956 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
957 return 0;
959 if (dmxdevfilter->buffer.error)
960 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
962 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
963 mask |= (POLLIN | POLLRDNORM | POLLPRI);
965 return mask;
968 static int dvb_demux_release(struct inode *inode, struct file *file)
970 struct dmxdev_filter *dmxdevfilter = file->private_data;
971 struct dmxdev *dmxdev = dmxdevfilter->dev;
973 int ret;
975 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
977 mutex_lock(&dmxdev->mutex);
978 dmxdev->dvbdev->users--;
979 if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
980 fops_put(file->f_op);
981 file->f_op = NULL;
982 mutex_unlock(&dmxdev->mutex);
983 wake_up(&dmxdev->dvbdev->wait_queue);
984 } else
985 mutex_unlock(&dmxdev->mutex);
987 return ret;
990 static struct file_operations dvb_demux_fops = {
991 .owner = THIS_MODULE,
992 .read = dvb_demux_read,
993 .ioctl = dvb_demux_ioctl,
994 .open = dvb_demux_open,
995 .release = dvb_demux_release,
996 .poll = dvb_demux_poll,
999 static struct dvb_device dvbdev_demux = {
1000 .priv = NULL,
1001 .users = 1,
1002 .writers = 1,
1003 .fops = &dvb_demux_fops
1006 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1007 unsigned int cmd, void *parg)
1009 struct dvb_device *dvbdev = file->private_data;
1010 struct dmxdev *dmxdev = dvbdev->priv;
1011 int ret;
1013 if (mutex_lock_interruptible(&dmxdev->mutex))
1014 return -ERESTARTSYS;
1016 switch (cmd) {
1017 case DMX_SET_BUFFER_SIZE:
1018 // FIXME: implement
1019 ret = 0;
1020 break;
1022 default:
1023 ret = -EINVAL;
1024 break;
1026 mutex_unlock(&dmxdev->mutex);
1027 return ret;
1030 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1031 unsigned int cmd, unsigned long arg)
1033 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1036 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1038 struct dvb_device *dvbdev = file->private_data;
1039 struct dmxdev *dmxdev = dvbdev->priv;
1040 unsigned int mask = 0;
1042 dprintk("function : %s\n", __FUNCTION__);
1044 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1046 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1047 if (dmxdev->dvr_buffer.error)
1048 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1050 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1051 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1052 } else
1053 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1055 return mask;
1058 static struct file_operations dvb_dvr_fops = {
1059 .owner = THIS_MODULE,
1060 .read = dvb_dvr_read,
1061 .write = dvb_dvr_write,
1062 .ioctl = dvb_dvr_ioctl,
1063 .open = dvb_dvr_open,
1064 .release = dvb_dvr_release,
1065 .poll = dvb_dvr_poll,
1068 static struct dvb_device dvbdev_dvr = {
1069 .priv = NULL,
1070 .readers = 1,
1071 .users = 1,
1072 .fops = &dvb_dvr_fops
1075 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1077 int i;
1079 if (dmxdev->demux->open(dmxdev->demux) < 0)
1080 return -EUSERS;
1082 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1083 if (!dmxdev->filter)
1084 return -ENOMEM;
1086 mutex_init(&dmxdev->mutex);
1087 spin_lock_init(&dmxdev->lock);
1088 for (i = 0; i < dmxdev->filternum; i++) {
1089 dmxdev->filter[i].dev = dmxdev;
1090 dmxdev->filter[i].buffer.data = NULL;
1091 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1092 DMXDEV_STATE_FREE);
1095 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1096 DVB_DEVICE_DEMUX);
1097 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1098 dmxdev, DVB_DEVICE_DVR);
1100 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1102 return 0;
1105 EXPORT_SYMBOL(dvb_dmxdev_init);
1107 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1109 dmxdev->exit=1;
1110 if (dmxdev->dvbdev->users > 1) {
1111 wait_event(dmxdev->dvbdev->wait_queue,
1112 dmxdev->dvbdev->users==1);
1114 if (dmxdev->dvr_dvbdev->users > 1) {
1115 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1116 dmxdev->dvr_dvbdev->users==1);
1119 dvb_unregister_device(dmxdev->dvbdev);
1120 dvb_unregister_device(dmxdev->dvr_dvbdev);
1122 vfree(dmxdev->filter);
1123 dmxdev->filter = NULL;
1124 dmxdev->demux->close(dmxdev->demux);
1127 EXPORT_SYMBOL(dvb_dmxdev_release);