xref: /freebsd/share/man/man4/sndstat.4 (revision ffc4f93e960d38b6f70d59409127f7562e40f916)
1c96151d3SKa Ho Ng.\"
24d846d26SWarner Losh.\" SPDX-License-Identifier: BSD-2-Clause
3c96151d3SKa Ho Ng.\"
4c96151d3SKa Ho Ng.\" This software was developed by Ka Ho Ng
5c96151d3SKa Ho Ng.\" under sponsorship from the FreeBSD Foundation.
6c96151d3SKa Ho Ng.\"
7c96151d3SKa Ho Ng.\" Copyright (c) 2020 The FreeBSD Foundation
8c96151d3SKa Ho Ng.\"
9c96151d3SKa Ho Ng.\" Redistribution and use in source and binary forms, with or without
10c96151d3SKa Ho Ng.\" modification, are permitted provided that the following conditions
11c96151d3SKa Ho Ng.\" are met:
12c96151d3SKa Ho Ng.\" 1. Redistributions of source code must retain the above copyright
13c96151d3SKa Ho Ng.\"    notice, this list of conditions and the following disclaimer.
14c96151d3SKa Ho Ng.\" 2. Redistributions in binary form must reproduce the above copyright
15c96151d3SKa Ho Ng.\"    notice, this list of conditions and the following disclaimer in the
16c96151d3SKa Ho Ng.\"    documentation and/or other materials provided with the distribution.
17c96151d3SKa Ho Ng.\"
18c96151d3SKa Ho Ng.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19c96151d3SKa Ho Ng.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20c96151d3SKa Ho Ng.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21c96151d3SKa Ho Ng.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22c96151d3SKa Ho Ng.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23c96151d3SKa Ho Ng.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24c96151d3SKa Ho Ng.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25c96151d3SKa Ho Ng.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26c96151d3SKa Ho Ng.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27c96151d3SKa Ho Ng.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28c96151d3SKa Ho Ng.\" SUCH DAMAGE.
29c96151d3SKa Ho Ng.\"
30c96151d3SKa Ho Ng.\" Note: The date here should be updated whenever a non-trivial
31c96151d3SKa Ho Ng.\" change is made to the manual page.
324ce1ba65SKa Ho Ng.Dd April 15, 2021
33c96151d3SKa Ho Ng.Dt SNDSTAT 4
34c96151d3SKa Ho Ng.Os
35c96151d3SKa Ho Ng.Sh NAME
36c96151d3SKa Ho Ng.Nm sndstat
37c96151d3SKa Ho Ng.Nd "nvlist-based PCM audio device enumeration interface"
38c96151d3SKa Ho Ng.Sh SYNOPSIS
39c96151d3SKa Ho NgTo compile the driver into the kernel,
40c96151d3SKa Ho Ngplace the following lines in the
41c96151d3SKa Ho Ngkernel configuration file:
42c96151d3SKa Ho Ng.Bd -ragged -offset indent
43c96151d3SKa Ho Ng.Cd "device sound"
44c96151d3SKa Ho Ng.Ed
45c96151d3SKa Ho Ng.Sh DESCRIPTION
46c96151d3SKa Ho NgThe ioctl interface provided by
47c96151d3SKa Ho Ng.Pa /dev/sndstat
48c232fd4bSGoran Mekićdevice allows callers to enumerate PCM audio devices available for use.
49c232fd4bSGoran MekićIn other words, it provides means to get the list of all audio devices
50c232fd4bSGoran Mekićavailable to the system.
51c96151d3SKa Ho Ng.Sh IOCTLS
52c232fd4bSGoran MekićFor ioctl calls that take an argument, the following structure is used:
53c96151d3SKa Ho Ng.Bd -literal -offset indent
544ce1ba65SKa Ho Ngstruct sndstioc_nv_arg {
55c96151d3SKa Ho Ng	size_t nbytes;
56c96151d3SKa Ho Ng	void *buf;
57c96151d3SKa Ho Ng};
58c96151d3SKa Ho Ng.Ed
59c96151d3SKa Ho Ng.Pp
60c232fd4bSGoran MekićHere is an example of an nvlist object with explanations of the common fields:
61c96151d3SKa Ho Ng.Bd -literal -offset indent
62c96151d3SKa Ho Ngdsps (NVLIST ARRAY): 1
63c96151d3SKa Ho Ng    from_user (BOOL): FALSE
64c96151d3SKa Ho Ng    nameunit (STRING): [pcm0]
65c96151d3SKa Ho Ng    devnode (STRING): [dsp0]
66c96151d3SKa Ho Ng    desc (STRING): [Generic (0x8086) (Analog Line-out)]
67c96151d3SKa Ho Ng    pchan (NUMBER): 1 (1) (0x1)
68c96151d3SKa Ho Ng    rchan (NUMBER): 0 (0) (0x0)
694ce1ba65SKa Ho Ng    info_play (NVLIST):
704ce1ba65SKa Ho Ng        min_rate (NUMBER): 48000 (48000) (0xbb80)
714ce1ba65SKa Ho Ng        max_rate (NUMBER): 48000 (48000) (0xbb80)
724ce1ba65SKa Ho Ng        formats (NUMBER): 16 (16) (0x10)
734ce1ba65SKa Ho Ng        min_chn (NUMBER): 2 (2) (0x2)
744ce1ba65SKa Ho Ng        max_chn (NUMBER): 2 (2) (0x2)
75c96151d3SKa Ho Ng    provider_info (NVLIST):
76c96151d3SKa Ho Ng        unit (NUMBER): 0 (0) (0x0)
77c96151d3SKa Ho Ng        bitperfect (BOOL): FALSE
78c96151d3SKa Ho Ng        pvchan (NUMBER): 1 (1) (0x1)
79c96151d3SKa Ho Ng        rvchan (NUMBER): 0 (0) (0x0)
80c96151d3SKa Ho Ng    provider (STRING): [sound(4)]
81c96151d3SKa Ho Ng    ,
82c96151d3SKa Ho Ng.Ed
83c96151d3SKa Ho Ng.Bl -tag -width ".Dv provider_info"
84c96151d3SKa Ho Ng.It Dv from_user
85c96151d3SKa Ho NgWhether the PCM audio device node is created by in-kernel audio subsystem or
86c96151d3SKa Ho Nguserspace providers.
87c96151d3SKa Ho Ng.It Dv nameunit
88c96151d3SKa Ho NgThe device identification in the form of subsystem plus a unit number.
89c96151d3SKa Ho Ng.It Dv devnode
90c96151d3SKa Ho NgThe PCM audio device node relative path in devfs.
91c96151d3SKa Ho Ng.It Dv desc
92*ffc4f93eSBenedict ReuschlingThe description of the PCM audio device.
93c96151d3SKa Ho Ng.It Dv pchan
94c96151d3SKa Ho NgThe number of playback channels supported by hardware.
95c96151d3SKa Ho NgThis can be 0 if this PCM audio device does not support playback at all.
96c96151d3SKa Ho Ng.It Dv rchan
97c96151d3SKa Ho NgThe number of recording channels supported by hardware.
98c96151d3SKa Ho NgThis can be 0 if this PCM audio device does not support recording at all.
994ce1ba65SKa Ho Ng.It Dv info_play
1004ce1ba65SKa Ho NgSupported configurations in playback direction.
1014ce1ba65SKa Ho NgThis exists only if this PCM audio device supports playback.
1024ce1ba65SKa Ho NgThere are a number of name/value pairs inside this field:
1034ce1ba65SKa Ho Ng.Bl -tag -width ".Dv min_rate"
1044ce1ba65SKa Ho Ng.It Dv min_rate
1054ce1ba65SKa Ho NgMinimum supported sampling rate.
1064ce1ba65SKa Ho Ng.It Dv max_rate
1074ce1ba65SKa Ho NgMaximum supported sampling rate.
1084ce1ba65SKa Ho Ng.It Dv formats
1094ce1ba65SKa Ho NgSupported sample formats.
1104ce1ba65SKa Ho Ng.It Dv min_chn
1114ce1ba65SKa Ho NgMinimum supported number of channels in channel layout
1124ce1ba65SKa Ho Ng.It Dv max_chn
1134ce1ba65SKa Ho NgMaximum supported number of channels in channel layout
1144ce1ba65SKa Ho Ng.El
1154ce1ba65SKa Ho Ng.It Dv info_rec
1164ce1ba65SKa Ho NgSupported configurations in recording direction.
1174ce1ba65SKa Ho NgThis exists only if this PCM audio device supports recording.
1184ce1ba65SKa Ho NgThere are a number of name/value pairs inside this field:
1194ce1ba65SKa Ho Ng.Bl -tag -width ".Dv min_rate"
1204ce1ba65SKa Ho Ng.It Dv min_rate
1214ce1ba65SKa Ho NgMinimum supported sampling rate.
1224ce1ba65SKa Ho Ng.It Dv max_rate
1234ce1ba65SKa Ho NgMaximum supported sampling rate.
1244ce1ba65SKa Ho Ng.It Dv formats
1254ce1ba65SKa Ho NgSupported sample formats.
1264ce1ba65SKa Ho Ng.It Dv min_chn
1274ce1ba65SKa Ho NgMinimum supported number of channels in channel layout
1284ce1ba65SKa Ho Ng.It Dv max_chn
1294ce1ba65SKa Ho NgMaximum supported number of channels in channel layout
1304ce1ba65SKa Ho Ng.El
131c96151d3SKa Ho Ng.It Dv provider_info
132c96151d3SKa Ho NgProvider-specific fields.
133c96151d3SKa Ho NgThis field may not exist if the PCM audio device is not provided by in-kernel
134c96151d3SKa Ho Nginterface.
135c96151d3SKa Ho NgThis field will not exist if the provider field is an empty string.
136c96151d3SKa Ho Ng.It Dv provider
137c96151d3SKa Ho NgA string specifying the provider of the PCm audio device.
138c96151d3SKa Ho Ng.El
139c96151d3SKa Ho Ng.Pp
1404ce1ba65SKa Ho NgThe following ioctls are provided for use:
1414ce1ba65SKa Ho Ng.Bl -tag -width ".Dv SNDSTIOC_FLUSH_USER_DEVS"
1424ce1ba65SKa Ho Ng.It Dv SNDSTIOC_REFRESH_DEVS
143c96151d3SKa Ho NgDrop any previously fetched PCM audio devices list snapshots.
144c96151d3SKa Ho NgThis ioctl takes no arguments.
1454ce1ba65SKa Ho Ng.It Dv SNDSTIOC_GET_DEVS
146c96151d3SKa Ho NgGenerate and/or return PCM audio devices list snapshots to callers.
147c96151d3SKa Ho NgThis ioctl takes a pointer to
1484ce1ba65SKa Ho Ng.Fa struct sndstioc_nv_arg
149c96151d3SKa Ho Ngas the first and the only argument.
150c96151d3SKa Ho NgCallers need to provide a sufficiently large buffer to hold a serialized
151c96151d3SKa Ho Ngnvlist.
152c96151d3SKa Ho NgIf there is no existing PCM audio device list snapshot available in the
153c96151d3SKa Ho Nginternal structure of the opened sndstat.
154c96151d3SKa Ho Ng.Fa fd ,
155c96151d3SKa Ho Nga new PCM audio device list snapshot will be automatically generated.
156c96151d3SKa Ho NgCallers have to set
157c96151d3SKa Ho Ng.Fa nbytes
158c96151d3SKa Ho Ngto either 0 or the size of buffer provided.
159c96151d3SKa Ho NgIn case
160c96151d3SKa Ho Ng.Fa nbytes
161c96151d3SKa Ho Ngis 0, the buffer size required to hold a serialized nvlist
162c96151d3SKa Ho Ngstream of current snapshot will be returned in
163c96151d3SKa Ho Ng.Fa nbytes ,
164c96151d3SKa Ho Ngand
165c96151d3SKa Ho Ng.Fa buf
166c96151d3SKa Ho Ngwill be ignored.
167c96151d3SKa Ho NgOtherwise, if the buffer is not sufficiently large,
168c96151d3SKa Ho Ngthe ioctl returns success, and
169c96151d3SKa Ho Ng.Fa nbytes
170c96151d3SKa Ho Ngwill be set to 0.
171c96151d3SKa Ho NgIf the buffer provided is sufficiently large,
172c96151d3SKa Ho Ng.Fa nbytes
173c96151d3SKa Ho Ngwill be set to the size of the serialized nvlist written to the provided buffer.
174c96151d3SKa Ho NgOnce a PCM audio device list snapshot is returned to user-space successfully,
175c96151d3SKa Ho Ngthe snapshot stored in the subsystem's internal structure of the given
176c96151d3SKa Ho Ng.Fa fd
177c96151d3SKa Ho Ngwill be freed.
1784ce1ba65SKa Ho Ng.It Dv SNDSTIOC_ADD_USER_DEVS
179c96151d3SKa Ho NgAdd a list of PCM audio devices provided by callers to
180c96151d3SKa Ho Ng.Pa /dev/sndstat
181c96151d3SKa Ho Ngdevice.
182c96151d3SKa Ho NgThis ioctl takes a pointer to
1834ce1ba65SKa Ho Ng.Fa struct sndstioc_nv_arg
184c96151d3SKa Ho Ngas the first and the only argument.
185c96151d3SKa Ho NgCallers have to provide a buffer holding a serialized nvlist.
186c96151d3SKa Ho Ng.Fa nbytes
187c96151d3SKa Ho Ngshould be set to the length in bytes of the serialized nvlist.
188c96151d3SKa Ho Ng.Fa buf
189c96151d3SKa Ho Ngshould be pointed to a buffer storing the serialized nvlist.
190c96151d3SKa Ho NgUserspace-backed PCM audio device nodes should be listed inside the serialized
191c96151d3SKa Ho Ngnvlist.
1924ce1ba65SKa Ho Ng.It Dv SNDSTIOC_FLUSH_USER_DEVS
193c96151d3SKa Ho NgFlush any PCM audio devices previously added by callers.
194c96151d3SKa Ho NgThis ioctl takes no arguments.
195c96151d3SKa Ho Ng.El
196c96151d3SKa Ho Ng.Sh FILES
197c96151d3SKa Ho Ng.Bl -tag -width ".Pa /dev/sndstat" -compact
198c96151d3SKa Ho Ng.It Pa /dev/sndstat
199c96151d3SKa Ho Ng.El
200c96151d3SKa Ho Ng.Sh EXAMPLES
201c96151d3SKa Ho NgThe following code enumerates all available PCM audio devices:
202c96151d3SKa Ho Ng.Bd -literal -offset indent
203c96151d3SKa Ho Ng#include <sys/types.h>
204c96151d3SKa Ho Ng#include <err.h>
205c96151d3SKa Ho Ng#include <fcntl.h>
206c96151d3SKa Ho Ng#include <stdio.h>
207c96151d3SKa Ho Ng#include <stdlib.h>
208c96151d3SKa Ho Ng#include <sys/nv.h>
209c96151d3SKa Ho Ng#include <sys/sndstat.h>
210c96151d3SKa Ho Ng#include <sysexits.h>
211c96151d3SKa Ho Ng#include <unistd.h>
212c96151d3SKa Ho Ng
213c96151d3SKa Ho Ngint
214c96151d3SKa Ho Ngmain()
215c96151d3SKa Ho Ng{
216c96151d3SKa Ho Ng	int fd;
2174ce1ba65SKa Ho Ng	struct sndstioc_nv_arg arg;
218c96151d3SKa Ho Ng	const nvlist_t * const *di;
219c96151d3SKa Ho Ng	size_t i, nitems;
220c96151d3SKa Ho Ng	nvlist_t *nvl;
221c96151d3SKa Ho Ng
222c96151d3SKa Ho Ng	/* Open sndstat node in read-only first */
223c96151d3SKa Ho Ng	fd = open("/dev/sndstat", O_RDONLY);
224c96151d3SKa Ho Ng
2254ce1ba65SKa Ho Ng	if (ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL))
2264ce1ba65SKa Ho Ng		err(1, "ioctl(fd, SNDSTIOC_REFRESH_DEVS, NULL)");
227c96151d3SKa Ho Ng
228c96151d3SKa Ho Ng	/* Get the size of snapshot, when nbytes = 0 */
229c96151d3SKa Ho Ng	arg.nbytes = 0;
230c96151d3SKa Ho Ng	arg.buf = NULL;
2314ce1ba65SKa Ho Ng	if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg))
2324ce1ba65SKa Ho Ng		err(1, "ioctl(fd, SNDSTIOC_GET_DEVS, &arg)");
233c96151d3SKa Ho Ng
234c96151d3SKa Ho Ng	/* Get snapshot data */
235c96151d3SKa Ho Ng	arg.buf = malloc(arg.nbytes);
236c96151d3SKa Ho Ng	if (arg.buf == NULL)
237c96151d3SKa Ho Ng		err(EX_OSERR, "malloc");
2384ce1ba65SKa Ho Ng	if (ioctl(fd, SNDSTIOC_GET_DEVS, &arg))
2394ce1ba65SKa Ho Ng		err(1, "ioctl(fd, SNDSTIOC_GET_DEVS, &arg)");
240c96151d3SKa Ho Ng
241c96151d3SKa Ho Ng	/* Deserialize the nvlist stream */
242c96151d3SKa Ho Ng	nvl = nvlist_unpack(arg.buf, arg.nbytes, 0);
243c96151d3SKa Ho Ng	free(arg.buf);
244c96151d3SKa Ho Ng
245c96151d3SKa Ho Ng	/* Get DSPs array */
2464ce1ba65SKa Ho Ng	di = nvlist_get_nvlist_array(nvl, SNDST_DSPS, &nitems);
247c96151d3SKa Ho Ng	for (i = 0; i < nitems; i++) {
248c96151d3SKa Ho Ng		const char *nameunit, *devnode, *desc;
249c96151d3SKa Ho Ng
250c96151d3SKa Ho Ng		/*
251c96151d3SKa Ho Ng		 * Examine each device nvlist item
252c96151d3SKa Ho Ng		 */
253c96151d3SKa Ho Ng
2544ce1ba65SKa Ho Ng		nameunit = nvlist_get_string(di[i], SNDST_DSPS_NAMEUNIT);
2554ce1ba65SKa Ho Ng		devnode = nvlist_get_string(di[i], SNDST_DSPS_DEVNODE);
2564ce1ba65SKa Ho Ng		desc = nvlist_get_string(di[i], SNDST_DSPS_DESC);
257c96151d3SKa Ho Ng		printf("Name unit: `%s`, Device node: `%s`, Description: `%s`\n",
258c96151d3SKa Ho Ng		    nameunit, devnode, desc);
259c96151d3SKa Ho Ng	}
260c96151d3SKa Ho Ng
261c96151d3SKa Ho Ng	nvlist_destroy(nvl);
262c96151d3SKa Ho Ng	return (0);
263c96151d3SKa Ho Ng}
264c96151d3SKa Ho Ng.Ed
265c96151d3SKa Ho Ng.Sh SEE ALSO
266c96151d3SKa Ho Ng.Xr sound 4 ,
267c96151d3SKa Ho Ng.Xr nv 9
268c96151d3SKa Ho Ng.Sh HISTORY
269c96151d3SKa Ho NgThe nvlist-based ioctls support for
270c96151d3SKa Ho Ng.Nm
271c96151d3SKa Ho Ngdevice first appeared in
272c96151d3SKa Ho Ng.Fx 13.0 .
273c96151d3SKa Ho Ng.Sh AUTHORS
274c96151d3SKa Ho NgThis manual page was written by
275c96151d3SKa Ho Ng.An Ka Ho Ng Aq Mt khng@FreeBSD.org .
276