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