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 * Copyright (c) 2024-2025 The FreeBSD Foundation
8 *
9 * Portions of this software were developed by Christos Margiolis
10 * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include "opt_snd.h"
36 #endif
37
38 #include <dev/sound/pcm/sound.h>
39
40 #include "feeder_if.h"
41
42 static MALLOC_DEFINE(M_FEEDER, "feeder", "pcm feeder");
43
44 static SLIST_HEAD(, feeder_class) feedertab = SLIST_HEAD_INITIALIZER(feedertab);
45
46 void
feeder_register(void * p)47 feeder_register(void *p)
48 {
49 struct feeder_class *fc = p;
50
51 SLIST_INSERT_HEAD(&feedertab, fc, link);
52 }
53
54 static void
feeder_unregisterall(void * p __unused)55 feeder_unregisterall(void *p __unused)
56 {
57 SLIST_INIT(&feedertab);
58 }
59
60 static void
feeder_destroy(struct pcm_feeder * f)61 feeder_destroy(struct pcm_feeder *f)
62 {
63 FEEDER_FREE(f);
64 kobj_delete((kobj_t)f, M_FEEDER);
65 }
66
67 static struct pcm_feeder *
feeder_create(struct feeder_class * fc,struct pcm_feederdesc * desc)68 feeder_create(struct feeder_class *fc, struct pcm_feederdesc *desc)
69 {
70 struct pcm_feeder *f;
71 int err;
72
73 f = (struct pcm_feeder *)kobj_create((kobj_class_t)fc, M_FEEDER, M_NOWAIT | M_ZERO);
74 if (f == NULL)
75 return NULL;
76
77 f->class = fc;
78 f->desc = &(f->desc_static);
79 if (desc != NULL)
80 *(f->desc) = *desc;
81
82 err = FEEDER_INIT(f);
83 if (err) {
84 printf("feeder_init(%p) on %s returned %d\n", f, fc->name, err);
85 feeder_destroy(f);
86
87 return NULL;
88 }
89
90 return f;
91 }
92
93 struct feeder_class *
feeder_getclass(u_int32_t type)94 feeder_getclass(u_int32_t type)
95 {
96 struct feeder_class *fc;
97
98 SLIST_FOREACH(fc, &feedertab, link) {
99 if (fc->type == type)
100 return (fc);
101 }
102 return (NULL);
103 }
104
105 int
feeder_add(struct pcm_channel * c,struct feeder_class * fc,struct pcm_feederdesc * desc)106 feeder_add(struct pcm_channel *c, struct feeder_class *fc, struct pcm_feederdesc *desc)
107 {
108 struct pcm_feeder *nf;
109
110 nf = feeder_create(fc, desc);
111 if (nf == NULL)
112 return ENOSPC;
113
114 nf->source = c->feeder;
115
116 if (c->feeder != NULL)
117 c->feeder->parent = nf;
118 c->feeder = nf;
119
120 return 0;
121 }
122
123 void
feeder_remove(struct pcm_channel * c)124 feeder_remove(struct pcm_channel *c)
125 {
126 struct pcm_feeder *f;
127
128 while (c->feeder != NULL) {
129 f = c->feeder;
130 c->feeder = c->feeder->source;
131 feeder_destroy(f);
132 }
133 }
134
135 struct pcm_feeder *
feeder_find(struct pcm_channel * c,u_int32_t type)136 feeder_find(struct pcm_channel *c, u_int32_t type)
137 {
138 struct pcm_feeder *f;
139
140 f = c->feeder;
141 while (f != NULL) {
142 if (f->class->type == type)
143 return f;
144 f = f->source;
145 }
146
147 return NULL;
148 }
149
150 /*
151 * 14bit format scoring
152 * --------------------
153 *
154 * 13 12 11 10 9 8 2 1 0 offset
155 * +---+---+---+---+---+---+-------------+---+---+
156 * | X | X | X | X | X | X | X X X X X X | X | X |
157 * +---+---+---+---+---+---+-------------+---+---+
158 * | | | | | | | | |
159 * | | | | | | | | +--> signed?
160 * | | | | | | | |
161 * | | | | | | | +------> bigendian?
162 * | | | | | | |
163 * | | | | | | +---------------> total channels
164 * | | | | | |
165 * | | | | | +------------------------> AFMT_A_LAW
166 * | | | | |
167 * | | | | +----------------------------> AFMT_MU_LAW
168 * | | | |
169 * | | | +--------------------------------> AFMT_8BIT
170 * | | |
171 * | | +------------------------------------> AFMT_16BIT
172 * | |
173 * | +----------------------------------------> AFMT_24BIT
174 * |
175 * +--------------------------------------------> AFMT_32BIT
176 */
177 #define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1))
178 #define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2))
179 #define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc))
180 #define score_chgt(s1, s2) (((s1) & 0xfc) > ((s2) & 0xfc))
181 #define score_chlt(s1, s2) (((s1) & 0xfc) < ((s2) & 0xfc))
182 #define score_val(s1) ((s1) & 0x3f00)
183 #define score_cse(s1) ((s1) & 0x7f)
184
185 u_int32_t
snd_fmtscore(u_int32_t fmt)186 snd_fmtscore(u_int32_t fmt)
187 {
188 u_int32_t ret;
189
190 ret = 0;
191 if (fmt & AFMT_SIGNED)
192 ret |= 1 << 0;
193 if (fmt & AFMT_BIGENDIAN)
194 ret |= 1 << 1;
195 /*if (fmt & AFMT_STEREO)
196 ret |= (2 & 0x3f) << 2;
197 else
198 ret |= (1 & 0x3f) << 2;*/
199 ret |= (AFMT_CHANNEL(fmt) & 0x3f) << 2;
200 if (fmt & AFMT_A_LAW)
201 ret |= 1 << 8;
202 else if (fmt & AFMT_MU_LAW)
203 ret |= 1 << 9;
204 else if (fmt & AFMT_8BIT)
205 ret |= 1 << 10;
206 else if (fmt & AFMT_16BIT)
207 ret |= 1 << 11;
208 else if (fmt & AFMT_24BIT)
209 ret |= 1 << 12;
210 else if (fmt & AFMT_32BIT)
211 ret |= 1 << 13;
212
213 return ret;
214 }
215
216 static u_int32_t
snd_fmtbestfunc(u_int32_t fmt,u_int32_t * fmts,int cheq)217 snd_fmtbestfunc(u_int32_t fmt, u_int32_t *fmts, int cheq)
218 {
219 u_int32_t best, score, score2, oldscore;
220 int i;
221
222 if (fmt == 0 || fmts == NULL || fmts[0] == 0)
223 return 0;
224
225 if (snd_fmtvalid(fmt, fmts))
226 return fmt;
227
228 best = 0;
229 score = snd_fmtscore(fmt);
230 oldscore = 0;
231 for (i = 0; fmts[i] != 0; i++) {
232 score2 = snd_fmtscore(fmts[i]);
233 if (cheq && !score_cheq(score, score2) &&
234 (score_chlt(score2, score) ||
235 (oldscore != 0 && score_chgt(score2, oldscore))))
236 continue;
237 if (oldscore == 0 ||
238 (score_val(score2) == score_val(score)) ||
239 (score_val(score2) == score_val(oldscore)) ||
240 (score_val(score2) > score_val(oldscore) &&
241 score_val(score2) < score_val(score)) ||
242 (score_val(score2) < score_val(oldscore) &&
243 score_val(score2) > score_val(score)) ||
244 (score_val(oldscore) < score_val(score) &&
245 score_val(score2) > score_val(oldscore))) {
246 if (score_val(oldscore) != score_val(score2) ||
247 score_cse(score) == score_cse(score2) ||
248 ((score_cse(oldscore) != score_cse(score) &&
249 !score_endianeq(score, oldscore) &&
250 (score_endianeq(score, score2) ||
251 (!score_signeq(score, oldscore) &&
252 score_signeq(score, score2)))))) {
253 best = fmts[i];
254 oldscore = score2;
255 }
256 }
257 }
258 return best;
259 }
260
261 u_int32_t
snd_fmtbestbit(u_int32_t fmt,u_int32_t * fmts)262 snd_fmtbestbit(u_int32_t fmt, u_int32_t *fmts)
263 {
264 return snd_fmtbestfunc(fmt, fmts, 0);
265 }
266
267 u_int32_t
snd_fmtbestchannel(u_int32_t fmt,u_int32_t * fmts)268 snd_fmtbestchannel(u_int32_t fmt, u_int32_t *fmts)
269 {
270 return snd_fmtbestfunc(fmt, fmts, 1);
271 }
272
273 u_int32_t
snd_fmtbest(u_int32_t fmt,u_int32_t * fmts)274 snd_fmtbest(u_int32_t fmt, u_int32_t *fmts)
275 {
276 u_int32_t best1, best2;
277 u_int32_t score, score1, score2;
278
279 if (snd_fmtvalid(fmt, fmts))
280 return fmt;
281
282 best1 = snd_fmtbestchannel(fmt, fmts);
283 best2 = snd_fmtbestbit(fmt, fmts);
284
285 if (best1 != 0 && best2 != 0 && best1 != best2) {
286 /*if (fmt & AFMT_STEREO)*/
287 if (AFMT_CHANNEL(fmt) > 1)
288 return best1;
289 else {
290 score = score_val(snd_fmtscore(fmt));
291 score1 = score_val(snd_fmtscore(best1));
292 score2 = score_val(snd_fmtscore(best2));
293 if (score1 == score2 || score1 == score)
294 return best1;
295 else if (score2 == score)
296 return best2;
297 else if (score1 > score2)
298 return best1;
299 return best2;
300 }
301 } else if (best2 == 0)
302 return best1;
303 else
304 return best2;
305 }
306
307 static int
feed_root(struct pcm_feeder * feeder,struct pcm_channel * ch,u_int8_t * buffer,u_int32_t count,void * source)308 feed_root(struct pcm_feeder *feeder, struct pcm_channel *ch, u_int8_t *buffer, u_int32_t count, void *source)
309 {
310 struct snd_dbuf *src = source;
311 int l, offset;
312
313 KASSERT(count > 0, ("feed_root: count == 0"));
314
315 if (++ch->feedcount == 0)
316 ch->feedcount = 2;
317
318 l = min(count, sndbuf_getready(src));
319
320 /* When recording only return as much data as available */
321 if (ch->direction == PCMDIR_REC) {
322 sndbuf_dispose(src, buffer, l);
323 return l;
324 }
325
326 offset = count - l;
327
328 if (offset > 0) {
329 if (snd_verbose > 3)
330 printf("%s: (%s) %spending %d bytes "
331 "(count=%d l=%d feed=%d)\n",
332 __func__,
333 (ch->flags & CHN_F_VIRTUAL) ? "virtual" : "hardware",
334 (ch->feedcount == 1) ? "pre" : "ap",
335 offset, count, l, ch->feedcount);
336
337 if (ch->feedcount == 1) {
338 memset(buffer, sndbuf_zerodata(src->fmt), offset);
339 if (l > 0)
340 sndbuf_dispose(src, buffer + offset, l);
341 else
342 ch->feedcount--;
343 } else {
344 if (l > 0)
345 sndbuf_dispose(src, buffer, l);
346 memset(buffer + l, sndbuf_zerodata(src->fmt), offset);
347 if (!(ch->flags & CHN_F_CLOSING))
348 ch->xruns++;
349 }
350 } else if (l > 0)
351 sndbuf_dispose(src, buffer, l);
352
353 return count;
354 }
355
356 static kobj_method_t feeder_root_methods[] = {
357 KOBJMETHOD(feeder_feed, feed_root),
358 KOBJMETHOD_END
359 };
360 static struct feeder_class feeder_root_class = {
361 .name = "feeder_root",
362 .methods = feeder_root_methods,
363 .size = sizeof(struct pcm_feeder),
364 .type = FEEDER_ROOT,
365 };
366 /*
367 * Register the root feeder first so that pcm_addchan() and subsequent
368 * functions can use it.
369 */
370 SYSINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_register,
371 &feeder_root_class);
372 SYSUNINIT(feeder_root, SI_SUB_DRIVERS, SI_ORDER_FIRST, feeder_unregisterall, NULL);
373