xref: /dragonfly/sys/dev/sound/pcm/sound.h (revision 0720b42f)
1 /*-
2  * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3  * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
4  * Copyright (c) 1995 Hannu Savolainen
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: head/sys/dev/sound/pcm/sound.h 274035 2014-11-03 11:11:45Z bapt $
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/ioccom.h>
42 #include <sys/filio.h>
43 #include <sys/sockio.h>
44 #include <sys/fcntl.h>
45 #include <sys/proc.h>
46 #include <sys/kernel.h> /* for DATA_SET */
47 #include <sys/module.h>
48 #include <sys/conf.h>
49 #include <sys/file.h>
50 #include <sys/uio.h>
51 #include <sys/syslog.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
54 #include <sys/bus.h>
55 #include <sys/rman.h>
56 #include <sys/limits.h>
57 #include <sys/mman.h>
58 #include <sys/poll.h>
59 #include <sys/sbuf.h>
60 #include <sys/soundcard.h>
61 #include <sys/sysctl.h>
62 #include <sys/kobj.h>
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 
66 #include <sys/lock.h>
67 #include <sys/mplock2.h>
68 #include <sys/mutex.h>
69 #include <sys/condvar.h>
70 #include <sys/taskqueue.h>
71 
72 #ifndef KOBJMETHOD_END
73 #define KOBJMETHOD_END	{ NULL, NULL }
74 #endif
75 
76 struct pcm_channel;
77 struct pcm_feeder;
78 struct snd_dbuf;
79 struct snd_mixer;
80 
81 #include <dev/sound/pcm/buffer.h>
82 #include <dev/sound/pcm/matrix.h>
83 #include <dev/sound/pcm/matrix_map.h>
84 #include <dev/sound/pcm/channel.h>
85 #include <dev/sound/pcm/feeder.h>
86 #include <dev/sound/pcm/mixer.h>
87 #include <dev/sound/pcm/dsp.h>
88 #include <dev/sound/clone.h>
89 #include <dev/sound/unit.h>
90 
91 #define	PCM_SOFTC_SIZE	(sizeof(struct snddev_info))
92 
93 #define SND_STATUSLEN	64
94 
95 #define SOUND_MODVER	5
96 
97 #define SOUND_MINVER	SOUND_MODVER
98 #define SOUND_PREFVER	SOUND_MODVER
99 #define SOUND_MAXVER	SOUND_MODVER
100 
101 /*
102  * Minor number encoding: 0xFFFF00FF
103  *	unit		  0x000000FF
104  *	chan		  0x07FF0000
105  *	devtype		  0xF8000000
106  */
107 
108 #define SND_U_MASK	0x000000FF
109 #define SND_C_MASK	0x07FF0000
110 #define SND_D_MASK	0xF8000000
111 
112 #define PCMMAXUNIT	255
113 #define PCMMAXDEV	31
114 #define PCMMAXCHAN	2047
115 #define PCMMAXCLONE	PCMMAXCHAN
116 
117 /*
118  *
119  */
120 #define PCMUNIT(x)		(minor(x) & 0x00FF)
121 #define PCMSUBUNIT(x)		((minor(x) >> 16) & 0x07FF)
122 #define PCMDEV(x)		((minor(x) >> 27) & 0x001F)
123 
124 #define PCMUNIT_DEFAULT		0x00FF
125 
126 #define PCMMKMINOR(u, d, sub)	(snd_u2unit(u) | snd_c2unit(sub) | snd_d2unit(d))
127 
128 #define snd_unit2u(m)		((m) & 0x00FF)
129 #define snd_unit2c(m)		(((m) >> 16) & 0x07FF)
130 #define snd_unit2d(m)		(((m) >> 27) & 0x001F)
131 
132 #define snd_u2unit(u)		((u) & 0x00FF)
133 #define snd_c2unit(c)		(((c) & 0x07FF) << 16)
134 #define snd_d2unit(d)		(((d) & 0x001F) << 27)
135 
136 #define snd_mkunit(u, d, c)	(snd_u2unit(u) | snd_d2unit(d) | snd_c2unit(c))
137 
138 /*
139  * By design, limit possible channels for each direction.
140  */
141 #define SND_MAXHWCHAN		256
142 #define SND_MAXVCHANS		SND_MAXHWCHAN
143 
144 #define SD_F_SIMPLEX		0x00000001
145 #define SD_F_AUTOVCHAN		0x00000002
146 #define SD_F_SOFTPCMVOL		0x00000004
147 /*
148  * Obsolete due to better matrixing
149  */
150 #if 0
151 #define SD_F_PSWAPLR		0x00000008
152 #define SD_F_RSWAPLR		0x00000010
153 #endif
154 #define SD_F_DYING		0x00000008
155 #define SD_F_SUICIDE		0x00000010
156 #define SD_F_BUSY		0x00000020
157 #define SD_F_MPSAFE		0x00000040
158 #define SD_F_REGISTERED		0x00000080
159 #define SD_F_BITPERFECT		0x00000100
160 #define SD_F_VPC		0x00000200	/* volume-per-channel */
161 #define SD_F_EQ			0x00000400	/* EQ */
162 #define SD_F_EQ_ENABLED		0x00000800	/* EQ enabled */
163 #define SD_F_EQ_BYPASSED	0x00001000	/* EQ bypassed */
164 #define SD_F_EQ_PC		0x00002000	/* EQ per-channel */
165 
166 #define SD_F_EQ_DEFAULT		(SD_F_EQ | SD_F_EQ_ENABLED)
167 #define SD_F_EQ_MASK		(SD_F_EQ | SD_F_EQ_ENABLED |		\
168 				 SD_F_EQ_BYPASSED | SD_F_EQ_PC)
169 
170 #define SD_F_PRIO_RD		0x10000000
171 #define SD_F_PRIO_WR		0x20000000
172 #define SD_F_PRIO_SET		(SD_F_PRIO_RD | SD_F_PRIO_WR)
173 #define SD_F_DIR_SET		0x40000000
174 #define SD_F_TRANSIENT		0xf0000000
175 
176 #define SD_F_BITS		"\020"					\
177 				"\001SIMPLEX"				\
178 				"\002AUTOVCHAN"				\
179 				"\003SOFTPCMVOL"			\
180 				"\004DYING"				\
181 				"\005SUICIDE"				\
182 				"\006BUSY"				\
183 				"\007MPSAFE"				\
184 				"\010REGISTERED"			\
185 				"\011BITPERFECT"			\
186 				"\012VPC"				\
187 				"\013EQ"				\
188 				"\014EQ_ENABLED"			\
189 				"\015EQ_BYPASSED"			\
190 				"\016EQ_PC"				\
191 				"\035PRIO_RD"				\
192 				"\036PRIO_WR"				\
193 				"\037DIR_SET"
194 
195 #define PCM_ALIVE(x)		((x) != NULL && (x)->lock != NULL &&	\
196 				 !((x)->flags & SD_F_DYING))
197 #define PCM_REGISTERED(x)	(PCM_ALIVE(x) &&			\
198 				 ((x)->flags & SD_F_REGISTERED))
199 
200 /* many variables should be reduced to a range. Here define a macro */
201 #define RANGE(var, low, high) (var) = \
202 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
203 #define DSP_BUFFSIZE (8192)
204 
205 /* make figuring out what a format is easier. got AFMT_STEREO already */
206 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
207 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
208 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
209 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
210 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
211 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
212 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
213 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
214 			AFMT_S16_BE | AFMT_U16_BE)
215 
216 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
217 				 AFMT_32BIT)
218 
219 /* Supported vchan mixing formats */
220 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
221 
222 #define AFMT_PASSTHROUGH		AFMT_AC3
223 #define AFMT_PASSTHROUGH_RATE		48000
224 #define AFMT_PASSTHROUGH_CHANNEL	2
225 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
226 
227 /*
228  * We're simply using unused, contiguous bits from various AFMT_ definitions.
229  * ~(0xb00ff7ff)
230  */
231 #define AFMT_ENCODING_MASK	0xf00fffff
232 #define AFMT_CHANNEL_MASK	0x01f00000
233 #define AFMT_CHANNEL_SHIFT	20
234 #define AFMT_CHANNEL_MAX	0x7f
235 #define AFMT_EXTCHANNEL_MASK	0x0e000000
236 #define AFMT_EXTCHANNEL_SHIFT	25
237 
238 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
239 
240 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
241 				AFMT_EXTCHANNEL_SHIFT)
242 
243 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
244 				AFMT_CHANNEL_SHIFT)
245 
246 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
247 				(((v) & AFMT_24BIT) ? 24 :		\
248 				((((v) & AFMT_16BIT) ||			\
249 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
250 
251 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
252 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
253 
254 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
255 				(((c) << AFMT_CHANNEL_SHIFT) &		\
256 				AFMT_CHANNEL_MASK) |			\
257 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
258 				AFMT_EXTCHANNEL_MASK))
259 
260 #define AFMT_U8_NE	AFMT_U8
261 #define AFMT_S8_NE	AFMT_S8
262 
263 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
264 
265 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
266 			 AFMT_U24_NE | AFMT_U32_NE)
267 
268 /*
269  * Minor numbers for the sound driver.
270  *
271  * Unfortunately Creative called the codec chip of SB as a DSP. For this
272  * reason the /dev/dsp is reserved for digitized audio use. There is a
273  * device for true DSP processors but it will be called something else.
274  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
275  */
276 
277 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
278 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
279 #define SND_DEV_MIDIN	2	/* Raw midi access */
280 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
281 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
282 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
283 #define SND_DEV_STATUS	6	/* /dev/sndstat */
284 				/* #7 not in use now. */
285 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
286 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
287 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
288 #define SND_DEV_NORESET	10
289 
290 #define SND_DEV_DSPHW_PLAY	11	/* specific playback channel */
291 #define SND_DEV_DSPHW_VPLAY	12	/* specific virtual playback channel */
292 #define SND_DEV_DSPHW_REC	13	/* specific record channel */
293 #define SND_DEV_DSPHW_VREC	14	/* specific virtual record channel */
294 
295 #define SND_DEV_DSPHW_CD	15	/* s16le/stereo 44100Hz CD */
296 
297 /*
298  * OSSv4 compatible device. For now, it serve no purpose and
299  * the cloning itself will forward the request to ordinary /dev/dsp
300  * instead.
301  */
302 #define SND_DEV_DSP_MMAP	16	/* /dev/dsp_mmap     */
303 #define SND_DEV_DSP_AC3		17	/* /dev/dsp_ac3      */
304 #define SND_DEV_DSP_MULTICH	18	/* /dev/dsp_multich  */
305 #define SND_DEV_DSP_SPDIFOUT	19	/* /dev/dsp_spdifout */
306 #define SND_DEV_DSP_SPDIFIN	20	/* /dev/dsp_spdifin  */
307 
308 #define SND_DEV_LAST		SND_DEV_DSP_SPDIFIN
309 #define SND_DEV_MAX		PCMMAXDEV
310 
311 #define DSP_DEFAULT_SPEED	8000
312 
313 #define ON		1
314 #define OFF		0
315 
316 extern int pcm_veto_load;
317 extern int snd_unit;
318 extern int snd_maxautovchans;
319 extern int snd_verbose;
320 extern devclass_t pcm_devclass;
321 extern struct unrhdr *pcmsg_unrhdr;
322 
323 /*
324  * some macros for debugging purposes
325  * DDB/DEB to enable/disable debugging stuff
326  * BVDDB   to enable debugging when bootverbose
327  */
328 #define BVDDB(x) if (bootverbose) x
329 
330 #ifndef DEB
331 #define DEB(x)
332 #endif
333 
334 SYSCTL_DECL(_hw_snd);
335 
336 int pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num);
337 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
338     pid_t pid, char *comm, int devunit);
339 int pcm_chnrelease(struct pcm_channel *c);
340 int pcm_chnref(struct pcm_channel *c, int ref);
341 int pcm_inprog(struct snddev_info *d, int delta);
342 
343 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
344 int pcm_chn_destroy(struct pcm_channel *ch);
345 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
346 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
347 
348 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
349 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
350 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
351 int pcm_unregister(device_t dev);
352 int pcm_setstatus(device_t dev, char *str);
353 u_int32_t pcm_getflags(device_t dev);
354 void pcm_setflags(device_t dev, u_int32_t val);
355 void *pcm_getdevinfo(device_t dev);
356 
357 
358 int snd_setup_intr(device_t dev, struct resource *res, int flags,
359 		   driver_intr_t hand, void *param, void **cookiep);
360 
361 void *snd_mtxcreate(const char *desc, const char *type);
362 void snd_mtxfree(void *m);
363 void snd_mtxassert(void *m);
364 #define	snd_mtxlock(m) lockmgr(m, LK_EXCLUSIVE)
365 #define	snd_mtxunlock(m) lockmgr(m, LK_RELEASE)
366 
367 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
368 int sndstat_acquire(struct thread *td);
369 int sndstat_release(struct thread *td);
370 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
371 int sndstat_registerfile(char *str);
372 int sndstat_unregister(device_t dev);
373 int sndstat_unregisterfile(char *str);
374 
375 #define SND_DECLARE_FILE(version) \
376 	_SND_DECLARE_FILE(__LINE__, version)
377 
378 #define _SND_DECLARE_FILE(uniq, version) \
379 	__SND_DECLARE_FILE(uniq, version)
380 
381 #define __SND_DECLARE_FILE(uniq, version) \
382 	static char sndstat_vinfo[] = version; \
383 	SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
384 	SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
385 
386 /* usage of flags in device config entry (config file) */
387 #define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
388 #define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
389 
390 /* ought to be made obsolete but still used by mss */
391 #define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
392 #define	DV_F_DEV_SHIFT	8		/* force device type/class */
393 
394 /*
395  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
396  * so that the macro versions of pcm_{,un}lock can dereference them.
397  * we also have to do this now makedev() has gone away.
398  */
399 
400 struct snddev_info {
401 	struct {
402 		struct {
403 			SLIST_HEAD(, pcm_channel) head;
404 			struct {
405 				SLIST_HEAD(, pcm_channel) head;
406 			} busy;
407 			struct {
408 				SLIST_HEAD(, pcm_channel) head;
409 			} opened;
410 		} pcm;
411 	} channels;
412 	TAILQ_HEAD(dsp_cdevinfo_linkhead, dsp_cdevinfo) dsp_cdevinfo_pool;
413 	struct snd_clone *clones;
414 	unsigned devcount, playcount, reccount, pvchancount, rvchancount ;
415 	unsigned flags;
416 	int inprog;
417 	unsigned int bufsz;
418 	void *devinfo;
419 	device_t dev;
420 	char status[SND_STATUSLEN];
421 	struct lock *lock;
422 	struct cdev *mixer_dev;
423 	struct cdev *dsp_clonedev;
424 	uint32_t pvchanrate, pvchanformat;
425 	uint32_t rvchanrate, rvchanformat;
426 	int32_t eqpreamp;
427 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
428 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
429 	struct cv cv;
430 };
431 
432 void	sound_oss_sysinfo(oss_sysinfo *);
433 int	sound_oss_card_info(oss_card_info *);
434 
435 #define PCM_LOCKOWNED(d)	(lockstatus((d)->lock, curthread) == LK_EXCLUSIVE)
436 #define PCM_LOCK(d)		lockmgr((d)->lock, LK_EXCLUSIVE)
437 #define PCM_UNLOCK(d)		lockmgr((d)->lock, LK_RELEASE)
438 #define PCM_TRYLOCK(d)		lockmgr((d)->lock, LK_EXCLUSIVE|LK_NOWAIT)
439 #define PCM_LOCKASSERT(d)	KKASSERT(lockstatus((d)->lock, curthread) == LK_EXCLUSIVE)
440 #define PCM_UNLOCKASSERT(d)	KKASSERT(lockstatus((d)->lock, curthread) != LK_EXCLUSIVE)
441 
442 /*
443  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
444  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
445  */
446 #ifdef SND_DIAGNOSTIC
447 #define PCM_WAIT(x)		do {					\
448 	if (!PCM_LOCKOWNED(x))						\
449 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
450 		    __func__, __LINE__);				\
451 	while ((x)->flags & SD_F_BUSY) {				\
452 		if (snd_verbose > 3)					\
453 			device_printf((x)->dev,				\
454 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
455 			    __func__, __LINE__);			\
456 		cv_wait(&(x)->cv, (x)->lock);				\
457 	}								\
458 } while (0)
459 
460 #define PCM_ACQUIRE(x)		do {					\
461 	if (!PCM_LOCKOWNED(x))						\
462 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
463 		    __func__, __LINE__);				\
464 	if ((x)->flags & SD_F_BUSY)					\
465 		panic("%s(%d): [PCM ACQUIRE] "				\
466 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
467 	(x)->flags |= SD_F_BUSY;					\
468 } while (0)
469 
470 #define PCM_RELEASE(x)		do {					\
471 	if (!PCM_LOCKOWNED(x))						\
472 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
473 		    __func__, __LINE__);				\
474 	if ((x)->flags & SD_F_BUSY) {					\
475 		(x)->flags &= ~SD_F_BUSY;				\
476 		if ((x)->cv.cv_waiters != 0) {				\
477 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
478 				device_printf((x)->dev,			\
479 				    "%s(%d): [PCM RELEASE] "		\
480 				    "cv_waiters=%d > 1!\n",		\
481 				    __func__, __LINE__,			\
482 				    (x)->cv.cv_waiters);		\
483 			cv_broadcast(&(x)->cv);				\
484 		}							\
485 	} else								\
486 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
487 		    __func__, __LINE__);				\
488 } while (0)
489 
490 /* Quick version, for shorter path. */
491 #define PCM_ACQUIRE_QUICK(x)	do {					\
492 	if (PCM_LOCKOWNED(x))						\
493 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
494 		    __func__, __LINE__);				\
495 	PCM_LOCK(x);							\
496 	PCM_WAIT(x);							\
497 	PCM_ACQUIRE(x);							\
498 	PCM_UNLOCK(x);							\
499 } while (0)
500 
501 #define PCM_RELEASE_QUICK(x)	do {					\
502 	if (PCM_LOCKOWNED(x))						\
503 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
504 		    __func__, __LINE__);				\
505 	PCM_LOCK(x);							\
506 	PCM_RELEASE(x);							\
507 	PCM_UNLOCK(x);							\
508 } while (0)
509 
510 #define PCM_BUSYASSERT(x)	do {					\
511 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
512 		panic("%s(%d): [PCM BUSYASSERT] "			\
513 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
514 } while (0)
515 
516 #define PCM_GIANT_ENTER(x)	do {					\
517 	int _pcm_giant = 0;						\
518 	if (PCM_LOCKOWNED(x))						\
519 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
520 		    __func__, __LINE__);				\
521 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
522 		device_printf((x)->dev,					\
523 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
524 		    __func__, __LINE__);				\
525 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
526 		do {							\
527 			mtx_lock(&Giant);				\
528 			_pcm_giant = 1;					\
529 		} while (0)
530 
531 #define PCM_GIANT_EXIT(x)	do {					\
532 	if (PCM_LOCKOWNED(x))						\
533 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
534 		    __func__, __LINE__);				\
535 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
536 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
537 		    __func__, __LINE__);				\
538 	if ((x)->flags & SD_F_MPSAFE) {					\
539 		if (_pcm_giant == 1)					\
540 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
541 			    __func__, __LINE__);			\
542 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
543 			device_printf((x)->dev,				\
544 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
545 			    __func__, __LINE__);			\
546 	}								\
547 	if (_pcm_giant != 0) {						\
548 		if (mtx_owned(&Giant) == 0)				\
549 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
550 			    __func__, __LINE__);			\
551 		_pcm_giant = 0;						\
552 		mtx_unlock(&Giant);					\
553 	}								\
554 } while (0)
555 #else /* SND_DIAGNOSTIC */
556 #define PCM_WAIT(x)		do {					\
557 	PCM_LOCKASSERT(x);						\
558 	while ((x)->flags & SD_F_BUSY)					\
559 		cv_wait(&(x)->cv, (x)->lock);				\
560 } while (0)
561 
562 #define PCM_ACQUIRE(x)		do {					\
563 	PCM_LOCKASSERT(x);						\
564 	KASSERT(!((x)->flags & SD_F_BUSY),				\
565 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
566 	    __func__, __LINE__));					\
567 	(x)->flags |= SD_F_BUSY;					\
568 } while (0)
569 
570 #define PCM_RELEASE(x)		do {					\
571 	PCM_LOCKASSERT(x);						\
572 	KASSERT((x)->flags & SD_F_BUSY,					\
573 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
574 	    __func__, __LINE__));					\
575 	(x)->flags &= ~SD_F_BUSY;					\
576 	if ((x)->cv.cv_waiters != 0)					\
577 		cv_broadcast(&(x)->cv);					\
578 } while (0)
579 
580 /* Quick version, for shorter path. */
581 #define PCM_ACQUIRE_QUICK(x)	do {					\
582 	PCM_UNLOCKASSERT(x);						\
583 	PCM_LOCK(x);							\
584 	PCM_WAIT(x);							\
585 	PCM_ACQUIRE(x);							\
586 	PCM_UNLOCK(x);							\
587 } while (0)
588 
589 #define PCM_RELEASE_QUICK(x)	do {					\
590 	PCM_UNLOCKASSERT(x);						\
591 	PCM_LOCK(x);							\
592 	PCM_RELEASE(x);							\
593 	PCM_UNLOCK(x);							\
594 } while (0)
595 
596 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
597 				    ((x)->flags & SD_F_BUSY),		\
598 				    ("%s(%d): [PCM BUSYASSERT] "	\
599 				    "Failed, snddev_info=%p",		\
600 				    __func__, __LINE__, x))
601 
602 #define PCM_GIANT_ENTER(x)	do {					\
603 	if (!((x)->flags & SD_F_MPSAFE))				\
604 		get_mplock();						\
605 } while (0)
606 
607 #define PCM_GIANT_EXIT(x)	do {					\
608 	if (!((x)->flags & SD_F_MPSAFE))				\
609 		rel_mplock();						\
610 } while (0)
611 #endif /* !SND_DIAGNOSTIC */
612 
613 #define PCM_GIANT_LEAVE(x)	PCM_GIANT_EXIT(x)
614 
615 #ifdef KLD_MODULE
616 #define PCM_KLDSTRING(a) ("kld " # a)
617 #else
618 #define PCM_KLDSTRING(a) ""
619 #endif
620 
621 #endif /* _KERNEL */
622 
623 #endif	/* _OS_H_ */
624