1 /* $FreeBSD: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.1.2.1 2002/08/24 08:03:07 nsayer Exp $ */
2 /* $DragonFly: src/sys/dev/sound/usb/uaudio_pcm.c,v 1.2 2003/06/17 04:28:31 dillon 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;
45 u_int32_t fmt, spd, blksz; /* XXXXX */
50 struct ua_chinfo pch, rch;
51 bus_dma_tag_t parent_dmat;
54 static u_int32_t ua_playfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
56 static struct pcmchan_caps ua_playcaps = {8000, 48000, ua_playfmt, 0};
58 static u_int32_t ua_recfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
60 static struct pcmchan_caps ua_reccaps = {8000, 48000, ua_recfmt, 0};
62 #define UAUDIO_PCM_BUFF_SIZE 16*1024
64 /************************************************************/
66 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
71 struct ua_info *sc = devinfo;
72 struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
78 /* allocate PCM side DMA buffer */
79 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, UAUDIO_PCM_BUFF_SIZE) != 0) {
83 pa_dev = device_get_parent(sc->sc_dev);
84 buf = end = sndbuf_getbuf(b);
85 end += sndbuf_getsize(b);
86 uaudio_chan_set_param_pcm_dma_buff(pa_dev, buf, end, ch->channel);
88 /* Create ua_playfmt[] & ua_recfmt[] */
89 uaudio_query_formats(pa_dev, (u_int32_t *)&ua_playfmt, (u_int32_t *)&ua_recfmt);
94 if (dir == PCMDIR_PLAY)
104 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
109 struct ua_chinfo *ch = data;
112 pa_dev = device_get_parent(ua->sc_dev);
113 uaudio_chan_set_param_format(pa_dev, format);
120 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
125 struct ua_chinfo *ch = data;
129 pa_dev = device_get_parent(ua->sc_dev);
130 uaudio_chan_set_param_speed(pa_dev, speed);
136 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
140 struct ua_chinfo *ch = data;
141 /* ch->blksz = blocksize; */
143 ch->blksz = blocksize;
145 ch->blksz = UAUDIO_PCM_BUFF_SIZE/2;
150 pa_dev = device_get_parent(ua->sc_dev);
151 uaudio_chan_set_param_blocksize(pa_dev, blocksize);
157 ua_chan_trigger(kobj_t obj, void *data, int go)
161 struct ua_chinfo *ch = data;
163 if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
167 pa_dev = device_get_parent(ua->sc_dev);
170 if (ch->dir == PCMDIR_PLAY) {
171 if (go == PCMTRIG_START) {
172 uaudio_trigger_output(pa_dev);
174 uaudio_halt_out_dma(pa_dev);
178 if (go == PCMTRIG_START)
179 uaudio_trigger_input(pa_dev);
181 uaudio_halt_in_dma(pa_dev);
189 ua_chan_getptr(kobj_t obj, void *data)
193 struct ua_chinfo *ch = data;
196 pa_dev = device_get_parent(ua->sc_dev);
198 return uaudio_chan_getptr(pa_dev);
201 static struct pcmchan_caps *
202 ua_chan_getcaps(kobj_t obj, void *data)
204 struct ua_chinfo *ch = data;
206 return (ch->dir == PCMDIR_PLAY) ? &ua_playcaps : & ua_reccaps;
209 static kobj_method_t ua_chan_methods[] = {
210 KOBJMETHOD(channel_init, ua_chan_init),
211 KOBJMETHOD(channel_setformat, ua_chan_setformat),
212 KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
213 KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
214 KOBJMETHOD(channel_trigger, ua_chan_trigger),
215 KOBJMETHOD(channel_getptr, ua_chan_getptr),
216 KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
220 CHANNEL_DECLARE(ua_chan);
222 /************************************************************/
224 ua_mixer_init(struct snd_mixer *m)
228 struct ua_info *ua = mix_getdevinfo(m);
230 pa_dev = device_get_parent(ua->sc_dev);
232 mask = uaudio_query_mix_info(pa_dev);
233 mix_setdevs(m, mask);
239 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
242 struct ua_info *ua = mix_getdevinfo(m);
244 pa_dev = device_get_parent(ua->sc_dev);
245 uaudio_mixer_set(pa_dev, type, left, right);
247 return left | (right << 8);
251 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
256 static kobj_method_t ua_mixer_methods[] = {
257 KOBJMETHOD(mixer_init, ua_mixer_init),
258 KOBJMETHOD(mixer_set, ua_mixer_set),
259 KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
263 MIXER_DECLARE(ua_mixer);
264 /************************************************************/
268 ua_probe(device_t dev)
271 struct sndcard_func *func;
273 /* The parent device has already been probed. */
275 func = device_get_ivars(dev);
276 if (func == NULL || func->func != SCF_PCM)
281 device_set_desc(dev, s);
286 ua_attach(device_t dev)
289 char status[SND_STATUSLEN];
292 ua = (struct ua_info *)malloc(sizeof *ua, M_DEVBUF, M_NOWAIT);
295 bzero(ua, sizeof *ua);
299 bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_PCM_BUFF_SIZE, 65536);
301 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
302 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
303 /*highaddr*/BUS_SPACE_MAXADDR,
304 /*filter*/NULL, /*filterarg*/NULL,
305 /*maxsize*/bufsz, /*nsegments*/1,
306 /*maxsegz*/0x3fff, /*flags*/0,
307 &ua->parent_dmat) != 0) {
308 device_printf(dev, "unable to create dma tag\n");
312 if (mixer_init(dev, &ua_mixer_class, ua)) {
316 snprintf(status, SND_STATUSLEN, "at addr ?");
318 if (pcm_register(dev, ua, 1, 0)) {
322 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
324 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
326 pcm_setstatus(dev, status);
331 bus_dma_tag_destroy(ua->parent_dmat);
338 ua_detach(device_t dev)
343 r = pcm_unregister(dev);
347 sc = pcm_getdevinfo(dev);
348 bus_dma_tag_destroy(sc->parent_dmat);
354 /************************************************************/
356 static device_method_t ua_pcm_methods[] = {
357 /* Device interface */
358 DEVMETHOD(device_probe, ua_probe),
359 DEVMETHOD(device_attach, ua_attach),
360 DEVMETHOD(device_detach, ua_detach),
365 static driver_t ua_pcm_driver = {
371 static devclass_t pcm_devclass;
373 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, 0, 0);
374 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
375 MODULE_DEPEND(ua_pcm, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
376 MODULE_VERSION(ua_pcm, 1);