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