Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp59291pxa; Tue, 18 Aug 2020 15:52:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyEPXGd4c5qAejNX941eioopVQ0g+RzjiUlUAXJ5stlGAtYgZDBAAJfVCSDylRVsZYz+7de X-Received: by 2002:a17:906:924b:: with SMTP id c11mr23195032ejx.75.1597791126741; Tue, 18 Aug 2020 15:52:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597791126; cv=none; d=google.com; s=arc-20160816; b=cUAbang3YhrWB5Z/IHnT8XRSXihmVxds/YUP3mUQWh7Zov2+uGJ0VtCidDzbmvMHSw VU3ctp+tp1xk2hgMDYplV9gUrkvlTodpRvaoc3Xq2NoSm5dELChqV3VAEhVhEhr6szMX PhlM6y04aJhIDkO6OjwpX4/8j6ixSgCVB/q/Jbxml+ak3ed4UpYcGi8GYq0w3u9li596 MKGWcsME+Y9ugzjkRmmbc3kUa2esc1YlLw7lGxXm4blPJ/ZJKGWUYc9ufsZmYrBlEl3r C/RgCkL/2Xasp+hZ0hOi7DZr/sSS9iFF9nFrR9Jpvcli+UcWuYQrVct46PpKsxN6/fEe P1Uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=xz+bvKvQv0bCe8YM3Nj8/uL1T6G/mLhuNU/Y6ps+cfA=; b=kECkVUGFQ8wdGx89t1IN8lNhBGJsix3ht45vv6MyQLS2DiETnb/vjkatGftAPOfC+U 60gPyJs0PNp6EKek4YeiNEZdmeihQij31x7x+3vlj9Ui1xa01nQ/Hl8mymvLYEy1+1xU E+WPu5LOPO1WsDul8j/UV8LWpbdCG/ia596KRjyBMo0yYkb51QsDUo3I8IoFF9YpMtdG 3m+7cR48Zs1TqZlUqTFefC2NpStBvVpBEJm4AEsWzf5V4odc0ccYUKP11xQtFiIFnXaG jD3gASI0V6/JryucXmqX/smq3C+/Gj4pm0hIdTAxO8RJb1vAannXqHVgY224WVC5A5GM FZcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZYqon0mc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b6si15604939ejb.68.2020.08.18.15.51.43; Tue, 18 Aug 2020 15:52:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZYqon0mc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727042AbgHRWu5 (ORCPT + 99 others); Tue, 18 Aug 2020 18:50:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726799AbgHRWuw (ORCPT ); Tue, 18 Aug 2020 18:50:52 -0400 Received: from mail-qv1-xf41.google.com (mail-qv1-xf41.google.com [IPv6:2607:f8b0:4864:20::f41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71E33C061389; Tue, 18 Aug 2020 15:50:52 -0700 (PDT) Received: by mail-qv1-xf41.google.com with SMTP id y11so10408458qvl.4; Tue, 18 Aug 2020 15:50:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xz+bvKvQv0bCe8YM3Nj8/uL1T6G/mLhuNU/Y6ps+cfA=; b=ZYqon0mcZve/s8eXx+ezJd/ppVE4kS5OR4k4qenBGx/ch94DthSEhnk5Gs7Wcyw+5q j9ueZM5QyBvPDJEpKUn7sSpzVQ+PwZjditF9rURHCh2YU4VALh+7xfvf3BBYfRolyTFM WSp0CnBkE4Rtd8rilGat5XajOwg4RyCMHehLdFiXfQqyoazaVoqWgBSq/ZuA89h/gAUp 3oIN0NysDrMJLN+9EiqupW7gbPqWCs6gWom1n1W7u7Kr5xP+AR/8IrD+rh8Uol7gnvsS h6gQwYWSg0oFrjEKVvc/MksYQc/U5xuD+1m+BdE3Xq+X/7REA/+K2aB6LT43tu1A3xVF yutA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xz+bvKvQv0bCe8YM3Nj8/uL1T6G/mLhuNU/Y6ps+cfA=; b=qzzMSx6NtXLPyKjo6evz0fESUazhtWO6ieZb622N2avgOq07lMES7mEK5rPxNkm4SX DJpP4zgdHRAkWVliFOJy86xN6LvZLnvKbDbtijGdI0naLBOniLAjNA9g8Ev9iMfMeNJh /lGfcS5dovBtQpbK5JfbJUGAz5DsG0PqRzCuYu3r4rYVYpWxB7R65rpCp4LqwcO5Pb92 3RruHrA1ATAUooPA5R1u4wPVvE1M8K2ylSnL7GWqY32slf4yDaT3GiPT7NpGDj1GkSF5 LA28O5reSPNx8MieThgK+Qt83C5moKPAVH2glJflOpjBDS9tLTuv2ydf5IrufO4n1vHn G/3w== X-Gm-Message-State: AOAM530qpFvSJW4a8XKlDeuNZuortSbo8ROtpnJ3vjiMYm+JWPjEW0J2 noNScmPjzcH6C5/FJ6biYKk= X-Received: by 2002:ad4:4365:: with SMTP id u5mr21448409qvt.109.1597791051504; Tue, 18 Aug 2020 15:50:51 -0700 (PDT) Received: from localhost.localdomain ([2804:14d:72b1:8920:a2ce:f815:f14d:bfac]) by smtp.gmail.com with ESMTPSA id h13sm24566519qtu.7.2020.08.18.15.50.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 15:50:51 -0700 (PDT) From: "Daniel W. S. Almeida" X-Google-Original-From: Daniel W. S. Almeida To: mchehab+huawei@kernel.org Cc: "Daniel W. S. Almeida" , linux-media@vger.kernel.org, skhan@linuxfoundation.org, linux-kernel-mentees@lists.linuxfoundation.org, linux-kernel@vger.kernel.org Subject: [v9 1/4] media: vidtv: implement a tuner driver Date: Tue, 18 Aug 2020 19:50:38 -0300 Message-Id: <20200818225041.1133770-2-dwlsalmeida@gmail.com> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200818225041.1133770-1-dwlsalmeida@gmail.com> References: <20200818225041.1133770-1-dwlsalmeida@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Daniel W. S. Almeida" The virtual DVB test driver serves as a reference DVB driver and helps validate the existing APIs in the media subsystem. It can also aid developers working on userspace applications. This dummy tuner should support common TV standards such as DVB-T/T2/S/S2, ISDB-T and ATSC when completed. Signed-off-by: Daniel W. S. Almeida --- MAINTAINERS | 8 + drivers/media/test-drivers/Kconfig | 16 + drivers/media/test-drivers/Makefile | 1 + drivers/media/test-drivers/vidtv/Kconfig | 11 + drivers/media/test-drivers/vidtv/Makefile | 5 + .../media/test-drivers/vidtv/vidtv_tuner.c | 427 ++++++++++++++++++ .../media/test-drivers/vidtv/vidtv_tuner.h | 43 ++ 7 files changed, 511 insertions(+) create mode 100644 drivers/media/test-drivers/vidtv/Kconfig create mode 100644 drivers/media/test-drivers/vidtv/Makefile create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.h diff --git a/MAINTAINERS b/MAINTAINERS index 4e2698cc7e23..145ec7625ba3 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -18291,6 +18291,14 @@ W: https://linuxtv.org T: git git://linuxtv.org/media_tree.git F: drivers/media/test-drivers/vivid/* +VIDTV VIRTUAL DIGITAL TV DRIVER +M: Daniel W. S. Almeida +L: linux-media@vger.kernel.org +S: Maintained +W: https://linuxtv.org +T: git git://linuxtv.org/media_tree.git +F: drivers/media/test-drivers/vidtv/* + VLYNQ BUS M: Florian Fainelli L: openwrt-devel@lists.openwrt.org (subscribers-only) diff --git a/drivers/media/test-drivers/Kconfig b/drivers/media/test-drivers/Kconfig index 188381c85593..874fcc72c48c 100644 --- a/drivers/media/test-drivers/Kconfig +++ b/drivers/media/test-drivers/Kconfig @@ -4,6 +4,16 @@ menuconfig V4L_TEST_DRIVERS bool "V4L test drivers" depends on VIDEO_DEV +menuconfig DVB_TEST_DRIVERS + bool "DVB test drivers" + depends on DVB_CORE && MEDIA_SUPPORT && I2C + help + Enables DVB test drivers. + + This enables the DVB test drivers. They are meant as an aid for + DVB device driver writers and developers working on userspace + media applications. + if V4L_TEST_DRIVERS source "drivers/media/test-drivers/vimc/Kconfig" @@ -24,3 +34,9 @@ config VIDEO_VIM2M source "drivers/media/test-drivers/vicodec/Kconfig" endif #V4L_TEST_DRIVERS + +if DVB_TEST_DRIVERS + +source "drivers/media/test-drivers/vidtv/Kconfig" + +endif #DVB_TEST_DRIVERS diff --git a/drivers/media/test-drivers/Makefile b/drivers/media/test-drivers/Makefile index 74410d3a9f2d..9f0e4ebb2efe 100644 --- a/drivers/media/test-drivers/Makefile +++ b/drivers/media/test-drivers/Makefile @@ -7,3 +7,4 @@ obj-$(CONFIG_VIDEO_VIMC) += vimc/ obj-$(CONFIG_VIDEO_VIVID) += vivid/ obj-$(CONFIG_VIDEO_VIM2M) += vim2m.o obj-$(CONFIG_VIDEO_VICODEC) += vicodec/ +obj-$(CONFIG_DVB_VIDTV) += vidtv/ diff --git a/drivers/media/test-drivers/vidtv/Kconfig b/drivers/media/test-drivers/vidtv/Kconfig new file mode 100644 index 000000000000..22c4fd39461f --- /dev/null +++ b/drivers/media/test-drivers/vidtv/Kconfig @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: GPL-2.0-only +config DVB_VIDTV + tristate "Virtual DVB Driver (vidtv)" + depends on DVB_CORE && MEDIA_SUPPORT && I2C + help + The virtual DVB test driver serves as a reference DVB driver and helps + validate the existing APIs in the media subsystem. It can also aid developers + working on userspace applications. + + + When in doubt, say N. diff --git a/drivers/media/test-drivers/vidtv/Makefile b/drivers/media/test-drivers/vidtv/Makefile new file mode 100644 index 000000000000..58e022c094e5 --- /dev/null +++ b/drivers/media/test-drivers/vidtv/Makefile @@ -0,0 +1,5 @@ +# SPDX-License-Identifier: GPL-2.0 + +dvb-vidtv-tuner-objs := vidtv_tuner.o + +obj-$(CONFIG_DVB_VIDTV) += dvb-vidtv-tuner.o diff --git a/drivers/media/test-drivers/vidtv/vidtv_tuner.c b/drivers/media/test-drivers/vidtv/vidtv_tuner.c new file mode 100644 index 000000000000..857668f8d714 --- /dev/null +++ b/drivers/media/test-drivers/vidtv/vidtv_tuner.c @@ -0,0 +1,427 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * The Virtual DVB test driver serves as a reference DVB driver and helps + * validate the existing APIs in the media subsystem. It can also aid + * developers working on userspace applications. + * + * The vidtv tuner should support common TV standards such as + * DVB-T/T2/S/S2, ISDB-T and ATSC when completed. + * + * Copyright (C) 2020 Daniel W. S. Almeida + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vidtv_tuner.h" + +struct vidtv_tuner_cnr_to_qual_s { + /* attempt to use the same values as libdvbv5 */ + u32 modulation; + u32 fec; + u32 cnr_ok; + u32 cnr_good; +}; + +static const struct vidtv_tuner_cnr_to_qual_s vidtv_tuner_c_cnr_2_qual[] = { + /* from libdvbv5 source code, in milli db */ + { QAM_256, FEC_NONE, 34000, 38000}, + { QAM_64, FEC_NONE, 30000, 34000}, +}; + +static const struct vidtv_tuner_cnr_to_qual_s vidtv_tuner_s_cnr_2_qual[] = { + /* from libdvbv5 source code, in milli db */ + { QPSK, FEC_1_2, 7000, 10000}, + { QPSK, FEC_2_3, 9000, 12000}, + { QPSK, FEC_3_4, 10000, 13000}, + { QPSK, FEC_5_6, 11000, 14000}, + { QPSK, FEC_7_8, 12000, 15000}, +}; + +static const struct vidtv_tuner_cnr_to_qual_s vidtv_tuner_s2_cnr_2_qual[] = { + /* from libdvbv5 source code, in milli db */ + { QPSK, FEC_1_2, 9000, 12000}, + { QPSK, FEC_2_3, 11000, 14000}, + { QPSK, FEC_3_4, 12000, 15000}, + { QPSK, FEC_5_6, 12000, 15000}, + { QPSK, FEC_8_9, 13000, 16000}, + { QPSK, FEC_9_10, 13500, 16500}, + { PSK_8, FEC_2_3, 14500, 17500}, + { PSK_8, FEC_3_4, 16000, 19000}, + { PSK_8, FEC_5_6, 17500, 20500}, + { PSK_8, FEC_8_9, 19000, 22000}, +}; + +static const struct vidtv_tuner_cnr_to_qual_s vidtv_tuner_t_cnr_2_qual[] = { + /* from libdvbv5 source code, in milli db*/ + { QPSK, FEC_1_2, 4100, 5900}, + { QPSK, FEC_2_3, 6100, 9600}, + { QPSK, FEC_3_4, 7200, 12400}, + { QPSK, FEC_5_6, 8500, 15600}, + { QPSK, FEC_7_8, 9200, 17500}, + { QAM_16, FEC_1_2, 9800, 11800}, + { QAM_16, FEC_2_3, 12100, 15300}, + { QAM_16, FEC_3_4, 13400, 18100}, + { QAM_16, FEC_5_6, 14800, 21300}, + { QAM_16, FEC_7_8, 15700, 23600}, + { QAM_64, FEC_1_2, 14000, 16000}, + { QAM_64, FEC_2_3, 19900, 25400}, + { QAM_64, FEC_3_4, 24900, 27900}, + { QAM_64, FEC_5_6, 21300, 23300}, + { QAM_64, FEC_7_8, 22000, 24000}, +}; + +/** + * struct vidtv_tuner_hardware_state - Simulate the tuner hardware status + * @asleep: whether the tuner is asleep, i.e whether _sleep() or _suspend() was + * called. + * @lock_status: Whether the tuner has managed to lock on the requested + * frequency. + * @if_frequency: The tuner's intermediate frequency. Hardcoded for the purposes + * of simulation. + * @tuned_frequency: The actual tuned frequency. + * @bandwidth: The actual bandwidth. + * + * This structure is meant to simulate the status of the tuner hardware, as if + * we had a physical tuner hardware. + */ +struct vidtv_tuner_hardware_state { + bool asleep; + u32 lock_status; + u32 if_frequency; + u32 tuned_frequency; + u32 bandwidth; +}; + +/** + * struct vidtv_tuner_dev - The tuner struct + * @fe: A pointer to the dvb_frontend structure allocated by vidtv_demod + * @hw_state: A struct to simulate the tuner's hardware state as if we had a + * physical tuner hardware. + * @config: The configuration used to start the tuner module, usually filled + * by a bridge driver. For vidtv, this is filled by vidtv_bridge before the + * tuner module is probed. + */ +struct vidtv_tuner_dev { + struct dvb_frontend *fe; + struct vidtv_tuner_hardware_state hw_state; + struct vidtv_tuner_config config; +}; + +static struct vidtv_tuner_dev* +vidtv_tuner_get_dev(struct dvb_frontend *fe) +{ + return i2c_get_clientdata(fe->tuner_priv); +} + +static s32 vidtv_tuner_check_frequency_shift(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + struct dtv_frontend_properties *c = &fe->dtv_property_cache; + struct vidtv_tuner_config config = tuner_dev->config; + u32 *valid_freqs = NULL; + u32 array_sz = 0; + u32 i; + u32 shift; + + switch (c->delivery_system) { + case SYS_DVBT: + case SYS_DVBT2: + valid_freqs = config.vidtv_valid_dvb_t_freqs; + array_sz = ARRAY_SIZE(config.vidtv_valid_dvb_t_freqs); + break; + case SYS_DVBS: + case SYS_DVBS2: + valid_freqs = config.vidtv_valid_dvb_s_freqs; + array_sz = ARRAY_SIZE(config.vidtv_valid_dvb_s_freqs); + break; + case SYS_DVBC_ANNEX_A: + valid_freqs = config.vidtv_valid_dvb_c_freqs; + array_sz = ARRAY_SIZE(config.vidtv_valid_dvb_c_freqs); + break; + + default: + pr_warn("%s: unsupported delivery system: %u\n", + __func__, + c->delivery_system); + + return -EINVAL; + } + + for (i = 0; i < array_sz; i++) { + shift = abs(c->frequency - valid_freqs[i]); + + if (!shift) + return 0; + + /* + * This will provide a value from 0 to 100 that would + * indicate how far is the tuned frequency from the + * right one. + */ + if (shift < config.max_frequency_shift_hz) + return shift * 100 / config.max_frequency_shift_hz; + } + + return -EINVAL; +} + +static int +vidtv_tuner_get_signal_strength(struct dvb_frontend *fe, u16 *strength) +{ + struct dtv_frontend_properties *c = &fe->dtv_property_cache; + const struct vidtv_tuner_cnr_to_qual_s *cnr2qual = NULL; + u32 array_size = 0; + s32 shift; + u32 i; + + shift = vidtv_tuner_check_frequency_shift(fe); + + switch (c->delivery_system) { + case SYS_DVBT: + case SYS_DVBT2: + cnr2qual = vidtv_tuner_t_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_tuner_t_cnr_2_qual); + break; + + case SYS_DVBS: + cnr2qual = vidtv_tuner_s_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_tuner_s_cnr_2_qual); + break; + + case SYS_DVBS2: + cnr2qual = vidtv_tuner_s2_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_tuner_s2_cnr_2_qual); + break; + + case SYS_DVBC_ANNEX_A: + cnr2qual = vidtv_tuner_c_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_tuner_c_cnr_2_qual); + break; + + default: + pr_warn_ratelimited("%s: unsupported delivery system: %u\n", + __func__, + c->delivery_system); + return -EINVAL; + } + + for (i = 0; i < array_size; i++) { + if (cnr2qual[i].modulation != c->modulation || + cnr2qual[i].fec != c->fec_inner) + continue; + + if (!shift) { + *strength = cnr2qual[i].cnr_good; + return 0; + } + if (shift < 0) { /* Channel not tuned */ + *strength = 0; + return 0; + } + /* + * Channel tuned at wrong frequency. Simulate that the + * Carrier S/N ratio is not too good. + */ + + *strength = cnr2qual[i].cnr_ok - + (cnr2qual[i].cnr_good - cnr2qual[i].cnr_ok); + return 0; + } + + /* + * do a linear interpolation between 34dB and 10dB if we can't + * match against the table + */ + *strength = 34 - 24 * shift / 100; + return 0; +} + +static int vidtv_tuner_init(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + struct vidtv_tuner_config config = tuner_dev->config; + + msleep_interruptible(config.mock_power_up_delay_msec); + + tuner_dev->hw_state.asleep = false; + tuner_dev->hw_state.if_frequency = 5000; + + return 0; +} + +static int vidtv_tuner_sleep(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + tuner_dev->hw_state.asleep = true; + return 0; +} + +static int vidtv_tuner_suspend(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + tuner_dev->hw_state.asleep = true; + return 0; +} + +static int vidtv_tuner_resume(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + tuner_dev->hw_state.asleep = false; + return 0; +} + +static int vidtv_tuner_set_params(struct dvb_frontend *fe) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + struct vidtv_tuner_config config = tuner_dev->config; + struct dtv_frontend_properties *c = &fe->dtv_property_cache; + + u32 min_freq = fe->ops.tuner_ops.info.frequency_min_hz; + u32 max_freq = fe->ops.tuner_ops.info.frequency_max_hz; + u32 min_bw = fe->ops.tuner_ops.info.bandwidth_min; + u32 max_bw = fe->ops.tuner_ops.info.bandwidth_max; + + if (c->frequency < min_freq || c->frequency > max_freq || + c->bandwidth_hz < min_bw || c->bandwidth_hz > max_bw) { + tuner_dev->hw_state.lock_status = 0; + return -EINVAL; + } + + tuner_dev->hw_state.tuned_frequency = c->frequency; + tuner_dev->hw_state.bandwidth = c->bandwidth_hz; + tuner_dev->hw_state.lock_status = TUNER_STATUS_LOCKED; + + msleep_interruptible(config.mock_tune_delay_msec); + return 0; +} + +static int vidtv_tuner_set_config(struct dvb_frontend *fe, + void *priv_cfg) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + memcpy(&tuner_dev->config, priv_cfg, sizeof(tuner_dev->config)); + + return 0; +} + +static int vidtv_tuner_get_frequency(struct dvb_frontend *fe, + u32 *frequency) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + *frequency = tuner_dev->hw_state.tuned_frequency; + + return 0; +} + +static int vidtv_tuner_get_bandwidth(struct dvb_frontend *fe, + u32 *bandwidth) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + *bandwidth = tuner_dev->hw_state.bandwidth; + + return 0; +} + +static int vidtv_tuner_get_if_frequency(struct dvb_frontend *fe, + u32 *frequency) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + *frequency = tuner_dev->hw_state.if_frequency; + + return 0; +} + +static int vidtv_tuner_get_status(struct dvb_frontend *fe, u32 *status) +{ + struct vidtv_tuner_dev *tuner_dev = vidtv_tuner_get_dev(fe); + + *status = tuner_dev->hw_state.lock_status; + + return 0; +} + +static const struct dvb_tuner_ops vidtv_tuner_ops = { + .init = vidtv_tuner_init, + .sleep = vidtv_tuner_sleep, + .suspend = vidtv_tuner_suspend, + .resume = vidtv_tuner_resume, + .set_params = vidtv_tuner_set_params, + .set_config = vidtv_tuner_set_config, + .get_bandwidth = vidtv_tuner_get_bandwidth, + .get_frequency = vidtv_tuner_get_frequency, + .get_if_frequency = vidtv_tuner_get_if_frequency, + .get_status = vidtv_tuner_get_status, + .get_rf_strength = vidtv_tuner_get_signal_strength +}; + +static const struct i2c_device_id vidtv_tuner_i2c_id_table[] = { + {"dvb_vidtv_tuner", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, vidtv_tuner_i2c_id_table); + +static int vidtv_tuner_i2c_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct vidtv_tuner_config *config = client->dev.platform_data; + struct dvb_frontend *fe = config->fe; + struct vidtv_tuner_dev *tuner_dev = NULL; + + tuner_dev = kzalloc(sizeof(*tuner_dev), GFP_KERNEL); + if (!tuner_dev) + return -ENOMEM; + + tuner_dev->fe = config->fe; + i2c_set_clientdata(client, tuner_dev); + + memcpy(&fe->ops.tuner_ops, + &vidtv_tuner_ops, + sizeof(struct dvb_tuner_ops)); + + fe->tuner_priv = client; + + return 0; +} + +static int vidtv_tuner_i2c_remove(struct i2c_client *client) +{ + struct vidtv_tuner_dev *tuner_dev = i2c_get_clientdata(client); + struct dvb_frontend *fe = tuner_dev->fe; + + memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops)); + fe->tuner_priv = NULL; + kfree(tuner_dev); + + return 0; +} + +static struct i2c_driver vidtv_tuner_i2c_driver = { + .driver = { + .name = "dvb_vidtv_tuner", + .suppress_bind_attrs = true, + }, + .probe = vidtv_tuner_i2c_probe, + .remove = vidtv_tuner_i2c_remove, + .id_table = vidtv_tuner_i2c_id_table, +}; +module_i2c_driver(vidtv_tuner_i2c_driver); + +MODULE_DESCRIPTION("Virtual DVB Tuner"); +MODULE_AUTHOR("Daniel W. S. Almeida"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/test-drivers/vidtv/vidtv_tuner.h b/drivers/media/test-drivers/vidtv/vidtv_tuner.h new file mode 100644 index 000000000000..8455b2d564b3 --- /dev/null +++ b/drivers/media/test-drivers/vidtv/vidtv_tuner.h @@ -0,0 +1,43 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * The Virtual DTV test driver serves as a reference DVB driver and helps + * validate the existing APIs in the media subsystem. It can also aid + * developers working on userspace applications. + * + * Copyright (C) 2020 Daniel W. S. Almeida + */ + +#ifndef VIDTV_TUNER_H +#define VIDTV_TUNER_H + +#include +#include + +#define NUM_VALID_TUNER_FREQS 8 + +/** + * struct vidtv_tuner_config - Configuration used to init the tuner. + * @fe: A pointer to the dvb_frontend structure allocated by vidtv_demod. + * @mock_power_up_delay_msec: Simulate a power-up delay. + * @mock_tune_delay_msec: Simulate a tune delay. + * @vidtv_valid_dvb_t_freqs: The valid DVB-T frequencies to simulate. + * @vidtv_valid_dvb_c_freqs: The valid DVB-C frequencies to simulate. + * @vidtv_valid_dvb_s_freqs: The valid DVB-S frequencies to simulate. + * @max_frequency_shift_hz: The maximum frequency shift in HZ allowed when + * tuning in a channel + * + * The configuration used to init the tuner module, usually filled + * by a bridge driver. For vidtv, this is filled by vidtv_bridge before the + * tuner module is probed. + */ +struct vidtv_tuner_config { + struct dvb_frontend *fe; + u32 mock_power_up_delay_msec; + u32 mock_tune_delay_msec; + u32 vidtv_valid_dvb_t_freqs[NUM_VALID_TUNER_FREQS]; + u32 vidtv_valid_dvb_c_freqs[NUM_VALID_TUNER_FREQS]; + u32 vidtv_valid_dvb_s_freqs[NUM_VALID_TUNER_FREQS]; + u8 max_frequency_shift_hz; +}; + +#endif //VIDTV_TUNER_H -- 2.28.0