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