V4L/DVB (11841): core: fix potential mutex_unlock without mutex_lock in dvb_dvr_read
[linux-2.6.git] / drivers / media / dvb / dvb-core / dmxdev.c
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/spinlock.h>
24 #include <linux/slab.h>
25 #include <linux/vmalloc.h>
26 #include <linux/module.h>
27 #include <linux/poll.h>
28 #include <linux/ioctl.h>
29 #include <linux/wait.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.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
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
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
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
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
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
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
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
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
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
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
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
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
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
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) */
431 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
432 {
433         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
434
435         switch (dmxdevfilter->type) {
436         case DMXDEV_TYPE_SEC:
437                 del_timer(&dmxdevfilter->timer);
438                 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
439                 break;
440         case DMXDEV_TYPE_PES:
441                 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
442                 break;
443         default:
444                 return -EINVAL;
445         }
446         return 0;
447 }
448
449 /* start feed associated with the specified filter */
450 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
451 {
452         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
453
454         switch (filter->type) {
455         case DMXDEV_TYPE_SEC:
456                 return filter->feed.sec->start_filtering(filter->feed.sec);
457         case DMXDEV_TYPE_PES:
458                 return filter->feed.ts->start_filtering(filter->feed.ts);
459         default:
460                 return -EINVAL;
461         }
462
463         return 0;
464 }
465
466 /* restart section feed if it has filters left associated with it,
467    otherwise release the feed */
468 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
469 {
470         int i;
471         struct dmxdev *dmxdev = filter->dev;
472         u16 pid = filter->params.sec.pid;
473
474         for (i = 0; i < dmxdev->filternum; i++)
475                 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
476                     dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
477                     dmxdev->filter[i].params.sec.pid == pid) {
478                         dvb_dmxdev_feed_start(&dmxdev->filter[i]);
479                         return 0;
480                 }
481
482         filter->dev->demux->release_section_feed(dmxdev->demux,
483                                                  filter->feed.sec);
484
485         return 0;
486 }
487
488 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
489 {
490         if (dmxdevfilter->state < DMXDEV_STATE_GO)
491                 return 0;
492
493         switch (dmxdevfilter->type) {
494         case DMXDEV_TYPE_SEC:
495                 if (!dmxdevfilter->feed.sec)
496                         break;
497                 dvb_dmxdev_feed_stop(dmxdevfilter);
498                 if (dmxdevfilter->filter.sec)
499                         dmxdevfilter->feed.sec->
500                             release_filter(dmxdevfilter->feed.sec,
501                                            dmxdevfilter->filter.sec);
502                 dvb_dmxdev_feed_restart(dmxdevfilter);
503                 dmxdevfilter->feed.sec = NULL;
504                 break;
505         case DMXDEV_TYPE_PES:
506                 if (!dmxdevfilter->feed.ts)
507                         break;
508                 dvb_dmxdev_feed_stop(dmxdevfilter);
509                 dmxdevfilter->dev->demux->
510                     release_ts_feed(dmxdevfilter->dev->demux,
511                                     dmxdevfilter->feed.ts);
512                 dmxdevfilter->feed.ts = NULL;
513                 break;
514         default:
515                 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
516                         return 0;
517                 return -EINVAL;
518         }
519
520         dvb_ringbuffer_flush(&dmxdevfilter->buffer);
521         return 0;
522 }
523
524 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
525 {
526         if (dmxdevfilter->state < DMXDEV_STATE_SET)
527                 return 0;
528
529         dmxdevfilter->type = DMXDEV_TYPE_NONE;
530         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
531         return 0;
532 }
533
534 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
535 {
536         struct dmxdev *dmxdev = filter->dev;
537         void *mem;
538         int ret, i;
539
540         if (filter->state < DMXDEV_STATE_SET)
541                 return -EINVAL;
542
543         if (filter->state >= DMXDEV_STATE_GO)
544                 dvb_dmxdev_filter_stop(filter);
545
546         if (!filter->buffer.data) {
547                 mem = vmalloc(filter->buffer.size);
548                 if (!mem)
549                         return -ENOMEM;
550                 spin_lock_irq(&filter->dev->lock);
551                 filter->buffer.data = mem;
552                 spin_unlock_irq(&filter->dev->lock);
553         }
554
555         dvb_ringbuffer_flush(&filter->buffer);
556
557         switch (filter->type) {
558         case DMXDEV_TYPE_SEC:
559         {
560                 struct dmx_sct_filter_params *para = &filter->params.sec;
561                 struct dmx_section_filter **secfilter = &filter->filter.sec;
562                 struct dmx_section_feed **secfeed = &filter->feed.sec;
563
564                 *secfilter = NULL;
565                 *secfeed = NULL;
566
567
568                 /* find active filter/feed with same PID */
569                 for (i = 0; i < dmxdev->filternum; i++) {
570                         if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
571                             dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
572                             dmxdev->filter[i].params.sec.pid == para->pid) {
573                                 *secfeed = dmxdev->filter[i].feed.sec;
574                                 break;
575                         }
576                 }
577
578                 /* if no feed found, try to allocate new one */
579                 if (!*secfeed) {
580                         ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
581                                                                    secfeed,
582                                                                    dvb_dmxdev_section_callback);
583                         if (ret < 0) {
584                                 printk("DVB (%s): could not alloc feed\n",
585                                        __func__);
586                                 return ret;
587                         }
588
589                         ret = (*secfeed)->set(*secfeed, para->pid, 32768,
590                                               (para->flags & DMX_CHECK_CRC) ? 1 : 0);
591                         if (ret < 0) {
592                                 printk("DVB (%s): could not set feed\n",
593                                        __func__);
594                                 dvb_dmxdev_feed_restart(filter);
595                                 return ret;
596                         }
597                 } else {
598                         dvb_dmxdev_feed_stop(filter);
599                 }
600
601                 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
602                 if (ret < 0) {
603                         dvb_dmxdev_feed_restart(filter);
604                         filter->feed.sec->start_filtering(*secfeed);
605                         dprintk("could not get filter\n");
606                         return ret;
607                 }
608
609                 (*secfilter)->priv = filter;
610
611                 memcpy(&((*secfilter)->filter_value[3]),
612                        &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
613                 memcpy(&(*secfilter)->filter_mask[3],
614                        &para->filter.mask[1], DMX_FILTER_SIZE - 1);
615                 memcpy(&(*secfilter)->filter_mode[3],
616                        &para->filter.mode[1], DMX_FILTER_SIZE - 1);
617
618                 (*secfilter)->filter_value[0] = para->filter.filter[0];
619                 (*secfilter)->filter_mask[0] = para->filter.mask[0];
620                 (*secfilter)->filter_mode[0] = para->filter.mode[0];
621                 (*secfilter)->filter_mask[1] = 0;
622                 (*secfilter)->filter_mask[2] = 0;
623
624                 filter->todo = 0;
625
626                 ret = filter->feed.sec->start_filtering(filter->feed.sec);
627                 if (ret < 0)
628                         return ret;
629
630                 dvb_dmxdev_filter_timer(filter);
631                 break;
632         }
633         case DMXDEV_TYPE_PES:
634         {
635                 struct timespec timeout = { 0 };
636                 struct dmx_pes_filter_params *para = &filter->params.pes;
637                 dmx_output_t otype;
638                 int ts_type;
639                 enum dmx_ts_pes ts_pes;
640                 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
641
642                 filter->feed.ts = NULL;
643                 otype = para->output;
644
645                 ts_pes = (enum dmx_ts_pes)para->pes_type;
646
647                 if (ts_pes < DMX_PES_OTHER)
648                         ts_type = TS_DECODER;
649                 else
650                         ts_type = 0;
651
652                 if (otype == DMX_OUT_TS_TAP)
653                         ts_type |= TS_PACKET;
654                 else if (otype == DMX_OUT_TSDEMUX_TAP)
655                         ts_type |= TS_PACKET | TS_DEMUX;
656                 else if (otype == DMX_OUT_TAP)
657                         ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
658
659                 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
660                                                       tsfeed,
661                                                       dvb_dmxdev_ts_callback);
662                 if (ret < 0)
663                         return ret;
664
665                 (*tsfeed)->priv = filter;
666
667                 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
668                                      32768, timeout);
669                 if (ret < 0) {
670                         dmxdev->demux->release_ts_feed(dmxdev->demux,
671                                                        *tsfeed);
672                         return ret;
673                 }
674
675                 ret = filter->feed.ts->start_filtering(filter->feed.ts);
676                 if (ret < 0) {
677                         dmxdev->demux->release_ts_feed(dmxdev->demux,
678                                                        *tsfeed);
679                         return ret;
680                 }
681
682                 break;
683         }
684         default:
685                 return -EINVAL;
686         }
687
688         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
689         return 0;
690 }
691
692 static int dvb_demux_open(struct inode *inode, struct file *file)
693 {
694         struct dvb_device *dvbdev = file->private_data;
695         struct dmxdev *dmxdev = dvbdev->priv;
696         int i;
697         struct dmxdev_filter *dmxdevfilter;
698
699         if (!dmxdev->filter)
700                 return -EINVAL;
701
702         if (mutex_lock_interruptible(&dmxdev->mutex))
703                 return -ERESTARTSYS;
704
705         for (i = 0; i < dmxdev->filternum; i++)
706                 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
707                         break;
708
709         if (i == dmxdev->filternum) {
710                 mutex_unlock(&dmxdev->mutex);
711                 return -EMFILE;
712         }
713
714         dmxdevfilter = &dmxdev->filter[i];
715         mutex_init(&dmxdevfilter->mutex);
716         file->private_data = dmxdevfilter;
717
718         dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
719         dmxdevfilter->type = DMXDEV_TYPE_NONE;
720         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
721         dmxdevfilter->feed.ts = NULL;
722         init_timer(&dmxdevfilter->timer);
723
724         dvbdev->users++;
725
726         mutex_unlock(&dmxdev->mutex);
727         return 0;
728 }
729
730 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
731                                   struct dmxdev_filter *dmxdevfilter)
732 {
733         mutex_lock(&dmxdev->mutex);
734         mutex_lock(&dmxdevfilter->mutex);
735
736         dvb_dmxdev_filter_stop(dmxdevfilter);
737         dvb_dmxdev_filter_reset(dmxdevfilter);
738
739         if (dmxdevfilter->buffer.data) {
740                 void *mem = dmxdevfilter->buffer.data;
741
742                 spin_lock_irq(&dmxdev->lock);
743                 dmxdevfilter->buffer.data = NULL;
744                 spin_unlock_irq(&dmxdev->lock);
745                 vfree(mem);
746         }
747
748         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
749         wake_up(&dmxdevfilter->buffer.queue);
750         mutex_unlock(&dmxdevfilter->mutex);
751         mutex_unlock(&dmxdev->mutex);
752         return 0;
753 }
754
755 static inline void invert_mode(dmx_filter_t *filter)
756 {
757         int i;
758
759         for (i = 0; i < DMX_FILTER_SIZE; i++)
760                 filter->mode[i] ^= 0xff;
761 }
762
763 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
764                                  struct dmxdev_filter *dmxdevfilter,
765                                  struct dmx_sct_filter_params *params)
766 {
767         dprintk("function : %s\n", __func__);
768
769         dvb_dmxdev_filter_stop(dmxdevfilter);
770
771         dmxdevfilter->type = DMXDEV_TYPE_SEC;
772         memcpy(&dmxdevfilter->params.sec,
773                params, sizeof(struct dmx_sct_filter_params));
774         invert_mode(&dmxdevfilter->params.sec.filter);
775         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
776
777         if (params->flags & DMX_IMMEDIATE_START)
778                 return dvb_dmxdev_filter_start(dmxdevfilter);
779
780         return 0;
781 }
782
783 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
784                                      struct dmxdev_filter *dmxdevfilter,
785                                      struct dmx_pes_filter_params *params)
786 {
787         dvb_dmxdev_filter_stop(dmxdevfilter);
788
789         if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
790                 return -EINVAL;
791
792         dmxdevfilter->type = DMXDEV_TYPE_PES;
793         memcpy(&dmxdevfilter->params, params,
794                sizeof(struct dmx_pes_filter_params));
795
796         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
797
798         if (params->flags & DMX_IMMEDIATE_START)
799                 return dvb_dmxdev_filter_start(dmxdevfilter);
800
801         return 0;
802 }
803
804 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
805                                    struct file *file, char __user *buf,
806                                    size_t count, loff_t *ppos)
807 {
808         int result, hcount;
809         int done = 0;
810
811         if (dfil->todo <= 0) {
812                 hcount = 3 + dfil->todo;
813                 if (hcount > count)
814                         hcount = count;
815                 result = dvb_dmxdev_buffer_read(&dfil->buffer,
816                                                 file->f_flags & O_NONBLOCK,
817                                                 buf, hcount, ppos);
818                 if (result < 0) {
819                         dfil->todo = 0;
820                         return result;
821                 }
822                 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
823                         return -EFAULT;
824                 buf += result;
825                 done = result;
826                 count -= result;
827                 dfil->todo -= result;
828                 if (dfil->todo > -3)
829                         return done;
830                 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
831                 if (!count)
832                         return done;
833         }
834         if (count > dfil->todo)
835                 count = dfil->todo;
836         result = dvb_dmxdev_buffer_read(&dfil->buffer,
837                                         file->f_flags & O_NONBLOCK,
838                                         buf, count, ppos);
839         if (result < 0)
840                 return result;
841         dfil->todo -= result;
842         return (result + done);
843 }
844
845 static ssize_t
846 dvb_demux_read(struct file *file, char __user *buf, size_t count,
847                loff_t *ppos)
848 {
849         struct dmxdev_filter *dmxdevfilter = file->private_data;
850         int ret;
851
852         if (mutex_lock_interruptible(&dmxdevfilter->mutex))
853                 return -ERESTARTSYS;
854
855         if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
856                 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
857         else
858                 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
859                                              file->f_flags & O_NONBLOCK,
860                                              buf, count, ppos);
861
862         mutex_unlock(&dmxdevfilter->mutex);
863         return ret;
864 }
865
866 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
867                               unsigned int cmd, void *parg)
868 {
869         struct dmxdev_filter *dmxdevfilter = file->private_data;
870         struct dmxdev *dmxdev = dmxdevfilter->dev;
871         unsigned long arg = (unsigned long)parg;
872         int ret = 0;
873
874         if (mutex_lock_interruptible(&dmxdev->mutex))
875                 return -ERESTARTSYS;
876
877         switch (cmd) {
878         case DMX_START:
879                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
880                         mutex_unlock(&dmxdev->mutex);
881                         return -ERESTARTSYS;
882                 }
883                 if (dmxdevfilter->state < DMXDEV_STATE_SET)
884                         ret = -EINVAL;
885                 else
886                         ret = dvb_dmxdev_filter_start(dmxdevfilter);
887                 mutex_unlock(&dmxdevfilter->mutex);
888                 break;
889
890         case DMX_STOP:
891                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
892                         mutex_unlock(&dmxdev->mutex);
893                         return -ERESTARTSYS;
894                 }
895                 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
896                 mutex_unlock(&dmxdevfilter->mutex);
897                 break;
898
899         case DMX_SET_FILTER:
900                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
901                         mutex_unlock(&dmxdev->mutex);
902                         return -ERESTARTSYS;
903                 }
904                 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
905                 mutex_unlock(&dmxdevfilter->mutex);
906                 break;
907
908         case DMX_SET_PES_FILTER:
909                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
910                         mutex_unlock(&dmxdev->mutex);
911                         return -ERESTARTSYS;
912                 }
913                 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
914                 mutex_unlock(&dmxdevfilter->mutex);
915                 break;
916
917         case DMX_SET_BUFFER_SIZE:
918                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
919                         mutex_unlock(&dmxdev->mutex);
920                         return -ERESTARTSYS;
921                 }
922                 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
923                 mutex_unlock(&dmxdevfilter->mutex);
924                 break;
925
926         case DMX_GET_PES_PIDS:
927                 if (!dmxdev->demux->get_pes_pids) {
928                         ret = -EINVAL;
929                         break;
930                 }
931                 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
932                 break;
933
934         case DMX_GET_CAPS:
935                 if (!dmxdev->demux->get_caps) {
936                         ret = -EINVAL;
937                         break;
938                 }
939                 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
940                 break;
941
942         case DMX_SET_SOURCE:
943                 if (!dmxdev->demux->set_source) {
944                         ret = -EINVAL;
945                         break;
946                 }
947                 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
948                 break;
949
950         case DMX_GET_STC:
951                 if (!dmxdev->demux->get_stc) {
952                         ret = -EINVAL;
953                         break;
954                 }
955                 ret = dmxdev->demux->get_stc(dmxdev->demux,
956                                              ((struct dmx_stc *)parg)->num,
957                                              &((struct dmx_stc *)parg)->stc,
958                                              &((struct dmx_stc *)parg)->base);
959                 break;
960
961         default:
962                 ret = -EINVAL;
963                 break;
964         }
965         mutex_unlock(&dmxdev->mutex);
966         return ret;
967 }
968
969 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
970                            unsigned int cmd, unsigned long arg)
971 {
972         return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
973 }
974
975 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
976 {
977         struct dmxdev_filter *dmxdevfilter = file->private_data;
978         unsigned int mask = 0;
979
980         if (!dmxdevfilter)
981                 return -EINVAL;
982
983         poll_wait(file, &dmxdevfilter->buffer.queue, wait);
984
985         if (dmxdevfilter->state != DMXDEV_STATE_GO &&
986             dmxdevfilter->state != DMXDEV_STATE_DONE &&
987             dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
988                 return 0;
989
990         if (dmxdevfilter->buffer.error)
991                 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
992
993         if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
994                 mask |= (POLLIN | POLLRDNORM | POLLPRI);
995
996         return mask;
997 }
998
999 static int dvb_demux_release(struct inode *inode, struct file *file)
1000 {
1001         struct dmxdev_filter *dmxdevfilter = file->private_data;
1002         struct dmxdev *dmxdev = dmxdevfilter->dev;
1003
1004         int ret;
1005
1006         ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1007
1008         mutex_lock(&dmxdev->mutex);
1009         dmxdev->dvbdev->users--;
1010         if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1011                 fops_put(file->f_op);
1012                 file->f_op = NULL;
1013                 mutex_unlock(&dmxdev->mutex);
1014                 wake_up(&dmxdev->dvbdev->wait_queue);
1015         } else
1016                 mutex_unlock(&dmxdev->mutex);
1017
1018         return ret;
1019 }
1020
1021 static const struct file_operations dvb_demux_fops = {
1022         .owner = THIS_MODULE,
1023         .read = dvb_demux_read,
1024         .ioctl = dvb_demux_ioctl,
1025         .open = dvb_demux_open,
1026         .release = dvb_demux_release,
1027         .poll = dvb_demux_poll,
1028 };
1029
1030 static struct dvb_device dvbdev_demux = {
1031         .priv = NULL,
1032         .users = 1,
1033         .writers = 1,
1034         .fops = &dvb_demux_fops
1035 };
1036
1037 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1038                             unsigned int cmd, void *parg)
1039 {
1040         struct dvb_device *dvbdev = file->private_data;
1041         struct dmxdev *dmxdev = dvbdev->priv;
1042         unsigned long arg = (unsigned long)parg;
1043         int ret;
1044
1045         if (mutex_lock_interruptible(&dmxdev->mutex))
1046                 return -ERESTARTSYS;
1047
1048         switch (cmd) {
1049         case DMX_SET_BUFFER_SIZE:
1050                 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1051                 break;
1052
1053         default:
1054                 ret = -EINVAL;
1055                 break;
1056         }
1057         mutex_unlock(&dmxdev->mutex);
1058         return ret;
1059 }
1060
1061 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1062                          unsigned int cmd, unsigned long arg)
1063 {
1064         return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1065 }
1066
1067 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1068 {
1069         struct dvb_device *dvbdev = file->private_data;
1070         struct dmxdev *dmxdev = dvbdev->priv;
1071         unsigned int mask = 0;
1072
1073         dprintk("function : %s\n", __func__);
1074
1075         poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1076
1077         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1078                 if (dmxdev->dvr_buffer.error)
1079                         mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1080
1081                 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1082                         mask |= (POLLIN | POLLRDNORM | POLLPRI);
1083         } else
1084                 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1085
1086         return mask;
1087 }
1088
1089 static struct file_operations dvb_dvr_fops = {
1090         .owner = THIS_MODULE,
1091         .read = dvb_dvr_read,
1092         .write = dvb_dvr_write,
1093         .ioctl = dvb_dvr_ioctl,
1094         .open = dvb_dvr_open,
1095         .release = dvb_dvr_release,
1096         .poll = dvb_dvr_poll,
1097 };
1098
1099 static struct dvb_device dvbdev_dvr = {
1100         .priv = NULL,
1101         .readers = 1,
1102         .users = 1,
1103         .fops = &dvb_dvr_fops
1104 };
1105
1106 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1107 {
1108         int i;
1109
1110         if (dmxdev->demux->open(dmxdev->demux) < 0)
1111                 return -EUSERS;
1112
1113         dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1114         if (!dmxdev->filter)
1115                 return -ENOMEM;
1116
1117         mutex_init(&dmxdev->mutex);
1118         spin_lock_init(&dmxdev->lock);
1119         for (i = 0; i < dmxdev->filternum; i++) {
1120                 dmxdev->filter[i].dev = dmxdev;
1121                 dmxdev->filter[i].buffer.data = NULL;
1122                 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1123                                             DMXDEV_STATE_FREE);
1124         }
1125
1126         dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1127                             DVB_DEVICE_DEMUX);
1128         dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1129                             dmxdev, DVB_DEVICE_DVR);
1130
1131         dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1132
1133         return 0;
1134 }
1135
1136 EXPORT_SYMBOL(dvb_dmxdev_init);
1137
1138 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1139 {
1140         dmxdev->exit=1;
1141         if (dmxdev->dvbdev->users > 1) {
1142                 wait_event(dmxdev->dvbdev->wait_queue,
1143                                 dmxdev->dvbdev->users==1);
1144         }
1145         if (dmxdev->dvr_dvbdev->users > 1) {
1146                 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1147                                 dmxdev->dvr_dvbdev->users==1);
1148         }
1149
1150         dvb_unregister_device(dmxdev->dvbdev);
1151         dvb_unregister_device(dmxdev->dvr_dvbdev);
1152
1153         vfree(dmxdev->filter);
1154         dmxdev->filter = NULL;
1155         dmxdev->demux->close(dmxdev->demux);
1156 }
1157
1158 EXPORT_SYMBOL(dvb_dmxdev_release);