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