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;
57 int vendor, product, release;
60 #define UAUDIO_DEFAULT_BUFSZ 16*1024
68 { 0x1130, 0xf211, 0x0101, SD_F_PSWAPLR },
71 /************************************************************/
73 ua_chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
77 struct ua_info *sc = devinfo;
78 struct ua_chinfo *ch = (dir == PCMDIR_PLAY)? &sc->pch : &sc->rch;
85 pa_dev = device_get_parent(sc->sc_dev);
87 ch->buf = malloc(sc->bufsz, M_DEVBUF, M_NOWAIT);
90 if (sndbuf_setup(b, ch->buf, sc->bufsz) != 0) {
91 free(ch->buf, M_DEVBUF);
94 uaudio_chan_set_param_pcm_dma_buff(pa_dev, ch->buf, ch->buf+sc->bufsz, ch->channel, dir);
96 device_printf(pa_dev, "%s buf %p\n", (dir == PCMDIR_PLAY)?
97 "play" : "rec", sndbuf_getbuf(ch->buffer));
102 if (dir == PCMDIR_PLAY)
112 ua_chan_free(kobj_t obj, void *data)
114 struct ua_chinfo *ua = data;
117 free(ua->buf, M_DEVBUF);
122 ua_chan_setformat(kobj_t obj, void *data, u_int32_t format)
127 struct ua_chinfo *ch = data;
130 * At this point, no need to query as we shouldn't select an unsorted format
133 pa_dev = device_get_parent(ua->sc_dev);
134 uaudio_chan_set_param_format(pa_dev, format, ch->dir);
141 ua_chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
143 struct ua_chinfo *ch;
148 pa_dev = device_get_parent(ch->parent->sc_dev);
150 if ((bestspeed = uaudio_chan_set_param_speed(pa_dev, speed, ch->dir)))
157 ua_chan_setfragments(kobj_t obj, void *data, u_int32_t blksz, u_int32_t blkcnt)
160 struct ua_chinfo *ch = data;
161 struct ua_info *ua = ch->parent;
163 RANGE(blksz, 128, sndbuf_getmaxsize(ch->buffer) / 2);
164 RANGE(blkcnt, 2, 512);
166 while ((blksz * blkcnt) > sndbuf_getmaxsize(ch->buffer)) {
167 if ((blkcnt >> 1) >= 2)
169 else if ((blksz >> 1) >= 128)
175 if ((sndbuf_getblksz(ch->buffer) != blksz ||
176 sndbuf_getblkcnt(ch->buffer) != blkcnt) &&
177 sndbuf_resize(ch->buffer, blkcnt, blksz) != 0)
178 device_printf(ua->sc_dev, "%s: failed blksz=%u blkcnt=%u\n",
179 __func__, blksz, blkcnt);
181 ch->blksz = sndbuf_getblksz(ch->buffer);
183 pa_dev = device_get_parent(ua->sc_dev);
184 uaudio_chan_set_param_pcm_dma_buff(pa_dev, ch->buf,
185 ch->buf + sndbuf_getsize(ch->buffer), ch->channel, ch->dir);
186 uaudio_chan_set_param_blocksize(pa_dev, ch->blksz, ch->dir);
192 ua_chan_setblocksize(kobj_t obj, void *data, u_int32_t blksz)
194 struct ua_chinfo *ch = data;
196 ua_chan_setfragments(obj, data, blksz,
197 sndbuf_getmaxsize(ch->buffer) / blksz);
203 ua_chan_trigger(kobj_t obj, void *data, int go)
207 struct ua_chinfo *ch = data;
209 if (!PCMTRIG_COMMON(go))
213 pa_dev = device_get_parent(ua->sc_dev);
216 if (ch->dir == PCMDIR_PLAY) {
217 if (go == PCMTRIG_START) {
218 uaudio_trigger_output(pa_dev);
220 uaudio_halt_out_dma(pa_dev);
224 if (go == PCMTRIG_START)
225 uaudio_trigger_input(pa_dev);
227 uaudio_halt_in_dma(pa_dev);
235 ua_chan_getptr(kobj_t obj, void *data)
239 struct ua_chinfo *ch = data;
242 pa_dev = device_get_parent(ua->sc_dev);
244 return uaudio_chan_getptr(pa_dev, ch->dir);
247 static struct pcmchan_caps *
248 ua_chan_getcaps(kobj_t obj, void *data)
250 struct ua_chinfo *ch;
253 return (ch->dir == PCMDIR_PLAY) ? &(ch->parent->ua_playcaps) : &(ch->parent->ua_reccaps);
256 static kobj_method_t ua_chan_methods[] = {
257 KOBJMETHOD(channel_init, ua_chan_init),
258 KOBJMETHOD(channel_free, ua_chan_free),
259 KOBJMETHOD(channel_setformat, ua_chan_setformat),
260 KOBJMETHOD(channel_setspeed, ua_chan_setspeed),
261 KOBJMETHOD(channel_setblocksize, ua_chan_setblocksize),
262 KOBJMETHOD(channel_setfragments, ua_chan_setfragments),
263 KOBJMETHOD(channel_trigger, ua_chan_trigger),
264 KOBJMETHOD(channel_getptr, ua_chan_getptr),
265 KOBJMETHOD(channel_getcaps, ua_chan_getcaps),
269 CHANNEL_DECLARE(ua_chan);
271 /************************************************************/
273 ua_mixer_init(struct snd_mixer *m)
277 struct ua_info *ua = mix_getdevinfo(m);
279 pa_dev = device_get_parent(ua->sc_dev);
281 mask = uaudio_query_mix_info(pa_dev);
282 if (!(mask & SOUND_MASK_PCM)) {
284 * Emulate missing pcm mixer controller
285 * through FEEDER_VOLUME
287 pcm_setflags(ua->sc_dev, pcm_getflags(ua->sc_dev) |
290 if (!(mask & SOUND_MASK_VOLUME)) {
291 mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM);
292 mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE);
294 mix_setdevs(m, mask);
296 mask = uaudio_query_recsrc_info(pa_dev);
297 mix_setrecdevs(m, mask);
303 ua_mixer_set(struct snd_mixer *m, unsigned type, unsigned left, unsigned right)
306 struct ua_info *ua = mix_getdevinfo(m);
308 pa_dev = device_get_parent(ua->sc_dev);
309 uaudio_mixer_set(pa_dev, type, left, right);
311 return left | (right << 8);
315 ua_mixer_setrecsrc(struct snd_mixer *m, u_int32_t src)
318 struct ua_info *ua = mix_getdevinfo(m);
320 pa_dev = device_get_parent(ua->sc_dev);
321 return uaudio_mixer_setrecsrc(pa_dev, src);
324 static kobj_method_t ua_mixer_methods[] = {
325 KOBJMETHOD(mixer_init, ua_mixer_init),
326 KOBJMETHOD(mixer_set, ua_mixer_set),
327 KOBJMETHOD(mixer_setrecsrc, ua_mixer_setrecsrc),
331 MIXER_DECLARE(ua_mixer);
332 /************************************************************/
336 ua_probe(device_t dev)
339 struct sndcard_func *func;
341 /* The parent device has already been probed. */
343 func = device_get_ivars(dev);
344 if (func == NULL || func->func != SCF_PCM)
349 device_set_desc(dev, s);
350 return BUS_PROBE_DEFAULT;
354 ua_attach(device_t dev)
357 struct sndcard_func *func;
358 char status[SND_STATUSLEN];
360 u_int32_t nplay, nrec, flags;
363 ua = malloc(sizeof(*ua), M_DEVBUF, M_WAITOK | M_ZERO);
366 /* Mark for existence */
367 func = device_get_ivars(dev);
369 func->varinfo = (void *)ua;
371 pa_dev = device_get_parent(dev);
372 ua->vendor = uaudio_get_vendor(pa_dev);
373 ua->product = uaudio_get_product(pa_dev);
374 ua->release = uaudio_get_release(pa_dev);
379 "vendor=0x%04x, product=0x%04x, release=0x%04x\n",
380 ua->vendor, ua->product, ua->release);
382 ua->bufsz = pcm_getbuffersize(dev, 4096, UAUDIO_DEFAULT_BUFSZ, 65536);
384 device_printf(dev, "using a default buffer size of %jd\n", (intmax_t)ua->bufsz);
386 if (mixer_init(dev, &ua_mixer_class, ua)) {
390 snprintf(status, SND_STATUSLEN, "at ? %s", PCM_KLDSTRING(snd_uaudio));
392 ua->ua_playcaps.fmtlist = ua->ua_playfmt;
393 ua->ua_reccaps.fmtlist = ua->ua_recfmt;
394 nplay = uaudio_query_formats(pa_dev, PCMDIR_PLAY, FORMAT_NUM * 2, &ua->ua_playcaps);
395 nrec = uaudio_query_formats(pa_dev, PCMDIR_REC, FORMAT_NUM * 2, &ua->ua_reccaps);
402 flags = pcm_getflags(dev);
403 for (i = 0; i < (sizeof(ua_quirks) / sizeof(ua_quirks[0])); i++) {
404 if (ua->vendor == ua_quirks[i].vendor &&
405 ua->product == ua_quirks[i].product &&
406 ua->release == ua_quirks[i].release)
407 flags |= ua_quirks[i].dflags;
409 pcm_setflags(dev, flags);
412 if (pcm_register(dev, ua, nplay, nrec)) {
414 if (pcm_register(dev, ua, nplay, 0)) {
419 sndstat_unregister(dev);
420 uaudio_sndstat_register(dev);
422 for (i = 0; i < nplay; i++) {
423 pcm_addchan(dev, PCMDIR_PLAY, &ua_chan_class, ua);
426 for (i = 0; i < nrec; i++) {
427 pcm_addchan(dev, PCMDIR_REC, &ua_chan_class, ua);
430 pcm_setstatus(dev, status);
434 bad: free(ua, M_DEVBUF);
439 ua_detach(device_t dev)
442 struct sndcard_func *func;
445 r = pcm_unregister(dev);
449 sc = pcm_getdevinfo(dev);
452 /* Mark for deletion */
453 func = device_get_ivars(dev);
455 func->varinfo = NULL;
460 /************************************************************/
462 static device_method_t ua_pcm_methods[] = {
463 /* Device interface */
464 DEVMETHOD(device_probe, ua_probe),
465 DEVMETHOD(device_attach, ua_attach),
466 DEVMETHOD(device_detach, ua_detach),
471 static driver_t ua_pcm_driver = {
478 DRIVER_MODULE(ua_pcm, uaudio, ua_pcm_driver, pcm_devclass, 0, 0);
479 MODULE_DEPEND(ua_pcm, uaudio, 1, 1, 1);
480 MODULE_DEPEND(ua_pcm, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
481 MODULE_VERSION(ua_pcm, 1);