1#!/bin/sh 2# SPDX-License-Identifier: GPL-2.0 3# 4# Generate a graph of the current DAPM state for an audio card 5# 6# Copyright 2024 Bootlin 7# Author: Luca Ceresoli <[email protected]> 8 9set -eu 10 11STYLE_NODE_ON="shape=box,style=bold,color=green4" 12STYLE_NODE_OFF="shape=box,style=filled,color=gray30,fillcolor=gray95" 13 14# Print usage and exit 15# 16# $1 = exit return value 17# $2 = error string (required if $1 != 0) 18usage() 19{ 20 if [ "${1}" -ne 0 ]; then 21 echo "${2}" >&2 22 fi 23 24 echo " 25Generate a graph of the current DAPM state for an audio card. 26 27The DAPM state can be obtained via debugfs for a card on the local host or 28a remote target, or from a local copy of the debugfs tree for the card. 29 30Usage: 31 $(basename $0) [options] -c CARD - Local sound card 32 $(basename $0) [options] -c CARD -r REMOTE_TARGET - Card on remote system 33 $(basename $0) [options] -d STATE_DIR - Local directory 34 35Options: 36 -c CARD Sound card to get DAPM state of 37 -r REMOTE_TARGET Get DAPM state from REMOTE_TARGET via SSH and SCP 38 instead of using a local sound card 39 -d STATE_DIR Get DAPM state from a local copy of a debugfs tree 40 -o OUT_FILE Output file (default: dapm.dot) 41 -D Show verbose debugging info 42 -h Print this help and exit 43 44The output format is implied by the extension of OUT_FILE: 45 46 * Use the .dot extension to generate a text graph representation in 47 graphviz dot syntax. 48 * Any other extension is assumed to be a format supported by graphviz for 49 rendering, e.g. 'png', 'svg', and will produce both the .dot file and a 50 picture from it. This requires the 'dot' program from the graphviz 51 package. 52" 53 54 exit ${1} 55} 56 57# Connect to a remote target via SSH, collect all DAPM files from debufs 58# into a tarball and get the tarball via SCP into $3/dapm.tar 59# 60# $1 = target as used by ssh and scp, e.g. "[email protected]" 61# $2 = sound card name 62# $3 = temp dir path (present on the host, created on the target) 63# $4 = local directory to extract the tarball into 64# 65# Requires an ssh+scp server, find and tar+gz on the target 66# 67# Note: the tarball is needed because plain 'scp -r' from debugfs would 68# copy only empty files 69grab_remote_files() 70{ 71 echo "Collecting DAPM state from ${1}" 72 dbg_echo "Collected DAPM state in ${3}" 73 74 ssh "${1}" " 75set -eu && 76cd \"/sys/kernel/debug/asoc/${2}\" && 77find * -type d -exec mkdir -p ${3}/dapm-tree/{} \; && 78find * -type f -exec cp \"{}\" \"${3}/dapm-tree/{}\" \; && 79cd ${3}/dapm-tree && 80tar cf ${3}/dapm.tar ." 81 scp -q "${1}:${3}/dapm.tar" "${3}" 82 83 mkdir -p "${4}" 84 tar xf "${tmp_dir}/dapm.tar" -C "${4}" 85} 86 87# Parse a widget file and generate graph description in graphviz dot format 88# 89# Skips any file named "bias_level". 90# 91# $1 = temporary work dir 92# $2 = component name 93# $3 = widget filename 94process_dapm_widget() 95{ 96 local tmp_dir="${1}" 97 local c_name="${2}" 98 local w_file="${3}" 99 local dot_file="${tmp_dir}/main.dot" 100 local links_file="${tmp_dir}/links.dot" 101 102 local w_name="$(basename "${w_file}")" 103 local w_tag="${c_name}_${w_name}" 104 105 if [ "${w_name}" = "bias_level" ]; then 106 return 0 107 fi 108 109 dbg_echo " + Widget: ${w_name}" 110 111 cat "${w_file}" | ( 112 read line 113 114 if echo "${line}" | grep -q ': On ' 115 then local node_style="${STYLE_NODE_ON}" 116 else local node_style="${STYLE_NODE_OFF}" 117 fi 118 119 local w_type="" 120 while read line; do 121 # Collect widget type if present 122 if echo "${line}" | grep -q '^widget-type '; then 123 local w_type_raw="$(echo "$line" | cut -d ' ' -f 2)" 124 dbg_echo " - Widget type: ${w_type_raw}" 125 126 # Note: escaping '\n' is tricky to get working with both 127 # bash and busybox ash, so use a '%' here and replace it 128 # later 129 local w_type="%n[${w_type_raw}]" 130 fi 131 132 # Collect any links. We could use "in" links or "out" links, 133 # let's use "in" links 134 if echo "${line}" | grep -q '^in '; then 135 local w_src=$(echo "$line" | 136 awk -F\" '{print $6 "_" $4}' | 137 sed 's/^(null)_/ROOT_/') 138 dbg_echo " - Input route from: ${w_src}" 139 echo " \"${w_src}\" -> \"$w_tag\"" >> "${links_file}" 140 fi 141 done 142 143 echo " \"${w_tag}\" [label=\"${w_name}${w_type}\",${node_style}]" | 144 tr '%' '\\' >> "${dot_file}" 145 ) 146} 147 148# Parse the DAPM tree for a sound card component and generate graph 149# description in graphviz dot format 150# 151# $1 = temporary work dir 152# $2 = component directory 153# $3 = "ROOT" for the root card directory, empty otherwise 154process_dapm_component() 155{ 156 local tmp_dir="${1}" 157 local c_dir="${2}" 158 local c_name="${3}" 159 local is_component=0 160 local dot_file="${tmp_dir}/main.dot" 161 local links_file="${tmp_dir}/links.dot" 162 163 if [ -z "${c_name}" ]; then 164 is_component=1 165 166 # Extract directory name into component name: 167 # "./cs42l51.0-004a/dapm" -> "cs42l51.0-004a" 168 c_name="$(basename $(dirname "${c_dir}"))" 169 170 echo "" >> "${dot_file}" 171 echo " subgraph \"${c_name}\" {" >> "${dot_file}" 172 echo " cluster = true" >> "${dot_file}" 173 echo " label = \"${c_name}\"" >> "${dot_file}" 174 echo " color=dodgerblue" >> "${dot_file}" 175 fi 176 177 dbg_echo " * Component: ${c_name}" 178 179 # Create empty file to ensure it will exist in all cases 180 >"${links_file}" 181 182 # Iterate over widgets in the component dir 183 for w_file in ${c_dir}/*; do 184 process_dapm_widget "${tmp_dir}" "${c_name}" "${w_file}" 185 done 186 187 if [ ${is_component} = 1 ]; then 188 echo " }" >> "${dot_file}" 189 fi 190 191 cat "${links_file}" >> "${dot_file}" 192} 193 194# Parse the DAPM tree for a sound card and generate graph description in 195# graphviz dot format 196# 197# $1 = temporary work dir 198# $2 = directory tree with DAPM state (either in debugfs or a mirror) 199process_dapm_tree() 200{ 201 local tmp_dir="${1}" 202 local dapm_dir="${2}" 203 local dot_file="${tmp_dir}/main.dot" 204 205 echo "digraph G {" > "${dot_file}" 206 echo " fontname=\"sans-serif\"" >> "${dot_file}" 207 echo " node [fontname=\"sans-serif\"]" >> "${dot_file}" 208 209 210 # Process root directory (no component) 211 process_dapm_component "${tmp_dir}" "${dapm_dir}/dapm" "ROOT" 212 213 # Iterate over components 214 for c_dir in "${dapm_dir}"/*/dapm 215 do 216 process_dapm_component "${tmp_dir}" "${c_dir}" "" 217 done 218 219 echo "}" >> "${dot_file}" 220} 221 222main() 223{ 224 # Parse command line 225 local out_file="dapm.dot" 226 local card_name="" 227 local remote_target="" 228 local dapm_tree="" 229 local dbg_on="" 230 while getopts "c:r:d:o:Dh" arg; do 231 case $arg in 232 c) card_name="${OPTARG}" ;; 233 r) remote_target="${OPTARG}" ;; 234 d) dapm_tree="${OPTARG}" ;; 235 o) out_file="${OPTARG}" ;; 236 D) dbg_on="1" ;; 237 h) usage 0 ;; 238 *) usage 1 ;; 239 esac 240 done 241 shift $(($OPTIND - 1)) 242 243 if [ -n "${dapm_tree}" ]; then 244 if [ -n "${card_name}${remote_target}" ]; then 245 usage 1 "Cannot use -c and -r with -d" 246 fi 247 echo "Using local tree: ${dapm_tree}" 248 elif [ -n "${remote_target}" ]; then 249 if [ -z "${card_name}" ]; then 250 usage 1 "-r requires -c" 251 fi 252 echo "Using card ${card_name} from remote target ${remote_target}" 253 elif [ -n "${card_name}" ]; then 254 echo "Using local card: ${card_name}" 255 else 256 usage 1 "Please choose mode using -c, -r or -d" 257 fi 258 259 # Define logging function 260 if [ "${dbg_on}" ]; then 261 dbg_echo() { 262 echo "$*" >&2 263 } 264 else 265 dbg_echo() { 266 : 267 } 268 fi 269 270 # Filename must have a dot in order the infer the format from the 271 # extension 272 if ! echo "${out_file}" | grep -qE '\.'; then 273 echo "Missing extension in output filename ${out_file}" >&2 274 usage 275 exit 1 276 fi 277 278 local out_fmt="${out_file##*.}" 279 local dot_file="${out_file%.*}.dot" 280 281 dbg_echo "dot file: $dot_file" 282 dbg_echo "Output file: $out_file" 283 dbg_echo "Output format: $out_fmt" 284 285 tmp_dir="$(mktemp -d /tmp/$(basename $0).XXXXXX)" 286 trap "{ rm -fr ${tmp_dir}; }" INT TERM EXIT 287 288 if [ -z "${dapm_tree}" ] 289 then 290 dapm_tree="/sys/kernel/debug/asoc/${card_name}" 291 fi 292 if [ -n "${remote_target}" ]; then 293 dapm_tree="${tmp_dir}/dapm-tree" 294 grab_remote_files "${remote_target}" "${card_name}" "${tmp_dir}" "${dapm_tree}" 295 fi 296 # In all cases now ${dapm_tree} contains the DAPM state 297 298 process_dapm_tree "${tmp_dir}" "${dapm_tree}" 299 cp "${tmp_dir}/main.dot" "${dot_file}" 300 301 if [ "${out_file}" != "${dot_file}" ]; then 302 dot -T"${out_fmt}" "${dot_file}" -o "${out_file}" 303 fi 304 305 echo "Generated file ${out_file}" 306} 307 308main "${@}" 309