card.c 21 KB

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