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