7dce074961692433bd28255ed7b462dbd5b354ac
[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. 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.
83  */
84 int 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
157 static 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         /*
174          * If we can't locate the USB_DT_CS_ENDPOINT descriptor in the extra
175          * bytes after the first endpoint, go search the entire interface.
176          * Some devices have it directly *before* the standard endpoint.
177          */
178         if (!csep)
179                 csep = snd_usb_find_desc(alts->extra, alts->extralen, NULL, USB_DT_CS_ENDPOINT);
180
181         if (!csep || csep->bLength < 7 ||
182             csep->bDescriptorSubtype != UAC_EP_GENERAL) {
183                 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
184                            " class specific endpoint descriptor\n",
185                            chip->dev->devnum, iface_no,
186                            altsd->bAlternateSetting);
187                 return 0;
188         }
189
190         if (protocol == UAC_VERSION_1) {
191                 attributes = csep->bmAttributes;
192         } else {
193                 struct uac2_iso_endpoint_descriptor *csep2 =
194                         (struct uac2_iso_endpoint_descriptor *) csep;
195
196                 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
197
198                 /* emulate the endpoint attributes of a v1 device */
199                 if (csep2->bmControls & UAC2_CONTROL_PITCH)
200                         attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
201         }
202
203         return attributes;
204 }
205
206 static struct uac2_input_terminal_descriptor *
207         snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
208                                                int terminal_id)
209 {
210         struct uac2_input_terminal_descriptor *term = NULL;
211
212         while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
213                                                ctrl_iface->extralen,
214                                                term, UAC_INPUT_TERMINAL))) {
215                 if (term->bTerminalID == terminal_id)
216                         return term;
217         }
218
219         return NULL;
220 }
221
222 static struct uac2_output_terminal_descriptor *
223         snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
224                                                 int terminal_id)
225 {
226         struct uac2_output_terminal_descriptor *term = NULL;
227
228         while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
229                                                ctrl_iface->extralen,
230                                                term, UAC_OUTPUT_TERMINAL))) {
231                 if (term->bTerminalID == terminal_id)
232                         return term;
233         }
234
235         return NULL;
236 }
237
238 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
239 {
240         struct usb_device *dev;
241         struct usb_interface *iface;
242         struct usb_host_interface *alts;
243         struct usb_interface_descriptor *altsd;
244         int i, altno, err, stream;
245         int format = 0, num_channels = 0;
246         struct audioformat *fp = NULL;
247         int num, protocol, clock = 0;
248         struct uac_format_type_i_continuous_descriptor *fmt;
249
250         dev = chip->dev;
251
252         /* parse the interface's altsettings */
253         iface = usb_ifnum_to_if(dev, iface_no);
254
255         num = iface->num_altsetting;
256
257         /*
258          * Dallas DS4201 workaround: It presents 5 altsettings, but the last
259          * one misses syncpipe, and does not produce any sound.
260          */
261         if (chip->usb_id == USB_ID(0x04fa, 0x4201))
262                 num = 4;
263
264         for (i = 0; i < num; i++) {
265                 alts = &iface->altsetting[i];
266                 altsd = get_iface_desc(alts);
267                 protocol = altsd->bInterfaceProtocol;
268                 /* skip invalid one */
269                 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
270                      altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
271                     (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
272                      altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
273                     altsd->bNumEndpoints < 1 ||
274                     le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
275                         continue;
276                 /* must be isochronous */
277                 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
278                     USB_ENDPOINT_XFER_ISOC)
279                         continue;
280                 /* check direction */
281                 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
282                         SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
283                 altno = altsd->bAlternateSetting;
284
285                 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
286                         continue;
287
288                 /* get audio formats */
289                 switch (protocol) {
290                 default:
291                         snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
292                                     dev->devnum, iface_no, altno, protocol);
293                         protocol = UAC_VERSION_1;
294                         /* fall through */
295
296                 case UAC_VERSION_1: {
297                         struct uac1_as_header_descriptor *as =
298                                 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
299
300                         if (!as) {
301                                 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
302                                            dev->devnum, iface_no, altno);
303                                 continue;
304                         }
305
306                         if (as->bLength < sizeof(*as)) {
307                                 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
308                                            dev->devnum, iface_no, altno);
309                                 continue;
310                         }
311
312                         format = le16_to_cpu(as->wFormatTag); /* remember the format value */
313                         break;
314                 }
315
316                 case UAC_VERSION_2: {
317                         struct uac2_input_terminal_descriptor *input_term;
318                         struct uac2_output_terminal_descriptor *output_term;
319                         struct uac2_as_header_descriptor *as =
320                                 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
321
322                         if (!as) {
323                                 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
324                                            dev->devnum, iface_no, altno);
325                                 continue;
326                         }
327
328                         if (as->bLength < sizeof(*as)) {
329                                 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
330                                            dev->devnum, iface_no, altno);
331                                 continue;
332                         }
333
334                         num_channels = as->bNrChannels;
335                         format = le32_to_cpu(as->bmFormats);
336
337                         /* lookup the terminal associated to this interface
338                          * to extract the clock */
339                         input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
340                                                                             as->bTerminalLink);
341                         if (input_term) {
342                                 clock = input_term->bCSourceID;
343                                 break;
344                         }
345
346                         output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
347                                                                               as->bTerminalLink);
348                         if (output_term) {
349                                 clock = output_term->bCSourceID;
350                                 break;
351                         }
352
353                         snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
354                                    dev->devnum, iface_no, altno, as->bTerminalLink);
355                         continue;
356                 }
357                 }
358
359                 /* get format type */
360                 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
361                 if (!fmt) {
362                         snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
363                                    dev->devnum, iface_no, altno);
364                         continue;
365                 }
366                 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
367                     ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
368                         snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
369                                    dev->devnum, iface_no, altno);
370                         continue;
371                 }
372
373                 /*
374                  * Blue Microphones workaround: The last altsetting is identical
375                  * with the previous one, except for a larger packet size, but
376                  * is actually a mislabeled two-channel setting; ignore it.
377                  */
378                 if (fmt->bNrChannels == 1 &&
379                     fmt->bSubframeSize == 2 &&
380                     altno == 2 && num == 3 &&
381                     fp && fp->altsetting == 1 && fp->channels == 1 &&
382                     fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
383                     protocol == UAC_VERSION_1 &&
384                     le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
385                                                         fp->maxpacksize * 2)
386                         continue;
387
388                 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
389                 if (! fp) {
390                         snd_printk(KERN_ERR "cannot malloc\n");
391                         return -ENOMEM;
392                 }
393
394                 fp->iface = iface_no;
395                 fp->altsetting = altno;
396                 fp->altset_idx = i;
397                 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
398                 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
399                 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
400                 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
401                 /* num_channels is only set for v2 interfaces */
402                 fp->channels = num_channels;
403                 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
404                         fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
405                                         * (fp->maxpacksize & 0x7ff);
406                 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
407                 fp->clock = clock;
408                 INIT_LIST_HEAD(&fp->list);
409
410                 /* some quirks for attributes here */
411
412                 switch (chip->usb_id) {
413                 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
414                         /* Optoplay sets the sample rate attribute although
415                          * it seems not supporting it in fact.
416                          */
417                         fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
418                         break;
419                 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
420                 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
421                         /* doesn't set the sample rate attribute, but supports it */
422                         fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
423                         break;
424                 case USB_ID(0x0763, 0x2001):  /* M-Audio Quattro USB */
425                 case USB_ID(0x0763, 0x2012):  /* M-Audio Fast Track Pro USB */
426                 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
427                 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
428                                                 an older model 77d:223) */
429                 /*
430                  * plantronics headset and Griffin iMic have set adaptive-in
431                  * although it's really not...
432                  */
433                         fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
434                         if (stream == SNDRV_PCM_STREAM_PLAYBACK)
435                                 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
436                         else
437                                 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
438                         break;
439                 }
440
441                 /* ok, let's parse further... */
442                 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
443                         kfree(fp->rate_table);
444                         kfree(fp);
445                         fp = NULL;
446                         continue;
447                 }
448
449                 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
450                 err = snd_usb_add_audio_stream(chip, stream, fp);
451                 if (err < 0) {
452                         list_del(&fp->list); /* unlink for avoiding double-free */
453                         kfree(fp->rate_table);
454                         kfree(fp);
455                         return err;
456                 }
457                 /* try to set the interface... */
458                 usb_set_interface(chip->dev, iface_no, altno);
459                 snd_usb_init_pitch(chip, iface_no, alts, fp);
460                 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
461         }
462         return 0;
463 }
464