1 /* $FreeBSD: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.15.2.2 2007/05/13 20:53:40 ariff Exp $ */
4 * Copyright (c) 2000-2002 Hiroyuki Aizu <aizu@navi.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
29 #include <sys/soundcard.h>
30 #include <dev/sound/pcm/sound.h>
31 #include <dev/sound/chip.h>
33 #include <dev/sound/usb/uaudio.h>
40 struct ua_info *parent;
41 struct pcm_channel *channel;
42 struct snd_dbuf *buffer;
45 u_int32_t fmt, spd, blksz; /* XXXXX */
51 struct ua_chinfo pch, rch;
53 u_int32_t ua_playfmt[FORMAT_NUM*2+1]; /* FORMAT_NUM format * (stereo or mono) + endptr */
54 u_int32_t ua_recfmt[FORMAT_NUM*2+1]; /* FORMAT_NUM format * (stereo or mono) + endptr */
55 struct pcmchan_caps ua_playcaps;
56 struct pcmchan_caps ua_reccaps;
59 #define UAUDIO_DEFAULT_BUFSZ 16*1024
61 /************************************************************/
63 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
67 struct ua_info *sc = devinfo;
68 struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
75 pa_dev = device_get_parent(sc->sc_dev);
77 ch->buf = kmalloc(sc->bufsz, M_DEVBUF, M_WAITOK);
78 if (sndbuf_setup(b, ch->buf, sc->bufsz) != 0) {
79 kfree(ch->buf, M_DEVBUF);
82 uaudio_chan_set_param_pcm_dma_buff(pa_dev, ch->buf, ch->buf+sc->bufsz, ch->channel, dir);
84 device_printf(pa_dev, "%s buf %p\n", (dir == PCMDIR_PLAY)?
85 "play" : "rec", sndbuf_getbuf(ch->buffer));
90 if (dir == PCMDIR_PLAY)
100 ua_chan_free(kobj_t obj, void *data)
102 struct ua_chinfo *ua = data;
105 kfree(ua->buf, M_DEVBUF);
110 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
115 struct ua_chinfo *ch = data;
118 * At this point, no need to query as we shouldn't select an unsorted format
121 pa_dev = device_get_parent(ua->sc_dev);
122 uaudio_chan_set_param_format(pa_dev, format, ch->dir);
129 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
131 struct ua_chinfo *ch;
136 pa_dev = device_get_parent(ch->parent->sc_dev);
138 if ((bestspeed = uaudio_chan_set_param_speed(pa_dev, speed, ch->dir)))
145 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
148 struct ua_chinfo *ch = data;
149 struct ua_info *ua = ch->parent;
152 RANGE(blocksize, 128, ua->bufsz / 2);
153 if (sndbuf_resize(ch->buffer, ua->bufsz/blocksize, blocksize) != 0) {
154 ch->blksz = blocksize;
157 pa_dev = device_get_parent(ua->sc_dev);
158 uaudio_chan_set_param_blocksize(pa_dev, blocksize, ch->dir);
164 ua_chan_trigger(kobj_t obj, void *data, int go)
168 struct ua_chinfo *ch = data;
170 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
174 pa_dev = device_get_parent(ua->sc_dev);
177 if (ch->dir == PCMDIR_PLAY) {
178 if (go == PCMTRIG_START) {
179 uaudio_trigger_output(pa_dev);
181 uaudio_halt_out_dma(pa_dev);
185 if (go == PCMTRIG_START)
186 uaudio_trigger_input(pa_dev);
188 uaudio_halt_in_dma(pa_dev);
196 ua_chan_getptr(kobj_t obj, void *data)
200 struct ua_chinfo *ch = data;
203 pa_dev = device_get_parent(ua->sc_dev);
205 return uaudio_chan_getptr(pa_dev, ch->dir);
208 static struct pcmchan_caps *
209 ua_chan_getcaps(kobj_t obj, void *data)
211 struct ua_chinfo *ch;
214 return (ch->dir == PCMDIR_PLAY) ? &(ch->parent->ua_playcaps) : &(ch->parent->ua_reccaps);
217 static kobj_method_t ua_chan_methods[] = {
218 KOBJMETHOD(channel_init, ua_chan_init),
219 KOBJMETHOD(channel_free, ua_chan_free),
220 KOBJMETHOD(channel_setformat, ua_chan_setformat),
221 KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
222 KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
223 KOBJMETHOD(channel_trigger, ua_chan_trigger),
224 KOBJMETHOD(channel_getptr, ua_chan_getptr),
225 KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
229 CHANNEL_DECLARE(ua_chan);
231 /************************************************************/
233 ua_mixer_init(struct snd_mixer *m)
237 struct snddev_info *d;
238 struct ua_info *ua = mix_getdevinfo(m);
240 pa_dev = device_get_parent(ua->sc_dev);
241 d = device_get_softc(ua->sc_dev);
243 mask = uaudio_query_mix_info(pa_dev);
245 if (!(mask & SOUND_MASK_PCM)) {
247 * Emulate missing pcm mixer controller
248 * through FEEDER_VOLUME
250 d->flags |= SD_F_SOFTPCMVOL;
252 if (!(mask & SOUND_MASK_VOLUME)) {
253 mix_setparentchild(m, SOUND_MIXER_VOLUME,
255 mix_setrealdev(m, SOUND_MIXER_VOLUME,
259 mix_setdevs(m, mask);
261 mask = uaudio_query_recsrc_info(pa_dev);
262 mix_setrecdevs(m, mask);
268 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
271 struct ua_info *ua = mix_getdevinfo(m);
273 pa_dev = device_get_parent(ua->sc_dev);
274 uaudio_mixer_set(pa_dev, type, left, right);
276 return left | (right << 8);
280 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
283 struct ua_info *ua = mix_getdevinfo(m);
285 pa_dev = device_get_parent(ua->sc_dev);
286 return uaudio_mixer_setrecsrc(pa_dev, src);
289 static kobj_method_t ua_mixer_methods[] = {
290 KOBJMETHOD(mixer_init, ua_mixer_init),
291 KOBJMETHOD(mixer_set, ua_mixer_set),
292 KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
296 MIXER_DECLARE(ua_mixer);
297 /************************************************************/
301 ua_probe(device_t dev)
304 struct sndcard_func *func;
306 /* The parent device has already been probed. */
308 func = device_get_ivars(dev);
309 if (func == NULL || func->func != SCF_PCM)
314 device_set_desc(dev, s);
315 return BUS_PROBE_DEFAULT;
319 ua_attach(device_t dev)
322 char status[SND_STATUSLEN];
324 u_int32_t nplay, nrec;
327 ua = kmalloc(sizeof *ua, M_DEVBUF, M_ZERO | M_WAITOK);
331 pa_dev = device_get_parent(dev);
333 ua->bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_DEFAULT_BUFSZ, 65536);
335 device_printf(dev, "using a default buffer size of %jd\n", (intmax_t)ua->bufsz);
337 if (mixer_init(dev, &ua_mixer_class, ua)) {
341 ksnprintf(status, SND_STATUSLEN, "at ? %s", PCM_KLDSTRING(snd_uaudio));
343 ua->ua_playcaps.fmtlist = ua->ua_playfmt;
344 ua->ua_reccaps.fmtlist = ua->ua_recfmt;
345 nplay = uaudio_query_formats(pa_dev, PCMDIR_PLAY, FORMAT_NUM * 2, &ua->ua_playcaps);
346 nrec = uaudio_query_formats(pa_dev, PCMDIR_REC, FORMAT_NUM * 2, &ua->ua_reccaps);
354 if (pcm_register(dev, ua, nplay, nrec)) {
356 if (pcm_register(dev, ua, nplay, 0)) {
361 sndstat_unregister(dev);
362 uaudio_sndstat_register(dev);
364 for (i = 0; i < nplay; i++) {
365 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
368 for (i = 0; i < nrec; i++) {
369 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
372 pcm_setstatus(dev, status);
376 bad: kfree(ua, M_DEVBUF);
381 ua_detach(device_t dev)
386 r = pcm_unregister(dev);
390 sc = pcm_getdevinfo(dev);
396 /************************************************************/
398 static device_method_t ua_pcm_methods[] = {
399 /* Device interface */
400 DEVMETHOD(device_probe, ua_probe),
401 DEVMETHOD(device_attach, ua_attach),
402 DEVMETHOD(device_detach, ua_detach),
407 static driver_t ua_pcm_driver = {
414 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, NULL, NULL);
415 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
416 MODULE_DEPEND(ua_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
417 MODULE_VERSION(ua_pcm, 1);