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 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/feeder.h> 88 #include <dev/sound/pcm/mixer.h> 89 #include <dev/sound/pcm/dsp.h> 90 91 #define PCM_SOFTC_SIZE (sizeof(struct snddev_info)) 92 93 #define SND_STATUSLEN 64 94 95 #define SOUND_MODVER 5 96 97 #define SOUND_MINVER SOUND_MODVER 98 #define SOUND_PREFVER SOUND_MODVER 99 #define SOUND_MAXVER SOUND_MODVER 100 101 #define SD_F_SIMPLEX 0x00000001 102 /* unused 0x00000002 */ 103 #define SD_F_SOFTPCMVOL 0x00000004 104 #define SD_F_BUSY 0x00000008 105 #define SD_F_MPSAFE 0x00000010 106 #define SD_F_REGISTERED 0x00000020 107 #define SD_F_BITPERFECT 0x00000040 108 #define SD_F_VPC 0x00000080 /* volume-per-channel */ 109 #define SD_F_EQ 0x00000100 /* EQ */ 110 #define SD_F_EQ_ENABLED 0x00000200 /* EQ enabled */ 111 #define SD_F_EQ_BYPASSED 0x00000400 /* EQ bypassed */ 112 #define SD_F_EQ_PC 0x00000800 /* EQ per-channel */ 113 #define SD_F_PVCHANS 0x00001000 /* Playback vchans enabled */ 114 #define SD_F_RVCHANS 0x00002000 /* Recording vchans enabled */ 115 116 #define SD_F_EQ_DEFAULT (SD_F_EQ | SD_F_EQ_ENABLED) 117 #define SD_F_EQ_MASK (SD_F_EQ | SD_F_EQ_ENABLED | \ 118 SD_F_EQ_BYPASSED | SD_F_EQ_PC) 119 120 #define SD_F_BITS "\020" \ 121 "\001SIMPLEX" \ 122 /* "\002 */ \ 123 "\003SOFTPCMVOL" \ 124 "\004BUSY" \ 125 "\005MPSAFE" \ 126 "\006REGISTERED" \ 127 "\007BITPERFECT" \ 128 "\010VPC" \ 129 "\011EQ" \ 130 "\012EQ_ENABLED" \ 131 "\013EQ_BYPASSED" \ 132 "\014EQ_PC" \ 133 "\015PVCHANS" \ 134 "\016RVCHANS" 135 136 #define PCM_REGISTERED(x) \ 137 ((x) != NULL && ((x)->flags & SD_F_REGISTERED)) 138 139 #define PCM_MAXCHANS 10000 140 #define PCM_CHANCOUNT(d) \ 141 (d->playcount + d->pvchancount + d->reccount + d->rvchancount) 142 143 /* many variables should be reduced to a range. Here define a macro */ 144 #define RANGE(var, low, high) (var) = \ 145 (((var)<(low))? (low) : ((var)>(high))? (high) : (var)) 146 147 extern int snd_unit; 148 extern int snd_verbose; 149 extern devclass_t pcm_devclass; 150 extern struct unrhdr *pcmsg_unrhdr; 151 152 #ifndef DEB 153 #define DEB(x) 154 #endif 155 156 SYSCTL_DECL(_hw_snd); 157 158 int pcm_addchan(device_t dev, int dir, kobj_class_t cls, void *devinfo); 159 unsigned int pcm_getbuffersize(device_t dev, unsigned int minbufsz, unsigned int deflt, unsigned int maxbufsz); 160 void pcm_init(device_t dev, void *devinfo); 161 int pcm_register(device_t dev, char *str); 162 int pcm_unregister(device_t dev); 163 u_int32_t pcm_getflags(device_t dev); 164 void pcm_setflags(device_t dev, u_int32_t val); 165 void *pcm_getdevinfo(device_t dev); 166 167 int snd_setup_intr(device_t dev, struct resource *res, int flags, 168 driver_intr_t hand, void *param, void **cookiep); 169 170 int sndstat_register(device_t dev, char *str); 171 int sndstat_unregister(device_t dev); 172 173 /* These are the function codes assigned to the children of sound cards. */ 174 enum { 175 SCF_PCM, 176 SCF_MIDI, 177 }; 178 179 /* 180 * This is the device information struct, used by a bridge device to pass the 181 * device function code to the children. 182 */ 183 struct sndcard_func { 184 int func; /* The function code. */ 185 void *varinfo; /* Bridge-specific information. */ 186 }; 187 188 /* 189 * this is rather kludgey- we need to duplicate these struct def'ns from sound.c 190 * so that the macro versions of pcm_{,un}lock can dereference them. 191 * we also have to do this now makedev() has gone away. 192 */ 193 194 struct snddev_info { 195 struct { 196 struct { 197 SLIST_HEAD(, pcm_channel) head; 198 struct { 199 SLIST_HEAD(, pcm_channel) head; 200 } busy; 201 struct { 202 SLIST_HEAD(, pcm_channel) head; 203 } opened; 204 struct { 205 SLIST_HEAD(, pcm_channel) head; 206 } primary; 207 } pcm; 208 } channels; 209 unsigned playcount, reccount, pvchancount, rvchancount; 210 unsigned flags; 211 unsigned int bufsz; 212 void *devinfo; 213 device_t dev; 214 char status[SND_STATUSLEN]; 215 struct mtx lock; 216 struct cdev *mixer_dev; 217 struct cdev *dsp_dev; 218 uint32_t pvchanrate, pvchanformat, pvchanmode; 219 uint32_t rvchanrate, rvchanformat, rvchanmode; 220 int32_t eqpreamp; 221 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx; 222 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree; 223 struct cv cv; 224 struct unrhdr *p_unr; 225 struct unrhdr *vp_unr; 226 struct unrhdr *r_unr; 227 struct unrhdr *vr_unr; 228 }; 229 230 void sound_oss_sysinfo(oss_sysinfo *); 231 int sound_oss_card_info(oss_card_info *); 232 233 #define PCM_MODE_MIXER 0x01 234 #define PCM_MODE_PLAY 0x02 235 #define PCM_MODE_REC 0x04 236 237 #define PCM_LOCKOWNED(d) mtx_owned(&(d)->lock) 238 #define PCM_LOCK(d) mtx_lock(&(d)->lock) 239 #define PCM_UNLOCK(d) mtx_unlock(&(d)->lock) 240 #define PCM_TRYLOCK(d) mtx_trylock(&(d)->lock) 241 #define PCM_LOCKASSERT(d) mtx_assert(&(d)->lock, MA_OWNED) 242 #define PCM_UNLOCKASSERT(d) mtx_assert(&(d)->lock, MA_NOTOWNED) 243 244 /* 245 * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these 246 * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you! 247 */ 248 #define PCM_WAIT(x) do { \ 249 PCM_LOCKASSERT(x); \ 250 while ((x)->flags & SD_F_BUSY) \ 251 cv_wait(&(x)->cv, &(x)->lock); \ 252 } while (0) 253 254 #define PCM_ACQUIRE(x) do { \ 255 PCM_LOCKASSERT(x); \ 256 KASSERT(!((x)->flags & SD_F_BUSY), \ 257 ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \ 258 __func__, __LINE__)); \ 259 (x)->flags |= SD_F_BUSY; \ 260 } while (0) 261 262 #define PCM_RELEASE(x) do { \ 263 PCM_LOCKASSERT(x); \ 264 KASSERT((x)->flags & SD_F_BUSY, \ 265 ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 266 __func__, __LINE__)); \ 267 (x)->flags &= ~SD_F_BUSY; \ 268 cv_broadcast(&(x)->cv); \ 269 } while (0) 270 271 /* Quick version, for shorter path. */ 272 #define PCM_ACQUIRE_QUICK(x) do { \ 273 PCM_UNLOCKASSERT(x); \ 274 PCM_LOCK(x); \ 275 PCM_WAIT(x); \ 276 PCM_ACQUIRE(x); \ 277 PCM_UNLOCK(x); \ 278 } while (0) 279 280 #define PCM_RELEASE_QUICK(x) do { \ 281 PCM_UNLOCKASSERT(x); \ 282 PCM_LOCK(x); \ 283 PCM_RELEASE(x); \ 284 PCM_UNLOCK(x); \ 285 } while (0) 286 287 #define PCM_BUSYASSERT(x) KASSERT(x != NULL && \ 288 ((x)->flags & SD_F_BUSY), \ 289 ("%s(%d): [PCM BUSYASSERT] " \ 290 "Failed, snddev_info=%p", \ 291 __func__, __LINE__, x)) 292 293 #define PCM_GIANT_ENTER(x) do { \ 294 int _pcm_giant = 0; \ 295 PCM_UNLOCKASSERT(x); \ 296 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 297 do { \ 298 mtx_lock(&Giant); \ 299 _pcm_giant = 1; \ 300 } while (0) 301 302 #define PCM_GIANT_EXIT(x) do { \ 303 PCM_UNLOCKASSERT(x); \ 304 KASSERT(_pcm_giant == 0 || _pcm_giant == 1, \ 305 ("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 306 __func__, __LINE__)); \ 307 KASSERT(!((x)->flags & SD_F_MPSAFE) || \ 308 (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0), \ 309 ("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 310 __func__, __LINE__)); \ 311 if (_pcm_giant != 0) { \ 312 mtx_assert(&Giant, MA_OWNED); \ 313 _pcm_giant = 0; \ 314 mtx_unlock(&Giant); \ 315 } \ 316 } while (0) 317 318 #define PCM_GIANT_LEAVE(x) \ 319 PCM_GIANT_EXIT(x); \ 320 } while (0) 321 322 #endif /* _KERNEL */ 323 324 /* make figuring out what a format is easier. got AFMT_STEREO already */ 325 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE | \ 326 AFMT_F32_LE | AFMT_F32_BE) 327 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE) 328 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE) 329 #define AFMT_G711 (AFMT_MU_LAW | AFMT_A_LAW) 330 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8) 331 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_F32_LE | AFMT_F32_BE | \ 332 AFMT_S24_LE | AFMT_S24_BE | \ 333 AFMT_S16_LE | AFMT_S16_BE | AFMT_S8) 334 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_F32_BE | \ 335 AFMT_S24_BE | AFMT_U24_BE | AFMT_S16_BE | AFMT_U16_BE) 336 337 #define AFMT_CONVERTIBLE (AFMT_8BIT | AFMT_16BIT | AFMT_24BIT | \ 338 AFMT_32BIT) 339 340 /* Supported vchan mixing formats */ 341 #define AFMT_VCHAN (AFMT_CONVERTIBLE & ~AFMT_G711) 342 343 #define AFMT_PASSTHROUGH AFMT_AC3 344 #define AFMT_PASSTHROUGH_RATE 48000 345 #define AFMT_PASSTHROUGH_CHANNEL 2 346 #define AFMT_PASSTHROUGH_EXTCHANNEL 0 347 348 /* 349 * We're simply using unused, contiguous bits from various AFMT_ definitions. 350 * ~(0xb00ff7ff) 351 */ 352 #define AFMT_ENCODING_MASK 0xf00fffff 353 #define AFMT_CHANNEL_MASK 0x07f00000 354 #define AFMT_CHANNEL_SHIFT 20 355 #define AFMT_CHANNEL_MAX 0x7f 356 #define AFMT_EXTCHANNEL_MASK 0x08000000 357 #define AFMT_EXTCHANNEL_SHIFT 27 358 #define AFMT_EXTCHANNEL_MAX 1 359 360 #define AFMT_ENCODING(v) ((v) & AFMT_ENCODING_MASK) 361 362 #define AFMT_EXTCHANNEL(v) (((v) & AFMT_EXTCHANNEL_MASK) >> \ 363 AFMT_EXTCHANNEL_SHIFT) 364 365 #define AFMT_CHANNEL(v) (((v) & AFMT_CHANNEL_MASK) >> \ 366 AFMT_CHANNEL_SHIFT) 367 368 #define AFMT_BIT(v) (((v) & AFMT_32BIT) ? 32 : \ 369 (((v) & AFMT_24BIT) ? 24 : \ 370 ((((v) & AFMT_16BIT) || \ 371 ((v) & AFMT_PASSTHROUGH)) ? 16 : 8))) 372 373 #define AFMT_BPS(v) (AFMT_BIT(v) >> 3) 374 #define AFMT_ALIGN(v) (AFMT_BPS(v) * AFMT_CHANNEL(v)) 375 376 #define SND_FORMAT(f, c, e) (AFMT_ENCODING(f) | \ 377 (((c) << AFMT_CHANNEL_SHIFT) & \ 378 AFMT_CHANNEL_MASK) | \ 379 (((e) << AFMT_EXTCHANNEL_SHIFT) & \ 380 AFMT_EXTCHANNEL_MASK)) 381 382 #define AFMT_U8_NE AFMT_U8 383 #define AFMT_S8_NE AFMT_S8 384 385 #define AFMT_SIGNED_NE (AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | \ 386 AFMT_S32_NE | AFMT_F32_NE) 387 388 #define AFMT_NE (AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE | \ 389 AFMT_U24_NE | AFMT_U32_NE) 390 391 #endif /* _OS_H_ */ 392