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 "implicit.h" 19 20 enum { 21 IMPLICIT_FB_NONE, 22 IMPLICIT_FB_GENERIC, 23 IMPLICIT_FB_FIXED, 24 IMPLICIT_FB_BOTH, /* generic playback + capture (for BOSS) */ 25 }; 26 27 struct snd_usb_implicit_fb_match { 28 unsigned int id; 29 unsigned int iface_class; 30 unsigned int ep_num; 31 unsigned int iface; 32 int type; 33 }; 34 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),\ 39 .iface = (ifnum) } 40 #define IMPLICIT_FB_BOTH_DEV(vend, prod, ep, ifnum) \ 41 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_BOTH, .ep_num = (ep),\ 42 .iface = (ifnum) } 43 #define IMPLICIT_FB_SKIP_DEV(vend, prod) \ 44 { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_NONE } 45 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, 0x2030), /* M-Audio Fast Track C400 */ 51 IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2031), /* M-Audio Fast Track C600 */ 52 53 /* Fixed EP */ 54 /* FIXME: check the availability of generic matching */ 55 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2080, 0x81, 2), /* M-Audio FastTrack Ultra */ 56 IMPLICIT_FB_FIXED_DEV(0x0763, 0x2081, 0x81, 2), /* M-Audio FastTrack Ultra */ 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 */ 66 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 */ 74 75 {} /* terminator */ 76 }; 77 78 /* Implicit feedback quirk table for capture: only FIXED type */ 79 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = { 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 return add_generic_implicit_fb(chip, fmt, alts); 355 356 /* No quirk */ 357 return 0; 358 } 359 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) 364 { 365 const struct snd_usb_implicit_fb_match *p; 366 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, 370 p->iface, NULL); 371 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) 375 return 1; 376 } 377 378 if (is_pioneer_implicit_fb(chip, alts)) 379 return 1; /* skip the quirk, also don't handle generic sync EP */ 380 return 0; 381 } 382 383 /* 384 * Parse altset and set up implicit feedback endpoint on the audioformat 385 */ 386 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip, 387 struct audioformat *fmt, 388 struct usb_host_interface *alts) 389 { 390 if (fmt->endpoint & USB_DIR_IN) 391 return audioformat_capture_quirk(chip, fmt, alts); 392 else 393 return audioformat_implicit_fb_quirk(chip, fmt, alts); 394 } 395 396 /* 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. 402 */ 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) 407 { 408 int i, score; 409 410 if (fp->channels < 1) 411 return 0; 412 413 if (!(fp->formats & pcm_format_to_bits(pcm_format))) 414 return 0; 415 416 if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) { 417 if (rate < fp->rate_min || rate > fp->rate_max) 418 return 0; 419 } else { 420 for (i = 0; i < fp->nr_rates; i++) { 421 if (fp->rate_table[i] == rate) 422 break; 423 } 424 if (i >= fp->nr_rates) 425 return 0; 426 } 427 428 score = 1; 429 if (fp->channels == channels) 430 score++; 431 432 return score; 433 } 434 435 static struct snd_usb_substream * 436 find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num, 437 int fmt_type) 438 { 439 struct snd_usb_stream *as; 440 struct snd_usb_substream *subs; 441 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) 445 return subs; 446 } 447 448 return NULL; 449 } 450 451 /* 452 * Return the audioformat that is suitable for the implicit fb 453 */ 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, 458 int stream) 459 { 460 struct snd_usb_substream *subs; 461 const struct audioformat *fp, *sync_fmt = NULL; 462 int score, high_score; 463 464 /* Use the original audioformat as fallback for the shared altset */ 465 if (target->iface == target->sync_iface && 466 target->altsetting == target->sync_altsetting) 467 sync_fmt = target; 468 469 subs = find_matching_substream(chip, stream, target->sync_ep, 470 target->fmt_type); 471 if (!subs) 472 return sync_fmt; 473 474 high_score = 0; 475 list_for_each_entry(fp, &subs->fmt_list, list) { 476 score = match_endpoint_audioformats(subs, fp, 477 params_rate(params), 478 params_channels(params), 479 params_format(params)); 480 if (score > high_score) { 481 sync_fmt = fp; 482 high_score = score; 483 } 484 } 485 486 return sync_fmt; 487 } 488 489