format.c (b3fdfc1b4b641d372e35ced98814289bc60bc5d1) format.c (0ba41d917eeb87f608cf147f870ff2f4c1056bab)
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of

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

69 format <<= 1;
70 break;
71 }
72 }
73
74 if ((pcm_formats == 0) &&
75 (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
76 /* some devices don't define this correctly... */
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of

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

69 format <<= 1;
70 break;
71 }
72 }
73
74 if ((pcm_formats == 0) &&
75 (format == 0 || format == (1 << UAC_FORMAT_TYPE_I_UNDEFINED))) {
76 /* some devices don't define this correctly... */
77 snd_printdd(KERN_INFO "%d:%u:%d : format type 0 is detected, processed as PCM\n",
78 chip->dev->devnum, fp->iface, fp->altsetting);
77 usb_audio_info(chip, "%u:%d : format type 0 is detected, processed as PCM\n",
78 fp->iface, fp->altsetting);
79 format = 1 << UAC_FORMAT_TYPE_I_PCM;
80 }
81 if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
82 if (chip->usb_id == USB_ID(0x0582, 0x0016) /* Edirol SD-90 */ &&
83 sample_width == 24 && sample_bytes == 2)
84 sample_bytes = 3;
85 else if (sample_width > sample_bytes * 8) {
79 format = 1 << UAC_FORMAT_TYPE_I_PCM;
80 }
81 if (format & (1 << UAC_FORMAT_TYPE_I_PCM)) {
82 if (chip->usb_id == USB_ID(0x0582, 0x0016) /* Edirol SD-90 */ &&
83 sample_width == 24 && sample_bytes == 2)
84 sample_bytes = 3;
85 else if (sample_width > sample_bytes * 8) {
86 snd_printk(KERN_INFO "%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
87 chip->dev->devnum, fp->iface, fp->altsetting,
88 sample_width, sample_bytes);
86 usb_audio_info(chip, "%u:%d : sample bitwidth %d in over sample bytes %d\n",
87 fp->iface, fp->altsetting,
88 sample_width, sample_bytes);
89 }
90 /* check the format byte size */
91 switch (sample_bytes) {
92 case 1:
93 pcm_formats |= SNDRV_PCM_FMTBIT_S8;
94 break;
95 case 2:
96 if (snd_usb_is_big_endian_format(chip, fp))

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

103 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
104 else
105 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
106 break;
107 case 4:
108 pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
109 break;
110 default:
89 }
90 /* check the format byte size */
91 switch (sample_bytes) {
92 case 1:
93 pcm_formats |= SNDRV_PCM_FMTBIT_S8;
94 break;
95 case 2:
96 if (snd_usb_is_big_endian_format(chip, fp))

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

103 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3BE; /* grrr, big endian!! */
104 else
105 pcm_formats |= SNDRV_PCM_FMTBIT_S24_3LE;
106 break;
107 case 4:
108 pcm_formats |= SNDRV_PCM_FMTBIT_S32_LE;
109 break;
110 default:
111 snd_printk(KERN_INFO "%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
112 chip->dev->devnum, fp->iface, fp->altsetting,
113 sample_width, sample_bytes);
111 usb_audio_info(chip,
112 "%u:%d : unsupported sample bitwidth %d in %d bytes\n",
113 fp->iface, fp->altsetting,
114 sample_width, sample_bytes);
114 break;
115 }
116 }
117 if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
118 /* Dallas DS4201 workaround: it advertises U8 format, but really
119 supports S8. */
120 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
121 pcm_formats |= SNDRV_PCM_FMTBIT_S8;

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

127 }
128 if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
129 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
130 }
131 if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
132 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
133 }
134 if (format & ~0x3f) {
115 break;
116 }
117 }
118 if (format & (1 << UAC_FORMAT_TYPE_I_PCM8)) {
119 /* Dallas DS4201 workaround: it advertises U8 format, but really
120 supports S8. */
121 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
122 pcm_formats |= SNDRV_PCM_FMTBIT_S8;

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

128 }
129 if (format & (1 << UAC_FORMAT_TYPE_I_ALAW)) {
130 pcm_formats |= SNDRV_PCM_FMTBIT_A_LAW;
131 }
132 if (format & (1 << UAC_FORMAT_TYPE_I_MULAW)) {
133 pcm_formats |= SNDRV_PCM_FMTBIT_MU_LAW;
134 }
135 if (format & ~0x3f) {
135 snd_printk(KERN_INFO "%d:%u:%d : unsupported format bits %#x\n",
136 chip->dev->devnum, fp->iface, fp->altsetting, format);
136 usb_audio_info(chip,
137 "%u:%d : unsupported format bits %#x\n",
138 fp->iface, fp->altsetting, format);
137 }
138
139 pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
140
141 return pcm_formats;
142}
143
144

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

153 * (7 for type I and II, 8 for type II)
154 */
155static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
156 unsigned char *fmt, int offset)
157{
158 int nr_rates = fmt[offset];
159
160 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
139 }
140
141 pcm_formats |= snd_usb_interface_dsd_format_quirks(chip, fp, sample_bytes);
142
143 return pcm_formats;
144}
145
146

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

155 * (7 for type I and II, 8 for type II)
156 */
157static int parse_audio_format_rates_v1(struct snd_usb_audio *chip, struct audioformat *fp,
158 unsigned char *fmt, int offset)
159{
160 int nr_rates = fmt[offset];
161
162 if (fmt[0] < offset + 1 + 3 * (nr_rates ? nr_rates : 2)) {
161 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
162 chip->dev->devnum, fp->iface, fp->altsetting);
163 usb_audio_err(chip,
164 "%u:%d : invalid UAC_FORMAT_TYPE desc\n",
165 fp->iface, fp->altsetting);
163 return -EINVAL;
164 }
165
166 if (nr_rates) {
167 /*
168 * build the rate table and bitmap flags
169 */
170 int r, idx;
171
172 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
173 if (fp->rate_table == NULL) {
166 return -EINVAL;
167 }
168
169 if (nr_rates) {
170 /*
171 * build the rate table and bitmap flags
172 */
173 int r, idx;
174
175 fp->rate_table = kmalloc(sizeof(int) * nr_rates, GFP_KERNEL);
176 if (fp->rate_table == NULL) {
174 snd_printk(KERN_ERR "cannot malloc\n");
177 usb_audio_err(chip, "cannot malloc\n");
175 return -ENOMEM;
176 }
177
178 fp->nr_rates = 0;
179 fp->rate_min = fp->rate_max = 0;
180 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
181 unsigned int rate = combine_triple(&fmt[idx]);
182 if (!rate)

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

217}
218
219/*
220 * Helper function to walk the array of sample rate triplets reported by
221 * the device. The problem is that we need to parse whole array first to
222 * get to know how many sample rates we have to expect.
223 * Then fp->rate_table can be allocated and filled.
224 */
178 return -ENOMEM;
179 }
180
181 fp->nr_rates = 0;
182 fp->rate_min = fp->rate_max = 0;
183 for (r = 0, idx = offset + 1; r < nr_rates; r++, idx += 3) {
184 unsigned int rate = combine_triple(&fmt[idx]);
185 if (!rate)

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

220}
221
222/*
223 * Helper function to walk the array of sample rate triplets reported by
224 * the device. The problem is that we need to parse whole array first to
225 * get to know how many sample rates we have to expect.
226 * Then fp->rate_table can be allocated and filled.
227 */
225static int parse_uac2_sample_rate_range(struct audioformat *fp, int nr_triplets,
228static int parse_uac2_sample_rate_range(struct snd_usb_audio *chip,
229 struct audioformat *fp, int nr_triplets,
226 const unsigned char *data)
227{
228 int i, nr_rates = 0;
229
230 fp->rates = fp->rate_min = fp->rate_max = 0;
231
232 for (i = 0; i < nr_triplets; i++) {
233 int min = combine_quad(&data[2 + 12 * i]);

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

256 if (!fp->rate_min || rate < fp->rate_min)
257 fp->rate_min = rate;
258 if (!fp->rate_max || rate > fp->rate_max)
259 fp->rate_max = rate;
260 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
261
262 nr_rates++;
263 if (nr_rates >= MAX_NR_RATES) {
230 const unsigned char *data)
231{
232 int i, nr_rates = 0;
233
234 fp->rates = fp->rate_min = fp->rate_max = 0;
235
236 for (i = 0; i < nr_triplets; i++) {
237 int min = combine_quad(&data[2 + 12 * i]);

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

260 if (!fp->rate_min || rate < fp->rate_min)
261 fp->rate_min = rate;
262 if (!fp->rate_max || rate > fp->rate_max)
263 fp->rate_max = rate;
264 fp->rates |= snd_pcm_rate_to_rate_bit(rate);
265
266 nr_rates++;
267 if (nr_rates >= MAX_NR_RATES) {
264 snd_printk(KERN_ERR "invalid uac2 rates\n");
268 usb_audio_err(chip, "invalid uac2 rates\n");
265 break;
266 }
267
268 /* avoid endless loop */
269 if (res == 0)
270 break;
271 }
272 }

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

282 struct audioformat *fp)
283{
284 struct usb_device *dev = chip->dev;
285 unsigned char tmp[2], *data;
286 int nr_triplets, data_size, ret = 0;
287 int clock = snd_usb_clock_find_source(chip, fp->clock, false);
288
289 if (clock < 0) {
269 break;
270 }
271
272 /* avoid endless loop */
273 if (res == 0)
274 break;
275 }
276 }

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

286 struct audioformat *fp)
287{
288 struct usb_device *dev = chip->dev;
289 unsigned char tmp[2], *data;
290 int nr_triplets, data_size, ret = 0;
291 int clock = snd_usb_clock_find_source(chip, fp->clock, false);
292
293 if (clock < 0) {
290 snd_printk(KERN_ERR "%s(): unable to find clock source (clock %d)\n",
294 dev_err(&dev->dev,
295 "%s(): unable to find clock source (clock %d)\n",
291 __func__, clock);
292 goto err;
293 }
294
295 /* get the number of sample rates first by only fetching 2 bytes */
296 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
297 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
298 UAC2_CS_CONTROL_SAM_FREQ << 8,
299 snd_usb_ctrl_intf(chip) | (clock << 8),
300 tmp, sizeof(tmp));
301
302 if (ret < 0) {
296 __func__, clock);
297 goto err;
298 }
299
300 /* get the number of sample rates first by only fetching 2 bytes */
301 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
302 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
303 UAC2_CS_CONTROL_SAM_FREQ << 8,
304 snd_usb_ctrl_intf(chip) | (clock << 8),
305 tmp, sizeof(tmp));
306
307 if (ret < 0) {
303 snd_printk(KERN_ERR "%s(): unable to retrieve number of sample rates (clock %d)\n",
308 dev_err(&dev->dev,
309 "%s(): unable to retrieve number of sample rates (clock %d)\n",
304 __func__, clock);
305 goto err;
306 }
307
308 nr_triplets = (tmp[1] << 8) | tmp[0];
309 data_size = 2 + 12 * nr_triplets;
310 data = kzalloc(data_size, GFP_KERNEL);
311 if (!data) {

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

316 /* now get the full information */
317 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
318 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
319 UAC2_CS_CONTROL_SAM_FREQ << 8,
320 snd_usb_ctrl_intf(chip) | (clock << 8),
321 data, data_size);
322
323 if (ret < 0) {
310 __func__, clock);
311 goto err;
312 }
313
314 nr_triplets = (tmp[1] << 8) | tmp[0];
315 data_size = 2 + 12 * nr_triplets;
316 data = kzalloc(data_size, GFP_KERNEL);
317 if (!data) {

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

322 /* now get the full information */
323 ret = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_RANGE,
324 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
325 UAC2_CS_CONTROL_SAM_FREQ << 8,
326 snd_usb_ctrl_intf(chip) | (clock << 8),
327 data, data_size);
328
329 if (ret < 0) {
324 snd_printk(KERN_ERR "%s(): unable to retrieve sample rate range (clock %d)\n",
330 dev_err(&dev->dev,
331 "%s(): unable to retrieve sample rate range (clock %d)\n",
325 __func__, clock);
326 ret = -EINVAL;
327 goto err_free;
328 }
329
330 /* Call the triplet parser, and make sure fp->rate_table is NULL.
331 * We just use the return value to know how many sample rates we
332 * will have to deal with. */
333 kfree(fp->rate_table);
334 fp->rate_table = NULL;
332 __func__, clock);
333 ret = -EINVAL;
334 goto err_free;
335 }
336
337 /* Call the triplet parser, and make sure fp->rate_table is NULL.
338 * We just use the return value to know how many sample rates we
339 * will have to deal with. */
340 kfree(fp->rate_table);
341 fp->rate_table = NULL;
335 fp->nr_rates = parse_uac2_sample_rate_range(fp, nr_triplets, data);
342 fp->nr_rates = parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
336
337 if (fp->nr_rates == 0) {
338 /* SNDRV_PCM_RATE_CONTINUOUS */
339 ret = 0;
340 goto err_free;
341 }
342
343 fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
344 if (!fp->rate_table) {
345 ret = -ENOMEM;
346 goto err_free;
347 }
348
349 /* Call the triplet parser again, but this time, fp->rate_table is
350 * allocated, so the rates will be stored */
343
344 if (fp->nr_rates == 0) {
345 /* SNDRV_PCM_RATE_CONTINUOUS */
346 ret = 0;
347 goto err_free;
348 }
349
350 fp->rate_table = kmalloc(sizeof(int) * fp->nr_rates, GFP_KERNEL);
351 if (!fp->rate_table) {
352 ret = -ENOMEM;
353 goto err_free;
354 }
355
356 /* Call the triplet parser again, but this time, fp->rate_table is
357 * allocated, so the rates will be stored */
351 parse_uac2_sample_rate_range(fp, nr_triplets, data);
358 parse_uac2_sample_rate_range(chip, fp, nr_triplets, data);
352
353err_free:
354 kfree(data);
355err:
356 return ret;
357}
358
359/*

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

403 break;
404 case UAC_VERSION_2:
405 /* fp->channels is already set in this case */
406 ret = parse_audio_format_rates_v2(chip, fp);
407 break;
408 }
409
410 if (fp->channels < 1) {
359
360err_free:
361 kfree(data);
362err:
363 return ret;
364}
365
366/*

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

410 break;
411 case UAC_VERSION_2:
412 /* fp->channels is already set in this case */
413 ret = parse_audio_format_rates_v2(chip, fp);
414 break;
415 }
416
417 if (fp->channels < 1) {
411 snd_printk(KERN_ERR "%d:%u:%d : invalid channels %d\n",
412 chip->dev->devnum, fp->iface, fp->altsetting, fp->channels);
418 usb_audio_err(chip,
419 "%u:%d : invalid channels %d\n",
420 fp->iface, fp->altsetting, fp->channels);
413 return -EINVAL;
414 }
415
416 return ret;
417}
418
419/*
420 * parse the format type II descriptor

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

430 /* FIXME: there is no AC3 format defined yet */
431 // fp->formats = SNDRV_PCM_FMTBIT_AC3;
432 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
433 break;
434 case UAC_FORMAT_TYPE_II_MPEG:
435 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
436 break;
437 default:
421 return -EINVAL;
422 }
423
424 return ret;
425}
426
427/*
428 * parse the format type II descriptor

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

438 /* FIXME: there is no AC3 format defined yet */
439 // fp->formats = SNDRV_PCM_FMTBIT_AC3;
440 fp->formats = SNDRV_PCM_FMTBIT_U8; /* temporary hack to receive byte streams */
441 break;
442 case UAC_FORMAT_TYPE_II_MPEG:
443 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
444 break;
445 default:
438 snd_printd(KERN_INFO "%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
439 chip->dev->devnum, fp->iface, fp->altsetting, format);
446 usb_audio_info(chip,
447 "%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
448 fp->iface, fp->altsetting, format);
440 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
441 break;
442 }
443
444 fp->channels = 1;
445
446 switch (fp->protocol) {
447 default:
448 case UAC_VERSION_1: {
449 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
450 brate = le16_to_cpu(fmt->wMaxBitRate);
451 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
449 fp->formats = SNDRV_PCM_FMTBIT_MPEG;
450 break;
451 }
452
453 fp->channels = 1;
454
455 switch (fp->protocol) {
456 default:
457 case UAC_VERSION_1: {
458 struct uac_format_type_ii_discrete_descriptor *fmt = _fmt;
459 brate = le16_to_cpu(fmt->wMaxBitRate);
460 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
452 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
461 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
453 fp->frame_size = framesize;
454 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
455 break;
456 }
457 case UAC_VERSION_2: {
458 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
459 brate = le16_to_cpu(fmt->wMaxBitRate);
460 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
462 fp->frame_size = framesize;
463 ret = parse_audio_format_rates_v1(chip, fp, _fmt, 8); /* fmt[8..] sample rates */
464 break;
465 }
466 case UAC_VERSION_2: {
467 struct uac_format_type_ii_ext_descriptor *fmt = _fmt;
468 brate = le16_to_cpu(fmt->wMaxBitRate);
469 framesize = le16_to_cpu(fmt->wSamplesPerFrame);
461 snd_printd(KERN_INFO "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
470 usb_audio_info(chip, "found format II with max.bitrate = %d, frame size=%d\n", brate, framesize);
462 fp->frame_size = framesize;
463 ret = parse_audio_format_rates_v2(chip, fp);
464 break;
465 }
466 }
467
468 return ret;
469}

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

479 case UAC_FORMAT_TYPE_I:
480 case UAC_FORMAT_TYPE_III:
481 err = parse_audio_format_i(chip, fp, format, fmt);
482 break;
483 case UAC_FORMAT_TYPE_II:
484 err = parse_audio_format_ii(chip, fp, format, fmt);
485 break;
486 default:
471 fp->frame_size = framesize;
472 ret = parse_audio_format_rates_v2(chip, fp);
473 break;
474 }
475 }
476
477 return ret;
478}

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

488 case UAC_FORMAT_TYPE_I:
489 case UAC_FORMAT_TYPE_III:
490 err = parse_audio_format_i(chip, fp, format, fmt);
491 break;
492 case UAC_FORMAT_TYPE_II:
493 err = parse_audio_format_ii(chip, fp, format, fmt);
494 break;
495 default:
487 snd_printd(KERN_INFO "%d:%u:%d : format type %d is not supported yet\n",
488 chip->dev->devnum, fp->iface, fp->altsetting,
489 fmt->bFormatType);
496 usb_audio_info(chip,
497 "%u:%d : format type %d is not supported yet\n",
498 fp->iface, fp->altsetting,
499 fmt->bFormatType);
490 return -ENOTSUPP;
491 }
492 fp->fmt_type = fmt->bFormatType;
493 if (err < 0)
494 return err;
495#if 1
496 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
497 /* extigy apparently supports sample rates other than 48k

--- 14 unchanged lines hidden ---
500 return -ENOTSUPP;
501 }
502 fp->fmt_type = fmt->bFormatType;
503 if (err < 0)
504 return err;
505#if 1
506 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
507 /* extigy apparently supports sample rates other than 48k

--- 14 unchanged lines hidden ---