Skip to contentSkip to conversation

media: vidtv: implement a virtual DVB driver

RFC Superseded Daniel W. S. Almeida 3 participants 22 messages May 20, 2020 – Jun 3, 2020

Subsystems (2)

v6 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Daniel W. S. Almeida

author maintainer
#0 lore raw

v6 cover media: vidtv: implement a virtual DVB driver

From: "Daniel W. S. Almeida" <[email protected]>

This series is work in progress. It represents the current work done on a virtual DVB driver for the Linux media subsystem. I am new to the media subsystem and to kernel development in general.

This driver aims to:
- Serve as template for new DVB driver writers
- Help userspace application writers in general
- Push fake audio/video to userspace for testing purposes
- Push debug information to userspace via debugfs

This should currently be able to feed PSI and PCM audio data to userspace.

I appreciate any feedback!

Changes in v6:
Addressed the following issues:
- Makefile was not actually compiling everything;
- The bridge driver should be a platform driver;
- There are lots of warnings and other errors produced by the driver.

Changes in v5:

Removed all calls to WARN_ON in favor of pr_warn_ratelimited Add a #define for pr_fmt
Reworked the byte swapping logic for big/little endian support:
big endian fields now prepended with __beXX for 'sparse' checks bitfields now laid in reverse order if LITTLE_ENDIAN_BITFIELD
is set

media: vidtv: implement a tuner driver
Return -EINVAL instead of -1 in vidtv_tuner_check_frequency_shift
media: vidtv: implement a demodulator driver
Add POLL_THRD_TIME #define
Implement dvb_frontend_ops as a single struct instead of three
media: vidtv: move config structs into a separate header
Removed this commit, configs are now in vidtv_tuner.h and vidtv_demod.h
media: vidtv: add a bridge driver
module_param: all fs permissions set to 0
removed param 'chosen_delsys'
module_param: removed "optional" string: all module_params are optional renamed vidtv_bridge -> vidtv_bridg: so the source code and module names
are different
media: vidtv: add wrappers for memcpy and memset
Added kernel-docs
write address is now computed internally
media: vidtv: add MPEG TS common code
Drop packets if the buffer is full
media: vidtv: implement a PSI generator
Removed vidtv_psi_ts_psi_write_stuffing()
Forcibly align misaligned buffers
Drop packets if buffer is full
media: vidtv: implement a PES packetizer
Remove vidtv_extract_bits() in favor of GENMASK() and bitwise & Forcibly align misaligned buffers
media: vidtv: Implement a SMPTE 302M encoder
Added kernel-docs for struct vidtv_encoder
media: vidtv: Add a MPEG Transport Stream Multiplexer
Added check for minimum and maximum buffer size.
Drop packets if buffer is full

Changes in v4:
Added a PES packetizer
Implemented a minimum version of the SMPTE 302m encoder for AES3 audio Fixed endianness in the PSI generator, converting fields to big endian where applicable Added a minimal TS mux abstraction

Changes in v3:
Added a bridge driver
Renamed the driver to vidtv
Renamed/reworked commits into smaller pieces
Moved the driver into its own directory
Fixed the code for the signal strength in the tuner
Removed useless enums in the tuner driver (e.g. lock_status, power_status...) Reworked the logic for the poll_snr thread in the demodulator driver Moved MPEG related code to the bridge driver, as it controls the demux logic Changed literals to #defines, used sizeof in place of integer literals when computing the size of PSI structs
Moved the MPEG PSI tables to the heap to reduce stack space usage Now using usleep_range in place of msleep_interruptible in the MPEG TS thread Wrapped memcpy and memset to protect against buffer overflow when writing to the MPEG TS buffer.

Changes in v2:
Attempted not to break assignments into multiple lines as much as possible. Code now passes checkpatch strict mode

media: dvb_dummy_tuner: implement driver skeleton Changed snr values to mili db
Return value from 0-100 to indicate how far off the requested frequency is from a valid one

Use the frequency shift to interpolate between 34dB and 10dB if we can not match against the SNR lookup table
Remove sleep calls for suspend/resume

Fix memcpy call for the config struct

media: dvb_dummy_fe.c: lose TS lock on bad snr Randomly recover the TS lock if the signal quality improves

media: dvb_dummy_fe.c: write PSI information into DMX buffer Split the patch into multiple header/source files

Hexadecimal literals are now lower case

Prefer short function names / reduce function signatures

Add #defines for constants when computing section lengths

Change signature for functions that take a dummy channel as argument (i.e. channels* is now channels[NUM_CHANNELS])

Daniel W. S. Almeida (10):
media: vidtv: add Kconfig entry
media: vidtv: implement a tuner driver
media: vidtv: implement a demodulator driver
media: vidtv: add a bridge driver
media: vidtv: add wrappers for memcpy and memset
media: vidtv: add MPEG TS common code
media: vidtv: implement a PSI generator
media: vidtv: implement a PES packetizer
media: vidtv: Implement a SMPTE 302M encoder
media: vidtv: Add a MPEG Transport Stream Multiplexer

 arch/Kconfig                                  |    2 +-
 drivers/media/dvb-core/dvbdev.c               |    1 +
 drivers/media/test-drivers/Kconfig            |   10 +
 drivers/media/test-drivers/Makefile           |    1 +
 drivers/media/test-drivers/vidtv/Kconfig      |   11 +
 drivers/media/test-drivers/vidtv/Makefile     |    9 +
 .../media/test-drivers/vidtv/vidtv_bridge.c   |  511 ++++++++
 .../media/test-drivers/vidtv/vidtv_bridge.h   |   41 +
 .../media/test-drivers/vidtv/vidtv_channel.c  |  339 ++++++
 .../media/test-drivers/vidtv/vidtv_channel.h  |   66 ++
 .../media/test-drivers/vidtv/vidtv_common.c   |   86 ++
 .../media/test-drivers/vidtv/vidtv_common.h   |   34 +
 .../media/test-drivers/vidtv/vidtv_demod.c    |  444 +++++++
 .../media/test-drivers/vidtv/vidtv_demod.h    |   41 +
 .../media/test-drivers/vidtv/vidtv_encoder.h  |  103 ++
 drivers/media/test-drivers/vidtv/vidtv_mux.c  |  434 +++++++
 drivers/media/test-drivers/vidtv/vidtv_mux.h  |  105 ++
 drivers/media/test-drivers/vidtv/vidtv_pes.c  |  450 +++++++
 drivers/media/test-drivers/vidtv/vidtv_pes.h  |  186 +++
 drivers/media/test-drivers/vidtv/vidtv_psi.c  | 1055 +++++++++++++++++
 drivers/media/test-drivers/vidtv/vidtv_psi.h  |  420 +++++++
 .../media/test-drivers/vidtv/vidtv_s302m.c    |  636 ++++++++++
 .../media/test-drivers/vidtv/vidtv_s302m.h    |  119 ++
 drivers/media/test-drivers/vidtv/vidtv_ts.c   |  157 +++
 drivers/media/test-drivers/vidtv/vidtv_ts.h   |  120 ++
 .../media/test-drivers/vidtv/vidtv_tuner.c    |  408 +++++++
 .../media/test-drivers/vidtv/vidtv_tuner.h    |   26 +
 27 files changed, 5814 insertions(+), 1 deletion(-)
 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_bridge.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_encoder.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.h
Signature · 1 line
2.26.2
author maintainer
#1 lore raw

RFC v6 1/10 media: vidtv: add Kconfig entry

From: "Daniel W. S. Almeida" <[email protected]>

Add the necessary Kconfig entries and a dummy Makefile to compile the new virtual DVB test driver (vidtv).

---
 drivers/media/test-drivers/Kconfig        | 10 ++++++++++
 drivers/media/test-drivers/Makefile       |  1 +
 drivers/media/test-drivers/vidtv/Kconfig  | 11 +++++++++++
 drivers/media/test-drivers/vidtv/Makefile |  2 ++
 4 files changed, 24 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/Kconfig
 create mode 100644 drivers/media/test-drivers/vidtv/Makefile
drivers/media/test-drivers/Kconfig +10 -0
index 188381c855939..7d273a8a7acc2 100644
@@ -4,6 +4,10 @@ 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
+
 if V4L_TEST_DRIVERS
 
 source "drivers/media/test-drivers/vimc/Kconfig"
@@ -24,3 +28,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
drivers/media/test-drivers/Makefile +1 -0
index 74410d3a9f2d2..9f0e4ebb2efe7 100644
@@ -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/
drivers/media/test-drivers/vidtv/Kconfig new file +11 -0
new file mode 100644
index 0000000000000..22c4fd39461f1
@@ -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.
drivers/media/test-drivers/vidtv/Makefile new file +2 -0
new file mode 100644
index 0000000000000..d1558d84eeaed
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+
Signature · 6 lines
2.26.2

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#2 lore raw

Re: [RFC, WIP, v6 01/10] media: vidtv: add Kconfig entry

Quoted 55 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Add the necessary Kconfig entries and a dummy Makefile to compile the new virtual DVB test driver (vidtv).

---
  drivers/media/test-drivers/Kconfig        | 10 ++++++++++
  drivers/media/test-drivers/Makefile       |  1 +
  drivers/media/test-drivers/vidtv/Kconfig  | 11 +++++++++++
  drivers/media/test-drivers/vidtv/Makefile |  2 ++
  4 files changed, 24 insertions(+)

create mode 100644 drivers/media/test-drivers/vidtv/Kconfig create mode 100644 drivers/media/test-drivers/vidtv/Makefile

drivers/media/test-drivers/Kconfig +10 -0
index 188381c855939..7d273a8a7acc2 100644
@@ -4,6 +4,10 @@ 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
+
  if V4L_TEST_DRIVERS
 
  source "drivers/media/test-drivers/vimc/Kconfig"
@@ -24,3 +28,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
drivers/media/test-drivers/Makefile +1 -0
index 74410d3a9f2d2..9f0e4ebb2efe7 100644
@@ -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/
drivers/media/test-drivers/vidtv/Kconfig new file +0 -0
new file mode 100644
index 0000000000000..22c4fd39461f1

drivers/media/test-drivers/vidtv/Kconfig line 5

Quoted 5 lines
drivers/media/test-drivers/vidtv/Kconfig new file +5 -0
@@ -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

Add one line summary/name for the driver "Virtual DVB test driver" is a good description.

drivers/media/test-drivers/vidtv/Kconfig line 9

Quoted 4 lines
drivers/media/test-drivers/vidtv/Kconfig new file +4 -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.
+

Wrapped lines. Split this to read better.

Take a look at others e.g: drivers/media/test-drivers/vimc/Kconfig for reference.

drivers/media/test-drivers/vidtv/Makefile line 2

Quoted 11 lines
drivers/media/test-drivers/vidtv/Kconfig new file +2 -0
+
+	  When in doubt, say N.
drivers/media/test-drivers/vidtv/Makefile new file +2 -0
new file mode 100644
index 0000000000000..d1558d84eeaed
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+

btw. Did you test it with just this patch?

thanks,
-- Shuah
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author maintainer
#3 lore raw

RFC v6 2/10 media: vidtv: implement a tuner driver

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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.

---
 drivers/media/test-drivers/vidtv/Makefile     |   3 +
 .../media/test-drivers/vidtv/vidtv_tuner.c    | 408 ++++++++++++++++++
 .../media/test-drivers/vidtv/vidtv_tuner.h    |  26 ++
 3 files changed, 437 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.h
drivers/media/test-drivers/vidtv/Makefile +3 -0
index d1558d84eeaed..58e022c094e5e 100644
@@ -1,2 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 
+dvb-vidtv-tuner-objs := vidtv_tuner.o
+
+obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +408 -0

408 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_tuner.h new file +26 -0
new file mode 100644
index 0000000000000..46407739f5d44
@@ -0,0 +1,26 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_TUNER_H
+#define VIDTV_TUNER_H
+
+#include <linux/types.h>
+#include <media/dvb_frontend.h>
+
+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[8];
+	u32 vidtv_valid_dvb_c_freqs[8];
+	u32 vidtv_valid_dvb_s_freqs[8];
+	u8  max_frequency_shift_hz;
+};
+
+#endif //VIDTV_TUNER_H
Signature · 1 line
2.26.2
#4 lore raw

Re: [RFC, WIP, v6 02/10] media: vidtv: implement a tuner driver

Quoted 1 line · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Are you using git send-email to send patches. Why do have this From line in here? Same comment on all other patches in this series.

Quoted 33 lines

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.

---
  drivers/media/test-drivers/vidtv/Makefile     |   3 +
  .../media/test-drivers/vidtv/vidtv_tuner.c    | 408 ++++++++++++++++++
  .../media/test-drivers/vidtv/vidtv_tuner.h    |  26 ++
  3 files changed, 437 insertions(+)

create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.h

drivers/media/test-drivers/vidtv/Makefile +3 -0
index d1558d84eeaed..58e022c094e5e 100644
@@ -1,2 +1,5 @@
  # SPDX-License-Identifier: GPL-2.0
 
+dvb-vidtv-tuner-objs := vidtv_tuner.o
+
+obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +0 -0
new file mode 100644
index 0000000000000..3f2b4b9a6d568

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 10

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -0
@@ -0,0 +1,408 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>

Use Copyright (C) 2020 Daniel W. S. Almeida <[email protected]>

Check other drivers for reference. Do the same in all other files in this series.

Quoted 14 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +14 -0
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <media/dvb_frontend.h>
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+
+#include "vidtv_tuner.h"

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 34

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -0
+
+MODULE_DESCRIPTION("Virtual DVB Tuner");
+MODULE_AUTHOR("Daniel W. S. Almeida");
+MODULE_LICENSE("GPL");
+
+struct vidtv_tuner_cnr_to_qual_s {
+	/* attempt to use the same values as libdvbv5 */
+	u32 modulation;
+	u32 fec;
+	u32 cnr_ok, cnr_good;

Add cnr_good on a separate line.

	u32 cnr_good;

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 35

Quoted 1 line
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +1 -0
+};

Add a comment for each of these fields describing
what they are for.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 46

Quoted 11 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +11 -0
+
+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},
+

Any reason for this blank line here?

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 50

Quoted 4 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +4 -0
+	{ QPSK, FEC_2_3,  9000, 12000},
+	{ QPSK, FEC_3_4, 10000, 13000},
+	{ QPSK, FEC_5_6, 11000, 14000},
+

Any reason for this blank line here?

Quoted 36 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +36 -0
+	{ 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},

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 96

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -0
+};
+
+struct vidtv_tuner_hardware_state {
+	bool asleep;
+	u32 lock_status;
+	u32 if_frequency;
+	u32 tuned_frequency;
+	u32 bandwidth;
+};
+

Add a comment for each of these fields describing
what they are for.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 102

Quoted 6 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +6 -0
+struct vidtv_tuner_dev {
+	struct dvb_frontend *fe;
+	struct vidtv_tuner_hardware_state hw_state;
+	struct vidtv_tuner_config config;
+};
+

Add a comment for each of these fields describing
what they are for.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 108

Quoted 6 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +6 -0
+static struct vidtv_tuner_dev*
+vidtv_tuner_get_dev(struct dvb_frontend *fe)
+{
+	struct i2c_client *client = fe->tuner_priv;
+
+	return i2c_get_clientdata(client);

Why not simplfy this to just return i2c_get_clientdata(fe->tuner_priv) Safer to check if tuner_priv is valid.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 119

Quoted 11 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +11 -0
+}
+
+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;

Align i and shift with the rest of the variables.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 121

Quoted 2 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +2 -0
+
+	switch (c->delivery_system) {

Add indetation for the below:

Quoted 35 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +35 -0
+	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)

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 166

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -0
+			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;

Why do you need white spaces between *c and assignment?

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 168

Quoted 2 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +2 -0
+	const struct vidtv_tuner_cnr_to_qual_s *cnr2qual = NULL;
+	u32    array_size                          = 0;

Same here. Don't align them when it doesn't make sense. This makes it not readable.

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 174

Quoted 6 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +6 -0
+	s32 shift;
+	u32 i;
+
+	shift = vidtv_tuner_check_frequency_shift(fe);
+
+	switch (c->delivery_system) {

Ident.

Quoted 58 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +58 -0
+	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;
+}

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 242

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -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;

There is no need to align these.

Quoted 23 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +23 -0
+
+	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);

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 275

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +10 -0
+
+	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;

Add a blank line and don't try to align these. Looks very odd.

Quoted 151 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +133 -0
+	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[] = {
+	{"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                = "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);
drivers/media/test-drivers/vidtv/vidtv_tuner.h new file +12 -0
new file mode 100644
index 0000000000000..46407739f5d44
@@ -0,0 +1,26 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_TUNER_H
+#define VIDTV_TUNER_H
+

drivers/media/test-drivers/vidtv/vidtv_tuner.h line 22

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.h new file +10 -0
+#include <linux/types.h>
+#include <media/dvb_frontend.h>
+
+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[8];
+	u32 vidtv_valid_dvb_c_freqs[8];
+	u32 vidtv_valid_dvb_s_freqs[8];

Don't hrd code these. Add defines for them.

drivers/media/test-drivers/vidtv/vidtv_tuner.h line 26

Quoted 5 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.h new file +4 -0
+	u8  max_frequency_shift_hz;
+};
+
+#endif //VIDTV_TUNER_H

thanks,
-- Shuah

author maintainer
#5 lore raw

Re: [RFC, WIP, v6 02/10] media: vidtv: implement a tuner driver

Hi Shuah, thanks for reviewing this!

Quoted 4 lines
Quoted 1 line

From: "Daniel W. S. Almeida" <[email protected]>

Are you using git send-email to send patches. Why do have this From line in here? Same comment on all other patches in this series.

I am using 'git format-patch --to=[...] --cc=[...]' and then 'git send-email'.

Git format-patch usually adds these two lines for every patch it generates:
1 From f966b0ea574a36b96ddfb8a8a716b174bc150eba Mon Sep 17 00:00:00 2001 2 From: "Daniel W. S. Almeida" <[email protected]>

I have no idea whether these are necessary, so I just leave them there :) The first line is specially weird.

As for this:

Quoted 9 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	switch (c->delivery_system) {

Add indetation for the below:

drivers/media/test-drivers/vidtv/vidtv_tuner.c line 126

Quoted 5 lines
drivers/media/test-drivers/vidtv/vidtv_tuner.c new file +5 -0
+	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;

I am not sure I understand. Indenting the case statements leads to a checkpatch complaint:

Quoted 2 lines

SWITCH_CASE_INDENT_LEVEL: switch and case should be at the same indent

And the actual statements under 'case' are already indented.

Thanks
- Daniel
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author maintainer
#6 lore raw

RFC v6 3/10 media: vidtv: implement a demodulator driver

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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.

---
 drivers/media/test-drivers/vidtv/Makefile     |   3 +-
 .../media/test-drivers/vidtv/vidtv_demod.c    | 444 ++++++++++++++++++
 .../media/test-drivers/vidtv/vidtv_demod.h    |  41 ++
 3 files changed, 487 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
drivers/media/test-drivers/vidtv/Makefile +2 -1
index 58e022c094e5e..21e50c11c94d0 100644
@@ -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
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +444 -0

444 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_demod.h new file +41 -0

41 lines not on this page · raw message

Signature · 1 line
2.26.2
#7 lore raw

Re: [RFC, WIP, v6 03/10] media: vidtv: implement a demodulator driver

Quoted 106 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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.

---
  drivers/media/test-drivers/vidtv/Makefile     |   3 +-
  .../media/test-drivers/vidtv/vidtv_demod.c    | 444 ++++++++++++++++++
  .../media/test-drivers/vidtv/vidtv_demod.h    |  41 ++
  3 files changed, 487 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

drivers/media/test-drivers/vidtv/Makefile +2 -1
index 58e022c094e5e..21e50c11c94d0 100644
@@ -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
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +70 -0
new file mode 100644
index 0000000000000..15912e063cf5a
@@ -0,0 +1,444 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ * Based on the example driver written by Emard <[email protected]>
+ */
+
+#include <linux/errno.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/printk.h>
+#include <linux/random.h>
+#include <linux/ratelimit.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/workqueue.h>
+#include <media/dvb_frontend.h>
+
+#include "vidtv_demod.h"
+
+// FIXME: just added a random value here
+#define POLL_THRD_TIME 10 /* 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},

drivers/media/test-drivers/vidtv/vidtv_demod.c line 80

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +10 -0
+
+	{ 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)

Looks very odd:

Move vidtv_match_cnr_s up and:
static const struct vidtv_demod_cnr_to_qual_s
vidtv_match_cnr_s(struct dvb_frontend *fe)

is better.

drivers/media/test-drivers/vidtv/vidtv_demod.c line 84

Quoted 4 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +4 -0
+{
+	struct dtv_frontend_properties *c;
+	const struct vidtv_demod_cnr_to_qual_s *cnr2qual = NULL;
+	u32    array_size                          = 0;

This looks really odd. Don't align them. Align them if it makes sense. It doesn't here.

drivers/media/test-drivers/vidtv/vidtv_demod.c line 89

Quoted 5 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +5 -0
+	u32 i;
+
+	c = &fe->dtv_property_cache;
+
+	switch (c->delivery_system) {

Indentation

Quoted 259 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +259 -0
+	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,

drivers/media/test-drivers/vidtv/vidtv_demod.c line 358

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +10 -0
+
+		.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_FEC_AUTO |

Here is the first one.

drivers/media/test-drivers/vidtv/vidtv_demod.c line 366

Quoted 8 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +8 -0
+			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 |

Duplicate entry? Cut and paste error perhaps.

Quoted 84 lines
drivers/media/test-drivers/vidtv/vidtv_demod.c new file +78 -0
+			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[] = {
+	{"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                = "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");
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +0 -0
new file mode 100644
index 0000000000000..b14957d9d4ea0

drivers/media/test-drivers/vidtv/vidtv_demod.h line 7

Quoted 7 lines
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +7 -0
@@ -0,0 +1,41 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>

Change it to Copyright. Same comment on all files in this series.

Quoted 4 lines
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +4 -0
+ * Based on the example driver written by Emard <[email protected]>
+ */
+
+#ifndef VIDTV_DEMOD_H

drivers/media/test-drivers/vidtv/vidtv_demod.h line 21

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +10 -0
+#define VIDTV_DEMOD_H
+
+#include <linux/dvb/frontend.h>
+#include <media/dvb_frontend.h>
+
+struct vidtv_demod_cnr_to_qual_s {
+	/* attempt to use the same values as libdvbv5 */
+	u32 modulation;
+	u32 fec;
+	u32 cnr_ok, cnr_good;

Move cnr_good to a separate line. Describe these variables. Whatare they for and what they do.

Quoted 11 lines
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +10 -0
+};
+
+struct vidtv_demod_config {
+	/* probability of losing the lock due to low snr */
+	u8 drop_tslock_prob_on_low_snr;
+	/* probability of recovering when the signal improves */
+	u8 recover_tslock_prob_on_good_snr;
+};
+
+struct vidtv_demod_state {

drivers/media/test-drivers/vidtv/vidtv_demod.h line 41

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_demod.h new file +10 -0
+	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

thanks,
-- Shuah
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

author maintainer
#8 lore raw

RFC v6 4/10 media: vidtv: add a bridge driver

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Digital TV devices consist of several independent hardware components which are controlled by different drivers.
Each media device is controlled by a group of cooperating drivers with the bridge driver as the main driver.

This patch adds a bridge driver for the Virtual Digital TV driver [vidtv].

The bridge driver binds to the other drivers, that is, vidtv_tuner and vidtv_demod and implements the digital demux logic, providing userspace with a MPEG Transport Stream.

Move config structs to a common header so they can be used by the bridge driver and by their respective drivers.

BRIDGE WIP

---
 arch/Kconfig                                  |   2 +-
 drivers/media/dvb-core/dvbdev.c               |   1 +
 drivers/media/test-drivers/vidtv/Makefile     |   4 +-
 .../media/test-drivers/vidtv/vidtv_bridge.c   | 435 ++++++++++++++++++
 .../media/test-drivers/vidtv/vidtv_bridge.h   |  39 ++
 5 files changed, 479 insertions(+), 2 deletions(-)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.h
arch/Kconfig +1 -1
index 786a85d4ad40d..ddcb4a68ee940 100644
@@ -868,7 +868,7 @@ config VMAP_STACK
 	  be enabled.
 
 config ARCH_OPTIONAL_KERNEL_RWX
-	def_bool n
+	def_bool y
 
 config ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
 	def_bool n
drivers/media/dvb-core/dvbdev.c +1 -0
index 80b6a71aa33e4..38c8b848c921f 100644
@@ -962,6 +962,7 @@ int dvb_usercopy(struct file *file,
 }
 
 #if IS_ENABLED(CONFIG_I2C)
+#pragma clang optimize off
 struct i2c_client *dvb_module_probe(const char *module_name,
 				    const char *name,
 				    struct i2c_adapter *adap,
drivers/media/test-drivers/vidtv/Makefile +3 -1
index 21e50c11c94d0..c99cd13f4adaf 100644
@@ -2,5 +2,7 @@
 
 dvb-vidtv-tuner-objs := vidtv_tuner.o
 dvb-vidtv-demod-objs := vidtv_demod.o
+dvb-vidtv-bridge-objs := vidtv_bridge.o
 
-obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o dvb-vidtv-demod.o
+obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o dvb-vidtv-demod.o \
+			   dvb-vidtv-bridge.o
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +435 -0

435 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_bridge.h new file +39 -0
new file mode 100644
index 0000000000000..4931bfb73273c
@@ -0,0 +1,39 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_BRIDGE_H
+#define VIDTV_BRIDGE_H
+
+#define NUM_FE 1
+
+#include <linux/i2c.h>
+#include <linux/platform_device.h>
+#include <linux/types.h>
+#include <media/dmxdev.h>
+#include <media/dvb_demux.h>
+#include <media/dvb_frontend.h>
+
+struct vidtv_dvb {
+	struct platform_device *pdev;
+	struct dvb_frontend *fe[NUM_FE];
+	struct dvb_adapter adapter;
+	struct dvb_demux demux;
+	struct dmxdev dmx_dev;
+	struct dmx_frontend dmx_fe[NUM_FE];
+	struct i2c_adapter i2c_adapter;
+	struct i2c_client *i2c_client_demod[NUM_FE];
+	struct i2c_client *i2c_client_tuner[NUM_FE];
+
+	u32 nfeeds;
+	struct mutex feed_lock; /* start/stop feed */
+
+	bool streaming;
+};
+
+#endif // VIDTV_BRIDG_H
Signature · 1 line
2.26.2
#9 lore raw

Re: [RFC, WIP, v6 04/10] media: vidtv: add a bridge driver

[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi "Daniel,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on linuxtv-media/master]
[also build test WARNING on next-20200519]
[cannot apply to linus/master linux/master v5.7-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Daniel-W-S-Almeida/media-vidtv-implement-a-virtual-DVB-driver/20200520-232710
base:   git://linuxtv.org/media_tree.git master
config: i386-tinyconfig (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> arch/x86/mm/init_32.c:872:6: warning: no previous prototype for 'mark_rodata_ro' [-Wmissing-prototypes]

void mark_rodata_ro(void)
^~~~~~~~~~~~~~

vim +/mark_rodata_ro +872 arch/x86/mm/init_32.c

5bd5a452662bc37 arch/x86/mm/init_32.c Matthieu CASTET 2010-11-16 871

63aaf3086baea7b arch/i386/mm/init.c   Arjan van de Ven 2006-01-06 @872  void mark_rodata_ro(void)
63aaf3086baea7b arch/i386/mm/init.c   Arjan van de Ven 2006-01-06  873  {
6fb14755a676282 arch/i386/mm/init.c   Jan Beulich      2007-05-02  874  	unsigned long start = PFN_ALIGN(_text);
2a25dc7c79c92c6 arch/x86/mm/init_32.c Thomas Gleixner  2018-09-17  875  	unsigned long size = (unsigned long)__end_rodata - start;
63aaf3086baea7b arch/i386/mm/init.c   Arjan van de Ven 2006-01-06  876  
6d238cc4dc8a36a arch/x86/mm/init_32.c Arjan van de Ven 2008-01-30  877  	set_pages_ro(virt_to_page(start), size >> PAGE_SHIFT);
2a25dc7c79c92c6 arch/x86/mm/init_32.c Thomas Gleixner  2018-09-17  878  	pr_info("Write protecting kernel text and read-only data: %luk\n",
d7d119d777d7ed2 arch/x86/mm/init_32.c Ingo Molnar      2008-01-30  879  		size >> 10);
0c42f392767d359 arch/x86/mm/init_32.c Andi Kleen       2008-01-30  880  
16239630974516a arch/x86/mm/init_32.c Steven Rostedt   2009-02-17  881  	kernel_set_to_readonly = 1;
16239630974516a arch/x86/mm/init_32.c Steven Rostedt   2009-02-17  882  

:::::: The code at line 872 was first introduced by commit
:::::: 63aaf3086baea7b94c218053af8237f9dbac5d05 [PATCH] x86/x86_64: mark rodata section read only: x86 parts

:::::: TO: Arjan van de Ven <[email protected]>
:::::: CC: Linus Torvalds <[email protected]>

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#10 lore raw

Re: [RFC, WIP, v6 04/10] media: vidtv: add a bridge driver

In reply to Daniel W. S. Almeida
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi "Daniel,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on linuxtv-media/master]
[also build test WARNING on next-20200519]
[cannot apply to linus/master linux/master v5.7-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Daniel-W-S-Almeida/media-vidtv-implement-a-virtual-DVB-driver/20200520-232710
base:   git://linuxtv.org/media_tree.git master
config: x86_64-defconfig (attached as .config)
compiler: gcc-7 (Ubuntu 7.5.0-6ubuntu2) 7.5.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>, old ones prefixed by <<):

>> arch/x86/mm/init_64.c:1264:6: warning: no previous prototype for 'mark_rodata_ro' [-Wmissing-prototypes]

void mark_rodata_ro(void)
^~~~~~~~~~~~~~

vim +/mark_rodata_ro +1264 arch/x86/mm/init_64.c

16239630974516a arch/x86/mm/init_64.c Steven Rostedt 2009-02-17 1263

67df197b1a07944 arch/x86_64/mm/init.c Arjan van de Ven 2006-01-06 @1264  void mark_rodata_ro(void)
67df197b1a07944 arch/x86_64/mm/init.c Arjan van de Ven 2006-01-06  1265  {
74e081797bd9d2a arch/x86/mm/init_64.c Suresh Siddha    2009-10-14  1266  	unsigned long start = PFN_ALIGN(_text);
fc8d782677f163d arch/x86/mm/init_64.c Alexander Duyck  2012-11-16  1267  	unsigned long rodata_start = PFN_ALIGN(__start_rodata);
2d0004d19829c84 arch/x86/mm/init_64.c Kees Cook        2019-10-29  1268  	unsigned long end = (unsigned long)__end_rodata_hpage_align;
2d0004d19829c84 arch/x86/mm/init_64.c Kees Cook        2019-10-29  1269  	unsigned long text_end = PFN_ALIGN(_etext);
2d0004d19829c84 arch/x86/mm/init_64.c Kees Cook        2019-10-29  1270  	unsigned long rodata_end = PFN_ALIGN(__end_rodata);
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1271  	unsigned long all_end;
8f0f996e80b980f arch/x86/mm/init_64.c Steven Rostedt   2008-05-12  1272  
6fb14755a676282 arch/x86_64/mm/init.c Jan Beulich      2007-05-02  1273  	printk(KERN_INFO "Write protecting the kernel read-only data: %luk\n",
e3ebadd95cb621e arch/x86_64/mm/init.c Linus Torvalds   2007-05-07  1274  	       (end - start) >> 10);
984bb80d94d8915 arch/x86/mm/init_64.c Arjan van de Ven 2008-02-06  1275  	set_memory_ro(start, (end - start) >> PAGE_SHIFT);
984bb80d94d8915 arch/x86/mm/init_64.c Arjan van de Ven 2008-02-06  1276  
16239630974516a arch/x86/mm/init_64.c Steven Rostedt   2009-02-17  1277  	kernel_set_to_readonly = 1;
16239630974516a arch/x86/mm/init_64.c Steven Rostedt   2009-02-17  1278  
984bb80d94d8915 arch/x86/mm/init_64.c Arjan van de Ven 2008-02-06  1279  	/*
72212675d1c96f5 arch/x86/mm/init_64.c Yinghai Lu       2013-01-24  1280  	 * The rodata/data/bss/brk section (but not the kernel text!)
72212675d1c96f5 arch/x86/mm/init_64.c Yinghai Lu       2013-01-24  1281  	 * should also be not-executable.
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1282  	 *
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1283  	 * We align all_end to PMD_SIZE because the existing mapping
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1284  	 * is a full PMD. If we would align _brk_end to PAGE_SIZE we
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1285  	 * split the PMD and the reminder between _brk_end and the end
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1286  	 * of the PMD will remain mapped executable.
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1287  	 *
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1288  	 * Any PMD which was setup after the one which covers _brk_end
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1289  	 * has been zapped already via cleanup_highmem().
984bb80d94d8915 arch/x86/mm/init_64.c Arjan van de Ven 2008-02-06  1290  	 */
45e2a9d4701d8c6 arch/x86/mm/init_64.c Kees Cook        2014-11-14  1291  	all_end = roundup((unsigned long)_brk_end, PMD_SIZE);
ab76f7b4ab2397f arch/x86/mm/init_64.c Stephen Smalley  2015-10-01  1292  	set_memory_nx(text_end, (all_end - text_end) >> PAGE_SHIFT);
67df197b1a07944 arch/x86_64/mm/init.c Arjan van de Ven 2006-01-06  1293  

:::::: The code at line 1264 was first introduced by commit
:::::: 67df197b1a07944c2e0e40ded3d4fd07d108e110 [PATCH] x86/x86_64: mark rodata section read-only: x86-64 support

:::::: TO: Arjan van de Ven <[email protected]>
:::::: CC: Linus Torvalds <[email protected]>

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#11 lore raw

Re: [RFC, WIP, v6 04/10] media: vidtv: add a bridge driver

In reply to Daniel W. S. Almeida

Hi Daniel,

Quoted 26 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Digital TV devices consist of several independent hardware components which are controlled by different drivers.
Each media device is controlled by a group of cooperating drivers with the bridge driver as the main driver.

This patch adds a bridge driver for the Virtual Digital TV driver [vidtv].

The bridge driver binds to the other drivers, that is, vidtv_tuner and vidtv_demod and implements the digital demux logic, providing userspace with a MPEG Transport Stream.

Move config structs to a common header so they can be used by the bridge driver and by their respective drivers.

BRIDGE WIP

---
  arch/Kconfig                                  |   2 +-
  drivers/media/dvb-core/dvbdev.c               |   1 +
  drivers/media/test-drivers/vidtv/Makefile     |   4 +-
  .../media/test-drivers/vidtv/vidtv_bridge.c   | 435 ++++++++++++++++++
  .../media/test-drivers/vidtv/vidtv_bridge.h   |  39 ++
  5 files changed, 479 insertions(+), 2 deletions(-)
Quoted 3 lines

create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.h

I ran a test and does't look like you are releasing demod and tuner module when removing dvb_vidtv_bridge. Probe for these happens when

After modprobe dvb_vidtv_bridge and lsmod shows all 3 modules.

dvb_vidtv_demod        16384  0
dvb_vidtv_bridge       45056  0
dvb_core              143360  1 dvb_vidtv_bridge

After rmmod dvb_vidtv_bridge, lsmod shows

dvb_vidtv_tuner        16384  0
dvb_vidtv_demod        16384  0

vidtv_bridge_remove() doesn't seem to cleaning up completely and releasing thes modules vidtv_bridge_init() loads. Also it has to delete i2c adapter that is added in vidtv_bridge_init(). Call to i2c_del_adapter() is miising in vidtv_bridge_remove()

Please see commnets inline after vidtv_bridge_remove().

After a couple of modprobe and rmmods on dvb_vidtv_bridge, I see paninc after modprobe.

[ 3651.863602] dvbdev: DVB: registering new adapter (dvb_vidtv_bridge)
[ 3821.179081] dvbdev: DVB: registering new adapter (dvb_vidtv_bridge)
[ 4087.198087] dvbdev: DVB: registering new adapter (dvb_vidtv_bridge)
[ 4087.202732] i2c i2c-10: DVB: registering adapter 0 frontend 0 (Dummy demod for DVB-T/T2/C/S/S2)...
[ 4087.203112] BUG: kernel NULL pointer dereference, address: 0000000000000000
[ 4087.203140] #PF: supervisor read access in kernel mode
[ 4087.203142] #PF: error_code(0x0000) - not-present page
[ 4087.203144] PGD 0 P4D 0
[ 4087.203147] Oops: 0000 [#1] SMP NOPTI
[ 4087.203150] CPU: 6 PID: 1875 Comm: modprobe Not tainted 5.7.0+ #3
[ 4087.203151] Hardware name: LENOVO 10VGCTO1WW/3130, BIOS M1XKT45A 08/21/2019
[ 4087.203157] RIP: 0010:strlen+0x0/0x20
[ 4087.203160] Code: 48 89 f9 74 09 48 83 c1 01 80 39 00 75 f7 31 d2 44 0f b6 04 16 44 88 04 11 48 83 c2 01 45 84 c0 75 ee c3 0f 1f 80 00 00 00 00 <80> 3f 00 74 10 48 89 f8 48 83 c0 01 80 38 00 75 f7 48 29 f8 c3 31
[ 4087.203162] RSP: 0018:ffffa604c090b940 EFLAGS: 00010246
[ 4087.203164] RAX: ffff9a5188735cc0 RBX: ffff9a5188735c40 RCX: 0000000000000000
[ 4087.203165] RDX: 0000000000000000 RSI: ffffffffc10d4fd8 RDI: 0000000000000000
[ 4087.203167] RBP: ffffa604c090b9b0 R08: ffff9a51b7bb20a0 R09: ffff9a51b6807640
[ 4087.203168] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9a5188735cc0
[ 4087.203170] R13: 0000000000000000 R14: ffff9a51acd74710 R15: ffff9a519531f000
[ 4087.203172] FS:  00007fd38f2b1540(0000) GS:ffff9a51b7b80000(0000) knlGS:0000000000000000
[ 4087.203174] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 4087.203175] CR2: 0000000000000000 CR3: 00000001ffc50000 CR4: 00000000003406e0
[ 4087.203177] Call Trace:
[ 4087.203191]  ? vidtv_channels_init+0x79/0x130 [dvb_vidtv_bridge]
[ 4087.203197]  vidtv_mux_init+0xa6/0x1a0 [dvb_vidtv_bridge]
[ 4087.203201]  vidtv_bridge_probe+0x397/0x400 [dvb_vidtv_bridge]
[ 4087.203205]  ? vidtv_bridge_remove+0x80/0x80 [dvb_vidtv_bridge]
[ 4087.203210]  platform_drv_probe+0x3b/0x80
[ 4087.203213]  really_probe+0x2b3/0x3e0
[ 4087.203216]  driver_probe_device+0xbc/0x100
[ 4087.203218]  device_driver_attach+0x5d/0x70
[ 4087.203220]  __driver_attach+0x8f/0x150
[ 4087.203222]  ? device_driver_attach+0x70/0x70
[ 4087.203225]  bus_for_each_dev+0x7e/0xc0
[ 4087.203227]  driver_attach+0x1e/0x20
[ 4087.203229]  bus_add_driver+0x152/0x1f0
[ 4087.203232]  driver_register+0x74/0xd0
[ 4087.203234]  __platform_driver_register+0x36/0x40
[ 4087.203238]  vidtv_bridge_init+0x31/0x1000 [dvb_vidtv_bridge]
[ 4087.203240]  ? 0xffffffffc0a4a000
[ 4087.203245]  do_one_initcall+0x4a/0x200
[ 4087.203248]  ? kfree+0x231/0x250
[ 4087.203252]  ? _cond_resched+0x19/0x30
[ 4087.203254]  ? kmem_cache_alloc_trace+0x16c/0x240
[ 4087.203258]  do_init_module+0x62/0x250
[ 4087.203260]  load_module+0x2894/0x2af0
[ 4087.203264]  __do_sys_finit_module+0xbe/0x120
[ 4087.203266]  ? __do_sys_finit_module+0xbe/0x120
[ 4087.203269]  __x64_sys_finit_module+0x1a/0x20
[ 4087.203271]  do_syscall_64+0x57/0x1b0
[ 4087.203274]  entry_SYSCALL_64_after_hwframe+0x44/0xa9
[ 4087.203276] RIP: 0033:0x7fd38f3f670d
[ 4087.203278] Code: 00 c3 66 2e 0f 1f 84 00 00 00 00 00 90 f3 0f 1e fa 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d 53 f7 0c 00 f7 d8 64 89 01 48
[ 4087.203280] RSP: 002b:00007ffe35039d08 EFLAGS: 00000246 ORIG_RAX: 0000000000000139
[ 4087.203282] RAX: ffffffffffffffda RBX: 000055933c9a7b50 RCX: 00007fd38f3f670d
[ 4087.203284] RDX: 0000000000000000 RSI: 000055933bacf358 RDI: 0000000000000003
[ 4087.203285] RBP: 0000000000040000 R08: 0000000000000000 R09: 0000000000000000
[ 4087.203286] R10: 0000000000000003 R11: 0000000000000246 R12: 000055933bacf358
[ 4087.203288] R13: 0000000000000000 R14: 000055933c9af3f0 R15: 000055933c9a7b50
[ 4087.203290] Modules linked in: dvb_vidtv_bridge(+) dvb_vidtv_tuner dvb_vidtv_demod dvb_core rfcomm ccm cmac algif_hash algif_skcipher af_alg bnep binfmt_misc nls_iso8859_1 edac_mce_amd kvm_amd snd_hda_codec_realtek kvm snd_hda_codec_hdmi snd_hda_codec_generic ledtrig_audio irqbypass snd_hda_intel snd_intel_dspcfg snd_usb_audio amdgpu snd_hda_codec snd_usbmidi_lib mc snd_hda_core snd_hwdep snd_pcm snd_seq_midi snd_seq_midi_event btusb snd_rawmidi btrtl btbcm btintel bluetooth crct10dif_pclmul ath10k_pci ath10k_core snd_seq ath snd_seq_device amd_iommu_v2 snd_timer ghash_clmulni_intel ecdh_generic gpu_sched ttm mac80211 aesni_intel drm_kms_helper crypto_simd ecc pl2303 cryptd glue_helper snd cec input_leds cfg80211 serio_raw i2c_algo_bit wmi_bmof fb_sys_fops efi_pstore syscopyarea sysfillrect snd_pci_acp3x k10temp ipmi_devintf sysimgblt ccp libarc4 soundcore ipmi_msghandler mac_hid sch_fq_codel parport_pc ppdev lp parport drm ip_tables x_tables autofs4 hid_generic usbhid hid crc32_pclmul
[ 4087.203325]  ahci psmouse i2c_piix4 libahci nvme nvme_core r8169 realtek wmi video [last unloaded: dvb_vidtv_bridge]
[ 4087.203335] CR2: 0000000000000000
[ 4087.203337] ---[ end trace c8d767383a27d74e ]---
[ 4087.505440] RIP: 0010:strlen+0x0/0x20
[ 4087.505446] Code: 48 89 f9 74 09 48 83 c1 01 80 39 00 75 f7 31 d2 44 0f b6 04 16 44 88 04 11 48 83 c2 01 45 84 c0 75 ee c3 0f 1f 80 00 00 00 00 <80> 3f 00 74 10 48 89 f8 48 83 c0 01 80 38 00 75 f7 48 29 f8 c3 31
[ 4087.505448] RSP: 0018:ffffa604c090b940 EFLAGS: 00010246
[ 4087.505450] RAX: ffff9a5188735cc0 RBX: ffff9a5188735c40 RCX: 0000000000000000
[ 4087.505452] RDX: 0000000000000000 RSI: ffffffffc10d4fd8 RDI: 0000000000000000
[ 4087.505453] RBP: ffffa604c090b9b0 R08: ffff9a51b7bb20a0 R09: ffff9a51b6807640
[ 4087.505455] R10: 0000000000000000 R11: 0000000000000001 R12: ffff9a5188735cc0
[ 4087.505456] R13: 0000000000000000 R14: ffff9a51acd74710 R15: ffff9a519531f000
[ 4087.505458] FS:  00007fd38f2b1540(0000) GS:ffff9a51b7b80000(0000) knlGS:0000000000000000
[ 4087.505460] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 4087.505461] CR2: 0000000000000000 CR3: 00000001ffc50000 CR4: 00000000003406e0
Quoted 408 lines
arch/Kconfig +1 -1
index 786a85d4ad40d..ddcb4a68ee940 100644
@@ -868,7 +868,7 @@ config VMAP_STACK
  	  be enabled.
 
  config ARCH_OPTIONAL_KERNEL_RWX
-	def_bool n
+	def_bool y
 
  config ARCH_OPTIONAL_KERNEL_RWX_DEFAULT
  	def_bool n
drivers/media/dvb-core/dvbdev.c +1 -0
index 80b6a71aa33e4..38c8b848c921f 100644
@@ -962,6 +962,7 @@ int dvb_usercopy(struct file *file,
  }
 
  #if IS_ENABLED(CONFIG_I2C)
+#pragma clang optimize off
  struct i2c_client *dvb_module_probe(const char *module_name,
  				    const char *name,
  				    struct i2c_adapter *adap,
drivers/media/test-drivers/vidtv/Makefile +3 -1
index 21e50c11c94d0..c99cd13f4adaf 100644
@@ -2,5 +2,7 @@
 
  dvb-vidtv-tuner-objs := vidtv_tuner.o
  dvb-vidtv-demod-objs := vidtv_demod.o
+dvb-vidtv-bridge-objs := vidtv_bridge.o
 
-obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o dvb-vidtv-demod.o
+obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o dvb-vidtv-demod.o \
+			   dvb-vidtv-bridge.o
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +364 -0
new file mode 100644
index 0000000000000..bc1c612c23013
@@ -0,0 +1,435 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/moduleparam.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/printk.h>
+#include <linux/time.h>
+#include <linux/types.h>
+#include <linux/workqueue.h>
+
+#include "vidtv_bridge.h"
+#include "vidtv_demod.h"
+#include "vidtv_tuner.h"
+
+#define TUNER_DEFAULT_ADDR 0x68
+#define DEMOD_DEFAULT_ADDR 0x60
+
+static unsigned int drop_tslock_prob_on_low_snr;
+module_param(drop_tslock_prob_on_low_snr, uint, 0);
+MODULE_PARM_DESC(drop_tslock_prob_on_low_snr,
+		 "Probability of losing the TS lock if the signal quality is bad");
+
+static unsigned int recover_tslock_prob_on_good_snr;
+module_param(recover_tslock_prob_on_good_snr, uint, 0);
+MODULE_PARM_DESC(recover_tslock_prob_on_good_snr,
+		 "Probability recovering the TS lock when the signal improves");
+
+static unsigned int mock_power_up_delay_msec;
+module_param(mock_power_up_delay_msec, uint, 0);
+MODULE_PARM_DESC(mock_power_up_delay_msec, "Simulate a power up delay");
+
+static unsigned int mock_tune_delay_msec;
+module_param(mock_tune_delay_msec, uint, 0);
+MODULE_PARM_DESC(mock_tune_delay_msec, "Simulate a tune delay");
+
+static unsigned int vidtv_valid_dvb_t_freqs[8];
+module_param_array(vidtv_valid_dvb_t_freqs, uint, NULL, 0);
+MODULE_PARM_DESC(vidtv_valid_dvb_t_freqs,
+		 "Valid DVB-T frequencies to simulate");
+
+static unsigned int vidtv_valid_dvb_c_freqs[8];
+module_param_array(vidtv_valid_dvb_c_freqs, uint, NULL, 0);
+MODULE_PARM_DESC(vidtv_valid_dvb_c_freqs,
+		 "Valid DVB-C frequencies to simulate");
+
+static unsigned int vidtv_valid_dvb_s_freqs[8];
+module_param_array(vidtv_valid_dvb_s_freqs, uint, NULL, 0);
+MODULE_PARM_DESC(vidtv_valid_dvb_s_freqs,
+		 "Valid DVB-C frequencies to simulate");
+
+static unsigned int max_frequency_shift_hz;
+module_param(max_frequency_shift_hz, uint, 0);
+MODULE_PARM_DESC(max_frequency_shift_hz,
+		 "Maximum shift in HZ allowed when tuning in a channel");
+
+DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nums);
+
+static int vidtv_start_streaming(struct vidtv_dvb *dvb)
+{
+	if (dvb->streaming) {
+		pr_warn_ratelimited("Already streaming. Skipping.\n");
+		return 0;
+	}
+
+	dvb->streaming = true;
+	return 0;
+}
+
+static int vidtv_stop_streaming(struct vidtv_dvb *dvb)
+{
+	dvb->streaming = false;
+
+	return 0;
+}
+
+static int vidtv_start_feed(struct dvb_demux_feed *feed)
+{
+	struct dvb_demux *demux = feed->demux;
+	struct vidtv_dvb *dvb   = demux->priv;
+	int rc;
+	int ret;
+
+	if (!demux->dmx.frontend)
+		return -EINVAL;
+
+	mutex_lock(&dvb->feed_lock);
+
+	dvb->nfeeds++;
+	rc = dvb->nfeeds;
+
+	if (dvb->nfeeds == 1) {
+		ret = vidtv_start_streaming(dvb);
+		if (ret < 0)
+			rc = ret;
+	}
+
+	mutex_unlock(&dvb->feed_lock);
+	return rc;
+}
+
+static int vidtv_stop_feed(struct dvb_demux_feed *feed)
+{
+	struct dvb_demux *demux = feed->demux;
+	struct vidtv_dvb *dvb   = demux->priv;
+	int    err              = 0;
+
+	mutex_lock(&dvb->feed_lock);
+	dvb->nfeeds--;
+
+	if (!dvb->nfeeds)
+		err = vidtv_stop_streaming(dvb);
+
+	mutex_unlock(&dvb->feed_lock);
+	return err;
+}
+
+static struct dvb_frontend *vidtv_get_frontend_ptr(struct i2c_client *c)
+{
+	/* the demod will set this when its probe function runs */
+	struct vidtv_demod_state *state = i2c_get_clientdata(c);
+
+	return &state->frontend;
+}
+
+static int vidtv_master_xfer(struct i2c_adapter *i2c_adap,
+			     struct i2c_msg msgs[],
+			     int num)
+{
+	return 0;
+}
+
+static u32 vidtv_i2c_func(struct i2c_adapter *adapter)
+{
+	return I2C_FUNC_I2C;
+}
+
+static const struct i2c_algorithm vidtv_i2c_algorithm = {
+	.master_xfer   = vidtv_master_xfer,
+	.functionality = vidtv_i2c_func,
+};
+
+static int vidtv_bridge_i2c_register_adap(struct vidtv_dvb *dvb)
+{
+	struct i2c_adapter *i2c_adapter = &dvb->i2c_adapter;
+
+	strscpy(i2c_adapter->name, "vidtv_i2c", sizeof(i2c_adapter->name));
+	i2c_adapter->owner      = THIS_MODULE;
+	i2c_adapter->algo       = &vidtv_i2c_algorithm;
+	i2c_adapter->algo_data  = NULL;
+	i2c_adapter->timeout    = 500;
+	i2c_adapter->retries    = 3;
+	i2c_adapter->dev.parent = &dvb->pdev->dev;
+
+	i2c_set_adapdata(i2c_adapter, dvb);
+	return i2c_add_adapter(&dvb->i2c_adapter);
+}
+
+static int vidtv_bridge_register_adap(struct vidtv_dvb *dvb)
+{
+	int ret = 0;
+
+	ret = dvb_register_adapter(&dvb->adapter,
+				   KBUILD_MODNAME,
+				   THIS_MODULE,
+				   &dvb->i2c_adapter.dev,
+				   adapter_nums);
+
+	return ret;
+}
+
+static int vidtv_bridge_dmx_init(struct vidtv_dvb *dvb)
+{
+	dvb->demux.dmx.capabilities = DMX_TS_FILTERING |
+				      DMX_SECTION_FILTERING;
+
+	dvb->demux.priv       = dvb;
+	dvb->demux.filternum  = 256;
+	dvb->demux.feednum    = 256;
+	dvb->demux.start_feed = vidtv_start_feed;
+	dvb->demux.stop_feed  = vidtv_stop_feed;
+
+	return dvb_dmx_init(&dvb->demux);
+}
+
+static int vidtv_bridge_dmxdev_init(struct vidtv_dvb *dvb)
+{
+	dvb->dmx_dev.filternum    = 256;
+	dvb->dmx_dev.demux        = &dvb->demux.dmx;
+	dvb->dmx_dev.capabilities = 0;
+
+	return dvb_dmxdev_init(&dvb->dmx_dev, &dvb->adapter);
+}
+
+static int vidtv_bridge_probe_demod(struct vidtv_dvb *dvb, u32 n)
+{
+	struct vidtv_demod_config cfg = {};
+
+	cfg.drop_tslock_prob_on_low_snr     = drop_tslock_prob_on_low_snr;
+	cfg.recover_tslock_prob_on_good_snr = recover_tslock_prob_on_good_snr;
+
+	dvb->i2c_client_demod[n] = dvb_module_probe("vidtv_demod",
+						    NULL,
+						    &dvb->i2c_adapter,
+						    DEMOD_DEFAULT_ADDR,
+						    &cfg);
+
+	/* driver will not work anyways so bail out */
+	if (!dvb->i2c_client_demod[n])
+		return -ENODEV;
+
+	/* retrieve a ptr to the frontend state */
+	dvb->fe[n] = vidtv_get_frontend_ptr(dvb->i2c_client_demod[n]);
+
+	return 0;
+}
+
+static int vidtv_bridge_probe_tuner(struct vidtv_dvb *dvb, u32 n)
+{
+	struct vidtv_tuner_config cfg = {};
+
+	cfg.fe                       = dvb->fe[n];
+	cfg.mock_power_up_delay_msec = mock_power_up_delay_msec;
+	cfg.mock_tune_delay_msec     = mock_tune_delay_msec;
+
+	memcpy(cfg.vidtv_valid_dvb_t_freqs,
+	       vidtv_valid_dvb_t_freqs,
+	       sizeof(vidtv_valid_dvb_t_freqs));
+
+	memcpy(cfg.vidtv_valid_dvb_c_freqs,
+	       vidtv_valid_dvb_c_freqs,
+	       sizeof(vidtv_valid_dvb_c_freqs));
+
+	memcpy(cfg.vidtv_valid_dvb_s_freqs,
+	       vidtv_valid_dvb_s_freqs,
+	       sizeof(vidtv_valid_dvb_s_freqs));
+
+	cfg.max_frequency_shift_hz = max_frequency_shift_hz;
+
+	dvb->i2c_client_tuner[n] = dvb_module_probe("vidtv_tuner",
+						    NULL,
+						    &dvb->i2c_adapter,
+						    TUNER_DEFAULT_ADDR,
+						    &cfg);
+
+	return (dvb->i2c_client_tuner[n]) ? 0 : -ENODEV;
+}
+
+static int vidtv_bridge_dvb_init(struct vidtv_dvb *dvb)
+{
+	int ret;
+	int i;
+	int j;
+	int k;
+	int w;
+	int l;
+
+	ret = vidtv_bridge_i2c_register_adap(dvb);
+	if (ret < 0)
+		goto fail_i2c;
+
+	ret = vidtv_bridge_register_adap(dvb);
+	if (ret < 0)
+		goto fail_adapter;
+
+	for (i = 0; i < NUM_FE; ++i) {
+		ret = vidtv_bridge_probe_demod(dvb, i);
+		if (ret < 0)
+			goto fail_demod_probe;
+
+		ret = vidtv_bridge_probe_tuner(dvb, i);
+		if (ret < 0)
+			goto fail_tuner_probe;
+
+		ret = dvb_register_frontend(&dvb->adapter, dvb->fe[i]);
+		if (ret < 0)
+			goto fail_fe;
+	}
+
+	ret = vidtv_bridge_dmx_init(dvb);
+	if (ret < 0)
+		goto fail_dmx;
+
+	ret = vidtv_bridge_dmxdev_init(dvb);
+	if (ret < 0)
+		goto fail_dmx_dev;
+
+	for (j = 0; j < NUM_FE; ++j) {
+		ret = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx,
+						      &dvb->dmx_fe[j]);
+		if (ret < 0)
+			goto fail_dmx_conn;
+
+		/*
+		 * The source of the demux is a frontend connected
+		 * to the demux.
+		 */
+		dvb->dmx_fe[j].source = DMX_FRONTEND_0;
+	}
+
+	return ret;
+
+fail_dmx_conn:
+	for (j = j - 1; j >= 0; --j)
+		dvb->demux.dmx.remove_frontend(&dvb->demux.dmx,
+					       &dvb->dmx_fe[j]);
+fail_dmx_dev:
+	dvb_dmxdev_release(&dvb->dmx_dev);
+fail_dmx:
+	dvb_dmx_release(&dvb->demux);
+fail_fe:
+	for (k = i; k >= 0; --k)
+		dvb_unregister_frontend(dvb->fe[k]);
+fail_tuner_probe:
+	for (w = i; w >= 0; --w)
+		dvb_module_release(dvb->i2c_client_tuner[w]);
+
+fail_demod_probe:
+	for (l = i; l >= 0; --l)
+		dvb_module_release(dvb->i2c_client_demod[l]);
+
+fail_adapter:
+	dvb_unregister_adapter(&dvb->adapter);
+
+fail_i2c:
+	i2c_del_adapter(&dvb->i2c_adapter);
+
+	return ret;
+}
+
+static int vidtv_bridge_probe(struct platform_device *pdev)
+{
+	int ret;
+	struct vidtv_dvb *dvb;
+
+	dvb = kzalloc(sizeof(*dvb), GFP_KERNEL);
+	if (!dvb)
+		return -ENOMEM;
+
+	dvb->pdev = pdev;
+
+	ret = vidtv_bridge_dvb_init(dvb);
+	if (ret < 0)
+		goto err_dvb;
+
+	mutex_init(&dvb->feed_lock);
+
+	platform_set_drvdata(pdev, dvb);
+
+	return ret;
+
+err_dvb:
+	kfree(dvb);
+	return ret;
+}
+

drivers/media/test-drivers/vidtv/vidtv_bridge.c line 374

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +10 -0
+static int vidtv_bridge_remove(struct platform_device *pdev)
+{
+	struct vidtv_dvb *dvb;
+	u32 i;
+
+	dvb = platform_get_drvdata(pdev);
+
+	mutex_destroy(&dvb->feed_lock);
+
+	for (i = 0; i < NUM_FE; ++i)

Using i++ is more common though

drivers/media/test-drivers/vidtv/vidtv_bridge.c line 385

Quoted 11 lines
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +11 -0
+		dvb->demux.dmx.remove_frontend(&dvb->demux.dmx,
+					       &dvb->dmx_fe[i]);
+
+	dvb_dmxdev_release(&dvb->dmx_dev);
+	dvb_dmx_release(&dvb->demux);
+
+	for (i = 0; i < NUM_FE; ++i) {
+		dvb_unregister_frontend(dvb->fe[i]);
+		dvb_frontend_detach(dvb->fe[i]);
+	}
+

Where do you release tuner and demod? probe is happening from vidtv_bridge_dvb_init(). Don't you have to do the reverse here?

drivers/media/test-drivers/vidtv/vidtv_bridge.c line 386

Quoted 1 line
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +1 -0
+	dvb_unregister_adapter(&dvb->adapter);

Don't you have to do i2c_del_adapter() here?

Quoted 85 lines
drivers/media/test-drivers/vidtv/vidtv_bridge.c new file +49 -0
+
+	return 0;
+}
+
+static void vidtv_bridge_dev_release(struct device *dev)
+{
+}
+
+static struct platform_device vidtv_bridge_dev = {
+	.name		= "vidtv_bridge",
+	.dev.release	= vidtv_bridge_dev_release,
+};
+
+static struct platform_driver vidtv_bridge_driver = {
+	.driver = {
+		.name                = "vidtv_bridge",
+		.suppress_bind_attrs = true,
+	},
+	.probe    = vidtv_bridge_probe,
+	.remove   = vidtv_bridge_remove,
+};
+
+static void __exit vidtv_bridge_exit(void)
+{
+	platform_driver_unregister(&vidtv_bridge_driver);
+	platform_device_unregister(&vidtv_bridge_dev);
+}
+
+static int __init vidtv_bridge_init(void)
+{
+	int ret;
+
+	ret = platform_device_register(&vidtv_bridge_dev);
+	if (ret)
+		return ret;
+
+	ret = platform_driver_register(&vidtv_bridge_driver);
+	if (ret)
+		platform_device_unregister(&vidtv_bridge_dev);
+
+	return ret;
+}
+
+module_init(vidtv_bridge_init);
+module_exit(vidtv_bridge_exit);
+
+MODULE_DESCRIPTION("Virtual Digital TV Test Driver");
+MODULE_AUTHOR("Daniel W. S. Almeida");
+MODULE_LICENSE("GPL");
drivers/media/test-drivers/vidtv/vidtv_bridge.h new file +29 -0
new file mode 100644
index 0000000000000..4931bfb73273c
@@ -0,0 +1,39 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_BRIDGE_H
+#define VIDTV_BRIDGE_H
+
+#define NUM_FE 1
+
+#include <linux/i2c.h>
+#include <linux/platform_device.h>
+#include <linux/types.h>
+#include <media/dmxdev.h>
+#include <media/dvb_demux.h>
+#include <media/dvb_frontend.h>
+
+struct vidtv_dvb {
+	struct platform_device *pdev;
+	struct dvb_frontend *fe[NUM_FE];
+	struct dvb_adapter adapter;
+	struct dvb_demux demux;
+	struct dmxdev dmx_dev;
+	struct dmx_frontend dmx_fe[NUM_FE];
+	struct i2c_adapter i2c_adapter;

drivers/media/test-drivers/vidtv/vidtv_bridge.h line 39

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_bridge.h new file +10 -0
+	struct i2c_client *i2c_client_demod[NUM_FE];
+	struct i2c_client *i2c_client_tuner[NUM_FE];
+
+	u32 nfeeds;
+	struct mutex feed_lock; /* start/stop feed */
+
+	bool streaming;
+};
+
+#endif // VIDTV_BRIDG_H

thanks,
-- Shuah

author maintainer
#12 lore raw

RFC v6 5/10 media: vidtv: add wrappers for memcpy and memset

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

A lot of code in this driver is for serializing structures. This is error prone.

Therefore, prevent buffer overflows by wrapping memcpy and memset, comparing the requested length against the buffer size.

---
 .../media/test-drivers/vidtv/vidtv_common.c   | 86 +++++++++++++++++++
 .../media/test-drivers/vidtv/vidtv_common.h   | 27 ++++++
 2 files changed, 113 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.h
drivers/media/test-drivers/vidtv/vidtv_common.c new file +86 -0
new file mode 100644
index 0000000000000..6810212087c17
@@ -0,0 +1,86 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include "vidtv_common.h"
+
+/**
+ * vidtv_memcpy() - wrapper routine to be used by MPEG-TS
+ *	generator, in order to avoid going past the
+ *	output buffer.
+ * @to:	Starting element to where a MPEG-TS packet will
+ *	be copied.
+ * @to_offset:	Starting position of the @to buffer to be filled.
+ * @to_size:	Size of the @to buffer.
+ * @from:	Starting element of the buffer to be copied.
+ * @len:	Number of elements to be copy from @from buffer
+ *	into @to+ @to_offset buffer.
+ *
+ * Note:
+ *	Real digital TV demod drivers should not have memcpy
+ *	wrappers. We use it here because emulating MPEG-TS
+ *	generation at kernelspace requires some extra care.
+ *
+ * Return:
+ *	Returns the number of bytes written
+ */
+u32 vidtv_memcpy(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 const void *from,
+		 size_t len)
+{
+	if (unlikely(to_offset + len > to_size)) {
+		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size\n");
+		return 0;
+	}
+
+	memcpy(to + to_offset, from, len);
+	return len;
+}
+
+/**
+ * vidtv_memset() - wrapper routine to be used by MPEG-TS
+ *	generator, in order to avoid going past the
+ *	output buffer.
+ * @to:	Starting element to set
+ * @to_offset:	Starting position of the @to buffer to be filled.
+ * @to_size:	Size of the @to buffer.
+ * @from:	Starting element of the buffer to be copied.
+ * @ten:	Number of elements to be copy from @from buffer
+ *	into @to+ @to_offset buffer.
+ *
+ * Note:
+ *	Real digital TV demod drivers should not have memset
+ *	wrappers. We use it here because emulating MPEG-TS
+ *	generation at kernelspace requires some extra care.
+ *
+ * Return:
+ *	Returns the number of bytes written
+ */
+u32 vidtv_memset(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 const int c,
+		 size_t len)
+{
+	if (unlikely(to_offset + len > to_size)) {
+		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size\n");
+		return 0;
+	}
+
+	memset(to + to_offset, c, len);
+	return len;
+}
drivers/media/test-drivers/vidtv/vidtv_common.h new file +27 -0
new file mode 100644
index 0000000000000..a3cb303cc7423
@@ -0,0 +1,27 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_COMMON_H
+#define VIDTV_COMMON_H
+
+#include <linux/types.h>
+
+u32 vidtv_memcpy(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 const void *from,
+		 size_t len);
+
+u32 vidtv_memset(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 int c,
+		 size_t len);
+
+#endif // VIDTV_COMMON_H
Signature · 1 line
2.26.2
#13 lore raw

Re: [RFC, WIP, v6 05/10] media: vidtv: add wrappers for memcpy and memset

Hi Daniel,

Quoted 8 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

A lot of code in this driver is for serializing structures. This is error prone.

Therefore, prevent buffer overflows by wrapping memcpy and memset, comparing the requested length against the buffer size.

Quoted 8 lines
---
  .../media/test-drivers/vidtv/vidtv_common.c   | 86 +++++++++++++++++++
  .../media/test-drivers/vidtv/vidtv_common.h   | 27 ++++++
  2 files changed, 113 insertions(+)

create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.h

I don't see these added to Makefile. Building the driver with the patch has no effect.

In any case I don't see any value in adding the wrappers here. They really do nothing other than doing range checks. It adda a layer of indirection that is unnecessary.

Quoted 41 lines
drivers/media/test-drivers/vidtv/vidtv_common.c new file +35 -0
new file mode 100644
index 0000000000000..6810212087c17
@@ -0,0 +1,86 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+#include <linux/string.h>
+#include <linux/types.h>
+
+#include "vidtv_common.h"
+
+/**
+ * vidtv_memcpy() - wrapper routine to be used by MPEG-TS
+ *	generator, in order to avoid going past the
+ *	output buffer.
+ * @to:	Starting element to where a MPEG-TS packet will
+ *	be copied.
+ * @to_offset:	Starting position of the @to buffer to be filled.
+ * @to_size:	Size of the @to buffer.
+ * @from:	Starting element of the buffer to be copied.
+ * @len:	Number of elements to be copy from @from buffer
+ *	into @to+ @to_offset buffer.
+ *
+ * Note:
+ *	Real digital TV demod drivers should not have memcpy
+ *	wrappers. We use it here because emulating MPEG-TS
+ *	generation at kernelspace requires some extra care.
+ *

drivers/media/test-drivers/vidtv/vidtv_common.c line 45

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_common.c new file +10 -0
+ * Return:
+ *	Returns the number of bytes written
+ */
+u32 vidtv_memcpy(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 const void *from,
+		 size_t len)
+{
+	if (unlikely(to_offset + len > to_size)) {

Use of unlikely isn't necessarily beneficial in all cases.

Quoted 65 lines
drivers/media/test-drivers/vidtv/vidtv_common.c new file +41 -0
+		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size\n");
+		return 0;
+	}
+
+	memcpy(to + to_offset, from, len);
+	return len;
+}
+
+/**
+ * vidtv_memset() - wrapper routine to be used by MPEG-TS
+ *	generator, in order to avoid going past the
+ *	output buffer.
+ * @to:	Starting element to set
+ * @to_offset:	Starting position of the @to buffer to be filled.
+ * @to_size:	Size of the @to buffer.
+ * @from:	Starting element of the buffer to be copied.
+ * @ten:	Number of elements to be copy from @from buffer
+ *	into @to+ @to_offset buffer.
+ *
+ * Note:
+ *	Real digital TV demod drivers should not have memset
+ *	wrappers. We use it here because emulating MPEG-TS
+ *	generation at kernelspace requires some extra care.
+ *
+ * Return:
+ *	Returns the number of bytes written
+ */
+u32 vidtv_memset(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 const int c,
+		 size_t len)
+{
+	if (unlikely(to_offset + len > to_size)) {
+		pr_err_ratelimited("overflow detected, skipping. Try increasing the buffer size\n");
+		return 0;
+	}
+
+	memset(to + to_offset, c, len);
+	return len;
+}
drivers/media/test-drivers/vidtv/vidtv_common.h new file +17 -0
new file mode 100644
index 0000000000000..a3cb303cc7423
@@ -0,0 +1,27 @@
+/* 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#ifndef VIDTV_COMMON_H
+#define VIDTV_COMMON_H
+
+#include <linux/types.h>
+
+u32 vidtv_memcpy(void *to,
+		 size_t to_offset,
+		 size_t to_size,

drivers/media/test-drivers/vidtv/vidtv_common.h line 27

Quoted 10 lines
drivers/media/test-drivers/vidtv/vidtv_common.h new file +10 -0
+		 const void *from,
+		 size_t len);
+
+u32 vidtv_memset(void *to,
+		 size_t to_offset,
+		 size_t to_size,
+		 int c,
+		 size_t len);
+
+#endif // VIDTV_COMMON_H

thanks,
-- Shuah

author maintainer
#14 lore raw

RFC v6 6/10 media: vidtv: add MPEG TS common code

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Add code to work with MPEG TS packets, such as TS headers, adaptation fields, PCR packets and NULL packets.

---
 drivers/media/test-drivers/vidtv/vidtv_ts.c | 157 ++++++++++++++++++++
 drivers/media/test-drivers/vidtv/vidtv_ts.h | 120 +++++++++++++++
 2 files changed, 277 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.h
drivers/media/test-drivers/vidtv/vidtv_ts.c new file +157 -0
new file mode 100644
index 0000000000000..77908c4e85eef
@@ -0,0 +1,157 @@
+// 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.
+ *
+ * Written by Daniel W. S. Almeida <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+#include <linux/types.h>
+#include <asm/byteorder.h>
+
+#include "vidtv_ts.h"
+#include "vidtv_common.h"
+
+static u32 vidtv_ts_write_pcr_bits(u8 *to, u32 to_offset, u64 pcr)
+{
+	/* Exact same from ffmpeg. PCR is a counter driven by a 27Mhz clock */
+	u64 pcr_low = pcr % 300;
+	u64 pcr_high = pcr / 300;
+	u8 *buf = to + to_offset;
+
+	*buf++ = pcr_high >> 25;
+	*buf++ = pcr_high >> 17;
+	*buf++ = pcr_high >>  9;
+	*buf++ = pcr_high >>  1;
+	*buf++ = pcr_high <<  7 | pcr_low >> 8 | 0x7e;
+	*buf++ = pcr_low;
+
+	return 6;
+}
+
+void vidtv_ts_inc_cc(u8 *continuity_counter)
+{
+	++*continuity_counter;
+	if (*continuity_counter > TS_CC_MAX_VAL)
+		*continuity_counter = 0;
+}
+
+u32 vidtv_ts_null_write_into(struct null_packet_write_args args)
+{
+	u32    nbytes                  = 0;
+	struct vidtv_mpeg_ts ts_header = {0};
+
+	ts_header.sync_byte          = TS_SYNC_BYTE;
+	ts_header.pid                = TS_NULL_PACKET_PID;
+	ts_header.payload            = 1;
+	ts_header.continuity_counter = *args.continuity_counter;
+
+	cpu_to_be16s(&ts_header.bitfield);
+
+	/* copy TS header */
+	nbytes += vidtv_memcpy(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.buf_sz,
+			       &ts_header,
+			       sizeof(ts_header));
+
+	be16_to_cpus(&ts_header.bitfield);
+
+	/*
+	 * Note:
+	 *	This will cause a discontinuity if the buffer is full,
+	 *	effectively dropping the packet, since nothing will get written
+	 *	but the continuity counter will still be incremented. This way,
+	 *	userspace can detect packet losses.
+	 */
+	vidtv_ts_inc_cc(args.continuity_counter);
+
+	/* fill the rest with empty data */
+	nbytes += vidtv_memset(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.buf_sz,
+			       TS_FILL_BYTE,
+			       TS_PACKET_LEN - nbytes);
+
+	/* we should have written exactly _one_ 188byte packet */
+	if (nbytes != TS_PACKET_LEN)
+		pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",
+				    TS_PACKET_LEN,
+				    nbytes);
+
+	return nbytes;
+}
+
+u32 vidtv_ts_pcr_write_into(struct pcr_write_args args)
+{
+	u32    nbytes                         = 0;
+	struct vidtv_mpeg_ts ts_header        = {0};
+	struct vidtv_mpeg_ts_adaption ts_adap = {0};
+
+	ts_header.sync_byte     = TS_SYNC_BYTE;
+	ts_header.tei           = 0;
+	ts_header.payload_start = 0;
+	ts_header.pid           = args.pid;
+	ts_header.priority      = 0;
+	ts_header.scrambling    = 0;
+	/* cc is not incremented, but we need it. see 13818-1 clause 2.4.3.3 */
+	ts_header.continuity_counter = *args.continuity_counter;
+	ts_header.payload            = 0;
+	ts_header.adaptation_field   = 1;
+
+	/* 13818-1 clause 2.4.3.5 */
+	ts_adap.length = 183;
+	ts_adap.PCR    = 1;
+
+	/*
+	 * Note:
+	 *	Nothing will get written if the buffer is full, this will
+	 *	essentially drop the packet. The continuity counter is not
+	 *	updated for PCR packets, so it will be as if no PCR packet was
+	 *	sent at all.
+	 */
+
+	cpu_to_be16s(&ts_header.bitfield);
+
+	/* copy TS header */
+	nbytes += vidtv_memcpy(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.buf_sz,
+			       &ts_header,
+			       sizeof(ts_header));
+
+	be16_to_cpus(&ts_header.bitfield);
+
+	/* write the adap after the TS header */
+	nbytes += vidtv_memcpy(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.buf_sz,
+			       &ts_adap,
+			       sizeof(ts_adap));
+
+	/* write the PCR optional */
+	cpu_to_be64s(&args.pcr);
+	nbytes += vidtv_ts_write_pcr_bits(args.dest_buf,
+					  args.dest_offset + nbytes,
+					  args.pcr);
+	be64_to_cpus(&args.pcr);
+
+	nbytes += vidtv_memset(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.buf_sz,
+			       TS_FILL_BYTE,
+			       TS_PACKET_LEN - nbytes);
+
+	/* we should have written exactly _one_ 188byte packet */
+	if (nbytes != TS_PACKET_LEN)
+		pr_warn_ratelimited("Expected exactly %d bytes, got %d\n",
+				    TS_PACKET_LEN,
+				    nbytes);
+
+	return nbytes;
+}
drivers/media/test-drivers/vidtv/vidtv_ts.h new file +120 -0

120 lines not on this page · raw message

Signature · 6 lines
2.26.2

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
author maintainer
#15 lore raw

RFC v6 7/10 media: vidtv: implement a PSI generator

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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 this commit adds support for working with 3 PSI tables:
PAT, PMT and SDT.

---
 drivers/media/test-drivers/vidtv/vidtv_psi.c | 1037 ++++++++++++++++++
 drivers/media/test-drivers/vidtv/vidtv_psi.h |  415 +++++++
 2 files changed, 1452 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.h
drivers/media/test-drivers/vidtv/vidtv_psi.c new file +1037 -0
new file mode 100644
index 0000000000000..51e9114173362
@@ -0,0 +1,1037 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * This file contains the logic to work with MPEG Program-Specific Information.
+ * These are defined both in ISO/IEC 13818-1 (systems) and ETSI EN 300 468.
+ * PSI is carried in the form of table structures, and although each table might
+ * technically be broken into one or more sections, we do not do this here,
+ * hence 'table' and 'section' are interchangeable for us.
+ *
+ * This code currently supports three tables: PAT, PMT and SDT. These are the
+ * bare minimum to get userspace to recognize our MPEG transport stream. It can
+ * be extended to support more PSI tables in the future.
+ *
+ * A note on endianness: MPEG layout is big-endian, therefore:
+ * - All fields spanning more than a byte must undergo 'cpu_to_beXX()'
+ * - All bitfields must have their ordering reversed if
+ *  __LITTLE_ENDIAN_BITFIELD is defined.
+ *
+ * Written by: Daniel W. S. Almeida <[email protected]>
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ":%s, %d: " fmt, __func__, __LINE__
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+#include <linux/crc32.h>
+#include <linux/string.h>
+#include <linux/printk.h>
+#include <linux/ratelimit.h>
+
+#include "vidtv_psi.h"
+#include "vidtv_common.h"
+#include "vidtv_ts.h"
+
+#define CRC_SIZE_IN_BYTES 4
+
+static u32
+vidtv_psi_ts_psi_write_into(struct psi_write_args args)
+{
+	/*
+	 * Packetize PSI sections into TS packets:
+	 * push a TS header (4bytes) every 184 bytes
+	 * manage the continuity_counter
+	 * add stuffing after the CRC
+	 */
+
+	u32  nbytes_past_boundary = (args.dest_offset % TS_PACKET_LEN);
+	bool aligned              = nbytes_past_boundary == 0;
+
+	/*
+	 * whether we need to fragment the data into multiple ts packets
+	 * if we are aligned we need to spare one byte for the pointer_field
+	 */
+	bool split = (aligned) ?
+		     args.len > TS_PAYLOAD_LEN - 1 :
+		     nbytes_past_boundary + args.len > TS_PACKET_LEN;
+
+	/* how much we can write in this packet */
+	u32 payload_write_len = (split) ?
+				(aligned)     ? TS_PAYLOAD_LEN       :
+				TS_PACKET_LEN - nbytes_past_boundary :
+				args.len;
+
+	struct psi_write_args new_args = {};
+	struct vidtv_mpeg_ts ts_header = {};
+
+	u32 nbytes = 0;  /* number of bytes written by this function */
+	u32 temp   = 0;
+
+	/* Just a sanity check, should not really happen because we stuff
+	 * the packet when we finish a section, i.e. when we write the crc at
+	 * the end. But if this happens then we have messed up the logic
+	 * somewhere.
+	 */
+	if (args.new_psi_section && !aligned) {
+		pr_warn_ratelimited("Cannot write a new PSI section in a misaligned buffer\n");
+
+		/* forcibly align and hope for the best */
+		nbytes += vidtv_memset(args.dest_buf,
+				       args.dest_offset + nbytes,
+				       args.dest_buf_sz,
+				       TS_FILL_BYTE,
+				       TS_PACKET_LEN - nbytes_past_boundary);
+
+		aligned = true;
+	}
+
+	if (aligned) {
+		/* if at a packet boundary, write a new TS header */
+		ts_header.sync_byte          = TS_SYNC_BYTE;
+		ts_header.tei                = 0;
+		ts_header.payload_start      = 1;
+		ts_header.pid                = args.pid;
+		ts_header.priority           = 0;
+		ts_header.scrambling         = 0;
+		ts_header.continuity_counter = *args.continuity_counter;
+		ts_header.payload            = 1;
+		/* no adaptation field */
+		ts_header.adaptation_field = 0;
+
+		/* copy the header */
+		nbytes += vidtv_memcpy(args.dest_buf,
+				       args.dest_offset + nbytes,
+				       args.dest_buf_sz,
+				       &ts_header,
+				       sizeof(ts_header));
+
+		/*
+		 * increment the countinuity counter since we have started a new
+		 * packet
+		 *
+		 * This will trigger a discontinuity if the buffer is full,
+		 * which is what we want: the continuity counter will be
+		 * incremented but nothing will get copied by
+		 * vidtv_memcpy/vidtv_memset, effectively dropping the packet.
+		 */
+		vidtv_ts_inc_cc(args.continuity_counter);
+	}
+
+	if (args.new_psi_section) {
+		/* write the pointer_field in the first byte of the payload */
+		temp = vidtv_memset(args.dest_buf,
+				    args.dest_offset + nbytes,
+				    args.dest_buf_sz,
+				    0x0,
+				    1);
+		/* one byte was used by the pointer field*/
+		nbytes += temp;
+		if (payload_write_len == TS_PAYLOAD_LEN)
+			payload_write_len -= temp;
+	}
+
+	/* write as much of the payload as we possibly can */
+	nbytes += vidtv_memcpy(args.dest_buf,
+			       args.dest_offset + nbytes,
+			       args.dest_buf_sz,
+			       args.from,
+			       payload_write_len);
+
+	if (split) {
+		/* 'payload_write_len' written from a total of 'len' requested*/
+		args.len -= payload_write_len;
+		/*
+		 * recursively write the rest of the data until we do not
+		 * need to split it anymore
+		 */
+		memcpy(&new_args, &args, sizeof(struct psi_write_args));
+		new_args.from            = args.from + payload_write_len;
+		new_args.dest_offset     = args.dest_offset + nbytes;
+		new_args.new_psi_section = false;
+
+		nbytes += vidtv_psi_ts_psi_write_into(new_args);
+	}
+
+	/*
+	 * as the CRC is last in the section, stuff the rest of the
+	 * packet if there is any remaining space in there
+	 */
+	if (args.is_crc)
+		nbytes += vidtv_memset(args.dest_buf,
+				       args.dest_offset + nbytes,
+				       args.dest_buf_sz,
+				       TS_FILL_BYTE,
+				       TS_PACKET_LEN - payload_write_len);
+
+	return nbytes;
+}
+
+static u32 table_section_crc32_write_into(struct crc32_write_args args)
+{
+	/* the CRC is the last entry in the section */
+	u32 nbytes = 0;
+	__be32 crc;
+	struct psi_write_args psi_args = {};
+
+	crc = cpu_to_be32(crc32_be(0, args.dest_buf, args.dest_offset));
+
+	psi_args.dest_buf           = args.dest_buf;
+	psi_args.from               = &crc;
+	psi_args.len                = CRC_SIZE_IN_BYTES;
+	psi_args.dest_offset        = args.dest_offset;
+	psi_args.pid                = args.pid;
+	psi_args.new_psi_section    = false;
+	psi_args.continuity_counter = args.continuity_counter;
+	psi_args.is_crc             = true;
+	psi_args.dest_buf_sz        = args.dest_buf_sz;
+
+	nbytes += vidtv_psi_ts_psi_write_into(psi_args);
+
+	return nbytes;
+}
+
+struct vidtv_psi_desc *vidtv_psi_desc_init(struct vidtv_psi_desc *head,
+					   u8 type,
+					   u8 length)
+{
+	struct vidtv_psi_desc *desc;
+
+	/* alloc enough memory for the flexible array too */
+	desc = kzalloc(sizeof(*desc) + length, GFP_KERNEL);
+
+	desc->type   = type;
+	desc->length = length;
+
+	if (head) {
+		while (head->next)
+			head = head->next;
+
+		head->next = desc;
+	}
+
+	return desc;
+}
+
+void vidtv_psi_desc_destroy(struct vidtv_psi_desc *desc)
+{
+	struct vidtv_psi_desc *curr = desc;
+	struct vidtv_psi_desc *tmp  = NULL;
+
+	while (curr) {
+		tmp  = curr;
+		curr = curr->next;
+		kfree(tmp);
+	}
+}
+
+static u32
+vidtv_psi_desc_comp_len(struct vidtv_psi_desc *desc)
+{
+	u32 length = 0;
+
+	if (!desc)
+		return 0;
+
+	while (desc) {
+		length += desc->length;
+		desc    = desc->next;
+	}
+
+	return length;
+}
+
+void vidtv_psi_desc_assign(struct vidtv_psi_desc **to,
+			   struct vidtv_psi_desc *desc)
+{
+	/*
+	 * Caller must recompute the section length afterwards at some point
+	 * This function transfers ownedship of desc.
+	 * Start by cleaning the old data
+	 */
+	if (*to)
+		vidtv_psi_desc_destroy(*to);
+
+	*to = desc;  /* reassign pointer */
+}
+
+static u32 vidtv_psi_desc_write_into(struct desc_write_args args)
+{
+	/* the number of bytes written by this function */
+	u32    nbytes                  = 0;
+	struct psi_write_args psi_args = {};
+
+	psi_args.dest_buf = args.dest_buf;
+	psi_args.from     = args.desc;
+
+	psi_args.len      = sizeof_field(struct vidtv_psi_desc, type) +
+			    sizeof_field(struct vidtv_psi_desc, length);
+
+	psi_args.dest_offset        = args.dest_offset;
+	psi_args.pid                = args.pid;
+	psi_args.new_psi_section    = false;
+	psi_args.continuity_counter = args.continuity_counter;
+	psi_args.is_crc             = false;
+	psi_args.dest_buf_sz        = args.dest_buf_sz;
+
+	nbytes += vidtv_psi_ts_psi_write_into(psi_args);
+
+	/* move 'from' pointer to point to u8 data[] */
+	psi_args.from = args.desc +
+			sizeof_field(struct vidtv_psi_desc, type) +
+			sizeof_field(struct vidtv_psi_desc, length) +
+			sizeof(struct vidtv_psi_desc *);
+
+	psi_args.len         = args.desc->length;
+	psi_args.dest_offset = args.dest_offset + nbytes;
+
+	nbytes += vidtv_psi_ts_psi_write_into(psi_args);
+
+	return nbytes;
+}
+
+static u32
+vidtv_psi_table_header_write_into(struct header_write_args args)
+{
+	/* the number of bytes written by this function */
+	u32    nbytes                  = 0;
+	struct psi_write_args psi_args = {};
+
+	psi_args.dest_buf           = args.dest_buf;
+	psi_args.from               = args.h;
+	psi_args.len                = sizeof(struct vidtv_psi_table_header);
+	psi_args.dest_offset        = args.dest_offset;
+	psi_args.pid                = args.pid;
+	psi_args.new_psi_section    = true;
+	psi_args.continuity_counter = args.continuity_counter;
+	psi_args.is_crc             = false;
+	psi_args.dest_buf_sz        = args.dest_buf_sz;
+
+	nbytes += vidtv_psi_ts_psi_write_into(psi_args);
+
+	return nbytes;
+}
+
+void
+vidtv_psi_pat_table_comp_sec_len(struct vidtv_psi_table_pat *pat)
+{
+	/* see ISO/IEC 13818-1 : 2000 p.43 */
+	u16 length = 0;
+	u32 i;
+
+	/* from immediately after 'section_length' until 'last_section_number'*/
+	length += PAT_LEN_UNTIL_LAST_SECTION_NUMBER;
+
+	/* do not count the pointer */
+	for (i = 0; i < pat->programs; ++i)
+		length += sizeof(struct vidtv_psi_table_pat_program) -
+			  sizeof(struct vidtv_psi_table_pat_program *);
+
+	length += CRC_SIZE_IN_BYTES;
+
+	if (length > MAX_SECTION_LEN)
+		pr_warn_ratelimited("len %d > %d\n", length, MAX_SECTION_LEN);
+
+	pat->header.section_length = length;
+}
+
+void vidtv_psi_pmt_table_comp_sec_len(struct vidtv_psi_table_pmt *pmt)
+{
+	/* see ISO/IEC 13818-1 : 2000 p.46 */
+	u16    length                        = 0;
+	struct vidtv_psi_table_pmt_stream *s = pmt->stream;
+
+	/* from immediately after 'section_length' until 'program_info_length'*/
+	length += PMT_LEN_UNTIL_PROGRAM_INFO_LENGTH;
+
+	pmt->desc_length  = vidtv_psi_desc_comp_len(pmt->descriptor);
+	length           += pmt->desc_length;
+
+	while (s) {
+		/* skip both pointers at the end */
+		length += sizeof(struct vidtv_psi_table_pmt_stream) -
+			  sizeof(struct vidtv_psi_desc *) -
+			  sizeof(struct vidtv_psi_table_pmt_stream *);
+
+		s->desc_length  = vidtv_psi_desc_comp_len(s->descriptor);
+		length         += s->desc_length;
+
+		s = s->next;
+	}
+
+	length += CRC_SIZE_IN_BYTES;
+
+	if (length > MAX_SECTION_LEN)
+		pr_warn_ratelimited("len %d > %d\n", length, MAX_SECTION_LEN);
+
+	pmt->header.section_length = length;
+}
+
+void vidtv_psi_sdt_table_comp_sec_len(struct vidtv_psi_table_sdt *sdt)
+{
+	/* see ETSI EN 300 468 V 1.10.1 p.24 */
+	u16    length                         = 0;
+	struct vidtv_psi_table_sdt_service *s = sdt->service;
+
+	/*
+	 * from immediately after 'section_length' until
+	 * 'reserved_for_future_use'
+	 */
+	length += SDT_LEN_UNTIL_RESERVED_FOR_FUTURE_USE;
+
+	while (s) {
+		/* skip both pointers at the end */
+		length += sizeof(struct vidtv_psi_table_sdt_service) -
+			  sizeof(struct vidtv_psi_desc *) -
+			  sizeof(struct vidtv_psi_table_sdt_service *);
+
+		s->desc_length  = vidtv_psi_desc_comp_len(s->descriptor);
+		length         += s->desc_length;
+
+		s = s->next;
+	}
+
+	length += CRC_SIZE_IN_BYTES;
+
+	if (length > MAX_SECTION_LEN)
+		pr_warn_ratelimited("len %d > %d\n", length, MAX_SECTION_LEN);
+
+	sdt->header.section_length = length;
+}
+
+struct vidtv_psi_table_pat_program*
+vidtv_psi_pat_program_init(struct vidtv_psi_table_pat_program *head,
+			   u16 service_id,
+			   u16 pid)
+{
+	/*
+	 * if 'head' is attached to a table, caller should recompute
+	 * the section length afterwards at some point
+	 */
+	struct vidtv_psi_table_pat_program *program;
+
+	program = kzalloc(sizeof(*program), GFP_KERNEL);
+
+	program->service_id = cpu_to_be16(service_id);
+	/* pid for the PMT section in the TS */
+	program->pid        = pid;
+	program->next       = NULL;
+	program->reserved   = 0x7;
+
+	if (head) {
+		while (head->next)
+			head = head->next;
+
+		head->next = program;
+	}
+
+	return program;
+}
+
+void
+vidtv_psi_pat_program_destroy(struct vidtv_psi_table_pat_program *p)
+{
+	struct vidtv_psi_table_pat_program *curr = p;
+	struct vidtv_psi_table_pat_program *tmp  = NULL;
+
+	while (curr) {
+		tmp  = curr;
+		curr = curr->next;
+		kfree(tmp);
+	}
+}
+
+void
+vidtv_psi_pat_program_assign(struct vidtv_psi_table_pat *pat,
+			     struct vidtv_psi_table_pat_program *p)
+{
+	/* This function transfers ownership of p to the table */
+
+	u16    program_count                        = 0;
+	struct vidtv_psi_table_pat_program *program = p;
+	struct vidtv_psi_table_pat_program *temp    = pat->program;
+
+	while (program) {
+		++program_count;
+		program = program->next;
+	}
+
+	pat->programs = program_count;
+	pat->program  = p;
+
+	/* Recompute section length */
+	vidtv_psi_pat_table_comp_sec_len(pat);
+
+	/* reassign if the new size is too big */
+	if (pat->header.section_length > MAX_SECTION_LEN)
+		vidtv_psi_pat_program_assign(pat, temp);
+	else
+		vidtv_psi_pat_program_destroy(temp);
+}
+
+void vidtv_psi_pat_table_init(struct vidtv_psi_table_pat *pat,
+			      bool update_version_num,
+			      u16 transport_stream_id)
+{
+	static u8 pat_version;
+
+	pat->header.table_id = 0x0;
+	pat->header.syntax   = 0x1;
+	pat->header.zero     = 0x0;
+	pat->header.one      = 0x03;
+
+	/* transport stream ID, at will */
+	pat->header.id           = cpu_to_be16(transport_stream_id);
+	pat->header.current_next = 0x1;
+
+	/* ETSI 300 468: indicates changes in the TS described by this table*/
+	if (update_version_num)
+		++pat_version;
+
+	pat->header.version = pat_version;
+
+	pat->header.one2         = 0x03;
+	pat->header.section_id   = 0x0;
+	pat->header.last_section = 0x0;
+
+	pat->programs = 0;
+
+	vidtv_psi_pat_table_comp_sec_len(pat);
+}
+
+u32 vidtv_psi_pat_write_into(char *buf,
+			     u32 offset,
+			     struct vidtv_psi_table_pat *pat,
+			     u32 buf_sz,
+			     u8 *continuity_counter)
+{
+	/* the number of bytes written by this function */
+	u32   nbytes      = 0;
+	const u16 pat_pid = VIDTV_PAT_PID;
+
+	struct vidtv_psi_table_pat_program *p = pat->program;
+	struct header_write_args h_args       = {};
+	struct psi_write_args args            = {};
+	struct crc32_write_args c_args        = {};
+
+	vidtv_psi_pat_table_comp_sec_len(pat);
+
+	h_args.dest_buf           = buf;
+	h_args.dest_offset        = offset;
+	h_args.h                  = &pat->header;
+	h_args.pid                = pat_pid;
+	h_args.continuity_counter = continuity_counter;
+	h_args.dest_buf_sz        = buf_sz;
+
+	nbytes += vidtv_psi_table_header_write_into(h_args);
+
+	/* note that the field 'u16 programs' is not really part of the PAT */
+
+	args.dest_buf           = buf;
+	args.pid                = pat_pid;
+	args.new_psi_section    = false;
+	args.continuity_counter = continuity_counter;
+	args.is_crc             = false;
+	args.dest_buf_sz        = buf_sz;
+
+	while (p) {
+		/* copy the PAT programs */
+		args.from = p;
+		/* skip the pointer */
+		args.len = sizeof(*p) -
+			   sizeof(struct vidtv_psi_table_pat_program *);
+		args.dest_offset = offset + nbytes;
+
+		nbytes += vidtv_psi_ts_psi_write_into(args);
+
+		p = p->next;
+	}
+
+	c_args.dest_buf           = buf;
+	c_args.dest_offset        = offset + nbytes;
+	c_args.pid                = pat_pid;
+	c_args.continuity_counter = continuity_counter;
+	c_args.dest_buf_sz        = buf_sz;
+
+	nbytes += table_section_crc32_write_into(c_args);
+
+	return nbytes;
+}
+
+void
+vidtv_psi_pat_table_destroy(struct vidtv_psi_table_pat *p)
+{
+	vidtv_psi_pat_program_destroy(p->program);
+}
+
+struct vidtv_psi_table_pmt_stream*
+vidtv_psi_pmt_stream_init(struct vidtv_psi_table_pmt_stream *head,
+			  enum vidtv_psi_stream_types stream_type,
+			  u16 es_pid)
+{
+	struct vidtv_psi_table_pmt_stream *stream;
+
+	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
+
+	stream->type           = stream_type;
+	stream->elementary_pid = es_pid;
+	stream->reserved       = 0x07;
+
+	stream->desc_length = vidtv_psi_desc_comp_len(stream->descriptor);
+
+	stream->zero      = 0x0;
+	stream->reserved2 = 0x0f;
+
+	if (head) {
+		while (head->next)
+			head = head->next;
+
+		head->next = stream;
+	}
+
+	return stream;
+}
+
+void vidtv_psi_pmt_stream_destroy(struct vidtv_psi_table_pmt_stream *s)
+{
+	struct vidtv_psi_table_pmt_stream *curr_stream = s;
+	struct vidtv_psi_table_pmt_stream *tmp_stream  = NULL;
+
+	while (curr_stream) {
+		tmp_stream  = curr_stream;
+		curr_stream = curr_stream->next;
+		kfree(tmp_stream);
+	}
+}
+
+void vidtv_psi_pmt_stream_assign(struct vidtv_psi_table_pmt *pmt,
+				 struct vidtv_psi_table_pmt_stream *s)
+{
+	/* This function transfers ownership of s to the table */
+	struct vidtv_psi_table_pmt_stream *stream = s;
+	struct vidtv_psi_desc *desc               = s->descriptor;
+	struct vidtv_psi_table_pmt_stream *temp   = pmt->stream;
+
+	while (stream)
+		stream = stream->next;
+
+	while (desc)
+		desc = desc->next;
+
+	pmt->stream = s;
+	/* Recompute section length */
+	vidtv_psi_pmt_table_comp_sec_len(pmt);
+
+	/* reassign if the new size is too big */
+	if (pmt->header.section_length > MAX_SECTION_LEN)
+		vidtv_psi_pmt_stream_assign(pmt, temp);
+	else
+		vidtv_psi_pmt_stream_destroy(temp);
+}
+
+u16 vidtv_psi_pmt_get_pid(struct vidtv_psi_table_pmt *section,
+			  struct vidtv_psi_table_pat *pat)
+{
+	struct vidtv_psi_table_pat_program *program = pat->program;
+
+	/*
+	 * service_id is the same as program_number in the
+	 * corresponding program_map_section
+	 * see ETSI EN 300 468 v1.15.1 p. 24
+	 */
+	while (program) {
+		if (program->service_id == section->header.id)
+			return pat->program->pid;
+
+		program = program->next;
+	}
+
+	return TS_LAST_VALID_PID + 1; /* not found */
+}
+
+void vidtv_psi_pmt_table_init(struct vidtv_psi_table_pmt *pmt,
+			      bool update_version_num,
+			      u16 program_number,
+			      u16 pcr_pid)
+{
+	static u8 pmt_version;
+
+	pmt->header.table_id = 0x2;
+	pmt->header.syntax   = 0x1;
+	pmt->header.zero     = 0x0;
+	pmt->header.one      = 0x3;
+
+	pmt->header.id           = cpu_to_be16(program_number);
+	pmt->header.current_next = 0x1;
+
+	/* ETSI 300 468: indicates changes in the TS described by this table*/
+	if (update_version_num)
+		++pmt_version;
+
+	pmt->header.version = pmt_version;
+
+	pmt->header.one2         = 0x3;
+	pmt->header.section_id   = 0;
+	pmt->header.last_section = 0;
+
+	pmt->pcr_pid   = (pcr_pid) ? pcr_pid : 0x1fff;
+	pmt->reserved2 = 0x03;
+
+	pmt->reserved3 = 0x0f;
+	pmt->zero3     = 0x0;
+
+	pmt->desc_length = vidtv_psi_desc_comp_len(pmt->descriptor);
+
+	vidtv_psi_pmt_table_comp_sec_len(pmt);
+}
+
+u32 vidtv_psi_pmt_write_into(char *buf,
+			     u32 offset,
+			     struct vidtv_psi_table_pmt *pmt,
+			     u16 pid,
+			     u32 buf_sz,
+			     u8 *continuity_counter)
+{
+	/* the number of bytes written by this function */
+	u32    nbytes                             = 0;
+	struct vidtv_psi_desc *table_descriptor   = pmt->descriptor;
+	struct vidtv_psi_table_pmt_stream *stream = pmt->stream;
+	struct vidtv_psi_desc *stream_descriptor  = (stream) ?
+						    pmt->stream->descriptor :
+						    NULL;
+
+	struct header_write_args h_args = {};
+	struct psi_write_args args      = {};
+	struct desc_write_args d_args   = {};
+	struct crc32_write_args c_args  = {};
+
+	vidtv_psi_pmt_table_comp_sec_len(pmt);
+
+	h_args.dest_buf           = buf;
+	h_args.dest_offset        = offset;
+	h_args.h                  = &pmt->header;
+	h_args.pid                = pid;
+	h_args.continuity_counter = continuity_counter;
+	h_args.dest_buf_sz        = buf_sz;
+
+	nbytes += vidtv_psi_table_header_write_into(h_args);
+
+	/* write the two bitfields */
+	args.dest_buf = buf;
+	args.from     = pmt + sizeof(struct vidtv_psi_table_header);
+	args.len      = sizeof_field(struct vidtv_psi_table_pmt, bitfield) +
+			sizeof_field(struct vidtv_psi_table_pmt, bitfield2);
+	args.dest_offset        = offset + nbytes;
+	args.pid                = pid;
+	args.new_psi_section    = false;
+	args.continuity_counter = continuity_counter;
+	args.is_crc             = false;
+	args.dest_buf_sz        = buf_sz;
+
+	nbytes += vidtv_psi_ts_psi_write_into(args);
+
+	while (table_descriptor) {
+		/* write the descriptors, if any */
+		d_args.dest_buf           = buf;
+		d_args.dest_offset        = offset + nbytes;
+		d_args.desc               = table_descriptor;
+		d_args.pid                = pid;
+		d_args.continuity_counter = continuity_counter;
+		d_args.dest_buf_sz        = buf_sz;
+
+		nbytes += vidtv_psi_desc_write_into(d_args);
+
+		table_descriptor = table_descriptor->next;
+	}
+
+	while (stream) {
+		/* write the streams, if any */
+		args.from = stream;
+		args.len  = sizeof_field(struct vidtv_psi_table_pmt_stream,
+					 type) +
+			    sizeof_field(struct vidtv_psi_table_pmt_stream,
+					 bitfield) +
+			    sizeof_field(struct vidtv_psi_table_pmt_stream,
+					 bitfield2);
+		args.dest_offset = offset + nbytes;
+
+		nbytes += vidtv_psi_ts_psi_write_into(args);
+
+		while (stream_descriptor) {
+			/* write the stream descriptors, if any */
+			d_args.desc        = stream_descriptor;
+			d_args.dest_offset = offset + nbytes;
+
+			nbytes += vidtv_psi_desc_write_into(d_args);
+
+			stream_descriptor = stream_descriptor->next;
+		}
+
+		stream = stream->next;
+	}
+
+	c_args.dest_buf           = buf;
+	c_args.dest_offset        = offset + nbytes;
+	c_args.pid                = pid;
+	c_args.continuity_counter = continuity_counter;
+	c_args.dest_buf_sz        = buf_sz;
+
+	nbytes += table_section_crc32_write_into(c_args);
+
+	return nbytes;
+}
+
+void vidtv_psi_pmt_table_destroy(struct vidtv_psi_table_pmt *pmt)
+{
+	struct vidtv_psi_desc *curr_desc = pmt->descriptor;
+	struct vidtv_psi_desc *tmp_desc  = NULL;
+
+	while (curr_desc) {
+		tmp_desc  = curr_desc;
+		curr_desc = curr_desc->next;
+		vidtv_psi_desc_destroy(tmp_desc);
+		kfree(tmp_desc);
+	}
+
+	vidtv_psi_pmt_stream_destroy(pmt->stream);
+}
+
+void vidtv_psi_sdt_table_init(struct vidtv_psi_table_sdt *sdt,
+			      bool update_version_num,
+			      u16 transport_stream_id)
+{
+	static u8 sdt_version;
+
+	sdt->header.table_id = 0x42;
+
+	sdt->header.one  = 0x3;
+	sdt->header.zero = 0x1;
+	/*
+	 * The PAT, PMT, and CAT all set this to 0.
+	 * Other tables set this to 1.
+	 */
+	sdt->header.syntax = 0x1;
+
+	/*
+	 * This is a 16-bit field which serves as a label for identification
+	 * of the TS, about which the SDT informs, from any other multiplex
+	 * within the delivery system.
+	 */
+	sdt->header.id           = cpu_to_be16(transport_stream_id);
+	sdt->header.current_next = 0x1;
+
+	/* ETSI 300 468: indicates changes in the TS described by this table*/
+	if (update_version_num)
+		++sdt_version;
+
+	sdt->header.version = sdt_version;
+
+	sdt->header.one2         = 0x3;
+	sdt->header.section_id   = 0;
+	sdt->header.last_section = 0;
+
+	sdt->network_id = cpu_to_be16(transport_stream_id);
+	sdt->reserved   = 0xff;
+
+	vidtv_psi_sdt_table_comp_sec_len(sdt);
+}
+
+u32 vidtv_psi_sdt_write_into(char *buf,
+			     u32 offset,
+			     struct vidtv_psi_table_sdt *sdt,
+			     u32 buf_sz,
+			     u8 *continuity_counter)
+{
+	u32 nbytes  = 0;              /* the number of bytes written */
+	u16 sdt_pid = VIDTV_SDT_PID;  /* see ETSI EN 300 468 v1.15.1 p. 11 */
+
+	struct vidtv_psi_table_sdt_service *service = sdt->service;
+	struct vidtv_psi_desc *service_desc = (sdt->service) ?
+					      sdt->service->descriptor :
+					      NULL;
+
+	struct header_write_args h_args = {};
+	struct psi_write_args args      = {};
+	struct desc_write_args d_args   = {};
+	struct crc32_write_args c_args  = {};
+
+	vidtv_psi_sdt_table_comp_sec_len(sdt);
+
+	h_args.dest_buf           = buf;
+	h_args.dest_offset        = offset;
+	h_args.h                  = &sdt->header;
+	h_args.pid                = sdt_pid;
+	h_args.continuity_counter = continuity_counter;
+	h_args.dest_buf_sz        = buf_sz;
+
+	nbytes += vidtv_psi_table_header_write_into(h_args);
+
+	args.dest_buf = buf;
+	args.from     = sdt + sizeof(struct vidtv_psi_table_header);
+
+	args.len      = sizeof_field(struct vidtv_psi_table_sdt, network_id) +
+			sizeof_field(struct vidtv_psi_table_sdt, reserved);
+
+	args.dest_offset        = offset + nbytes;
+	args.pid                = sdt_pid;
+	args.new_psi_section    = false;
+	args.continuity_counter = continuity_counter;
+	args.is_crc             = false;
+	args.dest_buf_sz        = buf_sz;
+
+	/* copy u16 network_id + u8 reserved)*/
+	nbytes += vidtv_psi_ts_psi_write_into(args);
+
+	while (service) {
+		/* copy the services, if any */
+		args.from = service;
+		/* skip both pointers at the end */
+		args.len = sizeof(struct vidtv_psi_table_sdt_service) -
+			   sizeof(struct vidtv_psi_desc *) -
+			   sizeof(struct vidtv_psi_table_sdt_service *);
+		args.dest_offset = offset + nbytes;
+
+		nbytes += vidtv_psi_ts_psi_write_into(args);
+
+		while (service_desc) {
+			/* copy the service descriptors, if any */
+			d_args.dest_buf           = buf;
+			d_args.dest_offset        = offset + nbytes;
+			d_args.desc               = service_desc;
+			d_args.pid                = sdt_pid;
+			d_args.continuity_counter = continuity_counter;
+			d_args.dest_buf_sz        = buf_sz;
+
+			nbytes += vidtv_psi_desc_write_into(d_args);
+
+			service_desc = service_desc->next;
+		}
+
+		service = service->next;
+	}
+
+	c_args.dest_buf           = buf;
+	c_args.dest_offset        = offset + nbytes;
+	c_args.pid                = sdt_pid;
+	c_args.continuity_counter = continuity_counter;
+	c_args.dest_buf_sz        = buf_sz;
+
+	nbytes += table_section_crc32_write_into(c_args);
+
+	return nbytes;
+}
+
+void vidtv_psi_sdt_table_destroy(struct vidtv_psi_table_sdt *sdt)
+{
+	struct vidtv_psi_table_sdt_service *curr_service = sdt->service;
+	struct vidtv_psi_table_sdt_service *tmp_service  = NULL;
+	struct vidtv_psi_desc *curr_desc = (sdt->service) ?
+					   sdt->service->descriptor : NULL;
+	struct vidtv_psi_desc *tmp_desc = NULL;
+
+	while (curr_service) {
+		curr_desc = curr_service->descriptor;
+
+		while (curr_desc) {
+			/* clear all descriptors for the service */
+			tmp_desc  = curr_desc;
+			curr_desc = curr_desc->next;
+			vidtv_psi_desc_destroy(tmp_desc);
+			kfree(tmp_desc);
+		}
+
+		/* then clear the current service */
+		tmp_service  = curr_service;
+		curr_service = curr_service->next;
+		kfree(tmp_service);
+	}
+}
+
+struct vidtv_psi_table_sdt_service
+*vidtv_psi_sdt_service_init(struct vidtv_psi_table_sdt_service *head,
+			    u16 service_id)
+{
+	/*
+	 * if 'head' is attached to a table, caller should recompute
+	 * the section length afterwards at some point
+	 */
+	struct vidtv_psi_table_sdt_service *service;
+
+	service = kzalloc(sizeof(*service), GFP_KERNEL);
+
+	/*
+	 * ETSI 300 468: this is a 16bit field which serves as a label to
+	 * identify this service from any other service within the TS.
+	 * The service id is the same as the program number in the
+	 * corresponding program_map_section
+	 */
+	service->service_id            = cpu_to_be16(service_id);
+	service->EIT_schedule          = 0x0;
+	service->EIT_present_following = 0x0;
+	service->reserved              = 0x3f;        /* all bits on */
+	service->free_CA_mode          = 0x0;         /* not scrambled */
+	service->running_status        = RUNNING;
+
+	if (head) {
+		while (head->next)
+			head = head->next;
+
+		head->next = service;
+	}
+
+	return service;
+}
+
+void
+vidtv_psi_sdt_service_destroy(struct vidtv_psi_table_sdt_service *service)
+{
+	struct vidtv_psi_table_sdt_service *curr = service;
+	struct vidtv_psi_table_sdt_service *tmp  = NULL;
+
+	while (curr) {
+		tmp  = curr;
+		curr = curr->next;
+		kfree(tmp);
+	}
+}
+
+void
+vidtv_psi_sdt_service_assign(struct vidtv_psi_table_sdt *sdt,
+			     struct vidtv_psi_table_sdt_service *service)
+{
+	struct vidtv_psi_table_sdt_service *temp = sdt->service;
+
+	sdt->service = service;
+
+	/* recompute section length */
+	vidtv_psi_sdt_table_comp_sec_len(sdt);
+
+	/* reassign if the new size is too big */
+	if (sdt->header.section_length > MAX_SECTION_LEN)
+		vidtv_psi_sdt_service_assign(sdt, temp);
+	else
+		vidtv_psi_sdt_service_destroy(temp);
+}
+
+void
+vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat,
+					    struct vidtv_psi_table_pmt sec[])
+
+{
+	/*
+	 * PMTs contain information about programs. For each program,
+	 * there is one PMT section. This function will create a section
+	 * for each program found in the PAT
+	 */
+	struct vidtv_psi_table_pat_program *program = pat->program;
+	u32    i                                    = 0;
+
+	while (program) {
+		vidtv_psi_pmt_table_init(&sec[i],
+					 false,
+					 be16_to_cpu(sec[i].header.id),
+					 0);
+
+		++i;
+		program = program->next;
+	}
+}
drivers/media/test-drivers/vidtv/vidtv_psi.h new file +415 -0

415 lines not on this page · raw message

Signature · 1 line
2.26.2
author maintainer
#16 lore raw

RFC v6 8/10 media: vidtv: implement a PES packetizer

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Implement the PES logic to convert encoder data into MPEG TS packets. These TS packets can then be fed into a TS multiplexer and eventually into userspace.

---
 .../media/test-drivers/vidtv/vidtv_common.h   |   2 +
 drivers/media/test-drivers/vidtv/vidtv_pes.c  | 450 ++++++++++++++++++
 drivers/media/test-drivers/vidtv/vidtv_pes.h  | 186 ++++++++
 3 files changed, 638 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.h
drivers/media/test-drivers/vidtv/vidtv_common.h +2 -0
index a3cb303cc7423..1a31973f3ac61 100644
@@ -24,4 +24,6 @@ u32 vidtv_memset(void *to,
 		 int c,
 		 size_t len);
 
+u64 vidtv_extract_bits(u64 value, u8 start_bit, u8 nbits);
+
 #endif // VIDTV_COMMON_H
drivers/media/test-drivers/vidtv/vidtv_pes.c new file +450 -0

450 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_pes.h new file +186 -0

186 lines not on this page · raw message

Signature · 1 line
2.26.2
author maintainer
#17 lore raw

RFC v6 9/10 media: vidtv: Implement a SMPTE 302M encoder

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Implement a S302M encoder to make it possible to insert PCM audio data in the generated MPEG Transport Stream.

This shall enable passing an audio signal into userspace so it can be decoded and played by media software.

---
 .../media/test-drivers/vidtv/vidtv_common.h   |   2 +
 .../media/test-drivers/vidtv/vidtv_encoder.h  | 103 +++
 .../media/test-drivers/vidtv/vidtv_s302m.c    | 636 ++++++++++++++++++
 .../media/test-drivers/vidtv/vidtv_s302m.h    | 119 ++++
 4 files changed, 860 insertions(+)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_encoder.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.h
drivers/media/test-drivers/vidtv/vidtv_common.h +2 -0
index 1a31973f3ac61..a0480825b23a9 100644
@@ -12,6 +12,8 @@
 
 #include <linux/types.h>
 
+#define CLOCK_UNIT_90KHZ 90000
+
 u32 vidtv_memcpy(void *to,
 		 size_t to_offset,
 		 size_t to_size,
drivers/media/test-drivers/vidtv/vidtv_encoder.h new file +103 -0

103 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_s302m.c new file +636 -0

636 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_s302m.h new file +119 -0

119 lines not on this page · raw message

Signature · 6 lines
2.26.2

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
author maintainer
#18 lore raw

RFC v6 10/10 media: vidtv: Add a MPEG Transport Stream Multiplexer

In reply to Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

Add a MPEG Transport Stream multiplexer responsible for polling encoders, interleaving packets, padding the resulting stream with NULL packets if necessary and then delivering the resulting TS packets to the bridge driver so it can feed the demux.

This patch includes a "channel" abstraction, which attempts to map a MPEG service into a struct that vidtv can work with.

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 to produce TS packets -These packets may be interleaved by the mux and then delivered to the bridg

---
 drivers/media/test-drivers/vidtv/Makefile     |   3 +-
 .../media/test-drivers/vidtv/vidtv_bridge.c   |  76 +++
 .../media/test-drivers/vidtv/vidtv_bridge.h   |   2 +
 .../media/test-drivers/vidtv/vidtv_channel.c  | 339 ++++++++++++++
 .../media/test-drivers/vidtv/vidtv_channel.h  |  66 +++
 .../media/test-drivers/vidtv/vidtv_common.h   |   3 +
 drivers/media/test-drivers/vidtv/vidtv_mux.c  | 434 ++++++++++++++++++
 drivers/media/test-drivers/vidtv/vidtv_mux.h  | 105 +++++
 drivers/media/test-drivers/vidtv/vidtv_psi.c  |  18 +
 drivers/media/test-drivers/vidtv/vidtv_psi.h  |   5 +
 10 files changed, 1050 insertions(+), 1 deletion(-)
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.h
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.c
 create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.h
drivers/media/test-drivers/vidtv/Makefile +2 -1
index c99cd13f4adaf..330089e3b70c3 100644
@@ -2,7 +2,8 @@
 
 dvb-vidtv-tuner-objs := vidtv_tuner.o
 dvb-vidtv-demod-objs := vidtv_demod.o
-dvb-vidtv-bridge-objs := vidtv_bridge.o
+dvb-vidtv-bridge-objs := vidtv_bridge.o vidtv_common.o vidtv_ts.o vidtv_psi.o \
+			 vidtv_pes.o vidtv_s302m.o vidtv_channel.o vidtv_mux.o
 
 obj-$(CONFIG_DVB_VIDTV)	+= dvb-vidtv-tuner.o dvb-vidtv-demod.o \
 			   dvb-vidtv-bridge.o
drivers/media/test-drivers/vidtv/vidtv_bridge.c +76 -0

119 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_bridge.h +2 -0
index 4931bfb73273c..aac1da13a50aa 100644
@@ -18,6 +18,7 @@
 #include <media/dmxdev.h>
 #include <media/dvb_demux.h>
 #include <media/dvb_frontend.h>
+#include "vidtv_mux.h"
 
 struct vidtv_dvb {
 	struct platform_device *pdev;
@@ -34,6 +35,7 @@ struct vidtv_dvb {
 	struct mutex feed_lock; /* start/stop feed */
 
 	bool streaming;
+	struct vidtv_mux *mux;
 };
 
 #endif // VIDTV_BRIDG_H
drivers/media/test-drivers/vidtv/vidtv_channel.c new file +339 -0

339 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_channel.h new file +66 -0

66 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_common.h +3 -0
index a0480825b23a9..a13e9331955a7 100644
@@ -13,6 +13,9 @@
 #include <linux/types.h>
 
 #define CLOCK_UNIT_90KHZ 90000
+#define CLOCK_UNIT_27MHZ 27000000
+#define SLEEP_USECS 10000
+#define TRANSPORT_STREAM_ID 0x744
 
 u32 vidtv_memcpy(void *to,
 		 size_t to_offset,
drivers/media/test-drivers/vidtv/vidtv_mux.c new file +434 -0

434 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_mux.h new file +105 -0

105 lines not on this page · raw message

drivers/media/test-drivers/vidtv/vidtv_psi.c +18 -0
index 51e9114173362..d9547efe4626b 100644
@@ -1035,3 +1035,21 @@ vidtv_psi_pmt_create_sec_for_each_pat_entry(struct vidtv_psi_table_pat *pat,
 		program = program->next;
 	}
 }
+
+struct vidtv_psi_table_pmt
+*vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt pmt_sections[],
+			u16 nsections,
+			u16 program_num)
+{
+	/* find the PMT section associated with 'program_num' */
+	struct vidtv_psi_table_pmt *sec = NULL;
+	u32 i;
+
+	for (i = 0; i < nsections; ++i) {
+		sec = &pmt_sections[i];
+		if (be16_to_cpu(sec->header.id) == program_num)
+			return sec;
+	}
+
+	return NULL;
+}
drivers/media/test-drivers/vidtv/vidtv_psi.h +5 -0
index 77b616a46757a..f45540d53b351 100644
@@ -412,4 +412,9 @@ u32 vidtv_psi_pmt_write_into(char *buf,
 			     u32 buf_sz,
 			     u8 *continuity_counter);
 
+struct vidtv_psi_table_pmt
+*vidtv_psi_find_pmt_sec(struct vidtv_psi_table_pmt *pmt_sections,
+			u16 nsections,
+			u16 program_num);
+
 #endif // VIDTV_PSI_H
Signature · 6 lines
2.26.2

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
#19 lore raw

Re: [RFC, WIP, v6 10/10] media: vidtv: Add a MPEG Transport Stream Multiplexer

[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi "Daniel,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on linuxtv-media/master]
[also build test ERROR on next-20200519]
[cannot apply to linus/master linux/master v5.7-rc6]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Daniel-W-S-Almeida/media-vidtv-implement-a-virtual-DVB-driver/20200520-232710
base:   git://linuxtv.org/media_tree.git master
config: h8300-allyesconfig (attached as .config)
compiler: h8300-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=h8300

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>, old ones prefixed by <<):

h8300-linux-ld: drivers/media/test-drivers/vidtv/vidtv_ts.o: in function `vidtv_ts_pcr_write_into':
vidtv_ts.c:(.text+0x23d): undefined reference to `__umoddi3'

>> h8300-linux-ld: vidtv_ts.c:(.text+0x273): undefined reference to `__udivdi3'

h8300-linux-ld: drivers/media/test-drivers/vidtv/vidtv_s302m.o: in function `.L108':
vidtv_s302m.c:(.text+0x7f9): undefined reference to `__udivdi3'
h8300-linux-ld: drivers/media/test-drivers/vidtv/vidtv_mux.o: in function `.L55':
vidtv_mux.c:(.text+0x8c5): undefined reference to `__udivdi3'

>> h8300-linux-ld: vidtv_mux.c:(.text+0x8f1): undefined reference to `__udivdi3'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#20 lore raw
In reply to Daniel W. S. Almeida
Quoted 13 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

This series is work in progress. It represents the current work done on a virtual DVB driver for the Linux media subsystem. I am new to the media subsystem and to kernel development in general.

This driver aims to:
- Serve as template for new DVB driver writers
- Help userspace application writers in general
- Push fake audio/video to userspace for testing purposes
- Push debug information to userspace via debugfs

Quoted 5 lines

This should currently be able to feed PSI and PCM audio data to userspace.

I appreciate any feedback!

Run checkpatch --strict on these. There are several warns and an error which could improve the code as well as the improving Kconfig entry as suggested below.

WARNING: please write a paragraph that describes the config symbol fully #108: FILE: drivers/media/test-drivers/Kconfig:7:
+menuconfig DVB_TEST_DRIVERS

I will send review comments on individual patches.

thanks,
-- Shuah

#21 lore raw
In reply to Daniel W. S. Almeida
Quoted 113 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

This series is work in progress. It represents the current work done on a virtual DVB driver for the Linux media subsystem. I am new to the media subsystem and to kernel development in general.

This driver aims to:
- Serve as template for new DVB driver writers
- Help userspace application writers in general
- Push fake audio/video to userspace for testing purposes
- Push debug information to userspace via debugfs

This should currently be able to feed PSI and PCM audio data to userspace.

I appreciate any feedback!

Changes in v6:
Addressed the following issues:
- Makefile was not actually compiling everything;
- The bridge driver should be a platform driver;
- There are lots of warnings and other errors produced by the driver.

Changes in v5:

Removed all calls to WARN_ON in favor of pr_warn_ratelimited Add a #define for pr_fmt
Reworked the byte swapping logic for big/little endian support:
big endian fields now prepended with __beXX for 'sparse' checks bitfields now laid in reverse order if LITTLE_ENDIAN_BITFIELD
is set

media: vidtv: implement a tuner driver
Return -EINVAL instead of -1 in vidtv_tuner_check_frequency_shift
media: vidtv: implement a demodulator driver
Add POLL_THRD_TIME #define
Implement dvb_frontend_ops as a single struct instead of three
media: vidtv: move config structs into a separate header
Removed this commit, configs are now in vidtv_tuner.h and vidtv_demod.h
media: vidtv: add a bridge driver
module_param: all fs permissions set to 0
removed param 'chosen_delsys'
module_param: removed "optional" string: all module_params are optional renamed vidtv_bridge -> vidtv_bridg: so the source code and module names
are different
media: vidtv: add wrappers for memcpy and memset
Added kernel-docs
write address is now computed internally
media: vidtv: add MPEG TS common code
Drop packets if the buffer is full
media: vidtv: implement a PSI generator
Removed vidtv_psi_ts_psi_write_stuffing()
Forcibly align misaligned buffers
Drop packets if buffer is full
media: vidtv: implement a PES packetizer
Remove vidtv_extract_bits() in favor of GENMASK() and bitwise & Forcibly align misaligned buffers
media: vidtv: Implement a SMPTE 302M encoder
Added kernel-docs for struct vidtv_encoder
media: vidtv: Add a MPEG Transport Stream Multiplexer
Added check for minimum and maximum buffer size.
Drop packets if buffer is full

Changes in v4:
Added a PES packetizer
Implemented a minimum version of the SMPTE 302m encoder for AES3 audio Fixed endianness in the PSI generator, converting fields to big endian where applicable Added a minimal TS mux abstraction

Changes in v3:
Added a bridge driver
Renamed the driver to vidtv
Renamed/reworked commits into smaller pieces
Moved the driver into its own directory
Fixed the code for the signal strength in the tuner
Removed useless enums in the tuner driver (e.g. lock_status, power_status...) Reworked the logic for the poll_snr thread in the demodulator driver Moved MPEG related code to the bridge driver, as it controls the demux logic Changed literals to #defines, used sizeof in place of integer literals when computing the size of PSI structs
Moved the MPEG PSI tables to the heap to reduce stack space usage Now using usleep_range in place of msleep_interruptible in the MPEG TS thread Wrapped memcpy and memset to protect against buffer overflow when writing to the MPEG TS buffer.

Changes in v2:
Attempted not to break assignments into multiple lines as much as possible. Code now passes checkpatch strict mode

media: dvb_dummy_tuner: implement driver skeleton Changed snr values to mili db
Return value from 0-100 to indicate how far off the requested frequency is from a valid one

Use the frequency shift to interpolate between 34dB and 10dB if we can not match against the SNR lookup table
Remove sleep calls for suspend/resume

Fix memcpy call for the config struct

media: dvb_dummy_fe.c: lose TS lock on bad snr Randomly recover the TS lock if the signal quality improves

media: dvb_dummy_fe.c: write PSI information into DMX buffer Split the patch into multiple header/source files

Hexadecimal literals are now lower case

Prefer short function names / reduce function signatures

Quoted 8 lines

Add #defines for constants when computing section lengths

Change signature for functions that take a dummy channel as argument (i.e. channels* is now channels[NUM_CHANNELS])

Daniel W. S. Almeida (10):
media: vidtv: add Kconfig entry
media: vidtv: implement a tuner driver

You can collapse patch 1 and 2. I don't see why you need two separate patches.

Quoted 10 lines

media: vidtv: implement a demodulator driver
media: vidtv: add a bridge driver
media: vidtv: add wrappers for memcpy and memset
media: vidtv: add MPEG TS common code
media: vidtv: implement a PSI generator
media: vidtv: implement a PES packetizer
media: vidtv: Implement a SMPTE 302M encoder
media: vidtv: Add a MPEG Transport Stream Multiplexer

  arch/Kconfig                                  |    2 +-

Why do you need this change. It is part of patch 04 with no explanation on why this change is necessary.

Quoted 1 line
  drivers/media/dvb-core/dvbdev.c               |    1 +

Same here.

Quoted 41 lines
  drivers/media/test-drivers/Kconfig            |   10 +
  drivers/media/test-drivers/Makefile           |    1 +
  drivers/media/test-drivers/vidtv/Kconfig      |   11 +
  drivers/media/test-drivers/vidtv/Makefile     |    9 +
  .../media/test-drivers/vidtv/vidtv_bridge.c   |  511 ++++++++
  .../media/test-drivers/vidtv/vidtv_bridge.h   |   41 +
  .../media/test-drivers/vidtv/vidtv_channel.c  |  339 ++++++
  .../media/test-drivers/vidtv/vidtv_channel.h  |   66 ++
  .../media/test-drivers/vidtv/vidtv_common.c   |   86 ++
  .../media/test-drivers/vidtv/vidtv_common.h   |   34 +
  .../media/test-drivers/vidtv/vidtv_demod.c    |  444 +++++++
  .../media/test-drivers/vidtv/vidtv_demod.h    |   41 +
  .../media/test-drivers/vidtv/vidtv_encoder.h  |  103 ++
  drivers/media/test-drivers/vidtv/vidtv_mux.c  |  434 +++++++
  drivers/media/test-drivers/vidtv/vidtv_mux.h  |  105 ++
  drivers/media/test-drivers/vidtv/vidtv_pes.c  |  450 +++++++
  drivers/media/test-drivers/vidtv/vidtv_pes.h  |  186 +++
  drivers/media/test-drivers/vidtv/vidtv_psi.c  | 1055 +++++++++++++++++
  drivers/media/test-drivers/vidtv/vidtv_psi.h  |  420 +++++++
  .../media/test-drivers/vidtv/vidtv_s302m.c    |  636 ++++++++++
  .../media/test-drivers/vidtv/vidtv_s302m.h    |  119 ++
  drivers/media/test-drivers/vidtv/vidtv_ts.c   |  157 +++
  drivers/media/test-drivers/vidtv/vidtv_ts.h   |  120 ++
  .../media/test-drivers/vidtv/vidtv_tuner.c    |  408 +++++++
  .../media/test-drivers/vidtv/vidtv_tuner.h    |   26 +
  27 files changed, 5814 insertions(+), 1 deletion(-)

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_bridge.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_bridge.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_channel.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_common.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_demod.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_encoder.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_mux.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_pes.h

Quoted 9 lines

create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_psi.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_s302m.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_ts.h create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.c create mode 100644 drivers/media/test-drivers/vidtv/vidtv_tuner.h

thanks,
-- Shuah