Skip to content

Commit bfa8930

Browse files
committed
test: add test: jack detection if DSP is in D3
Add a new test case to test jack detection during DSP is in D3 state. The test will check if the jack detection status is updated correctly when the jack is plugged in and unplugged and also if DSP status is changing correctly as well. For unplug/plug headset jack is using a USB relay. https://github.com/darrylb123/usbrelay Signed-off-by: Artur Wilczak <arturx.wilczak@intel.com>
1 parent 4380849 commit bfa8930

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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+
source "${TESTLIB}/lib.sh"
26+
source "${TESTLIB}/relay.sh"
27+
28+
# shellcheck disable=SC2153
29+
OPT_NAME['t']='tplg' OPT_DESC['t']="tplg file, default value is env TPLG: $TPLG"
30+
OPT_HAS_ARG['t']=1 OPT_VAL['t']="$TPLG"
31+
32+
OPT_NAME['l']='loop' OPT_DESC['l']='loop count'
33+
OPT_HAS_ARG['l']=1 OPT_VAL['l']=1
34+
35+
OPT_NAME['s']='sof-logger' OPT_DESC['s']="open sof-logger trace the data will store at $LOG_ROOT"
36+
OPT_HAS_ARG['s']=0 OPT_VAL['s']=1
37+
38+
OPT_NAME['d']='dsp-settle-sleep' OPT_DESC['d']='waiting time to change DSP state'
39+
OPT_HAS_ARG['d']=1 OPT_VAL['d']=5
40+
41+
OPT_NAME['r']='relay-settle-sleep' OPT_DESC['r']='waiting time to stabilize after relay change state'
42+
OPT_HAS_ARG['r']=1 OPT_VAL['r']=1
43+
44+
OPT_NAME['u']='relay' OPT_DESC['u']='name of usbrelay switch, default value is HURTM_2'
45+
OPT_HAS_ARG['u']=1 OPT_VAL['u']='HURTM_2'
46+
47+
OPT_NAME['H']='headphone' OPT_DESC['H']='name of pcm control for headphone jack'
48+
OPT_HAS_ARG['H']=1 OPT_VAL['H']='headphone jack'
49+
50+
OPT_NAME['M']='headset' OPT_DESC['M']='name of pcm control for headset mic jack'
51+
OPT_HAS_ARG['M']=1 OPT_VAL['M']='headset [a-z ]*jack'
52+
53+
func_opt_parse_option "$@"
54+
55+
tplg=${OPT_VAL['t']}
56+
relay=${OPT_VAL['u']}
57+
loop_cnt=${OPT_VAL['l']}
58+
dsp_settle_time=${OPT_VAL['d']}
59+
relay_settle_time=${OPT_VAL['r']}
60+
headphone_jack_name=${OPT_VAL['H']}
61+
headset_mic_jack_name=${OPT_VAL['M']}
62+
63+
64+
func_check_dsp_status()
65+
{
66+
dlogi "Wait for DSP power status to become suspended"
67+
for i in $(seq 1 "$1")
68+
do
69+
# Here we pass a hardcoded 0 to python script, and need to ensure
70+
# DSP is the first audio pci device in 'lspci', this is true unless
71+
# we have a third-party pci sound card installed.
72+
[[ $(sof-dump-status.py --dsp_status 0) == "suspended" ]] && break
73+
sleep 1
74+
if [ "$i" -eq "$1" ]; then
75+
die "DSP is not suspended after $1s, end test"
76+
fi
77+
done
78+
dlogi "DSP suspended in ${i}s"
79+
}
80+
81+
check_control_switch_state()
82+
{
83+
# Check the state of the switch using amixer.
84+
# The switch name is passed as the first argument, and the expected state (on/off)
85+
# is passed as the second argument.
86+
# Returns 0 if the state matches, 1 otherwise.
87+
local control_name="$1"
88+
local expected_control_state="$2"
89+
local control_state
90+
91+
dlogi "Check if the state of control: $control_name is correct."
92+
control_state=$(amixer -c "$SOFCARD" contents | \
93+
gawk -v name="$control_name" -f "${TESTLIB}/control_state.awk")
94+
dlogi "$control_name switch is: $control_state"
95+
96+
if [ "$expected_control_state" = "$control_state" ]; then
97+
return 0
98+
else
99+
dloge "Expected control state ($expected_control_state) but got ($control_state)."
100+
return 1
101+
fi
102+
}
103+
104+
testing_one_pcm()
105+
{
106+
dlogi "===== Testing: (Round: $i/$loop_cnt) (PCM: $pcm [$dev]<$type>) ====="
107+
dlogi "DEVICE: $dev, TYPE: $type, PCM: $pcm, RATE: $rate, CHANNEL: $channel"
108+
109+
if [ "$(sof-dump-status.py --dsp_status 0)" != "suspended" ] ; then
110+
die "Current DSP status is not suspended."
111+
fi
112+
113+
dlogi "Unplug jack audio."
114+
usbrelay_switch "$relay" 1
115+
116+
dlogi "Wait for ${relay_settle_time}s to ensure jack detection is off"
117+
sleep "$relay_settle_time"
118+
119+
if ! check_control_switch_state "$headset_mic_jack_name" "off"; then
120+
die "unplug $headset_mic_jack_name failed."
121+
fi
122+
123+
if ! check_control_switch_state "$headphone_jack_name" "off"; then
124+
die "unplug $headphone_jack_name failed."
125+
fi
126+
127+
func_check_dsp_status "$dsp_settle_time"
128+
129+
dlogi "Plug jack audio."
130+
usbrelay_switch "$relay" 0
131+
132+
dlogi "Wait for ${relay_settle_time}s to ensure jack detection is on"
133+
sleep "$relay_settle_time"
134+
135+
if ! check_control_switch_state "$headset_mic_jack_name" "on"; then
136+
die "Plug $headset_mic_jack_name failed."
137+
fi
138+
139+
if ! check_control_switch_state "$headphone_jack_name" "on"; then
140+
die "Plug $headphone_jack_name failed."
141+
fi
142+
143+
func_check_dsp_status "$dsp_settle_time"
144+
}
145+
146+
main()
147+
{
148+
func_pipeline_export "$tplg" "type:playback"
149+
150+
setup_kernel_check_point
151+
152+
start_test
153+
154+
dlogi "Checking usbrelay availability..."
155+
if ! command -v usbrelay > /dev/null; then
156+
# If usbrelay package is not installed
157+
skip_test "usbrelay command not found."
158+
fi
159+
160+
if ! usbrelay_switch --debug > /dev/null; then
161+
skip_test "Failed to get usbrelay status."
162+
fi
163+
164+
dlogi "Reset USB Relay - plug jack audio."
165+
usbrelay_switch "$relay" 0
166+
167+
for idx in $(seq 0 $((PIPELINE_COUNT - 1)))
168+
do
169+
initialize_audio_params "$idx"
170+
171+
if [[ "$pcm" != *"Jack"* ]] ; then
172+
dlogi "PCM $pcm is not a Jack, skipping..."
173+
continue
174+
fi
175+
176+
for i in $(seq 1 "$loop_cnt")
177+
do
178+
testing_one_pcm
179+
sof-kernel-log-check.sh "$KERNEL_CHECKPOINT"
180+
setup_kernel_check_point
181+
done
182+
done
183+
}
184+
185+
{
186+
main "$@"; exit "$?"
187+
}

0 commit comments

Comments
 (0)