1 // SPDX-License-Identifier: GPL-2.0-only
3 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
5 * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
8 #include <linux/kthread.h>
9 #include <linux/freezer.h>
10 #include <linux/slab.h>
12 #include <media/dvbdev.h>
13 #include "pvrusb2-debug.h"
14 #include "pvrusb2-hdw-internal.h"
15 #include "pvrusb2-hdw.h"
16 #include "pvrusb2-io.h"
17 #include "pvrusb2-dvb.h"
19 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
21 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter
*adap
)
25 struct pvr2_buffer
*bp
;
26 struct pvr2_stream
*stream
;
28 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread started");
31 stream
= adap
->channel
.stream
->stream
;
34 if (kthread_should_stop()) break;
36 bp
= pvr2_stream_get_ready_buffer(stream
);
38 count
= pvr2_buffer_get_count(bp
);
43 pvr2_buffer_get_id(bp
)],
46 ret
= pvr2_buffer_get_status(bp
);
49 ret
= pvr2_buffer_queue(bp
);
52 /* Since we know we did something to a buffer,
53 just go back and try again. No point in
54 blocking unless we really ran out of
55 buffers to process. */
60 /* Wait until more buffers become available or we're
61 told not to wait any longer. */
62 ret
= wait_event_freezable(adap
->buffer_wait_data
,
63 (pvr2_stream_get_ready_count(stream
) > 0) ||
64 kthread_should_stop());
68 /* If we get here and ret is < 0, then an error has occurred.
69 Probably would be a good idea to communicate that to DVB core... */
71 pvr2_trace(PVR2_TRACE_DVB_FEED
, "dvb feed thread stopped");
76 static int pvr2_dvb_feed_thread(void *data
)
78 int stat
= pvr2_dvb_feed_func(data
);
80 while (!kthread_should_stop()) {
81 set_current_state(TASK_INTERRUPTIBLE
);
87 static void pvr2_dvb_notify(void *ptr
)
89 struct pvr2_dvb_adapter
*adap
= ptr
;
91 wake_up(&adap
->buffer_wait_data
);
94 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter
*adap
)
97 struct pvr2_stream
*stream
;
100 kthread_stop(adap
->thread
);
104 if (adap
->channel
.stream
) {
105 stream
= adap
->channel
.stream
->stream
;
110 pvr2_hdw_set_streaming(adap
->channel
.hdw
, 0);
111 pvr2_stream_set_callback(stream
, NULL
, NULL
);
112 pvr2_stream_kill(stream
);
113 pvr2_stream_set_buffer_count(stream
, 0);
114 pvr2_channel_claim_stream(&adap
->channel
, NULL
);
117 if (adap
->stream_run
) {
118 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
119 if (!(adap
->buffer_storage
[idx
])) continue;
120 kfree(adap
->buffer_storage
[idx
]);
121 adap
->buffer_storage
[idx
] = NULL
;
123 adap
->stream_run
= 0;
127 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter
*adap
)
129 struct pvr2_context
*pvr
= adap
->channel
.mc_head
;
132 struct pvr2_buffer
*bp
;
133 struct pvr2_stream
*stream
= NULL
;
135 if (adap
->stream_run
) return -EIO
;
137 ret
= pvr2_channel_claim_stream(&adap
->channel
, &pvr
->video_stream
);
138 /* somebody else already has the stream */
139 if (ret
< 0) return ret
;
141 stream
= adap
->channel
.stream
->stream
;
143 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
144 adap
->buffer_storage
[idx
] = kmalloc(PVR2_DVB_BUFFER_SIZE
,
146 if (!(adap
->buffer_storage
[idx
])) return -ENOMEM
;
149 pvr2_stream_set_callback(pvr
->video_stream
.stream
,
150 pvr2_dvb_notify
, adap
);
152 ret
= pvr2_stream_set_buffer_count(stream
, PVR2_DVB_BUFFER_COUNT
);
153 if (ret
< 0) return ret
;
155 for (idx
= 0; idx
< PVR2_DVB_BUFFER_COUNT
; idx
++) {
156 bp
= pvr2_stream_get_buffer(stream
, idx
);
157 pvr2_buffer_set_buffer(bp
,
158 adap
->buffer_storage
[idx
],
159 PVR2_DVB_BUFFER_SIZE
);
162 ret
= pvr2_hdw_set_streaming(adap
->channel
.hdw
, 1);
163 if (ret
< 0) return ret
;
165 while ((bp
= pvr2_stream_get_idle_buffer(stream
)) != NULL
) {
166 ret
= pvr2_buffer_queue(bp
);
167 if (ret
< 0) return ret
;
170 adap
->thread
= kthread_run(pvr2_dvb_feed_thread
, adap
, "pvrusb2-dvb");
172 if (IS_ERR(adap
->thread
)) {
173 ret
= PTR_ERR(adap
->thread
);
178 adap
->stream_run
= !0;
183 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter
*adap
)
185 int ret
= pvr2_dvb_stream_do_start(adap
);
186 if (ret
< 0) pvr2_dvb_stream_end(adap
);
190 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed
*dvbdmxfeed
, int onoff
)
192 struct pvr2_dvb_adapter
*adap
= dvbdmxfeed
->demux
->priv
;
195 if (adap
== NULL
) return -ENODEV
;
197 mutex_lock(&adap
->lock
);
200 if (!adap
->feedcount
) {
201 pvr2_trace(PVR2_TRACE_DVB_FEED
,
202 "start feeding demux");
203 ret
= pvr2_dvb_stream_start(adap
);
207 } else if (adap
->feedcount
> 0) {
209 if (!adap
->feedcount
) {
210 pvr2_trace(PVR2_TRACE_DVB_FEED
,
211 "stop feeding demux");
212 pvr2_dvb_stream_end(adap
);
216 mutex_unlock(&adap
->lock
);
221 static int pvr2_dvb_start_feed(struct dvb_demux_feed
*dvbdmxfeed
)
223 pvr2_trace(PVR2_TRACE_DVB_FEED
, "start pid: 0x%04x", dvbdmxfeed
->pid
);
224 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 1);
227 static int pvr2_dvb_stop_feed(struct dvb_demux_feed
*dvbdmxfeed
)
229 pvr2_trace(PVR2_TRACE_DVB_FEED
, "stop pid: 0x%04x", dvbdmxfeed
->pid
);
230 return pvr2_dvb_ctrl_feed(dvbdmxfeed
, 0);
233 static int pvr2_dvb_bus_ctrl(struct dvb_frontend
*fe
, int acquire
)
235 struct pvr2_dvb_adapter
*adap
= fe
->dvb
->priv
;
236 return pvr2_channel_limit_inputs(
238 (acquire
? (1 << PVR2_CVAL_INPUT_DTV
) : 0));
241 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter
*adap
)
245 ret
= dvb_register_adapter(&adap
->dvb_adap
, "pvrusb2-dvb",
246 THIS_MODULE
/*&hdw->usb_dev->owner*/,
247 &adap
->channel
.hdw
->usb_dev
->dev
,
250 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
251 "dvb_register_adapter failed: error %d", ret
);
254 adap
->dvb_adap
.priv
= adap
;
256 adap
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
|
257 DMX_SECTION_FILTERING
|
258 DMX_MEMORY_BASED_FILTERING
;
259 adap
->demux
.priv
= adap
;
260 adap
->demux
.filternum
= 256;
261 adap
->demux
.feednum
= 256;
262 adap
->demux
.start_feed
= pvr2_dvb_start_feed
;
263 adap
->demux
.stop_feed
= pvr2_dvb_stop_feed
;
264 adap
->demux
.write_to_decoder
= NULL
;
266 ret
= dvb_dmx_init(&adap
->demux
);
268 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
269 "dvb_dmx_init failed: error %d", ret
);
273 adap
->dmxdev
.filternum
= adap
->demux
.filternum
;
274 adap
->dmxdev
.demux
= &adap
->demux
.dmx
;
275 adap
->dmxdev
.capabilities
= 0;
277 ret
= dvb_dmxdev_init(&adap
->dmxdev
, &adap
->dvb_adap
);
279 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
280 "dvb_dmxdev_init failed: error %d", ret
);
284 dvb_net_init(&adap
->dvb_adap
, &adap
->dvb_net
, &adap
->demux
.dmx
);
289 dvb_dmx_release(&adap
->demux
);
291 dvb_unregister_adapter(&adap
->dvb_adap
);
296 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter
*adap
)
298 pvr2_trace(PVR2_TRACE_INFO
, "unregistering DVB devices");
299 dvb_net_release(&adap
->dvb_net
);
300 adap
->demux
.dmx
.close(&adap
->demux
.dmx
);
301 dvb_dmxdev_release(&adap
->dmxdev
);
302 dvb_dmx_release(&adap
->demux
);
303 dvb_unregister_adapter(&adap
->dvb_adap
);
307 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter
*adap
)
309 struct pvr2_hdw
*hdw
= adap
->channel
.hdw
;
310 const struct pvr2_dvb_props
*dvb_props
= hdw
->hdw_desc
->dvb_props
;
313 if (dvb_props
== NULL
) {
314 pvr2_trace(PVR2_TRACE_ERROR_LEGS
, "fe_props not defined!");
318 ret
= pvr2_channel_limit_inputs(
320 (1 << PVR2_CVAL_INPUT_DTV
));
322 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
323 "failed to grab control of dtv input (code=%d)",
328 if (dvb_props
->frontend_attach
== NULL
) {
329 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
330 "frontend_attach not defined!");
335 if (dvb_props
->frontend_attach(adap
) == 0 && adap
->fe
[0]) {
336 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
[0])) {
337 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
338 "frontend registration failed!");
342 if (adap
->fe
[0]->ops
.analog_ops
.standby
)
343 adap
->fe
[0]->ops
.analog_ops
.standby(adap
->fe
[0]);
345 pvr2_trace(PVR2_TRACE_INFO
, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
347 adap
->fe
[0]->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
349 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
350 "no frontend was attached!");
355 if (dvb_props
->tuner_attach
&& dvb_props
->tuner_attach(adap
)) {
356 pvr2_trace(PVR2_TRACE_ERROR_LEGS
, "tuner attach failed");
363 adap
->fe
[1]->tuner_priv
= adap
->fe
[0]->tuner_priv
;
364 memcpy(&adap
->fe
[1]->ops
.tuner_ops
,
365 &adap
->fe
[0]->ops
.tuner_ops
,
366 sizeof(struct dvb_tuner_ops
));
368 if (dvb_register_frontend(&adap
->dvb_adap
, adap
->fe
[1])) {
369 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
370 "frontend registration failed!");
375 adap
->dvb_adap
.mfe_shared
= 1;
377 if (adap
->fe
[1]->ops
.analog_ops
.standby
)
378 adap
->fe
[1]->ops
.analog_ops
.standby(adap
->fe
[1]);
380 pvr2_trace(PVR2_TRACE_INFO
, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
382 adap
->fe
[1]->ops
.ts_bus_ctrl
= pvr2_dvb_bus_ctrl
;
385 pvr2_channel_limit_inputs(&adap
->channel
, 0);
389 dvb_frontend_detach(adap
->fe
[1]);
392 dvb_unregister_frontend(adap
->fe
[0]);
394 dvb_frontend_detach(adap
->fe
[0]);
396 dvb_module_release(adap
->i2c_client_tuner
);
397 dvb_module_release(adap
->i2c_client_demod
[1]);
398 dvb_module_release(adap
->i2c_client_demod
[0]);
403 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter
*adap
)
406 dvb_unregister_frontend(adap
->fe
[1]);
407 dvb_frontend_detach(adap
->fe
[1]);
411 dvb_unregister_frontend(adap
->fe
[0]);
412 dvb_frontend_detach(adap
->fe
[0]);
416 dvb_module_release(adap
->i2c_client_tuner
);
417 adap
->i2c_client_tuner
= NULL
;
418 dvb_module_release(adap
->i2c_client_demod
[1]);
419 adap
->i2c_client_demod
[1] = NULL
;
420 dvb_module_release(adap
->i2c_client_demod
[0]);
421 adap
->i2c_client_demod
[0] = NULL
;
426 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter
*adap
)
428 pvr2_dvb_stream_end(adap
);
429 pvr2_dvb_frontend_exit(adap
);
430 pvr2_dvb_adapter_exit(adap
);
431 pvr2_channel_done(&adap
->channel
);
435 static void pvr2_dvb_internal_check(struct pvr2_channel
*chp
)
437 struct pvr2_dvb_adapter
*adap
;
438 adap
= container_of(chp
, struct pvr2_dvb_adapter
, channel
);
439 if (!adap
->channel
.mc_head
->disconnect_flag
) return;
440 pvr2_dvb_destroy(adap
);
443 struct pvr2_dvb_adapter
*pvr2_dvb_create(struct pvr2_context
*pvr
)
446 struct pvr2_dvb_adapter
*adap
;
447 if (!pvr
->hdw
->hdw_desc
->dvb_props
) {
448 /* Device lacks a digital interface so don't set up
449 the DVB side of the driver either. For now. */
452 adap
= kzalloc(sizeof(*adap
), GFP_KERNEL
);
453 if (!adap
) return adap
;
454 pvr2_channel_init(&adap
->channel
, pvr
);
455 adap
->channel
.check_func
= pvr2_dvb_internal_check
;
456 init_waitqueue_head(&adap
->buffer_wait_data
);
457 mutex_init(&adap
->lock
);
458 ret
= pvr2_dvb_adapter_init(adap
);
459 if (ret
< 0) goto fail1
;
460 ret
= pvr2_dvb_frontend_init(adap
);
461 if (ret
< 0) goto fail2
;
465 pvr2_dvb_adapter_exit(adap
);
467 pvr2_channel_done(&adap
->channel
);