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