From nobody Wed Nov 27 11:54:16 2024 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FD262EB1D; Fri, 11 Oct 2024 00:07:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728605254; cv=none; b=tL+zrtmV2CPZ6WeHQnLjz8LVCqEEK7oW9SZWT3TOiTP45zkznydQXwQ6jbzOWCHSKT0TgOQ9kU1rrMRNuruzWz7Zon/d+9dREwaESp+Z4PYVACLJVxY7rgHhsaBF7o71xiT1fvZMuLNQA3hQY+bLWm/ABO5rgAKCs9870klVNBs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728605254; c=relaxed/simple; bh=5KJ7+nlo+guZwES5cAPb8FiXJM3/tZyf/+ePj0k54QI=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jeEiGnSOBEFI2vTNLWXW+vB07NqbJz5QotkT3sbbNwb2t6LSp2fFbvPDTUg1DTWD34Xo4xtlN2PjysN25Pr2/Hb0SqL19PQ58c+P3fb6CnDv9cimJ4WorgKq++VkO138wgpdcpkETn5uRs8VvWGQaoTYawH1X85SdPU2aYccZ8c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com; spf=pass smtp.mailfrom=quicinc.com; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b=ZuNdGPzA; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=quicinc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="ZuNdGPzA" Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49AC0cRb029507; Fri, 11 Oct 2024 00:07:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= s1yCIbobXIwRRl19rlXryQgqvNgUhDI2Hp1deBw+rA8=; b=ZuNdGPzAUF6eWIrx l53+V3G7v1FllVNnAz6ZbBo67mUHxI6DaeV4Wa0VFh763nkjiUj4H+YqyDuNK+as a38/XCy0PZcFlppOseKq7mrXaAD7XOerX70jjnG/r9EAjGv8k3qFBfy4pvu3HdNM GLEL9bmujXld3nxjQn/25KKYAL4yPjUedAou5l0Kx/H/8m+glGkSyXwaGJ+svItJ WGHDO+UpDyytOzk20wiOyhSSeXu+55z0X0HxDljz/XF/weOYQocZNpFNa0hb79kW F+mOb4EJBVep+FxAbVHKLBYI12BsLgauErE7bQvi2ZlVVViC0FV8msR48ec8C7EV Q3JKVA== Received: from nalasppmta03.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 425c8qydrc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Oct 2024 00:07:10 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA03.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 49B079tE009262 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 11 Oct 2024 00:07:09 GMT Received: from hu-wcheng-lv.qualcomm.com (10.49.16.6) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Thu, 10 Oct 2024 17:07:09 -0700 From: Wesley Cheng To: , , , , , , , , , , , , , , CC: , , , , , , , , Wesley Cheng Subject: [PATCH v28 13/32] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Thu, 10 Oct 2024 17:06:10 -0700 Message-ID: <20241011000650.2585600-26-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241011000650.2585600-1-quic_wcheng@quicinc.com> References: <20241011000650.2585600-1-quic_wcheng@quicinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: GLGFU4DI2edZ1Aa8KqclyQrSe1As7-4Y X-Proofpoint-ORIG-GUID: GLGFU4DI2edZ1Aa8KqclyQrSe1As7-4Y X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 lowpriorityscore=0 mlxlogscore=999 malwarescore=0 suspectscore=0 priorityscore=1501 mlxscore=0 spamscore=0 phishscore=0 clxscore=1015 bulkscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2409260000 definitions=main-2410100159 Content-Type: text/plain; charset="utf-8" With USB audio offloading, an audio session is started from the ASoC platform sound card and PCM devices. Likewise, the USB SND path is still readily available for use, in case the non-offload path is desired. In order to prevent the two entities from attempting to use the USB bus, introduce a flag that determines when either paths are in use. If a PCM device is already in use, the check will return an error to userspace notifying that the stream is currently busy. This ensures that only one path is using the USB substream. Reviewed-by: Pierre-Louis Bossart Signed-off-by: Wesley Cheng --- sound/usb/card.h | 1 + sound/usb/pcm.c | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/sound/usb/card.h b/sound/usb/card.h index b65163c60176..cdafc9e9cecd 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -165,6 +165,7 @@ struct snd_usb_substream { unsigned int pkt_offset_adj; /* Bytes to drop from beginning of packets (= for non-compliant devices) */ unsigned int stream_offset_adj; /* Bytes to drop from beginning of stream= (for non-compliant devices) */ =20 + unsigned int opened:1; /* pcm device opened */ unsigned int running: 1; /* running status */ unsigned int period_elapsed_pending; /* delay period handling */ =20 diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 18467da6fd9e..b24ee38fad72 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -1241,8 +1241,17 @@ static int snd_usb_pcm_open(struct snd_pcm_substream= *substream) struct snd_usb_stream *as =3D snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime =3D substream->runtime; struct snd_usb_substream *subs =3D &as->substream[direction]; + struct snd_usb_audio *chip =3D subs->stream->chip; int ret; =20 + mutex_lock(&chip->mutex); + if (subs->opened) { + mutex_unlock(&chip->mutex); + return -EBUSY; + } + subs->opened =3D 1; + mutex_unlock(&chip->mutex); + runtime->hw =3D snd_usb_hardware; /* need an explicit sync to catch applptr update in low-latency mode */ if (direction =3D=3D SNDRV_PCM_STREAM_PLAYBACK && @@ -1259,13 +1268,23 @@ static int snd_usb_pcm_open(struct snd_pcm_substrea= m *substream) =20 ret =3D setup_hw_info(runtime, subs); if (ret < 0) - return ret; + goto err_open; ret =3D snd_usb_autoresume(subs->stream->chip); if (ret < 0) - return ret; + goto err_open; ret =3D snd_media_stream_init(subs, as->pcm, direction); if (ret < 0) - snd_usb_autosuspend(subs->stream->chip); + goto err_resume; + + return 0; + +err_resume: + snd_usb_autosuspend(subs->stream->chip); +err_open: + mutex_lock(&chip->mutex); + subs->opened =3D 0; + mutex_unlock(&chip->mutex); + return ret; } =20 @@ -1274,6 +1293,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream= *substream) int direction =3D substream->stream; struct snd_usb_stream *as =3D snd_pcm_substream_chip(substream); struct snd_usb_substream *subs =3D &as->substream[direction]; + struct snd_usb_audio *chip =3D subs->stream->chip; int ret; =20 snd_media_stop_pipeline(subs); @@ -1287,6 +1307,9 @@ static int snd_usb_pcm_close(struct snd_pcm_substream= *substream) =20 subs->pcm_substream =3D NULL; snd_usb_autosuspend(subs->stream->chip); + mutex_lock(&chip->mutex); + subs->opened =3D 0; + mutex_unlock(&chip->mutex); =20 return 0; }