From patchwork Fri Apr 30 14:26:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 429823 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp488723jao; Fri, 30 Apr 2021 07:26:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxPMwqqQWFCvMLdsQI1APCWnur0nKW+SIHpGHQIbB6DaxapwpXBjQLbl9996FHvtT6u16bG X-Received: by 2002:a17:902:e8c1:b029:e9:3c98:2dd2 with SMTP id v1-20020a170902e8c1b02900e93c982dd2mr5423928plg.17.1619792797464; Fri, 30 Apr 2021 07:26:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619792797; cv=none; d=google.com; s=arc-20160816; b=mde8DZ4v1ewfpci38SlMvCjxmkH5yOaxtGNq/GCf/itT+ADnlXlVi+L1/Hi/wg5Q5q 3RDFpihA6mitlGuxzETMR2g+efBc2QZUjhtuWutrlk7IP7FwJwzfPIKZ/TTqD9ojROUi pGr4hUIsk4A/j9gQRT2HlhTTSEjMAUTmXVl1RToGW45/9eMJQCdo96b7mymJQFiY3Cqq O/i6SkvVUfCTBd70le8yZAgpfP176w1NCzDG27jspXvs9o+4ffei/J8IKx1jURrq3Ene iUnlaFiGCR47D+CysFefJ28LFQ76amLZLJj03X2OAx4SgVOBc4xfgjkIiFpJfk0gQ4fz iSQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8LwXkzZTW/R1ALWVbMU1xulwW28GybjGPZyhwD4sJg0=; b=Xv8Ay9QpWQRaMMN5uB5iFcnFZCagz+QH2XkaOypfE7jmIEvj7uvu29c8yuPNtAwiOg 6VrYra0fHfFcI6cdN8SSDSCVRWqMvghaDlRj3Kl9k3Dvslb3u4RLYhlsxm07Lry9fMvb Q2uFIyCZkBbqcw6plYGutR3Qpn4wGPE0HbgSIRF3mM6Mpk+y8+4rTcoa7ayNAIC1IapV 3zK5BwIl0UIzuAH3c+9BVn7ixHb2acsNjdnszkCT/PLr4JMB6zdRVpnzyEqKHldzrv2X rIM201VaCY6Xi9bKmYu5LhXeYLyuTZ/AArN8zQA/zQiLTLpZhjuzwD1SSutAr/8A+e9C PpDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b="niWn5T/Z"; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u9si3756173pgo.258.2021.04.30.07.26.37; Fri, 30 Apr 2021 07:26:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b="niWn5T/Z"; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232717AbhD3O1Y (ORCPT + 4 others); Fri, 30 Apr 2021 10:27:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232648AbhD3O1V (ORCPT ); Fri, 30 Apr 2021 10:27:21 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FA06C06138B for ; Fri, 30 Apr 2021 07:26:33 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id t4so22254087ejo.0 for ; Fri, 30 Apr 2021 07:26:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=8LwXkzZTW/R1ALWVbMU1xulwW28GybjGPZyhwD4sJg0=; b=niWn5T/ZfYcAVTW+Pe5SNE1uhYMYszI4/skoVLngbPNshJHKOVJBebIr2cpi5FF1gM cb3z/BFdcrgTHfkYC4fL21AYrtWe7VCSFh/X/Gq7+xKo/h6O/5Fq1GFMm1RU945L28TR MxfBZrU7O6V/IWi3Ym8op1M2YmjaGeg0xet8PTf5ZiKiU1YZNEARK7JovlFQJxEjsRfi YodFEwDvdbT7vw6TF2fH1YazzmEDS4s+TNTQc+ko2CrJlXLhW02XIbm4vFvjFmp7JSbf 82GkxncjujjgTNoxI15DynHNrqMhnie6EaS/ZKZO9Rrrv0xNSatIPS12PJpUImxV3Qe/ N5Rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=8LwXkzZTW/R1ALWVbMU1xulwW28GybjGPZyhwD4sJg0=; b=Z9EXkErKM4hnTD8fpE2Xxto2eIKLOEiK55WIOPwob7simbV50CKyAoGIYHypFt4lPC uliRF+oNNwAH39DCS6vI5fC2hEI/QXZ6QTg+9b3rCKrVKkOmSR+X4nndLwktHYKVnK6D 8Kf9prYNQ+qAwL2Wnh37pokasoTgz1SWlXWPPPNaYzs05Ty81pRvuH/DAVtz3G40XhAi 6dRUtHRctgJSb0RraSNbmkJGNt9s0dLjmZHKL8bta3pUlJJl6KYgAx1KgJM2O4nbYkFw NaWuXIamrVvIHCP22LogYgEVvRJ3dgQS3dm5JrYYOc/G5eOUJ6kLF++vGfwRWAkd0FRb nMEQ== X-Gm-Message-State: AOAM532su71jIr9bH41FsNIRLGZqdczSEYrKdBC3T7cli8psx6CnGKQw FwRlUB31zcaW+WkcgcIh3RKqgDG2n7qcGLGI X-Received: by 2002:a17:906:52d7:: with SMTP id w23mr4761772ejn.451.1619792791808; Fri, 30 Apr 2021 07:26:31 -0700 (PDT) Received: from localhost.localdomain (82-65-169-74.subs.proxad.net. [82.65.169.74]) by smtp.googlemail.com with ESMTPSA id t22sm1009290edw.29.2021.04.30.07.26.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Apr 2021 07:26:31 -0700 (PDT) From: Jerome Brunet To: Ruslan Bilovol , balbi@kernel.org Cc: linux-usb@vger.kernel.org, gschmottlach@gmail.com, Jerome Brunet Subject: [RFC PATCH v2 1/3] usb: gadget: f_uac2/u_audio: add feedback endpoint support Date: Fri, 30 Apr 2021 16:26:23 +0200 Message-Id: <20210430142625.357152-2-jbrunet@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210430142625.357152-1-jbrunet@baylibre.com> References: <20210430142625.357152-1-jbrunet@baylibre.com> MIME-Version: 1.0 X-Patchwork-Bot: notify Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Ruslan Bilovol As per USB and UAC2 specs, asynchronous audio sink endpoint requires explicit synchronization mechanism (Isochronous Feedback Endpoint) Implement feedback companion endpoint for ISO OUT endpoint This patch adds all required infrastructure and USB requests handling for feedback endpoint. Syncrhonization itself is still dummy (feedback ep always reports 'nomimal frequency' e.g. no adjustement is needed). This satisfies hosts that require feedback endpoint (like Win10) and poll it periodically Actual synchronization mechanism should be implemented separately Signed-off-by: Ruslan Bilovol Signed-off-by: Jerome Brunet --- drivers/usb/gadget/function/f_uac2.c | 49 ++++++++++- drivers/usb/gadget/function/u_audio.c | 119 +++++++++++++++++++++++++- drivers/usb/gadget/function/u_audio.h | 3 + 3 files changed, 168 insertions(+), 3 deletions(-) -- 2.31.1 diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index 7aa4c8bc5a1a..5d63244ba319 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -240,7 +240,7 @@ static struct usb_interface_descriptor std_as_out_if1_desc = { .bDescriptorType = USB_DT_INTERFACE, .bAlternateSetting = 1, - .bNumEndpoints = 1, + .bNumEndpoints = 2, .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, .bInterfaceProtocol = UAC_VERSION_2, @@ -317,6 +317,37 @@ static struct uac2_iso_endpoint_descriptor as_iso_out_desc = { .wLockDelay = 0, }; +/* STD AS ISO IN Feedback Endpoint */ +static struct usb_endpoint_descriptor fs_epin_fback_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK, + .wMaxPacketSize = cpu_to_le16(3), + .bInterval = 1, +}; + +static struct usb_endpoint_descriptor hs_epin_fback_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK, + .wMaxPacketSize = cpu_to_le16(4), + .bInterval = 4, +}; + +static struct usb_endpoint_descriptor ss_epin_fback_desc = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + + .bEndpointAddress = USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_USAGE_FEEDBACK, + .wMaxPacketSize = cpu_to_le16(4), + .bInterval = 4, +}; + + /* Audio Streaming IN Interface - Alt0 */ static struct usb_interface_descriptor std_as_in_if0_desc = { .bLength = sizeof std_as_in_if0_desc, @@ -431,6 +462,7 @@ static struct usb_descriptor_header *fs_audio_desc[] = { (struct usb_descriptor_header *)&as_out_fmt1_desc, (struct usb_descriptor_header *)&fs_epout_desc, (struct usb_descriptor_header *)&as_iso_out_desc, + (struct usb_descriptor_header *)&fs_epin_fback_desc, (struct usb_descriptor_header *)&std_as_in_if0_desc, (struct usb_descriptor_header *)&std_as_in_if1_desc, @@ -461,6 +493,7 @@ static struct usb_descriptor_header *hs_audio_desc[] = { (struct usb_descriptor_header *)&as_out_fmt1_desc, (struct usb_descriptor_header *)&hs_epout_desc, (struct usb_descriptor_header *)&as_iso_out_desc, + (struct usb_descriptor_header *)&hs_epin_fback_desc, (struct usb_descriptor_header *)&std_as_in_if0_desc, (struct usb_descriptor_header *)&std_as_in_if1_desc, @@ -492,6 +525,7 @@ static struct usb_descriptor_header *ss_audio_desc[] = { (struct usb_descriptor_header *)&ss_epout_desc, (struct usb_descriptor_header *)&ss_epout_desc_comp, (struct usb_descriptor_header *)&as_iso_out_desc, + (struct usb_descriptor_header *)&ss_epin_fback_desc, (struct usb_descriptor_header *)&std_as_in_if0_desc, (struct usb_descriptor_header *)&std_as_in_if1_desc, @@ -568,22 +602,26 @@ static void setup_headers(struct f_uac2_opts *opts, struct usb_ss_ep_comp_descriptor *epin_desc_comp = NULL; struct usb_endpoint_descriptor *epout_desc; struct usb_endpoint_descriptor *epin_desc; + struct usb_endpoint_descriptor *epin_fback_desc; int i; switch (speed) { case USB_SPEED_FULL: epout_desc = &fs_epout_desc; epin_desc = &fs_epin_desc; + epin_fback_desc = &fs_epin_fback_desc; break; case USB_SPEED_HIGH: epout_desc = &hs_epout_desc; epin_desc = &hs_epin_desc; + epin_fback_desc = &hs_epin_fback_desc; break; default: epout_desc = &ss_epout_desc; epin_desc = &ss_epin_desc; epout_desc_comp = &ss_epout_desc_comp; epin_desc_comp = &ss_epin_desc_comp; + epin_fback_desc = &ss_epin_fback_desc; } i = 0; @@ -611,6 +649,7 @@ static void setup_headers(struct f_uac2_opts *opts, headers[i++] = USBDHDR(epout_desc_comp); headers[i++] = USBDHDR(&as_iso_out_desc); + headers[i++] = USBDHDR(epin_fback_desc); } if (EPIN_EN(opts)) { headers[i++] = USBDHDR(&std_as_in_if0_desc); @@ -844,6 +883,12 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); return -ENODEV; } + agdev->in_ep_fback = usb_ep_autoconfig(gadget, + &fs_epin_fback_desc); + if (!agdev->in_ep_fback) { + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return -ENODEV; + } } if (EPIN_EN(uac2_opts)) { @@ -867,8 +912,10 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) le16_to_cpu(ss_epout_desc.wMaxPacketSize)); hs_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; + hs_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress; hs_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; ss_epout_desc.bEndpointAddress = fs_epout_desc.bEndpointAddress; + ss_epin_fback_desc.bEndpointAddress = fs_epin_fback_desc.bEndpointAddress; ss_epin_desc.bEndpointAddress = fs_epin_desc.bEndpointAddress; setup_descriptor(uac2_opts); diff --git a/drivers/usb/gadget/function/u_audio.c b/drivers/usb/gadget/function/u_audio.c index 5fbceee897a3..f637ebec80b0 100644 --- a/drivers/usb/gadget/function/u_audio.c +++ b/drivers/usb/gadget/function/u_audio.c @@ -38,6 +38,10 @@ struct uac_rtd_params { unsigned int max_psize; /* MaxPacketSize of endpoint */ struct usb_request **reqs; + + struct usb_request *req_fback; /* Feedback endpoint request */ + unsigned int ffback; /* Real frequency reported by feedback endpoint */ + bool fb_ep_enabled; /* if the ep is enabled */ }; struct snd_uac_chip { @@ -70,6 +74,32 @@ static const struct snd_pcm_hardware uac_pcm_hardware = { .periods_min = MIN_PERIODS, }; +static void u_audio_set_fback_frequency(enum usb_device_speed speed, + unsigned int freq, void *buf) +{ + u32 ff = 0; + + if (speed == USB_SPEED_FULL) { + /* + * Full-speed feedback endpoints report frequency + * in samples/microframe + * Format is encoded in Q10.10 left-justified in the 24 bits, + * so that it has a Q10.14 format. + */ + ff = DIV_ROUND_UP((freq << 14), 1000); + } else { + /* + * High-speed feedback endpoints report frequency + * in samples/microframe. + * Format is encoded in Q12.13 fitted into four bytes so that + * the binary point is located between the second and the third + * byte fromat (that is Q16.16) + */ + ff = DIV_ROUND_UP((freq << 13), 1000); + } + *(__le32 *)buf = cpu_to_le32(ff); +} + static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) { unsigned int pending; @@ -173,6 +203,34 @@ static void u_audio_iso_complete(struct usb_ep *ep, struct usb_request *req) dev_err(uac->card->dev, "%d Error!\n", __LINE__); } +static void u_audio_iso_fback_complete(struct usb_ep *ep, + struct usb_request *req) +{ + struct uac_rtd_params *prm = req->context; + struct snd_uac_chip *uac = prm->uac; + struct g_audio *audio_dev = uac->audio_dev; + int status = req->status; + unsigned long flags; + + /* i/f shutting down */ + if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN) + return; + + /* + * We can't really do much about bad xfers. + * Afterall, the ISOCH xfers could fail legitimately. + */ + if (status) + pr_debug("%s: iso_complete status(%d) %d/%d\n", + __func__, status, req->actual, req->length); + + u_audio_set_fback_frequency(audio_dev->gadget->speed, + prm->ffback, req->buf); + + if (usb_ep_queue(ep, req, GFP_ATOMIC)) + dev_err(uac->card->dev, "%d Error!\n", __LINE__); +} + static int uac_pcm_trigger(struct snd_pcm_substream *substream, int cmd) { struct snd_uac_chip *uac = snd_pcm_substream_chip(substream); @@ -335,14 +393,33 @@ static inline void free_ep(struct uac_rtd_params *prm, struct usb_ep *ep) dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); } +static inline void free_ep_fback(struct uac_rtd_params *prm, struct usb_ep *ep) +{ + struct snd_uac_chip *uac = prm->uac; + + if (!prm->fb_ep_enabled) + return; + + prm->fb_ep_enabled = false; + + if (prm->req_fback) { + usb_ep_dequeue(ep, prm->req_fback); + kfree(prm->req_fback->buf); + usb_ep_free_request(ep, prm->req_fback); + prm->req_fback = NULL; + } + + if (usb_ep_disable(ep)) + dev_err(uac->card->dev, "%s:%d Error!\n", __func__, __LINE__); +} int u_audio_start_capture(struct g_audio *audio_dev) { struct snd_uac_chip *uac = audio_dev->uac; struct usb_gadget *gadget = audio_dev->gadget; struct device *dev = &gadget->dev; - struct usb_request *req; - struct usb_ep *ep; + struct usb_request *req, *req_fback; + struct usb_ep *ep, *ep_fback; struct uac_rtd_params *prm; struct uac_params *params = &audio_dev->params; int req_len, i; @@ -374,6 +451,42 @@ int u_audio_start_capture(struct g_audio *audio_dev) dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); } + ep_fback = audio_dev->in_ep_fback; + if (!ep_fback) + return 0; + + /* Setup feedback endpoint */ + config_ep_by_speed(gadget, &audio_dev->func, ep_fback); + prm->fb_ep_enabled = true; + usb_ep_enable(ep_fback); + req_len = ep_fback->maxpacket; + + req_fback = usb_ep_alloc_request(ep_fback, GFP_ATOMIC); + if (req_fback == NULL) + return -ENOMEM; + + prm->req_fback = req_fback; + req_fback->zero = 0; + req_fback->context = prm; + req_fback->length = req_len; + req_fback->complete = u_audio_iso_fback_complete; + + req_fback->buf = kzalloc(req_len, GFP_ATOMIC); + if (!req_fback->buf) + return -ENOMEM; + + /* + * Configure the feedback endpoint's reported frequency. + * Always start with original frequency since its deviation can't + * be meauserd at start of playback + */ + prm->ffback = params->c_srate; + u_audio_set_fback_frequency(audio_dev->gadget->speed, + prm->ffback, req_fback->buf); + + if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC)) + dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); + return 0; } EXPORT_SYMBOL_GPL(u_audio_start_capture); @@ -382,6 +495,8 @@ void u_audio_stop_capture(struct g_audio *audio_dev) { struct snd_uac_chip *uac = audio_dev->uac; + if (audio_dev->in_ep_fback) + free_ep_fback(&uac->c_prm, audio_dev->in_ep_fback); free_ep(&uac->c_prm, audio_dev->out_ep); } EXPORT_SYMBOL_GPL(u_audio_stop_capture); diff --git a/drivers/usb/gadget/function/u_audio.h b/drivers/usb/gadget/function/u_audio.h index 5ea6b86f1fda..53e6baf55cbf 100644 --- a/drivers/usb/gadget/function/u_audio.h +++ b/drivers/usb/gadget/function/u_audio.h @@ -30,7 +30,10 @@ struct g_audio { struct usb_gadget *gadget; struct usb_ep *in_ep; + struct usb_ep *out_ep; + /* feedback IN endpoint corresponding to out_ep */ + struct usb_ep *in_ep_fback; /* Max packet size for all in_ep possible speeds */ unsigned int in_ep_maxpsize; From patchwork Fri Apr 30 14:26:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 429822 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp488712jao; Fri, 30 Apr 2021 07:26:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx2T/s69Q3PjqLf78G8SYkByan7y+Kh6y40DbOJqCrvAn6KxPEC8FLFlYTl7Etyr1K7E8HQ X-Received: by 2002:a63:a70b:: with SMTP id d11mr4873253pgf.26.1619792796995; Fri, 30 Apr 2021 07:26:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619792796; cv=none; d=google.com; s=arc-20160816; b=FnrSf0/b9d5N3oW6pGS7ijvxo4+HKYJFanRUI69oAe55Y7GTLiDy3jHR/xOWRpm/MU mi0pV2VOnlpdgkwGtDwoquePsfEYGCStgeLfwpn7TXNlw5S0q2o8kXOW8uS/HbafXmbj GdhekZCVqMbZo+xCFKVUMtABToZxgfRtinOa6jNN+DJ/wdnERvs6pe1dXjghHMnuj3Ad X1DvVbEfrXx5yMzhJjeNRnazyiUErSO1z+K2XFungENEsjX/JENJoGqc58OeMWVuMiRB +BZ9ibLiOUTeNZAea4x/pWIAcr5XHsvHSvEklGSYLH2EGFnkZNf4jeWHY+xiqSvyrGHX uVfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kRuFkGf+6iSk7X1JhBm9EtqPo3OM7aC4PTr5rB1CZo4=; b=Zt4jBm9tK3/VLtfyOPBDaxlNQMftEjLdl8mP9IzU6Zp6PlOBnMa9YA1U4Fjm5ap6In X6H3bUYaKPJiRZQ1fXdokVhDAyMTZk2J4FtTbBOIam21hF5jNQXYaWoQ/HAa2cW6MBn+ 1hUcMDK0H5jsBZb2FAd6jVt2DJXjilAhSxYTPAGjho9kuG1h7MGM6BfFQZx91EJWeM6K vIK2Sey05xp4yF/EJCVB+OOENFDNi2DY/HkoD9P66Ji/7w5gQRi+B5vhoIdci2JCrtjD 3yg4ZPUUtI3snbI8hYFangibDQuzfHEtWARS+l+Q5nMCJcLS2gkozYAOshE6+7WJ/L3C 32XQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=e398LU3F; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u9si3756173pgo.258.2021.04.30.07.26.36; Fri, 30 Apr 2021 07:26:36 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=e398LU3F; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232743AbhD3O1X (ORCPT + 4 others); Fri, 30 Apr 2021 10:27:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232709AbhD3O1W (ORCPT ); Fri, 30 Apr 2021 10:27:22 -0400 Received: from mail-ed1-x52b.google.com (mail-ed1-x52b.google.com [IPv6:2a00:1450:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C92F5C06138C for ; Fri, 30 Apr 2021 07:26:33 -0700 (PDT) Received: by mail-ed1-x52b.google.com with SMTP id q6so23590309edr.3 for ; Fri, 30 Apr 2021 07:26:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kRuFkGf+6iSk7X1JhBm9EtqPo3OM7aC4PTr5rB1CZo4=; b=e398LU3FCHX6XomaDhnWrRTg0QBFyF7o4dVJpmxBQo44Y3LhWZc/q2N/ZagrtN2GIA HwkvDJ5WAlOg1LG5QgwNVQ6i9h4LFVG/VKSZHSK/UoMFukJQAD3AJVU7lGgSKrH+Qj+F XaZGWE72ehZcBIj6p9kkPWHb/U2Z5ubXItbiRru3z6Axa+PyBECp/MaXvVZgaDz4ssI7 RxfpYbTbqin4CKgqc7yR+x9r7k8QGnja2NkLF4zmxvOB6pwJhZehDzjwy5sJz0C/6c4g 67rMbCN5X9EeP8GZIu2fuUvJ+lL29IWo8v0uqAroumEiMDygrGc+eFwgKKsrWifYRn6O GGng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kRuFkGf+6iSk7X1JhBm9EtqPo3OM7aC4PTr5rB1CZo4=; b=Z/xJUuKGrvDetMvG+hzhnpn3GifwugeV+nPu0R9l5t/KetCozDgThAUMPvdnSKFi5k yBtIlzZ3tNGCBPCbGAEIFWO215Cnn+DxnDwzFSVepHhnxh9h4ESN3sic1foKbEhptvzH F+4YPR5bgpq9onvHDW8W8TOy0N5Dq66n+OG40wtXmR24myRHnDZQmITbl3ZfWPwt4U9y VUgqTbIMRtIb6a6xRxoR72wfdXg+K9qFzmA+zBCJdQ3QmW23M6AyEWIicO9CM6jnOuHo klB+kvCrSvlxC83+LCALligp1T+btbMtnsxEZxRttZkkHLoklTO69w1CIURCxOJHhSIu lJdg== X-Gm-Message-State: AOAM530UCovvpy+84j/ON5Yz+Yq95OsKaNG8JiCv+JpJKF1UdLVmotto sV4LNqktl51MveZTxA2oaqP7VQ== X-Received: by 2002:a05:6402:1907:: with SMTP id e7mr6299432edz.313.1619792792471; Fri, 30 Apr 2021 07:26:32 -0700 (PDT) Received: from localhost.localdomain (82-65-169-74.subs.proxad.net. [82.65.169.74]) by smtp.googlemail.com with ESMTPSA id t22sm1009290edw.29.2021.04.30.07.26.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Apr 2021 07:26:32 -0700 (PDT) From: Jerome Brunet To: Ruslan Bilovol , balbi@kernel.org Cc: linux-usb@vger.kernel.org, gschmottlach@gmail.com, Jerome Brunet Subject: [RFC PATCH v2 2/3] usb: gadget: f_uac2: add adaptive sync support for capture Date: Fri, 30 Apr 2021 16:26:24 +0200 Message-Id: <20210430142625.357152-3-jbrunet@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210430142625.357152-1-jbrunet@baylibre.com> References: <20210430142625.357152-1-jbrunet@baylibre.com> MIME-Version: 1.0 X-Patchwork-Bot: notify Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Ruslan Bilovol Current f_uac2 USB OUT (aka 'capture') synchronization implements 'ASYNC' scenario which means USB Gadget has it's own freerunning clock and can update Host about real clock frequency through feedback endpoint so Host can align number of samples sent to the USB gadget to prevent overruns/underruns In case if Gadget can has no it's internal clock and can consume audio samples at any rate (for example, on the Gadget side someone records audio directly to a file, or audio samples are played through an external DAC as soon as they arrive), UAC2 spec suggests 'ADAPTIVE' synchronization type. Change UAC2 driver to make it configurable through additional 'c_sync' configfs file. Default remains 'asynchronous' with possibility to switch it to 'adaptive' Signed-off-by: Ruslan Bilovol Signed-off-by: Jerome Brunet --- .../ABI/testing/configfs-usb-gadget-uac2 | 1 + Documentation/usb/gadget-testing.rst | 1 + drivers/usb/gadget/function/f_uac2.c | 100 ++++++++++++++++-- drivers/usb/gadget/function/u_uac2.h | 2 + 4 files changed, 95 insertions(+), 9 deletions(-) -- 2.31.1 diff --git a/Documentation/ABI/testing/configfs-usb-gadget-uac2 b/Documentation/ABI/testing/configfs-usb-gadget-uac2 index d4356c8b8cd6..e7e59d7fb12f 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-uac2 +++ b/Documentation/ABI/testing/configfs-usb-gadget-uac2 @@ -8,6 +8,7 @@ Description: c_chmask capture channel mask c_srate capture sampling rate c_ssize capture sample size (bytes) + c_sync capture synchronization type (async/adaptive) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) diff --git a/Documentation/usb/gadget-testing.rst b/Documentation/usb/gadget-testing.rst index 2085e7b24eeb..f5a12667bd41 100644 --- a/Documentation/usb/gadget-testing.rst +++ b/Documentation/usb/gadget-testing.rst @@ -728,6 +728,7 @@ The uac2 function provides these attributes in its function directory: c_chmask capture channel mask c_srate capture sampling rate c_ssize capture sample size (bytes) + c_sync capture synchronization type (async/adaptive) p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index 5d63244ba319..321e6c05ba93 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -44,6 +44,7 @@ #define EPIN_EN(_opts) ((_opts)->p_chmask != 0) #define EPOUT_EN(_opts) ((_opts)->c_chmask != 0) +#define EPOUT_FBACK_IN_EN(_opts) ((_opts)->c_sync == USB_ENDPOINT_SYNC_ASYNC) struct f_uac2 { struct g_audio g_audio; @@ -240,7 +241,7 @@ static struct usb_interface_descriptor std_as_out_if1_desc = { .bDescriptorType = USB_DT_INTERFACE, .bAlternateSetting = 1, - .bNumEndpoints = 2, + .bNumEndpoints = 1, .bInterfaceClass = USB_CLASS_AUDIO, .bInterfaceSubClass = USB_SUBCLASS_AUDIOSTREAMING, .bInterfaceProtocol = UAC_VERSION_2, @@ -273,7 +274,7 @@ static struct usb_endpoint_descriptor fs_epout_desc = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, + /* .bmAttributes = DYNAMIC */ /* .wMaxPacketSize = DYNAMIC */ .bInterval = 1, }; @@ -282,7 +283,7 @@ static struct usb_endpoint_descriptor hs_epout_desc = { .bLength = USB_DT_ENDPOINT_SIZE, .bDescriptorType = USB_DT_ENDPOINT, - .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, + /* .bmAttributes = DYNAMIC */ /* .wMaxPacketSize = DYNAMIC */ .bInterval = 4, }; @@ -292,7 +293,7 @@ static struct usb_endpoint_descriptor ss_epout_desc = { .bDescriptorType = USB_DT_ENDPOINT, .bEndpointAddress = USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC, + /* .bmAttributes = DYNAMIC */ /* .wMaxPacketSize = DYNAMIC */ .bInterval = 4, }; @@ -649,7 +650,9 @@ static void setup_headers(struct f_uac2_opts *opts, headers[i++] = USBDHDR(epout_desc_comp); headers[i++] = USBDHDR(&as_iso_out_desc); - headers[i++] = USBDHDR(epin_fback_desc); + + if (EPOUT_FBACK_IN_EN(opts)) + headers[i++] = USBDHDR(epin_fback_desc); } if (EPIN_EN(opts)) { headers[i++] = USBDHDR(&std_as_in_if0_desc); @@ -820,6 +823,23 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) std_as_out_if1_desc.bInterfaceNumber = ret; uac2->as_out_intf = ret; uac2->as_out_alt = 0; + + if (EPOUT_FBACK_IN_EN(uac2_opts)) { + fs_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC; + hs_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC; + ss_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ASYNC; + std_as_out_if1_desc.bNumEndpoints++; + } else { + fs_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE; + hs_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE; + ss_epout_desc.bmAttributes = + USB_ENDPOINT_XFER_ISOC | USB_ENDPOINT_SYNC_ADAPTIVE; + } } if (EPIN_EN(uac2_opts)) { @@ -883,11 +903,14 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); return -ENODEV; } - agdev->in_ep_fback = usb_ep_autoconfig(gadget, + if (EPOUT_FBACK_IN_EN(uac2_opts)) { + agdev->in_ep_fback = usb_ep_autoconfig(gadget, &fs_epin_fback_desc); - if (!agdev->in_ep_fback) { - dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); - return -ENODEV; + if (!agdev->in_ep_fback) { + dev_err(dev, "%s:%d Error!\n", + __func__, __LINE__); + return -ENODEV; + } } } @@ -1242,11 +1265,68 @@ end: \ \ CONFIGFS_ATTR(f_uac2_opts_, name) +#define UAC2_ATTRIBUTE_SYNC(name) \ +static ssize_t f_uac2_opts_##name##_show(struct config_item *item, \ + char *page) \ +{ \ + struct f_uac2_opts *opts = to_f_uac2_opts(item); \ + int result; \ + char *str; \ + \ + mutex_lock(&opts->lock); \ + switch (opts->name) { \ + case USB_ENDPOINT_SYNC_ASYNC: \ + str = "async"; \ + break; \ + case USB_ENDPOINT_SYNC_ADAPTIVE: \ + str = "adaptive"; \ + break; \ + default: \ + str = "unknown"; \ + break; \ + } \ + result = sprintf(page, "%s\n", str); \ + mutex_unlock(&opts->lock); \ + \ + return result; \ +} \ + \ +static ssize_t f_uac2_opts_##name##_store(struct config_item *item, \ + const char *page, size_t len) \ +{ \ + struct f_uac2_opts *opts = to_f_uac2_opts(item); \ + int ret = 0; \ + \ + mutex_lock(&opts->lock); \ + if (opts->refcnt) { \ + ret = -EBUSY; \ + goto end; \ + } \ + \ + if (!strncmp(page, "async", 5)) \ + opts->name = USB_ENDPOINT_SYNC_ASYNC; \ + else if (!strncmp(page, "adaptive", 8)) \ + opts->name = USB_ENDPOINT_SYNC_ADAPTIVE; \ + else { \ + ret = -EINVAL; \ + goto end; \ + } \ + \ + ret = len; \ + \ +end: \ + mutex_unlock(&opts->lock); \ + return ret; \ +} \ + \ +CONFIGFS_ATTR(f_uac2_opts_, name) + UAC2_ATTRIBUTE(p_chmask); UAC2_ATTRIBUTE(p_srate); UAC2_ATTRIBUTE(p_ssize); UAC2_ATTRIBUTE(c_chmask); UAC2_ATTRIBUTE(c_srate); +UAC2_ATTRIBUTE_SYNC(c_sync); UAC2_ATTRIBUTE(c_ssize); UAC2_ATTRIBUTE(req_number); @@ -1257,6 +1337,7 @@ static struct configfs_attribute *f_uac2_attrs[] = { &f_uac2_opts_attr_c_chmask, &f_uac2_opts_attr_c_srate, &f_uac2_opts_attr_c_ssize, + &f_uac2_opts_attr_c_sync, &f_uac2_opts_attr_req_number, NULL, }; @@ -1295,6 +1376,7 @@ static struct usb_function_instance *afunc_alloc_inst(void) opts->c_chmask = UAC2_DEF_CCHMASK; opts->c_srate = UAC2_DEF_CSRATE; opts->c_ssize = UAC2_DEF_CSSIZE; + opts->c_sync = UAC2_DEF_CSYNC; opts->req_number = UAC2_DEF_REQ_NUM; return &opts->func_inst; } diff --git a/drivers/usb/gadget/function/u_uac2.h b/drivers/usb/gadget/function/u_uac2.h index b5035711172d..13589c3c805c 100644 --- a/drivers/usb/gadget/function/u_uac2.h +++ b/drivers/usb/gadget/function/u_uac2.h @@ -21,6 +21,7 @@ #define UAC2_DEF_CCHMASK 0x3 #define UAC2_DEF_CSRATE 64000 #define UAC2_DEF_CSSIZE 2 +#define UAC2_DEF_CSYNC USB_ENDPOINT_SYNC_ASYNC #define UAC2_DEF_REQ_NUM 2 struct f_uac2_opts { @@ -31,6 +32,7 @@ struct f_uac2_opts { int c_chmask; int c_srate; int c_ssize; + int c_sync; int req_number; bool bound; From patchwork Fri Apr 30 14:26:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jerome Brunet X-Patchwork-Id: 429825 Delivered-To: patch@linaro.org Received: by 2002:a02:c901:0:0:0:0:0 with SMTP id t1csp488737jao; Fri, 30 Apr 2021 07:26:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2jM65XVK/tSGoE5ixTi35Q8H4ZNWze4sZd79ORbeak9y1sHTfJwKEMUVZiJ1Fw1wali5b X-Received: by 2002:a17:90a:ea94:: with SMTP id h20mr15430447pjz.78.1619792798326; Fri, 30 Apr 2021 07:26:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619792798; cv=none; d=google.com; s=arc-20160816; b=TSnyzJ/30vaRj1DpPT7WvYUF1Yyfh6GjdggO7oqbT9I+uwBEFxGfqrkRfdYs76l9Ri HKx+ea1D4MkYrthQ6oxD/cZreRucZNuVSjAtu7atKwQXFl//Z5JlIZ3fPG4YzcVtPyC/ 6gpldUQNowf3knhDZIu0VHkziBbWt0UNiPXkHHxstTAh7q5w19CXqffwi86o/4vzQ/bh pz4HCeNHhXyEcZIwbuYWIKmMetQy/z7IHWE9Oiv0tyWSkexVxZPFaRaeojP8iO5Ctg8w vgjoh/HSwFK6pTskFJADWbCKreByCoMBZzwjseyPn5XFt7A6DnGmV1kTLG56GywYm9RK mUyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RyyrRt2ajEZ5GKudfRFcfpOEyyZsq4/5SeJfSMwSBuM=; b=1Eu0kvpRpKBvRbiP0qnsv74ThFu4fwo2dY6t6/Ll0h0jamMPd8dA3T03M5hs2hEJuN 5yZv53jzRmLhZLP6sZw+Y4F15eUYM6PqXOhkZ/TyHCcyg2fXlCGBRDq5VTN+q1O2TgJb 4EqbGJzy9rQPCSevOd9pPjQnXbKziPmJCEXlmb2s2XvCQv4TaAtMhO8j3/L8g5qMht7J /22qzZ+SSXfcQd3wbDsyOcHwYVEJEKg9Y+Px2RBwh1pcfcrntjI0Dj8/d3wTFdYg7QNq lzmrogP4ccmvJHGlSiDx59DTgLq9Q4KlrnbEWVuP9cdTxQut/V+M8VErTuniTLf0XO0S tD2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=WjW3DQ4v; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u9si3756173pgo.258.2021.04.30.07.26.38; Fri, 30 Apr 2021 07:26:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=WjW3DQ4v; spf=pass (google.com: domain of linux-usb-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-usb-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232760AbhD3O1Z (ORCPT + 4 others); Fri, 30 Apr 2021 10:27:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232744AbhD3O1Y (ORCPT ); Fri, 30 Apr 2021 10:27:24 -0400 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83247C06138B for ; Fri, 30 Apr 2021 07:26:34 -0700 (PDT) Received: by mail-ej1-x62a.google.com with SMTP id zg3so23952256ejb.8 for ; Fri, 30 Apr 2021 07:26:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RyyrRt2ajEZ5GKudfRFcfpOEyyZsq4/5SeJfSMwSBuM=; b=WjW3DQ4vZe2wFcirLbUcujLiRxl3Vi4wGAZlfbAqqY7QtQSpvEXnemSbqRpMczY1pj iWWCGY0sD1sjgtKavfAcLJulv/KE8k0rK9NyjP+9HcpCJPbBlkxPdtWARxYQ8pdl6aYS Rg9dJKYoiC+2BcDw9JimoXx6MvqGh2SiwssVibccvpTxLz6MsCcZYmKgC8vZfQROfJXw 3qIdVifK45eDXKWy6s/nDM22fbB5d/DEEZRzshp1coZqAr86tm7pQKL1XYEFhRBAHwSN bw24oNbcrXntc6mIlLrdK478FmMLBgaLnt847qYL2qRVEX5i9NtNlajo2ld9avWaouZN uT1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RyyrRt2ajEZ5GKudfRFcfpOEyyZsq4/5SeJfSMwSBuM=; b=MXhrs9gNaWH940Tdz+FSwyklPwPSHETJqpUG2XBEgyoLzJKkaEp9eMYd9es8IaLEIu DTIMGQgdE6my2DWUcVYzNpcLaF0N6FJ3ogJjnnb/3XPAU5qBFl4A20vT0Ji/bYV+/R1N 8kL1iNqjzMI4OGEjReJ3JdxBC+5q/oZxk8HM7QFRKN2SqMioyVOrmLSjaW6X3g2amwQz CDAaB0zplfNXCeVS1KSRAmI2a2QxaDlz6kTX9zx7oBgsY7mQfMudqSq6ZUZpbheuxDlW Me5sRe/Hmuhd6hLAm546IXbpgKKNakQb04NkJpIux9jBeFnXM7VuFQ76wtOo63ROd0Gp gVMA== X-Gm-Message-State: AOAM533rIZ0QDWJB/NWlwllAgy/wFal8EogZ8x1U6L0lQtRK1+hlL3E5 z5gMwMSPqDT9RsaIPD1bd2xggu/s7ObmOsF+ X-Received: by 2002:a17:906:1e0b:: with SMTP id g11mr4595564ejj.291.1619792793113; Fri, 30 Apr 2021 07:26:33 -0700 (PDT) Received: from localhost.localdomain (82-65-169-74.subs.proxad.net. [82.65.169.74]) by smtp.googlemail.com with ESMTPSA id t22sm1009290edw.29.2021.04.30.07.26.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Apr 2021 07:26:32 -0700 (PDT) From: Jerome Brunet To: Ruslan Bilovol , balbi@kernel.org Cc: linux-usb@vger.kernel.org, gschmottlach@gmail.com, Jerome Brunet Subject: [RFC PATCH v2 3/3] usb: gadget: u_audio: add real feedback implementation Date: Fri, 30 Apr 2021 16:26:25 +0200 Message-Id: <20210430142625.357152-4-jbrunet@baylibre.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210430142625.357152-1-jbrunet@baylibre.com> References: <20210430142625.357152-1-jbrunet@baylibre.com> MIME-Version: 1.0 X-Patchwork-Bot: notify Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org From: Ruslan Bilovol This adds interface between userspace and feedback endpoint to report real feedback frequency to the Host. Current implementation adds new userspace interface ALSA mixer control "Capture Pitch 1000000" (similar to aloop driver's "PCM Rate Shift 100000" mixer control) Value in PPM is chosen to have correction value agnostic of the actual HW rate, which the application is not necessarily dealing with, while still retaining a good enough precision to allow smooth clock correction on the playback side, if necessary. Similar to sound/usb/endpoint.c, a slow down is allowed up to 25%. This has no impact on the required bandwidth. Speedup correction has an impact on the bandwidth reserved for the isochronous endpoint. The default allowed speedup is 500ppm. This seems to be more than enough but, if necessary, this is configurable through a module parameter. The reserved bandwidth is rounded up to the next packet size. Usage of this new control is easy to implement in existing userspace tools like alsaloop from alsa-utils. Signed-off-by: Ruslan Bilovol Signed-off-by: Jerome Brunet --- .../ABI/testing/configfs-usb-gadget-uac2 | 1 + Documentation/usb/gadget-testing.rst | 1 + drivers/usb/gadget/function/f_uac2.c | 9 +- drivers/usb/gadget/function/u_audio.c | 111 ++++++++++++++++-- drivers/usb/gadget/function/u_audio.h | 9 ++ drivers/usb/gadget/function/u_uac2.h | 2 + 6 files changed, 124 insertions(+), 9 deletions(-) -- 2.31.1 diff --git a/Documentation/ABI/testing/configfs-usb-gadget-uac2 b/Documentation/ABI/testing/configfs-usb-gadget-uac2 index e7e59d7fb12f..26fb8e9b4e61 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-uac2 +++ b/Documentation/ABI/testing/configfs-usb-gadget-uac2 @@ -9,6 +9,7 @@ Description: c_srate capture sampling rate c_ssize capture sample size (bytes) c_sync capture synchronization type (async/adaptive) + fb_max maximum extra bandwidth in async mode p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) diff --git a/Documentation/usb/gadget-testing.rst b/Documentation/usb/gadget-testing.rst index f5a12667bd41..9d6276f82774 100644 --- a/Documentation/usb/gadget-testing.rst +++ b/Documentation/usb/gadget-testing.rst @@ -729,6 +729,7 @@ The uac2 function provides these attributes in its function directory: c_srate capture sampling rate c_ssize capture sample size (bytes) c_sync capture synchronization type (async/adaptive) + fb_max maximum extra bandwidth in async mode p_chmask playback channel mask p_srate playback sampling rate p_ssize playback sample size (bytes) diff --git a/drivers/usb/gadget/function/f_uac2.c b/drivers/usb/gadget/function/f_uac2.c index 321e6c05ba93..ae29ff2b2b68 100644 --- a/drivers/usb/gadget/function/f_uac2.c +++ b/drivers/usb/gadget/function/f_uac2.c @@ -584,8 +584,11 @@ static int set_ep_max_packet_size(const struct f_uac2_opts *uac2_opts, ssize = uac2_opts->c_ssize; } + if (!is_playback && (uac2_opts->c_sync == USB_ENDPOINT_SYNC_ASYNC)) + srate = srate * (1000 + uac2_opts->fb_max) / 1000; + max_size_bw = num_channels(chmask) * ssize * - ((srate / (factor / (1 << (ep_desc->bInterval - 1)))) + 1); + DIV_ROUND_UP(srate, factor / (1 << (ep_desc->bInterval - 1))); ep_desc->wMaxPacketSize = cpu_to_le16(min_t(u16, max_size_bw, max_size_ep)); @@ -957,6 +960,7 @@ afunc_bind(struct usb_configuration *cfg, struct usb_function *fn) agdev->params.c_srate = uac2_opts->c_srate; agdev->params.c_ssize = uac2_opts->c_ssize; agdev->params.req_number = uac2_opts->req_number; + agdev->params.fb_max = uac2_opts->fb_max; ret = g_audio_setup(agdev, "UAC2 PCM", "UAC2_Gadget"); if (ret) goto err_free_descs; @@ -1329,6 +1333,7 @@ UAC2_ATTRIBUTE(c_srate); UAC2_ATTRIBUTE_SYNC(c_sync); UAC2_ATTRIBUTE(c_ssize); UAC2_ATTRIBUTE(req_number); +UAC2_ATTRIBUTE(fb_max); static struct configfs_attribute *f_uac2_attrs[] = { &f_uac2_opts_attr_p_chmask, @@ -1339,6 +1344,7 @@ static struct configfs_attribute *f_uac2_attrs[] = { &f_uac2_opts_attr_c_ssize, &f_uac2_opts_attr_c_sync, &f_uac2_opts_attr_req_number, + &f_uac2_opts_attr_fb_max, NULL, }; @@ -1378,6 +1384,7 @@ static struct usb_function_instance *afunc_alloc_inst(void) opts->c_ssize = UAC2_DEF_CSSIZE; opts->c_sync = UAC2_DEF_CSYNC; opts->req_number = UAC2_DEF_REQ_NUM; + opts->fb_max = UAC2_DEF_FB_MAX; return &opts->func_inst; } diff --git a/drivers/usb/gadget/function/u_audio.c b/drivers/usb/gadget/function/u_audio.c index f637ebec80b0..b517beb17006 100644 --- a/drivers/usb/gadget/function/u_audio.c +++ b/drivers/usb/gadget/function/u_audio.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "u_audio.h" @@ -35,12 +36,12 @@ struct uac_rtd_params { void *rbuf; + unsigned int pitch; /* Stream pitch ratio to 1000000 */ unsigned int max_psize; /* MaxPacketSize of endpoint */ struct usb_request **reqs; struct usb_request *req_fback; /* Feedback endpoint request */ - unsigned int ffback; /* Real frequency reported by feedback endpoint */ bool fb_ep_enabled; /* if the ep is enabled */ }; @@ -75,7 +76,9 @@ static const struct snd_pcm_hardware uac_pcm_hardware = { }; static void u_audio_set_fback_frequency(enum usb_device_speed speed, - unsigned int freq, void *buf) + unsigned long long freq, + unsigned int pitch, + void *buf) { u32 ff = 0; @@ -86,7 +89,7 @@ static void u_audio_set_fback_frequency(enum usb_device_speed speed, * Format is encoded in Q10.10 left-justified in the 24 bits, * so that it has a Q10.14 format. */ - ff = DIV_ROUND_UP((freq << 14), 1000); + freq <<= 14; } else { /* * High-speed feedback endpoints report frequency @@ -95,8 +98,11 @@ static void u_audio_set_fback_frequency(enum usb_device_speed speed, * the binary point is located between the second and the third * byte fromat (that is Q16.16) */ - ff = DIV_ROUND_UP((freq << 13), 1000); + freq <<= 13; } + + ff = DIV_ROUND_CLOSEST_ULL((freq * pitch), (1000 * 1000000)); + *(__le32 *)buf = cpu_to_le32(ff); } @@ -209,8 +215,8 @@ static void u_audio_iso_fback_complete(struct usb_ep *ep, struct uac_rtd_params *prm = req->context; struct snd_uac_chip *uac = prm->uac; struct g_audio *audio_dev = uac->audio_dev; + struct uac_params *params = &audio_dev->params; int status = req->status; - unsigned long flags; /* i/f shutting down */ if (!prm->fb_ep_enabled || req->status == -ESHUTDOWN) @@ -225,7 +231,8 @@ static void u_audio_iso_fback_complete(struct usb_ep *ep, __func__, status, req->actual, req->length); u_audio_set_fback_frequency(audio_dev->gadget->speed, - prm->ffback, req->buf); + params->c_srate, prm->pitch, + req->buf); if (usb_ep_queue(ep, req, GFP_ATOMIC)) dev_err(uac->card->dev, "%d Error!\n", __LINE__); @@ -480,9 +487,10 @@ int u_audio_start_capture(struct g_audio *audio_dev) * Always start with original frequency since its deviation can't * be meauserd at start of playback */ - prm->ffback = params->c_srate; + prm->pitch = 1000000; u_audio_set_fback_frequency(audio_dev->gadget->speed, - prm->ffback, req_fback->buf); + params->c_srate, prm->pitch, + req_fback->buf); if (usb_ep_queue(ep_fback, req_fback, GFP_ATOMIC)) dev_err(dev, "%s:%d Error!\n", __func__, __LINE__); @@ -578,12 +586,82 @@ void u_audio_stop_playback(struct g_audio *audio_dev) } EXPORT_SYMBOL_GPL(u_audio_stop_playback); +static int u_audio_pitch_info(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); + struct snd_uac_chip *uac = prm->uac; + struct g_audio *audio_dev = uac->audio_dev; + struct uac_params *params = &audio_dev->params; + unsigned int pitch_min, pitch_max; + + pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; + pitch_max = (1000 + params->fb_max) * 1000; + + uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count = 1; + uinfo->value.integer.min = pitch_min; + uinfo->value.integer.max = pitch_max; + uinfo->value.integer.step = 1; + return 0; +} + +static int u_audio_pitch_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] = prm->pitch; + + return 0; +} + +static int u_audio_pitch_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct uac_rtd_params *prm = snd_kcontrol_chip(kcontrol); + struct snd_uac_chip *uac = prm->uac; + struct g_audio *audio_dev = uac->audio_dev; + struct uac_params *params = &audio_dev->params; + unsigned int val; + unsigned int pitch_min, pitch_max; + int change = 0; + + pitch_min = (1000 - FBACK_SLOW_MAX) * 1000; + pitch_max = (1000 + params->fb_max) * 1000; + + val = ucontrol->value.integer.value[0]; + + if (val < pitch_min) + val = pitch_min; + if (val > pitch_max) + val = pitch_max; + + if (prm->pitch != val) { + prm->pitch = val; + change = 1; + } + + return change; +} + +static const struct snd_kcontrol_new u_audio_controls[] = { +{ + .iface = SNDRV_CTL_ELEM_IFACE_PCM, + .name = "Capture Pitch 1000000", + .info = u_audio_pitch_info, + .get = u_audio_pitch_get, + .put = u_audio_pitch_put, +}, +}; + int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, const char *card_name) { struct snd_uac_chip *uac; struct snd_card *card; struct snd_pcm *pcm; + struct snd_kcontrol *kctl; struct uac_params *params; int p_chmask, c_chmask; int err; @@ -671,6 +749,23 @@ int g_audio_setup(struct g_audio *g_audio, const char *pcm_name, snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &uac_pcm_ops); snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &uac_pcm_ops); + if (c_chmask && g_audio->in_ep_fback) { + strscpy(card->mixername, card_name, sizeof(card->driver)); + + kctl = snd_ctl_new1(&u_audio_controls[0], &uac->c_prm); + if (!kctl) { + err = -ENOMEM; + goto snd_fail; + } + + kctl->id.device = pcm->device; + kctl->id.subdevice = 0; + + err = snd_ctl_add(card, kctl); + if (err < 0) + goto snd_fail; + } + strscpy(card->driver, card_name, sizeof(card->driver)); strscpy(card->shortname, card_name, sizeof(card->shortname)); sprintf(card->longname, "%s %i", card_name, card->dev->id); diff --git a/drivers/usb/gadget/function/u_audio.h b/drivers/usb/gadget/function/u_audio.h index 53e6baf55cbf..a218cdf771fe 100644 --- a/drivers/usb/gadget/function/u_audio.h +++ b/drivers/usb/gadget/function/u_audio.h @@ -11,6 +11,14 @@ #include +/* + * Same maximum frequency deviation on the slower side as in + * sound/usb/endpoint.c. Value is expressed in per-mil deviation. + * The maximum deviation on the faster side will be provided as + * parameter, as it impacts the endpoint required bandwidth. + */ +#define FBACK_SLOW_MAX 250 + struct uac_params { /* playback */ int p_chmask; /* channel mask */ @@ -23,6 +31,7 @@ struct uac_params { int c_ssize; /* sample size */ int req_number; /* number of preallocated requests */ + int fb_max; /* upper frequency drift feedback limit per-mil */ }; struct g_audio { diff --git a/drivers/usb/gadget/function/u_uac2.h b/drivers/usb/gadget/function/u_uac2.h index 13589c3c805c..179d3ef6a195 100644 --- a/drivers/usb/gadget/function/u_uac2.h +++ b/drivers/usb/gadget/function/u_uac2.h @@ -23,6 +23,7 @@ #define UAC2_DEF_CSSIZE 2 #define UAC2_DEF_CSYNC USB_ENDPOINT_SYNC_ASYNC #define UAC2_DEF_REQ_NUM 2 +#define UAC2_DEF_FB_MAX 5 struct f_uac2_opts { struct usb_function_instance func_inst; @@ -34,6 +35,7 @@ struct f_uac2_opts { int c_ssize; int c_sync; int req_number; + int fb_max; bool bound; struct mutex lock;