xref: /linux/drivers/media/usb/pvrusb2/pvrusb2-dvb.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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 
pvr2_dvb_feed_func(struct pvr2_dvb_adapter * adap)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 		bp = pvr2_stream_get_ready_buffer(stream);
37 		if (bp != NULL) {
38 			count = pvr2_buffer_get_count(bp);
39 			if (count) {
40 				dvb_dmx_swfilter(
41 					&adap->demux,
42 					adap->buffer_storage[
43 					    pvr2_buffer_get_id(bp)],
44 					count);
45 			} else {
46 				ret = pvr2_buffer_get_status(bp);
47 				if (ret < 0) break;
48 			}
49 			ret = pvr2_buffer_queue(bp);
50 			if (ret < 0) break;
51 
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. */
56 			continue;
57 		}
58 
59 
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());
65 		if (ret < 0) break;
66 	}
67 
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... */
70 
71 	pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
72 
73 	return 0;
74 }
75 
pvr2_dvb_feed_thread(void * data)76 static int pvr2_dvb_feed_thread(void *data)
77 {
78 	int stat = pvr2_dvb_feed_func(data);
79 
80 	while (!kthread_should_stop()) {
81 		set_current_state(TASK_INTERRUPTIBLE);
82 		schedule();
83 	}
84 	return stat;
85 }
86 
pvr2_dvb_notify(void * ptr)87 static void pvr2_dvb_notify(void *ptr)
88 {
89 	struct pvr2_dvb_adapter *adap = ptr;
90 
91 	wake_up(&adap->buffer_wait_data);
92 }
93 
pvr2_dvb_stream_end(struct pvr2_dvb_adapter * adap)94 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
95 {
96 	unsigned int idx;
97 	struct pvr2_stream *stream;
98 
99 	if (adap->thread) {
100 		kthread_stop(adap->thread);
101 		adap->thread = NULL;
102 	}
103 
104 	if (adap->channel.stream) {
105 		stream = adap->channel.stream->stream;
106 	} else {
107 		stream = NULL;
108 	}
109 	if (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);
115 	}
116 
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;
122 		}
123 		adap->stream_run = 0;
124 	}
125 }
126 
pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter * adap)127 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
128 {
129 	struct pvr2_context *pvr = adap->channel.mc_head;
130 	unsigned int idx;
131 	int ret;
132 	struct pvr2_buffer *bp;
133 	struct pvr2_stream *stream = NULL;
134 
135 	if (adap->stream_run) return -EIO;
136 
137 	ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
138 	/* somebody else already has the stream */
139 	if (ret < 0) return ret;
140 
141 	stream = adap->channel.stream->stream;
142 
143 	for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
144 		adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
145 						    GFP_KERNEL);
146 		if (!(adap->buffer_storage[idx])) return -ENOMEM;
147 	}
148 
149 	pvr2_stream_set_callback(pvr->video_stream.stream,
150 				 pvr2_dvb_notify, adap);
151 
152 	ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
153 	if (ret < 0) return ret;
154 
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);
160 	}
161 
162 	ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
163 	if (ret < 0) return ret;
164 
165 	while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
166 		ret = pvr2_buffer_queue(bp);
167 		if (ret < 0) return ret;
168 	}
169 
170 	adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
171 
172 	if (IS_ERR(adap->thread)) {
173 		ret = PTR_ERR(adap->thread);
174 		adap->thread = NULL;
175 		return ret;
176 	}
177 
178 	adap->stream_run = !0;
179 
180 	return 0;
181 }
182 
pvr2_dvb_stream_start(struct pvr2_dvb_adapter * adap)183 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
184 {
185 	int ret = pvr2_dvb_stream_do_start(adap);
186 	if (ret < 0) pvr2_dvb_stream_end(adap);
187 	return ret;
188 }
189 
pvr2_dvb_ctrl_feed(struct dvb_demux_feed * dvbdmxfeed,int onoff)190 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
191 {
192 	struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
193 	int ret = 0;
194 
195 	if (adap == NULL) return -ENODEV;
196 
197 	mutex_lock(&adap->lock);
198 	do {
199 		if (onoff) {
200 			if (!adap->feedcount) {
201 				pvr2_trace(PVR2_TRACE_DVB_FEED,
202 					   "start feeding demux");
203 				ret = pvr2_dvb_stream_start(adap);
204 				if (ret < 0) break;
205 			}
206 			(adap->feedcount)++;
207 		} else if (adap->feedcount > 0) {
208 			(adap->feedcount)--;
209 			if (!adap->feedcount) {
210 				pvr2_trace(PVR2_TRACE_DVB_FEED,
211 					   "stop feeding demux");
212 				pvr2_dvb_stream_end(adap);
213 			}
214 		}
215 	} while (0);
216 	mutex_unlock(&adap->lock);
217 
218 	return ret;
219 }
220 
pvr2_dvb_start_feed(struct dvb_demux_feed * dvbdmxfeed)221 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
222 {
223 	pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
224 	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
225 }
226 
pvr2_dvb_stop_feed(struct dvb_demux_feed * dvbdmxfeed)227 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
228 {
229 	pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
230 	return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
231 }
232 
pvr2_dvb_bus_ctrl(struct dvb_frontend * fe,int acquire)233 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
234 {
235 	struct pvr2_dvb_adapter *adap = fe->dvb->priv;
236 	return pvr2_channel_limit_inputs(
237 	    &adap->channel,
238 	    (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
239 }
240 
pvr2_dvb_adapter_init(struct pvr2_dvb_adapter * adap)241 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
242 {
243 	int ret;
244 
245 	ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
246 				   THIS_MODULE/*&hdw->usb_dev->owner*/,
247 				   &adap->channel.hdw->usb_dev->dev,
248 				   adapter_nr);
249 	if (ret < 0) {
250 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
251 			   "dvb_register_adapter failed: error %d", ret);
252 		goto err;
253 	}
254 	adap->dvb_adap.priv = adap;
255 
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;
265 
266 	ret = dvb_dmx_init(&adap->demux);
267 	if (ret < 0) {
268 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
269 			   "dvb_dmx_init failed: error %d", ret);
270 		goto err_dmx;
271 	}
272 
273 	adap->dmxdev.filternum       = adap->demux.filternum;
274 	adap->dmxdev.demux           = &adap->demux.dmx;
275 	adap->dmxdev.capabilities    = 0;
276 
277 	ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
278 	if (ret < 0) {
279 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
280 			   "dvb_dmxdev_init failed: error %d", ret);
281 		goto err_dmx_dev;
282 	}
283 
284 	dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
285 
286 	return 0;
287 
288 err_dmx_dev:
289 	dvb_dmx_release(&adap->demux);
290 err_dmx:
291 	dvb_unregister_adapter(&adap->dvb_adap);
292 err:
293 	return ret;
294 }
295 
pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter * adap)296 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
297 {
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);
304 	return 0;
305 }
306 
pvr2_dvb_frontend_init(struct pvr2_dvb_adapter * adap)307 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
308 {
309 	struct pvr2_hdw *hdw = adap->channel.hdw;
310 	const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
311 	int ret = 0;
312 
313 	if (dvb_props == NULL) {
314 		pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
315 		return -EINVAL;
316 	}
317 
318 	ret = pvr2_channel_limit_inputs(
319 	    &adap->channel,
320 	    (1 << PVR2_CVAL_INPUT_DTV));
321 	if (ret) {
322 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
323 			   "failed to grab control of dtv input (code=%d)",
324 		    ret);
325 		return ret;
326 	}
327 
328 	if (dvb_props->frontend_attach == NULL) {
329 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
330 			   "frontend_attach not defined!");
331 		ret = -EINVAL;
332 		goto done;
333 	}
334 
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!");
339 			ret = -ENODEV;
340 			goto fail_frontend0;
341 		}
342 		if (adap->fe[0]->ops.analog_ops.standby)
343 			adap->fe[0]->ops.analog_ops.standby(adap->fe[0]);
344 
345 		pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
346 			   adap->fe[0]->id);
347 		adap->fe[0]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
348 	} else {
349 		pvr2_trace(PVR2_TRACE_ERROR_LEGS,
350 			   "no frontend was attached!");
351 		ret = -ENODEV;
352 		return ret;
353 	}
354 
355 	if (dvb_props->tuner_attach && dvb_props->tuner_attach(adap)) {
356 		pvr2_trace(PVR2_TRACE_ERROR_LEGS, "tuner attach failed");
357 		ret = -ENODEV;
358 		goto fail_tuner;
359 	}
360 
361 	if (adap->fe[1]) {
362 		adap->fe[1]->id = 1;
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));
367 
368 		if (dvb_register_frontend(&adap->dvb_adap, adap->fe[1])) {
369 			pvr2_trace(PVR2_TRACE_ERROR_LEGS,
370 				   "frontend registration failed!");
371 			ret = -ENODEV;
372 			goto fail_frontend1;
373 		}
374 		/* MFE lock */
375 		adap->dvb_adap.mfe_shared = 1;
376 
377 		if (adap->fe[1]->ops.analog_ops.standby)
378 			adap->fe[1]->ops.analog_ops.standby(adap->fe[1]);
379 
380 		pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
381 			   adap->fe[1]->id);
382 		adap->fe[1]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
383 	}
384 done:
385 	pvr2_channel_limit_inputs(&adap->channel, 0);
386 	return ret;
387 
388 fail_frontend1:
389 	dvb_frontend_detach(adap->fe[1]);
390 	adap->fe[1] = NULL;
391 fail_tuner:
392 	dvb_unregister_frontend(adap->fe[0]);
393 fail_frontend0:
394 	dvb_frontend_detach(adap->fe[0]);
395 	adap->fe[0] = NULL;
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]);
399 
400 	return ret;
401 }
402 
pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter * adap)403 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
404 {
405 	if (adap->fe[1]) {
406 		dvb_unregister_frontend(adap->fe[1]);
407 		dvb_frontend_detach(adap->fe[1]);
408 		adap->fe[1] = NULL;
409 	}
410 	if (adap->fe[0]) {
411 		dvb_unregister_frontend(adap->fe[0]);
412 		dvb_frontend_detach(adap->fe[0]);
413 		adap->fe[0] = NULL;
414 	}
415 
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;
422 
423 	return 0;
424 }
425 
pvr2_dvb_destroy(struct pvr2_dvb_adapter * adap)426 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
427 {
428 	pvr2_dvb_stream_end(adap);
429 	pvr2_dvb_frontend_exit(adap);
430 	pvr2_dvb_adapter_exit(adap);
431 	pvr2_channel_done(&adap->channel);
432 	kfree(adap);
433 }
434 
pvr2_dvb_internal_check(struct pvr2_channel * chp)435 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
436 {
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);
441 }
442 
pvr2_dvb_create(struct pvr2_context * pvr)443 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
444 {
445 	int ret = 0;
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. */
450 		return NULL;
451 	}
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;
462 	return adap;
463 
464 fail2:
465 	pvr2_dvb_adapter_exit(adap);
466 fail1:
467 	pvr2_channel_done(&adap->channel);
468 	return NULL;
469 }
470 
471