1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
4  *
5  *  Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
6  */
7 
8 #include <linux/kthread.h>
9 #include <linux/freezer.h>
10 #include <linux/slab.h>
11 #include <linux/mm.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"
18 
19 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
20 
21 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
22 {
23 	int ret;
24 	unsigned int count;
25 	struct pvr2_buffer *bp;
26 	struct pvr2_stream *stream;
27 
28 	pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
29 	set_freezable();
30 
31 	stream = adap->channel.stream->stream;
32 
33 	for (;;) {
34 		if (kthread_should_stop()) break;
35 
36 		/* Not sure about this... */
37 		try_to_freeze();
38 
39 		bp = pvr2_stream_get_ready_buffer(stream);
40 		if (bp != NULL) {
41 			count = pvr2_buffer_get_count(bp);
42 			if (count) {
43 				dvb_dmx_swfilter(
44 					&adap->demux,
45 					adap->buffer_storage[
46 					    pvr2_buffer_get_id(bp)],
47 					count);
48 			} else {
49 				ret = pvr2_buffer_get_status(bp);
50 				if (ret < 0) break;
51 			}
52 			ret = pvr2_buffer_queue(bp);
53 			if (ret < 0) break;
54 
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. */
59 			continue;
60 		}
61 
62 
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());
69 		if (ret < 0) break;
70 	}
71 
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... */
74 
75 	pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
76 
77 	return 0;
78 }
79 
80 static int pvr2_dvb_feed_thread(void *data)
81 {
82 	int stat = pvr2_dvb_feed_func(data);
83 	/* from videobuf-dvb.c: */
84 	while (!kthread_should_stop()) {
85 		set_current_state(TASK_INTERRUPTIBLE);
86 		schedule();
87 	}
88 	return stat;
89 }
90 
91 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
92 {
93 	wake_up(&adap->buffer_wait_data);
94 }
95 
96 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
97 {
98 	unsigned int idx;
99 	struct pvr2_stream *stream;
100 
101 	if (adap->thread) {
102 		kthread_stop(adap->thread);
103 		adap->thread = NULL;
104 	}
105 
106 	if (adap->channel.stream) {
107 		stream = adap->channel.stream->stream;
108 	} else {
109 		stream = NULL;
110 	}
111 	if (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);
117 	}
118 
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;
124 		}
125 		adap->stream_run = 0;
126 	}
127 }
128 
129 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
130 {
131 	struct pvr2_context *pvr = adap->channel.mc_head;
132 	unsigned int idx;
133 	int ret;
134 	struct pvr2_buffer *bp;
135 	struct pvr2_stream *stream = NULL;
136 
137 	if (adap->stream_run) return -EIO;
138 
139 	ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
140 	/* somebody else already has the stream */
141 	if (ret < 0) return ret;
142 
143 	stream = adap->channel.stream->stream;
144 
145 	for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
146 		adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
147 						    GFP_KERNEL);
148 		if (!(adap->buffer_storage[idx])) return -ENOMEM;
149 	}
150 
151 	pvr2_stream_set_callback(pvr->video_stream.stream,
152 				 (pvr2_stream_callback) pvr2_dvb_notify, adap);
153 
154 	ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
155 	if (ret < 0) return ret;
156 
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);
162 	}
163 
164 	ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
165 	if (ret < 0) return ret;
166 
167 	while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
168 		ret = pvr2_buffer_queue(bp);
169 		if (ret < 0) return ret;
170 	}
171 
172 	adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
173 
174 	if (IS_ERR(adap->thread)) {
175 		ret = PTR_ERR(adap->thread);
176 		adap->thread = NULL;
177 		return ret;
178 	}
179 
180 	adap->stream_run = !0;
181 
182 	return 0;
183 }
184 
185 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
186 {
187 	int ret = pvr2_dvb_stream_do_start(adap);
188 	if (ret < 0) pvr2_dvb_stream_end(adap);
189 	return ret;
190 }
191 
192 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
193 {
194 	struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
195 	int ret = 0;
196 
197 	if (adap == NULL) return -ENODEV;
198 
199 	mutex_lock(&adap->lock);
200 	do {
201 		if (onoff) {
202 			if (!adap->feedcount) {
203 				pvr2_trace(PVR2_TRACE_DVB_FEED,
204 					   "start feeding demux");
205 				ret = pvr2_dvb_stream_start(adap);
206 				if (ret < 0) break;
207 			}
208 			(adap->feedcount)++;
209 		} else if (adap->feedcount > 0) {
210 			(adap->feedcount)--;
211 			if (!adap->feedcount) {
212 				pvr2_trace(PVR2_TRACE_DVB_FEED,
213 					   "stop feeding demux");
214 				pvr2_dvb_stream_end(adap);
215 			}
216 		}
217 	} while (0);
218 	mutex_unlock(&adap->lock);
219 
220 	return ret;
221 }
222 
223 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
224 {
225 	pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
226 	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
227 }
228 
229 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
230 {
231 	pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
232 	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
233 }
234 
235 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
236 {
237 	struct pvr2_dvb_adapter *adap = fe->dvb->priv;
238 	return pvr2_channel_limit_inputs(
239 	    &adap->channel,
240 	    (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
241 }
242 
243 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
244 {
245 	int ret;
246 
247 	ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
248 				   THIS_MODULE/*&hdw->usb_dev->owner*/,
249 				   &adap->channel.hdw->usb_dev->dev,
250 				   adapter_nr);
251 	if (ret < 0) {
252 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
253 			   "dvb_register_adapter failed: error %d", ret);
254 		goto err;
255 	}
256 	adap->dvb_adap.priv = adap;
257 
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;
267 
268 	ret = dvb_dmx_init(&adap->demux);
269 	if (ret < 0) {
270 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
271 			   "dvb_dmx_init failed: error %d", ret);
272 		goto err_dmx;
273 	}
274 
275 	adap->dmxdev.filternum       = adap->demux.filternum;
276 	adap->dmxdev.demux           = &adap->demux.dmx;
277 	adap->dmxdev.capabilities    = 0;
278 
279 	ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
280 	if (ret < 0) {
281 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
282 			   "dvb_dmxdev_init failed: error %d", ret);
283 		goto err_dmx_dev;
284 	}
285 
286 	dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
287 
288 	return 0;
289 
290 err_dmx_dev:
291 	dvb_dmx_release(&adap->demux);
292 err_dmx:
293 	dvb_unregister_adapter(&adap->dvb_adap);
294 err:
295 	return ret;
296 }
297 
298 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
299 {
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);
306 	return 0;
307 }
308 
309 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
310 {
311 	struct pvr2_hdw *hdw = adap->channel.hdw;
312 	const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
313 	int ret = 0;
314 
315 	if (dvb_props == NULL) {
316 		pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
317 		return -EINVAL;
318 	}
319 
320 	ret = pvr2_channel_limit_inputs(
321 	    &adap->channel,
322 	    (1 << PVR2_CVAL_INPUT_DTV));
323 	if (ret) {
324 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
325 			   "failed to grab control of dtv input (code=%d)",
326 		    ret);
327 		return ret;
328 	}
329 
330 	if (dvb_props->frontend_attach == NULL) {
331 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
332 			   "frontend_attach not defined!");
333 		ret = -EINVAL;
334 		goto done;
335 	}
336 
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!");
341 			ret = -ENODEV;
342 			goto fail_frontend0;
343 		}
344 		if (adap->fe[0]->ops.analog_ops.standby)
345 			adap->fe[0]->ops.analog_ops.standby(adap->fe[0]);
346 
347 		pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
348 			   adap->fe[0]->id);
349 		adap->fe[0]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
350 	} else {
351 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
352 			   "no frontend was attached!");
353 		ret = -ENODEV;
354 		return ret;
355 	}
356 
357 	if (dvb_props->tuner_attach && dvb_props->tuner_attach(adap)) {
358 		pvr2_trace(PVR2_TRACE_ERROR_LEGS, "tuner attach failed");
359 		ret = -ENODEV;
360 		goto fail_tuner;
361 	}
362 
363 	if (adap->fe[1]) {
364 		adap->fe[1]->id = 1;
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));
369 
370 		if (dvb_register_frontend(&adap->dvb_adap, adap->fe[1])) {
371 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
372 				   "frontend registration failed!");
373 			ret = -ENODEV;
374 			goto fail_frontend1;
375 		}
376 		/* MFE lock */
377 		adap->dvb_adap.mfe_shared = 1;
378 
379 		if (adap->fe[1]->ops.analog_ops.standby)
380 			adap->fe[1]->ops.analog_ops.standby(adap->fe[1]);
381 
382 		pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
383 			   adap->fe[1]->id);
384 		adap->fe[1]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
385 	}
386 done:
387 	pvr2_channel_limit_inputs(&adap->channel, 0);
388 	return ret;
389 
390 fail_frontend1:
391 	dvb_frontend_detach(adap->fe[1]);
392 	adap->fe[1] = NULL;
393 fail_tuner:
394 	dvb_unregister_frontend(adap->fe[0]);
395 fail_frontend0:
396 	dvb_frontend_detach(adap->fe[0]);
397 	adap->fe[0] = NULL;
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]);
401 
402 	return ret;
403 }
404 
405 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
406 {
407 	if (adap->fe[1]) {
408 		dvb_unregister_frontend(adap->fe[1]);
409 		dvb_frontend_detach(adap->fe[1]);
410 		adap->fe[1] = NULL;
411 	}
412 	if (adap->fe[0]) {
413 		dvb_unregister_frontend(adap->fe[0]);
414 		dvb_frontend_detach(adap->fe[0]);
415 		adap->fe[0] = NULL;
416 	}
417 
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;
424 
425 	return 0;
426 }
427 
428 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
429 {
430 	pvr2_dvb_stream_end(adap);
431 	pvr2_dvb_frontend_exit(adap);
432 	pvr2_dvb_adapter_exit(adap);
433 	pvr2_channel_done(&adap->channel);
434 	kfree(adap);
435 }
436 
437 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
438 {
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);
443 }
444 
445 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
446 {
447 	int ret = 0;
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. */
452 		return NULL;
453 	}
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;
464 	return adap;
465 
466 fail2:
467 	pvr2_dvb_adapter_exit(adap);
468 fail1:
469 	pvr2_channel_done(&adap->channel);
470 	return NULL;
471 }
472 
473