12504ba9fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20c0d06caSMauro Carvalho Chehab /*
30c0d06caSMauro Carvalho Chehab * pvrusb2-dvb.c - linux-dvb api interface to the pvrusb2 driver.
40c0d06caSMauro Carvalho Chehab *
50c0d06caSMauro Carvalho Chehab * Copyright (C) 2007, 2008 Michael Krufky <mkrufky@linuxtv.org>
60c0d06caSMauro Carvalho Chehab */
70c0d06caSMauro Carvalho Chehab
80c0d06caSMauro Carvalho Chehab #include <linux/kthread.h>
90c0d06caSMauro Carvalho Chehab #include <linux/freezer.h>
100c0d06caSMauro Carvalho Chehab #include <linux/slab.h>
110c0d06caSMauro Carvalho Chehab #include <linux/mm.h>
12fada1935SMauro Carvalho Chehab #include <media/dvbdev.h>
130c0d06caSMauro Carvalho Chehab #include "pvrusb2-debug.h"
140c0d06caSMauro Carvalho Chehab #include "pvrusb2-hdw-internal.h"
150c0d06caSMauro Carvalho Chehab #include "pvrusb2-hdw.h"
160c0d06caSMauro Carvalho Chehab #include "pvrusb2-io.h"
170c0d06caSMauro Carvalho Chehab #include "pvrusb2-dvb.h"
180c0d06caSMauro Carvalho Chehab
190c0d06caSMauro Carvalho Chehab DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
200c0d06caSMauro Carvalho Chehab
pvr2_dvb_feed_func(struct pvr2_dvb_adapter * adap)210c0d06caSMauro Carvalho Chehab static int pvr2_dvb_feed_func(struct pvr2_dvb_adapter *adap)
220c0d06caSMauro Carvalho Chehab {
230c0d06caSMauro Carvalho Chehab int ret;
240c0d06caSMauro Carvalho Chehab unsigned int count;
250c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
260c0d06caSMauro Carvalho Chehab struct pvr2_stream *stream;
270c0d06caSMauro Carvalho Chehab
280c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread started");
290c0d06caSMauro Carvalho Chehab set_freezable();
300c0d06caSMauro Carvalho Chehab
310c0d06caSMauro Carvalho Chehab stream = adap->channel.stream->stream;
320c0d06caSMauro Carvalho Chehab
330c0d06caSMauro Carvalho Chehab for (;;) {
340c0d06caSMauro Carvalho Chehab if (kthread_should_stop()) break;
350c0d06caSMauro Carvalho Chehab
360c0d06caSMauro Carvalho Chehab /* Not sure about this... */
370c0d06caSMauro Carvalho Chehab try_to_freeze();
380c0d06caSMauro Carvalho Chehab
390c0d06caSMauro Carvalho Chehab bp = pvr2_stream_get_ready_buffer(stream);
400c0d06caSMauro Carvalho Chehab if (bp != NULL) {
410c0d06caSMauro Carvalho Chehab count = pvr2_buffer_get_count(bp);
420c0d06caSMauro Carvalho Chehab if (count) {
430c0d06caSMauro Carvalho Chehab dvb_dmx_swfilter(
440c0d06caSMauro Carvalho Chehab &adap->demux,
450c0d06caSMauro Carvalho Chehab adap->buffer_storage[
460c0d06caSMauro Carvalho Chehab pvr2_buffer_get_id(bp)],
470c0d06caSMauro Carvalho Chehab count);
480c0d06caSMauro Carvalho Chehab } else {
490c0d06caSMauro Carvalho Chehab ret = pvr2_buffer_get_status(bp);
500c0d06caSMauro Carvalho Chehab if (ret < 0) break;
510c0d06caSMauro Carvalho Chehab }
520c0d06caSMauro Carvalho Chehab ret = pvr2_buffer_queue(bp);
530c0d06caSMauro Carvalho Chehab if (ret < 0) break;
540c0d06caSMauro Carvalho Chehab
550c0d06caSMauro Carvalho Chehab /* Since we know we did something to a buffer,
560c0d06caSMauro Carvalho Chehab just go back and try again. No point in
570c0d06caSMauro Carvalho Chehab blocking unless we really ran out of
580c0d06caSMauro Carvalho Chehab buffers to process. */
590c0d06caSMauro Carvalho Chehab continue;
600c0d06caSMauro Carvalho Chehab }
610c0d06caSMauro Carvalho Chehab
620c0d06caSMauro Carvalho Chehab
630c0d06caSMauro Carvalho Chehab /* Wait until more buffers become available or we're
640c0d06caSMauro Carvalho Chehab told not to wait any longer. */
650c0d06caSMauro Carvalho Chehab ret = wait_event_interruptible(
660c0d06caSMauro Carvalho Chehab adap->buffer_wait_data,
670c0d06caSMauro Carvalho Chehab (pvr2_stream_get_ready_count(stream) > 0) ||
680c0d06caSMauro Carvalho Chehab kthread_should_stop());
690c0d06caSMauro Carvalho Chehab if (ret < 0) break;
700c0d06caSMauro Carvalho Chehab }
710c0d06caSMauro Carvalho Chehab
720c0d06caSMauro Carvalho Chehab /* If we get here and ret is < 0, then an error has occurred.
730c0d06caSMauro Carvalho Chehab Probably would be a good idea to communicate that to DVB core... */
740c0d06caSMauro Carvalho Chehab
750c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED, "dvb feed thread stopped");
760c0d06caSMauro Carvalho Chehab
770c0d06caSMauro Carvalho Chehab return 0;
780c0d06caSMauro Carvalho Chehab }
790c0d06caSMauro Carvalho Chehab
pvr2_dvb_feed_thread(void * data)800c0d06caSMauro Carvalho Chehab static int pvr2_dvb_feed_thread(void *data)
810c0d06caSMauro Carvalho Chehab {
820c0d06caSMauro Carvalho Chehab int stat = pvr2_dvb_feed_func(data);
83f068a6ceSHans Verkuil
840c0d06caSMauro Carvalho Chehab while (!kthread_should_stop()) {
850c0d06caSMauro Carvalho Chehab set_current_state(TASK_INTERRUPTIBLE);
860c0d06caSMauro Carvalho Chehab schedule();
870c0d06caSMauro Carvalho Chehab }
880c0d06caSMauro Carvalho Chehab return stat;
890c0d06caSMauro Carvalho Chehab }
900c0d06caSMauro Carvalho Chehab
pvr2_dvb_notify(void * ptr)91*5b8d21f7SArnd Bergmann static void pvr2_dvb_notify(void *ptr)
920c0d06caSMauro Carvalho Chehab {
93*5b8d21f7SArnd Bergmann struct pvr2_dvb_adapter *adap = ptr;
94*5b8d21f7SArnd Bergmann
950c0d06caSMauro Carvalho Chehab wake_up(&adap->buffer_wait_data);
960c0d06caSMauro Carvalho Chehab }
970c0d06caSMauro Carvalho Chehab
pvr2_dvb_stream_end(struct pvr2_dvb_adapter * adap)980c0d06caSMauro Carvalho Chehab static void pvr2_dvb_stream_end(struct pvr2_dvb_adapter *adap)
990c0d06caSMauro Carvalho Chehab {
1000c0d06caSMauro Carvalho Chehab unsigned int idx;
1010c0d06caSMauro Carvalho Chehab struct pvr2_stream *stream;
1020c0d06caSMauro Carvalho Chehab
1030c0d06caSMauro Carvalho Chehab if (adap->thread) {
1040c0d06caSMauro Carvalho Chehab kthread_stop(adap->thread);
1050c0d06caSMauro Carvalho Chehab adap->thread = NULL;
1060c0d06caSMauro Carvalho Chehab }
1070c0d06caSMauro Carvalho Chehab
1080c0d06caSMauro Carvalho Chehab if (adap->channel.stream) {
1090c0d06caSMauro Carvalho Chehab stream = adap->channel.stream->stream;
1100c0d06caSMauro Carvalho Chehab } else {
1110c0d06caSMauro Carvalho Chehab stream = NULL;
1120c0d06caSMauro Carvalho Chehab }
1130c0d06caSMauro Carvalho Chehab if (stream) {
1140c0d06caSMauro Carvalho Chehab pvr2_hdw_set_streaming(adap->channel.hdw, 0);
1150c0d06caSMauro Carvalho Chehab pvr2_stream_set_callback(stream, NULL, NULL);
1160c0d06caSMauro Carvalho Chehab pvr2_stream_kill(stream);
1170c0d06caSMauro Carvalho Chehab pvr2_stream_set_buffer_count(stream, 0);
1180c0d06caSMauro Carvalho Chehab pvr2_channel_claim_stream(&adap->channel, NULL);
1190c0d06caSMauro Carvalho Chehab }
1200c0d06caSMauro Carvalho Chehab
1210c0d06caSMauro Carvalho Chehab if (adap->stream_run) {
1220c0d06caSMauro Carvalho Chehab for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
1230c0d06caSMauro Carvalho Chehab if (!(adap->buffer_storage[idx])) continue;
1240c0d06caSMauro Carvalho Chehab kfree(adap->buffer_storage[idx]);
1250c0d06caSMauro Carvalho Chehab adap->buffer_storage[idx] = NULL;
1260c0d06caSMauro Carvalho Chehab }
1270c0d06caSMauro Carvalho Chehab adap->stream_run = 0;
1280c0d06caSMauro Carvalho Chehab }
1290c0d06caSMauro Carvalho Chehab }
1300c0d06caSMauro Carvalho Chehab
pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter * adap)1310c0d06caSMauro Carvalho Chehab static int pvr2_dvb_stream_do_start(struct pvr2_dvb_adapter *adap)
1320c0d06caSMauro Carvalho Chehab {
1330c0d06caSMauro Carvalho Chehab struct pvr2_context *pvr = adap->channel.mc_head;
1340c0d06caSMauro Carvalho Chehab unsigned int idx;
1350c0d06caSMauro Carvalho Chehab int ret;
1360c0d06caSMauro Carvalho Chehab struct pvr2_buffer *bp;
1370c0d06caSMauro Carvalho Chehab struct pvr2_stream *stream = NULL;
1380c0d06caSMauro Carvalho Chehab
1390c0d06caSMauro Carvalho Chehab if (adap->stream_run) return -EIO;
1400c0d06caSMauro Carvalho Chehab
1410c0d06caSMauro Carvalho Chehab ret = pvr2_channel_claim_stream(&adap->channel, &pvr->video_stream);
1420c0d06caSMauro Carvalho Chehab /* somebody else already has the stream */
1430c0d06caSMauro Carvalho Chehab if (ret < 0) return ret;
1440c0d06caSMauro Carvalho Chehab
1450c0d06caSMauro Carvalho Chehab stream = adap->channel.stream->stream;
1460c0d06caSMauro Carvalho Chehab
1470c0d06caSMauro Carvalho Chehab for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
1480c0d06caSMauro Carvalho Chehab adap->buffer_storage[idx] = kmalloc(PVR2_DVB_BUFFER_SIZE,
1490c0d06caSMauro Carvalho Chehab GFP_KERNEL);
1500c0d06caSMauro Carvalho Chehab if (!(adap->buffer_storage[idx])) return -ENOMEM;
1510c0d06caSMauro Carvalho Chehab }
1520c0d06caSMauro Carvalho Chehab
1530c0d06caSMauro Carvalho Chehab pvr2_stream_set_callback(pvr->video_stream.stream,
154*5b8d21f7SArnd Bergmann pvr2_dvb_notify, adap);
1550c0d06caSMauro Carvalho Chehab
1560c0d06caSMauro Carvalho Chehab ret = pvr2_stream_set_buffer_count(stream, PVR2_DVB_BUFFER_COUNT);
1570c0d06caSMauro Carvalho Chehab if (ret < 0) return ret;
1580c0d06caSMauro Carvalho Chehab
1590c0d06caSMauro Carvalho Chehab for (idx = 0; idx < PVR2_DVB_BUFFER_COUNT; idx++) {
1600c0d06caSMauro Carvalho Chehab bp = pvr2_stream_get_buffer(stream, idx);
1610c0d06caSMauro Carvalho Chehab pvr2_buffer_set_buffer(bp,
1620c0d06caSMauro Carvalho Chehab adap->buffer_storage[idx],
1630c0d06caSMauro Carvalho Chehab PVR2_DVB_BUFFER_SIZE);
1640c0d06caSMauro Carvalho Chehab }
1650c0d06caSMauro Carvalho Chehab
1660c0d06caSMauro Carvalho Chehab ret = pvr2_hdw_set_streaming(adap->channel.hdw, 1);
1670c0d06caSMauro Carvalho Chehab if (ret < 0) return ret;
1680c0d06caSMauro Carvalho Chehab
1690c0d06caSMauro Carvalho Chehab while ((bp = pvr2_stream_get_idle_buffer(stream)) != NULL) {
1700c0d06caSMauro Carvalho Chehab ret = pvr2_buffer_queue(bp);
1710c0d06caSMauro Carvalho Chehab if (ret < 0) return ret;
1720c0d06caSMauro Carvalho Chehab }
1730c0d06caSMauro Carvalho Chehab
1740c0d06caSMauro Carvalho Chehab adap->thread = kthread_run(pvr2_dvb_feed_thread, adap, "pvrusb2-dvb");
1750c0d06caSMauro Carvalho Chehab
1760c0d06caSMauro Carvalho Chehab if (IS_ERR(adap->thread)) {
1770c0d06caSMauro Carvalho Chehab ret = PTR_ERR(adap->thread);
1780c0d06caSMauro Carvalho Chehab adap->thread = NULL;
1790c0d06caSMauro Carvalho Chehab return ret;
1800c0d06caSMauro Carvalho Chehab }
1810c0d06caSMauro Carvalho Chehab
1820c0d06caSMauro Carvalho Chehab adap->stream_run = !0;
1830c0d06caSMauro Carvalho Chehab
1840c0d06caSMauro Carvalho Chehab return 0;
1850c0d06caSMauro Carvalho Chehab }
1860c0d06caSMauro Carvalho Chehab
pvr2_dvb_stream_start(struct pvr2_dvb_adapter * adap)1870c0d06caSMauro Carvalho Chehab static int pvr2_dvb_stream_start(struct pvr2_dvb_adapter *adap)
1880c0d06caSMauro Carvalho Chehab {
1890c0d06caSMauro Carvalho Chehab int ret = pvr2_dvb_stream_do_start(adap);
1900c0d06caSMauro Carvalho Chehab if (ret < 0) pvr2_dvb_stream_end(adap);
1910c0d06caSMauro Carvalho Chehab return ret;
1920c0d06caSMauro Carvalho Chehab }
1930c0d06caSMauro Carvalho Chehab
pvr2_dvb_ctrl_feed(struct dvb_demux_feed * dvbdmxfeed,int onoff)1940c0d06caSMauro Carvalho Chehab static int pvr2_dvb_ctrl_feed(struct dvb_demux_feed *dvbdmxfeed, int onoff)
1950c0d06caSMauro Carvalho Chehab {
1960c0d06caSMauro Carvalho Chehab struct pvr2_dvb_adapter *adap = dvbdmxfeed->demux->priv;
1970c0d06caSMauro Carvalho Chehab int ret = 0;
1980c0d06caSMauro Carvalho Chehab
1990c0d06caSMauro Carvalho Chehab if (adap == NULL) return -ENODEV;
2000c0d06caSMauro Carvalho Chehab
2010c0d06caSMauro Carvalho Chehab mutex_lock(&adap->lock);
2020c0d06caSMauro Carvalho Chehab do {
2030c0d06caSMauro Carvalho Chehab if (onoff) {
2040c0d06caSMauro Carvalho Chehab if (!adap->feedcount) {
2050c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED,
2060c0d06caSMauro Carvalho Chehab "start feeding demux");
2070c0d06caSMauro Carvalho Chehab ret = pvr2_dvb_stream_start(adap);
2080c0d06caSMauro Carvalho Chehab if (ret < 0) break;
2090c0d06caSMauro Carvalho Chehab }
2100c0d06caSMauro Carvalho Chehab (adap->feedcount)++;
2110c0d06caSMauro Carvalho Chehab } else if (adap->feedcount > 0) {
2120c0d06caSMauro Carvalho Chehab (adap->feedcount)--;
2130c0d06caSMauro Carvalho Chehab if (!adap->feedcount) {
2140c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED,
2150c0d06caSMauro Carvalho Chehab "stop feeding demux");
2160c0d06caSMauro Carvalho Chehab pvr2_dvb_stream_end(adap);
2170c0d06caSMauro Carvalho Chehab }
2180c0d06caSMauro Carvalho Chehab }
2190c0d06caSMauro Carvalho Chehab } while (0);
2200c0d06caSMauro Carvalho Chehab mutex_unlock(&adap->lock);
2210c0d06caSMauro Carvalho Chehab
2220c0d06caSMauro Carvalho Chehab return ret;
2230c0d06caSMauro Carvalho Chehab }
2240c0d06caSMauro Carvalho Chehab
pvr2_dvb_start_feed(struct dvb_demux_feed * dvbdmxfeed)2250c0d06caSMauro Carvalho Chehab static int pvr2_dvb_start_feed(struct dvb_demux_feed *dvbdmxfeed)
2260c0d06caSMauro Carvalho Chehab {
2270c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED, "start pid: 0x%04x", dvbdmxfeed->pid);
2280c0d06caSMauro Carvalho Chehab return pvr2_dvb_ctrl_feed(dvbdmxfeed, 1);
2290c0d06caSMauro Carvalho Chehab }
2300c0d06caSMauro Carvalho Chehab
pvr2_dvb_stop_feed(struct dvb_demux_feed * dvbdmxfeed)2310c0d06caSMauro Carvalho Chehab static int pvr2_dvb_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
2320c0d06caSMauro Carvalho Chehab {
2330c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_DVB_FEED, "stop pid: 0x%04x", dvbdmxfeed->pid);
2340c0d06caSMauro Carvalho Chehab return pvr2_dvb_ctrl_feed(dvbdmxfeed, 0);
2350c0d06caSMauro Carvalho Chehab }
2360c0d06caSMauro Carvalho Chehab
pvr2_dvb_bus_ctrl(struct dvb_frontend * fe,int acquire)2370c0d06caSMauro Carvalho Chehab static int pvr2_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
2380c0d06caSMauro Carvalho Chehab {
2390c0d06caSMauro Carvalho Chehab struct pvr2_dvb_adapter *adap = fe->dvb->priv;
2400c0d06caSMauro Carvalho Chehab return pvr2_channel_limit_inputs(
2410c0d06caSMauro Carvalho Chehab &adap->channel,
2420c0d06caSMauro Carvalho Chehab (acquire ? (1 << PVR2_CVAL_INPUT_DTV) : 0));
2430c0d06caSMauro Carvalho Chehab }
2440c0d06caSMauro Carvalho Chehab
pvr2_dvb_adapter_init(struct pvr2_dvb_adapter * adap)2450c0d06caSMauro Carvalho Chehab static int pvr2_dvb_adapter_init(struct pvr2_dvb_adapter *adap)
2460c0d06caSMauro Carvalho Chehab {
2470c0d06caSMauro Carvalho Chehab int ret;
2480c0d06caSMauro Carvalho Chehab
2490c0d06caSMauro Carvalho Chehab ret = dvb_register_adapter(&adap->dvb_adap, "pvrusb2-dvb",
2500c0d06caSMauro Carvalho Chehab THIS_MODULE/*&hdw->usb_dev->owner*/,
2510c0d06caSMauro Carvalho Chehab &adap->channel.hdw->usb_dev->dev,
2520c0d06caSMauro Carvalho Chehab adapter_nr);
2530c0d06caSMauro Carvalho Chehab if (ret < 0) {
2540c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2550c0d06caSMauro Carvalho Chehab "dvb_register_adapter failed: error %d", ret);
2560c0d06caSMauro Carvalho Chehab goto err;
2570c0d06caSMauro Carvalho Chehab }
2580c0d06caSMauro Carvalho Chehab adap->dvb_adap.priv = adap;
2590c0d06caSMauro Carvalho Chehab
2600c0d06caSMauro Carvalho Chehab adap->demux.dmx.capabilities = DMX_TS_FILTERING |
2610c0d06caSMauro Carvalho Chehab DMX_SECTION_FILTERING |
2620c0d06caSMauro Carvalho Chehab DMX_MEMORY_BASED_FILTERING;
2630c0d06caSMauro Carvalho Chehab adap->demux.priv = adap;
2640c0d06caSMauro Carvalho Chehab adap->demux.filternum = 256;
2650c0d06caSMauro Carvalho Chehab adap->demux.feednum = 256;
2660c0d06caSMauro Carvalho Chehab adap->demux.start_feed = pvr2_dvb_start_feed;
2670c0d06caSMauro Carvalho Chehab adap->demux.stop_feed = pvr2_dvb_stop_feed;
2680c0d06caSMauro Carvalho Chehab adap->demux.write_to_decoder = NULL;
2690c0d06caSMauro Carvalho Chehab
2700c0d06caSMauro Carvalho Chehab ret = dvb_dmx_init(&adap->demux);
2710c0d06caSMauro Carvalho Chehab if (ret < 0) {
2720c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2730c0d06caSMauro Carvalho Chehab "dvb_dmx_init failed: error %d", ret);
2740c0d06caSMauro Carvalho Chehab goto err_dmx;
2750c0d06caSMauro Carvalho Chehab }
2760c0d06caSMauro Carvalho Chehab
2770c0d06caSMauro Carvalho Chehab adap->dmxdev.filternum = adap->demux.filternum;
2780c0d06caSMauro Carvalho Chehab adap->dmxdev.demux = &adap->demux.dmx;
2790c0d06caSMauro Carvalho Chehab adap->dmxdev.capabilities = 0;
2800c0d06caSMauro Carvalho Chehab
2810c0d06caSMauro Carvalho Chehab ret = dvb_dmxdev_init(&adap->dmxdev, &adap->dvb_adap);
2820c0d06caSMauro Carvalho Chehab if (ret < 0) {
2830c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
2840c0d06caSMauro Carvalho Chehab "dvb_dmxdev_init failed: error %d", ret);
2850c0d06caSMauro Carvalho Chehab goto err_dmx_dev;
2860c0d06caSMauro Carvalho Chehab }
2870c0d06caSMauro Carvalho Chehab
2880c0d06caSMauro Carvalho Chehab dvb_net_init(&adap->dvb_adap, &adap->dvb_net, &adap->demux.dmx);
2890c0d06caSMauro Carvalho Chehab
2900c0d06caSMauro Carvalho Chehab return 0;
2910c0d06caSMauro Carvalho Chehab
2920c0d06caSMauro Carvalho Chehab err_dmx_dev:
2930c0d06caSMauro Carvalho Chehab dvb_dmx_release(&adap->demux);
2940c0d06caSMauro Carvalho Chehab err_dmx:
2950c0d06caSMauro Carvalho Chehab dvb_unregister_adapter(&adap->dvb_adap);
2960c0d06caSMauro Carvalho Chehab err:
2970c0d06caSMauro Carvalho Chehab return ret;
2980c0d06caSMauro Carvalho Chehab }
2990c0d06caSMauro Carvalho Chehab
pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter * adap)3000c0d06caSMauro Carvalho Chehab static int pvr2_dvb_adapter_exit(struct pvr2_dvb_adapter *adap)
3010c0d06caSMauro Carvalho Chehab {
3020c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_INFO, "unregistering DVB devices");
3030c0d06caSMauro Carvalho Chehab dvb_net_release(&adap->dvb_net);
3040c0d06caSMauro Carvalho Chehab adap->demux.dmx.close(&adap->demux.dmx);
3050c0d06caSMauro Carvalho Chehab dvb_dmxdev_release(&adap->dmxdev);
3060c0d06caSMauro Carvalho Chehab dvb_dmx_release(&adap->demux);
3070c0d06caSMauro Carvalho Chehab dvb_unregister_adapter(&adap->dvb_adap);
3080c0d06caSMauro Carvalho Chehab return 0;
3090c0d06caSMauro Carvalho Chehab }
3100c0d06caSMauro Carvalho Chehab
pvr2_dvb_frontend_init(struct pvr2_dvb_adapter * adap)3110c0d06caSMauro Carvalho Chehab static int pvr2_dvb_frontend_init(struct pvr2_dvb_adapter *adap)
3120c0d06caSMauro Carvalho Chehab {
3130c0d06caSMauro Carvalho Chehab struct pvr2_hdw *hdw = adap->channel.hdw;
3140c0d06caSMauro Carvalho Chehab const struct pvr2_dvb_props *dvb_props = hdw->hdw_desc->dvb_props;
3150c0d06caSMauro Carvalho Chehab int ret = 0;
3160c0d06caSMauro Carvalho Chehab
3170c0d06caSMauro Carvalho Chehab if (dvb_props == NULL) {
3180c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS, "fe_props not defined!");
3190c0d06caSMauro Carvalho Chehab return -EINVAL;
3200c0d06caSMauro Carvalho Chehab }
3210c0d06caSMauro Carvalho Chehab
3220c0d06caSMauro Carvalho Chehab ret = pvr2_channel_limit_inputs(
3230c0d06caSMauro Carvalho Chehab &adap->channel,
3240c0d06caSMauro Carvalho Chehab (1 << PVR2_CVAL_INPUT_DTV));
3250c0d06caSMauro Carvalho Chehab if (ret) {
3260c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3270c0d06caSMauro Carvalho Chehab "failed to grab control of dtv input (code=%d)",
3280c0d06caSMauro Carvalho Chehab ret);
3290c0d06caSMauro Carvalho Chehab return ret;
3300c0d06caSMauro Carvalho Chehab }
3310c0d06caSMauro Carvalho Chehab
3320c0d06caSMauro Carvalho Chehab if (dvb_props->frontend_attach == NULL) {
3330c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3340c0d06caSMauro Carvalho Chehab "frontend_attach not defined!");
3350c0d06caSMauro Carvalho Chehab ret = -EINVAL;
3360c0d06caSMauro Carvalho Chehab goto done;
3370c0d06caSMauro Carvalho Chehab }
3380c0d06caSMauro Carvalho Chehab
339cac821d2SBrad Love if (dvb_props->frontend_attach(adap) == 0 && adap->fe[0]) {
340cac821d2SBrad Love if (dvb_register_frontend(&adap->dvb_adap, adap->fe[0])) {
3410c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3420c0d06caSMauro Carvalho Chehab "frontend registration failed!");
3430c0d06caSMauro Carvalho Chehab ret = -ENODEV;
344cac821d2SBrad Love goto fail_frontend0;
3450c0d06caSMauro Carvalho Chehab }
346cac821d2SBrad Love if (adap->fe[0]->ops.analog_ops.standby)
347cac821d2SBrad Love adap->fe[0]->ops.analog_ops.standby(adap->fe[0]);
3480c0d06caSMauro Carvalho Chehab
349cac821d2SBrad Love pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
350cac821d2SBrad Love adap->fe[0]->id);
351cac821d2SBrad Love adap->fe[0]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
3520c0d06caSMauro Carvalho Chehab } else {
3530c0d06caSMauro Carvalho Chehab pvr2_trace(PVR2_TRACE_ERROR_LEGS,
3540c0d06caSMauro Carvalho Chehab "no frontend was attached!");
3550c0d06caSMauro Carvalho Chehab ret = -ENODEV;
3560c0d06caSMauro Carvalho Chehab return ret;
3570c0d06caSMauro Carvalho Chehab }
3580c0d06caSMauro Carvalho Chehab
359cac821d2SBrad Love if (dvb_props->tuner_attach && dvb_props->tuner_attach(adap)) {
360cac821d2SBrad Love pvr2_trace(PVR2_TRACE_ERROR_LEGS, "tuner attach failed");
361cac821d2SBrad Love ret = -ENODEV;
362cac821d2SBrad Love goto fail_tuner;
363cac821d2SBrad Love }
364cac821d2SBrad Love
365cac821d2SBrad Love if (adap->fe[1]) {
366cac821d2SBrad Love adap->fe[1]->id = 1;
367cac821d2SBrad Love adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
368cac821d2SBrad Love memcpy(&adap->fe[1]->ops.tuner_ops,
369cac821d2SBrad Love &adap->fe[0]->ops.tuner_ops,
370cac821d2SBrad Love sizeof(struct dvb_tuner_ops));
371cac821d2SBrad Love
372cac821d2SBrad Love if (dvb_register_frontend(&adap->dvb_adap, adap->fe[1])) {
373cac821d2SBrad Love pvr2_trace(PVR2_TRACE_ERROR_LEGS,
374cac821d2SBrad Love "frontend registration failed!");
375cac821d2SBrad Love ret = -ENODEV;
376cac821d2SBrad Love goto fail_frontend1;
377cac821d2SBrad Love }
378cac821d2SBrad Love /* MFE lock */
379cac821d2SBrad Love adap->dvb_adap.mfe_shared = 1;
380cac821d2SBrad Love
381cac821d2SBrad Love if (adap->fe[1]->ops.analog_ops.standby)
382cac821d2SBrad Love adap->fe[1]->ops.analog_ops.standby(adap->fe[1]);
383cac821d2SBrad Love
384cac821d2SBrad Love pvr2_trace(PVR2_TRACE_INFO, "transferring fe[%d] ts_bus_ctrl() to pvr2_dvb_bus_ctrl()",
385cac821d2SBrad Love adap->fe[1]->id);
386cac821d2SBrad Love adap->fe[1]->ops.ts_bus_ctrl = pvr2_dvb_bus_ctrl;
387cac821d2SBrad Love }
3880c0d06caSMauro Carvalho Chehab done:
3890c0d06caSMauro Carvalho Chehab pvr2_channel_limit_inputs(&adap->channel, 0);
3900c0d06caSMauro Carvalho Chehab return ret;
391cac821d2SBrad Love
392cac821d2SBrad Love fail_frontend1:
393cac821d2SBrad Love dvb_frontend_detach(adap->fe[1]);
394cac821d2SBrad Love adap->fe[1] = NULL;
395cac821d2SBrad Love fail_tuner:
396cac821d2SBrad Love dvb_unregister_frontend(adap->fe[0]);
397cac821d2SBrad Love fail_frontend0:
398cac821d2SBrad Love dvb_frontend_detach(adap->fe[0]);
399cac821d2SBrad Love adap->fe[0] = NULL;
4006f6be371SBrad Love dvb_module_release(adap->i2c_client_tuner);
4016f6be371SBrad Love dvb_module_release(adap->i2c_client_demod[1]);
4026f6be371SBrad Love dvb_module_release(adap->i2c_client_demod[0]);
403cac821d2SBrad Love
404cac821d2SBrad Love return ret;
4050c0d06caSMauro Carvalho Chehab }
4060c0d06caSMauro Carvalho Chehab
pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter * adap)4070c0d06caSMauro Carvalho Chehab static int pvr2_dvb_frontend_exit(struct pvr2_dvb_adapter *adap)
4080c0d06caSMauro Carvalho Chehab {
409cac821d2SBrad Love if (adap->fe[1]) {
410cac821d2SBrad Love dvb_unregister_frontend(adap->fe[1]);
411cac821d2SBrad Love dvb_frontend_detach(adap->fe[1]);
412cac821d2SBrad Love adap->fe[1] = NULL;
4130c0d06caSMauro Carvalho Chehab }
414cac821d2SBrad Love if (adap->fe[0]) {
415cac821d2SBrad Love dvb_unregister_frontend(adap->fe[0]);
416cac821d2SBrad Love dvb_frontend_detach(adap->fe[0]);
417cac821d2SBrad Love adap->fe[0] = NULL;
4180c0d06caSMauro Carvalho Chehab }
4196f6be371SBrad Love
4206f6be371SBrad Love dvb_module_release(adap->i2c_client_tuner);
4216f6be371SBrad Love adap->i2c_client_tuner = NULL;
4226f6be371SBrad Love dvb_module_release(adap->i2c_client_demod[1]);
4236f6be371SBrad Love adap->i2c_client_demod[1] = NULL;
4246f6be371SBrad Love dvb_module_release(adap->i2c_client_demod[0]);
4256f6be371SBrad Love adap->i2c_client_demod[0] = NULL;
4266f6be371SBrad Love
4270c0d06caSMauro Carvalho Chehab return 0;
4280c0d06caSMauro Carvalho Chehab }
4290c0d06caSMauro Carvalho Chehab
pvr2_dvb_destroy(struct pvr2_dvb_adapter * adap)4300c0d06caSMauro Carvalho Chehab static void pvr2_dvb_destroy(struct pvr2_dvb_adapter *adap)
4310c0d06caSMauro Carvalho Chehab {
4320c0d06caSMauro Carvalho Chehab pvr2_dvb_stream_end(adap);
4330c0d06caSMauro Carvalho Chehab pvr2_dvb_frontend_exit(adap);
4340c0d06caSMauro Carvalho Chehab pvr2_dvb_adapter_exit(adap);
4350c0d06caSMauro Carvalho Chehab pvr2_channel_done(&adap->channel);
4360c0d06caSMauro Carvalho Chehab kfree(adap);
4370c0d06caSMauro Carvalho Chehab }
4380c0d06caSMauro Carvalho Chehab
pvr2_dvb_internal_check(struct pvr2_channel * chp)4390c0d06caSMauro Carvalho Chehab static void pvr2_dvb_internal_check(struct pvr2_channel *chp)
4400c0d06caSMauro Carvalho Chehab {
4410c0d06caSMauro Carvalho Chehab struct pvr2_dvb_adapter *adap;
4420c0d06caSMauro Carvalho Chehab adap = container_of(chp, struct pvr2_dvb_adapter, channel);
4430c0d06caSMauro Carvalho Chehab if (!adap->channel.mc_head->disconnect_flag) return;
4440c0d06caSMauro Carvalho Chehab pvr2_dvb_destroy(adap);
4450c0d06caSMauro Carvalho Chehab }
4460c0d06caSMauro Carvalho Chehab
pvr2_dvb_create(struct pvr2_context * pvr)4470c0d06caSMauro Carvalho Chehab struct pvr2_dvb_adapter *pvr2_dvb_create(struct pvr2_context *pvr)
4480c0d06caSMauro Carvalho Chehab {
4490c0d06caSMauro Carvalho Chehab int ret = 0;
4500c0d06caSMauro Carvalho Chehab struct pvr2_dvb_adapter *adap;
4510c0d06caSMauro Carvalho Chehab if (!pvr->hdw->hdw_desc->dvb_props) {
4520c0d06caSMauro Carvalho Chehab /* Device lacks a digital interface so don't set up
4530c0d06caSMauro Carvalho Chehab the DVB side of the driver either. For now. */
4540c0d06caSMauro Carvalho Chehab return NULL;
4550c0d06caSMauro Carvalho Chehab }
4560c0d06caSMauro Carvalho Chehab adap = kzalloc(sizeof(*adap), GFP_KERNEL);
4570c0d06caSMauro Carvalho Chehab if (!adap) return adap;
4580c0d06caSMauro Carvalho Chehab pvr2_channel_init(&adap->channel, pvr);
4590c0d06caSMauro Carvalho Chehab adap->channel.check_func = pvr2_dvb_internal_check;
4600c0d06caSMauro Carvalho Chehab init_waitqueue_head(&adap->buffer_wait_data);
4610c0d06caSMauro Carvalho Chehab mutex_init(&adap->lock);
4620c0d06caSMauro Carvalho Chehab ret = pvr2_dvb_adapter_init(adap);
4630c0d06caSMauro Carvalho Chehab if (ret < 0) goto fail1;
4640c0d06caSMauro Carvalho Chehab ret = pvr2_dvb_frontend_init(adap);
4650c0d06caSMauro Carvalho Chehab if (ret < 0) goto fail2;
4660c0d06caSMauro Carvalho Chehab return adap;
4670c0d06caSMauro Carvalho Chehab
4680c0d06caSMauro Carvalho Chehab fail2:
4690c0d06caSMauro Carvalho Chehab pvr2_dvb_adapter_exit(adap);
4700c0d06caSMauro Carvalho Chehab fail1:
4710c0d06caSMauro Carvalho Chehab pvr2_channel_done(&adap->channel);
4720c0d06caSMauro Carvalho Chehab return NULL;
4730c0d06caSMauro Carvalho Chehab }
4740c0d06caSMauro Carvalho Chehab
475