1.\" $NetBSD: audio.9,v 1.34 2005/12/20 19:53:14 rpaulo Exp $ 2.\" 3.\" Copyright (c) 1999, 2000 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Lennart Augustsson. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd December 20, 2005 38.Dt AUDIO 9 39.Os 40.Sh NAME 41.Nm audio 42.Nd interface between low and high level audio drivers 43.Sh DESCRIPTION 44The audio device driver is divided into a high level, 45hardware independent layer, and a low level hardware 46dependent layer. 47The interface between these is the 48.Va audio_hw_if 49structure. 50.Bd -literal 51struct audio_hw_if { 52 int (*open)(void *, int); 53 void (*close)(void *); 54 int (*drain)(void *); 55 56 int (*query_encoding)(void *, struct audio_encoding *); 57 int (*set_params)(void *, int, int, 58 audio_params_t *, audio_params_t *, 59 stream_filter_list_t *, stream_filter_list_t *); 60 int (*round_blocksize)(void *, int, int, const audio_params_t *); 61 62 int (*commit_settings)(void *); 63 64 int (*init_output)(void *, void *, int); 65 int (*init_input)(void *, void *, int); 66 int (*start_output)(void *, void *, int, void (*)(void *), 67 void *); 68 int (*start_input)(void *, void *, int, void (*)(void *), 69 void *); 70 int (*halt_output)(void *); 71 int (*halt_input)(void *); 72 73 int (*speaker_ctl)(void *, int); 74#define SPKR_ON 1 75#define SPKR_OFF 0 76 77 int (*getdev)(void *, struct audio_device *); 78 int (*setfd)(void *, int); 79 80 int (*set_port)(void *, mixer_ctrl_t *); 81 int (*get_port)(void *, mixer_ctrl_t *); 82 83 int (*query_devinfo)(void *, mixer_devinfo_t *); 84 85 void *(*allocm)(void *, int, size_t, struct malloc_type *, int); 86 void (*freem)(void *, void *, struct malloc_type *); 87 size_t (*round_buffersize)(void *, int, size_t); 88 paddr_t (*mappage)(void *, void *, off_t, int); 89 90 int (*get_props)(void *); 91 92 int (*trigger_output)(void *, void *, void *, int, 93 void (*)(void *), void *, const audio_params_t *); 94 int (*trigger_input)(void *, void *, void *, int, 95 void (*)(void *), void *, const audio_params_t *); 96 int (*dev_ioctl)(void *, u_long, caddr_t, int, struct lwp *); 97}; 98 99typedef struct audio_params { 100 u_int sample_rate; /* sample rate */ 101 u_int encoding; /* e.g. mu-law, linear, etc */ 102 u_int precision; /* bits/subframe */ 103 u_int validbits; /* valid bits in a subframe */ 104 u_int channels; /* mono(1), stereo(2) */ 105} audio_params_t; 106.Ed 107.Pp 108The high level audio driver attaches to the low level driver 109when the latter calls 110.Va audio_attach_mi . 111This call should be 112.Bd -literal 113 void 114 audio_attach_mi(ahwp, hdl, dev) 115 struct audio_hw_if *ahwp; 116 void *hdl; 117 struct device *dev; 118.Ed 119.Pp 120The 121.Va audio_hw_if 122struct is as shown above. 123The 124.Va hdl 125argument is a handle to some low level data structure. 126It is sent as the first argument to all the functions 127in 128.Va audio_hw_if 129when the high level driver calls them. 130.Va dev 131is the device struct for the hardware device. 132.Pp 133The upper layer of the audio driver allocates one buffer for playing 134and one for recording. 135It handles the buffering of data from the user processes in these. 136The data is presented to the lower level in smaller chunks, called blocks. 137If there, during playback, is no data available from the user process when 138the hardware request another block a block of silence will be used instead. 139Furthermore, if the user process does not read data quickly enough during 140recording data will be thrown away. 141.Pp 142The fields of 143.Va audio_hw_if 144are described in some more detail below. 145Some fields are optional and can be set to 0 if not needed. 146.Bl -tag -width indent 147.It Dv int open(void *hdl, int flags) 148optional, is called when the audio device is opened. 149It should initialize the hardware for I/O. 150Every successful call to 151.Va open 152is matched by a call to 153.Va close . 154Return 0 on success, otherwise an error code. 155.It Dv void close(void *hdl) 156optional, is called when the audio device is closed. 157.It Dv int drain(void *hdl) 158optional, is called before the device is closed or when 159.Dv AUDIO_DRAIN 160is called. 161It should make sure that no samples remain in to be played that could 162be lost when 163.Va close 164is called. 165Return 0 on success, otherwise an error code. 166.It Dv int query_encoding(void *hdl, struct audio_encoding *ae) 167is used when 168.Dv AUDIO_GETENC 169is called. 170It should fill the 171.Va audio_encoding 172structure and return 0 or, if there is no encoding with the 173given number, return EINVAL. 174.It Dv int set_params(void *hdl, int setmode, int usemode, 175.Dv "audio_params_t *play, audio_params_t *rec," 176.br 177.Dv "stream_filter_list_t *pfil, stream_filter_list_t *rfil)" 178.br 179Called to set the audio encoding mode. 180.Va setmode 181is a combination of the 182.Dv AUMODE_RECORD 183and 184.Dv AUMODE_PLAY 185flags to indicate which mode(s) are to be set. 186.Va usemode 187is also a combination of these flags, but indicates the current 188mode of the device (i.e., the value of 189.Va mode 190in the 191.Va audio_info 192struct). 193.Pp 194The 195.Va play 196and 197.Va rec 198structures contain the encoding parameters that should be set. 199The values of the structures may also be modified if the hardware 200cannot be set to exactly the requested mode (e.g., if the requested 201sampling rate is not supported, but one close enough is). 202.Pp 203If the hardware requires software assistance with some encoding 204(e.g., it might be lacking mu-law support) it should fill the 205.Va pfil 206for playing or 207.Va rfil 208for recording with conversion information. 209For example, if 210.Va play 211requests [8000Hz, mu-law, 8/8bit, 1ch] and the hardware does not 212support 8bit mu-law, but 16bit slinear_le, the driver should call 213.Dv pfil-\*[Gt]append() 214with 215.Va pfil , 216.Va mulaw_to_slinear16 , 217and audio_params_t representing [8000Hz, slinear_le, 16/16bit, 2ch]. 218If the driver needs multiple conversions, a conversion nearest to the 219hardware should be set to the head of 220.Va pfil 221or 222.Va rfil . 223The definition of 224.Dv stream_filter_list_t 225follows: 226.Bd -literal 227typedef struct stream_filter_list { 228 void (*append)(struct stream_filter_list *, 229 stream_filter_factory_t, 230 const audio_params_t *); 231 void (*prepend)(struct stream_filter_list *, 232 stream_filter_factory_t, 233 const audio_params_t *); 234 void (*set)(struct stream_filter_list *, int, 235 stream_filter_factory_t, 236 const audio_params_t *); 237 int req_size; 238 struct stream_filter_req { 239 stream_filter_factory_t *factory; 240 audio_params_t param; /* from-param for recording, 241 to-param for playing */ 242 } filters[AUDIO_MAX_FILTERS]; 243} stream_filter_list_t; 244.Ed 245.Pp 246For playing, 247.Va pfil 248constructs conversions as follows: 249.Bd -literal 250 (play) == write(2) input 251 | pfil-\*[Gt]filters[pfil-\*[Gt]req_size-1].factory 252 (pfil-\*[Gt]filters[pfil-\*[Gt]req_size-1].param) 253 | pfil-\*[Gt]filters[pfil-\*[Gt]req_size-2].factory 254 : 255 | pfil-\*[Gt]filters[1].factory 256 (pfil-\*[Gt]filters[1].param) 257 | pfil-\*[Gt]filters[0].factory 258 (pfil-\*[Gt]filters[0].param) == hardware input 259.Ed 260.Pp 261For recording, 262.Va rfil 263constructs conversions as follows: 264.Bd -literal 265 (rfil-\*[Gt]filters[0].param) == hardware output 266 | rfil-\*[Gt]filters[0].factory 267 (rfil-\*[Gt]filters[1].param) 268 | rfil-\*[Gt]filters[1].factory 269 : 270 | rfil-\*[Gt]filters[rfil-\*[Gt]req_size-2].factory 271 (rfil-\*[Gt]filters[rfil-\*[Gt]req_size-1].param) 272 | rfil-\*[Gt]filters[rfil-\*[Gt]req_size-1].factory 273 (rec) == read(2) output 274.Ed 275.Pp 276If the device does not have the 277.Dv AUDIO_PROP_INDEPENDENT 278property the same value is passed in both 279.Va play 280and 281.Va rec 282and the encoding parameters from 283.Va play 284is copied into 285.Va rec 286after the call to 287.Va set_params . 288Return 0 on success, otherwise an error code. 289.It Dv int round_blocksize(void *hdl, int bs, int mode, 290.Dv "const audio_params_t *param)" 291.br 292optional, is called with the block size, 293.Va bs , 294that has been computed by the upper layer, 295.Va mode , 296.Dv AUMODE_PLAY 297or 298.Dv AUMODE_RECORD , 299and 300.Va param , 301encoding parameters for the hardware. 302It should return a block size, possibly changed according to the needs 303of the hardware driver. 304.It Dv int commit_settings(void *hdl) 305optional, is called after all calls to 306.Va set_params , 307and 308.Va set_port , 309are done. 310A hardware driver that needs to get the hardware in and out of command 311mode for each change can save all the changes during previous calls and 312do them all here. 313Return 0 on success, otherwise an error code. 314.It Dv int init_output(void *hdl, void *buffer, int size) 315optional, is called before any output starts, but when the total 316.Va size 317of the output 318.Va buffer 319has been determined. 320It can be used to initialize looping DMA for hardware that needs that. 321Return 0 on success, otherwise an error code. 322.It Dv int init_input(void *hdl, void *buffer, int size) 323optional, is called before any input starts, but when the total 324.Va size 325of the input 326.Va buffer 327has been determined. 328It can be used to initialize looping DMA for hardware that needs that. 329Return 0 on success, otherwise an error code. 330.It Dv int start_output(void *hdl, void *block, int blksize, 331.Dv "void (*intr)(void*), void *intrarg)" 332.br 333is called to start the transfer of 334.Va blksize 335bytes from 336.Va block 337to the audio hardware. 338The call should return when the data transfer has been initiated 339(normally with DMA). 340When the hardware is ready to accept more samples the function 341.Va intr 342should be called with the argument 343.Va intrarg . 344Calling 345.Va intr 346will normally initiate another call to 347.Va start_output . 348Return 0 on success, otherwise an error code. 349.It Dv int start_input(void *hdl, void *block, int blksize, 350.Dv "void (*intr)(void*), void *intrarg)" 351.br 352is called to start the transfer of 353.Va blksize 354bytes to 355.Va block 356from the audio hardware. 357The call should return when the data transfer has been initiated 358(normally with DMA). 359When the hardware is ready to deliver more samples the function 360.Va intr 361should be called with the argument 362.Va intrarg . 363Calling 364.Va intr 365will normally initiate another call to 366.Va start_input . 367Return 0 on success, otherwise an error code. 368.It Dv int halt_output(void *hdl) 369is called to abort the output transfer (started by 370.Va start_output ) 371in progress. 372Return 0 on success, otherwise an error code. 373.It Dv int halt_input(void *hdl) 374is called to abort the input transfer (started by 375.Va start_input ) 376in progress. 377Return 0 on success, otherwise an error code. 378.It Dv int speaker_ctl(void *hdl, int on) 379optional, is called when a half duplex device changes between 380playing and recording. 381It can, e.g., be used to turn on 382and off the speaker. 383Return 0 on success, otherwise an error code. 384.It Dv int getdev(void *hdl, struct audio_device *ret) 385Should fill the 386.Va audio_device 387struct with relevant information about the driver. 388Return 0 on success, otherwise an error code. 389.It Dv int setfd(void *hdl, int fd) 390optional, is called when 391.Dv AUDIO_SETFD 392is used, but only if the device has AUDIO_PROP_FULLDUPLEX set. 393Return 0 on success, otherwise an error code. 394.It Dv int set_port(void *hdl, mixer_ctl_t *mc) 395is called in when 396.Dv AUDIO_MIXER_WRITE 397is used. 398It should take data from the 399.Va mixer_ctl_t 400struct at set the corresponding mixer values. 401Return 0 on success, otherwise an error code. 402.It Dv int get_port(void *hdl, mixer_ctl_t *mc) 403is called in when 404.Dv AUDIO_MIXER_READ 405is used. 406It should fill the 407.Va mixer_ctl_t 408struct. 409Return 0 on success, otherwise an error code. 410.It Dv int query_devinfo(void *hdl, mixer_devinfo_t *di) 411is called in when 412.Dv AUDIO_MIXER_DEVINFO 413is used. 414It should fill the 415.Va mixer_devinfo_t 416struct. 417Return 0 on success, otherwise an error code. 418.It Dv "void *allocm(void *hdl, int direction, size_t size, struct malloc_type *type, int flags)" 419.br 420optional, is called to allocate the device buffers. 421If not present 422.Xr malloc 9 423is used instead (with the same arguments but the first two). 424The reason for using a device dependent routine instead of 425.Xr malloc 9 426is that some buses need special allocation to do DMA. 427Returns the address of the buffer, or 0 on failure. 428.It Dv void freem(void *hdl, void *addr, struct malloc_type *type) 429optional, is called to free memory allocated by 430.Va alloc . 431If not supplied 432.Xr free 9 433is used. 434.It Dv size_t round_buffersize(void *hdl, int direction, size_t bufsize) 435optional, is called at startup to determine the audio 436buffer size. 437The upper layer supplies the suggested size in 438.Va bufsize , 439which the hardware driver can then change if needed. 440E.g., DMA on the ISA bus cannot exceed 65536 bytes. 441.It Dv "paddr_t mappage(void *hdl, void *addr, off_t offs, int prot)" 442.br 443optional, is called for 444.Xr mmap 2 . 445Should return the map value for the page at offset 446.Va offs 447from address 448.Va addr 449mapped with protection 450.Va prot . 451Returns -1 on failure, or a machine dependent opaque value 452on success. 453.It Dv int get_props(void *hdl) 454Should return the device properties; i.e., a combination of 455AUDIO_PROP_xxx. 456.It Dv int trigger_output(void *hdl, void *start, void *end, 457.Dv "int blksize, void (*intr)(void*), void *intrarg," 458.br 459.Dv "const audio_params_t *param)" 460.br 461optional, is called to start the transfer of data from the circular buffer 462delimited by 463.Va start 464and 465.Va end 466to the audio hardware, parameterized as in 467.Va param . 468The call should return when the data transfer has been initiated 469(normally with DMA). 470When the hardware is finished transferring each 471.Va blksize 472sized block, the function 473.Va intr 474should be called with the argument 475.Va intrarg 476(typically from the audio hardware interrupt service routine). 477Once started the transfer may be stopped using 478.Va halt_output . 479Return 0 on success, otherwise an error code. 480.It Dv int trigger_input(void *hdl, void *start, void *end, 481.Dv "int blksize, void (*intr)(void*), void *intrarg," 482.br 483.Dv "const audio_params_t *param)" 484.br 485optional, is called to start the transfer of data from the audio hardware, 486parameterized as in 487.Va param , 488to the circular buffer delimited by 489.Va start 490and 491.Va end . 492The call should return when the data transfer has been initiated 493(normally with DMA). 494When the hardware is finished transferring each 495.Va blksize 496sized block, the function 497.Va intr 498should be called with the argument 499.Va intrarg 500(typically from the audio hardware interrupt service routine). 501Once started the transfer may be stopped using 502.Va halt_input . 503Return 0 on success, otherwise an error code. 504.It Dv int dev_ioctl(void *hdl, u_long cmd, caddr_t addr, 505.br 506.Dv "int flag, struct lwp *l)" 507.br 508optional, is called when an 509.Xr ioctl 2 510is not recognized by the generic audio driver. 511Return 0 on success, otherwise an error code. 512.El 513.Pp 514The 515.Va query_devinfo 516method should define certain mixer controls for 517.Dv AUDIO_SETINFO 518to be able to change the port and gain, 519and 520.Dv AUDIO_GETINFO 521to read them, as follows. 522.Pp 523If the record mixer is capable of input from more than one source, 524it should define 525.Dv AudioNsource 526in class 527.Dv AudioCrecord . 528This mixer control should be of type 529.Dv AUDIO_MIXER_ENUM 530or 531.Dv AUDIO_MIXER_SET 532and enumerate the possible input sources. 533Each of the named sources for which the recording level can be set 534should have a control in the 535.Dv AudioCrecord 536class of type 537.Dv AUDIO_MIXER_VALUE , 538except the 539.Qq mixerout 540source is special, 541and will never have its own control. 542Its selection signifies, 543rather, 544that various sources in class 545.Dv AudioCrecord 546will be combined and presented to the single recording output 547in the same fashion that the sources of class 548.Dv AudioCinputs 549are combined and presented to the playback output(s). 550If the overall recording level can be changed, 551regardless of the input source, 552then this control should be named 553.Dv AudioNmaster 554and be of class 555.Dv AudioCrecord . 556.Pp 557Controls for various sources that affect only the playback output, 558as opposed to recording, 559should be in the 560.Dv AudioCinputs 561class, 562as of course should any controls that affect both playback and recording. 563.Pp 564If the play 565mixer is capable of output to more than one destination, 566it should define 567.Dv AudioNselect 568in class 569.Dv AudioCoutputs . 570This mixer control should be of type 571.Dv AUDIO_MIXER_ENUM 572or 573.Dv AUDIO_MIXER_SET 574and enumerate the possible destinations. 575For each of the named destinations for which the output level can be set, 576there should be 577a control in the 578.Dv AudioCoutputs 579class of type 580.Dv AUDIO_MIXER_VALUE . 581If the overall output level can be changed, 582which is invariably the case, 583then this control should be named 584.Dv AudioNmaster 585and be of class 586.Dv AudioCoutputs . 587.Pp 588There's one additional source recognized specially by 589.Dv AUDIO_SETINFO 590and 591.Dv AUDIO_GETINFO , 592to be presented as monitor_gain, 593and that is a control named 594.Dv AudioNmonitor , 595of class 596.Dv AudioCmonitor . 597.Sh SEE ALSO 598.Xr audio 4 599.Sh HISTORY 600This 601.Nm 602interface first appeared in 603.Nx 1.3 . 604