Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp702028ybz; Fri, 1 May 2020 07:04:41 -0700 (PDT) X-Google-Smtp-Source: APiQypIKIkU6mIShgEetHscn7wSwWJw3mdbhKZVvvL3AeWztZlyeqbUCU83L307JKp6mq8hpvRB+ X-Received: by 2002:a50:c60c:: with SMTP id k12mr3618262edg.111.1588341881502; Fri, 01 May 2020 07:04:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588341881; cv=none; d=google.com; s=arc-20160816; b=E/FwK87fkw4tIkvy/CWkXe1Lf5sTR5MeFT8aeSRJkh9L+ZYGz3bJn12nBNNdkbfGgP xKOKPYJVtkPwLjTe/R1Uxx5lfmh/ChbsLHVA9WsaPijeArQFzpBLkMkUnnzwV7BBa228 hY9u+K6gARawIBnkdqP3MMOfl6qBM3XoRsopwXoLw+qN+Fs5l8jap5fjZH/PRAT3uxTp XL+ApdZP0Ts5ZcZx6VHABRpTsHiYSfJ9aiFul118E1KpXDQMzMas1wXlM5dJ3LFRsHJ8 WEq3yTtiR82byk7r4/IWExkRfb293yqGLUc/Fw2zjqSWbh6mNlu9moA2BMPcVCpJbiWU Jl7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=ynm9YleOViPbL52bsFGNCLyXXYKqf4zIfE1xZKfNZVA=; b=E8bKzUXx0qfSSdhbzQtjavfYcrW5cwrDV6W5BxSgYDoKbXvtFo23dTWLCb7AMnRczT Dj95CnqLrHL8t9CCG/iqJBKANZwWk0mU3iJkVWCFY7meTFdlTfJ6XrHT6OwPlAU2rrjf MgHvAIv0ih6UV/THAnYW/N1TPJ56yandEefpGIyI9xvocS/f33MGPpAR8//WJvN8kAwh fHYeREgHyrUzX+de+xpy9BpZsWigaKqmsSsJ0KLNoxPadVVLX+51oxwv1bPNCG86mUHH 2ocLgHGnMIl/EFGTZZDkAc5FI5mk9u9yAEiOES7tqXizPV8vSbjHj8plkEV7DntStS2C qjhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=a7Njh+Je; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-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 t26si2272835edf.183.2020.05.01.07.04.16; Fri, 01 May 2020 07:04:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@kernel.org header.s=default header.b=a7Njh+Je; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729805AbgEAN3h (ORCPT + 99 others); Fri, 1 May 2020 09:29:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:53140 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729797AbgEAN3d (ORCPT ); Fri, 1 May 2020 09:29:33 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C80B124957; Fri, 1 May 2020 13:29:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1588339773; bh=N2g5AetZjjeqhnyaacK54kADqjcgydOL3a79SG4b514=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a7Njh+JeYIv4+y3c32zchREGK4qtc7d7RX2d38PnQmoNV2c+jcRXoriH/57TydVeQ gfhNEAfwN5f51EyaQNy5bi9lV8vAzM5epXPIkjUdqB4a8n8tqEAvUbCLR0KDtzkV6+ RMqrue/W3YSawi7zoxUQV8vxPr12Ndqwz7/vc8xc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Skobkin , Alexander Tsoy , Takashi Iwai Subject: [PATCH 4.9 42/80] ALSA: usb-audio: Filter out unsupported sample rates on Focusrite devices Date: Fri, 1 May 2020 15:21:36 +0200 Message-Id: <20200501131526.784723791@linuxfoundation.org> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200501131513.810761598@linuxfoundation.org> References: <20200501131513.810761598@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexander Tsoy commit 1c826792586f526a5a5cd21d55aad388f5bb0b23 upstream. Many Focusrite devices supports a limited set of sample rates per altsetting. These includes audio interfaces with ADAT ports: - Scarlett 18i6, 18i8 1st gen, 18i20 1st gen; - Scarlett 18i8 2nd gen, 18i20 2nd gen; - Scarlett 18i8 3rd gen, 18i20 3rd gen; - Clarett 2Pre USB, 4Pre USB, 8Pre USB. Maximum rate is exposed in the last 4 bytes of Format Type descriptor which has a non-standard bLength = 10. Tested-by: Alexey Skobkin Signed-off-by: Alexander Tsoy Cc: Link: https://lore.kernel.org/r/20200418175815.12211-1-alexander@tsoy.me Signed-off-by: Takashi Iwai Signed-off-by: Greg Kroah-Hartman --- sound/usb/format.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) --- a/sound/usb/format.c +++ b/sound/usb/format.c @@ -221,6 +221,52 @@ static int parse_audio_format_rates_v1(s } /* + * Many Focusrite devices supports a limited set of sampling rates per + * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type + * descriptor which has a non-standard bLength = 10. + */ +static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip, + struct audioformat *fp, + unsigned int rate) +{ + struct usb_interface *iface; + struct usb_host_interface *alts; + unsigned char *fmt; + unsigned int max_rate; + + iface = usb_ifnum_to_if(chip->dev, fp->iface); + if (!iface) + return true; + + alts = &iface->altsetting[fp->altset_idx]; + fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, + NULL, UAC_FORMAT_TYPE); + if (!fmt) + return true; + + if (fmt[0] == 10) { /* bLength */ + max_rate = combine_quad(&fmt[6]); + + /* Validate max rate */ + if (max_rate != 48000 && + max_rate != 96000 && + max_rate != 192000 && + max_rate != 384000) { + + usb_audio_info(chip, + "%u:%d : unexpected max rate: %u\n", + fp->iface, fp->altsetting, max_rate); + + return true; + } + + return rate <= max_rate; + } + + return true; +} + +/* * Helper function to walk the array of sample rate triplets reported by * the device. The problem is that we need to parse whole array first to * get to know how many sample rates we have to expect. @@ -256,6 +302,11 @@ static int parse_uac2_sample_rate_range( } for (rate = min; rate <= max; rate += res) { + /* Filter out invalid rates on Focusrite devices */ + if (USB_ID_VENDOR(chip->usb_id) == 0x1235 && + !focusrite_valid_sample_rate(chip, fp, rate)) + goto skip_rate; + if (fp->rate_table) fp->rate_table[nr_rates] = rate; if (!fp->rate_min || rate < fp->rate_min) @@ -270,6 +321,7 @@ static int parse_uac2_sample_rate_range( break; } +skip_rate: /* avoid endless loop */ if (res == 0) break;