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