From patchwork Thu Jan 25 22:35:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13531796 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 587B4111AD; Thu, 25 Jan 2024 22:35:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222138; cv=none; b=MNkIehPa/Xpkr0Jev41SdSL35YVFsA+y2iXkQaJDV0R8ooeD01vZe/TNHPHqLF2mhlucoYUDVL2ewuTYBLK9/gvnXCLcrAYILwiI8GeVoPFj1dh9Fb5rWdj0czaPVtEATJmsgpSonx9k5SEYCWBc9RbtINCjdVlCLRLXc0R2PTA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222138; c=relaxed/simple; bh=Uqv69FjQ+FATXs+N7RcNWXRmI484OnB4UG1WlGfNxug=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=YcHZZRnkG9EMTi7Q9EUSAbLk6R7fBgUmtnupdIXDkSp2XNcgOVeGBahfeVsuZ+IuZ0DV2tTaYF/NGv+F3e/s6wDJWUqk6u4w+TLq7UMln1qleKIJYpz1q57iQ1dLbU6mg2sY2VAGEnjYcNFTX6hMlHoFWOIGWvl8RsQ2GrkVVNQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=kb0FYpEM; arc=none smtp.client-ip=209.85.128.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kb0FYpEM" Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-40e60d74b81so10175635e9.1; Thu, 25 Jan 2024 14:35:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706222131; x=1706826931; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ZRFVAtNzf6+PVhrJOOZpzvV4WQX0zR7X/ikLU7ZJTXs=; b=kb0FYpEM/L/l8XuT1At/qTKvyKfPOC7/7YuD3sIRbnYDjd00v84EcVn5kUTjwlXel0 JLbipUn9Z+ymRrvmsZOzH5yIU9rULzHJgS8HRAHo5CSWMUK8kGYRbMiV9byY66pyVbDt OEIAdL8X01MD0bZiZHS1MDnsJkmi17s7I7XZUDXcnOwl3RIwvuEMu028V51Im4Kdlp5f 0IVAzg499N7DHIdnlOazgS6ONMPeWoS/7spXP/O/jtbF/Xn5gHa/oS4cphP5cRn0ZUEO 61NxBsvaKg4FEfZ8Xt5iQkKYFwzgEcGHxF2+crZbIv2j5MmmH05ptMRZxLnrmQeZONp/ 1Uxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706222131; x=1706826931; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZRFVAtNzf6+PVhrJOOZpzvV4WQX0zR7X/ikLU7ZJTXs=; b=FscNs0Stj5Jk3a5Vq8Kf8WMU+AWD7bMnPkBHOFNgQ/lRiz88SnyqhVWow3esIk2FRf vRXGiGbExhB4a2ak4okF89FMllt+Oy2oOHpGEmOegyWqLX3kTeZL5ZyO6ZWiYo918bg5 yN7i5vQceyqLJd+EpdD25tQSsSL12GAdzm5IBDiRgkndp+DUce2SaVwkISxfLml6zoHF uVxyRFsxgRhsNGd4CH+Xk5hUHTTvDArBeZInUBh2CoL8Ri2dRuk2ngZIusuFAm0RhNQ0 C0Dm1/lQde6jVbXpfGi9YWwFtlbk4gIWfhOoI5bREWcJpV5CTPinyRNVi/sbEfIMIEiP lsHQ== X-Gm-Message-State: AOJu0Yw5ILfPtFZ5tJZrS4WZiHEUgUfnKEamgLhbK5z70vPeQHZDGLib qvHA7Li6NRQmjP4/ya7P7WlW2AKAEtLK2fG+OfmFRdbBZ84I2+Xf X-Google-Smtp-Source: AGHT+IEU4r2sqDqmey0v+H7xvB+9ETTxcY4Ay3X1yHulwQjVliCKwdws7/3b5+P4uYgXtoL5iK92cw== X-Received: by 2002:a05:600c:1f8d:b0:40e:53ef:d9f8 with SMTP id je13-20020a05600c1f8d00b0040e53efd9f8mr566290wmb.1.1706222131256; Thu, 25 Jan 2024 14:35:31 -0800 (PST) Received: from ivan-HLYL-WXX9.. ([2a01:4b00:d20e:7300:9829:5527:ea0f:1fac]) by smtp.gmail.com with ESMTPSA id b19-20020a05600c4e1300b0040d8d11bf63sm40012wmq.41.2024.01.25.14.35.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 14:35:30 -0800 (PST) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com Cc: Ivan Orlov , linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Date: Thu, 25 Jan 2024 22:35:19 +0000 Message-Id: <20240125223522.1122765-1-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 Precedence: bulk X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add 4 missing formats to 'snd_pcm_format_names' array in order to be able to get their names with 'snd_pcm_format_name' function. Signed-off-by: Ivan Orlov --- sound/core/pcm.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sound/core/pcm.c b/sound/core/pcm.c index a09f0154e6a7..d0788126cbab 100644 --- a/sound/core/pcm.c +++ b/sound/core/pcm.c @@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = { FORMAT(DSD_U32_LE), FORMAT(DSD_U16_BE), FORMAT(DSD_U32_BE), + FORMAT(S20_LE), + FORMAT(S20_BE), + FORMAT(U20_LE), + FORMAT(U20_BE), }; /** From patchwork Thu Jan 25 22:35:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13531797 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A871E125AE; Thu, 25 Jan 2024 22:35:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222139; cv=none; b=sfV1TOR0PNjy3ESEKkmVxMuUptW8fU72BHz3O5HKU+JeVUAb8YQNxupEZrvVBKBfUCqjSw9PEmVXBj49OPF3D+UZGgpz/UGg96wb5ZQLzuY0GX3UaYoKT6rqAhX8YBzHaNzjNLPcsehVHo6mNBDzPtR/Qepau0qqIu68EwLCf4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222139; c=relaxed/simple; bh=EpcQXL2y4W10EtBJFDqdMhE3g0qPf4/XBmyrTjfctOQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pyWDBc0fetpKyFshNOspzzdf0xEDr39xKnFmBJUggYWxaziKz8Og7IQsxs19yi8kOeyc6CLFqZiDM6a26y6kKDQD3sVLyg/hapetQyMeGBh+Bawx8wFvw8fy66Fu/8RawHNsxPRYn5xFx99f6TA6ztYaqXMxLNjqA0vMqDB1O3A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UpLU7XJo; arc=none smtp.client-ip=209.85.128.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UpLU7XJo" Received: by mail-wm1-f44.google.com with SMTP id 5b1f17b1804b1-40e5980e1b8so172755e9.0; Thu, 25 Jan 2024 14:35:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706222133; x=1706826933; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eE8uTyZT2lbnF5CcaUIyumC8KPuh+xQv+eXtsqwE/0E=; b=UpLU7XJoNh4ZjIDl1RYmJog5IaX1/tF0Rh4EZsXVkWniK3g8WjOr5cDmM4xuMSRqZh 3ReyKJZPjCp2ZBDf6JzSD9c94b4yTgLFZgG1hKQkfFt07pA9YneMhlPW9ddW3DDK2ku4 2TXUXsnn2OE4thoLH4IYv+rNi3X0qvw/O2mLRIcZP0awEsyV/rg4UEtUpGIeTYUvt50q QrdseDhRnWQU5aKF/x12DSeZPajZN0a2DKOWiUnfVH6e5vaXhA3J1wKvzrQBojA2iRk/ PTm4blY2jrBCFz6c19vdDOWUMr9h0NerfFzoU/dOH/iqUIZT37Z1xmqW8qnEJa7X4jW/ yF6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706222133; x=1706826933; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eE8uTyZT2lbnF5CcaUIyumC8KPuh+xQv+eXtsqwE/0E=; b=B20RhnQX97dfVi2CwExXyNhjiO+lqNJFPg/auO6KpoSf3VWsD0Aj6wyI/iMlAS+BTn hJo4fglS1Ctukd730SYvYSil/YkOp0AbvXbBky2upug8//WTUlm2rcxnW9IOvFv8SJHn dgmOGG3kMw11t+zphaDunBhKGwsytOVQIA/Brd0Z8tDDr0OimIPyxeqfr0zI+COWnyFs JstJr32Vzg/L0rhpoDmy7eHZxXg9WXAXyPpNoPehmdcAOSCk9DZp42T9NeGdG5zEZjP4 0Hlx5cs6IdbChJDlYmoZmXxOFvi8RcYoVBIe3cyIZTuSo2C5SwK9CitJZ6wQFtTgwmEM QnXw== X-Gm-Message-State: AOJu0Yy3Ig0u3AiD5yqZ9WYQlIoX4Jt+u/0GzHWiYgbGRpgqrW4mPXSZ IUW/eLXqev/9rmXZfUfpQoPWotHPaoLBAUgYS5QxbPMRuNj5u2jH/A0HyrEg X-Google-Smtp-Source: AGHT+IG9F4+iX8pfFf9o6PsX3DOSxDi8GXHI0tSXi5IhF+Z+MBeJlUCvzgdVC68xDCvtzWuWWDN0qQ== X-Received: by 2002:a05:600c:5102:b0:40e:af30:4bd7 with SMTP id o2-20020a05600c510200b0040eaf304bd7mr547061wms.3.1706222132713; Thu, 25 Jan 2024 14:35:32 -0800 (PST) Received: from ivan-HLYL-WXX9.. ([2a01:4b00:d20e:7300:9829:5527:ea0f:1fac]) by smtp.gmail.com with ESMTPSA id b19-20020a05600c4e1300b0040d8d11bf63sm40012wmq.41.2024.01.25.14.35.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 14:35:31 -0800 (PST) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com Cc: Ivan Orlov , linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function Date: Thu, 25 Jan 2024 22:35:20 +0000 Message-Id: <20240125223522.1122765-2-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240125223522.1122765-1-ivan.orlov0322@gmail.com> References: <20240125223522.1122765-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Fix snd_pcm_format_name so it won't return NULL-pointer in case if it can't find the format in the 'snd_pcm_format_names' list. Return "Unknown" instead, as it is done if the number passed to the function is larger than a list size. Signed-off-by: Ivan Orlov --- sound/core/pcm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sound/core/pcm.c b/sound/core/pcm.c index d0788126cbab..d9b338088d10 100644 --- a/sound/core/pcm.c +++ b/sound/core/pcm.c @@ -225,9 +225,11 @@ static const char * const snd_pcm_format_names[] = { */ const char *snd_pcm_format_name(snd_pcm_format_t format) { - if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names)) + unsigned int format_num = (__force unsigned int)format; + + if (format_num >= ARRAY_SIZE(snd_pcm_format_names) || !snd_pcm_format_names[format_num]) return "Unknown"; - return snd_pcm_format_names[(__force unsigned int)format]; + return snd_pcm_format_names[format_num]; } EXPORT_SYMBOL_GPL(snd_pcm_format_name); From patchwork Thu Jan 25 22:35:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 13531798 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B509211CBE; Thu, 25 Jan 2024 22:35:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222140; cv=none; b=DhOmfK2zb0I2kbKePJ+6trXgkKmGyZberFvscra3uCONyLHLOPTAYdGjlrj2C/CXx6rPjEpheDruXsqHUjqvCxLTNW6+Sx+qbF9Fck/IUS4jiqzr4KeXLQLFlw+ozTiHv2+cu8N1rKk4wgGJzotVYW7fN1J79cn4Y+JSTcLdpm4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706222140; c=relaxed/simple; bh=iaENUkdckDjvCHndexFHmGTDmatDhxYTmV6H30IpC0k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DKBBc13F1R6GAO9lo4MHZVU5ui7lsJUc+LDopS85Cbh0UgjMAhmDi8EZ8Y5t3EdH+FIdNlpQ+67zKbxCtczcDZ8Ltd2iI/Leu8vuYKqqe5brtsecOIQ4ugzlwrx4NvmfX7Q64yvW7RoQu5mWNx0Wd8R5bBAolo+o1wB19V2d4DQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OdqVTOAO; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OdqVTOAO" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40e60d74b81so10175695e9.1; Thu, 25 Jan 2024 14:35:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1706222134; x=1706826934; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SVWS0jwQOTHPni8BkOdPMXu3CZySiGKR8/ARwF2iZME=; b=OdqVTOAOdS3rjs6vA6hEzxxtMQEn6aKBMy7X56QEqYwW7vp+S78YbGhZgMvX/AofVM gU+RBdCZkLiffzr54UYfyC1SrNxDDc9O+i3NZ+A4ttWt+u/11yNu8bl7b/Zhk+TPDdWH KtmLw9J2TQZ/n1933MyeIiWSinRaOcYOgKIUZuyf1bqt1JHZkVLD5OS8soevg71GclqR Eriyz+78QejA/AOKE8cjXlngnAsnCj9uUw4rcgh5VzWzL9T4lMUwxoV8A4hd2jwbyP20 AvRrJ2zpdYbooCLV/3+UzViJ2/ofSeo5gVdbaGL7/EaOQhWpOkksp9gX1yIEMKZwxoSo al5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706222134; x=1706826934; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SVWS0jwQOTHPni8BkOdPMXu3CZySiGKR8/ARwF2iZME=; b=KbT7mD14F/qvn1px80DLJkM26DHQmnoIa5RTCFCbMIRqxMKhQPae7hXFJ6v27HE76t qDePjG3lmTfUSEgpyhbkBljMt+GFjRJjLQfoHBfGDU/etnIu+0/9RnMpTfuFNN8MXBRy adZng/0dr6h0sADXGoA0LjUOWq30Kvoy8FlTfby1hCW/V74JGQ0CYhNG/6AWBBuzgQJM g6yDPKvq77QRt/JESd5UQ/EszXMa6+qyeigeaLLfQMTv3ass4Gmz0q47LOrvQ9fKnYEc UjzEhTEPyMO9/MX6+aX/9rhAT/8f5wxq/DjHTFmbvBM9nrof7QiMSvzC2L606zKj2v47 28Qw== X-Gm-Message-State: AOJu0YxorwmiZjSrkPtY1IhisUSJdG6zaDefbBInOYXf2+pszYG+DGuI xSNr2bfI3H9m4O8uiPzYkAla36p4OJNAiHiiNqAFoKT3vZjRJDjV0GrVIH60 X-Google-Smtp-Source: AGHT+IGhYdADvTN9PdF55Ze0bpbKRz4o0qWNI98fdmcK5xLpLb3w+AXQrpYhnm8FaAwUw/s0t4r1ew== X-Received: by 2002:a05:600c:1ca0:b0:40e:c217:7cb with SMTP id k32-20020a05600c1ca000b0040ec21707cbmr545064wms.4.1706222133616; Thu, 25 Jan 2024 14:35:33 -0800 (PST) Received: from ivan-HLYL-WXX9.. ([2a01:4b00:d20e:7300:9829:5527:ea0f:1fac]) by smtp.gmail.com with ESMTPSA id b19-20020a05600c4e1300b0040d8d11bf63sm40012wmq.41.2024.01.25.14.35.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 25 Jan 2024 14:35:33 -0800 (PST) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com Cc: Ivan Orlov , linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] ALSA: core: Add sound core KUnit test Date: Thu, 25 Jan 2024 22:35:21 +0000 Message-Id: <20240125223522.1122765-3-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240125223522.1122765-1-ivan.orlov0322@gmail.com> References: <20240125223522.1122765-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-sound@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 At the moment, we have a decent amount of integration tests (selftests) covering different aspects of the sound subsystem. However, a lot of of sound-related in-kernel functions remains uncovered. This patch introduces the KUnit test for the core part of the sound subsystem. It includes 10 test cases: - Coverage of the format-related inline functions from 'pcm.h' header file: snd_pcm_format_physical_width, snd_pcm_format_width, snd_pcm_format_signed, test_format_endianness - Coverage of the available bytes counting functions from 'pcm.h' header: snd_pcm_capture_avail, snd_pcm_playback_avail - Coverage of functions from pcm_misc: snd_pcm_format_set_silence, snd_pcm_format_name - Coverage of card-related functions from init.c: snd_card_set_id, snd_component_add This patch depends on the previous patches in this patch series as they contain fix for the bug, which was found during the test development. Without them, the test doesn't pass. Signed-off-by: Ivan Orlov --- MAINTAINERS | 6 + sound/core/Kconfig | 16 ++ sound/core/Makefile | 2 + sound/core/sound_kunit.c | 310 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 334 insertions(+) create mode 100644 sound/core/sound_kunit.c diff --git a/MAINTAINERS b/MAINTAINERS index 39219b144c23..40b79daad32b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20485,6 +20485,12 @@ F: include/uapi/sound/compress_* F: sound/core/compress_offload.c F: sound/soc/soc-compress.c +SOUND - CORE KUNIT TEST +M: Ivan Orlov +L: linux-sound@vger.kernel.org +S: Supported +F: sound/core/sound_kunit.c + SOUND - DMAENGINE HELPERS M: Lars-Peter Clausen S: Supported diff --git a/sound/core/Kconfig b/sound/core/Kconfig index e41818e59a15..664c6ee2b5a1 100644 --- a/sound/core/Kconfig +++ b/sound/core/Kconfig @@ -39,6 +39,22 @@ config SND_UMP_LEGACY_RAWMIDI legacy MIDI 1.0 byte streams is created for each UMP Endpoint. The device contains 16 substreams corresponding to UMP groups. +config SND_CORE_TEST + tristate "Sound core KUnit test" + depends on KUNIT + default KUNIT_ALL_TESTS + help + This options enables the sound core functions KUnit test. + + KUnit tests run during boot and output the results to the debug + log in TAP format (https://testanything.org/). Only useful for + kernel devs running KUnit test harness and are not for inclusion + into a production build. + + For more information on KUnit and unit tests in general, refer + to the KUnit documentation in Documentation/dev-tools/kunit/. + + config SND_COMPRESS_OFFLOAD tristate diff --git a/sound/core/Makefile b/sound/core/Makefile index a6b444ee2832..1d34e6950317 100644 --- a/sound/core/Makefile +++ b/sound/core/Makefile @@ -49,6 +49,8 @@ obj-$(CONFIG_SND_SEQ_DEVICE) += snd-seq-device.o obj-$(CONFIG_SND_RAWMIDI) += snd-rawmidi.o obj-$(CONFIG_SND_UMP) += snd-ump.o +obj-$(CONFIG_SND_CORE_TEST) += sound_kunit.o + obj-$(CONFIG_SND_OSSEMUL) += oss/ obj-$(CONFIG_SND_SEQUENCER) += seq/ diff --git a/sound/core/sound_kunit.c b/sound/core/sound_kunit.c new file mode 100644 index 000000000000..5d5a7bf88de4 --- /dev/null +++ b/sound/core/sound_kunit.c @@ -0,0 +1,310 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Sound core KUnit test + * Author: Ivan Orlov + */ + +#include +#include +#include + +#define SILENCE_BUFFER_SIZE 2048 +#define SILENCE(...) { __VA_ARGS__ } +#define DEFINE_FORMAT(fmt, pbits, wd, endianness, signd, silence_arr) { \ + .format = SNDRV_PCM_FORMAT_##fmt, .physical_bits = pbits, \ + .width = wd, .le = endianness, .sd = signd, .silence = silence_arr, \ + .name = #fmt, \ +} + +#define WRONG_FORMAT (SNDRV_PCM_FORMAT_LAST + 1) + +#define VALID_NAME "ValidName" +#define NAME_W_SPEC_CHARS "In%v@1id name" +#define NAME_W_SPACE "Test name" +#define NAME_W_SPACE_REMOVED "Testname" + +#define TEST_FIRST_COMPONENT "Component1" +#define TEST_SECOND_COMPONENT "Component2" + +struct snd_format_test_data { + snd_pcm_format_t format; + int physical_bits; + int width; + int le; + int sd; + unsigned char silence[8]; + unsigned char *name; +}; + +struct avail_test_data { + snd_pcm_uframes_t buffer_size; + snd_pcm_uframes_t hw_ptr; + snd_pcm_uframes_t appl_ptr; + snd_pcm_uframes_t expected_avail; +}; + +static struct snd_format_test_data valid_fmt[] = { + DEFINE_FORMAT(S8, 8, 8, -1, 1, SILENCE()), + DEFINE_FORMAT(U8, 8, 8, -1, 0, SILENCE(0x80)), + DEFINE_FORMAT(S16_LE, 16, 16, 1, 1, SILENCE()), + DEFINE_FORMAT(S16_BE, 16, 16, 0, 1, SILENCE()), + DEFINE_FORMAT(U16_LE, 16, 16, 1, 0, SILENCE(0x00, 0x80)), + DEFINE_FORMAT(U16_BE, 16, 16, 0, 0, SILENCE(0x80, 0x00)), + DEFINE_FORMAT(S24_LE, 32, 24, 1, 1, SILENCE()), + DEFINE_FORMAT(S24_BE, 32, 24, 0, 1, SILENCE()), + DEFINE_FORMAT(U24_LE, 32, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)), + DEFINE_FORMAT(U24_BE, 32, 24, 0, 0, SILENCE(0x00, 0x80, 0x00, 0x00)), + DEFINE_FORMAT(S32_LE, 32, 32, 1, 1, SILENCE()), + DEFINE_FORMAT(S32_BE, 32, 32, 0, 1, SILENCE()), + DEFINE_FORMAT(U32_LE, 32, 32, 1, 0, SILENCE(0x00, 0x00, 0x00, 0x80)), + DEFINE_FORMAT(U32_BE, 32, 32, 0, 0, SILENCE(0x80, 0x00, 0x00, 0x00)), + DEFINE_FORMAT(FLOAT_LE, 32, 32, 1, -1, SILENCE()), + DEFINE_FORMAT(FLOAT_BE, 32, 32, 0, -1, SILENCE()), + DEFINE_FORMAT(FLOAT64_LE, 64, 64, 1, -1, SILENCE()), + DEFINE_FORMAT(FLOAT64_BE, 64, 64, 0, -1, SILENCE()), + DEFINE_FORMAT(IEC958_SUBFRAME_LE, 32, 32, 1, -1, SILENCE()), + DEFINE_FORMAT(IEC958_SUBFRAME_BE, 32, 32, 0, -1, SILENCE()), + DEFINE_FORMAT(MU_LAW, 8, 8, -1, -1, SILENCE(0x7f)), + DEFINE_FORMAT(A_LAW, 8, 8, -1, -1, SILENCE(0x55)), + DEFINE_FORMAT(IMA_ADPCM, 4, 4, -1, -1, SILENCE()), + DEFINE_FORMAT(G723_24, 3, 3, -1, -1, SILENCE()), + DEFINE_FORMAT(G723_40, 5, 5, -1, -1, SILENCE()), + DEFINE_FORMAT(DSD_U8, 8, 8, 1, 0, SILENCE(0x69)), + DEFINE_FORMAT(DSD_U16_LE, 16, 16, 1, 0, SILENCE(0x69, 0x69)), + DEFINE_FORMAT(DSD_U32_LE, 32, 32, 1, 0, SILENCE(0x69, 0x69, 0x69, 0x69)), + DEFINE_FORMAT(DSD_U16_BE, 16, 16, 0, 0, SILENCE(0x69, 0x69)), + DEFINE_FORMAT(DSD_U32_BE, 32, 32, 0, 0, SILENCE(0x69, 0x69, 0x69, 0x69)), + DEFINE_FORMAT(S20_LE, 32, 20, 1, 1, SILENCE()), + DEFINE_FORMAT(S20_BE, 32, 20, 0, 1, SILENCE()), + DEFINE_FORMAT(U20_LE, 32, 20, 1, 0, SILENCE(0x00, 0x00, 0x08, 0x00)), + DEFINE_FORMAT(U20_BE, 32, 20, 0, 0, SILENCE(0x00, 0x08, 0x00, 0x00)), + DEFINE_FORMAT(S24_3LE, 24, 24, 1, 1, SILENCE()), + DEFINE_FORMAT(S24_3BE, 24, 24, 0, 1, SILENCE()), + DEFINE_FORMAT(U24_3LE, 24, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)), + DEFINE_FORMAT(U24_3BE, 24, 24, 0, 0, SILENCE(0x80, 0x00, 0x00)), + DEFINE_FORMAT(S20_3LE, 24, 20, 1, 1, SILENCE()), + DEFINE_FORMAT(S20_3BE, 24, 20, 0, 1, SILENCE()), + DEFINE_FORMAT(U20_3LE, 24, 20, 1, 0, SILENCE(0x00, 0x00, 0x08)), + DEFINE_FORMAT(U20_3BE, 24, 20, 0, 0, SILENCE(0x08, 0x00, 0x00)), + DEFINE_FORMAT(S18_3LE, 24, 18, 1, 1, SILENCE()), + DEFINE_FORMAT(S18_3BE, 24, 18, 0, 1, SILENCE()), + DEFINE_FORMAT(U18_3LE, 24, 18, 1, 0, SILENCE(0x00, 0x00, 0x02)), + DEFINE_FORMAT(U18_3BE, 24, 18, 0, 0, SILENCE(0x02, 0x00, 0x00)), + DEFINE_FORMAT(G723_24_1B, 8, 3, -1, -1, SILENCE()), + DEFINE_FORMAT(G723_40_1B, 8, 5, -1, -1, SILENCE()), +}; + +static void test_phys_format_size(struct kunit *test) +{ + u32 i; + + for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { + KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(valid_fmt[i].format), + valid_fmt[i].physical_bits); + } + + KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(-1), -EINVAL); +} + +static void test_format_width(struct kunit *test) +{ + u32 i; + + for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { + KUNIT_EXPECT_EQ(test, snd_pcm_format_width(valid_fmt[i].format), + valid_fmt[i].width); + } + + KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL); +} + +static void test_format_signed(struct kunit *test) +{ + u32 i; + + for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { + KUNIT_EXPECT_EQ(test, snd_pcm_format_signed(valid_fmt[i].format), + valid_fmt[i].sd < 0 ? -EINVAL : valid_fmt[i].sd); + KUNIT_EXPECT_EQ(test, snd_pcm_format_unsigned(valid_fmt[i].format), + valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd); + } + + KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL); +} + +static void test_format_endianness(struct kunit *test) +{ + u32 i; + + for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { + KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(valid_fmt[i].format), + valid_fmt[i].le < 0 ? -EINVAL : valid_fmt[i].le); + KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(valid_fmt[i].format), + valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le); + } + + KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(-1), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(-1), -EINVAL); +} + +static void _test_fill_silence(struct kunit *test, struct snd_format_test_data *data, + u8 *buffer, size_t samples_count) +{ + size_t sample_bytes = data->physical_bits >> 3; + u32 i; + + KUNIT_ASSERT_EQ(test, snd_pcm_format_set_silence(data->format, buffer, samples_count), 0); + for (i = 0; i < samples_count * sample_bytes; i++) + KUNIT_EXPECT_EQ(test, buffer[i], data->silence[i % sample_bytes]); +} + +static void test_format_fill_silence(struct kunit *test) +{ + u32 buf_samples[] = { 10, 20, 32, 64, 129, 260 }; + u8 *buffer; + u32 i, j; + + buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL); + + for (i = 0; i < ARRAY_SIZE(buf_samples); i++) { + for (j = 0; j < ARRAY_SIZE(valid_fmt); j++) + _test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]); + } + + KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT, buffer, 20), -EINVAL); + KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0); +} + +static snd_pcm_uframes_t calculate_boundary(snd_pcm_uframes_t buffer_size) +{ + snd_pcm_uframes_t boundary = buffer_size; + + while (boundary * 2 <= 0x7fffffffUL - buffer_size) + boundary *= 2; + return boundary; +} + +static struct avail_test_data p_avail_data[] = { + /* buf_size + hw_ptr < appl_ptr => avail = buf_size + hw_ptr - appl_ptr + boundary */ + { 128, 1000, 1129, 1073741824UL - 1 }, + /* + * buf_size + hw_ptr - appl_ptr >= boundary => + * => avail = buf_size + hw_ptr - appl_ptr - boundary + */ + { 128, 1073741824UL, 10, 118 }, + /* standard case: avail = buf_size + hw_ptr - appl_ptr */ + { 128, 1000, 1001, 127 }, +}; + +static void test_playback_avail(struct kunit *test) +{ + struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL); + u32 i; + + r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL); + r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL); + + for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) { + r->buffer_size = p_avail_data[i].buffer_size; + r->boundary = calculate_boundary(r->buffer_size); + r->status->hw_ptr = p_avail_data[i].hw_ptr; + r->control->appl_ptr = p_avail_data[i].appl_ptr; + KUNIT_EXPECT_EQ(test, snd_pcm_playback_avail(r), p_avail_data[i].expected_avail); + } +} + +static struct avail_test_data c_avail_data[] = { + /* hw_ptr - appl_ptr < 0 => avail = hw_ptr - appl_ptr + boundary */ + { 128, 1000, 1001, 1073741824UL - 1 }, + /* standard case: avail = hw_ptr - appl_ptr */ + { 128, 1001, 1000, 1 }, +}; + +static void test_capture_avail(struct kunit *test) +{ + struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL); + u32 i; + + r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL); + r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL); + + for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) { + r->buffer_size = c_avail_data[i].buffer_size; + r->boundary = calculate_boundary(r->buffer_size); + r->status->hw_ptr = c_avail_data[i].hw_ptr; + r->control->appl_ptr = c_avail_data[i].appl_ptr; + KUNIT_EXPECT_EQ(test, snd_pcm_capture_avail(r), c_avail_data[i].expected_avail); + } +} + +static void test_card_set_id(struct kunit *test) +{ + struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL); + + snd_card_set_id(card, VALID_NAME); + KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME); + + /* clear the first id character so we can set it again */ + card->id[0] = '\0'; + snd_card_set_id(card, NAME_W_SPEC_CHARS); + KUNIT_EXPECT_STRNEQ(test, card->id, NAME_W_SPEC_CHARS); + + card->id[0] = '\0'; + snd_card_set_id(card, NAME_W_SPACE); + kunit_info(test, "%s", card->id); + KUNIT_EXPECT_STREQ(test, card->id, NAME_W_SPACE_REMOVED); +} + +static void test_pcm_format_name(struct kunit *test) +{ + u32 i; + const char *name; + + for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) { + name = snd_pcm_format_name(valid_fmt[i].format); + KUNIT_ASSERT_NOT_NULL_MSG(test, name, "Don't have name for %s", valid_fmt[i].name); + KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name); + } + + KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT), "Unknown"); + KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(-1), "Unknown"); +} + +static void test_card_add_component(struct kunit *test) +{ + struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL); + + snd_component_add(card, TEST_FIRST_COMPONENT); + KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT); + + snd_component_add(card, TEST_SECOND_COMPONENT); + KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT " " TEST_SECOND_COMPONENT); +} + +static struct kunit_case sound_utils_cases[] = { + KUNIT_CASE(test_phys_format_size), + KUNIT_CASE(test_format_width), + KUNIT_CASE(test_format_endianness), + KUNIT_CASE(test_format_signed), + KUNIT_CASE(test_format_fill_silence), + KUNIT_CASE(test_playback_avail), + KUNIT_CASE(test_capture_avail), + KUNIT_CASE(test_card_set_id), + KUNIT_CASE(test_pcm_format_name), + KUNIT_CASE(test_card_add_component), + {}, +}; + +static struct kunit_suite sound_utils_suite = { + .name = "sound-core-test", + .test_cases = sound_utils_cases, +}; + +kunit_test_suite(sound_utils_suite); +MODULE_AUTHOR("Ivan Orlov"); +MODULE_LICENSE("GPL");