1.\" $OpenBSD: audio.9,v 1.34 2022/11/02 10:41:34 kn Exp $ 2.\" $NetBSD: audio.9,v 1.14 2000/02/11 22:56:15 kleink Exp $ 3.\" 4.\" Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. 5.\" All rights reserved. 6.\" 7.\" This code is derived from software contributed to The NetBSD Foundation 8.\" by Lennart Augustsson. 9.\" 10.\" Redistribution and use in source and binary forms, with or without 11.\" modification, are permitted provided that the following conditions 12.\" are met: 13.\" 1. Redistributions of source code must retain the above copyright 14.\" notice, this list of conditions and the following disclaimer. 15.\" 2. Redistributions in binary form must reproduce the above copyright 16.\" notice, this list of conditions and the following disclaimer in the 17.\" documentation and/or other materials provided with the distribution. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.\" POSSIBILITY OF SUCH DAMAGE. 30.\" 31.Dd $Mdocdate: November 2 2022 $ 32.Dt AUDIO 9 33.Os 34.Sh NAME 35.Nm audio 36.Nd interface between low and high level audio drivers 37.Sh DESCRIPTION 38The audio device driver is divided into a high level, 39hardware independent layer, and a low level, hardware 40dependent layer. 41The interface between these is the 42.Va audio_hw_if 43structure. 44.Bd -literal 45struct audio_hw_if { 46 int (*open)(void *, int); 47 void (*close)(void *); 48 int (*set_params)(void *, int, int, 49 struct audio_params *, struct audio_params *); 50 int (*round_blocksize)(void *, int); 51 52 int (*commit_settings)(void *); 53 54 int (*init_output)(void *, void *, int); 55 int (*init_input)(void *, void *, int); 56 int (*start_output)(void *, void *, int, 57 void (*)(void *), void *); 58 int (*start_input)(void *, void *, int, 59 void (*)(void *), void *); 60 int (*halt_output)(void *); 61 int (*halt_input)(void *); 62 63 int (*set_port)(void *, struct mixer_ctrl *); 64 int (*get_port)(void *, struct mixer_ctrl *); 65 66 int (*query_devinfo)(void *, struct mixer_devinfo *); 67 68 void *(*allocm)(void *, int, size_t, int, int); 69 void (*freem)(void *, void *, int); 70 size_t (*round_buffersize)(void *, int, size_t); 71 72 int (*trigger_output)(void *, void *, void *, int, 73 void (*)(void *), void *, struct audio_params *); 74 int (*trigger_input)(void *, void *, void *, int, 75 void (*)(void *), void *, struct audio_params *); 76 void (*copy_output)(void *hdl, size_t bytes); 77 void (*underrun)(void *hdl); 78 int (*set_blksz)(void *, int, 79 struct audio_params *, struct audio_params *, int); 80 int (*set_nblks)(void *, int, int, 81 struct audio_params *, int); 82}; 83 84struct audio_params { 85 u_long sample_rate; /* sample rate */ 86 u_int encoding; /* mu-law, linear, etc */ 87 u_int precision; /* bits/sample */ 88 u_int bps; /* bytes/sample */ 89 u_int msb; /* data alignment */ 90 u_int channels; /* mono(1), stereo(2) */ 91}; 92.Ed 93.Pp 94The high level audio driver attaches to the low level driver 95when the latter calls 96.Fn audio_attach_mi . 97This call is: 98.Bd -literal -offset indent 99struct device * 100audio_attach_mi(const struct audio_hw_if *ahwp, void *hdl, 101 struct device *dev); 102.Ed 103.Pp 104The 105.Va audio_hw_if 106struct is as shown above. 107The 108.Fa hdl 109argument is a handle to some low level data structure. 110It is sent as the first argument to all the functions in 111.Fa ahwp 112when the high level driver calls them. 113.Fa dev 114is the device struct for the hardware device. 115.Pp 116The upper layer of the audio driver allocates one buffer for playing 117and one for recording. 118It handles the buffering of data from the user processes in these. 119The data is presented to the lower level in smaller chunks, called blocks. 120During playback, if there is no data available from the user process 121when the hardware requests another block, a block of silence will be 122used instead. 123Similarly, if the user process does not read data quickly enough during 124recording, data will be thrown away. 125.Pp 126The fields of 127.Va audio_hw_if 128are described in some more detail below. 129Some fields are optional and can be set to 130.Dv NULL 131if not needed. 132.Bl -tag -width indent 133.It Ft int Fn (*open) "void *hdl" "int flags" 134This function is called when the audio device is opened, with 135.Fa flags 136the kernel representation of flags passed to the 137.Xr open 2 138system call 139.Po 140see 141.Dv OFLAGS 142and 143.Dv FFLAGS 144in 145.In sys/fcntl.h 146.Pc . 147It initializes the hardware for I/O. 148Every successful call to 149.Fn open 150is matched by a call to 151.Fn close . 152This function returns 0 on success, otherwise an error code. 153.It Ft void Fn (*close) "void *hdl" 154This function is called when the audio device is closed. 155.It Ft int Fn (*set_params) "void *hdl" "int setmode" "int usemode" \ 156"struct audio_params *play" "struct audio_params *rec" 157This function is called to set the audio encoding mode. 158.Fa setmode 159is a combination of the 160.Dv AUMODE_RECORD 161and 162.Dv AUMODE_PLAY 163flags to indicate which mode(s) are to be set. 164.Fa usemode 165is also a combination of these flags, but indicates the current 166mode of the device (i.e., the value corresponding to the 167.Va flags 168argument to the 169.Fn open 170function). 171The 172.Fa play 173and 174.Fa rec 175structures contain the encoding parameters that will be set. 176The values of the structures must also be modified if the hardware 177cannot be set to exactly the requested mode (e.g., if the requested 178sampling rate is not supported, but one close enough is). 179Except the channel count, the same value is passed in both 180.Fa play 181and 182.Fa rec . 183.Pp 184The machine independent audio driver does some preliminary parameter checking; 185it verifies that the precision is compatible with the encoding, 186and it translates 187.Dv AUDIO_ENCODING_[US]LINEAR 188to 189.Dv AUDIO_ENCODING_[US]LINEAR_{LE,BE} . 190.Pp 191This function returns 0 on success, otherwise an error code. 192.It Ft int Fn (*round_blocksize) "void *hdl" "int bs" 193This function is optional. 194If supplied, it is called with the block size, 195.Fa bs , 196which has been computed by the upper layer. 197It returns a block size, possibly changed according to the needs of the 198hardware driver. 199.It Ft int Fn (*commit_settings) "void *hdl" 200This function is optional. 201If supplied, it is called after all calls to 202.Fn set_params 203and 204.Fn set_port 205are done. 206A hardware driver that needs to get the hardware in 207and out of command mode for each change can save all the changes 208during previous calls and do them all here. 209This function returns 0 on success, otherwise an error code. 210.It Ft int Fn (*init_output) "void *hdl" "void *buffer" "int size" 211This function is optional. 212If supplied, it is called before any output starts, but only after the total 213.Fa size 214of the output 215.Fa buffer 216has been determined. 217It can be used to initialize looping DMA for hardware that needs it. 218This function returns 0 on success, otherwise an error code. 219.It Ft int Fn (*init_input) "void *hdl" "void *buffer" "int size" 220This function is optional. 221If supplied, it is called before any input starts, but only after the total 222.Fa size 223of the input 224.Fa buffer 225has been determined. 226It can be used to initialize looping DMA for hardware that needs it. 227This function returns 0 on success, otherwise an error code. 228.It Ft int Fn (*start_output) "void *hdl" "void *block" "int bsize" \ 229"void (*intr)(void *)" "void *intrarg" 230This function is called to start the transfer of 231.Fa bsize 232bytes from 233.Fa block 234to the audio hardware. 235The call returns when the data transfer 236has been initiated (normally with DMA). 237When the hardware is ready to accept more samples, the function 238.Fa intr 239will be called with the argument 240.Fa intrarg . 241Calling 242.Fa intr 243will normally initiate another call to 244.Fn start_output . 245This function returns 0 on success, otherwise an error code. 246.It Ft int Fn (*start_input) "void *hdl" "void *block" "int bsize" \ 247"void (*intr)(void *)" "void *intrarg" 248This function is called to start the transfer of 249.Fa bsize 250bytes to 251.Fa block 252from the audio hardware. 253The call returns when the data transfer 254has been initiated (normally with DMA). 255When the hardware is ready to deliver more samples, the function 256.Fa intr 257will be called with the argument 258.Fa intrarg . 259Calling 260.Fa intr 261will normally initiate another call to 262.Fn start_input . 263This function returns 0 on success, otherwise an error code. 264.It Ft int Fn (*halt_output) "void *hdl" 265This function is called to abort the output transfer (started by 266.Fn start_output ) 267in progress. 268This function returns 0 on success, otherwise an error code. 269.It Ft int Fn (*halt_input) "void *hdl" 270This function is called to abort the input transfer (started by 271.Fn start_input ) 272in progress. 273This function returns 0 on success, otherwise an error code. 274.It Ft int Fn (*set_port) "void *hdl" "struct mixer_ctrl *mc" 275This function is called when the 276.Dv AUDIO_MIXER_WRITE 277.Xr ioctl 2 278is used. 279It takes data from 280.Fa mc 281and sets the corresponding mixer values. 282This function returns 0 on success, otherwise an error code. 283.It Ft int Fn (*get_port) "void *hdl" "struct mixer_ctrl *mc" 284This function is called when the 285.Dv AUDIO_MIXER_READ 286.Xr ioctl 2 287is used. 288It fills 289.Fa mc 290and returns 0 on success, or it returns an error code on failure. 291.It Ft int Fn (*query_devinfo) "void *hdl" "struct mixer_devinfo *di" 292This function is called when the 293.Dv AUDIO_MIXER_DEVINFO 294.Xr ioctl 2 295is used. 296It fills 297.Fa di 298and returns 0 on success, or it returns an error code on failure. 299.It Ft void * Fn (*allocm) "void *hdl" "int direction" "size_t size" "int type" \ 300"int flags" 301This function is optional. 302If supplied, it is called to allocate the device buffers. 303If not supplied, 304.Xr malloc 9 305is used instead (with the same arguments but the first two). 306The reason for using a device dependent routine instead of 307.Xr malloc 9 308is that some buses need special allocation to do DMA. 309.Fa direction 310is 311.Dv AUMODE_PLAY 312or 313.Dv AUMODE_RECORD . 314This function returns the address of the buffer on success, or 0 on failure. 315.It Ft void Fn (*freem) "void *hdl" "void *addr" "int type" 316This function is optional. 317If supplied, it is called to free memory allocated by 318.Fn allocm . 319If not supplied, 320.Xr free 9 321is used instead. 322.\" XXX: code passes int instead of size_t, but decl is size_t 323.It Ft size_t Fn (*round_buffersize) "void *hdl" "int direction" \ 324"size_t bufsize" 325This function is optional. 326If supplied, it is called at startup to determine the audio buffer size. 327The upper layer supplies the suggested size in 328.Fa bufsize , 329which the hardware driver can then change if needed. 330E.g., DMA on the ISA bus cannot exceed 65536 bytes. 331Note that the buffer size is always a multiple of the block size, so 332.Fn round_blocksize 333and 334.Fn round_buffersize 335must be consistent. 336.It Ft int Fn (*trigger_output) "void *hdl" "void *start" "void *end" "int blksize" \ 337"void (*intr)(void *)" "void *intrarg" "struct audio_params *param" 338This function is optional. 339If supplied, it is called to start the transfer of data from the circular 340buffer delimited by 341.Fa start 342and 343.Fa end 344to the audio hardware, parameterized as in 345.Fa param . 346The call returns when the data transfer 347has been initiated (normally with DMA). 348When the hardware is finished transferring each 349.Fa blksize 350sized block, the function 351.Fa intr 352will be called with the argument 353.Fa intrarg 354(typically from the audio hardware interrupt service routine). 355Once started, the transfer may be stopped using 356.Fn halt_output . 357This function returns 0 on success, otherwise an error code. 358.It Ft int Fn (*trigger_input) "void *hdl" "void *start" "void *end" "int blksize" \ 359"void (*intr)(void *)" "void *intrarg" "struct audio_params *param" 360This function is optional. 361If supplied, it is called to start the transfer of data from the audio 362hardware, parameterized as in 363.Fa param , 364to the circular buffer delimited by 365.Fa start 366and 367.Fa end . 368The call returns when the data transfer 369has been initiated (normally with DMA). 370When the hardware is finished transferring each 371.Fa blksize 372sized block, the function 373.Fa intr 374will be called with the argument 375.Fa intrarg 376(typically from the audio hardware interrupt service routine). 377Once started, the transfer may be stopped using 378.Fn halt_input . 379This function returns 0 on success, otherwise an error code. 380.It Ft void Fn (*copy_output) "void *hdl" "size_t bytes" 381This function is called whenever the given amount of bytes was 382appended to the play ring buffer, typically during a 383.Xr write 2 384system call. 385Drivers using bounce buffers for transfers between the audio 386ring buffer and the device could implement this function 387to copy the given amount of bytes into their bounce buffers. 388There's no analogue function for recording as data is 389produced by the device and could simply be copied upon 390transfer completion. 391.It Ft void Fn (*underrun) "void *hdl" 392This function is called at interrupt context whenever a 393play block was skipped by the 394.Xr audio 4 395driver. 396Drivers using bounce buffers for transfers between the audio 397ring buffer and the device must implement this method to skip 398one block from the audio ring buffer and transfer the 399corresponding amount of silence to the device. 400.It Ft int Fn (*set_blksz) "void *hdl" "int mode" \ 401"struct audio_params *play" "struct audio_params *rec" "int blksz" 402This function is called to set the audio block size. 403.Fa mode 404is a combination of the 405.Dv AUMODE_RECORD 406and 407.Dv AUMODE_PLAY 408flags indicating the current mode set with the 409.Fn open 410function. 411The 412.Fa play 413and 414.Fa rec 415structures contain the current encoding set with the 416.Fn set_params 417function. 418.Fa blksz 419is the desired block size in frames. 420It may be adjusted to match hardware constraints. 421This function returns the adjusted block size. 422.It Ft int Fn (*set_nblks) "void *hdl" "int dir" "int blksz" \ 423"struct audio_params *params" "int nblks" 424This function is called to set the number of audio blocks in 425the ring buffer. 426.Fa dir 427is either the 428.Dv AUMODE_RECORD 429or the 430.Dv AUMODE_PLAY 431flag, indicating which ring buffer size is set. 432The 433.Fa params 434structure contains the encoding parameters set by the 435.Fn set_params 436method. 437.Fa blksz 438is the current block size in frames set with the 439.Fa set_params 440function. 441The 442.Fa params 443structure is the current encoding parameters, set with the 444.Fn set_params 445function. 446.Fa nblks 447is the desired number of blocks in the ring buffer. 448It may be lowered to at least two, to match hardware constraints. 449This function returns the adjusted number of blocks. 450.El 451.Pp 452If the audio hardware is capable of input from more 453than one source, it should define 454.Dv AudioNsource 455in class 456.Dv AudioCrecord . 457This mixer control should be of type 458.Dv AUDIO_MIXER_ENUM 459or 460.Dv AUDIO_MIXER_SET 461and enumerate the possible input sources. 462For each of the named sources there should be 463a control in the 464.Dv AudioCinputs 465class of type 466.Dv AUDIO_MIXER_VALUE 467if recording level of the source can be set. 468If the overall recording level can be changed (i.e., regardless 469of the input source) then this control should be named 470.Dv AudioNrecord 471and be of class 472.Dv AudioCinputs . 473.Pp 474If the audio hardware is capable of output to more than 475one destination, it should define 476.Dv AudioNoutput 477in class 478.Dv AudioCmonitor . 479This mixer control should be of type 480.Dv AUDIO_MIXER_ENUM 481or 482.Dv AUDIO_MIXER_SET 483and enumerate the possible destinations. 484For each of the named destinations there should be 485a control in the 486.Dv AudioCoutputs 487class of type 488.Dv AUDIO_MIXER_VALUE 489if output level of the destination can be set. 490If the overall output level can be changed (i.e., regardless 491of the destination) then this control should be named 492.Dv AudioNmaster 493and be of class 494.Dv AudioCoutputs . 495.Sh SEE ALSO 496.Xr ioctl 2 , 497.Xr open 2 , 498.Xr sio_open 3 , 499.Xr audio 4 , 500.Xr free 9 , 501.Xr malloc 9 502.Sh HISTORY 503This 504.Nm 505interface first appeared in 506.Ox 1.2 . 507