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