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