2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
3 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #ifdef HAVE_KERNEL_OPTION_HEADERS
32 #include <dev/sound/pcm/sound.h>
34 #include "feeder_if.h"
36 SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/feeder.c 227293 2011-11-07 06:44:47Z ed $");
38 static MALLOC_DEFINE(M_FEEDER
, "feeder", "pcm feeder");
40 #define MAXFEEDERS 256
43 struct feedertab_entry
{
44 SLIST_ENTRY(feedertab_entry
) link
;
45 struct feeder_class
*feederclass
;
46 struct pcm_feederdesc
*desc
;
50 static SLIST_HEAD(, feedertab_entry
) feedertab
;
52 /*****************************************************************************/
55 feeder_register(void *p
)
57 static int feedercnt
= 0;
59 struct feeder_class
*fc
= p
;
60 struct feedertab_entry
*fte
;
64 KASSERT(fc
->desc
== NULL
, ("first feeder not root: %s", fc
->name
));
66 SLIST_INIT(&feedertab
);
67 fte
= kmalloc(sizeof(*fte
), M_FEEDER
, M_WAITOK
| M_ZERO
);
69 kprintf("can't allocate memory for root feeder: %s\n",
74 fte
->feederclass
= fc
;
77 SLIST_INSERT_HEAD(&feedertab
, fte
, link
);
80 /* initialize global variables */
82 if (snd_verbose
< 0 || snd_verbose
> 4)
85 /* initialize unit numbering */
87 if (snd_unit
< 0 || snd_unit
> PCMMAXUNIT
)
90 if (snd_maxautovchans
< 0 ||
91 snd_maxautovchans
> SND_MAXVCHANS
)
92 snd_maxautovchans
= 0;
94 if (chn_latency
< CHN_LATENCY_MIN
||
95 chn_latency
> CHN_LATENCY_MAX
)
96 chn_latency
= CHN_LATENCY_DEFAULT
;
98 if (chn_latency_profile
< CHN_LATENCY_PROFILE_MIN
||
99 chn_latency_profile
> CHN_LATENCY_PROFILE_MAX
)
100 chn_latency_profile
= CHN_LATENCY_PROFILE_DEFAULT
;
102 if (feeder_rate_min
< FEEDRATE_MIN
||
103 feeder_rate_max
< FEEDRATE_MIN
||
104 feeder_rate_min
> FEEDRATE_MAX
||
105 feeder_rate_max
> FEEDRATE_MAX
||
106 !(feeder_rate_min
< feeder_rate_max
)) {
107 feeder_rate_min
= FEEDRATE_RATEMIN
;
108 feeder_rate_max
= FEEDRATE_RATEMAX
;
111 if (feeder_rate_round
< FEEDRATE_ROUNDHZ_MIN
||
112 feeder_rate_round
> FEEDRATE_ROUNDHZ_MAX
)
113 feeder_rate_round
= FEEDRATE_ROUNDHZ
;
116 kprintf("%s: snd_unit=%d snd_maxautovchans=%d "
118 "feeder_rate_min=%d feeder_rate_max=%d "
119 "feeder_rate_round=%d\n",
120 __func__
, snd_unit
, snd_maxautovchans
,
122 feeder_rate_min
, feeder_rate_max
,
125 /* we've got our root feeder so don't veto pcm loading anymore */
131 KASSERT(fc
->desc
!= NULL
, ("feeder '%s' has no descriptor", fc
->name
));
133 /* beyond this point failure is non-fatal but may result in some translations being unavailable */
135 while ((feedercnt
< MAXFEEDERS
) && (fc
->desc
[i
].type
> 0)) {
136 /* printf("adding feeder %s, %x -> %x\n", fc->name, fc->desc[i].in, fc->desc[i].out); */
137 fte
= kmalloc(sizeof(*fte
), M_FEEDER
, M_WAITOK
| M_ZERO
);
139 kprintf("can't allocate memory for feeder '%s', %x -> %x\n", fc
->name
, fc
->desc
[i
].in
, fc
->desc
[i
].out
);
143 fte
->feederclass
= fc
;
144 fte
->desc
= &fc
->desc
[i
];
145 fte
->idx
= feedercnt
;
146 fte
->desc
->idx
= feedercnt
;
147 SLIST_INSERT_HEAD(&feedertab
, fte
, link
);
151 if (feedercnt
>= MAXFEEDERS
)
152 kprintf("MAXFEEDERS (%d >= %d) exceeded\n", feedercnt
, MAXFEEDERS
);
156 feeder_unregisterall(void *p
)
158 struct feedertab_entry
*fte
, *next
;
160 next
= SLIST_FIRST(&feedertab
);
161 while (next
!= NULL
) {
163 next
= SLIST_NEXT(fte
, link
);
164 kfree(fte
, M_FEEDER
);
169 cmpdesc(struct pcm_feederdesc
*n
, struct pcm_feederdesc
*m
)
171 return ((n
->type
== m
->type
) &&
172 ((n
->in
== 0) || (n
->in
== m
->in
)) &&
173 ((n
->out
== 0) || (n
->out
== m
->out
)) &&
174 (n
->flags
== m
->flags
));
178 feeder_destroy(struct pcm_feeder
*f
)
181 kobj_delete((kobj_t
)f
, M_FEEDER
);
184 static struct pcm_feeder
*
185 feeder_create(struct feeder_class
*fc
, struct pcm_feederdesc
*desc
)
187 struct pcm_feeder
*f
;
190 f
= (struct pcm_feeder
*)kobj_create((kobj_class_t
)fc
, M_FEEDER
, M_WAITOK
| M_ZERO
);
198 f
->desc
= &(f
->desc_static
);
203 f
->desc
->type
= FEEDER_ROOT
;
210 err
= FEEDER_INIT(f
);
212 kprintf("feeder_init(%p) on %s returned %d\n", f
, fc
->name
, err
);
221 struct feeder_class
*
222 feeder_getclass(struct pcm_feederdesc
*desc
)
224 struct feedertab_entry
*fte
;
226 SLIST_FOREACH(fte
, &feedertab
, link
) {
227 if ((desc
== NULL
) && (fte
->desc
== NULL
))
228 return fte
->feederclass
;
229 if ((fte
->desc
!= NULL
) && (desc
!= NULL
) && cmpdesc(desc
, fte
->desc
))
230 return fte
->feederclass
;
236 chn_addfeeder(struct pcm_channel
*c
, struct feeder_class
*fc
, struct pcm_feederdesc
*desc
)
238 struct pcm_feeder
*nf
;
240 nf
= feeder_create(fc
, desc
);
244 nf
->source
= c
->feeder
;
246 if (c
->feeder
!= NULL
)
247 c
->feeder
->parent
= nf
;
254 chn_removefeeder(struct pcm_channel
*c
)
256 struct pcm_feeder
*f
;
258 if (c
->feeder
== NULL
)
261 c
->feeder
= c
->feeder
->source
;
268 chn_findfeeder(struct pcm_channel
*c
, u_int32_t type
)
270 struct pcm_feeder
*f
;
274 if (f
->desc
->type
== type
)
283 * 14bit format scoring
284 * --------------------
286 * 13 12 11 10 9 8 2 1 0 offset
287 * +---+---+---+---+---+---+-------------+---+---+
288 * | X | X | X | X | X | X | X X X X X X | X | X |
289 * +---+---+---+---+---+---+-------------+---+---+
291 * | | | | | | | | +--> signed?
293 * | | | | | | | +------> bigendian?
295 * | | | | | | +---------------> total channels
297 * | | | | | +------------------------> AFMT_A_LAW
299 * | | | | +----------------------------> AFMT_MU_LAW
301 * | | | +--------------------------------> AFMT_8BIT
303 * | | +------------------------------------> AFMT_16BIT
305 * | +----------------------------------------> AFMT_24BIT
307 * +--------------------------------------------> AFMT_32BIT
309 #define score_signeq(s1, s2) (((s1) & 0x1) == ((s2) & 0x1))
310 #define score_endianeq(s1, s2) (((s1) & 0x2) == ((s2) & 0x2))
311 #define score_cheq(s1, s2) (((s1) & 0xfc) == ((s2) & 0xfc))
312 #define score_chgt(s1, s2) (((s1) & 0xfc) > ((s2) & 0xfc))
313 #define score_chlt(s1, s2) (((s1) & 0xfc) < ((s2) & 0xfc))
314 #define score_val(s1) ((s1) & 0x3f00)
315 #define score_cse(s1) ((s1) & 0x7f)
318 snd_fmtscore(u_int32_t fmt
)
323 if (fmt
& AFMT_SIGNED
)
325 if (fmt
& AFMT_BIGENDIAN
)
327 /*if (fmt & AFMT_STEREO)
328 ret |= (2 & 0x3f) << 2;
330 ret |= (1 & 0x3f) << 2;*/
331 ret
|= (AFMT_CHANNEL(fmt
) & 0x3f) << 2;
332 if (fmt
& AFMT_A_LAW
)
334 else if (fmt
& AFMT_MU_LAW
)
336 else if (fmt
& AFMT_8BIT
)
338 else if (fmt
& AFMT_16BIT
)
340 else if (fmt
& AFMT_24BIT
)
342 else if (fmt
& AFMT_32BIT
)
349 snd_fmtbestfunc(u_int32_t fmt
, u_int32_t
*fmts
, int cheq
)
351 u_int32_t best
, score
, score2
, oldscore
;
354 if (fmt
== 0 || fmts
== NULL
|| fmts
[0] == 0)
357 if (snd_fmtvalid(fmt
, fmts
))
361 score
= snd_fmtscore(fmt
);
363 for (i
= 0; fmts
[i
] != 0; i
++) {
364 score2
= snd_fmtscore(fmts
[i
]);
365 if (cheq
&& !score_cheq(score
, score2
) &&
366 (score_chlt(score2
, score
) ||
367 (oldscore
!= 0 && score_chgt(score2
, oldscore
))))
370 (score_val(score2
) == score_val(score
)) ||
371 (score_val(score2
) == score_val(oldscore
)) ||
372 (score_val(score2
) > score_val(oldscore
) &&
373 score_val(score2
) < score_val(score
)) ||
374 (score_val(score2
) < score_val(oldscore
) &&
375 score_val(score2
) > score_val(score
)) ||
376 (score_val(oldscore
) < score_val(score
) &&
377 score_val(score2
) > score_val(oldscore
))) {
378 if (score_val(oldscore
) != score_val(score2
) ||
379 score_cse(score
) == score_cse(score2
) ||
380 ((score_cse(oldscore
) != score_cse(score
) &&
381 !score_endianeq(score
, oldscore
) &&
382 (score_endianeq(score
, score2
) ||
383 (!score_signeq(score
, oldscore
) &&
384 score_signeq(score
, score2
)))))) {
394 snd_fmtbestbit(u_int32_t fmt
, u_int32_t
*fmts
)
396 return snd_fmtbestfunc(fmt
, fmts
, 0);
400 snd_fmtbestchannel(u_int32_t fmt
, u_int32_t
*fmts
)
402 return snd_fmtbestfunc(fmt
, fmts
, 1);
406 snd_fmtbest(u_int32_t fmt
, u_int32_t
*fmts
)
408 u_int32_t best1
, best2
;
409 u_int32_t score
, score1
, score2
;
411 if (snd_fmtvalid(fmt
, fmts
))
414 best1
= snd_fmtbestchannel(fmt
, fmts
);
415 best2
= snd_fmtbestbit(fmt
, fmts
);
417 if (best1
!= 0 && best2
!= 0 && best1
!= best2
) {
418 /*if (fmt & AFMT_STEREO)*/
419 if (AFMT_CHANNEL(fmt
) > 1)
422 score
= score_val(snd_fmtscore(fmt
));
423 score1
= score_val(snd_fmtscore(best1
));
424 score2
= score_val(snd_fmtscore(best2
));
425 if (score1
== score2
|| score1
== score
)
427 else if (score2
== score
)
429 else if (score1
> score2
)
433 } else if (best2
== 0)
440 feeder_printchain(struct pcm_feeder
*head
)
442 struct pcm_feeder
*f
;
444 kprintf("feeder chain (head @%p)\n", head
);
447 kprintf("%s/%d @ %p\n", f
->class->name
, f
->desc
->idx
, f
);
450 kprintf("[end]\n\n");
453 /*****************************************************************************/
456 feed_root(struct pcm_feeder
*feeder
, struct pcm_channel
*ch
, u_int8_t
*buffer
, u_int32_t count
, void *source
)
458 struct snd_dbuf
*src
= source
;
461 KASSERT(count
> 0, ("feed_root: count == 0"));
463 if (++ch
->feedcount
== 0)
466 l
= min(count
, sndbuf_getready(src
));
468 /* When recording only return as much data as available */
469 if (ch
->direction
== PCMDIR_REC
) {
470 sndbuf_dispose(src
, buffer
, l
);
479 kprintf("%s: (%s) %spending %d bytes "
480 "(count=%d l=%d feed=%d)\n",
482 (ch
->flags
& CHN_F_VIRTUAL
) ? "virtual" : "hardware",
483 (ch
->feedcount
== 1) ? "pre" : "ap",
484 offset
, count
, l
, ch
->feedcount
);
486 if (ch
->feedcount
== 1) {
488 sndbuf_zerodata(sndbuf_getfmt(src
)),
491 sndbuf_dispose(src
, buffer
+ offset
, l
);
496 sndbuf_dispose(src
, buffer
, l
);
498 sndbuf_zerodata(sndbuf_getfmt(src
)),
500 if (!(ch
->flags
& CHN_F_CLOSING
))
504 sndbuf_dispose(src
, buffer
, l
);
509 static kobj_method_t feeder_root_methods
[] = {
510 KOBJMETHOD(feeder_feed
, feed_root
),
513 static struct feeder_class feeder_root_class
= {
514 .name
= "feeder_root",
515 .methods
= feeder_root_methods
,
516 .size
= sizeof(struct pcm_feeder
),
520 SYSINIT(feeder_root
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, feeder_register
, &feeder_root_class
);
521 SYSUNINIT(feeder_root
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, feeder_unregisterall
, NULL
);