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