18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *   (Tentative) USB Audio Driver for ALSA
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *   Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *   Many codes borrowed from audio.c by
88c2ecf20Sopenharmony_ci *	    Alan Cox (alan@lxorguk.ukuu.org.uk)
98c2ecf20Sopenharmony_ci *	    Thomas Sailer (sailer@ife.ee.ethz.ch)
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *   Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com>
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  NOTES:
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *   - the linked URBs would be preferred but not used so far because of
168c2ecf20Sopenharmony_ci *     the instability of unlinking.
178c2ecf20Sopenharmony_ci *   - type II is not supported properly.  there is no device which supports
188c2ecf20Sopenharmony_ci *     this type *correctly*.  SB extigy looks as if it supports, but it's
198c2ecf20Sopenharmony_ci *     indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/bitops.h>
248c2ecf20Sopenharmony_ci#include <linux/init.h>
258c2ecf20Sopenharmony_ci#include <linux/list.h>
268c2ecf20Sopenharmony_ci#include <linux/slab.h>
278c2ecf20Sopenharmony_ci#include <linux/string.h>
288c2ecf20Sopenharmony_ci#include <linux/ctype.h>
298c2ecf20Sopenharmony_ci#include <linux/usb.h>
308c2ecf20Sopenharmony_ci#include <linux/moduleparam.h>
318c2ecf20Sopenharmony_ci#include <linux/mutex.h>
328c2ecf20Sopenharmony_ci#include <linux/usb/audio.h>
338c2ecf20Sopenharmony_ci#include <linux/usb/audio-v2.h>
348c2ecf20Sopenharmony_ci#include <linux/usb/audio-v3.h>
358c2ecf20Sopenharmony_ci#include <linux/module.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <sound/control.h>
388c2ecf20Sopenharmony_ci#include <sound/core.h>
398c2ecf20Sopenharmony_ci#include <sound/info.h>
408c2ecf20Sopenharmony_ci#include <sound/pcm.h>
418c2ecf20Sopenharmony_ci#include <sound/pcm_params.h>
428c2ecf20Sopenharmony_ci#include <sound/initval.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "usbaudio.h"
458c2ecf20Sopenharmony_ci#include "card.h"
468c2ecf20Sopenharmony_ci#include "midi.h"
478c2ecf20Sopenharmony_ci#include "mixer.h"
488c2ecf20Sopenharmony_ci#include "proc.h"
498c2ecf20Sopenharmony_ci#include "quirks.h"
508c2ecf20Sopenharmony_ci#include "endpoint.h"
518c2ecf20Sopenharmony_ci#include "helper.h"
528c2ecf20Sopenharmony_ci#include "debug.h"
538c2ecf20Sopenharmony_ci#include "pcm.h"
548c2ecf20Sopenharmony_ci#include "format.h"
558c2ecf20Sopenharmony_ci#include "power.h"
568c2ecf20Sopenharmony_ci#include "stream.h"
578c2ecf20Sopenharmony_ci#include "media.h"
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciMODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
608c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("USB Audio");
618c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
628c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
668c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
678c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
688c2ecf20Sopenharmony_ci/* Vendor/product IDs for this card */
698c2ecf20Sopenharmony_cistatic int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
708c2ecf20Sopenharmony_cistatic int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
718c2ecf20Sopenharmony_cistatic int device_setup[SNDRV_CARDS]; /* device parameter for this card */
728c2ecf20Sopenharmony_cistatic bool ignore_ctl_error;
738c2ecf20Sopenharmony_cistatic bool autoclock = true;
748c2ecf20Sopenharmony_cistatic char *quirk_alias[SNDRV_CARDS];
758c2ecf20Sopenharmony_cistatic char *delayed_register[SNDRV_CARDS];
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cibool snd_usb_use_vmalloc = true;
788c2ecf20Sopenharmony_cibool snd_usb_skip_validation;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444);
818c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
828c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444);
838c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
848c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444);
858c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable USB audio adapter.");
868c2ecf20Sopenharmony_cimodule_param_array(vid, int, NULL, 0444);
878c2ecf20Sopenharmony_ciMODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
888c2ecf20Sopenharmony_cimodule_param_array(pid, int, NULL, 0444);
898c2ecf20Sopenharmony_ciMODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
908c2ecf20Sopenharmony_cimodule_param_array(device_setup, int, NULL, 0444);
918c2ecf20Sopenharmony_ciMODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
928c2ecf20Sopenharmony_cimodule_param(ignore_ctl_error, bool, 0444);
938c2ecf20Sopenharmony_ciMODULE_PARM_DESC(ignore_ctl_error,
948c2ecf20Sopenharmony_ci		 "Ignore errors from USB controller for mixer interfaces.");
958c2ecf20Sopenharmony_cimodule_param(autoclock, bool, 0444);
968c2ecf20Sopenharmony_ciMODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
978c2ecf20Sopenharmony_cimodule_param_array(quirk_alias, charp, NULL, 0444);
988c2ecf20Sopenharmony_ciMODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
998c2ecf20Sopenharmony_cimodule_param_array(delayed_register, charp, NULL, 0444);
1008c2ecf20Sopenharmony_ciMODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4.");
1018c2ecf20Sopenharmony_cimodule_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
1028c2ecf20Sopenharmony_ciMODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
1038c2ecf20Sopenharmony_cimodule_param_named(skip_validation, snd_usb_skip_validation, bool, 0444);
1048c2ecf20Sopenharmony_ciMODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no).");
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/*
1078c2ecf20Sopenharmony_ci * we keep the snd_usb_audio_t instances by ourselves for merging
1088c2ecf20Sopenharmony_ci * the all interfaces on the same card as one sound device.
1098c2ecf20Sopenharmony_ci */
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(register_mutex);
1128c2ecf20Sopenharmony_cistatic struct snd_usb_audio *usb_chip[SNDRV_CARDS];
1138c2ecf20Sopenharmony_cistatic struct usb_driver usb_audio_driver;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/*
1168c2ecf20Sopenharmony_ci * disconnect streams
1178c2ecf20Sopenharmony_ci * called from usb_audio_disconnect()
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_cistatic void snd_usb_stream_disconnect(struct snd_usb_stream *as)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	int idx;
1228c2ecf20Sopenharmony_ci	struct snd_usb_substream *subs;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	for (idx = 0; idx < 2; idx++) {
1258c2ecf20Sopenharmony_ci		subs = &as->substream[idx];
1268c2ecf20Sopenharmony_ci		if (!subs->num_formats)
1278c2ecf20Sopenharmony_ci			continue;
1288c2ecf20Sopenharmony_ci		subs->interface = -1;
1298c2ecf20Sopenharmony_ci		subs->data_endpoint = NULL;
1308c2ecf20Sopenharmony_ci		subs->sync_endpoint = NULL;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	struct usb_device *dev = chip->dev;
1378c2ecf20Sopenharmony_ci	struct usb_host_interface *alts;
1388c2ecf20Sopenharmony_ci	struct usb_interface_descriptor *altsd;
1398c2ecf20Sopenharmony_ci	struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (!iface) {
1428c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "%u:%d : does not exist\n",
1438c2ecf20Sopenharmony_ci			ctrlif, interface);
1448c2ecf20Sopenharmony_ci		return -EINVAL;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	alts = &iface->altsetting[0];
1488c2ecf20Sopenharmony_ci	altsd = get_iface_desc(alts);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/*
1518c2ecf20Sopenharmony_ci	 * Android with both accessory and audio interfaces enabled gets the
1528c2ecf20Sopenharmony_ci	 * interface numbers wrong.
1538c2ecf20Sopenharmony_ci	 */
1548c2ecf20Sopenharmony_ci	if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
1558c2ecf20Sopenharmony_ci	     chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
1568c2ecf20Sopenharmony_ci	    interface == 0 &&
1578c2ecf20Sopenharmony_ci	    altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
1588c2ecf20Sopenharmony_ci	    altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
1598c2ecf20Sopenharmony_ci		interface = 2;
1608c2ecf20Sopenharmony_ci		iface = usb_ifnum_to_if(dev, interface);
1618c2ecf20Sopenharmony_ci		if (!iface)
1628c2ecf20Sopenharmony_ci			return -EINVAL;
1638c2ecf20Sopenharmony_ci		alts = &iface->altsetting[0];
1648c2ecf20Sopenharmony_ci		altsd = get_iface_desc(alts);
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (usb_interface_claimed(iface)) {
1688c2ecf20Sopenharmony_ci		dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
1698c2ecf20Sopenharmony_ci			ctrlif, interface);
1708c2ecf20Sopenharmony_ci		return -EINVAL;
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
1748c2ecf20Sopenharmony_ci	     altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
1758c2ecf20Sopenharmony_ci	    altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
1768c2ecf20Sopenharmony_ci		int err = __snd_usbmidi_create(chip->card, iface,
1778c2ecf20Sopenharmony_ci					     &chip->midi_list, NULL,
1788c2ecf20Sopenharmony_ci					     chip->usb_id);
1798c2ecf20Sopenharmony_ci		if (err < 0) {
1808c2ecf20Sopenharmony_ci			dev_err(&dev->dev,
1818c2ecf20Sopenharmony_ci				"%u:%d: cannot create sequencer device\n",
1828c2ecf20Sopenharmony_ci				ctrlif, interface);
1838c2ecf20Sopenharmony_ci			return -EINVAL;
1848c2ecf20Sopenharmony_ci		}
1858c2ecf20Sopenharmony_ci		return usb_driver_claim_interface(&usb_audio_driver, iface,
1868c2ecf20Sopenharmony_ci						  USB_AUDIO_IFACE_UNUSED);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
1908c2ecf20Sopenharmony_ci	     altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
1918c2ecf20Sopenharmony_ci	    altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
1928c2ecf20Sopenharmony_ci		dev_dbg(&dev->dev,
1938c2ecf20Sopenharmony_ci			"%u:%d: skipping non-supported interface %d\n",
1948c2ecf20Sopenharmony_ci			ctrlif, interface, altsd->bInterfaceClass);
1958c2ecf20Sopenharmony_ci		/* skip non-supported classes */
1968c2ecf20Sopenharmony_ci		return -EINVAL;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
2008c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "low speed audio streaming not supported\n");
2018c2ecf20Sopenharmony_ci		return -EINVAL;
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (! snd_usb_parse_audio_interface(chip, interface)) {
2058c2ecf20Sopenharmony_ci		usb_set_interface(dev, interface, 0); /* reset the current interface */
2068c2ecf20Sopenharmony_ci		return usb_driver_claim_interface(&usb_audio_driver, iface,
2078c2ecf20Sopenharmony_ci						  USB_AUDIO_IFACE_UNUSED);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/*
2148c2ecf20Sopenharmony_ci * parse audio control descriptor and create pcm/midi streams
2158c2ecf20Sopenharmony_ci */
2168c2ecf20Sopenharmony_cistatic int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct usb_device *dev = chip->dev;
2198c2ecf20Sopenharmony_ci	struct usb_host_interface *host_iface;
2208c2ecf20Sopenharmony_ci	struct usb_interface_descriptor *altsd;
2218c2ecf20Sopenharmony_ci	int i, protocol;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* find audiocontrol interface */
2248c2ecf20Sopenharmony_ci	host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
2258c2ecf20Sopenharmony_ci	altsd = get_iface_desc(host_iface);
2268c2ecf20Sopenharmony_ci	protocol = altsd->bInterfaceProtocol;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	switch (protocol) {
2298c2ecf20Sopenharmony_ci	default:
2308c2ecf20Sopenharmony_ci		dev_warn(&dev->dev,
2318c2ecf20Sopenharmony_ci			 "unknown interface protocol %#02x, assuming v1\n",
2328c2ecf20Sopenharmony_ci			 protocol);
2338c2ecf20Sopenharmony_ci		fallthrough;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	case UAC_VERSION_1: {
2368c2ecf20Sopenharmony_ci		struct uac1_ac_header_descriptor *h1;
2378c2ecf20Sopenharmony_ci		int rest_bytes;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		h1 = snd_usb_find_csint_desc(host_iface->extra,
2408c2ecf20Sopenharmony_ci							 host_iface->extralen,
2418c2ecf20Sopenharmony_ci							 NULL, UAC_HEADER);
2428c2ecf20Sopenharmony_ci		if (!h1 || h1->bLength < sizeof(*h1)) {
2438c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "cannot find UAC_HEADER\n");
2448c2ecf20Sopenharmony_ci			return -EINVAL;
2458c2ecf20Sopenharmony_ci		}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci		rest_bytes = (void *)(host_iface->extra +
2488c2ecf20Sopenharmony_ci				host_iface->extralen) - (void *)h1;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci		/* just to be sure -- this shouldn't hit at all */
2518c2ecf20Sopenharmony_ci		if (rest_bytes <= 0) {
2528c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "invalid control header\n");
2538c2ecf20Sopenharmony_ci			return -EINVAL;
2548c2ecf20Sopenharmony_ci		}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci		if (rest_bytes < sizeof(*h1)) {
2578c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "too short v1 buffer descriptor\n");
2588c2ecf20Sopenharmony_ci			return -EINVAL;
2598c2ecf20Sopenharmony_ci		}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		if (!h1->bInCollection) {
2628c2ecf20Sopenharmony_ci			dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
2638c2ecf20Sopenharmony_ci			return -EINVAL;
2648c2ecf20Sopenharmony_ci		}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci		if (rest_bytes < h1->bLength) {
2678c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "invalid buffer length (v1)\n");
2688c2ecf20Sopenharmony_ci			return -EINVAL;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
2728c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
2738c2ecf20Sopenharmony_ci			return -EINVAL;
2748c2ecf20Sopenharmony_ci		}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci		for (i = 0; i < h1->bInCollection; i++)
2778c2ecf20Sopenharmony_ci			snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci		break;
2808c2ecf20Sopenharmony_ci	}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	case UAC_VERSION_2:
2838c2ecf20Sopenharmony_ci	case UAC_VERSION_3: {
2848c2ecf20Sopenharmony_ci		struct usb_interface_assoc_descriptor *assoc =
2858c2ecf20Sopenharmony_ci			usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci		if (!assoc) {
2888c2ecf20Sopenharmony_ci			/*
2898c2ecf20Sopenharmony_ci			 * Firmware writers cannot count to three.  So to find
2908c2ecf20Sopenharmony_ci			 * the IAD on the NuForce UDH-100, also check the next
2918c2ecf20Sopenharmony_ci			 * interface.
2928c2ecf20Sopenharmony_ci			 */
2938c2ecf20Sopenharmony_ci			struct usb_interface *iface =
2948c2ecf20Sopenharmony_ci				usb_ifnum_to_if(dev, ctrlif + 1);
2958c2ecf20Sopenharmony_ci			if (iface &&
2968c2ecf20Sopenharmony_ci			    iface->intf_assoc &&
2978c2ecf20Sopenharmony_ci			    iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
2988c2ecf20Sopenharmony_ci			    iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
2998c2ecf20Sopenharmony_ci				assoc = iface->intf_assoc;
3008c2ecf20Sopenharmony_ci		}
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci		if (!assoc) {
3038c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
3048c2ecf20Sopenharmony_ci			return -EINVAL;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci		if (protocol == UAC_VERSION_3) {
3088c2ecf20Sopenharmony_ci			int badd = assoc->bFunctionSubClass;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci			if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
3118c2ecf20Sopenharmony_ci			    (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
3128c2ecf20Sopenharmony_ci			     badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
3138c2ecf20Sopenharmony_ci				dev_err(&dev->dev,
3148c2ecf20Sopenharmony_ci					"Unsupported UAC3 BADD profile\n");
3158c2ecf20Sopenharmony_ci				return -EINVAL;
3168c2ecf20Sopenharmony_ci			}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci			chip->badd_profile = badd;
3198c2ecf20Sopenharmony_ci		}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci		for (i = 0; i < assoc->bInterfaceCount; i++) {
3228c2ecf20Sopenharmony_ci			int intf = assoc->bFirstInterface + i;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci			if (intf != ctrlif)
3258c2ecf20Sopenharmony_ci				snd_usb_create_stream(chip, ctrlif, intf);
3268c2ecf20Sopenharmony_ci		}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci		break;
3298c2ecf20Sopenharmony_ci	}
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci/*
3368c2ecf20Sopenharmony_ci * Profile name preset table
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistruct usb_audio_device_name {
3398c2ecf20Sopenharmony_ci	u32 id;
3408c2ecf20Sopenharmony_ci	const char *vendor_name;
3418c2ecf20Sopenharmony_ci	const char *product_name;
3428c2ecf20Sopenharmony_ci	const char *profile_name;	/* override card->longname */
3438c2ecf20Sopenharmony_ci};
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci#define PROFILE_NAME(vid, pid, vendor, product, profile)	 \
3468c2ecf20Sopenharmony_ci	{ .id = USB_ID(vid, pid), .vendor_name = (vendor),	 \
3478c2ecf20Sopenharmony_ci	  .product_name = (product), .profile_name = (profile) }
3488c2ecf20Sopenharmony_ci#define DEVICE_NAME(vid, pid, vendor, product) \
3498c2ecf20Sopenharmony_ci	PROFILE_NAME(vid, pid, vendor, product, NULL)
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci/* vendor/product and profile name presets, sorted in device id order */
3528c2ecf20Sopenharmony_cistatic const struct usb_audio_device_name usb_audio_names[] = {
3538c2ecf20Sopenharmony_ci	/* HP Thunderbolt Dock Audio Headset */
3548c2ecf20Sopenharmony_ci	PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset",
3558c2ecf20Sopenharmony_ci		     "HP-Thunderbolt-Dock-Audio-Headset"),
3568c2ecf20Sopenharmony_ci	/* HP Thunderbolt Dock Audio Module */
3578c2ecf20Sopenharmony_ci	PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module",
3588c2ecf20Sopenharmony_ci		     "HP-Thunderbolt-Dock-Audio-Module"),
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	/* Two entries for Gigabyte TRX40 Aorus Master:
3618c2ecf20Sopenharmony_ci	 * TRX40 Aorus Master has two USB-audio devices, one for the front
3628c2ecf20Sopenharmony_ci	 * headphone with ESS SABRE9218 DAC chip, while another for the rest
3638c2ecf20Sopenharmony_ci	 * I/O (the rear panel and the front mic) with Realtek ALC1220-VB.
3648c2ecf20Sopenharmony_ci	 * Here we provide two distinct names for making UCM profiles easier.
3658c2ecf20Sopenharmony_ci	 */
3668c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone",
3678c2ecf20Sopenharmony_ci		     "Gigabyte-Aorus-Master-Front-Headphone"),
3688c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio",
3698c2ecf20Sopenharmony_ci		     "Gigabyte-Aorus-Master-Main-Audio"),
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	/* Gigabyte TRX40 Aorus Pro WiFi */
3728c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0414, 0xa002,
3738c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	/* Creative/E-Mu devices */
3768c2ecf20Sopenharmony_ci	DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"),
3778c2ecf20Sopenharmony_ci	/* Creative/Toshiba Multimedia Center SB-0500 */
3788c2ecf20Sopenharmony_ci	DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"),
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"),
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	/* ASUS ROG Zenith II: this machine has also two devices, one for
3838c2ecf20Sopenharmony_ci	 * the front headphone and another for the rest
3848c2ecf20Sopenharmony_ci	 */
3858c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0b05, 0x1915, "ASUS", "Zenith II Front Headphone",
3868c2ecf20Sopenharmony_ci		     "Zenith-II-Front-Headphone"),
3878c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0b05, 0x1916, "ASUS", "Zenith II Main Audio",
3888c2ecf20Sopenharmony_ci		     "Zenith-II-Main-Audio"),
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	/* ASUS ROG Strix */
3918c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0b05, 0x1917,
3928c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
3938c2ecf20Sopenharmony_ci	/* ASUS PRIME TRX40 PRO-S */
3948c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0b05, 0x1918,
3958c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	/* Dell WD15 Dock */
3988c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"),
3998c2ecf20Sopenharmony_ci	/* Dell WD19 Dock */
4008c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"),
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"),
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	/*
4058c2ecf20Sopenharmony_ci	 * The original product_name is "USB Sound Device", however this name
4068c2ecf20Sopenharmony_ci	 * is also used by the CM106 based cards, so make it unique.
4078c2ecf20Sopenharmony_ci	 */
4088c2ecf20Sopenharmony_ci	DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"),
4098c2ecf20Sopenharmony_ci	DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"),
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/* MSI TRX40 Creator */
4128c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0db0, 0x0d64,
4138c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
4148c2ecf20Sopenharmony_ci	/* MSI TRX40 */
4158c2ecf20Sopenharmony_ci	PROFILE_NAME(0x0db0, 0x543d,
4168c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	/* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */
4198c2ecf20Sopenharmony_ci	DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"),
4208c2ecf20Sopenharmony_ci	DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"),
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci	/* aka. Serato Scratch Live DJ Box */
4238c2ecf20Sopenharmony_ci	DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"),
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
4268c2ecf20Sopenharmony_ci	PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear",
4278c2ecf20Sopenharmony_ci		     "Lenovo-ThinkStation-P620-Rear"),
4288c2ecf20Sopenharmony_ci	/* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
4298c2ecf20Sopenharmony_ci	PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main",
4308c2ecf20Sopenharmony_ci		     "Lenovo-ThinkStation-P620-Main"),
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* Asrock TRX40 Creator */
4338c2ecf20Sopenharmony_ci	PROFILE_NAME(0x26ce, 0x0a01,
4348c2ecf20Sopenharmony_ci		     "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	{ } /* terminator */
4378c2ecf20Sopenharmony_ci};
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_cistatic const struct usb_audio_device_name *
4408c2ecf20Sopenharmony_cilookup_device_name(u32 id)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	static const struct usb_audio_device_name *p;
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	for (p = usb_audio_names; p->id; p++)
4458c2ecf20Sopenharmony_ci		if (p->id == id)
4468c2ecf20Sopenharmony_ci			return p;
4478c2ecf20Sopenharmony_ci	return NULL;
4488c2ecf20Sopenharmony_ci}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci/*
4518c2ecf20Sopenharmony_ci * free the chip instance
4528c2ecf20Sopenharmony_ci *
4538c2ecf20Sopenharmony_ci * here we have to do not much, since pcm and controls are already freed
4548c2ecf20Sopenharmony_ci *
4558c2ecf20Sopenharmony_ci */
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic void snd_usb_audio_free(struct snd_card *card)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = card->private_data;
4608c2ecf20Sopenharmony_ci	struct snd_usb_endpoint *ep, *n;
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_ci	list_for_each_entry_safe(ep, n, &chip->ep_list, list)
4638c2ecf20Sopenharmony_ci		snd_usb_endpoint_free(ep);
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	mutex_destroy(&chip->mutex);
4668c2ecf20Sopenharmony_ci	if (!atomic_read(&chip->shutdown))
4678c2ecf20Sopenharmony_ci		dev_set_drvdata(&chip->dev->dev, NULL);
4688c2ecf20Sopenharmony_ci}
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic void usb_audio_make_shortname(struct usb_device *dev,
4718c2ecf20Sopenharmony_ci				     struct snd_usb_audio *chip,
4728c2ecf20Sopenharmony_ci				     const struct snd_usb_audio_quirk *quirk)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	struct snd_card *card = chip->card;
4758c2ecf20Sopenharmony_ci	const struct usb_audio_device_name *preset;
4768c2ecf20Sopenharmony_ci	const char *s = NULL;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	preset = lookup_device_name(chip->usb_id);
4798c2ecf20Sopenharmony_ci	if (preset && preset->product_name)
4808c2ecf20Sopenharmony_ci		s = preset->product_name;
4818c2ecf20Sopenharmony_ci	else if (quirk && quirk->product_name)
4828c2ecf20Sopenharmony_ci		s = quirk->product_name;
4838c2ecf20Sopenharmony_ci	if (s && *s) {
4848c2ecf20Sopenharmony_ci		strlcpy(card->shortname, s, sizeof(card->shortname));
4858c2ecf20Sopenharmony_ci		return;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	/* retrieve the device string as shortname */
4898c2ecf20Sopenharmony_ci	if (!dev->descriptor.iProduct ||
4908c2ecf20Sopenharmony_ci	    usb_string(dev, dev->descriptor.iProduct,
4918c2ecf20Sopenharmony_ci		       card->shortname, sizeof(card->shortname)) <= 0) {
4928c2ecf20Sopenharmony_ci		/* no name available from anywhere, so use ID */
4938c2ecf20Sopenharmony_ci		sprintf(card->shortname, "USB Device %#04x:%#04x",
4948c2ecf20Sopenharmony_ci			USB_ID_VENDOR(chip->usb_id),
4958c2ecf20Sopenharmony_ci			USB_ID_PRODUCT(chip->usb_id));
4968c2ecf20Sopenharmony_ci	}
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	strim(card->shortname);
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic void usb_audio_make_longname(struct usb_device *dev,
5028c2ecf20Sopenharmony_ci				    struct snd_usb_audio *chip,
5038c2ecf20Sopenharmony_ci				    const struct snd_usb_audio_quirk *quirk)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	struct snd_card *card = chip->card;
5068c2ecf20Sopenharmony_ci	const struct usb_audio_device_name *preset;
5078c2ecf20Sopenharmony_ci	const char *s = NULL;
5088c2ecf20Sopenharmony_ci	int len;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	preset = lookup_device_name(chip->usb_id);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	/* shortcut - if any pre-defined string is given, use it */
5138c2ecf20Sopenharmony_ci	if (preset && preset->profile_name)
5148c2ecf20Sopenharmony_ci		s = preset->profile_name;
5158c2ecf20Sopenharmony_ci	if (s && *s) {
5168c2ecf20Sopenharmony_ci		strlcpy(card->longname, s, sizeof(card->longname));
5178c2ecf20Sopenharmony_ci		return;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	if (preset && preset->vendor_name)
5218c2ecf20Sopenharmony_ci		s = preset->vendor_name;
5228c2ecf20Sopenharmony_ci	else if (quirk && quirk->vendor_name)
5238c2ecf20Sopenharmony_ci		s = quirk->vendor_name;
5248c2ecf20Sopenharmony_ci	if (s && *s) {
5258c2ecf20Sopenharmony_ci		len = strlcpy(card->longname, s, sizeof(card->longname));
5268c2ecf20Sopenharmony_ci	} else {
5278c2ecf20Sopenharmony_ci		/* retrieve the vendor and device strings as longname */
5288c2ecf20Sopenharmony_ci		if (dev->descriptor.iManufacturer)
5298c2ecf20Sopenharmony_ci			len = usb_string(dev, dev->descriptor.iManufacturer,
5308c2ecf20Sopenharmony_ci					 card->longname, sizeof(card->longname));
5318c2ecf20Sopenharmony_ci		else
5328c2ecf20Sopenharmony_ci			len = 0;
5338c2ecf20Sopenharmony_ci		/* we don't really care if there isn't any vendor string */
5348c2ecf20Sopenharmony_ci	}
5358c2ecf20Sopenharmony_ci	if (len > 0) {
5368c2ecf20Sopenharmony_ci		strim(card->longname);
5378c2ecf20Sopenharmony_ci		if (*card->longname)
5388c2ecf20Sopenharmony_ci			strlcat(card->longname, " ", sizeof(card->longname));
5398c2ecf20Sopenharmony_ci	}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	strlcat(card->longname, card->shortname, sizeof(card->longname));
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	len = strlcat(card->longname, " at ", sizeof(card->longname));
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	if (len < sizeof(card->longname))
5468c2ecf20Sopenharmony_ci		usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	switch (snd_usb_get_speed(dev)) {
5498c2ecf20Sopenharmony_ci	case USB_SPEED_LOW:
5508c2ecf20Sopenharmony_ci		strlcat(card->longname, ", low speed", sizeof(card->longname));
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci	case USB_SPEED_FULL:
5538c2ecf20Sopenharmony_ci		strlcat(card->longname, ", full speed", sizeof(card->longname));
5548c2ecf20Sopenharmony_ci		break;
5558c2ecf20Sopenharmony_ci	case USB_SPEED_HIGH:
5568c2ecf20Sopenharmony_ci		strlcat(card->longname, ", high speed", sizeof(card->longname));
5578c2ecf20Sopenharmony_ci		break;
5588c2ecf20Sopenharmony_ci	case USB_SPEED_SUPER:
5598c2ecf20Sopenharmony_ci		strlcat(card->longname, ", super speed", sizeof(card->longname));
5608c2ecf20Sopenharmony_ci		break;
5618c2ecf20Sopenharmony_ci	case USB_SPEED_SUPER_PLUS:
5628c2ecf20Sopenharmony_ci		strlcat(card->longname, ", super speed plus", sizeof(card->longname));
5638c2ecf20Sopenharmony_ci		break;
5648c2ecf20Sopenharmony_ci	default:
5658c2ecf20Sopenharmony_ci		break;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/*
5708c2ecf20Sopenharmony_ci * create a chip instance and set its names.
5718c2ecf20Sopenharmony_ci */
5728c2ecf20Sopenharmony_cistatic int snd_usb_audio_create(struct usb_interface *intf,
5738c2ecf20Sopenharmony_ci				struct usb_device *dev, int idx,
5748c2ecf20Sopenharmony_ci				const struct snd_usb_audio_quirk *quirk,
5758c2ecf20Sopenharmony_ci				unsigned int usb_id,
5768c2ecf20Sopenharmony_ci				struct snd_usb_audio **rchip)
5778c2ecf20Sopenharmony_ci{
5788c2ecf20Sopenharmony_ci	struct snd_card *card;
5798c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip;
5808c2ecf20Sopenharmony_ci	int err;
5818c2ecf20Sopenharmony_ci	char component[14];
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	*rchip = NULL;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	switch (snd_usb_get_speed(dev)) {
5868c2ecf20Sopenharmony_ci	case USB_SPEED_LOW:
5878c2ecf20Sopenharmony_ci	case USB_SPEED_FULL:
5888c2ecf20Sopenharmony_ci	case USB_SPEED_HIGH:
5898c2ecf20Sopenharmony_ci	case USB_SPEED_WIRELESS:
5908c2ecf20Sopenharmony_ci	case USB_SPEED_SUPER:
5918c2ecf20Sopenharmony_ci	case USB_SPEED_SUPER_PLUS:
5928c2ecf20Sopenharmony_ci		break;
5938c2ecf20Sopenharmony_ci	default:
5948c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
5958c2ecf20Sopenharmony_ci		return -ENXIO;
5968c2ecf20Sopenharmony_ci	}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
5998c2ecf20Sopenharmony_ci			   sizeof(*chip), &card);
6008c2ecf20Sopenharmony_ci	if (err < 0) {
6018c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "cannot create card instance %d\n", idx);
6028c2ecf20Sopenharmony_ci		return err;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	chip = card->private_data;
6068c2ecf20Sopenharmony_ci	mutex_init(&chip->mutex);
6078c2ecf20Sopenharmony_ci	init_waitqueue_head(&chip->shutdown_wait);
6088c2ecf20Sopenharmony_ci	chip->index = idx;
6098c2ecf20Sopenharmony_ci	chip->dev = dev;
6108c2ecf20Sopenharmony_ci	chip->card = card;
6118c2ecf20Sopenharmony_ci	chip->setup = device_setup[idx];
6128c2ecf20Sopenharmony_ci	chip->autoclock = autoclock;
6138c2ecf20Sopenharmony_ci	atomic_set(&chip->active, 1); /* avoid autopm during probing */
6148c2ecf20Sopenharmony_ci	atomic_set(&chip->usage_count, 0);
6158c2ecf20Sopenharmony_ci	atomic_set(&chip->shutdown, 0);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	chip->usb_id = usb_id;
6188c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&chip->pcm_list);
6198c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&chip->ep_list);
6208c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&chip->midi_list);
6218c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&chip->mixer_list);
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	card->private_free = snd_usb_audio_free;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	strcpy(card->driver, "USB-Audio");
6268c2ecf20Sopenharmony_ci	sprintf(component, "USB%04x:%04x",
6278c2ecf20Sopenharmony_ci		USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
6288c2ecf20Sopenharmony_ci	snd_component_add(card, component);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	usb_audio_make_shortname(dev, chip, quirk);
6318c2ecf20Sopenharmony_ci	usb_audio_make_longname(dev, chip, quirk);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	snd_usb_audio_create_proc(chip);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	*rchip = chip;
6368c2ecf20Sopenharmony_ci	return 0;
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci/* look for a matching quirk alias id */
6408c2ecf20Sopenharmony_cistatic bool get_alias_id(struct usb_device *dev, unsigned int *id)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	int i;
6438c2ecf20Sopenharmony_ci	unsigned int src, dst;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
6468c2ecf20Sopenharmony_ci		if (!quirk_alias[i] ||
6478c2ecf20Sopenharmony_ci		    sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
6488c2ecf20Sopenharmony_ci		    src != *id)
6498c2ecf20Sopenharmony_ci			continue;
6508c2ecf20Sopenharmony_ci		dev_info(&dev->dev,
6518c2ecf20Sopenharmony_ci			 "device (%04x:%04x): applying quirk alias %04x:%04x\n",
6528c2ecf20Sopenharmony_ci			 USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
6538c2ecf20Sopenharmony_ci			 USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
6548c2ecf20Sopenharmony_ci		*id = dst;
6558c2ecf20Sopenharmony_ci		return true;
6568c2ecf20Sopenharmony_ci	}
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci	return false;
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic bool check_delayed_register_option(struct snd_usb_audio *chip, int iface)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	int i;
6648c2ecf20Sopenharmony_ci	unsigned int id, inum;
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
6678c2ecf20Sopenharmony_ci		if (delayed_register[i] &&
6688c2ecf20Sopenharmony_ci		    sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
6698c2ecf20Sopenharmony_ci		    id == chip->usb_id)
6708c2ecf20Sopenharmony_ci			return iface < inum;
6718c2ecf20Sopenharmony_ci	}
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	return false;
6748c2ecf20Sopenharmony_ci}
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_cistatic const struct usb_device_id usb_audio_ids[]; /* defined below */
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci/* look for the corresponding quirk */
6798c2ecf20Sopenharmony_cistatic const struct snd_usb_audio_quirk *
6808c2ecf20Sopenharmony_ciget_alias_quirk(struct usb_device *dev, unsigned int id)
6818c2ecf20Sopenharmony_ci{
6828c2ecf20Sopenharmony_ci	const struct usb_device_id *p;
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	for (p = usb_audio_ids; p->match_flags; p++) {
6858c2ecf20Sopenharmony_ci		/* FIXME: this checks only vendor:product pair in the list */
6868c2ecf20Sopenharmony_ci		if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
6878c2ecf20Sopenharmony_ci		    USB_DEVICE_ID_MATCH_DEVICE &&
6888c2ecf20Sopenharmony_ci		    p->idVendor == USB_ID_VENDOR(id) &&
6898c2ecf20Sopenharmony_ci		    p->idProduct == USB_ID_PRODUCT(id))
6908c2ecf20Sopenharmony_ci			return (const struct snd_usb_audio_quirk *)p->driver_info;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return NULL;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci/*
6978c2ecf20Sopenharmony_ci * probe the active usb device
6988c2ecf20Sopenharmony_ci *
6998c2ecf20Sopenharmony_ci * note that this can be called multiple times per a device, when it
7008c2ecf20Sopenharmony_ci * includes multiple audio control interfaces.
7018c2ecf20Sopenharmony_ci *
7028c2ecf20Sopenharmony_ci * thus we check the usb device pointer and creates the card instance
7038c2ecf20Sopenharmony_ci * only at the first time.  the successive calls of this function will
7048c2ecf20Sopenharmony_ci * append the pcm interface to the corresponding card.
7058c2ecf20Sopenharmony_ci */
7068c2ecf20Sopenharmony_cistatic int usb_audio_probe(struct usb_interface *intf,
7078c2ecf20Sopenharmony_ci			   const struct usb_device_id *usb_id)
7088c2ecf20Sopenharmony_ci{
7098c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(intf);
7108c2ecf20Sopenharmony_ci	const struct snd_usb_audio_quirk *quirk =
7118c2ecf20Sopenharmony_ci		(const struct snd_usb_audio_quirk *)usb_id->driver_info;
7128c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip;
7138c2ecf20Sopenharmony_ci	int i, err;
7148c2ecf20Sopenharmony_ci	struct usb_host_interface *alts;
7158c2ecf20Sopenharmony_ci	int ifnum;
7168c2ecf20Sopenharmony_ci	u32 id;
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	alts = &intf->altsetting[0];
7198c2ecf20Sopenharmony_ci	ifnum = get_iface_desc(alts)->bInterfaceNumber;
7208c2ecf20Sopenharmony_ci	id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
7218c2ecf20Sopenharmony_ci		    le16_to_cpu(dev->descriptor.idProduct));
7228c2ecf20Sopenharmony_ci	if (get_alias_id(dev, &id))
7238c2ecf20Sopenharmony_ci		quirk = get_alias_quirk(dev, id);
7248c2ecf20Sopenharmony_ci	if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
7258c2ecf20Sopenharmony_ci		return -ENXIO;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
7288c2ecf20Sopenharmony_ci	if (err < 0)
7298c2ecf20Sopenharmony_ci		return err;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/*
7328c2ecf20Sopenharmony_ci	 * found a config.  now register to ALSA
7338c2ecf20Sopenharmony_ci	 */
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	/* check whether it's already registered */
7368c2ecf20Sopenharmony_ci	chip = NULL;
7378c2ecf20Sopenharmony_ci	mutex_lock(&register_mutex);
7388c2ecf20Sopenharmony_ci	for (i = 0; i < SNDRV_CARDS; i++) {
7398c2ecf20Sopenharmony_ci		if (usb_chip[i] && usb_chip[i]->dev == dev) {
7408c2ecf20Sopenharmony_ci			if (atomic_read(&usb_chip[i]->shutdown)) {
7418c2ecf20Sopenharmony_ci				dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
7428c2ecf20Sopenharmony_ci				err = -EIO;
7438c2ecf20Sopenharmony_ci				goto __error;
7448c2ecf20Sopenharmony_ci			}
7458c2ecf20Sopenharmony_ci			chip = usb_chip[i];
7468c2ecf20Sopenharmony_ci			atomic_inc(&chip->active); /* avoid autopm */
7478c2ecf20Sopenharmony_ci			break;
7488c2ecf20Sopenharmony_ci		}
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci	if (! chip) {
7518c2ecf20Sopenharmony_ci		err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
7528c2ecf20Sopenharmony_ci		if (err < 0)
7538c2ecf20Sopenharmony_ci			goto __error;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci		/* it's a fresh one.
7568c2ecf20Sopenharmony_ci		 * now look for an empty slot and create a new card instance
7578c2ecf20Sopenharmony_ci		 */
7588c2ecf20Sopenharmony_ci		for (i = 0; i < SNDRV_CARDS; i++)
7598c2ecf20Sopenharmony_ci			if (!usb_chip[i] &&
7608c2ecf20Sopenharmony_ci			    (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
7618c2ecf20Sopenharmony_ci			    (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
7628c2ecf20Sopenharmony_ci				if (enable[i]) {
7638c2ecf20Sopenharmony_ci					err = snd_usb_audio_create(intf, dev, i, quirk,
7648c2ecf20Sopenharmony_ci								   id, &chip);
7658c2ecf20Sopenharmony_ci					if (err < 0)
7668c2ecf20Sopenharmony_ci						goto __error;
7678c2ecf20Sopenharmony_ci					break;
7688c2ecf20Sopenharmony_ci				} else if (vid[i] != -1 || pid[i] != -1) {
7698c2ecf20Sopenharmony_ci					dev_info(&dev->dev,
7708c2ecf20Sopenharmony_ci						 "device (%04x:%04x) is disabled\n",
7718c2ecf20Sopenharmony_ci						 USB_ID_VENDOR(id),
7728c2ecf20Sopenharmony_ci						 USB_ID_PRODUCT(id));
7738c2ecf20Sopenharmony_ci					err = -ENOENT;
7748c2ecf20Sopenharmony_ci					goto __error;
7758c2ecf20Sopenharmony_ci				}
7768c2ecf20Sopenharmony_ci			}
7778c2ecf20Sopenharmony_ci		if (!chip) {
7788c2ecf20Sopenharmony_ci			dev_err(&dev->dev, "no available usb audio device\n");
7798c2ecf20Sopenharmony_ci			err = -ENODEV;
7808c2ecf20Sopenharmony_ci			goto __error;
7818c2ecf20Sopenharmony_ci		}
7828c2ecf20Sopenharmony_ci	}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_ci	if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
7858c2ecf20Sopenharmony_ci		dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
7868c2ecf20Sopenharmony_ci		err = -EINVAL;
7878c2ecf20Sopenharmony_ci		goto __error;
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	dev_set_drvdata(&dev->dev, chip);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	/*
7938c2ecf20Sopenharmony_ci	 * For devices with more than one control interface, we assume the
7948c2ecf20Sopenharmony_ci	 * first contains the audio controls. We might need a more specific
7958c2ecf20Sopenharmony_ci	 * check here in the future.
7968c2ecf20Sopenharmony_ci	 */
7978c2ecf20Sopenharmony_ci	if (!chip->ctrl_intf)
7988c2ecf20Sopenharmony_ci		chip->ctrl_intf = alts;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	chip->txfr_quirk = 0;
8018c2ecf20Sopenharmony_ci	err = 1; /* continue */
8028c2ecf20Sopenharmony_ci	if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
8038c2ecf20Sopenharmony_ci		/* need some special handlings */
8048c2ecf20Sopenharmony_ci		err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
8058c2ecf20Sopenharmony_ci		if (err < 0)
8068c2ecf20Sopenharmony_ci			goto __error;
8078c2ecf20Sopenharmony_ci	}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	if (err > 0) {
8108c2ecf20Sopenharmony_ci		/* create normal USB audio interfaces */
8118c2ecf20Sopenharmony_ci		err = snd_usb_create_streams(chip, ifnum);
8128c2ecf20Sopenharmony_ci		if (err < 0)
8138c2ecf20Sopenharmony_ci			goto __error;
8148c2ecf20Sopenharmony_ci		err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error);
8158c2ecf20Sopenharmony_ci		if (err < 0)
8168c2ecf20Sopenharmony_ci			goto __error;
8178c2ecf20Sopenharmony_ci	}
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	if (chip->need_delayed_register) {
8208c2ecf20Sopenharmony_ci		dev_info(&dev->dev,
8218c2ecf20Sopenharmony_ci			 "Found post-registration device assignment: %08x:%02x\n",
8228c2ecf20Sopenharmony_ci			 chip->usb_id, ifnum);
8238c2ecf20Sopenharmony_ci		chip->need_delayed_register = false; /* clear again */
8248c2ecf20Sopenharmony_ci	}
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	/* we are allowed to call snd_card_register() many times, but first
8278c2ecf20Sopenharmony_ci	 * check to see if a device needs to skip it or do anything special
8288c2ecf20Sopenharmony_ci	 */
8298c2ecf20Sopenharmony_ci	if (!snd_usb_registration_quirk(chip, ifnum) &&
8308c2ecf20Sopenharmony_ci	    !check_delayed_register_option(chip, ifnum)) {
8318c2ecf20Sopenharmony_ci		err = snd_card_register(chip->card);
8328c2ecf20Sopenharmony_ci		if (err < 0)
8338c2ecf20Sopenharmony_ci			goto __error;
8348c2ecf20Sopenharmony_ci	}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	if (quirk && quirk->shares_media_device) {
8378c2ecf20Sopenharmony_ci		/* don't want to fail when snd_media_device_create() fails */
8388c2ecf20Sopenharmony_ci		snd_media_device_create(chip, intf);
8398c2ecf20Sopenharmony_ci	}
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	if (quirk)
8428c2ecf20Sopenharmony_ci		chip->quirk_type = quirk->type;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	usb_chip[chip->index] = chip;
8458c2ecf20Sopenharmony_ci	chip->intf[chip->num_interfaces] = intf;
8468c2ecf20Sopenharmony_ci	chip->num_interfaces++;
8478c2ecf20Sopenharmony_ci	usb_set_intfdata(intf, chip);
8488c2ecf20Sopenharmony_ci	atomic_dec(&chip->active);
8498c2ecf20Sopenharmony_ci	mutex_unlock(&register_mutex);
8508c2ecf20Sopenharmony_ci	return 0;
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci __error:
8538c2ecf20Sopenharmony_ci	if (chip) {
8548c2ecf20Sopenharmony_ci		/* chip->active is inside the chip->card object,
8558c2ecf20Sopenharmony_ci		 * decrement before memory is possibly returned.
8568c2ecf20Sopenharmony_ci		 */
8578c2ecf20Sopenharmony_ci		atomic_dec(&chip->active);
8588c2ecf20Sopenharmony_ci		if (!chip->num_interfaces)
8598c2ecf20Sopenharmony_ci			snd_card_free(chip->card);
8608c2ecf20Sopenharmony_ci	}
8618c2ecf20Sopenharmony_ci	mutex_unlock(&register_mutex);
8628c2ecf20Sopenharmony_ci	return err;
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci/*
8668c2ecf20Sopenharmony_ci * we need to take care of counter, since disconnection can be called also
8678c2ecf20Sopenharmony_ci * many times as well as usb_audio_probe().
8688c2ecf20Sopenharmony_ci */
8698c2ecf20Sopenharmony_cistatic void usb_audio_disconnect(struct usb_interface *intf)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = usb_get_intfdata(intf);
8728c2ecf20Sopenharmony_ci	struct snd_card *card;
8738c2ecf20Sopenharmony_ci	struct list_head *p;
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci	if (chip == USB_AUDIO_IFACE_UNUSED)
8768c2ecf20Sopenharmony_ci		return;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	card = chip->card;
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	mutex_lock(&register_mutex);
8818c2ecf20Sopenharmony_ci	if (atomic_inc_return(&chip->shutdown) == 1) {
8828c2ecf20Sopenharmony_ci		struct snd_usb_stream *as;
8838c2ecf20Sopenharmony_ci		struct snd_usb_endpoint *ep;
8848c2ecf20Sopenharmony_ci		struct usb_mixer_interface *mixer;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci		/* wait until all pending tasks done;
8878c2ecf20Sopenharmony_ci		 * they are protected by snd_usb_lock_shutdown()
8888c2ecf20Sopenharmony_ci		 */
8898c2ecf20Sopenharmony_ci		wait_event(chip->shutdown_wait,
8908c2ecf20Sopenharmony_ci			   !atomic_read(&chip->usage_count));
8918c2ecf20Sopenharmony_ci		snd_card_disconnect(card);
8928c2ecf20Sopenharmony_ci		/* release the pcm resources */
8938c2ecf20Sopenharmony_ci		list_for_each_entry(as, &chip->pcm_list, list) {
8948c2ecf20Sopenharmony_ci			snd_usb_stream_disconnect(as);
8958c2ecf20Sopenharmony_ci		}
8968c2ecf20Sopenharmony_ci		/* release the endpoint resources */
8978c2ecf20Sopenharmony_ci		list_for_each_entry(ep, &chip->ep_list, list) {
8988c2ecf20Sopenharmony_ci			snd_usb_endpoint_release(ep);
8998c2ecf20Sopenharmony_ci		}
9008c2ecf20Sopenharmony_ci		/* release the midi resources */
9018c2ecf20Sopenharmony_ci		list_for_each(p, &chip->midi_list) {
9028c2ecf20Sopenharmony_ci			snd_usbmidi_disconnect(p);
9038c2ecf20Sopenharmony_ci		}
9048c2ecf20Sopenharmony_ci		/*
9058c2ecf20Sopenharmony_ci		 * Nice to check quirk && quirk->shares_media_device and
9068c2ecf20Sopenharmony_ci		 * then call the snd_media_device_delete(). Don't have
9078c2ecf20Sopenharmony_ci		 * access to the quirk here. snd_media_device_delete()
9088c2ecf20Sopenharmony_ci		 * accesses mixer_list
9098c2ecf20Sopenharmony_ci		 */
9108c2ecf20Sopenharmony_ci		snd_media_device_delete(chip);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci		/* release mixer resources */
9138c2ecf20Sopenharmony_ci		list_for_each_entry(mixer, &chip->mixer_list, list) {
9148c2ecf20Sopenharmony_ci			snd_usb_mixer_disconnect(mixer);
9158c2ecf20Sopenharmony_ci		}
9168c2ecf20Sopenharmony_ci	}
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	if (chip->quirk_type == QUIRK_SETUP_DISABLE_AUTOSUSPEND)
9198c2ecf20Sopenharmony_ci		usb_enable_autosuspend(interface_to_usbdev(intf));
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	chip->num_interfaces--;
9228c2ecf20Sopenharmony_ci	if (chip->num_interfaces <= 0) {
9238c2ecf20Sopenharmony_ci		usb_chip[chip->index] = NULL;
9248c2ecf20Sopenharmony_ci		mutex_unlock(&register_mutex);
9258c2ecf20Sopenharmony_ci		snd_card_free_when_closed(card);
9268c2ecf20Sopenharmony_ci	} else {
9278c2ecf20Sopenharmony_ci		mutex_unlock(&register_mutex);
9288c2ecf20Sopenharmony_ci	}
9298c2ecf20Sopenharmony_ci}
9308c2ecf20Sopenharmony_ci
9318c2ecf20Sopenharmony_ci/* lock the shutdown (disconnect) task and autoresume */
9328c2ecf20Sopenharmony_ciint snd_usb_lock_shutdown(struct snd_usb_audio *chip)
9338c2ecf20Sopenharmony_ci{
9348c2ecf20Sopenharmony_ci	int err;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	atomic_inc(&chip->usage_count);
9378c2ecf20Sopenharmony_ci	if (atomic_read(&chip->shutdown)) {
9388c2ecf20Sopenharmony_ci		err = -EIO;
9398c2ecf20Sopenharmony_ci		goto error;
9408c2ecf20Sopenharmony_ci	}
9418c2ecf20Sopenharmony_ci	err = snd_usb_autoresume(chip);
9428c2ecf20Sopenharmony_ci	if (err < 0)
9438c2ecf20Sopenharmony_ci		goto error;
9448c2ecf20Sopenharmony_ci	return 0;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci error:
9478c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&chip->usage_count))
9488c2ecf20Sopenharmony_ci		wake_up(&chip->shutdown_wait);
9498c2ecf20Sopenharmony_ci	return err;
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/* autosuspend and unlock the shutdown */
9538c2ecf20Sopenharmony_civoid snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
9548c2ecf20Sopenharmony_ci{
9558c2ecf20Sopenharmony_ci	snd_usb_autosuspend(chip);
9568c2ecf20Sopenharmony_ci	if (atomic_dec_and_test(&chip->usage_count))
9578c2ecf20Sopenharmony_ci		wake_up(&chip->shutdown_wait);
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ciint snd_usb_autoresume(struct snd_usb_audio *chip)
9638c2ecf20Sopenharmony_ci{
9648c2ecf20Sopenharmony_ci	int i, err;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	if (atomic_read(&chip->shutdown))
9678c2ecf20Sopenharmony_ci		return -EIO;
9688c2ecf20Sopenharmony_ci	if (atomic_inc_return(&chip->active) != 1)
9698c2ecf20Sopenharmony_ci		return 0;
9708c2ecf20Sopenharmony_ci
9718c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_interfaces; i++) {
9728c2ecf20Sopenharmony_ci		err = usb_autopm_get_interface(chip->intf[i]);
9738c2ecf20Sopenharmony_ci		if (err < 0) {
9748c2ecf20Sopenharmony_ci			/* rollback */
9758c2ecf20Sopenharmony_ci			while (--i >= 0)
9768c2ecf20Sopenharmony_ci				usb_autopm_put_interface(chip->intf[i]);
9778c2ecf20Sopenharmony_ci			atomic_dec(&chip->active);
9788c2ecf20Sopenharmony_ci			return err;
9798c2ecf20Sopenharmony_ci		}
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci	return 0;
9828c2ecf20Sopenharmony_ci}
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_civoid snd_usb_autosuspend(struct snd_usb_audio *chip)
9858c2ecf20Sopenharmony_ci{
9868c2ecf20Sopenharmony_ci	int i;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	if (atomic_read(&chip->shutdown))
9898c2ecf20Sopenharmony_ci		return;
9908c2ecf20Sopenharmony_ci	if (!atomic_dec_and_test(&chip->active))
9918c2ecf20Sopenharmony_ci		return;
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	for (i = 0; i < chip->num_interfaces; i++)
9948c2ecf20Sopenharmony_ci		usb_autopm_put_interface(chip->intf[i]);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_cistatic int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
9988c2ecf20Sopenharmony_ci{
9998c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = usb_get_intfdata(intf);
10008c2ecf20Sopenharmony_ci	struct snd_usb_stream *as;
10018c2ecf20Sopenharmony_ci	struct usb_mixer_interface *mixer;
10028c2ecf20Sopenharmony_ci	struct list_head *p;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	if (chip == USB_AUDIO_IFACE_UNUSED)
10058c2ecf20Sopenharmony_ci		return 0;
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	if (!chip->num_suspended_intf++) {
10088c2ecf20Sopenharmony_ci		list_for_each_entry(as, &chip->pcm_list, list) {
10098c2ecf20Sopenharmony_ci			snd_usb_pcm_suspend(as);
10108c2ecf20Sopenharmony_ci			as->substream[0].need_setup_ep =
10118c2ecf20Sopenharmony_ci				as->substream[1].need_setup_ep = true;
10128c2ecf20Sopenharmony_ci		}
10138c2ecf20Sopenharmony_ci		list_for_each(p, &chip->midi_list)
10148c2ecf20Sopenharmony_ci			snd_usbmidi_suspend(p);
10158c2ecf20Sopenharmony_ci		list_for_each_entry(mixer, &chip->mixer_list, list)
10168c2ecf20Sopenharmony_ci			snd_usb_mixer_suspend(mixer);
10178c2ecf20Sopenharmony_ci	}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
10208c2ecf20Sopenharmony_ci		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
10218c2ecf20Sopenharmony_ci		chip->system_suspend = chip->num_suspended_intf;
10228c2ecf20Sopenharmony_ci	}
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci	return 0;
10258c2ecf20Sopenharmony_ci}
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_cistatic int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
10288c2ecf20Sopenharmony_ci{
10298c2ecf20Sopenharmony_ci	struct snd_usb_audio *chip = usb_get_intfdata(intf);
10308c2ecf20Sopenharmony_ci	struct snd_usb_stream *as;
10318c2ecf20Sopenharmony_ci	struct usb_mixer_interface *mixer;
10328c2ecf20Sopenharmony_ci	struct list_head *p;
10338c2ecf20Sopenharmony_ci	int err = 0;
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	if (chip == USB_AUDIO_IFACE_UNUSED)
10368c2ecf20Sopenharmony_ci		return 0;
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci	atomic_inc(&chip->active); /* avoid autopm */
10398c2ecf20Sopenharmony_ci	if (chip->num_suspended_intf > 1)
10408c2ecf20Sopenharmony_ci		goto out;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	list_for_each_entry(as, &chip->pcm_list, list) {
10438c2ecf20Sopenharmony_ci		err = snd_usb_pcm_resume(as);
10448c2ecf20Sopenharmony_ci		if (err < 0)
10458c2ecf20Sopenharmony_ci			goto err_out;
10468c2ecf20Sopenharmony_ci	}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci	/*
10498c2ecf20Sopenharmony_ci	 * ALSA leaves material resumption to user space
10508c2ecf20Sopenharmony_ci	 * we just notify and restart the mixers
10518c2ecf20Sopenharmony_ci	 */
10528c2ecf20Sopenharmony_ci	list_for_each_entry(mixer, &chip->mixer_list, list) {
10538c2ecf20Sopenharmony_ci		err = snd_usb_mixer_resume(mixer, reset_resume);
10548c2ecf20Sopenharmony_ci		if (err < 0)
10558c2ecf20Sopenharmony_ci			goto err_out;
10568c2ecf20Sopenharmony_ci	}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_ci	list_for_each(p, &chip->midi_list) {
10598c2ecf20Sopenharmony_ci		snd_usbmidi_resume(p);
10608c2ecf20Sopenharmony_ci	}
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci out:
10638c2ecf20Sopenharmony_ci	if (chip->num_suspended_intf == chip->system_suspend) {
10648c2ecf20Sopenharmony_ci		snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
10658c2ecf20Sopenharmony_ci		chip->system_suspend = 0;
10668c2ecf20Sopenharmony_ci	}
10678c2ecf20Sopenharmony_ci	chip->num_suspended_intf--;
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_cierr_out:
10708c2ecf20Sopenharmony_ci	atomic_dec(&chip->active); /* allow autopm after this point */
10718c2ecf20Sopenharmony_ci	return err;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_cistatic int usb_audio_resume(struct usb_interface *intf)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	return __usb_audio_resume(intf, false);
10778c2ecf20Sopenharmony_ci}
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_cistatic int usb_audio_reset_resume(struct usb_interface *intf)
10808c2ecf20Sopenharmony_ci{
10818c2ecf20Sopenharmony_ci	return __usb_audio_resume(intf, true);
10828c2ecf20Sopenharmony_ci}
10838c2ecf20Sopenharmony_ci#else
10848c2ecf20Sopenharmony_ci#define usb_audio_suspend	NULL
10858c2ecf20Sopenharmony_ci#define usb_audio_resume	NULL
10868c2ecf20Sopenharmony_ci#define usb_audio_reset_resume	NULL
10878c2ecf20Sopenharmony_ci#endif		/* CONFIG_PM */
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_cistatic const struct usb_device_id usb_audio_ids [] = {
10908c2ecf20Sopenharmony_ci#include "quirks-table.h"
10918c2ecf20Sopenharmony_ci    { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
10928c2ecf20Sopenharmony_ci      .bInterfaceClass = USB_CLASS_AUDIO,
10938c2ecf20Sopenharmony_ci      .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
10948c2ecf20Sopenharmony_ci    { }						/* Terminating entry */
10958c2ecf20Sopenharmony_ci};
10968c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, usb_audio_ids);
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci/*
10998c2ecf20Sopenharmony_ci * entry point for linux usb interface
11008c2ecf20Sopenharmony_ci */
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_cistatic struct usb_driver usb_audio_driver = {
11038c2ecf20Sopenharmony_ci	.name =		"snd-usb-audio",
11048c2ecf20Sopenharmony_ci	.probe =	usb_audio_probe,
11058c2ecf20Sopenharmony_ci	.disconnect =	usb_audio_disconnect,
11068c2ecf20Sopenharmony_ci	.suspend =	usb_audio_suspend,
11078c2ecf20Sopenharmony_ci	.resume =	usb_audio_resume,
11088c2ecf20Sopenharmony_ci	.reset_resume =	usb_audio_reset_resume,
11098c2ecf20Sopenharmony_ci	.id_table =	usb_audio_ids,
11108c2ecf20Sopenharmony_ci	.supports_autosuspend = 1,
11118c2ecf20Sopenharmony_ci};
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_cimodule_usb_driver(usb_audio_driver);
1114