1 /* $FreeBSD: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.15.2.2 2007/05/13 20:53:40 ariff Exp $ */
2 /* $DragonFly: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.8 2007/06/16 19:48:05 hasso Exp $ */
5 * Copyright (c) 2000-2002 Hiroyuki Aizu <aizu@navi.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/soundcard.h>
31 #include <dev/sound/pcm/sound.h>
32 #include <dev/sound/chip.h>
34 #include <dev/sound/usb/uaudio.h>
41 struct ua_info *parent;
42 struct pcm_channel *channel;
43 struct snd_dbuf *buffer;
46 u_int32_t fmt, spd, blksz; /* XXXXX */
52 struct ua_chinfo pch, rch;
54 u_int32_t ua_playfmt[FORMAT_NUM*2+1]; /* FORMAT_NUM format * (stereo or mono) + endptr */
55 u_int32_t ua_recfmt[FORMAT_NUM*2+1]; /* FORMAT_NUM format * (stereo or mono) + endptr */
56 struct pcmchan_caps ua_playcaps;
57 struct pcmchan_caps ua_reccaps;
60 #define UAUDIO_DEFAULT_BUFSZ 16*1024
62 /************************************************************/
64 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
68 struct ua_info *sc = devinfo;
69 struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
76 pa_dev = device_get_parent(sc->sc_dev);
78 ch->buf = kmalloc(sc->bufsz, M_DEVBUF, M_WAITOK);
79 if (sndbuf_setup(b, ch->buf, sc->bufsz) != 0) {
80 kfree(ch->buf, M_DEVBUF);
83 uaudio_chan_set_param_pcm_dma_buff(pa_dev, ch->buf, ch->buf+sc->bufsz, ch->channel, dir);
85 device_printf(pa_dev, "%s buf %p\n", (dir == PCMDIR_PLAY)?
86 "play" : "rec", sndbuf_getbuf(ch->buffer));
91 if (dir == PCMDIR_PLAY)
101 ua_chan_free(kobj_t obj, void *data)
103 struct ua_chinfo *ua = data;
106 kfree(ua->buf, M_DEVBUF);
111 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
116 struct ua_chinfo *ch = data;
119 * At this point, no need to query as we shouldn't select an unsorted format
122 pa_dev = device_get_parent(ua->sc_dev);
123 uaudio_chan_set_param_format(pa_dev, format, ch->dir);
130 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
132 struct ua_chinfo *ch;
137 pa_dev = device_get_parent(ch->parent->sc_dev);
139 if ((bestspeed = uaudio_chan_set_param_speed(pa_dev, speed, ch->dir)))
146 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
149 struct ua_chinfo *ch = data;
150 struct ua_info *ua = ch->parent;
153 RANGE(blocksize, 128, ua->bufsz / 2);
154 if (sndbuf_resize(ch->buffer, ua->bufsz/blocksize, blocksize) != 0) {
155 ch->blksz = blocksize;
158 pa_dev = device_get_parent(ua->sc_dev);
159 uaudio_chan_set_param_blocksize(pa_dev, blocksize, ch->dir);
165 ua_chan_trigger(kobj_t obj, void *data, int go)
169 struct ua_chinfo *ch = data;
171 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
175 pa_dev = device_get_parent(ua->sc_dev);
178 if (ch->dir == PCMDIR_PLAY) {
179 if (go == PCMTRIG_START) {
180 uaudio_trigger_output(pa_dev);
182 uaudio_halt_out_dma(pa_dev);
186 if (go == PCMTRIG_START)
187 uaudio_trigger_input(pa_dev);
189 uaudio_halt_in_dma(pa_dev);
197 ua_chan_getptr(kobj_t obj, void *data)
201 struct ua_chinfo *ch = data;
204 pa_dev = device_get_parent(ua->sc_dev);
206 return uaudio_chan_getptr(pa_dev, ch->dir);
209 static struct pcmchan_caps *
210 ua_chan_getcaps(kobj_t obj, void *data)
212 struct ua_chinfo *ch;
215 return (ch->dir == PCMDIR_PLAY) ? &(ch->parent->ua_playcaps) : &(ch->parent->ua_reccaps);
218 static kobj_method_t ua_chan_methods[] = {
219 KOBJMETHOD(channel_init, ua_chan_init),
220 KOBJMETHOD(channel_free, ua_chan_free),
221 KOBJMETHOD(channel_setformat, ua_chan_setformat),
222 KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
223 KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
224 KOBJMETHOD(channel_trigger, ua_chan_trigger),
225 KOBJMETHOD(channel_getptr, ua_chan_getptr),
226 KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
230 CHANNEL_DECLARE(ua_chan);
232 /************************************************************/
234 ua_mixer_init(struct snd_mixer *m)
238 struct snddev_info *d;
239 struct ua_info *ua = mix_getdevinfo(m);
241 pa_dev = device_get_parent(ua->sc_dev);
242 d = device_get_softc(ua->sc_dev);
244 mask = uaudio_query_mix_info(pa_dev);
246 if (!(mask & SOUND_MASK_PCM)) {
248 * Emulate missing pcm mixer controller
249 * through FEEDER_VOLUME
251 d->flags |= SD_F_SOFTPCMVOL;
253 if (!(mask & SOUND_MASK_VOLUME)) {
254 mix_setparentchild(m, SOUND_MIXER_VOLUME,
256 mix_setrealdev(m, SOUND_MIXER_VOLUME,
260 mix_setdevs(m, mask);
262 mask = uaudio_query_recsrc_info(pa_dev);
263 mix_setrecdevs(m, mask);
269 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
272 struct ua_info *ua = mix_getdevinfo(m);
274 pa_dev = device_get_parent(ua->sc_dev);
275 uaudio_mixer_set(pa_dev, type, left, right);
277 return left | (right << 8);
281 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
284 struct ua_info *ua = mix_getdevinfo(m);
286 pa_dev = device_get_parent(ua->sc_dev);
287 return uaudio_mixer_setrecsrc(pa_dev, src);
290 static kobj_method_t ua_mixer_methods[] = {
291 KOBJMETHOD(mixer_init, ua_mixer_init),
292 KOBJMETHOD(mixer_set, ua_mixer_set),
293 KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
297 MIXER_DECLARE(ua_mixer);
298 /************************************************************/
302 ua_probe(device_t dev)
305 struct sndcard_func *func;
307 /* The parent device has already been probed. */
309 func = device_get_ivars(dev);
310 if (func == NULL || func->func != SCF_PCM)
315 device_set_desc(dev, s);
316 return BUS_PROBE_DEFAULT;
320 ua_attach(device_t dev)
323 char status[SND_STATUSLEN];
325 u_int32_t nplay, nrec;
328 ua = kmalloc(sizeof *ua, M_DEVBUF, M_ZERO | M_WAITOK);
332 pa_dev = device_get_parent(dev);
334 ua->bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_DEFAULT_BUFSZ, 65536);
336 device_printf(dev, "using a default buffer size of %jd\n", (intmax_t)ua->bufsz);
338 if (mixer_init(dev, &ua_mixer_class, ua)) {
342 ksnprintf(status, SND_STATUSLEN, "at ? %s", PCM_KLDSTRING(snd_uaudio));
344 ua->ua_playcaps.fmtlist = ua->ua_playfmt;
345 ua->ua_reccaps.fmtlist = ua->ua_recfmt;
346 nplay = uaudio_query_formats(pa_dev, PCMDIR_PLAY, FORMAT_NUM * 2, &ua->ua_playcaps);
347 nrec = uaudio_query_formats(pa_dev, PCMDIR_REC, FORMAT_NUM * 2, &ua->ua_reccaps);
355 if (pcm_register(dev, ua, nplay, nrec)) {
357 if (pcm_register(dev, ua, nplay, 0)) {
362 sndstat_unregister(dev);
363 uaudio_sndstat_register(dev);
365 for (i = 0; i < nplay; i++) {
366 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
369 for (i = 0; i < nrec; i++) {
370 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
373 pcm_setstatus(dev, status);
377 bad: kfree(ua, M_DEVBUF);
382 ua_detach(device_t dev)
387 r = pcm_unregister(dev);
391 sc = pcm_getdevinfo(dev);
397 /************************************************************/
399 static device_method_t ua_pcm_methods[] = {
400 /* Device interface */
401 DEVMETHOD(device_probe, ua_probe),
402 DEVMETHOD(device_attach, ua_attach),
403 DEVMETHOD(device_detach, ua_detach),
408 static driver_t ua_pcm_driver = {
415 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, 0, 0);
416 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
417 MODULE_DEPEND(ua_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
418 MODULE_VERSION(ua_pcm, 1);