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