[PATCH] W1: possible cleanups
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob04578df3f24910dd25fa8bbea25b90344cb4c1ed
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/sched.h>
29 #include <linux/poll.h>
30 #include <linux/ioctl.h>
31 #include <linux/wait.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.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 if (debug) printk
43 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
44 const u8 *src, size_t len)
46 ssize_t free;
48 if (!len)
49 return 0;
50 if (!buf->data)
51 return 0;
53 free = dvb_ringbuffer_free(buf);
54 if (len > free) {
55 dprintk("dmxdev: buffer overflow\n");
56 return -EOVERFLOW;
59 return dvb_ringbuffer_write(buf, src, len);
62 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
63 int non_blocking, char __user *buf,
64 size_t count, loff_t *ppos)
66 size_t todo;
67 ssize_t avail;
68 ssize_t ret = 0;
70 if (!src->data)
71 return 0;
73 if (src->error) {
74 ret = src->error;
75 dvb_ringbuffer_flush(src);
76 return ret;
79 for (todo = count; todo > 0; todo -= ret) {
80 if (non_blocking && dvb_ringbuffer_empty(src)) {
81 ret = -EWOULDBLOCK;
82 break;
85 ret = wait_event_interruptible(src->queue,
86 !dvb_ringbuffer_empty(src) ||
87 (src->error != 0));
88 if (ret < 0)
89 break;
91 if (src->error) {
92 ret = src->error;
93 dvb_ringbuffer_flush(src);
94 break;
97 avail = dvb_ringbuffer_avail(src);
98 if (avail > todo)
99 avail = todo;
101 ret = dvb_ringbuffer_read(src, buf, avail, 1);
102 if (ret < 0)
103 break;
105 buf += ret;
108 return (count - todo) ? (count - todo) : ret;
111 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
113 struct list_head *head, *pos;
115 head = demux->get_frontends(demux);
116 if (!head)
117 return NULL;
118 list_for_each(pos, head)
119 if (DMX_FE_ENTRY(pos)->source == type)
120 return DMX_FE_ENTRY(pos);
122 return NULL;
125 static int dvb_dvr_open(struct inode *inode, struct file *file)
127 struct dvb_device *dvbdev = file->private_data;
128 struct dmxdev *dmxdev = dvbdev->priv;
129 struct dmx_frontend *front;
131 dprintk("function : %s\n", __FUNCTION__);
133 if (mutex_lock_interruptible(&dmxdev->mutex))
134 return -ERESTARTSYS;
136 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
137 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
138 mutex_unlock(&dmxdev->mutex);
139 return -EOPNOTSUPP;
143 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
144 void *mem;
145 if (!dvbdev->readers) {
146 mutex_unlock(&dmxdev->mutex);
147 return -EBUSY;
149 mem = vmalloc(DVR_BUFFER_SIZE);
150 if (!mem) {
151 mutex_unlock(&dmxdev->mutex);
152 return -ENOMEM;
154 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
155 dvbdev->readers--;
158 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
159 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
161 if (!dmxdev->demux->write) {
162 mutex_unlock(&dmxdev->mutex);
163 return -EOPNOTSUPP;
166 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
168 if (!front) {
169 mutex_unlock(&dmxdev->mutex);
170 return -EINVAL;
172 dmxdev->demux->disconnect_frontend(dmxdev->demux);
173 dmxdev->demux->connect_frontend(dmxdev->demux, front);
175 mutex_unlock(&dmxdev->mutex);
176 return 0;
179 static int dvb_dvr_release(struct inode *inode, struct file *file)
181 struct dvb_device *dvbdev = file->private_data;
182 struct dmxdev *dmxdev = dvbdev->priv;
184 if (mutex_lock_interruptible(&dmxdev->mutex))
185 return -ERESTARTSYS;
187 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
188 dmxdev->demux->disconnect_frontend(dmxdev->demux);
189 dmxdev->demux->connect_frontend(dmxdev->demux,
190 dmxdev->dvr_orig_fe);
192 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
193 dvbdev->readers++;
194 if (dmxdev->dvr_buffer.data) {
195 void *mem = dmxdev->dvr_buffer.data;
196 mb();
197 spin_lock_irq(&dmxdev->lock);
198 dmxdev->dvr_buffer.data = NULL;
199 spin_unlock_irq(&dmxdev->lock);
200 vfree(mem);
203 mutex_unlock(&dmxdev->mutex);
204 return 0;
207 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
208 size_t count, loff_t *ppos)
210 struct dvb_device *dvbdev = file->private_data;
211 struct dmxdev *dmxdev = dvbdev->priv;
212 int ret;
214 if (!dmxdev->demux->write)
215 return -EOPNOTSUPP;
216 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
217 return -EINVAL;
218 if (mutex_lock_interruptible(&dmxdev->mutex))
219 return -ERESTARTSYS;
220 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
221 mutex_unlock(&dmxdev->mutex);
222 return ret;
225 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
226 loff_t *ppos)
228 struct dvb_device *dvbdev = file->private_data;
229 struct dmxdev *dmxdev = dvbdev->priv;
230 int ret;
232 //mutex_lock(&dmxdev->mutex);
233 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
234 file->f_flags & O_NONBLOCK,
235 buf, count, ppos);
236 //mutex_unlock(&dmxdev->mutex);
237 return ret;
240 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
241 *dmxdevfilter, int state)
243 spin_lock_irq(&dmxdevfilter->dev->lock);
244 dmxdevfilter->state = state;
245 spin_unlock_irq(&dmxdevfilter->dev->lock);
248 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
249 unsigned long size)
251 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
252 void *mem;
254 if (buf->size == size)
255 return 0;
256 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
257 return -EBUSY;
258 spin_lock_irq(&dmxdevfilter->dev->lock);
259 mem = buf->data;
260 buf->data = NULL;
261 buf->size = size;
262 dvb_ringbuffer_flush(buf);
263 spin_unlock_irq(&dmxdevfilter->dev->lock);
264 vfree(mem);
266 if (buf->size) {
267 mem = vmalloc(dmxdevfilter->buffer.size);
268 if (!mem)
269 return -ENOMEM;
270 spin_lock_irq(&dmxdevfilter->dev->lock);
271 buf->data = mem;
272 spin_unlock_irq(&dmxdevfilter->dev->lock);
274 return 0;
277 static void dvb_dmxdev_filter_timeout(unsigned long data)
279 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
281 dmxdevfilter->buffer.error = -ETIMEDOUT;
282 spin_lock_irq(&dmxdevfilter->dev->lock);
283 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
284 spin_unlock_irq(&dmxdevfilter->dev->lock);
285 wake_up(&dmxdevfilter->buffer.queue);
288 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
290 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
292 del_timer(&dmxdevfilter->timer);
293 if (para->timeout) {
294 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
295 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
296 dmxdevfilter->timer.expires =
297 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
298 add_timer(&dmxdevfilter->timer);
302 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
303 const u8 *buffer2, size_t buffer2_len,
304 struct dmx_section_filter *filter,
305 enum dmx_success success)
307 struct dmxdev_filter *dmxdevfilter = filter->priv;
308 int ret;
310 if (dmxdevfilter->buffer.error) {
311 wake_up(&dmxdevfilter->buffer.queue);
312 return 0;
314 spin_lock(&dmxdevfilter->dev->lock);
315 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
316 spin_unlock(&dmxdevfilter->dev->lock);
317 return 0;
319 del_timer(&dmxdevfilter->timer);
320 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
321 buffer1[0], buffer1[1],
322 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
323 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
324 buffer1_len);
325 if (ret == buffer1_len) {
326 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
327 buffer2_len);
329 if (ret < 0) {
330 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
331 dmxdevfilter->buffer.error = ret;
333 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
334 dmxdevfilter->state = DMXDEV_STATE_DONE;
335 spin_unlock(&dmxdevfilter->dev->lock);
336 wake_up(&dmxdevfilter->buffer.queue);
337 return 0;
340 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
341 const u8 *buffer2, size_t buffer2_len,
342 struct dmx_ts_feed *feed,
343 enum dmx_success success)
345 struct dmxdev_filter *dmxdevfilter = feed->priv;
346 struct dvb_ringbuffer *buffer;
347 int ret;
349 spin_lock(&dmxdevfilter->dev->lock);
350 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
351 spin_unlock(&dmxdevfilter->dev->lock);
352 return 0;
355 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
356 buffer = &dmxdevfilter->buffer;
357 else
358 buffer = &dmxdevfilter->dev->dvr_buffer;
359 if (buffer->error) {
360 spin_unlock(&dmxdevfilter->dev->lock);
361 wake_up(&buffer->queue);
362 return 0;
364 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
365 if (ret == buffer1_len)
366 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
367 if (ret < 0) {
368 dvb_ringbuffer_flush(buffer);
369 buffer->error = ret;
371 spin_unlock(&dmxdevfilter->dev->lock);
372 wake_up(&buffer->queue);
373 return 0;
376 /* stop feed but only mark the specified filter as stopped (state set) */
377 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
379 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
381 switch (dmxdevfilter->type) {
382 case DMXDEV_TYPE_SEC:
383 del_timer(&dmxdevfilter->timer);
384 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
385 break;
386 case DMXDEV_TYPE_PES:
387 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
388 break;
389 default:
390 return -EINVAL;
392 return 0;
395 /* start feed associated with the specified filter */
396 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
398 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
400 switch (filter->type) {
401 case DMXDEV_TYPE_SEC:
402 return filter->feed.sec->start_filtering(filter->feed.sec);
403 case DMXDEV_TYPE_PES:
404 return filter->feed.ts->start_filtering(filter->feed.ts);
405 default:
406 return -EINVAL;
409 return 0;
412 /* restart section feed if it has filters left associated with it,
413 otherwise release the feed */
414 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
416 int i;
417 struct dmxdev *dmxdev = filter->dev;
418 u16 pid = filter->params.sec.pid;
420 for (i = 0; i < dmxdev->filternum; i++)
421 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
422 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
423 dmxdev->filter[i].params.sec.pid == pid) {
424 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
425 return 0;
428 filter->dev->demux->release_section_feed(dmxdev->demux,
429 filter->feed.sec);
431 return 0;
434 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
436 if (dmxdevfilter->state < DMXDEV_STATE_GO)
437 return 0;
439 switch (dmxdevfilter->type) {
440 case DMXDEV_TYPE_SEC:
441 if (!dmxdevfilter->feed.sec)
442 break;
443 dvb_dmxdev_feed_stop(dmxdevfilter);
444 if (dmxdevfilter->filter.sec)
445 dmxdevfilter->feed.sec->
446 release_filter(dmxdevfilter->feed.sec,
447 dmxdevfilter->filter.sec);
448 dvb_dmxdev_feed_restart(dmxdevfilter);
449 dmxdevfilter->feed.sec = NULL;
450 break;
451 case DMXDEV_TYPE_PES:
452 if (!dmxdevfilter->feed.ts)
453 break;
454 dvb_dmxdev_feed_stop(dmxdevfilter);
455 dmxdevfilter->dev->demux->
456 release_ts_feed(dmxdevfilter->dev->demux,
457 dmxdevfilter->feed.ts);
458 dmxdevfilter->feed.ts = NULL;
459 break;
460 default:
461 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
462 return 0;
463 return -EINVAL;
466 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
467 return 0;
470 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
472 if (dmxdevfilter->state < DMXDEV_STATE_SET)
473 return 0;
475 dmxdevfilter->type = DMXDEV_TYPE_NONE;
476 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
477 return 0;
480 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
482 struct dmxdev *dmxdev = filter->dev;
483 void *mem;
484 int ret, i;
486 if (filter->state < DMXDEV_STATE_SET)
487 return -EINVAL;
489 if (filter->state >= DMXDEV_STATE_GO)
490 dvb_dmxdev_filter_stop(filter);
492 if (!filter->buffer.data) {
493 mem = vmalloc(filter->buffer.size);
494 if (!mem)
495 return -ENOMEM;
496 spin_lock_irq(&filter->dev->lock);
497 filter->buffer.data = mem;
498 spin_unlock_irq(&filter->dev->lock);
501 dvb_ringbuffer_flush(&filter->buffer);
503 switch (filter->type) {
504 case DMXDEV_TYPE_SEC:
506 struct dmx_sct_filter_params *para = &filter->params.sec;
507 struct dmx_section_filter **secfilter = &filter->filter.sec;
508 struct dmx_section_feed **secfeed = &filter->feed.sec;
510 *secfilter = NULL;
511 *secfeed = NULL;
514 /* find active filter/feed with same PID */
515 for (i = 0; i < dmxdev->filternum; i++) {
516 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
517 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
518 dmxdev->filter[i].params.sec.pid == para->pid) {
519 *secfeed = dmxdev->filter[i].feed.sec;
520 break;
524 /* if no feed found, try to allocate new one */
525 if (!*secfeed) {
526 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
527 secfeed,
528 dvb_dmxdev_section_callback);
529 if (ret < 0) {
530 printk("DVB (%s): could not alloc feed\n",
531 __FUNCTION__);
532 return ret;
535 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
536 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
537 if (ret < 0) {
538 printk("DVB (%s): could not set feed\n",
539 __FUNCTION__);
540 dvb_dmxdev_feed_restart(filter);
541 return ret;
543 } else {
544 dvb_dmxdev_feed_stop(filter);
547 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
548 if (ret < 0) {
549 dvb_dmxdev_feed_restart(filter);
550 filter->feed.sec->start_filtering(*secfeed);
551 dprintk("could not get filter\n");
552 return ret;
555 (*secfilter)->priv = filter;
557 memcpy(&((*secfilter)->filter_value[3]),
558 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
559 memcpy(&(*secfilter)->filter_mask[3],
560 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
561 memcpy(&(*secfilter)->filter_mode[3],
562 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
564 (*secfilter)->filter_value[0] = para->filter.filter[0];
565 (*secfilter)->filter_mask[0] = para->filter.mask[0];
566 (*secfilter)->filter_mode[0] = para->filter.mode[0];
567 (*secfilter)->filter_mask[1] = 0;
568 (*secfilter)->filter_mask[2] = 0;
570 filter->todo = 0;
572 ret = filter->feed.sec->start_filtering(filter->feed.sec);
573 if (ret < 0)
574 return ret;
576 dvb_dmxdev_filter_timer(filter);
577 break;
579 case DMXDEV_TYPE_PES:
581 struct timespec timeout = { 0 };
582 struct dmx_pes_filter_params *para = &filter->params.pes;
583 dmx_output_t otype;
584 int ret;
585 int ts_type;
586 enum dmx_ts_pes ts_pes;
587 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
589 filter->feed.ts = NULL;
590 otype = para->output;
592 ts_pes = (enum dmx_ts_pes)para->pes_type;
594 if (ts_pes < DMX_PES_OTHER)
595 ts_type = TS_DECODER;
596 else
597 ts_type = 0;
599 if (otype == DMX_OUT_TS_TAP)
600 ts_type |= TS_PACKET;
602 if (otype == DMX_OUT_TAP)
603 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
605 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
606 tsfeed,
607 dvb_dmxdev_ts_callback);
608 if (ret < 0)
609 return ret;
611 (*tsfeed)->priv = filter;
613 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
614 32768, timeout);
615 if (ret < 0) {
616 dmxdev->demux->release_ts_feed(dmxdev->demux,
617 *tsfeed);
618 return ret;
621 ret = filter->feed.ts->start_filtering(filter->feed.ts);
622 if (ret < 0) {
623 dmxdev->demux->release_ts_feed(dmxdev->demux,
624 *tsfeed);
625 return ret;
628 break;
630 default:
631 return -EINVAL;
634 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
635 return 0;
638 static int dvb_demux_open(struct inode *inode, struct file *file)
640 struct dvb_device *dvbdev = file->private_data;
641 struct dmxdev *dmxdev = dvbdev->priv;
642 int i;
643 struct dmxdev_filter *dmxdevfilter;
645 if (!dmxdev->filter)
646 return -EINVAL;
648 if (mutex_lock_interruptible(&dmxdev->mutex))
649 return -ERESTARTSYS;
651 for (i = 0; i < dmxdev->filternum; i++)
652 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
653 break;
655 if (i == dmxdev->filternum) {
656 mutex_unlock(&dmxdev->mutex);
657 return -EMFILE;
660 dmxdevfilter = &dmxdev->filter[i];
661 mutex_init(&dmxdevfilter->mutex);
662 file->private_data = dmxdevfilter;
664 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
665 dmxdevfilter->type = DMXDEV_TYPE_NONE;
666 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
667 dmxdevfilter->feed.ts = NULL;
668 init_timer(&dmxdevfilter->timer);
670 mutex_unlock(&dmxdev->mutex);
671 return 0;
674 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
675 struct dmxdev_filter *dmxdevfilter)
677 if (mutex_lock_interruptible(&dmxdev->mutex))
678 return -ERESTARTSYS;
680 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
681 mutex_unlock(&dmxdev->mutex);
682 return -ERESTARTSYS;
685 dvb_dmxdev_filter_stop(dmxdevfilter);
686 dvb_dmxdev_filter_reset(dmxdevfilter);
688 if (dmxdevfilter->buffer.data) {
689 void *mem = dmxdevfilter->buffer.data;
691 spin_lock_irq(&dmxdev->lock);
692 dmxdevfilter->buffer.data = NULL;
693 spin_unlock_irq(&dmxdev->lock);
694 vfree(mem);
697 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
698 wake_up(&dmxdevfilter->buffer.queue);
699 mutex_unlock(&dmxdevfilter->mutex);
700 mutex_unlock(&dmxdev->mutex);
701 return 0;
704 static inline void invert_mode(dmx_filter_t *filter)
706 int i;
708 for (i = 0; i < DMX_FILTER_SIZE; i++)
709 filter->mode[i] ^= 0xff;
712 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
713 struct dmxdev_filter *dmxdevfilter,
714 struct dmx_sct_filter_params *params)
716 dprintk("function : %s\n", __FUNCTION__);
718 dvb_dmxdev_filter_stop(dmxdevfilter);
720 dmxdevfilter->type = DMXDEV_TYPE_SEC;
721 memcpy(&dmxdevfilter->params.sec,
722 params, sizeof(struct dmx_sct_filter_params));
723 invert_mode(&dmxdevfilter->params.sec.filter);
724 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
726 if (params->flags & DMX_IMMEDIATE_START)
727 return dvb_dmxdev_filter_start(dmxdevfilter);
729 return 0;
732 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
733 struct dmxdev_filter *dmxdevfilter,
734 struct dmx_pes_filter_params *params)
736 dvb_dmxdev_filter_stop(dmxdevfilter);
738 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
739 return -EINVAL;
741 dmxdevfilter->type = DMXDEV_TYPE_PES;
742 memcpy(&dmxdevfilter->params, params,
743 sizeof(struct dmx_pes_filter_params));
745 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
747 if (params->flags & DMX_IMMEDIATE_START)
748 return dvb_dmxdev_filter_start(dmxdevfilter);
750 return 0;
753 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
754 struct file *file, char __user *buf,
755 size_t count, loff_t *ppos)
757 int result, hcount;
758 int done = 0;
760 if (dfil->todo <= 0) {
761 hcount = 3 + dfil->todo;
762 if (hcount > count)
763 hcount = count;
764 result = dvb_dmxdev_buffer_read(&dfil->buffer,
765 file->f_flags & O_NONBLOCK,
766 buf, hcount, ppos);
767 if (result < 0) {
768 dfil->todo = 0;
769 return result;
771 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
772 return -EFAULT;
773 buf += result;
774 done = result;
775 count -= result;
776 dfil->todo -= result;
777 if (dfil->todo > -3)
778 return done;
779 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
780 if (!count)
781 return done;
783 if (count > dfil->todo)
784 count = dfil->todo;
785 result = dvb_dmxdev_buffer_read(&dfil->buffer,
786 file->f_flags & O_NONBLOCK,
787 buf, count, ppos);
788 if (result < 0)
789 return result;
790 dfil->todo -= result;
791 return (result + done);
794 static ssize_t
795 dvb_demux_read(struct file *file, char __user *buf, size_t count,
796 loff_t *ppos)
798 struct dmxdev_filter *dmxdevfilter = file->private_data;
799 int ret;
801 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
802 return -ERESTARTSYS;
804 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
805 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
806 else
807 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
808 file->f_flags & O_NONBLOCK,
809 buf, count, ppos);
811 mutex_unlock(&dmxdevfilter->mutex);
812 return ret;
815 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
816 unsigned int cmd, void *parg)
818 struct dmxdev_filter *dmxdevfilter = file->private_data;
819 struct dmxdev *dmxdev = dmxdevfilter->dev;
820 unsigned long arg = (unsigned long)parg;
821 int ret = 0;
823 if (mutex_lock_interruptible(&dmxdev->mutex))
824 return -ERESTARTSYS;
826 switch (cmd) {
827 case DMX_START:
828 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
829 mutex_unlock(&dmxdev->mutex);
830 return -ERESTARTSYS;
832 if (dmxdevfilter->state < DMXDEV_STATE_SET)
833 ret = -EINVAL;
834 else
835 ret = dvb_dmxdev_filter_start(dmxdevfilter);
836 mutex_unlock(&dmxdevfilter->mutex);
837 break;
839 case DMX_STOP:
840 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
841 mutex_unlock(&dmxdev->mutex);
842 return -ERESTARTSYS;
844 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
845 mutex_unlock(&dmxdevfilter->mutex);
846 break;
848 case DMX_SET_FILTER:
849 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
850 mutex_unlock(&dmxdev->mutex);
851 return -ERESTARTSYS;
853 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
854 mutex_unlock(&dmxdevfilter->mutex);
855 break;
857 case DMX_SET_PES_FILTER:
858 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
859 mutex_unlock(&dmxdev->mutex);
860 return -ERESTARTSYS;
862 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
863 mutex_unlock(&dmxdevfilter->mutex);
864 break;
866 case DMX_SET_BUFFER_SIZE:
867 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
868 mutex_unlock(&dmxdev->mutex);
869 return -ERESTARTSYS;
871 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
872 mutex_unlock(&dmxdevfilter->mutex);
873 break;
875 case DMX_GET_EVENT:
876 break;
878 case DMX_GET_PES_PIDS:
879 if (!dmxdev->demux->get_pes_pids) {
880 ret = -EINVAL;
881 break;
883 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
884 break;
886 case DMX_GET_CAPS:
887 if (!dmxdev->demux->get_caps) {
888 ret = -EINVAL;
889 break;
891 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
892 break;
894 case DMX_SET_SOURCE:
895 if (!dmxdev->demux->set_source) {
896 ret = -EINVAL;
897 break;
899 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
900 break;
902 case DMX_GET_STC:
903 if (!dmxdev->demux->get_stc) {
904 ret = -EINVAL;
905 break;
907 ret = dmxdev->demux->get_stc(dmxdev->demux,
908 ((struct dmx_stc *)parg)->num,
909 &((struct dmx_stc *)parg)->stc,
910 &((struct dmx_stc *)parg)->base);
911 break;
913 default:
914 ret = -EINVAL;
915 break;
917 mutex_unlock(&dmxdev->mutex);
918 return ret;
921 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
922 unsigned int cmd, unsigned long arg)
924 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
927 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
929 struct dmxdev_filter *dmxdevfilter = file->private_data;
930 unsigned int mask = 0;
932 if (!dmxdevfilter)
933 return -EINVAL;
935 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
937 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
938 dmxdevfilter->state != DMXDEV_STATE_DONE &&
939 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
940 return 0;
942 if (dmxdevfilter->buffer.error)
943 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
945 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
946 mask |= (POLLIN | POLLRDNORM | POLLPRI);
948 return mask;
951 static int dvb_demux_release(struct inode *inode, struct file *file)
953 struct dmxdev_filter *dmxdevfilter = file->private_data;
954 struct dmxdev *dmxdev = dmxdevfilter->dev;
956 return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
959 static struct file_operations dvb_demux_fops = {
960 .owner = THIS_MODULE,
961 .read = dvb_demux_read,
962 .ioctl = dvb_demux_ioctl,
963 .open = dvb_demux_open,
964 .release = dvb_demux_release,
965 .poll = dvb_demux_poll,
968 static struct dvb_device dvbdev_demux = {
969 .priv = NULL,
970 .users = 1,
971 .writers = 1,
972 .fops = &dvb_demux_fops
975 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
976 unsigned int cmd, void *parg)
978 struct dvb_device *dvbdev = file->private_data;
979 struct dmxdev *dmxdev = dvbdev->priv;
980 int ret;
982 if (mutex_lock_interruptible(&dmxdev->mutex))
983 return -ERESTARTSYS;
985 switch (cmd) {
986 case DMX_SET_BUFFER_SIZE:
987 // FIXME: implement
988 ret = 0;
989 break;
991 default:
992 ret = -EINVAL;
993 break;
995 mutex_unlock(&dmxdev->mutex);
996 return ret;
999 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1000 unsigned int cmd, unsigned long arg)
1002 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1005 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1007 struct dvb_device *dvbdev = file->private_data;
1008 struct dmxdev *dmxdev = dvbdev->priv;
1009 unsigned int mask = 0;
1011 dprintk("function : %s\n", __FUNCTION__);
1013 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1015 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1016 if (dmxdev->dvr_buffer.error)
1017 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1019 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1020 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1021 } else
1022 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1024 return mask;
1027 static struct file_operations dvb_dvr_fops = {
1028 .owner = THIS_MODULE,
1029 .read = dvb_dvr_read,
1030 .write = dvb_dvr_write,
1031 .ioctl = dvb_dvr_ioctl,
1032 .open = dvb_dvr_open,
1033 .release = dvb_dvr_release,
1034 .poll = dvb_dvr_poll,
1037 static struct dvb_device dvbdev_dvr = {
1038 .priv = NULL,
1039 .readers = 1,
1040 .fops = &dvb_dvr_fops
1043 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1045 int i;
1047 if (dmxdev->demux->open(dmxdev->demux) < 0)
1048 return -EUSERS;
1050 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1051 if (!dmxdev->filter)
1052 return -ENOMEM;
1054 mutex_init(&dmxdev->mutex);
1055 spin_lock_init(&dmxdev->lock);
1056 for (i = 0; i < dmxdev->filternum; i++) {
1057 dmxdev->filter[i].dev = dmxdev;
1058 dmxdev->filter[i].buffer.data = NULL;
1059 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1060 DMXDEV_STATE_FREE);
1063 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1064 DVB_DEVICE_DEMUX);
1065 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1066 dmxdev, DVB_DEVICE_DVR);
1068 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1070 return 0;
1073 EXPORT_SYMBOL(dvb_dmxdev_init);
1075 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1077 dvb_unregister_device(dmxdev->dvbdev);
1078 dvb_unregister_device(dmxdev->dvr_dvbdev);
1080 vfree(dmxdev->filter);
1081 dmxdev->filter = NULL;
1082 dmxdev->demux->close(dmxdev->demux);
1085 EXPORT_SYMBOL(dvb_dmxdev_release);