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