Merge git://git.kernel.org/pub/scm/linux/kernel/git/bunk/trivial
[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/sched.h>
29 #include <linux/poll.h>
30 #include <linux/ioctl.h>
31 #include <linux/wait.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include "dmxdev.h"
35
36 static int debug;
37
38 module_param(debug, int, 0644);
39 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
40
41 #define dprintk if (debug) printk
42
43 static int dvb_dmxdev_buffer_write(struct dvb_ringbuffer *buf,
44                                    const u8 *src, size_t len)
45 {
46         ssize_t free;
47
48         if (!len)
49                 return 0;
50         if (!buf->data)
51                 return 0;
52
53         free = dvb_ringbuffer_free(buf);
54         if (len > free) {
55                 dprintk("dmxdev: buffer overflow\n");
56                 return -EOVERFLOW;
57         }
58
59         return dvb_ringbuffer_write(buf, src, len);
60 }
61
62 static ssize_t dvb_dmxdev_buffer_read(struct dvb_ringbuffer *src,
63                                       int non_blocking, char __user *buf,
64                                       size_t count, loff_t *ppos)
65 {
66         size_t todo;
67         ssize_t avail;
68         ssize_t ret = 0;
69
70         if (!src->data)
71                 return 0;
72
73         if (src->error) {
74                 ret = src->error;
75                 dvb_ringbuffer_flush(src);
76                 return ret;
77         }
78
79         for (todo = count; todo > 0; todo -= ret) {
80                 if (non_blocking && dvb_ringbuffer_empty(src)) {
81                         ret = -EWOULDBLOCK;
82                         break;
83                 }
84
85                 ret = wait_event_interruptible(src->queue,
86                                                !dvb_ringbuffer_empty(src) ||
87                                                (src->error != 0));
88                 if (ret < 0)
89                         break;
90
91                 if (src->error) {
92                         ret = src->error;
93                         dvb_ringbuffer_flush(src);
94                         break;
95                 }
96
97                 avail = dvb_ringbuffer_avail(src);
98                 if (avail > todo)
99                         avail = todo;
100
101                 ret = dvb_ringbuffer_read(src, buf, avail, 1);
102                 if (ret < 0)
103                         break;
104
105                 buf += ret;
106         }
107
108         return (count - todo) ? (count - todo) : ret;
109 }
110
111 static struct dmx_frontend *get_fe(struct dmx_demux *demux, int type)
112 {
113         struct list_head *head, *pos;
114
115         head = demux->get_frontends(demux);
116         if (!head)
117                 return NULL;
118         list_for_each(pos, head)
119                 if (DMX_FE_ENTRY(pos)->source == type)
120                         return DMX_FE_ENTRY(pos);
121
122         return NULL;
123 }
124
125 static int dvb_dvr_open(struct inode *inode, struct file *file)
126 {
127         struct dvb_device *dvbdev = file->private_data;
128         struct dmxdev *dmxdev = dvbdev->priv;
129         struct dmx_frontend *front;
130
131         dprintk("function : %s\n", __FUNCTION__);
132
133         if (mutex_lock_interruptible(&dmxdev->mutex))
134                 return -ERESTARTSYS;
135
136         if ((file->f_flags & O_ACCMODE) == O_RDWR) {
137                 if (!(dmxdev->capabilities & DMXDEV_CAP_DUPLEX)) {
138                         mutex_unlock(&dmxdev->mutex);
139                         return -EOPNOTSUPP;
140                 }
141         }
142
143         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
144                 void *mem = vmalloc(DVR_BUFFER_SIZE);
145                 if (!mem) {
146                         mutex_unlock(&dmxdev->mutex);
147                         return -ENOMEM;
148                 }
149                 dvb_ringbuffer_init(&dmxdev->dvr_buffer, mem, DVR_BUFFER_SIZE);
150         }
151
152         if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
153                 dmxdev->dvr_orig_fe = dmxdev->demux->frontend;
154
155                 if (!dmxdev->demux->write) {
156                         mutex_unlock(&dmxdev->mutex);
157                         return -EOPNOTSUPP;
158                 }
159
160                 front = get_fe(dmxdev->demux, DMX_MEMORY_FE);
161
162                 if (!front) {
163                         mutex_unlock(&dmxdev->mutex);
164                         return -EINVAL;
165                 }
166                 dmxdev->demux->disconnect_frontend(dmxdev->demux);
167                 dmxdev->demux->connect_frontend(dmxdev->demux, front);
168         }
169         mutex_unlock(&dmxdev->mutex);
170         return 0;
171 }
172
173 static int dvb_dvr_release(struct inode *inode, struct file *file)
174 {
175         struct dvb_device *dvbdev = file->private_data;
176         struct dmxdev *dmxdev = dvbdev->priv;
177
178         if (mutex_lock_interruptible(&dmxdev->mutex))
179                 return -ERESTARTSYS;
180
181         if ((file->f_flags & O_ACCMODE) == O_WRONLY) {
182                 dmxdev->demux->disconnect_frontend(dmxdev->demux);
183                 dmxdev->demux->connect_frontend(dmxdev->demux,
184                                                 dmxdev->dvr_orig_fe);
185         }
186         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
187                 if (dmxdev->dvr_buffer.data) {
188                         void *mem = dmxdev->dvr_buffer.data;
189                         mb();
190                         spin_lock_irq(&dmxdev->lock);
191                         dmxdev->dvr_buffer.data = NULL;
192                         spin_unlock_irq(&dmxdev->lock);
193                         vfree(mem);
194                 }
195         }
196         mutex_unlock(&dmxdev->mutex);
197         return 0;
198 }
199
200 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
201                              size_t count, loff_t *ppos)
202 {
203         struct dvb_device *dvbdev = file->private_data;
204         struct dmxdev *dmxdev = dvbdev->priv;
205         int ret;
206
207         if (!dmxdev->demux->write)
208                 return -EOPNOTSUPP;
209         if ((file->f_flags & O_ACCMODE) != O_WRONLY)
210                 return -EINVAL;
211         if (mutex_lock_interruptible(&dmxdev->mutex))
212                 return -ERESTARTSYS;
213         ret = dmxdev->demux->write(dmxdev->demux, buf, count);
214         mutex_unlock(&dmxdev->mutex);
215         return ret;
216 }
217
218 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
219                             loff_t *ppos)
220 {
221         struct dvb_device *dvbdev = file->private_data;
222         struct dmxdev *dmxdev = dvbdev->priv;
223         int ret;
224
225         //mutex_lock(&dmxdev->mutex);
226         ret = dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
227                                      file->f_flags & O_NONBLOCK,
228                                      buf, count, ppos);
229         //mutex_unlock(&dmxdev->mutex);
230         return ret;
231 }
232
233 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter
234                                                *dmxdevfilter, int state)
235 {
236         spin_lock_irq(&dmxdevfilter->dev->lock);
237         dmxdevfilter->state = state;
238         spin_unlock_irq(&dmxdevfilter->dev->lock);
239 }
240
241 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter,
242                                       unsigned long size)
243 {
244         struct dvb_ringbuffer *buf = &dmxdevfilter->buffer;
245         void *mem;
246
247         if (buf->size == size)
248                 return 0;
249         if (dmxdevfilter->state >= DMXDEV_STATE_GO)
250                 return -EBUSY;
251         spin_lock_irq(&dmxdevfilter->dev->lock);
252         mem = buf->data;
253         buf->data = NULL;
254         buf->size = size;
255         dvb_ringbuffer_flush(buf);
256         spin_unlock_irq(&dmxdevfilter->dev->lock);
257         vfree(mem);
258
259         if (buf->size) {
260                 mem = vmalloc(dmxdevfilter->buffer.size);
261                 if (!mem)
262                         return -ENOMEM;
263                 spin_lock_irq(&dmxdevfilter->dev->lock);
264                 buf->data = mem;
265                 spin_unlock_irq(&dmxdevfilter->dev->lock);
266         }
267         return 0;
268 }
269
270 static void dvb_dmxdev_filter_timeout(unsigned long data)
271 {
272         struct dmxdev_filter *dmxdevfilter = (struct dmxdev_filter *)data;
273
274         dmxdevfilter->buffer.error = -ETIMEDOUT;
275         spin_lock_irq(&dmxdevfilter->dev->lock);
276         dmxdevfilter->state = DMXDEV_STATE_TIMEDOUT;
277         spin_unlock_irq(&dmxdevfilter->dev->lock);
278         wake_up(&dmxdevfilter->buffer.queue);
279 }
280
281 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
282 {
283         struct dmx_sct_filter_params *para = &dmxdevfilter->params.sec;
284
285         del_timer(&dmxdevfilter->timer);
286         if (para->timeout) {
287                 dmxdevfilter->timer.function = dvb_dmxdev_filter_timeout;
288                 dmxdevfilter->timer.data = (unsigned long)dmxdevfilter;
289                 dmxdevfilter->timer.expires =
290                     jiffies + 1 + (HZ / 2 + HZ * para->timeout) / 1000;
291                 add_timer(&dmxdevfilter->timer);
292         }
293 }
294
295 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
296                                        const u8 *buffer2, size_t buffer2_len,
297                                        struct dmx_section_filter *filter,
298                                        enum dmx_success success)
299 {
300         struct dmxdev_filter *dmxdevfilter = filter->priv;
301         int ret;
302
303         if (dmxdevfilter->buffer.error) {
304                 wake_up(&dmxdevfilter->buffer.queue);
305                 return 0;
306         }
307         spin_lock(&dmxdevfilter->dev->lock);
308         if (dmxdevfilter->state != DMXDEV_STATE_GO) {
309                 spin_unlock(&dmxdevfilter->dev->lock);
310                 return 0;
311         }
312         del_timer(&dmxdevfilter->timer);
313         dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
314                 buffer1[0], buffer1[1],
315                 buffer1[2], buffer1[3], buffer1[4], buffer1[5]);
316         ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1,
317                                       buffer1_len);
318         if (ret == buffer1_len) {
319                 ret = dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2,
320                                               buffer2_len);
321         }
322         if (ret < 0) {
323                 dvb_ringbuffer_flush(&dmxdevfilter->buffer);
324                 dmxdevfilter->buffer.error = ret;
325         }
326         if (dmxdevfilter->params.sec.flags & DMX_ONESHOT)
327                 dmxdevfilter->state = DMXDEV_STATE_DONE;
328         spin_unlock(&dmxdevfilter->dev->lock);
329         wake_up(&dmxdevfilter->buffer.queue);
330         return 0;
331 }
332
333 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
334                                   const u8 *buffer2, size_t buffer2_len,
335                                   struct dmx_ts_feed *feed,
336                                   enum dmx_success success)
337 {
338         struct dmxdev_filter *dmxdevfilter = feed->priv;
339         struct dvb_ringbuffer *buffer;
340         int ret;
341
342         spin_lock(&dmxdevfilter->dev->lock);
343         if (dmxdevfilter->params.pes.output == DMX_OUT_DECODER) {
344                 spin_unlock(&dmxdevfilter->dev->lock);
345                 return 0;
346         }
347
348         if (dmxdevfilter->params.pes.output == DMX_OUT_TAP)
349                 buffer = &dmxdevfilter->buffer;
350         else
351                 buffer = &dmxdevfilter->dev->dvr_buffer;
352         if (buffer->error) {
353                 spin_unlock(&dmxdevfilter->dev->lock);
354                 wake_up(&buffer->queue);
355                 return 0;
356         }
357         ret = dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
358         if (ret == buffer1_len)
359                 ret = dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
360         if (ret < 0) {
361                 dvb_ringbuffer_flush(buffer);
362                 buffer->error = ret;
363         }
364         spin_unlock(&dmxdevfilter->dev->lock);
365         wake_up(&buffer->queue);
366         return 0;
367 }
368
369 /* stop feed but only mark the specified filter as stopped (state set) */
370 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
371 {
372         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
373
374         switch (dmxdevfilter->type) {
375         case DMXDEV_TYPE_SEC:
376                 del_timer(&dmxdevfilter->timer);
377                 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
378                 break;
379         case DMXDEV_TYPE_PES:
380                 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
381                 break;
382         default:
383                 return -EINVAL;
384         }
385         return 0;
386 }
387
388 /* start feed associated with the specified filter */
389 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
390 {
391         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
392
393         switch (filter->type) {
394         case DMXDEV_TYPE_SEC:
395                 return filter->feed.sec->start_filtering(filter->feed.sec);
396         case DMXDEV_TYPE_PES:
397                 return filter->feed.ts->start_filtering(filter->feed.ts);
398         default:
399                 return -EINVAL;
400         }
401
402         return 0;
403 }
404
405 /* restart section feed if it has filters left associated with it,
406    otherwise release the feed */
407 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
408 {
409         int i;
410         struct dmxdev *dmxdev = filter->dev;
411         u16 pid = filter->params.sec.pid;
412
413         for (i = 0; i < dmxdev->filternum; i++)
414                 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
415                     dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
416                     dmxdev->filter[i].params.sec.pid == pid) {
417                         dvb_dmxdev_feed_start(&dmxdev->filter[i]);
418                         return 0;
419                 }
420
421         filter->dev->demux->release_section_feed(dmxdev->demux,
422                                                  filter->feed.sec);
423
424         return 0;
425 }
426
427 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
428 {
429         if (dmxdevfilter->state < DMXDEV_STATE_GO)
430                 return 0;
431
432         switch (dmxdevfilter->type) {
433         case DMXDEV_TYPE_SEC:
434                 if (!dmxdevfilter->feed.sec)
435                         break;
436                 dvb_dmxdev_feed_stop(dmxdevfilter);
437                 if (dmxdevfilter->filter.sec)
438                         dmxdevfilter->feed.sec->
439                             release_filter(dmxdevfilter->feed.sec,
440                                            dmxdevfilter->filter.sec);
441                 dvb_dmxdev_feed_restart(dmxdevfilter);
442                 dmxdevfilter->feed.sec = NULL;
443                 break;
444         case DMXDEV_TYPE_PES:
445                 if (!dmxdevfilter->feed.ts)
446                         break;
447                 dvb_dmxdev_feed_stop(dmxdevfilter);
448                 dmxdevfilter->dev->demux->
449                     release_ts_feed(dmxdevfilter->dev->demux,
450                                     dmxdevfilter->feed.ts);
451                 dmxdevfilter->feed.ts = NULL;
452                 break;
453         default:
454                 if (dmxdevfilter->state == DMXDEV_STATE_ALLOCATED)
455                         return 0;
456                 return -EINVAL;
457         }
458
459         dvb_ringbuffer_flush(&dmxdevfilter->buffer);
460         return 0;
461 }
462
463 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
464 {
465         if (dmxdevfilter->state < DMXDEV_STATE_SET)
466                 return 0;
467
468         dmxdevfilter->type = DMXDEV_TYPE_NONE;
469         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
470         return 0;
471 }
472
473 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
474 {
475         struct dmxdev *dmxdev = filter->dev;
476         void *mem;
477         int ret, i;
478
479         if (filter->state < DMXDEV_STATE_SET)
480                 return -EINVAL;
481
482         if (filter->state >= DMXDEV_STATE_GO)
483                 dvb_dmxdev_filter_stop(filter);
484
485         if (!filter->buffer.data) {
486                 mem = vmalloc(filter->buffer.size);
487                 if (!mem)
488                         return -ENOMEM;
489                 spin_lock_irq(&filter->dev->lock);
490                 filter->buffer.data = mem;
491                 spin_unlock_irq(&filter->dev->lock);
492         }
493
494         dvb_ringbuffer_flush(&filter->buffer);
495
496         switch (filter->type) {
497         case DMXDEV_TYPE_SEC:
498         {
499                 struct dmx_sct_filter_params *para = &filter->params.sec;
500                 struct dmx_section_filter **secfilter = &filter->filter.sec;
501                 struct dmx_section_feed **secfeed = &filter->feed.sec;
502
503                 *secfilter = NULL;
504                 *secfeed = NULL;
505
506
507                 /* find active filter/feed with same PID */
508                 for (i = 0; i < dmxdev->filternum; i++) {
509                         if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
510                             dmxdev->filter[i].type == DMXDEV_TYPE_SEC &&
511                             dmxdev->filter[i].params.sec.pid == para->pid) {
512                                 *secfeed = dmxdev->filter[i].feed.sec;
513                                 break;
514                         }
515                 }
516
517                 /* if no feed found, try to allocate new one */
518                 if (!*secfeed) {
519                         ret = dmxdev->demux->allocate_section_feed(dmxdev->demux,
520                                                                    secfeed,
521                                                                    dvb_dmxdev_section_callback);
522                         if (ret < 0) {
523                                 printk("DVB (%s): could not alloc feed\n",
524                                        __FUNCTION__);
525                                 return ret;
526                         }
527
528                         ret = (*secfeed)->set(*secfeed, para->pid, 32768,
529                                               (para->flags & DMX_CHECK_CRC) ? 1 : 0);
530                         if (ret < 0) {
531                                 printk("DVB (%s): could not set feed\n",
532                                        __FUNCTION__);
533                                 dvb_dmxdev_feed_restart(filter);
534                                 return ret;
535                         }
536                 } else {
537                         dvb_dmxdev_feed_stop(filter);
538                 }
539
540                 ret = (*secfeed)->allocate_filter(*secfeed, secfilter);
541                 if (ret < 0) {
542                         dvb_dmxdev_feed_restart(filter);
543                         filter->feed.sec->start_filtering(*secfeed);
544                         dprintk("could not get filter\n");
545                         return ret;
546                 }
547
548                 (*secfilter)->priv = filter;
549
550                 memcpy(&((*secfilter)->filter_value[3]),
551                        &(para->filter.filter[1]), DMX_FILTER_SIZE - 1);
552                 memcpy(&(*secfilter)->filter_mask[3],
553                        &para->filter.mask[1], DMX_FILTER_SIZE - 1);
554                 memcpy(&(*secfilter)->filter_mode[3],
555                        &para->filter.mode[1], DMX_FILTER_SIZE - 1);
556
557                 (*secfilter)->filter_value[0] = para->filter.filter[0];
558                 (*secfilter)->filter_mask[0] = para->filter.mask[0];
559                 (*secfilter)->filter_mode[0] = para->filter.mode[0];
560                 (*secfilter)->filter_mask[1] = 0;
561                 (*secfilter)->filter_mask[2] = 0;
562
563                 filter->todo = 0;
564
565                 ret = filter->feed.sec->start_filtering(filter->feed.sec);
566                 if (ret < 0)
567                         return ret;
568
569                 dvb_dmxdev_filter_timer(filter);
570                 break;
571         }
572         case DMXDEV_TYPE_PES:
573         {
574                 struct timespec timeout = { 0 };
575                 struct dmx_pes_filter_params *para = &filter->params.pes;
576                 dmx_output_t otype;
577                 int ret;
578                 int ts_type;
579                 enum dmx_ts_pes ts_pes;
580                 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
581
582                 filter->feed.ts = NULL;
583                 otype = para->output;
584
585                 ts_pes = (enum dmx_ts_pes)para->pes_type;
586
587                 if (ts_pes < DMX_PES_OTHER)
588                         ts_type = TS_DECODER;
589                 else
590                         ts_type = 0;
591
592                 if (otype == DMX_OUT_TS_TAP)
593                         ts_type |= TS_PACKET;
594
595                 if (otype == DMX_OUT_TAP)
596                         ts_type |= TS_PAYLOAD_ONLY | TS_PACKET;
597
598                 ret = dmxdev->demux->allocate_ts_feed(dmxdev->demux,
599                                                       tsfeed,
600                                                       dvb_dmxdev_ts_callback);
601                 if (ret < 0)
602                         return ret;
603
604                 (*tsfeed)->priv = filter;
605
606                 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
607                                      32768, timeout);
608                 if (ret < 0) {
609                         dmxdev->demux->release_ts_feed(dmxdev->demux,
610                                                        *tsfeed);
611                         return ret;
612                 }
613
614                 ret = filter->feed.ts->start_filtering(filter->feed.ts);
615                 if (ret < 0) {
616                         dmxdev->demux->release_ts_feed(dmxdev->demux,
617                                                        *tsfeed);
618                         return ret;
619                 }
620
621                 break;
622         }
623         default:
624                 return -EINVAL;
625         }
626
627         dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
628         return 0;
629 }
630
631 static int dvb_demux_open(struct inode *inode, struct file *file)
632 {
633         struct dvb_device *dvbdev = file->private_data;
634         struct dmxdev *dmxdev = dvbdev->priv;
635         int i;
636         struct dmxdev_filter *dmxdevfilter;
637
638         if (!dmxdev->filter)
639                 return -EINVAL;
640
641         if (mutex_lock_interruptible(&dmxdev->mutex))
642                 return -ERESTARTSYS;
643
644         for (i = 0; i < dmxdev->filternum; i++)
645                 if (dmxdev->filter[i].state == DMXDEV_STATE_FREE)
646                         break;
647
648         if (i == dmxdev->filternum) {
649                 mutex_unlock(&dmxdev->mutex);
650                 return -EMFILE;
651         }
652
653         dmxdevfilter = &dmxdev->filter[i];
654         mutex_init(&dmxdevfilter->mutex);
655         file->private_data = dmxdevfilter;
656
657         dvb_ringbuffer_init(&dmxdevfilter->buffer, NULL, 8192);
658         dmxdevfilter->type = DMXDEV_TYPE_NONE;
659         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
660         dmxdevfilter->feed.ts = NULL;
661         init_timer(&dmxdevfilter->timer);
662
663         mutex_unlock(&dmxdev->mutex);
664         return 0;
665 }
666
667 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev,
668                                   struct dmxdev_filter *dmxdevfilter)
669 {
670         if (mutex_lock_interruptible(&dmxdev->mutex))
671                 return -ERESTARTSYS;
672
673         if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
674                 mutex_unlock(&dmxdev->mutex);
675                 return -ERESTARTSYS;
676         }
677
678         dvb_dmxdev_filter_stop(dmxdevfilter);
679         dvb_dmxdev_filter_reset(dmxdevfilter);
680
681         if (dmxdevfilter->buffer.data) {
682                 void *mem = dmxdevfilter->buffer.data;
683
684                 spin_lock_irq(&dmxdev->lock);
685                 dmxdevfilter->buffer.data = NULL;
686                 spin_unlock_irq(&dmxdev->lock);
687                 vfree(mem);
688         }
689
690         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
691         wake_up(&dmxdevfilter->buffer.queue);
692         mutex_unlock(&dmxdevfilter->mutex);
693         mutex_unlock(&dmxdev->mutex);
694         return 0;
695 }
696
697 static inline void invert_mode(dmx_filter_t *filter)
698 {
699         int i;
700
701         for (i = 0; i < DMX_FILTER_SIZE; i++)
702                 filter->mode[i] ^= 0xff;
703 }
704
705 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
706                                  struct dmxdev_filter *dmxdevfilter,
707                                  struct dmx_sct_filter_params *params)
708 {
709         dprintk("function : %s\n", __FUNCTION__);
710
711         dvb_dmxdev_filter_stop(dmxdevfilter);
712
713         dmxdevfilter->type = DMXDEV_TYPE_SEC;
714         memcpy(&dmxdevfilter->params.sec,
715                params, sizeof(struct dmx_sct_filter_params));
716         invert_mode(&dmxdevfilter->params.sec.filter);
717         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
718
719         if (params->flags & DMX_IMMEDIATE_START)
720                 return dvb_dmxdev_filter_start(dmxdevfilter);
721
722         return 0;
723 }
724
725 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
726                                      struct dmxdev_filter *dmxdevfilter,
727                                      struct dmx_pes_filter_params *params)
728 {
729         dvb_dmxdev_filter_stop(dmxdevfilter);
730
731         if (params->pes_type > DMX_PES_OTHER || params->pes_type < 0)
732                 return -EINVAL;
733
734         dmxdevfilter->type = DMXDEV_TYPE_PES;
735         memcpy(&dmxdevfilter->params, params,
736                sizeof(struct dmx_pes_filter_params));
737
738         dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
739
740         if (params->flags & DMX_IMMEDIATE_START)
741                 return dvb_dmxdev_filter_start(dmxdevfilter);
742
743         return 0;
744 }
745
746 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
747                                    struct file *file, char __user *buf,
748                                    size_t count, loff_t *ppos)
749 {
750         int result, hcount;
751         int done = 0;
752
753         if (dfil->todo <= 0) {
754                 hcount = 3 + dfil->todo;
755                 if (hcount > count)
756                         hcount = count;
757                 result = dvb_dmxdev_buffer_read(&dfil->buffer,
758                                                 file->f_flags & O_NONBLOCK,
759                                                 buf, hcount, ppos);
760                 if (result < 0) {
761                         dfil->todo = 0;
762                         return result;
763                 }
764                 if (copy_from_user(dfil->secheader - dfil->todo, buf, result))
765                         return -EFAULT;
766                 buf += result;
767                 done = result;
768                 count -= result;
769                 dfil->todo -= result;
770                 if (dfil->todo > -3)
771                         return done;
772                 dfil->todo = ((dfil->secheader[1] << 8) | dfil->secheader[2]) & 0xfff;
773                 if (!count)
774                         return done;
775         }
776         if (count > dfil->todo)
777                 count = dfil->todo;
778         result = dvb_dmxdev_buffer_read(&dfil->buffer,
779                                         file->f_flags & O_NONBLOCK,
780                                         buf, count, ppos);
781         if (result < 0)
782                 return result;
783         dfil->todo -= result;
784         return (result + done);
785 }
786
787 static ssize_t
788 dvb_demux_read(struct file *file, char __user *buf, size_t count,
789                loff_t *ppos)
790 {
791         struct dmxdev_filter *dmxdevfilter = file->private_data;
792         int ret;
793
794         if (mutex_lock_interruptible(&dmxdevfilter->mutex))
795                 return -ERESTARTSYS;
796
797         if (dmxdevfilter->type == DMXDEV_TYPE_SEC)
798                 ret = dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
799         else
800                 ret = dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
801                                              file->f_flags & O_NONBLOCK,
802                                              buf, count, ppos);
803
804         mutex_unlock(&dmxdevfilter->mutex);
805         return ret;
806 }
807
808 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
809                               unsigned int cmd, void *parg)
810 {
811         struct dmxdev_filter *dmxdevfilter = file->private_data;
812         struct dmxdev *dmxdev = dmxdevfilter->dev;
813         unsigned long arg = (unsigned long)parg;
814         int ret = 0;
815
816         if (mutex_lock_interruptible(&dmxdev->mutex))
817                 return -ERESTARTSYS;
818
819         switch (cmd) {
820         case DMX_START:
821                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
822                         mutex_unlock(&dmxdev->mutex);
823                         return -ERESTARTSYS;
824                 }
825                 if (dmxdevfilter->state < DMXDEV_STATE_SET)
826                         ret = -EINVAL;
827                 else
828                         ret = dvb_dmxdev_filter_start(dmxdevfilter);
829                 mutex_unlock(&dmxdevfilter->mutex);
830                 break;
831
832         case DMX_STOP:
833                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
834                         mutex_unlock(&dmxdev->mutex);
835                         return -ERESTARTSYS;
836                 }
837                 ret = dvb_dmxdev_filter_stop(dmxdevfilter);
838                 mutex_unlock(&dmxdevfilter->mutex);
839                 break;
840
841         case DMX_SET_FILTER:
842                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
843                         mutex_unlock(&dmxdev->mutex);
844                         return -ERESTARTSYS;
845                 }
846                 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter, parg);
847                 mutex_unlock(&dmxdevfilter->mutex);
848                 break;
849
850         case DMX_SET_PES_FILTER:
851                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
852                         mutex_unlock(&dmxdev->mutex);
853                         return -ERESTARTSYS;
854                 }
855                 ret = dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter, parg);
856                 mutex_unlock(&dmxdevfilter->mutex);
857                 break;
858
859         case DMX_SET_BUFFER_SIZE:
860                 if (mutex_lock_interruptible(&dmxdevfilter->mutex)) {
861                         mutex_unlock(&dmxdev->mutex);
862                         return -ERESTARTSYS;
863                 }
864                 ret = dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
865                 mutex_unlock(&dmxdevfilter->mutex);
866                 break;
867
868         case DMX_GET_EVENT:
869                 break;
870
871         case DMX_GET_PES_PIDS:
872                 if (!dmxdev->demux->get_pes_pids) {
873                         ret = -EINVAL;
874                         break;
875                 }
876                 dmxdev->demux->get_pes_pids(dmxdev->demux, parg);
877                 break;
878
879         case DMX_GET_CAPS:
880                 if (!dmxdev->demux->get_caps) {
881                         ret = -EINVAL;
882                         break;
883                 }
884                 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
885                 break;
886
887         case DMX_SET_SOURCE:
888                 if (!dmxdev->demux->set_source) {
889                         ret = -EINVAL;
890                         break;
891                 }
892                 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
893                 break;
894
895         case DMX_GET_STC:
896                 if (!dmxdev->demux->get_stc) {
897                         ret = -EINVAL;
898                         break;
899                 }
900                 ret = dmxdev->demux->get_stc(dmxdev->demux,
901                                              ((struct dmx_stc *)parg)->num,
902                                              &((struct dmx_stc *)parg)->stc,
903                                              &((struct dmx_stc *)parg)->base);
904                 break;
905
906         default:
907                 ret = -EINVAL;
908                 break;
909         }
910         mutex_unlock(&dmxdev->mutex);
911         return ret;
912 }
913
914 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
915                            unsigned int cmd, unsigned long arg)
916 {
917         return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
918 }
919
920 static unsigned int dvb_demux_poll(struct file *file, poll_table *wait)
921 {
922         struct dmxdev_filter *dmxdevfilter = file->private_data;
923         unsigned int mask = 0;
924
925         if (!dmxdevfilter)
926                 return -EINVAL;
927
928         poll_wait(file, &dmxdevfilter->buffer.queue, wait);
929
930         if (dmxdevfilter->state != DMXDEV_STATE_GO &&
931             dmxdevfilter->state != DMXDEV_STATE_DONE &&
932             dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
933                 return 0;
934
935         if (dmxdevfilter->buffer.error)
936                 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
937
938         if (!dvb_ringbuffer_empty(&dmxdevfilter->buffer))
939                 mask |= (POLLIN | POLLRDNORM | POLLPRI);
940
941         return mask;
942 }
943
944 static int dvb_demux_release(struct inode *inode, struct file *file)
945 {
946         struct dmxdev_filter *dmxdevfilter = file->private_data;
947         struct dmxdev *dmxdev = dmxdevfilter->dev;
948
949         return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
950 }
951
952 static struct file_operations dvb_demux_fops = {
953         .owner = THIS_MODULE,
954         .read = dvb_demux_read,
955         .ioctl = dvb_demux_ioctl,
956         .open = dvb_demux_open,
957         .release = dvb_demux_release,
958         .poll = dvb_demux_poll,
959 };
960
961 static struct dvb_device dvbdev_demux = {
962         .priv = NULL,
963         .users = 1,
964         .writers = 1,
965         .fops = &dvb_demux_fops
966 };
967
968 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
969                             unsigned int cmd, void *parg)
970 {
971         struct dvb_device *dvbdev = file->private_data;
972         struct dmxdev *dmxdev = dvbdev->priv;
973         int ret;
974
975         if (mutex_lock_interruptible(&dmxdev->mutex))
976                 return -ERESTARTSYS;
977
978         switch (cmd) {
979         case DMX_SET_BUFFER_SIZE:
980                 // FIXME: implement
981                 ret = 0;
982                 break;
983
984         default:
985                 ret = -EINVAL;
986                 break;
987         }
988         mutex_unlock(&dmxdev->mutex);
989         return ret;
990 }
991
992 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
993                          unsigned int cmd, unsigned long arg)
994 {
995         return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
996 }
997
998 static unsigned int dvb_dvr_poll(struct file *file, poll_table *wait)
999 {
1000         struct dvb_device *dvbdev = file->private_data;
1001         struct dmxdev *dmxdev = dvbdev->priv;
1002         unsigned int mask = 0;
1003
1004         dprintk("function : %s\n", __FUNCTION__);
1005
1006         poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1007
1008         if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1009                 if (dmxdev->dvr_buffer.error)
1010                         mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1011
1012                 if (!dvb_ringbuffer_empty(&dmxdev->dvr_buffer))
1013                         mask |= (POLLIN | POLLRDNORM | POLLPRI);
1014         } else
1015                 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1016
1017         return mask;
1018 }
1019
1020 static struct file_operations dvb_dvr_fops = {
1021         .owner = THIS_MODULE,
1022         .read = dvb_dvr_read,
1023         .write = dvb_dvr_write,
1024         .ioctl = dvb_dvr_ioctl,
1025         .open = dvb_dvr_open,
1026         .release = dvb_dvr_release,
1027         .poll = dvb_dvr_poll,
1028 };
1029
1030 static struct dvb_device dvbdev_dvr = {
1031         .priv = NULL,
1032         .users = 1,
1033         .writers = 1,
1034         .fops = &dvb_dvr_fops
1035 };
1036
1037 int dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1038 {
1039         int i;
1040
1041         if (dmxdev->demux->open(dmxdev->demux) < 0)
1042                 return -EUSERS;
1043
1044         dmxdev->filter = vmalloc(dmxdev->filternum * sizeof(struct dmxdev_filter));
1045         if (!dmxdev->filter)
1046                 return -ENOMEM;
1047
1048         mutex_init(&dmxdev->mutex);
1049         spin_lock_init(&dmxdev->lock);
1050         for (i = 0; i < dmxdev->filternum; i++) {
1051                 dmxdev->filter[i].dev = dmxdev;
1052                 dmxdev->filter[i].buffer.data = NULL;
1053                 dvb_dmxdev_filter_state_set(&dmxdev->filter[i],
1054                                             DMXDEV_STATE_FREE);
1055         }
1056
1057         dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev,
1058                             DVB_DEVICE_DEMUX);
1059         dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr,
1060                             dmxdev, DVB_DEVICE_DVR);
1061
1062         dvb_ringbuffer_init(&dmxdev->dvr_buffer, NULL, 8192);
1063
1064         return 0;
1065 }
1066
1067 EXPORT_SYMBOL(dvb_dmxdev_init);
1068
1069 void dvb_dmxdev_release(struct dmxdev *dmxdev)
1070 {
1071         dvb_unregister_device(dmxdev->dvbdev);
1072         dvb_unregister_device(dmxdev->dvr_dvbdev);
1073
1074         vfree(dmxdev->filter);
1075         dmxdev->filter = NULL;
1076         dmxdev->demux->close(dmxdev->demux);
1077 }
1078
1079 EXPORT_SYMBOL(dvb_dmxdev_release);