1 // SPDX-License-Identifier: GPL-2.0-only
3 * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
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 /* Not sure about this... */
39 bp = pvr2_stream_get_ready_buffer(stream);
41 count = pvr2_buffer_get_count(bp);
46 pvr2_buffer_get_id(bp)],
49 ret = pvr2_buffer_get_status(bp);
52 ret = pvr2_buffer_queue(bp);
55 /* Since we know we did something to a buffer,
56 just go back and try again. No point in
57 blocking unless we really ran out of
58 buffers to process. */
63 /* Wait until more buffers become available or we're
64 told not to wait any longer. */
65 ret = wait_event_interruptible(
66 adap->buffer_wait_data,
67 (pvr2_stream_get_ready_count(stream) > 0) ||
68 kthread_should_stop());
72 /* If we get here and ret is < 0, then an error has occurred.
73 Probably would be a good idea to communicate that to DVB core... */
75 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
80 static int pvr2_dvb_feed_thread(void *data)
82 int stat = pvr2_dvb_feed_func(data);
83 /* from videobuf-dvb.c: */
84 while (!kthread_should_stop()) {
85 set_current_state(TASK_INTERRUPTIBLE);
91 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
93 wake_up(&adap->buffer_wait_data);
96 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
99 struct pvr2_stream *stream;
102 kthread_stop(adap->thread);
106 if (adap->channel.stream) {
107 stream = adap->channel.stream->stream;
112 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
113 pvr2_stream_set_callback(stream, NULL, NULL);
114 pvr2_stream_kill(stream);
115 pvr2_stream_set_buffer_count(stream, 0);
116 pvr2_channel_claim_stream(&adap->channel, NULL);
119 if (adap->stream_run) {
120 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
121 if (!(adap->buffer_storage[idx])) continue;
122 kfree(adap->buffer_storage[idx]);
123 adap->buffer_storage[idx] = NULL;
125 adap->stream_run = 0;
129 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
131 struct pvr2_context *pvr = adap->channel.mc_head;
134 struct pvr2_buffer *bp;
135 struct pvr2_stream *stream = NULL;
137 if (adap->stream_run) return -EIO;
139 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
140 /* somebody else already has the stream */
141 if (ret < 0) return ret;
143 stream = adap->channel.stream->stream;
145 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
146 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
148 if (!(adap->buffer_storage[idx])) return -ENOMEM;
151 pvr2_stream_set_callback(pvr->video_stream.stream,
152 (pvr2_stream_callback) pvr2_dvb_notify, adap);
154 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
155 if (ret < 0) return ret;
157 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
158 bp = pvr2_stream_get_buffer(stream, idx);
159 pvr2_buffer_set_buffer(bp,
160 adap->buffer_storage[idx],
161 PVR2_DVB_BUFFER_SIZE);
164 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
165 if (ret < 0) return ret;
167 while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
168 ret = pvr2_buffer_queue(bp);
169 if (ret < 0) return ret;
172 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
174 if (IS_ERR(adap->thread)) {
175 ret = PTR_ERR(adap->thread);
180 adap->stream_run = !0;
185 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
187 int ret = pvr2_dvb_stream_do_start(adap);
188 if (ret < 0) pvr2_dvb_stream_end(adap);
192 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
194 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
197 if (adap == NULL) return -ENODEV;
199 mutex_lock(&adap->lock);
202 if (!adap->feedcount) {
203 pvr2_trace(PVR2_TRACE_DVB_FEED,
204 "start feeding demux");
205 ret = pvr2_dvb_stream_start(adap);
209 } else if (adap->feedcount > 0) {
211 if (!adap->feedcount) {
212 pvr2_trace(PVR2_TRACE_DVB_FEED,
213 "stop feeding demux");
214 pvr2_dvb_stream_end(adap);
218 mutex_unlock(&adap->lock);
223 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
225 pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
226 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
229 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
231 pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
232 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
235 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
237 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
238 return pvr2_channel_limit_inputs(
240 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
243 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
247 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
248 THIS_MODULE/*&hdw->usb_dev->owner*/,
249 &adap->channel.hdw->usb_dev->dev,
252 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
253 "dvb_register_adapter failed: error %d", ret);
256 adap->dvb_adap.priv = adap;
258 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
259 DMX_SECTION_FILTERING |
260 DMX_MEMORY_BASED_FILTERING;
261 adap->demux.priv = adap;
262 adap->demux.filternum = 256;
263 adap->demux.feednum = 256;
264 adap->demux.start_feed = pvr2_dvb_start_feed;
265 adap->demux.stop_feed = pvr2_dvb_stop_feed;
266 adap->demux.write_to_decoder = NULL;
268 ret = dvb_dmx_init(&adap->demux);
270 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
271 "dvb_dmx_init failed: error %d", ret);
275 adap->dmxdev.filternum = adap->demux.filternum;
276 adap->dmxdev.demux = &adap->demux.dmx;
277 adap->dmxdev.capabilities = 0;
279 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
281 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
282 "dvb_dmxdev_init failed: error %d", ret);
286 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
291 dvb_dmx_release(&adap->demux);
293 dvb_unregister_adapter(&adap->dvb_adap);
298 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
300 pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
301 dvb_net_release(&adap->dvb_net);
302 adap->demux.dmx.close(&adap->demux.dmx);
303 dvb_dmxdev_release(&adap->dmxdev);
304 dvb_dmx_release(&adap->demux);
305 dvb_unregister_adapter(&adap->dvb_adap);
309 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
311 struct pvr2_hdw *hdw = adap->channel.hdw;
312 const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
315 if (dvb_props == NULL) {
316 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
320 ret = pvr2_channel_limit_inputs(
322 (1 << PVR2_CVAL_INPUT_DTV));
324 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
325 "failed to grab control of dtv input (code=%d)",
330 if (dvb_props->frontend_attach == NULL) {
331 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
332 "frontend_attach not defined!");
337 if (dvb_props->frontend_attach(adap) == 0 && adap->fe[0]) {
338 if (dvb_register_frontend(&adap->dvb_adap, adap->fe[0])) {
339 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
340 "frontend registration failed!");
344 if (adap->fe[0]->ops.analog_ops.standby)
345 adap->fe[0]->ops.analog_ops.standby(adap->fe[0]);
347 pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
349 adap->fe[0]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
351 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352 "no frontend was attached!");
357 if (dvb_props->tuner_attach && dvb_props->tuner_attach(adap)) {
358 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "tuner attach failed");
365 adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
366 memcpy(&adap->fe[1]->ops.tuner_ops,
367 &adap->fe[0]->ops.tuner_ops,
368 sizeof(struct dvb_tuner_ops));
370 if (dvb_register_frontend(&adap->dvb_adap, adap->fe[1])) {
371 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
372 "frontend registration failed!");
377 adap->dvb_adap.mfe_shared = 1;
379 if (adap->fe[1]->ops.analog_ops.standby)
380 adap->fe[1]->ops.analog_ops.standby(adap->fe[1]);
382 pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
384 adap->fe[1]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
387 pvr2_channel_limit_inputs(&adap->channel, 0);
391 dvb_frontend_detach(adap->fe[1]);
394 dvb_unregister_frontend(adap->fe[0]);
396 dvb_frontend_detach(adap->fe[0]);
398 dvb_module_release(adap->i2c_client_tuner);
399 dvb_module_release(adap->i2c_client_demod[1]);
400 dvb_module_release(adap->i2c_client_demod[0]);
405 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
408 dvb_unregister_frontend(adap->fe[1]);
409 dvb_frontend_detach(adap->fe[1]);
413 dvb_unregister_frontend(adap->fe[0]);
414 dvb_frontend_detach(adap->fe[0]);
418 dvb_module_release(adap->i2c_client_tuner);
419 adap->i2c_client_tuner = NULL;
420 dvb_module_release(adap->i2c_client_demod[1]);
421 adap->i2c_client_demod[1] = NULL;
422 dvb_module_release(adap->i2c_client_demod[0]);
423 adap->i2c_client_demod[0] = NULL;
428 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
430 pvr2_dvb_stream_end(adap);
431 pvr2_dvb_frontend_exit(adap);
432 pvr2_dvb_adapter_exit(adap);
433 pvr2_channel_done(&adap->channel);
437 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
439 struct pvr2_dvb_adapter *adap;
440 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
441 if (!adap->channel.mc_head->disconnect_flag) return;
442 pvr2_dvb_destroy(adap);
445 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
448 struct pvr2_dvb_adapter *adap;
449 if (!pvr->hdw->hdw_desc->dvb_props) {
450 /* Device lacks a digital interface so don't set up
451 the DVB side of the driver either. For now. */
454 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
455 if (!adap) return adap;
456 pvr2_channel_init(&adap->channel, pvr);
457 adap->channel.check_func = pvr2_dvb_internal_check;
458 init_waitqueue_head(&adap->buffer_wait_data);
459 mutex_init(&adap->lock);
460 ret = pvr2_dvb_adapter_init(adap);
461 if (ret < 0) goto fail1;
462 ret = pvr2_dvb_frontend_init(adap);
463 if (ret < 0) goto fail2;
467 pvr2_dvb_adapter_exit(adap);
469 pvr2_channel_done(&adap->channel);