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/slab.h>
20 #include <linux/usb.h>
21 #include <linux/usb/audio.h>
22 #include <linux/usb/audio-v2.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
40 static void free_substream(struct snd_usb_substream
*subs
)
42 struct list_head
*p
, *n
;
44 if (!subs
->num_formats
)
45 return; /* not initialized */
46 list_for_each_safe(p
, n
, &subs
->fmt_list
) {
47 struct audioformat
*fp
= list_entry(p
, struct audioformat
, list
);
48 kfree(fp
->rate_table
);
51 kfree(subs
->rate_list
.list
);
56 * free a usb stream instance
58 static void snd_usb_audio_stream_free(struct snd_usb_stream
*stream
)
60 free_substream(&stream
->substream
[0]);
61 free_substream(&stream
->substream
[1]);
62 list_del(&stream
->list
);
66 static void snd_usb_audio_pcm_free(struct snd_pcm
*pcm
)
68 struct snd_usb_stream
*stream
= pcm
->private_data
;
71 snd_usb_audio_stream_free(stream
);
77 * add this endpoint to the chip instance.
78 * if a stream with the same endpoint already exists, append to it.
79 * if not, create a new pcm stream.
81 int snd_usb_add_audio_endpoint(struct snd_usb_audio
*chip
, int stream
, struct audioformat
*fp
)
84 struct snd_usb_stream
*as
;
85 struct snd_usb_substream
*subs
;
89 list_for_each(p
, &chip
->pcm_list
) {
90 as
= list_entry(p
, struct snd_usb_stream
, list
);
91 if (as
->fmt_type
!= fp
->fmt_type
)
93 subs
= &as
->substream
[stream
];
96 if (subs
->endpoint
== fp
->endpoint
) {
97 list_add_tail(&fp
->list
, &subs
->fmt_list
);
99 subs
->formats
|= fp
->formats
;
103 /* look for an empty stream */
104 list_for_each(p
, &chip
->pcm_list
) {
105 as
= list_entry(p
, struct snd_usb_stream
, list
);
106 if (as
->fmt_type
!= fp
->fmt_type
)
108 subs
= &as
->substream
[stream
];
111 err
= snd_pcm_new_stream(as
->pcm
, stream
, 1);
114 snd_usb_init_substream(as
, stream
, fp
);
118 /* create a new pcm */
119 as
= kzalloc(sizeof(*as
), GFP_KERNEL
);
122 as
->pcm_index
= chip
->pcm_devs
;
124 as
->fmt_type
= fp
->fmt_type
;
125 err
= snd_pcm_new(chip
->card
, "USB Audio", chip
->pcm_devs
,
126 stream
== SNDRV_PCM_STREAM_PLAYBACK
? 1 : 0,
127 stream
== SNDRV_PCM_STREAM_PLAYBACK
? 0 : 1,
134 pcm
->private_data
= as
;
135 pcm
->private_free
= snd_usb_audio_pcm_free
;
137 if (chip
->pcm_devs
> 0)
138 sprintf(pcm
->name
, "USB Audio #%d", chip
->pcm_devs
);
140 strcpy(pcm
->name
, "USB Audio");
142 snd_usb_init_substream(as
, stream
, fp
);
144 list_add(&as
->list
, &chip
->pcm_list
);
147 snd_usb_proc_pcm_format_add(as
);
152 int snd_usb_parse_audio_endpoints(struct snd_usb_audio
*chip
, int iface_no
)
154 struct usb_device
*dev
;
155 struct usb_interface
*iface
;
156 struct usb_host_interface
*alts
;
157 struct usb_interface_descriptor
*altsd
;
158 int i
, altno
, err
, stream
;
159 int format
= 0, num_channels
= 0;
160 struct audioformat
*fp
= NULL
;
161 unsigned char *fmt
, *csep
;
166 /* parse the interface's altsettings */
167 iface
= usb_ifnum_to_if(dev
, iface_no
);
169 num
= iface
->num_altsetting
;
172 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
173 * one misses syncpipe, and does not produce any sound.
175 if (chip
->usb_id
== USB_ID(0x04fa, 0x4201))
178 for (i
= 0; i
< num
; i
++) {
179 alts
= &iface
->altsetting
[i
];
180 altsd
= get_iface_desc(alts
);
181 protocol
= altsd
->bInterfaceProtocol
;
182 /* skip invalid one */
183 if ((altsd
->bInterfaceClass
!= USB_CLASS_AUDIO
&&
184 altsd
->bInterfaceClass
!= USB_CLASS_VENDOR_SPEC
) ||
185 (altsd
->bInterfaceSubClass
!= USB_SUBCLASS_AUDIOSTREAMING
&&
186 altsd
->bInterfaceSubClass
!= USB_SUBCLASS_VENDOR_SPEC
) ||
187 altsd
->bNumEndpoints
< 1 ||
188 le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
) == 0)
190 /* must be isochronous */
191 if ((get_endpoint(alts
, 0)->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) !=
192 USB_ENDPOINT_XFER_ISOC
)
194 /* check direction */
195 stream
= (get_endpoint(alts
, 0)->bEndpointAddress
& USB_DIR_IN
) ?
196 SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
197 altno
= altsd
->bAlternateSetting
;
199 if (snd_usb_apply_interface_quirk(chip
, iface_no
, altno
))
202 /* get audio formats */
204 case UAC_VERSION_1
: {
205 struct uac_as_header_descriptor_v1
*as
=
206 snd_usb_find_csint_desc(alts
->extra
, alts
->extralen
, NULL
, UAC_AS_GENERAL
);
209 snd_printk(KERN_ERR
"%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
210 dev
->devnum
, iface_no
, altno
);
214 if (as
->bLength
< sizeof(*as
)) {
215 snd_printk(KERN_ERR
"%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
216 dev
->devnum
, iface_no
, altno
);
220 format
= le16_to_cpu(as
->wFormatTag
); /* remember the format value */
224 case UAC_VERSION_2
: {
225 struct uac_as_header_descriptor_v2
*as
=
226 snd_usb_find_csint_desc(alts
->extra
, alts
->extralen
, NULL
, UAC_AS_GENERAL
);
229 snd_printk(KERN_ERR
"%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
230 dev
->devnum
, iface_no
, altno
);
234 if (as
->bLength
< sizeof(*as
)) {
235 snd_printk(KERN_ERR
"%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
236 dev
->devnum
, iface_no
, altno
);
240 num_channels
= as
->bNrChannels
;
241 format
= le32_to_cpu(as
->bmFormats
);
247 snd_printk(KERN_ERR
"%d:%u:%d : unknown interface protocol %04x\n",
248 dev
->devnum
, iface_no
, altno
, protocol
);
252 /* get format type */
253 fmt
= snd_usb_find_csint_desc(alts
->extra
, alts
->extralen
, NULL
, UAC_FORMAT_TYPE
);
255 snd_printk(KERN_ERR
"%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
256 dev
->devnum
, iface_no
, altno
);
259 if (((protocol
== UAC_VERSION_1
) && (fmt
[0] < 8)) ||
260 ((protocol
== UAC_VERSION_2
) && (fmt
[0] != 6))) {
261 snd_printk(KERN_ERR
"%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
262 dev
->devnum
, iface_no
, altno
);
267 * Blue Microphones workaround: The last altsetting is identical
268 * with the previous one, except for a larger packet size, but
269 * is actually a mislabeled two-channel setting; ignore it.
271 if (fmt
[4] == 1 && fmt
[5] == 2 && altno
== 2 && num
== 3 &&
272 fp
&& fp
->altsetting
== 1 && fp
->channels
== 1 &&
273 fp
->formats
== SNDRV_PCM_FMTBIT_S16_LE
&&
274 protocol
== UAC_VERSION_1
&&
275 le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
) ==
279 csep
= snd_usb_find_desc(alts
->endpoint
[0].extra
, alts
->endpoint
[0].extralen
, NULL
, USB_DT_CS_ENDPOINT
);
280 /* Creamware Noah has this descriptor after the 2nd endpoint */
281 if (!csep
&& altsd
->bNumEndpoints
>= 2)
282 csep
= snd_usb_find_desc(alts
->endpoint
[1].extra
, alts
->endpoint
[1].extralen
, NULL
, USB_DT_CS_ENDPOINT
);
283 if (!csep
|| csep
[0] < 7 || csep
[2] != UAC_EP_GENERAL
) {
284 snd_printk(KERN_WARNING
"%d:%u:%d : no or invalid"
285 " class specific endpoint descriptor\n",
286 dev
->devnum
, iface_no
, altno
);
290 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
292 snd_printk(KERN_ERR
"cannot malloc\n");
296 fp
->iface
= iface_no
;
297 fp
->altsetting
= altno
;
299 fp
->endpoint
= get_endpoint(alts
, 0)->bEndpointAddress
;
300 fp
->ep_attr
= get_endpoint(alts
, 0)->bmAttributes
;
301 fp
->datainterval
= snd_usb_parse_datainterval(chip
, alts
);
302 fp
->maxpacksize
= le16_to_cpu(get_endpoint(alts
, 0)->wMaxPacketSize
);
303 /* num_channels is only set for v2 interfaces */
304 fp
->channels
= num_channels
;
305 if (snd_usb_get_speed(dev
) == USB_SPEED_HIGH
)
306 fp
->maxpacksize
= (((fp
->maxpacksize
>> 11) & 3) + 1)
307 * (fp
->maxpacksize
& 0x7ff);
308 fp
->attributes
= csep
? csep
[3] : 0;
310 /* some quirks for attributes here */
312 switch (chip
->usb_id
) {
313 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
314 /* Optoplay sets the sample rate attribute although
315 * it seems not supporting it in fact.
317 fp
->attributes
&= ~UAC_EP_CS_ATTR_SAMPLE_RATE
;
319 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
320 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
321 case USB_ID(0x0763, 0x2080): /* M-Audio Fast Track Ultra 8 */
322 case USB_ID(0x0763, 0x2081): /* M-Audio Fast Track Ultra 8R */
323 /* doesn't set the sample rate attribute, but supports it */
324 fp
->attributes
|= UAC_EP_CS_ATTR_SAMPLE_RATE
;
326 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
327 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
328 an older model 77d:223) */
330 * plantronics headset and Griffin iMic have set adaptive-in
331 * although it's really not...
333 fp
->ep_attr
&= ~USB_ENDPOINT_SYNCTYPE
;
334 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
)
335 fp
->ep_attr
|= USB_ENDPOINT_SYNC_ADAPTIVE
;
337 fp
->ep_attr
|= USB_ENDPOINT_SYNC_SYNC
;
341 /* ok, let's parse further... */
342 if (snd_usb_parse_audio_format(chip
, fp
, format
, fmt
, stream
, alts
) < 0) {
343 kfree(fp
->rate_table
);
348 snd_printdd(KERN_INFO
"%d:%u:%d: add audio endpoint %#x\n", dev
->devnum
, iface_no
, altno
, fp
->endpoint
);
349 err
= snd_usb_add_audio_endpoint(chip
, stream
, fp
);
351 kfree(fp
->rate_table
);
355 /* try to set the interface... */
356 usb_set_interface(chip
->dev
, iface_no
, altno
);
357 snd_usb_init_pitch(chip
, iface_no
, alts
, fp
);
358 snd_usb_init_sample_rate(chip
, iface_no
, alts
, fp
, fp
->rate_max
);