xtensa: support DMA buffers in high memory
[cris-mirror.git] / drivers / media / dvb-core / dmxdev.c
blob6d53af00190e396fda03bab661b56d4e4a1fc4a2
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.
19 #define pr_fmt(fmt) "dmxdev: " fmt
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/vmalloc.h>
25 #include <linux/module.h>
26 #include <linux/poll.h>
27 #include <linux/ioctl.h>
28 #include <linux/wait.h>
29 #include <linux/uaccess.h>
30 #include <media/dmxdev.h>
31 #include <media/dvb_vb2.h>
33 static int debug;
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
38 #define dprintk(fmt, arg...) do { \
39 if (debug) \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
41 __func__, ##arg); \
42 } while (0)
44 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
45 const u8 *src, size_t len)
47 ssize_t free;
49 if (!len)
50 return 0;
51 if (!buf->data)
52 return 0;
54 free = dvb_ringbuffer_free(buf);
55 if (len > free) {
56 dprintk("buffer overflow\n");
57 return -EOVERFLOW;
60 return dvb_ringbuffer_write(buf, src, len);
63 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
64 int non_blocking, char __user *buf,
65 size_t count, loff_t *ppos)
67 size_t todo;
68 ssize_t avail;
69 ssize_t ret = 0;
71 if (!src->data)
72 return 0;
74 if (src->error) {
75 ret = src->error;
76 dvb_ringbuffer_flush(src);
77 return ret;
80 for (todo = count; todo > 0; todo -= ret) {
81 if (non_blocking && dvb_ringbuffer_empty(src)) {
82 ret = -EWOULDBLOCK;
83 break;
86 ret = wait_event_interruptible(src->queue,
87 !dvb_ringbuffer_empty(src) ||
88 (src->error != 0));
89 if (ret < 0)
90 break;
92 if (src->error) {
93 ret = src->error;
94 dvb_ringbuffer_flush(src);
95 break;
98 avail = dvb_ringbuffer_avail(src);
99 if (avail > todo)
100 avail = todo;
102 ret = dvb_ringbuffer_read_user(src, buf, avail);
103 if (ret < 0)
104 break;
106 buf += ret;
109 return (count - todo) ? (count - todo) : ret;
112 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
114 struct list_head *head, *pos;
116 head = demux->get_frontends(demux);
117 if (!head)
118 return NULL;
119 list_for_each(pos, head)
120 if (DMX_FE_ENTRY(pos)->source == type)
121 return DMX_FE_ENTRY(pos);
123 return NULL;
126 static int dvb_dvr_open(struct inode *inode, struct file *file)
128 struct dvb_device *dvbdev = file->private_data;
129 struct dmxdev *dmxdev = dvbdev->priv;
130 struct dmx_frontend *front;
131 #ifndef DVB_MMAP
132 bool need_ringbuffer = false;
133 #else
134 const bool need_ringbuffer = true;
135 #endif
137 dprintk("%s\n", __func__);
139 if (mutex_lock_interruptible(&dmxdev->mutex))
140 return -ERESTARTSYS;
142 if (dmxdev->exit) {
143 mutex_unlock(&dmxdev->mutex);
144 return -ENODEV;
147 #ifndef DVB_MMAP
148 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
149 need_ringbuffer = true;
150 #else
151 if ((file->f_flags & O_ACCMODE) == O_RDWR) {
152 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
153 mutex_unlock(&dmxdev->mutex);
154 return -EOPNOTSUPP;
157 #endif
159 if (need_ringbuffer) {
160 void *mem;
162 if (!dvbdev->readers) {
163 mutex_unlock(&dmxdev->mutex);
164 return -EBUSY;
166 mem = vmalloc(DVR_BUFFER_SIZE);
167 if (!mem) {
168 mutex_unlock(&dmxdev->mutex);
169 return -ENOMEM;
171 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
172 dvb_vb2_init(&dmxdev->dvr_vb2_ctx, "dvr",
173 file->f_flags & O_NONBLOCK);
174 dvbdev->readers--;
177 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
178 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
180 if (!dmxdev->demux->write) {
181 mutex_unlock(&dmxdev->mutex);
182 return -EOPNOTSUPP;
185 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
187 if (!front) {
188 mutex_unlock(&dmxdev->mutex);
189 return -EINVAL;
191 dmxdev->demux->disconnect_frontend(dmxdev->demux);
192 dmxdev->demux->connect_frontend(dmxdev->demux, front);
194 dvbdev->users++;
195 mutex_unlock(&dmxdev->mutex);
196 return 0;
199 static int dvb_dvr_release(struct inode *inode, struct file *file)
201 struct dvb_device *dvbdev = file->private_data;
202 struct dmxdev *dmxdev = dvbdev->priv;
203 #ifndef DVB_MMAP
204 bool need_ringbuffer = false;
205 #else
206 const bool need_ringbuffer = true;
207 #endif
209 mutex_lock(&dmxdev->mutex);
211 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
212 dmxdev->demux->disconnect_frontend(dmxdev->demux);
213 dmxdev->demux->connect_frontend(dmxdev->demux,
214 dmxdev->dvr_orig_fe);
216 #ifndef DVB_MMAP
217 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
218 need_ringbuffer = true;
219 #endif
221 if (need_ringbuffer) {
222 if (dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
223 dvb_vb2_stream_off(&dmxdev->dvr_vb2_ctx);
224 dvb_vb2_release(&dmxdev->dvr_vb2_ctx);
225 dvbdev->readers++;
226 if (dmxdev->dvr_buffer.data) {
227 void *mem = dmxdev->dvr_buffer.data;
228 /*memory barrier*/
229 mb();
230 spin_lock_irq(&dmxdev->lock);
231 dmxdev->dvr_buffer.data = NULL;
232 spin_unlock_irq(&dmxdev->lock);
233 vfree(mem);
236 /* TODO */
237 dvbdev->users--;
238 if (dvbdev->users == 1 && dmxdev->exit == 1) {
239 mutex_unlock(&dmxdev->mutex);
240 wake_up(&dvbdev->wait_queue);
241 } else
242 mutex_unlock(&dmxdev->mutex);
244 return 0;
247 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
248 size_t count, loff_t *ppos)
250 struct dvb_device *dvbdev = file->private_data;
251 struct dmxdev *dmxdev = dvbdev->priv;
252 int ret;
254 if (!dmxdev->demux->write)
255 return -EOPNOTSUPP;
256 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
257 return -EINVAL;
258 if (mutex_lock_interruptible(&dmxdev->mutex))
259 return -ERESTARTSYS;
261 if (dmxdev->exit) {
262 mutex_unlock(&dmxdev->mutex);
263 return -ENODEV;
265 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
266 mutex_unlock(&dmxdev->mutex);
267 return ret;
270 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
271 loff_t *ppos)
273 struct dvb_device *dvbdev = file->private_data;
274 struct dmxdev *dmxdev = dvbdev->priv;
276 if (dmxdev->exit)
277 return -ENODEV;
279 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
280 file->f_flags & O_NONBLOCK,
281 buf, count, ppos);
284 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
285 unsigned long size)
287 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
288 void *newmem;
289 void *oldmem;
291 dprintk("%s\n", __func__);
293 if (buf->size == size)
294 return 0;
295 if (!size)
296 return -EINVAL;
298 newmem = vmalloc(size);
299 if (!newmem)
300 return -ENOMEM;
302 oldmem = buf->data;
304 spin_lock_irq(&dmxdev->lock);
305 buf->data = newmem;
306 buf->size = size;
308 /* reset and not flush in case the buffer shrinks */
309 dvb_ringbuffer_reset(buf);
310 spin_unlock_irq(&dmxdev->lock);
312 vfree(oldmem);
314 return 0;
317 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
318 *dmxdevfilter, int state)
320 spin_lock_irq(&dmxdevfilter->dev->lock);
321 dmxdevfilter->state = state;
322 spin_unlock_irq(&dmxdevfilter->dev->lock);
325 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
326 unsigned long size)
328 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
329 void *newmem;
330 void *oldmem;
332 if (buf->size == size)
333 return 0;
334 if (!size)
335 return -EINVAL;
336 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
337 return -EBUSY;
339 newmem = vmalloc(size);
340 if (!newmem)
341 return -ENOMEM;
343 oldmem = buf->data;
345 spin_lock_irq(&dmxdevfilter->dev->lock);
346 buf->data = newmem;
347 buf->size = size;
349 /* reset and not flush in case the buffer shrinks */
350 dvb_ringbuffer_reset(buf);
351 spin_unlock_irq(&dmxdevfilter->dev->lock);
353 vfree(oldmem);
355 return 0;
358 static void dvb_dmxdev_filter_timeout(struct timer_list *t)
360 struct dmxdev_filter *dmxdevfilter = from_timer(dmxdevfilter, t, timer);
362 dmxdevfilter->buffer.error = -ETIMEDOUT;
363 spin_lock_irq(&dmxdevfilter->dev->lock);
364 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
365 spin_unlock_irq(&dmxdevfilter->dev->lock);
366 wake_up(&dmxdevfilter->buffer.queue);
369 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
371 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
373 del_timer(&dmxdevfilter->timer);
374 if (para->timeout) {
375 dmxdevfilter->timer.expires =
376 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
377 add_timer(&dmxdevfilter->timer);
381 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
382 const u8 *buffer2, size_t buffer2_len,
383 struct dmx_section_filter *filter)
385 struct dmxdev_filter *dmxdevfilter = filter->priv;
386 int ret;
388 if (!dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx) &&
389 dmxdevfilter->buffer.error) {
390 wake_up(&dmxdevfilter->buffer.queue);
391 return 0;
393 spin_lock(&dmxdevfilter->dev->lock);
394 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
395 spin_unlock(&dmxdevfilter->dev->lock);
396 return 0;
398 del_timer(&dmxdevfilter->timer);
399 dprintk("section callback %*ph\n", 6, buffer1);
400 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx)) {
401 ret = dvb_vb2_fill_buffer(&dmxdevfilter->vb2_ctx,
402 buffer1, buffer1_len);
403 if (ret == buffer1_len)
404 ret = dvb_vb2_fill_buffer(&dmxdevfilter->vb2_ctx,
405 buffer2, buffer2_len);
406 } else {
407 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer,
408 buffer1, buffer1_len);
409 if (ret == buffer1_len) {
410 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer,
411 buffer2, buffer2_len);
414 if (ret < 0)
415 dmxdevfilter->buffer.error = ret;
416 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
417 dmxdevfilter->state = DMXDEV_STATE_DONE;
418 spin_unlock(&dmxdevfilter->dev->lock);
419 wake_up(&dmxdevfilter->buffer.queue);
420 return 0;
423 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
424 const u8 *buffer2, size_t buffer2_len,
425 struct dmx_ts_feed *feed)
427 struct dmxdev_filter *dmxdevfilter = feed->priv;
428 struct dvb_ringbuffer *buffer;
429 #ifdef DVB_MMAP
430 struct dvb_vb2_ctx *ctx;
431 #endif
432 int ret;
434 spin_lock(&dmxdevfilter->dev->lock);
435 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
436 spin_unlock(&dmxdevfilter->dev->lock);
437 return 0;
440 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP ||
441 dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) {
442 buffer = &dmxdevfilter->buffer;
443 #ifdef DVB_MMAP
444 ctx = &dmxdevfilter->vb2_ctx;
445 #endif
446 } else {
447 buffer = &dmxdevfilter->dev->dvr_buffer;
448 #ifdef DVB_MMAP
449 ctx = &dmxdevfilter->dev->dvr_vb2_ctx;
450 #endif
453 if (dvb_vb2_is_streaming(ctx)) {
454 ret = dvb_vb2_fill_buffer(ctx, buffer1, buffer1_len);
455 if (ret == buffer1_len)
456 ret = dvb_vb2_fill_buffer(ctx, buffer2, buffer2_len);
457 } else {
458 if (buffer->error) {
459 spin_unlock(&dmxdevfilter->dev->lock);
460 wake_up(&buffer->queue);
461 return 0;
463 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
464 if (ret == buffer1_len)
465 ret = dvb_dmxdev_buffer_write(buffer,
466 buffer2, buffer2_len);
468 if (ret < 0)
469 buffer->error = ret;
470 spin_unlock(&dmxdevfilter->dev->lock);
471 wake_up(&buffer->queue);
472 return 0;
475 /* stop feed but only mark the specified filter as stopped (state set) */
476 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
478 struct dmxdev_feed *feed;
480 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
482 switch (dmxdevfilter->type) {
483 case DMXDEV_TYPE_SEC:
484 del_timer(&dmxdevfilter->timer);
485 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
486 break;
487 case DMXDEV_TYPE_PES:
488 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
489 feed->ts->stop_filtering(feed->ts);
490 break;
491 default:
492 return -EINVAL;
494 return 0;
497 /* start feed associated with the specified filter */
498 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
500 struct dmxdev_feed *feed;
501 int ret;
503 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
505 switch (filter->type) {
506 case DMXDEV_TYPE_SEC:
507 return filter->feed.sec->start_filtering(filter->feed.sec);
508 case DMXDEV_TYPE_PES:
509 list_for_each_entry(feed, &filter->feed.ts, next) {
510 ret = feed->ts->start_filtering(feed->ts);
511 if (ret < 0) {
512 dvb_dmxdev_feed_stop(filter);
513 return ret;
516 break;
517 default:
518 return -EINVAL;
521 return 0;
524 /* restart section feed if it has filters left associated with it,
525 otherwise release the feed */
526 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
528 int i;
529 struct dmxdev *dmxdev = filter->dev;
530 u16 pid = filter->params.sec.pid;
532 for (i = 0; i < dmxdev->filternum; i++)
533 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
534 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
535 dmxdev->filter[i].params.sec.pid == pid) {
536 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
537 return 0;
540 filter->dev->demux->release_section_feed(dmxdev->demux,
541 filter->feed.sec);
543 return 0;
546 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
548 struct dmxdev_feed *feed;
549 struct dmx_demux *demux;
551 if (dmxdevfilter->state < DMXDEV_STATE_GO)
552 return 0;
554 switch (dmxdevfilter->type) {
555 case DMXDEV_TYPE_SEC:
556 if (!dmxdevfilter->feed.sec)
557 break;
558 dvb_dmxdev_feed_stop(dmxdevfilter);
559 if (dmxdevfilter->filter.sec)
560 dmxdevfilter->feed.sec->
561 release_filter(dmxdevfilter->feed.sec,
562 dmxdevfilter->filter.sec);
563 dvb_dmxdev_feed_restart(dmxdevfilter);
564 dmxdevfilter->feed.sec = NULL;
565 break;
566 case DMXDEV_TYPE_PES:
567 dvb_dmxdev_feed_stop(dmxdevfilter);
568 demux = dmxdevfilter->dev->demux;
569 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
570 demux->release_ts_feed(demux, feed->ts);
571 feed->ts = NULL;
573 break;
574 default:
575 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
576 return 0;
577 return -EINVAL;
580 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
581 return 0;
584 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
586 struct dmxdev_feed *feed, *tmp;
588 /* delete all PIDs */
589 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
590 list_del(&feed->next);
591 kfree(feed);
594 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
597 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
599 if (dmxdevfilter->state < DMXDEV_STATE_SET)
600 return 0;
602 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
603 dvb_dmxdev_delete_pids(dmxdevfilter);
605 dmxdevfilter->type = DMXDEV_TYPE_NONE;
606 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
607 return 0;
610 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
611 struct dmxdev_filter *filter,
612 struct dmxdev_feed *feed)
614 ktime_t timeout = 0;
615 struct dmx_pes_filter_params *para = &filter->params.pes;
616 enum dmx_output otype;
617 int ret;
618 int ts_type;
619 enum dmx_ts_pes ts_pes;
620 struct dmx_ts_feed *tsfeed;
622 feed->ts = NULL;
623 otype = para->output;
625 ts_pes = para->pes_type;
627 if (ts_pes < DMX_PES_OTHER)
628 ts_type = TS_DECODER;
629 else
630 ts_type = 0;
632 if (otype == DMX_OUT_TS_TAP)
633 ts_type |= TS_PACKET;
634 else if (otype == DMX_OUT_TSDEMUX_TAP)
635 ts_type |= TS_PACKET | TS_DEMUX;
636 else if (otype == DMX_OUT_TAP)
637 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
639 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
640 dvb_dmxdev_ts_callback);
641 if (ret < 0)
642 return ret;
644 tsfeed = feed->ts;
645 tsfeed->priv = filter;
647 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, timeout);
648 if (ret < 0) {
649 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
650 return ret;
653 ret = tsfeed->start_filtering(tsfeed);
654 if (ret < 0) {
655 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
656 return ret;
659 return 0;
662 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
664 struct dmxdev *dmxdev = filter->dev;
665 struct dmxdev_feed *feed;
666 void *mem;
667 int ret, i;
669 if (filter->state < DMXDEV_STATE_SET)
670 return -EINVAL;
672 if (filter->state >= DMXDEV_STATE_GO)
673 dvb_dmxdev_filter_stop(filter);
675 if (!filter->buffer.data) {
676 mem = vmalloc(filter->buffer.size);
677 if (!mem)
678 return -ENOMEM;
679 spin_lock_irq(&filter->dev->lock);
680 filter->buffer.data = mem;
681 spin_unlock_irq(&filter->dev->lock);
684 dvb_ringbuffer_flush(&filter->buffer);
686 switch (filter->type) {
687 case DMXDEV_TYPE_SEC:
689 struct dmx_sct_filter_params *para = &filter->params.sec;
690 struct dmx_section_filter **secfilter = &filter->filter.sec;
691 struct dmx_section_feed **secfeed = &filter->feed.sec;
693 *secfilter = NULL;
694 *secfeed = NULL;
697 /* find active filter/feed with same PID */
698 for (i = 0; i < dmxdev->filternum; i++) {
699 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
700 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
701 dmxdev->filter[i].params.sec.pid == para->pid) {
702 *secfeed = dmxdev->filter[i].feed.sec;
703 break;
707 /* if no feed found, try to allocate new one */
708 if (!*secfeed) {
709 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
710 secfeed,
711 dvb_dmxdev_section_callback);
712 if (ret < 0) {
713 pr_err("DVB (%s): could not alloc feed\n",
714 __func__);
715 return ret;
718 ret = (*secfeed)->set(*secfeed, para->pid,
719 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
720 if (ret < 0) {
721 pr_err("DVB (%s): could not set feed\n",
722 __func__);
723 dvb_dmxdev_feed_restart(filter);
724 return ret;
726 } else {
727 dvb_dmxdev_feed_stop(filter);
730 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
731 if (ret < 0) {
732 dvb_dmxdev_feed_restart(filter);
733 filter->feed.sec->start_filtering(*secfeed);
734 dprintk("could not get filter\n");
735 return ret;
738 (*secfilter)->priv = filter;
740 memcpy(&((*secfilter)->filter_value[3]),
741 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
742 memcpy(&(*secfilter)->filter_mask[3],
743 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
744 memcpy(&(*secfilter)->filter_mode[3],
745 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
747 (*secfilter)->filter_value[0] = para->filter.filter[0];
748 (*secfilter)->filter_mask[0] = para->filter.mask[0];
749 (*secfilter)->filter_mode[0] = para->filter.mode[0];
750 (*secfilter)->filter_mask[1] = 0;
751 (*secfilter)->filter_mask[2] = 0;
753 filter->todo = 0;
755 ret = filter->feed.sec->start_filtering(filter->feed.sec);
756 if (ret < 0)
757 return ret;
759 dvb_dmxdev_filter_timer(filter);
760 break;
762 case DMXDEV_TYPE_PES:
763 list_for_each_entry(feed, &filter->feed.ts, next) {
764 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
765 if (ret < 0) {
766 dvb_dmxdev_filter_stop(filter);
767 return ret;
770 break;
771 default:
772 return -EINVAL;
775 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
776 return 0;
779 static int dvb_demux_open(struct inode *inode, struct file *file)
781 struct dvb_device *dvbdev = file->private_data;
782 struct dmxdev *dmxdev = dvbdev->priv;
783 int i;
784 struct dmxdev_filter *dmxdevfilter;
786 if (!dmxdev->filter)
787 return -EINVAL;
789 if (mutex_lock_interruptible(&dmxdev->mutex))
790 return -ERESTARTSYS;
792 for (i = 0; i < dmxdev->filternum; i++)
793 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
794 break;
796 if (i == dmxdev->filternum) {
797 mutex_unlock(&dmxdev->mutex);
798 return -EMFILE;
801 dmxdevfilter = &dmxdev->filter[i];
802 mutex_init(&dmxdevfilter->mutex);
803 file->private_data = dmxdevfilter;
805 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
806 dvb_vb2_init(&dmxdevfilter->vb2_ctx, "demux_filter",
807 file->f_flags & O_NONBLOCK);
808 dmxdevfilter->type = DMXDEV_TYPE_NONE;
809 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
810 timer_setup(&dmxdevfilter->timer, dvb_dmxdev_filter_timeout, 0);
812 dvbdev->users++;
814 mutex_unlock(&dmxdev->mutex);
815 return 0;
818 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
819 struct dmxdev_filter *dmxdevfilter)
821 mutex_lock(&dmxdev->mutex);
822 mutex_lock(&dmxdevfilter->mutex);
823 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
824 dvb_vb2_stream_off(&dmxdevfilter->vb2_ctx);
825 dvb_vb2_release(&dmxdevfilter->vb2_ctx);
828 dvb_dmxdev_filter_stop(dmxdevfilter);
829 dvb_dmxdev_filter_reset(dmxdevfilter);
831 if (dmxdevfilter->buffer.data) {
832 void *mem = dmxdevfilter->buffer.data;
834 spin_lock_irq(&dmxdev->lock);
835 dmxdevfilter->buffer.data = NULL;
836 spin_unlock_irq(&dmxdev->lock);
837 vfree(mem);
840 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
841 wake_up(&dmxdevfilter->buffer.queue);
842 mutex_unlock(&dmxdevfilter->mutex);
843 mutex_unlock(&dmxdev->mutex);
844 return 0;
847 static inline void invert_mode(struct dmx_filter *filter)
849 int i;
851 for (i = 0; i < DMX_FILTER_SIZE; i++)
852 filter->mode[i] ^= 0xff;
855 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
856 struct dmxdev_filter *filter, u16 pid)
858 struct dmxdev_feed *feed;
860 if ((filter->type != DMXDEV_TYPE_PES) ||
861 (filter->state < DMXDEV_STATE_SET))
862 return -EINVAL;
864 /* only TS packet filters may have multiple PIDs */
865 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
866 (!list_empty(&filter->feed.ts)))
867 return -EINVAL;
869 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
870 if (feed == NULL)
871 return -ENOMEM;
873 feed->pid = pid;
874 list_add(&feed->next, &filter->feed.ts);
876 if (filter->state >= DMXDEV_STATE_GO)
877 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
879 return 0;
882 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
883 struct dmxdev_filter *filter, u16 pid)
885 struct dmxdev_feed *feed, *tmp;
887 if ((filter->type != DMXDEV_TYPE_PES) ||
888 (filter->state < DMXDEV_STATE_SET))
889 return -EINVAL;
891 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
892 if ((feed->pid == pid) && (feed->ts != NULL)) {
893 feed->ts->stop_filtering(feed->ts);
894 filter->dev->demux->release_ts_feed(filter->dev->demux,
895 feed->ts);
896 list_del(&feed->next);
897 kfree(feed);
901 return 0;
904 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
905 struct dmxdev_filter *dmxdevfilter,
906 struct dmx_sct_filter_params *params)
908 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
909 __func__, params->pid, params->flags, params->timeout);
911 dvb_dmxdev_filter_stop(dmxdevfilter);
913 dmxdevfilter->type = DMXDEV_TYPE_SEC;
914 memcpy(&dmxdevfilter->params.sec,
915 params, sizeof(struct dmx_sct_filter_params));
916 invert_mode(&dmxdevfilter->params.sec.filter);
917 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
919 if (params->flags & DMX_IMMEDIATE_START)
920 return dvb_dmxdev_filter_start(dmxdevfilter);
922 return 0;
925 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
926 struct dmxdev_filter *dmxdevfilter,
927 struct dmx_pes_filter_params *params)
929 int ret;
931 dvb_dmxdev_filter_stop(dmxdevfilter);
932 dvb_dmxdev_filter_reset(dmxdevfilter);
934 if ((unsigned int)params->pes_type > DMX_PES_OTHER)
935 return -EINVAL;
937 dmxdevfilter->type = DMXDEV_TYPE_PES;
938 memcpy(&dmxdevfilter->params, params,
939 sizeof(struct dmx_pes_filter_params));
940 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
942 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
944 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
945 dmxdevfilter->params.pes.pid);
946 if (ret < 0)
947 return ret;
949 if (params->flags & DMX_IMMEDIATE_START)
950 return dvb_dmxdev_filter_start(dmxdevfilter);
952 return 0;
955 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
956 struct file *file, char __user *buf,
957 size_t count, loff_t *ppos)
959 int result, hcount;
960 int done = 0;
962 if (dfil->todo <= 0) {
963 hcount = 3 + dfil->todo;
964 if (hcount > count)
965 hcount = count;
966 result = dvb_dmxdev_buffer_read(&dfil->buffer,
967 file->f_flags & O_NONBLOCK,
968 buf, hcount, ppos);
969 if (result < 0) {
970 dfil->todo = 0;
971 return result;
973 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
974 return -EFAULT;
975 buf += result;
976 done = result;
977 count -= result;
978 dfil->todo -= result;
979 if (dfil->todo > -3)
980 return done;
981 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
982 if (!count)
983 return done;
985 if (count > dfil->todo)
986 count = dfil->todo;
987 result = dvb_dmxdev_buffer_read(&dfil->buffer,
988 file->f_flags & O_NONBLOCK,
989 buf, count, ppos);
990 if (result < 0)
991 return result;
992 dfil->todo -= result;
993 return (result + done);
996 static ssize_t
997 dvb_demux_read(struct file *file, char __user *buf, size_t count,
998 loff_t *ppos)
1000 struct dmxdev_filter *dmxdevfilter = file->private_data;
1001 int ret;
1003 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
1004 return -ERESTARTSYS;
1006 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
1007 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
1008 else
1009 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
1010 file->f_flags & O_NONBLOCK,
1011 buf, count, ppos);
1013 mutex_unlock(&dmxdevfilter->mutex);
1014 return ret;
1017 static int dvb_demux_do_ioctl(struct file *file,
1018 unsigned int cmd, void *parg)
1020 struct dmxdev_filter *dmxdevfilter = file->private_data;
1021 struct dmxdev *dmxdev = dmxdevfilter->dev;
1022 unsigned long arg = (unsigned long)parg;
1023 int ret = 0;
1025 if (mutex_lock_interruptible(&dmxdev->mutex))
1026 return -ERESTARTSYS;
1028 switch (cmd) {
1029 case DMX_START:
1030 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1031 mutex_unlock(&dmxdev->mutex);
1032 return -ERESTARTSYS;
1034 if (dmxdevfilter->state < DMXDEV_STATE_SET)
1035 ret = -EINVAL;
1036 else
1037 ret = dvb_dmxdev_filter_start(dmxdevfilter);
1038 mutex_unlock(&dmxdevfilter->mutex);
1039 break;
1041 case DMX_STOP:
1042 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1043 mutex_unlock(&dmxdev->mutex);
1044 return -ERESTARTSYS;
1046 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
1047 mutex_unlock(&dmxdevfilter->mutex);
1048 break;
1050 case DMX_SET_FILTER:
1051 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1052 mutex_unlock(&dmxdev->mutex);
1053 return -ERESTARTSYS;
1055 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1056 mutex_unlock(&dmxdevfilter->mutex);
1057 break;
1059 case DMX_SET_PES_FILTER:
1060 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1061 mutex_unlock(&dmxdev->mutex);
1062 return -ERESTARTSYS;
1064 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1065 mutex_unlock(&dmxdevfilter->mutex);
1066 break;
1068 case DMX_SET_BUFFER_SIZE:
1069 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1070 mutex_unlock(&dmxdev->mutex);
1071 return -ERESTARTSYS;
1073 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1074 mutex_unlock(&dmxdevfilter->mutex);
1075 break;
1077 case DMX_GET_PES_PIDS:
1078 if (!dmxdev->demux->get_pes_pids) {
1079 ret = -EINVAL;
1080 break;
1082 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1083 break;
1085 case DMX_GET_STC:
1086 if (!dmxdev->demux->get_stc) {
1087 ret = -EINVAL;
1088 break;
1090 ret = dmxdev->demux->get_stc(dmxdev->demux,
1091 ((struct dmx_stc *)parg)->num,
1092 &((struct dmx_stc *)parg)->stc,
1093 &((struct dmx_stc *)parg)->base);
1094 break;
1096 case DMX_ADD_PID:
1097 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1098 ret = -ERESTARTSYS;
1099 break;
1101 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1102 mutex_unlock(&dmxdevfilter->mutex);
1103 break;
1105 case DMX_REMOVE_PID:
1106 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1107 ret = -ERESTARTSYS;
1108 break;
1110 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1111 mutex_unlock(&dmxdevfilter->mutex);
1112 break;
1114 #ifdef DVB_MMAP
1115 case DMX_REQBUFS:
1116 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1117 mutex_unlock(&dmxdev->mutex);
1118 return -ERESTARTSYS;
1120 ret = dvb_vb2_reqbufs(&dmxdevfilter->vb2_ctx, parg);
1121 mutex_unlock(&dmxdevfilter->mutex);
1122 break;
1124 case DMX_QUERYBUF:
1125 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1126 mutex_unlock(&dmxdev->mutex);
1127 return -ERESTARTSYS;
1129 ret = dvb_vb2_querybuf(&dmxdevfilter->vb2_ctx, parg);
1130 mutex_unlock(&dmxdevfilter->mutex);
1131 break;
1133 case DMX_EXPBUF:
1134 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1135 mutex_unlock(&dmxdev->mutex);
1136 return -ERESTARTSYS;
1138 ret = dvb_vb2_expbuf(&dmxdevfilter->vb2_ctx, parg);
1139 mutex_unlock(&dmxdevfilter->mutex);
1140 break;
1142 case DMX_QBUF:
1143 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1144 mutex_unlock(&dmxdev->mutex);
1145 return -ERESTARTSYS;
1147 ret = dvb_vb2_qbuf(&dmxdevfilter->vb2_ctx, parg);
1148 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1149 ret = dvb_vb2_stream_on(&dmxdevfilter->vb2_ctx);
1150 mutex_unlock(&dmxdevfilter->mutex);
1151 break;
1153 case DMX_DQBUF:
1154 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1155 mutex_unlock(&dmxdev->mutex);
1156 return -ERESTARTSYS;
1158 ret = dvb_vb2_dqbuf(&dmxdevfilter->vb2_ctx, parg);
1159 mutex_unlock(&dmxdevfilter->mutex);
1160 break;
1161 #endif
1162 default:
1163 ret = -EINVAL;
1164 break;
1166 mutex_unlock(&dmxdev->mutex);
1167 return ret;
1170 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1171 unsigned long arg)
1173 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1176 static __poll_t dvb_demux_poll(struct file *file, poll_table *wait)
1178 struct dmxdev_filter *dmxdevfilter = file->private_data;
1179 __poll_t mask = 0;
1181 if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
1182 return EPOLLERR;
1183 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1184 return dvb_vb2_poll(&dmxdevfilter->vb2_ctx, file, wait);
1186 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1188 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1189 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1190 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1191 return 0;
1193 if (dmxdevfilter->buffer.error)
1194 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1196 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1197 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1199 return mask;
1202 #ifdef DVB_MMAP
1203 static int dvb_demux_mmap(struct file *file, struct vm_area_struct *vma)
1205 struct dmxdev_filter *dmxdevfilter = file->private_data;
1206 struct dmxdev *dmxdev = dmxdevfilter->dev;
1207 int ret;
1209 if (mutex_lock_interruptible(&dmxdev->mutex))
1210 return -ERESTARTSYS;
1212 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1213 mutex_unlock(&dmxdev->mutex);
1214 return -ERESTARTSYS;
1216 ret = dvb_vb2_mmap(&dmxdevfilter->vb2_ctx, vma);
1218 mutex_unlock(&dmxdevfilter->mutex);
1219 mutex_unlock(&dmxdev->mutex);
1221 return ret;
1223 #endif
1225 static int dvb_demux_release(struct inode *inode, struct file *file)
1227 struct dmxdev_filter *dmxdevfilter = file->private_data;
1228 struct dmxdev *dmxdev = dmxdevfilter->dev;
1229 int ret;
1231 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1233 mutex_lock(&dmxdev->mutex);
1234 dmxdev->dvbdev->users--;
1235 if (dmxdev->dvbdev->users == 1 && dmxdev->exit == 1) {
1236 mutex_unlock(&dmxdev->mutex);
1237 wake_up(&dmxdev->dvbdev->wait_queue);
1238 } else
1239 mutex_unlock(&dmxdev->mutex);
1241 return ret;
1244 static const struct file_operations dvb_demux_fops = {
1245 .owner = THIS_MODULE,
1246 .read = dvb_demux_read,
1247 .unlocked_ioctl = dvb_demux_ioctl,
1248 .open = dvb_demux_open,
1249 .release = dvb_demux_release,
1250 .poll = dvb_demux_poll,
1251 .llseek = default_llseek,
1252 #ifdef DVB_MMAP
1253 .mmap = dvb_demux_mmap,
1254 #endif
1257 static const struct dvb_device dvbdev_demux = {
1258 .priv = NULL,
1259 .users = 1,
1260 .writers = 1,
1261 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1262 .name = "dvb-demux",
1263 #endif
1264 .fops = &dvb_demux_fops
1267 static int dvb_dvr_do_ioctl(struct file *file,
1268 unsigned int cmd, void *parg)
1270 struct dvb_device *dvbdev = file->private_data;
1271 struct dmxdev *dmxdev = dvbdev->priv;
1272 unsigned long arg = (unsigned long)parg;
1273 int ret;
1275 if (mutex_lock_interruptible(&dmxdev->mutex))
1276 return -ERESTARTSYS;
1278 switch (cmd) {
1279 case DMX_SET_BUFFER_SIZE:
1280 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1281 break;
1283 #ifdef DVB_MMAP
1284 case DMX_REQBUFS:
1285 ret = dvb_vb2_reqbufs(&dmxdev->dvr_vb2_ctx, parg);
1286 break;
1288 case DMX_QUERYBUF:
1289 ret = dvb_vb2_querybuf(&dmxdev->dvr_vb2_ctx, parg);
1290 break;
1292 case DMX_EXPBUF:
1293 ret = dvb_vb2_expbuf(&dmxdev->dvr_vb2_ctx, parg);
1294 break;
1296 case DMX_QBUF:
1297 ret = dvb_vb2_qbuf(&dmxdev->dvr_vb2_ctx, parg);
1298 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1299 ret = dvb_vb2_stream_on(&dmxdev->dvr_vb2_ctx);
1300 break;
1302 case DMX_DQBUF:
1303 ret = dvb_vb2_dqbuf(&dmxdev->dvr_vb2_ctx, parg);
1304 break;
1305 #endif
1306 default:
1307 ret = -EINVAL;
1308 break;
1310 mutex_unlock(&dmxdev->mutex);
1311 return ret;
1314 static long dvb_dvr_ioctl(struct file *file,
1315 unsigned int cmd, unsigned long arg)
1317 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1320 static __poll_t dvb_dvr_poll(struct file *file, poll_table *wait)
1322 struct dvb_device *dvbdev = file->private_data;
1323 struct dmxdev *dmxdev = dvbdev->priv;
1324 __poll_t mask = 0;
1325 #ifndef DVB_MMAP
1326 bool need_ringbuffer = false;
1327 #else
1328 const bool need_ringbuffer = true;
1329 #endif
1331 dprintk("%s\n", __func__);
1333 if (dmxdev->exit)
1334 return EPOLLERR;
1335 if (dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1336 return dvb_vb2_poll(&dmxdev->dvr_vb2_ctx, file, wait);
1338 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1340 #ifndef DVB_MMAP
1341 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
1342 need_ringbuffer = true;
1343 #endif
1344 if (need_ringbuffer) {
1345 if (dmxdev->dvr_buffer.error)
1346 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1348 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1349 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1350 } else
1351 mask |= (EPOLLOUT | EPOLLWRNORM | EPOLLPRI);
1353 return mask;
1356 #ifdef DVB_MMAP
1357 static int dvb_dvr_mmap(struct file *file, struct vm_area_struct *vma)
1359 struct dvb_device *dvbdev = file->private_data;
1360 struct dmxdev *dmxdev = dvbdev->priv;
1361 int ret;
1363 if (dmxdev->exit)
1364 return -ENODEV;
1366 if (mutex_lock_interruptible(&dmxdev->mutex))
1367 return -ERESTARTSYS;
1369 ret = dvb_vb2_mmap(&dmxdev->dvr_vb2_ctx, vma);
1370 mutex_unlock(&dmxdev->mutex);
1371 return ret;
1373 #endif
1375 static const struct file_operations dvb_dvr_fops = {
1376 .owner = THIS_MODULE,
1377 .read = dvb_dvr_read,
1378 .write = dvb_dvr_write,
1379 .unlocked_ioctl = dvb_dvr_ioctl,
1380 .open = dvb_dvr_open,
1381 .release = dvb_dvr_release,
1382 .poll = dvb_dvr_poll,
1383 .llseek = default_llseek,
1384 #ifdef DVB_MMAP
1385 .mmap = dvb_dvr_mmap,
1386 #endif
1389 static const struct dvb_device dvbdev_dvr = {
1390 .priv = NULL,
1391 .readers = 1,
1392 .users = 1,
1393 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1394 .name = "dvb-dvr",
1395 #endif
1396 .fops = &dvb_dvr_fops
1398 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1400 int i;
1402 if (dmxdev->demux->open(dmxdev->demux) < 0)
1403 return -EUSERS;
1405 dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1406 if (!dmxdev->filter)
1407 return -ENOMEM;
1409 mutex_init(&dmxdev->mutex);
1410 spin_lock_init(&dmxdev->lock);
1411 for (i = 0; i < dmxdev->filternum; i++) {
1412 dmxdev->filter[i].dev = dmxdev;
1413 dmxdev->filter[i].buffer.data = NULL;
1414 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1415 DMXDEV_STATE_FREE);
1418 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1419 DVB_DEVICE_DEMUX, dmxdev->filternum);
1420 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1421 dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
1423 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1425 return 0;
1428 EXPORT_SYMBOL(dvb_dmxdev_init);
1430 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1432 dmxdev->exit = 1;
1433 if (dmxdev->dvbdev->users > 1) {
1434 wait_event(dmxdev->dvbdev->wait_queue,
1435 dmxdev->dvbdev->users == 1);
1437 if (dmxdev->dvr_dvbdev->users > 1) {
1438 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1439 dmxdev->dvr_dvbdev->users == 1);
1442 dvb_unregister_device(dmxdev->dvbdev);
1443 dvb_unregister_device(dmxdev->dvr_dvbdev);
1445 vfree(dmxdev->filter);
1446 dmxdev->filter = NULL;
1447 dmxdev->demux->close(dmxdev->demux);
1450 EXPORT_SYMBOL(dvb_dmxdev_release);