xref: /linux/sound/usb/usbaudio.h (revision ab8f7ffd63d5074c865935ad3720f0d995d2160f)
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 	unsigned int 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 
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 
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_BROKEN
247  *  Some mixers are sticky, which means that setting their current volume is a
248  *  no-op, and reading the current volume returns a constant value. The sticky
249  *  check disables these mixers to prevent confusing userspace. However, some
250  *  devices do have a tunable volume despite the reported current volume being
251  *  constant. As the sticky check can't distinguish between the two categories,
252  *  setting this flag tells that the device should fall into the second
253  *  category when GET_CUR returns a constant value, resulting in the sticky
254  *  check being non-fatal and only disabling GET_CUR instead of the whole mixer.
255  *  The current volume will then be provided by the internal cache that stores
256  *  the last set volume
257  */
258 
259 enum {
260 	QUIRK_TYPE_GET_SAMPLE_RATE		= 0,
261 	QUIRK_TYPE_SHARE_MEDIA_DEVICE		= 1,
262 	QUIRK_TYPE_ALIGN_TRANSFER		= 2,
263 	QUIRK_TYPE_TX_LENGTH			= 3,
264 	QUIRK_TYPE_PLAYBACK_FIRST		= 4,
265 	QUIRK_TYPE_SKIP_CLOCK_SELECTOR		= 5,
266 	QUIRK_TYPE_IGNORE_CLOCK_SOURCE		= 6,
267 	QUIRK_TYPE_ITF_USB_DSD_DAC		= 7,
268 	QUIRK_TYPE_CTL_MSG_DELAY		= 8,
269 	QUIRK_TYPE_CTL_MSG_DELAY_1M		= 9,
270 	QUIRK_TYPE_CTL_MSG_DELAY_5M		= 10,
271 	QUIRK_TYPE_IFACE_DELAY			= 11,
272 	QUIRK_TYPE_VALIDATE_RATES		= 12,
273 	QUIRK_TYPE_DISABLE_AUTOSUSPEND		= 13,
274 	QUIRK_TYPE_IGNORE_CTL_ERROR		= 14,
275 	QUIRK_TYPE_DSD_RAW			= 15,
276 	QUIRK_TYPE_SET_IFACE_FIRST		= 16,
277 	QUIRK_TYPE_GENERIC_IMPLICIT_FB		= 17,
278 	QUIRK_TYPE_SKIP_IMPLICIT_FB		= 18,
279 	QUIRK_TYPE_IFACE_SKIP_CLOSE		= 19,
280 	QUIRK_TYPE_FORCE_IFACE_RESET		= 20,
281 	QUIRK_TYPE_FIXED_RATE			= 21,
282 	QUIRK_TYPE_MIC_RES_16			= 22,
283 	QUIRK_TYPE_MIC_RES_384			= 23,
284 	QUIRK_TYPE_MIXER_PLAYBACK_MIN_MUTE	= 24,
285 	QUIRK_TYPE_MIXER_CAPTURE_MIN_MUTE	= 25,
286 	QUIRK_TYPE_SKIP_IFACE_SETUP		= 26,
287 	QUIRK_TYPE_MIXER_PLAYBACK_LINEAR_VOL	= 27,
288 	QUIRK_TYPE_MIXER_CAPTURE_LINEAR_VOL	= 28,
289 	QUIRK_TYPE_IFB_SILENCE_ON_EMPTY		= 29,
290 	QUIRK_TYPE_MIXER_GET_CUR_BROKEN		= 30,
291 /* Please also edit snd_usb_audio_quirk_flag_names */
292 };
293 
294 #define QUIRK_FLAG(x)	BIT_U32(QUIRK_TYPE_ ## x)
295 
296 #define QUIRK_FLAG_GET_SAMPLE_RATE		QUIRK_FLAG(GET_SAMPLE_RATE)
297 #define QUIRK_FLAG_SHARE_MEDIA_DEVICE		QUIRK_FLAG(SHARE_MEDIA_DEVICE)
298 #define QUIRK_FLAG_ALIGN_TRANSFER		QUIRK_FLAG(ALIGN_TRANSFER)
299 #define QUIRK_FLAG_TX_LENGTH			QUIRK_FLAG(TX_LENGTH)
300 #define QUIRK_FLAG_PLAYBACK_FIRST		QUIRK_FLAG(PLAYBACK_FIRST)
301 #define QUIRK_FLAG_SKIP_CLOCK_SELECTOR		QUIRK_FLAG(SKIP_CLOCK_SELECTOR)
302 #define QUIRK_FLAG_IGNORE_CLOCK_SOURCE		QUIRK_FLAG(IGNORE_CLOCK_SOURCE)
303 #define QUIRK_FLAG_ITF_USB_DSD_DAC		QUIRK_FLAG(ITF_USB_DSD_DAC)
304 #define QUIRK_FLAG_CTL_MSG_DELAY		QUIRK_FLAG(CTL_MSG_DELAY)
305 #define QUIRK_FLAG_CTL_MSG_DELAY_1M		QUIRK_FLAG(CTL_MSG_DELAY_1M)
306 #define QUIRK_FLAG_CTL_MSG_DELAY_5M		QUIRK_FLAG(CTL_MSG_DELAY_5M)
307 #define QUIRK_FLAG_IFACE_DELAY			QUIRK_FLAG(IFACE_DELAY)
308 #define QUIRK_FLAG_VALIDATE_RATES		QUIRK_FLAG(VALIDATE_RATES)
309 #define QUIRK_FLAG_DISABLE_AUTOSUSPEND		QUIRK_FLAG(DISABLE_AUTOSUSPEND)
310 #define QUIRK_FLAG_IGNORE_CTL_ERROR		QUIRK_FLAG(IGNORE_CTL_ERROR)
311 #define QUIRK_FLAG_DSD_RAW			QUIRK_FLAG(DSD_RAW)
312 #define QUIRK_FLAG_SET_IFACE_FIRST		QUIRK_FLAG(SET_IFACE_FIRST)
313 #define QUIRK_FLAG_GENERIC_IMPLICIT_FB		QUIRK_FLAG(GENERIC_IMPLICIT_FB)
314 #define QUIRK_FLAG_SKIP_IMPLICIT_FB		QUIRK_FLAG(SKIP_IMPLICIT_FB)
315 #define QUIRK_FLAG_IFACE_SKIP_CLOSE		QUIRK_FLAG(IFACE_SKIP_CLOSE)
316 #define QUIRK_FLAG_FORCE_IFACE_RESET		QUIRK_FLAG(FORCE_IFACE_RESET)
317 #define QUIRK_FLAG_FIXED_RATE			QUIRK_FLAG(FIXED_RATE)
318 #define QUIRK_FLAG_MIC_RES_16			QUIRK_FLAG(MIC_RES_16)
319 #define QUIRK_FLAG_MIC_RES_384			QUIRK_FLAG(MIC_RES_384)
320 #define QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE	QUIRK_FLAG(MIXER_PLAYBACK_MIN_MUTE)
321 #define QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE	QUIRK_FLAG(MIXER_CAPTURE_MIN_MUTE)
322 #define QUIRK_FLAG_SKIP_IFACE_SETUP		QUIRK_FLAG(SKIP_IFACE_SETUP)
323 #define QUIRK_FLAG_MIXER_PLAYBACK_LINEAR_VOL	QUIRK_FLAG(MIXER_PLAYBACK_LINEAR_VOL)
324 #define QUIRK_FLAG_MIXER_CAPTURE_LINEAR_VOL	QUIRK_FLAG(MIXER_CAPTURE_LINEAR_VOL)
325 #define QUIRK_FLAG_IFB_SILENCE_ON_EMPTY		QUIRK_FLAG(IFB_SILENCE_ON_EMPTY)
326 #define QUIRK_FLAG_MIXER_GET_CUR_BROKEN		QUIRK_FLAG(MIXER_GET_CUR_BROKEN)
327 
328 #endif /* __USBAUDIO_H */
329