Merge branch 'viper-for-rmk' of git://www.misterjones.org/linux-2.6-arm
[linux-2.6] / 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         int ret;
248
249         if (dmxdev->exit) {
250                 mutex_unlock(&dmxdev->mutex);
251                 return -ENODEV;
252         }
253
254         //mutex_lock(&dmxdev->mutex);
255         ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
256                                      file->f_flags & O_NONBLOCK,
257                                      buf, count, ppos);
258         //mutex_unlock(&dmxdev->mutex);
259         return ret;
260 }
261
262 static int dvb_dvr_set_buffer_size(struct dmxdev *dmxdev,
263                                       unsigned long size)
264 {
265         struct dvb_ringbuffer *buf = &dmxdev->dvr_buffer;
266         void *newmem;
267         void *oldmem;
268
269         dprintk("function : %s\n", __func__);
270
271         if (buf->size == size)
272                 return 0;
273         if (!size)
274                 return -EINVAL;
275
276         newmem = vmalloc(size);
277         if (!newmem)
278                 return -ENOMEM;
279
280         oldmem = buf->data;
281
282         spin_lock_irq(&dmxdev->lock);
283         buf->data = newmem;
284         buf->size = size;
285
286         /* reset and not flush in case the buffer shrinks */
287         dvb_ringbuffer_reset(buf);
288         spin_unlock_irq(&dmxdev->lock);
289
290         vfree(oldmem);
291
292         return 0;
293 }
294
295 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
296                                                *dmxdevfilter, int state)
297 {
298         spin_lock_irq(&dmxdevfilter->dev->lock);
299         dmxdevfilter->state = state;
300         spin_unlock_irq(&dmxdevfilter->dev->lock);
301 }
302
303 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
304                                       unsigned long size)
305 {
306         struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
307         void *newmem;
308         void *oldmem;
309
310         if (buf->size == size)
311                 return 0;
312         if (!size)
313                 return -EINVAL;
314         if (dmxdevfilter->state >= DMXDEV_STATE_GO)
315                 return -EBUSY;
316
317         newmem = vmalloc(size);
318         if (!newmem)
319                 return -ENOMEM;
320
321         oldmem = buf->data;
322
323         spin_lock_irq(&dmxdevfilter->dev->lock);
324         buf->data = newmem;
325         buf->size = size;
326
327         /* reset and not flush in case the buffer shrinks */
328         dvb_ringbuffer_reset(buf);
329         spin_unlock_irq(&dmxdevfilter->dev->lock);
330
331         vfree(oldmem);
332
333         return 0;
334 }
335
336 static void dvb_dmxdev_filter_timeout(unsigned long data)
337 {
338         struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
339
340         dmxdevfilter->buffer.error = -ETIMEDOUT;
341         spin_lock_irq(&dmxdevfilter->dev->lock);
342         dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
343         spin_unlock_irq(&dmxdevfilter->dev->lock);
344         wake_up(&dmxdevfilter->buffer.queue);
345 }
346
347 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
348 {
349         struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
350
351         del_timer(&dmxdevfilter->timer);
352         if (para->timeout) {
353                 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
354                 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
355                 dmxdevfilter->timer.expires =
356                     jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
357                 add_timer(&dmxdevfilter->timer);
358         }
359 }
360
361 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
362                                        const u8 *buffer2, size_t buffer2_len,
363                                        struct dmx_section_filter *filter,
364                                        enum dmx_success success)
365 {
366         struct dmxdev_filter *dmxdevfilter = filter->priv;
367         int ret;
368
369         if (dmxdevfilter->buffer.error) {
370                 wake_up(&dmxdevfilter->buffer.queue);
371                 return 0;
372         }
373         spin_lock(&dmxdevfilter->dev->lock);
374         if (dmxdevfilter->state != DMXDEV_STATE_GO) {
375                 spin_unlock(&dmxdevfilter->dev->lock);
376                 return 0;
377         }
378         del_timer(&dmxdevfilter->timer);
379         dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
380                 buffer1[0], buffer1[1],
381                 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
382         ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
383                                       buffer1_len);
384         if (ret == buffer1_len) {
385                 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
386                                               buffer2_len);
387         }
388         if (ret < 0) {
389                 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
390                 dmxdevfilter->buffer.error = ret;
391         }
392         if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
393                 dmxdevfilter->state = DMXDEV_STATE_DONE;
394         spin_unlock(&dmxdevfilter->dev->lock);
395         wake_up(&dmxdevfilter->buffer.queue);
396         return 0;
397 }
398
399 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
400                                   const u8 *buffer2, size_t buffer2_len,
401                                   struct dmx_ts_feed *feed,
402                                   enum dmx_success success)
403 {
404         struct dmxdev_filter *dmxdevfilter = feed->priv;
405         struct dvb_ringbuffer *buffer;
406         int ret;
407
408         spin_lock(&dmxdevfilter->dev->lock);
409         if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
410                 spin_unlock(&dmxdevfilter->dev->lock);
411                 return 0;
412         }
413
414         if (dmxdevfilter->params.pes.output == DMX_OUT_TAP
415             || dmxdevfilter->params.pes.output == DMX_OUT_TSDEMUX_TAP)
416                 buffer = &dmxdevfilter->buffer;
417         else
418                 buffer = &dmxdevfilter->dev->dvr_buffer;
419         if (buffer->error) {
420                 spin_unlock(&dmxdevfilter->dev->lock);
421                 wake_up(&buffer->queue);
422                 return 0;
423         }
424         ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
425         if (ret == buffer1_len)
426                 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
427         if (ret < 0) {
428                 dvb_ringbuffer_flush(buffer);
429                 buffer->error = ret;
430         }
431         spin_unlock(&dmxdevfilter->dev->lock);
432         wake_up(&buffer->queue);
433         return 0;
434 }
435
436 /* stop feed but only mark the specified filter as stopped (state set) */
437 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
438 {
439         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
440
441         switch (dmxdevfilter->type) {
442         case DMXDEV_TYPE_SEC:
443                 del_timer(&dmxdevfilter->timer);
444                 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
445                 break;
446         case DMXDEV_TYPE_PES:
447                 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
448                 break;
449         default:
450                 return -EINVAL;
451         }
452         return 0;
453 }
454
455 /* start feed associated with the specified filter */
456 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
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                 return filter->feed.ts->start_filtering(filter->feed.ts);
465         default:
466                 return -EINVAL;
467         }
468
469         return 0;
470 }
471
472 /* restart section feed if it has filters left associated with it,
473    otherwise release the feed */
474 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
475 {
476         int i;
477         struct dmxdev *dmxdev = filter->dev;
478         u16 pid = filter->params.sec.pid;
479
480         for (i = 0; i < dmxdev->filternum; i++)
481                 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
482                     dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
483                     dmxdev->filter[i].params.sec.pid == pid) {
484                         dvb_dmxdev_feed_start(&dmxdev->filter[i]);
485                         return 0;
486                 }
487
488         filter->dev->demux->release_section_feed(dmxdev->demux,
489                                                  filter->feed.sec);
490
491         return 0;
492 }
493
494 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
495 {
496         if (dmxdevfilter->state < DMXDEV_STATE_GO)
497                 return 0;
498
499         switch (dmxdevfilter->type) {
500         case DMXDEV_TYPE_SEC:
501                 if (!dmxdevfilter->feed.sec)
502                         break;
503                 dvb_dmxdev_feed_stop(dmxdevfilter);
504                 if (dmxdevfilter->filter.sec)
505                         dmxdevfilter->feed.sec->
506                             release_filter(dmxdevfilter->feed.sec,
507                                            dmxdevfilter->filter.sec);
508                 dvb_dmxdev_feed_restart(dmxdevfilter);
509                 dmxdevfilter->feed.sec = NULL;
510                 break;
511         case DMXDEV_TYPE_PES:
512                 if (!dmxdevfilter->feed.ts)
513                         break;
514                 dvb_dmxdev_feed_stop(dmxdevfilter);
515                 dmxdevfilter->dev->demux->
516                     release_ts_feed(dmxdevfilter->dev->demux,
517                                     dmxdevfilter->feed.ts);
518                 dmxdevfilter->feed.ts = NULL;
519                 break;
520         default:
521                 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
522                         return 0;
523                 return -EINVAL;
524         }
525
526         dvb_ringbuffer_flush(&dmxdevfilter->buffer);
527         return 0;
528 }
529
530 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
531 {
532         if (dmxdevfilter->state < DMXDEV_STATE_SET)
533                 return 0;
534
535         dmxdevfilter->type = DMXDEV_TYPE_NONE;
536         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
537         return 0;
538 }
539
540 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
541 {
542         struct dmxdev *dmxdev = filter->dev;
543         void *mem;
544         int ret, i;
545
546         if (filter->state < DMXDEV_STATE_SET)
547                 return -EINVAL;
548
549         if (filter->state >= DMXDEV_STATE_GO)
550                 dvb_dmxdev_filter_stop(filter);
551
552         if (!filter->buffer.data) {
553                 mem = vmalloc(filter->buffer.size);
554                 if (!mem)
555                         return -ENOMEM;
556                 spin_lock_irq(&filter->dev->lock);
557                 filter->buffer.data = mem;
558                 spin_unlock_irq(&filter->dev->lock);
559         }
560
561         dvb_ringbuffer_flush(&filter->buffer);
562
563         switch (filter->type) {
564         case DMXDEV_TYPE_SEC:
565         {
566                 struct dmx_sct_filter_params *para = &filter->params.sec;
567                 struct dmx_section_filter **secfilter = &filter->filter.sec;
568                 struct dmx_section_feed **secfeed = &filter->feed.sec;
569
570                 *secfilter = NULL;
571                 *secfeed = NULL;
572
573
574                 /* find active filter/feed with same PID */
575                 for (i = 0; i < dmxdev->filternum; i++) {
576                         if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
577                             dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
578                             dmxdev->filter[i].params.sec.pid == para->pid) {
579                                 *secfeed = dmxdev->filter[i].feed.sec;
580                                 break;
581                         }
582                 }
583
584                 /* if no feed found, try to allocate new one */
585                 if (!*secfeed) {
586                         ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
587                                                                    secfeed,
588                                                                    dvb_dmxdev_section_callback);
589                         if (ret < 0) {
590                                 printk("DVB (%s): could not alloc feed\n",
591                                        __func__);
592                                 return ret;
593                         }
594
595                         ret = (*secfeed)->set(*secfeed, para->pid, 32768,
596                                               (para->flags & DMX_CHECK_CRC) ? 1 : 0);
597                         if (ret < 0) {
598                                 printk("DVB (%s): could not set feed\n",
599                                        __func__);
600                                 dvb_dmxdev_feed_restart(filter);
601                                 return ret;
602                         }
603                 } else {
604                         dvb_dmxdev_feed_stop(filter);
605                 }
606
607                 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
608                 if (ret < 0) {
609                         dvb_dmxdev_feed_restart(filter);
610                         filter->feed.sec->start_filtering(*secfeed);
611                         dprintk("could not get filter\n");
612                         return ret;
613                 }
614
615                 (*secfilter)->priv = filter;
616
617                 memcpy(&((*secfilter)->filter_value[3]),
618                        &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
619                 memcpy(&(*secfilter)->filter_mask[3],
620                        &para->filter.mask[1], DMX_FILTER_SIZE - 1);
621                 memcpy(&(*secfilter)->filter_mode[3],
622                        &para->filter.mode[1], DMX_FILTER_SIZE - 1);
623
624                 (*secfilter)->filter_value[0] = para->filter.filter[0];
625                 (*secfilter)->filter_mask[0] = para->filter.mask[0];
626                 (*secfilter)->filter_mode[0] = para->filter.mode[0];
627                 (*secfilter)->filter_mask[1] = 0;
628                 (*secfilter)->filter_mask[2] = 0;
629
630                 filter->todo = 0;
631
632                 ret = filter->feed.sec->start_filtering(filter->feed.sec);
633                 if (ret < 0)
634                         return ret;
635
636                 dvb_dmxdev_filter_timer(filter);
637                 break;
638         }
639         case DMXDEV_TYPE_PES:
640         {
641                 struct timespec timeout = { 0 };
642                 struct dmx_pes_filter_params *para = &filter->params.pes;
643                 dmx_output_t otype;
644                 int ts_type;
645                 enum dmx_ts_pes ts_pes;
646                 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
647
648                 filter->feed.ts = NULL;
649                 otype = para->output;
650
651                 ts_pes = (enum dmx_ts_pes)para->pes_type;
652
653                 if (ts_pes < DMX_PES_OTHER)
654                         ts_type = TS_DECODER;
655                 else
656                         ts_type = 0;
657
658                 if (otype == DMX_OUT_TS_TAP)
659                         ts_type |= TS_PACKET;
660                 else if (otype == DMX_OUT_TSDEMUX_TAP)
661                         ts_type |= TS_PACKET | TS_DEMUX;
662                 else if (otype == DMX_OUT_TAP)
663                         ts_type |= TS_PACKET | TS_DEMUX | TS_PAYLOAD_ONLY;
664
665                 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
666                                                       tsfeed,
667                                                       dvb_dmxdev_ts_callback);
668                 if (ret < 0)
669                         return ret;
670
671                 (*tsfeed)->priv = filter;
672
673                 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
674                                      32768, timeout);
675                 if (ret < 0) {
676                         dmxdev->demux->release_ts_feed(dmxdev->demux,
677                                                        *tsfeed);
678                         return ret;
679                 }
680
681                 ret = filter->feed.ts->start_filtering(filter->feed.ts);
682                 if (ret < 0) {
683                         dmxdev->demux->release_ts_feed(dmxdev->demux,
684                                                        *tsfeed);
685                         return ret;
686                 }
687
688                 break;
689         }
690         default:
691                 return -EINVAL;
692         }
693
694         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
695         return 0;
696 }
697
698 static int dvb_demux_open(struct inode *inode, struct file *file)
699 {
700         struct dvb_device *dvbdev = file->private_data;
701         struct dmxdev *dmxdev = dvbdev->priv;
702         int i;
703         struct dmxdev_filter *dmxdevfilter;
704
705         if (!dmxdev->filter)
706                 return -EINVAL;
707
708         if (mutex_lock_interruptible(&dmxdev->mutex))
709                 return -ERESTARTSYS;
710
711         for (i = 0; i < dmxdev->filternum; i++)
712                 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
713                         break;
714
715         if (i == dmxdev->filternum) {
716                 mutex_unlock(&dmxdev->mutex);
717                 return -EMFILE;
718         }
719
720         dmxdevfilter = &dmxdev->filter[i];
721         mutex_init(&dmxdevfilter->mutex);
722         file->private_data = dmxdevfilter;
723
724         dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
725         dmxdevfilter->type = DMXDEV_TYPE_NONE;
726         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
727         dmxdevfilter->feed.ts = NULL;
728         init_timer(&dmxdevfilter->timer);
729
730         dvbdev->users++;
731
732         mutex_unlock(&dmxdev->mutex);
733         return 0;
734 }
735
736 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
737                                   struct dmxdev_filter *dmxdevfilter)
738 {
739         mutex_lock(&dmxdev->mutex);
740         mutex_lock(&dmxdevfilter->mutex);
741
742         dvb_dmxdev_filter_stop(dmxdevfilter);
743         dvb_dmxdev_filter_reset(dmxdevfilter);
744
745         if (dmxdevfilter->buffer.data) {
746                 void *mem = dmxdevfilter->buffer.data;
747
748                 spin_lock_irq(&dmxdev->lock);
749                 dmxdevfilter->buffer.data = NULL;
750                 spin_unlock_irq(&dmxdev->lock);
751                 vfree(mem);
752         }
753
754         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
755         wake_up(&dmxdevfilter->buffer.queue);
756         mutex_unlock(&dmxdevfilter->mutex);
757         mutex_unlock(&dmxdev->mutex);
758         return 0;
759 }
760
761 static inline void invert_mode(dmx_filter_t *filter)
762 {
763         int i;
764
765         for (i = 0; i < DMX_FILTER_SIZE; i++)
766                 filter->mode[i] ^= 0xff;
767 }
768
769 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
770                                  struct dmxdev_filter *dmxdevfilter,
771                                  struct dmx_sct_filter_params *params)
772 {
773         dprintk("function : %s\n", __func__);
774
775         dvb_dmxdev_filter_stop(dmxdevfilter);
776
777         dmxdevfilter->type = DMXDEV_TYPE_SEC;
778         memcpy(&dmxdevfilter->params.sec,
779                params, sizeof(struct dmx_sct_filter_params));
780         invert_mode(&dmxdevfilter->params.sec.filter);
781         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
782
783         if (params->flags & DMX_IMMEDIATE_START)
784                 return dvb_dmxdev_filter_start(dmxdevfilter);
785
786         return 0;
787 }
788
789 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
790                                      struct dmxdev_filter *dmxdevfilter,
791                                      struct dmx_pes_filter_params *params)
792 {
793         dvb_dmxdev_filter_stop(dmxdevfilter);
794
795         if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
796                 return -EINVAL;
797
798         dmxdevfilter->type = DMXDEV_TYPE_PES;
799         memcpy(&dmxdevfilter->params, params,
800                sizeof(struct dmx_pes_filter_params));
801
802         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
803
804         if (params->flags & DMX_IMMEDIATE_START)
805                 return dvb_dmxdev_filter_start(dmxdevfilter);
806
807         return 0;
808 }
809
810 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
811                                    struct file *file, char __user *buf,
812                                    size_t count, loff_t *ppos)
813 {
814         int result, hcount;
815         int done = 0;
816
817         if (dfil->todo <= 0) {
818                 hcount = 3 + dfil->todo;
819                 if (hcount > count)
820                         hcount = count;
821                 result = dvb_dmxdev_buffer_read(&dfil->buffer,
822                                                 file->f_flags & O_NONBLOCK,
823                                                 buf, hcount, ppos);
824                 if (result < 0) {
825                         dfil->todo = 0;
826                         return result;
827                 }
828                 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
829                         return -EFAULT;
830                 buf += result;
831                 done = result;
832                 count -= result;
833                 dfil->todo -= result;
834                 if (dfil->todo > -3)
835                         return done;
836                 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
837                 if (!count)
838                         return done;
839         }
840         if (count > dfil->todo)
841                 count = dfil->todo;
842         result = dvb_dmxdev_buffer_read(&dfil->buffer,
843                                         file->f_flags & O_NONBLOCK,
844                                         buf, count, ppos);
845         if (result < 0)
846                 return result;
847         dfil->todo -= result;
848         return (result + done);
849 }
850
851 static ssize_t
852 dvb_demux_read(struct file *file, char __user *buf, size_t count,
853                loff_t *ppos)
854 {
855         struct dmxdev_filter *dmxdevfilter = file->private_data;
856         int ret;
857
858         if (mutex_lock_interruptible(&dmxdevfilter->mutex))
859                 return -ERESTARTSYS;
860
861         if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
862                 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
863         else
864                 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
865                                              file->f_flags & O_NONBLOCK,
866                                              buf, count, ppos);
867
868         mutex_unlock(&dmxdevfilter->mutex);
869         return ret;
870 }
871
872 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
873                               unsigned int cmd, void *parg)
874 {
875         struct dmxdev_filter *dmxdevfilter = file->private_data;
876         struct dmxdev *dmxdev = dmxdevfilter->dev;
877         unsigned long arg = (unsigned long)parg;
878         int ret = 0;
879
880         if (mutex_lock_interruptible(&dmxdev->mutex))
881                 return -ERESTARTSYS;
882
883         switch (cmd) {
884         case DMX_START:
885                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
886                         mutex_unlock(&dmxdev->mutex);
887                         return -ERESTARTSYS;
888                 }
889                 if (dmxdevfilter->state < DMXDEV_STATE_SET)
890                         ret = -EINVAL;
891                 else
892                         ret = dvb_dmxdev_filter_start(dmxdevfilter);
893                 mutex_unlock(&dmxdevfilter->mutex);
894                 break;
895
896         case DMX_STOP:
897                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
898                         mutex_unlock(&dmxdev->mutex);
899                         return -ERESTARTSYS;
900                 }
901                 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
902                 mutex_unlock(&dmxdevfilter->mutex);
903                 break;
904
905         case DMX_SET_FILTER:
906                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
907                         mutex_unlock(&dmxdev->mutex);
908                         return -ERESTARTSYS;
909                 }
910                 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
911                 mutex_unlock(&dmxdevfilter->mutex);
912                 break;
913
914         case DMX_SET_PES_FILTER:
915                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
916                         mutex_unlock(&dmxdev->mutex);
917                         return -ERESTARTSYS;
918                 }
919                 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
920                 mutex_unlock(&dmxdevfilter->mutex);
921                 break;
922
923         case DMX_SET_BUFFER_SIZE:
924                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
925                         mutex_unlock(&dmxdev->mutex);
926                         return -ERESTARTSYS;
927                 }
928                 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
929                 mutex_unlock(&dmxdevfilter->mutex);
930                 break;
931
932         case DMX_GET_PES_PIDS:
933                 if (!dmxdev->demux->get_pes_pids) {
934                         ret = -EINVAL;
935                         break;
936                 }
937                 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
938                 break;
939
940         case DMX_GET_CAPS:
941                 if (!dmxdev->demux->get_caps) {
942                         ret = -EINVAL;
943                         break;
944                 }
945                 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
946                 break;
947
948         case DMX_SET_SOURCE:
949                 if (!dmxdev->demux->set_source) {
950                         ret = -EINVAL;
951                         break;
952                 }
953                 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
954                 break;
955
956         case DMX_GET_STC:
957                 if (!dmxdev->demux->get_stc) {
958                         ret = -EINVAL;
959                         break;
960                 }
961                 ret = dmxdev->demux->get_stc(dmxdev->demux,
962                                              ((struct dmx_stc *)parg)->num,
963                                              &((struct dmx_stc *)parg)->stc,
964                                              &((struct dmx_stc *)parg)->base);
965                 break;
966
967         default:
968                 ret = -EINVAL;
969                 break;
970         }
971         mutex_unlock(&dmxdev->mutex);
972         return ret;
973 }
974
975 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
976                            unsigned int cmd, unsigned long arg)
977 {
978         return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
979 }
980
981 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
982 {
983         struct dmxdev_filter *dmxdevfilter = file->private_data;
984         unsigned int mask = 0;
985
986         if (!dmxdevfilter)
987                 return -EINVAL;
988
989         poll_wait(file, &dmxdevfilter->buffer.queue, wait);
990
991         if (dmxdevfilter->state != DMXDEV_STATE_GO &&
992             dmxdevfilter->state != DMXDEV_STATE_DONE &&
993             dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
994                 return 0;
995
996         if (dmxdevfilter->buffer.error)
997                 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
998
999         if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
1000                 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1001
1002         return mask;
1003 }
1004
1005 static int dvb_demux_release(struct inode *inode, struct file *file)
1006 {
1007         struct dmxdev_filter *dmxdevfilter = file->private_data;
1008         struct dmxdev *dmxdev = dmxdevfilter->dev;
1009
1010         int ret;
1011
1012         ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1013
1014         mutex_lock(&dmxdev->mutex);
1015         dmxdev->dvbdev->users--;
1016         if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
1017                 fops_put(file->f_op);
1018                 file->f_op = NULL;
1019                 mutex_unlock(&dmxdev->mutex);
1020                 wake_up(&dmxdev->dvbdev->wait_queue);
1021         } else
1022                 mutex_unlock(&dmxdev->mutex);
1023
1024         return ret;
1025 }
1026
1027 static struct file_operations dvb_demux_fops = {
1028         .owner = THIS_MODULE,
1029         .read = dvb_demux_read,
1030         .ioctl = dvb_demux_ioctl,
1031         .open = dvb_demux_open,
1032         .release = dvb_demux_release,
1033         .poll = dvb_demux_poll,
1034 };
1035
1036 static struct dvb_device dvbdev_demux = {
1037         .priv = NULL,
1038         .users = 1,
1039         .writers = 1,
1040         .fops = &dvb_demux_fops
1041 };
1042
1043 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1044                             unsigned int cmd, void *parg)
1045 {
1046         struct dvb_device *dvbdev = file->private_data;
1047         struct dmxdev *dmxdev = dvbdev->priv;
1048         unsigned long arg = (unsigned long)parg;
1049         int ret;
1050
1051         if (mutex_lock_interruptible(&dmxdev->mutex))
1052                 return -ERESTARTSYS;
1053
1054         switch (cmd) {
1055         case DMX_SET_BUFFER_SIZE:
1056                 ret = dvb_dvr_set_buffer_size(dmxdev, arg);
1057                 break;
1058
1059         default:
1060                 ret = -EINVAL;
1061                 break;
1062         }
1063         mutex_unlock(&dmxdev->mutex);
1064         return ret;
1065 }
1066
1067 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1068                          unsigned int cmd, unsigned long arg)
1069 {
1070         return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1071 }
1072
1073 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1074 {
1075         struct dvb_device *dvbdev = file->private_data;
1076         struct dmxdev *dmxdev = dvbdev->priv;
1077         unsigned int mask = 0;
1078
1079         dprintk("function : %s\n", __func__);
1080
1081         poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1082
1083         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1084                 if (dmxdev->dvr_buffer.error)
1085                         mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1086
1087                 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1088                         mask |= (POLLIN | POLLRDNORM | POLLPRI);
1089         } else
1090                 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1091
1092         return mask;
1093 }
1094
1095 static struct file_operations dvb_dvr_fops = {
1096         .owner = THIS_MODULE,
1097         .read = dvb_dvr_read,
1098         .write = dvb_dvr_write,
1099         .ioctl = dvb_dvr_ioctl,
1100         .open = dvb_dvr_open,
1101         .release = dvb_dvr_release,
1102         .poll = dvb_dvr_poll,
1103 };
1104
1105 static struct dvb_device dvbdev_dvr = {
1106         .priv = NULL,
1107         .readers = 1,
1108         .users = 1,
1109         .fops = &dvb_dvr_fops
1110 };
1111
1112 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1113 {
1114         int i;
1115
1116         if (dmxdev->demux->open(dmxdev->demux) < 0)
1117                 return -EUSERS;
1118
1119         dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1120         if (!dmxdev->filter)
1121                 return -ENOMEM;
1122
1123         mutex_init(&dmxdev->mutex);
1124         spin_lock_init(&dmxdev->lock);
1125         for (i = 0; i < dmxdev->filternum; i++) {
1126                 dmxdev->filter[i].dev = dmxdev;
1127                 dmxdev->filter[i].buffer.data = NULL;
1128                 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1129                                             DMXDEV_STATE_FREE);
1130         }
1131
1132         dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1133                             DVB_DEVICE_DEMUX);
1134         dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1135                             dmxdev, DVB_DEVICE_DVR);
1136
1137         dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1138
1139         return 0;
1140 }
1141
1142 EXPORT_SYMBOL(dvb_dmxdev_init);
1143
1144 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1145 {
1146         dmxdev->exit=1;
1147         if (dmxdev->dvbdev->users > 1) {
1148                 wait_event(dmxdev->dvbdev->wait_queue,
1149                                 dmxdev->dvbdev->users==1);
1150         }
1151         if (dmxdev->dvr_dvbdev->users > 1) {
1152                 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1153                                 dmxdev->dvr_dvbdev->users==1);
1154         }
1155
1156         dvb_unregister_device(dmxdev->dvbdev);
1157         dvb_unregister_device(dmxdev->dvr_dvbdev);
1158
1159         vfree(dmxdev->filter);
1160         dmxdev->filter = NULL;
1161         dmxdev->demux->close(dmxdev->demux);
1162 }
1163
1164 EXPORT_SYMBOL(dvb_dmxdev_release);