Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (Tentative) USB Audio Driver for ALSA |
| 3 | * |
| 4 | * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * Many codes borrowed from audio.c by |
| 7 | * Alan Cox (alan@lxorguk.ukuu.org.uk) |
| 8 | * Thomas Sailer (sailer@ife.ee.ethz.ch) |
| 9 | * |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 10 | * Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * |
| 27 | * NOTES: |
| 28 | * |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 29 | * - the linked URBs would be preferred but not used so far because of |
| 30 | * the instability of unlinking. |
| 31 | * - type II is not supported properly. there is no device which supports |
| 32 | * this type *correctly*. SB extigy looks as if it supports, but it's |
| 33 | * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). |
| 34 | */ |
| 35 | |
| 36 | |
| 37 | #include <linux/bitops.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/list.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/string.h> |
Takashi Iwai | 3ffc122 | 2011-03-21 12:00:00 +0100 | [diff] [blame] | 42 | #include <linux/ctype.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 43 | #include <linux/usb.h> |
| 44 | #include <linux/moduleparam.h> |
| 45 | #include <linux/mutex.h> |
| 46 | #include <linux/usb/audio.h> |
Daniel Mack | 7e84789 | 2010-03-11 21:13:20 +0100 | [diff] [blame] | 47 | #include <linux/usb/audio-v2.h> |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 48 | #include <linux/usb/audio-v3.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 49 | #include <linux/module.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 50 | |
Daniel Mack | 9e38658 | 2011-05-25 09:09:01 +0200 | [diff] [blame] | 51 | #include <sound/control.h> |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 52 | #include <sound/core.h> |
| 53 | #include <sound/info.h> |
| 54 | #include <sound/pcm.h> |
| 55 | #include <sound/pcm_params.h> |
| 56 | #include <sound/initval.h> |
| 57 | |
| 58 | #include "usbaudio.h" |
| 59 | #include "card.h" |
| 60 | #include "midi.h" |
Daniel Mack | f0b5e63 | 2010-03-11 21:13:23 +0100 | [diff] [blame] | 61 | #include "mixer.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 62 | #include "proc.h" |
| 63 | #include "quirks.h" |
| 64 | #include "endpoint.h" |
| 65 | #include "helper.h" |
| 66 | #include "debug.h" |
| 67 | #include "pcm.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 68 | #include "format.h" |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 69 | #include "power.h" |
Daniel Mack | e8e8bab | 2011-09-12 18:54:12 +0200 | [diff] [blame] | 70 | #include "stream.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 71 | |
| 72 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); |
| 73 | MODULE_DESCRIPTION("USB Audio"); |
| 74 | MODULE_LICENSE("GPL"); |
| 75 | MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); |
| 76 | |
| 77 | |
| 78 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ |
| 79 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 80 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 81 | /* Vendor/product IDs for this card */ |
| 82 | static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; |
| 83 | static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 84 | static int device_setup[SNDRV_CARDS]; /* device parameter for this card */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 85 | static bool ignore_ctl_error; |
Eldad Zack | ef02e29 | 2013-04-03 23:18:56 +0200 | [diff] [blame] | 86 | static bool autoclock = true; |
Takashi Iwai | e270336 | 2016-01-11 14:39:12 +0100 | [diff] [blame] | 87 | static char *quirk_alias[SNDRV_CARDS]; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 88 | |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 89 | bool snd_usb_use_vmalloc = true; |
| 90 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 91 | module_param_array(index, int, NULL, 0444); |
| 92 | MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); |
| 93 | module_param_array(id, charp, NULL, 0444); |
| 94 | MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); |
| 95 | module_param_array(enable, bool, NULL, 0444); |
| 96 | MODULE_PARM_DESC(enable, "Enable USB audio adapter."); |
| 97 | module_param_array(vid, int, NULL, 0444); |
| 98 | MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); |
| 99 | module_param_array(pid, int, NULL, 0444); |
| 100 | MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 101 | module_param_array(device_setup, int, NULL, 0444); |
| 102 | MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); |
| 103 | module_param(ignore_ctl_error, bool, 0444); |
| 104 | MODULE_PARM_DESC(ignore_ctl_error, |
| 105 | "Ignore errors from USB controller for mixer interfaces."); |
Eldad Zack | ef02e29 | 2013-04-03 23:18:56 +0200 | [diff] [blame] | 106 | module_param(autoclock, bool, 0444); |
| 107 | MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes)."); |
Takashi Iwai | e270336 | 2016-01-11 14:39:12 +0100 | [diff] [blame] | 108 | module_param_array(quirk_alias, charp, NULL, 0444); |
| 109 | MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef."); |
Takashi Iwai | f274baa | 2018-05-27 13:01:17 +0200 | [diff] [blame] | 110 | module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444); |
| 111 | MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes)."); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * we keep the snd_usb_audio_t instances by ourselves for merging |
| 115 | * the all interfaces on the same card as one sound device. |
| 116 | */ |
| 117 | |
| 118 | static DEFINE_MUTEX(register_mutex); |
| 119 | static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; |
| 120 | static struct usb_driver usb_audio_driver; |
| 121 | |
| 122 | /* |
| 123 | * disconnect streams |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 124 | * called from usb_audio_disconnect() |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 125 | */ |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 126 | static void snd_usb_stream_disconnect(struct snd_usb_stream *as) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 127 | { |
| 128 | int idx; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 129 | struct snd_usb_substream *subs; |
| 130 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 131 | for (idx = 0; idx < 2; idx++) { |
| 132 | subs = &as->substream[idx]; |
| 133 | if (!subs->num_formats) |
Takashi Iwai | 76195fb | 2010-09-08 08:27:02 +0200 | [diff] [blame] | 134 | continue; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 135 | subs->interface = -1; |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 136 | subs->data_endpoint = NULL; |
| 137 | subs->sync_endpoint = NULL; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) |
| 142 | { |
| 143 | struct usb_device *dev = chip->dev; |
| 144 | struct usb_host_interface *alts; |
| 145 | struct usb_interface_descriptor *altsd; |
| 146 | struct usb_interface *iface = usb_ifnum_to_if(dev, interface); |
| 147 | |
| 148 | if (!iface) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 149 | dev_err(&dev->dev, "%u:%d : does not exist\n", |
| 150 | ctrlif, interface); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 151 | return -EINVAL; |
| 152 | } |
| 153 | |
Clemens Ladisch | 342cda29 | 2013-06-15 11:21:09 +0200 | [diff] [blame] | 154 | alts = &iface->altsetting[0]; |
| 155 | altsd = get_iface_desc(alts); |
| 156 | |
| 157 | /* |
| 158 | * Android with both accessory and audio interfaces enabled gets the |
| 159 | * interface numbers wrong. |
| 160 | */ |
| 161 | if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || |
| 162 | chip->usb_id == USB_ID(0x18d1, 0x2d05)) && |
| 163 | interface == 0 && |
| 164 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && |
| 165 | altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { |
| 166 | interface = 2; |
| 167 | iface = usb_ifnum_to_if(dev, interface); |
| 168 | if (!iface) |
| 169 | return -EINVAL; |
| 170 | alts = &iface->altsetting[0]; |
| 171 | altsd = get_iface_desc(alts); |
| 172 | } |
| 173 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 174 | if (usb_interface_claimed(iface)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 175 | dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n", |
| 176 | ctrlif, interface); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 180 | if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || |
| 181 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && |
| 182 | altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { |
Takashi Iwai | 79289e2 | 2016-01-11 11:33:34 +0100 | [diff] [blame] | 183 | int err = __snd_usbmidi_create(chip->card, iface, |
| 184 | &chip->midi_list, NULL, |
| 185 | chip->usb_id); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 186 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 187 | dev_err(&dev->dev, |
| 188 | "%u:%d: cannot create sequencer device\n", |
| 189 | ctrlif, interface); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 190 | return -EINVAL; |
| 191 | } |
| 192 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && |
| 198 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || |
| 199 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 200 | dev_dbg(&dev->dev, |
| 201 | "%u:%d: skipping non-supported interface %d\n", |
| 202 | ctrlif, interface, altsd->bInterfaceClass); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 203 | /* skip non-supported classes */ |
| 204 | return -EINVAL; |
| 205 | } |
| 206 | |
| 207 | if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 208 | dev_err(&dev->dev, "low speed audio streaming not supported\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 209 | return -EINVAL; |
| 210 | } |
| 211 | |
Daniel Mack | e8e8bab | 2011-09-12 18:54:12 +0200 | [diff] [blame] | 212 | if (! snd_usb_parse_audio_interface(chip, interface)) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 213 | usb_set_interface(dev, interface, 0); /* reset the current interface */ |
| 214 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * parse audio control descriptor and create pcm/midi streams |
| 222 | */ |
| 223 | static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) |
| 224 | { |
| 225 | struct usb_device *dev = chip->dev; |
| 226 | struct usb_host_interface *host_iface; |
| 227 | struct usb_interface_descriptor *altsd; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 228 | int i, protocol; |
| 229 | |
| 230 | /* find audiocontrol interface */ |
| 231 | host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 232 | altsd = get_iface_desc(host_iface); |
| 233 | protocol = altsd->bInterfaceProtocol; |
| 234 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 235 | switch (protocol) { |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 236 | default: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 237 | dev_warn(&dev->dev, |
| 238 | "unknown interface protocol %#02x, assuming v1\n", |
| 239 | protocol); |
Clemens Ladisch | a2acad8 | 2010-09-03 10:53:11 +0200 | [diff] [blame] | 240 | /* fall through */ |
| 241 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 242 | case UAC_VERSION_1: { |
Jorge Sanjuan | 3763f61 | 2018-05-04 04:24:01 +0300 | [diff] [blame] | 243 | struct uac1_ac_header_descriptor *h1; |
| 244 | int rest_bytes; |
| 245 | |
| 246 | h1 = snd_usb_find_csint_desc(host_iface->extra, |
| 247 | host_iface->extralen, |
| 248 | NULL, UAC_HEADER); |
Takashi Iwai | 378f9df | 2019-01-02 17:12:21 +0100 | [diff] [blame] | 249 | if (!h1 || h1->bLength < sizeof(*h1)) { |
Jorge Sanjuan | 3763f61 | 2018-05-04 04:24:01 +0300 | [diff] [blame] | 250 | dev_err(&dev->dev, "cannot find UAC_HEADER\n"); |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
| 254 | rest_bytes = (void *)(host_iface->extra + |
| 255 | host_iface->extralen) - (void *)h1; |
| 256 | |
| 257 | /* just to be sure -- this shouldn't hit at all */ |
| 258 | if (rest_bytes <= 0) { |
| 259 | dev_err(&dev->dev, "invalid control header\n"); |
| 260 | return -EINVAL; |
| 261 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 262 | |
Takashi Iwai | bfc81a8 | 2017-09-22 16:18:53 +0200 | [diff] [blame] | 263 | if (rest_bytes < sizeof(*h1)) { |
| 264 | dev_err(&dev->dev, "too short v1 buffer descriptor\n"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 268 | if (!h1->bInCollection) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 269 | dev_info(&dev->dev, "skipping empty audio interface (v1)\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 270 | return -EINVAL; |
| 271 | } |
| 272 | |
Takashi Iwai | bfc81a8 | 2017-09-22 16:18:53 +0200 | [diff] [blame] | 273 | if (rest_bytes < h1->bLength) { |
| 274 | dev_err(&dev->dev, "invalid buffer length (v1)\n"); |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 278 | if (h1->bLength < sizeof(*h1) + h1->bInCollection) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 279 | dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | } |
| 282 | |
| 283 | for (i = 0; i < h1->bInCollection; i++) |
| 284 | snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); |
| 285 | |
| 286 | break; |
| 287 | } |
| 288 | |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 289 | case UAC_VERSION_2: |
| 290 | case UAC_VERSION_3: { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 291 | struct usb_interface_assoc_descriptor *assoc = |
| 292 | usb_ifnum_to_if(dev, ctrlif)->intf_assoc; |
| 293 | |
| 294 | if (!assoc) { |
Clemens Ladisch | 281a6ac | 2013-03-11 20:15:34 +0100 | [diff] [blame] | 295 | /* |
| 296 | * Firmware writers cannot count to three. So to find |
| 297 | * the IAD on the NuForce UDH-100, also check the next |
| 298 | * interface. |
| 299 | */ |
| 300 | struct usb_interface *iface = |
| 301 | usb_ifnum_to_if(dev, ctrlif + 1); |
| 302 | if (iface && |
| 303 | iface->intf_assoc && |
| 304 | iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && |
| 305 | iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) |
| 306 | assoc = iface->intf_assoc; |
| 307 | } |
| 308 | |
| 309 | if (!assoc) { |
Ruslan Bilovol | 9a2fe9b | 2018-03-21 02:03:59 +0200 | [diff] [blame] | 310 | dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n"); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 311 | return -EINVAL; |
| 312 | } |
| 313 | |
Ruslan Bilovol | 17156f2 | 2018-05-04 04:24:04 +0300 | [diff] [blame] | 314 | if (protocol == UAC_VERSION_3) { |
| 315 | int badd = assoc->bFunctionSubClass; |
| 316 | |
| 317 | if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 && |
| 318 | (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO || |
| 319 | badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) { |
| 320 | dev_err(&dev->dev, |
| 321 | "Unsupported UAC3 BADD profile\n"); |
| 322 | return -EINVAL; |
| 323 | } |
| 324 | |
| 325 | chip->badd_profile = badd; |
| 326 | } |
| 327 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 328 | for (i = 0; i < assoc->bInterfaceCount; i++) { |
| 329 | int intf = assoc->bFirstInterface + i; |
| 330 | |
| 331 | if (intf != ctrlif) |
| 332 | snd_usb_create_stream(chip, ctrlif, intf); |
| 333 | } |
| 334 | |
| 335 | break; |
| 336 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * free the chip instance |
| 344 | * |
| 345 | * here we have to do not much, since pcm and controls are already freed |
| 346 | * |
| 347 | */ |
| 348 | |
Takashi Iwai | 011ae2b | 2018-05-27 15:07:01 +0200 | [diff] [blame] | 349 | static void snd_usb_audio_free(struct snd_card *card) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 350 | { |
Takashi Iwai | 011ae2b | 2018-05-27 15:07:01 +0200 | [diff] [blame] | 351 | struct snd_usb_audio *chip = card->private_data; |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 352 | struct snd_usb_endpoint *ep, *n; |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 353 | |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 354 | list_for_each_entry_safe(ep, n, &chip->ep_list, list) |
| 355 | snd_usb_endpoint_free(ep); |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 356 | |
Daniel Mack | 596580d | 2012-04-12 13:51:10 +0200 | [diff] [blame] | 357 | mutex_destroy(&chip->mutex); |
Takashi Iwai | 6ff1a25 | 2016-11-14 21:46:47 +0100 | [diff] [blame] | 358 | if (!atomic_read(&chip->shutdown)) |
| 359 | dev_set_drvdata(&chip->dev->dev, NULL); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Takashi Iwai | 07eca5f | 2018-05-02 14:45:37 +0200 | [diff] [blame] | 362 | static void usb_audio_make_shortname(struct usb_device *dev, |
| 363 | struct snd_usb_audio *chip, |
| 364 | const struct snd_usb_audio_quirk *quirk) |
| 365 | { |
| 366 | struct snd_card *card = chip->card; |
| 367 | |
| 368 | if (quirk && quirk->product_name && *quirk->product_name) { |
| 369 | strlcpy(card->shortname, quirk->product_name, |
| 370 | sizeof(card->shortname)); |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | /* retrieve the device string as shortname */ |
| 375 | if (!dev->descriptor.iProduct || |
| 376 | usb_string(dev, dev->descriptor.iProduct, |
| 377 | card->shortname, sizeof(card->shortname)) <= 0) { |
| 378 | /* no name available from anywhere, so use ID */ |
| 379 | sprintf(card->shortname, "USB Device %#04x:%#04x", |
| 380 | USB_ID_VENDOR(chip->usb_id), |
| 381 | USB_ID_PRODUCT(chip->usb_id)); |
| 382 | } |
| 383 | |
| 384 | strim(card->shortname); |
| 385 | } |
| 386 | |
| 387 | static void usb_audio_make_longname(struct usb_device *dev, |
| 388 | struct snd_usb_audio *chip, |
| 389 | const struct snd_usb_audio_quirk *quirk) |
| 390 | { |
| 391 | struct snd_card *card = chip->card; |
| 392 | int len; |
| 393 | |
| 394 | /* shortcut - if any pre-defined string is given, use it */ |
| 395 | if (quirk && quirk->profile_name && *quirk->profile_name) { |
| 396 | strlcpy(card->longname, quirk->profile_name, |
| 397 | sizeof(card->longname)); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | if (quirk && quirk->vendor_name && *quirk->vendor_name) { |
| 402 | len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); |
| 403 | } else { |
| 404 | /* retrieve the vendor and device strings as longname */ |
| 405 | if (dev->descriptor.iManufacturer) |
| 406 | len = usb_string(dev, dev->descriptor.iManufacturer, |
| 407 | card->longname, sizeof(card->longname)); |
| 408 | else |
| 409 | len = 0; |
| 410 | /* we don't really care if there isn't any vendor string */ |
| 411 | } |
| 412 | if (len > 0) { |
| 413 | strim(card->longname); |
| 414 | if (*card->longname) |
| 415 | strlcat(card->longname, " ", sizeof(card->longname)); |
| 416 | } |
| 417 | |
| 418 | strlcat(card->longname, card->shortname, sizeof(card->longname)); |
| 419 | |
| 420 | len = strlcat(card->longname, " at ", sizeof(card->longname)); |
| 421 | |
| 422 | if (len < sizeof(card->longname)) |
| 423 | usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); |
| 424 | |
| 425 | switch (snd_usb_get_speed(dev)) { |
| 426 | case USB_SPEED_LOW: |
| 427 | strlcat(card->longname, ", low speed", sizeof(card->longname)); |
| 428 | break; |
| 429 | case USB_SPEED_FULL: |
| 430 | strlcat(card->longname, ", full speed", sizeof(card->longname)); |
| 431 | break; |
| 432 | case USB_SPEED_HIGH: |
| 433 | strlcat(card->longname, ", high speed", sizeof(card->longname)); |
| 434 | break; |
| 435 | case USB_SPEED_SUPER: |
| 436 | strlcat(card->longname, ", super speed", sizeof(card->longname)); |
| 437 | break; |
| 438 | case USB_SPEED_SUPER_PLUS: |
| 439 | strlcat(card->longname, ", super speed plus", sizeof(card->longname)); |
| 440 | break; |
| 441 | default: |
| 442 | break; |
| 443 | } |
| 444 | } |
| 445 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 446 | /* |
| 447 | * create a chip instance and set its names. |
| 448 | */ |
Takashi Iwai | 874b8d4 | 2014-01-29 14:22:20 +0100 | [diff] [blame] | 449 | static int snd_usb_audio_create(struct usb_interface *intf, |
| 450 | struct usb_device *dev, int idx, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 451 | const struct snd_usb_audio_quirk *quirk, |
Takashi Iwai | 03a1f48 | 2017-03-31 11:19:19 +0200 | [diff] [blame] | 452 | unsigned int usb_id, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 453 | struct snd_usb_audio **rchip) |
| 454 | { |
| 455 | struct snd_card *card; |
| 456 | struct snd_usb_audio *chip; |
Takashi Iwai | 07eca5f | 2018-05-02 14:45:37 +0200 | [diff] [blame] | 457 | int err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 458 | char component[14]; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 459 | |
| 460 | *rchip = NULL; |
| 461 | |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 462 | switch (snd_usb_get_speed(dev)) { |
| 463 | case USB_SPEED_LOW: |
| 464 | case USB_SPEED_FULL: |
| 465 | case USB_SPEED_HIGH: |
Thomas Pugliese | df3774c | 2013-10-01 14:32:15 -0500 | [diff] [blame] | 466 | case USB_SPEED_WIRELESS: |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 467 | case USB_SPEED_SUPER: |
Oliver Neukum | 748a1cc | 2016-05-04 14:18:39 +0200 | [diff] [blame] | 468 | case USB_SPEED_SUPER_PLUS: |
Paul Zimmerman | 4f4e8f6 | 2010-08-13 12:42:07 -0700 | [diff] [blame] | 469 | break; |
| 470 | default: |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 471 | dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev)); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 472 | return -ENXIO; |
| 473 | } |
| 474 | |
Takashi Iwai | 874b8d4 | 2014-01-29 14:22:20 +0100 | [diff] [blame] | 475 | err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, |
Takashi Iwai | 011ae2b | 2018-05-27 15:07:01 +0200 | [diff] [blame] | 476 | sizeof(*chip), &card); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 477 | if (err < 0) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 478 | dev_err(&dev->dev, "cannot create card instance %d\n", idx); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 479 | return err; |
| 480 | } |
| 481 | |
Takashi Iwai | 011ae2b | 2018-05-27 15:07:01 +0200 | [diff] [blame] | 482 | chip = card->private_data; |
Daniel Mack | 596580d | 2012-04-12 13:51:10 +0200 | [diff] [blame] | 483 | mutex_init(&chip->mutex); |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 484 | init_waitqueue_head(&chip->shutdown_wait); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 485 | chip->index = idx; |
| 486 | chip->dev = dev; |
| 487 | chip->card = card; |
| 488 | chip->setup = device_setup[idx]; |
Eldad Zack | ef02e29 | 2013-04-03 23:18:56 +0200 | [diff] [blame] | 489 | chip->autoclock = autoclock; |
Takashi Iwai | a6da499 | 2015-08-26 10:20:59 +0200 | [diff] [blame] | 490 | atomic_set(&chip->active, 1); /* avoid autopm during probing */ |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 491 | atomic_set(&chip->usage_count, 0); |
| 492 | atomic_set(&chip->shutdown, 0); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 493 | |
Takashi Iwai | 03a1f48 | 2017-03-31 11:19:19 +0200 | [diff] [blame] | 494 | chip->usb_id = usb_id; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 495 | INIT_LIST_HEAD(&chip->pcm_list); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 496 | INIT_LIST_HEAD(&chip->ep_list); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 497 | INIT_LIST_HEAD(&chip->midi_list); |
| 498 | INIT_LIST_HEAD(&chip->mixer_list); |
| 499 | |
Takashi Iwai | 011ae2b | 2018-05-27 15:07:01 +0200 | [diff] [blame] | 500 | card->private_free = snd_usb_audio_free; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 501 | |
| 502 | strcpy(card->driver, "USB-Audio"); |
| 503 | sprintf(component, "USB%04x:%04x", |
| 504 | USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); |
| 505 | snd_component_add(card, component); |
| 506 | |
Takashi Iwai | 07eca5f | 2018-05-02 14:45:37 +0200 | [diff] [blame] | 507 | usb_audio_make_shortname(dev, chip, quirk); |
| 508 | usb_audio_make_longname(dev, chip, quirk); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 509 | |
| 510 | snd_usb_audio_create_proc(chip); |
| 511 | |
| 512 | *rchip = chip; |
| 513 | return 0; |
| 514 | } |
| 515 | |
Takashi Iwai | e270336 | 2016-01-11 14:39:12 +0100 | [diff] [blame] | 516 | /* look for a matching quirk alias id */ |
| 517 | static bool get_alias_id(struct usb_device *dev, unsigned int *id) |
| 518 | { |
| 519 | int i; |
| 520 | unsigned int src, dst; |
| 521 | |
| 522 | for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) { |
| 523 | if (!quirk_alias[i] || |
| 524 | sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 || |
| 525 | src != *id) |
| 526 | continue; |
| 527 | dev_info(&dev->dev, |
| 528 | "device (%04x:%04x): applying quirk alias %04x:%04x\n", |
| 529 | USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id), |
| 530 | USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst)); |
| 531 | *id = dst; |
| 532 | return true; |
| 533 | } |
| 534 | |
| 535 | return false; |
| 536 | } |
| 537 | |
Arvind Yadav | 8824ae2 | 2017-08-06 12:18:56 +0530 | [diff] [blame] | 538 | static const struct usb_device_id usb_audio_ids[]; /* defined below */ |
Takashi Iwai | e270336 | 2016-01-11 14:39:12 +0100 | [diff] [blame] | 539 | |
| 540 | /* look for the corresponding quirk */ |
| 541 | static const struct snd_usb_audio_quirk * |
| 542 | get_alias_quirk(struct usb_device *dev, unsigned int id) |
| 543 | { |
| 544 | const struct usb_device_id *p; |
| 545 | |
| 546 | for (p = usb_audio_ids; p->match_flags; p++) { |
| 547 | /* FIXME: this checks only vendor:product pair in the list */ |
| 548 | if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) == |
| 549 | USB_DEVICE_ID_MATCH_DEVICE && |
| 550 | p->idVendor == USB_ID_VENDOR(id) && |
| 551 | p->idProduct == USB_ID_PRODUCT(id)) |
| 552 | return (const struct snd_usb_audio_quirk *)p->driver_info; |
| 553 | } |
| 554 | |
| 555 | return NULL; |
| 556 | } |
| 557 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 558 | /* |
| 559 | * probe the active usb device |
| 560 | * |
| 561 | * note that this can be called multiple times per a device, when it |
| 562 | * includes multiple audio control interfaces. |
| 563 | * |
| 564 | * thus we check the usb device pointer and creates the card instance |
| 565 | * only at the first time. the successive calls of this function will |
| 566 | * append the pcm interface to the corresponding card. |
| 567 | */ |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 568 | static int usb_audio_probe(struct usb_interface *intf, |
| 569 | const struct usb_device_id *usb_id) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 570 | { |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 571 | struct usb_device *dev = interface_to_usbdev(intf); |
| 572 | const struct snd_usb_audio_quirk *quirk = |
| 573 | (const struct snd_usb_audio_quirk *)usb_id->driver_info; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 574 | struct snd_usb_audio *chip; |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 575 | int i, err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 576 | struct usb_host_interface *alts; |
| 577 | int ifnum; |
| 578 | u32 id; |
| 579 | |
| 580 | alts = &intf->altsetting[0]; |
| 581 | ifnum = get_iface_desc(alts)->bInterfaceNumber; |
| 582 | id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), |
| 583 | le16_to_cpu(dev->descriptor.idProduct)); |
Takashi Iwai | e270336 | 2016-01-11 14:39:12 +0100 | [diff] [blame] | 584 | if (get_alias_id(dev, &id)) |
| 585 | quirk = get_alias_quirk(dev, id); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 586 | if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 587 | return -ENXIO; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 588 | |
Takashi Iwai | 79289e2 | 2016-01-11 11:33:34 +0100 | [diff] [blame] | 589 | err = snd_usb_apply_boot_quirk(dev, intf, quirk, id); |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 590 | if (err < 0) |
| 591 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * found a config. now register to ALSA |
| 595 | */ |
| 596 | |
| 597 | /* check whether it's already registered */ |
| 598 | chip = NULL; |
| 599 | mutex_lock(®ister_mutex); |
| 600 | for (i = 0; i < SNDRV_CARDS; i++) { |
| 601 | if (usb_chip[i] && usb_chip[i]->dev == dev) { |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 602 | if (atomic_read(&usb_chip[i]->shutdown)) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 603 | dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n"); |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 604 | err = -EIO; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 605 | goto __error; |
| 606 | } |
| 607 | chip = usb_chip[i]; |
Takashi Iwai | a6da499 | 2015-08-26 10:20:59 +0200 | [diff] [blame] | 608 | atomic_inc(&chip->active); /* avoid autopm */ |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 609 | break; |
| 610 | } |
| 611 | } |
| 612 | if (! chip) { |
| 613 | /* it's a fresh one. |
| 614 | * now look for an empty slot and create a new card instance |
| 615 | */ |
| 616 | for (i = 0; i < SNDRV_CARDS; i++) |
Tamaki Nishino | 108884e | 2017-11-30 20:27:52 +0900 | [diff] [blame] | 617 | if (!usb_chip[i] && |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 618 | (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && |
| 619 | (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { |
Tamaki Nishino | 108884e | 2017-11-30 20:27:52 +0900 | [diff] [blame] | 620 | if (enable[i]) { |
| 621 | err = snd_usb_audio_create(intf, dev, i, quirk, |
| 622 | id, &chip); |
| 623 | if (err < 0) |
| 624 | goto __error; |
| 625 | chip->pm_intf = intf; |
| 626 | break; |
| 627 | } else if (vid[i] != -1 || pid[i] != -1) { |
| 628 | dev_info(&dev->dev, |
| 629 | "device (%04x:%04x) is disabled\n", |
| 630 | USB_ID_VENDOR(id), |
| 631 | USB_ID_PRODUCT(id)); |
| 632 | err = -ENOENT; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 633 | goto __error; |
Tamaki Nishino | 108884e | 2017-11-30 20:27:52 +0900 | [diff] [blame] | 634 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 635 | } |
| 636 | if (!chip) { |
Takashi Iwai | 0ba41d9 | 2014-02-26 13:02:17 +0100 | [diff] [blame] | 637 | dev_err(&dev->dev, "no available usb audio device\n"); |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 638 | err = -ENODEV; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 639 | goto __error; |
| 640 | } |
| 641 | } |
Kazuki Oikawa | 76df529 | 2016-07-18 01:16:15 +0900 | [diff] [blame] | 642 | dev_set_drvdata(&dev->dev, chip); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 643 | |
Daniel Mack | 7b6717e | 2010-09-02 17:13:15 +0800 | [diff] [blame] | 644 | /* |
| 645 | * For devices with more than one control interface, we assume the |
| 646 | * first contains the audio controls. We might need a more specific |
| 647 | * check here in the future. |
| 648 | */ |
| 649 | if (!chip->ctrl_intf) |
| 650 | chip->ctrl_intf = alts; |
Daniel Mack | 79f920f | 2010-05-31 14:51:31 +0200 | [diff] [blame] | 651 | |
Daniel Mack | 5875c2c | 2011-05-25 09:08:59 +0200 | [diff] [blame] | 652 | chip->txfr_quirk = 0; |
| 653 | err = 1; /* continue */ |
| 654 | if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { |
| 655 | /* need some special handlings */ |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 656 | err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk); |
| 657 | if (err < 0) |
Daniel Mack | 5875c2c | 2011-05-25 09:08:59 +0200 | [diff] [blame] | 658 | goto __error; |
| 659 | } |
| 660 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 661 | if (err > 0) { |
| 662 | /* create normal USB audio interfaces */ |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 663 | err = snd_usb_create_streams(chip, ifnum); |
| 664 | if (err < 0) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 665 | goto __error; |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 666 | err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error); |
| 667 | if (err < 0) |
| 668 | goto __error; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | /* we are allowed to call snd_card_register() many times */ |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 672 | err = snd_card_register(chip->card); |
| 673 | if (err < 0) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 674 | goto __error; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 675 | |
| 676 | usb_chip[chip->index] = chip; |
| 677 | chip->num_interfaces++; |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 678 | usb_set_intfdata(intf, chip); |
Takashi Iwai | a6da499 | 2015-08-26 10:20:59 +0200 | [diff] [blame] | 679 | atomic_dec(&chip->active); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 680 | mutex_unlock(®ister_mutex); |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 681 | return 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 682 | |
| 683 | __error: |
Thomas Pfaff | 61a6a10 | 2011-09-26 15:43:59 +0200 | [diff] [blame] | 684 | if (chip) { |
Hui Peng | a7e719a | 2018-12-03 16:09:34 +0100 | [diff] [blame] | 685 | /* chip->active is inside the chip->card object, |
| 686 | * decrement before memory is possibly returned. |
| 687 | */ |
| 688 | atomic_dec(&chip->active); |
Thomas Pfaff | 61a6a10 | 2011-09-26 15:43:59 +0200 | [diff] [blame] | 689 | if (!chip->num_interfaces) |
| 690 | snd_card_free(chip->card); |
Thomas Pfaff | 61a6a10 | 2011-09-26 15:43:59 +0200 | [diff] [blame] | 691 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 692 | mutex_unlock(®ister_mutex); |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 693 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | /* |
| 697 | * we need to take care of counter, since disconnection can be called also |
| 698 | * many times as well as usb_audio_probe(). |
| 699 | */ |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 700 | static void usb_audio_disconnect(struct usb_interface *intf) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 701 | { |
Takashi Iwai | 4c8c3a4 | 2014-10-31 11:00:23 +0100 | [diff] [blame] | 702 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 703 | struct snd_card *card; |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 704 | struct list_head *p; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 705 | |
Pavel Roskin | 81b85b6 | 2011-07-06 11:20:13 -0400 | [diff] [blame] | 706 | if (chip == (void *)-1L) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 707 | return; |
| 708 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 709 | card = chip->card; |
Takashi Iwai | 10e4423 | 2012-11-13 11:22:48 +0100 | [diff] [blame] | 710 | |
| 711 | mutex_lock(®ister_mutex); |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 712 | if (atomic_inc_return(&chip->shutdown) == 1) { |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 713 | struct snd_usb_stream *as; |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 714 | struct snd_usb_endpoint *ep; |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 715 | struct usb_mixer_interface *mixer; |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 716 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 717 | /* wait until all pending tasks done; |
| 718 | * they are protected by snd_usb_lock_shutdown() |
| 719 | */ |
| 720 | wait_event(chip->shutdown_wait, |
| 721 | !atomic_read(&chip->usage_count)); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 722 | snd_card_disconnect(card); |
| 723 | /* release the pcm resources */ |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 724 | list_for_each_entry(as, &chip->pcm_list, list) { |
| 725 | snd_usb_stream_disconnect(as); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 726 | } |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 727 | /* release the endpoint resources */ |
Takashi Iwai | 92a586b | 2014-06-25 14:24:47 +0200 | [diff] [blame] | 728 | list_for_each_entry(ep, &chip->ep_list, list) { |
| 729 | snd_usb_endpoint_release(ep); |
Daniel Mack | edcd363 | 2012-04-12 13:51:12 +0200 | [diff] [blame] | 730 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 731 | /* release the midi resources */ |
| 732 | list_for_each(p, &chip->midi_list) { |
| 733 | snd_usbmidi_disconnect(p); |
| 734 | } |
| 735 | /* release mixer resources */ |
Takashi Iwai | a6cece9 | 2014-10-31 11:24:32 +0100 | [diff] [blame] | 736 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
| 737 | snd_usb_mixer_disconnect(mixer); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 738 | } |
Takashi Iwai | 0725dda | 2014-11-05 15:08:49 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | chip->num_interfaces--; |
| 742 | if (chip->num_interfaces <= 0) { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 743 | usb_chip[chip->index] = NULL; |
| 744 | mutex_unlock(®ister_mutex); |
| 745 | snd_card_free_when_closed(card); |
| 746 | } else { |
| 747 | mutex_unlock(®ister_mutex); |
| 748 | } |
| 749 | } |
| 750 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 751 | /* lock the shutdown (disconnect) task and autoresume */ |
| 752 | int snd_usb_lock_shutdown(struct snd_usb_audio *chip) |
| 753 | { |
| 754 | int err; |
| 755 | |
| 756 | atomic_inc(&chip->usage_count); |
| 757 | if (atomic_read(&chip->shutdown)) { |
| 758 | err = -EIO; |
| 759 | goto error; |
| 760 | } |
| 761 | err = snd_usb_autoresume(chip); |
| 762 | if (err < 0) |
| 763 | goto error; |
| 764 | return 0; |
| 765 | |
| 766 | error: |
| 767 | if (atomic_dec_and_test(&chip->usage_count)) |
| 768 | wake_up(&chip->shutdown_wait); |
| 769 | return err; |
| 770 | } |
| 771 | |
| 772 | /* autosuspend and unlock the shutdown */ |
| 773 | void snd_usb_unlock_shutdown(struct snd_usb_audio *chip) |
| 774 | { |
| 775 | snd_usb_autosuspend(chip); |
| 776 | if (atomic_dec_and_test(&chip->usage_count)) |
| 777 | wake_up(&chip->shutdown_wait); |
| 778 | } |
| 779 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 780 | #ifdef CONFIG_PM |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 781 | |
| 782 | int snd_usb_autoresume(struct snd_usb_audio *chip) |
| 783 | { |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 784 | if (atomic_read(&chip->shutdown)) |
| 785 | return -EIO; |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 786 | if (atomic_inc_return(&chip->active) == 1) |
| 787 | return usb_autopm_get_interface(chip->pm_intf); |
| 788 | return 0; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | void snd_usb_autosuspend(struct snd_usb_audio *chip) |
| 792 | { |
Takashi Iwai | 5c06d68 | 2016-01-12 14:03:33 +0100 | [diff] [blame] | 793 | if (atomic_read(&chip->shutdown)) |
| 794 | return; |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 795 | if (atomic_dec_and_test(&chip->active)) |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 796 | usb_autopm_put_interface(chip->pm_intf); |
| 797 | } |
| 798 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 799 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) |
| 800 | { |
| 801 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 802 | struct snd_usb_stream *as; |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 803 | struct usb_mixer_interface *mixer; |
Adam Goode | f7881e5 | 2014-08-05 12:44:50 -0400 | [diff] [blame] | 804 | struct list_head *p; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 805 | |
| 806 | if (chip == (void *)-1L) |
| 807 | return 0; |
| 808 | |
Takashi Iwai | 0662292 | 2015-08-26 10:23:26 +0200 | [diff] [blame] | 809 | chip->autosuspended = !!PMSG_IS_AUTO(message); |
| 810 | if (!chip->autosuspended) |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 811 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); |
Takashi Iwai | 0662292 | 2015-08-26 10:23:26 +0200 | [diff] [blame] | 812 | if (!chip->num_suspended_intf++) { |
| 813 | list_for_each_entry(as, &chip->pcm_list, list) { |
| 814 | snd_pcm_suspend_all(as->pcm); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 815 | snd_usb_pcm_suspend(as); |
Takashi Iwai | 0662292 | 2015-08-26 10:23:26 +0200 | [diff] [blame] | 816 | as->substream[0].need_setup_ep = |
| 817 | as->substream[1].need_setup_ep = true; |
Antonio Ospite | aa53f98 | 2013-01-29 12:56:30 +0100 | [diff] [blame] | 818 | } |
Takashi Iwai | 0662292 | 2015-08-26 10:23:26 +0200 | [diff] [blame] | 819 | list_for_each(p, &chip->midi_list) |
| 820 | snd_usbmidi_suspend(p); |
Takashi Iwai | 1c53e72 | 2014-05-02 18:14:42 +0200 | [diff] [blame] | 821 | list_for_each_entry(mixer, &chip->mixer_list, list) |
| 822 | snd_usb_mixer_suspend(mixer); |
Takashi Iwai | 0662292 | 2015-08-26 10:23:26 +0200 | [diff] [blame] | 823 | } |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 824 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 828 | static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume) |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 829 | { |
| 830 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 831 | struct snd_usb_stream *as; |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 832 | struct usb_mixer_interface *mixer; |
Adam Goode | f7881e5 | 2014-08-05 12:44:50 -0400 | [diff] [blame] | 833 | struct list_head *p; |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 834 | int err = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 835 | |
| 836 | if (chip == (void *)-1L) |
| 837 | return 0; |
| 838 | if (--chip->num_suspended_intf) |
| 839 | return 0; |
Takashi Iwai | 1ee23fe | 2014-05-02 18:17:06 +0200 | [diff] [blame] | 840 | |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 841 | atomic_inc(&chip->active); /* avoid autopm */ |
Jorge Sanjuan | 3f59aa1 | 2018-07-31 13:28:44 +0100 | [diff] [blame] | 842 | |
| 843 | list_for_each_entry(as, &chip->pcm_list, list) { |
| 844 | err = snd_usb_pcm_resume(as); |
| 845 | if (err < 0) |
| 846 | goto err_out; |
| 847 | } |
| 848 | |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 849 | /* |
| 850 | * ALSA leaves material resumption to user space |
Oliver Neukum | edf7de3 | 2011-03-11 13:19:43 +0100 | [diff] [blame] | 851 | * we just notify and restart the mixers |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 852 | */ |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 853 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 854 | err = snd_usb_mixer_resume(mixer, reset_resume); |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 855 | if (err < 0) |
| 856 | goto err_out; |
| 857 | } |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 858 | |
Adam Goode | f7881e5 | 2014-08-05 12:44:50 -0400 | [diff] [blame] | 859 | list_for_each(p, &chip->midi_list) { |
| 860 | snd_usbmidi_resume(p); |
| 861 | } |
| 862 | |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 863 | if (!chip->autosuspended) |
| 864 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); |
| 865 | chip->autosuspended = 0; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 866 | |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 867 | err_out: |
Takashi Iwai | 47ab154 | 2015-08-25 16:09:00 +0200 | [diff] [blame] | 868 | atomic_dec(&chip->active); /* allow autopm after this point */ |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 869 | return err; |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 870 | } |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 871 | |
| 872 | static int usb_audio_resume(struct usb_interface *intf) |
| 873 | { |
| 874 | return __usb_audio_resume(intf, false); |
| 875 | } |
| 876 | |
| 877 | static int usb_audio_reset_resume(struct usb_interface *intf) |
| 878 | { |
| 879 | return __usb_audio_resume(intf, true); |
| 880 | } |
Randy Dunlap | 6407d47 | 2010-03-22 08:55:35 -0700 | [diff] [blame] | 881 | #else |
| 882 | #define usb_audio_suspend NULL |
| 883 | #define usb_audio_resume NULL |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 884 | #define usb_audio_reset_resume NULL |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 885 | #endif /* CONFIG_PM */ |
| 886 | |
Arvind Yadav | 8824ae2 | 2017-08-06 12:18:56 +0530 | [diff] [blame] | 887 | static const struct usb_device_id usb_audio_ids [] = { |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 888 | #include "quirks-table.h" |
| 889 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), |
| 890 | .bInterfaceClass = USB_CLASS_AUDIO, |
| 891 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, |
| 892 | { } /* Terminating entry */ |
| 893 | }; |
Eldad Zack | f9d3543 | 2013-04-03 23:18:50 +0200 | [diff] [blame] | 894 | MODULE_DEVICE_TABLE(usb, usb_audio_ids); |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * entry point for linux usb interface |
| 898 | */ |
| 899 | |
| 900 | static struct usb_driver usb_audio_driver = { |
| 901 | .name = "snd-usb-audio", |
| 902 | .probe = usb_audio_probe, |
| 903 | .disconnect = usb_audio_disconnect, |
| 904 | .suspend = usb_audio_suspend, |
| 905 | .resume = usb_audio_resume, |
Takashi Iwai | 400362f | 2014-01-20 16:51:16 +0100 | [diff] [blame] | 906 | .reset_resume = usb_audio_reset_resume, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 907 | .id_table = usb_audio_ids, |
Oliver Neukum | 88a8516 | 2011-03-11 14:51:12 +0100 | [diff] [blame] | 908 | .supports_autosuspend = 1, |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 909 | }; |
| 910 | |
Sachin Kamat | 6b5a7c6 | 2013-10-09 17:22:32 +0530 | [diff] [blame] | 911 | module_usb_driver(usb_audio_driver); |