From patchwork Tue Aug 18 22:50:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Almeida X-Patchwork-Id: 256098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.6 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6D842C433DF for ; Tue, 18 Aug 2020 22:50:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DAD42065D for ; Tue, 18 Aug 2020 22:50:59 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="F5Q+i6KZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727043AbgHRWu5 (ORCPT ); Tue, 18 Aug 2020 18:50:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45976 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727031AbgHRWuz (ORCPT ); Tue, 18 Aug 2020 18:50:55 -0400 Received: from mail-qt1-x844.google.com (mail-qt1-x844.google.com [IPv6:2607:f8b0:4864:20::844]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 744CCC061342; Tue, 18 Aug 2020 15:50:55 -0700 (PDT) Received: by mail-qt1-x844.google.com with SMTP id f19so11967171qtp.2; Tue, 18 Aug 2020 15:50:55 -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=2JWFYeA2hO04mgRAoASPfcZD3EC4kFK9GI+U7GFdp7U=; b=F5Q+i6KZg9AD3JhIKg2sIKQ35pBLCWHGuDT4HKb9nY7JILb1DMGeOnQXjvvvbh0UDL 3Mp9fDQ6vcAN2zvhBm2nA6vy3ncUGRLjGwvtjGVf0nzXQ/4UkKq2ETDQyYerIZVar7z4 ywZtqQFZeE+d2FynO1fMuzHyc7uKFODZj6g7Tl4O98sk52Dsck1CiVqjLjljQ4YLduZh vfLqJxtTK0WYb0v6riNe5yKoXnwbStUiKUK4656kBUeiRtPS+Abi1F33HCIS35ZCN8SW CArE0FrFsfE5XQzEfnG6vXQ1zWudLJQf9Ea8/S96fd0w1NinwVRQV1/m4dISjAmAATEO 2kog== 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=2JWFYeA2hO04mgRAoASPfcZD3EC4kFK9GI+U7GFdp7U=; b=qiyWU5dMp6alM7rhKZvI5rnz7p45lZGBFCXBJwOjsDKzIAylaTkVjmK7y7vlUNLube MHTGIKC6GqJ7W9hyYllecegq1DGLlT9dTFw7k6w2Vw8wlrps3H7cT/8AplR9fR2CwlwC IlueZsaLy7efGzm4SOGL0E86zYXTYtbR2qoMq8ojOqOJPb4cV0Sf2Ro5zeRkcrM662b0 pN/3Oss7J7AAuGKM2s9Wa01TsXNAuPQHeY9aqb+TSSwJyMYkgRvOKAcY43KELe+40a5l Zj2VApeShvFF841UBLVIQkCVMztpgoDxmhxHVcix8O6Y9nyFQBzjdPHMouFM8agqf4ob mvzw== X-Gm-Message-State: AOAM5323qZp1WKAkiafCtHtFqm54fe0F38XDggN1OqmqRH48faX9y8qR I4vXXLIyN8wj71RtMOWCShsxtifS/dzlFA== X-Google-Smtp-Source: ABdhPJxRCzQHfs4lgrD6wwk0TL6up2cNy7/9ndeC+uarMn105im4A1ZQ9VLVPLyXUmnzmWRMbfWf3w== X-Received: by 2002:ac8:6782:: with SMTP id b2mr20037810qtp.152.1597791054316; Tue, 18 Aug 2020 15:50:54 -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.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 15:50:53 -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 2/4] media: vidtv: implement a demodulator driver Date: Tue, 18 Aug 2020 19:50:39 -0300 Message-Id: <20200818225041.1133770-3-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 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: "Daniel W. S. Almeida" Implement a I2C demodulator driver, simulating support for DVB-T, DVB-C and DVB-S. This demodulator will periodically check the signal quality against a table and drop the TS lock if it drops below a threshold value, regaining it in the event that the signal improves. Signed-off-by: Daniel W. S. Almeida --- drivers/media/test-drivers/vidtv/Makefile | 3 +- .../media/test-drivers/vidtv/vidtv_demod.c | 440 ++++++++++++++++++ .../media/test-drivers/vidtv/vidtv_demod.h | 73 +++ 3 files changed, 515 insertions(+), 1 deletion(-) create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.h -- 2.28.0 diff --git a/drivers/media/test-drivers/vidtv/Makefile b/drivers/media/test-drivers/vidtv/Makefile index 58e022c094e5..21e50c11c94d 100644 --- a/drivers/media/test-drivers/vidtv/Makefile +++ b/drivers/media/test-drivers/vidtv/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 dvb-vidtv-tuner-objs := vidtv_tuner.o +dvb-vidtv-demod-objs := vidtv_demod.o -obj-$(CONFIG_DVB_VIDTV) += dvb-vidtv-tuner.o +obj-$(CONFIG_DVB_VIDTV) += dvb-vidtv-tuner.o dvb-vidtv-demod.o diff --git a/drivers/media/test-drivers/vidtv/vidtv_demod.c b/drivers/media/test-drivers/vidtv/vidtv_demod.c new file mode 100644 index 000000000000..25c895912505 --- /dev/null +++ b/drivers/media/test-drivers/vidtv/vidtv_demod.c @@ -0,0 +1,440 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * 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. + * + * Copyright (C) 2020 Daniel W. S. Almeida + * Based on the example driver written by Emard + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vidtv_demod.h" + +#define POLL_THRD_TIME 2000 /* ms */ + +static const struct vidtv_demod_cnr_to_qual_s vidtv_demod_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_demod_cnr_to_qual_s vidtv_demod_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_demod_cnr_to_qual_s vidtv_demod_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_demod_cnr_to_qual_s vidtv_demod_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}, +}; + +static const struct vidtv_demod_cnr_to_qual_s + *vidtv_match_cnr_s(struct dvb_frontend *fe) +{ + struct dtv_frontend_properties *c; + const struct vidtv_demod_cnr_to_qual_s *cnr2qual = NULL; + u32 array_size = 0; + u32 i; + + c = &fe->dtv_property_cache; + + switch (c->delivery_system) { + case SYS_DVBT: + case SYS_DVBT2: + cnr2qual = vidtv_demod_t_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_demod_t_cnr_2_qual); + break; + + case SYS_DVBS: + cnr2qual = vidtv_demod_s_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_demod_s_cnr_2_qual); + break; + + case SYS_DVBS2: + cnr2qual = vidtv_demod_s2_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_demod_s2_cnr_2_qual); + break; + + case SYS_DVBC_ANNEX_A: + cnr2qual = vidtv_demod_c_cnr_2_qual; + array_size = ARRAY_SIZE(vidtv_demod_c_cnr_2_qual); + break; + + default: + pr_warn_ratelimited("%s: unsupported delivery system: %u\n", + __func__, + c->delivery_system); + break; + } + + for (i = 0; i < array_size; i++) + if (cnr2qual[i].modulation == c->modulation && + cnr2qual[i].fec == c->fec_inner) + return &cnr2qual[i]; + + return NULL; /* not found */ +} + +static void vidtv_demod_poll_snr_handler(struct work_struct *work) +{ + /* + * periodically check the signal quality and eventually + * lose the TS lock if it dips too low + */ + struct vidtv_demod_state *state; + const struct vidtv_demod_cnr_to_qual_s *cnr2qual = NULL; + struct vidtv_demod_config *config; + u16 snr = 0; + + state = container_of(work, struct vidtv_demod_state, poll_snr.work); + config = &state->config; + + if (!state->frontend.ops.tuner_ops.get_rf_strength) + return; + + state->frontend.ops.tuner_ops.get_rf_strength(&state->frontend, &snr); + + cnr2qual = vidtv_match_cnr_s(&state->frontend); + if (!cnr2qual) + return; + + if (snr < cnr2qual->cnr_ok) { + /* eventually lose the TS lock */ + if (prandom_u32_max(100) < config->drop_tslock_prob_on_low_snr) + state->status = 0; + } else { + /* recover if the signal improves */ + if (prandom_u32_max(100) < + config->recover_tslock_prob_on_good_snr) + state->status = FE_HAS_SIGNAL | + FE_HAS_CARRIER | + FE_HAS_VITERBI | + FE_HAS_SYNC | + FE_HAS_LOCK; + } + + schedule_delayed_work(&state->poll_snr, + msecs_to_jiffies(POLL_THRD_TIME)); +} + +static int vidtv_demod_read_status(struct dvb_frontend *fe, + enum fe_status *status) +{ + struct vidtv_demod_state *state = fe->demodulator_priv; + + *status = state->status; + + return 0; +} + +static int vidtv_demod_read_ber(struct dvb_frontend *fe, u32 *ber) +{ + *ber = 0; + return 0; +} + +static int vidtv_demod_read_signal_strength(struct dvb_frontend *fe, + u16 *strength) +{ + *strength = 0; + return 0; +} + +static int vidtv_demod_read_snr(struct dvb_frontend *fe, u16 *snr) +{ + *snr = 0; + return 0; +} + +static int vidtv_demod_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) +{ + *ucblocks = 0; + return 0; +} + +/* + * NOTE: + * This is implemented here just to be used as an example for real + * demod drivers. + * + * Should only be implemented if it actually reads something from the hardware. + * Also, it should check for the locks, in order to avoid report wrong data + * to userspace. + */ +static int vidtv_demod_get_frontend(struct dvb_frontend *fe, + struct dtv_frontend_properties *p) +{ + return 0; +} + +static int vidtv_demod_set_frontend(struct dvb_frontend *fe) +{ + struct vidtv_demod_state *state = fe->demodulator_priv; + const struct vidtv_demod_cnr_to_qual_s *cnr2qual = NULL; + u32 tuner_status = 0; + + if (fe->ops.tuner_ops.set_params) { + fe->ops.tuner_ops.set_params(fe); + + /* store the CNR returned by the tuner */ + fe->ops.tuner_ops.get_rf_strength(fe, &state->tuner_cnr); + + fe->ops.tuner_ops.get_status(fe, &tuner_status); + state->status = (state->tuner_cnr > 0) ? FE_HAS_SIGNAL | + FE_HAS_CARRIER | + FE_HAS_VITERBI | + FE_HAS_SYNC | + FE_HAS_LOCK : + 0; + cnr2qual = vidtv_match_cnr_s(fe); + + /* signal isn't good: might lose the lock eventually */ + if (tuner_status == TUNER_STATUS_LOCKED && + state->tuner_cnr < cnr2qual->cnr_good) { + schedule_delayed_work(&state->poll_snr, + msecs_to_jiffies(POLL_THRD_TIME)); + + state->poll_snr_thread_running = true; + } + + if (fe->ops.i2c_gate_ctrl) + fe->ops.i2c_gate_ctrl(fe, 0); + } + + return 0; +} + +static int vidtv_demod_sleep(struct dvb_frontend *fe) +{ + struct vidtv_demod_state *state = fe->demodulator_priv; + + if (state->poll_snr_thread_running) { + cancel_delayed_work_sync(&state->poll_snr); + state->poll_snr_thread_running = false; + state->poll_snr_thread_restart = true; + } + return 0; +} + +static int vidtv_demod_init(struct dvb_frontend *fe) +{ + struct vidtv_demod_state *state = fe->demodulator_priv; + u32 tuner_status = 0; + + if (state->cold_start) + INIT_DELAYED_WORK(&state->poll_snr, + &vidtv_demod_poll_snr_handler); + + /* + * At resume, start the snr poll thread only if it was suspended with + * the thread running. Extra care should be taken here, as some tuner + * status change might happen at resume time (for example, due to an + * ioctl syscall to set_frontend, or due to a release syscall). + */ + fe->ops.tuner_ops.get_status(fe, &tuner_status); + + if (tuner_status == TUNER_STATUS_LOCKED && + state->poll_snr_thread_restart) { + schedule_delayed_work(&state->poll_snr, + msecs_to_jiffies(POLL_THRD_TIME)); + + state->poll_snr_thread_restart = false; + } + + state->cold_start = false; + return 0; +} + +/* + * NOTE: + * This is implemented here just to be used as an example for real + * demod drivers. + * + * Should only be implemented if the demod has support for DVB-S or DVB-S2 + */ +static int vidtv_demod_set_tone(struct dvb_frontend *fe, + enum fe_sec_tone_mode tone) +{ + return 0; +} + +/* + * NOTE: + * This is implemented here just to be used as an example for real + * demod drivers. + * + * Should only be implemented if the demod has support for DVB-S or DVB-S2 + */ +static int vidtv_demod_set_voltage(struct dvb_frontend *fe, + enum fe_sec_voltage voltage) +{ + return 0; +} + +static void vidtv_demod_release(struct dvb_frontend *fe) +{ + struct vidtv_demod_state *state = fe->demodulator_priv; + + if (state->poll_snr_thread_running) + cancel_delayed_work_sync(&state->poll_snr); + + kfree(state); +} + +static const struct dvb_frontend_ops vidtv_demod_ops = { + .delsys = { + SYS_DVBT, + SYS_DVBT2, + SYS_DVBC_ANNEX_A, + SYS_DVBS, + SYS_DVBS2, + }, + + .info = { + .name = "Dummy demod for DVB-T/T2/C/S/S2", + .frequency_min_hz = 51 * MHz, + .frequency_max_hz = 2150 * MHz, + .frequency_stepsize_hz = 62500, + .frequency_tolerance_hz = 29500 * kHz, + .symbol_rate_min = 1000000, + .symbol_rate_max = 45000000, + + .caps = FE_CAN_FEC_1_2 | + FE_CAN_FEC_2_3 | + FE_CAN_FEC_3_4 | + FE_CAN_FEC_4_5 | + FE_CAN_FEC_5_6 | + FE_CAN_FEC_6_7 | + FE_CAN_FEC_7_8 | + FE_CAN_FEC_8_9 | + FE_CAN_QAM_16 | + FE_CAN_QAM_64 | + FE_CAN_QAM_32 | + FE_CAN_QAM_128 | + FE_CAN_QAM_256 | + FE_CAN_QAM_AUTO | + FE_CAN_QPSK | + FE_CAN_FEC_AUTO | + FE_CAN_INVERSION_AUTO | + FE_CAN_TRANSMISSION_MODE_AUTO | + FE_CAN_GUARD_INTERVAL_AUTO | + FE_CAN_HIERARCHY_AUTO, + }, + + .release = vidtv_demod_release, + + .init = vidtv_demod_init, + .sleep = vidtv_demod_sleep, + + .set_frontend = vidtv_demod_set_frontend, + .get_frontend = vidtv_demod_get_frontend, + + .read_status = vidtv_demod_read_status, + .read_ber = vidtv_demod_read_ber, + .read_signal_strength = vidtv_demod_read_signal_strength, + .read_snr = vidtv_demod_read_snr, + .read_ucblocks = vidtv_demod_read_ucblocks, + + /* For DVB-S/S2 */ + .set_voltage = vidtv_demod_set_voltage, + .set_tone = vidtv_demod_set_tone, +}; + +static const struct i2c_device_id vidtv_demod_i2c_id_table[] = { + {"dvb_vidtv_demod", 0}, + {} +}; +MODULE_DEVICE_TABLE(i2c, vidtv_demod_i2c_id_table); + +static int vidtv_demod_i2c_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct vidtv_demod_state *state; + + /* allocate memory for the internal state */ + state = kzalloc(sizeof(*state), GFP_KERNEL); + if (!state) + return -ENOMEM; + + /* create dvb_frontend */ + memcpy(&state->frontend.ops, + &vidtv_demod_ops, + sizeof(struct dvb_frontend_ops)); + + state->frontend.demodulator_priv = state; + i2c_set_clientdata(client, state); + + return 0; +} + +static int vidtv_demod_i2c_remove(struct i2c_client *client) +{ + struct vidtv_demod_state *state = i2c_get_clientdata(client); + + memset(&state->frontend.ops, 0, sizeof(struct dvb_frontend_ops)); + state->frontend.demodulator_priv = NULL; + kfree(state); + + return 0; +} + +static struct i2c_driver vidtv_demod_i2c_driver = { + .driver = { + .name = "dvb_vidtv_demod", + .suppress_bind_attrs = true, + }, + .probe = vidtv_demod_i2c_probe, + .remove = vidtv_demod_i2c_remove, + .id_table = vidtv_demod_i2c_id_table, +}; + +module_i2c_driver(vidtv_demod_i2c_driver); + +MODULE_DESCRIPTION("Virtual DVB Demodulator Driver"); +MODULE_AUTHOR("Daniel W. S. Almeida"); +MODULE_LICENSE("GPL"); diff --git a/drivers/media/test-drivers/vidtv/vidtv_demod.h b/drivers/media/test-drivers/vidtv/vidtv_demod.h new file mode 100644 index 000000000000..dfb36c515e4d --- /dev/null +++ b/drivers/media/test-drivers/vidtv/vidtv_demod.h @@ -0,0 +1,73 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * 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 + * Based on the example driver written by Emard + */ + +#ifndef VIDTV_DEMOD_H +#define VIDTV_DEMOD_H + +#include +#include + +/** + * struct vidtv_demod_cnr_to_qual_s - Map CNR values to a given combination of + * modulation and fec_inner + * @modulation: see enum fe_modulation + * @fec: see enum fe_fec_rate + * + * This struct matches values for 'good' and 'ok' CNRs given the combination + * of modulation and fec_inner in use. We might simulate some noise if the + * signal quality is not too good. + * + * The values were taken from libdvbv5. + */ +struct vidtv_demod_cnr_to_qual_s { + u32 modulation; + u32 fec; + u32 cnr_ok; + u32 cnr_good; +}; + +/** + * struct vidtv_demod_config - Configuration used to init the demod + * @drop_tslock_prob_on_low_snr: probability of losing the lock due to low snr + * @recover_tslock_prob_on_good_snr: probability of recovering when the signal + * improves + * + * The configuration used to init the demodulator module, usually filled + * by a bridge driver. For vidtv, this is filled by vidtv_bridge before the + * demodulator module is probed. + */ +struct vidtv_demod_config { + u8 drop_tslock_prob_on_low_snr; + u8 recover_tslock_prob_on_good_snr; +}; + +/** + * struct vidtv_demod_state - The demodulator state + * @frontend: The frontend structure allocated by the demod. + * @config: The config used to init the demod. + * @poll_snr: The task responsible for periodically checking the simulated + * signal quality, eventually dropping or reacquiring the TS lock. + * @status: the demod status. + * @cold_start: Whether the demod has not been init yet. + * @poll_snr_thread_running: Whether the task responsible for periodically + * checking the simulated signal quality is running. + * @poll_snr_thread_restart: Whether we should restart the poll_snr task. + */ +struct vidtv_demod_state { + struct dvb_frontend frontend; + struct vidtv_demod_config config; + struct delayed_work poll_snr; + enum fe_status status; + u16 tuner_cnr; + bool cold_start; + bool poll_snr_thread_running; + bool poll_snr_thread_restart; +}; +#endif // VIDTV_DEMOD_H From patchwork Tue Aug 18 22:50:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Almeida X-Patchwork-Id: 256097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, URIBL_SBL, URIBL_SBL_A, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7562DC433E4 for ; Tue, 18 Aug 2020 22:51:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 28E572065D for ; Tue, 18 Aug 2020 22:51:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="sq7Sx8As" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727078AbgHRWvS (ORCPT ); Tue, 18 Aug 2020 18:51:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45994 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726980AbgHRWvC (ORCPT ); Tue, 18 Aug 2020 18:51:02 -0400 Received: from mail-qk1-x742.google.com (mail-qk1-x742.google.com [IPv6:2607:f8b0:4864:20::742]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E280C061342; Tue, 18 Aug 2020 15:51:02 -0700 (PDT) Received: by mail-qk1-x742.google.com with SMTP id 62so19929714qkj.7; Tue, 18 Aug 2020 15:51:02 -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=0ekCKllPOZ8Wr18F4aZIRtwIo8BDZiwXNgOPMTdFB30=; b=sq7Sx8AsOSoDpAdCSb8i19ov1TE1veXMoQc1ITBum9vz8uwAhoB3LmYZGROLS9IiYA aYEO1yDcFyEtNjYUBJV92uMlfU+n9GhjRMOBJxp6fpyevVXPBRXDbRjFqSCxzQdQpss1 FdiIKk+zipUc7C1J5V+oxvq3LCNcSSHa6pO39UnARFK+Hf3KKnGd556aiGxO1yb1fijP YsFxCXiixReoWONbYYjHxtHbOTe1rdlPafO9n5Oc84viCX42XaJhKF0iQIPKfk2FtvBx oNCO7ixz07CBsxFK6CowVOmqfB4/8HxTp16vYk8isvqZdtJ3cAdAYSS/ZAuNx8D6fC30 M0Sw== 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=0ekCKllPOZ8Wr18F4aZIRtwIo8BDZiwXNgOPMTdFB30=; b=DOaYJZ2e4OeWqhd7ZrCpL56cFINbMG/Ee2WslvQeWvEmKk5+LjFZt4aDLbgYtsWnrj tS1mtMtRpyz3KbasHM0eavY4TX9igO+fo0fHd0Zq9w73YBZQTAMIdyUxuGIsVFV4lQ6u iV6698nlq5wFbrAnqDnlw1eDXSIptxGyz3qyv0vVUQyOCA0w9SCvDMUJsXrKfy8A/xu9 DwzOgRDi28K8IJNE6yornKpKq3eHQL1INR2Oyhi8dPiKTjhSiX7yH1uGgIyq6PcUoZiZ zwo8pHBiwb9ZKGJun2g8gIB7IRlW3fQUO4eWVFCoELiBwwj75pATO7i2CDplrZfMFdvo fSdw== X-Gm-Message-State: AOAM531sEzpkGYIe0vkJYyAsqAN/M4pbaV2RjfyHFAnppcaaj5BTc8RH vQfONenOrJYj7mWgopHvimM= X-Google-Smtp-Source: ABdhPJy6sqyO8RaDgJ4D7GRdl1rMz1FQcQGW6+5mVK0r1VL0n0Frz8uZ2efqNQba6cAjapHsZ9PcCQ== X-Received: by 2002:a37:ac8:: with SMTP id 191mr19499917qkk.293.1597791060974; Tue, 18 Aug 2020 15:51:00 -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.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2020 15:51:00 -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 4/4] media: Documentation: vidtv: Add ReST documentation for vidtv Date: Tue, 18 Aug 2020 19:50:41 -0300 Message-Id: <20200818225041.1133770-5-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 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org From: "Daniel W. S. Almeida" Add documentation for the Virtual Digital TV driver (vidtv) in the Restructured Text (ReST) format. This discusses: - What is vidtv - Why vidtv is needed - How to build and run vidtv - How vidtv is structured - How to test vidtv - How to improve vidtv Signed-off-by: Daniel W. S. Almeida --- .../driver-api/media/drivers/index.rst | 1 + .../driver-api/media/drivers/vidtv.rst | 417 ++++++++++++++++++ 2 files changed, 418 insertions(+) create mode 100644 Documentation/driver-api/media/drivers/vidtv.rst -- 2.28.0 diff --git a/Documentation/driver-api/media/drivers/index.rst b/Documentation/driver-api/media/drivers/index.rst index 0df85fc96605..5f340cfdb4cc 100644 --- a/Documentation/driver-api/media/drivers/index.rst +++ b/Documentation/driver-api/media/drivers/index.rst @@ -35,4 +35,5 @@ Digital TV drivers dvb-usb frontends + vidtv contributors diff --git a/Documentation/driver-api/media/drivers/vidtv.rst b/Documentation/driver-api/media/drivers/vidtv.rst new file mode 100644 index 000000000000..303227a67f60 --- /dev/null +++ b/Documentation/driver-api/media/drivers/vidtv.rst @@ -0,0 +1,417 @@ +.. SPDX-License-Identifier: GPL-2.0 + +================================ +vidtv: Virtual Digital TV driver +================================ + +Author: Daniel W. S. Almeida , June 2020. + +Background +---------- + +Vidtv is a virtual DVB driver that aims to serve as a reference for driver +writers by serving as a template. It also validates the existing media DVB +APIs, thus helping userspace application writers. + +Currently, it consists of: + +- A fake tuner driver, which will report a bad signal quality if the chosen + frequency is too far away from a table of valid frequencies for a + particular delivery system. + +- A fake demod driver, which will constantly poll the fake signal quality + returned by the tuner, simulating a device that can lose/reacquire a lock + on the signal depending on the CNR levels. + +- A fake bridge driver, which is the module responsible for modprobing the + fake tuner and demod modules and implementing the demux logic. This module + takes parameters at initialization that will dictate how the simulation + behaves. + +- Code reponsible for encoding a valid MPEG Transport Stream, which is then + passed to the bridge driver. This fake stream contains some hardcoded content. + For now, we have a single, audio-only channel containing a single MPEG + Elementary Stream, which in turn contains a SMPTE 302m encoded sine-wave. + Note that this particular encoder was chosen because it is the easiest + way to encode PCM audio data in a MPEG Transport Stream. + +Building vidtv +-------------- +vidtv is a test driver and thus is **not** enabled by default when +compiling the kernel. + +In order to enable compilation of vidtv: + +- Enable **DVB_TEST_DRIVERS**, then +- Enable **DVB_VIDTV** + +When compiled as a module, expect the following .ko files: + +- dvb_vidtv_tuner.ko + +- dvb_vidtv_demod.ko + +- dvb_vidtv_bridge.ko + +Running vidtv +------------- +When compiled as a module, run:: + + modprobe dvb_vidtv_bridge + +That's it! The bridge driver will initialize the tuner and demod drivers as +part of its own initialization. + +You can optionally define some command-line arguments to vidtv. + +Command-line arguments to vidtv +------------------------------- +Below is a list of all arguments that can be supplied to vidtv: + +drop_tslock_prob_on_low_snr + Probability of losing the TS lock if the signal quality is bad. + This probability be used by the fake demodulator driver to + eventually return a status of 0 when the signal quality is not + good. + +recover_tslock_prob_on_good_snr: + Probability recovering the TS lock when the signal improves. This + probability be used by the fake demodulator driver to eventually + return a status of 0x1f when/if the signal quality improves. + +mock_power_up_delay_msec + Simulate a power up delay. Default: 0. + +mock_tune_delay_msec + Simulate a tune delay. Default 0. + +vidtv_valid_dvb_t_freqs + Valid DVB-T frequencies to simulate. + +vidtv_valid_dvb_c_freqs + Valid DVB-C frequencies to simulate. + +vidtv_valid_dvb_s_freqs + Valid DVB-C frequencies to simulate. + +max_frequency_shift_hz, + Maximum shift in HZ allowed when tuning in a channel. + +si_period_msec + How often to send SI packets. Default: 40ms. + +pcr_period_msec + How often to send PCR packets. Default: 40ms. + +mux_rate_kbytes_sec + Attempt to maintain this bit rate by inserting TS null packets, if + necessary. Default: 4096. + +pcr_pid, + PCR PID for all channels. Default: 0x200. + +mux_buf_sz_pkts, + Size for the mux buffer in multiples of 188 bytes. + +vidtv internal structure +------------------------ +The kernel modules are split in the following way: + +vidtv_tuner.[ch] + Implements a fake tuner DVB driver. + +vidtv_demod.[ch] + Implements a fake demodulator DVB driver. + +vidtv_bridge.[ch] + Implements a bridge driver. + +The MPEG related code is split in the following way: + +vidtv_ts.[ch] + Code to work with MPEG TS packets, such as TS headers, adaptation + fields, PCR packets and NULL packets. + +vidtv_psi.[ch] + This is the PSI generator. PSI packets contain general information + about a MPEG Transport Stream. A PSI generator is needed so + userspace apps can retrieve information about the Transport Stream + and eventually tune into a (dummy) channel. + + Because the generator is implemented in a separate file, it can be + reused elsewhere in the media subsystem. + + Currently vidtv supports working with 3 PSI tables: PAT, PMT and + SDT. + + The specification for PAT and PMT can be found in *ISO 13818-1: + Systems*, while the specification for the SDT can be found in *ETSI + EN 300 468: Specification for Service Information (SI) in DVB + systems*. + + It isn't strictly necessary, but using a real TS file helps when + debugging PSI tables. Vidtv currently tries to replicate the PSI + structure found in this file: `TS1Globo.ts + `_. + + A good way to visualize the structure of streams is by using + `DVBInspector `_. + +vidtv_pes.[ch] + Implements the PES logic to convert encoder data into MPEG TS + packets. These can then be fed into a TS multiplexer and eventually + into userspace. + +vidtv_encoder.h + An interface for vidtv encoders. New encoders can be added to this + driver by implementing the calls in this file. + +vidtv_s302m.[ch] + Implements a S302M encoder to make it possible to insert PCM audio + data in the generated MPEG Transport Stream. The relevant + specification is available online as *SMPTE 302M-2007: Television - + Mapping of AES3 Data into MPEG-2 Transport Stream*. + + + The resulting MPEG Elementary Stream is conveyed in a private + stream with a S302M registration descriptor attached. + + This shall enable passing an audio signal into userspace so it can + be decoded and played by media software. The corresponding decoder + in ffmpeg is located in 'libavcodec/s302m.c' and is experimental. + +vidtv_channel.[ch] + Implements a 'channel' abstraction. + + When vidtv boots, it will create some hardcoded channels: + + #. Their services will be concatenated to populate the SDT. + + #. Their programs will be concatenated to populate the PAT + + #. For each program in the PAT, a PMT section will be created + + #. The PMT section for a channel will be assigned its streams. + + #. Every stream will have its corresponding encoder polled in a + loop to produce TS packets. + These packets may be interleaved by the muxer and then delivered + to the bridge. + +vidtv_mux.[ch] + Implements a MPEG TS mux, loosely based on the ffmpeg + implementation in "libavcodec/mpegtsenc.c" + + The muxer runs a loop which is responsible for: + + #. Keeping track of the amount of time elapsed since the last + iteration. + + #. Polling encoders in order to fetch 'elapsed_time' worth of data. + + #. Inserting PSI and/or PCR packets, if needed. + + #. Padding the resulting stream with NULL packets if + necessary in order to maintain the chosen bit rate. + + #. Delivering the resulting TS packets to the bridge + driver so it can pass them to the demux. + +Testing vidtv with v4l-utils +---------------------------- + +Using the tools in v4l-utils is a great way to test and inspect the output of +vidtv. It is hosted here: `v4l-utils Documentation +`_. + +From its webpage:: + + The v4l-utils are a series of packages for handling media devices. + + It is hosted at http://git.linuxtv.org/v4l-utils.git, and packaged + on most distributions. + + It provides a series of libraries and utilities to be used to + control several aspect of the media boards. + + +Start by installing v4l-utils and then modprobing vidtv:: + + modprobe dvb_vidtv_bridge + +If the driver is OK, it should load and its probing code will run. This will +pull in the tuner and demod drivers. + +Using dvb-fe-tool +~~~~~~~~~~~~~~~~~ + +The first step to check whether the demod loaded successfully is to run:: + + $ dvb-fe-tool + +This should return what is currently set up at the demod struct, i.e.:: + + static const struct dvb_frontend_ops vidtv_demod_ops = { + .delsys = { + SYS_DVBT, + SYS_DVBT2, + SYS_DVBC_ANNEX_A, + SYS_DVBS, + SYS_DVBS2, + }, + + .info = { + .name = "Dummy demod for DVB-T/T2/C/S/S2", + .frequency_min_hz = 51 * MHz, + .frequency_max_hz = 2150 * MHz, + .frequency_stepsize_hz = 62500, + .frequency_tolerance_hz = 29500 * kHz, + .symbol_rate_min = 1000000, + .symbol_rate_max = 45000000, + + .caps = FE_CAN_FEC_1_2 | + FE_CAN_FEC_2_3 | + FE_CAN_FEC_3_4 | + FE_CAN_FEC_4_5 | + FE_CAN_FEC_5_6 | + FE_CAN_FEC_6_7 | + FE_CAN_FEC_7_8 | + FE_CAN_FEC_8_9 | + FE_CAN_QAM_16 | + FE_CAN_QAM_64 | + FE_CAN_QAM_32 | + FE_CAN_QAM_128 | + FE_CAN_QAM_256 | + FE_CAN_QAM_AUTO | + FE_CAN_QPSK | + FE_CAN_FEC_AUTO | + FE_CAN_INVERSION_AUTO | + FE_CAN_TRANSMISSION_MODE_AUTO | + FE_CAN_GUARD_INTERVAL_AUTO | + FE_CAN_HIERARCHY_AUTO, + } + + .... + +For more information on dvb-fe-tools check its online documentation here: +`dvb-fe-tool Documentation +`_. + +Using dvb-scan +~~~~~~~~~~~~~~ + +In order to tune into a channel and read the PSI tables, we can use dvb-scan. + +For this, one should provide a configuration file known as a 'scan file', +here's an example:: + + [Channel] + FREQUENCY = 330000000 + MODULATION = QAM/AUTO + SYMBOL_RATE = 6940000 + INNER_FEC = AUTO + DELIVERY_SYSTEM = DVBC/ANNEX_A + +.. note:: + The parameters depend on the video standard you're testing. + +.. note:: + Vidtv is a fake driver and does not validate much of the information + in the scan file. Just specifying 'FREQUENCY' and 'DELIVERY_SYSTEM' + should be enough for DVB-T/DVB-T2. For DVB-S/DVB-C however, you + should also provide 'SYMBOL_RATE'. + +You can browse scan tables online here: `dvb-scan-tables +`_. + +Assuming this channel is named 'channel.conf', you can then run:: + + $ dvbv5-scan channel.conf + +For more information on dvb-scan, check its documentation online here: +`dvb-scan Documentation `_. + +Using dvb-zap +~~~~~~~~~~~~~ + +dvbv5-zap is a command line tool that can be used to record MPEG-TS to disk. The +typical use is to tune into a channel and put it into record mode. The example +below - which is taken from the documentation - illustrates that:: + + $ dvbv5-zap -c dvb_channel.conf "trilhas sonoras" -r + using demux '/dev/dvb/adapter0/demux0' + reading channels from file 'dvb_channel.conf' + service has pid type 05: 204 + tuning to 573000000 Hz + audio pid 104 + dvb_set_pesfilter 104 + Lock (0x1f) Quality= Good Signal= 100.00% C/N= -13.80dB UCB= 70 postBER= 3.14x10^-3 PER= 0 + DVR interface '/dev/dvb/adapter0/dvr0' can now be opened + +The channel can be watched by playing the contents of the DVR interface, with +some player that recognizes the MPEG-TS format, such as *mplayer* or *vlc*. + +By playing the contents of the stream one can visually inspect the workings of +vidtv, e.g.:: + + $ mplayer /dev/dvb/adapter0/dvr0 + +For more information on dvb-zap check its online documentation here: +`dvb-zap Documentation +`_. +See also: `zap `_. + + +What can still be improved in vidtv +----------------------------------- + +Add *debugfs* integration +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Although frontend drivers provide DVBv5 statistics via the .read_status +call, a nice addition would be to make additional statistics available to +userspace via debugfs, which is a simple-to-use, RAM-based filesystem +specifically designed for debug purposes. + +The logic for this would be implemented on a separate file so as not to +pollute the frontend driver. These statistics are driver-specific and can +be useful during tests. + +The Siano driver is one example of a driver using +debugfs to convey driver-specific statistics to userspace and it can be +used as a reference. + +This should be further enabled and disabled via a Kconfig +option for convenience. + +Add a way to test video +~~~~~~~~~~~~~~~~~~~~~~~ + +Currently, vidtv can only encode PCM audio. It would be great to implement +a barebones version of MPEG-2 video encoding so we can also test video. The +first place to look into is *ISO 13818-2: Information technology — Generic +coding of moving pictures and associated audio information — Part 2: Video*, +which covers the encoding of compressed video in MPEG Transport Streams. + +This might optionally use the Video4Linux2 Test Pattern Generator, v4l2-tpg, +which resides at:: + + drivers/media/common/v4l2-tpg/ + + +Add white noise simulation +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The vidtv tuner already has code to identify whether the chosen frequency +is too far away from a table of valid frequencies. For now, this means that +the demodulator can eventually lose the lock on the signal, since the tuner will +report a bad signal quality. + +A nice addition is to simulate some noise when the signal quality is bad by: + +- Randomly dropping some TS packets. This will trigger a continuity error if the + continuity counter is updated but the packet is not passed on to the demux. + +- Updating the error statistics accordingly (e.g. BER, etc). + +- Simulating some noise in the encoded data.