1 /*
2 * dmxdev.c - DVB demultiplexer device
3 *
4 * Copyright (C) 2000 Ralph Metzler & Marcus Metzler
5 * for convergence integrated media GmbH
6 *
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.
11 *
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.
16 *
17 */
18
19 #define pr_fmt(fmt) "dmxdev: " fmt
20
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>
32
33 static int debug;
34
35 module_param(debug, int, 0644);
36 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
37
38 #define dprintk(fmt, arg...) do { \
39 if (debug) \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), \
41 __func__, ##arg); \
42 } while (0)
43
dvb_dmxdev_buffer_write(struct dvb_ringbuffer * buf,const u8 * src,size_t len)44 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
45 const u8 *src, size_t len)
46 {
47 ssize_t free;
48
49 if (!len)
50 return 0;
51 if (!buf->data)
52 return 0;
53
54 free = dvb_ringbuffer_free(buf);
55 if (len > free) {
56 dprintk("buffer overflow\n");
57 return -EOVERFLOW;
58 }
59
60 return dvb_ringbuffer_write(buf, src, len);
61 }
62
dvb_dmxdev_buffer_read(struct dvb_ringbuffer * src,int non_blocking,char __user * buf,size_t count,loff_t * ppos)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)
66 {
67 size_t todo;
68 ssize_t avail;
69 ssize_t ret = 0;
70
71 if (!src->data)
72 return 0;
73
74 if (src->error) {
75 ret = src->error;
76 dvb_ringbuffer_flush(src);
77 return ret;
78 }
79
80 for (todo = count; todo > 0; todo -= ret) {
81 if (non_blocking && dvb_ringbuffer_empty(src)) {
82 ret = -EWOULDBLOCK;
83 break;
84 }
85
86 ret = wait_event_interruptible(src->queue,
87 !dvb_ringbuffer_empty(src) ||
88 (src->error != 0));
89 if (ret < 0)
90 break;
91
92 if (src->error) {
93 ret = src->error;
94 dvb_ringbuffer_flush(src);
95 break;
96 }
97
98 avail = dvb_ringbuffer_avail(src);
99 if (avail > todo)
100 avail = todo;
101
102 ret = dvb_ringbuffer_read_user(src, buf, avail);
103 if (ret < 0)
104 break;
105
106 buf += ret;
107 }
108
109 return (count - todo) ? (count - todo) : ret;
110 }
111
get_fe(struct dmx_demux * demux,int type)112 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
113 {
114 struct list_head *head, *pos;
115
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);
122
123 return NULL;
124 }
125
dvb_dvr_open(struct inode * inode,struct file * file)126 static int dvb_dvr_open(struct inode *inode, struct file *file)
127 {
128 struct dvb_device *dvbdev = file->private_data;
129 struct dmxdev *dmxdev = dvbdev->priv;
130 struct dmx_frontend *front;
131 bool need_ringbuffer = false;
132
133 dprintk("%s\n", __func__);
134
135 if (mutex_lock_interruptible(&dmxdev->mutex))
136 return -ERESTARTSYS;
137
138 if (dmxdev->exit) {
139 mutex_unlock(&dmxdev->mutex);
140 return -ENODEV;
141 }
142
143 dmxdev->may_do_mmap = 0;
144
145 /*
146 * The logic here is a little tricky due to the ifdef.
147 *
148 * The ringbuffer is used for both read and mmap.
149 *
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.
153 */
154
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
166 }
167 }
168
169 if (need_ringbuffer) {
170 void *mem;
171
172 if (!dvbdev->readers) {
173 mutex_unlock(&dmxdev->mutex);
174 return -EBUSY;
175 }
176 mem = vmalloc(DVR_BUFFER_SIZE);
177 if (!mem) {
178 mutex_unlock(&dmxdev->mutex);
179 return -ENOMEM;
180 }
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--;
186 }
187
188 if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
189 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
190
191 if (!dmxdev->demux->write) {
192 mutex_unlock(&dmxdev->mutex);
193 return -EOPNOTSUPP;
194 }
195
196 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
197
198 if (!front) {
199 mutex_unlock(&dmxdev->mutex);
200 return -EINVAL;
201 }
202 dmxdev->demux->disconnect_frontend(dmxdev->demux);
203 dmxdev->demux->connect_frontend(dmxdev->demux, front);
204 }
205 dvbdev->users++;
206 mutex_unlock(&dmxdev->mutex);
207 return 0;
208 }
209
dvb_dvr_release(struct inode * inode,struct file * file)210 static int dvb_dvr_release(struct inode *inode, struct file *file)
211 {
212 struct dvb_device *dvbdev = file->private_data;
213 struct dmxdev *dmxdev = dvbdev->priv;
214
215 mutex_lock(&dmxdev->mutex);
216
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);
221 }
222
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);
229 }
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);
239 }
240 }
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);
248
249 return 0;
250 }
251
dvb_dvr_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)252 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
253 size_t count, loff_t *ppos)
254 {
255 struct dvb_device *dvbdev = file->private_data;
256 struct dmxdev *dmxdev = dvbdev->priv;
257 int ret;
258
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;
265
266 if (dmxdev->exit) {
267 mutex_unlock(&dmxdev->mutex);
268 return -ENODEV;
269 }
270 ret = dmxdev->demux->write(dmxdev->demux, buf, count);
271 mutex_unlock(&dmxdev->mutex);
272 return ret;
273 }
274
dvb_dvr_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)275 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
276 loff_t *ppos)
277 {
278 struct dvb_device *dvbdev = file->private_data;
279 struct dmxdev *dmxdev = dvbdev->priv;
280
281 if (dmxdev->exit)
282 return -ENODEV;
283
284 return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
285 file->f_flags & O_NONBLOCK,
286 buf, count, ppos);
287 }
288
dvb_dvr_set_buffer_size(struct dmxdev * dmxdev,unsigned long size)289 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
290 unsigned long size)
291 {
292 struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
293 void *newmem;
294 void *oldmem;
295
296 dprintk("%s\n", __func__);
297
298 if (buf->size == size)
299 return 0;
300 if (!size)
301 return -EINVAL;
302
303 newmem = vmalloc(size);
304 if (!newmem)
305 return -ENOMEM;
306
307 oldmem = buf->data;
308
309 spin_lock_irq(&dmxdev->lock);
310 buf->data = newmem;
311 buf->size = size;
312
313 /* reset and not flush in case the buffer shrinks */
314 dvb_ringbuffer_reset(buf);
315 spin_unlock_irq(&dmxdev->lock);
316
317 vfree(oldmem);
318
319 return 0;
320 }
321
dvb_dmxdev_filter_state_set(struct dmxdev_filter * dmxdevfilter,int state)322 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
323 *dmxdevfilter, int state)
324 {
325 spin_lock_irq(&dmxdevfilter->dev->lock);
326 dmxdevfilter->state = state;
327 spin_unlock_irq(&dmxdevfilter->dev->lock);
328 }
329
dvb_dmxdev_set_buffer_size(struct dmxdev_filter * dmxdevfilter,unsigned long size)330 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
331 unsigned long size)
332 {
333 struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
334 void *newmem;
335 void *oldmem;
336
337 if (buf->size == size)
338 return 0;
339 if (!size)
340 return -EINVAL;
341 if (dmxdevfilter->state >= DMXDEV_STATE_GO)
342 return -EBUSY;
343
344 newmem = vmalloc(size);
345 if (!newmem)
346 return -ENOMEM;
347
348 oldmem = buf->data;
349
350 spin_lock_irq(&dmxdevfilter->dev->lock);
351 buf->data = newmem;
352 buf->size = size;
353
354 /* reset and not flush in case the buffer shrinks */
355 dvb_ringbuffer_reset(buf);
356 spin_unlock_irq(&dmxdevfilter->dev->lock);
357
358 vfree(oldmem);
359
360 return 0;
361 }
362
dvb_dmxdev_filter_timeout(struct timer_list * t)363 static void dvb_dmxdev_filter_timeout(struct timer_list *t)
364 {
365 struct dmxdev_filter *dmxdevfilter = from_timer(dmxdevfilter, t, timer);
366
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);
372 }
373
dvb_dmxdev_filter_timer(struct dmxdev_filter * dmxdevfilter)374 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
375 {
376 struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
377
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);
383 }
384 }
385
dvb_dmxdev_section_callback(const u8 * buffer1,size_t buffer1_len,const u8 * buffer2,size_t buffer2_len,struct dmx_section_filter * filter,u32 * buffer_flags)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)
390 {
391 struct dmxdev_filter *dmxdevfilter = filter->priv;
392 int ret;
393
394 if (!dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx) &&
395 dmxdevfilter->buffer.error) {
396 wake_up(&dmxdevfilter->buffer.queue);
397 return 0;
398 }
399 spin_lock(&dmxdevfilter->dev->lock);
400 if (dmxdevfilter->state != DMXDEV_STATE_GO) {
401 spin_unlock(&dmxdevfilter->dev->lock);
402 return 0;
403 }
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);
420 }
421 }
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;
429 }
430
dvb_dmxdev_ts_callback(const u8 * buffer1,size_t buffer1_len,const u8 * buffer2,size_t buffer2_len,struct dmx_ts_feed * feed,u32 * buffer_flags)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)
435 {
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;
442
443 spin_lock(&dmxdevfilter->dev->lock);
444 if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
445 spin_unlock(&dmxdevfilter->dev->lock);
446 return 0;
447 }
448
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
460 }
461
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;
473 }
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);
478 }
479 if (ret < 0)
480 buffer->error = ret;
481 spin_unlock(&dmxdevfilter->dev->lock);
482 wake_up(&buffer->queue);
483 return 0;
484 }
485
486 /* stop feed but only mark the specified filter as stopped (state set) */
dvb_dmxdev_feed_stop(struct dmxdev_filter * dmxdevfilter)487 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
488 {
489 struct dmxdev_feed *feed;
490
491 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
492
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;
504 }
505 return 0;
506 }
507
508 /* start feed associated with the specified filter */
dvb_dmxdev_feed_start(struct dmxdev_filter * filter)509 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
510 {
511 struct dmxdev_feed *feed;
512 int ret;
513
514 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
515
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;
525 }
526 }
527 break;
528 default:
529 return -EINVAL;
530 }
531
532 return 0;
533 }
534
535 /* restart section feed if it has filters left associated with it,
536 otherwise release the feed */
dvb_dmxdev_feed_restart(struct dmxdev_filter * filter)537 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
538 {
539 int i;
540 struct dmxdev *dmxdev = filter->dev;
541 u16 pid = filter->params.sec.pid;
542
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;
549 }
550
551 filter->dev->demux->release_section_feed(dmxdev->demux,
552 filter->feed.sec);
553
554 return 0;
555 }
556
dvb_dmxdev_filter_stop(struct dmxdev_filter * dmxdevfilter)557 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
558 {
559 struct dmxdev_feed *feed;
560 struct dmx_demux *demux;
561
562 if (dmxdevfilter->state < DMXDEV_STATE_GO)
563 return 0;
564
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;
583 }
584 break;
585 default:
586 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
587 return 0;
588 return -EINVAL;
589 }
590
591 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
592 return 0;
593 }
594
dvb_dmxdev_delete_pids(struct dmxdev_filter * dmxdevfilter)595 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
596 {
597 struct dmxdev_feed *feed, *tmp;
598
599 /* delete all PIDs */
600 list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
601 list_del(&feed->next);
602 kfree(feed);
603 }
604
605 BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
606 }
607
dvb_dmxdev_filter_reset(struct dmxdev_filter * dmxdevfilter)608 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
609 {
610 if (dmxdevfilter->state < DMXDEV_STATE_SET)
611 return 0;
612
613 if (dmxdevfilter->type == DMXDEV_TYPE_PES)
614 dvb_dmxdev_delete_pids(dmxdevfilter);
615
616 dmxdevfilter->type = DMXDEV_TYPE_NONE;
617 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
618 return 0;
619 }
620
dvb_dmxdev_start_feed(struct dmxdev * dmxdev,struct dmxdev_filter * filter,struct dmxdev_feed * feed)621 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
622 struct dmxdev_filter *filter,
623 struct dmxdev_feed *feed)
624 {
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;
632
633 feed->ts = NULL;
634 otype = para->output;
635
636 ts_pes = para->pes_type;
637
638 if (ts_pes < DMX_PES_OTHER)
639 ts_type = TS_DECODER;
640 else
641 ts_type = 0;
642
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;
649
650 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
651 dvb_dmxdev_ts_callback);
652 if (ret < 0)
653 return ret;
654
655 tsfeed = feed->ts;
656 tsfeed->priv = filter;
657
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;
662 }
663
664 ret = tsfeed->start_filtering(tsfeed);
665 if (ret < 0) {
666 dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
667 return ret;
668 }
669
670 return 0;
671 }
672
dvb_dmxdev_filter_start(struct dmxdev_filter * filter)673 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
674 {
675 struct dmxdev *dmxdev = filter->dev;
676 struct dmxdev_feed *feed;
677 void *mem;
678 int ret, i;
679
680 if (filter->state < DMXDEV_STATE_SET)
681 return -EINVAL;
682
683 if (filter->state >= DMXDEV_STATE_GO)
684 dvb_dmxdev_filter_stop(filter);
685
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);
693 }
694
695 dvb_ringbuffer_flush(&filter->buffer);
696
697 switch (filter->type) {
698 case DMXDEV_TYPE_SEC:
699 {
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;
703
704 *secfilter = NULL;
705 *secfeed = NULL;
706
707
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;
715 }
716 }
717
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;
727 }
728
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;
736 }
737 } else {
738 dvb_dmxdev_feed_stop(filter);
739 }
740
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;
747 }
748
749 (*secfilter)->priv = filter;
750
751 memcpy(&((*secfilter)->filter_value[3]),
752 &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
753 memcpy(&(*secfilter)->filter_mask[3],
754 ¶->filter.mask[1], DMX_FILTER_SIZE - 1);
755 memcpy(&(*secfilter)->filter_mode[3],
756 ¶->filter.mode[1], DMX_FILTER_SIZE - 1);
757
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;
763
764 filter->todo = 0;
765
766 ret = filter->feed.sec->start_filtering(filter->feed.sec);
767 if (ret < 0)
768 return ret;
769
770 dvb_dmxdev_filter_timer(filter);
771 break;
772 }
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;
779 }
780 }
781 break;
782 default:
783 return -EINVAL;
784 }
785
786 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
787 return 0;
788 }
789
dvb_demux_open(struct inode * inode,struct file * file)790 static int dvb_demux_open(struct inode *inode, struct file *file)
791 {
792 struct dvb_device *dvbdev = file->private_data;
793 struct dmxdev *dmxdev = dvbdev->priv;
794 int i;
795 struct dmxdev_filter *dmxdevfilter;
796
797 if (!dmxdev->filter)
798 return -EINVAL;
799
800 if (mutex_lock_interruptible(&dmxdev->mutex))
801 return -ERESTARTSYS;
802
803 if (dmxdev->exit) {
804 mutex_unlock(&dmxdev->mutex);
805 return -ENODEV;
806 }
807
808 for (i = 0; i < dmxdev->filternum; i++)
809 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
810 break;
811
812 if (i == dmxdev->filternum) {
813 mutex_unlock(&dmxdev->mutex);
814 return -EMFILE;
815 }
816
817 dmxdevfilter = &dmxdev->filter[i];
818 mutex_init(&dmxdevfilter->mutex);
819 file->private_data = dmxdevfilter;
820
821 #ifdef CONFIG_DVB_MMAP
822 dmxdev->may_do_mmap = 1;
823 #else
824 dmxdev->may_do_mmap = 0;
825 #endif
826
827 dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
828 dvb_vb2_init(&dmxdevfilter->vb2_ctx, "demux_filter",
829 file->f_flags & O_NONBLOCK);
830 dmxdevfilter->type = DMXDEV_TYPE_NONE;
831 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
832 timer_setup(&dmxdevfilter->timer, dvb_dmxdev_filter_timeout, 0);
833
834 dvbdev->users++;
835
836 mutex_unlock(&dmxdev->mutex);
837 return 0;
838 }
839
dvb_dmxdev_filter_free(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter)840 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
841 struct dmxdev_filter *dmxdevfilter)
842 {
843 mutex_lock(&dmxdev->mutex);
844 mutex_lock(&dmxdevfilter->mutex);
845 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
846 dvb_vb2_stream_off(&dmxdevfilter->vb2_ctx);
847 dvb_vb2_release(&dmxdevfilter->vb2_ctx);
848
849
850 dvb_dmxdev_filter_stop(dmxdevfilter);
851 dvb_dmxdev_filter_reset(dmxdevfilter);
852
853 if (dmxdevfilter->buffer.data) {
854 void *mem = dmxdevfilter->buffer.data;
855
856 spin_lock_irq(&dmxdev->lock);
857 dmxdevfilter->buffer.data = NULL;
858 spin_unlock_irq(&dmxdev->lock);
859 vfree(mem);
860 }
861
862 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
863 wake_up(&dmxdevfilter->buffer.queue);
864 mutex_unlock(&dmxdevfilter->mutex);
865 mutex_unlock(&dmxdev->mutex);
866 return 0;
867 }
868
invert_mode(struct dmx_filter * filter)869 static inline void invert_mode(struct dmx_filter *filter)
870 {
871 int i;
872
873 for (i = 0; i < DMX_FILTER_SIZE; i++)
874 filter->mode[i] ^= 0xff;
875 }
876
dvb_dmxdev_add_pid(struct dmxdev * dmxdev,struct dmxdev_filter * filter,u16 pid)877 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
878 struct dmxdev_filter *filter, u16 pid)
879 {
880 struct dmxdev_feed *feed;
881
882 if ((filter->type != DMXDEV_TYPE_PES) ||
883 (filter->state < DMXDEV_STATE_SET))
884 return -EINVAL;
885
886 /* only TS packet filters may have multiple PIDs */
887 if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
888 (!list_empty(&filter->feed.ts)))
889 return -EINVAL;
890
891 feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
892 if (feed == NULL)
893 return -ENOMEM;
894
895 feed->pid = pid;
896 list_add(&feed->next, &filter->feed.ts);
897
898 if (filter->state >= DMXDEV_STATE_GO)
899 return dvb_dmxdev_start_feed(dmxdev, filter, feed);
900
901 return 0;
902 }
903
dvb_dmxdev_remove_pid(struct dmxdev * dmxdev,struct dmxdev_filter * filter,u16 pid)904 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
905 struct dmxdev_filter *filter, u16 pid)
906 {
907 struct dmxdev_feed *feed, *tmp;
908
909 if ((filter->type != DMXDEV_TYPE_PES) ||
910 (filter->state < DMXDEV_STATE_SET))
911 return -EINVAL;
912
913 list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
914 if ((feed->pid == pid) && (feed->ts != NULL)) {
915 feed->ts->stop_filtering(feed->ts);
916 filter->dev->demux->release_ts_feed(filter->dev->demux,
917 feed->ts);
918 list_del(&feed->next);
919 kfree(feed);
920 }
921 }
922
923 return 0;
924 }
925
dvb_dmxdev_filter_set(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter,struct dmx_sct_filter_params * params)926 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
927 struct dmxdev_filter *dmxdevfilter,
928 struct dmx_sct_filter_params *params)
929 {
930 dprintk("%s: PID=0x%04x, flags=%02x, timeout=%d\n",
931 __func__, params->pid, params->flags, params->timeout);
932
933 dvb_dmxdev_filter_stop(dmxdevfilter);
934
935 dmxdevfilter->type = DMXDEV_TYPE_SEC;
936 memcpy(&dmxdevfilter->params.sec,
937 params, sizeof(struct dmx_sct_filter_params));
938 invert_mode(&dmxdevfilter->params.sec.filter);
939 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
940
941 if (params->flags & DMX_IMMEDIATE_START)
942 return dvb_dmxdev_filter_start(dmxdevfilter);
943
944 return 0;
945 }
946
dvb_dmxdev_pes_filter_set(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter,struct dmx_pes_filter_params * params)947 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
948 struct dmxdev_filter *dmxdevfilter,
949 struct dmx_pes_filter_params *params)
950 {
951 int ret;
952
953 dvb_dmxdev_filter_stop(dmxdevfilter);
954 dvb_dmxdev_filter_reset(dmxdevfilter);
955
956 if ((unsigned int)params->pes_type > DMX_PES_OTHER)
957 return -EINVAL;
958
959 dmxdevfilter->type = DMXDEV_TYPE_PES;
960 memcpy(&dmxdevfilter->params, params,
961 sizeof(struct dmx_pes_filter_params));
962 INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
963
964 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
965
966 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
967 dmxdevfilter->params.pes.pid);
968 if (ret < 0)
969 return ret;
970
971 if (params->flags & DMX_IMMEDIATE_START)
972 return dvb_dmxdev_filter_start(dmxdevfilter);
973
974 return 0;
975 }
976
dvb_dmxdev_read_sec(struct dmxdev_filter * dfil,struct file * file,char __user * buf,size_t count,loff_t * ppos)977 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
978 struct file *file, char __user *buf,
979 size_t count, loff_t *ppos)
980 {
981 int result, hcount;
982 int done = 0;
983
984 if (dfil->todo <= 0) {
985 hcount = 3 + dfil->todo;
986 if (hcount > count)
987 hcount = count;
988 result = dvb_dmxdev_buffer_read(&dfil->buffer,
989 file->f_flags & O_NONBLOCK,
990 buf, hcount, ppos);
991 if (result < 0) {
992 dfil->todo = 0;
993 return result;
994 }
995 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
996 return -EFAULT;
997 buf += result;
998 done = result;
999 count -= result;
1000 dfil->todo -= result;
1001 if (dfil->todo > -3)
1002 return done;
1003 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
1004 if (!count)
1005 return done;
1006 }
1007 if (count > dfil->todo)
1008 count = dfil->todo;
1009 result = dvb_dmxdev_buffer_read(&dfil->buffer,
1010 file->f_flags & O_NONBLOCK,
1011 buf, count, ppos);
1012 if (result < 0)
1013 return result;
1014 dfil->todo -= result;
1015 return (result + done);
1016 }
1017
1018 static ssize_t
dvb_demux_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)1019 dvb_demux_read(struct file *file, char __user *buf, size_t count,
1020 loff_t *ppos)
1021 {
1022 struct dmxdev_filter *dmxdevfilter = file->private_data;
1023 int ret;
1024
1025 if (mutex_lock_interruptible(&dmxdevfilter->mutex))
1026 return -ERESTARTSYS;
1027
1028 if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
1029 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
1030 else
1031 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
1032 file->f_flags & O_NONBLOCK,
1033 buf, count, ppos);
1034
1035 mutex_unlock(&dmxdevfilter->mutex);
1036 return ret;
1037 }
1038
dvb_demux_do_ioctl(struct file * file,unsigned int cmd,void * parg)1039 static int dvb_demux_do_ioctl(struct file *file,
1040 unsigned int cmd, void *parg)
1041 {
1042 struct dmxdev_filter *dmxdevfilter = file->private_data;
1043 struct dmxdev *dmxdev = dmxdevfilter->dev;
1044 unsigned long arg = (unsigned long)parg;
1045 int ret = 0;
1046
1047 if (mutex_lock_interruptible(&dmxdev->mutex))
1048 return -ERESTARTSYS;
1049
1050 switch (cmd) {
1051 case DMX_START:
1052 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1053 mutex_unlock(&dmxdev->mutex);
1054 return -ERESTARTSYS;
1055 }
1056 if (dmxdevfilter->state < DMXDEV_STATE_SET)
1057 ret = -EINVAL;
1058 else
1059 ret = dvb_dmxdev_filter_start(dmxdevfilter);
1060 mutex_unlock(&dmxdevfilter->mutex);
1061 break;
1062
1063 case DMX_STOP:
1064 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1065 mutex_unlock(&dmxdev->mutex);
1066 return -ERESTARTSYS;
1067 }
1068 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
1069 mutex_unlock(&dmxdevfilter->mutex);
1070 break;
1071
1072 case DMX_SET_FILTER:
1073 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1074 mutex_unlock(&dmxdev->mutex);
1075 return -ERESTARTSYS;
1076 }
1077 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1078 mutex_unlock(&dmxdevfilter->mutex);
1079 break;
1080
1081 case DMX_SET_PES_FILTER:
1082 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1083 mutex_unlock(&dmxdev->mutex);
1084 return -ERESTARTSYS;
1085 }
1086 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1087 mutex_unlock(&dmxdevfilter->mutex);
1088 break;
1089
1090 case DMX_SET_BUFFER_SIZE:
1091 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1092 mutex_unlock(&dmxdev->mutex);
1093 return -ERESTARTSYS;
1094 }
1095 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1096 mutex_unlock(&dmxdevfilter->mutex);
1097 break;
1098
1099 case DMX_GET_PES_PIDS:
1100 if (!dmxdev->demux->get_pes_pids) {
1101 ret = -EINVAL;
1102 break;
1103 }
1104 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1105 break;
1106
1107 case DMX_GET_STC:
1108 if (!dmxdev->demux->get_stc) {
1109 ret = -EINVAL;
1110 break;
1111 }
1112 ret = dmxdev->demux->get_stc(dmxdev->demux,
1113 ((struct dmx_stc *)parg)->num,
1114 &((struct dmx_stc *)parg)->stc,
1115 &((struct dmx_stc *)parg)->base);
1116 break;
1117
1118 case DMX_ADD_PID:
1119 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1120 ret = -ERESTARTSYS;
1121 break;
1122 }
1123 ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1124 mutex_unlock(&dmxdevfilter->mutex);
1125 break;
1126
1127 case DMX_REMOVE_PID:
1128 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1129 ret = -ERESTARTSYS;
1130 break;
1131 }
1132 ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1133 mutex_unlock(&dmxdevfilter->mutex);
1134 break;
1135
1136 #ifdef CONFIG_DVB_MMAP
1137 case DMX_REQBUFS:
1138 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1139 mutex_unlock(&dmxdev->mutex);
1140 return -ERESTARTSYS;
1141 }
1142 ret = dvb_vb2_reqbufs(&dmxdevfilter->vb2_ctx, parg);
1143 mutex_unlock(&dmxdevfilter->mutex);
1144 break;
1145
1146 case DMX_QUERYBUF:
1147 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1148 mutex_unlock(&dmxdev->mutex);
1149 return -ERESTARTSYS;
1150 }
1151 ret = dvb_vb2_querybuf(&dmxdevfilter->vb2_ctx, parg);
1152 mutex_unlock(&dmxdevfilter->mutex);
1153 break;
1154
1155 case DMX_EXPBUF:
1156 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1157 mutex_unlock(&dmxdev->mutex);
1158 return -ERESTARTSYS;
1159 }
1160 ret = dvb_vb2_expbuf(&dmxdevfilter->vb2_ctx, parg);
1161 mutex_unlock(&dmxdevfilter->mutex);
1162 break;
1163
1164 case DMX_QBUF:
1165 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1166 mutex_unlock(&dmxdev->mutex);
1167 return -ERESTARTSYS;
1168 }
1169 ret = dvb_vb2_qbuf(&dmxdevfilter->vb2_ctx, parg);
1170 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1171 ret = dvb_vb2_stream_on(&dmxdevfilter->vb2_ctx);
1172 mutex_unlock(&dmxdevfilter->mutex);
1173 break;
1174
1175 case DMX_DQBUF:
1176 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1177 mutex_unlock(&dmxdev->mutex);
1178 return -ERESTARTSYS;
1179 }
1180 ret = dvb_vb2_dqbuf(&dmxdevfilter->vb2_ctx, parg);
1181 mutex_unlock(&dmxdevfilter->mutex);
1182 break;
1183 #endif
1184 default:
1185 ret = -ENOTTY;
1186 break;
1187 }
1188 mutex_unlock(&dmxdev->mutex);
1189 return ret;
1190 }
1191
dvb_demux_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1192 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1193 unsigned long arg)
1194 {
1195 return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1196 }
1197
dvb_demux_poll(struct file * file,poll_table * wait)1198 static __poll_t dvb_demux_poll(struct file *file, poll_table *wait)
1199 {
1200 struct dmxdev_filter *dmxdevfilter = file->private_data;
1201 __poll_t mask = 0;
1202
1203 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1204
1205 if ((!dmxdevfilter) || dmxdevfilter->dev->exit)
1206 return EPOLLERR;
1207 if (dvb_vb2_is_streaming(&dmxdevfilter->vb2_ctx))
1208 return dvb_vb2_poll(&dmxdevfilter->vb2_ctx, file, wait);
1209
1210 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1211 dmxdevfilter->state != DMXDEV_STATE_DONE &&
1212 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1213 return 0;
1214
1215 if (dmxdevfilter->buffer.error)
1216 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1217
1218 if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1219 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1220
1221 return mask;
1222 }
1223
1224 #ifdef CONFIG_DVB_MMAP
dvb_demux_mmap(struct file * file,struct vm_area_struct * vma)1225 static int dvb_demux_mmap(struct file *file, struct vm_area_struct *vma)
1226 {
1227 struct dmxdev_filter *dmxdevfilter = file->private_data;
1228 struct dmxdev *dmxdev = dmxdevfilter->dev;
1229 int ret;
1230
1231 if (!dmxdev->may_do_mmap)
1232 return -ENOTTY;
1233
1234 if (mutex_lock_interruptible(&dmxdev->mutex))
1235 return -ERESTARTSYS;
1236
1237 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1238 mutex_unlock(&dmxdev->mutex);
1239 return -ERESTARTSYS;
1240 }
1241 ret = dvb_vb2_mmap(&dmxdevfilter->vb2_ctx, vma);
1242
1243 mutex_unlock(&dmxdevfilter->mutex);
1244 mutex_unlock(&dmxdev->mutex);
1245
1246 return ret;
1247 }
1248 #endif
1249
dvb_demux_release(struct inode * inode,struct file * file)1250 static int dvb_demux_release(struct inode *inode, struct file *file)
1251 {
1252 struct dmxdev_filter *dmxdevfilter = file->private_data;
1253 struct dmxdev *dmxdev = dmxdevfilter->dev;
1254 int ret;
1255
1256 ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1257
1258 mutex_lock(&dmxdev->mutex);
1259 dmxdev->dvbdev->users--;
1260 if (dmxdev->dvbdev->users == 1 && dmxdev->exit == 1) {
1261 mutex_unlock(&dmxdev->mutex);
1262 wake_up(&dmxdev->dvbdev->wait_queue);
1263 } else
1264 mutex_unlock(&dmxdev->mutex);
1265
1266 return ret;
1267 }
1268
1269 static const struct file_operations dvb_demux_fops = {
1270 .owner = THIS_MODULE,
1271 .read = dvb_demux_read,
1272 .unlocked_ioctl = dvb_demux_ioctl,
1273 .compat_ioctl = dvb_demux_ioctl,
1274 .open = dvb_demux_open,
1275 .release = dvb_demux_release,
1276 .poll = dvb_demux_poll,
1277 .llseek = default_llseek,
1278 #ifdef CONFIG_DVB_MMAP
1279 .mmap = dvb_demux_mmap,
1280 #endif
1281 };
1282
1283 static const struct dvb_device dvbdev_demux = {
1284 .priv = NULL,
1285 .users = 1,
1286 .writers = 1,
1287 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1288 .name = "dvb-demux",
1289 #endif
1290 .fops = &dvb_demux_fops
1291 };
1292
dvb_dvr_do_ioctl(struct file * file,unsigned int cmd,void * parg)1293 static int dvb_dvr_do_ioctl(struct file *file,
1294 unsigned int cmd, void *parg)
1295 {
1296 struct dvb_device *dvbdev = file->private_data;
1297 struct dmxdev *dmxdev = dvbdev->priv;
1298 unsigned long arg = (unsigned long)parg;
1299 int ret;
1300
1301 if (mutex_lock_interruptible(&dmxdev->mutex))
1302 return -ERESTARTSYS;
1303
1304 switch (cmd) {
1305 case DMX_SET_BUFFER_SIZE:
1306 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1307 break;
1308
1309 #ifdef CONFIG_DVB_MMAP
1310 case DMX_REQBUFS:
1311 ret = dvb_vb2_reqbufs(&dmxdev->dvr_vb2_ctx, parg);
1312 break;
1313
1314 case DMX_QUERYBUF:
1315 ret = dvb_vb2_querybuf(&dmxdev->dvr_vb2_ctx, parg);
1316 break;
1317
1318 case DMX_EXPBUF:
1319 ret = dvb_vb2_expbuf(&dmxdev->dvr_vb2_ctx, parg);
1320 break;
1321
1322 case DMX_QBUF:
1323 ret = dvb_vb2_qbuf(&dmxdev->dvr_vb2_ctx, parg);
1324 if (ret == 0 && !dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1325 ret = dvb_vb2_stream_on(&dmxdev->dvr_vb2_ctx);
1326 break;
1327
1328 case DMX_DQBUF:
1329 ret = dvb_vb2_dqbuf(&dmxdev->dvr_vb2_ctx, parg);
1330 break;
1331 #endif
1332 default:
1333 ret = -ENOTTY;
1334 break;
1335 }
1336 mutex_unlock(&dmxdev->mutex);
1337 return ret;
1338 }
1339
dvb_dvr_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1340 static long dvb_dvr_ioctl(struct file *file,
1341 unsigned int cmd, unsigned long arg)
1342 {
1343 return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1344 }
1345
dvb_dvr_poll(struct file * file,poll_table * wait)1346 static __poll_t dvb_dvr_poll(struct file *file, poll_table *wait)
1347 {
1348 struct dvb_device *dvbdev = file->private_data;
1349 struct dmxdev *dmxdev = dvbdev->priv;
1350 __poll_t mask = 0;
1351
1352 dprintk("%s\n", __func__);
1353
1354 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1355
1356 if (dmxdev->exit)
1357 return EPOLLERR;
1358 if (dvb_vb2_is_streaming(&dmxdev->dvr_vb2_ctx))
1359 return dvb_vb2_poll(&dmxdev->dvr_vb2_ctx, file, wait);
1360
1361 if (((file->f_flags & O_ACCMODE) == O_RDONLY) ||
1362 dmxdev->may_do_mmap) {
1363 if (dmxdev->dvr_buffer.error)
1364 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI | EPOLLERR);
1365
1366 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1367 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLPRI);
1368 } else
1369 mask |= (EPOLLOUT | EPOLLWRNORM | EPOLLPRI);
1370
1371 return mask;
1372 }
1373
1374 #ifdef CONFIG_DVB_MMAP
dvb_dvr_mmap(struct file * file,struct vm_area_struct * vma)1375 static int dvb_dvr_mmap(struct file *file, struct vm_area_struct *vma)
1376 {
1377 struct dvb_device *dvbdev = file->private_data;
1378 struct dmxdev *dmxdev = dvbdev->priv;
1379 int ret;
1380
1381 if (!dmxdev->may_do_mmap)
1382 return -ENOTTY;
1383
1384 if (dmxdev->exit)
1385 return -ENODEV;
1386
1387 if (mutex_lock_interruptible(&dmxdev->mutex))
1388 return -ERESTARTSYS;
1389
1390 ret = dvb_vb2_mmap(&dmxdev->dvr_vb2_ctx, vma);
1391 mutex_unlock(&dmxdev->mutex);
1392 return ret;
1393 }
1394 #endif
1395
1396 static const struct file_operations dvb_dvr_fops = {
1397 .owner = THIS_MODULE,
1398 .read = dvb_dvr_read,
1399 .write = dvb_dvr_write,
1400 .unlocked_ioctl = dvb_dvr_ioctl,
1401 .open = dvb_dvr_open,
1402 .release = dvb_dvr_release,
1403 .poll = dvb_dvr_poll,
1404 .llseek = default_llseek,
1405 #ifdef CONFIG_DVB_MMAP
1406 .mmap = dvb_dvr_mmap,
1407 #endif
1408 };
1409
1410 static const struct dvb_device dvbdev_dvr = {
1411 .priv = NULL,
1412 .readers = 1,
1413 .users = 1,
1414 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1415 .name = "dvb-dvr",
1416 #endif
1417 .fops = &dvb_dvr_fops
1418 };
dvb_dmxdev_init(struct dmxdev * dmxdev,struct dvb_adapter * dvb_adapter)1419 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1420 {
1421 int i, ret;
1422
1423 if (dmxdev->demux->open(dmxdev->demux) < 0)
1424 return -EUSERS;
1425
1426 dmxdev->filter = vmalloc(array_size(sizeof(struct dmxdev_filter),
1427 dmxdev->filternum));
1428 if (!dmxdev->filter)
1429 return -ENOMEM;
1430
1431 mutex_init(&dmxdev->mutex);
1432 spin_lock_init(&dmxdev->lock);
1433 for (i = 0; i < dmxdev->filternum; i++) {
1434 dmxdev->filter[i].dev = dmxdev;
1435 dmxdev->filter[i].buffer.data = NULL;
1436 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1437 DMXDEV_STATE_FREE);
1438 }
1439
1440 ret = dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1441 DVB_DEVICE_DEMUX, dmxdev->filternum);
1442 if (ret < 0)
1443 goto err_register_dvbdev;
1444
1445 ret = dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1446 dmxdev, DVB_DEVICE_DVR, dmxdev->filternum);
1447 if (ret < 0)
1448 goto err_register_dvr_dvbdev;
1449
1450 dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1451
1452 return 0;
1453
1454 err_register_dvr_dvbdev:
1455 dvb_unregister_device(dmxdev->dvbdev);
1456 err_register_dvbdev:
1457 vfree(dmxdev->filter);
1458 dmxdev->filter = NULL;
1459 return ret;
1460 }
1461
1462 EXPORT_SYMBOL(dvb_dmxdev_init);
1463
dvb_dmxdev_release(struct dmxdev * dmxdev)1464 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1465 {
1466 mutex_lock(&dmxdev->mutex);
1467 dmxdev->exit = 1;
1468 mutex_unlock(&dmxdev->mutex);
1469
1470 if (dmxdev->dvbdev->users > 1) {
1471 wait_event(dmxdev->dvbdev->wait_queue,
1472 dmxdev->dvbdev->users == 1);
1473 }
1474 if (dmxdev->dvr_dvbdev->users > 1) {
1475 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1476 dmxdev->dvr_dvbdev->users == 1);
1477 }
1478
1479 dvb_unregister_device(dmxdev->dvbdev);
1480 dvb_unregister_device(dmxdev->dvr_dvbdev);
1481
1482 vfree(dmxdev->filter);
1483 dmxdev->filter = NULL;
1484 dmxdev->demux->close(dmxdev->demux);
1485 }
1486
1487 EXPORT_SYMBOL(dvb_dmxdev_release);
1488