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