1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Copyright (c) 2023 Collabora Ltd
5#
6# Based on Frank Rowand's dt_stat script.
7#
8# This script tests for devices that were declared on the Devicetree and are
9# expected to bind to a driver, but didn't.
10#
11# To achieve this, two lists are used:
12# * a list of the compatibles that can be matched by a Devicetree node
13# * a list of compatibles that should be ignored
14#
15
16DIR="$(dirname $(readlink -f "$0"))"
17
18source "${DIR}"/../kselftest/ktap_helpers.sh
19
20PDT=/proc/device-tree/
21COMPAT_LIST="${DIR}"/compatible_list
22IGNORE_LIST="${DIR}"/compatible_ignore_list
23
24ktap_print_header
25
26if [[ ! -d "${PDT}" ]]; then
27	ktap_skip_all "${PDT} doesn't exist."
28	exit "${KSFT_SKIP}"
29fi
30
31nodes_compatible=$(
32	for node in $(find ${PDT} -type d); do
33		[ ! -f "${node}"/compatible ] && continue
34		# Check if node is available
35		if [[ -e "${node}"/status ]]; then
36			status=$(tr -d '\000' < "${node}"/status)
37			[[ "${status}" != "okay" && "${status}" != "ok" ]] && continue
38		fi
39		echo "${node}" | sed -e 's|\/proc\/device-tree||'
40	done | sort
41	)
42
43nodes_dev_bound=$(
44	IFS=$'\n'
45	for dev_dir in $(find /sys/devices -type d); do
46		[ ! -f "${dev_dir}"/uevent ] && continue
47		[ ! -d "${dev_dir}"/driver ] && continue
48
49		grep '^OF_FULLNAME=' "${dev_dir}"/uevent | sed -e 's|OF_FULLNAME=||'
50	done
51	)
52
53num_tests=$(echo ${nodes_compatible} | wc -w)
54ktap_set_plan "${num_tests}"
55
56retval="${KSFT_PASS}"
57for node in ${nodes_compatible}; do
58	if ! echo "${nodes_dev_bound}" | grep -E -q "(^| )${node}( |\$)"; then
59		compatibles=$(tr '\000' '\n' < "${PDT}"/"${node}"/compatible)
60
61		for compatible in ${compatibles}; do
62			if grep -x -q "${compatible}" "${IGNORE_LIST}"; then
63				continue
64			fi
65
66			if grep -x -q "${compatible}" "${COMPAT_LIST}"; then
67				ktap_test_fail "${node}"
68				retval="${KSFT_FAIL}"
69				continue 2
70			fi
71		done
72		ktap_test_skip "${node}"
73	else
74		ktap_test_pass "${node}"
75	fi
76
77done
78
79ktap_print_totals
80exit "${retval}"
81