- JMC260 with full mask revision 2 and ECO revision 0 does not support
[dragonfly.git] / sys / dev / sound / usb / uaudio_pcm.c
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 $ */
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         u_char *buf;
45         int dir, hwch;
46         u_int32_t fmt, spd, blksz;      /* XXXXX */
47 };
48
49 struct ua_info {
50         device_t sc_dev;
51         u_int32_t bufsz;
52         struct ua_chinfo pch, rch;
53 #define FORMAT_NUM      32
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;
58 };
59
60 #define UAUDIO_DEFAULT_BUFSZ            16*1024
61
62 /************************************************************/
63 static void *
64 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
65 {
66         device_t pa_dev;
67
68         struct ua_info *sc = devinfo;
69         struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
70
71         ch->parent = sc;
72         ch->channel = c;
73         ch->buffer = b;
74         ch->dir = dir;
75
76         pa_dev = device_get_parent(sc->sc_dev);
77
78         ch->buf = kmalloc(sc->bufsz, M_DEVBUF, M_NOWAIT);
79         if (ch->buf == NULL)
80                 return NULL;
81         if (sndbuf_setup(b, ch->buf, sc->bufsz) != 0) {
82                 kfree(ch->buf, M_DEVBUF);
83                 return NULL;
84         }
85         uaudio_chan_set_param_pcm_dma_buff(pa_dev, ch->buf, ch->buf+sc->bufsz, ch->channel, dir);
86         if (bootverbose)
87                 device_printf(pa_dev, "%s buf %p\n", (dir == PCMDIR_PLAY)?
88                               "play" : "rec", sndbuf_getbuf(ch->buffer));
89
90         ch->dir = dir;
91 #ifndef NO_RECORDING
92         ch->hwch = 1;
93         if (dir == PCMDIR_PLAY)
94                 ch->hwch = 2;
95 #else
96         ch->hwch = 2;
97 #endif
98
99         return ch;
100 }
101
102 static int
103 ua_chan_free(kobj_t obj, void *data)
104 {
105         struct ua_chinfo *ua = data;
106
107         if (ua->buf != NULL)
108                 kfree(ua->buf, M_DEVBUF);
109         return 0;
110 }
111
112 static int
113 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
114 {
115         device_t pa_dev;
116         struct ua_info *ua;
117
118         struct ua_chinfo *ch = data;
119
120         /*
121          * At this point, no need to query as we shouldn't select an unsorted format
122          */
123         ua = ch->parent;
124         pa_dev = device_get_parent(ua->sc_dev);
125         uaudio_chan_set_param_format(pa_dev, format, ch->dir);
126
127         ch->fmt = format;
128         return 0;
129 }
130
131 static int
132 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
133 {
134         struct ua_chinfo *ch;
135         device_t pa_dev;
136         int bestspeed;
137
138         ch = data;
139         pa_dev = device_get_parent(ch->parent->sc_dev);
140
141         if ((bestspeed = uaudio_chan_set_param_speed(pa_dev, speed, ch->dir)))
142                 ch->spd = bestspeed;
143
144         return ch->spd;
145 }
146
147 static int
148 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
149 {
150         device_t pa_dev;
151         struct ua_chinfo *ch = data;
152         struct ua_info *ua = ch->parent;
153
154         if (blocksize) {
155                 RANGE(blocksize, 128, ua->bufsz / 2);
156                 if (sndbuf_resize(ch->buffer, ua->bufsz/blocksize, blocksize) != 0) {
157                         ch->blksz = blocksize;
158                 }
159         }
160         pa_dev = device_get_parent(ua->sc_dev);
161         uaudio_chan_set_param_blocksize(pa_dev, blocksize, ch->dir);
162
163         return ch->blksz;
164 }
165
166 static int
167 ua_chan_trigger(kobj_t obj, void *data, int go)
168 {
169         device_t pa_dev;
170         struct ua_info *ua;
171         struct ua_chinfo *ch = data;
172
173         if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
174                 return 0;
175
176         ua = ch->parent;
177         pa_dev = device_get_parent(ua->sc_dev);
178
179         /* XXXXX */
180         if (ch->dir == PCMDIR_PLAY) {
181                 if (go == PCMTRIG_START) {
182                         uaudio_trigger_output(pa_dev);
183                 } else {
184                         uaudio_halt_out_dma(pa_dev);
185                 }
186         } else {
187 #ifndef NO_RECORDING
188                 if (go == PCMTRIG_START)
189                         uaudio_trigger_input(pa_dev);
190                 else
191                         uaudio_halt_in_dma(pa_dev);
192 #endif
193         }
194
195         return 0;
196 }
197
198 static int
199 ua_chan_getptr(kobj_t obj, void *data)
200 {
201         device_t pa_dev;
202         struct ua_info *ua;
203         struct ua_chinfo *ch = data;
204
205         ua = ch->parent;
206         pa_dev = device_get_parent(ua->sc_dev);
207
208         return uaudio_chan_getptr(pa_dev, ch->dir);
209 }
210
211 static struct pcmchan_caps *
212 ua_chan_getcaps(kobj_t obj, void *data)
213 {
214         struct ua_chinfo *ch;
215
216         ch = data;
217         return (ch->dir == PCMDIR_PLAY) ? &(ch->parent->ua_playcaps) : &(ch->parent->ua_reccaps);
218 }
219
220 static kobj_method_t ua_chan_methods[] = {
221         KOBJMETHOD(channel_init,                ua_chan_init),
222         KOBJMETHOD(channel_free,                ua_chan_free),
223         KOBJMETHOD(channel_setformat,           ua_chan_setformat),
224         KOBJMETHOD(channel_setspeed,            ua_chan_setspeed),
225         KOBJMETHOD(channel_setblocksize,        ua_chan_setblocksize),
226         KOBJMETHOD(channel_trigger,             ua_chan_trigger),
227         KOBJMETHOD(channel_getptr,              ua_chan_getptr),
228         KOBJMETHOD(channel_getcaps,             ua_chan_getcaps),
229         { 0, 0 }
230 };
231
232 CHANNEL_DECLARE(ua_chan);
233
234 /************************************************************/
235 static int
236 ua_mixer_init(struct snd_mixer *m)
237 {
238         u_int32_t mask;
239         device_t pa_dev;
240         struct snddev_info *d;
241         struct ua_info *ua = mix_getdevinfo(m);
242
243         pa_dev = device_get_parent(ua->sc_dev);
244         d = device_get_softc(ua->sc_dev);
245
246         mask = uaudio_query_mix_info(pa_dev);
247         if (d != NULL) {
248                 if (!(mask & SOUND_MASK_PCM)) {
249                         /*
250                          * Emulate missing pcm mixer controller
251                          * through FEEDER_VOLUME
252                          */
253                          d->flags |= SD_F_SOFTPCMVOL;
254                 }
255                 if (!(mask & SOUND_MASK_VOLUME)) {
256                         mix_setparentchild(m, SOUND_MIXER_VOLUME,
257                             SOUND_MASK_PCM);
258                         mix_setrealdev(m, SOUND_MIXER_VOLUME,
259                             SOUND_MIXER_NONE);
260                 }
261         }
262         mix_setdevs(m,  mask);
263
264         mask = uaudio_query_recsrc_info(pa_dev);
265         mix_setrecdevs(m, mask);
266
267         return 0;
268 }
269
270 static int
271 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
272 {
273         device_t pa_dev;
274         struct ua_info *ua = mix_getdevinfo(m);
275
276         pa_dev = device_get_parent(ua->sc_dev);
277         uaudio_mixer_set(pa_dev, type, left, right);
278
279         return left | (right << 8);
280 }
281
282 static int
283 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
284 {
285         device_t pa_dev;
286         struct ua_info *ua = mix_getdevinfo(m);
287
288         pa_dev = device_get_parent(ua->sc_dev);
289         return uaudio_mixer_setrecsrc(pa_dev, src);
290 }
291
292 static kobj_method_t ua_mixer_methods[] = {
293         KOBJMETHOD(mixer_init,          ua_mixer_init),
294         KOBJMETHOD(mixer_set,           ua_mixer_set),
295         KOBJMETHOD(mixer_setrecsrc,     ua_mixer_setrecsrc),
296
297         { 0, 0 }
298 };
299 MIXER_DECLARE(ua_mixer);
300 /************************************************************/
301
302
303 static int
304 ua_probe(device_t dev)
305 {
306         char *s;
307         struct sndcard_func *func;
308
309         /* The parent device has already been probed. */
310
311         func = device_get_ivars(dev);
312         if (func == NULL || func->func != SCF_PCM)
313                 return (ENXIO);
314
315         s = "USB Audio";
316
317         device_set_desc(dev, s);
318         return BUS_PROBE_DEFAULT;
319 }
320
321 static int
322 ua_attach(device_t dev)
323 {
324         struct ua_info *ua;
325         char status[SND_STATUSLEN];
326         device_t pa_dev;
327         u_int32_t nplay, nrec;
328         int i;
329
330         ua = (struct ua_info *)kmalloc(sizeof *ua, M_DEVBUF, M_ZERO | M_NOWAIT);
331         if (ua == NULL)
332                 return ENXIO;
333
334         ua->sc_dev = dev;
335
336         pa_dev = device_get_parent(dev);
337
338         ua->bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_DEFAULT_BUFSZ, 65536);
339         if (bootverbose)
340                 device_printf(dev, "using a default buffer size of %jd\n", (intmax_t)ua->bufsz);
341
342         if (mixer_init(dev, &ua_mixer_class, ua)) {
343                 goto bad;
344         }
345
346         ksnprintf(status, SND_STATUSLEN, "at ? %s", PCM_KLDSTRING(snd_uaudio));
347
348         ua->ua_playcaps.fmtlist = ua->ua_playfmt;
349         ua->ua_reccaps.fmtlist = ua->ua_recfmt;
350         nplay = uaudio_query_formats(pa_dev, PCMDIR_PLAY, FORMAT_NUM * 2, &ua->ua_playcaps);
351         nrec = uaudio_query_formats(pa_dev, PCMDIR_REC, FORMAT_NUM * 2, &ua->ua_reccaps);
352
353         if (nplay > 1)
354                 nplay = 1;
355         if (nrec > 1)
356                 nrec = 1;
357
358 #ifndef NO_RECORDING
359         if (pcm_register(dev, ua, nplay, nrec)) {
360 #else
361         if (pcm_register(dev, ua, nplay, 0)) {
362 #endif
363                 goto bad;
364         }
365
366         sndstat_unregister(dev);
367         uaudio_sndstat_register(dev);
368
369         for (i = 0; i < nplay; i++) {
370                 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
371         }
372 #ifndef NO_RECORDING
373         for (i = 0; i < nrec; i++) {
374                 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
375         }
376 #endif
377         pcm_setstatus(dev, status);
378
379         return 0;
380
381 bad:    kfree(ua, M_DEVBUF);
382         return ENXIO;
383 }
384
385 static int
386 ua_detach(device_t dev)
387 {
388         int r;
389         struct ua_info *sc;
390
391         r = pcm_unregister(dev);
392         if (r)
393                 return r;
394
395         sc = pcm_getdevinfo(dev);
396         kfree(sc, M_DEVBUF);
397
398         return 0;
399 }
400
401 /************************************************************/
402
403 static device_method_t ua_pcm_methods[] = {
404         /* Device interface */
405         DEVMETHOD(device_probe,         ua_probe),
406         DEVMETHOD(device_attach,        ua_attach),
407         DEVMETHOD(device_detach,        ua_detach),
408
409         { 0, 0 }
410 };
411
412 static driver_t ua_pcm_driver = {
413         "pcm",
414         ua_pcm_methods,
415         PCM_SOFTC_SIZE,
416 };
417
418
419 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, 0, 0);
420 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
421 MODULE_DEPEND(ua_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
422 MODULE_VERSION(ua_pcm, 1);