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.
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.
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
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 #include <linux/usb/audio-v3.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/control.h>
28 #include <sound/tlv.h>
45 static void free_substream(struct snd_usb_substream *subs)
47 struct audioformat *fp, *n;
49 if (!subs->num_formats)
50 return; /* not initialized */
51 list_for_each_entry_safe(fp, n, &subs->fmt_list, list) {
52 kfree(fp->rate_table);
56 kfree(subs->rate_list.list);
62 * free a usb stream instance
64 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
66 free_substream(&stream->substream[0]);
67 free_substream(&stream->substream[1]);
68 list_del(&stream->list);
72 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
74 struct snd_usb_stream *stream = pcm->private_data;
77 snd_usb_audio_stream_free(stream);
82 * initialize the substream instance.
85 static void snd_usb_init_substream(struct snd_usb_stream *as,
87 struct audioformat *fp,
88 struct snd_usb_power_domain *pd)
90 struct snd_usb_substream *subs = &as->substream[stream];
92 INIT_LIST_HEAD(&subs->fmt_list);
93 spin_lock_init(&subs->lock);
96 subs->direction = stream;
97 subs->dev = as->chip->dev;
98 subs->txfr_quirk = as->chip->txfr_quirk;
99 subs->tx_length_quirk = as->chip->tx_length_quirk;
100 subs->speed = snd_usb_get_speed(subs->dev);
101 subs->pkt_offset_adj = 0;
102 subs->stream_offset_adj = 0;
104 snd_usb_set_pcm_ops(as->pcm, stream);
106 list_add_tail(&fp->list, &subs->fmt_list);
107 subs->formats |= fp->formats;
109 subs->fmt_type = fp->fmt_type;
110 subs->ep_num = fp->endpoint;
111 if (fp->channels > subs->channels_max)
112 subs->channels_max = fp->channels;
116 /* Initialize Power Domain to idle status D1 */
117 snd_usb_power_domain_set(subs->stream->chip, pd,
121 snd_usb_preallocate_buffer(subs);
124 /* kctl callbacks for usb-audio channel maps */
125 static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
126 struct snd_ctl_elem_info *uinfo)
128 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
129 struct snd_usb_substream *subs = info->private_data;
131 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
132 uinfo->count = subs->channels_max;
133 uinfo->value.integer.min = 0;
134 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
138 /* check whether a duplicated entry exists in the audiofmt list */
139 static bool have_dup_chmap(struct snd_usb_substream *subs,
140 struct audioformat *fp)
142 struct audioformat *prev = fp;
144 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
146 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
152 static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
153 unsigned int size, unsigned int __user *tlv)
155 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
156 struct snd_usb_substream *subs = info->private_data;
157 struct audioformat *fp;
158 unsigned int __user *dst;
163 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
167 list_for_each_entry(fp, &subs->fmt_list, list) {
172 if (have_dup_chmap(subs, fp))
175 ch_bytes = fp->chmap->channels * 4;
176 if (size < 8 + ch_bytes)
178 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
179 put_user(ch_bytes, dst + 1))
182 for (i = 0; i < fp->chmap->channels; i++, dst++) {
183 if (put_user(fp->chmap->map[i], dst))
187 count += 8 + ch_bytes;
188 size -= 8 + ch_bytes;
190 if (put_user(count, tlv + 1))
195 static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_value *ucontrol)
198 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
199 struct snd_usb_substream *subs = info->private_data;
200 struct snd_pcm_chmap_elem *chmap = NULL;
203 if (subs->cur_audiofmt)
204 chmap = subs->cur_audiofmt->chmap;
206 for (i = 0; i < chmap->channels; i++)
207 ucontrol->value.integer.value[i] = chmap->map[i];
209 for (; i < subs->channels_max; i++)
210 ucontrol->value.integer.value[i] = 0;
214 /* create a chmap kctl assigned to the given USB substream */
215 static int add_chmap(struct snd_pcm *pcm, int stream,
216 struct snd_usb_substream *subs)
218 struct audioformat *fp;
219 struct snd_pcm_chmap *chmap;
220 struct snd_kcontrol *kctl;
223 list_for_each_entry(fp, &subs->fmt_list, list)
226 /* no chmap is found */
230 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
234 /* override handlers */
235 chmap->private_data = subs;
237 kctl->info = usb_chmap_ctl_info;
238 kctl->get = usb_chmap_ctl_get;
239 kctl->tlv.c = usb_chmap_ctl_tlv;
244 /* convert from USB ChannelConfig bits to ALSA chmap element */
245 static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
248 static const unsigned int uac1_maps[] = {
249 SNDRV_CHMAP_FL, /* left front */
250 SNDRV_CHMAP_FR, /* right front */
251 SNDRV_CHMAP_FC, /* center front */
252 SNDRV_CHMAP_LFE, /* LFE */
253 SNDRV_CHMAP_SL, /* left surround */
254 SNDRV_CHMAP_SR, /* right surround */
255 SNDRV_CHMAP_FLC, /* left of center */
256 SNDRV_CHMAP_FRC, /* right of center */
257 SNDRV_CHMAP_RC, /* surround */
258 SNDRV_CHMAP_SL, /* side left */
259 SNDRV_CHMAP_SR, /* side right */
260 SNDRV_CHMAP_TC, /* top */
263 static const unsigned int uac2_maps[] = {
264 SNDRV_CHMAP_FL, /* front left */
265 SNDRV_CHMAP_FR, /* front right */
266 SNDRV_CHMAP_FC, /* front center */
267 SNDRV_CHMAP_LFE, /* LFE */
268 SNDRV_CHMAP_RL, /* back left */
269 SNDRV_CHMAP_RR, /* back right */
270 SNDRV_CHMAP_FLC, /* front left of center */
271 SNDRV_CHMAP_FRC, /* front right of center */
272 SNDRV_CHMAP_RC, /* back center */
273 SNDRV_CHMAP_SL, /* side left */
274 SNDRV_CHMAP_SR, /* side right */
275 SNDRV_CHMAP_TC, /* top center */
276 SNDRV_CHMAP_TFL, /* top front left */
277 SNDRV_CHMAP_TFC, /* top front center */
278 SNDRV_CHMAP_TFR, /* top front right */
279 SNDRV_CHMAP_TRL, /* top back left */
280 SNDRV_CHMAP_TRC, /* top back center */
281 SNDRV_CHMAP_TRR, /* top back right */
282 SNDRV_CHMAP_TFLC, /* top front left of center */
283 SNDRV_CHMAP_TFRC, /* top front right of center */
284 SNDRV_CHMAP_LLFE, /* left LFE */
285 SNDRV_CHMAP_RLFE, /* right LFE */
286 SNDRV_CHMAP_TSL, /* top side left */
287 SNDRV_CHMAP_TSR, /* top side right */
288 SNDRV_CHMAP_BC, /* bottom center */
289 SNDRV_CHMAP_RLC, /* back left of center */
290 SNDRV_CHMAP_RRC, /* back right of center */
293 struct snd_pcm_chmap_elem *chmap;
294 const unsigned int *maps;
297 if (channels > ARRAY_SIZE(chmap->map))
300 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
304 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
305 chmap->channels = channels;
309 for (; bits && *maps; maps++, bits >>= 1) {
311 chmap->map[c++] = *maps;
312 if (c == chmap->channels)
316 /* If we're missing wChannelConfig, then guess something
317 to make sure the channel map is not skipped entirely */
319 chmap->map[c++] = SNDRV_CHMAP_MONO;
321 for (; c < channels && *maps; maps++)
322 chmap->map[c++] = *maps;
325 for (; c < channels; c++)
326 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
331 /* UAC3 device stores channels information in Cluster Descriptors */
333 snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
336 unsigned int channels = cluster->bNrChannels;
337 struct snd_pcm_chmap_elem *chmap;
341 if (channels > ARRAY_SIZE(chmap->map))
344 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
348 len = le16_to_cpu(cluster->wLength);
350 p += sizeof(struct uac3_cluster_header_descriptor);
352 while (((p - (void *)cluster) < len) && (c < channels)) {
353 struct uac3_cluster_segment_descriptor *cs_desc = p;
357 cs_len = le16_to_cpu(cs_desc->wLength);
358 cs_type = cs_desc->bSegmentType;
360 if (cs_type == UAC3_CHANNEL_INFORMATION) {
361 struct uac3_cluster_information_segment_descriptor *is = p;
365 * TODO: this conversion is not complete, update it
366 * after adding UAC3 values to asound.h
368 switch (is->bChRelationship) {
370 map = SNDRV_CHMAP_MONO;
373 case UAC3_CH_FRONT_LEFT:
374 case UAC3_CH_HEADPHONE_LEFT:
375 map = SNDRV_CHMAP_FL;
378 case UAC3_CH_FRONT_RIGHT:
379 case UAC3_CH_HEADPHONE_RIGHT:
380 map = SNDRV_CHMAP_FR;
382 case UAC3_CH_FRONT_CENTER:
383 map = SNDRV_CHMAP_FC;
385 case UAC3_CH_FRONT_LEFT_OF_CENTER:
386 map = SNDRV_CHMAP_FLC;
388 case UAC3_CH_FRONT_RIGHT_OF_CENTER:
389 map = SNDRV_CHMAP_FRC;
391 case UAC3_CH_SIDE_LEFT:
392 map = SNDRV_CHMAP_SL;
394 case UAC3_CH_SIDE_RIGHT:
395 map = SNDRV_CHMAP_SR;
397 case UAC3_CH_BACK_LEFT:
398 map = SNDRV_CHMAP_RL;
400 case UAC3_CH_BACK_RIGHT:
401 map = SNDRV_CHMAP_RR;
403 case UAC3_CH_BACK_CENTER:
404 map = SNDRV_CHMAP_RC;
406 case UAC3_CH_BACK_LEFT_OF_CENTER:
407 map = SNDRV_CHMAP_RLC;
409 case UAC3_CH_BACK_RIGHT_OF_CENTER:
410 map = SNDRV_CHMAP_RRC;
412 case UAC3_CH_TOP_CENTER:
413 map = SNDRV_CHMAP_TC;
415 case UAC3_CH_TOP_FRONT_LEFT:
416 map = SNDRV_CHMAP_TFL;
418 case UAC3_CH_TOP_FRONT_RIGHT:
419 map = SNDRV_CHMAP_TFR;
421 case UAC3_CH_TOP_FRONT_CENTER:
422 map = SNDRV_CHMAP_TFC;
424 case UAC3_CH_TOP_FRONT_LOC:
425 map = SNDRV_CHMAP_TFLC;
427 case UAC3_CH_TOP_FRONT_ROC:
428 map = SNDRV_CHMAP_TFRC;
430 case UAC3_CH_TOP_SIDE_LEFT:
431 map = SNDRV_CHMAP_TSL;
433 case UAC3_CH_TOP_SIDE_RIGHT:
434 map = SNDRV_CHMAP_TSR;
436 case UAC3_CH_TOP_BACK_LEFT:
437 map = SNDRV_CHMAP_TRL;
439 case UAC3_CH_TOP_BACK_RIGHT:
440 map = SNDRV_CHMAP_TRR;
442 case UAC3_CH_TOP_BACK_CENTER:
443 map = SNDRV_CHMAP_TRC;
445 case UAC3_CH_BOTTOM_CENTER:
446 map = SNDRV_CHMAP_BC;
448 case UAC3_CH_LOW_FREQUENCY_EFFECTS:
449 map = SNDRV_CHMAP_LFE;
451 case UAC3_CH_LFE_LEFT:
452 map = SNDRV_CHMAP_LLFE;
454 case UAC3_CH_LFE_RIGHT:
455 map = SNDRV_CHMAP_RLFE;
457 case UAC3_CH_RELATIONSHIP_UNDEFINED:
459 map = SNDRV_CHMAP_UNKNOWN;
462 chmap->map[c++] = map;
468 pr_err("%s: channel number mismatch\n", __func__);
470 chmap->channels = channels;
472 for (; c < channels; c++)
473 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
479 * add this endpoint to the chip instance.
480 * if a stream with the same endpoint already exists, append to it.
481 * if not, create a new pcm stream. note, fp is added to the substream
482 * fmt_list and will be freed on the chip instance release. do not free
483 * fp or do remove it from the substream fmt_list to avoid double-free.
485 static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip,
487 struct audioformat *fp,
488 struct snd_usb_power_domain *pd)
491 struct snd_usb_stream *as;
492 struct snd_usb_substream *subs;
496 list_for_each_entry(as, &chip->pcm_list, list) {
497 if (as->fmt_type != fp->fmt_type)
499 subs = &as->substream[stream];
500 if (subs->ep_num == fp->endpoint) {
501 list_add_tail(&fp->list, &subs->fmt_list);
503 subs->formats |= fp->formats;
507 /* look for an empty stream */
508 list_for_each_entry(as, &chip->pcm_list, list) {
509 if (as->fmt_type != fp->fmt_type)
511 subs = &as->substream[stream];
514 err = snd_pcm_new_stream(as->pcm, stream, 1);
517 snd_usb_init_substream(as, stream, fp, pd);
518 return add_chmap(as->pcm, stream, subs);
521 /* create a new pcm */
522 as = kzalloc(sizeof(*as), GFP_KERNEL);
525 as->pcm_index = chip->pcm_devs;
527 as->fmt_type = fp->fmt_type;
528 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
529 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
530 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
537 pcm->private_data = as;
538 pcm->private_free = snd_usb_audio_pcm_free;
540 if (chip->pcm_devs > 0)
541 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
543 strcpy(pcm->name, "USB Audio");
545 snd_usb_init_substream(as, stream, fp, pd);
548 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
549 * fix to swap capture stream order in conf/cards/USB-audio.conf
551 if (chip->usb_id == USB_ID(0x0763, 0x2003))
552 list_add(&as->list, &chip->pcm_list);
554 list_add_tail(&as->list, &chip->pcm_list);
558 snd_usb_proc_pcm_format_add(as);
560 return add_chmap(pcm, stream, &as->substream[stream]);
563 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
565 struct audioformat *fp)
567 return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
570 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
572 struct audioformat *fp,
573 struct snd_usb_power_domain *pd)
575 return __snd_usb_add_audio_stream(chip, stream, fp, pd);
578 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
579 struct usb_host_interface *alts,
580 int protocol, int iface_no)
582 /* parsed with a v1 header here. that's ok as we only look at the
583 * header first which is the same for both versions */
584 struct uac_iso_endpoint_descriptor *csep;
585 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
588 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
590 /* Creamware Noah has this descriptor after the 2nd endpoint */
591 if (!csep && altsd->bNumEndpoints >= 2)
592 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
595 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
596 * bytes after the first endpoint, go search the entire interface.
597 * Some devices have it directly *before* the standard endpoint.
600 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
602 if (!csep || csep->bLength < 7 ||
603 csep->bDescriptorSubtype != UAC_EP_GENERAL)
606 if (protocol == UAC_VERSION_1) {
607 attributes = csep->bmAttributes;
608 } else if (protocol == UAC_VERSION_2) {
609 struct uac2_iso_endpoint_descriptor *csep2 =
610 (struct uac2_iso_endpoint_descriptor *) csep;
612 if (csep2->bLength < sizeof(*csep2))
614 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
616 /* emulate the endpoint attributes of a v1 device */
617 if (csep2->bmControls & UAC2_CONTROL_PITCH)
618 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
619 } else { /* UAC_VERSION_3 */
620 struct uac3_iso_endpoint_descriptor *csep3 =
621 (struct uac3_iso_endpoint_descriptor *) csep;
623 if (csep3->bLength < sizeof(*csep3))
625 /* emulate the endpoint attributes of a v1 device */
626 if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
627 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
634 "%u:%d : no or invalid class specific endpoint descriptor\n",
635 iface_no, altsd->bAlternateSetting);
639 /* find an input terminal descriptor (either UAC1 or UAC2) with the given
643 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
644 int terminal_id, int protocol)
646 struct uac2_input_terminal_descriptor *term = NULL;
648 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
649 ctrl_iface->extralen,
650 term, UAC_INPUT_TERMINAL))) {
651 if (!snd_usb_validate_audio_desc(term, protocol))
653 if (term->bTerminalID == terminal_id)
661 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
662 int terminal_id, int protocol)
664 /* OK to use with both UAC2 and UAC3 */
665 struct uac2_output_terminal_descriptor *term = NULL;
667 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
668 ctrl_iface->extralen,
669 term, UAC_OUTPUT_TERMINAL))) {
670 if (!snd_usb_validate_audio_desc(term, protocol))
672 if (term->bTerminalID == terminal_id)
679 static struct audioformat *
680 audio_format_alloc_init(struct snd_usb_audio *chip,
681 struct usb_host_interface *alts,
682 int protocol, int iface_no, int altset_idx,
683 int altno, int num_channels, int clock)
685 struct audioformat *fp;
687 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
691 fp->iface = iface_no;
692 fp->altsetting = altno;
693 fp->altset_idx = altset_idx;
694 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
695 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
696 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
697 fp->protocol = protocol;
698 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
699 fp->channels = num_channels;
700 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH)
701 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
702 * (fp->maxpacksize & 0x7ff);
704 INIT_LIST_HEAD(&fp->list);
709 static struct audioformat *
710 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
711 struct usb_host_interface *alts,
712 int protocol, int iface_no, int altset_idx,
713 int altno, int stream, int bm_quirk)
715 struct usb_device *dev = chip->dev;
716 struct uac_format_type_i_continuous_descriptor *fmt;
717 unsigned int num_channels = 0, chconfig = 0;
718 struct audioformat *fp;
722 /* get audio formats */
723 if (protocol == UAC_VERSION_1) {
724 struct uac1_as_header_descriptor *as =
725 snd_usb_find_csint_desc(alts->extra, alts->extralen,
726 NULL, UAC_AS_GENERAL);
727 struct uac_input_terminal_descriptor *iterm;
731 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
736 if (as->bLength < sizeof(*as)) {
738 "%u:%d : invalid UAC_AS_GENERAL desc\n",
743 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
745 iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
749 num_channels = iterm->bNrChannels;
750 chconfig = le16_to_cpu(iterm->wChannelConfig);
752 } else { /* UAC_VERSION_2 */
753 struct uac2_input_terminal_descriptor *input_term;
754 struct uac2_output_terminal_descriptor *output_term;
755 struct uac2_as_header_descriptor *as =
756 snd_usb_find_csint_desc(alts->extra, alts->extralen,
757 NULL, UAC_AS_GENERAL);
761 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
766 if (as->bLength < sizeof(*as)) {
768 "%u:%d : invalid UAC_AS_GENERAL desc\n",
773 num_channels = as->bNrChannels;
774 format = le32_to_cpu(as->bmFormats);
775 chconfig = le32_to_cpu(as->bmChannelConfig);
778 * lookup the terminal associated to this interface
779 * to extract the clock
781 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
785 clock = input_term->bCSourceID;
786 if (!chconfig && (num_channels == input_term->bNrChannels))
787 chconfig = le32_to_cpu(input_term->bmChannelConfig);
791 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
795 clock = output_term->bCSourceID;
800 "%u:%d : bogus bTerminalLink %d\n",
801 iface_no, altno, as->bTerminalLink);
806 /* get format type */
807 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
808 NULL, UAC_FORMAT_TYPE);
811 "%u:%d : no UAC_FORMAT_TYPE desc\n",
815 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
816 || ((protocol == UAC_VERSION_2) &&
817 (fmt->bLength < 6))) {
819 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
825 * Blue Microphones workaround: The last altsetting is
826 * identical with the previous one, except for a larger
827 * packet size, but is actually a mislabeled two-channel
828 * setting; ignore it.
830 * Part 2: analyze quirk flag and format
832 if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
835 fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
836 altset_idx, altno, num_channels, clock);
838 return ERR_PTR(-ENOMEM);
840 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
843 /* some quirks for attributes here */
844 snd_usb_audioformat_attributes_quirk(chip, fp, stream);
846 /* ok, let's parse further... */
847 if (snd_usb_parse_audio_format(chip, fp, format,
849 kfree(fp->rate_table);
855 if (fp->channels != num_channels)
858 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
863 static struct audioformat *
864 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
865 struct usb_host_interface *alts,
866 struct snd_usb_power_domain **pd_out,
867 int iface_no, int altset_idx,
868 int altno, int stream)
870 struct usb_device *dev = chip->dev;
871 struct uac3_input_terminal_descriptor *input_term;
872 struct uac3_output_terminal_descriptor *output_term;
873 struct uac3_cluster_header_descriptor *cluster;
874 struct uac3_as_header_descriptor *as = NULL;
875 struct uac3_hc_descriptor_header hc_header;
876 struct snd_pcm_chmap_elem *chmap;
877 struct snd_usb_power_domain *pd;
878 unsigned char badd_profile;
879 u64 badd_formats = 0;
880 unsigned int num_channels;
881 struct audioformat *fp;
882 u16 cluster_id, wLength;
886 badd_profile = chip->badd_profile;
888 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
889 unsigned int maxpacksize =
890 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
892 switch (maxpacksize) {
895 "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
898 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
899 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
900 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
903 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
904 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
905 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
908 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
909 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
910 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
913 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
914 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
915 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
920 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
922 return ERR_PTR(-ENOMEM);
924 if (num_channels == 1) {
925 chmap->map[0] = SNDRV_CHMAP_MONO;
927 chmap->map[0] = SNDRV_CHMAP_FL;
928 chmap->map[1] = SNDRV_CHMAP_FR;
931 chmap->channels = num_channels;
932 clock = UAC3_BADD_CS_ID9;
936 as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
937 NULL, UAC_AS_GENERAL);
940 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
945 if (as->bLength < sizeof(*as)) {
947 "%u:%d : invalid UAC_AS_GENERAL desc\n",
952 cluster_id = le16_to_cpu(as->wClusterDescrID);
955 "%u:%d : no cluster descriptor\n",
961 * Get number of channels and channel map through
962 * High Capability Cluster Descriptor
964 * First step: get High Capability header and
965 * read size of Cluster Descriptor
967 err = snd_usb_ctl_msg(chip->dev,
968 usb_rcvctrlpipe(chip->dev, 0),
969 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
970 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
972 snd_usb_ctrl_intf(chip),
973 &hc_header, sizeof(hc_header));
976 else if (err != sizeof(hc_header)) {
978 "%u:%d : can't get High Capability descriptor\n",
980 return ERR_PTR(-EIO);
984 * Second step: allocate needed amount of memory
985 * and request Cluster Descriptor
987 wLength = le16_to_cpu(hc_header.wLength);
988 cluster = kzalloc(wLength, GFP_KERNEL);
990 return ERR_PTR(-ENOMEM);
991 err = snd_usb_ctl_msg(chip->dev,
992 usb_rcvctrlpipe(chip->dev, 0),
993 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
994 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
996 snd_usb_ctrl_intf(chip),
1000 return ERR_PTR(err);
1001 } else if (err != wLength) {
1003 "%u:%d : can't get Cluster Descriptor\n",
1006 return ERR_PTR(-EIO);
1009 num_channels = cluster->bNrChannels;
1010 chmap = convert_chmap_v3(cluster);
1014 * lookup the terminal associated to this interface
1015 * to extract the clock
1017 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
1021 clock = input_term->bCSourceID;
1025 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
1029 clock = output_term->bCSourceID;
1033 dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1034 iface_no, altno, as->bTerminalLink);
1039 fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1040 altset_idx, altno, num_channels, clock);
1043 return ERR_PTR(-ENOMEM);
1048 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1049 fp->attributes = 0; /* No attributes */
1051 fp->fmt_type = UAC_FORMAT_TYPE_I;
1052 fp->formats = badd_formats;
1054 fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */
1055 fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1056 fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1057 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1059 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
1062 kfree(fp->rate_table);
1066 pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1067 UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1068 pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1069 pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1072 fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1076 pd = snd_usb_find_power_domain(chip->ctrl_intf,
1079 /* ok, let's parse further... */
1080 if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1083 kfree(fp->rate_table);
1095 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1097 struct usb_device *dev;
1098 struct usb_interface *iface;
1099 struct usb_host_interface *alts;
1100 struct usb_interface_descriptor *altsd;
1101 int i, altno, err, stream;
1102 struct audioformat *fp = NULL;
1103 struct snd_usb_power_domain *pd = NULL;
1108 /* parse the interface's altsettings */
1109 iface = usb_ifnum_to_if(dev, iface_no);
1111 num = iface->num_altsetting;
1114 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1115 * one misses syncpipe, and does not produce any sound.
1117 if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1120 for (i = 0; i < num; i++) {
1121 alts = &iface->altsetting[i];
1122 altsd = get_iface_desc(alts);
1123 protocol = altsd->bInterfaceProtocol;
1124 /* skip invalid one */
1125 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1126 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1127 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1128 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1129 altsd->bNumEndpoints < 1 ||
1130 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1132 /* must be isochronous */
1133 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1134 USB_ENDPOINT_XFER_ISOC)
1136 /* check direction */
1137 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1138 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1139 altno = altsd->bAlternateSetting;
1141 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1145 * Roland audio streaming interfaces are marked with protocols
1146 * 0/1/2, but are UAC 1 compatible.
1148 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1149 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1151 protocol = UAC_VERSION_1;
1155 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1156 iface_no, altno, protocol);
1157 protocol = UAC_VERSION_1;
1161 case UAC_VERSION_2: {
1165 * Blue Microphones workaround: The last altsetting is
1166 * identical with the previous one, except for a larger
1167 * packet size, but is actually a mislabeled two-channel
1168 * setting; ignore it.
1170 * Part 1: prepare quirk flag
1172 if (altno == 2 && num == 3 &&
1173 fp && fp->altsetting == 1 && fp->channels == 1 &&
1174 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1175 protocol == UAC_VERSION_1 &&
1176 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1177 fp->maxpacksize * 2)
1180 fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1186 fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1187 iface_no, i, altno, stream);
1193 else if (IS_ERR(fp))
1196 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
1197 if (protocol == UAC_VERSION_3)
1198 err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
1200 err = snd_usb_add_audio_stream(chip, stream, fp);
1203 list_del(&fp->list); /* unlink for avoiding double-free */
1205 kfree(fp->rate_table);
1210 /* try to set the interface... */
1211 usb_set_interface(chip->dev, iface_no, altno);
1212 snd_usb_init_pitch(chip, iface_no, alts, fp);
1213 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);