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