Say hello to a sound system update from FreeBSD. This includes the long
[dragonfly.git] / sys / dev / sound / usb / uaudio.c
1 /*      $NetBSD: uaudio.c,v 1.91 2004/11/05 17:46:14 kent Exp $ */
2 /*      $FreeBSD: src/sys/dev/sound/usb/uaudio.c,v 1.14.2.2 2006/04/04 17:34:10 ariff Exp $ */
3 /*      $DragonFly: src/sys/dev/sound/usb/uaudio.c,v 1.11 2007/01/04 21:47:03 corecode Exp $: */
4
5 /*-
6  * Copyright (c) 1999 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Lennart Augustsson (lennart@augustsson.net) at
11  * Carlstedt Research & Technology.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *        This product includes software developed by the NetBSD
24  *        Foundation, Inc. and its contributors.
25  * 4. Neither the name of The NetBSD Foundation nor the names of its
26  *    contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
33  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39  * POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 /*
43  * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf
44  *                  http://www.usb.org/developers/devclass_docs/frmts10.pdf
45  *                  http://www.usb.org/developers/devclass_docs/termt10.pdf
46  */
47
48 #include <sys/cdefs.h>
49 #if defined(__NetBSD__) || defined(__OpenBSD__)
50 __KERNEL_RCSID(0, "$NetBSD: uaudio.c,v 1.91 2004/11/05 17:46:14 kent Exp $");
51 #endif
52
53 /*
54  * Also merged:
55  *  $NetBSD: uaudio.c,v 1.94 2005/01/15 15:19:53 kent Exp $
56  *  $NetBSD: uaudio.c,v 1.95 2005/01/16 06:02:19 dsainty Exp $
57  *  $NetBSD: uaudio.c,v 1.96 2005/01/16 12:46:00 kent Exp $
58  *  $NetBSD: uaudio.c,v 1.97 2005/02/24 08:19:38 martin Exp $
59  */
60
61 #include <sys/param.h>
62 #include <sys/systm.h>
63 #include <sys/kernel.h>
64 #include <sys/malloc.h>
65 #if defined(__NetBSD__) || defined(__OpenBSD__)
66 #include <sys/device.h>
67 #include <sys/ioctl.h>
68 #endif
69 #include <sys/tty.h>
70 #include <sys/file.h>
71 #include <sys/reboot.h>         /* for bootverbose */
72 #include <sys/select.h>
73 #include <sys/proc.h>
74 #if defined(__NetBSD__) || defined(__OpenBSD__)
75 #include <sys/device.h>
76 #elif defined(__FreeBSD__) || defined(__DragonFly__)
77 #include <sys/module.h>
78 #include <sys/bus.h>
79 #include <sys/conf.h>
80 #endif
81 #include <sys/poll.h>
82 #if defined(__FreeBSD__)
83 #include <sys/sysctl.h>
84 #include <sys/sbuf.h>
85 #elif defined(__DragonFly__)
86 #include <sys/sysctl.h>
87 #include <sys/thread2.h>
88 #endif
89
90 #if defined(__NetBSD__) || defined(__OpenBSD__)
91 #include <sys/audioio.h>
92 #include <dev/audio_if.h>
93 #include <dev/audiovar.h>
94 #include <dev/mulaw.h>
95 #include <dev/auconv.h>
96 #elif defined(__FreeBSD__) || defined(__DragonFly__)
97 #include <dev/sound/pcm/sound.h>        /* XXXXX */
98 #include <dev/sound/chip.h>
99 #include "feeder_if.h"
100 #endif
101
102 #include <bus/usb/usb.h>
103 #include <bus/usb/usbdi.h>
104 #include <bus/usb/usbdi_util.h>
105 #include <bus/usb/usb_quirks.h>
106
107 #if defined(__NetBSD__) || defined(__OpenBSD__)
108 #include <dev/usb/uaudioreg.h>
109 #elif defined(__FreeBSD__) || defined(__DragonFly__)
110 #include <dev/sound/usb/uaudioreg.h>
111 #include <dev/sound/usb/uaudio.h>
112 #endif
113
114 #if defined(__NetBSD__) || defined(__OpenBSD__)
115 /* #define UAUDIO_DEBUG */
116 #else
117 /* #define USB_DEBUG */
118 #endif
119 /* #define UAUDIO_MULTIPLE_ENDPOINTS */
120 #ifdef USB_DEBUG
121 #define DPRINTF(x)      do { if (uaudiodebug) logprintf x; } while (0)
122 #define DPRINTFN(n,x)   do { if (uaudiodebug>(n)) logprintf x; } while (0)
123 int     uaudiodebug = 0;
124 #if defined(__FreeBSD__) || defined(__DragonFly__)
125 SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW, 0, "USB uaudio");
126 SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, debug, CTLFLAG_RW,
127            &uaudiodebug, 0, "uaudio debug level");
128 #endif
129 #else
130 #define DPRINTF(x)
131 #define DPRINTFN(n,x)
132 #endif
133
134 #define UAUDIO_NCHANBUFS 6      /* number of outstanding request */
135 #if defined(__NetBSD__) || defined(__OpenBSD__)
136 #define UAUDIO_NFRAMES   10     /* ms of sound in each request */
137 #elif defined(__FreeBSD__) || defined(__DragonFly__)
138 #define UAUDIO_NFRAMES   20     /* ms of sound in each request */
139 #endif
140
141
142 #define MIX_MAX_CHAN 8
143 struct mixerctl {
144         uint16_t        wValue[MIX_MAX_CHAN]; /* using nchan */
145         uint16_t        wIndex;
146         uint8_t         nchan;
147         uint8_t         type;
148 #define MIX_ON_OFF      1
149 #define MIX_SIGNED_16   2
150 #define MIX_UNSIGNED_16 3
151 #define MIX_SIGNED_8    4
152 #define MIX_SELECTOR    5
153 #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1)
154 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
155         int             minval, maxval;
156         u_int           delta;
157         u_int           mul;
158 #if defined(__FreeBSD__) || defined(__DragonFly__) /* XXXXX */
159         unsigned        ctl;
160 #define MAX_SELECTOR_INPUT_PIN 256
161         uint8_t         slctrtype[MAX_SELECTOR_INPUT_PIN];
162 #endif
163         uint8_t         class;
164 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
165         char            ctlname[MAX_AUDIO_DEV_LEN];
166         char            *ctlunit;
167 #endif
168 };
169 #define MAKE(h,l) (((h) << 8) | (l))
170
171 struct as_info {
172         uint8_t         alt;
173         uint8_t         encoding;
174         uint8_t         attributes; /* Copy of bmAttributes of
175                                      * usb_audio_streaming_endpoint_descriptor
176                                      */
177         usbd_interface_handle   ifaceh;
178         const usb_interface_descriptor_t *idesc;
179         const usb_endpoint_descriptor_audio_t *edesc;
180         const usb_endpoint_descriptor_audio_t *edesc1;
181         const struct usb_audio_streaming_type1_descriptor *asf1desc;
182         int             sc_busy;        /* currently used */
183 };
184
185 struct chan {
186 #if defined(__NetBSD__) || defined(__OpenBSD__)
187         void    (*intr)(void *);        /* DMA completion intr handler */
188         void    *arg;           /* arg for intr() */
189 #else
190         struct pcm_channel *pcm_ch;
191 #endif
192         usbd_pipe_handle pipe;
193         usbd_pipe_handle sync_pipe;
194
195         u_int   sample_size;
196         u_int   sample_rate;
197         u_int   bytes_per_frame;
198         u_int   fraction;       /* fraction/1000 is the extra samples/frame */
199         u_int   residue;        /* accumulates the fractional samples */
200
201         u_char  *start;         /* upper layer buffer start */
202         u_char  *end;           /* upper layer buffer end */
203         u_char  *cur;           /* current position in upper layer buffer */
204         int     blksize;        /* chunk size to report up */
205         int     transferred;    /* transferred bytes not reported up */
206
207         int     altidx;         /* currently used altidx */
208
209         int     curchanbuf;
210         struct chanbuf {
211                 struct chan     *chan;
212                 usbd_xfer_handle xfer;
213                 u_char          *buffer;
214                 u_int16_t       sizes[UAUDIO_NFRAMES];
215                 u_int16_t       offsets[UAUDIO_NFRAMES];
216                 u_int16_t       size;
217         } chanbufs[UAUDIO_NCHANBUFS];
218
219         struct uaudio_softc *sc; /* our softc */
220 #if defined(__FreeBSD__) || defined(__DragonFly__)
221         u_int32_t format;
222         int     precision;
223         int     channels;
224 #endif
225 };
226
227 struct uaudio_softc {
228         USBBASEDEVICE   sc_dev;         /* base device */
229         usbd_device_handle sc_udev;     /* USB device */
230         int             sc_ac_iface;    /* Audio Control interface */
231         usbd_interface_handle   sc_ac_ifaceh;
232         struct chan     sc_playchan;    /* play channel */
233         struct chan     sc_recchan;     /* record channel */
234         int             sc_nullalt;
235         int             sc_audio_rev;
236         struct as_info  *sc_alts;       /* alternate settings */
237         int             sc_nalts;       /* # of alternate settings */
238         int             sc_altflags;
239 #define HAS_8           0x01
240 #define HAS_16          0x02
241 #define HAS_8U          0x04
242 #define HAS_ALAW        0x08
243 #define HAS_MULAW       0x10
244 #define UA_NOFRAC       0x20            /* don't do sample rate adjustment */
245 #define HAS_24          0x40
246 #define HAS_32          0x80
247         int             sc_mode;        /* play/record capability */
248         struct mixerctl *sc_ctls;       /* mixer controls */
249         int             sc_nctls;       /* # of mixer controls */
250         device_ptr_t    sc_audiodev;
251         char            sc_dying;
252 #if defined(__FreeBSD__) || defined(__DragonFly__)
253         struct sbuf     uaudio_sndstat;
254         int             uaudio_sndstat_flag;
255 #endif
256 };
257
258 struct terminal_list {
259         int size;
260         uint16_t terminals[1];
261 };
262 #define TERMINAL_LIST_SIZE(N)   (offsetof(struct terminal_list, terminals) \
263                                 + sizeof(uint16_t) * (N))
264
265 struct io_terminal {
266         union {
267                 const usb_descriptor_t *desc;
268                 const struct usb_audio_input_terminal *it;
269                 const struct usb_audio_output_terminal *ot;
270                 const struct usb_audio_mixer_unit *mu;
271                 const struct usb_audio_selector_unit *su;
272                 const struct usb_audio_feature_unit *fu;
273                 const struct usb_audio_processing_unit *pu;
274                 const struct usb_audio_extension_unit *eu;
275         } d;
276         int inputs_size;
277         struct terminal_list **inputs; /* list of source input terminals */
278         struct terminal_list *output; /* list of destination output terminals */
279         int direct;             /* directly connected to an output terminal */
280 };
281
282 #define UAC_OUTPUT      0
283 #define UAC_INPUT       1
284 #define UAC_EQUAL       2
285 #define UAC_RECORD      3
286 #define UAC_NCLASSES    4
287 #ifdef USB_DEBUG
288 #if defined(__FreeBSD__) || defined(__DragonFly__)
289 #define AudioCinputs    "inputs"
290 #define AudioCoutputs   "outputs"
291 #define AudioCrecord    "record"
292 #define AudioCequalization      "equalization"
293 #endif
294 Static const char *uac_names[] = {
295         AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord,
296 };
297 #endif
298
299 Static usbd_status uaudio_identify_ac
300         (struct uaudio_softc *, const usb_config_descriptor_t *);
301 Static usbd_status uaudio_identify_as
302         (struct uaudio_softc *, const usb_config_descriptor_t *);
303 Static usbd_status uaudio_process_as
304         (struct uaudio_softc *, const char *, int *, int,
305          const usb_interface_descriptor_t *);
306
307 Static void     uaudio_add_alt(struct uaudio_softc *, const struct as_info *);
308
309 Static const usb_interface_descriptor_t *uaudio_find_iface
310         (const char *, int, int *, int);
311
312 Static void     uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *);
313
314 #if defined(__NetBSD__) || defined(__OpenBSD__)
315 Static char     *uaudio_id_name
316         (struct uaudio_softc *, const struct io_terminal *, int);
317 #endif
318
319 #ifdef USB_DEBUG
320 Static void     uaudio_dump_cluster(const struct usb_audio_cluster *);
321 #endif
322 Static struct usb_audio_cluster uaudio_get_cluster
323         (int, const struct io_terminal *);
324 Static void     uaudio_add_input
325         (struct uaudio_softc *, const struct io_terminal *, int);
326 Static void     uaudio_add_output
327         (struct uaudio_softc *, const struct io_terminal *, int);
328 Static void     uaudio_add_mixer
329         (struct uaudio_softc *, const struct io_terminal *, int);
330 Static void     uaudio_add_selector
331         (struct uaudio_softc *, const struct io_terminal *, int);
332 #ifdef USB_DEBUG
333 Static const char *uaudio_get_terminal_name(int);
334 #endif
335 Static int      uaudio_determine_class
336         (const struct io_terminal *, struct mixerctl *);
337 #if defined(__FreeBSD__) || defined(__DragonFly__)
338 Static const int uaudio_feature_name(const struct io_terminal *,
339                     struct mixerctl *);
340 #else
341 Static const char *uaudio_feature_name
342         (const struct io_terminal *, struct mixerctl *);
343 #endif
344 Static void     uaudio_add_feature
345         (struct uaudio_softc *, const struct io_terminal *, int);
346 Static void     uaudio_add_processing_updown
347         (struct uaudio_softc *, const struct io_terminal *, int);
348 Static void     uaudio_add_processing
349         (struct uaudio_softc *, const struct io_terminal *, int);
350 Static void     uaudio_add_extension
351         (struct uaudio_softc *, const struct io_terminal *, int);
352 Static struct terminal_list *uaudio_merge_terminal_list
353         (const struct io_terminal *);
354 Static struct terminal_list *uaudio_io_terminaltype
355         (int, struct io_terminal *, int);
356 Static usbd_status uaudio_identify
357         (struct uaudio_softc *, const usb_config_descriptor_t *);
358
359 Static int      uaudio_signext(int, int);
360 #if defined(__NetBSD__) || defined(__OpenBSD__)
361 Static int      uaudio_value2bsd(struct mixerctl *, int);
362 #endif
363 Static int      uaudio_bsd2value(struct mixerctl *, int);
364 Static int      uaudio_get(struct uaudio_softc *, int, int, int, int, int);
365 #if defined(__NetBSD__) || defined(__OpenBSD__)
366 Static int      uaudio_ctl_get
367         (struct uaudio_softc *, int, struct mixerctl *, int);
368 #endif
369 Static void     uaudio_set
370         (struct uaudio_softc *, int, int, int, int, int, int);
371 Static void     uaudio_ctl_set
372         (struct uaudio_softc *, int, struct mixerctl *, int, int);
373
374 Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int);
375
376 Static usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *);
377 Static void     uaudio_chan_close(struct uaudio_softc *, struct chan *);
378 Static usbd_status uaudio_chan_alloc_buffers
379         (struct uaudio_softc *, struct chan *);
380 Static void     uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *);
381
382 #if defined(__NetBSD__) || defined(__OpenBSD__)
383 Static void     uaudio_chan_init
384         (struct chan *, int, const struct audio_params *, int);
385 Static void     uaudio_chan_set_param(struct chan *, u_char *, u_char *, int);
386 #endif
387
388 Static void     uaudio_chan_ptransfer(struct chan *);
389 Static void     uaudio_chan_pintr
390         (usbd_xfer_handle, usbd_private_handle, usbd_status);
391
392 Static void     uaudio_chan_rtransfer(struct chan *);
393 Static void     uaudio_chan_rintr
394         (usbd_xfer_handle, usbd_private_handle, usbd_status);
395
396 #if defined(__NetBSD__) || defined(__OpenBSD__)
397 Static int      uaudio_open(void *, int);
398 Static void     uaudio_close(void *);
399 Static int      uaudio_drain(void *);
400 Static int      uaudio_query_encoding(void *, struct audio_encoding *);
401 Static void     uaudio_get_minmax_rates
402         (int, const struct as_info *, const struct audio_params *,
403          int, u_long *, u_long *);
404 Static int      uaudio_match_alt_sub
405         (int, const struct as_info *, const struct audio_params *, int, u_long);
406 Static int      uaudio_match_alt_chan
407         (int, const struct as_info *, struct audio_params *, int);
408 Static int      uaudio_match_alt
409         (int, const struct as_info *, struct audio_params *, int);
410 Static int      uaudio_set_params
411         (void *, int, int, struct audio_params *, struct audio_params *);
412 Static int      uaudio_round_blocksize(void *, int);
413 Static int      uaudio_trigger_output
414         (void *, void *, void *, int, void (*)(void *), void *,
415          struct audio_params *);
416 Static int      uaudio_trigger_input
417         (void *, void *, void *, int, void (*)(void *), void *,
418          struct audio_params *);
419 Static int      uaudio_halt_in_dma(void *);
420 Static int      uaudio_halt_out_dma(void *);
421 Static int      uaudio_getdev(void *, struct audio_device *);
422 Static int      uaudio_mixer_set_port(void *, mixer_ctrl_t *);
423 Static int      uaudio_mixer_get_port(void *, mixer_ctrl_t *);
424 Static int      uaudio_query_devinfo(void *, mixer_devinfo_t *);
425 Static int      uaudio_get_props(void *);
426
427 Static const struct audio_hw_if uaudio_hw_if = {
428         uaudio_open,
429         uaudio_close,
430         uaudio_drain,
431         uaudio_query_encoding,
432         uaudio_set_params,
433         uaudio_round_blocksize,
434         NULL,
435         NULL,
436         NULL,
437         NULL,
438         NULL,
439         uaudio_halt_out_dma,
440         uaudio_halt_in_dma,
441         NULL,
442         uaudio_getdev,
443         NULL,
444         uaudio_mixer_set_port,
445         uaudio_mixer_get_port,
446         uaudio_query_devinfo,
447         NULL,
448         NULL,
449         NULL,
450         NULL,
451         uaudio_get_props,
452         uaudio_trigger_output,
453         uaudio_trigger_input,
454         NULL,
455 };
456
457 Static struct audio_device uaudio_device = {
458         "USB audio",
459         "",
460         "uaudio"
461 };
462
463 #elif defined(__FreeBSD__) || defined(__DragonFly__)
464 Static int      audio_attach_mi(device_t);
465 Static int      uaudio_init_params(struct uaudio_softc * sc, struct chan *ch, int mode);
466 static int      uaudio_sndstat_prepare_pcm(struct sbuf *s, device_t dev, int verbose);
467
468 /* for NetBSD compatibirity */
469 #define AUMODE_PLAY     0x01
470 #define AUMODE_RECORD   0x02
471
472 #define AUDIO_PROP_FULLDUPLEX   0x01
473
474 #define AUDIO_ENCODING_ULAW             1
475 #define AUDIO_ENCODING_ALAW             2
476 #define AUDIO_ENCODING_SLINEAR_LE       6
477 #define AUDIO_ENCODING_SLINEAR_BE       7
478 #define AUDIO_ENCODING_ULINEAR_LE       8
479 #define AUDIO_ENCODING_ULINEAR_BE       9
480
481 #endif  /* FreeBSD || DragonFly */
482
483
484 #if defined(__NetBSD__) || defined(__OpenBSD__)
485
486 USB_DECLARE_DRIVER(uaudio);
487
488 #elif defined(__FreeBSD__) || defined(__DragonFly__)
489
490 USB_DECLARE_DRIVER_INIT(uaudio,
491                 DEVMETHOD(device_suspend, bus_generic_suspend),
492                 DEVMETHOD(device_resume, bus_generic_resume),
493                 DEVMETHOD(device_shutdown, bus_generic_shutdown),
494                 DEVMETHOD(bus_print_child, bus_generic_print_child)
495                 );
496 #endif
497
498
499 USB_MATCH(uaudio)
500 {
501         USB_MATCH_START(uaudio, uaa);
502         usb_interface_descriptor_t *id;
503
504         if (uaa->iface == NULL)
505                 return UMATCH_NONE;
506
507         id = usbd_get_interface_descriptor(uaa->iface);
508         /* Trigger on the control interface. */
509         if (id == NULL ||
510             id->bInterfaceClass != UICLASS_AUDIO ||
511             id->bInterfaceSubClass != UISUBCLASS_AUDIOCONTROL ||
512             (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_AUDIO))
513                 return UMATCH_NONE;
514
515         return UMATCH_IFACECLASS_IFACESUBCLASS;
516 }
517
518 USB_ATTACH(uaudio)
519 {
520         USB_ATTACH_START(uaudio, sc, uaa);
521         usb_interface_descriptor_t *id;
522         usb_config_descriptor_t *cdesc;
523         char devinfo[1024];
524         usbd_status err;
525         int i, j, found;
526
527 #if defined(__FreeBSD__) || defined(__DragonFly__)
528         usbd_devinfo(uaa->device, 0, devinfo);
529         USB_ATTACH_SETUP;
530 #else
531         usbd_devinfo(uaa->device, 0, devinfo, sizeof(devinfo));
532         kprintf(": %s\n", devinfo);
533 #endif
534
535         sc->sc_udev = uaa->device;
536
537         cdesc = usbd_get_config_descriptor(sc->sc_udev);
538         if (cdesc == NULL) {
539                 kprintf("%s: failed to get configuration descriptor\n",
540                        USBDEVNAME(sc->sc_dev));
541                 USB_ATTACH_ERROR_RETURN;
542         }
543
544         err = uaudio_identify(sc, cdesc);
545         if (err) {
546                 kprintf("%s: audio descriptors make no sense, error=%d\n",
547                        USBDEVNAME(sc->sc_dev), err);
548                 USB_ATTACH_ERROR_RETURN;
549         }
550
551         sc->sc_ac_ifaceh = uaa->iface;
552         /* Pick up the AS interface. */
553         for (i = 0; i < uaa->nifaces; i++) {
554                 if (uaa->ifaces[i] == NULL)
555                         continue;
556                 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
557                 if (id == NULL)
558                         continue;
559                 found = 0;
560                 for (j = 0; j < sc->sc_nalts; j++) {
561                         if (id->bInterfaceNumber ==
562                             sc->sc_alts[j].idesc->bInterfaceNumber) {
563                                 sc->sc_alts[j].ifaceh = uaa->ifaces[i];
564                                 found = 1;
565                         }
566                 }
567                 if (found)
568                         uaa->ifaces[i] = NULL;
569         }
570
571         for (j = 0; j < sc->sc_nalts; j++) {
572                 if (sc->sc_alts[j].ifaceh == NULL) {
573                         kprintf("%s: alt %d missing AS interface(s)\n",
574                             USBDEVNAME(sc->sc_dev), j);
575                         USB_ATTACH_ERROR_RETURN;
576                 }
577         }
578
579         kprintf("%s: audio rev %d.%02x\n", USBDEVNAME(sc->sc_dev),
580                sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff);
581
582         sc->sc_playchan.sc = sc->sc_recchan.sc = sc;
583         sc->sc_playchan.altidx = -1;
584         sc->sc_recchan.altidx = -1;
585
586         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC)
587                 sc->sc_altflags |= UA_NOFRAC;
588
589 #ifndef USB_DEBUG
590         if (bootverbose)
591 #endif
592                 kprintf("%s: %d mixer controls\n", USBDEVNAME(sc->sc_dev),
593                     sc->sc_nctls);
594
595 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
596         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
597                            USBDEV(sc->sc_dev));
598 #endif
599
600         DPRINTF(("uaudio_attach: doing audio_attach_mi\n"));
601 #if defined(__OpenBSD__)
602         audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev);
603 #elif defined(__NetBSD__)
604         sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, &sc->sc_dev);
605 #elif defined(__FreeBSD__) || defined(__DragonFly__)
606         sc->sc_dying = 0;
607         if (audio_attach_mi(sc->sc_dev)) {
608                 kprintf("audio_attach_mi failed\n");
609                 USB_ATTACH_ERROR_RETURN;
610         }
611 #endif
612
613         USB_ATTACH_SUCCESS_RETURN;
614 }
615
616 #if defined(__NetBSD__) || defined(__OpenBSD__)
617 int
618 uaudio_activate(device_ptr_t self, enum devact act)
619 {
620         struct uaudio_softc *sc;
621         int rv;
622
623         sc = (struct uaudio_softc *)self;
624         rv = 0;
625         switch (act) {
626         case DVACT_ACTIVATE:
627                 return EOPNOTSUPP;
628
629         case DVACT_DEACTIVATE:
630                 if (sc->sc_audiodev != NULL)
631                         rv = config_deactivate(sc->sc_audiodev);
632                 sc->sc_dying = 1;
633                 break;
634         }
635         return rv;
636 }
637 #endif
638
639 #if defined(__NetBSD__) || defined(__OpenBSD__)
640 int
641 uaudio_detach(device_ptr_t self, int flags)
642 {
643         struct uaudio_softc *sc;
644         int rv;
645
646         sc = (struct uaudio_softc *)self;
647         rv = 0;
648         /* Wait for outstanding requests to complete. */
649         usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
650
651         if (sc->sc_audiodev != NULL)
652                 rv = config_detach(sc->sc_audiodev, flags);
653
654         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
655                            USBDEV(sc->sc_dev));
656
657         return rv;
658 }
659 #elif defined(__FreeBSD__) || defined(__DragonFly__)
660
661 USB_DETACH(uaudio)
662 {
663         USB_DETACH_START(uaudio, sc);
664
665         sbuf_delete(&(sc->uaudio_sndstat));
666         sc->uaudio_sndstat_flag = 0;
667
668         sc->sc_dying = 1;
669
670 #if 0 /* XXX */
671         /* Wait for outstanding requests to complete. */
672         usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
673 #endif
674
675         /* do nothing ? */
676         return bus_generic_detach(sc->sc_dev);
677 }
678 #endif
679
680 #if defined(__NetBSD__) || defined(__OpenBSD__)
681 Static int
682 uaudio_query_encoding(void *addr, struct audio_encoding *fp)
683 {
684         struct uaudio_softc *sc;
685         int flags;
686         int idx;
687
688         sc = addr;
689         flags = sc->sc_altflags;
690         if (sc->sc_dying)
691                 return EIO;
692
693         if (sc->sc_nalts == 0 || flags == 0)
694                 return ENXIO;
695
696         idx = fp->index;
697         switch (idx) {
698         case 0:
699                 strlcpy(fp->name, AudioEulinear, sizeof(fp->name));
700                 fp->encoding = AUDIO_ENCODING_ULINEAR;
701                 fp->precision = 8;
702                 fp->flags = flags&HAS_8U ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
703                 return (0);
704         case 1:
705                 strlcpy(fp->name, AudioEmulaw, sizeof(fp->name));
706                 fp->encoding = AUDIO_ENCODING_ULAW;
707                 fp->precision = 8;
708                 fp->flags = flags&HAS_MULAW ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
709                 return (0);
710         case 2:
711                 strlcpy(fp->name, AudioEalaw, sizeof(fp->name));
712                 fp->encoding = AUDIO_ENCODING_ALAW;
713                 fp->precision = 8;
714                 fp->flags = flags&HAS_ALAW ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
715                 return (0);
716         case 3:
717                 strlcpy(fp->name, AudioEslinear, sizeof(fp->name));
718                 fp->encoding = AUDIO_ENCODING_SLINEAR;
719                 fp->precision = 8;
720                 fp->flags = flags&HAS_8 ? 0 : AUDIO_ENCODINGFLAG_EMULATED;
721                 return (0);
722         case 4:
723                 strlcpy(fp->name, AudioEslinear_le, sizeof(fp->name));
724                 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
725                 fp->precision = 16;
726                 fp->flags = 0;
727                 return (0);
728         case 5:
729                 strlcpy(fp->name, AudioEulinear_le, sizeof(fp->name));
730                 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
731                 fp->precision = 16;
732                 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
733                 return (0);
734         case 6:
735                 strlcpy(fp->name, AudioEslinear_be, sizeof(fp->name));
736                 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
737                 fp->precision = 16;
738                 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
739                 return (0);
740         case 7:
741                 strlcpy(fp->name, AudioEulinear_be, sizeof(fp->name));
742                 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
743                 fp->precision = 16;
744                 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
745                 return (0);
746         default:
747                 return (EINVAL);
748         }
749 }
750 #endif
751
752 Static const usb_interface_descriptor_t *
753 uaudio_find_iface(const char *buf, int size, int *offsp, int subtype)
754 {
755         const usb_interface_descriptor_t *d;
756
757         while (*offsp < size) {
758                 d = (const void *)(buf + *offsp);
759                 *offsp += d->bLength;
760                 if (d->bDescriptorType == UDESC_INTERFACE &&
761                     d->bInterfaceClass == UICLASS_AUDIO &&
762                     d->bInterfaceSubClass == subtype)
763                         return d;
764         }
765         return NULL;
766 }
767
768 Static void
769 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc)
770 {
771         int res;
772         size_t len;
773         struct mixerctl *nmc;
774
775 #if defined(__FreeBSD__) || defined(__DragonFly__)
776         if (mc->class < UAC_NCLASSES) {
777                 DPRINTF(("%s: adding %s.%d\n",
778                          __func__, uac_names[mc->class], mc->ctl));
779         } else {
780                 DPRINTF(("%s: adding %d\n", __func__, mc->ctl));
781         }
782 #else
783         if (mc->class < UAC_NCLASSES) {
784                 DPRINTF(("%s: adding %s.%s\n",
785                          __func__, uac_names[mc->class], mc->ctlname));
786         } else {
787                 DPRINTF(("%s: adding %s\n", __func__, mc->ctlname));
788         }
789 #endif
790         len = sizeof(*mc) * (sc->sc_nctls + 1);
791         nmc = kmalloc(len, M_USBDEV, M_NOWAIT);
792         if (nmc == NULL) {
793                 kprintf("uaudio_mixer_add_ctl: no memory\n");
794                 return;
795         }
796         /* Copy old data, if there was any */
797         if (sc->sc_nctls != 0) {
798                 memcpy(nmc, sc->sc_ctls, sizeof(*mc) * (sc->sc_nctls));
799                 kfree(sc->sc_ctls, M_USBDEV);
800         }
801         sc->sc_ctls = nmc;
802
803         mc->delta = 0;
804         if (mc->type == MIX_ON_OFF) {
805                 mc->minval = 0;
806                 mc->maxval = 1;
807         } else if (mc->type == MIX_SELECTOR) {
808                 ;
809         } else {
810                 /* Determine min and max values. */
811                 mc->minval = uaudio_signext(mc->type,
812                         uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE,
813                                    mc->wValue[0], mc->wIndex,
814                                    MIX_SIZE(mc->type)));
815                 mc->maxval = 1 + uaudio_signext(mc->type,
816                         uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE,
817                                    mc->wValue[0], mc->wIndex,
818                                    MIX_SIZE(mc->type)));
819                 mc->mul = mc->maxval - mc->minval;
820                 if (mc->mul == 0)
821                         mc->mul = 1;
822                 res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE,
823                                  mc->wValue[0], mc->wIndex,
824                                  MIX_SIZE(mc->type));
825                 if (res > 0)
826                         mc->delta = (res * 255 + mc->mul/2) / mc->mul;
827         }
828
829         sc->sc_ctls[sc->sc_nctls++] = *mc;
830
831 #ifdef USB_DEBUG
832         if (uaudiodebug > 2) {
833                 int i;
834                 DPRINTF(("uaudio_mixer_add_ctl: wValue=%04x",mc->wValue[0]));
835                 for (i = 1; i < mc->nchan; i++)
836                         DPRINTF((",%04x", mc->wValue[i]));
837 #if defined(__FreeBSD__) || defined(__DragonFly__)
838                 DPRINTF((" wIndex=%04x type=%d ctl='%d' "
839                          "min=%d max=%d\n",
840                          mc->wIndex, mc->type, mc->ctl,
841                          mc->minval, mc->maxval));
842 #else
843                 DPRINTF((" wIndex=%04x type=%d name='%s' unit='%s' "
844                          "min=%d max=%d\n",
845                          mc->wIndex, mc->type, mc->ctlname, mc->ctlunit,
846                          mc->minval, mc->maxval));
847 #endif
848         }
849 #endif
850 }
851
852 #if defined(__NetBSD__) || defined(__OpenBSD__)
853 Static char *
854 uaudio_id_name(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
855 {
856         static char buf[32];
857
858         ksnprintf(buf, sizeof(buf), "i%d", id);
859         return buf;
860 }
861 #endif
862
863 #ifdef USB_DEBUG
864 Static void
865 uaudio_dump_cluster(const struct usb_audio_cluster *cl)
866 {
867         static const char *channel_names[16] = {
868                 "LEFT", "RIGHT", "CENTER", "LFE",
869                 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
870                 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
871                 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
872         };
873         int cc, i, first;
874
875         cc = UGETW(cl->wChannelConfig);
876         logprintf("cluster: bNrChannels=%u wChannelConfig=0x%.4x",
877                   cl->bNrChannels, cc);
878         first = TRUE;
879         for (i = 0; cc != 0; i++) {
880                 if (cc & 1) {
881                         logprintf("%c%s", first ? '<' : ',', channel_names[i]);
882                         first = FALSE;
883                 }
884                 cc = cc >> 1;
885         }
886         logprintf("> iChannelNames=%u", cl->iChannelNames);
887 }
888 #endif
889
890 Static struct usb_audio_cluster
891 uaudio_get_cluster(int id, const struct io_terminal *iot)
892 {
893         struct usb_audio_cluster r;
894         const usb_descriptor_t *dp;
895         int i;
896
897         for (i = 0; i < 25; i++) { /* avoid infinite loops */
898                 dp = iot[id].d.desc;
899                 if (dp == 0)
900                         goto bad;
901                 switch (dp->bDescriptorSubtype) {
902                 case UDESCSUB_AC_INPUT:
903                         r.bNrChannels = iot[id].d.it->bNrChannels;
904                         USETW(r.wChannelConfig, UGETW(iot[id].d.it->wChannelConfig));
905                         r.iChannelNames = iot[id].d.it->iChannelNames;
906                         return r;
907                 case UDESCSUB_AC_OUTPUT:
908                         id = iot[id].d.ot->bSourceId;
909                         break;
910                 case UDESCSUB_AC_MIXER:
911                         r = *(const struct usb_audio_cluster *)
912                                 &iot[id].d.mu->baSourceId[iot[id].d.mu->bNrInPins];
913                         return r;
914                 case UDESCSUB_AC_SELECTOR:
915                         /* XXX This is not really right */
916                         id = iot[id].d.su->baSourceId[0];
917                         break;
918                 case UDESCSUB_AC_FEATURE:
919                         id = iot[id].d.fu->bSourceId;
920                         break;
921                 case UDESCSUB_AC_PROCESSING:
922                         r = *(const struct usb_audio_cluster *)
923                                 &iot[id].d.pu->baSourceId[iot[id].d.pu->bNrInPins];
924                         return r;
925                 case UDESCSUB_AC_EXTENSION:
926                         r = *(const struct usb_audio_cluster *)
927                                 &iot[id].d.eu->baSourceId[iot[id].d.eu->bNrInPins];
928                         return r;
929                 default:
930                         goto bad;
931                 }
932         }
933  bad:
934         kprintf("uaudio_get_cluster: bad data\n");
935         memset(&r, 0, sizeof r);
936         return r;
937
938 }
939
940 Static void
941 uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
942 {
943 #ifdef USB_DEBUG
944         const struct usb_audio_input_terminal *d = iot[id].d.it;
945
946         DPRINTFN(2,("uaudio_add_input: bTerminalId=%d wTerminalType=0x%04x "
947                     "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
948                     "iChannelNames=%d iTerminal=%d\n",
949                     d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
950                     d->bNrChannels, UGETW(d->wChannelConfig),
951                     d->iChannelNames, d->iTerminal));
952 #endif
953 }
954
955 Static void
956 uaudio_add_output(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
957 {
958 #ifdef USB_DEBUG
959         const struct usb_audio_output_terminal *d;
960
961         d = iot[id].d.ot;
962         DPRINTFN(2,("uaudio_add_output: bTerminalId=%d wTerminalType=0x%04x "
963                     "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
964                     d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
965                     d->bSourceId, d->iTerminal));
966 #endif
967 }
968
969 Static void
970 uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
971 {
972         const struct usb_audio_mixer_unit *d = iot[id].d.mu;
973         const struct usb_audio_mixer_unit_1 *d1;
974         int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k;
975         const uByte *bm;
976         struct mixerctl mix;
977
978         DPRINTFN(2,("uaudio_add_mixer: bUnitId=%d bNrInPins=%d\n",
979                     d->bUnitId, d->bNrInPins));
980
981         /* Compute the number of input channels */
982         ichs = 0;
983         for (i = 0; i < d->bNrInPins; i++)
984                 ichs += uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
985
986         /* and the number of output channels */
987         d1 = (const struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins];
988         ochs = d1->bNrChannels;
989         DPRINTFN(2,("uaudio_add_mixer: ichs=%d ochs=%d\n", ichs, ochs));
990
991         bm = d1->bmControls;
992         mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
993         uaudio_determine_class(&iot[id], &mix);
994         mix.type = MIX_SIGNED_16;
995 #if !defined(__FreeBSD__) && !defined(__DragonFly__)    /* XXXXX */
996         mix.ctlunit = AudioNvolume;
997 #endif
998
999 #define BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1)
1000         for (p = i = 0; i < d->bNrInPins; i++) {
1001                 chs = uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
1002                 mc = 0;
1003                 for (c = 0; c < chs; c++) {
1004                         mo = 0;
1005                         for (o = 0; o < ochs; o++) {
1006                                 bno = (p + c) * ochs + o;
1007                                 if (BIT(bno))
1008                                         mo++;
1009                         }
1010                         if (mo == 1)
1011                                 mc++;
1012                 }
1013                 if (mc == chs && chs <= MIX_MAX_CHAN) {
1014                         k = 0;
1015                         for (c = 0; c < chs; c++)
1016                                 for (o = 0; o < ochs; o++) {
1017                                         bno = (p + c) * ochs + o;
1018                                         if (BIT(bno))
1019                                                 mix.wValue[k++] =
1020                                                         MAKE(p+c+1, o+1);
1021                                 }
1022 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1023                         ksnprintf(mix.ctlname, sizeof(mix.ctlname), "mix%d-%s",
1024                             d->bUnitId, uaudio_id_name(sc, iot,
1025                             d->baSourceId[i]));
1026 #endif
1027                         mix.nchan = chs;
1028                         uaudio_mixer_add_ctl(sc, &mix);
1029                 } else {
1030                         /* XXX */
1031                 }
1032 #undef BIT
1033                 p += chs;
1034         }
1035
1036 }
1037
1038 Static void
1039 uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1040 {
1041         const struct usb_audio_selector_unit *d;
1042         struct mixerctl mix;
1043 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1044         int i, wp;
1045 #else
1046         int i;
1047         struct mixerctl dummy;
1048 #endif
1049
1050         d = iot[id].d.su;
1051         DPRINTFN(2,("uaudio_add_selector: bUnitId=%d bNrInPins=%d\n",
1052                     d->bUnitId, d->bNrInPins));
1053         mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1054         mix.wValue[0] = MAKE(0, 0);
1055         uaudio_determine_class(&iot[id], &mix);
1056         mix.nchan = 1;
1057         mix.type = MIX_SELECTOR;
1058 #if defined(__FreeBSD__) || defined(__DragonFly__)
1059         mix.ctl = SOUND_MIXER_NRDEVICES;        /* XXXXX */
1060         mix.minval = 1;
1061         mix.maxval = d->bNrInPins;
1062         mix.mul = mix.maxval - mix.minval;
1063         for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) {
1064                 mix.slctrtype[i] = SOUND_MIXER_NRDEVICES;
1065         }
1066         for (i = mix.minval; i <= mix.maxval; i++) {
1067                 mix.slctrtype[i - 1] = uaudio_feature_name(&iot[d->baSourceId[i - 1]], &dummy);
1068         }
1069 #else
1070         mix.ctlunit = "";
1071         mix.minval = 1;
1072         mix.maxval = d->bNrInPins;
1073         mix.mul = mix.maxval - mix.minval;
1074         wp = ksnprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId);
1075         for (i = 1; i <= d->bNrInPins; i++) {
1076                 wp += ksnprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp,
1077                                "i%d", d->baSourceId[i - 1]);
1078                 if (wp > MAX_AUDIO_DEV_LEN - 1)
1079                         break;
1080         }
1081 #endif
1082         uaudio_mixer_add_ctl(sc, &mix);
1083 }
1084
1085 #ifdef USB_DEBUG
1086 Static const char *
1087 uaudio_get_terminal_name(int terminal_type)
1088 {
1089         static char buf[100];
1090
1091         switch (terminal_type) {
1092         /* USB terminal types */
1093         case UAT_UNDEFINED:     return "UAT_UNDEFINED";
1094         case UAT_STREAM:        return "UAT_STREAM";
1095         case UAT_VENDOR:        return "UAT_VENDOR";
1096         /* input terminal types */
1097         case UATI_UNDEFINED:    return "UATI_UNDEFINED";
1098         case UATI_MICROPHONE:   return "UATI_MICROPHONE";
1099         case UATI_DESKMICROPHONE:       return "UATI_DESKMICROPHONE";
1100         case UATI_PERSONALMICROPHONE:   return "UATI_PERSONALMICROPHONE";
1101         case UATI_OMNIMICROPHONE:       return "UATI_OMNIMICROPHONE";
1102         case UATI_MICROPHONEARRAY:      return "UATI_MICROPHONEARRAY";
1103         case UATI_PROCMICROPHONEARR:    return "UATI_PROCMICROPHONEARR";
1104         /* output terminal types */
1105         case UATO_UNDEFINED:    return "UATO_UNDEFINED";
1106         case UATO_SPEAKER:      return "UATO_SPEAKER";
1107         case UATO_HEADPHONES:   return "UATO_HEADPHONES";
1108         case UATO_DISPLAYAUDIO: return "UATO_DISPLAYAUDIO";
1109         case UATO_DESKTOPSPEAKER:       return "UATO_DESKTOPSPEAKER";
1110         case UATO_ROOMSPEAKER:  return "UATO_ROOMSPEAKER";
1111         case UATO_COMMSPEAKER:  return "UATO_COMMSPEAKER";
1112         case UATO_SUBWOOFER:    return "UATO_SUBWOOFER";
1113         /* bidir terminal types */
1114         case UATB_UNDEFINED:    return "UATB_UNDEFINED";
1115         case UATB_HANDSET:      return "UATB_HANDSET";
1116         case UATB_HEADSET:      return "UATB_HEADSET";
1117         case UATB_SPEAKERPHONE: return "UATB_SPEAKERPHONE";
1118         case UATB_SPEAKERPHONEESUP:     return "UATB_SPEAKERPHONEESUP";
1119         case UATB_SPEAKERPHONEECANC:    return "UATB_SPEAKERPHONEECANC";
1120         /* telephony terminal types */
1121         case UATT_UNDEFINED:    return "UATT_UNDEFINED";
1122         case UATT_PHONELINE:    return "UATT_PHONELINE";
1123         case UATT_TELEPHONE:    return "UATT_TELEPHONE";
1124         case UATT_DOWNLINEPHONE:        return "UATT_DOWNLINEPHONE";
1125         /* external terminal types */
1126         case UATE_UNDEFINED:    return "UATE_UNDEFINED";
1127         case UATE_ANALOGCONN:   return "UATE_ANALOGCONN";
1128         case UATE_LINECONN:     return "UATE_LINECONN";
1129         case UATE_LEGACYCONN:   return "UATE_LEGACYCONN";
1130         case UATE_DIGITALAUIFC: return "UATE_DIGITALAUIFC";
1131         case UATE_SPDIF:        return "UATE_SPDIF";
1132         case UATE_1394DA:       return "UATE_1394DA";
1133         case UATE_1394DV:       return "UATE_1394DV";
1134         /* embedded function terminal types */
1135         case UATF_UNDEFINED:    return "UATF_UNDEFINED";
1136         case UATF_CALIBNOISE:   return "UATF_CALIBNOISE";
1137         case UATF_EQUNOISE:     return "UATF_EQUNOISE";
1138         case UATF_CDPLAYER:     return "UATF_CDPLAYER";
1139         case UATF_DAT:  return "UATF_DAT";
1140         case UATF_DCC:  return "UATF_DCC";
1141         case UATF_MINIDISK:     return "UATF_MINIDISK";
1142         case UATF_ANALOGTAPE:   return "UATF_ANALOGTAPE";
1143         case UATF_PHONOGRAPH:   return "UATF_PHONOGRAPH";
1144         case UATF_VCRAUDIO:     return "UATF_VCRAUDIO";
1145         case UATF_VIDEODISCAUDIO:       return "UATF_VIDEODISCAUDIO";
1146         case UATF_DVDAUDIO:     return "UATF_DVDAUDIO";
1147         case UATF_TVTUNERAUDIO: return "UATF_TVTUNERAUDIO";
1148         case UATF_SATELLITE:    return "UATF_SATELLITE";
1149         case UATF_CABLETUNER:   return "UATF_CABLETUNER";
1150         case UATF_DSS:  return "UATF_DSS";
1151         case UATF_RADIORECV:    return "UATF_RADIORECV";
1152         case UATF_RADIOXMIT:    return "UATF_RADIOXMIT";
1153         case UATF_MULTITRACK:   return "UATF_MULTITRACK";
1154         case UATF_SYNTHESIZER:  return "UATF_SYNTHESIZER";
1155         default:
1156                 ksnprintf(buf, sizeof(buf), "unknown type (0x%.4x)", terminal_type);
1157                 return buf;
1158         }
1159 }
1160 #endif
1161
1162 Static int
1163 uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix)
1164 {
1165         int terminal_type;
1166
1167         if (iot == NULL || iot->output == NULL) {
1168                 mix->class = UAC_OUTPUT;
1169                 return 0;
1170         }
1171         terminal_type = 0;
1172         if (iot->output->size == 1)
1173                 terminal_type = iot->output->terminals[0];
1174         /*
1175          * If the only output terminal is USB,
1176          * the class is UAC_RECORD.
1177          */
1178         if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
1179                 mix->class = UAC_RECORD;
1180                 if (iot->inputs_size == 1
1181                     && iot->inputs[0] != NULL
1182                     && iot->inputs[0]->size == 1)
1183                         return iot->inputs[0]->terminals[0];
1184                 else
1185                         return 0;
1186         }
1187         /*
1188          * If the ultimate destination of the unit is just one output
1189          * terminal and the unit is connected to the output terminal
1190          * directly, the class is UAC_OUTPUT.
1191          */
1192         if (terminal_type != 0 && iot->direct) {
1193                 mix->class = UAC_OUTPUT;
1194                 return terminal_type;
1195         }
1196         /*
1197          * If the unit is connected to just one input terminal,
1198          * the class is UAC_INPUT.
1199          */
1200         if (iot->inputs_size == 1 && iot->inputs[0] != NULL
1201             && iot->inputs[0]->size == 1) {
1202                 mix->class = UAC_INPUT;
1203                 return iot->inputs[0]->terminals[0];
1204         }
1205         /*
1206          * Otherwise, the class is UAC_OUTPUT.
1207          */
1208         mix->class = UAC_OUTPUT;
1209         return terminal_type;
1210 }
1211
1212 #if defined(__FreeBSD__) || defined(__DragonFly__)
1213 const int 
1214 uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix)
1215 {
1216         int terminal_type;
1217
1218         terminal_type = uaudio_determine_class(iot, mix);
1219         if (mix->class == UAC_RECORD && terminal_type == 0)
1220                 return SOUND_MIXER_IMIX;
1221         DPRINTF(("%s: terminal_type=%s\n", __func__,
1222                  uaudio_get_terminal_name(terminal_type)));
1223         switch (terminal_type) {
1224         case UAT_STREAM:
1225                 return SOUND_MIXER_PCM;
1226
1227         case UATI_MICROPHONE:
1228         case UATI_DESKMICROPHONE:
1229         case UATI_PERSONALMICROPHONE:
1230         case UATI_OMNIMICROPHONE:
1231         case UATI_MICROPHONEARRAY:
1232         case UATI_PROCMICROPHONEARR:
1233                 return SOUND_MIXER_MIC;
1234
1235         case UATO_SPEAKER:
1236         case UATO_DESKTOPSPEAKER:
1237         case UATO_ROOMSPEAKER:
1238         case UATO_COMMSPEAKER:
1239                 return SOUND_MIXER_SPEAKER;
1240
1241         case UATE_ANALOGCONN:
1242         case UATE_LINECONN:
1243         case UATE_LEGACYCONN:
1244                 return SOUND_MIXER_LINE;
1245
1246         case UATE_DIGITALAUIFC:
1247         case UATE_SPDIF:
1248         case UATE_1394DA:
1249         case UATE_1394DV:
1250                 return SOUND_MIXER_ALTPCM;
1251
1252         case UATF_CDPLAYER:
1253                 return SOUND_MIXER_CD;
1254
1255         case UATF_SYNTHESIZER:
1256                 return SOUND_MIXER_SYNTH;
1257
1258         case UATF_VIDEODISCAUDIO:
1259         case UATF_DVDAUDIO:
1260         case UATF_TVTUNERAUDIO:
1261                 return SOUND_MIXER_VIDEO;
1262
1263 /* telephony terminal types */
1264         case UATT_UNDEFINED:
1265         case UATT_PHONELINE:
1266         case UATT_TELEPHONE:
1267         case UATT_DOWNLINEPHONE:
1268                 return SOUND_MIXER_PHONEIN;
1269 /*              return SOUND_MIXER_PHONEOUT;*/
1270
1271         case UATF_RADIORECV:
1272         case UATF_RADIOXMIT:
1273                 return SOUND_MIXER_RADIO;
1274
1275         case UAT_UNDEFINED:
1276         case UAT_VENDOR:
1277         case UATI_UNDEFINED:
1278 /* output terminal types */
1279         case UATO_UNDEFINED:
1280         case UATO_DISPLAYAUDIO:
1281         case UATO_SUBWOOFER:
1282         case UATO_HEADPHONES:
1283 /* bidir terminal types */
1284         case UATB_UNDEFINED:
1285         case UATB_HANDSET:
1286         case UATB_HEADSET:
1287         case UATB_SPEAKERPHONE:
1288         case UATB_SPEAKERPHONEESUP:
1289         case UATB_SPEAKERPHONEECANC:
1290 /* external terminal types */
1291         case UATE_UNDEFINED:
1292 /* embedded function terminal types */
1293         case UATF_UNDEFINED:
1294         case UATF_CALIBNOISE:
1295         case UATF_EQUNOISE:
1296         case UATF_DAT:
1297         case UATF_DCC:
1298         case UATF_MINIDISK:
1299         case UATF_ANALOGTAPE:
1300         case UATF_PHONOGRAPH:
1301         case UATF_VCRAUDIO:
1302         case UATF_SATELLITE:
1303         case UATF_CABLETUNER:
1304         case UATF_DSS:
1305         case UATF_MULTITRACK:
1306         case 0xffff:
1307         default:
1308                 DPRINTF(("%s: 'master' for 0x%.4x\n", __func__, terminal_type));
1309                 return SOUND_MIXER_VOLUME;
1310         }
1311         return SOUND_MIXER_VOLUME;
1312 }
1313 #else
1314 Static const char *
1315 uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix)
1316 {
1317         int terminal_type;
1318
1319         terminal_type = uaudio_determine_class(iot, mix);
1320         if (mix->class == UAC_RECORD && terminal_type == 0)
1321                 return AudioNmixerout;
1322         DPRINTF(("%s: terminal_type=%s\n", __func__,
1323                  uaudio_get_terminal_name(terminal_type)));
1324         switch (terminal_type) {
1325         case UAT_STREAM:
1326                 return AudioNdac;
1327
1328         case UATI_MICROPHONE:
1329         case UATI_DESKMICROPHONE:
1330         case UATI_PERSONALMICROPHONE:
1331         case UATI_OMNIMICROPHONE:
1332         case UATI_MICROPHONEARRAY:
1333         case UATI_PROCMICROPHONEARR:
1334                 return AudioNmicrophone;
1335
1336         case UATO_SPEAKER:
1337         case UATO_DESKTOPSPEAKER:
1338         case UATO_ROOMSPEAKER:
1339         case UATO_COMMSPEAKER:
1340                 return AudioNspeaker;
1341
1342         case UATO_HEADPHONES:
1343                 return AudioNheadphone;
1344
1345         case UATO_SUBWOOFER:
1346                 return AudioNlfe;
1347
1348         /* telephony terminal types */
1349         case UATT_UNDEFINED:
1350         case UATT_PHONELINE:
1351         case UATT_TELEPHONE:
1352         case UATT_DOWNLINEPHONE:
1353                 return "phone";
1354
1355         case UATE_ANALOGCONN:
1356         case UATE_LINECONN:
1357         case UATE_LEGACYCONN:
1358                 return AudioNline;
1359
1360         case UATE_DIGITALAUIFC:
1361         case UATE_SPDIF:
1362         case UATE_1394DA:
1363         case UATE_1394DV:
1364                 return AudioNaux;
1365
1366         case UATF_CDPLAYER:
1367                 return AudioNcd;
1368
1369         case UATF_SYNTHESIZER:
1370                 return AudioNfmsynth;
1371
1372         case UATF_VIDEODISCAUDIO:
1373         case UATF_DVDAUDIO:
1374         case UATF_TVTUNERAUDIO:
1375                 return AudioNvideo;
1376
1377         case UAT_UNDEFINED:
1378         case UAT_VENDOR:
1379         case UATI_UNDEFINED:
1380 /* output terminal types */
1381         case UATO_UNDEFINED:
1382         case UATO_DISPLAYAUDIO:
1383 /* bidir terminal types */
1384         case UATB_UNDEFINED:
1385         case UATB_HANDSET:
1386         case UATB_HEADSET:
1387         case UATB_SPEAKERPHONE:
1388         case UATB_SPEAKERPHONEESUP:
1389         case UATB_SPEAKERPHONEECANC:
1390 /* external terminal types */
1391         case UATE_UNDEFINED:
1392 /* embedded function terminal types */
1393         case UATF_UNDEFINED:
1394         case UATF_CALIBNOISE:
1395         case UATF_EQUNOISE:
1396         case UATF_DAT:
1397         case UATF_DCC:
1398         case UATF_MINIDISK:
1399         case UATF_ANALOGTAPE:
1400         case UATF_PHONOGRAPH:
1401         case UATF_VCRAUDIO:
1402         case UATF_SATELLITE:
1403         case UATF_CABLETUNER:
1404         case UATF_DSS:
1405         case UATF_RADIORECV:
1406         case UATF_RADIOXMIT:
1407         case UATF_MULTITRACK:
1408         case 0xffff:
1409         default:
1410                 DPRINTF(("%s: 'master' for 0x%.4x\n", __func__, terminal_type));
1411                 return AudioNmaster;
1412         }
1413         return AudioNmaster;
1414 }
1415 #endif
1416
1417 Static void
1418 uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1419 {
1420         const struct usb_audio_feature_unit *d;
1421         const uByte *ctls;
1422         int ctlsize;
1423         int nchan;
1424         u_int fumask, mmask, cmask;
1425         struct mixerctl mix;
1426         int chan, ctl, i, unit;
1427 #if defined(__FreeBSD__) || defined(__DragonFly__)
1428         int mixernumber;
1429 #else
1430         const char *mixername;
1431 #endif
1432
1433 #define GET(i) (ctls[(i)*ctlsize] | \
1434                 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0))
1435         d = iot[id].d.fu;
1436         ctls = d->bmaControls;
1437         ctlsize = d->bControlSize;
1438         nchan = (d->bLength - 7) / ctlsize;
1439         mmask = GET(0);
1440         /* Figure out what we can control */
1441         for (cmask = 0, chan = 1; chan < nchan; chan++) {
1442                 DPRINTFN(9,("uaudio_add_feature: chan=%d mask=%x\n",
1443                             chan, GET(chan)));
1444                 cmask |= GET(chan);
1445         }
1446
1447 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1448         DPRINTFN(1,("uaudio_add_feature: bUnitId=%d, "
1449                     "%d channels, mmask=0x%04x, cmask=0x%04x\n",
1450                     d->bUnitId, nchan, mmask, cmask));
1451 #endif
1452
1453         if (nchan > MIX_MAX_CHAN)
1454                 nchan = MIX_MAX_CHAN;
1455         unit = d->bUnitId;
1456         mix.wIndex = MAKE(unit, sc->sc_ac_iface);
1457         for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) {
1458                 fumask = FU_MASK(ctl);
1459                 DPRINTFN(4,("uaudio_add_feature: ctl=%d fumask=0x%04x\n",
1460                             ctl, fumask));
1461                 if (mmask & fumask) {
1462                         mix.nchan = 1;
1463                         mix.wValue[0] = MAKE(ctl, 0);
1464                 } else if (cmask & fumask) {
1465                         mix.nchan = nchan - 1;
1466                         for (i = 1; i < nchan; i++) {
1467                                 if (GET(i) & fumask)
1468                                         mix.wValue[i-1] = MAKE(ctl, i);
1469                                 else
1470                                         mix.wValue[i-1] = -1;
1471                         }
1472                 } else {
1473                         continue;
1474                 }
1475 #undef GET
1476
1477 #if defined(__FreeBSD__) || defined(__DragonFly__)
1478                 mixernumber = uaudio_feature_name(&iot[id], &mix);
1479 #else
1480                 mixername = uaudio_feature_name(&iot[id], &mix);
1481 #endif
1482                 switch (ctl) {
1483                 case MUTE_CONTROL:
1484                         mix.type = MIX_ON_OFF;
1485 #if defined(__FreeBSD__) || defined(__DragonFly__)
1486                         mix.ctl = SOUND_MIXER_NRDEVICES;
1487 #else
1488                         mix.ctlunit = "";
1489                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1490                                  "%s.%s", mixername, AudioNmute);
1491 #endif
1492                         break;
1493                 case VOLUME_CONTROL:
1494                         mix.type = MIX_SIGNED_16;
1495 #if defined(__FreeBSD__) || defined(__DragonFly__)
1496                         mix.ctl = mixernumber;
1497 #else
1498                         mix.ctlunit = AudioNvolume;
1499                         strlcpy(mix.ctlname, mixername, sizeof(mix.ctlname));
1500 #endif
1501                         break;
1502                 case BASS_CONTROL:
1503                         mix.type = MIX_SIGNED_8;
1504 #if defined(__FreeBSD__) || defined(__DragonFly__)
1505                         mix.ctl = SOUND_MIXER_BASS;
1506 #else
1507                         mix.ctlunit = AudioNbass;
1508                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1509                                  "%s.%s", mixername, AudioNbass);
1510 #endif
1511                         break;
1512                 case MID_CONTROL:
1513                         mix.type = MIX_SIGNED_8;
1514 #if defined(__FreeBSD__) || defined(__DragonFly__)
1515                         mix.ctl = SOUND_MIXER_NRDEVICES;        /* XXXXX */
1516 #else
1517                         mix.ctlunit = AudioNmid;
1518                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1519                                  "%s.%s", mixername, AudioNmid);
1520 #endif
1521                         break;
1522                 case TREBLE_CONTROL:
1523                         mix.type = MIX_SIGNED_8;
1524 #if defined(__FreeBSD__) || defined(__DragonFly__)
1525                         mix.ctl = SOUND_MIXER_TREBLE;
1526 #else
1527                         mix.ctlunit = AudioNtreble;
1528                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1529                                  "%s.%s", mixername, AudioNtreble);
1530 #endif
1531                         break;
1532                 case GRAPHIC_EQUALIZER_CONTROL:
1533                         continue; /* XXX don't add anything */
1534                         break;
1535                 case AGC_CONTROL:
1536                         mix.type = MIX_ON_OFF;
1537 #if defined(__FreeBSD__) || defined(__DragonFly__)
1538                         mix.ctl = SOUND_MIXER_NRDEVICES;        /* XXXXX */
1539 #else
1540                         mix.ctlunit = "";
1541                         ksnprintf(mix.ctlname, sizeof(mix.ctlname), "%s.%s",
1542                                  mixername, AudioNagc);
1543 #endif
1544                         break;
1545                 case DELAY_CONTROL:
1546                         mix.type = MIX_UNSIGNED_16;
1547 #if defined(__FreeBSD__) || defined(__DragonFly__)
1548                         mix.ctl = SOUND_MIXER_NRDEVICES;        /* XXXXX */
1549 #else
1550                         mix.ctlunit = "4 ms";
1551                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1552                                  "%s.%s", mixername, AudioNdelay);
1553 #endif
1554                         break;
1555                 case BASS_BOOST_CONTROL:
1556                         mix.type = MIX_ON_OFF;
1557 #if defined(__FreeBSD__) || defined(__DragonFly__)
1558                         mix.ctl = SOUND_MIXER_NRDEVICES;        /* XXXXX */
1559 #else
1560                         mix.ctlunit = "";
1561                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1562                                  "%s.%s", mixername, AudioNbassboost);
1563 #endif
1564                         break;
1565                 case LOUDNESS_CONTROL:
1566                         mix.type = MIX_ON_OFF;
1567 #if defined(__FreeBSD__) || defined(__DragonFly__)
1568                         mix.ctl = SOUND_MIXER_LOUD;     /* Is this correct ? */
1569 #else
1570                         mix.ctlunit = "";
1571                         ksnprintf(mix.ctlname, sizeof(mix.ctlname),
1572                                  "%s.%s", mixername, AudioNloudness);
1573 #endif
1574                         break;
1575                 }
1576                 uaudio_mixer_add_ctl(sc, &mix);
1577         }
1578 }
1579
1580 Static void
1581 uaudio_add_processing_updown(struct uaudio_softc *sc,
1582                              const struct io_terminal *iot, int id)
1583 {
1584         const struct usb_audio_processing_unit *d;
1585         const struct usb_audio_processing_unit_1 *d1;
1586         const struct usb_audio_processing_unit_updown *ud;
1587         struct mixerctl mix;
1588         int i;
1589
1590         d = iot[id].d.pu;
1591         d1 = (const struct usb_audio_processing_unit_1 *)
1592                 &d->baSourceId[d->bNrInPins];
1593         ud = (const struct usb_audio_processing_unit_updown *)
1594                 &d1->bmControls[d1->bControlSize];
1595         DPRINTFN(2,("uaudio_add_processing_updown: bUnitId=%d bNrModes=%d\n",
1596                     d->bUnitId, ud->bNrModes));
1597
1598         if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
1599                 DPRINTF(("uaudio_add_processing_updown: no mode select\n"));
1600                 return;
1601         }
1602
1603         mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1604         mix.nchan = 1;
1605         mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0);
1606         uaudio_determine_class(&iot[id], &mix);
1607         mix.type = MIX_ON_OFF;  /* XXX */
1608 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1609         mix.ctlunit = "";
1610         ksnprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId);
1611 #endif
1612
1613         for (i = 0; i < ud->bNrModes; i++) {
1614                 DPRINTFN(2,("uaudio_add_processing_updown: i=%d bm=0x%x\n",
1615                             i, UGETW(ud->waModes[i])));
1616                 /* XXX */
1617         }
1618         uaudio_mixer_add_ctl(sc, &mix);
1619 }
1620
1621 Static void
1622 uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1623 {
1624         const struct usb_audio_processing_unit *d;
1625         const struct usb_audio_processing_unit_1 *d1;
1626         int ptype;
1627         struct mixerctl mix;
1628
1629         d = iot[id].d.pu;
1630         d1 = (const struct usb_audio_processing_unit_1 *)
1631                 &d->baSourceId[d->bNrInPins];
1632         ptype = UGETW(d->wProcessType);
1633         DPRINTFN(2,("uaudio_add_processing: wProcessType=%d bUnitId=%d "
1634                     "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins));
1635
1636         if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
1637                 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1638                 mix.nchan = 1;
1639                 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0);
1640                 uaudio_determine_class(&iot[id], &mix);
1641                 mix.type = MIX_ON_OFF;
1642 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1643                 mix.ctlunit = "";
1644                 ksnprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable",
1645                     d->bUnitId, ptype);
1646 #endif
1647                 uaudio_mixer_add_ctl(sc, &mix);
1648         }
1649
1650         switch(ptype) {
1651         case UPDOWNMIX_PROCESS:
1652                 uaudio_add_processing_updown(sc, iot, id);
1653                 break;
1654         case DOLBY_PROLOGIC_PROCESS:
1655         case P3D_STEREO_EXTENDER_PROCESS:
1656         case REVERBATION_PROCESS:
1657         case CHORUS_PROCESS:
1658         case DYN_RANGE_COMP_PROCESS:
1659         default:
1660 #ifdef USB_DEBUG
1661                 kprintf("uaudio_add_processing: unit %d, type=%d not impl.\n",
1662                        d->bUnitId, ptype);
1663 #endif
1664                 break;
1665         }
1666 }
1667
1668 Static void
1669 uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1670 {
1671         const struct usb_audio_extension_unit *d;
1672         const struct usb_audio_extension_unit_1 *d1;
1673         struct mixerctl mix;
1674
1675         d = iot[id].d.eu;
1676         d1 = (const struct usb_audio_extension_unit_1 *)
1677                 &d->baSourceId[d->bNrInPins];
1678         DPRINTFN(2,("uaudio_add_extension: bUnitId=%d bNrInPins=%d\n",
1679                     d->bUnitId, d->bNrInPins));
1680
1681         if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU)
1682                 return;
1683
1684         if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
1685                 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1686                 mix.nchan = 1;
1687                 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0);
1688                 uaudio_determine_class(&iot[id], &mix);
1689                 mix.type = MIX_ON_OFF;
1690 #if !defined(__FreeBSD__) && !defined(__DragonFly__)
1691                 mix.ctlunit = "";
1692                 ksnprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable",
1693                     d->bUnitId);
1694 #endif
1695                 uaudio_mixer_add_ctl(sc, &mix);
1696         }
1697 }
1698
1699 Static struct terminal_list*
1700 uaudio_merge_terminal_list(const struct io_terminal *iot)
1701 {
1702         struct terminal_list *tml;
1703         uint16_t *ptm;
1704         int i, len;
1705
1706         len = 0;
1707         if (iot->inputs == NULL)
1708                 return NULL;
1709         for (i = 0; i < iot->inputs_size; i++) {
1710                 if (iot->inputs[i] != NULL)
1711                         len += iot->inputs[i]->size;
1712         }
1713         tml = kmalloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT);
1714         if (tml == NULL) {
1715                 kprintf("uaudio_merge_terminal_list: no memory\n");
1716                 return NULL;
1717         }
1718         tml->size = 0;
1719         ptm = tml->terminals;
1720         for (i = 0; i < iot->inputs_size; i++) {
1721                 if (iot->inputs[i] == NULL)
1722                         continue;
1723                 if (iot->inputs[i]->size > len)
1724                         break;
1725                 memcpy(ptm, iot->inputs[i]->terminals,
1726                        iot->inputs[i]->size * sizeof(uint16_t));
1727                 tml->size += iot->inputs[i]->size;
1728                 ptm += iot->inputs[i]->size;
1729                 len -= iot->inputs[i]->size;
1730         }
1731         return tml;
1732 }
1733
1734 Static struct terminal_list *
1735 uaudio_io_terminaltype(int outtype, struct io_terminal *iot, int id)
1736 {
1737         struct terminal_list *tml;
1738         struct io_terminal *it;
1739         int src_id, i;
1740
1741         it = &iot[id];
1742         if (it->output != NULL) {
1743                 /* already has outtype? */
1744                 for (i = 0; i < it->output->size; i++)
1745                         if (it->output->terminals[i] == outtype)
1746                                 return uaudio_merge_terminal_list(it);
1747                 tml = kmalloc(TERMINAL_LIST_SIZE(it->output->size + 1),
1748                              M_TEMP, M_NOWAIT);
1749                 if (tml == NULL) {
1750                         kprintf("uaudio_io_terminaltype: no memory\n");
1751                         return uaudio_merge_terminal_list(it);
1752                 }
1753                 memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size));
1754                 tml->terminals[it->output->size] = outtype;
1755                 tml->size++;
1756                 kfree(it->output, M_TEMP);
1757                 it->output = tml;
1758                 if (it->inputs != NULL) {
1759                         for (i = 0; i < it->inputs_size; i++)
1760                                 if (it->inputs[i] != NULL)
1761                                         kfree(it->inputs[i], M_TEMP);
1762                         kfree(it->inputs, M_TEMP);
1763                 }
1764                 it->inputs_size = 0;
1765                 it->inputs = NULL;
1766         } else {                /* end `iot[id] != NULL' */
1767                 it->inputs_size = 0;
1768                 it->inputs = NULL;
1769                 it->output = kmalloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1770                 if (it->output == NULL) {
1771                         kprintf("uaudio_io_terminaltype: no memory\n");
1772                         return NULL;
1773                 }
1774                 it->output->terminals[0] = outtype;
1775                 it->output->size = 1;
1776                 it->direct = FALSE;
1777         }
1778
1779         switch (it->d.desc->bDescriptorSubtype) {
1780         case UDESCSUB_AC_INPUT:
1781                 it->inputs = kmalloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1782                 if (it->inputs == NULL) {
1783                         kprintf("uaudio_io_terminaltype: no memory\n");
1784                         return NULL;
1785                 }
1786                 tml = kmalloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1787                 if (tml == NULL) {
1788                         kprintf("uaudio_io_terminaltype: no memory\n");
1789                         kfree(it->inputs, M_TEMP);
1790                         it->inputs = NULL;
1791                         return NULL;
1792                 }
1793                 it->inputs[0] = tml;
1794                 tml->terminals[0] = UGETW(it->d.it->wTerminalType);
1795                 tml->size = 1;
1796                 it->inputs_size = 1;
1797                 return uaudio_merge_terminal_list(it);
1798         case UDESCSUB_AC_FEATURE:
1799                 src_id = it->d.fu->bSourceId;
1800                 it->inputs = kmalloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1801                 if (it->inputs == NULL) {
1802                         kprintf("uaudio_io_terminaltype: no memory\n");
1803                         return uaudio_io_terminaltype(outtype, iot, src_id);
1804                 }
1805                 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1806                 it->inputs_size = 1;
1807                 return uaudio_merge_terminal_list(it);
1808         case UDESCSUB_AC_OUTPUT:
1809                 it->inputs = kmalloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1810                 if (it->inputs == NULL) {
1811                         kprintf("uaudio_io_terminaltype: no memory\n");
1812                         return NULL;
1813                 }
1814                 src_id = it->d.ot->bSourceId;
1815                 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1816                 it->inputs_size = 1;
1817                 iot[src_id].direct = TRUE;
1818                 return NULL;
1819         case UDESCSUB_AC_MIXER:
1820                 it->inputs_size = 0;
1821                 it->inputs = kmalloc(sizeof(struct terminal_list *)
1822                                     * it->d.mu->bNrInPins, M_TEMP, M_NOWAIT);
1823                 if (it->inputs == NULL) {
1824                         kprintf("uaudio_io_terminaltype: no memory\n");
1825                         return NULL;
1826                 }
1827                 for (i = 0; i < it->d.mu->bNrInPins; i++) {
1828                         src_id = it->d.mu->baSourceId[i];
1829                         it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1830                                                                src_id);
1831                         it->inputs_size++;
1832                 }
1833                 return uaudio_merge_terminal_list(it);
1834         case UDESCSUB_AC_SELECTOR:
1835                 it->inputs_size = 0;
1836                 it->inputs = kmalloc(sizeof(struct terminal_list *)
1837                                     * it->d.su->bNrInPins, M_TEMP, M_NOWAIT);
1838                 if (it->inputs == NULL) {
1839                         kprintf("uaudio_io_terminaltype: no memory\n");
1840                         return NULL;
1841                 }
1842                 for (i = 0; i < it->d.su->bNrInPins; i++) {
1843                         src_id = it->d.su->baSourceId[i];
1844                         it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1845                                                                src_id);
1846                         it->inputs_size++;
1847                 }
1848                 return uaudio_merge_terminal_list(it);
1849         case UDESCSUB_AC_PROCESSING:
1850                 it->inputs_size = 0;
1851                 it->inputs = kmalloc(sizeof(struct terminal_list *)
1852                                     * it->d.pu->bNrInPins, M_TEMP, M_NOWAIT);
1853                 if (it->inputs == NULL) {
1854                         kprintf("uaudio_io_terminaltype: no memory\n");
1855                         return NULL;
1856                 }
1857                 for (i = 0; i < it->d.pu->bNrInPins; i++) {
1858                         src_id = it->d.pu->baSourceId[i];
1859                         it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1860                                                                src_id);
1861                         it->inputs_size++;
1862                 }
1863                 return uaudio_merge_terminal_list(it);
1864         case UDESCSUB_AC_EXTENSION:
1865                 it->inputs_size = 0;
1866                 it->inputs = kmalloc(sizeof(struct terminal_list *)
1867                                     * it->d.eu->bNrInPins, M_TEMP, M_NOWAIT);
1868                 if (it->inputs == NULL) {
1869                         kprintf("uaudio_io_terminaltype: no memory\n");
1870                         return NULL;
1871                 }
1872                 for (i = 0; i < it->d.eu->bNrInPins; i++) {
1873                         src_id = it->d.eu->baSourceId[i];
1874                         it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1875                                                                src_id);
1876                         it->inputs_size++;
1877                 }
1878                 return uaudio_merge_terminal_list(it);
1879         case UDESCSUB_AC_HEADER:
1880         default:
1881                 return NULL;
1882         }
1883 }
1884
1885 Static usbd_status
1886 uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
1887 {
1888         usbd_status err;
1889
1890         err = uaudio_identify_ac(sc, cdesc);
1891         if (err)
1892                 return err;
1893         return uaudio_identify_as(sc, cdesc);
1894 }
1895
1896 Static void
1897 uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai)
1898 {
1899         size_t len;
1900         struct as_info *nai;
1901
1902         len = sizeof(*ai) * (sc->sc_nalts + 1);
1903         nai = kmalloc(len, M_USBDEV, M_NOWAIT);
1904         if (nai == NULL) {
1905                 kprintf("uaudio_add_alt: no memory\n");
1906                 return;
1907         }
1908         /* Copy old data, if there was any */
1909         if (sc->sc_nalts != 0) {
1910                 memcpy(nai, sc->sc_alts, sizeof(*ai) * (sc->sc_nalts));
1911                 kfree(sc->sc_alts, M_USBDEV);
1912         }
1913         sc->sc_alts = nai;
1914         DPRINTFN(2,("uaudio_add_alt: adding alt=%d, enc=%d\n",
1915                     ai->alt, ai->encoding));
1916         sc->sc_alts[sc->sc_nalts++] = *ai;
1917 }
1918
1919 Static usbd_status
1920 uaudio_process_as(struct uaudio_softc *sc, const char *buf, int *offsp,
1921                   int size, const usb_interface_descriptor_t *id)
1922 #define offs (*offsp)
1923 {
1924         const struct usb_audio_streaming_interface_descriptor *asid;
1925         const struct usb_audio_streaming_type1_descriptor *asf1d;
1926         const usb_endpoint_descriptor_audio_t *ed;
1927         const usb_endpoint_descriptor_audio_t *epdesc1;
1928         const struct usb_audio_streaming_endpoint_descriptor *sed;
1929         int format, chan, prec, enc;
1930         int dir, type, sync;
1931         struct as_info ai;
1932         const char *format_str;
1933
1934         asid = (const void *)(buf + offs);
1935
1936         if (asid->bDescriptorType != UDESC_CS_INTERFACE ||
1937             asid->bDescriptorSubtype != AS_GENERAL)
1938                 return USBD_INVAL;
1939         DPRINTF(("uaudio_process_as: asid: bTerminakLink=%d wFormatTag=%d\n",
1940                  asid->bTerminalLink, UGETW(asid->wFormatTag)));
1941         offs += asid->bLength;
1942         if (offs > size)
1943                 return USBD_INVAL;
1944
1945         asf1d = (const void *)(buf + offs);
1946         if (asf1d->bDescriptorType != UDESC_CS_INTERFACE ||
1947             asf1d->bDescriptorSubtype != FORMAT_TYPE)
1948                 return USBD_INVAL;
1949         offs += asf1d->bLength;
1950         if (offs > size)
1951                 return USBD_INVAL;
1952
1953         if (asf1d->bFormatType != FORMAT_TYPE_I) {
1954                 kprintf("%s: ignored setting with type %d format\n",
1955                        USBDEVNAME(sc->sc_dev), UGETW(asid->wFormatTag));
1956                 return USBD_NORMAL_COMPLETION;
1957         }
1958
1959         ed = (const void *)(buf + offs);
1960         if (ed->bDescriptorType != UDESC_ENDPOINT)
1961                 return USBD_INVAL;
1962         DPRINTF(("uaudio_process_as: endpoint[0] bLength=%d bDescriptorType=%d "
1963                  "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d "
1964                  "bInterval=%d bRefresh=%d bSynchAddress=%d\n",
1965                  ed->bLength, ed->bDescriptorType, ed->bEndpointAddress,
1966                  ed->bmAttributes, UGETW(ed->wMaxPacketSize),
1967                  ed->bInterval, ed->bRefresh, ed->bSynchAddress));
1968         offs += ed->bLength;
1969         if (offs > size)
1970                 return USBD_INVAL;
1971         if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
1972                 return USBD_INVAL;
1973
1974         dir = UE_GET_DIR(ed->bEndpointAddress);
1975         type = UE_GET_ISO_TYPE(ed->bmAttributes);
1976         if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) &&
1977             dir == UE_DIR_IN && type == UE_ISO_ADAPT)
1978                 type = UE_ISO_ASYNC;
1979
1980         /* We can't handle endpoints that need a sync pipe yet. */
1981         sync = FALSE;
1982         if (dir == UE_DIR_IN && type == UE_ISO_ADAPT) {
1983                 sync = TRUE;
1984 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1985                 kprintf("%s: ignored input endpoint of type adaptive\n",
1986                        USBDEVNAME(sc->sc_dev));
1987                 return USBD_NORMAL_COMPLETION;
1988 #endif
1989         }
1990         if (dir != UE_DIR_IN && type == UE_ISO_ASYNC) {
1991                 sync = TRUE;
1992 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1993                 kprintf("%s: ignored output endpoint of type async\n",
1994                        USBDEVNAME(sc->sc_dev));
1995                 return USBD_NORMAL_COMPLETION;
1996 #endif
1997         }
1998
1999         sed = (const void *)(buf + offs);
2000         if (sed->bDescriptorType != UDESC_CS_ENDPOINT ||
2001             sed->bDescriptorSubtype != AS_GENERAL)
2002                 return USBD_INVAL;
2003         DPRINTF((" streadming_endpoint: offset=%d bLength=%d\n", offs, sed->bLength));
2004         offs += sed->bLength;
2005         if (offs > size)
2006                 return USBD_INVAL;
2007
2008         if (sync && id->bNumEndpoints <= 1) {
2009                 kprintf("%s: a sync-pipe endpoint but no other endpoint\n",
2010                        USBDEVNAME(sc->sc_dev));
2011                 return USBD_INVAL;
2012         }
2013         if (!sync && id->bNumEndpoints > 1) {
2014                 kprintf("%s: non sync-pipe endpoint but multiple endpoints\n",
2015                        USBDEVNAME(sc->sc_dev));
2016                 return USBD_INVAL;
2017         }
2018         epdesc1 = NULL;
2019         if (id->bNumEndpoints > 1) {
2020                 epdesc1 = (const void*)(buf + offs);
2021                 if (epdesc1->bDescriptorType != UDESC_ENDPOINT)
2022                         return USBD_INVAL;
2023                 DPRINTF(("uaudio_process_as: endpoint[1] bLength=%d "
2024                          "bDescriptorType=%d bEndpointAddress=%d "
2025                          "bmAttributes=0x%x wMaxPacketSize=%d bInterval=%d "
2026                          "bRefresh=%d bSynchAddress=%d\n",
2027                          epdesc1->bLength, epdesc1->bDescriptorType,
2028                          epdesc1->bEndpointAddress, epdesc1->bmAttributes,
2029                          UGETW(epdesc1->wMaxPacketSize), epdesc1->bInterval,
2030                          epdesc1->bRefresh, epdesc1->bSynchAddress));
2031                 offs += epdesc1->bLength;
2032                 if (offs > size)
2033                         return USBD_INVAL;
2034                 if (epdesc1->bSynchAddress != 0) {
2035                         kprintf("%s: invalid endpoint: bSynchAddress=0\n",
2036                                USBDEVNAME(sc->sc_dev));
2037                         return USBD_INVAL;
2038                 }
2039                 if (UE_GET_XFERTYPE(epdesc1->bmAttributes) != UE_ISOCHRONOUS) {
2040                         kprintf("%s: invalid endpoint: bmAttributes=0x%x\n",
2041                                USBDEVNAME(sc->sc_dev), epdesc1->bmAttributes);
2042                         return USBD_INVAL;
2043                 }
2044                 if (epdesc1->bEndpointAddress != ed->bSynchAddress) {
2045                         kprintf("%s: invalid endpoint addresses: "
2046                                "ep[0]->bSynchAddress=0x%x "
2047                                "ep[1]->bEndpointAddress=0x%x\n",
2048                                USBDEVNAME(sc->sc_dev), ed->bSynchAddress,
2049                                epdesc1->bEndpointAddress);
2050                         return USBD_INVAL;
2051                 }
2052                 /* UE_GET_ADDR(epdesc1->bEndpointAddress), and epdesc1->bRefresh */
2053         }
2054
2055         format = UGETW(asid->wFormatTag);
2056         chan = asf1d->bNrChannels;
2057         prec = asf1d->bBitResolution;
2058         if (prec != 8 && prec != 16 && prec != 24 && prec != 32) {
2059                 kprintf("%s: ignored setting with precision %d\n",
2060                        USBDEVNAME(sc->sc_dev), prec);
2061                 return USBD_NORMAL_COMPLETION;
2062         }
2063         switch (format) {
2064         case UA_FMT_PCM:
2065                 if (prec == 8) {
2066                         sc->sc_altflags |= HAS_8;
2067                 } else if (prec == 16) {
2068                         sc->sc_altflags |= HAS_16;
2069                 } else if (prec == 24) {
2070                         sc->sc_altflags |= HAS_24;
2071                 } else if (prec == 32) {
2072                         sc->sc_altflags |= HAS_32;
2073                 }
2074                 enc = AUDIO_ENCODING_SLINEAR_LE;
2075                 format_str = "pcm";
2076                 break;
2077         case UA_FMT_PCM8:
2078                 enc = AUDIO_ENCODING_ULINEAR_LE;
2079                 sc->sc_altflags |= HAS_8U;
2080                 format_str = "pcm8";
2081                 break;
2082         case UA_FMT_ALAW:
2083                 enc = AUDIO_ENCODING_ALAW;
2084                 sc->sc_altflags |= HAS_ALAW;
2085                 format_str = "alaw";
2086                 break;
2087         case UA_FMT_MULAW:
2088                 enc = AUDIO_ENCODING_ULAW;
2089                 sc->sc_altflags |= HAS_MULAW;
2090                 format_str = "mulaw";
2091                 break;
2092         case UA_FMT_IEEE_FLOAT:
2093         default:
2094                 kprintf("%s: ignored setting with format %d\n",
2095                        USBDEVNAME(sc->sc_dev), format);
2096                 return USBD_NORMAL_COMPLETION;
2097         }
2098 #ifdef USB_DEBUG
2099         kprintf("%s: %s: %dch, %d/%dbit, %s,", USBDEVNAME(sc->sc_dev),
2100                dir == UE_DIR_IN ? "recording" : "playback",
2101                chan, prec, asf1d->bSubFrameSize * 8, format_str);
2102         if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
2103                 kprintf(" %d-%dHz\n", UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d));
2104         } else {
2105                 int r;
2106                 kprintf(" %d", UA_GETSAMP(asf1d, 0));
2107                 for (r = 1; r < asf1d->bSamFreqType; r++)
2108                         kprintf(",%d", UA_GETSAMP(asf1d, r));
2109                 kprintf("Hz\n");
2110         }
2111 #endif
2112 #if defined(__FreeBSD__) || defined(__DragonFly__)
2113         if (sc->uaudio_sndstat_flag != 0) {
2114                 sbuf_printf(&(sc->uaudio_sndstat), "\n\t");
2115                 sbuf_printf(&(sc->uaudio_sndstat), 
2116                     "mode %d:(%s) %dch, %d/%dbit, %s,", 
2117                     id->bAlternateSetting,
2118                     dir == UE_DIR_IN ? "input" : "output",
2119                     chan, prec, asf1d->bSubFrameSize * 8, format_str);
2120                 if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
2121                         sbuf_printf(&(sc->uaudio_sndstat), " %d-%dHz", 
2122                             UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d));
2123                 } else {
2124                         int r;
2125                         sbuf_printf(&(sc->uaudio_sndstat), 
2126                             " %d", UA_GETSAMP(asf1d, 0));
2127                         for (r = 1; r < asf1d->bSamFreqType; r++)
2128                                 sbuf_printf(&(sc->uaudio_sndstat), 
2129                                     ",%d", UA_GETSAMP(asf1d, r));
2130                         sbuf_printf(&(sc->uaudio_sndstat), "Hz");
2131                 }
2132         }
2133 #endif
2134         ai.alt = id->bAlternateSetting;
2135         ai.encoding = enc;
2136         ai.attributes = sed->bmAttributes;
2137         ai.idesc = id;
2138         ai.edesc = ed;
2139         ai.edesc1 = epdesc1;
2140         ai.asf1desc = asf1d;
2141         ai.sc_busy = 0;
2142         uaudio_add_alt(sc, &ai);
2143 #ifdef USB_DEBUG
2144         if (ai.attributes & UA_SED_FREQ_CONTROL)
2145                 DPRINTFN(1, ("uaudio_process_as:  FREQ_CONTROL\n"));
2146         if (ai.attributes & UA_SED_PITCH_CONTROL)
2147                 DPRINTFN(1, ("uaudio_process_as:  PITCH_CONTROL\n"));
2148 #endif
2149         sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD;
2150
2151         return USBD_NORMAL_COMPLETION;
2152 }
2153 #undef offs
2154
2155 Static usbd_status
2156 uaudio_identify_as(struct uaudio_softc *sc,
2157                    const usb_config_descriptor_t *cdesc)
2158 {
2159         const usb_interface_descriptor_t *id;
2160         const char *buf;
2161         int size, offs;
2162
2163         size = UGETW(cdesc->wTotalLength);
2164         buf = (const char *)cdesc;
2165
2166         /* Locate the AudioStreaming interface descriptor. */
2167         offs = 0;
2168         id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOSTREAM);
2169         if (id == NULL)
2170                 return USBD_INVAL;
2171
2172 #if defined(__FreeBSD__) || defined(__DragonFly__)
2173         sc->uaudio_sndstat_flag = 0;
2174         if (sbuf_new(&(sc->uaudio_sndstat), NULL, 4096, SBUF_AUTOEXTEND) != NULL)
2175                 sc->uaudio_sndstat_flag = 1;
2176 #endif
2177         /* Loop through all the alternate settings. */
2178         while (offs <= size) {
2179                 DPRINTFN(2, ("uaudio_identify: interface=%d offset=%d\n",
2180                     id->bInterfaceNumber, offs));
2181                 switch (id->bNumEndpoints) {
2182                 case 0:
2183                         DPRINTFN(2, ("uaudio_identify: AS null alt=%d\n",
2184                                      id->bAlternateSetting));
2185                         sc->sc_nullalt = id->bAlternateSetting;
2186                         break;
2187                 case 1:
2188 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
2189                 case 2:
2190 #endif
2191                         uaudio_process_as(sc, buf, &offs, size, id);
2192                         break;
2193                 default:
2194                         kprintf("%s: ignored audio interface with %d "
2195                                "endpoints\n",
2196                                USBDEVNAME(sc->sc_dev), id->bNumEndpoints);
2197                         break;
2198                 }
2199                 id = uaudio_find_iface(buf, size, &offs,UISUBCLASS_AUDIOSTREAM);
2200                 if (id == NULL)
2201                         break;
2202         }
2203 #if defined(__FreeBSD__) || defined(__DragonFly__)
2204         sbuf_finish(&(sc->uaudio_sndstat));
2205 #endif
2206         if (offs > size)
2207                 return USBD_INVAL;
2208         DPRINTF(("uaudio_identify_as: %d alts available\n", sc->sc_nalts));
2209
2210         if (sc->sc_mode == 0) {
2211                 kprintf("%s: no usable endpoint found\n",
2212                        USBDEVNAME(sc->sc_dev));
2213                 return USBD_INVAL;
2214         }
2215
2216         return USBD_NORMAL_COMPLETION;
2217 }
2218
2219 Static usbd_status
2220 uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
2221 {
2222         struct io_terminal* iot;
2223         const usb_interface_descriptor_t *id;
2224         const struct usb_audio_control_descriptor *acdp;
2225         const usb_descriptor_t *dp;
2226         const struct usb_audio_output_terminal *pot;
2227         struct terminal_list *tml;
2228         const char *buf, *ibuf, *ibufend;
2229         int size, offs, aclen, ndps, i, j;
2230
2231         size = UGETW(cdesc->wTotalLength);
2232         buf = (const char *)cdesc;
2233
2234         /* Locate the AudioControl interface descriptor. */
2235         offs = 0;
2236         id = uaudio_find_iface(buf, size, &offs, UISUBCLASS_AUDIOCONTROL);
2237         if (id == NULL)
2238                 return USBD_INVAL;
2239         if (offs + sizeof *acdp > size)
2240                 return USBD_INVAL;
2241         sc->sc_ac_iface = id->bInterfaceNumber;
2242         DPRINTFN(2,("uaudio_identify_ac: AC interface is %d\n", sc->sc_ac_iface));
2243
2244         /* A class-specific AC interface header should follow. */
2245         ibuf = buf + offs;
2246         acdp = (const struct usb_audio_control_descriptor *)ibuf;
2247         if (acdp->bDescriptorType != UDESC_CS_INTERFACE ||
2248             acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)
2249                 return USBD_INVAL;
2250         aclen = UGETW(acdp->wTotalLength);
2251         if (offs + aclen > size)
2252                 return USBD_INVAL;
2253
2254         if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC) &&
2255              UGETW(acdp->bcdADC) != UAUDIO_VERSION)
2256                 return USBD_INVAL;
2257
2258         sc->sc_audio_rev = UGETW(acdp->bcdADC);
2259         DPRINTFN(2,("uaudio_identify_ac: found AC header, vers=%03x, len=%d\n",
2260                  sc->sc_audio_rev, aclen));
2261
2262         sc->sc_nullalt = -1;
2263
2264         /* Scan through all the AC specific descriptors */
2265         ibufend = ibuf + aclen;
2266         dp = (const usb_descriptor_t *)ibuf;
2267         ndps = 0;
2268         iot = kmalloc(sizeof(struct io_terminal) * 256, M_TEMP, M_NOWAIT | M_ZERO);
2269         if (iot == NULL) {
2270                 kprintf("%s: no memory\n", __func__);
2271                 return USBD_NOMEM;
2272         }
2273         for (;;) {
2274                 ibuf += dp->bLength;
2275                 if (ibuf >= ibufend)
2276                         break;
2277                 dp = (const usb_descriptor_t *)ibuf;
2278                 if (ibuf + dp->bLength > ibufend) {
2279                         kfree(iot, M_TEMP);
2280                         return USBD_INVAL;
2281                 }
2282                 if (dp->bDescriptorType != UDESC_CS_INTERFACE) {
2283                         kprintf("uaudio_identify_ac: skip desc type=0x%02x\n",
2284                                dp->bDescriptorType);
2285                         continue;
2286                 }
2287                 i = ((const struct usb_audio_input_terminal *)dp)->bTerminalId;
2288                 iot[i].d.desc = dp;
2289                 if (i > ndps)
2290                         ndps = i;
2291         }
2292         ndps++;
2293
2294         /* construct io_terminal */
2295         for (i = 0; i < ndps; i++) {
2296                 dp = iot[i].d.desc;
2297                 if (dp == NULL)
2298                         continue;
2299                 if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT)
2300                         continue;
2301                 pot = iot[i].d.ot;
2302                 tml = uaudio_io_terminaltype(UGETW(pot->wTerminalType), iot, i);
2303                 if (tml != NULL)
2304                         kfree(tml, M_TEMP);
2305         }
2306
2307 #ifdef USB_DEBUG
2308         for (i = 0; i < 256; i++) {
2309                 struct usb_audio_cluster cluster;
2310
2311                 if (iot[i].d.desc == NULL)
2312                         continue;
2313                 logprintf("id %d:\t", i);
2314                 switch (iot[i].d.desc->bDescriptorSubtype) {
2315                 case UDESCSUB_AC_INPUT:
2316                         logprintf("AC_INPUT type=%s\n", uaudio_get_terminal_name
2317                                   (UGETW(iot[i].d.it->wTerminalType)));
2318                         logprintf("\t");
2319                         cluster = uaudio_get_cluster(i, iot);
2320                         uaudio_dump_cluster(&cluster);
2321                         logprintf("\n");
2322                         break;
2323                 case UDESCSUB_AC_OUTPUT:
2324                         logprintf("AC_OUTPUT type=%s ", uaudio_get_terminal_name
2325                                   (UGETW(iot[i].d.ot->wTerminalType)));
2326                         logprintf("src=%d\n", iot[i].d.ot->bSourceId);
2327                         break;
2328                 case UDESCSUB_AC_MIXER:
2329                         logprintf("AC_MIXER src=");
2330                         for (j = 0; j < iot[i].d.mu->bNrInPins; j++)
2331                                 logprintf("%d ", iot[i].d.mu->baSourceId[j]);
2332                         logprintf("\n\t");
2333                         cluster = uaudio_get_cluster(i, iot);
2334                         uaudio_dump_cluster(&cluster);
2335                         logprintf("\n");
2336                         break;
2337                 case UDESCSUB_AC_SELECTOR:
2338                         logprintf("AC_SELECTOR src=");
2339                         for (j = 0; j < iot[i].d.su->bNrInPins; j++)
2340                                 logprintf("%d ", iot[i].d.su->baSourceId[j]);
2341                         logprintf("\n");
2342                         break;
2343                 case UDESCSUB_AC_FEATURE:
2344                         logprintf("AC_FEATURE src=%d\n", iot[i].d.fu->bSourceId);
2345                         break;
2346                 case UDESCSUB_AC_PROCESSING:
2347                         logprintf("AC_PROCESSING src=");
2348                         for (j = 0; j < iot[i].d.pu->bNrInPins; j++)
2349                                 logprintf("%d ", iot[i].d.pu->baSourceId[j]);
2350                         logprintf("\n\t");
2351                         cluster = uaudio_get_cluster(i, iot);
2352                         uaudio_dump_cluster(&cluster);
2353                         logprintf("\n");
2354                         break;
2355                 case UDESCSUB_AC_EXTENSION:
2356                         logprintf("AC_EXTENSION src=");
2357                         for (j = 0; j < iot[i].d.eu->bNrInPins; j++)
2358                                 logprintf("%d ", iot[i].d.eu->baSourceId[j]);
2359                         logprintf("\n\t");
2360                         cluster = uaudio_get_cluster(i, iot);
2361                         uaudio_dump_cluster(&cluster);
2362                         logprintf("\n");
2363                         break;
2364                 default:
2365                         logprintf("unknown audio control (subtype=%d)\n",
2366                                   iot[i].d.desc->bDescriptorSubtype);
2367                 }
2368                 for (j = 0; j < iot[i].inputs_size; j++) {
2369                         int k;
2370                         logprintf("\tinput%d: ", j);
2371                         tml = iot[i].inputs[j];
2372                         if (tml == NULL) {
2373                                 logprintf("NULL\n");
2374                                 continue;
2375                         }
2376                         for (k = 0; k < tml->size; k++)
2377                                 logprintf("%s ", uaudio_get_terminal_name
2378                                           (tml->terminals[k]));
2379                         logprintf("\n");
2380                 }
2381                 logprintf("\toutput: ");
2382                 tml = iot[i].output;
2383                 for (j = 0; j < tml->size; j++)
2384                         logprintf("%s ", uaudio_get_terminal_name(tml->terminals[j]));
2385                 logprintf("\n");
2386         }
2387 #endif
2388
2389         for (i = 0; i < ndps; i++) {
2390                 dp = iot[i].d.desc;
2391                 if (dp == NULL)
2392                         continue;
2393                 DPRINTF(("uaudio_identify_ac: id=%d subtype=%d\n",
2394                          i, dp->bDescriptorSubtype));
2395                 switch (dp->bDescriptorSubtype) {
2396                 case UDESCSUB_AC_HEADER:
2397                         kprintf("uaudio_identify_ac: unexpected AC header\n");
2398                         break;
2399                 case UDESCSUB_AC_INPUT:
2400                         uaudio_add_input(sc, iot, i);
2401                         break;
2402                 case UDESCSUB_AC_OUTPUT:
2403                         uaudio_add_output(sc, iot, i);
2404                         break;
2405                 case UDESCSUB_AC_MIXER:
2406                         uaudio_add_mixer(sc, iot, i);
2407                         break;
2408                 case UDESCSUB_AC_SELECTOR:
2409                         uaudio_add_selector(sc, iot, i);
2410                         break;
2411                 case UDESCSUB_AC_FEATURE:
2412                         uaudio_add_feature(sc, iot, i);
2413                         break;
2414                 case UDESCSUB_AC_PROCESSING:
2415                         uaudio_add_processing(sc, iot, i);
2416                         break;
2417                 case UDESCSUB_AC_EXTENSION:
2418                         uaudio_add_extension(sc, iot, i);
2419                         break;
2420                 default:
2421                         kprintf("uaudio_identify_ac: bad AC desc subtype=0x%02x\n",
2422                                dp->bDescriptorSubtype);
2423                         break;
2424                 }
2425         }
2426
2427         /* delete io_terminal */
2428         for (i = 0; i < 256; i++) {
2429                 if (iot[i].d.desc == NULL)
2430                         continue;
2431                 if (iot[i].inputs != NULL) {
2432                         for (j = 0; j < iot[i].inputs_size; j++) {
2433                                 if (iot[i].inputs[j] != NULL)
2434                                         kfree(iot[i].inputs[j], M_TEMP);
2435                         }
2436                         kfree(iot[i].inputs, M_TEMP);
2437                 }
2438                 if (iot[i].output != NULL)
2439                         kfree(iot[i].output, M_TEMP);
2440                 iot[i].d.desc = NULL;
2441         }
2442         kfree(iot, M_TEMP);
2443
2444         return USBD_NORMAL_COMPLETION;
2445 }
2446
2447 #if defined(__NetBSD__) || defined(__OpenBSD__)
2448 Static int
2449 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi)
2450 {
2451         struct uaudio_softc *sc;
2452         struct mixerctl *mc;
2453         int n, nctls, i;
2454
2455         sc = addr;
2456         DPRINTFN(2,("uaudio_query_devinfo: index=%d\n", mi->index));
2457         if (sc->sc_dying)
2458                 return EIO;
2459
2460         n = mi->index;
2461         nctls = sc->sc_nctls;
2462
2463         switch (n) {
2464         case UAC_OUTPUT:
2465                 mi->type = AUDIO_MIXER_CLASS;
2466                 mi->mixer_class = UAC_OUTPUT;
2467                 mi->next = mi->prev = AUDIO_MIXER_LAST;
2468                 strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name));
2469                 return 0;
2470         case UAC_INPUT:
2471                 mi->type = AUDIO_MIXER_CLASS;
2472                 mi->mixer_class = UAC_INPUT;
2473                 mi->next = mi->prev = AUDIO_MIXER_LAST;
2474                 strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name));
2475                 return 0;
2476         case UAC_EQUAL:
2477                 mi->type = AUDIO_MIXER_CLASS;
2478                 mi->mixer_class = UAC_EQUAL;
2479                 mi->next = mi->prev = AUDIO_MIXER_LAST;
2480                 strlcpy(mi->label.name, AudioCequalization,
2481                     sizeof(mi->label.name));
2482                 return 0;
2483         case UAC_RECORD:
2484                 mi->type = AUDIO_MIXER_CLASS;
2485                 mi->mixer_class = UAC_RECORD;
2486                 mi->next = mi->prev = AUDIO_MIXER_LAST;
2487                 strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name));
2488                 return 0;
2489         default:
2490                 break;
2491         }
2492
2493         n -= UAC_NCLASSES;
2494         if (n < 0 || n >= nctls)
2495                 return ENXIO;
2496
2497         mc = &sc->sc_ctls[n];
2498         strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name));
2499         mi->mixer_class = mc->class;
2500         mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */
2501         switch (mc->type) {
2502         case MIX_ON_OFF:
2503                 mi->type = AUDIO_MIXER_ENUM;
2504                 mi->un.e.num_mem = 2;
2505                 strlcpy(mi->un.e.member[0].label.name, AudioNoff,
2506                     sizeof(mi->un.e.member[0].label.name));
2507                 mi->un.e.member[0].ord = 0;
2508                 strlcpy(mi->un.e.member[1].label.name, AudioNon,
2509                     sizeof(mi->un.e.member[1].label.name));
2510                 mi->un.e.member[1].ord = 1;
2511                 break;
2512         case MIX_SELECTOR:
2513                 mi->type = AUDIO_MIXER_ENUM;
2514                 mi->un.e.num_mem = mc->maxval - mc->minval + 1;
2515                 for (i = 0; i <= mc->maxval - mc->minval; i++) {
2516                         ksnprintf(mi->un.e.member[i].label.name,
2517                                  sizeof(mi->un.e.member[i].label.name),
2518                                  "%d", i + mc->minval);
2519                         mi->un.e.member[i].ord = i + mc->minval;
2520                 }
2521                 break;
2522         default:
2523                 mi->type = AUDIO_MIXER_VALUE;
2524                 strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN);
2525                 mi->un.v.num_channels = mc->nchan;
2526                 mi->un.v.delta = mc->delta;
2527                 break;
2528         }
2529         return 0;
2530 }
2531
2532 Static int
2533 uaudio_open(void *addr, int flags)
2534 {
2535         struct uaudio_softc *sc;
2536
2537         sc = addr;
2538         DPRINTF(("uaudio_open: sc=%p\n", sc));
2539         if (sc->sc_dying)
2540                 return EIO;
2541
2542         if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY))
2543                 return EACCES;
2544         if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD))
2545                 return EACCES;
2546
2547         return 0;
2548 }
2549
2550 /*
2551  * Close function is called at splaudio().
2552  */
2553 Static void
2554 uaudio_close(void *addr)
2555 {
2556 }
2557
2558 Static int
2559 uaudio_drain(void *addr)
2560 {
2561         struct uaudio_softc *sc;
2562
2563         sc = addr;
2564         usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
2565
2566         return 0;
2567 }
2568
2569 Static int
2570 uaudio_halt_out_dma(void *addr)
2571 {
2572         struct uaudio_softc *sc;
2573
2574         sc = addr;
2575         if (sc->sc_dying)
2576                 return EIO;
2577
2578         DPRINTF(("uaudio_halt_out_dma: enter\n"));
2579         if (sc->sc_playchan.pipe != NULL) {
2580                 uaudio_chan_close(sc, &sc->sc_playchan);
2581                 sc->sc_playchan.pipe = NULL;
2582                 uaudio_chan_free_buffers(sc, &sc->sc_playchan);
2583                 sc->sc_playchan.intr = NULL;
2584         }
2585         return 0;
2586 }
2587
2588 Static int
2589 uaudio_halt_in_dma(void *addr)
2590 {
2591         struct uaudio_softc *sc;
2592
2593         DPRINTF(("uaudio_halt_in_dma: enter\n"));
2594         sc = addr;
2595         if (sc->sc_recchan.pipe != NULL) {
2596                 uaudio_chan_close(sc, &sc->sc_recchan);
2597                 sc->sc_recchan.pipe = NULL;
2598                 uaudio_chan_free_buffers(sc, &sc->sc_recchan);
2599                 sc->sc_recchan.intr = NULL;
2600         }
2601         return 0;
2602 }
2603
2604 Static int
2605 uaudio_getdev(void *addr, struct audio_device *retp)
2606 {
2607         struct uaudio_softc *sc;
2608
2609         DPRINTF(("uaudio_mixer_getdev:\n"));
2610         sc = addr;
2611         if (sc->sc_dying)
2612                 return EIO;
2613
2614         *retp = uaudio_device;
2615         return 0;
2616 }
2617
2618 /*
2619  * Make sure the block size is large enough to hold all outstanding transfers.
2620  */
2621 Static int
2622 uaudio_round_blocksize(void *addr, int blk)
2623 {
2624         struct uaudio_softc *sc;
2625         int b;
2626
2627         sc = addr;
2628         DPRINTF(("uaudio_round_blocksize: blk=%d mode=%s\n", blk,
2629                 mode == AUMODE_PLAY ? "AUMODE_PLAY" : "AUMODE_RECORD"));
2630
2631         /* chan.bytes_per_frame can be 0. */
2632         if (mode == AUMODE_PLAY || sc->sc_recchan.bytes_per_frame <= 0) {
2633                 b = param->sample_rate * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2634
2635                 /*
2636                  * This does not make accurate value in the case
2637                  * of b % USB_FRAMES_PER_SECOND != 0
2638                  */
2639                 b /= USB_FRAMES_PER_SECOND;
2640
2641                 b *= param->precision / 8 * param->channels;
2642         } else {
2643                 /*
2644                  * use wMaxPacketSize in bytes_per_frame.
2645                  * See uaudio_set_params() and uaudio_chan_init()
2646                  */
2647                 b = sc->sc_recchan.bytes_per_frame
2648                         * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2649         }
2650
2651         if (b <= 0)
2652                 b = 1;
2653         blk = blk <= b ? b : blk / b * b;
2654
2655 #ifdef DIAGNOSTIC
2656         if (blk <= 0) {
2657                 kprintf("uaudio_round_blocksize: blk=%d\n", blk);
2658                 blk = 512;
2659         }
2660 #endif
2661
2662         DPRINTF(("uaudio_round_blocksize: resultant blk=%d\n", blk));
2663         return blk;
2664 }
2665
2666 Static int
2667 uaudio_get_props(void *addr)
2668 {
2669         return AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
2670
2671 }
2672 #endif  /* NetBSD or OpenBSD */
2673
2674 Static int
2675 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue,
2676            int wIndex, int len)
2677 {
2678         usb_device_request_t req;
2679         uint8_t data[4];
2680         usbd_status err;
2681         int val;
2682
2683 #if defined(__FreeBSD__) || defined(__DragonFly__)
2684         if (sc->sc_dying)
2685                 return EIO;
2686 #endif
2687
2688         if (wValue == -1)
2689                 return 0;
2690
2691         req.bmRequestType = type;
2692         req.bRequest = which;
2693         USETW(req.wValue, wValue);
2694         USETW(req.wIndex, wIndex);
2695         USETW(req.wLength, len);
2696         DPRINTFN(2,("uaudio_get: type=0x%02x req=0x%02x wValue=0x%04x "
2697                     "wIndex=0x%04x len=%d\n",
2698                     type, which, wValue, wIndex, len));
2699         err = usbd_do_request(sc->sc_udev, &req, data);
2700         if (err) {
2701                 DPRINTF(("uaudio_get: err=%s\n", usbd_errstr(err)));
2702                 return -1;
2703         }
2704         switch (len) {
2705         case 1:
2706                 val = data[0];
2707                 break;
2708         case 2:
2709                 val = data[0] | (data[1] << 8);
2710                 break;
2711         default:
2712                 DPRINTF(("uaudio_get: bad length=%d\n", len));
2713                 return -1;
2714         }
2715         DPRINTFN(2,("uaudio_get: val=%d\n", val));
2716         return val;
2717 }
2718
2719 Static void
2720 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue,
2721            int wIndex, int len, int val)
2722 {
2723         usb_device_request_t req;
2724         uint8_t data[4];
2725         usbd_status err;
2726
2727 #if defined(__FreeBSD__) || defined(__DragonFly__)
2728         if (sc->sc_dying)
2729                 return;
2730 #endif
2731
2732         if (wValue == -1)
2733                 return;
2734
2735         req.bmRequestType = type;
2736         req.bRequest = which;
2737         USETW(req.wValue, wValue);
2738         USETW(req.wIndex, wIndex);
2739         USETW(req.wLength, len);
2740         switch (len) {
2741         case 1:
2742                 data[0] = val;
2743                 break;
2744         case 2:
2745                 data[0] = val;
2746                 data[1] = val >> 8;
2747                 break;
2748         default:
2749                 return;
2750         }
2751         DPRINTFN(2,("uaudio_set: type=0x%02x req=0x%02x wValue=0x%04x "
2752                     "wIndex=0x%04x len=%d, val=%d\n",
2753                     type, which, wValue, wIndex, len, val & 0xffff));
2754         err = usbd_do_request(sc->sc_udev, &req, data);
2755 #ifdef USB_DEBUG
2756         if (err)
2757                 DPRINTF(("uaudio_set: err=%d\n", err));
2758 #endif
2759 }
2760
2761 Static int
2762 uaudio_signext(int type, int val)
2763 {
2764         if (!MIX_UNSIGNED(type)) {
2765                 if (MIX_SIZE(type) == 2)
2766                         val = (int16_t)val;
2767                 else
2768                         val = (int8_t)val;
2769         }
2770         return val;
2771 }
2772
2773 #if defined(__NetBSD__) || defined(__OpenBSD__)
2774 Static int
2775 uaudio_value2bsd(struct mixerctl *mc, int val)
2776 {
2777         DPRINTFN(5, ("uaudio_value2bsd: type=%03x val=%d min=%d max=%d ",
2778                      mc->type, val, mc->minval, mc->maxval));
2779         if (mc->type == MIX_ON_OFF) {
2780                 val = (val != 0);
2781         } else if (mc->type == MIX_SELECTOR) {
2782                 if (val < mc->minval || val > mc->maxval)
2783                         val = mc->minval;
2784         } else
2785                 val = ((uaudio_signext(mc->type, val) - mc->minval) * 255
2786                         + mc->mul/2) / mc->mul;
2787         DPRINTFN(5, ("val'=%d\n", val));
2788         return val;
2789 }
2790 #endif
2791
2792 int
2793 uaudio_bsd2value(struct mixerctl *mc, int val)
2794 {
2795         DPRINTFN(5,("uaudio_bsd2value: type=%03x val=%d min=%d max=%d ",
2796                     mc->type, val, mc->minval, mc->maxval));
2797         if (mc->type == MIX_ON_OFF) {
2798                 val = (val != 0);
2799         } else if (mc->type == MIX_SELECTOR) {
2800                 if (val < mc->minval || val > mc->maxval)
2801                         val = mc->minval;
2802         } else
2803                 val = (val + mc->delta/2) * mc->mul / 255 + mc->minval;
2804         DPRINTFN(5, ("val'=%d\n", val));
2805         return val;
2806 }
2807
2808 #if defined(__NetBSD__) || defined(__OpenBSD__)
2809 Static int
2810 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2811                int chan)
2812 {
2813         int val;
2814
2815         DPRINTFN(5,("uaudio_ctl_get: which=%d chan=%d\n", which, chan));
2816         val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan],
2817                          mc->wIndex, MIX_SIZE(mc->type));
2818         return uaudio_value2bsd(mc, val);
2819 }
2820 #endif
2821
2822 Static void
2823 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2824                int chan, int val)
2825 {
2826         val = uaudio_bsd2value(mc, val);
2827         uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan],
2828                    mc->wIndex, MIX_SIZE(mc->type), val);
2829 }
2830
2831 #if defined(__NetBSD__) || defined(__OpenBSD__)
2832 Static int
2833 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp)
2834 {
2835         struct uaudio_softc *sc;
2836         struct mixerctl *mc;
2837         int i, n, vals[MIX_MAX_CHAN], val;
2838
2839         DPRINTFN(2,("uaudio_mixer_get_port: index=%d\n", cp->dev));
2840         sc = addr;
2841         if (sc->sc_dying)
2842                 return EIO;
2843
2844         n = cp->dev - UAC_NCLASSES;
2845         if (n < 0 || n >= sc->sc_nctls)
2846                 return ENXIO;
2847         mc = &sc->sc_ctls[n];
2848
2849         if (mc->type == MIX_ON_OFF) {
2850                 if (cp->type != AUDIO_MIXER_ENUM)
2851                         return EINVAL;
2852                 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2853         } else if (mc->type == MIX_SELECTOR) {
2854                 if (cp->type != AUDIO_MIXER_ENUM)
2855                         return EINVAL;
2856                 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2857         } else {
2858                 if (cp->type != AUDIO_MIXER_VALUE)
2859                         return (EINVAL);
2860                 if (cp->un.value.num_channels != 1 &&
2861                     cp->un.value.num_channels != mc->nchan)
2862                         return EINVAL;
2863                 for (i = 0; i < mc->nchan; i++)
2864                         vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i);
2865                 if (cp->un.value.num_channels == 1 && mc->nchan != 1) {
2866                         for (val = 0, i = 0; i < mc->nchan; i++)
2867                                 val += vals[i];
2868                         vals[0] = val / mc->nchan;
2869                 }
2870                 for (i = 0; i < cp->un.value.num_channels; i++)
2871                         cp->un.value.level[i] = vals[i];
2872         }
2873
2874         return 0;
2875 }
2876
2877 Static int
2878 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp)
2879 {
2880         struct uaudio_softc *sc;
2881         struct mixerctl *mc;
2882         int i, n, vals[MIX_MAX_CHAN];
2883
2884         DPRINTFN(2,("uaudio_mixer_set_port: index = %d\n", cp->dev));
2885         sc = addr;
2886         if (sc->sc_dying)
2887                 return EIO;
2888
2889         n = cp->dev - UAC_NCLASSES;
2890         if (n < 0 || n >= sc->sc_nctls)
2891                 return ENXIO;
2892         mc = &sc->sc_ctls[n];
2893
2894         if (mc->type == MIX_ON_OFF) {
2895                 if (cp->type != AUDIO_MIXER_ENUM)
2896                         return EINVAL;
2897                 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2898         } else if (mc->type == MIX_SELECTOR) {
2899                 if (cp->type != AUDIO_MIXER_ENUM)
2900                         return EINVAL;
2901                 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2902         } else {
2903                 if (cp->type != AUDIO_MIXER_VALUE)
2904                         return EINVAL;
2905                 if (cp->un.value.num_channels == 1)
2906                         for (i = 0; i < mc->nchan; i++)
2907                                 vals[i] = cp->un.value.level[0];
2908                 else if (cp->un.value.num_channels == mc->nchan)
2909                         for (i = 0; i < mc->nchan; i++)
2910                                 vals[i] = cp->un.value.level[i];
2911                 else
2912                         return EINVAL;
2913                 for (i = 0; i < mc->nchan; i++)
2914                         uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]);
2915         }
2916         return 0;
2917 }
2918
2919 Static int
2920 uaudio_trigger_input(void *addr, void *start, void *end, int blksize,
2921                      void (*intr)(void *), void *arg,
2922                      struct audio_params *param)
2923 {
2924         struct uaudio_softc *sc;
2925         struct chan *ch;
2926         usbd_status err;
2927         int i, s;
2928
2929         sc = addr;
2930         if (sc->sc_dying)
2931                 return EIO;
2932
2933         DPRINTFN(3,("uaudio_trigger_input: sc=%p start=%p end=%p "
2934                     "blksize=%d\n", sc, start, end, blksize));
2935         ch = &sc->sc_recchan;
2936         uaudio_chan_set_param(ch, start, end, blksize);
2937         DPRINTFN(3,("uaudio_trigger_input: sample_size=%d bytes/frame=%d "
2938                     "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2939                     ch->fraction));
2940
2941         err = uaudio_chan_alloc_buffers(sc, ch);
2942         if (err)
2943                 return EIO;
2944
2945         err = uaudio_chan_open(sc, ch);
2946         if (err) {
2947                 uaudio_chan_free_buffers(sc, ch);
2948                 return EIO;
2949         }
2950
2951         ch->intr = intr;
2952         ch->arg = arg;
2953
2954         s = splusb();
2955         for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX -1 shouldn't be needed */
2956                 uaudio_chan_rtransfer(ch);
2957         splx(s);
2958
2959         return 0;
2960 }
2961
2962 Static int
2963 uaudio_trigger_output(void *addr, void *start, void *end, int blksize,
2964                       void (*intr)(void *), void *arg,
2965                       struct audio_params *param)
2966 {
2967         struct uaudio_softc *sc;
2968         struct chan *ch;
2969         usbd_status err;
2970         int i, s;
2971
2972         sc = addr;
2973         if (sc->sc_dying)
2974                 return EIO;
2975
2976         DPRINTFN(3,("uaudio_trigger_output: sc=%p start=%p end=%p "
2977                     "blksize=%d\n", sc, start, end, blksize));
2978         ch = &sc->sc_playchan;
2979         uaudio_chan_set_param(ch, start, end, blksize);
2980         DPRINTFN(3,("uaudio_trigger_output: sample_size=%d bytes/frame=%d "
2981                     "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2982                     ch->fraction));
2983
2984         err = uaudio_chan_alloc_buffers(sc, ch);
2985         if (err)
2986                 return EIO;
2987
2988         err = uaudio_chan_open(sc, ch);
2989         if (err) {
2990                 uaudio_chan_free_buffers(sc, ch);
2991                 return EIO;
2992         }
2993
2994         ch->intr = intr;
2995         ch->arg = arg;
2996
2997         s = splusb();
2998         for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX */
2999                 uaudio_chan_ptransfer(ch);
3000         splx(s);
3001
3002         return 0;
3003 }
3004 #endif  /* NetBSD or OpenBSD */
3005
3006 /* Set up a pipe for a channel. */
3007 Static usbd_status
3008 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch)
3009 {
3010         struct as_info *as;
3011         int endpt;
3012         usbd_status err;
3013
3014 #if defined(__FreeBSD__) || defined(__DragonFly__)
3015         if (sc->sc_dying)
3016                 return EIO;
3017 #endif
3018
3019         as = &sc->sc_alts[ch->altidx];
3020         endpt = as->edesc->bEndpointAddress;
3021         DPRINTF(("uaudio_chan_open: endpt=0x%02x, speed=%d, alt=%d\n",
3022                  endpt, ch->sample_rate, as->alt));
3023
3024         /* Set alternate interface corresponding to the mode. */
3025         err = usbd_set_interface(as->ifaceh, as->alt);
3026         if (err)
3027                 return err;
3028
3029         /*
3030          * If just one sampling rate is supported,
3031          * no need to call uaudio_set_speed().
3032          * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request.
3033          */
3034         if (as->asf1desc->bSamFreqType != 1) {
3035                 err = uaudio_set_speed(sc, endpt, ch->sample_rate);
3036                 if (err)
3037                         DPRINTF(("uaudio_chan_open: set_speed failed err=%s\n",
3038                                  usbd_errstr(err)));
3039         }
3040
3041         ch->pipe = 0;
3042         ch->sync_pipe = 0;
3043         DPRINTF(("uaudio_chan_open: create pipe to 0x%02x\n", endpt));
3044         err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->pipe);
3045         if (err)
3046                 return err;
3047         if (as->edesc1 != NULL) {
3048                 endpt = as->edesc1->bEndpointAddress;
3049                 DPRINTF(("uaudio_chan_open: create sync-pipe to 0x%02x\n", endpt));
3050                 err = usbd_open_pipe(as->ifaceh, endpt, 0, &ch->sync_pipe);
3051         }
3052         return err;
3053 }
3054
3055 Static void
3056 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch)
3057 {
3058         struct as_info *as;
3059
3060 #if defined(__FreeBSD__) || defined(__DragonFly__)
3061         if (sc->sc_dying)
3062                 return ;
3063 #endif
3064
3065         as = &sc->sc_alts[ch->altidx];
3066         as->sc_busy = 0;
3067         if (sc->sc_nullalt >= 0) {
3068                 DPRINTF(("uaudio_chan_close: set null alt=%d\n",
3069                          sc->sc_nullalt));
3070                 usbd_set_interface(as->ifaceh, sc->sc_nullalt);
3071         }
3072         if (ch->pipe) {
3073                 usbd_abort_pipe(ch->pipe);
3074                 usbd_close_pipe(ch->pipe);
3075         }
3076         if (ch->sync_pipe) {
3077                 usbd_abort_pipe(ch->sync_pipe);
3078                 usbd_close_pipe(ch->sync_pipe);
3079         }
3080 }
3081
3082 Static usbd_status
3083 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch)
3084 {
3085         usbd_xfer_handle xfer;
3086         void *buf;
3087         int i, size;
3088
3089         size = (ch->bytes_per_frame + ch->sample_size) * UAUDIO_NFRAMES;
3090         for (i = 0; i < UAUDIO_NCHANBUFS; i++) {
3091                 xfer = usbd_alloc_xfer(sc->sc_udev);
3092                 if (xfer == 0)
3093                         goto bad;
3094                 ch->chanbufs[i].xfer = xfer;
3095                 buf = usbd_alloc_buffer(xfer, size);
3096                 if (buf == 0) {
3097                         i++;
3098                         goto bad;
3099                 }
3100                 ch->chanbufs[i].buffer = buf;
3101                 ch->chanbufs[i].chan = ch;
3102         }
3103
3104         return USBD_NORMAL_COMPLETION;
3105
3106 bad:
3107         while (--i >= 0)
3108                 /* implicit buffer kfree */
3109                 usbd_free_xfer(ch->chanbufs[i].xfer);
3110         return USBD_NOMEM;
3111 }
3112
3113 Static void
3114 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch)
3115 {
3116         int i;
3117
3118         for (i = 0; i < UAUDIO_NCHANBUFS; i++)
3119                 usbd_free_xfer(ch->chanbufs[i].xfer);
3120 }
3121
3122 /* Called at splusb() */
3123 Static void
3124 uaudio_chan_ptransfer(struct chan *ch)
3125 {
3126         struct chanbuf *cb;
3127         int i, n, size, residue, total;
3128
3129         if (ch->sc->sc_dying)
3130                 return;
3131
3132         /* Pick the next channel buffer. */
3133         cb = &ch->chanbufs[ch->curchanbuf];
3134         if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
3135                 ch->curchanbuf = 0;
3136
3137         /* Compute the size of each frame in the next transfer. */
3138         residue = ch->residue;
3139         total = 0;
3140         for (i = 0; i < UAUDIO_NFRAMES; i++) {
3141                 size = ch->bytes_per_frame;
3142                 residue += ch->fraction;
3143                 if (residue >= USB_FRAMES_PER_SECOND) {
3144                         if ((ch->sc->sc_altflags & UA_NOFRAC) == 0)
3145                                 size += ch->sample_size;
3146                         residue -= USB_FRAMES_PER_SECOND;
3147                 }
3148                 cb->sizes[i] = size;
3149                 total += size;
3150         }
3151         ch->residue = residue;
3152         cb->size = total;
3153
3154         /*
3155          * Transfer data from upper layer buffer to channel buffer, taking
3156          * care of wrapping the upper layer buffer.
3157          */
3158         n = min(total, ch->end - ch->cur);
3159         memcpy(cb->buffer, ch->cur, n);
3160         ch->cur += n;
3161         if (ch->cur >= ch->end)
3162                 ch->cur = ch->start;
3163         if (total > n) {
3164                 total -= n;
3165                 memcpy(cb->buffer + n, ch->cur, total);
3166                 ch->cur += total;
3167         }
3168
3169 #ifdef USB_DEBUG
3170         if (uaudiodebug > 8) {
3171                 DPRINTF(("uaudio_chan_ptransfer: buffer=%p, residue=0.%03d\n",
3172                          cb->buffer, ch->residue));
3173                 for (i = 0; i < UAUDIO_NFRAMES; i++) {
3174                         DPRINTF(("   [%d] length %d\n", i, cb->sizes[i]));
3175                 }
3176         }
3177 #endif
3178
3179         DPRINTFN(5,("uaudio_chan_transfer: ptransfer xfer=%p\n", cb->xfer));
3180         /* Fill the request */
3181         usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
3182                              UAUDIO_NFRAMES, USBD_NO_COPY,
3183                              uaudio_chan_pintr);
3184
3185         (void)usbd_transfer(cb->xfer);
3186 }
3187
3188 Static void
3189 uaudio_chan_pintr(usbd_xfer_handle xfer, usbd_private_handle priv,
3190                   usbd_status status)
3191 {
3192         struct chanbuf *cb;
3193         struct chan *ch;
3194         u_int32_t count;
3195 #if !defined(__DragonFly__)
3196         int s;
3197 #endif
3198
3199         cb = priv;
3200         ch = cb->chan;
3201         /* Return if we are aborting. */
3202         if (status == USBD_CANCELLED)
3203                 return;
3204
3205         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
3206         DPRINTFN(5,("uaudio_chan_pintr: count=%d, transferred=%d\n",
3207                     count, ch->transferred));
3208 #ifdef DIAGNOSTIC
3209         if (count != cb->size) {
3210                 kprintf("uaudio_chan_pintr: count(%d) != size(%d)\n",
3211                        count, cb->size);
3212         }
3213 #endif
3214
3215         ch->transferred += cb->size;
3216 #if defined(__FreeBSD__)
3217         /* s = spltty(); */
3218         s = splhigh();
3219         chn_intr(ch->pcm_ch);
3220         splx(s);
3221 #elif defined(__DragonFly__)
3222         crit_enter();
3223         chn_intr(ch->pcm_ch);
3224         crit_exit();
3225 #else
3226         s = splaudio();
3227         /* Call back to upper layer */
3228         while (ch->transferred >= ch->blksize) {
3229                 ch->transferred -= ch->blksize;
3230                 DPRINTFN(5,("uaudio_chan_pintr: call %p(%p)\n",
3231                             ch->intr, ch->arg));
3232                 ch->intr(ch->arg);
3233         }
3234         splx(s);
3235 #endif
3236
3237         /* start next transfer */
3238         uaudio_chan_ptransfer(ch);
3239 }
3240
3241 /* Called at splusb() */
3242 Static void
3243 uaudio_chan_rtransfer(struct chan *ch)
3244 {
3245         struct chanbuf *cb;
3246         int i, size, residue, total;
3247
3248         if (ch->sc->sc_dying)
3249                 return;
3250
3251         /* Pick the next channel buffer. */
3252         cb = &ch->chanbufs[ch->curchanbuf];
3253         if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
3254                 ch->curchanbuf = 0;
3255
3256         /* Compute the size of each frame in the next transfer. */
3257         residue = ch->residue;
3258         total = 0;
3259         for (i = 0; i < UAUDIO_NFRAMES; i++) {
3260                 size = ch->bytes_per_frame;
3261                 cb->sizes[i] = size;
3262                 cb->offsets[i] = total;
3263                 total += size;
3264         }
3265         ch->residue = residue;
3266         cb->size = total;
3267
3268 #ifdef USB_DEBUG
3269         if (uaudiodebug > 8) {
3270                 DPRINTF(("uaudio_chan_rtransfer: buffer=%p, residue=0.%03d\n",
3271                          cb->buffer, ch->residue));
3272                 for (i = 0; i < UAUDIO_NFRAMES; i++) {
3273                         DPRINTF(("   [%d] length %d\n", i, cb->sizes[i]));
3274                 }
3275         }
3276 #endif
3277
3278         DPRINTFN(5,("uaudio_chan_rtransfer: transfer xfer=%p\n", cb->xfer));
3279         /* Fill the request */
3280         usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
3281                              UAUDIO_NFRAMES, USBD_NO_COPY,
3282                              uaudio_chan_rintr);
3283
3284         (void)usbd_transfer(cb->xfer);
3285 }
3286
3287 Static void
3288 uaudio_chan_rintr(usbd_xfer_handle xfer, usbd_private_handle priv,
3289                   usbd_status status)
3290 {
3291         struct chanbuf *cb = priv;
3292         struct chan *ch = cb->chan;
3293         u_int32_t count;
3294 #if !defined(__DragonFly__)
3295         int s;
3296 #endif
3297         int i, n, frsize;
3298
3299         /* Return if we are aborting. */
3300         if (status == USBD_CANCELLED)
3301                 return;
3302
3303         usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
3304         DPRINTFN(5,("uaudio_chan_rintr: count=%d, transferred=%d\n",
3305                     count, ch->transferred));
3306
3307         /* count < cb->size is normal for asynchronous source */
3308 #ifdef DIAGNOSTIC
3309         if (count > cb->size) {
3310                 kprintf("uaudio_chan_rintr: count(%d) > size(%d)\n",
3311                        count, cb->size);
3312         }
3313 #endif
3314
3315         /*
3316          * Transfer data from channel buffer to upper layer buffer, taking
3317          * care of wrapping the upper layer buffer.
3318          */
3319         for(i = 0; i < UAUDIO_NFRAMES; i++) {
3320                 frsize = cb->sizes[i];
3321                 n = min(frsize, ch->end - ch->cur);
3322                 memcpy(ch->cur, cb->buffer + cb->offsets[i], n);
3323                 ch->cur += n;
3324                 if (ch->cur >= ch->end)
3325                         ch->cur = ch->start;
3326                 if (frsize > n) {
3327                         memcpy(ch->cur, cb->buffer + cb->offsets[i] + n,
3328                             frsize - n);
3329                         ch->cur += frsize - n;
3330                 }
3331         }
3332
3333         /* Call back to upper layer */
3334         ch->transferred += count;
3335 #if defined(__FreeBSD__)
3336         s = spltty();
3337         chn_intr(ch->pcm_ch);
3338         splx(s);
3339 #elif defined(__DragonFly__)
3340         crit_enter();
3341         chn_intr(ch->pcm_ch);
3342         crit_exit();
3343 #else
3344         s = splaudio();
3345         while (ch->transferred >= ch->blksize) {
3346                 ch->transferred -= ch->blksize;
3347                 DPRINTFN(5,("uaudio_chan_rintr: call %p(%p)\n",
3348                             ch->intr, ch->arg));
3349                 ch->intr(ch->arg);
3350         }
3351         splx(s);
3352 #endif
3353
3354         /* start next transfer */
3355         uaudio_chan_rtransfer(ch);
3356 }
3357
3358 #if defined(__NetBSD__) || defined(__OpenBSD__)
3359 Static void
3360 uaudio_chan_init(struct chan *ch, int altidx, const struct audio_params *param,
3361     int maxpktsize)
3362 {
3363         int samples_per_frame, sample_size;
3364
3365         ch->altidx = altidx;
3366         sample_size = param->precision * param->factor * param->hw_channels / 8;
3367         samples_per_frame = param->hw_sample_rate / USB_FRAMES_PER_SECOND;
3368         ch->sample_size = sample_size;
3369         ch->sample_rate = param->hw_sample_rate;
3370         if (maxpktsize == 0) {
3371                 ch->fraction = param->hw_sample_rate % USB_FRAMES_PER_SECOND;
3372                 ch->bytes_per_frame = samples_per_frame * sample_size;
3373         } else {
3374                 ch->fraction = 0;
3375                 ch->bytes_per_frame = maxpktsize;
3376         }
3377         ch->residue = 0;
3378 }
3379
3380 Static void
3381 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize)
3382 {
3383         ch->start = start;
3384         ch->end = end;
3385         ch->cur = start;
3386         ch->blksize = blksize;
3387         ch->transferred = 0;
3388         ch->curchanbuf = 0;
3389 }
3390
3391 Static void
3392 uaudio_get_minmax_rates(int nalts, const struct as_info *alts,
3393                         const struct audio_params *p, int mode,
3394                         u_long *min, u_long *max)
3395 {
3396         const struct usb_audio_streaming_type1_descriptor *a1d;
3397         int i, j;
3398
3399         *min = ULONG_MAX;
3400         *max = 0;
3401         for (i = 0; i < nalts; i++) {
3402                 a1d = alts[i].asf1desc;
3403                 if (alts[i].sc_busy)
3404                         continue;
3405                 if (p->hw_channels != a1d->bNrChannels)
3406                         continue;
3407                 if (p->hw_precision != a1d->bBitResolution)
3408                         continue;
3409                 if (p->hw_encoding != alts[i].encoding)
3410                         continue;
3411                 if (mode != UE_GET_DIR(alts[i].edesc->bEndpointAddress))
3412                         continue;
3413                 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
3414                         DPRINTFN(2,("uaudio_get_minmax_rates: cont %d-%d\n",
3415                                     UA_SAMP_LO(a1d), UA_SAMP_HI(a1d)));
3416                         if (UA_SAMP_LO(a1d) < *min)
3417                                 *min = UA_SAMP_LO(a1d);
3418                         if (UA_SAMP_HI(a1d) > *max)
3419                                 *max = UA_SAMP_HI(a1d);
3420                 } else {
3421                         for (j = 0; j < a1d->bSamFreqType; j++) {
3422                                 DPRINTFN(2,("uaudio_get_minmax_rates: disc #%d: %d\n",
3423                                             j, UA_GETSAMP(a1d, j)));
3424                                 if (UA_GETSAMP(a1d, j) < *min)
3425                                         *min = UA_GETSAMP(a1d, j);
3426                                 if (UA_GETSAMP(a1d, j) > *max)
3427                                         *max = UA_GETSAMP(a1d, j);
3428                         }
3429                 }
3430         }
3431 }
3432
3433 Static int
3434 uaudio_match_alt_sub(int nalts, const struct as_info *alts,
3435                      const struct audio_params *p, int mode, u_long rate)
3436 {
3437         const struct usb_audio_streaming_type1_descriptor *a1d;
3438         int i, j;
3439
3440         DPRINTF(("uaudio_match_alt_sub: search for %luHz %dch\n",
3441                  rate, p->hw_channels));
3442         for (i = 0; i < nalts; i++) {
3443                 a1d = alts[i].asf1desc;
3444                 if (alts[i].sc_busy)
3445                         continue;
3446                 if (p->hw_channels != a1d->bNrChannels)
3447                         continue;
3448                 if (p->hw_precision != a1d->bBitResolution)
3449                         continue;
3450                 if (p->hw_encoding != alts[i].encoding)
3451                         continue;
3452                 if (mode != UE_GET_DIR(alts[i].edesc->bEndpointAddress))
3453                         continue;
3454                 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
3455                         DPRINTFN(3,("uaudio_match_alt_sub: cont %d-%d\n",
3456                                     UA_SAMP_LO(a1d), UA_SAMP_HI(a1d)));
3457                         if (UA_SAMP_LO(a1d) <= rate && rate <= UA_SAMP_HI(a1d))
3458                                 return i;
3459                 } else {
3460                         for (j = 0; j < a1d->bSamFreqType; j++) {
3461                                 DPRINTFN(3,("uaudio_match_alt_sub: disc #%d: %d\n",
3462                                             j, UA_GETSAMP(a1d, j)));
3463                                 /* XXX allow for some slack */
3464                                 if (UA_GETSAMP(a1d, j) == rate)
3465                                         return i;
3466                         }
3467                 }
3468         }
3469         return -1;
3470 }
3471
3472 Static int
3473 uaudio_match_alt_chan(int nalts, const struct as_info *alts,
3474                       struct audio_params *p, int mode)
3475 {
3476         int i, n;
3477         u_long min, max;
3478         u_long rate;
3479
3480         /* Exact match */
3481         DPRINTF(("uaudio_match_alt_chan: examine %ldHz %dch %dbit.\n",
3482                  p->sample_rate, p->hw_channels, p->hw_precision));
3483         i = uaudio_match_alt_sub(nalts, alts, p, mode, p->sample_rate);
3484         if (i >= 0)
3485                 return i;
3486
3487         uaudio_get_minmax_rates(nalts, alts, p, mode, &min, &max);
3488         DPRINTF(("uaudio_match_alt_chan: min=%lu max=%lu\n", min, max));
3489         if (max <= 0)
3490                 return -1;
3491         /* Search for biggers */
3492         n = 2;
3493         while ((rate = p->sample_rate * n++) <= max) {
3494                 i = uaudio_match_alt_sub(nalts, alts, p, mode, rate);
3495                 if (i >= 0) {
3496                         p->hw_sample_rate = rate;
3497                         return i;
3498                 }
3499         }
3500         if (p->sample_rate >= min) {
3501                 i = uaudio_match_alt_sub(nalts, alts, p, mode, max);
3502                 if (i >= 0) {
3503                         p->hw_sample_rate = max;
3504                         return i;
3505                 }
3506         } else {
3507                 i = uaudio_match_alt_sub(nalts, alts, p, mode, min);
3508                 if (i >= 0) {
3509                         p->hw_sample_rate = min;
3510                         return i;
3511                 }
3512         }
3513         return -1;
3514 }
3515
3516 Static int
3517 uaudio_match_alt(int nalts, const struct as_info *alts,
3518                  struct audio_params *p, int mode)
3519 {
3520         int i, n;
3521
3522         mode = mode == AUMODE_PLAY ? UE_DIR_OUT : UE_DIR_IN;
3523         i = uaudio_match_alt_chan(nalts, alts, p, mode);
3524         if (i >= 0)
3525                 return i;
3526
3527         for (n = p->channels + 1; n <= AUDIO_MAX_CHANNELS; n++) {
3528                 p->hw_channels = n;
3529                 i = uaudio_match_alt_chan(nalts, alts, p, mode);
3530                 if (i >= 0)
3531                         return i;
3532         }
3533
3534         if (p->channels != 2)
3535                 return -1;
3536         p->hw_channels = 1;
3537         return uaudio_match_alt_chan(nalts, alts, p, mode);
3538 }
3539
3540 Static int
3541 uaudio_set_params(void *addr, int setmode, int usemode,
3542                   struct audio_params *play, struct audio_params *rec)
3543 {
3544         struct uaudio_softc *sc;
3545         int flags;
3546         int factor;
3547         int enc, i;
3548         int paltidx, raltidx;
3549         void (*swcode)(void *, u_char *buf, int cnt);
3550         struct audio_params *p;
3551         int mode;
3552
3553         sc = addr;
3554         flags = sc->sc_altflags;
3555         paltidx = -1;
3556         raltidx = -1;
3557         if (sc->sc_dying)
3558                 return EIO;
3559
3560         if (((usemode & AUMODE_PLAY) && sc->sc_playchan.pipe != NULL) ||
3561             ((usemode & AUMODE_RECORD) && sc->sc_recchan.pipe != NULL))
3562                 return EBUSY;
3563
3564         if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1)
3565                 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0;
3566         if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1)
3567                 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0;
3568
3569         /* Some uaudio devices are unidirectional.  Don't try to find a
3570            matching mode for the unsupported direction. */
3571         setmode &= sc->sc_mode;
3572
3573         for (mode = AUMODE_RECORD; mode != -1;
3574              mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
3575                 if ((setmode & mode) == 0)
3576                         continue;
3577
3578                 p = (mode == AUMODE_PLAY) ? play : rec;
3579
3580                 factor = 1;
3581                 swcode = 0;
3582                 enc = p->encoding;
3583                 switch (enc) {
3584                 case AUDIO_ENCODING_SLINEAR_BE:
3585                         /* FALLTHROUGH */
3586                 case AUDIO_ENCODING_SLINEAR_LE:
3587                         if (enc == AUDIO_ENCODING_SLINEAR_BE
3588                             && p->precision == 16 && (flags & HAS_16)) {
3589                                 swcode = swap_bytes;
3590                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3591                         } else if (p->precision == 8) {
3592                                 if (flags & HAS_8) {
3593                                         /* No conversion */
3594                                 } else if (flags & HAS_8U) {
3595                                         swcode = change_sign8;
3596                                         enc = AUDIO_ENCODING_ULINEAR_LE;
3597                                 } else if (flags & HAS_16) {
3598                                         factor = 2;
3599                                         p->hw_precision = 16;
3600                                         if (mode == AUMODE_PLAY)
3601                                                 swcode = linear8_to_linear16_le;
3602                                         else
3603                                                 swcode = linear16_to_linear8_le;
3604                                 }
3605                         }
3606                         break;
3607                 case AUDIO_ENCODING_ULINEAR_BE:
3608                         /* FALLTHROUGH */
3609                 case AUDIO_ENCODING_ULINEAR_LE:
3610                         if (p->precision == 16) {
3611                                 if (enc == AUDIO_ENCODING_ULINEAR_LE)
3612                                         swcode = change_sign16_le;
3613                                 else if (mode == AUMODE_PLAY)
3614                                         swcode = swap_bytes_change_sign16_le;
3615                                 else
3616                                         swcode = change_sign16_swap_bytes_le;
3617                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3618                         } else if (p->precision == 8) {
3619                                 if (flags & HAS_8U) {
3620                                         /* No conversion */
3621                                 } else if (flags & HAS_8) {
3622                                         swcode = change_sign8;
3623                                         enc = AUDIO_ENCODING_SLINEAR_LE;
3624                                 } else if (flags & HAS_16) {
3625                                         factor = 2;
3626                                         p->hw_precision = 16;
3627                                         enc = AUDIO_ENCODING_SLINEAR_LE;
3628                                         if (mode == AUMODE_PLAY)
3629                                                 swcode = ulinear8_to_slinear16_le;
3630                                         else
3631                                                 swcode = slinear16_to_ulinear8_le;
3632                                 }
3633                         }
3634                         break;
3635                 case AUDIO_ENCODING_ULAW:
3636                         if (flags & HAS_MULAW)
3637                                 break;
3638                         if (flags & HAS_16) {
3639                                 if (mode == AUMODE_PLAY)
3640                                         swcode = mulaw_to_slinear16_le;
3641                                 else
3642                                         swcode = slinear16_to_mulaw_le;
3643                                 factor = 2;
3644                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3645                                 p->hw_precision = 16;
3646                         } else if (flags & HAS_8U) {
3647                                 if (mode == AUMODE_PLAY)
3648                                         swcode = mulaw_to_ulinear8;
3649                                 else
3650                                         swcode = ulinear8_to_mulaw;
3651                                 enc = AUDIO_ENCODING_ULINEAR_LE;
3652                         } else if (flags & HAS_8) {
3653                                 if (mode == AUMODE_PLAY)
3654                                         swcode = mulaw_to_slinear8;
3655                                 else
3656                                         swcode = slinear8_to_mulaw;
3657                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3658                         } else
3659                                 return (EINVAL);
3660                         break;
3661                 case AUDIO_ENCODING_ALAW:
3662                         if (flags & HAS_ALAW)
3663                                 break;
3664                         if (mode == AUMODE_PLAY && (flags & HAS_16)) {
3665                                 swcode = alaw_to_slinear16_le;
3666                                 factor = 2;
3667                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3668                                 p->hw_precision = 16;
3669                         } else if (flags & HAS_8U) {
3670                                 if (mode == AUMODE_PLAY)
3671                                         swcode = alaw_to_ulinear8;
3672                                 else
3673                                         swcode = ulinear8_to_alaw;
3674                                 enc = AUDIO_ENCODING_ULINEAR_LE;
3675                         } else if (flags & HAS_8) {
3676                                 if (mode == AUMODE_PLAY)
3677                                         swcode = alaw_to_slinear8;
3678                                 else
3679                                         swcode = slinear8_to_alaw;
3680                                 enc = AUDIO_ENCODING_SLINEAR_LE;
3681                         } else
3682                                 return (EINVAL);
3683                         break;
3684                 default:
3685                         return (EINVAL);
3686                 }
3687                 /* XXX do some other conversions... */
3688
3689                 DPRINTF(("uaudio_set_params: chan=%d prec=%d enc=%d rate=%ld\n",
3690                          p->channels, p->hw_precision, enc, p->sample_rate));
3691
3692                 p->hw_encoding = enc;
3693                 i = uaudio_match_alt(sc->sc_nalts, sc->sc_alts, p, mode);
3694                 if (i < 0)
3695                         return (EINVAL);
3696
3697                 p->sw_code = swcode;
3698                 p->factor  = factor;
3699
3700                 if (mode == AUMODE_PLAY)
3701                         paltidx = i;
3702                 else
3703                         raltidx = i;
3704         }
3705
3706         if ((setmode & AUMODE_PLAY)) {
3707                 /* XXX abort transfer if currently happening? */
3708                 uaudio_chan_init(&sc->sc_playchan, paltidx, play, 0);
3709         }
3710         if ((setmode & AUMODE_RECORD)) {
3711                 /* XXX abort transfer if currently happening? */
3712                 uaudio_chan_init(&sc->sc_recchan, raltidx, rec,
3713                     UGETW(sc->sc_alts[raltidx].edesc->wMaxPacketSize));
3714         }
3715
3716         if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1)
3717                 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1;
3718         if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1)
3719                 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1;
3720
3721         DPRINTF(("uaudio_set_params: use altidx=p%d/r%d, altno=p%d/r%d\n",
3722                  sc->sc_playchan.altidx, sc->sc_recchan.altidx,
3723                  (sc->sc_playchan.altidx >= 0)
3724                    ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting
3725                    : -1,
3726                  (sc->sc_recchan.altidx >= 0)
3727                    ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting
3728                    : -1));
3729
3730         return 0;
3731 }
3732 #endif /* NetBSD or OpenBSD */
3733
3734 Static usbd_status
3735 uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed)
3736 {
3737         usb_device_request_t req;
3738         uint8_t data[3];
3739
3740         DPRINTFN(5,("uaudio_set_speed: endpt=%d speed=%u\n", endpt, speed));
3741         req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
3742         req.bRequest = SET_CUR;
3743         USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
3744         USETW(req.wIndex, endpt);
3745         USETW(req.wLength, 3);
3746         data[0] = speed;
3747         data[1] = speed >> 8;
3748         data[2] = speed >> 16;
3749
3750         return usbd_do_request(sc->sc_udev, &req, data);
3751 }
3752
3753
3754 #if defined(__FreeBSD__) || defined(__DragonFly__)
3755 /************************************************************/
3756 int
3757 uaudio_init_params(struct uaudio_softc *sc, struct chan *ch, int mode)
3758 {
3759         int i, j, enc;
3760         int samples_per_frame, sample_size;
3761
3762         if ((sc->sc_playchan.pipe != NULL) || (sc->sc_recchan.pipe != NULL))
3763                 return (-1);
3764
3765         switch(ch->format & 0x000FFFFF) {
3766         case AFMT_U8:
3767                 enc = AUDIO_ENCODING_ULINEAR_LE;
3768                 ch->precision = 8;
3769                 break;
3770         case AFMT_S8:
3771                 enc = AUDIO_ENCODING_SLINEAR_LE;
3772                 ch->precision = 8;
3773                 break;
3774         case AFMT_A_LAW:        /* ? */
3775                 enc = AUDIO_ENCODING_ALAW;
3776                 ch->precision = 8;
3777                 break;
3778         case AFMT_MU_LAW:       /* ? */
3779                 enc = AUDIO_ENCODING_ULAW;
3780                 ch->precision = 8;
3781                 break;
3782         case AFMT_S16_LE:
3783                 enc = AUDIO_ENCODING_SLINEAR_LE;
3784                 ch->precision = 16;
3785                 break;
3786         case AFMT_S16_BE:
3787                 enc = AUDIO_ENCODING_SLINEAR_BE;
3788                 ch->precision = 16;
3789                 break;
3790         case AFMT_U16_LE:
3791                 enc = AUDIO_ENCODING_ULINEAR_LE;
3792                 ch->precision = 16;
3793                 break;
3794         case AFMT_U16_BE:
3795                 enc = AUDIO_ENCODING_ULINEAR_BE;
3796                 ch->precision = 16;
3797                 break;
3798         case AFMT_S24_LE:
3799                 enc = AUDIO_ENCODING_SLINEAR_LE;
3800                 ch->precision = 24;
3801                 break;
3802         case AFMT_S24_BE:
3803                 enc = AUDIO_ENCODING_SLINEAR_BE;
3804                 ch->precision = 24;
3805                 break;
3806         case AFMT_U24_LE:
3807                 enc = AUDIO_ENCODING_ULINEAR_LE;
3808                 ch->precision = 24;
3809                 break;
3810         case AFMT_U24_BE:
3811                 enc = AUDIO_ENCODING_ULINEAR_BE;
3812                 ch->precision = 24;
3813                 break;
3814         case AFMT_S32_LE:
3815                 enc = AUDIO_ENCODING_SLINEAR_LE;
3816                 ch->precision = 32;
3817                 break;
3818         case AFMT_S32_BE:
3819                 enc = AUDIO_ENCODING_SLINEAR_BE;
3820                 ch->precision = 32;
3821                 break;
3822         case AFMT_U32_LE:
3823                 enc = AUDIO_ENCODING_ULINEAR_LE;
3824                 ch->precision = 32;
3825                 break;
3826         case AFMT_U32_BE:
3827                 enc = AUDIO_ENCODING_ULINEAR_BE;
3828                 ch->precision = 32;
3829                 break;
3830         default:
3831                 enc = 0;
3832                 ch->precision = 16;
3833                 kprintf("Unknown format %x\n", ch->format);
3834         }
3835
3836         if (ch->format & AFMT_STEREO) {
3837                 ch->channels = 2;
3838         } else {
3839                 ch->channels = 1;
3840         }
3841
3842 /*      for (mode =  ......      */
3843                 for (i = 0; i < sc->sc_nalts; i++) {
3844                         const struct usb_audio_streaming_type1_descriptor *a1d =
3845                                 sc->sc_alts[i].asf1desc;
3846                         if (ch->channels == a1d->bNrChannels &&
3847                             ch->precision == a1d->bBitResolution &&
3848 #if 0
3849                             enc == sc->sc_alts[i].encoding) {
3850 #else
3851                             enc == sc->sc_alts[i].encoding &&
3852                             (mode == AUMODE_PLAY ? UE_DIR_OUT : UE_DIR_IN) ==
3853                             UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress)) {
3854 #endif
3855                                 if (a1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
3856                                         DPRINTFN(2,("uaudio_set_params: cont %d-%d\n",
3857                                             UA_SAMP_LO(a1d), UA_SAMP_HI(a1d)));
3858                                         if (UA_SAMP_LO(a1d) <= ch->sample_rate &&
3859                                             ch->sample_rate <= UA_SAMP_HI(a1d)) {
3860                                                 if (mode == AUMODE_PLAY)
3861                                                         sc->sc_playchan.altidx = i;
3862                                                 else
3863                                                         sc->sc_recchan.altidx = i;
3864                                                 goto found;
3865                                         }
3866                                 } else {
3867                                         for (j = 0; j < a1d->bSamFreqType; j++) {
3868                                                 DPRINTFN(2,("uaudio_set_params: disc #"
3869                                                     "%d: %d\n", j, UA_GETSAMP(a1d, j)));
3870                                                 /* XXX allow for some slack */
3871                                                 if (UA_GETSAMP(a1d, j) ==
3872                                                     ch->sample_rate) {
3873                                                         if (mode == AUMODE_PLAY)
3874                                                                 sc->sc_playchan.altidx = i;
3875                                                         else
3876                                                                 sc->sc_recchan.altidx = i;
3877                                                         goto found;
3878                                                 }
3879                                         }
3880                                 }
3881                         }
3882                 }
3883                 /* return (EINVAL); */
3884                 if (mode == AUMODE_PLAY) 
3885                         kprintf("uaudio: This device can't play in rate=%d.\n", ch->sample_rate);
3886                 else
3887                         kprintf("uaudio: This device can't record in rate=%d.\n", ch->sample_rate);
3888                 return (-1);
3889
3890         found:
3891 #if 0 /* XXX */
3892                 p->sw_code = swcode;
3893                 p->factor  = factor;
3894                 if (usemode == mode)
3895                         sc->sc_curaltidx = i;
3896 #endif
3897 /*      } */
3898
3899         sample_size = ch->precision * ch->channels / 8;
3900         samples_per_frame = ch->sample_rate / USB_FRAMES_PER_SECOND;
3901         ch->fraction = ch->sample_rate % USB_FRAMES_PER_SECOND;
3902         ch->sample_size = sample_size;
3903         ch->bytes_per_frame = samples_per_frame * sample_size;
3904         ch->residue = 0;
3905
3906         ch->cur = ch->start;
3907         ch->transferred = 0;
3908         ch->curchanbuf = 0;
3909         return (0);
3910 }
3911
3912 struct uaudio_conversion {
3913         uint8_t uaudio_fmt;
3914         uint8_t uaudio_prec;
3915         uint32_t freebsd_fmt;
3916 };
3917
3918 const struct uaudio_conversion const accepted_conversion[] = {
3919         {AUDIO_ENCODING_ULINEAR_LE, 8, AFMT_U8},
3920         {AUDIO_ENCODING_ULINEAR_LE, 16, AFMT_U16_LE},
3921         {AUDIO_ENCODING_ULINEAR_LE, 24, AFMT_U24_LE},
3922         {AUDIO_ENCODING_ULINEAR_LE, 32, AFMT_U32_LE},
3923         {AUDIO_ENCODING_ULINEAR_BE, 16, AFMT_U16_BE},
3924         {AUDIO_ENCODING_ULINEAR_BE, 24, AFMT_U24_BE},
3925         {AUDIO_ENCODING_ULINEAR_BE, 32, AFMT_U32_BE},
3926         {AUDIO_ENCODING_SLINEAR_LE, 8, AFMT_S8},
3927         {AUDIO_ENCODING_SLINEAR_LE, 16, AFMT_S16_LE},
3928         {AUDIO_ENCODING_SLINEAR_LE, 24, AFMT_S24_LE},
3929         {AUDIO_ENCODING_SLINEAR_LE, 32, AFMT_S32_LE},
3930         {AUDIO_ENCODING_SLINEAR_BE, 16, AFMT_S16_BE},
3931         {AUDIO_ENCODING_SLINEAR_BE, 24, AFMT_S24_BE},
3932         {AUDIO_ENCODING_SLINEAR_BE, 32, AFMT_S32_BE},
3933         {AUDIO_ENCODING_ALAW, 8, AFMT_A_LAW},
3934         {AUDIO_ENCODING_ULAW, 8, AFMT_MU_LAW},
3935         {0,0,0}
3936 };
3937
3938 unsigned
3939 uaudio_query_formats(device_t dev, int reqdir, unsigned maxfmt, struct pcmchan_caps *cap)
3940 {
3941         struct uaudio_softc *sc;
3942         const struct usb_audio_streaming_type1_descriptor *asf1d;
3943         const struct uaudio_conversion *iterator;
3944         unsigned fmtcount, foundcount;
3945         u_int32_t fmt;
3946         uint8_t format, numchan, subframesize, prec, dir, iscontinuous;
3947         int freq, freq_min, freq_max;
3948         char *numchannel_descr;
3949         char freq_descr[64];
3950         int i,r;
3951
3952         sc = device_get_softc(dev);
3953         if (sc == NULL)
3954                 return 0;
3955
3956         cap->minspeed = cap->maxspeed = 0;
3957         foundcount = fmtcount = 0;
3958
3959         for (i = 0; i < sc->sc_nalts; i++) {
3960                 dir = UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress);
3961
3962                 if ((dir == UE_DIR_OUT) != (reqdir == PCMDIR_PLAY))
3963                         continue;
3964
3965                 asf1d = sc->sc_alts[i].asf1desc;
3966                 format = sc->sc_alts[i].encoding;
3967
3968                 numchan = asf1d->bNrChannels;
3969                 subframesize = asf1d->bSubFrameSize;
3970                 prec = asf1d->bBitResolution;   /* precision */
3971                 iscontinuous = asf1d->bSamFreqType == UA_SAMP_CONTNUOUS;
3972
3973                 if (iscontinuous)
3974                         ksnprintf(freq_descr, sizeof(freq_descr), "continous min %d max %d", UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d));
3975                 else
3976                         ksnprintf(freq_descr, sizeof(freq_descr), "fixed frequency (%d listed formats)", asf1d->bSamFreqType);
3977
3978                 if (numchan == 1)
3979                         numchannel_descr = " (mono)";
3980                 else if (numchan == 2)
3981                         numchannel_descr = " (stereo)";
3982                 else
3983                         numchannel_descr = "";
3984
3985                 if (bootverbose) {
3986                         device_printf(dev, "uaudio_query_formats: found a native %s channel%s %s %dbit %dbytes/subframe X %d channels = %d bytes per sample\n",
3987                                         (dir==UE_DIR_OUT)?"playback":"record",
3988                                         numchannel_descr, freq_descr,
3989                                         prec, subframesize, numchan, subframesize*numchan);
3990                 }
3991                 /*
3992                  * Now start rejecting the ones that don't map to FreeBSD
3993                  */
3994
3995                 if (numchan != 1 && numchan != 2)
3996                         continue;
3997
3998                 for (iterator = accepted_conversion ; iterator->uaudio_fmt != 0 ; iterator++)
3999                         if (iterator->uaudio_fmt == format && iterator->uaudio_prec == prec)
4000                                 break;
4001
4002                 if (iterator->uaudio_fmt == 0)
4003                         continue;
4004
4005                 fmt = iterator->freebsd_fmt;
4006
4007                 if (numchan == 2)
4008                         fmt |= AFMT_STEREO;
4009
4010                 foundcount++;
4011
4012                 if (fmtcount >= maxfmt)
4013                         continue;
4014
4015                 cap->fmtlist[fmtcount++] = fmt;
4016
4017                 if (iscontinuous) {
4018                         freq_min = UA_SAMP_LO(asf1d);
4019                         freq_max = UA_SAMP_HI(asf1d);
4020
4021                         if (cap->minspeed == 0 || freq_min < cap->minspeed)
4022                                 cap->minspeed = freq_min;
4023                         if (cap->maxspeed == 0)
4024                                 cap->maxspeed = cap->minspeed;
4025                         if (freq_max > cap->maxspeed)
4026                                 cap->maxspeed = freq_max;
4027                 } else {
4028                         for (r = 0; r < asf1d->bSamFreqType; r++) {
4029                                 freq = UA_GETSAMP(asf1d, r);
4030                                 if (cap->minspeed == 0 || freq < cap->minspeed)
4031                                         cap->minspeed = freq;
4032                                 if (cap->maxspeed == 0)
4033                                         cap->maxspeed = cap->minspeed;
4034                                 if (freq > cap->maxspeed)
4035                                         cap->maxspeed = freq;
4036                         }
4037                 }
4038         }
4039         cap->fmtlist[fmtcount] = 0;
4040         return foundcount;
4041 }
4042
4043 void
4044 uaudio_chan_set_param_pcm_dma_buff(device_t dev, u_char *start, u_char *end,
4045                 struct pcm_channel *pc, int dir)
4046 {
4047         struct uaudio_softc *sc;
4048         struct chan *ch;
4049
4050         sc = device_get_softc(dev);
4051 #ifndef NO_RECORDING
4052         if (dir == PCMDIR_PLAY)
4053                 ch = &sc->sc_playchan;
4054         else
4055                 ch = &sc->sc_recchan;
4056 #else
4057         ch = &sc->sc_playchan;
4058 #endif
4059
4060         ch->start = start;
4061         ch->end = end;
4062
4063         ch->pcm_ch = pc;
4064
4065         return;
4066 }
4067
4068 void
4069 uaudio_chan_set_param_blocksize(device_t dev, u_int32_t blocksize, int dir)
4070 {
4071         struct uaudio_softc *sc;
4072         struct chan *ch;
4073
4074         sc = device_get_softc(dev);
4075 #ifndef NO_RECORDING
4076         if (dir == PCMDIR_PLAY)
4077                 ch = &sc->sc_playchan;
4078         else
4079                 ch = &sc->sc_recchan;
4080 #else
4081         ch = &sc->sc_playchan;
4082 #endif
4083
4084         ch->blksize = blocksize;
4085
4086         return;
4087 }
4088
4089 int
4090 uaudio_chan_set_param_speed(device_t dev, u_int32_t speed, int reqdir)
4091 {
4092         const struct uaudio_conversion *iterator;
4093         struct uaudio_softc *sc;
4094         struct chan *ch;
4095         int i, r, score, hiscore, bestspeed;
4096
4097         sc = device_get_softc(dev);
4098 #ifndef NO_RECORDING
4099         if (reqdir == PCMDIR_PLAY)
4100                 ch = &sc->sc_playchan;
4101         else
4102                 ch = &sc->sc_recchan;
4103 #else
4104         ch = &sc->sc_playchan;
4105 #endif
4106         /*
4107          * We are successful if we find an endpoint that matches our selected format and it
4108          * supports the requested speed.
4109          */
4110         hiscore = 0;
4111         bestspeed = 1;
4112         for (i = 0; i < sc->sc_nalts; i++) {
4113                 int dir = UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress);
4114                 int format = sc->sc_alts[i].encoding;
4115                 const struct usb_audio_streaming_type1_descriptor *asf1d = sc->sc_alts[i].asf1desc;
4116                 int iscontinuous = asf1d->bSamFreqType == UA_SAMP_CONTNUOUS;
4117
4118                 if ((dir == UE_DIR_OUT) != (reqdir == PCMDIR_PLAY))
4119                         continue;
4120
4121                 for (iterator = accepted_conversion ; iterator->uaudio_fmt != 0 ; iterator++)
4122                         if (iterator->uaudio_fmt != format || iterator->freebsd_fmt != (ch->format&0xfffffff))
4123                                 continue;
4124                         if (iscontinuous) {
4125                                 if (speed >= UA_SAMP_LO(asf1d) && speed <= UA_SAMP_HI(asf1d)) {
4126                                         ch->sample_rate = speed;
4127                                         return speed;
4128                                 } else if (speed < UA_SAMP_LO(asf1d)) {
4129                                         score = 0xfff * speed / UA_SAMP_LO(asf1d);
4130                                         if (score > hiscore) {
4131                                                 bestspeed = UA_SAMP_LO(asf1d);
4132                                                 hiscore = score;
4133                                         }
4134                                 } else if (speed < UA_SAMP_HI(asf1d)) {
4135                                         score = 0xfff * UA_SAMP_HI(asf1d) / speed;
4136                                         if (score > hiscore) {
4137                                                 bestspeed = UA_SAMP_HI(asf1d);
4138                                                 hiscore = score;
4139                                         }
4140                                 }
4141                                 continue;
4142                         }
4143                         for (r = 0; r < asf1d->bSamFreqType; r++) {
4144                                 if (speed == UA_GETSAMP(asf1d, r)) {
4145                                         ch->sample_rate = speed;
4146                                         return speed;
4147                                 }
4148                                 if (speed > UA_GETSAMP(asf1d, r))
4149                                         score = 0xfff * UA_GETSAMP(asf1d, r) / speed;
4150                                 else
4151                                         score = 0xfff * speed / UA_GETSAMP(asf1d, r);
4152                                 if (score > hiscore) { 
4153                                         bestspeed = UA_GETSAMP(asf1d, r);
4154                                         hiscore = score;
4155                                 }
4156                         }
4157         }
4158         if (bestspeed != 1) {
4159                 ch->sample_rate = bestspeed;
4160                 return bestspeed;
4161         }
4162
4163         return 0;
4164 }
4165
4166 int
4167 uaudio_chan_getptr(device_t dev, int dir)
4168 {
4169         struct uaudio_softc *sc;
4170         struct chan *ch;
4171         int ptr;
4172
4173         sc = device_get_softc(dev);
4174 #ifndef NO_RECORDING
4175         if (dir == PCMDIR_PLAY)
4176                 ch = &sc->sc_playchan;
4177         else
4178                 ch = &sc->sc_recchan;
4179 #else
4180         ch = &sc->sc_playchan;
4181 #endif
4182
4183         ptr = ch->cur - ch->start;
4184
4185         return ptr;
4186 }
4187
4188 void
4189 uaudio_chan_set_param_format(device_t dev, u_int32_t format, int dir)
4190 {
4191         struct uaudio_softc *sc;
4192         struct chan *ch;
4193
4194         sc = device_get_softc(dev);
4195 #ifndef NO_RECORDING
4196         if (dir == PCMDIR_PLAY)
4197                 ch = &sc->sc_playchan;
4198         else
4199                 ch = &sc->sc_recchan;
4200 #else
4201         ch = &sc->sc_playchan;
4202 #endif
4203
4204         ch->format = format;
4205
4206         return;
4207 }
4208
4209 int
4210 uaudio_halt_out_dma(device_t dev)
4211 {
4212         struct uaudio_softc *sc;
4213
4214         sc = device_get_softc(dev);
4215
4216         DPRINTF(("uaudio_halt_out_dma: enter\n"));
4217         if (sc->sc_playchan.pipe != NULL) {
4218                 uaudio_chan_close(sc, &sc->sc_playchan);
4219                 sc->sc_playchan.pipe = 0;
4220                 uaudio_chan_free_buffers(sc, &sc->sc_playchan);
4221         }
4222         return (0);
4223 }
4224
4225 int
4226 uaudio_halt_in_dma(device_t dev)
4227 {
4228         struct uaudio_softc *sc;
4229
4230         sc = device_get_softc(dev);
4231
4232         if (sc->sc_dying)
4233                 return (EIO);
4234
4235         DPRINTF(("uaudio_halt_in_dma: enter\n"));
4236         if (sc->sc_recchan.pipe != NULL) {
4237                 uaudio_chan_close(sc, &sc->sc_recchan);
4238                 sc->sc_recchan.pipe = NULL;
4239                 uaudio_chan_free_buffers(sc, &sc->sc_recchan);
4240 /*              sc->sc_recchan.intr = NULL; */
4241         }
4242         return (0);
4243 }
4244
4245 int
4246 uaudio_trigger_input(device_t dev)
4247 {
4248         struct uaudio_softc *sc;
4249         struct chan *ch;
4250         usbd_status err;
4251         int i;
4252 #if !defined(__DragonFly__)
4253         int s;
4254 #endif
4255
4256         sc = device_get_softc(dev);
4257         ch = &sc->sc_recchan;
4258
4259         if (sc->sc_dying)
4260                 return (EIO);
4261
4262 /*      uaudio_chan_set_param(ch, start, end, blksize) */
4263         if (uaudio_init_params(sc, ch, AUMODE_RECORD))
4264                 return (EIO);
4265
4266         err = uaudio_chan_alloc_buffers(sc, ch);
4267         if (err)
4268                 return (EIO);
4269
4270         err = uaudio_chan_open(sc, ch);
4271         if (err) {
4272                 uaudio_chan_free_buffers(sc, ch);
4273                 return (EIO);
4274         }
4275
4276 /*      ch->intr = intr;
4277         ch->arg = arg; */
4278
4279 #if defined(__DragonFly__)
4280         crit_enter();
4281 #else
4282         s = splusb();
4283 #endif
4284         for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX -1 shouldn't be needed */
4285                 uaudio_chan_rtransfer(ch);
4286 #if defined(__DragonFly__)
4287         crit_exit();
4288 #else
4289         splx(s);
4290 #endif
4291
4292         return (0);
4293 }
4294
4295 int
4296 uaudio_trigger_output(device_t dev)
4297 {
4298         struct uaudio_softc *sc;
4299         struct chan *ch;
4300         usbd_status err;
4301         int i;
4302 #if !defined(__DragonFly__)
4303         int s;
4304 #endif
4305
4306         sc = device_get_softc(dev);
4307         ch = &sc->sc_playchan;
4308
4309         if (sc->sc_dying)
4310                 return (EIO);
4311
4312         if (uaudio_init_params(sc, ch, AUMODE_PLAY))
4313                 return (EIO);
4314
4315         err = uaudio_chan_alloc_buffers(sc, ch);
4316         if (err)
4317                 return (EIO);
4318
4319         err = uaudio_chan_open(sc, ch);
4320         if (err) {
4321                 uaudio_chan_free_buffers(sc, ch);
4322                 return (EIO);
4323         }
4324
4325 #if defined(__DragonFly__)
4326         crit_enter();
4327 #else
4328         s = splusb();
4329 #endif
4330         for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX */
4331                 uaudio_chan_ptransfer(ch);
4332 #if defined(__DragonFly__)
4333         crit_exit();
4334 #else
4335         splx(s);
4336 #endif
4337
4338         return (0);
4339 }
4340
4341 u_int32_t
4342 uaudio_query_mix_info(device_t dev)
4343 {
4344         int i;
4345         u_int32_t mask = 0;
4346         struct uaudio_softc *sc;
4347         struct mixerctl *mc;
4348
4349         sc = device_get_softc(dev);
4350         for (i=0; i < sc->sc_nctls; i++) {
4351                 mc = &sc->sc_ctls[i];
4352                 if (mc->ctl != SOUND_MIXER_NRDEVICES) {
4353                         /* Set device mask bits. 
4354                            See /usr/include/machine/soundcard.h */
4355                         mask |= (1 << mc->ctl);
4356                 }
4357         }
4358         return mask;
4359 }
4360
4361 u_int32_t
4362 uaudio_query_recsrc_info(device_t dev)
4363 {
4364         int i, rec_selector_id;
4365         u_int32_t mask = 0;
4366         struct uaudio_softc *sc;
4367         struct mixerctl *mc;
4368
4369         sc = device_get_softc(dev);
4370         rec_selector_id = -1;
4371         for (i=0; i < sc->sc_nctls; i++) {
4372                 mc = &sc->sc_ctls[i];
4373                 if (mc->ctl == SOUND_MIXER_NRDEVICES && 
4374                     mc->type == MIX_SELECTOR && mc->class == UAC_RECORD) {
4375                         if (rec_selector_id == -1) {
4376                                 rec_selector_id = i;
4377                         } else {
4378                                 kprintf("There are many selectors.  Can't recognize which selector is a record source selector.\n");
4379                                 return mask;
4380                         }
4381                 }
4382         }
4383         if (rec_selector_id == -1)
4384                 return mask;
4385         mc = &sc->sc_ctls[rec_selector_id];
4386         for (i = mc->minval; i <= mc->maxval; i++) {
4387                 if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES)
4388                         continue;
4389                 mask |= 1 << mc->slctrtype[i - 1];
4390         }
4391         return mask;
4392 }
4393
4394 void
4395 uaudio_mixer_set(device_t dev, unsigned type, unsigned left, unsigned right)
4396 {
4397         int i;
4398         struct uaudio_softc *sc;
4399         struct mixerctl *mc;
4400
4401         sc = device_get_softc(dev);
4402         for (i=0; i < sc->sc_nctls; i++) {
4403                 mc = &sc->sc_ctls[i];
4404                 if (mc->ctl == type) {
4405                         if (mc->nchan == 2) {
4406                                 /* set Right */
4407                                 uaudio_ctl_set(sc, SET_CUR, mc, 1, (int)(right*255)/100);
4408                         }
4409                         /* set Left or Mono */
4410                         uaudio_ctl_set(sc, SET_CUR, mc, 0, (int)(left*255)/100);
4411                 }
4412         }
4413         return;
4414 }
4415
4416 u_int32_t
4417 uaudio_mixer_setrecsrc(device_t dev, u_int32_t src)
4418 {
4419         int i, rec_selector_id;
4420         struct uaudio_softc *sc;
4421         struct mixerctl *mc;
4422
4423         sc = device_get_softc(dev);
4424         rec_selector_id = -1;
4425         for (i=0; i < sc->sc_nctls; i++) {
4426                 mc = &sc->sc_ctls[i];
4427                 if (mc->ctl == SOUND_MIXER_NRDEVICES && 
4428                     mc->type == MIX_SELECTOR && mc->class == UAC_RECORD) {
4429                         if (rec_selector_id == -1) {
4430                                 rec_selector_id = i;
4431                         } else {
4432                                 return src; /* Can't recognize which selector is record source selector */
4433                         }
4434                 }
4435         }
4436         if (rec_selector_id == -1)
4437                 return src;
4438         mc = &sc->sc_ctls[rec_selector_id];
4439         for (i = mc->minval; i <= mc->maxval; i++) {
4440                 if (src != (1 << mc->slctrtype[i - 1]))
4441                         continue;
4442                 uaudio_ctl_set(sc, SET_CUR, mc, 0, i);
4443                 return (1 << mc->slctrtype[i - 1]);
4444         }
4445         uaudio_ctl_set(sc, SET_CUR, mc, 0, mc->minval);
4446         return (1 << mc->slctrtype[mc->minval - 1]);
4447 }
4448
4449 static int
4450 uaudio_sndstat_prepare_pcm(struct sbuf *s, device_t dev, int verbose)
4451 {
4452         struct snddev_info *d;
4453         struct snddev_channel *sce;
4454         struct pcm_channel *c;
4455         struct pcm_feeder *f;
4456         int pc, rc, vc;
4457         device_t pa_dev = device_get_parent(dev);
4458         struct uaudio_softc *sc = device_get_softc(pa_dev);
4459
4460         if (verbose < 1)
4461                 return 0;
4462
4463         d = device_get_softc(dev);
4464         if (!d)
4465                 return ENXIO;
4466
4467         snd_mtxlock(d->lock);
4468         if (SLIST_EMPTY(&d->channels)) {
4469                 sbuf_printf(s, " (mixer only)");
4470                 snd_mtxunlock(d->lock);
4471                 return 0;
4472         }
4473         pc = rc = vc = 0;
4474         SLIST_FOREACH(sce, &d->channels, link) {
4475                 c = sce->channel;
4476                 if (c->direction == PCMDIR_PLAY) {
4477                         if (c->flags & CHN_F_VIRTUAL)
4478                                 vc++;
4479                         else
4480                                 pc++;
4481                 } else
4482                         rc++;
4483         }
4484         sbuf_printf(s, " (%dp/%dr/%dv channels%s%s)", 
4485                         d->playcount, d->reccount, d->vchancount,
4486                         (d->flags & SD_F_SIMPLEX)? "" : " duplex",
4487 #ifdef USING_DEVFS
4488                         (device_get_unit(dev) == snd_unit)? " default" : ""
4489 #else
4490                         ""
4491 #endif
4492                         );
4493
4494         if (sc->uaudio_sndstat_flag != 0) {
4495                 sbuf_cat(s, sbuf_data(&(sc->uaudio_sndstat)));
4496         }
4497
4498         if (verbose <= 1) {
4499                 snd_mtxunlock(d->lock);
4500                 return 0;
4501         }
4502
4503         SLIST_FOREACH(sce, &d->channels, link) {
4504                 c = sce->channel;
4505                 sbuf_printf(s, "\n\t");
4506
4507                 KASSERT(c->bufhard != NULL && c->bufsoft != NULL,
4508                         ("hosed pcm channel setup"));
4509
4510                 /* it would be better to indent child channels */
4511                 sbuf_printf(s, "%s[%s]: ", c->parentchannel? c->parentchannel->name : "", c->name);
4512                 sbuf_printf(s, "spd %d", c->speed);
4513                 if (c->speed != sndbuf_getspd(c->bufhard))
4514                         sbuf_printf(s, "/%d", sndbuf_getspd(c->bufhard));
4515                 sbuf_printf(s, ", fmt 0x%08x", c->format);
4516                 if (c->format != sndbuf_getfmt(c->bufhard))
4517                         sbuf_printf(s, "/0x%08x", sndbuf_getfmt(c->bufhard));
4518                 sbuf_printf(s, ", flags 0x%08x, 0x%08x", c->flags, c->feederflags);
4519                 if (c->pid != -1)
4520                         sbuf_printf(s, ", pid %d", c->pid);
4521                 sbuf_printf(s, "\n\t");
4522
4523                 sbuf_printf(s, "interrupts %d, ", c->interrupts);
4524                 if (c->direction == PCMDIR_REC)
4525                         sbuf_printf(s, "overruns %d, hfree %d, sfree %d",
4526                                 c->xruns, sndbuf_getfree(c->bufhard), sndbuf_getfree(c->bufsoft));
4527                 else
4528                         sbuf_printf(s, "underruns %d, ready %d",
4529                                 c->xruns, sndbuf_getready(c->bufsoft));
4530                 sbuf_printf(s, "\n\t");
4531
4532                 sbuf_printf(s, "{%s}", (c->direction == PCMDIR_REC)? "hardware" : "userland");
4533                 sbuf_printf(s, " -> ");
4534                 f = c->feeder;
4535                 while (f->source != NULL)
4536                         f = f->source;
4537                 while (f != NULL) {
4538                         sbuf_printf(s, "%s", f->class->name);
4539                         if (f->desc->type == FEEDER_FMT)
4540                                 sbuf_printf(s, "(0x%08x -> 0x%08x)", f->desc->in, f->desc->out);
4541                         if (f->desc->type == FEEDER_RATE)
4542                                 sbuf_printf(s, "(%d -> %d)", FEEDER_GET(f, FEEDRATE_SRC), FEEDER_GET(f, FEEDRATE_DST));
4543                         if (f->desc->type == FEEDER_ROOT || f->desc->type == FEEDER_MIXER)
4544                                 sbuf_printf(s, "(0x%08x)", f->desc->out);
4545                         sbuf_printf(s, " -> ");
4546                         f = f->parent;
4547                 }
4548                 sbuf_printf(s, "{%s}", (c->direction == PCMDIR_REC)? "userland" : "hardware");
4549         }
4550         snd_mtxunlock(d->lock);
4551
4552         return 0;
4553 }
4554
4555 void
4556 uaudio_sndstat_register(device_t dev)
4557 {
4558         struct snddev_info *d = device_get_softc(dev);
4559         sndstat_register(dev, d->status, uaudio_sndstat_prepare_pcm);
4560 }
4561         
4562 Static int
4563 audio_attach_mi(device_t dev)
4564 {
4565         device_t child;
4566         struct sndcard_func *func;
4567
4568         /* Attach the children. */
4569         /* PCM Audio */
4570         func = kmalloc(sizeof(struct sndcard_func), M_DEVBUF, M_NOWAIT);
4571         if (func == NULL)
4572                 return (ENOMEM);
4573         bzero(func, sizeof(*func));
4574         func->func = SCF_PCM;
4575         child = device_add_child(dev, "pcm", -1);
4576         device_set_ivars(child, func);
4577
4578         bus_generic_attach(dev);
4579
4580         return 0; /* XXXXX */
4581 }
4582
4583 DRIVER_MODULE(uaudio, uhub, uaudio_driver, uaudio_devclass, usbd_driver_load, 0);
4584 MODULE_VERSION(uaudio, 1);
4585
4586 #endif