Merge branch 'master'
[linux-2.6] / drivers / media / video / video-buf-dvb.c
1 /*
2  *
3  * some helper function for simple DVB cards which simply DMA the
4  * complete transport stream and let the computer sort everything else
5  * (i.e. we are using the software demux, ...).  Also uses the
6  * video-buf to manage DMA buffers.
7  *
8  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SUSE Labs]
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  */
15
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/fs.h>
20 #include <linux/kthread.h>
21 #include <linux/file.h>
22 #include <linux/suspend.h>
23
24 #include <media/video-buf.h>
25 #include <media/video-buf-dvb.h>
26
27 /* ------------------------------------------------------------------ */
28
29 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
30 MODULE_LICENSE("GPL");
31
32 static unsigned int debug = 0;
33 module_param(debug, int, 0644);
34 MODULE_PARM_DESC(debug,"enable debug messages");
35
36 #define dprintk(fmt, arg...)    if (debug)                      \
37         printk(KERN_DEBUG "%s/dvb: " fmt, dvb->name , ## arg)
38
39 /* ------------------------------------------------------------------ */
40
41 static int videobuf_dvb_thread(void *data)
42 {
43         struct videobuf_dvb *dvb = data;
44         struct videobuf_buffer *buf;
45         unsigned long flags;
46         int err;
47
48         dprintk("dvb thread started\n");
49         videobuf_read_start(&dvb->dvbq);
50
51         for (;;) {
52                 /* fetch next buffer */
53                 buf = list_entry(dvb->dvbq.stream.next,
54                                  struct videobuf_buffer, stream);
55                 list_del(&buf->stream);
56                 err = videobuf_waiton(buf,0,1);
57                 BUG_ON(0 != err);
58
59                 /* no more feeds left or stop_feed() asked us to quit */
60                 if (0 == dvb->nfeeds)
61                         break;
62                 if (kthread_should_stop())
63                         break;
64                 try_to_freeze();
65
66                 /* feed buffer data to demux */
67                 if (buf->state == STATE_DONE)
68                         dvb_dmx_swfilter(&dvb->demux, buf->dma.vmalloc,
69                                          buf->size);
70
71                 /* requeue buffer */
72                 list_add_tail(&buf->stream,&dvb->dvbq.stream);
73                 spin_lock_irqsave(dvb->dvbq.irqlock,flags);
74                 dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
75                 spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
76         }
77
78         videobuf_read_stop(&dvb->dvbq);
79         dprintk("dvb thread stopped\n");
80
81         /* Hmm, linux becomes *very* unhappy without this ... */
82         while (!kthread_should_stop()) {
83                 set_current_state(TASK_INTERRUPTIBLE);
84                 schedule();
85         }
86         return 0;
87 }
88
89 static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
90 {
91         struct dvb_demux *demux  = feed->demux;
92         struct videobuf_dvb *dvb = demux->priv;
93         int rc;
94
95         if (!demux->dmx.frontend)
96                 return -EINVAL;
97
98         down(&dvb->lock);
99         dvb->nfeeds++;
100         rc = dvb->nfeeds;
101
102         if (NULL != dvb->thread)
103                 goto out;
104         dvb->thread = kthread_run(videobuf_dvb_thread,
105                                   dvb, "%s dvb", dvb->name);
106         if (IS_ERR(dvb->thread)) {
107                 rc = PTR_ERR(dvb->thread);
108                 dvb->thread = NULL;
109         }
110
111 out:
112         up(&dvb->lock);
113         return rc;
114 }
115
116 static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
117 {
118         struct dvb_demux *demux  = feed->demux;
119         struct videobuf_dvb *dvb = demux->priv;
120         int err = 0;
121
122         down(&dvb->lock);
123         dvb->nfeeds--;
124         if (0 == dvb->nfeeds  &&  NULL != dvb->thread) {
125                 // FIXME: cx8802_cancel_buffers(dev);
126                 err = kthread_stop(dvb->thread);
127                 dvb->thread = NULL;
128         }
129         up(&dvb->lock);
130         return err;
131 }
132
133 /* ------------------------------------------------------------------ */
134
135 int videobuf_dvb_register(struct videobuf_dvb *dvb,
136                           struct module *module,
137                           void *adapter_priv)
138 {
139         int result;
140
141         init_MUTEX(&dvb->lock);
142
143         /* register adapter */
144         result = dvb_register_adapter(&dvb->adapter, dvb->name, module);
145         if (result < 0) {
146                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
147                        dvb->name, result);
148                 goto fail_adapter;
149         }
150         dvb->adapter.priv = adapter_priv;
151
152         /* register frontend */
153         result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
154         if (result < 0) {
155                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
156                        dvb->name, result);
157                 goto fail_frontend;
158         }
159
160         /* register demux stuff */
161         dvb->demux.dmx.capabilities =
162                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
163                 DMX_MEMORY_BASED_FILTERING;
164         dvb->demux.priv       = dvb;
165         dvb->demux.filternum  = 256;
166         dvb->demux.feednum    = 256;
167         dvb->demux.start_feed = videobuf_dvb_start_feed;
168         dvb->demux.stop_feed  = videobuf_dvb_stop_feed;
169         result = dvb_dmx_init(&dvb->demux);
170         if (result < 0) {
171                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
172                        dvb->name, result);
173                 goto fail_dmx;
174         }
175
176         dvb->dmxdev.filternum    = 256;
177         dvb->dmxdev.demux        = &dvb->demux.dmx;
178         dvb->dmxdev.capabilities = 0;
179         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
180         if (result < 0) {
181                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
182                        dvb->name, result);
183                 goto fail_dmxdev;
184         }
185
186         dvb->fe_hw.source = DMX_FRONTEND_0;
187         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
188         if (result < 0) {
189                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
190                        dvb->name, result);
191                 goto fail_fe_hw;
192         }
193
194         dvb->fe_mem.source = DMX_MEMORY_FE;
195         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
196         if (result < 0) {
197                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
198                        dvb->name, result);
199                 goto fail_fe_mem;
200         }
201
202         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
203         if (result < 0) {
204                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
205                        dvb->name, result);
206                 goto fail_fe_conn;
207         }
208
209         /* register network adapter */
210         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
211         return 0;
212
213 fail_fe_conn:
214         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
215 fail_fe_mem:
216         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
217 fail_fe_hw:
218         dvb_dmxdev_release(&dvb->dmxdev);
219 fail_dmxdev:
220         dvb_dmx_release(&dvb->demux);
221 fail_dmx:
222         dvb_unregister_frontend(dvb->frontend);
223 fail_frontend:
224         dvb_unregister_adapter(&dvb->adapter);
225 fail_adapter:
226         return result;
227 }
228
229 void videobuf_dvb_unregister(struct videobuf_dvb *dvb)
230 {
231         dvb_net_release(&dvb->net);
232         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
233         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
234         dvb_dmxdev_release(&dvb->dmxdev);
235         dvb_dmx_release(&dvb->demux);
236         dvb_unregister_frontend(dvb->frontend);
237         dvb_unregister_adapter(&dvb->adapter);
238 }
239
240 EXPORT_SYMBOL(videobuf_dvb_register);
241 EXPORT_SYMBOL(videobuf_dvb_unregister);
242
243 /* ------------------------------------------------------------------ */
244 /*
245  * Local variables:
246  * c-basic-offset: 8
247  * compile-command: "make DVB=1"
248  * End:
249  */