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