card.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Many codes borrowed from audio.c by
  7. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  8. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  9. *
  10. * Audio Class 3.0 support by Ruslan Bilovol <ruslan.bilovol@gmail.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. *
  27. * NOTES:
  28. *
  29. * - the linked URBs would be preferred but not used so far because of
  30. * the instability of unlinking.
  31. * - type II is not supported properly. there is no device which supports
  32. * this type *correctly*. SB extigy looks as if it supports, but it's
  33. * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
  34. */
  35. #include <linux/bitops.h>
  36. #include <linux/init.h>
  37. #include <linux/list.h>
  38. #include <linux/slab.h>
  39. #include <linux/string.h>
  40. #include <linux/ctype.h>
  41. #include <linux/usb.h>
  42. #include <linux/moduleparam.h>
  43. #include <linux/mutex.h>
  44. #include <linux/usb/audio.h>
  45. #include <linux/usb/audio-v2.h>
  46. #include <linux/usb/audio-v3.h>
  47. #include <linux/module.h>
  48. #include <sound/control.h>
  49. #include <sound/core.h>
  50. #include <sound/info.h>
  51. #include <sound/pcm.h>
  52. #include <sound/pcm_params.h>
  53. #include <sound/initval.h>
  54. #include "usbaudio.h"
  55. #include "card.h"
  56. #include "midi.h"
  57. #include "mixer.h"
  58. #include "proc.h"
  59. #include "quirks.h"
  60. #include "endpoint.h"
  61. #include "helper.h"
  62. #include "debug.h"
  63. #include "pcm.h"
  64. #include "format.h"
  65. #include "power.h"
  66. #include "stream.h"
  67. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  68. MODULE_DESCRIPTION("USB Audio");
  69. MODULE_LICENSE("GPL");
  70. MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
  71. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  72. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  73. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
  74. /* Vendor/product IDs for this card */
  75. static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  76. static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  77. static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
  78. static bool ignore_ctl_error;
  79. static bool autoclock = true;
  80. static char *quirk_alias[SNDRV_CARDS];
  81. bool snd_usb_use_vmalloc = true;
  82. module_param_array(index, int, NULL, 0444);
  83. MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
  84. module_param_array(id, charp, NULL, 0444);
  85. MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
  86. module_param_array(enable, bool, NULL, 0444);
  87. MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
  88. module_param_array(vid, int, NULL, 0444);
  89. MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
  90. module_param_array(pid, int, NULL, 0444);
  91. MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
  92. module_param_array(device_setup, int, NULL, 0444);
  93. MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
  94. module_param(ignore_ctl_error, bool, 0444);
  95. MODULE_PARM_DESC(ignore_ctl_error,
  96. "Ignore errors from USB controller for mixer interfaces.");
  97. module_param(autoclock, bool, 0444);
  98. MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
  99. module_param_array(quirk_alias, charp, NULL, 0444);
  100. MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
  101. module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
  102. MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
  103. /*
  104. * we keep the snd_usb_audio_t instances by ourselves for merging
  105. * the all interfaces on the same card as one sound device.
  106. */
  107. static DEFINE_MUTEX(register_mutex);
  108. static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
  109. static struct usb_driver usb_audio_driver;
  110. /*
  111. * disconnect streams
  112. * called from usb_audio_disconnect()
  113. */
  114. static void snd_usb_stream_disconnect(struct snd_usb_stream *as)
  115. {
  116. int idx;
  117. struct snd_usb_substream *subs;
  118. for (idx = 0; idx < 2; idx++) {
  119. subs = &as->substream[idx];
  120. if (!subs->num_formats)
  121. continue;
  122. subs->interface = -1;
  123. subs->data_endpoint = NULL;
  124. subs->sync_endpoint = NULL;
  125. }
  126. }
  127. static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
  128. {
  129. struct usb_device *dev = chip->dev;
  130. struct usb_host_interface *alts;
  131. struct usb_interface_descriptor *altsd;
  132. struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
  133. if (!iface) {
  134. dev_err(&dev->dev, "%u:%d : does not exist\n",
  135. ctrlif, interface);
  136. return -EINVAL;
  137. }
  138. alts = &iface->altsetting[0];
  139. altsd = get_iface_desc(alts);
  140. /*
  141. * Android with both accessory and audio interfaces enabled gets the
  142. * interface numbers wrong.
  143. */
  144. if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
  145. chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
  146. interface == 0 &&
  147. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  148. altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
  149. interface = 2;
  150. iface = usb_ifnum_to_if(dev, interface);
  151. if (!iface)
  152. return -EINVAL;
  153. alts = &iface->altsetting[0];
  154. altsd = get_iface_desc(alts);
  155. }
  156. if (usb_interface_claimed(iface)) {
  157. dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
  158. ctrlif, interface);
  159. return -EINVAL;
  160. }
  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. chip->usb_id);
  167. if (err < 0) {
  168. dev_err(&dev->dev,
  169. "%u:%d: cannot create sequencer device\n",
  170. ctrlif, interface);
  171. return -EINVAL;
  172. }
  173. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  174. return 0;
  175. }
  176. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  177. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  178. altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
  179. dev_dbg(&dev->dev,
  180. "%u:%d: skipping non-supported interface %d\n",
  181. ctrlif, interface, altsd->bInterfaceClass);
  182. /* skip non-supported classes */
  183. return -EINVAL;
  184. }
  185. if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
  186. dev_err(&dev->dev, "low speed audio streaming not supported\n");
  187. return -EINVAL;
  188. }
  189. if (! snd_usb_parse_audio_interface(chip, interface)) {
  190. usb_set_interface(dev, interface, 0); /* reset the current interface */
  191. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  192. }
  193. return 0;
  194. }
  195. /*
  196. * parse audio control descriptor and create pcm/midi streams
  197. */
  198. static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
  199. {
  200. struct usb_device *dev = chip->dev;
  201. struct usb_host_interface *host_iface;
  202. struct usb_interface_descriptor *altsd;
  203. int i, protocol;
  204. /* find audiocontrol interface */
  205. host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
  206. altsd = get_iface_desc(host_iface);
  207. protocol = altsd->bInterfaceProtocol;
  208. switch (protocol) {
  209. default:
  210. dev_warn(&dev->dev,
  211. "unknown interface protocol %#02x, assuming v1\n",
  212. protocol);
  213. /* fall through */
  214. case UAC_VERSION_1: {
  215. struct uac1_ac_header_descriptor *h1;
  216. int rest_bytes;
  217. h1 = snd_usb_find_csint_desc(host_iface->extra,
  218. host_iface->extralen,
  219. NULL, UAC_HEADER);
  220. if (!h1) {
  221. dev_err(&dev->dev, "cannot find UAC_HEADER\n");
  222. return -EINVAL;
  223. }
  224. rest_bytes = (void *)(host_iface->extra +
  225. host_iface->extralen) - (void *)h1;
  226. /* just to be sure -- this shouldn't hit at all */
  227. if (rest_bytes <= 0) {
  228. dev_err(&dev->dev, "invalid control header\n");
  229. return -EINVAL;
  230. }
  231. if (rest_bytes < sizeof(*h1)) {
  232. dev_err(&dev->dev, "too short v1 buffer descriptor\n");
  233. return -EINVAL;
  234. }
  235. if (!h1->bInCollection) {
  236. dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
  237. return -EINVAL;
  238. }
  239. if (rest_bytes < h1->bLength) {
  240. dev_err(&dev->dev, "invalid buffer length (v1)\n");
  241. return -EINVAL;
  242. }
  243. if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
  244. dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
  245. return -EINVAL;
  246. }
  247. for (i = 0; i < h1->bInCollection; i++)
  248. snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
  249. break;
  250. }
  251. case UAC_VERSION_2:
  252. case UAC_VERSION_3: {
  253. struct usb_interface_assoc_descriptor *assoc =
  254. usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  255. if (!assoc) {
  256. /*
  257. * Firmware writers cannot count to three. So to find
  258. * the IAD on the NuForce UDH-100, also check the next
  259. * interface.
  260. */
  261. struct usb_interface *iface =
  262. usb_ifnum_to_if(dev, ctrlif + 1);
  263. if (iface &&
  264. iface->intf_assoc &&
  265. iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
  266. iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
  267. assoc = iface->intf_assoc;
  268. }
  269. if (!assoc) {
  270. dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
  271. return -EINVAL;
  272. }
  273. if (protocol == UAC_VERSION_3) {
  274. int badd = assoc->bFunctionSubClass;
  275. if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
  276. (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
  277. badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
  278. dev_err(&dev->dev,
  279. "Unsupported UAC3 BADD profile\n");
  280. return -EINVAL;
  281. }
  282. chip->badd_profile = badd;
  283. }
  284. for (i = 0; i < assoc->bInterfaceCount; i++) {
  285. int intf = assoc->bFirstInterface + i;
  286. if (intf != ctrlif)
  287. snd_usb_create_stream(chip, ctrlif, intf);
  288. }
  289. break;
  290. }
  291. }
  292. return 0;
  293. }
  294. /*
  295. * free the chip instance
  296. *
  297. * here we have to do not much, since pcm and controls are already freed
  298. *
  299. */
  300. static void snd_usb_audio_free(struct snd_card *card)
  301. {
  302. struct snd_usb_audio *chip = card->private_data;
  303. struct snd_usb_endpoint *ep, *n;
  304. list_for_each_entry_safe(ep, n, &chip->ep_list, list)
  305. snd_usb_endpoint_free(ep);
  306. mutex_destroy(&chip->mutex);
  307. if (!atomic_read(&chip->shutdown))
  308. dev_set_drvdata(&chip->dev->dev, NULL);
  309. }
  310. static void usb_audio_make_shortname(struct usb_device *dev,
  311. struct snd_usb_audio *chip,
  312. const struct snd_usb_audio_quirk *quirk)
  313. {
  314. struct snd_card *card = chip->card;
  315. if (quirk && quirk->product_name && *quirk->product_name) {
  316. strlcpy(card->shortname, quirk->product_name,
  317. sizeof(card->shortname));
  318. return;
  319. }
  320. /* retrieve the device string as shortname */
  321. if (!dev->descriptor.iProduct ||
  322. usb_string(dev, dev->descriptor.iProduct,
  323. card->shortname, sizeof(card->shortname)) <= 0) {
  324. /* no name available from anywhere, so use ID */
  325. sprintf(card->shortname, "USB Device %#04x:%#04x",
  326. USB_ID_VENDOR(chip->usb_id),
  327. USB_ID_PRODUCT(chip->usb_id));
  328. }
  329. strim(card->shortname);
  330. }
  331. static void usb_audio_make_longname(struct usb_device *dev,
  332. struct snd_usb_audio *chip,
  333. const struct snd_usb_audio_quirk *quirk)
  334. {
  335. struct snd_card *card = chip->card;
  336. int len;
  337. /* shortcut - if any pre-defined string is given, use it */
  338. if (quirk && quirk->profile_name && *quirk->profile_name) {
  339. strlcpy(card->longname, quirk->profile_name,
  340. sizeof(card->longname));
  341. return;
  342. }
  343. if (quirk && quirk->vendor_name && *quirk->vendor_name) {
  344. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  345. } else {
  346. /* retrieve the vendor and device strings as longname */
  347. if (dev->descriptor.iManufacturer)
  348. len = usb_string(dev, dev->descriptor.iManufacturer,
  349. card->longname, sizeof(card->longname));
  350. else
  351. len = 0;
  352. /* we don't really care if there isn't any vendor string */
  353. }
  354. if (len > 0) {
  355. strim(card->longname);
  356. if (*card->longname)
  357. strlcat(card->longname, " ", sizeof(card->longname));
  358. }
  359. strlcat(card->longname, card->shortname, sizeof(card->longname));
  360. len = strlcat(card->longname, " at ", sizeof(card->longname));
  361. if (len < sizeof(card->longname))
  362. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  363. switch (snd_usb_get_speed(dev)) {
  364. case USB_SPEED_LOW:
  365. strlcat(card->longname, ", low speed", sizeof(card->longname));
  366. break;
  367. case USB_SPEED_FULL:
  368. strlcat(card->longname, ", full speed", sizeof(card->longname));
  369. break;
  370. case USB_SPEED_HIGH:
  371. strlcat(card->longname, ", high speed", sizeof(card->longname));
  372. break;
  373. case USB_SPEED_SUPER:
  374. strlcat(card->longname, ", super speed", sizeof(card->longname));
  375. break;
  376. case USB_SPEED_SUPER_PLUS:
  377. strlcat(card->longname, ", super speed plus", sizeof(card->longname));
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. /*
  384. * create a chip instance and set its names.
  385. */
  386. static int snd_usb_audio_create(struct usb_interface *intf,
  387. struct usb_device *dev, int idx,
  388. const struct snd_usb_audio_quirk *quirk,
  389. unsigned int usb_id,
  390. struct snd_usb_audio **rchip)
  391. {
  392. struct snd_card *card;
  393. struct snd_usb_audio *chip;
  394. int err;
  395. char component[14];
  396. *rchip = NULL;
  397. switch (snd_usb_get_speed(dev)) {
  398. case USB_SPEED_LOW:
  399. case USB_SPEED_FULL:
  400. case USB_SPEED_HIGH:
  401. case USB_SPEED_WIRELESS:
  402. case USB_SPEED_SUPER:
  403. case USB_SPEED_SUPER_PLUS:
  404. break;
  405. default:
  406. dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
  407. return -ENXIO;
  408. }
  409. err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
  410. sizeof(*chip), &card);
  411. if (err < 0) {
  412. dev_err(&dev->dev, "cannot create card instance %d\n", idx);
  413. return err;
  414. }
  415. chip = card->private_data;
  416. mutex_init(&chip->mutex);
  417. init_waitqueue_head(&chip->shutdown_wait);
  418. chip->index = idx;
  419. chip->dev = dev;
  420. chip->card = card;
  421. chip->setup = device_setup[idx];
  422. chip->autoclock = autoclock;
  423. atomic_set(&chip->active, 1); /* avoid autopm during probing */
  424. atomic_set(&chip->usage_count, 0);
  425. atomic_set(&chip->shutdown, 0);
  426. chip->usb_id = usb_id;
  427. INIT_LIST_HEAD(&chip->pcm_list);
  428. INIT_LIST_HEAD(&chip->ep_list);
  429. INIT_LIST_HEAD(&chip->midi_list);
  430. INIT_LIST_HEAD(&chip->mixer_list);
  431. card->private_free = snd_usb_audio_free;
  432. strcpy(card->driver, "USB-Audio");
  433. sprintf(component, "USB%04x:%04x",
  434. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  435. snd_component_add(card, component);
  436. usb_audio_make_shortname(dev, chip, quirk);
  437. usb_audio_make_longname(dev, chip, quirk);
  438. snd_usb_audio_create_proc(chip);
  439. *rchip = chip;
  440. return 0;
  441. }
  442. /* look for a matching quirk alias id */
  443. static bool get_alias_id(struct usb_device *dev, unsigned int *id)
  444. {
  445. int i;
  446. unsigned int src, dst;
  447. for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
  448. if (!quirk_alias[i] ||
  449. sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
  450. src != *id)
  451. continue;
  452. dev_info(&dev->dev,
  453. "device (%04x:%04x): applying quirk alias %04x:%04x\n",
  454. USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
  455. USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
  456. *id = dst;
  457. return true;
  458. }
  459. return false;
  460. }
  461. static const struct usb_device_id usb_audio_ids[]; /* defined below */
  462. /* look for the corresponding quirk */
  463. static const struct snd_usb_audio_quirk *
  464. get_alias_quirk(struct usb_device *dev, unsigned int id)
  465. {
  466. const struct usb_device_id *p;
  467. for (p = usb_audio_ids; p->match_flags; p++) {
  468. /* FIXME: this checks only vendor:product pair in the list */
  469. if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
  470. USB_DEVICE_ID_MATCH_DEVICE &&
  471. p->idVendor == USB_ID_VENDOR(id) &&
  472. p->idProduct == USB_ID_PRODUCT(id))
  473. return (const struct snd_usb_audio_quirk *)p->driver_info;
  474. }
  475. return NULL;
  476. }
  477. /*
  478. * probe the active usb device
  479. *
  480. * note that this can be called multiple times per a device, when it
  481. * includes multiple audio control interfaces.
  482. *
  483. * thus we check the usb device pointer and creates the card instance
  484. * only at the first time. the successive calls of this function will
  485. * append the pcm interface to the corresponding card.
  486. */
  487. static int usb_audio_probe(struct usb_interface *intf,
  488. const struct usb_device_id *usb_id)
  489. {
  490. struct usb_device *dev = interface_to_usbdev(intf);
  491. const struct snd_usb_audio_quirk *quirk =
  492. (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  493. struct snd_usb_audio *chip;
  494. int i, err;
  495. struct usb_host_interface *alts;
  496. int ifnum;
  497. u32 id;
  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 (get_alias_id(dev, &id))
  503. quirk = get_alias_quirk(dev, id);
  504. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  505. return -ENXIO;
  506. err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
  507. if (err < 0)
  508. return err;
  509. /*
  510. * found a config. now register to ALSA
  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 (atomic_read(&usb_chip[i]->shutdown)) {
  518. dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
  519. err = -EIO;
  520. goto __error;
  521. }
  522. chip = usb_chip[i];
  523. atomic_inc(&chip->active); /* avoid autopm */
  524. break;
  525. }
  526. }
  527. if (! chip) {
  528. /* it's a fresh one.
  529. * now look for an empty slot and create a new card instance
  530. */
  531. for (i = 0; i < SNDRV_CARDS; i++)
  532. if (!usb_chip[i] &&
  533. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  534. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  535. if (enable[i]) {
  536. err = snd_usb_audio_create(intf, dev, i, quirk,
  537. id, &chip);
  538. if (err < 0)
  539. goto __error;
  540. chip->pm_intf = intf;
  541. break;
  542. } else if (vid[i] != -1 || pid[i] != -1) {
  543. dev_info(&dev->dev,
  544. "device (%04x:%04x) is disabled\n",
  545. USB_ID_VENDOR(id),
  546. USB_ID_PRODUCT(id));
  547. err = -ENOENT;
  548. goto __error;
  549. }
  550. }
  551. if (!chip) {
  552. dev_err(&dev->dev, "no available usb audio device\n");
  553. err = -ENODEV;
  554. goto __error;
  555. }
  556. }
  557. dev_set_drvdata(&dev->dev, chip);
  558. /*
  559. * For devices with more than one control interface, we assume the
  560. * first contains the audio controls. We might need a more specific
  561. * check here in the future.
  562. */
  563. if (!chip->ctrl_intf)
  564. chip->ctrl_intf = alts;
  565. chip->txfr_quirk = 0;
  566. err = 1; /* continue */
  567. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  568. /* need some special handlings */
  569. err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
  570. if (err < 0)
  571. goto __error;
  572. }
  573. if (err > 0) {
  574. /* create normal USB audio interfaces */
  575. err = snd_usb_create_streams(chip, ifnum);
  576. if (err < 0)
  577. goto __error;
  578. err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error);
  579. if (err < 0)
  580. goto __error;
  581. }
  582. /* we are allowed to call snd_card_register() many times */
  583. err = snd_card_register(chip->card);
  584. if (err < 0)
  585. goto __error;
  586. usb_chip[chip->index] = chip;
  587. chip->num_interfaces++;
  588. usb_set_intfdata(intf, chip);
  589. atomic_dec(&chip->active);
  590. mutex_unlock(&register_mutex);
  591. return 0;
  592. __error:
  593. if (chip) {
  594. if (!chip->num_interfaces)
  595. snd_card_free(chip->card);
  596. atomic_dec(&chip->active);
  597. }
  598. mutex_unlock(&register_mutex);
  599. return err;
  600. }
  601. /*
  602. * we need to take care of counter, since disconnection can be called also
  603. * many times as well as usb_audio_probe().
  604. */
  605. static void usb_audio_disconnect(struct usb_interface *intf)
  606. {
  607. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  608. struct snd_card *card;
  609. struct list_head *p;
  610. if (chip == (void *)-1L)
  611. return;
  612. card = chip->card;
  613. mutex_lock(&register_mutex);
  614. if (atomic_inc_return(&chip->shutdown) == 1) {
  615. struct snd_usb_stream *as;
  616. struct snd_usb_endpoint *ep;
  617. struct usb_mixer_interface *mixer;
  618. /* wait until all pending tasks done;
  619. * they are protected by snd_usb_lock_shutdown()
  620. */
  621. wait_event(chip->shutdown_wait,
  622. !atomic_read(&chip->usage_count));
  623. snd_card_disconnect(card);
  624. /* release the pcm resources */
  625. list_for_each_entry(as, &chip->pcm_list, list) {
  626. snd_usb_stream_disconnect(as);
  627. }
  628. /* release the endpoint resources */
  629. list_for_each_entry(ep, &chip->ep_list, list) {
  630. snd_usb_endpoint_release(ep);
  631. }
  632. /* release the midi resources */
  633. list_for_each(p, &chip->midi_list) {
  634. snd_usbmidi_disconnect(p);
  635. }
  636. /* release mixer resources */
  637. list_for_each_entry(mixer, &chip->mixer_list, list) {
  638. snd_usb_mixer_disconnect(mixer);
  639. }
  640. }
  641. chip->num_interfaces--;
  642. if (chip->num_interfaces <= 0) {
  643. usb_chip[chip->index] = NULL;
  644. mutex_unlock(&register_mutex);
  645. snd_card_free_when_closed(card);
  646. } else {
  647. mutex_unlock(&register_mutex);
  648. }
  649. }
  650. /* lock the shutdown (disconnect) task and autoresume */
  651. int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
  652. {
  653. int err;
  654. atomic_inc(&chip->usage_count);
  655. if (atomic_read(&chip->shutdown)) {
  656. err = -EIO;
  657. goto error;
  658. }
  659. err = snd_usb_autoresume(chip);
  660. if (err < 0)
  661. goto error;
  662. return 0;
  663. error:
  664. if (atomic_dec_and_test(&chip->usage_count))
  665. wake_up(&chip->shutdown_wait);
  666. return err;
  667. }
  668. /* autosuspend and unlock the shutdown */
  669. void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
  670. {
  671. snd_usb_autosuspend(chip);
  672. if (atomic_dec_and_test(&chip->usage_count))
  673. wake_up(&chip->shutdown_wait);
  674. }
  675. #ifdef CONFIG_PM
  676. int snd_usb_autoresume(struct snd_usb_audio *chip)
  677. {
  678. if (atomic_read(&chip->shutdown))
  679. return -EIO;
  680. if (atomic_inc_return(&chip->active) == 1)
  681. return usb_autopm_get_interface(chip->pm_intf);
  682. return 0;
  683. }
  684. void snd_usb_autosuspend(struct snd_usb_audio *chip)
  685. {
  686. if (atomic_read(&chip->shutdown))
  687. return;
  688. if (atomic_dec_and_test(&chip->active))
  689. usb_autopm_put_interface(chip->pm_intf);
  690. }
  691. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  692. {
  693. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  694. struct snd_usb_stream *as;
  695. struct usb_mixer_interface *mixer;
  696. struct list_head *p;
  697. if (chip == (void *)-1L)
  698. return 0;
  699. chip->autosuspended = !!PMSG_IS_AUTO(message);
  700. if (!chip->autosuspended)
  701. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  702. if (!chip->num_suspended_intf++) {
  703. list_for_each_entry(as, &chip->pcm_list, list) {
  704. snd_pcm_suspend_all(as->pcm);
  705. snd_usb_pcm_suspend(as);
  706. as->substream[0].need_setup_ep =
  707. as->substream[1].need_setup_ep = true;
  708. }
  709. list_for_each(p, &chip->midi_list)
  710. snd_usbmidi_suspend(p);
  711. list_for_each_entry(mixer, &chip->mixer_list, list)
  712. snd_usb_mixer_suspend(mixer);
  713. }
  714. return 0;
  715. }
  716. static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
  717. {
  718. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  719. struct snd_usb_stream *as;
  720. struct usb_mixer_interface *mixer;
  721. struct list_head *p;
  722. int err = 0;
  723. if (chip == (void *)-1L)
  724. return 0;
  725. if (--chip->num_suspended_intf)
  726. return 0;
  727. atomic_inc(&chip->active); /* avoid autopm */
  728. list_for_each_entry(as, &chip->pcm_list, list) {
  729. err = snd_usb_pcm_resume(as);
  730. if (err < 0)
  731. goto err_out;
  732. }
  733. /*
  734. * ALSA leaves material resumption to user space
  735. * we just notify and restart the mixers
  736. */
  737. list_for_each_entry(mixer, &chip->mixer_list, list) {
  738. err = snd_usb_mixer_resume(mixer, reset_resume);
  739. if (err < 0)
  740. goto err_out;
  741. }
  742. list_for_each(p, &chip->midi_list) {
  743. snd_usbmidi_resume(p);
  744. }
  745. if (!chip->autosuspended)
  746. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  747. chip->autosuspended = 0;
  748. err_out:
  749. atomic_dec(&chip->active); /* allow autopm after this point */
  750. return err;
  751. }
  752. static int usb_audio_resume(struct usb_interface *intf)
  753. {
  754. return __usb_audio_resume(intf, false);
  755. }
  756. static int usb_audio_reset_resume(struct usb_interface *intf)
  757. {
  758. return __usb_audio_resume(intf, true);
  759. }
  760. #else
  761. #define usb_audio_suspend NULL
  762. #define usb_audio_resume NULL
  763. #define usb_audio_reset_resume NULL
  764. #endif /* CONFIG_PM */
  765. static const struct usb_device_id usb_audio_ids [] = {
  766. #include "quirks-table.h"
  767. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  768. .bInterfaceClass = USB_CLASS_AUDIO,
  769. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  770. { } /* Terminating entry */
  771. };
  772. MODULE_DEVICE_TABLE(usb, usb_audio_ids);
  773. /*
  774. * entry point for linux usb interface
  775. */
  776. static struct usb_driver usb_audio_driver = {
  777. .name = "snd-usb-audio",
  778. .probe = usb_audio_probe,
  779. .disconnect = usb_audio_disconnect,
  780. .suspend = usb_audio_suspend,
  781. .resume = usb_audio_resume,
  782. .reset_resume = usb_audio_reset_resume,
  783. .id_table = usb_audio_ids,
  784. .supports_autosuspend = 1,
  785. };
  786. module_usb_driver(usb_audio_driver);