• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  */
22 
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/vmalloc.h>
27 #include <linux/module.h>
28 #include <linux/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
32 #include "dmxdev.h"
33 
34 static int debug;
35 
36 module_param(debug, int, 0644);
37 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
38 
39 #define dprintk	if (debug) printk
40 
dvb_dmxdev_buffer_write(struct dvb_ringbuffer * buf,const u8 * src,size_t len)41 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
42 				   const u8 *src, size_t len)
43 {
44 	ssize_t free;
45 
46 	if (!len)
47 		return 0;
48 	if (!buf->data)
49 		return 0;
50 
51 	free = dvb_ringbuffer_free(buf);
52 	if (len > free) {
53 		dprintk("dmxdev: buffer overflow\n");
54 		return -EOVERFLOW;
55 	}
56 
57 	return dvb_ringbuffer_write(buf, src, len);
58 }
59 
dvb_dmxdev_buffer_read(struct dvb_ringbuffer * src,int non_blocking,char __user * buf,size_t count,loff_t * ppos)60 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
61 				      int non_blocking, char __user *buf,
62 				      size_t count, loff_t *ppos)
63 {
64 	size_t todo;
65 	ssize_t avail;
66 	ssize_t ret = 0;
67 
68 	if (!src->data)
69 		return 0;
70 
71 	if (src->error) {
72 		ret = src->error;
73 		dvb_ringbuffer_flush(src);
74 		return ret;
75 	}
76 
77 	for (todo = count; todo > 0; todo -= ret) {
78 		if (non_blocking && dvb_ringbuffer_empty(src)) {
79 			ret = -EWOULDBLOCK;
80 			break;
81 		}
82 
83 		ret = wait_event_interruptible(src->queue,
84 					       !dvb_ringbuffer_empty(src) ||
85 					       (src->error != 0));
86 		if (ret < 0)
87 			break;
88 
89 		if (src->error) {
90 			ret = src->error;
91 			dvb_ringbuffer_flush(src);
92 			break;
93 		}
94 
95 		avail = dvb_ringbuffer_avail(src);
96 		if (avail > todo)
97 			avail = todo;
98 
99 		ret = dvb_ringbuffer_read_user(src, buf, avail);
100 		if (ret < 0)
101 			break;
102 
103 		buf += ret;
104 	}
105 
106 	return (count - todo) ? (count - todo) : ret;
107 }
108 
get_fe(struct dmx_demux * demux,int type)109 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
110 {
111 	struct list_head *head, *pos;
112 
113 	head = demux->get_frontends(demux);
114 	if (!head)
115 		return NULL;
116 	list_for_each(pos, head)
117 		if (DMX_FE_ENTRY(pos)->source == type)
118 			return DMX_FE_ENTRY(pos);
119 
120 	return NULL;
121 }
122 
dvb_dvr_open(struct inode * inode,struct file * file)123 static int dvb_dvr_open(struct inode *inode, struct file *file)
124 {
125 	struct dvb_device *dvbdev = file->private_data;
126 	struct dmxdev *dmxdev = dvbdev->priv;
127 	struct dmx_frontend *front;
128 
129 	dprintk("function : %s\n", __func__);
130 
131 	if (mutex_lock_interruptible(&dmxdev->mutex))
132 		return -ERESTARTSYS;
133 
134 	if (dmxdev->exit) {
135 		mutex_unlock(&dmxdev->mutex);
136 		return -ENODEV;
137 	}
138 
139 	if ((file->f_flags & O_ACCMODE) == O_RDWR) {
140 		if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
141 			mutex_unlock(&dmxdev->mutex);
142 			return -EOPNOTSUPP;
143 		}
144 	}
145 
146 	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
147 		void *mem;
148 		if (!dvbdev->readers) {
149 			mutex_unlock(&dmxdev->mutex);
150 			return -EBUSY;
151 		}
152 		mem = vmalloc(DVR_BUFFER_SIZE);
153 		if (!mem) {
154 			mutex_unlock(&dmxdev->mutex);
155 			return -ENOMEM;
156 		}
157 		dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
158 		dvbdev->readers--;
159 	}
160 
161 	if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
162 		dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
163 
164 		if (!dmxdev->demux->write) {
165 			mutex_unlock(&dmxdev->mutex);
166 			return -EOPNOTSUPP;
167 		}
168 
169 		front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
170 
171 		if (!front) {
172 			mutex_unlock(&dmxdev->mutex);
173 			return -EINVAL;
174 		}
175 		dmxdev->demux->disconnect_frontend(dmxdev->demux);
176 		dmxdev->demux->connect_frontend(dmxdev->demux, front);
177 	}
178 	dvbdev->users++;
179 	mutex_unlock(&dmxdev->mutex);
180 	return 0;
181 }
182 
dvb_dvr_release(struct inode * inode,struct file * file)183 static int dvb_dvr_release(struct inode *inode, struct file *file)
184 {
185 	struct dvb_device *dvbdev = file->private_data;
186 	struct dmxdev *dmxdev = dvbdev->priv;
187 
188 	mutex_lock(&dmxdev->mutex);
189 
190 	if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
191 		dmxdev->demux->disconnect_frontend(dmxdev->demux);
192 		dmxdev->demux->connect_frontend(dmxdev->demux,
193 						dmxdev->dvr_orig_fe);
194 	}
195 	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
196 		dvbdev->readers++;
197 		if (dmxdev->dvr_buffer.data) {
198 			void *mem = dmxdev->dvr_buffer.data;
199 			mb();
200 			spin_lock_irq(&dmxdev->lock);
201 			dmxdev->dvr_buffer.data = NULL;
202 			spin_unlock_irq(&dmxdev->lock);
203 			vfree(mem);
204 		}
205 	}
206 	/* TODO */
207 	dvbdev->users--;
208 	if (dvbdev->users == 1 && dmxdev->exit == 1) {
209 		fops_put(file->f_op);
210 		file->f_op = NULL;
211 		mutex_unlock(&dmxdev->mutex);
212 		wake_up(&dvbdev->wait_queue);
213 	} else
214 		mutex_unlock(&dmxdev->mutex);
215 
216 	return 0;
217 }
218 
dvb_dvr_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)219 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
220 			     size_t count, loff_t *ppos)
221 {
222 	struct dvb_device *dvbdev = file->private_data;
223 	struct dmxdev *dmxdev = dvbdev->priv;
224 	int ret;
225 
226 	if (!dmxdev->demux->write)
227 		return -EOPNOTSUPP;
228 	if ((file->f_flags & O_ACCMODE) != O_WRONLY)
229 		return -EINVAL;
230 	if (mutex_lock_interruptible(&dmxdev->mutex))
231 		return -ERESTARTSYS;
232 
233 	if (dmxdev->exit) {
234 		mutex_unlock(&dmxdev->mutex);
235 		return -ENODEV;
236 	}
237 	ret = dmxdev->demux->write(dmxdev->demux, buf, count);
238 	mutex_unlock(&dmxdev->mutex);
239 	return ret;
240 }
241 
dvb_dvr_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)242 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
243 			    loff_t *ppos)
244 {
245 	struct dvb_device *dvbdev = file->private_data;
246 	struct dmxdev *dmxdev = dvbdev->priv;
247 
248 	if (dmxdev->exit)
249 		return -ENODEV;
250 
251 	return dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
252 				      file->f_flags & O_NONBLOCK,
253 				      buf, count, ppos);
254 }
255 
dvb_dvr_set_buffer_size(struct dmxdev * dmxdev,unsigned long size)256 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
257 				      unsigned long size)
258 {
259 	struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
260 	void *newmem;
261 	void *oldmem;
262 
263 	dprintk("function : %s\n", __func__);
264 
265 	if (buf->size == size)
266 		return 0;
267 	if (!size)
268 		return -EINVAL;
269 
270 	newmem = vmalloc(size);
271 	if (!newmem)
272 		return -ENOMEM;
273 
274 	oldmem = buf->data;
275 
276 	spin_lock_irq(&dmxdev->lock);
277 	buf->data = newmem;
278 	buf->size = size;
279 
280 	/* reset and not flush in case the buffer shrinks */
281 	dvb_ringbuffer_reset(buf);
282 	spin_unlock_irq(&dmxdev->lock);
283 
284 	vfree(oldmem);
285 
286 	return 0;
287 }
288 
dvb_dmxdev_filter_state_set(struct dmxdev_filter * dmxdevfilter,int state)289 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
290 					       *dmxdevfilter, int state)
291 {
292 	spin_lock_irq(&dmxdevfilter->dev->lock);
293 	dmxdevfilter->state = state;
294 	spin_unlock_irq(&dmxdevfilter->dev->lock);
295 }
296 
dvb_dmxdev_set_buffer_size(struct dmxdev_filter * dmxdevfilter,unsigned long size)297 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
298 				      unsigned long size)
299 {
300 	struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
301 	void *newmem;
302 	void *oldmem;
303 
304 	if (buf->size == size)
305 		return 0;
306 	if (!size)
307 		return -EINVAL;
308 	if (dmxdevfilter->state >= DMXDEV_STATE_GO)
309 		return -EBUSY;
310 
311 	newmem = vmalloc(size);
312 	if (!newmem)
313 		return -ENOMEM;
314 
315 	oldmem = buf->data;
316 
317 	spin_lock_irq(&dmxdevfilter->dev->lock);
318 	buf->data = newmem;
319 	buf->size = size;
320 
321 	/* reset and not flush in case the buffer shrinks */
322 	dvb_ringbuffer_reset(buf);
323 	spin_unlock_irq(&dmxdevfilter->dev->lock);
324 
325 	vfree(oldmem);
326 
327 	return 0;
328 }
329 
dvb_dmxdev_filter_timeout(unsigned long data)330 static void dvb_dmxdev_filter_timeout(unsigned long data)
331 {
332 	struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
333 
334 	dmxdevfilter->buffer.error = -ETIMEDOUT;
335 	spin_lock_irq(&dmxdevfilter->dev->lock);
336 	dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
337 	spin_unlock_irq(&dmxdevfilter->dev->lock);
338 	wake_up(&dmxdevfilter->buffer.queue);
339 }
340 
dvb_dmxdev_filter_timer(struct dmxdev_filter * dmxdevfilter)341 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
342 {
343 	struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
344 
345 	del_timer(&dmxdevfilter->timer);
346 	if (para->timeout) {
347 		dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
348 		dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
349 		dmxdevfilter->timer.expires =
350 		    jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
351 		add_timer(&dmxdevfilter->timer);
352 	}
353 }
354 
dvb_dmxdev_section_callback(const u8 * buffer1,size_t buffer1_len,const u8 * buffer2,size_t buffer2_len,struct dmx_section_filter * filter,enum dmx_success success)355 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
356 				       const u8 *buffer2, size_t buffer2_len,
357 				       struct dmx_section_filter *filter,
358 				       enum dmx_success success)
359 {
360 	struct dmxdev_filter *dmxdevfilter = filter->priv;
361 	int ret;
362 
363 	if (dmxdevfilter->buffer.error) {
364 		wake_up(&dmxdevfilter->buffer.queue);
365 		return 0;
366 	}
367 	spin_lock(&dmxdevfilter->dev->lock);
368 	if (dmxdevfilter->state != DMXDEV_STATE_GO) {
369 		spin_unlock(&dmxdevfilter->dev->lock);
370 		return 0;
371 	}
372 	del_timer(&dmxdevfilter->timer);
373 	dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
374 		buffer1[0], buffer1[1],
375 		buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
376 	ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
377 				      buffer1_len);
378 	if (ret == buffer1_len) {
379 		ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
380 					      buffer2_len);
381 	}
382 	if (ret < 0) {
383 		dvb_ringbuffer_flush(&dmxdevfilter->buffer);
384 		dmxdevfilter->buffer.error = ret;
385 	}
386 	if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
387 		dmxdevfilter->state = DMXDEV_STATE_DONE;
388 	spin_unlock(&dmxdevfilter->dev->lock);
389 	wake_up(&dmxdevfilter->buffer.queue);
390 	return 0;
391 }
392 
dvb_dmxdev_ts_callback(const u8 * buffer1,size_t buffer1_len,const u8 * buffer2,size_t buffer2_len,struct dmx_ts_feed * feed,enum dmx_success success)393 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
394 				  const u8 *buffer2, size_t buffer2_len,
395 				  struct dmx_ts_feed *feed,
396 				  enum dmx_success success)
397 {
398 	struct dmxdev_filter *dmxdevfilter = feed->priv;
399 	struct dvb_ringbuffer *buffer;
400 	int ret;
401 
402 	spin_lock(&dmxdevfilter->dev->lock);
403 	if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
404 		spin_unlock(&dmxdevfilter->dev->lock);
405 		return 0;
406 	}
407 
408 	if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
409 	    || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
410 		buffer = &dmxdevfilter->buffer;
411 	else
412 		buffer = &dmxdevfilter->dev->dvr_buffer;
413 	if (buffer->error) {
414 		spin_unlock(&dmxdevfilter->dev->lock);
415 		wake_up(&buffer->queue);
416 		return 0;
417 	}
418 	ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
419 	if (ret == buffer1_len)
420 		ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
421 	if (ret < 0) {
422 		dvb_ringbuffer_flush(buffer);
423 		buffer->error = ret;
424 	}
425 	spin_unlock(&dmxdevfilter->dev->lock);
426 	wake_up(&buffer->queue);
427 	return 0;
428 }
429 
430 /* stop feed but only mark the specified filter as stopped (state set) */
dvb_dmxdev_feed_stop(struct dmxdev_filter * dmxdevfilter)431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
432 {
433 	struct dmxdev_feed *feed;
434 
435 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
436 
437 	switch (dmxdevfilter->type) {
438 	case DMXDEV_TYPE_SEC:
439 		del_timer(&dmxdevfilter->timer);
440 		dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
441 		break;
442 	case DMXDEV_TYPE_PES:
443 		list_for_each_entry(feed, &dmxdevfilter->feed.ts, next)
444 			feed->ts->stop_filtering(feed->ts);
445 		break;
446 	default:
447 		return -EINVAL;
448 	}
449 	return 0;
450 }
451 
452 /* start feed associated with the specified filter */
dvb_dmxdev_feed_start(struct dmxdev_filter * filter)453 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
454 {
455 	struct dmxdev_feed *feed;
456 	int ret;
457 
458 	dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
459 
460 	switch (filter->type) {
461 	case DMXDEV_TYPE_SEC:
462 		return filter->feed.sec->start_filtering(filter->feed.sec);
463 	case DMXDEV_TYPE_PES:
464 		list_for_each_entry(feed, &filter->feed.ts, next) {
465 			ret = feed->ts->start_filtering(feed->ts);
466 			if (ret < 0) {
467 				dvb_dmxdev_feed_stop(filter);
468 				return ret;
469 			}
470 		}
471 		break;
472 	default:
473 		return -EINVAL;
474 	}
475 
476 	return 0;
477 }
478 
479 /* restart section feed if it has filters left associated with it,
480    otherwise release the feed */
dvb_dmxdev_feed_restart(struct dmxdev_filter * filter)481 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
482 {
483 	int i;
484 	struct dmxdev *dmxdev = filter->dev;
485 	u16 pid = filter->params.sec.pid;
486 
487 	for (i = 0; i < dmxdev->filternum; i++)
488 		if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
489 		    dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
490 		    dmxdev->filter[i].params.sec.pid == pid) {
491 			dvb_dmxdev_feed_start(&dmxdev->filter[i]);
492 			return 0;
493 		}
494 
495 	filter->dev->demux->release_section_feed(dmxdev->demux,
496 						 filter->feed.sec);
497 
498 	return 0;
499 }
500 
dvb_dmxdev_filter_stop(struct dmxdev_filter * dmxdevfilter)501 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
502 {
503 	struct dmxdev_feed *feed;
504 	struct dmx_demux *demux;
505 
506 	if (dmxdevfilter->state < DMXDEV_STATE_GO)
507 		return 0;
508 
509 	switch (dmxdevfilter->type) {
510 	case DMXDEV_TYPE_SEC:
511 		if (!dmxdevfilter->feed.sec)
512 			break;
513 		dvb_dmxdev_feed_stop(dmxdevfilter);
514 		if (dmxdevfilter->filter.sec)
515 			dmxdevfilter->feed.sec->
516 			    release_filter(dmxdevfilter->feed.sec,
517 					   dmxdevfilter->filter.sec);
518 		dvb_dmxdev_feed_restart(dmxdevfilter);
519 		dmxdevfilter->feed.sec = NULL;
520 		break;
521 	case DMXDEV_TYPE_PES:
522 		dvb_dmxdev_feed_stop(dmxdevfilter);
523 		demux = dmxdevfilter->dev->demux;
524 		list_for_each_entry(feed, &dmxdevfilter->feed.ts, next) {
525 			demux->release_ts_feed(demux, feed->ts);
526 			feed->ts = NULL;
527 		}
528 		break;
529 	default:
530 		if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
531 			return 0;
532 		return -EINVAL;
533 	}
534 
535 	dvb_ringbuffer_flush(&dmxdevfilter->buffer);
536 	return 0;
537 }
538 
dvb_dmxdev_delete_pids(struct dmxdev_filter * dmxdevfilter)539 static void dvb_dmxdev_delete_pids(struct dmxdev_filter *dmxdevfilter)
540 {
541 	struct dmxdev_feed *feed, *tmp;
542 
543 	/* delete all PIDs */
544 	list_for_each_entry_safe(feed, tmp, &dmxdevfilter->feed.ts, next) {
545 		list_del(&feed->next);
546 		kfree(feed);
547 	}
548 
549 	BUG_ON(!list_empty(&dmxdevfilter->feed.ts));
550 }
551 
dvb_dmxdev_filter_reset(struct dmxdev_filter * dmxdevfilter)552 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
553 {
554 	if (dmxdevfilter->state < DMXDEV_STATE_SET)
555 		return 0;
556 
557 	if (dmxdevfilter->type == DMXDEV_TYPE_PES)
558 		dvb_dmxdev_delete_pids(dmxdevfilter);
559 
560 	dmxdevfilter->type = DMXDEV_TYPE_NONE;
561 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
562 	return 0;
563 }
564 
dvb_dmxdev_start_feed(struct dmxdev * dmxdev,struct dmxdev_filter * filter,struct dmxdev_feed * feed)565 static int dvb_dmxdev_start_feed(struct dmxdev *dmxdev,
566 				 struct dmxdev_filter *filter,
567 				 struct dmxdev_feed *feed)
568 {
569 	struct timespec timeout = { 0 };
570 	struct dmx_pes_filter_params *para = &filter->params.pes;
571 	dmx_output_t otype;
572 	int ret;
573 	int ts_type;
574 	dmx_pes_type_t ts_pes;
575 	struct dmx_ts_feed *tsfeed;
576 
577 	feed->ts = NULL;
578 	otype = para->output;
579 
580 	ts_pes = para->pes_type;
581 
582 	if (ts_pes < DMX_PES_OTHER)
583 		ts_type = TS_DECODER;
584 	else
585 		ts_type = 0;
586 
587 	if (otype == DMX_OUT_TS_TAP)
588 		ts_type |= TS_PACKET;
589 	else if (otype == DMX_OUT_TSDEMUX_TAP)
590 		ts_type |= TS_PACKET | TS_DEMUX;
591 	else if (otype == DMX_OUT_TAP)
592 		ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
593 
594 	ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux, &feed->ts,
595 					      dvb_dmxdev_ts_callback);
596 	if (ret < 0)
597 		return ret;
598 
599 	tsfeed = feed->ts;
600 	tsfeed->priv = filter;
601 
602 	ret = tsfeed->set(tsfeed, feed->pid, ts_type, ts_pes, 32768, timeout);
603 	if (ret < 0) {
604 		dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
605 		return ret;
606 	}
607 
608 	ret = tsfeed->start_filtering(tsfeed);
609 	if (ret < 0) {
610 		dmxdev->demux->release_ts_feed(dmxdev->demux, tsfeed);
611 		return ret;
612 	}
613 
614 	return 0;
615 }
616 
dvb_dmxdev_filter_start(struct dmxdev_filter * filter)617 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
618 {
619 	struct dmxdev *dmxdev = filter->dev;
620 	struct dmxdev_feed *feed;
621 	void *mem;
622 	int ret, i;
623 
624 	if (filter->state < DMXDEV_STATE_SET)
625 		return -EINVAL;
626 
627 	if (filter->state >= DMXDEV_STATE_GO)
628 		dvb_dmxdev_filter_stop(filter);
629 
630 	if (!filter->buffer.data) {
631 		mem = vmalloc(filter->buffer.size);
632 		if (!mem)
633 			return -ENOMEM;
634 		spin_lock_irq(&filter->dev->lock);
635 		filter->buffer.data = mem;
636 		spin_unlock_irq(&filter->dev->lock);
637 	}
638 
639 	dvb_ringbuffer_flush(&filter->buffer);
640 
641 	switch (filter->type) {
642 	case DMXDEV_TYPE_SEC:
643 	{
644 		struct dmx_sct_filter_params *para = &filter->params.sec;
645 		struct dmx_section_filter **secfilter = &filter->filter.sec;
646 		struct dmx_section_feed **secfeed = &filter->feed.sec;
647 
648 		*secfilter = NULL;
649 		*secfeed = NULL;
650 
651 
652 		/* find active filter/feed with same PID */
653 		for (i = 0; i < dmxdev->filternum; i++) {
654 			if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
655 			    dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
656 			    dmxdev->filter[i].params.sec.pid == para->pid) {
657 				*secfeed = dmxdev->filter[i].feed.sec;
658 				break;
659 			}
660 		}
661 
662 		/* if no feed found, try to allocate new one */
663 		if (!*secfeed) {
664 			ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
665 								   secfeed,
666 								   dvb_dmxdev_section_callback);
667 			if (ret < 0) {
668 				printk("DVB (%s): could not alloc feed\n",
669 				       __func__);
670 				return ret;
671 			}
672 
673 			ret = (*secfeed)->set(*secfeed, para->pid, 32768,
674 					      (para->flags & DMX_CHECK_CRC) ? 1 : 0);
675 			if (ret < 0) {
676 				printk("DVB (%s): could not set feed\n",
677 				       __func__);
678 				dvb_dmxdev_feed_restart(filter);
679 				return ret;
680 			}
681 		} else {
682 			dvb_dmxdev_feed_stop(filter);
683 		}
684 
685 		ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
686 		if (ret < 0) {
687 			dvb_dmxdev_feed_restart(filter);
688 			filter->feed.sec->start_filtering(*secfeed);
689 			dprintk("could not get filter\n");
690 			return ret;
691 		}
692 
693 		(*secfilter)->priv = filter;
694 
695 		memcpy(&((*secfilter)->filter_value[3]),
696 		       &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
697 		memcpy(&(*secfilter)->filter_mask[3],
698 		       &para->filter.mask[1], DMX_FILTER_SIZE - 1);
699 		memcpy(&(*secfilter)->filter_mode[3],
700 		       &para->filter.mode[1], DMX_FILTER_SIZE - 1);
701 
702 		(*secfilter)->filter_value[0] = para->filter.filter[0];
703 		(*secfilter)->filter_mask[0] = para->filter.mask[0];
704 		(*secfilter)->filter_mode[0] = para->filter.mode[0];
705 		(*secfilter)->filter_mask[1] = 0;
706 		(*secfilter)->filter_mask[2] = 0;
707 
708 		filter->todo = 0;
709 
710 		ret = filter->feed.sec->start_filtering(filter->feed.sec);
711 		if (ret < 0)
712 			return ret;
713 
714 		dvb_dmxdev_filter_timer(filter);
715 		break;
716 	}
717 	case DMXDEV_TYPE_PES:
718 		list_for_each_entry(feed, &filter->feed.ts, next) {
719 			ret = dvb_dmxdev_start_feed(dmxdev, filter, feed);
720 			if (ret < 0) {
721 				dvb_dmxdev_filter_stop(filter);
722 				return ret;
723 			}
724 		}
725 		break;
726 	default:
727 		return -EINVAL;
728 	}
729 
730 	dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
731 	return 0;
732 }
733 
dvb_demux_open(struct inode * inode,struct file * file)734 static int dvb_demux_open(struct inode *inode, struct file *file)
735 {
736 	struct dvb_device *dvbdev = file->private_data;
737 	struct dmxdev *dmxdev = dvbdev->priv;
738 	int i;
739 	struct dmxdev_filter *dmxdevfilter;
740 
741 	if (!dmxdev->filter)
742 		return -EINVAL;
743 
744 	if (mutex_lock_interruptible(&dmxdev->mutex))
745 		return -ERESTARTSYS;
746 
747 	for (i = 0; i < dmxdev->filternum; i++)
748 		if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
749 			break;
750 
751 	if (i == dmxdev->filternum) {
752 		mutex_unlock(&dmxdev->mutex);
753 		return -EMFILE;
754 	}
755 
756 	dmxdevfilter = &dmxdev->filter[i];
757 	mutex_init(&dmxdevfilter->mutex);
758 	file->private_data = dmxdevfilter;
759 
760 	dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
761 	dmxdevfilter->type = DMXDEV_TYPE_NONE;
762 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
763 	init_timer(&dmxdevfilter->timer);
764 
765 	dvbdev->users++;
766 
767 	mutex_unlock(&dmxdev->mutex);
768 	return 0;
769 }
770 
dvb_dmxdev_filter_free(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter)771 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
772 				  struct dmxdev_filter *dmxdevfilter)
773 {
774 	mutex_lock(&dmxdev->mutex);
775 	mutex_lock(&dmxdevfilter->mutex);
776 
777 	dvb_dmxdev_filter_stop(dmxdevfilter);
778 	dvb_dmxdev_filter_reset(dmxdevfilter);
779 
780 	if (dmxdevfilter->buffer.data) {
781 		void *mem = dmxdevfilter->buffer.data;
782 
783 		spin_lock_irq(&dmxdev->lock);
784 		dmxdevfilter->buffer.data = NULL;
785 		spin_unlock_irq(&dmxdev->lock);
786 		vfree(mem);
787 	}
788 
789 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
790 	wake_up(&dmxdevfilter->buffer.queue);
791 	mutex_unlock(&dmxdevfilter->mutex);
792 	mutex_unlock(&dmxdev->mutex);
793 	return 0;
794 }
795 
invert_mode(dmx_filter_t * filter)796 static inline void invert_mode(dmx_filter_t *filter)
797 {
798 	int i;
799 
800 	for (i = 0; i < DMX_FILTER_SIZE; i++)
801 		filter->mode[i] ^= 0xff;
802 }
803 
dvb_dmxdev_add_pid(struct dmxdev * dmxdev,struct dmxdev_filter * filter,u16 pid)804 static int dvb_dmxdev_add_pid(struct dmxdev *dmxdev,
805 			      struct dmxdev_filter *filter, u16 pid)
806 {
807 	struct dmxdev_feed *feed;
808 
809 	if ((filter->type != DMXDEV_TYPE_PES) ||
810 	    (filter->state < DMXDEV_STATE_SET))
811 		return -EINVAL;
812 
813 	/* only TS packet filters may have multiple PIDs */
814 	if ((filter->params.pes.output != DMX_OUT_TSDEMUX_TAP) &&
815 	    (!list_empty(&filter->feed.ts)))
816 		return -EINVAL;
817 
818 	feed = kzalloc(sizeof(struct dmxdev_feed), GFP_KERNEL);
819 	if (feed == NULL)
820 		return -ENOMEM;
821 
822 	feed->pid = pid;
823 	list_add(&feed->next, &filter->feed.ts);
824 
825 	if (filter->state >= DMXDEV_STATE_GO)
826 		return dvb_dmxdev_start_feed(dmxdev, filter, feed);
827 
828 	return 0;
829 }
830 
dvb_dmxdev_remove_pid(struct dmxdev * dmxdev,struct dmxdev_filter * filter,u16 pid)831 static int dvb_dmxdev_remove_pid(struct dmxdev *dmxdev,
832 				  struct dmxdev_filter *filter, u16 pid)
833 {
834 	struct dmxdev_feed *feed, *tmp;
835 
836 	if ((filter->type != DMXDEV_TYPE_PES) ||
837 	    (filter->state < DMXDEV_STATE_SET))
838 		return -EINVAL;
839 
840 	list_for_each_entry_safe(feed, tmp, &filter->feed.ts, next) {
841 		if ((feed->pid == pid) && (feed->ts != NULL)) {
842 			feed->ts->stop_filtering(feed->ts);
843 			filter->dev->demux->release_ts_feed(filter->dev->demux,
844 							    feed->ts);
845 			list_del(&feed->next);
846 			kfree(feed);
847 		}
848 	}
849 
850 	return 0;
851 }
852 
dvb_dmxdev_filter_set(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter,struct dmx_sct_filter_params * params)853 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
854 				 struct dmxdev_filter *dmxdevfilter,
855 				 struct dmx_sct_filter_params *params)
856 {
857 	dprintk("function : %s\n", __func__);
858 
859 	dvb_dmxdev_filter_stop(dmxdevfilter);
860 
861 	dmxdevfilter->type = DMXDEV_TYPE_SEC;
862 	memcpy(&dmxdevfilter->params.sec,
863 	       params, sizeof(struct dmx_sct_filter_params));
864 	invert_mode(&dmxdevfilter->params.sec.filter);
865 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
866 
867 	if (params->flags & DMX_IMMEDIATE_START)
868 		return dvb_dmxdev_filter_start(dmxdevfilter);
869 
870 	return 0;
871 }
872 
dvb_dmxdev_pes_filter_set(struct dmxdev * dmxdev,struct dmxdev_filter * dmxdevfilter,struct dmx_pes_filter_params * params)873 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
874 				     struct dmxdev_filter *dmxdevfilter,
875 				     struct dmx_pes_filter_params *params)
876 {
877 	int ret;
878 
879 	dvb_dmxdev_filter_stop(dmxdevfilter);
880 	dvb_dmxdev_filter_reset(dmxdevfilter);
881 
882 	if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
883 		return -EINVAL;
884 
885 	dmxdevfilter->type = DMXDEV_TYPE_PES;
886 	memcpy(&dmxdevfilter->params, params,
887 	       sizeof(struct dmx_pes_filter_params));
888 	INIT_LIST_HEAD(&dmxdevfilter->feed.ts);
889 
890 	dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
891 
892 	ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter,
893 				 dmxdevfilter->params.pes.pid);
894 	if (ret < 0)
895 		return ret;
896 
897 	if (params->flags & DMX_IMMEDIATE_START)
898 		return dvb_dmxdev_filter_start(dmxdevfilter);
899 
900 	return 0;
901 }
902 
dvb_dmxdev_read_sec(struct dmxdev_filter * dfil,struct file * file,char __user * buf,size_t count,loff_t * ppos)903 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
904 				   struct file *file, char __user *buf,
905 				   size_t count, loff_t *ppos)
906 {
907 	int result, hcount;
908 	int done = 0;
909 
910 	if (dfil->todo <= 0) {
911 		hcount = 3 + dfil->todo;
912 		if (hcount > count)
913 			hcount = count;
914 		result = dvb_dmxdev_buffer_read(&dfil->buffer,
915 						file->f_flags & O_NONBLOCK,
916 						buf, hcount, ppos);
917 		if (result < 0) {
918 			dfil->todo = 0;
919 			return result;
920 		}
921 		if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
922 			return -EFAULT;
923 		buf += result;
924 		done = result;
925 		count -= result;
926 		dfil->todo -= result;
927 		if (dfil->todo > -3)
928 			return done;
929 		dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
930 		if (!count)
931 			return done;
932 	}
933 	if (count > dfil->todo)
934 		count = dfil->todo;
935 	result = dvb_dmxdev_buffer_read(&dfil->buffer,
936 					file->f_flags & O_NONBLOCK,
937 					buf, count, ppos);
938 	if (result < 0)
939 		return result;
940 	dfil->todo -= result;
941 	return (result + done);
942 }
943 
944 static ssize_t
dvb_demux_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)945 dvb_demux_read(struct file *file, char __user *buf, size_t count,
946 	       loff_t *ppos)
947 {
948 	struct dmxdev_filter *dmxdevfilter = file->private_data;
949 	int ret;
950 
951 	if (mutex_lock_interruptible(&dmxdevfilter->mutex))
952 		return -ERESTARTSYS;
953 
954 	if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
955 		ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
956 	else
957 		ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
958 					     file->f_flags & O_NONBLOCK,
959 					     buf, count, ppos);
960 
961 	mutex_unlock(&dmxdevfilter->mutex);
962 	return ret;
963 }
964 
dvb_demux_do_ioctl(struct file * file,unsigned int cmd,void * parg)965 static int dvb_demux_do_ioctl(struct file *file,
966 			      unsigned int cmd, void *parg)
967 {
968 	struct dmxdev_filter *dmxdevfilter = file->private_data;
969 	struct dmxdev *dmxdev = dmxdevfilter->dev;
970 	unsigned long arg = (unsigned long)parg;
971 	int ret = 0;
972 
973 	if (mutex_lock_interruptible(&dmxdev->mutex))
974 		return -ERESTARTSYS;
975 
976 	switch (cmd) {
977 	case DMX_START:
978 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
979 			mutex_unlock(&dmxdev->mutex);
980 			return -ERESTARTSYS;
981 		}
982 		if (dmxdevfilter->state < DMXDEV_STATE_SET)
983 			ret = -EINVAL;
984 		else
985 			ret = dvb_dmxdev_filter_start(dmxdevfilter);
986 		mutex_unlock(&dmxdevfilter->mutex);
987 		break;
988 
989 	case DMX_STOP:
990 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
991 			mutex_unlock(&dmxdev->mutex);
992 			return -ERESTARTSYS;
993 		}
994 		ret = dvb_dmxdev_filter_stop(dmxdevfilter);
995 		mutex_unlock(&dmxdevfilter->mutex);
996 		break;
997 
998 	case DMX_SET_FILTER:
999 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1000 			mutex_unlock(&dmxdev->mutex);
1001 			return -ERESTARTSYS;
1002 		}
1003 		ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
1004 		mutex_unlock(&dmxdevfilter->mutex);
1005 		break;
1006 
1007 	case DMX_SET_PES_FILTER:
1008 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1009 			mutex_unlock(&dmxdev->mutex);
1010 			return -ERESTARTSYS;
1011 		}
1012 		ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
1013 		mutex_unlock(&dmxdevfilter->mutex);
1014 		break;
1015 
1016 	case DMX_SET_BUFFER_SIZE:
1017 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1018 			mutex_unlock(&dmxdev->mutex);
1019 			return -ERESTARTSYS;
1020 		}
1021 		ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
1022 		mutex_unlock(&dmxdevfilter->mutex);
1023 		break;
1024 
1025 	case DMX_GET_PES_PIDS:
1026 		if (!dmxdev->demux->get_pes_pids) {
1027 			ret = -EINVAL;
1028 			break;
1029 		}
1030 		dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
1031 		break;
1032 
1033 	case DMX_GET_CAPS:
1034 		if (!dmxdev->demux->get_caps) {
1035 			ret = -EINVAL;
1036 			break;
1037 		}
1038 		ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
1039 		break;
1040 
1041 	case DMX_SET_SOURCE:
1042 		if (!dmxdev->demux->set_source) {
1043 			ret = -EINVAL;
1044 			break;
1045 		}
1046 		ret = dmxdev->demux->set_source(dmxdev->demux, parg);
1047 		break;
1048 
1049 	case DMX_GET_STC:
1050 		if (!dmxdev->demux->get_stc) {
1051 			ret = -EINVAL;
1052 			break;
1053 		}
1054 		ret = dmxdev->demux->get_stc(dmxdev->demux,
1055 					     ((struct dmx_stc *)parg)->num,
1056 					     &((struct dmx_stc *)parg)->stc,
1057 					     &((struct dmx_stc *)parg)->base);
1058 		break;
1059 
1060 	case DMX_ADD_PID:
1061 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1062 			ret = -ERESTARTSYS;
1063 			break;
1064 		}
1065 		ret = dvb_dmxdev_add_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1066 		mutex_unlock(&dmxdevfilter->mutex);
1067 		break;
1068 
1069 	case DMX_REMOVE_PID:
1070 		if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
1071 			ret = -ERESTARTSYS;
1072 			break;
1073 		}
1074 		ret = dvb_dmxdev_remove_pid(dmxdev, dmxdevfilter, *(u16 *)parg);
1075 		mutex_unlock(&dmxdevfilter->mutex);
1076 		break;
1077 
1078 	default:
1079 		ret = -EINVAL;
1080 		break;
1081 	}
1082 	mutex_unlock(&dmxdev->mutex);
1083 	return ret;
1084 }
1085 
dvb_demux_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1086 static long dvb_demux_ioctl(struct file *file, unsigned int cmd,
1087 			    unsigned long arg)
1088 {
1089 	return dvb_usercopy(file, cmd, arg, dvb_demux_do_ioctl);
1090 }
1091 
dvb_demux_poll(struct file * file,poll_table * wait)1092 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
1093 {
1094 	struct dmxdev_filter *dmxdevfilter = file->private_data;
1095 	unsigned int mask = 0;
1096 
1097 	if (!dmxdevfilter)
1098 		return -EINVAL;
1099 
1100 	poll_wait(file, &dmxdevfilter->buffer.queue, wait);
1101 
1102 	if (dmxdevfilter->state != DMXDEV_STATE_GO &&
1103 	    dmxdevfilter->state != DMXDEV_STATE_DONE &&
1104 	    dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
1105 		return 0;
1106 
1107 	if (dmxdevfilter->buffer.error)
1108 		mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1109 
1110 	if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1111 		mask |= (POLLIN | POLLRDNORM | POLLPRI);
1112 
1113 	return mask;
1114 }
1115 
dvb_demux_release(struct inode * inode,struct file * file)1116 static int dvb_demux_release(struct inode *inode, struct file *file)
1117 {
1118 	struct dmxdev_filter *dmxdevfilter = file->private_data;
1119 	struct dmxdev *dmxdev = dmxdevfilter->dev;
1120 
1121 	int ret;
1122 
1123 	ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1124 
1125 	mutex_lock(&dmxdev->mutex);
1126 	dmxdev->dvbdev->users--;
1127 	if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1128 		fops_put(file->f_op);
1129 		file->f_op = NULL;
1130 		mutex_unlock(&dmxdev->mutex);
1131 		wake_up(&dmxdev->dvbdev->wait_queue);
1132 	} else
1133 		mutex_unlock(&dmxdev->mutex);
1134 
1135 	return ret;
1136 }
1137 
1138 static const struct file_operations dvb_demux_fops = {
1139 	.owner = THIS_MODULE,
1140 	.read = dvb_demux_read,
1141 	.unlocked_ioctl = dvb_demux_ioctl,
1142 	.open = dvb_demux_open,
1143 	.release = dvb_demux_release,
1144 	.poll = dvb_demux_poll,
1145 	.llseek = default_llseek,
1146 };
1147 
1148 static struct dvb_device dvbdev_demux = {
1149 	.priv = NULL,
1150 	.users = 1,
1151 	.writers = 1,
1152 	.fops = &dvb_demux_fops
1153 };
1154 
dvb_dvr_do_ioctl(struct file * file,unsigned int cmd,void * parg)1155 static int dvb_dvr_do_ioctl(struct file *file,
1156 			    unsigned int cmd, void *parg)
1157 {
1158 	struct dvb_device *dvbdev = file->private_data;
1159 	struct dmxdev *dmxdev = dvbdev->priv;
1160 	unsigned long arg = (unsigned long)parg;
1161 	int ret;
1162 
1163 	if (mutex_lock_interruptible(&dmxdev->mutex))
1164 		return -ERESTARTSYS;
1165 
1166 	switch (cmd) {
1167 	case DMX_SET_BUFFER_SIZE:
1168 		ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1169 		break;
1170 
1171 	default:
1172 		ret = -EINVAL;
1173 		break;
1174 	}
1175 	mutex_unlock(&dmxdev->mutex);
1176 	return ret;
1177 }
1178 
dvb_dvr_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1179 static long dvb_dvr_ioctl(struct file *file,
1180 			 unsigned int cmd, unsigned long arg)
1181 {
1182 	return dvb_usercopy(file, cmd, arg, dvb_dvr_do_ioctl);
1183 }
1184 
dvb_dvr_poll(struct file * file,poll_table * wait)1185 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1186 {
1187 	struct dvb_device *dvbdev = file->private_data;
1188 	struct dmxdev *dmxdev = dvbdev->priv;
1189 	unsigned int mask = 0;
1190 
1191 	dprintk("function : %s\n", __func__);
1192 
1193 	poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1194 
1195 	if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1196 		if (dmxdev->dvr_buffer.error)
1197 			mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1198 
1199 		if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1200 			mask |= (POLLIN | POLLRDNORM | POLLPRI);
1201 	} else
1202 		mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1203 
1204 	return mask;
1205 }
1206 
1207 static const struct file_operations dvb_dvr_fops = {
1208 	.owner = THIS_MODULE,
1209 	.read = dvb_dvr_read,
1210 	.write = dvb_dvr_write,
1211 	.unlocked_ioctl = dvb_dvr_ioctl,
1212 	.open = dvb_dvr_open,
1213 	.release = dvb_dvr_release,
1214 	.poll = dvb_dvr_poll,
1215 	.llseek = default_llseek,
1216 };
1217 
1218 static struct dvb_device dvbdev_dvr = {
1219 	.priv = NULL,
1220 	.readers = 1,
1221 	.users = 1,
1222 	.fops = &dvb_dvr_fops
1223 };
1224 
dvb_dmxdev_init(struct dmxdev * dmxdev,struct dvb_adapter * dvb_adapter)1225 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1226 {
1227 	int i;
1228 
1229 	if (dmxdev->demux->open(dmxdev->demux) < 0)
1230 		return -EUSERS;
1231 
1232 	dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1233 	if (!dmxdev->filter)
1234 		return -ENOMEM;
1235 
1236 	mutex_init(&dmxdev->mutex);
1237 	spin_lock_init(&dmxdev->lock);
1238 	for (i = 0; i < dmxdev->filternum; i++) {
1239 		dmxdev->filter[i].dev = dmxdev;
1240 		dmxdev->filter[i].buffer.data = NULL;
1241 		dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1242 					    DMXDEV_STATE_FREE);
1243 	}
1244 
1245 	dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1246 			    DVB_DEVICE_DEMUX);
1247 	dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1248 			    dmxdev, DVB_DEVICE_DVR);
1249 
1250 	dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1251 
1252 	return 0;
1253 }
1254 
1255 EXPORT_SYMBOL(dvb_dmxdev_init);
1256 
dvb_dmxdev_release(struct dmxdev * dmxdev)1257 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1258 {
1259 	dmxdev->exit=1;
1260 	if (dmxdev->dvbdev->users > 1) {
1261 		wait_event(dmxdev->dvbdev->wait_queue,
1262 				dmxdev->dvbdev->users==1);
1263 	}
1264 	if (dmxdev->dvr_dvbdev->users > 1) {
1265 		wait_event(dmxdev->dvr_dvbdev->wait_queue,
1266 				dmxdev->dvr_dvbdev->users==1);
1267 	}
1268 
1269 	dvb_unregister_device(dmxdev->dvbdev);
1270 	dvb_unregister_device(dmxdev->dvr_dvbdev);
1271 
1272 	vfree(dmxdev->filter);
1273 	dmxdev->filter = NULL;
1274 	dmxdev->demux->close(dmxdev->demux);
1275 }
1276 
1277 EXPORT_SYMBOL(dvb_dmxdev_release);
1278