V4L/DVB (3501): Dmxdev: use dvb_ringbuffer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob09e96e9ddbdfc5a093af5315d8ef83cdbe9d5075
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 = vmalloc(DVR_BUFFER_SIZE);
145 if (!mem) {
146 mutex_unlock(&dmxdev->mutex);
147 return -ENOMEM;
149 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
152 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
153 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
155 if (!dmxdev->demux->write) {
156 mutex_unlock(&dmxdev->mutex);
157 return -EOPNOTSUPP;
160 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
162 if (!front) {
163 mutex_unlock(&dmxdev->mutex);
164 return -EINVAL;
166 dmxdev->demux->disconnect_frontend(dmxdev->demux);
167 dmxdev->demux->connect_frontend(dmxdev->demux, front);
169 mutex_unlock(&dmxdev->mutex);
170 return 0;
173 static int dvb_dvr_release(struct inode *inode, struct file *file)
175 struct dvb_device *dvbdev = file->private_data;
176 struct dmxdev *dmxdev = dvbdev->priv;
178 if (mutex_lock_interruptible(&dmxdev->mutex))
179 return -ERESTARTSYS;
181 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
182 dmxdev->demux->disconnect_frontend(dmxdev->demux);
183 dmxdev->demux->connect_frontend(dmxdev->demux,
184 dmxdev->dvr_orig_fe);
186 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
187 if (dmxdev->dvr_buffer.data) {
188 void *mem = dmxdev->dvr_buffer.data;
189 mb();
190 spin_lock_irq(&dmxdev->lock);
191 dmxdev->dvr_buffer.data = NULL;
192 spin_unlock_irq(&dmxdev->lock);
193 vfree(mem);
196 mutex_unlock(&dmxdev->mutex);
197 return 0;
200 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
201 size_t count, loff_t *ppos)
203 struct dvb_device *dvbdev = file->private_data;
204 struct dmxdev *dmxdev = dvbdev->priv;
205 int ret;
207 if (!dmxdev->demux->write)
208 return -EOPNOTSUPP;
209 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
210 return -EINVAL;
211 if (mutex_lock_interruptible(&dmxdev->mutex))
212 return -ERESTARTSYS;
213 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
214 mutex_unlock(&dmxdev->mutex);
215 return ret;
218 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
219 loff_t *ppos)
221 struct dvb_device *dvbdev = file->private_data;
222 struct dmxdev *dmxdev = dvbdev->priv;
223 int ret;
225 //mutex_lock(&dmxdev->mutex);
226 ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
227 file->f_flags & O_NONBLOCK,
228 buf, count, ppos);
229 //mutex_unlock(&dmxdev->mutex);
230 return ret;
233 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
234 *dmxdevfilter, int state)
236 spin_lock_irq(&dmxdevfilter->dev->lock);
237 dmxdevfilter->state = state;
238 spin_unlock_irq(&dmxdevfilter->dev->lock);
241 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
242 unsigned long size)
244 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
245 void *mem;
247 if (buf->size == size)
248 return 0;
249 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
250 return -EBUSY;
251 spin_lock_irq(&dmxdevfilter->dev->lock);
252 mem = buf->data;
253 buf->data = NULL;
254 buf->size = size;
255 dvb_ringbuffer_flush(buf);
256 spin_unlock_irq(&dmxdevfilter->dev->lock);
257 vfree(mem);
259 if (buf->size) {
260 mem = vmalloc(dmxdevfilter->buffer.size);
261 if (!mem)
262 return -ENOMEM;
263 spin_lock_irq(&dmxdevfilter->dev->lock);
264 buf->data = mem;
265 spin_unlock_irq(&dmxdevfilter->dev->lock);
267 return 0;
270 static void dvb_dmxdev_filter_timeout(unsigned long data)
272 struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
274 dmxdevfilter->buffer.error = -ETIMEDOUT;
275 spin_lock_irq(&dmxdevfilter->dev->lock);
276 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
277 spin_unlock_irq(&dmxdevfilter->dev->lock);
278 wake_up(&dmxdevfilter->buffer.queue);
281 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
283 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
285 del_timer(&dmxdevfilter->timer);
286 if (para->timeout) {
287 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
288 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
289 dmxdevfilter->timer.expires =
290 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
291 add_timer(&dmxdevfilter->timer);
295 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
296 const u8 *buffer2, size_t buffer2_len,
297 struct dmx_section_filter *filter,
298 enum dmx_success success)
300 struct dmxdev_filter *dmxdevfilter = filter->priv;
301 int ret;
303 if (dmxdevfilter->buffer.error) {
304 wake_up(&dmxdevfilter->buffer.queue);
305 return 0;
307 spin_lock(&dmxdevfilter->dev->lock);
308 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
309 spin_unlock(&dmxdevfilter->dev->lock);
310 return 0;
312 del_timer(&dmxdevfilter->timer);
313 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
314 buffer1[0], buffer1[1],
315 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
316 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
317 buffer1_len);
318 if (ret == buffer1_len) {
319 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
320 buffer2_len);
322 if (ret < 0) {
323 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
324 dmxdevfilter->buffer.error = ret;
326 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
327 dmxdevfilter->state = DMXDEV_STATE_DONE;
328 spin_unlock(&dmxdevfilter->dev->lock);
329 wake_up(&dmxdevfilter->buffer.queue);
330 return 0;
333 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
334 const u8 *buffer2, size_t buffer2_len,
335 struct dmx_ts_feed *feed,
336 enum dmx_success success)
338 struct dmxdev_filter *dmxdevfilter = feed->priv;
339 struct dvb_ringbuffer *buffer;
340 int ret;
342 spin_lock(&dmxdevfilter->dev->lock);
343 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
344 spin_unlock(&dmxdevfilter->dev->lock);
345 return 0;
348 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
349 buffer = &dmxdevfilter->buffer;
350 else
351 buffer = &dmxdevfilter->dev->dvr_buffer;
352 if (buffer->error) {
353 spin_unlock(&dmxdevfilter->dev->lock);
354 wake_up(&buffer->queue);
355 return 0;
357 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
358 if (ret == buffer1_len)
359 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
360 if (ret < 0) {
361 dvb_ringbuffer_flush(buffer);
362 buffer->error = ret;
364 spin_unlock(&dmxdevfilter->dev->lock);
365 wake_up(&buffer->queue);
366 return 0;
369 /* stop feed but only mark the specified filter as stopped (state set) */
370 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
372 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
374 switch (dmxdevfilter->type) {
375 case DMXDEV_TYPE_SEC:
376 del_timer(&dmxdevfilter->timer);
377 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
378 break;
379 case DMXDEV_TYPE_PES:
380 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
381 break;
382 default:
383 return -EINVAL;
385 return 0;
388 /* start feed associated with the specified filter */
389 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
391 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
393 switch (filter->type) {
394 case DMXDEV_TYPE_SEC:
395 return filter->feed.sec->start_filtering(filter->feed.sec);
396 case DMXDEV_TYPE_PES:
397 return filter->feed.ts->start_filtering(filter->feed.ts);
398 default:
399 return -EINVAL;
402 return 0;
405 /* restart section feed if it has filters left associated with it,
406 otherwise release the feed */
407 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
409 int i;
410 struct dmxdev *dmxdev = filter->dev;
411 u16 pid = filter->params.sec.pid;
413 for (i = 0; i < dmxdev->filternum; i++)
414 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
415 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
416 dmxdev->filter[i].params.sec.pid == pid) {
417 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
418 return 0;
421 filter->dev->demux->release_section_feed(dmxdev->demux,
422 filter->feed.sec);
424 return 0;
427 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
429 if (dmxdevfilter->state < DMXDEV_STATE_GO)
430 return 0;
432 switch (dmxdevfilter->type) {
433 case DMXDEV_TYPE_SEC:
434 if (!dmxdevfilter->feed.sec)
435 break;
436 dvb_dmxdev_feed_stop(dmxdevfilter);
437 if (dmxdevfilter->filter.sec)
438 dmxdevfilter->feed.sec->
439 release_filter(dmxdevfilter->feed.sec,
440 dmxdevfilter->filter.sec);
441 dvb_dmxdev_feed_restart(dmxdevfilter);
442 dmxdevfilter->feed.sec = NULL;
443 break;
444 case DMXDEV_TYPE_PES:
445 if (!dmxdevfilter->feed.ts)
446 break;
447 dvb_dmxdev_feed_stop(dmxdevfilter);
448 dmxdevfilter->dev->demux->
449 release_ts_feed(dmxdevfilter->dev->demux,
450 dmxdevfilter->feed.ts);
451 dmxdevfilter->feed.ts = NULL;
452 break;
453 default:
454 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
455 return 0;
456 return -EINVAL;
459 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
460 return 0;
463 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
465 if (dmxdevfilter->state < DMXDEV_STATE_SET)
466 return 0;
468 dmxdevfilter->type = DMXDEV_TYPE_NONE;
469 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
470 return 0;
473 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
475 struct dmxdev *dmxdev = filter->dev;
476 void *mem;
477 int ret, i;
479 if (filter->state < DMXDEV_STATE_SET)
480 return -EINVAL;
482 if (filter->state >= DMXDEV_STATE_GO)
483 dvb_dmxdev_filter_stop(filter);
485 if (!filter->buffer.data) {
486 mem = vmalloc(filter->buffer.size);
487 if (!mem)
488 return -ENOMEM;
489 spin_lock_irq(&filter->dev->lock);
490 filter->buffer.data = mem;
491 spin_unlock_irq(&filter->dev->lock);
494 dvb_ringbuffer_flush(&filter->buffer);
496 switch (filter->type) {
497 case DMXDEV_TYPE_SEC:
499 struct dmx_sct_filter_params *para = &filter->params.sec;
500 struct dmx_section_filter **secfilter = &filter->filter.sec;
501 struct dmx_section_feed **secfeed = &filter->feed.sec;
503 *secfilter = NULL;
504 *secfeed = NULL;
507 /* find active filter/feed with same PID */
508 for (i = 0; i < dmxdev->filternum; i++) {
509 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
510 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
511 dmxdev->filter[i].params.sec.pid == para->pid) {
512 *secfeed = dmxdev->filter[i].feed.sec;
513 break;
517 /* if no feed found, try to allocate new one */
518 if (!*secfeed) {
519 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
520 secfeed,
521 dvb_dmxdev_section_callback);
522 if (ret < 0) {
523 printk("DVB (%s): could not alloc feed\n",
524 __FUNCTION__);
525 return ret;
528 ret = (*secfeed)->set(*secfeed, para->pid, 32768,
529 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
530 if (ret < 0) {
531 printk("DVB (%s): could not set feed\n",
532 __FUNCTION__);
533 dvb_dmxdev_feed_restart(filter);
534 return ret;
536 } else {
537 dvb_dmxdev_feed_stop(filter);
540 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
541 if (ret < 0) {
542 dvb_dmxdev_feed_restart(filter);
543 filter->feed.sec->start_filtering(*secfeed);
544 dprintk("could not get filter\n");
545 return ret;
548 (*secfilter)->priv = filter;
550 memcpy(&((*secfilter)->filter_value[3]),
551 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
552 memcpy(&(*secfilter)->filter_mask[3],
553 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
554 memcpy(&(*secfilter)->filter_mode[3],
555 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
557 (*secfilter)->filter_value[0] = para->filter.filter[0];
558 (*secfilter)->filter_mask[0] = para->filter.mask[0];
559 (*secfilter)->filter_mode[0] = para->filter.mode[0];
560 (*secfilter)->filter_mask[1] = 0;
561 (*secfilter)->filter_mask[2] = 0;
563 filter->todo = 0;
565 ret = filter->feed.sec->start_filtering(filter->feed.sec);
566 if (ret < 0)
567 return ret;
569 dvb_dmxdev_filter_timer(filter);
570 break;
572 case DMXDEV_TYPE_PES:
574 struct timespec timeout = { 0 };
575 struct dmx_pes_filter_params *para = &filter->params.pes;
576 dmx_output_t otype;
577 int ret;
578 int ts_type;
579 enum dmx_ts_pes ts_pes;
580 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
582 filter->feed.ts = NULL;
583 otype = para->output;
585 ts_pes = (enum dmx_ts_pes)para->pes_type;
587 if (ts_pes < DMX_PES_OTHER)
588 ts_type = TS_DECODER;
589 else
590 ts_type = 0;
592 if (otype == DMX_OUT_TS_TAP)
593 ts_type |= TS_PACKET;
595 if (otype == DMX_OUT_TAP)
596 ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
598 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
599 tsfeed,
600 dvb_dmxdev_ts_callback);
601 if (ret < 0)
602 return ret;
604 (*tsfeed)->priv = filter;
606 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
607 32768, timeout);
608 if (ret < 0) {
609 dmxdev->demux->release_ts_feed(dmxdev->demux,
610 *tsfeed);
611 return ret;
614 ret = filter->feed.ts->start_filtering(filter->feed.ts);
615 if (ret < 0) {
616 dmxdev->demux->release_ts_feed(dmxdev->demux,
617 *tsfeed);
618 return ret;
621 break;
623 default:
624 return -EINVAL;
627 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
628 return 0;
631 static int dvb_demux_open(struct inode *inode, struct file *file)
633 struct dvb_device *dvbdev = file->private_data;
634 struct dmxdev *dmxdev = dvbdev->priv;
635 int i;
636 struct dmxdev_filter *dmxdevfilter;
638 if (!dmxdev->filter)
639 return -EINVAL;
641 if (mutex_lock_interruptible(&dmxdev->mutex))
642 return -ERESTARTSYS;
644 for (i = 0; i < dmxdev->filternum; i++)
645 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
646 break;
648 if (i == dmxdev->filternum) {
649 mutex_unlock(&dmxdev->mutex);
650 return -EMFILE;
653 dmxdevfilter = &dmxdev->filter[i];
654 mutex_init(&dmxdevfilter->mutex);
655 file->private_data = dmxdevfilter;
657 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
658 dmxdevfilter->type = DMXDEV_TYPE_NONE;
659 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
660 dmxdevfilter->feed.ts = NULL;
661 init_timer(&dmxdevfilter->timer);
663 mutex_unlock(&dmxdev->mutex);
664 return 0;
667 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
668 struct dmxdev_filter *dmxdevfilter)
670 if (mutex_lock_interruptible(&dmxdev->mutex))
671 return -ERESTARTSYS;
673 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
674 mutex_unlock(&dmxdev->mutex);
675 return -ERESTARTSYS;
678 dvb_dmxdev_filter_stop(dmxdevfilter);
679 dvb_dmxdev_filter_reset(dmxdevfilter);
681 if (dmxdevfilter->buffer.data) {
682 void *mem = dmxdevfilter->buffer.data;
684 spin_lock_irq(&dmxdev->lock);
685 dmxdevfilter->buffer.data = NULL;
686 spin_unlock_irq(&dmxdev->lock);
687 vfree(mem);
690 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
691 wake_up(&dmxdevfilter->buffer.queue);
692 mutex_unlock(&dmxdevfilter->mutex);
693 mutex_unlock(&dmxdev->mutex);
694 return 0;
697 static inline void invert_mode(dmx_filter_t *filter)
699 int i;
701 for (i = 0; i < DMX_FILTER_SIZE; i++)
702 filter->mode[i] ^= 0xff;
705 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
706 struct dmxdev_filter *dmxdevfilter,
707 struct dmx_sct_filter_params *params)
709 dprintk("function : %s\n", __FUNCTION__);
711 dvb_dmxdev_filter_stop(dmxdevfilter);
713 dmxdevfilter->type = DMXDEV_TYPE_SEC;
714 memcpy(&dmxdevfilter->params.sec,
715 params, sizeof(struct dmx_sct_filter_params));
716 invert_mode(&dmxdevfilter->params.sec.filter);
717 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
719 if (params->flags & DMX_IMMEDIATE_START)
720 return dvb_dmxdev_filter_start(dmxdevfilter);
722 return 0;
725 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
726 struct dmxdev_filter *dmxdevfilter,
727 struct dmx_pes_filter_params *params)
729 dvb_dmxdev_filter_stop(dmxdevfilter);
731 if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
732 return -EINVAL;
734 dmxdevfilter->type = DMXDEV_TYPE_PES;
735 memcpy(&dmxdevfilter->params, params,
736 sizeof(struct dmx_pes_filter_params));
738 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
740 if (params->flags & DMX_IMMEDIATE_START)
741 return dvb_dmxdev_filter_start(dmxdevfilter);
743 return 0;
746 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
747 struct file *file, char __user *buf,
748 size_t count, loff_t *ppos)
750 int result, hcount;
751 int done = 0;
753 if (dfil->todo <= 0) {
754 hcount = 3 + dfil->todo;
755 if (hcount > count)
756 hcount = count;
757 result = dvb_dmxdev_buffer_read(&dfil->buffer,
758 file->f_flags & O_NONBLOCK,
759 buf, hcount, ppos);
760 if (result < 0) {
761 dfil->todo = 0;
762 return result;
764 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
765 return -EFAULT;
766 buf += result;
767 done = result;
768 count -= result;
769 dfil->todo -= result;
770 if (dfil->todo > -3)
771 return done;
772 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
773 if (!count)
774 return done;
776 if (count > dfil->todo)
777 count = dfil->todo;
778 result = dvb_dmxdev_buffer_read(&dfil->buffer,
779 file->f_flags & O_NONBLOCK,
780 buf, count, ppos);
781 if (result < 0)
782 return result;
783 dfil->todo -= result;
784 return (result + done);
787 static ssize_t
788 dvb_demux_read(struct file *file, char __user *buf, size_t count,
789 loff_t *ppos)
791 struct dmxdev_filter *dmxdevfilter = file->private_data;
792 int ret;
794 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
795 return -ERESTARTSYS;
797 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
798 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
799 else
800 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
801 file->f_flags & O_NONBLOCK,
802 buf, count, ppos);
804 mutex_unlock(&dmxdevfilter->mutex);
805 return ret;
808 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
809 unsigned int cmd, void *parg)
811 struct dmxdev_filter *dmxdevfilter = file->private_data;
812 struct dmxdev *dmxdev = dmxdevfilter->dev;
813 unsigned long arg = (unsigned long)parg;
814 int ret = 0;
816 if (mutex_lock_interruptible(&dmxdev->mutex))
817 return -ERESTARTSYS;
819 switch (cmd) {
820 case DMX_START:
821 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
822 mutex_unlock(&dmxdev->mutex);
823 return -ERESTARTSYS;
825 if (dmxdevfilter->state < DMXDEV_STATE_SET)
826 ret = -EINVAL;
827 else
828 ret = dvb_dmxdev_filter_start(dmxdevfilter);
829 mutex_unlock(&dmxdevfilter->mutex);
830 break;
832 case DMX_STOP:
833 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
834 mutex_unlock(&dmxdev->mutex);
835 return -ERESTARTSYS;
837 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
838 mutex_unlock(&dmxdevfilter->mutex);
839 break;
841 case DMX_SET_FILTER:
842 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
843 mutex_unlock(&dmxdev->mutex);
844 return -ERESTARTSYS;
846 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
847 mutex_unlock(&dmxdevfilter->mutex);
848 break;
850 case DMX_SET_PES_FILTER:
851 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
852 mutex_unlock(&dmxdev->mutex);
853 return -ERESTARTSYS;
855 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
856 mutex_unlock(&dmxdevfilter->mutex);
857 break;
859 case DMX_SET_BUFFER_SIZE:
860 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861 mutex_unlock(&dmxdev->mutex);
862 return -ERESTARTSYS;
864 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
865 mutex_unlock(&dmxdevfilter->mutex);
866 break;
868 case DMX_GET_EVENT:
869 break;
871 case DMX_GET_PES_PIDS:
872 if (!dmxdev->demux->get_pes_pids) {
873 ret = -EINVAL;
874 break;
876 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
877 break;
879 case DMX_GET_CAPS:
880 if (!dmxdev->demux->get_caps) {
881 ret = -EINVAL;
882 break;
884 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
885 break;
887 case DMX_SET_SOURCE:
888 if (!dmxdev->demux->set_source) {
889 ret = -EINVAL;
890 break;
892 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
893 break;
895 case DMX_GET_STC:
896 if (!dmxdev->demux->get_stc) {
897 ret = -EINVAL;
898 break;
900 ret = dmxdev->demux->get_stc(dmxdev->demux,
901 ((struct dmx_stc *)parg)->num,
902 &((struct dmx_stc *)parg)->stc,
903 &((struct dmx_stc *)parg)->base);
904 break;
906 default:
907 ret = -EINVAL;
908 break;
910 mutex_unlock(&dmxdev->mutex);
911 return ret;
914 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
915 unsigned int cmd, unsigned long arg)
917 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
920 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
922 struct dmxdev_filter *dmxdevfilter = file->private_data;
923 unsigned int mask = 0;
925 if (!dmxdevfilter)
926 return -EINVAL;
928 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
930 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
931 dmxdevfilter->state != DMXDEV_STATE_DONE &&
932 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
933 return 0;
935 if (dmxdevfilter->buffer.error)
936 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
938 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
939 mask |= (POLLIN | POLLRDNORM | POLLPRI);
941 return mask;
944 static int dvb_demux_release(struct inode *inode, struct file *file)
946 struct dmxdev_filter *dmxdevfilter = file->private_data;
947 struct dmxdev *dmxdev = dmxdevfilter->dev;
949 return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
952 static struct file_operations dvb_demux_fops = {
953 .owner = THIS_MODULE,
954 .read = dvb_demux_read,
955 .ioctl = dvb_demux_ioctl,
956 .open = dvb_demux_open,
957 .release = dvb_demux_release,
958 .poll = dvb_demux_poll,
961 static struct dvb_device dvbdev_demux = {
962 .priv = NULL,
963 .users = 1,
964 .writers = 1,
965 .fops = &dvb_demux_fops
968 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
969 unsigned int cmd, void *parg)
971 struct dvb_device *dvbdev = file->private_data;
972 struct dmxdev *dmxdev = dvbdev->priv;
973 int ret;
975 if (mutex_lock_interruptible(&dmxdev->mutex))
976 return -ERESTARTSYS;
978 switch (cmd) {
979 case DMX_SET_BUFFER_SIZE:
980 // FIXME: implement
981 ret = 0;
982 break;
984 default:
985 ret = -EINVAL;
986 break;
988 mutex_unlock(&dmxdev->mutex);
989 return ret;
992 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
993 unsigned int cmd, unsigned long arg)
995 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
998 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1000 struct dvb_device *dvbdev = file->private_data;
1001 struct dmxdev *dmxdev = dvbdev->priv;
1002 unsigned int mask = 0;
1004 dprintk("function : %s\n", __FUNCTION__);
1006 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1008 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1009 if (dmxdev->dvr_buffer.error)
1010 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1012 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1013 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1014 } else
1015 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1017 return mask;
1020 static struct file_operations dvb_dvr_fops = {
1021 .owner = THIS_MODULE,
1022 .read = dvb_dvr_read,
1023 .write = dvb_dvr_write,
1024 .ioctl = dvb_dvr_ioctl,
1025 .open = dvb_dvr_open,
1026 .release = dvb_dvr_release,
1027 .poll = dvb_dvr_poll,
1030 static struct dvb_device dvbdev_dvr = {
1031 .priv = NULL,
1032 .users = 1,
1033 .writers = 1,
1034 .fops = &dvb_dvr_fops
1037 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1039 int i;
1041 if (dmxdev->demux->open(dmxdev->demux) < 0)
1042 return -EUSERS;
1044 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1045 if (!dmxdev->filter)
1046 return -ENOMEM;
1048 mutex_init(&dmxdev->mutex);
1049 spin_lock_init(&dmxdev->lock);
1050 for (i = 0; i < dmxdev->filternum; i++) {
1051 dmxdev->filter[i].dev = dmxdev;
1052 dmxdev->filter[i].buffer.data = NULL;
1053 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1054 DMXDEV_STATE_FREE);
1057 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1058 DVB_DEVICE_DEMUX);
1059 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1060 dmxdev, DVB_DEVICE_DVR);
1062 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1064 return 0;
1067 EXPORT_SYMBOL(dvb_dmxdev_init);
1069 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1071 dvb_unregister_device(dmxdev->dvbdev);
1072 dvb_unregister_device(dmxdev->dvr_dvbdev);
1074 vfree(dmxdev->filter);
1075 dmxdev->filter = NULL;
1076 dmxdev->demux->close(dmxdev->demux);
1079 EXPORT_SYMBOL(dvb_dmxdev_release);