11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2e5779998SDaniel Mack /* 3e5779998SDaniel Mack */ 4e5779998SDaniel Mack 5e5779998SDaniel Mack #include <linux/init.h> 636db0456SStephen Rothwell #include <linux/slab.h> 7e5779998SDaniel Mack #include <linux/usb.h> 8e5779998SDaniel Mack #include <linux/usb/audio.h> 97e847894SDaniel Mack #include <linux/usb/audio-v2.h> 109a2fe9b8SRuslan Bilovol #include <linux/usb/audio-v3.h> 11e5779998SDaniel Mack 12e5779998SDaniel Mack #include <sound/core.h> 13e5779998SDaniel Mack #include <sound/pcm.h> 14e5779998SDaniel Mack 15e5779998SDaniel Mack #include "usbaudio.h" 16e5779998SDaniel Mack #include "card.h" 17e5779998SDaniel Mack #include "quirks.h" 18e5779998SDaniel Mack #include "helper.h" 19e5779998SDaniel Mack #include "debug.h" 2079f920fbSDaniel Mack #include "clock.h" 21ee95cb61SDaniel Mack #include "format.h" 22e5779998SDaniel Mack 23e5779998SDaniel Mack /* 24e5779998SDaniel Mack * parse the audio format type I descriptor 25e5779998SDaniel Mack * and returns the corresponding pcm format 26e5779998SDaniel Mack * 27e5779998SDaniel Mack * @dev: usb device 28e5779998SDaniel Mack * @fp: audioformat record 29e5779998SDaniel Mack * @format: the format tag (wFormatTag) 309a2fe9b8SRuslan Bilovol * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3) 31e5779998SDaniel Mack */ 3229088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip, 33e5779998SDaniel Mack struct audioformat *fp, 349a2fe9b8SRuslan Bilovol u64 format, void *_fmt) 35e5779998SDaniel Mack { 36e5779998SDaniel Mack int sample_width, sample_bytes; 37717bfb5fSDaniel Mack u64 pcm_formats = 0; 38e5779998SDaniel Mack 398f898e92SClemens Ladisch switch (fp->protocol) { 40a2acad82SClemens Ladisch case UAC_VERSION_1: 41a2acad82SClemens Ladisch default: { 42e5779998SDaniel Mack struct uac_format_type_i_discrete_descriptor *fmt = _fmt; 43e5779998SDaniel Mack sample_width = fmt->bBitResolution; 44e5779998SDaniel Mack sample_bytes = fmt->bSubframeSize; 45b44d419bSDan Carpenter format = 1ULL << format; 46e5779998SDaniel Mack break; 47e5779998SDaniel Mack } 48e5779998SDaniel Mack 49e5779998SDaniel Mack case UAC_VERSION_2: { 50e5779998SDaniel Mack struct uac_format_type_i_ext_descriptor *fmt = _fmt; 51e5779998SDaniel Mack sample_width = fmt->bBitResolution; 52e5779998SDaniel Mack sample_bytes = fmt->bSubslotSize; 53717bfb5fSDaniel Mack 543a572d94SJussi Laako if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) { 55717bfb5fSDaniel Mack pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 563a572d94SJussi Laako /* flag potentially raw DSD capable altsettings */ 573a572d94SJussi Laako fp->dsd_raw = true; 583a572d94SJussi Laako } 59717bfb5fSDaniel Mack 6029088fefSClemens Ladisch format <<= 1; 61e5779998SDaniel Mack break; 62e5779998SDaniel Mack } 639a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 649a2fe9b8SRuslan Bilovol struct uac3_as_header_descriptor *as = _fmt; 659a2fe9b8SRuslan Bilovol 669a2fe9b8SRuslan Bilovol sample_width = as->bBitResolution; 679a2fe9b8SRuslan Bilovol sample_bytes = as->bSubslotSize; 689a2fe9b8SRuslan Bilovol 699a2fe9b8SRuslan Bilovol if (format & UAC3_FORMAT_TYPE_I_RAW_DATA) 709a2fe9b8SRuslan Bilovol pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 719a2fe9b8SRuslan Bilovol 729a2fe9b8SRuslan Bilovol format <<= 1; 739a2fe9b8SRuslan Bilovol break; 749a2fe9b8SRuslan Bilovol } 75e5779998SDaniel Mack } 76e5779998SDaniel Mack 77c7a13264SJussi Laako fp->fmt_bits = sample_width; 78c7a13264SJussi Laako 79717bfb5fSDaniel Mack if ((pcm_formats == 0) && 80717bfb5fSDaniel Mack (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) { 8129088fefSClemens Ladisch /* some devices don't define this correctly... */ 820ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n", 830ba41d91STakashi Iwai fp->iface, fp->altsetting); 8429088fefSClemens Ladisch format = 1 << UAC_FORMAT_TYPE_I_PCM; 8529088fefSClemens Ladisch } 8629088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) { 876d1f2f60STakamichi Horikawa if (((chip->usb_id == USB_ID(0x0582, 0x0016)) || 886d1f2f60STakamichi Horikawa /* Edirol SD-90 */ 896d1f2f60STakamichi Horikawa (chip->usb_id == USB_ID(0x0582, 0x000c))) && 906d1f2f60STakamichi Horikawa /* Roland SC-D70 */ 91061b869eSClemens Ladisch sample_width == 24 && sample_bytes == 2) 92061b869eSClemens Ladisch sample_bytes = 3; 93061b869eSClemens Ladisch else if (sample_width > sample_bytes * 8) { 940ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n", 950ba41d91STakashi Iwai fp->iface, fp->altsetting, 96e5779998SDaniel Mack sample_width, sample_bytes); 97e5779998SDaniel Mack } 98e5779998SDaniel Mack /* check the format byte size */ 99e5779998SDaniel Mack switch (sample_bytes) { 100e5779998SDaniel Mack case 1: 10129088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 102e5779998SDaniel Mack break; 103e5779998SDaniel Mack case 2: 104e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 10529088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */ 106e5779998SDaniel Mack else 10729088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE; 108e5779998SDaniel Mack break; 109e5779998SDaniel Mack case 3: 110e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 11129088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */ 112e5779998SDaniel Mack else 11329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE; 114e5779998SDaniel Mack break; 115e5779998SDaniel Mack case 4: 11629088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE; 117e5779998SDaniel Mack break; 118e5779998SDaniel Mack default: 1190ba41d91STakashi Iwai usb_audio_info(chip, 1200ba41d91STakashi Iwai "%u:%d : unsupported sample bitwidth %d in %d bytes\n", 1210ba41d91STakashi Iwai fp->iface, fp->altsetting, 122e5779998SDaniel Mack sample_width, sample_bytes); 123e5779998SDaniel Mack break; 124e5779998SDaniel Mack } 12529088fefSClemens Ladisch } 12629088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) { 127e5779998SDaniel Mack /* Dallas DS4201 workaround: it advertises U8 format, but really 128e5779998SDaniel Mack supports S8. */ 129e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 13029088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 13129088fefSClemens Ladisch else 13229088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_U8; 133e5779998SDaniel Mack } 13429088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) { 13529088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 13629088fefSClemens Ladisch } 13729088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) { 13829088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW; 13929088fefSClemens Ladisch } 14029088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) { 14129088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW; 14229088fefSClemens Ladisch } 14329088fefSClemens Ladisch if (format & ~0x3f) { 1440ba41d91STakashi Iwai usb_audio_info(chip, 1459a2fe9b8SRuslan Bilovol "%u:%d : unsupported format bits %#llx\n", 1460ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 14729088fefSClemens Ladisch } 148126825e7SDaniel Mack 149126825e7SDaniel Mack pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes); 150126825e7SDaniel Mack 15129088fefSClemens Ladisch return pcm_formats; 152e5779998SDaniel Mack } 153e5779998SDaniel Mack 15474f73476STakashi Iwai static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits) 15574f73476STakashi Iwai { 15674f73476STakashi Iwai kfree(fp->rate_table); 15774f73476STakashi Iwai fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL); 15874f73476STakashi Iwai if (!fp->rate_table) 15974f73476STakashi Iwai return -ENOMEM; 16074f73476STakashi Iwai fp->nr_rates = 1; 16174f73476STakashi Iwai fp->rate_min = rate; 16274f73476STakashi Iwai fp->rate_max = rate; 16374f73476STakashi Iwai fp->rates = rate_bits; 16474f73476STakashi Iwai fp->rate_table[0] = rate; 16574f73476STakashi Iwai return 0; 16674f73476STakashi Iwai } 167e5779998SDaniel Mack 168e5779998SDaniel Mack /* 169e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 170e5779998SDaniel Mack * on the audioformat table (audio class v1). 171e5779998SDaniel Mack * 172e5779998SDaniel Mack * @dev: usb device 173e5779998SDaniel Mack * @fp: audioformat record 174e5779998SDaniel Mack * @fmt: the format descriptor 175e5779998SDaniel Mack * @offset: the start offset of descriptor pointing the rate type 176e5779998SDaniel Mack * (7 for type I and II, 8 for type II) 177e5779998SDaniel Mack */ 178e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp, 179e5779998SDaniel Mack unsigned char *fmt, int offset) 180e5779998SDaniel Mack { 181e5779998SDaniel Mack int nr_rates = fmt[offset]; 182e5779998SDaniel Mack 183e5779998SDaniel Mack if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { 1840ba41d91STakashi Iwai usb_audio_err(chip, 1850ba41d91STakashi Iwai "%u:%d : invalid UAC_FORMAT_TYPE desc\n", 1860ba41d91STakashi Iwai fp->iface, fp->altsetting); 1878ad10dc6SSachin Kamat return -EINVAL; 188e5779998SDaniel Mack } 189e5779998SDaniel Mack 190e5779998SDaniel Mack if (nr_rates) { 191e5779998SDaniel Mack /* 192e5779998SDaniel Mack * build the rate table and bitmap flags 193e5779998SDaniel Mack */ 194e5779998SDaniel Mack int r, idx; 195e5779998SDaniel Mack 1966da2ec56SKees Cook fp->rate_table = kmalloc_array(nr_rates, sizeof(int), 1976da2ec56SKees Cook GFP_KERNEL); 1981bc00f32SShawn Lin if (fp->rate_table == NULL) 1998ad10dc6SSachin Kamat return -ENOMEM; 200e5779998SDaniel Mack 201e5779998SDaniel Mack fp->nr_rates = 0; 202e5779998SDaniel Mack fp->rate_min = fp->rate_max = 0; 203e5779998SDaniel Mack for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 204e5779998SDaniel Mack unsigned int rate = combine_triple(&fmt[idx]); 205e5779998SDaniel Mack if (!rate) 206e5779998SDaniel Mack continue; 207e5779998SDaniel Mack /* C-Media CM6501 mislabels its 96 kHz altsetting */ 2088129e79eSWolfgang Breyha /* Terratec Aureon 7.1 USB C-Media 6206, too */ 209e5779998SDaniel Mack if (rate == 48000 && nr_rates == 1 && 210e5779998SDaniel Mack (chip->usb_id == USB_ID(0x0d8c, 0x0201) || 2118129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0d8c, 0x0102) || 2128129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0ccd, 0x00b1)) && 213e5779998SDaniel Mack fp->altsetting == 5 && fp->maxpacksize == 392) 214e5779998SDaniel Mack rate = 96000; 2158c4b79cfSPavel Hofman /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */ 2168c4b79cfSPavel Hofman if (rate == 16000 && 2178c4b79cfSPavel Hofman (chip->usb_id == USB_ID(0x041e, 0x4064) || 2188c4b79cfSPavel Hofman chip->usb_id == USB_ID(0x041e, 0x4068))) 219e5779998SDaniel Mack rate = 8000; 220e5779998SDaniel Mack 221e5779998SDaniel Mack fp->rate_table[fp->nr_rates] = rate; 222e5779998SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 223e5779998SDaniel Mack fp->rate_min = rate; 224e5779998SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 225e5779998SDaniel Mack fp->rate_max = rate; 226e5779998SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 227e5779998SDaniel Mack fp->nr_rates++; 228e5779998SDaniel Mack } 229e5779998SDaniel Mack if (!fp->nr_rates) { 230e5779998SDaniel Mack hwc_debug("All rates were zero. Skipping format!\n"); 2318ad10dc6SSachin Kamat return -EINVAL; 232e5779998SDaniel Mack } 233e5779998SDaniel Mack } else { 234e5779998SDaniel Mack /* continuous rates */ 235e5779998SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 236e5779998SDaniel Mack fp->rate_min = combine_triple(&fmt[offset + 1]); 237e5779998SDaniel Mack fp->rate_max = combine_triple(&fmt[offset + 4]); 238e5779998SDaniel Mack } 23974f73476STakashi Iwai 24074f73476STakashi Iwai /* Jabra Evolve 65 headset */ 24174f73476STakashi Iwai if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) { 24274f73476STakashi Iwai /* only 48kHz for playback while keeping 16kHz for capture */ 24374f73476STakashi Iwai if (fp->nr_rates != 1) 24474f73476STakashi Iwai return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000); 24574f73476STakashi Iwai } 24674f73476STakashi Iwai 247e5779998SDaniel Mack return 0; 248e5779998SDaniel Mack } 249e5779998SDaniel Mack 2508dc5efe3SNick Kossifidis 2518dc5efe3SNick Kossifidis /* 2528dc5efe3SNick Kossifidis * Presonus Studio 1810c supports a limited set of sampling 2538dc5efe3SNick Kossifidis * rates per altsetting but reports the full set each time. 2548dc5efe3SNick Kossifidis * If we don't filter out the unsupported rates and attempt 2558dc5efe3SNick Kossifidis * to configure the card, it will hang refusing to do any 2568dc5efe3SNick Kossifidis * further audio I/O until a hard reset is performed. 2578dc5efe3SNick Kossifidis * 2588dc5efe3SNick Kossifidis * The list of supported rates per altsetting (set of available 2598dc5efe3SNick Kossifidis * I/O channels) is described in the owner's manual, section 2.2. 2608dc5efe3SNick Kossifidis */ 2618dc5efe3SNick Kossifidis static bool s1810c_valid_sample_rate(struct audioformat *fp, 2628dc5efe3SNick Kossifidis unsigned int rate) 2638dc5efe3SNick Kossifidis { 2648dc5efe3SNick Kossifidis switch (fp->altsetting) { 2658dc5efe3SNick Kossifidis case 1: 2668dc5efe3SNick Kossifidis /* All ADAT ports available */ 2678dc5efe3SNick Kossifidis return rate <= 48000; 2688dc5efe3SNick Kossifidis case 2: 2698dc5efe3SNick Kossifidis /* Half of ADAT ports available */ 2708dc5efe3SNick Kossifidis return (rate == 88200 || rate == 96000); 2718dc5efe3SNick Kossifidis case 3: 2728dc5efe3SNick Kossifidis /* Analog I/O only (no S/PDIF nor ADAT) */ 2738dc5efe3SNick Kossifidis return rate >= 176400; 2748dc5efe3SNick Kossifidis default: 2758dc5efe3SNick Kossifidis return false; 2768dc5efe3SNick Kossifidis } 2778dc5efe3SNick Kossifidis return false; 2788dc5efe3SNick Kossifidis } 2798dc5efe3SNick Kossifidis 280e5779998SDaniel Mack /* 2811c826792SAlexander Tsoy * Many Focusrite devices supports a limited set of sampling rates per 2821c826792SAlexander Tsoy * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type 2831c826792SAlexander Tsoy * descriptor which has a non-standard bLength = 10. 2841c826792SAlexander Tsoy */ 2851c826792SAlexander Tsoy static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip, 2861c826792SAlexander Tsoy struct audioformat *fp, 2871c826792SAlexander Tsoy unsigned int rate) 2881c826792SAlexander Tsoy { 2891c826792SAlexander Tsoy struct usb_interface *iface; 2901c826792SAlexander Tsoy struct usb_host_interface *alts; 2911c826792SAlexander Tsoy unsigned char *fmt; 2921c826792SAlexander Tsoy unsigned int max_rate; 2931c826792SAlexander Tsoy 2941c826792SAlexander Tsoy iface = usb_ifnum_to_if(chip->dev, fp->iface); 2951c826792SAlexander Tsoy if (!iface) 2961c826792SAlexander Tsoy return true; 2971c826792SAlexander Tsoy 2981c826792SAlexander Tsoy alts = &iface->altsetting[fp->altset_idx]; 2991c826792SAlexander Tsoy fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, 3001c826792SAlexander Tsoy NULL, UAC_FORMAT_TYPE); 3011c826792SAlexander Tsoy if (!fmt) 3021c826792SAlexander Tsoy return true; 3031c826792SAlexander Tsoy 3041c826792SAlexander Tsoy if (fmt[0] == 10) { /* bLength */ 3051c826792SAlexander Tsoy max_rate = combine_quad(&fmt[6]); 3061c826792SAlexander Tsoy 3071c826792SAlexander Tsoy /* Validate max rate */ 3081c826792SAlexander Tsoy if (max_rate != 48000 && 3091c826792SAlexander Tsoy max_rate != 96000 && 3101c826792SAlexander Tsoy max_rate != 192000 && 3111c826792SAlexander Tsoy max_rate != 384000) { 3121c826792SAlexander Tsoy 3131c826792SAlexander Tsoy usb_audio_info(chip, 3141c826792SAlexander Tsoy "%u:%d : unexpected max rate: %u\n", 3151c826792SAlexander Tsoy fp->iface, fp->altsetting, max_rate); 3161c826792SAlexander Tsoy 3171c826792SAlexander Tsoy return true; 3181c826792SAlexander Tsoy } 3191c826792SAlexander Tsoy 3201c826792SAlexander Tsoy return rate <= max_rate; 3211c826792SAlexander Tsoy } 3221c826792SAlexander Tsoy 3231c826792SAlexander Tsoy return true; 3241c826792SAlexander Tsoy } 3251c826792SAlexander Tsoy 3261c826792SAlexander Tsoy /* 32767c10366SDaniel Mack * Helper function to walk the array of sample rate triplets reported by 32867c10366SDaniel Mack * the device. The problem is that we need to parse whole array first to 32967c10366SDaniel Mack * get to know how many sample rates we have to expect. 33067c10366SDaniel Mack * Then fp->rate_table can be allocated and filled. 33167c10366SDaniel Mack */ 3320ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip, 3330ba41d91STakashi Iwai struct audioformat *fp, int nr_triplets, 33467c10366SDaniel Mack const unsigned char *data) 33567c10366SDaniel Mack { 33667c10366SDaniel Mack int i, nr_rates = 0; 33767c10366SDaniel Mack 33867c10366SDaniel Mack fp->rates = fp->rate_min = fp->rate_max = 0; 33967c10366SDaniel Mack 34067c10366SDaniel Mack for (i = 0; i < nr_triplets; i++) { 34167c10366SDaniel Mack int min = combine_quad(&data[2 + 12 * i]); 34267c10366SDaniel Mack int max = combine_quad(&data[6 + 12 * i]); 34367c10366SDaniel Mack int res = combine_quad(&data[10 + 12 * i]); 3444fa0e81bSXi Wang unsigned int rate; 34567c10366SDaniel Mack 34667c10366SDaniel Mack if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 34767c10366SDaniel Mack continue; 34867c10366SDaniel Mack 34967c10366SDaniel Mack /* 35067c10366SDaniel Mack * for ranges with res == 1, we announce a continuous sample 35167c10366SDaniel Mack * rate range, and this function should return 0 for no further 35267c10366SDaniel Mack * parsing. 35367c10366SDaniel Mack */ 35467c10366SDaniel Mack if (res == 1) { 35567c10366SDaniel Mack fp->rate_min = min; 35667c10366SDaniel Mack fp->rate_max = max; 35767c10366SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 35867c10366SDaniel Mack return 0; 35967c10366SDaniel Mack } 36067c10366SDaniel Mack 36167c10366SDaniel Mack for (rate = min; rate <= max; rate += res) { 3628dc5efe3SNick Kossifidis 3638dc5efe3SNick Kossifidis /* Filter out invalid rates on Presonus Studio 1810c */ 3648dc5efe3SNick Kossifidis if (chip->usb_id == USB_ID(0x0194f, 0x010c) && 3658dc5efe3SNick Kossifidis !s1810c_valid_sample_rate(fp, rate)) 3668dc5efe3SNick Kossifidis goto skip_rate; 3678dc5efe3SNick Kossifidis 3681c826792SAlexander Tsoy /* Filter out invalid rates on Focusrite devices */ 3691c826792SAlexander Tsoy if (USB_ID_VENDOR(chip->usb_id) == 0x1235 && 3701c826792SAlexander Tsoy !focusrite_valid_sample_rate(chip, fp, rate)) 3711c826792SAlexander Tsoy goto skip_rate; 3721c826792SAlexander Tsoy 37367c10366SDaniel Mack if (fp->rate_table) 37467c10366SDaniel Mack fp->rate_table[nr_rates] = rate; 37567c10366SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 37667c10366SDaniel Mack fp->rate_min = rate; 37767c10366SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 37867c10366SDaniel Mack fp->rate_max = rate; 37967c10366SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 38067c10366SDaniel Mack 38167c10366SDaniel Mack nr_rates++; 3828866f405SXi Wang if (nr_rates >= MAX_NR_RATES) { 3830ba41d91STakashi Iwai usb_audio_err(chip, "invalid uac2 rates\n"); 3844fa0e81bSXi Wang break; 3854fa0e81bSXi Wang } 38667c10366SDaniel Mack 3878dc5efe3SNick Kossifidis skip_rate: 38867c10366SDaniel Mack /* avoid endless loop */ 38967c10366SDaniel Mack if (res == 0) 39067c10366SDaniel Mack break; 39167c10366SDaniel Mack } 39267c10366SDaniel Mack } 39367c10366SDaniel Mack 39467c10366SDaniel Mack return nr_rates; 39567c10366SDaniel Mack } 39667c10366SDaniel Mack 3978abf41dcSChristopher Swenson /* Line6 Helix series and the Rode Rodecaster Pro don't support the 3988abf41dcSChristopher Swenson * UAC2_CS_RANGE usb function call. Return a static table of known 3998abf41dcSChristopher Swenson * clock rates. 400d4bd3053SNicola Lunghi */ 401d4bd3053SNicola Lunghi static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip, 402d4bd3053SNicola Lunghi struct audioformat *fp) 403d4bd3053SNicola Lunghi { 404d4bd3053SNicola Lunghi switch (chip->usb_id) { 4058be03a71STakashi Iwai case USB_ID(0x0e41, 0x4241): /* Line6 Helix */ 4068be03a71STakashi Iwai case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */ 4078be03a71STakashi Iwai case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */ 4088be03a71STakashi Iwai case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */ 409*7da4c510SLukasz Halman case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */ 4109b132f27STakashi Iwai case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */ 4119b132f27STakashi Iwai case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */ 4129b132f27STakashi Iwai case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */ 4138abf41dcSChristopher Swenson case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */ 41474f73476STakashi Iwai return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000); 415d4bd3053SNicola Lunghi } 416d4bd3053SNicola Lunghi 417d4bd3053SNicola Lunghi return -ENODEV; 418d4bd3053SNicola Lunghi } 419d4bd3053SNicola Lunghi 42067c10366SDaniel Mack /* 421e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 4229a2fe9b8SRuslan Bilovol * on the audioformat table (audio class v2 and v3). 423e5779998SDaniel Mack */ 4249a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip, 4253d8d4dcfSDaniel Mack struct audioformat *fp) 426e5779998SDaniel Mack { 427e5779998SDaniel Mack struct usb_device *dev = chip->dev; 428e5779998SDaniel Mack unsigned char tmp[2], *data; 429d4bd3053SNicola Lunghi int nr_triplets, data_size, ret = 0, ret_l6; 4309f35a312SAlexander Tsoy int clock = snd_usb_clock_find_source(chip, fp, false); 431e5779998SDaniel Mack 432d07140baSDaniel Mack if (clock < 0) { 4330ba41d91STakashi Iwai dev_err(&dev->dev, 4340ba41d91STakashi Iwai "%s(): unable to find clock source (clock %d)\n", 435d07140baSDaniel Mack __func__, clock); 436d07140baSDaniel Mack goto err; 437d07140baSDaniel Mack } 438d07140baSDaniel Mack 439e5779998SDaniel Mack /* get the number of sample rates first by only fetching 2 bytes */ 440e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 441e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 44211bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 44311bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 44417d900c4SClemens Ladisch tmp, sizeof(tmp)); 445e5779998SDaniel Mack 446e5779998SDaniel Mack if (ret < 0) { 447d4bd3053SNicola Lunghi /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */ 448d4bd3053SNicola Lunghi ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp); 449d4bd3053SNicola Lunghi if (ret_l6 == -ENODEV) { 450d4bd3053SNicola Lunghi /* no line6 device found continue showing the error */ 4510ba41d91STakashi Iwai dev_err(&dev->dev, 4520ba41d91STakashi Iwai "%s(): unable to retrieve number of sample rates (clock %d)\n", 45379f920fbSDaniel Mack __func__, clock); 454e5779998SDaniel Mack goto err; 455e5779998SDaniel Mack } 456d4bd3053SNicola Lunghi if (ret_l6 == 0) { 457d4bd3053SNicola Lunghi dev_info(&dev->dev, 458d4bd3053SNicola Lunghi "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n", 459d4bd3053SNicola Lunghi __func__, clock); 460d4bd3053SNicola Lunghi return 0; 461d4bd3053SNicola Lunghi } 462d4bd3053SNicola Lunghi ret = ret_l6; 463d4bd3053SNicola Lunghi goto err; 464d4bd3053SNicola Lunghi } 465e5779998SDaniel Mack 46667c10366SDaniel Mack nr_triplets = (tmp[1] << 8) | tmp[0]; 46767c10366SDaniel Mack data_size = 2 + 12 * nr_triplets; 468e5779998SDaniel Mack data = kzalloc(data_size, GFP_KERNEL); 469e5779998SDaniel Mack if (!data) { 470e5779998SDaniel Mack ret = -ENOMEM; 471e5779998SDaniel Mack goto err; 472e5779998SDaniel Mack } 473e5779998SDaniel Mack 474e5779998SDaniel Mack /* now get the full information */ 475e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 476e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 47711bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 47811bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 47917d900c4SClemens Ladisch data, data_size); 480e5779998SDaniel Mack 481e5779998SDaniel Mack if (ret < 0) { 4820ba41d91STakashi Iwai dev_err(&dev->dev, 4830ba41d91STakashi Iwai "%s(): unable to retrieve sample rate range (clock %d)\n", 48479f920fbSDaniel Mack __func__, clock); 485e5779998SDaniel Mack ret = -EINVAL; 486e5779998SDaniel Mack goto err_free; 487e5779998SDaniel Mack } 488e5779998SDaniel Mack 48967c10366SDaniel Mack /* Call the triplet parser, and make sure fp->rate_table is NULL. 49067c10366SDaniel Mack * We just use the return value to know how many sample rates we 49167c10366SDaniel Mack * will have to deal with. */ 49267c10366SDaniel Mack kfree(fp->rate_table); 49367c10366SDaniel Mack fp->rate_table = NULL; 4940ba41d91STakashi Iwai fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 49567c10366SDaniel Mack 49667c10366SDaniel Mack if (fp->nr_rates == 0) { 49767c10366SDaniel Mack /* SNDRV_PCM_RATE_CONTINUOUS */ 49867c10366SDaniel Mack ret = 0; 49967c10366SDaniel Mack goto err_free; 50067c10366SDaniel Mack } 50167c10366SDaniel Mack 5026da2ec56SKees Cook fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL); 503e5779998SDaniel Mack if (!fp->rate_table) { 504e5779998SDaniel Mack ret = -ENOMEM; 505e5779998SDaniel Mack goto err_free; 506e5779998SDaniel Mack } 507e5779998SDaniel Mack 50867c10366SDaniel Mack /* Call the triplet parser again, but this time, fp->rate_table is 50967c10366SDaniel Mack * allocated, so the rates will be stored */ 5100ba41d91STakashi Iwai parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 511e5779998SDaniel Mack 512e5779998SDaniel Mack err_free: 513e5779998SDaniel Mack kfree(data); 514e5779998SDaniel Mack err: 515e5779998SDaniel Mack return ret; 516e5779998SDaniel Mack } 517e5779998SDaniel Mack 518e5779998SDaniel Mack /* 519e5779998SDaniel Mack * parse the format type I and III descriptors 520e5779998SDaniel Mack */ 521e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip, 5229a2fe9b8SRuslan Bilovol struct audioformat *fp, u64 format, 5239a2fe9b8SRuslan Bilovol void *_fmt) 524e5779998SDaniel Mack { 52574c34ca1SEldad Zack snd_pcm_format_t pcm_format; 5269a2fe9b8SRuslan Bilovol unsigned int fmt_type; 52774c34ca1SEldad Zack int ret; 528e5779998SDaniel Mack 5299a2fe9b8SRuslan Bilovol switch (fp->protocol) { 5309a2fe9b8SRuslan Bilovol default: 5319a2fe9b8SRuslan Bilovol case UAC_VERSION_1: 5329a2fe9b8SRuslan Bilovol case UAC_VERSION_2: { 5339a2fe9b8SRuslan Bilovol struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 5349a2fe9b8SRuslan Bilovol 5359a2fe9b8SRuslan Bilovol fmt_type = fmt->bFormatType; 5369a2fe9b8SRuslan Bilovol break; 5379a2fe9b8SRuslan Bilovol } 5389a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 5399a2fe9b8SRuslan Bilovol /* fp->fmt_type is already set in this case */ 5409a2fe9b8SRuslan Bilovol fmt_type = fp->fmt_type; 5419a2fe9b8SRuslan Bilovol break; 5429a2fe9b8SRuslan Bilovol } 5439a2fe9b8SRuslan Bilovol } 5449a2fe9b8SRuslan Bilovol 5459a2fe9b8SRuslan Bilovol if (fmt_type == UAC_FORMAT_TYPE_III) { 546e5779998SDaniel Mack /* FIXME: the format type is really IECxxx 547e5779998SDaniel Mack * but we give normal PCM format to get the existing 548e5779998SDaniel Mack * apps working... 549e5779998SDaniel Mack */ 550e5779998SDaniel Mack switch (chip->usb_id) { 551e5779998SDaniel Mack 552e5779998SDaniel Mack case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 553e5779998SDaniel Mack if (chip->setup == 0x00 && 554e5779998SDaniel Mack fp->altsetting == 6) 555e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_BE; 556e5779998SDaniel Mack else 557e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 558e5779998SDaniel Mack break; 559e5779998SDaniel Mack default: 560e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 561e5779998SDaniel Mack } 56274c34ca1SEldad Zack fp->formats = pcm_format_to_bits(pcm_format); 563e5779998SDaniel Mack } else { 5649a2fe9b8SRuslan Bilovol fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt); 56529088fefSClemens Ladisch if (!fp->formats) 5668ad10dc6SSachin Kamat return -EINVAL; 567e5779998SDaniel Mack } 568e5779998SDaniel Mack 569e5779998SDaniel Mack /* gather possible sample rates */ 570e5779998SDaniel Mack /* audio class v1 reports possible sample rates as part of the 571e5779998SDaniel Mack * proprietary class specific descriptor. 572e5779998SDaniel Mack * audio class v2 uses class specific EP0 range requests for that. 573e5779998SDaniel Mack */ 5748f898e92SClemens Ladisch switch (fp->protocol) { 575a2acad82SClemens Ladisch default: 5769a2fe9b8SRuslan Bilovol case UAC_VERSION_1: { 5779a2fe9b8SRuslan Bilovol struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 5789a2fe9b8SRuslan Bilovol 579e5779998SDaniel Mack fp->channels = fmt->bNrChannels; 58074754f97SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 581e5779998SDaniel Mack break; 5829a2fe9b8SRuslan Bilovol } 583e5779998SDaniel Mack case UAC_VERSION_2: 5849a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 585e5779998SDaniel Mack /* fp->channels is already set in this case */ 5869a2fe9b8SRuslan Bilovol ret = parse_audio_format_rates_v2v3(chip, fp); 587e5779998SDaniel Mack break; 588e5779998SDaniel Mack } 5899a2fe9b8SRuslan Bilovol } 590e5779998SDaniel Mack 591e5779998SDaniel Mack if (fp->channels < 1) { 5920ba41d91STakashi Iwai usb_audio_err(chip, 5930ba41d91STakashi Iwai "%u:%d : invalid channels %d\n", 5940ba41d91STakashi Iwai fp->iface, fp->altsetting, fp->channels); 5958ad10dc6SSachin Kamat return -EINVAL; 596e5779998SDaniel Mack } 597e5779998SDaniel Mack 598e5779998SDaniel Mack return ret; 599e5779998SDaniel Mack } 600e5779998SDaniel Mack 601e5779998SDaniel Mack /* 602e5779998SDaniel Mack * parse the format type II descriptor 603e5779998SDaniel Mack */ 604e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip, 605e5779998SDaniel Mack struct audioformat *fp, 6069a2fe9b8SRuslan Bilovol u64 format, void *_fmt) 607e5779998SDaniel Mack { 608e5779998SDaniel Mack int brate, framesize, ret; 609e5779998SDaniel Mack 610e5779998SDaniel Mack switch (format) { 611e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_AC3: 612e5779998SDaniel Mack /* FIXME: there is no AC3 format defined yet */ 613015eb0b0SClemens Ladisch // fp->formats = SNDRV_PCM_FMTBIT_AC3; 614015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 615e5779998SDaniel Mack break; 616e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_MPEG: 617015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 618e5779998SDaniel Mack break; 619e5779998SDaniel Mack default: 6200ba41d91STakashi Iwai usb_audio_info(chip, 6219a2fe9b8SRuslan Bilovol "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n", 6220ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 623015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 624e5779998SDaniel Mack break; 625e5779998SDaniel Mack } 626e5779998SDaniel Mack 627e5779998SDaniel Mack fp->channels = 1; 628e5779998SDaniel Mack 6298f898e92SClemens Ladisch switch (fp->protocol) { 630a2acad82SClemens Ladisch default: 631e5779998SDaniel Mack case UAC_VERSION_1: { 632e5779998SDaniel Mack struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 633e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 634e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 6350ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 636e5779998SDaniel Mack fp->frame_size = framesize; 637e5779998SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 638e5779998SDaniel Mack break; 639e5779998SDaniel Mack } 640e5779998SDaniel Mack case UAC_VERSION_2: { 641e5779998SDaniel Mack struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 642e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 643e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 6440ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 645e5779998SDaniel Mack fp->frame_size = framesize; 6469a2fe9b8SRuslan Bilovol ret = parse_audio_format_rates_v2v3(chip, fp); 647e5779998SDaniel Mack break; 648e5779998SDaniel Mack } 649e5779998SDaniel Mack } 650e5779998SDaniel Mack 651e5779998SDaniel Mack return ret; 652e5779998SDaniel Mack } 653e5779998SDaniel Mack 6542fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip, 6559a2fe9b8SRuslan Bilovol struct audioformat *fp, u64 format, 6562fcdb06dSDaniel Mack struct uac_format_type_i_continuous_descriptor *fmt, 6578f898e92SClemens Ladisch int stream) 658e5779998SDaniel Mack { 659e5779998SDaniel Mack int err; 660e5779998SDaniel Mack 66174754f97SDaniel Mack switch (fmt->bFormatType) { 662e5779998SDaniel Mack case UAC_FORMAT_TYPE_I: 663e5779998SDaniel Mack case UAC_FORMAT_TYPE_III: 6648f898e92SClemens Ladisch err = parse_audio_format_i(chip, fp, format, fmt); 665e5779998SDaniel Mack break; 666e5779998SDaniel Mack case UAC_FORMAT_TYPE_II: 6678f898e92SClemens Ladisch err = parse_audio_format_ii(chip, fp, format, fmt); 668e5779998SDaniel Mack break; 669e5779998SDaniel Mack default: 6700ba41d91STakashi Iwai usb_audio_info(chip, 6710ba41d91STakashi Iwai "%u:%d : format type %d is not supported yet\n", 6720ba41d91STakashi Iwai fp->iface, fp->altsetting, 67374754f97SDaniel Mack fmt->bFormatType); 6748d091242SDaniel Mack return -ENOTSUPP; 675e5779998SDaniel Mack } 67674754f97SDaniel Mack fp->fmt_type = fmt->bFormatType; 677e5779998SDaniel Mack if (err < 0) 678e5779998SDaniel Mack return err; 679e5779998SDaniel Mack #if 1 680e5779998SDaniel Mack /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 681e5779998SDaniel Mack /* extigy apparently supports sample rates other than 48k 682e5779998SDaniel Mack * but not in ordinary way. so we enable only 48k atm. 683e5779998SDaniel Mack */ 684e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x041e, 0x3000) || 685e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3020) || 686e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3061)) { 68774754f97SDaniel Mack if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 688e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_48000 && 689e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_96000) 6908d091242SDaniel Mack return -ENOTSUPP; 691e5779998SDaniel Mack } 692e5779998SDaniel Mack #endif 693e5779998SDaniel Mack return 0; 694e5779998SDaniel Mack } 695e5779998SDaniel Mack 6969a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip, 6979a2fe9b8SRuslan Bilovol struct audioformat *fp, 6989a2fe9b8SRuslan Bilovol struct uac3_as_header_descriptor *as, 6999a2fe9b8SRuslan Bilovol int stream) 7009a2fe9b8SRuslan Bilovol { 7019a2fe9b8SRuslan Bilovol u64 format = le64_to_cpu(as->bmFormats); 7029a2fe9b8SRuslan Bilovol int err; 7039a2fe9b8SRuslan Bilovol 7049a2fe9b8SRuslan Bilovol /* 7059a2fe9b8SRuslan Bilovol * Type I format bits are D0..D6 7069a2fe9b8SRuslan Bilovol * This test works because type IV is not supported 7079a2fe9b8SRuslan Bilovol */ 7089a2fe9b8SRuslan Bilovol if (format & 0x7f) 7099a2fe9b8SRuslan Bilovol fp->fmt_type = UAC_FORMAT_TYPE_I; 7109a2fe9b8SRuslan Bilovol else 7119a2fe9b8SRuslan Bilovol fp->fmt_type = UAC_FORMAT_TYPE_III; 7129a2fe9b8SRuslan Bilovol 7139a2fe9b8SRuslan Bilovol err = parse_audio_format_i(chip, fp, format, as); 7149a2fe9b8SRuslan Bilovol if (err < 0) 7159a2fe9b8SRuslan Bilovol return err; 7169a2fe9b8SRuslan Bilovol 7179a2fe9b8SRuslan Bilovol return 0; 7189a2fe9b8SRuslan Bilovol } 719