2 * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
3 * Copyright by Hannu Savolainen 1995
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * first, include kernel header files.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/ioccom.h>
41 #include <sys/filio.h>
42 #include <sys/sockio.h>
43 #include <sys/fcntl.h>
46 #include <sys/kernel.h> /* for DATA_SET */
47 #include <sys/module.h>
51 #include <sys/syslog.h>
52 #include <sys/errno.h>
53 #include <sys/malloc.h>
55 #if __FreeBSD_version < 500000
58 #include <machine/resource.h>
59 #include <machine/bus.h>
61 #include <sys/limits.h>
65 #include <sys/soundcard.h>
66 #include <sys/sysctl.h>
74 #if __FreeBSD_version > 500000
76 #include <sys/mutex.h>
77 #include <sys/condvar.h>
82 #define INTR_TYPE_AV INTR_TYPE_TTY
93 #include <dev/sound/pcm/buffer.h>
94 #include <dev/sound/pcm/channel.h>
95 #include <dev/sound/pcm/feeder.h>
96 #include <dev/sound/pcm/mixer.h>
97 #include <dev/sound/pcm/dsp.h>
98 #include <dev/sound/clone.h>
99 #include <dev/sound/unit.h>
101 #define PCM_SOFTC_SIZE 512
103 #define SND_STATUSLEN 64
105 #define SOUND_MODVER 2
107 #define SOUND_MINVER SOUND_MODVER
108 #define SOUND_PREFVER SOUND_MODVER
109 #define SOUND_MAXVER SOUND_MODVER
112 * We're abusing the fact that MAXMINOR still have enough room
113 * for our bit twiddling and nobody ever need 512 unique soundcards,
114 * 32 unique device types and 1024 unique cloneable devices for the
118 #define PCMMAXUNIT (snd_max_u())
119 #define PCMMAXDEV (snd_max_d())
120 #define PCMMAXCHAN (snd_max_c())
122 #define PCMMAXCLONE PCMMAXCHAN
124 #define PCMUNIT(x) (snd_unit2u(dev2unit(x)))
125 #define PCMDEV(x) (snd_unit2d(dev2unit(x)))
126 #define PCMCHAN(x) (snd_unit2c(dev2unit(x)))
129 * By design, limit possible channels for each direction.
131 #define SND_MAXHWCHAN 256
132 #define SND_MAXVCHANS SND_MAXHWCHAN
134 #define SD_F_SIMPLEX 0x00000001
135 #define SD_F_AUTOVCHAN 0x00000002
136 #define SD_F_SOFTPCMVOL 0x00000004
137 #define SD_F_PSWAPLR 0x00000008
138 #define SD_F_RSWAPLR 0x00000010
139 #define SD_F_DYING 0x00000020
140 #define SD_F_SUICIDE 0x00000040
141 #define SD_F_BUSY 0x00000080
142 #define SD_F_MPSAFE 0x00000100
143 #define SD_F_REGISTERED 0x00000200
145 #define SD_F_PRIO_RD 0x10000000
146 #define SD_F_PRIO_WR 0x20000000
147 #define SD_F_PRIO_SET (SD_F_PRIO_RD | SD_F_PRIO_WR)
148 #define SD_F_DIR_SET 0x40000000
149 #define SD_F_TRANSIENT 0xf0000000
151 #define PCM_ALIVE(x) ((x) != NULL && (x)->lock != NULL && \
152 !((x)->flags & SD_F_DYING))
153 #define PCM_REGISTERED(x) (PCM_ALIVE(x) && \
154 ((x)->flags & SD_F_REGISTERED))
156 /* many variables should be reduced to a range. Here define a macro */
157 #define RANGE(var, low, high) (var) = \
158 (((var)<(low))? (low) : ((var)>(high))? (high) : (var))
159 #define DSP_BUFFSIZE (8192)
162 * Macros for reading/writing PCM sample / int values from bytes array.
163 * Since every process is done using signed integer (and to make our life
164 * less miserable), unsigned sample will be converted to its signed
165 * counterpart and restored during writing back. To avoid overflow,
166 * we truncate 32bit (and only 32bit) samples down to 24bit (see below
167 * for the reason), unless PCM_USE_64BIT_ARITH is defined.
171 * Automatically turn on 64bit arithmetic on suitable archs
172 * (amd64 64bit, ia64, etc..) for wider 32bit samples / integer processing.
175 #undef PCM_USE_64BIT_ARITH
176 #define PCM_USE_64BIT_ARITH 1
179 #undef PCM_USE_64BIT_ARITH
180 #define PCM_USE_64BIT_ARITH 1
184 #ifdef PCM_USE_64BIT_ARITH
185 typedef int64_t intpcm_t;
187 typedef int32_t intpcm_t;
190 /* 32bit fixed point shift */
191 #define PCM_FXSHIFT 8
193 #define PCM_S8_MAX 0x7f
194 #define PCM_S8_MIN -0x80
195 #define PCM_S16_MAX 0x7fff
196 #define PCM_S16_MIN -0x8000
197 #define PCM_S24_MAX 0x7fffff
198 #define PCM_S24_MIN -0x800000
199 #ifdef PCM_USE_64BIT_ARITH
201 #define PCM_S32_MAX 0x7fffffffL
202 #define PCM_S32_MIN -0x80000000L
204 #define PCM_S32_MAX 0x7fffffffLL
205 #define PCM_S32_MIN -0x80000000LL
208 #define PCM_S32_MAX 0x7fffffff
209 #define PCM_S32_MIN (-0x7fffffff - 1)
212 /* Bytes-per-sample definition */
218 #if BYTE_ORDER == LITTLE_ENDIAN
219 #define PCM_READ_S16_LE(b8) *((int16_t *)(b8))
220 #define _PCM_READ_S32_LE(b8) *((int32_t *)(b8))
221 #define PCM_READ_S16_BE(b8) \
222 ((int32_t)((b8)[1] | ((int8_t)((b8)[0])) << 8))
223 #define _PCM_READ_S32_BE(b8) \
224 ((int32_t)((b8)[3] | (b8)[2] << 8 | (b8)[1] << 16 | \
225 ((int8_t)((b8)[0])) << 24))
227 #define PCM_WRITE_S16_LE(b8, val) *((int16_t *)(b8)) = (val)
228 #define _PCM_WRITE_S32_LE(b8, val) *((int32_t *)(b8)) = (val)
229 #define PCM_WRITE_S16_BE(bb8, vval) do { \
230 int32_t val = (vval); \
231 uint8_t *b8 = (bb8); \
235 #define _PCM_WRITE_S32_BE(bb8, vval) do { \
236 int32_t val = (vval); \
237 uint8_t *b8 = (bb8); \
244 #define PCM_READ_U16_LE(b8) ((int16_t)(*((uint16_t *)(b8)) ^ 0x8000))
245 #define _PCM_READ_U32_LE(b8) ((int32_t)(*((uint32_t *)(b8)) ^ 0x80000000))
246 #define PCM_READ_U16_BE(b8) \
247 ((int32_t)((b8)[1] | ((int8_t)((b8)[0] ^ 0x80)) << 8))
248 #define _PCM_READ_U32_BE(b8) \
249 ((int32_t)((b8)[3] | (b8)[2] << 8 | (b8)[1] << 16 | \
250 ((int8_t)((b8)[0] ^ 0x80)) << 24))
252 #define PCM_WRITE_U16_LE(b8, val) *((uint16_t *)(b8)) = (val) ^ 0x8000
253 #define _PCM_WRITE_U32_LE(b8, val) *((uint32_t *)(b8)) = (val) ^ 0x80000000
254 #define PCM_WRITE_U16_BE(bb8, vval) do { \
255 int32_t val = (vval); \
256 uint8_t *b8 = (bb8); \
258 b8[0] = (val >> 8) ^ 0x80; \
260 #define _PCM_WRITE_U32_BE(bb8, vval) do { \
261 int32_t val = (vval); \
262 uint8_t *b8 = (bb8); \
266 b8[0] = (val >> 24) ^ 0x80; \
268 #else /* !LITTLE_ENDIAN */
269 #define PCM_READ_S16_LE(b8) \
270 ((int32_t)((b8)[0] | ((int8_t)((b8)[1])) << 8))
271 #define _PCM_READ_S32_LE(b8) \
272 ((int32_t)((b8)[0] | (b8)[1] << 8 | (b8)[2] << 16 | \
273 ((int8_t)((b8)[3])) << 24))
274 #define PCM_READ_S16_BE(b8) *((int16_t *)(b8))
275 #define _PCM_READ_S32_BE(b8) *((int32_t *)(b8))
277 #define PCM_WRITE_S16_LE(bb8, vval) do { \
278 int32_t val = (vval); \
279 uint8_t *b8 = (bb8); \
283 #define _PCM_WRITE_S32_LE(bb8, vval) do { \
284 int32_t val = (vval); \
285 uint8_t *b8 = (bb8); \
291 #define PCM_WRITE_S16_BE(b8, val) *((int16_t *)(b8)) = (val)
292 #define _PCM_WRITE_S32_BE(b8, val) *((int32_t *)(b8)) = (val)
294 #define PCM_READ_U16_LE(b8) \
295 ((int32_t)((b8)[0] | ((int8_t)((b8)[1] ^ 0x80)) << 8))
296 #define _PCM_READ_U32_LE(b8) \
297 ((int32_t)((b8)[0] | (b8)[1] << 8 | (b8)[2] << 16 | \
298 ((int8_t)((b8)[3] ^ 0x80)) << 24))
299 #define PCM_READ_U16_BE(b8) ((int16_t)(*((uint16_t *)(b8)) ^ 0x8000))
300 #define _PCM_READ_U32_BE(b8) ((int32_t)(*((uint32_t *)(b8)) ^ 0x80000000))
302 #define PCM_WRITE_U16_LE(bb8, vval) do { \
303 int32_t val = (vval); \
304 uint8_t *b8 = (bb8); \
306 b8[1] = (val >> 8) ^ 0x80; \
308 #define _PCM_WRITE_U32_LE(bb8, vval) do { \
309 int32_t val = (vval); \
310 uint8_t *b8 = (bb8); \
314 b8[3] = (val >> 24) ^ 0x80; \
316 #define PCM_WRITE_U16_BE(b8, val) *((uint16_t *)(b8)) = (val) ^ 0x8000
317 #define _PCM_WRITE_U32_BE(b8, val) *((uint32_t *)(b8)) = (val) ^ 0x80000000
320 #define PCM_READ_S24_LE(b8) \
321 ((int32_t)((b8)[0] | (b8)[1] << 8 | ((int8_t)((b8)[2])) << 16))
322 #define PCM_READ_S24_BE(b8) \
323 ((int32_t)((b8)[2] | (b8)[1] << 8 | ((int8_t)((b8)[0])) << 16))
325 #define PCM_WRITE_S24_LE(bb8, vval) do { \
326 int32_t val = (vval); \
327 uint8_t *b8 = (bb8); \
332 #define PCM_WRITE_S24_BE(bb8, vval) do { \
333 int32_t val = (vval); \
334 uint8_t *b8 = (bb8); \
340 #define PCM_READ_U24_LE(b8) \
341 ((int32_t)((b8)[0] | (b8)[1] << 8 | \
342 ((int8_t)((b8)[2] ^ 0x80)) << 16))
343 #define PCM_READ_U24_BE(b8) \
344 ((int32_t)((b8)[2] | (b8)[1] << 8 | \
345 ((int8_t)((b8)[0] ^ 0x80)) << 16))
347 #define PCM_WRITE_U24_LE(bb8, vval) do { \
348 int32_t val = (vval); \
349 uint8_t *b8 = (bb8); \
352 b8[2] = (val >> 16) ^ 0x80; \
354 #define PCM_WRITE_U24_BE(bb8, vval) do { \
355 int32_t val = (vval); \
356 uint8_t *b8 = (bb8); \
359 b8[0] = (val >> 16) ^ 0x80; \
362 #ifdef PCM_USE_64BIT_ARITH
363 #define PCM_READ_S32_LE(b8) _PCM_READ_S32_LE(b8)
364 #define PCM_READ_S32_BE(b8) _PCM_READ_S32_BE(b8)
365 #define PCM_WRITE_S32_LE(b8, val) _PCM_WRITE_S32_LE(b8, val)
366 #define PCM_WRITE_S32_BE(b8, val) _PCM_WRITE_S32_BE(b8, val)
368 #define PCM_READ_U32_LE(b8) _PCM_READ_U32_LE(b8)
369 #define PCM_READ_U32_BE(b8) _PCM_READ_U32_BE(b8)
370 #define PCM_WRITE_U32_LE(b8, val) _PCM_WRITE_U32_LE(b8, val)
371 #define PCM_WRITE_U32_BE(b8, val) _PCM_WRITE_U32_BE(b8, val)
372 #else /* !PCM_USE_64BIT_ARITH */
374 * 24bit integer ?!? This is quite unfortunate, eh? Get the fact straight:
377 * 2) 16bit = 96db (close enough)
378 * 3) 24bit = 144db (perfect)
379 * 4) 32bit = 196db (way too much)
380 * 5) Bugs Bunny = Gazillion!@%$Erbzzztt-EINVAL db
381 * Since we're not Bugs Bunny ..uh..err.. avoiding 64bit arithmetic, 24bit
382 * is pretty much sufficient for our signed integer processing.
384 #define PCM_READ_S32_LE(b8) (_PCM_READ_S32_LE(b8) >> PCM_FXSHIFT)
385 #define PCM_READ_S32_BE(b8) (_PCM_READ_S32_BE(b8) >> PCM_FXSHIFT)
386 #define PCM_WRITE_S32_LE(b8, val) _PCM_WRITE_S32_LE(b8, (val) << PCM_FXSHIFT)
387 #define PCM_WRITE_S32_BE(b8, val) _PCM_WRITE_S32_BE(b8, (val) << PCM_FXSHIFT)
389 #define PCM_READ_U32_LE(b8) (_PCM_READ_U32_LE(b8) >> PCM_FXSHIFT)
390 #define PCM_READ_U32_BE(b8) (_PCM_READ_U32_BE(b8) >> PCM_FXSHIFT)
391 #define PCM_WRITE_U32_LE(b8, val) _PCM_WRITE_U32_LE(b8, (val) << PCM_FXSHIFT)
392 #define PCM_WRITE_U32_BE(b8, val) _PCM_WRITE_U32_BE(b8, (val) << PCM_FXSHIFT)
396 * 8bit sample is pretty much useless since it doesn't provide
397 * sufficient dynamic range throughout our filtering process.
398 * For the sake of completeness, declare it anyway.
400 #define PCM_READ_S8(b8) *((int8_t *)(b8))
401 #define PCM_READ_S8_NE(b8) PCM_READ_S8(b8)
402 #define PCM_READ_U8(b8) ((int8_t)(*((uint8_t *)(b8)) ^ 0x80))
403 #define PCM_READ_U8_NE(b8) PCM_READ_U8(b8)
405 #define PCM_WRITE_S8(b8, val) *((int8_t *)(b8)) = (val)
406 #define PCM_WRITE_S8_NE(b8, val) PCM_WRITE_S8(b8, val)
407 #define PCM_WRITE_U8(b8, val) *((uint8_t *)(b8)) = (val) ^ 0x80
408 #define PCM_WRITE_U8_NE(b8, val) PCM_WRITE_U8(b8, val)
410 #define PCM_CLAMP_S8(val) \
411 (((val) > PCM_S8_MAX) ? PCM_S8_MAX : \
412 (((val) < PCM_S8_MIN) ? PCM_S8_MIN : (val)))
413 #define PCM_CLAMP_S16(val) \
414 (((val) > PCM_S16_MAX) ? PCM_S16_MAX : \
415 (((val) < PCM_S16_MIN) ? PCM_S16_MIN : (val)))
416 #define PCM_CLAMP_S24(val) \
417 (((val) > PCM_S24_MAX) ? PCM_S24_MAX : \
418 (((val) < PCM_S24_MIN) ? PCM_S24_MIN : (val)))
420 #ifdef PCM_USE_64BIT_ARITH
421 #define PCM_CLAMP_S32(val) \
422 (((val) > PCM_S32_MAX) ? PCM_S32_MAX : \
423 (((val) < PCM_S32_MIN) ? PCM_S32_MIN : (val)))
425 #define PCM_CLAMP_S32(val) \
426 (((val) > PCM_S24_MAX) ? PCM_S32_MAX : \
427 (((val) < PCM_S24_MIN) ? PCM_S32_MIN : \
428 ((val) << PCM_FXSHIFT)))
431 #define PCM_CLAMP_U8(val) PCM_CLAMP_S8(val)
432 #define PCM_CLAMP_U16(val) PCM_CLAMP_S16(val)
433 #define PCM_CLAMP_U24(val) PCM_CLAMP_S24(val)
434 #define PCM_CLAMP_U32(val) PCM_CLAMP_S32(val)
436 /* make figuring out what a format is easier. got AFMT_STEREO already */
437 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE)
438 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE)
439 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE)
440 #define AFMT_8BIT (AFMT_MU_LAW | AFMT_A_LAW | AFMT_U8 | AFMT_S8)
441 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \
442 AFMT_S16_LE | AFMT_S16_BE | AFMT_S8)
443 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \
444 AFMT_S16_BE | AFMT_U16_BE)
446 struct pcm_channel *fkchan_setup(device_t dev);
447 int fkchan_kill(struct pcm_channel *c);
450 * Minor numbers for the sound driver.
452 * Unfortunately Creative called the codec chip of SB as a DSP. For this
453 * reason the /dev/dsp is reserved for digitized audio use. There is a
454 * device for true DSP processors but it will be called something else.
455 * In v3.0 it's /dev/sndproc but this could be a temporary solution.
458 #define SND_DEV_CTL 0 /* Control port /dev/mixer */
459 #define SND_DEV_SEQ 1 /* Sequencer /dev/sequencer */
460 #define SND_DEV_MIDIN 2 /* Raw midi access */
461 #define SND_DEV_DSP 3 /* Digitized voice /dev/dsp */
462 #define SND_DEV_AUDIO 4 /* Sparc compatible /dev/audio */
463 #define SND_DEV_DSP16 5 /* Like /dev/dsp but 16 bits/sample */
464 #define SND_DEV_STATUS 6 /* /dev/sndstat */
465 /* #7 not in use now. */
466 #define SND_DEV_SEQ2 8 /* /dev/sequencer, level 2 interface */
467 #define SND_DEV_SNDPROC 9 /* /dev/sndproc for programmable devices */
468 #define SND_DEV_PSS SND_DEV_SNDPROC /* ? */
469 #define SND_DEV_NORESET 10
471 #define SND_DEV_DSPHW_PLAY 11 /* specific playback channel */
472 #define SND_DEV_DSPHW_VPLAY 12 /* specific virtual playback channel */
473 #define SND_DEV_DSPHW_REC 13 /* specific record channel */
474 #define SND_DEV_DSPHW_VREC 14 /* specific virtual record channel */
476 #define SND_DEV_DSPHW_CD 15 /* s16le/stereo 44100Hz CD */
478 #define SND_DEV_DSP_MMAP 16 /* OSSv4 compatible /dev/dsp_mmap */
480 #define SND_DEV_LAST SND_DEV_DSP_MMAP
481 #define SND_DEV_MAX PCMMAXDEV
483 #define DSP_DEFAULT_SPEED 8000
488 extern int pcm_veto_load;
490 extern int snd_maxautovchans;
491 extern int snd_verbose;
492 extern devclass_t pcm_devclass;
493 extern struct unrhdr *pcmsg_unrhdr;
496 * some macros for debugging purposes
497 * DDB/DEB to enable/disable debugging stuff
498 * BVDDB to enable debugging when bootverbose
500 #define BVDDB(x) if (bootverbose) x
506 SYSCTL_DECL(_hw_snd);
508 struct pcm_channel *pcm_getfakechan(struct snddev_info *d);
509 int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction, pid_t pid, int devunit);
510 int pcm_chnrelease(struct pcm_channel *c);
511 int pcm_chnref(struct pcm_channel *c, int ref);
512 int pcm_inprog(struct snddev_info *d, int delta);
514 struct pcm_channel *pcm_chn_create(struct snddev_info *d, struct pcm_channel *parent, kobj_class_t cls, int dir, int num, void *devinfo);
515 int pcm_chn_destroy(struct pcm_channel *ch);
516 int pcm_chn_add(struct snddev_info *d, struct pcm_channel *ch);
517 int pcm_chn_remove(struct snddev_info *d, struct pcm_channel *ch);
519 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo);
520 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz);
521 int pcm_register(device_t dev, void *devinfo, int numplay, int numrec);
522 int pcm_unregister(device_t dev);
523 int pcm_setstatus(device_t dev, char *str);
524 u_int32_t pcm_getflags(device_t dev);
525 void pcm_setflags(device_t dev, u_int32_t val);
526 void *pcm_getdevinfo(device_t dev);
529 int snd_setup_intr(device_t dev, struct resource *res, int flags,
530 driver_intr_t hand, void *param, void **cookiep);
532 void *snd_mtxcreate(const char *desc, const char *type);
533 void snd_mtxfree(void *m);
534 void snd_mtxassert(void *m);
535 #define snd_mtxlock(m) mtx_lock(m)
536 #define snd_mtxunlock(m) mtx_unlock(m)
538 int sysctl_hw_snd_vchans(SYSCTL_HANDLER_ARGS);
540 typedef int (*sndstat_handler)(struct sbuf *s, device_t dev, int verbose);
541 int sndstat_acquire(struct thread *td);
542 int sndstat_release(struct thread *td);
543 int sndstat_register(device_t dev, char *str, sndstat_handler handler);
544 int sndstat_registerfile(char *str);
545 int sndstat_unregister(device_t dev);
546 int sndstat_unregisterfile(char *str);
548 #define SND_DECLARE_FILE(version) \
549 _SND_DECLARE_FILE(__LINE__, version)
551 #define _SND_DECLARE_FILE(uniq, version) \
552 __SND_DECLARE_FILE(uniq, version)
554 #define __SND_DECLARE_FILE(uniq, version) \
555 static char sndstat_vinfo[] = version; \
556 SYSINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_registerfile, sndstat_vinfo); \
557 SYSUNINIT(sdf_ ## uniq, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, sndstat_unregisterfile, sndstat_vinfo);
559 /* usage of flags in device config entry (config file) */
560 #define DV_F_DRQ_MASK 0x00000007 /* mask for secondary drq */
561 #define DV_F_DUAL_DMA 0x00000010 /* set to use secondary dma channel */
563 /* ought to be made obsolete but still used by mss */
564 #define DV_F_DEV_MASK 0x0000ff00 /* force device type/class */
565 #define DV_F_DEV_SHIFT 8 /* force device type/class */
567 #define PCM_DEBUG_MTX
570 * this is rather kludgey- we need to duplicate these struct def'ns from sound.c
571 * so that the macro versions of pcm_{,un}lock can dereference them.
572 * we also have to do this now makedev() has gone away.
578 SLIST_HEAD(, pcm_channel) head;
580 SLIST_HEAD(, pcm_channel) head;
584 TAILQ_HEAD(dsp_cdevinfo_linkhead, dsp_cdevinfo) dsp_cdevinfo_pool;
585 struct snd_clone *clones;
586 struct pcm_channel *fakechan;
587 unsigned devcount, playcount, reccount, pvchancount, rvchancount ;
593 char status[SND_STATUSLEN];
595 struct cdev *mixer_dev;
596 uint32_t pvchanrate, pvchanformat;
597 uint32_t rvchanrate, rvchanformat;
598 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
599 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
603 void sound_oss_sysinfo(oss_sysinfo *);
606 #define pcm_lock(d) mtx_lock(((struct snddev_info *)(d))->lock)
607 #define pcm_unlock(d) mtx_unlock(((struct snddev_info *)(d))->lock)
609 void pcm_lock(struct snddev_info *d);
610 void pcm_unlock(struct snddev_info *d);
614 * For PCM_CV_[WAIT | ACQUIRE | RELEASE], be sure to surround these
615 * with pcm_lock/unlock() sequence, or I'll come to gnaw upon you!
617 #ifdef SND_DIAGNOSTIC
618 #define PCM_WAIT(x) do { \
619 if (mtx_owned((x)->lock) == 0) \
620 panic("%s(%d): [PCM WAIT] Mutex not owned!", \
621 __func__, __LINE__); \
622 while ((x)->flags & SD_F_BUSY) { \
623 if (snd_verbose > 3) \
624 device_printf((x)->dev, \
625 "%s(%d): [PCM WAIT] calling cv_wait().\n", \
626 __func__, __LINE__); \
627 cv_wait(&(x)->cv, (x)->lock); \
631 #define PCM_ACQUIRE(x) do { \
632 if (mtx_owned((x)->lock) == 0) \
633 panic("%s(%d): [PCM ACQUIRE] Mutex not owned!", \
634 __func__, __LINE__); \
635 if ((x)->flags & SD_F_BUSY) \
636 panic("%s(%d): [PCM ACQUIRE] " \
637 "Trying to acquire BUSY cv!", __func__, __LINE__); \
638 (x)->flags |= SD_F_BUSY; \
641 #define PCM_RELEASE(x) do { \
642 if (mtx_owned((x)->lock) == 0) \
643 panic("%s(%d): [PCM RELEASE] Mutex not owned!", \
644 __func__, __LINE__); \
645 if ((x)->flags & SD_F_BUSY) { \
646 (x)->flags &= ~SD_F_BUSY; \
647 if ((x)->cv.cv_waiters != 0) { \
648 if ((x)->cv.cv_waiters > 1 && snd_verbose > 3) \
649 device_printf((x)->dev, \
650 "%s(%d): [PCM RELEASE] " \
651 "cv_waiters=%d > 1!\n", \
652 __func__, __LINE__, \
653 (x)->cv.cv_waiters); \
654 cv_broadcast(&(x)->cv); \
657 panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \
658 __func__, __LINE__); \
661 /* Quick version, for shorter path. */
662 #define PCM_ACQUIRE_QUICK(x) do { \
663 if (mtx_owned((x)->lock) != 0) \
664 panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!", \
665 __func__, __LINE__); \
672 #define PCM_RELEASE_QUICK(x) do { \
673 if (mtx_owned((x)->lock) != 0) \
674 panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!", \
675 __func__, __LINE__); \
681 #define PCM_BUSYASSERT(x) do { \
682 if (!((x) != NULL && ((x)->flags & SD_F_BUSY))) \
683 panic("%s(%d): [PCM BUSYASSERT] " \
684 "Failed, snddev_info=%p", __func__, __LINE__, x); \
687 #define PCM_GIANT_ENTER(x) do { \
688 int _pcm_giant = 0; \
689 if (mtx_owned((x)->lock) != 0) \
690 panic("%s(%d): [GIANT ENTER] PCM lock owned!", \
691 __func__, __LINE__); \
692 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \
693 device_printf((x)->dev, \
694 "%s(%d): [GIANT ENTER] Giant owned!\n", \
695 __func__, __LINE__); \
696 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \
702 #define PCM_GIANT_EXIT(x) do { \
703 if (mtx_owned((x)->lock) != 0) \
704 panic("%s(%d): [GIANT EXIT] PCM lock owned!", \
705 __func__, __LINE__); \
706 if (!(_pcm_giant == 0 || _pcm_giant == 1)) \
707 panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \
708 __func__, __LINE__); \
709 if ((x)->flags & SD_F_MPSAFE) { \
710 if (_pcm_giant == 1) \
711 panic("%s(%d): [GIANT EXIT] MPSAFE Giant?", \
712 __func__, __LINE__); \
713 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \
714 device_printf((x)->dev, \
715 "%s(%d): [GIANT EXIT] Giant owned!\n", \
716 __func__, __LINE__); \
718 if (_pcm_giant != 0) { \
719 if (mtx_owned(&Giant) == 0) \
720 panic("%s(%d): [GIANT EXIT] Giant not owned!", \
721 __func__, __LINE__); \
723 mtx_unlock(&Giant); \
726 #else /* SND_DIAGNOSTIC */
727 #define PCM_WAIT(x) do { \
728 mtx_assert((x)->lock, MA_OWNED); \
729 while ((x)->flags & SD_F_BUSY) \
730 cv_wait(&(x)->cv, (x)->lock); \
733 #define PCM_ACQUIRE(x) do { \
734 mtx_assert((x)->lock, MA_OWNED); \
735 KASSERT(!((x)->flags & SD_F_BUSY), \
736 ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \
737 __func__, __LINE__)); \
738 (x)->flags |= SD_F_BUSY; \
741 #define PCM_RELEASE(x) do { \
742 mtx_assert((x)->lock, MA_OWNED); \
743 KASSERT((x)->flags & SD_F_BUSY, \
744 ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \
745 __func__, __LINE__)); \
746 (x)->flags &= ~SD_F_BUSY; \
747 if ((x)->cv.cv_waiters != 0) \
748 cv_broadcast(&(x)->cv); \
751 /* Quick version, for shorter path. */
752 #define PCM_ACQUIRE_QUICK(x) do { \
753 mtx_assert((x)->lock, MA_NOTOWNED); \
760 #define PCM_RELEASE_QUICK(x) do { \
761 mtx_assert((x)->lock, MA_NOTOWNED); \
767 #define PCM_BUSYASSERT(x) KASSERT(x != NULL && \
768 ((x)->flags & SD_F_BUSY), \
769 ("%s(%d): [PCM BUSYASSERT] " \
770 "Failed, snddev_info=%p", \
771 __func__, __LINE__, x))
773 #define PCM_GIANT_ENTER(x) do { \
774 int _pcm_giant = 0; \
775 mtx_assert((x)->lock, MA_NOTOWNED); \
776 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \
782 #define PCM_GIANT_EXIT(x) do { \
783 mtx_assert((x)->lock, MA_NOTOWNED); \
784 KASSERT(_pcm_giant == 0 || _pcm_giant == 1, \
785 ("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \
786 __func__, __LINE__)); \
787 KASSERT(!((x)->flags & SD_F_MPSAFE) || \
788 (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0), \
789 ("%s(%d): [GIANT EXIT] MPSAFE Giant?", \
790 __func__, __LINE__)); \
791 if (_pcm_giant != 0) { \
792 mtx_assert(&Giant, MA_OWNED); \
794 mtx_unlock(&Giant); \
797 #endif /* !SND_DIAGNOSTIC */
799 #define PCM_GIANT_LEAVE(x) \
804 #define PCM_KLDSTRING(a) ("kld " # a)
806 #define PCM_KLDSTRING(a) ""