999d7203270d82143c41c5c1f40c813af060f2c3
[linux-2.6.git] / sound / usb / card.c
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  *
28  *   - async unlink should be used for avoiding the sleep inside lock.
29  *     2.4.22 usb-uhci seems buggy for async unlinking and results in
30  *     oops.  in such a cse, pass async_unlink=0 option.
31  *   - the linked URBs would be preferred but not used so far because of
32  *     the instability of unlinking.
33  *   - type II is not supported properly.  there is no device which supports
34  *     this type *correctly*.  SB extigy looks as if it supports, but it's
35  *     indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
36  */
37
38 #include <linux/bitops.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/ctype.h>
44 #include <linux/usb.h>
45 #include <linux/moduleparam.h>
46 #include <linux/mutex.h>
47 #include <linux/usb/audio.h>
48 #include <linux/usb/audio-v2.h>
49 #include <linux/module.h>
50 #ifdef CONFIG_SWITCH
51 #include <linux/switch.h>
52 #endif
53
54 #include <sound/control.h>
55 #include <sound/core.h>
56 #include <sound/info.h>
57 #include <sound/pcm.h>
58 #include <sound/pcm_params.h>
59 #include <sound/initval.h>
60
61 #include "usbaudio.h"
62 #include "card.h"
63 #include "midi.h"
64 #include "mixer.h"
65 #include "proc.h"
66 #include "quirks.h"
67 #include "endpoint.h"
68 #include "helper.h"
69 #include "debug.h"
70 #include "pcm.h"
71 #include "format.h"
72 #include "power.h"
73 #include "stream.h"
74
75 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
76 MODULE_DESCRIPTION("USB Audio");
77 MODULE_LICENSE("GPL");
78 MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
79
80
81 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
82 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
83 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
84 /* Vendor/product IDs for this card */
85 static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
86 static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
87 static int nrpacks = 8;         /* max. number of packets per urb */
88 static bool async_unlink = 1;
89 static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
90 static bool ignore_ctl_error;
91
92 module_param_array(index, int, NULL, 0444);
93 MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
94 module_param_array(id, charp, NULL, 0444);
95 MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
96 module_param_array(enable, bool, NULL, 0444);
97 MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
98 module_param_array(vid, int, NULL, 0444);
99 MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
100 module_param_array(pid, int, NULL, 0444);
101 MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
102 module_param(nrpacks, int, 0644);
103 MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
104 module_param(async_unlink, bool, 0444);
105 MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
106 module_param_array(device_setup, int, NULL, 0444);
107 MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
108 module_param(ignore_ctl_error, bool, 0444);
109 MODULE_PARM_DESC(ignore_ctl_error,
110                  "Ignore errors from USB controller for mixer interfaces.");
111
112 /*
113  * we keep the snd_usb_audio_t instances by ourselves for merging
114  * the all interfaces on the same card as one sound device.
115  */
116
117 static DEFINE_MUTEX(register_mutex);
118 static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
119 static struct usb_driver usb_audio_driver;
120
121 #ifdef CONFIG_SWITCH
122 enum switch_state {
123         STATE_CONNECTED_UNKNOWN = -1,
124         STATE_DISCONNECTED = 0,
125         STATE_CONNECTED = 1
126 };
127
128 static struct switch_dev usb_switch_dev = {
129         .name = "usb_audio",
130 };
131 #endif
132
133 /*
134  * disconnect streams
135  * called from snd_usb_audio_disconnect()
136  */
137 static void snd_usb_stream_disconnect(struct list_head *head)
138 {
139         int idx;
140         struct snd_usb_stream *as;
141         struct snd_usb_substream *subs;
142
143         as = list_entry(head, struct snd_usb_stream, list);
144         for (idx = 0; idx < 2; idx++) {
145                 subs = &as->substream[idx];
146                 if (!subs->num_formats)
147                         continue;
148                 snd_usb_release_substream_urbs(subs, 1);
149                 subs->interface = -1;
150         }
151 }
152
153 static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
154 {
155         struct usb_device *dev = chip->dev;
156         struct usb_host_interface *alts;
157         struct usb_interface_descriptor *altsd;
158         struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
159
160         if (!iface) {
161                 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
162                            dev->devnum, ctrlif, interface);
163                 return -EINVAL;
164         }
165
166         if (usb_interface_claimed(iface)) {
167                 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
168                                                 dev->devnum, ctrlif, interface);
169                 return -EINVAL;
170         }
171
172         alts = &iface->altsetting[0];
173         altsd = get_iface_desc(alts);
174         if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
175              altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
176             altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
177                 int err = snd_usbmidi_create(chip->card, iface,
178                                              &chip->midi_list, NULL);
179                 if (err < 0) {
180                         snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
181                                                 dev->devnum, ctrlif, interface);
182                         return -EINVAL;
183                 }
184                 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
185
186                 return 0;
187         }
188
189         if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
190              altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
191             altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
192                 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
193                                         dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
194                 /* skip non-supported classes */
195                 return -EINVAL;
196         }
197
198         if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
199                 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
200                 return -EINVAL;
201         }
202
203         if (! snd_usb_parse_audio_interface(chip, interface)) {
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) {
232                 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
233                 return -EINVAL;
234         }
235
236         switch (protocol) {
237         default:
238                 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
239                             protocol);
240                 /* fall through */
241
242         case UAC_VERSION_1: {
243                 struct uac1_ac_header_descriptor *h1 = control_header;
244
245                 if (!h1->bInCollection) {
246                         snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
247                         return -EINVAL;
248                 }
249
250                 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
251                         snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
252                         return -EINVAL;
253                 }
254
255                 for (i = 0; i < h1->bInCollection; i++)
256                         snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
257
258                 break;
259         }
260
261         case UAC_VERSION_2: {
262                 struct usb_interface_assoc_descriptor *assoc =
263                         usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
264
265                 if (!assoc) {
266                         snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
267                         return -EINVAL;
268                 }
269
270                 for (i = 0; i < assoc->bInterfaceCount; i++) {
271                         int intf = assoc->bFirstInterface + i;
272
273                         if (intf != ctrlif)
274                                 snd_usb_create_stream(chip, ctrlif, intf);
275                 }
276
277                 break;
278         }
279         }
280
281         return 0;
282 }
283
284 /*
285  * free the chip instance
286  *
287  * here we have to do not much, since pcm and controls are already freed
288  *
289  */
290
291 static int snd_usb_audio_free(struct snd_usb_audio *chip)
292 {
293         kfree(chip);
294         return 0;
295 }
296
297 static int snd_usb_audio_dev_free(struct snd_device *device)
298 {
299         struct snd_usb_audio *chip = device->device_data;
300         return snd_usb_audio_free(chip);
301 }
302
303 static void remove_trailing_spaces(char *str)
304 {
305         char *p;
306
307         if (!*str)
308                 return;
309         for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
310                 *p = 0;
311 }
312
313 /*
314  * create a chip instance and set its names.
315  */
316 static int snd_usb_audio_create(struct usb_device *dev, int idx,
317                                 const struct snd_usb_audio_quirk *quirk,
318                                 struct snd_usb_audio **rchip)
319 {
320         struct snd_card *card;
321         struct snd_usb_audio *chip;
322         int err, len;
323         char component[14];
324         static struct snd_device_ops ops = {
325                 .dev_free =     snd_usb_audio_dev_free,
326         };
327
328         *rchip = NULL;
329
330         switch (snd_usb_get_speed(dev)) {
331         case USB_SPEED_LOW:
332         case USB_SPEED_FULL:
333         case USB_SPEED_HIGH:
334         case USB_SPEED_SUPER:
335                 break;
336         default:
337                 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
338                 return -ENXIO;
339         }
340
341         err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
342         if (err < 0) {
343                 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
344                 return err;
345         }
346
347         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
348         if (! chip) {
349                 snd_card_free(card);
350                 return -ENOMEM;
351         }
352
353         mutex_init(&chip->shutdown_mutex);
354         chip->index = idx;
355         chip->dev = dev;
356         chip->card = card;
357         chip->setup = device_setup[idx];
358         chip->nrpacks = nrpacks;
359         chip->async_unlink = async_unlink;
360         chip->probing = 1;
361
362         chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
363                               le16_to_cpu(dev->descriptor.idProduct));
364         INIT_LIST_HEAD(&chip->pcm_list);
365         INIT_LIST_HEAD(&chip->midi_list);
366         INIT_LIST_HEAD(&chip->mixer_list);
367
368         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
369                 snd_usb_audio_free(chip);
370                 snd_card_free(card);
371                 return err;
372         }
373
374         strcpy(card->driver, "USB-Audio");
375         sprintf(component, "USB%04x:%04x",
376                 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
377         snd_component_add(card, component);
378
379         /* retrieve the device string as shortname */
380         if (quirk && quirk->product_name && *quirk->product_name) {
381                 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
382         } else {
383                 if (!dev->descriptor.iProduct ||
384                     usb_string(dev, dev->descriptor.iProduct,
385                     card->shortname, sizeof(card->shortname)) <= 0) {
386                         /* no name available from anywhere, so use ID */
387                         sprintf(card->shortname, "USB Device %#04x:%#04x",
388                                 USB_ID_VENDOR(chip->usb_id),
389                                 USB_ID_PRODUCT(chip->usb_id));
390                 }
391         }
392         remove_trailing_spaces(card->shortname);
393
394         /* retrieve the vendor and device strings as longname */
395         if (quirk && quirk->vendor_name && *quirk->vendor_name) {
396                 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
397         } else {
398                 if (dev->descriptor.iManufacturer)
399                         len = usb_string(dev, dev->descriptor.iManufacturer,
400                                          card->longname, sizeof(card->longname));
401                 else
402                         len = 0;
403                 /* we don't really care if there isn't any vendor string */
404         }
405         if (len > 0) {
406                 remove_trailing_spaces(card->longname);
407                 if (*card->longname)
408                         strlcat(card->longname, " ", sizeof(card->longname));
409         }
410
411         strlcat(card->longname, card->shortname, sizeof(card->longname));
412
413         len = strlcat(card->longname, " at ", sizeof(card->longname));
414
415         if (len < sizeof(card->longname))
416                 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
417
418         switch (snd_usb_get_speed(dev)) {
419         case USB_SPEED_LOW:
420                 strlcat(card->longname, ", low speed", sizeof(card->longname));
421                 break;
422         case USB_SPEED_FULL:
423                 strlcat(card->longname, ", full speed", sizeof(card->longname));
424                 break;
425         case USB_SPEED_HIGH:
426                 strlcat(card->longname, ", high speed", sizeof(card->longname));
427                 break;
428         case USB_SPEED_SUPER:
429                 strlcat(card->longname, ", super speed", sizeof(card->longname));
430                 break;
431         default:
432                 break;
433         }
434
435         snd_usb_audio_create_proc(chip);
436
437         *rchip = chip;
438         return 0;
439 }
440
441 /*
442  * probe the active usb device
443  *
444  * note that this can be called multiple times per a device, when it
445  * includes multiple audio control interfaces.
446  *
447  * thus we check the usb device pointer and creates the card instance
448  * only at the first time.  the successive calls of this function will
449  * append the pcm interface to the corresponding card.
450  */
451 static struct snd_usb_audio *
452 snd_usb_audio_probe(struct usb_device *dev,
453                     struct usb_interface *intf,
454                     const struct usb_device_id *usb_id)
455 {
456         const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
457         int i, err;
458         struct snd_usb_audio *chip;
459         struct usb_host_interface *alts;
460         int ifnum;
461         u32 id;
462
463         alts = &intf->altsetting[0];
464         ifnum = get_iface_desc(alts)->bInterfaceNumber;
465         id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
466                     le16_to_cpu(dev->descriptor.idProduct));
467         if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
468                 goto __err_val;
469
470         if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
471                 goto __err_val;
472
473         /*
474          * found a config.  now register to ALSA
475          */
476
477         /* check whether it's already registered */
478         chip = NULL;
479         mutex_lock(&register_mutex);
480         for (i = 0; i < SNDRV_CARDS; i++) {
481                 if (usb_chip[i] && usb_chip[i]->dev == dev) {
482                         if (usb_chip[i]->shutdown) {
483                                 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
484                                 goto __error;
485                         }
486                         chip = usb_chip[i];
487                         chip->probing = 1;
488                         break;
489                 }
490         }
491         if (! chip) {
492                 /* it's a fresh one.
493                  * now look for an empty slot and create a new card instance
494                  */
495                 for (i = 0; i < SNDRV_CARDS; i++)
496                         if (enable[i] && ! usb_chip[i] &&
497                             (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
498                             (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
499                                 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
500                                         goto __error;
501                                 }
502                                 snd_card_set_dev(chip->card, &intf->dev);
503                                 chip->pm_intf = intf;
504                                 break;
505                         }
506                 if (!chip) {
507                         printk(KERN_ERR "no available usb audio device\n");
508                         goto __error;
509                 }
510         }
511
512         /*
513          * For devices with more than one control interface, we assume the
514          * first contains the audio controls. We might need a more specific
515          * check here in the future.
516          */
517         if (!chip->ctrl_intf)
518                 chip->ctrl_intf = alts;
519
520         chip->txfr_quirk = 0;
521         err = 1; /* continue */
522         if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
523                 /* need some special handlings */
524                 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
525                         goto __error;
526         }
527
528         if (err > 0) {
529                 /* create normal USB audio interfaces */
530                 if (snd_usb_create_streams(chip, ifnum) < 0 ||
531                     snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
532                         goto __error;
533                 }
534         }
535
536         /* we are allowed to call snd_card_register() many times */
537         if (snd_card_register(chip->card) < 0) {
538                 goto __error;
539         }
540
541 #ifdef CONFIG_SWITCH
542         switch_set_state(&usb_switch_dev, STATE_CONNECTED);
543 #endif
544
545         usb_chip[chip->index] = chip;
546         chip->num_interfaces++;
547         chip->probing = 0;
548         mutex_unlock(&register_mutex);
549
550         return chip;
551
552  __error:
553         if (chip) {
554                 if (!chip->num_interfaces)
555                         snd_card_free(chip->card);
556                 chip->probing = 0;
557         }
558         mutex_unlock(&register_mutex);
559  __err_val:
560
561         return NULL;
562 }
563
564 /*
565  * we need to take care of counter, since disconnection can be called also
566  * many times as well as usb_audio_probe().
567  */
568 static void snd_usb_audio_disconnect(struct usb_device *dev,
569                                      struct snd_usb_audio *chip)
570 {
571         struct snd_card *card;
572         struct list_head *p;
573
574         if (chip == (void *)-1L)
575                 return;
576
577         card = chip->card;
578         mutex_lock(&register_mutex);
579         mutex_lock(&chip->shutdown_mutex);
580         chip->shutdown = 1;
581         chip->num_interfaces--;
582
583 #ifdef CONFIG_SWITCH
584         switch_set_state(&usb_switch_dev, STATE_DISCONNECTED);
585 #endif
586
587         if (chip->num_interfaces <= 0) {
588                 snd_card_disconnect(card);
589                 /* release the pcm resources */
590                 list_for_each(p, &chip->pcm_list) {
591                         snd_usb_stream_disconnect(p);
592                 }
593                 /* release the midi resources */
594                 list_for_each(p, &chip->midi_list) {
595                         snd_usbmidi_disconnect(p);
596                 }
597                 /* release mixer resources */
598                 list_for_each(p, &chip->mixer_list) {
599                         snd_usb_mixer_disconnect(p);
600                 }
601                 usb_chip[chip->index] = NULL;
602                 mutex_unlock(&chip->shutdown_mutex);
603                 mutex_unlock(&register_mutex);
604                 snd_card_free_when_closed(card);
605         } else {
606                 mutex_unlock(&chip->shutdown_mutex);
607                 mutex_unlock(&register_mutex);
608         }
609 }
610
611 /*
612  * new 2.5 USB kernel API
613  */
614 static int usb_audio_probe(struct usb_interface *intf,
615                            const struct usb_device_id *id)
616 {
617         struct snd_usb_audio *chip;
618         chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
619         if (chip) {
620                 usb_set_intfdata(intf, chip);
621                 return 0;
622         } else
623                 return -EIO;
624 }
625
626 static void usb_audio_disconnect(struct usb_interface *intf)
627 {
628         snd_usb_audio_disconnect(interface_to_usbdev(intf),
629                                  usb_get_intfdata(intf));
630 }
631
632 #ifdef CONFIG_PM
633
634 int snd_usb_autoresume(struct snd_usb_audio *chip)
635 {
636         int err = -ENODEV;
637
638         if (!chip->shutdown && !chip->probing)
639                 err = usb_autopm_get_interface(chip->pm_intf);
640
641         return err;
642 }
643
644 void snd_usb_autosuspend(struct snd_usb_audio *chip)
645 {
646         if (!chip->shutdown && !chip->probing)
647                 usb_autopm_put_interface(chip->pm_intf);
648 }
649
650 static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
651 {
652         struct snd_usb_audio *chip = usb_get_intfdata(intf);
653         struct list_head *p;
654         struct snd_usb_stream *as;
655         struct usb_mixer_interface *mixer;
656
657         if (chip == (void *)-1L)
658                 return 0;
659
660         if (!PMSG_IS_AUTO(message)) {
661                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
662                 if (!chip->num_suspended_intf++) {
663                         list_for_each(p, &chip->pcm_list) {
664                                 as = list_entry(p, struct snd_usb_stream, list);
665                                 snd_pcm_suspend_all(as->pcm);
666                         }
667                 }
668         } else {
669                 /*
670                  * otherwise we keep the rest of the system in the dark
671                  * to keep this transparent
672                  */
673                 if (!chip->num_suspended_intf++)
674                         chip->autosuspended = 1;
675         }
676
677         list_for_each_entry(mixer, &chip->mixer_list, list)
678                 snd_usb_mixer_inactivate(mixer);
679
680         return 0;
681 }
682
683 static int usb_audio_resume(struct usb_interface *intf)
684 {
685         struct snd_usb_audio *chip = usb_get_intfdata(intf);
686         struct usb_mixer_interface *mixer;
687         int err = 0;
688
689         if (chip == (void *)-1L)
690                 return 0;
691         if (--chip->num_suspended_intf)
692                 return 0;
693         /*
694          * ALSA leaves material resumption to user space
695          * we just notify and restart the mixers
696          */
697         list_for_each_entry(mixer, &chip->mixer_list, list) {
698                 err = snd_usb_mixer_activate(mixer);
699                 if (err < 0)
700                         goto err_out;
701         }
702
703         if (!chip->autosuspended)
704                 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
705         chip->autosuspended = 0;
706
707 err_out:
708         return err;
709 }
710 #else
711 #define usb_audio_suspend       NULL
712 #define usb_audio_resume        NULL
713 #endif          /* CONFIG_PM */
714
715 static struct usb_device_id usb_audio_ids [] = {
716 #include "quirks-table.h"
717     { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
718       .bInterfaceClass = USB_CLASS_AUDIO,
719       .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
720     { }                                         /* Terminating entry */
721 };
722
723 MODULE_DEVICE_TABLE (usb, usb_audio_ids);
724
725 /*
726  * entry point for linux usb interface
727  */
728
729 static struct usb_driver usb_audio_driver = {
730         .name =         "snd-usb-audio",
731         .probe =        usb_audio_probe,
732         .disconnect =   usb_audio_disconnect,
733         .suspend =      usb_audio_suspend,
734         .resume =       usb_audio_resume,
735         .id_table =     usb_audio_ids,
736         .supports_autosuspend = 1,
737 };
738
739 static int __init snd_usb_audio_init(void)
740 {
741         int err = 0;
742
743         if (nrpacks < 1 || nrpacks > MAX_PACKS) {
744                 printk(KERN_WARNING "invalid nrpacks value.\n");
745                 return -EINVAL;
746         }
747
748 #ifdef CONFIG_SWITCH
749         /* Add usb_audio swith class support */
750         err = switch_dev_register(&usb_switch_dev);
751         if (err < 0){
752                 printk(KERN_ERR "failed to register switch device");
753                 return -EINVAL;
754         }
755 #endif
756
757         err = usb_register(&usb_audio_driver);
758         if (err) {
759                 switch_dev_unregister(&usb_switch_dev);
760         }
761
762         return err;
763 }
764
765 static void __exit snd_usb_audio_cleanup(void)
766 {
767 #ifdef CONFIG_SWITCH
768         switch_dev_unregister(&usb_switch_dev);
769 #endif
770         usb_deregister(&usb_audio_driver);
771 }
772
773 module_init(snd_usb_audio_init);
774 module_exit(snd_usb_audio_cleanup);