Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 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, |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 22 | IMPLICIT_FB_GENERIC, |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 23 | IMPLICIT_FB_FIXED, |
| 24 | }; |
| 25 | |
| 26 | struct snd_usb_implicit_fb_match { |
| 27 | unsigned int id; |
| 28 | unsigned int iface_class; |
| 29 | unsigned int ep_num; |
| 30 | unsigned int iface; |
| 31 | int type; |
| 32 | }; |
| 33 | |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 34 | #define IMPLICIT_FB_GENERIC_DEV(vend, prod) \ |
| 35 | { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_GENERIC } |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 36 | #define IMPLICIT_FB_FIXED_DEV(vend, prod, ep, ifnum) \ |
| 37 | { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_FIXED, .ep_num = (ep),\ |
| 38 | .iface = (ifnum) } |
| 39 | #define IMPLICIT_FB_SKIP_DEV(vend, prod) \ |
| 40 | { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_NONE } |
| 41 | |
| 42 | /* Implicit feedback quirk table for playback */ |
| 43 | static const struct snd_usb_implicit_fb_match playback_implicit_fb_quirks[] = { |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 44 | /* Generic matching */ |
| 45 | IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2080), /* M-Audio FastTrack Ultra */ |
| 46 | IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2081), /* M-Audio FastTrack Ultra */ |
| 47 | IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2030), /* M-Audio Fast Track C400 */ |
| 48 | IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2031), /* M-Audio Fast Track C600 */ |
| 49 | |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 50 | /* Fixed EP */ |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 51 | /* FIXME: check the availability of generic matching */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 52 | IMPLICIT_FB_FIXED_DEV(0x1397, 0x0001, 0x81, 1), /* Behringer UFX1604 */ |
| 53 | IMPLICIT_FB_FIXED_DEV(0x1397, 0x0002, 0x81, 1), /* Behringer UFX1204 */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 54 | IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */ |
| 55 | IMPLICIT_FB_FIXED_DEV(0x07fd, 0x0008, 0x81, 2), /* MOTU M Series */ |
| 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 */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 60 | IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */ |
| 61 | IMPLICIT_FB_FIXED_DEV(0x2b73, 0x000a, 0x82, 0), /* Pioneer DJ DJM-900NXS2 */ |
| 62 | IMPLICIT_FB_FIXED_DEV(0x2b73, 0x0017, 0x82, 0), /* Pioneer DJ DJM-250MK2 */ |
| 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 | |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 75 | /* No quirk for playback but with capture quirk (see below) */ |
Takashi Iwai | ad0e6a3 | 2020-11-23 09:53:46 +0100 | [diff] [blame^] | 76 | IMPLICIT_FB_SKIP_DEV(0x0582, 0x0130), /* BOSS BR-80 */ |
| 77 | IMPLICIT_FB_SKIP_DEV(0x0582, 0x0189), /* BOSS GT-100v2 */ |
| 78 | IMPLICIT_FB_SKIP_DEV(0x0582, 0x01d8), /* BOSS Katana */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 79 | IMPLICIT_FB_SKIP_DEV(0x0582, 0x01e5), /* BOSS GT-001 */ |
| 80 | |
| 81 | {} /* terminator */ |
| 82 | }; |
| 83 | |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 84 | /* Implicit feedback quirk table for capture: only FIXED type */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 85 | static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = { |
Takashi Iwai | ad0e6a3 | 2020-11-23 09:53:46 +0100 | [diff] [blame^] | 86 | IMPLICIT_FB_FIXED_DEV(0x0582, 0x0130, 0x0d, 0x01), /* BOSS BR-80 */ |
| 87 | IMPLICIT_FB_FIXED_DEV(0x0582, 0x0189, 0x0d, 0x01), /* BOSS GT-100v2 */ |
| 88 | IMPLICIT_FB_FIXED_DEV(0x0582, 0x01d8, 0x0d, 0x01), /* BOSS Katana */ |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 89 | IMPLICIT_FB_FIXED_DEV(0x0582, 0x01e5, 0x0d, 0x01), /* BOSS GT-001 */ |
| 90 | |
| 91 | {} /* terminator */ |
| 92 | }; |
| 93 | |
| 94 | /* set up sync EP information on the audioformat */ |
| 95 | static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip, |
| 96 | struct audioformat *fmt, |
| 97 | int ep, int ifnum, |
| 98 | const struct usb_host_interface *alts) |
| 99 | { |
| 100 | struct usb_interface *iface; |
| 101 | |
| 102 | if (!alts) { |
| 103 | iface = usb_ifnum_to_if(chip->dev, ifnum); |
| 104 | if (!iface || iface->num_altsetting < 2) |
| 105 | return 0; |
| 106 | alts = &iface->altsetting[1]; |
| 107 | } |
| 108 | |
| 109 | fmt->sync_ep = ep; |
| 110 | fmt->sync_iface = ifnum; |
| 111 | fmt->sync_altsetting = alts->desc.bAlternateSetting; |
| 112 | fmt->sync_ep_idx = 0; |
| 113 | fmt->implicit_fb = 1; |
| 114 | usb_audio_dbg(chip, |
| 115 | "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n", |
| 116 | fmt->iface, fmt->altsetting, |
| 117 | (ep & USB_DIR_IN) ? "playback" : "capture", |
| 118 | fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting); |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | /* Check whether the given UAC2 iface:altset points to an implicit fb source */ |
| 123 | static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip, |
| 124 | struct audioformat *fmt, |
| 125 | unsigned int ifnum, |
| 126 | unsigned int altsetting) |
| 127 | { |
| 128 | struct usb_host_interface *alts; |
| 129 | struct usb_endpoint_descriptor *epd; |
| 130 | |
| 131 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
| 132 | if (!alts) |
| 133 | return 0; |
| 134 | if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO || |
| 135 | alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING || |
| 136 | alts->desc.bInterfaceProtocol != UAC_VERSION_2 || |
| 137 | alts->desc.bNumEndpoints < 1) |
| 138 | return 0; |
| 139 | epd = get_endpoint(alts, 0); |
| 140 | if (!usb_endpoint_is_isoc_in(epd) || |
| 141 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 142 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 143 | return 0; |
| 144 | return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, |
| 145 | ifnum, alts); |
| 146 | } |
| 147 | |
| 148 | /* Like the function above, but specific to Roland with vendor class and hack */ |
| 149 | static int add_roland_implicit_fb(struct snd_usb_audio *chip, |
| 150 | struct audioformat *fmt, |
| 151 | unsigned int ifnum, |
| 152 | unsigned int altsetting) |
| 153 | { |
| 154 | struct usb_host_interface *alts; |
| 155 | struct usb_endpoint_descriptor *epd; |
| 156 | |
| 157 | alts = snd_usb_get_host_interface(chip, ifnum, altsetting); |
| 158 | if (!alts) |
| 159 | return 0; |
| 160 | if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC || |
| 161 | (alts->desc.bInterfaceSubClass != 2 && |
| 162 | alts->desc.bInterfaceProtocol != 2) || |
| 163 | alts->desc.bNumEndpoints < 1) |
| 164 | return 0; |
| 165 | epd = get_endpoint(alts, 0); |
| 166 | if (!usb_endpoint_is_isoc_in(epd) || |
| 167 | (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) != |
| 168 | USB_ENDPOINT_USAGE_IMPLICIT_FB) |
| 169 | return 0; |
| 170 | return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, |
| 171 | ifnum, alts); |
| 172 | } |
| 173 | |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 174 | |
| 175 | static int __add_generic_implicit_fb(struct snd_usb_audio *chip, |
| 176 | struct audioformat *fmt, |
| 177 | int iface, int altset) |
| 178 | { |
| 179 | struct usb_host_interface *alts; |
| 180 | struct usb_endpoint_descriptor *epd; |
| 181 | |
| 182 | alts = snd_usb_get_host_interface(chip, iface, altset); |
| 183 | if (!alts) |
| 184 | return 0; |
| 185 | |
| 186 | if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC && |
| 187 | alts->desc.bInterfaceClass != USB_CLASS_AUDIO) || |
| 188 | alts->desc.bNumEndpoints < 1) |
| 189 | return 0; |
| 190 | epd = get_endpoint(alts, 0); |
| 191 | if (!usb_endpoint_is_isoc_in(epd) || |
| 192 | (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) |
| 193 | return 0; |
| 194 | return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, |
| 195 | iface, alts); |
| 196 | } |
| 197 | |
| 198 | /* More generic quirk: look for the sync EP next to the data EP */ |
| 199 | static int add_generic_implicit_fb(struct snd_usb_audio *chip, |
| 200 | struct audioformat *fmt, |
| 201 | struct usb_host_interface *alts) |
| 202 | { |
| 203 | if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC) |
| 204 | return 0; |
| 205 | |
| 206 | if (__add_generic_implicit_fb(chip, fmt, |
| 207 | alts->desc.bInterfaceNumber + 1, |
| 208 | alts->desc.bAlternateSetting)) |
| 209 | return 1; |
| 210 | return __add_generic_implicit_fb(chip, fmt, |
| 211 | alts->desc.bInterfaceNumber - 1, |
| 212 | alts->desc.bAlternateSetting); |
| 213 | } |
| 214 | |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 215 | static const struct snd_usb_implicit_fb_match * |
| 216 | find_implicit_fb_entry(struct snd_usb_audio *chip, |
| 217 | const struct snd_usb_implicit_fb_match *match, |
| 218 | const struct usb_host_interface *alts) |
| 219 | { |
| 220 | for (; match->id; match++) |
| 221 | if (match->id == chip->usb_id && |
| 222 | (!match->iface_class || |
| 223 | (alts->desc.bInterfaceClass == match->iface_class))) |
| 224 | return match; |
| 225 | |
| 226 | return NULL; |
| 227 | } |
| 228 | |
| 229 | /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk |
| 230 | * applies. Returns 1 if a quirk was found. |
| 231 | */ |
| 232 | static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip, |
| 233 | struct audioformat *fmt, |
| 234 | struct usb_host_interface *alts) |
| 235 | { |
| 236 | const struct snd_usb_implicit_fb_match *p; |
| 237 | unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE; |
| 238 | |
| 239 | p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts); |
| 240 | if (p) { |
| 241 | switch (p->type) { |
Takashi Iwai | 83b7dcb | 2020-11-23 09:53:44 +0100 | [diff] [blame] | 242 | case IMPLICIT_FB_GENERIC: |
| 243 | return add_generic_implicit_fb(chip, fmt, alts); |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 244 | case IMPLICIT_FB_NONE: |
| 245 | return 0; /* No quirk */ |
| 246 | case IMPLICIT_FB_FIXED: |
| 247 | return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, |
| 248 | p->iface, NULL); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /* Generic UAC2 implicit feedback */ |
| 253 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
| 254 | alts->desc.bInterfaceClass == USB_CLASS_AUDIO && |
| 255 | alts->desc.bInterfaceProtocol == UAC_VERSION_2 && |
| 256 | alts->desc.bNumEndpoints == 1) { |
| 257 | if (add_generic_uac2_implicit_fb(chip, fmt, |
| 258 | alts->desc.bInterfaceNumber + 1, |
| 259 | alts->desc.bAlternateSetting)) |
| 260 | return 1; |
| 261 | } |
| 262 | |
| 263 | /* Roland/BOSS implicit feedback with vendor spec class */ |
| 264 | if (attr == USB_ENDPOINT_SYNC_ASYNC && |
| 265 | alts->desc.bInterfaceClass == USB_CLASS_VENDOR_SPEC && |
| 266 | alts->desc.bInterfaceProtocol == 2 && |
| 267 | alts->desc.bNumEndpoints == 1 && |
| 268 | USB_ID_VENDOR(chip->usb_id) == 0x0582 /* Roland */) { |
| 269 | if (add_roland_implicit_fb(chip, fmt, |
| 270 | alts->desc.bInterfaceNumber + 1, |
| 271 | alts->desc.bAlternateSetting)) |
| 272 | return 1; |
| 273 | } |
| 274 | |
Takashi Iwai | 62abd09 | 2020-11-23 09:53:45 +0100 | [diff] [blame] | 275 | /* Try the generic implicit fb if available */ |
| 276 | if (chip->generic_implicit_fb) |
| 277 | return add_generic_implicit_fb(chip, fmt, alts); |
| 278 | |
Takashi Iwai | 9fddc15 | 2020-11-23 09:53:43 +0100 | [diff] [blame] | 279 | /* No quirk */ |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /* same for capture, but only handling FIXED entry */ |
| 284 | static int audioformat_capture_quirk(struct snd_usb_audio *chip, |
| 285 | struct audioformat *fmt, |
| 286 | struct usb_host_interface *alts) |
| 287 | { |
| 288 | const struct snd_usb_implicit_fb_match *p; |
| 289 | |
| 290 | p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts); |
| 291 | if (p && p->type == IMPLICIT_FB_FIXED) |
| 292 | return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, p->iface, |
| 293 | NULL); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Parse altset and set up implicit feedback endpoint on the audioformat |
| 299 | */ |
| 300 | int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip, |
| 301 | struct audioformat *fmt, |
| 302 | struct usb_host_interface *alts) |
| 303 | { |
| 304 | if (fmt->endpoint & USB_DIR_IN) |
| 305 | return audioformat_capture_quirk(chip, fmt, alts); |
| 306 | else |
| 307 | return audioformat_implicit_fb_quirk(chip, fmt, alts); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * Return the score of matching two audioformats. |
| 312 | * Veto the audioformat if: |
| 313 | * - It has no channels for some reason. |
| 314 | * - Requested PCM format is not supported. |
| 315 | * - Requested sample rate is not supported. |
| 316 | */ |
| 317 | static int match_endpoint_audioformats(struct snd_usb_substream *subs, |
| 318 | const struct audioformat *fp, |
| 319 | int rate, int channels, |
| 320 | snd_pcm_format_t pcm_format) |
| 321 | { |
| 322 | int i, score; |
| 323 | |
| 324 | if (fp->channels < 1) |
| 325 | return 0; |
| 326 | |
| 327 | if (!(fp->formats & pcm_format_to_bits(pcm_format))) |
| 328 | return 0; |
| 329 | |
| 330 | if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) { |
| 331 | if (rate < fp->rate_min || rate > fp->rate_max) |
| 332 | return 0; |
| 333 | } else { |
| 334 | for (i = 0; i < fp->nr_rates; i++) { |
| 335 | if (fp->rate_table[i] == rate) |
| 336 | break; |
| 337 | } |
| 338 | if (i >= fp->nr_rates) |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | score = 1; |
| 343 | if (fp->channels == channels) |
| 344 | score++; |
| 345 | |
| 346 | return score; |
| 347 | } |
| 348 | |
| 349 | static struct snd_usb_substream * |
| 350 | find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num, |
| 351 | int fmt_type) |
| 352 | { |
| 353 | struct snd_usb_stream *as; |
| 354 | struct snd_usb_substream *subs; |
| 355 | |
| 356 | list_for_each_entry(as, &chip->pcm_list, list) { |
| 357 | subs = &as->substream[stream]; |
| 358 | if (as->fmt_type == fmt_type && subs->ep_num == ep_num) |
| 359 | return subs; |
| 360 | } |
| 361 | |
| 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Return the audioformat that is suitable for the implicit fb |
| 367 | */ |
| 368 | const struct audioformat * |
| 369 | snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip, |
| 370 | const struct audioformat *target, |
| 371 | const struct snd_pcm_hw_params *params, |
| 372 | int stream) |
| 373 | { |
| 374 | struct snd_usb_substream *subs; |
| 375 | const struct audioformat *fp, *sync_fmt; |
| 376 | int score, high_score; |
| 377 | |
| 378 | /* When sharing the same altset, use the original audioformat */ |
| 379 | if (target->iface == target->sync_iface && |
| 380 | target->altsetting == target->sync_altsetting) |
| 381 | return target; |
| 382 | |
| 383 | subs = find_matching_substream(chip, stream, target->sync_ep, |
| 384 | target->fmt_type); |
| 385 | if (!subs) |
| 386 | return NULL; |
| 387 | |
| 388 | sync_fmt = NULL; |
| 389 | high_score = 0; |
| 390 | list_for_each_entry(fp, &subs->fmt_list, list) { |
| 391 | score = match_endpoint_audioformats(subs, fp, |
| 392 | params_rate(params), |
| 393 | params_channels(params), |
| 394 | params_format(params)); |
| 395 | if (score > high_score) { |
| 396 | sync_fmt = fp; |
| 397 | high_score = score; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return sync_fmt; |
| 402 | } |
| 403 | |