blob: e617925ba31e495c79b70e552eb5cc040871208f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/device.h>
20#include <linux/fs.h>
21#include <linux/kthread.h>
22#include <linux/file.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080023#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <media/video-buf.h>
26#include <media/video-buf-dvb.h>
27
28/* ------------------------------------------------------------------ */
29
30MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
31MODULE_LICENSE("GPL");
32
33static unsigned int debug = 0;
34module_param(debug, int, 0644);
35MODULE_PARM_DESC(debug,"enable debug messages");
36
37#define dprintk(fmt, arg...) if (debug) \
38 printk(KERN_DEBUG "%s/dvb: " fmt, dvb->name , ## arg)
39
40/* ------------------------------------------------------------------ */
41
42static int videobuf_dvb_thread(void *data)
43{
44 struct videobuf_dvb *dvb = data;
45 struct videobuf_buffer *buf;
46 unsigned long flags;
47 int err;
48
49 dprintk("dvb thread started\n");
Rafael J. Wysocki83144182007-07-17 04:03:35 -070050 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 videobuf_read_start(&dvb->dvbq);
52
53 for (;;) {
54 /* fetch next buffer */
55 buf = list_entry(dvb->dvbq.stream.next,
56 struct videobuf_buffer, stream);
57 list_del(&buf->stream);
58 err = videobuf_waiton(buf,0,1);
59 BUG_ON(0 != err);
60
61 /* no more feeds left or stop_feed() asked us to quit */
62 if (0 == dvb->nfeeds)
63 break;
64 if (kthread_should_stop())
65 break;
Christoph Lameter3e1d1d22005-06-24 23:13:50 -070066 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /* feed buffer data to demux */
69 if (buf->state == STATE_DONE)
70 dvb_dmx_swfilter(&dvb->demux, buf->dma.vmalloc,
71 buf->size);
72
73 /* requeue buffer */
74 list_add_tail(&buf->stream,&dvb->dvbq.stream);
75 spin_lock_irqsave(dvb->dvbq.irqlock,flags);
76 dvb->dvbq.ops->buf_queue(&dvb->dvbq,buf);
77 spin_unlock_irqrestore(dvb->dvbq.irqlock,flags);
78 }
79
80 videobuf_read_stop(&dvb->dvbq);
81 dprintk("dvb thread stopped\n");
82
83 /* Hmm, linux becomes *very* unhappy without this ... */
84 while (!kthread_should_stop()) {
85 set_current_state(TASK_INTERRUPTIBLE);
86 schedule();
87 }
88 return 0;
89}
90
91static int videobuf_dvb_start_feed(struct dvb_demux_feed *feed)
92{
93 struct dvb_demux *demux = feed->demux;
94 struct videobuf_dvb *dvb = demux->priv;
95 int rc;
96
97 if (!demux->dmx.frontend)
98 return -EINVAL;
99
Ingo Molnar3593cab2006-02-07 06:49:14 -0200100 mutex_lock(&dvb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dvb->nfeeds++;
102 rc = dvb->nfeeds;
103
104 if (NULL != dvb->thread)
105 goto out;
106 dvb->thread = kthread_run(videobuf_dvb_thread,
107 dvb, "%s dvb", dvb->name);
108 if (IS_ERR(dvb->thread)) {
109 rc = PTR_ERR(dvb->thread);
110 dvb->thread = NULL;
111 }
112
113out:
Ingo Molnar3593cab2006-02-07 06:49:14 -0200114 mutex_unlock(&dvb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return rc;
116}
117
118static int videobuf_dvb_stop_feed(struct dvb_demux_feed *feed)
119{
120 struct dvb_demux *demux = feed->demux;
121 struct videobuf_dvb *dvb = demux->priv;
122 int err = 0;
123
Ingo Molnar3593cab2006-02-07 06:49:14 -0200124 mutex_lock(&dvb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 dvb->nfeeds--;
126 if (0 == dvb->nfeeds && NULL != dvb->thread) {
127 // FIXME: cx8802_cancel_buffers(dev);
128 err = kthread_stop(dvb->thread);
129 dvb->thread = NULL;
130 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200131 mutex_unlock(&dvb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return err;
133}
134
135/* ------------------------------------------------------------------ */
136
137int videobuf_dvb_register(struct videobuf_dvb *dvb,
138 struct module *module,
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300139 void *adapter_priv,
140 struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 int result;
143
Ingo Molnar3593cab2006-02-07 06:49:14 -0200144 mutex_init(&dvb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* register adapter */
Andrew de Quinceyd09dbf92006-04-10 09:27:37 -0300147 result = dvb_register_adapter(&dvb->adapter, dvb->name, module, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (result < 0) {
149 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
150 dvb->name, result);
151 goto fail_adapter;
152 }
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700153 dvb->adapter.priv = adapter_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 /* register frontend */
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700156 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (result < 0) {
158 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
159 dvb->name, result);
160 goto fail_frontend;
161 }
162
163 /* register demux stuff */
164 dvb->demux.dmx.capabilities =
165 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
166 DMX_MEMORY_BASED_FILTERING;
167 dvb->demux.priv = dvb;
168 dvb->demux.filternum = 256;
169 dvb->demux.feednum = 256;
170 dvb->demux.start_feed = videobuf_dvb_start_feed;
171 dvb->demux.stop_feed = videobuf_dvb_stop_feed;
172 result = dvb_dmx_init(&dvb->demux);
173 if (result < 0) {
174 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
175 dvb->name, result);
176 goto fail_dmx;
177 }
178
179 dvb->dmxdev.filternum = 256;
180 dvb->dmxdev.demux = &dvb->demux.dmx;
181 dvb->dmxdev.capabilities = 0;
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700182 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (result < 0) {
184 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
185 dvb->name, result);
186 goto fail_dmxdev;
187 }
188
189 dvb->fe_hw.source = DMX_FRONTEND_0;
190 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
191 if (result < 0) {
192 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
193 dvb->name, result);
194 goto fail_fe_hw;
195 }
196
197 dvb->fe_mem.source = DMX_MEMORY_FE;
198 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
199 if (result < 0) {
200 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
201 dvb->name, result);
202 goto fail_fe_mem;
203 }
204
205 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
206 if (result < 0) {
207 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
208 dvb->name, result);
209 goto fail_fe_conn;
210 }
211
212 /* register network adapter */
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700213 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return 0;
215
216fail_fe_conn:
217 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
218fail_fe_mem:
219 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
220fail_fe_hw:
221 dvb_dmxdev_release(&dvb->dmxdev);
222fail_dmxdev:
223 dvb_dmx_release(&dvb->demux);
224fail_dmx:
225 dvb_unregister_frontend(dvb->frontend);
226fail_frontend:
Andrew de Quinceyf52a8382006-08-08 09:10:09 -0300227 dvb_frontend_detach(dvb->frontend);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700228 dvb_unregister_adapter(&dvb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229fail_adapter:
230 return result;
231}
232
233void videobuf_dvb_unregister(struct videobuf_dvb *dvb)
234{
235 dvb_net_release(&dvb->net);
236 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
237 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
238 dvb_dmxdev_release(&dvb->dmxdev);
239 dvb_dmx_release(&dvb->demux);
240 dvb_unregister_frontend(dvb->frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -0300241 dvb_frontend_detach(dvb->frontend);
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -0700242 dvb_unregister_adapter(&dvb->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245EXPORT_SYMBOL(videobuf_dvb_register);
246EXPORT_SYMBOL(videobuf_dvb_unregister);
247
248/* ------------------------------------------------------------------ */
249/*
250 * Local variables:
251 * c-basic-offset: 8
252 * compile-command: "make DVB=1"
253 * End:
254 */
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -0800255