xref: /freebsd/sys/dev/sound/pcm/sound.h (revision 516a9c0212b003e1da0c6f4476dbe4f3f431606c)
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 };
229 
230 #define DSP_DEFAULT_SPEED	8000
231 
232 extern int snd_unit;
233 extern int snd_verbose;
234 extern devclass_t pcm_devclass;
235 extern struct unrhdr *pcmsg_unrhdr;
236 
237 #ifndef DEB
238 #define DEB(x)
239 #endif
240 
241 SYSCTL_DECL(_hw_snd);
242 
243 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
244     pid_t pid, char *comm);
245 
246 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
247 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
248 void pcm_init(device_t dev, void *devinfo);
249 int pcm_register(device_t dev, char *str);
250 int pcm_unregister(device_t dev);
251 u_int32_t pcm_getflags(device_t dev);
252 void pcm_setflags(device_t dev, u_int32_t val);
253 void *pcm_getdevinfo(device_t dev);
254 
255 int snd_setup_intr(device_t dev, struct resource *res, int flags,
256 		   driver_intr_t hand, void *param, void **cookiep);
257 
258 void *snd_mtxcreate(const char *desc, const char *type);
259 void snd_mtxfree(void *m);
260 void snd_mtxassert(void *m);
261 #define	snd_mtxlock(m) mtx_lock(m)
262 #define	snd_mtxunlock(m) mtx_unlock(m)
263 
264 int sndstat_register(device_t dev, char *str);
265 int sndstat_unregister(device_t dev);
266 
267 /* These are the function codes assigned to the children of sound cards. */
268 enum {
269 	SCF_PCM,
270 	SCF_MIDI,
271 	SCF_SYNTH,
272 };
273 
274 /*
275  * This is the device information struct, used by a bridge device to pass the
276  * device function code to the children.
277  */
278 struct sndcard_func {
279 	int func;	/* The function code. */
280 	void *varinfo;	/* Bridge-specific information. */
281 };
282 
283 /*
284  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
285  * so that the macro versions of pcm_{,un}lock can dereference them.
286  * we also have to do this now makedev() has gone away.
287  */
288 
289 struct snddev_info {
290 	struct {
291 		struct {
292 			SLIST_HEAD(, pcm_channel) head;
293 			struct {
294 				SLIST_HEAD(, pcm_channel) head;
295 			} busy;
296 			struct {
297 				SLIST_HEAD(, pcm_channel) head;
298 			} opened;
299 		} pcm;
300 	} channels;
301 	unsigned playcount, reccount, pvchancount, rvchancount;
302 	unsigned flags;
303 	unsigned int bufsz;
304 	void *devinfo;
305 	device_t dev;
306 	char status[SND_STATUSLEN];
307 	struct mtx *lock;
308 	struct cdev *mixer_dev;
309 	struct cdev *dsp_dev;
310 	uint32_t pvchanrate, pvchanformat;
311 	uint32_t rvchanrate, rvchanformat;
312 	int32_t eqpreamp;
313 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
314 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
315 	struct cv cv;
316 	struct unrhdr *p_unr;
317 	struct unrhdr *vp_unr;
318 	struct unrhdr *r_unr;
319 	struct unrhdr *vr_unr;
320 };
321 
322 void	sound_oss_sysinfo(oss_sysinfo *);
323 int	sound_oss_card_info(oss_card_info *);
324 
325 #define	PCM_MODE_MIXER		0x01
326 #define	PCM_MODE_PLAY		0x02
327 #define	PCM_MODE_REC		0x04
328 
329 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
330 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
331 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
332 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
333 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
334 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
335 
336 /*
337  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
338  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
339  */
340 #ifdef SND_DIAGNOSTIC
341 #define PCM_WAIT(x)		do {					\
342 	if (!PCM_LOCKOWNED(x))						\
343 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
344 		    __func__, __LINE__);				\
345 	while ((x)->flags & SD_F_BUSY) {				\
346 		if (snd_verbose > 3)					\
347 			device_printf((x)->dev,				\
348 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
349 			    __func__, __LINE__);			\
350 		cv_wait(&(x)->cv, (x)->lock);				\
351 	}								\
352 } while (0)
353 
354 #define PCM_ACQUIRE(x)		do {					\
355 	if (!PCM_LOCKOWNED(x))						\
356 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
357 		    __func__, __LINE__);				\
358 	if ((x)->flags & SD_F_BUSY)					\
359 		panic("%s(%d): [PCM ACQUIRE] "				\
360 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
361 	(x)->flags |= SD_F_BUSY;					\
362 } while (0)
363 
364 #define PCM_RELEASE(x)		do {					\
365 	if (!PCM_LOCKOWNED(x))						\
366 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
367 		    __func__, __LINE__);				\
368 	if ((x)->flags & SD_F_BUSY) {					\
369 		(x)->flags &= ~SD_F_BUSY;				\
370 		if ((x)->cv.cv_waiters != 0) {				\
371 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
372 				device_printf((x)->dev,			\
373 				    "%s(%d): [PCM RELEASE] "		\
374 				    "cv_waiters=%d > 1!\n",		\
375 				    __func__, __LINE__,			\
376 				    (x)->cv.cv_waiters);		\
377 			cv_broadcast(&(x)->cv);				\
378 		}							\
379 	} else								\
380 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
381 		    __func__, __LINE__);				\
382 } while (0)
383 
384 /* Quick version, for shorter path. */
385 #define PCM_ACQUIRE_QUICK(x)	do {					\
386 	if (PCM_LOCKOWNED(x))						\
387 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
388 		    __func__, __LINE__);				\
389 	PCM_LOCK(x);							\
390 	PCM_WAIT(x);							\
391 	PCM_ACQUIRE(x);							\
392 	PCM_UNLOCK(x);							\
393 } while (0)
394 
395 #define PCM_RELEASE_QUICK(x)	do {					\
396 	if (PCM_LOCKOWNED(x))						\
397 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
398 		    __func__, __LINE__);				\
399 	PCM_LOCK(x);							\
400 	PCM_RELEASE(x);							\
401 	PCM_UNLOCK(x);							\
402 } while (0)
403 
404 #define PCM_BUSYASSERT(x)	do {					\
405 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
406 		panic("%s(%d): [PCM BUSYASSERT] "			\
407 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
408 } while (0)
409 
410 #define PCM_GIANT_ENTER(x)	do {					\
411 	int _pcm_giant = 0;						\
412 	if (PCM_LOCKOWNED(x))						\
413 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
414 		    __func__, __LINE__);				\
415 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
416 		device_printf((x)->dev,					\
417 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
418 		    __func__, __LINE__);				\
419 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
420 		do {							\
421 			mtx_lock(&Giant);				\
422 			_pcm_giant = 1;					\
423 		} while (0)
424 
425 #define PCM_GIANT_EXIT(x)	do {					\
426 	if (PCM_LOCKOWNED(x))						\
427 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
428 		    __func__, __LINE__);				\
429 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
430 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
431 		    __func__, __LINE__);				\
432 	if ((x)->flags & SD_F_MPSAFE) {					\
433 		if (_pcm_giant == 1)					\
434 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
435 			    __func__, __LINE__);			\
436 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
437 			device_printf((x)->dev,				\
438 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
439 			    __func__, __LINE__);			\
440 	}								\
441 	if (_pcm_giant != 0) {						\
442 		if (mtx_owned(&Giant) == 0)				\
443 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
444 			    __func__, __LINE__);			\
445 		_pcm_giant = 0;						\
446 		mtx_unlock(&Giant);					\
447 	}								\
448 } while (0)
449 #else /* !SND_DIAGNOSTIC */
450 #define PCM_WAIT(x)		do {					\
451 	PCM_LOCKASSERT(x);						\
452 	while ((x)->flags & SD_F_BUSY)					\
453 		cv_wait(&(x)->cv, (x)->lock);				\
454 } while (0)
455 
456 #define PCM_ACQUIRE(x)		do {					\
457 	PCM_LOCKASSERT(x);						\
458 	KASSERT(!((x)->flags & SD_F_BUSY),				\
459 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
460 	    __func__, __LINE__));					\
461 	(x)->flags |= SD_F_BUSY;					\
462 } while (0)
463 
464 #define PCM_RELEASE(x)		do {					\
465 	PCM_LOCKASSERT(x);						\
466 	KASSERT((x)->flags & SD_F_BUSY,					\
467 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
468 	    __func__, __LINE__));					\
469 	(x)->flags &= ~SD_F_BUSY;					\
470 	if ((x)->cv.cv_waiters != 0)					\
471 		cv_broadcast(&(x)->cv);					\
472 } while (0)
473 
474 /* Quick version, for shorter path. */
475 #define PCM_ACQUIRE_QUICK(x)	do {					\
476 	PCM_UNLOCKASSERT(x);						\
477 	PCM_LOCK(x);							\
478 	PCM_WAIT(x);							\
479 	PCM_ACQUIRE(x);							\
480 	PCM_UNLOCK(x);							\
481 } while (0)
482 
483 #define PCM_RELEASE_QUICK(x)	do {					\
484 	PCM_UNLOCKASSERT(x);						\
485 	PCM_LOCK(x);							\
486 	PCM_RELEASE(x);							\
487 	PCM_UNLOCK(x);							\
488 } while (0)
489 
490 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
491 				    ((x)->flags & SD_F_BUSY),		\
492 				    ("%s(%d): [PCM BUSYASSERT] "	\
493 				    "Failed, snddev_info=%p",		\
494 				    __func__, __LINE__, x))
495 
496 #define PCM_GIANT_ENTER(x)	do {					\
497 	int _pcm_giant = 0;						\
498 	PCM_UNLOCKASSERT(x);						\
499 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
500 		do {							\
501 			mtx_lock(&Giant);				\
502 			_pcm_giant = 1;					\
503 		} while (0)
504 
505 #define PCM_GIANT_EXIT(x)	do {					\
506 	PCM_UNLOCKASSERT(x);						\
507 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
508 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
509 	    __func__, __LINE__));					\
510 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
511 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
512 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
513 	    __func__, __LINE__));					\
514 	if (_pcm_giant != 0) {						\
515 		mtx_assert(&Giant, MA_OWNED);				\
516 		_pcm_giant = 0;						\
517 		mtx_unlock(&Giant);					\
518 	}								\
519 } while (0)
520 #endif /* SND_DIAGNOSTIC */
521 
522 #define PCM_GIANT_LEAVE(x)						\
523 	PCM_GIANT_EXIT(x);						\
524 } while (0)
525 
526 #endif /* _KERNEL */
527 
528 #endif	/* _OS_H_ */
529