1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * (Tentative) USB Audio Driver for ALSA 4 * 5 * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> 6 * 7 * Many codes borrowed from audio.c by 8 * Alan Cox (alan@lxorguk.ukuu.org.uk) 9 * Thomas Sailer (sailer@ife.ee.ethz.ch) 10 * 11 * Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com> 12 * 13 * NOTES: 14 * 15 * - the linked URBs would be preferred but not used so far because of 16 * the instability of unlinking. 17 * - type II is not supported properly. there is no device which supports 18 * this type *correctly*. SB extigy looks as if it supports, but it's 19 * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). 20 */ 21 22 23 #include <linux/bitops.h> 24 #include <linux/init.h> 25 #include <linux/list.h> 26 #include <linux/slab.h> 27 #include <linux/string.h> 28 #include <linux/ctype.h> 29 #include <linux/usb.h> 30 #include <linux/moduleparam.h> 31 #include <linux/mutex.h> 32 #include <linux/usb/audio.h> 33 #include <linux/usb/audio-v2.h> 34 #include <linux/usb/audio-v3.h> 35 #include <linux/module.h> 36 37 #include <sound/control.h> 38 #include <sound/core.h> 39 #include <sound/info.h> 40 #include <sound/pcm.h> 41 #include <sound/pcm_params.h> 42 #include <sound/initval.h> 43 44 #include "usbaudio.h" 45 #include "card.h" 46 #include "midi.h" 47 #include "midi2.h" 48 #include "mixer.h" 49 #include "proc.h" 50 #include "quirks.h" 51 #include "endpoint.h" 52 #include "helper.h" 53 #include "pcm.h" 54 #include "format.h" 55 #include "power.h" 56 #include "stream.h" 57 #include "media.h" 58 59 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); 60 MODULE_DESCRIPTION("USB Audio"); 61 MODULE_LICENSE("GPL"); 62 63 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ 64 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ 65 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ 66 /* Vendor/product IDs for this card */ 67 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; 68 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; 69 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */ 70 static bool ignore_ctl_error; 71 static bool autoclock = true; 72 static bool lowlatency = true; 73 static char *quirk_alias[SNDRV_CARDS]; 74 static char *delayed_register[SNDRV_CARDS]; 75 static bool implicit_fb[SNDRV_CARDS]; 76 static unsigned int quirk_flags[SNDRV_CARDS]; 77 78 bool snd_usb_use_vmalloc = true; 79 bool snd_usb_skip_validation; 80 81 module_param_array(index, int, NULL, 0444); 82 MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); 83 module_param_array(id, charp, NULL, 0444); 84 MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); 85 module_param_array(enable, bool, NULL, 0444); 86 MODULE_PARM_DESC(enable, "Enable USB audio adapter."); 87 module_param_array(vid, int, NULL, 0444); 88 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); 89 module_param_array(pid, int, NULL, 0444); 90 MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); 91 module_param_array(device_setup, int, NULL, 0444); 92 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); 93 module_param(ignore_ctl_error, bool, 0444); 94 MODULE_PARM_DESC(ignore_ctl_error, 95 "Ignore errors from USB controller for mixer interfaces."); 96 module_param(autoclock, bool, 0444); 97 MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes)."); 98 module_param(lowlatency, bool, 0444); 99 MODULE_PARM_DESC(lowlatency, "Enable low latency playback (default: yes)."); 100 module_param_array(quirk_alias, charp, NULL, 0444); 101 MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef."); 102 module_param_array(delayed_register, charp, NULL, 0444); 103 MODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4."); 104 module_param_array(implicit_fb, bool, NULL, 0444); 105 MODULE_PARM_DESC(implicit_fb, "Apply generic implicit feedback sync mode."); 106 module_param_array(quirk_flags, uint, NULL, 0444); 107 MODULE_PARM_DESC(quirk_flags, "Driver quirk bit flags."); 108 module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444); 109 MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes)."); 110 module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444); 111 MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)."); 112 113 /* 114 * we keep the snd_usb_audio_t instances by ourselves for merging 115 * the all interfaces on the same card as one sound device. 116 */ 117 118 static DEFINE_MUTEX(register_mutex); 119 static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; 120 static struct usb_driver usb_audio_driver; 121 122 /* 123 * disconnect streams 124 * called from usb_audio_disconnect() 125 */ 126 static void snd_usb_stream_disconnect(struct snd_usb_stream *as) 127 { 128 int idx; 129 struct snd_usb_substream *subs; 130 131 for (idx = 0; idx < 2; idx++) { 132 subs = &as->substream[idx]; 133 if (!subs->num_formats) 134 continue; 135 subs->data_endpoint = NULL; 136 subs->sync_endpoint = NULL; 137 } 138 } 139 140 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) 141 { 142 struct usb_device *dev = chip->dev; 143 struct usb_host_interface *alts; 144 struct usb_interface_descriptor *altsd; 145 struct usb_interface *iface = usb_ifnum_to_if(dev, interface); 146 147 if (!iface) { 148 dev_err(&dev->dev, "%u:%d : does not exist\n", 149 ctrlif, interface); 150 return -EINVAL; 151 } 152 153 alts = &iface->altsetting[0]; 154 altsd = get_iface_desc(alts); 155 156 /* 157 * Android with both accessory and audio interfaces enabled gets the 158 * interface numbers wrong. 159 */ 160 if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || 161 chip->usb_id == USB_ID(0x18d1, 0x2d05)) && 162 interface == 0 && 163 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && 164 altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { 165 interface = 2; 166 iface = usb_ifnum_to_if(dev, interface); 167 if (!iface) 168 return -EINVAL; 169 alts = &iface->altsetting[0]; 170 altsd = get_iface_desc(alts); 171 } 172 173 if (usb_interface_claimed(iface)) { 174 dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n", 175 ctrlif, interface); 176 return -EINVAL; 177 } 178 179 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || 180 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && 181 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { 182 int err = snd_usb_midi_v2_create(chip, iface, NULL, 183 chip->usb_id); 184 if (err < 0) { 185 dev_err(&dev->dev, 186 "%u:%d: cannot create sequencer device\n", 187 ctrlif, interface); 188 return -EINVAL; 189 } 190 return usb_driver_claim_interface(&usb_audio_driver, iface, 191 USB_AUDIO_IFACE_UNUSED); 192 } 193 194 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && 195 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || 196 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { 197 dev_dbg(&dev->dev, 198 "%u:%d: skipping non-supported interface %d\n", 199 ctrlif, interface, altsd->bInterfaceClass); 200 /* skip non-supported classes */ 201 return -EINVAL; 202 } 203 204 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { 205 dev_err(&dev->dev, "low speed audio streaming not supported\n"); 206 return -EINVAL; 207 } 208 209 if (! snd_usb_parse_audio_interface(chip, interface)) { 210 usb_set_interface(dev, interface, 0); /* reset the current interface */ 211 return usb_driver_claim_interface(&usb_audio_driver, iface, 212 USB_AUDIO_IFACE_UNUSED); 213 } 214 215 return 0; 216 } 217 218 /* 219 * parse audio control descriptor and create pcm/midi streams 220 */ 221 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) 222 { 223 struct usb_device *dev = chip->dev; 224 struct usb_host_interface *host_iface; 225 struct usb_interface_descriptor *altsd; 226 int i, protocol; 227 228 /* find audiocontrol interface */ 229 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; 230 altsd = get_iface_desc(host_iface); 231 protocol = altsd->bInterfaceProtocol; 232 233 switch (protocol) { 234 default: 235 dev_warn(&dev->dev, 236 "unknown interface protocol %#02x, assuming v1\n", 237 protocol); 238 fallthrough; 239 240 case UAC_VERSION_1: { 241 struct uac1_ac_header_descriptor *h1; 242 int rest_bytes; 243 244 h1 = snd_usb_find_csint_desc(host_iface->extra, 245 host_iface->extralen, 246 NULL, UAC_HEADER); 247 if (!h1 || h1->bLength < sizeof(*h1)) { 248 dev_err(&dev->dev, "cannot find UAC_HEADER\n"); 249 return -EINVAL; 250 } 251 252 rest_bytes = (void *)(host_iface->extra + 253 host_iface->extralen) - (void *)h1; 254 255 /* just to be sure -- this shouldn't hit at all */ 256 if (rest_bytes <= 0) { 257 dev_err(&dev->dev, "invalid control header\n"); 258 return -EINVAL; 259 } 260 261 if (rest_bytes < sizeof(*h1)) { 262 dev_err(&dev->dev, "too short v1 buffer descriptor\n"); 263 return -EINVAL; 264 } 265 266 if (!h1->bInCollection) { 267 dev_info(&dev->dev, "skipping empty audio interface (v1)\n"); 268 return -EINVAL; 269 } 270 271 if (rest_bytes < h1->bLength) { 272 dev_err(&dev->dev, "invalid buffer length (v1)\n"); 273 return -EINVAL; 274 } 275 276 if (h1->bLength < sizeof(*h1) + h1->bInCollection) { 277 dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n"); 278 return -EINVAL; 279 } 280 281 for (i = 0; i < h1->bInCollection; i++) 282 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); 283 284 break; 285 } 286 287 case UAC_VERSION_2: 288 case UAC_VERSION_3: { 289 struct usb_interface_assoc_descriptor *assoc = 290 usb_ifnum_to_if(dev, ctrlif)->intf_assoc; 291 292 if (!assoc) { 293 /* 294 * Firmware writers cannot count to three. So to find 295 * the IAD on the NuForce UDH-100, also check the next 296 * interface. 297 */ 298 struct usb_interface *iface = 299 usb_ifnum_to_if(dev, ctrlif + 1); 300 if (iface && 301 iface->intf_assoc && 302 iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && 303 iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) 304 assoc = iface->intf_assoc; 305 } 306 307 if (!assoc) { 308 dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n"); 309 return -EINVAL; 310 } 311 312 if (protocol == UAC_VERSION_3) { 313 int badd = assoc->bFunctionSubClass; 314 315 if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 && 316 (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO || 317 badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) { 318 dev_err(&dev->dev, 319 "Unsupported UAC3 BADD profile\n"); 320 return -EINVAL; 321 } 322 323 chip->badd_profile = badd; 324 } 325 326 for (i = 0; i < assoc->bInterfaceCount; i++) { 327 int intf = assoc->bFirstInterface + i; 328 329 if (intf != ctrlif) 330 snd_usb_create_stream(chip, ctrlif, intf); 331 } 332 333 break; 334 } 335 } 336 337 return 0; 338 } 339 340 /* 341 * Profile name preset table 342 */ 343 struct usb_audio_device_name { 344 u32 id; 345 const char *vendor_name; 346 const char *product_name; 347 const char *profile_name; /* override card->longname */ 348 }; 349 350 #define PROFILE_NAME(vid, pid, vendor, product, profile) \ 351 { .id = USB_ID(vid, pid), .vendor_name = (vendor), \ 352 .product_name = (product), .profile_name = (profile) } 353 #define DEVICE_NAME(vid, pid, vendor, product) \ 354 PROFILE_NAME(vid, pid, vendor, product, NULL) 355 356 /* vendor/product and profile name presets, sorted in device id order */ 357 static const struct usb_audio_device_name usb_audio_names[] = { 358 /* HP Thunderbolt Dock Audio Headset */ 359 PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset", 360 "HP-Thunderbolt-Dock-Audio-Headset"), 361 /* HP Thunderbolt Dock Audio Module */ 362 PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module", 363 "HP-Thunderbolt-Dock-Audio-Module"), 364 365 /* Two entries for Gigabyte TRX40 Aorus Master: 366 * TRX40 Aorus Master has two USB-audio devices, one for the front 367 * headphone with ESS SABRE9218 DAC chip, while another for the rest 368 * I/O (the rear panel and the front mic) with Realtek ALC1220-VB. 369 * Here we provide two distinct names for making UCM profiles easier. 370 */ 371 PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone", 372 "Gigabyte-Aorus-Master-Front-Headphone"), 373 PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio", 374 "Gigabyte-Aorus-Master-Main-Audio"), 375 376 /* Gigabyte TRX40 Aorus Pro WiFi */ 377 PROFILE_NAME(0x0414, 0xa002, 378 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 379 380 /* Creative/E-Mu devices */ 381 DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"), 382 /* Creative/Toshiba Multimedia Center SB-0500 */ 383 DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"), 384 385 /* Logitech Audio Devices */ 386 DEVICE_NAME(0x046d, 0x0867, "Logitech, Inc.", "Logi-MeetUp"), 387 DEVICE_NAME(0x046d, 0x0874, "Logitech, Inc.", "Logi-Tap-Audio"), 388 DEVICE_NAME(0x046d, 0x087c, "Logitech, Inc.", "Logi-Huddle"), 389 DEVICE_NAME(0x046d, 0x0898, "Logitech, Inc.", "Logi-RB-Audio"), 390 DEVICE_NAME(0x046d, 0x08d2, "Logitech, Inc.", "Logi-RBM-Audio"), 391 DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"), 392 393 DEVICE_NAME(0x05e1, 0x0408, "Syntek", "STK1160"), 394 DEVICE_NAME(0x05e1, 0x0480, "Hauppauge", "Woodbury"), 395 396 /* ASUS ROG Zenith II: this machine has also two devices, one for 397 * the front headphone and another for the rest 398 */ 399 PROFILE_NAME(0x0b05, 0x1915, "ASUS", "Zenith II Front Headphone", 400 "Zenith-II-Front-Headphone"), 401 PROFILE_NAME(0x0b05, 0x1916, "ASUS", "Zenith II Main Audio", 402 "Zenith-II-Main-Audio"), 403 404 /* ASUS ROG Strix */ 405 PROFILE_NAME(0x0b05, 0x1917, 406 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 407 /* ASUS PRIME TRX40 PRO-S */ 408 PROFILE_NAME(0x0b05, 0x1918, 409 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 410 411 /* Dell WD15 Dock */ 412 PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"), 413 /* Dell WD19 Dock */ 414 PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"), 415 416 DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"), 417 418 /* 419 * The original product_name is "USB Sound Device", however this name 420 * is also used by the CM106 based cards, so make it unique. 421 */ 422 DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"), 423 DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"), 424 425 /* MSI TRX40 Creator */ 426 PROFILE_NAME(0x0db0, 0x0d64, 427 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 428 /* MSI TRX40 */ 429 PROFILE_NAME(0x0db0, 0x543d, 430 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 431 432 DEVICE_NAME(0x0fd9, 0x0008, "Hauppauge", "HVR-950Q"), 433 434 /* Dock/Stand for HP Engage Go */ 435 PROFILE_NAME(0x103c, 0x830a, "HP", "HP Engage Go Dock", 436 "HP-Engage-Go-Dock"), 437 438 /* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */ 439 DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"), 440 DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"), 441 442 /* aka. Serato Scratch Live DJ Box */ 443 DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"), 444 445 /* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */ 446 PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear", 447 "Lenovo-ThinkStation-P620-Rear"), 448 /* Lenovo ThinkStation P620 Internal Speaker + Front Headset */ 449 PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main", 450 "Lenovo-ThinkStation-P620-Main"), 451 452 /* Asrock TRX40 Creator */ 453 PROFILE_NAME(0x26ce, 0x0a01, 454 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 455 456 DEVICE_NAME(0x2040, 0x7200, "Hauppauge", "HVR-950Q"), 457 DEVICE_NAME(0x2040, 0x7201, "Hauppauge", "HVR-950Q-MXL"), 458 DEVICE_NAME(0x2040, 0x7210, "Hauppauge", "HVR-950Q"), 459 DEVICE_NAME(0x2040, 0x7211, "Hauppauge", "HVR-950Q-MXL"), 460 DEVICE_NAME(0x2040, 0x7213, "Hauppauge", "HVR-950Q"), 461 DEVICE_NAME(0x2040, 0x7217, "Hauppauge", "HVR-950Q"), 462 DEVICE_NAME(0x2040, 0x721b, "Hauppauge", "HVR-950Q"), 463 DEVICE_NAME(0x2040, 0x721e, "Hauppauge", "HVR-950Q"), 464 DEVICE_NAME(0x2040, 0x721f, "Hauppauge", "HVR-950Q"), 465 DEVICE_NAME(0x2040, 0x7240, "Hauppauge", "HVR-850"), 466 DEVICE_NAME(0x2040, 0x7260, "Hauppauge", "HVR-950Q"), 467 DEVICE_NAME(0x2040, 0x7270, "Hauppauge", "HVR-950Q"), 468 DEVICE_NAME(0x2040, 0x7280, "Hauppauge", "HVR-950Q"), 469 DEVICE_NAME(0x2040, 0x7281, "Hauppauge", "HVR-950Q-MXL"), 470 DEVICE_NAME(0x2040, 0x8200, "Hauppauge", "Woodbury"), 471 472 { } /* terminator */ 473 }; 474 475 static const struct usb_audio_device_name * 476 lookup_device_name(u32 id) 477 { 478 static const struct usb_audio_device_name *p; 479 480 for (p = usb_audio_names; p->id; p++) 481 if (p->id == id) 482 return p; 483 return NULL; 484 } 485 486 /* 487 * free the chip instance 488 * 489 * here we have to do not much, since pcm and controls are already freed 490 * 491 */ 492 493 static void snd_usb_audio_free(struct snd_card *card) 494 { 495 struct snd_usb_audio *chip = card->private_data; 496 497 snd_usb_endpoint_free_all(chip); 498 snd_usb_midi_v2_free_all(chip); 499 500 mutex_destroy(&chip->mutex); 501 if (!atomic_read(&chip->shutdown)) 502 dev_set_drvdata(&chip->dev->dev, NULL); 503 } 504 505 static void usb_audio_make_shortname(struct usb_device *dev, 506 struct snd_usb_audio *chip, 507 const struct snd_usb_audio_quirk *quirk) 508 { 509 struct snd_card *card = chip->card; 510 const struct usb_audio_device_name *preset; 511 const char *s = NULL; 512 513 preset = lookup_device_name(chip->usb_id); 514 if (preset && preset->product_name) 515 s = preset->product_name; 516 else if (quirk && quirk->product_name) 517 s = quirk->product_name; 518 if (s && *s) { 519 strscpy(card->shortname, s, sizeof(card->shortname)); 520 return; 521 } 522 523 /* retrieve the device string as shortname */ 524 if (!dev->descriptor.iProduct || 525 usb_string(dev, dev->descriptor.iProduct, 526 card->shortname, sizeof(card->shortname)) <= 0) { 527 /* no name available from anywhere, so use ID */ 528 sprintf(card->shortname, "USB Device %#04x:%#04x", 529 USB_ID_VENDOR(chip->usb_id), 530 USB_ID_PRODUCT(chip->usb_id)); 531 } 532 533 strim(card->shortname); 534 } 535 536 static void usb_audio_make_longname(struct usb_device *dev, 537 struct snd_usb_audio *chip, 538 const struct snd_usb_audio_quirk *quirk) 539 { 540 struct snd_card *card = chip->card; 541 const struct usb_audio_device_name *preset; 542 const char *s = NULL; 543 int len; 544 545 preset = lookup_device_name(chip->usb_id); 546 547 /* shortcut - if any pre-defined string is given, use it */ 548 if (preset && preset->profile_name) 549 s = preset->profile_name; 550 if (s && *s) { 551 strscpy(card->longname, s, sizeof(card->longname)); 552 return; 553 } 554 555 if (preset && preset->vendor_name) 556 s = preset->vendor_name; 557 else if (quirk && quirk->vendor_name) 558 s = quirk->vendor_name; 559 *card->longname = 0; 560 if (s && *s) { 561 strscpy(card->longname, s, sizeof(card->longname)); 562 } else { 563 /* retrieve the vendor and device strings as longname */ 564 if (dev->descriptor.iManufacturer) 565 usb_string(dev, dev->descriptor.iManufacturer, 566 card->longname, sizeof(card->longname)); 567 /* we don't really care if there isn't any vendor string */ 568 } 569 if (*card->longname) { 570 strim(card->longname); 571 if (*card->longname) 572 strlcat(card->longname, " ", sizeof(card->longname)); 573 } 574 575 strlcat(card->longname, card->shortname, sizeof(card->longname)); 576 577 len = strlcat(card->longname, " at ", sizeof(card->longname)); 578 579 if (len < sizeof(card->longname)) 580 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); 581 582 switch (snd_usb_get_speed(dev)) { 583 case USB_SPEED_LOW: 584 strlcat(card->longname, ", low speed", sizeof(card->longname)); 585 break; 586 case USB_SPEED_FULL: 587 strlcat(card->longname, ", full speed", sizeof(card->longname)); 588 break; 589 case USB_SPEED_HIGH: 590 strlcat(card->longname, ", high speed", sizeof(card->longname)); 591 break; 592 case USB_SPEED_SUPER: 593 strlcat(card->longname, ", super speed", sizeof(card->longname)); 594 break; 595 case USB_SPEED_SUPER_PLUS: 596 strlcat(card->longname, ", super speed plus", sizeof(card->longname)); 597 break; 598 default: 599 break; 600 } 601 } 602 603 /* 604 * create a chip instance and set its names. 605 */ 606 static int snd_usb_audio_create(struct usb_interface *intf, 607 struct usb_device *dev, int idx, 608 const struct snd_usb_audio_quirk *quirk, 609 unsigned int usb_id, 610 struct snd_usb_audio **rchip) 611 { 612 struct snd_card *card; 613 struct snd_usb_audio *chip; 614 int err; 615 char component[14]; 616 617 *rchip = NULL; 618 619 switch (snd_usb_get_speed(dev)) { 620 case USB_SPEED_LOW: 621 case USB_SPEED_FULL: 622 case USB_SPEED_HIGH: 623 case USB_SPEED_SUPER: 624 case USB_SPEED_SUPER_PLUS: 625 break; 626 default: 627 dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev)); 628 return -ENXIO; 629 } 630 631 err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, 632 sizeof(*chip), &card); 633 if (err < 0) { 634 dev_err(&dev->dev, "cannot create card instance %d\n", idx); 635 return err; 636 } 637 638 chip = card->private_data; 639 mutex_init(&chip->mutex); 640 init_waitqueue_head(&chip->shutdown_wait); 641 chip->index = idx; 642 chip->dev = dev; 643 chip->card = card; 644 chip->setup = device_setup[idx]; 645 chip->generic_implicit_fb = implicit_fb[idx]; 646 chip->autoclock = autoclock; 647 chip->lowlatency = lowlatency; 648 atomic_set(&chip->active, 1); /* avoid autopm during probing */ 649 atomic_set(&chip->usage_count, 0); 650 atomic_set(&chip->shutdown, 0); 651 652 chip->usb_id = usb_id; 653 INIT_LIST_HEAD(&chip->pcm_list); 654 INIT_LIST_HEAD(&chip->ep_list); 655 INIT_LIST_HEAD(&chip->iface_ref_list); 656 INIT_LIST_HEAD(&chip->clock_ref_list); 657 INIT_LIST_HEAD(&chip->midi_list); 658 INIT_LIST_HEAD(&chip->midi_v2_list); 659 INIT_LIST_HEAD(&chip->mixer_list); 660 661 if (quirk_flags[idx]) 662 chip->quirk_flags = quirk_flags[idx]; 663 else 664 snd_usb_init_quirk_flags(chip); 665 666 card->private_free = snd_usb_audio_free; 667 668 strcpy(card->driver, "USB-Audio"); 669 sprintf(component, "USB%04x:%04x", 670 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); 671 snd_component_add(card, component); 672 673 usb_audio_make_shortname(dev, chip, quirk); 674 usb_audio_make_longname(dev, chip, quirk); 675 676 snd_usb_audio_create_proc(chip); 677 678 *rchip = chip; 679 return 0; 680 } 681 682 /* look for a matching quirk alias id */ 683 static bool get_alias_id(struct usb_device *dev, unsigned int *id) 684 { 685 int i; 686 unsigned int src, dst; 687 688 for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) { 689 if (!quirk_alias[i] || 690 sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 || 691 src != *id) 692 continue; 693 dev_info(&dev->dev, 694 "device (%04x:%04x): applying quirk alias %04x:%04x\n", 695 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id), 696 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst)); 697 *id = dst; 698 return true; 699 } 700 701 return false; 702 } 703 704 static int check_delayed_register_option(struct snd_usb_audio *chip) 705 { 706 int i; 707 unsigned int id, inum; 708 709 for (i = 0; i < ARRAY_SIZE(delayed_register); i++) { 710 if (delayed_register[i] && 711 sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 && 712 id == chip->usb_id) 713 return inum; 714 } 715 716 return -1; 717 } 718 719 static const struct usb_device_id usb_audio_ids[]; /* defined below */ 720 721 /* look for the last interface that matches with our ids and remember it */ 722 static void find_last_interface(struct snd_usb_audio *chip) 723 { 724 struct usb_host_config *config = chip->dev->actconfig; 725 struct usb_interface *intf; 726 int i; 727 728 if (!config) 729 return; 730 for (i = 0; i < config->desc.bNumInterfaces; i++) { 731 intf = config->interface[i]; 732 if (usb_match_id(intf, usb_audio_ids)) 733 chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber; 734 } 735 usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface); 736 } 737 738 /* look for the corresponding quirk */ 739 static const struct snd_usb_audio_quirk * 740 get_alias_quirk(struct usb_device *dev, unsigned int id) 741 { 742 const struct usb_device_id *p; 743 744 for (p = usb_audio_ids; p->match_flags; p++) { 745 /* FIXME: this checks only vendor:product pair in the list */ 746 if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) == 747 USB_DEVICE_ID_MATCH_DEVICE && 748 p->idVendor == USB_ID_VENDOR(id) && 749 p->idProduct == USB_ID_PRODUCT(id)) 750 return (const struct snd_usb_audio_quirk *)p->driver_info; 751 } 752 753 return NULL; 754 } 755 756 /* register card if we reach to the last interface or to the specified 757 * one given via option 758 */ 759 static int try_to_register_card(struct snd_usb_audio *chip, int ifnum) 760 { 761 if (check_delayed_register_option(chip) == ifnum || 762 chip->last_iface == ifnum || 763 usb_interface_claimed(usb_ifnum_to_if(chip->dev, chip->last_iface))) 764 return snd_card_register(chip->card); 765 return 0; 766 } 767 768 /* 769 * probe the active usb device 770 * 771 * note that this can be called multiple times per a device, when it 772 * includes multiple audio control interfaces. 773 * 774 * thus we check the usb device pointer and creates the card instance 775 * only at the first time. the successive calls of this function will 776 * append the pcm interface to the corresponding card. 777 */ 778 static int usb_audio_probe(struct usb_interface *intf, 779 const struct usb_device_id *usb_id) 780 { 781 struct usb_device *dev = interface_to_usbdev(intf); 782 const struct snd_usb_audio_quirk *quirk = 783 (const struct snd_usb_audio_quirk *)usb_id->driver_info; 784 struct snd_usb_audio *chip; 785 int i, err; 786 struct usb_host_interface *alts; 787 int ifnum; 788 u32 id; 789 790 alts = &intf->altsetting[0]; 791 ifnum = get_iface_desc(alts)->bInterfaceNumber; 792 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), 793 le16_to_cpu(dev->descriptor.idProduct)); 794 if (get_alias_id(dev, &id)) 795 quirk = get_alias_quirk(dev, id); 796 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) 797 return -ENXIO; 798 if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE) 799 return -ENODEV; 800 801 err = snd_usb_apply_boot_quirk(dev, intf, quirk, id); 802 if (err < 0) 803 return err; 804 805 /* 806 * found a config. now register to ALSA 807 */ 808 809 /* check whether it's already registered */ 810 chip = NULL; 811 mutex_lock(®ister_mutex); 812 for (i = 0; i < SNDRV_CARDS; i++) { 813 if (usb_chip[i] && usb_chip[i]->dev == dev) { 814 if (atomic_read(&usb_chip[i]->shutdown)) { 815 dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n"); 816 err = -EIO; 817 goto __error; 818 } 819 chip = usb_chip[i]; 820 atomic_inc(&chip->active); /* avoid autopm */ 821 break; 822 } 823 } 824 if (! chip) { 825 err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id); 826 if (err < 0) 827 goto __error; 828 829 /* it's a fresh one. 830 * now look for an empty slot and create a new card instance 831 */ 832 for (i = 0; i < SNDRV_CARDS; i++) 833 if (!usb_chip[i] && 834 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && 835 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { 836 if (enable[i]) { 837 err = snd_usb_audio_create(intf, dev, i, quirk, 838 id, &chip); 839 if (err < 0) 840 goto __error; 841 break; 842 } else if (vid[i] != -1 || pid[i] != -1) { 843 dev_info(&dev->dev, 844 "device (%04x:%04x) is disabled\n", 845 USB_ID_VENDOR(id), 846 USB_ID_PRODUCT(id)); 847 err = -ENOENT; 848 goto __error; 849 } 850 } 851 if (!chip) { 852 dev_err(&dev->dev, "no available usb audio device\n"); 853 err = -ENODEV; 854 goto __error; 855 } 856 find_last_interface(chip); 857 } 858 859 if (chip->num_interfaces >= MAX_CARD_INTERFACES) { 860 dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n"); 861 err = -EINVAL; 862 goto __error; 863 } 864 865 dev_set_drvdata(&dev->dev, chip); 866 867 if (ignore_ctl_error) 868 chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR; 869 870 if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND) 871 usb_disable_autosuspend(interface_to_usbdev(intf)); 872 873 /* 874 * For devices with more than one control interface, we assume the 875 * first contains the audio controls. We might need a more specific 876 * check here in the future. 877 */ 878 if (!chip->ctrl_intf) 879 chip->ctrl_intf = alts; 880 881 err = 1; /* continue */ 882 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { 883 /* need some special handlings */ 884 err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk); 885 if (err < 0) 886 goto __error; 887 } 888 889 if (err > 0) { 890 /* create normal USB audio interfaces */ 891 err = snd_usb_create_streams(chip, ifnum); 892 if (err < 0) 893 goto __error; 894 err = snd_usb_create_mixer(chip, ifnum); 895 if (err < 0) 896 goto __error; 897 } 898 899 if (chip->need_delayed_register) { 900 dev_info(&dev->dev, 901 "Found post-registration device assignment: %08x:%02x\n", 902 chip->usb_id, ifnum); 903 chip->need_delayed_register = false; /* clear again */ 904 } 905 906 err = try_to_register_card(chip, ifnum); 907 if (err < 0) 908 goto __error_no_register; 909 910 if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) { 911 /* don't want to fail when snd_media_device_create() fails */ 912 snd_media_device_create(chip, intf); 913 } 914 915 if (quirk) 916 chip->quirk_type = quirk->type; 917 918 usb_chip[chip->index] = chip; 919 chip->intf[chip->num_interfaces] = intf; 920 chip->num_interfaces++; 921 usb_set_intfdata(intf, chip); 922 atomic_dec(&chip->active); 923 mutex_unlock(®ister_mutex); 924 return 0; 925 926 __error: 927 /* in the case of error in secondary interface, still try to register */ 928 if (chip) 929 try_to_register_card(chip, ifnum); 930 931 __error_no_register: 932 if (chip) { 933 /* chip->active is inside the chip->card object, 934 * decrement before memory is possibly returned. 935 */ 936 atomic_dec(&chip->active); 937 if (!chip->num_interfaces) 938 snd_card_free(chip->card); 939 } 940 mutex_unlock(®ister_mutex); 941 return err; 942 } 943 944 /* 945 * we need to take care of counter, since disconnection can be called also 946 * many times as well as usb_audio_probe(). 947 */ 948 static void usb_audio_disconnect(struct usb_interface *intf) 949 { 950 struct snd_usb_audio *chip = usb_get_intfdata(intf); 951 struct snd_card *card; 952 struct list_head *p; 953 954 if (chip == USB_AUDIO_IFACE_UNUSED) 955 return; 956 957 card = chip->card; 958 959 mutex_lock(®ister_mutex); 960 if (atomic_inc_return(&chip->shutdown) == 1) { 961 struct snd_usb_stream *as; 962 struct snd_usb_endpoint *ep; 963 struct usb_mixer_interface *mixer; 964 965 /* wait until all pending tasks done; 966 * they are protected by snd_usb_lock_shutdown() 967 */ 968 wait_event(chip->shutdown_wait, 969 !atomic_read(&chip->usage_count)); 970 snd_card_disconnect(card); 971 /* release the pcm resources */ 972 list_for_each_entry(as, &chip->pcm_list, list) { 973 snd_usb_stream_disconnect(as); 974 } 975 /* release the endpoint resources */ 976 list_for_each_entry(ep, &chip->ep_list, list) { 977 snd_usb_endpoint_release(ep); 978 } 979 /* release the midi resources */ 980 list_for_each(p, &chip->midi_list) { 981 snd_usbmidi_disconnect(p); 982 } 983 snd_usb_midi_v2_disconnect_all(chip); 984 /* 985 * Nice to check quirk && quirk->shares_media_device and 986 * then call the snd_media_device_delete(). Don't have 987 * access to the quirk here. snd_media_device_delete() 988 * accesses mixer_list 989 */ 990 snd_media_device_delete(chip); 991 992 /* release mixer resources */ 993 list_for_each_entry(mixer, &chip->mixer_list, list) { 994 snd_usb_mixer_disconnect(mixer); 995 } 996 } 997 998 if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND) 999 usb_enable_autosuspend(interface_to_usbdev(intf)); 1000 1001 chip->num_interfaces--; 1002 if (chip->num_interfaces <= 0) { 1003 usb_chip[chip->index] = NULL; 1004 mutex_unlock(®ister_mutex); 1005 snd_card_free_when_closed(card); 1006 } else { 1007 mutex_unlock(®ister_mutex); 1008 } 1009 } 1010 1011 /* lock the shutdown (disconnect) task and autoresume */ 1012 int snd_usb_lock_shutdown(struct snd_usb_audio *chip) 1013 { 1014 int err; 1015 1016 atomic_inc(&chip->usage_count); 1017 if (atomic_read(&chip->shutdown)) { 1018 err = -EIO; 1019 goto error; 1020 } 1021 err = snd_usb_autoresume(chip); 1022 if (err < 0) 1023 goto error; 1024 return 0; 1025 1026 error: 1027 if (atomic_dec_and_test(&chip->usage_count)) 1028 wake_up(&chip->shutdown_wait); 1029 return err; 1030 } 1031 1032 /* autosuspend and unlock the shutdown */ 1033 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip) 1034 { 1035 snd_usb_autosuspend(chip); 1036 if (atomic_dec_and_test(&chip->usage_count)) 1037 wake_up(&chip->shutdown_wait); 1038 } 1039 1040 int snd_usb_autoresume(struct snd_usb_audio *chip) 1041 { 1042 int i, err; 1043 1044 if (atomic_read(&chip->shutdown)) 1045 return -EIO; 1046 if (atomic_inc_return(&chip->active) != 1) 1047 return 0; 1048 1049 for (i = 0; i < chip->num_interfaces; i++) { 1050 err = usb_autopm_get_interface(chip->intf[i]); 1051 if (err < 0) { 1052 /* rollback */ 1053 while (--i >= 0) 1054 usb_autopm_put_interface(chip->intf[i]); 1055 atomic_dec(&chip->active); 1056 return err; 1057 } 1058 } 1059 return 0; 1060 } 1061 1062 void snd_usb_autosuspend(struct snd_usb_audio *chip) 1063 { 1064 int i; 1065 1066 if (atomic_read(&chip->shutdown)) 1067 return; 1068 if (!atomic_dec_and_test(&chip->active)) 1069 return; 1070 1071 for (i = 0; i < chip->num_interfaces; i++) 1072 usb_autopm_put_interface(chip->intf[i]); 1073 } 1074 1075 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) 1076 { 1077 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1078 struct snd_usb_stream *as; 1079 struct snd_usb_endpoint *ep; 1080 struct usb_mixer_interface *mixer; 1081 struct list_head *p; 1082 1083 if (chip == USB_AUDIO_IFACE_UNUSED) 1084 return 0; 1085 1086 if (!chip->num_suspended_intf++) { 1087 list_for_each_entry(as, &chip->pcm_list, list) 1088 snd_usb_pcm_suspend(as); 1089 list_for_each_entry(ep, &chip->ep_list, list) 1090 snd_usb_endpoint_suspend(ep); 1091 list_for_each(p, &chip->midi_list) 1092 snd_usbmidi_suspend(p); 1093 list_for_each_entry(mixer, &chip->mixer_list, list) 1094 snd_usb_mixer_suspend(mixer); 1095 snd_usb_midi_v2_suspend_all(chip); 1096 } 1097 1098 if (!PMSG_IS_AUTO(message) && !chip->system_suspend) { 1099 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); 1100 chip->system_suspend = chip->num_suspended_intf; 1101 } 1102 1103 return 0; 1104 } 1105 1106 static int usb_audio_resume(struct usb_interface *intf) 1107 { 1108 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1109 struct snd_usb_stream *as; 1110 struct usb_mixer_interface *mixer; 1111 struct list_head *p; 1112 int err = 0; 1113 1114 if (chip == USB_AUDIO_IFACE_UNUSED) 1115 return 0; 1116 1117 atomic_inc(&chip->active); /* avoid autopm */ 1118 if (chip->num_suspended_intf > 1) 1119 goto out; 1120 1121 list_for_each_entry(as, &chip->pcm_list, list) { 1122 err = snd_usb_pcm_resume(as); 1123 if (err < 0) 1124 goto err_out; 1125 } 1126 1127 /* 1128 * ALSA leaves material resumption to user space 1129 * we just notify and restart the mixers 1130 */ 1131 list_for_each_entry(mixer, &chip->mixer_list, list) { 1132 err = snd_usb_mixer_resume(mixer); 1133 if (err < 0) 1134 goto err_out; 1135 } 1136 1137 list_for_each(p, &chip->midi_list) { 1138 snd_usbmidi_resume(p); 1139 } 1140 1141 snd_usb_midi_v2_resume_all(chip); 1142 1143 out: 1144 if (chip->num_suspended_intf == chip->system_suspend) { 1145 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); 1146 chip->system_suspend = 0; 1147 } 1148 chip->num_suspended_intf--; 1149 1150 err_out: 1151 atomic_dec(&chip->active); /* allow autopm after this point */ 1152 return err; 1153 } 1154 1155 static const struct usb_device_id usb_audio_ids [] = { 1156 #include "quirks-table.h" 1157 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), 1158 .bInterfaceClass = USB_CLASS_AUDIO, 1159 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, 1160 { } /* Terminating entry */ 1161 }; 1162 MODULE_DEVICE_TABLE(usb, usb_audio_ids); 1163 1164 /* 1165 * entry point for linux usb interface 1166 */ 1167 1168 static struct usb_driver usb_audio_driver = { 1169 .name = "snd-usb-audio", 1170 .probe = usb_audio_probe, 1171 .disconnect = usb_audio_disconnect, 1172 .suspend = usb_audio_suspend, 1173 .resume = usb_audio_resume, 1174 .reset_resume = usb_audio_resume, 1175 .id_table = usb_audio_ids, 1176 .supports_autosuspend = 1, 1177 }; 1178 1179 module_usb_driver(usb_audio_driver); 1180
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.