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