1 /* $FreeBSD: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.1.2.1 2002/08/24 08:03:07 nsayer 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;
44 u_int32_t fmt, spd, blksz; /* XXXXX */
49 struct ua_chinfo pch, rch;
50 bus_dma_tag_t parent_dmat;
53 static u_int32_t ua_playfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
55 static struct pcmchan_caps ua_playcaps = {8000, 48000, ua_playfmt, 0};
57 static u_int32_t ua_recfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
59 static struct pcmchan_caps ua_reccaps = {8000, 48000, ua_recfmt, 0};
61 #define UAUDIO_PCM_BUFF_SIZE 16*1024
63 /************************************************************/
65 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
70 struct ua_info *sc = devinfo;
71 struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
77 /* allocate PCM side DMA buffer */
78 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, UAUDIO_PCM_BUFF_SIZE) != 0) {
82 pa_dev = device_get_parent(sc->sc_dev);
83 buf = end = sndbuf_getbuf(b);
84 end += sndbuf_getsize(b);
85 uaudio_chan_set_param_pcm_dma_buff(pa_dev, buf, end, ch->channel);
87 /* Create ua_playfmt[] & ua_recfmt[] */
88 uaudio_query_formats(pa_dev, (u_int32_t *)&ua_playfmt, (u_int32_t *)&ua_recfmt);
93 if (dir == PCMDIR_PLAY)
103 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
108 struct ua_chinfo *ch = data;
111 pa_dev = device_get_parent(ua->sc_dev);
112 uaudio_chan_set_param_format(pa_dev, format);
119 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
124 struct ua_chinfo *ch = data;
128 pa_dev = device_get_parent(ua->sc_dev);
129 uaudio_chan_set_param_speed(pa_dev, speed);
135 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
139 struct ua_chinfo *ch = data;
140 /* ch->blksz = blocksize; */
142 ch->blksz = blocksize;
144 ch->blksz = UAUDIO_PCM_BUFF_SIZE/2;
149 pa_dev = device_get_parent(ua->sc_dev);
150 uaudio_chan_set_param_blocksize(pa_dev, blocksize);
156 ua_chan_trigger(kobj_t obj, void *data, int go)
160 struct ua_chinfo *ch = data;
162 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
166 pa_dev = device_get_parent(ua->sc_dev);
169 if (ch->dir == PCMDIR_PLAY) {
170 if (go == PCMTRIG_START) {
171 uaudio_trigger_output(pa_dev);
173 uaudio_halt_out_dma(pa_dev);
177 if (go == PCMTRIG_START)
178 uaudio_trigger_input(pa_dev);
180 uaudio_halt_in_dma(pa_dev);
188 ua_chan_getptr(kobj_t obj, void *data)
192 struct ua_chinfo *ch = data;
195 pa_dev = device_get_parent(ua->sc_dev);
197 return uaudio_chan_getptr(pa_dev);
200 static struct pcmchan_caps *
201 ua_chan_getcaps(kobj_t obj, void *data)
203 struct ua_chinfo *ch = data;
205 return (ch->dir == PCMDIR_PLAY) ? &ua_playcaps : & ua_reccaps;
208 static kobj_method_t ua_chan_methods[] = {
209 KOBJMETHOD(channel_init, ua_chan_init),
210 KOBJMETHOD(channel_setformat, ua_chan_setformat),
211 KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
212 KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
213 KOBJMETHOD(channel_trigger, ua_chan_trigger),
214 KOBJMETHOD(channel_getptr, ua_chan_getptr),
215 KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
219 CHANNEL_DECLARE(ua_chan);
221 /************************************************************/
223 ua_mixer_init(struct snd_mixer *m)
227 struct ua_info *ua = mix_getdevinfo(m);
229 pa_dev = device_get_parent(ua->sc_dev);
231 mask = uaudio_query_mix_info(pa_dev);
232 mix_setdevs(m, mask);
238 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
241 struct ua_info *ua = mix_getdevinfo(m);
243 pa_dev = device_get_parent(ua->sc_dev);
244 uaudio_mixer_set(pa_dev, type, left, right);
246 return left | (right << 8);
250 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
255 static kobj_method_t ua_mixer_methods[] = {
256 KOBJMETHOD(mixer_init, ua_mixer_init),
257 KOBJMETHOD(mixer_set, ua_mixer_set),
258 KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
262 MIXER_DECLARE(ua_mixer);
263 /************************************************************/
267 ua_probe(device_t dev)
270 struct sndcard_func *func;
272 /* The parent device has already been probed. */
274 func = device_get_ivars(dev);
275 if (func == NULL || func->func != SCF_PCM)
280 device_set_desc(dev, s);
285 ua_attach(device_t dev)
288 char status[SND_STATUSLEN];
291 ua = (struct ua_info *)malloc(sizeof *ua, M_DEVBUF, M_NOWAIT);
294 bzero(ua, sizeof *ua);
298 bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_PCM_BUFF_SIZE, 65536);
300 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
301 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
302 /*highaddr*/BUS_SPACE_MAXADDR,
303 /*filter*/NULL, /*filterarg*/NULL,
304 /*maxsize*/bufsz, /*nsegments*/1,
305 /*maxsegz*/0x3fff, /*flags*/0,
306 &ua->parent_dmat) != 0) {
307 device_printf(dev, "unable to create dma tag\n");
311 if (mixer_init(dev, &ua_mixer_class, ua)) {
315 snprintf(status, SND_STATUSLEN, "at addr ?");
317 if (pcm_register(dev, ua, 1, 0)) {
321 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
323 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
325 pcm_setstatus(dev, status);
330 bus_dma_tag_destroy(ua->parent_dmat);
337 ua_detach(device_t dev)
342 r = pcm_unregister(dev);
346 sc = pcm_getdevinfo(dev);
347 bus_dma_tag_destroy(sc->parent_dmat);
353 /************************************************************/
355 static device_method_t ua_pcm_methods[] = {
356 /* Device interface */
357 DEVMETHOD(device_probe, ua_probe),
358 DEVMETHOD(device_attach, ua_attach),
359 DEVMETHOD(device_detach, ua_detach),
364 static driver_t ua_pcm_driver = {
370 static devclass_t pcm_devclass;
372 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, 0, 0);
373 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
374 MODULE_DEPEND(ua_pcm, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
375 MODULE_VERSION(ua_pcm, 1);