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