blob: 5bc84b460d725e449b2bcb98c2466ae8f86fdead [file] [log] [blame]
Daniel Macke8e8bab2011-09-12 18:54:12 +02001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 */
16
17
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/usb.h>
21#include <linux/usb/audio.h>
22#include <linux/usb/audio-v2.h>
Ajay Agarwalabed8d42017-02-07 16:40:09 +053023#include <linux/usb/audio-v3.h>
Daniel Macke8e8bab2011-09-12 18:54:12 +020024
25#include <sound/core.h>
26#include <sound/pcm.h>
Takashi Iwai04324cc2012-11-26 16:24:02 +010027#include <sound/control.h>
28#include <sound/tlv.h>
Daniel Macke8e8bab2011-09-12 18:54:12 +020029
30#include "usbaudio.h"
31#include "card.h"
32#include "proc.h"
33#include "quirks.h"
34#include "endpoint.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020035#include "pcm.h"
36#include "helper.h"
37#include "format.h"
38#include "clock.h"
39#include "stream.h"
40
41/*
42 * free a substream
43 */
44static void free_substream(struct snd_usb_substream *subs)
45{
Eldad Zack88766f02013-04-03 23:18:49 +020046 struct audioformat *fp, *n;
Daniel Macke8e8bab2011-09-12 18:54:12 +020047
48 if (!subs->num_formats)
49 return; /* not initialized */
Eldad Zack88766f02013-04-03 23:18:49 +020050 list_for_each_entry_safe(fp, n, &subs->fmt_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +020051 kfree(fp->rate_table);
Takashi Iwai04324cc2012-11-26 16:24:02 +010052 kfree(fp->chmap);
Daniel Macke8e8bab2011-09-12 18:54:12 +020053 kfree(fp);
54 }
55 kfree(subs->rate_list.list);
56}
57
58
59/*
60 * free a usb stream instance
61 */
62static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
63{
64 free_substream(&stream->substream[0]);
65 free_substream(&stream->substream[1]);
66 list_del(&stream->list);
67 kfree(stream);
68}
69
70static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
71{
72 struct snd_usb_stream *stream = pcm->private_data;
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080073 struct snd_usb_audio *chip;
74
Daniel Macke8e8bab2011-09-12 18:54:12 +020075 if (stream) {
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080076 mutex_lock(&stream->chip->dev_lock);
77 chip = stream->chip;
Daniel Macke8e8bab2011-09-12 18:54:12 +020078 stream->pcm = NULL;
79 snd_usb_audio_stream_free(stream);
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080080 mutex_unlock(&chip->dev_lock);
Daniel Macke8e8bab2011-09-12 18:54:12 +020081 }
82}
83
Daniel Mackedcd3632012-04-12 13:51:12 +020084/*
85 * initialize the substream instance.
86 */
87
88static void snd_usb_init_substream(struct snd_usb_stream *as,
89 int stream,
90 struct audioformat *fp)
91{
92 struct snd_usb_substream *subs = &as->substream[stream];
93
94 INIT_LIST_HEAD(&subs->fmt_list);
95 spin_lock_init(&subs->lock);
96
97 subs->stream = as;
98 subs->direction = stream;
99 subs->dev = as->chip->dev;
100 subs->txfr_quirk = as->chip->txfr_quirk;
Ricard Wanderlofe0570442015-10-19 08:52:53 +0200101 subs->tx_length_quirk = as->chip->tx_length_quirk;
Takashi Iwai978520b2012-10-12 15:12:55 +0200102 subs->speed = snd_usb_get_speed(subs->dev);
Calvin Owens1539d4f2013-04-12 22:33:59 -0500103 subs->pkt_offset_adj = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200104
105 snd_usb_set_pcm_ops(as->pcm, stream);
106
107 list_add_tail(&fp->list, &subs->fmt_list);
108 subs->formats |= fp->formats;
109 subs->num_formats++;
110 subs->fmt_type = fp->fmt_type;
Takashi Iwai8260ef02012-06-08 09:01:37 +0200111 subs->ep_num = fp->endpoint;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100112 if (fp->channels > subs->channels_max)
113 subs->channels_max = fp->channels;
114}
115
116/* kctl callbacks for usb-audio channel maps */
117static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
118 struct snd_ctl_elem_info *uinfo)
119{
120 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
121 struct snd_usb_substream *subs = info->private_data;
122
123 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
124 uinfo->count = subs->channels_max;
125 uinfo->value.integer.min = 0;
126 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
127 return 0;
128}
129
130/* check whether a duplicated entry exists in the audiofmt list */
131static bool have_dup_chmap(struct snd_usb_substream *subs,
132 struct audioformat *fp)
133{
Geliang Tangf67d71a2015-12-21 23:55:39 +0800134 struct audioformat *prev = fp;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100135
Geliang Tangf67d71a2015-12-21 23:55:39 +0800136 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
Takashi Iwai04324cc2012-11-26 16:24:02 +0100137 if (prev->chmap &&
138 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
139 return true;
140 }
141 return false;
142}
143
144static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
145 unsigned int size, unsigned int __user *tlv)
146{
147 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
148 struct snd_usb_substream *subs = info->private_data;
149 struct audioformat *fp;
150 unsigned int __user *dst;
151 int count = 0;
152
153 if (size < 8)
154 return -ENOMEM;
155 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
156 return -EFAULT;
157 size -= 8;
158 dst = tlv + 2;
159 list_for_each_entry(fp, &subs->fmt_list, list) {
160 int i, ch_bytes;
161
162 if (!fp->chmap)
163 continue;
164 if (have_dup_chmap(subs, fp))
165 continue;
166 /* copy the entry */
167 ch_bytes = fp->chmap->channels * 4;
168 if (size < 8 + ch_bytes)
169 return -ENOMEM;
170 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
171 put_user(ch_bytes, dst + 1))
172 return -EFAULT;
173 dst += 2;
174 for (i = 0; i < fp->chmap->channels; i++, dst++) {
175 if (put_user(fp->chmap->map[i], dst))
176 return -EFAULT;
177 }
178
179 count += 8 + ch_bytes;
180 size -= 8 + ch_bytes;
181 }
182 if (put_user(count, tlv + 1))
183 return -EFAULT;
184 return 0;
185}
186
187static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
188 struct snd_ctl_elem_value *ucontrol)
189{
190 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
191 struct snd_usb_substream *subs = info->private_data;
192 struct snd_pcm_chmap_elem *chmap = NULL;
193 int i;
194
195 memset(ucontrol->value.integer.value, 0,
196 sizeof(ucontrol->value.integer.value));
197 if (subs->cur_audiofmt)
198 chmap = subs->cur_audiofmt->chmap;
199 if (chmap) {
200 for (i = 0; i < chmap->channels; i++)
201 ucontrol->value.integer.value[i] = chmap->map[i];
202 }
203 return 0;
204}
205
206/* create a chmap kctl assigned to the given USB substream */
207static int add_chmap(struct snd_pcm *pcm, int stream,
208 struct snd_usb_substream *subs)
209{
210 struct audioformat *fp;
211 struct snd_pcm_chmap *chmap;
212 struct snd_kcontrol *kctl;
213 int err;
214
215 list_for_each_entry(fp, &subs->fmt_list, list)
216 if (fp->chmap)
217 goto ok;
218 /* no chmap is found */
219 return 0;
220
221 ok:
222 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
223 if (err < 0)
224 return err;
225
226 /* override handlers */
227 chmap->private_data = subs;
228 kctl = chmap->kctl;
229 kctl->info = usb_chmap_ctl_info;
230 kctl->get = usb_chmap_ctl_get;
231 kctl->tlv.c = usb_chmap_ctl_tlv;
232
233 return 0;
234}
235
236/* convert from USB ChannelConfig bits to ALSA chmap element */
237static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
238 int protocol)
239{
240 static unsigned int uac1_maps[] = {
241 SNDRV_CHMAP_FL, /* left front */
242 SNDRV_CHMAP_FR, /* right front */
243 SNDRV_CHMAP_FC, /* center front */
244 SNDRV_CHMAP_LFE, /* LFE */
245 SNDRV_CHMAP_SL, /* left surround */
246 SNDRV_CHMAP_SR, /* right surround */
247 SNDRV_CHMAP_FLC, /* left of center */
248 SNDRV_CHMAP_FRC, /* right of center */
249 SNDRV_CHMAP_RC, /* surround */
250 SNDRV_CHMAP_SL, /* side left */
251 SNDRV_CHMAP_SR, /* side right */
252 SNDRV_CHMAP_TC, /* top */
253 0 /* terminator */
254 };
255 static unsigned int uac2_maps[] = {
256 SNDRV_CHMAP_FL, /* front left */
257 SNDRV_CHMAP_FR, /* front right */
258 SNDRV_CHMAP_FC, /* front center */
259 SNDRV_CHMAP_LFE, /* LFE */
260 SNDRV_CHMAP_RL, /* back left */
261 SNDRV_CHMAP_RR, /* back right */
262 SNDRV_CHMAP_FLC, /* front left of center */
263 SNDRV_CHMAP_FRC, /* front right of center */
264 SNDRV_CHMAP_RC, /* back center */
265 SNDRV_CHMAP_SL, /* side left */
266 SNDRV_CHMAP_SR, /* side right */
267 SNDRV_CHMAP_TC, /* top center */
268 SNDRV_CHMAP_TFL, /* top front left */
269 SNDRV_CHMAP_TFC, /* top front center */
270 SNDRV_CHMAP_TFR, /* top front right */
271 SNDRV_CHMAP_TRL, /* top back left */
272 SNDRV_CHMAP_TRC, /* top back center */
273 SNDRV_CHMAP_TRR, /* top back right */
274 SNDRV_CHMAP_TFLC, /* top front left of center */
275 SNDRV_CHMAP_TFRC, /* top front right of center */
276 SNDRV_CHMAP_LLFE, /* left LFE */
277 SNDRV_CHMAP_RLFE, /* right LFE */
278 SNDRV_CHMAP_TSL, /* top side left */
279 SNDRV_CHMAP_TSR, /* top side right */
280 SNDRV_CHMAP_BC, /* bottom center */
Anssi Hannula71373fd2013-11-10 21:24:05 +0200281 SNDRV_CHMAP_RLC, /* back left of center */
282 SNDRV_CHMAP_RRC, /* back right of center */
Takashi Iwai04324cc2012-11-26 16:24:02 +0100283 0 /* terminator */
284 };
285 struct snd_pcm_chmap_elem *chmap;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100286
Takashi Iwai04324cc2012-11-26 16:24:02 +0100287 if (channels > ARRAY_SIZE(chmap->map))
288 return NULL;
289
290 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
291 if (!chmap)
292 return NULL;
293
Takashi Iwai04324cc2012-11-26 16:24:02 +0100294 chmap->channels = channels;
David Henningsson0dca01c2013-11-05 04:41:06 +0100295
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530296 if (protocol == UAC_VERSION_3) {
297 switch (channels) {
298 case 1:
299 chmap->map[0] = SNDRV_CHMAP_MONO;
300 break;
301 case 2:
302 chmap->map[0] = SNDRV_CHMAP_FL;
303 chmap->map[1] = SNDRV_CHMAP_FR;
304 break;
305 }
David Henningsson0dca01c2013-11-05 04:41:06 +0100306 } else {
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530307 int c = 0;
308 const unsigned int *maps =
309 protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100310
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530311 if (bits) {
312 for (; bits && *maps; maps++, bits >>= 1)
313 if (bits & 1)
314 chmap->map[c++] = *maps;
315 } else {
316 /*
317 * If we're missing wChannelConfig, then guess something
318 * to make sure the channel map is not skipped entirely
319 */
320 if (channels == 1)
321 chmap->map[c++] = SNDRV_CHMAP_MONO;
322 else
323 for (; c < channels && *maps; maps++)
324 chmap->map[c++] = *maps;
325 }
326 for (; c < channels; c++)
327 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
328 }
Takashi Iwai04324cc2012-11-26 16:24:02 +0100329
330 return chmap;
Daniel Mackedcd3632012-04-12 13:51:12 +0200331}
Daniel Macke8e8bab2011-09-12 18:54:12 +0200332
333/*
334 * add this endpoint to the chip instance.
335 * if a stream with the same endpoint already exists, append to it.
Vladis Dronov836b34a2016-03-31 12:05:43 -0400336 * if not, create a new pcm stream. note, fp is added to the substream
337 * fmt_list and will be freed on the chip instance release. do not free
338 * fp or do remove it from the substream fmt_list to avoid double-free.
Daniel Macke8e8bab2011-09-12 18:54:12 +0200339 */
340int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
341 int stream,
342 struct audioformat *fp)
343{
Daniel Macke8e8bab2011-09-12 18:54:12 +0200344 struct snd_usb_stream *as;
345 struct snd_usb_substream *subs;
346 struct snd_pcm *pcm;
347 int err;
348
Eldad Zack88766f02013-04-03 23:18:49 +0200349 list_for_each_entry(as, &chip->pcm_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200350 if (as->fmt_type != fp->fmt_type)
351 continue;
352 subs = &as->substream[stream];
Takashi Iwai8260ef02012-06-08 09:01:37 +0200353 if (subs->ep_num == fp->endpoint) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200354 list_add_tail(&fp->list, &subs->fmt_list);
355 subs->num_formats++;
356 subs->formats |= fp->formats;
357 return 0;
358 }
359 }
360 /* look for an empty stream */
Eldad Zack88766f02013-04-03 23:18:49 +0200361 list_for_each_entry(as, &chip->pcm_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200362 if (as->fmt_type != fp->fmt_type)
363 continue;
364 subs = &as->substream[stream];
Takashi Iwai8260ef02012-06-08 09:01:37 +0200365 if (subs->ep_num)
Daniel Macke8e8bab2011-09-12 18:54:12 +0200366 continue;
367 err = snd_pcm_new_stream(as->pcm, stream, 1);
368 if (err < 0)
369 return err;
370 snd_usb_init_substream(as, stream, fp);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100371 return add_chmap(as->pcm, stream, subs);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200372 }
373
374 /* create a new pcm */
375 as = kzalloc(sizeof(*as), GFP_KERNEL);
376 if (!as)
377 return -ENOMEM;
378 as->pcm_index = chip->pcm_devs;
379 as->chip = chip;
380 as->fmt_type = fp->fmt_type;
381 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
382 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
383 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
384 &pcm);
385 if (err < 0) {
386 kfree(as);
387 return err;
388 }
389 as->pcm = pcm;
390 pcm->private_data = as;
391 pcm->private_free = snd_usb_audio_pcm_free;
392 pcm->info_flags = 0;
393 if (chip->pcm_devs > 0)
394 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
395 else
396 strcpy(pcm->name, "USB Audio");
397
398 snd_usb_init_substream(as, stream, fp);
399
Johan Rastén5ee20bc2015-09-06 18:16:13 +0200400 /*
401 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
402 * fix to swap capture stream order in conf/cards/USB-audio.conf
403 */
404 if (chip->usb_id == USB_ID(0x0763, 0x2003))
405 list_add(&as->list, &chip->pcm_list);
406 else
407 list_add_tail(&as->list, &chip->pcm_list);
408
Daniel Macke8e8bab2011-09-12 18:54:12 +0200409 chip->pcm_devs++;
410
411 snd_usb_proc_pcm_format_add(as);
412
Takashi Iwai04324cc2012-11-26 16:24:02 +0100413 return add_chmap(pcm, stream, &as->substream[stream]);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200414}
415
416static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
417 struct usb_host_interface *alts,
418 int protocol, int iface_no)
419{
420 /* parsed with a v1 header here. that's ok as we only look at the
421 * header first which is the same for both versions */
422 struct uac_iso_endpoint_descriptor *csep;
423 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
424 int attributes = 0;
425
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530426 if (protocol == UAC_VERSION_3)
427 return 0;
428
Daniel Macke8e8bab2011-09-12 18:54:12 +0200429 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
430
431 /* Creamware Noah has this descriptor after the 2nd endpoint */
432 if (!csep && altsd->bNumEndpoints >= 2)
433 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
434
Daniel Mackebfc5942013-04-24 19:38:42 +0200435 /*
436 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
437 * bytes after the first endpoint, go search the entire interface.
438 * Some devices have it directly *before* the standard endpoint.
439 */
440 if (!csep)
441 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
442
Daniel Macke8e8bab2011-09-12 18:54:12 +0200443 if (!csep || csep->bLength < 7 ||
444 csep->bDescriptorSubtype != UAC_EP_GENERAL) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100445 usb_audio_warn(chip,
446 "%u:%d : no or invalid class specific endpoint descriptor\n",
447 iface_no, altsd->bAlternateSetting);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200448 return 0;
449 }
450
451 if (protocol == UAC_VERSION_1) {
452 attributes = csep->bmAttributes;
453 } else {
454 struct uac2_iso_endpoint_descriptor *csep2 =
455 (struct uac2_iso_endpoint_descriptor *) csep;
456
457 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
458
459 /* emulate the endpoint attributes of a v1 device */
460 if (csep2->bmControls & UAC2_CONTROL_PITCH)
461 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
462 }
463
464 return attributes;
465}
466
Takashi Iwai04324cc2012-11-26 16:24:02 +0100467/* find an input terminal descriptor (either UAC1 or UAC2) with the given
468 * terminal id
469 */
470static void *
471snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
Daniel Macke8e8bab2011-09-12 18:54:12 +0200472 int terminal_id)
473{
474 struct uac2_input_terminal_descriptor *term = NULL;
475
476 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
477 ctrl_iface->extralen,
478 term, UAC_INPUT_TERMINAL))) {
479 if (term->bTerminalID == terminal_id)
480 return term;
481 }
482
483 return NULL;
484}
485
486static struct uac2_output_terminal_descriptor *
487 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
488 int terminal_id)
489{
490 struct uac2_output_terminal_descriptor *term = NULL;
491
492 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
493 ctrl_iface->extralen,
494 term, UAC_OUTPUT_TERMINAL))) {
495 if (term->bTerminalID == terminal_id)
496 return term;
497 }
498
499 return NULL;
500}
501
502int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
503{
504 struct usb_device *dev;
505 struct usb_interface *iface;
506 struct usb_host_interface *alts;
507 struct usb_interface_descriptor *altsd;
508 int i, altno, err, stream;
Daniel Mack2fcdb062013-03-17 20:07:24 +0800509 unsigned int format = 0, num_channels = 0;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200510 struct audioformat *fp = NULL;
511 int num, protocol, clock = 0;
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530512 struct uac_format_type_i_continuous_descriptor *fmt = NULL;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100513 unsigned int chconfig;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200514
515 dev = chip->dev;
516
517 /* parse the interface's altsettings */
518 iface = usb_ifnum_to_if(dev, iface_no);
519
520 num = iface->num_altsetting;
521
522 /*
523 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
524 * one misses syncpipe, and does not produce any sound.
525 */
526 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
527 num = 4;
528
529 for (i = 0; i < num; i++) {
530 alts = &iface->altsetting[i];
531 altsd = get_iface_desc(alts);
532 protocol = altsd->bInterfaceProtocol;
533 /* skip invalid one */
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200534 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
535 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
536 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
Daniel Macke8e8bab2011-09-12 18:54:12 +0200537 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
Daniel Macke8e8bab2011-09-12 18:54:12 +0200538 altsd->bNumEndpoints < 1 ||
539 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
540 continue;
541 /* must be isochronous */
542 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
543 USB_ENDPOINT_XFER_ISOC)
544 continue;
545 /* check direction */
546 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
547 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
548 altno = altsd->bAlternateSetting;
549
550 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
551 continue;
552
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200553 /*
554 * Roland audio streaming interfaces are marked with protocols
555 * 0/1/2, but are UAC 1 compatible.
556 */
557 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
558 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
559 protocol <= 2)
560 protocol = UAC_VERSION_1;
561
Takashi Iwai04324cc2012-11-26 16:24:02 +0100562 chconfig = 0;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200563 /* get audio formats */
564 switch (protocol) {
565 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100566 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
567 iface_no, altno, protocol);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200568 protocol = UAC_VERSION_1;
569 /* fall through */
570
571 case UAC_VERSION_1: {
572 struct uac1_as_header_descriptor *as =
573 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100574 struct uac_input_terminal_descriptor *iterm;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200575
576 if (!as) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100577 dev_err(&dev->dev,
578 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
579 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200580 continue;
581 }
582
583 if (as->bLength < sizeof(*as)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100584 dev_err(&dev->dev,
585 "%u:%d : invalid UAC_AS_GENERAL desc\n",
586 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200587 continue;
588 }
589
590 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
Takashi Iwai04324cc2012-11-26 16:24:02 +0100591
592 iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
593 as->bTerminalLink);
594 if (iterm) {
595 num_channels = iterm->bNrChannels;
596 chconfig = le16_to_cpu(iterm->wChannelConfig);
597 }
598
Daniel Macke8e8bab2011-09-12 18:54:12 +0200599 break;
600 }
601
602 case UAC_VERSION_2: {
603 struct uac2_input_terminal_descriptor *input_term;
604 struct uac2_output_terminal_descriptor *output_term;
605 struct uac2_as_header_descriptor *as =
606 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
607
608 if (!as) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100609 dev_err(&dev->dev,
610 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
611 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200612 continue;
613 }
614
615 if (as->bLength < sizeof(*as)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100616 dev_err(&dev->dev,
617 "%u:%d : invalid UAC_AS_GENERAL desc\n",
618 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200619 continue;
620 }
621
622 num_channels = as->bNrChannels;
623 format = le32_to_cpu(as->bmFormats);
David Henningssone3e35f72013-11-05 04:41:07 +0100624 chconfig = le32_to_cpu(as->bmChannelConfig);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200625
626 /* lookup the terminal associated to this interface
627 * to extract the clock */
628 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
629 as->bTerminalLink);
630 if (input_term) {
631 clock = input_term->bCSourceID;
David Henningssone3e35f72013-11-05 04:41:07 +0100632 if (!chconfig && (num_channels == input_term->bNrChannels))
633 chconfig = le32_to_cpu(input_term->bmChannelConfig);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200634 break;
635 }
636
637 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
638 as->bTerminalLink);
639 if (output_term) {
640 clock = output_term->bCSourceID;
641 break;
642 }
643
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100644 dev_err(&dev->dev,
645 "%u:%d : bogus bTerminalLink %d\n",
646 iface_no, altno, as->bTerminalLink);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200647 continue;
648 }
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530649
650 case UAC_VERSION_3: {
651 int wMaxPacketSize;
652
653 format = UAC_FORMAT_TYPE_I_PCM;
654 clock = BADD_CLOCK_SOURCE;
655 wMaxPacketSize = le16_to_cpu(get_endpoint(alts, 0)
656 ->wMaxPacketSize);
657 switch (wMaxPacketSize) {
658 case BADD_MAXPSIZE_SYNC_MONO_16:
659 case BADD_MAXPSIZE_SYNC_MONO_24:
660 case BADD_MAXPSIZE_ASYNC_MONO_16:
661 case BADD_MAXPSIZE_ASYNC_MONO_24: {
662 num_channels = NUM_CHANNELS_MONO;
663 chconfig = BADD_CH_CONFIG_MONO;
664 break;
665 }
666
667 case BADD_MAXPSIZE_SYNC_STEREO_16:
668 case BADD_MAXPSIZE_SYNC_STEREO_24:
669 case BADD_MAXPSIZE_ASYNC_STEREO_16:
670 case BADD_MAXPSIZE_ASYNC_STEREO_24: {
671 num_channels = NUM_CHANNELS_STEREO;
672 chconfig = BADD_CH_CONFIG_STEREO;
673 break;
674 }
675 default:
676 dev_err(&dev->dev,
677 "%u:%d: invalid wMaxPacketSize\n",
678 iface_no, altno);
679 continue;
680 }
681 }
Daniel Macke8e8bab2011-09-12 18:54:12 +0200682 }
683
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530684 if ((protocol == UAC_VERSION_1) ||
685 (protocol == UAC_VERSION_2)) {
686 /* get format type */
687 fmt = snd_usb_find_csint_desc(alts->extra,
688 alts->extralen, NULL, UAC_FORMAT_TYPE);
689 if (!fmt) {
690 dev_err(&dev->dev,
691 "%u:%d : no UAC_FORMAT_TYPE desc\n",
692 iface_no, altno);
693 continue;
694 }
695 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
696 || ((protocol == UAC_VERSION_2) &&
697 (fmt->bLength < 6))) {
698 dev_err(&dev->dev,
699 "%u:%d :invalid UAC_FORMAT_TYPE desc\n",
700 iface_no, altno);
701 continue;
702 }
Daniel Macke8e8bab2011-09-12 18:54:12 +0200703
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530704 /*
705 * Blue Microphones workaround: The last altsetting is
706 * identical with the previous one, except for a larger
707 * packet size, but is actually a mislabeled two-channel
708 * setting; ignore it.
709 */
710 if (fmt->bNrChannels == 1 &&
711 fmt->bSubframeSize == 2 &&
712 altno == 2 && num == 3 &&
713 fp && fp->altsetting == 1 && fp->channels == 1 &&
714 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
715 protocol == UAC_VERSION_1 &&
716 le16_to_cpu(
717 get_endpoint(alts, 0)->wMaxPacketSize) ==
Daniel Macke8e8bab2011-09-12 18:54:12 +0200718 fp->maxpacksize * 2)
Ajay Agarwalabed8d42017-02-07 16:40:09 +0530719 continue;
720 }
Daniel Macke8e8bab2011-09-12 18:54:12 +0200721
722 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
723 if (! fp) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100724 dev_err(&dev->dev, "cannot malloc\n");
Daniel Macke8e8bab2011-09-12 18:54:12 +0200725 return -ENOMEM;
726 }
727
728 fp->iface = iface_no;
729 fp->altsetting = altno;
730 fp->altset_idx = i;
731 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
732 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
733 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100734 fp->protocol = protocol;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200735 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200736 fp->channels = num_channels;
737 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
738 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
739 * (fp->maxpacksize & 0x7ff);
740 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
741 fp->clock = clock;
Vladis Dronov836b34a2016-03-31 12:05:43 -0400742 INIT_LIST_HEAD(&fp->list);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200743
744 /* some quirks for attributes here */
745
746 switch (chip->usb_id) {
747 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
748 /* Optoplay sets the sample rate attribute although
749 * it seems not supporting it in fact.
750 */
751 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
752 break;
753 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
754 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
755 /* doesn't set the sample rate attribute, but supports it */
756 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
757 break;
758 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
759 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
760 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
761 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
762 an older model 77d:223) */
763 /*
764 * plantronics headset and Griffin iMic have set adaptive-in
765 * although it's really not...
766 */
767 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
768 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
769 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
770 else
771 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
772 break;
773 }
774
775 /* ok, let's parse further... */
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100776 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200777 kfree(fp->rate_table);
778 kfree(fp);
779 fp = NULL;
780 continue;
781 }
782
David Henningsson504333d2013-11-05 04:41:08 +0100783 /* Create chmap */
784 if (fp->channels != num_channels)
785 chconfig = 0;
786 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
787
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100788 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200789 err = snd_usb_add_audio_stream(chip, stream, fp);
790 if (err < 0) {
Vladis Dronov836b34a2016-03-31 12:05:43 -0400791 list_del(&fp->list); /* unlink for avoiding double-free */
Daniel Macke8e8bab2011-09-12 18:54:12 +0200792 kfree(fp->rate_table);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100793 kfree(fp->chmap);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200794 kfree(fp);
795 return err;
796 }
797 /* try to set the interface... */
798 usb_set_interface(chip->dev, iface_no, altno);
799 snd_usb_init_pitch(chip, iface_no, alts, fp);
800 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
801 }
802 return 0;
803}
804