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