[ARM] Support register switch in nommu mode
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dmxdev.c
blob7b8373ad121b051cccbcbd675ca32e4f9d16efaa
1 /*
2 * dmxdev.c - DVB demultiplexer device
4 * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
5 * & Marcus Metzler <marcus@convergence.de>
6 for convergence integrated media GmbH
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.
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.
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.
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>
36 #include "dmxdev.h"
38 static int debug;
40 module_param(debug, int, 0644);
41 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
43 #define dprintk if (debug) printk
45 static inline void dvb_dmxdev_buffer_init(struct dmxdev_buffer *buffer)
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);
55 static inline int dvb_dmxdev_buffer_write(struct dmxdev_buffer *buf, const u8 *src, int len)
57 int split;
58 int free;
59 int todo;
61 if (!len)
62 return 0;
63 if (!buf->data)
64 return 0;
66 free=buf->pread-buf->pwrite;
67 split=0;
68 if (free<=0) {
69 free+=buf->size;
70 split=buf->size-buf->pwrite;
72 if (len>=free) {
73 dprintk("dmxdev: buffer overflow\n");
74 return -1;
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;
84 memcpy(buf->data + buf->pwrite, src+split, todo);
85 buf->pwrite=(buf->pwrite+todo)%buf->size;
86 return len;
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)
92 unsigned long todo=count;
93 int split, avail, error;
95 if (!src->data)
96 return 0;
98 if ((error=src->error)) {
99 src->pwrite=src->pread;
100 src->error=0;
101 return error;
104 if (non_blocking && (src->pwrite==src->pread))
105 return -EWOULDBLOCK;
107 while (todo>0) {
108 if (non_blocking && (src->pwrite==src->pread))
109 return (count-todo) ? (count-todo) : -EWOULDBLOCK;
111 if (wait_event_interruptible(src->queue,
112 (src->pread!=src->pwrite) ||
113 (src->error))<0)
114 return count-todo;
116 if ((error=src->error)) {
117 src->pwrite=src->pread;
118 src->error=0;
119 return error;
122 split=src->size;
123 avail=src->pwrite - src->pread;
124 if (avail<0) {
125 avail+=src->size;
126 split=src->size - src->pread;
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;
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;
146 return count;
149 static struct dmx_frontend * get_fe(struct dmx_demux *demux, int type)
151 struct list_head *head, *pos;
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);
160 return NULL;
163 static inline void dvb_dmxdev_dvr_state_set(struct dmxdev_dvr *dmxdevdvr, int state)
165 spin_lock_irq(&dmxdevdvr->dev->lock);
166 dmxdevdvr->state=state;
167 spin_unlock_irq(&dmxdevdvr->dev->lock);
170 static int dvb_dvr_open(struct inode *inode, struct file *file)
172 struct dvb_device *dvbdev = file->private_data;
173 struct dmxdev *dmxdev = dvbdev->priv;
174 struct dmx_frontend *front;
176 dprintk ("function : %s\n", __FUNCTION__);
178 if (down_interruptible (&dmxdev->mutex))
179 return -ERESTARTSYS;
181 if ((file->f_flags&O_ACCMODE)==O_RDWR) {
182 if (!(dmxdev->capabilities&DMXDEV_CAP_DUPLEX)) {
183 up(&dmxdev->mutex);
184 return -EOPNOTSUPP;
188 if ((file->f_flags&O_ACCMODE)==O_RDONLY) {
189 dvb_dmxdev_buffer_init(&dmxdev->dvr_buffer);
190 dmxdev->dvr_buffer.size=DVR_BUFFER_SIZE;
191 dmxdev->dvr_buffer.data=vmalloc(DVR_BUFFER_SIZE);
192 if (!dmxdev->dvr_buffer.data) {
193 up(&dmxdev->mutex);
194 return -ENOMEM;
198 if ((file->f_flags&O_ACCMODE)==O_WRONLY) {
199 dmxdev->dvr_orig_fe=dmxdev->demux->frontend;
201 if (!dmxdev->demux->write) {
202 up(&dmxdev->mutex);
203 return -EOPNOTSUPP;
206 front=get_fe(dmxdev->demux, DMX_MEMORY_FE);
208 if (!front) {
209 up(&dmxdev->mutex);
210 return -EINVAL;
212 dmxdev->demux->disconnect_frontend(dmxdev->demux);
213 dmxdev->demux->connect_frontend(dmxdev->demux, front);
215 up(&dmxdev->mutex);
216 return 0;
219 static int dvb_dvr_release(struct inode *inode, struct file *file)
221 struct dvb_device *dvbdev = file->private_data;
222 struct dmxdev *dmxdev = dvbdev->priv;
224 if (down_interruptible (&dmxdev->mutex))
225 return -ERESTARTSYS;
227 if ((file->f_flags&O_ACCMODE)==O_WRONLY) {
228 dmxdev->demux->disconnect_frontend(dmxdev->demux);
229 dmxdev->demux->connect_frontend(dmxdev->demux,
230 dmxdev->dvr_orig_fe);
232 if ((file->f_flags&O_ACCMODE)==O_RDONLY) {
233 if (dmxdev->dvr_buffer.data) {
234 void *mem=dmxdev->dvr_buffer.data;
235 mb();
236 spin_lock_irq(&dmxdev->lock);
237 dmxdev->dvr_buffer.data=NULL;
238 spin_unlock_irq(&dmxdev->lock);
239 vfree(mem);
242 up(&dmxdev->mutex);
243 return 0;
246 static ssize_t dvb_dvr_write(struct file *file, const char __user *buf,
247 size_t count, loff_t *ppos)
249 struct dvb_device *dvbdev = file->private_data;
250 struct dmxdev *dmxdev = dvbdev->priv;
251 int ret;
253 if (!dmxdev->demux->write)
254 return -EOPNOTSUPP;
255 if ((file->f_flags&O_ACCMODE)!=O_WRONLY)
256 return -EINVAL;
257 if (down_interruptible (&dmxdev->mutex))
258 return -ERESTARTSYS;
259 ret=dmxdev->demux->write(dmxdev->demux, buf, count);
260 up(&dmxdev->mutex);
261 return ret;
264 static ssize_t dvb_dvr_read(struct file *file, char __user *buf, size_t count,
265 loff_t *ppos)
267 struct dvb_device *dvbdev = file->private_data;
268 struct dmxdev *dmxdev = dvbdev->priv;
269 int ret;
271 //down(&dmxdev->mutex);
272 ret= dvb_dmxdev_buffer_read(&dmxdev->dvr_buffer,
273 file->f_flags&O_NONBLOCK,
274 buf, count, ppos);
275 //up(&dmxdev->mutex);
276 return ret;
279 static inline void dvb_dmxdev_filter_state_set(struct dmxdev_filter *dmxdevfilter, int state)
281 spin_lock_irq(&dmxdevfilter->dev->lock);
282 dmxdevfilter->state=state;
283 spin_unlock_irq(&dmxdevfilter->dev->lock);
286 static int dvb_dmxdev_set_buffer_size(struct dmxdev_filter *dmxdevfilter, unsigned long size)
288 struct dmxdev_buffer *buf=&dmxdevfilter->buffer;
289 void *mem;
291 if (buf->size==size)
292 return 0;
293 if (dmxdevfilter->state>=DMXDEV_STATE_GO)
294 return -EBUSY;
295 spin_lock_irq(&dmxdevfilter->dev->lock);
296 mem=buf->data;
297 buf->data=NULL;
298 buf->size=size;
299 buf->pwrite=buf->pread=0;
300 spin_unlock_irq(&dmxdevfilter->dev->lock);
301 vfree(mem);
303 if (buf->size) {
304 mem=vmalloc(dmxdevfilter->buffer.size);
305 if (!mem)
306 return -ENOMEM;
307 spin_lock_irq(&dmxdevfilter->dev->lock);
308 buf->data=mem;
309 spin_unlock_irq(&dmxdevfilter->dev->lock);
311 return 0;
314 static void dvb_dmxdev_filter_timeout(unsigned long data)
316 struct dmxdev_filter *dmxdevfilter=(struct dmxdev_filter *)data;
318 dmxdevfilter->buffer.error=-ETIMEDOUT;
319 spin_lock_irq(&dmxdevfilter->dev->lock);
320 dmxdevfilter->state=DMXDEV_STATE_TIMEDOUT;
321 spin_unlock_irq(&dmxdevfilter->dev->lock);
322 wake_up(&dmxdevfilter->buffer.queue);
325 static void dvb_dmxdev_filter_timer(struct dmxdev_filter *dmxdevfilter)
327 struct dmx_sct_filter_params *para=&dmxdevfilter->params.sec;
329 del_timer(&dmxdevfilter->timer);
330 if (para->timeout) {
331 dmxdevfilter->timer.function=dvb_dmxdev_filter_timeout;
332 dmxdevfilter->timer.data=(unsigned long) dmxdevfilter;
333 dmxdevfilter->timer.expires=jiffies+1+(HZ/2+HZ*para->timeout)/1000;
334 add_timer(&dmxdevfilter->timer);
338 static int dvb_dmxdev_section_callback(const u8 *buffer1, size_t buffer1_len,
339 const u8 *buffer2, size_t buffer2_len,
340 struct dmx_section_filter *filter, enum dmx_success success)
342 struct dmxdev_filter *dmxdevfilter = filter->priv;
343 int ret;
345 if (dmxdevfilter->buffer.error) {
346 wake_up(&dmxdevfilter->buffer.queue);
347 return 0;
349 spin_lock(&dmxdevfilter->dev->lock);
350 if (dmxdevfilter->state!=DMXDEV_STATE_GO) {
351 spin_unlock(&dmxdevfilter->dev->lock);
352 return 0;
354 del_timer(&dmxdevfilter->timer);
355 dprintk("dmxdev: section callback %02x %02x %02x %02x %02x %02x\n",
356 buffer1[0], buffer1[1],
357 buffer1[2], buffer1[3],
358 buffer1[4], buffer1[5]);
359 ret=dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer1, buffer1_len);
360 if (ret==buffer1_len) {
361 ret=dvb_dmxdev_buffer_write(&dmxdevfilter->buffer, buffer2, buffer2_len);
363 if (ret<0) {
364 dmxdevfilter->buffer.pwrite=dmxdevfilter->buffer.pread;
365 dmxdevfilter->buffer.error=-EOVERFLOW;
367 if (dmxdevfilter->params.sec.flags&DMX_ONESHOT)
368 dmxdevfilter->state=DMXDEV_STATE_DONE;
369 spin_unlock(&dmxdevfilter->dev->lock);
370 wake_up(&dmxdevfilter->buffer.queue);
371 return 0;
374 static int dvb_dmxdev_ts_callback(const u8 *buffer1, size_t buffer1_len,
375 const u8 *buffer2, size_t buffer2_len,
376 struct dmx_ts_feed *feed, enum dmx_success success)
378 struct dmxdev_filter *dmxdevfilter = feed->priv;
379 struct dmxdev_buffer *buffer;
380 int ret;
382 spin_lock(&dmxdevfilter->dev->lock);
383 if (dmxdevfilter->params.pes.output==DMX_OUT_DECODER) {
384 spin_unlock(&dmxdevfilter->dev->lock);
385 return 0;
388 if (dmxdevfilter->params.pes.output==DMX_OUT_TAP)
389 buffer=&dmxdevfilter->buffer;
390 else
391 buffer=&dmxdevfilter->dev->dvr_buffer;
392 if (buffer->error) {
393 spin_unlock(&dmxdevfilter->dev->lock);
394 wake_up(&buffer->queue);
395 return 0;
397 ret=dvb_dmxdev_buffer_write(buffer, buffer1, buffer1_len);
398 if (ret==buffer1_len)
399 ret=dvb_dmxdev_buffer_write(buffer, buffer2, buffer2_len);
400 if (ret<0) {
401 buffer->pwrite=buffer->pread;
402 buffer->error=-EOVERFLOW;
404 spin_unlock(&dmxdevfilter->dev->lock);
405 wake_up(&buffer->queue);
406 return 0;
410 /* stop feed but only mark the specified filter as stopped (state set) */
412 static int dvb_dmxdev_feed_stop(struct dmxdev_filter *dmxdevfilter)
414 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
416 switch (dmxdevfilter->type) {
417 case DMXDEV_TYPE_SEC:
418 del_timer(&dmxdevfilter->timer);
419 dmxdevfilter->feed.sec->stop_filtering(dmxdevfilter->feed.sec);
420 break;
421 case DMXDEV_TYPE_PES:
422 dmxdevfilter->feed.ts->stop_filtering(dmxdevfilter->feed.ts);
423 break;
424 default:
425 return -EINVAL;
427 return 0;
431 /* start feed associated with the specified filter */
433 static int dvb_dmxdev_feed_start(struct dmxdev_filter *filter)
435 dvb_dmxdev_filter_state_set (filter, DMXDEV_STATE_GO);
437 switch (filter->type) {
438 case DMXDEV_TYPE_SEC:
439 return filter->feed.sec->start_filtering(filter->feed.sec);
440 break;
441 case DMXDEV_TYPE_PES:
442 return filter->feed.ts->start_filtering(filter->feed.ts);
443 break;
444 default:
445 return -EINVAL;
448 return 0;
452 /* restart section feed if it has filters left associated with it,
453 otherwise release the feed */
455 static int dvb_dmxdev_feed_restart(struct dmxdev_filter *filter)
457 int i;
458 struct dmxdev *dmxdev = filter->dev;
459 u16 pid = filter->params.sec.pid;
461 for (i=0; i<dmxdev->filternum; i++)
462 if (dmxdev->filter[i].state>=DMXDEV_STATE_GO &&
463 dmxdev->filter[i].type==DMXDEV_TYPE_SEC &&
464 dmxdev->filter[i].pid==pid) {
465 dvb_dmxdev_feed_start(&dmxdev->filter[i]);
466 return 0;
469 filter->dev->demux->release_section_feed(dmxdev->demux, filter->feed.sec);
471 return 0;
474 static int dvb_dmxdev_filter_stop(struct dmxdev_filter *dmxdevfilter)
476 if (dmxdevfilter->state<DMXDEV_STATE_GO)
477 return 0;
479 switch (dmxdevfilter->type) {
480 case DMXDEV_TYPE_SEC:
481 if (!dmxdevfilter->feed.sec)
482 break;
483 dvb_dmxdev_feed_stop(dmxdevfilter);
484 if (dmxdevfilter->filter.sec)
485 dmxdevfilter->feed.sec->
486 release_filter(dmxdevfilter->feed.sec,
487 dmxdevfilter->filter.sec);
488 dvb_dmxdev_feed_restart(dmxdevfilter);
489 dmxdevfilter->feed.sec=NULL;
490 break;
491 case DMXDEV_TYPE_PES:
492 if (!dmxdevfilter->feed.ts)
493 break;
494 dvb_dmxdev_feed_stop(dmxdevfilter);
495 dmxdevfilter->dev->demux->
496 release_ts_feed(dmxdevfilter->dev->demux,
497 dmxdevfilter->feed.ts);
498 dmxdevfilter->feed.ts=NULL;
499 break;
500 default:
501 if (dmxdevfilter->state==DMXDEV_STATE_ALLOCATED)
502 return 0;
503 return -EINVAL;
505 dmxdevfilter->buffer.pwrite=dmxdevfilter->buffer.pread=0;
506 return 0;
509 static inline int dvb_dmxdev_filter_reset(struct dmxdev_filter *dmxdevfilter)
511 if (dmxdevfilter->state<DMXDEV_STATE_SET)
512 return 0;
514 dmxdevfilter->type=DMXDEV_TYPE_NONE;
515 dmxdevfilter->pid=0xffff;
516 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
517 return 0;
520 static int dvb_dmxdev_filter_start(struct dmxdev_filter *filter)
522 struct dmxdev *dmxdev = filter->dev;
523 void *mem;
524 int ret, i;
526 if (filter->state < DMXDEV_STATE_SET)
527 return -EINVAL;
529 if (filter->state >= DMXDEV_STATE_GO)
530 dvb_dmxdev_filter_stop(filter);
532 if (!(mem = filter->buffer.data)) {
533 mem = vmalloc(filter->buffer.size);
534 spin_lock_irq(&filter->dev->lock);
535 filter->buffer.data=mem;
536 spin_unlock_irq(&filter->dev->lock);
537 if (!filter->buffer.data)
538 return -ENOMEM;
541 filter->buffer.pwrite = filter->buffer.pread = 0;
543 switch (filter->type) {
544 case DMXDEV_TYPE_SEC:
546 struct dmx_sct_filter_params *para=&filter->params.sec;
547 struct dmx_section_filter **secfilter=&filter->filter.sec;
548 struct dmx_section_feed **secfeed=&filter->feed.sec;
550 *secfilter=NULL;
551 *secfeed=NULL;
553 /* find active filter/feed with same PID */
554 for (i=0; i<dmxdev->filternum; i++) {
555 if (dmxdev->filter[i].state >= DMXDEV_STATE_GO &&
556 dmxdev->filter[i].pid == para->pid &&
557 dmxdev->filter[i].type == DMXDEV_TYPE_SEC) {
558 *secfeed = dmxdev->filter[i].feed.sec;
559 break;
563 /* if no feed found, try to allocate new one */
564 if (!*secfeed) {
565 ret=dmxdev->demux->allocate_section_feed(dmxdev->demux,
566 secfeed,
567 dvb_dmxdev_section_callback);
568 if (ret<0) {
569 printk ("DVB (%s): could not alloc feed\n",
570 __FUNCTION__);
571 return ret;
574 ret=(*secfeed)->set(*secfeed, para->pid, 32768,
575 (para->flags & DMX_CHECK_CRC) ? 1 : 0);
577 if (ret<0) {
578 printk ("DVB (%s): could not set feed\n",
579 __FUNCTION__);
580 dvb_dmxdev_feed_restart(filter);
581 return ret;
583 } else {
584 dvb_dmxdev_feed_stop(filter);
587 ret=(*secfeed)->allocate_filter(*secfeed, secfilter);
589 if (ret < 0) {
590 dvb_dmxdev_feed_restart(filter);
591 filter->feed.sec->start_filtering(*secfeed);
592 dprintk ("could not get filter\n");
593 return ret;
596 (*secfilter)->priv = filter;
598 memcpy(&((*secfilter)->filter_value[3]),
599 &(para->filter.filter[1]), DMX_FILTER_SIZE-1);
600 memcpy(&(*secfilter)->filter_mask[3],
601 &para->filter.mask[1], DMX_FILTER_SIZE-1);
602 memcpy(&(*secfilter)->filter_mode[3],
603 &para->filter.mode[1], DMX_FILTER_SIZE-1);
605 (*secfilter)->filter_value[0]=para->filter.filter[0];
606 (*secfilter)->filter_mask[0]=para->filter.mask[0];
607 (*secfilter)->filter_mode[0]=para->filter.mode[0];
608 (*secfilter)->filter_mask[1]=0;
609 (*secfilter)->filter_mask[2]=0;
611 filter->todo = 0;
613 ret = filter->feed.sec->start_filtering (filter->feed.sec);
615 if (ret < 0)
616 return ret;
618 dvb_dmxdev_filter_timer(filter);
619 break;
622 case DMXDEV_TYPE_PES:
624 struct timespec timeout = { 0 };
625 struct dmx_pes_filter_params *para = &filter->params.pes;
626 dmx_output_t otype;
627 int ret;
628 int ts_type;
629 enum dmx_ts_pes ts_pes;
630 struct dmx_ts_feed **tsfeed = &filter->feed.ts;
632 filter->feed.ts = NULL;
633 otype=para->output;
635 ts_pes=(enum dmx_ts_pes) para->pes_type;
637 if (ts_pes<DMX_PES_OTHER)
638 ts_type=TS_DECODER;
639 else
640 ts_type=0;
642 if (otype == DMX_OUT_TS_TAP)
643 ts_type |= TS_PACKET;
645 if (otype == DMX_OUT_TAP)
646 ts_type |= TS_PAYLOAD_ONLY|TS_PACKET;
648 ret=dmxdev->demux->allocate_ts_feed(dmxdev->demux,
649 tsfeed,
650 dvb_dmxdev_ts_callback);
651 if (ret<0)
652 return ret;
654 (*tsfeed)->priv = (void *) filter;
656 ret = (*tsfeed)->set(*tsfeed, para->pid, ts_type, ts_pes,
657 32768, timeout);
659 if (ret < 0) {
660 dmxdev->demux->release_ts_feed(dmxdev->demux, *tsfeed);
661 return ret;
664 ret = filter->feed.ts->start_filtering(filter->feed.ts);
666 if (ret < 0) {
667 dmxdev->demux->release_ts_feed(dmxdev->demux, *tsfeed);
668 return ret;
671 break;
673 default:
674 return -EINVAL;
677 dvb_dmxdev_filter_state_set(filter, DMXDEV_STATE_GO);
678 return 0;
681 static int dvb_demux_open(struct inode *inode, struct file *file)
683 struct dvb_device *dvbdev = file->private_data;
684 struct dmxdev *dmxdev = dvbdev->priv;
685 int i;
686 struct dmxdev_filter *dmxdevfilter;
688 if (!dmxdev->filter)
689 return -EINVAL;
691 if (down_interruptible(&dmxdev->mutex))
692 return -ERESTARTSYS;
694 for (i=0; i<dmxdev->filternum; i++)
695 if (dmxdev->filter[i].state==DMXDEV_STATE_FREE)
696 break;
698 if (i==dmxdev->filternum) {
699 up(&dmxdev->mutex);
700 return -EMFILE;
703 dmxdevfilter=&dmxdev->filter[i];
704 sema_init(&dmxdevfilter->mutex, 1);
705 dmxdevfilter->dvbdev=dmxdev->dvbdev;
706 file->private_data=dmxdevfilter;
708 dvb_dmxdev_buffer_init(&dmxdevfilter->buffer);
709 dmxdevfilter->type=DMXDEV_TYPE_NONE;
710 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_ALLOCATED);
711 dmxdevfilter->feed.ts=NULL;
712 init_timer(&dmxdevfilter->timer);
714 up(&dmxdev->mutex);
715 return 0;
719 static int dvb_dmxdev_filter_free(struct dmxdev *dmxdev, struct dmxdev_filter *dmxdevfilter)
721 if (down_interruptible(&dmxdev->mutex))
722 return -ERESTARTSYS;
724 if (down_interruptible(&dmxdevfilter->mutex)) {
725 up(&dmxdev->mutex);
726 return -ERESTARTSYS;
729 dvb_dmxdev_filter_stop(dmxdevfilter);
730 dvb_dmxdev_filter_reset(dmxdevfilter);
732 if (dmxdevfilter->buffer.data) {
733 void *mem=dmxdevfilter->buffer.data;
735 spin_lock_irq(&dmxdev->lock);
736 dmxdevfilter->buffer.data=NULL;
737 spin_unlock_irq(&dmxdev->lock);
738 vfree(mem);
741 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_FREE);
742 wake_up(&dmxdevfilter->buffer.queue);
743 up(&dmxdevfilter->mutex);
744 up(&dmxdev->mutex);
745 return 0;
748 static inline void invert_mode(dmx_filter_t *filter)
750 int i;
752 for (i=0; i<DMX_FILTER_SIZE; i++)
753 filter->mode[i]^=0xff;
757 static int dvb_dmxdev_filter_set(struct dmxdev *dmxdev,
758 struct dmxdev_filter *dmxdevfilter,
759 struct dmx_sct_filter_params *params)
761 dprintk ("function : %s\n", __FUNCTION__);
763 dvb_dmxdev_filter_stop(dmxdevfilter);
765 dmxdevfilter->type=DMXDEV_TYPE_SEC;
766 dmxdevfilter->pid=params->pid;
767 memcpy(&dmxdevfilter->params.sec,
768 params, sizeof(struct dmx_sct_filter_params));
769 invert_mode(&dmxdevfilter->params.sec.filter);
770 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
772 if (params->flags&DMX_IMMEDIATE_START)
773 return dvb_dmxdev_filter_start(dmxdevfilter);
775 return 0;
778 static int dvb_dmxdev_pes_filter_set(struct dmxdev *dmxdev,
779 struct dmxdev_filter *dmxdevfilter,
780 struct dmx_pes_filter_params *params)
782 dvb_dmxdev_filter_stop(dmxdevfilter);
784 if (params->pes_type>DMX_PES_OTHER || params->pes_type<0)
785 return -EINVAL;
787 dmxdevfilter->type=DMXDEV_TYPE_PES;
788 dmxdevfilter->pid=params->pid;
789 memcpy(&dmxdevfilter->params, params, sizeof(struct dmx_pes_filter_params));
791 dvb_dmxdev_filter_state_set(dmxdevfilter, DMXDEV_STATE_SET);
793 if (params->flags&DMX_IMMEDIATE_START)
794 return dvb_dmxdev_filter_start(dmxdevfilter);
796 return 0;
799 static ssize_t dvb_dmxdev_read_sec(struct dmxdev_filter *dfil,
800 struct file *file, char __user *buf, size_t count, loff_t *ppos)
802 int result, hcount;
803 int done=0;
805 if (dfil->todo<=0) {
806 hcount=3+dfil->todo;
807 if (hcount>count)
808 hcount=count;
809 result=dvb_dmxdev_buffer_read(&dfil->buffer, file->f_flags&O_NONBLOCK,
810 buf, hcount, ppos);
811 if (result<0) {
812 dfil->todo=0;
813 return result;
815 if (copy_from_user(dfil->secheader-dfil->todo, buf, result))
816 return -EFAULT;
817 buf+=result;
818 done=result;
819 count-=result;
820 dfil->todo-=result;
821 if (dfil->todo>-3)
822 return done;
823 dfil->todo=((dfil->secheader[1]<<8)|dfil->secheader[2])&0xfff;
824 if (!count)
825 return done;
827 if (count>dfil->todo)
828 count=dfil->todo;
829 result=dvb_dmxdev_buffer_read(&dfil->buffer, file->f_flags&O_NONBLOCK,
830 buf, count, ppos);
831 if (result<0)
832 return result;
833 dfil->todo-=result;
834 return (result+done);
838 static ssize_t
839 dvb_demux_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
841 struct dmxdev_filter *dmxdevfilter= file->private_data;
842 int ret=0;
844 if (down_interruptible(&dmxdevfilter->mutex))
845 return -ERESTARTSYS;
847 if (dmxdevfilter->type==DMXDEV_TYPE_SEC)
848 ret=dvb_dmxdev_read_sec(dmxdevfilter, file, buf, count, ppos);
849 else
850 ret=dvb_dmxdev_buffer_read(&dmxdevfilter->buffer,
851 file->f_flags&O_NONBLOCK,
852 buf, count, ppos);
854 up(&dmxdevfilter->mutex);
855 return ret;
859 static int dvb_demux_do_ioctl(struct inode *inode, struct file *file,
860 unsigned int cmd, void *parg)
862 struct dmxdev_filter *dmxdevfilter = file->private_data;
863 struct dmxdev *dmxdev=dmxdevfilter->dev;
864 unsigned long arg=(unsigned long) parg;
865 int ret=0;
867 if (down_interruptible (&dmxdev->mutex))
868 return -ERESTARTSYS;
870 switch (cmd) {
871 case DMX_START:
872 if (down_interruptible(&dmxdevfilter->mutex)) {
873 up(&dmxdev->mutex);
874 return -ERESTARTSYS;
876 if (dmxdevfilter->state<DMXDEV_STATE_SET)
877 ret = -EINVAL;
878 else
879 ret = dvb_dmxdev_filter_start(dmxdevfilter);
880 up(&dmxdevfilter->mutex);
881 break;
883 case DMX_STOP:
884 if (down_interruptible(&dmxdevfilter->mutex)) {
885 up(&dmxdev->mutex);
886 return -ERESTARTSYS;
888 ret=dvb_dmxdev_filter_stop(dmxdevfilter);
889 up(&dmxdevfilter->mutex);
890 break;
892 case DMX_SET_FILTER:
893 if (down_interruptible(&dmxdevfilter->mutex)) {
894 up(&dmxdev->mutex);
895 return -ERESTARTSYS;
897 ret = dvb_dmxdev_filter_set(dmxdev, dmxdevfilter,
898 (struct dmx_sct_filter_params *)parg);
899 up(&dmxdevfilter->mutex);
900 break;
902 case DMX_SET_PES_FILTER:
903 if (down_interruptible(&dmxdevfilter->mutex)) {
904 up(&dmxdev->mutex);
905 return -ERESTARTSYS;
907 ret=dvb_dmxdev_pes_filter_set(dmxdev, dmxdevfilter,
908 (struct dmx_pes_filter_params *)parg);
909 up(&dmxdevfilter->mutex);
910 break;
912 case DMX_SET_BUFFER_SIZE:
913 if (down_interruptible(&dmxdevfilter->mutex)) {
914 up(&dmxdev->mutex);
915 return -ERESTARTSYS;
917 ret=dvb_dmxdev_set_buffer_size(dmxdevfilter, arg);
918 up(&dmxdevfilter->mutex);
919 break;
921 case DMX_GET_EVENT:
922 break;
924 case DMX_GET_PES_PIDS:
925 if (!dmxdev->demux->get_pes_pids) {
926 ret=-EINVAL;
927 break;
929 dmxdev->demux->get_pes_pids(dmxdev->demux, (u16 *)parg);
930 break;
932 case DMX_GET_CAPS:
933 if (!dmxdev->demux->get_caps) {
934 ret = -EINVAL;
935 break;
937 ret = dmxdev->demux->get_caps(dmxdev->demux, parg);
938 break;
940 case DMX_SET_SOURCE:
941 if (!dmxdev->demux->set_source) {
942 ret = -EINVAL;
943 break;
945 ret = dmxdev->demux->set_source(dmxdev->demux, parg);
946 break;
948 case DMX_GET_STC:
949 if (!dmxdev->demux->get_stc) {
950 ret=-EINVAL;
951 break;
953 ret = dmxdev->demux->get_stc(dmxdev->demux,
954 ((struct dmx_stc *)parg)->num,
955 &((struct dmx_stc *)parg)->stc,
956 &((struct dmx_stc *)parg)->base);
957 break;
959 default:
960 ret=-EINVAL;
962 up(&dmxdev->mutex);
963 return ret;
966 static int dvb_demux_ioctl(struct inode *inode, struct file *file,
967 unsigned int cmd, unsigned long arg)
969 return dvb_usercopy(inode, file, cmd, arg, dvb_demux_do_ioctl);
973 static unsigned int dvb_demux_poll (struct file *file, poll_table *wait)
975 struct dmxdev_filter *dmxdevfilter = file->private_data;
976 unsigned int mask = 0;
978 if (!dmxdevfilter)
979 return -EINVAL;
981 poll_wait(file, &dmxdevfilter->buffer.queue, wait);
983 if (dmxdevfilter->state != DMXDEV_STATE_GO &&
984 dmxdevfilter->state != DMXDEV_STATE_DONE &&
985 dmxdevfilter->state != DMXDEV_STATE_TIMEDOUT)
986 return 0;
988 if (dmxdevfilter->buffer.error)
989 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
991 if (dmxdevfilter->buffer.pread != dmxdevfilter->buffer.pwrite)
992 mask |= (POLLIN | POLLRDNORM | POLLPRI);
994 return mask;
998 static int dvb_demux_release(struct inode *inode, struct file *file)
1000 struct dmxdev_filter *dmxdevfilter = file->private_data;
1001 struct dmxdev *dmxdev = dmxdevfilter->dev;
1003 return dvb_dmxdev_filter_free(dmxdev, dmxdevfilter);
1007 static struct file_operations dvb_demux_fops = {
1008 .owner = THIS_MODULE,
1009 .read = dvb_demux_read,
1010 .ioctl = dvb_demux_ioctl,
1011 .open = dvb_demux_open,
1012 .release = dvb_demux_release,
1013 .poll = dvb_demux_poll,
1017 static struct dvb_device dvbdev_demux = {
1018 .priv = NULL,
1019 .users = 1,
1020 .writers = 1,
1021 .fops = &dvb_demux_fops
1025 static int dvb_dvr_do_ioctl(struct inode *inode, struct file *file,
1026 unsigned int cmd, void *parg)
1028 struct dvb_device *dvbdev = file->private_data;
1029 struct dmxdev *dmxdev = dvbdev->priv;
1031 int ret=0;
1033 if (down_interruptible (&dmxdev->mutex))
1034 return -ERESTARTSYS;
1036 switch (cmd) {
1037 case DMX_SET_BUFFER_SIZE:
1038 // FIXME: implement
1039 ret=0;
1040 break;
1042 default:
1043 ret=-EINVAL;
1045 up(&dmxdev->mutex);
1046 return ret;
1050 static int dvb_dvr_ioctl(struct inode *inode, struct file *file,
1051 unsigned int cmd, unsigned long arg)
1053 return dvb_usercopy(inode, file, cmd, arg, dvb_dvr_do_ioctl);
1057 static unsigned int dvb_dvr_poll (struct file *file, poll_table *wait)
1059 struct dvb_device *dvbdev = file->private_data;
1060 struct dmxdev *dmxdev = dvbdev->priv;
1061 unsigned int mask = 0;
1063 dprintk ("function : %s\n", __FUNCTION__);
1065 poll_wait(file, &dmxdev->dvr_buffer.queue, wait);
1067 if ((file->f_flags&O_ACCMODE) == O_RDONLY) {
1068 if (dmxdev->dvr_buffer.error)
1069 mask |= (POLLIN | POLLRDNORM | POLLPRI | POLLERR);
1071 if (dmxdev->dvr_buffer.pread!=dmxdev->dvr_buffer.pwrite)
1072 mask |= (POLLIN | POLLRDNORM | POLLPRI);
1073 } else
1074 mask |= (POLLOUT | POLLWRNORM | POLLPRI);
1076 return mask;
1080 static struct file_operations dvb_dvr_fops = {
1081 .owner = THIS_MODULE,
1082 .read = dvb_dvr_read,
1083 .write = dvb_dvr_write,
1084 .ioctl = dvb_dvr_ioctl,
1085 .open = dvb_dvr_open,
1086 .release = dvb_dvr_release,
1087 .poll = dvb_dvr_poll,
1090 static struct dvb_device dvbdev_dvr = {
1091 .priv = NULL,
1092 .users = 1,
1093 .writers = 1,
1094 .fops = &dvb_dvr_fops
1098 dvb_dmxdev_init(struct dmxdev *dmxdev, struct dvb_adapter *dvb_adapter)
1100 int i;
1102 if (dmxdev->demux->open(dmxdev->demux) < 0)
1103 return -EUSERS;
1105 dmxdev->filter = vmalloc(dmxdev->filternum*sizeof(struct dmxdev_filter));
1106 if (!dmxdev->filter)
1107 return -ENOMEM;
1109 dmxdev->dvr = vmalloc(dmxdev->filternum*sizeof(struct dmxdev_dvr));
1110 if (!dmxdev->dvr) {
1111 vfree(dmxdev->filter);
1112 dmxdev->filter = NULL;
1113 return -ENOMEM;
1116 sema_init(&dmxdev->mutex, 1);
1117 spin_lock_init(&dmxdev->lock);
1118 for (i=0; i<dmxdev->filternum; i++) {
1119 dmxdev->filter[i].dev=dmxdev;
1120 dmxdev->filter[i].buffer.data=NULL;
1121 dvb_dmxdev_filter_state_set(&dmxdev->filter[i], DMXDEV_STATE_FREE);
1122 dmxdev->dvr[i].dev=dmxdev;
1123 dmxdev->dvr[i].buffer.data=NULL;
1124 dvb_dmxdev_dvr_state_set(&dmxdev->dvr[i], DMXDEV_STATE_FREE);
1127 dvb_register_device(dvb_adapter, &dmxdev->dvbdev, &dvbdev_demux, dmxdev, DVB_DEVICE_DEMUX);
1128 dvb_register_device(dvb_adapter, &dmxdev->dvr_dvbdev, &dvbdev_dvr, dmxdev, DVB_DEVICE_DVR);
1130 dvb_dmxdev_buffer_init(&dmxdev->dvr_buffer);
1132 return 0;
1134 EXPORT_SYMBOL(dvb_dmxdev_init);
1136 void
1137 dvb_dmxdev_release(struct dmxdev *dmxdev)
1139 dvb_unregister_device(dmxdev->dvbdev);
1140 dvb_unregister_device(dmxdev->dvr_dvbdev);
1142 vfree(dmxdev->filter);
1143 dmxdev->filter=NULL;
1144 vfree(dmxdev->dvr);
1145 dmxdev->dvr=NULL;
1146 dmxdev->demux->close(dmxdev->demux);
1148 EXPORT_SYMBOL(dvb_dmxdev_release);