|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# SPDX-License-Identifier: BSD-3-Clause |
| 4 | +# Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 5 | + |
| 6 | +## |
| 7 | +## Preconditions |
| 8 | +# USB relay switch is available and configured. |
| 9 | +# Jack detection header should be connected to the USB relay switch |
| 10 | +# to the port HURTM_2 (NC) connector. |
| 11 | + |
| 12 | +## Test Description |
| 13 | +# Verify jack detection functionality by simulating plugging and unplugging |
| 14 | +# of audio jack using a USB relay switch. |
| 15 | +# The unplugging and plugging of the jack is when DSP is in D3 state (suspend) |
| 16 | +# to ensure the system can handle jack detection events correctly. |
| 17 | +# The test will check if the jack detection status is updated correctly |
| 18 | +# when the jack is plugged in and unplugged and if DSP status is changing |
| 19 | +# correctly as well. |
| 20 | + |
| 21 | + |
| 22 | +TESTDIR=$(realpath -e "$(dirname "${BASH_SOURCE[0]}")/..") |
| 23 | +TESTLIB="${TESTDIR}/case-lib" |
| 24 | + |
| 25 | +DSP_SUSPEND_TIMEOUT=15 |
| 26 | + |
| 27 | +# shellcheck disable=SC1091 source=case-lib/lib.sh |
| 28 | +source "${TESTLIB}/lib.sh" |
| 29 | +# shellcheck disable=SC1091 source=case-lib/relay.sh |
| 30 | +source "${TESTLIB}/relay.sh" |
| 31 | + |
| 32 | +# shellcheck disable=SC2153 |
| 33 | +OPT_NAME['t']='tplg' OPT_DESC['t']="tplg file, default value is env TPLG: $TPLG" |
| 34 | +OPT_HAS_ARG['t']=1 OPT_VAL['t']="$TPLG" |
| 35 | + |
| 36 | +OPT_NAME['l']='loop' OPT_DESC['l']='loop count' |
| 37 | +OPT_HAS_ARG['l']=1 OPT_VAL['l']=1 |
| 38 | + |
| 39 | +OPT_NAME['s']='sof-logger' OPT_DESC['s']="open sof-logger trace the data will store at $LOG_ROOT" |
| 40 | +OPT_HAS_ARG['s']=0 OPT_VAL['s']=1 |
| 41 | + |
| 42 | +OPT_NAME['d']='dsp-settle-sleep' OPT_DESC['d']='waiting time to change DSP state' |
| 43 | +OPT_HAS_ARG['d']=1 OPT_VAL['d']=5 |
| 44 | + |
| 45 | +OPT_NAME['r']='relay-settle-sleep' OPT_DESC['r']='waiting time to stabilize after relay change state' |
| 46 | +OPT_HAS_ARG['r']=1 OPT_VAL['r']=1 |
| 47 | + |
| 48 | +OPT_NAME['u']='relay' OPT_DESC['u']='name of usbrelay switch, default value is HURTM_2' |
| 49 | +OPT_HAS_ARG['u']=1 OPT_VAL['u']='HURTM_2' |
| 50 | + |
| 51 | +OPT_NAME['H']='headphone' OPT_DESC['H']='name of pcm control for headphone jack' |
| 52 | +OPT_HAS_ARG['H']=1 OPT_VAL['H']='headphone jack' |
| 53 | + |
| 54 | +OPT_NAME['M']='headset' OPT_DESC['M']='name of pcm control for headset mic jack' |
| 55 | +OPT_HAS_ARG['M']=1 OPT_VAL['M']='headset [a-z ]*jack' |
| 56 | + |
| 57 | +func_opt_parse_option "$@" |
| 58 | + |
| 59 | +tplg=${OPT_VAL['t']} |
| 60 | +relay=${OPT_VAL['u']} |
| 61 | +loop_cnt=${OPT_VAL['l']} |
| 62 | +dsp_settle_time=${OPT_VAL['d']} |
| 63 | +relay_settle_time=${OPT_VAL['r']} |
| 64 | +headphone_jack_name=${OPT_VAL['H']} |
| 65 | +headset_mic_jack_name=${OPT_VAL['M']} |
| 66 | + |
| 67 | + |
| 68 | +func_check_dsp_status() |
| 69 | +{ |
| 70 | + dlogi "Wait for DSP power status to become suspended" |
| 71 | + for i in $(seq 1 "$1") |
| 72 | + do |
| 73 | + # Here we pass a hardcoded 0 to python script, and need to ensure |
| 74 | + # DSP is the first audio pci device in 'lspci', this is true unless |
| 75 | + # we have a third-party pci sound card installed. |
| 76 | + [[ $(sof-dump-status.py --dsp_status 0) == "suspended" ]] && break |
| 77 | + sleep 1 |
| 78 | + if [ "$i" -eq "$1" ]; then |
| 79 | + die "DSP is not suspended after $1s, end test" |
| 80 | + fi |
| 81 | + done |
| 82 | + dlogi "DSP suspended in ${i}s" |
| 83 | +} |
| 84 | + |
| 85 | +check_control_switch_state() |
| 86 | +{ |
| 87 | + # Check the state of the switch using amixer. |
| 88 | + # The switch name is passed as the first argument, and the expected state (on/off) |
| 89 | + # is passed as the second argument. |
| 90 | + # Returns 0 if the state matches, 1 otherwise. |
| 91 | + local control_name="$1" |
| 92 | + local expected_control_state="$2" |
| 93 | + local control_state |
| 94 | + |
| 95 | + dlogi "Check if the state of control: $control_name is correct." |
| 96 | + control_state=$(amixer -c "$SOFCARD" contents | \ |
| 97 | + gawk -v name="$control_name" -f "${TESTLIB}/control_state.awk") |
| 98 | + dlogi "$control_name switch is: $control_state" |
| 99 | + |
| 100 | + if [ "$expected_control_state" = "$control_state" ]; then |
| 101 | + return 0 |
| 102 | + else |
| 103 | + dloge "Expected control state ($expected_control_state) but got ($control_state)." |
| 104 | + return 1 |
| 105 | + fi |
| 106 | +} |
| 107 | + |
| 108 | +testing_one_pcm() |
| 109 | +{ |
| 110 | + dlogi "===== Testing: (Round: $i/$loop_cnt) (PCM: $pcm [$dev]<$type>) =====" |
| 111 | + dlogi "DEVICE: $dev, TYPE: $type, PCM: $pcm, RATE: $rate, CHANNEL: $channel" |
| 112 | + |
| 113 | + local dsp_status |
| 114 | + local timeout=0 |
| 115 | + while [ "$timeout" -lt "$DSP_SUSPEND_TIMEOUT" ] ; do |
| 116 | + dsp_status=$(sof-dump-status.py --dsp_status 0) |
| 117 | + if [ "$dsp_status" = "suspended" ] ; then |
| 118 | + break |
| 119 | + fi |
| 120 | + dlogi "Current DSP status is $dsp_status, waiting for it to be suspended..." |
| 121 | + sleep 1 |
| 122 | + timeout=$((timeout + 1)) |
| 123 | + done |
| 124 | + |
| 125 | + if [ "$dsp_status" != "suspended" ] ; then |
| 126 | + die "Current DSP status ($dsp_status) is not suspended." |
| 127 | + fi |
| 128 | + |
| 129 | + dlogi "Unplug jack audio." |
| 130 | + usbrelay_switch "$relay" 1 |
| 131 | + |
| 132 | + dlogi "Wait for ${relay_settle_time}s to ensure jack detection is off" |
| 133 | + sleep "$relay_settle_time" |
| 134 | + |
| 135 | + if ! check_control_switch_state "$headset_mic_jack_name" "off"; then |
| 136 | + die "unplug $headset_mic_jack_name failed." |
| 137 | + fi |
| 138 | + |
| 139 | + if ! check_control_switch_state "$headphone_jack_name" "off"; then |
| 140 | + die "unplug $headphone_jack_name failed." |
| 141 | + fi |
| 142 | + |
| 143 | + func_check_dsp_status "$dsp_settle_time" |
| 144 | + |
| 145 | + dlogi "Plug jack audio." |
| 146 | + usbrelay_switch "$relay" 0 |
| 147 | + |
| 148 | + dlogi "Wait for ${relay_settle_time}s to ensure jack detection is on" |
| 149 | + sleep "$relay_settle_time" |
| 150 | + |
| 151 | + if ! check_control_switch_state "$headset_mic_jack_name" "on"; then |
| 152 | + die "Plug $headset_mic_jack_name failed." |
| 153 | + fi |
| 154 | + |
| 155 | + if ! check_control_switch_state "$headphone_jack_name" "on"; then |
| 156 | + die "Plug $headphone_jack_name failed." |
| 157 | + fi |
| 158 | + |
| 159 | + func_check_dsp_status "$dsp_settle_time" |
| 160 | +} |
| 161 | + |
| 162 | +main() |
| 163 | +{ |
| 164 | + func_pipeline_export "$tplg" "type:playback" |
| 165 | + |
| 166 | + setup_kernel_check_point |
| 167 | + |
| 168 | + start_test |
| 169 | + |
| 170 | + dlogi "Checking usbrelay availability..." |
| 171 | + if ! command -v usbrelay > /dev/null; then |
| 172 | + # If usbrelay package is not installed |
| 173 | + skip_test "usbrelay command not found." |
| 174 | + fi |
| 175 | + |
| 176 | + if ! usbrelay_switch --debug > /dev/null; then |
| 177 | + skip_test "Failed to get usbrelay status." |
| 178 | + fi |
| 179 | + |
| 180 | + dlogi "Reset USB Relay - plug jack audio." |
| 181 | + usbrelay_switch "$relay" 0 |
| 182 | + |
| 183 | + for idx in $(seq 0 $((PIPELINE_COUNT - 1))) |
| 184 | + do |
| 185 | + initialize_audio_params "$idx" |
| 186 | + |
| 187 | + if [[ "$pcm" != *"Jack"* ]] ; then |
| 188 | + dlogi "PCM $pcm is not a Jack, skipping..." |
| 189 | + continue |
| 190 | + fi |
| 191 | + |
| 192 | + for i in $(seq 1 "$loop_cnt") |
| 193 | + do |
| 194 | + testing_one_pcm |
| 195 | + sof-kernel-log-check.sh "$KERNEL_CHECKPOINT" |
| 196 | + setup_kernel_check_point |
| 197 | + done |
| 198 | + done |
| 199 | +} |
| 200 | + |
| 201 | +{ |
| 202 | + main "$@"; exit "$?" |
| 203 | +} |
0 commit comments