blob: 7437cd51a362097d8189aec63da296387939cb98 [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>
23
24#include <sound/core.h>
25#include <sound/pcm.h>
Takashi Iwai04324cc2012-11-26 16:24:02 +010026#include <sound/control.h>
27#include <sound/tlv.h>
Daniel Macke8e8bab2011-09-12 18:54:12 +020028
29#include "usbaudio.h"
30#include "card.h"
31#include "proc.h"
32#include "quirks.h"
33#include "endpoint.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020034#include "pcm.h"
35#include "helper.h"
36#include "format.h"
37#include "clock.h"
38#include "stream.h"
39
40/*
41 * free a substream
42 */
43static void free_substream(struct snd_usb_substream *subs)
44{
Eldad Zack88766f02013-04-03 23:18:49 +020045 struct audioformat *fp, *n;
Daniel Macke8e8bab2011-09-12 18:54:12 +020046
47 if (!subs->num_formats)
48 return; /* not initialized */
Eldad Zack88766f02013-04-03 23:18:49 +020049 list_for_each_entry_safe(fp, n, &subs->fmt_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +020050 kfree(fp->rate_table);
Takashi Iwai04324cc2012-11-26 16:24:02 +010051 kfree(fp->chmap);
Daniel Macke8e8bab2011-09-12 18:54:12 +020052 kfree(fp);
53 }
54 kfree(subs->rate_list.list);
55}
56
57
58/*
59 * free a usb stream instance
60 */
61static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
62{
63 free_substream(&stream->substream[0]);
64 free_substream(&stream->substream[1]);
65 list_del(&stream->list);
66 kfree(stream);
67}
68
69static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
70{
71 struct snd_usb_stream *stream = pcm->private_data;
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080072 struct snd_usb_audio *chip;
73
Daniel Macke8e8bab2011-09-12 18:54:12 +020074 if (stream) {
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080075 mutex_lock(&stream->chip->dev_lock);
76 chip = stream->chip;
Daniel Macke8e8bab2011-09-12 18:54:12 +020077 stream->pcm = NULL;
78 snd_usb_audio_stream_free(stream);
Hemant Kumar0ef9fbb2016-01-28 11:41:42 -080079 mutex_unlock(&chip->dev_lock);
Daniel Macke8e8bab2011-09-12 18:54:12 +020080 }
81}
82
Daniel Mackedcd3632012-04-12 13:51:12 +020083/*
84 * initialize the substream instance.
85 */
86
87static void snd_usb_init_substream(struct snd_usb_stream *as,
88 int stream,
89 struct audioformat *fp)
90{
91 struct snd_usb_substream *subs = &as->substream[stream];
92
93 INIT_LIST_HEAD(&subs->fmt_list);
94 spin_lock_init(&subs->lock);
95
96 subs->stream = as;
97 subs->direction = stream;
98 subs->dev = as->chip->dev;
99 subs->txfr_quirk = as->chip->txfr_quirk;
Ricard Wanderlofe0570442015-10-19 08:52:53 +0200100 subs->tx_length_quirk = as->chip->tx_length_quirk;
Takashi Iwai978520b2012-10-12 15:12:55 +0200101 subs->speed = snd_usb_get_speed(subs->dev);
Calvin Owens1539d4f2013-04-12 22:33:59 -0500102 subs->pkt_offset_adj = 0;
Daniel Mackedcd3632012-04-12 13:51:12 +0200103
104 snd_usb_set_pcm_ops(as->pcm, stream);
105
106 list_add_tail(&fp->list, &subs->fmt_list);
107 subs->formats |= fp->formats;
108 subs->num_formats++;
109 subs->fmt_type = fp->fmt_type;
Takashi Iwai8260ef02012-06-08 09:01:37 +0200110 subs->ep_num = fp->endpoint;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100111 if (fp->channels > subs->channels_max)
112 subs->channels_max = fp->channels;
113}
114
115/* kctl callbacks for usb-audio channel maps */
116static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
117 struct snd_ctl_elem_info *uinfo)
118{
119 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
120 struct snd_usb_substream *subs = info->private_data;
121
122 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
123 uinfo->count = subs->channels_max;
124 uinfo->value.integer.min = 0;
125 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
126 return 0;
127}
128
129/* check whether a duplicated entry exists in the audiofmt list */
130static bool have_dup_chmap(struct snd_usb_substream *subs,
131 struct audioformat *fp)
132{
Geliang Tangf67d71a2015-12-21 23:55:39 +0800133 struct audioformat *prev = fp;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100134
Geliang Tangf67d71a2015-12-21 23:55:39 +0800135 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
Takashi Iwai04324cc2012-11-26 16:24:02 +0100136 if (prev->chmap &&
137 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
138 return true;
139 }
140 return false;
141}
142
143static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
144 unsigned int size, unsigned int __user *tlv)
145{
146 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
147 struct snd_usb_substream *subs = info->private_data;
148 struct audioformat *fp;
149 unsigned int __user *dst;
150 int count = 0;
151
152 if (size < 8)
153 return -ENOMEM;
154 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
155 return -EFAULT;
156 size -= 8;
157 dst = tlv + 2;
158 list_for_each_entry(fp, &subs->fmt_list, list) {
159 int i, ch_bytes;
160
161 if (!fp->chmap)
162 continue;
163 if (have_dup_chmap(subs, fp))
164 continue;
165 /* copy the entry */
166 ch_bytes = fp->chmap->channels * 4;
167 if (size < 8 + ch_bytes)
168 return -ENOMEM;
169 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
170 put_user(ch_bytes, dst + 1))
171 return -EFAULT;
172 dst += 2;
173 for (i = 0; i < fp->chmap->channels; i++, dst++) {
174 if (put_user(fp->chmap->map[i], dst))
175 return -EFAULT;
176 }
177
178 count += 8 + ch_bytes;
179 size -= 8 + ch_bytes;
180 }
181 if (put_user(count, tlv + 1))
182 return -EFAULT;
183 return 0;
184}
185
186static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
187 struct snd_ctl_elem_value *ucontrol)
188{
189 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
190 struct snd_usb_substream *subs = info->private_data;
191 struct snd_pcm_chmap_elem *chmap = NULL;
192 int i;
193
194 memset(ucontrol->value.integer.value, 0,
195 sizeof(ucontrol->value.integer.value));
196 if (subs->cur_audiofmt)
197 chmap = subs->cur_audiofmt->chmap;
198 if (chmap) {
199 for (i = 0; i < chmap->channels; i++)
200 ucontrol->value.integer.value[i] = chmap->map[i];
201 }
202 return 0;
203}
204
205/* create a chmap kctl assigned to the given USB substream */
206static int add_chmap(struct snd_pcm *pcm, int stream,
207 struct snd_usb_substream *subs)
208{
209 struct audioformat *fp;
210 struct snd_pcm_chmap *chmap;
211 struct snd_kcontrol *kctl;
212 int err;
213
214 list_for_each_entry(fp, &subs->fmt_list, list)
215 if (fp->chmap)
216 goto ok;
217 /* no chmap is found */
218 return 0;
219
220 ok:
221 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
222 if (err < 0)
223 return err;
224
225 /* override handlers */
226 chmap->private_data = subs;
227 kctl = chmap->kctl;
228 kctl->info = usb_chmap_ctl_info;
229 kctl->get = usb_chmap_ctl_get;
230 kctl->tlv.c = usb_chmap_ctl_tlv;
231
232 return 0;
233}
234
235/* convert from USB ChannelConfig bits to ALSA chmap element */
236static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
237 int protocol)
238{
239 static unsigned int uac1_maps[] = {
240 SNDRV_CHMAP_FL, /* left front */
241 SNDRV_CHMAP_FR, /* right front */
242 SNDRV_CHMAP_FC, /* center front */
243 SNDRV_CHMAP_LFE, /* LFE */
244 SNDRV_CHMAP_SL, /* left surround */
245 SNDRV_CHMAP_SR, /* right surround */
246 SNDRV_CHMAP_FLC, /* left of center */
247 SNDRV_CHMAP_FRC, /* right of center */
248 SNDRV_CHMAP_RC, /* surround */
249 SNDRV_CHMAP_SL, /* side left */
250 SNDRV_CHMAP_SR, /* side right */
251 SNDRV_CHMAP_TC, /* top */
252 0 /* terminator */
253 };
254 static unsigned int uac2_maps[] = {
255 SNDRV_CHMAP_FL, /* front left */
256 SNDRV_CHMAP_FR, /* front right */
257 SNDRV_CHMAP_FC, /* front center */
258 SNDRV_CHMAP_LFE, /* LFE */
259 SNDRV_CHMAP_RL, /* back left */
260 SNDRV_CHMAP_RR, /* back right */
261 SNDRV_CHMAP_FLC, /* front left of center */
262 SNDRV_CHMAP_FRC, /* front right of center */
263 SNDRV_CHMAP_RC, /* back center */
264 SNDRV_CHMAP_SL, /* side left */
265 SNDRV_CHMAP_SR, /* side right */
266 SNDRV_CHMAP_TC, /* top center */
267 SNDRV_CHMAP_TFL, /* top front left */
268 SNDRV_CHMAP_TFC, /* top front center */
269 SNDRV_CHMAP_TFR, /* top front right */
270 SNDRV_CHMAP_TRL, /* top back left */
271 SNDRV_CHMAP_TRC, /* top back center */
272 SNDRV_CHMAP_TRR, /* top back right */
273 SNDRV_CHMAP_TFLC, /* top front left of center */
274 SNDRV_CHMAP_TFRC, /* top front right of center */
275 SNDRV_CHMAP_LLFE, /* left LFE */
276 SNDRV_CHMAP_RLFE, /* right LFE */
277 SNDRV_CHMAP_TSL, /* top side left */
278 SNDRV_CHMAP_TSR, /* top side right */
279 SNDRV_CHMAP_BC, /* bottom center */
Anssi Hannula71373fd2013-11-10 21:24:05 +0200280 SNDRV_CHMAP_RLC, /* back left of center */
281 SNDRV_CHMAP_RRC, /* back right of center */
Takashi Iwai04324cc2012-11-26 16:24:02 +0100282 0 /* terminator */
283 };
284 struct snd_pcm_chmap_elem *chmap;
285 const unsigned int *maps;
286 int c;
287
Takashi Iwai04324cc2012-11-26 16:24:02 +0100288 if (channels > ARRAY_SIZE(chmap->map))
289 return NULL;
290
291 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
292 if (!chmap)
293 return NULL;
294
295 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
296 chmap->channels = channels;
297 c = 0;
David Henningsson0dca01c2013-11-05 04:41:06 +0100298
299 if (bits) {
300 for (; bits && *maps; maps++, bits >>= 1)
301 if (bits & 1)
302 chmap->map[c++] = *maps;
303 } else {
304 /* If we're missing wChannelConfig, then guess something
305 to make sure the channel map is not skipped entirely */
306 if (channels == 1)
307 chmap->map[c++] = SNDRV_CHMAP_MONO;
308 else
309 for (; c < channels && *maps; maps++)
310 chmap->map[c++] = *maps;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100311 }
312
313 for (; c < channels; c++)
314 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
315
316 return chmap;
Daniel Mackedcd3632012-04-12 13:51:12 +0200317}
Daniel Macke8e8bab2011-09-12 18:54:12 +0200318
319/*
320 * add this endpoint to the chip instance.
321 * if a stream with the same endpoint already exists, append to it.
Vladis Dronov836b34a2016-03-31 12:05:43 -0400322 * if not, create a new pcm stream. note, fp is added to the substream
323 * fmt_list and will be freed on the chip instance release. do not free
324 * fp or do remove it from the substream fmt_list to avoid double-free.
Daniel Macke8e8bab2011-09-12 18:54:12 +0200325 */
326int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
327 int stream,
328 struct audioformat *fp)
329{
Daniel Macke8e8bab2011-09-12 18:54:12 +0200330 struct snd_usb_stream *as;
331 struct snd_usb_substream *subs;
332 struct snd_pcm *pcm;
333 int err;
334
Eldad Zack88766f02013-04-03 23:18:49 +0200335 list_for_each_entry(as, &chip->pcm_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200336 if (as->fmt_type != fp->fmt_type)
337 continue;
338 subs = &as->substream[stream];
Takashi Iwai8260ef02012-06-08 09:01:37 +0200339 if (subs->ep_num == fp->endpoint) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200340 list_add_tail(&fp->list, &subs->fmt_list);
341 subs->num_formats++;
342 subs->formats |= fp->formats;
343 return 0;
344 }
345 }
346 /* look for an empty stream */
Eldad Zack88766f02013-04-03 23:18:49 +0200347 list_for_each_entry(as, &chip->pcm_list, list) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200348 if (as->fmt_type != fp->fmt_type)
349 continue;
350 subs = &as->substream[stream];
Takashi Iwai8260ef02012-06-08 09:01:37 +0200351 if (subs->ep_num)
Daniel Macke8e8bab2011-09-12 18:54:12 +0200352 continue;
353 err = snd_pcm_new_stream(as->pcm, stream, 1);
354 if (err < 0)
355 return err;
356 snd_usb_init_substream(as, stream, fp);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100357 return add_chmap(as->pcm, stream, subs);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200358 }
359
360 /* create a new pcm */
361 as = kzalloc(sizeof(*as), GFP_KERNEL);
362 if (!as)
363 return -ENOMEM;
364 as->pcm_index = chip->pcm_devs;
365 as->chip = chip;
366 as->fmt_type = fp->fmt_type;
367 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
368 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
369 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
370 &pcm);
371 if (err < 0) {
372 kfree(as);
373 return err;
374 }
375 as->pcm = pcm;
376 pcm->private_data = as;
377 pcm->private_free = snd_usb_audio_pcm_free;
378 pcm->info_flags = 0;
379 if (chip->pcm_devs > 0)
380 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
381 else
382 strcpy(pcm->name, "USB Audio");
383
384 snd_usb_init_substream(as, stream, fp);
385
Johan Rastén5ee20bc2015-09-06 18:16:13 +0200386 /*
387 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
388 * fix to swap capture stream order in conf/cards/USB-audio.conf
389 */
390 if (chip->usb_id == USB_ID(0x0763, 0x2003))
391 list_add(&as->list, &chip->pcm_list);
392 else
393 list_add_tail(&as->list, &chip->pcm_list);
394
Daniel Macke8e8bab2011-09-12 18:54:12 +0200395 chip->pcm_devs++;
396
397 snd_usb_proc_pcm_format_add(as);
398
Takashi Iwai04324cc2012-11-26 16:24:02 +0100399 return add_chmap(pcm, stream, &as->substream[stream]);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200400}
401
402static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
403 struct usb_host_interface *alts,
404 int protocol, int iface_no)
405{
406 /* parsed with a v1 header here. that's ok as we only look at the
407 * header first which is the same for both versions */
408 struct uac_iso_endpoint_descriptor *csep;
409 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
410 int attributes = 0;
411
412 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
413
414 /* Creamware Noah has this descriptor after the 2nd endpoint */
415 if (!csep && altsd->bNumEndpoints >= 2)
416 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
417
Daniel Mackebfc5942013-04-24 19:38:42 +0200418 /*
419 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
420 * bytes after the first endpoint, go search the entire interface.
421 * Some devices have it directly *before* the standard endpoint.
422 */
423 if (!csep)
424 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
425
Daniel Macke8e8bab2011-09-12 18:54:12 +0200426 if (!csep || csep->bLength < 7 ||
427 csep->bDescriptorSubtype != UAC_EP_GENERAL) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100428 usb_audio_warn(chip,
429 "%u:%d : no or invalid class specific endpoint descriptor\n",
430 iface_no, altsd->bAlternateSetting);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200431 return 0;
432 }
433
434 if (protocol == UAC_VERSION_1) {
435 attributes = csep->bmAttributes;
436 } else {
437 struct uac2_iso_endpoint_descriptor *csep2 =
438 (struct uac2_iso_endpoint_descriptor *) csep;
439
440 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
441
442 /* emulate the endpoint attributes of a v1 device */
443 if (csep2->bmControls & UAC2_CONTROL_PITCH)
444 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
445 }
446
447 return attributes;
448}
449
Takashi Iwai04324cc2012-11-26 16:24:02 +0100450/* find an input terminal descriptor (either UAC1 or UAC2) with the given
451 * terminal id
452 */
453static void *
454snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
Daniel Macke8e8bab2011-09-12 18:54:12 +0200455 int terminal_id)
456{
457 struct uac2_input_terminal_descriptor *term = NULL;
458
459 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
460 ctrl_iface->extralen,
461 term, UAC_INPUT_TERMINAL))) {
462 if (term->bTerminalID == terminal_id)
463 return term;
464 }
465
466 return NULL;
467}
468
469static struct uac2_output_terminal_descriptor *
470 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
471 int terminal_id)
472{
473 struct uac2_output_terminal_descriptor *term = NULL;
474
475 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
476 ctrl_iface->extralen,
477 term, UAC_OUTPUT_TERMINAL))) {
478 if (term->bTerminalID == terminal_id)
479 return term;
480 }
481
482 return NULL;
483}
484
485int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
486{
487 struct usb_device *dev;
488 struct usb_interface *iface;
489 struct usb_host_interface *alts;
490 struct usb_interface_descriptor *altsd;
491 int i, altno, err, stream;
Daniel Mack2fcdb062013-03-17 20:07:24 +0800492 unsigned int format = 0, num_channels = 0;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200493 struct audioformat *fp = NULL;
494 int num, protocol, clock = 0;
495 struct uac_format_type_i_continuous_descriptor *fmt;
Takashi Iwai04324cc2012-11-26 16:24:02 +0100496 unsigned int chconfig;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200497
498 dev = chip->dev;
499
500 /* parse the interface's altsettings */
501 iface = usb_ifnum_to_if(dev, iface_no);
502
503 num = iface->num_altsetting;
504
505 /*
506 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
507 * one misses syncpipe, and does not produce any sound.
508 */
509 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
510 num = 4;
511
512 for (i = 0; i < num; i++) {
513 alts = &iface->altsetting[i];
514 altsd = get_iface_desc(alts);
515 protocol = altsd->bInterfaceProtocol;
516 /* skip invalid one */
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200517 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
518 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
519 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
Daniel Macke8e8bab2011-09-12 18:54:12 +0200520 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
Daniel Macke8e8bab2011-09-12 18:54:12 +0200521 altsd->bNumEndpoints < 1 ||
522 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
523 continue;
524 /* must be isochronous */
525 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
526 USB_ENDPOINT_XFER_ISOC)
527 continue;
528 /* check direction */
529 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
530 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
531 altno = altsd->bAlternateSetting;
532
533 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
534 continue;
535
Clemens Ladischaafe77c2013-03-31 23:43:12 +0200536 /*
537 * Roland audio streaming interfaces are marked with protocols
538 * 0/1/2, but are UAC 1 compatible.
539 */
540 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
541 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
542 protocol <= 2)
543 protocol = UAC_VERSION_1;
544
Takashi Iwai04324cc2012-11-26 16:24:02 +0100545 chconfig = 0;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200546 /* get audio formats */
547 switch (protocol) {
548 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100549 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
550 iface_no, altno, protocol);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200551 protocol = UAC_VERSION_1;
552 /* fall through */
553
554 case UAC_VERSION_1: {
555 struct uac1_as_header_descriptor *as =
556 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100557 struct uac_input_terminal_descriptor *iterm;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200558
559 if (!as) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100560 dev_err(&dev->dev,
561 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
562 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200563 continue;
564 }
565
566 if (as->bLength < sizeof(*as)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100567 dev_err(&dev->dev,
568 "%u:%d : invalid UAC_AS_GENERAL desc\n",
569 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200570 continue;
571 }
572
573 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
Takashi Iwai04324cc2012-11-26 16:24:02 +0100574
575 iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
576 as->bTerminalLink);
577 if (iterm) {
578 num_channels = iterm->bNrChannels;
579 chconfig = le16_to_cpu(iterm->wChannelConfig);
580 }
581
Daniel Macke8e8bab2011-09-12 18:54:12 +0200582 break;
583 }
584
585 case UAC_VERSION_2: {
586 struct uac2_input_terminal_descriptor *input_term;
587 struct uac2_output_terminal_descriptor *output_term;
588 struct uac2_as_header_descriptor *as =
589 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
590
591 if (!as) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100592 dev_err(&dev->dev,
593 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
594 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200595 continue;
596 }
597
598 if (as->bLength < sizeof(*as)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100599 dev_err(&dev->dev,
600 "%u:%d : invalid UAC_AS_GENERAL desc\n",
601 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200602 continue;
603 }
604
605 num_channels = as->bNrChannels;
606 format = le32_to_cpu(as->bmFormats);
David Henningssone3e35f72013-11-05 04:41:07 +0100607 chconfig = le32_to_cpu(as->bmChannelConfig);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200608
609 /* lookup the terminal associated to this interface
610 * to extract the clock */
611 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
612 as->bTerminalLink);
613 if (input_term) {
614 clock = input_term->bCSourceID;
David Henningssone3e35f72013-11-05 04:41:07 +0100615 if (!chconfig && (num_channels == input_term->bNrChannels))
616 chconfig = le32_to_cpu(input_term->bmChannelConfig);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200617 break;
618 }
619
620 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
621 as->bTerminalLink);
622 if (output_term) {
623 clock = output_term->bCSourceID;
624 break;
625 }
626
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100627 dev_err(&dev->dev,
628 "%u:%d : bogus bTerminalLink %d\n",
629 iface_no, altno, as->bTerminalLink);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200630 continue;
631 }
632 }
633
634 /* get format type */
635 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
636 if (!fmt) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100637 dev_err(&dev->dev,
638 "%u:%d : no UAC_FORMAT_TYPE desc\n",
639 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200640 continue;
641 }
642 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
643 ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100644 dev_err(&dev->dev,
645 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
646 iface_no, altno);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200647 continue;
648 }
649
650 /*
651 * Blue Microphones workaround: The last altsetting is identical
652 * with the previous one, except for a larger packet size, but
653 * is actually a mislabeled two-channel setting; ignore it.
654 */
655 if (fmt->bNrChannels == 1 &&
656 fmt->bSubframeSize == 2 &&
657 altno == 2 && num == 3 &&
658 fp && fp->altsetting == 1 && fp->channels == 1 &&
659 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
660 protocol == UAC_VERSION_1 &&
661 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
662 fp->maxpacksize * 2)
663 continue;
664
665 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
666 if (! fp) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100667 dev_err(&dev->dev, "cannot malloc\n");
Daniel Macke8e8bab2011-09-12 18:54:12 +0200668 return -ENOMEM;
669 }
670
671 fp->iface = iface_no;
672 fp->altsetting = altno;
673 fp->altset_idx = i;
674 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
675 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
676 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100677 fp->protocol = protocol;
Daniel Macke8e8bab2011-09-12 18:54:12 +0200678 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200679 fp->channels = num_channels;
680 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
681 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
682 * (fp->maxpacksize & 0x7ff);
683 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
684 fp->clock = clock;
Vladis Dronov836b34a2016-03-31 12:05:43 -0400685 INIT_LIST_HEAD(&fp->list);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200686
687 /* some quirks for attributes here */
688
689 switch (chip->usb_id) {
690 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
691 /* Optoplay sets the sample rate attribute although
692 * it seems not supporting it in fact.
693 */
694 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
695 break;
696 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
697 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
698 /* doesn't set the sample rate attribute, but supports it */
699 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
700 break;
701 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
702 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
703 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
704 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
705 an older model 77d:223) */
706 /*
707 * plantronics headset and Griffin iMic have set adaptive-in
708 * although it's really not...
709 */
710 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
711 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
712 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
713 else
714 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
715 break;
716 }
717
718 /* ok, let's parse further... */
Clemens Ladisch8f898e92013-01-31 21:39:17 +0100719 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream) < 0) {
Daniel Macke8e8bab2011-09-12 18:54:12 +0200720 kfree(fp->rate_table);
721 kfree(fp);
722 fp = NULL;
723 continue;
724 }
725
David Henningsson504333d2013-11-05 04:41:08 +0100726 /* Create chmap */
727 if (fp->channels != num_channels)
728 chconfig = 0;
729 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
730
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100731 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200732 err = snd_usb_add_audio_stream(chip, stream, fp);
733 if (err < 0) {
Vladis Dronov836b34a2016-03-31 12:05:43 -0400734 list_del(&fp->list); /* unlink for avoiding double-free */
Daniel Macke8e8bab2011-09-12 18:54:12 +0200735 kfree(fp->rate_table);
Takashi Iwai04324cc2012-11-26 16:24:02 +0100736 kfree(fp->chmap);
Daniel Macke8e8bab2011-09-12 18:54:12 +0200737 kfree(fp);
738 return err;
739 }
740 /* try to set the interface... */
741 usb_set_interface(chip->dev, iface_no, altno);
742 snd_usb_init_pitch(chip, iface_no, alts, fp);
743 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
744 }
745 return 0;
746}
747