xref: /freebsd/sys/dev/sound/pcm/sound.h (revision 5ca8c28cd8c725b81781201cfdb5f9969396f934)
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 
92 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
93 
94 #define SND_STATUSLEN	64
95 
96 #define SOUND_MODVER	5
97 
98 #define SOUND_MINVER	SOUND_MODVER
99 #define SOUND_PREFVER	SOUND_MODVER
100 #define SOUND_MAXVER	SOUND_MODVER
101 
102 #define SND_MAXVCHANS		256
103 
104 #define SD_F_SIMPLEX		0x00000001
105 #define SD_F_AUTOVCHAN		0x00000002
106 #define SD_F_SOFTPCMVOL		0x00000004
107 #define SD_F_DYING		0x00000008
108 #define SD_F_DETACHING		0x00000010
109 #define SD_F_BUSY		0x00000020
110 #define SD_F_MPSAFE		0x00000040
111 #define SD_F_REGISTERED		0x00000080
112 #define SD_F_BITPERFECT		0x00000100
113 #define SD_F_VPC		0x00000200	/* volume-per-channel */
114 #define SD_F_EQ			0x00000400	/* EQ */
115 #define SD_F_EQ_ENABLED		0x00000800	/* EQ enabled */
116 #define SD_F_EQ_BYPASSED	0x00001000	/* EQ bypassed */
117 #define SD_F_EQ_PC		0x00002000	/* EQ per-channel */
118 
119 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
120 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
121 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
122 
123 #define SD_F_PRIO_RD		0x10000000
124 #define SD_F_PRIO_WR		0x20000000
125 
126 #define SD_F_BITS		"\020"					\
127 				"\001SIMPLEX"				\
128 				"\002AUTOVCHAN"				\
129 				"\003SOFTPCMVOL"			\
130 				"\004DYING"				\
131 				"\005DETACHING"				\
132 				"\006BUSY"				\
133 				"\007MPSAFE"				\
134 				"\010REGISTERED"			\
135 				"\011BITPERFECT"			\
136 				"\012VPC"				\
137 				"\013EQ"				\
138 				"\014EQ_ENABLED"			\
139 				"\015EQ_BYPASSED"			\
140 				"\016EQ_PC"				\
141 				"\035PRIO_RD"				\
142 				"\036PRIO_WR"
143 
144 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL &&	\
145 				 !((x)->flags & SD_F_DYING))
146 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) &&			\
147 				 ((x)->flags & SD_F_REGISTERED))
148 
149 #define	PCM_DETACHING(x)	((x)->flags & SD_F_DETACHING)
150 
151 #define	PCM_CHANCOUNT(d)	\
152 	(d->playcount + d->pvchancount + d->reccount + d->rvchancount)
153 
154 /* many variables should be reduced to a range. Here define a macro */
155 #define RANGE(var, low, high) (var) = \
156 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
157 
158 /* make figuring out what a format is easier. got AFMT_STEREO already */
159 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
160 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
161 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
162 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
163 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
164 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
165 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
166 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
167 			AFMT_S16_BE | AFMT_U16_BE)
168 
169 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
170 				 AFMT_32BIT)
171 
172 /* Supported vchan mixing formats */
173 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
174 
175 #define AFMT_PASSTHROUGH		AFMT_AC3
176 #define AFMT_PASSTHROUGH_RATE		48000
177 #define AFMT_PASSTHROUGH_CHANNEL	2
178 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
179 
180 /*
181  * We're simply using unused, contiguous bits from various AFMT_ definitions.
182  * ~(0xb00ff7ff)
183  */
184 #define AFMT_ENCODING_MASK	0xf00fffff
185 #define AFMT_CHANNEL_MASK	0x07f00000
186 #define AFMT_CHANNEL_SHIFT	20
187 #define AFMT_CHANNEL_MAX	0x7f
188 #define AFMT_EXTCHANNEL_MASK	0x08000000
189 #define AFMT_EXTCHANNEL_SHIFT	27
190 #define AFMT_EXTCHANNEL_MAX	1
191 
192 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
193 
194 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
195 				AFMT_EXTCHANNEL_SHIFT)
196 
197 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
198 				AFMT_CHANNEL_SHIFT)
199 
200 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
201 				(((v) & AFMT_24BIT) ? 24 :		\
202 				((((v) & AFMT_16BIT) ||			\
203 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
204 
205 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
206 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
207 
208 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
209 				(((c) << AFMT_CHANNEL_SHIFT) &		\
210 				AFMT_CHANNEL_MASK) |			\
211 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
212 				AFMT_EXTCHANNEL_MASK))
213 
214 #define AFMT_U8_NE	AFMT_U8
215 #define AFMT_S8_NE	AFMT_S8
216 
217 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
218 
219 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
220 			 AFMT_U24_NE | AFMT_U32_NE)
221 
222 enum {
223 	SND_DEV_CTL = 0,	/* Control port /dev/mixer */
224 	SND_DEV_SEQ,		/* Sequencer /dev/sequencer */
225 	SND_DEV_MIDIN,		/* Raw midi access */
226 	SND_DEV_DSP,		/* Digitized voice /dev/dsp */
227 	SND_DEV_STATUS,		/* /dev/sndstat */
228 	SND_DEV_DSPHW_PLAY,	/* specific playback channel */
229 	SND_DEV_DSPHW_VPLAY,	/* specific virtual playback channel */
230 	SND_DEV_DSPHW_REC,	/* specific record channel */
231 	SND_DEV_DSPHW_VREC,	/* specific virtual record channel */
232 };
233 
234 #define DSP_DEFAULT_SPEED	8000
235 
236 extern int pcm_veto_load;
237 extern int snd_unit;
238 extern int snd_verbose;
239 extern devclass_t pcm_devclass;
240 extern struct unrhdr *pcmsg_unrhdr;
241 
242 #ifndef DEB
243 #define DEB(x)
244 #endif
245 
246 SYSCTL_DECL(_hw_snd);
247 
248 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
249     pid_t pid, char *comm);
250 
251 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
252 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
253 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
254 int pcm_unregister(device_t dev);
255 int pcm_setstatus(device_t dev, char *str);
256 u_int32_t pcm_getflags(device_t dev);
257 void pcm_setflags(device_t dev, u_int32_t val);
258 void *pcm_getdevinfo(device_t dev);
259 
260 int snd_setup_intr(device_t dev, struct resource *res, int flags,
261 		   driver_intr_t hand, void *param, void **cookiep);
262 
263 void *snd_mtxcreate(const char *desc, const char *type);
264 void snd_mtxfree(void *m);
265 void snd_mtxassert(void *m);
266 #define	snd_mtxlock(m) mtx_lock(m)
267 #define	snd_mtxunlock(m) mtx_unlock(m)
268 
269 int sndstat_register(device_t dev, char *str);
270 int sndstat_unregister(device_t dev);
271 
272 /* These are the function codes assigned to the children of sound cards. */
273 enum {
274 	SCF_PCM,
275 	SCF_MIDI,
276 	SCF_SYNTH,
277 };
278 
279 /*
280  * This is the device information struct, used by a bridge device to pass the
281  * device function code to the children.
282  */
283 struct sndcard_func {
284 	int func;	/* The function code. */
285 	void *varinfo;	/* Bridge-specific information. */
286 };
287 
288 /*
289  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
290  * so that the macro versions of pcm_{,un}lock can dereference them.
291  * we also have to do this now makedev() has gone away.
292  */
293 
294 struct snddev_info {
295 	struct {
296 		struct {
297 			SLIST_HEAD(, pcm_channel) head;
298 			struct {
299 				SLIST_HEAD(, pcm_channel) head;
300 			} busy;
301 			struct {
302 				SLIST_HEAD(, pcm_channel) head;
303 			} opened;
304 		} pcm;
305 	} channels;
306 	unsigned playcount, reccount, pvchancount, rvchancount;
307 	unsigned flags;
308 	unsigned int bufsz;
309 	void *devinfo;
310 	device_t dev;
311 	char status[SND_STATUSLEN];
312 	struct mtx *lock;
313 	struct cdev *mixer_dev;
314 	struct cdev *dsp_dev;
315 	uint32_t pvchanrate, pvchanformat;
316 	uint32_t rvchanrate, rvchanformat;
317 	int32_t eqpreamp;
318 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
319 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
320 	struct cv cv;
321 	struct unrhdr *p_unr;
322 	struct unrhdr *vp_unr;
323 	struct unrhdr *r_unr;
324 	struct unrhdr *vr_unr;
325 };
326 
327 void	sound_oss_sysinfo(oss_sysinfo *);
328 int	sound_oss_card_info(oss_card_info *);
329 
330 #define	PCM_MODE_MIXER		0x01
331 #define	PCM_MODE_PLAY		0x02
332 #define	PCM_MODE_REC		0x04
333 
334 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
335 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
336 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
337 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
338 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
339 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
340 
341 /*
342  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
343  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
344  */
345 #ifdef SND_DIAGNOSTIC
346 #define PCM_WAIT(x)		do {					\
347 	if (!PCM_LOCKOWNED(x))						\
348 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
349 		    __func__, __LINE__);				\
350 	while ((x)->flags & SD_F_BUSY) {				\
351 		if (snd_verbose > 3)					\
352 			device_printf((x)->dev,				\
353 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
354 			    __func__, __LINE__);			\
355 		cv_wait(&(x)->cv, (x)->lock);				\
356 	}								\
357 } while (0)
358 
359 #define PCM_ACQUIRE(x)		do {					\
360 	if (!PCM_LOCKOWNED(x))						\
361 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
362 		    __func__, __LINE__);				\
363 	if ((x)->flags & SD_F_BUSY)					\
364 		panic("%s(%d): [PCM ACQUIRE] "				\
365 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
366 	(x)->flags |= SD_F_BUSY;					\
367 } while (0)
368 
369 #define PCM_RELEASE(x)		do {					\
370 	if (!PCM_LOCKOWNED(x))						\
371 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
372 		    __func__, __LINE__);				\
373 	if ((x)->flags & SD_F_BUSY) {					\
374 		(x)->flags &= ~SD_F_BUSY;				\
375 		if ((x)->cv.cv_waiters != 0) {				\
376 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
377 				device_printf((x)->dev,			\
378 				    "%s(%d): [PCM RELEASE] "		\
379 				    "cv_waiters=%d > 1!\n",		\
380 				    __func__, __LINE__,			\
381 				    (x)->cv.cv_waiters);		\
382 			cv_broadcast(&(x)->cv);				\
383 		}							\
384 	} else								\
385 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
386 		    __func__, __LINE__);				\
387 } while (0)
388 
389 /* Quick version, for shorter path. */
390 #define PCM_ACQUIRE_QUICK(x)	do {					\
391 	if (PCM_LOCKOWNED(x))						\
392 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
393 		    __func__, __LINE__);				\
394 	PCM_LOCK(x);							\
395 	PCM_WAIT(x);							\
396 	PCM_ACQUIRE(x);							\
397 	PCM_UNLOCK(x);							\
398 } while (0)
399 
400 #define PCM_RELEASE_QUICK(x)	do {					\
401 	if (PCM_LOCKOWNED(x))						\
402 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
403 		    __func__, __LINE__);				\
404 	PCM_LOCK(x);							\
405 	PCM_RELEASE(x);							\
406 	PCM_UNLOCK(x);							\
407 } while (0)
408 
409 #define PCM_BUSYASSERT(x)	do {					\
410 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
411 		panic("%s(%d): [PCM BUSYASSERT] "			\
412 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
413 } while (0)
414 
415 #define PCM_GIANT_ENTER(x)	do {					\
416 	int _pcm_giant = 0;						\
417 	if (PCM_LOCKOWNED(x))						\
418 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
419 		    __func__, __LINE__);				\
420 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
421 		device_printf((x)->dev,					\
422 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
423 		    __func__, __LINE__);				\
424 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
425 		do {							\
426 			mtx_lock(&Giant);				\
427 			_pcm_giant = 1;					\
428 		} while (0)
429 
430 #define PCM_GIANT_EXIT(x)	do {					\
431 	if (PCM_LOCKOWNED(x))						\
432 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
433 		    __func__, __LINE__);				\
434 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
435 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
436 		    __func__, __LINE__);				\
437 	if ((x)->flags & SD_F_MPSAFE) {					\
438 		if (_pcm_giant == 1)					\
439 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
440 			    __func__, __LINE__);			\
441 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
442 			device_printf((x)->dev,				\
443 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
444 			    __func__, __LINE__);			\
445 	}								\
446 	if (_pcm_giant != 0) {						\
447 		if (mtx_owned(&Giant) == 0)				\
448 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
449 			    __func__, __LINE__);			\
450 		_pcm_giant = 0;						\
451 		mtx_unlock(&Giant);					\
452 	}								\
453 } while (0)
454 #else /* !SND_DIAGNOSTIC */
455 #define PCM_WAIT(x)		do {					\
456 	PCM_LOCKASSERT(x);						\
457 	while ((x)->flags & SD_F_BUSY)					\
458 		cv_wait(&(x)->cv, (x)->lock);				\
459 } while (0)
460 
461 #define PCM_ACQUIRE(x)		do {					\
462 	PCM_LOCKASSERT(x);						\
463 	KASSERT(!((x)->flags & SD_F_BUSY),				\
464 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
465 	    __func__, __LINE__));					\
466 	(x)->flags |= SD_F_BUSY;					\
467 } while (0)
468 
469 #define PCM_RELEASE(x)		do {					\
470 	PCM_LOCKASSERT(x);						\
471 	KASSERT((x)->flags & SD_F_BUSY,					\
472 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
473 	    __func__, __LINE__));					\
474 	(x)->flags &= ~SD_F_BUSY;					\
475 	if ((x)->cv.cv_waiters != 0)					\
476 		cv_broadcast(&(x)->cv);					\
477 } while (0)
478 
479 /* Quick version, for shorter path. */
480 #define PCM_ACQUIRE_QUICK(x)	do {					\
481 	PCM_UNLOCKASSERT(x);						\
482 	PCM_LOCK(x);							\
483 	PCM_WAIT(x);							\
484 	PCM_ACQUIRE(x);							\
485 	PCM_UNLOCK(x);							\
486 } while (0)
487 
488 #define PCM_RELEASE_QUICK(x)	do {					\
489 	PCM_UNLOCKASSERT(x);						\
490 	PCM_LOCK(x);							\
491 	PCM_RELEASE(x);							\
492 	PCM_UNLOCK(x);							\
493 } while (0)
494 
495 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
496 				    ((x)->flags & SD_F_BUSY),		\
497 				    ("%s(%d): [PCM BUSYASSERT] "	\
498 				    "Failed, snddev_info=%p",		\
499 				    __func__, __LINE__, x))
500 
501 #define PCM_GIANT_ENTER(x)	do {					\
502 	int _pcm_giant = 0;						\
503 	PCM_UNLOCKASSERT(x);						\
504 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
505 		do {							\
506 			mtx_lock(&Giant);				\
507 			_pcm_giant = 1;					\
508 		} while (0)
509 
510 #define PCM_GIANT_EXIT(x)	do {					\
511 	PCM_UNLOCKASSERT(x);						\
512 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
513 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
514 	    __func__, __LINE__));					\
515 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
516 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
517 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
518 	    __func__, __LINE__));					\
519 	if (_pcm_giant != 0) {						\
520 		mtx_assert(&Giant, MA_OWNED);				\
521 		_pcm_giant = 0;						\
522 		mtx_unlock(&Giant);					\
523 	}								\
524 } while (0)
525 #endif /* SND_DIAGNOSTIC */
526 
527 #define PCM_GIANT_LEAVE(x)						\
528 	PCM_GIANT_EXIT(x);						\
529 } while (0)
530 
531 #endif /* _KERNEL */
532 
533 #endif	/* _OS_H_ */
534