Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2716672rdb; Fri, 22 Sep 2023 06:43:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH8Cr/E2leWGdh7ohz7HaWtUE4/jUP7AM0r9+vZgpLyO/qJisW/C3hAPZfXLizQAh/4/OUy X-Received: by 2002:a05:6358:6f8a:b0:143:26ab:1ee8 with SMTP id s10-20020a0563586f8a00b0014326ab1ee8mr6831544rwn.28.1695390186790; Fri, 22 Sep 2023 06:43:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695390186; cv=none; d=google.com; s=arc-20160816; b=aGP1uvOtnJ+WdSbwGB0Tw/mHr8z2NtbBMDPmgyvb86FWpNmlw4EezjC74tmkpRQQS0 6Er256m89rX5nqQ65K8g+6hGK4XoL8cyxjk35cK77rJhyh8VncSEXxeJ8RLHjnBWE3fL L6G/X46CCtNfWzevvbnaXL06sw1dWZD3omTSL8EsrAeaVzfuoHykQrq9vzT5mySUHyix nthnDe0jF8ZNZVX6DiUu540XAzndOnORqomih3o4z8oR5b4RoL2ZkpZ7QsF7GAFT7HoX sziDmVgrcdvwHSTScobx2ywHwmpfelr0/+hVEXBgKDl0n5fn/nai5drbe42Y/FoEdvJP +4Og== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=nZHs65wuBxC3BjUfJ5Q1+mjrtbgySNUK60JxPD6B7FI=; fh=MhCCCRx4c0AeUz43uLzZOKA4YzcBN2mk3tAWrRRVvW4=; b=oylq8Lfgurr4dUmUwdM1sgbPlRhvMvqbsBpbwO5nXAYhOGnfiEHxAm5VsxosjnPoa0 ggJCaKUGS3ti4h6hCiujqoYhw1dxo+HGyt0XgPDNqpKCBqybdo50SPPpGvxCQThpn/bv BuWwkyga5rTElqPPFjZ8lZNdvAWWOmA4CdaHSMLC8xjRCVv5DhKi/s6EOjNzwbvIXhm0 H/LyRRPVRdpmygMjXsfkYcXfCg9Kg5HDyEb3DwnyZadqeQdNQu/aOaIza52bjH3RoSrk ZyuqUaX+xfZcyqiVUu2atncz/IEbqcgywk5eke0llH5F5kBJ5A+PEUob8M03mAN2b0Ck eKPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=VNF0ysBc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id 69-20020a630048000000b00566058702e8si3829188pga.236.2023.09.22.06.42.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 06:43:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=VNF0ysBc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 54F1E837086F; Thu, 21 Sep 2023 14:54:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbjIUVyG (ORCPT + 99 others); Thu, 21 Sep 2023 17:54:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232744AbjIUVxX (ORCPT ); Thu, 21 Sep 2023 17:53:23 -0400 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 246DB3C0E; Thu, 21 Sep 2023 14:49:40 -0700 (PDT) Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38LKTAAW023350; Thu, 21 Sep 2023 21:49:22 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=qcppdkim1; bh=nZHs65wuBxC3BjUfJ5Q1+mjrtbgySNUK60JxPD6B7FI=; b=VNF0ysBc4NDwW1iPLXL1Z/eZ0uYtBBt6A1u0bi0hkrxQPzNIJRdSJBdvFLIDPJy7JcPu lX9Zs3bWoCbvsDbL/pQoiwC6KDzc2uLzkAfzhGTZP8+L9vBbI8h7fWcydt3TPkx3f87E YxYaSV8kzdNH2APuudVTcyLQ/hFAEz3/PHc3VbUl6J/OayOSCFC5fQExakGvthvzHI63 kQYCngKwUQBGVEjCAOWt6lrfuQv3LqFltdvWqI+ciRKPPCVccHoumNAi+VUgQokt5sDZ dCfxkDhF1bLkbMBmzPzLNp5nzJt4wd0dQAv3hb9FzHxfNOh0eBc7NQ0Y2OGpx6LmsxSs Tg== Received: from nalasppmta01.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3t8uknr96y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 21 Sep 2023 21:49:21 +0000 Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 38LLmxvL031086 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 21 Sep 2023 21:48:59 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.1118.36; Thu, 21 Sep 2023 14:48:59 -0700 From: Wesley Cheng To: , , , , , , , , , , , , , , CC: , , , , , Wesley Cheng Subject: [PATCH v7 22/33] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Thu, 21 Sep 2023 14:48:32 -0700 Message-ID: <20230921214843.18450-23-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230921214843.18450-1-quic_wcheng@quicinc.com> References: <20230921214843.18450-1-quic_wcheng@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) 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: kfcaEtSHfao10NiDg4yKa2CwXlBDiaPs X-Proofpoint-ORIG-GUID: kfcaEtSHfao10NiDg4yKa2CwXlBDiaPs X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-21_19,2023-09-21_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 suspectscore=0 bulkscore=0 adultscore=0 impostorscore=0 spamscore=0 lowpriorityscore=0 mlxlogscore=999 priorityscore=1501 phishscore=0 mlxscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2309210189 X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 21 Sep 2023 14:54:10 -0700 (PDT) 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. Signed-off-by: Wesley Cheng --- sound/usb/card.h | 1 + sound/usb/pcm.c | 19 +++++++++++++++++-- sound/usb/qcom/qc_audio_offload.c | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/sound/usb/card.h b/sound/usb/card.h index e26292363cf0..01f7e10f30f4 100644 --- a/sound/usb/card.h +++ b/sound/usb/card.h @@ -164,6 +164,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) */ + unsigned int opened:1; /* pcm device opened */ unsigned int running: 1; /* running status */ unsigned int period_elapsed_pending; /* delay period handling */ diff --git a/sound/usb/pcm.c b/sound/usb/pcm.c index 3adb09ce1702..c2cb52cd5d23 100644 --- a/sound/usb/pcm.c +++ b/sound/usb/pcm.c @@ -1241,8 +1241,15 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) struct snd_usb_stream *as = snd_pcm_substream_chip(substream); struct snd_pcm_runtime *runtime = substream->runtime; struct snd_usb_substream *subs = &as->substream[direction]; + struct snd_usb_audio *chip = subs->stream->chip; int ret; + mutex_lock(&chip->mutex); + if (subs->opened) { + mutex_unlock(&chip->mutex); + return -EBUSY; + } + runtime->hw = snd_usb_hardware; /* need an explicit sync to catch applptr update in low-latency mode */ if (direction == SNDRV_PCM_STREAM_PLAYBACK && @@ -1259,13 +1266,17 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) ret = setup_hw_info(runtime, subs); if (ret < 0) - return ret; + goto out; ret = snd_usb_autoresume(subs->stream->chip); if (ret < 0) - return ret; + goto out; ret = snd_media_stream_init(subs, as->pcm, direction); if (ret < 0) snd_usb_autosuspend(subs->stream->chip); + subs->opened = 1; +out: + mutex_unlock(&chip->mutex); + return ret; } @@ -1274,6 +1285,7 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) int direction = substream->stream; struct snd_usb_stream *as = snd_pcm_substream_chip(substream); struct snd_usb_substream *subs = &as->substream[direction]; + struct snd_usb_audio *chip = subs->stream->chip; int ret; snd_media_stop_pipeline(subs); @@ -1287,6 +1299,9 @@ static int snd_usb_pcm_close(struct snd_pcm_substream *substream) subs->pcm_substream = NULL; snd_usb_autosuspend(subs->stream->chip); + mutex_lock(&chip->mutex); + subs->opened = 0; + mutex_unlock(&chip->mutex); return 0; } diff --git a/sound/usb/qcom/qc_audio_offload.c b/sound/usb/qcom/qc_audio_offload.c index f0977bd8084f..2ce39aa26906 100644 --- a/sound/usb/qcom/qc_audio_offload.c +++ b/sound/usb/qcom/qc_audio_offload.c @@ -1400,12 +1400,17 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle, goto response; } + mutex_lock(&chip->mutex); if (req_msg->enable) { - if (info_idx < 0 || chip->system_suspend) { + if (info_idx < 0 || chip->system_suspend || subs->opened) { ret = -EBUSY; + mutex_unlock(&chip->mutex); + goto response; } + subs->opened = 1; } + mutex_unlock(&chip->mutex); if (req_msg->service_interval_valid) { ret = get_data_interval_from_si(subs, @@ -1427,6 +1432,11 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle, if (!ret) ret = prepare_qmi_response(subs, req_msg, &resp, info_idx); + if (ret < 0) { + mutex_lock(&chip->mutex); + subs->opened = 0; + mutex_unlock(&chip->mutex); + } } else { info = &uadev[pcm_card_num].info[info_idx]; if (info->data_ep_pipe) { @@ -1450,6 +1460,9 @@ static void handle_uaudio_stream_req(struct qmi_handle *handle, } disable_audio_stream(subs); + mutex_lock(&chip->mutex); + subs->opened = 0; + mutex_unlock(&chip->mutex); } response: