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