Merge from vendor branch CVS:
[dragonfly.git] / sys / dev / sound / usb / uaudio_pcm.c
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 $ */
3
4 /*
5  * Copyright (c) 2000-2002 Hiroyuki Aizu <aizu@navi.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28
29
30 #include <sys/soundcard.h>
31 #include <dev/sound/pcm/sound.h>
32 #include <dev/sound/chip.h>
33
34 #include <dev/sound/usb/uaudio.h>
35
36 #include "mixer_if.h"
37
38 struct ua_info;
39
40 struct ua_chinfo {
41         struct ua_info *parent;
42         struct pcm_channel *channel;
43         struct snd_dbuf *buffer;
44         int dir, hwch;
45         u_int32_t fmt, spd, blksz;      /* XXXXX */
46 };
47
48 struct ua_info {
49         device_t sc_dev;
50         struct ua_chinfo pch, rch;
51         bus_dma_tag_t   parent_dmat;
52 };
53
54 static u_int32_t ua_playfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
55
56 static struct pcmchan_caps ua_playcaps = {8000, 48000, ua_playfmt, 0};
57
58 static u_int32_t ua_recfmt[8*2+1]; /* 8 format * (stereo or mono) + endptr */
59
60 static struct pcmchan_caps ua_reccaps = {8000, 48000, ua_recfmt, 0};
61
62 #define UAUDIO_PCM_BUFF_SIZE    16*1024
63
64 /************************************************************/
65 static void *
66 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
67 {
68         device_t pa_dev;
69         u_char *buf,*end;
70
71         struct ua_info *sc = devinfo;
72         struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
73
74         ch->parent = sc;
75         ch->channel = c;
76         ch->buffer = b;
77
78         /* allocate PCM side DMA buffer */
79         if (sndbuf_alloc(ch->buffer, sc->parent_dmat, UAUDIO_PCM_BUFF_SIZE) != 0) {
80             return NULL;
81         }
82
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);
87
88         /* Create ua_playfmt[] & ua_recfmt[] */
89         uaudio_query_formats(pa_dev, (u_int32_t *)&ua_playfmt, (u_int32_t *)&ua_recfmt);
90
91         ch->dir = dir;
92 #ifndef NO_RECORDING
93         ch->hwch = 1;
94         if (dir == PCMDIR_PLAY)
95                 ch->hwch = 2;
96 #else
97         ch->hwch = 2;
98 #endif
99
100         return ch;
101 }
102
103 static int
104 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
105 {
106         device_t pa_dev;
107         struct ua_info *ua;
108
109         struct ua_chinfo *ch = data;
110
111         ua = ch->parent;
112         pa_dev = device_get_parent(ua->sc_dev);
113         uaudio_chan_set_param_format(pa_dev, format);
114
115         ch->fmt = format;
116         return 0;
117 }
118
119 static int
120 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
121 {
122         device_t pa_dev;
123         struct ua_info *ua;
124
125         struct ua_chinfo *ch = data;
126         ch->spd = speed;
127
128         ua = ch->parent;
129         pa_dev = device_get_parent(ua->sc_dev);
130         uaudio_chan_set_param_speed(pa_dev, speed);
131
132         return ch->spd;
133 }
134
135 static int
136 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
137 {
138         device_t pa_dev;
139         struct ua_info *ua;
140         struct ua_chinfo *ch = data;
141         /* ch->blksz = blocksize; */
142         if (blocksize) {
143                 ch->blksz = blocksize;
144         } else {
145                 ch->blksz = UAUDIO_PCM_BUFF_SIZE/2;
146         }
147
148         /* XXXXX */
149         ua = ch->parent;
150         pa_dev = device_get_parent(ua->sc_dev);
151         uaudio_chan_set_param_blocksize(pa_dev, blocksize);
152
153         return ch->blksz;
154 }
155
156 static int
157 ua_chan_trigger(kobj_t obj, void *data, int go)
158 {
159         device_t pa_dev;
160         struct ua_info *ua;
161         struct ua_chinfo *ch = data;
162
163         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
164                 return 0;
165
166         ua = ch->parent;
167         pa_dev = device_get_parent(ua->sc_dev);
168
169         /* XXXXX */
170         if (ch->dir == PCMDIR_PLAY) {
171                 if (go == PCMTRIG_START) {
172                         uaudio_trigger_output(pa_dev);
173                 } else {
174                         uaudio_halt_out_dma(pa_dev);
175                 }
176         } else {
177 #ifndef NO_RECORDING
178                 if (go == PCMTRIG_START)
179                         uaudio_trigger_input(pa_dev);
180                 else
181                         uaudio_halt_in_dma(pa_dev);
182 #endif
183         }
184
185         return 0;
186 }
187
188 static int
189 ua_chan_getptr(kobj_t obj, void *data)
190 {
191         device_t pa_dev;
192         struct ua_info *ua;
193         struct ua_chinfo *ch = data;
194
195         ua = ch->parent;
196         pa_dev = device_get_parent(ua->sc_dev);
197
198         return uaudio_chan_getptr(pa_dev);
199 }
200
201 static struct pcmchan_caps *
202 ua_chan_getcaps(kobj_t obj, void *data)
203 {
204         struct ua_chinfo *ch = data;
205
206         return (ch->dir == PCMDIR_PLAY) ? &ua_playcaps : & ua_reccaps;
207 }
208
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),
217         { 0, 0 }
218 };
219
220 CHANNEL_DECLARE(ua_chan);
221
222 /************************************************************/
223 static int
224 ua_mixer_init(struct snd_mixer *m)
225 {
226         u_int32_t mask;
227         device_t pa_dev;
228         struct ua_info *ua = mix_getdevinfo(m);
229
230         pa_dev = device_get_parent(ua->sc_dev);
231
232         mask = uaudio_query_mix_info(pa_dev);
233         mix_setdevs(m,  mask);
234
235         return 0;
236 }
237
238 static int
239 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
240 {
241         device_t pa_dev;
242         struct ua_info *ua = mix_getdevinfo(m);
243
244         pa_dev = device_get_parent(ua->sc_dev);
245         uaudio_mixer_set(pa_dev, type, left, right);
246
247         return left | (right << 8);
248 }
249
250 static int
251 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
252 {
253         return src;
254 }
255
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),
260
261         { 0, 0 }
262 };
263 MIXER_DECLARE(ua_mixer);
264 /************************************************************/
265
266
267 static int
268 ua_probe(device_t dev)
269 {
270         char *s;
271         struct sndcard_func *func;
272
273         /* The parent device has already been probed. */
274
275         func = device_get_ivars(dev);
276         if (func == NULL || func->func != SCF_PCM)
277                 return (ENXIO);
278
279         s = "USB Audio";
280
281         device_set_desc(dev, s);
282         return 0;
283 }
284
285 static int
286 ua_attach(device_t dev)
287 {
288         struct ua_info *ua;
289         char status[SND_STATUSLEN];
290         unsigned int bufsz;
291
292         ua = (struct ua_info *)malloc(sizeof *ua, M_DEVBUF, M_NOWAIT);
293         if (!ua)
294                 return ENXIO;
295         bzero(ua, sizeof *ua);
296
297         ua->sc_dev = dev;
298
299         bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_PCM_BUFF_SIZE, 65536);
300
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");
309                 goto bad;
310         }
311
312         if (mixer_init(dev, &ua_mixer_class, ua)) {
313                 return(ENXIO);
314         }
315
316         snprintf(status, SND_STATUSLEN, "at addr ?");
317
318         if (pcm_register(dev, ua, 1, 0)) {
319                 return(ENXIO);
320         }
321
322         pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
323 #ifndef NO_RECORDING
324         pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
325 #endif
326         pcm_setstatus(dev, status);
327
328         return 0;
329 bad:
330         if (ua->parent_dmat)
331                 bus_dma_tag_destroy(ua->parent_dmat);
332         free(ua, M_DEVBUF);
333
334         return ENXIO;
335 }
336
337 static int
338 ua_detach(device_t dev)
339 {
340         int r;
341         struct ua_info *sc;
342
343         r = pcm_unregister(dev);
344         if (r)
345                 return r;
346
347         sc = pcm_getdevinfo(dev);
348         bus_dma_tag_destroy(sc->parent_dmat);
349         free(sc, M_DEVBUF);
350
351         return 0;
352 }
353
354 /************************************************************/
355
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),
361
362         { 0, 0 }
363 };
364
365 static driver_t ua_pcm_driver = {
366         "pcm",
367         ua_pcm_methods,
368         PCM_SOFTC_SIZE,
369 };
370
371 static devclass_t pcm_devclass;
372
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);