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