Debian Codegraph @

sound/usb/stream.c

raw
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 */
4
5
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/usb.h>
9 #include <linux/usb/audio.h>
10 #include <linux/usb/audio-v2.h>
11 #include <linux/usb/audio-v3.h>
12
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/control.h>
16 #include <sound/tlv.h>
17
18 #include "usbaudio.h"
19 #include "card.h"
20 #include "proc.h"
21 #include "quirks.h"
22 #include "endpoint.h"
23 #include "pcm.h"
24 #include "helper.h"
25 #include "format.h"
26 #include "clock.h"
27 #include "stream.h"
28 #include "power.h"
29 #include "media.h"
30
31 static void audioformat_free(struct audioformat *fp)
32 {
33 list_del(&fp->list); /* unlink for avoiding double-free */
34 kfree(fp->rate_table);
35 kfree(fp->chmap);
36 kfree(fp);
37 }
38
39 /*
40 * free a substream
41 */
42 static void free_substream(struct snd_usb_substream *subs)
43 {
44 struct audioformat *fp, *n;
45
46 if (!subs->num_formats)
47 return; /* not initialized */
48 list_for_each_entry_safe(fp, n, &subs->fmt_list, list)
49 audioformat_free(fp);
50 kfree(subs->str_pd);
51 snd_media_stream_delete(subs);
52 }
53
54
55 /*
56 * free a usb stream instance
57 */
58 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
59 {
60 free_substream(&stream->substream[0]);
61 free_substream(&stream->substream[1]);
62 list_del(&stream->list);
63 kfree(stream);
64 }
65
66 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
67 {
68 struct snd_usb_stream *stream = pcm->private_data;
69 if (stream) {
70 stream->pcm = NULL;
71 snd_usb_audio_stream_free(stream);
72 }
73 }
74
75 /*
76 * initialize the substream instance.
77 */
78
79 static void snd_usb_init_substream(struct snd_usb_stream *as,
80 int stream,
81 struct audioformat *fp,
82 struct snd_usb_power_domain **pdptr)
83 {
84 struct snd_usb_substream *subs = &as->substream[stream];
85
86 INIT_LIST_HEAD(&subs->fmt_list);
87 spin_lock_init(&subs->lock);
88
89 subs->stream = as;
90 subs->direction = stream;
91 subs->dev = as->chip->dev;
92 subs->txfr_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_ALIGN_TRANSFER);
93 subs->tx_length_quirk = !!(as->chip->quirk_flags & QUIRK_FLAG_TX_LENGTH);
94 subs->speed = snd_usb_get_speed(subs->dev);
95 subs->pkt_offset_adj = 0;
96 subs->stream_offset_adj = 0;
97
98 snd_usb_set_pcm_ops(as->pcm, stream);
99
100 list_add_tail(&fp->list, &subs->fmt_list);
101 subs->formats |= fp->formats;
102 subs->num_formats++;
103 subs->fmt_type = fp->fmt_type;
104 subs->ep_num = fp->endpoint;
105 if (fp->channels > subs->channels_max)
106 subs->channels_max = fp->channels;
107
108 if (pdptr && *pdptr) {
109 subs->str_pd = *pdptr;
110 *pdptr = NULL; /* assigned */
111 /* Initialize Power Domain to idle status D1 */
112 snd_usb_power_domain_set(subs->stream->chip, subs->str_pd,
113 UAC3_PD_STATE_D1);
114 }
115
116 snd_usb_preallocate_buffer(subs);
117 }
118
119 /* kctl callbacks for usb-audio channel maps */
120 static int usb_chmap_ctl_info(struct snd_kcontrol *kcontrol,
121 struct snd_ctl_elem_info *uinfo)
122 {
123 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
124 struct snd_usb_substream *subs = info->private_data;
125
126 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
127 uinfo->count = subs->channels_max;
128 uinfo->value.integer.min = 0;
129 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
130 return 0;
131 }
132
133 /* check whether a duplicated entry exists in the audiofmt list */
134 static bool have_dup_chmap(struct snd_usb_substream *subs,
135 struct audioformat *fp)
136 {
137 struct audioformat *prev = fp;
138
139 list_for_each_entry_continue_reverse(prev, &subs->fmt_list, list) {
140 if (prev->chmap &&
141 !memcmp(prev->chmap, fp->chmap, sizeof(*fp->chmap)))
142 return true;
143 }
144 return false;
145 }
146
147 static int usb_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
148 unsigned int size, unsigned int __user *tlv)
149 {
150 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
151 struct snd_usb_substream *subs = info->private_data;
152 struct audioformat *fp;
153 unsigned int __user *dst;
154 int count = 0;
155
156 if (size < 8)
157 return -ENOMEM;
158 if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
159 return -EFAULT;
160 size -= 8;
161 dst = tlv + 2;
162 list_for_each_entry(fp, &subs->fmt_list, list) {
163 int i, ch_bytes;
164
165 if (!fp->chmap)
166 continue;
167 if (have_dup_chmap(subs, fp))
168 continue;
169 /* copy the entry */
170 ch_bytes = fp->chmap->channels * 4;
171 if (size < 8 + ch_bytes)
172 return -ENOMEM;
173 if (put_user(SNDRV_CTL_TLVT_CHMAP_FIXED, dst) ||
174 put_user(ch_bytes, dst + 1))
175 return -EFAULT;
176 dst += 2;
177 for (i = 0; i < fp->chmap->channels; i++, dst++) {
178 if (put_user(fp->chmap->map[i], dst))
179 return -EFAULT;
180 }
181
182 count += 8 + ch_bytes;
183 size -= 8 + ch_bytes;
184 }
185 if (put_user(count, tlv + 1))
186 return -EFAULT;
187 return 0;
188 }
189
190 static int usb_chmap_ctl_get(struct snd_kcontrol *kcontrol,
191 struct snd_ctl_elem_value *ucontrol)
192 {
193 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
194 struct snd_usb_substream *subs = info->private_data;
195 struct snd_pcm_chmap_elem *chmap = NULL;
196 int i = 0;
197
198 if (subs->cur_audiofmt)
199 chmap = subs->cur_audiofmt->chmap;
200 if (chmap) {
201 for (i = 0; i < chmap->channels; i++)
202 ucontrol->value.integer.value[i] = chmap->map[i];
203 }
204 for (; i < subs->channels_max; i++)
205 ucontrol->value.integer.value[i] = 0;
206 return 0;
207 }
208
209 /* create a chmap kctl assigned to the given USB substream */
210 static int add_chmap(struct snd_pcm *pcm, int stream,
211 struct snd_usb_substream *subs)
212 {
213 struct audioformat *fp;
214 struct snd_pcm_chmap *chmap;
215 struct snd_kcontrol *kctl;
216 int err;
217
218 list_for_each_entry(fp, &subs->fmt_list, list)
219 if (fp->chmap)
220 goto ok;
221 /* no chmap is found */
222 return 0;
223
224 ok:
225 err = snd_pcm_add_chmap_ctls(pcm, stream, NULL, 0, 0, &chmap);
226 if (err < 0)
227 return err;
228
229 /* override handlers */
230 chmap->private_data = subs;
231 kctl = chmap->kctl;
232 kctl->info = usb_chmap_ctl_info;
233 kctl->get = usb_chmap_ctl_get;
234 kctl->tlv.c = usb_chmap_ctl_tlv;
235
236 return 0;
237 }
238
239 /* convert from USB ChannelConfig bits to ALSA chmap element */
240 static struct snd_pcm_chmap_elem *convert_chmap(int channels, unsigned int bits,
241 int protocol)
242 {
243 static const unsigned int uac1_maps[] = {
244 SNDRV_CHMAP_FL, /* left front */
245 SNDRV_CHMAP_FR, /* right front */
246 SNDRV_CHMAP_FC, /* center front */
247 SNDRV_CHMAP_LFE, /* LFE */
248 SNDRV_CHMAP_RL, /* left surround */
249 SNDRV_CHMAP_RR, /* right surround */
250 SNDRV_CHMAP_FLC, /* left of center */
251 SNDRV_CHMAP_FRC, /* right of center */
252 SNDRV_CHMAP_RC, /* surround */
253 SNDRV_CHMAP_SL, /* side left */
254 SNDRV_CHMAP_SR, /* side right */
255 SNDRV_CHMAP_TC, /* top */
256 0 /* terminator */
257 };
258 static const unsigned int uac2_maps[] = {
259 SNDRV_CHMAP_FL, /* front left */
260 SNDRV_CHMAP_FR, /* front right */
261 SNDRV_CHMAP_FC, /* front center */
262 SNDRV_CHMAP_LFE, /* LFE */
263 SNDRV_CHMAP_RL, /* back left */
264 SNDRV_CHMAP_RR, /* back right */
265 SNDRV_CHMAP_FLC, /* front left of center */
266 SNDRV_CHMAP_FRC, /* front right of center */
267 SNDRV_CHMAP_RC, /* back center */
268 SNDRV_CHMAP_SL, /* side left */
269 SNDRV_CHMAP_SR, /* side right */
270 SNDRV_CHMAP_TC, /* top center */
271 SNDRV_CHMAP_TFL, /* top front left */
272 SNDRV_CHMAP_TFC, /* top front center */
273 SNDRV_CHMAP_TFR, /* top front right */
274 SNDRV_CHMAP_TRL, /* top back left */
275 SNDRV_CHMAP_TRC, /* top back center */
276 SNDRV_CHMAP_TRR, /* top back right */
277 SNDRV_CHMAP_TFLC, /* top front left of center */
278 SNDRV_CHMAP_TFRC, /* top front right of center */
279 SNDRV_CHMAP_LLFE, /* left LFE */
280 SNDRV_CHMAP_RLFE, /* right LFE */
281 SNDRV_CHMAP_TSL, /* top side left */
282 SNDRV_CHMAP_TSR, /* top side right */
283 SNDRV_CHMAP_BC, /* bottom center */
284 SNDRV_CHMAP_RLC, /* back left of center */
285 SNDRV_CHMAP_RRC, /* back right of center */
286 0 /* terminator */
287 };
288 struct snd_pcm_chmap_elem *chmap;
289 const unsigned int *maps;
290 int c;
291
292 if (channels > ARRAY_SIZE(chmap->map))
293 return NULL;
294
295 chmap = kzalloc_obj(*chmap);
296 if (!chmap)
297 return NULL;
298
299 maps = protocol == UAC_VERSION_2 ? uac2_maps : uac1_maps;
300 chmap->channels = channels;
301 c = 0;
302
303 if (bits) {
304 for (; bits && *maps; maps++, bits >>= 1) {
305 if (bits & 1)
306 chmap->map[c++] = *maps;
307 if (c == chmap->channels)
308 break;
309 }
310 } else {
311 /* If we're missing wChannelConfig, then guess something
312 to make sure the channel map is not skipped entirely */
313 if (channels == 1)
314 chmap->map[c++] = SNDRV_CHMAP_MONO;
315 else
316 for (; c < channels && *maps; maps++)
317 chmap->map[c++] = *maps;
318 }
319
320 for (; c < channels; c++)
321 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
322
323 return chmap;
324 }
325
326 /* UAC3 device stores channels information in Cluster Descriptors */
327 static struct
328 snd_pcm_chmap_elem *convert_chmap_v3(struct uac3_cluster_header_descriptor
329 *cluster)
330 {
331 unsigned int channels = cluster->bNrChannels;
332 struct snd_pcm_chmap_elem *chmap;
333 void *p = cluster;
334 int len, c;
335
336 if (channels > ARRAY_SIZE(chmap->map))
337 return NULL;
338
339 chmap = kzalloc_obj(*chmap);
340 if (!chmap)
341 return NULL;
342
343 len = le16_to_cpu(cluster->wLength);
344 c = 0;
345 p += sizeof(*cluster);
346 len -= sizeof(*cluster);
347
348 while (len > 0 && (c < channels)) {
349 struct uac3_cluster_segment_descriptor *cs_desc = p;
350 u16 cs_len;
351 u8 cs_type;
352
353 if (len < sizeof(*cs_desc))
354 break;
355 cs_len = le16_to_cpu(cs_desc->wLength);
356 if (cs_len < sizeof(*cs_desc))
357 break;
358 if (len < cs_len)
359 break;
360 cs_type = cs_desc->bSegmentType;
361
362 if (cs_type == UAC3_CHANNEL_INFORMATION) {
363 struct uac3_cluster_information_segment_descriptor *is = p;
364 unsigned char map;
365
366 if (cs_len < sizeof(*is))
367 break;
368
369 /*
370 * TODO: this conversion is not complete, update it
371 * after adding UAC3 values to asound.h
372 */
373 switch (is->bChRelationship) {
374 case UAC3_CH_MONO:
375 map = SNDRV_CHMAP_MONO;
376 break;
377 case UAC3_CH_LEFT:
378 case UAC3_CH_FRONT_LEFT:
379 case UAC3_CH_HEADPHONE_LEFT:
380 map = SNDRV_CHMAP_FL;
381 break;
382 case UAC3_CH_RIGHT:
383 case UAC3_CH_FRONT_RIGHT:
384 case UAC3_CH_HEADPHONE_RIGHT:
385 map = SNDRV_CHMAP_FR;
386 break;
387 case UAC3_CH_FRONT_CENTER:
388 map = SNDRV_CHMAP_FC;
389 break;
390 case UAC3_CH_FRONT_LEFT_OF_CENTER:
391 map = SNDRV_CHMAP_FLC;
392 break;
393 case UAC3_CH_FRONT_RIGHT_OF_CENTER:
394 map = SNDRV_CHMAP_FRC;
395 break;
396 case UAC3_CH_SIDE_LEFT:
397 map = SNDRV_CHMAP_SL;
398 break;
399 case UAC3_CH_SIDE_RIGHT:
400 map = SNDRV_CHMAP_SR;
401 break;
402 case UAC3_CH_BACK_LEFT:
403 map = SNDRV_CHMAP_RL;
404 break;
405 case UAC3_CH_BACK_RIGHT:
406 map = SNDRV_CHMAP_RR;
407 break;
408 case UAC3_CH_BACK_CENTER:
409 map = SNDRV_CHMAP_RC;
410 break;
411 case UAC3_CH_BACK_LEFT_OF_CENTER:
412 map = SNDRV_CHMAP_RLC;
413 break;
414 case UAC3_CH_BACK_RIGHT_OF_CENTER:
415 map = SNDRV_CHMAP_RRC;
416 break;
417 case UAC3_CH_TOP_CENTER:
418 map = SNDRV_CHMAP_TC;
419 break;
420 case UAC3_CH_TOP_FRONT_LEFT:
421 map = SNDRV_CHMAP_TFL;
422 break;
423 case UAC3_CH_TOP_FRONT_RIGHT:
424 map = SNDRV_CHMAP_TFR;
425 break;
426 case UAC3_CH_TOP_FRONT_CENTER:
427 map = SNDRV_CHMAP_TFC;
428 break;
429 case UAC3_CH_TOP_FRONT_LOC:
430 map = SNDRV_CHMAP_TFLC;
431 break;
432 case UAC3_CH_TOP_FRONT_ROC:
433 map = SNDRV_CHMAP_TFRC;
434 break;
435 case UAC3_CH_TOP_SIDE_LEFT:
436 map = SNDRV_CHMAP_TSL;
437 break;
438 case UAC3_CH_TOP_SIDE_RIGHT:
439 map = SNDRV_CHMAP_TSR;
440 break;
441 case UAC3_CH_TOP_BACK_LEFT:
442 map = SNDRV_CHMAP_TRL;
443 break;
444 case UAC3_CH_TOP_BACK_RIGHT:
445 map = SNDRV_CHMAP_TRR;
446 break;
447 case UAC3_CH_TOP_BACK_CENTER:
448 map = SNDRV_CHMAP_TRC;
449 break;
450 case UAC3_CH_BOTTOM_CENTER:
451 map = SNDRV_CHMAP_BC;
452 break;
453 case UAC3_CH_LOW_FREQUENCY_EFFECTS:
454 map = SNDRV_CHMAP_LFE;
455 break;
456 case UAC3_CH_LFE_LEFT:
457 map = SNDRV_CHMAP_LLFE;
458 break;
459 case UAC3_CH_LFE_RIGHT:
460 map = SNDRV_CHMAP_RLFE;
461 break;
462 case UAC3_CH_RELATIONSHIP_UNDEFINED:
463 default:
464 map = SNDRV_CHMAP_UNKNOWN;
465 break;
466 }
467 chmap->map[c++] = map;
468 }
469 p += cs_len;
470 len -= cs_len;
471 }
472
473 if (channels < c)
474 pr_err("%s: channel number mismatch\n", __func__);
475
476 chmap->channels = channels;
477
478 for (; c < channels; c++)
479 chmap->map[c] = SNDRV_CHMAP_UNKNOWN;
480
481 return chmap;
482 }
483
484 /*
485 * add this endpoint to the chip instance.
486 * if a stream with the same endpoint already exists, append to it.
487 * if not, create a new pcm stream. note, fp is added to the substream
488 * fmt_list and will be freed on the chip instance release. do not free
489 * fp or do remove it from the substream fmt_list to avoid double-free.
490 *
491 * pdptr is optional and can be NULL. When it's non-NULL and the PD gets
492 * assigned to the stream, *pdptr is cleared to NULL upon return.
493 */
494 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
495 int stream,
496 struct audioformat *fp,
497 struct snd_usb_power_domain **pdptr)
498
499 {
500 struct snd_usb_stream *as;
501 struct snd_usb_substream *subs;
502 struct snd_pcm *pcm;
503 int err;
504
505 list_for_each_entry(as, &chip->pcm_list, list) {
506 if (as->fmt_type != fp->fmt_type)
507 continue;
508 subs = &as->substream[stream];
509 if (subs->ep_num == fp->endpoint) {
510 list_add_tail(&fp->list, &subs->fmt_list);
511 subs->num_formats++;
512 subs->formats |= fp->formats;
513 return 0;
514 }
515 }
516
517 if (chip->card->registered)
518 chip->need_delayed_register = true;
519
520 /* look for an empty stream */
521 list_for_each_entry(as, &chip->pcm_list, list) {
522 if (as->fmt_type != fp->fmt_type)
523 continue;
524 subs = &as->substream[stream];
525 if (subs->ep_num)
526 continue;
527 err = snd_pcm_new_stream(as->pcm, stream, 1);
528 if (err < 0)
529 return err;
530 snd_usb_init_substream(as, stream, fp, pdptr);
531 return add_chmap(as->pcm, stream, subs);
532 }
533
534 /* create a new pcm */
535 as = kzalloc_obj(*as);
536 if (!as)
537 return -ENOMEM;
538 as->pcm_index = chip->pcm_devs;
539 as->chip = chip;
540 as->fmt_type = fp->fmt_type;
541 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
542 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
543 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
544 &pcm);
545 if (err < 0) {
546 kfree(as);
547 return err;
548 }
549 as->pcm = pcm;
550 pcm->private_data = as;
551 pcm->private_free = snd_usb_audio_pcm_free;
552 pcm->info_flags = 0;
553 if (chip->pcm_devs > 0)
554 scnprintf(pcm->name, sizeof(pcm->name), "USB Audio #%d",
555 chip->pcm_devs);
556 else
557 strscpy(pcm->name, "USB Audio");
558
559 snd_usb_init_substream(as, stream, fp, pdptr);
560
561 /*
562 * Keep using head insertion for M-Audio Audiophile USB (tm) which has a
563 * fix to swap capture stream order in conf/cards/USB-audio.conf
564 */
565 if (chip->usb_id == USB_ID(0x0763, 0x2003))
566 list_add(&as->list, &chip->pcm_list);
567 else
568 list_add_tail(&as->list, &chip->pcm_list);
569
570 chip->pcm_devs++;
571
572 snd_usb_proc_pcm_format_add(as);
573
574 return add_chmap(pcm, stream, &as->substream[stream]);
575 }
576
577 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
578 struct usb_host_interface *alts,
579 int protocol, int iface_no)
580 {
581 /* parsed with a v1 header here. that's ok as we only look at the
582 * header first which is the same for both versions */
583 struct uac_iso_endpoint_descriptor *csep;
584 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
585 int attributes = 0;
586
587 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
588
589 /* Creamware Noah has this descriptor after the 2nd endpoint */
590 if (!csep && altsd->bNumEndpoints >= 2)
591 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
592
593 /*
594 * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
595 * bytes after the first endpoint, go search the entire interface.
596 * Some devices have it directly *before* the standard endpoint.
597 */
598 if (!csep)
599 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
600
601 if (!csep || csep->bLength < 7 ||
602 csep->bDescriptorSubtype != UAC_EP_GENERAL)
603 goto error;
604
605 if (protocol == UAC_VERSION_1) {
606 attributes = csep->bmAttributes;
607 } else if (protocol == UAC_VERSION_2) {
608 struct uac2_iso_endpoint_descriptor *csep2 =
609 (struct uac2_iso_endpoint_descriptor *) csep;
610
611 if (csep2->bLength < sizeof(*csep2))
612 goto error;
613 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
614
615 /* emulate the endpoint attributes of a v1 device */
616 if (csep2->bmControls & UAC2_CONTROL_PITCH)
617 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
618 } else { /* UAC_VERSION_3 */
619 struct uac3_iso_endpoint_descriptor *csep3 =
620 (struct uac3_iso_endpoint_descriptor *) csep;
621
622 if (csep3->bLength < sizeof(*csep3))
623 goto error;
624 /* emulate the endpoint attributes of a v1 device */
625 if (le32_to_cpu(csep3->bmControls) & UAC2_CONTROL_PITCH)
626 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
627 }
628
629 return attributes;
630
631 error:
632 usb_audio_warn(chip,
633 "%u:%d : no or invalid class specific endpoint descriptor\n",
634 iface_no, altsd->bAlternateSetting);
635 return 0;
636 }
637
638 /* find an input terminal descriptor (either UAC1 or UAC2) with the given
639 * terminal id
640 */
641 static void *
642 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
643 int terminal_id, int protocol)
644 {
645 struct uac2_input_terminal_descriptor *term = NULL;
646
647 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
648 ctrl_iface->extralen,
649 term, UAC_INPUT_TERMINAL))) {
650 if (!snd_usb_validate_audio_desc(term, protocol))
651 continue;
652 if (term->bTerminalID == terminal_id)
653 return term;
654 }
655
656 return NULL;
657 }
658
659 static void *
660 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
661 int terminal_id, int protocol)
662 {
663 /* OK to use with both UAC2 and UAC3 */
664 struct uac2_output_terminal_descriptor *term = NULL;
665
666 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
667 ctrl_iface->extralen,
668 term, UAC_OUTPUT_TERMINAL))) {
669 if (!snd_usb_validate_audio_desc(term, protocol))
670 continue;
671 if (term->bTerminalID == terminal_id)
672 return term;
673 }
674
675 return NULL;
676 }
677
678 static struct audioformat *
679 audio_format_alloc_init(struct snd_usb_audio *chip,
680 struct usb_host_interface *alts,
681 int protocol, int iface_no, int altset_idx,
682 int altno, int num_channels, int clock)
683 {
684 struct usb_host_endpoint *ep = &alts->endpoint[0];
685 struct audioformat *fp;
686
687 fp = kzalloc_obj(*fp);
688 if (!fp)
689 return NULL;
690
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 = usb_endpoint_max_periodic_payload(chip->dev, ep);
699 fp->channels = num_channels;
700 fp->clock = clock;
701 INIT_LIST_HEAD(&fp->list);
702
703 return fp;
704 }
705
706 static struct audioformat *
707 snd_usb_get_audioformat_uac12(struct snd_usb_audio *chip,
708 struct usb_host_interface *alts,
709 int protocol, int iface_no, int altset_idx,
710 int altno, int stream, int bm_quirk)
711 {
712 struct usb_device *dev = chip->dev;
713 struct uac_format_type_i_continuous_descriptor *fmt;
714 unsigned int num_channels = 0, chconfig = 0;
715 struct usb_host_interface *ctrl_intf;
716 struct audioformat *fp;
717 int clock = 0;
718 u64 format;
719
720 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
721
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;
728
729 if (!as) {
730 dev_err(&dev->dev,
731 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
732 iface_no, altno);
733 return NULL;
734 }
735
736 if (as->bLength < sizeof(*as)) {
737 dev_err(&dev->dev,
738 "%u:%d : invalid UAC_AS_GENERAL desc\n",
739 iface_no, altno);
740 return NULL;
741 }
742
743 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
744
745 iterm = snd_usb_find_input_terminal_descriptor(ctrl_intf,
746 as->bTerminalLink,
747 protocol);
748 if (iterm) {
749 num_channels = iterm->bNrChannels;
750 chconfig = le16_to_cpu(iterm->wChannelConfig);
751 }
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);
758
759 if (!as) {
760 dev_err(&dev->dev,
761 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
762 iface_no, altno);
763 return NULL;
764 }
765
766 if (as->bLength < sizeof(*as)) {
767 dev_err(&dev->dev,
768 "%u:%d : invalid UAC_AS_GENERAL desc\n",
769 iface_no, altno);
770 return NULL;
771 }
772
773 num_channels = as->bNrChannels;
774 format = le32_to_cpu(as->bmFormats);
775 chconfig = le32_to_cpu(as->bmChannelConfig);
776
777 /*
778 * lookup the terminal associated to this interface
779 * to extract the clock
780 */
781 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
782 as->bTerminalLink,
783 protocol);
784 if (input_term) {
785 clock = input_term->bCSourceID;
786 if (!chconfig && (num_channels == input_term->bNrChannels))
787 chconfig = le32_to_cpu(input_term->bmChannelConfig);
788 goto found_clock;
789 }
790
791 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
792 as->bTerminalLink,
793 protocol);
794 if (output_term) {
795 clock = output_term->bCSourceID;
796 goto found_clock;
797 }
798
799 dev_err(&dev->dev,
800 "%u:%d : bogus bTerminalLink %d\n",
801 iface_no, altno, as->bTerminalLink);
802 return NULL;
803 }
804
805 found_clock:
806 /* get format type */
807 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
808 NULL, UAC_FORMAT_TYPE);
809 if (!fmt) {
810 dev_err(&dev->dev,
811 "%u:%d : no UAC_FORMAT_TYPE desc\n",
812 iface_no, altno);
813 return NULL;
814 }
815 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8))
816 || ((protocol == UAC_VERSION_2) &&
817 (fmt->bLength < 6))) {
818 dev_err(&dev->dev,
819 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
820 iface_no, altno);
821 return NULL;
822 }
823
824 /*
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.
829 *
830 * Part 2: analyze quirk flag and format
831 */
832 if (bm_quirk && fmt->bNrChannels == 1 && fmt->bSubframeSize == 2)
833 return NULL;
834
835 fp = audio_format_alloc_init(chip, alts, protocol, iface_no,
836 altset_idx, altno, num_channels, clock);
837 if (!fp)
838 return ERR_PTR(-ENOMEM);
839
840 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol,
841 iface_no);
842
843 /* some quirks for attributes here */
844 snd_usb_audioformat_attributes_quirk(chip, fp, stream);
845
846 /* ok, let's parse further... */
847 if (snd_usb_parse_audio_format(chip, fp, format,
848 fmt, stream) < 0) {
849 audioformat_free(fp);
850 return NULL;
851 }
852
853 /* Create chmap */
854 if (fp->channels != num_channels)
855 chconfig = 0;
856
857 fp->chmap = convert_chmap(fp->channels, chconfig, protocol);
858
859 return fp;
860 }
861
862 static struct audioformat *
863 snd_usb_get_audioformat_uac3(struct snd_usb_audio *chip,
864 struct usb_host_interface *alts,
865 struct snd_usb_power_domain **pd_out,
866 int iface_no, int altset_idx,
867 int altno, int stream)
868 {
869 struct usb_device *dev = chip->dev;
870 struct uac3_input_terminal_descriptor *input_term;
871 struct uac3_output_terminal_descriptor *output_term;
872 struct uac3_cluster_header_descriptor *cluster;
873 struct uac3_as_header_descriptor *as = NULL;
874 struct uac3_hc_descriptor_header hc_header;
875 struct usb_host_interface *ctrl_intf;
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, cluster_wLength;
883 int clock = 0;
884 int err;
885
886 badd_profile = chip->badd_profile;
887 ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
888
889 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
890 unsigned int maxpacksize =
891 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
892
893 switch (maxpacksize) {
894 default:
895 dev_err(&dev->dev,
896 "%u:%d : incorrect wMaxPacketSize for BADD profile\n",
897 iface_no, altno);
898 return NULL;
899 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_16:
900 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_16:
901 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
902 num_channels = 1;
903 break;
904 case UAC3_BADD_EP_MAXPSIZE_SYNC_MONO_24:
905 case UAC3_BADD_EP_MAXPSIZE_ASYNC_MONO_24:
906 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
907 num_channels = 1;
908 break;
909 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_16:
910 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_16:
911 badd_formats = SNDRV_PCM_FMTBIT_S16_LE;
912 num_channels = 2;
913 break;
914 case UAC3_BADD_EP_MAXPSIZE_SYNC_STEREO_24:
915 case UAC3_BADD_EP_MAXPSIZE_ASYNC_STEREO_24:
916 badd_formats = SNDRV_PCM_FMTBIT_S24_3LE;
917 num_channels = 2;
918 break;
919 }
920
921 chmap = kzalloc_obj(*chmap);
922 if (!chmap)
923 return ERR_PTR(-ENOMEM);
924
925 if (num_channels == 1) {
926 chmap->map[0] = SNDRV_CHMAP_MONO;
927 } else {
928 chmap->map[0] = SNDRV_CHMAP_FL;
929 chmap->map[1] = SNDRV_CHMAP_FR;
930 }
931
932 chmap->channels = num_channels;
933 clock = UAC3_BADD_CS_ID9;
934 goto found_clock;
935 }
936
937 as = snd_usb_find_csint_desc(alts->extra, alts->extralen,
938 NULL, UAC_AS_GENERAL);
939 if (!as) {
940 dev_err(&dev->dev,
941 "%u:%d : UAC_AS_GENERAL descriptor not found\n",
942 iface_no, altno);
943 return NULL;
944 }
945
946 if (as->bLength < sizeof(*as)) {
947 dev_err(&dev->dev,
948 "%u:%d : invalid UAC_AS_GENERAL desc\n",
949 iface_no, altno);
950 return NULL;
951 }
952
953 cluster_id = le16_to_cpu(as->wClusterDescrID);
954 if (!cluster_id) {
955 dev_err(&dev->dev,
956 "%u:%d : no cluster descriptor\n",
957 iface_no, altno);
958 return NULL;
959 }
960
961 /*
962 * Get number of channels and channel map through
963 * High Capability Cluster Descriptor
964 *
965 * First step: get High Capability header and
966 * read size of Cluster Descriptor
967 */
968 err = snd_usb_ctl_msg(chip->dev,
969 usb_rcvctrlpipe(chip->dev, 0),
970 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
971 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
972 cluster_id,
973 snd_usb_ctrl_intf(ctrl_intf),
974 &hc_header, sizeof(hc_header));
975 if (err < 0)
976 return ERR_PTR(err);
977 else if (err != sizeof(hc_header)) {
978 dev_err(&dev->dev,
979 "%u:%d : can't get High Capability descriptor\n",
980 iface_no, altno);
981 return ERR_PTR(-EIO);
982 }
983
984 /*
985 * Second step: allocate needed amount of memory
986 * and request Cluster Descriptor
987 */
988 wLength = le16_to_cpu(hc_header.wLength);
989 if (wLength < sizeof(*cluster))
990 return NULL;
991 cluster = kzalloc(wLength, GFP_KERNEL);
992 if (!cluster)
993 return ERR_PTR(-ENOMEM);
994 err = snd_usb_ctl_msg(chip->dev,
995 usb_rcvctrlpipe(chip->dev, 0),
996 UAC3_CS_REQ_HIGH_CAPABILITY_DESCRIPTOR,
997 USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
998 cluster_id,
999 snd_usb_ctrl_intf(ctrl_intf),
1000 cluster, wLength);
1001 if (err < 0) {
1002 kfree(cluster);
1003 return ERR_PTR(err);
1004 } else if (err != wLength) {
1005 dev_err(&dev->dev,
1006 "%u:%d : can't get Cluster Descriptor\n",
1007 iface_no, altno);
1008 kfree(cluster);
1009 return ERR_PTR(-EIO);
1010 }
1011
1012 cluster_wLength = le16_to_cpu(cluster->wLength);
1013 if (cluster_wLength < sizeof(*cluster) ||
1014 cluster_wLength > wLength) {
1015 dev_err(&dev->dev,
1016 "%u:%d : invalid Cluster Descriptor size\n",
1017 iface_no, altno);
1018 kfree(cluster);
1019 return ERR_PTR(-EIO);
1020 }
1021
1022 num_channels = cluster->bNrChannels;
1023 chmap = convert_chmap_v3(cluster);
1024 kfree(cluster);
1025
1026 /*
1027 * lookup the terminal associated to this interface
1028 * to extract the clock
1029 */
1030 input_term = snd_usb_find_input_terminal_descriptor(ctrl_intf,
1031 as->bTerminalLink,
1032 UAC_VERSION_3);
1033 if (input_term) {
1034 clock = input_term->bCSourceID;
1035 goto found_clock;
1036 }
1037
1038 output_term = snd_usb_find_output_terminal_descriptor(ctrl_intf,
1039 as->bTerminalLink,
1040 UAC_VERSION_3);
1041 if (output_term) {
1042 clock = output_term->bCSourceID;
1043 goto found_clock;
1044 }
1045
1046 dev_err(&dev->dev, "%u:%d : bogus bTerminalLink %d\n",
1047 iface_no, altno, as->bTerminalLink);
1048 kfree(chmap);
1049 return NULL;
1050
1051 found_clock:
1052 fp = audio_format_alloc_init(chip, alts, UAC_VERSION_3, iface_no,
1053 altset_idx, altno, num_channels, clock);
1054 if (!fp) {
1055 kfree(chmap);
1056 return ERR_PTR(-ENOMEM);
1057 }
1058
1059 fp->chmap = chmap;
1060
1061 if (badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
1062 fp->attributes = 0; /* No attributes */
1063
1064 fp->fmt_type = UAC_FORMAT_TYPE_I;
1065 fp->formats = badd_formats;
1066
1067 fp->nr_rates = 0; /* SNDRV_PCM_RATE_CONTINUOUS */
1068 fp->rate_min = UAC3_BADD_SAMPLING_RATE;
1069 fp->rate_max = UAC3_BADD_SAMPLING_RATE;
1070 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
1071
1072 pd = kzalloc_obj(*pd);
1073 if (!pd) {
1074 audioformat_free(fp);
1075 return NULL;
1076 }
1077 pd->pd_id = (stream == SNDRV_PCM_STREAM_PLAYBACK) ?
1078 UAC3_BADD_PD_ID10 : UAC3_BADD_PD_ID11;
1079 pd->pd_d1d0_rec = UAC3_BADD_PD_RECOVER_D1D0;
1080 pd->pd_d2d0_rec = UAC3_BADD_PD_RECOVER_D2D0;
1081 pd->ctrl_iface = ctrl_intf;
1082
1083 } else {
1084 fp->attributes = parse_uac_endpoint_attributes(chip, alts,
1085 UAC_VERSION_3,
1086 iface_no);
1087
1088 pd = snd_usb_find_power_domain(ctrl_intf,
1089 as->bTerminalLink);
1090
1091 /* ok, let's parse further... */
1092 if (snd_usb_parse_audio_format_v3(chip, fp, as, stream) < 0) {
1093 kfree(pd);
1094 audioformat_free(fp);
1095 return NULL;
1096 }
1097 }
1098
1099 *pd_out = pd;
1100
1101 return fp;
1102 }
1103
1104 static int __snd_usb_parse_audio_interface(struct snd_usb_audio *chip,
1105 int iface_no,
1106 bool *has_non_pcm, bool non_pcm)
1107 {
1108 struct usb_device *dev;
1109 struct usb_interface *iface;
1110 struct usb_host_interface *alts;
1111 struct usb_interface_descriptor *altsd;
1112 int i, altno, err, stream;
1113 struct audioformat *fp = NULL;
1114 bool set_iface_first;
1115 int num, protocol;
1116
1117 dev = chip->dev;
1118
1119 /* parse the interface's altsettings */
1120 iface = usb_ifnum_to_if(dev, iface_no);
1121
1122 num = iface->num_altsetting;
1123
1124 /*
1125 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
1126 * one misses syncpipe, and does not produce any sound.
1127 */
1128 if (chip->usb_id == USB_ID(0x04fa, 0x4201) && num >= 4)
1129 num = 4;
1130
1131 for (i = 0; i < num; i++) {
1132 alts = &iface->altsetting[i];
1133 altsd = get_iface_desc(alts);
1134 protocol = altsd->bInterfaceProtocol;
1135 /* skip invalid one */
1136 if (((altsd->bInterfaceClass != USB_CLASS_AUDIO ||
1137 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
1138 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC)) &&
1139 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1140 altsd->bNumEndpoints < 1 ||
1141 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
1142 continue;
1143 /* must be isochronous */
1144 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
1145 USB_ENDPOINT_XFER_ISOC)
1146 continue;
1147 /* check direction */
1148 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
1149 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
1150 altno = altsd->bAlternateSetting;
1151
1152 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
1153 continue;
1154
1155 /* pd may be allocated at snd_usb_get_audioformat_uac3() and
1156 * assigned at snd_usb_add_audio_stream(); otherwise it'll be
1157 * freed automatically by cleanup at each loop.
1158 */
1159 struct snd_usb_power_domain *pd __free(kfree) = NULL;
1160
1161 /*
1162 * Roland audio streaming interfaces are marked with protocols
1163 * 0/1/2, but are UAC 1 compatible.
1164 */
1165 if (USB_ID_VENDOR(chip->usb_id) == 0x0582 &&
1166 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1167 protocol <= 2)
1168 protocol = UAC_VERSION_1;
1169
1170 switch (protocol) {
1171 default:
1172 dev_dbg(&dev->dev, "%u:%d: unknown interface protocol %#02x, assuming v1\n",
1173 iface_no, altno, protocol);
1174 protocol = UAC_VERSION_1;
1175 fallthrough;
1176 case UAC_VERSION_1:
1177 case UAC_VERSION_2: {
1178 int bm_quirk = 0;
1179
1180 /*
1181 * Blue Microphones workaround: The last altsetting is
1182 * identical with the previous one, except for a larger
1183 * packet size, but is actually a mislabeled two-channel
1184 * setting; ignore it.
1185 *
1186 * Part 1: prepare quirk flag
1187 */
1188 if (altno == 2 && num == 3 &&
1189 fp && fp->altsetting == 1 && fp->channels == 1 &&
1190 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
1191 protocol == UAC_VERSION_1 &&
1192 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
1193 fp->maxpacksize * 2)
1194 bm_quirk = 1;
1195
1196 fp = snd_usb_get_audioformat_uac12(chip, alts, protocol,
1197 iface_no, i, altno,
1198 stream, bm_quirk);
1199 break;
1200 }
1201 case UAC_VERSION_3:
1202 fp = snd_usb_get_audioformat_uac3(chip, alts, &pd,
1203 iface_no, i, altno, stream);
1204 break;
1205 }
1206
1207 if (!fp)
1208 continue;
1209 else if (IS_ERR(fp))
1210 return PTR_ERR(fp);
1211
1212 if (fp->fmt_type != UAC_FORMAT_TYPE_I)
1213 *has_non_pcm = true;
1214 if ((fp->fmt_type == UAC_FORMAT_TYPE_I) == non_pcm) {
1215 audioformat_free(fp);
1216 fp = NULL;
1217 continue;
1218 }
1219
1220 snd_usb_audioformat_set_sync_ep(chip, fp);
1221
1222 dev_dbg(&dev->dev, "%u:%d: add audio endpoint %#x\n", iface_no, altno, fp->endpoint);
1223 err = snd_usb_add_audio_stream(chip, stream, fp, &pd);
1224 if (err < 0) {
1225 audioformat_free(fp);
1226 return err;
1227 }
1228
1229 /* add endpoints */
1230 err = snd_usb_add_endpoint(chip, fp->endpoint,
1231 SND_USB_ENDPOINT_TYPE_DATA);
1232 if (err < 0)
1233 return err;
1234
1235 if (fp->sync_ep) {
1236 err = snd_usb_add_endpoint(chip, fp->sync_ep,
1237 fp->implicit_fb ?
1238 SND_USB_ENDPOINT_TYPE_DATA :
1239 SND_USB_ENDPOINT_TYPE_SYNC);
1240 if (err < 0)
1241 return err;
1242 }
1243
1244 set_iface_first = false;
1245 if (protocol == UAC_VERSION_1 ||
1246 (chip->quirk_flags & QUIRK_FLAG_SET_IFACE_FIRST))
1247 set_iface_first = true;
1248
1249 /* try to set the interface... */
1250 if (chip->quirk_flags & QUIRK_FLAG_SKIP_IFACE_SETUP)
1251 continue;
1252
1253 usb_set_interface(chip->dev, iface_no, 0);
1254 if (set_iface_first)
1255 usb_set_interface(chip->dev, iface_no, altno);
1256 snd_usb_init_pitch(chip, fp);
1257 snd_usb_init_sample_rate(chip, fp, fp->rate_max);
1258 if (!set_iface_first)
1259 usb_set_interface(chip->dev, iface_no, altno);
1260 }
1261 return 0;
1262 }
1263
1264 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
1265 {
1266 int err;
1267 bool has_non_pcm = false;
1268
1269 /* parse PCM formats */
1270 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, false);
1271 if (err < 0)
1272 return err;
1273
1274 if (has_non_pcm) {
1275 /* parse non-PCM formats */
1276 err = __snd_usb_parse_audio_interface(chip, iface_no, &has_non_pcm, true);
1277 if (err < 0)
1278 return err;
1279 }
1280
1281 return 0;
1282 }
1283