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.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <linux/init.h>
19 #include <linux/usb.h>
20 #include <linux/usb/audio.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
32 * parse the audio format type I descriptor
33 * and returns the corresponding pcm format
36 * @fp: audioformat record
37 * @format: the format tag (wFormatTag)
38 * @fmt: the format type descriptor
40 static int parse_audio_format_i_type(struct snd_usb_audio
*chip
,
41 struct audioformat
*fp
,
42 int format
, void *_fmt
,
46 int sample_width
, sample_bytes
;
50 struct uac_format_type_i_discrete_descriptor
*fmt
= _fmt
;
51 sample_width
= fmt
->bBitResolution
;
52 sample_bytes
= fmt
->bSubframeSize
;
57 struct uac_format_type_i_ext_descriptor
*fmt
= _fmt
;
58 sample_width
= fmt
->bBitResolution
;
59 sample_bytes
= fmt
->bSubslotSize
;
63 * USB audio class v2 devices specify a bitmap of possible
64 * audio formats rather than one fix value. For now, we just
65 * pick one of them and report that as the only possible
66 * value for this setting.
67 * The bit allocation map is in fact compatible to the
68 * wFormatTag of the v1 AS streaming descriptors, which is why
69 * we can simply map the matrix.
72 for (i
= 0; i
< 5; i
++)
73 if (format
& (1UL << i
)) {
85 /* FIXME: correct endianess and sign? */
89 case UAC_FORMAT_TYPE_I_UNDEFINED
: /* some devices don't define this correctly... */
90 snd_printdd(KERN_INFO
"%d:%u:%d : format type 0 is detected, processed as PCM\n",
91 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
);
93 case UAC_FORMAT_TYPE_I_PCM
:
94 if (sample_width
> sample_bytes
* 8) {
95 snd_printk(KERN_INFO
"%d:%u:%d : sample bitwidth %d in over sample bytes %d\n",
96 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
,
97 sample_width
, sample_bytes
);
99 /* check the format byte size */
100 switch (sample_bytes
) {
102 pcm_format
= SNDRV_PCM_FORMAT_S8
;
105 if (snd_usb_is_big_endian_format(chip
, fp
))
106 pcm_format
= SNDRV_PCM_FORMAT_S16_BE
; /* grrr, big endian!! */
108 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
111 if (snd_usb_is_big_endian_format(chip
, fp
))
112 pcm_format
= SNDRV_PCM_FORMAT_S24_3BE
; /* grrr, big endian!! */
114 pcm_format
= SNDRV_PCM_FORMAT_S24_3LE
;
117 pcm_format
= SNDRV_PCM_FORMAT_S32_LE
;
120 snd_printk(KERN_INFO
"%d:%u:%d : unsupported sample bitwidth %d in %d bytes\n",
121 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
,
122 sample_width
, sample_bytes
);
126 case UAC_FORMAT_TYPE_I_PCM8
:
127 pcm_format
= SNDRV_PCM_FORMAT_U8
;
129 /* Dallas DS4201 workaround: it advertises U8 format, but really
131 if (chip
->usb_id
== USB_ID(0x04fa, 0x4201))
132 pcm_format
= SNDRV_PCM_FORMAT_S8
;
134 case UAC_FORMAT_TYPE_I_IEEE_FLOAT
:
135 pcm_format
= SNDRV_PCM_FORMAT_FLOAT_LE
;
137 case UAC_FORMAT_TYPE_I_ALAW
:
138 pcm_format
= SNDRV_PCM_FORMAT_A_LAW
;
140 case UAC_FORMAT_TYPE_I_MULAW
:
141 pcm_format
= SNDRV_PCM_FORMAT_MU_LAW
;
144 snd_printk(KERN_INFO
"%d:%u:%d : unsupported format type %d\n",
145 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, format
);
153 * parse the format descriptor and stores the possible sample rates
154 * on the audioformat table (audio class v1).
157 * @fp: audioformat record
158 * @fmt: the format descriptor
159 * @offset: the start offset of descriptor pointing the rate type
160 * (7 for type I and II, 8 for type II)
162 static int parse_audio_format_rates_v1(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
163 unsigned char *fmt
, int offset
)
165 int nr_rates
= fmt
[offset
];
167 if (fmt
[0] < offset
+ 1 + 3 * (nr_rates
? nr_rates
: 2)) {
168 snd_printk(KERN_ERR
"%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
169 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
);
175 * build the rate table and bitmap flags
179 fp
->rate_table
= kmalloc(sizeof(int) * nr_rates
, GFP_KERNEL
);
180 if (fp
->rate_table
== NULL
) {
181 snd_printk(KERN_ERR
"cannot malloc\n");
186 fp
->rate_min
= fp
->rate_max
= 0;
187 for (r
= 0, idx
= offset
+ 1; r
< nr_rates
; r
++, idx
+= 3) {
188 unsigned int rate
= combine_triple(&fmt
[idx
]);
191 /* C-Media CM6501 mislabels its 96 kHz altsetting */
192 if (rate
== 48000 && nr_rates
== 1 &&
193 (chip
->usb_id
== USB_ID(0x0d8c, 0x0201) ||
194 chip
->usb_id
== USB_ID(0x0d8c, 0x0102)) &&
195 fp
->altsetting
== 5 && fp
->maxpacksize
== 392)
197 /* Creative VF0470 Live Cam reports 16 kHz instead of 8kHz */
198 if (rate
== 16000 && chip
->usb_id
== USB_ID(0x041e, 0x4068))
201 fp
->rate_table
[fp
->nr_rates
] = rate
;
202 if (!fp
->rate_min
|| rate
< fp
->rate_min
)
204 if (!fp
->rate_max
|| rate
> fp
->rate_max
)
206 fp
->rates
|= snd_pcm_rate_to_rate_bit(rate
);
210 hwc_debug("All rates were zero. Skipping format!\n");
214 /* continuous rates */
215 fp
->rates
= SNDRV_PCM_RATE_CONTINUOUS
;
216 fp
->rate_min
= combine_triple(&fmt
[offset
+ 1]);
217 fp
->rate_max
= combine_triple(&fmt
[offset
+ 4]);
223 * parse the format descriptor and stores the possible sample rates
224 * on the audioformat table (audio class v2).
226 static int parse_audio_format_rates_v2(struct snd_usb_audio
*chip
,
227 struct audioformat
*fp
,
228 struct usb_host_interface
*iface
)
230 struct usb_device
*dev
= chip
->dev
;
231 unsigned char tmp
[2], *data
;
232 int i
, nr_rates
, data_size
, ret
= 0;
234 /* get the number of sample rates first by only fetching 2 bytes */
235 ret
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_RANGE
,
236 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
237 0x0100, chip
->clock_id
<< 8, tmp
, sizeof(tmp
), 1000);
240 snd_printk(KERN_ERR
"unable to retrieve number of sample rates\n");
244 nr_rates
= (tmp
[1] << 8) | tmp
[0];
245 data_size
= 2 + 12 * nr_rates
;
246 data
= kzalloc(data_size
, GFP_KERNEL
);
252 /* now get the full information */
253 ret
= snd_usb_ctl_msg(dev
, usb_rcvctrlpipe(dev
, 0), UAC2_CS_RANGE
,
254 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
255 0x0100, chip
->clock_id
<< 8, data
, data_size
, 1000);
258 snd_printk(KERN_ERR
"unable to retrieve sample rate range\n");
263 fp
->rate_table
= kmalloc(sizeof(int) * nr_rates
, GFP_KERNEL
);
264 if (!fp
->rate_table
) {
270 fp
->rate_min
= fp
->rate_max
= 0;
272 for (i
= 0; i
< nr_rates
; i
++) {
273 int rate
= combine_quad(&data
[2 + 12 * i
]);
275 fp
->rate_table
[fp
->nr_rates
] = rate
;
276 if (!fp
->rate_min
|| rate
< fp
->rate_min
)
278 if (!fp
->rate_max
|| rate
> fp
->rate_max
)
280 fp
->rates
|= snd_pcm_rate_to_rate_bit(rate
);
291 * parse the format type I and III descriptors
293 static int parse_audio_format_i(struct snd_usb_audio
*chip
,
294 struct audioformat
*fp
,
295 int format
, void *_fmt
,
296 struct usb_host_interface
*iface
)
298 struct usb_interface_descriptor
*altsd
= get_iface_desc(iface
);
299 struct uac_format_type_i_discrete_descriptor
*fmt
= _fmt
;
300 int protocol
= altsd
->bInterfaceProtocol
;
303 if (fmt
->bFormatType
== UAC_FORMAT_TYPE_III
) {
304 /* FIXME: the format type is really IECxxx
305 * but we give normal PCM format to get the existing
308 switch (chip
->usb_id
) {
310 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
311 if (chip
->setup
== 0x00 &&
313 pcm_format
= SNDRV_PCM_FORMAT_S16_BE
;
315 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
318 pcm_format
= SNDRV_PCM_FORMAT_S16_LE
;
321 pcm_format
= parse_audio_format_i_type(chip
, fp
, format
, fmt
, protocol
);
326 fp
->format
= pcm_format
;
328 /* gather possible sample rates */
329 /* audio class v1 reports possible sample rates as part of the
330 * proprietary class specific descriptor.
331 * audio class v2 uses class specific EP0 range requests for that.
335 fp
->channels
= fmt
->bNrChannels
;
336 ret
= parse_audio_format_rates_v1(chip
, fp
, _fmt
, 7);
339 /* fp->channels is already set in this case */
340 ret
= parse_audio_format_rates_v2(chip
, fp
, iface
);
344 if (fp
->channels
< 1) {
345 snd_printk(KERN_ERR
"%d:%u:%d : invalid channels %d\n",
346 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, fp
->channels
);
354 * parse the format type II descriptor
356 static int parse_audio_format_ii(struct snd_usb_audio
*chip
,
357 struct audioformat
*fp
,
358 int format
, void *_fmt
,
359 struct usb_host_interface
*iface
)
361 int brate
, framesize
, ret
;
362 struct usb_interface_descriptor
*altsd
= get_iface_desc(iface
);
363 int protocol
= altsd
->bInterfaceProtocol
;
366 case UAC_FORMAT_TYPE_II_AC3
:
367 /* FIXME: there is no AC3 format defined yet */
368 // fp->format = SNDRV_PCM_FORMAT_AC3;
369 fp
->format
= SNDRV_PCM_FORMAT_U8
; /* temporarily hack to receive byte streams */
371 case UAC_FORMAT_TYPE_II_MPEG
:
372 fp
->format
= SNDRV_PCM_FORMAT_MPEG
;
375 snd_printd(KERN_INFO
"%d:%u:%d : unknown format tag %#x is detected. processed as MPEG.\n",
376 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, format
);
377 fp
->format
= SNDRV_PCM_FORMAT_MPEG
;
384 case UAC_VERSION_1
: {
385 struct uac_format_type_ii_discrete_descriptor
*fmt
= _fmt
;
386 brate
= le16_to_cpu(fmt
->wMaxBitRate
);
387 framesize
= le16_to_cpu(fmt
->wSamplesPerFrame
);
388 snd_printd(KERN_INFO
"found format II with max.bitrate = %d, frame size=%d\n", brate
, framesize
);
389 fp
->frame_size
= framesize
;
390 ret
= parse_audio_format_rates_v1(chip
, fp
, _fmt
, 8); /* fmt[8..] sample rates */
393 case UAC_VERSION_2
: {
394 struct uac_format_type_ii_ext_descriptor
*fmt
= _fmt
;
395 brate
= le16_to_cpu(fmt
->wMaxBitRate
);
396 framesize
= le16_to_cpu(fmt
->wSamplesPerFrame
);
397 snd_printd(KERN_INFO
"found format II with max.bitrate = %d, frame size=%d\n", brate
, framesize
);
398 fp
->frame_size
= framesize
;
399 ret
= parse_audio_format_rates_v2(chip
, fp
, iface
);
407 int snd_usb_parse_audio_format(struct snd_usb_audio
*chip
, struct audioformat
*fp
,
408 int format
, unsigned char *fmt
, int stream
,
409 struct usb_host_interface
*iface
)
414 case UAC_FORMAT_TYPE_I
:
415 case UAC_FORMAT_TYPE_III
:
416 err
= parse_audio_format_i(chip
, fp
, format
, fmt
, iface
);
418 case UAC_FORMAT_TYPE_II
:
419 err
= parse_audio_format_ii(chip
, fp
, format
, fmt
, iface
);
422 snd_printd(KERN_INFO
"%d:%u:%d : format type %d is not supported yet\n",
423 chip
->dev
->devnum
, fp
->iface
, fp
->altsetting
, fmt
[3]);
426 fp
->fmt_type
= fmt
[3];
430 /* FIXME: temporary hack for extigy/audigy 2 nx/zs */
431 /* extigy apparently supports sample rates other than 48k
432 * but not in ordinary way. so we enable only 48k atm.
434 if (chip
->usb_id
== USB_ID(0x041e, 0x3000) ||
435 chip
->usb_id
== USB_ID(0x041e, 0x3020) ||
436 chip
->usb_id
== USB_ID(0x041e, 0x3061)) {
437 if (fmt
[3] == UAC_FORMAT_TYPE_I
&&
438 fp
->rates
!= SNDRV_PCM_RATE_48000
&&
439 fp
->rates
!= SNDRV_PCM_RATE_96000
)