AUDIO(9)
π§ AUDIO(9) β the audio(9) page, Kernel Developer's Manual β¨
okay bestie, buckle up β this is the manual page for audio(9), and it's literally the whole interface between your low-level and high-level audio drivers π
πΈ NAME
audio β the interface between low and high level audio drivers. that's it, that's the vibe π
π DESCRIPTION
okay so the audio device driver is split into two whole layers, and yes it's giving main character energy:
- a high level, hardware independent layer π₯
- a low level, hardware dependent layer π
and the thing that lives between them β the glue, the moment, the bridge β is the audio_hw_if structure. here she is, in all her glory:
struct audio_hw_if {
int (*open)(void *, int);
void (*close)(void *);
int (*query_format)(void *, audio_format_query_t *);
int (*set_format)(void *, int,
const audio_params_t *, const audio_params_t *,
audio_filter_reg_t *, audio_filter_reg_t *);
int (*round_blocksize)(void *, int, int, const audio_params_t *);
int (*commit_settings)(void *);
int (*init_output)(void *, void *, int);
int (*init_input)(void *, void *, int);
int (*start_output)(void *, void *, int, void (*)(void *),
void *);
int (*start_input)(void *, void *, int, void (*)(void *),
void *);
int (*halt_output)(void *);
int (*halt_input)(void *);
int (*speaker_ctl)(void *, int);
#define SPKR_ON 1
#define SPKR_OFF 0
int (*getdev)(void *, struct audio_device *);
int (*set_port)(void *, mixer_ctrl_t *);
int (*get_port)(void *, mixer_ctrl_t *);
int (*query_devinfo)(void *, mixer_devinfo_t *);
void *(*allocm)(void *, int, size_t);
void (*freem)(void *, void *, size_t);
size_t (*round_buffersize)(void *, int, size_t);
int (*get_props)(void *);
int (*trigger_output)(void *, void *, void *, int,
void (*)(void *), void *, const audio_params_t *);
int (*trigger_input)(void *, void *, void *, int,
void (*)(void *), void *, const audio_params_t *);
int (*dev_ioctl)(void *, u_long, void *, int, struct lwp *);
void (*get_locks)(void *, kmutex_t **, kmutex_t **);
};
and then there's this lil cutie, the params struct π:
typedef struct audio_params {
u_int sample_rate; /* sample rate */
u_int encoding; /* e.g. mu-law, linear, etc */
u_int precision; /* bits/subframe */
u_int validbits; /* valid bits in a subframe */
u_int channels; /* mono(1), stereo(2) */
} audio_params_t;
π§ how the layers actually meet
the high level audio driver attaches to the low level driver when the low level driver calls audio_attach_mi. and that call should look exactly like this, no cap:
device_t
audio_attach_mi(const struct audio_hw_if *ahwp, void *hdl, device_t dev);
the audio_hw_if struct is the one up there β¬οΈ. the hdl argument is a handle to some low level data structure β and it gets sent as the first argument to every single function in audio_hw_if whenever the high level driver calls them. dev is the device struct for the hardware device. iykyk π
π¦ buffering, aka where your audio actually lives
the upper layer of the audio driver allocates one buffer for playing and one for recording π and it handles buffering data from the user processes in those. the data gets presented to the lower level in smaller chunks, and those chunks are called blocks.
now the spicy part πΆ β during playback, if there's no data available from the user process when the hardware requests another block, a block of silence gets used instead. and during recording, if the user process doesn't read data quickly enough, that data gets thrown away. gone. deleted. π
π the three phases (vibe check for function calls)
every function call happens in one of three phases β attach phase, closed phase, opened phase. let's break it down:
- Attach phase β this is during device attach, and it transits to the Closed phase when the attach succeeded β¨
- Closed phase β this is when no sampling device is opened, and it transits to the Opened phase when
opensucceeded π«Ά - Opened phase β this is when any sampling device is opened, and it transits to the Closed phase when
closesucceeded π
π the fields of audio_hw_if, explained with love
the fields get described in a lil more detail below. and listen β some of these fields are optional and can be set to NULL if you don't need them. no pressure bestie π
int open(void *hdl, int flags)
optional! this one's called when the first device combining playback and recording is opened.
- on a full duplex hardware,
(FREAD | FWRITE)is passed toflags - on a half duplex hardware,
FWRITEis passed for playback, orFREADfor recording
every successful call to open is matched by a call to close (they're literally soulmates π«Ά). return 0 on success, otherwise an error code. it's called in the Closed phase.
void close(void *hdl)
optional too! called when the last audio device combining playback and recording is closed. before this call happens, halt_input and halt_output are called if necessary. it's called in the Opened phase.
int query_format(void *hdl, audio_format_query_t *afp)
this one's called to enumerate the formats the hardware supports π¨. it should fill the audio_format_t structure according to the given number afp->index. if there's no format with that given number, return EINVAL. it can be called at any time β°
typedef struct audio_format_query {
u_int index;
struct audio_format fmt;
} audio_format_query_t;
it's also used by the upper layer to figure out the default format, like this:
- higher priority is preferred (normally
0, the highest is3, the lowest is0) AUDIO_ENCODING_SLINEAR_NE:16is preferred if it existsAUDIO_ENCODING_SLINEAR_OE:16is preferred if it exists- the format with more channels is preferred
so here's the tea β: if the driver supports SLINEAR_NE:16 and the upper layer chooses it, the driver does not need to provide a conversion function in set_format. similarly, if the driver supports SLINEAR_OE:16 and the upper layer chooses it, the driver does not need to provide a conversion function β because the upper layer supports conversion between SLINEAR_NE:16 and SLINEAR_OE:16 for convenience π. but if the upper layer chooses another format, the driver does need to provide a conversion function in set_format. see also set_format below π
and if the driver can't provide the conversion from/to SLINEAR_NE:16, set priority to -1. that means the hardware supports this format but the driver does not (e.g. AC3), and it will never be chosen. periodt π
int set_format(void *hdl, int setmode, const audio_params_t *play, const audio_params_t *rec, audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
called to set a specified format to the hardware, when the device is attached or the hardware format is changed π. setmode is a combination of the AUMODE_RECORD and AUMODE_PLAY flags, indicating which modes are to be set.
the play and rec structures contain the encoding parameters that should be set to the hardware. and here's the reassuring bit, bestie: all of these parameters are chosen from formats returned by query_format. therefore play and/or rec are always settable β
if the hardware doesn't support AUDIO_ENCODING_SLINEAR_{NE,OE}:16, conversion information should be filled into pfil for playing or rfil for recording. the definition of audio_filter_reg_t and its related structure look like this:
typedef struct {
const void *src;
const audio_format2_t *srcfmt;
void *dst;
const audio_format2_t *dstfmt;
int count;
void *context;
} audio_filter_arg_t;
typedef void(*audio_filter_t)(audio_filter_arg_t *arg);
typedef struct {
audio_filter_t codec;
void *context;
} audio_filter_reg_t;
codec is a conversion function, and context is an optional opaque pointer passed to codec π
when codec is called, all parameters required by codec are contained in arg:
srcpoints to the input buffer blocksrcfmtcontains the input encoding parametersdstpoints to the output buffer blockdstfmtcontains the output encoding parameterscountrepresents the number of frames to process on this call
src and dst are guaranteed to be able to consecutively access the number of frames specified by count. codec must fill the entirety of dst β no half-jobs, no shortcuts π
for example, let count = 100, srcfmt is { precision = 16, channels = 3 }, dstfmt is { precision = 8, channels = 4 }. in this case, src block length = 2(bytes) * 3(channels) * 100(frames) = 600 bytes, and the length to be written to dst block is 1(byte) * 4(channels) * 100(frames) = 400 bytes.
and here's the serious bit, so listen up π«Ά: codec cannot abort the conversion halfway and there is no error reporting mechanism. none. not a single one. so get it right the first time π
context is an opaque pointer that can be used by codec if necessary.
if the device does not have the AUDIO_PROP_INDEPENDENT property, the same value is passed in both play and rec. returns 0 on success, otherwise an error code. it's called in the Attach or Closed phases.
int round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
optional! π this gets called with:
- the block size
bs, which has been computed by the upper layer mode, which isAUMODE_PLAYorAUMODE_RECORDparam, the encoding parameters for the hardware
and heads up: the bs passed in is always non-zero and a multiple of the frame size represented by param->channels * param->precision / 8. it should return a block size β possibly changed according to the needs of the hardware driver. the return value also must be non-zero and a multiple of the frame size. it's called in the Attach or Closed phases.
int commit_settings(void *hdl)
optional! called after all the calls to set_format and set_port are done π§Ύ. a hardware driver that needs to get the hardware in and out of command mode for each change can save all the changes during the previous calls and do them all here β one big batch, one big slay β¨ returns 0 on success, otherwise an error code. it's called in the Attach or Closed phases.
int init_output(void *hdl, void *buffer, int size)
optional π¦ called before any output starts, but when the total size of the output buffer has been determined. it can be used to initialize looping DMA for hardware that needs that. return 0 on success, otherwise an error code. it's called in the Attach or Closed phases.
and that's where part 1 wraps... except no it doesn't, keep reading bestie, there's more coming π
int init_input(void *hdl, void *buffer, int size)β optional bestie, and it gets called before any input starts, but at the exact moment when the total size of the input buffer has been determined. It's the vibe-setter for hardware that needs looping DMA initialized early. Returns0on success, otherwise an error code. Runs in the Attach or Closed phases.int start_output(void *hdl, void *block, int blksize, void (*intr)(void*), void *intrarg)β gets called to start the transfer ofblksizebytes fromblockto the audio hardware π§. The call should return once the data transfer has been initiated (normally with DMA, obviously). When the hardware is ready to accept more samples, the functionintrshould be called with the argumentintrarg. And callingintrwill normally initiate another call tostart_outputβ yes it's a whole little loop, iykyk π. Returns0on success, otherwise an error code. This field is optional only if the driver doesn't support playback. Runs in the Opened phase.int start_input(void *hdl, void *block, int blksize, void (*intr)(void*), void *intrarg)β the β¨mirror imageβ¨: called to start the transfer ofblksizebytes toblockfrom the audio hardware. Same deal β the call should return once the data transfer has been initiated (normally with DMA). When the hardware is ready to deliver more samples,intrshould be called with the argumentintrarg. Callingintrwill normally initiate another call tostart_input. Returns0on success, otherwise an error code. Optional only if the driver doesn't support recording. Runs in the Opened phase.int halt_output(void *hdl)β the "stop, babe" button π. Called to abort the output transfer (the one started bystart_output) that's currently in progress. Returns0on success, otherwise an error code. Optional only if the driver doesn't support playback. Runs in the Opened phase.int halt_input(void *hdl)β same energy for the other direction: aborts the input transfer (started bystart_input) in progress. Returns0on success, otherwise an error code. Optional only if the driver doesn't support recording. Runs in the Opened phase.int speaker_ctl(void *hdl, int on)β optional! Called when a half duplex device flips between playing and recording π. Classic use case: turning the speaker on and off. Returns0on success, otherwise an error code. Runs in the Opened phase.int getdev(void *hdl, struct audio_device *ret)β should fill theaudio_devicestruct with the relevant info about the driver π. Returns0on success, otherwise an error code. Runs in the Opened phase.int set_port(void *hdl, mixer_ctrl_t *mc)β called in whenAUDIO_MIXER_WRITEis used. It should take data from themixer_ctrl_tstruct and set the corresponding mixer values ποΈ. Returns0on success, otherwise an error code. Runs in the Opened or Closed phases.int get_port(void *hdl, mixer_ctrl_t *mc)β called in whenAUDIO_MIXER_READis used. It should fill themixer_ctrl_tstruct. Returns0on success, otherwise an error code. Runs in the Opened or Closed phases.int query_devinfo(void *hdl, mixer_devinfo_t *di)β called in whenAUDIO_MIXER_DEVINFOis used. It should fill themixer_devinfo_tstruct. Returns0on success, otherwise an error code. This one runs at any time β°, main character energy honestly.void *allocm(void *hdl, int direction, size_t size)β optional, called to allocate the device buffers. If it's not present,malloc(9)is used instead (with the same arguments but the first two π). The whole reason for a device dependent routine instead ofmalloc(9)is that some buses need special allocation to do DMA. Returns the address of the buffer, orNULLon failure. Runs in the Attached or Closed phases.void freem(void *hdl, void *addr, size_t size)β optional, called to free memory allocated byallocm. If it's not supplied,free(9)is used. Runs in the Attached or Closed phases.size_t round_buffersize(void *hdl, int direction, size_t bufsize)β optional, called at startup to determine the audio buffer size π. The upper layer supplies the suggested size inbufsize, which the hardware driver can then change if needed. Real talk: e.g. DMA on the ISA bus cannot exceed65536bytes. Runs in the Attached or Closed phases.int get_props(void *hdl)β should return the device properties as a combination of the following flags π:AUDIO_PROP_PLAYBACKβ the device is capable of audio playback.AUDIO_PROP_CAPTUREβ the device is capable of audio capture.AUDIO_PROP_FULLDUPLEXβ the device admits full duplex operation. Don't set it if the device is unidirectional.AUDIO_PROP_INDEPENDENTβ the device can set the playing and recording encoding parameters independently. Don't set it if the device is unidirectional.AUDIO_PROP_MMAPβ is handled in the upper layer, so new drivers should not return this property. π
Runs in the Attach phase.
int trigger_output(void *hdl, void *start, void *end, int blksize, void (*intr)(void*), void *intrarg, const audio_params_t *param)β optional, called to start the transfer of data from the circular buffer delimited bystartandendto the audio hardware, parameterized as inparamπ. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is finished transferring eachblksizesized block, the functionintrshould be called with the argumentintrarg(typically from the audio hardware interrupt service routine). Once started, the transfer may be stopped usinghalt_output. Return0on success, otherwise an error code. Runs in the Opened phase.int trigger_input(void *hdl, void *start, void *end, int blksize, void (*intr)(void*), void *intrarg, const audio_params_t *param)β optional, the reverse cut π : called to start the transfer of data from the audio hardware, parameterized as inparam, to the circular buffer delimited bystartandend. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is finished transferring eachblksizesized block,intrshould be called with the argumentintrarg(typically from the audio hardware interrupt service routine). Once started, the transfer may be stopped usinghalt_input. Return0on success, otherwise an error code. Runs in the Opened phase.int dev_ioctl(void *hdl, u_long cmd, void *addr, int flag, struct lwp *l)β optional, called when anioctl(2)is not recognized by the generic audio driver. Return0on success, otherwise an error code. Runs in the Opened phase.void get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)β returns the interrupt and thread locks to the common audio layer π. Runs in the Attach phase.
ποΈ the mixer-control lore (still DESCRIPTION, we're just deep in it now)
The query_devinfo method should define certain mixer controls for AUDIO_SETINFO to be able to change the port and gain, and AUDIO_GETINFO to read them, as follows π
If the record mixer is capable of input from more than one source, it should define AudioNsource in class AudioCrecord. This mixer control should be of type AUDIO_MIXER_ENUM or AUDIO_MIXER_SET and enumerate the possible input sources. Each of the named sources for which the recording level can be set should have a control in the AudioCrecord class of type AUDIO_MIXER_VALUE, except the "mixerout" source is special, and will never have its own control. Its selection signifies, rather, that various sources in class AudioCrecord will be combined and presented to the single recording output in the same fashion that the sources of class AudioCinputs are combined and presented to the playback output(s) β¨. If the overall recording level can be changed, regardless of the input source, then this control should be named AudioNmaster and be of class AudioCrecord.
Controls for various sources that affect only the playback output, as opposed to recording, should be in the AudioCinputs class β as of course should any controls that affect both playback and recording π«Ά.
If the play mixer is capable of output to more than one destination, it should define AudioNselect in class AudioCoutputs. This mixer control should be of type AUDIO_MIXER_ENUM or AUDIO_MIXER_SET and enumerate the possible destinations. For each of the named destinations for which the output level can be set, there should be a control in the AudioCoutputs class of type AUDIO_MIXER_VALUE. If the overall output level can be changed β which is invariably the case β then this control should be named AudioNmaster and be of class AudioCoutputs π
There's one additional source recognized specially by AUDIO_SETINFO and AUDIO_GETINFO, to be presented as monitor_gain, and that is a control named AudioNmonitor, of class AudioCmonitor. That's the whole list, no cap π§
π SEE ALSO
π HISTORY
This audio interface first appeared in NetBSD 1.3. Yes she's an icon, she's a legend, she is the moment π
okay that's the whole page bestie, go forth and write drivers π This one came from the Debian crew and is dated February 2, 2021 β the page itself is AUDIO(9), section 9, and honestly? She ate. π
π¦ where this page comes from
- nixpkgs unstable: netbsd-source 11.0
- Copyright (c) 1999, 2000 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms