1 /*-
2 * Copyright (c) 2001 Cameron Grant <cg@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <dev/sound/pcm/sound.h>
28 #include <dev/sound/pcm/vchan.h>
29 #include "feeder_if.h"
30
31 SND_DECLARE_FILE("$FreeBSD: releng/6.1/sys/dev/sound/pcm/vchan.c 157495 2006-04-04 17:43:49Z ariff $");
32
33 /*
34 * Default speed
35 */
36 #define VCHAN_DEFAULT_SPEED 48000
37
38 extern int feeder_rate_ratemin;
39 extern int feeder_rate_ratemax;
40
41 struct vchinfo {
42 u_int32_t spd, fmt, blksz, bps, run;
43 struct pcm_channel *channel, *parent;
44 struct pcmchan_caps caps;
45 };
46
47 static u_int32_t vchan_fmt[] = {
48 AFMT_STEREO | AFMT_S16_LE,
49 0
50 };
51
52 static int
53 vchan_mix_s16(int16_t *to, int16_t *tmp, unsigned int count)
54 {
55 /*
56 * to is the output buffer, tmp is the input buffer
57 * count is the number of 16bit samples to mix
58 */
59 int i;
60 int x;
61
62 for(i = 0; i < count; i++) {
63 x = to[i];
64 x += tmp[i];
65 if (x < -32768) {
66 /* printf("%d + %d = %d (u)\n", to[i], tmp[i], x); */
67 x = -32768;
68 }
69 if (x > 32767) {
70 /* printf("%d + %d = %d (o)\n", to[i], tmp[i], x); */
71 x = 32767;
72 }
73 to[i] = x & 0x0000ffff;
74 }
75 return 0;
76 }
77
78 static int
79 feed_vchan_s16(struct pcm_feeder *f, struct pcm_channel *c, u_int8_t *b, u_int32_t count, void *source)
80 {
81 /* we're going to abuse things a bit */
82 struct snd_dbuf *src = source;
83 struct pcmchan_children *cce;
84 struct pcm_channel *ch;
85 uint32_t sz;
86 int16_t *tmp, *dst;
87 unsigned int cnt, rcnt = 0;
88
89 #if 0
90 if (sndbuf_getsize(src) < count)
91 panic("feed_vchan_s16(%s): tmp buffer size %d < count %d, flags = 0x%x",
92 c->name, sndbuf_getsize(src), count, c->flags);
93 #endif
94 sz = sndbuf_getsize(src);
95 if (sz < count)
96 count = sz;
97 count &= ~1;
98 if (count < 2)
99 return 0;
100 bzero(b, count);
101
102 /*
103 * we are going to use our source as a temporary buffer since it's
104 * got no other purpose. we obtain our data by traversing the channel
105 * list of children and calling vchan_mix_* to mix count bytes from each
106 * into our destination buffer, b
107 */
108 dst = (int16_t *)b;
109 tmp = (int16_t *)sndbuf_getbuf(src);
110 bzero(tmp, count);
111 SLIST_FOREACH(cce, &c->children, link) {
112 ch = cce->channel;
113 CHN_LOCK(ch);
114 if (ch->flags & CHN_F_TRIGGERED) {
115 if (ch->flags & CHN_F_MAPPED)
116 sndbuf_acquire(ch->bufsoft, NULL, sndbuf_getfree(ch->bufsoft));
117 cnt = FEEDER_FEED(ch->feeder, ch, (u_int8_t *)tmp, count, ch->bufsoft);
118 vchan_mix_s16(dst, tmp, cnt >> 1);
119 if (cnt > rcnt)
120 rcnt = cnt;
121 }
122 CHN_UNLOCK(ch);
123 }
124
125 return rcnt & ~1;
126 }
127
128 static struct pcm_feederdesc feeder_vchan_s16_desc[] = {
129 {FEEDER_MIXER, AFMT_S16_LE | AFMT_STEREO, AFMT_S16_LE | AFMT_STEREO, 0},
130 {0},
131 };
132 static kobj_method_t feeder_vchan_s16_methods[] = {
133 KOBJMETHOD(feeder_feed, feed_vchan_s16),
134 { 0, 0 }
135 };
136 FEEDER_DECLARE(feeder_vchan_s16, 2, NULL);
137
138 /************************************************************/
139
140 static void *
141 vchan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
142 {
143 struct vchinfo *ch;
144 struct pcm_channel *parent = devinfo;
145
146 KASSERT(dir == PCMDIR_PLAY, ("vchan_init: bad direction"));
147 ch = malloc(sizeof(*ch), M_DEVBUF, M_WAITOK | M_ZERO);
148 if (!ch)
149 return NULL;
150 ch->parent = parent;
151 ch->channel = c;
152 ch->fmt = AFMT_U8;
153 ch->spd = DSP_DEFAULT_SPEED;
154 ch->blksz = 2048;
155
156 c->flags |= CHN_F_VIRTUAL;
157
158 return ch;
159 }
160
161 static int
162 vchan_free(kobj_t obj, void *data)
163 {
164 return 0;
165 }
166
167 static int
168 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
169 {
170 struct vchinfo *ch = data;
171 struct pcm_channel *parent = ch->parent;
172 struct pcm_channel *channel = ch->channel;
173
174 ch->fmt = format;
175 ch->bps = 1;
176 ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
177 if (ch->fmt & AFMT_16BIT)
178 ch->bps <<= 1;
179 else if (ch->fmt & AFMT_24BIT)
180 ch->bps *= 3;
181 else if (ch->fmt & AFMT_32BIT)
182 ch->bps <<= 2;
183 CHN_UNLOCK(channel);
184 chn_notify(parent, CHN_N_FORMAT);
185 CHN_LOCK(channel);
186 sndbuf_setfmt(channel->bufsoft, format);
187 return 0;
188 }
189
190 static int
191 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
192 {
193 struct vchinfo *ch = data;
194 struct pcm_channel *parent = ch->parent;
195 struct pcm_channel *channel = ch->channel;
196
197 ch->spd = speed;
198 CHN_UNLOCK(channel);
199 CHN_LOCK(parent);
200 speed = sndbuf_getspd(parent->bufsoft);
201 CHN_UNLOCK(parent);
202 CHN_LOCK(channel);
203 return speed;
204 }
205
206 static int
207 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
208 {
209 struct vchinfo *ch = data;
210 struct pcm_channel *channel = ch->channel;
211 struct pcm_channel *parent = ch->parent;
212 /* struct pcm_channel *channel = ch->channel; */
213 int prate, crate;
214
215 ch->blksz = blocksize;
216 /* CHN_UNLOCK(channel); */
217 sndbuf_setblksz(channel->bufhard, blocksize);
218 chn_notify(parent, CHN_N_BLOCKSIZE);
219 CHN_LOCK(parent);
220 /* CHN_LOCK(channel); */
221
222 crate = ch->spd * ch->bps;
223 prate = sndbuf_getspd(parent->bufsoft) * sndbuf_getbps(parent->bufsoft);
224 blocksize = sndbuf_getblksz(parent->bufsoft);
225 CHN_UNLOCK(parent);
226 blocksize *= prate;
227 blocksize /= crate;
228
229 return blocksize;
230 }
231
232 static int
233 vchan_trigger(kobj_t obj, void *data, int go)
234 {
235 struct vchinfo *ch = data;
236 struct pcm_channel *parent = ch->parent;
237 struct pcm_channel *channel = ch->channel;
238
239 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
240 return 0;
241
242 ch->run = (go == PCMTRIG_START)? 1 : 0;
243 CHN_UNLOCK(channel);
244 chn_notify(parent, CHN_N_TRIGGER);
245 CHN_LOCK(channel);
246
247 return 0;
248 }
249
250 static struct pcmchan_caps *
251 vchan_getcaps(kobj_t obj, void *data)
252 {
253 struct vchinfo *ch = data;
254
255 ch->caps.minspeed = sndbuf_getspd(ch->parent->bufsoft);
256 ch->caps.maxspeed = ch->caps.minspeed;
257 ch->caps.fmtlist = vchan_fmt;
258 ch->caps.caps = 0;
259
260 return &ch->caps;
261 }
262
263 static kobj_method_t vchan_methods[] = {
264 KOBJMETHOD(channel_init, vchan_init),
265 KOBJMETHOD(channel_free, vchan_free),
266 KOBJMETHOD(channel_setformat, vchan_setformat),
267 KOBJMETHOD(channel_setspeed, vchan_setspeed),
268 KOBJMETHOD(channel_setblocksize, vchan_setblocksize),
269 KOBJMETHOD(channel_trigger, vchan_trigger),
270 KOBJMETHOD(channel_getcaps, vchan_getcaps),
271 { 0, 0 }
272 };
273 CHANNEL_DECLARE(vchan);
274
275 #if 0
276 /*
277 * On the fly vchan rate settings
278 */
279 #ifdef SND_DYNSYSCTL
280 static int
281 sysctl_hw_snd_vchanrate(SYSCTL_HANDLER_ARGS)
282 {
283 struct snddev_info *d;
284 struct snddev_channel *sce;
285 struct pcm_channel *c, *ch = NULL, *fake;
286 struct pcmchan_caps *caps;
287 int err = 0;
288 int newspd = 0;
289
290 d = oidp->oid_arg1;
291 if (!(d->flags & SD_F_AUTOVCHAN) || d->vchancount < 1)
292 return EINVAL;
293 if (pcm_inprog(d, 1) != 1 && req->newptr != NULL) {
294 pcm_inprog(d, -1);
295 return EINPROGRESS;
296 }
297 SLIST_FOREACH(sce, &d->channels, link) {
298 c = sce->channel;
299 CHN_LOCK(c);
300 if (c->direction == PCMDIR_PLAY) {
301 if (c->flags & CHN_F_VIRTUAL) {
302 /* Sanity check */
303 if (ch != NULL && ch != c->parentchannel) {
304 CHN_UNLOCK(c);
305 pcm_inprog(d, -1);
306 return EINVAL;
307 }
308 if (req->newptr != NULL &&
309 (c->flags & CHN_F_BUSY)) {
310 CHN_UNLOCK(c);
311 pcm_inprog(d, -1);
312 return EBUSY;
313 }
314 } else if (c->flags & CHN_F_HAS_VCHAN) {
315 /* No way!! */
316 if (ch != NULL) {
317 CHN_UNLOCK(c);
318 pcm_inprog(d, -1);
319 return EINVAL;
320 }
321 ch = c;
322 newspd = ch->speed;
323 }
324 }
325 CHN_UNLOCK(c);
326 }
327 if (ch == NULL) {
328 pcm_inprog(d, -1);
329 return EINVAL;
330 }
331 err = sysctl_handle_int(oidp, &newspd, sizeof(newspd), req);
332 if (err == 0 && req->newptr != NULL) {
333 if (newspd < 1 || newspd < feeder_rate_ratemin ||
334 newspd > feeder_rate_ratemax) {
335 pcm_inprog(d, -1);
336 return EINVAL;
337 }
338 CHN_LOCK(ch);
339 caps = chn_getcaps(ch);
340 if (caps == NULL || newspd < caps->minspeed ||
341 newspd > caps->maxspeed) {
342 CHN_UNLOCK(ch);
343 pcm_inprog(d, -1);
344 return EINVAL;
345 }
346 if (newspd != ch->speed) {
347 err = chn_setspeed(ch, newspd);
348 /*
349 * Try to avoid FEEDER_RATE on parent channel if the
350 * requested value is not supported by the hardware.
351 */
352 if (!err && (ch->feederflags & (1 << FEEDER_RATE))) {
353 newspd = sndbuf_getspd(ch->bufhard);
354 err = chn_setspeed(ch, newspd);
355 }
356 CHN_UNLOCK(ch);
357 if (err == 0) {
358 fake = pcm_getfakechan(d);
359 if (fake != NULL) {
360 CHN_LOCK(fake);
361 fake->speed = newspd;
362 CHN_UNLOCK(fake);
363 }
364 }
365 } else
366 CHN_UNLOCK(ch);
367 }
368 pcm_inprog(d, -1);
369 return err;
370 }
371 #endif
372 #endif
373
374 /* virtual channel interface */
375
376 int
377 vchan_create(struct pcm_channel *parent)
378 {
379 struct snddev_info *d = parent->parentsnddev;
380 struct pcmchan_children *pce;
381 struct pcm_channel *child, *fake;
382 struct pcmchan_caps *parent_caps;
383 int err, first, speed = 0;
384
385 if (!(parent->flags & CHN_F_BUSY))
386 return EBUSY;
387
388
389 CHN_UNLOCK(parent);
390
391 pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
392 if (!pce) {
393 CHN_LOCK(parent);
394 return ENOMEM;
395 }
396
397 /* create a new playback channel */
398 child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
399 if (!child) {
400 free(pce, M_DEVBUF);
401 CHN_LOCK(parent);
402 return ENODEV;
403 }
404 pce->channel = child;
405
406 /* add us to our grandparent's channel list */
407 /*
408 * XXX maybe we shouldn't always add the dev_t
409 */
410 err = pcm_chn_add(d, child);
411 if (err) {
412 pcm_chn_destroy(child);
413 free(pce, M_DEVBUF);
414 CHN_LOCK(parent);
415 return err;
416 }
417
418 CHN_LOCK(parent);
419 /* add us to our parent channel's children */
420 first = SLIST_EMPTY(&parent->children);
421 SLIST_INSERT_HEAD(&parent->children, pce, link);
422 parent->flags |= CHN_F_HAS_VCHAN;
423
424 if (first) {
425 parent_caps = chn_getcaps(parent);
426 if (parent_caps == NULL)
427 err = EINVAL;
428
429 if (!err)
430 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
431
432 if (!err) {
433 fake = pcm_getfakechan(d);
434 if (fake != NULL) {
435 /*
436 * Avoid querying kernel hint, use saved value
437 * from fake channel.
438 */
439 CHN_UNLOCK(parent);
440 CHN_LOCK(fake);
441 speed = fake->speed;
442 CHN_UNLOCK(fake);
443 CHN_LOCK(parent);
444 }
445
446 /*
447 * This is very sad. Few soundcards advertised as being
448 * able to do (insanely) higher/lower speed, but in
449 * reality, they simply can't. At least, we give user chance
450 * to set sane value via kernel hints or sysctl.
451 */
452 if (speed < 1) {
453 int r;
454 CHN_UNLOCK(parent);
455 r = resource_int_value(device_get_name(parent->dev),
456 device_get_unit(parent->dev),
457 "vchanrate", &speed);
458 CHN_LOCK(parent);
459 if (r != 0) {
460 /*
461 * Workaround for sb16 running
462 * poorly at 45k / 49k.
463 */
464 switch (parent_caps->maxspeed) {
465 case 45000:
466 case 49000:
467 speed = 44100;
468 break;
469 default:
470 speed = VCHAN_DEFAULT_SPEED;
471 break;
472 }
473 }
474 }
475
476 /*
477 * Limit speed based on driver caps.
478 * This is supposed to help fixed rate, non-VRA
479 * AC97 cards, but.. (see below)
480 */
481 if (speed < parent_caps->minspeed)
482 speed = parent_caps->minspeed;
483 if (speed > parent_caps->maxspeed)
484 speed = parent_caps->maxspeed;
485
486 /*
487 * We still need to limit the speed between
488 * feeder_rate_ratemin <-> feeder_rate_ratemax. This is
489 * just an escape goat if all of the above failed
490 * miserably.
491 */
492 if (speed < feeder_rate_ratemin)
493 speed = feeder_rate_ratemin;
494 if (speed > feeder_rate_ratemax)
495 speed = feeder_rate_ratemax;
496
497 err = chn_setspeed(parent, speed);
498 /*
499 * Try to avoid FEEDER_RATE on parent channel if the
500 * requested value is not supported by the hardware.
501 */
502 if (!err && (parent->feederflags & (1 << FEEDER_RATE))) {
503 speed = sndbuf_getspd(parent->bufhard);
504 err = chn_setspeed(parent, speed);
505 }
506
507 if (!err && fake != NULL) {
508 /*
509 * Save new value to fake channel.
510 */
511 CHN_UNLOCK(parent);
512 CHN_LOCK(fake);
513 fake->speed = speed;
514 CHN_UNLOCK(fake);
515 CHN_LOCK(parent);
516 }
517 }
518
519 if (err) {
520 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
521 parent->flags &= ~CHN_F_HAS_VCHAN;
522 CHN_UNLOCK(parent);
523 free(pce, M_DEVBUF);
524 if (pcm_chn_remove(d, child) == 0)
525 pcm_chn_destroy(child);
526 CHN_LOCK(parent);
527 return err;
528 }
529 }
530
531 return 0;
532 }
533
534 int
535 vchan_destroy(struct pcm_channel *c)
536 {
537 struct pcm_channel *parent = c->parentchannel;
538 struct snddev_info *d = parent->parentsnddev;
539 struct pcmchan_children *pce;
540 struct snddev_channel *sce;
541 uint32_t spd;
542 int err;
543
544 CHN_LOCK(parent);
545 if (!(parent->flags & CHN_F_BUSY)) {
546 CHN_UNLOCK(parent);
547 return EBUSY;
548 }
549 if (SLIST_EMPTY(&parent->children)) {
550 CHN_UNLOCK(parent);
551 return EINVAL;
552 }
553
554 /* remove us from our parent's children list */
555 SLIST_FOREACH(pce, &parent->children, link) {
556 if (pce->channel == c)
557 goto gotch;
558 }
559 CHN_UNLOCK(parent);
560 return EINVAL;
561 gotch:
562 SLIST_FOREACH(sce, &d->channels, link) {
563 if (sce->channel == c) {
564 if (sce->dsp_devt) {
565 destroy_dev(sce->dsp_devt);
566 sce->dsp_devt = NULL;
567 }
568 if (sce->dspW_devt) {
569 destroy_dev(sce->dspW_devt);
570 sce->dspW_devt = NULL;
571 }
572 if (sce->audio_devt) {
573 destroy_dev(sce->audio_devt);
574 sce->audio_devt = NULL;
575 }
576 if (sce->dspr_devt) {
577 destroy_dev(sce->dspr_devt);
578 sce->dspr_devt = NULL;
579 }
580 d->devcount--;
581 break;
582 }
583 }
584 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
585 free(pce, M_DEVBUF);
586
587 if (SLIST_EMPTY(&parent->children)) {
588 parent->flags &= ~(CHN_F_BUSY | CHN_F_HAS_VCHAN);
589 spd = parent->speed;
590 if (chn_reset(parent, parent->format) == 0)
591 chn_setspeed(parent, spd);
592 }
593
594 /* remove us from our grandparent's channel list */
595 err = pcm_chn_remove(d, c);
596
597 CHN_UNLOCK(parent);
598 /* destroy ourselves */
599 if (!err)
600 err = pcm_chn_destroy(c);
601
602 return err;
603 }
604
605 int
606 vchan_initsys(device_t dev)
607 {
608 #ifdef SND_DYNSYSCTL
609 struct snddev_info *d;
610
611 d = device_get_softc(dev);
612 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
613 OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
614 sysctl_hw_snd_vchans, "I", "");
615 #if 0
616 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
617 OID_AUTO, "vchanrate", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
618 sysctl_hw_snd_vchanrate, "I", "");
619 #endif
620 #endif
621
622 return 0;
623 }
Cache object: 15088bdab221d05cee315422e0fdda0d
|