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