[RFC PATCH 1/2] scripts/dtc: Add script to extract matchable DT compatibles

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Introduce a script to extract all compatibles that can match a device
described on the Devicetree to a driver.

The compatible extraction is done by running Coccinelle with a newly
introduced Coccinelle file that detects compatibles listed inside a
of_device_id table which is referenced by the of_match_table of a
driver struct.

Signed-off-by: Nícolas F. R. A. Prado <nfraprado@xxxxxxxxxxxxx>

---

 scripts/dtc/extract-matchable-dt-compatibles | 33 +++++++++++
 scripts/dtc/matchable_dt_compatibles.cocci   | 58 ++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100755 scripts/dtc/extract-matchable-dt-compatibles
 create mode 100644 scripts/dtc/matchable_dt_compatibles.cocci

diff --git a/scripts/dtc/extract-matchable-dt-compatibles b/scripts/dtc/extract-matchable-dt-compatibles
new file mode 100755
index 000000000000..b5e96c7ba42e
--- /dev/null
+++ b/scripts/dtc/extract-matchable-dt-compatibles
@@ -0,0 +1,33 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+#
+# Copyright (c) 2023 Collabora Ltd
+#
+# Based on coccicheck script.
+#
+# This script uses Coccinelle to extract all compatibles that can be matched by
+# Devicetree devices from the kernel source.
+
+DIR="$(dirname $(readlink -f $0))"
+SPATCH=`which ${SPATCH:=spatch}`
+COCCI_FILE=$DIR/matchable_dt_compatibles.cocci
+
+if [ ! -x "$SPATCH" ]; then
+    echo 'spatch is part of the Coccinelle project and is available at http://coccinelle.lip6.fr/'
+    exit 1
+fi
+
+# Use only one thread per core by default if hyperthreading is enabled
+THREADS_PER_CORE=$(LANG=C lscpu | grep "Thread(s) per core: " | tr -cd "[:digit:]")
+if [ -z "$J" ]; then
+	NPROC=$(getconf _NPROCESSORS_ONLN)
+	if [ $THREADS_PER_CORE -gt 1 -a $NPROC -gt 4 ] ; then
+		NPROC=$((NPROC/2))
+	fi
+else
+	NPROC="$J"
+fi
+
+"$SPATCH" --cocci-file "$COCCI_FILE" --dir .  --jobs "$NPROC" --chunksize 1 \
+	--no-show-diff --no-includes --include-headers --very-quiet \
+	| grep '"' | sed -e 's/"//g;s/^ *\([^ ]*\) *$/\1/'
diff --git a/scripts/dtc/matchable_dt_compatibles.cocci b/scripts/dtc/matchable_dt_compatibles.cocci
new file mode 100644
index 000000000000..ecd3705681aa
--- /dev/null
+++ b/scripts/dtc/matchable_dt_compatibles.cocci
@@ -0,0 +1,58 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright: (C) 2023 Collabora Ltd
+@rule1@
+identifier table;
+type drv;
+identifier drvname;
+identifier drvfield;
+identifier drvfield1;
+identifier drvfield2;
+@@
+
+(
+drv drvname = {
+	.drvfield = {
+		.of_match_table = table,
+	},
+};
+|
+drv drvname = {
+	.drvfield = {
+		.of_match_table = of_match_ptr(table),
+	},
+};
+|
+// Accounts for mdio and spimem drivers
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = table,
+		},
+	},
+};
+|
+drv drvname = {
+	.drvfield1 = {
+		.drvfield2 = {
+			.of_match_table = of_match_ptr(table),
+		},
+	},
+};
+)
+
+@rule2@
+identifier rule1.table;
+expression compat;
+@@
+
+struct of_device_id table[] = {
+...,
+{ .compatible = compat, },
+...
+};
+
+@script:python@
+compat << rule2.compat;
+@@
+
+print("%s" % compat)
-- 
2.41.0




[Index of Archives]     [Device Tree Compilter]     [Device Tree Spec]     [Linux Driver Backports]     [Video for Linux]     [Linux USB Devel]     [Linux PCI Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Yosemite Backpacking]


  Powered by Linux