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