Commit | Line | Data |
---|---|---|
e8e8babf DM |
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 | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
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 | |
15 | */ | |
16 | ||
17 | ||
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> | |
23 | ||
24 | #include <sound/core.h> | |
25 | #include <sound/pcm.h> | |
26 | ||
27 | #include "usbaudio.h" | |
28 | #include "card.h" | |
29 | #include "proc.h" | |
30 | #include "quirks.h" | |
31 | #include "endpoint.h" | |
e8e8babf DM |
32 | #include "pcm.h" |
33 | #include "helper.h" | |
34 | #include "format.h" | |
35 | #include "clock.h" | |
36 | #include "stream.h" | |
37 | ||
38 | /* | |
39 | * free a substream | |
40 | */ | |
41 | static void free_substream(struct snd_usb_substream *subs) | |
42 | { | |
43 | struct list_head *p, *n; | |
44 | ||
45 | if (!subs->num_formats) | |
46 | return; /* not initialized */ | |
47 | list_for_each_safe(p, n, &subs->fmt_list) { | |
48 | struct audioformat *fp = list_entry(p, struct audioformat, list); | |
49 | kfree(fp->rate_table); | |
50 | kfree(fp); | |
51 | } | |
52 | kfree(subs->rate_list.list); | |
53 | } | |
54 | ||
55 | ||
56 | /* | |
57 | * free a usb stream instance | |
58 | */ | |
59 | static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) | |
60 | { | |
61 | free_substream(&stream->substream[0]); | |
62 | free_substream(&stream->substream[1]); | |
63 | list_del(&stream->list); | |
64 | kfree(stream); | |
65 | } | |
66 | ||
67 | static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) | |
68 | { | |
69 | struct snd_usb_stream *stream = pcm->private_data; | |
70 | if (stream) { | |
71 | stream->pcm = NULL; | |
72 | snd_usb_audio_stream_free(stream); | |
73 | } | |
74 | } | |
75 | ||
edcd3633 DM |
76 | /* |
77 | * initialize the substream instance. | |
78 | */ | |
79 | ||
80 | static void snd_usb_init_substream(struct snd_usb_stream *as, | |
81 | int stream, | |
82 | struct audioformat *fp) | |
83 | { | |
84 | struct snd_usb_substream *subs = &as->substream[stream]; | |
85 | ||
86 | INIT_LIST_HEAD(&subs->fmt_list); | |
87 | spin_lock_init(&subs->lock); | |
88 | ||
89 | subs->stream = as; | |
90 | subs->direction = stream; | |
91 | subs->dev = as->chip->dev; | |
92 | subs->txfr_quirk = as->chip->txfr_quirk; | |
978520b7 | 93 | subs->speed = snd_usb_get_speed(subs->dev); |
edcd3633 DM |
94 | |
95 | snd_usb_set_pcm_ops(as->pcm, stream); | |
96 | ||
97 | list_add_tail(&fp->list, &subs->fmt_list); | |
98 | subs->formats |= fp->formats; | |
99 | subs->num_formats++; | |
100 | subs->fmt_type = fp->fmt_type; | |
8260ef07 | 101 | subs->ep_num = fp->endpoint; |
edcd3633 | 102 | } |
e8e8babf DM |
103 | |
104 | /* | |
105 | * add this endpoint to the chip instance. | |
106 | * if a stream with the same endpoint already exists, append to it. | |
107 | * if not, create a new pcm stream. | |
108 | */ | |
109 | int snd_usb_add_audio_stream(struct snd_usb_audio *chip, | |
110 | int stream, | |
111 | struct audioformat *fp) | |
112 | { | |
113 | struct list_head *p; | |
114 | struct snd_usb_stream *as; | |
115 | struct snd_usb_substream *subs; | |
116 | struct snd_pcm *pcm; | |
117 | int err; | |
118 | ||
119 | list_for_each(p, &chip->pcm_list) { | |
120 | as = list_entry(p, struct snd_usb_stream, list); | |
121 | if (as->fmt_type != fp->fmt_type) | |
122 | continue; | |
123 | subs = &as->substream[stream]; | |
8260ef07 | 124 | if (subs->ep_num == fp->endpoint) { |
e8e8babf DM |
125 | list_add_tail(&fp->list, &subs->fmt_list); |
126 | subs->num_formats++; | |
127 | subs->formats |= fp->formats; | |
128 | return 0; | |
129 | } | |
130 | } | |
131 | /* look for an empty stream */ | |
132 | list_for_each(p, &chip->pcm_list) { | |
133 | as = list_entry(p, struct snd_usb_stream, list); | |
134 | if (as->fmt_type != fp->fmt_type) | |
135 | continue; | |
136 | subs = &as->substream[stream]; | |
8260ef07 | 137 | if (subs->ep_num) |
e8e8babf DM |
138 | continue; |
139 | err = snd_pcm_new_stream(as->pcm, stream, 1); | |
140 | if (err < 0) | |
141 | return err; | |
142 | snd_usb_init_substream(as, stream, fp); | |
143 | return 0; | |
144 | } | |
145 | ||
146 | /* create a new pcm */ | |
147 | as = kzalloc(sizeof(*as), GFP_KERNEL); | |
148 | if (!as) | |
149 | return -ENOMEM; | |
150 | as->pcm_index = chip->pcm_devs; | |
151 | as->chip = chip; | |
152 | as->fmt_type = fp->fmt_type; | |
153 | err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, | |
154 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, | |
155 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, | |
156 | &pcm); | |
157 | if (err < 0) { | |
158 | kfree(as); | |
159 | return err; | |
160 | } | |
161 | as->pcm = pcm; | |
162 | pcm->private_data = as; | |
163 | pcm->private_free = snd_usb_audio_pcm_free; | |
164 | pcm->info_flags = 0; | |
165 | if (chip->pcm_devs > 0) | |
166 | sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs); | |
167 | else | |
168 | strcpy(pcm->name, "USB Audio"); | |
169 | ||
170 | snd_usb_init_substream(as, stream, fp); | |
171 | ||
172 | list_add(&as->list, &chip->pcm_list); | |
173 | chip->pcm_devs++; | |
174 | ||
175 | snd_usb_proc_pcm_format_add(as); | |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip, | |
181 | struct usb_host_interface *alts, | |
182 | int protocol, int iface_no) | |
183 | { | |
184 | /* parsed with a v1 header here. that's ok as we only look at the | |
185 | * header first which is the same for both versions */ | |
186 | struct uac_iso_endpoint_descriptor *csep; | |
187 | struct usb_interface_descriptor *altsd = get_iface_desc(alts); | |
188 | int attributes = 0; | |
189 | ||
190 | csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); | |
191 | ||
192 | /* Creamware Noah has this descriptor after the 2nd endpoint */ | |
193 | if (!csep && altsd->bNumEndpoints >= 2) | |
194 | csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); | |
195 | ||
196 | if (!csep || csep->bLength < 7 || | |
197 | csep->bDescriptorSubtype != UAC_EP_GENERAL) { | |
198 | snd_printk(KERN_WARNING "%d:%u:%d : no or invalid" | |
199 | " class specific endpoint descriptor\n", | |
200 | chip->dev->devnum, iface_no, | |
201 | altsd->bAlternateSetting); | |
202 | return 0; | |
203 | } | |
204 | ||
205 | if (protocol == UAC_VERSION_1) { | |
206 | attributes = csep->bmAttributes; | |
207 | } else { | |
208 | struct uac2_iso_endpoint_descriptor *csep2 = | |
209 | (struct uac2_iso_endpoint_descriptor *) csep; | |
210 | ||
211 | attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX; | |
212 | ||
213 | /* emulate the endpoint attributes of a v1 device */ | |
214 | if (csep2->bmControls & UAC2_CONTROL_PITCH) | |
215 | attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL; | |
216 | } | |
217 | ||
218 | return attributes; | |
219 | } | |
220 | ||
221 | static struct uac2_input_terminal_descriptor * | |
222 | snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface, | |
223 | int terminal_id) | |
224 | { | |
225 | struct uac2_input_terminal_descriptor *term = NULL; | |
226 | ||
227 | while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, | |
228 | ctrl_iface->extralen, | |
229 | term, UAC_INPUT_TERMINAL))) { | |
230 | if (term->bTerminalID == terminal_id) | |
231 | return term; | |
232 | } | |
233 | ||
234 | return NULL; | |
235 | } | |
236 | ||
237 | static struct uac2_output_terminal_descriptor * | |
238 | snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface, | |
239 | int terminal_id) | |
240 | { | |
241 | struct uac2_output_terminal_descriptor *term = NULL; | |
242 | ||
243 | while ((term = snd_usb_find_csint_desc(ctrl_iface->extra, | |
244 | ctrl_iface->extralen, | |
245 | term, UAC_OUTPUT_TERMINAL))) { | |
246 | if (term->bTerminalID == terminal_id) | |
247 | return term; | |
248 | } | |
249 | ||
250 | return NULL; | |
251 | } | |
252 | ||
253 | int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no) | |
254 | { | |
255 | struct usb_device *dev; | |
256 | struct usb_interface *iface; | |
257 | struct usb_host_interface *alts; | |
258 | struct usb_interface_descriptor *altsd; | |
259 | int i, altno, err, stream; | |
260 | int format = 0, num_channels = 0; | |
261 | struct audioformat *fp = NULL; | |
262 | int num, protocol, clock = 0; | |
263 | struct uac_format_type_i_continuous_descriptor *fmt; | |
264 | ||
265 | dev = chip->dev; | |
266 | ||
267 | /* parse the interface's altsettings */ | |
268 | iface = usb_ifnum_to_if(dev, iface_no); | |
269 | ||
270 | num = iface->num_altsetting; | |
271 | ||
272 | /* | |
273 | * Dallas DS4201 workaround: It presents 5 altsettings, but the last | |
274 | * one misses syncpipe, and does not produce any sound. | |
275 | */ | |
276 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) | |
277 | num = 4; | |
278 | ||
279 | for (i = 0; i < num; i++) { | |
280 | alts = &iface->altsetting[i]; | |
281 | altsd = get_iface_desc(alts); | |
282 | protocol = altsd->bInterfaceProtocol; | |
283 | /* skip invalid one */ | |
284 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
285 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
286 | (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING && | |
287 | altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) || | |
288 | altsd->bNumEndpoints < 1 || | |
289 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0) | |
290 | continue; | |
291 | /* must be isochronous */ | |
292 | if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != | |
293 | USB_ENDPOINT_XFER_ISOC) | |
294 | continue; | |
295 | /* check direction */ | |
296 | stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ? | |
297 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
298 | altno = altsd->bAlternateSetting; | |
299 | ||
300 | if (snd_usb_apply_interface_quirk(chip, iface_no, altno)) | |
301 | continue; | |
302 | ||
303 | /* get audio formats */ | |
304 | switch (protocol) { | |
305 | default: | |
306 | snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n", | |
307 | dev->devnum, iface_no, altno, protocol); | |
308 | protocol = UAC_VERSION_1; | |
309 | /* fall through */ | |
310 | ||
311 | case UAC_VERSION_1: { | |
312 | struct uac1_as_header_descriptor *as = | |
313 | snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); | |
314 | ||
315 | if (!as) { | |
316 | snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n", | |
317 | dev->devnum, iface_no, altno); | |
318 | continue; | |
319 | } | |
320 | ||
321 | if (as->bLength < sizeof(*as)) { | |
322 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", | |
323 | dev->devnum, iface_no, altno); | |
324 | continue; | |
325 | } | |
326 | ||
327 | format = le16_to_cpu(as->wFormatTag); /* remember the format value */ | |
328 | break; | |
329 | } | |
330 | ||
331 | case UAC_VERSION_2: { | |
332 | struct uac2_input_terminal_descriptor *input_term; | |
333 | struct uac2_output_terminal_descriptor *output_term; | |
334 | struct uac2_as_header_descriptor *as = | |
335 | snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); | |
336 | ||
337 | if (!as) { | |
338 | snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n", | |
339 | dev->devnum, iface_no, altno); | |
340 | continue; | |
341 | } | |
342 | ||
343 | if (as->bLength < sizeof(*as)) { | |
344 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", | |
345 | dev->devnum, iface_no, altno); | |
346 | continue; | |
347 | } | |
348 | ||
349 | num_channels = as->bNrChannels; | |
350 | format = le32_to_cpu(as->bmFormats); | |
351 | ||
352 | /* lookup the terminal associated to this interface | |
353 | * to extract the clock */ | |
354 | input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf, | |
355 | as->bTerminalLink); | |
356 | if (input_term) { | |
357 | clock = input_term->bCSourceID; | |
358 | break; | |
359 | } | |
360 | ||
361 | output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf, | |
362 | as->bTerminalLink); | |
363 | if (output_term) { | |
364 | clock = output_term->bCSourceID; | |
365 | break; | |
366 | } | |
367 | ||
368 | snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n", | |
369 | dev->devnum, iface_no, altno, as->bTerminalLink); | |
370 | continue; | |
371 | } | |
372 | } | |
373 | ||
374 | /* get format type */ | |
375 | fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE); | |
376 | if (!fmt) { | |
377 | snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n", | |
378 | dev->devnum, iface_no, altno); | |
379 | continue; | |
380 | } | |
381 | if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) || | |
382 | ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) { | |
383 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n", | |
384 | dev->devnum, iface_no, altno); | |
385 | continue; | |
386 | } | |
387 | ||
388 | /* | |
389 | * Blue Microphones workaround: The last altsetting is identical | |
390 | * with the previous one, except for a larger packet size, but | |
391 | * is actually a mislabeled two-channel setting; ignore it. | |
392 | */ | |
393 | if (fmt->bNrChannels == 1 && | |
394 | fmt->bSubframeSize == 2 && | |
395 | altno == 2 && num == 3 && | |
396 | fp && fp->altsetting == 1 && fp->channels == 1 && | |
397 | fp->formats == SNDRV_PCM_FMTBIT_S16_LE && | |
398 | protocol == UAC_VERSION_1 && | |
399 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == | |
400 | fp->maxpacksize * 2) | |
401 | continue; | |
402 | ||
403 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); | |
404 | if (! fp) { | |
405 | snd_printk(KERN_ERR "cannot malloc\n"); | |
406 | return -ENOMEM; | |
407 | } | |
408 | ||
409 | fp->iface = iface_no; | |
410 | fp->altsetting = altno; | |
411 | fp->altset_idx = i; | |
412 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; | |
413 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; | |
414 | fp->datainterval = snd_usb_parse_datainterval(chip, alts); | |
415 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); | |
416 | /* num_channels is only set for v2 interfaces */ | |
417 | fp->channels = num_channels; | |
418 | if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) | |
419 | fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) | |
420 | * (fp->maxpacksize & 0x7ff); | |
421 | fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no); | |
422 | fp->clock = clock; | |
423 | ||
424 | /* some quirks for attributes here */ | |
425 | ||
426 | switch (chip->usb_id) { | |
427 | case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */ | |
428 | /* Optoplay sets the sample rate attribute although | |
429 | * it seems not supporting it in fact. | |
430 | */ | |
431 | fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE; | |
432 | break; | |
433 | case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */ | |
434 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ | |
435 | /* doesn't set the sample rate attribute, but supports it */ | |
436 | fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE; | |
437 | break; | |
438 | case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */ | |
439 | case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */ | |
440 | case USB_ID(0x047f, 0x0ca1): /* plantronics headset */ | |
441 | case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is | |
442 | an older model 77d:223) */ | |
443 | /* | |
444 | * plantronics headset and Griffin iMic have set adaptive-in | |
445 | * although it's really not... | |
446 | */ | |
447 | fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE; | |
448 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
449 | fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE; | |
450 | else | |
451 | fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC; | |
452 | break; | |
453 | } | |
454 | ||
455 | /* ok, let's parse further... */ | |
456 | if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) { | |
457 | kfree(fp->rate_table); | |
458 | kfree(fp); | |
459 | fp = NULL; | |
460 | continue; | |
461 | } | |
462 | ||
463 | snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint); | |
464 | err = snd_usb_add_audio_stream(chip, stream, fp); | |
465 | if (err < 0) { | |
466 | kfree(fp->rate_table); | |
467 | kfree(fp); | |
468 | return err; | |
469 | } | |
470 | /* try to set the interface... */ | |
471 | usb_set_interface(chip->dev, iface_no, altno); | |
472 | snd_usb_init_pitch(chip, iface_no, alts, fp); | |
473 | snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max); | |
474 | } | |
475 | return 0; | |
476 | } | |
477 |