usb4bsd: Use NULL for pointers.
[dragonfly.git] / sys / bus / u4b / audio / uaudio.c
CommitLineData
09b9c6f2
SW
1/* $NetBSD: uaudio.c,v 1.91 2004/11/05 17:46:14 kent Exp $ */
2/* $FreeBSD$ */
3
4/*-
5 * Copyright (c) 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Lennart Augustsson (lennart@augustsson.net) at
10 * Carlstedt Research & Technology.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf
36 * http://www.usb.org/developers/devclass_docs/frmts10.pdf
37 * http://www.usb.org/developers/devclass_docs/termt10.pdf
38 */
39
40/*
41 * Also merged:
42 * $NetBSD: uaudio.c,v 1.94 2005/01/15 15:19:53 kent Exp $
43 * $NetBSD: uaudio.c,v 1.95 2005/01/16 06:02:19 dsainty Exp $
44 * $NetBSD: uaudio.c,v 1.96 2005/01/16 12:46:00 kent Exp $
45 * $NetBSD: uaudio.c,v 1.97 2005/02/24 08:19:38 martin Exp $
46 */
47
48#include <sys/stdint.h>
49#include <sys/param.h>
50#include <sys/queue.h>
51#include <sys/types.h>
52#include <sys/systm.h>
53#include <sys/kernel.h>
54#include <sys/bus.h>
55#include <sys/module.h>
56#include <sys/lock.h>
57#include <sys/condvar.h>
58#include <sys/sysctl.h>
59#include <sys/unistd.h>
60#include <sys/callout.h>
61#include <sys/malloc.h>
62#include <sys/priv.h>
63
64#include <bus/u4b/usbdevs.h>
65#include <bus/u4b/usb.h>
66#include <bus/u4b/usbdi.h>
67#include <bus/u4b/usbdi_util.h>
68
69#define USB_DEBUG_VAR uaudio_debug
70#include <bus/u4b/usb_debug.h>
71
72#include <bus/u4b/quirk/usb_quirk.h>
73
74#include <sys/reboot.h> /* for bootverbose */
75
76#ifdef HAVE_KERNEL_OPTION_HEADERS
77#include "opt_snd.h"
78#endif
79
80#include <dev/sound/pcm/sound.h>
81#include <bus/u4b/audio/uaudioreg.h>
82#include <bus/u4b/audio/uaudio.h>
83#include <dev/sound/chip.h>
84#include "feeder_if.h"
85
86static int uaudio_default_rate = 0; /* use rate list */
87static int uaudio_default_bits = 32;
88static int uaudio_default_channels = 0; /* use default */
89
90#ifdef USB_DEBUG
91static int uaudio_debug = 0;
92
93static SYSCTL_NODE(_hw_usb, OID_AUTO, uaudio, CTLFLAG_RW, 0, "USB uaudio");
94
95SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, debug, CTLFLAG_RW,
96 &uaudio_debug, 0, "uaudio debug level");
97
98TUNABLE_INT("hw.usb.uaudio.default_rate", &uaudio_default_rate);
99SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_rate, CTLFLAG_RW,
100 &uaudio_default_rate, 0, "uaudio default sample rate");
101
102TUNABLE_INT("hw.usb.uaudio.default_bits", &uaudio_default_bits);
103SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_bits, CTLFLAG_RW,
104 &uaudio_default_bits, 0, "uaudio default sample bits");
105
106TUNABLE_INT("hw.usb.uaudio.default_channels", &uaudio_default_channels);
107SYSCTL_INT(_hw_usb_uaudio, OID_AUTO, default_channels, CTLFLAG_RW,
108 &uaudio_default_channels, 0, "uaudio default sample channels");
109#endif
110
111#define UAUDIO_NFRAMES 64 /* must be factor of 8 due HS-USB */
112#define UAUDIO_NCHANBUFS 2 /* number of outstanding request */
113#define UAUDIO_RECURSE_LIMIT 24 /* rounds */
114
115#define MAKE_WORD(h,l) (((h) << 8) | (l))
116#define BIT_TEST(bm,bno) (((bm)[(bno) / 8] >> (7 - ((bno) % 8))) & 1)
117#define UAUDIO_MAX_CHAN(x) (x)
118
119struct uaudio_mixer_node {
120 int32_t minval;
121 int32_t maxval;
122#define MIX_MAX_CHAN 8
123 int32_t wValue[MIX_MAX_CHAN]; /* using nchan */
124 uint32_t mul;
125 uint32_t ctl;
126
127 uint16_t wData[MIX_MAX_CHAN]; /* using nchan */
128 uint16_t wIndex;
129
130 uint8_t update[(MIX_MAX_CHAN + 7) / 8];
131 uint8_t nchan;
132 uint8_t type;
133#define MIX_ON_OFF 1
134#define MIX_SIGNED_16 2
135#define MIX_UNSIGNED_16 3
136#define MIX_SIGNED_8 4
137#define MIX_SELECTOR 5
138#define MIX_UNKNOWN 6
139#define MIX_SIZE(n) ((((n) == MIX_SIGNED_16) || \
140 ((n) == MIX_UNSIGNED_16)) ? 2 : 1)
141#define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
142
143#define MAX_SELECTOR_INPUT_PIN 256
144 uint8_t slctrtype[MAX_SELECTOR_INPUT_PIN];
145 uint8_t class;
146
147 struct uaudio_mixer_node *next;
148};
149
150struct uaudio_chan {
151 struct pcmchan_caps pcm_cap; /* capabilities */
152
153 struct snd_dbuf *pcm_buf;
154 const struct usb_config *usb_cfg;
155 struct lock *pcm_lock; /* lock protecting this structure */
156 struct uaudio_softc *priv_sc;
157 struct pcm_channel *pcm_ch;
158 struct usb_xfer *xfer[UAUDIO_NCHANBUFS];
159 const struct usb_audio_streaming_interface_descriptor *p_asid;
160 const struct usb_audio_streaming_type1_descriptor *p_asf1d;
161 const struct usb_audio_streaming_endpoint_descriptor *p_sed;
162 const usb_endpoint_descriptor_audio_t *p_ed1;
163 const usb_endpoint_descriptor_audio_t *p_ed2;
164 const struct uaudio_format *p_fmt;
165
166 uint8_t *buf; /* pointer to buffer */
167 uint8_t *start; /* upper layer buffer start */
168 uint8_t *end; /* upper layer buffer end */
169 uint8_t *cur; /* current position in upper layer
170 * buffer */
171
172 uint32_t intr_size; /* in bytes */
173 uint32_t intr_frames; /* in units */
174 uint32_t sample_rate;
175 uint32_t frames_per_second;
176 uint32_t sample_rem;
177 uint32_t sample_curr;
178
179 uint32_t format;
180 uint32_t pcm_format[2];
181
182 uint16_t bytes_per_frame[2];
183
184 uint16_t sample_size;
185
186 uint8_t valid;
187 uint8_t iface_index;
188 uint8_t iface_alt_index;
189};
190
191#define UMIDI_CABLES_MAX 16 /* units */
192#define UMIDI_TX_FRAMES 256 /* units */
193#define UMIDI_TX_BUFFER (UMIDI_TX_FRAMES * 4) /* bytes */
194
195enum {
196 UMIDI_TX_TRANSFER,
197 UMIDI_RX_TRANSFER,
198 UMIDI_N_TRANSFER,
199};
200
201struct umidi_sub_chan {
202 struct usb_fifo_sc fifo;
203 uint8_t *temp_cmd;
204 uint8_t temp_0[4];
205 uint8_t temp_1[4];
206 uint8_t state;
207#define UMIDI_ST_UNKNOWN 0 /* scan for command */
208#define UMIDI_ST_1PARAM 1
209#define UMIDI_ST_2PARAM_1 2
210#define UMIDI_ST_2PARAM_2 3
211#define UMIDI_ST_SYSEX_0 4
212#define UMIDI_ST_SYSEX_1 5
213#define UMIDI_ST_SYSEX_2 6
214
215 uint8_t read_open:1;
216 uint8_t write_open:1;
217 uint8_t unused:6;
218};
219
220struct umidi_chan {
221
222 struct umidi_sub_chan sub[UMIDI_CABLES_MAX];
223 struct lock lock;
224
225 struct usb_xfer *xfer[UMIDI_N_TRANSFER];
226
227 uint8_t iface_index;
228 uint8_t iface_alt_index;
229
230 uint8_t read_open_refcount;
231 uint8_t write_open_refcount;
232
233 uint8_t curr_cable;
234 uint8_t max_cable;
235 uint8_t valid;
236 uint8_t single_command;
237};
238
239struct uaudio_softc {
240 struct lock sc_lock;
241
242 struct sbuf sc_sndstat;
243 struct sndcard_func sc_sndcard_func;
244 struct uaudio_chan sc_rec_chan;
245 struct uaudio_chan sc_play_chan;
246 struct umidi_chan sc_midi_chan;
247
248 struct usb_device *sc_udev;
249 struct usb_xfer *sc_mixer_xfer[1];
250 struct uaudio_mixer_node *sc_mixer_root;
251 struct uaudio_mixer_node *sc_mixer_curr;
252
253 uint32_t sc_mix_info;
254 uint32_t sc_recsrc_info;
255
256 uint16_t sc_audio_rev;
257 uint16_t sc_mixer_count;
258
259 uint8_t sc_sndstat_valid;
260 uint8_t sc_mixer_iface_index;
261 uint8_t sc_mixer_iface_no;
262 uint8_t sc_mixer_chan;
263 uint8_t sc_pcm_registered:1;
264 uint8_t sc_mixer_init:1;
265 uint8_t sc_uq_audio_swap_lr:1;
266 uint8_t sc_uq_au_inp_async:1;
267 uint8_t sc_uq_au_no_xu:1;
268 uint8_t sc_uq_bad_adc:1;
269 uint8_t sc_uq_au_vendor_class:1;
270};
271
272struct uaudio_search_result {
273 uint8_t bit_input[(256 + 7) / 8];
274 uint8_t bit_output[(256 + 7) / 8];
275 uint8_t bit_visited[(256 + 7) / 8];
276 uint8_t recurse_level;
277 uint8_t id_max;
278};
279
280struct uaudio_terminal_node {
281 union {
282 const struct usb_descriptor *desc;
283 const struct usb_audio_input_terminal *it;
284 const struct usb_audio_output_terminal *ot;
285 const struct usb_audio_mixer_unit_0 *mu;
286 const struct usb_audio_selector_unit *su;
287 const struct usb_audio_feature_unit *fu;
288 const struct usb_audio_processing_unit_0 *pu;
289 const struct usb_audio_extension_unit_0 *eu;
290 } u;
291 struct uaudio_search_result usr;
292 struct uaudio_terminal_node *root;
293};
294
295struct uaudio_format {
296 uint16_t wFormat;
297 uint8_t bPrecision;
298 uint32_t freebsd_fmt;
299 const char *description;
300};
301
302static const struct uaudio_format uaudio_formats[] = {
303
304 {UA_FMT_PCM8, 8, AFMT_U8, "8-bit U-LE PCM"},
305 {UA_FMT_PCM8, 16, AFMT_U16_LE, "16-bit U-LE PCM"},
306 {UA_FMT_PCM8, 24, AFMT_U24_LE, "24-bit U-LE PCM"},
307 {UA_FMT_PCM8, 32, AFMT_U32_LE, "32-bit U-LE PCM"},
308
309 {UA_FMT_PCM, 8, AFMT_S8, "8-bit S-LE PCM"},
310 {UA_FMT_PCM, 16, AFMT_S16_LE, "16-bit S-LE PCM"},
311 {UA_FMT_PCM, 24, AFMT_S24_LE, "24-bit S-LE PCM"},
312 {UA_FMT_PCM, 32, AFMT_S32_LE, "32-bit S-LE PCM"},
313
314 {UA_FMT_ALAW, 8, AFMT_A_LAW, "8-bit A-Law"},
315 {UA_FMT_MULAW, 8, AFMT_MU_LAW, "8-bit mu-Law"},
316
317 {0, 0, 0, NULL}
318};
319
320#define UAC_OUTPUT 0
321#define UAC_INPUT 1
322#define UAC_EQUAL 2
323#define UAC_RECORD 3
324#define UAC_NCLASSES 4
325
326#ifdef USB_DEBUG
327static const char *uac_names[] = {
328 "outputs", "inputs", "equalization", "record"
329};
330
331#endif
332
333/* prototypes */
334
335static device_probe_t uaudio_probe;
336static device_attach_t uaudio_attach;
337static device_detach_t uaudio_detach;
338
339static usb_callback_t uaudio_chan_play_callback;
340static usb_callback_t uaudio_chan_record_callback;
341static usb_callback_t uaudio_mixer_write_cfg_callback;
342static usb_callback_t umidi_bulk_read_callback;
343static usb_callback_t umidi_bulk_write_callback;
344
345static void uaudio_chan_fill_info_sub(struct uaudio_softc *,
346 struct usb_device *, uint32_t, uint8_t, uint8_t);
347static void uaudio_chan_fill_info(struct uaudio_softc *,
348 struct usb_device *);
349static void uaudio_mixer_add_ctl_sub(struct uaudio_softc *,
350 struct uaudio_mixer_node *);
351static void uaudio_mixer_add_ctl(struct uaudio_softc *,
352 struct uaudio_mixer_node *);
353static void uaudio_mixer_add_input(struct uaudio_softc *,
354 const struct uaudio_terminal_node *, int);
355static void uaudio_mixer_add_output(struct uaudio_softc *,
356 const struct uaudio_terminal_node *, int);
357static void uaudio_mixer_add_mixer(struct uaudio_softc *,
358 const struct uaudio_terminal_node *, int);
359static void uaudio_mixer_add_selector(struct uaudio_softc *,
360 const struct uaudio_terminal_node *, int);
361static uint32_t uaudio_mixer_feature_get_bmaControls(
362 const struct usb_audio_feature_unit *, uint8_t);
363static void uaudio_mixer_add_feature(struct uaudio_softc *,
364 const struct uaudio_terminal_node *, int);
365static void uaudio_mixer_add_processing_updown(struct uaudio_softc *,
366 const struct uaudio_terminal_node *, int);
367static void uaudio_mixer_add_processing(struct uaudio_softc *,
368 const struct uaudio_terminal_node *, int);
369static void uaudio_mixer_add_extension(struct uaudio_softc *,
370 const struct uaudio_terminal_node *, int);
371static struct usb_audio_cluster uaudio_mixer_get_cluster(uint8_t,
372 const struct uaudio_terminal_node *);
373static uint16_t uaudio_mixer_determine_class(const struct uaudio_terminal_node *,
374 struct uaudio_mixer_node *);
375static uint16_t uaudio_mixer_feature_name(const struct uaudio_terminal_node *,
376 struct uaudio_mixer_node *);
377static const struct uaudio_terminal_node *uaudio_mixer_get_input(
378 const struct uaudio_terminal_node *, uint8_t);
379static const struct uaudio_terminal_node *uaudio_mixer_get_output(
380 const struct uaudio_terminal_node *, uint8_t);
381static void uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *,
382 const uint8_t *, uint8_t, struct uaudio_search_result *);
383static void uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *,
384 uint8_t, uint8_t, struct uaudio_search_result *);
385static void uaudio_mixer_fill_info(struct uaudio_softc *,
386 struct usb_device *, void *);
387static uint16_t uaudio_mixer_get(struct usb_device *, uint8_t,
388 struct uaudio_mixer_node *);
389static void uaudio_mixer_ctl_set(struct uaudio_softc *,
390 struct uaudio_mixer_node *, uint8_t, int32_t val);
391static usb_error_t uaudio_set_speed(struct usb_device *, uint8_t, uint32_t);
392static int uaudio_mixer_signext(uint8_t, int);
393static int uaudio_mixer_bsd2value(struct uaudio_mixer_node *, int32_t val);
394static const void *uaudio_mixer_verify_desc(const void *, uint32_t);
395static void uaudio_mixer_init(struct uaudio_softc *);
396static uint8_t umidi_convert_to_usb(struct umidi_sub_chan *, uint8_t, uint8_t);
397static struct umidi_sub_chan *umidi_sub_by_fifo(struct usb_fifo *);
398static void umidi_start_read(struct usb_fifo *);
399static void umidi_stop_read(struct usb_fifo *);
400static void umidi_start_write(struct usb_fifo *);
401static void umidi_stop_write(struct usb_fifo *);
402static int umidi_open(struct usb_fifo *, int);
403static int umidi_ioctl(struct usb_fifo *, u_long cmd, void *, int);
404static void umidi_close(struct usb_fifo *, int);
405static void umidi_init(device_t dev);
406static int umidi_probe(device_t dev);
407static int umidi_detach(device_t dev);
408
409#ifdef USB_DEBUG
410static void uaudio_chan_dump_ep_desc(
411 const usb_endpoint_descriptor_audio_t *);
412static void uaudio_mixer_dump_cluster(uint8_t,
413 const struct uaudio_terminal_node *);
414static const char *uaudio_mixer_get_terminal_name(uint16_t);
415#endif
416
417static const struct usb_config
418 uaudio_cfg_record[UAUDIO_NCHANBUFS] = {
419 [0] = {
420 .type = UE_ISOCHRONOUS,
421 .endpoint = UE_ADDR_ANY,
422 .direction = UE_DIR_IN,
423 .bufsize = 0, /* use "wMaxPacketSize * frames" */
424 .frames = UAUDIO_NFRAMES,
425 .flags = {.short_xfer_ok = 1,},
426 .callback = &uaudio_chan_record_callback,
427 },
428
429 [1] = {
430 .type = UE_ISOCHRONOUS,
431 .endpoint = UE_ADDR_ANY,
432 .direction = UE_DIR_IN,
433 .bufsize = 0, /* use "wMaxPacketSize * frames" */
434 .frames = UAUDIO_NFRAMES,
435 .flags = {.short_xfer_ok = 1,},
436 .callback = &uaudio_chan_record_callback,
437 },
438};
439
440static const struct usb_config
441 uaudio_cfg_play[UAUDIO_NCHANBUFS] = {
442 [0] = {
443 .type = UE_ISOCHRONOUS,
444 .endpoint = UE_ADDR_ANY,
445 .direction = UE_DIR_OUT,
446 .bufsize = 0, /* use "wMaxPacketSize * frames" */
447 .frames = UAUDIO_NFRAMES,
448 .flags = {.short_xfer_ok = 1,},
449 .callback = &uaudio_chan_play_callback,
450 },
451
452 [1] = {
453 .type = UE_ISOCHRONOUS,
454 .endpoint = UE_ADDR_ANY,
455 .direction = UE_DIR_OUT,
456 .bufsize = 0, /* use "wMaxPacketSize * frames" */
457 .frames = UAUDIO_NFRAMES,
458 .flags = {.short_xfer_ok = 1,},
459 .callback = &uaudio_chan_play_callback,
460 },
461};
462
463static const struct usb_config
464 uaudio_mixer_config[1] = {
465 [0] = {
466 .type = UE_CONTROL,
467 .endpoint = 0x00, /* Control pipe */
468 .direction = UE_DIR_ANY,
469 .bufsize = (sizeof(struct usb_device_request) + 4),
470 .callback = &uaudio_mixer_write_cfg_callback,
471 .timeout = 1000, /* 1 second */
472 },
473};
474
475static const
476uint8_t umidi_cmd_to_len[16] = {
477 [0x0] = 0, /* reserved */
478 [0x1] = 0, /* reserved */
479 [0x2] = 2, /* bytes */
480 [0x3] = 3, /* bytes */
481 [0x4] = 3, /* bytes */
482 [0x5] = 1, /* bytes */
483 [0x6] = 2, /* bytes */
484 [0x7] = 3, /* bytes */
485 [0x8] = 3, /* bytes */
486 [0x9] = 3, /* bytes */
487 [0xA] = 3, /* bytes */
488 [0xB] = 3, /* bytes */
489 [0xC] = 2, /* bytes */
490 [0xD] = 2, /* bytes */
491 [0xE] = 3, /* bytes */
492 [0xF] = 1, /* bytes */
493};
494
495static const struct usb_config
496 umidi_config[UMIDI_N_TRANSFER] = {
497 [UMIDI_TX_TRANSFER] = {
498 .type = UE_BULK,
499 .endpoint = UE_ADDR_ANY,
500 .direction = UE_DIR_OUT,
501 .bufsize = UMIDI_TX_BUFFER,
502 .callback = &umidi_bulk_write_callback,
503 },
504
505 [UMIDI_RX_TRANSFER] = {
506 .type = UE_BULK,
507 .endpoint = UE_ADDR_ANY,
508 .direction = UE_DIR_IN,
509 .bufsize = 4, /* bytes */
510 .flags = {.short_xfer_ok = 1,.proxy_buffer = 1,},
511 .callback = &umidi_bulk_read_callback,
512 },
513};
514
515static devclass_t uaudio_devclass;
516
517static device_method_t uaudio_methods[] = {
518 DEVMETHOD(device_probe, uaudio_probe),
519 DEVMETHOD(device_attach, uaudio_attach),
520 DEVMETHOD(device_detach, uaudio_detach),
521 DEVMETHOD(device_suspend, bus_generic_suspend),
522 DEVMETHOD(device_resume, bus_generic_resume),
523 DEVMETHOD(device_shutdown, bus_generic_shutdown),
524
525 {0, 0}
526};
527
528static driver_t uaudio_driver = {
529 .name = "uaudio",
530 .methods = uaudio_methods,
531 .size = sizeof(struct uaudio_softc),
532};
533
534static const STRUCT_USB_HOST_ID __used uaudio_devs[] = {
535 /* Generic USB audio class match */
536 {USB_IFACE_CLASS(UICLASS_AUDIO),
537 USB_IFACE_SUBCLASS(UISUBCLASS_AUDIOCONTROL),},
538 /* Generic USB MIDI class match */
539 {USB_IFACE_CLASS(UICLASS_AUDIO),
540 USB_IFACE_SUBCLASS(UISUBCLASS_MIDISTREAM),},
541};
542
543static int
544uaudio_probe(device_t dev)
545{
546 struct usb_attach_arg *uaa = device_get_ivars(dev);
547
548 if (uaa->usb_mode != USB_MODE_HOST)
549 return (ENXIO);
550
551 /* lookup non-standard device */
552
553 if (uaa->info.bInterfaceClass != UICLASS_AUDIO) {
554 if (usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS) == 0)
555 return (ENXIO);
556 }
557
558 /* check for AUDIO control interface */
559
560 if (uaa->info.bInterfaceSubClass == UISUBCLASS_AUDIOCONTROL) {
561 if (usb_test_quirk(uaa, UQ_BAD_AUDIO))
562 return (ENXIO);
563 else
564 return (BUS_PROBE_GENERIC);
565 }
566
567 /* check for MIDI stream */
568
569 if (uaa->info.bInterfaceSubClass == UISUBCLASS_MIDISTREAM) {
570 if (usb_test_quirk(uaa, UQ_BAD_MIDI))
571 return (ENXIO);
572 else
573 return (BUS_PROBE_GENERIC);
574 }
575 return (ENXIO);
576}
577
578static int
579uaudio_attach(device_t dev)
580{
581 struct usb_attach_arg *uaa = device_get_ivars(dev);
582 struct uaudio_softc *sc = device_get_softc(dev);
583 struct usb_interface_descriptor *id;
584 device_t child;
585
586 lockinit(&sc->sc_lock, "uaudio", 0, 0);
587
588 sc->sc_play_chan.priv_sc = sc;
589 sc->sc_rec_chan.priv_sc = sc;
590 sc->sc_udev = uaa->device;
591 sc->sc_mixer_iface_index = uaa->info.bIfaceIndex;
592 sc->sc_mixer_iface_no = uaa->info.bIfaceNum;
593
594 if (usb_test_quirk(uaa, UQ_AUDIO_SWAP_LR))
595 sc->sc_uq_audio_swap_lr = 1;
596
597 if (usb_test_quirk(uaa, UQ_AU_INP_ASYNC))
598 sc->sc_uq_au_inp_async = 1;
599
600 if (usb_test_quirk(uaa, UQ_AU_NO_XU))
601 sc->sc_uq_au_no_xu = 1;
602
603 if (usb_test_quirk(uaa, UQ_BAD_ADC))
604 sc->sc_uq_bad_adc = 1;
605
606 if (usb_test_quirk(uaa, UQ_AU_VENDOR_CLASS))
607 sc->sc_uq_au_vendor_class = 1;
608
609 umidi_init(dev);
610
611 device_set_usb_desc(dev);
612
613 id = usbd_get_interface_descriptor(uaa->iface);
614
615 uaudio_chan_fill_info(sc, uaa->device);
616
617 uaudio_mixer_fill_info(sc, uaa->device, id);
618
619 DPRINTF("audio rev %d.%02x\n",
620 sc->sc_audio_rev >> 8,
621 sc->sc_audio_rev & 0xff);
622
623 DPRINTF("%d mixer controls\n",
624 sc->sc_mixer_count);
625
626 if (sc->sc_play_chan.valid) {
627 device_printf(dev, "Play: %d Hz, %d ch, %s format\n",
628 sc->sc_play_chan.sample_rate,
629 sc->sc_play_chan.p_asf1d->bNrChannels,
630 sc->sc_play_chan.p_fmt->description);
631 } else {
632 device_printf(dev, "No playback!\n");
633 }
634
635 if (sc->sc_rec_chan.valid) {
636 device_printf(dev, "Record: %d Hz, %d ch, %s format\n",
637 sc->sc_rec_chan.sample_rate,
638 sc->sc_rec_chan.p_asf1d->bNrChannels,
639 sc->sc_rec_chan.p_fmt->description);
640 } else {
641 device_printf(dev, "No recording!\n");
642 }
643
644 if (sc->sc_midi_chan.valid) {
645
646 if (umidi_probe(dev)) {
647 goto detach;
648 }
649 device_printf(dev, "MIDI sequencer\n");
650 } else {
651 device_printf(dev, "No midi sequencer\n");
652 }
653
654 DPRINTF("doing child attach\n");
655
656 /* attach the children */
657
658 sc->sc_sndcard_func.func = SCF_PCM;
659
660 child = device_add_child(dev, "pcm", -1);
661
662 if (child == NULL) {
663 DPRINTF("out of memory\n");
664 goto detach;
665 }
666 device_set_ivars(child, &sc->sc_sndcard_func);
667
668 if (bus_generic_attach(dev)) {
669 DPRINTF("child attach failed\n");
670 goto detach;
671 }
672 return (0); /* success */
673
674detach:
675 uaudio_detach(dev);
676 return (ENXIO);
677}
678
679static void
680uaudio_pcm_setflags(device_t dev, uint32_t flags)
681{
682 pcm_setflags(dev, pcm_getflags(dev) | flags);
683}
684
685int
686uaudio_attach_sub(device_t dev, kobj_class_t mixer_class, kobj_class_t chan_class)
687{
688 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev));
689 char status[SND_STATUSLEN];
690
691 uaudio_mixer_init(sc);
692
693 if (sc->sc_uq_audio_swap_lr) {
694 DPRINTF("hardware has swapped left and right\n");
695 /* uaudio_pcm_setflags(dev, SD_F_PSWAPLR); */
696 }
697 if (!(sc->sc_mix_info & SOUND_MASK_PCM)) {
698
699 DPRINTF("emulating master volume\n");
700
701 /*
702 * Emulate missing pcm mixer controller
703 * through FEEDER_VOLUME
704 */
705 uaudio_pcm_setflags(dev, SD_F_SOFTPCMVOL);
706 }
707 if (mixer_init(dev, mixer_class, sc)) {
708 goto detach;
709 }
710 sc->sc_mixer_init = 1;
711
712 ksnprintf(status, sizeof(status), "at ? %s", PCM_KLDSTRING(snd_uaudio));
713
714 if (pcm_register(dev, sc,
715 sc->sc_play_chan.valid ? 1 : 0,
716 sc->sc_rec_chan.valid ? 1 : 0)) {
717 goto detach;
718 }
719
720#if 0 /* XXX */
721 uaudio_pcm_setflags(dev, SD_F_MPSAFE);
722#endif
723 uaudio_pcm_setflags(dev, 0);
724 sc->sc_pcm_registered = 1;
725
726 if (sc->sc_play_chan.valid) {
727 pcm_addchan(dev, PCMDIR_PLAY, chan_class, sc);
728 }
729 if (sc->sc_rec_chan.valid) {
730 pcm_addchan(dev, PCMDIR_REC, chan_class, sc);
731 }
732 pcm_setstatus(dev, status);
733
734 return (0); /* success */
735
736detach:
737 uaudio_detach_sub(dev);
738 return (ENXIO);
739}
740
741int
742uaudio_detach_sub(device_t dev)
743{
744 struct uaudio_softc *sc = device_get_softc(device_get_parent(dev));
745 int error = 0;
746
747repeat:
748 if (sc->sc_pcm_registered) {
749 error = pcm_unregister(dev);
750 } else {
751 if (sc->sc_mixer_init) {
752 error = mixer_uninit(dev);
753 }
754 }
755
756 if (error) {
757 device_printf(dev, "Waiting for sound application to exit!\n");
758 usb_pause_mtx(NULL, 2 * hz);
759 goto repeat; /* try again */
760 }
761 return (0); /* success */
762}
763
764static int
765uaudio_detach(device_t dev)
766{
767 struct uaudio_softc *sc = device_get_softc(dev);
768
769 /*
770 * Stop USB transfers early so that any audio applications
771 * will time out and close opened /dev/dspX.Y device(s), if
772 * any.
773 */
774 if (sc->sc_play_chan.valid)
775 usbd_transfer_unsetup(sc->sc_play_chan.xfer, UAUDIO_NCHANBUFS);
776 if (sc->sc_rec_chan.valid)
777 usbd_transfer_unsetup(sc->sc_rec_chan.xfer, UAUDIO_NCHANBUFS);
778
779 if (bus_generic_detach(dev) != 0) {
780 DPRINTF("detach failed!\n");
781 }
782 sbuf_delete(&sc->sc_sndstat);
783 sc->sc_sndstat_valid = 0;
784
785 umidi_detach(dev);
786 lockuninit(&sc->sc_lock);
787
788 return (0);
789}
790
791/*========================================================================*
792 * AS - Audio Stream - routines
793 *========================================================================*/
794
795#ifdef USB_DEBUG
796static void
797uaudio_chan_dump_ep_desc(const usb_endpoint_descriptor_audio_t *ed)
798{
799 if (ed) {
800 DPRINTF("endpoint=%p bLength=%d bDescriptorType=%d \n"
801 "bEndpointAddress=%d bmAttributes=0x%x \n"
802 "wMaxPacketSize=%d bInterval=%d \n"
803 "bRefresh=%d bSynchAddress=%d\n",
804 ed, ed->bLength, ed->bDescriptorType,
805 ed->bEndpointAddress, ed->bmAttributes,
806 UGETW(ed->wMaxPacketSize), ed->bInterval,
807 UEP_HAS_REFRESH(ed) ? ed->bRefresh : 0,
808 UEP_HAS_SYNCADDR(ed) ? ed->bSynchAddress : 0);
809 }
810}
811
812#endif
813
814/*
815 * The following is a workaround for broken no-name USB audio devices
816 * sold by dealextreme called "3D sound". The problem is that the
817 * manufacturer computed wMaxPacketSize is too small to hold the
818 * actual data sent. In other words the device sometimes sends more
819 * data than it actually reports it can send in a single isochronous
820 * packet.
821 */
822static void
823uaudio_record_fix_fs(usb_endpoint_descriptor_audio_t *ep,
824 uint32_t xps, uint32_t add)
825{
826 uint32_t mps;
827
828 mps = UGETW(ep->wMaxPacketSize);
829
830 /*
831 * If the device indicates it can send more data than what the
832 * sample rate indicates, we apply the workaround.
833 */
834 if (mps > xps) {
835
836 /* allow additional data */
837 xps += add;
838
839 /* check against the maximum USB 1.x length */
840 if (xps > 1023)
841 xps = 1023;
842
843 /* check if we should do an update */
844 if (mps < xps) {
845 /* simply update the wMaxPacketSize field */
846 USETW(ep->wMaxPacketSize, xps);
847 DPRINTF("Workaround: Updated wMaxPacketSize "
848 "from %d to %d bytes.\n",
849 (int)mps, (int)xps);
850 }
851 }
852}
853
854static void
855uaudio_chan_fill_info_sub(struct uaudio_softc *sc, struct usb_device *udev,
856 uint32_t rate, uint8_t channels, uint8_t bit_resolution)
857{
858 struct usb_descriptor *desc = NULL;
859 const struct usb_audio_streaming_interface_descriptor *asid = NULL;
860 const struct usb_audio_streaming_type1_descriptor *asf1d = NULL;
861 const struct usb_audio_streaming_endpoint_descriptor *sed = NULL;
862 usb_endpoint_descriptor_audio_t *ed1 = NULL;
863 const usb_endpoint_descriptor_audio_t *ed2 = NULL;
864 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
865 struct usb_interface_descriptor *id;
866 const struct uaudio_format *p_fmt;
867 struct uaudio_chan *chan;
868 uint16_t curidx = 0xFFFF;
869 uint16_t lastidx = 0xFFFF;
870 uint16_t alt_index = 0;
871 uint16_t wFormat;
872 uint8_t ep_dir;
873 uint8_t bChannels;
874 uint8_t bBitResolution;
875 uint8_t x;
876 uint8_t audio_if = 0;
877 uint8_t uma_if_class;
878
879 while ((desc = usb_desc_foreach(cd, desc))) {
880
881 if ((desc->bDescriptorType == UDESC_INTERFACE) &&
882 (desc->bLength >= sizeof(*id))) {
883
884 id = (void *)desc;
885
886 if (id->bInterfaceNumber != lastidx) {
887 lastidx = id->bInterfaceNumber;
888 curidx++;
889 alt_index = 0;
890
891 } else {
892 alt_index++;
893 }
894
895 uma_if_class =
896 ((id->bInterfaceClass == UICLASS_AUDIO) ||
897 ((id->bInterfaceClass == UICLASS_VENDOR) &&
898 (sc->sc_uq_au_vendor_class != 0)));
899
900 if ((uma_if_class != 0) && (id->bInterfaceSubClass == UISUBCLASS_AUDIOSTREAM)) {
901 audio_if = 1;
902 } else {
903 audio_if = 0;
904 }
905
906 if ((uma_if_class != 0) &&
907 (id->bInterfaceSubClass == UISUBCLASS_MIDISTREAM)) {
908
909 /*
910 * XXX could allow multiple MIDI interfaces
911 */
912
913 if ((sc->sc_midi_chan.valid == 0) &&
914 usbd_get_iface(udev, curidx)) {
915 sc->sc_midi_chan.iface_index = curidx;
916 sc->sc_midi_chan.iface_alt_index = alt_index;
917 sc->sc_midi_chan.valid = 1;
918 }
919 }
920 asid = NULL;
921 asf1d = NULL;
922 ed1 = NULL;
923 ed2 = NULL;
924 sed = NULL;
925 }
926 if ((desc->bDescriptorType == UDESC_CS_INTERFACE) &&
927 (desc->bDescriptorSubtype == AS_GENERAL) &&
928 (desc->bLength >= sizeof(*asid))) {
929 if (asid == NULL) {
930 asid = (void *)desc;
931 }
932 }
933 if ((desc->bDescriptorType == UDESC_CS_INTERFACE) &&
934 (desc->bDescriptorSubtype == FORMAT_TYPE) &&
935 (desc->bLength >= sizeof(*asf1d))) {
936 if (asf1d == NULL) {
937 asf1d = (void *)desc;
938 if (asf1d->bFormatType != FORMAT_TYPE_I) {
939 DPRINTFN(11, "ignored bFormatType = %d\n",
940 asf1d->bFormatType);
941 asf1d = NULL;
942 continue;
943 }
944 if (asf1d->bLength < (sizeof(*asf1d) +
945 ((asf1d->bSamFreqType == 0) ? 6 :
946 (asf1d->bSamFreqType * 3)))) {
947 DPRINTFN(11, "'asf1d' descriptor is too short\n");
948 asf1d = NULL;
949 continue;
950 }
951 }
952 }
953 if ((desc->bDescriptorType == UDESC_ENDPOINT) &&
954 (desc->bLength >= UEP_MINSIZE)) {
955 if (ed1 == NULL) {
956 ed1 = (void *)desc;
957 if (UE_GET_XFERTYPE(ed1->bmAttributes) != UE_ISOCHRONOUS) {
958 ed1 = NULL;
959 }
960 }
961 }
962 if ((desc->bDescriptorType == UDESC_CS_ENDPOINT) &&
963 (desc->bDescriptorSubtype == AS_GENERAL) &&
964 (desc->bLength >= sizeof(*sed))) {
965 if (sed == NULL) {
966 sed = (void *)desc;
967 }
968 }
969 if (audio_if && asid && asf1d && ed1 && sed) {
970
971 ep_dir = UE_GET_DIR(ed1->bEndpointAddress);
972
973 /* We ignore sync endpoint information until further. */
974
975 wFormat = UGETW(asid->wFormatTag);
976 bChannels = UAUDIO_MAX_CHAN(asf1d->bNrChannels);
977 bBitResolution = asf1d->bBitResolution;
978
979 if (asf1d->bSamFreqType == 0) {
980 DPRINTFN(16, "Sample rate: %d-%dHz\n",
981 UA_SAMP_LO(asf1d), UA_SAMP_HI(asf1d));
982
983 if ((rate >= UA_SAMP_LO(asf1d)) &&
984 (rate <= UA_SAMP_HI(asf1d))) {
985 goto found_rate;
986 }
987 } else {
988
989 for (x = 0; x < asf1d->bSamFreqType; x++) {
990 DPRINTFN(16, "Sample rate = %dHz\n",
991 UA_GETSAMP(asf1d, x));
992
993 if (rate == UA_GETSAMP(asf1d, x)) {
994 goto found_rate;
995 }
996 }
997 }
998
999 audio_if = 0;
1000 continue;
1001
1002 found_rate:
1003
1004 for (p_fmt = uaudio_formats;
1005 p_fmt->wFormat;
1006 p_fmt++) {
1007 if ((p_fmt->wFormat == wFormat) &&
1008 (p_fmt->bPrecision == bBitResolution)) {
1009 goto found_format;
1010 }
1011 }
1012
1013 audio_if = 0;
1014 continue;
1015
1016 found_format:
1017
1018 if ((bChannels == channels) &&
1019 (bBitResolution == bit_resolution)) {
1020
1021 chan = (ep_dir == UE_DIR_IN) ?
1022 &sc->sc_rec_chan :
1023 &sc->sc_play_chan;
1024
1025 if ((chan->valid == 0) && usbd_get_iface(udev, curidx)) {
1026
1027 chan->valid = 1;
1028#ifdef USB_DEBUG
1029 uaudio_chan_dump_ep_desc(ed1);
1030 uaudio_chan_dump_ep_desc(ed2);
1031
1032 if (sed->bmAttributes & UA_SED_FREQ_CONTROL) {
1033 DPRINTFN(2, "FREQ_CONTROL\n");
1034 }
1035 if (sed->bmAttributes & UA_SED_PITCH_CONTROL) {
1036 DPRINTFN(2, "PITCH_CONTROL\n");
1037 }
1038#endif
1039 DPRINTF("Sample rate = %dHz, channels = %d, "
1040 "bits = %d, format = %s\n", rate, channels,
1041 bit_resolution, p_fmt->description);
1042
1043 chan->sample_rate = rate;
1044 chan->p_asid = asid;
1045 chan->p_asf1d = asf1d;
1046 chan->p_ed1 = ed1;
1047 chan->p_ed2 = ed2;
1048 chan->p_fmt = p_fmt;
1049 chan->p_sed = sed;
1050 chan->iface_index = curidx;
1051 chan->iface_alt_index = alt_index;
1052
1053 if (ep_dir == UE_DIR_IN)
1054 chan->usb_cfg =
1055 uaudio_cfg_record;
1056 else
1057 chan->usb_cfg =
1058 uaudio_cfg_play;
1059
1060 chan->sample_size = ((
1061 UAUDIO_MAX_CHAN(chan->p_asf1d->bNrChannels) *
1062 chan->p_asf1d->bBitResolution) / 8);
1063
1064 if (ep_dir == UE_DIR_IN &&
1065 usbd_get_speed(udev) == USB_SPEED_FULL) {
1066 uaudio_record_fix_fs(ed1,
1067 chan->sample_size * (rate / 1000),
1068 chan->sample_size * (rate / 4000));
1069 }
1070
1071 if (sc->sc_sndstat_valid) {
1072 sbuf_printf(&sc->sc_sndstat, "\n\t"
1073 "mode %d.%d:(%s) %dch, %d/%dbit, %s, %dHz",
1074 curidx, alt_index,
1075 (ep_dir == UE_DIR_IN) ? "input" : "output",
1076 asf1d->bNrChannels, asf1d->bBitResolution,
1077 asf1d->bSubFrameSize * 8,
1078 p_fmt->description, rate);
1079 }
1080 }
1081 }
1082 audio_if = 0;
1083 continue;
1084 }
1085 }
1086}
1087
1088/* This structure defines all the supported rates. */
1089
1090static const uint32_t uaudio_rate_list[] = {
1091 96000,
1092 88000,
1093 80000,
1094 72000,
1095 64000,
1096 56000,
1097 48000,
1098 44100,
1099 40000,
1100 32000,
1101 24000,
1102 22050,
1103 16000,
1104 11025,
1105 8000,
1106 0
1107};
1108
1109static void
1110uaudio_chan_fill_info(struct uaudio_softc *sc, struct usb_device *udev)
1111{
1112 uint32_t rate = uaudio_default_rate;
1113 uint8_t z;
1114 uint8_t bits = uaudio_default_bits;
1115 uint8_t y;
1116 uint8_t channels = uaudio_default_channels;
1117 uint8_t x;
1118
1119 bits -= (bits % 8);
1120 if ((bits == 0) || (bits > 32)) {
1121 /* set a valid value */
1122 bits = 32;
1123 }
1124 if (channels == 0) {
1125 switch (usbd_get_speed(udev)) {
1126 case USB_SPEED_LOW:
1127 case USB_SPEED_FULL:
1128 /*
1129 * Due to high bandwidth usage and problems
1130 * with HIGH-speed split transactions we
1131 * disable surround setups on FULL-speed USB
1132 * by default
1133 */
1134 channels = 2;
1135 break;
1136 default:
1137 channels = 16;
1138 break;
1139 }
1140 } else if (channels > 16) {
1141 channels = 16;
1142 }
1143 if (sbuf_new(&sc->sc_sndstat, NULL, 4096, SBUF_AUTOEXTEND)) {
1144 sc->sc_sndstat_valid = 1;
1145 }
1146 /* try to search for a valid config */
1147
1148 for (x = channels; x; x--) {
1149 for (y = bits; y; y -= 8) {
1150
1151 /* try user defined rate, if any */
1152 if (rate != 0)
1153 uaudio_chan_fill_info_sub(sc, udev, rate, x, y);
1154
1155 /* try find a matching rate, if any */
1156 for (z = 0; uaudio_rate_list[z]; z++) {
1157 uaudio_chan_fill_info_sub(sc, udev, uaudio_rate_list[z], x, y);
1158
1159 if (sc->sc_rec_chan.valid &&
1160 sc->sc_play_chan.valid) {
1161 goto done;
1162 }
1163 }
1164 }
1165 }
1166
1167done:
1168 if (sc->sc_sndstat_valid) {
1169 sbuf_finish(&sc->sc_sndstat);
1170 }
1171}
1172
1173static void
1174uaudio_chan_play_callback(struct usb_xfer *xfer, usb_error_t error)
1175{
1176 struct uaudio_chan *ch = usbd_xfer_softc(xfer);
1177 struct usb_page_cache *pc;
1178 uint32_t total;
1179 uint32_t blockcount;
1180 uint32_t n;
1181 uint32_t offset;
1182 int actlen;
1183 int sumlen;
1184
1185 usbd_xfer_status(xfer, &actlen, &sumlen, NULL, NULL);
1186
1187 if (ch->end == ch->start) {
1188 DPRINTF("no buffer!\n");
1189 return;
1190 }
1191
1192 switch (USB_GET_STATE(xfer)) {
1193 case USB_ST_TRANSFERRED:
1194tr_transferred:
1195 if (actlen < sumlen) {
1196 DPRINTF("short transfer, "
1197 "%d of %d bytes\n", actlen, sumlen);
1198 }
1199 chn_intr(ch->pcm_ch);
1200
1201 case USB_ST_SETUP:
1202 if (ch->bytes_per_frame[1] > usbd_xfer_max_framelen(xfer)) {
1203 DPRINTF("bytes per transfer, %d, "
1204 "exceeds maximum, %d!\n",
1205 ch->bytes_per_frame[1],
1206 usbd_xfer_max_framelen(xfer));
1207 break;
1208 }
1209
1210 blockcount = ch->intr_frames;
1211
1212 /* setup number of frames */
1213 usbd_xfer_set_frames(xfer, blockcount);
1214
1215 /* reset total length */
1216 total = 0;
1217
1218 /* setup frame lengths */
1219 for (n = 0; n != blockcount; n++) {
1220 ch->sample_curr += ch->sample_rem;
1221 if (ch->sample_curr >= ch->frames_per_second) {
1222 ch->sample_curr -= ch->frames_per_second;
1223 usbd_xfer_set_frame_len(xfer, n, ch->bytes_per_frame[1]);
1224 total += ch->bytes_per_frame[1];
1225 } else {
1226 usbd_xfer_set_frame_len(xfer, n, ch->bytes_per_frame[0]);
1227 total += ch->bytes_per_frame[0];
1228 }
1229 }
1230
1231 DPRINTFN(6, "transfer %d bytes\n", total);
1232
1233 offset = 0;
1234
1235 pc = usbd_xfer_get_frame(xfer, 0);
1236 while (total > 0) {
1237
1238 n = (ch->end - ch->cur);
1239 if (n > total) {
1240 n = total;
1241 }
1242 usbd_copy_in(pc, offset, ch->cur, n);
1243
1244 total -= n;
1245 ch->cur += n;
1246 offset += n;
1247
1248 if (ch->cur >= ch->end) {
1249 ch->cur = ch->start;
1250 }
1251 }
1252
1253 usbd_transfer_submit(xfer);
1254 break;
1255
1256 default: /* Error */
1257 if (error == USB_ERR_CANCELLED) {
1258 break;
1259 }
1260 goto tr_transferred;
1261 }
1262}
1263
1264static void
1265uaudio_chan_record_callback(struct usb_xfer *xfer, usb_error_t error)
1266{
1267 struct uaudio_chan *ch = usbd_xfer_softc(xfer);
1268 struct usb_page_cache *pc;
1269 uint32_t n;
1270 uint32_t m;
1271 uint32_t blockcount;
1272 uint32_t offset0;
1273 uint32_t offset1;
1274 uint32_t mfl;
1275 int len;
1276 int actlen;
1277 int nframes;
1278
1279 usbd_xfer_status(xfer, &actlen, NULL, NULL, &nframes);
1280 mfl = usbd_xfer_max_framelen(xfer);
1281
1282 if (ch->end == ch->start) {
1283 DPRINTF("no buffer!\n");
1284 return;
1285 }
1286
1287 switch (USB_GET_STATE(xfer)) {
1288 case USB_ST_TRANSFERRED:
1289
1290 DPRINTFN(6, "transferred %d bytes\n", actlen);
1291
1292 offset0 = 0;
1293 pc = usbd_xfer_get_frame(xfer, 0);
1294
1295 for (n = 0; n != nframes; n++) {
1296
1297 offset1 = offset0;
1298 len = usbd_xfer_frame_len(xfer, n);
1299
1300 while (len > 0) {
1301
1302 m = (ch->end - ch->cur);
1303
1304 if (m > len) {
1305 m = len;
1306 }
1307 usbd_copy_out(pc, offset1, ch->cur, m);
1308
1309 len -= m;
1310 offset1 += m;
1311 ch->cur += m;
1312
1313 if (ch->cur >= ch->end) {
1314 ch->cur = ch->start;
1315 }
1316 }
1317
1318 offset0 += mfl;
1319 }
1320
1321 chn_intr(ch->pcm_ch);
1322
1323 case USB_ST_SETUP:
1324tr_setup:
1325 blockcount = ch->intr_frames;
1326
1327 usbd_xfer_set_frames(xfer, blockcount);
1328 for (n = 0; n < blockcount; n++) {
1329 usbd_xfer_set_frame_len(xfer, n, mfl);
1330 }
1331
1332 usbd_transfer_submit(xfer);
1333 break;
1334
1335 default: /* Error */
1336 if (error == USB_ERR_CANCELLED) {
1337 break;
1338 }
1339 goto tr_setup;
1340 }
1341}
1342
1343void *
1344uaudio_chan_init(struct uaudio_softc *sc, struct snd_dbuf *b,
1345 struct pcm_channel *c, int dir)
1346{
1347 struct uaudio_chan *ch = ((dir == PCMDIR_PLAY) ?
1348 &sc->sc_play_chan : &sc->sc_rec_chan);
1349 uint32_t buf_size;
1350 uint32_t frames;
1351 uint32_t format;
1352 uint16_t fps;
1353 uint8_t endpoint;
1354 uint8_t blocks;
1355 uint8_t iface_index;
1356 uint8_t alt_index;
1357 uint8_t fps_shift;
1358 usb_error_t err;
1359
1360 fps = usbd_get_isoc_fps(sc->sc_udev);
1361
1362 if (fps < 8000) {
1363 /* FULL speed USB */
1364 frames = 8;
1365 } else {
1366 /* HIGH speed USB */
1367 frames = UAUDIO_NFRAMES;
1368 }
1369
1370 /* setup play/record format */
1371
1372 ch->pcm_cap.fmtlist = ch->pcm_format;
1373
1374 ch->pcm_format[0] = 0;
1375 ch->pcm_format[1] = 0;
1376
1377 ch->pcm_cap.minspeed = ch->sample_rate;
1378 ch->pcm_cap.maxspeed = ch->sample_rate;
1379
1380 /* setup mutex and PCM channel */
1381
1382 ch->pcm_ch = c;
1383 ch->pcm_lock = c->lock;
1384
1385 format = ch->p_fmt->freebsd_fmt;
1386
1387#ifdef XXX
1388 switch (ch->p_asf1d->bNrChannels) {
1389 case 2:
1390 /* stereo */
1391 format = SND_FORMAT(format, 2, 0);
1392 break;
1393 case 1:
1394 /* mono */
1395 format = SND_FORMAT(format, 1, 0);
1396 break;
1397 default:
1398 /* surround and more */
1399 format = feeder_matrix_default_format(
1400 SND_FORMAT(format, ch->p_asf1d->bNrChannels, 0));
1401 break;
1402 }
1403#endif
1404 ch->pcm_cap.fmtlist[0] = format;
1405 ch->pcm_cap.fmtlist[1] = 0;
1406
1407 /* check if format is not supported */
1408
1409 if (format == 0) {
1410 DPRINTF("The selected audio format is not supported\n");
1411 goto error;
1412 }
1413
1414 /* set alternate interface corresponding to the mode */
1415
1416 endpoint = ch->p_ed1->bEndpointAddress;
1417 iface_index = ch->iface_index;
1418 alt_index = ch->iface_alt_index;
1419
1420 DPRINTF("endpoint=0x%02x, speed=%d, iface=%d alt=%d\n",
1421 endpoint, ch->sample_rate, iface_index, alt_index);
1422
1423 err = usbd_set_alt_interface_index(sc->sc_udev, iface_index, alt_index);
1424 if (err) {
1425 DPRINTF("setting of alternate index failed: %s!\n",
1426 usbd_errstr(err));
1427 goto error;
1428 }
1429 usbd_set_parent_iface(sc->sc_udev, iface_index,
1430 sc->sc_mixer_iface_index);
1431
1432 /*
1433 * Only set the sample rate if the channel reports that it
1434 * supports the frequency control.
1435 */
1436 if (ch->p_sed->bmAttributes & UA_SED_FREQ_CONTROL) {
1437 if (uaudio_set_speed(sc->sc_udev, endpoint, ch->sample_rate)) {
1438 /*
1439 * If the endpoint is adaptive setting the speed may
1440 * fail.
1441 */
1442 DPRINTF("setting of sample rate failed! (continuing anyway)\n");
1443 }
1444 }
1445 if (usbd_transfer_setup(sc->sc_udev, &iface_index, ch->xfer,
1446 ch->usb_cfg, UAUDIO_NCHANBUFS, ch, ch->pcm_lock)) {
1447 DPRINTF("could not allocate USB transfers!\n");
1448 goto error;
1449 }
1450
1451 fps_shift = usbd_xfer_get_fps_shift(ch->xfer[0]);
1452
1453 /* down shift number of frames per second, if any */
1454 fps >>= fps_shift;
1455 frames >>= fps_shift;
1456
1457 /* bytes per frame should not be zero */
1458 ch->bytes_per_frame[0] = ((ch->sample_rate / fps) * ch->sample_size);
1459 ch->bytes_per_frame[1] = (((ch->sample_rate + fps - 1) / fps) * ch->sample_size);
1460
1461 /* setup data rate dithering, if any */
1462 ch->frames_per_second = fps;
1463 ch->sample_rem = ch->sample_rate % fps;
1464 ch->sample_curr = 0;
1465 ch->frames_per_second = fps;
1466
1467 /* compute required buffer size */
1468 buf_size = (ch->bytes_per_frame[1] * frames);
1469
1470 ch->intr_size = buf_size;
1471 ch->intr_frames = frames;
1472
1473 DPRINTF("fps=%d sample_rem=%d\n", fps, ch->sample_rem);
1474
1475 if (ch->intr_frames == 0) {
1476 DPRINTF("frame shift is too high!\n");
1477 goto error;
1478 }
1479
1480 /* setup double buffering */
1481 buf_size *= 2;
1482 blocks = 2;
1483
1484 ch->buf = kmalloc(buf_size, M_DEVBUF, M_WAITOK | M_ZERO);
1485 if (ch->buf == NULL)
1486 goto error;
1487 if (sndbuf_setup(b, ch->buf, buf_size) != 0)
1488 goto error;
1489 if (sndbuf_resize(b, blocks, ch->intr_size))
1490 goto error;
1491
1492 ch->start = ch->buf;
1493 ch->end = ch->buf + buf_size;
1494 ch->cur = ch->buf;
1495 ch->pcm_buf = b;
1496
1497 if (ch->pcm_lock == NULL) {
1498 DPRINTF("ERROR: PCM channels does not have a mutex!\n");
1499 goto error;
1500 }
1501
1502 return (ch);
1503
1504error:
1505 uaudio_chan_free(ch);
1506 return (NULL);
1507}
1508
1509int
1510uaudio_chan_free(struct uaudio_chan *ch)
1511{
1512 if (ch->buf != NULL) {
1513 kfree(ch->buf, M_DEVBUF);
1514 ch->buf = NULL;
1515 }
1516 usbd_transfer_unsetup(ch->xfer, UAUDIO_NCHANBUFS);
1517
1518 ch->valid = 0;
1519
1520 return (0);
1521}
1522
1523int
1524uaudio_chan_set_param_blocksize(struct uaudio_chan *ch, uint32_t blocksize)
1525{
1526 return (ch->intr_size);
1527}
1528
1529int
1530uaudio_chan_set_param_fragments(struct uaudio_chan *ch, uint32_t blocksize,
1531 uint32_t blockcount)
1532{
1533 return (1);
1534}
1535
1536int
1537uaudio_chan_set_param_speed(struct uaudio_chan *ch, uint32_t speed)
1538{
1539 if (speed != ch->sample_rate) {
1540 DPRINTF("rate conversion required\n");
1541 }
1542 return (ch->sample_rate);
1543}
1544
1545int
1546uaudio_chan_getptr(struct uaudio_chan *ch)
1547{
1548 return (ch->cur - ch->start);
1549}
1550
1551struct pcmchan_caps *
1552uaudio_chan_getcaps(struct uaudio_chan *ch)
1553{
1554 return (&ch->pcm_cap);
1555}
1556
1557#ifdef XXXDF
1558static struct pcmchan_matrix uaudio_chan_matrix_swap_2_0 = {
1559 .id = SND_CHN_MATRIX_DRV,
1560 .channels = 2,
1561 .ext = 0,
1562 .map = {
1563 /* Right */
1564 [0] = {
1565 .type = SND_CHN_T_FR,
1566 .members =
1567 SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FC |
1568 SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BR |
1569 SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SR
1570 },
1571 /* Left */
1572 [1] = {
1573 .type = SND_CHN_T_FL,
1574 .members =
1575 SND_CHN_T_MASK_FL | SND_CHN_T_MASK_FC |
1576 SND_CHN_T_MASK_LF | SND_CHN_T_MASK_BL |
1577 SND_CHN_T_MASK_BC | SND_CHN_T_MASK_SL
1578 },
1579 [2] = {
1580 .type = SND_CHN_T_MAX,
1581 .members = 0
1582 }
1583 },
1584 .mask = SND_CHN_T_MASK_FR | SND_CHN_T_MASK_FL,
1585 .offset = { 1, 0, -1, -1, -1, -1, -1, -1, -1,
1586 -1, -1, -1, -1, -1, -1, -1, -1, -1 }
1587};
1588
1589struct pcmchan_matrix *
1590uaudio_chan_getmatrix(struct uaudio_chan *ch, uint32_t format)
1591{
1592 struct uaudio_softc *sc;
1593
1594 sc = ch->priv_sc;
1595
1596 if (sc != NULL && sc->sc_uq_audio_swap_lr != 0 &&
1597 AFMT_CHANNEL(format) == 2)
1598 return (&uaudio_chan_matrix_swap_2_0);
1599
1600 return (feeder_matrix_format_map(format));
1601}
1602#endif
1603int
1604uaudio_chan_set_param_format(struct uaudio_chan *ch, uint32_t format)
1605{
1606 ch->format = format;
1607 return (0);
1608}
1609
1610int
1611uaudio_chan_start(struct uaudio_chan *ch)
1612{
1613 ch->cur = ch->start;
1614
1615#if (UAUDIO_NCHANBUFS != 2)
1616#error "please update code"
1617#endif
1618 if (ch->xfer[0]) {
1619 usbd_transfer_start(ch->xfer[0]);
1620 }
1621 if (ch->xfer[1]) {
1622 usbd_transfer_start(ch->xfer[1]);
1623 }
1624 return (0);
1625}
1626
1627int
1628uaudio_chan_stop(struct uaudio_chan *ch)
1629{
1630#if (UAUDIO_NCHANBUFS != 2)
1631#error "please update code"
1632#endif
1633 usbd_transfer_stop(ch->xfer[0]);
1634 usbd_transfer_stop(ch->xfer[1]);
1635 return (0);
1636}
1637
1638/*========================================================================*
1639 * AC - Audio Controller - routines
1640 *========================================================================*/
1641
1642static void
1643uaudio_mixer_add_ctl_sub(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
1644{
1645 struct uaudio_mixer_node *p_mc_new =
1646 kmalloc(sizeof(*p_mc_new), M_USBDEV, M_WAITOK);
1647
1648 if (p_mc_new != NULL) {
1649 memcpy(p_mc_new, mc, sizeof(*p_mc_new));
1650 p_mc_new->next = sc->sc_mixer_root;
1651 sc->sc_mixer_root = p_mc_new;
1652 sc->sc_mixer_count++;
1653 } else {
1654 DPRINTF("out of memory\n");
1655 }
1656}
1657
1658static void
1659uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct uaudio_mixer_node *mc)
1660{
1661 int32_t res;
1662
1663 if (mc->class < UAC_NCLASSES) {
1664 DPRINTF("adding %s.%d\n",
1665 uac_names[mc->class], mc->ctl);
1666 } else {
1667 DPRINTF("adding %d\n", mc->ctl);
1668 }
1669
1670 if (mc->type == MIX_ON_OFF) {
1671 mc->minval = 0;
1672 mc->maxval = 1;
1673 } else if (mc->type == MIX_SELECTOR) {
1674 } else {
1675
1676 /* determine min and max values */
1677
1678 mc->minval = uaudio_mixer_get(sc->sc_udev, GET_MIN, mc);
1679
1680 mc->minval = uaudio_mixer_signext(mc->type, mc->minval);
1681
1682 mc->maxval = uaudio_mixer_get(sc->sc_udev, GET_MAX, mc);
1683
1684 mc->maxval = uaudio_mixer_signext(mc->type, mc->maxval);
1685
1686 /* check if max and min was swapped */
1687
1688 if (mc->maxval < mc->minval) {
1689 res = mc->maxval;
1690 mc->maxval = mc->minval;
1691 mc->minval = res;
1692 }
1693
1694 /* compute value range */
1695 mc->mul = mc->maxval - mc->minval;
1696 if (mc->mul == 0)
1697 mc->mul = 1;
1698
1699 /* compute value alignment */
1700 res = uaudio_mixer_get(sc->sc_udev, GET_RES, mc);
1701
1702 DPRINTF("Resolution = %d\n", (int)res);
1703 }
1704
1705 uaudio_mixer_add_ctl_sub(sc, mc);
1706
1707#ifdef USB_DEBUG
1708 if (uaudio_debug > 2) {
1709 uint8_t i;
1710
1711 for (i = 0; i < mc->nchan; i++) {
1712 DPRINTF("[mix] wValue=%04x\n", mc->wValue[0]);
1713 }
1714 DPRINTF("[mix] wIndex=%04x type=%d ctl='%d' "
1715 "min=%d max=%d\n",
1716 mc->wIndex, mc->type, mc->ctl,
1717 mc->minval, mc->maxval);
1718 }
1719#endif
1720}
1721
1722static void
1723uaudio_mixer_add_input(struct uaudio_softc *sc,
1724 const struct uaudio_terminal_node *iot, int id)
1725{
1726#ifdef USB_DEBUG
1727 const struct usb_audio_input_terminal *d = iot[id].u.it;
1728
1729 DPRINTFN(3, "bTerminalId=%d wTerminalType=0x%04x "
1730 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
1731 "iChannelNames=%d\n",
1732 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
1733 d->bNrChannels, UGETW(d->wChannelConfig),
1734 d->iChannelNames);
1735#endif
1736}
1737
1738static void
1739uaudio_mixer_add_output(struct uaudio_softc *sc,
1740 const struct uaudio_terminal_node *iot, int id)
1741{
1742#ifdef USB_DEBUG
1743 const struct usb_audio_output_terminal *d = iot[id].u.ot;
1744
1745 DPRINTFN(3, "bTerminalId=%d wTerminalType=0x%04x "
1746 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
1747 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
1748 d->bSourceId, d->iTerminal);
1749#endif
1750}
1751
1752static void
1753uaudio_mixer_add_mixer(struct uaudio_softc *sc,
1754 const struct uaudio_terminal_node *iot, int id)
1755{
1756 struct uaudio_mixer_node mix;
1757
1758 const struct usb_audio_mixer_unit_0 *d0 = iot[id].u.mu;
1759 const struct usb_audio_mixer_unit_1 *d1;
1760
1761 uint32_t bno; /* bit number */
1762 uint32_t p; /* bit number accumulator */
1763 uint32_t mo; /* matching outputs */
1764 uint32_t mc; /* matching channels */
1765 uint32_t ichs; /* input channels */
1766 uint32_t ochs; /* output channels */
1767 uint32_t c;
1768 uint32_t chs; /* channels */
1769 uint32_t i;
1770 uint32_t o;
1771
1772 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
1773 d0->bUnitId, d0->bNrInPins);
1774
1775 /* compute the number of input channels */
1776
1777 ichs = 0;
1778 for (i = 0; i < d0->bNrInPins; i++) {
1779 ichs += (uaudio_mixer_get_cluster(d0->baSourceId[i], iot)
1780 .bNrChannels);
1781 }
1782
1783 d1 = (const void *)(d0->baSourceId + d0->bNrInPins);
1784
1785 /* and the number of output channels */
1786
1787 ochs = d1->bNrChannels;
1788
1789 DPRINTFN(3, "ichs=%d ochs=%d\n", ichs, ochs);
1790
1791 memset(&mix, 0, sizeof(mix));
1792
1793 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
1794 uaudio_mixer_determine_class(&iot[id], &mix);
1795 mix.type = MIX_SIGNED_16;
1796
1797 if (uaudio_mixer_verify_desc(d0, ((ichs * ochs) + 7) / 8) == NULL) {
1798 return;
1799 }
1800 for (p = i = 0; i < d0->bNrInPins; i++) {
1801 chs = uaudio_mixer_get_cluster(d0->baSourceId[i], iot).bNrChannels;
1802 mc = 0;
1803 for (c = 0; c < chs; c++) {
1804 mo = 0;
1805 for (o = 0; o < ochs; o++) {
1806 bno = ((p + c) * ochs) + o;
1807 if (BIT_TEST(d1->bmControls, bno)) {
1808 mo++;
1809 }
1810 }
1811 if (mo == 1) {
1812 mc++;
1813 }
1814 }
1815 if ((mc == chs) && (chs <= MIX_MAX_CHAN)) {
1816
1817 /* repeat bit-scan */
1818
1819 mc = 0;
1820 for (c = 0; c < chs; c++) {
1821 for (o = 0; o < ochs; o++) {
1822 bno = ((p + c) * ochs) + o;
1823 if (BIT_TEST(d1->bmControls, bno)) {
1824 mix.wValue[mc++] = MAKE_WORD(p + c + 1, o + 1);
1825 }
1826 }
1827 }
1828 mix.nchan = chs;
1829 uaudio_mixer_add_ctl(sc, &mix);
1830 } else {
1831 /* XXX */
1832 }
1833 p += chs;
1834 }
1835}
1836
1837static void
1838uaudio_mixer_add_selector(struct uaudio_softc *sc,
1839 const struct uaudio_terminal_node *iot, int id)
1840{
1841 const struct usb_audio_selector_unit *d = iot[id].u.su;
1842 struct uaudio_mixer_node mix;
1843 uint16_t i;
1844
1845 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
1846 d->bUnitId, d->bNrInPins);
1847
1848 if (d->bNrInPins == 0) {
1849 return;
1850 }
1851 memset(&mix, 0, sizeof(mix));
1852
1853 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
1854 mix.wValue[0] = MAKE_WORD(0, 0);
1855 uaudio_mixer_determine_class(&iot[id], &mix);
1856 mix.nchan = 1;
1857 mix.type = MIX_SELECTOR;
1858
1859 mix.ctl = SOUND_MIXER_NRDEVICES;
1860 mix.minval = 1;
1861 mix.maxval = d->bNrInPins;
1862
1863 if (mix.maxval > MAX_SELECTOR_INPUT_PIN) {
1864 mix.maxval = MAX_SELECTOR_INPUT_PIN;
1865 }
1866 mix.mul = (mix.maxval - mix.minval);
1867 for (i = 0; i < MAX_SELECTOR_INPUT_PIN; i++) {
1868 mix.slctrtype[i] = SOUND_MIXER_NRDEVICES;
1869 }
1870
1871 for (i = 0; i < mix.maxval; i++) {
1872 mix.slctrtype[i] = uaudio_mixer_feature_name
1873 (&iot[d->baSourceId[i]], &mix);
1874 }
1875
1876 mix.class = 0; /* not used */
1877
1878 uaudio_mixer_add_ctl(sc, &mix);
1879}
1880
1881static uint32_t
1882uaudio_mixer_feature_get_bmaControls(const struct usb_audio_feature_unit *d,
1883 uint8_t index)
1884{
1885 uint32_t temp = 0;
1886 uint32_t offset = (index * d->bControlSize);
1887
1888 if (d->bControlSize > 0) {
1889 temp |= d->bmaControls[offset];
1890 if (d->bControlSize > 1) {
1891 temp |= d->bmaControls[offset + 1] << 8;
1892 if (d->bControlSize > 2) {
1893 temp |= d->bmaControls[offset + 2] << 16;
1894 if (d->bControlSize > 3) {
1895 temp |= d->bmaControls[offset + 3] << 24;
1896 }
1897 }
1898 }
1899 }
1900 return (temp);
1901}
1902
1903static void
1904uaudio_mixer_add_feature(struct uaudio_softc *sc,
1905 const struct uaudio_terminal_node *iot, int id)
1906{
1907 const struct usb_audio_feature_unit *d = iot[id].u.fu;
1908 struct uaudio_mixer_node mix;
1909 uint32_t fumask;
1910 uint32_t mmask;
1911 uint32_t cmask;
1912 uint16_t mixernumber;
1913 uint8_t nchan;
1914 uint8_t chan;
1915 uint8_t ctl;
1916 uint8_t i;
1917
1918 if (d->bControlSize == 0) {
1919 return;
1920 }
1921 memset(&mix, 0, sizeof(mix));
1922
1923 nchan = (d->bLength - 7) / d->bControlSize;
1924 mmask = uaudio_mixer_feature_get_bmaControls(d, 0);
1925 cmask = 0;
1926
1927 if (nchan == 0) {
1928 return;
1929 }
1930 /* figure out what we can control */
1931
1932 for (chan = 1; chan < nchan; chan++) {
1933 DPRINTFN(10, "chan=%d mask=%x\n",
1934 chan, uaudio_mixer_feature_get_bmaControls(d, chan));
1935
1936 cmask |= uaudio_mixer_feature_get_bmaControls(d, chan);
1937 }
1938
1939 if (nchan > MIX_MAX_CHAN) {
1940 nchan = MIX_MAX_CHAN;
1941 }
1942 mix.wIndex = MAKE_WORD(d->bUnitId, sc->sc_mixer_iface_no);
1943
1944 for (ctl = 1; ctl <= LOUDNESS_CONTROL; ctl++) {
1945
1946 fumask = FU_MASK(ctl);
1947
1948 DPRINTFN(5, "ctl=%d fumask=0x%04x\n",
1949 ctl, fumask);
1950
1951 if (mmask & fumask) {
1952 mix.nchan = 1;
1953 mix.wValue[0] = MAKE_WORD(ctl, 0);
1954 } else if (cmask & fumask) {
1955 mix.nchan = nchan - 1;
1956 for (i = 1; i < nchan; i++) {
1957 if (uaudio_mixer_feature_get_bmaControls(d, i) & fumask)
1958 mix.wValue[i - 1] = MAKE_WORD(ctl, i);
1959 else
1960 mix.wValue[i - 1] = -1;
1961 }
1962 } else {
1963 continue;
1964 }
1965
1966 mixernumber = uaudio_mixer_feature_name(&iot[id], &mix);
1967
1968 switch (ctl) {
1969 case MUTE_CONTROL:
1970 mix.type = MIX_ON_OFF;
1971 mix.ctl = SOUND_MIXER_NRDEVICES;
1972 break;
1973
1974 case VOLUME_CONTROL:
1975 mix.type = MIX_SIGNED_16;
1976 mix.ctl = mixernumber;
1977 break;
1978
1979 case BASS_CONTROL:
1980 mix.type = MIX_SIGNED_8;
1981 mix.ctl = SOUND_MIXER_BASS;
1982 break;
1983
1984 case MID_CONTROL:
1985 mix.type = MIX_SIGNED_8;
1986 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */
1987 break;
1988
1989 case TREBLE_CONTROL:
1990 mix.type = MIX_SIGNED_8;
1991 mix.ctl = SOUND_MIXER_TREBLE;
1992 break;
1993
1994 case GRAPHIC_EQUALIZER_CONTROL:
1995 continue; /* XXX don't add anything */
1996 break;
1997
1998 case AGC_CONTROL:
1999 mix.type = MIX_ON_OFF;
2000 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */
2001 break;
2002
2003 case DELAY_CONTROL:
2004 mix.type = MIX_UNSIGNED_16;
2005 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */
2006 break;
2007
2008 case BASS_BOOST_CONTROL:
2009 mix.type = MIX_ON_OFF;
2010 mix.ctl = SOUND_MIXER_NRDEVICES; /* XXXXX */
2011 break;
2012
2013 case LOUDNESS_CONTROL:
2014 mix.type = MIX_ON_OFF;
2015 mix.ctl = SOUND_MIXER_LOUD; /* Is this correct ? */
2016 break;
2017
2018 default:
2019 mix.type = MIX_UNKNOWN;
2020 break;
2021 }
2022
2023 if (mix.type != MIX_UNKNOWN) {
2024 uaudio_mixer_add_ctl(sc, &mix);
2025 }
2026 }
2027}
2028
2029static void
2030uaudio_mixer_add_processing_updown(struct uaudio_softc *sc,
2031 const struct uaudio_terminal_node *iot, int id)
2032{
2033 const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu;
2034 const struct usb_audio_processing_unit_1 *d1 =
2035 (const void *)(d0->baSourceId + d0->bNrInPins);
2036 const struct usb_audio_processing_unit_updown *ud =
2037 (const void *)(d1->bmControls + d1->bControlSize);
2038 struct uaudio_mixer_node mix;
2039 uint8_t i;
2040
2041 if (uaudio_mixer_verify_desc(d0, sizeof(*ud)) == NULL) {
2042 return;
2043 }
2044 if (uaudio_mixer_verify_desc(d0, sizeof(*ud) + (2 * ud->bNrModes))
2045 == NULL) {
2046 return;
2047 }
2048 DPRINTFN(3, "bUnitId=%d bNrModes=%d\n",
2049 d0->bUnitId, ud->bNrModes);
2050
2051 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
2052 DPRINTF("no mode select\n");
2053 return;
2054 }
2055 memset(&mix, 0, sizeof(mix));
2056
2057 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2058 mix.nchan = 1;
2059 mix.wValue[0] = MAKE_WORD(UD_MODE_SELECT_CONTROL, 0);
2060 uaudio_mixer_determine_class(&iot[id], &mix);
2061 mix.type = MIX_ON_OFF; /* XXX */
2062
2063 for (i = 0; i < ud->bNrModes; i++) {
2064 DPRINTFN(3, "i=%d bm=0x%x\n", i, UGETW(ud->waModes[i]));
2065 /* XXX */
2066 }
2067
2068 uaudio_mixer_add_ctl(sc, &mix);
2069}
2070
2071static void
2072uaudio_mixer_add_processing(struct uaudio_softc *sc,
2073 const struct uaudio_terminal_node *iot, int id)
2074{
2075 const struct usb_audio_processing_unit_0 *d0 = iot[id].u.pu;
2076 const struct usb_audio_processing_unit_1 *d1 =
2077 (const void *)(d0->baSourceId + d0->bNrInPins);
2078 struct uaudio_mixer_node mix;
2079 uint16_t ptype;
2080
2081 memset(&mix, 0, sizeof(mix));
2082
2083 ptype = UGETW(d0->wProcessType);
2084
2085 DPRINTFN(3, "wProcessType=%d bUnitId=%d "
2086 "bNrInPins=%d\n", ptype, d0->bUnitId, d0->bNrInPins);
2087
2088 if (d1->bControlSize == 0) {
2089 return;
2090 }
2091 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
2092 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2093 mix.nchan = 1;
2094 mix.wValue[0] = MAKE_WORD(XX_ENABLE_CONTROL, 0);
2095 uaudio_mixer_determine_class(&iot[id], &mix);
2096 mix.type = MIX_ON_OFF;
2097 uaudio_mixer_add_ctl(sc, &mix);
2098 }
2099 switch (ptype) {
2100 case UPDOWNMIX_PROCESS:
2101 uaudio_mixer_add_processing_updown(sc, iot, id);
2102 break;
2103
2104 case DOLBY_PROLOGIC_PROCESS:
2105 case P3D_STEREO_EXTENDER_PROCESS:
2106 case REVERBATION_PROCESS:
2107 case CHORUS_PROCESS:
2108 case DYN_RANGE_COMP_PROCESS:
2109 default:
2110 DPRINTF("unit %d, type=%d is not implemented\n",
2111 d0->bUnitId, ptype);
2112 break;
2113 }
2114}
2115
2116static void
2117uaudio_mixer_add_extension(struct uaudio_softc *sc,
2118 const struct uaudio_terminal_node *iot, int id)
2119{
2120 const struct usb_audio_extension_unit_0 *d0 = iot[id].u.eu;
2121 const struct usb_audio_extension_unit_1 *d1 =
2122 (const void *)(d0->baSourceId + d0->bNrInPins);
2123 struct uaudio_mixer_node mix;
2124
2125 DPRINTFN(3, "bUnitId=%d bNrInPins=%d\n",
2126 d0->bUnitId, d0->bNrInPins);
2127
2128 if (sc->sc_uq_au_no_xu) {
2129 return;
2130 }
2131 if (d1->bControlSize == 0) {
2132 return;
2133 }
2134 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
2135
2136 memset(&mix, 0, sizeof(mix));
2137
2138 mix.wIndex = MAKE_WORD(d0->bUnitId, sc->sc_mixer_iface_no);
2139 mix.nchan = 1;
2140 mix.wValue[0] = MAKE_WORD(UA_EXT_ENABLE, 0);
2141 uaudio_mixer_determine_class(&iot[id], &mix);
2142 mix.type = MIX_ON_OFF;
2143
2144 uaudio_mixer_add_ctl(sc, &mix);
2145 }
2146}
2147
2148static const void *
2149uaudio_mixer_verify_desc(const void *arg, uint32_t len)
2150{
2151 const struct usb_audio_mixer_unit_1 *d1;
2152 const struct usb_audio_extension_unit_1 *e1;
2153 const struct usb_audio_processing_unit_1 *u1;
2154
2155 union {
2156 const struct usb_descriptor *desc;
2157 const struct usb_audio_input_terminal *it;
2158 const struct usb_audio_output_terminal *ot;
2159 const struct usb_audio_mixer_unit_0 *mu;
2160 const struct usb_audio_selector_unit *su;
2161 const struct usb_audio_feature_unit *fu;
2162 const struct usb_audio_processing_unit_0 *pu;
2163 const struct usb_audio_extension_unit_0 *eu;
2164 } u;
2165
2166 u.desc = arg;
2167
2168 if (u.desc == NULL) {
2169 goto error;
2170 }
2171 if (u.desc->bDescriptorType != UDESC_CS_INTERFACE) {
2172 goto error;
2173 }
2174 switch (u.desc->bDescriptorSubtype) {
2175 case UDESCSUB_AC_INPUT:
2176 len += sizeof(*u.it);
2177 break;
2178
2179 case UDESCSUB_AC_OUTPUT:
2180 len += sizeof(*u.ot);
2181 break;
2182
2183 case UDESCSUB_AC_MIXER:
2184 len += sizeof(*u.mu);
2185
2186 if (u.desc->bLength < len) {
2187 goto error;
2188 }
2189 len += u.mu->bNrInPins;
2190
2191 if (u.desc->bLength < len) {
2192 goto error;
2193 }
2194 d1 = (const void *)(u.mu->baSourceId + u.mu->bNrInPins);
2195
2196 len += sizeof(*d1);
2197 break;
2198
2199 case UDESCSUB_AC_SELECTOR:
2200 len += sizeof(*u.su);
2201
2202 if (u.desc->bLength < len) {
2203 goto error;
2204 }
2205 len += u.su->bNrInPins;
2206 break;
2207
2208 case UDESCSUB_AC_FEATURE:
2209 len += (sizeof(*u.fu) + 1);
2210 break;
2211
2212 case UDESCSUB_AC_PROCESSING:
2213 len += sizeof(*u.pu);
2214
2215 if (u.desc->bLength < len) {
2216 goto error;
2217 }
2218 len += u.pu->bNrInPins;
2219
2220 if (u.desc->bLength < len) {
2221 goto error;
2222 }
2223 u1 = (const void *)(u.pu->baSourceId + u.pu->bNrInPins);
2224
2225 len += sizeof(*u1);
2226
2227 if (u.desc->bLength < len) {
2228 goto error;
2229 }
2230 len += u1->bControlSize;
2231
2232 break;
2233
2234 case UDESCSUB_AC_EXTENSION:
2235 len += sizeof(*u.eu);
2236
2237 if (u.desc->bLength < len) {
2238 goto error;
2239 }
2240 len += u.eu->bNrInPins;
2241
2242 if (u.desc->bLength < len) {
2243 goto error;
2244 }
2245 e1 = (const void *)(u.eu->baSourceId + u.eu->bNrInPins);
2246
2247 len += sizeof(*e1);
2248
2249 if (u.desc->bLength < len) {
2250 goto error;
2251 }
2252 len += e1->bControlSize;
2253 break;
2254
2255 default:
2256 goto error;
2257 }
2258
2259 if (u.desc->bLength < len) {
2260 goto error;
2261 }
2262 return (u.desc);
2263
2264error:
2265 if (u.desc) {
2266 DPRINTF("invalid descriptor, type=%d, "
2267 "sub_type=%d, len=%d of %d bytes\n",
2268 u.desc->bDescriptorType,
2269 u.desc->bDescriptorSubtype,
2270 u.desc->bLength, len);
2271 }
2272 return (NULL);
2273}
2274
2275#ifdef USB_DEBUG
2276static void
2277uaudio_mixer_dump_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
2278{
2279 static const char *channel_names[16] = {
2280 "LEFT", "RIGHT", "CENTER", "LFE",
2281 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
2282 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
2283 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
2284 };
2285 uint16_t cc;
2286 uint8_t i;
2287 const struct usb_audio_cluster cl = uaudio_mixer_get_cluster(id, iot);
2288
2289 cc = UGETW(cl.wChannelConfig);
2290
2291 DPRINTF("cluster: bNrChannels=%u iChannelNames=%u wChannelConfig="
2292 "0x%04x:\n", cl.iChannelNames, cl.bNrChannels, cc);
2293
2294 for (i = 0; cc; i++) {
2295 if (cc & 1) {
2296 DPRINTF(" - %s\n", channel_names[i]);
2297 }
2298 cc >>= 1;
2299 }
2300}
2301
2302#endif
2303
2304static struct usb_audio_cluster
2305uaudio_mixer_get_cluster(uint8_t id, const struct uaudio_terminal_node *iot)
2306{
2307 struct usb_audio_cluster r;
2308 const struct usb_descriptor *dp;
2309 uint8_t i;
2310
2311 for (i = 0; i < UAUDIO_RECURSE_LIMIT; i++) { /* avoid infinite loops */
2312 dp = iot[id].u.desc;
2313 if (dp == NULL) {
2314 goto error;
2315 }
2316 switch (dp->bDescriptorSubtype) {
2317 case UDESCSUB_AC_INPUT:
2318 r.bNrChannels = iot[id].u.it->bNrChannels;
2319 r.wChannelConfig[0] = iot[id].u.it->wChannelConfig[0];
2320 r.wChannelConfig[1] = iot[id].u.it->wChannelConfig[1];
2321 r.iChannelNames = iot[id].u.it->iChannelNames;
2322 goto done;
2323
2324 case UDESCSUB_AC_OUTPUT:
2325 id = iot[id].u.ot->bSourceId;
2326 break;
2327
2328 case UDESCSUB_AC_MIXER:
2329 r = *(const struct usb_audio_cluster *)
2330 &iot[id].u.mu->baSourceId[iot[id].u.mu->
2331 bNrInPins];
2332 goto done;
2333
2334 case UDESCSUB_AC_SELECTOR:
2335 if (iot[id].u.su->bNrInPins > 0) {
2336 /* XXX This is not really right */
2337 id = iot[id].u.su->baSourceId[0];
2338 }
2339 break;
2340
2341 case UDESCSUB_AC_FEATURE:
2342 id = iot[id].u.fu->bSourceId;
2343 break;
2344
2345 case UDESCSUB_AC_PROCESSING:
2346 r = *((const struct usb_audio_cluster *)
2347 &iot[id].u.pu->baSourceId[iot[id].u.pu->
2348 bNrInPins]);
2349 goto done;
2350
2351 case UDESCSUB_AC_EXTENSION:
2352 r = *((const struct usb_audio_cluster *)
2353 &iot[id].u.eu->baSourceId[iot[id].u.eu->
2354 bNrInPins]);
2355 goto done;
2356
2357 default:
2358 goto error;
2359 }
2360 }
2361error:
2362 DPRINTF("bad data\n");
2363 memset(&r, 0, sizeof(r));
2364done:
2365 return (r);
2366}
2367
2368#ifdef USB_DEBUG
2369
2370struct uaudio_tt_to_string {
2371 uint16_t terminal_type;
2372 const char *desc;
2373};
2374
2375static const struct uaudio_tt_to_string uaudio_tt_to_string[] = {
2376
2377 /* USB terminal types */
2378 {UAT_UNDEFINED, "UAT_UNDEFINED"},
2379 {UAT_STREAM, "UAT_STREAM"},
2380 {UAT_VENDOR, "UAT_VENDOR"},
2381
2382 /* input terminal types */
2383 {UATI_UNDEFINED, "UATI_UNDEFINED"},
2384 {UATI_MICROPHONE, "UATI_MICROPHONE"},
2385 {UATI_DESKMICROPHONE, "UATI_DESKMICROPHONE"},
2386 {UATI_PERSONALMICROPHONE, "UATI_PERSONALMICROPHONE"},
2387 {UATI_OMNIMICROPHONE, "UATI_OMNIMICROPHONE"},
2388 {UATI_MICROPHONEARRAY, "UATI_MICROPHONEARRAY"},
2389 {UATI_PROCMICROPHONEARR, "UATI_PROCMICROPHONEARR"},
2390
2391 /* output terminal types */
2392 {UATO_UNDEFINED, "UATO_UNDEFINED"},
2393 {UATO_SPEAKER, "UATO_SPEAKER"},
2394 {UATO_HEADPHONES, "UATO_HEADPHONES"},
2395 {UATO_DISPLAYAUDIO, "UATO_DISPLAYAUDIO"},
2396 {UATO_DESKTOPSPEAKER, "UATO_DESKTOPSPEAKER"},
2397 {UATO_ROOMSPEAKER, "UATO_ROOMSPEAKER"},
2398 {UATO_COMMSPEAKER, "UATO_COMMSPEAKER"},
2399 {UATO_SUBWOOFER, "UATO_SUBWOOFER"},
2400
2401 /* bidir terminal types */
2402 {UATB_UNDEFINED, "UATB_UNDEFINED"},
2403 {UATB_HANDSET, "UATB_HANDSET"},
2404 {UATB_HEADSET, "UATB_HEADSET"},
2405 {UATB_SPEAKERPHONE, "UATB_SPEAKERPHONE"},
2406 {UATB_SPEAKERPHONEESUP, "UATB_SPEAKERPHONEESUP"},
2407 {UATB_SPEAKERPHONEECANC, "UATB_SPEAKERPHONEECANC"},
2408
2409 /* telephony terminal types */
2410 {UATT_UNDEFINED, "UATT_UNDEFINED"},
2411 {UATT_PHONELINE, "UATT_PHONELINE"},
2412 {UATT_TELEPHONE, "UATT_TELEPHONE"},
2413 {UATT_DOWNLINEPHONE, "UATT_DOWNLINEPHONE"},
2414
2415 /* external terminal types */
2416 {UATE_UNDEFINED, "UATE_UNDEFINED"},
2417 {UATE_ANALOGCONN, "UATE_ANALOGCONN"},
2418 {UATE_LINECONN, "UATE_LINECONN"},
2419 {UATE_LEGACYCONN, "UATE_LEGACYCONN"},
2420 {UATE_DIGITALAUIFC, "UATE_DIGITALAUIFC"},
2421 {UATE_SPDIF, "UATE_SPDIF"},
2422 {UATE_1394DA, "UATE_1394DA"},
2423 {UATE_1394DV, "UATE_1394DV"},
2424
2425 /* embedded function terminal types */
2426 {UATF_UNDEFINED, "UATF_UNDEFINED"},
2427 {UATF_CALIBNOISE, "UATF_CALIBNOISE"},
2428 {UATF_EQUNOISE, "UATF_EQUNOISE"},
2429 {UATF_CDPLAYER, "UATF_CDPLAYER"},
2430 {UATF_DAT, "UATF_DAT"},
2431 {UATF_DCC, "UATF_DCC"},
2432 {UATF_MINIDISK, "UATF_MINIDISK"},
2433 {UATF_ANALOGTAPE, "UATF_ANALOGTAPE"},
2434 {UATF_PHONOGRAPH, "UATF_PHONOGRAPH"},
2435 {UATF_VCRAUDIO, "UATF_VCRAUDIO"},
2436 {UATF_VIDEODISCAUDIO, "UATF_VIDEODISCAUDIO"},
2437 {UATF_DVDAUDIO, "UATF_DVDAUDIO"},
2438 {UATF_TVTUNERAUDIO, "UATF_TVTUNERAUDIO"},
2439 {UATF_SATELLITE, "UATF_SATELLITE"},
2440 {UATF_CABLETUNER, "UATF_CABLETUNER"},
2441 {UATF_DSS, "UATF_DSS"},
2442 {UATF_RADIORECV, "UATF_RADIORECV"},
2443 {UATF_RADIOXMIT, "UATF_RADIOXMIT"},
2444 {UATF_MULTITRACK, "UATF_MULTITRACK"},
2445 {UATF_SYNTHESIZER, "UATF_SYNTHESIZER"},
2446
2447 /* unknown */
2448 {0x0000, "UNKNOWN"},
2449};
2450
2451static const char *
2452uaudio_mixer_get_terminal_name(uint16_t terminal_type)
2453{
2454 const struct uaudio_tt_to_string *uat = uaudio_tt_to_string;
2455
2456 while (uat->terminal_type) {
2457 if (uat->terminal_type == terminal_type) {
2458 break;
2459 }
2460 uat++;
2461 }
2462 if (uat->terminal_type == 0) {
2463 DPRINTF("unknown terminal type (0x%04x)", terminal_type);
2464 }
2465 return (uat->desc);
2466}
2467
2468#endif
2469
2470static uint16_t
2471uaudio_mixer_determine_class(const struct uaudio_terminal_node *iot,
2472 struct uaudio_mixer_node *mix)
2473{
2474 uint16_t terminal_type = 0x0000;
2475 const struct uaudio_terminal_node *input[2];
2476 const struct uaudio_terminal_node *output[2];
2477
2478 input[0] = uaudio_mixer_get_input(iot, 0);
2479 input[1] = uaudio_mixer_get_input(iot, 1);
2480
2481 output[0] = uaudio_mixer_get_output(iot, 0);
2482 output[1] = uaudio_mixer_get_output(iot, 1);
2483
2484 /*
2485 * check if there is only
2486 * one output terminal:
2487 */
2488 if (output[0] && (!output[1])) {
2489 terminal_type = UGETW(output[0]->u.ot->wTerminalType);
2490 }
2491 /*
2492 * If the only output terminal is USB,
2493 * the class is UAC_RECORD.
2494 */
2495 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
2496
2497 mix->class = UAC_RECORD;
2498 if (input[0] && (!input[1])) {
2499 terminal_type = UGETW(input[0]->u.it->wTerminalType);
2500 } else {
2501 terminal_type = 0;
2502 }
2503 goto done;
2504 }
2505 /*
2506 * if the unit is connected to just
2507 * one input terminal, the
2508 * class is UAC_INPUT:
2509 */
2510 if (input[0] && (!input[1])) {
2511 mix->class = UAC_INPUT;
2512 terminal_type = UGETW(input[0]->u.it->wTerminalType);
2513 goto done;
2514 }
2515 /*
2516 * Otherwise, the class is UAC_OUTPUT.
2517 */
2518 mix->class = UAC_OUTPUT;
2519done:
2520 return (terminal_type);
2521}
2522
2523struct uaudio_tt_to_feature {
2524 uint16_t terminal_type;
2525 uint16_t feature;
2526};
2527
2528static const struct uaudio_tt_to_feature uaudio_tt_to_feature[] = {
2529
2530 {UAT_STREAM, SOUND_MIXER_PCM},
2531
2532 {UATI_MICROPHONE, SOUND_MIXER_MIC},
2533 {UATI_DESKMICROPHONE, SOUND_MIXER_MIC},
2534 {UATI_PERSONALMICROPHONE, SOUND_MIXER_MIC},
2535 {UATI_OMNIMICROPHONE, SOUND_MIXER_MIC},
2536 {UATI_MICROPHONEARRAY, SOUND_MIXER_MIC},
2537 {UATI_PROCMICROPHONEARR, SOUND_MIXER_MIC},
2538
2539 {UATO_SPEAKER, SOUND_MIXER_SPEAKER},
2540 {UATO_DESKTOPSPEAKER, SOUND_MIXER_SPEAKER},
2541 {UATO_ROOMSPEAKER, SOUND_MIXER_SPEAKER},
2542 {UATO_COMMSPEAKER, SOUND_MIXER_SPEAKER},
2543
2544 {UATE_ANALOGCONN, SOUND_MIXER_LINE},
2545 {UATE_LINECONN, SOUND_MIXER_LINE},
2546 {UATE_LEGACYCONN, SOUND_MIXER_LINE},
2547
2548 {UATE_DIGITALAUIFC, SOUND_MIXER_ALTPCM},
2549 {UATE_SPDIF, SOUND_MIXER_ALTPCM},
2550 {UATE_1394DA, SOUND_MIXER_ALTPCM},
2551 {UATE_1394DV, SOUND_MIXER_ALTPCM},
2552
2553 {UATF_CDPLAYER, SOUND_MIXER_CD},
2554
2555 {UATF_SYNTHESIZER, SOUND_MIXER_SYNTH},
2556
2557 {UATF_VIDEODISCAUDIO, SOUND_MIXER_VIDEO},
2558 {UATF_DVDAUDIO, SOUND_MIXER_VIDEO},
2559 {UATF_TVTUNERAUDIO, SOUND_MIXER_VIDEO},
2560
2561 /* telephony terminal types */
2562 {UATT_UNDEFINED, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */
2563 {UATT_PHONELINE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */
2564 {UATT_TELEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */
2565 {UATT_DOWNLINEPHONE, SOUND_MIXER_PHONEIN}, /* SOUND_MIXER_PHONEOUT */
2566
2567 {UATF_RADIORECV, SOUND_MIXER_RADIO},
2568 {UATF_RADIOXMIT, SOUND_MIXER_RADIO},
2569
2570 {UAT_UNDEFINED, SOUND_MIXER_VOLUME},
2571 {UAT_VENDOR, SOUND_MIXER_VOLUME},
2572 {UATI_UNDEFINED, SOUND_MIXER_VOLUME},
2573
2574 /* output terminal types */
2575 {UATO_UNDEFINED, SOUND_MIXER_VOLUME},
2576 {UATO_DISPLAYAUDIO, SOUND_MIXER_VOLUME},
2577 {UATO_SUBWOOFER, SOUND_MIXER_VOLUME},
2578 {UATO_HEADPHONES, SOUND_MIXER_VOLUME},
2579
2580 /* bidir terminal types */
2581 {UATB_UNDEFINED, SOUND_MIXER_VOLUME},
2582 {UATB_HANDSET, SOUND_MIXER_VOLUME},
2583 {UATB_HEADSET, SOUND_MIXER_VOLUME},
2584 {UATB_SPEAKERPHONE, SOUND_MIXER_VOLUME},
2585 {UATB_SPEAKERPHONEESUP, SOUND_MIXER_VOLUME},
2586 {UATB_SPEAKERPHONEECANC, SOUND_MIXER_VOLUME},
2587
2588 /* external terminal types */
2589 {UATE_UNDEFINED, SOUND_MIXER_VOLUME},
2590
2591 /* embedded function terminal types */
2592 {UATF_UNDEFINED, SOUND_MIXER_VOLUME},
2593 {UATF_CALIBNOISE, SOUND_MIXER_VOLUME},
2594 {UATF_EQUNOISE, SOUND_MIXER_VOLUME},
2595 {UATF_DAT, SOUND_MIXER_VOLUME},
2596 {UATF_DCC, SOUND_MIXER_VOLUME},
2597 {UATF_MINIDISK, SOUND_MIXER_VOLUME},
2598 {UATF_ANALOGTAPE, SOUND_MIXER_VOLUME},
2599 {UATF_PHONOGRAPH, SOUND_MIXER_VOLUME},
2600 {UATF_VCRAUDIO, SOUND_MIXER_VOLUME},
2601 {UATF_SATELLITE, SOUND_MIXER_VOLUME},
2602 {UATF_CABLETUNER, SOUND_MIXER_VOLUME},
2603 {UATF_DSS, SOUND_MIXER_VOLUME},
2604 {UATF_MULTITRACK, SOUND_MIXER_VOLUME},
2605 {0xffff, SOUND_MIXER_VOLUME},
2606
2607 /* default */
2608 {0x0000, SOUND_MIXER_VOLUME},
2609};
2610
2611static uint16_t
2612uaudio_mixer_feature_name(const struct uaudio_terminal_node *iot,
2613 struct uaudio_mixer_node *mix)
2614{
2615 const struct uaudio_tt_to_feature *uat = uaudio_tt_to_feature;
2616 uint16_t terminal_type = uaudio_mixer_determine_class(iot, mix);
2617
2618 if ((mix->class == UAC_RECORD) && (terminal_type == 0)) {
2619 return (SOUND_MIXER_IMIX);
2620 }
2621 while (uat->terminal_type) {
2622 if (uat->terminal_type == terminal_type) {
2623 break;
2624 }
2625 uat++;
2626 }
2627
2628 DPRINTF("terminal_type=%s (0x%04x) -> %d\n",
2629 uaudio_mixer_get_terminal_name(terminal_type),
2630 terminal_type, uat->feature);
2631
2632 return (uat->feature);
2633}
2634
7ea90cc9 2635static const struct uaudio_terminal_node *
09b9c6f2
SW
2636uaudio_mixer_get_input(const struct uaudio_terminal_node *iot, uint8_t index)
2637{
2638 struct uaudio_terminal_node *root = iot->root;
2639 uint8_t n;
2640
2641 n = iot->usr.id_max;
2642 do {
2643 if (iot->usr.bit_input[n / 8] & (1 << (n % 8))) {
2644 if (!index--) {
2645 return (root + n);
2646 }
2647 }
2648 } while (n--);
2649
2650 return (NULL);
2651}
2652
7ea90cc9 2653static const struct uaudio_terminal_node *
09b9c6f2
SW
2654uaudio_mixer_get_output(const struct uaudio_terminal_node *iot, uint8_t index)
2655{
2656 struct uaudio_terminal_node *root = iot->root;
2657 uint8_t n;
2658
2659 n = iot->usr.id_max;
2660 do {
2661 if (iot->usr.bit_output[n / 8] & (1 << (n % 8))) {
2662 if (!index--) {
2663 return (root + n);
2664 }
2665 }
2666 } while (n--);
2667
2668 return (NULL);
2669}
2670
2671static void
2672uaudio_mixer_find_inputs_sub(struct uaudio_terminal_node *root,
2673 const uint8_t *p_id, uint8_t n_id,
2674 struct uaudio_search_result *info)
2675{
2676 struct uaudio_terminal_node *iot;
2677 uint8_t n;
2678 uint8_t i;
2679
2680 if (info->recurse_level >= UAUDIO_RECURSE_LIMIT) {
2681 return;
2682 }
2683 info->recurse_level++;
2684
2685 for (n = 0; n < n_id; n++) {
2686
2687 i = p_id[n];
2688
2689 if (info->bit_visited[i / 8] & (1 << (i % 8))) {
2690 /* don't go into a circle */
2691 DPRINTF("avoided going into a circle at id=%d!\n", i);
2692 continue;
2693 } else {
2694 info->bit_visited[i / 8] |= (1 << (i % 8));
2695 }
2696
2697 iot = (root + i);
2698
2699 if (iot->u.desc == NULL) {
2700 continue;
2701 }
2702 switch (iot->u.desc->bDescriptorSubtype) {
2703 case UDESCSUB_AC_INPUT:
2704 info->bit_input[i / 8] |= (1 << (i % 8));
2705 break;
2706
2707 case UDESCSUB_AC_FEATURE:
2708 uaudio_mixer_find_inputs_sub
2709 (root, &iot->u.fu->bSourceId, 1, info);
2710 break;
2711
2712 case UDESCSUB_AC_OUTPUT:
2713 uaudio_mixer_find_inputs_sub
2714 (root, &iot->u.ot->bSourceId, 1, info);
2715 break;
2716
2717 case UDESCSUB_AC_MIXER:
2718 uaudio_mixer_find_inputs_sub
2719 (root, iot->u.mu->baSourceId,
2720 iot->u.mu->bNrInPins, info);
2721 break;
2722
2723 case UDESCSUB_AC_SELECTOR:
2724 uaudio_mixer_find_inputs_sub
2725 (root, iot->u.su->baSourceId,
2726 iot->u.su->bNrInPins, info);
2727 break;
2728
2729 case UDESCSUB_AC_PROCESSING:
2730 uaudio_mixer_find_inputs_sub
2731 (root, iot->u.pu->baSourceId,
2732 iot->u.pu->bNrInPins, info);
2733 break;
2734
2735 case UDESCSUB_AC_EXTENSION:
2736 uaudio_mixer_find_inputs_sub
2737 (root, iot->u.eu->baSourceId,
2738 iot->u.eu->bNrInPins, info);
2739 break;
2740
2741 case UDESCSUB_AC_HEADER:
2742 default:
2743 break;
2744 }
2745 }
2746 info->recurse_level--;
2747}
2748
2749static void
2750uaudio_mixer_find_outputs_sub(struct uaudio_terminal_node *root, uint8_t id,
2751 uint8_t n_id, struct uaudio_search_result *info)
2752{
2753 struct uaudio_terminal_node *iot = (root + id);
2754 uint8_t j;
2755
2756 j = n_id;
2757 do {
2758 if ((j != id) && ((root + j)->u.desc) &&
2759 ((root + j)->u.desc->bDescriptorSubtype == UDESCSUB_AC_OUTPUT)) {
2760
2761 /*
2762 * "j" (output) <--- virtual wire <--- "id" (input)
2763 *
2764 * if "j" has "id" on the input, then "id" have "j" on
2765 * the output, because they are connected:
2766 */
2767 if ((root + j)->usr.bit_input[id / 8] & (1 << (id % 8))) {
2768 iot->usr.bit_output[j / 8] |= (1 << (j % 8));
2769 }
2770 }
2771 } while (j--);
2772}
2773
2774static void
2775uaudio_mixer_fill_info(struct uaudio_softc *sc, struct usb_device *udev,
2776 void *desc)
2777{
2778 const struct usb_audio_control_descriptor *acdp;
2779 struct usb_config_descriptor *cd = usbd_get_config_descriptor(udev);
2780 const struct usb_descriptor *dp;
2781 const struct usb_audio_unit *au;
2782 struct uaudio_terminal_node *iot = NULL;
2783 uint16_t wTotalLen;
2784 uint8_t ID_max = 0; /* inclusive */
2785 uint8_t i;
2786
2787 desc = usb_desc_foreach(cd, desc);
2788
2789 if (desc == NULL) {
2790 DPRINTF("no Audio Control header\n");
2791 goto done;
2792 }
2793 acdp = desc;
2794
2795 if ((acdp->bLength < sizeof(*acdp)) ||
2796 (acdp->bDescriptorType != UDESC_CS_INTERFACE) ||
2797 (acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)) {
2798 DPRINTF("invalid Audio Control header\n");
2799 goto done;
2800 }
2801 /* "wTotalLen" is allowed to be corrupt */
2802 wTotalLen = UGETW(acdp->wTotalLength) - acdp->bLength;
2803
2804 /* get USB audio revision */
2805 sc->sc_audio_rev = UGETW(acdp->bcdADC);
2806
2807 DPRINTFN(3, "found AC header, vers=%03x, len=%d\n",
2808 sc->sc_audio_rev, wTotalLen);
2809
2810 if (sc->sc_audio_rev != UAUDIO_VERSION) {
2811
2812 if (sc->sc_uq_bad_adc) {
2813
2814 } else {
2815 DPRINTF("invalid audio version\n");
2816 goto done;
2817 }
2818 }
2819 iot = kmalloc(sizeof(struct uaudio_terminal_node) * 256, M_TEMP,
2820 M_WAITOK | M_ZERO);
2821
2822 if (iot == NULL) {
2823 DPRINTF("no memory!\n");
2824 goto done;
2825 }
2826 while ((desc = usb_desc_foreach(cd, desc))) {
2827
2828 dp = desc;
2829
2830 if (dp->bLength > wTotalLen) {
2831 break;
2832 } else {
2833 wTotalLen -= dp->bLength;
2834 }
2835
2836 au = uaudio_mixer_verify_desc(dp, 0);
2837
2838 if (au) {
2839 iot[au->bUnitId].u.desc = (const void *)au;
2840 if (au->bUnitId > ID_max) {
2841 ID_max = au->bUnitId;
2842 }
2843 }
2844 }
2845
2846 DPRINTF("Maximum ID=%d\n", ID_max);
2847
2848 /*
2849 * determine sourcing inputs for
2850 * all nodes in the tree:
2851 */
2852 i = ID_max;
2853 do {
2854 uaudio_mixer_find_inputs_sub(iot, &i, 1, &((iot + i)->usr));
2855 } while (i--);
2856
2857 /*
2858 * determine outputs for
2859 * all nodes in the tree:
2860 */
2861 i = ID_max;
2862 do {
2863 uaudio_mixer_find_outputs_sub(iot, i, ID_max, &((iot + i)->usr));
2864 } while (i--);
2865
2866 /* set "id_max" and "root" */
2867
2868 i = ID_max;
2869 do {
2870 (iot + i)->usr.id_max = ID_max;
2871 (iot + i)->root = iot;
2872 } while (i--);
2873
2874#ifdef USB_DEBUG
2875 i = ID_max;
2876 do {
2877 uint8_t j;
2878
2879 if (iot[i].u.desc == NULL) {
2880 continue;
2881 }
2882 DPRINTF("id %d:\n", i);
2883
2884 switch (iot[i].u.desc->bDescriptorSubtype) {
2885 case UDESCSUB_AC_INPUT:
2886 DPRINTF(" - AC_INPUT type=%s\n",
2887 uaudio_mixer_get_terminal_name
2888 (UGETW(iot[i].u.it->wTerminalType)));
2889 uaudio_mixer_dump_cluster(i, iot);
2890 break;
2891
2892 case UDESCSUB_AC_OUTPUT:
2893 DPRINTF(" - AC_OUTPUT type=%s "
2894 "src=%d\n", uaudio_mixer_get_terminal_name
2895 (UGETW(iot[i].u.ot->wTerminalType)),
2896 iot[i].u.ot->bSourceId);
2897 break;
2898
2899 case UDESCSUB_AC_MIXER:
2900 DPRINTF(" - AC_MIXER src:\n");
2901 for (j = 0; j < iot[i].u.mu->bNrInPins; j++) {
2902 DPRINTF(" - %d\n", iot[i].u.mu->baSourceId[j]);
2903 }
2904 uaudio_mixer_dump_cluster(i, iot);
2905 break;
2906
2907 case UDESCSUB_AC_SELECTOR:
2908 DPRINTF(" - AC_SELECTOR src:\n");
2909 for (j = 0; j < iot[i].u.su->bNrInPins; j++) {
2910 DPRINTF(" - %d\n", iot[i].u.su->baSourceId[j]);
2911 }
2912 break;
2913
2914 case UDESCSUB_AC_FEATURE:
2915 DPRINTF(" - AC_FEATURE src=%d\n", iot[i].u.fu->bSourceId);
2916 break;
2917
2918 case UDESCSUB_AC_PROCESSING:
2919 DPRINTF(" - AC_PROCESSING src:\n");
2920 for (j = 0; j < iot[i].u.pu->bNrInPins; j++) {
2921 DPRINTF(" - %d\n", iot[i].u.pu->baSourceId[j]);
2922 }
2923 uaudio_mixer_dump_cluster(i, iot);
2924 break;
2925
2926 case UDESCSUB_AC_EXTENSION:
2927 DPRINTF(" - AC_EXTENSION src:\n");
2928 for (j = 0; j < iot[i].u.eu->bNrInPins; j++) {
2929 DPRINTF("%d ", iot[i].u.eu->baSourceId[j]);
2930 }
2931 uaudio_mixer_dump_cluster(i, iot);
2932 break;
2933
2934 default:
2935 DPRINTF("unknown audio control (subtype=%d)\n",
2936 iot[i].u.desc->bDescriptorSubtype);
2937 }
2938
2939 DPRINTF("Inputs to this ID are:\n");
2940
2941 j = ID_max;
2942 do {
2943 if (iot[i].usr.bit_input[j / 8] & (1 << (j % 8))) {
2944 DPRINTF(" -- ID=%d\n", j);
2945 }
2946 } while (j--);
2947
2948 DPRINTF("Outputs from this ID are:\n");
2949
2950 j = ID_max;
2951 do {
2952 if (iot[i].usr.bit_output[j / 8] & (1 << (j % 8))) {
2953 DPRINTF(" -- ID=%d\n", j);
2954 }
2955 } while (j--);
2956
2957 } while (i--);
2958#endif
2959
2960 /*
2961 * scan the config to create a linked
2962 * list of "mixer" nodes:
2963 */
2964
2965 i = ID_max;
2966 do {
2967 dp = iot[i].u.desc;
2968
2969 if (dp == NULL) {
2970 continue;
2971 }
2972 DPRINTFN(11, "id=%d subtype=%d\n",
2973 i, dp->bDescriptorSubtype);
2974
2975 switch (dp->bDescriptorSubtype) {
2976 case UDESCSUB_AC_HEADER:
2977 DPRINTF("unexpected AC header\n");
2978 break;
2979
2980 case UDESCSUB_AC_INPUT:
2981 uaudio_mixer_add_input(sc, iot, i);
2982 break;
2983
2984 case UDESCSUB_AC_OUTPUT:
2985 uaudio_mixer_add_output(sc, iot, i);
2986 break;
2987
2988 case UDESCSUB_AC_MIXER:
2989 uaudio_mixer_add_mixer(sc, iot, i);
2990 break;
2991
2992 case UDESCSUB_AC_SELECTOR:
2993 uaudio_mixer_add_selector(sc, iot, i);
2994 break;
2995
2996 case UDESCSUB_AC_FEATURE:
2997 uaudio_mixer_add_feature(sc, iot, i);
2998 break;
2999
3000 case UDESCSUB_AC_PROCESSING:
3001 uaudio_mixer_add_processing(sc, iot, i);
3002 break;
3003
3004 case UDESCSUB_AC_EXTENSION:
3005 uaudio_mixer_add_extension(sc, iot, i);
3006 break;
3007
3008 default:
3009 DPRINTF("bad AC desc subtype=0x%02x\n",
3010 dp->bDescriptorSubtype);
3011 break;
3012 }
3013
3014 } while (i--);
3015
3016done:
3017 if (iot) {
3018 kfree(iot, M_TEMP);
3019 }
3020}
3021
3022static uint16_t
3023uaudio_mixer_get(struct usb_device *udev, uint8_t what,
3024 struct uaudio_mixer_node *mc)
3025{
3026 struct usb_device_request req;
3027 uint16_t val;
3028 uint16_t len = MIX_SIZE(mc->type);
3029 uint8_t data[4];
3030 usb_error_t err;
3031
3032 if (mc->wValue[0] == -1) {
3033 return (0);
3034 }
3035 req.bmRequestType = UT_READ_CLASS_INTERFACE;
3036 req.bRequest = what;
3037 USETW(req.wValue, mc->wValue[0]);
3038 USETW(req.wIndex, mc->wIndex);
3039 USETW(req.wLength, len);
3040
3041 err = usbd_do_request(udev, NULL, &req, data);
3042 if (err) {
3043 DPRINTF("err=%s\n", usbd_errstr(err));
3044 return (0);
3045 }
3046 if (len < 1) {
3047 data[0] = 0;
3048 }
3049 if (len < 2) {
3050 data[1] = 0;
3051 }
3052 val = (data[0] | (data[1] << 8));
3053
3054 DPRINTFN(3, "val=%d\n", val);
3055
3056 return (val);
3057}
3058
3059static void
3060uaudio_mixer_write_cfg_callback(struct usb_xfer *xfer, usb_error_t error)
3061{
3062 struct usb_device_request req;
3063 struct uaudio_softc *sc = usbd_xfer_softc(xfer);
3064 struct uaudio_mixer_node *mc = sc->sc_mixer_curr;
3065 struct usb_page_cache *pc;
3066 uint16_t len;
3067 uint8_t repeat = 1;
3068 uint8_t update;
3069 uint8_t chan;
3070 uint8_t buf[2];
3071
3072 DPRINTF("\n");
3073
3074 switch (USB_GET_STATE(xfer)) {
3075 case USB_ST_TRANSFERRED:
3076tr_transferred:
3077 case USB_ST_SETUP:
3078tr_setup:
3079
3080 if (mc == NULL) {
3081 mc = sc->sc_mixer_root;
3082 sc->sc_mixer_curr = mc;
3083 sc->sc_mixer_chan = 0;
3084 repeat = 0;
3085 }
3086 while (mc) {
3087 while (sc->sc_mixer_chan < mc->nchan) {
3088
3089 len = MIX_SIZE(mc->type);
3090
3091 chan = sc->sc_mixer_chan;
3092
3093 sc->sc_mixer_chan++;
3094
3095 update = ((mc->update[chan / 8] & (1 << (chan % 8))) &&
3096 (mc->wValue[chan] != -1));
3097
3098 mc->update[chan / 8] &= ~(1 << (chan % 8));
3099
3100 if (update) {
3101
3102 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
3103 req.bRequest = SET_CUR;
3104 USETW(req.wValue, mc->wValue[chan]);
3105 USETW(req.wIndex, mc->wIndex);
3106 USETW(req.wLength, len);
3107
3108 if (len > 0) {
3109 buf[0] = (mc->wData[chan] & 0xFF);
3110 }
3111 if (len > 1) {
3112 buf[1] = (mc->wData[chan] >> 8) & 0xFF;
3113 }
3114 pc = usbd_xfer_get_frame(xfer, 0);
3115 usbd_copy_in(pc, 0, &req, sizeof(req));
3116 pc = usbd_xfer_get_frame(xfer, 1);
3117 usbd_copy_in(pc, 0, buf, len);
3118
3119 usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
3120 usbd_xfer_set_frame_len(xfer, 1, len);
3121 usbd_xfer_set_frames(xfer, len ? 2 : 1);
3122 usbd_transfer_submit(xfer);
3123 return;
3124 }
3125 }
3126
3127 mc = mc->next;
3128 sc->sc_mixer_curr = mc;
3129 sc->sc_mixer_chan = 0;
3130 }
3131
3132 if (repeat) {
3133 goto tr_setup;
3134 }
3135 break;
3136
3137 default: /* Error */
3138 DPRINTF("error=%s\n", usbd_errstr(error));
3139 if (error == USB_ERR_CANCELLED) {
3140 /* do nothing - we are detaching */
3141 break;
3142 }
3143 goto tr_transferred;
3144 }
3145}
3146
3147static usb_error_t
3148uaudio_set_speed(struct usb_device *udev, uint8_t endpt, uint32_t speed)
3149{
3150 struct usb_device_request req;
3151 uint8_t data[3];
3152
3153 DPRINTFN(6, "endpt=%d speed=%u\n", endpt, speed);
3154
3155 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
3156 req.bRequest = SET_CUR;
3157 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
3158 USETW(req.wIndex, endpt);
3159 USETW(req.wLength, 3);
3160 data[0] = speed;
3161 data[1] = speed >> 8;
3162 data[2] = speed >> 16;
3163
3164 return (usbd_do_request(udev, NULL, &req, data));
3165}
3166
3167static int
3168uaudio_mixer_signext(uint8_t type, int val)
3169{
3170 if (!MIX_UNSIGNED(type)) {
3171 if (MIX_SIZE(type) == 2) {
3172 val = (int16_t)val;
3173 } else {
3174 val = (int8_t)val;
3175 }
3176 }
3177 return (val);
3178}
3179
3180static int
3181uaudio_mixer_bsd2value(struct uaudio_mixer_node *mc, int32_t val)
3182{
3183 if (mc->type == MIX_ON_OFF) {
3184 val = (val != 0);
3185 } else if (mc->type == MIX_SELECTOR) {
3186 if ((val < mc->minval) ||
3187 (val > mc->maxval)) {
3188 val = mc->minval;
3189 }
3190 } else {
3191
3192 /* compute actual volume */
3193 val = (val * mc->mul) / 255;
3194
3195 /* add lower offset */
3196 val = val + mc->minval;
3197
3198 /* make sure we don't write a value out of range */
3199 if (val > mc->maxval)
3200 val = mc->maxval;
3201 else if (val < mc->minval)
3202 val = mc->minval;
3203 }
3204
3205 DPRINTFN(6, "type=0x%03x val=%d min=%d max=%d val=%d\n",
3206 mc->type, val, mc->minval, mc->maxval, val);
3207 return (val);
3208}
3209
3210static void
3211uaudio_mixer_ctl_set(struct uaudio_softc *sc, struct uaudio_mixer_node *mc,
3212 uint8_t chan, int32_t val)
3213{
3214 val = uaudio_mixer_bsd2value(mc, val);
3215
3216 mc->update[chan / 8] |= (1 << (chan % 8));
3217 mc->wData[chan] = val;
3218
3219 /* start the transfer, if not already started */
3220
3221 usbd_transfer_start(sc->sc_mixer_xfer[0]);
3222}
3223
3224static void
3225uaudio_mixer_init(struct uaudio_softc *sc)
3226{
3227 struct uaudio_mixer_node *mc;
3228 int32_t i;
3229
3230 for (mc = sc->sc_mixer_root; mc;
3231 mc = mc->next) {
3232
3233 if (mc->ctl != SOUND_MIXER_NRDEVICES) {
3234 /*
3235 * Set device mask bits. See
3236 * /usr/include/machine/soundcard.h
3237 */
3238 sc->sc_mix_info |= (1 << mc->ctl);
3239 }
3240 if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
3241 (mc->type == MIX_SELECTOR)) {
3242
3243 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
3244 if (mc->slctrtype[i - 1] == SOUND_MIXER_NRDEVICES) {
3245 continue;
3246 }
3247 sc->sc_recsrc_info |= 1 << mc->slctrtype[i - 1];
3248 }
3249 }
3250 }
3251}
3252
3253int
3254uaudio_mixer_init_sub(struct uaudio_softc *sc, struct snd_mixer *m)
3255{
3256 DPRINTF("\n");
3257
3258 if (usbd_transfer_setup(sc->sc_udev, &sc->sc_mixer_iface_index,
3259 sc->sc_mixer_xfer, uaudio_mixer_config, 1, sc,
3260 &sc->sc_lock)) {
3261 DPRINTFN(0, "could not allocate USB "
3262 "transfer for audio mixer!\n");
3263 return (ENOMEM);
3264 }
3265 if (!(sc->sc_mix_info & SOUND_MASK_VOLUME)) {
3266 mix_setparentchild(m, SOUND_MIXER_VOLUME, SOUND_MASK_PCM);
3267 mix_setrealdev(m, SOUND_MIXER_VOLUME, SOUND_MIXER_NONE);
3268 }
3269 mix_setdevs(m, sc->sc_mix_info);
3270 mix_setrecdevs(m, sc->sc_recsrc_info);
3271 return (0);
3272}
3273
3274int
3275uaudio_mixer_uninit_sub(struct uaudio_softc *sc)
3276{
3277 DPRINTF("\n");
3278
3279 usbd_transfer_unsetup(sc->sc_mixer_xfer, 1);
3280
3281 return (0);
3282}
3283
3284void
3285uaudio_mixer_set(struct uaudio_softc *sc, unsigned type,
3286 unsigned left, unsigned right)
3287{
3288 struct uaudio_mixer_node *mc;
3289
3290 for (mc = sc->sc_mixer_root; mc;
3291 mc = mc->next) {
3292
3293 if (mc->ctl == type) {
3294 if (mc->nchan == 2) {
3295 /* set Right */
3296 uaudio_mixer_ctl_set(sc, mc, 1, (int)(right * 255) / 100);
3297 }
3298 /* set Left or Mono */
3299 uaudio_mixer_ctl_set(sc, mc, 0, (int)(left * 255) / 100);
3300 }
3301 }
3302}
3303
3304uint32_t
3305uaudio_mixer_setrecsrc(struct uaudio_softc *sc, uint32_t src)
3306{
3307 struct uaudio_mixer_node *mc;
3308 uint32_t mask;
3309 uint32_t temp;
3310 int32_t i;
3311
3312 for (mc = sc->sc_mixer_root; mc;
3313 mc = mc->next) {
3314
3315 if ((mc->ctl == SOUND_MIXER_NRDEVICES) &&
3316 (mc->type == MIX_SELECTOR)) {
3317
3318 /* compute selector mask */
3319
3320 mask = 0;
3321 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
3322 mask |= (1 << mc->slctrtype[i - 1]);
3323 }
3324
3325 temp = mask & src;
3326 if (temp == 0) {
3327 continue;
3328 }
3329 /* find the first set bit */
3330 temp = (-temp) & temp;
3331
3332 /* update "src" */
3333 src &= ~mask;
3334 src |= temp;
3335
3336 for (i = mc->minval; (i > 0) && (i <= mc->maxval); i++) {
3337 if (temp != (1 << mc->slctrtype[i - 1])) {
3338 continue;
3339 }
3340 uaudio_mixer_ctl_set(sc, mc, 0, i);
3341 break;
3342 }
3343 }
3344 }
3345 return (src);
3346}
3347
3348/*========================================================================*
3349 * MIDI support routines
3350 *========================================================================*/
3351
3352static void
3353umidi_bulk_read_callback(struct usb_xfer *xfer, usb_error_t error)
3354{
3355 struct umidi_chan *chan = usbd_xfer_softc(xfer);
3356 struct umidi_sub_chan *sub;
3357 struct usb_page_cache *pc;
3358 uint8_t buf[4];
3359 uint8_t cmd_len;
3360 uint8_t cn;
3361 uint16_t pos;
3362 int actlen;
3363
3364 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
3365
3366 switch (USB_GET_STATE(xfer)) {
3367 case USB_ST_TRANSFERRED:
3368
3369 DPRINTF("actlen=%d bytes\n", actlen);
3370
3371 pos = 0;
3372 pc = usbd_xfer_get_frame(xfer, 0);
3373
3374 while (actlen >= 4) {
3375
3376 /* copy out the MIDI data */
3377 usbd_copy_out(pc, pos, buf, 4);
3378 /* command length */
3379 cmd_len = umidi_cmd_to_len[buf[0] & 0xF];
3380 /* cable number */
3381 cn = buf[0] >> 4;
3382 /*
3383 * Lookup sub-channel. The index is range
3384 * checked below.
3385 */
3386 sub = &chan->sub[cn];
3387
3388 if ((cmd_len != 0) &&
3389 (cn < chan->max_cable) &&
3390 (sub->read_open != 0)) {
3391
3392 /* Send data to the application */
3393 usb_fifo_put_data_linear(
3394 sub->fifo.fp[USB_FIFO_RX],
3395 buf + 1, cmd_len, 1);
3396 }
3397 actlen -= 4;
3398 pos += 4;
3399 }
3400
3401 case USB_ST_SETUP:
3402 DPRINTF("start\n");
3403tr_setup:
3404 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
3405 usbd_transfer_submit(xfer);
3406 break;
3407
3408 default:
3409 DPRINTF("error=%s\n", usbd_errstr(error));
3410
3411 if (error != USB_ERR_CANCELLED) {
3412 /* try to clear stall first */
3413 usbd_xfer_set_stall(xfer);
3414 goto tr_setup;
3415 }
3416 break;
3417 }
3418}
3419
3420/*
3421 * The following statemachine, that converts MIDI commands to
3422 * USB MIDI packets, derives from Linux's usbmidi.c, which
3423 * was written by "Clemens Ladisch":
3424 *
3425 * Returns:
3426 * 0: No command
3427 * Else: Command is complete
3428 */
3429static uint8_t
3430umidi_convert_to_usb(struct umidi_sub_chan *sub, uint8_t cn, uint8_t b)
3431{
3432 uint8_t p0 = (cn << 4);
3433
3434 if (b >= 0xf8) {
3435 sub->temp_0[0] = p0 | 0x0f;
3436 sub->temp_0[1] = b;
3437 sub->temp_0[2] = 0;
3438 sub->temp_0[3] = 0;
3439 sub->temp_cmd = sub->temp_0;
3440 return (1);
3441
3442 } else if (b >= 0xf0) {
3443 switch (b) {
3444 case 0xf0: /* system exclusive begin */
3445 sub->temp_1[1] = b;
3446 sub->state = UMIDI_ST_SYSEX_1;
3447 break;
3448 case 0xf1: /* MIDI time code */
3449 case 0xf3: /* song select */
3450 sub->temp_1[1] = b;
3451 sub->state = UMIDI_ST_1PARAM;
3452 break;
3453 case 0xf2: /* song position pointer */
3454 sub->temp_1[1] = b;
3455 sub->state = UMIDI_ST_2PARAM_1;
3456 break;
3457 case 0xf4: /* unknown */
3458 case 0xf5: /* unknown */
3459 sub->state = UMIDI_ST_UNKNOWN;
3460 break;
3461 case 0xf6: /* tune request */
3462 sub->temp_1[0] = p0 | 0x05;
3463 sub->temp_1[1] = 0xf6;
3464 sub->temp_1[2] = 0;
3465 sub->temp_1[3] = 0;
3466 sub->temp_cmd = sub->temp_1;
3467 sub->state = UMIDI_ST_UNKNOWN;
3468 return (1);
3469
3470 case 0xf7: /* system exclusive end */
3471 switch (sub->state) {
3472 case UMIDI_ST_SYSEX_0:
3473 sub->temp_1[0] = p0 | 0x05;
3474 sub->temp_1[1] = 0xf7;
3475 sub->temp_1[2] = 0;
3476 sub->temp_1[3] = 0;
3477 sub->temp_cmd = sub->temp_1;
3478 sub->state = UMIDI_ST_UNKNOWN;
3479 return (1);
3480 case UMIDI_ST_SYSEX_1:
3481 sub->temp_1[0] = p0 | 0x06;
3482 sub->temp_1[2] = 0xf7;
3483 sub->temp_1[3] = 0;
3484 sub->temp_cmd = sub->temp_1;
3485 sub->state = UMIDI_ST_UNKNOWN;
3486 return (1);
3487 case UMIDI_ST_SYSEX_2:
3488 sub->temp_1[0] = p0 | 0x07;
3489 sub->temp_1[3] = 0xf7;
3490 sub->temp_cmd = sub->temp_1;
3491 sub->state = UMIDI_ST_UNKNOWN;
3492 return (1);
3493 }
3494 sub->state = UMIDI_ST_UNKNOWN;
3495 break;
3496 }
3497 } else if (b >= 0x80) {
3498 sub->temp_1[1] = b;
3499 if ((b >= 0xc0) && (b <= 0xdf)) {
3500 sub->state = UMIDI_ST_1PARAM;
3501 } else {
3502 sub->state = UMIDI_ST_2PARAM_1;
3503 }
3504 } else { /* b < 0x80 */
3505 switch (sub->state) {
3506 case UMIDI_ST_1PARAM:
3507 if (sub->temp_1[1] < 0xf0) {
3508 p0 |= sub->temp_1[1] >> 4;
3509 } else {
3510 p0 |= 0x02;
3511 sub->state = UMIDI_ST_UNKNOWN;
3512 }
3513 sub->temp_1[0] = p0;
3514 sub->temp_1[2] = b;
3515 sub->temp_1[3] = 0;
3516 sub->temp_cmd = sub->temp_1;
3517 return (1);
3518 case UMIDI_ST_2PARAM_1:
3519 sub->temp_1[2] = b;
3520 sub->state = UMIDI_ST_2PARAM_2;
3521 break;
3522 case UMIDI_ST_2PARAM_2:
3523 if (sub->temp_1[1] < 0xf0) {
3524 p0 |= sub->temp_1[1] >> 4;
3525 sub->state = UMIDI_ST_2PARAM_1;
3526 } else {
3527 p0 |= 0x03;
3528 sub->state = UMIDI_ST_UNKNOWN;
3529 }
3530 sub->temp_1[0] = p0;
3531 sub->temp_1[3] = b;
3532 sub->temp_cmd = sub->temp_1;
3533 return (1);
3534 case UMIDI_ST_SYSEX_0:
3535 sub->temp_1[1] = b;
3536 sub->state = UMIDI_ST_SYSEX_1;
3537 break;
3538 case UMIDI_ST_SYSEX_1:
3539 sub->temp_1[2] = b;
3540 sub->state = UMIDI_ST_SYSEX_2;
3541 break;
3542 case UMIDI_ST_SYSEX_2:
3543 sub->temp_1[0] = p0 | 0x04;
3544 sub->temp_1[3] = b;
3545 sub->temp_cmd = sub->temp_1;
3546 sub->state = UMIDI_ST_SYSEX_0;
3547 return (1);
3548 default:
3549 break;
3550 }
3551 }
3552 return (0);
3553}
3554
3555static void
3556umidi_bulk_write_callback(struct usb_xfer *xfer, usb_error_t error)
3557{
3558 struct umidi_chan *chan = usbd_xfer_softc(xfer);
3559 struct umidi_sub_chan *sub;
3560 struct usb_page_cache *pc;
3561 uint32_t actlen;
3562 uint16_t nframes;
3563 uint8_t buf;
3564 uint8_t start_cable;
3565 uint8_t tr_any;
3566 int len;
3567
3568 usbd_xfer_status(xfer, &len, NULL, NULL, NULL);
3569
3570 /*
3571 * NOTE: Some MIDI devices only accept 4 bytes of data per
3572 * short terminated USB transfer.
3573 */
3574 switch (USB_GET_STATE(xfer)) {
3575 case USB_ST_TRANSFERRED:
3576 DPRINTF("actlen=%d bytes\n", len);
3577
3578 case USB_ST_SETUP:
3579tr_setup:
3580 DPRINTF("start\n");
3581
3582 nframes = 0; /* reset */
3583 start_cable = chan->curr_cable;
3584 tr_any = 0;
3585 pc = usbd_xfer_get_frame(xfer, 0);
3586
3587 while (1) {
3588
3589 /* round robin de-queueing */
3590
3591 sub = &chan->sub[chan->curr_cable];
3592
3593 if (sub->write_open) {
3594 usb_fifo_get_data_linear(sub->fifo.fp[USB_FIFO_TX],
3595 &buf, 1, &actlen, 0);
3596 } else {
3597 actlen = 0;
3598 }
3599
3600 if (actlen) {
3601
3602 tr_any = 1;
3603
3604 DPRINTF("byte=0x%02x from FIFO %u\n", buf,
3605 (unsigned int)chan->curr_cable);
3606
3607 if (umidi_convert_to_usb(sub, chan->curr_cable, buf)) {
3608
3609 DPRINTF("sub=0x%02x 0x%02x 0x%02x 0x%02x\n",
3610 sub->temp_cmd[0], sub->temp_cmd[1],
3611 sub->temp_cmd[2], sub->temp_cmd[3]);
3612
3613 usbd_copy_in(pc, nframes * 4, sub->temp_cmd, 4);
3614
3615 nframes++;
3616
3617 if ((nframes >= UMIDI_TX_FRAMES) || (chan->single_command != 0))
3618 break;
3619 } else {
3620 continue;
3621 }
3622 }
3623
3624 chan->curr_cable++;
3625 if (chan->curr_cable >= chan->max_cable)
3626 chan->curr_cable = 0;
3627
3628 if (chan->curr_cable == start_cable) {
3629 if (tr_any == 0)
3630 break;
3631 tr_any = 0;
3632 }
3633 }
3634
3635 if (nframes != 0) {
3636 DPRINTF("Transferring %d frames\n", (int)nframes);
3637 usbd_xfer_set_frame_len(xfer, 0, 4 * nframes);
3638 usbd_transfer_submit(xfer);
3639 }
3640 break;
3641
3642 default: /* Error */
3643
3644 DPRINTF("error=%s\n", usbd_errstr(error));
3645
3646 if (error != USB_ERR_CANCELLED) {
3647 /* try to clear stall first */
3648 usbd_xfer_set_stall(xfer);
3649 goto tr_setup;
3650 }
3651 break;
3652 }
3653}
3654
3655static struct umidi_sub_chan *
3656umidi_sub_by_fifo(struct usb_fifo *fifo)
3657{
3658 struct umidi_chan *chan = usb_fifo_softc(fifo);
3659 struct umidi_sub_chan *sub;
3660 uint32_t n;
3661
3662 for (n = 0; n < UMIDI_CABLES_MAX; n++) {
3663 sub = &chan->sub[n];
3664 if ((sub->fifo.fp[USB_FIFO_RX] == fifo) ||
3665 (sub->fifo.fp[USB_FIFO_TX] == fifo)) {
3666 return (sub);
3667 }
3668 }
3669
3670 panic("%s:%d cannot find usb_fifo!\n",
3671 __FILE__, __LINE__);
3672
3673 return (NULL);
3674}
3675
3676static void
3677umidi_start_read(struct usb_fifo *fifo)
3678{
3679 struct umidi_chan *chan = usb_fifo_softc(fifo);
3680
3681 usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
3682}
3683
3684static void
3685umidi_stop_read(struct usb_fifo *fifo)
3686{
3687 struct umidi_chan *chan = usb_fifo_softc(fifo);
3688 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
3689
3690 DPRINTF("\n");
3691
3692 sub->read_open = 0;
3693
3694 if (--(chan->read_open_refcount) == 0) {
3695 /*
3696 * XXX don't stop the read transfer here, hence that causes
3697 * problems with some MIDI adapters
3698 */
3699 DPRINTF("(stopping read transfer)\n");
3700 }
3701}
3702
3703static void
3704umidi_start_write(struct usb_fifo *fifo)
3705{
3706 struct umidi_chan *chan = usb_fifo_softc(fifo);
3707
3708 usbd_transfer_start(chan->xfer[UMIDI_TX_TRANSFER]);
3709}
3710
3711static void
3712umidi_stop_write(struct usb_fifo *fifo)
3713{
3714 struct umidi_chan *chan = usb_fifo_softc(fifo);
3715 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
3716
3717 DPRINTF("\n");
3718
3719 sub->write_open = 0;
3720
3721 if (--(chan->write_open_refcount) == 0) {
3722 DPRINTF("(stopping write transfer)\n");
3723 usbd_transfer_stop(chan->xfer[UMIDI_TX_TRANSFER]);
3724 }
3725}
3726
3727static int
3728umidi_open(struct usb_fifo *fifo, int fflags)
3729{
3730 struct umidi_chan *chan = usb_fifo_softc(fifo);
3731 struct umidi_sub_chan *sub = umidi_sub_by_fifo(fifo);
3732
3733 if (fflags & FREAD) {
3734 if (usb_fifo_alloc_buffer(fifo, 4, (1024 / 4))) {
3735 return (ENOMEM);
3736 }
3737 lockmgr(&chan->lock, LK_EXCLUSIVE);
3738 chan->read_open_refcount++;
3739 sub->read_open = 1;
3740 lockmgr(&chan->lock, LK_RELEASE);
3741 }
3742 if (fflags & FWRITE) {
3743 if (usb_fifo_alloc_buffer(fifo, 32, (1024 / 32))) {
3744 return (ENOMEM);
3745 }
3746 /* clear stall first */
3747 lockmgr(&chan->lock, LK_EXCLUSIVE);
3748 usbd_xfer_set_stall(chan->xfer[UMIDI_TX_TRANSFER]);
3749 chan->write_open_refcount++;
3750 sub->write_open = 1;
3751
3752 /* reset */
3753 sub->state = UMIDI_ST_UNKNOWN;
3754 lockmgr(&chan->lock, LK_RELEASE);
3755 }
3756 return (0); /* success */
3757}
3758
3759static void
3760umidi_close(struct usb_fifo *fifo, int fflags)
3761{
3762 if (fflags & FREAD) {
3763 usb_fifo_free_buffer(fifo);
3764 }
3765 if (fflags & FWRITE) {
3766 usb_fifo_free_buffer(fifo);
3767 }
3768}
3769
3770
3771static int
3772umidi_ioctl(struct usb_fifo *fifo, u_long cmd, void *data,
3773 int fflags)
3774{
3775 return (ENODEV);
3776}
3777
3778static void
3779umidi_init(device_t dev)
3780{
3781 struct uaudio_softc *sc = device_get_softc(dev);
3782 struct umidi_chan *chan = &sc->sc_midi_chan;
3783
3784 lockinit(&chan->lock, "umidi lock", 0, LK_CANRECURSE);
3785}
3786
3787static struct usb_fifo_methods umidi_fifo_methods = {
3788 .f_start_read = &umidi_start_read,
3789 .f_start_write = &umidi_start_write,
3790 .f_stop_read = &umidi_stop_read,
3791 .f_stop_write = &umidi_stop_write,
3792 .f_open = &umidi_open,
3793 .f_close = &umidi_close,
3794 .f_ioctl = &umidi_ioctl,
3795 .basename[0] = "umidi",
3796};
3797
3798static int
3799umidi_probe(device_t dev)
3800{
3801 struct uaudio_softc *sc = device_get_softc(dev);
3802 struct usb_attach_arg *uaa = device_get_ivars(dev);
3803 struct umidi_chan *chan = &sc->sc_midi_chan;
3804 struct umidi_sub_chan *sub;
3805 int unit = device_get_unit(dev);
3806 int error;
3807 uint32_t n;
3808
3809 if (usb_test_quirk(uaa, UQ_SINGLE_CMD_MIDI))
3810 chan->single_command = 1;
3811
3812 if (usbd_set_alt_interface_index(sc->sc_udev, chan->iface_index,
3813 chan->iface_alt_index)) {
3814 DPRINTF("setting of alternate index failed!\n");
3815 goto detach;
3816 }
3817 usbd_set_parent_iface(sc->sc_udev, chan->iface_index,
3818 sc->sc_mixer_iface_index);
3819
3820 error = usbd_transfer_setup(uaa->device, &chan->iface_index,
3821 chan->xfer, umidi_config, UMIDI_N_TRANSFER,
3822 chan, &chan->lock);
3823 if (error) {
3824 DPRINTF("error=%s\n", usbd_errstr(error));
3825 goto detach;
3826 }
3827 if ((chan->max_cable > UMIDI_CABLES_MAX) ||
3828 (chan->max_cable == 0)) {
3829 chan->max_cable = UMIDI_CABLES_MAX;
3830 }
3831
3832 for (n = 0; n < chan->max_cable; n++) {
3833
3834 sub = &chan->sub[n];
3835
3836 error = usb_fifo_attach(sc->sc_udev, chan, &chan->lock,
3837 &umidi_fifo_methods, &sub->fifo, unit, n,
3838 chan->iface_index,
3839 UID_ROOT, GID_OPERATOR, 0644);
3840 if (error) {
3841 goto detach;
3842 }
3843 }
3844
3845 lockmgr(&chan->lock, LK_EXCLUSIVE);
3846
3847 /* clear stall first */
3848 usbd_xfer_set_stall(chan->xfer[UMIDI_RX_TRANSFER]);
3849
3850 /*
3851 * NOTE: At least one device will not work properly unless the
3852 * BULK IN pipe is open all the time. This might have to do
3853 * about that the internal queues of the device overflow if we
3854 * don't read them regularly.
3855 */
3856 usbd_transfer_start(chan->xfer[UMIDI_RX_TRANSFER]);
3857
3858 lockmgr(&chan->lock, LK_RELEASE);
3859
3860 return (0); /* success */
3861
3862detach:
3863 return (ENXIO); /* failure */
3864}
3865
3866static int
3867umidi_detach(device_t dev)
3868{
3869 struct uaudio_softc *sc = device_get_softc(dev);
3870 struct umidi_chan *chan = &sc->sc_midi_chan;
3871 uint32_t n;
3872
3873 for (n = 0; n < UMIDI_CABLES_MAX; n++) {
3874 usb_fifo_detach(&chan->sub[n].fifo);
3875 }
3876
3877 lockmgr(&chan->lock, LK_EXCLUSIVE);
3878
3879 usbd_transfer_stop(chan->xfer[UMIDI_RX_TRANSFER]);
3880
3881 lockmgr(&chan->lock, LK_RELEASE);
3882
3883 usbd_transfer_unsetup(chan->xfer, UMIDI_N_TRANSFER);
3884
3885 lockuninit(&chan->lock);
3886
3887 return (0);
3888}
3889
15f415f6 3890DRIVER_MODULE(uaudio, uhub, uaudio_driver, uaudio_devclass, NULL, NULL);
09b9c6f2
SW
3891MODULE_DEPEND(uaudio, usb, 1, 1, 1);
3892MODULE_DEPEND(uaudio, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
3893MODULE_VERSION(uaudio, 1);