]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - sound/usb/card.c
ALSA: snd-usb: implement new endpoint streaming model
[mirror_ubuntu-hirsute-kernel.git] / sound / usb / card.c
CommitLineData
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 *
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>
3ffc1222 44#include <linux/ctype.h>
e5779998
DM
45#include <linux/usb.h>
46#include <linux/moduleparam.h>
47#include <linux/mutex.h>
48#include <linux/usb/audio.h>
7e847894 49#include <linux/usb/audio-v2.h>
da155d5b 50#include <linux/module.h>
e5779998 51
9e38658f 52#include <sound/control.h>
e5779998
DM
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"
f0b5e634 62#include "mixer.h"
e5779998
DM
63#include "proc.h"
64#include "quirks.h"
65#include "endpoint.h"
66#include "helper.h"
67#include "debug.h"
68#include "pcm.h"
e5779998 69#include "format.h"
88a8516a 70#include "power.h"
e8e8babf 71#include "stream.h"
e5779998
DM
72
73MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
74MODULE_DESCRIPTION("USB Audio");
75MODULE_LICENSE("GPL");
76MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
77
78
79static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
80static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
a67ff6a5 81static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
e5779998
DM
82/* Vendor/product IDs for this card */
83static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
84static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
85static int nrpacks = 8; /* max. number of packets per urb */
a67ff6a5 86static bool async_unlink = 1;
e5779998 87static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
a67ff6a5 88static bool ignore_ctl_error;
e5779998
DM
89
90module_param_array(index, int, NULL, 0444);
91MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
92module_param_array(id, charp, NULL, 0444);
93MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
94module_param_array(enable, bool, NULL, 0444);
95MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
96module_param_array(vid, int, NULL, 0444);
97MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
98module_param_array(pid, int, NULL, 0444);
99MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
100module_param(nrpacks, int, 0644);
101MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
102module_param(async_unlink, bool, 0444);
103MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
104module_param_array(device_setup, int, NULL, 0444);
105MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
106module_param(ignore_ctl_error, bool, 0444);
107MODULE_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
115static DEFINE_MUTEX(register_mutex);
116static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
117static struct usb_driver usb_audio_driver;
118
119/*
120 * disconnect streams
121 * called from snd_usb_audio_disconnect()
122 */
123static 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)
76195fb0 133 continue;
e5779998
DM
134 snd_usb_release_substream_urbs(subs, 1);
135 subs->interface = -1;
136 }
137}
138
139static 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
e8e8babf 189 if (! snd_usb_parse_audio_interface(chip, interface)) {
e5779998
DM
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 */
201static 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) {
a2acad82
CL
223 default:
224 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
225 protocol);
226 /* fall through */
227
e5779998 228 case UAC_VERSION_1: {
69da9bcb 229 struct uac1_ac_header_descriptor *h1 = control_header;
e5779998
DM
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: {
e5779998
DM
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
e5779998
DM
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 }
e5779998
DM
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
277static int snd_usb_audio_free(struct snd_usb_audio *chip)
278{
596580d0 279 mutex_destroy(&chip->mutex);
e5779998
DM
280 kfree(chip);
281 return 0;
282}
283
284static 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
3ffc1222
TI
290static 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}
e5779998
DM
299
300/*
301 * create a chip instance and set its names.
302 */
303static 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
4f4e8f69
PZ
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:
e5779998
DM
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
596580d0 340 mutex_init(&chip->mutex);
382225e6 341 mutex_init(&chip->shutdown_mutex);
e5779998
DM
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;
88a8516a 348 chip->probing = 1;
e5779998
DM
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 */
3ffc1222 368 if (quirk && quirk->product_name && *quirk->product_name) {
e5779998
DM
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 }
3ffc1222 380 remove_trailing_spaces(card->shortname);
e5779998
DM
381
382 /* retrieve the vendor and device strings as longname */
3ffc1222 383 if (quirk && quirk->vendor_name && *quirk->vendor_name) {
e5779998
DM
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 }
3ffc1222
TI
393 if (len > 0) {
394 remove_trailing_spaces(card->longname);
395 if (*card->longname)
396 strlcat(card->longname, " ", sizeof(card->longname));
397 }
e5779998
DM
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
4f4e8f69
PZ
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 }
e5779998
DM
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 */
81b85b6b
PR
439static struct snd_usb_audio *
440snd_usb_audio_probe(struct usb_device *dev,
441 struct usb_interface *intf,
442 const struct usb_device_id *usb_id)
e5779998
DM
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];
88a8516a 475 chip->probing = 1;
e5779998
DM
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);
88a8516a 491 chip->pm_intf = intf;
e5779998
DM
492 break;
493 }
494 if (!chip) {
495 printk(KERN_ERR "no available usb audio device\n");
496 goto __error;
497 }
498 }
499
7b6717e1
DM
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;
79f920fb 507
5875c2cb
DM
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
e5779998
DM
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++;
88a8516a 531 chip->probing = 0;
e5779998
DM
532 mutex_unlock(&register_mutex);
533 return chip;
534
535 __error:
61a6a108
TP
536 if (chip) {
537 if (!chip->num_interfaces)
538 snd_card_free(chip->card);
539 chip->probing = 0;
540 }
e5779998
DM
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 */
81b85b6b
PR
550static void snd_usb_audio_disconnect(struct usb_device *dev,
551 struct snd_usb_audio *chip)
e5779998 552{
e5779998
DM
553 struct snd_card *card;
554 struct list_head *p;
555
81b85b6b 556 if (chip == (void *)-1L)
e5779998
DM
557 return;
558
e5779998
DM
559 card = chip->card;
560 mutex_lock(&register_mutex);
382225e6 561 mutex_lock(&chip->shutdown_mutex);
e5779998
DM
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;
382225e6 579 mutex_unlock(&chip->shutdown_mutex);
e5779998
DM
580 mutex_unlock(&register_mutex);
581 snd_card_free_when_closed(card);
582 } else {
382225e6 583 mutex_unlock(&chip->shutdown_mutex);
e5779998
DM
584 mutex_unlock(&register_mutex);
585 }
586}
587
588/*
589 * new 2.5 USB kernel API
590 */
591static int usb_audio_probe(struct usb_interface *intf,
592 const struct usb_device_id *id)
593{
81b85b6b 594 struct snd_usb_audio *chip;
e5779998
DM
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
603static 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
88a8516a
ON
610
611int 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
621void 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
e5779998
DM
627static 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;
edf7de31 632 struct usb_mixer_interface *mixer;
e5779998
DM
633
634 if (chip == (void *)-1L)
635 return 0;
636
5b1b0b81 637 if (!PMSG_IS_AUTO(message)) {
88a8516a
ON
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;
e5779998
DM
652 }
653
88a8516a
ON
654 list_for_each_entry(mixer, &chip->mixer_list, list)
655 snd_usb_mixer_inactivate(mixer);
656
e5779998
DM
657 return 0;
658}
659
660static int usb_audio_resume(struct usb_interface *intf)
661{
662 struct snd_usb_audio *chip = usb_get_intfdata(intf);
edf7de31 663 struct usb_mixer_interface *mixer;
88a8516a 664 int err = 0;
e5779998
DM
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
edf7de31 672 * we just notify and restart the mixers
e5779998 673 */
88a8516a
ON
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 }
e5779998 679
88a8516a
ON
680 if (!chip->autosuspended)
681 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
682 chip->autosuspended = 0;
e5779998 683
88a8516a
ON
684err_out:
685 return err;
e5779998 686}
6407d474
RD
687#else
688#define usb_audio_suspend NULL
689#define usb_audio_resume NULL
e5779998
DM
690#endif /* CONFIG_PM */
691
692static 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
700MODULE_DEVICE_TABLE (usb, usb_audio_ids);
701
702/*
703 * entry point for linux usb interface
704 */
705
706static 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,
88a8516a 713 .supports_autosuspend = 1,
e5779998
DM
714};
715
716static 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
725static void __exit snd_usb_audio_cleanup(void)
726{
727 usb_deregister(&usb_audio_driver);
728}
729
730module_init(snd_usb_audio_init);
731module_exit(snd_usb_audio_cleanup);