xref: /openbmc/linux/sound/usb/implicit.c (revision 85250a24)
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 	/* Fixed EP */
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 */
63 
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 */
71 
72 	{} /* terminator */
73 };
74 
75 /* Implicit feedback quirk table for capture: only FIXED type */
76 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
77 	{} /* terminator */
78 };
79 
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)
85 {
86 	struct usb_interface *iface;
87 
88 	if (!alts) {
89 		iface = usb_ifnum_to_if(chip->dev, ifnum);
90 		if (!iface || iface->num_altsetting < 2)
91 			return 0;
92 		alts = &iface->altsetting[1];
93 	}
94 
95 	fmt->sync_ep = ep;
96 	fmt->sync_iface = ifnum;
97 	fmt->sync_altsetting = alts->desc.bAlternateSetting;
98 	fmt->sync_ep_idx = ep_idx;
99 	fmt->implicit_fb = 1;
100 	usb_audio_dbg(chip,
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);
105 	return 1;
106 }
107 
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,
111 					unsigned int ifnum,
112 					unsigned int altsetting)
113 {
114 	struct usb_host_interface *alts;
115 	struct usb_endpoint_descriptor *epd;
116 
117 	alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
118 	if (!alts)
119 		return 0;
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)
124 		return 0;
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)
129 		return 0;
130 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
131 				       ifnum, alts);
132 }
133 
134 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
135 {
136 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
137 	    (alts->desc.bInterfaceSubClass != 2 &&
138 	     alts->desc.bInterfaceProtocol != 2) ||
139 	    alts->desc.bNumEndpoints < 1)
140 		return false;
141 	return true;
142 }
143 
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)
148 {
149 	struct usb_endpoint_descriptor *epd;
150 
151 	if (!roland_sanity_check_iface(alts))
152 		return 0;
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)
157 		return 0;
158 
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))
164 		return 0;
165 	epd = get_endpoint(alts, 0);
166 	if (!usb_endpoint_is_isoc_in(epd) ||
167 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
168 		return 0;
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);
172 }
173 
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)
178 {
179 	struct usb_endpoint_descriptor *epd;
180 
181 	if (!roland_sanity_check_iface(alts))
182 		return 0;
183 	epd = get_endpoint(alts, 0);
184 	if (!usb_endpoint_is_isoc_in(epd) ||
185 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
186 		return 0;
187 
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))
192 		return 0;
193 	epd = get_endpoint(alts, 0);
194 	if (!usb_endpoint_is_isoc_out(epd))
195 		return 0;
196 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
197 				       alts->desc.bInterfaceNumber, alts);
198 }
199 
200 /* Playback and capture EPs on Pioneer devices share the same iface/altset
201  * for the implicit feedback operation
202  */
203 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
204 				   struct usb_host_interface *alts)
205 
206 {
207 	struct usb_endpoint_descriptor *epd;
208 
209 	if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
210 	    USB_ID_VENDOR(chip->usb_id) != 0x08e4)
211 		return false;
212 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
213 		return false;
214 	if (alts->desc.bNumEndpoints != 2)
215 		return false;
216 
217 	epd = get_endpoint(alts, 0);
218 	if (!usb_endpoint_is_isoc_out(epd) ||
219 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
220 		return false;
221 
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))
229 		return false;
230 
231 	return true;
232 }
233 
234 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
235 				     struct audioformat *fmt,
236 				     int iface, int altset)
237 {
238 	struct usb_host_interface *alts;
239 	struct usb_endpoint_descriptor *epd;
240 
241 	alts = snd_usb_get_host_interface(chip, iface, altset);
242 	if (!alts)
243 		return 0;
244 
245 	if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
246 	     alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
247 	    alts->desc.bNumEndpoints < 1)
248 		return 0;
249 	epd = get_endpoint(alts, 0);
250 	if (!usb_endpoint_is_isoc_in(epd) ||
251 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
252 		return 0;
253 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
254 				       iface, alts);
255 }
256 
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)
261 {
262 	if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
263 		return 0;
264 
265 	if (__add_generic_implicit_fb(chip, fmt,
266 				      alts->desc.bInterfaceNumber + 1,
267 				      alts->desc.bAlternateSetting))
268 		return 1;
269 	return __add_generic_implicit_fb(chip, fmt,
270 					 alts->desc.bInterfaceNumber - 1,
271 					 alts->desc.bAlternateSetting);
272 }
273 
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)
278 {
279 	for (; match->id; match++)
280 		if (match->id == chip->usb_id &&
281 		    (!match->iface_class ||
282 		     (alts->desc.bInterfaceClass == match->iface_class)))
283 			return match;
284 
285 	return NULL;
286 }
287 
288 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
289  * applies. Returns 1 if a quirk was found.
290  */
291 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
292 					 struct audioformat *fmt,
293 					 struct usb_host_interface *alts)
294 {
295 	const struct snd_usb_implicit_fb_match *p;
296 	unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
297 
298 	p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
299 	if (p) {
300 		switch (p->type) {
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,
307 						       p->iface, NULL);
308 		}
309 	}
310 
311 	/* Special handling for devices with capture quirks */
312 	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
313 	if (p) {
314 		switch (p->type) {
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);
320 		}
321 	}
322 
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))
331 			return 1;
332 	}
333 
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)
337 			return 1;
338 	}
339 
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,
346 					       alts);
347 	}
348 
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);
353 
354 	/* No quirk */
355 	return 0;
356 }
357 
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)
362 {
363 	const struct snd_usb_implicit_fb_match *p;
364 
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,
368 					       p->iface, NULL);
369 
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)
373 			return 1;
374 	}
375 
376 	if (is_pioneer_implicit_fb(chip, alts))
377 		return 1; /* skip the quirk, also don't handle generic sync EP */
378 	return 0;
379 }
380 
381 /*
382  * Parse altset and set up implicit feedback endpoint on the audioformat
383  */
384 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
385 				    struct audioformat *fmt,
386 				    struct usb_host_interface *alts)
387 {
388 	if (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB)
389 		return 0;
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