0001
0002
0003
0004
0005
0006 #include <linux/init.h>
0007 #include <linux/slab.h>
0008 #include <linux/usb.h>
0009 #include <linux/usb/audio.h>
0010 #include <linux/usb/audio-v2.h>
0011 #include <linux/usb/audio-v3.h>
0012
0013 #include <sound/core.h>
0014 #include <sound/pcm.h>
0015 #include <sound/control.h>
0016 #include <sound/tlv.h>
0017
0018 #include "usbaudio.h"
0019 #include "card.h"
0020 #include "proc.h"
0021 #include "quirks.h"
0022 #include "endpoint.h"
0023 #include "pcm.h"
0024 #include "helper.h"
0025 #include "format.h"
0026 #include "clock.h"
0027 #include "stream.h"
0028 #include "power.h"
0029 #include "media.h"
0030
0031 static void audioformat_free(struct audioformat *fp)
0032 {
0033 list_del(&fp->list);
0034 kfree(fp->rate_table);
0035 kfree(fp->chmap);
0036 kfree(fp);
0037 }
0038
0039
0040
0041
0042 static void free_substream(struct snd_usb_substream *subs)
0043 {
0044 struct audioformat *fp, *n;
0045
0046 if (!subs->num_formats)
0047 return;
0048 list_for_each_entry_safe(fp, n, &subs->fmt_list, list)
0049 audioformat_free(fp);
0050 kfree(subs->str_pd);
0051 snd_media_stream_delete(subs);
0052 }
0053
0054
0055
0056
0057
0058 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
0059 {
0060 free_substream(&stream->substream[0]);
0061 free_substream(&stream->substream[1]);
0062 list_del(&stream->list);
0063 kfree(stream);
0064 }
0065
0066 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
0067 {
0068 struct snd_usb_stream *stream = pcm->private_data;
0069 if (stream) {
0070 stream->pcm = NULL;
0071 snd_usb_audio_stream_free(stream);
0072 }
0073 }
0074
0075
0076
0077
0078
0079 static void snd_usb_init_substream(struct snd_usb_stream *as,
0080 int stream,
0081 struct audioformat *fp,
0082 struct snd_usb_power_domain *pd)
0083 {
0084 struct snd_usb_substream *subs = &as->substream[stream];
0085
0086 INIT_LIST_HEAD(&subs->fmt_list);
0087 spin_lock_init(&subs->lock);
0088
0089 subs->stream = as;
0090 subs->direction = stream;
0091 subs->dev = as->chip->dev;
0092 subs->txfr_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_ALIGN_TRANSFER);
0093 subs->tx_length_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_TX_LENGTH);
0094 subs->speed = snd_usb_get_speed(subs->dev);
0095 subs->pkt_offset_adj = 0;
0096 subs->stream_offset_adj = 0;
0097
0098 snd_usb_set_pcm_ops(as->pcm, stream);
0099
0100 list_add_tail(&fp->list, &subs->fmt_list);
0101 subs->formats |= fp->formats;
0102 subs->num_formats++;
0103 subs->fmt_type = fp->fmt_type;
0104 subs->ep_num = fp->endpoint;
0105 if (fp->channels > subs->channels_max)
0106 subs->channels_max = fp->channels;
0107
0108 if (pd) {
0109 subs->str_pd = pd;
0110
0111 snd_usb_power_domain_set(subs->stream->chip, pd,
0112 UAC3_PD_STATE_D1);
0113 }
0114
0115 snd_usb_preallocate_buffer(subs);
0116 }
0117
0118
0119 static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
0120 struct snd_ctl_elem_info *uinfo)
0121 {
0122 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
0123 struct snd_usb_substream *subs = info->private_data;
0124
0125 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
0126 uinfo->count = subs->channels_max;
0127 uinfo->value.integer.min = 0;
0128 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
0129 return 0;
0130 }
0131
0132
0133 static bool have_dup_chmap(struct snd_usb_substream *subs,
0134 struct audioformat *fp)
0135 {
0136 struct audioformat *prev = fp;
0137
0138 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
0139 if (prev->chmap &&
0140 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
0141 return true;
0142 }
0143 return false;
0144 }
0145
0146 static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
0147 unsigned int size, unsigned int __user *tlv)
0148 {
0149 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
0150 struct snd_usb_substream *subs = info->private_data;
0151 struct audioformat *fp;
0152 unsigned int __user *dst;
0153 int count = 0;
0154
0155 if (size < 8)
0156 return -ENOMEM;
0157 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
0158 return -EFAULT;
0159 size -= 8;
0160 dst = tlv + 2;
0161 list_for_each_entry(fp, &subs->fmt_list, list) {
0162 int i, ch_bytes;
0163
0164 if (!fp->chmap)
0165 continue;
0166 if (have_dup_chmap(subs, fp))
0167 continue;
0168
0169 ch_bytes = fp->chmap->channels * 4;
0170 if (size < 8 + ch_bytes)
0171 return -ENOMEM;
0172 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
0173 put_user(ch_bytes, dst + 1))
0174 return -EFAULT;
0175 dst += 2;
0176 for (i = 0; i < fp->chmap->channels; i++, dst++) {
0177 if (put_user(fp->chmap->map[i], dst))
0178 return -EFAULT;
0179 }
0180
0181 count += 8 + ch_bytes;
0182 size -= 8 + ch_bytes;
0183 }
0184 if (put_user(count, tlv + 1))
0185 return -EFAULT;
0186 return 0;
0187 }
0188
0189 static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
0190 struct snd_ctl_elem_value *ucontrol)
0191 {
0192 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
0193 struct snd_usb_substream *subs = info->private_data;
0194 struct snd_pcm_chmap_elem *chmap = NULL;
0195 int i = 0;
0196
0197 if (subs->cur_audiofmt)
0198 chmap = subs->cur_audiofmt->chmap;
0199 if (chmap) {
0200 for (i = 0; i < chmap->channels; i++)
0201 ucontrol->value.integer.value[i] = chmap->map[i];
0202 }
0203 for (; i < subs->channels_max; i++)
0204 ucontrol->value.integer.value[i] = 0;
0205 return 0;
0206 }
0207
0208
0209 static int add_chmap(struct snd_pcm *pcm, int stream,
0210 struct snd_usb_substream *subs)
0211 {
0212 struct audioformat *fp;
0213 struct snd_pcm_chmap *chmap;
0214 struct snd_kcontrol *kctl;
0215 int err;
0216
0217 list_for_each_entry(fp, &subs->fmt_list, list)
0218 if (fp->chmap)
0219 goto ok;
0220
0221 return 0;
0222
0223 ok:
0224 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
0225 if (err < 0)
0226 return err;
0227
0228
0229 chmap->private_data = subs;
0230 kctl = chmap->kctl;
0231 kctl->info = usb_chmap_ctl_info;
0232 kctl->get = usb_chmap_ctl_get;
0233 kctl->tlv.c = usb_chmap_ctl_tlv;
0234
0235 return 0;
0236 }
0237
0238
0239 static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
0240 int protocol)
0241 {
0242 static const unsigned int uac1_maps[] = {
0243 SNDRV_CHMAP_FL,
0244 SNDRV_CHMAP_FR,
0245 SNDRV_CHMAP_FC,
0246 SNDRV_CHMAP_LFE,
0247 SNDRV_CHMAP_SL,
0248 SNDRV_CHMAP_SR,
0249 SNDRV_CHMAP_FLC,
0250 SNDRV_CHMAP_FRC,
0251 SNDRV_CHMAP_RC,
0252 SNDRV_CHMAP_SL,
0253 SNDRV_CHMAP_SR,
0254 SNDRV_CHMAP_TC,
0255 0
0256 };
0257 static const unsigned int uac2_maps[] = {
0258 SNDRV_CHMAP_FL,
0259 SNDRV_CHMAP_FR,
0260 SNDRV_CHMAP_FC,
0261 SNDRV_CHMAP_LFE,
0262 SNDRV_CHMAP_RL,
0263 SNDRV_CHMAP_RR,
0264 SNDRV_CHMAP_FLC,
0265 SNDRV_CHMAP_FRC,
0266 SNDRV_CHMAP_RC,
0267 SNDRV_CHMAP_SL,
0268 SNDRV_CHMAP_SR,
0269 SNDRV_CHMAP_TC,
0270 SNDRV_CHMAP_TFL,
0271 SNDRV_CHMAP_TFC,
0272 SNDRV_CHMAP_TFR,
0273 SNDRV_CHMAP_TRL,
0274 SNDRV_CHMAP_TRC,
0275 SNDRV_CHMAP_TRR,
0276 SNDRV_CHMAP_TFLC,
0277 SNDRV_CHMAP_TFRC,
0278 SNDRV_CHMAP_LLFE,
0279 SNDRV_CHMAP_RLFE,
0280 SNDRV_CHMAP_TSL,
0281 SNDRV_CHMAP_TSR,
0282 SNDRV_CHMAP_BC,
0283 SNDRV_CHMAP_RLC,
0284 SNDRV_CHMAP_RRC,
0285 0
0286 };
0287 struct snd_pcm_chmap_elem *chmap;
0288 const unsigned int *maps;
0289 int c;
0290
0291 if (channels > ARRAY_SIZE(chmap->map))
0292 return NULL;
0293
0294 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
0295 if (!chmap)
0296 return NULL;
0297
0298 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
0299 chmap->channels = channels;
0300 c = 0;
0301
0302 if (bits) {
0303 for (; bits && *maps; maps++, bits >>= 1)
0304 if (bits & 1)
0305 chmap->map[c++] = *maps;
0306 } else {
0307
0308
0309 if (channels == 1)
0310 chmap->map[c++] = SNDRV_CHMAP_MONO;
0311 else
0312 for (; c < channels && *maps; maps++)
0313 chmap->map[c++] = *maps;
0314 }
0315
0316 for (; c < channels; c++)
0317 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
0318
0319 return chmap;
0320 }
0321
0322
0323 static struct
0324 snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
0325 *cluster)
0326 {
0327 unsigned int channels = cluster->bNrChannels;
0328 struct snd_pcm_chmap_elem *chmap;
0329 void *p = cluster;
0330 int len, c;
0331
0332 if (channels > ARRAY_SIZE(chmap->map))
0333 return NULL;
0334
0335 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
0336 if (!chmap)
0337 return NULL;
0338
0339 len = le16_to_cpu(cluster->wLength);
0340 c = 0;
0341 p += sizeof(struct uac3_cluster_header_descriptor);
0342
0343 while (((p - (void *)cluster) < len) && (c < channels)) {
0344 struct uac3_cluster_segment_descriptor *cs_desc = p;
0345 u16 cs_len;
0346 u8 cs_type;
0347
0348 cs_len = le16_to_cpu(cs_desc->wLength);
0349 cs_type = cs_desc->bSegmentType;
0350
0351 if (cs_type == UAC3_CHANNEL_INFORMATION) {
0352 struct uac3_cluster_information_segment_descriptor *is = p;
0353 unsigned char map;
0354
0355
0356
0357
0358
0359 switch (is->bChRelationship) {
0360 case UAC3_CH_MONO:
0361 map = SNDRV_CHMAP_MONO;
0362 break;
0363 case UAC3_CH_LEFT:
0364 case UAC3_CH_FRONT_LEFT:
0365 case UAC3_CH_HEADPHONE_LEFT:
0366 map = SNDRV_CHMAP_FL;
0367 break;
0368 case UAC3_CH_RIGHT:
0369 case UAC3_CH_FRONT_RIGHT:
0370 case UAC3_CH_HEADPHONE_RIGHT:
0371 map = SNDRV_CHMAP_FR;
0372 break;
0373 case UAC3_CH_FRONT_CENTER:
0374 map = SNDRV_CHMAP_FC;
0375 break;
0376 case UAC3_CH_FRONT_LEFT_OF_CENTER:
0377 map = SNDRV_CHMAP_FLC;
0378 break;
0379 case UAC3_CH_FRONT_RIGHT_OF_CENTER:
0380 map = SNDRV_CHMAP_FRC;
0381 break;
0382 case UAC3_CH_SIDE_LEFT:
0383 map = SNDRV_CHMAP_SL;
0384 break;
0385 case UAC3_CH_SIDE_RIGHT:
0386 map = SNDRV_CHMAP_SR;
0387 break;
0388 case UAC3_CH_BACK_LEFT:
0389 map = SNDRV_CHMAP_RL;
0390 break;
0391 case UAC3_CH_BACK_RIGHT:
0392 map = SNDRV_CHMAP_RR;
0393 break;
0394 case UAC3_CH_BACK_CENTER:
0395 map = SNDRV_CHMAP_RC;
0396 break;
0397 case UAC3_CH_BACK_LEFT_OF_CENTER:
0398 map = SNDRV_CHMAP_RLC;
0399 break;
0400 case UAC3_CH_BACK_RIGHT_OF_CENTER:
0401 map = SNDRV_CHMAP_RRC;
0402 break;
0403 case UAC3_CH_TOP_CENTER:
0404 map = SNDRV_CHMAP_TC;
0405 break;
0406 case UAC3_CH_TOP_FRONT_LEFT:
0407 map = SNDRV_CHMAP_TFL;
0408 break;
0409 case UAC3_CH_TOP_FRONT_RIGHT:
0410 map = SNDRV_CHMAP_TFR;
0411 break;
0412 case UAC3_CH_TOP_FRONT_CENTER:
0413 map = SNDRV_CHMAP_TFC;
0414 break;
0415 case UAC3_CH_TOP_FRONT_LOC:
0416 map = SNDRV_CHMAP_TFLC;
0417 break;
0418 case UAC3_CH_TOP_FRONT_ROC:
0419 map = SNDRV_CHMAP_TFRC;
0420 break;
0421 case UAC3_CH_TOP_SIDE_LEFT:
0422 map = SNDRV_CHMAP_TSL;
0423 break;
0424 case UAC3_CH_TOP_SIDE_RIGHT:
0425 map = SNDRV_CHMAP_TSR;
0426 break;
0427 case UAC3_CH_TOP_BACK_LEFT:
0428 map = SNDRV_CHMAP_TRL;
0429 break;
0430 case UAC3_CH_TOP_BACK_RIGHT:
0431 map = SNDRV_CHMAP_TRR;
0432 break;
0433 case UAC3_CH_TOP_BACK_CENTER:
0434 map = SNDRV_CHMAP_TRC;
0435 break;
0436 case UAC3_CH_BOTTOM_CENTER:
0437 map = SNDRV_CHMAP_BC;
0438 break;
0439 case UAC3_CH_LOW_FREQUENCY_EFFECTS:
0440 map = SNDRV_CHMAP_LFE;
0441 break;
0442 case UAC3_CH_LFE_LEFT:
0443 map = SNDRV_CHMAP_LLFE;
0444 break;
0445 case UAC3_CH_LFE_RIGHT:
0446 map = SNDRV_CHMAP_RLFE;
0447 break;
0448 case UAC3_CH_RELATIONSHIP_UNDEFINED:
0449 default:
0450 map = SNDRV_CHMAP_UNKNOWN;
0451 break;
0452 }
0453 chmap->map[c++] = map;
0454 }
0455 p += cs_len;
0456 }
0457
0458 if (channels < c)
0459 pr_err("%s: channel number mismatch\n", __func__);
0460
0461 chmap->channels = channels;
0462
0463 for (; c < channels; c++)
0464 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
0465
0466 return chmap;
0467 }
0468
0469
0470
0471
0472
0473
0474
0475
0476 static int __snd_usb_add_audio_stream(struct snd_usb_audio *chip,
0477 int stream,
0478 struct audioformat *fp,
0479 struct snd_usb_power_domain *pd)
0480
0481 {
0482 struct snd_usb_stream *as;
0483 struct snd_usb_substream *subs;
0484 struct snd_pcm *pcm;
0485 int err;
0486
0487 list_for_each_entry(as, &chip->pcm_list, list) {
0488 if (as->fmt_type != fp->fmt_type)
0489 continue;
0490 subs = &as->substream[stream];
0491 if (subs->ep_num == fp->endpoint) {
0492 list_add_tail(&fp->list, &subs->fmt_list);
0493 subs->num_formats++;
0494 subs->formats |= fp->formats;
0495 return 0;
0496 }
0497 }
0498
0499 if (chip->card->registered)
0500 chip->need_delayed_register = true;
0501
0502
0503 list_for_each_entry(as, &chip->pcm_list, list) {
0504 if (as->fmt_type != fp->fmt_type)
0505 continue;
0506 subs = &as->substream[stream];
0507 if (subs->ep_num)
0508 continue;
0509 err = snd_pcm_new_stream(as->pcm, stream, 1);
0510 if (err < 0)
0511 return err;
0512 snd_usb_init_substream(as, stream, fp, pd);
0513 return add_chmap(as->pcm, stream, subs);
0514 }
0515
0516
0517 as = kzalloc(sizeof(*as), GFP_KERNEL);
0518 if (!as)
0519 return -ENOMEM;
0520 as->pcm_index = chip->pcm_devs;
0521 as->chip = chip;
0522 as->fmt_type = fp->fmt_type;
0523 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
0524 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
0525 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
0526 &pcm);
0527 if (err < 0) {
0528 kfree(as);
0529 return err;
0530 }
0531 as->pcm = pcm;
0532 pcm->private_data = as;
0533 pcm->private_free = snd_usb_audio_pcm_free;
0534 pcm->info_flags = 0;
0535 if (chip->pcm_devs > 0)
0536 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
0537 else
0538 strcpy(pcm->name, "USB Audio");
0539
0540 snd_usb_init_substream(as, stream, fp, pd);
0541
0542
0543
0544
0545
0546 if (chip->usb_id == USB_ID(0x0763, 0x2003))
0547 list_add(&as->list, &chip->pcm_list);
0548 else
0549 list_add_tail(&as->list, &chip->pcm_list);
0550
0551 chip->pcm_devs++;
0552
0553 snd_usb_proc_pcm_format_add(as);
0554
0555 return add_chmap(pcm, stream, &as->substream[stream]);
0556 }
0557
0558 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
0559 int stream,
0560 struct audioformat *fp)
0561 {
0562 return __snd_usb_add_audio_stream(chip, stream, fp, NULL);
0563 }
0564
0565 static int snd_usb_add_audio_stream_v3(struct snd_usb_audio *chip,
0566 int stream,
0567 struct audioformat *fp,
0568 struct snd_usb_power_domain *pd)
0569 {
0570 return __snd_usb_add_audio_stream(chip, stream, fp, pd);
0571 }
0572
0573 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
0574 struct usb_host_interface *alts,
0575 int protocol, int iface_no)
0576 {
0577
0578
0579 struct uac_iso_endpoint_descriptor *csep;
0580 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
0581 int attributes = 0;
0582
0583 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
0584
0585
0586 if (!csep && altsd->bNumEndpoints >= 2)
0587 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
0588
0589
0590
0591
0592
0593
0594 if (!csep)
0595 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
0596
0597 if (!csep || csep->bLength < 7 ||
0598 csep->bDescriptorSubtype != UAC_EP_GENERAL)
0599 goto error;
0600
0601 if (protocol == UAC_VERSION_1) {
0602 attributes = csep->bmAttributes;
0603 } else if (protocol == UAC_VERSION_2) {
0604 struct uac2_iso_endpoint_descriptor *csep2 =
0605 (struct uac2_iso_endpoint_descriptor *) csep;
0606
0607 if (csep2->bLength < sizeof(*csep2))
0608 goto error;
0609 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
0610
0611
0612 if (csep2->bmControls & UAC2_CONTROL_PITCH)
0613 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
0614 } else {
0615 struct uac3_iso_endpoint_descriptor *csep3 =
0616 (struct uac3_iso_endpoint_descriptor *) csep;
0617
0618 if (csep3->bLength < sizeof(*csep3))
0619 goto error;
0620
0621 if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
0622 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
0623 }
0624
0625 return attributes;
0626
0627 error:
0628 usb_audio_warn(chip,
0629 "%u:%d : no or invalid class specific endpoint descriptor\n",
0630 iface_no, altsd->bAlternateSetting);
0631 return 0;
0632 }
0633
0634
0635
0636
0637 static void *
0638 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
0639 int terminal_id, int protocol)
0640 {
0641 struct uac2_input_terminal_descriptor *term = NULL;
0642
0643 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
0644 ctrl_iface->extralen,
0645 term, UAC_INPUT_TERMINAL))) {
0646 if (!snd_usb_validate_audio_desc(term, protocol))
0647 continue;
0648 if (term->bTerminalID == terminal_id)
0649 return term;
0650 }
0651
0652 return NULL;
0653 }
0654
0655 static void *
0656 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
0657 int terminal_id, int protocol)
0658 {
0659
0660 struct uac2_output_terminal_descriptor *term = NULL;
0661
0662 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
0663 ctrl_iface->extralen,
0664 term, UAC_OUTPUT_TERMINAL))) {
0665 if (!snd_usb_validate_audio_desc(term, protocol))
0666 continue;
0667 if (term->bTerminalID == terminal_id)
0668 return term;
0669 }
0670
0671 return NULL;
0672 }
0673
0674 static struct audioformat *
0675 audio_format_alloc_init(struct snd_usb_audio *chip,
0676 struct usb_host_interface *alts,
0677 int protocol, int iface_no, int altset_idx,
0678 int altno, int num_channels, int clock)
0679 {
0680 struct audioformat *fp;
0681
0682 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
0683 if (!fp)
0684 return NULL;
0685
0686 fp->iface = iface_no;
0687 fp->altsetting = altno;
0688 fp->altset_idx = altset_idx;
0689 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
0690 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
0691 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
0692 fp->protocol = protocol;
0693 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
0694 fp->channels = num_channels;
0695 if (snd_usb_get_speed(chip->dev) == USB_SPEED_HIGH)
0696 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
0697 * (fp->maxpacksize & 0x7ff);
0698 fp->clock = clock;
0699 INIT_LIST_HEAD(&fp->list);
0700
0701 return fp;
0702 }
0703
0704 static struct audioformat *
0705 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
0706 struct usb_host_interface *alts,
0707 int protocol, int iface_no, int altset_idx,
0708 int altno, int stream, int bm_quirk)
0709 {
0710 struct usb_device *dev = chip->dev;
0711 struct uac_format_type_i_continuous_descriptor *fmt;
0712 unsigned int num_channels = 0, chconfig = 0;
0713 struct audioformat *fp;
0714 int clock = 0;
0715 u64 format;
0716
0717
0718 if (protocol == UAC_VERSION_1) {
0719 struct uac1_as_header_descriptor *as =
0720 snd_usb_find_csint_desc(alts->extra, alts->extralen,
0721 NULL, UAC_AS_GENERAL);
0722 struct uac_input_terminal_descriptor *iterm;
0723
0724 if (!as) {
0725 dev_err(&dev->dev,
0726 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
0727 iface_no, altno);
0728 return NULL;
0729 }
0730
0731 if (as->bLength < sizeof(*as)) {
0732 dev_err(&dev->dev,
0733 "%u:%d : invalid UAC_AS_GENERAL desc\n",
0734 iface_no, altno);
0735 return NULL;
0736 }
0737
0738 format = le16_to_cpu(as->wFormatTag);
0739
0740 iterm = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
0741 as->bTerminalLink,
0742 protocol);
0743 if (iterm) {
0744 num_channels = iterm->bNrChannels;
0745 chconfig = le16_to_cpu(iterm->wChannelConfig);
0746 }
0747 } else {
0748 struct uac2_input_terminal_descriptor *input_term;
0749 struct uac2_output_terminal_descriptor *output_term;
0750 struct uac2_as_header_descriptor *as =
0751 snd_usb_find_csint_desc(alts->extra, alts->extralen,
0752 NULL, UAC_AS_GENERAL);
0753
0754 if (!as) {
0755 dev_err(&dev->dev,
0756 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
0757 iface_no, altno);
0758 return NULL;
0759 }
0760
0761 if (as->bLength < sizeof(*as)) {
0762 dev_err(&dev->dev,
0763 "%u:%d : invalid UAC_AS_GENERAL desc\n",
0764 iface_no, altno);
0765 return NULL;
0766 }
0767
0768 num_channels = as->bNrChannels;
0769 format = le32_to_cpu(as->bmFormats);
0770 chconfig = le32_to_cpu(as->bmChannelConfig);
0771
0772
0773
0774
0775
0776 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
0777 as->bTerminalLink,
0778 protocol);
0779 if (input_term) {
0780 clock = input_term->bCSourceID;
0781 if (!chconfig && (num_channels == input_term->bNrChannels))
0782 chconfig = le32_to_cpu(input_term->bmChannelConfig);
0783 goto found_clock;
0784 }
0785
0786 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
0787 as->bTerminalLink,
0788 protocol);
0789 if (output_term) {
0790 clock = output_term->bCSourceID;
0791 goto found_clock;
0792 }
0793
0794 dev_err(&dev->dev,
0795 "%u:%d : bogus bTerminalLink %d\n",
0796 iface_no, altno, as->bTerminalLink);
0797 return NULL;
0798 }
0799
0800 found_clock:
0801
0802 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
0803 NULL, UAC_FORMAT_TYPE);
0804 if (!fmt) {
0805 dev_err(&dev->dev,
0806 "%u:%d : no UAC_FORMAT_TYPE desc\n",
0807 iface_no, altno);
0808 return NULL;
0809 }
0810 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
0811 || ((protocol == UAC_VERSION_2) &&
0812 (fmt->bLength < 6))) {
0813 dev_err(&dev->dev,
0814 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
0815 iface_no, altno);
0816 return NULL;
0817 }
0818
0819
0820
0821
0822
0823
0824
0825
0826
0827 if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
0828 return NULL;
0829
0830 fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
0831 altset_idx, altno, num_channels, clock);
0832 if (!fp)
0833 return ERR_PTR(-ENOMEM);
0834
0835 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
0836 iface_no);
0837
0838
0839 snd_usb_audioformat_attributes_quirk(chip, fp, stream);
0840
0841
0842 if (snd_usb_parse_audio_format(chip, fp, format,
0843 fmt, stream) < 0) {
0844 audioformat_free(fp);
0845 return NULL;
0846 }
0847
0848
0849 if (fp->channels != num_channels)
0850 chconfig = 0;
0851
0852 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
0853
0854 return fp;
0855 }
0856
0857 static struct audioformat *
0858 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
0859 struct usb_host_interface *alts,
0860 struct snd_usb_power_domain **pd_out,
0861 int iface_no, int altset_idx,
0862 int altno, int stream)
0863 {
0864 struct usb_device *dev = chip->dev;
0865 struct uac3_input_terminal_descriptor *input_term;
0866 struct uac3_output_terminal_descriptor *output_term;
0867 struct uac3_cluster_header_descriptor *cluster;
0868 struct uac3_as_header_descriptor *as = NULL;
0869 struct uac3_hc_descriptor_header hc_header;
0870 struct snd_pcm_chmap_elem *chmap;
0871 struct snd_usb_power_domain *pd;
0872 unsigned char badd_profile;
0873 u64 badd_formats = 0;
0874 unsigned int num_channels;
0875 struct audioformat *fp;
0876 u16 cluster_id, wLength;
0877 int clock = 0;
0878 int err;
0879
0880 badd_profile = chip->badd_profile;
0881
0882 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
0883 unsigned int maxpacksize =
0884 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
0885
0886 switch (maxpacksize) {
0887 default:
0888 dev_err(&dev->dev,
0889 "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
0890 iface_no, altno);
0891 return NULL;
0892 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
0893 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
0894 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
0895 num_channels = 1;
0896 break;
0897 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
0898 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
0899 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
0900 num_channels = 1;
0901 break;
0902 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
0903 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
0904 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
0905 num_channels = 2;
0906 break;
0907 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
0908 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
0909 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
0910 num_channels = 2;
0911 break;
0912 }
0913
0914 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
0915 if (!chmap)
0916 return ERR_PTR(-ENOMEM);
0917
0918 if (num_channels == 1) {
0919 chmap->map[0] = SNDRV_CHMAP_MONO;
0920 } else {
0921 chmap->map[0] = SNDRV_CHMAP_FL;
0922 chmap->map[1] = SNDRV_CHMAP_FR;
0923 }
0924
0925 chmap->channels = num_channels;
0926 clock = UAC3_BADD_CS_ID9;
0927 goto found_clock;
0928 }
0929
0930 as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
0931 NULL, UAC_AS_GENERAL);
0932 if (!as) {
0933 dev_err(&dev->dev,
0934 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
0935 iface_no, altno);
0936 return NULL;
0937 }
0938
0939 if (as->bLength < sizeof(*as)) {
0940 dev_err(&dev->dev,
0941 "%u:%d : invalid UAC_AS_GENERAL desc\n",
0942 iface_no, altno);
0943 return NULL;
0944 }
0945
0946 cluster_id = le16_to_cpu(as->wClusterDescrID);
0947 if (!cluster_id) {
0948 dev_err(&dev->dev,
0949 "%u:%d : no cluster descriptor\n",
0950 iface_no, altno);
0951 return NULL;
0952 }
0953
0954
0955
0956
0957
0958
0959
0960
0961 err = snd_usb_ctl_msg(chip->dev,
0962 usb_rcvctrlpipe(chip->dev, 0),
0963 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
0964 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
0965 cluster_id,
0966 snd_usb_ctrl_intf(chip),
0967 &hc_header, sizeof(hc_header));
0968 if (err < 0)
0969 return ERR_PTR(err);
0970 else if (err != sizeof(hc_header)) {
0971 dev_err(&dev->dev,
0972 "%u:%d : can't get High Capability descriptor\n",
0973 iface_no, altno);
0974 return ERR_PTR(-EIO);
0975 }
0976
0977
0978
0979
0980
0981 wLength = le16_to_cpu(hc_header.wLength);
0982 cluster = kzalloc(wLength, GFP_KERNEL);
0983 if (!cluster)
0984 return ERR_PTR(-ENOMEM);
0985 err = snd_usb_ctl_msg(chip->dev,
0986 usb_rcvctrlpipe(chip->dev, 0),
0987 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
0988 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
0989 cluster_id,
0990 snd_usb_ctrl_intf(chip),
0991 cluster, wLength);
0992 if (err < 0) {
0993 kfree(cluster);
0994 return ERR_PTR(err);
0995 } else if (err != wLength) {
0996 dev_err(&dev->dev,
0997 "%u:%d : can't get Cluster Descriptor\n",
0998 iface_no, altno);
0999 kfree(cluster);
1000 return ERR_PTR(-EIO);
1001 }
1002
1003 num_channels = cluster->bNrChannels;
1004 chmap = convert_chmap_v3(cluster);
1005 kfree(cluster);
1006
1007
1008
1009
1010
1011 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
1012 as->bTerminalLink,
1013 UAC_VERSION_3);
1014 if (input_term) {
1015 clock = input_term->bCSourceID;
1016 goto found_clock;
1017 }
1018
1019 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
1020 as->bTerminalLink,
1021 UAC_VERSION_3);
1022 if (output_term) {
1023 clock = output_term->bCSourceID;
1024 goto found_clock;
1025 }
1026
1027 dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1028 iface_no, altno, as->bTerminalLink);
1029 kfree(chmap);
1030 return NULL;
1031
1032 found_clock:
1033 fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1034 altset_idx, altno, num_channels, clock);
1035 if (!fp) {
1036 kfree(chmap);
1037 return ERR_PTR(-ENOMEM);
1038 }
1039
1040 fp->chmap = chmap;
1041
1042 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1043 fp->attributes = 0;
1044
1045 fp->fmt_type = UAC_FORMAT_TYPE_I;
1046 fp->formats = badd_formats;
1047
1048 fp->nr_rates = 0;
1049 fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1050 fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1051 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1052
1053 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
1054 if (!pd) {
1055 audioformat_free(fp);
1056 return NULL;
1057 }
1058 pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1059 UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1060 pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1061 pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1062
1063 } else {
1064 fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1065 UAC_VERSION_3,
1066 iface_no);
1067
1068 pd = snd_usb_find_power_domain(chip->ctrl_intf,
1069 as->bTerminalLink);
1070
1071
1072 if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1073 kfree(pd);
1074 audioformat_free(fp);
1075 return NULL;
1076 }
1077 }
1078
1079 if (pd)
1080 *pd_out = pd;
1081
1082 return fp;
1083 }
1084
1085 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
1086 int iface_no,
1087 bool *has_non_pcm, bool non_pcm)
1088 {
1089 struct usb_device *dev;
1090 struct usb_interface *iface;
1091 struct usb_host_interface *alts;
1092 struct usb_interface_descriptor *altsd;
1093 int i, altno, err, stream;
1094 struct audioformat *fp = NULL;
1095 struct snd_usb_power_domain *pd = NULL;
1096 int num, protocol;
1097
1098 dev = chip->dev;
1099
1100
1101 iface = usb_ifnum_to_if(dev, iface_no);
1102
1103 num = iface->num_altsetting;
1104
1105
1106
1107
1108
1109 if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1110 num = 4;
1111
1112 for (i = 0; i < num; i++) {
1113 alts = &iface->altsetting[i];
1114 altsd = get_iface_desc(alts);
1115 protocol = altsd->bInterfaceProtocol;
1116
1117 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1118 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1119 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1120 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1121 altsd->bNumEndpoints < 1 ||
1122 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1123 continue;
1124
1125 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1126 USB_ENDPOINT_XFER_ISOC)
1127 continue;
1128
1129 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1130 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1131 altno = altsd->bAlternateSetting;
1132
1133 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1134 continue;
1135
1136
1137
1138
1139
1140 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1141 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1142 protocol <= 2)
1143 protocol = UAC_VERSION_1;
1144
1145 switch (protocol) {
1146 default:
1147 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1148 iface_no, altno, protocol);
1149 protocol = UAC_VERSION_1;
1150 fallthrough;
1151 case UAC_VERSION_1:
1152 case UAC_VERSION_2: {
1153 int bm_quirk = 0;
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 if (altno == 2 && num == 3 &&
1164 fp && fp->altsetting == 1 && fp->channels == 1 &&
1165 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1166 protocol == UAC_VERSION_1 &&
1167 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1168 fp->maxpacksize * 2)
1169 bm_quirk = 1;
1170
1171 fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1172 iface_no, i, altno,
1173 stream, bm_quirk);
1174 break;
1175 }
1176 case UAC_VERSION_3:
1177 fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1178 iface_no, i, altno, stream);
1179 break;
1180 }
1181
1182 if (!fp)
1183 continue;
1184 else if (IS_ERR(fp))
1185 return PTR_ERR(fp);
1186
1187 if (fp->fmt_type != UAC_FORMAT_TYPE_I)
1188 *has_non_pcm = true;
1189 if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
1190 audioformat_free(fp);
1191 kfree(pd);
1192 fp = NULL;
1193 pd = NULL;
1194 continue;
1195 }
1196
1197 snd_usb_audioformat_set_sync_ep(chip, fp);
1198
1199 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
1200 if (protocol == UAC_VERSION_3)
1201 err = snd_usb_add_audio_stream_v3(chip, stream, fp, pd);
1202 else
1203 err = snd_usb_add_audio_stream(chip, stream, fp);
1204
1205 if (err < 0) {
1206 audioformat_free(fp);
1207 kfree(pd);
1208 return err;
1209 }
1210
1211
1212 err = snd_usb_add_endpoint(chip, fp->endpoint,
1213 SND_USB_ENDPOINT_TYPE_DATA);
1214 if (err < 0)
1215 return err;
1216
1217 if (fp->sync_ep) {
1218 err = snd_usb_add_endpoint(chip, fp->sync_ep,
1219 fp->implicit_fb ?
1220 SND_USB_ENDPOINT_TYPE_DATA :
1221 SND_USB_ENDPOINT_TYPE_SYNC);
1222 if (err < 0)
1223 return err;
1224 }
1225
1226
1227 usb_set_interface(chip->dev, iface_no, 0);
1228 snd_usb_init_pitch(chip, fp);
1229 snd_usb_init_sample_rate(chip, fp, fp->rate_max);
1230 usb_set_interface(chip->dev, iface_no, altno);
1231 }
1232 return 0;
1233 }
1234
1235 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1236 {
1237 int err;
1238 bool has_non_pcm = false;
1239
1240
1241 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
1242 if (err < 0)
1243 return err;
1244
1245 if (has_non_pcm) {
1246
1247 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
1248 if (err < 0)
1249 return err;
1250 }
1251
1252 return 0;
1253 }
1254