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