blob: 64952e2d3ed1e94a12ad9e593a15298e8eaceee7 [file] [log] [blame]
Daniel Macke5779992010-03-04 19:46:13 +01001/*
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 *
Daniel Macke5779992010-03-04 19:46:13 +010028 * - 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>
Takashi Iwai3ffc1222011-03-21 12:00:00 +010041#include <linux/ctype.h>
Daniel Macke5779992010-03-04 19:46:13 +010042#include <linux/usb.h>
43#include <linux/moduleparam.h>
44#include <linux/mutex.h>
45#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010046#include <linux/usb/audio-v2.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040047#include <linux/module.h>
Daniel Macke5779992010-03-04 19:46:13 +010048
Daniel Mack9e386582011-05-25 09:09:01 +020049#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010050#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"
Daniel Mackf0b5e632010-03-11 21:13:23 +010059#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010060#include "proc.h"
61#include "quirks.h"
62#include "endpoint.h"
63#include "helper.h"
64#include "debug.h"
65#include "pcm.h"
Daniel Macke5779992010-03-04 19:46:13 +010066#include "format.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010067#include "power.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020068#include "stream.h"
Daniel Macke5779992010-03-04 19:46:13 +010069
70MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
71MODULE_DESCRIPTION("USB Audio");
72MODULE_LICENSE("GPL");
73MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
74
75
76static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
77static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103078static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Daniel Macke5779992010-03-04 19:46:13 +010079/* Vendor/product IDs for this card */
80static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
81static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
82static int nrpacks = 8; /* max. number of packets per urb */
Daniel Macke5779992010-03-04 19:46:13 +010083static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103084static bool ignore_ctl_error;
Eldad Zackef02e292013-04-03 23:18:56 +020085static bool autoclock = true;
Daniel Macke5779992010-03-04 19:46:13 +010086
87module_param_array(index, int, NULL, 0444);
88MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
89module_param_array(id, charp, NULL, 0444);
90MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
91module_param_array(enable, bool, NULL, 0444);
92MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
93module_param_array(vid, int, NULL, 0444);
94MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
95module_param_array(pid, int, NULL, 0444);
96MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
97module_param(nrpacks, int, 0644);
98MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
Daniel Macke5779992010-03-04 19:46:13 +010099module_param_array(device_setup, int, NULL, 0444);
100MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
101module_param(ignore_ctl_error, bool, 0444);
102MODULE_PARM_DESC(ignore_ctl_error,
103 "Ignore errors from USB controller for mixer interfaces.");
Eldad Zackef02e292013-04-03 23:18:56 +0200104module_param(autoclock, bool, 0444);
105MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
Daniel Macke5779992010-03-04 19:46:13 +0100106
107/*
108 * we keep the snd_usb_audio_t instances by ourselves for merging
109 * the all interfaces on the same card as one sound device.
110 */
111
112static DEFINE_MUTEX(register_mutex);
113static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
114static struct usb_driver usb_audio_driver;
115
116/*
117 * disconnect streams
118 * called from snd_usb_audio_disconnect()
119 */
120static void snd_usb_stream_disconnect(struct list_head *head)
121{
122 int idx;
123 struct snd_usb_stream *as;
124 struct snd_usb_substream *subs;
125
126 as = list_entry(head, struct snd_usb_stream, list);
127 for (idx = 0; idx < 2; idx++) {
128 subs = &as->substream[idx];
129 if (!subs->num_formats)
Takashi Iwai76195fb2010-09-08 08:27:02 +0200130 continue;
Daniel Macke5779992010-03-04 19:46:13 +0100131 subs->interface = -1;
Daniel Mackedcd3632012-04-12 13:51:12 +0200132 subs->data_endpoint = NULL;
133 subs->sync_endpoint = NULL;
Daniel Macke5779992010-03-04 19:46:13 +0100134 }
135}
136
137static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
138{
139 struct usb_device *dev = chip->dev;
140 struct usb_host_interface *alts;
141 struct usb_interface_descriptor *altsd;
142 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
143
144 if (!iface) {
145 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
146 dev->devnum, ctrlif, interface);
147 return -EINVAL;
148 }
149
Clemens Ladisch342cda292013-06-15 11:21:09 +0200150 alts = &iface->altsetting[0];
151 altsd = get_iface_desc(alts);
152
153 /*
154 * Android with both accessory and audio interfaces enabled gets the
155 * interface numbers wrong.
156 */
157 if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
158 chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
159 interface == 0 &&
160 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
161 altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
162 interface = 2;
163 iface = usb_ifnum_to_if(dev, interface);
164 if (!iface)
165 return -EINVAL;
166 alts = &iface->altsetting[0];
167 altsd = get_iface_desc(alts);
168 }
169
Daniel Macke5779992010-03-04 19:46:13 +0100170 if (usb_interface_claimed(iface)) {
171 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
172 dev->devnum, ctrlif, interface);
173 return -EINVAL;
174 }
175
Daniel Macke5779992010-03-04 19:46:13 +0100176 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
177 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
178 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
179 int err = snd_usbmidi_create(chip->card, iface,
180 &chip->midi_list, NULL);
181 if (err < 0) {
182 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
183 dev->devnum, ctrlif, interface);
184 return -EINVAL;
185 }
186 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
187
188 return 0;
189 }
190
191 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
192 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
193 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
194 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
195 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
196 /* skip non-supported classes */
197 return -EINVAL;
198 }
199
200 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
201 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
202 return -EINVAL;
203 }
204
Daniel Macke8e8bab2011-09-12 18:54:12 +0200205 if (! snd_usb_parse_audio_interface(chip, interface)) {
Daniel Macke5779992010-03-04 19:46:13 +0100206 usb_set_interface(dev, interface, 0); /* reset the current interface */
207 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
208 return -EINVAL;
209 }
210
211 return 0;
212}
213
214/*
215 * parse audio control descriptor and create pcm/midi streams
216 */
217static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
218{
219 struct usb_device *dev = chip->dev;
220 struct usb_host_interface *host_iface;
221 struct usb_interface_descriptor *altsd;
222 void *control_header;
223 int i, protocol;
224
225 /* find audiocontrol interface */
226 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
227 control_header = snd_usb_find_csint_desc(host_iface->extra,
228 host_iface->extralen,
229 NULL, UAC_HEADER);
230 altsd = get_iface_desc(host_iface);
231 protocol = altsd->bInterfaceProtocol;
232
233 if (!control_header) {
234 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
235 return -EINVAL;
236 }
237
238 switch (protocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +0200239 default:
240 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
241 protocol);
242 /* fall through */
243
Daniel Macke5779992010-03-04 19:46:13 +0100244 case UAC_VERSION_1: {
Daniel Mack69da9bc2010-06-16 17:57:28 +0200245 struct uac1_ac_header_descriptor *h1 = control_header;
Daniel Macke5779992010-03-04 19:46:13 +0100246
247 if (!h1->bInCollection) {
248 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
249 return -EINVAL;
250 }
251
252 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
253 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
254 return -EINVAL;
255 }
256
257 for (i = 0; i < h1->bInCollection; i++)
258 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
259
260 break;
261 }
262
263 case UAC_VERSION_2: {
Daniel Macke5779992010-03-04 19:46:13 +0100264 struct usb_interface_assoc_descriptor *assoc =
265 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
266
267 if (!assoc) {
Clemens Ladisch281a6ac2013-03-11 20:15:34 +0100268 /*
269 * Firmware writers cannot count to three. So to find
270 * the IAD on the NuForce UDH-100, also check the next
271 * interface.
272 */
273 struct usb_interface *iface =
274 usb_ifnum_to_if(dev, ctrlif + 1);
275 if (iface &&
276 iface->intf_assoc &&
277 iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
278 iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
279 assoc = iface->intf_assoc;
280 }
281
282 if (!assoc) {
Daniel Macke5779992010-03-04 19:46:13 +0100283 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
284 return -EINVAL;
285 }
286
Daniel Macke5779992010-03-04 19:46:13 +0100287 for (i = 0; i < assoc->bInterfaceCount; i++) {
288 int intf = assoc->bFirstInterface + i;
289
290 if (intf != ctrlif)
291 snd_usb_create_stream(chip, ctrlif, intf);
292 }
293
294 break;
295 }
Daniel Macke5779992010-03-04 19:46:13 +0100296 }
297
298 return 0;
299}
300
301/*
302 * free the chip instance
303 *
304 * here we have to do not much, since pcm and controls are already freed
305 *
306 */
307
308static int snd_usb_audio_free(struct snd_usb_audio *chip)
309{
Daniel Mack596580d2012-04-12 13:51:10 +0200310 mutex_destroy(&chip->mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100311 kfree(chip);
312 return 0;
313}
314
315static int snd_usb_audio_dev_free(struct snd_device *device)
316{
317 struct snd_usb_audio *chip = device->device_data;
318 return snd_usb_audio_free(chip);
319}
320
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100321static void remove_trailing_spaces(char *str)
322{
323 char *p;
324
325 if (!*str)
326 return;
327 for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
328 *p = 0;
329}
Daniel Macke5779992010-03-04 19:46:13 +0100330
331/*
332 * create a chip instance and set its names.
333 */
334static int snd_usb_audio_create(struct usb_device *dev, int idx,
335 const struct snd_usb_audio_quirk *quirk,
336 struct snd_usb_audio **rchip)
337{
338 struct snd_card *card;
339 struct snd_usb_audio *chip;
340 int err, len;
341 char component[14];
342 static struct snd_device_ops ops = {
343 .dev_free = snd_usb_audio_dev_free,
344 };
345
346 *rchip = NULL;
347
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700348 switch (snd_usb_get_speed(dev)) {
349 case USB_SPEED_LOW:
350 case USB_SPEED_FULL:
351 case USB_SPEED_HIGH:
352 case USB_SPEED_SUPER:
353 break;
354 default:
Daniel Macke5779992010-03-04 19:46:13 +0100355 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
356 return -ENXIO;
357 }
358
359 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
360 if (err < 0) {
361 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
362 return err;
363 }
364
365 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
366 if (! chip) {
367 snd_card_free(card);
368 return -ENOMEM;
369 }
370
Daniel Mack596580d2012-04-12 13:51:10 +0200371 mutex_init(&chip->mutex);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200372 init_rwsem(&chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100373 chip->index = idx;
374 chip->dev = dev;
375 chip->card = card;
376 chip->setup = device_setup[idx];
377 chip->nrpacks = nrpacks;
Eldad Zackef02e292013-04-03 23:18:56 +0200378 chip->autoclock = autoclock;
Oliver Neukum88a85162011-03-11 14:51:12 +0100379 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100380
381 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
382 le16_to_cpu(dev->descriptor.idProduct));
383 INIT_LIST_HEAD(&chip->pcm_list);
Daniel Mackedcd3632012-04-12 13:51:12 +0200384 INIT_LIST_HEAD(&chip->ep_list);
Daniel Macke5779992010-03-04 19:46:13 +0100385 INIT_LIST_HEAD(&chip->midi_list);
386 INIT_LIST_HEAD(&chip->mixer_list);
387
388 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
389 snd_usb_audio_free(chip);
390 snd_card_free(card);
391 return err;
392 }
393
394 strcpy(card->driver, "USB-Audio");
395 sprintf(component, "USB%04x:%04x",
396 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
397 snd_component_add(card, component);
398
399 /* retrieve the device string as shortname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100400 if (quirk && quirk->product_name && *quirk->product_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100401 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
402 } else {
403 if (!dev->descriptor.iProduct ||
404 usb_string(dev, dev->descriptor.iProduct,
405 card->shortname, sizeof(card->shortname)) <= 0) {
406 /* no name available from anywhere, so use ID */
407 sprintf(card->shortname, "USB Device %#04x:%#04x",
408 USB_ID_VENDOR(chip->usb_id),
409 USB_ID_PRODUCT(chip->usb_id));
410 }
411 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100412 remove_trailing_spaces(card->shortname);
Daniel Macke5779992010-03-04 19:46:13 +0100413
414 /* retrieve the vendor and device strings as longname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100415 if (quirk && quirk->vendor_name && *quirk->vendor_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100416 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
417 } else {
418 if (dev->descriptor.iManufacturer)
419 len = usb_string(dev, dev->descriptor.iManufacturer,
420 card->longname, sizeof(card->longname));
421 else
422 len = 0;
423 /* we don't really care if there isn't any vendor string */
424 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100425 if (len > 0) {
426 remove_trailing_spaces(card->longname);
427 if (*card->longname)
428 strlcat(card->longname, " ", sizeof(card->longname));
429 }
Daniel Macke5779992010-03-04 19:46:13 +0100430
431 strlcat(card->longname, card->shortname, sizeof(card->longname));
432
433 len = strlcat(card->longname, " at ", sizeof(card->longname));
434
435 if (len < sizeof(card->longname))
436 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
437
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700438 switch (snd_usb_get_speed(dev)) {
439 case USB_SPEED_LOW:
440 strlcat(card->longname, ", low speed", sizeof(card->longname));
441 break;
442 case USB_SPEED_FULL:
443 strlcat(card->longname, ", full speed", sizeof(card->longname));
444 break;
445 case USB_SPEED_HIGH:
446 strlcat(card->longname, ", high speed", sizeof(card->longname));
447 break;
448 case USB_SPEED_SUPER:
449 strlcat(card->longname, ", super speed", sizeof(card->longname));
450 break;
451 default:
452 break;
453 }
Daniel Macke5779992010-03-04 19:46:13 +0100454
455 snd_usb_audio_create_proc(chip);
456
457 *rchip = chip;
458 return 0;
459}
460
461/*
462 * probe the active usb device
463 *
464 * note that this can be called multiple times per a device, when it
465 * includes multiple audio control interfaces.
466 *
467 * thus we check the usb device pointer and creates the card instance
468 * only at the first time. the successive calls of this function will
469 * append the pcm interface to the corresponding card.
470 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400471static struct snd_usb_audio *
472snd_usb_audio_probe(struct usb_device *dev,
473 struct usb_interface *intf,
474 const struct usb_device_id *usb_id)
Daniel Macke5779992010-03-04 19:46:13 +0100475{
476 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
477 int i, err;
478 struct snd_usb_audio *chip;
479 struct usb_host_interface *alts;
480 int ifnum;
481 u32 id;
482
483 alts = &intf->altsetting[0];
484 ifnum = get_iface_desc(alts)->bInterfaceNumber;
485 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
486 le16_to_cpu(dev->descriptor.idProduct));
487 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
488 goto __err_val;
489
490 if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
491 goto __err_val;
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(&register_mutex);
500 for (i = 0; i < SNDRV_CARDS; i++) {
501 if (usb_chip[i] && usb_chip[i]->dev == dev) {
502 if (usb_chip[i]->shutdown) {
503 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
504 goto __error;
505 }
506 chip = usb_chip[i];
Oliver Neukum88a85162011-03-11 14:51:12 +0100507 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100508 break;
509 }
510 }
511 if (! chip) {
512 /* it's a fresh one.
513 * now look for an empty slot and create a new card instance
514 */
515 for (i = 0; i < SNDRV_CARDS; i++)
516 if (enable[i] && ! usb_chip[i] &&
517 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
518 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
519 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
520 goto __error;
521 }
522 snd_card_set_dev(chip->card, &intf->dev);
Oliver Neukum88a85162011-03-11 14:51:12 +0100523 chip->pm_intf = intf;
Daniel Macke5779992010-03-04 19:46:13 +0100524 break;
525 }
526 if (!chip) {
527 printk(KERN_ERR "no available usb audio device\n");
528 goto __error;
529 }
530 }
531
Daniel Mack7b6717e2010-09-02 17:13:15 +0800532 /*
533 * For devices with more than one control interface, we assume the
534 * first contains the audio controls. We might need a more specific
535 * check here in the future.
536 */
537 if (!chip->ctrl_intf)
538 chip->ctrl_intf = alts;
Daniel Mack79f920f2010-05-31 14:51:31 +0200539
Daniel Mack5875c2c2011-05-25 09:08:59 +0200540 chip->txfr_quirk = 0;
541 err = 1; /* continue */
542 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
543 /* need some special handlings */
544 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
545 goto __error;
546 }
547
Daniel Macke5779992010-03-04 19:46:13 +0100548 if (err > 0) {
549 /* create normal USB audio interfaces */
550 if (snd_usb_create_streams(chip, ifnum) < 0 ||
551 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
552 goto __error;
553 }
554 }
555
556 /* we are allowed to call snd_card_register() many times */
557 if (snd_card_register(chip->card) < 0) {
558 goto __error;
559 }
560
561 usb_chip[chip->index] = chip;
562 chip->num_interfaces++;
Oliver Neukum88a85162011-03-11 14:51:12 +0100563 chip->probing = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100564 mutex_unlock(&register_mutex);
565 return chip;
566
567 __error:
Thomas Pfaff61a6a102011-09-26 15:43:59 +0200568 if (chip) {
569 if (!chip->num_interfaces)
570 snd_card_free(chip->card);
571 chip->probing = 0;
572 }
Daniel Macke5779992010-03-04 19:46:13 +0100573 mutex_unlock(&register_mutex);
574 __err_val:
575 return NULL;
576}
577
578/*
579 * we need to take care of counter, since disconnection can be called also
580 * many times as well as usb_audio_probe().
581 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400582static void snd_usb_audio_disconnect(struct usb_device *dev,
583 struct snd_usb_audio *chip)
Daniel Macke5779992010-03-04 19:46:13 +0100584{
Daniel Macke5779992010-03-04 19:46:13 +0100585 struct snd_card *card;
Pavel Roskin03d2f442012-08-30 17:11:17 -0400586 struct list_head *p, *n;
Daniel Macke5779992010-03-04 19:46:13 +0100587
Pavel Roskin81b85b62011-07-06 11:20:13 -0400588 if (chip == (void *)-1L)
Daniel Macke5779992010-03-04 19:46:13 +0100589 return;
590
Daniel Macke5779992010-03-04 19:46:13 +0100591 card = chip->card;
Takashi Iwai34f3c892012-10-15 12:16:02 +0200592 down_write(&chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100593 chip->shutdown = 1;
Takashi Iwai10e44232012-11-13 11:22:48 +0100594 up_write(&chip->shutdown_rwsem);
595
596 mutex_lock(&register_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100597 chip->num_interfaces--;
598 if (chip->num_interfaces <= 0) {
599 snd_card_disconnect(card);
600 /* release the pcm resources */
601 list_for_each(p, &chip->pcm_list) {
602 snd_usb_stream_disconnect(p);
603 }
Daniel Mackedcd3632012-04-12 13:51:12 +0200604 /* release the endpoint resources */
Pavel Roskin03d2f442012-08-30 17:11:17 -0400605 list_for_each_safe(p, n, &chip->ep_list) {
Daniel Mackedcd3632012-04-12 13:51:12 +0200606 snd_usb_endpoint_free(p);
607 }
Daniel Macke5779992010-03-04 19:46:13 +0100608 /* release the midi resources */
609 list_for_each(p, &chip->midi_list) {
610 snd_usbmidi_disconnect(p);
611 }
612 /* release mixer resources */
613 list_for_each(p, &chip->mixer_list) {
614 snd_usb_mixer_disconnect(p);
615 }
616 usb_chip[chip->index] = NULL;
617 mutex_unlock(&register_mutex);
618 snd_card_free_when_closed(card);
619 } else {
620 mutex_unlock(&register_mutex);
621 }
622}
623
624/*
625 * new 2.5 USB kernel API
626 */
627static int usb_audio_probe(struct usb_interface *intf,
628 const struct usb_device_id *id)
629{
Pavel Roskin81b85b62011-07-06 11:20:13 -0400630 struct snd_usb_audio *chip;
Daniel Macke5779992010-03-04 19:46:13 +0100631 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
632 if (chip) {
633 usb_set_intfdata(intf, chip);
634 return 0;
635 } else
636 return -EIO;
637}
638
639static void usb_audio_disconnect(struct usb_interface *intf)
640{
641 snd_usb_audio_disconnect(interface_to_usbdev(intf),
642 usb_get_intfdata(intf));
643}
644
645#ifdef CONFIG_PM
Oliver Neukum88a85162011-03-11 14:51:12 +0100646
647int snd_usb_autoresume(struct snd_usb_audio *chip)
648{
649 int err = -ENODEV;
650
Takashi Iwai34f3c892012-10-15 12:16:02 +0200651 down_read(&chip->shutdown_rwsem);
Takashi Iwai60af3d02013-04-25 07:38:15 +0200652 if (chip->probing)
653 err = 0;
654 else if (!chip->shutdown)
Oliver Neukum88a85162011-03-11 14:51:12 +0100655 err = usb_autopm_get_interface(chip->pm_intf);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200656 up_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100657
658 return err;
659}
660
661void snd_usb_autosuspend(struct snd_usb_audio *chip)
662{
Takashi Iwai34f3c892012-10-15 12:16:02 +0200663 down_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100664 if (!chip->shutdown && !chip->probing)
665 usb_autopm_put_interface(chip->pm_intf);
Takashi Iwai34f3c892012-10-15 12:16:02 +0200666 up_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100667}
668
Daniel Macke5779992010-03-04 19:46:13 +0100669static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
670{
671 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Daniel Macke5779992010-03-04 19:46:13 +0100672 struct snd_usb_stream *as;
Oliver Neukumedf7de32011-03-11 13:19:43 +0100673 struct usb_mixer_interface *mixer;
Daniel Macke5779992010-03-04 19:46:13 +0100674
675 if (chip == (void *)-1L)
676 return 0;
677
Alan Stern5b1b0b82011-08-19 23:49:48 +0200678 if (!PMSG_IS_AUTO(message)) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100679 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
680 if (!chip->num_suspended_intf++) {
Eldad Zack88766f02013-04-03 23:18:49 +0200681 list_for_each_entry(as, &chip->pcm_list, list) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100682 snd_pcm_suspend_all(as->pcm);
Takashi Iwai384dc0852012-09-18 14:49:31 +0200683 as->substream[0].need_setup_ep =
684 as->substream[1].need_setup_ep = true;
Oliver Neukum88a85162011-03-11 14:51:12 +0100685 }
Antonio Ospiteaa53f982013-01-29 12:56:30 +0100686 }
Oliver Neukum88a85162011-03-11 14:51:12 +0100687 } else {
688 /*
689 * otherwise we keep the rest of the system in the dark
690 * to keep this transparent
691 */
692 if (!chip->num_suspended_intf++)
693 chip->autosuspended = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100694 }
695
Oliver Neukum88a85162011-03-11 14:51:12 +0100696 list_for_each_entry(mixer, &chip->mixer_list, list)
697 snd_usb_mixer_inactivate(mixer);
698
Daniel Macke5779992010-03-04 19:46:13 +0100699 return 0;
700}
701
702static int usb_audio_resume(struct usb_interface *intf)
703{
704 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumedf7de32011-03-11 13:19:43 +0100705 struct usb_mixer_interface *mixer;
Oliver Neukum88a85162011-03-11 14:51:12 +0100706 int err = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100707
708 if (chip == (void *)-1L)
709 return 0;
710 if (--chip->num_suspended_intf)
711 return 0;
712 /*
713 * ALSA leaves material resumption to user space
Oliver Neukumedf7de32011-03-11 13:19:43 +0100714 * we just notify and restart the mixers
Daniel Macke5779992010-03-04 19:46:13 +0100715 */
Oliver Neukum88a85162011-03-11 14:51:12 +0100716 list_for_each_entry(mixer, &chip->mixer_list, list) {
717 err = snd_usb_mixer_activate(mixer);
718 if (err < 0)
719 goto err_out;
720 }
Daniel Macke5779992010-03-04 19:46:13 +0100721
Oliver Neukum88a85162011-03-11 14:51:12 +0100722 if (!chip->autosuspended)
723 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
724 chip->autosuspended = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100725
Oliver Neukum88a85162011-03-11 14:51:12 +0100726err_out:
727 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100728}
Randy Dunlap6407d472010-03-22 08:55:35 -0700729#else
730#define usb_audio_suspend NULL
731#define usb_audio_resume NULL
Daniel Macke5779992010-03-04 19:46:13 +0100732#endif /* CONFIG_PM */
733
734static struct usb_device_id usb_audio_ids [] = {
735#include "quirks-table.h"
736 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
737 .bInterfaceClass = USB_CLASS_AUDIO,
738 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
739 { } /* Terminating entry */
740};
Eldad Zackf9d35432013-04-03 23:18:50 +0200741MODULE_DEVICE_TABLE(usb, usb_audio_ids);
Daniel Macke5779992010-03-04 19:46:13 +0100742
743/*
744 * entry point for linux usb interface
745 */
746
747static struct usb_driver usb_audio_driver = {
748 .name = "snd-usb-audio",
749 .probe = usb_audio_probe,
750 .disconnect = usb_audio_disconnect,
751 .suspend = usb_audio_suspend,
752 .resume = usb_audio_resume,
753 .id_table = usb_audio_ids,
Oliver Neukum88a85162011-03-11 14:51:12 +0100754 .supports_autosuspend = 1,
Daniel Macke5779992010-03-04 19:46:13 +0100755};
756
757static int __init snd_usb_audio_init(void)
758{
759 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
760 printk(KERN_WARNING "invalid nrpacks value.\n");
761 return -EINVAL;
762 }
763 return usb_register(&usb_audio_driver);
764}
765
766static void __exit snd_usb_audio_cleanup(void)
767{
768 usb_deregister(&usb_audio_driver);
769}
770
771module_init(snd_usb_audio_init);
772module_exit(snd_usb_audio_cleanup);