xref: /freebsd/sys/dev/sound/pcm/sound.h (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
6  * Copyright (c) 1995 Hannu Savolainen
7  * All rights reserved.
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * first, include kernel header files.
33  */
34 
35 #ifndef _OS_H_
36 #define _OS_H_
37 
38 #ifdef _KERNEL
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/eventhandler.h>
42 #include <sys/ioccom.h>
43 #include <sys/filio.h>
44 #include <sys/sockio.h>
45 #include <sys/fcntl.h>
46 #include <sys/selinfo.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h> /* for DATA_SET */
49 #include <sys/module.h>
50 #include <sys/conf.h>
51 #include <sys/file.h>
52 #include <sys/uio.h>
53 #include <sys/syslog.h>
54 #include <sys/errno.h>
55 #include <sys/malloc.h>
56 #include <sys/bus.h>
57 #include <machine/resource.h>
58 #include <machine/bus.h>
59 #include <sys/rman.h>
60 #include <sys/limits.h>
61 #include <sys/mman.h>
62 #include <sys/poll.h>
63 #include <sys/sbuf.h>
64 #include <sys/soundcard.h>
65 #include <sys/sndstat.h>
66 #include <sys/sysctl.h>
67 #include <sys/kobj.h>
68 #include <vm/vm.h>
69 #include <vm/pmap.h>
70 
71 #include <sys/lock.h>
72 #include <sys/mutex.h>
73 #include <sys/condvar.h>
74 
75 #ifndef KOBJMETHOD_END
76 #define KOBJMETHOD_END	{ NULL, NULL }
77 #endif
78 
79 struct pcm_channel;
80 struct pcm_feeder;
81 struct snd_dbuf;
82 struct snd_mixer;
83 
84 #include <dev/sound/pcm/buffer.h>
85 #include <dev/sound/pcm/matrix.h>
86 #include <dev/sound/pcm/matrix_map.h>
87 #include <dev/sound/pcm/channel.h>
88 #include <dev/sound/pcm/feeder.h>
89 #include <dev/sound/pcm/mixer.h>
90 #include <dev/sound/pcm/dsp.h>
91 #include <dev/sound/clone.h>
92 #include <dev/sound/unit.h>
93 
94 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
95 
96 #define SND_STATUSLEN	64
97 
98 #define SOUND_MODVER	5
99 
100 #define SOUND_MINVER	SOUND_MODVER
101 #define SOUND_PREFVER	SOUND_MODVER
102 #define SOUND_MAXVER	SOUND_MODVER
103 
104 /*
105  * We're abusing the fact that MAXMINOR still have enough room
106  * for our bit twiddling and nobody ever need 512 unique soundcards,
107  * 32 unique device types and 1024 unique cloneable devices for the
108  * next 100 years...
109  */
110 
111 #define PCMMAXUNIT		(snd_max_u())
112 #define PCMMAXDEV		(snd_max_d())
113 #define PCMMAXCHAN		(snd_max_c())
114 
115 #define PCMMAXCLONE		PCMMAXCHAN
116 
117 #define PCMUNIT(x)		(snd_unit2u(dev2unit(x)))
118 #define PCMDEV(x)		(snd_unit2d(dev2unit(x)))
119 #define PCMCHAN(x)		(snd_unit2c(dev2unit(x)))
120 
121 /* XXX unit2minor compat */
122 #define PCMMINOR(x)	(x)
123 
124 /*
125  * By design, limit possible channels for each direction.
126  */
127 #define SND_MAXHWCHAN		256
128 #define SND_MAXVCHANS		SND_MAXHWCHAN
129 
130 #define SD_F_SIMPLEX		0x00000001
131 #define SD_F_AUTOVCHAN		0x00000002
132 #define SD_F_SOFTPCMVOL		0x00000004
133 #define SD_F_DYING		0x00000008
134 #define SD_F_DETACHING		0x00000010
135 #define SD_F_BUSY		0x00000020
136 #define SD_F_MPSAFE		0x00000040
137 #define SD_F_REGISTERED		0x00000080
138 #define SD_F_BITPERFECT		0x00000100
139 #define SD_F_VPC		0x00000200	/* volume-per-channel */
140 #define SD_F_EQ			0x00000400	/* EQ */
141 #define SD_F_EQ_ENABLED		0x00000800	/* EQ enabled */
142 #define SD_F_EQ_BYPASSED	0x00001000	/* EQ bypassed */
143 #define SD_F_EQ_PC		0x00002000	/* EQ per-channel */
144 
145 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
146 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
147 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
148 
149 #define SD_F_PRIO_RD		0x10000000
150 #define SD_F_PRIO_WR		0x20000000
151 #define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
152 #define SD_F_DIR_SET		0x40000000
153 #define SD_F_TRANSIENT		0xf0000000
154 
155 #define SD_F_BITS		"\020"					\
156 				"\001SIMPLEX"				\
157 				"\002AUTOVCHAN"				\
158 				"\003SOFTPCMVOL"			\
159 				"\004DYING"				\
160 				"\005DETACHING"				\
161 				"\006BUSY"				\
162 				"\007MPSAFE"				\
163 				"\010REGISTERED"			\
164 				"\011BITPERFECT"			\
165 				"\012VPC"				\
166 				"\013EQ"				\
167 				"\014EQ_ENABLED"			\
168 				"\015EQ_BYPASSED"			\
169 				"\016EQ_PC"				\
170 				"\035PRIO_RD"				\
171 				"\036PRIO_WR"				\
172 				"\037DIR_SET"
173 
174 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL &&	\
175 				 !((x)->flags & SD_F_DYING))
176 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) &&			\
177 				 ((x)->flags & SD_F_REGISTERED))
178 
179 #define	PCM_DETACHING(x)	((x)->flags & SD_F_DETACHING)
180 
181 /* many variables should be reduced to a range. Here define a macro */
182 #define RANGE(var, low, high) (var) = \
183 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
184 #define DSP_BUFFSIZE (8192)
185 
186 /* make figuring out what a format is easier. got AFMT_STEREO already */
187 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
188 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
189 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
190 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
191 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
192 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
193 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
194 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
195 			AFMT_S16_BE | AFMT_U16_BE)
196 
197 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
198 				 AFMT_32BIT)
199 
200 /* Supported vchan mixing formats */
201 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
202 
203 #define AFMT_PASSTHROUGH		AFMT_AC3
204 #define AFMT_PASSTHROUGH_RATE		48000
205 #define AFMT_PASSTHROUGH_CHANNEL	2
206 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
207 
208 /*
209  * We're simply using unused, contiguous bits from various AFMT_ definitions.
210  * ~(0xb00ff7ff)
211  */
212 #define AFMT_ENCODING_MASK	0xf00fffff
213 #define AFMT_CHANNEL_MASK	0x07f00000
214 #define AFMT_CHANNEL_SHIFT	20
215 #define AFMT_CHANNEL_MAX	0x7f
216 #define AFMT_EXTCHANNEL_MASK	0x08000000
217 #define AFMT_EXTCHANNEL_SHIFT	27
218 #define AFMT_EXTCHANNEL_MAX	1
219 
220 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
221 
222 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
223 				AFMT_EXTCHANNEL_SHIFT)
224 
225 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
226 				AFMT_CHANNEL_SHIFT)
227 
228 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
229 				(((v) & AFMT_24BIT) ? 24 :		\
230 				((((v) & AFMT_16BIT) ||			\
231 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
232 
233 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
234 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
235 
236 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
237 				(((c) << AFMT_CHANNEL_SHIFT) &		\
238 				AFMT_CHANNEL_MASK) |			\
239 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
240 				AFMT_EXTCHANNEL_MASK))
241 
242 #define AFMT_U8_NE	AFMT_U8
243 #define AFMT_S8_NE	AFMT_S8
244 
245 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
246 
247 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
248 			 AFMT_U24_NE | AFMT_U32_NE)
249 
250 /*
251  * Minor numbers for the sound driver.
252  *
253  * Unfortunately Creative called the codec chip of SB as a DSP. For this
254  * reason the /dev/dsp is reserved for digitized audio use. There is a
255  * device for true DSP processors but it will be called something else.
256  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
257  */
258 
259 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
260 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
261 #define SND_DEV_MIDIN	2	/* Raw midi access */
262 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
263 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
264 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
265 #define SND_DEV_STATUS	6	/* /dev/sndstat */
266 				/* #7 not in use now. */
267 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
268 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
269 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
270 #define SND_DEV_NORESET	10
271 
272 #define SND_DEV_DSPHW_PLAY	11	/* specific playback channel */
273 #define SND_DEV_DSPHW_VPLAY	12	/* specific virtual playback channel */
274 #define SND_DEV_DSPHW_REC	13	/* specific record channel */
275 #define SND_DEV_DSPHW_VREC	14	/* specific virtual record channel */
276 
277 #define SND_DEV_DSPHW_CD	15	/* s16le/stereo 44100Hz CD */
278 
279 /*
280  * OSSv4 compatible device. For now, it serve no purpose and
281  * the cloning itself will forward the request to ordinary /dev/dsp
282  * instead.
283  */
284 #define SND_DEV_DSP_MMAP	16	/* /dev/dsp_mmap     */
285 #define SND_DEV_DSP_AC3		17	/* /dev/dsp_ac3      */
286 #define SND_DEV_DSP_MULTICH	18	/* /dev/dsp_multich  */
287 #define SND_DEV_DSP_SPDIFOUT	19	/* /dev/dsp_spdifout */
288 #define SND_DEV_DSP_SPDIFIN	20	/* /dev/dsp_spdifin  */
289 
290 #define SND_DEV_LAST		SND_DEV_DSP_SPDIFIN
291 #define SND_DEV_MAX		PCMMAXDEV
292 
293 #define DSP_DEFAULT_SPEED	8000
294 
295 #define ON		1
296 #define OFF		0
297 
298 extern int pcm_veto_load;
299 extern int snd_unit;
300 extern int snd_maxautovchans;
301 extern int snd_verbose;
302 extern devclass_t pcm_devclass;
303 extern struct unrhdr *pcmsg_unrhdr;
304 
305 /*
306  * some macros for debugging purposes
307  * DDB/DEB to enable/disable debugging stuff
308  * BVDDB   to enable debugging when bootverbose
309  */
310 #define BVDDB(x) if (bootverbose) x
311 
312 #ifndef DEB
313 #define DEB(x)
314 #endif
315 
316 SYSCTL_DECL(_hw_snd);
317 
318 int pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num);
319 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
320     pid_t pid, char *comm, int devunit);
321 int pcm_chnrelease(struct pcm_channel *c);
322 int pcm_chnref(struct pcm_channel *c, int ref);
323 int pcm_inprog(struct snddev_info *d, int delta);
324 
325 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
326 int pcm_chn_destroy(struct pcm_channel *ch);
327 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
328 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
329 
330 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
331 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
332 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
333 int pcm_unregister(device_t dev);
334 int pcm_setstatus(device_t dev, char *str);
335 u_int32_t pcm_getflags(device_t dev);
336 void pcm_setflags(device_t dev, u_int32_t val);
337 void *pcm_getdevinfo(device_t dev);
338 
339 int snd_setup_intr(device_t dev, struct resource *res, int flags,
340 		   driver_intr_t hand, void *param, void **cookiep);
341 
342 void *snd_mtxcreate(const char *desc, const char *type);
343 void snd_mtxfree(void *m);
344 void snd_mtxassert(void *m);
345 #define	snd_mtxlock(m) mtx_lock(m)
346 #define	snd_mtxunlock(m) mtx_unlock(m)
347 
348 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
349 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
350 int sndstat_unregister(device_t dev);
351 
352 /* usage of flags in device config entry (config file) */
353 #define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
354 #define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
355 
356 /* ought to be made obsolete but still used by mss */
357 #define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
358 #define	DV_F_DEV_SHIFT	8		/* force device type/class */
359 
360 /*
361  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
362  * so that the macro versions of pcm_{,un}lock can dereference them.
363  * we also have to do this now makedev() has gone away.
364  */
365 
366 struct snddev_info {
367 	struct {
368 		struct {
369 			SLIST_HEAD(, pcm_channel) head;
370 			struct {
371 				SLIST_HEAD(, pcm_channel) head;
372 			} busy;
373 			struct {
374 				SLIST_HEAD(, pcm_channel) head;
375 			} opened;
376 		} pcm;
377 	} channels;
378 	TAILQ_HEAD(dsp_cdevinfo_linkhead, dsp_cdevinfo) dsp_cdevinfo_pool;
379 	struct snd_clone *clones;
380 	unsigned devcount, playcount, reccount, pvchancount, rvchancount ;
381 	unsigned flags;
382 	int inprog;
383 	unsigned int bufsz;
384 	void *devinfo;
385 	device_t dev;
386 	char status[SND_STATUSLEN];
387 	struct mtx *lock;
388 	struct cdev *mixer_dev;
389 	uint32_t pvchanrate, pvchanformat;
390 	uint32_t rvchanrate, rvchanformat;
391 	int32_t eqpreamp;
392 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
393 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
394 	struct cv cv;
395 };
396 
397 void	sound_oss_sysinfo(oss_sysinfo *);
398 int	sound_oss_card_info(oss_card_info *);
399 
400 #define	PCM_MODE_MIXER		0x01
401 #define	PCM_MODE_PLAY		0x02
402 #define	PCM_MODE_REC		0x04
403 
404 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
405 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
406 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
407 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
408 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
409 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
410 
411 /*
412  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
413  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
414  */
415 #ifdef SND_DIAGNOSTIC
416 #define PCM_WAIT(x)		do {					\
417 	if (!PCM_LOCKOWNED(x))						\
418 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
419 		    __func__, __LINE__);				\
420 	while ((x)->flags & SD_F_BUSY) {				\
421 		if (snd_verbose > 3)					\
422 			device_printf((x)->dev,				\
423 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
424 			    __func__, __LINE__);			\
425 		cv_wait(&(x)->cv, (x)->lock);				\
426 	}								\
427 } while (0)
428 
429 #define PCM_ACQUIRE(x)		do {					\
430 	if (!PCM_LOCKOWNED(x))						\
431 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
432 		    __func__, __LINE__);				\
433 	if ((x)->flags & SD_F_BUSY)					\
434 		panic("%s(%d): [PCM ACQUIRE] "				\
435 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
436 	(x)->flags |= SD_F_BUSY;					\
437 } while (0)
438 
439 #define PCM_RELEASE(x)		do {					\
440 	if (!PCM_LOCKOWNED(x))						\
441 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
442 		    __func__, __LINE__);				\
443 	if ((x)->flags & SD_F_BUSY) {					\
444 		(x)->flags &= ~SD_F_BUSY;				\
445 		if ((x)->cv.cv_waiters != 0) {				\
446 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
447 				device_printf((x)->dev,			\
448 				    "%s(%d): [PCM RELEASE] "		\
449 				    "cv_waiters=%d > 1!\n",		\
450 				    __func__, __LINE__,			\
451 				    (x)->cv.cv_waiters);		\
452 			cv_broadcast(&(x)->cv);				\
453 		}							\
454 	} else								\
455 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
456 		    __func__, __LINE__);				\
457 } while (0)
458 
459 /* Quick version, for shorter path. */
460 #define PCM_ACQUIRE_QUICK(x)	do {					\
461 	if (PCM_LOCKOWNED(x))						\
462 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
463 		    __func__, __LINE__);				\
464 	PCM_LOCK(x);							\
465 	PCM_WAIT(x);							\
466 	PCM_ACQUIRE(x);							\
467 	PCM_UNLOCK(x);							\
468 } while (0)
469 
470 #define PCM_RELEASE_QUICK(x)	do {					\
471 	if (PCM_LOCKOWNED(x))						\
472 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
473 		    __func__, __LINE__);				\
474 	PCM_LOCK(x);							\
475 	PCM_RELEASE(x);							\
476 	PCM_UNLOCK(x);							\
477 } while (0)
478 
479 #define PCM_BUSYASSERT(x)	do {					\
480 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
481 		panic("%s(%d): [PCM BUSYASSERT] "			\
482 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
483 } while (0)
484 
485 #define PCM_GIANT_ENTER(x)	do {					\
486 	int _pcm_giant = 0;						\
487 	if (PCM_LOCKOWNED(x))						\
488 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
489 		    __func__, __LINE__);				\
490 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
491 		device_printf((x)->dev,					\
492 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
493 		    __func__, __LINE__);				\
494 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
495 		do {							\
496 			mtx_lock(&Giant);				\
497 			_pcm_giant = 1;					\
498 		} while (0)
499 
500 #define PCM_GIANT_EXIT(x)	do {					\
501 	if (PCM_LOCKOWNED(x))						\
502 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
503 		    __func__, __LINE__);				\
504 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
505 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
506 		    __func__, __LINE__);				\
507 	if ((x)->flags & SD_F_MPSAFE) {					\
508 		if (_pcm_giant == 1)					\
509 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
510 			    __func__, __LINE__);			\
511 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
512 			device_printf((x)->dev,				\
513 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
514 			    __func__, __LINE__);			\
515 	}								\
516 	if (_pcm_giant != 0) {						\
517 		if (mtx_owned(&Giant) == 0)				\
518 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
519 			    __func__, __LINE__);			\
520 		_pcm_giant = 0;						\
521 		mtx_unlock(&Giant);					\
522 	}								\
523 } while (0)
524 #else /* SND_DIAGNOSTIC */
525 #define PCM_WAIT(x)		do {					\
526 	PCM_LOCKASSERT(x);						\
527 	while ((x)->flags & SD_F_BUSY)					\
528 		cv_wait(&(x)->cv, (x)->lock);				\
529 } while (0)
530 
531 #define PCM_ACQUIRE(x)		do {					\
532 	PCM_LOCKASSERT(x);						\
533 	KASSERT(!((x)->flags & SD_F_BUSY),				\
534 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
535 	    __func__, __LINE__));					\
536 	(x)->flags |= SD_F_BUSY;					\
537 } while (0)
538 
539 #define PCM_RELEASE(x)		do {					\
540 	PCM_LOCKASSERT(x);						\
541 	KASSERT((x)->flags & SD_F_BUSY,					\
542 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
543 	    __func__, __LINE__));					\
544 	(x)->flags &= ~SD_F_BUSY;					\
545 	if ((x)->cv.cv_waiters != 0)					\
546 		cv_broadcast(&(x)->cv);					\
547 } while (0)
548 
549 /* Quick version, for shorter path. */
550 #define PCM_ACQUIRE_QUICK(x)	do {					\
551 	PCM_UNLOCKASSERT(x);						\
552 	PCM_LOCK(x);							\
553 	PCM_WAIT(x);							\
554 	PCM_ACQUIRE(x);							\
555 	PCM_UNLOCK(x);							\
556 } while (0)
557 
558 #define PCM_RELEASE_QUICK(x)	do {					\
559 	PCM_UNLOCKASSERT(x);						\
560 	PCM_LOCK(x);							\
561 	PCM_RELEASE(x);							\
562 	PCM_UNLOCK(x);							\
563 } while (0)
564 
565 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
566 				    ((x)->flags & SD_F_BUSY),		\
567 				    ("%s(%d): [PCM BUSYASSERT] "	\
568 				    "Failed, snddev_info=%p",		\
569 				    __func__, __LINE__, x))
570 
571 #define PCM_GIANT_ENTER(x)	do {					\
572 	int _pcm_giant = 0;						\
573 	PCM_UNLOCKASSERT(x);						\
574 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
575 		do {							\
576 			mtx_lock(&Giant);				\
577 			_pcm_giant = 1;					\
578 		} while (0)
579 
580 #define PCM_GIANT_EXIT(x)	do {					\
581 	PCM_UNLOCKASSERT(x);						\
582 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
583 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
584 	    __func__, __LINE__));					\
585 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
586 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
587 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
588 	    __func__, __LINE__));					\
589 	if (_pcm_giant != 0) {						\
590 		mtx_assert(&Giant, MA_OWNED);				\
591 		_pcm_giant = 0;						\
592 		mtx_unlock(&Giant);					\
593 	}								\
594 } while (0)
595 #endif /* !SND_DIAGNOSTIC */
596 
597 #define PCM_GIANT_LEAVE(x)						\
598 	PCM_GIANT_EXIT(x);						\
599 } while (0)
600 
601 #ifdef KLD_MODULE
602 #define PCM_KLDSTRING(a) ("kld " # a)
603 #else
604 #define PCM_KLDSTRING(a) ""
605 #endif
606 
607 #endif /* _KERNEL */
608 
609 #endif	/* _OS_H_ */
610