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