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