2 * SPDX-License-Identifier: BSD-2-Clause
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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * first, include kernel header files.
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>
48 #include <sys/kernel.h> /* for DATA_SET */
49 #include <sys/module.h>
53 #include <sys/syslog.h>
54 #include <sys/errno.h>
55 #include <sys/malloc.h>
57 #include <machine/resource.h>
58 #include <machine/bus.h>
60 #include <sys/limits.h>
64 #include <sys/soundcard.h>
65 #include <sys/sndstat.h>
66 #include <sys/sysctl.h>
72 #include <sys/mutex.h>
73 #include <sys/condvar.h>
75 #ifndef KOBJMETHOD_END
76 #define KOBJMETHOD_END { NULL, NULL }
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>
92 #define PCM_SOFTC_SIZE (sizeof(struct snddev_info))
94 #define SND_STATUSLEN 64
96 #define SOUND_MODVER 5
98 #define SOUND_MINVER SOUND_MODVER
99 #define SOUND_PREFVER SOUND_MODVER
100 #define SOUND_MAXVER SOUND_MODVER
103 * By design, limit possible channels for each direction.
105 #define SND_MAXHWCHAN 256
106 #define SND_MAXVCHANS SND_MAXHWCHAN
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 */
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)
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
133 #define SD_F_BITS "\020" \
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))
157 #define PCM_DETACHING(x) ((x)->flags & SD_F_DETACHING)
159 #define PCM_CHANCOUNT(d) \
160 (d->playcount + d->pvchancount + d->reccount + d->rvchancount)
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)
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)
178 #define AFMT_CONVERTIBLE (AFMT_8BIT | AFMT_16BIT | AFMT_24BIT | \
181 /* Supported vchan mixing formats */
182 #define AFMT_VCHAN (AFMT_CONVERTIBLE & ~AFMT_G711)
184 #define AFMT_PASSTHROUGH AFMT_AC3
185 #define AFMT_PASSTHROUGH_RATE 48000
186 #define AFMT_PASSTHROUGH_CHANNEL 2
187 #define AFMT_PASSTHROUGH_EXTCHANNEL 0
190 * We're simply using unused, contiguous bits from various AFMT_ definitions.
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
201 #define AFMT_ENCODING(v) ((v) & AFMT_ENCODING_MASK)
203 #define AFMT_EXTCHANNEL(v) (((v) & AFMT_EXTCHANNEL_MASK) >> \
204 AFMT_EXTCHANNEL_SHIFT)
206 #define AFMT_CHANNEL(v) (((v) & AFMT_CHANNEL_MASK) >> \
209 #define AFMT_BIT(v) (((v) & AFMT_32BIT) ? 32 : \
210 (((v) & AFMT_24BIT) ? 24 : \
211 ((((v) & AFMT_16BIT) || \
212 ((v) & AFMT_PASSTHROUGH)) ? 16 : 8)))
214 #define AFMT_BPS(v) (AFMT_BIT(v) >> 3)
215 #define AFMT_ALIGN(v) (AFMT_BPS(v) * AFMT_CHANNEL(v))
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))
223 #define AFMT_U8_NE AFMT_U8
224 #define AFMT_S8_NE AFMT_S8
226 #define AFMT_SIGNED_NE (AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE)
228 #define AFMT_NE (AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE | \
229 AFMT_U24_NE | AFMT_U32_NE)
232 * Minor numbers for the sound driver.
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.
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
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 */
258 #define SND_DEV_DSPHW_CD 15 /* s16le/stereo 44100Hz CD */
261 * OSSv4 compatible device. For now, it serve no purpose and
262 * the cloning itself will forward the request to ordinary /dev/dsp
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 */
271 #define DSP_DEFAULT_SPEED 8000
276 extern int pcm_veto_load;
278 extern int snd_maxautovchans;
279 extern int snd_verbose;
280 extern devclass_t pcm_devclass;
281 extern struct unrhdr *pcmsg_unrhdr;
284 * some macros for debugging purposes
285 * DDB/DEB to enable/disable debugging stuff
286 * BVDDB to enable debugging when bootverbose
288 #define BVDDB(x) if (bootverbose) x
294 SYSCTL_DECL(_hw_snd);
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);
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);
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);
315 int snd_setup_intr(device_t dev, struct resource *res, int flags,
316 driver_intr_t hand, void *param, void **cookiep);
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)
324 int sndstat_register(device_t dev, char *str);
325 int sndstat_unregister(device_t dev);
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.
336 SLIST_HEAD(, pcm_channel) head;
338 SLIST_HEAD(, pcm_channel) head;
341 SLIST_HEAD(, pcm_channel) head;
345 unsigned playcount, reccount, pvchancount, rvchancount;
350 char status[SND_STATUSLEN];
352 struct cdev *mixer_dev;
353 struct cdev *dsp_dev;
354 uint32_t pvchanrate, pvchanformat;
355 uint32_t rvchanrate, rvchanformat;
357 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx;
358 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree;
362 void sound_oss_sysinfo(oss_sysinfo *);
363 int sound_oss_card_info(oss_card_info *);
365 #define PCM_MODE_MIXER 0x01
366 #define PCM_MODE_PLAY 0x02
367 #define PCM_MODE_REC 0x04
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)
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!
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); \
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; \
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); \
420 panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \
421 __func__, __LINE__); \
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__); \
435 #define PCM_RELEASE_QUICK(x) do { \
436 if (PCM_LOCKOWNED(x)) \
437 panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!", \
438 __func__, __LINE__); \
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); \
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) \
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__); \
481 if (_pcm_giant != 0) { \
482 if (mtx_owned(&Giant) == 0) \
483 panic("%s(%d): [GIANT EXIT] Giant not owned!", \
484 __func__, __LINE__); \
486 mtx_unlock(&Giant); \
489 #else /* !SND_DIAGNOSTIC */
490 #define PCM_WAIT(x) do { \
492 while ((x)->flags & SD_F_BUSY) \
493 cv_wait(&(x)->cv, (x)->lock); \
496 #define PCM_ACQUIRE(x) do { \
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; \
504 #define PCM_RELEASE(x) do { \
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); \
514 /* Quick version, for shorter path. */
515 #define PCM_ACQUIRE_QUICK(x) do { \
516 PCM_UNLOCKASSERT(x); \
523 #define PCM_RELEASE_QUICK(x) do { \
524 PCM_UNLOCKASSERT(x); \
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))
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) \
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); \
557 mtx_unlock(&Giant); \
560 #endif /* SND_DIAGNOSTIC */
562 #define PCM_GIANT_LEAVE(x) \