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