Pull osi into release branch
[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/moduleparam.h>
28 #include <linux/poll.h>
29 #include <linux/ioctl.h>
30 #include <linux/wait.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include "dmxdev.h"
34
35 static int debug;
36
37 module_param(debug, int, 0644);
38 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
39
40 #define dprintk if (debug) printk
41
42 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
43                                    const u8 *src, size_t len)
44 {
45         ssize_t free;
46
47         if (!len)
48                 return 0;
49         if (!buf->data)
50                 return 0;
51
52         free = dvb_ringbuffer_free(buf);
53         if (len > free) {
54                 dprintk("dmxdev: buffer overflow\n");
55                 return -EOVERFLOW;
56         }
57
58         return dvb_ringbuffer_write(buf, src, len);
59 }
60
61 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
62                                       int non_blocking, char __user *buf,
63                                       size_t count, loff_t *ppos)
64 {
65         size_t todo;
66         ssize_t avail;
67         ssize_t ret = 0;
68
69         if (!src->data)
70                 return 0;
71
72         if (src->error) {
73                 ret = src->error;
74                 dvb_ringbuffer_flush(src);
75                 return ret;
76         }
77
78         for (todo = count; todo > 0; todo -= ret) {
79                 if (non_blocking && dvb_ringbuffer_empty(src)) {
80                         ret = -EWOULDBLOCK;
81                         break;
82                 }
83
84                 ret = wait_event_interruptible(src->queue,
85                                                !dvb_ringbuffer_empty(src) ||
86                                                (src->error != 0));
87                 if (ret < 0)
88                         break;
89
90                 if (src->error) {
91                         ret = src->error;
92                         dvb_ringbuffer_flush(src);
93                         break;
94                 }
95
96                 avail = dvb_ringbuffer_avail(src);
97                 if (avail > todo)
98                         avail = todo;
99
100                 ret = dvb_ringbuffer_read(src, (u8 *)buf, avail, 1);
101                 if (ret < 0)
102                         break;
103
104                 buf += ret;
105         }
106
107         return (count - todo) ? (count - todo) : ret;
108 }
109
110 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
111 {
112         struct list_head *head, *pos;
113
114         head = demux->get_frontends(demux);
115         if (!head)
116                 return NULL;
117         list_for_each(pos, head)
118                 if (DMX_FE_ENTRY(pos)->source == type)
119                         return DMX_FE_ENTRY(pos);
120
121         return NULL;
122 }
123
124 static int dvb_dvr_open(struct inode *inode, struct file *file)
125 {
126         struct dvb_device *dvbdev = file->private_data;
127         struct dmxdev *dmxdev = dvbdev->priv;
128         struct dmx_frontend *front;
129
130         dprintk("function : %s\n", __FUNCTION__);
131
132         if (mutex_lock_interruptible(&dmxdev->mutex))
133                 return -ERESTARTSYS;
134
135         if (dmxdev->exit) {
136                 mutex_unlock(&dmxdev->mutex);
137                 return -ENODEV;
138         }
139
140         if ((file->f_flags & O_ACCMODE) == O_RDWR) {
141                 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
142                         mutex_unlock(&dmxdev->mutex);
143                         return -EOPNOTSUPP;
144                 }
145         }
146
147         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
148                 void *mem;
149                 if (!dvbdev->readers) {
150                         mutex_unlock(&dmxdev->mutex);
151                         return -EBUSY;
152                 }
153                 mem = vmalloc(DVR_BUFFER_SIZE);
154                 if (!mem) {
155                         mutex_unlock(&dmxdev->mutex);
156                         return -ENOMEM;
157                 }
158                 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
159                 dvbdev->readers--;
160         }
161
162         if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
163                 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
164
165                 if (!dmxdev->demux->write) {
166                         mutex_unlock(&dmxdev->mutex);
167                         return -EOPNOTSUPP;
168                 }
169
170                 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
171
172                 if (!front) {
173                         mutex_unlock(&dmxdev->mutex);
174                         return -EINVAL;
175                 }
176                 dmxdev->demux->disconnect_frontend(dmxdev->demux);
177                 dmxdev->demux->connect_frontend(dmxdev->demux, front);
178         }
179         dvbdev->users++;
180         mutex_unlock(&dmxdev->mutex);
181         return 0;
182 }
183
184 static int dvb_dvr_release(struct inode *inode, struct file *file)
185 {
186         struct dvb_device *dvbdev = file->private_data;
187         struct dmxdev *dmxdev = dvbdev->priv;
188
189         mutex_lock(&dmxdev->mutex);
190
191         if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
192                 dmxdev->demux->disconnect_frontend(dmxdev->demux);
193                 dmxdev->demux->connect_frontend(dmxdev->demux,
194                                                 dmxdev->dvr_orig_fe);
195         }
196         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
197                 dvbdev->readers++;
198                 if (dmxdev->dvr_buffer.data) {
199                         void *mem = dmxdev->dvr_buffer.data;
200                         mb();
201                         spin_lock_irq(&dmxdev->lock);
202                         dmxdev->dvr_buffer.data = NULL;
203                         spin_unlock_irq(&dmxdev->lock);
204                         vfree(mem);
205                 }
206         }
207         /* TODO */
208         dvbdev->users--;
209         if(dvbdev->users==-1 && dmxdev->exit==1) {
210                 fops_put(file->f_op);
211                 file->f_op = NULL;
212                 mutex_unlock(&dmxdev->mutex);
213                 wake_up(&dvbdev->wait_queue);
214         } else
215                 mutex_unlock(&dmxdev->mutex);
216
217         return 0;
218 }
219
220 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
221                              size_t count, loff_t *ppos)
222 {
223         struct dvb_device *dvbdev = file->private_data;
224         struct dmxdev *dmxdev = dvbdev->priv;
225         int ret;
226
227         if (!dmxdev->demux->write)
228                 return -EOPNOTSUPP;
229         if ((file->f_flags & O_ACCMODE) != O_WRONLY)
230                 return -EINVAL;
231         if (mutex_lock_interruptible(&dmxdev->mutex))
232                 return -ERESTARTSYS;
233
234         if (dmxdev->exit) {
235                 mutex_unlock(&dmxdev->mutex);
236                 return -ENODEV;
237         }
238         ret = dmxdev->demux->write(dmxdev->demux, buf, count);
239         mutex_unlock(&dmxdev->mutex);
240         return ret;
241 }
242
243 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
244                             loff_t *ppos)
245 {
246         struct dvb_device *dvbdev = file->private_data;
247         struct dmxdev *dmxdev = dvbdev->priv;
248         int ret;
249
250         if (dmxdev->exit) {
251                 mutex_unlock(&dmxdev->mutex);
252                 return -ENODEV;
253         }
254
255         //mutex_lock(&dmxdev->mutex);
256         ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
257                                      file->f_flags & O_NONBLOCK,
258                                      buf, count, ppos);
259         //mutex_unlock(&dmxdev->mutex);
260         return ret;
261 }
262
263 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
264                                                *dmxdevfilter, int state)
265 {
266         spin_lock_irq(&dmxdevfilter->dev->lock);
267         dmxdevfilter->state = state;
268         spin_unlock_irq(&dmxdevfilter->dev->lock);
269 }
270
271 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
272                                       unsigned long size)
273 {
274         struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
275         void *mem;
276
277         if (buf->size == size)
278                 return 0;
279         if (dmxdevfilter->state >= DMXDEV_STATE_GO)
280                 return -EBUSY;
281         spin_lock_irq(&dmxdevfilter->dev->lock);
282         mem = buf->data;
283         buf->data = NULL;
284         buf->size = size;
285         dvb_ringbuffer_flush(buf);
286         spin_unlock_irq(&dmxdevfilter->dev->lock);
287         vfree(mem);
288
289         if (buf->size) {
290                 mem = vmalloc(dmxdevfilter->buffer.size);
291                 if (!mem)
292                         return -ENOMEM;
293                 spin_lock_irq(&dmxdevfilter->dev->lock);
294                 buf->data = mem;
295                 spin_unlock_irq(&dmxdevfilter->dev->lock);
296         }
297         return 0;
298 }
299
300 static void dvb_dmxdev_filter_timeout(unsigned long data)
301 {
302         struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
303
304         dmxdevfilter->buffer.error = -ETIMEDOUT;
305         spin_lock_irq(&dmxdevfilter->dev->lock);
306         dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
307         spin_unlock_irq(&dmxdevfilter->dev->lock);
308         wake_up(&dmxdevfilter->buffer.queue);
309 }
310
311 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
312 {
313         struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
314
315         del_timer(&dmxdevfilter->timer);
316         if (para->timeout) {
317                 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
318                 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
319                 dmxdevfilter->timer.expires =
320                     jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
321                 add_timer(&dmxdevfilter->timer);
322         }
323 }
324
325 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
326                                        const u8 *buffer2, size_t buffer2_len,
327                                        struct dmx_section_filter *filter,
328                                        enum dmx_success success)
329 {
330         struct dmxdev_filter *dmxdevfilter = filter->priv;
331         int ret;
332
333         if (dmxdevfilter->buffer.error) {
334                 wake_up(&dmxdevfilter->buffer.queue);
335                 return 0;
336         }
337         spin_lock(&dmxdevfilter->dev->lock);
338         if (dmxdevfilter->state != DMXDEV_STATE_GO) {
339                 spin_unlock(&dmxdevfilter->dev->lock);
340                 return 0;
341         }
342         del_timer(&dmxdevfilter->timer);
343         dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
344                 buffer1[0], buffer1[1],
345                 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
346         ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
347                                       buffer1_len);
348         if (ret == buffer1_len) {
349                 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
350                                               buffer2_len);
351         }
352         if (ret < 0) {
353                 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
354                 dmxdevfilter->buffer.error = ret;
355         }
356         if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
357                 dmxdevfilter->state = DMXDEV_STATE_DONE;
358         spin_unlock(&dmxdevfilter->dev->lock);
359         wake_up(&dmxdevfilter->buffer.queue);
360         return 0;
361 }
362
363 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
364                                   const u8 *buffer2, size_t buffer2_len,
365                                   struct dmx_ts_feed *feed,
366                                   enum dmx_success success)
367 {
368         struct dmxdev_filter *dmxdevfilter = feed->priv;
369         struct dvb_ringbuffer *buffer;
370         int ret;
371
372         spin_lock(&dmxdevfilter->dev->lock);
373         if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
374                 spin_unlock(&dmxdevfilter->dev->lock);
375                 return 0;
376         }
377
378         if (dmxdevfilter->params.pes.output == DMX_OUT_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
625                 if (otype == DMX_OUT_TAP)
626                         ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
627
628                 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
629                                                       tsfeed,
630                                                       dvb_dmxdev_ts_callback);
631                 if (ret < 0)
632                         return ret;
633
634                 (*tsfeed)->priv = filter;
635
636                 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
637                                      32768, timeout);
638                 if (ret < 0) {
639                         dmxdev->demux->release_ts_feed(dmxdev->demux,
640                                                        *tsfeed);
641                         return ret;
642                 }
643
644                 ret = filter->feed.ts->start_filtering(filter->feed.ts);
645                 if (ret < 0) {
646                         dmxdev->demux->release_ts_feed(dmxdev->demux,
647                                                        *tsfeed);
648                         return ret;
649                 }
650
651                 break;
652         }
653         default:
654                 return -EINVAL;
655         }
656
657         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
658         return 0;
659 }
660
661 static int dvb_demux_open(struct inode *inode, struct file *file)
662 {
663         struct dvb_device *dvbdev = file->private_data;
664         struct dmxdev *dmxdev = dvbdev->priv;
665         int i;
666         struct dmxdev_filter *dmxdevfilter;
667
668         if (!dmxdev->filter)
669                 return -EINVAL;
670
671         if (mutex_lock_interruptible(&dmxdev->mutex))
672                 return -ERESTARTSYS;
673
674         for (i = 0; i < dmxdev->filternum; i++)
675                 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
676                         break;
677
678         if (i == dmxdev->filternum) {
679                 mutex_unlock(&dmxdev->mutex);
680                 return -EMFILE;
681         }
682
683         dmxdevfilter = &dmxdev->filter[i];
684         mutex_init(&dmxdevfilter->mutex);
685         file->private_data = dmxdevfilter;
686
687         dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
688         dmxdevfilter->type = DMXDEV_TYPE_NONE;
689         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
690         dmxdevfilter->feed.ts = NULL;
691         init_timer(&dmxdevfilter->timer);
692
693         dvbdev->users++;
694
695         mutex_unlock(&dmxdev->mutex);
696         return 0;
697 }
698
699 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
700                                   struct dmxdev_filter *dmxdevfilter)
701 {
702         mutex_lock(&dmxdev->mutex);
703         mutex_lock(&dmxdevfilter->mutex);
704
705         dvb_dmxdev_filter_stop(dmxdevfilter);
706         dvb_dmxdev_filter_reset(dmxdevfilter);
707
708         if (dmxdevfilter->buffer.data) {
709                 void *mem = dmxdevfilter->buffer.data;
710
711                 spin_lock_irq(&dmxdev->lock);
712                 dmxdevfilter->buffer.data = NULL;
713                 spin_unlock_irq(&dmxdev->lock);
714                 vfree(mem);
715         }
716
717         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
718         wake_up(&dmxdevfilter->buffer.queue);
719         mutex_unlock(&dmxdevfilter->mutex);
720         mutex_unlock(&dmxdev->mutex);
721         return 0;
722 }
723
724 static inline void invert_mode(dmx_filter_t *filter)
725 {
726         int i;
727
728         for (i = 0; i < DMX_FILTER_SIZE; i++)
729                 filter->mode[i] ^= 0xff;
730 }
731
732 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
733                                  struct dmxdev_filter *dmxdevfilter,
734                                  struct dmx_sct_filter_params *params)
735 {
736         dprintk("function : %s\n", __FUNCTION__);
737
738         dvb_dmxdev_filter_stop(dmxdevfilter);
739
740         dmxdevfilter->type = DMXDEV_TYPE_SEC;
741         memcpy(&dmxdevfilter->params.sec,
742                params, sizeof(struct dmx_sct_filter_params));
743         invert_mode(&dmxdevfilter->params.sec.filter);
744         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
745
746         if (params->flags & DMX_IMMEDIATE_START)
747                 return dvb_dmxdev_filter_start(dmxdevfilter);
748
749         return 0;
750 }
751
752 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
753                                      struct dmxdev_filter *dmxdevfilter,
754                                      struct dmx_pes_filter_params *params)
755 {
756         dvb_dmxdev_filter_stop(dmxdevfilter);
757
758         if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
759                 return -EINVAL;
760
761         dmxdevfilter->type = DMXDEV_TYPE_PES;
762         memcpy(&dmxdevfilter->params, params,
763                sizeof(struct dmx_pes_filter_params));
764
765         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
766
767         if (params->flags & DMX_IMMEDIATE_START)
768                 return dvb_dmxdev_filter_start(dmxdevfilter);
769
770         return 0;
771 }
772
773 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
774                                    struct file *file, char __user *buf,
775                                    size_t count, loff_t *ppos)
776 {
777         int result, hcount;
778         int done = 0;
779
780         if (dfil->todo <= 0) {
781                 hcount = 3 + dfil->todo;
782                 if (hcount > count)
783                         hcount = count;
784                 result = dvb_dmxdev_buffer_read(&dfil->buffer,
785                                                 file->f_flags & O_NONBLOCK,
786                                                 buf, hcount, ppos);
787                 if (result < 0) {
788                         dfil->todo = 0;
789                         return result;
790                 }
791                 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
792                         return -EFAULT;
793                 buf += result;
794                 done = result;
795                 count -= result;
796                 dfil->todo -= result;
797                 if (dfil->todo > -3)
798                         return done;
799                 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
800                 if (!count)
801                         return done;
802         }
803         if (count > dfil->todo)
804                 count = dfil->todo;
805         result = dvb_dmxdev_buffer_read(&dfil->buffer,
806                                         file->f_flags & O_NONBLOCK,
807                                         buf, count, ppos);
808         if (result < 0)
809                 return result;
810         dfil->todo -= result;
811         return (result + done);
812 }
813
814 static ssize_t
815 dvb_demux_read(struct file *file, char __user *buf, size_t count,
816                loff_t *ppos)
817 {
818         struct dmxdev_filter *dmxdevfilter = file->private_data;
819         int ret;
820
821         if (mutex_lock_interruptible(&dmxdevfilter->mutex))
822                 return -ERESTARTSYS;
823
824         if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
825                 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
826         else
827                 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
828                                              file->f_flags & O_NONBLOCK,
829                                              buf, count, ppos);
830
831         mutex_unlock(&dmxdevfilter->mutex);
832         return ret;
833 }
834
835 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
836                               unsigned int cmd, void *parg)
837 {
838         struct dmxdev_filter *dmxdevfilter = file->private_data;
839         struct dmxdev *dmxdev = dmxdevfilter->dev;
840         unsigned long arg = (unsigned long)parg;
841         int ret = 0;
842
843         if (mutex_lock_interruptible(&dmxdev->mutex))
844                 return -ERESTARTSYS;
845
846         switch (cmd) {
847         case DMX_START:
848                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
849                         mutex_unlock(&dmxdev->mutex);
850                         return -ERESTARTSYS;
851                 }
852                 if (dmxdevfilter->state < DMXDEV_STATE_SET)
853                         ret = -EINVAL;
854                 else
855                         ret = dvb_dmxdev_filter_start(dmxdevfilter);
856                 mutex_unlock(&dmxdevfilter->mutex);
857                 break;
858
859         case DMX_STOP:
860                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861                         mutex_unlock(&dmxdev->mutex);
862                         return -ERESTARTSYS;
863                 }
864                 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
865                 mutex_unlock(&dmxdevfilter->mutex);
866                 break;
867
868         case DMX_SET_FILTER:
869                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
870                         mutex_unlock(&dmxdev->mutex);
871                         return -ERESTARTSYS;
872                 }
873                 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
874                 mutex_unlock(&dmxdevfilter->mutex);
875                 break;
876
877         case DMX_SET_PES_FILTER:
878                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
879                         mutex_unlock(&dmxdev->mutex);
880                         return -ERESTARTSYS;
881                 }
882                 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
883                 mutex_unlock(&dmxdevfilter->mutex);
884                 break;
885
886         case DMX_SET_BUFFER_SIZE:
887                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
888                         mutex_unlock(&dmxdev->mutex);
889                         return -ERESTARTSYS;
890                 }
891                 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
892                 mutex_unlock(&dmxdevfilter->mutex);
893                 break;
894
895         case DMX_GET_PES_PIDS:
896                 if (!dmxdev->demux->get_pes_pids) {
897                         ret = -EINVAL;
898                         break;
899                 }
900                 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
901                 break;
902
903         case DMX_GET_CAPS:
904                 if (!dmxdev->demux->get_caps) {
905                         ret = -EINVAL;
906                         break;
907                 }
908                 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
909                 break;
910
911         case DMX_SET_SOURCE:
912                 if (!dmxdev->demux->set_source) {
913                         ret = -EINVAL;
914                         break;
915                 }
916                 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
917                 break;
918
919         case DMX_GET_STC:
920                 if (!dmxdev->demux->get_stc) {
921                         ret = -EINVAL;
922                         break;
923                 }
924                 ret = dmxdev->demux->get_stc(dmxdev->demux,
925                                              ((struct dmx_stc *)parg)->num,
926                                              &((struct dmx_stc *)parg)->stc,
927                                              &((struct dmx_stc *)parg)->base);
928                 break;
929
930         default:
931                 ret = -EINVAL;
932                 break;
933         }
934         mutex_unlock(&dmxdev->mutex);
935         return ret;
936 }
937
938 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
939                            unsigned int cmd, unsigned long arg)
940 {
941         return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
942 }
943
944 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
945 {
946         struct dmxdev_filter *dmxdevfilter = file->private_data;
947         unsigned int mask = 0;
948
949         if (!dmxdevfilter)
950                 return -EINVAL;
951
952         poll_wait(file, &dmxdevfilter->buffer.queue, wait);
953
954         if (dmxdevfilter->state != DMXDEV_STATE_GO &&
955             dmxdevfilter->state != DMXDEV_STATE_DONE &&
956             dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
957                 return 0;
958
959         if (dmxdevfilter->buffer.error)
960                 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
961
962         if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
963                 mask |= (POLLIN | POLLRDNORM | POLLPRI);
964
965         return mask;
966 }
967
968 static int dvb_demux_release(struct inode *inode, struct file *file)
969 {
970         struct dmxdev_filter *dmxdevfilter = file->private_data;
971         struct dmxdev *dmxdev = dmxdevfilter->dev;
972
973         int ret;
974
975         ret = dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
976
977         mutex_lock(&dmxdev->mutex);
978         dmxdev->dvbdev->users--;
979         if(dmxdev->dvbdev->users==1 && dmxdev->exit==1) {
980                 fops_put(file->f_op);
981                 file->f_op = NULL;
982                 mutex_unlock(&dmxdev->mutex);
983                 wake_up(&dmxdev->dvbdev->wait_queue);
984         } else
985                 mutex_unlock(&dmxdev->mutex);
986
987         return ret;
988 }
989
990 static struct file_operations dvb_demux_fops = {
991         .owner = THIS_MODULE,
992         .read = dvb_demux_read,
993         .ioctl = dvb_demux_ioctl,
994         .open = dvb_demux_open,
995         .release = dvb_demux_release,
996         .poll = dvb_demux_poll,
997 };
998
999 static struct dvb_device dvbdev_demux = {
1000         .priv = NULL,
1001         .users = 1,
1002         .writers = 1,
1003         .fops = &dvb_demux_fops
1004 };
1005
1006 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1007                             unsigned int cmd, void *parg)
1008 {
1009         struct dvb_device *dvbdev = file->private_data;
1010         struct dmxdev *dmxdev = dvbdev->priv;
1011         int ret;
1012
1013         if (mutex_lock_interruptible(&dmxdev->mutex))
1014                 return -ERESTARTSYS;
1015
1016         switch (cmd) {
1017         case DMX_SET_BUFFER_SIZE:
1018                 // FIXME: implement
1019                 ret = 0;
1020                 break;
1021
1022         default:
1023                 ret = -EINVAL;
1024                 break;
1025         }
1026         mutex_unlock(&dmxdev->mutex);
1027         return ret;
1028 }
1029
1030 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1031                          unsigned int cmd, unsigned long arg)
1032 {
1033         return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1034 }
1035
1036 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
1037 {
1038         struct dvb_device *dvbdev = file->private_data;
1039         struct dmxdev *dmxdev = dvbdev->priv;
1040         unsigned int mask = 0;
1041
1042         dprintk("function : %s\n", __FUNCTION__);
1043
1044         poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1045
1046         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1047                 if (dmxdev->dvr_buffer.error)
1048                         mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1049
1050                 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1051                         mask |= (POLLIN | POLLRDNORM | POLLPRI);
1052         } else
1053                 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1054
1055         return mask;
1056 }
1057
1058 static struct file_operations dvb_dvr_fops = {
1059         .owner = THIS_MODULE,
1060         .read = dvb_dvr_read,
1061         .write = dvb_dvr_write,
1062         .ioctl = dvb_dvr_ioctl,
1063         .open = dvb_dvr_open,
1064         .release = dvb_dvr_release,
1065         .poll = dvb_dvr_poll,
1066 };
1067
1068 static struct dvb_device dvbdev_dvr = {
1069         .priv = NULL,
1070         .readers = 1,
1071         .users = 1,
1072         .fops = &dvb_dvr_fops
1073 };
1074
1075 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1076 {
1077         int i;
1078
1079         if (dmxdev->demux->open(dmxdev->demux) < 0)
1080                 return -EUSERS;
1081
1082         dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1083         if (!dmxdev->filter)
1084                 return -ENOMEM;
1085
1086         mutex_init(&dmxdev->mutex);
1087         spin_lock_init(&dmxdev->lock);
1088         for (i = 0; i < dmxdev->filternum; i++) {
1089                 dmxdev->filter[i].dev = dmxdev;
1090                 dmxdev->filter[i].buffer.data = NULL;
1091                 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1092                                             DMXDEV_STATE_FREE);
1093         }
1094
1095         dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1096                             DVB_DEVICE_DEMUX);
1097         dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1098                             dmxdev, DVB_DEVICE_DVR);
1099
1100         dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1101
1102         return 0;
1103 }
1104
1105 EXPORT_SYMBOL(dvb_dmxdev_init);
1106
1107 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1108 {
1109         dmxdev->exit=1;
1110         if (dmxdev->dvbdev->users > 1) {
1111                 wait_event(dmxdev->dvbdev->wait_queue,
1112                                 dmxdev->dvbdev->users==1);
1113         }
1114         if (dmxdev->dvr_dvbdev->users > 1) {
1115                 wait_event(dmxdev->dvr_dvbdev->wait_queue,
1116                                 dmxdev->dvr_dvbdev->users==1);
1117         }
1118
1119         dvb_unregister_device(dmxdev->dvbdev);
1120         dvb_unregister_device(dmxdev->dvr_dvbdev);
1121
1122         vfree(dmxdev->filter);
1123         dmxdev->filter = NULL;
1124         dmxdev->demux->close(dmxdev->demux);
1125 }
1126
1127 EXPORT_SYMBOL(dvb_dmxdev_release);