xref: /linux/sound/usb/usbaudio.h (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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 /* handling of USB vendor/product ID pairs as 32-bit numbers */
11 #define USB_ID(vendor, product) (((unsigned int)(vendor) << 16) | (product))
12 #define USB_ID_VENDOR(id) ((id) >> 16)
13 #define USB_ID_PRODUCT(id) ((u16)(id))
14 
15 /*
16  *
17  */
18 
19 struct media_device;
20 struct media_intf_devnode;
21 
22 #define MAX_CARD_INTERFACES	16
23 
24 /*
25  * Structure holding assosiation between Audio Control Interface
26  * and given Streaming or Midi Interface.
27  */
28 struct snd_intf_to_ctrl {
29 	u8 interface;
30 	struct usb_host_interface *ctrl_intf;
31 };
32 
33 struct snd_usb_audio {
34 	int index;
35 	struct usb_device *dev;
36 	struct snd_card *card;
37 	struct usb_interface *intf[MAX_CARD_INTERFACES];
38 	u32 usb_id;
39 	uint16_t quirk_type;
40 	struct mutex mutex;
41 	unsigned int system_suspend;
42 	atomic_t active;
43 	atomic_t shutdown;
44 	atomic_t usage_count;
45 	wait_queue_head_t shutdown_wait;
46 	unsigned int quirk_flags;
47 	unsigned int need_delayed_register:1; /* warn for delayed registration */
48 	int num_interfaces;
49 	int last_iface;
50 	int num_suspended_intf;
51 	int sample_rate_read_error;
52 
53 	int badd_profile;		/* UAC3 BADD profile */
54 
55 	struct list_head pcm_list;	/* list of pcm streams */
56 	struct list_head ep_list;	/* list of audio-related endpoints */
57 	struct list_head iface_ref_list; /* list of interface refcounts */
58 	struct list_head clock_ref_list; /* list of clock refcounts */
59 	int pcm_devs;
60 
61 	unsigned int num_rawmidis;	/* number of created rawmidi devices */
62 	struct list_head midi_list;	/* list of midi interfaces */
63 	struct list_head midi_v2_list;	/* list of MIDI 2 interfaces */
64 
65 	struct list_head mixer_list;	/* list of mixer interfaces */
66 
67 	int setup;			/* from the 'device_setup' module param */
68 	bool generic_implicit_fb;	/* from the 'implicit_fb' module param */
69 	bool autoclock;			/* from the 'autoclock' module param */
70 
71 	bool lowlatency;		/* from the 'lowlatency' module param */
72 	struct usb_host_interface *ctrl_intf;	/* the audio control interface */
73 	struct media_device *media_dev;
74 	struct media_intf_devnode *ctl_intf_media_devnode;
75 
76 	unsigned int num_intf_to_ctrl;
77 	struct snd_intf_to_ctrl intf_to_ctrl[MAX_CARD_INTERFACES];
78 };
79 
80 #define USB_AUDIO_IFACE_UNUSED	((void *)-1L)
81 
82 #define usb_audio_err(chip, fmt, args...) \
83 	dev_err(&(chip)->dev->dev, fmt, ##args)
84 #define usb_audio_err_ratelimited(chip, fmt, args...) \
85 	dev_err_ratelimited(&(chip)->dev->dev, fmt, ##args)
86 #define usb_audio_warn(chip, fmt, args...) \
87 	dev_warn(&(chip)->dev->dev, fmt, ##args)
88 #define usb_audio_info(chip, fmt, args...) \
89 	dev_info(&(chip)->dev->dev, fmt, ##args)
90 #define usb_audio_dbg(chip, fmt, args...) \
91 	dev_dbg(&(chip)->dev->dev, fmt, ##args)
92 
93 /*
94  * Information about devices with broken descriptors
95  */
96 
97 /* special values for .ifnum */
98 #define QUIRK_NODEV_INTERFACE		-3	/* return -ENODEV */
99 #define QUIRK_NO_INTERFACE		-2
100 #define QUIRK_ANY_INTERFACE		-1
101 
102 enum quirk_type {
103 	QUIRK_IGNORE_INTERFACE,
104 	QUIRK_COMPOSITE,
105 	QUIRK_AUTODETECT,
106 	QUIRK_MIDI_STANDARD_INTERFACE,
107 	QUIRK_MIDI_FIXED_ENDPOINT,
108 	QUIRK_MIDI_YAMAHA,
109 	QUIRK_MIDI_ROLAND,
110 	QUIRK_MIDI_MIDIMAN,
111 	QUIRK_MIDI_NOVATION,
112 	QUIRK_MIDI_RAW_BYTES,
113 	QUIRK_MIDI_EMAGIC,
114 	QUIRK_MIDI_CME,
115 	QUIRK_MIDI_AKAI,
116 	QUIRK_MIDI_US122L,
117 	QUIRK_MIDI_FTDI,
118 	QUIRK_MIDI_CH345,
119 	QUIRK_AUDIO_STANDARD_INTERFACE,
120 	QUIRK_AUDIO_FIXED_ENDPOINT,
121 	QUIRK_AUDIO_EDIROL_UAXX,
122 	QUIRK_AUDIO_STANDARD_MIXER,
123 
124 	QUIRK_TYPE_COUNT
125 };
126 
127 struct snd_usb_audio_quirk {
128 	const char *vendor_name;
129 	const char *product_name;
130 	int16_t ifnum;
131 	uint16_t type;
132 	const void *data;
133 };
134 
135 #define combine_word(s)    ((*(s)) | ((unsigned int)(s)[1] << 8))
136 #define combine_triple(s)  (combine_word(s) | ((unsigned int)(s)[2] << 16))
137 #define combine_quad(s)    (combine_triple(s) | ((unsigned int)(s)[3] << 24))
138 
139 int snd_usb_lock_shutdown(struct snd_usb_audio *chip);
140 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip);
141 
142 /* auto-cleanup */
143 struct __snd_usb_lock {
144 	struct snd_usb_audio *chip;
145 	int err;
146 };
147 
148 static inline struct __snd_usb_lock __snd_usb_lock_shutdown(struct snd_usb_audio *chip)
149 {
150 	struct __snd_usb_lock T = { .chip = chip };
151 	T.err = snd_usb_lock_shutdown(chip);
152 	return T;
153 }
154 
155 static inline void __snd_usb_unlock_shutdown(struct __snd_usb_lock *lock)
156 {
157 	if (!lock->err)
158 		snd_usb_unlock_shutdown(lock->chip);
159 }
160 
161 DEFINE_CLASS(snd_usb_lock, struct __snd_usb_lock,
162 	     __snd_usb_unlock_shutdown(&(_T)), __snd_usb_lock_shutdown(chip),
163 	     struct snd_usb_audio *chip)
164 
165 extern bool snd_usb_use_vmalloc;
166 extern bool snd_usb_skip_validation;
167 
168 /*
169  * Driver behavior quirk flags, stored in chip->quirk_flags
170  *
171  * QUIRK_FLAG_GET_SAMPLE_RATE:
172  *  Skip reading sample rate for devices, as some devices behave inconsistently
173  *  or return error
174  * QUIRK_FLAG_SHARE_MEDIA_DEVICE:
175  *  Create Media Controller API entries
176  * QUIRK_FLAG_ALIGN_TRANSFER:
177  *  Allow alignment on audio sub-slot (channel samples) rather than on audio
178  *  slots (audio frames)
179  * QUIRK_TX_LENGTH:
180  *  Add length specifier to transfers
181  * QUIRK_FLAG_PLAYBACK_FIRST:
182  *  Start playback stream at first even in implement feedback mode
183  * QUIRK_FLAG_SKIP_CLOCK_SELECTOR:
184  *  Skip clock selector setup; the device may reset to invalid state
185  * QUIRK_FLAG_IGNORE_CLOCK_SOURCE:
186  *  Ignore errors from clock source search; i.e. hardcoded clock
187  * QUIRK_FLAG_ITF_USB_DSD_DAC:
188  *  Indicates the device is for ITF-USB DSD based DACs that need a vendor cmd
189  *  to switch between PCM and native DSD mode
190  * QUIRK_FLAG_CTL_MSG_DELAY:
191  *  Add a delay of 20ms at each control message handling
192  * QUIRK_FLAG_CTL_MSG_DELAY_1M:
193  *  Add a delay of 1-2ms at each control message handling
194  * QUIRK_FLAG_CTL_MSG_DELAY_5M:
195  *  Add a delay of 5-6ms at each control message handling
196  * QUIRK_FLAG_IFACE_DELAY:
197  *  Add a delay of 50ms at each interface setup
198  * QUIRK_FLAG_VALIDATE_RATES:
199  *  Perform sample rate validations at probe
200  * QUIRK_FLAG_DISABLE_AUTOSUSPEND:
201  *  Disable runtime PM autosuspend
202  * QUIRK_FLAG_IGNORE_CTL_ERROR:
203  *  Ignore errors for mixer access
204  * QUIRK_FLAG_DSD_RAW:
205  *  Support generic DSD raw U32_BE format
206  * QUIRK_FLAG_SET_IFACE_FIRST:
207  *  Set up the interface at first like UAC1
208  * QUIRK_FLAG_GENERIC_IMPLICIT_FB
209  *  Apply the generic implicit feedback sync mode (same as implicit_fb=1 option)
210  * QUIRK_FLAG_SKIP_IMPLICIT_FB
211  *  Don't apply implicit feedback sync mode
212  * QUIRK_FLAG_IFACE_SKIP_CLOSE
213  *  Don't closed interface during setting sample rate
214  * QUIRK_FLAG_FORCE_IFACE_RESET
215  *  Force an interface reset whenever stopping & restarting a stream
216  *  (e.g. after xrun)
217  * QUIRK_FLAG_FIXED_RATE
218  *  Do not set PCM rate (frequency) when only one rate is available
219  *  for the given endpoint.
220  * QUIRK_FLAG_MIC_RES_16 and QUIRK_FLAG_MIC_RES_384
221  *  Set the fixed resolution for Mic Capture Volume (mostly for webcams)
222  * QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE
223  *  Set minimum volume control value as mute for devices where the lowest
224  *  playback value represents muted state instead of minimum audible volume
225  * QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE
226  *  Similar to QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE, but for capture streams
227  */
228 
229 enum {
230 	QUIRK_TYPE_GET_SAMPLE_RATE		= 0,
231 	QUIRK_TYPE_SHARE_MEDIA_DEVICE		= 1,
232 	QUIRK_TYPE_ALIGN_TRANSFER		= 2,
233 	QUIRK_TYPE_TX_LENGTH			= 3,
234 	QUIRK_TYPE_PLAYBACK_FIRST		= 4,
235 	QUIRK_TYPE_SKIP_CLOCK_SELECTOR		= 5,
236 	QUIRK_TYPE_IGNORE_CLOCK_SOURCE		= 6,
237 	QUIRK_TYPE_ITF_USB_DSD_DAC		= 7,
238 	QUIRK_TYPE_CTL_MSG_DELAY		= 8,
239 	QUIRK_TYPE_CTL_MSG_DELAY_1M		= 9,
240 	QUIRK_TYPE_CTL_MSG_DELAY_5M		= 10,
241 	QUIRK_TYPE_IFACE_DELAY			= 11,
242 	QUIRK_TYPE_VALIDATE_RATES		= 12,
243 	QUIRK_TYPE_DISABLE_AUTOSUSPEND		= 13,
244 	QUIRK_TYPE_IGNORE_CTL_ERROR		= 14,
245 	QUIRK_TYPE_DSD_RAW			= 15,
246 	QUIRK_TYPE_SET_IFACE_FIRST		= 16,
247 	QUIRK_TYPE_GENERIC_IMPLICIT_FB		= 17,
248 	QUIRK_TYPE_SKIP_IMPLICIT_FB		= 18,
249 	QUIRK_TYPE_IFACE_SKIP_CLOSE		= 19,
250 	QUIRK_TYPE_FORCE_IFACE_RESET		= 20,
251 	QUIRK_TYPE_FIXED_RATE			= 21,
252 	QUIRK_TYPE_MIC_RES_16			= 22,
253 	QUIRK_TYPE_MIC_RES_384			= 23,
254 	QUIRK_TYPE_MIXER_PLAYBACK_MIN_MUTE	= 24,
255 	QUIRK_TYPE_MIXER_CAPTURE_MIN_MUTE	= 25,
256 /* Please also edit snd_usb_audio_quirk_flag_names */
257 };
258 
259 #define QUIRK_FLAG(x)	BIT_U32(QUIRK_TYPE_ ## x)
260 
261 #define QUIRK_FLAG_GET_SAMPLE_RATE		QUIRK_FLAG(GET_SAMPLE_RATE)
262 #define QUIRK_FLAG_SHARE_MEDIA_DEVICE		QUIRK_FLAG(SHARE_MEDIA_DEVICE)
263 #define QUIRK_FLAG_ALIGN_TRANSFER		QUIRK_FLAG(ALIGN_TRANSFER)
264 #define QUIRK_FLAG_TX_LENGTH			QUIRK_FLAG(TX_LENGTH)
265 #define QUIRK_FLAG_PLAYBACK_FIRST		QUIRK_FLAG(PLAYBACK_FIRST)
266 #define QUIRK_FLAG_SKIP_CLOCK_SELECTOR		QUIRK_FLAG(SKIP_CLOCK_SELECTOR)
267 #define QUIRK_FLAG_IGNORE_CLOCK_SOURCE		QUIRK_FLAG(IGNORE_CLOCK_SOURCE)
268 #define QUIRK_FLAG_ITF_USB_DSD_DAC		QUIRK_FLAG(ITF_USB_DSD_DAC)
269 #define QUIRK_FLAG_CTL_MSG_DELAY		QUIRK_FLAG(CTL_MSG_DELAY)
270 #define QUIRK_FLAG_CTL_MSG_DELAY_1M		QUIRK_FLAG(CTL_MSG_DELAY_1M)
271 #define QUIRK_FLAG_CTL_MSG_DELAY_5M		QUIRK_FLAG(CTL_MSG_DELAY_5M)
272 #define QUIRK_FLAG_IFACE_DELAY			QUIRK_FLAG(IFACE_DELAY)
273 #define QUIRK_FLAG_VALIDATE_RATES		QUIRK_FLAG(VALIDATE_RATES)
274 #define QUIRK_FLAG_DISABLE_AUTOSUSPEND		QUIRK_FLAG(DISABLE_AUTOSUSPEND)
275 #define QUIRK_FLAG_IGNORE_CTL_ERROR		QUIRK_FLAG(IGNORE_CTL_ERROR)
276 #define QUIRK_FLAG_DSD_RAW			QUIRK_FLAG(DSD_RAW)
277 #define QUIRK_FLAG_SET_IFACE_FIRST		QUIRK_FLAG(SET_IFACE_FIRST)
278 #define QUIRK_FLAG_GENERIC_IMPLICIT_FB		QUIRK_FLAG(GENERIC_IMPLICIT_FB)
279 #define QUIRK_FLAG_SKIP_IMPLICIT_FB		QUIRK_FLAG(SKIP_IMPLICIT_FB)
280 #define QUIRK_FLAG_IFACE_SKIP_CLOSE		QUIRK_FLAG(IFACE_SKIP_CLOSE)
281 #define QUIRK_FLAG_FORCE_IFACE_RESET		QUIRK_FLAG(FORCE_IFACE_RESET)
282 #define QUIRK_FLAG_FIXED_RATE			QUIRK_FLAG(FIXED_RATE)
283 #define QUIRK_FLAG_MIC_RES_16			QUIRK_FLAG(MIC_RES_16)
284 #define QUIRK_FLAG_MIC_RES_384			QUIRK_FLAG(MIC_RES_384)
285 #define QUIRK_FLAG_MIXER_PLAYBACK_MIN_MUTE	QUIRK_FLAG(MIXER_PLAYBACK_MIN_MUTE)
286 #define QUIRK_FLAG_MIXER_CAPTURE_MIN_MUTE	QUIRK_FLAG(MIXER_CAPTURE_MIN_MUTE)
287 
288 #endif /* __USBAUDIO_H */
289