Re: uaudio device re-attach and persisting dev.pcm.$pcm.bitperfect sysctl

From: Alban Hertroys <haramrae_at_gmail.com>
Date: Sat, 05 Oct 2024 12:01:23 UTC
> On 2 Oct 2024, at 23:53, Christos Margiolis <christos@freebsd.org> wrote:
> 
> Alban Hertroys wrote:
>> Meanwhile, several people here suggested that devd is the way to go
>> about this. I had actually looked into that a bit, but that seemed to
>> require a related device node in /dev, and there’s neither one for pcm
>> nor for uaudio, so I discarded that as not being a viable option.
>> Perhaps too soon.
> 
> Audio device nodes are /dev/dspX, where X is the unit number, which has
> a 1-1 relation with the pcmX unit number, so pcm2's device node is
> /dev/dsp2.

Doh! I knew that, just didn’t realise it while writing that devd config.

I got it working now. Here’s what I came up with:

> cat /usr/local/etc/devd/uaudio.conf
notify 100 {
	match "system"		"USB";
	match "subsystem"	"DEVICE";
	match "type"		"ATTACH";
	match "vendor"		"0x152a";
	match "product"		"0x8750";

	action "/usr/bin/touch /var/run/usb.0x152a.0x8750";
};

notify 100 {
	match "system"		"USB";
	match "subsystem"	"DEVICE";
	match "type"		"DETACH";
	match "vendor"		"0x152a";
	match "product"		"0x8750";

	action "/bin/rm -f /var/run/usb.0x152a.0x8750";
};


notify 100 {
	match "system"		"DEVFS";
	match "subsystem"	"CDEV";
	match "type"		"CREATE";
	match "cdev"		"dsp[0-9]";

	action "test -f /var/run/usb.0x152a.0x8750 && \
	    PCM=`echo $cdev | sed 's@^dsp@@'` && \
		sysctl hw.snd.default_unit=${PCM} \
		dev.pcm.${PCM}.play.vchans=0 \
		dev.pcm.${PCM}.bitperfect=1";

};

As you can see, I build in a check that it’s the correct device before starting to set sysctl’s, by checking against a file created in /var/run. I’m not too enthusiastic about that solution… Better suggestions are welcome.

It would be nice if I could set a variable in the USB ATTACH and ‘match’ that in the CDEV CREATE step to filter the rule, or something like that.

Originally I tried checking for the device using:
	test "`sysctl -n dev.pcm.${PCM}.%desc`" = 'Topping D90SE’
, but that statement seems to require the double-quotes for test to accept it, which conflict with the double-quotes of the action string. I couldn’t find a way to escape those inner quotes. So that’s another issue I ran into, although approaches to use the USB attach event for matching the dsp device to the usb device are clearly superior to reading out a sysctl that’s an effect of it. 

Additionally, I noticed that other people also use sed or similar to cut the device number off the cdev variable in their devd configs. That would have been a convenient variable to have available by default, I think.

I mean, obviously it’s cool that we can show off how good we are at looking up sed expressions on Google, but as a data engineer I can’t help but feel that the data that we need for these configs could be easier to get at.

All in all, I got it working, thanks to the help here.

Regards,

Alban Hertroys
--
There is always an exception to always.