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[] = {
49 /* FIXME: check the availability of generic matching */
50 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2030, 0x81, 3), /* M-Audio Fast Track C400 */
51 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2031, 0x81, 3), /* M-Audio Fast Track C600 */
52 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2080, 0x81, 2), /* M-Audio FastTrack Ultra */
53 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2081, 0x81, 2), /* M-Audio FastTrack Ultra */
54 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */
55 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */
56 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */
57 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */
58 IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */
59 IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
60 IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
61 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
62 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
64 /* Special matching */
65 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
66 .type = IMPLICIT_FB_NONE }, /* MicroBook IIc */
67 /* ep = 0x84, ifnum = 0 */
68 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
69 .type = IMPLICIT_FB_FIXED,
70 .ep_num = 0x84, .iface = 0 }, /* MOTU MicroBook II */
75 /* Implicit feedback quirk table for capture: only FIXED type */
76 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
80 /* set up sync EP information on the audioformat */
81 static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
82 struct audioformat *fmt,
83 int ep, int ep_idx, int ifnum,
84 const struct usb_host_interface *alts)
86 struct usb_interface *iface;
89 iface = usb_ifnum_to_if(chip->dev, ifnum);
90 if (!iface || iface->num_altsetting < 2)
92 alts = &iface->altsetting[1];
96 fmt->sync_iface = ifnum;
97 fmt->sync_altsetting = alts->desc.bAlternateSetting;
98 fmt->sync_ep_idx = ep_idx;
101 "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
102 fmt->iface, fmt->altsetting,
103 (ep & USB_DIR_IN) ? "playback" : "capture",
104 fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
108 /* Check whether the given UAC2 iface:altset points to an implicit fb source */
109 static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
110 struct audioformat *fmt,
112 unsigned int altsetting)
114 struct usb_host_interface *alts;
115 struct usb_endpoint_descriptor *epd;
117 alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
120 if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
121 alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
122 alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
123 alts->desc.bNumEndpoints < 1)
125 epd = get_endpoint(alts, 0);
126 if (!usb_endpoint_is_isoc_in(epd) ||
127 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
128 USB_ENDPOINT_USAGE_IMPLICIT_FB)
130 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
134 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
136 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
137 (alts->desc.bInterfaceSubClass != 2 &&
138 alts->desc.bInterfaceProtocol != 2) ||
139 alts->desc.bNumEndpoints < 1)
144 /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
145 static int add_roland_implicit_fb(struct snd_usb_audio *chip,
146 struct audioformat *fmt,
147 struct usb_host_interface *alts)
149 struct usb_endpoint_descriptor *epd;
151 if (!roland_sanity_check_iface(alts))
153 /* only when both streams are with ASYNC type */
154 epd = get_endpoint(alts, 0);
155 if (!usb_endpoint_is_isoc_out(epd) ||
156 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
159 /* check capture EP */
160 alts = snd_usb_get_host_interface(chip,
161 alts->desc.bInterfaceNumber + 1,
162 alts->desc.bAlternateSetting);
163 if (!alts || !roland_sanity_check_iface(alts))
165 epd = get_endpoint(alts, 0);
166 if (!usb_endpoint_is_isoc_in(epd) ||
167 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
169 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
170 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
171 alts->desc.bInterfaceNumber, alts);
174 /* capture quirk for Roland device; always full-duplex */
175 static int add_roland_capture_quirk(struct snd_usb_audio *chip,
176 struct audioformat *fmt,
177 struct usb_host_interface *alts)
179 struct usb_endpoint_descriptor *epd;
181 if (!roland_sanity_check_iface(alts))
183 epd = get_endpoint(alts, 0);
184 if (!usb_endpoint_is_isoc_in(epd) ||
185 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
188 alts = snd_usb_get_host_interface(chip,
189 alts->desc.bInterfaceNumber - 1,
190 alts->desc.bAlternateSetting);
191 if (!alts || !roland_sanity_check_iface(alts))
193 epd = get_endpoint(alts, 0);
194 if (!usb_endpoint_is_isoc_out(epd))
196 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
197 alts->desc.bInterfaceNumber, alts);
200 /* Playback and capture EPs on Pioneer devices share the same iface/altset
201 * for the implicit feedback operation
203 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
204 struct usb_host_interface *alts)
207 struct usb_endpoint_descriptor *epd;
209 if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
210 USB_ID_VENDOR(chip->usb_id) != 0x08e4)
212 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
214 if (alts->desc.bNumEndpoints != 2)
217 epd = get_endpoint(alts, 0);
218 if (!usb_endpoint_is_isoc_out(epd) ||
219 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
222 epd = get_endpoint(alts, 1);
223 if (!usb_endpoint_is_isoc_in(epd) ||
224 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
225 ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
226 USB_ENDPOINT_USAGE_DATA &&
227 (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
228 USB_ENDPOINT_USAGE_IMPLICIT_FB))
234 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
235 struct audioformat *fmt,
236 int iface, int altset)
238 struct usb_host_interface *alts;
239 struct usb_endpoint_descriptor *epd;
241 alts = snd_usb_get_host_interface(chip, iface, altset);
245 if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
246 alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
247 alts->desc.bNumEndpoints < 1)
249 epd = get_endpoint(alts, 0);
250 if (!usb_endpoint_is_isoc_in(epd) ||
251 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
253 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
257 /* More generic quirk: look for the sync EP next to the data EP */
258 static int add_generic_implicit_fb(struct snd_usb_audio *chip,
259 struct audioformat *fmt,
260 struct usb_host_interface *alts)
262 if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
265 if (__add_generic_implicit_fb(chip, fmt,
266 alts->desc.bInterfaceNumber + 1,
267 alts->desc.bAlternateSetting))
269 return __add_generic_implicit_fb(chip, fmt,
270 alts->desc.bInterfaceNumber - 1,
271 alts->desc.bAlternateSetting);
274 static const struct snd_usb_implicit_fb_match *
275 find_implicit_fb_entry(struct snd_usb_audio *chip,
276 const struct snd_usb_implicit_fb_match *match,
277 const struct usb_host_interface *alts)
279 for (; match->id; match++)
280 if (match->id == chip->usb_id &&
281 (!match->iface_class ||
282 (alts->desc.bInterfaceClass == match->iface_class)))
288 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
289 * applies. Returns 1 if a quirk was found.
291 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
292 struct audioformat *fmt,
293 struct usb_host_interface *alts)
295 const struct snd_usb_implicit_fb_match *p;
296 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
298 p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
301 case IMPLICIT_FB_GENERIC:
302 return add_generic_implicit_fb(chip, fmt, alts);
303 case IMPLICIT_FB_NONE:
304 return 0; /* No quirk */
305 case IMPLICIT_FB_FIXED:
306 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
311 /* Special handling for devices with capture quirks */
312 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
315 case IMPLICIT_FB_FIXED:
316 return 0; /* no quirk */
317 case IMPLICIT_FB_BOTH:
318 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
319 return add_generic_implicit_fb(chip, fmt, alts);
323 /* Generic UAC2 implicit feedback */
324 if (attr == USB_ENDPOINT_SYNC_ASYNC &&
325 alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
326 alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
327 alts->desc.bNumEndpoints == 1) {
328 if (add_generic_uac2_implicit_fb(chip, fmt,
329 alts->desc.bInterfaceNumber + 1,
330 alts->desc.bAlternateSetting))
334 /* Roland/BOSS implicit feedback with vendor spec class */
335 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
336 if (add_roland_implicit_fb(chip, fmt, alts) > 0)
340 /* Pioneer devices with vendor spec class */
341 if (is_pioneer_implicit_fb(chip, alts)) {
342 chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
343 return add_implicit_fb_sync_ep(chip, fmt,
344 get_endpoint(alts, 1)->bEndpointAddress,
345 1, alts->desc.bInterfaceNumber,
349 /* Try the generic implicit fb if available */
350 if (chip->generic_implicit_fb ||
351 (chip->quirk_flags & QUIRK_FLAG_GENERIC_IMPLICIT_FB))
352 return add_generic_implicit_fb(chip, fmt, alts);
358 /* same for capture, but only handling FIXED entry */
359 static int audioformat_capture_quirk(struct snd_usb_audio *chip,
360 struct audioformat *fmt,
361 struct usb_host_interface *alts)
363 const struct snd_usb_implicit_fb_match *p;
365 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
366 if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
367 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
370 /* Roland/BOSS need full-duplex streams */
371 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
372 if (add_roland_capture_quirk(chip, fmt, alts) > 0)
376 if (is_pioneer_implicit_fb(chip, alts))
377 return 1; /* skip the quirk, also don't handle generic sync EP */
382 * Parse altset and set up implicit feedback endpoint on the audioformat
384 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
385 struct audioformat *fmt,
386 struct usb_host_interface *alts)
388 if (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB)
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) {