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 * Copyright (c) 2024-2025 The FreeBSD Foundation 9 * 10 * Portions of this software were developed by Christos Margiolis 11 * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * first, include kernel header files. 37 */ 38 39 #ifndef _OS_H_ 40 #define _OS_H_ 41 42 #ifdef _KERNEL 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/eventhandler.h> 46 #include <sys/ioccom.h> 47 #include <sys/filio.h> 48 #include <sys/sockio.h> 49 #include <sys/fcntl.h> 50 #include <sys/selinfo.h> 51 #include <sys/proc.h> 52 #include <sys/kernel.h> /* for DATA_SET */ 53 #include <sys/module.h> 54 #include <sys/conf.h> 55 #include <sys/file.h> 56 #include <sys/uio.h> 57 #include <sys/syslog.h> 58 #include <sys/errno.h> 59 #include <sys/malloc.h> 60 #include <sys/bus.h> 61 #include <machine/resource.h> 62 #include <machine/bus.h> 63 #include <sys/rman.h> 64 #include <sys/limits.h> 65 #include <sys/mman.h> 66 #include <sys/poll.h> 67 #include <sys/sbuf.h> 68 #include <sys/soundcard.h> 69 #include <sys/sndstat.h> 70 #include <sys/sysctl.h> 71 #include <sys/kobj.h> 72 #include <vm/vm.h> 73 #include <vm/pmap.h> 74 75 #include <sys/lock.h> 76 #include <sys/mutex.h> 77 #include <sys/condvar.h> 78 79 #ifndef KOBJMETHOD_END 80 #define KOBJMETHOD_END { NULL, NULL } 81 #endif 82 83 struct pcm_channel; 84 struct pcm_feeder; 85 struct snd_dbuf; 86 struct snd_mixer; 87 88 #include <dev/sound/pcm/buffer.h> 89 #include <dev/sound/pcm/matrix.h> 90 #include <dev/sound/pcm/channel.h> 91 #include <dev/sound/pcm/feeder.h> 92 #include <dev/sound/pcm/mixer.h> 93 #include <dev/sound/pcm/dsp.h> 94 95 #define PCM_SOFTC_SIZE (sizeof(struct snddev_info)) 96 97 #define SND_STATUSLEN 64 98 99 #define SOUND_MODVER 5 100 101 #define SOUND_MINVER SOUND_MODVER 102 #define SOUND_PREFVER SOUND_MODVER 103 #define SOUND_MAXVER SOUND_MODVER 104 105 #define SD_F_SIMPLEX 0x00000001 106 /* unused 0x00000002 */ 107 #define SD_F_SOFTPCMVOL 0x00000004 108 #define SD_F_BUSY 0x00000008 109 #define SD_F_MPSAFE 0x00000010 110 #define SD_F_REGISTERED 0x00000020 111 #define SD_F_BITPERFECT 0x00000040 112 #define SD_F_VPC 0x00000080 /* volume-per-channel */ 113 #define SD_F_EQ 0x00000100 /* EQ */ 114 #define SD_F_EQ_ENABLED 0x00000200 /* EQ enabled */ 115 #define SD_F_EQ_BYPASSED 0x00000400 /* EQ bypassed */ 116 #define SD_F_EQ_PC 0x00000800 /* EQ per-channel */ 117 #define SD_F_PVCHANS 0x00001000 /* Playback vchans enabled */ 118 #define SD_F_RVCHANS 0x00002000 /* Recording vchans enabled */ 119 120 #define SD_F_EQ_DEFAULT (SD_F_EQ | SD_F_EQ_ENABLED) 121 #define SD_F_EQ_MASK (SD_F_EQ | SD_F_EQ_ENABLED | \ 122 SD_F_EQ_BYPASSED | SD_F_EQ_PC) 123 124 #define SD_F_BITS "\020" \ 125 "\001SIMPLEX" \ 126 /* "\002 */ \ 127 "\003SOFTPCMVOL" \ 128 "\004BUSY" \ 129 "\005MPSAFE" \ 130 "\006REGISTERED" \ 131 "\007BITPERFECT" \ 132 "\010VPC" \ 133 "\011EQ" \ 134 "\012EQ_ENABLED" \ 135 "\013EQ_BYPASSED" \ 136 "\014EQ_PC" \ 137 "\015PVCHANS" \ 138 "\016RVCHANS" 139 140 #define PCM_ALIVE(x) ((x) != NULL && (x)->lock != NULL) 141 #define PCM_REGISTERED(x) (PCM_ALIVE(x) && ((x)->flags & SD_F_REGISTERED)) 142 143 #define PCM_MAXCHANS 10000 144 #define PCM_CHANCOUNT(d) \ 145 (d->playcount + d->pvchancount + d->reccount + d->rvchancount) 146 147 /* many variables should be reduced to a range. Here define a macro */ 148 #define RANGE(var, low, high) (var) = \ 149 (((var)<(low))? (low) : ((var)>(high))? (high) : (var)) 150 151 enum { 152 SND_DEV_CTL = 0, /* Control port /dev/mixer */ 153 SND_DEV_SEQ, /* Sequencer /dev/sequencer */ 154 SND_DEV_MIDIN, /* Raw midi access */ 155 SND_DEV_DSP, /* Digitized voice /dev/dsp */ 156 SND_DEV_STATUS, /* /dev/sndstat */ 157 }; 158 159 #define DSP_DEFAULT_SPEED 8000 160 161 extern int snd_unit; 162 extern int snd_verbose; 163 extern devclass_t pcm_devclass; 164 extern struct unrhdr *pcmsg_unrhdr; 165 166 #ifndef DEB 167 #define DEB(x) 168 #endif 169 170 SYSCTL_DECL(_hw_snd); 171 172 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo); 173 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz); 174 void pcm_init(device_t dev, void *devinfo); 175 int pcm_register(device_t dev, char *str); 176 int pcm_unregister(device_t dev); 177 u_int32_t pcm_getflags(device_t dev); 178 void pcm_setflags(device_t dev, u_int32_t val); 179 void *pcm_getdevinfo(device_t dev); 180 181 int snd_setup_intr(device_t dev, struct resource *res, int flags, 182 driver_intr_t hand, void *param, void **cookiep); 183 184 void *snd_mtxcreate(const char *desc, const char *type); 185 void snd_mtxfree(void *m); 186 void snd_mtxassert(void *m); 187 #define snd_mtxlock(m) mtx_lock(m) 188 #define snd_mtxunlock(m) mtx_unlock(m) 189 190 int sndstat_register(device_t dev, char *str); 191 int sndstat_unregister(device_t dev); 192 193 /* These are the function codes assigned to the children of sound cards. */ 194 enum { 195 SCF_PCM, 196 SCF_MIDI, 197 SCF_SYNTH, 198 }; 199 200 /* 201 * This is the device information struct, used by a bridge device to pass the 202 * device function code to the children. 203 */ 204 struct sndcard_func { 205 int func; /* The function code. */ 206 void *varinfo; /* Bridge-specific information. */ 207 }; 208 209 /* 210 * this is rather kludgey- we need to duplicate these struct def'ns from sound.c 211 * so that the macro versions of pcm_{,un}lock can dereference them. 212 * we also have to do this now makedev() has gone away. 213 */ 214 215 struct snddev_info { 216 struct { 217 struct { 218 SLIST_HEAD(, pcm_channel) head; 219 struct { 220 SLIST_HEAD(, pcm_channel) head; 221 } busy; 222 struct { 223 SLIST_HEAD(, pcm_channel) head; 224 } opened; 225 struct { 226 SLIST_HEAD(, pcm_channel) head; 227 } primary; 228 } pcm; 229 } channels; 230 unsigned playcount, reccount, pvchancount, rvchancount; 231 unsigned flags; 232 unsigned int bufsz; 233 void *devinfo; 234 device_t dev; 235 char status[SND_STATUSLEN]; 236 struct mtx *lock; 237 struct cdev *mixer_dev; 238 struct cdev *dsp_dev; 239 uint32_t pvchanrate, pvchanformat, pvchanmode; 240 uint32_t rvchanrate, rvchanformat, rvchanmode; 241 int32_t eqpreamp; 242 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx; 243 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree; 244 struct cv cv; 245 struct unrhdr *p_unr; 246 struct unrhdr *vp_unr; 247 struct unrhdr *r_unr; 248 struct unrhdr *vr_unr; 249 }; 250 251 void sound_oss_sysinfo(oss_sysinfo *); 252 int sound_oss_card_info(oss_card_info *); 253 254 #define PCM_MODE_MIXER 0x01 255 #define PCM_MODE_PLAY 0x02 256 #define PCM_MODE_REC 0x04 257 258 #define PCM_LOCKOWNED(d) mtx_owned((d)->lock) 259 #define PCM_LOCK(d) mtx_lock((d)->lock) 260 #define PCM_UNLOCK(d) mtx_unlock((d)->lock) 261 #define PCM_TRYLOCK(d) mtx_trylock((d)->lock) 262 #define PCM_LOCKASSERT(d) mtx_assert((d)->lock, MA_OWNED) 263 #define PCM_UNLOCKASSERT(d) mtx_assert((d)->lock, MA_NOTOWNED) 264 265 /* 266 * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these 267 * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you! 268 */ 269 #ifdef SND_DIAGNOSTIC 270 #define PCM_WAIT(x) do { \ 271 if (!PCM_LOCKOWNED(x)) \ 272 panic("%s(%d): [PCM WAIT] Mutex not owned!", \ 273 __func__, __LINE__); \ 274 while ((x)->flags & SD_F_BUSY) { \ 275 if (snd_verbose > 3) \ 276 device_printf((x)->dev, \ 277 "%s(%d): [PCM WAIT] calling cv_wait().\n", \ 278 __func__, __LINE__); \ 279 cv_wait(&(x)->cv, (x)->lock); \ 280 } \ 281 } while (0) 282 283 #define PCM_ACQUIRE(x) do { \ 284 if (!PCM_LOCKOWNED(x)) \ 285 panic("%s(%d): [PCM ACQUIRE] Mutex not owned!", \ 286 __func__, __LINE__); \ 287 if ((x)->flags & SD_F_BUSY) \ 288 panic("%s(%d): [PCM ACQUIRE] " \ 289 "Trying to acquire BUSY cv!", __func__, __LINE__); \ 290 (x)->flags |= SD_F_BUSY; \ 291 } while (0) 292 293 #define PCM_RELEASE(x) do { \ 294 if (!PCM_LOCKOWNED(x)) \ 295 panic("%s(%d): [PCM RELEASE] Mutex not owned!", \ 296 __func__, __LINE__); \ 297 if ((x)->flags & SD_F_BUSY) { \ 298 (x)->flags &= ~SD_F_BUSY; \ 299 cv_broadcast(&(x)->cv); \ 300 } else \ 301 panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 302 __func__, __LINE__); \ 303 } while (0) 304 305 /* Quick version, for shorter path. */ 306 #define PCM_ACQUIRE_QUICK(x) do { \ 307 if (PCM_LOCKOWNED(x)) \ 308 panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!", \ 309 __func__, __LINE__); \ 310 PCM_LOCK(x); \ 311 PCM_WAIT(x); \ 312 PCM_ACQUIRE(x); \ 313 PCM_UNLOCK(x); \ 314 } while (0) 315 316 #define PCM_RELEASE_QUICK(x) do { \ 317 if (PCM_LOCKOWNED(x)) \ 318 panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!", \ 319 __func__, __LINE__); \ 320 PCM_LOCK(x); \ 321 PCM_RELEASE(x); \ 322 PCM_UNLOCK(x); \ 323 } while (0) 324 325 #define PCM_BUSYASSERT(x) do { \ 326 if (!((x) != NULL && ((x)->flags & SD_F_BUSY))) \ 327 panic("%s(%d): [PCM BUSYASSERT] " \ 328 "Failed, snddev_info=%p", __func__, __LINE__, x); \ 329 } while (0) 330 331 #define PCM_GIANT_ENTER(x) do { \ 332 int _pcm_giant = 0; \ 333 if (PCM_LOCKOWNED(x)) \ 334 panic("%s(%d): [GIANT ENTER] PCM lock owned!", \ 335 __func__, __LINE__); \ 336 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \ 337 device_printf((x)->dev, \ 338 "%s(%d): [GIANT ENTER] Giant owned!\n", \ 339 __func__, __LINE__); \ 340 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 341 do { \ 342 mtx_lock(&Giant); \ 343 _pcm_giant = 1; \ 344 } while (0) 345 346 #define PCM_GIANT_EXIT(x) do { \ 347 if (PCM_LOCKOWNED(x)) \ 348 panic("%s(%d): [GIANT EXIT] PCM lock owned!", \ 349 __func__, __LINE__); \ 350 if (!(_pcm_giant == 0 || _pcm_giant == 1)) \ 351 panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 352 __func__, __LINE__); \ 353 if ((x)->flags & SD_F_MPSAFE) { \ 354 if (_pcm_giant == 1) \ 355 panic("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 356 __func__, __LINE__); \ 357 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \ 358 device_printf((x)->dev, \ 359 "%s(%d): [GIANT EXIT] Giant owned!\n", \ 360 __func__, __LINE__); \ 361 } \ 362 if (_pcm_giant != 0) { \ 363 if (mtx_owned(&Giant) == 0) \ 364 panic("%s(%d): [GIANT EXIT] Giant not owned!", \ 365 __func__, __LINE__); \ 366 _pcm_giant = 0; \ 367 mtx_unlock(&Giant); \ 368 } \ 369 } while (0) 370 #else /* !SND_DIAGNOSTIC */ 371 #define PCM_WAIT(x) do { \ 372 PCM_LOCKASSERT(x); \ 373 while ((x)->flags & SD_F_BUSY) \ 374 cv_wait(&(x)->cv, (x)->lock); \ 375 } while (0) 376 377 #define PCM_ACQUIRE(x) do { \ 378 PCM_LOCKASSERT(x); \ 379 KASSERT(!((x)->flags & SD_F_BUSY), \ 380 ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \ 381 __func__, __LINE__)); \ 382 (x)->flags |= SD_F_BUSY; \ 383 } while (0) 384 385 #define PCM_RELEASE(x) do { \ 386 PCM_LOCKASSERT(x); \ 387 KASSERT((x)->flags & SD_F_BUSY, \ 388 ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 389 __func__, __LINE__)); \ 390 (x)->flags &= ~SD_F_BUSY; \ 391 cv_broadcast(&(x)->cv); \ 392 } while (0) 393 394 /* Quick version, for shorter path. */ 395 #define PCM_ACQUIRE_QUICK(x) do { \ 396 PCM_UNLOCKASSERT(x); \ 397 PCM_LOCK(x); \ 398 PCM_WAIT(x); \ 399 PCM_ACQUIRE(x); \ 400 PCM_UNLOCK(x); \ 401 } while (0) 402 403 #define PCM_RELEASE_QUICK(x) do { \ 404 PCM_UNLOCKASSERT(x); \ 405 PCM_LOCK(x); \ 406 PCM_RELEASE(x); \ 407 PCM_UNLOCK(x); \ 408 } while (0) 409 410 #define PCM_BUSYASSERT(x) KASSERT(x != NULL && \ 411 ((x)->flags & SD_F_BUSY), \ 412 ("%s(%d): [PCM BUSYASSERT] " \ 413 "Failed, snddev_info=%p", \ 414 __func__, __LINE__, x)) 415 416 #define PCM_GIANT_ENTER(x) do { \ 417 int _pcm_giant = 0; \ 418 PCM_UNLOCKASSERT(x); \ 419 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 420 do { \ 421 mtx_lock(&Giant); \ 422 _pcm_giant = 1; \ 423 } while (0) 424 425 #define PCM_GIANT_EXIT(x) do { \ 426 PCM_UNLOCKASSERT(x); \ 427 KASSERT(_pcm_giant == 0 || _pcm_giant == 1, \ 428 ("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 429 __func__, __LINE__)); \ 430 KASSERT(!((x)->flags & SD_F_MPSAFE) || \ 431 (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0), \ 432 ("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 433 __func__, __LINE__)); \ 434 if (_pcm_giant != 0) { \ 435 mtx_assert(&Giant, MA_OWNED); \ 436 _pcm_giant = 0; \ 437 mtx_unlock(&Giant); \ 438 } \ 439 } while (0) 440 #endif /* SND_DIAGNOSTIC */ 441 442 #define PCM_GIANT_LEAVE(x) \ 443 PCM_GIANT_EXIT(x); \ 444 } while (0) 445 446 #endif /* _KERNEL */ 447 448 /* make figuring out what a format is easier. got AFMT_STEREO already */ 449 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE | \ 450 AFMT_F32_LE | AFMT_F32_BE) 451 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE) 452 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE) 453 #define AFMT_G711 (AFMT_MU_LAW | AFMT_A_LAW) 454 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8) 455 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_F32_LE | AFMT_F32_BE | \ 456 AFMT_S24_LE | AFMT_S24_BE | \ 457 AFMT_S16_LE | AFMT_S16_BE | AFMT_S8) 458 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_F32_BE | \ 459 AFMT_S24_BE | AFMT_U24_BE | AFMT_S16_BE | AFMT_U16_BE) 460 461 #define AFMT_CONVERTIBLE (AFMT_8BIT | AFMT_16BIT | AFMT_24BIT | \ 462 AFMT_32BIT) 463 464 /* Supported vchan mixing formats */ 465 #define AFMT_VCHAN (AFMT_CONVERTIBLE & ~AFMT_G711) 466 467 #define AFMT_PASSTHROUGH AFMT_AC3 468 #define AFMT_PASSTHROUGH_RATE 48000 469 #define AFMT_PASSTHROUGH_CHANNEL 2 470 #define AFMT_PASSTHROUGH_EXTCHANNEL 0 471 472 /* 473 * We're simply using unused, contiguous bits from various AFMT_ definitions. 474 * ~(0xb00ff7ff) 475 */ 476 #define AFMT_ENCODING_MASK 0xf00fffff 477 #define AFMT_CHANNEL_MASK 0x07f00000 478 #define AFMT_CHANNEL_SHIFT 20 479 #define AFMT_CHANNEL_MAX 0x7f 480 #define AFMT_EXTCHANNEL_MASK 0x08000000 481 #define AFMT_EXTCHANNEL_SHIFT 27 482 #define AFMT_EXTCHANNEL_MAX 1 483 484 #define AFMT_ENCODING(v) ((v) & AFMT_ENCODING_MASK) 485 486 #define AFMT_EXTCHANNEL(v) (((v) & AFMT_EXTCHANNEL_MASK) >> \ 487 AFMT_EXTCHANNEL_SHIFT) 488 489 #define AFMT_CHANNEL(v) (((v) & AFMT_CHANNEL_MASK) >> \ 490 AFMT_CHANNEL_SHIFT) 491 492 #define AFMT_BIT(v) (((v) & AFMT_32BIT) ? 32 : \ 493 (((v) & AFMT_24BIT) ? 24 : \ 494 ((((v) & AFMT_16BIT) || \ 495 ((v) & AFMT_PASSTHROUGH)) ? 16 : 8))) 496 497 #define AFMT_BPS(v) (AFMT_BIT(v) >> 3) 498 #define AFMT_ALIGN(v) (AFMT_BPS(v) * AFMT_CHANNEL(v)) 499 500 #define SND_FORMAT(f, c, e) (AFMT_ENCODING(f) | \ 501 (((c) << AFMT_CHANNEL_SHIFT) & \ 502 AFMT_CHANNEL_MASK) | \ 503 (((e) << AFMT_EXTCHANNEL_SHIFT) & \ 504 AFMT_EXTCHANNEL_MASK)) 505 506 #define AFMT_U8_NE AFMT_U8 507 #define AFMT_S8_NE AFMT_S8 508 509 #define AFMT_SIGNED_NE (AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | \ 510 AFMT_S32_NE | AFMT_F32_NE) 511 512 #define AFMT_NE (AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE | \ 513 AFMT_U24_NE | AFMT_U32_NE) 514 515 #endif /* _OS_H_ */ 516