audiovar.h revision 1.7 1 /* $NetBSD: audiovar.h,v 1.7 2020/01/11 04:53:10 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 #include <sys/condvar.h>
71 #include <sys/proc.h>
72 #include <sys/queue.h>
73
74 #include <dev/audio/audio_if.h>
75 #include <dev/audio/audiofil.h>
76
77 /*
78 * Whether supports [US]LINEAR24/24 as userland format.
79 */
80 /* #define AUDIO_SUPPORT_LINEAR24 */
81
82 /*
83 * Frequency range.
84 * For lower limit, there are some antique machines who supports under
85 * 4000Hz, so that we accept 1000Hz as lower limit, regardless of
86 * practicality(?).
87 * For upper limit, there are some devices who supports 384000Hz, but
88 * I don't have them. :-)
89 */
90 #define AUDIO_MIN_FREQUENCY (1000)
91 #define AUDIO_MAX_FREQUENCY (192000)
92
93 typedef struct audio_file audio_file_t;
94 typedef struct audio_trackmixer audio_trackmixer_t;
95
96 /* ring buffer */
97 typedef struct {
98 audio_format2_t fmt; /* format */
99 int capacity; /* capacity by frame */
100 int head; /* head position in frame */
101 int used; /* used frame count */
102 void *mem; /* sample ptr */
103 } audio_ring_t;
104
105 #define AUDIO_N_PORTS 4
106
107 struct au_mixer_ports {
108 int index; /* index of port-selector mixerctl */
109 int master; /* index of master mixerctl */
110 int nports; /* number of selectable ports */
111 bool isenum; /* selector is enum type */
112 u_int allports; /* all aumasks or'd */
113 u_int aumask[AUDIO_N_PORTS]; /* exposed value of "ports" */
114 int misel [AUDIO_N_PORTS]; /* ord of port, for selector */
115 int miport[AUDIO_N_PORTS]; /* index of port's mixerctl */
116 bool isdual; /* has working mixerout */
117 int mixerout; /* ord of mixerout, for dual case */
118 int cur_port; /* the port that gain actually controls when
119 mixerout is selected, for dual case */
120 };
121
122 struct audio_softc {
123 /* Myself (e.g.; audio0, audio1, ...) */
124 device_t sc_dev;
125
126 /* Hardware device struct (e.g.; sb0, hdafg0, ...) */
127 device_t hw_dev;
128
129 /*
130 * Hardware interface and driver handle.
131 * hw_if == NULL means that the device is (attached but) disabled.
132 */
133 const struct audio_hw_if *hw_if;
134 void *hw_hdl;
135
136 /*
137 * Properties obtained by get_props().
138 * No need any locks to read this variable.
139 */
140 int sc_props;
141
142 /*
143 * List of opened descriptors.
144 * Must be protected by sc_lock || sc_intr_lock for traversal(FOREACH).
145 * Must be protected by sc_lock && sc_intr_lock for insertion/removal.
146 */
147 SLIST_HEAD(, audio_file) sc_files;
148
149 /*
150 * Blocksize in msec.
151 * Must be protected by sc_lock.
152 */
153 int sc_blk_ms;
154
155 /*
156 * Track mixer for playback and recording.
157 * If null, the mixer is disabled.
158 */
159 audio_trackmixer_t *sc_pmixer;
160 audio_trackmixer_t *sc_rmixer;
161
162 /*
163 * Opening track counter.
164 * Must be protected by sc_lock.
165 */
166 int sc_popens;
167 int sc_ropens;
168
169 /*
170 * true if the track mixer is running.
171 * Must be protected by sc_lock.
172 */
173 bool sc_pbusy;
174 bool sc_rbusy;
175
176 /*
177 * These four are the parameters sustained with /dev/sound.
178 * Must be protected by sc_lock.
179 */
180 audio_format2_t sc_sound_pparams;
181 audio_format2_t sc_sound_rparams;
182 bool sc_sound_ppause;
183 bool sc_sound_rpause;
184
185 /* recent info for /dev/sound */
186 /* XXX TODO */
187 struct audio_info sc_ai;
188
189 /*
190 * Playback(write)/Recording(read) selector.
191 * Must be protected by sc_lock.
192 */
193 struct selinfo sc_wsel;
194 struct selinfo sc_rsel;
195
196 /*
197 * Processes who want mixer SIGIO.
198 * sc_am is an array of pids, or NULL if empty.
199 * sc_am_capacity is the number of allocated elements.
200 * sc_am_used is the number of elements actually used.
201 * Must be protected by sc_lock.
202 */
203 pid_t *sc_am;
204 int sc_am_capacity;
205 int sc_am_used;
206
207 /*
208 * Thread lock and interrupt lock obtained by get_locks().
209 */
210 kmutex_t *sc_lock;
211 kmutex_t *sc_intr_lock;
212
213 /*
214 * Critical section.
215 * Must be protected by sc_lock.
216 */
217 int sc_exlock;
218 kcondvar_t sc_exlockcv;
219
220 /*
221 * Must be protected by sc_lock (?)
222 */
223 bool sc_dying;
224
225 /*
226 * If multiuser is false, other users who have different euid
227 * than the first user cannot open this device.
228 * Must be protected by sc_lock.
229 */
230 bool sc_multiuser;
231 kauth_cred_t sc_cred;
232
233 struct sysctllog *sc_log;
234
235 mixer_ctrl_t *sc_mixer_state;
236 int sc_nmixer_states;
237 struct au_mixer_ports sc_inports;
238 struct au_mixer_ports sc_outports;
239 int sc_monitor_port;
240 u_int sc_lastgain;
241 };
242
243 #ifdef DIAGNOSTIC
244 #define DIAGNOSTIC_filter_arg(arg) audio_diagnostic_filter_arg(__func__, (arg))
245 #define DIAGNOSTIC_format2(fmt) audio_diagnostic_format2(__func__, (fmt))
246 extern void audio_diagnostic_filter_arg(const char *,
247 const audio_filter_arg_t *);
248 extern void audio_diagnostic_format2(const char *, const audio_format2_t *);
249 #else
250 #define DIAGNOSTIC_filter_arg(arg)
251 #define DIAGNOSTIC_format2(fmt)
252 #endif
253
254 /*
255 * Return true if 'fmt' is the internal format.
256 * It does not check for frequency and number of channels.
257 */
258 static __inline bool
259 audio_format2_is_internal(const audio_format2_t *fmt)
260 {
261
262 if (fmt->encoding != AUDIO_ENCODING_SLINEAR_NE)
263 return false;
264 if (fmt->precision != AUDIO_INTERNAL_BITS)
265 return false;
266 if (fmt->stride != AUDIO_INTERNAL_BITS)
267 return false;
268 return true;
269 }
270
271 /*
272 * Return true if fmt's encoding is one of LINEAR.
273 */
274 static __inline bool
275 audio_format2_is_linear(const audio_format2_t *fmt)
276 {
277 return (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE)
278 || (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE)
279 || (fmt->encoding == AUDIO_ENCODING_ULINEAR_LE)
280 || (fmt->encoding == AUDIO_ENCODING_ULINEAR_BE);
281 }
282
283 /*
284 * Return true if fmt's encoding is one of SLINEAR.
285 */
286 static __inline bool
287 audio_format2_is_signed(const audio_format2_t *fmt)
288 {
289 return (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE)
290 || (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE);
291 }
292
293 /*
294 * Return fmt's endian as LITTLE_ENDIAN or BIG_ENDIAN.
295 */
296 static __inline int
297 audio_format2_endian(const audio_format2_t *fmt)
298 {
299 if (fmt->stride == 8) {
300 /* HOST ENDIAN */
301 return BYTE_ORDER;
302 }
303
304 if (fmt->encoding == AUDIO_ENCODING_SLINEAR_LE ||
305 fmt->encoding == AUDIO_ENCODING_ULINEAR_LE) {
306 return LITTLE_ENDIAN;
307 }
308 if (fmt->encoding == AUDIO_ENCODING_SLINEAR_BE ||
309 fmt->encoding == AUDIO_ENCODING_ULINEAR_BE) {
310 return BIG_ENDIAN;
311 }
312 return BYTE_ORDER;
313 }
314
315 /* Interfaces for audiobell. */
316 int audiobellopen(dev_t, audio_file_t **);
317 int audiobellsetrate(audio_file_t *, u_int);
318 int audiobellclose(audio_file_t *);
319 int audiobellwrite(audio_file_t *, struct uio *);
320
321 #endif /* !_SYS_DEV_AUDIO_AUDIOVAR_H_ */
322