From nobody Wed Dec 17 12:47:21 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D632C4167B for ; Mon, 27 Nov 2023 23:36:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233862AbjK0XgP (ORCPT ); Mon, 27 Nov 2023 18:36:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233697AbjK0XgH (ORCPT ); Mon, 27 Nov 2023 18:36:07 -0500 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D9C5B1B8; Mon, 27 Nov 2023 15:36:12 -0800 (PST) Received: from notapiano.myfiosgateway.com (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nfraprado) by madras.collabora.co.uk (Postfix) with ESMTPSA id A4CA56607286; Mon, 27 Nov 2023 23:36:07 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1701128171; bh=uszfqMsRyEw54/XQFNZxt8fdYO5WUerKHy+2zOlCCgg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n2p++2b+KtXB0WF15oFP3ygUdj8gd69ZgSggXl1cbnsTZZJyY6igjbC+Ujwfr53i7 Ufo1jzKmoinwhvsbOPGLkW+3gdhiWSwnfxKh6A/EzNpBYlbfjzNk7B348gYRhbyPz8 gW7mGRlLrwsaGjdRILs/m+Y36uEqUNhNGUb7ZgrRUUxt0taN5xIRc53f+jUCZeBliL LjXy9S4OxsR3OZ4SxMO/PNEOi5ughBjb+wWAdIcsnse5WajqbWSBO7+edpq1LmgYzl BjxK5scWjK+CZMgFrRsZ4+ATPWMLZccNBkwoDTqZktQSQUwHHAfLeO0VRM5RIzMwOr ZohhinjpSkCAg== From: =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= To: Shuah Khan , Greg Kroah-Hartman , Bjorn Helgaas Cc: Saravana Kannan , Rob Herring , kernelci@lists.linux.dev, David Gow , Guenter Roeck , linux-kselftest@vger.kernel.org, linux-usb@vger.kernel.org, kernel@collabora.com, Dan Carpenter , Tim Bird , linux-pci@vger.kernel.org, Doug Anderson , devicetree@vger.kernel.org, =?UTF-8?q?N=C3=ADcolas=20F=2E=20R=2E=20A=2E=20Prado?= , linux-kernel@vger.kernel.org Subject: [RFC PATCH v2 1/2] kselftest: Add test to verify probe of devices from discoverable busses Date: Mon, 27 Nov 2023 18:34:06 -0500 Message-ID: <20231127233558.868365-2-nfraprado@collabora.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231127233558.868365-1-nfraprado@collabora.com> References: <20231127233558.868365-1-nfraprado@collabora.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a new test to verify that a list of expected devices on a given platform have been successfully probed by a driver. Add a new test to verify that all expected devices from discoverable busses (ie USB, PCI) have been successfully instantiated and probed by a driver. The per-platform list of expected devices is selected from the ones under the boards/ directory based on compatible. Signed-off-by: N=C3=ADcolas F. R. A. Prado --- (no changes since v1) tools/testing/selftests/Makefile | 1 + tools/testing/selftests/devices/.gitignore | 1 + tools/testing/selftests/devices/Makefile | 8 + .../devices/test_discoverable_devices.sh | 160 ++++++++++++++++++ 4 files changed, 170 insertions(+) create mode 100644 tools/testing/selftests/devices/.gitignore create mode 100644 tools/testing/selftests/devices/Makefile create mode 100755 tools/testing/selftests/devices/test_discoverable_devic= es.sh diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Mak= efile index 3b2061d1c1a5..7f5088006c3c 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile @@ -13,6 +13,7 @@ TARGETS +=3D core TARGETS +=3D cpufreq TARGETS +=3D cpu-hotplug TARGETS +=3D damon +TARGETS +=3D devices TARGETS +=3D dmabuf-heaps TARGETS +=3D drivers/dma-buf TARGETS +=3D drivers/s390x/uvdevice diff --git a/tools/testing/selftests/devices/.gitignore b/tools/testing/sel= ftests/devices/.gitignore new file mode 100644 index 000000000000..e3c5c04d1b19 --- /dev/null +++ b/tools/testing/selftests/devices/.gitignore @@ -0,0 +1 @@ +ktap_helpers.sh diff --git a/tools/testing/selftests/devices/Makefile b/tools/testing/selft= ests/devices/Makefile new file mode 100644 index 000000000000..ff2fdc8fc5e2 --- /dev/null +++ b/tools/testing/selftests/devices/Makefile @@ -0,0 +1,8 @@ +TEST_PROGS :=3D test_discoverable_devices.sh +TEST_GEN_FILES :=3D ktap_helpers.sh +TEST_FILES :=3D boards + +include ../lib.mk + +$(OUTPUT)/ktap_helpers.sh: + cp ../dt/ktap_helpers.sh $@ diff --git a/tools/testing/selftests/devices/test_discoverable_devices.sh b= /tools/testing/selftests/devices/test_discoverable_devices.sh new file mode 100755 index 000000000000..82bad5ba7081 --- /dev/null +++ b/tools/testing/selftests/devices/test_discoverable_devices.sh @@ -0,0 +1,160 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2023 Collabora Ltd +# +# This script tests for presence and driver binding of devices from discov= erable +# busses (ie USB, PCI). +# +# The per-platform list of devices to be tested is stored inside the board= s/ +# directory and chosen based on compatible. Each line of the file follows = the +# following format: +# +# usb|pci test_name number_of_matches field=3Dvalue [ field=3Dvalue ... ] +# +# The available match fields vary by bus. The field-value match pairs for a +# device can be retrieved from the device's modalias attribute in sysfs. +# + +DIR=3D"$(dirname $(readlink -f "$0"))" + +source "${DIR}"/ktap_helpers.sh + +KSFT_FAIL=3D1 +KSFT_SKIP=3D4 + +retval=3D0 + +match() +{ + FILE=3D"$1" + ID=3D"$2" + + [ ! -f "$FILE" ] && return 1 + [ "$ID" =3D "" ] && return 0 + grep -q "$ID" "$FILE" || return 1 + return 0 +} + +usb() +{ + name=3D"$1" + count=3D"$2" + shift 2 + + for arg in $@; do + [[ "$arg" =3D~ ^v=3D ]] && v=3D"${arg#v=3D}" + [[ "$arg" =3D~ ^p=3D ]] && p=3D"${arg#p=3D}" + [[ "$arg" =3D~ ^d=3D ]] && d=3D"${arg#d=3D}" + [[ "$arg" =3D~ ^dc=3D ]] && dc=3D"${arg#dc=3D}" + [[ "$arg" =3D~ ^dsc=3D ]] && dsc=3D"${arg#dsc=3D}" + [[ "$arg" =3D~ ^dp=3D ]] && dp=3D"${arg#dp=3D}" + [[ "$arg" =3D~ ^ic=3D ]] && ic=3D"${arg#ic=3D}" + [[ "$arg" =3D~ ^isc=3D ]] && isc=3D"${arg#isc=3D}" + [[ "$arg" =3D~ ^ip=3D ]] && ip=3D"${arg#ip=3D}" + [[ "$arg" =3D~ ^in=3D ]] && in=3D"${arg#in=3D}" + done + + cur_count=3D0 + + for dev in $(find /sys/bus/usb/devices -maxdepth 1); do + match "$dev"/idVendor "$v" || continue + match "$dev"/idProduct "$p" || continue + match "$dev"/bcdDevice "$d" || continue + match "$dev"/bDeviceClass "$dc" || continue + match "$dev"/bDeviceSubClass "$dsc" || continue + match "$dev"/bDeviceProtocol "$dp" || continue + + # Matched device. Now search through interfaces + for intf in $(find "$dev"/ -maxdepth 1 -type d); do + match "$intf"/bInterfaceClass "$ic" || continue + match "$intf"/bInterfaceSubClass "$isc" || continue + match "$intf"/bInterfaceProtocol "$ip" || continue + match "$intf"/bInterfaceNumber "$in" || continue + + # Matched interface. Add to count if it was probed by driver. + [ -d "$intf"/driver ] && cur_count=3D$((cur_count+1)) + done + done + + if [ "$cur_count" -eq "$count" ]; then + ktap_test_pass usb."$name" + else + ktap_test_fail usb."$name" + retval=3D"$KSFT_FAIL" + fi +} + +pci() +{ + name=3D"$1" + count=3D"$2" + shift 2 + + for arg in $@; do + [[ "$arg" =3D~ ^v=3D ]] && v=3D"${arg#v=3D}" + [[ "$arg" =3D~ ^d=3D ]] && d=3D"${arg#d=3D}" + [[ "$arg" =3D~ ^sv=3D ]] && sv=3D"${arg#sv=3D}" + [[ "$arg" =3D~ ^sd=3D ]] && sd=3D"${arg#sd=3D}" + [[ "$arg" =3D~ ^bc=3D ]] && bc=3D"${arg#bc=3D}" + [[ "$arg" =3D~ ^sc=3D ]] && sc=3D"${arg#sc=3D}" + [[ "$arg" =3D~ ^i=3D ]] && i=3D"${arg#i=3D}" + done + + cur_count=3D0 + + for dev in $(find /sys/bus/pci/devices -maxdepth 1); do + match "$dev"/vendor "$v" || continue + match "$dev"/device "$d" || continue + match "$dev"/subsystem_vendor "$sv" || continue + match "$dev"/subsystem_device "$sd" || continue + + [ -z "$bc" ] && bc=3D'..' + [ -z "$sc" ] && sc=3D'..' + [ -z "$i" ] && i=3D'..' + match "$dev/"class "$bc$sc$i" || continue + + # Matched device. Add to count if it was probed by driver. + [ -d "$dev"/driver ] && cur_count=3D$((cur_count+1)) + done + + if [ "$cur_count" -eq "$count" ]; then + ktap_test_pass pci."$name" + else + ktap_test_fail pci."$name" + retval=3D"$KSFT_FAIL" + fi +} + +ktap_print_header + +plat_compatible=3D/proc/device-tree/compatible + +if [ ! -f "$plat_compatible" ]; then + ktap_skip_all "No board compatible available" + exit "$KSFT_SKIP" +fi + +compatibles=3D$(tr '\000' '\n' < "$plat_compatible") + +for compatible in $compatibles; do + if [ -f boards/"$compatible" ]; then + board_file=3Dboards/"$compatible" + break + fi +done + +if [ -z "$board_file" ]; then + ktap_skip_all "No matching board file found" + exit "$KSFT_SKIP" +fi + +echo "# Using board file: " "$board_file" + +num_tests=3D$(grep -E "^(usb|pci)" "$board_file" | wc -l) +ktap_set_plan "$num_tests" + +source "$board_file" + +ktap_print_totals +exit "${retval}" --=20 2.42.1