1e5779998SDaniel Mack /* 2e5779998SDaniel Mack * This program is free software; you can redistribute it and/or modify 3e5779998SDaniel Mack * it under the terms of the GNU General Public License as published by 4e5779998SDaniel Mack * the Free Software Foundation; either version 2 of the License, or 5e5779998SDaniel Mack * (at your option) any later version. 6e5779998SDaniel Mack * 7e5779998SDaniel Mack * This program is distributed in the hope that it will be useful, 8e5779998SDaniel Mack * but WITHOUT ANY WARRANTY; without even the implied warranty of 9e5779998SDaniel Mack * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10e5779998SDaniel Mack * GNU General Public License for more details. 11e5779998SDaniel Mack * 12e5779998SDaniel Mack * You should have received a copy of the GNU General Public License 13e5779998SDaniel Mack * along with this program; if not, write to the Free Software 14e5779998SDaniel Mack * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 15e5779998SDaniel Mack * 16e5779998SDaniel Mack */ 17e5779998SDaniel Mack 18e5779998SDaniel Mack #include <linux/init.h> 1936db0456SStephen Rothwell #include <linux/slab.h> 20e5779998SDaniel Mack #include <linux/usb.h> 21e5779998SDaniel Mack #include <linux/usb/audio.h> 227e847894SDaniel Mack #include <linux/usb/audio-v2.h> 23*9a2fe9b8SRuslan Bilovol #include <linux/usb/audio-v3.h> 24e5779998SDaniel Mack 25e5779998SDaniel Mack #include <sound/core.h> 26e5779998SDaniel Mack #include <sound/pcm.h> 27e5779998SDaniel Mack 28e5779998SDaniel Mack #include "usbaudio.h" 29e5779998SDaniel Mack #include "card.h" 30e5779998SDaniel Mack #include "quirks.h" 31e5779998SDaniel Mack #include "helper.h" 32e5779998SDaniel Mack #include "debug.h" 3379f920fbSDaniel Mack #include "clock.h" 34ee95cb61SDaniel Mack #include "format.h" 35e5779998SDaniel Mack 36e5779998SDaniel Mack /* 37e5779998SDaniel Mack * parse the audio format type I descriptor 38e5779998SDaniel Mack * and returns the corresponding pcm format 39e5779998SDaniel Mack * 40e5779998SDaniel Mack * @dev: usb device 41e5779998SDaniel Mack * @fp: audioformat record 42e5779998SDaniel Mack * @format: the format tag (wFormatTag) 43*9a2fe9b8SRuslan Bilovol * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3) 44e5779998SDaniel Mack */ 4529088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip, 46e5779998SDaniel Mack struct audioformat *fp, 47*9a2fe9b8SRuslan Bilovol u64 format, void *_fmt) 48e5779998SDaniel Mack { 49e5779998SDaniel Mack int sample_width, sample_bytes; 50717bfb5fSDaniel Mack u64 pcm_formats = 0; 51e5779998SDaniel Mack 528f898e92SClemens Ladisch switch (fp->protocol) { 53a2acad82SClemens Ladisch case UAC_VERSION_1: 54a2acad82SClemens Ladisch default: { 55e5779998SDaniel Mack struct uac_format_type_i_discrete_descriptor *fmt = _fmt; 56e5779998SDaniel Mack sample_width = fmt->bBitResolution; 57e5779998SDaniel Mack sample_bytes = fmt->bSubframeSize; 5829088fefSClemens Ladisch format = 1 << format; 59e5779998SDaniel Mack break; 60e5779998SDaniel Mack } 61e5779998SDaniel Mack 62e5779998SDaniel Mack case UAC_VERSION_2: { 63e5779998SDaniel Mack struct uac_format_type_i_ext_descriptor *fmt = _fmt; 64e5779998SDaniel Mack sample_width = fmt->bBitResolution; 65e5779998SDaniel Mack sample_bytes = fmt->bSubslotSize; 66717bfb5fSDaniel Mack 67717bfb5fSDaniel Mack if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) 68717bfb5fSDaniel Mack pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 69717bfb5fSDaniel Mack 7029088fefSClemens Ladisch format <<= 1; 71e5779998SDaniel Mack break; 72e5779998SDaniel Mack } 73*9a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 74*9a2fe9b8SRuslan Bilovol struct uac3_as_header_descriptor *as = _fmt; 75*9a2fe9b8SRuslan Bilovol 76*9a2fe9b8SRuslan Bilovol sample_width = as->bBitResolution; 77*9a2fe9b8SRuslan Bilovol sample_bytes = as->bSubslotSize; 78*9a2fe9b8SRuslan Bilovol 79*9a2fe9b8SRuslan Bilovol if (format & UAC3_FORMAT_TYPE_I_RAW_DATA) 80*9a2fe9b8SRuslan Bilovol pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 81*9a2fe9b8SRuslan Bilovol 82*9a2fe9b8SRuslan Bilovol format <<= 1; 83*9a2fe9b8SRuslan Bilovol break; 84*9a2fe9b8SRuslan Bilovol } 85e5779998SDaniel Mack } 86e5779998SDaniel Mack 87717bfb5fSDaniel Mack if ((pcm_formats == 0) && 88717bfb5fSDaniel Mack (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) { 8929088fefSClemens Ladisch /* some devices don't define this correctly... */ 900ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n", 910ba41d91STakashi Iwai fp->iface, fp->altsetting); 9229088fefSClemens Ladisch format = 1 << UAC_FORMAT_TYPE_I_PCM; 9329088fefSClemens Ladisch } 9429088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) { 956d1f2f60STakamichi Horikawa if (((chip->usb_id == USB_ID(0x0582, 0x0016)) || 966d1f2f60STakamichi Horikawa /* Edirol SD-90 */ 976d1f2f60STakamichi Horikawa (chip->usb_id == USB_ID(0x0582, 0x000c))) && 986d1f2f60STakamichi Horikawa /* Roland SC-D70 */ 99061b869eSClemens Ladisch sample_width == 24 && sample_bytes == 2) 100061b869eSClemens Ladisch sample_bytes = 3; 101061b869eSClemens Ladisch else if (sample_width > sample_bytes * 8) { 1020ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n", 1030ba41d91STakashi Iwai fp->iface, fp->altsetting, 104e5779998SDaniel Mack sample_width, sample_bytes); 105e5779998SDaniel Mack } 106e5779998SDaniel Mack /* check the format byte size */ 107e5779998SDaniel Mack switch (sample_bytes) { 108e5779998SDaniel Mack case 1: 10929088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 110e5779998SDaniel Mack break; 111e5779998SDaniel Mack case 2: 112e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 11329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */ 114e5779998SDaniel Mack else 11529088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE; 116e5779998SDaniel Mack break; 117e5779998SDaniel Mack case 3: 118e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 11929088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */ 120e5779998SDaniel Mack else 12129088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE; 122e5779998SDaniel Mack break; 123e5779998SDaniel Mack case 4: 12429088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE; 125e5779998SDaniel Mack break; 126e5779998SDaniel Mack default: 1270ba41d91STakashi Iwai usb_audio_info(chip, 1280ba41d91STakashi Iwai "%u:%d : unsupported sample bitwidth %d in %d bytes\n", 1290ba41d91STakashi Iwai fp->iface, fp->altsetting, 130e5779998SDaniel Mack sample_width, sample_bytes); 131e5779998SDaniel Mack break; 132e5779998SDaniel Mack } 13329088fefSClemens Ladisch } 13429088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) { 135e5779998SDaniel Mack /* Dallas DS4201 workaround: it advertises U8 format, but really 136e5779998SDaniel Mack supports S8. */ 137e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 13829088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 13929088fefSClemens Ladisch else 14029088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_U8; 141e5779998SDaniel Mack } 14229088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) { 14329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 14429088fefSClemens Ladisch } 14529088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) { 14629088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW; 14729088fefSClemens Ladisch } 14829088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) { 14929088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW; 15029088fefSClemens Ladisch } 15129088fefSClemens Ladisch if (format & ~0x3f) { 1520ba41d91STakashi Iwai usb_audio_info(chip, 153*9a2fe9b8SRuslan Bilovol "%u:%d : unsupported format bits %#llx\n", 1540ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 15529088fefSClemens Ladisch } 156126825e7SDaniel Mack 157126825e7SDaniel Mack pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes); 158126825e7SDaniel Mack 15929088fefSClemens Ladisch return pcm_formats; 160e5779998SDaniel Mack } 161e5779998SDaniel Mack 162e5779998SDaniel Mack 163e5779998SDaniel Mack /* 164e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 165e5779998SDaniel Mack * on the audioformat table (audio class v1). 166e5779998SDaniel Mack * 167e5779998SDaniel Mack * @dev: usb device 168e5779998SDaniel Mack * @fp: audioformat record 169e5779998SDaniel Mack * @fmt: the format descriptor 170e5779998SDaniel Mack * @offset: the start offset of descriptor pointing the rate type 171e5779998SDaniel Mack * (7 for type I and II, 8 for type II) 172e5779998SDaniel Mack */ 173e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp, 174e5779998SDaniel Mack unsigned char *fmt, int offset) 175e5779998SDaniel Mack { 176e5779998SDaniel Mack int nr_rates = fmt[offset]; 177e5779998SDaniel Mack 178e5779998SDaniel Mack if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { 1790ba41d91STakashi Iwai usb_audio_err(chip, 1800ba41d91STakashi Iwai "%u:%d : invalid UAC_FORMAT_TYPE desc\n", 1810ba41d91STakashi Iwai fp->iface, fp->altsetting); 1828ad10dc6SSachin Kamat return -EINVAL; 183e5779998SDaniel Mack } 184e5779998SDaniel Mack 185e5779998SDaniel Mack if (nr_rates) { 186e5779998SDaniel Mack /* 187e5779998SDaniel Mack * build the rate table and bitmap flags 188e5779998SDaniel Mack */ 189e5779998SDaniel Mack int r, idx; 190e5779998SDaniel Mack 191e5779998SDaniel Mack fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); 1921bc00f32SShawn Lin if (fp->rate_table == NULL) 1938ad10dc6SSachin Kamat return -ENOMEM; 194e5779998SDaniel Mack 195e5779998SDaniel Mack fp->nr_rates = 0; 196e5779998SDaniel Mack fp->rate_min = fp->rate_max = 0; 197e5779998SDaniel Mack for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 198e5779998SDaniel Mack unsigned int rate = combine_triple(&fmt[idx]); 199e5779998SDaniel Mack if (!rate) 200e5779998SDaniel Mack continue; 201e5779998SDaniel Mack /* C-Media CM6501 mislabels its 96 kHz altsetting */ 2028129e79eSWolfgang Breyha /* Terratec Aureon 7.1 USB C-Media 6206, too */ 203e5779998SDaniel Mack if (rate == 48000 && nr_rates == 1 && 204e5779998SDaniel Mack (chip->usb_id == USB_ID(0x0d8c, 0x0201) || 2058129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0d8c, 0x0102) || 2068129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0ccd, 0x00b1)) && 207e5779998SDaniel Mack fp->altsetting == 5 && fp->maxpacksize == 392) 208e5779998SDaniel Mack rate = 96000; 2098c4b79cfSPavel Hofman /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */ 2108c4b79cfSPavel Hofman if (rate == 16000 && 2118c4b79cfSPavel Hofman (chip->usb_id == USB_ID(0x041e, 0x4064) || 2128c4b79cfSPavel Hofman chip->usb_id == USB_ID(0x041e, 0x4068))) 213e5779998SDaniel Mack rate = 8000; 214e5779998SDaniel Mack 215e5779998SDaniel Mack fp->rate_table[fp->nr_rates] = rate; 216e5779998SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 217e5779998SDaniel Mack fp->rate_min = rate; 218e5779998SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 219e5779998SDaniel Mack fp->rate_max = rate; 220e5779998SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 221e5779998SDaniel Mack fp->nr_rates++; 222e5779998SDaniel Mack } 223e5779998SDaniel Mack if (!fp->nr_rates) { 224e5779998SDaniel Mack hwc_debug("All rates were zero. Skipping format!\n"); 2258ad10dc6SSachin Kamat return -EINVAL; 226e5779998SDaniel Mack } 227e5779998SDaniel Mack } else { 228e5779998SDaniel Mack /* continuous rates */ 229e5779998SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 230e5779998SDaniel Mack fp->rate_min = combine_triple(&fmt[offset + 1]); 231e5779998SDaniel Mack fp->rate_max = combine_triple(&fmt[offset + 4]); 232e5779998SDaniel Mack } 233e5779998SDaniel Mack return 0; 234e5779998SDaniel Mack } 235e5779998SDaniel Mack 236e5779998SDaniel Mack /* 23767c10366SDaniel Mack * Helper function to walk the array of sample rate triplets reported by 23867c10366SDaniel Mack * the device. The problem is that we need to parse whole array first to 23967c10366SDaniel Mack * get to know how many sample rates we have to expect. 24067c10366SDaniel Mack * Then fp->rate_table can be allocated and filled. 24167c10366SDaniel Mack */ 2420ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip, 2430ba41d91STakashi Iwai struct audioformat *fp, int nr_triplets, 24467c10366SDaniel Mack const unsigned char *data) 24567c10366SDaniel Mack { 24667c10366SDaniel Mack int i, nr_rates = 0; 24767c10366SDaniel Mack 24867c10366SDaniel Mack fp->rates = fp->rate_min = fp->rate_max = 0; 24967c10366SDaniel Mack 25067c10366SDaniel Mack for (i = 0; i < nr_triplets; i++) { 25167c10366SDaniel Mack int min = combine_quad(&data[2 + 12 * i]); 25267c10366SDaniel Mack int max = combine_quad(&data[6 + 12 * i]); 25367c10366SDaniel Mack int res = combine_quad(&data[10 + 12 * i]); 2544fa0e81bSXi Wang unsigned int rate; 25567c10366SDaniel Mack 25667c10366SDaniel Mack if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 25767c10366SDaniel Mack continue; 25867c10366SDaniel Mack 25967c10366SDaniel Mack /* 26067c10366SDaniel Mack * for ranges with res == 1, we announce a continuous sample 26167c10366SDaniel Mack * rate range, and this function should return 0 for no further 26267c10366SDaniel Mack * parsing. 26367c10366SDaniel Mack */ 26467c10366SDaniel Mack if (res == 1) { 26567c10366SDaniel Mack fp->rate_min = min; 26667c10366SDaniel Mack fp->rate_max = max; 26767c10366SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 26867c10366SDaniel Mack return 0; 26967c10366SDaniel Mack } 27067c10366SDaniel Mack 27167c10366SDaniel Mack for (rate = min; rate <= max; rate += res) { 27267c10366SDaniel Mack if (fp->rate_table) 27367c10366SDaniel Mack fp->rate_table[nr_rates] = rate; 27467c10366SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 27567c10366SDaniel Mack fp->rate_min = rate; 27667c10366SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 27767c10366SDaniel Mack fp->rate_max = rate; 27867c10366SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 27967c10366SDaniel Mack 28067c10366SDaniel Mack nr_rates++; 2818866f405SXi Wang if (nr_rates >= MAX_NR_RATES) { 2820ba41d91STakashi Iwai usb_audio_err(chip, "invalid uac2 rates\n"); 2834fa0e81bSXi Wang break; 2844fa0e81bSXi Wang } 28567c10366SDaniel Mack 28667c10366SDaniel Mack /* avoid endless loop */ 28767c10366SDaniel Mack if (res == 0) 28867c10366SDaniel Mack break; 28967c10366SDaniel Mack } 29067c10366SDaniel Mack } 29167c10366SDaniel Mack 29267c10366SDaniel Mack return nr_rates; 29367c10366SDaniel Mack } 29467c10366SDaniel Mack 29567c10366SDaniel Mack /* 296e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 297*9a2fe9b8SRuslan Bilovol * on the audioformat table (audio class v2 and v3). 298e5779998SDaniel Mack */ 299*9a2fe9b8SRuslan Bilovol static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip, 3003d8d4dcfSDaniel Mack struct audioformat *fp) 301e5779998SDaniel Mack { 302e5779998SDaniel Mack struct usb_device *dev = chip->dev; 303e5779998SDaniel Mack unsigned char tmp[2], *data; 30467c10366SDaniel Mack int nr_triplets, data_size, ret = 0; 305*9a2fe9b8SRuslan Bilovol int clock = snd_usb_clock_find_source(chip, fp->protocol, 306*9a2fe9b8SRuslan Bilovol fp->clock, false); 307e5779998SDaniel Mack 308d07140baSDaniel Mack if (clock < 0) { 3090ba41d91STakashi Iwai dev_err(&dev->dev, 3100ba41d91STakashi Iwai "%s(): unable to find clock source (clock %d)\n", 311d07140baSDaniel Mack __func__, clock); 312d07140baSDaniel Mack goto err; 313d07140baSDaniel Mack } 314d07140baSDaniel Mack 315e5779998SDaniel Mack /* get the number of sample rates first by only fetching 2 bytes */ 316e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 317e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 31811bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 31911bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 32017d900c4SClemens Ladisch tmp, sizeof(tmp)); 321e5779998SDaniel Mack 322e5779998SDaniel Mack if (ret < 0) { 3230ba41d91STakashi Iwai dev_err(&dev->dev, 3240ba41d91STakashi Iwai "%s(): unable to retrieve number of sample rates (clock %d)\n", 32579f920fbSDaniel Mack __func__, clock); 326e5779998SDaniel Mack goto err; 327e5779998SDaniel Mack } 328e5779998SDaniel Mack 32967c10366SDaniel Mack nr_triplets = (tmp[1] << 8) | tmp[0]; 33067c10366SDaniel Mack data_size = 2 + 12 * nr_triplets; 331e5779998SDaniel Mack data = kzalloc(data_size, GFP_KERNEL); 332e5779998SDaniel Mack if (!data) { 333e5779998SDaniel Mack ret = -ENOMEM; 334e5779998SDaniel Mack goto err; 335e5779998SDaniel Mack } 336e5779998SDaniel Mack 337e5779998SDaniel Mack /* now get the full information */ 338e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 339e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 34011bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 34111bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 34217d900c4SClemens Ladisch data, data_size); 343e5779998SDaniel Mack 344e5779998SDaniel Mack if (ret < 0) { 3450ba41d91STakashi Iwai dev_err(&dev->dev, 3460ba41d91STakashi Iwai "%s(): unable to retrieve sample rate range (clock %d)\n", 34779f920fbSDaniel Mack __func__, clock); 348e5779998SDaniel Mack ret = -EINVAL; 349e5779998SDaniel Mack goto err_free; 350e5779998SDaniel Mack } 351e5779998SDaniel Mack 35267c10366SDaniel Mack /* Call the triplet parser, and make sure fp->rate_table is NULL. 35367c10366SDaniel Mack * We just use the return value to know how many sample rates we 35467c10366SDaniel Mack * will have to deal with. */ 35567c10366SDaniel Mack kfree(fp->rate_table); 35667c10366SDaniel Mack fp->rate_table = NULL; 3570ba41d91STakashi Iwai fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 35867c10366SDaniel Mack 35967c10366SDaniel Mack if (fp->nr_rates == 0) { 36067c10366SDaniel Mack /* SNDRV_PCM_RATE_CONTINUOUS */ 36167c10366SDaniel Mack ret = 0; 36267c10366SDaniel Mack goto err_free; 36367c10366SDaniel Mack } 36467c10366SDaniel Mack 36567c10366SDaniel Mack fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL); 366e5779998SDaniel Mack if (!fp->rate_table) { 367e5779998SDaniel Mack ret = -ENOMEM; 368e5779998SDaniel Mack goto err_free; 369e5779998SDaniel Mack } 370e5779998SDaniel Mack 37167c10366SDaniel Mack /* Call the triplet parser again, but this time, fp->rate_table is 37267c10366SDaniel Mack * allocated, so the rates will be stored */ 3730ba41d91STakashi Iwai parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 374e5779998SDaniel Mack 375e5779998SDaniel Mack err_free: 376e5779998SDaniel Mack kfree(data); 377e5779998SDaniel Mack err: 378e5779998SDaniel Mack return ret; 379e5779998SDaniel Mack } 380e5779998SDaniel Mack 381e5779998SDaniel Mack /* 382e5779998SDaniel Mack * parse the format type I and III descriptors 383e5779998SDaniel Mack */ 384e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip, 385*9a2fe9b8SRuslan Bilovol struct audioformat *fp, u64 format, 386*9a2fe9b8SRuslan Bilovol void *_fmt) 387e5779998SDaniel Mack { 38874c34ca1SEldad Zack snd_pcm_format_t pcm_format; 389*9a2fe9b8SRuslan Bilovol unsigned int fmt_type; 39074c34ca1SEldad Zack int ret; 391e5779998SDaniel Mack 392*9a2fe9b8SRuslan Bilovol switch (fp->protocol) { 393*9a2fe9b8SRuslan Bilovol default: 394*9a2fe9b8SRuslan Bilovol case UAC_VERSION_1: 395*9a2fe9b8SRuslan Bilovol case UAC_VERSION_2: { 396*9a2fe9b8SRuslan Bilovol struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 397*9a2fe9b8SRuslan Bilovol 398*9a2fe9b8SRuslan Bilovol fmt_type = fmt->bFormatType; 399*9a2fe9b8SRuslan Bilovol break; 400*9a2fe9b8SRuslan Bilovol } 401*9a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 402*9a2fe9b8SRuslan Bilovol /* fp->fmt_type is already set in this case */ 403*9a2fe9b8SRuslan Bilovol fmt_type = fp->fmt_type; 404*9a2fe9b8SRuslan Bilovol break; 405*9a2fe9b8SRuslan Bilovol } 406*9a2fe9b8SRuslan Bilovol } 407*9a2fe9b8SRuslan Bilovol 408*9a2fe9b8SRuslan Bilovol if (fmt_type == UAC_FORMAT_TYPE_III) { 409e5779998SDaniel Mack /* FIXME: the format type is really IECxxx 410e5779998SDaniel Mack * but we give normal PCM format to get the existing 411e5779998SDaniel Mack * apps working... 412e5779998SDaniel Mack */ 413e5779998SDaniel Mack switch (chip->usb_id) { 414e5779998SDaniel Mack 415e5779998SDaniel Mack case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 416e5779998SDaniel Mack if (chip->setup == 0x00 && 417e5779998SDaniel Mack fp->altsetting == 6) 418e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_BE; 419e5779998SDaniel Mack else 420e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 421e5779998SDaniel Mack break; 422e5779998SDaniel Mack default: 423e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 424e5779998SDaniel Mack } 42574c34ca1SEldad Zack fp->formats = pcm_format_to_bits(pcm_format); 426e5779998SDaniel Mack } else { 427*9a2fe9b8SRuslan Bilovol fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt); 42829088fefSClemens Ladisch if (!fp->formats) 4298ad10dc6SSachin Kamat return -EINVAL; 430e5779998SDaniel Mack } 431e5779998SDaniel Mack 432e5779998SDaniel Mack /* gather possible sample rates */ 433e5779998SDaniel Mack /* audio class v1 reports possible sample rates as part of the 434e5779998SDaniel Mack * proprietary class specific descriptor. 435e5779998SDaniel Mack * audio class v2 uses class specific EP0 range requests for that. 436e5779998SDaniel Mack */ 4378f898e92SClemens Ladisch switch (fp->protocol) { 438a2acad82SClemens Ladisch default: 439*9a2fe9b8SRuslan Bilovol case UAC_VERSION_1: { 440*9a2fe9b8SRuslan Bilovol struct uac_format_type_i_continuous_descriptor *fmt = _fmt; 441*9a2fe9b8SRuslan Bilovol 442e5779998SDaniel Mack fp->channels = fmt->bNrChannels; 44374754f97SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 444e5779998SDaniel Mack break; 445*9a2fe9b8SRuslan Bilovol } 446e5779998SDaniel Mack case UAC_VERSION_2: 447*9a2fe9b8SRuslan Bilovol case UAC_VERSION_3: { 448e5779998SDaniel Mack /* fp->channels is already set in this case */ 449*9a2fe9b8SRuslan Bilovol ret = parse_audio_format_rates_v2v3(chip, fp); 450e5779998SDaniel Mack break; 451e5779998SDaniel Mack } 452*9a2fe9b8SRuslan Bilovol } 453e5779998SDaniel Mack 454e5779998SDaniel Mack if (fp->channels < 1) { 4550ba41d91STakashi Iwai usb_audio_err(chip, 4560ba41d91STakashi Iwai "%u:%d : invalid channels %d\n", 4570ba41d91STakashi Iwai fp->iface, fp->altsetting, fp->channels); 4588ad10dc6SSachin Kamat return -EINVAL; 459e5779998SDaniel Mack } 460e5779998SDaniel Mack 461e5779998SDaniel Mack return ret; 462e5779998SDaniel Mack } 463e5779998SDaniel Mack 464e5779998SDaniel Mack /* 465e5779998SDaniel Mack * parse the format type II descriptor 466e5779998SDaniel Mack */ 467e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip, 468e5779998SDaniel Mack struct audioformat *fp, 469*9a2fe9b8SRuslan Bilovol u64 format, void *_fmt) 470e5779998SDaniel Mack { 471e5779998SDaniel Mack int brate, framesize, ret; 472e5779998SDaniel Mack 473e5779998SDaniel Mack switch (format) { 474e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_AC3: 475e5779998SDaniel Mack /* FIXME: there is no AC3 format defined yet */ 476015eb0b0SClemens Ladisch // fp->formats = SNDRV_PCM_FMTBIT_AC3; 477015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 478e5779998SDaniel Mack break; 479e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_MPEG: 480015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 481e5779998SDaniel Mack break; 482e5779998SDaniel Mack default: 4830ba41d91STakashi Iwai usb_audio_info(chip, 484*9a2fe9b8SRuslan Bilovol "%u:%d : unknown format tag %#llx is detected. processed as MPEG.\n", 4850ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 486015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 487e5779998SDaniel Mack break; 488e5779998SDaniel Mack } 489e5779998SDaniel Mack 490e5779998SDaniel Mack fp->channels = 1; 491e5779998SDaniel Mack 4928f898e92SClemens Ladisch switch (fp->protocol) { 493a2acad82SClemens Ladisch default: 494e5779998SDaniel Mack case UAC_VERSION_1: { 495e5779998SDaniel Mack struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 496e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 497e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 4980ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 499e5779998SDaniel Mack fp->frame_size = framesize; 500e5779998SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 501e5779998SDaniel Mack break; 502e5779998SDaniel Mack } 503e5779998SDaniel Mack case UAC_VERSION_2: { 504e5779998SDaniel Mack struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 505e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 506e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 5070ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 508e5779998SDaniel Mack fp->frame_size = framesize; 509*9a2fe9b8SRuslan Bilovol ret = parse_audio_format_rates_v2v3(chip, fp); 510e5779998SDaniel Mack break; 511e5779998SDaniel Mack } 512e5779998SDaniel Mack } 513e5779998SDaniel Mack 514e5779998SDaniel Mack return ret; 515e5779998SDaniel Mack } 516e5779998SDaniel Mack 5172fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip, 518*9a2fe9b8SRuslan Bilovol struct audioformat *fp, u64 format, 5192fcdb06dSDaniel Mack struct uac_format_type_i_continuous_descriptor *fmt, 5208f898e92SClemens Ladisch int stream) 521e5779998SDaniel Mack { 522e5779998SDaniel Mack int err; 523e5779998SDaniel Mack 52474754f97SDaniel Mack switch (fmt->bFormatType) { 525e5779998SDaniel Mack case UAC_FORMAT_TYPE_I: 526e5779998SDaniel Mack case UAC_FORMAT_TYPE_III: 5278f898e92SClemens Ladisch err = parse_audio_format_i(chip, fp, format, fmt); 528e5779998SDaniel Mack break; 529e5779998SDaniel Mack case UAC_FORMAT_TYPE_II: 5308f898e92SClemens Ladisch err = parse_audio_format_ii(chip, fp, format, fmt); 531e5779998SDaniel Mack break; 532e5779998SDaniel Mack default: 5330ba41d91STakashi Iwai usb_audio_info(chip, 5340ba41d91STakashi Iwai "%u:%d : format type %d is not supported yet\n", 5350ba41d91STakashi Iwai fp->iface, fp->altsetting, 53674754f97SDaniel Mack fmt->bFormatType); 5378d091242SDaniel Mack return -ENOTSUPP; 538e5779998SDaniel Mack } 53974754f97SDaniel Mack fp->fmt_type = fmt->bFormatType; 540e5779998SDaniel Mack if (err < 0) 541e5779998SDaniel Mack return err; 542e5779998SDaniel Mack #if 1 543e5779998SDaniel Mack /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 544e5779998SDaniel Mack /* extigy apparently supports sample rates other than 48k 545e5779998SDaniel Mack * but not in ordinary way. so we enable only 48k atm. 546e5779998SDaniel Mack */ 547e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x041e, 0x3000) || 548e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3020) || 549e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3061)) { 55074754f97SDaniel Mack if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 551e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_48000 && 552e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_96000) 5538d091242SDaniel Mack return -ENOTSUPP; 554e5779998SDaniel Mack } 555e5779998SDaniel Mack #endif 556e5779998SDaniel Mack return 0; 557e5779998SDaniel Mack } 558e5779998SDaniel Mack 559*9a2fe9b8SRuslan Bilovol int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip, 560*9a2fe9b8SRuslan Bilovol struct audioformat *fp, 561*9a2fe9b8SRuslan Bilovol struct uac3_as_header_descriptor *as, 562*9a2fe9b8SRuslan Bilovol int stream) 563*9a2fe9b8SRuslan Bilovol { 564*9a2fe9b8SRuslan Bilovol u64 format = le64_to_cpu(as->bmFormats); 565*9a2fe9b8SRuslan Bilovol int err; 566*9a2fe9b8SRuslan Bilovol 567*9a2fe9b8SRuslan Bilovol /* 568*9a2fe9b8SRuslan Bilovol * Type I format bits are D0..D6 569*9a2fe9b8SRuslan Bilovol * This test works because type IV is not supported 570*9a2fe9b8SRuslan Bilovol */ 571*9a2fe9b8SRuslan Bilovol if (format & 0x7f) 572*9a2fe9b8SRuslan Bilovol fp->fmt_type = UAC_FORMAT_TYPE_I; 573*9a2fe9b8SRuslan Bilovol else 574*9a2fe9b8SRuslan Bilovol fp->fmt_type = UAC_FORMAT_TYPE_III; 575*9a2fe9b8SRuslan Bilovol 576*9a2fe9b8SRuslan Bilovol err = parse_audio_format_i(chip, fp, format, as); 577*9a2fe9b8SRuslan Bilovol if (err < 0) 578*9a2fe9b8SRuslan Bilovol return err; 579*9a2fe9b8SRuslan Bilovol 580*9a2fe9b8SRuslan Bilovol return 0; 581*9a2fe9b8SRuslan Bilovol } 582