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