Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp598343rdb; Thu, 8 Feb 2024 15:32:08 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVEBEIRKlSPWA2BbRlvxHrTjbaFOITtxqQiWlRLVbOA8qxB8GNFBj76cILeC4j8NKl6zdZs5w8N6s914AZyK9pdavXNNLzQZEI7p2TpYQ== X-Google-Smtp-Source: AGHT+IGMzey+4hk7eBJniVfH62+9xVctJ/1L87wWI+QFPpXCzyTtWJtTMGIe+MkpagTuVpfnQPFv X-Received: by 2002:a17:906:2449:b0:a38:9cf1:6dc with SMTP id a9-20020a170906244900b00a389cf106dcmr473077ejb.53.1707435128255; Thu, 08 Feb 2024 15:32:08 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707435128; cv=pass; d=google.com; s=arc-20160816; b=Q+HzVvlBadlNnKtJ1cvfWTXXqlg4lvHZ13Qso8DHZS2rDvNNPnKN0eM0QHH/S9AmXm +NiUlh9awu1+4D3yyhWUriYOd0SEcKs/222c2hW4/P4Jg+Zgg7IuDzipljZ5hGlt5i9R o5n0tJJzkajhgwny/Vox/7fA+AiqotzudszMeh39jy6d08dtQpaqutfgg9odRUO1m77c E97uzPzzhNQ+8BCbnyHOPN1eB2gYVlYzLixbHRFB/9H3lcb81RYuzesRNvzGeWUI0Gd9 Bt9BjUw8LMCfrj7D7cXUQ4q11BBOrp5pm7nHMdv2IDlN5dA9vAWhSRyp1/nnxVtoxYhN BCsw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4JPA4eHmUMHFWIjYZV4HYPfzi6+ok1DDMOkZIqZ6MOQ=; fh=osD4Gzvr2dAddNiSo/GCV/nFUc0YZvPPtfXmzPLaVXk=; b=yprRCCMON13/+WuBG8lR5mKaopq577aHYK8uHOc/eYWb9bTmlYMaDh8D1Av7OxN8Ke VyaY9ni3gFiLICgI5+VRkrpdF+5zuQwcrScciu50wWaMLtS89aMl2NME9gb9940LDuNA 15H45aQcIQPzoxzegsJoFaPXv/lB1XcByCgPltXCRiorQCO95UVuEcg2U8KZB5KpUnVc hzSTIf4LJjMyYLGvKPtZbGPs4wlWBoE+lRa00lBTW8SmB7oPuEa23yo19HzLpbMRKq+o 8X+WgX8jhxNWmFx6qrFPUpqS54/DN1bNsX4qEPSil7gZowL2z8mxoEdCGrhLsO0nunNY 9IPQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Okk+jkH1; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-58853-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58853-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com X-Forwarded-Encrypted: i=2; AJvYcCXIl2jhEqz/czA3L5m2vqGoGInRwcSzeG+E6n/jyBnDKibVaw0JetHnxhgQfPms4bfBWH871DQLd7sz3cwU4EZP9U0bGkQc474b2UV0rg== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j18-20020a170906279200b00a38273c1415si166066ejc.275.2024.02.08.15.32.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 15:32:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-58853-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=Okk+jkH1; arc=pass (i=1 spf=pass spfdomain=quicinc.com dkim=pass dkdomain=quicinc.com dmarc=pass fromdomain=quicinc.com); spf=pass (google.com: domain of linux-kernel+bounces-58853-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-58853-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0564A1F21C0F for ; Thu, 8 Feb 2024 23:32:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AFFD63125; Thu, 8 Feb 2024 23:15:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="Okk+jkH1" 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 B25355786E; Thu, 8 Feb 2024 23:14:51 +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=1707434094; cv=none; b=uPLbCY9Gv8xNstIFLtoKhUNymZpgiQIv1DeYIu8rR9OVjlZqvsbxVSpmPdgkEDR5Tz1C9FncYIZJnzRczPOtPKDYzAPQJwJXc3/7MiAcHcyfXTOZM7cvJiaTD9fpHCSFI2Umk/2xjQT2bMrFVLHpKKDDwPe4KqR5AI/ivg7fejY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707434094; c=relaxed/simple; bh=JtJrswJjSBSTvEffu+QUKkRoYrxTr/tQnHphN5S+Y2U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Zrc9QNsl8Jzt/DBnjq7sybVdgDU/8Q1tnMKgYAjkaBKRg8W3stNf0K9oyM4KBcySOU5V7zeRKevgLbbLlhauHTKBT8jBwwLhJ4QZXD8umPoiyx5IHduczs2KV+e9Ur1TiPYTi5gf+ghTfCXwETH72Np4sMpY0lgRwiELw/9EuBo= 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=Okk+jkH1; 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 Received: from pps.filterd (m0279873.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 418MdPi3016538; Thu, 8 Feb 2024 23:14:26 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=4JPA4eHmUMHFWIjYZV4H YPfzi6+ok1DDMOkZIqZ6MOQ=; b=Okk+jkH1HM+a9NhlFpGg8gfOXP7AOtSSB9C4 k7TF/5rYOHxD1Sl7LHR2OZL7TyzKEhBCtO1Xw84UIOptOdhcRjaTp5u3+ajSCc9a xryH3GbbBZwM7rfZgXwRKHcyRWz6lpEvatw1Cn9WSnsQZIGraY8N/sGNuPFKcNHY LitwQK1Yov9GCZrs3vKwaNDuB0mbhBVtVM/BIwL8ZqYsB4UNa9WcIN3ljo6HhQ0C A31Vg4MNUgZRxgNlKOw4weGvHIog6LmWIbFqyMziO9zfE3GHBEPgrbNuuYSzeUOt nTQ0hd3eqN0DnWZiv82nx81PcJQ58ihEou0vKMecv6Ah05t5uQ== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3w4m3ak1pg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 08 Feb 2024 23:14:26 +0000 (GMT) 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 418NEPlX022108 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 8 Feb 2024 23:14:25 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.40; Thu, 8 Feb 2024 15:14:25 -0800 From: Wesley Cheng To: , , , , , , , , , , , , , , CC: , , , , , , , Wesley Cheng Subject: [PATCH v14 35/53] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Thu, 8 Feb 2024 15:13:48 -0800 Message-ID: <20240208231406.27397-36-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240208231406.27397-1-quic_wcheng@quicinc.com> References: <20240208231406.27397-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-Type: text/plain X-ClientProxiedBy: nalasex01c.na.qualcomm.com (10.47.97.35) 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-ORIG-GUID: TtsJA_TsSQZf7sKMj9gFZPUZajkby-SI X-Proofpoint-GUID: TtsJA_TsSQZf7sKMj9gFZPUZajkby-SI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-02-08_11,2024-02-08_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=0 bulkscore=0 lowpriorityscore=0 impostorscore=0 phishscore=0 mlxlogscore=999 adultscore=0 priorityscore=1501 malwarescore=0 spamscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2401310000 definitions=main-2402080131 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 | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/sound/usb/card.h b/sound/usb/card.h index ed4a664e24e5..6d59995440c3 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) */ + 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..3e6d33de6ce6 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 = 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; + } + subs->opened = 1; + mutex_unlock(&chip->mutex); + 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 +1268,23 @@ static int snd_usb_pcm_open(struct snd_pcm_substream *substream) ret = setup_hw_info(runtime, subs); if (ret < 0) - return ret; + goto err_open; ret = snd_usb_autoresume(subs->stream->chip); if (ret < 0) - return ret; + goto err_open; ret = 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 = 0; + mutex_unlock(&chip->mutex); + return ret; } @@ -1274,6 +1293,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 +1307,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; }