Introduce userspace-driven ALSA timers
Subsystems (4)
v3 cover Introduce userspace-driven ALSA timers
There are multiple possible timer sources which could be useful for the sound stream synchronization: hrtimers, hardware clocks (e.g. PTP), timer wheels (jiffies). Currently, using one of them to synchronize the audio stream of snd-aloop module would require writing a kernel-space driver which exports an ALSA timer through the snd_timer interface.
However, it is not really convenient for application developers, who may want to define their custom timer sources for audio synchronization.
For instance, we could have a network application which receives frames and sends them to snd-aloop pcm device, and another application listening on the other end of snd-aloop. It makes sense to transfer a new period of data only when certain amount of frames is received through the network, but definitely not when a certain amount of jiffies on a local system elapses. Since all of the devices are purely virtual it won't introduce any glitches and will help the application developers to avoid using sample-rate conversion.
This patch series introduces userspace-driven ALSA timers: virtual timers which are created and controlled from userspace. The timer can be created from the userspace using the new ioctl SNDRV_TIMER_IOCTL_CREATE. After creating a timer, it becomes available for use system-wide, so it can be passed to snd-aloop as a timer source (timer_source parameter would be "-1.SNDRV_TIMER_GLOBAL_UDRIVEN.{timer_id}"). When the userspace app decides to trigger a timer, it calls another ioctl SNDRV_TIMER_IOCTL_TRIGGER on the file descriptor of a timer. It initiates a transfer of a new period of data.
Userspace-driven timers are associated with file descriptors. If the application wishes to destroy the timer, it can simply release the file descriptor of a virtual timer.
I believe introducing new ioctl calls is quite inconvenient (as we have a limited amount of them), but other possible ways of app <-> kernel communication (like virtual FS) seem completely inappropriate for this task (but I'd love to discuss alternative solutions).
This patch series also updates the snd-aloop module so the global timers can be used as a timer_source for it (it allows using userspace-driven timers as timer source).
V1 -> V2:
- Fix some problems found by Christophe Jaillet <[email protected]>
V2 -> V3:
- Add improvements suggested by Takashi Iwai <[email protected]>
Please, find the patch-specific changelog in the following patches.
Ivan Orlov (4):
ALSA: aloop: Allow using global timers
Docs/sound: Add documentation for userspace-driven ALSA timers
ALSA: timer: Introduce virtual userspace-driven timers
selftests: ALSA: Cover userspace-driven timers with test
Documentation/sound/index.rst | 1 + Documentation/sound/utimers.rst | 120 +++++++++++ include/uapi/sound/asound.h | 20 +- sound/core/Kconfig | 10 + sound/core/timer.c | 221 ++++++++++++++++++++ sound/drivers/aloop.c | 2 + tools/testing/selftests/alsa/Makefile | 2 +- tools/testing/selftests/alsa/global-timer.c | 87 ++++++++ tools/testing/selftests/alsa/utimer-test.c | 170 +++++++++++++++ 9 files changed, 631 insertions(+), 2 deletions(-) create mode 100644 Documentation/sound/utimers.rst create mode 100644 tools/testing/selftests/alsa/global-timer.c create mode 100644 tools/testing/selftests/alsa/utimer-test.c
Signature · 1 line
2.34.1
PATCH v3 1/4 ALSA: aloop: Allow using global timers
Allow using global timers as a timer source when card id is equal to -1 in the timer_source parameter.
---
V1 -> V2:
- No changes
V2 -> V3:
- No changes
sound/drivers/aloop.c | 2 ++ 1 file changed, 2 insertions(+)
sound/drivers/aloop.c +2 -0
@@ -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;
Signature · 1 line
2.34.1
PATCH v3 2/4 Docs/sound: Add documentation for userspace-driven ALSA timers
In reply to Ivan OrlovAdd 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
---
V1 -> V2:
- No changes
V2 -> V3:
- No changes
Documentation/sound/index.rst | 1 + Documentation/sound/utimers.rst | 120 ++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 Documentation/sound/utimers.rst
Documentation/sound/index.rst +1 -0
@@ -13,6 +13,7 @@ Sound Subsystem Documentation alsa-configuration hd-audio/index cards/index + utimers .. only:: subproject and html
Documentation/sound/utimers.rst new file +120 -0
120 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 3/4 ALSA: timer: Introduce virtual userspace-driven timers
In reply to Ivan OrlovImplement 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.
---
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 V2 -> V3:
- Use __u64 instead of snd_pcm_uframes_t in snd_utimer_info struct
- Add 16 reserved bytes to snd_utimer_info struct just in case we decide to add some other fields to this struct
- Bump the timer protocol version in SNDRV_TIMER_VERSION to 2.0.8
- Make the 'snd_utimer_ids' variable static
- Add sanity checks to the 'snd_utimer_create' function: 0-check for period size and frame rate, overflow and 0- checks for resolution
- Use automatic cleanup in 'snd_utimer_ioctl_create'
- Return -ENOTTY instead of -EINVAL from ioctl if userspace-driven timers are disabled
include/uapi/sound/asound.h | 20 +++- sound/core/Kconfig | 10 ++ sound/core/timer.c | 221 ++++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+), 1 deletion(-)
include/uapi/sound/asound.h +19 -1
@@ -869,7 +869,7 @@ struct snd_ump_block_info {* Timer section - /dev/snd/timer */ -#define SNDRV_TIMER_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 7) +#define SNDRV_TIMER_VERSION SNDRV_PROTOCOL_VERSION(2, 0, 8)enum {SNDRV_TIMER_CLASS_NONE =-1 ,
@@ -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,21 @@ 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. +*/ +__u64 frame_rate; +__u64 period_size; +unsigned int id; +unsigned char reserved[16 ]; +}; +#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 +1006,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
sound/core/Kconfig +10 -0
@@ -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
sound/core/timer.c +221 -0
245 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 4/4 selftests: ALSA: Cover userspace-driven timers with test
In reply to Ivan OrlovAdd 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 <alsa/asoundlib.h> and <sound/asound.h> 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.
---
V1 -> V2:
- Return NULL in the pthreaded function (ticking_func)
- Process TIMER_NO_EVENT enum in the timer app output processing loop V2 -> V3:
- Add new test case to cover invalid period sizes and frame rates for the userspace-driven timers (to test the sanity checks in snd_utimer_create)
tools/testing/selftests/alsa/Makefile | 2 +- tools/testing/selftests/alsa/global-timer.c | 87 ++++++++++ tools/testing/selftests/alsa/utimer-test.c | 170 ++++++++++++++++++++ 3 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/alsa/global-timer.c create mode 100644 tools/testing/selftests/alsa/utimer-test.c
tools/testing/selftests/alsa/Makefile +1 -1
@@ -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
tools/testing/selftests/alsa/global-timer.c new file +87 -0
87 lines not on this page · raw message
tools/testing/selftests/alsa/utimer-test.c new file +170 -0
170 lines not on this page · raw message
Signature · 1 line
2.34.1
Mark Brown
maintainerRe: [PATCH v3 4/4] selftests: ALSA: Cover userspace-driven timers with test
tools/testing/selftests/alsa/Makefile LDLIBS+=-lpthread · line 15
Quoted 2 lines · Ivan Orlov
tools/testing/selftests/alsa/Makefile +1
-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
This is adding the timer timer tests as standard kselftests to be run by the wrapper script...
tools/testing/selftests/alsa/global-timer.c line 74
Quoted 12 lines
index 000000000000..c15ec0ba851a
tools/testing/selftests/alsa/global-timer.c new file +8
-0+int main (int argc,char *argv[]) +{ +int device, subdevice, timeout; + +if (argc <4 ) { +perror ("Usage: %s <device> <subdevice> <timeout>" ); +return EXIT_FAILURE ; + }
...but this requires specific arguments to be run which the kselftest runner won't supply. I'd expect it to be a good default to enumerate and test every possible device and generate a test for each. However it looks like this is really intended not as a standalone test but rather as something run from within utimer-test, in that case it should be a TEST_GEN_PROGS_EXTENDED.
Jaroslav Kysela
maintainerRe: [PATCH v3 3/4] ALSA: timer: Introduce virtual userspace-driven timers
In reply to Ivan OrlovQuoted 2 lines · Ivan Orlov
Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.
...
include/uapi/sound/asound.h struct snd_timer_status { · line 988
Quoted 7 lines
include/uapi/sound/asound.h +7
-0+struct snd_utimer_info { +/* +* To pretend being a normal timer, we need to know the frame rate and +* the period size in frames. +*/ +__u64 frame_rate; +__u64 period_size;
There should be just one timer resolution in ns member (like in struct snd_timer_ginfo - not frame/period members here - it's too specific). The resolution can be calculated in the user space from the rate and period size.
Also naming - the timer API uses snd_timer prefix for structures, thus snd_timer_uinfo should be more appropriate.
Jaroslav
Signature · 2 lines
Jaroslav Kysela <[email protected]> Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
Re: [PATCH v3 4/4] selftests: ALSA: Cover userspace-driven timers with test
In reply to Mark BrownQuoted 12 lines · Mark Brown
tools/testing/selftests/alsa/Makefile LDLIBS+=-lpthread · line 15
Quoted 2 lines · Ivan Orlov
tools/testing/selftests/alsa/Makefile +1
-1-TEST_GEN_PROGS := mixer-test pcm-test test-pcmtest-driver +TEST_GEN_PROGS := mixer-test pcm-test utimer-test test-pcmtest-driver global-timerThis is adding the timer timer tests as standard kselftests to be run by the wrapper script...
Quoted 3 lines
index 000000000000..c15ec0ba851a
tools/testing/selftests/alsa/global-timer.c new file +0
-0
Quoted 15 lines
tools/testing/selftests/alsa/global-timer.c line 74
Quoted 8 lines
tools/testing/selftests/alsa/global-timer.c new file +8
-0+int main (int argc,char *argv[]) +{ +int device, subdevice, timeout; + +if (argc <4 ) { +perror ("Usage: %s <device> <subdevice> <timeout>" ); +return EXIT_FAILURE ; + }...but this requires specific arguments to be run which the kselftest runner won't supply. I'd expect it to be a good default to enumerate and test every possible device and generate a test for each. However it looks like this is really intended not as a standalone test but rather as something run from within utimer-test, in that case it should be a
TEST_GEN_PROGS_EXTENDED.
Hi Mark,
Yes, the 'global-timer' application is not a standalone test and it should be ran by 'utimer-test'. I had to extract the timer-binding functionality into a different application as we can't have 'sound/asound.h' and 'alsa/asoundlib.h' in single source due to some declarations overlap problems.
I'll move the 'global-timer' tool into the TEST_GEN_PROGS_EXTENDED list.
Thank you so much for the review!
Signature · 2 lines
Kind regards, Ivan Orlov
Re: [PATCH v3 3/4] ALSA: timer: Introduce virtual userspace-driven timers
In reply to Jaroslav KyselaQuoted 6 lines · Jaroslav Kysela
Quoted 2 lines · Ivan Orlov
Implement two ioctl calls in order to support virtual userspace-driven ALSA timers.
...
Hi Jaroslav,
Quoted 11 lines
Quoted 8 lines
+struct
snd_utimer_info{
+ /*
+ * To pretend being a normal timer, we need to know the frame rate and
+ * the period size in frames.
+ */
+__u64frame_rate;
+__u64period_size;There should be just one timer resolution in ns member (like in struct
snd_timer_ginfo- not frame/period members here - it's too specific). The resolution can be calculated in the user space from the rate and period size.
Ah, yes, I agree... Also, it should help us to avoid complex calculations and sanity checks in the kernel space. I'll replace these two fields with 'resolution' field in V4, thanks!
Quoted 2 lines
Also naming - the timer API uses
snd_timerprefix for structures, thussnd_timer_uinfoshould be more appropriate.
Alright, I'll rename the structure.
Thank you so much for the review!
Signature · 2 lines
Kind regards, Ivan Orlov
kernel test robot
build botRe: [PATCH v3 3/4] ALSA: timer: Introduce virtual userspace-driven timers
In reply to Ivan Orlovkernel test robot noticed the following build errors:
Hi Ivan,
kernel test robot noticed the following build errors:
[auto build test ERROR on tiwai-sound/for-next]
[also build test ERROR on tiwai-sound/for-linus linus/master v6.11-rc2 next-20240808] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Ivan-Orlov/ALSA-aloop-Allow-using-global-timers/20240806-210332 base: https://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound.git for-next patch link: https://lore.kernel.org/r/20240806125243.449959-4-ivan.orlov0322%40gmail.com patch subject: [PATCH v3 3/4] ALSA: timer: Introduce virtual userspace-driven timers config: arm-randconfig-r064-20240807 (https://download.01.org/0day-ci/archive/20240808/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240808/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_submit_wmt_recv_urb':
drivers/bluetooth/btmtk.c:531:(.text+0x6c2): undefined reference to `usb_alloc_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:537:(.text+0x6e6): undefined reference to `usb_free_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:561:(.text+0x750): undefined reference to `usb_anchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:562:(.text+0x75a): undefined reference to `usb_submit_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:567:(.text+0x796): undefined reference to `usb_unanchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:570:(.text+0x7a0): undefined reference to `usb_free_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_submit_intr_urb':
drivers/bluetooth/btmtk.c:1174:(.text+0x80e): undefined reference to `usb_alloc_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1181:(.text+0x83a): undefined reference to `usb_free_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1195:(.text+0x8c4): undefined reference to `usb_anchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1197:(.text+0x8cc): undefined reference to `usb_submit_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1202:(.text+0x908): undefined reference to `usb_unanchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1205:(.text+0x912): undefined reference to `usb_free_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_suspend':
drivers/bluetooth/btmtk.c:1265:(.text+0x988): undefined reference to `usb_kill_anchored_urbs'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_intr_complete':
drivers/bluetooth/btmtk.c:1145:(.text+0xbf2): undefined reference to `usb_anchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1147:(.text+0xbfc): undefined reference to `usb_submit_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:1157:(.text+0xc48): undefined reference to `usb_unanchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `__set_mtk_intr_interface':
drivers/bluetooth/btmtk.c:991:(.text+0xcf4): undefined reference to `usb_set_interface'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_isointf_init':
drivers/bluetooth/btmtk.c:1224:(.text+0xd30): undefined reference to `usb_kill_anchored_urbs'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_hci_wmt_sync':
drivers/bluetooth/btmtk.c:610:(.text+0xf26): undefined reference to `usb_autopm_get_interface'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:618:(.text+0xf54): undefined reference to `usb_autopm_put_interface'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:625:(.text+0xf7e): undefined reference to `usb_autopm_put_interface'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_reg_read':
drivers/bluetooth/btmtk.c:790:(.text+0x1248): undefined reference to `usb_control_msg'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_uhw_reg_write':
drivers/bluetooth/btmtk.c:738:(.text+0x12e0): undefined reference to `usb_control_msg'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_uhw_reg_read':
drivers/bluetooth/btmtk.c:761:(.text+0x1374): undefined reference to `usb_control_msg'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_recv_acl':
drivers/bluetooth/btmtk.c:946:(.text+0x1e06): undefined reference to `usb_disable_autosuspend'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `btmtk_usb_wmt_recv':
drivers/bluetooth/btmtk.c:508:(.text+0x1e72): undefined reference to `usb_anchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:509:(.text+0x1e7c): undefined reference to `usb_submit_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.c:518:(.text+0x1ec0): undefined reference to `usb_unanchor_urb'
arm-linux-gnueabi-ld: drivers/bluetooth/btmtk.o: in function `alloc_mtk_intr_urb':
drivers/bluetooth/btmtk.c:1037:(.text+0x1fc8): undefined reference to `usb_alloc_urb'
arm-linux-gnueabi-ld: sound/core/timer.o: in function `snd_utimer_create':
>> sound/core/timer.c:2127:(.text+0x255c): undefined reference to `__aeabi_uldivmod'
vim +2127 sound/core/timer.c
2112
2113 static int snd_utimer_create(struct snd_utimer_info *utimer_info,
2114 struct snd_utimer **r_utimer)
2115 {
2116 struct snd_utimer *utimer;
2117 struct snd_timer *timer;
2118 struct snd_timer_id tid;
2119 int utimer_id;
2120 int err = 0;
2121 u64 resolution;
2122
2123 if (!utimer_info || utimer_info->frame_rate == 0 || utimer_info->period_size == 0)
2124 return -EINVAL;
2125
2126 /* Let's check that we won't get an overflow in the resolution */
> 2127 if (NANO / utimer_info->frame_rate > U64_MAX / utimer_info->period_size)
2128 return -EINVAL;
2129
2130 resolution = NANO / utimer_info->frame_rate * utimer_info->period_size;
2131
2132 if (resolution == 0)
2133 return -EINVAL;
2134
2135 utimer = kzalloc(sizeof(*utimer), GFP_KERNEL);
2136 if (!utimer)
2137 return -ENOMEM;
2138
2139 /* We hold the ioctl lock here so we won't get a race condition when allocating id */
2140 utimer_id = snd_utimer_take_id();
2141 if (utimer_id < 0) {
2142 err = utimer_id;
2143 goto err_take_id;
2144 }
2145
2146 utimer->name = kasprintf(GFP_KERNEL, "snd-utimer%d", utimer_id);
2147 if (!utimer->name) {
2148 err = -ENOMEM;
2149 goto err_get_name;
2150 }
2151
2152 utimer->id = utimer_id;
2153
2154 tid.dev_sclass = SNDRV_TIMER_SCLASS_APPLICATION;
2155 tid.dev_class = SNDRV_TIMER_CLASS_GLOBAL;
2156 tid.card = -1;
2157 tid.device = SNDRV_TIMER_GLOBAL_UDRIVEN;
2158 tid.subdevice = utimer_id;
2159
2160 err = snd_timer_new(NULL, utimer->name, &tid, &timer);
2161 if (err < 0) {
2162 pr_err("Can't create userspace-driven timer\n");
2163 goto err_timer_new;
2164 }
2165
2166 timer->module = THIS_MODULE;
2167 timer->hw = timer_hw;
2168 timer->hw.resolution = resolution;
2169 timer->hw.ticks = 1;
2170 timer->max_instances = MAX_SLAVE_INSTANCES;
2171
2172 utimer->timer = timer;
2173
2174 err = snd_timer_global_register(timer);
2175 if (err < 0) {
2176 pr_err("Can't register a userspace-driven timer\n");
2177 goto err_timer_reg;
2178 }
2179
2180 *r_utimer = utimer;
2181 return 0;
2182
2183 err_timer_reg:
2184 snd_timer_free(timer);
2185 err_timer_new:
2186 kfree(utimer->name);
2187 err_get_name:
2188 snd_utimer_put_id(utimer);
2189 err_take_id:
2190 kfree(utimer);
2191
2192 return err;
2193 }
2194
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki