1 // SPDX-License-Identifier: GPL-2.0-or-later 2 // 3 // Special handling for implicit feedback mode 4 // 5 6 #include <linux/init.h> 7 #include <linux/usb.h> 8 #include <linux/usb/audio.h> 9 #include <linux/usb/audio-v2.h> 10 11 #include <sound/core.h> 12 #include <sound/pcm.h> 13 #include <sound/pcm_params.h> 14 15 #include "usbaudio.h" 16 #include "card.h" 17 #include "helper.h" 18 #include "pcm.h" 19 #include "implicit.h" 20 21 enum { 22 IMPLICIT_FB_NONE, 23 IMPLICIT_FB_GENERIC, 24 IMPLICIT_FB_FIXED, 25 IMPLICIT_FB_BOTH, /* generic playback + capture (for BOSS) */ 26 }; 27 28 struct snd_usb_implicit_fb_match { 29 unsigned int id; 30 unsigned int iface_class; 31 unsigned int ep_num; 32 unsigned int iface; 33 int type; 34 }; 35 36 #define IMPLICIT_FB_GENERIC_DEV(vend, prod) \ 37 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_GENERIC } 38 #define IMPLICIT_FB_FIXED_DEV(vend, prod, ep, ifnum) \ 39 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_FIXED, .ep_num = (ep),\ 40 .iface = (ifnum) } 41 #define IMPLICIT_FB_BOTH_DEV(vend, prod, ep, ifnum) \ 42 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_BOTH, .ep_num = (ep),\ 43 .iface = (ifnum) } 44 #define IMPLICIT_FB_SKIP_DEV(vend, prod) \ 45 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_NONE } 46 47 /* Implicit feedback quirk table for playback */ 48 static const struct snd_usb_implicit_fb_match playback_implicit_fb_quirks[] = { 49 /* Fixed EP */ 50 /* FIXME: check the availability of generic matching */ 51 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2030, 0x81, 3), /* M-Audio Fast Track C400 */ 52 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2031, 0x81, 3), /* M-Audio Fast Track C600 */ 53 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2080, 0x81, 2), /* M-Audio FastTrack Ultra */ 54 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2081, 0x81, 2), /* M-Audio FastTrack Ultra */ 55 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */ 56 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */ 57 IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */ 58 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */ 59 IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */ 60 IMPLICIT_FB_FIXED_DEV(0x19f7, 0x000a, 0x84, 3), /* RODE AI-1 */ 61 IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */ 62 IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */ 63 IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */ 64 IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */ 65 66 /* Special matching */ 67 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO, 68 .type = IMPLICIT_FB_NONE }, /* MicroBook IIc */ 69 /* ep = 0x84, ifnum = 0 */ 70 { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC, 71 .type = IMPLICIT_FB_FIXED, 72 .ep_num = 0x84, .iface = 0 }, /* MOTU MicroBook II */ 73 74 {} /* terminator */ 75 }; 76 77 /* Implicit feedback quirk table for capture: only FIXED type */ 78 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = { 79 IMPLICIT_FB_FIXED_DEV(0x1397, 0x0004, 0x01, 1), /* Behringer FCA1616 */ 80 {} /* terminator */ 81 }; 82 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) 88 { 89 struct usb_interface *iface; 90 91 if (!alts) { 92 iface = usb_ifnum_to_if(chip->dev, ifnum); 93 if (!iface || iface->num_altsetting < 2) 94 return 0; 95 alts = &iface->altsetting[1]; 96 } 97 98 fmt->sync_ep = ep; 99 fmt->sync_iface = ifnum; 100 fmt->sync_altsetting = alts->desc.bAlternateSetting; 101 fmt->sync_ep_idx = ep_idx; 102 fmt->implicit_fb = 1; 103 usb_audio_dbg(chip, 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); 108 return 1; 109 } 110 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, 114 unsigned int ifnum, 115 unsigned int altsetting) 116 { 117 struct usb_host_interface *alts; 118 struct usb_endpoint_descriptor *epd; 119 120 alts = snd_usb_get_host_interface(chip, ifnum, altsetting); 121 if (!alts) 122 return 0; 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) 127 return 0; 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) 132 return 0; 133 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0, 134 ifnum, alts); 135 } 136 137 static bool roland_sanity_check_iface(struct usb_host_interface *alts) 138 { 139 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC || 140 (alts->desc.bInterfaceSubClass != 2 && 141 alts->desc.bInterfaceProtocol != 2) || 142 alts->desc.bNumEndpoints < 1) 143 return false; 144 return true; 145 } 146 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) 151 { 152 struct usb_endpoint_descriptor *epd; 153 154 if (!roland_sanity_check_iface(alts)) 155 return 0; 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) 160 return 0; 161 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)) 167 return 0; 168 epd = get_endpoint(alts, 0); 169 if (!usb_endpoint_is_isoc_in(epd) || 170 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) 171 return 0; 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); 175 } 176 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) 181 { 182 struct usb_endpoint_descriptor *epd; 183 184 if (!roland_sanity_check_iface(alts)) 185 return 0; 186 epd = get_endpoint(alts, 0); 187 if (!usb_endpoint_is_isoc_in(epd) || 188 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) 189 return 0; 190 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)) 195 return 0; 196 epd = get_endpoint(alts, 0); 197 if (!usb_endpoint_is_isoc_out(epd)) 198 return 0; 199 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0, 200 alts->desc.bInterfaceNumber, alts); 201 } 202 203 /* Playback and capture EPs on Pioneer devices share the same iface/altset 204 * for the implicit feedback operation 205 */ 206 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip, 207 struct usb_host_interface *alts) 208 209 { 210 struct usb_endpoint_descriptor *epd; 211 212 if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 && 213 USB_ID_VENDOR(chip->usb_id) != 0x08e4) 214 return false; 215 if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC) 216 return false; 217 if (alts->desc.bNumEndpoints != 2) 218 return false; 219 220 epd = get_endpoint(alts, 0); 221 if (!usb_endpoint_is_isoc_out(epd) || 222 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) 223 return false; 224 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)) 232 return false; 233 234 return true; 235 } 236 237 static int __add_generic_implicit_fb(struct snd_usb_audio *chip, 238 struct audioformat *fmt, 239 int iface, int altset) 240 { 241 struct usb_host_interface *alts; 242 struct usb_endpoint_descriptor *epd; 243 244 alts = snd_usb_get_host_interface(chip, iface, altset); 245 if (!alts) 246 return 0; 247 248 if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC && 249 alts->desc.bInterfaceClass != USB_CLASS_AUDIO) || 250 alts->desc.bNumEndpoints < 1) 251 return 0; 252 epd = get_endpoint(alts, 0); 253 if (!usb_endpoint_is_isoc_in(epd) || 254 (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) 255 return 0; 256 return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0, 257 iface, alts); 258 } 259 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) 264 { 265 if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) 266 return 0; 267 268 if (__add_generic_implicit_fb(chip, fmt, 269 alts->desc.bInterfaceNumber + 1, 270 alts->desc.bAlternateSetting)) 271 return 1; 272 return __add_generic_implicit_fb(chip, fmt, 273 alts->desc.bInterfaceNumber - 1, 274 alts->desc.bAlternateSetting); 275 } 276 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) 281 { 282 for (; match->id; match++) 283 if (match->id == chip->usb_id && 284 (!match->iface_class || 285 (alts->desc.bInterfaceClass == match->iface_class))) 286 return match; 287 288 return NULL; 289 } 290 291 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk 292 * applies. Returns 1 if a quirk was found. 293 */ 294 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, 295 struct audioformat *fmt, 296 struct usb_host_interface *alts) 297 { 298 const struct snd_usb_implicit_fb_match *p; 299 unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; 300 301 p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts); 302 if (p) { 303 switch (p->type) { 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, 310 p->iface, NULL); 311 } 312 } 313 314 /* Special handling for devices with capture quirks */ 315 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts); 316 if (p) { 317 switch (p->type) { 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); 323 } 324 } 325 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)) 334 return 1; 335 } 336 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) 340 return 1; 341 } 342 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, 349 alts); 350 } 351 352 /* Try the generic implicit fb if available */ 353 if (chip->generic_implicit_fb || 354 (chip->quirk_flags & QUIRK_FLAG_GENERIC_IMPLICIT_FB)) 355 return add_generic_implicit_fb(chip, fmt, alts); 356 357 /* No quirk */ 358 return 0; 359 } 360 361 /* same for capture, but only handling FIXED entry */ 362 static int audioformat_capture_quirk(struct snd_usb_audio *chip, 363 struct audioformat *fmt, 364 struct usb_host_interface *alts) 365 { 366 const struct snd_usb_implicit_fb_match *p; 367 368 p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts); 369 if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH)) 370 return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0, 371 p->iface, NULL); 372 373 /* Roland/BOSS need full-duplex streams */ 374 if (USB_ID_VENDOR(chip->usb_id) == 0x0582) { 375 if (add_roland_capture_quirk(chip, fmt, alts) > 0) 376 return 1; 377 } 378 379 if (is_pioneer_implicit_fb(chip, alts)) 380 return 1; /* skip the quirk, also don't handle generic sync EP */ 381 return 0; 382 } 383 384 /* 385 * Parse altset and set up implicit feedback endpoint on the audioformat 386 */ 387 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip, 388 struct audioformat *fmt, 389 struct usb_host_interface *alts) 390 { 391 if (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB) 392 return 0; 393 if (fmt->endpoint & USB_DIR_IN) 394 return audioformat_capture_quirk(chip, fmt, alts); 395 else 396 return audioformat_implicit_fb_quirk(chip, fmt, alts); 397 } 398 399 /* 400 * Return the score of matching two audioformats. 401 * Veto the audioformat if: 402 * - It has no channels for some reason. 403 * - Requested PCM format is not supported. 404 * - Requested sample rate is not supported. 405 */ 406 static int match_endpoint_audioformats(struct snd_usb_substream *subs, 407 const struct audioformat *fp, 408 int rate, int channels, 409 snd_pcm_format_t pcm_format) 410 { 411 int i, score; 412 413 if (fp->channels < 1) 414 return 0; 415 416 if (!(fp->formats & pcm_format_to_bits(pcm_format))) 417 return 0; 418 419 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) { 420 if (rate < fp->rate_min || rate > fp->rate_max) 421 return 0; 422 } else { 423 for (i = 0; i < fp->nr_rates; i++) { 424 if (fp->rate_table[i] == rate) 425 break; 426 } 427 if (i >= fp->nr_rates) 428 return 0; 429 } 430 431 score = 1; 432 if (fp->channels == channels) 433 score++; 434 435 return score; 436 } 437 438 static struct snd_usb_substream * 439 find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num, 440 int fmt_type) 441 { 442 struct snd_usb_stream *as; 443 struct snd_usb_substream *subs; 444 445 list_for_each_entry(as, &chip->pcm_list, list) { 446 subs = &as->substream[stream]; 447 if (as->fmt_type == fmt_type && subs->ep_num == ep_num) 448 return subs; 449 } 450 451 return NULL; 452 } 453 454 /* 455 * Return the audioformat that is suitable for the implicit fb 456 */ 457 const struct audioformat * 458 snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip, 459 const struct audioformat *target, 460 const struct snd_pcm_hw_params *params, 461 int stream, 462 bool *fixed_rate) 463 { 464 struct snd_usb_substream *subs; 465 const struct audioformat *fp, *sync_fmt = NULL; 466 int score, high_score; 467 468 /* Use the original audioformat as fallback for the shared altset */ 469 if (target->iface == target->sync_iface && 470 target->altsetting == target->sync_altsetting) 471 sync_fmt = target; 472 473 subs = find_matching_substream(chip, stream, target->sync_ep, 474 target->fmt_type); 475 if (!subs) 476 goto end; 477 478 high_score = 0; 479 list_for_each_entry(fp, &subs->fmt_list, list) { 480 score = match_endpoint_audioformats(subs, fp, 481 params_rate(params), 482 params_channels(params), 483 params_format(params)); 484 if (score > high_score) { 485 sync_fmt = fp; 486 high_score = score; 487 } 488 } 489 490 end: 491 if (fixed_rate) 492 *fixed_rate = snd_usb_pcm_has_fixed_rate(subs); 493 return sync_fmt; 494 } 495 496