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> 23e5779998SDaniel Mack 24e5779998SDaniel Mack #include <sound/core.h> 25e5779998SDaniel Mack #include <sound/pcm.h> 26e5779998SDaniel Mack 27e5779998SDaniel Mack #include "usbaudio.h" 28e5779998SDaniel Mack #include "card.h" 29e5779998SDaniel Mack #include "quirks.h" 30e5779998SDaniel Mack #include "helper.h" 31e5779998SDaniel Mack #include "debug.h" 3279f920fbSDaniel Mack #include "clock.h" 33ee95cb61SDaniel Mack #include "format.h" 34e5779998SDaniel Mack 35e5779998SDaniel Mack /* 36e5779998SDaniel Mack * parse the audio format type I descriptor 37e5779998SDaniel Mack * and returns the corresponding pcm format 38e5779998SDaniel Mack * 39e5779998SDaniel Mack * @dev: usb device 40e5779998SDaniel Mack * @fp: audioformat record 41e5779998SDaniel Mack * @format: the format tag (wFormatTag) 42e5779998SDaniel Mack * @fmt: the format type descriptor 43e5779998SDaniel Mack */ 4429088fefSClemens Ladisch static u64 parse_audio_format_i_type(struct snd_usb_audio *chip, 45e5779998SDaniel Mack struct audioformat *fp, 468f898e92SClemens Ladisch unsigned int format, void *_fmt) 47e5779998SDaniel Mack { 48e5779998SDaniel Mack int sample_width, sample_bytes; 49717bfb5fSDaniel Mack u64 pcm_formats = 0; 50e5779998SDaniel Mack 518f898e92SClemens Ladisch switch (fp->protocol) { 52a2acad82SClemens Ladisch case UAC_VERSION_1: 53a2acad82SClemens Ladisch default: { 54e5779998SDaniel Mack struct uac_format_type_i_discrete_descriptor *fmt = _fmt; 55e5779998SDaniel Mack sample_width = fmt->bBitResolution; 56e5779998SDaniel Mack sample_bytes = fmt->bSubframeSize; 5729088fefSClemens Ladisch format = 1 << format; 58e5779998SDaniel Mack break; 59e5779998SDaniel Mack } 60e5779998SDaniel Mack 61e5779998SDaniel Mack case UAC_VERSION_2: { 62e5779998SDaniel Mack struct uac_format_type_i_ext_descriptor *fmt = _fmt; 63e5779998SDaniel Mack sample_width = fmt->bBitResolution; 64e5779998SDaniel Mack sample_bytes = fmt->bSubslotSize; 65717bfb5fSDaniel Mack 66717bfb5fSDaniel Mack if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) 67717bfb5fSDaniel Mack pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL; 68717bfb5fSDaniel Mack 6929088fefSClemens Ladisch format <<= 1; 70e5779998SDaniel Mack break; 71e5779998SDaniel Mack } 72e5779998SDaniel Mack } 73e5779998SDaniel Mack 74717bfb5fSDaniel Mack if ((pcm_formats == 0) && 75717bfb5fSDaniel Mack (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) { 7629088fefSClemens Ladisch /* some devices don't define this correctly... */ 77*0ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n", 78*0ba41d91STakashi Iwai fp->iface, fp->altsetting); 7929088fefSClemens Ladisch format = 1 << UAC_FORMAT_TYPE_I_PCM; 8029088fefSClemens Ladisch } 8129088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) { 82061b869eSClemens Ladisch if (chip->usb_id == USB_ID(0x0582, 0x0016) /* Edirol SD-90 */ && 83061b869eSClemens Ladisch sample_width == 24 && sample_bytes == 2) 84061b869eSClemens Ladisch sample_bytes = 3; 85061b869eSClemens Ladisch else if (sample_width > sample_bytes * 8) { 86*0ba41d91STakashi Iwai usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n", 87*0ba41d91STakashi Iwai fp->iface, fp->altsetting, 88e5779998SDaniel Mack sample_width, sample_bytes); 89e5779998SDaniel Mack } 90e5779998SDaniel Mack /* check the format byte size */ 91e5779998SDaniel Mack switch (sample_bytes) { 92e5779998SDaniel Mack case 1: 9329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 94e5779998SDaniel Mack break; 95e5779998SDaniel Mack case 2: 96e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 9729088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */ 98e5779998SDaniel Mack else 9929088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE; 100e5779998SDaniel Mack break; 101e5779998SDaniel Mack case 3: 102e5779998SDaniel Mack if (snd_usb_is_big_endian_format(chip, fp)) 10329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */ 104e5779998SDaniel Mack else 10529088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE; 106e5779998SDaniel Mack break; 107e5779998SDaniel Mack case 4: 10829088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE; 109e5779998SDaniel Mack break; 110e5779998SDaniel Mack default: 111*0ba41d91STakashi Iwai usb_audio_info(chip, 112*0ba41d91STakashi Iwai "%u:%d : unsupported sample bitwidth %d in %d bytes\n", 113*0ba41d91STakashi Iwai fp->iface, fp->altsetting, 114e5779998SDaniel Mack sample_width, sample_bytes); 115e5779998SDaniel Mack break; 116e5779998SDaniel Mack } 11729088fefSClemens Ladisch } 11829088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) { 119e5779998SDaniel Mack /* Dallas DS4201 workaround: it advertises U8 format, but really 120e5779998SDaniel Mack supports S8. */ 121e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x04fa, 0x4201)) 12229088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_S8; 12329088fefSClemens Ladisch else 12429088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_U8; 125e5779998SDaniel Mack } 12629088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) { 12729088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 12829088fefSClemens Ladisch } 12929088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) { 13029088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW; 13129088fefSClemens Ladisch } 13229088fefSClemens Ladisch if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) { 13329088fefSClemens Ladisch pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW; 13429088fefSClemens Ladisch } 13529088fefSClemens Ladisch if (format & ~0x3f) { 136*0ba41d91STakashi Iwai usb_audio_info(chip, 137*0ba41d91STakashi Iwai "%u:%d : unsupported format bits %#x\n", 138*0ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 13929088fefSClemens Ladisch } 140126825e7SDaniel Mack 141126825e7SDaniel Mack pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes); 142126825e7SDaniel Mack 14329088fefSClemens Ladisch return pcm_formats; 144e5779998SDaniel Mack } 145e5779998SDaniel Mack 146e5779998SDaniel Mack 147e5779998SDaniel Mack /* 148e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 149e5779998SDaniel Mack * on the audioformat table (audio class v1). 150e5779998SDaniel Mack * 151e5779998SDaniel Mack * @dev: usb device 152e5779998SDaniel Mack * @fp: audioformat record 153e5779998SDaniel Mack * @fmt: the format descriptor 154e5779998SDaniel Mack * @offset: the start offset of descriptor pointing the rate type 155e5779998SDaniel Mack * (7 for type I and II, 8 for type II) 156e5779998SDaniel Mack */ 157e5779998SDaniel Mack static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp, 158e5779998SDaniel Mack unsigned char *fmt, int offset) 159e5779998SDaniel Mack { 160e5779998SDaniel Mack int nr_rates = fmt[offset]; 161e5779998SDaniel Mack 162e5779998SDaniel Mack if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) { 163*0ba41d91STakashi Iwai usb_audio_err(chip, 164*0ba41d91STakashi Iwai "%u:%d : invalid UAC_FORMAT_TYPE desc\n", 165*0ba41d91STakashi Iwai fp->iface, fp->altsetting); 1668ad10dc6SSachin Kamat return -EINVAL; 167e5779998SDaniel Mack } 168e5779998SDaniel Mack 169e5779998SDaniel Mack if (nr_rates) { 170e5779998SDaniel Mack /* 171e5779998SDaniel Mack * build the rate table and bitmap flags 172e5779998SDaniel Mack */ 173e5779998SDaniel Mack int r, idx; 174e5779998SDaniel Mack 175e5779998SDaniel Mack fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL); 176e5779998SDaniel Mack if (fp->rate_table == NULL) { 177*0ba41d91STakashi Iwai usb_audio_err(chip, "cannot malloc\n"); 1788ad10dc6SSachin Kamat return -ENOMEM; 179e5779998SDaniel Mack } 180e5779998SDaniel Mack 181e5779998SDaniel Mack fp->nr_rates = 0; 182e5779998SDaniel Mack fp->rate_min = fp->rate_max = 0; 183e5779998SDaniel Mack for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) { 184e5779998SDaniel Mack unsigned int rate = combine_triple(&fmt[idx]); 185e5779998SDaniel Mack if (!rate) 186e5779998SDaniel Mack continue; 187e5779998SDaniel Mack /* C-Media CM6501 mislabels its 96 kHz altsetting */ 1888129e79eSWolfgang Breyha /* Terratec Aureon 7.1 USB C-Media 6206, too */ 189e5779998SDaniel Mack if (rate == 48000 && nr_rates == 1 && 190e5779998SDaniel Mack (chip->usb_id == USB_ID(0x0d8c, 0x0201) || 1918129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0d8c, 0x0102) || 1928129e79eSWolfgang Breyha chip->usb_id == USB_ID(0x0ccd, 0x00b1)) && 193e5779998SDaniel Mack fp->altsetting == 5 && fp->maxpacksize == 392) 194e5779998SDaniel Mack rate = 96000; 1958c4b79cfSPavel Hofman /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */ 1968c4b79cfSPavel Hofman if (rate == 16000 && 1978c4b79cfSPavel Hofman (chip->usb_id == USB_ID(0x041e, 0x4064) || 1988c4b79cfSPavel Hofman chip->usb_id == USB_ID(0x041e, 0x4068))) 199e5779998SDaniel Mack rate = 8000; 200e5779998SDaniel Mack 201e5779998SDaniel Mack fp->rate_table[fp->nr_rates] = rate; 202e5779998SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 203e5779998SDaniel Mack fp->rate_min = rate; 204e5779998SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 205e5779998SDaniel Mack fp->rate_max = rate; 206e5779998SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 207e5779998SDaniel Mack fp->nr_rates++; 208e5779998SDaniel Mack } 209e5779998SDaniel Mack if (!fp->nr_rates) { 210e5779998SDaniel Mack hwc_debug("All rates were zero. Skipping format!\n"); 2118ad10dc6SSachin Kamat return -EINVAL; 212e5779998SDaniel Mack } 213e5779998SDaniel Mack } else { 214e5779998SDaniel Mack /* continuous rates */ 215e5779998SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 216e5779998SDaniel Mack fp->rate_min = combine_triple(&fmt[offset + 1]); 217e5779998SDaniel Mack fp->rate_max = combine_triple(&fmt[offset + 4]); 218e5779998SDaniel Mack } 219e5779998SDaniel Mack return 0; 220e5779998SDaniel Mack } 221e5779998SDaniel Mack 222e5779998SDaniel Mack /* 22367c10366SDaniel Mack * Helper function to walk the array of sample rate triplets reported by 22467c10366SDaniel Mack * the device. The problem is that we need to parse whole array first to 22567c10366SDaniel Mack * get to know how many sample rates we have to expect. 22667c10366SDaniel Mack * Then fp->rate_table can be allocated and filled. 22767c10366SDaniel Mack */ 228*0ba41d91STakashi Iwai static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip, 229*0ba41d91STakashi Iwai struct audioformat *fp, int nr_triplets, 23067c10366SDaniel Mack const unsigned char *data) 23167c10366SDaniel Mack { 23267c10366SDaniel Mack int i, nr_rates = 0; 23367c10366SDaniel Mack 23467c10366SDaniel Mack fp->rates = fp->rate_min = fp->rate_max = 0; 23567c10366SDaniel Mack 23667c10366SDaniel Mack for (i = 0; i < nr_triplets; i++) { 23767c10366SDaniel Mack int min = combine_quad(&data[2 + 12 * i]); 23867c10366SDaniel Mack int max = combine_quad(&data[6 + 12 * i]); 23967c10366SDaniel Mack int res = combine_quad(&data[10 + 12 * i]); 2404fa0e81bSXi Wang unsigned int rate; 24167c10366SDaniel Mack 24267c10366SDaniel Mack if ((max < 0) || (min < 0) || (res < 0) || (max < min)) 24367c10366SDaniel Mack continue; 24467c10366SDaniel Mack 24567c10366SDaniel Mack /* 24667c10366SDaniel Mack * for ranges with res == 1, we announce a continuous sample 24767c10366SDaniel Mack * rate range, and this function should return 0 for no further 24867c10366SDaniel Mack * parsing. 24967c10366SDaniel Mack */ 25067c10366SDaniel Mack if (res == 1) { 25167c10366SDaniel Mack fp->rate_min = min; 25267c10366SDaniel Mack fp->rate_max = max; 25367c10366SDaniel Mack fp->rates = SNDRV_PCM_RATE_CONTINUOUS; 25467c10366SDaniel Mack return 0; 25567c10366SDaniel Mack } 25667c10366SDaniel Mack 25767c10366SDaniel Mack for (rate = min; rate <= max; rate += res) { 25867c10366SDaniel Mack if (fp->rate_table) 25967c10366SDaniel Mack fp->rate_table[nr_rates] = rate; 26067c10366SDaniel Mack if (!fp->rate_min || rate < fp->rate_min) 26167c10366SDaniel Mack fp->rate_min = rate; 26267c10366SDaniel Mack if (!fp->rate_max || rate > fp->rate_max) 26367c10366SDaniel Mack fp->rate_max = rate; 26467c10366SDaniel Mack fp->rates |= snd_pcm_rate_to_rate_bit(rate); 26567c10366SDaniel Mack 26667c10366SDaniel Mack nr_rates++; 2678866f405SXi Wang if (nr_rates >= MAX_NR_RATES) { 268*0ba41d91STakashi Iwai usb_audio_err(chip, "invalid uac2 rates\n"); 2694fa0e81bSXi Wang break; 2704fa0e81bSXi Wang } 27167c10366SDaniel Mack 27267c10366SDaniel Mack /* avoid endless loop */ 27367c10366SDaniel Mack if (res == 0) 27467c10366SDaniel Mack break; 27567c10366SDaniel Mack } 27667c10366SDaniel Mack } 27767c10366SDaniel Mack 27867c10366SDaniel Mack return nr_rates; 27967c10366SDaniel Mack } 28067c10366SDaniel Mack 28167c10366SDaniel Mack /* 282e5779998SDaniel Mack * parse the format descriptor and stores the possible sample rates 283e5779998SDaniel Mack * on the audioformat table (audio class v2). 284e5779998SDaniel Mack */ 285e5779998SDaniel Mack static int parse_audio_format_rates_v2(struct snd_usb_audio *chip, 2863d8d4dcfSDaniel Mack struct audioformat *fp) 287e5779998SDaniel Mack { 288e5779998SDaniel Mack struct usb_device *dev = chip->dev; 289e5779998SDaniel Mack unsigned char tmp[2], *data; 29067c10366SDaniel Mack int nr_triplets, data_size, ret = 0; 29106ffc1ebSEldad Zack int clock = snd_usb_clock_find_source(chip, fp->clock, false); 292e5779998SDaniel Mack 293d07140baSDaniel Mack if (clock < 0) { 294*0ba41d91STakashi Iwai dev_err(&dev->dev, 295*0ba41d91STakashi Iwai "%s(): unable to find clock source (clock %d)\n", 296d07140baSDaniel Mack __func__, clock); 297d07140baSDaniel Mack goto err; 298d07140baSDaniel Mack } 299d07140baSDaniel Mack 300e5779998SDaniel Mack /* get the number of sample rates first by only fetching 2 bytes */ 301e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 302e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 30311bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 30411bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 30517d900c4SClemens Ladisch tmp, sizeof(tmp)); 306e5779998SDaniel Mack 307e5779998SDaniel Mack if (ret < 0) { 308*0ba41d91STakashi Iwai dev_err(&dev->dev, 309*0ba41d91STakashi Iwai "%s(): unable to retrieve number of sample rates (clock %d)\n", 31079f920fbSDaniel Mack __func__, clock); 311e5779998SDaniel Mack goto err; 312e5779998SDaniel Mack } 313e5779998SDaniel Mack 31467c10366SDaniel Mack nr_triplets = (tmp[1] << 8) | tmp[0]; 31567c10366SDaniel Mack data_size = 2 + 12 * nr_triplets; 316e5779998SDaniel Mack data = kzalloc(data_size, GFP_KERNEL); 317e5779998SDaniel Mack if (!data) { 318e5779998SDaniel Mack ret = -ENOMEM; 319e5779998SDaniel Mack goto err; 320e5779998SDaniel Mack } 321e5779998SDaniel Mack 322e5779998SDaniel Mack /* now get the full information */ 323e5779998SDaniel Mack ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE, 324e5779998SDaniel Mack USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, 32511bcbc44SDaniel Mack UAC2_CS_CONTROL_SAM_FREQ << 8, 32611bcbc44SDaniel Mack snd_usb_ctrl_intf(chip) | (clock << 8), 32717d900c4SClemens Ladisch data, data_size); 328e5779998SDaniel Mack 329e5779998SDaniel Mack if (ret < 0) { 330*0ba41d91STakashi Iwai dev_err(&dev->dev, 331*0ba41d91STakashi Iwai "%s(): unable to retrieve sample rate range (clock %d)\n", 33279f920fbSDaniel Mack __func__, clock); 333e5779998SDaniel Mack ret = -EINVAL; 334e5779998SDaniel Mack goto err_free; 335e5779998SDaniel Mack } 336e5779998SDaniel Mack 33767c10366SDaniel Mack /* Call the triplet parser, and make sure fp->rate_table is NULL. 33867c10366SDaniel Mack * We just use the return value to know how many sample rates we 33967c10366SDaniel Mack * will have to deal with. */ 34067c10366SDaniel Mack kfree(fp->rate_table); 34167c10366SDaniel Mack fp->rate_table = NULL; 342*0ba41d91STakashi Iwai fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 34367c10366SDaniel Mack 34467c10366SDaniel Mack if (fp->nr_rates == 0) { 34567c10366SDaniel Mack /* SNDRV_PCM_RATE_CONTINUOUS */ 34667c10366SDaniel Mack ret = 0; 34767c10366SDaniel Mack goto err_free; 34867c10366SDaniel Mack } 34967c10366SDaniel Mack 35067c10366SDaniel Mack fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL); 351e5779998SDaniel Mack if (!fp->rate_table) { 352e5779998SDaniel Mack ret = -ENOMEM; 353e5779998SDaniel Mack goto err_free; 354e5779998SDaniel Mack } 355e5779998SDaniel Mack 35667c10366SDaniel Mack /* Call the triplet parser again, but this time, fp->rate_table is 35767c10366SDaniel Mack * allocated, so the rates will be stored */ 358*0ba41d91STakashi Iwai parse_uac2_sample_rate_range(chip, fp, nr_triplets, data); 359e5779998SDaniel Mack 360e5779998SDaniel Mack err_free: 361e5779998SDaniel Mack kfree(data); 362e5779998SDaniel Mack err: 363e5779998SDaniel Mack return ret; 364e5779998SDaniel Mack } 365e5779998SDaniel Mack 366e5779998SDaniel Mack /* 367e5779998SDaniel Mack * parse the format type I and III descriptors 368e5779998SDaniel Mack */ 369e5779998SDaniel Mack static int parse_audio_format_i(struct snd_usb_audio *chip, 3702fcdb06dSDaniel Mack struct audioformat *fp, unsigned int format, 3718f898e92SClemens Ladisch struct uac_format_type_i_continuous_descriptor *fmt) 372e5779998SDaniel Mack { 37374c34ca1SEldad Zack snd_pcm_format_t pcm_format; 37474c34ca1SEldad Zack int ret; 375e5779998SDaniel Mack 376e5779998SDaniel Mack if (fmt->bFormatType == UAC_FORMAT_TYPE_III) { 377e5779998SDaniel Mack /* FIXME: the format type is really IECxxx 378e5779998SDaniel Mack * but we give normal PCM format to get the existing 379e5779998SDaniel Mack * apps working... 380e5779998SDaniel Mack */ 381e5779998SDaniel Mack switch (chip->usb_id) { 382e5779998SDaniel Mack 383e5779998SDaniel Mack case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ 384e5779998SDaniel Mack if (chip->setup == 0x00 && 385e5779998SDaniel Mack fp->altsetting == 6) 386e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_BE; 387e5779998SDaniel Mack else 388e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 389e5779998SDaniel Mack break; 390e5779998SDaniel Mack default: 391e5779998SDaniel Mack pcm_format = SNDRV_PCM_FORMAT_S16_LE; 392e5779998SDaniel Mack } 39374c34ca1SEldad Zack fp->formats = pcm_format_to_bits(pcm_format); 394e5779998SDaniel Mack } else { 3958f898e92SClemens Ladisch fp->formats = parse_audio_format_i_type(chip, fp, format, fmt); 39629088fefSClemens Ladisch if (!fp->formats) 3978ad10dc6SSachin Kamat return -EINVAL; 398e5779998SDaniel Mack } 399e5779998SDaniel Mack 400e5779998SDaniel Mack /* gather possible sample rates */ 401e5779998SDaniel Mack /* audio class v1 reports possible sample rates as part of the 402e5779998SDaniel Mack * proprietary class specific descriptor. 403e5779998SDaniel Mack * audio class v2 uses class specific EP0 range requests for that. 404e5779998SDaniel Mack */ 4058f898e92SClemens Ladisch switch (fp->protocol) { 406a2acad82SClemens Ladisch default: 407e5779998SDaniel Mack case UAC_VERSION_1: 408e5779998SDaniel Mack fp->channels = fmt->bNrChannels; 40974754f97SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7); 410e5779998SDaniel Mack break; 411e5779998SDaniel Mack case UAC_VERSION_2: 412e5779998SDaniel Mack /* fp->channels is already set in this case */ 4133d8d4dcfSDaniel Mack ret = parse_audio_format_rates_v2(chip, fp); 414e5779998SDaniel Mack break; 415e5779998SDaniel Mack } 416e5779998SDaniel Mack 417e5779998SDaniel Mack if (fp->channels < 1) { 418*0ba41d91STakashi Iwai usb_audio_err(chip, 419*0ba41d91STakashi Iwai "%u:%d : invalid channels %d\n", 420*0ba41d91STakashi Iwai fp->iface, fp->altsetting, fp->channels); 4218ad10dc6SSachin Kamat return -EINVAL; 422e5779998SDaniel Mack } 423e5779998SDaniel Mack 424e5779998SDaniel Mack return ret; 425e5779998SDaniel Mack } 426e5779998SDaniel Mack 427e5779998SDaniel Mack /* 428e5779998SDaniel Mack * parse the format type II descriptor 429e5779998SDaniel Mack */ 430e5779998SDaniel Mack static int parse_audio_format_ii(struct snd_usb_audio *chip, 431e5779998SDaniel Mack struct audioformat *fp, 4328f898e92SClemens Ladisch int format, void *_fmt) 433e5779998SDaniel Mack { 434e5779998SDaniel Mack int brate, framesize, ret; 435e5779998SDaniel Mack 436e5779998SDaniel Mack switch (format) { 437e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_AC3: 438e5779998SDaniel Mack /* FIXME: there is no AC3 format defined yet */ 439015eb0b0SClemens Ladisch // fp->formats = SNDRV_PCM_FMTBIT_AC3; 440015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */ 441e5779998SDaniel Mack break; 442e5779998SDaniel Mack case UAC_FORMAT_TYPE_II_MPEG: 443015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 444e5779998SDaniel Mack break; 445e5779998SDaniel Mack default: 446*0ba41d91STakashi Iwai usb_audio_info(chip, 447*0ba41d91STakashi Iwai "%u:%d : unknown format tag %#x is detected. processed as MPEG.\n", 448*0ba41d91STakashi Iwai fp->iface, fp->altsetting, format); 449015eb0b0SClemens Ladisch fp->formats = SNDRV_PCM_FMTBIT_MPEG; 450e5779998SDaniel Mack break; 451e5779998SDaniel Mack } 452e5779998SDaniel Mack 453e5779998SDaniel Mack fp->channels = 1; 454e5779998SDaniel Mack 4558f898e92SClemens Ladisch switch (fp->protocol) { 456a2acad82SClemens Ladisch default: 457e5779998SDaniel Mack case UAC_VERSION_1: { 458e5779998SDaniel Mack struct uac_format_type_ii_discrete_descriptor *fmt = _fmt; 459e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 460e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 461*0ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 462e5779998SDaniel Mack fp->frame_size = framesize; 463e5779998SDaniel Mack ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */ 464e5779998SDaniel Mack break; 465e5779998SDaniel Mack } 466e5779998SDaniel Mack case UAC_VERSION_2: { 467e5779998SDaniel Mack struct uac_format_type_ii_ext_descriptor *fmt = _fmt; 468e5779998SDaniel Mack brate = le16_to_cpu(fmt->wMaxBitRate); 469e5779998SDaniel Mack framesize = le16_to_cpu(fmt->wSamplesPerFrame); 470*0ba41d91STakashi Iwai usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize); 471e5779998SDaniel Mack fp->frame_size = framesize; 4723d8d4dcfSDaniel Mack ret = parse_audio_format_rates_v2(chip, fp); 473e5779998SDaniel Mack break; 474e5779998SDaniel Mack } 475e5779998SDaniel Mack } 476e5779998SDaniel Mack 477e5779998SDaniel Mack return ret; 478e5779998SDaniel Mack } 479e5779998SDaniel Mack 4802fcdb06dSDaniel Mack int snd_usb_parse_audio_format(struct snd_usb_audio *chip, 4812fcdb06dSDaniel Mack struct audioformat *fp, unsigned int format, 4822fcdb06dSDaniel Mack struct uac_format_type_i_continuous_descriptor *fmt, 4838f898e92SClemens Ladisch int stream) 484e5779998SDaniel Mack { 485e5779998SDaniel Mack int err; 486e5779998SDaniel Mack 48774754f97SDaniel Mack switch (fmt->bFormatType) { 488e5779998SDaniel Mack case UAC_FORMAT_TYPE_I: 489e5779998SDaniel Mack case UAC_FORMAT_TYPE_III: 4908f898e92SClemens Ladisch err = parse_audio_format_i(chip, fp, format, fmt); 491e5779998SDaniel Mack break; 492e5779998SDaniel Mack case UAC_FORMAT_TYPE_II: 4938f898e92SClemens Ladisch err = parse_audio_format_ii(chip, fp, format, fmt); 494e5779998SDaniel Mack break; 495e5779998SDaniel Mack default: 496*0ba41d91STakashi Iwai usb_audio_info(chip, 497*0ba41d91STakashi Iwai "%u:%d : format type %d is not supported yet\n", 498*0ba41d91STakashi Iwai fp->iface, fp->altsetting, 49974754f97SDaniel Mack fmt->bFormatType); 5008d091242SDaniel Mack return -ENOTSUPP; 501e5779998SDaniel Mack } 50274754f97SDaniel Mack fp->fmt_type = fmt->bFormatType; 503e5779998SDaniel Mack if (err < 0) 504e5779998SDaniel Mack return err; 505e5779998SDaniel Mack #if 1 506e5779998SDaniel Mack /* FIXME: temporary hack for extigy/audigy 2 nx/zs */ 507e5779998SDaniel Mack /* extigy apparently supports sample rates other than 48k 508e5779998SDaniel Mack * but not in ordinary way. so we enable only 48k atm. 509e5779998SDaniel Mack */ 510e5779998SDaniel Mack if (chip->usb_id == USB_ID(0x041e, 0x3000) || 511e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3020) || 512e5779998SDaniel Mack chip->usb_id == USB_ID(0x041e, 0x3061)) { 51374754f97SDaniel Mack if (fmt->bFormatType == UAC_FORMAT_TYPE_I && 514e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_48000 && 515e5779998SDaniel Mack fp->rates != SNDRV_PCM_RATE_96000) 5168d091242SDaniel Mack return -ENOTSUPP; 517e5779998SDaniel Mack } 518e5779998SDaniel Mack #endif 519e5779998SDaniel Mack return 0; 520e5779998SDaniel Mack } 521e5779998SDaniel Mack 522