OMAP3 SRF: Generic shared resource f/w
[linux-ginger.git] / drivers / media / video / pvrusb2 / pvrusb2-dvb.c
blobb7f5c49b1dbc19f35bc7ce59461d9996cef111a6
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 <linux/mm.h>
24 #include "dvbdev.h"
25 #include "pvrusb2-debug.h"
26 #include "pvrusb2-hdw-internal.h"
27 #include "pvrusb2-hdw.h"
28 #include "pvrusb2-io.h"
29 #include "pvrusb2-dvb.h"
31 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
35 int ret;
36 unsigned int count;
37 struct pvr2_buffer *bp;
38 struct pvr2_stream *stream;
40 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
41 set_freezable();
43 stream = adap->channel.stream->stream;
45 for (;;) {
46 if (kthread_should_stop()) break;
48 /* Not sure about this... */
49 try_to_freeze();
51 bp = pvr2_stream_get_ready_buffer(stream);
52 if (bp != NULL) {
53 count = pvr2_buffer_get_count(bp);
54 if (count) {
55 dvb_dmx_swfilter(
56 &adap->demux,
57 adap->buffer_storage[
58 pvr2_buffer_get_id(bp)],
59 count);
60 } else {
61 ret = pvr2_buffer_get_status(bp);
62 if (ret < 0) break;
64 ret = pvr2_buffer_queue(bp);
65 if (ret < 0) break;
67 /* Since we know we did something to a buffer,
68 just go back and try again. No point in
69 blocking unless we really ran out of
70 buffers to process. */
71 continue;
75 /* Wait until more buffers become available or we're
76 told not to wait any longer. */
77 ret = wait_event_interruptible(
78 adap->buffer_wait_data,
79 (pvr2_stream_get_ready_count(stream) > 0) ||
80 kthread_should_stop());
81 if (ret < 0) break;
84 /* If we get here and ret is < 0, then an error has occurred.
85 Probably would be a good idea to communicate that to DVB core... */
87 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
89 return 0;
92 static int pvr2_dvb_feed_thread(void *data)
94 int stat = pvr2_dvb_feed_func(data);
95 /* from videobuf-dvb.c: */
96 while (!kthread_should_stop()) {
97 set_current_state(TASK_INTERRUPTIBLE);
98 schedule();
100 return stat;
103 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
105 wake_up(&adap->buffer_wait_data);
108 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
110 unsigned int idx;
111 struct pvr2_stream *stream;
113 if (adap->thread) {
114 kthread_stop(adap->thread);
115 adap->thread = NULL;
118 if (adap->channel.stream) {
119 stream = adap->channel.stream->stream;
120 } else {
121 stream = NULL;
123 if (stream) {
124 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
125 pvr2_stream_set_callback(stream, NULL, NULL);
126 pvr2_stream_kill(stream);
127 pvr2_stream_set_buffer_count(stream, 0);
128 pvr2_channel_claim_stream(&adap->channel, NULL);
131 if (adap->stream_run) {
132 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
133 if (!(adap->buffer_storage[idx])) continue;
134 kfree(adap->buffer_storage[idx]);
135 adap->buffer_storage[idx] = NULL;
137 adap->stream_run = 0;
141 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
143 struct pvr2_context *pvr = adap->channel.mc_head;
144 unsigned int idx;
145 int ret;
146 struct pvr2_buffer *bp;
147 struct pvr2_stream *stream = NULL;
149 if (adap->stream_run) return -EIO;
151 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
152 /* somebody else already has the stream */
153 if (ret < 0) return ret;
155 stream = adap->channel.stream->stream;
157 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
158 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
159 GFP_KERNEL);
160 if (!(adap->buffer_storage[idx])) return -ENOMEM;
163 pvr2_stream_set_callback(pvr->video_stream.stream,
164 (pvr2_stream_callback) pvr2_dvb_notify, adap);
166 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
167 if (ret < 0) return ret;
169 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
170 bp = pvr2_stream_get_buffer(stream, idx);
171 pvr2_buffer_set_buffer(bp,
172 adap->buffer_storage[idx],
173 PVR2_DVB_BUFFER_SIZE);
176 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
177 if (ret < 0) return ret;
179 while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
180 ret = pvr2_buffer_queue(bp);
181 if (ret < 0) return ret;
184 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
186 if (IS_ERR(adap->thread)) {
187 ret = PTR_ERR(adap->thread);
188 adap->thread = NULL;
189 return ret;
192 adap->stream_run = !0;
194 return 0;
197 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
199 int ret = pvr2_dvb_stream_do_start(adap);
200 if (ret < 0) pvr2_dvb_stream_end(adap);
201 return ret;
204 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
206 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
207 int ret = 0;
209 if (adap == NULL) return -ENODEV;
211 mutex_lock(&adap->lock);
212 do {
213 if (onoff) {
214 if (!adap->feedcount) {
215 pvr2_trace(PVR2_TRACE_DVB_FEED,
216 "start feeding demux");
217 ret = pvr2_dvb_stream_start(adap);
218 if (ret < 0) break;
220 (adap->feedcount)++;
221 } else if (adap->feedcount > 0) {
222 (adap->feedcount)--;
223 if (!adap->feedcount) {
224 pvr2_trace(PVR2_TRACE_DVB_FEED,
225 "stop feeding demux");
226 pvr2_dvb_stream_end(adap);
229 } while (0);
230 mutex_unlock(&adap->lock);
232 return ret;
235 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
237 pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
238 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
241 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
243 pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
244 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
247 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
249 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
250 return pvr2_channel_limit_inputs(
251 &adap->channel,
252 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
255 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
257 int ret;
259 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
260 THIS_MODULE/*&hdw->usb_dev->owner*/,
261 &adap->channel.hdw->usb_dev->dev,
262 adapter_nr);
263 if (ret < 0) {
264 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
265 "dvb_register_adapter failed: error %d", ret);
266 goto err;
268 adap->dvb_adap.priv = adap;
270 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
271 DMX_SECTION_FILTERING |
272 DMX_MEMORY_BASED_FILTERING;
273 adap->demux.priv = adap;
274 adap->demux.filternum = 256;
275 adap->demux.feednum = 256;
276 adap->demux.start_feed = pvr2_dvb_start_feed;
277 adap->demux.stop_feed = pvr2_dvb_stop_feed;
278 adap->demux.write_to_decoder = NULL;
280 ret = dvb_dmx_init(&adap->demux);
281 if (ret < 0) {
282 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
283 "dvb_dmx_init failed: error %d", ret);
284 goto err_dmx;
287 adap->dmxdev.filternum = adap->demux.filternum;
288 adap->dmxdev.demux = &adap->demux.dmx;
289 adap->dmxdev.capabilities = 0;
291 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
292 if (ret < 0) {
293 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
294 "dvb_dmxdev_init failed: error %d", ret);
295 goto err_dmx_dev;
298 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
300 return 0;
302 err_dmx_dev:
303 dvb_dmx_release(&adap->demux);
304 err_dmx:
305 dvb_unregister_adapter(&adap->dvb_adap);
306 err:
307 return ret;
310 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
312 pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
313 dvb_net_release(&adap->dvb_net);
314 adap->demux.dmx.close(&adap->demux.dmx);
315 dvb_dmxdev_release(&adap->dmxdev);
316 dvb_dmx_release(&adap->demux);
317 dvb_unregister_adapter(&adap->dvb_adap);
318 return 0;
321 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
323 struct pvr2_hdw *hdw = adap->channel.hdw;
324 const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
325 int ret = 0;
327 if (dvb_props == NULL) {
328 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
329 return -EINVAL;
332 ret = pvr2_channel_limit_inputs(
333 &adap->channel,
334 (1 << PVR2_CVAL_INPUT_DTV));
335 if (ret) {
336 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
337 "failed to grab control of dtv input (code=%d)",
338 ret);
339 return ret;
342 if (dvb_props->frontend_attach == NULL) {
343 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
344 "frontend_attach not defined!");
345 ret = -EINVAL;
346 goto done;
349 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
351 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
352 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
353 "frontend registration failed!");
354 dvb_frontend_detach(adap->fe);
355 adap->fe = NULL;
356 ret = -ENODEV;
357 goto done;
360 if (dvb_props->tuner_attach)
361 dvb_props->tuner_attach(adap);
363 if (adap->fe->ops.analog_ops.standby)
364 adap->fe->ops.analog_ops.standby(adap->fe);
366 /* Ensure all frontends negotiate bus access */
367 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
369 } else {
370 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
371 "no frontend was attached!");
372 ret = -ENODEV;
373 return ret;
376 done:
377 pvr2_channel_limit_inputs(&adap->channel, 0);
378 return ret;
381 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
383 if (adap->fe != NULL) {
384 dvb_unregister_frontend(adap->fe);
385 dvb_frontend_detach(adap->fe);
387 return 0;
390 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
392 pvr2_dvb_stream_end(adap);
393 pvr2_dvb_frontend_exit(adap);
394 pvr2_dvb_adapter_exit(adap);
395 pvr2_channel_done(&adap->channel);
396 kfree(adap);
399 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
401 struct pvr2_dvb_adapter *adap;
402 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
403 if (!adap->channel.mc_head->disconnect_flag) return;
404 pvr2_dvb_destroy(adap);
407 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
409 int ret = 0;
410 struct pvr2_dvb_adapter *adap;
411 if (!pvr->hdw->hdw_desc->dvb_props) {
412 /* Device lacks a digital interface so don't set up
413 the DVB side of the driver either. For now. */
414 return NULL;
416 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
417 if (!adap) return adap;
418 pvr2_channel_init(&adap->channel, pvr);
419 adap->channel.check_func = pvr2_dvb_internal_check;
420 init_waitqueue_head(&adap->buffer_wait_data);
421 mutex_init(&adap->lock);
422 ret = pvr2_dvb_adapter_init(adap);
423 if (ret < 0) goto fail1;
424 ret = pvr2_dvb_frontend_init(adap);
425 if (ret < 0) goto fail2;
426 return adap;
428 fail2:
429 pvr2_dvb_adapter_exit(adap);
430 fail1:
431 pvr2_channel_done(&adap->channel);
432 return NULL;