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