treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / dvb-core / dmxdev.c
blobf14a872d126872c3fc695cbd3f8460c32d317b14
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 bool need_ringbuffer = false;
133 dprintk("%s\n", __func__);
135 if (mutex_lock_interruptible(&dmxdev->mutex))
136 return -ERESTARTSYS;
138 if (dmxdev->exit) {
139 mutex_unlock(&dmxdev->mutex);
140 return -ENODEV;
143 dmxdev->may_do_mmap = 0;
146 * The logic here is a little tricky due to the ifdef.
148 * The ringbuffer is used for both read and mmap.
150 * It is not needed, however, on two situations:
151 * - Write devices (access with O_WRONLY);
152 * - For duplex device nodes, opened with O_RDWR.
155 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
156 need_ringbuffer = true;
157 else if ((file->f_flags & O_ACCMODE) == O_RDWR) {
158 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
159 #ifdef CONFIG_DVB_MMAP
160 dmxdev->may_do_mmap = 1;
161 need_ringbuffer = true;
162 #else
163 mutex_unlock(&dmxdev->mutex);
164 return -EOPNOTSUPP;
165 #endif
169 if (need_ringbuffer) {
170 void *mem;
172 if (!dvbdev->readers) {
173 mutex_unlock(&dmxdev->mutex);
174 return -EBUSY;
176 mem = vmalloc(DVR_BUFFER_SIZE);
177 if (!mem) {
178 mutex_unlock(&dmxdev->mutex);
179 return -ENOMEM;
181 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
182 if (dmxdev->may_do_mmap)
183 dvb_vb2_init(&dmxdev->dvr_vb2_ctx, "dvr",
184 file->f_flags & O_NONBLOCK);
185 dvbdev->readers--;
188 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
189 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
191 if (!dmxdev->demux->write) {
192 mutex_unlock(&dmxdev->mutex);
193 return -EOPNOTSUPP;
196 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
198 if (!front) {
199 mutex_unlock(&dmxdev->mutex);
200 return -EINVAL;
202 dmxdev->demux->disconnect_frontend(dmxdev->demux);
203 dmxdev->demux->connect_frontend(dmxdev->demux, front);
205 dvbdev->users++;
206 mutex_unlock(&dmxdev->mutex);
207 return 0;
210 static int dvb_dvr_release(struct inode *inode, struct file *file)
212 struct dvb_device *dvbdev = file->private_data;
213 struct dmxdev *dmxdev = dvbdev->priv;
215 mutex_lock(&dmxdev->mutex);
217 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
218 dmxdev->demux->disconnect_frontend(dmxdev->demux);
219 dmxdev->demux->connect_frontend(dmxdev->demux,
220 dmxdev->dvr_orig_fe);
223 if (((file->f_flags & O_ACCMODE) == O_RDONLY) ||
224 dmxdev->may_do_mmap) {
225 if (dmxdev->may_do_mmap) {
226 if (dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
227 dvb_vb2_stream_off(&dmxdev->dvr_vb2_ctx);
228 dvb_vb2_release(&dmxdev->dvr_vb2_ctx);
230 dvbdev->readers++;
231 if (dmxdev->dvr_buffer.data) {
232 void *mem = dmxdev->dvr_buffer.data;
233 /*memory barrier*/
234 mb();
235 spin_lock_irq(&dmxdev->lock);
236 dmxdev->dvr_buffer.data = NULL;
237 spin_unlock_irq(&dmxdev->lock);
238 vfree(mem);
241 /* TODO */
242 dvbdev->users--;
243 if (dvbdev->users == 1 && dmxdev->exit == 1) {
244 mutex_unlock(&dmxdev->mutex);
245 wake_up(&dvbdev->wait_queue);
246 } else
247 mutex_unlock(&dmxdev->mutex);
249 return 0;
252 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
253 size_t count, loff_t *ppos)
255 struct dvb_device *dvbdev = file->private_data;
256 struct dmxdev *dmxdev = dvbdev->priv;
257 int ret;
259 if (!dmxdev->demux->write)
260 return -EOPNOTSUPP;
261 if ((file->f_flags & O_ACCMODE) != O_WRONLY)
262 return -EINVAL;
263 if (mutex_lock_interruptible(&dmxdev->mutex))
264 return -ERESTARTSYS;
266 if (dmxdev->exit) {
267 mutex_unlock(&dmxdev->mutex);
268 return -ENODEV;
270 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
271 mutex_unlock(&dmxdev->mutex);
272 return ret;
275 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
276 loff_t *ppos)
278 struct dvb_device *dvbdev = file->private_data;
279 struct dmxdev *dmxdev = dvbdev->priv;
281 if (dmxdev->exit)
282 return -ENODEV;
284 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
285 file->f_flags & O_NONBLOCK,
286 buf, count, ppos);
289 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
290 unsigned long size)
292 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
293 void *newmem;
294 void *oldmem;
296 dprintk("%s\n", __func__);
298 if (buf->size == size)
299 return 0;
300 if (!size)
301 return -EINVAL;
303 newmem = vmalloc(size);
304 if (!newmem)
305 return -ENOMEM;
307 oldmem = buf->data;
309 spin_lock_irq(&dmxdev->lock);
310 buf->data = newmem;
311 buf->size = size;
313 /* reset and not flush in case the buffer shrinks */
314 dvb_ringbuffer_reset(buf);
315 spin_unlock_irq(&dmxdev->lock);
317 vfree(oldmem);
319 return 0;
322 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
323 *dmxdevfilter, int state)
325 spin_lock_irq(&dmxdevfilter->dev->lock);
326 dmxdevfilter->state = state;
327 spin_unlock_irq(&dmxdevfilter->dev->lock);
330 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
331 unsigned long size)
333 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
334 void *newmem;
335 void *oldmem;
337 if (buf->size == size)
338 return 0;
339 if (!size)
340 return -EINVAL;
341 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
342 return -EBUSY;
344 newmem = vmalloc(size);
345 if (!newmem)
346 return -ENOMEM;
348 oldmem = buf->data;
350 spin_lock_irq(&dmxdevfilter->dev->lock);
351 buf->data = newmem;
352 buf->size = size;
354 /* reset and not flush in case the buffer shrinks */
355 dvb_ringbuffer_reset(buf);
356 spin_unlock_irq(&dmxdevfilter->dev->lock);
358 vfree(oldmem);
360 return 0;
363 static void dvb_dmxdev_filter_timeout(struct timer_list *t)
365 struct dmxdev_filter *dmxdevfilter = from_timer(dmxdevfilter, t, timer);
367 dmxdevfilter->buffer.error = -ETIMEDOUT;
368 spin_lock_irq(&dmxdevfilter->dev->lock);
369 dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
370 spin_unlock_irq(&dmxdevfilter->dev->lock);
371 wake_up(&dmxdevfilter->buffer.queue);
374 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
376 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
378 del_timer(&dmxdevfilter->timer);
379 if (para->timeout) {
380 dmxdevfilter->timer.expires =
381 jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
382 add_timer(&dmxdevfilter->timer);
386 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
387 const u8 *buffer2, size_t buffer2_len,
388 struct dmx_section_filter *filter,
389 u32 *buffer_flags)
391 struct dmxdev_filter *dmxdevfilter = filter->priv;
392 int ret;
394 if (!dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx) &&
395 dmxdevfilter->buffer.error) {
396 wake_up(&dmxdevfilter->buffer.queue);
397 return 0;
399 spin_lock(&dmxdevfilter->dev->lock);
400 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
401 spin_unlock(&dmxdevfilter->dev->lock);
402 return 0;
404 del_timer(&dmxdevfilter->timer);
405 dprintk("section callback %*ph\n", 6, buffer1);
406 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx)) {
407 ret = dvb_vb2_fill_buffer(&dmxdevfilter->vb2_ctx,
408 buffer1, buffer1_len,
409 buffer_flags);
410 if (ret == buffer1_len)
411 ret = dvb_vb2_fill_buffer(&dmxdevfilter->vb2_ctx,
412 buffer2, buffer2_len,
413 buffer_flags);
414 } else {
415 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer,
416 buffer1, buffer1_len);
417 if (ret == buffer1_len) {
418 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer,
419 buffer2, buffer2_len);
422 if (ret < 0)
423 dmxdevfilter->buffer.error = ret;
424 if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
425 dmxdevfilter->state = DMXDEV_STATE_DONE;
426 spin_unlock(&dmxdevfilter->dev->lock);
427 wake_up(&dmxdevfilter->buffer.queue);
428 return 0;
431 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
432 const u8 *buffer2, size_t buffer2_len,
433 struct dmx_ts_feed *feed,
434 u32 *buffer_flags)
436 struct dmxdev_filter *dmxdevfilter = feed->priv;
437 struct dvb_ringbuffer *buffer;
438 #ifdef CONFIG_DVB_MMAP
439 struct dvb_vb2_ctx *ctx;
440 #endif
441 int ret;
443 spin_lock(&dmxdevfilter->dev->lock);
444 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
445 spin_unlock(&dmxdevfilter->dev->lock);
446 return 0;
449 if (dmxdevfilter->params.pes.output == DMX_OUT_TAP ||
450 dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP) {
451 buffer = &dmxdevfilter->buffer;
452 #ifdef CONFIG_DVB_MMAP
453 ctx = &dmxdevfilter->vb2_ctx;
454 #endif
455 } else {
456 buffer = &dmxdevfilter->dev->dvr_buffer;
457 #ifdef CONFIG_DVB_MMAP
458 ctx = &dmxdevfilter->dev->dvr_vb2_ctx;
459 #endif
462 if (dvb_vb2_is_streaming(ctx)) {
463 ret = dvb_vb2_fill_buffer(ctx, buffer1, buffer1_len,
464 buffer_flags);
465 if (ret == buffer1_len)
466 ret = dvb_vb2_fill_buffer(ctx, buffer2, buffer2_len,
467 buffer_flags);
468 } else {
469 if (buffer->error) {
470 spin_unlock(&dmxdevfilter->dev->lock);
471 wake_up(&buffer->queue);
472 return 0;
474 ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
475 if (ret == buffer1_len)
476 ret = dvb_dmxdev_buffer_write(buffer,
477 buffer2, buffer2_len);
479 if (ret < 0)
480 buffer->error = ret;
481 spin_unlock(&dmxdevfilter->dev->lock);
482 wake_up(&buffer->queue);
483 return 0;
486 /* stop feed but only mark the specified filter as stopped (state set) */
487 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
489 struct dmxdev_feed *feed;
491 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
493 switch (dmxdevfilter->type) {
494 case DMXDEV_TYPE_SEC:
495 del_timer(&dmxdevfilter->timer);
496 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
497 break;
498 case DMXDEV_TYPE_PES:
499 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
500 feed->ts->stop_filtering(feed->ts);
501 break;
502 default:
503 return -EINVAL;
505 return 0;
508 /* start feed associated with the specified filter */
509 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
511 struct dmxdev_feed *feed;
512 int ret;
514 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
516 switch (filter->type) {
517 case DMXDEV_TYPE_SEC:
518 return filter->feed.sec->start_filtering(filter->feed.sec);
519 case DMXDEV_TYPE_PES:
520 list_for_each_entry(feed, &filter->feed.ts, next) {
521 ret = feed->ts->start_filtering(feed->ts);
522 if (ret < 0) {
523 dvb_dmxdev_feed_stop(filter);
524 return ret;
527 break;
528 default:
529 return -EINVAL;
532 return 0;
535 /* restart section feed if it has filters left associated with it,
536 otherwise release the feed */
537 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
539 int i;
540 struct dmxdev *dmxdev = filter->dev;
541 u16 pid = filter->params.sec.pid;
543 for (i = 0; i < dmxdev->filternum; i++)
544 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
545 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
546 dmxdev->filter[i].params.sec.pid == pid) {
547 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
548 return 0;
551 filter->dev->demux->release_section_feed(dmxdev->demux,
552 filter->feed.sec);
554 return 0;
557 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
559 struct dmxdev_feed *feed;
560 struct dmx_demux *demux;
562 if (dmxdevfilter->state < DMXDEV_STATE_GO)
563 return 0;
565 switch (dmxdevfilter->type) {
566 case DMXDEV_TYPE_SEC:
567 if (!dmxdevfilter->feed.sec)
568 break;
569 dvb_dmxdev_feed_stop(dmxdevfilter);
570 if (dmxdevfilter->filter.sec)
571 dmxdevfilter->feed.sec->
572 release_filter(dmxdevfilter->feed.sec,
573 dmxdevfilter->filter.sec);
574 dvb_dmxdev_feed_restart(dmxdevfilter);
575 dmxdevfilter->feed.sec = NULL;
576 break;
577 case DMXDEV_TYPE_PES:
578 dvb_dmxdev_feed_stop(dmxdevfilter);
579 demux = dmxdevfilter->dev->demux;
580 list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
581 demux->release_ts_feed(demux, feed->ts);
582 feed->ts = NULL;
584 break;
585 default:
586 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
587 return 0;
588 return -EINVAL;
591 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
592 return 0;
595 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
597 struct dmxdev_feed *feed, *tmp;
599 /* delete all PIDs */
600 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
601 list_del(&feed->next);
602 kfree(feed);
605 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
608 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
610 if (dmxdevfilter->state < DMXDEV_STATE_SET)
611 return 0;
613 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
614 dvb_dmxdev_delete_pids(dmxdevfilter);
616 dmxdevfilter->type = DMXDEV_TYPE_NONE;
617 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
618 return 0;
621 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
622 struct dmxdev_filter *filter,
623 struct dmxdev_feed *feed)
625 ktime_t timeout = ktime_set(0, 0);
626 struct dmx_pes_filter_params *para = &filter->params.pes;
627 enum dmx_output otype;
628 int ret;
629 int ts_type;
630 enum dmx_ts_pes ts_pes;
631 struct dmx_ts_feed *tsfeed;
633 feed->ts = NULL;
634 otype = para->output;
636 ts_pes = para->pes_type;
638 if (ts_pes < DMX_PES_OTHER)
639 ts_type = TS_DECODER;
640 else
641 ts_type = 0;
643 if (otype == DMX_OUT_TS_TAP)
644 ts_type |= TS_PACKET;
645 else if (otype == DMX_OUT_TSDEMUX_TAP)
646 ts_type |= TS_PACKET | TS_DEMUX;
647 else if (otype == DMX_OUT_TAP)
648 ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
650 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
651 dvb_dmxdev_ts_callback);
652 if (ret < 0)
653 return ret;
655 tsfeed = feed->ts;
656 tsfeed->priv = filter;
658 ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, timeout);
659 if (ret < 0) {
660 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
661 return ret;
664 ret = tsfeed->start_filtering(tsfeed);
665 if (ret < 0) {
666 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
667 return ret;
670 return 0;
673 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
675 struct dmxdev *dmxdev = filter->dev;
676 struct dmxdev_feed *feed;
677 void *mem;
678 int ret, i;
680 if (filter->state < DMXDEV_STATE_SET)
681 return -EINVAL;
683 if (filter->state >= DMXDEV_STATE_GO)
684 dvb_dmxdev_filter_stop(filter);
686 if (!filter->buffer.data) {
687 mem = vmalloc(filter->buffer.size);
688 if (!mem)
689 return -ENOMEM;
690 spin_lock_irq(&filter->dev->lock);
691 filter->buffer.data = mem;
692 spin_unlock_irq(&filter->dev->lock);
695 dvb_ringbuffer_flush(&filter->buffer);
697 switch (filter->type) {
698 case DMXDEV_TYPE_SEC:
700 struct dmx_sct_filter_params *para = &filter->params.sec;
701 struct dmx_section_filter **secfilter = &filter->filter.sec;
702 struct dmx_section_feed **secfeed = &filter->feed.sec;
704 *secfilter = NULL;
705 *secfeed = NULL;
708 /* find active filter/feed with same PID */
709 for (i = 0; i < dmxdev->filternum; i++) {
710 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
711 dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
712 dmxdev->filter[i].params.sec.pid == para->pid) {
713 *secfeed = dmxdev->filter[i].feed.sec;
714 break;
718 /* if no feed found, try to allocate new one */
719 if (!*secfeed) {
720 ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
721 secfeed,
722 dvb_dmxdev_section_callback);
723 if (ret < 0) {
724 pr_err("DVB (%s): could not alloc feed\n",
725 __func__);
726 return ret;
729 ret = (*secfeed)->set(*secfeed, para->pid,
730 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
731 if (ret < 0) {
732 pr_err("DVB (%s): could not set feed\n",
733 __func__);
734 dvb_dmxdev_feed_restart(filter);
735 return ret;
737 } else {
738 dvb_dmxdev_feed_stop(filter);
741 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
742 if (ret < 0) {
743 dvb_dmxdev_feed_restart(filter);
744 filter->feed.sec->start_filtering(*secfeed);
745 dprintk("could not get filter\n");
746 return ret;
749 (*secfilter)->priv = filter;
751 memcpy(&((*secfilter)->filter_value[3]),
752 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
753 memcpy(&(*secfilter)->filter_mask[3],
754 &para->filter.mask[1], DMX_FILTER_SIZE - 1);
755 memcpy(&(*secfilter)->filter_mode[3],
756 &para->filter.mode[1], DMX_FILTER_SIZE - 1);
758 (*secfilter)->filter_value[0] = para->filter.filter[0];
759 (*secfilter)->filter_mask[0] = para->filter.mask[0];
760 (*secfilter)->filter_mode[0] = para->filter.mode[0];
761 (*secfilter)->filter_mask[1] = 0;
762 (*secfilter)->filter_mask[2] = 0;
764 filter->todo = 0;
766 ret = filter->feed.sec->start_filtering(filter->feed.sec);
767 if (ret < 0)
768 return ret;
770 dvb_dmxdev_filter_timer(filter);
771 break;
773 case DMXDEV_TYPE_PES:
774 list_for_each_entry(feed, &filter->feed.ts, next) {
775 ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
776 if (ret < 0) {
777 dvb_dmxdev_filter_stop(filter);
778 return ret;
781 break;
782 default:
783 return -EINVAL;
786 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
787 return 0;
790 static int dvb_demux_open(struct inode *inode, struct file *file)
792 struct dvb_device *dvbdev = file->private_data;
793 struct dmxdev *dmxdev = dvbdev->priv;
794 int i;
795 struct dmxdev_filter *dmxdevfilter;
797 if (!dmxdev->filter)
798 return -EINVAL;
800 if (mutex_lock_interruptible(&dmxdev->mutex))
801 return -ERESTARTSYS;
803 for (i = 0; i < dmxdev->filternum; i++)
804 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
805 break;
807 if (i == dmxdev->filternum) {
808 mutex_unlock(&dmxdev->mutex);
809 return -EMFILE;
812 dmxdevfilter = &dmxdev->filter[i];
813 mutex_init(&dmxdevfilter->mutex);
814 file->private_data = dmxdevfilter;
816 #ifdef CONFIG_DVB_MMAP
817 dmxdev->may_do_mmap = 1;
818 #else
819 dmxdev->may_do_mmap = 0;
820 #endif
822 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
823 dvb_vb2_init(&dmxdevfilter->vb2_ctx, "demux_filter",
824 file->f_flags & O_NONBLOCK);
825 dmxdevfilter->type = DMXDEV_TYPE_NONE;
826 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
827 timer_setup(&dmxdevfilter->timer, dvb_dmxdev_filter_timeout, 0);
829 dvbdev->users++;
831 mutex_unlock(&dmxdev->mutex);
832 return 0;
835 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
836 struct dmxdev_filter *dmxdevfilter)
838 mutex_lock(&dmxdev->mutex);
839 mutex_lock(&dmxdevfilter->mutex);
840 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
841 dvb_vb2_stream_off(&dmxdevfilter->vb2_ctx);
842 dvb_vb2_release(&dmxdevfilter->vb2_ctx);
845 dvb_dmxdev_filter_stop(dmxdevfilter);
846 dvb_dmxdev_filter_reset(dmxdevfilter);
848 if (dmxdevfilter->buffer.data) {
849 void *mem = dmxdevfilter->buffer.data;
851 spin_lock_irq(&dmxdev->lock);
852 dmxdevfilter->buffer.data = NULL;
853 spin_unlock_irq(&dmxdev->lock);
854 vfree(mem);
857 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
858 wake_up(&dmxdevfilter->buffer.queue);
859 mutex_unlock(&dmxdevfilter->mutex);
860 mutex_unlock(&dmxdev->mutex);
861 return 0;
864 static inline void invert_mode(struct dmx_filter *filter)
866 int i;
868 for (i = 0; i < DMX_FILTER_SIZE; i++)
869 filter->mode[i] ^= 0xff;
872 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
873 struct dmxdev_filter *filter, u16 pid)
875 struct dmxdev_feed *feed;
877 if ((filter->type != DMXDEV_TYPE_PES) ||
878 (filter->state < DMXDEV_STATE_SET))
879 return -EINVAL;
881 /* only TS packet filters may have multiple PIDs */
882 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
883 (!list_empty(&filter->feed.ts)))
884 return -EINVAL;
886 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
887 if (feed == NULL)
888 return -ENOMEM;
890 feed->pid = pid;
891 list_add(&feed->next, &filter->feed.ts);
893 if (filter->state >= DMXDEV_STATE_GO)
894 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
896 return 0;
899 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
900 struct dmxdev_filter *filter, u16 pid)
902 struct dmxdev_feed *feed, *tmp;
904 if ((filter->type != DMXDEV_TYPE_PES) ||
905 (filter->state < DMXDEV_STATE_SET))
906 return -EINVAL;
908 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
909 if ((feed->pid == pid) && (feed->ts != NULL)) {
910 feed->ts->stop_filtering(feed->ts);
911 filter->dev->demux->release_ts_feed(filter->dev->demux,
912 feed->ts);
913 list_del(&feed->next);
914 kfree(feed);
918 return 0;
921 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
922 struct dmxdev_filter *dmxdevfilter,
923 struct dmx_sct_filter_params *params)
925 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
926 __func__, params->pid, params->flags, params->timeout);
928 dvb_dmxdev_filter_stop(dmxdevfilter);
930 dmxdevfilter->type = DMXDEV_TYPE_SEC;
931 memcpy(&dmxdevfilter->params.sec,
932 params, sizeof(struct dmx_sct_filter_params));
933 invert_mode(&dmxdevfilter->params.sec.filter);
934 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
936 if (params->flags & DMX_IMMEDIATE_START)
937 return dvb_dmxdev_filter_start(dmxdevfilter);
939 return 0;
942 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
943 struct dmxdev_filter *dmxdevfilter,
944 struct dmx_pes_filter_params *params)
946 int ret;
948 dvb_dmxdev_filter_stop(dmxdevfilter);
949 dvb_dmxdev_filter_reset(dmxdevfilter);
951 if ((unsigned int)params->pes_type > DMX_PES_OTHER)
952 return -EINVAL;
954 dmxdevfilter->type = DMXDEV_TYPE_PES;
955 memcpy(&dmxdevfilter->params, params,
956 sizeof(struct dmx_pes_filter_params));
957 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
959 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
961 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
962 dmxdevfilter->params.pes.pid);
963 if (ret < 0)
964 return ret;
966 if (params->flags & DMX_IMMEDIATE_START)
967 return dvb_dmxdev_filter_start(dmxdevfilter);
969 return 0;
972 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
973 struct file *file, char __user *buf,
974 size_t count, loff_t *ppos)
976 int result, hcount;
977 int done = 0;
979 if (dfil->todo <= 0) {
980 hcount = 3 + dfil->todo;
981 if (hcount > count)
982 hcount = count;
983 result = dvb_dmxdev_buffer_read(&dfil->buffer,
984 file->f_flags & O_NONBLOCK,
985 buf, hcount, ppos);
986 if (result < 0) {
987 dfil->todo = 0;
988 return result;
990 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
991 return -EFAULT;
992 buf += result;
993 done = result;
994 count -= result;
995 dfil->todo -= result;
996 if (dfil->todo > -3)
997 return done;
998 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
999 if (!count)
1000 return done;
1002 if (count > dfil->todo)
1003 count = dfil->todo;
1004 result = dvb_dmxdev_buffer_read(&dfil->buffer,
1005 file->f_flags & O_NONBLOCK,
1006 buf, count, ppos);
1007 if (result < 0)
1008 return result;
1009 dfil->todo -= result;
1010 return (result + done);
1013 static ssize_t
1014 dvb_demux_read(struct file *file, char __user *buf, size_t count,
1015 loff_t *ppos)
1017 struct dmxdev_filter *dmxdevfilter = file->private_data;
1018 int ret;
1020 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
1021 return -ERESTARTSYS;
1023 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
1024 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
1025 else
1026 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
1027 file->f_flags & O_NONBLOCK,
1028 buf, count, ppos);
1030 mutex_unlock(&dmxdevfilter->mutex);
1031 return ret;
1034 static int dvb_demux_do_ioctl(struct file *file,
1035 unsigned int cmd, void *parg)
1037 struct dmxdev_filter *dmxdevfilter = file->private_data;
1038 struct dmxdev *dmxdev = dmxdevfilter->dev;
1039 unsigned long arg = (unsigned long)parg;
1040 int ret = 0;
1042 if (mutex_lock_interruptible(&dmxdev->mutex))
1043 return -ERESTARTSYS;
1045 switch (cmd) {
1046 case DMX_START:
1047 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1048 mutex_unlock(&dmxdev->mutex);
1049 return -ERESTARTSYS;
1051 if (dmxdevfilter->state < DMXDEV_STATE_SET)
1052 ret = -EINVAL;
1053 else
1054 ret = dvb_dmxdev_filter_start(dmxdevfilter);
1055 mutex_unlock(&dmxdevfilter->mutex);
1056 break;
1058 case DMX_STOP:
1059 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1060 mutex_unlock(&dmxdev->mutex);
1061 return -ERESTARTSYS;
1063 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
1064 mutex_unlock(&dmxdevfilter->mutex);
1065 break;
1067 case DMX_SET_FILTER:
1068 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1069 mutex_unlock(&dmxdev->mutex);
1070 return -ERESTARTSYS;
1072 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1073 mutex_unlock(&dmxdevfilter->mutex);
1074 break;
1076 case DMX_SET_PES_FILTER:
1077 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1078 mutex_unlock(&dmxdev->mutex);
1079 return -ERESTARTSYS;
1081 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1082 mutex_unlock(&dmxdevfilter->mutex);
1083 break;
1085 case DMX_SET_BUFFER_SIZE:
1086 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1087 mutex_unlock(&dmxdev->mutex);
1088 return -ERESTARTSYS;
1090 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1091 mutex_unlock(&dmxdevfilter->mutex);
1092 break;
1094 case DMX_GET_PES_PIDS:
1095 if (!dmxdev->demux->get_pes_pids) {
1096 ret = -EINVAL;
1097 break;
1099 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1100 break;
1102 case DMX_GET_STC:
1103 if (!dmxdev->demux->get_stc) {
1104 ret = -EINVAL;
1105 break;
1107 ret = dmxdev->demux->get_stc(dmxdev->demux,
1108 ((struct dmx_stc *)parg)->num,
1109 &((struct dmx_stc *)parg)->stc,
1110 &((struct dmx_stc *)parg)->base);
1111 break;
1113 case DMX_ADD_PID:
1114 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1115 ret = -ERESTARTSYS;
1116 break;
1118 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1119 mutex_unlock(&dmxdevfilter->mutex);
1120 break;
1122 case DMX_REMOVE_PID:
1123 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1124 ret = -ERESTARTSYS;
1125 break;
1127 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1128 mutex_unlock(&dmxdevfilter->mutex);
1129 break;
1131 #ifdef CONFIG_DVB_MMAP
1132 case DMX_REQBUFS:
1133 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1134 mutex_unlock(&dmxdev->mutex);
1135 return -ERESTARTSYS;
1137 ret = dvb_vb2_reqbufs(&dmxdevfilter->vb2_ctx, parg);
1138 mutex_unlock(&dmxdevfilter->mutex);
1139 break;
1141 case DMX_QUERYBUF:
1142 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1143 mutex_unlock(&dmxdev->mutex);
1144 return -ERESTARTSYS;
1146 ret = dvb_vb2_querybuf(&dmxdevfilter->vb2_ctx, parg);
1147 mutex_unlock(&dmxdevfilter->mutex);
1148 break;
1150 case DMX_EXPBUF:
1151 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1152 mutex_unlock(&dmxdev->mutex);
1153 return -ERESTARTSYS;
1155 ret = dvb_vb2_expbuf(&dmxdevfilter->vb2_ctx, parg);
1156 mutex_unlock(&dmxdevfilter->mutex);
1157 break;
1159 case DMX_QBUF:
1160 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1161 mutex_unlock(&dmxdev->mutex);
1162 return -ERESTARTSYS;
1164 ret = dvb_vb2_qbuf(&dmxdevfilter->vb2_ctx, parg);
1165 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1166 ret = dvb_vb2_stream_on(&dmxdevfilter->vb2_ctx);
1167 mutex_unlock(&dmxdevfilter->mutex);
1168 break;
1170 case DMX_DQBUF:
1171 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1172 mutex_unlock(&dmxdev->mutex);
1173 return -ERESTARTSYS;
1175 ret = dvb_vb2_dqbuf(&dmxdevfilter->vb2_ctx, parg);
1176 mutex_unlock(&dmxdevfilter->mutex);
1177 break;
1178 #endif
1179 default:
1180 ret = -ENOTTY;
1181 break;
1183 mutex_unlock(&dmxdev->mutex);
1184 return ret;
1187 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1188 unsigned long arg)
1190 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1193 static __poll_t dvb_demux_poll(struct file *file, poll_table *wait)
1195 struct dmxdev_filter *dmxdevfilter = file->private_data;
1196 __poll_t mask = 0;
1198 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1200 if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
1201 return EPOLLERR;
1202 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1203 return dvb_vb2_poll(&dmxdevfilter->vb2_ctx, file, wait);
1205 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1206 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1207 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1208 return 0;
1210 if (dmxdevfilter->buffer.error)
1211 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1213 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1214 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1216 return mask;
1219 #ifdef CONFIG_DVB_MMAP
1220 static int dvb_demux_mmap(struct file *file, struct vm_area_struct *vma)
1222 struct dmxdev_filter *dmxdevfilter = file->private_data;
1223 struct dmxdev *dmxdev = dmxdevfilter->dev;
1224 int ret;
1226 if (!dmxdev->may_do_mmap)
1227 return -ENOTTY;
1229 if (mutex_lock_interruptible(&dmxdev->mutex))
1230 return -ERESTARTSYS;
1232 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1233 mutex_unlock(&dmxdev->mutex);
1234 return -ERESTARTSYS;
1236 ret = dvb_vb2_mmap(&dmxdevfilter->vb2_ctx, vma);
1238 mutex_unlock(&dmxdevfilter->mutex);
1239 mutex_unlock(&dmxdev->mutex);
1241 return ret;
1243 #endif
1245 static int dvb_demux_release(struct inode *inode, struct file *file)
1247 struct dmxdev_filter *dmxdevfilter = file->private_data;
1248 struct dmxdev *dmxdev = dmxdevfilter->dev;
1249 int ret;
1251 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1253 mutex_lock(&dmxdev->mutex);
1254 dmxdev->dvbdev->users--;
1255 if (dmxdev->dvbdev->users == 1 && dmxdev->exit == 1) {
1256 mutex_unlock(&dmxdev->mutex);
1257 wake_up(&dmxdev->dvbdev->wait_queue);
1258 } else
1259 mutex_unlock(&dmxdev->mutex);
1261 return ret;
1264 static const struct file_operations dvb_demux_fops = {
1265 .owner = THIS_MODULE,
1266 .read = dvb_demux_read,
1267 .unlocked_ioctl = dvb_demux_ioctl,
1268 .compat_ioctl = dvb_demux_ioctl,
1269 .open = dvb_demux_open,
1270 .release = dvb_demux_release,
1271 .poll = dvb_demux_poll,
1272 .llseek = default_llseek,
1273 #ifdef CONFIG_DVB_MMAP
1274 .mmap = dvb_demux_mmap,
1275 #endif
1278 static const struct dvb_device dvbdev_demux = {
1279 .priv = NULL,
1280 .users = 1,
1281 .writers = 1,
1282 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1283 .name = "dvb-demux",
1284 #endif
1285 .fops = &dvb_demux_fops
1288 static int dvb_dvr_do_ioctl(struct file *file,
1289 unsigned int cmd, void *parg)
1291 struct dvb_device *dvbdev = file->private_data;
1292 struct dmxdev *dmxdev = dvbdev->priv;
1293 unsigned long arg = (unsigned long)parg;
1294 int ret;
1296 if (mutex_lock_interruptible(&dmxdev->mutex))
1297 return -ERESTARTSYS;
1299 switch (cmd) {
1300 case DMX_SET_BUFFER_SIZE:
1301 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1302 break;
1304 #ifdef CONFIG_DVB_MMAP
1305 case DMX_REQBUFS:
1306 ret = dvb_vb2_reqbufs(&dmxdev->dvr_vb2_ctx, parg);
1307 break;
1309 case DMX_QUERYBUF:
1310 ret = dvb_vb2_querybuf(&dmxdev->dvr_vb2_ctx, parg);
1311 break;
1313 case DMX_EXPBUF:
1314 ret = dvb_vb2_expbuf(&dmxdev->dvr_vb2_ctx, parg);
1315 break;
1317 case DMX_QBUF:
1318 ret = dvb_vb2_qbuf(&dmxdev->dvr_vb2_ctx, parg);
1319 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1320 ret = dvb_vb2_stream_on(&dmxdev->dvr_vb2_ctx);
1321 break;
1323 case DMX_DQBUF:
1324 ret = dvb_vb2_dqbuf(&dmxdev->dvr_vb2_ctx, parg);
1325 break;
1326 #endif
1327 default:
1328 ret = -ENOTTY;
1329 break;
1331 mutex_unlock(&dmxdev->mutex);
1332 return ret;
1335 static long dvb_dvr_ioctl(struct file *file,
1336 unsigned int cmd, unsigned long arg)
1338 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1341 static __poll_t dvb_dvr_poll(struct file *file, poll_table *wait)
1343 struct dvb_device *dvbdev = file->private_data;
1344 struct dmxdev *dmxdev = dvbdev->priv;
1345 __poll_t mask = 0;
1347 dprintk("%s\n", __func__);
1349 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1351 if (dmxdev->exit)
1352 return EPOLLERR;
1353 if (dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1354 return dvb_vb2_poll(&dmxdev->dvr_vb2_ctx, file, wait);
1356 if (((file->f_flags & O_ACCMODE) == O_RDONLY) ||
1357 dmxdev->may_do_mmap) {
1358 if (dmxdev->dvr_buffer.error)
1359 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1361 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1362 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1363 } else
1364 mask |= (EPOLLOUT | EPOLLWRNORM | EPOLLPRI);
1366 return mask;
1369 #ifdef CONFIG_DVB_MMAP
1370 static int dvb_dvr_mmap(struct file *file, struct vm_area_struct *vma)
1372 struct dvb_device *dvbdev = file->private_data;
1373 struct dmxdev *dmxdev = dvbdev->priv;
1374 int ret;
1376 if (!dmxdev->may_do_mmap)
1377 return -ENOTTY;
1379 if (dmxdev->exit)
1380 return -ENODEV;
1382 if (mutex_lock_interruptible(&dmxdev->mutex))
1383 return -ERESTARTSYS;
1385 ret = dvb_vb2_mmap(&dmxdev->dvr_vb2_ctx, vma);
1386 mutex_unlock(&dmxdev->mutex);
1387 return ret;
1389 #endif
1391 static const struct file_operations dvb_dvr_fops = {
1392 .owner = THIS_MODULE,
1393 .read = dvb_dvr_read,
1394 .write = dvb_dvr_write,
1395 .unlocked_ioctl = dvb_dvr_ioctl,
1396 .open = dvb_dvr_open,
1397 .release = dvb_dvr_release,
1398 .poll = dvb_dvr_poll,
1399 .llseek = default_llseek,
1400 #ifdef CONFIG_DVB_MMAP
1401 .mmap = dvb_dvr_mmap,
1402 #endif
1405 static const struct dvb_device dvbdev_dvr = {
1406 .priv = NULL,
1407 .readers = 1,
1408 .users = 1,
1409 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1410 .name = "dvb-dvr",
1411 #endif
1412 .fops = &dvb_dvr_fops
1414 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1416 int i;
1418 if (dmxdev->demux->open(dmxdev->demux) < 0)
1419 return -EUSERS;
1421 dmxdev->filter = vmalloc(array_size(sizeof(struct dmxdev_filter),
1422 dmxdev->filternum));
1423 if (!dmxdev->filter)
1424 return -ENOMEM;
1426 mutex_init(&dmxdev->mutex);
1427 spin_lock_init(&dmxdev->lock);
1428 for (i = 0; i < dmxdev->filternum; i++) {
1429 dmxdev->filter[i].dev = dmxdev;
1430 dmxdev->filter[i].buffer.data = NULL;
1431 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1432 DMXDEV_STATE_FREE);
1435 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1436 DVB_DEVICE_DEMUX, dmxdev->filternum);
1437 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1438 dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
1440 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1442 return 0;
1445 EXPORT_SYMBOL(dvb_dmxdev_init);
1447 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1449 dmxdev->exit = 1;
1450 if (dmxdev->dvbdev->users > 1) {
1451 wait_event(dmxdev->dvbdev->wait_queue,
1452 dmxdev->dvbdev->users == 1);
1454 if (dmxdev->dvr_dvbdev->users > 1) {
1455 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1456 dmxdev->dvr_dvbdev->users == 1);
1459 dvb_unregister_device(dmxdev->dvbdev);
1460 dvb_unregister_device(dmxdev->dvr_dvbdev);
1462 vfree(dmxdev->filter);
1463 dmxdev->filter = NULL;
1464 dmxdev->demux->close(dmxdev->demux);
1467 EXPORT_SYMBOL(dvb_dmxdev_release);