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>
24 #include "pvrusb2-debug.h"
25 #include "pvrusb2-hdw-internal.h"
26 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-io.h"
28 #include "pvrusb2-dvb.h"
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
32 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter
*adap
)
36 struct pvr2_buffer
*bp
;
37 struct pvr2_stream
*stream
;
39 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread started");
42 stream
= adap
->channel
.stream
->stream
;
45 if (kthread_should_stop()) break;
47 /* Not sure about this... */
50 bp
= pvr2_stream_get_ready_buffer(stream
);
52 count
= pvr2_buffer_get_count(bp
);
57 pvr2_buffer_get_id(bp
)],
60 ret
= pvr2_buffer_get_status(bp
);
63 ret
= pvr2_buffer_queue(bp
);
66 /* Since we know we did something to a buffer,
67 just go back and try again. No point in
68 blocking unless we really ran out of
69 buffers to process. */
74 /* Wait until more buffers become available or we're
75 told not to wait any longer. */
76 ret
= wait_event_interruptible(
77 adap
->buffer_wait_data
,
78 (pvr2_stream_get_ready_count(stream
) > 0) ||
79 kthread_should_stop());
83 /* If we get here and ret is < 0, then an error has occurred.
84 Probably would be a good idea to communicate that to DVB core... */
86 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread stopped");
91 static int pvr2_dvb_feed_thread(void *data
)
93 int stat
= pvr2_dvb_feed_func(data
);
94 /* from videobuf-dvb.c: */
95 while (!kthread_should_stop()) {
96 set_current_state(TASK_INTERRUPTIBLE
);
102 static void pvr2_dvb_notify(struct pvr2_dvb_adapter
*adap
)
104 wake_up(&adap
->buffer_wait_data
);
107 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter
*adap
)
110 struct pvr2_stream
*stream
;
113 kthread_stop(adap
->thread
);
117 if (adap
->channel
.stream
) {
118 stream
= adap
->channel
.stream
->stream
;
123 pvr2_hdw_set_streaming(adap
->channel
.hdw
, 0);
124 pvr2_stream_set_callback(stream
, NULL
, NULL
);
125 pvr2_stream_kill(stream
);
126 pvr2_stream_set_buffer_count(stream
, 0);
127 pvr2_channel_claim_stream(&adap
->channel
, NULL
);
130 if (adap
->stream_run
) {
131 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
132 if (!(adap
->buffer_storage
[idx
])) continue;
133 kfree(adap
->buffer_storage
[idx
]);
134 adap
->buffer_storage
[idx
] = NULL
;
136 adap
->stream_run
= 0;
140 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter
*adap
)
142 struct pvr2_context
*pvr
= adap
->channel
.mc_head
;
145 struct pvr2_buffer
*bp
;
146 struct pvr2_stream
*stream
= NULL
;
148 if (adap
->stream_run
) return -EIO
;
150 ret
= pvr2_channel_claim_stream(&adap
->channel
, &pvr
->video_stream
);
151 /* somebody else already has the stream */
152 if (ret
< 0) return ret
;
154 stream
= adap
->channel
.stream
->stream
;
156 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
157 adap
->buffer_storage
[idx
] = kmalloc(PVR2_DVB_BUFFER_SIZE
,
159 if (!(adap
->buffer_storage
[idx
])) return -ENOMEM
;
162 pvr2_stream_set_callback(pvr
->video_stream
.stream
,
163 (pvr2_stream_callback
) pvr2_dvb_notify
, adap
);
165 ret
= pvr2_stream_set_buffer_count(stream
, PVR2_DVB_BUFFER_COUNT
);
166 if (ret
< 0) return ret
;
168 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
169 bp
= pvr2_stream_get_buffer(stream
, idx
);
170 pvr2_buffer_set_buffer(bp
,
171 adap
->buffer_storage
[idx
],
172 PVR2_DVB_BUFFER_SIZE
);
175 ret
= pvr2_hdw_set_streaming(adap
->channel
.hdw
, 1);
176 if (ret
< 0) return ret
;
178 while ((bp
= pvr2_stream_get_idle_buffer(stream
)) != NULL
) {
179 ret
= pvr2_buffer_queue(bp
);
180 if (ret
< 0) return ret
;
183 adap
->thread
= kthread_run(pvr2_dvb_feed_thread
, adap
, "pvrusb2-dvb");
185 if (IS_ERR(adap
->thread
)) {
186 ret
= PTR_ERR(adap
->thread
);
191 adap
->stream_run
= !0;
196 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter
*adap
)
198 int ret
= pvr2_dvb_stream_do_start(adap
);
199 if (ret
< 0) pvr2_dvb_stream_end(adap
);
203 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed
*dvbdmxfeed
, int onoff
)
205 struct pvr2_dvb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
208 if (adap
== NULL
) return -ENODEV
;
210 mutex_lock(&adap
->lock
);
213 if (!adap
->feedcount
) {
214 pvr2_trace(PVR2_TRACE_DVB_FEED
,
215 "start feeding demux");
216 ret
= pvr2_dvb_stream_start(adap
);
220 } else if (adap
->feedcount
> 0) {
222 if (!adap
->feedcount
) {
223 pvr2_trace(PVR2_TRACE_DVB_FEED
,
224 "stop feeding demux");
225 pvr2_dvb_stream_end(adap
);
229 mutex_unlock(&adap
->lock
);
234 static int pvr2_dvb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
236 pvr2_trace(PVR2_TRACE_DVB_FEED
, "start pid: 0x%04x", dvbdmxfeed
->pid
);
237 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 1);
240 static int pvr2_dvb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
242 pvr2_trace(PVR2_TRACE_DVB_FEED
, "stop pid: 0x%04x", dvbdmxfeed
->pid
);
243 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 0);
246 static int pvr2_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
248 struct pvr2_dvb_adapter
*adap
= fe
->dvb
->priv
;
249 return pvr2_channel_limit_inputs(
251 (acquire
? (1 << PVR2_CVAL_INPUT_DTV
) : 0));
254 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter
*adap
)
258 ret
= dvb_register_adapter(&adap
->dvb_adap
, "pvrusb2-dvb",
259 THIS_MODULE
/*&hdw->usb_dev->owner*/,
260 &adap
->channel
.hdw
->usb_dev
->dev
,
263 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
264 "dvb_register_adapter failed: error %d", ret
);
267 adap
->dvb_adap
.priv
= adap
;
269 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
|
270 DMX_SECTION_FILTERING
|
271 DMX_MEMORY_BASED_FILTERING
;
272 adap
->demux
.priv
= adap
;
273 adap
->demux
.filternum
= 256;
274 adap
->demux
.feednum
= 256;
275 adap
->demux
.start_feed
= pvr2_dvb_start_feed
;
276 adap
->demux
.stop_feed
= pvr2_dvb_stop_feed
;
277 adap
->demux
.write_to_decoder
= NULL
;
279 ret
= dvb_dmx_init(&adap
->demux
);
281 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
282 "dvb_dmx_init failed: error %d", ret
);
286 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
287 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
288 adap
->dmxdev
.capabilities
= 0;
290 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
292 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
293 "dvb_dmxdev_init failed: error %d", ret
);
297 dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
302 dvb_dmx_release(&adap
->demux
);
304 dvb_unregister_adapter(&adap
->dvb_adap
);
309 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter
*adap
)
311 pvr2_trace(PVR2_TRACE_INFO
, "unregistering DVB devices");
312 dvb_net_release(&adap
->dvb_net
);
313 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
314 dvb_dmxdev_release(&adap
->dmxdev
);
315 dvb_dmx_release(&adap
->demux
);
316 dvb_unregister_adapter(&adap
->dvb_adap
);
320 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter
*adap
)
322 struct pvr2_hdw
*hdw
= adap
->channel
.hdw
;
323 struct pvr2_dvb_props
*dvb_props
= hdw
->hdw_desc
->dvb_props
;
326 if (dvb_props
== NULL
) {
327 pvr2_trace(PVR2_TRACE_ERROR_LEGS
, "fe_props not defined!");
331 ret
= pvr2_channel_limit_inputs(
333 (1 << PVR2_CVAL_INPUT_DTV
));
335 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
336 "failed to grab control of dtv input (code=%d)",
341 if (dvb_props
->frontend_attach
== NULL
) {
342 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
343 "frontend_attach not defined!");
348 if ((dvb_props
->frontend_attach(adap
) == 0) && (adap
->fe
)) {
350 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
)) {
351 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
352 "frontend registration failed!");
353 dvb_frontend_detach(adap
->fe
);
359 if (dvb_props
->tuner_attach
)
360 dvb_props
->tuner_attach(adap
);
362 if (adap
->fe
->ops
.analog_ops
.standby
)
363 adap
->fe
->ops
.analog_ops
.standby(adap
->fe
);
365 /* Ensure all frontends negotiate bus access */
366 adap
->fe
->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
369 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
370 "no frontend was attached!");
376 pvr2_channel_limit_inputs(&adap
->channel
, 0);
380 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter
*adap
)
382 if (adap
->fe
!= NULL
) {
383 dvb_unregister_frontend(adap
->fe
);
384 dvb_frontend_detach(adap
->fe
);
389 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter
*adap
)
391 pvr2_dvb_stream_end(adap
);
392 pvr2_dvb_frontend_exit(adap
);
393 pvr2_dvb_adapter_exit(adap
);
394 pvr2_channel_done(&adap
->channel
);
398 static void pvr2_dvb_internal_check(struct pvr2_channel
*chp
)
400 struct pvr2_dvb_adapter
*adap
;
401 adap
= container_of(chp
, struct pvr2_dvb_adapter
, channel
);
402 if (!adap
->channel
.mc_head
->disconnect_flag
) return;
403 pvr2_dvb_destroy(adap
);
406 struct pvr2_dvb_adapter
*pvr2_dvb_create(struct pvr2_context
*pvr
)
409 struct pvr2_dvb_adapter
*adap
;
410 if (!pvr
->hdw
->hdw_desc
->dvb_props
) {
411 /* Device lacks a digital interface so don't set up
412 the DVB side of the driver either. For now. */
415 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
416 if (!adap
) return adap
;
417 pvr2_channel_init(&adap
->channel
, pvr
);
418 adap
->channel
.check_func
= pvr2_dvb_internal_check
;
419 init_waitqueue_head(&adap
->buffer_wait_data
);
420 mutex_init(&adap
->lock
);
421 ret
= pvr2_dvb_adapter_init(adap
);
422 if (ret
< 0) goto fail1
;
423 ret
= pvr2_dvb_frontend_init(adap
);
424 if (ret
< 0) goto fail2
;
428 pvr2_dvb_adapter_exit(adap
);
430 pvr2_channel_done(&adap
->channel
);