Commit graph

78 commits

Author SHA1 Message Date
Wim Taymans
1ae4374ccf Fix compilation with -Werror=float-conversion
Better make the conversions explicit so that we don't get any surprises.

Fixes #4065
2024-06-18 12:17:56 +02:00
Alper Nebi Yasak
9b9ce21b1d acp: Don't sync hardware volumes while UCM device is disabled
The hardware controls we track for volume and mute state can change as
part of enabling or disabling the UCM device. This then triggers a
re-sync of our state with the hardware controls, which can e.g. set our
volume to an unwanted value as the result of enabling the device, or set
a hardware control to a wrong value based on our volume while disabling
the device. So these UCM-triggered changes to enable/disable the device
will at best show up on user interfaces and cause confusion, but maybe
even will push the hardware into an unexpected state.

The volume and mute state we set from user interfaces only make sense
when the devices are enabled. They should not be kept in sync with
hardware for inactive UCM devices [1]. Skip the callbacks for reading
and changing volume and mute state if the UCM device is disabled. This
way, the volume/mute controls for sinks/sources are essentially detached
from the hardware controls until the UCM device is re-enabled.

[1] https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/772#note_1872757

Link: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/772
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
0bdad320cd alsa-ucm: Replace port device UCM context assertion with an error
The pa_alsa_ucm_set_port() function is passed both a mapping context and
a device port, and both of these refer to their respective UCM device.
While switching over to having one port per mapping per UCM device, I
expected both of these to be the same device struct, so added an assert
checking so.

This assertion gets triggered when we have multiple UCM verbs declaring
the same UCM device name. The root cause here is that the ports' UCM
device references are set once while creating the ports for the card, so
they happen to be those of a specific verb and may not match those from
a different UCM verb's profiles' mappings.

Solving the root cause necessitates a larger refactor. What we actually
assume here is that name of the UCM device is same for both the port and
the UCM context, which ends up always true in practice. For now, replace
the assert with a check and error.

Link: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/802
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
5b3dea6319 alsa-ucm: Check UCM verb before working with device status
Some versions of the ALSA libraries run into a segmentation fault when
we query a UCM device/modifier status without first setting a UCM verb.
It's not a reasonable thing to do anyway, so check for this case and
return an error. Also do the check in other helpers.

Link: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/801
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
a7e9f79cd0 alsa-ucm: Fix more instances of profile-verb conflation
The ucm_get_device_property() function adds to each UCM device's
playback_volumes (or capture_volumes) hash map an associated volume
mixer keyed with the UCM verb. These key-value pairs are then iterated
over in various places which assume the key is a profile name. This
assumption is no longer true since we can generate multiple profiles to
use conflicting devices.

A previous commit 45278904167f ("alsa-ucm: Stop conflating profile name
with UCM verb name") fixes some instances of this assumption, but misses
the relation explained above. Fix more instances of misleading
"profile"s where the UCM verb name is actually meant.

Link: 7005eafee3
Fixes: 57295beee1a6 ("alsa-ucm: Stop conflating profile name with UCM verb name")
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
d2046adcd1 alsa-ucm: Consider devices using the same PCM as incompatible
Although it's a valid UCM configuration to have multiple devices using
the same PlaybackPCM or CapturePCM, it's unclear how PulseAudio should
handle the cases where multiple of these devices are enabled. Some
options I can think of are:

- Merge all devices sharing the same PCM into the same mapping, open
  only one PCM substream for this mapping, and add 'combination ports'
  that enable combinations of the devices. This has been the case until
  recently, although the combination port logic was broken. A problem
  with this is that we can't independently control device volumes. We
  most likely cannot use hardware volumes either.

- Have one mapping for each device in the same profile, and open one PCM
  substream for each mapping. This is the current state, and it fails
  when there are fewer substreams than devices. Otherwise it works, but
  it's still confusing, as sound directed to a device-specific mapping
  might end up playing at multiple devices.

- Make multiple profiles each with combinations of upto-substream-count
  devices, and have one mapping/substream per device. This still causes
  the confusion mentioned above. And it's likely that the substream
  count will almost always be one, where this case degenerates into the
  last one.

- Have one mapping for each device in the same profile, but open only
  one PCM substream. I assume this is possible with software mixing, but
  it is still confusing like the above, and probably less performant.

- Generate multiple profiles each with one of the shared-PCM devices,
  again with one mapping/substream for that one device. The trade-off
  with this is that we can't use multiple of these devices at the same
  time. However, this doesn't have the output device confusion,
  combination port's volume problems, or the substream count limitation.

This patch takes a short-cut to achieve the last option, by considering
shared-PCM devices implicitly conflicting with each other.

Link: 45509ff9bc
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
cff0ef13c1 alsa-ucm: Disable old modifiers when switching profiles of same verb
While switching profiles of the same UCM verb, existing code first
disables devices that are only on the first profile to avoid conflicts.
However, it only disables devices, not modifiers. Even worse, modifiers
which have PlaybackPCM/CapturePCM are incorrectly treated as devices and
result in a segmentation fault.

Check what we are disabling, and call the appropriate disable function
for both devices and modifiers. Modifiers are disabled before devices,
because _dismod calls fail when the modifier's supported devices are
disabled.

Link: b74269016c
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
905241c4a1 alsa-ucm: Add enable, disable, status helpers for modifiers
These are mostly the same as the device helpers added in c83b34516929
("alsa-ucm: Add enable, disable, status helpers for devices"), but for
modifiers instead.

Link: b382a00f8b
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
d99e0586b0 alsa-ucm: Create multiple profiles per verb for conflicting devices
Right now we try to add all UCM devices of a verb to a single profile.
But if some devices using different PCMs are configured as conflicting
with one another, we will only be able to utilize one of them, chosen
seemingly based on the order in the UCM config file.

This is not a problem with conflicting devices sharing a PCM, as they
are assigned to the same mapping and the ports mechanism only enables
one of them to be active at a time.

To utilize all devices in a UCM verb even when there are conflicting
devices using different PCMs, calculate subsets of devices which
can be simultaneously used and create a profile for each such set.

Link: 5dd411190d
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
63a44038d2 alsa-ucm: Disable old devices when switching profiles of same verb
While switching profiles, it was enough to switch UCM verbs since that
disables all enabled UCM devices and every profile had a distinct verb.
However, switching to the current verb does not disable any devices.

To support multiple profiles for a verb we need to explicitly disable
the old profile's devices, since they might be conflicting with the new
profile's devices and will prevent them from being enabled. Compare both
profiles' mappings, and disable the devices not in the new mappings.

Link: e49da7bcd6
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
6b46909083 alsa-ucm: Make mapping UCM contexts have only one modifier
After previous patches, we should be generating no combination ports, so
we don't need to store multiple modifiers per mapping. Simplify the code
based on this.

Link: 4821a0569e
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
bc42a617a4 alsa-ucm: Make mapping UCM contexts have only one device
After previous patches, we should be generating no combination ports, so
we don't need to store multiple devices per mapping. Simplify the code
based on this.

Link: 084d70a1e2
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
[Wim: Apply to acp add_profiles() and acp_card_set_profile()]
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
b8361cc568 alsa-ucm: Make ports store only one device
After previous patches, we should be generating no combination ports, so
we don't need to store multiple devices per port. Simplify the code
based on this.

Link: bf1708213b
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
0fc80db7fe alsa-ucm: Remove combination port generation logic
A previous commit makes mapping names depend on the UCM device name.
Since UCM device names are unique, this means a mapping will at most
have one port and thus no combination ports can be generated.

This removes the dead code in the pa_alsa_ucm_add_ports_combination()
function, unrolls the remaining code in its helper functions that it
used, and renames it to pa_alsa_ucm_add_port() to signal that it no
longer generates combinations.

Link: 0789a8fb76
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
[Wim: Apply to acp add_profiles(), acp_card_set_profile(), compat.h]
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
833c86d35b alsa-ucm: Make one input/output mapping per UCM device
PulseAudio combines UCM devices that have the same PlaybackPCM or
CapturePCM value into a single mapping with multiple ports. It also
creates ports in the same mapping for each valid combination of those
UCM devices.

Since mappings are the things we put in profiles, we can put in a
profile either all devices of a joint mapping or none of them. This
causes some complications with device conflicts. For example, a
different UCM device might be marked as conflicting with some (but not
all) of the devices in a joint mapping. In this case we can do one of
three things:

- Include all devices in one profile, and hope the conflicting device
  isn't chosen as the mapping's active port. We shouldn't do this as it
  puts conflicting devices in the same profile.

- Make one profile with the joint group, and one with the other device.
  This is somewhat acceptable as we have no conflicts, but we sacrifice
  some compatible combinations of devices.

- Do not group the devices into the same mapping, and make one profile
  for each compatible combination of devices. This appears to be the
  best option, one where we can always have the maximum number of
  working devices.

This patch chooses the third option and makes one input and/or output
mapping per UCM device, by using UCM device names instead of PCM device
strings in the mapping names.

Link: a50330a4a5
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
d93e96b666 alsa-ucm: Split out helpers for device set name, description, priority
Combination port logic calculates some useful properties for device
groups that we could reuse while generating multiple profiles to support
conflicting devices. Split them into their own functions.

Link: e8e2f4320c
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
943967b826 alsa-ucm: Fix device conformance check
Right now this check is rejecting devices whose UCM config specifies
neither a conflicting device nor a supported device list, and accepting
devices which specify both. However, a device without neither list is
actually unrestricted, and a device with both lists is a configuration
error. Fix the check to accept the former.

Furthermore, this is missing another case where an already selected
device might have a supported devices list that doesn't have the
candidate device. Make this function also check against that, and also
make it accept devices already in the set.

Link: 5245117781
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
d7591edc25 alsa-ucm: Rewrite conformant device group generation with idxsets
The existing code meant to generate device groups for combination ports
is tightly coupled to port creation. Similar functionality would be
useful to generate nonconflicting device groups for multiple profiles as
well, so this tries to rewrite it into a more reusable state.

Several things (e.g devices, mapping contexts) use idxsets to store a
device selection. This also switches this conformance check and device
group generation to using idxsets to make it easier to work with those,
with the eventual aim to unify device group representations.

Also try to adjust users of these functions to use idxsets these will
need/return, without causing too much interference.

Link: 0f26022843
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
a208716724 alsa-ucm: Split profile creation into verb and profile parts
To support having multiple profiles per UCM verb, split the profile
creation into two parts based on whether they should run once for each
verb or for each profile (maybe multiple times per verb).

Link: 638574c0b7
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
7883da6f52 alsa-ucm: Make mapping creation independent from indvidual profiles
The ucm_create_mapping() function is not idempotent. It looks like it
was meant to be called once per device for the devices of a UCM verb
and takes a profile argument simply because a verb has generated a
single profile so far.

Make sure creating mappings per device and adding those mappings to the
profiles happens as separate steps to make it easier to split UCM verbs
and profiles as concepts.

Link: aa5ced3887
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
07a347d3b6 alsa-ucm: Stop conflating profile name with UCM verb name
So far each profile had the exact name as their associated UCM verb,
which caused the one to be used where the other should have been.
Explicitly get and use the verb name where that was intended, and make
sure things about profiles aren't named after verbs.

Link: 4527890416
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
f4c66de9f0 alsa-ucm: Let profiles know their associated UCM verb
Currently each UCM verb generates one profile named the same as the
verb, meaning it's trivial to know which verb the profile belongs to.
This will be slightly harder to do when we generate multiple profiles
per UCM verb (e.g. to make use of conflicting devices).

It would still be possible to parse the profile name to get the UCM
verb, but instead let's keep track of the struct instance representing
the profile's associated verb. This also lets us remove a block of code
searching for the verb by its name.

Co-authored-by: Jaroslav Kysela <perex@perex.cz>
[Alper: Reused Jaroslav's UCM profile context changes for UCM verb
instead of combined devices.]
Link: 9fc7064b9a
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
a5d87a43df alsa-ucm: Set profiles by their struct instance, not their name
While switching profiles, it's possible that we will want to do more
work besides switching UCM verbs. The alsa-card module already has our
profiles as structs, but passes in only the names instead of the entire
struct. Make things work with the struct instead, so we can add other
things (like a UCM context) to it and use those here.

Co-authored-by: Tanu Kaskinen <tanuk@iki.fi>
[Alper: Split into its own commit and integrated Tanu's snippet.]
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
Link: 880ff393f1
[Wim: Apply to acp_card_set_profile() as well]
Co-authored-by: Wim Taymans <wtaymans@redhat.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
396382ebb2 alsa-ucm: Add enable, disable, status helpers for devices
Right now manipulating device status is done inline once while setting a
port. However, we will need to reuse this code to disable conflicting
devices of a device we want to enable. Split it into enable and disable
helper functions.

There is another issue with the device enable logic, where trying to
disabling an already disabled device sometimes fails. To avoid that,
implement a status helper and check if the device we want to enable is
already enabled/disabled before trying to do so.

Link: 9b06e8fef4
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
3ef2e874b2 alsa-ucm: Make modifiers track conflicting/supported devices as idxsets
Modifiers currently keep their conflicting and supported devices's
names, and these names are resolved to devices every time we need to use
them. Instead, resolve these device names while creating the modifier
struct and keep track of the resulting device structs in idxsets, same
as how device structs keep track of their support relations.

Link: 9b06e8fef4
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Alper Nebi Yasak
36af5f074a alsa-ucm: Always create device conflicting/supported device idxsets
This is intended to make the current and upcoming code a bit clearer, as
we won't need to constantly check for the existence of these idxsets
before using or operating on them.

Link: d8c89de24d
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-12-04 15:34:48 +00:00
Hector Martin
9c868740c2 alsa: ucm: Fix spurious mixer path removal on initial profile set
The code that removes the mixer path if probing fails can be called in
the path that sets a non-off device profile on hotplug *before*
card->active_profile is updated, which results in spuriously removing
the mixer path. By this point, context->ucm->active_verb would be set
to the same as the profile name, so we can use that instead to avoid
the issue.

On Apple Silicon machines with the UCM profiles in the Asahi Linux repo,
this manifests as the headphones jack having hardware volume controls
*only* if PA is started with headphones connected and until they are
disconnected. Hotplugs end up triggering the bad codepath, and it falls
back to software volume (which is particularly a problem when the
hardware volume happens to be very low or 0 at that point).

Signed-off-by: Hector Martin <marcan@marcan.st>
Link: 3e038c6d88
2023-12-04 15:34:48 +00:00
Igor V. Kovalenko
082680d8a2 alsa-util: Perform format and rate detection before setting HW params
Perform detection of supported sample format and rates just after device is
opened, before `snd_pcm_hw_params()` is called for the first time. This fixes a
problem where device restricts available sample rates after HW params are set
preventing sample rate detection (seen with UAC2 devices and kernel 6.1.9)

Bug: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/issues/1414
Bug: https://github.com/alsa-project/alsa-lib/issues/119
Link: aed52c507f
[Wim: Apply to acp add_pro_profile(), enable pa_alsa_supported_*()]
2023-12-04 15:34:48 +00:00
Jaroslav Kysela
2c3e8d90d3 alsa-ucm: remove extra space from the device.intended_roles property
Before:
  device.intended_roles = " voice"

After:
  device.intended_roles = "voice"

Link: eb870fcba9
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-12-04 15:34:48 +00:00
Guido Günther
88bb0bd7cc alsa: Allow to augment ucm port properties
Since UCM doesn't perform the path lookups of alsa-mixer all UCM devices
fall back to the card's properties for e.g. icons and hence usually show
the `audio-card` icon for all ports giving a confusing UI to users as
all ports show the same icon.

As ucm names are pretty standard augment some common port names with
icons.
2023-06-26 19:39:31 +00:00
Wim Taymans
a66d5b90f9 Revert "alsa: UCM conflicting devices changes"
If needs some more work

This reverts commit aae770f18b.
This reverts commit 2216956e05.
This reverts commit 1d3c4501ef.
This reverts commit c7ca024607.
This reverts commit 8e4a5f5d5e.
This reverts commit 8b20675ad1.
This reverts commit ebe6f8b51a.
This reverts commit 4b97da1277.
This reverts commit 7c46a656c7.
This reverts commit 96ed03e1fc.
This reverts commit 43770c533c.
This reverts commit ccb2fce054.
This reverts commit 93d0ddc8b3.
This reverts commit 815e8c8fa0.
This reverts commit 97b99a0f7c.
This reverts commit 7e4d87402b.
This reverts commit 168b3ee22c.
This reverts commit 5128cdba31.
This reverts commit 31cde774c5.
This reverts commit b02c8ba153.
This reverts commit 68ac72d098.
This reverts commit 96cfc9bc1a.
This reverts commit e2d642a20b.
This reverts commit 80fc80c343.
This reverts commit 3a68905c7c.
This reverts commit 7e64680a65.
This reverts commit 8d1e38a281.
This reverts commit 591188195c.
This reverts commit 973fe0cb79.
This reverts commit 4d08c9ad60.
This reverts commit 3dce92e580.
This reverts commit 6b5e232c5c.
This reverts commit b1286c62c9.
2023-04-12 13:05:08 +02:00
Wim Taymans
aae770f18b alsa: avoid assert
We're doing something different than pulseaudio when setting up the
ports.
2023-03-21 12:29:37 +01:00
Wim Taymans
2216956e05 alsa: fixup after merge 2023-03-21 10:07:39 +01:00
Wim Taymans
c7ca024607 alsa: ucm: Fix spurious mixer path removal on initial profile set
The code that removes the mixer path if probing fails can be called in
the path that sets a non-off device profile on hotplug *before*
card->active_profile is updated, which results in spuriously removing
the mixer path. By this point, context->ucm->active_verb would be set
to the same as the profile name, so we can use that instead to avoid
the issue.

On Apple Silicon machines with the UCM profiles in the Asahi Linux repo,
this manifests as the headphones jack having hardware volume controls
*only* if PA is started with headphones connected and until they are
disconnected. Hotplugs end up triggering the bad codepath, and it falls
back to software volume (which is particularly a problem when the
hardware volume happens to be very low or 0 at that point).

Fixes: a9cc1373e2a7 ("alsa: ucm - update the mixer path also after volume probe")
Signed-off-by: Hector Martin <marcan@marcan.st>
2023-03-20 18:35:15 +01:00
Wim Taymans
8e4a5f5d5e alsa-ucm: Fix more instances of profile-verb conflation
The ucm_get_device_property() function adds to each UCM device's
playback_volumes (or capture_volumes) hash map an associated volume
mixer keyed with the UCM verb. These key-value pairs are then iterated
over in various places which assume the key is a profile name. This
assumption is no longer true since we can generate multiple profiles to
use conflicting devices.

A previous commit 45278904167f ("alsa-ucm: Stop conflating profile name
with UCM verb name") fixes some instances of this assumption, but misses
the relation explained above. Fix more instances of misleading
"profile"s where the UCM verb name is actually meant.

Fixes: 45278904167f ("alsa-ucm: Stop conflating profile name with UCM verb name")
Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 18:33:36 +01:00
Wim Taymans
8b20675ad1 alsa-ucm: Consider devices using the same PCM as incompatible
Although it's a valid UCM configuration to have multiple devices using
the same PlaybackPCM or CapturePCM, it's unclear how PulseAudio should
handle the cases where multiple of these devices are enabled. Some
options I can think of are:

- Merge all devices sharing the same PCM into the same mapping, open
  only one PCM substream for this mapping, and add 'combination ports'
  that enable combinations of the devices. This has been the case until
  recently, although the combination port logic was broken. A problem
  with this is that we can't independently control device volumes. We
  most likely cannot use hardware volumes either.

- Have one mapping for each device in the same profile, and open one PCM
  substream for each mapping. This is the current state, and it fails
  when there are fewer substreams than devices. Otherwise it works, but
  it's still confusing, as sound directed to a device-specific mapping
  might end up playing at multiple devices.

- Make multiple profiles each with combinations of upto-substream-count
  devices, and have one mapping/substream per device. This still causes
  the confusion mentioned above. And it's likely that the substream
  count will almost always be one, where this case degenerates into the
  last one.

- Have one mapping for each device in the same profile, but open only
  one PCM substream. I assume this is possible with software mixing, but
  it is still confusing like the above, and probably less performant.

- Generate multiple profiles each with one of the shared-PCM devices,
  again with one mapping/substream for that one device. The trade-off
  with this is that we can't use multiple of these devices at the same
  time. However, this doesn't have the output device confusion,
  combination port's volume problems, or the substream count limitation.

This patch takes a short-cut to achieve the last option, by considering
shared-PCM devices implicitly conflicting with each other.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 18:29:51 +01:00
Wim Taymans
ebe6f8b51a alsa-ucm: Disable old modifiers when switching profiles of same verb
While switching profiles of the same UCM verb, existing code first
disables devices that are only on the first profile to avoid conflicts.
However, it only disables devices, not modifiers. Even worse, modifiers
which have PlaybackPCM/CapturePCM are incorrectly treated as devices and
result in a segmentation fault.

Check what we are disabling, and call the appropriate disable function
for both devices and modifiers. Modifiers are disabled before devices,
because _dismod calls fail when the modifier's supported devices are
disabled.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 18:28:10 +01:00
Wim Taymans
4b97da1277 alsa-ucm: Add enable, disable, status helpers for modifiers
These are mostly the same as the device helpers added in c83b34516929
("alsa-ucm: Add enable, disable, status helpers for devices"), but for
modifiers instead.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 18:26:46 +01:00
Wim Taymans
96ed03e1fc alsa-util: Perform format and rate detection before setting HW params
Perform detection of supported sample format and rates just after device is
opened, before `snd_pcm_hw_params()` is called for the first time. This fixes a
problem where device restricts available sample rates after HW params are set
preventing sample rate detection (seen with UAC2 devices and kernel 6.1.9)
2023-03-20 18:22:09 +01:00
Wim Taymans
97b99a0f7c alsa-ucm: remove extra space from the device.intended_roles property
Before:
  device.intended_roles = " voice"

After:
  device.intended_roles = "voice"

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2023-03-20 17:40:33 +01:00
Wim Taymans
7e4d87402b alsa-ucm: Create multiple profiles per verb for conflicting devices
Right now we try to add all UCM devices of a verb to a single profile.
But if some devices using different PCMs are configured as conflicting
with one another, we will only be able to utilize one of them, chosen
seemingly based on the order in the UCM config file.

This is not a problem with conflicting devices sharing a PCM, as they
are assigned to the same mapping and the ports mechanism only enables
one of them to be active at a time.

To utilize all devices in a UCM verb even when there are conflicting
devices using different PCMs, calculate subsets of devices which
can be simultaneously used and create a profile for each such set.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 17:38:44 +01:00
Wim Taymans
168b3ee22c alsa-ucm: Disable old devices when switching profiles of same verb
While switching profiles, it was enough to switch UCM verbs since that
disables all enabled UCM devices and every profile had a distinct verb.
However, switching to the current verb does not disable any devices.

To support multiple profiles for a verb we need to explicitly disable
the old profile's devices, since they might be conflicting with the new
profile's devices and will prevent them from being enabled. Compare both
profiles' mappings, and disable the devices not in the new mappings.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 17:33:10 +01:00
Wim Taymans
5128cdba31 alsa-ucm: Make mapping UCM contexts have only one modifier
After previous patches, we should be generating no combination ports, so
we don't need to store multiple modifiers per mapping. Simplify the code
based on this.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 17:31:31 +01:00
Wim Taymans
31cde774c5 alsa-ucm: Make mapping UCM contexts have only one device
After previous patches, we should be generating no combination ports, so
we don't need to store multiple devices per mapping. Simplify the code
based on this.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 17:24:12 +01:00
Wim Taymans
b02c8ba153 alsa-ucm: Make ports store only one device
After previous patches, we should be generating no combination ports, so
we don't need to store multiple devices per port. Simplify the code
based on this.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 17:14:39 +01:00
Wim Taymans
68ac72d098 alsa-ucm: Remove combination port generation logic
A previous commit makes mapping names depend on the UCM device name.
Since UCM device names are unique, this means a mapping will at most
have one port and thus no combination ports can be generated.

This removes the dead code in the pa_alsa_ucm_add_ports_combination()
function, unrolls the remaining code in its helper functions that it
used, and renames it to pa_alsa_ucm_add_port() to signal that it no
longer generates combinations.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 16:49:35 +01:00
Wim Taymans
96cfc9bc1a alsa-ucm: Make one input/output mapping per UCM device
PulseAudio combines UCM devices that have the same PlaybackPCM or
CapturePCM value into a single mapping with multiple ports. It also
creates ports in the same mapping for each valid combination of those
UCM devices.

Since mappings are the things we put in profiles, we can put in a
profile either all devices of a joint mapping or none of them. This
causes some complications with device conflicts. For example, a
different UCM device might be marked as conflicting with some (but not
all) of the devices in a joint mapping. In this case we can do one of
three things:

- Include all devices in one profile, and hope the conflicting device
  isn't chosen as the mapping's active port. We shouldn't do this as it
  puts conflicting devices in the same profile.

- Make one profile with the joint group, and one with the other device.
  This is somewhat acceptable as we have no conflicts, but we sacrifice
  some compatible combinations of devices.

- Do not group the devices into the same mapping, and make one profile
  for each compatible combination of devices. This appears to be the
  best option, one where we can always have the maximum number of
  working devices.

This patch chooses the third option and makes one input and/or output
mapping per UCM device, by using UCM device names instead of PCM device
strings in the mapping names.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 16:36:40 +01:00
Wim Taymans
e2d642a20b alsa-ucm: Split out helpers for device set name, description, priority
Combination port logic calculates some useful properties for device
groups that we could reuse while generating multiple profiles to support
conflicting devices. Split them into their own functions.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 16:34:05 +01:00
Wim Taymans
80fc80c343 alsa-ucm: Fix device conformance check
Right now this check is rejecting devices whose UCM config specifies
neither a conflicting device nor a supported device list, and accepting
devices which specify both. However, a device without neither list is
actually unrestricted, and a device with both lists is a configuration
error. Fix the check to accept the former.

Furthermore, this is missing another case where an already selected
device might have a supported devices list that doesn't have the
candidate device. Make this function also check against that, and also
make it accept devices already in the set.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 16:30:00 +01:00
Wim Taymans
3a68905c7c alsa-ucm: Rewrite conformant device group generation with idxsets
The existing code meant to generate device groups for combination ports
is tightly coupled to port creation. Similar functionality would be
useful to generate nonconflicting device groups for multiple profiles as
well, so this tries to rewrite it into a more reusable state.

Several things (e.g devices, mapping contexts) use idxsets to store a
device selection. This also switches this conformance check and device
group generation to using idxsets to make it easier to work with those,
with the eventual aim to unify device group representations.

Also try to adjust users of these functions to use idxsets these will
need/return, without causing too much interference.

Signed-off-by: Alper Nebi Yasak <alpernebiyasak@gmail.com>
2023-03-20 16:28:32 +01:00