1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org> 5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006 6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org> 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 struct pcmchan_caps { 36 uint32_t minspeed, maxspeed; 37 uint32_t *fmtlist; 38 uint32_t caps; 39 }; 40 41 struct pcmchan_matrix { 42 int id; 43 uint8_t channels, ext; 44 struct { 45 int type; 46 uint32_t members; 47 } map[SND_CHN_T_MAX + 1]; 48 uint32_t mask; 49 int8_t offset[SND_CHN_T_MAX]; 50 }; 51 52 /* Forward declarations */ 53 struct pcm_channel; 54 struct pcmchan_syncgroup; 55 struct pcmchan_syncmember; 56 57 extern struct mtx snd_pcm_syncgroups_mtx; 58 extern SLIST_HEAD(pcm_synclist, pcmchan_syncgroup) snd_pcm_syncgroups; 59 60 #define PCM_SG_LOCKPTR() (&snd_pcm_syncgroups_mtx) 61 #define PCM_SG_LOCK() mtx_lock(PCM_SG_LOCKPTR()) 62 #define PCM_SG_TRYLOCK() mtx_trylock(PCM_SG_LOCKPTR()) 63 #define PCM_SG_UNLOCK() mtx_unlock(PCM_SG_LOCKPTR()) 64 #define PCM_SG_LOCKASSERT(arg) mtx_assert(PCM_SG_LOCKPTR(), arg) 65 66 /** 67 * @brief Specifies an audio device sync group 68 */ 69 struct pcmchan_syncgroup { 70 SLIST_ENTRY(pcmchan_syncgroup) link; 71 SLIST_HEAD(, pcmchan_syncmember) members; 72 int id; /**< Group identifier; set to address of group. */ 73 }; 74 75 /** 76 * @brief Specifies a container for members of a sync group 77 */ 78 struct pcmchan_syncmember { 79 SLIST_ENTRY(pcmchan_syncmember) link; 80 struct pcmchan_syncgroup *parent; /**< group head */ 81 struct pcm_channel *ch; 82 }; 83 84 #define CHN_NAMELEN 32 85 #define CHN_COMM_UNUSED "<UNUSED>" 86 #define CHN_COMM_UNKNOWN "<UNKNOWN>" 87 88 struct pcm_channel { 89 kobj_t methods; 90 91 pid_t pid; 92 struct pcm_feeder *feeder; 93 uint32_t align; 94 95 int latency; 96 uint32_t speed; 97 uint32_t format; 98 uint32_t flags; 99 uint32_t feederflags; 100 uint64_t blocks; 101 102 int direction; 103 unsigned int interrupts, xruns, feedcount; 104 unsigned int timeout; 105 struct snd_dbuf *bufhard, *bufsoft; 106 struct snddev_info *parentsnddev; 107 struct pcm_channel *parentchannel; 108 void *devinfo; 109 device_t dev; 110 int unit; 111 int type; 112 char name[CHN_NAMELEN]; 113 char comm[MAXCOMLEN + 1]; 114 struct mtx lock; 115 int trigger; 116 /** 117 * For interrupt manipulations. 118 */ 119 struct cv intr_cv; 120 /** 121 * Increment,decrement this around operations that temporarily yield 122 * lock. 123 */ 124 unsigned int inprog; 125 /* Incrememnt/decrement around cv_timedwait_sig() in chn_sleep(). */ 126 unsigned int sleeping; 127 /** 128 * Special channel operations should examine @c inprog after acquiring 129 * lock. If zero, operations may continue. Else, thread should 130 * wait on this cv for previous operation to finish. 131 */ 132 struct cv cv; 133 /** 134 * Low water mark for select()/poll(). 135 * 136 * This is initialized to the channel's fragment size, and will be 137 * overwritten if a new fragment size is set. Users may alter this 138 * value directly with the @c SNDCTL_DSP_LOW_WATER ioctl. 139 */ 140 unsigned int lw; 141 /** 142 * If part of a sync group, this will point to the syncmember 143 * container. 144 */ 145 struct pcmchan_syncmember *sm; 146 #ifdef OSSV4_EXPERIMENT 147 uint16_t lpeak, rpeak; /**< Peak value from 0-32767. */ 148 #endif 149 150 struct { 151 SLIST_HEAD(, pcm_channel) head; 152 SLIST_ENTRY(pcm_channel) link; 153 struct { 154 SLIST_HEAD(, pcm_channel) head; 155 SLIST_ENTRY(pcm_channel) link; 156 } busy; 157 } children; 158 159 struct { 160 struct { 161 SLIST_ENTRY(pcm_channel) link; 162 struct { 163 SLIST_ENTRY(pcm_channel) link; 164 } busy; 165 struct { 166 SLIST_ENTRY(pcm_channel) link; 167 } opened; 168 struct { 169 SLIST_ENTRY(pcm_channel) link; 170 } primary; 171 } pcm; 172 } channels; 173 174 struct pcmchan_matrix matrix; 175 struct pcmchan_matrix matrix_scratch; 176 177 int16_t volume[SND_VOL_C_MAX][SND_CHN_T_VOL_MAX]; 178 int8_t muted[SND_VOL_C_MAX][SND_CHN_T_VOL_MAX]; 179 }; 180 181 #define CHN_HEAD(x, y) &(x)->y.head 182 #define CHN_INIT(x, y) SLIST_INIT(CHN_HEAD(x, y)) 183 #define CHN_LINK(y) y.link 184 #define CHN_EMPTY(x, y) SLIST_EMPTY(CHN_HEAD(x, y)) 185 #define CHN_FIRST(x, y) SLIST_FIRST(CHN_HEAD(x, y)) 186 #define CHN_NEXT(elm, list) SLIST_NEXT((elm), CHN_LINK(list)) 187 188 #define CHN_FOREACH(x, y, z) \ 189 SLIST_FOREACH(x, CHN_HEAD(y, z), CHN_LINK(z)) 190 191 #define CHN_FOREACH_SAFE(w, x, y, z) \ 192 SLIST_FOREACH_SAFE(w, CHN_HEAD(x, z), CHN_LINK(z), y) 193 194 #define CHN_INSERT_HEAD(x, y, z) \ 195 SLIST_INSERT_HEAD(CHN_HEAD(x, z), y, CHN_LINK(z)) 196 197 #define CHN_INSERT_AFTER(x, y, z) \ 198 SLIST_INSERT_AFTER(x, y, CHN_LINK(z)) 199 200 #define CHN_INSERT_HEAD_SAFE(x, y, z) do { \ 201 struct pcm_channel *t = NULL; \ 202 CHN_FOREACH(t, x, z) { \ 203 if (t == y) \ 204 break; \ 205 } \ 206 if (t != y) \ 207 CHN_INSERT_HEAD(x, y, z); \ 208 } while (0) 209 210 #define CHN_INSERT_AFTER_SAFE(w, x, y, z) do { \ 211 struct pcm_channel *t = NULL; \ 212 CHN_FOREACH(t, w, z) { \ 213 if (t == y) \ 214 break; \ 215 } \ 216 if (t != y) \ 217 CHN_INSERT_AFTER(x, y, z); \ 218 } while (0) 219 220 #define CHN_REMOVE(holder, elm, list) do { \ 221 if (CHN_FIRST(holder, list) == (elm)) { \ 222 SLIST_REMOVE_HEAD(CHN_HEAD(holder, list), CHN_LINK(list)); \ 223 } else { \ 224 struct pcm_channel *t = NULL; \ 225 CHN_FOREACH(t, holder, list) { \ 226 if (CHN_NEXT(t, list) == (elm)) { \ 227 SLIST_REMOVE_AFTER(t, CHN_LINK(list)); \ 228 break; \ 229 } \ 230 } \ 231 } \ 232 } while (0) 233 234 #define CHN_INSERT_SORT(w, x, y, z) do { \ 235 struct pcm_channel *t, *a = NULL; \ 236 CHN_FOREACH(t, x, z) { \ 237 if (((y)->type w t->type) || \ 238 (((y)->type == t->type) && ((y)->unit w t->unit))) \ 239 a = t; \ 240 else \ 241 break; \ 242 } \ 243 if (a != NULL) \ 244 CHN_INSERT_AFTER(a, y, z); \ 245 else \ 246 CHN_INSERT_HEAD(x, y, z); \ 247 } while (0) 248 249 #define CHN_INSERT_SORT_ASCEND(x, y, z) CHN_INSERT_SORT(>, x, y, z) 250 #define CHN_INSERT_SORT_DESCEND(x, y, z) CHN_INSERT_SORT(<, x, y, z) 251 252 #define CHN_BUF_PARENT(x, y) \ 253 (((x) != NULL && (x)->parentchannel != NULL && \ 254 (x)->parentchannel->bufhard != NULL) ? \ 255 (x)->parentchannel->bufhard : (y)) 256 257 #include "channel_if.h" 258 259 int chn_write(struct pcm_channel *c, struct uio *buf); 260 int chn_read(struct pcm_channel *c, struct uio *buf); 261 uint32_t chn_start(struct pcm_channel *c, int force); 262 int chn_sync(struct pcm_channel *c, int threshold); 263 int chn_flush(struct pcm_channel *c); 264 int chn_polltrigger(struct pcm_channel *c, uint64_t ref_total); 265 int chn_poll(struct pcm_channel *c, int ev, struct thread *td); 266 267 char *chn_mkname(char *buf, size_t len, struct pcm_channel *c); 268 struct pcm_channel *chn_init(struct snddev_info *d, struct pcm_channel *parent, 269 kobj_class_t cls, int dir, void *devinfo); 270 void chn_kill(struct pcm_channel *c); 271 void chn_shutdown(struct pcm_channel *c); 272 int chn_release(struct pcm_channel *c); 273 int chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd); 274 int chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right, 275 int center); 276 int chn_setvolume_matrix(struct pcm_channel *c, int vc, int vt, int val); 277 int chn_getvolume_matrix(struct pcm_channel *c, int vc, int vt); 278 int chn_setmute_multi(struct pcm_channel *c, int vc, int mute); 279 int chn_setmute_matrix(struct pcm_channel *c, int vc, int vt, int mute); 280 int chn_getmute_matrix(struct pcm_channel *c, int vc, int vt); 281 void chn_vpc_reset(struct pcm_channel *c, int vc, int force); 282 int chn_setparam(struct pcm_channel *c, uint32_t format, uint32_t speed); 283 int chn_setspeed(struct pcm_channel *c, uint32_t speed); 284 int chn_setformat(struct pcm_channel *c, uint32_t format); 285 int chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz); 286 int chn_setlatency(struct pcm_channel *c, int latency); 287 void chn_syncstate(struct pcm_channel *c); 288 int chn_trigger(struct pcm_channel *c, int go); 289 int chn_getptr(struct pcm_channel *c); 290 struct pcmchan_caps *chn_getcaps(struct pcm_channel *c); 291 uint32_t chn_getformats(struct pcm_channel *c); 292 293 struct pcmchan_matrix *chn_getmatrix(struct pcm_channel *); 294 int chn_setmatrix(struct pcm_channel *, struct pcmchan_matrix *); 295 296 int chn_oss_getorder(struct pcm_channel *, unsigned long long *); 297 int chn_oss_setorder(struct pcm_channel *, unsigned long long *); 298 int chn_oss_getmask(struct pcm_channel *, uint32_t *); 299 300 void chn_resetbuf(struct pcm_channel *c); 301 void chn_intr(struct pcm_channel *c); 302 int chn_abort(struct pcm_channel *c); 303 304 int chn_notify(struct pcm_channel *c, uint32_t flags); 305 306 int chn_getrates(struct pcm_channel *c, int **rates); 307 int chn_syncdestroy(struct pcm_channel *c); 308 309 #ifdef OSSV4_EXPERIMENT 310 int chn_getpeaks(struct pcm_channel *c, int *lpeak, int *rpeak); 311 #endif 312 313 #define CHN_LOCKOWNED(c) mtx_owned(&(c)->lock) 314 #define CHN_LOCK(c) mtx_lock(&(c)->lock) 315 #define CHN_UNLOCK(c) mtx_unlock(&(c)->lock) 316 #define CHN_TRYLOCK(c) mtx_trylock(&(c)->lock) 317 #define CHN_LOCKASSERT(c) mtx_assert(&(c)->lock, MA_OWNED) 318 #define CHN_UNLOCKASSERT(c) mtx_assert(&(c)->lock, MA_NOTOWNED) 319 320 #define CHN_BROADCAST(x) cv_broadcastpri(x, PRIBIO) 321 322 int snd_fmtvalid(uint32_t fmt, uint32_t *fmtlist); 323 324 uint32_t snd_str2afmt(const char *); 325 uint32_t snd_afmt2str(uint32_t, char *, size_t); 326 327 #define AFMTSTR_LEN 16 328 329 extern int chn_latency; 330 extern int chn_latency_profile; 331 extern int report_soft_formats; 332 extern int report_soft_matrix; 333 334 enum { 335 PCMDIR_PLAY = 1, 336 PCMDIR_PLAY_VIRTUAL, 337 PCMDIR_REC, 338 PCMDIR_REC_VIRTUAL, 339 }; 340 341 #define PCMTRIG_START 1 342 #define PCMTRIG_EMLDMAWR 2 343 #define PCMTRIG_EMLDMARD 3 344 #define PCMTRIG_STOP 0 345 #define PCMTRIG_ABORT -1 346 347 #define PCMTRIG_COMMON(x) ((x) == PCMTRIG_START || \ 348 (x) == PCMTRIG_STOP || \ 349 (x) == PCMTRIG_ABORT) 350 351 #define CHN_F_CLOSING 0x00000001 /* a pending close */ 352 #define CHN_F_ABORTING 0x00000002 /* a pending abort */ 353 #define CHN_F_RUNNING 0x00000004 /* dma is running */ 354 #define CHN_F_TRIGGERED 0x00000008 355 #define CHN_F_NOTRIGGER 0x00000010 356 /* unused 0x00000020 */ 357 358 #define CHN_F_NBIO 0x00000040 /* do non-blocking i/o */ 359 #define CHN_F_MMAP 0x00000080 /* has been mmap()ed */ 360 361 #define CHN_F_BUSY 0x00000100 /* has been opened */ 362 #define CHN_F_DIRTY 0x00000200 /* need re-config */ 363 #define CHN_F_DEAD 0x00000400 /* too many errors, dead, mdk */ 364 /* unused 0x00000800 */ 365 366 #define CHN_F_HAS_SIZE 0x00001000 /* user set block size */ 367 #define CHN_F_HAS_VCHAN 0x00002000 /* vchan master */ 368 369 #define CHN_F_VCHAN_PASSTHROUGH 0x00004000 /* digital ac3/dts passthrough */ 370 #define CHN_F_VCHAN_ADAPTIVE 0x00008000 /* adaptive format/rate selection */ 371 #define CHN_F_VCHAN_DYNAMIC (CHN_F_VCHAN_PASSTHROUGH | CHN_F_VCHAN_ADAPTIVE) 372 373 #define CHN_F_VIRTUAL 0x10000000 /* not backed by hardware */ 374 #define CHN_F_BITPERFECT 0x20000000 /* un-cooked, Heh.. */ 375 #define CHN_F_PASSTHROUGH 0x40000000 /* passthrough re-config */ 376 #define CHN_F_EXCLUSIVE 0x80000000 /* exclusive access */ 377 378 #define CHN_F_BITS "\020" \ 379 "\001CLOSING" \ 380 "\002ABORTING" \ 381 "\003RUNNING" \ 382 "\004TRIGGERED" \ 383 "\005NOTRIGGER" \ 384 /* \006 */ \ 385 "\007NBIO" \ 386 "\010MMAP" \ 387 "\011BUSY" \ 388 "\012DIRTY" \ 389 "\013DEAD" \ 390 /* \014 */ \ 391 "\015HAS_SIZE" \ 392 "\016HAS_VCHAN" \ 393 "\017VCHAN_PASSTHROUGH" \ 394 "\020VCHAN_ADAPTIVE" \ 395 "\035VIRTUAL" \ 396 "\036BITPERFECT" \ 397 "\037PASSTHROUGH" \ 398 "\040EXCLUSIVE" 399 400 #define CHN_F_RESET (CHN_F_BUSY | CHN_F_DEAD | \ 401 CHN_F_VIRTUAL | CHN_F_HAS_VCHAN | \ 402 CHN_F_VCHAN_DYNAMIC | CHN_F_NBIO | \ 403 CHN_F_PASSTHROUGH | CHN_F_EXCLUSIVE) 404 405 #define CHN_F_MMAP_INVALID (CHN_F_DEAD | CHN_F_RUNNING) 406 407 408 409 #define CHN_N_BLOCKSIZE 0x00000001 410 #define CHN_N_TRIGGER 0x00000002 411 412 #define CHN_LATENCY_MIN 0 413 #define CHN_LATENCY_MAX 10 414 #define CHN_LATENCY_DEFAULT 2 /* 21.3ms total buffering */ 415 #define CHN_POLICY_MIN CHN_LATENCY_MIN 416 #define CHN_POLICY_MAX CHN_LATENCY_MAX 417 #define CHN_POLICY_DEFAULT CHN_LATENCY_DEFAULT 418 419 #define CHN_LATENCY_PROFILE_MIN 0 420 #define CHN_LATENCY_PROFILE_MAX 1 421 #define CHN_LATENCY_PROFILE_DEFAULT CHN_LATENCY_PROFILE_MAX 422 423 #define CHN_STARTED(c) ((c)->flags & CHN_F_TRIGGERED) 424 #define CHN_STOPPED(c) (!CHN_STARTED(c)) 425 #define CHN_DIRSTR(c) (((c)->direction == PCMDIR_PLAY) ? \ 426 "PCMDIR_PLAY" : "PCMDIR_REC") 427 #define CHN_BITPERFECT(c) ((c)->flags & CHN_F_BITPERFECT) 428 #define CHN_PASSTHROUGH(c) ((c)->flags & CHN_F_PASSTHROUGH) 429 430 #define CHN_TIMEOUT 5 431 #define CHN_TIMEOUT_MIN 1 432 #define CHN_TIMEOUT_MAX 10 433 434 /* Default block size for the secondary buffer. */ 435 #define CHN_2NDBUFBLKSIZE (2 * 1024) 436 /* The total number of blocks per secondary bufhard. */ 437 #define CHN_2NDBUFBLKNUM (32) 438 /* 439 * The secondary buffer cap scales with the channel byte rate, targeting 440 * CHN_2NDBUFTIME_MS of stream so that all pcm data between tsleeps in 441 * chn_{read,write} fits, clamped to [CHN_2NDBUFSIZE_MIN, 442 * CHN_2NDBUFSIZE_MAX]. 443 * 444 * The floor is the historical secondary buffer size and preserves memory 445 * use for low byte-rate streams; it holds ~680 ms at the once-typical 446 * 48kHz * 16bit * stereo rate (192000 bytes/sec), well above the target 447 * (~38 KiB there). 448 * 449 * The ceiling bounds per-channel buffer memory. Buffers are allocated at 450 * the derived size, so only streams that actually run at high byte rates 451 * approach it. It holds the full target through MADI-class streams 452 * (64ch * 32-bit * 48kHz, ~12.3 MB/s); beyond that, coverage shrinks 453 * proportionally (e.g. ~85 ms at 64ch * 32-bit * 192kHz). 454 */ 455 #define CHN_2NDBUFSIZE_MIN (131072) 456 #define CHN_2NDBUFSIZE_MAX (4 * 1024 * 1024) 457 #define CHN_2NDBUFTIME_MS 200 458 459 uint32_t chn_2ndbufmaxsize(struct pcm_channel *); 460 461 #define CHANNEL_DECLARE(name) static DEFINE_CLASS(name, name ## _methods, sizeof(struct kobj)) 462