blob: 27a644cc80fb614c89e92ec4a962d741b6ca1109 [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>
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"
Daniel Macke5779992010-03-04 19:46:13 +010069#include "format.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010070#include "power.h"
Daniel Macke8e8bab2011-09-12 18:54:12 +020071#include "stream.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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103081static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
Daniel Macke5779992010-03-04 19:46:13 +010082/* 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 */
Rusty Russella67ff6a2011-12-15 13:49:36 +103086static bool async_unlink = 1;
Daniel Macke5779992010-03-04 19:46:13 +010087static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103088static bool ignore_ctl_error;
Daniel Macke5779992010-03-04 19:46:13 +010089
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)
Takashi Iwai76195fb2010-09-08 08:27:02 +0200133 continue;
Daniel Macke5779992010-03-04 19:46:13 +0100134 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
Clemens Ladisch43dd0d42013-06-15 11:21:09 +0200152 alts = &iface->altsetting[0];
153 altsd = get_iface_desc(alts);
154
155 /*
156 * Android with both accessory and audio interfaces enabled gets the
157 * interface numbers wrong.
158 */
159 if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
160 chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
161 interface == 0 &&
162 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
163 altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
164 interface = 2;
165 iface = usb_ifnum_to_if(dev, interface);
166 if (!iface)
167 return -EINVAL;
168 alts = &iface->altsetting[0];
169 altsd = get_iface_desc(alts);
170 }
171
Daniel Macke5779992010-03-04 19:46:13 +0100172 if (usb_interface_claimed(iface)) {
173 snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
174 dev->devnum, ctrlif, interface);
175 return -EINVAL;
176 }
177
Daniel Macke5779992010-03-04 19:46:13 +0100178 if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
179 altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
180 altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
181 int err = snd_usbmidi_create(chip->card, iface,
182 &chip->midi_list, NULL);
183 if (err < 0) {
184 snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
185 dev->devnum, ctrlif, interface);
186 return -EINVAL;
187 }
188 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
189
190 return 0;
191 }
192
193 if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
194 altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
195 altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
196 snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
197 dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
198 /* skip non-supported classes */
199 return -EINVAL;
200 }
201
202 if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
203 snd_printk(KERN_ERR "low speed audio streaming not supported\n");
204 return -EINVAL;
205 }
206
Daniel Macke8e8bab2011-09-12 18:54:12 +0200207 if (! snd_usb_parse_audio_interface(chip, interface)) {
Daniel Macke5779992010-03-04 19:46:13 +0100208 usb_set_interface(dev, interface, 0); /* reset the current interface */
209 usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
210 return -EINVAL;
211 }
212
213 return 0;
214}
215
216/*
217 * parse audio control descriptor and create pcm/midi streams
218 */
219static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
220{
221 struct usb_device *dev = chip->dev;
222 struct usb_host_interface *host_iface;
223 struct usb_interface_descriptor *altsd;
Mayank Ranae369f452013-10-18 14:46:36 +0530224 struct usb_interface *usb_iface;
Daniel Macke5779992010-03-04 19:46:13 +0100225 void *control_header;
226 int i, protocol;
Takashi Iwai98e26022017-09-22 16:18:53 +0200227 int rest_bytes;
Daniel Macke5779992010-03-04 19:46:13 +0100228
Mayank Ranae369f452013-10-18 14:46:36 +0530229 usb_iface = usb_ifnum_to_if(dev, ctrlif);
230 if (!usb_iface) {
231 snd_printk(KERN_ERR "%d:%u : does not exist\n",
232 dev->devnum, ctrlif);
233 return -EINVAL;
234 }
235
Daniel Macke5779992010-03-04 19:46:13 +0100236 /* find audiocontrol interface */
Mayank Ranae369f452013-10-18 14:46:36 +0530237 host_iface = &usb_iface->altsetting[0];
238 if (!host_iface) {
239 snd_printk(KERN_ERR "Audio Control interface is not available.");
240 return -EINVAL;
241 }
242
Daniel Macke5779992010-03-04 19:46:13 +0100243 control_header = snd_usb_find_csint_desc(host_iface->extra,
244 host_iface->extralen,
245 NULL, UAC_HEADER);
246 altsd = get_iface_desc(host_iface);
247 protocol = altsd->bInterfaceProtocol;
248
249 if (!control_header) {
250 snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
251 return -EINVAL;
252 }
253
Takashi Iwai98e26022017-09-22 16:18:53 +0200254 rest_bytes = (void *)(host_iface->extra + host_iface->extralen) -
255 control_header;
256
257 /* just to be sure -- this shouldn't hit at all */
258 if (rest_bytes <= 0) {
259 dev_err(&dev->dev, "invalid control header\n");
260 return -EINVAL;
261 }
262
Daniel Macke5779992010-03-04 19:46:13 +0100263 switch (protocol) {
Clemens Ladischa2acad82010-09-03 10:53:11 +0200264 default:
265 snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
266 protocol);
267 /* fall through */
268
Daniel Macke5779992010-03-04 19:46:13 +0100269 case UAC_VERSION_1: {
Daniel Mack69da9bc2010-06-16 17:57:28 +0200270 struct uac1_ac_header_descriptor *h1 = control_header;
Daniel Macke5779992010-03-04 19:46:13 +0100271
Takashi Iwai98e26022017-09-22 16:18:53 +0200272 if (rest_bytes < sizeof(*h1)) {
273 dev_err(&dev->dev, "too short v1 buffer descriptor\n");
274 return -EINVAL;
275 }
276
Daniel Macke5779992010-03-04 19:46:13 +0100277 if (!h1->bInCollection) {
278 snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
279 return -EINVAL;
280 }
281
Takashi Iwai98e26022017-09-22 16:18:53 +0200282 if (rest_bytes < h1->bLength) {
283 dev_err(&dev->dev, "invalid buffer length (v1)\n");
284 return -EINVAL;
285 }
286
Daniel Macke5779992010-03-04 19:46:13 +0100287 if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
288 snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
289 return -EINVAL;
290 }
291
292 for (i = 0; i < h1->bInCollection; i++)
293 snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
294
295 break;
296 }
297
298 case UAC_VERSION_2: {
Daniel Macke5779992010-03-04 19:46:13 +0100299 struct usb_interface_assoc_descriptor *assoc =
Mayank Ranae369f452013-10-18 14:46:36 +0530300 usb_iface->intf_assoc;
Daniel Macke5779992010-03-04 19:46:13 +0100301 if (!assoc) {
302 snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
303 return -EINVAL;
304 }
305
Daniel Macke5779992010-03-04 19:46:13 +0100306 for (i = 0; i < assoc->bInterfaceCount; i++) {
307 int intf = assoc->bFirstInterface + i;
308
309 if (intf != ctrlif)
310 snd_usb_create_stream(chip, ctrlif, intf);
311 }
312
313 break;
314 }
Daniel Macke5779992010-03-04 19:46:13 +0100315 }
Daniel Macke5779992010-03-04 19:46:13 +0100316 return 0;
317}
318
319/*
320 * free the chip instance
321 *
322 * here we have to do not much, since pcm and controls are already freed
323 *
324 */
325
326static int snd_usb_audio_free(struct snd_usb_audio *chip)
327{
328 kfree(chip);
329 return 0;
330}
331
332static int snd_usb_audio_dev_free(struct snd_device *device)
333{
334 struct snd_usb_audio *chip = device->device_data;
335 return snd_usb_audio_free(chip);
336}
337
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100338static void remove_trailing_spaces(char *str)
339{
340 char *p;
341
342 if (!*str)
343 return;
344 for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
345 *p = 0;
346}
Daniel Macke5779992010-03-04 19:46:13 +0100347
348/*
349 * create a chip instance and set its names.
350 */
351static int snd_usb_audio_create(struct usb_device *dev, int idx,
352 const struct snd_usb_audio_quirk *quirk,
353 struct snd_usb_audio **rchip)
354{
355 struct snd_card *card;
356 struct snd_usb_audio *chip;
357 int err, len;
358 char component[14];
359 static struct snd_device_ops ops = {
360 .dev_free = snd_usb_audio_dev_free,
361 };
362
363 *rchip = NULL;
364
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700365 switch (snd_usb_get_speed(dev)) {
366 case USB_SPEED_LOW:
367 case USB_SPEED_FULL:
368 case USB_SPEED_HIGH:
369 case USB_SPEED_SUPER:
370 break;
371 default:
Daniel Macke5779992010-03-04 19:46:13 +0100372 snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
373 return -ENXIO;
374 }
375
376 err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
377 if (err < 0) {
378 snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
379 return err;
380 }
381
382 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
383 if (! chip) {
384 snd_card_free(card);
385 return -ENOMEM;
386 }
387
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100388 init_rwsem(&chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100389 chip->index = idx;
390 chip->dev = dev;
391 chip->card = card;
392 chip->setup = device_setup[idx];
393 chip->nrpacks = nrpacks;
394 chip->async_unlink = async_unlink;
Oliver Neukum88a85162011-03-11 14:51:12 +0100395 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100396
397 chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
398 le16_to_cpu(dev->descriptor.idProduct));
399 INIT_LIST_HEAD(&chip->pcm_list);
400 INIT_LIST_HEAD(&chip->midi_list);
401 INIT_LIST_HEAD(&chip->mixer_list);
402
403 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
404 snd_usb_audio_free(chip);
405 snd_card_free(card);
406 return err;
407 }
408
409 strcpy(card->driver, "USB-Audio");
410 sprintf(component, "USB%04x:%04x",
411 USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
412 snd_component_add(card, component);
413
414 /* retrieve the device string as shortname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100415 if (quirk && quirk->product_name && *quirk->product_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100416 strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
417 } else {
418 if (!dev->descriptor.iProduct ||
419 usb_string(dev, dev->descriptor.iProduct,
420 card->shortname, sizeof(card->shortname)) <= 0) {
421 /* no name available from anywhere, so use ID */
422 sprintf(card->shortname, "USB Device %#04x:%#04x",
423 USB_ID_VENDOR(chip->usb_id),
424 USB_ID_PRODUCT(chip->usb_id));
425 }
426 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100427 remove_trailing_spaces(card->shortname);
Daniel Macke5779992010-03-04 19:46:13 +0100428
429 /* retrieve the vendor and device strings as longname */
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100430 if (quirk && quirk->vendor_name && *quirk->vendor_name) {
Daniel Macke5779992010-03-04 19:46:13 +0100431 len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
432 } else {
433 if (dev->descriptor.iManufacturer)
434 len = usb_string(dev, dev->descriptor.iManufacturer,
435 card->longname, sizeof(card->longname));
436 else
437 len = 0;
438 /* we don't really care if there isn't any vendor string */
439 }
Takashi Iwai3ffc1222011-03-21 12:00:00 +0100440 if (len > 0) {
441 remove_trailing_spaces(card->longname);
442 if (*card->longname)
443 strlcat(card->longname, " ", sizeof(card->longname));
444 }
Daniel Macke5779992010-03-04 19:46:13 +0100445
446 strlcat(card->longname, card->shortname, sizeof(card->longname));
447
448 len = strlcat(card->longname, " at ", sizeof(card->longname));
449
450 if (len < sizeof(card->longname))
451 usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
452
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700453 switch (snd_usb_get_speed(dev)) {
454 case USB_SPEED_LOW:
455 strlcat(card->longname, ", low speed", sizeof(card->longname));
456 break;
457 case USB_SPEED_FULL:
458 strlcat(card->longname, ", full speed", sizeof(card->longname));
459 break;
460 case USB_SPEED_HIGH:
461 strlcat(card->longname, ", high speed", sizeof(card->longname));
462 break;
463 case USB_SPEED_SUPER:
464 strlcat(card->longname, ", super speed", sizeof(card->longname));
465 break;
466 default:
467 break;
468 }
Daniel Macke5779992010-03-04 19:46:13 +0100469
470 snd_usb_audio_create_proc(chip);
471
472 *rchip = chip;
473 return 0;
474}
475
476/*
477 * probe the active usb device
478 *
479 * note that this can be called multiple times per a device, when it
480 * includes multiple audio control interfaces.
481 *
482 * thus we check the usb device pointer and creates the card instance
483 * only at the first time. the successive calls of this function will
484 * append the pcm interface to the corresponding card.
485 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400486static struct snd_usb_audio *
487snd_usb_audio_probe(struct usb_device *dev,
488 struct usb_interface *intf,
489 const struct usb_device_id *usb_id)
Daniel Macke5779992010-03-04 19:46:13 +0100490{
491 const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
492 int i, err;
493 struct snd_usb_audio *chip;
494 struct usb_host_interface *alts;
495 int ifnum;
496 u32 id;
497
498 alts = &intf->altsetting[0];
499 ifnum = get_iface_desc(alts)->bInterfaceNumber;
500 id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
501 le16_to_cpu(dev->descriptor.idProduct));
502 if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
503 goto __err_val;
504
505 if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
506 goto __err_val;
507
508 /*
509 * found a config. now register to ALSA
510 */
511
512 /* check whether it's already registered */
513 chip = NULL;
514 mutex_lock(&register_mutex);
515 for (i = 0; i < SNDRV_CARDS; i++) {
516 if (usb_chip[i] && usb_chip[i]->dev == dev) {
517 if (usb_chip[i]->shutdown) {
518 snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
519 goto __error;
520 }
521 chip = usb_chip[i];
Oliver Neukum88a85162011-03-11 14:51:12 +0100522 chip->probing = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100523 break;
524 }
525 }
526 if (! chip) {
527 /* it's a fresh one.
528 * now look for an empty slot and create a new card instance
529 */
530 for (i = 0; i < SNDRV_CARDS; i++)
531 if (enable[i] && ! usb_chip[i] &&
532 (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
533 (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
534 if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
535 goto __error;
536 }
537 snd_card_set_dev(chip->card, &intf->dev);
Oliver Neukum88a85162011-03-11 14:51:12 +0100538 chip->pm_intf = intf;
Daniel Macke5779992010-03-04 19:46:13 +0100539 break;
540 }
541 if (!chip) {
542 printk(KERN_ERR "no available usb audio device\n");
543 goto __error;
544 }
545 }
546
Daniel Mack7b6717e2010-09-02 17:13:15 +0800547 /*
548 * For devices with more than one control interface, we assume the
549 * first contains the audio controls. We might need a more specific
550 * check here in the future.
551 */
552 if (!chip->ctrl_intf)
553 chip->ctrl_intf = alts;
Daniel Mack79f920f2010-05-31 14:51:31 +0200554
Daniel Mack5875c2c2011-05-25 09:08:59 +0200555 chip->txfr_quirk = 0;
556 err = 1; /* continue */
557 if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
558 /* need some special handlings */
559 if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
560 goto __error;
561 }
562
Daniel Macke5779992010-03-04 19:46:13 +0100563 if (err > 0) {
564 /* create normal USB audio interfaces */
565 if (snd_usb_create_streams(chip, ifnum) < 0 ||
566 snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
567 goto __error;
568 }
569 }
570
571 /* we are allowed to call snd_card_register() many times */
572 if (snd_card_register(chip->card) < 0) {
573 goto __error;
574 }
575
576 usb_chip[chip->index] = chip;
577 chip->num_interfaces++;
Oliver Neukum88a85162011-03-11 14:51:12 +0100578 chip->probing = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100579 mutex_unlock(&register_mutex);
580 return chip;
581
582 __error:
Thomas Pfaff61a6a102011-09-26 15:43:59 +0200583 if (chip) {
584 if (!chip->num_interfaces)
585 snd_card_free(chip->card);
586 chip->probing = 0;
587 }
Daniel Macke5779992010-03-04 19:46:13 +0100588 mutex_unlock(&register_mutex);
589 __err_val:
590 return NULL;
591}
592
593/*
594 * we need to take care of counter, since disconnection can be called also
595 * many times as well as usb_audio_probe().
596 */
Pavel Roskin81b85b62011-07-06 11:20:13 -0400597static void snd_usb_audio_disconnect(struct usb_device *dev,
598 struct snd_usb_audio *chip)
Daniel Macke5779992010-03-04 19:46:13 +0100599{
Daniel Macke5779992010-03-04 19:46:13 +0100600 struct snd_card *card;
601 struct list_head *p;
602
Pavel Roskin81b85b62011-07-06 11:20:13 -0400603 if (chip == (void *)-1L)
Daniel Macke5779992010-03-04 19:46:13 +0100604 return;
605
Daniel Macke5779992010-03-04 19:46:13 +0100606 card = chip->card;
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100607 down_write(&chip->shutdown_rwsem);
Daniel Macke5779992010-03-04 19:46:13 +0100608 chip->shutdown = 1;
Takashi Iwaifd496872012-11-13 11:22:48 +0100609 up_write(&chip->shutdown_rwsem);
610
611 mutex_lock(&register_mutex);
Daniel Macke5779992010-03-04 19:46:13 +0100612 chip->num_interfaces--;
613 if (chip->num_interfaces <= 0) {
614 snd_card_disconnect(card);
615 /* release the pcm resources */
616 list_for_each(p, &chip->pcm_list) {
617 snd_usb_stream_disconnect(p);
618 }
619 /* release the midi resources */
620 list_for_each(p, &chip->midi_list) {
621 snd_usbmidi_disconnect(p);
622 }
623 /* release mixer resources */
624 list_for_each(p, &chip->mixer_list) {
625 snd_usb_mixer_disconnect(p);
626 }
627 usb_chip[chip->index] = NULL;
628 mutex_unlock(&register_mutex);
629 snd_card_free_when_closed(card);
630 } else {
631 mutex_unlock(&register_mutex);
632 }
633}
634
635/*
636 * new 2.5 USB kernel API
637 */
638static int usb_audio_probe(struct usb_interface *intf,
639 const struct usb_device_id *id)
640{
Pavel Roskin81b85b62011-07-06 11:20:13 -0400641 struct snd_usb_audio *chip;
Daniel Macke5779992010-03-04 19:46:13 +0100642 chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
643 if (chip) {
644 usb_set_intfdata(intf, chip);
645 return 0;
646 } else
647 return -EIO;
648}
649
650static void usb_audio_disconnect(struct usb_interface *intf)
651{
652 snd_usb_audio_disconnect(interface_to_usbdev(intf),
653 usb_get_intfdata(intf));
654}
655
656#ifdef CONFIG_PM
Oliver Neukum88a85162011-03-11 14:51:12 +0100657
658int snd_usb_autoresume(struct snd_usb_audio *chip)
659{
660 int err = -ENODEV;
661
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100662 down_read(&chip->shutdown_rwsem);
Takashi Iwaief71c222013-04-25 07:38:15 +0200663 if (chip->probing)
664 err = 0;
665 else if (!chip->shutdown)
Oliver Neukum88a85162011-03-11 14:51:12 +0100666 err = usb_autopm_get_interface(chip->pm_intf);
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100667 up_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100668
669 return err;
670}
671
672void snd_usb_autosuspend(struct snd_usb_audio *chip)
673{
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100674 down_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100675 if (!chip->shutdown && !chip->probing)
676 usb_autopm_put_interface(chip->pm_intf);
Takashi Iwaicd3ad242012-11-07 12:42:45 +0100677 up_read(&chip->shutdown_rwsem);
Oliver Neukum88a85162011-03-11 14:51:12 +0100678}
679
Daniel Macke5779992010-03-04 19:46:13 +0100680static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
681{
682 struct snd_usb_audio *chip = usb_get_intfdata(intf);
683 struct list_head *p;
684 struct snd_usb_stream *as;
Oliver Neukumedf7de32011-03-11 13:19:43 +0100685 struct usb_mixer_interface *mixer;
Daniel Macke5779992010-03-04 19:46:13 +0100686
687 if (chip == (void *)-1L)
688 return 0;
689
Alan Stern5b1b0b82011-08-19 23:49:48 +0200690 if (!PMSG_IS_AUTO(message)) {
Oliver Neukum88a85162011-03-11 14:51:12 +0100691 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
692 if (!chip->num_suspended_intf++) {
693 list_for_each(p, &chip->pcm_list) {
694 as = list_entry(p, struct snd_usb_stream, list);
695 snd_pcm_suspend_all(as->pcm);
696 }
697 }
698 } else {
699 /*
700 * otherwise we keep the rest of the system in the dark
701 * to keep this transparent
702 */
703 if (!chip->num_suspended_intf++)
704 chip->autosuspended = 1;
Daniel Macke5779992010-03-04 19:46:13 +0100705 }
706
Oliver Neukum88a85162011-03-11 14:51:12 +0100707 list_for_each_entry(mixer, &chip->mixer_list, list)
708 snd_usb_mixer_inactivate(mixer);
709
Daniel Macke5779992010-03-04 19:46:13 +0100710 return 0;
711}
712
713static int usb_audio_resume(struct usb_interface *intf)
714{
715 struct snd_usb_audio *chip = usb_get_intfdata(intf);
Oliver Neukumedf7de32011-03-11 13:19:43 +0100716 struct usb_mixer_interface *mixer;
Oliver Neukum88a85162011-03-11 14:51:12 +0100717 int err = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100718
719 if (chip == (void *)-1L)
720 return 0;
721 if (--chip->num_suspended_intf)
722 return 0;
723 /*
724 * ALSA leaves material resumption to user space
Oliver Neukumedf7de32011-03-11 13:19:43 +0100725 * we just notify and restart the mixers
Daniel Macke5779992010-03-04 19:46:13 +0100726 */
Oliver Neukum88a85162011-03-11 14:51:12 +0100727 list_for_each_entry(mixer, &chip->mixer_list, list) {
728 err = snd_usb_mixer_activate(mixer);
729 if (err < 0)
730 goto err_out;
731 }
Daniel Macke5779992010-03-04 19:46:13 +0100732
Oliver Neukum88a85162011-03-11 14:51:12 +0100733 if (!chip->autosuspended)
734 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
735 chip->autosuspended = 0;
Daniel Macke5779992010-03-04 19:46:13 +0100736
Oliver Neukum88a85162011-03-11 14:51:12 +0100737err_out:
738 return err;
Daniel Macke5779992010-03-04 19:46:13 +0100739}
Randy Dunlap6407d472010-03-22 08:55:35 -0700740#else
741#define usb_audio_suspend NULL
742#define usb_audio_resume NULL
Daniel Macke5779992010-03-04 19:46:13 +0100743#endif /* CONFIG_PM */
744
745static struct usb_device_id usb_audio_ids [] = {
746#include "quirks-table.h"
747 { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
748 .bInterfaceClass = USB_CLASS_AUDIO,
749 .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
750 { } /* Terminating entry */
751};
752
753MODULE_DEVICE_TABLE (usb, usb_audio_ids);
754
755/*
756 * entry point for linux usb interface
757 */
758
759static struct usb_driver usb_audio_driver = {
760 .name = "snd-usb-audio",
761 .probe = usb_audio_probe,
762 .disconnect = usb_audio_disconnect,
763 .suspend = usb_audio_suspend,
764 .resume = usb_audio_resume,
765 .id_table = usb_audio_ids,
Oliver Neukum88a85162011-03-11 14:51:12 +0100766 .supports_autosuspend = 1,
Daniel Macke5779992010-03-04 19:46:13 +0100767};
768
769static int __init snd_usb_audio_init(void)
770{
771 if (nrpacks < 1 || nrpacks > MAX_PACKS) {
772 printk(KERN_WARNING "invalid nrpacks value.\n");
773 return -EINVAL;
774 }
Harmandeep Singh52ca48e2012-03-05 16:38:33 -0800775
Daniel Macke5779992010-03-04 19:46:13 +0100776 return usb_register(&usb_audio_driver);
777}
778
779static void __exit snd_usb_audio_cleanup(void)
780{
781 usb_deregister(&usb_audio_driver);
Daniel Macke5779992010-03-04 19:46:13 +0100782}
783
784module_init(snd_usb_audio_init);
785module_exit(snd_usb_audio_cleanup);