xref: /openbmc/linux/sound/usb/implicit.c (revision 44ad3baf1cca483e418b6aadf2d3994f69e0f16a)
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 	{} /* terminator */
80 };
81 
82 /* set up sync EP information on the audioformat */
add_implicit_fb_sync_ep(struct snd_usb_audio * chip,struct audioformat * fmt,int ep,int ep_idx,int ifnum,const struct usb_host_interface * alts)83 static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
84 				   struct audioformat *fmt,
85 				   int ep, int ep_idx, int ifnum,
86 				   const struct usb_host_interface *alts)
87 {
88 	struct usb_interface *iface;
89 
90 	if (!alts) {
91 		iface = usb_ifnum_to_if(chip->dev, ifnum);
92 		if (!iface || iface->num_altsetting < 2)
93 			return 0;
94 		alts = &iface->altsetting[1];
95 	}
96 
97 	fmt->sync_ep = ep;
98 	fmt->sync_iface = ifnum;
99 	fmt->sync_altsetting = alts->desc.bAlternateSetting;
100 	fmt->sync_ep_idx = ep_idx;
101 	fmt->implicit_fb = 1;
102 	usb_audio_dbg(chip,
103 		      "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
104 		      fmt->iface, fmt->altsetting,
105 		      (ep & USB_DIR_IN) ? "playback" : "capture",
106 		      fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
107 	return 1;
108 }
109 
110 /* Check whether the given UAC2 iface:altset points to an implicit fb source */
add_generic_uac2_implicit_fb(struct snd_usb_audio * chip,struct audioformat * fmt,unsigned int ifnum,unsigned int altsetting)111 static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
112 					struct audioformat *fmt,
113 					unsigned int ifnum,
114 					unsigned int altsetting)
115 {
116 	struct usb_host_interface *alts;
117 	struct usb_endpoint_descriptor *epd;
118 
119 	alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
120 	if (!alts)
121 		return 0;
122 	if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
123 	    alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
124 	    alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
125 	    alts->desc.bNumEndpoints < 1)
126 		return 0;
127 	epd = get_endpoint(alts, 0);
128 	if (!usb_endpoint_is_isoc_in(epd) ||
129 	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
130 					USB_ENDPOINT_USAGE_IMPLICIT_FB)
131 		return 0;
132 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
133 				       ifnum, alts);
134 }
135 
roland_sanity_check_iface(struct usb_host_interface * alts)136 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
137 {
138 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
139 	    (alts->desc.bInterfaceSubClass != 2 &&
140 	     alts->desc.bInterfaceProtocol != 2) ||
141 	    alts->desc.bNumEndpoints < 1)
142 		return false;
143 	return true;
144 }
145 
146 /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
add_roland_implicit_fb(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)147 static int add_roland_implicit_fb(struct snd_usb_audio *chip,
148 				  struct audioformat *fmt,
149 				  struct usb_host_interface *alts)
150 {
151 	struct usb_endpoint_descriptor *epd;
152 
153 	if (!roland_sanity_check_iface(alts))
154 		return 0;
155 	/* only when both streams are with ASYNC type */
156 	epd = get_endpoint(alts, 0);
157 	if (!usb_endpoint_is_isoc_out(epd) ||
158 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
159 		return 0;
160 
161 	/* check capture EP */
162 	alts = snd_usb_get_host_interface(chip,
163 					  alts->desc.bInterfaceNumber + 1,
164 					  alts->desc.bAlternateSetting);
165 	if (!alts || !roland_sanity_check_iface(alts))
166 		return 0;
167 	epd = get_endpoint(alts, 0);
168 	if (!usb_endpoint_is_isoc_in(epd) ||
169 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
170 		return 0;
171 	chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
172 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
173 				       alts->desc.bInterfaceNumber, alts);
174 }
175 
176 /* capture quirk for Roland device; always full-duplex */
add_roland_capture_quirk(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)177 static int add_roland_capture_quirk(struct snd_usb_audio *chip,
178 				    struct audioformat *fmt,
179 				    struct usb_host_interface *alts)
180 {
181 	struct usb_endpoint_descriptor *epd;
182 
183 	if (!roland_sanity_check_iface(alts))
184 		return 0;
185 	epd = get_endpoint(alts, 0);
186 	if (!usb_endpoint_is_isoc_in(epd) ||
187 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
188 		return 0;
189 
190 	alts = snd_usb_get_host_interface(chip,
191 					  alts->desc.bInterfaceNumber - 1,
192 					  alts->desc.bAlternateSetting);
193 	if (!alts || !roland_sanity_check_iface(alts))
194 		return 0;
195 	epd = get_endpoint(alts, 0);
196 	if (!usb_endpoint_is_isoc_out(epd))
197 		return 0;
198 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
199 				       alts->desc.bInterfaceNumber, alts);
200 }
201 
202 /* Playback and capture EPs on Pioneer devices share the same iface/altset
203  * for the implicit feedback operation
204  */
is_pioneer_implicit_fb(struct snd_usb_audio * chip,struct usb_host_interface * alts)205 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
206 				   struct usb_host_interface *alts)
207 
208 {
209 	struct usb_endpoint_descriptor *epd;
210 
211 	if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
212 	    USB_ID_VENDOR(chip->usb_id) != 0x08e4)
213 		return false;
214 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
215 		return false;
216 	if (alts->desc.bNumEndpoints != 2)
217 		return false;
218 
219 	epd = get_endpoint(alts, 0);
220 	if (!usb_endpoint_is_isoc_out(epd) ||
221 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
222 		return false;
223 
224 	epd = get_endpoint(alts, 1);
225 	if (!usb_endpoint_is_isoc_in(epd) ||
226 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
227 	    ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
228 	     USB_ENDPOINT_USAGE_DATA &&
229 	     (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
230 	     USB_ENDPOINT_USAGE_IMPLICIT_FB))
231 		return false;
232 
233 	return true;
234 }
235 
__add_generic_implicit_fb(struct snd_usb_audio * chip,struct audioformat * fmt,int iface,int altset)236 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
237 				     struct audioformat *fmt,
238 				     int iface, int altset)
239 {
240 	struct usb_host_interface *alts;
241 	struct usb_endpoint_descriptor *epd;
242 
243 	alts = snd_usb_get_host_interface(chip, iface, altset);
244 	if (!alts)
245 		return 0;
246 
247 	if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
248 	     alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
249 	    alts->desc.bNumEndpoints < 1)
250 		return 0;
251 	epd = get_endpoint(alts, 0);
252 	if (!usb_endpoint_is_isoc_in(epd) ||
253 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
254 		return 0;
255 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
256 				       iface, alts);
257 }
258 
259 /* More generic quirk: look for the sync EP next to the data EP */
add_generic_implicit_fb(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)260 static int add_generic_implicit_fb(struct snd_usb_audio *chip,
261 				   struct audioformat *fmt,
262 				   struct usb_host_interface *alts)
263 {
264 	if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
265 		return 0;
266 
267 	if (__add_generic_implicit_fb(chip, fmt,
268 				      alts->desc.bInterfaceNumber + 1,
269 				      alts->desc.bAlternateSetting))
270 		return 1;
271 	return __add_generic_implicit_fb(chip, fmt,
272 					 alts->desc.bInterfaceNumber - 1,
273 					 alts->desc.bAlternateSetting);
274 }
275 
276 static const struct snd_usb_implicit_fb_match *
find_implicit_fb_entry(struct snd_usb_audio * chip,const struct snd_usb_implicit_fb_match * match,const struct usb_host_interface * alts)277 find_implicit_fb_entry(struct snd_usb_audio *chip,
278 		       const struct snd_usb_implicit_fb_match *match,
279 		       const struct usb_host_interface *alts)
280 {
281 	for (; match->id; match++)
282 		if (match->id == chip->usb_id &&
283 		    (!match->iface_class ||
284 		     (alts->desc.bInterfaceClass == match->iface_class)))
285 			return match;
286 
287 	return NULL;
288 }
289 
290 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
291  * applies. Returns 1 if a quirk was found.
292  */
audioformat_implicit_fb_quirk(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)293 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
294 					 struct audioformat *fmt,
295 					 struct usb_host_interface *alts)
296 {
297 	const struct snd_usb_implicit_fb_match *p;
298 	unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
299 
300 	p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
301 	if (p) {
302 		switch (p->type) {
303 		case IMPLICIT_FB_GENERIC:
304 			return add_generic_implicit_fb(chip, fmt, alts);
305 		case IMPLICIT_FB_NONE:
306 			return 0; /* No quirk */
307 		case IMPLICIT_FB_FIXED:
308 			return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
309 						       p->iface, NULL);
310 		}
311 	}
312 
313 	/* Special handling for devices with capture quirks */
314 	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
315 	if (p) {
316 		switch (p->type) {
317 		case IMPLICIT_FB_FIXED:
318 			return 0; /* no quirk */
319 		case IMPLICIT_FB_BOTH:
320 			chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
321 			return add_generic_implicit_fb(chip, fmt, alts);
322 		}
323 	}
324 
325 	/* Generic UAC2 implicit feedback */
326 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
327 	    alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
328 	    alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
329 	    alts->desc.bNumEndpoints == 1) {
330 		if (add_generic_uac2_implicit_fb(chip, fmt,
331 						 alts->desc.bInterfaceNumber + 1,
332 						 alts->desc.bAlternateSetting))
333 			return 1;
334 	}
335 
336 	/* Roland/BOSS implicit feedback with vendor spec class */
337 	if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
338 		if (add_roland_implicit_fb(chip, fmt, alts) > 0)
339 			return 1;
340 	}
341 
342 	/* Pioneer devices with vendor spec class */
343 	if (is_pioneer_implicit_fb(chip, alts)) {
344 		chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
345 		return add_implicit_fb_sync_ep(chip, fmt,
346 					       get_endpoint(alts, 1)->bEndpointAddress,
347 					       1, alts->desc.bInterfaceNumber,
348 					       alts);
349 	}
350 
351 	/* Try the generic implicit fb if available */
352 	if (chip->generic_implicit_fb ||
353 	    (chip->quirk_flags & QUIRK_FLAG_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 */
audioformat_capture_quirk(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)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  */
snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio * chip,struct audioformat * fmt,struct usb_host_interface * alts)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 (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB)
391 		return 0;
392 	if (fmt->endpoint & USB_DIR_IN)
393 		return audioformat_capture_quirk(chip, fmt, alts);
394 	else
395 		return audioformat_implicit_fb_quirk(chip, fmt, alts);
396 }
397 
398 /*
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.
404  */
match_endpoint_audioformats(struct snd_usb_substream * subs,const struct audioformat * fp,int rate,int channels,snd_pcm_format_t pcm_format)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)
409 {
410 	int i, score;
411 
412 	if (fp->channels < 1)
413 		return 0;
414 
415 	if (!(fp->formats & pcm_format_to_bits(pcm_format)))
416 		return 0;
417 
418 	if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
419 		if (rate < fp->rate_min || rate > fp->rate_max)
420 			return 0;
421 	} else {
422 		for (i = 0; i < fp->nr_rates; i++) {
423 			if (fp->rate_table[i] == rate)
424 				break;
425 		}
426 		if (i >= fp->nr_rates)
427 			return 0;
428 	}
429 
430 	score = 1;
431 	if (fp->channels == channels)
432 		score++;
433 
434 	return score;
435 }
436 
437 static struct snd_usb_substream *
find_matching_substream(struct snd_usb_audio * chip,int stream,int ep_num,int fmt_type)438 find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
439 			int fmt_type)
440 {
441 	struct snd_usb_stream *as;
442 	struct snd_usb_substream *subs;
443 
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)
447 			return subs;
448 	}
449 
450 	return NULL;
451 }
452 
453 /*
454  * Return the audioformat that is suitable for the implicit fb
455  */
456 const struct audioformat *
snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio * chip,const struct audioformat * target,const struct snd_pcm_hw_params * params,int stream,bool * fixed_rate)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,
460 				     int stream,
461 				     bool *fixed_rate)
462 {
463 	struct snd_usb_substream *subs;
464 	const struct audioformat *fp, *sync_fmt = NULL;
465 	int score, high_score;
466 
467 	/* Use the original audioformat as fallback for the shared altset */
468 	if (target->iface == target->sync_iface &&
469 	    target->altsetting == target->sync_altsetting)
470 		sync_fmt = target;
471 
472 	subs = find_matching_substream(chip, stream, target->sync_ep,
473 				       target->fmt_type);
474 	if (!subs)
475 		goto end;
476 
477 	high_score = 0;
478 	list_for_each_entry(fp, &subs->fmt_list, list) {
479 		score = match_endpoint_audioformats(subs, fp,
480 						    params_rate(params),
481 						    params_channels(params),
482 						    params_format(params));
483 		if (score > high_score) {
484 			sync_fmt = fp;
485 			high_score = score;
486 		}
487 	}
488 
489  end:
490 	if (fixed_rate)
491 		*fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
492 	return sync_fmt;
493 }
494 
495