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 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifdef HAVE_KERNEL_OPTION_HEADERS 31 #include "opt_snd.h" 32 #endif 33 34 #include <dev/sound/pcm/sound.h> 35 36 #include "feeder_if.h" 37 38 static MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder"); 39 40 #define MAXFEEDERS 256 41 #undef FEEDER_DEBUG 42 43 struct feedertab_entry { 44 SLIST_ENTRY(feedertab_entry) link; 45 struct feeder_class *feederclass; 46 struct pcm_feederdesc *desc; 47 48 int idx; 49 }; 50 static SLIST_HEAD(, feedertab_entry) feedertab; 51 52 /*****************************************************************************/ 53 54 void 55 feeder_register(void *p) 56 { 57 static int feedercnt = 0; 58 59 struct feeder_class *fc = p; 60 struct feedertab_entry *fte; 61 int i; 62 63 if (feedercnt == 0) { 64 KASSERT(fc->desc == NULL, ("first feeder not root: %s", fc->name)); 65 66 SLIST_INIT(&feedertab); 67 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 68 if (fte == NULL) { 69 printf("can't allocate memory for root feeder: %s\n", 70 fc->name); 71 72 return; 73 } 74 fte->feederclass = fc; 75 fte->desc = NULL; 76 fte->idx = feedercnt; 77 SLIST_INSERT_HEAD(&feedertab, fte, link); 78 feedercnt++; 79 80 /* initialize global variables */ 81 82 if (snd_verbose < 0 || snd_verbose > 4) 83 snd_verbose = 1; 84 85 if (snd_unit < 0) 86 snd_unit = -1; 87 88 if (snd_maxautovchans < 0 || 89 snd_maxautovchans > SND_MAXVCHANS) 90 snd_maxautovchans = 0; 91 92 if (chn_latency < CHN_LATENCY_MIN || 93 chn_latency > CHN_LATENCY_MAX) 94 chn_latency = CHN_LATENCY_DEFAULT; 95 96 if (chn_latency_profile < CHN_LATENCY_PROFILE_MIN || 97 chn_latency_profile > CHN_LATENCY_PROFILE_MAX) 98 chn_latency_profile = CHN_LATENCY_PROFILE_DEFAULT; 99 100 if (feeder_rate_min < FEEDRATE_MIN || 101 feeder_rate_max < FEEDRATE_MIN || 102 feeder_rate_min > FEEDRATE_MAX || 103 feeder_rate_max > FEEDRATE_MAX || 104 !(feeder_rate_min < feeder_rate_max)) { 105 feeder_rate_min = FEEDRATE_RATEMIN; 106 feeder_rate_max = FEEDRATE_RATEMAX; 107 } 108 109 if (feeder_rate_round < FEEDRATE_ROUNDHZ_MIN || 110 feeder_rate_round > FEEDRATE_ROUNDHZ_MAX) 111 feeder_rate_round = FEEDRATE_ROUNDHZ; 112 113 if (bootverbose) 114 printf("%s: snd_unit=%d snd_maxautovchans=%d " 115 "latency=%d " 116 "feeder_rate_min=%d feeder_rate_max=%d " 117 "feeder_rate_round=%d\n", 118 __func__, snd_unit, snd_maxautovchans, 119 chn_latency, 120 feeder_rate_min, feeder_rate_max, 121 feeder_rate_round); 122 123 /* we've got our root feeder so don't veto pcm loading anymore */ 124 pcm_veto_load = 0; 125 126 return; 127 } 128 129 KASSERT(fc->desc != NULL, ("feeder '%s' has no descriptor", fc->name)); 130 131 /* beyond this point failure is non-fatal but may result in some translations being unavailable */ 132 i = 0; 133 while ((feedercnt < MAXFEEDERS) && (fc->desc[i].type > 0)) { 134 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */ 135 fte = malloc(sizeof(*fte), M_FEEDER, M_NOWAIT | M_ZERO); 136 if (fte == NULL) { 137 printf("can't allocate memory for feeder '%s', %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); 138 139 return; 140 } 141 fte->feederclass = fc; 142 fte->desc = &fc->desc[i]; 143 fte->idx = feedercnt; 144 fte->desc->idx = feedercnt; 145 SLIST_INSERT_HEAD(&feedertab, fte, link); 146 i++; 147 } 148 feedercnt++; 149 if (feedercnt >= MAXFEEDERS) 150 printf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt, MAXFEEDERS); 151 } 152 153 static void 154 feeder_unregisterall(void *p) 155 { 156 struct feedertab_entry *fte, *next; 157 158 next = SLIST_FIRST(&feedertab); 159 while (next != NULL) { 160 fte = next; 161 next = SLIST_NEXT(fte, link); 162 free(fte, M_FEEDER); 163 } 164 } 165 166 static int 167 cmpdesc(struct pcm_feederdesc *n, struct pcm_feederdesc *m) 168 { 169 return ((n->type == m->type) && 170 ((n->in == 0) || (n->in == m->in)) && 171 ((n->out == 0) || (n->out == m->out)) && 172 (n->flags == m->flags)); 173 } 174 175 static void 176 feeder_destroy(struct pcm_feeder *f) 177 { 178 FEEDER_FREE(f); 179 kobj_delete((kobj_t)f, M_FEEDER); 180 } 181 182 static struct pcm_feeder * 183 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc) 184 { 185 struct pcm_feeder *f; 186 int err; 187 188 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO); 189 if (f == NULL) 190 return NULL; 191 192 f->data = fc->data; 193 f->source = NULL; 194 f->parent = NULL; 195 f->class = fc; 196 f->desc = &(f->desc_static); 197 198 if (desc) { 199 *(f->desc) = *desc; 200 } else { 201 f->desc->type = FEEDER_ROOT; 202 f->desc->in = 0; 203 f->desc->out = 0; 204 f->desc->flags = 0; 205 f->desc->idx = 0; 206 } 207 208 err = FEEDER_INIT(f); 209 if (err) { 210 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err); 211 feeder_destroy(f); 212 213 return NULL; 214 } 215 216 return f; 217 } 218 219 struct feeder_class * 220 feeder_getclass(struct pcm_feederdesc *desc) 221 { 222 struct feedertab_entry *fte; 223 224 SLIST_FOREACH(fte, &feedertab, link) { 225 if ((desc == NULL) && (fte->desc == NULL)) 226 return fte->feederclass; 227 if ((fte->desc != NULL) && (desc != NULL) && cmpdesc(desc, fte->desc)) 228 return fte->feederclass; 229 } 230 return NULL; 231 } 232 233 int 234 chn_addfeeder(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc) 235 { 236 struct pcm_feeder *nf; 237 238 nf = feeder_create(fc, desc); 239 if (nf == NULL) 240 return ENOSPC; 241 242 nf->source = c->feeder; 243 244 if (c->feeder != NULL) 245 c->feeder->parent = nf; 246 c->feeder = nf; 247 248 return 0; 249 } 250 251 int 252 chn_removefeeder(struct pcm_channel *c) 253 { 254 struct pcm_feeder *f; 255 256 if (c->feeder == NULL) 257 return -1; 258 f = c->feeder; 259 c->feeder = c->feeder->source; 260 feeder_destroy(f); 261 262 return 0; 263 } 264 265 struct pcm_feeder * 266 chn_findfeeder(struct pcm_channel *c, u_int32_t type) 267 { 268 struct pcm_feeder *f; 269 270 f = c->feeder; 271 while (f != NULL) { 272 if (f->desc->type == type) 273 return f; 274 f = f->source; 275 } 276 277 return NULL; 278 } 279 280 /* 281 * 14bit format scoring 282 * -------------------- 283 * 284 * 13 12 11 10 9 8 2 1 0 offset 285 * +---+---+---+---+---+---+-------------+---+---+ 286 * | X | X | X | X | X | X | X X X X X X | X | X | 287 * +---+---+---+---+---+---+-------------+---+---+ 288 * | | | | | | | | | 289 * | | | | | | | | +--> signed? 290 * | | | | | | | | 291 * | | | | | | | +------> bigendian? 292 * | | | | | | | 293 * | | | | | | +---------------> total channels 294 * | | | | | | 295 * | | | | | +------------------------> AFMT_A_LAW 296 * | | | | | 297 * | | | | +----------------------------> AFMT_MU_LAW 298 * | | | | 299 * | | | +--------------------------------> AFMT_8BIT 300 * | | | 301 * | | +------------------------------------> AFMT_16BIT 302 * | | 303 * | +----------------------------------------> AFMT_24BIT 304 * | 305 * +--------------------------------------------> AFMT_32BIT 306 */ 307 #define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1)) 308 #define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2)) 309 #define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc)) 310 #define score_chgt(s1, s2) (((s1) & 0xfc) > ((s2) & 0xfc)) 311 #define score_chlt(s1, s2) (((s1) & 0xfc) < ((s2) & 0xfc)) 312 #define score_val(s1) ((s1) & 0x3f00) 313 #define score_cse(s1) ((s1) & 0x7f) 314 315 u_int32_t 316 snd_fmtscore(u_int32_t fmt) 317 { 318 u_int32_t ret; 319 320 ret = 0; 321 if (fmt & AFMT_SIGNED) 322 ret |= 1 << 0; 323 if (fmt & AFMT_BIGENDIAN) 324 ret |= 1 << 1; 325 /*if (fmt & AFMT_STEREO) 326 ret |= (2 & 0x3f) << 2; 327 else 328 ret |= (1 & 0x3f) << 2;*/ 329 ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2; 330 if (fmt & AFMT_A_LAW) 331 ret |= 1 << 8; 332 else if (fmt & AFMT_MU_LAW) 333 ret |= 1 << 9; 334 else if (fmt & AFMT_8BIT) 335 ret |= 1 << 10; 336 else if (fmt & AFMT_16BIT) 337 ret |= 1 << 11; 338 else if (fmt & AFMT_24BIT) 339 ret |= 1 << 12; 340 else if (fmt & AFMT_32BIT) 341 ret |= 1 << 13; 342 343 return ret; 344 } 345 346 static u_int32_t 347 snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq) 348 { 349 u_int32_t best, score, score2, oldscore; 350 int i; 351 352 if (fmt == 0 || fmts == NULL || fmts[0] == 0) 353 return 0; 354 355 if (snd_fmtvalid(fmt, fmts)) 356 return fmt; 357 358 best = 0; 359 score = snd_fmtscore(fmt); 360 oldscore = 0; 361 for (i = 0; fmts[i] != 0; i++) { 362 score2 = snd_fmtscore(fmts[i]); 363 if (cheq && !score_cheq(score, score2) && 364 (score_chlt(score2, score) || 365 (oldscore != 0 && score_chgt(score2, oldscore)))) 366 continue; 367 if (oldscore == 0 || 368 (score_val(score2) == score_val(score)) || 369 (score_val(score2) == score_val(oldscore)) || 370 (score_val(score2) > score_val(oldscore) && 371 score_val(score2) < score_val(score)) || 372 (score_val(score2) < score_val(oldscore) && 373 score_val(score2) > score_val(score)) || 374 (score_val(oldscore) < score_val(score) && 375 score_val(score2) > score_val(oldscore))) { 376 if (score_val(oldscore) != score_val(score2) || 377 score_cse(score) == score_cse(score2) || 378 ((score_cse(oldscore) != score_cse(score) && 379 !score_endianeq(score, oldscore) && 380 (score_endianeq(score, score2) || 381 (!score_signeq(score, oldscore) && 382 score_signeq(score, score2)))))) { 383 best = fmts[i]; 384 oldscore = score2; 385 } 386 } 387 } 388 return best; 389 } 390 391 u_int32_t 392 snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts) 393 { 394 return snd_fmtbestfunc(fmt, fmts, 0); 395 } 396 397 u_int32_t 398 snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts) 399 { 400 return snd_fmtbestfunc(fmt, fmts, 1); 401 } 402 403 u_int32_t 404 snd_fmtbest(u_int32_t fmt, u_int32_t *fmts) 405 { 406 u_int32_t best1, best2; 407 u_int32_t score, score1, score2; 408 409 if (snd_fmtvalid(fmt, fmts)) 410 return fmt; 411 412 best1 = snd_fmtbestchannel(fmt, fmts); 413 best2 = snd_fmtbestbit(fmt, fmts); 414 415 if (best1 != 0 && best2 != 0 && best1 != best2) { 416 /*if (fmt & AFMT_STEREO)*/ 417 if (AFMT_CHANNEL(fmt) > 1) 418 return best1; 419 else { 420 score = score_val(snd_fmtscore(fmt)); 421 score1 = score_val(snd_fmtscore(best1)); 422 score2 = score_val(snd_fmtscore(best2)); 423 if (score1 == score2 || score1 == score) 424 return best1; 425 else if (score2 == score) 426 return best2; 427 else if (score1 > score2) 428 return best1; 429 return best2; 430 } 431 } else if (best2 == 0) 432 return best1; 433 else 434 return best2; 435 } 436 437 void 438 feeder_printchain(struct pcm_feeder *head) 439 { 440 struct pcm_feeder *f; 441 442 printf("feeder chain (head @%p)\n", head); 443 f = head; 444 while (f != NULL) { 445 printf("%s/%d @ %p\n", f->class->name, f->desc->idx, f); 446 f = f->source; 447 } 448 printf("[end]\n\n"); 449 } 450 451 /*****************************************************************************/ 452 453 static int 454 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source) 455 { 456 struct snd_dbuf *src = source; 457 int l, offset; 458 459 KASSERT(count > 0, ("feed_root: count == 0")); 460 461 if (++ch->feedcount == 0) 462 ch->feedcount = 2; 463 464 l = min(count, sndbuf_getready(src)); 465 466 /* When recording only return as much data as available */ 467 if (ch->direction == PCMDIR_REC) { 468 sndbuf_dispose(src, buffer, l); 469 return l; 470 } 471 472 offset = count - l; 473 474 if (offset > 0) { 475 if (snd_verbose > 3) 476 printf("%s: (%s) %spending %d bytes " 477 "(count=%d l=%d feed=%d)\n", 478 __func__, 479 (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware", 480 (ch->feedcount == 1) ? "pre" : "ap", 481 offset, count, l, ch->feedcount); 482 483 if (ch->feedcount == 1) { 484 memset(buffer, 485 sndbuf_zerodata(sndbuf_getfmt(src)), 486 offset); 487 if (l > 0) 488 sndbuf_dispose(src, buffer + offset, l); 489 else 490 ch->feedcount--; 491 } else { 492 if (l > 0) 493 sndbuf_dispose(src, buffer, l); 494 memset(buffer + l, 495 sndbuf_zerodata(sndbuf_getfmt(src)), 496 offset); 497 if (!(ch->flags & CHN_F_CLOSING)) 498 ch->xruns++; 499 } 500 } else if (l > 0) 501 sndbuf_dispose(src, buffer, l); 502 503 return count; 504 } 505 506 static kobj_method_t feeder_root_methods[] = { 507 KOBJMETHOD(feeder_feed, feed_root), 508 KOBJMETHOD_END 509 }; 510 static struct feeder_class feeder_root_class = { 511 .name = "feeder_root", 512 .methods = feeder_root_methods, 513 .size = sizeof(struct pcm_feeder), 514 .desc = NULL, 515 .data = NULL, 516 }; 517 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register, &feeder_root_class); 518 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL); 519