1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef __USBAUDIO_H
3 #define __USBAUDIO_H
4 /*
5 * (Tentative) USB Audio Driver for ALSA
6 *
7 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
8 */
9
10 #include <sound/core.h>
11
12 /* handling of USB vendor/product ID pairs as 32-bit numbers */
13 #define USB_ID(vendor, product) (((unsigned int)(vendor) << 16) | (product))
14 #define USB_ID_VENDOR(id) ((id) >> 16)
15 #define USB_ID_PRODUCT(id) ((u16)(id))
16
17 /*
18 *
19 */
20
21 struct media_device;
22 struct media_intf_devnode;
23
24 #define MAX_CARD_INTERFACES 16
25
26 /*
27 * Structure holding assosiation between Audio Control Interface
28 * and given Streaming or Midi Interface.
29 */
30 struct snd_intf_to_ctrl {
31 u8 interface;
32 struct usb_host_interface *ctrl_intf;
33 };
34
35 struct snd_usb_audio {
36 int index;
37 struct usb_device *dev;
38 struct snd_card *card;
39 struct usb_interface *intf[MAX_CARD_INTERFACES];
40 u32 usb_id;
41 uint16_t quirk_type;
42 struct mutex mutex;
43 unsigned int system_suspend;
44 atomic_t active;
45 atomic_t shutdown;
46 struct snd_refcount usage_count;
47 u64 quirk_flags;
48 unsigned int need_delayed_register:1; /* warn for delayed registration */
49 int num_interfaces;
50 int last_iface;
51 int num_suspended_intf;
52 int sample_rate_read_error;
53
54 int badd_profile; /* UAC3 BADD profile */
55
56 struct list_head pcm_list; /* list of pcm streams */
57 struct list_head ep_list; /* list of audio-related endpoints */
58 struct list_head iface_ref_list; /* list of interface refcounts */
59 struct list_head clock_ref_list; /* list of clock refcounts */
60 int pcm_devs;
61
62 unsigned int num_rawmidis; /* number of created rawmidi devices */
63 struct list_head midi_list; /* list of midi interfaces */
64 struct list_head midi_v2_list; /* list of MIDI 2 interfaces */
65
66 struct list_head mixer_list; /* list of mixer interfaces */
67
68 int setup; /* from the 'device_setup' module param */
69 bool generic_implicit_fb; /* from the 'implicit_fb' module param */
70 bool autoclock; /* from the 'autoclock' module param */
71
72 bool lowlatency; /* from the 'lowlatency' module param */
73 struct usb_host_interface *ctrl_intf; /* the audio control interface */
74 struct media_device *media_dev;
75 struct media_intf_devnode *ctl_intf_media_devnode;
76
77 unsigned int num_intf_to_ctrl;
78 struct snd_intf_to_ctrl intf_to_ctrl[MAX_CARD_INTERFACES];
79 };
80
81 #define USB_AUDIO_IFACE_UNUSED ((void *)-1L)
82
83 #define usb_audio_err(chip, fmt, args...) \
84 dev_err(&(chip)->dev->dev, fmt, ##args)
85 #define usb_audio_err_ratelimited(chip, fmt, args...) \
86 dev_err_ratelimited(&(chip)->dev->dev, fmt, ##args)
87 #define usb_audio_warn(chip, fmt, args...) \
88 dev_warn(&(chip)->dev->dev, fmt, ##args)
89 #define usb_audio_info(chip, fmt, args...) \
90 dev_info(&(chip)->dev->dev, fmt, ##args)
91 #define usb_audio_dbg(chip, fmt, args...) \
92 dev_dbg(&(chip)->dev->dev, fmt, ##args)
93
94 /*
95 * Information about devices with broken descriptors
96 */
97
98 /* special values for .ifnum */
99 #define QUIRK_NODEV_INTERFACE -3 /* return -ENODEV */
100 #define QUIRK_NO_INTERFACE -2
101 #define QUIRK_ANY_INTERFACE -1
102
103 enum quirk_type {
104 QUIRK_IGNORE_INTERFACE,
105 QUIRK_COMPOSITE,
106 QUIRK_AUTODETECT,
107 QUIRK_MIDI_STANDARD_INTERFACE,
108 QUIRK_MIDI_FIXED_ENDPOINT,
109 QUIRK_MIDI_YAMAHA,
110 QUIRK_MIDI_ROLAND,
111 QUIRK_MIDI_MIDIMAN,
112 QUIRK_MIDI_NOVATION,
113 QUIRK_MIDI_RAW_BYTES,
114 QUIRK_MIDI_EMAGIC,
115 QUIRK_MIDI_CME,
116 QUIRK_MIDI_AKAI,
117 QUIRK_MIDI_US122L,
118 QUIRK_MIDI_FTDI,
119 QUIRK_MIDI_CH345,
120 QUIRK_AUDIO_STANDARD_INTERFACE,
121 QUIRK_AUDIO_FIXED_ENDPOINT,
122 QUIRK_AUDIO_EDIROL_UAXX,
123 QUIRK_AUDIO_STANDARD_MIXER,
124
125 QUIRK_TYPE_COUNT
126 };
127
128 struct snd_usb_audio_quirk {
129 const char *vendor_name;
130 const char *product_name;
131 int16_t ifnum;
132 uint16_t type;
133 const void *data;
134 };
135
136 #define combine_word(s) ((*(s)) | ((unsigned int)(s)[1] << 8))
137 #define combine_triple(s) (combine_word(s) | ((unsigned int)(s)[2] << 16))
138 #define combine_quad(s) (combine_triple(s) | ((unsigned int)(s)[3] << 24))
139
140 int snd_usb_lock_shutdown(struct snd_usb_audio *chip);
141 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip);
142
143 /* auto-cleanup */
144 struct __snd_usb_lock {
145 struct snd_usb_audio *chip;
146 int err;
147 };
148
__snd_usb_lock_shutdown(struct snd_usb_audio * chip)149 static inline struct __snd_usb_lock __snd_usb_lock_shutdown(struct snd_usb_audio *chip)
150 {
151 struct __snd_usb_lock T = { .chip = chip };
152 T.err = snd_usb_lock_shutdown(chip);
153 return T;
154 }
155
__snd_usb_unlock_shutdown(struct __snd_usb_lock * lock)156 static inline void __snd_usb_unlock_shutdown(struct __snd_usb_lock *lock)
157 {
158 if (!lock->err)
159 snd_usb_unlock_shutdown(lock->chip);
160 }
161
162 DEFINE_CLASS(snd_usb_lock, struct __snd_usb_lock,
163 __snd_usb_unlock_shutdown(&(_T)), __snd_usb_lock_shutdown(chip),
164 struct snd_usb_audio *chip)
165
166 extern bool snd_usb_use_vmalloc;
167 extern bool snd_usb_skip_validation;
168
169 /*
170 * Driver behavior quirk flags, stored in chip->quirk_flags
171 *
172 * QUIRK_FLAG_GET_SAMPLE_RATE:
173 * Skip reading sample rate for devices, as some devices behave inconsistently
174 * or return error
175 * QUIRK_FLAG_SHARE_MEDIA_DEVICE:
176 * Create Media Controller API entries
177 * QUIRK_FLAG_ALIGN_TRANSFER:
178 * Allow alignment on audio sub-slot (channel samples) rather than on audio
179 * slots (audio frames)
180 * QUIRK_TX_LENGTH:
181 * Add length specifier to transfers
182 * QUIRK_FLAG_PLAYBACK_FIRST:
183 * Start playback stream at first even in implement feedback mode
184 * QUIRK_FLAG_SKIP_CLOCK_SELECTOR:
185 * Skip clock selector setup; the device may reset to invalid state
186 * QUIRK_FLAG_IGNORE_CLOCK_SOURCE:
187 * Ignore errors from clock source search; i.e. hardcoded clock
188 * QUIRK_FLAG_ITF_USB_DSD_DAC:
189 * Indicates the device is for ITF-USB DSD based DACs that need a vendor cmd
190 * to switch between PCM and native DSD mode
191 * QUIRK_FLAG_CTL_MSG_DELAY:
192 * Add a delay of 20ms at each control message handling
193 * QUIRK_FLAG_CTL_MSG_DELAY_1M:
194 * Add a delay of 1-2ms at each control message handling
195 * QUIRK_FLAG_CTL_MSG_DELAY_5M:
196 * Add a delay of 5-6ms at each control message handling
197 * QUIRK_FLAG_IFACE_DELAY:
198 * Add a delay of 50ms at each interface setup
199 * QUIRK_FLAG_VALIDATE_RATES:
200 * Perform sample rate validations at probe
201 * QUIRK_FLAG_DISABLE_AUTOSUSPEND:
202 * Disable runtime PM autosuspend
203 * QUIRK_FLAG_IGNORE_CTL_ERROR:
204 * Ignore errors for mixer access
205 * QUIRK_FLAG_DSD_RAW:
206 * Support generic DSD raw U32_BE format
207 * QUIRK_FLAG_SET_IFACE_FIRST:
208 * Set up the interface at first like UAC1
209 * QUIRK_FLAG_GENERIC_IMPLICIT_FB
210 * Apply the generic implicit feedback sync mode (same as implicit_fb=1 option)
211 * QUIRK_FLAG_SKIP_IMPLICIT_FB
212 * Don't apply implicit feedback sync mode
213 * QUIRK_FLAG_IFACE_SKIP_CLOSE
214 * Don't closed interface during setting sample rate
215 * QUIRK_FLAG_FORCE_IFACE_RESET
216 * Force an interface reset whenever stopping & restarting a stream
217 * (e.g. after xrun)
218 * QUIRK_FLAG_FIXED_RATE
219 * Do not set PCM rate (frequency) when only one rate is available
220 * for the given endpoint.
221 * QUIRK_FLAG_MIC_RES_16 and QUIRK_FLAG_MIC_RES_384
222 * Set the fixed resolution for Mic Capture Volume (mostly for webcams)
223 * QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE
224 * Set minimum volume control value as mute for devices where the lowest
225 * playback value represents muted state instead of minimum audible volume
226 * QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE
227 * Similar to QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE, but for capture streams
228 * QUIRK_FLAG_SKIP_IFACE_SETUP
229 * Skip the probe-time interface setup (usb_set_interface,
230 * init_pitch, init_sample_rate); redundant with
231 * snd_usb_endpoint_prepare() at stream-open time
232 * QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL
233 * Set linear volume mapping for devices where the playback volume control
234 * value is mapped to voltage (instead of dB) level linearly. In short:
235 * x(raw) = (raw - raw_min) / (raw_max - raw_min); V(x) = k * x;
236 * dB(x) = 20 * log10(x). Overrides QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE
237 * QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL
238 * Similar to QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL, but for capture streams.
239 * Overrides QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE
240 * QUIRK_FLAG_IFB_SILENCE_ON_EMPTY
241 * In implicit feedback mode, when an entire capture URB returns with
242 * all iso_frame_desc[i].status != 0 (bytes==0), do not silently return
243 * from snd_usb_handle_sync_urb. Instead fall through and enqueue a
244 * packet_info containing only size-0 packets, so the OUT ring keeps
245 * moving (emits silence). Needed by Behringer Flow 8 (1397:050c).
246 * QUIRK_FLAG_MIXER_GET_CUR_OK
247 * On some devices, whether their GET_CUR being sticky depends on whether
248 * hotpluggable components are present. When the hotpluggable components are
249 * missing on probe, their GET_CUR behavior is classified as broken. Set the
250 * flag to prevent the heuristics from gating GET_CUR.
251 * QUIRK_FLAG_PLAYBACK_URB_FIXUP
252 * Set URB_ISO_ASAP flag for isochronous URBs and force nurbs to MAX_URBS.
253 * This is needed for devices that exhibit boot-time audio stuttering due
254 * to insufficient buffer depth combined with xHCI scheduling variability.
255 * The larger buffer (MAX_URBS = 12, ~64ms) absorbs system scheduling
256 * jitter during boot, while URB_ISO_ASAP ensures consistent xHCI scheduling.
257 * QUIRK_FLAG_ALWAYS_SET_RATE
258 * Issue SET_CUR for the sample rate even when the clock already reports the
259 * requested rate. A device advertising a single rate is otherwise never sent
260 * the request at all, and some require it before streaming will start.
261 */
262
263 enum {
264 QUIRK_TYPE_GET_SAMPLE_RATE = 0,
265 QUIRK_TYPE_SHARE_MEDIA_DEVICE = 1,
266 QUIRK_TYPE_ALIGN_TRANSFER = 2,
267 QUIRK_TYPE_TX_LENGTH = 3,
268 QUIRK_TYPE_PLAYBACK_FIRST = 4,
269 QUIRK_TYPE_SKIP_CLOCK_SELECTOR = 5,
270 QUIRK_TYPE_IGNORE_CLOCK_SOURCE = 6,
271 QUIRK_TYPE_ITF_USB_DSD_DAC = 7,
272 QUIRK_TYPE_CTL_MSG_DELAY = 8,
273 QUIRK_TYPE_CTL_MSG_DELAY_1M = 9,
274 QUIRK_TYPE_CTL_MSG_DELAY_5M = 10,
275 QUIRK_TYPE_IFACE_DELAY = 11,
276 QUIRK_TYPE_VALIDATE_RATES = 12,
277 QUIRK_TYPE_DISABLE_AUTOSUSPEND = 13,
278 QUIRK_TYPE_IGNORE_CTL_ERROR = 14,
279 QUIRK_TYPE_DSD_RAW = 15,
280 QUIRK_TYPE_SET_IFACE_FIRST = 16,
281 QUIRK_TYPE_GENERIC_IMPLICIT_FB = 17,
282 QUIRK_TYPE_SKIP_IMPLICIT_FB = 18,
283 QUIRK_TYPE_IFACE_SKIP_CLOSE = 19,
284 QUIRK_TYPE_FORCE_IFACE_RESET = 20,
285 QUIRK_TYPE_FIXED_RATE = 21,
286 QUIRK_TYPE_MIC_RES_16 = 22,
287 QUIRK_TYPE_MIC_RES_384 = 23,
288 QUIRK_TYPE_MIXER_PLAYBACK_MIN_MUTE = 24,
289 QUIRK_TYPE_MIXER_CAPTURE_MIN_MUTE = 25,
290 QUIRK_TYPE_SKIP_IFACE_SETUP = 26,
291 QUIRK_TYPE_MIXER_PLAYBACK_LINEAR_VOL = 27,
292 QUIRK_TYPE_MIXER_CAPTURE_LINEAR_VOL = 28,
293 QUIRK_TYPE_IFB_SILENCE_ON_EMPTY = 29,
294 QUIRK_TYPE_MIXER_GET_CUR_OK = 30,
295 QUIRK_TYPE_PLAYBACK_URB_FIXUP = 31,
296 QUIRK_TYPE_ALWAYS_SET_RATE = 32,
297 /* Please also edit snd_usb_audio_quirk_flag_names and alsa-configuration.rst */
298 };
299
300 #define QUIRK_FLAG(x) BIT_U64(QUIRK_TYPE_ ## x)
301
302 #define QUIRK_FLAG_GET_SAMPLE_RATE QUIRK_FLAG(GET_SAMPLE_RATE)
303 #define QUIRK_FLAG_SHARE_MEDIA_DEVICE QUIRK_FLAG(SHARE_MEDIA_DEVICE)
304 #define QUIRK_FLAG_ALIGN_TRANSFER QUIRK_FLAG(ALIGN_TRANSFER)
305 #define QUIRK_FLAG_TX_LENGTH QUIRK_FLAG(TX_LENGTH)
306 #define QUIRK_FLAG_PLAYBACK_FIRST QUIRK_FLAG(PLAYBACK_FIRST)
307 #define QUIRK_FLAG_SKIP_CLOCK_SELECTOR QUIRK_FLAG(SKIP_CLOCK_SELECTOR)
308 #define QUIRK_FLAG_IGNORE_CLOCK_SOURCE QUIRK_FLAG(IGNORE_CLOCK_SOURCE)
309 #define QUIRK_FLAG_ITF_USB_DSD_DAC QUIRK_FLAG(ITF_USB_DSD_DAC)
310 #define QUIRK_FLAG_CTL_MSG_DELAY QUIRK_FLAG(CTL_MSG_DELAY)
311 #define QUIRK_FLAG_CTL_MSG_DELAY_1M QUIRK_FLAG(CTL_MSG_DELAY_1M)
312 #define QUIRK_FLAG_CTL_MSG_DELAY_5M QUIRK_FLAG(CTL_MSG_DELAY_5M)
313 #define QUIRK_FLAG_IFACE_DELAY QUIRK_FLAG(IFACE_DELAY)
314 #define QUIRK_FLAG_VALIDATE_RATES QUIRK_FLAG(VALIDATE_RATES)
315 #define QUIRK_FLAG_DISABLE_AUTOSUSPEND QUIRK_FLAG(DISABLE_AUTOSUSPEND)
316 #define QUIRK_FLAG_IGNORE_CTL_ERROR QUIRK_FLAG(IGNORE_CTL_ERROR)
317 #define QUIRK_FLAG_DSD_RAW QUIRK_FLAG(DSD_RAW)
318 #define QUIRK_FLAG_SET_IFACE_FIRST QUIRK_FLAG(SET_IFACE_FIRST)
319 #define QUIRK_FLAG_GENERIC_IMPLICIT_FB QUIRK_FLAG(GENERIC_IMPLICIT_FB)
320 #define QUIRK_FLAG_SKIP_IMPLICIT_FB QUIRK_FLAG(SKIP_IMPLICIT_FB)
321 #define QUIRK_FLAG_IFACE_SKIP_CLOSE QUIRK_FLAG(IFACE_SKIP_CLOSE)
322 #define QUIRK_FLAG_FORCE_IFACE_RESET QUIRK_FLAG(FORCE_IFACE_RESET)
323 #define QUIRK_FLAG_FIXED_RATE QUIRK_FLAG(FIXED_RATE)
324 #define QUIRK_FLAG_MIC_RES_16 QUIRK_FLAG(MIC_RES_16)
325 #define QUIRK_FLAG_MIC_RES_384 QUIRK_FLAG(MIC_RES_384)
326 #define QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE QUIRK_FLAG(MIXER_PLAYBACK_MIN_MUTE)
327 #define QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE QUIRK_FLAG(MIXER_CAPTURE_MIN_MUTE)
328 #define QUIRK_FLAG_SKIP_IFACE_SETUP QUIRK_FLAG(SKIP_IFACE_SETUP)
329 #define QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL QUIRK_FLAG(MIXER_PLAYBACK_LINEAR_VOL)
330 #define QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL QUIRK_FLAG(MIXER_CAPTURE_LINEAR_VOL)
331 #define QUIRK_FLAG_IFB_SILENCE_ON_EMPTY QUIRK_FLAG(IFB_SILENCE_ON_EMPTY)
332 #define QUIRK_FLAG_MIXER_GET_CUR_OK QUIRK_FLAG(MIXER_GET_CUR_OK)
333 #define QUIRK_FLAG_PLAYBACK_URB_FIXUP QUIRK_FLAG(PLAYBACK_URB_FIXUP)
334 #define QUIRK_FLAG_ALWAYS_SET_RATE QUIRK_FLAG(ALWAYS_SET_RATE)
335
336 #endif /* __USBAUDIO_H */
337