1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Special handling for implicit feedback mode
6 #include <linux/init.h>
8 #include <linux/usb/audio.h>
9 #include <linux/usb/audio-v2.h>
11 #include <sound/core.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
24 IMPLICIT_FB_BOTH, /* generic playback + capture (for BOSS) */
27 struct snd_usb_implicit_fb_match {
29 unsigned int iface_class;
35 #define IMPLICIT_FB_GENERIC_DEV(vend, prod) \
36 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_GENERIC }
37 #define IMPLICIT_FB_FIXED_DEV(vend, prod, ep, ifnum) \
38 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_FIXED, .ep_num = (ep),\
40 #define IMPLICIT_FB_BOTH_DEV(vend, prod, ep, ifnum) \
41 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_BOTH, .ep_num = (ep),\
43 #define IMPLICIT_FB_SKIP_DEV(vend, prod) \
44 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_NONE }
46 /* Implicit feedback quirk table for playback */
47 static const struct snd_usb_implicit_fb_match playback_implicit_fb_quirks[] = {
48 /* Generic matching */
49 IMPLICIT_FB_GENERIC_DEV(0x0499, 0x1509), /* Steinberg UR22 */
50 IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2080), /* M-Audio FastTrack Ultra */
51 IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2081), /* M-Audio FastTrack Ultra */
52 IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2030), /* M-Audio Fast Track C400 */
53 IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2031), /* M-Audio Fast Track C600 */
56 /* FIXME: check the availability of generic matching */
57 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */
58 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */
59 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */
60 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */
61 IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */
62 IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
63 IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
64 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
65 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
67 /* Special matching */
68 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
69 .type = IMPLICIT_FB_NONE }, /* MicroBook IIc */
70 /* ep = 0x84, ifnum = 0 */
71 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
72 .type = IMPLICIT_FB_FIXED,
73 .ep_num = 0x84, .iface = 0 }, /* MOTU MicroBook II */
78 /* Implicit feedback quirk table for capture: only FIXED type */
79 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
83 /* set up sync EP information on the audioformat */
84 static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
85 struct audioformat *fmt,
86 int ep, int ep_idx, int ifnum,
87 const struct usb_host_interface *alts)
89 struct usb_interface *iface;
92 iface = usb_ifnum_to_if(chip->dev, ifnum);
93 if (!iface || iface->num_altsetting < 2)
95 alts = &iface->altsetting[1];
99 fmt->sync_iface = ifnum;
100 fmt->sync_altsetting = alts->desc.bAlternateSetting;
101 fmt->sync_ep_idx = ep_idx;
102 fmt->implicit_fb = 1;
104 "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
105 fmt->iface, fmt->altsetting,
106 (ep & USB_DIR_IN) ? "playback" : "capture",
107 fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
111 /* Check whether the given UAC2 iface:altset points to an implicit fb source */
112 static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
113 struct audioformat *fmt,
115 unsigned int altsetting)
117 struct usb_host_interface *alts;
118 struct usb_endpoint_descriptor *epd;
120 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
123 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
124 alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
125 alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
126 alts->desc.bNumEndpoints < 1)
128 epd = get_endpoint(alts, 0);
129 if (!usb_endpoint_is_isoc_in(epd) ||
130 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
131 USB_ENDPOINT_USAGE_IMPLICIT_FB)
133 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
137 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
139 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
140 (alts->desc.bInterfaceSubClass != 2 &&
141 alts->desc.bInterfaceProtocol != 2) ||
142 alts->desc.bNumEndpoints < 1)
147 /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
148 static int add_roland_implicit_fb(struct snd_usb_audio *chip,
149 struct audioformat *fmt,
150 struct usb_host_interface *alts)
152 struct usb_endpoint_descriptor *epd;
154 if (!roland_sanity_check_iface(alts))
156 /* only when both streams are with ASYNC type */
157 epd = get_endpoint(alts, 0);
158 if (!usb_endpoint_is_isoc_out(epd) ||
159 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
162 /* check capture EP */
163 alts = snd_usb_get_host_interface(chip,
164 alts->desc.bInterfaceNumber + 1,
165 alts->desc.bAlternateSetting);
166 if (!alts || !roland_sanity_check_iface(alts))
168 epd = get_endpoint(alts, 0);
169 if (!usb_endpoint_is_isoc_in(epd) ||
170 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
172 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
173 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
174 alts->desc.bInterfaceNumber, alts);
177 /* capture quirk for Roland device; always full-duplex */
178 static int add_roland_capture_quirk(struct snd_usb_audio *chip,
179 struct audioformat *fmt,
180 struct usb_host_interface *alts)
182 struct usb_endpoint_descriptor *epd;
184 if (!roland_sanity_check_iface(alts))
186 epd = get_endpoint(alts, 0);
187 if (!usb_endpoint_is_isoc_in(epd) ||
188 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
191 alts = snd_usb_get_host_interface(chip,
192 alts->desc.bInterfaceNumber - 1,
193 alts->desc.bAlternateSetting);
194 if (!alts || !roland_sanity_check_iface(alts))
196 epd = get_endpoint(alts, 0);
197 if (!usb_endpoint_is_isoc_out(epd))
199 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
200 alts->desc.bInterfaceNumber, alts);
203 /* Playback and capture EPs on Pioneer devices share the same iface/altset
204 * for the implicit feedback operation
206 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
207 struct usb_host_interface *alts)
210 struct usb_endpoint_descriptor *epd;
212 if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
213 USB_ID_VENDOR(chip->usb_id) != 0x08e4)
215 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
217 if (alts->desc.bNumEndpoints != 2)
220 epd = get_endpoint(alts, 0);
221 if (!usb_endpoint_is_isoc_out(epd) ||
222 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
225 epd = get_endpoint(alts, 1);
226 if (!usb_endpoint_is_isoc_in(epd) ||
227 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
228 ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
229 USB_ENDPOINT_USAGE_DATA &&
230 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
231 USB_ENDPOINT_USAGE_IMPLICIT_FB))
237 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
238 struct audioformat *fmt,
239 int iface, int altset)
241 struct usb_host_interface *alts;
242 struct usb_endpoint_descriptor *epd;
244 alts = snd_usb_get_host_interface(chip, iface, altset);
248 if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
249 alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
250 alts->desc.bNumEndpoints < 1)
252 epd = get_endpoint(alts, 0);
253 if (!usb_endpoint_is_isoc_in(epd) ||
254 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
256 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
260 /* More generic quirk: look for the sync EP next to the data EP */
261 static int add_generic_implicit_fb(struct snd_usb_audio *chip,
262 struct audioformat *fmt,
263 struct usb_host_interface *alts)
265 if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
268 if (__add_generic_implicit_fb(chip, fmt,
269 alts->desc.bInterfaceNumber + 1,
270 alts->desc.bAlternateSetting))
272 return __add_generic_implicit_fb(chip, fmt,
273 alts->desc.bInterfaceNumber - 1,
274 alts->desc.bAlternateSetting);
277 static const struct snd_usb_implicit_fb_match *
278 find_implicit_fb_entry(struct snd_usb_audio *chip,
279 const struct snd_usb_implicit_fb_match *match,
280 const struct usb_host_interface *alts)
282 for (; match->id; match++)
283 if (match->id == chip->usb_id &&
284 (!match->iface_class ||
285 (alts->desc.bInterfaceClass == match->iface_class)))
291 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
292 * applies. Returns 1 if a quirk was found.
294 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
295 struct audioformat *fmt,
296 struct usb_host_interface *alts)
298 const struct snd_usb_implicit_fb_match *p;
299 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
301 p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
304 case IMPLICIT_FB_GENERIC:
305 return add_generic_implicit_fb(chip, fmt, alts);
306 case IMPLICIT_FB_NONE:
307 return 0; /* No quirk */
308 case IMPLICIT_FB_FIXED:
309 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
314 /* Special handling for devices with capture quirks */
315 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
318 case IMPLICIT_FB_FIXED:
319 return 0; /* no quirk */
320 case IMPLICIT_FB_BOTH:
321 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
322 return add_generic_implicit_fb(chip, fmt, alts);
326 /* Generic UAC2 implicit feedback */
327 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
328 alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
329 alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
330 alts->desc.bNumEndpoints == 1) {
331 if (add_generic_uac2_implicit_fb(chip, fmt,
332 alts->desc.bInterfaceNumber + 1,
333 alts->desc.bAlternateSetting))
337 /* Roland/BOSS implicit feedback with vendor spec class */
338 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
339 if (add_roland_implicit_fb(chip, fmt, alts) > 0)
343 /* Pioneer devices with vendor spec class */
344 if (is_pioneer_implicit_fb(chip, alts)) {
345 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
346 return add_implicit_fb_sync_ep(chip, fmt,
347 get_endpoint(alts, 1)->bEndpointAddress,
348 1, alts->desc.bInterfaceNumber,
352 /* Try the generic implicit fb if available */
353 if (chip->generic_implicit_fb)
354 return add_generic_implicit_fb(chip, fmt, alts);
360 /* same for capture, but only handling FIXED entry */
361 static int audioformat_capture_quirk(struct snd_usb_audio *chip,
362 struct audioformat *fmt,
363 struct usb_host_interface *alts)
365 const struct snd_usb_implicit_fb_match *p;
367 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
368 if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
369 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
372 /* Roland/BOSS need full-duplex streams */
373 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
374 if (add_roland_capture_quirk(chip, fmt, alts) > 0)
378 if (is_pioneer_implicit_fb(chip, alts))
379 return 1; /* skip the quirk, also don't handle generic sync EP */
384 * Parse altset and set up implicit feedback endpoint on the audioformat
386 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
387 struct audioformat *fmt,
388 struct usb_host_interface *alts)
390 if (fmt->endpoint & USB_DIR_IN)
391 return audioformat_capture_quirk(chip, fmt, alts);
393 return audioformat_implicit_fb_quirk(chip, fmt, alts);
397 * Return the score of matching two audioformats.
398 * Veto the audioformat if:
399 * - It has no channels for some reason.
400 * - Requested PCM format is not supported.
401 * - Requested sample rate is not supported.
403 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
404 const struct audioformat *fp,
405 int rate, int channels,
406 snd_pcm_format_t pcm_format)
410 if (fp->channels < 1)
413 if (!(fp->formats & pcm_format_to_bits(pcm_format)))
416 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
417 if (rate < fp->rate_min || rate > fp->rate_max)
420 for (i = 0; i < fp->nr_rates; i++) {
421 if (fp->rate_table[i] == rate)
424 if (i >= fp->nr_rates)
429 if (fp->channels == channels)
435 static struct snd_usb_substream *
436 find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
439 struct snd_usb_stream *as;
440 struct snd_usb_substream *subs;
442 list_for_each_entry(as, &chip->pcm_list, list) {
443 subs = &as->substream[stream];
444 if (as->fmt_type == fmt_type && subs->ep_num == ep_num)
452 * Return the audioformat that is suitable for the implicit fb
454 const struct audioformat *
455 snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
456 const struct audioformat *target,
457 const struct snd_pcm_hw_params *params,
460 struct snd_usb_substream *subs;
461 const struct audioformat *fp, *sync_fmt = NULL;
462 int score, high_score;
464 /* Use the original audioformat as fallback for the shared altset */
465 if (target->iface == target->sync_iface &&
466 target->altsetting == target->sync_altsetting)
469 subs = find_matching_substream(chip, stream, target->sync_ep,
475 list_for_each_entry(fp, &subs->fmt_list, list) {
476 score = match_endpoint_audioformats(subs, fp,
478 params_channels(params),
479 params_format(params));
480 if (score > high_score) {