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