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