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