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 /* unused 0x00000080 */ 108 /* unused 0x00000100 */ 109 #define SD_F_EQ 0x00000200 /* EQ enabled */ 110 /* unused 0x00000400 */ 111 /* unused 0x00000800 */ 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 /* "\010 */ \ 124 /* "\011 */ \ 125 "\012EQ" \ 126 /* "\013 */ \ 127 /* "\014 */ \ 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 uint32_t pcm_getflags(device_t dev); 159 void pcm_setflags(device_t dev, uint32_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 /* 166 * this is rather kludgey- we need to duplicate these struct def'ns from sound.c 167 * so that the macro versions of pcm_{,un}lock can dereference them. 168 * we also have to do this now makedev() has gone away. 169 */ 170 171 struct snddev_info { 172 struct { 173 struct { 174 SLIST_HEAD(, pcm_channel) head; 175 struct { 176 SLIST_HEAD(, pcm_channel) head; 177 } busy; 178 struct { 179 SLIST_HEAD(, pcm_channel) head; 180 } opened; 181 struct { 182 SLIST_HEAD(, pcm_channel) head; 183 } primary; 184 } pcm; 185 } channels; 186 unsigned playcount, reccount, pvchancount, rvchancount; 187 unsigned flags; 188 void *devinfo; 189 device_t dev; 190 char status[SND_STATUSLEN]; 191 struct mtx lock; 192 struct cdev *mixer_dev; 193 struct cdev *dsp_dev; 194 uint32_t pvchanrate, pvchanformat, pvchanmode; 195 uint32_t rvchanrate, rvchanformat, rvchanmode; 196 int32_t eqpreamp; 197 struct sysctl_ctx_list play_sysctl_ctx, rec_sysctl_ctx; 198 struct sysctl_oid *play_sysctl_tree, *rec_sysctl_tree; 199 struct cv cv; 200 struct unrhdr *p_unr; 201 struct unrhdr *vp_unr; 202 struct unrhdr *r_unr; 203 struct unrhdr *vr_unr; 204 }; 205 206 void sound_oss_sysinfo(oss_sysinfo *); 207 int sound_oss_card_info(oss_card_info *); 208 209 #define PCM_MODE_MIXER 0x01 210 #define PCM_MODE_PLAY 0x02 211 #define PCM_MODE_REC 0x04 212 213 #define PCM_LOCKOWNED(d) mtx_owned(&(d)->lock) 214 #define PCM_LOCK(d) mtx_lock(&(d)->lock) 215 #define PCM_UNLOCK(d) mtx_unlock(&(d)->lock) 216 #define PCM_TRYLOCK(d) mtx_trylock(&(d)->lock) 217 #define PCM_LOCKASSERT(d) mtx_assert(&(d)->lock, MA_OWNED) 218 #define PCM_UNLOCKASSERT(d) mtx_assert(&(d)->lock, MA_NOTOWNED) 219 220 /* 221 * For PCM_[WAIT | ACQUIRE | RELEASE], be sure to surround these 222 * with PCM_LOCK/UNLOCK() sequence, or I'll come to gnaw upon you! 223 */ 224 #define PCM_WAIT(x) do { \ 225 PCM_LOCKASSERT(x); \ 226 while ((x)->flags & SD_F_BUSY) \ 227 cv_wait(&(x)->cv, &(x)->lock); \ 228 } while (0) 229 230 #define PCM_ACQUIRE(x) do { \ 231 PCM_LOCKASSERT(x); \ 232 KASSERT(!((x)->flags & SD_F_BUSY), \ 233 ("%s(%d): [PCM ACQUIRE] Trying to acquire BUSY cv!", \ 234 __func__, __LINE__)); \ 235 (x)->flags |= SD_F_BUSY; \ 236 } while (0) 237 238 #define PCM_RELEASE(x) do { \ 239 PCM_LOCKASSERT(x); \ 240 KASSERT((x)->flags & SD_F_BUSY, \ 241 ("%s(%d): [PCM RELEASE] Releasing non-BUSY cv!", \ 242 __func__, __LINE__)); \ 243 (x)->flags &= ~SD_F_BUSY; \ 244 cv_broadcast(&(x)->cv); \ 245 } while (0) 246 247 /* Quick version, for shorter path. */ 248 #define PCM_ACQUIRE_QUICK(x) do { \ 249 PCM_UNLOCKASSERT(x); \ 250 PCM_LOCK(x); \ 251 PCM_WAIT(x); \ 252 PCM_ACQUIRE(x); \ 253 PCM_UNLOCK(x); \ 254 } while (0) 255 256 #define PCM_RELEASE_QUICK(x) do { \ 257 PCM_UNLOCKASSERT(x); \ 258 PCM_LOCK(x); \ 259 PCM_RELEASE(x); \ 260 PCM_UNLOCK(x); \ 261 } while (0) 262 263 #define PCM_BUSYASSERT(x) KASSERT(x != NULL && \ 264 ((x)->flags & SD_F_BUSY), \ 265 ("%s(%d): [PCM BUSYASSERT] " \ 266 "Failed, snddev_info=%p", \ 267 __func__, __LINE__, x)) 268 269 #define PCM_GIANT_ENTER(x) do { \ 270 int _pcm_giant = 0; \ 271 PCM_UNLOCKASSERT(x); \ 272 if (!((x)->flags & SD_F_MPSAFE) && mtx_owned(&Giant) == 0) \ 273 do { \ 274 mtx_lock(&Giant); \ 275 _pcm_giant = 1; \ 276 } while (0) 277 278 #define PCM_GIANT_EXIT(x) do { \ 279 PCM_UNLOCKASSERT(x); \ 280 KASSERT(_pcm_giant == 0 || _pcm_giant == 1, \ 281 ("%s(%d): [GIANT EXIT] _pcm_giant screwed!", \ 282 __func__, __LINE__)); \ 283 KASSERT(!((x)->flags & SD_F_MPSAFE) || \ 284 (((x)->flags & SD_F_MPSAFE) && _pcm_giant == 0), \ 285 ("%s(%d): [GIANT EXIT] MPSAFE Giant?", \ 286 __func__, __LINE__)); \ 287 if (_pcm_giant != 0) { \ 288 mtx_assert(&Giant, MA_OWNED); \ 289 _pcm_giant = 0; \ 290 mtx_unlock(&Giant); \ 291 } \ 292 } while (0) 293 294 #define PCM_GIANT_LEAVE(x) \ 295 PCM_GIANT_EXIT(x); \ 296 } while (0) 297 298 #endif /* _KERNEL */ 299 300 /* make figuring out what a format is easier. got AFMT_STEREO already */ 301 #define AFMT_32BIT (AFMT_S32_LE | AFMT_S32_BE | AFMT_U32_LE | AFMT_U32_BE | \ 302 AFMT_F32_LE | AFMT_F32_BE) 303 #define AFMT_24BIT (AFMT_S24_LE | AFMT_S24_BE | AFMT_U24_LE | AFMT_U24_BE) 304 #define AFMT_16BIT (AFMT_S16_LE | AFMT_S16_BE | AFMT_U16_LE | AFMT_U16_BE) 305 #define AFMT_G711 (AFMT_MU_LAW | AFMT_A_LAW) 306 #define AFMT_8BIT (AFMT_G711 | AFMT_U8 | AFMT_S8) 307 #define AFMT_SIGNED (AFMT_S32_LE | AFMT_S32_BE | AFMT_F32_LE | AFMT_F32_BE | \ 308 AFMT_S24_LE | AFMT_S24_BE | \ 309 AFMT_S16_LE | AFMT_S16_BE | AFMT_S8) 310 #define AFMT_BIGENDIAN (AFMT_S32_BE | AFMT_U32_BE | AFMT_F32_BE | \ 311 AFMT_S24_BE | AFMT_U24_BE | AFMT_S16_BE | AFMT_U16_BE) 312 313 #define AFMT_CONVERTIBLE (AFMT_8BIT | AFMT_16BIT | AFMT_24BIT | \ 314 AFMT_32BIT) 315 316 /* Supported vchan mixing formats */ 317 #define AFMT_VCHAN (AFMT_CONVERTIBLE & ~AFMT_G711) 318 319 #define AFMT_PASSTHROUGH AFMT_AC3 320 #define AFMT_PASSTHROUGH_RATE 48000 321 #define AFMT_PASSTHROUGH_CHANNEL 2 322 #define AFMT_PASSTHROUGH_EXTCHANNEL 0 323 324 /* 325 * We're simply using unused, contiguous bits from various AFMT_ definitions. 326 * ~(0xb00ff7ff) 327 */ 328 #define AFMT_ENCODING_MASK 0xf00fffff 329 #define AFMT_CHANNEL_MASK 0x07f00000 330 #define AFMT_CHANNEL_SHIFT 20 331 #define AFMT_CHANNEL_MAX 0x7f 332 #define AFMT_EXTCHANNEL_MASK 0x08000000 333 #define AFMT_EXTCHANNEL_SHIFT 27 334 #define AFMT_EXTCHANNEL_MAX 1 335 336 #define AFMT_ENCODING(v) ((v) & AFMT_ENCODING_MASK) 337 338 #define AFMT_EXTCHANNEL(v) (((v) & AFMT_EXTCHANNEL_MASK) >> \ 339 AFMT_EXTCHANNEL_SHIFT) 340 341 #define AFMT_CHANNEL(v) (((v) & AFMT_CHANNEL_MASK) >> \ 342 AFMT_CHANNEL_SHIFT) 343 344 #define AFMT_BIT(v) (((v) & AFMT_32BIT) ? 32 : \ 345 (((v) & AFMT_24BIT) ? 24 : \ 346 ((((v) & AFMT_16BIT) || \ 347 ((v) & AFMT_PASSTHROUGH)) ? 16 : 8))) 348 349 #define AFMT_BPS(v) (AFMT_BIT(v) >> 3) 350 #define AFMT_ALIGN(v) (AFMT_BPS(v) * AFMT_CHANNEL(v)) 351 352 #define SND_FORMAT(f, c, e) (AFMT_ENCODING(f) | \ 353 (((c) << AFMT_CHANNEL_SHIFT) & \ 354 AFMT_CHANNEL_MASK) | \ 355 (((e) << AFMT_EXTCHANNEL_SHIFT) & \ 356 AFMT_EXTCHANNEL_MASK)) 357 358 #define AFMT_U8_NE AFMT_U8 359 #define AFMT_S8_NE AFMT_S8 360 361 #define AFMT_SIGNED_NE (AFMT_S8_NE | AFMT_S16_NE | AFMT_S24_NE | \ 362 AFMT_S32_NE | AFMT_F32_NE) 363 364 #define AFMT_NE (AFMT_SIGNED_NE | AFMT_U8_NE | AFMT_U16_NE | \ 365 AFMT_U24_NE | AFMT_U32_NE) 366 367 #endif /* _OS_H_ */ 368