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.4/sys/dev/sound/pcm/vchan.c 166482 2007-02-04 06:17:14Z 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 free(data, M_DEVBUF);
165 return 0;
166 }
167
168 static int
169 vchan_setformat(kobj_t obj, void *data, u_int32_t format)
170 {
171 struct vchinfo *ch = data;
172 struct pcm_channel *parent = ch->parent;
173 struct pcm_channel *channel = ch->channel;
174
175 ch->fmt = format;
176 ch->bps = 1;
177 ch->bps <<= (ch->fmt & AFMT_STEREO)? 1 : 0;
178 if (ch->fmt & AFMT_16BIT)
179 ch->bps <<= 1;
180 else if (ch->fmt & AFMT_24BIT)
181 ch->bps *= 3;
182 else if (ch->fmt & AFMT_32BIT)
183 ch->bps <<= 2;
184 CHN_UNLOCK(channel);
185 chn_notify(parent, CHN_N_FORMAT);
186 CHN_LOCK(channel);
187 sndbuf_setfmt(channel->bufsoft, format);
188 return 0;
189 }
190
191 static int
192 vchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
193 {
194 struct vchinfo *ch = data;
195 struct pcm_channel *parent = ch->parent;
196 struct pcm_channel *channel = ch->channel;
197
198 ch->spd = speed;
199 CHN_UNLOCK(channel);
200 CHN_LOCK(parent);
201 speed = sndbuf_getspd(parent->bufsoft);
202 CHN_UNLOCK(parent);
203 CHN_LOCK(channel);
204 return speed;
205 }
206
207 static int
208 vchan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
209 {
210 struct vchinfo *ch = data;
211 struct pcm_channel *channel = ch->channel;
212 struct pcm_channel *parent = ch->parent;
213 /* struct pcm_channel *channel = ch->channel; */
214 int prate, crate;
215
216 ch->blksz = blocksize;
217 /* CHN_UNLOCK(channel); */
218 sndbuf_setblksz(channel->bufhard, blocksize);
219 chn_notify(parent, CHN_N_BLOCKSIZE);
220 CHN_LOCK(parent);
221 /* CHN_LOCK(channel); */
222
223 crate = ch->spd * ch->bps;
224 prate = sndbuf_getspd(parent->bufsoft) * sndbuf_getbps(parent->bufsoft);
225 blocksize = sndbuf_getblksz(parent->bufsoft);
226 CHN_UNLOCK(parent);
227 blocksize *= prate;
228 blocksize /= crate;
229
230 return blocksize;
231 }
232
233 static int
234 vchan_trigger(kobj_t obj, void *data, int go)
235 {
236 struct vchinfo *ch = data;
237 struct pcm_channel *parent = ch->parent;
238 struct pcm_channel *channel = ch->channel;
239
240 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
241 return 0;
242
243 ch->run = (go == PCMTRIG_START)? 1 : 0;
244 CHN_UNLOCK(channel);
245 chn_notify(parent, CHN_N_TRIGGER);
246 CHN_LOCK(channel);
247
248 return 0;
249 }
250
251 static struct pcmchan_caps *
252 vchan_getcaps(kobj_t obj, void *data)
253 {
254 struct vchinfo *ch = data;
255
256 ch->caps.minspeed = sndbuf_getspd(ch->parent->bufsoft);
257 ch->caps.maxspeed = ch->caps.minspeed;
258 ch->caps.fmtlist = vchan_fmt;
259 ch->caps.caps = 0;
260
261 return &ch->caps;
262 }
263
264 static kobj_method_t vchan_methods[] = {
265 KOBJMETHOD(channel_init, vchan_init),
266 KOBJMETHOD(channel_free, vchan_free),
267 KOBJMETHOD(channel_setformat, vchan_setformat),
268 KOBJMETHOD(channel_setspeed, vchan_setspeed),
269 KOBJMETHOD(channel_setblocksize, vchan_setblocksize),
270 KOBJMETHOD(channel_trigger, vchan_trigger),
271 KOBJMETHOD(channel_getcaps, vchan_getcaps),
272 { 0, 0 }
273 };
274 CHANNEL_DECLARE(vchan);
275
276 #if 0
277 /*
278 * On the fly vchan rate settings
279 */
280 #ifdef SND_DYNSYSCTL
281 static int
282 sysctl_hw_snd_vchanrate(SYSCTL_HANDLER_ARGS)
283 {
284 struct snddev_info *d;
285 struct snddev_channel *sce;
286 struct pcm_channel *c, *ch = NULL, *fake;
287 struct pcmchan_caps *caps;
288 int err = 0;
289 int newspd = 0;
290
291 d = oidp->oid_arg1;
292 if (!(d->flags & SD_F_AUTOVCHAN) || d->vchancount < 1)
293 return EINVAL;
294 if (pcm_inprog(d, 1) != 1 && req->newptr != NULL) {
295 pcm_inprog(d, -1);
296 return EINPROGRESS;
297 }
298 SLIST_FOREACH(sce, &d->channels, link) {
299 c = sce->channel;
300 CHN_LOCK(c);
301 if (c->direction == PCMDIR_PLAY) {
302 if (c->flags & CHN_F_VIRTUAL) {
303 /* Sanity check */
304 if (ch != NULL && ch != c->parentchannel) {
305 CHN_UNLOCK(c);
306 pcm_inprog(d, -1);
307 return EINVAL;
308 }
309 if (req->newptr != NULL &&
310 (c->flags & CHN_F_BUSY)) {
311 CHN_UNLOCK(c);
312 pcm_inprog(d, -1);
313 return EBUSY;
314 }
315 } else if (c->flags & CHN_F_HAS_VCHAN) {
316 /* No way!! */
317 if (ch != NULL) {
318 CHN_UNLOCK(c);
319 pcm_inprog(d, -1);
320 return EINVAL;
321 }
322 ch = c;
323 newspd = ch->speed;
324 }
325 }
326 CHN_UNLOCK(c);
327 }
328 if (ch == NULL) {
329 pcm_inprog(d, -1);
330 return EINVAL;
331 }
332 err = sysctl_handle_int(oidp, &newspd, sizeof(newspd), req);
333 if (err == 0 && req->newptr != NULL) {
334 if (newspd < 1 || newspd < feeder_rate_ratemin ||
335 newspd > feeder_rate_ratemax) {
336 pcm_inprog(d, -1);
337 return EINVAL;
338 }
339 CHN_LOCK(ch);
340 caps = chn_getcaps(ch);
341 if (caps == NULL || newspd < caps->minspeed ||
342 newspd > caps->maxspeed) {
343 CHN_UNLOCK(ch);
344 pcm_inprog(d, -1);
345 return EINVAL;
346 }
347 if (newspd != ch->speed) {
348 err = chn_setspeed(ch, newspd);
349 /*
350 * Try to avoid FEEDER_RATE on parent channel if the
351 * requested value is not supported by the hardware.
352 */
353 if (!err && (ch->feederflags & (1 << FEEDER_RATE))) {
354 newspd = sndbuf_getspd(ch->bufhard);
355 err = chn_setspeed(ch, newspd);
356 }
357 CHN_UNLOCK(ch);
358 if (err == 0) {
359 fake = pcm_getfakechan(d);
360 if (fake != NULL) {
361 CHN_LOCK(fake);
362 fake->speed = newspd;
363 CHN_UNLOCK(fake);
364 }
365 }
366 } else
367 CHN_UNLOCK(ch);
368 }
369 pcm_inprog(d, -1);
370 return err;
371 }
372 #endif
373 #endif
374
375 /* virtual channel interface */
376
377 int
378 vchan_create(struct pcm_channel *parent)
379 {
380 struct snddev_info *d = parent->parentsnddev;
381 struct pcmchan_children *pce;
382 struct pcm_channel *child, *fake;
383 struct pcmchan_caps *parent_caps;
384 int err, first, speed = 0;
385
386 if (!(parent->flags & CHN_F_BUSY))
387 return EBUSY;
388
389
390 CHN_UNLOCK(parent);
391
392 pce = malloc(sizeof(*pce), M_DEVBUF, M_WAITOK | M_ZERO);
393 if (!pce) {
394 CHN_LOCK(parent);
395 return ENOMEM;
396 }
397
398 /* create a new playback channel */
399 child = pcm_chn_create(d, parent, &vchan_class, PCMDIR_VIRTUAL, parent);
400 if (!child) {
401 free(pce, M_DEVBUF);
402 CHN_LOCK(parent);
403 return ENODEV;
404 }
405 pce->channel = child;
406
407 /* add us to our grandparent's channel list */
408 /*
409 * XXX maybe we shouldn't always add the dev_t
410 */
411 err = pcm_chn_add(d, child);
412 if (err) {
413 pcm_chn_destroy(child);
414 free(pce, M_DEVBUF);
415 CHN_LOCK(parent);
416 return err;
417 }
418
419 CHN_LOCK(parent);
420 /* add us to our parent channel's children */
421 first = SLIST_EMPTY(&parent->children);
422 SLIST_INSERT_HEAD(&parent->children, pce, link);
423 parent->flags |= CHN_F_HAS_VCHAN;
424
425 if (first) {
426 parent_caps = chn_getcaps(parent);
427 if (parent_caps == NULL)
428 err = EINVAL;
429
430 if (!err)
431 err = chn_reset(parent, AFMT_STEREO | AFMT_S16_LE);
432
433 if (!err) {
434 fake = pcm_getfakechan(d);
435 if (fake != NULL) {
436 /*
437 * Avoid querying kernel hint, use saved value
438 * from fake channel.
439 */
440 CHN_UNLOCK(parent);
441 CHN_LOCK(fake);
442 speed = fake->speed;
443 CHN_UNLOCK(fake);
444 CHN_LOCK(parent);
445 }
446
447 /*
448 * This is very sad. Few soundcards advertised as being
449 * able to do (insanely) higher/lower speed, but in
450 * reality, they simply can't. At least, we give user chance
451 * to set sane value via kernel hints or sysctl.
452 */
453 if (speed < 1) {
454 int r;
455 CHN_UNLOCK(parent);
456 r = resource_int_value(device_get_name(parent->dev),
457 device_get_unit(parent->dev),
458 "vchanrate", &speed);
459 CHN_LOCK(parent);
460 if (r != 0) {
461 /*
462 * Workaround for sb16 running
463 * poorly at 45k / 49k.
464 */
465 switch (parent_caps->maxspeed) {
466 case 45000:
467 case 49000:
468 speed = 44100;
469 break;
470 default:
471 speed = VCHAN_DEFAULT_SPEED;
472 break;
473 }
474 }
475 }
476
477 /*
478 * Limit speed based on driver caps.
479 * This is supposed to help fixed rate, non-VRA
480 * AC97 cards, but.. (see below)
481 */
482 if (speed < parent_caps->minspeed)
483 speed = parent_caps->minspeed;
484 if (speed > parent_caps->maxspeed)
485 speed = parent_caps->maxspeed;
486
487 /*
488 * We still need to limit the speed between
489 * feeder_rate_ratemin <-> feeder_rate_ratemax. This is
490 * just an escape goat if all of the above failed
491 * miserably.
492 */
493 if (speed < feeder_rate_ratemin)
494 speed = feeder_rate_ratemin;
495 if (speed > feeder_rate_ratemax)
496 speed = feeder_rate_ratemax;
497
498 err = chn_setspeed(parent, speed);
499 /*
500 * Try to avoid FEEDER_RATE on parent channel if the
501 * requested value is not supported by the hardware.
502 */
503 if (!err && (parent->feederflags & (1 << FEEDER_RATE))) {
504 speed = sndbuf_getspd(parent->bufhard);
505 err = chn_setspeed(parent, speed);
506 }
507
508 if (!err && fake != NULL) {
509 /*
510 * Save new value to fake channel.
511 */
512 CHN_UNLOCK(parent);
513 CHN_LOCK(fake);
514 fake->speed = speed;
515 CHN_UNLOCK(fake);
516 CHN_LOCK(parent);
517 }
518 }
519
520 if (err) {
521 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
522 parent->flags &= ~CHN_F_HAS_VCHAN;
523 CHN_UNLOCK(parent);
524 free(pce, M_DEVBUF);
525 if (pcm_chn_remove(d, child) == 0)
526 pcm_chn_destroy(child);
527 CHN_LOCK(parent);
528 return err;
529 }
530 }
531
532 return 0;
533 }
534
535 int
536 vchan_destroy(struct pcm_channel *c)
537 {
538 struct pcm_channel *parent = c->parentchannel;
539 struct snddev_info *d = parent->parentsnddev;
540 struct pcmchan_children *pce;
541 struct snddev_channel *sce;
542 uint32_t spd;
543 int err;
544
545 CHN_LOCK(parent);
546 if (!(parent->flags & CHN_F_BUSY)) {
547 CHN_UNLOCK(parent);
548 return EBUSY;
549 }
550 if (SLIST_EMPTY(&parent->children)) {
551 CHN_UNLOCK(parent);
552 return EINVAL;
553 }
554
555 /* remove us from our parent's children list */
556 SLIST_FOREACH(pce, &parent->children, link) {
557 if (pce->channel == c)
558 goto gotch;
559 }
560 CHN_UNLOCK(parent);
561 return EINVAL;
562 gotch:
563 SLIST_FOREACH(sce, &d->channels, link) {
564 if (sce->channel == c) {
565 if (sce->dsp_devt) {
566 destroy_dev(sce->dsp_devt);
567 sce->dsp_devt = NULL;
568 }
569 if (sce->dspW_devt) {
570 destroy_dev(sce->dspW_devt);
571 sce->dspW_devt = NULL;
572 }
573 if (sce->audio_devt) {
574 destroy_dev(sce->audio_devt);
575 sce->audio_devt = NULL;
576 }
577 if (sce->dspr_devt) {
578 destroy_dev(sce->dspr_devt);
579 sce->dspr_devt = NULL;
580 }
581 d->devcount--;
582 break;
583 }
584 }
585 SLIST_REMOVE(&parent->children, pce, pcmchan_children, link);
586 free(pce, M_DEVBUF);
587
588 if (SLIST_EMPTY(&parent->children)) {
589 parent->flags &= ~(CHN_F_BUSY | CHN_F_HAS_VCHAN);
590 spd = parent->speed;
591 if (chn_reset(parent, parent->format) == 0)
592 chn_setspeed(parent, spd);
593 }
594
595 /* remove us from our grandparent's channel list */
596 err = pcm_chn_remove(d, c);
597
598 CHN_UNLOCK(parent);
599 /* destroy ourselves */
600 if (!err)
601 err = pcm_chn_destroy(c);
602
603 return err;
604 }
605
606 int
607 vchan_initsys(device_t dev)
608 {
609 #ifdef SND_DYNSYSCTL
610 struct snddev_info *d;
611
612 d = device_get_softc(dev);
613 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
614 OID_AUTO, "vchans", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
615 sysctl_hw_snd_vchans, "I", "");
616 #if 0
617 SYSCTL_ADD_PROC(snd_sysctl_tree(dev), SYSCTL_CHILDREN(snd_sysctl_tree_top(dev)),
618 OID_AUTO, "vchanrate", CTLTYPE_INT | CTLFLAG_RW, d, sizeof(d),
619 sysctl_hw_snd_vchanrate, "I", "");
620 #endif
621 #endif
622
623 return 0;
624 }
Cache object: cb8cd15e65517fb2ec6f16a0d33014c3
|