Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / pvrusb2 / pvrusb2-dvb.c
blob4b32b214116959a699121ab8a3c930346f313648
1 /*
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.
17 #include <linux/kthread.h>
18 #include <linux/freezer.h>
19 #include <linux/slab.h>
20 #include <linux/mm.h>
21 #include <media/dvbdev.h>
22 #include "pvrusb2-debug.h"
23 #include "pvrusb2-hdw-internal.h"
24 #include "pvrusb2-hdw.h"
25 #include "pvrusb2-io.h"
26 #include "pvrusb2-dvb.h"
28 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
30 static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
32 int ret;
33 unsigned int count;
34 struct pvr2_buffer *bp;
35 struct pvr2_stream *stream;
37 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
38 set_freezable();
40 stream = adap->channel.stream->stream;
42 for (;;) {
43 if (kthread_should_stop()) break;
45 /* Not sure about this... */
46 try_to_freeze();
48 bp = pvr2_stream_get_ready_buffer(stream);
49 if (bp != NULL) {
50 count = pvr2_buffer_get_count(bp);
51 if (count) {
52 dvb_dmx_swfilter(
53 &adap->demux,
54 adap->buffer_storage[
55 pvr2_buffer_get_id(bp)],
56 count);
57 } else {
58 ret = pvr2_buffer_get_status(bp);
59 if (ret < 0) break;
61 ret = pvr2_buffer_queue(bp);
62 if (ret < 0) break;
64 /* Since we know we did something to a buffer,
65 just go back and try again. No point in
66 blocking unless we really ran out of
67 buffers to process. */
68 continue;
72 /* Wait until more buffers become available or we're
73 told not to wait any longer. */
74 ret = wait_event_interruptible(
75 adap->buffer_wait_data,
76 (pvr2_stream_get_ready_count(stream) > 0) ||
77 kthread_should_stop());
78 if (ret < 0) break;
81 /* If we get here and ret is < 0, then an error has occurred.
82 Probably would be a good idea to communicate that to DVB core... */
84 pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
86 return 0;
89 static int pvr2_dvb_feed_thread(void *data)
91 int stat = pvr2_dvb_feed_func(data);
92 /* from videobuf-dvb.c: */
93 while (!kthread_should_stop()) {
94 set_current_state(TASK_INTERRUPTIBLE);
95 schedule();
97 return stat;
100 static void pvr2_dvb_notify(struct pvr2_dvb_adapter *adap)
102 wake_up(&adap->buffer_wait_data);
105 static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
107 unsigned int idx;
108 struct pvr2_stream *stream;
110 if (adap->thread) {
111 kthread_stop(adap->thread);
112 adap->thread = NULL;
115 if (adap->channel.stream) {
116 stream = adap->channel.stream->stream;
117 } else {
118 stream = NULL;
120 if (stream) {
121 pvr2_hdw_set_streaming(adap->channel.hdw, 0);
122 pvr2_stream_set_callback(stream, NULL, NULL);
123 pvr2_stream_kill(stream);
124 pvr2_stream_set_buffer_count(stream, 0);
125 pvr2_channel_claim_stream(&adap->channel, NULL);
128 if (adap->stream_run) {
129 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
130 if (!(adap->buffer_storage[idx])) continue;
131 kfree(adap->buffer_storage[idx]);
132 adap->buffer_storage[idx] = NULL;
134 adap->stream_run = 0;
138 static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
140 struct pvr2_context *pvr = adap->channel.mc_head;
141 unsigned int idx;
142 int ret;
143 struct pvr2_buffer *bp;
144 struct pvr2_stream *stream = NULL;
146 if (adap->stream_run) return -EIO;
148 ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
149 /* somebody else already has the stream */
150 if (ret < 0) return ret;
152 stream = adap->channel.stream->stream;
154 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
155 adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
156 GFP_KERNEL);
157 if (!(adap->buffer_storage[idx])) return -ENOMEM;
160 pvr2_stream_set_callback(pvr->video_stream.stream,
161 (pvr2_stream_callback) pvr2_dvb_notify, adap);
163 ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
164 if (ret < 0) return ret;
166 for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
167 bp = pvr2_stream_get_buffer(stream, idx);
168 pvr2_buffer_set_buffer(bp,
169 adap->buffer_storage[idx],
170 PVR2_DVB_BUFFER_SIZE);
173 ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
174 if (ret < 0) return ret;
176 while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
177 ret = pvr2_buffer_queue(bp);
178 if (ret < 0) return ret;
181 adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
183 if (IS_ERR(adap->thread)) {
184 ret = PTR_ERR(adap->thread);
185 adap->thread = NULL;
186 return ret;
189 adap->stream_run = !0;
191 return 0;
194 static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
196 int ret = pvr2_dvb_stream_do_start(adap);
197 if (ret < 0) pvr2_dvb_stream_end(adap);
198 return ret;
201 static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
203 struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
204 int ret = 0;
206 if (adap == NULL) return -ENODEV;
208 mutex_lock(&adap->lock);
209 do {
210 if (onoff) {
211 if (!adap->feedcount) {
212 pvr2_trace(PVR2_TRACE_DVB_FEED,
213 "start feeding demux");
214 ret = pvr2_dvb_stream_start(adap);
215 if (ret < 0) break;
217 (adap->feedcount)++;
218 } else if (adap->feedcount > 0) {
219 (adap->feedcount)--;
220 if (!adap->feedcount) {
221 pvr2_trace(PVR2_TRACE_DVB_FEED,
222 "stop feeding demux");
223 pvr2_dvb_stream_end(adap);
226 } while (0);
227 mutex_unlock(&adap->lock);
229 return ret;
232 static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
234 pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
235 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
238 static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
240 pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
241 return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
244 static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
246 struct pvr2_dvb_adapter *adap = fe->dvb->priv;
247 return pvr2_channel_limit_inputs(
248 &adap->channel,
249 (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
252 static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
254 int ret;
256 ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
257 THIS_MODULE/*&hdw->usb_dev->owner*/,
258 &adap->channel.hdw->usb_dev->dev,
259 adapter_nr);
260 if (ret < 0) {
261 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
262 "dvb_register_adapter failed: error %d", ret);
263 goto err;
265 adap->dvb_adap.priv = adap;
267 adap->demux.dmx.capabilities = DMX_TS_FILTERING |
268 DMX_SECTION_FILTERING |
269 DMX_MEMORY_BASED_FILTERING;
270 adap->demux.priv = adap;
271 adap->demux.filternum = 256;
272 adap->demux.feednum = 256;
273 adap->demux.start_feed = pvr2_dvb_start_feed;
274 adap->demux.stop_feed = pvr2_dvb_stop_feed;
275 adap->demux.write_to_decoder = NULL;
277 ret = dvb_dmx_init(&adap->demux);
278 if (ret < 0) {
279 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
280 "dvb_dmx_init failed: error %d", ret);
281 goto err_dmx;
284 adap->dmxdev.filternum = adap->demux.filternum;
285 adap->dmxdev.demux = &adap->demux.dmx;
286 adap->dmxdev.capabilities = 0;
288 ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
289 if (ret < 0) {
290 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
291 "dvb_dmxdev_init failed: error %d", ret);
292 goto err_dmx_dev;
295 dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
297 return 0;
299 err_dmx_dev:
300 dvb_dmx_release(&adap->demux);
301 err_dmx:
302 dvb_unregister_adapter(&adap->dvb_adap);
303 err:
304 return ret;
307 static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
309 pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
310 dvb_net_release(&adap->dvb_net);
311 adap->demux.dmx.close(&adap->demux.dmx);
312 dvb_dmxdev_release(&adap->dmxdev);
313 dvb_dmx_release(&adap->demux);
314 dvb_unregister_adapter(&adap->dvb_adap);
315 return 0;
318 static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
320 struct pvr2_hdw *hdw = adap->channel.hdw;
321 const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
322 int ret = 0;
324 if (dvb_props == NULL) {
325 pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
326 return -EINVAL;
329 ret = pvr2_channel_limit_inputs(
330 &adap->channel,
331 (1 << PVR2_CVAL_INPUT_DTV));
332 if (ret) {
333 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
334 "failed to grab control of dtv input (code=%d)",
335 ret);
336 return ret;
339 if (dvb_props->frontend_attach == NULL) {
340 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
341 "frontend_attach not defined!");
342 ret = -EINVAL;
343 goto done;
346 if ((dvb_props->frontend_attach(adap) == 0) && (adap->fe)) {
348 if (dvb_register_frontend(&adap->dvb_adap, adap->fe)) {
349 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
350 "frontend registration failed!");
351 dvb_frontend_detach(adap->fe);
352 adap->fe = NULL;
353 ret = -ENODEV;
354 goto done;
357 if (dvb_props->tuner_attach)
358 dvb_props->tuner_attach(adap);
360 if (adap->fe->ops.analog_ops.standby)
361 adap->fe->ops.analog_ops.standby(adap->fe);
363 /* Ensure all frontends negotiate bus access */
364 adap->fe->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
366 } else {
367 pvr2_trace(PVR2_TRACE_ERROR_LEGS,
368 "no frontend was attached!");
369 ret = -ENODEV;
370 return ret;
373 done:
374 pvr2_channel_limit_inputs(&adap->channel, 0);
375 return ret;
378 static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
380 if (adap->fe != NULL) {
381 dvb_unregister_frontend(adap->fe);
382 dvb_frontend_detach(adap->fe);
384 return 0;
387 static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
389 pvr2_dvb_stream_end(adap);
390 pvr2_dvb_frontend_exit(adap);
391 pvr2_dvb_adapter_exit(adap);
392 pvr2_channel_done(&adap->channel);
393 kfree(adap);
396 static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
398 struct pvr2_dvb_adapter *adap;
399 adap = container_of(chp, struct pvr2_dvb_adapter, channel);
400 if (!adap->channel.mc_head->disconnect_flag) return;
401 pvr2_dvb_destroy(adap);
404 struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
406 int ret = 0;
407 struct pvr2_dvb_adapter *adap;
408 if (!pvr->hdw->hdw_desc->dvb_props) {
409 /* Device lacks a digital interface so don't set up
410 the DVB side of the driver either. For now. */
411 return NULL;
413 adap = kzalloc(sizeof(*adap), GFP_KERNEL);
414 if (!adap) return adap;
415 pvr2_channel_init(&adap->channel, pvr);
416 adap->channel.check_func = pvr2_dvb_internal_check;
417 init_waitqueue_head(&adap->buffer_wait_data);
418 mutex_init(&adap->lock);
419 ret = pvr2_dvb_adapter_init(adap);
420 if (ret < 0) goto fail1;
421 ret = pvr2_dvb_frontend_init(adap);
422 if (ret < 0) goto fail2;
423 return adap;
425 fail2:
426 pvr2_dvb_adapter_exit(adap);
427 fail1:
428 pvr2_channel_done(&adap->channel);
429 return NULL;