blob: 1452312cd69a26e37625e06aa3856cca25c32ece [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 *
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>
Takashi Iwai3ffc1222011-03-21 12:00:00 +010044#include <linux/ctype.h>
Daniel Macke5779992010-03-04 19:46:13 +010045#include <linux/usb.h>
46#include <linux/moduleparam.h>
47#include <linux/mutex.h>
48#include <linux/usb/audio.h>
Daniel Mack7e847892010-03-11 21:13:20 +010049#include <linux/usb/audio-v2.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040050#include <linux/module.h>
Harmandeep Singh52ca48e2012-03-05 16:38:33 -080051#include <linux/switch.h>
Daniel Macke5779992010-03-04 19:46:13 +010052
Daniel Mack9e386582011-05-25 09:09:01 +020053#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010054#include <sound/core.h>
55#include <sound/info.h>
56#include <sound/pcm.h>
57#include <sound/pcm_params.h>
58#include <sound/initval.h>
59
60#include "usbaudio.h"
61#include "card.h"
62#include "midi.h"
Daniel Mackf0b5e632010-03-11 21:13:23 +010063#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010064#include "proc.h"
65#include "quirks.h"
66#include "endpoint.h"
67#include "helper.h"
68#include "debug.h"
69#include "pcm.h"
Daniel Macke5779992010-03-04 19:46:13 +010070#include "format.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010071#include "power.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020072#include "stream.h"
Daniel Macke5779992010-03-04 19:46:13 +010073
74MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
75MODULE_DESCRIPTION("USB Audio");
76MODULE_LICENSE("GPL");
77MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
78
79
80static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
81static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103082static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Daniel Macke5779992010-03-04 19:46:13 +010083/* Vendor/product IDs for this card */
84static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
85static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
86static int nrpacks = 8; /* max. number of packets per urb */
Rusty Russella67ff6a2011-12-15 13:49:36 +103087static bool async_unlink = 1;
Daniel Macke5779992010-03-04 19:46:13 +010088static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103089static bool ignore_ctl_error;
Harmandeep Singh52ca48e2012-03-05 16:38:33 -080090struct switch_dev *usbaudiosdev;
Daniel Macke5779992010-03-04 19:46:13 +010091
92module_param_array(index, int, NULL, 0444);
93MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
94module_param_array(id, charp, NULL, 0444);
95MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
96module_param_array(enable, bool, NULL, 0444);
97MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
98module_param_array(vid, int, NULL, 0444);
99MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
100module_param_array(pid, int, NULL, 0444);
101MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
102module_param(nrpacks, int, 0644);
103MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
104module_param(async_unlink, bool, 0444);
105MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
106module_param_array(device_setup, int, NULL, 0444);
107MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
108module_param(ignore_ctl_error, bool, 0444);
109MODULE_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
117static DEFINE_MUTEX(register_mutex);
118static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
119static struct usb_driver usb_audio_driver;
120
121/*
122 * disconnect streams
123 * called from snd_usb_audio_disconnect()
124 */
125static void snd_usb_stream_disconnect(struct list_head *head)
126{
127 int idx;
128 struct snd_usb_stream *as;
129 struct snd_usb_substream *subs;
130
131 as = list_entry(head, struct snd_usb_stream, list);
132 for (idx = 0; idx < 2; idx++) {
133 subs = &as->substream[idx];
134 if (!subs->num_formats)
Takashi Iwai76195fb2010-09-08 08:27:02 +0200135 continue;
Daniel Macke5779992010-03-04 19:46:13 +0100136 snd_usb_release_substream_urbs(subs, 1);
137 subs->interface = -1;
138 }
139}
140
141static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
142{
143 struct usb_device *dev = chip->dev;
144 struct usb_host_interface *alts;
145 struct usb_interface_descriptor *altsd;
146 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
147
148 if (!iface) {
149 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
150 dev->devnum, ctrlif, interface);
151 return -EINVAL;
152 }
153
154 if (usb_interface_claimed(iface)) {
155 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
156 dev->devnum, ctrlif, interface);
157 return -EINVAL;
158 }
159
160 alts = &iface->altsetting[0];
161 altsd = get_iface_desc(alts);
162 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
163 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
164 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
165 int err = snd_usbmidi_create(chip->card, iface,
166 &chip->midi_list, NULL);
167 if (err < 0) {
168 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
169 dev->devnum, ctrlif, interface);
170 return -EINVAL;
171 }
172 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
173
174 return 0;
175 }
176
177 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
178 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
179 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
180 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
181 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
182 /* skip non-supported classes */
183 return -EINVAL;
184 }
185
186 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
187 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
188 return -EINVAL;
189 }
190
Daniel Macke8e8bab2011-09-12 18:54:12 +0200191 if (! snd_usb_parse_audio_interface(chip, interface)) {
Daniel Macke5779992010-03-04 19:46:13 +0100192 usb_set_interface(dev, interface, 0); /* reset the current interface */
193 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
194 return -EINVAL;
195 }
196
197 return 0;
198}
199
200/*
201 * parse audio control descriptor and create pcm/midi streams
202 */
203static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
204{
205 struct usb_device *dev = chip->dev;
206 struct usb_host_interface *host_iface;
207 struct usb_interface_descriptor *altsd;
208 void *control_header;
209 int i, protocol;
210
211 /* find audiocontrol interface */
212 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
213 control_header = snd_usb_find_csint_desc(host_iface->extra,
214 host_iface->extralen,
215 NULL, UAC_HEADER);
216 altsd = get_iface_desc(host_iface);
217 protocol = altsd->bInterfaceProtocol;
218
219 if (!control_header) {
220 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
221 return -EINVAL;
222 }
223
224 switch (protocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +0200225 default:
226 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
227 protocol);
228 /* fall through */
229
Daniel Macke5779992010-03-04 19:46:13 +0100230 case UAC_VERSION_1: {
Daniel Mack69da9bc2010-06-16 17:57:28 +0200231 struct uac1_ac_header_descriptor *h1 = control_header;
Daniel Macke5779992010-03-04 19:46:13 +0100232
233 if (!h1->bInCollection) {
234 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
235 return -EINVAL;
236 }
237
238 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
239 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
240 return -EINVAL;
241 }
242
243 for (i = 0; i < h1->bInCollection; i++)
244 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
245
246 break;
247 }
248
249 case UAC_VERSION_2: {
Daniel Macke5779992010-03-04 19:46:13 +0100250 struct usb_interface_assoc_descriptor *assoc =
251 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
252
253 if (!assoc) {
254 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
255 return -EINVAL;
256 }
257
Daniel Macke5779992010-03-04 19:46:13 +0100258 for (i = 0; i < assoc->bInterfaceCount; i++) {
259 int intf = assoc->bFirstInterface + i;
260
261 if (intf != ctrlif)
262 snd_usb_create_stream(chip, ctrlif, intf);
263 }
264
265 break;
266 }
Daniel Macke5779992010-03-04 19:46:13 +0100267 }
268
269 return 0;
270}
271
272/*
273 * free the chip instance
274 *
275 * here we have to do not much, since pcm and controls are already freed
276 *
277 */
278
279static int snd_usb_audio_free(struct snd_usb_audio *chip)
280{
281 kfree(chip);
282 return 0;
283}
284
285static int snd_usb_audio_dev_free(struct snd_device *device)
286{
287 struct snd_usb_audio *chip = device->device_data;
288 return snd_usb_audio_free(chip);
289}
290
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100291static void remove_trailing_spaces(char *str)
292{
293 char *p;
294
295 if (!*str)
296 return;
297 for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
298 *p = 0;
299}
Daniel Macke5779992010-03-04 19:46:13 +0100300
301/*
302 * create a chip instance and set its names.
303 */
304static int snd_usb_audio_create(struct usb_device *dev, int idx,
305 const struct snd_usb_audio_quirk *quirk,
306 struct snd_usb_audio **rchip)
307{
308 struct snd_card *card;
309 struct snd_usb_audio *chip;
310 int err, len;
311 char component[14];
312 static struct snd_device_ops ops = {
313 .dev_free = snd_usb_audio_dev_free,
314 };
315
316 *rchip = NULL;
317
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700318 switch (snd_usb_get_speed(dev)) {
319 case USB_SPEED_LOW:
320 case USB_SPEED_FULL:
321 case USB_SPEED_HIGH:
322 case USB_SPEED_SUPER:
323 break;
324 default:
Daniel Macke5779992010-03-04 19:46:13 +0100325 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
326 return -ENXIO;
327 }
328
329 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
330 if (err < 0) {
331 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
332 return err;
333 }
334
335 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
336 if (! chip) {
337 snd_card_free(card);
338 return -ENOMEM;
339 }
340
Takashi Iwai382225e2011-02-22 10:21:18 +0100341 mutex_init(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100342 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;
Oliver Neukum88a85162011-03-11 14:51:12 +0100348 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100349
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 */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100368 if (quirk && quirk->product_name && *quirk->product_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100369 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 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100380 remove_trailing_spaces(card->shortname);
Daniel Macke5779992010-03-04 19:46:13 +0100381
382 /* retrieve the vendor and device strings as longname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100383 if (quirk && quirk->vendor_name && *quirk->vendor_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100384 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 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100393 if (len > 0) {
394 remove_trailing_spaces(card->longname);
395 if (*card->longname)
396 strlcat(card->longname, " ", sizeof(card->longname));
397 }
Daniel Macke5779992010-03-04 19:46:13 +0100398
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
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700406 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 }
Daniel Macke5779992010-03-04 19:46:13 +0100422
423 snd_usb_audio_create_proc(chip);
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800424 switch_set_state(usbaudiosdev, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100425
426 *rchip = chip;
427 return 0;
428}
429
430/*
431 * probe the active usb device
432 *
433 * note that this can be called multiple times per a device, when it
434 * includes multiple audio control interfaces.
435 *
436 * thus we check the usb device pointer and creates the card instance
437 * only at the first time. the successive calls of this function will
438 * append the pcm interface to the corresponding card.
439 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400440static struct snd_usb_audio *
441snd_usb_audio_probe(struct usb_device *dev,
442 struct usb_interface *intf,
443 const struct usb_device_id *usb_id)
Daniel Macke5779992010-03-04 19:46:13 +0100444{
445 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
446 int i, err;
447 struct snd_usb_audio *chip;
448 struct usb_host_interface *alts;
449 int ifnum;
450 u32 id;
451
452 alts = &intf->altsetting[0];
453 ifnum = get_iface_desc(alts)->bInterfaceNumber;
454 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
455 le16_to_cpu(dev->descriptor.idProduct));
456 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
457 goto __err_val;
458
459 if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
460 goto __err_val;
461
462 /*
463 * found a config. now register to ALSA
464 */
465
466 /* check whether it's already registered */
467 chip = NULL;
468 mutex_lock(&register_mutex);
469 for (i = 0; i < SNDRV_CARDS; i++) {
470 if (usb_chip[i] && usb_chip[i]->dev == dev) {
471 if (usb_chip[i]->shutdown) {
472 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
473 goto __error;
474 }
475 chip = usb_chip[i];
Oliver Neukum88a85162011-03-11 14:51:12 +0100476 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100477 break;
478 }
479 }
480 if (! chip) {
481 /* it's a fresh one.
482 * now look for an empty slot and create a new card instance
483 */
484 for (i = 0; i < SNDRV_CARDS; i++)
485 if (enable[i] && ! usb_chip[i] &&
486 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
487 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
488 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
489 goto __error;
490 }
491 snd_card_set_dev(chip->card, &intf->dev);
Oliver Neukum88a85162011-03-11 14:51:12 +0100492 chip->pm_intf = intf;
Daniel Macke5779992010-03-04 19:46:13 +0100493 break;
494 }
495 if (!chip) {
496 printk(KERN_ERR "no available usb audio device\n");
497 goto __error;
498 }
499 }
500
Daniel Mack7b6717e2010-09-02 17:13:15 +0800501 /*
502 * For devices with more than one control interface, we assume the
503 * first contains the audio controls. We might need a more specific
504 * check here in the future.
505 */
506 if (!chip->ctrl_intf)
507 chip->ctrl_intf = alts;
Daniel Mack79f920f2010-05-31 14:51:31 +0200508
Daniel Mack5875c2c2011-05-25 09:08:59 +0200509 chip->txfr_quirk = 0;
510 err = 1; /* continue */
511 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
512 /* need some special handlings */
513 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
514 goto __error;
515 }
516
Daniel Macke5779992010-03-04 19:46:13 +0100517 if (err > 0) {
518 /* create normal USB audio interfaces */
519 if (snd_usb_create_streams(chip, ifnum) < 0 ||
520 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
521 goto __error;
522 }
523 }
524
525 /* we are allowed to call snd_card_register() many times */
526 if (snd_card_register(chip->card) < 0) {
527 goto __error;
528 }
529
530 usb_chip[chip->index] = chip;
531 chip->num_interfaces++;
Oliver Neukum88a85162011-03-11 14:51:12 +0100532 chip->probing = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100533 mutex_unlock(&register_mutex);
534 return chip;
535
536 __error:
Thomas Pfaff61a6a102011-09-26 15:43:59 +0200537 if (chip) {
538 if (!chip->num_interfaces)
539 snd_card_free(chip->card);
540 chip->probing = 0;
541 }
Daniel Macke5779992010-03-04 19:46:13 +0100542 mutex_unlock(&register_mutex);
543 __err_val:
544 return NULL;
545}
546
547/*
548 * we need to take care of counter, since disconnection can be called also
549 * many times as well as usb_audio_probe().
550 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400551static void snd_usb_audio_disconnect(struct usb_device *dev,
552 struct snd_usb_audio *chip)
Daniel Macke5779992010-03-04 19:46:13 +0100553{
Daniel Macke5779992010-03-04 19:46:13 +0100554 struct snd_card *card;
555 struct list_head *p;
556
Pavel Roskin81b85b62011-07-06 11:20:13 -0400557 if (chip == (void *)-1L)
Daniel Macke5779992010-03-04 19:46:13 +0100558 return;
559
Daniel Macke5779992010-03-04 19:46:13 +0100560 card = chip->card;
561 mutex_lock(&register_mutex);
Takashi Iwai382225e2011-02-22 10:21:18 +0100562 mutex_lock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100563 chip->shutdown = 1;
564 chip->num_interfaces--;
565 if (chip->num_interfaces <= 0) {
566 snd_card_disconnect(card);
567 /* release the pcm resources */
568 list_for_each(p, &chip->pcm_list) {
569 snd_usb_stream_disconnect(p);
570 }
571 /* release the midi resources */
572 list_for_each(p, &chip->midi_list) {
573 snd_usbmidi_disconnect(p);
574 }
575 /* release mixer resources */
576 list_for_each(p, &chip->mixer_list) {
577 snd_usb_mixer_disconnect(p);
578 }
579 usb_chip[chip->index] = NULL;
Takashi Iwai382225e2011-02-22 10:21:18 +0100580 mutex_unlock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100581 mutex_unlock(&register_mutex);
582 snd_card_free_when_closed(card);
583 } else {
Takashi Iwai382225e2011-02-22 10:21:18 +0100584 mutex_unlock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100585 mutex_unlock(&register_mutex);
586 }
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800587 switch_set_state(usbaudiosdev, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100588}
589
590/*
591 * new 2.5 USB kernel API
592 */
593static int usb_audio_probe(struct usb_interface *intf,
594 const struct usb_device_id *id)
595{
Pavel Roskin81b85b62011-07-06 11:20:13 -0400596 struct snd_usb_audio *chip;
Daniel Macke5779992010-03-04 19:46:13 +0100597 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
598 if (chip) {
599 usb_set_intfdata(intf, chip);
600 return 0;
601 } else
602 return -EIO;
603}
604
605static void usb_audio_disconnect(struct usb_interface *intf)
606{
607 snd_usb_audio_disconnect(interface_to_usbdev(intf),
608 usb_get_intfdata(intf));
609}
610
611#ifdef CONFIG_PM
Oliver Neukum88a85162011-03-11 14:51:12 +0100612
613int snd_usb_autoresume(struct snd_usb_audio *chip)
614{
615 int err = -ENODEV;
616
617 if (!chip->shutdown && !chip->probing)
618 err = usb_autopm_get_interface(chip->pm_intf);
619
620 return err;
621}
622
623void snd_usb_autosuspend(struct snd_usb_audio *chip)
624{
625 if (!chip->shutdown && !chip->probing)
626 usb_autopm_put_interface(chip->pm_intf);
627}
628
Daniel Macke5779992010-03-04 19:46:13 +0100629static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
630{
631 struct snd_usb_audio *chip = usb_get_intfdata(intf);
632 struct list_head *p;
633 struct snd_usb_stream *as;
Oliver Neukumedf7de32011-03-11 13:19:43 +0100634 struct usb_mixer_interface *mixer;
Daniel Macke5779992010-03-04 19:46:13 +0100635
636 if (chip == (void *)-1L)
637 return 0;
638
Alan Stern5b1b0b82011-08-19 23:49:48 +0200639 if (!PMSG_IS_AUTO(message)) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100640 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
641 if (!chip->num_suspended_intf++) {
642 list_for_each(p, &chip->pcm_list) {
643 as = list_entry(p, struct snd_usb_stream, list);
644 snd_pcm_suspend_all(as->pcm);
645 }
646 }
647 } else {
648 /*
649 * otherwise we keep the rest of the system in the dark
650 * to keep this transparent
651 */
652 if (!chip->num_suspended_intf++)
653 chip->autosuspended = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100654 }
655
Oliver Neukum88a85162011-03-11 14:51:12 +0100656 list_for_each_entry(mixer, &chip->mixer_list, list)
657 snd_usb_mixer_inactivate(mixer);
658
Daniel Macke5779992010-03-04 19:46:13 +0100659 return 0;
660}
661
662static int usb_audio_resume(struct usb_interface *intf)
663{
664 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumedf7de32011-03-11 13:19:43 +0100665 struct usb_mixer_interface *mixer;
Oliver Neukum88a85162011-03-11 14:51:12 +0100666 int err = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100667
668 if (chip == (void *)-1L)
669 return 0;
670 if (--chip->num_suspended_intf)
671 return 0;
672 /*
673 * ALSA leaves material resumption to user space
Oliver Neukumedf7de32011-03-11 13:19:43 +0100674 * we just notify and restart the mixers
Daniel Macke5779992010-03-04 19:46:13 +0100675 */
Oliver Neukum88a85162011-03-11 14:51:12 +0100676 list_for_each_entry(mixer, &chip->mixer_list, list) {
677 err = snd_usb_mixer_activate(mixer);
678 if (err < 0)
679 goto err_out;
680 }
Daniel Macke5779992010-03-04 19:46:13 +0100681
Oliver Neukum88a85162011-03-11 14:51:12 +0100682 if (!chip->autosuspended)
683 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
684 chip->autosuspended = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100685
Oliver Neukum88a85162011-03-11 14:51:12 +0100686err_out:
687 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100688}
Randy Dunlap6407d472010-03-22 08:55:35 -0700689#else
690#define usb_audio_suspend NULL
691#define usb_audio_resume NULL
Daniel Macke5779992010-03-04 19:46:13 +0100692#endif /* CONFIG_PM */
693
694static struct usb_device_id usb_audio_ids [] = {
695#include "quirks-table.h"
696 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
697 .bInterfaceClass = USB_CLASS_AUDIO,
698 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
699 { } /* Terminating entry */
700};
701
702MODULE_DEVICE_TABLE (usb, usb_audio_ids);
703
704/*
705 * entry point for linux usb interface
706 */
707
708static struct usb_driver usb_audio_driver = {
709 .name = "snd-usb-audio",
710 .probe = usb_audio_probe,
711 .disconnect = usb_audio_disconnect,
712 .suspend = usb_audio_suspend,
713 .resume = usb_audio_resume,
714 .id_table = usb_audio_ids,
Oliver Neukum88a85162011-03-11 14:51:12 +0100715 .supports_autosuspend = 1,
Daniel Macke5779992010-03-04 19:46:13 +0100716};
717
718static int __init snd_usb_audio_init(void)
719{
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800720 int err;
Daniel Macke5779992010-03-04 19:46:13 +0100721 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
722 printk(KERN_WARNING "invalid nrpacks value.\n");
723 return -EINVAL;
724 }
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800725
726 usbaudiosdev = kzalloc(sizeof(usbaudiosdev), GFP_KERNEL);
727 usbaudiosdev->name = "usb_audio";
728
729 err = switch_dev_register(usbaudiosdev);
730 if (err)
731 pr_err("Usb-audio switch registration failed\n");
732 else
733 pr_debug("usb hs_detected\n");
Daniel Macke5779992010-03-04 19:46:13 +0100734 return usb_register(&usb_audio_driver);
735}
736
737static void __exit snd_usb_audio_cleanup(void)
738{
739 usb_deregister(&usb_audio_driver);
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800740 kfree(usbaudiosdev);
Daniel Macke5779992010-03-04 19:46:13 +0100741}
742
743module_init(snd_usb_audio_init);
744module_exit(snd_usb_audio_cleanup);