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