Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  */
0004 
0005 #include <linux/init.h>
0006 #include <linux/slab.h>
0007 #include <linux/usb.h>
0008 #include <linux/usb/audio.h>
0009 #include <linux/usb/audio-v2.h>
0010 #include <linux/usb/audio-v3.h>
0011 
0012 #include <sound/core.h>
0013 #include <sound/pcm.h>
0014 
0015 #include "usbaudio.h"
0016 #include "card.h"
0017 #include "quirks.h"
0018 #include "helper.h"
0019 #include "clock.h"
0020 #include "format.h"
0021 
0022 /*
0023  * parse the audio format type I descriptor
0024  * and returns the corresponding pcm format
0025  *
0026  * @dev: usb device
0027  * @fp: audioformat record
0028  * @format: the format tag (wFormatTag)
0029  * @fmt: the format type descriptor (v1/v2) or AudioStreaming descriptor (v3)
0030  */
0031 static u64 parse_audio_format_i_type(struct snd_usb_audio *chip,
0032                      struct audioformat *fp,
0033                      u64 format, void *_fmt)
0034 {
0035     int sample_width, sample_bytes;
0036     u64 pcm_formats = 0;
0037 
0038     switch (fp->protocol) {
0039     case UAC_VERSION_1:
0040     default: {
0041         struct uac_format_type_i_discrete_descriptor *fmt = _fmt;
0042         if (format >= 64)
0043             return 0; /* invalid format */
0044         sample_width = fmt->bBitResolution;
0045         sample_bytes = fmt->bSubframeSize;
0046         format = 1ULL << format;
0047         break;
0048     }
0049 
0050     case UAC_VERSION_2: {
0051         struct uac_format_type_i_ext_descriptor *fmt = _fmt;
0052         sample_width = fmt->bBitResolution;
0053         sample_bytes = fmt->bSubslotSize;
0054 
0055         if (format & UAC2_FORMAT_TYPE_I_RAW_DATA) {
0056             pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
0057             /* flag potentially raw DSD capable altsettings */
0058             fp->dsd_raw = true;
0059         }
0060 
0061         format <<= 1;
0062         break;
0063     }
0064     case UAC_VERSION_3: {
0065         struct uac3_as_header_descriptor *as = _fmt;
0066 
0067         sample_width = as->bBitResolution;
0068         sample_bytes = as->bSubslotSize;
0069 
0070         if (format & UAC3_FORMAT_TYPE_I_RAW_DATA)
0071             pcm_formats |= SNDRV_PCM_FMTBIT_SPECIAL;
0072 
0073         format <<= 1;
0074         break;
0075     }
0076     }
0077 
0078     fp->fmt_bits = sample_width;
0079 
0080     if ((pcm_formats == 0) &&
0081         (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
0082         /* some devices don't define this correctly... */
0083         usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
0084             fp->iface, fp->altsetting);
0085         format = 1 << UAC_FORMAT_TYPE_I_PCM;
0086     }
0087     if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
0088         if (((chip->usb_id == USB_ID(0x0582, 0x0016)) ||
0089              /* Edirol SD-90 */
0090              (chip->usb_id == USB_ID(0x0582, 0x000c))) &&
0091              /* Roland SC-D70 */
0092             sample_width == 24 && sample_bytes == 2)
0093             sample_bytes = 3;
0094         else if (sample_width > sample_bytes * 8) {
0095             usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
0096                  fp->iface, fp->altsetting,
0097                  sample_width, sample_bytes);
0098         }
0099         /* check the format byte size */
0100         switch (sample_bytes) {
0101         case 1:
0102             pcm_formats |= SNDRV_PCM_FMTBIT_S8;
0103             break;
0104         case 2:
0105             if (snd_usb_is_big_endian_format(chip, fp))
0106                 pcm_formats |= SNDRV_PCM_FMTBIT_S16_BE; /* grrr, big endian!! */
0107             else
0108                 pcm_formats |= SNDRV_PCM_FMTBIT_S16_LE;
0109             break;
0110         case 3:
0111             if (snd_usb_is_big_endian_format(chip, fp))
0112                 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
0113             else
0114                 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
0115             break;
0116         case 4:
0117             pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
0118             break;
0119         default:
0120             usb_audio_info(chip,
0121                  "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
0122                  fp->iface, fp->altsetting,
0123                  sample_width, sample_bytes);
0124             break;
0125         }
0126     }
0127     if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
0128         /* Dallas DS4201 workaround: it advertises U8 format, but really
0129            supports S8. */
0130         if (chip->usb_id == USB_ID(0x04fa, 0x4201))
0131             pcm_formats |= SNDRV_PCM_FMTBIT_S8;
0132         else
0133             pcm_formats |= SNDRV_PCM_FMTBIT_U8;
0134     }
0135     if (format & (1 << UAC_FORMAT_TYPE_I_IEEE_FLOAT)) {
0136         pcm_formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
0137     }
0138     if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
0139         pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
0140     }
0141     if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
0142         pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
0143     }
0144     if (format & ~0x3f) {
0145         usb_audio_info(chip,
0146              "%u:%d : unsupported format bits %#llx\n",
0147              fp->iface, fp->altsetting, format);
0148     }
0149 
0150     pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
0151 
0152     return pcm_formats;
0153 }
0154 
0155 static int set_fixed_rate(struct audioformat *fp, int rate, int rate_bits)
0156 {
0157     kfree(fp->rate_table);
0158     fp->rate_table = kmalloc(sizeof(int), GFP_KERNEL);
0159     if (!fp->rate_table)
0160         return -ENOMEM;
0161     fp->nr_rates = 1;
0162     fp->rate_min = rate;
0163     fp->rate_max = rate;
0164     fp->rates = rate_bits;
0165     fp->rate_table[0] = rate;
0166     return 0;
0167 }
0168 
0169 /* set up rate_min, rate_max and rates from the rate table */
0170 static void set_rate_table_min_max(struct audioformat *fp)
0171 {
0172     unsigned int rate;
0173     int i;
0174 
0175     fp->rate_min = INT_MAX;
0176     fp->rate_max = 0;
0177     fp->rates = 0;
0178     for (i = 0; i < fp->nr_rates; i++) {
0179         rate = fp->rate_table[i];
0180         fp->rate_min = min(fp->rate_min, rate);
0181         fp->rate_max = max(fp->rate_max, rate);
0182         fp->rates |= snd_pcm_rate_to_rate_bit(rate);
0183     }
0184 }
0185 
0186 /*
0187  * parse the format descriptor and stores the possible sample rates
0188  * on the audioformat table (audio class v1).
0189  *
0190  * @dev: usb device
0191  * @fp: audioformat record
0192  * @fmt: the format descriptor
0193  * @offset: the start offset of descriptor pointing the rate type
0194  *          (7 for type I and II, 8 for type II)
0195  */
0196 static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
0197                        unsigned char *fmt, int offset)
0198 {
0199     int nr_rates = fmt[offset];
0200 
0201     if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
0202         usb_audio_err(chip,
0203             "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
0204             fp->iface, fp->altsetting);
0205         return -EINVAL;
0206     }
0207 
0208     if (nr_rates) {
0209         /*
0210          * build the rate table and bitmap flags
0211          */
0212         int r, idx;
0213 
0214         fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
0215                            GFP_KERNEL);
0216         if (fp->rate_table == NULL)
0217             return -ENOMEM;
0218 
0219         fp->nr_rates = 0;
0220         for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
0221             unsigned int rate = combine_triple(&fmt[idx]);
0222             if (!rate)
0223                 continue;
0224             /* C-Media CM6501 mislabels its 96 kHz altsetting */
0225             /* Terratec Aureon 7.1 USB C-Media 6206, too */
0226             /* Ozone Z90 USB C-Media, too */
0227             if (rate == 48000 && nr_rates == 1 &&
0228                 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
0229                  chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
0230                  chip->usb_id == USB_ID(0x0d8c, 0x0078) ||
0231                  chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
0232                 fp->altsetting == 5 && fp->maxpacksize == 392)
0233                 rate = 96000;
0234             /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
0235             if (rate == 16000 &&
0236                 (chip->usb_id == USB_ID(0x041e, 0x4064) ||
0237                  chip->usb_id == USB_ID(0x041e, 0x4068)))
0238                 rate = 8000;
0239 
0240             fp->rate_table[fp->nr_rates++] = rate;
0241         }
0242         if (!fp->nr_rates) {
0243             usb_audio_info(chip,
0244                        "%u:%d: All rates were zero\n",
0245                        fp->iface, fp->altsetting);
0246             return -EINVAL;
0247         }
0248         set_rate_table_min_max(fp);
0249     } else {
0250         /* continuous rates */
0251         fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
0252         fp->rate_min = combine_triple(&fmt[offset + 1]);
0253         fp->rate_max = combine_triple(&fmt[offset + 4]);
0254     }
0255 
0256     /* Jabra Evolve 65 headset */
0257     if (chip->usb_id == USB_ID(0x0b0e, 0x030b)) {
0258         /* only 48kHz for playback while keeping 16kHz for capture */
0259         if (fp->nr_rates != 1)
0260             return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
0261     }
0262 
0263     return 0;
0264 }
0265 
0266 
0267 /*
0268  * Presonus Studio 1810c supports a limited set of sampling
0269  * rates per altsetting but reports the full set each time.
0270  * If we don't filter out the unsupported rates and attempt
0271  * to configure the card, it will hang refusing to do any
0272  * further audio I/O until a hard reset is performed.
0273  *
0274  * The list of supported rates per altsetting (set of available
0275  * I/O channels) is described in the owner's manual, section 2.2.
0276  */
0277 static bool s1810c_valid_sample_rate(struct audioformat *fp,
0278                      unsigned int rate)
0279 {
0280     switch (fp->altsetting) {
0281     case 1:
0282         /* All ADAT ports available */
0283         return rate <= 48000;
0284     case 2:
0285         /* Half of ADAT ports available */
0286         return (rate == 88200 || rate == 96000);
0287     case 3:
0288         /* Analog I/O only (no S/PDIF nor ADAT) */
0289         return rate >= 176400;
0290     default:
0291         return false;
0292     }
0293     return false;
0294 }
0295 
0296 /*
0297  * Many Focusrite devices supports a limited set of sampling rates per
0298  * altsetting. Maximum rate is exposed in the last 4 bytes of Format Type
0299  * descriptor which has a non-standard bLength = 10.
0300  */
0301 static bool focusrite_valid_sample_rate(struct snd_usb_audio *chip,
0302                     struct audioformat *fp,
0303                     unsigned int rate)
0304 {
0305     struct usb_interface *iface;
0306     struct usb_host_interface *alts;
0307     unsigned char *fmt;
0308     unsigned int max_rate;
0309 
0310     iface = usb_ifnum_to_if(chip->dev, fp->iface);
0311     if (!iface)
0312         return true;
0313 
0314     alts = &iface->altsetting[fp->altset_idx];
0315     fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen,
0316                       NULL, UAC_FORMAT_TYPE);
0317     if (!fmt)
0318         return true;
0319 
0320     if (fmt[0] == 10) { /* bLength */
0321         max_rate = combine_quad(&fmt[6]);
0322 
0323         /* Validate max rate */
0324         if (max_rate != 48000 &&
0325             max_rate != 96000 &&
0326             max_rate != 192000 &&
0327             max_rate != 384000) {
0328 
0329             usb_audio_info(chip,
0330                 "%u:%d : unexpected max rate: %u\n",
0331                 fp->iface, fp->altsetting, max_rate);
0332 
0333             return true;
0334         }
0335 
0336         return rate <= max_rate;
0337     }
0338 
0339     return true;
0340 }
0341 
0342 /*
0343  * Helper function to walk the array of sample rate triplets reported by
0344  * the device. The problem is that we need to parse whole array first to
0345  * get to know how many sample rates we have to expect.
0346  * Then fp->rate_table can be allocated and filled.
0347  */
0348 static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
0349                     struct audioformat *fp, int nr_triplets,
0350                     const unsigned char *data)
0351 {
0352     int i, nr_rates = 0;
0353 
0354     for (i = 0; i < nr_triplets; i++) {
0355         int min = combine_quad(&data[2 + 12 * i]);
0356         int max = combine_quad(&data[6 + 12 * i]);
0357         int res = combine_quad(&data[10 + 12 * i]);
0358         unsigned int rate;
0359 
0360         if ((max < 0) || (min < 0) || (res < 0) || (max < min))
0361             continue;
0362 
0363         /*
0364          * for ranges with res == 1, we announce a continuous sample
0365          * rate range, and this function should return 0 for no further
0366          * parsing.
0367          */
0368         if (res == 1) {
0369             fp->rate_min = min;
0370             fp->rate_max = max;
0371             fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
0372             return 0;
0373         }
0374 
0375         for (rate = min; rate <= max; rate += res) {
0376 
0377             /* Filter out invalid rates on Presonus Studio 1810c */
0378             if (chip->usb_id == USB_ID(0x194f, 0x010c) &&
0379                 !s1810c_valid_sample_rate(fp, rate))
0380                 goto skip_rate;
0381 
0382             /* Filter out invalid rates on Focusrite devices */
0383             if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
0384                 !focusrite_valid_sample_rate(chip, fp, rate))
0385                 goto skip_rate;
0386 
0387             if (fp->rate_table)
0388                 fp->rate_table[nr_rates] = rate;
0389             nr_rates++;
0390             if (nr_rates >= MAX_NR_RATES) {
0391                 usb_audio_err(chip, "invalid uac2 rates\n");
0392                 break;
0393             }
0394 
0395 skip_rate:
0396             /* avoid endless loop */
0397             if (res == 0)
0398                 break;
0399         }
0400     }
0401 
0402     return nr_rates;
0403 }
0404 
0405 /* Line6 Helix series and the Rode Rodecaster Pro don't support the
0406  * UAC2_CS_RANGE usb function call. Return a static table of known
0407  * clock rates.
0408  */
0409 static int line6_parse_audio_format_rates_quirk(struct snd_usb_audio *chip,
0410                         struct audioformat *fp)
0411 {
0412     switch (chip->usb_id) {
0413     case USB_ID(0x0e41, 0x4241): /* Line6 Helix */
0414     case USB_ID(0x0e41, 0x4242): /* Line6 Helix Rack */
0415     case USB_ID(0x0e41, 0x4244): /* Line6 Helix LT */
0416     case USB_ID(0x0e41, 0x4246): /* Line6 HX-Stomp */
0417     case USB_ID(0x0e41, 0x4253): /* Line6 HX-Stomp XL */
0418     case USB_ID(0x0e41, 0x4247): /* Line6 Pod Go */
0419     case USB_ID(0x0e41, 0x4248): /* Line6 Helix >= fw 2.82 */
0420     case USB_ID(0x0e41, 0x4249): /* Line6 Helix Rack >= fw 2.82 */
0421     case USB_ID(0x0e41, 0x424a): /* Line6 Helix LT >= fw 2.82 */
0422     case USB_ID(0x19f7, 0x0011): /* Rode Rodecaster Pro */
0423         return set_fixed_rate(fp, 48000, SNDRV_PCM_RATE_48000);
0424     }
0425 
0426     return -ENODEV;
0427 }
0428 
0429 /* check whether the given altsetting is supported for the already set rate */
0430 static bool check_valid_altsetting_v2v3(struct snd_usb_audio *chip, int iface,
0431                     int altsetting)
0432 {
0433     struct usb_device *dev = chip->dev;
0434     __le64 raw_data = 0;
0435     u64 data;
0436     int err;
0437 
0438     /* we assume 64bit is enough for any altsettings */
0439     if (snd_BUG_ON(altsetting >= 64 - 8))
0440         return false;
0441 
0442     err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
0443                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0444                   UAC2_AS_VAL_ALT_SETTINGS << 8,
0445                   iface, &raw_data, sizeof(raw_data));
0446     if (err < 0)
0447         return false;
0448 
0449     data = le64_to_cpu(raw_data);
0450     /* first byte contains the bitmap size */
0451     if ((data & 0xff) * 8 < altsetting)
0452         return false;
0453     if (data & (1ULL << (altsetting + 8)))
0454         return true;
0455 
0456     return false;
0457 }
0458 
0459 /*
0460  * Validate each sample rate with the altsetting
0461  * Rebuild the rate table if only partial values are valid
0462  */
0463 static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
0464                        struct audioformat *fp,
0465                        int clock)
0466 {
0467     struct usb_device *dev = chip->dev;
0468     unsigned int *table;
0469     unsigned int nr_rates;
0470     int i, err;
0471 
0472     /* performing the rate verification may lead to unexpected USB bus
0473      * behavior afterwards by some unknown reason.  Do this only for the
0474      * known devices.
0475      */
0476     if (!(chip->quirk_flags & QUIRK_FLAG_VALIDATE_RATES))
0477         return 0; /* don't perform the validation as default */
0478 
0479     table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
0480     if (!table)
0481         return -ENOMEM;
0482 
0483     /* clear the interface altsetting at first */
0484     usb_set_interface(dev, fp->iface, 0);
0485 
0486     nr_rates = 0;
0487     for (i = 0; i < fp->nr_rates; i++) {
0488         err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
0489                            fp->rate_table[i]);
0490         if (err < 0)
0491             continue;
0492 
0493         if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
0494             table[nr_rates++] = fp->rate_table[i];
0495     }
0496 
0497     if (!nr_rates) {
0498         usb_audio_dbg(chip,
0499                   "No valid sample rate available for %d:%d, assuming a firmware bug\n",
0500                   fp->iface, fp->altsetting);
0501         nr_rates = fp->nr_rates; /* continue as is */
0502     }
0503 
0504     if (fp->nr_rates == nr_rates) {
0505         kfree(table);
0506         return 0;
0507     }
0508 
0509     kfree(fp->rate_table);
0510     fp->rate_table = table;
0511     fp->nr_rates = nr_rates;
0512     return 0;
0513 }
0514 
0515 /*
0516  * parse the format descriptor and stores the possible sample rates
0517  * on the audioformat table (audio class v2 and v3).
0518  */
0519 static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,
0520                        struct audioformat *fp)
0521 {
0522     struct usb_device *dev = chip->dev;
0523     unsigned char tmp[2], *data;
0524     int nr_triplets, data_size, ret = 0, ret_l6;
0525     int clock = snd_usb_clock_find_source(chip, fp, false);
0526 
0527     if (clock < 0) {
0528         dev_err(&dev->dev,
0529             "%s(): unable to find clock source (clock %d)\n",
0530                 __func__, clock);
0531         goto err;
0532     }
0533 
0534     /* get the number of sample rates first by only fetching 2 bytes */
0535     ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
0536                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0537                   UAC2_CS_CONTROL_SAM_FREQ << 8,
0538                   snd_usb_ctrl_intf(chip) | (clock << 8),
0539                   tmp, sizeof(tmp));
0540 
0541     if (ret < 0) {
0542         /* line6 helix devices don't support UAC2_CS_CONTROL_SAM_FREQ call */
0543         ret_l6 = line6_parse_audio_format_rates_quirk(chip, fp);
0544         if (ret_l6 == -ENODEV) {
0545             /* no line6 device found continue showing the error */
0546             dev_err(&dev->dev,
0547                 "%s(): unable to retrieve number of sample rates (clock %d)\n",
0548                 __func__, clock);
0549             goto err;
0550         }
0551         if (ret_l6 == 0) {
0552             dev_info(&dev->dev,
0553                 "%s(): unable to retrieve number of sample rates: set it to a predefined value (clock %d).\n",
0554                 __func__, clock);
0555             return 0;
0556         }
0557         ret = ret_l6;
0558         goto err;
0559     }
0560 
0561     nr_triplets = (tmp[1] << 8) | tmp[0];
0562     data_size = 2 + 12 * nr_triplets;
0563     data = kzalloc(data_size, GFP_KERNEL);
0564     if (!data) {
0565         ret = -ENOMEM;
0566         goto err;
0567     }
0568 
0569     /* now get the full information */
0570     ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
0571                   USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
0572                   UAC2_CS_CONTROL_SAM_FREQ << 8,
0573                   snd_usb_ctrl_intf(chip) | (clock << 8),
0574                   data, data_size);
0575 
0576     if (ret < 0) {
0577         dev_err(&dev->dev,
0578             "%s(): unable to retrieve sample rate range (clock %d)\n",
0579                 __func__, clock);
0580         ret = -EINVAL;
0581         goto err_free;
0582     }
0583 
0584     /* Call the triplet parser, and make sure fp->rate_table is NULL.
0585      * We just use the return value to know how many sample rates we
0586      * will have to deal with. */
0587     kfree(fp->rate_table);
0588     fp->rate_table = NULL;
0589     fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
0590 
0591     if (fp->nr_rates == 0) {
0592         /* SNDRV_PCM_RATE_CONTINUOUS */
0593         ret = 0;
0594         goto err_free;
0595     }
0596 
0597     fp->rate_table = kmalloc_array(fp->nr_rates, sizeof(int), GFP_KERNEL);
0598     if (!fp->rate_table) {
0599         ret = -ENOMEM;
0600         goto err_free;
0601     }
0602 
0603     /* Call the triplet parser again, but this time, fp->rate_table is
0604      * allocated, so the rates will be stored */
0605     parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
0606 
0607     ret = validate_sample_rate_table_v2v3(chip, fp, clock);
0608     if (ret < 0)
0609         goto err_free;
0610 
0611     set_rate_table_min_max(fp);
0612 
0613 err_free:
0614     kfree(data);
0615 err:
0616     return ret;
0617 }
0618 
0619 /*
0620  * parse the format type I and III descriptors
0621  */
0622 static int parse_audio_format_i(struct snd_usb_audio *chip,
0623                 struct audioformat *fp, u64 format,
0624                 void *_fmt)
0625 {
0626     snd_pcm_format_t pcm_format;
0627     unsigned int fmt_type;
0628     int ret;
0629 
0630     switch (fp->protocol) {
0631     default:
0632     case UAC_VERSION_1:
0633     case UAC_VERSION_2: {
0634         struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
0635 
0636         fmt_type = fmt->bFormatType;
0637         break;
0638     }
0639     case UAC_VERSION_3: {
0640         /* fp->fmt_type is already set in this case */
0641         fmt_type = fp->fmt_type;
0642         break;
0643     }
0644     }
0645 
0646     if (fmt_type == UAC_FORMAT_TYPE_III) {
0647         /* FIXME: the format type is really IECxxx
0648          *        but we give normal PCM format to get the existing
0649          *        apps working...
0650          */
0651         switch (chip->usb_id) {
0652 
0653         case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
0654             if (chip->setup == 0x00 && 
0655                 fp->altsetting == 6)
0656                 pcm_format = SNDRV_PCM_FORMAT_S16_BE;
0657             else
0658                 pcm_format = SNDRV_PCM_FORMAT_S16_LE;
0659             break;
0660         default:
0661             pcm_format = SNDRV_PCM_FORMAT_S16_LE;
0662         }
0663         fp->formats = pcm_format_to_bits(pcm_format);
0664     } else {
0665         fp->formats = parse_audio_format_i_type(chip, fp, format, _fmt);
0666         if (!fp->formats)
0667             return -EINVAL;
0668     }
0669 
0670     /* gather possible sample rates */
0671     /* audio class v1 reports possible sample rates as part of the
0672      * proprietary class specific descriptor.
0673      * audio class v2 uses class specific EP0 range requests for that.
0674      */
0675     switch (fp->protocol) {
0676     default:
0677     case UAC_VERSION_1: {
0678         struct uac_format_type_i_continuous_descriptor *fmt = _fmt;
0679 
0680         fp->channels = fmt->bNrChannels;
0681         ret = parse_audio_format_rates_v1(chip, fp, (unsigned char *) fmt, 7);
0682         break;
0683     }
0684     case UAC_VERSION_2:
0685     case UAC_VERSION_3: {
0686         /* fp->channels is already set in this case */
0687         ret = parse_audio_format_rates_v2v3(chip, fp);
0688         break;
0689     }
0690     }
0691 
0692     if (fp->channels < 1) {
0693         usb_audio_err(chip,
0694             "%u:%d : invalid channels %d\n",
0695             fp->iface, fp->altsetting, fp->channels);
0696         return -EINVAL;
0697     }
0698 
0699     return ret;
0700 }
0701 
0702 /*
0703  * parse the format type II descriptor
0704  */
0705 static int parse_audio_format_ii(struct snd_usb_audio *chip,
0706                  struct audioformat *fp,
0707                  u64 format, void *_fmt)
0708 {
0709     int brate, framesize, ret;
0710 
0711     switch (format) {
0712     case UAC_FORMAT_TYPE_II_AC3:
0713         /* FIXME: there is no AC3 format defined yet */
0714         // fp->formats = SNDRV_PCM_FMTBIT_AC3;
0715         fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
0716         break;
0717     case UAC_FORMAT_TYPE_II_MPEG:
0718         fp->formats = SNDRV_PCM_FMTBIT_MPEG;
0719         break;
0720     default:
0721         usb_audio_info(chip,
0722              "%u:%d : unknown format tag %#llx is detected.  processed as MPEG.\n",
0723              fp->iface, fp->altsetting, format);
0724         fp->formats = SNDRV_PCM_FMTBIT_MPEG;
0725         break;
0726     }
0727 
0728     fp->channels = 1;
0729 
0730     switch (fp->protocol) {
0731     default:
0732     case UAC_VERSION_1: {
0733         struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
0734         brate = le16_to_cpu(fmt->wMaxBitRate);
0735         framesize = le16_to_cpu(fmt->wSamplesPerFrame);
0736         usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
0737         fp->frame_size = framesize;
0738         ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
0739         break;
0740     }
0741     case UAC_VERSION_2: {
0742         struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
0743         brate = le16_to_cpu(fmt->wMaxBitRate);
0744         framesize = le16_to_cpu(fmt->wSamplesPerFrame);
0745         usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
0746         fp->frame_size = framesize;
0747         ret = parse_audio_format_rates_v2v3(chip, fp);
0748         break;
0749     }
0750     }
0751 
0752     return ret;
0753 }
0754 
0755 int snd_usb_parse_audio_format(struct snd_usb_audio *chip,
0756                    struct audioformat *fp, u64 format,
0757                    struct uac_format_type_i_continuous_descriptor *fmt,
0758                    int stream)
0759 {
0760     int err;
0761 
0762     switch (fmt->bFormatType) {
0763     case UAC_FORMAT_TYPE_I:
0764     case UAC_FORMAT_TYPE_III:
0765         err = parse_audio_format_i(chip, fp, format, fmt);
0766         break;
0767     case UAC_FORMAT_TYPE_II:
0768         err = parse_audio_format_ii(chip, fp, format, fmt);
0769         break;
0770     default:
0771         usb_audio_info(chip,
0772              "%u:%d : format type %d is not supported yet\n",
0773              fp->iface, fp->altsetting,
0774              fmt->bFormatType);
0775         return -ENOTSUPP;
0776     }
0777     fp->fmt_type = fmt->bFormatType;
0778     if (err < 0)
0779         return err;
0780 #if 1
0781     /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
0782     /* extigy apparently supports sample rates other than 48k
0783      * but not in ordinary way.  so we enable only 48k atm.
0784      */
0785     if (chip->usb_id == USB_ID(0x041e, 0x3000) ||
0786         chip->usb_id == USB_ID(0x041e, 0x3020) ||
0787         chip->usb_id == USB_ID(0x041e, 0x3061)) {
0788         if (fmt->bFormatType == UAC_FORMAT_TYPE_I &&
0789             fp->rates != SNDRV_PCM_RATE_48000 &&
0790             fp->rates != SNDRV_PCM_RATE_96000)
0791             return -ENOTSUPP;
0792     }
0793 #endif
0794     return 0;
0795 }
0796 
0797 int snd_usb_parse_audio_format_v3(struct snd_usb_audio *chip,
0798                    struct audioformat *fp,
0799                    struct uac3_as_header_descriptor *as,
0800                    int stream)
0801 {
0802     u64 format = le64_to_cpu(as->bmFormats);
0803     int err;
0804 
0805     /*
0806      * Type I format bits are D0..D6
0807      * This test works because type IV is not supported
0808      */
0809     if (format & 0x7f)
0810         fp->fmt_type = UAC_FORMAT_TYPE_I;
0811     else
0812         fp->fmt_type = UAC_FORMAT_TYPE_III;
0813 
0814     err = parse_audio_format_i(chip, fp, format, as);
0815     if (err < 0)
0816         return err;
0817 
0818     return 0;
0819 }