AUDIO(9)

AUDIO(9) | NetBSD Kernel Developer's Manual | AUDIO(9)

# NAME

audio — interface between low and high level audio drivers

# DESCRIPTION

The audio device driver is divided into a high level, hardware independent layer, and a low level hardware dependent layer. The interface between these is the audio_hw_if structure.

    struct audio_hw_if {
    	int	(*open)(void *, int);
    	void	(*close)(void *);
    	int	(*drain)(void *);

    	int	(*query_encoding)(void *, struct audio_encoding *);
    	int	(*set_params)(void *, int, int,
    		    audio_params_t *, audio_params_t *,
    		    stream_filter_list_t *, stream_filter_list_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	(*setfd)(void *, int);

    	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, struct malloc_type *, int);
    	void	(*freem)(void *, void *, struct malloc_type *);
    	size_t	(*round_buffersize)(void *, int, size_t);
    	paddr_t	(*mappage)(void *, void *, off_t, int);

    	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 **);
    };

    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;

The high level audio driver attaches to the low level driver when the latter calls audio_attach_mi. This call should be

        void
        audio_attach_mi(ahwp, hdl, dev)
    	struct audio_hw_if *ahwp;
    	void *hdl;
    	struct device *dev;

The audio_hw_if struct is as shown above. The hdl argument is a handle to some low level data structure. It is sent as the first argument to all the functions in audio_hw_if when the high level driver calls them. dev is the device struct for the hardware device.

The upper layer of the audio driver allocates one buffer for playing and one for recording. It handles the buffering of data from the user processes in these. The data is presented to the lower level in smaller chunks, called blocks. If, during playback, there is no data available from the user process when the hardware request another block a block of silence will be used instead. Furthermore, if the user process does not read data quickly enough during recording data will be thrown away.

The fields of audio_hw_if are described in some more detail below. Some fields are optional and can be set to 0 if not needed.

int open(void *hdl, int flags)
  optional, is called when the audio device is opened. It should initialize the hardware for I/O. Every successful call to open is matched by a call to close. Return 0 on success, otherwise an error code.
void close(void *hdl)
  optional, is called when the audio device is closed.
int drain(void *hdl)
  optional, is called before the device is closed or when AUDIO_DRAIN is called. It should make sure that no samples remain in to be played that could be lost when close is called. Return 0 on success, otherwise an error code.
int query_encoding(void *hdl, struct audio_encoding *ae)
  is used when AUDIO_GETENC is called. It should fill the audio_encoding structure and return 0 or, if there is no encoding with the given number, return EINVAL.
int set_params(void *hdl, int setmode, int usemode,
  audio_params_t *play, audio_params_t *rec,

  stream_filter_list_t *pfil, stream_filter_list_t *rfil)

  Called to set the audio encoding mode. setmode is a combination of the AUMODE_RECORD and AUMODE_PLAY flags to indicate which mode(s) are to be set. usemode is also a combination of these flags, but indicates the current mode of the device (i.e., the value of mode in the audio_info struct).

  The play and rec structures contain the encoding parameters that should be set. The values of the structures may also be modified if the hardware cannot be set to exactly the requested mode (e.g., if the requested sampling rate is not supported, but one close enough is).

  If the hardware requires software assistance with some encoding (e.g., it might be lacking mu-law support) it should fill the pfil for playing or rfil for recording with conversion information. For example, if play requests [8000Hz, mu-law, 8/8bit, 1ch] and the hardware does not support 8bit mu-law, but 16bit slinear_le, the driver should call pfil->append() with pfil, mulaw_to_slinear16, and audio_params_t representing [8000Hz, slinear_le, 16/16bit, 2ch]. If the driver needs multiple conversions, a conversion nearest to the hardware should be set to the head of pfil or rfil. The definition of stream_filter_list_t follows:

      typedef struct stream_filter_list {
      	void (*append)(struct stream_filter_list *,
      		       stream_filter_factory_t,
      		       const audio_params_t *);
      	void (*prepend)(struct stream_filter_list *,
      			stream_filter_factory_t,
      			const audio_params_t *);
      	void (*set)(struct stream_filter_list *, int,
      		    stream_filter_factory_t,
      		    const audio_params_t *);
      	int req_size;
      	struct stream_filter_req {
      		stream_filter_factory_t *factory;
      		audio_params_t param; /* from-param for recording,
      					 to-param for playing */
      	} filters[AUDIO_MAX_FILTERS];
      } stream_filter_list_t;

  For playing, pfil constructs conversions as follows:

      	(play) == write(2) input
      	  |	pfil->filters[pfil->req_size-1].factory
      	(pfil->filters[pfil->req_size-1].param)
      	  |	pfil->filters[pfil->req_size-2].factory
      	  :
      	  |	pfil->filters[1].factory
      	(pfil->filters[1].param)
      	  |	pfil->filters[0].factory
      	(pfil->filters[0].param)  == hardware input

  For recording, rfil constructs conversions as follows:

      	(rfil->filters[0].param) == hardware output
      	  |	rfil->filters[0].factory
      	(rfil->filters[1].param)
      	  |	rfil->filters[1].factory
      	  :
      	  |	rfil->filters[rfil->req_size-2].factory
      	(rfil->filters[rfil->req_size-1].param)
      	  |	rfil->filters[rfil->req_size-1].factory
      	(rec)  == read(2) output

  If the device does not have the AUDIO_PROP_INDEPENDENT property the same value is passed in both play and rec and the encoding parameters from play is copied into rec after the call to set_params. Return 0 on success, otherwise an error code.

int round_blocksize(void *hdl, int bs, int mode,
  const audio_params_t *param)

  optional, is called with the block size, bs, that has been computed by the upper layer, mode, AUMODE_PLAY or AUMODE_RECORD, and param, encoding parameters for the hardware. It should return a block size, possibly changed according to the needs of the hardware driver.

int commit_settings(void *hdl)
  optional, is called after all calls to set_params, 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 previous calls and do them all here. Return 0 on success, otherwise an error code.
int init_output(void *hdl, void *buffer, int size)
  optional, is 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.
int init_input(void *hdl, void *buffer, int size)
  optional, is called before any input starts, but when the total size of the input 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.
int start_output(void *hdl, void *block, int blksize,
  void (*intr)(void*), void *intrarg)

  is called to start the transfer of blksize bytes from block to the audio hardware. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is ready to accept more samples the function intr should be called with the argument intrarg. Calling intr will normally initiate another call to start_output. Return 0 on success, otherwise an error code.

int start_input(void *hdl, void *block, int blksize,
  void (*intr)(void*), void *intrarg)

  is called to start the transfer of blksize bytes to block from the audio hardware. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is ready to deliver more samples the function intr should be called with the argument intrarg. Calling intr will normally initiate another call to start_input. Return 0 on success, otherwise an error code.

int halt_output(void *hdl)
  is called to abort the output transfer (started by start_output) in progress. Return 0 on success, otherwise an error code.
int halt_input(void *hdl)
  is called to abort the input transfer (started by start_input) in progress. Return 0 on success, otherwise an error code.
int speaker_ctl(void *hdl, int on)
  optional, is called when a half duplex device changes between playing and recording. It can, e.g., be used to turn on and off the speaker. Return 0 on success, otherwise an error code.
int getdev(void *hdl, struct audio_device *ret)
  Should fill the audio_device struct with relevant information about the driver. Return 0 on success, otherwise an error code.
int setfd(void *hdl, int fd)
  optional, is called when AUDIO_SETFD is used, but only if the device has AUDIO_PROP_FULLDUPLEX set. Return 0 on success, otherwise an error code.
int set_port(void *hdl, mixer_ctrl_t *mc)
  is called in when AUDIO_MIXER_WRITE is used. It should take data from the mixer_ctrl_t struct at set the corresponding mixer values. Return 0 on success, otherwise an error code.
int get_port(void *hdl, mixer_ctrl_t *mc)
  is called in when AUDIO_MIXER_READ is used. It should fill the mixer_ctrl_t struct. Return 0 on success, otherwise an error code.
int query_devinfo(void *hdl, mixer_devinfo_t *di)
  is called in when AUDIO_MIXER_DEVINFO is used. It should fill the mixer_devinfo_t struct. Return 0 on success, otherwise an error code.
void *allocm(void *hdl, int direction, size_t size, struct malloc_type *type, int flags)

  optional, is called to allocate the device buffers. If not present malloc(9) is used instead (with the same arguments but the first two). The reason for using a device dependent routine instead of malloc(9) is that some buses need special allocation to do DMA. Returns the address of the buffer, or 0 on failure.

void freem(void *hdl, void *addr, struct malloc_type *type)
  optional, is called to free memory allocated by alloc. If not supplied free(9) is used.
size_t round_buffersize(void *hdl, int direction, size_t bufsize)
  optional, is called at startup to determine the audio buffer size. The upper layer supplies the suggested size in bufsize, which the hardware driver can then change if needed. E.g., DMA on the ISA bus cannot exceed 65536 bytes.
paddr_t mappage(void *hdl, void *addr, off_t offs, int prot)

  optional, is called for mmap(2). Should return the map value for the page at offset offs from address addr mapped with protection prot. Returns -1 on failure, or a machine dependent opaque value on success.

int get_props(void *hdl)
  Should return the device properties; i.e., a combination of AUDIO_PROP_xxx.
int trigger_output(void *hdl, void *start, void *end,
  int blksize, void (*intr)(void*), void *intrarg,

  const audio_params_t *param)

  optional, is called to start the transfer of data from the circular buffer delimited by start and end to the audio hardware, parameterized as in param. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is finished transferring each blksize sized block, the function intr should be called with the argument intrarg (typically from the audio hardware interrupt service routine). Once started the transfer may be stopped using halt_output. Return 0 on success, otherwise an error code.

int trigger_input(void *hdl, void *start, void *end,
  int blksize, void (*intr)(void*), void *intrarg,

  const audio_params_t *param)

  optional, is called to start the transfer of data from the audio hardware, parameterized as in param, to the circular buffer delimited by start and end. The call should return when the data transfer has been initiated (normally with DMA). When the hardware is finished transferring each blksize sized block, the function intr should be called with the argument intrarg (typically from the audio hardware interrupt service routine). Once started the transfer may be stopped using halt_input. Return 0 on success, otherwise an error code.

int dev_ioctl(void *hdl, u_long cmd, void *addr,

  int flag, struct lwp *l)

  optional, is called when an ioctl(2) is not recognized by the generic audio driver. Return 0 on success, otherwise an error code.

void get_locks(void *hdl, kmutex_t **intr, kmutex_t **thread)
  Returns the interrupt and thread locks to the common audio layer.

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.

# SEE ALSO

audio(4)

# HISTORY

This audio interface first appeared in NetBSD 1.3.

November 23, 2011 | NetBSD 5.99