1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * All rights reserved.
6 * Copyright (c) 2024-2025 The FreeBSD Foundation
7 *
8 * Portions of this software were developed by Christos Margiolis
9 * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * feeder_matrix: Generic any-to-any channel matrixing. Probably not the
35 * accurate way of doing things, but it should be fast and
36 * transparent enough, not to mention capable of handling
37 * possible non-standard way of multichannel interleaving
38 * order. In other words, it is tough to break.
39 *
40 * The Good:
41 * + very generic and compact, provided that the supplied matrix map is in a
42 * sane form.
43 * + should be fast enough.
44 *
45 * The Bad:
46 * + somebody might disagree with it.
47 * + 'matrix' is kind of 0x7a69, due to prolong mental block.
48 */
49
50 #ifdef _KERNEL
51 #ifdef HAVE_KERNEL_OPTION_HEADERS
52 #include "opt_snd.h"
53 #endif
54 #include <dev/sound/pcm/sound.h>
55 #include <dev/sound/pcm/pcm.h>
56 #include "feeder_if.h"
57
58 #define SND_USE_FXDIV
59 #include "snd_fxdiv_gen.h"
60 #endif
61
62 #define FEEDMATRIX_RESERVOIR (SND_CHN_MAX * PCM_32_BPS)
63
64 #define SND_CHN_T_EOF 0x00e0fe0f
65 #define SND_CHN_T_NULL 0x0e0e0e0e
66
67 struct feed_matrix_info {
68 uint32_t fmt;
69 uint32_t bps;
70 uint32_t ialign, oalign;
71 uint32_t in, out;
72 struct {
73 int chn[SND_CHN_T_MAX + 1];
74 int mul, shift;
75 } matrix[SND_CHN_T_MAX + 1];
76 uint8_t reservoir[FEEDMATRIX_RESERVOIR];
77 };
78
79 static struct pcmchan_matrix feeder_matrix_maps[SND_CHN_MATRIX_MAX] = {
80 [SND_CHN_MATRIX_1_0] = SND_CHN_MATRIX_MAP_1_0,
81 [SND_CHN_MATRIX_2_0] = SND_CHN_MATRIX_MAP_2_0,
82 [SND_CHN_MATRIX_2_1] = SND_CHN_MATRIX_MAP_2_1,
83 [SND_CHN_MATRIX_3_0] = SND_CHN_MATRIX_MAP_3_0,
84 [SND_CHN_MATRIX_3_1] = SND_CHN_MATRIX_MAP_3_1,
85 [SND_CHN_MATRIX_4_0] = SND_CHN_MATRIX_MAP_4_0,
86 [SND_CHN_MATRIX_4_1] = SND_CHN_MATRIX_MAP_4_1,
87 [SND_CHN_MATRIX_5_0] = SND_CHN_MATRIX_MAP_5_0,
88 [SND_CHN_MATRIX_5_1] = SND_CHN_MATRIX_MAP_5_1,
89 [SND_CHN_MATRIX_6_0] = SND_CHN_MATRIX_MAP_6_0,
90 [SND_CHN_MATRIX_6_1] = SND_CHN_MATRIX_MAP_6_1,
91 [SND_CHN_MATRIX_7_0] = SND_CHN_MATRIX_MAP_7_0,
92 [SND_CHN_MATRIX_7_1] = SND_CHN_MATRIX_MAP_7_1
93 };
94
95 static int feeder_matrix_default_ids[9] = {
96 [0] = SND_CHN_MATRIX_UNKNOWN,
97 [1] = SND_CHN_MATRIX_1,
98 [2] = SND_CHN_MATRIX_2,
99 [3] = SND_CHN_MATRIX_3,
100 [4] = SND_CHN_MATRIX_4,
101 [5] = SND_CHN_MATRIX_5,
102 [6] = SND_CHN_MATRIX_6,
103 [7] = SND_CHN_MATRIX_7,
104 [8] = SND_CHN_MATRIX_8
105 };
106
107 #ifdef _KERNEL
108 #define FEEDMATRIX_CLIP_CHECK(...)
109 #else
110 #define FEEDMATRIX_CLIP_CHECK(v, BIT) do { \
111 if ((v) < PCM_S##BIT##_MIN || (v) > PCM_S##BIT##_MAX) \
112 errx(1, "\n\n%s(): Sample clipping: %jd\n", \
113 __func__, (intmax_t)(v)); \
114 } while (0)
115 #endif
116
117 __always_inline static void
feed_matrix_apply(struct feed_matrix_info * info,uint8_t * src,uint8_t * dst,uint32_t count,const uint32_t fmt)118 feed_matrix_apply(struct feed_matrix_info *info, uint8_t *src, uint8_t *dst,
119 uint32_t count, const uint32_t fmt)
120 {
121 intpcm64_t accum;
122 intpcm_t v;
123 int i, j;
124
125 do {
126 for (i = 0; info->matrix[i].chn[0] != SND_CHN_T_EOF; i++) {
127 if (info->matrix[i].chn[0] == SND_CHN_T_NULL) {
128 pcm_sample_write(dst, 0, fmt);
129 dst += info->bps;
130 continue;
131 } else if (info->matrix[i].chn[1] == SND_CHN_T_EOF) {
132 v = pcm_sample_read(src +
133 info->matrix[i].chn[0], fmt);
134 pcm_sample_write(dst, v, fmt);
135 dst += info->bps;
136 continue;
137 }
138
139 accum = 0;
140 for (j = 0; info->matrix[i].chn[j] != SND_CHN_T_EOF;
141 j++) {
142 v = pcm_sample_read(src +
143 info->matrix[i].chn[j], fmt);
144 accum += v;
145 }
146
147 accum = (accum * info->matrix[i].mul) >>
148 info->matrix[i].shift;
149
150 FEEDMATRIX_CLIP_CHECK(accum, AFMT_BIT(fmt));
151
152 v = pcm_clamp(accum, fmt);
153 pcm_sample_write(dst, v, fmt);
154 dst += info->bps;
155 }
156 src += info->ialign;
157 } while (--count != 0);
158 }
159
160 static void
feed_matrix_reset(struct feed_matrix_info * info)161 feed_matrix_reset(struct feed_matrix_info *info)
162 {
163 uint32_t i, j;
164
165 for (i = 0; i < nitems(info->matrix); i++) {
166 for (j = 0;
167 j < (sizeof(info->matrix[i].chn) /
168 sizeof(info->matrix[i].chn[0])); j++) {
169 info->matrix[i].chn[j] = SND_CHN_T_EOF;
170 }
171 info->matrix[i].mul = 1;
172 info->matrix[i].shift = 0;
173 }
174 }
175
176 static int
feed_matrix_setup(struct feed_matrix_info * info,struct pcmchan_matrix * m_in,struct pcmchan_matrix * m_out)177 feed_matrix_setup(struct feed_matrix_info *info, struct pcmchan_matrix *m_in,
178 struct pcmchan_matrix *m_out)
179 {
180 uint32_t i, j, ch, in_mask, merge_mask;
181 int mul, shift;
182
183 if (info == NULL || m_in == NULL || m_out == NULL ||
184 AFMT_CHANNEL(info->in) != m_in->channels ||
185 AFMT_CHANNEL(info->out) != m_out->channels ||
186 m_in->channels < SND_CHN_MIN || m_in->channels > SND_CHN_MAX ||
187 m_out->channels < SND_CHN_MIN || m_out->channels > SND_CHN_MAX)
188 return (EINVAL);
189
190 feed_matrix_reset(info);
191
192 /*
193 * If both in and out are part of standard matrix and identical, skip
194 * everything altogether.
195 */
196 if (m_in->id == m_out->id && !(m_in->id < SND_CHN_MATRIX_BEGIN ||
197 m_in->id > SND_CHN_MATRIX_END))
198 return (0);
199
200 /*
201 * Special case for mono input matrix. If the output supports
202 * possible 'center' channel, route it there. Otherwise, let it be
203 * matrixed to left/right.
204 */
205 if (m_in->id == SND_CHN_MATRIX_1_0) {
206 if (m_out->id == SND_CHN_MATRIX_1_0)
207 in_mask = SND_CHN_T_MASK_FL;
208 else if (m_out->mask & SND_CHN_T_MASK_FC)
209 in_mask = SND_CHN_T_MASK_FC;
210 else
211 in_mask = SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR;
212 } else
213 in_mask = m_in->mask;
214
215 /* Merge, reduce, expand all possibilites. */
216 for (ch = SND_CHN_T_BEGIN; ch <= SND_CHN_T_END &&
217 m_out->map[ch].type != SND_CHN_T_MAX; ch += SND_CHN_T_STEP) {
218 merge_mask = m_out->map[ch].members & in_mask;
219 if (merge_mask == 0) {
220 info->matrix[ch].chn[0] = SND_CHN_T_NULL;
221 continue;
222 }
223
224 j = 0;
225 for (i = SND_CHN_T_BEGIN; i <= SND_CHN_T_END;
226 i += SND_CHN_T_STEP) {
227 if (merge_mask & (1 << i)) {
228 if (m_in->offset[i] >= 0 &&
229 m_in->offset[i] < (int)m_in->channels)
230 info->matrix[ch].chn[j++] =
231 m_in->offset[i] * info->bps;
232 else {
233 info->matrix[ch].chn[j++] =
234 SND_CHN_T_EOF;
235 break;
236 }
237 }
238 }
239
240 #define FEEDMATRIX_ATTN_SHIFT 16
241
242 if (j > 1) {
243 /*
244 * XXX For channel that require accumulation from
245 * multiple channels, apply a slight attenuation to
246 * avoid clipping.
247 */
248 mul = (1 << (FEEDMATRIX_ATTN_SHIFT - 1)) + 143 - j;
249 shift = FEEDMATRIX_ATTN_SHIFT;
250 while ((mul & 1) == 0 && shift > 0) {
251 mul >>= 1;
252 shift--;
253 }
254 info->matrix[ch].mul = mul;
255 info->matrix[ch].shift = shift;
256 }
257 }
258
259 #ifndef _KERNEL
260 fprintf(stderr, "Total: %d\n", ch);
261
262 for (i = 0; info->matrix[i].chn[0] != SND_CHN_T_EOF; i++) {
263 fprintf(stderr, "%d: [", i);
264 for (j = 0; info->matrix[i].chn[j] != SND_CHN_T_EOF; j++) {
265 if (j != 0)
266 fprintf(stderr, ", ");
267 fprintf(stderr, "%d",
268 (info->matrix[i].chn[j] == SND_CHN_T_NULL) ?
269 0xffffffff : info->matrix[i].chn[j] / info->bps);
270 }
271 fprintf(stderr, "] attn: (x * %d) >> %d\n",
272 info->matrix[i].mul, info->matrix[i].shift);
273 }
274 #endif
275
276 return (0);
277 }
278
279 static int
feed_matrix_init(struct pcm_feeder * f)280 feed_matrix_init(struct pcm_feeder *f)
281 {
282 struct feed_matrix_info *info;
283 struct pcmchan_matrix *m_in, *m_out;
284 int ret;
285
286 if (AFMT_ENCODING(f->desc.in) != AFMT_ENCODING(f->desc.out))
287 return (EINVAL);
288
289 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO);
290 if (info == NULL)
291 return (ENOMEM);
292
293 info->in = f->desc.in;
294 info->out = f->desc.out;
295 info->fmt = AFMT_ENCODING(info->in);
296 info->bps = AFMT_BPS(info->in);
297 info->ialign = AFMT_ALIGN(info->in);
298 info->oalign = AFMT_ALIGN(info->out);
299
300 m_in = feeder_matrix_format_map(info->in);
301 m_out = feeder_matrix_format_map(info->out);
302
303 ret = feed_matrix_setup(info, m_in, m_out);
304 if (ret != 0) {
305 free(info, M_DEVBUF);
306 return (ret);
307 }
308
309 f->data = info;
310
311 return (0);
312 }
313
314 static int
feed_matrix_free(struct pcm_feeder * f)315 feed_matrix_free(struct pcm_feeder *f)
316 {
317 struct feed_matrix_info *info;
318
319 info = f->data;
320 free(info, M_DEVBUF);
321
322 f->data = NULL;
323
324 return (0);
325 }
326
327 static int
feed_matrix_feed(struct pcm_feeder * f,struct pcm_channel * c,uint8_t * b,uint32_t count,void * source)328 feed_matrix_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
329 uint32_t count, void *source)
330 {
331 struct feed_matrix_info *info;
332 uint32_t j, inmax;
333 uint8_t *src, *dst;
334
335 info = f->data;
336 if (info->matrix[0].chn[0] == SND_CHN_T_EOF)
337 return (FEEDER_FEED(f->source, c, b, count, source));
338
339 dst = b;
340 count = SND_FXROUND(count, info->oalign);
341 inmax = info->ialign + info->oalign;
342
343 /*
344 * This loop might look simmilar to other feeder_* loops, but be
345 * advised: matrixing might involve overlapping (think about
346 * swapping end to front or something like that). In this regard it
347 * might be simmilar to feeder_format, but feeder_format works on
348 * 'sample' domain where it can be fitted into single 32bit integer
349 * while matrixing works on 'sample frame' domain.
350 */
351 do {
352 if (count < info->oalign)
353 break;
354
355 if (count < inmax) {
356 src = info->reservoir;
357 j = info->ialign;
358 } else {
359 if (info->ialign == info->oalign)
360 j = count - info->oalign;
361 else if (info->ialign > info->oalign)
362 j = SND_FXROUND(count - info->oalign,
363 info->ialign);
364 else
365 j = (SND_FXDIV(count, info->oalign) - 1) *
366 info->ialign;
367 src = dst + count - j;
368 }
369
370 j = SND_FXDIV(FEEDER_FEED(f->source, c, src, j, source),
371 info->ialign);
372 if (j == 0)
373 break;
374
375 /* Optimize some common formats. */
376 switch (info->fmt) {
377 case AFMT_S16_NE:
378 feed_matrix_apply(info, src, dst, j, AFMT_S16_NE);
379 break;
380 case AFMT_S24_NE:
381 feed_matrix_apply(info, src, dst, j, AFMT_S24_NE);
382 break;
383 case AFMT_S32_NE:
384 feed_matrix_apply(info, src, dst, j, AFMT_S32_NE);
385 break;
386 default:
387 feed_matrix_apply(info, src, dst, j, info->fmt);
388 break;
389 }
390
391 j *= info->oalign;
392 dst += j;
393 count -= j;
394
395 } while (count != 0);
396
397 return (dst - b);
398 }
399
400 static kobj_method_t feeder_matrix_methods[] = {
401 KOBJMETHOD(feeder_init, feed_matrix_init),
402 KOBJMETHOD(feeder_free, feed_matrix_free),
403 KOBJMETHOD(feeder_feed, feed_matrix_feed),
404 KOBJMETHOD_END
405 };
406
407 FEEDER_DECLARE(feeder_matrix, FEEDER_MATRIX);
408
409 /* External */
410 int
feeder_matrix_setup(struct pcm_feeder * f,struct pcmchan_matrix * m_in,struct pcmchan_matrix * m_out)411 feeder_matrix_setup(struct pcm_feeder *f, struct pcmchan_matrix *m_in,
412 struct pcmchan_matrix *m_out)
413 {
414
415 if (f == NULL || f->class->type != FEEDER_MATRIX || f->data == NULL)
416 return (EINVAL);
417
418 return (feed_matrix_setup(f->data, m_in, m_out));
419 }
420
421 /*
422 * feeder_matrix_default_id(): For a given number of channels, return
423 * default preferred id (example: both 5.1 and
424 * 6.0 are simply 6 channels, but 5.1 is more
425 * preferable).
426 */
427 int
feeder_matrix_default_id(uint32_t ch)428 feeder_matrix_default_id(uint32_t ch)
429 {
430
431 if (ch < feeder_matrix_maps[SND_CHN_MATRIX_BEGIN].channels ||
432 ch > feeder_matrix_maps[SND_CHN_MATRIX_END].channels)
433 return (SND_CHN_MATRIX_UNKNOWN);
434
435 return (feeder_matrix_maps[feeder_matrix_default_ids[ch]].id);
436 }
437
438 /*
439 * feeder_matrix_default_channel_map(): Ditto, but return matrix map
440 * instead.
441 */
442 struct pcmchan_matrix *
feeder_matrix_default_channel_map(uint32_t ch)443 feeder_matrix_default_channel_map(uint32_t ch)
444 {
445
446 if (ch < feeder_matrix_maps[SND_CHN_MATRIX_BEGIN].channels ||
447 ch > feeder_matrix_maps[SND_CHN_MATRIX_END].channels)
448 return (NULL);
449
450 return (&feeder_matrix_maps[feeder_matrix_default_ids[ch]]);
451 }
452
453 /*
454 * feeder_matrix_default_format(): For a given audio format, return the
455 * proper audio format based on preferable
456 * matrix.
457 */
458 uint32_t
feeder_matrix_default_format(uint32_t format)459 feeder_matrix_default_format(uint32_t format)
460 {
461 struct pcmchan_matrix *m;
462 uint32_t i, ch, ext;
463
464 ch = AFMT_CHANNEL(format);
465 ext = AFMT_EXTCHANNEL(format);
466
467 if (ext != 0) {
468 for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) {
469 if (feeder_matrix_maps[i].channels == ch &&
470 feeder_matrix_maps[i].ext == ext)
471 return (SND_FORMAT(format, ch, ext));
472 }
473 }
474
475 m = feeder_matrix_default_channel_map(ch);
476 if (m == NULL)
477 return (0x00000000);
478
479 return (SND_FORMAT(format, ch, m->ext));
480 }
481
482 /*
483 * feeder_matrix_format_id(): For a given audio format, return its matrix
484 * id.
485 */
486 int
feeder_matrix_format_id(uint32_t format)487 feeder_matrix_format_id(uint32_t format)
488 {
489 uint32_t i, ch, ext;
490
491 ch = AFMT_CHANNEL(format);
492 ext = AFMT_EXTCHANNEL(format);
493
494 for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) {
495 if (feeder_matrix_maps[i].channels == ch &&
496 feeder_matrix_maps[i].ext == ext)
497 return (feeder_matrix_maps[i].id);
498 }
499
500 return (SND_CHN_MATRIX_UNKNOWN);
501 }
502
503 /*
504 * feeder_matrix_format_map(): For a given audio format, return its matrix
505 * map.
506 */
507 struct pcmchan_matrix *
feeder_matrix_format_map(uint32_t format)508 feeder_matrix_format_map(uint32_t format)
509 {
510 uint32_t i, ch, ext;
511
512 ch = AFMT_CHANNEL(format);
513 ext = AFMT_EXTCHANNEL(format);
514
515 for (i = SND_CHN_MATRIX_BEGIN; i <= SND_CHN_MATRIX_END; i++) {
516 if (feeder_matrix_maps[i].channels == ch &&
517 feeder_matrix_maps[i].ext == ext)
518 return (&feeder_matrix_maps[i]);
519 }
520
521 return (NULL);
522 }
523
524 /*
525 * feeder_matrix_id_map(): For a given matrix id, return its matrix map.
526 */
527 struct pcmchan_matrix *
feeder_matrix_id_map(int id)528 feeder_matrix_id_map(int id)
529 {
530
531 if (id < SND_CHN_MATRIX_BEGIN || id > SND_CHN_MATRIX_END)
532 return (NULL);
533
534 return (&feeder_matrix_maps[id]);
535 }
536
537 /*
538 * feeder_matrix_compare(): Compare the simmilarities of matrices.
539 */
540 int
feeder_matrix_compare(struct pcmchan_matrix * m_in,struct pcmchan_matrix * m_out)541 feeder_matrix_compare(struct pcmchan_matrix *m_in, struct pcmchan_matrix *m_out)
542 {
543 uint32_t i;
544
545 if (m_in == m_out)
546 return (0);
547
548 if (m_in->channels != m_out->channels || m_in->ext != m_out->ext ||
549 m_in->mask != m_out->mask)
550 return (1);
551
552 for (i = 0; i < nitems(m_in->map); i++) {
553 if (m_in->map[i].type != m_out->map[i].type)
554 return (1);
555 if (m_in->map[i].type == SND_CHN_T_MAX)
556 break;
557 if (m_in->map[i].members != m_out->map[i].members)
558 return (1);
559 if (i <= SND_CHN_T_END) {
560 if (m_in->offset[m_in->map[i].type] !=
561 m_out->offset[m_out->map[i].type])
562 return (1);
563 }
564 }
565
566 return (0);
567 }
568
569 /*
570 * XXX 4front interpretation of "surround" is ambigous and sort of
571 * conflicting with "rear"/"back". Map it to "side". Well..
572 * who cares?
573 */
574 static int snd_chn_to_oss[SND_CHN_T_MAX] = {
575 [SND_CHN_T_FL] = CHID_L,
576 [SND_CHN_T_FR] = CHID_R,
577 [SND_CHN_T_FC] = CHID_C,
578 [SND_CHN_T_LF] = CHID_LFE,
579 [SND_CHN_T_SL] = CHID_LS,
580 [SND_CHN_T_SR] = CHID_RS,
581 [SND_CHN_T_BL] = CHID_LR,
582 [SND_CHN_T_BR] = CHID_RR
583 };
584
585 #define SND_CHN_OSS_VALIDMASK \
586 (SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FR | \
587 SND_CHN_T_MASK_FC | SND_CHN_T_MASK_LF | \
588 SND_CHN_T_MASK_SL | SND_CHN_T_MASK_SR | \
589 SND_CHN_T_MASK_BL | SND_CHN_T_MASK_BR)
590
591 #define SND_CHN_OSS_MAX 8
592 #define SND_CHN_OSS_BEGIN CHID_L
593 #define SND_CHN_OSS_END CHID_RR
594
595 static int oss_to_snd_chn[SND_CHN_OSS_END + 1] = {
596 [CHID_L] = SND_CHN_T_FL,
597 [CHID_R] = SND_CHN_T_FR,
598 [CHID_C] = SND_CHN_T_FC,
599 [CHID_LFE] = SND_CHN_T_LF,
600 [CHID_LS] = SND_CHN_T_SL,
601 [CHID_RS] = SND_CHN_T_SR,
602 [CHID_LR] = SND_CHN_T_BL,
603 [CHID_RR] = SND_CHN_T_BR
604 };
605
606 /*
607 * Used by SNDCTL_DSP_GET_CHNORDER.
608 */
609 int
feeder_matrix_oss_get_channel_order(struct pcmchan_matrix * m,unsigned long long * map)610 feeder_matrix_oss_get_channel_order(struct pcmchan_matrix *m,
611 unsigned long long *map)
612 {
613 unsigned long long tmpmap;
614 uint32_t i;
615
616 if (m == NULL || map == NULL || (m->mask & ~SND_CHN_OSS_VALIDMASK) ||
617 m->channels > SND_CHN_OSS_MAX)
618 return (EINVAL);
619
620 tmpmap = 0x0000000000000000ULL;
621
622 for (i = 0; i < SND_CHN_OSS_MAX && m->map[i].type != SND_CHN_T_MAX;
623 i++) {
624 if ((1 << m->map[i].type) & ~SND_CHN_OSS_VALIDMASK)
625 return (EINVAL);
626 tmpmap |=
627 (unsigned long long)snd_chn_to_oss[m->map[i].type] <<
628 (i * 4);
629 }
630
631 *map = tmpmap;
632
633 return (0);
634 }
635
636 /*
637 * Used by SNDCTL_DSP_SET_CHNORDER.
638 */
639 int
feeder_matrix_oss_set_channel_order(struct pcmchan_matrix * m,unsigned long long * map)640 feeder_matrix_oss_set_channel_order(struct pcmchan_matrix *m,
641 unsigned long long *map)
642 {
643 struct pcmchan_matrix tmp;
644 uint32_t chmask, i;
645 int ch, cheof;
646
647 if (m == NULL || map == NULL || (m->mask & ~SND_CHN_OSS_VALIDMASK) ||
648 m->channels > SND_CHN_OSS_MAX || (*map & 0xffffffff00000000ULL))
649 return (EINVAL);
650
651 tmp = *m;
652 tmp.channels = 0;
653 tmp.ext = 0;
654 tmp.mask = 0;
655 memset(tmp.offset, -1, sizeof(tmp.offset));
656 cheof = 0;
657
658 for (i = 0; i < SND_CHN_OSS_MAX; i++) {
659 ch = (*map >> (i * 4)) & 0xf;
660 if (ch < SND_CHN_OSS_BEGIN) {
661 if (cheof == 0 && m->map[i].type != SND_CHN_T_MAX)
662 return (EINVAL);
663 cheof++;
664 tmp.map[i] = m->map[i];
665 continue;
666 } else if (ch > SND_CHN_OSS_END)
667 return (EINVAL);
668 else if (cheof != 0)
669 return (EINVAL);
670 ch = oss_to_snd_chn[ch];
671 chmask = 1 << ch;
672 /* channel not exist in matrix */
673 if (!(chmask & m->mask))
674 return (EINVAL);
675 /* duplicated channel */
676 if (chmask & tmp.mask)
677 return (EINVAL);
678 tmp.map[i] = m->map[m->offset[ch]];
679 if (tmp.map[i].type != ch)
680 return (EINVAL);
681 tmp.offset[ch] = i;
682 tmp.mask |= chmask;
683 tmp.channels++;
684 if (chmask & SND_CHN_T_MASK_LF)
685 tmp.ext++;
686 }
687
688 if (tmp.channels != m->channels || tmp.ext != m->ext ||
689 tmp.mask != m->mask ||
690 tmp.map[m->channels].type != SND_CHN_T_MAX)
691 return (EINVAL);
692
693 *m = tmp;
694
695 return (0);
696 }
697