xref: /linux/tools/testing/selftests/dt/test_unprobed_devices.sh (revision 233d0bc4d81ca1abf60158bd39b90be97d85840a)
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}"/ktap_helpers.sh
19
20PDT=/proc/device-tree/
21COMPAT_LIST="${DIR}"/compatible_list
22IGNORE_LIST="${DIR}"/compatible_ignore_list
23
24KSFT_PASS=0
25KSFT_FAIL=1
26KSFT_SKIP=4
27
28ktap_print_header
29
30if [[ ! -d "${PDT}" ]]; then
31	ktap_skip_all "${PDT} doesn't exist."
32	exit "${KSFT_SKIP}"
33fi
34
35nodes_compatible=$(
36	for node in $(find ${PDT} -type d); do
37		[ ! -f "${node}"/compatible ] && continue
38		# Check if node is available
39		if [[ -e "${node}"/status ]]; then
40			status=$(tr -d '\000' < "${node}"/status)
41			[[ "${status}" != "okay" && "${status}" != "ok" ]] && continue
42		fi
43		echo "${node}" | sed -e 's|\/proc\/device-tree||'
44	done | sort
45	)
46
47nodes_dev_bound=$(
48	IFS=$'\n'
49	for dev_dir in $(find /sys/devices -type d); do
50		[ ! -f "${dev_dir}"/uevent ] && continue
51		[ ! -d "${dev_dir}"/driver ] && continue
52
53		grep '^OF_FULLNAME=' "${dev_dir}"/uevent | sed -e 's|OF_FULLNAME=||'
54	done
55	)
56
57num_tests=$(echo ${nodes_compatible} | wc -w)
58ktap_set_plan "${num_tests}"
59
60retval="${KSFT_PASS}"
61for node in ${nodes_compatible}; do
62	if ! echo "${nodes_dev_bound}" | grep -E -q "(^| )${node}( |\$)"; then
63		compatibles=$(tr '\000' '\n' < "${PDT}"/"${node}"/compatible)
64
65		for compatible in ${compatibles}; do
66			if grep -x -q "${compatible}" "${IGNORE_LIST}"; then
67				continue
68			fi
69
70			if grep -x -q "${compatible}" "${COMPAT_LIST}"; then
71				ktap_test_fail "${node}"
72				retval="${KSFT_FAIL}"
73				continue 2
74			fi
75		done
76		ktap_test_skip "${node}"
77	else
78		ktap_test_pass "${node}"
79	fi
80
81done
82
83ktap_print_totals
84exit "${retval}"
85