format.c (7ec827b9465c427511f05bab02dddbcfdd4ecc53) format.c (13ee03361fc5c9284067f4bfaaea4ebadbd874cf)
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 */
4
5#include <linux/init.h>
6#include <linux/slab.h>
7#include <linux/usb.h>
8#include <linux/usb/audio.h>

--- 150 unchanged lines hidden (view full) ---

159 fp->nr_rates = 1;
160 fp->rate_min = rate;
161 fp->rate_max = rate;
162 fp->rates = rate_bits;
163 fp->rate_table[0] = rate;
164 return 0;
165}
166
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 */
4
5#include <linux/init.h>
6#include <linux/slab.h>
7#include <linux/usb.h>
8#include <linux/usb/audio.h>

--- 150 unchanged lines hidden (view full) ---

159 fp->nr_rates = 1;
160 fp->rate_min = rate;
161 fp->rate_max = rate;
162 fp->rates = rate_bits;
163 fp->rate_table[0] = rate;
164 return 0;
165}
166
167/* set up rate_min, rate_max and rates from the rate table */
168static void set_rate_table_min_max(struct audioformat *fp)
169{
170 unsigned int rate;
171 int i;
172
173 fp->rate_min = INT_MAX;
174 fp->rate_max = 0;
175 fp->rates = 0;
176 for (i = 0; i < fp->nr_rates; i++) {
177 rate = fp->rate_table[i];
178 fp->rate_min = min(fp->rate_min, rate);
179 fp->rate_max = max(fp->rate_max, rate);
180 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
181 }
182}
183
167/*
168 * parse the format descriptor and stores the possible sample rates
169 * on the audioformat table (audio class v1).
170 *
171 * @dev: usb device
172 * @fp: audioformat record
173 * @fmt: the format descriptor
174 * @offset: the start offset of descriptor pointing the rate type

--- 18 unchanged lines hidden (view full) ---

193 int r, idx;
194
195 fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
196 GFP_KERNEL);
197 if (fp->rate_table == NULL)
198 return -ENOMEM;
199
200 fp->nr_rates = 0;
184/*
185 * parse the format descriptor and stores the possible sample rates
186 * on the audioformat table (audio class v1).
187 *
188 * @dev: usb device
189 * @fp: audioformat record
190 * @fmt: the format descriptor
191 * @offset: the start offset of descriptor pointing the rate type

--- 18 unchanged lines hidden (view full) ---

210 int r, idx;
211
212 fp->rate_table = kmalloc_array(nr_rates, sizeof(int),
213 GFP_KERNEL);
214 if (fp->rate_table == NULL)
215 return -ENOMEM;
216
217 fp->nr_rates = 0;
201 fp->rate_min = fp->rate_max = 0;
202 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
203 unsigned int rate = combine_triple(&fmt[idx]);
204 if (!rate)
205 continue;
206 /* C-Media CM6501 mislabels its 96 kHz altsetting */
207 /* Terratec Aureon 7.1 USB C-Media 6206, too */
208 if (rate == 48000 && nr_rates == 1 &&
209 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
210 chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
211 chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
212 fp->altsetting == 5 && fp->maxpacksize == 392)
213 rate = 96000;
214 /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
215 if (rate == 16000 &&
216 (chip->usb_id == USB_ID(0x041e, 0x4064) ||
217 chip->usb_id == USB_ID(0x041e, 0x4068)))
218 rate = 8000;
219
218 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
219 unsigned int rate = combine_triple(&fmt[idx]);
220 if (!rate)
221 continue;
222 /* C-Media CM6501 mislabels its 96 kHz altsetting */
223 /* Terratec Aureon 7.1 USB C-Media 6206, too */
224 if (rate == 48000 && nr_rates == 1 &&
225 (chip->usb_id == USB_ID(0x0d8c, 0x0201) ||
226 chip->usb_id == USB_ID(0x0d8c, 0x0102) ||
227 chip->usb_id == USB_ID(0x0ccd, 0x00b1)) &&
228 fp->altsetting == 5 && fp->maxpacksize == 392)
229 rate = 96000;
230 /* Creative VF0420/VF0470 Live Cams report 16 kHz instead of 8kHz */
231 if (rate == 16000 &&
232 (chip->usb_id == USB_ID(0x041e, 0x4064) ||
233 chip->usb_id == USB_ID(0x041e, 0x4068)))
234 rate = 8000;
235
220 fp->rate_table[fp->nr_rates] = rate;
221 if (!fp->rate_min || rate < fp->rate_min)
222 fp->rate_min = rate;
223 if (!fp->rate_max || rate > fp->rate_max)
224 fp->rate_max = rate;
225 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
226 fp->nr_rates++;
236 fp->rate_table[fp->nr_rates++] = rate;
227 }
228 if (!fp->nr_rates) {
229 usb_audio_info(chip,
230 "%u:%d: All rates were zero\n",
231 fp->iface, fp->altsetting);
232 return -EINVAL;
233 }
237 }
238 if (!fp->nr_rates) {
239 usb_audio_info(chip,
240 "%u:%d: All rates were zero\n",
241 fp->iface, fp->altsetting);
242 return -EINVAL;
243 }
244 set_rate_table_min_max(fp);
234 } else {
235 /* continuous rates */
236 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
237 fp->rate_min = combine_triple(&fmt[offset + 1]);
238 fp->rate_max = combine_triple(&fmt[offset + 4]);
239 }
240
241 /* Jabra Evolve 65 headset */

--- 89 unchanged lines hidden (view full) ---

331 * Then fp->rate_table can be allocated and filled.
332 */
333static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
334 struct audioformat *fp, int nr_triplets,
335 const unsigned char *data)
336{
337 int i, nr_rates = 0;
338
245 } else {
246 /* continuous rates */
247 fp->rates = SNDRV_PCM_RATE_CONTINUOUS;
248 fp->rate_min = combine_triple(&fmt[offset + 1]);
249 fp->rate_max = combine_triple(&fmt[offset + 4]);
250 }
251
252 /* Jabra Evolve 65 headset */

--- 89 unchanged lines hidden (view full) ---

342 * Then fp->rate_table can be allocated and filled.
343 */
344static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
345 struct audioformat *fp, int nr_triplets,
346 const unsigned char *data)
347{
348 int i, nr_rates = 0;
349
339 fp->rates = fp->rate_min = fp->rate_max = 0;
340
341 for (i = 0; i < nr_triplets; i++) {
342 int min = combine_quad(&data[2 + 12 * i]);
343 int max = combine_quad(&data[6 + 12 * i]);
344 int res = combine_quad(&data[10 + 12 * i]);
345 unsigned int rate;
346
347 if ((max < 0) || (min < 0) || (res < 0) || (max < min))
348 continue;

--- 19 unchanged lines hidden (view full) ---

368
369 /* Filter out invalid rates on Focusrite devices */
370 if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
371 !focusrite_valid_sample_rate(chip, fp, rate))
372 goto skip_rate;
373
374 if (fp->rate_table)
375 fp->rate_table[nr_rates] = rate;
350 for (i = 0; i < nr_triplets; i++) {
351 int min = combine_quad(&data[2 + 12 * i]);
352 int max = combine_quad(&data[6 + 12 * i]);
353 int res = combine_quad(&data[10 + 12 * i]);
354 unsigned int rate;
355
356 if ((max < 0) || (min < 0) || (res < 0) || (max < min))
357 continue;

--- 19 unchanged lines hidden (view full) ---

377
378 /* Filter out invalid rates on Focusrite devices */
379 if (USB_ID_VENDOR(chip->usb_id) == 0x1235 &&
380 !focusrite_valid_sample_rate(chip, fp, rate))
381 goto skip_rate;
382
383 if (fp->rate_table)
384 fp->rate_table[nr_rates] = rate;
376 if (!fp->rate_min || rate < fp->rate_min)
377 fp->rate_min = rate;
378 if (!fp->rate_max || rate > fp->rate_max)
379 fp->rate_max = rate;
380 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
381
382 nr_rates++;
383 if (nr_rates >= MAX_NR_RATES) {
384 usb_audio_err(chip, "invalid uac2 rates\n");
385 break;
386 }
387
388skip_rate:
389 /* avoid endless loop */

--- 64 unchanged lines hidden (view full) ---

454 */
455static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
456 struct audioformat *fp,
457 int clock)
458{
459 struct usb_device *dev = chip->dev;
460 unsigned int *table;
461 unsigned int nr_rates;
385 nr_rates++;
386 if (nr_rates >= MAX_NR_RATES) {
387 usb_audio_err(chip, "invalid uac2 rates\n");
388 break;
389 }
390
391skip_rate:
392 /* avoid endless loop */

--- 64 unchanged lines hidden (view full) ---

457 */
458static int validate_sample_rate_table_v2v3(struct snd_usb_audio *chip,
459 struct audioformat *fp,
460 int clock)
461{
462 struct usb_device *dev = chip->dev;
463 unsigned int *table;
464 unsigned int nr_rates;
462 unsigned int rate_min = 0x7fffffff;
463 unsigned int rate_max = 0;
464 unsigned int rates = 0;
465 int i, err;
466
467 table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
468 if (!table)
469 return -ENOMEM;
470
471 /* clear the interface altsetting at first */
472 usb_set_interface(dev, fp->iface, 0);
473
474 nr_rates = 0;
475 for (i = 0; i < fp->nr_rates; i++) {
476 err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
477 fp->rate_table[i]);
478 if (err < 0)
479 continue;
480
465 int i, err;
466
467 table = kcalloc(fp->nr_rates, sizeof(*table), GFP_KERNEL);
468 if (!table)
469 return -ENOMEM;
470
471 /* clear the interface altsetting at first */
472 usb_set_interface(dev, fp->iface, 0);
473
474 nr_rates = 0;
475 for (i = 0; i < fp->nr_rates; i++) {
476 err = snd_usb_set_sample_rate_v2v3(chip, fp, clock,
477 fp->rate_table[i]);
478 if (err < 0)
479 continue;
480
481 if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting)) {
481 if (check_valid_altsetting_v2v3(chip, fp->iface, fp->altsetting))
482 table[nr_rates++] = fp->rate_table[i];
482 table[nr_rates++] = fp->rate_table[i];
483 if (rate_min > fp->rate_table[i])
484 rate_min = fp->rate_table[i];
485 if (rate_max < fp->rate_table[i])
486 rate_max = fp->rate_table[i];
487 rates |= snd_pcm_rate_to_rate_bit(fp->rate_table[i]);
488 }
489 }
490
491 if (!nr_rates) {
492 usb_audio_dbg(chip,
493 "No valid sample rate available for %d:%d, assuming a firmware bug\n",
494 fp->iface, fp->altsetting);
495 nr_rates = fp->nr_rates; /* continue as is */
496 }
497
498 if (fp->nr_rates == nr_rates) {
499 kfree(table);
500 return 0;
501 }
502
503 kfree(fp->rate_table);
504 fp->rate_table = table;
505 fp->nr_rates = nr_rates;
483 }
484
485 if (!nr_rates) {
486 usb_audio_dbg(chip,
487 "No valid sample rate available for %d:%d, assuming a firmware bug\n",
488 fp->iface, fp->altsetting);
489 nr_rates = fp->nr_rates; /* continue as is */
490 }
491
492 if (fp->nr_rates == nr_rates) {
493 kfree(table);
494 return 0;
495 }
496
497 kfree(fp->rate_table);
498 fp->rate_table = table;
499 fp->nr_rates = nr_rates;
506 fp->rate_min = rate_min;
507 fp->rate_max = rate_max;
508 fp->rates = rates;
509 return 0;
510}
511
512/*
513 * parse the format descriptor and stores the possible sample rates
514 * on the audioformat table (audio class v2 and v3).
515 */
516static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,

--- 80 unchanged lines hidden (view full) ---

597 goto err_free;
598 }
599
600 /* Call the triplet parser again, but this time, fp->rate_table is
601 * allocated, so the rates will be stored */
602 parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
603
604 ret = validate_sample_rate_table_v2v3(chip, fp, clock);
500 return 0;
501}
502
503/*
504 * parse the format descriptor and stores the possible sample rates
505 * on the audioformat table (audio class v2 and v3).
506 */
507static int parse_audio_format_rates_v2v3(struct snd_usb_audio *chip,

--- 80 unchanged lines hidden (view full) ---

588 goto err_free;
589 }
590
591 /* Call the triplet parser again, but this time, fp->rate_table is
592 * allocated, so the rates will be stored */
593 parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
594
595 ret = validate_sample_rate_table_v2v3(chip, fp, clock);
596 if (ret < 0)
597 goto err_free;
605
598
599 set_rate_table_min_max(fp);
600
606err_free:
607 kfree(data);
608err:
609 return ret;
610}
611
612/*
613 * parse the format type I and III descriptors

--- 199 unchanged lines hidden ---
601err_free:
602 kfree(data);
603err:
604 return ret;
605}
606
607/*
608 * parse the format type I and III descriptors

--- 199 unchanged lines hidden ---