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 char *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_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444); 107 MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes)."); 108 module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444); 109 MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no)."); 110 111 /* protects quirk_flags */ 112 static DEFINE_MUTEX(quirk_flags_mutex); 113 114 static int param_set_quirkp(const char *val, 115 const struct kernel_param *kp) 116 { 117 guard(mutex)(&quirk_flags_mutex); 118 return param_set_charp(val, kp); 119 } 120 121 static const struct kernel_param_ops param_ops_quirkp = { 122 .set = param_set_quirkp, 123 .get = param_get_charp, 124 .free = param_free_charp, 125 }; 126 127 #define param_check_quirkp param_check_charp 128 129 module_param_array(quirk_flags, quirkp, NULL, 0644); 130 MODULE_PARM_DESC(quirk_flags, "Add/modify USB audio quirks"); 131 132 /* 133 * we keep the snd_usb_audio_t instances by ourselves for merging 134 * the all interfaces on the same card as one sound device. 135 */ 136 137 static DEFINE_MUTEX(register_mutex); 138 static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; 139 static struct usb_driver usb_audio_driver; 140 static struct snd_usb_platform_ops *platform_ops; 141 142 /* 143 * Register platform specific operations that will be notified on events 144 * which occur in USB SND. The platform driver can utilize this path to 145 * enable features, such as USB audio offloading, which allows for audio data 146 * to be queued by an audio DSP. 147 * 148 * Only one set of platform operations can be registered to USB SND. The 149 * platform register operation is protected by the register_mutex. 150 */ 151 int snd_usb_register_platform_ops(struct snd_usb_platform_ops *ops) 152 { 153 guard(mutex)(®ister_mutex); 154 if (platform_ops) 155 return -EEXIST; 156 157 platform_ops = ops; 158 return 0; 159 } 160 EXPORT_SYMBOL_GPL(snd_usb_register_platform_ops); 161 162 /* 163 * Unregisters the current set of platform operations. This allows for 164 * a new set to be registered if required. 165 * 166 * The platform unregister operation is protected by the register_mutex. 167 */ 168 int snd_usb_unregister_platform_ops(void) 169 { 170 guard(mutex)(®ister_mutex); 171 platform_ops = NULL; 172 173 return 0; 174 } 175 EXPORT_SYMBOL_GPL(snd_usb_unregister_platform_ops); 176 177 /* 178 * in case the platform driver was not ready at the time of USB SND 179 * device connect, expose an API to discover all connected USB devices 180 * so it can populate any dependent resources/structures. 181 */ 182 void snd_usb_rediscover_devices(void) 183 { 184 int i; 185 186 guard(mutex)(®ister_mutex); 187 188 if (!platform_ops || !platform_ops->connect_cb) 189 return; 190 191 for (i = 0; i < SNDRV_CARDS; i++) { 192 if (usb_chip[i]) 193 platform_ops->connect_cb(usb_chip[i]); 194 } 195 } 196 EXPORT_SYMBOL_GPL(snd_usb_rediscover_devices); 197 198 /* 199 * Checks to see if requested audio profile, i.e sample rate, # of 200 * channels, etc... is supported by the substream associated to the 201 * USB audio device. 202 */ 203 struct snd_usb_stream * 204 snd_usb_find_suppported_substream(int card_idx, struct snd_pcm_hw_params *params, 205 int direction) 206 { 207 struct snd_usb_audio *chip; 208 struct snd_usb_substream *subs; 209 struct snd_usb_stream *as; 210 211 /* 212 * Register mutex is held when populating and clearing usb_chip 213 * array. 214 */ 215 guard(mutex)(®ister_mutex); 216 chip = usb_chip[card_idx]; 217 218 if (chip && enable[card_idx]) { 219 list_for_each_entry(as, &chip->pcm_list, list) { 220 subs = &as->substream[direction]; 221 if (snd_usb_find_substream_format(subs, params)) 222 return as; 223 } 224 } 225 226 return NULL; 227 } 228 EXPORT_SYMBOL_GPL(snd_usb_find_suppported_substream); 229 230 /* 231 * disconnect streams 232 * called from usb_audio_disconnect() 233 */ 234 static void snd_usb_stream_disconnect(struct snd_usb_stream *as) 235 { 236 int idx; 237 struct snd_usb_substream *subs; 238 239 for (idx = 0; idx < 2; idx++) { 240 subs = &as->substream[idx]; 241 if (!subs->num_formats) 242 continue; 243 subs->data_endpoint = NULL; 244 subs->sync_endpoint = NULL; 245 } 246 } 247 248 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) 249 { 250 struct usb_device *dev = chip->dev; 251 struct usb_host_interface *alts; 252 struct usb_interface_descriptor *altsd; 253 struct usb_interface *iface = usb_ifnum_to_if(dev, interface); 254 255 if (!iface) { 256 dev_err(&dev->dev, "%u:%d : does not exist\n", 257 ctrlif, interface); 258 return -EINVAL; 259 } 260 261 alts = &iface->altsetting[0]; 262 altsd = get_iface_desc(alts); 263 264 /* 265 * Android with both accessory and audio interfaces enabled gets the 266 * interface numbers wrong. 267 */ 268 if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || 269 chip->usb_id == USB_ID(0x18d1, 0x2d05)) && 270 interface == 0 && 271 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && 272 altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { 273 interface = 2; 274 iface = usb_ifnum_to_if(dev, interface); 275 if (!iface) 276 return -EINVAL; 277 alts = &iface->altsetting[0]; 278 altsd = get_iface_desc(alts); 279 } 280 281 if (usb_interface_claimed(iface)) { 282 dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n", 283 ctrlif, interface); 284 return -EINVAL; 285 } 286 287 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || 288 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && 289 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { 290 int err = snd_usb_midi_v2_create(chip, iface, NULL, 291 chip->usb_id); 292 if (err < 0) { 293 dev_err(&dev->dev, 294 "%u:%d: cannot create sequencer device\n", 295 ctrlif, interface); 296 return -EINVAL; 297 } 298 return usb_driver_claim_interface(&usb_audio_driver, iface, 299 USB_AUDIO_IFACE_UNUSED); 300 } 301 302 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && 303 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || 304 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { 305 dev_dbg(&dev->dev, 306 "%u:%d: skipping non-supported interface %d\n", 307 ctrlif, interface, altsd->bInterfaceClass); 308 /* skip non-supported classes */ 309 return -EINVAL; 310 } 311 312 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { 313 dev_err(&dev->dev, "low speed audio streaming not supported\n"); 314 return -EINVAL; 315 } 316 317 snd_usb_add_ctrl_interface_link(chip, interface, ctrlif); 318 319 if (! snd_usb_parse_audio_interface(chip, interface)) { 320 usb_set_interface(dev, interface, 0); /* reset the current interface */ 321 return usb_driver_claim_interface(&usb_audio_driver, iface, 322 USB_AUDIO_IFACE_UNUSED); 323 } 324 325 return 0; 326 } 327 328 /* 329 * parse audio control descriptor and create pcm/midi streams 330 */ 331 static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) 332 { 333 struct usb_device *dev = chip->dev; 334 struct usb_host_interface *host_iface; 335 struct usb_interface_descriptor *altsd; 336 int i, protocol; 337 338 /* find audiocontrol interface */ 339 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; 340 altsd = get_iface_desc(host_iface); 341 protocol = altsd->bInterfaceProtocol; 342 343 switch (protocol) { 344 default: 345 dev_warn(&dev->dev, 346 "unknown interface protocol %#02x, assuming v1\n", 347 protocol); 348 fallthrough; 349 350 case UAC_VERSION_1: { 351 struct uac1_ac_header_descriptor *h1; 352 int rest_bytes; 353 354 h1 = snd_usb_find_csint_desc(host_iface->extra, 355 host_iface->extralen, 356 NULL, UAC_HEADER); 357 if (!h1 || h1->bLength < sizeof(*h1)) { 358 dev_err(&dev->dev, "cannot find UAC_HEADER\n"); 359 return -EINVAL; 360 } 361 362 rest_bytes = (void *)(host_iface->extra + 363 host_iface->extralen) - (void *)h1; 364 365 /* just to be sure -- this shouldn't hit at all */ 366 if (rest_bytes <= 0) { 367 dev_err(&dev->dev, "invalid control header\n"); 368 return -EINVAL; 369 } 370 371 if (rest_bytes < sizeof(*h1)) { 372 dev_err(&dev->dev, "too short v1 buffer descriptor\n"); 373 return -EINVAL; 374 } 375 376 if (!h1->bInCollection) { 377 dev_info(&dev->dev, "skipping empty audio interface (v1)\n"); 378 return -EINVAL; 379 } 380 381 if (rest_bytes < h1->bLength) { 382 dev_err(&dev->dev, "invalid buffer length (v1)\n"); 383 return -EINVAL; 384 } 385 386 if (h1->bLength < sizeof(*h1) + h1->bInCollection) { 387 dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n"); 388 return -EINVAL; 389 } 390 391 for (i = 0; i < h1->bInCollection; i++) 392 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); 393 394 break; 395 } 396 397 case UAC_VERSION_2: 398 case UAC_VERSION_3: { 399 struct usb_interface_assoc_descriptor *assoc = 400 usb_ifnum_to_if(dev, ctrlif)->intf_assoc; 401 402 if (!assoc) { 403 /* 404 * Firmware writers cannot count to three. So to find 405 * the IAD on the NuForce UDH-100, also check the next 406 * interface. 407 */ 408 struct usb_interface *iface = 409 usb_ifnum_to_if(dev, ctrlif + 1); 410 if (iface && 411 iface->intf_assoc && 412 iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && 413 iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) 414 assoc = iface->intf_assoc; 415 } 416 417 if (!assoc) { 418 dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n"); 419 return -EINVAL; 420 } 421 422 if (protocol == UAC_VERSION_3) { 423 int badd = assoc->bFunctionSubClass; 424 425 if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 && 426 (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO || 427 badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) { 428 dev_err(&dev->dev, 429 "Unsupported UAC3 BADD profile\n"); 430 return -EINVAL; 431 } 432 433 chip->badd_profile = badd; 434 } 435 436 for (i = 0; i < assoc->bInterfaceCount; i++) { 437 int intf = assoc->bFirstInterface + i; 438 439 if (intf != ctrlif) 440 snd_usb_create_stream(chip, ctrlif, intf); 441 } 442 443 break; 444 } 445 } 446 447 return 0; 448 } 449 450 /* 451 * Profile name preset table 452 */ 453 struct usb_audio_device_name { 454 u32 id; 455 const char *vendor_name; 456 const char *product_name; 457 const char *profile_name; /* override card->longname */ 458 }; 459 460 #define PROFILE_NAME(vid, pid, vendor, product, profile) \ 461 { .id = USB_ID(vid, pid), .vendor_name = (vendor), \ 462 .product_name = (product), .profile_name = (profile) } 463 #define DEVICE_NAME(vid, pid, vendor, product) \ 464 PROFILE_NAME(vid, pid, vendor, product, NULL) 465 466 /* vendor/product and profile name presets, sorted in device id order */ 467 static const struct usb_audio_device_name usb_audio_names[] = { 468 /* HP Thunderbolt Dock Audio Headset */ 469 PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset", 470 "HP-Thunderbolt-Dock-Audio-Headset"), 471 /* HP Thunderbolt Dock Audio Module */ 472 PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module", 473 "HP-Thunderbolt-Dock-Audio-Module"), 474 475 /* Two entries for Gigabyte TRX40 Aorus Master: 476 * TRX40 Aorus Master has two USB-audio devices, one for the front 477 * headphone with ESS SABRE9218 DAC chip, while another for the rest 478 * I/O (the rear panel and the front mic) with Realtek ALC1220-VB. 479 * Here we provide two distinct names for making UCM profiles easier. 480 */ 481 PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone", 482 "Gigabyte-Aorus-Master-Front-Headphone"), 483 PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio", 484 "Gigabyte-Aorus-Master-Main-Audio"), 485 486 /* Gigabyte TRX40 Aorus Pro WiFi */ 487 PROFILE_NAME(0x0414, 0xa002, 488 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 489 490 /* Creative/E-Mu devices */ 491 DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"), 492 /* Creative/Toshiba Multimedia Center SB-0500 */ 493 DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"), 494 495 /* Logitech Audio Devices */ 496 DEVICE_NAME(0x046d, 0x0867, "Logitech, Inc.", "Logi-MeetUp"), 497 DEVICE_NAME(0x046d, 0x0874, "Logitech, Inc.", "Logi-Tap-Audio"), 498 DEVICE_NAME(0x046d, 0x087c, "Logitech, Inc.", "Logi-Huddle"), 499 DEVICE_NAME(0x046d, 0x0898, "Logitech, Inc.", "Logi-RB-Audio"), 500 DEVICE_NAME(0x046d, 0x08d2, "Logitech, Inc.", "Logi-RBM-Audio"), 501 DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"), 502 503 DEVICE_NAME(0x05e1, 0x0408, "Syntek", "STK1160"), 504 DEVICE_NAME(0x05e1, 0x0480, "Hauppauge", "Woodbury"), 505 506 /* ASUS ROG Zenith II: this machine has also two devices, one for 507 * the front headphone and another for the rest 508 */ 509 PROFILE_NAME(0x0b05, 0x1915, "ASUS", "Zenith II Front Headphone", 510 "Zenith-II-Front-Headphone"), 511 PROFILE_NAME(0x0b05, 0x1916, "ASUS", "Zenith II Main Audio", 512 "Zenith-II-Main-Audio"), 513 514 /* ASUS ROG Strix */ 515 PROFILE_NAME(0x0b05, 0x1917, 516 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 517 /* ASUS PRIME TRX40 PRO-S */ 518 PROFILE_NAME(0x0b05, 0x1918, 519 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 520 521 /* Dell WD15 Dock */ 522 PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"), 523 /* Dell WD19 Dock */ 524 PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"), 525 526 DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"), 527 528 /* 529 * The original product_name is "USB Sound Device", however this name 530 * is also used by the CM106 based cards, so make it unique. 531 */ 532 DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"), 533 DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"), 534 535 /* MSI TRX40 Creator */ 536 PROFILE_NAME(0x0db0, 0x0d64, 537 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 538 /* MSI TRX40 */ 539 PROFILE_NAME(0x0db0, 0x543d, 540 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 541 542 DEVICE_NAME(0x0fd9, 0x0008, "Hauppauge", "HVR-950Q"), 543 544 /* Dock/Stand for HP Engage Go */ 545 PROFILE_NAME(0x103c, 0x830a, "HP", "HP Engage Go Dock", 546 "HP-Engage-Go-Dock"), 547 548 /* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */ 549 DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"), 550 DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"), 551 552 /* aka. Serato Scratch Live DJ Box */ 553 DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"), 554 555 /* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */ 556 PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear", 557 "Lenovo-ThinkStation-P620-Rear"), 558 /* Lenovo ThinkStation P620 Internal Speaker + Front Headset */ 559 PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main", 560 "Lenovo-ThinkStation-P620-Main"), 561 562 /* Asrock TRX40 Creator */ 563 PROFILE_NAME(0x26ce, 0x0a01, 564 "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"), 565 566 DEVICE_NAME(0x2040, 0x7200, "Hauppauge", "HVR-950Q"), 567 DEVICE_NAME(0x2040, 0x7201, "Hauppauge", "HVR-950Q-MXL"), 568 DEVICE_NAME(0x2040, 0x7210, "Hauppauge", "HVR-950Q"), 569 DEVICE_NAME(0x2040, 0x7211, "Hauppauge", "HVR-950Q-MXL"), 570 DEVICE_NAME(0x2040, 0x7213, "Hauppauge", "HVR-950Q"), 571 DEVICE_NAME(0x2040, 0x7217, "Hauppauge", "HVR-950Q"), 572 DEVICE_NAME(0x2040, 0x721b, "Hauppauge", "HVR-950Q"), 573 DEVICE_NAME(0x2040, 0x721e, "Hauppauge", "HVR-950Q"), 574 DEVICE_NAME(0x2040, 0x721f, "Hauppauge", "HVR-950Q"), 575 DEVICE_NAME(0x2040, 0x7240, "Hauppauge", "HVR-850"), 576 DEVICE_NAME(0x2040, 0x7260, "Hauppauge", "HVR-950Q"), 577 DEVICE_NAME(0x2040, 0x7270, "Hauppauge", "HVR-950Q"), 578 DEVICE_NAME(0x2040, 0x7280, "Hauppauge", "HVR-950Q"), 579 DEVICE_NAME(0x2040, 0x7281, "Hauppauge", "HVR-950Q-MXL"), 580 DEVICE_NAME(0x2040, 0x8200, "Hauppauge", "Woodbury"), 581 582 { } /* terminator */ 583 }; 584 585 static const struct usb_audio_device_name * 586 lookup_device_name(u32 id) 587 { 588 static const struct usb_audio_device_name *p; 589 590 for (p = usb_audio_names; p->id; p++) 591 if (p->id == id) 592 return p; 593 return NULL; 594 } 595 596 /* 597 * free the chip instance 598 * 599 * here we have to do not much, since pcm and controls are already freed 600 * 601 */ 602 603 static void snd_usb_audio_free(struct snd_card *card) 604 { 605 struct snd_usb_audio *chip = card->private_data; 606 607 snd_usb_endpoint_free_all(chip); 608 snd_usb_midi_v2_free_all(chip); 609 610 mutex_destroy(&chip->mutex); 611 if (!atomic_read(&chip->shutdown)) 612 dev_set_drvdata(&chip->dev->dev, NULL); 613 } 614 615 static void usb_audio_make_shortname(struct usb_device *dev, 616 struct snd_usb_audio *chip, 617 const struct snd_usb_audio_quirk *quirk) 618 { 619 struct snd_card *card = chip->card; 620 const struct usb_audio_device_name *preset; 621 622 *card->shortname = 0; 623 preset = lookup_device_name(chip->usb_id); 624 if (preset && preset->product_name) 625 strscpy(card->shortname, preset->product_name); 626 else if (quirk && quirk->product_name) 627 strscpy(card->shortname, quirk->product_name); 628 if (*card->shortname) 629 return; 630 631 if (dev->product && *dev->product) { 632 strscpy(card->shortname, dev->product); 633 strim(card->shortname); 634 return; 635 } 636 637 scnprintf(card->shortname, sizeof(card->shortname), "USB Device %#04x:%#04x", 638 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); 639 } 640 641 static void usb_audio_make_longname(struct usb_device *dev, 642 struct snd_usb_audio *chip, 643 const struct snd_usb_audio_quirk *quirk) 644 { 645 static const char * const speed_suffixes[] = { 646 [USB_SPEED_LOW] = ", low speed", 647 [USB_SPEED_FULL] = ", full speed", 648 [USB_SPEED_HIGH] = ", high speed", 649 [USB_SPEED_SUPER] = ", super speed", 650 [USB_SPEED_SUPER_PLUS] = ", super speed plus", 651 }; 652 struct snd_card *card = chip->card; 653 const struct usb_audio_device_name *preset; 654 enum usb_device_speed speed = snd_usb_get_speed(dev); 655 int len, ret; 656 657 preset = lookup_device_name(chip->usb_id); 658 659 /* shortcut - if any pre-defined string is given, use it */ 660 if (preset && preset->profile_name && *preset->profile_name) { 661 strscpy(card->longname, preset->profile_name); 662 return; 663 } 664 665 *card->longname = 0; 666 if (preset && preset->vendor_name) 667 strscpy(card->longname, preset->vendor_name); 668 else if (quirk && quirk->vendor_name) 669 strscpy(card->longname, quirk->vendor_name); 670 if (!*card->longname) 671 strscpy(card->longname, dev->manufacturer ?: ""); 672 673 strim(card->longname); 674 len = strlen(card->longname); 675 len += scnprintf(card->longname + len, sizeof(card->longname) - len, 676 "%s%s at ", len ? " " : "", card->shortname); 677 678 ret = usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); 679 if (ret < 0) 680 return; 681 len += ret; 682 683 if (speed < ARRAY_SIZE(speed_suffixes) && speed_suffixes[speed]) 684 strscpy(card->longname + len, speed_suffixes[speed], sizeof(card->longname) - len); 685 } 686 687 static void snd_usb_init_quirk_flags(int idx, struct snd_usb_audio *chip) 688 { 689 size_t i; 690 691 guard(mutex)(&quirk_flags_mutex); 692 693 /* old style option found: the position-based integer value */ 694 if (quirk_flags[idx] && 695 !kstrtou64(quirk_flags[idx], 0, &chip->quirk_flags)) { 696 snd_usb_apply_flag_dbg("module param", chip, chip->quirk_flags); 697 return; 698 } 699 700 /* take the default quirk from the quirk table */ 701 snd_usb_init_quirk_flags_table(chip); 702 703 /* add or correct quirk bits from options */ 704 for (i = 0; i < ARRAY_SIZE(quirk_flags); i++) { 705 if (!quirk_flags[i] || !*quirk_flags[i]) 706 break; 707 708 snd_usb_init_quirk_flags_parse_string(chip, quirk_flags[i]); 709 } 710 } 711 712 /* 713 * create a chip instance and set its names. 714 */ 715 static int snd_usb_audio_create(struct usb_interface *intf, 716 struct usb_device *dev, int idx, 717 const struct snd_usb_audio_quirk *quirk, 718 unsigned int usb_id, 719 struct snd_usb_audio **rchip) 720 { 721 struct snd_card *card; 722 struct snd_usb_audio *chip; 723 int err; 724 char component[14]; 725 726 *rchip = NULL; 727 728 switch (snd_usb_get_speed(dev)) { 729 case USB_SPEED_LOW: 730 case USB_SPEED_FULL: 731 case USB_SPEED_HIGH: 732 case USB_SPEED_SUPER: 733 case USB_SPEED_SUPER_PLUS: 734 break; 735 default: 736 dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev)); 737 return -ENXIO; 738 } 739 740 err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, 741 sizeof(*chip), &card); 742 if (err < 0) { 743 dev_err(&dev->dev, "cannot create card instance %d\n", idx); 744 return err; 745 } 746 747 chip = card->private_data; 748 mutex_init(&chip->mutex); 749 chip->index = idx; 750 chip->dev = dev; 751 chip->card = card; 752 chip->setup = device_setup[idx]; 753 chip->generic_implicit_fb = implicit_fb[idx]; 754 chip->autoclock = autoclock; 755 chip->lowlatency = lowlatency; 756 atomic_set(&chip->active, 1); /* avoid autopm during probing */ 757 snd_refcount_init(&chip->usage_count); 758 atomic_set(&chip->shutdown, 0); 759 760 chip->usb_id = usb_id; 761 INIT_LIST_HEAD(&chip->pcm_list); 762 INIT_LIST_HEAD(&chip->ep_list); 763 INIT_LIST_HEAD(&chip->iface_ref_list); 764 INIT_LIST_HEAD(&chip->clock_ref_list); 765 INIT_LIST_HEAD(&chip->midi_list); 766 INIT_LIST_HEAD(&chip->midi_v2_list); 767 INIT_LIST_HEAD(&chip->mixer_list); 768 769 snd_usb_init_quirk_flags(idx, chip); 770 771 card->private_free = snd_usb_audio_free; 772 773 strscpy(card->driver, "USB-Audio"); 774 scnprintf(component, sizeof(component), "USB%04x:%04x", 775 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); 776 snd_component_add(card, component); 777 778 usb_audio_make_shortname(dev, chip, quirk); 779 usb_audio_make_longname(dev, chip, quirk); 780 781 snd_usb_audio_create_proc(chip); 782 783 *rchip = chip; 784 return 0; 785 } 786 787 /* look for a matching quirk alias id */ 788 static bool get_alias_id(struct usb_device *dev, unsigned int *id) 789 { 790 int i; 791 unsigned int src, dst; 792 793 for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) { 794 if (!quirk_alias[i] || 795 sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 || 796 src != *id) 797 continue; 798 dev_info(&dev->dev, 799 "device (%04x:%04x): applying quirk alias %04x:%04x\n", 800 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id), 801 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst)); 802 *id = dst; 803 return true; 804 } 805 806 return false; 807 } 808 809 static int check_delayed_register_option(struct snd_usb_audio *chip) 810 { 811 int i; 812 unsigned int id, inum; 813 814 for (i = 0; i < ARRAY_SIZE(delayed_register); i++) { 815 if (delayed_register[i] && 816 sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 && 817 id == chip->usb_id) 818 return inum; 819 } 820 821 return -1; 822 } 823 824 static const struct usb_device_id usb_audio_ids[]; /* defined below */ 825 826 /* look for the last interface that matches with our ids and remember it */ 827 static void find_last_interface(struct snd_usb_audio *chip) 828 { 829 struct usb_host_config *config = chip->dev->actconfig; 830 struct usb_interface *intf; 831 int i; 832 833 if (!config) 834 return; 835 for (i = 0; i < config->desc.bNumInterfaces; i++) { 836 intf = config->interface[i]; 837 if (usb_match_id(intf, usb_audio_ids)) 838 chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber; 839 } 840 usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface); 841 } 842 843 /* look for the corresponding quirk */ 844 static const struct snd_usb_audio_quirk * 845 get_alias_quirk(struct usb_interface *intf, unsigned int id) 846 { 847 const struct usb_device_id *p; 848 struct usb_device_id match_id; 849 850 for (p = usb_audio_ids; p->match_flags; p++) { 851 if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) != 852 USB_DEVICE_ID_MATCH_DEVICE) 853 continue; 854 if (p->idVendor != USB_ID_VENDOR(id) || 855 p->idProduct != USB_ID_PRODUCT(id)) 856 continue; 857 858 match_id = *p; 859 match_id.match_flags &= ~USB_DEVICE_ID_MATCH_DEVICE; 860 if (!match_id.match_flags || usb_match_one_id(intf, &match_id)) 861 return (const struct snd_usb_audio_quirk *) 862 p->driver_info; 863 } 864 return NULL; 865 } 866 867 /* register card if we reach to the last interface or to the specified 868 * one given via option 869 */ 870 static int try_to_register_card(struct snd_usb_audio *chip, int ifnum) 871 { 872 struct usb_interface *iface; 873 874 if (check_delayed_register_option(chip) == ifnum || 875 chip->last_iface == ifnum) 876 return snd_card_register(chip->card); 877 878 iface = usb_ifnum_to_if(chip->dev, chip->last_iface); 879 if (iface && usb_interface_claimed(iface)) 880 return snd_card_register(chip->card); 881 882 return 0; 883 } 884 885 static void usb_audio_disconnect_components(struct snd_usb_audio *chip) 886 { 887 struct snd_usb_stream *as; 888 struct snd_usb_endpoint *ep; 889 struct usb_mixer_interface *mixer; 890 struct list_head *p; 891 892 /* release the pcm resources */ 893 list_for_each_entry(as, &chip->pcm_list, list) { 894 snd_usb_stream_disconnect(as); 895 } 896 /* release the endpoint resources */ 897 list_for_each_entry(ep, &chip->ep_list, list) { 898 snd_usb_endpoint_release(ep); 899 } 900 /* release the midi resources */ 901 list_for_each(p, &chip->midi_list) { 902 snd_usbmidi_disconnect(p); 903 } 904 snd_usb_midi_v2_disconnect_all(chip); 905 /* 906 * Nice to check quirk && quirk->shares_media_device and 907 * then call the snd_media_device_delete(). Don't have 908 * access to the quirk here. snd_media_device_delete() 909 * accesses mixer_list 910 */ 911 snd_media_device_delete(chip); 912 913 /* release mixer resources */ 914 list_for_each_entry(mixer, &chip->mixer_list, list) { 915 snd_usb_mixer_disconnect(mixer); 916 } 917 } 918 919 /* 920 * probe the active usb device 921 * 922 * note that this can be called multiple times per a device, when it 923 * includes multiple audio control interfaces. 924 * 925 * thus we check the usb device pointer and creates the card instance 926 * only at the first time. the successive calls of this function will 927 * append the pcm interface to the corresponding card. 928 */ 929 static int usb_audio_probe(struct usb_interface *intf, 930 const struct usb_device_id *usb_id) 931 { 932 struct usb_device *dev = interface_to_usbdev(intf); 933 const struct snd_usb_audio_quirk *quirk = 934 (const struct snd_usb_audio_quirk *)usb_id->driver_info; 935 struct snd_usb_audio *chip; 936 int i, err; 937 struct usb_host_interface *alts; 938 int ifnum; 939 u32 id; 940 941 alts = &intf->altsetting[0]; 942 ifnum = get_iface_desc(alts)->bInterfaceNumber; 943 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), 944 le16_to_cpu(dev->descriptor.idProduct)); 945 if (get_alias_id(dev, &id)) 946 quirk = get_alias_quirk(intf, id); 947 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) 948 return -ENXIO; 949 if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE) 950 return -ENODEV; 951 952 err = snd_usb_apply_boot_quirk(dev, intf, quirk, id); 953 if (err < 0) 954 return err; 955 956 /* 957 * found a config. now register to ALSA 958 */ 959 960 /* check whether it's already registered */ 961 chip = NULL; 962 guard(mutex)(®ister_mutex); 963 for (i = 0; i < SNDRV_CARDS; i++) { 964 if (usb_chip[i] && usb_chip[i]->dev == dev) { 965 if (atomic_read(&usb_chip[i]->shutdown)) { 966 dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n"); 967 err = -EIO; 968 goto __error; 969 } 970 chip = usb_chip[i]; 971 atomic_inc(&chip->active); /* avoid autopm */ 972 break; 973 } 974 } 975 if (! chip) { 976 err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id); 977 if (err < 0) 978 goto __error; 979 980 /* it's a fresh one. 981 * now look for an empty slot and create a new card instance 982 */ 983 for (i = 0; i < SNDRV_CARDS; i++) 984 if (!usb_chip[i] && 985 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && 986 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { 987 if (enable[i]) { 988 err = snd_usb_audio_create(intf, dev, i, quirk, 989 id, &chip); 990 if (err < 0) 991 goto __error; 992 break; 993 } else if (vid[i] != -1 || pid[i] != -1) { 994 dev_info(&dev->dev, 995 "device (%04x:%04x) is disabled\n", 996 USB_ID_VENDOR(id), 997 USB_ID_PRODUCT(id)); 998 err = -ENOENT; 999 goto __error; 1000 } 1001 } 1002 if (!chip) { 1003 dev_err(&dev->dev, "no available usb audio device\n"); 1004 err = -ENODEV; 1005 goto __error; 1006 } 1007 find_last_interface(chip); 1008 } 1009 1010 if (chip->num_interfaces >= MAX_CARD_INTERFACES) { 1011 dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n"); 1012 err = -EINVAL; 1013 goto __error; 1014 } 1015 1016 dev_set_drvdata(&dev->dev, chip); 1017 1018 if (ignore_ctl_error) 1019 chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR; 1020 1021 if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND) 1022 usb_disable_autosuspend(interface_to_usbdev(intf)); 1023 1024 /* 1025 * For devices with more than one control interface, we assume the 1026 * first contains the audio controls. We might need a more specific 1027 * check here in the future. 1028 */ 1029 if (!chip->ctrl_intf) 1030 chip->ctrl_intf = alts; 1031 1032 err = 1; /* continue */ 1033 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { 1034 /* need some special handlings */ 1035 err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk); 1036 if (err < 0) 1037 goto __error; 1038 } 1039 1040 if (err > 0) { 1041 /* create normal USB audio interfaces */ 1042 err = snd_usb_create_streams(chip, ifnum); 1043 if (err < 0) 1044 goto __error; 1045 err = snd_usb_create_mixer(chip, ifnum); 1046 if (err < 0) 1047 goto __error; 1048 } 1049 1050 if (chip->need_delayed_register) { 1051 dev_info(&dev->dev, 1052 "Found post-registration device assignment: %08x:%02x\n", 1053 chip->usb_id, ifnum); 1054 chip->need_delayed_register = false; /* clear again */ 1055 } 1056 1057 err = try_to_register_card(chip, ifnum); 1058 if (err < 0) 1059 goto __error_no_register; 1060 1061 if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) { 1062 /* don't want to fail when snd_media_device_create() fails */ 1063 snd_media_device_create(chip, intf); 1064 } 1065 1066 if (quirk) 1067 chip->quirk_type = quirk->type; 1068 1069 usb_chip[chip->index] = chip; 1070 chip->intf[chip->num_interfaces] = intf; 1071 chip->num_interfaces++; 1072 usb_set_intfdata(intf, chip); 1073 atomic_dec(&chip->active); 1074 1075 if (platform_ops && platform_ops->connect_cb) 1076 platform_ops->connect_cb(chip); 1077 1078 return 0; 1079 1080 __error: 1081 /* in the case of error in secondary interface, still try to register */ 1082 if (chip) 1083 try_to_register_card(chip, ifnum); 1084 1085 __error_no_register: 1086 if (chip) { 1087 /* chip->active is inside the chip->card object, 1088 * decrement before memory is possibly returned. 1089 */ 1090 atomic_dec(&chip->active); 1091 if (!chip->num_interfaces) { 1092 usb_audio_disconnect_components(chip); 1093 snd_card_free(chip->card); 1094 } 1095 } 1096 return err; 1097 } 1098 1099 /* 1100 * we need to take care of counter, since disconnection can be called also 1101 * many times as well as usb_audio_probe(). 1102 */ 1103 static bool __usb_audio_disconnect(struct usb_interface *intf, 1104 struct snd_usb_audio *chip, 1105 struct snd_card *card) 1106 { 1107 guard(mutex)(®ister_mutex); 1108 1109 if (platform_ops && platform_ops->disconnect_cb) 1110 platform_ops->disconnect_cb(chip); 1111 1112 if (atomic_inc_return(&chip->shutdown) == 1) { 1113 /* wait until all pending tasks done; 1114 * they are protected by snd_usb_lock_shutdown() 1115 */ 1116 snd_refcount_sync(&chip->usage_count); 1117 snd_card_disconnect(card); 1118 usb_audio_disconnect_components(chip); 1119 } 1120 1121 if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND) 1122 usb_enable_autosuspend(interface_to_usbdev(intf)); 1123 1124 chip->num_interfaces--; 1125 if (chip->num_interfaces > 0) 1126 return false; 1127 1128 usb_chip[chip->index] = NULL; 1129 return true; 1130 } 1131 1132 static void usb_audio_disconnect(struct usb_interface *intf) 1133 { 1134 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1135 struct snd_card *card; 1136 1137 if (chip == USB_AUDIO_IFACE_UNUSED) 1138 return; 1139 1140 card = chip->card; 1141 if (__usb_audio_disconnect(intf, chip, card)) 1142 snd_card_free_when_closed(card); 1143 } 1144 1145 /* lock the shutdown (disconnect) task and autoresume */ 1146 int snd_usb_lock_shutdown(struct snd_usb_audio *chip) 1147 { 1148 int err; 1149 1150 snd_refcount_get(&chip->usage_count); 1151 if (atomic_read(&chip->shutdown)) { 1152 err = -EIO; 1153 goto error; 1154 } 1155 err = snd_usb_autoresume(chip); 1156 if (err < 0) 1157 goto error; 1158 return 0; 1159 1160 error: 1161 snd_refcount_put(&chip->usage_count); 1162 return err; 1163 } 1164 EXPORT_SYMBOL_GPL(snd_usb_lock_shutdown); 1165 1166 /* autosuspend and unlock the shutdown */ 1167 void snd_usb_unlock_shutdown(struct snd_usb_audio *chip) 1168 { 1169 snd_usb_autosuspend(chip); 1170 snd_refcount_put(&chip->usage_count); 1171 } 1172 EXPORT_SYMBOL_GPL(snd_usb_unlock_shutdown); 1173 1174 int snd_usb_autoresume(struct snd_usb_audio *chip) 1175 { 1176 int i, err; 1177 1178 if (atomic_read(&chip->shutdown)) 1179 return -EIO; 1180 if (atomic_inc_return(&chip->active) != 1) 1181 return 0; 1182 1183 for (i = 0; i < chip->num_interfaces; i++) { 1184 err = usb_autopm_get_interface(chip->intf[i]); 1185 if (err < 0) { 1186 /* rollback */ 1187 while (--i >= 0) 1188 usb_autopm_put_interface(chip->intf[i]); 1189 atomic_dec(&chip->active); 1190 return err; 1191 } 1192 } 1193 return 0; 1194 } 1195 EXPORT_SYMBOL_GPL(snd_usb_autoresume); 1196 1197 void snd_usb_autosuspend(struct snd_usb_audio *chip) 1198 { 1199 int i; 1200 1201 if (atomic_read(&chip->shutdown)) 1202 return; 1203 if (!atomic_dec_and_test(&chip->active)) 1204 return; 1205 1206 for (i = 0; i < chip->num_interfaces; i++) 1207 usb_autopm_put_interface(chip->intf[i]); 1208 } 1209 EXPORT_SYMBOL_GPL(snd_usb_autosuspend); 1210 1211 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) 1212 { 1213 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1214 struct snd_usb_stream *as; 1215 struct snd_usb_endpoint *ep; 1216 struct usb_mixer_interface *mixer; 1217 struct list_head *p; 1218 1219 if (chip == USB_AUDIO_IFACE_UNUSED) 1220 return 0; 1221 1222 if (!chip->num_suspended_intf++) { 1223 list_for_each_entry(as, &chip->pcm_list, list) 1224 snd_usb_pcm_suspend(as); 1225 list_for_each_entry(ep, &chip->ep_list, list) 1226 snd_usb_endpoint_suspend(ep); 1227 list_for_each(p, &chip->midi_list) 1228 snd_usbmidi_suspend(p); 1229 list_for_each_entry(mixer, &chip->mixer_list, list) 1230 snd_usb_mixer_suspend(mixer); 1231 snd_usb_midi_v2_suspend_all(chip); 1232 } 1233 1234 if (!PMSG_IS_AUTO(message) && !chip->system_suspend) { 1235 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); 1236 chip->system_suspend = chip->num_suspended_intf; 1237 } 1238 1239 if (platform_ops && platform_ops->suspend_cb) 1240 platform_ops->suspend_cb(intf, message); 1241 1242 return 0; 1243 } 1244 1245 static int usb_audio_resume(struct usb_interface *intf) 1246 { 1247 struct snd_usb_audio *chip = usb_get_intfdata(intf); 1248 struct snd_usb_stream *as; 1249 struct usb_mixer_interface *mixer; 1250 struct list_head *p; 1251 int err = 0; 1252 1253 if (chip == USB_AUDIO_IFACE_UNUSED) 1254 return 0; 1255 1256 atomic_inc(&chip->active); /* avoid autopm */ 1257 if (chip->num_suspended_intf > 1) 1258 goto out; 1259 1260 list_for_each_entry(as, &chip->pcm_list, list) { 1261 err = snd_usb_pcm_resume(as); 1262 if (err < 0) { 1263 if (!chip->system_suspend) 1264 goto err_out; 1265 goto out; 1266 } 1267 } 1268 1269 /* 1270 * ALSA leaves material resumption to user space 1271 * we just notify and restart the mixers 1272 */ 1273 list_for_each_entry(mixer, &chip->mixer_list, list) { 1274 err = snd_usb_mixer_resume(mixer); 1275 if (err < 0) { 1276 if (!chip->system_suspend) 1277 goto err_out; 1278 goto out; 1279 } 1280 } 1281 1282 list_for_each(p, &chip->midi_list) { 1283 snd_usbmidi_resume(p); 1284 } 1285 1286 snd_usb_midi_v2_resume_all(chip); 1287 1288 if (platform_ops && platform_ops->resume_cb) 1289 platform_ops->resume_cb(intf); 1290 1291 out: 1292 if (chip->num_suspended_intf == chip->system_suspend) { 1293 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); 1294 chip->system_suspend = 0; 1295 } 1296 chip->num_suspended_intf--; 1297 1298 err_out: 1299 atomic_dec(&chip->active); /* allow autopm after this point */ 1300 return err; 1301 } 1302 1303 static const struct usb_device_id usb_audio_ids [] = { 1304 #include "quirks-table.h" 1305 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), 1306 .bInterfaceClass = USB_CLASS_AUDIO, 1307 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, 1308 { } /* Terminating entry */ 1309 }; 1310 MODULE_DEVICE_TABLE(usb, usb_audio_ids); 1311 1312 /* 1313 * entry point for linux usb interface 1314 */ 1315 1316 static struct usb_driver usb_audio_driver = { 1317 .name = "snd-usb-audio", 1318 .probe = usb_audio_probe, 1319 .disconnect = usb_audio_disconnect, 1320 .suspend = usb_audio_suspend, 1321 .resume = usb_audio_resume, 1322 .reset_resume = usb_audio_resume, 1323 .id_table = usb_audio_ids, 1324 .supports_autosuspend = 1, 1325 }; 1326 1327 module_usb_driver(usb_audio_driver); 1328