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(0x1397, 0x0001, 0x81, 1), /* Behringer UFX1604 */
58 IMPLICIT_FB_FIXED_DEV(0x1397, 0x0002, 0x81, 1), /* Behringer UFX1204 */
59 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */
60 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */
61 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */
62 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */
63 IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */
64 IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
65 IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
66 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
67 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
69 /* Special matching */
70 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
71 .type = IMPLICIT_FB_NONE }, /* MicroBook IIc */
72 /* ep = 0x84, ifnum = 0 */
73 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
74 .type = IMPLICIT_FB_FIXED,
75 .ep_num = 0x84, .iface = 0 }, /* MOTU MicroBook II */
80 /* Implicit feedback quirk table for capture: only FIXED type */
81 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
85 /* set up sync EP information on the audioformat */
86 static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
87 struct audioformat *fmt,
88 int ep, int ep_idx, int ifnum,
89 const struct usb_host_interface *alts)
91 struct usb_interface *iface;
94 iface = usb_ifnum_to_if(chip->dev, ifnum);
95 if (!iface || iface->num_altsetting < 2)
97 alts = &iface->altsetting[1];
101 fmt->sync_iface = ifnum;
102 fmt->sync_altsetting = alts->desc.bAlternateSetting;
103 fmt->sync_ep_idx = ep_idx;
104 fmt->implicit_fb = 1;
106 "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
107 fmt->iface, fmt->altsetting,
108 (ep & USB_DIR_IN) ? "playback" : "capture",
109 fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
113 /* Check whether the given UAC2 iface:altset points to an implicit fb source */
114 static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
115 struct audioformat *fmt,
117 unsigned int altsetting)
119 struct usb_host_interface *alts;
120 struct usb_endpoint_descriptor *epd;
122 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
125 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
126 alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
127 alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
128 alts->desc.bNumEndpoints < 1)
130 epd = get_endpoint(alts, 0);
131 if (!usb_endpoint_is_isoc_in(epd) ||
132 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
133 USB_ENDPOINT_USAGE_IMPLICIT_FB)
135 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
139 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
141 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
142 (alts->desc.bInterfaceSubClass != 2 &&
143 alts->desc.bInterfaceProtocol != 2) ||
144 alts->desc.bNumEndpoints < 1)
149 /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
150 static int add_roland_implicit_fb(struct snd_usb_audio *chip,
151 struct audioformat *fmt,
152 struct usb_host_interface *alts)
154 struct usb_endpoint_descriptor *epd;
156 if (!roland_sanity_check_iface(alts))
158 /* only when both streams are with ASYNC type */
159 epd = get_endpoint(alts, 0);
160 if (!usb_endpoint_is_isoc_out(epd) ||
161 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
164 /* check capture EP */
165 alts = snd_usb_get_host_interface(chip,
166 alts->desc.bInterfaceNumber + 1,
167 alts->desc.bAlternateSetting);
168 if (!alts || !roland_sanity_check_iface(alts))
170 epd = get_endpoint(alts, 0);
171 if (!usb_endpoint_is_isoc_in(epd) ||
172 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
174 chip->playback_first = 1;
175 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
176 alts->desc.bInterfaceNumber, alts);
179 /* capture quirk for Roland device; always full-duplex */
180 static int add_roland_capture_quirk(struct snd_usb_audio *chip,
181 struct audioformat *fmt,
182 struct usb_host_interface *alts)
184 struct usb_endpoint_descriptor *epd;
186 if (!roland_sanity_check_iface(alts))
188 epd = get_endpoint(alts, 0);
189 if (!usb_endpoint_is_isoc_in(epd) ||
190 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
193 alts = snd_usb_get_host_interface(chip,
194 alts->desc.bInterfaceNumber - 1,
195 alts->desc.bAlternateSetting);
196 if (!alts || !roland_sanity_check_iface(alts))
198 epd = get_endpoint(alts, 0);
199 if (!usb_endpoint_is_isoc_out(epd))
201 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
202 alts->desc.bInterfaceNumber, alts);
205 /* Playback and capture EPs on Pioneer devices share the same iface/altset
206 * for the implicit feedback operation
208 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
209 struct usb_host_interface *alts)
212 struct usb_endpoint_descriptor *epd;
214 if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
215 USB_ID_VENDOR(chip->usb_id) != 0x08e4)
217 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
219 if (alts->desc.bNumEndpoints != 2)
222 epd = get_endpoint(alts, 0);
223 if (!usb_endpoint_is_isoc_out(epd) ||
224 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
227 epd = get_endpoint(alts, 1);
228 if (!usb_endpoint_is_isoc_in(epd) ||
229 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
230 ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
231 USB_ENDPOINT_USAGE_DATA &&
232 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
233 USB_ENDPOINT_USAGE_IMPLICIT_FB))
239 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
240 struct audioformat *fmt,
241 int iface, int altset)
243 struct usb_host_interface *alts;
244 struct usb_endpoint_descriptor *epd;
246 alts = snd_usb_get_host_interface(chip, iface, altset);
250 if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
251 alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
252 alts->desc.bNumEndpoints < 1)
254 epd = get_endpoint(alts, 0);
255 if (!usb_endpoint_is_isoc_in(epd) ||
256 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
258 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
262 /* More generic quirk: look for the sync EP next to the data EP */
263 static int add_generic_implicit_fb(struct snd_usb_audio *chip,
264 struct audioformat *fmt,
265 struct usb_host_interface *alts)
267 if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
270 if (__add_generic_implicit_fb(chip, fmt,
271 alts->desc.bInterfaceNumber + 1,
272 alts->desc.bAlternateSetting))
274 return __add_generic_implicit_fb(chip, fmt,
275 alts->desc.bInterfaceNumber - 1,
276 alts->desc.bAlternateSetting);
279 static const struct snd_usb_implicit_fb_match *
280 find_implicit_fb_entry(struct snd_usb_audio *chip,
281 const struct snd_usb_implicit_fb_match *match,
282 const struct usb_host_interface *alts)
284 for (; match->id; match++)
285 if (match->id == chip->usb_id &&
286 (!match->iface_class ||
287 (alts->desc.bInterfaceClass == match->iface_class)))
293 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
294 * applies. Returns 1 if a quirk was found.
296 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
297 struct audioformat *fmt,
298 struct usb_host_interface *alts)
300 const struct snd_usb_implicit_fb_match *p;
301 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
303 p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
306 case IMPLICIT_FB_GENERIC:
307 return add_generic_implicit_fb(chip, fmt, alts);
308 case IMPLICIT_FB_NONE:
309 return 0; /* No quirk */
310 case IMPLICIT_FB_FIXED:
311 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
316 /* Special handling for devices with capture quirks */
317 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
320 case IMPLICIT_FB_FIXED:
321 return 0; /* no quirk */
322 case IMPLICIT_FB_BOTH:
323 chip->playback_first = 1;
324 return add_generic_implicit_fb(chip, fmt, alts);
328 /* Generic UAC2 implicit feedback */
329 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
330 alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
331 alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
332 alts->desc.bNumEndpoints == 1) {
333 if (add_generic_uac2_implicit_fb(chip, fmt,
334 alts->desc.bInterfaceNumber + 1,
335 alts->desc.bAlternateSetting))
339 /* Roland/BOSS implicit feedback with vendor spec class */
340 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
341 if (add_roland_implicit_fb(chip, fmt, alts) > 0)
345 /* Pioneer devices with vendor spec class */
346 if (is_pioneer_implicit_fb(chip, alts)) {
347 chip->playback_first = 1;
348 return add_implicit_fb_sync_ep(chip, fmt,
349 get_endpoint(alts, 1)->bEndpointAddress,
350 1, alts->desc.bInterfaceNumber,
354 /* Try the generic implicit fb if available */
355 if (chip->generic_implicit_fb)
356 return add_generic_implicit_fb(chip, fmt, alts);
362 /* same for capture, but only handling FIXED entry */
363 static int audioformat_capture_quirk(struct snd_usb_audio *chip,
364 struct audioformat *fmt,
365 struct usb_host_interface *alts)
367 const struct snd_usb_implicit_fb_match *p;
369 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
370 if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
371 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
374 /* Roland/BOSS need full-duplex streams */
375 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
376 if (add_roland_capture_quirk(chip, fmt, alts) > 0)
380 if (is_pioneer_implicit_fb(chip, alts))
381 return 1; /* skip the quirk, also don't handle generic sync EP */
386 * Parse altset and set up implicit feedback endpoint on the audioformat
388 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
389 struct audioformat *fmt,
390 struct usb_host_interface *alts)
392 if (fmt->endpoint & USB_DIR_IN)
393 return audioformat_capture_quirk(chip, fmt, alts);
395 return audioformat_implicit_fb_quirk(chip, fmt, alts);
399 * Return the score of matching two audioformats.
400 * Veto the audioformat if:
401 * - It has no channels for some reason.
402 * - Requested PCM format is not supported.
403 * - Requested sample rate is not supported.
405 static int match_endpoint_audioformats(struct snd_usb_substream *subs,
406 const struct audioformat *fp,
407 int rate, int channels,
408 snd_pcm_format_t pcm_format)
412 if (fp->channels < 1)
415 if (!(fp->formats & pcm_format_to_bits(pcm_format)))
418 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
419 if (rate < fp->rate_min || rate > fp->rate_max)
422 for (i = 0; i < fp->nr_rates; i++) {
423 if (fp->rate_table[i] == rate)
426 if (i >= fp->nr_rates)
431 if (fp->channels == channels)
437 static struct snd_usb_substream *
438 find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
441 struct snd_usb_stream *as;
442 struct snd_usb_substream *subs;
444 list_for_each_entry(as, &chip->pcm_list, list) {
445 subs = &as->substream[stream];
446 if (as->fmt_type == fmt_type && subs->ep_num == ep_num)
454 * Return the audioformat that is suitable for the implicit fb
456 const struct audioformat *
457 snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
458 const struct audioformat *target,
459 const struct snd_pcm_hw_params *params,
462 struct snd_usb_substream *subs;
463 const struct audioformat *fp, *sync_fmt = NULL;
464 int score, high_score;
466 /* Use the original audioformat as fallback for the shared altset */
467 if (target->iface == target->sync_iface &&
468 target->altsetting == target->sync_altsetting)
471 subs = find_matching_substream(chip, stream, target->sync_ep,
477 list_for_each_entry(fp, &subs->fmt_list, list) {
478 score = match_endpoint_audioformats(subs, fp,
480 params_channels(params),
481 params_format(params));
482 if (score > high_score) {