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