1 .\" $NetBSD: audio.9,v 1.41 2008/04/25 14:16:05 mjf Exp $
3 .\" Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Lennart Augustsson.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
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.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd interface between low and high level audio drivers
37 The audio device driver is divided into a high level,
38 hardware independent layer, and a low level hardware
40 The interface between these is the
45 int (*open)(void *, int);
46 void (*close)(void *);
49 int (*query_encoding)(void *, struct audio_encoding *);
50 int (*set_params)(void *, int, int,
51 audio_params_t *, audio_params_t *,
52 stream_filter_list_t *, stream_filter_list_t *);
53 int (*round_blocksize)(void *, int, int, const audio_params_t *);
55 int (*commit_settings)(void *);
57 int (*init_output)(void *, void *, int);
58 int (*init_input)(void *, void *, int);
59 int (*start_output)(void *, void *, int, void (*)(void *),
61 int (*start_input)(void *, void *, int, void (*)(void *),
63 int (*halt_output)(void *);
64 int (*halt_input)(void *);
66 int (*speaker_ctl)(void *, int);
70 int (*getdev)(void *, struct audio_device *);
71 int (*setfd)(void *, int);
73 int (*set_port)(void *, mixer_ctrl_t *);
74 int (*get_port)(void *, mixer_ctrl_t *);
76 int (*query_devinfo)(void *, mixer_devinfo_t *);
78 void *(*allocm)(void *, int, size_t, struct malloc_type *, int);
79 void (*freem)(void *, void *, struct malloc_type *);
80 size_t (*round_buffersize)(void *, int, size_t);
81 paddr_t (*mappage)(void *, void *, off_t, int);
83 int (*get_props)(void *);
85 int (*trigger_output)(void *, void *, void *, int,
86 void (*)(void *), void *, const audio_params_t *);
87 int (*trigger_input)(void *, void *, void *, int,
88 void (*)(void *), void *, const audio_params_t *);
89 int (*dev_ioctl)(void *, u_long, void *, int, struct lwp *);
90 int (*powerstate)(void *, int);
91 #define AUDIOPOWER_ON 1
92 #define AUDIOPOWER_OFF 0
95 typedef struct audio_params {
96 u_int sample_rate; /* sample rate */
97 u_int encoding; /* e.g. mu-law, linear, etc */
98 u_int precision; /* bits/subframe */
99 u_int validbits; /* valid bits in a subframe */
100 u_int channels; /* mono(1), stereo(2) */
104 The high level audio driver attaches to the low level driver
105 when the latter calls
106 .Va audio_attach_mi .
110 audio_attach_mi(ahwp, hdl, dev)
111 struct audio_hw_if *ahwp;
118 struct is as shown above.
121 argument is a handle to some low level data structure.
122 It is sent as the first argument to all the functions
125 when the high level driver calls them.
127 is the device struct for the hardware device.
129 The upper layer of the audio driver allocates one buffer for playing
130 and one for recording.
131 It handles the buffering of data from the user processes in these.
132 The data is presented to the lower level in smaller chunks, called blocks.
133 If, during playback, there is no data available from the user process when
134 the hardware request another block a block of silence will be used instead.
135 Furthermore, if the user process does not read data quickly enough during
136 recording data will be thrown away.
140 are described in some more detail below.
141 Some fields are optional and can be set to 0 if not needed.
142 .Bl -tag -width indent
143 .It Dv int open(void *hdl, int flags)
144 optional, is called when the audio device is opened.
145 It should initialize the hardware for I/O.
146 Every successful call to
148 is matched by a call to
150 Return 0 on success, otherwise an error code.
151 .It Dv void close(void *hdl)
152 optional, is called when the audio device is closed.
153 .It Dv int drain(void *hdl)
154 optional, is called before the device is closed or when
157 It should make sure that no samples remain in to be played that could
161 Return 0 on success, otherwise an error code.
162 .It Dv int query_encoding(void *hdl, struct audio_encoding *ae)
168 structure and return 0 or, if there is no encoding with the
169 given number, return EINVAL.
170 .It Dv int set_params(void *hdl, int setmode, int usemode,
171 .Dv "audio_params_t *play, audio_params_t *rec,"
173 .Dv "stream_filter_list_t *pfil, stream_filter_list_t *rfil)"
175 Called to set the audio encoding mode.
177 is a combination of the
181 flags to indicate which mode(s) are to be set.
183 is also a combination of these flags, but indicates the current
184 mode of the device (i.e., the value of
194 structures contain the encoding parameters that should be set.
195 The values of the structures may also be modified if the hardware
196 cannot be set to exactly the requested mode (e.g., if the requested
197 sampling rate is not supported, but one close enough is).
199 If the hardware requires software assistance with some encoding
200 (e.g., it might be lacking mu-law support) it should fill the
204 for recording with conversion information.
207 requests [8000Hz, mu-law, 8/8bit, 1ch] and the hardware does not
208 support 8bit mu-law, but 16bit slinear_le, the driver should call
209 .Dv pfil-\*[Gt]append()
212 .Va mulaw_to_slinear16 ,
213 and audio_params_t representing [8000Hz, slinear_le, 16/16bit, 2ch].
214 If the driver needs multiple conversions, a conversion nearest to the
215 hardware should be set to the head of
220 .Dv stream_filter_list_t
223 typedef struct stream_filter_list {
224 void (*append)(struct stream_filter_list *,
225 stream_filter_factory_t,
226 const audio_params_t *);
227 void (*prepend)(struct stream_filter_list *,
228 stream_filter_factory_t,
229 const audio_params_t *);
230 void (*set)(struct stream_filter_list *, int,
231 stream_filter_factory_t,
232 const audio_params_t *);
234 struct stream_filter_req {
235 stream_filter_factory_t *factory;
236 audio_params_t param; /* from-param for recording,
237 to-param for playing */
238 } filters[AUDIO_MAX_FILTERS];
239 } stream_filter_list_t;
244 constructs conversions as follows:
246 (play) == write(2) input
247 | pfil-\*[Gt]filters[pfil-\*[Gt]req_size-1].factory
248 (pfil-\*[Gt]filters[pfil-\*[Gt]req_size-1].param)
249 | pfil-\*[Gt]filters[pfil-\*[Gt]req_size-2].factory
251 | pfil-\*[Gt]filters[1].factory
252 (pfil-\*[Gt]filters[1].param)
253 | pfil-\*[Gt]filters[0].factory
254 (pfil-\*[Gt]filters[0].param) == hardware input
259 constructs conversions as follows:
261 (rfil-\*[Gt]filters[0].param) == hardware output
262 | rfil-\*[Gt]filters[0].factory
263 (rfil-\*[Gt]filters[1].param)
264 | rfil-\*[Gt]filters[1].factory
266 | rfil-\*[Gt]filters[rfil-\*[Gt]req_size-2].factory
267 (rfil-\*[Gt]filters[rfil-\*[Gt]req_size-1].param)
268 | rfil-\*[Gt]filters[rfil-\*[Gt]req_size-1].factory
269 (rec) == read(2) output
272 If the device does not have the
273 .Dv AUDIO_PROP_INDEPENDENT
274 property the same value is passed in both
278 and the encoding parameters from
284 Return 0 on success, otherwise an error code.
285 .It Dv int round_blocksize(void *hdl, int bs, int mode,
286 .Dv "const audio_params_t *param)"
288 optional, is called with the block size,
290 that has been computed by the upper layer,
297 encoding parameters for the hardware.
298 It should return a block size, possibly changed according to the needs
299 of the hardware driver.
300 .It Dv int commit_settings(void *hdl)
301 optional, is called after all calls to
306 A hardware driver that needs to get the hardware in and out of command
307 mode for each change can save all the changes during previous calls and
309 Return 0 on success, otherwise an error code.
310 .It Dv int init_output(void *hdl, void *buffer, int size)
311 optional, is called before any output starts, but when the total
316 It can be used to initialize looping DMA for hardware that needs that.
317 Return 0 on success, otherwise an error code.
318 .It Dv int init_input(void *hdl, void *buffer, int size)
319 optional, is called before any input starts, but when the total
324 It can be used to initialize looping DMA for hardware that needs that.
325 Return 0 on success, otherwise an error code.
326 .It Dv int start_output(void *hdl, void *block, int blksize,
327 .Dv "void (*intr)(void*), void *intrarg)"
329 is called to start the transfer of
333 to the audio hardware.
334 The call should return when the data transfer has been initiated
336 When the hardware is ready to accept more samples the function
338 should be called with the argument
342 will normally initiate another call to
344 Return 0 on success, otherwise an error code.
345 .It Dv int start_input(void *hdl, void *block, int blksize,
346 .Dv "void (*intr)(void*), void *intrarg)"
348 is called to start the transfer of
352 from the audio hardware.
353 The call should return when the data transfer has been initiated
355 When the hardware is ready to deliver more samples the function
357 should be called with the argument
361 will normally initiate another call to
363 Return 0 on success, otherwise an error code.
364 .It Dv int halt_output(void *hdl)
365 is called to abort the output transfer (started by
368 Return 0 on success, otherwise an error code.
369 .It Dv int halt_input(void *hdl)
370 is called to abort the input transfer (started by
373 Return 0 on success, otherwise an error code.
374 .It Dv int speaker_ctl(void *hdl, int on)
375 optional, is called when a half duplex device changes between
376 playing and recording.
377 It can, e.g., be used to turn on
379 Return 0 on success, otherwise an error code.
380 .It Dv int getdev(void *hdl, struct audio_device *ret)
383 struct with relevant information about the driver.
384 Return 0 on success, otherwise an error code.
385 .It Dv int setfd(void *hdl, int fd)
386 optional, is called when
388 is used, but only if the device has AUDIO_PROP_FULLDUPLEX set.
389 Return 0 on success, otherwise an error code.
390 .It Dv int set_port(void *hdl, mixer_ctrl_t *mc)
392 .Dv AUDIO_MIXER_WRITE
394 It should take data from the
396 struct at set the corresponding mixer values.
397 Return 0 on success, otherwise an error code.
398 .It Dv int get_port(void *hdl, mixer_ctrl_t *mc)
405 Return 0 on success, otherwise an error code.
406 .It Dv int query_devinfo(void *hdl, mixer_devinfo_t *di)
408 .Dv AUDIO_MIXER_DEVINFO
413 Return 0 on success, otherwise an error code.
414 .It Dv "void *allocm(void *hdl, int direction, size_t size, struct malloc_type *type, int flags)"
416 optional, is called to allocate the device buffers.
419 is used instead (with the same arguments but the first two).
420 The reason for using a device dependent routine instead of
422 is that some buses need special allocation to do DMA.
423 Returns the address of the buffer, or 0 on failure.
424 .It Dv void freem(void *hdl, void *addr, struct malloc_type *type)
425 optional, is called to free memory allocated by
430 .It Dv size_t round_buffersize(void *hdl, int direction, size_t bufsize)
431 optional, is called at startup to determine the audio
433 The upper layer supplies the suggested size in
435 which the hardware driver can then change if needed.
436 E.g., DMA on the ISA bus cannot exceed 65536 bytes.
437 .It Dv "paddr_t mappage(void *hdl, void *addr, off_t offs, int prot)"
439 optional, is called for
441 Should return the map value for the page at offset
445 mapped with protection
447 Returns -1 on failure, or a machine dependent opaque value
449 .It Dv int get_props(void *hdl)
450 Should return the device properties; i.e., a combination of
452 .It Dv int trigger_output(void *hdl, void *start, void *end,
453 .Dv "int blksize, void (*intr)(void*), void *intrarg,"
455 .Dv "const audio_params_t *param)"
457 optional, is called to start the transfer of data from the circular buffer
462 to the audio hardware, parameterized as in
464 The call should return when the data transfer has been initiated
466 When the hardware is finished transferring each
468 sized block, the function
470 should be called with the argument
472 (typically from the audio hardware interrupt service routine).
473 Once started the transfer may be stopped using
475 Return 0 on success, otherwise an error code.
476 .It Dv int trigger_input(void *hdl, void *start, void *end,
477 .Dv "int blksize, void (*intr)(void*), void *intrarg,"
479 .Dv "const audio_params_t *param)"
481 optional, is called to start the transfer of data from the audio hardware,
484 to the circular buffer delimited by
488 The call should return when the data transfer has been initiated
490 When the hardware is finished transferring each
492 sized block, the function
494 should be called with the argument
496 (typically from the audio hardware interrupt service routine).
497 Once started the transfer may be stopped using
499 Return 0 on success, otherwise an error code.
500 .It Dv int dev_ioctl(void *hdl, u_long cmd, void *addr,
502 .Dv "int flag, struct lwp *l)"
504 optional, is called when an
506 is not recognized by the generic audio driver.
507 Return 0 on success, otherwise an error code.
508 .It Dv int powerstate(void *hdl, int state)
510 optional, is called on the first open and last close of the audio
517 Returns 0 on success, otherwise an error code.
522 method should define certain mixer controls for
524 to be able to change the port and gain,
527 to read them, as follows.
529 If the record mixer is capable of input from more than one source,
534 This mixer control should be of type
538 and enumerate the possible input sources.
539 Each of the named sources for which the recording level can be set
540 should have a control in the
543 .Dv AUDIO_MIXER_VALUE ,
547 and will never have its own control.
548 Its selection signifies,
550 that various sources in class
552 will be combined and presented to the single recording output
553 in the same fashion that the sources of class
555 are combined and presented to the playback output(s).
556 If the overall recording level can be changed,
557 regardless of the input source,
558 then this control should be named
563 Controls for various sources that affect only the playback output,
564 as opposed to recording,
568 as of course should any controls that affect both playback and recording.
571 mixer is capable of output to more than one destination,
576 This mixer control should be of type
580 and enumerate the possible destinations.
581 For each of the named destinations for which the output level can be set,
586 .Dv AUDIO_MIXER_VALUE .
587 If the overall output level can be changed,
588 which is invariably the case,
589 then this control should be named
594 There's one additional source recognized specially by
598 to be presented as monitor_gain,
599 and that is a control named
608 interface first appeared in