From patchwork Tue Jul 25 02:34:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wesley Cheng X-Patchwork-Id: 125285 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9010:0:b0:3e4:2afc:c1 with SMTP id l16csp2196074vqg; Mon, 24 Jul 2023 20:00:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlGsyXRQEMQ5dD25732E2aJiT7DcUELZ4K56k7+Kyjc4qHGOrsfAcV3TBSBhQczbYhe7Ee8S X-Received: by 2002:a17:907:7631:b0:993:e9b8:90f5 with SMTP id jy17-20020a170907763100b00993e9b890f5mr9887658ejc.8.1690254019519; Mon, 24 Jul 2023 20:00:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690254019; cv=none; d=google.com; s=arc-20160816; b=SBy2Mw0BOzFj//pJNwDk10SwhkUAXaCGTFKIAnLTVkXSBb5kPFDuMwrvGM8HjOJFW1 nvJYX2SJXARX/KP61CSWitUwF6K42BQwMZh79kX9UwwbIlfo921oRJ1h2cAJpDIfTdVI /UL6rj6CW74Bb6TyNMG9qf5XfSvM2V2Zp9Tci4GvfnhFarh8WeulWzs8xVlrB2yGxvPl zB7vfDGFeMVeiSrvrxOpjMFGvGI2gA2VauACPd4xUEDQWn+UDb9sXI0UOf1LVAT9J5or v4TOoemnxCdId6nWWSudRmaqYYarw1Uf5VWQlaykTvPH7d3QIMTOKJk+6YUGzaX6zcSM eQkA== 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=1K6mTWm02w/WVnpqp+hBl8rcxxwmoOZzcj/DI5dZs3I=; fh=Zah5MX43vfshi7s92Ld+vn5YZO52CQtO4oeiq16jPIc=; b=nNU8+QgDDpEeIYCbYzak9rtod1VtS/v3n6t06JbyG0mQona5cIPEhJZXNAHHzFARCc s/SucVYZ02MUO+eI0BUmYOv1uWKqdEQr1l589fUYOnLHgpOSyHyK1UtNamY5gf4tY/Sl ZAkAgv3itaiyqArF+KdyHLVmSQ4MwF16s0i6tO98sT/Z1NrO2qtjDnksRfKkEC/KTlW0 1U9WwPS9sSKcIIgsfNZvBuJRcUnbT1V6JNE62eftumlVFZTdogVAy/0FlVObFxM5FKHW TdOiTJbbAEOueC3oxVgktdkEwU/49HWRTg7AW8SNgZXpYZdWYp+ucubS+OTHqtiVBo3R 37gQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=FGfsXIA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jx1-20020a170906ca4100b0099315476299si7242634ejb.1040.2023.07.24.19.59.51; Mon, 24 Jul 2023 20:00:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=FGfsXIA8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231438AbjGYChH (ORCPT + 99 others); Mon, 24 Jul 2023 22:37:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229975AbjGYCgH (ORCPT ); Mon, 24 Jul 2023 22:36:07 -0400 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE98C1FEC; Mon, 24 Jul 2023 19:35:24 -0700 (PDT) Received: from pps.filterd (m0279871.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 36P2H9BD028396; Tue, 25 Jul 2023 02:34:51 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=1K6mTWm02w/WVnpqp+hBl8rcxxwmoOZzcj/DI5dZs3I=; b=FGfsXIA8Fmsas94yHucqsawLaIE70Y0Zk6CRN96bqPLqX79I8GdcaJnvyx7uLRTP1CFI mPUqzaz4mxJUV3eTZd9IqkuJM66wiRnDA7NLJ4oE+gVS4oOsbt0YNTvndYqLUf9zeErn 04NyCfA2SOsaAq4QBopc4K/CToSkpRX23foSh9FNyS1Q+qo6Y4EnYVyHNqTrUUqVSoaH P/c7VK/UxVfZ1hQvf3mAHGLHe3X0Sma+m5KnWfGbQUSgu3gEuql6m046LfX9RB6BYs9e fiAQP/qZYELow0rSVCi/9Fa0dUuhipq0WaRcUZx9/7YS+FnHyMWw0CrqjXC1xZjIdH+K Ow== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3s1qassvyf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 25 Jul 2023 02:34:50 +0000 Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 36P2YnxL015301 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 25 Jul 2023 02:34:49 GMT Received: from hu-wcheng-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.30; Mon, 24 Jul 2023 19:34:48 -0700 From: Wesley Cheng To: , , , , , , , , , , , , , , , CC: , , , , , , , , , , , Wesley Cheng Subject: [PATCH v4 28/32] sound: soc: qdsp6: Add SND kcontrol for fetching offload status Date: Mon, 24 Jul 2023 19:34:12 -0700 Message-ID: <20230725023416.11205-29-quic_wcheng@quicinc.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230725023416.11205-1-quic_wcheng@quicinc.com> References: <20230725023416.11205-1-quic_wcheng@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: xeXeNRvkAVTLJPKCYytV-G97zQz2Iim2 X-Proofpoint-GUID: xeXeNRvkAVTLJPKCYytV-G97zQz2Iim2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-24_18,2023-07-24_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 phishscore=0 impostorscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 priorityscore=1501 clxscore=1015 spamscore=0 suspectscore=0 bulkscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2306200000 definitions=main-2307250020 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1772359798592489346 X-GMAIL-MSGID: 1772359798592489346 Add a kcontrol to the platform sound card to fetch the current offload status. This can allow for userspace to ensure/check which USB SND resources are actually busy versus having to attempt opening the USB SND devices, which will result in an error if offloading is active. Signed-off-by: Wesley Cheng --- sound/soc/qcom/qdsp6/q6usb.c | 104 ++++++++++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 3 deletions(-) diff --git a/sound/soc/qcom/qdsp6/q6usb.c b/sound/soc/qcom/qdsp6/q6usb.c index 87e88dafd8ad..e4ccb9d912b0 100644 --- a/sound/soc/qcom/qdsp6/q6usb.c +++ b/sound/soc/qcom/qdsp6/q6usb.c @@ -30,6 +30,8 @@ struct q6usb_status { unsigned int num_pcm; unsigned int chip_index; unsigned int pcm_index; + bool prepared; + bool running; }; struct q6usb_port_data { @@ -52,6 +54,17 @@ static const struct snd_soc_dapm_route q6usb_dapm_routes[] = { {"USB Playback", NULL, "USB_RX_BE"}, }; +static int q6usb_find_running(struct q6usb_port_data *data) +{ + int i; + + for (i = 0; i < SNDRV_CARDS; i++) { + if (data->status[i].running) + return i; + } + return -ENODEV; +} + static int q6usb_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) @@ -81,14 +94,40 @@ static int q6usb_hw_params(struct snd_pcm_substream *substream, goto out; data->status[data->sel_card_idx].pcm_index = data->sel_pcm_idx; + data->status[data->sel_card_idx].prepared = true; out: mutex_unlock(&data->mutex); return ret; } +static int q6usb_prepare(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct q6usb_port_data *data = dev_get_drvdata(dai->dev); + + mutex_lock(&data->mutex); + data->status[data->sel_card_idx].running = true; + mutex_unlock(&data->mutex); + + return 0; +} + +static void q6usb_shutdown(struct snd_pcm_substream *substream, + struct snd_soc_dai *dai) +{ + struct q6usb_port_data *data = dev_get_drvdata(dai->dev); + + mutex_lock(&data->mutex); + data->status[data->sel_card_idx].running = false; + data->status[data->sel_card_idx].prepared = false; + mutex_unlock(&data->mutex); +} + static const struct snd_soc_dai_ops q6usb_ops = { .hw_params = q6usb_hw_params, + .prepare = q6usb_prepare, + .shutdown = q6usb_shutdown, }; static struct snd_soc_dai_driver q6usb_be_dais[] = { @@ -148,10 +187,15 @@ static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol, int pcmidx; int cardidx; + mutex_lock(&data->mutex); + + /* Don't allow changes to the offloading devices if session is busy */ + if (data->sel_card_idx >= 0 && data->status[data->sel_card_idx].prepared) + goto out; + cardidx = ucontrol->value.integer.value[0]; pcmidx = ucontrol->value.integer.value[1]; - mutex_lock(&data->mutex); if ((cardidx >= 0 && test_bit(cardidx, &data->available_card_slot))) { data->sel_card_idx = cardidx; changed = 1; @@ -162,6 +206,8 @@ static int q6usb_put_offload_dev(struct snd_kcontrol *kcontrol, data->idx_valid = true; changed = 1; } + +out: mutex_unlock(&data->mutex); return changed; @@ -187,11 +233,59 @@ static const struct snd_kcontrol_new q6usb_offload_dev_ctrl = { .put = q6usb_put_offload_dev, }; +static int q6usb_mixer_get_offload_status(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); + struct q6usb_port_data *data = dev_get_drvdata(component->dev); + int running; + int card_idx; + int pcm_idx; + + running = q6usb_find_running(data); + if (running < 0) { + card_idx = -1; + pcm_idx = -1; + } else { + card_idx = running; + pcm_idx = data->status[running].pcm_index; + } + + ucontrol->value.integer.value[0] = card_idx; + ucontrol->value.integer.value[1] = pcm_idx; + return 0; +} + +static int q6usb_offload_ctl_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 2; + uinfo->value.integer.min = 0; + uinfo->value.integer.max = SNDRV_CARDS; + + return 0; +} + +static const struct snd_kcontrol_new q6usb_offload_control = { + .iface = SNDRV_CTL_ELEM_IFACE_CARD, + .access = SNDRV_CTL_ELEM_ACCESS_READ, + .name = "Q6USB offload status", + .info = q6usb_offload_ctl_info, + .get = q6usb_mixer_get_offload_status, + .put = NULL, +}; + /* Build a mixer control for a UAC connector control (jack-detect) */ static void q6usb_connector_control_init(struct snd_soc_component *component) { int ret; + ret = snd_ctl_add(component->card->snd_card, + snd_ctl_new1(&q6usb_offload_control, component)); + if (ret < 0) + return; + ret = snd_ctl_add(component->card->snd_card, snd_ctl_new1(&q6usb_offload_dev_ctrl, component)); if (ret < 0) @@ -229,8 +323,12 @@ static int q6usb_alsa_connection_cb(struct snd_soc_usb *usb, int card_idx, mutex_lock(&data->mutex); if (connected) { snd_soc_dapm_enable_pin(dapm, "USB_RX_BE"); - /* We only track the latest USB headset plugged in */ - if (!data->idx_valid || data->sel_card_idx < 0) + /* + * Update the latest USB headset plugged in, if session is + * idle. + */ + if ((!data->idx_valid || data->sel_card_idx < 0) && + !data->status[data->sel_card_idx].prepared) data->sel_card_idx = card_idx; set_bit(card_idx, &data->available_card_slot);