audiovar.h revision 1.1.2.2 1 /* $NetBSD: audiovar.h,v 1.1.2.2 2019/05/04 07:20:09 isaki Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by TAMURA Kent
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1991-1993 Regents of the University of California.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the Computer Systems
47 * Engineering Group at Lawrence Berkeley Laboratory.
48 * 4. Neither the name of the University nor of the Laboratory may be used
49 * to endorse or promote products derived from this software without
50 * specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * From: Header: audiovar.h,v 1.3 93/07/18 14:07:25 mccanne Exp (LBL)
65 */
66
67 #ifndef _SYS_DEV_AUDIO_AUDIOVAR_H_
68 #define _SYS_DEV_AUDIO_AUDIOVAR_H_
69
70 #if defined(_KERNEL)
71 #include <sys/condvar.h>
72 #include <sys/proc.h>
73 #include <sys/queue.h>
74
75 #include <dev/audio/audio_if.h>
76 #include <dev/audio/audiofil.h>
77 #else
78 #include <stdint.h>
79 #include <stdbool.h>
80 #include "compat.h"
81 #include "userland.h"
82 #include "audiofil.h"
83 #endif /* _KERNEL */
84
85 /*
86 * Whether supports [US]LINEAR24/24 as userland format.
87 */
88 /* #define AUDIO_SUPPORT_LINEAR24 */
89
90 /*
91 * Frequency range.
92 * For lower limit, there are some antique machines who supports under
93 * 4000Hz, so that we accept 1000Hz as lower limit, regardless of
94 * practicality(?).
95 * For upper limit, there are some devices who supports 384000Hz, but
96 * I don't have them. :-)
97 */
98 #define AUDIO_MIN_FREQUENCY (1000)
99 #define AUDIO_MAX_FREQUENCY (192000)
100
101 typedef struct audio_file audio_file_t;
102 typedef struct audio_trackmixer audio_trackmixer_t;
103
104 /* ring buffer */
105 typedef struct {
106 audio_format2_t fmt; /* format */
107 int capacity; /* capacity by frame */
108 int head; /* head position in frame */
109 int used; /* used frame count */
110 void *mem; /* sample ptr */
111 } audio_ring_t;
112
113 #define AUDIO_N_PORTS 4
114
115 struct au_mixer_ports {
116 int index; /* index of port-selector mixerctl */
117 int master; /* index of master mixerctl */
118 int nports; /* number of selectable ports */
119 bool isenum; /* selector is enum type */
120 u_int allports; /* all aumasks or'd */
121 u_int aumask[AUDIO_N_PORTS]; /* exposed value of "ports" */
122 int misel [AUDIO_N_PORTS]; /* ord of port, for selector */
123 int miport[AUDIO_N_PORTS]; /* index of port's mixerctl */
124 bool isdual; /* has working mixerout */
125 int mixerout; /* ord of mixerout, for dual case */
126 int cur_port; /* the port that gain actually controls when
127 mixerout is selected, for dual case */
128 };
129
130 struct audio_softc {
131 /* Myself (e.g.; audio0, audio1, ...) */
132 device_t sc_dev;
133
134 /* Hardware device struct (e.g.; sb0, hdafg0, ...) */
135 device_t hw_dev;
136
137 /*
138 * Hardware interface and driver handle.
139 * hw_if == NULL means that the device is (attached but) disabled.
140 */
141 const struct audio_hw_if *hw_if;
142 void *hw_hdl;
143
144 /*
145 * List of opened descriptors.
146 * Must be protected by sc_intr_lock.
147 */
148 SLIST_HEAD(, audio_file) sc_files;
149
150 /*
151 * Blocksize in msec.
152 * Must be protected by sc_lock.
153 */
154 int sc_blk_ms;
155
156 /*
157 * Track mixer for playback and recording.
158 * If null, the mixer is disabled.
159 */
160 audio_trackmixer_t *sc_pmixer;
161 audio_trackmixer_t *sc_rmixer;
162
163 /*
164 * Opening track counter.
165 * Must be protected by sc_lock.
166 */
167 int sc_popens;
168 int sc_ropens;
169
170 /*
171 * true if the track mixer is running.
172 * Must be protected by sc_lock.
173 */
174 bool sc_pbusy;
175 bool sc_rbusy;
176
177 /*
178 * These four are the parameters sustained with /dev/sound.
179 * Must be protected by sc_lock.
180 */
181 audio_format2_t sc_sound_pparams;
182 audio_format2_t sc_sound_rparams;
183 bool sc_sound_ppause;
184 bool sc_sound_rpause;
185
186 /* recent info for /dev/sound */
187 /* XXX TODO */
188 struct audio_info sc_ai;
189
190 /*
191 * Playback(write)/Recording(read) selector.
192 * Must be protected by sc_lock.
193 */
194 struct selinfo sc_wsel;
195 struct selinfo sc_rsel;
196
197 /*
198 * processes who want mixer SIGIO.
199 * Must be protected by sc_lock.
200 */
201 struct mixer_asyncs {
202 struct mixer_asyncs *next;
203 pid_t pid;
204 } *sc_async_mixer;
205
206 /*
207 * Thread lock and interrupt lock obtained by get_locks().
208 */
209 kmutex_t *sc_lock;
210 kmutex_t *sc_intr_lock;
211
212 /*
213 * Critical section.
214 * Must be protected by sc_lock.
215 */
216 int sc_exlock;
217 kcondvar_t sc_exlockcv;
218
219 /*
220 * Must be protected by sc_lock (?)
221 */
222 bool sc_dying;
223
224 kauth_cred_t sc_cred;
225
226 struct sysctllog *sc_log;
227
228 mixer_ctrl_t *sc_mixer_state;
229 int sc_nmixer_states;
230 struct au_mixer_ports sc_inports;
231 struct au_mixer_ports sc_outports;
232 int sc_monitor_port;
233 u_int sc_lastgain;
234 };
235
236 #ifdef DIAGNOSTIC
237 #define DIAGNOSTIC_filter_arg(arg) audio_diagnostic_filter_arg(__func__, (arg))
238 #define DIAGNOSTIC_format2(fmt) audio_diagnostic_format2(__func__, (fmt))
239 extern void audio_diagnostic_filter_arg(const char *,
240 const audio_filter_arg_t *);
241 extern void audio_diagnostic_format2(const char *, const audio_format2_t *);
242 #else
243 #define DIAGNOSTIC_filter_arg(arg)
244 #define DIAGNOSTIC_format2(fmt)
245 #endif
246
247 /*
248 * Return true if 'fmt' is the internal format.
249 * It does not check for frequency and number of channels.
250 */
251 static __inline bool
252 audio_format2_is_internal(const audio_format2_t *fmt)
253 {
254
255 if (fmt->encoding != AUDIO_ENCODING_SLINEAR_NE)
256 return false;
257 if (fmt->precision != AUDIO_INTERNAL_BITS)
258 return false;
259 if (fmt->stride != AUDIO_INTERNAL_BITS)
260 return false;
261 return true;
262 }
263
264 /*
265 * Return true if fmt's encoding is one of LINEAR.
266 */
267 static __inline bool
268 audio_format2_is_linear(const audio_format2_t *fmt)
269 {
270 return (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE)
271 || (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE)
272 || (fmt->encoding == AUDIO_ENCODING_ULINEAR_LE)
273 || (fmt->encoding == AUDIO_ENCODING_ULINEAR_BE);
274 }
275
276 /*
277 * Return true if fmt's encoding is one of SLINEAR.
278 */
279 static __inline bool
280 audio_format2_is_signed(const audio_format2_t *fmt)
281 {
282 return (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE)
283 || (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE);
284 }
285
286 /*
287 * Return fmt's endian as LITTLE_ENDIAN or BIG_ENDIAN.
288 */
289 static __inline int
290 audio_format2_endian(const audio_format2_t *fmt)
291 {
292 if (fmt->stride == 8) {
293 /* HOST ENDIAN */
294 return BYTE_ORDER;
295 }
296
297 if (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE ||
298 fmt->encoding == AUDIO_ENCODING_ULINEAR_LE) {
299 return LITTLE_ENDIAN;
300 }
301 if (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE ||
302 fmt->encoding == AUDIO_ENCODING_ULINEAR_BE) {
303 return BIG_ENDIAN;
304 }
305 return BYTE_ORDER;
306 }
307
308 /* Interfaces for audiobell. */
309 struct audiobell_arg {
310 u_int sample_rate; /* IN */
311 u_int encoding; /* IN */
312 u_int channels; /* IN */
313 u_int precision; /* IN */
314 u_int blocksize; /* OUT */
315 audio_file_t *file; /* OUT */
316 };
317 int audiobellopen(dev_t, struct audiobell_arg *);
318 int audiobellclose(audio_file_t *);
319 int audiobellwrite(audio_file_t *, struct uio *);
320
321 #endif /* !_SYS_DEV_AUDIO_AUDIOVAR_H_ */
322