Commit | Line | Data |
---|---|---|
e5779998 DM |
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 | * | |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | * | |
25 | * | |
26 | * NOTES: | |
27 | * | |
e5779998 DM |
28 | * - the linked URBs would be preferred but not used so far because of |
29 | * the instability of unlinking. | |
30 | * - type II is not supported properly. there is no device which supports | |
31 | * this type *correctly*. SB extigy looks as if it supports, but it's | |
32 | * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream). | |
33 | */ | |
34 | ||
35 | ||
36 | #include <linux/bitops.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/list.h> | |
39 | #include <linux/slab.h> | |
40 | #include <linux/string.h> | |
3ffc1222 | 41 | #include <linux/ctype.h> |
e5779998 DM |
42 | #include <linux/usb.h> |
43 | #include <linux/moduleparam.h> | |
44 | #include <linux/mutex.h> | |
45 | #include <linux/usb/audio.h> | |
7e847894 | 46 | #include <linux/usb/audio-v2.h> |
da155d5b | 47 | #include <linux/module.h> |
e5779998 | 48 | |
9e38658f | 49 | #include <sound/control.h> |
e5779998 DM |
50 | #include <sound/core.h> |
51 | #include <sound/info.h> | |
52 | #include <sound/pcm.h> | |
53 | #include <sound/pcm_params.h> | |
54 | #include <sound/initval.h> | |
55 | ||
56 | #include "usbaudio.h" | |
57 | #include "card.h" | |
58 | #include "midi.h" | |
f0b5e634 | 59 | #include "mixer.h" |
e5779998 DM |
60 | #include "proc.h" |
61 | #include "quirks.h" | |
62 | #include "endpoint.h" | |
63 | #include "helper.h" | |
64 | #include "debug.h" | |
65 | #include "pcm.h" | |
e5779998 | 66 | #include "format.h" |
88a8516a | 67 | #include "power.h" |
e8e8babf | 68 | #include "stream.h" |
e5779998 DM |
69 | |
70 | MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>"); | |
71 | MODULE_DESCRIPTION("USB Audio"); | |
72 | MODULE_LICENSE("GPL"); | |
73 | MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}"); | |
74 | ||
75 | ||
76 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */ | |
77 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */ | |
a67ff6a5 | 78 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */ |
e5779998 DM |
79 | /* Vendor/product IDs for this card */ |
80 | static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
81 | static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 }; | |
e5779998 | 82 | static int device_setup[SNDRV_CARDS]; /* device parameter for this card */ |
a67ff6a5 | 83 | static bool ignore_ctl_error; |
ef02e29b | 84 | static bool autoclock = true; |
e5779998 DM |
85 | |
86 | module_param_array(index, int, NULL, 0444); | |
87 | MODULE_PARM_DESC(index, "Index value for the USB audio adapter."); | |
88 | module_param_array(id, charp, NULL, 0444); | |
89 | MODULE_PARM_DESC(id, "ID string for the USB audio adapter."); | |
90 | module_param_array(enable, bool, NULL, 0444); | |
91 | MODULE_PARM_DESC(enable, "Enable USB audio adapter."); | |
92 | module_param_array(vid, int, NULL, 0444); | |
93 | MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device."); | |
94 | module_param_array(pid, int, NULL, 0444); | |
95 | MODULE_PARM_DESC(pid, "Product ID for the USB audio device."); | |
e5779998 DM |
96 | module_param_array(device_setup, int, NULL, 0444); |
97 | MODULE_PARM_DESC(device_setup, "Specific device setup (if needed)."); | |
98 | module_param(ignore_ctl_error, bool, 0444); | |
99 | MODULE_PARM_DESC(ignore_ctl_error, | |
100 | "Ignore errors from USB controller for mixer interfaces."); | |
ef02e29b EZ |
101 | module_param(autoclock, bool, 0444); |
102 | MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes)."); | |
e5779998 DM |
103 | |
104 | /* | |
105 | * we keep the snd_usb_audio_t instances by ourselves for merging | |
106 | * the all interfaces on the same card as one sound device. | |
107 | */ | |
108 | ||
109 | static DEFINE_MUTEX(register_mutex); | |
110 | static struct snd_usb_audio *usb_chip[SNDRV_CARDS]; | |
111 | static struct usb_driver usb_audio_driver; | |
112 | ||
113 | /* | |
114 | * disconnect streams | |
4c8c3a4f | 115 | * called from usb_audio_disconnect() |
e5779998 | 116 | */ |
a6cece9d | 117 | static void snd_usb_stream_disconnect(struct snd_usb_stream *as) |
e5779998 DM |
118 | { |
119 | int idx; | |
e5779998 DM |
120 | struct snd_usb_substream *subs; |
121 | ||
e5779998 DM |
122 | for (idx = 0; idx < 2; idx++) { |
123 | subs = &as->substream[idx]; | |
124 | if (!subs->num_formats) | |
76195fb0 | 125 | continue; |
e5779998 | 126 | subs->interface = -1; |
edcd3633 DM |
127 | subs->data_endpoint = NULL; |
128 | subs->sync_endpoint = NULL; | |
e5779998 DM |
129 | } |
130 | } | |
131 | ||
132 | static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface) | |
133 | { | |
134 | struct usb_device *dev = chip->dev; | |
135 | struct usb_host_interface *alts; | |
136 | struct usb_interface_descriptor *altsd; | |
137 | struct usb_interface *iface = usb_ifnum_to_if(dev, interface); | |
138 | ||
139 | if (!iface) { | |
0ba41d91 TI |
140 | dev_err(&dev->dev, "%u:%d : does not exist\n", |
141 | ctrlif, interface); | |
e5779998 DM |
142 | return -EINVAL; |
143 | } | |
144 | ||
342cda29 CL |
145 | alts = &iface->altsetting[0]; |
146 | altsd = get_iface_desc(alts); | |
147 | ||
148 | /* | |
149 | * Android with both accessory and audio interfaces enabled gets the | |
150 | * interface numbers wrong. | |
151 | */ | |
152 | if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) || | |
153 | chip->usb_id == USB_ID(0x18d1, 0x2d05)) && | |
154 | interface == 0 && | |
155 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC && | |
156 | altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) { | |
157 | interface = 2; | |
158 | iface = usb_ifnum_to_if(dev, interface); | |
159 | if (!iface) | |
160 | return -EINVAL; | |
161 | alts = &iface->altsetting[0]; | |
162 | altsd = get_iface_desc(alts); | |
163 | } | |
164 | ||
e5779998 | 165 | if (usb_interface_claimed(iface)) { |
0ba41d91 TI |
166 | dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n", |
167 | ctrlif, interface); | |
e5779998 DM |
168 | return -EINVAL; |
169 | } | |
170 | ||
e5779998 DM |
171 | if ((altsd->bInterfaceClass == USB_CLASS_AUDIO || |
172 | altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) && | |
173 | altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) { | |
79289e24 TI |
174 | int err = __snd_usbmidi_create(chip->card, iface, |
175 | &chip->midi_list, NULL, | |
176 | chip->usb_id); | |
e5779998 | 177 | if (err < 0) { |
0ba41d91 TI |
178 | dev_err(&dev->dev, |
179 | "%u:%d: cannot create sequencer device\n", | |
180 | ctrlif, interface); | |
e5779998 DM |
181 | return -EINVAL; |
182 | } | |
183 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | if ((altsd->bInterfaceClass != USB_CLASS_AUDIO && | |
189 | altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) || | |
190 | altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) { | |
0ba41d91 TI |
191 | dev_dbg(&dev->dev, |
192 | "%u:%d: skipping non-supported interface %d\n", | |
193 | ctrlif, interface, altsd->bInterfaceClass); | |
e5779998 DM |
194 | /* skip non-supported classes */ |
195 | return -EINVAL; | |
196 | } | |
197 | ||
198 | if (snd_usb_get_speed(dev) == USB_SPEED_LOW) { | |
0ba41d91 | 199 | dev_err(&dev->dev, "low speed audio streaming not supported\n"); |
e5779998 DM |
200 | return -EINVAL; |
201 | } | |
202 | ||
e8e8babf | 203 | if (! snd_usb_parse_audio_interface(chip, interface)) { |
e5779998 DM |
204 | usb_set_interface(dev, interface, 0); /* reset the current interface */ |
205 | usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L); | |
206 | return -EINVAL; | |
207 | } | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
212 | /* | |
213 | * parse audio control descriptor and create pcm/midi streams | |
214 | */ | |
215 | static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif) | |
216 | { | |
217 | struct usb_device *dev = chip->dev; | |
218 | struct usb_host_interface *host_iface; | |
219 | struct usb_interface_descriptor *altsd; | |
220 | void *control_header; | |
221 | int i, protocol; | |
222 | ||
223 | /* find audiocontrol interface */ | |
224 | host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0]; | |
225 | control_header = snd_usb_find_csint_desc(host_iface->extra, | |
226 | host_iface->extralen, | |
227 | NULL, UAC_HEADER); | |
228 | altsd = get_iface_desc(host_iface); | |
229 | protocol = altsd->bInterfaceProtocol; | |
230 | ||
231 | if (!control_header) { | |
0ba41d91 | 232 | dev_err(&dev->dev, "cannot find UAC_HEADER\n"); |
e5779998 DM |
233 | return -EINVAL; |
234 | } | |
235 | ||
236 | switch (protocol) { | |
a2acad82 | 237 | default: |
0ba41d91 TI |
238 | dev_warn(&dev->dev, |
239 | "unknown interface protocol %#02x, assuming v1\n", | |
240 | protocol); | |
a2acad82 CL |
241 | /* fall through */ |
242 | ||
e5779998 | 243 | case UAC_VERSION_1: { |
69da9bcb | 244 | struct uac1_ac_header_descriptor *h1 = control_header; |
e5779998 DM |
245 | |
246 | if (!h1->bInCollection) { | |
0ba41d91 | 247 | dev_info(&dev->dev, "skipping empty audio interface (v1)\n"); |
e5779998 DM |
248 | return -EINVAL; |
249 | } | |
250 | ||
251 | if (h1->bLength < sizeof(*h1) + h1->bInCollection) { | |
0ba41d91 | 252 | dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n"); |
e5779998 DM |
253 | return -EINVAL; |
254 | } | |
255 | ||
256 | for (i = 0; i < h1->bInCollection; i++) | |
257 | snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]); | |
258 | ||
259 | break; | |
260 | } | |
261 | ||
262 | case UAC_VERSION_2: { | |
e5779998 DM |
263 | struct usb_interface_assoc_descriptor *assoc = |
264 | usb_ifnum_to_if(dev, ctrlif)->intf_assoc; | |
265 | ||
281a6ac0 CL |
266 | if (!assoc) { |
267 | /* | |
268 | * Firmware writers cannot count to three. So to find | |
269 | * the IAD on the NuForce UDH-100, also check the next | |
270 | * interface. | |
271 | */ | |
272 | struct usb_interface *iface = | |
273 | usb_ifnum_to_if(dev, ctrlif + 1); | |
274 | if (iface && | |
275 | iface->intf_assoc && | |
276 | iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO && | |
277 | iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2) | |
278 | assoc = iface->intf_assoc; | |
279 | } | |
280 | ||
e5779998 | 281 | if (!assoc) { |
0ba41d91 | 282 | dev_err(&dev->dev, "Audio class v2 interfaces need an interface association\n"); |
e5779998 DM |
283 | return -EINVAL; |
284 | } | |
285 | ||
e5779998 DM |
286 | for (i = 0; i < assoc->bInterfaceCount; i++) { |
287 | int intf = assoc->bFirstInterface + i; | |
288 | ||
289 | if (intf != ctrlif) | |
290 | snd_usb_create_stream(chip, ctrlif, intf); | |
291 | } | |
292 | ||
293 | break; | |
294 | } | |
e5779998 DM |
295 | } |
296 | ||
297 | return 0; | |
298 | } | |
299 | ||
300 | /* | |
301 | * free the chip instance | |
302 | * | |
303 | * here we have to do not much, since pcm and controls are already freed | |
304 | * | |
305 | */ | |
306 | ||
307 | static int snd_usb_audio_free(struct snd_usb_audio *chip) | |
308 | { | |
a6cece9d | 309 | struct snd_usb_endpoint *ep, *n; |
92a586bd | 310 | |
a6cece9d TI |
311 | list_for_each_entry_safe(ep, n, &chip->ep_list, list) |
312 | snd_usb_endpoint_free(ep); | |
92a586bd | 313 | |
596580d0 | 314 | mutex_destroy(&chip->mutex); |
79289e24 | 315 | dev_set_drvdata(&chip->dev->dev, NULL); |
e5779998 DM |
316 | kfree(chip); |
317 | return 0; | |
318 | } | |
319 | ||
320 | static int snd_usb_audio_dev_free(struct snd_device *device) | |
321 | { | |
322 | struct snd_usb_audio *chip = device->device_data; | |
323 | return snd_usb_audio_free(chip); | |
324 | } | |
325 | ||
e5779998 DM |
326 | /* |
327 | * create a chip instance and set its names. | |
328 | */ | |
874b8d42 TI |
329 | static int snd_usb_audio_create(struct usb_interface *intf, |
330 | struct usb_device *dev, int idx, | |
e5779998 DM |
331 | const struct snd_usb_audio_quirk *quirk, |
332 | struct snd_usb_audio **rchip) | |
333 | { | |
334 | struct snd_card *card; | |
335 | struct snd_usb_audio *chip; | |
336 | int err, len; | |
337 | char component[14]; | |
338 | static struct snd_device_ops ops = { | |
339 | .dev_free = snd_usb_audio_dev_free, | |
340 | }; | |
341 | ||
342 | *rchip = NULL; | |
343 | ||
4f4e8f69 PZ |
344 | switch (snd_usb_get_speed(dev)) { |
345 | case USB_SPEED_LOW: | |
346 | case USB_SPEED_FULL: | |
347 | case USB_SPEED_HIGH: | |
df3774c5 | 348 | case USB_SPEED_WIRELESS: |
4f4e8f69 PZ |
349 | case USB_SPEED_SUPER: |
350 | break; | |
351 | default: | |
0ba41d91 | 352 | dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev)); |
e5779998 DM |
353 | return -ENXIO; |
354 | } | |
355 | ||
874b8d42 TI |
356 | err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE, |
357 | 0, &card); | |
e5779998 | 358 | if (err < 0) { |
0ba41d91 | 359 | dev_err(&dev->dev, "cannot create card instance %d\n", idx); |
e5779998 DM |
360 | return err; |
361 | } | |
362 | ||
363 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | |
364 | if (! chip) { | |
365 | snd_card_free(card); | |
366 | return -ENOMEM; | |
367 | } | |
368 | ||
596580d0 | 369 | mutex_init(&chip->mutex); |
47ab1545 | 370 | init_waitqueue_head(&chip->shutdown_wait); |
e5779998 DM |
371 | chip->index = idx; |
372 | chip->dev = dev; | |
373 | chip->card = card; | |
374 | chip->setup = device_setup[idx]; | |
ef02e29b | 375 | chip->autoclock = autoclock; |
a6da499b | 376 | atomic_set(&chip->active, 1); /* avoid autopm during probing */ |
47ab1545 TI |
377 | atomic_set(&chip->usage_count, 0); |
378 | atomic_set(&chip->shutdown, 0); | |
e5779998 DM |
379 | |
380 | chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), | |
381 | le16_to_cpu(dev->descriptor.idProduct)); | |
382 | INIT_LIST_HEAD(&chip->pcm_list); | |
edcd3633 | 383 | INIT_LIST_HEAD(&chip->ep_list); |
e5779998 DM |
384 | INIT_LIST_HEAD(&chip->midi_list); |
385 | INIT_LIST_HEAD(&chip->mixer_list); | |
386 | ||
387 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) { | |
388 | snd_usb_audio_free(chip); | |
389 | snd_card_free(card); | |
390 | return err; | |
391 | } | |
392 | ||
393 | strcpy(card->driver, "USB-Audio"); | |
394 | sprintf(component, "USB%04x:%04x", | |
395 | USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id)); | |
396 | snd_component_add(card, component); | |
397 | ||
398 | /* retrieve the device string as shortname */ | |
3ffc1222 | 399 | if (quirk && quirk->product_name && *quirk->product_name) { |
e5779998 DM |
400 | strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname)); |
401 | } else { | |
402 | if (!dev->descriptor.iProduct || | |
403 | usb_string(dev, dev->descriptor.iProduct, | |
404 | card->shortname, sizeof(card->shortname)) <= 0) { | |
405 | /* no name available from anywhere, so use ID */ | |
406 | sprintf(card->shortname, "USB Device %#04x:%#04x", | |
407 | USB_ID_VENDOR(chip->usb_id), | |
408 | USB_ID_PRODUCT(chip->usb_id)); | |
409 | } | |
410 | } | |
ae366c20 | 411 | strim(card->shortname); |
e5779998 DM |
412 | |
413 | /* retrieve the vendor and device strings as longname */ | |
3ffc1222 | 414 | if (quirk && quirk->vendor_name && *quirk->vendor_name) { |
e5779998 DM |
415 | len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname)); |
416 | } else { | |
417 | if (dev->descriptor.iManufacturer) | |
418 | len = usb_string(dev, dev->descriptor.iManufacturer, | |
419 | card->longname, sizeof(card->longname)); | |
420 | else | |
421 | len = 0; | |
422 | /* we don't really care if there isn't any vendor string */ | |
423 | } | |
3ffc1222 | 424 | if (len > 0) { |
ae366c20 | 425 | strim(card->longname); |
3ffc1222 TI |
426 | if (*card->longname) |
427 | strlcat(card->longname, " ", sizeof(card->longname)); | |
428 | } | |
e5779998 DM |
429 | |
430 | strlcat(card->longname, card->shortname, sizeof(card->longname)); | |
431 | ||
432 | len = strlcat(card->longname, " at ", sizeof(card->longname)); | |
433 | ||
434 | if (len < sizeof(card->longname)) | |
435 | usb_make_path(dev, card->longname + len, sizeof(card->longname) - len); | |
436 | ||
4f4e8f69 PZ |
437 | switch (snd_usb_get_speed(dev)) { |
438 | case USB_SPEED_LOW: | |
439 | strlcat(card->longname, ", low speed", sizeof(card->longname)); | |
440 | break; | |
441 | case USB_SPEED_FULL: | |
442 | strlcat(card->longname, ", full speed", sizeof(card->longname)); | |
443 | break; | |
444 | case USB_SPEED_HIGH: | |
445 | strlcat(card->longname, ", high speed", sizeof(card->longname)); | |
446 | break; | |
447 | case USB_SPEED_SUPER: | |
448 | strlcat(card->longname, ", super speed", sizeof(card->longname)); | |
449 | break; | |
450 | default: | |
451 | break; | |
452 | } | |
e5779998 DM |
453 | |
454 | snd_usb_audio_create_proc(chip); | |
455 | ||
456 | *rchip = chip; | |
457 | return 0; | |
458 | } | |
459 | ||
460 | /* | |
461 | * probe the active usb device | |
462 | * | |
463 | * note that this can be called multiple times per a device, when it | |
464 | * includes multiple audio control interfaces. | |
465 | * | |
466 | * thus we check the usb device pointer and creates the card instance | |
467 | * only at the first time. the successive calls of this function will | |
468 | * append the pcm interface to the corresponding card. | |
469 | */ | |
4c8c3a4f TI |
470 | static int usb_audio_probe(struct usb_interface *intf, |
471 | const struct usb_device_id *usb_id) | |
e5779998 | 472 | { |
4c8c3a4f TI |
473 | struct usb_device *dev = interface_to_usbdev(intf); |
474 | const struct snd_usb_audio_quirk *quirk = | |
475 | (const struct snd_usb_audio_quirk *)usb_id->driver_info; | |
e5779998 | 476 | struct snd_usb_audio *chip; |
4c8c3a4f | 477 | int i, err; |
e5779998 DM |
478 | struct usb_host_interface *alts; |
479 | int ifnum; | |
480 | u32 id; | |
481 | ||
482 | alts = &intf->altsetting[0]; | |
483 | ifnum = get_iface_desc(alts)->bInterfaceNumber; | |
484 | id = USB_ID(le16_to_cpu(dev->descriptor.idVendor), | |
485 | le16_to_cpu(dev->descriptor.idProduct)); | |
486 | if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum) | |
4c8c3a4f | 487 | return -ENXIO; |
e5779998 | 488 | |
79289e24 | 489 | err = snd_usb_apply_boot_quirk(dev, intf, quirk, id); |
4c8c3a4f TI |
490 | if (err < 0) |
491 | return err; | |
e5779998 DM |
492 | |
493 | /* | |
494 | * found a config. now register to ALSA | |
495 | */ | |
496 | ||
497 | /* check whether it's already registered */ | |
498 | chip = NULL; | |
499 | mutex_lock(®ister_mutex); | |
500 | for (i = 0; i < SNDRV_CARDS; i++) { | |
501 | if (usb_chip[i] && usb_chip[i]->dev == dev) { | |
47ab1545 | 502 | if (atomic_read(&usb_chip[i]->shutdown)) { |
0ba41d91 | 503 | dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n"); |
4c8c3a4f | 504 | err = -EIO; |
e5779998 DM |
505 | goto __error; |
506 | } | |
507 | chip = usb_chip[i]; | |
79289e24 | 508 | dev_set_drvdata(&dev->dev, chip); |
a6da499b | 509 | atomic_inc(&chip->active); /* avoid autopm */ |
e5779998 DM |
510 | break; |
511 | } | |
512 | } | |
513 | if (! chip) { | |
514 | /* it's a fresh one. | |
515 | * now look for an empty slot and create a new card instance | |
516 | */ | |
517 | for (i = 0; i < SNDRV_CARDS; i++) | |
518 | if (enable[i] && ! usb_chip[i] && | |
519 | (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) && | |
520 | (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) { | |
4c8c3a4f TI |
521 | err = snd_usb_audio_create(intf, dev, i, quirk, |
522 | &chip); | |
523 | if (err < 0) | |
e5779998 | 524 | goto __error; |
88a8516a | 525 | chip->pm_intf = intf; |
e5779998 DM |
526 | break; |
527 | } | |
528 | if (!chip) { | |
0ba41d91 | 529 | dev_err(&dev->dev, "no available usb audio device\n"); |
4c8c3a4f | 530 | err = -ENODEV; |
e5779998 DM |
531 | goto __error; |
532 | } | |
533 | } | |
534 | ||
7b6717e1 DM |
535 | /* |
536 | * For devices with more than one control interface, we assume the | |
537 | * first contains the audio controls. We might need a more specific | |
538 | * check here in the future. | |
539 | */ | |
540 | if (!chip->ctrl_intf) | |
541 | chip->ctrl_intf = alts; | |
79f920fb | 542 | |
5875c2cb DM |
543 | chip->txfr_quirk = 0; |
544 | err = 1; /* continue */ | |
545 | if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) { | |
546 | /* need some special handlings */ | |
4c8c3a4f TI |
547 | err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk); |
548 | if (err < 0) | |
5875c2cb DM |
549 | goto __error; |
550 | } | |
551 | ||
e5779998 DM |
552 | if (err > 0) { |
553 | /* create normal USB audio interfaces */ | |
4c8c3a4f TI |
554 | err = snd_usb_create_streams(chip, ifnum); |
555 | if (err < 0) | |
556 | goto __error; | |
557 | err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error); | |
558 | if (err < 0) | |
e5779998 | 559 | goto __error; |
e5779998 DM |
560 | } |
561 | ||
562 | /* we are allowed to call snd_card_register() many times */ | |
4c8c3a4f TI |
563 | err = snd_card_register(chip->card); |
564 | if (err < 0) | |
e5779998 | 565 | goto __error; |
e5779998 DM |
566 | |
567 | usb_chip[chip->index] = chip; | |
568 | chip->num_interfaces++; | |
4c8c3a4f | 569 | usb_set_intfdata(intf, chip); |
a6da499b | 570 | atomic_dec(&chip->active); |
e5779998 | 571 | mutex_unlock(®ister_mutex); |
4c8c3a4f | 572 | return 0; |
e5779998 DM |
573 | |
574 | __error: | |
61a6a108 TP |
575 | if (chip) { |
576 | if (!chip->num_interfaces) | |
577 | snd_card_free(chip->card); | |
a6da499b | 578 | atomic_dec(&chip->active); |
61a6a108 | 579 | } |
e5779998 | 580 | mutex_unlock(®ister_mutex); |
4c8c3a4f | 581 | return err; |
e5779998 DM |
582 | } |
583 | ||
584 | /* | |
585 | * we need to take care of counter, since disconnection can be called also | |
586 | * many times as well as usb_audio_probe(). | |
587 | */ | |
4c8c3a4f | 588 | static void usb_audio_disconnect(struct usb_interface *intf) |
e5779998 | 589 | { |
4c8c3a4f | 590 | struct snd_usb_audio *chip = usb_get_intfdata(intf); |
e5779998 | 591 | struct snd_card *card; |
92a586bd | 592 | struct list_head *p; |
e5779998 | 593 | |
81b85b6b | 594 | if (chip == (void *)-1L) |
e5779998 DM |
595 | return; |
596 | ||
e5779998 | 597 | card = chip->card; |
10e44239 TI |
598 | |
599 | mutex_lock(®ister_mutex); | |
47ab1545 | 600 | if (atomic_inc_return(&chip->shutdown) == 1) { |
a6cece9d | 601 | struct snd_usb_stream *as; |
92a586bd | 602 | struct snd_usb_endpoint *ep; |
a6cece9d | 603 | struct usb_mixer_interface *mixer; |
92a586bd | 604 | |
47ab1545 TI |
605 | /* wait until all pending tasks done; |
606 | * they are protected by snd_usb_lock_shutdown() | |
607 | */ | |
608 | wait_event(chip->shutdown_wait, | |
609 | !atomic_read(&chip->usage_count)); | |
e5779998 DM |
610 | snd_card_disconnect(card); |
611 | /* release the pcm resources */ | |
a6cece9d TI |
612 | list_for_each_entry(as, &chip->pcm_list, list) { |
613 | snd_usb_stream_disconnect(as); | |
e5779998 | 614 | } |
edcd3633 | 615 | /* release the endpoint resources */ |
92a586bd TI |
616 | list_for_each_entry(ep, &chip->ep_list, list) { |
617 | snd_usb_endpoint_release(ep); | |
edcd3633 | 618 | } |
e5779998 DM |
619 | /* release the midi resources */ |
620 | list_for_each(p, &chip->midi_list) { | |
621 | snd_usbmidi_disconnect(p); | |
622 | } | |
623 | /* release mixer resources */ | |
a6cece9d TI |
624 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
625 | snd_usb_mixer_disconnect(mixer); | |
e5779998 | 626 | } |
0725dda2 TI |
627 | } |
628 | ||
629 | chip->num_interfaces--; | |
630 | if (chip->num_interfaces <= 0) { | |
e5779998 DM |
631 | usb_chip[chip->index] = NULL; |
632 | mutex_unlock(®ister_mutex); | |
633 | snd_card_free_when_closed(card); | |
634 | } else { | |
635 | mutex_unlock(®ister_mutex); | |
636 | } | |
637 | } | |
638 | ||
47ab1545 TI |
639 | /* lock the shutdown (disconnect) task and autoresume */ |
640 | int snd_usb_lock_shutdown(struct snd_usb_audio *chip) | |
88a8516a | 641 | { |
47ab1545 | 642 | int err; |
88a8516a | 643 | |
47ab1545 TI |
644 | atomic_inc(&chip->usage_count); |
645 | if (atomic_read(&chip->shutdown)) { | |
646 | err = -EIO; | |
647 | goto error; | |
648 | } | |
649 | err = snd_usb_autoresume(chip); | |
650 | if (err < 0) | |
651 | goto error; | |
652 | return 0; | |
88a8516a | 653 | |
47ab1545 TI |
654 | error: |
655 | if (atomic_dec_and_test(&chip->usage_count)) | |
656 | wake_up(&chip->shutdown_wait); | |
88a8516a ON |
657 | return err; |
658 | } | |
659 | ||
47ab1545 TI |
660 | /* autosuspend and unlock the shutdown */ |
661 | void snd_usb_unlock_shutdown(struct snd_usb_audio *chip) | |
662 | { | |
663 | snd_usb_autosuspend(chip); | |
664 | if (atomic_dec_and_test(&chip->usage_count)) | |
665 | wake_up(&chip->shutdown_wait); | |
666 | } | |
667 | ||
668 | #ifdef CONFIG_PM | |
669 | ||
670 | int snd_usb_autoresume(struct snd_usb_audio *chip) | |
671 | { | |
672 | if (atomic_read(&chip->shutdown)) | |
673 | return -EIO; | |
47ab1545 TI |
674 | if (atomic_inc_return(&chip->active) == 1) |
675 | return usb_autopm_get_interface(chip->pm_intf); | |
676 | return 0; | |
677 | } | |
678 | ||
88a8516a ON |
679 | void snd_usb_autosuspend(struct snd_usb_audio *chip) |
680 | { | |
5c06d68b TI |
681 | if (atomic_read(&chip->shutdown)) |
682 | return; | |
47ab1545 | 683 | if (atomic_dec_and_test(&chip->active)) |
88a8516a ON |
684 | usb_autopm_put_interface(chip->pm_intf); |
685 | } | |
686 | ||
e5779998 DM |
687 | static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message) |
688 | { | |
689 | struct snd_usb_audio *chip = usb_get_intfdata(intf); | |
e5779998 | 690 | struct snd_usb_stream *as; |
edf7de31 | 691 | struct usb_mixer_interface *mixer; |
f7881e5e | 692 | struct list_head *p; |
e5779998 DM |
693 | |
694 | if (chip == (void *)-1L) | |
695 | return 0; | |
696 | ||
0662292a TI |
697 | chip->autosuspended = !!PMSG_IS_AUTO(message); |
698 | if (!chip->autosuspended) | |
88a8516a | 699 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot); |
0662292a TI |
700 | if (!chip->num_suspended_intf++) { |
701 | list_for_each_entry(as, &chip->pcm_list, list) { | |
702 | snd_pcm_suspend_all(as->pcm); | |
703 | as->substream[0].need_setup_ep = | |
704 | as->substream[1].need_setup_ep = true; | |
aa53f986 | 705 | } |
0662292a TI |
706 | list_for_each(p, &chip->midi_list) |
707 | snd_usbmidi_suspend(p); | |
1c53e725 TI |
708 | list_for_each_entry(mixer, &chip->mixer_list, list) |
709 | snd_usb_mixer_suspend(mixer); | |
0662292a | 710 | } |
88a8516a | 711 | |
e5779998 DM |
712 | return 0; |
713 | } | |
714 | ||
400362f1 | 715 | static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume) |
e5779998 DM |
716 | { |
717 | struct snd_usb_audio *chip = usb_get_intfdata(intf); | |
edf7de31 | 718 | struct usb_mixer_interface *mixer; |
f7881e5e | 719 | struct list_head *p; |
88a8516a | 720 | int err = 0; |
e5779998 DM |
721 | |
722 | if (chip == (void *)-1L) | |
723 | return 0; | |
724 | if (--chip->num_suspended_intf) | |
725 | return 0; | |
1ee23fe0 | 726 | |
47ab1545 | 727 | atomic_inc(&chip->active); /* avoid autopm */ |
e5779998 DM |
728 | /* |
729 | * ALSA leaves material resumption to user space | |
edf7de31 | 730 | * we just notify and restart the mixers |
e5779998 | 731 | */ |
88a8516a | 732 | list_for_each_entry(mixer, &chip->mixer_list, list) { |
400362f1 | 733 | err = snd_usb_mixer_resume(mixer, reset_resume); |
88a8516a ON |
734 | if (err < 0) |
735 | goto err_out; | |
736 | } | |
e5779998 | 737 | |
f7881e5e AG |
738 | list_for_each(p, &chip->midi_list) { |
739 | snd_usbmidi_resume(p); | |
740 | } | |
741 | ||
88a8516a ON |
742 | if (!chip->autosuspended) |
743 | snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0); | |
744 | chip->autosuspended = 0; | |
e5779998 | 745 | |
88a8516a | 746 | err_out: |
47ab1545 | 747 | atomic_dec(&chip->active); /* allow autopm after this point */ |
88a8516a | 748 | return err; |
e5779998 | 749 | } |
400362f1 TI |
750 | |
751 | static int usb_audio_resume(struct usb_interface *intf) | |
752 | { | |
753 | return __usb_audio_resume(intf, false); | |
754 | } | |
755 | ||
756 | static int usb_audio_reset_resume(struct usb_interface *intf) | |
757 | { | |
758 | return __usb_audio_resume(intf, true); | |
759 | } | |
6407d474 RD |
760 | #else |
761 | #define usb_audio_suspend NULL | |
762 | #define usb_audio_resume NULL | |
400362f1 | 763 | #define usb_audio_reset_resume NULL |
e5779998 DM |
764 | #endif /* CONFIG_PM */ |
765 | ||
766 | static struct usb_device_id usb_audio_ids [] = { | |
767 | #include "quirks-table.h" | |
768 | { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS), | |
769 | .bInterfaceClass = USB_CLASS_AUDIO, | |
770 | .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL }, | |
771 | { } /* Terminating entry */ | |
772 | }; | |
f9d35435 | 773 | MODULE_DEVICE_TABLE(usb, usb_audio_ids); |
e5779998 DM |
774 | |
775 | /* | |
776 | * entry point for linux usb interface | |
777 | */ | |
778 | ||
779 | static struct usb_driver usb_audio_driver = { | |
780 | .name = "snd-usb-audio", | |
781 | .probe = usb_audio_probe, | |
782 | .disconnect = usb_audio_disconnect, | |
783 | .suspend = usb_audio_suspend, | |
784 | .resume = usb_audio_resume, | |
400362f1 | 785 | .reset_resume = usb_audio_reset_resume, |
e5779998 | 786 | .id_table = usb_audio_ids, |
88a8516a | 787 | .supports_autosuspend = 1, |
e5779998 DM |
788 | }; |
789 | ||
6b5a7c66 | 790 | module_usb_driver(usb_audio_driver); |