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