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