Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp3385576rdg; Tue, 17 Oct 2023 13:03:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEPBjfb97/+lHV+pl5bgqI4VN7aDVpi7+LMWoaKQsx4ZeyCbHuABwwh6krmSQzu8QV4C4Ei X-Received: by 2002:a17:90a:8684:b0:27d:882f:e6cd with SMTP id p4-20020a17090a868400b0027d882fe6cdmr3103619pjn.44.1697573011477; Tue, 17 Oct 2023 13:03:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697573011; cv=none; d=google.com; s=arc-20160816; b=Q8vFM5v9FRvFC1MzuHEcimT09Cr9gZIFCxY2NkhGzrp8NTtcrAOUwfl10Sg5vmSMvp 4j7lT/WFDpNLm3MJvAD/wgBhTgE6SzlL5ugtmYbLU0E12jr5aha/RKhf1Lug29r9N9lo U9b97pvQYVzhoF2x/2DIOdr4KXcPzIMOFdTk62HO7SraN/dTxZ4/557c1qpCXgapRceZ 4zq6PFLOs14K3wW0tAYTlbRYfhIygHQRSaq/BbrgKV6q0yImD8s57Ar5MCsI2D2Q7x5h FK0ZvPmgPQ3DfdjMy44MtY708Qu4O6vgogPE8aH8MCotb6bun1iMP1GY6l2xn2PerR/V eU+g== 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=vKvZJ8/paUyTbfzmfvENqjgOyObTZWvthersPXfiBAM=; fh=ASZxE/GudOQkGCAz0d02NHHSpg4iNHYOcDWl1imlof0=; b=ooMckeLHCUcyjkTB3Tdl9aFjjwFSU907NkLtbSbIRoWq2Z8N8eC1NOU+fM9ihl+/ug jR9VuCr0GnJoJMvJrRfhgUdGveHFSbV8wMW5IKGiAPp19Tz0ctp64jj+Y/xVKmO7gxt0 oDlaZCL3r8WEK0JGvplhGh+gHla5qvSKtoiGgBZfoloRbmjdtX79SzuLDiKtjHADdre+ ffl+uDXrRvN1iZZxvdO/a/eRleV0OCe85N4cREWhvUfjm20BhfTErrV7X0bFwGUkEpFU OYEclj2smDL1JNVaaMPVfyXJW4WCj/I3nSt2abH9n3pfcfjgbCzoVVCovdjZ1OXNBZ5v GZ0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=dPcvQuaB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id q4-20020a632a04000000b0056569ee3ae6si489388pgq.798.2023.10.17.13.03.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 13:03:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=dPcvQuaB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 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 pete.vger.email (Postfix) with ESMTP id 43676802854F; Tue, 17 Oct 2023 13:03:18 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344215AbjJQUCA (ORCPT + 99 others); Tue, 17 Oct 2023 16:02:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35796 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234787AbjJQUBs (ORCPT ); Tue, 17 Oct 2023 16:01:48 -0400 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF9DEF9; Tue, 17 Oct 2023 13:01:46 -0700 (PDT) Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39HJx01M026011; Tue, 17 Oct 2023 20:01:27 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=vKvZJ8/paUyTbfzmfvENqjgOyObTZWvthersPXfiBAM=; b=dPcvQuaBHrfH2w1ba2Rz1Fr/UxWN7ezhYuqIYV67xiQBXQkpA+M71otIDb8vGjy7/Ofv JeMxAJdM9+2l22XQrPF3F2LNXLk8OKLOAGINJUQrMFOST8YNYF1iADaC63Rhyyk3Y75i MabLrWvzhhp6YR9+IBixee4n55C+9mVkHoCwvYHQUWF1moEw0t1X2GnSQfe8Fxn2kTVk G1JA8QXIcMZGjs6KPkd5Q5eqIGapE3VBCx934X6R5Vn74Ai4m1wz9Kr+iIIp1vhOOeS2 MMf9p1dd0Uv/mAXQcNpDLs5c+6j+3bWXcEoQHRXhmD8lKeqXEXZkfC4C1GBy68K+YmGN oA== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3tsnej9qj2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Oct 2023 20:01:27 +0000 Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 39HK1Qcb027382 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Oct 2023 20:01:26 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.39; Tue, 17 Oct 2023 13:01:26 -0700 From: Wesley Cheng To: , , , , , , , , , , , , , , CC: , , , , , Wesley Cheng Subject: [PATCH v9 23/34] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Tue, 17 Oct 2023 13:00:58 -0700 Message-ID: <20231017200109.11407-24-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20231017200109.11407-1-quic_wcheng@quicinc.com> References: <20231017200109.11407-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: _qNx3be26sCbGbS-TINJy7LtsZG3Buau X-Proofpoint-ORIG-GUID: _qNx3be26sCbGbS-TINJy7LtsZG3Buau X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-17_03,2023-10-17_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 spamscore=0 bulkscore=0 impostorscore=0 mlxscore=0 lowpriorityscore=0 suspectscore=0 priorityscore=1501 phishscore=0 malwarescore=0 mlxlogscore=995 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310170169 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.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 (pete.vger.email [0.0.0.0]); Tue, 17 Oct 2023 13:03:18 -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 320ce3a6688f..bd6b84f72c74 100644 --- a/sound/usb/qcom/qc_audio_offload.c +++ b/sound/usb/qcom/qc_audio_offload.c @@ -1413,12 +1413,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, @@ -1440,6 +1445,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) { @@ -1463,6 +1473,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: