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