uaudio.c revision 1.141 1 /* $NetBSD: uaudio.c,v 1.141 2015/01/26 09:25:08 gson Exp $ */
2
3 /*
4 * Copyright (c) 1999, 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology, and Matthew R. Green (mrg (at) eterna.com.au).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * USB audio specs: http://www.usb.org/developers/devclass_docs/audio10.pdf
35 * http://www.usb.org/developers/devclass_docs/frmts10.pdf
36 * http://www.usb.org/developers/devclass_docs/termt10.pdf
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: uaudio.c,v 1.141 2015/01/26 09:25:08 gson Exp $");
41
42 #ifdef _KERNEL_OPT
43 #include "opt_usb.h"
44 #endif
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/device.h>
51 #include <sys/ioctl.h>
52 #include <sys/file.h>
53 #include <sys/reboot.h> /* for bootverbose */
54 #include <sys/select.h>
55 #include <sys/proc.h>
56 #include <sys/vnode.h>
57 #include <sys/poll.h>
58 #include <sys/module.h>
59 #include <sys/bus.h>
60 #include <sys/cpu.h>
61 #include <sys/atomic.h>
62
63 #include <sys/audioio.h>
64 #include <dev/audio_if.h>
65 #include <dev/audiovar.h>
66 #include <dev/mulaw.h>
67 #include <dev/auconv.h>
68
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usbdi.h>
71 #include <dev/usb/usbdivar.h>
72 #include <dev/usb/usbdi_util.h>
73 #include <dev/usb/usb_quirks.h>
74
75 #include <dev/usb/usbdevs.h>
76
77 #include <dev/usb/uaudioreg.h>
78
79 /* #define UAUDIO_DEBUG */
80 /* #define UAUDIO_MULTIPLE_ENDPOINTS */
81 #ifdef UAUDIO_DEBUG
82 #define DPRINTF(x,y...) do { \
83 if (uaudiodebug) { \
84 struct lwp *l = curlwp; \
85 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
86 } \
87 } while (0)
88 #define DPRINTFN_CLEAN(n,x...) do { \
89 if (uaudiodebug > (n)) \
90 printf(x); \
91 } while (0)
92 #define DPRINTFN(n,x,y...) do { \
93 if (uaudiodebug > (n)) { \
94 struct lwp *l = curlwp; \
95 printf("%s[%d:%d]: "x, __func__, l->l_proc->p_pid, l->l_lid, y); \
96 } \
97 } while (0)
98 int uaudiodebug = 0;
99 #else
100 #define DPRINTF(x,y...)
101 #define DPRINTFN_CLEAN(n,x...)
102 #define DPRINTFN(n,x,y...)
103 #endif
104
105 #define UAUDIO_NCHANBUFS 6 /* number of outstanding request */
106 #define UAUDIO_NFRAMES 10 /* ms of sound in each request */
107
108
109 #define MIX_MAX_CHAN 8
110 struct mixerctl {
111 uint16_t wValue[MIX_MAX_CHAN]; /* using nchan */
112 uint16_t wIndex;
113 uint8_t nchan;
114 uint8_t type;
115 #define MIX_ON_OFF 1
116 #define MIX_SIGNED_16 2
117 #define MIX_UNSIGNED_16 3
118 #define MIX_SIGNED_8 4
119 #define MIX_SELECTOR 5
120 #define MIX_SIZE(n) ((n) == MIX_SIGNED_16 || (n) == MIX_UNSIGNED_16 ? 2 : 1)
121 #define MIX_UNSIGNED(n) ((n) == MIX_UNSIGNED_16)
122 int minval, maxval;
123 u_int delta;
124 u_int mul;
125 uint8_t class;
126 char ctlname[MAX_AUDIO_DEV_LEN];
127 const char *ctlunit;
128 };
129 #define MAKE(h,l) (((h) << 8) | (l))
130
131 struct as_info {
132 uint8_t alt;
133 uint8_t encoding;
134 uint8_t attributes; /* Copy of bmAttributes of
135 * usb_audio_streaming_endpoint_descriptor
136 */
137 usbd_interface_handle ifaceh;
138 const usb_interface_descriptor_t *idesc;
139 const usb_endpoint_descriptor_audio_t *edesc;
140 const usb_endpoint_descriptor_audio_t *edesc1;
141 const struct usb_audio_streaming_type1_descriptor *asf1desc;
142 struct audio_format *aformat;
143 int sc_busy; /* currently used */
144 };
145
146 struct chan {
147 void (*intr)(void *); /* DMA completion intr handler */
148 void *arg; /* arg for intr() */
149 usbd_pipe_handle pipe;
150 usbd_pipe_handle sync_pipe;
151
152 u_int sample_size;
153 u_int sample_rate;
154 u_int bytes_per_frame;
155 u_int fraction; /* fraction/1000 is the extra samples/frame */
156 u_int residue; /* accumulates the fractional samples */
157
158 u_char *start; /* upper layer buffer start */
159 u_char *end; /* upper layer buffer end */
160 u_char *cur; /* current position in upper layer buffer */
161 int blksize; /* chunk size to report up */
162 int transferred; /* transferred bytes not reported up */
163
164 int altidx; /* currently used altidx */
165
166 int curchanbuf;
167 struct chanbuf {
168 struct chan *chan;
169 usbd_xfer_handle xfer;
170 u_char *buffer;
171 uint16_t sizes[UAUDIO_NFRAMES];
172 uint16_t offsets[UAUDIO_NFRAMES];
173 uint16_t size;
174 } chanbufs[UAUDIO_NCHANBUFS];
175
176 struct uaudio_softc *sc; /* our softc */
177 };
178
179 /*
180 * XXX Locking notes:
181 *
182 * The MI USB audio subsystem is not MP-SAFE. Our strategy here
183 * is to ensure we have the kernel lock held when calling into
184 * usbd, and, generally, to have dropped the sc_intr_lock during
185 * these sections as well since the usb code will sleep.
186 */
187 struct uaudio_softc {
188 device_t sc_dev; /* base device */
189 kmutex_t sc_lock;
190 kmutex_t sc_intr_lock;
191 usbd_device_handle sc_udev; /* USB device */
192 int sc_ac_iface; /* Audio Control interface */
193 usbd_interface_handle sc_ac_ifaceh;
194 struct chan sc_playchan; /* play channel */
195 struct chan sc_recchan; /* record channel */
196 int sc_nullalt;
197 int sc_audio_rev;
198 struct as_info *sc_alts; /* alternate settings */
199 int sc_nalts; /* # of alternate settings */
200 int sc_altflags;
201 #define HAS_8 0x01
202 #define HAS_16 0x02
203 #define HAS_8U 0x04
204 #define HAS_ALAW 0x08
205 #define HAS_MULAW 0x10
206 #define UA_NOFRAC 0x20 /* don't do sample rate adjustment */
207 #define HAS_24 0x40
208 int sc_mode; /* play/record capability */
209 struct mixerctl *sc_ctls; /* mixer controls */
210 int sc_nctls; /* # of mixer controls */
211 device_t sc_audiodev;
212 struct audio_format *sc_formats;
213 int sc_nformats;
214 struct audio_encoding_set *sc_encodings;
215 u_int sc_channel_config;
216 char sc_dying;
217 struct audio_device sc_adev;
218 };
219
220 struct terminal_list {
221 int size;
222 uint16_t terminals[1];
223 };
224 #define TERMINAL_LIST_SIZE(N) (offsetof(struct terminal_list, terminals) \
225 + sizeof(uint16_t) * (N))
226
227 struct io_terminal {
228 union {
229 const uaudio_cs_descriptor_t *desc;
230 const struct usb_audio_input_terminal *it;
231 const struct usb_audio_output_terminal *ot;
232 const struct usb_audio_mixer_unit *mu;
233 const struct usb_audio_selector_unit *su;
234 const struct usb_audio_feature_unit *fu;
235 const struct usb_audio_processing_unit *pu;
236 const struct usb_audio_extension_unit *eu;
237 } d;
238 int inputs_size;
239 struct terminal_list **inputs; /* list of source input terminals */
240 struct terminal_list *output; /* list of destination output terminals */
241 int direct; /* directly connected to an output terminal */
242 };
243
244 #define UAC_OUTPUT 0
245 #define UAC_INPUT 1
246 #define UAC_EQUAL 2
247 #define UAC_RECORD 3
248 #define UAC_NCLASSES 4
249 #ifdef UAUDIO_DEBUG
250 Static const char *uac_names[] = {
251 AudioCoutputs, AudioCinputs, AudioCequalization, AudioCrecord,
252 };
253 #endif
254
255 Static usbd_status uaudio_identify_ac
256 (struct uaudio_softc *, const usb_config_descriptor_t *);
257 Static usbd_status uaudio_identify_as
258 (struct uaudio_softc *, const usb_config_descriptor_t *);
259 Static usbd_status uaudio_process_as
260 (struct uaudio_softc *, const char *, int *, int,
261 const usb_interface_descriptor_t *);
262
263 Static void uaudio_add_alt(struct uaudio_softc *, const struct as_info *);
264
265 Static const usb_interface_descriptor_t *uaudio_find_iface
266 (const char *, int, int *, int);
267
268 Static void uaudio_mixer_add_ctl(struct uaudio_softc *, struct mixerctl *);
269 Static char *uaudio_id_name
270 (struct uaudio_softc *, const struct io_terminal *, int);
271 #ifdef UAUDIO_DEBUG
272 Static void uaudio_dump_cluster(const struct usb_audio_cluster *);
273 #endif
274 Static struct usb_audio_cluster uaudio_get_cluster
275 (int, const struct io_terminal *);
276 Static void uaudio_add_input
277 (struct uaudio_softc *, const struct io_terminal *, int);
278 Static void uaudio_add_output
279 (struct uaudio_softc *, const struct io_terminal *, int);
280 Static void uaudio_add_mixer
281 (struct uaudio_softc *, const struct io_terminal *, int);
282 Static void uaudio_add_selector
283 (struct uaudio_softc *, const struct io_terminal *, int);
284 #ifdef UAUDIO_DEBUG
285 Static const char *uaudio_get_terminal_name(int);
286 #endif
287 Static int uaudio_determine_class
288 (const struct io_terminal *, struct mixerctl *);
289 Static const char *uaudio_feature_name
290 (const struct io_terminal *, struct mixerctl *);
291 Static void uaudio_add_feature
292 (struct uaudio_softc *, const struct io_terminal *, int);
293 Static void uaudio_add_processing_updown
294 (struct uaudio_softc *, const struct io_terminal *, int);
295 Static void uaudio_add_processing
296 (struct uaudio_softc *, const struct io_terminal *, int);
297 Static void uaudio_add_extension
298 (struct uaudio_softc *, const struct io_terminal *, int);
299 Static struct terminal_list *uaudio_merge_terminal_list
300 (const struct io_terminal *);
301 Static struct terminal_list *uaudio_io_terminaltype
302 (int, struct io_terminal *, int);
303 Static usbd_status uaudio_identify
304 (struct uaudio_softc *, const usb_config_descriptor_t *);
305
306 Static int uaudio_signext(int, int);
307 Static int uaudio_value2bsd(struct mixerctl *, int);
308 Static int uaudio_bsd2value(struct mixerctl *, int);
309 Static int uaudio_get(struct uaudio_softc *, int, int, int, int, int);
310 Static int uaudio_ctl_get
311 (struct uaudio_softc *, int, struct mixerctl *, int);
312 Static void uaudio_set
313 (struct uaudio_softc *, int, int, int, int, int, int);
314 Static void uaudio_ctl_set
315 (struct uaudio_softc *, int, struct mixerctl *, int, int);
316
317 Static usbd_status uaudio_set_speed(struct uaudio_softc *, int, u_int);
318
319 Static usbd_status uaudio_chan_open(struct uaudio_softc *, struct chan *);
320 Static void uaudio_chan_close(struct uaudio_softc *, struct chan *);
321 Static usbd_status uaudio_chan_alloc_buffers
322 (struct uaudio_softc *, struct chan *);
323 Static void uaudio_chan_free_buffers(struct uaudio_softc *, struct chan *);
324 Static void uaudio_chan_init
325 (struct chan *, int, const struct audio_params *, int);
326 Static void uaudio_chan_set_param(struct chan *, u_char *, u_char *, int);
327 Static void uaudio_chan_ptransfer(struct chan *);
328 Static void uaudio_chan_pintr
329 (usbd_xfer_handle, usbd_private_handle, usbd_status);
330
331 Static void uaudio_chan_rtransfer(struct chan *);
332 Static void uaudio_chan_rintr
333 (usbd_xfer_handle, usbd_private_handle, usbd_status);
334
335 Static int uaudio_open(void *, int);
336 Static void uaudio_close(void *);
337 Static int uaudio_drain(void *);
338 Static int uaudio_query_encoding(void *, struct audio_encoding *);
339 Static int uaudio_set_params
340 (void *, int, int, struct audio_params *, struct audio_params *,
341 stream_filter_list_t *, stream_filter_list_t *);
342 Static int uaudio_round_blocksize(void *, int, int, const audio_params_t *);
343 Static int uaudio_trigger_output
344 (void *, void *, void *, int, void (*)(void *), void *,
345 const audio_params_t *);
346 Static int uaudio_trigger_input
347 (void *, void *, void *, int, void (*)(void *), void *,
348 const audio_params_t *);
349 Static int uaudio_halt_in_dma(void *);
350 Static int uaudio_halt_out_dma(void *);
351 Static int uaudio_getdev(void *, struct audio_device *);
352 Static int uaudio_mixer_set_port(void *, mixer_ctrl_t *);
353 Static int uaudio_mixer_get_port(void *, mixer_ctrl_t *);
354 Static int uaudio_query_devinfo(void *, mixer_devinfo_t *);
355 Static int uaudio_get_props(void *);
356 Static void uaudio_get_locks(void *, kmutex_t **, kmutex_t **);
357
358 Static const struct audio_hw_if uaudio_hw_if = {
359 uaudio_open,
360 uaudio_close,
361 uaudio_drain,
362 uaudio_query_encoding,
363 uaudio_set_params,
364 uaudio_round_blocksize,
365 NULL,
366 NULL,
367 NULL,
368 NULL,
369 NULL,
370 uaudio_halt_out_dma,
371 uaudio_halt_in_dma,
372 NULL,
373 uaudio_getdev,
374 NULL,
375 uaudio_mixer_set_port,
376 uaudio_mixer_get_port,
377 uaudio_query_devinfo,
378 NULL,
379 NULL,
380 NULL,
381 NULL,
382 uaudio_get_props,
383 uaudio_trigger_output,
384 uaudio_trigger_input,
385 NULL,
386 uaudio_get_locks,
387 };
388
389 int uaudio_match(device_t, cfdata_t, void *);
390 void uaudio_attach(device_t, device_t, void *);
391 int uaudio_detach(device_t, int);
392 void uaudio_childdet(device_t, device_t);
393 int uaudio_activate(device_t, enum devact);
394
395 extern struct cfdriver uaudio_cd;
396
397 CFATTACH_DECL2_NEW(uaudio, sizeof(struct uaudio_softc),
398 uaudio_match, uaudio_attach, uaudio_detach, uaudio_activate, NULL,
399 uaudio_childdet);
400
401 int
402 uaudio_match(device_t parent, cfdata_t match, void *aux)
403 {
404 struct usbif_attach_arg *uaa = aux;
405
406 /* Trigger on the control interface. */
407 if (uaa->class != UICLASS_AUDIO ||
408 uaa->subclass != UISUBCLASS_AUDIOCONTROL ||
409 (usbd_get_quirks(uaa->device)->uq_flags & UQ_BAD_AUDIO))
410 return UMATCH_NONE;
411
412 return UMATCH_IFACECLASS_IFACESUBCLASS;
413 }
414
415 void
416 uaudio_attach(device_t parent, device_t self, void *aux)
417 {
418 struct uaudio_softc *sc = device_private(self);
419 struct usbif_attach_arg *uaa = aux;
420 usb_interface_descriptor_t *id;
421 usb_config_descriptor_t *cdesc;
422 char *devinfop;
423 usbd_status err;
424 int i, j, found;
425
426 sc->sc_dev = self;
427 sc->sc_udev = uaa->device;
428 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
429 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_SCHED);
430
431 strlcpy(sc->sc_adev.name, "USB audio", sizeof(sc->sc_adev.name));
432 strlcpy(sc->sc_adev.version, "", sizeof(sc->sc_adev.version));
433 snprintf(sc->sc_adev.config, sizeof(sc->sc_adev.config), "usb:%08x",
434 sc->sc_udev->cookie.cookie);
435
436 aprint_naive("\n");
437 aprint_normal("\n");
438
439 devinfop = usbd_devinfo_alloc(uaa->device, 0);
440 aprint_normal_dev(self, "%s\n", devinfop);
441 usbd_devinfo_free(devinfop);
442
443 cdesc = usbd_get_config_descriptor(sc->sc_udev);
444 if (cdesc == NULL) {
445 aprint_error_dev(self,
446 "failed to get configuration descriptor\n");
447 return;
448 }
449
450 err = uaudio_identify(sc, cdesc);
451 if (err) {
452 aprint_error_dev(self,
453 "audio descriptors make no sense, error=%d\n", err);
454 return;
455 }
456
457 sc->sc_ac_ifaceh = uaa->iface;
458 /* Pick up the AS interface. */
459 for (i = 0; i < uaa->nifaces; i++) {
460 if (uaa->ifaces[i] == NULL)
461 continue;
462 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
463 if (id == NULL)
464 continue;
465 found = 0;
466 for (j = 0; j < sc->sc_nalts; j++) {
467 if (id->bInterfaceNumber ==
468 sc->sc_alts[j].idesc->bInterfaceNumber) {
469 sc->sc_alts[j].ifaceh = uaa->ifaces[i];
470 found = 1;
471 }
472 }
473 if (found)
474 uaa->ifaces[i] = NULL;
475 }
476
477 for (j = 0; j < sc->sc_nalts; j++) {
478 if (sc->sc_alts[j].ifaceh == NULL) {
479 aprint_error_dev(self,
480 "alt %d missing AS interface(s)\n", j);
481 return;
482 }
483 }
484
485 aprint_normal_dev(self, "audio rev %d.%02x\n",
486 sc->sc_audio_rev >> 8, sc->sc_audio_rev & 0xff);
487
488 sc->sc_playchan.sc = sc->sc_recchan.sc = sc;
489 sc->sc_playchan.altidx = -1;
490 sc->sc_recchan.altidx = -1;
491
492 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_FRAC)
493 sc->sc_altflags |= UA_NOFRAC;
494
495 #ifndef UAUDIO_DEBUG
496 if (bootverbose)
497 #endif
498 aprint_normal_dev(self, "%d mixer controls\n",
499 sc->sc_nctls);
500
501 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
502 sc->sc_dev);
503
504 DPRINTF("%s", "doing audio_attach_mi\n");
505 sc->sc_audiodev = audio_attach_mi(&uaudio_hw_if, sc, sc->sc_dev);
506
507 return;
508 }
509
510 int
511 uaudio_activate(device_t self, enum devact act)
512 {
513 struct uaudio_softc *sc = device_private(self);
514
515 switch (act) {
516 case DVACT_DEACTIVATE:
517 sc->sc_dying = 1;
518 return 0;
519 default:
520 return EOPNOTSUPP;
521 }
522 }
523
524 void
525 uaudio_childdet(device_t self, device_t child)
526 {
527 struct uaudio_softc *sc = device_private(self);
528
529 KASSERT(sc->sc_audiodev == child);
530 sc->sc_audiodev = NULL;
531 }
532
533 int
534 uaudio_detach(device_t self, int flags)
535 {
536 struct uaudio_softc *sc = device_private(self);
537 int rv;
538
539 rv = 0;
540 /* Wait for outstanding requests to complete. */
541 usbd_delay_ms(sc->sc_udev, UAUDIO_NCHANBUFS * UAUDIO_NFRAMES);
542
543 if (sc->sc_audiodev != NULL)
544 rv = config_detach(sc->sc_audiodev, flags);
545
546 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
547 sc->sc_dev);
548
549 if (sc->sc_formats != NULL)
550 free(sc->sc_formats, M_USBDEV);
551 auconv_delete_encodings(sc->sc_encodings);
552
553 mutex_destroy(&sc->sc_lock);
554 mutex_destroy(&sc->sc_intr_lock);
555
556 return rv;
557 }
558
559 Static int
560 uaudio_query_encoding(void *addr, struct audio_encoding *fp)
561 {
562 struct uaudio_softc *sc;
563 int flags;
564
565 sc = addr;
566 flags = sc->sc_altflags;
567 if (sc->sc_dying)
568 return EIO;
569
570 if (sc->sc_nalts == 0 || flags == 0)
571 return ENXIO;
572
573 return auconv_query_encoding(sc->sc_encodings, fp);
574 }
575
576 Static const usb_interface_descriptor_t *
577 uaudio_find_iface(const char *tbuf, int size, int *offsp, int subtype)
578 {
579 const usb_interface_descriptor_t *d;
580
581 while (*offsp < size) {
582 d = (const void *)(tbuf + *offsp);
583 *offsp += d->bLength;
584 if (d->bDescriptorType == UDESC_INTERFACE &&
585 d->bInterfaceClass == UICLASS_AUDIO &&
586 d->bInterfaceSubClass == subtype)
587 return d;
588 }
589 return NULL;
590 }
591
592 Static void
593 uaudio_mixer_add_ctl(struct uaudio_softc *sc, struct mixerctl *mc)
594 {
595 int res;
596 size_t len;
597 struct mixerctl *nmc;
598
599 if (mc->class < UAC_NCLASSES) {
600 DPRINTF("adding %s.%s\n", uac_names[mc->class], mc->ctlname);
601 } else {
602 DPRINTF("adding %s\n", mc->ctlname);
603 }
604 len = sizeof(*mc) * (sc->sc_nctls + 1);
605 nmc = malloc(len, M_USBDEV, M_NOWAIT);
606 if (nmc == NULL) {
607 aprint_error("uaudio_mixer_add_ctl: no memory\n");
608 return;
609 }
610 /* Copy old data, if there was any */
611 if (sc->sc_nctls != 0) {
612 memcpy(nmc, sc->sc_ctls, sizeof(*mc) * (sc->sc_nctls));
613 free(sc->sc_ctls, M_USBDEV);
614 }
615 sc->sc_ctls = nmc;
616
617 mc->delta = 0;
618 if (mc->type == MIX_ON_OFF) {
619 mc->minval = 0;
620 mc->maxval = 1;
621 } else if (mc->type == MIX_SELECTOR) {
622 ;
623 } else {
624 /* Determine min and max values. */
625 mc->minval = uaudio_signext(mc->type,
626 uaudio_get(sc, GET_MIN, UT_READ_CLASS_INTERFACE,
627 mc->wValue[0], mc->wIndex,
628 MIX_SIZE(mc->type)));
629 mc->maxval = 1 + uaudio_signext(mc->type,
630 uaudio_get(sc, GET_MAX, UT_READ_CLASS_INTERFACE,
631 mc->wValue[0], mc->wIndex,
632 MIX_SIZE(mc->type)));
633 mc->mul = mc->maxval - mc->minval;
634 if (mc->mul == 0)
635 mc->mul = 1;
636 res = uaudio_get(sc, GET_RES, UT_READ_CLASS_INTERFACE,
637 mc->wValue[0], mc->wIndex,
638 MIX_SIZE(mc->type));
639 if (res > 0)
640 mc->delta = (res * 255 + mc->mul/2) / mc->mul;
641 }
642
643 sc->sc_ctls[sc->sc_nctls++] = *mc;
644
645 #ifdef UAUDIO_DEBUG
646 if (uaudiodebug > 2) {
647 int i;
648
649 DPRINTFN_CLEAN(2, "wValue=%04x", mc->wValue[0]);
650 for (i = 1; i < mc->nchan; i++)
651 DPRINTFN_CLEAN(2, ",%04x", mc->wValue[i]);
652 DPRINTFN_CLEAN(2, " wIndex=%04x type=%d name='%s' unit='%s' "
653 "min=%d max=%d\n",
654 mc->wIndex, mc->type, mc->ctlname, mc->ctlunit,
655 mc->minval, mc->maxval);
656 }
657 #endif
658 }
659
660 Static char *
661 uaudio_id_name(struct uaudio_softc *sc,
662 const struct io_terminal *iot, int id)
663 {
664 static char tbuf[32];
665
666 snprintf(tbuf, sizeof(tbuf), "i%d", id);
667 return tbuf;
668 }
669
670 #ifdef UAUDIO_DEBUG
671 Static void
672 uaudio_dump_cluster(const struct usb_audio_cluster *cl)
673 {
674 static const char *channel_names[16] = {
675 "LEFT", "RIGHT", "CENTER", "LFE",
676 "LEFT_SURROUND", "RIGHT_SURROUND", "LEFT_CENTER", "RIGHT_CENTER",
677 "SURROUND", "LEFT_SIDE", "RIGHT_SIDE", "TOP",
678 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15",
679 };
680 int cc, i, first;
681
682 cc = UGETW(cl->wChannelConfig);
683 printf("cluster: bNrChannels=%u wChannelConfig=0x%.4x",
684 cl->bNrChannels, cc);
685 first = TRUE;
686 for (i = 0; cc != 0; i++) {
687 if (cc & 1) {
688 printf("%c%s", first ? '<' : ',', channel_names[i]);
689 first = FALSE;
690 }
691 cc = cc >> 1;
692 }
693 printf("> iChannelNames=%u", cl->iChannelNames);
694 }
695 #endif
696
697 Static struct usb_audio_cluster
698 uaudio_get_cluster(int id, const struct io_terminal *iot)
699 {
700 struct usb_audio_cluster r;
701 const uaudio_cs_descriptor_t *dp;
702 int i;
703
704 for (i = 0; i < 25; i++) { /* avoid infinite loops */
705 dp = iot[id].d.desc;
706 if (dp == 0)
707 goto bad;
708 switch (dp->bDescriptorSubtype) {
709 case UDESCSUB_AC_INPUT:
710 r.bNrChannels = iot[id].d.it->bNrChannels;
711 USETW(r.wChannelConfig, UGETW(iot[id].d.it->wChannelConfig));
712 r.iChannelNames = iot[id].d.it->iChannelNames;
713 return r;
714 case UDESCSUB_AC_OUTPUT:
715 id = iot[id].d.ot->bSourceId;
716 break;
717 case UDESCSUB_AC_MIXER:
718 r = *(const struct usb_audio_cluster *)
719 &iot[id].d.mu->baSourceId[iot[id].d.mu->bNrInPins];
720 return r;
721 case UDESCSUB_AC_SELECTOR:
722 /* XXX This is not really right */
723 id = iot[id].d.su->baSourceId[0];
724 break;
725 case UDESCSUB_AC_FEATURE:
726 id = iot[id].d.fu->bSourceId;
727 break;
728 case UDESCSUB_AC_PROCESSING:
729 r = *(const struct usb_audio_cluster *)
730 &iot[id].d.pu->baSourceId[iot[id].d.pu->bNrInPins];
731 return r;
732 case UDESCSUB_AC_EXTENSION:
733 r = *(const struct usb_audio_cluster *)
734 &iot[id].d.eu->baSourceId[iot[id].d.eu->bNrInPins];
735 return r;
736 default:
737 goto bad;
738 }
739 }
740 bad:
741 aprint_error("uaudio_get_cluster: bad data\n");
742 memset(&r, 0, sizeof r);
743 return r;
744
745 }
746
747 Static void
748 uaudio_add_input(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
749 {
750 const struct usb_audio_input_terminal *d;
751
752 d = iot[id].d.it;
753 #ifdef UAUDIO_DEBUG
754 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
755 "bAssocTerminal=%d bNrChannels=%d wChannelConfig=%d "
756 "iChannelNames=%d iTerminal=%d\n",
757 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
758 d->bNrChannels, UGETW(d->wChannelConfig),
759 d->iChannelNames, d->iTerminal);
760 #endif
761 /* If USB input terminal, record wChannelConfig */
762 if ((UGETW(d->wTerminalType) & 0xff00) != 0x0100)
763 return;
764 sc->sc_channel_config = UGETW(d->wChannelConfig);
765 }
766
767 Static void
768 uaudio_add_output(struct uaudio_softc *sc,
769 const struct io_terminal *iot, int id)
770 {
771 #ifdef UAUDIO_DEBUG
772 const struct usb_audio_output_terminal *d;
773
774 d = iot[id].d.ot;
775 DPRINTFN(2,"bTerminalId=%d wTerminalType=0x%04x "
776 "bAssocTerminal=%d bSourceId=%d iTerminal=%d\n",
777 d->bTerminalId, UGETW(d->wTerminalType), d->bAssocTerminal,
778 d->bSourceId, d->iTerminal);
779 #endif
780 }
781
782 Static void
783 uaudio_add_mixer(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
784 {
785 const struct usb_audio_mixer_unit *d;
786 const struct usb_audio_mixer_unit_1 *d1;
787 int c, chs, ichs, ochs, i, o, bno, p, mo, mc, k;
788 const uByte *bm;
789 struct mixerctl mix;
790
791 d = iot[id].d.mu;
792 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
793 d->bUnitId, d->bNrInPins);
794
795 /* Compute the number of input channels */
796 ichs = 0;
797 for (i = 0; i < d->bNrInPins; i++)
798 ichs += uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
799
800 /* and the number of output channels */
801 d1 = (const struct usb_audio_mixer_unit_1 *)&d->baSourceId[d->bNrInPins];
802 ochs = d1->bNrChannels;
803 DPRINTFN(2,"ichs=%d ochs=%d\n", ichs, ochs);
804
805 bm = d1->bmControls;
806 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
807 uaudio_determine_class(&iot[id], &mix);
808 mix.type = MIX_SIGNED_16;
809 mix.ctlunit = AudioNvolume;
810 #define _BIT(bno) ((bm[bno / 8] >> (7 - bno % 8)) & 1)
811 for (p = i = 0; i < d->bNrInPins; i++) {
812 chs = uaudio_get_cluster(d->baSourceId[i], iot).bNrChannels;
813 mc = 0;
814 for (c = 0; c < chs; c++) {
815 mo = 0;
816 for (o = 0; o < ochs; o++) {
817 bno = (p + c) * ochs + o;
818 if (_BIT(bno))
819 mo++;
820 }
821 if (mo == 1)
822 mc++;
823 }
824 if (mc == chs && chs <= MIX_MAX_CHAN) {
825 k = 0;
826 for (c = 0; c < chs; c++)
827 for (o = 0; o < ochs; o++) {
828 bno = (p + c) * ochs + o;
829 if (_BIT(bno))
830 mix.wValue[k++] =
831 MAKE(p+c+1, o+1);
832 }
833 snprintf(mix.ctlname, sizeof(mix.ctlname), "mix%d-%s",
834 d->bUnitId, uaudio_id_name(sc, iot,
835 d->baSourceId[i]));
836 mix.nchan = chs;
837 uaudio_mixer_add_ctl(sc, &mix);
838 } else {
839 /* XXX */
840 }
841 #undef _BIT
842 p += chs;
843 }
844
845 }
846
847 Static void
848 uaudio_add_selector(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
849 {
850 const struct usb_audio_selector_unit *d;
851 struct mixerctl mix;
852 int i, wp;
853
854 d = iot[id].d.su;
855 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
856 d->bUnitId, d->bNrInPins);
857 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
858 mix.wValue[0] = MAKE(0, 0);
859 uaudio_determine_class(&iot[id], &mix);
860 mix.nchan = 1;
861 mix.type = MIX_SELECTOR;
862 mix.ctlunit = "";
863 mix.minval = 1;
864 mix.maxval = d->bNrInPins;
865 mix.mul = mix.maxval - mix.minval;
866 wp = snprintf(mix.ctlname, MAX_AUDIO_DEV_LEN, "sel%d-", d->bUnitId);
867 for (i = 1; i <= d->bNrInPins; i++) {
868 wp += snprintf(mix.ctlname + wp, MAX_AUDIO_DEV_LEN - wp,
869 "i%d", d->baSourceId[i - 1]);
870 if (wp > MAX_AUDIO_DEV_LEN - 1)
871 break;
872 }
873 uaudio_mixer_add_ctl(sc, &mix);
874 }
875
876 #ifdef UAUDIO_DEBUG
877 Static const char *
878 uaudio_get_terminal_name(int terminal_type)
879 {
880 static char tbuf[100];
881
882 switch (terminal_type) {
883 /* USB terminal types */
884 case UAT_UNDEFINED: return "UAT_UNDEFINED";
885 case UAT_STREAM: return "UAT_STREAM";
886 case UAT_VENDOR: return "UAT_VENDOR";
887 /* input terminal types */
888 case UATI_UNDEFINED: return "UATI_UNDEFINED";
889 case UATI_MICROPHONE: return "UATI_MICROPHONE";
890 case UATI_DESKMICROPHONE: return "UATI_DESKMICROPHONE";
891 case UATI_PERSONALMICROPHONE: return "UATI_PERSONALMICROPHONE";
892 case UATI_OMNIMICROPHONE: return "UATI_OMNIMICROPHONE";
893 case UATI_MICROPHONEARRAY: return "UATI_MICROPHONEARRAY";
894 case UATI_PROCMICROPHONEARR: return "UATI_PROCMICROPHONEARR";
895 /* output terminal types */
896 case UATO_UNDEFINED: return "UATO_UNDEFINED";
897 case UATO_SPEAKER: return "UATO_SPEAKER";
898 case UATO_HEADPHONES: return "UATO_HEADPHONES";
899 case UATO_DISPLAYAUDIO: return "UATO_DISPLAYAUDIO";
900 case UATO_DESKTOPSPEAKER: return "UATO_DESKTOPSPEAKER";
901 case UATO_ROOMSPEAKER: return "UATO_ROOMSPEAKER";
902 case UATO_COMMSPEAKER: return "UATO_COMMSPEAKER";
903 case UATO_SUBWOOFER: return "UATO_SUBWOOFER";
904 /* bidir terminal types */
905 case UATB_UNDEFINED: return "UATB_UNDEFINED";
906 case UATB_HANDSET: return "UATB_HANDSET";
907 case UATB_HEADSET: return "UATB_HEADSET";
908 case UATB_SPEAKERPHONE: return "UATB_SPEAKERPHONE";
909 case UATB_SPEAKERPHONEESUP: return "UATB_SPEAKERPHONEESUP";
910 case UATB_SPEAKERPHONEECANC: return "UATB_SPEAKERPHONEECANC";
911 /* telephony terminal types */
912 case UATT_UNDEFINED: return "UATT_UNDEFINED";
913 case UATT_PHONELINE: return "UATT_PHONELINE";
914 case UATT_TELEPHONE: return "UATT_TELEPHONE";
915 case UATT_DOWNLINEPHONE: return "UATT_DOWNLINEPHONE";
916 /* external terminal types */
917 case UATE_UNDEFINED: return "UATE_UNDEFINED";
918 case UATE_ANALOGCONN: return "UATE_ANALOGCONN";
919 case UATE_LINECONN: return "UATE_LINECONN";
920 case UATE_LEGACYCONN: return "UATE_LEGACYCONN";
921 case UATE_DIGITALAUIFC: return "UATE_DIGITALAUIFC";
922 case UATE_SPDIF: return "UATE_SPDIF";
923 case UATE_1394DA: return "UATE_1394DA";
924 case UATE_1394DV: return "UATE_1394DV";
925 /* embedded function terminal types */
926 case UATF_UNDEFINED: return "UATF_UNDEFINED";
927 case UATF_CALIBNOISE: return "UATF_CALIBNOISE";
928 case UATF_EQUNOISE: return "UATF_EQUNOISE";
929 case UATF_CDPLAYER: return "UATF_CDPLAYER";
930 case UATF_DAT: return "UATF_DAT";
931 case UATF_DCC: return "UATF_DCC";
932 case UATF_MINIDISK: return "UATF_MINIDISK";
933 case UATF_ANALOGTAPE: return "UATF_ANALOGTAPE";
934 case UATF_PHONOGRAPH: return "UATF_PHONOGRAPH";
935 case UATF_VCRAUDIO: return "UATF_VCRAUDIO";
936 case UATF_VIDEODISCAUDIO: return "UATF_VIDEODISCAUDIO";
937 case UATF_DVDAUDIO: return "UATF_DVDAUDIO";
938 case UATF_TVTUNERAUDIO: return "UATF_TVTUNERAUDIO";
939 case UATF_SATELLITE: return "UATF_SATELLITE";
940 case UATF_CABLETUNER: return "UATF_CABLETUNER";
941 case UATF_DSS: return "UATF_DSS";
942 case UATF_RADIORECV: return "UATF_RADIORECV";
943 case UATF_RADIOXMIT: return "UATF_RADIOXMIT";
944 case UATF_MULTITRACK: return "UATF_MULTITRACK";
945 case UATF_SYNTHESIZER: return "UATF_SYNTHESIZER";
946 default:
947 snprintf(tbuf, sizeof(tbuf), "unknown type (0x%.4x)", terminal_type);
948 return tbuf;
949 }
950 }
951 #endif
952
953 Static int
954 uaudio_determine_class(const struct io_terminal *iot, struct mixerctl *mix)
955 {
956 int terminal_type;
957
958 if (iot == NULL || iot->output == NULL) {
959 mix->class = UAC_OUTPUT;
960 return 0;
961 }
962 terminal_type = 0;
963 if (iot->output->size == 1)
964 terminal_type = iot->output->terminals[0];
965 /*
966 * If the only output terminal is USB,
967 * the class is UAC_RECORD.
968 */
969 if ((terminal_type & 0xff00) == (UAT_UNDEFINED & 0xff00)) {
970 mix->class = UAC_RECORD;
971 if (iot->inputs_size == 1
972 && iot->inputs[0] != NULL
973 && iot->inputs[0]->size == 1)
974 return iot->inputs[0]->terminals[0];
975 else
976 return 0;
977 }
978 /*
979 * If the ultimate destination of the unit is just one output
980 * terminal and the unit is connected to the output terminal
981 * directly, the class is UAC_OUTPUT.
982 */
983 if (terminal_type != 0 && iot->direct) {
984 mix->class = UAC_OUTPUT;
985 return terminal_type;
986 }
987 /*
988 * If the unit is connected to just one input terminal,
989 * the class is UAC_INPUT.
990 */
991 if (iot->inputs_size == 1 && iot->inputs[0] != NULL
992 && iot->inputs[0]->size == 1) {
993 mix->class = UAC_INPUT;
994 return iot->inputs[0]->terminals[0];
995 }
996 /*
997 * Otherwise, the class is UAC_OUTPUT.
998 */
999 mix->class = UAC_OUTPUT;
1000 return terminal_type;
1001 }
1002
1003 Static const char *
1004 uaudio_feature_name(const struct io_terminal *iot, struct mixerctl *mix)
1005 {
1006 int terminal_type;
1007
1008 terminal_type = uaudio_determine_class(iot, mix);
1009 if (mix->class == UAC_RECORD && terminal_type == 0)
1010 return AudioNmixerout;
1011 DPRINTF("terminal_type=%s\n", uaudio_get_terminal_name(terminal_type));
1012 switch (terminal_type) {
1013 case UAT_STREAM:
1014 return AudioNdac;
1015
1016 case UATI_MICROPHONE:
1017 case UATI_DESKMICROPHONE:
1018 case UATI_PERSONALMICROPHONE:
1019 case UATI_OMNIMICROPHONE:
1020 case UATI_MICROPHONEARRAY:
1021 case UATI_PROCMICROPHONEARR:
1022 return AudioNmicrophone;
1023
1024 case UATO_SPEAKER:
1025 case UATO_DESKTOPSPEAKER:
1026 case UATO_ROOMSPEAKER:
1027 case UATO_COMMSPEAKER:
1028 return AudioNspeaker;
1029
1030 case UATO_HEADPHONES:
1031 return AudioNheadphone;
1032
1033 case UATO_SUBWOOFER:
1034 return AudioNlfe;
1035
1036 /* telephony terminal types */
1037 case UATT_UNDEFINED:
1038 case UATT_PHONELINE:
1039 case UATT_TELEPHONE:
1040 case UATT_DOWNLINEPHONE:
1041 return "phone";
1042
1043 case UATE_ANALOGCONN:
1044 case UATE_LINECONN:
1045 case UATE_LEGACYCONN:
1046 return AudioNline;
1047
1048 case UATE_DIGITALAUIFC:
1049 case UATE_SPDIF:
1050 case UATE_1394DA:
1051 case UATE_1394DV:
1052 return AudioNaux;
1053
1054 case UATF_CDPLAYER:
1055 return AudioNcd;
1056
1057 case UATF_SYNTHESIZER:
1058 return AudioNfmsynth;
1059
1060 case UATF_VIDEODISCAUDIO:
1061 case UATF_DVDAUDIO:
1062 case UATF_TVTUNERAUDIO:
1063 return AudioNvideo;
1064
1065 case UAT_UNDEFINED:
1066 case UAT_VENDOR:
1067 case UATI_UNDEFINED:
1068 /* output terminal types */
1069 case UATO_UNDEFINED:
1070 case UATO_DISPLAYAUDIO:
1071 /* bidir terminal types */
1072 case UATB_UNDEFINED:
1073 case UATB_HANDSET:
1074 case UATB_HEADSET:
1075 case UATB_SPEAKERPHONE:
1076 case UATB_SPEAKERPHONEESUP:
1077 case UATB_SPEAKERPHONEECANC:
1078 /* external terminal types */
1079 case UATE_UNDEFINED:
1080 /* embedded function terminal types */
1081 case UATF_UNDEFINED:
1082 case UATF_CALIBNOISE:
1083 case UATF_EQUNOISE:
1084 case UATF_DAT:
1085 case UATF_DCC:
1086 case UATF_MINIDISK:
1087 case UATF_ANALOGTAPE:
1088 case UATF_PHONOGRAPH:
1089 case UATF_VCRAUDIO:
1090 case UATF_SATELLITE:
1091 case UATF_CABLETUNER:
1092 case UATF_DSS:
1093 case UATF_RADIORECV:
1094 case UATF_RADIOXMIT:
1095 case UATF_MULTITRACK:
1096 case 0xffff:
1097 default:
1098 DPRINTF("'master' for 0x%.4x\n", terminal_type);
1099 return AudioNmaster;
1100 }
1101 return AudioNmaster;
1102 }
1103
1104 Static void
1105 uaudio_add_feature(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1106 {
1107 const struct usb_audio_feature_unit *d;
1108 const uByte *ctls;
1109 int ctlsize;
1110 int nchan;
1111 u_int fumask, mmask, cmask;
1112 struct mixerctl mix;
1113 int chan, ctl, i, unit;
1114 const char *mixername;
1115
1116 #define GET(i) (ctls[(i)*ctlsize] | \
1117 (ctlsize > 1 ? ctls[(i)*ctlsize+1] << 8 : 0))
1118 d = iot[id].d.fu;
1119 ctls = d->bmaControls;
1120 ctlsize = d->bControlSize;
1121 nchan = (d->bLength - 7) / ctlsize;
1122 mmask = GET(0);
1123 /* Figure out what we can control */
1124 for (cmask = 0, chan = 1; chan < nchan; chan++) {
1125 DPRINTFN(9,"chan=%d mask=%x\n",
1126 chan, GET(chan));
1127 cmask |= GET(chan);
1128 }
1129
1130 DPRINTFN(1,"bUnitId=%d, "
1131 "%d channels, mmask=0x%04x, cmask=0x%04x\n",
1132 d->bUnitId, nchan, mmask, cmask);
1133
1134 if (nchan > MIX_MAX_CHAN)
1135 nchan = MIX_MAX_CHAN;
1136 unit = d->bUnitId;
1137 mix.wIndex = MAKE(unit, sc->sc_ac_iface);
1138 for (ctl = MUTE_CONTROL; ctl < LOUDNESS_CONTROL; ctl++) {
1139 fumask = FU_MASK(ctl);
1140 DPRINTFN(4,"ctl=%d fumask=0x%04x\n",
1141 ctl, fumask);
1142 if (mmask & fumask) {
1143 mix.nchan = 1;
1144 mix.wValue[0] = MAKE(ctl, 0);
1145 } else if (cmask & fumask) {
1146 mix.nchan = nchan - 1;
1147 for (i = 1; i < nchan; i++) {
1148 if (GET(i) & fumask)
1149 mix.wValue[i-1] = MAKE(ctl, i);
1150 else
1151 mix.wValue[i-1] = -1;
1152 }
1153 } else {
1154 continue;
1155 }
1156 #undef GET
1157 mixername = uaudio_feature_name(&iot[id], &mix);
1158 switch (ctl) {
1159 case MUTE_CONTROL:
1160 mix.type = MIX_ON_OFF;
1161 mix.ctlunit = "";
1162 snprintf(mix.ctlname, sizeof(mix.ctlname),
1163 "%s.%s", mixername, AudioNmute);
1164 break;
1165 case VOLUME_CONTROL:
1166 mix.type = MIX_SIGNED_16;
1167 mix.ctlunit = AudioNvolume;
1168 strlcpy(mix.ctlname, mixername, sizeof(mix.ctlname));
1169 break;
1170 case BASS_CONTROL:
1171 mix.type = MIX_SIGNED_8;
1172 mix.ctlunit = AudioNbass;
1173 snprintf(mix.ctlname, sizeof(mix.ctlname),
1174 "%s.%s", mixername, AudioNbass);
1175 break;
1176 case MID_CONTROL:
1177 mix.type = MIX_SIGNED_8;
1178 mix.ctlunit = AudioNmid;
1179 snprintf(mix.ctlname, sizeof(mix.ctlname),
1180 "%s.%s", mixername, AudioNmid);
1181 break;
1182 case TREBLE_CONTROL:
1183 mix.type = MIX_SIGNED_8;
1184 mix.ctlunit = AudioNtreble;
1185 snprintf(mix.ctlname, sizeof(mix.ctlname),
1186 "%s.%s", mixername, AudioNtreble);
1187 break;
1188 case GRAPHIC_EQUALIZER_CONTROL:
1189 continue; /* XXX don't add anything */
1190 break;
1191 case AGC_CONTROL:
1192 mix.type = MIX_ON_OFF;
1193 mix.ctlunit = "";
1194 snprintf(mix.ctlname, sizeof(mix.ctlname), "%s.%s",
1195 mixername, AudioNagc);
1196 break;
1197 case DELAY_CONTROL:
1198 mix.type = MIX_UNSIGNED_16;
1199 mix.ctlunit = "4 ms";
1200 snprintf(mix.ctlname, sizeof(mix.ctlname),
1201 "%s.%s", mixername, AudioNdelay);
1202 break;
1203 case BASS_BOOST_CONTROL:
1204 mix.type = MIX_ON_OFF;
1205 mix.ctlunit = "";
1206 snprintf(mix.ctlname, sizeof(mix.ctlname),
1207 "%s.%s", mixername, AudioNbassboost);
1208 break;
1209 case LOUDNESS_CONTROL:
1210 mix.type = MIX_ON_OFF;
1211 mix.ctlunit = "";
1212 snprintf(mix.ctlname, sizeof(mix.ctlname),
1213 "%s.%s", mixername, AudioNloudness);
1214 break;
1215 }
1216 uaudio_mixer_add_ctl(sc, &mix);
1217 }
1218 }
1219
1220 Static void
1221 uaudio_add_processing_updown(struct uaudio_softc *sc,
1222 const struct io_terminal *iot, int id)
1223 {
1224 const struct usb_audio_processing_unit *d;
1225 const struct usb_audio_processing_unit_1 *d1;
1226 const struct usb_audio_processing_unit_updown *ud;
1227 struct mixerctl mix;
1228 int i;
1229
1230 d = iot[id].d.pu;
1231 d1 = (const struct usb_audio_processing_unit_1 *)
1232 &d->baSourceId[d->bNrInPins];
1233 ud = (const struct usb_audio_processing_unit_updown *)
1234 &d1->bmControls[d1->bControlSize];
1235 DPRINTFN(2,"bUnitId=%d bNrModes=%d\n",
1236 d->bUnitId, ud->bNrModes);
1237
1238 if (!(d1->bmControls[0] & UA_PROC_MASK(UD_MODE_SELECT_CONTROL))) {
1239 DPRINTF("%s", "no mode select\n");
1240 return;
1241 }
1242
1243 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1244 mix.nchan = 1;
1245 mix.wValue[0] = MAKE(UD_MODE_SELECT_CONTROL, 0);
1246 uaudio_determine_class(&iot[id], &mix);
1247 mix.type = MIX_ON_OFF; /* XXX */
1248 mix.ctlunit = "";
1249 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d-mode", d->bUnitId);
1250
1251 for (i = 0; i < ud->bNrModes; i++) {
1252 DPRINTFN(2,"i=%d bm=0x%x\n",
1253 i, UGETW(ud->waModes[i]));
1254 /* XXX */
1255 }
1256 uaudio_mixer_add_ctl(sc, &mix);
1257 }
1258
1259 Static void
1260 uaudio_add_processing(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1261 {
1262 const struct usb_audio_processing_unit *d;
1263 const struct usb_audio_processing_unit_1 *d1;
1264 int ptype;
1265 struct mixerctl mix;
1266
1267 d = iot[id].d.pu;
1268 d1 = (const struct usb_audio_processing_unit_1 *)
1269 &d->baSourceId[d->bNrInPins];
1270 ptype = UGETW(d->wProcessType);
1271 DPRINTFN(2,"wProcessType=%d bUnitId=%d "
1272 "bNrInPins=%d\n", ptype, d->bUnitId, d->bNrInPins);
1273
1274 if (d1->bmControls[0] & UA_PROC_ENABLE_MASK) {
1275 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1276 mix.nchan = 1;
1277 mix.wValue[0] = MAKE(XX_ENABLE_CONTROL, 0);
1278 uaudio_determine_class(&iot[id], &mix);
1279 mix.type = MIX_ON_OFF;
1280 mix.ctlunit = "";
1281 snprintf(mix.ctlname, sizeof(mix.ctlname), "pro%d.%d-enable",
1282 d->bUnitId, ptype);
1283 uaudio_mixer_add_ctl(sc, &mix);
1284 }
1285
1286 switch(ptype) {
1287 case UPDOWNMIX_PROCESS:
1288 uaudio_add_processing_updown(sc, iot, id);
1289 break;
1290 case DOLBY_PROLOGIC_PROCESS:
1291 case P3D_STEREO_EXTENDER_PROCESS:
1292 case REVERBATION_PROCESS:
1293 case CHORUS_PROCESS:
1294 case DYN_RANGE_COMP_PROCESS:
1295 default:
1296 #ifdef UAUDIO_DEBUG
1297 aprint_debug(
1298 "uaudio_add_processing: unit %d, type=%d not impl.\n",
1299 d->bUnitId, ptype);
1300 #endif
1301 break;
1302 }
1303 }
1304
1305 Static void
1306 uaudio_add_extension(struct uaudio_softc *sc, const struct io_terminal *iot, int id)
1307 {
1308 const struct usb_audio_extension_unit *d;
1309 const struct usb_audio_extension_unit_1 *d1;
1310 struct mixerctl mix;
1311
1312 d = iot[id].d.eu;
1313 d1 = (const struct usb_audio_extension_unit_1 *)
1314 &d->baSourceId[d->bNrInPins];
1315 DPRINTFN(2,"bUnitId=%d bNrInPins=%d\n",
1316 d->bUnitId, d->bNrInPins);
1317
1318 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_NO_XU)
1319 return;
1320
1321 if (d1->bmControls[0] & UA_EXT_ENABLE_MASK) {
1322 mix.wIndex = MAKE(d->bUnitId, sc->sc_ac_iface);
1323 mix.nchan = 1;
1324 mix.wValue[0] = MAKE(UA_EXT_ENABLE, 0);
1325 uaudio_determine_class(&iot[id], &mix);
1326 mix.type = MIX_ON_OFF;
1327 mix.ctlunit = "";
1328 snprintf(mix.ctlname, sizeof(mix.ctlname), "ext%d-enable",
1329 d->bUnitId);
1330 uaudio_mixer_add_ctl(sc, &mix);
1331 }
1332 }
1333
1334 Static struct terminal_list*
1335 uaudio_merge_terminal_list(const struct io_terminal *iot)
1336 {
1337 struct terminal_list *tml;
1338 uint16_t *ptm;
1339 int i, len;
1340
1341 len = 0;
1342 if (iot->inputs == NULL)
1343 return NULL;
1344 for (i = 0; i < iot->inputs_size; i++) {
1345 if (iot->inputs[i] != NULL)
1346 len += iot->inputs[i]->size;
1347 }
1348 tml = malloc(TERMINAL_LIST_SIZE(len), M_TEMP, M_NOWAIT);
1349 if (tml == NULL) {
1350 aprint_error("uaudio_merge_terminal_list: no memory\n");
1351 return NULL;
1352 }
1353 tml->size = 0;
1354 ptm = tml->terminals;
1355 for (i = 0; i < iot->inputs_size; i++) {
1356 if (iot->inputs[i] == NULL)
1357 continue;
1358 if (iot->inputs[i]->size > len)
1359 break;
1360 memcpy(ptm, iot->inputs[i]->terminals,
1361 iot->inputs[i]->size * sizeof(uint16_t));
1362 tml->size += iot->inputs[i]->size;
1363 ptm += iot->inputs[i]->size;
1364 len -= iot->inputs[i]->size;
1365 }
1366 return tml;
1367 }
1368
1369 Static struct terminal_list *
1370 uaudio_io_terminaltype(int outtype, struct io_terminal *iot, int id)
1371 {
1372 struct terminal_list *tml;
1373 struct io_terminal *it;
1374 int src_id, i;
1375
1376 it = &iot[id];
1377 if (it->output != NULL) {
1378 /* already has outtype? */
1379 for (i = 0; i < it->output->size; i++)
1380 if (it->output->terminals[i] == outtype)
1381 return uaudio_merge_terminal_list(it);
1382 tml = malloc(TERMINAL_LIST_SIZE(it->output->size + 1),
1383 M_TEMP, M_NOWAIT);
1384 if (tml == NULL) {
1385 aprint_error("uaudio_io_terminaltype: no memory\n");
1386 return uaudio_merge_terminal_list(it);
1387 }
1388 memcpy(tml, it->output, TERMINAL_LIST_SIZE(it->output->size));
1389 tml->terminals[it->output->size] = outtype;
1390 tml->size++;
1391 free(it->output, M_TEMP);
1392 it->output = tml;
1393 if (it->inputs != NULL) {
1394 for (i = 0; i < it->inputs_size; i++)
1395 if (it->inputs[i] != NULL)
1396 free(it->inputs[i], M_TEMP);
1397 free(it->inputs, M_TEMP);
1398 }
1399 it->inputs_size = 0;
1400 it->inputs = NULL;
1401 } else { /* end `iot[id] != NULL' */
1402 it->inputs_size = 0;
1403 it->inputs = NULL;
1404 it->output = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1405 if (it->output == NULL) {
1406 aprint_error("uaudio_io_terminaltype: no memory\n");
1407 return NULL;
1408 }
1409 it->output->terminals[0] = outtype;
1410 it->output->size = 1;
1411 it->direct = FALSE;
1412 }
1413
1414 switch (it->d.desc->bDescriptorSubtype) {
1415 case UDESCSUB_AC_INPUT:
1416 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1417 if (it->inputs == NULL) {
1418 aprint_error("uaudio_io_terminaltype: no memory\n");
1419 return NULL;
1420 }
1421 tml = malloc(TERMINAL_LIST_SIZE(1), M_TEMP, M_NOWAIT);
1422 if (tml == NULL) {
1423 aprint_error("uaudio_io_terminaltype: no memory\n");
1424 free(it->inputs, M_TEMP);
1425 it->inputs = NULL;
1426 return NULL;
1427 }
1428 it->inputs[0] = tml;
1429 tml->terminals[0] = UGETW(it->d.it->wTerminalType);
1430 tml->size = 1;
1431 it->inputs_size = 1;
1432 return uaudio_merge_terminal_list(it);
1433 case UDESCSUB_AC_FEATURE:
1434 src_id = it->d.fu->bSourceId;
1435 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1436 if (it->inputs == NULL) {
1437 aprint_error("uaudio_io_terminaltype: no memory\n");
1438 return uaudio_io_terminaltype(outtype, iot, src_id);
1439 }
1440 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1441 it->inputs_size = 1;
1442 return uaudio_merge_terminal_list(it);
1443 case UDESCSUB_AC_OUTPUT:
1444 it->inputs = malloc(sizeof(struct terminal_list *), M_TEMP, M_NOWAIT);
1445 if (it->inputs == NULL) {
1446 aprint_error("uaudio_io_terminaltype: no memory\n");
1447 return NULL;
1448 }
1449 src_id = it->d.ot->bSourceId;
1450 it->inputs[0] = uaudio_io_terminaltype(outtype, iot, src_id);
1451 it->inputs_size = 1;
1452 iot[src_id].direct = TRUE;
1453 return NULL;
1454 case UDESCSUB_AC_MIXER:
1455 it->inputs_size = 0;
1456 it->inputs = malloc(sizeof(struct terminal_list *)
1457 * it->d.mu->bNrInPins, M_TEMP, M_NOWAIT);
1458 if (it->inputs == NULL) {
1459 aprint_error("uaudio_io_terminaltype: no memory\n");
1460 return NULL;
1461 }
1462 for (i = 0; i < it->d.mu->bNrInPins; i++) {
1463 src_id = it->d.mu->baSourceId[i];
1464 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1465 src_id);
1466 it->inputs_size++;
1467 }
1468 return uaudio_merge_terminal_list(it);
1469 case UDESCSUB_AC_SELECTOR:
1470 it->inputs_size = 0;
1471 it->inputs = malloc(sizeof(struct terminal_list *)
1472 * it->d.su->bNrInPins, M_TEMP, M_NOWAIT);
1473 if (it->inputs == NULL) {
1474 aprint_error("uaudio_io_terminaltype: no memory\n");
1475 return NULL;
1476 }
1477 for (i = 0; i < it->d.su->bNrInPins; i++) {
1478 src_id = it->d.su->baSourceId[i];
1479 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1480 src_id);
1481 it->inputs_size++;
1482 }
1483 return uaudio_merge_terminal_list(it);
1484 case UDESCSUB_AC_PROCESSING:
1485 it->inputs_size = 0;
1486 it->inputs = malloc(sizeof(struct terminal_list *)
1487 * it->d.pu->bNrInPins, M_TEMP, M_NOWAIT);
1488 if (it->inputs == NULL) {
1489 aprint_error("uaudio_io_terminaltype: no memory\n");
1490 return NULL;
1491 }
1492 for (i = 0; i < it->d.pu->bNrInPins; i++) {
1493 src_id = it->d.pu->baSourceId[i];
1494 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1495 src_id);
1496 it->inputs_size++;
1497 }
1498 return uaudio_merge_terminal_list(it);
1499 case UDESCSUB_AC_EXTENSION:
1500 it->inputs_size = 0;
1501 it->inputs = malloc(sizeof(struct terminal_list *)
1502 * it->d.eu->bNrInPins, M_TEMP, M_NOWAIT);
1503 if (it->inputs == NULL) {
1504 aprint_error("uaudio_io_terminaltype: no memory\n");
1505 return NULL;
1506 }
1507 for (i = 0; i < it->d.eu->bNrInPins; i++) {
1508 src_id = it->d.eu->baSourceId[i];
1509 it->inputs[i] = uaudio_io_terminaltype(outtype, iot,
1510 src_id);
1511 it->inputs_size++;
1512 }
1513 return uaudio_merge_terminal_list(it);
1514 case UDESCSUB_AC_HEADER:
1515 default:
1516 return NULL;
1517 }
1518 }
1519
1520 Static usbd_status
1521 uaudio_identify(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
1522 {
1523 usbd_status err;
1524
1525 err = uaudio_identify_ac(sc, cdesc);
1526 if (err)
1527 return err;
1528 return uaudio_identify_as(sc, cdesc);
1529 }
1530
1531 Static void
1532 uaudio_add_alt(struct uaudio_softc *sc, const struct as_info *ai)
1533 {
1534 size_t len;
1535 struct as_info *nai;
1536
1537 len = sizeof(*ai) * (sc->sc_nalts + 1);
1538 nai = malloc(len, M_USBDEV, M_NOWAIT);
1539 if (nai == NULL) {
1540 aprint_error("uaudio_add_alt: no memory\n");
1541 return;
1542 }
1543 /* Copy old data, if there was any */
1544 if (sc->sc_nalts != 0) {
1545 memcpy(nai, sc->sc_alts, sizeof(*ai) * (sc->sc_nalts));
1546 free(sc->sc_alts, M_USBDEV);
1547 }
1548 sc->sc_alts = nai;
1549 DPRINTFN(2,"adding alt=%d, enc=%d\n",
1550 ai->alt, ai->encoding);
1551 sc->sc_alts[sc->sc_nalts++] = *ai;
1552 }
1553
1554 Static usbd_status
1555 uaudio_process_as(struct uaudio_softc *sc, const char *tbuf, int *offsp,
1556 int size, const usb_interface_descriptor_t *id)
1557 #define offs (*offsp)
1558 {
1559 const struct usb_audio_streaming_interface_descriptor *asid;
1560 const struct usb_audio_streaming_type1_descriptor *asf1d;
1561 const usb_endpoint_descriptor_audio_t *ed;
1562 const usb_endpoint_descriptor_audio_t *epdesc1;
1563 const struct usb_audio_streaming_endpoint_descriptor *sed;
1564 int format, chan __unused, prec, enc;
1565 int dir, type, sync;
1566 struct as_info ai;
1567 const char *format_str __unused;
1568
1569 asid = (const void *)(tbuf + offs);
1570 if (asid->bDescriptorType != UDESC_CS_INTERFACE ||
1571 asid->bDescriptorSubtype != AS_GENERAL)
1572 return USBD_INVAL;
1573 DPRINTF("asid: bTerminakLink=%d wFormatTag=%d\n",
1574 asid->bTerminalLink, UGETW(asid->wFormatTag));
1575 offs += asid->bLength;
1576 if (offs > size)
1577 return USBD_INVAL;
1578
1579 asf1d = (const void *)(tbuf + offs);
1580 if (asf1d->bDescriptorType != UDESC_CS_INTERFACE ||
1581 asf1d->bDescriptorSubtype != FORMAT_TYPE)
1582 return USBD_INVAL;
1583 offs += asf1d->bLength;
1584 if (offs > size)
1585 return USBD_INVAL;
1586
1587 if (asf1d->bFormatType != FORMAT_TYPE_I) {
1588 aprint_error_dev(sc->sc_dev,
1589 "ignored setting with type %d format\n", UGETW(asid->wFormatTag));
1590 return USBD_NORMAL_COMPLETION;
1591 }
1592
1593 ed = (const void *)(tbuf + offs);
1594 if (ed->bDescriptorType != UDESC_ENDPOINT)
1595 return USBD_INVAL;
1596 DPRINTF("endpoint[0] bLength=%d bDescriptorType=%d "
1597 "bEndpointAddress=%d bmAttributes=0x%x wMaxPacketSize=%d "
1598 "bInterval=%d bRefresh=%d bSynchAddress=%d\n",
1599 ed->bLength, ed->bDescriptorType, ed->bEndpointAddress,
1600 ed->bmAttributes, UGETW(ed->wMaxPacketSize),
1601 ed->bInterval, ed->bRefresh, ed->bSynchAddress);
1602 offs += ed->bLength;
1603 if (offs > size)
1604 return USBD_INVAL;
1605 if (UE_GET_XFERTYPE(ed->bmAttributes) != UE_ISOCHRONOUS)
1606 return USBD_INVAL;
1607
1608 dir = UE_GET_DIR(ed->bEndpointAddress);
1609 type = UE_GET_ISO_TYPE(ed->bmAttributes);
1610 if ((usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_AU_INP_ASYNC) &&
1611 dir == UE_DIR_IN && type == UE_ISO_ADAPT)
1612 type = UE_ISO_ASYNC;
1613
1614 /* We can't handle endpoints that need a sync pipe yet. */
1615 sync = FALSE;
1616 if (dir == UE_DIR_IN && type == UE_ISO_ADAPT) {
1617 sync = TRUE;
1618 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1619 aprint_error_dev(sc->sc_dev,
1620 "ignored input endpoint of type adaptive\n");
1621 return USBD_NORMAL_COMPLETION;
1622 #endif
1623 }
1624 if (dir != UE_DIR_IN && type == UE_ISO_ASYNC) {
1625 sync = TRUE;
1626 #ifndef UAUDIO_MULTIPLE_ENDPOINTS
1627 aprint_error_dev(sc->sc_dev,
1628 "ignored output endpoint of type async\n");
1629 return USBD_NORMAL_COMPLETION;
1630 #endif
1631 }
1632
1633 sed = (const void *)(tbuf + offs);
1634 if (sed->bDescriptorType != UDESC_CS_ENDPOINT ||
1635 sed->bDescriptorSubtype != AS_GENERAL)
1636 return USBD_INVAL;
1637 DPRINTF(" streadming_endpoint: offset=%d bLength=%d\n", offs, sed->bLength);
1638 offs += sed->bLength;
1639 if (offs > size)
1640 return USBD_INVAL;
1641
1642 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
1643 if (sync && id->bNumEndpoints <= 1) {
1644 aprint_error_dev(sc->sc_dev,
1645 "a sync-pipe endpoint but no other endpoint\n");
1646 return USBD_INVAL;
1647 }
1648 #endif
1649 if (!sync && id->bNumEndpoints > 1) {
1650 aprint_error_dev(sc->sc_dev,
1651 "non sync-pipe endpoint but multiple endpoints\n");
1652 return USBD_INVAL;
1653 }
1654 epdesc1 = NULL;
1655 if (id->bNumEndpoints > 1) {
1656 epdesc1 = (const void*)(tbuf + offs);
1657 if (epdesc1->bDescriptorType != UDESC_ENDPOINT)
1658 return USBD_INVAL;
1659 DPRINTF("endpoint[1] bLength=%d "
1660 "bDescriptorType=%d bEndpointAddress=%d "
1661 "bmAttributes=0x%x wMaxPacketSize=%d bInterval=%d "
1662 "bRefresh=%d bSynchAddress=%d\n",
1663 epdesc1->bLength, epdesc1->bDescriptorType,
1664 epdesc1->bEndpointAddress, epdesc1->bmAttributes,
1665 UGETW(epdesc1->wMaxPacketSize), epdesc1->bInterval,
1666 epdesc1->bRefresh, epdesc1->bSynchAddress);
1667 offs += epdesc1->bLength;
1668 if (offs > size)
1669 return USBD_INVAL;
1670 if (epdesc1->bSynchAddress != 0) {
1671 aprint_error_dev(sc->sc_dev,
1672 "invalid endpoint: bSynchAddress=0\n");
1673 return USBD_INVAL;
1674 }
1675 if (UE_GET_XFERTYPE(epdesc1->bmAttributes) != UE_ISOCHRONOUS) {
1676 aprint_error_dev(sc->sc_dev,
1677 "invalid endpoint: bmAttributes=0x%x\n",
1678 epdesc1->bmAttributes);
1679 return USBD_INVAL;
1680 }
1681 if (epdesc1->bEndpointAddress != ed->bSynchAddress) {
1682 aprint_error_dev(sc->sc_dev,
1683 "invalid endpoint addresses: "
1684 "ep[0]->bSynchAddress=0x%x "
1685 "ep[1]->bEndpointAddress=0x%x\n",
1686 ed->bSynchAddress, epdesc1->bEndpointAddress);
1687 return USBD_INVAL;
1688 }
1689 /* UE_GET_ADDR(epdesc1->bEndpointAddress), and epdesc1->bRefresh */
1690 }
1691
1692 format = UGETW(asid->wFormatTag);
1693 chan = asf1d->bNrChannels;
1694 prec = asf1d->bBitResolution;
1695 if (prec != 8 && prec != 16 && prec != 24) {
1696 aprint_error_dev(sc->sc_dev,
1697 "ignored setting with precision %d\n", prec);
1698 return USBD_NORMAL_COMPLETION;
1699 }
1700 switch (format) {
1701 case UA_FMT_PCM:
1702 if (prec == 8) {
1703 sc->sc_altflags |= HAS_8;
1704 } else if (prec == 16) {
1705 sc->sc_altflags |= HAS_16;
1706 } else if (prec == 24) {
1707 sc->sc_altflags |= HAS_24;
1708 }
1709 enc = AUDIO_ENCODING_SLINEAR_LE;
1710 format_str = "pcm";
1711 break;
1712 case UA_FMT_PCM8:
1713 enc = AUDIO_ENCODING_ULINEAR_LE;
1714 sc->sc_altflags |= HAS_8U;
1715 format_str = "pcm8";
1716 break;
1717 case UA_FMT_ALAW:
1718 enc = AUDIO_ENCODING_ALAW;
1719 sc->sc_altflags |= HAS_ALAW;
1720 format_str = "alaw";
1721 break;
1722 case UA_FMT_MULAW:
1723 enc = AUDIO_ENCODING_ULAW;
1724 sc->sc_altflags |= HAS_MULAW;
1725 format_str = "mulaw";
1726 break;
1727 case UA_FMT_IEEE_FLOAT:
1728 default:
1729 aprint_error_dev(sc->sc_dev,
1730 "ignored setting with format %d\n", format);
1731 return USBD_NORMAL_COMPLETION;
1732 }
1733 #ifdef UAUDIO_DEBUG
1734 aprint_debug_dev(sc->sc_dev, "%s: %dch, %d/%dbit, %s,",
1735 dir == UE_DIR_IN ? "recording" : "playback",
1736 chan, prec, asf1d->bSubFrameSize * 8, format_str);
1737 if (asf1d->bSamFreqType == UA_SAMP_CONTNUOUS) {
1738 aprint_debug(" %d-%dHz\n", UA_SAMP_LO(asf1d),
1739 UA_SAMP_HI(asf1d));
1740 } else {
1741 int r;
1742 aprint_debug(" %d", UA_GETSAMP(asf1d, 0));
1743 for (r = 1; r < asf1d->bSamFreqType; r++)
1744 aprint_debug(",%d", UA_GETSAMP(asf1d, r));
1745 aprint_debug("Hz\n");
1746 }
1747 #endif
1748 ai.alt = id->bAlternateSetting;
1749 ai.encoding = enc;
1750 ai.attributes = sed->bmAttributes;
1751 ai.idesc = id;
1752 ai.edesc = ed;
1753 ai.edesc1 = epdesc1;
1754 ai.asf1desc = asf1d;
1755 ai.sc_busy = 0;
1756 ai.aformat = NULL;
1757 ai.ifaceh = NULL;
1758 uaudio_add_alt(sc, &ai);
1759 #ifdef UAUDIO_DEBUG
1760 if (ai.attributes & UA_SED_FREQ_CONTROL)
1761 DPRINTFN(1, "%s", "FREQ_CONTROL\n");
1762 if (ai.attributes & UA_SED_PITCH_CONTROL)
1763 DPRINTFN(1, "%s", "PITCH_CONTROL\n");
1764 #endif
1765 sc->sc_mode |= (dir == UE_DIR_OUT) ? AUMODE_PLAY : AUMODE_RECORD;
1766
1767 return USBD_NORMAL_COMPLETION;
1768 }
1769 #undef offs
1770
1771 Static usbd_status
1772 uaudio_identify_as(struct uaudio_softc *sc,
1773 const usb_config_descriptor_t *cdesc)
1774 {
1775 const usb_interface_descriptor_t *id;
1776 const char *tbuf;
1777 struct audio_format *auf;
1778 const struct usb_audio_streaming_type1_descriptor *t1desc;
1779 int size, offs;
1780 int i, j;
1781
1782 size = UGETW(cdesc->wTotalLength);
1783 tbuf = (const char *)cdesc;
1784
1785 /* Locate the AudioStreaming interface descriptor. */
1786 offs = 0;
1787 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOSTREAM);
1788 if (id == NULL)
1789 return USBD_INVAL;
1790
1791 /* Loop through all the alternate settings. */
1792 while (offs <= size) {
1793 DPRINTFN(2, "interface=%d offset=%d\n",
1794 id->bInterfaceNumber, offs);
1795 switch (id->bNumEndpoints) {
1796 case 0:
1797 DPRINTFN(2, "AS null alt=%d\n",
1798 id->bAlternateSetting);
1799 sc->sc_nullalt = id->bAlternateSetting;
1800 break;
1801 case 1:
1802 #ifdef UAUDIO_MULTIPLE_ENDPOINTS
1803 case 2:
1804 #endif
1805 uaudio_process_as(sc, tbuf, &offs, size, id);
1806 break;
1807 default:
1808 aprint_error_dev(sc->sc_dev,
1809 "ignored audio interface with %d endpoints\n",
1810 id->bNumEndpoints);
1811 break;
1812 }
1813 id = uaudio_find_iface(tbuf, size, &offs,UISUBCLASS_AUDIOSTREAM);
1814 if (id == NULL)
1815 break;
1816 }
1817 if (offs > size)
1818 return USBD_INVAL;
1819 DPRINTF("%d alts available\n", sc->sc_nalts);
1820
1821 if (sc->sc_mode == 0) {
1822 aprint_error_dev(sc->sc_dev, "no usable endpoint found\n");
1823 return USBD_INVAL;
1824 }
1825
1826 /* build audio_format array */
1827 sc->sc_formats = malloc(sizeof(struct audio_format) * sc->sc_nalts,
1828 M_USBDEV, M_NOWAIT);
1829 if (sc->sc_formats == NULL)
1830 return USBD_NOMEM;
1831 sc->sc_nformats = sc->sc_nalts;
1832 for (i = 0; i < sc->sc_nalts; i++) {
1833 auf = &sc->sc_formats[i];
1834 t1desc = sc->sc_alts[i].asf1desc;
1835 auf->driver_data = NULL;
1836 if (UE_GET_DIR(sc->sc_alts[i].edesc->bEndpointAddress) == UE_DIR_OUT)
1837 auf->mode = AUMODE_PLAY;
1838 else
1839 auf->mode = AUMODE_RECORD;
1840 auf->encoding = sc->sc_alts[i].encoding;
1841 auf->validbits = t1desc->bBitResolution;
1842 auf->precision = t1desc->bSubFrameSize * 8;
1843 auf->channels = t1desc->bNrChannels;
1844 auf->channel_mask = sc->sc_channel_config;
1845 auf->frequency_type = t1desc->bSamFreqType;
1846 if (t1desc->bSamFreqType == UA_SAMP_CONTNUOUS) {
1847 auf->frequency[0] = UA_SAMP_LO(t1desc);
1848 auf->frequency[1] = UA_SAMP_HI(t1desc);
1849 } else {
1850 for (j = 0; j < t1desc->bSamFreqType; j++) {
1851 if (j >= AUFMT_MAX_FREQUENCIES) {
1852 aprint_error("%s: please increase "
1853 "AUFMT_MAX_FREQUENCIES to %d\n",
1854 __func__, t1desc->bSamFreqType);
1855 auf->frequency_type =
1856 AUFMT_MAX_FREQUENCIES;
1857 break;
1858 }
1859 auf->frequency[j] = UA_GETSAMP(t1desc, j);
1860 }
1861 }
1862 sc->sc_alts[i].aformat = auf;
1863 }
1864
1865 if (0 != auconv_create_encodings(sc->sc_formats, sc->sc_nformats,
1866 &sc->sc_encodings)) {
1867 free(sc->sc_formats, M_DEVBUF);
1868 sc->sc_formats = NULL;
1869 return ENOMEM;
1870 }
1871
1872 return USBD_NORMAL_COMPLETION;
1873 }
1874
1875 Static usbd_status
1876 uaudio_identify_ac(struct uaudio_softc *sc, const usb_config_descriptor_t *cdesc)
1877 {
1878 struct io_terminal* iot;
1879 const usb_interface_descriptor_t *id;
1880 const struct usb_audio_control_descriptor *acdp;
1881 const uaudio_cs_descriptor_t *dp;
1882 const struct usb_audio_output_terminal *pot;
1883 struct terminal_list *tml;
1884 const char *tbuf, *ibuf, *ibufend;
1885 int size, offs, ndps, i, j;
1886
1887 size = UGETW(cdesc->wTotalLength);
1888 tbuf = (const char *)cdesc;
1889
1890 /* Locate the AudioControl interface descriptor. */
1891 offs = 0;
1892 id = uaudio_find_iface(tbuf, size, &offs, UISUBCLASS_AUDIOCONTROL);
1893 if (id == NULL)
1894 return USBD_INVAL;
1895 if (offs + sizeof *acdp > size)
1896 return USBD_INVAL;
1897 sc->sc_ac_iface = id->bInterfaceNumber;
1898 DPRINTFN(2,"AC interface is %d\n", sc->sc_ac_iface);
1899
1900 /* A class-specific AC interface header should follow. */
1901 ibuf = tbuf + offs;
1902 ibufend = tbuf + size;
1903 acdp = (const struct usb_audio_control_descriptor *)ibuf;
1904 if (acdp->bDescriptorType != UDESC_CS_INTERFACE ||
1905 acdp->bDescriptorSubtype != UDESCSUB_AC_HEADER)
1906 return USBD_INVAL;
1907
1908 if (!(usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_BAD_ADC) &&
1909 UGETW(acdp->bcdADC) != UAUDIO_VERSION)
1910 return USBD_INVAL;
1911
1912 sc->sc_audio_rev = UGETW(acdp->bcdADC);
1913 DPRINTFN(2, "found AC header, vers=%03x\n", sc->sc_audio_rev);
1914
1915 sc->sc_nullalt = -1;
1916
1917 /* Scan through all the AC specific descriptors */
1918 dp = (const uaudio_cs_descriptor_t *)ibuf;
1919 ndps = 0;
1920 iot = malloc(sizeof(struct io_terminal) * 256, M_TEMP, M_NOWAIT | M_ZERO);
1921 if (iot == NULL) {
1922 aprint_error("%s: no memory\n", __func__);
1923 return USBD_NOMEM;
1924 }
1925 for (;;) {
1926 ibuf += dp->bLength;
1927 if (ibuf >= ibufend)
1928 break;
1929 dp = (const uaudio_cs_descriptor_t *)ibuf;
1930 if (ibuf + dp->bLength > ibufend) {
1931 free(iot, M_TEMP);
1932 return USBD_INVAL;
1933 }
1934 if (dp->bDescriptorType != UDESC_CS_INTERFACE)
1935 break;
1936 i = ((const struct usb_audio_input_terminal *)dp)->bTerminalId;
1937 iot[i].d.desc = dp;
1938 if (i > ndps)
1939 ndps = i;
1940 }
1941 ndps++;
1942
1943 /* construct io_terminal */
1944 for (i = 0; i < ndps; i++) {
1945 dp = iot[i].d.desc;
1946 if (dp == NULL)
1947 continue;
1948 if (dp->bDescriptorSubtype != UDESCSUB_AC_OUTPUT)
1949 continue;
1950 pot = iot[i].d.ot;
1951 tml = uaudio_io_terminaltype(UGETW(pot->wTerminalType), iot, i);
1952 if (tml != NULL)
1953 free(tml, M_TEMP);
1954 }
1955
1956 #ifdef UAUDIO_DEBUG
1957 for (i = 0; i < 256; i++) {
1958 struct usb_audio_cluster cluster;
1959
1960 if (iot[i].d.desc == NULL)
1961 continue;
1962 printf("id %d:\t", i);
1963 switch (iot[i].d.desc->bDescriptorSubtype) {
1964 case UDESCSUB_AC_INPUT:
1965 printf("AC_INPUT type=%s\n", uaudio_get_terminal_name
1966 (UGETW(iot[i].d.it->wTerminalType)));
1967 printf("\t");
1968 cluster = uaudio_get_cluster(i, iot);
1969 uaudio_dump_cluster(&cluster);
1970 printf("\n");
1971 break;
1972 case UDESCSUB_AC_OUTPUT:
1973 printf("AC_OUTPUT type=%s ", uaudio_get_terminal_name
1974 (UGETW(iot[i].d.ot->wTerminalType)));
1975 printf("src=%d\n", iot[i].d.ot->bSourceId);
1976 break;
1977 case UDESCSUB_AC_MIXER:
1978 printf("AC_MIXER src=");
1979 for (j = 0; j < iot[i].d.mu->bNrInPins; j++)
1980 printf("%d ", iot[i].d.mu->baSourceId[j]);
1981 printf("\n\t");
1982 cluster = uaudio_get_cluster(i, iot);
1983 uaudio_dump_cluster(&cluster);
1984 printf("\n");
1985 break;
1986 case UDESCSUB_AC_SELECTOR:
1987 printf("AC_SELECTOR src=");
1988 for (j = 0; j < iot[i].d.su->bNrInPins; j++)
1989 printf("%d ", iot[i].d.su->baSourceId[j]);
1990 printf("\n");
1991 break;
1992 case UDESCSUB_AC_FEATURE:
1993 printf("AC_FEATURE src=%d\n", iot[i].d.fu->bSourceId);
1994 break;
1995 case UDESCSUB_AC_PROCESSING:
1996 printf("AC_PROCESSING src=");
1997 for (j = 0; j < iot[i].d.pu->bNrInPins; j++)
1998 printf("%d ", iot[i].d.pu->baSourceId[j]);
1999 printf("\n\t");
2000 cluster = uaudio_get_cluster(i, iot);
2001 uaudio_dump_cluster(&cluster);
2002 printf("\n");
2003 break;
2004 case UDESCSUB_AC_EXTENSION:
2005 printf("AC_EXTENSION src=");
2006 for (j = 0; j < iot[i].d.eu->bNrInPins; j++)
2007 printf("%d ", iot[i].d.eu->baSourceId[j]);
2008 printf("\n\t");
2009 cluster = uaudio_get_cluster(i, iot);
2010 uaudio_dump_cluster(&cluster);
2011 printf("\n");
2012 break;
2013 default:
2014 printf("unknown audio control (subtype=%d)\n",
2015 iot[i].d.desc->bDescriptorSubtype);
2016 }
2017 for (j = 0; j < iot[i].inputs_size; j++) {
2018 int k;
2019 printf("\tinput%d: ", j);
2020 tml = iot[i].inputs[j];
2021 if (tml == NULL) {
2022 printf("NULL\n");
2023 continue;
2024 }
2025 for (k = 0; k < tml->size; k++)
2026 printf("%s ", uaudio_get_terminal_name
2027 (tml->terminals[k]));
2028 printf("\n");
2029 }
2030 printf("\toutput: ");
2031 tml = iot[i].output;
2032 for (j = 0; j < tml->size; j++)
2033 printf("%s ", uaudio_get_terminal_name(tml->terminals[j]));
2034 printf("\n");
2035 }
2036 #endif
2037
2038 for (i = 0; i < ndps; i++) {
2039 dp = iot[i].d.desc;
2040 if (dp == NULL)
2041 continue;
2042 DPRINTF("id=%d subtype=%d\n", i, dp->bDescriptorSubtype);
2043 switch (dp->bDescriptorSubtype) {
2044 case UDESCSUB_AC_HEADER:
2045 aprint_error("uaudio_identify_ac: unexpected AC header\n");
2046 break;
2047 case UDESCSUB_AC_INPUT:
2048 uaudio_add_input(sc, iot, i);
2049 break;
2050 case UDESCSUB_AC_OUTPUT:
2051 uaudio_add_output(sc, iot, i);
2052 break;
2053 case UDESCSUB_AC_MIXER:
2054 uaudio_add_mixer(sc, iot, i);
2055 break;
2056 case UDESCSUB_AC_SELECTOR:
2057 uaudio_add_selector(sc, iot, i);
2058 break;
2059 case UDESCSUB_AC_FEATURE:
2060 uaudio_add_feature(sc, iot, i);
2061 break;
2062 case UDESCSUB_AC_PROCESSING:
2063 uaudio_add_processing(sc, iot, i);
2064 break;
2065 case UDESCSUB_AC_EXTENSION:
2066 uaudio_add_extension(sc, iot, i);
2067 break;
2068 default:
2069 aprint_error(
2070 "uaudio_identify_ac: bad AC desc subtype=0x%02x\n",
2071 dp->bDescriptorSubtype);
2072 break;
2073 }
2074 }
2075
2076 /* delete io_terminal */
2077 for (i = 0; i < 256; i++) {
2078 if (iot[i].d.desc == NULL)
2079 continue;
2080 if (iot[i].inputs != NULL) {
2081 for (j = 0; j < iot[i].inputs_size; j++) {
2082 if (iot[i].inputs[j] != NULL)
2083 free(iot[i].inputs[j], M_TEMP);
2084 }
2085 free(iot[i].inputs, M_TEMP);
2086 }
2087 if (iot[i].output != NULL)
2088 free(iot[i].output, M_TEMP);
2089 iot[i].d.desc = NULL;
2090 }
2091 free(iot, M_TEMP);
2092
2093 return USBD_NORMAL_COMPLETION;
2094 }
2095
2096 Static int
2097 uaudio_query_devinfo(void *addr, mixer_devinfo_t *mi)
2098 {
2099 struct uaudio_softc *sc;
2100 struct mixerctl *mc;
2101 int n, nctls, i;
2102
2103 DPRINTFN(7, "index=%d\n", mi->index);
2104 sc = addr;
2105 if (sc->sc_dying)
2106 return EIO;
2107
2108 n = mi->index;
2109 nctls = sc->sc_nctls;
2110
2111 switch (n) {
2112 case UAC_OUTPUT:
2113 mi->type = AUDIO_MIXER_CLASS;
2114 mi->mixer_class = UAC_OUTPUT;
2115 mi->next = mi->prev = AUDIO_MIXER_LAST;
2116 strlcpy(mi->label.name, AudioCoutputs, sizeof(mi->label.name));
2117 return 0;
2118 case UAC_INPUT:
2119 mi->type = AUDIO_MIXER_CLASS;
2120 mi->mixer_class = UAC_INPUT;
2121 mi->next = mi->prev = AUDIO_MIXER_LAST;
2122 strlcpy(mi->label.name, AudioCinputs, sizeof(mi->label.name));
2123 return 0;
2124 case UAC_EQUAL:
2125 mi->type = AUDIO_MIXER_CLASS;
2126 mi->mixer_class = UAC_EQUAL;
2127 mi->next = mi->prev = AUDIO_MIXER_LAST;
2128 strlcpy(mi->label.name, AudioCequalization,
2129 sizeof(mi->label.name));
2130 return 0;
2131 case UAC_RECORD:
2132 mi->type = AUDIO_MIXER_CLASS;
2133 mi->mixer_class = UAC_RECORD;
2134 mi->next = mi->prev = AUDIO_MIXER_LAST;
2135 strlcpy(mi->label.name, AudioCrecord, sizeof(mi->label.name));
2136 return 0;
2137 default:
2138 break;
2139 }
2140
2141 n -= UAC_NCLASSES;
2142 if (n < 0 || n >= nctls)
2143 return ENXIO;
2144
2145 mc = &sc->sc_ctls[n];
2146 strlcpy(mi->label.name, mc->ctlname, sizeof(mi->label.name));
2147 mi->mixer_class = mc->class;
2148 mi->next = mi->prev = AUDIO_MIXER_LAST; /* XXX */
2149 switch (mc->type) {
2150 case MIX_ON_OFF:
2151 mi->type = AUDIO_MIXER_ENUM;
2152 mi->un.e.num_mem = 2;
2153 strlcpy(mi->un.e.member[0].label.name, AudioNoff,
2154 sizeof(mi->un.e.member[0].label.name));
2155 mi->un.e.member[0].ord = 0;
2156 strlcpy(mi->un.e.member[1].label.name, AudioNon,
2157 sizeof(mi->un.e.member[1].label.name));
2158 mi->un.e.member[1].ord = 1;
2159 break;
2160 case MIX_SELECTOR:
2161 mi->type = AUDIO_MIXER_ENUM;
2162 mi->un.e.num_mem = mc->maxval - mc->minval + 1;
2163 for (i = 0; i <= mc->maxval - mc->minval; i++) {
2164 snprintf(mi->un.e.member[i].label.name,
2165 sizeof(mi->un.e.member[i].label.name),
2166 "%d", i + mc->minval);
2167 mi->un.e.member[i].ord = i + mc->minval;
2168 }
2169 break;
2170 default:
2171 mi->type = AUDIO_MIXER_VALUE;
2172 strncpy(mi->un.v.units.name, mc->ctlunit, MAX_AUDIO_DEV_LEN);
2173 mi->un.v.num_channels = mc->nchan;
2174 mi->un.v.delta = mc->delta;
2175 break;
2176 }
2177 return 0;
2178 }
2179
2180 Static int
2181 uaudio_open(void *addr, int flags)
2182 {
2183 struct uaudio_softc *sc;
2184
2185 sc = addr;
2186 DPRINTF("sc=%p\n", sc);
2187 if (sc->sc_dying)
2188 return EIO;
2189
2190 if ((flags & FWRITE) && !(sc->sc_mode & AUMODE_PLAY))
2191 return EACCES;
2192 if ((flags & FREAD) && !(sc->sc_mode & AUMODE_RECORD))
2193 return EACCES;
2194
2195 return 0;
2196 }
2197
2198 /*
2199 * Close function is called at splaudio().
2200 */
2201 Static void
2202 uaudio_close(void *addr)
2203 {
2204 }
2205
2206 Static int
2207 uaudio_drain(void *addr)
2208 {
2209 struct uaudio_softc *sc = addr;
2210
2211 KASSERT(mutex_owned(&sc->sc_intr_lock));
2212
2213 kpause("uaudiodr", false,
2214 mstohz(UAUDIO_NCHANBUFS * UAUDIO_NFRAMES), &sc->sc_intr_lock);
2215
2216 return 0;
2217 }
2218
2219 Static int
2220 uaudio_halt_out_dma(void *addr)
2221 {
2222 struct uaudio_softc *sc = addr;
2223
2224 DPRINTF("%s", "enter\n");
2225
2226 mutex_spin_exit(&sc->sc_intr_lock);
2227 if (sc->sc_playchan.pipe != NULL) {
2228 uaudio_chan_close(sc, &sc->sc_playchan);
2229 uaudio_chan_free_buffers(sc, &sc->sc_playchan);
2230 sc->sc_playchan.intr = NULL;
2231 }
2232 mutex_spin_enter(&sc->sc_intr_lock);
2233
2234 return 0;
2235 }
2236
2237 Static int
2238 uaudio_halt_in_dma(void *addr)
2239 {
2240 struct uaudio_softc *sc = addr;
2241
2242 DPRINTF("%s", "enter\n");
2243
2244 mutex_spin_exit(&sc->sc_intr_lock);
2245 if (sc->sc_recchan.pipe != NULL) {
2246 uaudio_chan_close(sc, &sc->sc_recchan);
2247 uaudio_chan_free_buffers(sc, &sc->sc_recchan);
2248 sc->sc_recchan.intr = NULL;
2249 }
2250 mutex_spin_enter(&sc->sc_intr_lock);
2251
2252 return 0;
2253 }
2254
2255 Static int
2256 uaudio_getdev(void *addr, struct audio_device *retp)
2257 {
2258 struct uaudio_softc *sc;
2259
2260 DPRINTF("%s", "\n");
2261 sc = addr;
2262 if (sc->sc_dying)
2263 return EIO;
2264
2265 *retp = sc->sc_adev;
2266 return 0;
2267 }
2268
2269 /*
2270 * Make sure the block size is large enough to hold all outstanding transfers.
2271 */
2272 Static int
2273 uaudio_round_blocksize(void *addr, int blk,
2274 int mode, const audio_params_t *param)
2275 {
2276 struct uaudio_softc *sc;
2277 int b;
2278
2279 sc = addr;
2280 DPRINTF("blk=%d mode=%s\n", blk,
2281 mode == AUMODE_PLAY ? "AUMODE_PLAY" : "AUMODE_RECORD");
2282
2283 /* chan.bytes_per_frame can be 0. */
2284 if (mode == AUMODE_PLAY || sc->sc_recchan.bytes_per_frame <= 0) {
2285 b = param->sample_rate * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2286
2287 /*
2288 * This does not make accurate value in the case
2289 * of b % USB_FRAMES_PER_SECOND != 0
2290 */
2291 b /= USB_FRAMES_PER_SECOND;
2292
2293 b *= param->precision / 8 * param->channels;
2294 } else {
2295 /*
2296 * use wMaxPacketSize in bytes_per_frame.
2297 * See uaudio_set_params() and uaudio_chan_init()
2298 */
2299 b = sc->sc_recchan.bytes_per_frame
2300 * UAUDIO_NFRAMES * UAUDIO_NCHANBUFS;
2301 }
2302
2303 if (b <= 0)
2304 b = 1;
2305 blk = blk <= b ? b : blk / b * b;
2306
2307 #ifdef DIAGNOSTIC
2308 if (blk <= 0) {
2309 aprint_debug("uaudio_round_blocksize: blk=%d\n", blk);
2310 blk = 512;
2311 }
2312 #endif
2313
2314 DPRINTF("resultant blk=%d\n", blk);
2315 return blk;
2316 }
2317
2318 Static int
2319 uaudio_get_props(void *addr)
2320 {
2321 return AUDIO_PROP_FULLDUPLEX | AUDIO_PROP_INDEPENDENT;
2322
2323 }
2324
2325 Static void
2326 uaudio_get_locks(void *addr, kmutex_t **intr, kmutex_t **thread)
2327 {
2328 struct uaudio_softc *sc;
2329
2330 sc = addr;
2331 *intr = &sc->sc_intr_lock;
2332 *thread = &sc->sc_lock;
2333 }
2334
2335 Static int
2336 uaudio_get(struct uaudio_softc *sc, int which, int type, int wValue,
2337 int wIndex, int len)
2338 {
2339 usb_device_request_t req;
2340 u_int8_t data[4];
2341 usbd_status err;
2342 int val;
2343
2344 if (wValue == -1)
2345 return 0;
2346
2347 req.bmRequestType = type;
2348 req.bRequest = which;
2349 USETW(req.wValue, wValue);
2350 USETW(req.wIndex, wIndex);
2351 USETW(req.wLength, len);
2352 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
2353 "wIndex=0x%04x len=%d\n",
2354 type, which, wValue, wIndex, len);
2355 err = usbd_do_request(sc->sc_udev, &req, data);
2356 if (err) {
2357 DPRINTF("err=%s\n", usbd_errstr(err));
2358 return -1;
2359 }
2360 switch (len) {
2361 case 1:
2362 val = data[0];
2363 break;
2364 case 2:
2365 val = data[0] | (data[1] << 8);
2366 break;
2367 default:
2368 DPRINTF("bad length=%d\n", len);
2369 return -1;
2370 }
2371 DPRINTFN(2,"val=%d\n", val);
2372 return val;
2373 }
2374
2375 Static void
2376 uaudio_set(struct uaudio_softc *sc, int which, int type, int wValue,
2377 int wIndex, int len, int val)
2378 {
2379 usb_device_request_t req;
2380 u_int8_t data[4];
2381 int err __unused;
2382
2383 if (wValue == -1)
2384 return;
2385
2386 req.bmRequestType = type;
2387 req.bRequest = which;
2388 USETW(req.wValue, wValue);
2389 USETW(req.wIndex, wIndex);
2390 USETW(req.wLength, len);
2391 switch (len) {
2392 case 1:
2393 data[0] = val;
2394 break;
2395 case 2:
2396 data[0] = val;
2397 data[1] = val >> 8;
2398 break;
2399 default:
2400 return;
2401 }
2402 DPRINTFN(2,"type=0x%02x req=0x%02x wValue=0x%04x "
2403 "wIndex=0x%04x len=%d, val=%d\n",
2404 type, which, wValue, wIndex, len, val & 0xffff);
2405 err = usbd_do_request(sc->sc_udev, &req, data);
2406 #ifdef UAUDIO_DEBUG
2407 if (err)
2408 DPRINTF("err=%d\n", err);
2409 #endif
2410 }
2411
2412 Static int
2413 uaudio_signext(int type, int val)
2414 {
2415 if (!MIX_UNSIGNED(type)) {
2416 if (MIX_SIZE(type) == 2)
2417 val = (int16_t)val;
2418 else
2419 val = (int8_t)val;
2420 }
2421 return val;
2422 }
2423
2424 Static int
2425 uaudio_value2bsd(struct mixerctl *mc, int val)
2426 {
2427 DPRINTFN(5, "type=%03x val=%d min=%d max=%d ",
2428 mc->type, val, mc->minval, mc->maxval);
2429 if (mc->type == MIX_ON_OFF) {
2430 val = (val != 0);
2431 } else if (mc->type == MIX_SELECTOR) {
2432 if (val < mc->minval || val > mc->maxval)
2433 val = mc->minval;
2434 } else
2435 val = ((uaudio_signext(mc->type, val) - mc->minval) * 255
2436 + mc->mul/2) / mc->mul;
2437 DPRINTFN_CLEAN(5, "val'=%d\n", val);
2438 return val;
2439 }
2440
2441 int
2442 uaudio_bsd2value(struct mixerctl *mc, int val)
2443 {
2444 DPRINTFN(5,"type=%03x val=%d min=%d max=%d ",
2445 mc->type, val, mc->minval, mc->maxval);
2446 if (mc->type == MIX_ON_OFF) {
2447 val = (val != 0);
2448 } else if (mc->type == MIX_SELECTOR) {
2449 if (val < mc->minval || val > mc->maxval)
2450 val = mc->minval;
2451 } else
2452 val = (val + mc->delta/2) * mc->mul / 255 + mc->minval;
2453 DPRINTFN_CLEAN(5, "val'=%d\n", val);
2454 return val;
2455 }
2456
2457 Static int
2458 uaudio_ctl_get(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2459 int chan)
2460 {
2461 int val;
2462
2463 DPRINTFN(5,"which=%d chan=%d\n", which, chan);
2464 mutex_exit(&sc->sc_lock);
2465 val = uaudio_get(sc, which, UT_READ_CLASS_INTERFACE, mc->wValue[chan],
2466 mc->wIndex, MIX_SIZE(mc->type));
2467 mutex_enter(&sc->sc_lock);
2468 return uaudio_value2bsd(mc, val);
2469 }
2470
2471 Static void
2472 uaudio_ctl_set(struct uaudio_softc *sc, int which, struct mixerctl *mc,
2473 int chan, int val)
2474 {
2475
2476 val = uaudio_bsd2value(mc, val);
2477 mutex_exit(&sc->sc_lock);
2478 uaudio_set(sc, which, UT_WRITE_CLASS_INTERFACE, mc->wValue[chan],
2479 mc->wIndex, MIX_SIZE(mc->type), val);
2480 mutex_enter(&sc->sc_lock);
2481 }
2482
2483 Static int
2484 uaudio_mixer_get_port(void *addr, mixer_ctrl_t *cp)
2485 {
2486 struct uaudio_softc *sc;
2487 struct mixerctl *mc;
2488 int i, n, vals[MIX_MAX_CHAN], val;
2489
2490 DPRINTFN(2, "index=%d\n", cp->dev);
2491 sc = addr;
2492 if (sc->sc_dying)
2493 return EIO;
2494
2495 n = cp->dev - UAC_NCLASSES;
2496 if (n < 0 || n >= sc->sc_nctls)
2497 return ENXIO;
2498 mc = &sc->sc_ctls[n];
2499
2500 if (mc->type == MIX_ON_OFF) {
2501 if (cp->type != AUDIO_MIXER_ENUM)
2502 return EINVAL;
2503 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2504 } else if (mc->type == MIX_SELECTOR) {
2505 if (cp->type != AUDIO_MIXER_ENUM)
2506 return EINVAL;
2507 cp->un.ord = uaudio_ctl_get(sc, GET_CUR, mc, 0);
2508 } else {
2509 if (cp->type != AUDIO_MIXER_VALUE)
2510 return EINVAL;
2511 if (cp->un.value.num_channels != 1 &&
2512 cp->un.value.num_channels != mc->nchan)
2513 return EINVAL;
2514 for (i = 0; i < mc->nchan; i++)
2515 vals[i] = uaudio_ctl_get(sc, GET_CUR, mc, i);
2516 if (cp->un.value.num_channels == 1 && mc->nchan != 1) {
2517 for (val = 0, i = 0; i < mc->nchan; i++)
2518 val += vals[i];
2519 vals[0] = val / mc->nchan;
2520 }
2521 for (i = 0; i < cp->un.value.num_channels; i++)
2522 cp->un.value.level[i] = vals[i];
2523 }
2524
2525 return 0;
2526 }
2527
2528 Static int
2529 uaudio_mixer_set_port(void *addr, mixer_ctrl_t *cp)
2530 {
2531 struct uaudio_softc *sc;
2532 struct mixerctl *mc;
2533 int i, n, vals[MIX_MAX_CHAN];
2534
2535 DPRINTFN(2, "index = %d\n", cp->dev);
2536 sc = addr;
2537 if (sc->sc_dying)
2538 return EIO;
2539
2540 n = cp->dev - UAC_NCLASSES;
2541 if (n < 0 || n >= sc->sc_nctls)
2542 return ENXIO;
2543 mc = &sc->sc_ctls[n];
2544
2545 if (mc->type == MIX_ON_OFF) {
2546 if (cp->type != AUDIO_MIXER_ENUM)
2547 return EINVAL;
2548 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2549 } else if (mc->type == MIX_SELECTOR) {
2550 if (cp->type != AUDIO_MIXER_ENUM)
2551 return EINVAL;
2552 uaudio_ctl_set(sc, SET_CUR, mc, 0, cp->un.ord);
2553 } else {
2554 if (cp->type != AUDIO_MIXER_VALUE)
2555 return EINVAL;
2556 if (cp->un.value.num_channels == 1)
2557 for (i = 0; i < mc->nchan; i++)
2558 vals[i] = cp->un.value.level[0];
2559 else if (cp->un.value.num_channels == mc->nchan)
2560 for (i = 0; i < mc->nchan; i++)
2561 vals[i] = cp->un.value.level[i];
2562 else
2563 return EINVAL;
2564 for (i = 0; i < mc->nchan; i++)
2565 uaudio_ctl_set(sc, SET_CUR, mc, i, vals[i]);
2566 }
2567 return 0;
2568 }
2569
2570 Static int
2571 uaudio_trigger_input(void *addr, void *start, void *end, int blksize,
2572 void (*intr)(void *), void *arg,
2573 const audio_params_t *param)
2574 {
2575 struct uaudio_softc *sc;
2576 struct chan *ch;
2577 usbd_status err;
2578 int i;
2579
2580 sc = addr;
2581 if (sc->sc_dying)
2582 return EIO;
2583
2584 DPRINTFN(3, "sc=%p start=%p end=%p "
2585 "blksize=%d\n", sc, start, end, blksize);
2586 ch = &sc->sc_recchan;
2587 uaudio_chan_set_param(ch, start, end, blksize);
2588 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
2589 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2590 ch->fraction);
2591
2592 mutex_spin_exit(&sc->sc_intr_lock);
2593 err = uaudio_chan_alloc_buffers(sc, ch);
2594 if (err) {
2595 mutex_spin_enter(&sc->sc_intr_lock);
2596 return EIO;
2597 }
2598
2599 err = uaudio_chan_open(sc, ch);
2600 if (err) {
2601 uaudio_chan_free_buffers(sc, ch);
2602 mutex_spin_enter(&sc->sc_intr_lock);
2603 return EIO;
2604 }
2605
2606 ch->intr = intr;
2607 ch->arg = arg;
2608
2609 for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX -1 shouldn't be needed */
2610 uaudio_chan_rtransfer(ch);
2611 mutex_spin_enter(&sc->sc_intr_lock);
2612
2613 return 0;
2614 }
2615
2616 Static int
2617 uaudio_trigger_output(void *addr, void *start, void *end, int blksize,
2618 void (*intr)(void *), void *arg,
2619 const audio_params_t *param)
2620 {
2621 struct uaudio_softc *sc;
2622 struct chan *ch;
2623 usbd_status err;
2624 int i;
2625
2626 sc = addr;
2627 if (sc->sc_dying)
2628 return EIO;
2629
2630 DPRINTFN(3, "sc=%p start=%p end=%p "
2631 "blksize=%d\n", sc, start, end, blksize);
2632 ch = &sc->sc_playchan;
2633 uaudio_chan_set_param(ch, start, end, blksize);
2634 DPRINTFN(3, "sample_size=%d bytes/frame=%d "
2635 "fraction=0.%03d\n", ch->sample_size, ch->bytes_per_frame,
2636 ch->fraction);
2637
2638 mutex_spin_exit(&sc->sc_intr_lock);
2639 err = uaudio_chan_alloc_buffers(sc, ch);
2640 if (err) {
2641 mutex_spin_enter(&sc->sc_intr_lock);
2642 return EIO;
2643 }
2644
2645 err = uaudio_chan_open(sc, ch);
2646 if (err) {
2647 uaudio_chan_free_buffers(sc, ch);
2648 mutex_spin_enter(&sc->sc_intr_lock);
2649 return EIO;
2650 }
2651
2652 ch->intr = intr;
2653 ch->arg = arg;
2654
2655 for (i = 0; i < UAUDIO_NCHANBUFS-1; i++) /* XXX */
2656 uaudio_chan_ptransfer(ch);
2657 mutex_spin_enter(&sc->sc_intr_lock);
2658
2659 return 0;
2660 }
2661
2662 /* Set up a pipe for a channel. */
2663 Static usbd_status
2664 uaudio_chan_open(struct uaudio_softc *sc, struct chan *ch)
2665 {
2666 struct as_info *as;
2667 usb_device_descriptor_t *ddesc;
2668 int endpt;
2669 usbd_status err;
2670
2671 as = &sc->sc_alts[ch->altidx];
2672 endpt = as->edesc->bEndpointAddress;
2673 DPRINTF("endpt=0x%02x, speed=%d, alt=%d\n",
2674 endpt, ch->sample_rate, as->alt);
2675
2676 /* Set alternate interface corresponding to the mode. */
2677 err = usbd_set_interface(as->ifaceh, as->alt);
2678 if (err)
2679 return err;
2680
2681 /*
2682 * Roland SD-90 freezes by a SAMPLING_FREQ_CONTROL request.
2683 */
2684 ddesc = usbd_get_device_descriptor(sc->sc_udev);
2685 if ((UGETW(ddesc->idVendor) != USB_VENDOR_ROLAND) &&
2686 (UGETW(ddesc->idProduct) != USB_PRODUCT_ROLAND_SD90)) {
2687 err = uaudio_set_speed(sc, endpt, ch->sample_rate);
2688 if (err) {
2689 DPRINTF("set_speed failed err=%s\n", usbd_errstr(err));
2690 }
2691 }
2692
2693 DPRINTF("create pipe to 0x%02x\n", endpt);
2694 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE, &ch->pipe);
2695 if (err)
2696 return err;
2697 if (as->edesc1 != NULL) {
2698 endpt = as->edesc1->bEndpointAddress;
2699 DPRINTF("create sync-pipe to 0x%02x\n", endpt);
2700 err = usbd_open_pipe(as->ifaceh, endpt, USBD_MPSAFE,
2701 &ch->sync_pipe);
2702 }
2703 return err;
2704 }
2705
2706 Static void
2707 uaudio_chan_close(struct uaudio_softc *sc, struct chan *ch)
2708 {
2709 usbd_pipe_handle pipe;
2710 struct as_info *as;
2711
2712 as = &sc->sc_alts[ch->altidx];
2713 as->sc_busy = 0;
2714 AUFMT_VALIDATE(as->aformat);
2715 if (sc->sc_nullalt >= 0) {
2716 DPRINTF("set null alt=%d\n", sc->sc_nullalt);
2717 usbd_set_interface(as->ifaceh, sc->sc_nullalt);
2718 }
2719 pipe = atomic_swap_ptr(&ch->pipe, NULL);
2720 if (pipe) {
2721 usbd_abort_pipe(pipe);
2722 usbd_close_pipe(pipe);
2723 }
2724 pipe = atomic_swap_ptr(&ch->sync_pipe, NULL);
2725 if (pipe) {
2726 usbd_abort_pipe(pipe);
2727 usbd_close_pipe(pipe);
2728 }
2729 }
2730
2731 Static usbd_status
2732 uaudio_chan_alloc_buffers(struct uaudio_softc *sc, struct chan *ch)
2733 {
2734 usbd_xfer_handle xfer;
2735 void *tbuf;
2736 int i, size;
2737
2738 size = (ch->bytes_per_frame + ch->sample_size) * UAUDIO_NFRAMES;
2739 for (i = 0; i < UAUDIO_NCHANBUFS; i++) {
2740 xfer = usbd_alloc_xfer(sc->sc_udev);
2741 if (xfer == 0)
2742 goto bad;
2743 ch->chanbufs[i].xfer = xfer;
2744 tbuf = usbd_alloc_buffer(xfer, size);
2745 if (tbuf == 0) {
2746 i++;
2747 goto bad;
2748 }
2749 ch->chanbufs[i].buffer = tbuf;
2750 ch->chanbufs[i].chan = ch;
2751 }
2752
2753 return USBD_NORMAL_COMPLETION;
2754
2755 bad:
2756 while (--i >= 0)
2757 /* implicit buffer free */
2758 usbd_free_xfer(ch->chanbufs[i].xfer);
2759 return USBD_NOMEM;
2760 }
2761
2762 Static void
2763 uaudio_chan_free_buffers(struct uaudio_softc *sc, struct chan *ch)
2764 {
2765 int i;
2766
2767 for (i = 0; i < UAUDIO_NCHANBUFS; i++)
2768 usbd_free_xfer(ch->chanbufs[i].xfer);
2769 }
2770
2771 /* Called with USB lock held. */
2772 Static void
2773 uaudio_chan_ptransfer(struct chan *ch)
2774 {
2775 struct chanbuf *cb;
2776 int i, n, size, residue, total;
2777
2778 if (ch->sc->sc_dying)
2779 return;
2780
2781 /* Pick the next channel buffer. */
2782 cb = &ch->chanbufs[ch->curchanbuf];
2783 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
2784 ch->curchanbuf = 0;
2785
2786 /* Compute the size of each frame in the next transfer. */
2787 residue = ch->residue;
2788 total = 0;
2789 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2790 size = ch->bytes_per_frame;
2791 residue += ch->fraction;
2792 if (residue >= USB_FRAMES_PER_SECOND) {
2793 if ((ch->sc->sc_altflags & UA_NOFRAC) == 0)
2794 size += ch->sample_size;
2795 residue -= USB_FRAMES_PER_SECOND;
2796 }
2797 cb->sizes[i] = size;
2798 total += size;
2799 }
2800 ch->residue = residue;
2801 cb->size = total;
2802
2803 /*
2804 * Transfer data from upper layer buffer to channel buffer, taking
2805 * care of wrapping the upper layer buffer.
2806 */
2807 n = min(total, ch->end - ch->cur);
2808 memcpy(cb->buffer, ch->cur, n);
2809 ch->cur += n;
2810 if (ch->cur >= ch->end)
2811 ch->cur = ch->start;
2812 if (total > n) {
2813 total -= n;
2814 memcpy(cb->buffer + n, ch->cur, total);
2815 ch->cur += total;
2816 }
2817
2818 #ifdef UAUDIO_DEBUG
2819 if (uaudiodebug > 8) {
2820 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
2821 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2822 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
2823 }
2824 }
2825 #endif
2826
2827 //DPRINTFN(5, "ptransfer xfer=%p\n", cb->xfer);
2828 /* Fill the request */
2829 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
2830 UAUDIO_NFRAMES, USBD_NO_COPY,
2831 uaudio_chan_pintr);
2832
2833 (void)usbd_transfer(cb->xfer);
2834 }
2835
2836 Static void
2837 uaudio_chan_pintr(usbd_xfer_handle xfer, usbd_private_handle priv,
2838 usbd_status status)
2839 {
2840 struct chanbuf *cb;
2841 struct chan *ch;
2842 uint32_t count;
2843
2844 cb = priv;
2845 ch = cb->chan;
2846 /* Return if we are aborting. */
2847 if (status == USBD_CANCELLED)
2848 return;
2849
2850 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
2851 DPRINTFN(5, "count=%d, transferred=%d\n",
2852 count, ch->transferred);
2853 #ifdef DIAGNOSTIC
2854 if (count != cb->size) {
2855 aprint_error("uaudio_chan_pintr: count(%d) != size(%d)\n",
2856 count, cb->size);
2857 }
2858 #endif
2859
2860 ch->transferred += cb->size;
2861 mutex_spin_enter(&ch->sc->sc_intr_lock);
2862 /* Call back to upper layer */
2863 while (ch->transferred >= ch->blksize) {
2864 ch->transferred -= ch->blksize;
2865 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
2866 ch->intr(ch->arg);
2867 }
2868 mutex_spin_exit(&ch->sc->sc_intr_lock);
2869
2870 /* start next transfer */
2871 uaudio_chan_ptransfer(ch);
2872 }
2873
2874 /* Called with USB lock held. */
2875 Static void
2876 uaudio_chan_rtransfer(struct chan *ch)
2877 {
2878 struct chanbuf *cb;
2879 int i, size, residue, total;
2880
2881 if (ch->sc->sc_dying)
2882 return;
2883
2884 /* Pick the next channel buffer. */
2885 cb = &ch->chanbufs[ch->curchanbuf];
2886 if (++ch->curchanbuf >= UAUDIO_NCHANBUFS)
2887 ch->curchanbuf = 0;
2888
2889 /* Compute the size of each frame in the next transfer. */
2890 residue = ch->residue;
2891 total = 0;
2892 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2893 size = ch->bytes_per_frame;
2894 cb->sizes[i] = size;
2895 cb->offsets[i] = total;
2896 total += size;
2897 }
2898 ch->residue = residue;
2899 cb->size = total;
2900
2901 #ifdef UAUDIO_DEBUG
2902 if (uaudiodebug > 8) {
2903 DPRINTF("buffer=%p, residue=0.%03d\n", cb->buffer, ch->residue);
2904 for (i = 0; i < UAUDIO_NFRAMES; i++) {
2905 DPRINTF(" [%d] length %d\n", i, cb->sizes[i]);
2906 }
2907 }
2908 #endif
2909
2910 DPRINTFN(5, "transfer xfer=%p\n", cb->xfer);
2911 /* Fill the request */
2912 usbd_setup_isoc_xfer(cb->xfer, ch->pipe, cb, cb->sizes,
2913 UAUDIO_NFRAMES, USBD_NO_COPY,
2914 uaudio_chan_rintr);
2915
2916 (void)usbd_transfer(cb->xfer);
2917 }
2918
2919 Static void
2920 uaudio_chan_rintr(usbd_xfer_handle xfer, usbd_private_handle priv,
2921 usbd_status status)
2922 {
2923 struct chanbuf *cb;
2924 struct chan *ch;
2925 uint32_t count;
2926 int i, n, frsize;
2927
2928 cb = priv;
2929 ch = cb->chan;
2930 /* Return if we are aborting. */
2931 if (status == USBD_CANCELLED)
2932 return;
2933
2934 usbd_get_xfer_status(xfer, NULL, NULL, &count, NULL);
2935 DPRINTFN(5, "count=%d, transferred=%d\n", count, ch->transferred);
2936
2937 /* count < cb->size is normal for asynchronous source */
2938 #ifdef DIAGNOSTIC
2939 if (count > cb->size) {
2940 aprint_error("uaudio_chan_rintr: count(%d) > size(%d)\n",
2941 count, cb->size);
2942 }
2943 #endif
2944
2945 /*
2946 * Transfer data from channel buffer to upper layer buffer, taking
2947 * care of wrapping the upper layer buffer.
2948 */
2949 for(i = 0; i < UAUDIO_NFRAMES; i++) {
2950 frsize = cb->sizes[i];
2951 n = min(frsize, ch->end - ch->cur);
2952 memcpy(ch->cur, cb->buffer + cb->offsets[i], n);
2953 ch->cur += n;
2954 if (ch->cur >= ch->end)
2955 ch->cur = ch->start;
2956 if (frsize > n) {
2957 memcpy(ch->cur, cb->buffer + cb->offsets[i] + n,
2958 frsize - n);
2959 ch->cur += frsize - n;
2960 }
2961 }
2962
2963 /* Call back to upper layer */
2964 ch->transferred += count;
2965 mutex_spin_enter(&ch->sc->sc_intr_lock);
2966 while (ch->transferred >= ch->blksize) {
2967 ch->transferred -= ch->blksize;
2968 DPRINTFN(5, "call %p(%p)\n", ch->intr, ch->arg);
2969 ch->intr(ch->arg);
2970 }
2971 mutex_spin_exit(&ch->sc->sc_intr_lock);
2972
2973 /* start next transfer */
2974 uaudio_chan_rtransfer(ch);
2975 }
2976
2977 Static void
2978 uaudio_chan_init(struct chan *ch, int altidx, const struct audio_params *param,
2979 int maxpktsize)
2980 {
2981 int samples_per_frame, sample_size;
2982
2983 ch->altidx = altidx;
2984 sample_size = param->precision * param->channels / 8;
2985 samples_per_frame = param->sample_rate / USB_FRAMES_PER_SECOND;
2986 ch->sample_size = sample_size;
2987 ch->sample_rate = param->sample_rate;
2988 if (maxpktsize == 0) {
2989 ch->fraction = param->sample_rate % USB_FRAMES_PER_SECOND;
2990 ch->bytes_per_frame = samples_per_frame * sample_size;
2991 } else {
2992 ch->fraction = 0;
2993 ch->bytes_per_frame = maxpktsize;
2994 }
2995 ch->residue = 0;
2996 }
2997
2998 Static void
2999 uaudio_chan_set_param(struct chan *ch, u_char *start, u_char *end, int blksize)
3000 {
3001
3002 ch->start = start;
3003 ch->end = end;
3004 ch->cur = start;
3005 ch->blksize = blksize;
3006 ch->transferred = 0;
3007 ch->curchanbuf = 0;
3008 }
3009
3010 Static int
3011 uaudio_set_params(void *addr, int setmode, int usemode,
3012 struct audio_params *play, struct audio_params *rec,
3013 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
3014 {
3015 struct uaudio_softc *sc;
3016 int paltidx, raltidx;
3017 struct audio_params *p;
3018 stream_filter_list_t *fil;
3019 int mode, i;
3020
3021 sc = addr;
3022 paltidx = -1;
3023 raltidx = -1;
3024 if (sc->sc_dying)
3025 return EIO;
3026
3027 if (((usemode & AUMODE_PLAY) && sc->sc_playchan.pipe != NULL) ||
3028 ((usemode & AUMODE_RECORD) && sc->sc_recchan.pipe != NULL))
3029 return EBUSY;
3030
3031 if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
3032 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 0;
3033 AUFMT_VALIDATE(sc->sc_alts[sc->sc_playchan.altidx].aformat);
3034 }
3035 if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
3036 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 0;
3037 AUFMT_VALIDATE(sc->sc_alts[sc->sc_recchan.altidx].aformat);
3038 }
3039
3040 /* Some uaudio devices are unidirectional. Don't try to find a
3041 matching mode for the unsupported direction. */
3042 setmode &= sc->sc_mode;
3043
3044 for (mode = AUMODE_RECORD; mode != -1;
3045 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
3046 if ((setmode & mode) == 0)
3047 continue;
3048
3049 if (mode == AUMODE_PLAY) {
3050 p = play;
3051 fil = pfil;
3052 } else {
3053 p = rec;
3054 fil = rfil;
3055 }
3056 i = auconv_set_converter(sc->sc_formats, sc->sc_nformats,
3057 mode, p, TRUE, fil);
3058 if (i < 0)
3059 return EINVAL;
3060
3061 if (mode == AUMODE_PLAY)
3062 paltidx = i;
3063 else
3064 raltidx = i;
3065 }
3066
3067 if ((setmode & AUMODE_PLAY)) {
3068 p = pfil->req_size > 0 ? &pfil->filters[0].param : play;
3069 /* XXX abort transfer if currently happening? */
3070 uaudio_chan_init(&sc->sc_playchan, paltidx, p, 0);
3071 }
3072 if ((setmode & AUMODE_RECORD)) {
3073 p = rfil->req_size > 0 ? &rfil->filters[0].param : rec;
3074 /* XXX abort transfer if currently happening? */
3075 uaudio_chan_init(&sc->sc_recchan, raltidx, p,
3076 UGETW(sc->sc_alts[raltidx].edesc->wMaxPacketSize));
3077 }
3078
3079 if ((usemode & AUMODE_PLAY) && sc->sc_playchan.altidx != -1) {
3080 sc->sc_alts[sc->sc_playchan.altidx].sc_busy = 1;
3081 AUFMT_INVALIDATE(sc->sc_alts[sc->sc_playchan.altidx].aformat);
3082 }
3083 if ((usemode & AUMODE_RECORD) && sc->sc_recchan.altidx != -1) {
3084 sc->sc_alts[sc->sc_recchan.altidx].sc_busy = 1;
3085 AUFMT_INVALIDATE(sc->sc_alts[sc->sc_recchan.altidx].aformat);
3086 }
3087
3088 DPRINTF("use altidx=p%d/r%d, altno=p%d/r%d\n",
3089 sc->sc_playchan.altidx, sc->sc_recchan.altidx,
3090 (sc->sc_playchan.altidx >= 0)
3091 ?sc->sc_alts[sc->sc_playchan.altidx].idesc->bAlternateSetting
3092 : -1,
3093 (sc->sc_recchan.altidx >= 0)
3094 ? sc->sc_alts[sc->sc_recchan.altidx].idesc->bAlternateSetting
3095 : -1);
3096
3097 return 0;
3098 }
3099
3100 Static usbd_status
3101 uaudio_set_speed(struct uaudio_softc *sc, int endpt, u_int speed)
3102 {
3103 usb_device_request_t req;
3104 usbd_status err;
3105 uint8_t data[3];
3106
3107 DPRINTFN(5, "endpt=%d speed=%u\n", endpt, speed);
3108 req.bmRequestType = UT_WRITE_CLASS_ENDPOINT;
3109 req.bRequest = SET_CUR;
3110 USETW2(req.wValue, SAMPLING_FREQ_CONTROL, 0);
3111 USETW(req.wIndex, endpt);
3112 USETW(req.wLength, 3);
3113 data[0] = speed;
3114 data[1] = speed >> 8;
3115 data[2] = speed >> 16;
3116
3117 err = usbd_do_request(sc->sc_udev, &req, data);
3118
3119 return err;
3120 }
3121
3122 #ifdef _MODULE
3123
3124 MODULE(MODULE_CLASS_DRIVER, uaudio, NULL);
3125
3126 static const struct cfiattrdata audiobuscf_iattrdata = {
3127 "audiobus", 0, { { NULL, NULL, 0 }, }
3128 };
3129 static const struct cfiattrdata * const uaudio_attrs[] = {
3130 &audiobuscf_iattrdata, NULL
3131 };
3132 CFDRIVER_DECL(uaudio, DV_DULL, uaudio_attrs);
3133 extern struct cfattach uaudio_ca;
3134 static int uaudioloc[6/*USBIFIFCF_NLOCS*/] = {
3135 -1/*USBIFIFCF_PORT_DEFAULT*/,
3136 -1/*USBIFIFCF_CONFIGURATION_DEFAULT*/,
3137 -1/*USBIFIFCF_INTERFACE_DEFAULT*/,
3138 -1/*USBIFIFCF_VENDOR_DEFAULT*/,
3139 -1/*USBIFIFCF_PRODUCT_DEFAULT*/,
3140 -1/*USBIFIFCF_RELEASE_DEFAULT*/};
3141 static struct cfparent uhubparent = {
3142 "usbifif", NULL, DVUNIT_ANY
3143 };
3144 static struct cfdata uaudio_cfdata[] = {
3145 {
3146 .cf_name = "uaudio",
3147 .cf_atname = "uaudio",
3148 .cf_unit = 0,
3149 .cf_fstate = FSTATE_STAR,
3150 .cf_loc = uaudioloc,
3151 .cf_flags = 0,
3152 .cf_pspec = &uhubparent,
3153 },
3154 { NULL }
3155 };
3156
3157 static int
3158 uaudio_modcmd(modcmd_t cmd, void *arg)
3159 {
3160 int err;
3161
3162 switch (cmd) {
3163 case MODULE_CMD_INIT:
3164 err = config_cfdriver_attach(&uaudio_cd);
3165 if (err) {
3166 return err;
3167 }
3168 err = config_cfattach_attach("uaudio", &uaudio_ca);
3169 if (err) {
3170 config_cfdriver_detach(&uaudio_cd);
3171 return err;
3172 }
3173 err = config_cfdata_attach(uaudio_cfdata, 1);
3174 if (err) {
3175 config_cfattach_detach("uaudio", &uaudio_ca);
3176 config_cfdriver_detach(&uaudio_cd);
3177 return err;
3178 }
3179 return 0;
3180 case MODULE_CMD_FINI:
3181 err = config_cfdata_detach(uaudio_cfdata);
3182 if (err)
3183 return err;
3184 config_cfattach_detach("uaudio", &uaudio_ca);
3185 config_cfdriver_detach(&uaudio_cd);
3186 return 0;
3187 default:
3188 return ENOTTY;
3189 }
3190 }
3191
3192 #endif
3193