xref: /linux/tools/testing/selftests/dt/test_unprobed_devices.sh (revision 54ee3526796f56c249124686a33e1cc05f76ea21)
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_compat in $(find ${PDT} -name compatible); do
33		node=$(dirname "${node_compat}")
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 uevent in $(find /sys/devices -name uevent); do
46		if [[ -d "$(dirname "${uevent}")"/driver ]]; then
47			grep '^OF_FULLNAME=' "${uevent}" | sed -e 's|OF_FULLNAME=||'
48		fi
49	done
50	)
51
52num_tests=$(echo ${nodes_compatible} | wc -w)
53ktap_set_plan "${num_tests}"
54
55retval="${KSFT_PASS}"
56for node in ${nodes_compatible}; do
57	if ! echo "${nodes_dev_bound}" | grep -E -q "(^| )${node}( |\$)"; then
58		compatibles=$(tr '\000' '\n' < "${PDT}"/"${node}"/compatible)
59
60		for compatible in ${compatibles}; do
61			if grep -x -q "${compatible}" "${IGNORE_LIST}"; then
62				continue
63			fi
64
65			if grep -x -q "${compatible}" "${COMPAT_LIST}"; then
66				ktap_test_fail "${node}"
67				retval="${KSFT_FAIL}"
68				continue 2
69			fi
70		done
71		ktap_test_skip "${node}"
72	else
73		ktap_test_pass "${node}"
74	fi
75
76done
77
78ktap_print_totals
79exit "${retval}"
80