Commit | Line | Data |
---|---|---|
e5779998 DM |
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/usb.h> | |
20 | #include <linux/usb/audio.h> | |
7e847894 | 21 | #include <linux/usb/audio-v2.h> |
e5779998 DM |
22 | |
23 | #include <sound/core.h> | |
24 | #include <sound/pcm.h> | |
25 | ||
26 | #include "usbaudio.h" | |
27 | #include "card.h" | |
28 | #include "proc.h" | |
29 | #include "quirks.h" | |
30 | #include "endpoint.h" | |
31 | #include "urb.h" | |
32 | #include "pcm.h" | |
33 | #include "helper.h" | |
34 | #include "format.h" | |
35 | ||
36 | /* | |
37 | * free a substream | |
38 | */ | |
39 | static void free_substream(struct snd_usb_substream *subs) | |
40 | { | |
41 | struct list_head *p, *n; | |
42 | ||
43 | if (!subs->num_formats) | |
44 | return; /* not initialized */ | |
45 | list_for_each_safe(p, n, &subs->fmt_list) { | |
46 | struct audioformat *fp = list_entry(p, struct audioformat, list); | |
47 | kfree(fp->rate_table); | |
48 | kfree(fp); | |
49 | } | |
50 | kfree(subs->rate_list.list); | |
51 | } | |
52 | ||
53 | ||
54 | /* | |
55 | * free a usb stream instance | |
56 | */ | |
57 | static void snd_usb_audio_stream_free(struct snd_usb_stream *stream) | |
58 | { | |
59 | free_substream(&stream->substream[0]); | |
60 | free_substream(&stream->substream[1]); | |
61 | list_del(&stream->list); | |
62 | kfree(stream); | |
63 | } | |
64 | ||
65 | static void snd_usb_audio_pcm_free(struct snd_pcm *pcm) | |
66 | { | |
67 | struct snd_usb_stream *stream = pcm->private_data; | |
68 | if (stream) { | |
69 | stream->pcm = NULL; | |
70 | snd_usb_audio_stream_free(stream); | |
71 | } | |
72 | } | |
73 | ||
74 | ||
75 | /* | |
76 | * add this endpoint to the chip instance. | |
77 | * if a stream with the same endpoint already exists, append to it. | |
78 | * if not, create a new pcm stream. | |
79 | */ | |
80 | int snd_usb_add_audio_endpoint(struct snd_usb_audio *chip, int stream, struct audioformat *fp) | |
81 | { | |
82 | struct list_head *p; | |
83 | struct snd_usb_stream *as; | |
84 | struct snd_usb_substream *subs; | |
85 | struct snd_pcm *pcm; | |
86 | int err; | |
87 | ||
88 | list_for_each(p, &chip->pcm_list) { | |
89 | as = list_entry(p, struct snd_usb_stream, list); | |
90 | if (as->fmt_type != fp->fmt_type) | |
91 | continue; | |
92 | subs = &as->substream[stream]; | |
93 | if (!subs->endpoint) | |
94 | continue; | |
95 | if (subs->endpoint == fp->endpoint) { | |
96 | list_add_tail(&fp->list, &subs->fmt_list); | |
97 | subs->num_formats++; | |
015eb0b0 | 98 | subs->formats |= fp->formats; |
e5779998 DM |
99 | return 0; |
100 | } | |
101 | } | |
102 | /* look for an empty stream */ | |
103 | list_for_each(p, &chip->pcm_list) { | |
104 | as = list_entry(p, struct snd_usb_stream, list); | |
105 | if (as->fmt_type != fp->fmt_type) | |
106 | continue; | |
107 | subs = &as->substream[stream]; | |
108 | if (subs->endpoint) | |
109 | continue; | |
110 | err = snd_pcm_new_stream(as->pcm, stream, 1); | |
111 | if (err < 0) | |
112 | return err; | |
113 | snd_usb_init_substream(as, stream, fp); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | /* create a new pcm */ | |
118 | as = kzalloc(sizeof(*as), GFP_KERNEL); | |
119 | if (!as) | |
120 | return -ENOMEM; | |
121 | as->pcm_index = chip->pcm_devs; | |
122 | as->chip = chip; | |
123 | as->fmt_type = fp->fmt_type; | |
124 | err = snd_pcm_new(chip->card, "USB Audio", chip->pcm_devs, | |
125 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0, | |
126 | stream == SNDRV_PCM_STREAM_PLAYBACK ? 0 : 1, | |
127 | &pcm); | |
128 | if (err < 0) { | |
129 | kfree(as); | |
130 | return err; | |
131 | } | |
132 | as->pcm = pcm; | |
133 | pcm->private_data = as; | |
134 | pcm->private_free = snd_usb_audio_pcm_free; | |
135 | pcm->info_flags = 0; | |
136 | if (chip->pcm_devs > 0) | |
137 | sprintf(pcm->name, "USB Audio #%d", chip->pcm_devs); | |
138 | else | |
139 | strcpy(pcm->name, "USB Audio"); | |
140 | ||
141 | snd_usb_init_substream(as, stream, fp); | |
142 | ||
143 | list_add(&as->list, &chip->pcm_list); | |
144 | chip->pcm_devs++; | |
145 | ||
146 | snd_usb_proc_pcm_format_add(as); | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
151 | int snd_usb_parse_audio_endpoints(struct snd_usb_audio *chip, int iface_no) | |
152 | { | |
153 | struct usb_device *dev; | |
154 | struct usb_interface *iface; | |
155 | struct usb_host_interface *alts; | |
156 | struct usb_interface_descriptor *altsd; | |
157 | int i, altno, err, stream; | |
158 | int format = 0, num_channels = 0; | |
159 | struct audioformat *fp = NULL; | |
160 | unsigned char *fmt, *csep; | |
161 | int num, protocol; | |
162 | ||
163 | dev = chip->dev; | |
164 | ||
165 | /* parse the interface's altsettings */ | |
166 | iface = usb_ifnum_to_if(dev, iface_no); | |
167 | ||
168 | num = iface->num_altsetting; | |
169 | ||
170 | /* | |
171 | * Dallas DS4201 workaround: It presents 5 altsettings, but the last | |
172 | * one misses syncpipe, and does not produce any sound. | |
173 | */ | |
174 | if (chip->usb_id == USB_ID(0x04fa, 0x4201)) | |
175 | num = 4; | |
176 | ||
177 | for (i = 0; i < num; i++) { | |
178 | alts = &iface->altsetting[i]; | |
179 | altsd = get_iface_desc(alts); | |
180 | protocol = altsd->bInterfaceProtocol; | |
181 | /* skip invalid one */ | |
182 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
183 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
184 | (altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING && | |
185 | altsd->bInterfaceSubClass != USB_SUBCLASS_VENDOR_SPEC) || | |
186 | altsd->bNumEndpoints < 1 || | |
187 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == 0) | |
188 | continue; | |
189 | /* must be isochronous */ | |
190 | if ((get_endpoint(alts, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != | |
191 | USB_ENDPOINT_XFER_ISOC) | |
192 | continue; | |
193 | /* check direction */ | |
194 | stream = (get_endpoint(alts, 0)->bEndpointAddress & USB_DIR_IN) ? | |
195 | SNDRV_PCM_STREAM_CAPTURE : SNDRV_PCM_STREAM_PLAYBACK; | |
196 | altno = altsd->bAlternateSetting; | |
197 | ||
198 | if (snd_usb_apply_interface_quirk(chip, iface_no, altno)) | |
199 | continue; | |
200 | ||
201 | /* get audio formats */ | |
202 | switch (protocol) { | |
203 | case UAC_VERSION_1: { | |
204 | struct uac_as_header_descriptor_v1 *as = | |
205 | snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); | |
206 | ||
207 | if (!as) { | |
208 | snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n", | |
209 | dev->devnum, iface_no, altno); | |
210 | continue; | |
211 | } | |
212 | ||
213 | if (as->bLength < sizeof(*as)) { | |
214 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", | |
215 | dev->devnum, iface_no, altno); | |
216 | continue; | |
217 | } | |
218 | ||
219 | format = le16_to_cpu(as->wFormatTag); /* remember the format value */ | |
220 | break; | |
221 | } | |
222 | ||
223 | case UAC_VERSION_2: { | |
224 | struct uac_as_header_descriptor_v2 *as = | |
225 | snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_AS_GENERAL); | |
226 | ||
227 | if (!as) { | |
228 | snd_printk(KERN_ERR "%d:%u:%d : UAC_AS_GENERAL descriptor not found\n", | |
229 | dev->devnum, iface_no, altno); | |
230 | continue; | |
231 | } | |
232 | ||
233 | if (as->bLength < sizeof(*as)) { | |
234 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_AS_GENERAL desc\n", | |
235 | dev->devnum, iface_no, altno); | |
236 | continue; | |
237 | } | |
238 | ||
239 | num_channels = as->bNrChannels; | |
240 | format = le32_to_cpu(as->bmFormats); | |
241 | ||
242 | break; | |
243 | } | |
244 | ||
245 | default: | |
246 | snd_printk(KERN_ERR "%d:%u:%d : unknown interface protocol %04x\n", | |
247 | dev->devnum, iface_no, altno, protocol); | |
248 | continue; | |
249 | } | |
250 | ||
251 | /* get format type */ | |
252 | fmt = snd_usb_find_csint_desc(alts->extra, alts->extralen, NULL, UAC_FORMAT_TYPE); | |
253 | if (!fmt) { | |
254 | snd_printk(KERN_ERR "%d:%u:%d : no UAC_FORMAT_TYPE desc\n", | |
255 | dev->devnum, iface_no, altno); | |
256 | continue; | |
257 | } | |
258 | if (((protocol == UAC_VERSION_1) && (fmt[0] < 8)) || | |
259 | ((protocol == UAC_VERSION_2) && (fmt[0] != 6))) { | |
260 | snd_printk(KERN_ERR "%d:%u:%d : invalid UAC_FORMAT_TYPE desc\n", | |
261 | dev->devnum, iface_no, altno); | |
262 | continue; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Blue Microphones workaround: The last altsetting is identical | |
267 | * with the previous one, except for a larger packet size, but | |
268 | * is actually a mislabeled two-channel setting; ignore it. | |
269 | */ | |
270 | if (fmt[4] == 1 && fmt[5] == 2 && altno == 2 && num == 3 && | |
271 | fp && fp->altsetting == 1 && fp->channels == 1 && | |
015eb0b0 | 272 | fp->formats == SNDRV_PCM_FMTBIT_S16_LE && |
e5779998 DM |
273 | protocol == UAC_VERSION_1 && |
274 | le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize) == | |
275 | fp->maxpacksize * 2) | |
276 | continue; | |
277 | ||
278 | csep = snd_usb_find_desc(alts->endpoint[0].extra, alts->endpoint[0].extralen, NULL, USB_DT_CS_ENDPOINT); | |
279 | /* Creamware Noah has this descriptor after the 2nd endpoint */ | |
280 | if (!csep && altsd->bNumEndpoints >= 2) | |
281 | csep = snd_usb_find_desc(alts->endpoint[1].extra, alts->endpoint[1].extralen, NULL, USB_DT_CS_ENDPOINT); | |
282 | if (!csep || csep[0] < 7 || csep[2] != UAC_EP_GENERAL) { | |
283 | snd_printk(KERN_WARNING "%d:%u:%d : no or invalid" | |
284 | " class specific endpoint descriptor\n", | |
285 | dev->devnum, iface_no, altno); | |
286 | csep = NULL; | |
287 | } | |
288 | ||
289 | fp = kzalloc(sizeof(*fp), GFP_KERNEL); | |
290 | if (! fp) { | |
291 | snd_printk(KERN_ERR "cannot malloc\n"); | |
292 | return -ENOMEM; | |
293 | } | |
294 | ||
295 | fp->iface = iface_no; | |
296 | fp->altsetting = altno; | |
297 | fp->altset_idx = i; | |
298 | fp->endpoint = get_endpoint(alts, 0)->bEndpointAddress; | |
299 | fp->ep_attr = get_endpoint(alts, 0)->bmAttributes; | |
300 | fp->datainterval = snd_usb_parse_datainterval(chip, alts); | |
301 | fp->maxpacksize = le16_to_cpu(get_endpoint(alts, 0)->wMaxPacketSize); | |
302 | /* num_channels is only set for v2 interfaces */ | |
303 | fp->channels = num_channels; | |
304 | if (snd_usb_get_speed(dev) == USB_SPEED_HIGH) | |
305 | fp->maxpacksize = (((fp->maxpacksize >> 11) & 3) + 1) | |
306 | * (fp->maxpacksize & 0x7ff); | |
307 | fp->attributes = csep ? csep[3] : 0; | |
308 | ||
309 | /* some quirks for attributes here */ | |
310 | ||
311 | switch (chip->usb_id) { | |
312 | case USB_ID(0x0a92, 0x0053): /* AudioTrak Optoplay */ | |
313 | /* Optoplay sets the sample rate attribute although | |
314 | * it seems not supporting it in fact. | |
315 | */ | |
316 | fp->attributes &= ~UAC_EP_CS_ATTR_SAMPLE_RATE; | |
317 | break; | |
318 | case USB_ID(0x041e, 0x3020): /* Creative SB Audigy 2 NX */ | |
319 | case USB_ID(0x0763, 0x2003): /* M-Audio Audiophile USB */ | |
320 | /* doesn't set the sample rate attribute, but supports it */ | |
321 | fp->attributes |= UAC_EP_CS_ATTR_SAMPLE_RATE; | |
322 | break; | |
323 | case USB_ID(0x047f, 0x0ca1): /* plantronics headset */ | |
324 | case USB_ID(0x077d, 0x07af): /* Griffin iMic (note that there is | |
325 | an older model 77d:223) */ | |
326 | /* | |
327 | * plantronics headset and Griffin iMic have set adaptive-in | |
328 | * although it's really not... | |
329 | */ | |
330 | fp->ep_attr &= ~USB_ENDPOINT_SYNCTYPE; | |
331 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) | |
332 | fp->ep_attr |= USB_ENDPOINT_SYNC_ADAPTIVE; | |
333 | else | |
334 | fp->ep_attr |= USB_ENDPOINT_SYNC_SYNC; | |
335 | break; | |
336 | } | |
337 | ||
338 | /* ok, let's parse further... */ | |
339 | if (snd_usb_parse_audio_format(chip, fp, format, fmt, stream, alts) < 0) { | |
340 | kfree(fp->rate_table); | |
341 | kfree(fp); | |
342 | continue; | |
343 | } | |
344 | ||
345 | snd_printdd(KERN_INFO "%d:%u:%d: add audio endpoint %#x\n", dev->devnum, iface_no, altno, fp->endpoint); | |
346 | err = snd_usb_add_audio_endpoint(chip, stream, fp); | |
347 | if (err < 0) { | |
348 | kfree(fp->rate_table); | |
349 | kfree(fp); | |
350 | return err; | |
351 | } | |
352 | /* try to set the interface... */ | |
353 | usb_set_interface(chip->dev, iface_no, altno); | |
767d75ad DM |
354 | snd_usb_init_pitch(chip, iface_no, alts, fp); |
355 | snd_usb_init_sample_rate(chip, iface_no, alts, fp, fp->rate_max); | |
e5779998 DM |
356 | } |
357 | return 0; | |
358 | } | |
359 |