Received: by 2002:a89:48b:0:b0:1f5:f2ab:c469 with SMTP id a11csp1408111lqd; Thu, 25 Apr 2024 14:59:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXyb7OhkMOHOkHC0rnAH5UYIdQ4PGeSbJcIdE83eDDAskVamNGQ+czOG68S6QFiounq4l/UKzRvCUqRLK3nPZOia1CP0ja7L4gQhcAYng== X-Google-Smtp-Source: AGHT+IGihfZtbtYnxjzqD6dOlD5OkcUjZTJ5vnNp3ZDq1f3euWKswdnrHsMQlEisu+/EqlGUpOGY X-Received: by 2002:a17:902:d4cc:b0:1ea:c8fe:238f with SMTP id o12-20020a170902d4cc00b001eac8fe238fmr1321181plg.0.1714082342963; Thu, 25 Apr 2024 14:59:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714082342; cv=pass; d=google.com; s=arc-20160816; b=NQeJLzbpvWsLZzmKqW/ANM5SbGpvZGA1O6VHOS7utY4kSdqrW903x8TLLiIrSk1Dot UVXWB8ugtl5lMQ/SNcESF5mmHx1S/dsHWYqflc14F8g7fvZrrhCh/Ym1i+7CMem/5K6W eLKoExwS12fgyt9supHUFAipBSY7lSjYZDL3nDhHYCt2PTFmanz+U71PSKScNL/pJNjg 19N/IRidVztk8pH1abKCtvi1oLfDMz+UlpvFtzmfKkC60L+ivG+J5qOnupEyetzLBcNq MGiBIfjvZvjaehjRwgzmu5m24/g8Sgq5fU0ejENdyzn/b4Ml1vs8J5H+X9UQ2dHi57pM krKg== 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=TpwOAfyqOPAgZ/VwWPB85gMdXpP36sPB+y45fk5KoPQ=; fh=usWEz1eJjYCle08WABmDyuqCrJGq9m48/LVlmybnzRs=; b=CThYkjqPaYY3zr22aROxxoOM7z1SjrIxz1zo35FFWxCT5xzdIqu2snttwGTCu16Bq2 WFpCTxOrOjcEymni/RSbqYQHeytyUU/vNNUUJl/P7XA4bz4FHaZ81LIEJy5/Ya4nqihN jd3wjloG8K6FcIv41BwqjXy+RSzKw4zqbjMmCOSt3Ecac2spYojZH0suWrLsoiTTrJfL Uer/JCGsjITY7Iom6bSuXWMZccy4TWUu5wm5KdJtWj+bvtBs850QIcnvi31Jn3MvQB0y 73l9XmItwyVUFbU+4NYenV7upBvoJTJckBB151HLdBgivTv9qJYL2H/UQ9gQS1t3n6bD FKVg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=lRUPpFq1; 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-159263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159263-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l14-20020a170903120e00b001e67732a182si13687559plh.637.2024.04.25.14.59.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Apr 2024 14:59:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-159263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=lRUPpFq1; 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-159263-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-159263-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 90F7A28B4C4 for ; Thu, 25 Apr 2024 21:59:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9446A181316; Thu, 25 Apr 2024 21:52:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="lRUPpFq1" Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.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 3AC3717F38B; Thu, 25 Apr 2024 21:52:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714081939; cv=none; b=Z6IuotF0+8OWV9bzcOBIvFEu8FO9x1/ElYTxYkdrv6OTY2dyJIdq8q0fn2/M23+0HtsEfkyy/iIDi9Cp8T0w2xqfWebRpotiaYDkeMVBIv4ivAjC2+umfeAIEEOkYDNBKG9YoWs/nk4n0dZHDjHhok06IgdRHgw3dx84AArW6jI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714081939; c=relaxed/simple; bh=c9cBIvB9gJ0iRjmdwjmxFBq3GIAlsGXKejPSwin0vYg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OIJRTEooqoqEHlB6Lka9fMjHOuH3fOJQiyL5h8t9r0gb8TG563nJrcusmq36U4ls5tXczZ/gMKd5PBPZuCF2WJy2FWJbX5gjTF/J42a13qdIclDewrcl33YSQv4fAgseirlwi/I1uSqOzPW5uncnLQUjEoNjsJWtfxmR+7vE9r4= 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=lRUPpFq1; arc=none smtp.client-ip=205.220.168.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 (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 43PL8Sv7004910; Thu, 25 Apr 2024 21:51:44 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=TpwOAfyqOPAgZ/VwWPB8 5gMdXpP36sPB+y45fk5KoPQ=; b=lRUPpFq1BWjmDUP6BzD9Fvz3f/2CxYKRSR/c B4X28sdl524SyDZJn0eA2fcwc6BCU8AyeBF0sSlAYtA9ltMVtBptB04uM/hLcr/N p333DROi4MYxLs6zJp4BgAfSAQNwXZnANbRy4IuG1a11RGnF5RFFvDDZFW66Wgyg LG8MowbjEWHS3rjXEKlVcZ68law26oqcnwQGF4dvKyi8xzg3ALg221B8py21r7vF SjcJmTaCanX13fDzOrR7mEeVQulnxQQZEQKHgemuTlXZ+ovlf3xUL727rzG/3hon WY0aqU2DR3kCZGkub4zja2kOwFdc04h+SzaA2E/G1Aj6nYD7dw== Received: from nalasppmta05.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3xqenh3s64-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 21:51:44 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA05.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 43PLph3L008032 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 25 Apr 2024 21:51:43 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, 25 Apr 2024 14:51:43 -0700 From: Wesley Cheng To: , , , , , , , , , , , , , , CC: , , , , , , , Wesley Cheng Subject: [PATCH v20 24/41] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Thu, 25 Apr 2024 14:51:08 -0700 Message-ID: <20240425215125.29761-25-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240425215125.29761-1-quic_wcheng@quicinc.com> References: <20240425215125.29761-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: 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-ORIG-GUID: nrJa8qm33rFcxlrClhMUE6sJVbutxsGG X-Proofpoint-GUID: nrJa8qm33rFcxlrClhMUE6sJVbutxsGG X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1011,Hydra:6.0.650,FMLib:17.11.176.26 definitions=2024-04-25_21,2024-04-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 malwarescore=0 impostorscore=0 spamscore=0 priorityscore=1501 mlxscore=0 mlxlogscore=999 bulkscore=0 clxscore=1015 suspectscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404250159 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 d66cc0b139af..3c900e5afbce 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; }