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_PRIO_RD 0x10000000 125 #define SD_F_PRIO_WR 0x20000000 126 127 #define SD_F_BITS "\020" \ 128 "\001SIMPLEX" \ 129 /* "\002 */ \ 130 "\003SOFTPCMVOL" \ 131 "\004BUSY" \ 132 "\005MPSAFE" \ 133 "\006REGISTERED" \ 134 "\007BITPERFECT" \ 135 "\010VPC" \ 136 "\011EQ" \ 137 "\012EQ_ENABLED" \ 138 "\013EQ_BYPASSED" \ 139 "\014EQ_PC" \ 140 "\015PVCHANS" \ 141 "\016RVCHANS" \ 142 "\035PRIO_RD" \ 143 "\036PRIO_WR" 144 145 #define PCM_ALIVE(x) ((x) != NULL && (x)->lock != NULL) 146 #define PCM_REGISTERED(x) (PCM_ALIVE(x) && ((x)->flags & SD_F_REGISTERED)) 147 148 #define PCM_MAXCHANS 10000 149 #define PCM_CHANCOUNT(d) \ 150 (d->playcount + d->pvchancount + d->reccount + d->rvchancount) 151 152 /* many variables should be reduced to a range. Here define a macro */ 153 #define RANGE(var, low, high) (var) = \ 154 (((var)<(low))? (low) : ((var)>(high))? (high) : (var)) 155 156 enum { 157 SND_DEV_CTL = 0, /* Control port /dev/mixer */ 158 SND_DEV_SEQ, /* Sequencer /dev/sequencer */ 159 SND_DEV_MIDIN, /* Raw midi access */ 160 SND_DEV_DSP, /* Digitized voice /dev/dsp */ 161 SND_DEV_STATUS, /* /dev/sndstat */ 162 }; 163 164 #define DSP_DEFAULT_SPEED 8000 165 166 extern int snd_unit; 167 extern int snd_verbose; 168 extern devclass_t pcm_devclass; 169 extern struct unrhdr *pcmsg_unrhdr; 170 171 #ifndef DEB 172 #define DEB(x) 173 #endif 174 175 SYSCTL_DECL(_hw_snd); 176 177 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo); 178 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz); 179 void pcm_init(device_t dev, void *devinfo); 180 int pcm_register(device_t dev, char *str); 181 int pcm_unregister(device_t dev); 182 u_int32_t pcm_getflags(device_t dev); 183 void pcm_setflags(device_t dev, u_int32_t val); 184 void *pcm_getdevinfo(device_t dev); 185 186 int snd_setup_intr(device_t dev, struct resource *res, int flags, 187 driver_intr_t hand, void *param, void **cookiep); 188 189 void *snd_mtxcreate(const char *desc, const char *type); 190 void snd_mtxfree(void *m); 191 void snd_mtxassert(void *m); 192 #define snd_mtxlock(m) mtx_lock(m) 193 #define snd_mtxunlock(m) mtx_unlock(m) 194 195 int sndstat_register(device_t dev, char *str); 196 int sndstat_unregister(device_t dev); 197 198 /* These are the function codes assigned to the children of sound cards. */ 199 enum { 200 SCF_PCM, 201 SCF_MIDI, 202 SCF_SYNTH, 203 }; 204 205 /* 206 * This is the device information struct, used by a bridge device to pass the 207 * device function code to the children. 208 */ 209 struct sndcard_func { 210 int func; /* The function code. */ 211 void *varinfo; /* Bridge-specific information. */ 212 }; 213 214 /* 215 * this is rather kludgey- we need to duplicate these struct def'ns from sound.c 216 * so that the macro versions of pcm_{,un}lock can dereference them. 217 * we also have to do this now makedev() has gone away. 218 */ 219 220 struct snddev_info { 221 struct { 222 struct { 223 SLIST_HEAD(, pcm_channel) head; 224 struct { 225 SLIST_HEAD(, pcm_channel) head; 226 } busy; 227 struct { 228 SLIST_HEAD(, pcm_channel) head; 229 } opened; 230 struct { 231 SLIST_HEAD(, pcm_channel) head; 232 } primary; 233 } pcm; 234 } channels; 235 unsigned playcount, reccount, pvchancount, rvchancount; 236 unsigned flags; 237 unsigned int bufsz; 238 void *devinfo; 239 device_t dev; 240 char status[SND_STATUSLEN]; 241 struct mtx *lock; 242 struct cdev *mixer_dev; 243 struct cdev *dsp_dev; 244 uint32_t pvchanrate, pvchanformat, pvchanmode; 245 uint32_t rvchanrate, rvchanformat, rvchanmode; 246 int32_t eqpreamp; 247 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx; 248 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree; 249 struct cv cv; 250 struct unrhdr *p_unr; 251 struct unrhdr *vp_unr; 252 struct unrhdr *r_unr; 253 struct unrhdr *vr_unr; 254 }; 255 256 void sound_oss_sysinfo(oss_sysinfo *); 257 int sound_oss_card_info(oss_card_info *); 258 259 #define PCM_MODE_MIXER 0x01 260 #define PCM_MODE_PLAY 0x02 261 #define PCM_MODE_REC 0x04 262 263 #define PCM_LOCKOWNED(d) mtx_owned((d)->lock) 264 #define PCM_LOCK(d) mtx_lock((d)->lock) 265 #define PCM_UNLOCK(d) mtx_unlock((d)->lock) 266 #define PCM_TRYLOCK(d) mtx_trylock((d)->lock) 267 #define PCM_LOCKASSERT(d) mtx_assert((d)->lock, MA_OWNED) 268 #define PCM_UNLOCKASSERT(d) mtx_assert((d)->lock, MA_NOTOWNED) 269 270 /* 271 * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these 272 * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you! 273 */ 274 #ifdef SND_DIAGNOSTIC 275 #define PCM_WAIT(x) do { \ 276 if (!PCM_LOCKOWNED(x)) \ 277 panic("%s(%d): [PCM WAIT] Mutex not owned!", \ 278 __func__, __LINE__); \ 279 while ((x)->flags & SD_F_BUSY) { \ 280 if (snd_verbose > 3) \ 281 device_printf((x)->dev, \ 282 "%s(%d): [PCM WAIT] calling cv_wait().\n", \ 283 __func__, __LINE__); \ 284 cv_wait(&(x)->cv, (x)->lock); \ 285 } \ 286 } while (0) 287 288 #define PCM_ACQUIRE(x) do { \ 289 if (!PCM_LOCKOWNED(x)) \ 290 panic("%s(%d): [PCM ACQUIRE] Mutex not owned!", \ 291 __func__, __LINE__); \ 292 if ((x)->flags & SD_F_BUSY) \ 293 panic("%s(%d): [PCM ACQUIRE] " \ 294 "Trying to acquire BUSY cv!", __func__, __LINE__); \ 295 (x)->flags |= SD_F_BUSY; \ 296 } while (0) 297 298 #define PCM_RELEASE(x) do { \ 299 if (!PCM_LOCKOWNED(x)) \ 300 panic("%s(%d): [PCM RELEASE] Mutex not owned!", \ 301 __func__, __LINE__); \ 302 if ((x)->flags & SD_F_BUSY) { \ 303 (x)->flags &= ~SD_F_BUSY; \ 304 cv_broadcast(&(x)->cv); \ 305 } else \ 306 panic("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 307 __func__, __LINE__); \ 308 } while (0) 309 310 /* Quick version, for shorter path. */ 311 #define PCM_ACQUIRE_QUICK(x) do { \ 312 if (PCM_LOCKOWNED(x)) \ 313 panic("%s(%d): [PCM ACQUIRE QUICK] Mutex owned!", \ 314 __func__, __LINE__); \ 315 PCM_LOCK(x); \ 316 PCM_WAIT(x); \ 317 PCM_ACQUIRE(x); \ 318 PCM_UNLOCK(x); \ 319 } while (0) 320 321 #define PCM_RELEASE_QUICK(x) do { \ 322 if (PCM_LOCKOWNED(x)) \ 323 panic("%s(%d): [PCM RELEASE QUICK] Mutex owned!", \ 324 __func__, __LINE__); \ 325 PCM_LOCK(x); \ 326 PCM_RELEASE(x); \ 327 PCM_UNLOCK(x); \ 328 } while (0) 329 330 #define PCM_BUSYASSERT(x) do { \ 331 if (!((x) != NULL && ((x)->flags & SD_F_BUSY))) \ 332 panic("%s(%d): [PCM BUSYASSERT] " \ 333 "Failed, snddev_info=%p", __func__, __LINE__, x); \ 334 } while (0) 335 336 #define PCM_GIANT_ENTER(x) do { \ 337 int _pcm_giant = 0; \ 338 if (PCM_LOCKOWNED(x)) \ 339 panic("%s(%d): [GIANT ENTER] PCM lock owned!", \ 340 __func__, __LINE__); \ 341 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \ 342 device_printf((x)->dev, \ 343 "%s(%d): [GIANT ENTER] Giant owned!\n", \ 344 __func__, __LINE__); \ 345 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 346 do { \ 347 mtx_lock(&Giant); \ 348 _pcm_giant = 1; \ 349 } while (0) 350 351 #define PCM_GIANT_EXIT(x) do { \ 352 if (PCM_LOCKOWNED(x)) \ 353 panic("%s(%d): [GIANT EXIT] PCM lock owned!", \ 354 __func__, __LINE__); \ 355 if (!(_pcm_giant == 0 || _pcm_giant == 1)) \ 356 panic("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 357 __func__, __LINE__); \ 358 if ((x)->flags & SD_F_MPSAFE) { \ 359 if (_pcm_giant == 1) \ 360 panic("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 361 __func__, __LINE__); \ 362 if (mtx_owned(&Giant) != 0 && snd_verbose > 3) \ 363 device_printf((x)->dev, \ 364 "%s(%d): [GIANT EXIT] Giant owned!\n", \ 365 __func__, __LINE__); \ 366 } \ 367 if (_pcm_giant != 0) { \ 368 if (mtx_owned(&Giant) == 0) \ 369 panic("%s(%d): [GIANT EXIT] Giant not owned!", \ 370 __func__, __LINE__); \ 371 _pcm_giant = 0; \ 372 mtx_unlock(&Giant); \ 373 } \ 374 } while (0) 375 #else /* !SND_DIAGNOSTIC */ 376 #define PCM_WAIT(x) do { \ 377 PCM_LOCKASSERT(x); \ 378 while ((x)->flags & SD_F_BUSY) \ 379 cv_wait(&(x)->cv, (x)->lock); \ 380 } while (0) 381 382 #define PCM_ACQUIRE(x) do { \ 383 PCM_LOCKASSERT(x); \ 384 KASSERT(!((x)->flags & SD_F_BUSY), \ 385 ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \ 386 __func__, __LINE__)); \ 387 (x)->flags |= SD_F_BUSY; \ 388 } while (0) 389 390 #define PCM_RELEASE(x) do { \ 391 PCM_LOCKASSERT(x); \ 392 KASSERT((x)->flags & SD_F_BUSY, \ 393 ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 394 __func__, __LINE__)); \ 395 (x)->flags &= ~SD_F_BUSY; \ 396 cv_broadcast(&(x)->cv); \ 397 } while (0) 398 399 /* Quick version, for shorter path. */ 400 #define PCM_ACQUIRE_QUICK(x) do { \ 401 PCM_UNLOCKASSERT(x); \ 402 PCM_LOCK(x); \ 403 PCM_WAIT(x); \ 404 PCM_ACQUIRE(x); \ 405 PCM_UNLOCK(x); \ 406 } while (0) 407 408 #define PCM_RELEASE_QUICK(x) do { \ 409 PCM_UNLOCKASSERT(x); \ 410 PCM_LOCK(x); \ 411 PCM_RELEASE(x); \ 412 PCM_UNLOCK(x); \ 413 } while (0) 414 415 #define PCM_BUSYASSERT(x) KASSERT(x != NULL && \ 416 ((x)->flags & SD_F_BUSY), \ 417 ("%s(%d): [PCM BUSYASSERT] " \ 418 "Failed, snddev_info=%p", \ 419 __func__, __LINE__, x)) 420 421 #define PCM_GIANT_ENTER(x) do { \ 422 int _pcm_giant = 0; \ 423 PCM_UNLOCKASSERT(x); \ 424 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 425 do { \ 426 mtx_lock(&Giant); \ 427 _pcm_giant = 1; \ 428 } while (0) 429 430 #define PCM_GIANT_EXIT(x) do { \ 431 PCM_UNLOCKASSERT(x); \ 432 KASSERT(_pcm_giant == 0 || _pcm_giant == 1, \ 433 ("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 434 __func__, __LINE__)); \ 435 KASSERT(!((x)->flags & SD_F_MPSAFE) || \ 436 (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0), \ 437 ("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 438 __func__, __LINE__)); \ 439 if (_pcm_giant != 0) { \ 440 mtx_assert(&Giant, MA_OWNED); \ 441 _pcm_giant = 0; \ 442 mtx_unlock(&Giant); \ 443 } \ 444 } while (0) 445 #endif /* SND_DIAGNOSTIC */ 446 447 #define PCM_GIANT_LEAVE(x) \ 448 PCM_GIANT_EXIT(x); \ 449 } while (0) 450 451 #endif /* _KERNEL */ 452 453 /* make figuring out what a format is easier. got AFMT_STEREO already */ 454 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE) 455 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE) 456 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE) 457 #define AFMT_G711 (AFMT_MU_LAW | AFMT_A_LAW) 458 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8) 459 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_S24_LE | AFMT_S24_BE | \ 460 AFMT_S16_LE | AFMT_S16_BE | AFMT_S8) 461 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_S24_BE | AFMT_U24_BE | \ 462 AFMT_S16_BE | AFMT_U16_BE) 463 464 #define AFMT_CONVERTIBLE (AFMT_8BIT | AFMT_16BIT | AFMT_24BIT | \ 465 AFMT_32BIT) 466 467 /* Supported vchan mixing formats */ 468 #define AFMT_VCHAN (AFMT_CONVERTIBLE & ~AFMT_G711) 469 470 #define AFMT_PASSTHROUGH AFMT_AC3 471 #define AFMT_PASSTHROUGH_RATE 48000 472 #define AFMT_PASSTHROUGH_CHANNEL 2 473 #define AFMT_PASSTHROUGH_EXTCHANNEL 0 474 475 /* 476 * We're simply using unused, contiguous bits from various AFMT_ definitions. 477 * ~(0xb00ff7ff) 478 */ 479 #define AFMT_ENCODING_MASK 0xf00fffff 480 #define AFMT_CHANNEL_MASK 0x07f00000 481 #define AFMT_CHANNEL_SHIFT 20 482 #define AFMT_CHANNEL_MAX 0x7f 483 #define AFMT_EXTCHANNEL_MASK 0x08000000 484 #define AFMT_EXTCHANNEL_SHIFT 27 485 #define AFMT_EXTCHANNEL_MAX 1 486 487 #define AFMT_ENCODING(v) ((v) & AFMT_ENCODING_MASK) 488 489 #define AFMT_EXTCHANNEL(v) (((v) & AFMT_EXTCHANNEL_MASK) >> \ 490 AFMT_EXTCHANNEL_SHIFT) 491 492 #define AFMT_CHANNEL(v) (((v) & AFMT_CHANNEL_MASK) >> \ 493 AFMT_CHANNEL_SHIFT) 494 495 #define AFMT_BIT(v) (((v) & AFMT_32BIT) ? 32 : \ 496 (((v) & AFMT_24BIT) ? 24 : \ 497 ((((v) & AFMT_16BIT) || \ 498 ((v) & AFMT_PASSTHROUGH)) ? 16 : 8))) 499 500 #define AFMT_BPS(v) (AFMT_BIT(v) >> 3) 501 #define AFMT_ALIGN(v) (AFMT_BPS(v) * AFMT_CHANNEL(v)) 502 503 #define SND_FORMAT(f, c, e) (AFMT_ENCODING(f) | \ 504 (((c) << AFMT_CHANNEL_SHIFT) & \ 505 AFMT_CHANNEL_MASK) | \ 506 (((e) << AFMT_EXTCHANNEL_SHIFT) & \ 507 AFMT_EXTCHANNEL_MASK)) 508 509 #define AFMT_U8_NE AFMT_U8 510 #define AFMT_S8_NE AFMT_S8 511 512 #define AFMT_SIGNED_NE (AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | AFMT_S32_NE) 513 514 #define AFMT_NE (AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE | \ 515 AFMT_U24_NE | AFMT_U32_NE) 516 517 #endif /* _OS_H_ */ 518