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