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: stable/10/sys/dev/sound/pcm/sound.h 358879 2020-03-11 08:26:11Z hselasky $
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 #define SD_F_DYING		0x00000008
139 #define SD_F_DETACHING		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 				"\005DETACHING"				\
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 #define	PCM_DETACHING(x)	((x)->flags & SD_F_DETACHING)
185 
186 /* many variables should be reduced to a range. Here define a macro */
187 #define RANGE(var, low, high) (var) = \
188 	(((var)<(low))? (low) : ((var)>(high))? (high) : (var))
189 #define DSP_BUFFSIZE (8192)
190 
191 /* make figuring out what a format is easier. got AFMT_STEREO already */
192 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
193 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
194 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
195 #define AFMT_G711  (AFMT_MU_LAW | AFMT_A_LAW)
196 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8)
197 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
198 			AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
199 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
200 			AFMT_S16_BE | AFMT_U16_BE)
201 
202 #define AFMT_CONVERTIBLE	(AFMT_8BIT | AFMT_16BIT | AFMT_24BIT |	\
203 				 AFMT_32BIT)
204 
205 /* Supported vchan mixing formats */
206 #define AFMT_VCHAN		(AFMT_CONVERTIBLE & ~AFMT_G711)
207 
208 #define AFMT_PASSTHROUGH		AFMT_AC3
209 #define AFMT_PASSTHROUGH_RATE		48000
210 #define AFMT_PASSTHROUGH_CHANNEL	2
211 #define AFMT_PASSTHROUGH_EXTCHANNEL	0
212 
213 /*
214  * We're simply using unused, contiguous bits from various AFMT_ definitions.
215  * ~(0xb00ff7ff)
216  */
217 #define AFMT_ENCODING_MASK	0xf00fffff
218 #define AFMT_CHANNEL_MASK	0x07f00000
219 #define AFMT_CHANNEL_SHIFT	20
220 #define AFMT_CHANNEL_MAX	0x7f
221 #define AFMT_EXTCHANNEL_MASK	0x08000000
222 #define AFMT_EXTCHANNEL_SHIFT	27
223 #define AFMT_EXTCHANNEL_MAX	1
224 
225 #define AFMT_ENCODING(v)	((v) & AFMT_ENCODING_MASK)
226 
227 #define AFMT_EXTCHANNEL(v)	(((v) & AFMT_EXTCHANNEL_MASK) >>	\
228 				AFMT_EXTCHANNEL_SHIFT)
229 
230 #define AFMT_CHANNEL(v)		(((v) & AFMT_CHANNEL_MASK) >>		\
231 				AFMT_CHANNEL_SHIFT)
232 
233 #define AFMT_BIT(v)		(((v) & AFMT_32BIT) ? 32 :		\
234 				(((v) & AFMT_24BIT) ? 24 :		\
235 				((((v) & AFMT_16BIT) ||			\
236 				((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
237 
238 #define AFMT_BPS(v)		(AFMT_BIT(v) >> 3)
239 #define AFMT_ALIGN(v)		(AFMT_BPS(v) * AFMT_CHANNEL(v))
240 
241 #define SND_FORMAT(f, c, e)	(AFMT_ENCODING(f) |		\
242 				(((c) << AFMT_CHANNEL_SHIFT) &		\
243 				AFMT_CHANNEL_MASK) |			\
244 				(((e) << AFMT_EXTCHANNEL_SHIFT) &	\
245 				AFMT_EXTCHANNEL_MASK))
246 
247 #define AFMT_U8_NE	AFMT_U8
248 #define AFMT_S8_NE	AFMT_S8
249 
250 #define AFMT_SIGNED_NE	(AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
251 
252 #define AFMT_NE		(AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE |	\
253 			 AFMT_U24_NE | AFMT_U32_NE)
254 
255 /*
256  * Minor numbers for the sound driver.
257  *
258  * Unfortunately Creative called the codec chip of SB as a DSP. For this
259  * reason the /dev/dsp is reserved for digitized audio use. There is a
260  * device for true DSP processors but it will be called something else.
261  * In v3.0 it's /dev/sndproc but this could be a temporary solution.
262  */
263 
264 #define SND_DEV_CTL	0	/* Control port /dev/mixer */
265 #define SND_DEV_SEQ	1	/* Sequencer /dev/sequencer */
266 #define SND_DEV_MIDIN	2	/* Raw midi access */
267 #define SND_DEV_DSP	3	/* Digitized voice /dev/dsp */
268 #define SND_DEV_AUDIO	4	/* Sparc compatible /dev/audio */
269 #define SND_DEV_DSP16	5	/* Like /dev/dsp but 16 bits/sample */
270 #define SND_DEV_STATUS	6	/* /dev/sndstat */
271 				/* #7 not in use now. */
272 #define SND_DEV_SEQ2	8	/* /dev/sequencer, level 2 interface */
273 #define SND_DEV_SNDPROC 9	/* /dev/sndproc for programmable devices */
274 #define SND_DEV_PSS	SND_DEV_SNDPROC /* ? */
275 #define SND_DEV_NORESET	10
276 
277 #define SND_DEV_DSPHW_PLAY	11	/* specific playback channel */
278 #define SND_DEV_DSPHW_VPLAY	12	/* specific virtual playback channel */
279 #define SND_DEV_DSPHW_REC	13	/* specific record channel */
280 #define SND_DEV_DSPHW_VREC	14	/* specific virtual record channel */
281 
282 #define SND_DEV_DSPHW_CD	15	/* s16le/stereo 44100Hz CD */
283 
284 /*
285  * OSSv4 compatible device. For now, it serve no purpose and
286  * the cloning itself will forward the request to ordinary /dev/dsp
287  * instead.
288  */
289 #define SND_DEV_DSP_MMAP	16	/* /dev/dsp_mmap     */
290 #define SND_DEV_DSP_AC3		17	/* /dev/dsp_ac3      */
291 #define SND_DEV_DSP_MULTICH	18	/* /dev/dsp_multich  */
292 #define SND_DEV_DSP_SPDIFOUT	19	/* /dev/dsp_spdifout */
293 #define SND_DEV_DSP_SPDIFIN	20	/* /dev/dsp_spdifin  */
294 
295 #define SND_DEV_LAST		SND_DEV_DSP_SPDIFIN
296 #define SND_DEV_MAX		PCMMAXDEV
297 
298 #define DSP_DEFAULT_SPEED	8000
299 
300 #define ON		1
301 #define OFF		0
302 
303 extern int pcm_veto_load;
304 extern int snd_unit;
305 extern int snd_maxautovchans;
306 extern int snd_verbose;
307 extern devclass_t pcm_devclass;
308 extern struct unrhdr *pcmsg_unrhdr;
309 
310 /*
311  * some macros for debugging purposes
312  * DDB/DEB to enable/disable debugging stuff
313  * BVDDB   to enable debugging when bootverbose
314  */
315 #define BVDDB(x) if (bootverbose) x
316 
317 #ifndef DEB
318 #define DEB(x)
319 #endif
320 
321 SYSCTL_DECL(_hw_snd);
322 
323 int pcm_setvchans(struct snddev_info *d, int direction, int newcnt, int num);
324 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction,
325     pid_t pid, char *comm, int devunit);
326 int pcm_chnrelease(struct pcm_channel *c);
327 int pcm_chnref(struct pcm_channel *c, int ref);
328 int pcm_inprog(struct snddev_info *d, int delta);
329 
330 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
331 int pcm_chn_destroy(struct pcm_channel *ch);
332 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
333 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
334 
335 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
336 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
337 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
338 int pcm_unregister(device_t dev);
339 int pcm_setstatus(device_t dev, char *str);
340 u_int32_t pcm_getflags(device_t dev);
341 void pcm_setflags(device_t dev, u_int32_t val);
342 void *pcm_getdevinfo(device_t dev);
343 
344 
345 int snd_setup_intr(device_t dev, struct resource *res, int flags,
346 		   driver_intr_t hand, void *param, void **cookiep);
347 
348 void *snd_mtxcreate(const char *desc, const char *type);
349 void snd_mtxfree(void *m);
350 void snd_mtxassert(void *m);
351 #define	snd_mtxlock(m) mtx_lock(m)
352 #define	snd_mtxunlock(m) mtx_unlock(m)
353 
354 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
355 int sndstat_acquire(struct thread *td);
356 int sndstat_release(struct thread *td);
357 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
358 int sndstat_registerfile(char *str);
359 int sndstat_unregister(device_t dev);
360 int sndstat_unregisterfile(char *str);
361 
362 #define SND_DECLARE_FILE(version) \
363 	_SND_DECLARE_FILE(__LINE__, version)
364 
365 #define _SND_DECLARE_FILE(uniq, version) \
366 	__SND_DECLARE_FILE(uniq, version)
367 
368 #define __SND_DECLARE_FILE(uniq, version) \
369 	static char sndstat_vinfo[] = version; \
370 	SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
371 	SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
372 
373 /* usage of flags in device config entry (config file) */
374 #define DV_F_DRQ_MASK	0x00000007	/* mask for secondary drq */
375 #define	DV_F_DUAL_DMA	0x00000010	/* set to use secondary dma channel */
376 
377 /* ought to be made obsolete but still used by mss */
378 #define	DV_F_DEV_MASK	0x0000ff00	/* force device type/class */
379 #define	DV_F_DEV_SHIFT	8		/* force device type/class */
380 
381 /*
382  * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
383  * so that the macro versions of pcm_{,un}lock can dereference them.
384  * we also have to do this now makedev() has gone away.
385  */
386 
387 struct snddev_info {
388 	struct {
389 		struct {
390 			SLIST_HEAD(, pcm_channel) head;
391 			struct {
392 				SLIST_HEAD(, pcm_channel) head;
393 			} busy;
394 			struct {
395 				SLIST_HEAD(, pcm_channel) head;
396 			} opened;
397 		} pcm;
398 	} channels;
399 	TAILQ_HEAD(dsp_cdevinfo_linkhead, dsp_cdevinfo) dsp_cdevinfo_pool;
400 	struct snd_clone *clones;
401 	unsigned devcount, playcount, reccount, pvchancount, rvchancount ;
402 	unsigned flags;
403 	int inprog;
404 	unsigned int bufsz;
405 	void *devinfo;
406 	device_t dev;
407 	char status[SND_STATUSLEN];
408 	struct mtx *lock;
409 	struct cdev *mixer_dev;
410 	uint32_t pvchanrate, pvchanformat;
411 	uint32_t rvchanrate, rvchanformat;
412 	int32_t eqpreamp;
413 	struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
414 	struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
415 	struct cv cv;
416 };
417 
418 void	sound_oss_sysinfo(oss_sysinfo *);
419 int	sound_oss_card_info(oss_card_info *);
420 
421 #define PCM_LOCKOWNED(d)	mtx_owned((d)->lock)
422 #define	PCM_LOCK(d)		mtx_lock((d)->lock)
423 #define	PCM_UNLOCK(d)		mtx_unlock((d)->lock)
424 #define PCM_TRYLOCK(d)		mtx_trylock((d)->lock)
425 #define PCM_LOCKASSERT(d)	mtx_assert((d)->lock, MA_OWNED)
426 #define PCM_UNLOCKASSERT(d)	mtx_assert((d)->lock, MA_NOTOWNED)
427 
428 /*
429  * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these
430  * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you!
431  */
432 #ifdef SND_DIAGNOSTIC
433 #define PCM_WAIT(x)		do {					\
434 	if (!PCM_LOCKOWNED(x))						\
435 		panic("%s(%d): [PCM WAIT] Mutex not owned!",		\
436 		    __func__, __LINE__);				\
437 	while ((x)->flags & SD_F_BUSY) {				\
438 		if (snd_verbose > 3)					\
439 			device_printf((x)->dev,				\
440 			    "%s(%d): [PCM WAIT] calling cv_wait().\n",	\
441 			    __func__, __LINE__);			\
442 		cv_wait(&(x)->cv, (x)->lock);				\
443 	}								\
444 } while (0)
445 
446 #define PCM_ACQUIRE(x)		do {					\
447 	if (!PCM_LOCKOWNED(x))						\
448 		panic("%s(%d): [PCM ACQUIRE] Mutex not owned!",		\
449 		    __func__, __LINE__);				\
450 	if ((x)->flags & SD_F_BUSY)					\
451 		panic("%s(%d): [PCM ACQUIRE] "				\
452 		    "Trying to acquire BUSY cv!", __func__, __LINE__);	\
453 	(x)->flags |= SD_F_BUSY;					\
454 } while (0)
455 
456 #define PCM_RELEASE(x)		do {					\
457 	if (!PCM_LOCKOWNED(x))						\
458 		panic("%s(%d): [PCM RELEASE] Mutex not owned!",		\
459 		    __func__, __LINE__);				\
460 	if ((x)->flags & SD_F_BUSY) {					\
461 		(x)->flags &= ~SD_F_BUSY;				\
462 		if ((x)->cv.cv_waiters != 0) {				\
463 			if ((x)->cv.cv_waiters > 1 && snd_verbose > 3)	\
464 				device_printf((x)->dev,			\
465 				    "%s(%d): [PCM RELEASE] "		\
466 				    "cv_waiters=%d > 1!\n",		\
467 				    __func__, __LINE__,			\
468 				    (x)->cv.cv_waiters);		\
469 			cv_broadcast(&(x)->cv);				\
470 		}							\
471 	} else								\
472 		panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",	\
473 		    __func__, __LINE__);				\
474 } while (0)
475 
476 /* Quick version, for shorter path. */
477 #define PCM_ACQUIRE_QUICK(x)	do {					\
478 	if (PCM_LOCKOWNED(x))						\
479 		panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!",	\
480 		    __func__, __LINE__);				\
481 	PCM_LOCK(x);							\
482 	PCM_WAIT(x);							\
483 	PCM_ACQUIRE(x);							\
484 	PCM_UNLOCK(x);							\
485 } while (0)
486 
487 #define PCM_RELEASE_QUICK(x)	do {					\
488 	if (PCM_LOCKOWNED(x))						\
489 		panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!",	\
490 		    __func__, __LINE__);				\
491 	PCM_LOCK(x);							\
492 	PCM_RELEASE(x);							\
493 	PCM_UNLOCK(x);							\
494 } while (0)
495 
496 #define PCM_BUSYASSERT(x)	do {					\
497 	if (!((x) != NULL && ((x)->flags & SD_F_BUSY)))			\
498 		panic("%s(%d): [PCM BUSYASSERT] "			\
499 		    "Failed, snddev_info=%p", __func__, __LINE__, x);	\
500 } while (0)
501 
502 #define PCM_GIANT_ENTER(x)	do {					\
503 	int _pcm_giant = 0;						\
504 	if (PCM_LOCKOWNED(x))						\
505 		panic("%s(%d): [GIANT ENTER] PCM lock owned!",		\
506 		    __func__, __LINE__);				\
507 	if (mtx_owned(&Giant) != 0 && snd_verbose > 3)			\
508 		device_printf((x)->dev,					\
509 		    "%s(%d): [GIANT ENTER] Giant owned!\n",		\
510 		    __func__, __LINE__);				\
511 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
512 		do {							\
513 			mtx_lock(&Giant);				\
514 			_pcm_giant = 1;					\
515 		} while (0)
516 
517 #define PCM_GIANT_EXIT(x)	do {					\
518 	if (PCM_LOCKOWNED(x))						\
519 		panic("%s(%d): [GIANT EXIT] PCM lock owned!",		\
520 		    __func__, __LINE__);				\
521 	if (!(_pcm_giant == 0 || _pcm_giant == 1))			\
522 		panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!",	\
523 		    __func__, __LINE__);				\
524 	if ((x)->flags & SD_F_MPSAFE) {					\
525 		if (_pcm_giant == 1)					\
526 			panic("%s(%d): [GIANT EXIT] MPSAFE Giant?",	\
527 			    __func__, __LINE__);			\
528 		if (mtx_owned(&Giant) != 0 && snd_verbose > 3)		\
529 			device_printf((x)->dev,				\
530 			    "%s(%d): [GIANT EXIT] Giant owned!\n",	\
531 			    __func__, __LINE__);			\
532 	}								\
533 	if (_pcm_giant != 0) {						\
534 		if (mtx_owned(&Giant) == 0)				\
535 			panic("%s(%d): [GIANT EXIT] Giant not owned!",	\
536 			    __func__, __LINE__);			\
537 		_pcm_giant = 0;						\
538 		mtx_unlock(&Giant);					\
539 	}								\
540 } while (0)
541 #else /* SND_DIAGNOSTIC */
542 #define PCM_WAIT(x)		do {					\
543 	PCM_LOCKASSERT(x);						\
544 	while ((x)->flags & SD_F_BUSY)					\
545 		cv_wait(&(x)->cv, (x)->lock);				\
546 } while (0)
547 
548 #define PCM_ACQUIRE(x)		do {					\
549 	PCM_LOCKASSERT(x);						\
550 	KASSERT(!((x)->flags & SD_F_BUSY),				\
551 	    ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!",	\
552 	    __func__, __LINE__));					\
553 	(x)->flags |= SD_F_BUSY;					\
554 } while (0)
555 
556 #define PCM_RELEASE(x)		do {					\
557 	PCM_LOCKASSERT(x);						\
558 	KASSERT((x)->flags & SD_F_BUSY,					\
559 	    ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!",		\
560 	    __func__, __LINE__));					\
561 	(x)->flags &= ~SD_F_BUSY;					\
562 	if ((x)->cv.cv_waiters != 0)					\
563 		cv_broadcast(&(x)->cv);					\
564 } while (0)
565 
566 /* Quick version, for shorter path. */
567 #define PCM_ACQUIRE_QUICK(x)	do {					\
568 	PCM_UNLOCKASSERT(x);						\
569 	PCM_LOCK(x);							\
570 	PCM_WAIT(x);							\
571 	PCM_ACQUIRE(x);							\
572 	PCM_UNLOCK(x);							\
573 } while (0)
574 
575 #define PCM_RELEASE_QUICK(x)	do {					\
576 	PCM_UNLOCKASSERT(x);						\
577 	PCM_LOCK(x);							\
578 	PCM_RELEASE(x);							\
579 	PCM_UNLOCK(x);							\
580 } while (0)
581 
582 #define PCM_BUSYASSERT(x)	KASSERT(x != NULL &&			\
583 				    ((x)->flags & SD_F_BUSY),		\
584 				    ("%s(%d): [PCM BUSYASSERT] "	\
585 				    "Failed, snddev_info=%p",		\
586 				    __func__, __LINE__, x))
587 
588 #define PCM_GIANT_ENTER(x)	do {					\
589 	int _pcm_giant = 0;						\
590 	PCM_UNLOCKASSERT(x);						\
591 	if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0)	\
592 		do {							\
593 			mtx_lock(&Giant);				\
594 			_pcm_giant = 1;					\
595 		} while (0)
596 
597 #define PCM_GIANT_EXIT(x)	do {					\
598 	PCM_UNLOCKASSERT(x);						\
599 	KASSERT(_pcm_giant == 0 || _pcm_giant == 1,			\
600 	    ("%s(%d): [GIANT EXIT] _pcm_giant screwed!",		\
601 	    __func__, __LINE__));					\
602 	KASSERT(!((x)->flags & SD_F_MPSAFE) ||				\
603 	    (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0),		\
604 	    ("%s(%d): [GIANT EXIT] MPSAFE Giant?",			\
605 	    __func__, __LINE__));					\
606 	if (_pcm_giant != 0) {						\
607 		mtx_assert(&Giant, MA_OWNED);				\
608 		_pcm_giant = 0;						\
609 		mtx_unlock(&Giant);					\
610 	}								\
611 } while (0)
612 #endif /* !SND_DIAGNOSTIC */
613 
614 #define PCM_GIANT_LEAVE(x)						\
615 	PCM_GIANT_EXIT(x);						\
616 } while (0)
617 
618 #ifdef KLD_MODULE
619 #define PCM_KLDSTRING(a) ("kld " # a)
620 #else
621 #define PCM_KLDSTRING(a) ""
622 #endif
623 
624 #endif /* _KERNEL */
625 
626 #endif	/* _OS_H_ */
627