blob: 12dfa0a65d5dbf6b88f6b73d37e693a8dfbb1645 [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>
Harmandeep Singh52ca48e2012-03-05 16:38:33 -080050#include <linux/switch.h>
Daniel Macke5779992010-03-04 19:46:13 +010051
Daniel Mack9e386582011-05-25 09:09:01 +020052#include <sound/control.h>
Daniel Macke5779992010-03-04 19:46:13 +010053#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"
Daniel Mackf0b5e632010-03-11 21:13:23 +010062#include "mixer.h"
Daniel Macke5779992010-03-04 19:46:13 +010063#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 "urb.h"
70#include "format.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010071#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010072
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 */
81static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
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 */
86static int async_unlink = 1;
87static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
88static int ignore_ctl_error;
Harmandeep Singh52ca48e2012-03-05 16:38:33 -080089struct switch_dev *usbaudiosdev;
Daniel Macke5779992010-03-04 19:46:13 +010090
91module_param_array(index, int, NULL, 0444);
92MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
93module_param_array(id, charp, NULL, 0444);
94MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
95module_param_array(enable, bool, NULL, 0444);
96MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
97module_param_array(vid, int, NULL, 0444);
98MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
99module_param_array(pid, int, NULL, 0444);
100MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
101module_param(nrpacks, int, 0644);
102MODULE_PARM_DESC(nrpacks, "Max. number of packets per URB.");
103module_param(async_unlink, bool, 0444);
104MODULE_PARM_DESC(async_unlink, "Use async unlink mode.");
105module_param_array(device_setup, int, NULL, 0444);
106MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
107module_param(ignore_ctl_error, bool, 0444);
108MODULE_PARM_DESC(ignore_ctl_error,
109 "Ignore errors from USB controller for mixer interfaces.");
110
111/*
112 * we keep the snd_usb_audio_t instances by ourselves for merging
113 * the all interfaces on the same card as one sound device.
114 */
115
116static DEFINE_MUTEX(register_mutex);
117static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
118static struct usb_driver usb_audio_driver;
119
120/*
121 * disconnect streams
122 * called from snd_usb_audio_disconnect()
123 */
124static void snd_usb_stream_disconnect(struct list_head *head)
125{
126 int idx;
127 struct snd_usb_stream *as;
128 struct snd_usb_substream *subs;
129
130 as = list_entry(head, struct snd_usb_stream, list);
131 for (idx = 0; idx < 2; idx++) {
132 subs = &as->substream[idx];
133 if (!subs->num_formats)
Takashi Iwai76195fb2010-09-08 08:27:02 +0200134 continue;
Daniel Macke5779992010-03-04 19:46:13 +0100135 snd_usb_release_substream_urbs(subs, 1);
136 subs->interface = -1;
137 }
138}
139
140static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
141{
142 struct usb_device *dev = chip->dev;
143 struct usb_host_interface *alts;
144 struct usb_interface_descriptor *altsd;
145 struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
146
147 if (!iface) {
148 snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
149 dev->devnum, ctrlif, interface);
150 return -EINVAL;
151 }
152
153 if (usb_interface_claimed(iface)) {
154 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
155 dev->devnum, ctrlif, interface);
156 return -EINVAL;
157 }
158
159 alts = &iface->altsetting[0];
160 altsd = get_iface_desc(alts);
161 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
162 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
163 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
164 int err = snd_usbmidi_create(chip->card, iface,
165 &chip->midi_list, NULL);
166 if (err < 0) {
167 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
168 dev->devnum, ctrlif, interface);
169 return -EINVAL;
170 }
171 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
172
173 return 0;
174 }
175
176 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
177 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
178 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
179 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
180 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
181 /* skip non-supported classes */
182 return -EINVAL;
183 }
184
185 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
186 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
187 return -EINVAL;
188 }
189
190 if (! snd_usb_parse_audio_endpoints(chip, interface)) {
191 usb_set_interface(dev, interface, 0); /* reset the current interface */
192 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
193 return -EINVAL;
194 }
195
196 return 0;
197}
198
199/*
200 * parse audio control descriptor and create pcm/midi streams
201 */
202static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
203{
204 struct usb_device *dev = chip->dev;
205 struct usb_host_interface *host_iface;
206 struct usb_interface_descriptor *altsd;
207 void *control_header;
208 int i, protocol;
209
210 /* find audiocontrol interface */
211 host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
212 control_header = snd_usb_find_csint_desc(host_iface->extra,
213 host_iface->extralen,
214 NULL, UAC_HEADER);
215 altsd = get_iface_desc(host_iface);
216 protocol = altsd->bInterfaceProtocol;
217
218 if (!control_header) {
219 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
220 return -EINVAL;
221 }
222
223 switch (protocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +0200224 default:
225 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
226 protocol);
227 /* fall through */
228
Daniel Macke5779992010-03-04 19:46:13 +0100229 case UAC_VERSION_1: {
Daniel Mack69da9bc2010-06-16 17:57:28 +0200230 struct uac1_ac_header_descriptor *h1 = control_header;
Daniel Macke5779992010-03-04 19:46:13 +0100231
232 if (!h1->bInCollection) {
233 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
234 return -EINVAL;
235 }
236
237 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
238 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
239 return -EINVAL;
240 }
241
242 for (i = 0; i < h1->bInCollection; i++)
243 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
244
245 break;
246 }
247
248 case UAC_VERSION_2: {
Daniel Macke5779992010-03-04 19:46:13 +0100249 struct usb_interface_assoc_descriptor *assoc =
250 usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
251
252 if (!assoc) {
253 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
254 return -EINVAL;
255 }
256
Daniel Macke5779992010-03-04 19:46:13 +0100257 for (i = 0; i < assoc->bInterfaceCount; i++) {
258 int intf = assoc->bFirstInterface + i;
259
260 if (intf != ctrlif)
261 snd_usb_create_stream(chip, ctrlif, intf);
262 }
263
264 break;
265 }
Daniel Macke5779992010-03-04 19:46:13 +0100266 }
267
268 return 0;
269}
270
271/*
272 * free the chip instance
273 *
274 * here we have to do not much, since pcm and controls are already freed
275 *
276 */
277
278static int snd_usb_audio_free(struct snd_usb_audio *chip)
279{
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
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100290static 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}
Daniel Macke5779992010-03-04 19:46:13 +0100299
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
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700317 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:
Daniel Macke5779992010-03-04 19:46:13 +0100324 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
Takashi Iwai382225e2011-02-22 10:21:18 +0100340 mutex_init(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100341 chip->index = idx;
342 chip->dev = dev;
343 chip->card = card;
344 chip->setup = device_setup[idx];
345 chip->nrpacks = nrpacks;
346 chip->async_unlink = async_unlink;
Oliver Neukum88a85162011-03-11 14:51:12 +0100347 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100348
349 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
350 le16_to_cpu(dev->descriptor.idProduct));
351 INIT_LIST_HEAD(&chip->pcm_list);
352 INIT_LIST_HEAD(&chip->midi_list);
353 INIT_LIST_HEAD(&chip->mixer_list);
354
355 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
356 snd_usb_audio_free(chip);
357 snd_card_free(card);
358 return err;
359 }
360
361 strcpy(card->driver, "USB-Audio");
362 sprintf(component, "USB%04x:%04x",
363 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
364 snd_component_add(card, component);
365
366 /* retrieve the device string as shortname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100367 if (quirk && quirk->product_name && *quirk->product_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100368 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
369 } else {
370 if (!dev->descriptor.iProduct ||
371 usb_string(dev, dev->descriptor.iProduct,
372 card->shortname, sizeof(card->shortname)) <= 0) {
373 /* no name available from anywhere, so use ID */
374 sprintf(card->shortname, "USB Device %#04x:%#04x",
375 USB_ID_VENDOR(chip->usb_id),
376 USB_ID_PRODUCT(chip->usb_id));
377 }
378 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100379 remove_trailing_spaces(card->shortname);
Daniel Macke5779992010-03-04 19:46:13 +0100380
381 /* retrieve the vendor and device strings as longname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100382 if (quirk && quirk->vendor_name && *quirk->vendor_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100383 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
384 } else {
385 if (dev->descriptor.iManufacturer)
386 len = usb_string(dev, dev->descriptor.iManufacturer,
387 card->longname, sizeof(card->longname));
388 else
389 len = 0;
390 /* we don't really care if there isn't any vendor string */
391 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100392 if (len > 0) {
393 remove_trailing_spaces(card->longname);
394 if (*card->longname)
395 strlcat(card->longname, " ", sizeof(card->longname));
396 }
Daniel Macke5779992010-03-04 19:46:13 +0100397
398 strlcat(card->longname, card->shortname, sizeof(card->longname));
399
400 len = strlcat(card->longname, " at ", sizeof(card->longname));
401
402 if (len < sizeof(card->longname))
403 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
404
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700405 switch (snd_usb_get_speed(dev)) {
406 case USB_SPEED_LOW:
407 strlcat(card->longname, ", low speed", sizeof(card->longname));
408 break;
409 case USB_SPEED_FULL:
410 strlcat(card->longname, ", full speed", sizeof(card->longname));
411 break;
412 case USB_SPEED_HIGH:
413 strlcat(card->longname, ", high speed", sizeof(card->longname));
414 break;
415 case USB_SPEED_SUPER:
416 strlcat(card->longname, ", super speed", sizeof(card->longname));
417 break;
418 default:
419 break;
420 }
Daniel Macke5779992010-03-04 19:46:13 +0100421
422 snd_usb_audio_create_proc(chip);
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800423 switch_set_state(usbaudiosdev, 1);
Daniel Macke5779992010-03-04 19:46:13 +0100424
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 */
439static void *snd_usb_audio_probe(struct usb_device *dev,
440 struct usb_interface *intf,
441 const struct usb_device_id *usb_id)
442{
443 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
444 int i, err;
445 struct snd_usb_audio *chip;
446 struct usb_host_interface *alts;
447 int ifnum;
448 u32 id;
449
450 alts = &intf->altsetting[0];
451 ifnum = get_iface_desc(alts)->bInterfaceNumber;
452 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
453 le16_to_cpu(dev->descriptor.idProduct));
454 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
455 goto __err_val;
456
457 if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
458 goto __err_val;
459
460 /*
461 * found a config. now register to ALSA
462 */
463
464 /* check whether it's already registered */
465 chip = NULL;
466 mutex_lock(&register_mutex);
467 for (i = 0; i < SNDRV_CARDS; i++) {
468 if (usb_chip[i] && usb_chip[i]->dev == dev) {
469 if (usb_chip[i]->shutdown) {
470 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
471 goto __error;
472 }
473 chip = usb_chip[i];
Oliver Neukum88a85162011-03-11 14:51:12 +0100474 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100475 break;
476 }
477 }
478 if (! chip) {
479 /* it's a fresh one.
480 * now look for an empty slot and create a new card instance
481 */
482 for (i = 0; i < SNDRV_CARDS; i++)
483 if (enable[i] && ! usb_chip[i] &&
484 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
485 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
486 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
487 goto __error;
488 }
489 snd_card_set_dev(chip->card, &intf->dev);
Oliver Neukum88a85162011-03-11 14:51:12 +0100490 chip->pm_intf = intf;
Daniel Macke5779992010-03-04 19:46:13 +0100491 break;
492 }
493 if (!chip) {
494 printk(KERN_ERR "no available usb audio device\n");
495 goto __error;
496 }
497 }
498
Daniel Mack7b6717e2010-09-02 17:13:15 +0800499 /*
500 * For devices with more than one control interface, we assume the
501 * first contains the audio controls. We might need a more specific
502 * check here in the future.
503 */
504 if (!chip->ctrl_intf)
505 chip->ctrl_intf = alts;
Daniel Mack79f920f2010-05-31 14:51:31 +0200506
Daniel Mack5875c2c2011-05-25 09:08:59 +0200507 chip->txfr_quirk = 0;
508 err = 1; /* continue */
509 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
510 /* need some special handlings */
511 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
512 goto __error;
513 }
514
Daniel Macke5779992010-03-04 19:46:13 +0100515 if (err > 0) {
516 /* create normal USB audio interfaces */
517 if (snd_usb_create_streams(chip, ifnum) < 0 ||
518 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
519 goto __error;
520 }
521 }
522
523 /* we are allowed to call snd_card_register() many times */
524 if (snd_card_register(chip->card) < 0) {
525 goto __error;
526 }
527
528 usb_chip[chip->index] = chip;
529 chip->num_interfaces++;
Oliver Neukum88a85162011-03-11 14:51:12 +0100530 chip->probing = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100531 mutex_unlock(&register_mutex);
532 return chip;
533
534 __error:
Thomas Pfaff81aac2e2011-09-26 15:43:59 +0200535 if (chip) {
536 if (!chip->num_interfaces)
537 snd_card_free(chip->card);
538 chip->probing = 0;
539 }
Daniel Macke5779992010-03-04 19:46:13 +0100540 mutex_unlock(&register_mutex);
541 __err_val:
542 return NULL;
543}
544
545/*
546 * we need to take care of counter, since disconnection can be called also
547 * many times as well as usb_audio_probe().
548 */
549static void snd_usb_audio_disconnect(struct usb_device *dev, void *ptr)
550{
551 struct snd_usb_audio *chip;
552 struct snd_card *card;
553 struct list_head *p;
554
555 if (ptr == (void *)-1L)
556 return;
557
558 chip = ptr;
559 card = chip->card;
560 mutex_lock(&register_mutex);
Takashi Iwai382225e2011-02-22 10:21:18 +0100561 mutex_lock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100562 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;
Takashi Iwai382225e2011-02-22 10:21:18 +0100579 mutex_unlock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100580 mutex_unlock(&register_mutex);
581 snd_card_free_when_closed(card);
582 } else {
Takashi Iwai382225e2011-02-22 10:21:18 +0100583 mutex_unlock(&chip->shutdown_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100584 mutex_unlock(&register_mutex);
585 }
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800586 switch_set_state(usbaudiosdev, 0);
Daniel Macke5779992010-03-04 19:46:13 +0100587}
588
589/*
590 * new 2.5 USB kernel API
591 */
592static int usb_audio_probe(struct usb_interface *intf,
593 const struct usb_device_id *id)
594{
595 void *chip;
596 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
597 if (chip) {
598 usb_set_intfdata(intf, chip);
599 return 0;
600 } else
601 return -EIO;
602}
603
604static void usb_audio_disconnect(struct usb_interface *intf)
605{
606 snd_usb_audio_disconnect(interface_to_usbdev(intf),
607 usb_get_intfdata(intf));
608}
609
610#ifdef CONFIG_PM
Oliver Neukum88a85162011-03-11 14:51:12 +0100611
612int snd_usb_autoresume(struct snd_usb_audio *chip)
613{
614 int err = -ENODEV;
615
616 if (!chip->shutdown && !chip->probing)
617 err = usb_autopm_get_interface(chip->pm_intf);
618
619 return err;
620}
621
622void snd_usb_autosuspend(struct snd_usb_audio *chip)
623{
624 if (!chip->shutdown && !chip->probing)
625 usb_autopm_put_interface(chip->pm_intf);
626}
627
Daniel Macke5779992010-03-04 19:46:13 +0100628static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
629{
630 struct snd_usb_audio *chip = usb_get_intfdata(intf);
631 struct list_head *p;
632 struct snd_usb_stream *as;
Oliver Neukumedf7de32011-03-11 13:19:43 +0100633 struct usb_mixer_interface *mixer;
Daniel Macke5779992010-03-04 19:46:13 +0100634
635 if (chip == (void *)-1L)
636 return 0;
637
Oliver Neukum88a85162011-03-11 14:51:12 +0100638 if (!(message.event & PM_EVENT_AUTO)) {
639 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
640 if (!chip->num_suspended_intf++) {
641 list_for_each(p, &chip->pcm_list) {
642 as = list_entry(p, struct snd_usb_stream, list);
643 snd_pcm_suspend_all(as->pcm);
644 }
645 }
646 } else {
647 /*
648 * otherwise we keep the rest of the system in the dark
649 * to keep this transparent
650 */
651 if (!chip->num_suspended_intf++)
652 chip->autosuspended = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100653 }
654
Oliver Neukum88a85162011-03-11 14:51:12 +0100655 list_for_each_entry(mixer, &chip->mixer_list, list)
656 snd_usb_mixer_inactivate(mixer);
657
Daniel Macke5779992010-03-04 19:46:13 +0100658 return 0;
659}
660
661static int usb_audio_resume(struct usb_interface *intf)
662{
663 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumedf7de32011-03-11 13:19:43 +0100664 struct usb_mixer_interface *mixer;
Oliver Neukum88a85162011-03-11 14:51:12 +0100665 int err = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100666
667 if (chip == (void *)-1L)
668 return 0;
669 if (--chip->num_suspended_intf)
670 return 0;
671 /*
672 * ALSA leaves material resumption to user space
Oliver Neukumedf7de32011-03-11 13:19:43 +0100673 * we just notify and restart the mixers
Daniel Macke5779992010-03-04 19:46:13 +0100674 */
Oliver Neukum88a85162011-03-11 14:51:12 +0100675 list_for_each_entry(mixer, &chip->mixer_list, list) {
676 err = snd_usb_mixer_activate(mixer);
677 if (err < 0)
678 goto err_out;
679 }
Daniel Macke5779992010-03-04 19:46:13 +0100680
Oliver Neukum88a85162011-03-11 14:51:12 +0100681 if (!chip->autosuspended)
682 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
683 chip->autosuspended = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100684
Oliver Neukum88a85162011-03-11 14:51:12 +0100685err_out:
686 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100687}
Randy Dunlap6407d472010-03-22 08:55:35 -0700688#else
689#define usb_audio_suspend NULL
690#define usb_audio_resume NULL
Daniel Macke5779992010-03-04 19:46:13 +0100691#endif /* CONFIG_PM */
692
693static struct usb_device_id usb_audio_ids [] = {
694#include "quirks-table.h"
695 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
696 .bInterfaceClass = USB_CLASS_AUDIO,
697 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
698 { } /* Terminating entry */
699};
700
701MODULE_DEVICE_TABLE (usb, usb_audio_ids);
702
703/*
704 * entry point for linux usb interface
705 */
706
707static struct usb_driver usb_audio_driver = {
708 .name = "snd-usb-audio",
709 .probe = usb_audio_probe,
710 .disconnect = usb_audio_disconnect,
711 .suspend = usb_audio_suspend,
712 .resume = usb_audio_resume,
713 .id_table = usb_audio_ids,
Oliver Neukum88a85162011-03-11 14:51:12 +0100714 .supports_autosuspend = 1,
Daniel Macke5779992010-03-04 19:46:13 +0100715};
716
717static int __init snd_usb_audio_init(void)
718{
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800719 int err;
Daniel Macke5779992010-03-04 19:46:13 +0100720 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
721 printk(KERN_WARNING "invalid nrpacks value.\n");
722 return -EINVAL;
723 }
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800724
725 usbaudiosdev = kzalloc(sizeof(usbaudiosdev), GFP_KERNEL);
726 usbaudiosdev->name = "usb_audio";
727
728 err = switch_dev_register(usbaudiosdev);
729 if (err)
730 pr_err("Usb-audio switch registration failed\n");
731 else
732 pr_debug("usb hs_detected\n");
Daniel Macke5779992010-03-04 19:46:13 +0100733 return usb_register(&usb_audio_driver);
734}
735
736static void __exit snd_usb_audio_cleanup(void)
737{
738 usb_deregister(&usb_audio_driver);
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800739 kfree(usbaudiosdev);
Daniel Macke5779992010-03-04 19:46:13 +0100740}
741
742module_init(snd_usb_audio_init);
743module_exit(snd_usb_audio_cleanup);