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