xref: /openbmc/linux/sound/usb/implicit.c (revision e0302638)
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, 0x2080), /* M-Audio FastTrack Ultra */
51 	IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2081), /* M-Audio FastTrack Ultra */
52 	IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2030), /* M-Audio Fast Track C400 */
53 	IMPLICIT_FB_GENERIC_DEV(0x0763, 0x2031), /* M-Audio Fast Track C600 */
54 
55 	/* Fixed EP */
56 	/* FIXME: check the availability of generic matching */
57 	IMPLICIT_FB_FIXED_DEV(0x1397, 0x0001, 0x81, 1), /* Behringer UFX1604 */
58 	IMPLICIT_FB_FIXED_DEV(0x1397, 0x0002, 0x81, 1), /* Behringer UFX1204 */
59 	IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */
60 	IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */
61 	IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */
62 	IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */
63 	IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */
64 	IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
65 	IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
66 	IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
67 	IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
68 
69 	/* Special matching */
70 	{ .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
71 	  .type = IMPLICIT_FB_NONE },		/* MicroBook IIc */
72 	/* ep = 0x84, ifnum = 0 */
73 	{ .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
74 	  .type = IMPLICIT_FB_FIXED,
75 	  .ep_num = 0x84, .iface = 0 },		/* MOTU MicroBook II */
76 
77 	{} /* terminator */
78 };
79 
80 /* Implicit feedback quirk table for capture: only FIXED type */
81 static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
82 	{} /* terminator */
83 };
84 
85 /* set up sync EP information on the audioformat */
86 static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
87 				   struct audioformat *fmt,
88 				   int ep, int ep_idx, int ifnum,
89 				   const struct usb_host_interface *alts)
90 {
91 	struct usb_interface *iface;
92 
93 	if (!alts) {
94 		iface = usb_ifnum_to_if(chip->dev, ifnum);
95 		if (!iface || iface->num_altsetting < 2)
96 			return 0;
97 		alts = &iface->altsetting[1];
98 	}
99 
100 	fmt->sync_ep = ep;
101 	fmt->sync_iface = ifnum;
102 	fmt->sync_altsetting = alts->desc.bAlternateSetting;
103 	fmt->sync_ep_idx = ep_idx;
104 	fmt->implicit_fb = 1;
105 	usb_audio_dbg(chip,
106 		      "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
107 		      fmt->iface, fmt->altsetting,
108 		      (ep & USB_DIR_IN) ? "playback" : "capture",
109 		      fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
110 	return 1;
111 }
112 
113 /* Check whether the given UAC2 iface:altset points to an implicit fb source */
114 static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
115 					struct audioformat *fmt,
116 					unsigned int ifnum,
117 					unsigned int altsetting)
118 {
119 	struct usb_host_interface *alts;
120 	struct usb_endpoint_descriptor *epd;
121 
122 	alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
123 	if (!alts)
124 		return 0;
125 	if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
126 	    alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
127 	    alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
128 	    alts->desc.bNumEndpoints < 1)
129 		return 0;
130 	epd = get_endpoint(alts, 0);
131 	if (!usb_endpoint_is_isoc_in(epd) ||
132 	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
133 					USB_ENDPOINT_USAGE_IMPLICIT_FB)
134 		return 0;
135 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
136 				       ifnum, alts);
137 }
138 
139 static bool roland_sanity_check_iface(struct usb_host_interface *alts)
140 {
141 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
142 	    (alts->desc.bInterfaceSubClass != 2 &&
143 	     alts->desc.bInterfaceProtocol != 2) ||
144 	    alts->desc.bNumEndpoints < 1)
145 		return false;
146 	return true;
147 }
148 
149 /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
150 static int add_roland_implicit_fb(struct snd_usb_audio *chip,
151 				  struct audioformat *fmt,
152 				  struct usb_host_interface *alts)
153 {
154 	struct usb_endpoint_descriptor *epd;
155 
156 	if (!roland_sanity_check_iface(alts))
157 		return 0;
158 	/* only when both streams are with ASYNC type */
159 	epd = get_endpoint(alts, 0);
160 	if (!usb_endpoint_is_isoc_out(epd) ||
161 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
162 		return 0;
163 
164 	/* check capture EP */
165 	alts = snd_usb_get_host_interface(chip,
166 					  alts->desc.bInterfaceNumber + 1,
167 					  alts->desc.bAlternateSetting);
168 	if (!alts || !roland_sanity_check_iface(alts))
169 		return 0;
170 	epd = get_endpoint(alts, 0);
171 	if (!usb_endpoint_is_isoc_in(epd) ||
172 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
173 		return 0;
174 	chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
175 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
176 				       alts->desc.bInterfaceNumber, alts);
177 }
178 
179 /* capture quirk for Roland device; always full-duplex */
180 static int add_roland_capture_quirk(struct snd_usb_audio *chip,
181 				    struct audioformat *fmt,
182 				    struct usb_host_interface *alts)
183 {
184 	struct usb_endpoint_descriptor *epd;
185 
186 	if (!roland_sanity_check_iface(alts))
187 		return 0;
188 	epd = get_endpoint(alts, 0);
189 	if (!usb_endpoint_is_isoc_in(epd) ||
190 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
191 		return 0;
192 
193 	alts = snd_usb_get_host_interface(chip,
194 					  alts->desc.bInterfaceNumber - 1,
195 					  alts->desc.bAlternateSetting);
196 	if (!alts || !roland_sanity_check_iface(alts))
197 		return 0;
198 	epd = get_endpoint(alts, 0);
199 	if (!usb_endpoint_is_isoc_out(epd))
200 		return 0;
201 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
202 				       alts->desc.bInterfaceNumber, alts);
203 }
204 
205 /* Playback and capture EPs on Pioneer devices share the same iface/altset
206  * for the implicit feedback operation
207  */
208 static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
209 				   struct usb_host_interface *alts)
210 
211 {
212 	struct usb_endpoint_descriptor *epd;
213 
214 	if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
215 	    USB_ID_VENDOR(chip->usb_id) != 0x08e4)
216 		return false;
217 	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
218 		return false;
219 	if (alts->desc.bNumEndpoints != 2)
220 		return false;
221 
222 	epd = get_endpoint(alts, 0);
223 	if (!usb_endpoint_is_isoc_out(epd) ||
224 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
225 		return false;
226 
227 	epd = get_endpoint(alts, 1);
228 	if (!usb_endpoint_is_isoc_in(epd) ||
229 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
230 	    ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
231 	     USB_ENDPOINT_USAGE_DATA &&
232 	     (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
233 	     USB_ENDPOINT_USAGE_IMPLICIT_FB))
234 		return false;
235 
236 	return true;
237 }
238 
239 static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
240 				     struct audioformat *fmt,
241 				     int iface, int altset)
242 {
243 	struct usb_host_interface *alts;
244 	struct usb_endpoint_descriptor *epd;
245 
246 	alts = snd_usb_get_host_interface(chip, iface, altset);
247 	if (!alts)
248 		return 0;
249 
250 	if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
251 	     alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
252 	    alts->desc.bNumEndpoints < 1)
253 		return 0;
254 	epd = get_endpoint(alts, 0);
255 	if (!usb_endpoint_is_isoc_in(epd) ||
256 	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
257 		return 0;
258 	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
259 				       iface, alts);
260 }
261 
262 /* More generic quirk: look for the sync EP next to the data EP */
263 static int add_generic_implicit_fb(struct snd_usb_audio *chip,
264 				   struct audioformat *fmt,
265 				   struct usb_host_interface *alts)
266 {
267 	if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
268 		return 0;
269 
270 	if (__add_generic_implicit_fb(chip, fmt,
271 				      alts->desc.bInterfaceNumber + 1,
272 				      alts->desc.bAlternateSetting))
273 		return 1;
274 	return __add_generic_implicit_fb(chip, fmt,
275 					 alts->desc.bInterfaceNumber - 1,
276 					 alts->desc.bAlternateSetting);
277 }
278 
279 static const struct snd_usb_implicit_fb_match *
280 find_implicit_fb_entry(struct snd_usb_audio *chip,
281 		       const struct snd_usb_implicit_fb_match *match,
282 		       const struct usb_host_interface *alts)
283 {
284 	for (; match->id; match++)
285 		if (match->id == chip->usb_id &&
286 		    (!match->iface_class ||
287 		     (alts->desc.bInterfaceClass == match->iface_class)))
288 			return match;
289 
290 	return NULL;
291 }
292 
293 /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
294  * applies. Returns 1 if a quirk was found.
295  */
296 static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
297 					 struct audioformat *fmt,
298 					 struct usb_host_interface *alts)
299 {
300 	const struct snd_usb_implicit_fb_match *p;
301 	unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
302 
303 	p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
304 	if (p) {
305 		switch (p->type) {
306 		case IMPLICIT_FB_GENERIC:
307 			return add_generic_implicit_fb(chip, fmt, alts);
308 		case IMPLICIT_FB_NONE:
309 			return 0; /* No quirk */
310 		case IMPLICIT_FB_FIXED:
311 			return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
312 						       p->iface, NULL);
313 		}
314 	}
315 
316 	/* Special handling for devices with capture quirks */
317 	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
318 	if (p) {
319 		switch (p->type) {
320 		case IMPLICIT_FB_FIXED:
321 			return 0; /* no quirk */
322 		case IMPLICIT_FB_BOTH:
323 			chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
324 			return add_generic_implicit_fb(chip, fmt, alts);
325 		}
326 	}
327 
328 	/* Generic UAC2 implicit feedback */
329 	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
330 	    alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
331 	    alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
332 	    alts->desc.bNumEndpoints == 1) {
333 		if (add_generic_uac2_implicit_fb(chip, fmt,
334 						 alts->desc.bInterfaceNumber + 1,
335 						 alts->desc.bAlternateSetting))
336 			return 1;
337 	}
338 
339 	/* Roland/BOSS implicit feedback with vendor spec class */
340 	if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
341 		if (add_roland_implicit_fb(chip, fmt, alts) > 0)
342 			return 1;
343 	}
344 
345 	/* Pioneer devices with vendor spec class */
346 	if (is_pioneer_implicit_fb(chip, alts)) {
347 		chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
348 		return add_implicit_fb_sync_ep(chip, fmt,
349 					       get_endpoint(alts, 1)->bEndpointAddress,
350 					       1, alts->desc.bInterfaceNumber,
351 					       alts);
352 	}
353 
354 	/* Try the generic implicit fb if available */
355 	if (chip->generic_implicit_fb)
356 		return add_generic_implicit_fb(chip, fmt, alts);
357 
358 	/* No quirk */
359 	return 0;
360 }
361 
362 /* same for capture, but only handling FIXED entry */
363 static int audioformat_capture_quirk(struct snd_usb_audio *chip,
364 				     struct audioformat *fmt,
365 				     struct usb_host_interface *alts)
366 {
367 	const struct snd_usb_implicit_fb_match *p;
368 
369 	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
370 	if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
371 		return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
372 					       p->iface, NULL);
373 
374 	/* Roland/BOSS need full-duplex streams */
375 	if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
376 		if (add_roland_capture_quirk(chip, fmt, alts) > 0)
377 			return 1;
378 	}
379 
380 	if (is_pioneer_implicit_fb(chip, alts))
381 		return 1; /* skip the quirk, also don't handle generic sync EP */
382 	return 0;
383 }
384 
385 /*
386  * Parse altset and set up implicit feedback endpoint on the audioformat
387  */
388 int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
389 				    struct audioformat *fmt,
390 				    struct usb_host_interface *alts)
391 {
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  */
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 *
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 *
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 {
462 	struct snd_usb_substream *subs;
463 	const struct audioformat *fp, *sync_fmt = NULL;
464 	int score, high_score;
465 
466 	/* Use the original audioformat as fallback for the shared altset */
467 	if (target->iface == target->sync_iface &&
468 	    target->altsetting == target->sync_altsetting)
469 		sync_fmt = target;
470 
471 	subs = find_matching_substream(chip, stream, target->sync_ep,
472 				       target->fmt_type);
473 	if (!subs)
474 		return sync_fmt;
475 
476 	high_score = 0;
477 	list_for_each_entry(fp, &subs->fmt_list, list) {
478 		score = match_endpoint_audioformats(subs, fp,
479 						    params_rate(params),
480 						    params_channels(params),
481 						    params_format(params));
482 		if (score > high_score) {
483 			sync_fmt = fp;
484 			high_score = score;
485 		}
486 	}
487 
488 	return sync_fmt;
489 }
490 
491