Linux Audio

Check our new training course

Loading...
v5.14.15
  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
 20enum {
 21	IMPLICIT_FB_NONE,
 22	IMPLICIT_FB_GENERIC,
 23	IMPLICIT_FB_FIXED,
 24	IMPLICIT_FB_BOTH,	/* generic playback + capture (for BOSS) */
 25};
 26
 27struct 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 */
 47static 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 */
 81static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
 82	{} /* terminator */
 83};
 84
 85/* set up sync EP information on the audioformat */
 86static 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 */
114static 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
139static 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 */
150static 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->playback_first = 1;
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 */
180static 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 */
208static 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
239static 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 */
263static 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
279static const struct snd_usb_implicit_fb_match *
280find_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 */
296static 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->playback_first = 1;
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->playback_first = 1;
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 */
363static 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 */
388int 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 */
405static 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
437static struct snd_usb_substream *
438find_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 */
456const struct audioformat *
457snd_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
v6.2
  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
 21enum {
 22	IMPLICIT_FB_NONE,
 23	IMPLICIT_FB_GENERIC,
 24	IMPLICIT_FB_FIXED,
 25	IMPLICIT_FB_BOTH,	/* generic playback + capture (for BOSS) */
 26};
 27
 28struct 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 */
 48static 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(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
 61	IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
 62	IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
 63	IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
 64
 65	/* Special matching */
 66	{ .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
 67	  .type = IMPLICIT_FB_NONE },		/* MicroBook IIc */
 68	/* ep = 0x84, ifnum = 0 */
 69	{ .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
 70	  .type = IMPLICIT_FB_FIXED,
 71	  .ep_num = 0x84, .iface = 0 },		/* MOTU MicroBook II */
 72
 73	{} /* terminator */
 74};
 75
 76/* Implicit feedback quirk table for capture: only FIXED type */
 77static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
 78	{} /* terminator */
 79};
 80
 81/* set up sync EP information on the audioformat */
 82static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
 83				   struct audioformat *fmt,
 84				   int ep, int ep_idx, int ifnum,
 85				   const struct usb_host_interface *alts)
 86{
 87	struct usb_interface *iface;
 88
 89	if (!alts) {
 90		iface = usb_ifnum_to_if(chip->dev, ifnum);
 91		if (!iface || iface->num_altsetting < 2)
 92			return 0;
 93		alts = &iface->altsetting[1];
 94	}
 95
 96	fmt->sync_ep = ep;
 97	fmt->sync_iface = ifnum;
 98	fmt->sync_altsetting = alts->desc.bAlternateSetting;
 99	fmt->sync_ep_idx = ep_idx;
100	fmt->implicit_fb = 1;
101	usb_audio_dbg(chip,
102		      "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
103		      fmt->iface, fmt->altsetting,
104		      (ep & USB_DIR_IN) ? "playback" : "capture",
105		      fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
106	return 1;
107}
108
109/* Check whether the given UAC2 iface:altset points to an implicit fb source */
110static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
111					struct audioformat *fmt,
112					unsigned int ifnum,
113					unsigned int altsetting)
114{
115	struct usb_host_interface *alts;
116	struct usb_endpoint_descriptor *epd;
117
118	alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
119	if (!alts)
120		return 0;
121	if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
122	    alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
123	    alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
124	    alts->desc.bNumEndpoints < 1)
125		return 0;
126	epd = get_endpoint(alts, 0);
127	if (!usb_endpoint_is_isoc_in(epd) ||
128	    (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
129					USB_ENDPOINT_USAGE_IMPLICIT_FB)
130		return 0;
131	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
132				       ifnum, alts);
133}
134
135static bool roland_sanity_check_iface(struct usb_host_interface *alts)
136{
137	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
138	    (alts->desc.bInterfaceSubClass != 2 &&
139	     alts->desc.bInterfaceProtocol != 2) ||
140	    alts->desc.bNumEndpoints < 1)
141		return false;
142	return true;
143}
144
145/* Like the UAC2 case above, but specific to Roland with vendor class and hack */
146static int add_roland_implicit_fb(struct snd_usb_audio *chip,
147				  struct audioformat *fmt,
148				  struct usb_host_interface *alts)
149{
150	struct usb_endpoint_descriptor *epd;
151
152	if (!roland_sanity_check_iface(alts))
153		return 0;
154	/* only when both streams are with ASYNC type */
155	epd = get_endpoint(alts, 0);
156	if (!usb_endpoint_is_isoc_out(epd) ||
157	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
158		return 0;
159
160	/* check capture EP */
161	alts = snd_usb_get_host_interface(chip,
162					  alts->desc.bInterfaceNumber + 1,
163					  alts->desc.bAlternateSetting);
164	if (!alts || !roland_sanity_check_iface(alts))
165		return 0;
166	epd = get_endpoint(alts, 0);
167	if (!usb_endpoint_is_isoc_in(epd) ||
168	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
169		return 0;
170	chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
171	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
172				       alts->desc.bInterfaceNumber, alts);
173}
174
175/* capture quirk for Roland device; always full-duplex */
176static int add_roland_capture_quirk(struct snd_usb_audio *chip,
177				    struct audioformat *fmt,
178				    struct usb_host_interface *alts)
179{
180	struct usb_endpoint_descriptor *epd;
181
182	if (!roland_sanity_check_iface(alts))
183		return 0;
184	epd = get_endpoint(alts, 0);
185	if (!usb_endpoint_is_isoc_in(epd) ||
186	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
187		return 0;
188
189	alts = snd_usb_get_host_interface(chip,
190					  alts->desc.bInterfaceNumber - 1,
191					  alts->desc.bAlternateSetting);
192	if (!alts || !roland_sanity_check_iface(alts))
193		return 0;
194	epd = get_endpoint(alts, 0);
195	if (!usb_endpoint_is_isoc_out(epd))
196		return 0;
197	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
198				       alts->desc.bInterfaceNumber, alts);
199}
200
201/* Playback and capture EPs on Pioneer devices share the same iface/altset
202 * for the implicit feedback operation
203 */
204static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
205				   struct usb_host_interface *alts)
206
207{
208	struct usb_endpoint_descriptor *epd;
209
210	if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
211	    USB_ID_VENDOR(chip->usb_id) != 0x08e4)
212		return false;
213	if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
214		return false;
215	if (alts->desc.bNumEndpoints != 2)
216		return false;
217
218	epd = get_endpoint(alts, 0);
219	if (!usb_endpoint_is_isoc_out(epd) ||
220	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
221		return false;
222
223	epd = get_endpoint(alts, 1);
224	if (!usb_endpoint_is_isoc_in(epd) ||
225	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
226	    ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
227	     USB_ENDPOINT_USAGE_DATA &&
228	     (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
229	     USB_ENDPOINT_USAGE_IMPLICIT_FB))
230		return false;
231
232	return true;
233}
234
235static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
236				     struct audioformat *fmt,
237				     int iface, int altset)
238{
239	struct usb_host_interface *alts;
240	struct usb_endpoint_descriptor *epd;
241
242	alts = snd_usb_get_host_interface(chip, iface, altset);
243	if (!alts)
244		return 0;
245
246	if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
247	     alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
248	    alts->desc.bNumEndpoints < 1)
249		return 0;
250	epd = get_endpoint(alts, 0);
251	if (!usb_endpoint_is_isoc_in(epd) ||
252	    (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
253		return 0;
254	return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
255				       iface, alts);
256}
257
258/* More generic quirk: look for the sync EP next to the data EP */
259static int add_generic_implicit_fb(struct snd_usb_audio *chip,
260				   struct audioformat *fmt,
261				   struct usb_host_interface *alts)
262{
263	if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
264		return 0;
265
266	if (__add_generic_implicit_fb(chip, fmt,
267				      alts->desc.bInterfaceNumber + 1,
268				      alts->desc.bAlternateSetting))
269		return 1;
270	return __add_generic_implicit_fb(chip, fmt,
271					 alts->desc.bInterfaceNumber - 1,
272					 alts->desc.bAlternateSetting);
273}
274
275static const struct snd_usb_implicit_fb_match *
276find_implicit_fb_entry(struct snd_usb_audio *chip,
277		       const struct snd_usb_implicit_fb_match *match,
278		       const struct usb_host_interface *alts)
279{
280	for (; match->id; match++)
281		if (match->id == chip->usb_id &&
282		    (!match->iface_class ||
283		     (alts->desc.bInterfaceClass == match->iface_class)))
284			return match;
285
286	return NULL;
287}
288
289/* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
290 * applies. Returns 1 if a quirk was found.
291 */
292static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
293					 struct audioformat *fmt,
294					 struct usb_host_interface *alts)
295{
296	const struct snd_usb_implicit_fb_match *p;
297	unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
298
299	p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
300	if (p) {
301		switch (p->type) {
302		case IMPLICIT_FB_GENERIC:
303			return add_generic_implicit_fb(chip, fmt, alts);
304		case IMPLICIT_FB_NONE:
305			return 0; /* No quirk */
306		case IMPLICIT_FB_FIXED:
307			return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
308						       p->iface, NULL);
309		}
310	}
311
312	/* Special handling for devices with capture quirks */
313	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
314	if (p) {
315		switch (p->type) {
316		case IMPLICIT_FB_FIXED:
317			return 0; /* no quirk */
318		case IMPLICIT_FB_BOTH:
319			chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
320			return add_generic_implicit_fb(chip, fmt, alts);
321		}
322	}
323
324	/* Generic UAC2 implicit feedback */
325	if (attr == USB_ENDPOINT_SYNC_ASYNC &&
326	    alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
327	    alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
328	    alts->desc.bNumEndpoints == 1) {
329		if (add_generic_uac2_implicit_fb(chip, fmt,
330						 alts->desc.bInterfaceNumber + 1,
331						 alts->desc.bAlternateSetting))
332			return 1;
333	}
334
335	/* Roland/BOSS implicit feedback with vendor spec class */
336	if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
337		if (add_roland_implicit_fb(chip, fmt, alts) > 0)
338			return 1;
339	}
340
341	/* Pioneer devices with vendor spec class */
342	if (is_pioneer_implicit_fb(chip, alts)) {
343		chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
344		return add_implicit_fb_sync_ep(chip, fmt,
345					       get_endpoint(alts, 1)->bEndpointAddress,
346					       1, alts->desc.bInterfaceNumber,
347					       alts);
348	}
349
350	/* Try the generic implicit fb if available */
351	if (chip->generic_implicit_fb ||
352	    (chip->quirk_flags & QUIRK_FLAG_GENERIC_IMPLICIT_FB))
353		return add_generic_implicit_fb(chip, fmt, alts);
354
355	/* No quirk */
356	return 0;
357}
358
359/* same for capture, but only handling FIXED entry */
360static int audioformat_capture_quirk(struct snd_usb_audio *chip,
361				     struct audioformat *fmt,
362				     struct usb_host_interface *alts)
363{
364	const struct snd_usb_implicit_fb_match *p;
365
366	p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
367	if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
368		return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
369					       p->iface, NULL);
370
371	/* Roland/BOSS need full-duplex streams */
372	if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
373		if (add_roland_capture_quirk(chip, fmt, alts) > 0)
374			return 1;
375	}
376
377	if (is_pioneer_implicit_fb(chip, alts))
378		return 1; /* skip the quirk, also don't handle generic sync EP */
379	return 0;
380}
381
382/*
383 * Parse altset and set up implicit feedback endpoint on the audioformat
384 */
385int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
386				    struct audioformat *fmt,
387				    struct usb_host_interface *alts)
388{
389	if (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB)
390		return 0;
391	if (fmt->endpoint & USB_DIR_IN)
392		return audioformat_capture_quirk(chip, fmt, alts);
393	else
394		return audioformat_implicit_fb_quirk(chip, fmt, alts);
395}
396
397/*
398 * Return the score of matching two audioformats.
399 * Veto the audioformat if:
400 * - It has no channels for some reason.
401 * - Requested PCM format is not supported.
402 * - Requested sample rate is not supported.
403 */
404static int match_endpoint_audioformats(struct snd_usb_substream *subs,
405				       const struct audioformat *fp,
406				       int rate, int channels,
407				       snd_pcm_format_t pcm_format)
408{
409	int i, score;
410
411	if (fp->channels < 1)
412		return 0;
413
414	if (!(fp->formats & pcm_format_to_bits(pcm_format)))
415		return 0;
416
417	if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
418		if (rate < fp->rate_min || rate > fp->rate_max)
419			return 0;
420	} else {
421		for (i = 0; i < fp->nr_rates; i++) {
422			if (fp->rate_table[i] == rate)
423				break;
424		}
425		if (i >= fp->nr_rates)
426			return 0;
427	}
428
429	score = 1;
430	if (fp->channels == channels)
431		score++;
432
433	return score;
434}
435
436static struct snd_usb_substream *
437find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
438			int fmt_type)
439{
440	struct snd_usb_stream *as;
441	struct snd_usb_substream *subs;
442
443	list_for_each_entry(as, &chip->pcm_list, list) {
444		subs = &as->substream[stream];
445		if (as->fmt_type == fmt_type && subs->ep_num == ep_num)
446			return subs;
447	}
448
449	return NULL;
450}
451
452/*
453 * Return the audioformat that is suitable for the implicit fb
454 */
455const struct audioformat *
456snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
457				     const struct audioformat *target,
458				     const struct snd_pcm_hw_params *params,
459				     int stream,
460				     bool *fixed_rate)
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		goto end;
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 end:
489	if (fixed_rate)
490		*fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
491	return sync_fmt;
492}
493