V4L/DVB (7719): pvrusb2: Implement input selection enforcement
[linux-2.6.git] / drivers / media / video / pvrusb2 / pvrusb2-dvb.c
blob2e64f98d124149404ba76d6f5ab3131e14910662
1 /*
2 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
4 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <linux/kthread.h>
22 #include <linux/freezer.h>
23 #include "dvbdev.h"
24 #include "pvrusb2-hdw-internal.h"
25 #include "pvrusb2-hdw.h"
26 #include "pvrusb2-io.h"
27 #include "pvrusb2-dvb.h"
29 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
33 int ret;
34 unsigned int count;
35 struct pvr2_buffer *bp;
36 struct pvr2_stream *stream;
38 printk(KERN_DEBUG "dvb thread started\n");
39 set_freezable();
41 stream = adap->channel.stream->stream;
43 for (;;) {
44 if (kthread_should_stop()) break;
46 /* Not sure about this... */
47 try_to_freeze();
49 bp = pvr2_stream_get_ready_buffer(stream);
50 if (bp != NULL) {
51 count = pvr2_buffer_get_count(bp);
52 if (count) {
53 dvb_dmx_swfilter(
54 &adap->demux,
55 adap->buffer_storage[
56 pvr2_buffer_get_id(bp)],
57 count);
58 } else {
59 ret = pvr2_buffer_get_status(bp);
60 if (ret < 0) break;
62 ret = pvr2_buffer_queue(bp);
63 if (ret < 0) break;
65 /* Since we know we did something to a buffer,
66 just go back and try again. No point in
67 blocking unless we really ran out of
68 buffers to process. */
69 continue;
73 /* Wait until more buffers become available or we're
74 told not to wait any longer. */
75 ret = wait_event_interruptible(
76 adap->buffer_wait_data,
77 (pvr2_stream_get_ready_count(stream) > 0) ||
78 kthread_should_stop());
79 if (ret < 0) break;
82 /* If we get here and ret is < 0, then an error has occurred.
83 Probably would be a good idea to communicate that to DVB core... */
85 printk(KERN_DEBUG "dvb thread stopped\n");
87 return 0;
90 static int pvr2_dvb_feed_thread(void *data)
92 int stat = pvr2_dvb_feed_func(data);
93 /* from videobuf-dvb.c: */
94 while (!kthread_should_stop()) {
95 set_current_state(TASK_INTERRUPTIBLE);
96 schedule();
98 return stat;
101 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
103 wake_up(&adap->buffer_wait_data);
106 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
108 unsigned int idx;
109 struct pvr2_stream *stream;
111 if (adap->thread) {
112 kthread_stop(adap->thread);
113 adap->thread = NULL;
116 if (adap->channel.stream) {
117 stream = adap->channel.stream->stream;
118 } else {
119 stream = NULL;
121 if (stream) {
122 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
123 pvr2_stream_set_callback(stream, NULL, NULL);
124 pvr2_stream_kill(stream);
125 pvr2_stream_set_buffer_count(stream, 0);
126 pvr2_channel_claim_stream(&adap->channel, NULL);
129 if (adap->stream_run) {
130 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
131 if (!(adap->buffer_storage[idx])) continue;
132 kfree(adap->buffer_storage[idx]);
133 adap->buffer_storage[idx] = 0;
135 adap->stream_run = 0;
139 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
141 struct pvr2_context *pvr = adap->channel.mc_head;
142 unsigned int idx;
143 int ret;
144 struct pvr2_buffer *bp;
145 struct pvr2_stream *stream = 0;
147 if (adap->stream_run) return -EIO;
149 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
150 /* somebody else already has the stream */
151 if (ret < 0) return ret;
153 stream = adap->channel.stream->stream;
155 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
156 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
157 GFP_KERNEL);
158 if (!(adap->buffer_storage[idx])) return -ENOMEM;
161 pvr2_stream_set_callback(pvr->video_stream.stream,
162 (pvr2_stream_callback) pvr2_dvb_notify, adap);
164 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
165 if (ret < 0) return ret;
167 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
168 bp = pvr2_stream_get_buffer(stream, idx);
169 pvr2_buffer_set_buffer(bp,
170 adap->buffer_storage[idx],
171 PVR2_DVB_BUFFER_SIZE);
174 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
175 if (ret < 0) return ret;
177 while ((bp = pvr2_stream_get_idle_buffer(stream)) != 0) {
178 ret = pvr2_buffer_queue(bp);
179 if (ret < 0) return ret;
182 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
184 if (IS_ERR(adap->thread)) {
185 ret = PTR_ERR(adap->thread);
186 adap->thread = NULL;
187 return ret;
190 adap->stream_run = !0;
192 return 0;
195 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
197 int ret = pvr2_dvb_stream_do_start(adap);
198 if (ret < 0) pvr2_dvb_stream_end(adap);
199 return ret;
202 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
204 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
205 int ret = 0;
207 if (adap == NULL) return -ENODEV;
209 mutex_lock(&adap->lock);
210 do {
211 if (onoff) {
212 if (!adap->feedcount) {
213 printk(KERN_DEBUG "start feeding\n");
214 ret = pvr2_dvb_stream_start(adap);
215 if (ret < 0) break;
217 (adap->feedcount)++;
218 } else if (adap->feedcount > 0) {
219 (adap->feedcount)--;
220 if (!adap->feedcount) {
221 printk(KERN_DEBUG "stop feeding\n");
222 pvr2_dvb_stream_end(adap);
225 } while (0);
226 mutex_unlock(&adap->lock);
228 return ret;
231 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
233 printk(KERN_DEBUG "start pid: 0x%04x, feedtype: %d\n",
234 dvbdmxfeed->pid, dvbdmxfeed->type);
235 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
238 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
240 printk(KERN_DEBUG "stop pid: 0x%04x, feedtype: %d\n",
241 dvbdmxfeed->pid, dvbdmxfeed->type);
242 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
245 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
247 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
248 return pvr2_channel_limit_inputs(
249 &adap->channel,
250 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
253 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
255 int ret;
257 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
258 THIS_MODULE/*&hdw->usb_dev->owner*/,
259 &adap->channel.hdw->usb_dev->dev,
260 adapter_nr);
261 if (ret < 0) {
262 err("dvb_register_adapter failed: error %d", ret);
263 goto err;
265 adap->dvb_adap.priv = adap;
267 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
268 DMX_SECTION_FILTERING |
269 DMX_MEMORY_BASED_FILTERING;
270 adap->demux.priv = adap;
271 adap->demux.filternum = 256;
272 adap->demux.feednum = 256;
273 adap->demux.start_feed = pvr2_dvb_start_feed;
274 adap->demux.stop_feed = pvr2_dvb_stop_feed;
275 adap->demux.write_to_decoder = NULL;
277 ret = dvb_dmx_init(&adap->demux);
278 if (ret < 0) {
279 err("dvb_dmx_init failed: error %d", ret);
280 goto err_dmx;
283 adap->dmxdev.filternum = adap->demux.filternum;
284 adap->dmxdev.demux = &adap->demux.dmx;
285 adap->dmxdev.capabilities = 0;
287 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
288 if (ret < 0) {
289 err("dvb_dmxdev_init failed: error %d", ret);
290 goto err_dmx_dev;
293 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
295 return 0;
297 err_dmx_dev:
298 dvb_dmx_release(&adap->demux);
299 err_dmx:
300 dvb_unregister_adapter(&adap->dvb_adap);
301 err:
302 return ret;
305 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
307 printk(KERN_DEBUG "unregistering DVB devices\n");
308 dvb_net_release(&adap->dvb_net);
309 adap->demux.dmx.close(&adap->demux.dmx);
310 dvb_dmxdev_release(&adap->dmxdev);
311 dvb_dmx_release(&adap->demux);
312 dvb_unregister_adapter(&adap->dvb_adap);
313 return 0;
316 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
318 struct pvr2_hdw *hdw = adap->channel.hdw;
319 struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
320 int ret = 0;
322 if (dvb_props == NULL) {
323 err("fe_props not defined!");
324 return -EINVAL;
327 ret = pvr2_channel_limit_inputs(
328 &adap->channel,
329 (1 << PVR2_CVAL_INPUT_DTV));
330 if (ret) {
331 err("failed to grab control of dtv input (code=%d)",
332 ret);
333 return ret;
336 if (dvb_props->frontend_attach == NULL) {
337 err("frontend_attach not defined!");
338 ret = -EINVAL;
339 goto done;
342 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
344 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
345 err("frontend registration failed!");
346 dvb_frontend_detach(adap->fe);
347 adap->fe = NULL;
348 ret = -ENODEV;
349 goto done;
352 if (dvb_props->tuner_attach)
353 dvb_props->tuner_attach(adap);
355 if (adap->fe->ops.analog_ops.standby)
356 adap->fe->ops.analog_ops.standby(adap->fe);
358 /* Ensure all frontends negotiate bus access */
359 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
361 } else {
362 err("no frontend was attached!");
363 ret = -ENODEV;
364 return ret;
367 done:
368 pvr2_channel_limit_inputs(&adap->channel, 0);
369 return ret;
372 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
374 if (adap->fe != NULL) {
375 dvb_unregister_frontend(adap->fe);
376 dvb_frontend_detach(adap->fe);
378 return 0;
381 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
383 pvr2_dvb_stream_end(adap);
384 pvr2_dvb_frontend_exit(adap);
385 pvr2_dvb_adapter_exit(adap);
386 pvr2_channel_done(&adap->channel);
387 kfree(adap);
390 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
392 struct pvr2_dvb_adapter *adap;
393 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
394 if (!adap->channel.mc_head->disconnect_flag) return;
395 pvr2_dvb_destroy(adap);
398 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
400 int ret = 0;
401 struct pvr2_dvb_adapter *adap;
402 if (!pvr->hdw->hdw_desc->dvb_props) {
403 /* Device lacks a digital interface so don't set up
404 the DVB side of the driver either. For now. */
405 return NULL;
407 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
408 if (!adap) return adap;
409 pvr2_channel_init(&adap->channel, pvr);
410 adap->channel.check_func = pvr2_dvb_internal_check;
411 init_waitqueue_head(&adap->buffer_wait_data);
412 mutex_init(&adap->lock);
413 ret = pvr2_dvb_adapter_init(adap);
414 if (ret < 0) goto fail1;
415 ret = pvr2_dvb_frontend_init(adap);
416 if (ret < 0) goto fail2;
417 return adap;
419 fail2:
420 pvr2_dvb_adapter_exit(adap);
421 fail1:
422 pvr2_channel_done(&adap->channel);
423 return NULL;