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