ixgbe: fix RAR entry counting for generic and fdb_add()
[linux-2.6/cjktty.git] / sound / usb / stream.c
blob083ed81160e58b094ee8f4fca637371aa5b5fff4
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.
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>
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"
39 * free a substream
41 static void free_substream(struct snd_usb_substream *subs)
43 struct list_head *p, *n;
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);
52 kfree(subs->rate_list.list);
57 * free a usb stream instance
59 static void snd_usb_audio_stream_free(struct snd_usb_stream *stream)
61 free_substream(&stream->substream[0]);
62 free_substream(&stream->substream[1]);
63 list_del(&stream->list);
64 kfree(stream);
67 static void snd_usb_audio_pcm_free(struct snd_pcm *pcm)
69 struct snd_usb_stream *stream = pcm->private_data;
70 if (stream) {
71 stream->pcm = NULL;
72 snd_usb_audio_stream_free(stream);
77 * initialize the substream instance.
80 static void snd_usb_init_substream(struct snd_usb_stream *as,
81 int stream,
82 struct audioformat *fp)
84 struct snd_usb_substream *subs = &as->substream[stream];
86 INIT_LIST_HEAD(&subs->fmt_list);
87 spin_lock_init(&subs->lock);
89 subs->stream = as;
90 subs->direction = stream;
91 subs->dev = as->chip->dev;
92 subs->txfr_quirk = as->chip->txfr_quirk;
94 snd_usb_set_pcm_ops(as->pcm, stream);
96 list_add_tail(&fp->list, &subs->fmt_list);
97 subs->formats |= fp->formats;
98 subs->num_formats++;
99 subs->fmt_type = fp->fmt_type;
100 subs->ep_num = fp->endpoint;
104 * add this endpoint to the chip instance.
105 * if a stream with the same endpoint already exists, append to it.
106 * if not, create a new pcm stream.
108 int snd_usb_add_audio_stream(struct snd_usb_audio *chip,
109 int stream,
110 struct audioformat *fp)
112 struct list_head *p;
113 struct snd_usb_stream *as;
114 struct snd_usb_substream *subs;
115 struct snd_pcm *pcm;
116 int err;
118 list_for_each(p, &chip->pcm_list) {
119 as = list_entry(p, struct snd_usb_stream, list);
120 if (as->fmt_type != fp->fmt_type)
121 continue;
122 subs = &as->substream[stream];
123 if (subs->ep_num == fp->endpoint) {
124 list_add_tail(&fp->list, &subs->fmt_list);
125 subs->num_formats++;
126 subs->formats |= fp->formats;
127 return 0;
130 /* look for an empty stream */
131 list_for_each(p, &chip->pcm_list) {
132 as = list_entry(p, struct snd_usb_stream, list);
133 if (as->fmt_type != fp->fmt_type)
134 continue;
135 subs = &as->substream[stream];
136 if (subs->ep_num)
137 continue;
138 err = snd_pcm_new_stream(as->pcm, stream, 1);
139 if (err < 0)
140 return err;
141 snd_usb_init_substream(as, stream, fp);
142 return 0;
145 /* create a new pcm */
146 as = kzalloc(sizeof(*as), GFP_KERNEL);
147 if (!as)
148 return -ENOMEM;
149 as->pcm_index = chip->pcm_devs;
150 as->chip = chip;
151 as->fmt_type = fp->fmt_type;
152 err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs,
153 stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0,
154 stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1,
155 &pcm);
156 if (err < 0) {
157 kfree(as);
158 return err;
160 as->pcm = pcm;
161 pcm->private_data = as;
162 pcm->private_free = snd_usb_audio_pcm_free;
163 pcm->info_flags = 0;
164 if (chip->pcm_devs > 0)
165 sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs);
166 else
167 strcpy(pcm->name, "USB Audio");
169 snd_usb_init_substream(as, stream, fp);
171 list_add(&as->list, &chip->pcm_list);
172 chip->pcm_devs++;
174 snd_usb_proc_pcm_format_add(as);
176 return 0;
179 static int parse_uac_endpoint_attributes(struct snd_usb_audio *chip,
180 struct usb_host_interface *alts,
181 int protocol, int iface_no)
183 /* parsed with a v1 header here. that's ok as we only look at the
184 * header first which is the same for both versions */
185 struct uac_iso_endpoint_descriptor *csep;
186 struct usb_interface_descriptor *altsd = get_iface_desc(alts);
187 int attributes = 0;
189 csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT);
191 /* Creamware Noah has this descriptor after the 2nd endpoint */
192 if (!csep && altsd->bNumEndpoints >= 2)
193 csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT);
195 if (!csep || csep->bLength < 7 ||
196 csep->bDescriptorSubtype != UAC_EP_GENERAL) {
197 snd_printk(KERN_WARNING "%d:%u:%d : no or invalid"
198 " class specific endpoint descriptor\n",
199 chip->dev->devnum, iface_no,
200 altsd->bAlternateSetting);
201 return 0;
204 if (protocol == UAC_VERSION_1) {
205 attributes = csep->bmAttributes;
206 } else {
207 struct uac2_iso_endpoint_descriptor *csep2 =
208 (struct uac2_iso_endpoint_descriptor *) csep;
210 attributes = csep->bmAttributes & UAC_EP_CS_ATTR_FILL_MAX;
212 /* emulate the endpoint attributes of a v1 device */
213 if (csep2->bmControls & UAC2_CONTROL_PITCH)
214 attributes |= UAC_EP_CS_ATTR_PITCH_CONTROL;
217 return attributes;
220 static struct uac2_input_terminal_descriptor *
221 snd_usb_find_input_terminal_descriptor(struct usb_host_interface *ctrl_iface,
222 int terminal_id)
224 struct uac2_input_terminal_descriptor *term = NULL;
226 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
227 ctrl_iface->extralen,
228 term, UAC_INPUT_TERMINAL))) {
229 if (term->bTerminalID == terminal_id)
230 return term;
233 return NULL;
236 static struct uac2_output_terminal_descriptor *
237 snd_usb_find_output_terminal_descriptor(struct usb_host_interface *ctrl_iface,
238 int terminal_id)
240 struct uac2_output_terminal_descriptor *term = NULL;
242 while ((term = snd_usb_find_csint_desc(ctrl_iface->extra,
243 ctrl_iface->extralen,
244 term, UAC_OUTPUT_TERMINAL))) {
245 if (term->bTerminalID == terminal_id)
246 return term;
249 return NULL;
252 int snd_usb_parse_audio_interface(struct snd_usb_audio *chip, int iface_no)
254 struct usb_device *dev;
255 struct usb_interface *iface;
256 struct usb_host_interface *alts;
257 struct usb_interface_descriptor *altsd;
258 int i, altno, err, stream;
259 int format = 0, num_channels = 0;
260 struct audioformat *fp = NULL;
261 int num, protocol, clock = 0;
262 struct uac_format_type_i_continuous_descriptor *fmt;
264 dev = chip->dev;
266 /* parse the interface's altsettings */
267 iface = usb_ifnum_to_if(dev, iface_no);
269 num = iface->num_altsetting;
272 * Dallas DS4201 workaround: It presents 5 altsettings, but the last
273 * one misses syncpipe, and does not produce any sound.
275 if (chip->usb_id == USB_ID(0x04fa, 0x4201))
276 num = 4;
278 for (i = 0; i < num; i++) {
279 alts = &iface->altsetting[i];
280 altsd = get_iface_desc(alts);
281 protocol = altsd->bInterfaceProtocol;
282 /* skip invalid one */
283 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
284 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
285 (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING &&
286 altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) ||
287 altsd->bNumEndpoints < 1 ||
288 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0)
289 continue;
290 /* must be isochronous */
291 if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
292 USB_ENDPOINT_XFER_ISOC)
293 continue;
294 /* check direction */
295 stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ?
296 SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK;
297 altno = altsd->bAlternateSetting;
299 if (snd_usb_apply_interface_quirk(chip, iface_no, altno))
300 continue;
302 /* get audio formats */
303 switch (protocol) {
304 default:
305 snd_printdd(KERN_WARNING "%d:%u:%d: unknown interface protocol %#02x, assuming v1\n",
306 dev->devnum, iface_no, altno, protocol);
307 protocol = UAC_VERSION_1;
308 /* fall through */
310 case UAC_VERSION_1: {
311 struct uac1_as_header_descriptor *as =
312 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
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;
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;
326 format = le16_to_cpu(as->wFormatTag); /* remember the format value */
327 break;
330 case UAC_VERSION_2: {
331 struct uac2_input_terminal_descriptor *input_term;
332 struct uac2_output_terminal_descriptor *output_term;
333 struct uac2_as_header_descriptor *as =
334 snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL);
336 if (!as) {
337 snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n",
338 dev->devnum, iface_no, altno);
339 continue;
342 if (as->bLength < sizeof(*as)) {
343 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n",
344 dev->devnum, iface_no, altno);
345 continue;
348 num_channels = as->bNrChannels;
349 format = le32_to_cpu(as->bmFormats);
351 /* lookup the terminal associated to this interface
352 * to extract the clock */
353 input_term = snd_usb_find_input_terminal_descriptor(chip->ctrl_intf,
354 as->bTerminalLink);
355 if (input_term) {
356 clock = input_term->bCSourceID;
357 break;
360 output_term = snd_usb_find_output_terminal_descriptor(chip->ctrl_intf,
361 as->bTerminalLink);
362 if (output_term) {
363 clock = output_term->bCSourceID;
364 break;
367 snd_printk(KERN_ERR "%d:%u:%d : bogus bTerminalLink %d\n",
368 dev->devnum, iface_no, altno, as->bTerminalLink);
369 continue;
373 /* get format type */
374 fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE);
375 if (!fmt) {
376 snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n",
377 dev->devnum, iface_no, altno);
378 continue;
380 if (((protocol == UAC_VERSION_1) && (fmt->bLength < 8)) ||
381 ((protocol == UAC_VERSION_2) && (fmt->bLength < 6))) {
382 snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n",
383 dev->devnum, iface_no, altno);
384 continue;
388 * Blue Microphones workaround: The last altsetting is identical
389 * with the previous one, except for a larger packet size, but
390 * is actually a mislabeled two-channel setting; ignore it.
392 if (fmt->bNrChannels == 1 &&
393 fmt->bSubframeSize == 2 &&
394 altno == 2 && num == 3 &&
395 fp && fp->altsetting == 1 && fp->channels == 1 &&
396 fp->formats == SNDRV_PCM_FMTBIT_S16_LE &&
397 protocol == UAC_VERSION_1 &&
398 le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) ==
399 fp->maxpacksize * 2)
400 continue;
402 fp = kzalloc(sizeof(*fp), GFP_KERNEL);
403 if (! fp) {
404 snd_printk(KERN_ERR "cannot malloc\n");
405 return -ENOMEM;
408 fp->iface = iface_no;
409 fp->altsetting = altno;
410 fp->altset_idx = i;
411 fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress;
412 fp->ep_attr = get_endpoint(alts, 0)->bmAttributes;
413 fp->datainterval = snd_usb_parse_datainterval(chip, alts);
414 fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize);
415 /* num_channels is only set for v2 interfaces */
416 fp->channels = num_channels;
417 if (snd_usb_get_speed(dev) == USB_SPEED_HIGH)
418 fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1)
419 * (fp->maxpacksize & 0x7ff);
420 fp->attributes = parse_uac_endpoint_attributes(chip, alts, protocol, iface_no);
421 fp->clock = clock;
423 /* some quirks for attributes here */
425 switch (chip->usb_id) {
426 case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */
427 /* Optoplay sets the sample rate attribute although
428 * it seems not supporting it in fact.
430 fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE;
431 break;
432 case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */
433 case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */
434 /* doesn't set the sample rate attribute, but supports it */
435 fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE;
436 break;
437 case USB_ID(0x0763, 0x2001): /* M-Audio Quattro USB */
438 case USB_ID(0x0763, 0x2012): /* M-Audio Fast Track Pro USB */
439 case USB_ID(0x047f, 0x0ca1): /* plantronics headset */
440 case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is
441 an older model 77d:223) */
443 * plantronics headset and Griffin iMic have set adaptive-in
444 * although it's really not...
446 fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE;
447 if (stream == SNDRV_PCM_STREAM_PLAYBACK)
448 fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE;
449 else
450 fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC;
451 break;
454 /* ok, let's parse further... */
455 if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) {
456 kfree(fp->rate_table);
457 kfree(fp);
458 fp = NULL;
459 continue;
462 snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint);
463 err = snd_usb_add_audio_stream(chip, stream, fp);
464 if (err < 0) {
465 kfree(fp->rate_table);
466 kfree(fp);
467 return err;
469 /* try to set the interface... */
470 usb_set_interface(chip->dev, iface_no, altno);
471 snd_usb_init_pitch(chip, iface_no, alts, fp);
472 snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max);
474 return 0;