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