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