Received: by 2002:ab2:69cc:0:b0:1fd:c486:4f03 with SMTP id n12csp29663lqp; Mon, 10 Jun 2024 17:03:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWlvXq4qowjRbsp9qMVAw4mE2nNBijMJmSs1F8i5/gH8ojG+rsXWUGXpzME/R8sInbpSA/efHHafuyY02nGSsAYaLXu/VSBWv+cEXgBTg== X-Google-Smtp-Source: AGHT+IHdlJUvDE+YQE28q52iUeQPRPi2PphUuT0PdgAuJbzSKz7PtN1CFGzrlMhJWxjfki7y0UWV X-Received: by 2002:a17:906:d196:b0:a6f:1378:1328 with SMTP id a640c23a62f3a-a6f137814f0mr333300866b.10.1718064186457; Mon, 10 Jun 2024 17:03:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718064186; cv=pass; d=google.com; s=arc-20160816; b=L+obz3WAMe7CtgOCf5tnhFrtTwnqPTdR1jec5fCpCtLBKs834W1p4gJgQqdcsJLiAX G0X2LpA590U2erK093bbwIXwe0aaR8Bddb8QtCxrd8rTr8iWSFQbFrm/wEcHirI8uhmZ LWkuEU2/+sAF0rVwSJ5aI/h6pjn0h6clBp0CpxmrGDdUqhpZOCPa98PhNTZ2uOc1Gtw5 05RQWYk6SliU7CFGNXpDAH8dR+JRZHYQsXTjH9VypBXPv5NFZEBS017w8Q22W5dtVRxM iI70t5TtBmkyqBKIVsKRX1lPx3xDO064awm6JUGyWAVkToSfbmSxNamL76Ax3tzYoQcI n/yw== 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=wtHMo7F9o0E380HmQ3oEdtbLMOdVnxgjuxg3Tsny7VY=; b=dhP/FlojXVy0DRKohP3jz3cD8KS0c2tUc5ewi5T/idTGZFmQrXJQWy53HC/8YV3Ym8 IiularIpfxXr/AuAuwyK0FZqwPWAdJW2EiZNX+6JK4Ki/ZwRWHONkA69TsonhX295BIt LdCPzCumLM0ijh0ja8GTLkSf4pU4gVZrJFIfM8WAVoW4ho3QmSXKliUL9MVg9m5UXSfZ ISqCWdDkLn1BDxYAHxuYVHE5EFFw+6bMpEeEZJeQHCxe4X8cz05UwriOg5N6apvW4OhI +plTYT1tCpKCVfWve9qKubTjzKbgdy7PcXHgVYRFsms13AQdxEoRz2fIO0vk2x/Wwx6U VAsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=bb2rhfL6; 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-209080-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209080-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6f209a55e2si128386266b.129.2024.06.10.17.03.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 17:03:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-209080-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=bb2rhfL6; 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-209080-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-209080-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 688841F21F36 for ; Tue, 11 Jun 2024 00:02:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5E54215884C; Mon, 10 Jun 2024 23:58:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=quicinc.com header.i=@quicinc.com header.b="bb2rhfL6" 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 699741534EB; Mon, 10 Jun 2024 23:58:43 +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=1718063925; cv=none; b=HtKQfi+wETNC4KVoqJlrZ/RQBBEkk3rw428QYjBH8l0f2CUV4oui1BFD79Ll+wDdcKGoSsHLcrnSzgcuOft5GjYSISDddXPrzFFzgGMBqOUQRDLcbJnri1jXjK54NrdH84+RZ6r3iEGOYfE6b8VZMjf8hzIlptj6ygCJhlz0mhs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718063925; c=relaxed/simple; bh=c9cBIvB9gJ0iRjmdwjmxFBq3GIAlsGXKejPSwin0vYg=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=qUtVynpVQqnPc6yI7XWAVwoSrOgh2qzK6S573xnWxT6sH0oQMWrAD5DRo3MnKJ/bd1y8RstpFZna0E3j8OBkrWXTg1VYHQxdfz0dyy6GX55cdcebNBZiyYgcFQR4g0wsK420yjd5rbnH3kVq3/Ts/RHOpsoHrG2ng/ckcoKmzLY= 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=bb2rhfL6; 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 (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 45AEDUJ8018769; Mon, 10 Jun 2024 23:58:25 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to; s=qcppdkim1; bh=TpwOAfyqOPAgZ/VwWPB85gMd XpP36sPB+y45fk5KoPQ=; b=bb2rhfL6CA6Ty9H8mv0m+oEMZvPLklvjtciUVnM3 J7vS+ggOYhG2anO0QPbFYJGiHoeo847WagI+UGSgYMUP30oCnEWtptAqBQWnSCWm b2gZi19H8gaVnVeguvVvGzExwXs8Xrp9akUjxrPAsJ0+WQA1ih6G1bh1E6oEAelo v88rY9ny9HxbtcUFd/+SKN3PSQn/wepOLycLivvKG5BiPn+3Db9+Vz3N7/ugWGhc IXkTnV7nSooCVM6sGCGmfZg69H9Nj+cGlX+J+0EWzKUB2w446+WoisQWVgNkABmN RMKWLUedBtDiJ4QrIEP01qua2HxQXMGHVG4GYCOhHAgCLA== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3ymemgmyb0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 10 Jun 2024 23:58:25 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA02.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 45ANwNYh016573 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 10 Jun 2024 23:58:23 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; Mon, 10 Jun 2024 16:58:23 -0700 From: Wesley Cheng To: , , , , , , , , , , , , CC: , , , , , , , Wesley Cheng Subject: [PATCH v23 16/32] ALSA: usb-audio: Prevent starting of audio stream if in use Date: Mon, 10 Jun 2024 16:57:52 -0700 Message-ID: <20240610235808.22173-17-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20240610235808.22173-1-quic_wcheng@quicinc.com> References: <20240610235808.22173-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-GUID: -3Psb4xIwpeTQ_paa_FtY-3NuK5hYxM9 X-Proofpoint-ORIG-GUID: -3Psb4xIwpeTQ_paa_FtY-3NuK5hYxM9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-06-10_06,2024-06-10_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 impostorscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 phishscore=0 malwarescore=0 mlxscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405170001 definitions=main-2406100176 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; }