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