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