From patchwork Mon Jul 29 08:59:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 815148 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.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 CA35013DDC0; Mon, 29 Jul 2024 08:59:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243566; cv=none; b=fwLif+IAueQ3F6UgJVAD5FhlyzzxCpGWK9CM2bm8WrxQ9JerQHFmoBB7RCbML4iMJHZD5jcJZj+MXvU2WHrekcqtuNyWTtwigA86XcL7xkvzfOhcW9ErNVaBfbSefG8WebP/98yNl8JMYSWPNf+GOldOUnHQ6mu1K7ScJgv4Rs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243566; c=relaxed/simple; bh=kCOkijbVZPA0bTMuA0k1KDGDNjW/CTFQMRFUMcaI3Yk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FLYvDf7ykmU3qab9/EPUhmYssIDPyprLp4CoYSbapRgQO1dNfvoEPVNT5WFgUQ2mhFWB4P4GLKtV/fmuSFS1LkMLRWOYqRDivjaXpHWPF68Y8ORAu4ZGBCuwys4zYtYTyC8/4MbG5e3P4INFMsuYnxskCBLgoj78kbYnGArJnW4= 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=YDXDxZyQ; arc=none smtp.client-ip=209.85.221.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="YDXDxZyQ" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-3684c05f7afso309499f8f.2; Mon, 29 Jul 2024 01:59:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722243563; x=1722848363; 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=CYI54NILj5XrVQ9i1xhNNwdpH346o8zj3Rv7bwR6cIw=; b=YDXDxZyQBczSwtM5+4kB171fFT9EPn80xDKCIStzMgviL9BfwtIZ5wKbyiPkv8kikK IYGs7pcg8TbxmVWWjo8C7UxdaZIPtEUjrDS85qJpBjdhYFWMAj7ZplWAAfV6Xo6ROv9H 78ETknw1XpHMJ4lD8wtWEPifHO4jcprsur/9mTFfrQ2TuJS0Rhg+VC7tD7W/5gfmFuj/ OKoEXBFNpOabTBitQdwImAu+U4p1TYY7PcGYUFSpUBluexDBNpgZWlWYnUPORO6Se3W/ ecjqxcfeV4yq2QLIt3VQ3bMwrvdbr0xo/Y3PE0OwV2Tm/MVM7Rwvz7LYeDu3+8ns5IXH rIuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722243563; x=1722848363; 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=CYI54NILj5XrVQ9i1xhNNwdpH346o8zj3Rv7bwR6cIw=; b=uWGhbnqAKYVPxpssYs47x0VI0EixsV72YediDui3DGaEyMCDMLMFWFleCWyBwJ8Rqd 7rwRkZVh7YXqJJtqGROPZPsgrPrKqWIDUK2S6R/bxss/apkajC3aN0e0YF11tFaMt7dv vVwdD3oJr+a3Qy4cT7WGxNdmPXmSaGiewK8hratScemUvm2LnL3sS3C2uZZVEewJefeM 4gclAl6KB5bGzREdG6KNWMpGfnqYV6QrN7PWcALfTxWdpvLr8rNcA4KeKdhsZuSu7YcG NU4joSMMW0/FO8YhW8m4y1jAsLjjK2wvQMIKYhEPBnBvGMeqneWjiHdPpA9EHaletPXz 70sw== X-Forwarded-Encrypted: i=1; AJvYcCUARVSWnnDPsG8hvbAoDL0Jp9LaMttYGwjTLJl4Zvjxee/PVG8DvIoxhSR+7ftNvi11ULFUOKIrOfAhRt5oqBCx@vger.kernel.org, AJvYcCVnro7RI5apEqOyeJgrT891O6zM6rZf5vn+REqquDOhwY926IbOqEL/FFvA7Icvf0fuWxR00p8K5X0AujW4@vger.kernel.org, AJvYcCWP8AEAOeii7MQj/6tDieq8HOjS6dV25lf0Yu1099WXuxnCfNBsGuayHbq1Pzm/J2Z1O4t5MO1g6Dk=@vger.kernel.org, AJvYcCXikcLLyAxKrCM8Hf3mwKZiQrDov6sVM5q7sEBxRw48wzbbUnAGdv7/R7uFR97L60U+3Nj5l6y6osjO+oQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz9zwXRmHtGELcNDPdsb5gz8+3d6lJDyBJm2/fYTkRjki2xDLQ/ Z0ZzKQRnBtN41wWu1gJPMpdyO0KMSdZo3hyOBCsteUDrTtfRHPTl X-Google-Smtp-Source: AGHT+IEOjhLMNFawjY53IvOv0Vx656pm5RPkGE2DaZZQ7OJcqlXcOn55xPiiETw2mHN3nuSxdCbN4g== X-Received: by 2002:a05:6000:1849:b0:35f:2584:76e9 with SMTP id ffacd0b85a97d-36b34d183b9mr5958093f8f.2.1722243563129; Mon, 29 Jul 2024 01:59:23 -0700 (PDT) Received: from ivan-HLYL-WXX9.. ([31.94.28.214]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857e46sm11715337f8f.67.2024.07.29.01.59.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 01:59:22 -0700 (PDT) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com, corbet@lwn.net, broonie@kernel.org, shuah@kernel.org Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, christophe.jaillet@wanadoo.fr Subject: [PATCH v2 1/4] ALSA: aloop: Allow using global timers Date: Mon, 29 Jul 2024 09:59:02 +0100 Message-Id: <20240729085905.6602-2-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240729085905.6602-1-ivan.orlov0322@gmail.com> References: <20240729085905.6602-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Allow using global timers as a timer source when card id is equal to -1 in the timer_source parameter. Signed-off-by: Ivan Orlov --- V1 -> V2: - No changes sound/drivers/aloop.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c index d6dd4b8c750a..a1e5e7fe9c72 100644 --- a/sound/drivers/aloop.c +++ b/sound/drivers/aloop.c @@ -1130,6 +1130,8 @@ static int loopback_parse_timer_id(const char *str, } } } + if (card_idx == -1) + tid->dev_class = SNDRV_TIMER_CLASS_GLOBAL; if (!err && tid) { tid->card = card_idx; tid->device = dev; From patchwork Mon Jul 29 08:59:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 815511 Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 92FF513D8A2; Mon, 29 Jul 2024 08:59:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243572; cv=none; b=bAC72bivYx19pUjEPCLx2Cfqd/YIiFSGCemqX5z7XmkSNvKrHdCScptN4I/wcw7MlRbiHA5Zun9Uys4iDv+AJhRTjzMPl2uPps0j3Kcrv+t1+V/XgceOaWOKd/XKbvP59oBCMvhQPfFVzQGRMxV7hiehIIpPF63O98a+uQLFl6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243572; c=relaxed/simple; bh=1ITLH7WZTXYHLb8vO8Qu8TyaVRGYFlY83qJu+QsdetA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SwcWuxjLb0oP9SfWgJjGla4FxaYbrqquuqvi70Yr3KLyI5dmy6bxBMBwQz51/ja0k+vxlQHa5/80Slac6GemkwCsEZ4kWlkV8iAKsDLxb/RiKwS4omygXf1JRp6wTPDhAuSOrn3bstOHqU5eIZy8GfRoiDEOfo7L2vZqcS8ln94= 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=AUcVfLmx; arc=none smtp.client-ip=209.85.221.52 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="AUcVfLmx" Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-36872b6d9bcso66043f8f.3; Mon, 29 Jul 2024 01:59:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722243569; x=1722848369; 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=Encnxu5bo2KYPGeU7l1IOtPLycNQoJe8lNlB67W9ohs=; b=AUcVfLmxg5iqtbpfUAsRW2H4CYLMPxXZDea1psyrRA4nfR6UI6CfzoMOSBTt5jDRtz gUPW1VWmpSee6tfRV/vGHyFhR9GYJ3JPL7U1DrbVHqgpLjOBV4w+MBGTTGk24rRq6tD5 dNDU19B1HVtnMGKsvocNXAQ069wbJQ9aR7QmqBeJvnVCRWCu+q3Knzd8iSnENdLmjOkV UlHFwvYsfiui/Uhn5F0MEyeJxTGPbB9pWLdOXDLm2rE183iJNeEs6RUDKizR9MZf+clf HG4TYmh2xetsljgAXxsII6W81j5rxmayZalBXW17eE/hnBlNWM2muIm6T6xe4UQazTdU sGSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722243569; x=1722848369; 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=Encnxu5bo2KYPGeU7l1IOtPLycNQoJe8lNlB67W9ohs=; b=mCVuujpPSgQsvdjxvFT3vSGEczEXelzRoKriCavThDnRsr1ybL2QTVweac6Zan0FRE VOs31cSKGYkN+KKC6E+SECB/Un2Guk5hWD04cSQ13X5gnfUbxJ5ZzuyMFf1yVFZib41g fvwu/Vjqvab/+51kvP3bh4b8HNXakZpqX/N2/PcC6hl+OFzs7MFdFfqSx5EZFiG0QqI7 8udCPIRym2E4Ts/66aSsFGnLQLWtkpM0vFvJ/mnM538AeqEhPvgjHKM9stHl7k/pIv23 rDc54Nwf+3oDrcmiuGaHqWv5Qs7++AeOv9o/Kuto/HLdr6ETuWtvOV46En/HPKMDjg3C RETg== X-Forwarded-Encrypted: i=1; AJvYcCXMEAs0KDfHBS7J5iWmTIJEplDtJYXipDsV+3utickAZL+j+FE6NDKrLlrpGznPJB2eZRRM0RVxRSDdM/rusKC/kyyEdLYWyswahhHlOYoWu0fnmJ37gVe+Pyn9/jwS2K8I58KwNTna/hzg7QtWPDxjdOSAS/dIrxgbkz+y+/q1lTNO632gUjVxPLm4kjXFnQcjQvdr6Cn1SRUxlzttXu3F1A9Hz3Y= X-Gm-Message-State: AOJu0YzoK/2g8wBgVZF/+PdHRIFmUI9besSWCRys8kl0WaaegX8bnWA7 JyuJhMmm7bDpnsj2zz5C44DaLjOSzoEbDjPLdMX5Xb/RhGS2/UBH X-Google-Smtp-Source: AGHT+IGLquqQFEebe7H0zV2X2QmqWfA3O598xFQWgBOpRugP4/tIrCWhReegzIRGtJh8KT/76ECx+g== X-Received: by 2002:a05:6000:178c:b0:368:4e52:5877 with SMTP id ffacd0b85a97d-36b34e30238mr5755386f8f.9.1722243568742; Mon, 29 Jul 2024 01:59:28 -0700 (PDT) Received: from ivan-HLYL-WXX9.. ([31.94.28.214]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857e46sm11715337f8f.67.2024.07.29.01.59.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 01:59:28 -0700 (PDT) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com, corbet@lwn.net, broonie@kernel.org, shuah@kernel.org Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, christophe.jaillet@wanadoo.fr, Axel Holzinger Subject: [PATCH v2 2/4] Docs/sound: Add documentation for userspace-driven ALSA timers Date: Mon, 29 Jul 2024 09:59:03 +0100 Message-Id: <20240729085905.6602-3-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240729085905.6602-1-ivan.orlov0322@gmail.com> References: <20240729085905.6602-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add the documentation which describes the new userspace-driven timers API introduced in this patch series. The documentation contains: - Description of userspace-driven ALSA timers, what they are for - Description of the timers API - Example of how the timers can be created and triggered - How the timers can be used as a timer sources for snd-aloop module Suggested-by: Axel Holzinger Signed-off-by: Ivan Orlov --- V1 -> V2: - No changes Documentation/sound/index.rst | 1 + Documentation/sound/utimers.rst | 120 ++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 Documentation/sound/utimers.rst diff --git a/Documentation/sound/index.rst b/Documentation/sound/index.rst index 7e67e12730d3..c437f2a4bc85 100644 --- a/Documentation/sound/index.rst +++ b/Documentation/sound/index.rst @@ -13,6 +13,7 @@ Sound Subsystem Documentation alsa-configuration hd-audio/index cards/index + utimers .. only:: subproject and html diff --git a/Documentation/sound/utimers.rst b/Documentation/sound/utimers.rst new file mode 100644 index 000000000000..0bd875ea9986 --- /dev/null +++ b/Documentation/sound/utimers.rst @@ -0,0 +1,120 @@ +.. SPDX-License-Identifier: GPL-2.0 + +======================= +Userspace-driven timers +======================= + +:Author: Ivan Orlov + +Preface +======= + +This document describes the userspace-driven timers: virtual ALSA timers +which could be created and controlled by userspace applications using +IOCTL calls. Such timers could be useful when synchronizing audio +stream with timer sources which we don't have ALSA timers exported for +(e.g. PTP clocks), and when synchronizing the audio stream going through +two virtual sound devices using ``snd-aloop`` (for instance, when +we have a network application sending frames to one snd-aloop device, +and another sound application listening on the other end of snd-aloop). + +Enabling userspace-driven timers +================================ + +The userspace-driven timers could be enabled in the kernel using the +``CONFIG_SND_UTIMER`` configuration option. It depends on the +``CONFIG_SND_TIMER`` option, so it also should be enabled. + +Userspace-driven timers API +=========================== + +Userspace application can create a userspace-driven ALSA timer by +executing the ``SNDRV_TIMER_IOCTL_CREATE`` ioctl call on the +``/dev/snd/timer`` device file descriptor. The ``snd_utimer_info`` +structure should be passed as an ioctl argument: + +:: + + struct snd_utimer_info { + snd_pcm_uframes_t frame_rate; + snd_pcm_uframes_t period_size; + unsigned int id; + } + +``frame_rate`` and ``period_size`` set the desired frame rate and period +size emulated by the virtual timer respectively. ``id`` field gets +overwritten by the ioctl, and the identifier you get in this field after +the call can be used as a timer subdevice number when passing the timer +to ``snd-aloop`` kernel module or other userspace applications. There +could be up to 128 userspace-driven timers in the system at one moment +of time, thus the id value ranges from 0 to 127. + +Besides from overwriting the ``snd_utimer_info`` struct, ioctl returns +a timer file descriptor, which can be used to trigger the timer. This +guarantees that the timer can only be triggered by the process which +created it. The timer then can be triggered with +``SNDRV_TIMER_IOCTL_TRIGGER`` ioctl call on the timer file descriptor. + +So, the example code for creating and triggering the timer would be: + +:: + + static const struct snd_utimer_info utimer_info = { + .frame_rate = 8000, + .period_size = 4410, + .id = -1, + }; + + int timer_device_fd = open("/dev/snd/timer", O_RDWR | O_CLOEXEC); + int utimer_fd = ioctl(timer_device_fd, SNDRV_TIMER_IOCTL_CREATE, &utimer_info); + + if (utimer_fd < 0) { + perror("Failed to create the timer"); + return -1; + } + + ... + + /* + * Now we want to trigger the timer. Callbacks of all of the + * timer instances binded to this timer will be executed after + * this call. + */ + ioctl(utimer_fd, SNDRV_TIMER_IOCTL_TRIGGER, NULL); + + ... + + /* Now, destroy the timer */ + close(utimer_fd); + + +More detailed example of creating and ticking the timer could be found +in the utimer ALSA selftest. + +Userspace-driven timers and snd-aloop +------------------------------------- + +Userspace-driven timers could be easily used with ``snd-aloop`` module +when synchronizing two sound applications on both ends of the virtual +sound loopback. For instance, if one of the applications receives sound +frames from network and sends them to snd-aloop pcm device, and another +application listens for frames on the other snd-aloop pcm device, it +makes sense that the ALSA middle layer should initiate a data +transaction when the new period of data is received through network, but +not when the certain amount of jiffies elapses. Userspace-driven ALSA +timers could be used to achieve this. + +To use userspace-driven ALSA timer as a timer source of snd-aloop, pass +the following string as the snd-aloop ``timer_source`` parameter: + +:: + + # modprobe snd-aloop timer_source="-1.4." + +Where ``utimer_id`` is the id of the timer you created with +``SNDRV_TIMER_IOCTL_CREATE``, and ``4`` is the number of +userspace-driven timers device (``SNDRV_TIMER_GLOBAL_UDRIVEN``). + +After that, each time you trigger the timer with +``SNDRV_TIMER_IOCTL_TRIGGER`` the new period of data will be transferred +from one snd-aloop device to another. From patchwork Mon Jul 29 08:59:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 815147 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 DB46113DDC0; Mon, 29 Jul 2024 08:59:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243576; cv=none; b=TUGyQvjzrNSZjBw+vn0gH39FO6lO+RyfFSMV7Hnko8RNZVbTUzLs2cCv14QIl/pXK5yg9q6vwP+mnuAb7JvOGmUM6G4hbDDQD/DZc7E7KWni2CmMoOELE/H2hHAMx6SgaGbxtJob7C1MJe+RSiNSgw5k6MOVVbF+5E4a+0qMpBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243576; c=relaxed/simple; bh=J3PQ8BsFf+vEHgJHkLWSeFPf5OLW/U3dzqmyi+haA5Y=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Gg+E8GeaeXXxU7uqpAoV2wWmcXoXyLrKdkYvz1ViVRKKqgLHvKtZB50pfXAcLz4QD6HrsHNqdE7j0Jw8LDZn4bX1qFvFoOgI+XNPmgT1CNaGWfr2GUGuSU/BViqkzt6LKhCR0wpgVnVvwv3W62VE9eLaoTQH9fJJ8t9HvUQSSt8= 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=iKUeVLm8; arc=none smtp.client-ip=209.85.128.49 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="iKUeVLm8" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-42111cf2706so2489945e9.0; Mon, 29 Jul 2024 01:59:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722243573; x=1722848373; 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=/vp32JISpq9hjsoktzc32ijoICkArdzEgblzI4oe8J4=; b=iKUeVLm84ADbL4IyuOoPZmZVpe+wz17rJoibQEWI7aJ8Io5zst2G3TGQ8qxpvDXNzV ypNA0h39PEh6xz4Xw88m1k2T6VhLIzR42R2LeFdcTbNGrKr3oIJfvsjgaPYhK63RVFqH cijz9jwIHtaYhuezwwZu8AF31KZ802shAKFd/lo/j2zsQ/op45AJg4jf6b8bJgZhiALq HGiw+uO01Y7bEgXWhio93iq1A/cF+TfWEjE6dw1l5fha/6W13u5CZrLQpgC+9VdLkwzG 9rsjzVELHr7zMDYeY4BpIBdmH/o60oN184bMC4POII5mrSEtVTs62gUfo+0cLomoA7N6 ij2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722243573; x=1722848373; 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=/vp32JISpq9hjsoktzc32ijoICkArdzEgblzI4oe8J4=; b=FQ9mdBtHqnFOEnMqzm63QGh3C1MduPTiOKa+Y3y+EyLdiVoUFoih1XEIE54LD53zse usxtkm1TLdbZXNJipAuXrzLppg5Tze40c8iDhqLN7/i066HG1eB6pkgfrtRn/UfepeSd Ud4bo0ZdFqImc06lu5HnajKa1WtBHe8VcFmOGyHI9r7Gf32KrQfzAquoMwzETm6UyfIO Y1LEBCTE6r4pBZC4G/SzmNIBqrTXGrCxPX6/Fqv8FuqcWHIoUZOfJWqtHJUp2lIj0GSD NgMmvPIcYkKPxpzRl89OZc9mFzldv5TrK+VYfSwVsmCv8tPcaABnRFodYZeJO2os1LKR cEUg== X-Forwarded-Encrypted: i=1; AJvYcCUdDIvrAEbRfu7sCF0zQppFBGJ4B+lcbWMwLUPGvwNAGefGRI0j0lQmmNyd0xrdWbX2KzCQu78BJY85leXA9sDD6EchaSrmBRo+608EcIGkbiUmRnLgdFfHSGIBKhRSMVqE357D+xRVPKoPupb8SHk9Sqn3Z8vs8H+OyYeccysRKTaPuFcB3XA+oq8iuze+VTSpRw4ypPrfzDXOqDXsdug6QsPF4HU= X-Gm-Message-State: AOJu0YyJvDBgPeUM+1wnMnx9nF1qAvmahhj1KKTCaKmEwMzPAcfHXiAc lKLRtVXXWU1iIUoWokGVhSCc3rgIhRO+dOl7NAYcyODV7CeylXdR X-Google-Smtp-Source: AGHT+IE4d6fetVvL65CH1RKUxN5tlVbWehlLcuGZHUySnvU0sRuequzaxg78zJAyqzBZ40Dp9QoHEQ== X-Received: by 2002:a05:600c:35ca:b0:427:9f71:16bb with SMTP id 5b1f17b1804b1-42805713a1emr55445495e9.6.1722243572596; Mon, 29 Jul 2024 01:59:32 -0700 (PDT) Received: from ivan-HLYL-WXX9.. ([31.94.28.214]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857e46sm11715337f8f.67.2024.07.29.01.59.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 01:59:32 -0700 (PDT) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com, corbet@lwn.net, broonie@kernel.org, shuah@kernel.org Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, christophe.jaillet@wanadoo.fr, Axel Holzinger Subject: [PATCH v2 3/4] ALSA: timer: Introduce virtual userspace-driven timers Date: Mon, 29 Jul 2024 09:59:04 +0100 Message-Id: <20240729085905.6602-4-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240729085905.6602-1-ivan.orlov0322@gmail.com> References: <20240729085905.6602-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Implement two ioctl calls in order to support virtual userspace-driven ALSA timers. The first ioctl is SNDRV_TIMER_IOCTL_CREATE, which gets the snd_utimer_info struct as a parameter and returns a file descriptor of a virtual timer. It also updates the `id` field of the snd_utimer_info struct, which provides a unique identifier for the timer (basically, the subdevice number which can be used when creating timer instances). This patch also introduces a tiny id allocator for the userspace-driven timers, which guarantees that we don't have more than 128 of them in the system. Another ioctl is SNDRV_TIMER_IOCTL_TRIGGER, which allows us to trigger the virtual timer (and calls snd_timer_interrupt for the timer under the hood), causing all of the timer instances binded to this timer to execute their callbacks. The maximum amount of ticks available for the timer is 1 for the sake of simplification of the userspace API. 'start', 'stop', 'open' and 'close' callbacks for the userspace-driven timers are empty since we don't really do any hardware initialization here. Suggested-by: Axel Holzinger Signed-off-by: Ivan Orlov --- V1 -> V2: - Add missing kfree for the utimer name in snd_utimer_free - Remove extra newline in sound core Kconfig - Use IDA allocator API to allocate utimer ids - Use kasprintf for the timer name instead of kzalloc + sprintf include/uapi/sound/asound.h | 17 +++ sound/core/Kconfig | 10 ++ sound/core/timer.c | 213 ++++++++++++++++++++++++++++++++++++ 3 files changed, 240 insertions(+) diff --git a/include/uapi/sound/asound.h b/include/uapi/sound/asound.h index 8bf7e8a0eb6f..ade952a54edd 100644 --- a/include/uapi/sound/asound.h +++ b/include/uapi/sound/asound.h @@ -894,6 +894,7 @@ enum { #define SNDRV_TIMER_GLOBAL_RTC 1 /* unused */ #define SNDRV_TIMER_GLOBAL_HPET 2 #define SNDRV_TIMER_GLOBAL_HRTIMER 3 +#define SNDRV_TIMER_GLOBAL_UDRIVEN 4 /* info flags */ #define SNDRV_TIMER_FLG_SLAVE (1<<0) /* cannot be controlled */ @@ -974,6 +975,20 @@ struct snd_timer_status { }; #endif +/* + * This structure describes the userspace-driven timer. Such timers are purely virtual, + * and can only be triggered from software (for instance, by userspace application). + */ +struct snd_utimer_info { + /* + * To pretend being a normal timer, we need to know the frame rate and + * the period size in frames. + */ + snd_pcm_uframes_t frame_rate; + snd_pcm_uframes_t period_size; + unsigned int id; +}; + #define SNDRV_TIMER_IOCTL_PVERSION _IOR('T', 0x00, int) #define SNDRV_TIMER_IOCTL_NEXT_DEVICE _IOWR('T', 0x01, struct snd_timer_id) #define SNDRV_TIMER_IOCTL_TREAD_OLD _IOW('T', 0x02, int) @@ -990,6 +1005,8 @@ struct snd_timer_status { #define SNDRV_TIMER_IOCTL_CONTINUE _IO('T', 0xa2) #define SNDRV_TIMER_IOCTL_PAUSE _IO('T', 0xa3) #define SNDRV_TIMER_IOCTL_TREAD64 _IOW('T', 0xa4, int) +#define SNDRV_TIMER_IOCTL_CREATE _IOWR('T', 0xa5, struct snd_utimer_info) +#define SNDRV_TIMER_IOCTL_TRIGGER _IO('T', 0xa6) #if __BITS_PER_LONG == 64 #define SNDRV_TIMER_IOCTL_TREAD SNDRV_TIMER_IOCTL_TREAD_OLD diff --git a/sound/core/Kconfig b/sound/core/Kconfig index b970a1734647..670b26cf3065 100644 --- a/sound/core/Kconfig +++ b/sound/core/Kconfig @@ -251,6 +251,16 @@ config SND_JACK_INJECTION_DEBUG Say Y if you are debugging via jack injection interface. If unsure select "N". +config SND_UTIMER + bool "Enable support for userspace-controlled virtual timers" + depends on SND_TIMER + help + Say Y to enable the support of userspace-controlled timers. These + timers are purely virtual, and they are supposed to be triggered + from userspace. They could be quite useful when synchronizing the + sound timing with userspace applications (for instance, when sending + data through snd-aloop). + config SND_VMASTER bool diff --git a/sound/core/timer.c b/sound/core/timer.c index d104adc75a8b..6e445df7d9a0 100644 --- a/sound/core/timer.c +++ b/sound/core/timer.c @@ -13,6 +13,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -109,6 +112,16 @@ struct snd_timer_status64 { unsigned char reserved[64]; /* reserved */ }; +#ifdef CONFIG_SND_UTIMER +#define SNDRV_UTIMERS_MAX_COUNT 128 +/* Internal data structure for keeping the state of the userspace-driven timer */ +struct snd_utimer { + char *name; + struct snd_timer *timer; + unsigned int id; +}; +#endif + #define SNDRV_TIMER_IOCTL_STATUS64 _IOR('T', 0x14, struct snd_timer_status64) /* list of timers */ @@ -2009,6 +2022,204 @@ enum { SNDRV_TIMER_IOCTL_PAUSE_OLD = _IO('T', 0x23), }; +#ifdef CONFIG_SND_UTIMER +/* + * Since userspace-driven timers are passed to userspace, we need to have an identifier + * which will allow us to use them (basically, the subdevice number of udriven timer). + */ +DEFINE_IDA(snd_utimer_ids); + +static void snd_utimer_put_id(struct snd_utimer *utimer) +{ + int timer_id = utimer->id; + + snd_BUG_ON(timer_id < 0 || timer_id >= SNDRV_UTIMERS_MAX_COUNT); + ida_free(&snd_utimer_ids, timer_id); +} + +static int snd_utimer_take_id(void) +{ + return ida_alloc_max(&snd_utimer_ids, SNDRV_UTIMERS_MAX_COUNT - 1, GFP_KERNEL); +} + +static void snd_utimer_free(struct snd_utimer *utimer) +{ + snd_timer_free(utimer->timer); + snd_utimer_put_id(utimer); + kfree(utimer->name); + kfree(utimer); +} + +static int snd_utimer_release(struct inode *inode, struct file *file) +{ + struct snd_utimer *utimer = (struct snd_utimer *)file->private_data; + + snd_utimer_free(utimer); + return 0; +} + +static int snd_utimer_trigger(struct file *file) +{ + struct snd_utimer *utimer = (struct snd_utimer *)file->private_data; + + snd_timer_interrupt(utimer->timer, utimer->timer->sticks); + return 0; +} + +static long snd_utimer_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) +{ + switch (ioctl) { + case SNDRV_TIMER_IOCTL_TRIGGER: + return snd_utimer_trigger(file); + } + + return -ENOTTY; +} + +static const struct file_operations snd_utimer_fops = { + .llseek = noop_llseek, + .release = snd_utimer_release, + .unlocked_ioctl = snd_utimer_ioctl, +}; + +static int snd_utimer_start(struct snd_timer *t) +{ + return 0; +} + +static int snd_utimer_stop(struct snd_timer *t) +{ + return 0; +} + +static int snd_utimer_open(struct snd_timer *t) +{ + return 0; +} + +static int snd_utimer_close(struct snd_timer *t) +{ + return 0; +} + +static const struct snd_timer_hardware timer_hw = { + .flags = SNDRV_TIMER_HW_AUTO | SNDRV_TIMER_HW_WORK, + .open = snd_utimer_open, + .close = snd_utimer_close, + .start = snd_utimer_start, + .stop = snd_utimer_stop, +}; + +static int snd_utimer_create(struct snd_utimer_info *utimer_info, + struct snd_utimer **r_utimer) +{ + struct snd_utimer *utimer; + struct snd_timer *timer; + struct snd_timer_id tid; + int utimer_id; + int err = 0; + + utimer = kzalloc(sizeof(*utimer), GFP_KERNEL); + if (!utimer) + return -ENOMEM; + + /* We hold the ioctl lock here so we won't get a race condition when allocating id */ + utimer_id = snd_utimer_take_id(); + if (utimer_id < 0) { + err = utimer_id; + goto err_take_id; + } + + utimer->name = kasprintf(GFP_KERNEL, "snd-utimer%d", utimer_id); + if (!utimer->name) { + err = -ENOMEM; + goto err_get_name; + } + + utimer->id = utimer_id; + + tid.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION; + tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL; + tid.card = -1; + tid.device = SNDRV_TIMER_GLOBAL_UDRIVEN; + tid.subdevice = utimer_id; + + err = snd_timer_new(NULL, utimer->name, &tid, &timer); + if (err < 0) { + pr_err("Can't create userspace-driven timer\n"); + goto err_timer_new; + } + + timer->module = THIS_MODULE; + timer->hw = timer_hw; + timer->hw.resolution = NANO / utimer_info->frame_rate * utimer_info->period_size; + timer->hw.ticks = 1; + timer->max_instances = MAX_SLAVE_INSTANCES; + + utimer->timer = timer; + + err = snd_timer_global_register(timer); + if (err < 0) { + pr_err("Can't register a userspace-driven timer\n"); + goto err_timer_reg; + } + + *r_utimer = utimer; + return 0; + +err_timer_reg: + snd_timer_free(timer); +err_timer_new: + kfree(utimer->name); +err_get_name: + snd_utimer_put_id(utimer); +err_take_id: + kfree(utimer); + + return err; +} + +static int snd_utimer_ioctl_create(struct file *file, + struct snd_utimer_info __user *_utimer_info) +{ + struct snd_utimer *utimer; + struct snd_utimer_info *utimer_info; + int err; + + utimer_info = memdup_user(_utimer_info, sizeof(*utimer_info)); + if (IS_ERR(utimer_info)) + return PTR_ERR(no_free_ptr(utimer_info)); + + err = snd_utimer_create(utimer_info, &utimer); + if (err < 0) { + kfree(utimer_info); + return err; + } + + utimer_info->id = utimer->id; + + err = copy_to_user(_utimer_info, utimer_info, sizeof(*utimer_info)); + if (err) { + snd_utimer_free(utimer); + kfree(utimer_info); + return -EFAULT; + } + + kfree(utimer_info); + + return anon_inode_getfd(utimer->name, &snd_utimer_fops, utimer, O_RDWR | O_CLOEXEC); +} + +#else + +static int snd_utimer_ioctl_create(struct file *file, + struct snd_utimer_info __user *_utimer_info) +{ + return -EINVAL; +} + +#endif + static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd, unsigned long arg, bool compat) { @@ -2053,6 +2264,8 @@ static long __snd_timer_user_ioctl(struct file *file, unsigned int cmd, case SNDRV_TIMER_IOCTL_PAUSE: case SNDRV_TIMER_IOCTL_PAUSE_OLD: return snd_timer_user_pause(file); + case SNDRV_TIMER_IOCTL_CREATE: + return snd_utimer_ioctl_create(file, argp); } return -ENOTTY; } From patchwork Mon Jul 29 08:59:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Orlov X-Patchwork-Id: 815510 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (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 E96FE13DDAE; Mon, 29 Jul 2024 08:59:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243580; cv=none; b=PfBth2nKplo+Recf9vTzkFKWNuvWfYx9t0DZ5/hPIz8/l9UZ2Y9bnXQxFbPhJ/qrgOFeFwSeYv8DeTfaEBf8DMX/VFySgPFAK+dziIXFWndXq/ZbOtjRllJxQ48pv1lNPhNiSnBy+C9GHR/e5z/Xwm8iYVvmZRBh5+MGLXWkBvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722243580; c=relaxed/simple; bh=l08lgEMB6AMQ9VKxEFqHNoD4YCli9+3L6tCRgwh0Flc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uV55JYDIXWnQJXhaOVgS0w74HwqtVp56eBkKQzusAWtlM9AVF/KZK0T/jHP+URAuXKQ2nCQmmmxUxmYqqFPgg8AsorrT5SiR+Q/sVRZiVPdSbgR7tlslHYBUSaQZyBodR0xm6GIlUZSZ7h4XLcGVGNAzrWAX4lkk7qvlMxhaW+0= 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=CNTSa1vW; arc=none smtp.client-ip=209.85.221.43 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="CNTSa1vW" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-36841f56cf6so301336f8f.3; Mon, 29 Jul 2024 01:59:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722243577; x=1722848377; 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=fpcUC/gtXojJN/CiyqGEY07Ph3Yr5arD3Bg1bEmMUnE=; b=CNTSa1vWQIPoo89h2T4H9B+vscA3TT6Axygqn7kC6mRjz2K0MSYiqWlP0yfJsvtuqd JHzttZ4rIs4mSXgnCDZhA8UoCmmTC5R7ObpMi+7Z6fLByvICprRO7APpIIkh52V30qRB dGbZE4sYrpjyaePqy047ft1xt5bLoYf/4BwPluktx73i73Ir2jWRtUrlJSL3Mq4iAZZA LgeXjm+IelgzRwLBbw9vBUKpCU9L4YvlVlAjzgkkDihQvNMC+vXnSHQ+Yz9+Jg7UrP64 idLad673UdpwPdoDdOtFn7XFGEIywdzbBzaxf447zcgZ/iUyJXdZEG7UQ3Sqwvap4siW L37A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722243577; x=1722848377; 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=fpcUC/gtXojJN/CiyqGEY07Ph3Yr5arD3Bg1bEmMUnE=; b=tN+qxUQI89dj261Jdiz9QlbTetop0yIOPYKAN0M8xN0qUco+/+5Qa9wnjE076uuNbW XbE2FSr7GbwvDsw+K2zI/4Y/scOh/ncZknWpBzWwdtjJL7HKdhW7v60bvFxyjH/OpL1N zpJIDLUGXmG7nHqtaRu26bH/BsCc7UvMAMJkp74h2HYvX3EF8I3VEKRIvqjcXC1mJ9Od LNaCM6xonfWU2BZt/85moIXOkEufudnMu0Qxe7tCSvxDW/DLtMn+jSM6haxTKKiMJX6N qyRlJS7h7TJTR9Vnu6/S28rEgeKYo7+QsLFu88ouYWy078xgOCQbrkvjO1wmj26ktgMz AYFQ== X-Forwarded-Encrypted: i=1; AJvYcCU9X7KMa8zLgq9Idw6fZjSTMoXnG7gsT6lLX9uo9nP5gPgQsjuQwc0yLttOXphK857Iep5xKB6apqEt5Io=@vger.kernel.org, AJvYcCUE6qEcnc27rEk+wYVdQT0Q4wyN0zBuFdjpet+pIMm07ZL82Ud6MZaYiDx2rdFwSA+jADigVCKKAbXoDOW6@vger.kernel.org, AJvYcCUGn0tyf/7e66aCCniEnCvQb3LlLjYx2lfjGqjpaAksJ6EpTfXp6zL6GnWZJ5laTGHEo6tkp9mwASM=@vger.kernel.org, AJvYcCWpbqoeE2qtxaRvdbvz3lO7QPI4iD7x0QIXo8ppIpHMCqohNZEKRpvGmJgWQ+h2YjlO7+0i2ClA/YvuvzCHiVQT@vger.kernel.org X-Gm-Message-State: AOJu0YzTh0jyR9jAgX3Ldgrr1onWZzq/hZtdDXm8IzH+BpVGtlnrT0G8 NCsRhD9UD6FIKJgniKQBRxJXTeP8HMAuwSU1WfcbGrqksHgb2d1n X-Google-Smtp-Source: AGHT+IEDukh4bbv/MXsG2iqAEdWo7FbQ4vqcKp+kWjHzDZmIZjhbZ57xNvCJaMTnHMaWDi8F4NHoBg== X-Received: by 2002:a5d:6d8d:0:b0:364:8215:7142 with SMTP id ffacd0b85a97d-36b34d1840emr5838579f8f.1.1722243577128; Mon, 29 Jul 2024 01:59:37 -0700 (PDT) Received: from ivan-HLYL-WXX9.. ([31.94.28.214]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-36b36857e46sm11715337f8f.67.2024.07.29.01.59.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 01:59:36 -0700 (PDT) From: Ivan Orlov To: perex@perex.cz, tiwai@suse.com, corbet@lwn.net, broonie@kernel.org, shuah@kernel.org Cc: Ivan Orlov , linux-kselftest@vger.kernel.org, linux-doc@vger.kernel.org, linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org, christophe.jaillet@wanadoo.fr Subject: [PATCH v2 4/4] selftests: ALSA: Cover userspace-driven timers with test Date: Mon, 29 Jul 2024 09:59:05 +0100 Message-Id: <20240729085905.6602-5-ivan.orlov0322@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240729085905.6602-1-ivan.orlov0322@gmail.com> References: <20240729085905.6602-1-ivan.orlov0322@gmail.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a test for the new functionality of userspace-driven timers and the tool which allows us to count timer ticks in a certain time period. The test: 1. Creates a userspace-driven timer with ioctl to /dev/snd/timer 2. Starts the `global-timer` application to count the ticks of the timer from step 1. 3. Asynchronously triggers the timer multiple times with some interval 4. Compares the amount of caught ticks with the amount of trigger calls. Since we can't include and in one file due to overlapping declarations, I have to split the test into two applications: one of them counts the amount of timer ticks in the defined time period, and another one is the actual test which creates the timer, triggers it periodically and starts the first app to count the amount of ticks in a separate thread. Besides from testing the functionality itself, the test represents a sample application showing userspace-driven ALSA timers API. Signed-off-by: Ivan Orlov --- - Return NULL in the pthreaded function (ticking_func) - Process TIMER_NO_EVENT enum in the timer app output processing loop tools/testing/selftests/alsa/Makefile | 2 +- tools/testing/selftests/alsa/global-timer.c | 87 +++++++++++++ tools/testing/selftests/alsa/utimer-test.c | 137 ++++++++++++++++++++ 3 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/alsa/global-timer.c create mode 100644 tools/testing/selftests/alsa/utimer-test.c diff --git a/tools/testing/selftests/alsa/Makefile b/tools/testing/selftests/alsa/Makefile index c1ce39874e2b..0d5bd8ea900b 100644 --- a/tools/testing/selftests/alsa/Makefile +++ b/tools/testing/selftests/alsa/Makefile @@ -12,7 +12,7 @@ LDLIBS+=-lpthread OVERRIDE_TARGETS = 1 -TEST_GEN_PROGS := mixer-test pcm-test test-pcmtest-driver +TEST_GEN_PROGS := mixer-test pcm-test utimer-test test-pcmtest-driver global-timer TEST_GEN_PROGS_EXTENDED := libatest.so diff --git a/tools/testing/selftests/alsa/global-timer.c b/tools/testing/selftests/alsa/global-timer.c new file mode 100644 index 000000000000..c15ec0ba851a --- /dev/null +++ b/tools/testing/selftests/alsa/global-timer.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This tool is used by the utimer test, and it allows us to + * count the ticks of a global timer in a certain time frame + * (which is set by `timeout` parameter). + * + * Author: Ivan Orlov + */ +#include +#include +#include +#include + +static int ticked; +static void async_callback(snd_async_handler_t *ahandler) +{ + ticked++; +} + +static char timer_name[64]; +static void bind_to_timer(int device, int subdevice, int timeout) +{ + snd_timer_t *handle; + snd_timer_params_t *params; + snd_async_handler_t *ahandler; + + time_t end; + + sprintf(timer_name, "hw:CLASS=%d,SCLASS=%d,DEV=%d,SUBDEV=%d", + SND_TIMER_CLASS_GLOBAL, SND_TIMER_SCLASS_NONE, + device, subdevice); + + snd_timer_params_alloca(¶ms); + + if (snd_timer_open(&handle, timer_name, SND_TIMER_OPEN_NONBLOCK) < 0) { + perror("Can't open the timer"); + exit(EXIT_FAILURE); + } + + snd_timer_params_set_auto_start(params, 1); + snd_timer_params_set_ticks(params, 1); + if (snd_timer_params(handle, params) < 0) { + perror("Can't set timer params"); + exit(EXIT_FAILURE); + } + + if (snd_async_add_timer_handler(&ahandler, handle, async_callback, NULL) < 0) { + perror("Can't create a handler"); + exit(EXIT_FAILURE); + } + end = time(NULL) + timeout; + if (snd_timer_start(handle) < 0) { + perror("Failed to start the timer"); + exit(EXIT_FAILURE); + } + printf("Timer has started\n"); + while (time(NULL) <= end) { + /* + * Waiting for the timeout to elapse. Can't use sleep here, as it gets + * constantly interrupted by the signal from the timer (SIGIO) + */ + } + snd_timer_stop(handle); + snd_timer_close(handle); +} + +int main(int argc, char *argv[]) +{ + int device, subdevice, timeout; + + if (argc < 4) { + perror("Usage: %s "); + return EXIT_FAILURE; + } + + setlinebuf(stdout); + + device = atoi(argv[1]); + subdevice = atoi(argv[2]); + timeout = atoi(argv[3]); + + bind_to_timer(device, subdevice, timeout); + + printf("Total ticks count: %d\n", ticked); + + return EXIT_SUCCESS; +} diff --git a/tools/testing/selftests/alsa/utimer-test.c b/tools/testing/selftests/alsa/utimer-test.c new file mode 100644 index 000000000000..e4607a51f21b --- /dev/null +++ b/tools/testing/selftests/alsa/utimer-test.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * This test covers the functionality of userspace-driven ALSA timers. Such timers + * are purely virtual (so they don't directly depend on the hardware), and they could be + * created and triggered by userspace applications. + * + * Author: Ivan Orlov + */ +#include "../kselftest_harness.h" +#include +#include +#include +#include +#include +#include +#include + +#define FRAME_RATE 8000 +#define PERIOD_SIZE 4410 +#define TICKS_COUNT 10 +#define TICKS_RECORDING_DELTA 5 +#define TIMER_OUTPUT_BUF_LEN 1024 +#define TIMER_FREQ_SEC 1 +#define RESULT_PREFIX_LEN strlen("Total ticks count: ") + +enum timer_app_event { + TIMER_APP_STARTED, + TIMER_APP_RESULT, + TIMER_NO_EVENT, +}; + +FIXTURE(timer_f) { + int utimer_fd; + struct snd_utimer_info *utimer_info; +}; + +FIXTURE_SETUP(timer_f) { + int timer_dev_fd; + + if (geteuid()) + SKIP(return, "This test needs root to run!"); + + self->utimer_info = calloc(1, sizeof(*self->utimer_info)); + ASSERT_NE(NULL, self->utimer_info); + + self->utimer_info->frame_rate = FRAME_RATE; + self->utimer_info->period_size = PERIOD_SIZE; + + timer_dev_fd = open("/dev/snd/timer", O_RDONLY); + ASSERT_GE(timer_dev_fd, 0); + + self->utimer_fd = ioctl(timer_dev_fd, SNDRV_TIMER_IOCTL_CREATE, self->utimer_info); + ASSERT_GE(self->utimer_fd, 0); + + close(timer_dev_fd); +} + +FIXTURE_TEARDOWN(timer_f) { + close(self->utimer_fd); + free(self->utimer_info); +} + +static void *ticking_func(void *data) +{ + int i; + int *fd = (int *)data; + + for (i = 0; i < TICKS_COUNT; i++) { + /* Well, trigger the timer! */ + ioctl(*fd, SNDRV_TIMER_IOCTL_TRIGGER, NULL); + sleep(TIMER_FREQ_SEC); + } + + return NULL; +} + +static enum timer_app_event parse_timer_output(const char *s) +{ + if (strstr(s, "Timer has started")) + return TIMER_APP_STARTED; + if (strstr(s, "Total ticks count")) + return TIMER_APP_RESULT; + + return TIMER_NO_EVENT; +} + +static int parse_timer_result(const char *s) +{ + char *end; + long d; + + d = strtol(s + RESULT_PREFIX_LEN, &end, 10); + if (end == s + RESULT_PREFIX_LEN) + return -1; + + return d; +} + +/* + * This test triggers the timer and counts ticks at the same time. The amount + * of the timer trigger calls should be equal to the amount of ticks received. + */ +TEST_F(timer_f, utimer) { + char command[64]; + pthread_t ticking_thread; + int total_ticks = 0; + FILE *rfp; + char *buf = malloc(TIMER_OUTPUT_BUF_LEN); + + ASSERT_NE(buf, NULL); + + /* The timeout should be the ticks interval * count of ticks + some delta */ + sprintf(command, "./global-timer %d %d %d", SNDRV_TIMER_GLOBAL_UDRIVEN, + self->utimer_info->id, TICKS_COUNT * TIMER_FREQ_SEC + TICKS_RECORDING_DELTA); + + rfp = popen(command, "r"); + while (fgets(buf, TIMER_OUTPUT_BUF_LEN, rfp)) { + buf[TIMER_OUTPUT_BUF_LEN - 1] = 0; + switch (parse_timer_output(buf)) { + case TIMER_APP_STARTED: + /* global-timer waits for timer to trigger, so start the ticking thread */ + pthread_create(&ticking_thread, NULL, ticking_func, + &self->utimer_fd); + break; + case TIMER_APP_RESULT: + total_ticks = parse_timer_result(buf); + break; + case TIMER_NO_EVENT: + break; + } + } + pthread_join(ticking_thread, NULL); + ASSERT_EQ(total_ticks, TICKS_COUNT); + pclose(rfp); +} + +TEST_HARNESS_MAIN