card.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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 snd_usb_audio_disconnect()
  105. */
  106. static void snd_usb_stream_disconnect(struct list_head *head)
  107. {
  108. int idx;
  109. struct snd_usb_stream *as;
  110. struct snd_usb_substream *subs;
  111. as = list_entry(head, struct snd_usb_stream, list);
  112. for (idx = 0; idx < 2; idx++) {
  113. subs = &as->substream[idx];
  114. if (!subs->num_formats)
  115. continue;
  116. subs->interface = -1;
  117. subs->data_endpoint = NULL;
  118. subs->sync_endpoint = NULL;
  119. }
  120. }
  121. static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
  122. {
  123. struct usb_device *dev = chip->dev;
  124. struct usb_host_interface *alts;
  125. struct usb_interface_descriptor *altsd;
  126. struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
  127. if (!iface) {
  128. snd_printk(KERN_ERR "%d:%u:%d : does not exist\n",
  129. dev->devnum, ctrlif, interface);
  130. return -EINVAL;
  131. }
  132. alts = &iface->altsetting[0];
  133. altsd = get_iface_desc(alts);
  134. /*
  135. * Android with both accessory and audio interfaces enabled gets the
  136. * interface numbers wrong.
  137. */
  138. if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
  139. chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
  140. interface == 0 &&
  141. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  142. altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
  143. interface = 2;
  144. iface = usb_ifnum_to_if(dev, interface);
  145. if (!iface)
  146. return -EINVAL;
  147. alts = &iface->altsetting[0];
  148. altsd = get_iface_desc(alts);
  149. }
  150. if (usb_interface_claimed(iface)) {
  151. snd_printdd(KERN_INFO "%d:%d:%d: skipping, already claimed\n",
  152. dev->devnum, ctrlif, interface);
  153. return -EINVAL;
  154. }
  155. if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
  156. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
  157. altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
  158. int err = snd_usbmidi_create(chip->card, iface,
  159. &chip->midi_list, NULL);
  160. if (err < 0) {
  161. snd_printk(KERN_ERR "%d:%u:%d: cannot create sequencer device\n",
  162. dev->devnum, ctrlif, interface);
  163. return -EINVAL;
  164. }
  165. usb_driver_claim_interface(&usb_audio_driver, iface, (void *)-1L);
  166. return 0;
  167. }
  168. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  169. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  170. altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
  171. snd_printdd(KERN_ERR "%d:%u:%d: skipping non-supported interface %d\n",
  172. dev->devnum, ctrlif, interface, altsd->bInterfaceClass);
  173. /* skip non-supported classes */
  174. return -EINVAL;
  175. }
  176. if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
  177. snd_printk(KERN_ERR "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. return -EINVAL;
  184. }
  185. return 0;
  186. }
  187. /*
  188. * parse audio control descriptor and create pcm/midi streams
  189. */
  190. static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
  191. {
  192. struct usb_device *dev = chip->dev;
  193. struct usb_host_interface *host_iface;
  194. struct usb_interface_descriptor *altsd;
  195. void *control_header;
  196. int i, protocol;
  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. snd_printk(KERN_ERR "cannot find UAC_HEADER\n");
  206. return -EINVAL;
  207. }
  208. switch (protocol) {
  209. default:
  210. snd_printdd(KERN_WARNING "unknown interface protocol %#02x, assuming v1\n",
  211. protocol);
  212. /* fall through */
  213. case UAC_VERSION_1: {
  214. struct uac1_ac_header_descriptor *h1 = control_header;
  215. if (!h1->bInCollection) {
  216. snd_printk(KERN_INFO "skipping empty audio interface (v1)\n");
  217. return -EINVAL;
  218. }
  219. if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
  220. snd_printk(KERN_ERR "invalid UAC_HEADER (v1)\n");
  221. return -EINVAL;
  222. }
  223. for (i = 0; i < h1->bInCollection; i++)
  224. snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
  225. break;
  226. }
  227. case UAC_VERSION_2: {
  228. struct usb_interface_assoc_descriptor *assoc =
  229. usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  230. if (!assoc) {
  231. /*
  232. * Firmware writers cannot count to three. So to find
  233. * the IAD on the NuForce UDH-100, also check the next
  234. * interface.
  235. */
  236. struct usb_interface *iface =
  237. usb_ifnum_to_if(dev, ctrlif + 1);
  238. if (iface &&
  239. iface->intf_assoc &&
  240. iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
  241. iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
  242. assoc = iface->intf_assoc;
  243. }
  244. if (!assoc) {
  245. snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
  246. return -EINVAL;
  247. }
  248. for (i = 0; i < assoc->bInterfaceCount; i++) {
  249. int intf = assoc->bFirstInterface + i;
  250. if (intf != ctrlif)
  251. snd_usb_create_stream(chip, ctrlif, intf);
  252. }
  253. break;
  254. }
  255. }
  256. return 0;
  257. }
  258. /*
  259. * free the chip instance
  260. *
  261. * here we have to do not much, since pcm and controls are already freed
  262. *
  263. */
  264. static int snd_usb_audio_free(struct snd_usb_audio *chip)
  265. {
  266. mutex_destroy(&chip->mutex);
  267. kfree(chip);
  268. return 0;
  269. }
  270. static int snd_usb_audio_dev_free(struct snd_device *device)
  271. {
  272. struct snd_usb_audio *chip = device->device_data;
  273. return snd_usb_audio_free(chip);
  274. }
  275. static void remove_trailing_spaces(char *str)
  276. {
  277. char *p;
  278. if (!*str)
  279. return;
  280. for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
  281. *p = 0;
  282. }
  283. /*
  284. * create a chip instance and set its names.
  285. */
  286. static int snd_usb_audio_create(struct usb_device *dev, int idx,
  287. const struct snd_usb_audio_quirk *quirk,
  288. struct snd_usb_audio **rchip)
  289. {
  290. struct snd_card *card;
  291. struct snd_usb_audio *chip;
  292. int err, len;
  293. char component[14];
  294. static struct snd_device_ops ops = {
  295. .dev_free = snd_usb_audio_dev_free,
  296. };
  297. *rchip = NULL;
  298. switch (snd_usb_get_speed(dev)) {
  299. case USB_SPEED_LOW:
  300. case USB_SPEED_FULL:
  301. case USB_SPEED_HIGH:
  302. case USB_SPEED_WIRELESS:
  303. case USB_SPEED_SUPER:
  304. break;
  305. default:
  306. snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
  307. return -ENXIO;
  308. }
  309. err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
  310. if (err < 0) {
  311. snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
  312. return err;
  313. }
  314. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  315. if (! chip) {
  316. snd_card_free(card);
  317. return -ENOMEM;
  318. }
  319. mutex_init(&chip->mutex);
  320. init_rwsem(&chip->shutdown_rwsem);
  321. chip->index = idx;
  322. chip->dev = dev;
  323. chip->card = card;
  324. chip->setup = device_setup[idx];
  325. chip->autoclock = autoclock;
  326. chip->probing = 1;
  327. chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  328. le16_to_cpu(dev->descriptor.idProduct));
  329. INIT_LIST_HEAD(&chip->pcm_list);
  330. INIT_LIST_HEAD(&chip->ep_list);
  331. INIT_LIST_HEAD(&chip->midi_list);
  332. INIT_LIST_HEAD(&chip->mixer_list);
  333. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  334. snd_usb_audio_free(chip);
  335. snd_card_free(card);
  336. return err;
  337. }
  338. strcpy(card->driver, "USB-Audio");
  339. sprintf(component, "USB%04x:%04x",
  340. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  341. snd_component_add(card, component);
  342. /* retrieve the device string as shortname */
  343. if (quirk && quirk->product_name && *quirk->product_name) {
  344. strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
  345. } else {
  346. if (!dev->descriptor.iProduct ||
  347. usb_string(dev, dev->descriptor.iProduct,
  348. card->shortname, sizeof(card->shortname)) <= 0) {
  349. /* no name available from anywhere, so use ID */
  350. sprintf(card->shortname, "USB Device %#04x:%#04x",
  351. USB_ID_VENDOR(chip->usb_id),
  352. USB_ID_PRODUCT(chip->usb_id));
  353. }
  354. }
  355. remove_trailing_spaces(card->shortname);
  356. /* retrieve the vendor and device strings as longname */
  357. if (quirk && quirk->vendor_name && *quirk->vendor_name) {
  358. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  359. } else {
  360. if (dev->descriptor.iManufacturer)
  361. len = usb_string(dev, dev->descriptor.iManufacturer,
  362. card->longname, sizeof(card->longname));
  363. else
  364. len = 0;
  365. /* we don't really care if there isn't any vendor string */
  366. }
  367. if (len > 0) {
  368. remove_trailing_spaces(card->longname);
  369. if (*card->longname)
  370. strlcat(card->longname, " ", sizeof(card->longname));
  371. }
  372. strlcat(card->longname, card->shortname, sizeof(card->longname));
  373. len = strlcat(card->longname, " at ", sizeof(card->longname));
  374. if (len < sizeof(card->longname))
  375. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  376. switch (snd_usb_get_speed(dev)) {
  377. case USB_SPEED_LOW:
  378. strlcat(card->longname, ", low speed", sizeof(card->longname));
  379. break;
  380. case USB_SPEED_FULL:
  381. strlcat(card->longname, ", full speed", sizeof(card->longname));
  382. break;
  383. case USB_SPEED_HIGH:
  384. strlcat(card->longname, ", high speed", sizeof(card->longname));
  385. break;
  386. case USB_SPEED_SUPER:
  387. strlcat(card->longname, ", super speed", sizeof(card->longname));
  388. break;
  389. default:
  390. break;
  391. }
  392. snd_usb_audio_create_proc(chip);
  393. *rchip = chip;
  394. return 0;
  395. }
  396. /*
  397. * probe the active usb device
  398. *
  399. * note that this can be called multiple times per a device, when it
  400. * includes multiple audio control interfaces.
  401. *
  402. * thus we check the usb device pointer and creates the card instance
  403. * only at the first time. the successive calls of this function will
  404. * append the pcm interface to the corresponding card.
  405. */
  406. static struct snd_usb_audio *
  407. snd_usb_audio_probe(struct usb_device *dev,
  408. struct usb_interface *intf,
  409. const struct usb_device_id *usb_id)
  410. {
  411. const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  412. int i, err;
  413. struct snd_usb_audio *chip;
  414. struct usb_host_interface *alts;
  415. int ifnum;
  416. u32 id;
  417. alts = &intf->altsetting[0];
  418. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  419. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  420. le16_to_cpu(dev->descriptor.idProduct));
  421. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  422. goto __err_val;
  423. if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
  424. goto __err_val;
  425. /*
  426. * found a config. now register to ALSA
  427. */
  428. /* check whether it's already registered */
  429. chip = NULL;
  430. mutex_lock(&register_mutex);
  431. for (i = 0; i < SNDRV_CARDS; i++) {
  432. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  433. if (usb_chip[i]->shutdown) {
  434. snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
  435. goto __error;
  436. }
  437. chip = usb_chip[i];
  438. chip->probing = 1;
  439. break;
  440. }
  441. }
  442. if (! chip) {
  443. /* it's a fresh one.
  444. * now look for an empty slot and create a new card instance
  445. */
  446. for (i = 0; i < SNDRV_CARDS; i++)
  447. if (enable[i] && ! usb_chip[i] &&
  448. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  449. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  450. if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
  451. goto __error;
  452. }
  453. snd_card_set_dev(chip->card, &intf->dev);
  454. chip->pm_intf = intf;
  455. break;
  456. }
  457. if (!chip) {
  458. printk(KERN_ERR "no available usb audio device\n");
  459. goto __error;
  460. }
  461. }
  462. /*
  463. * For devices with more than one control interface, we assume the
  464. * first contains the audio controls. We might need a more specific
  465. * check here in the future.
  466. */
  467. if (!chip->ctrl_intf)
  468. chip->ctrl_intf = alts;
  469. chip->txfr_quirk = 0;
  470. err = 1; /* continue */
  471. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  472. /* need some special handlings */
  473. if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
  474. goto __error;
  475. }
  476. if (err > 0) {
  477. /* create normal USB audio interfaces */
  478. if (snd_usb_create_streams(chip, ifnum) < 0 ||
  479. snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
  480. goto __error;
  481. }
  482. }
  483. /* we are allowed to call snd_card_register() many times */
  484. if (snd_card_register(chip->card) < 0) {
  485. goto __error;
  486. }
  487. usb_chip[chip->index] = chip;
  488. chip->num_interfaces++;
  489. chip->probing = 0;
  490. mutex_unlock(&register_mutex);
  491. return chip;
  492. __error:
  493. if (chip) {
  494. if (!chip->num_interfaces)
  495. snd_card_free(chip->card);
  496. chip->probing = 0;
  497. }
  498. mutex_unlock(&register_mutex);
  499. __err_val:
  500. return NULL;
  501. }
  502. /*
  503. * we need to take care of counter, since disconnection can be called also
  504. * many times as well as usb_audio_probe().
  505. */
  506. static void snd_usb_audio_disconnect(struct usb_device *dev,
  507. struct snd_usb_audio *chip)
  508. {
  509. struct snd_card *card;
  510. struct list_head *p, *n;
  511. if (chip == (void *)-1L)
  512. return;
  513. card = chip->card;
  514. down_write(&chip->shutdown_rwsem);
  515. chip->shutdown = 1;
  516. up_write(&chip->shutdown_rwsem);
  517. mutex_lock(&register_mutex);
  518. chip->num_interfaces--;
  519. if (chip->num_interfaces <= 0) {
  520. snd_card_disconnect(card);
  521. /* release the pcm resources */
  522. list_for_each(p, &chip->pcm_list) {
  523. snd_usb_stream_disconnect(p);
  524. }
  525. /* release the endpoint resources */
  526. list_for_each_safe(p, n, &chip->ep_list) {
  527. snd_usb_endpoint_free(p);
  528. }
  529. /* release the midi resources */
  530. list_for_each(p, &chip->midi_list) {
  531. snd_usbmidi_disconnect(p);
  532. }
  533. /* release mixer resources */
  534. list_for_each(p, &chip->mixer_list) {
  535. snd_usb_mixer_disconnect(p);
  536. }
  537. usb_chip[chip->index] = NULL;
  538. mutex_unlock(&register_mutex);
  539. snd_card_free_when_closed(card);
  540. } else {
  541. mutex_unlock(&register_mutex);
  542. }
  543. }
  544. /*
  545. * new 2.5 USB kernel API
  546. */
  547. static int usb_audio_probe(struct usb_interface *intf,
  548. const struct usb_device_id *id)
  549. {
  550. struct snd_usb_audio *chip;
  551. chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
  552. if (chip) {
  553. usb_set_intfdata(intf, chip);
  554. return 0;
  555. } else
  556. return -EIO;
  557. }
  558. static void usb_audio_disconnect(struct usb_interface *intf)
  559. {
  560. snd_usb_audio_disconnect(interface_to_usbdev(intf),
  561. usb_get_intfdata(intf));
  562. }
  563. #ifdef CONFIG_PM
  564. int snd_usb_autoresume(struct snd_usb_audio *chip)
  565. {
  566. int err = -ENODEV;
  567. down_read(&chip->shutdown_rwsem);
  568. if (chip->probing)
  569. err = 0;
  570. else if (!chip->shutdown)
  571. err = usb_autopm_get_interface(chip->pm_intf);
  572. up_read(&chip->shutdown_rwsem);
  573. return err;
  574. }
  575. void snd_usb_autosuspend(struct snd_usb_audio *chip)
  576. {
  577. down_read(&chip->shutdown_rwsem);
  578. if (!chip->shutdown && !chip->probing)
  579. usb_autopm_put_interface(chip->pm_intf);
  580. up_read(&chip->shutdown_rwsem);
  581. }
  582. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  583. {
  584. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  585. struct snd_usb_stream *as;
  586. struct usb_mixer_interface *mixer;
  587. if (chip == (void *)-1L)
  588. return 0;
  589. if (!PMSG_IS_AUTO(message)) {
  590. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  591. if (!chip->num_suspended_intf++) {
  592. list_for_each_entry(as, &chip->pcm_list, list) {
  593. snd_pcm_suspend_all(as->pcm);
  594. as->substream[0].need_setup_ep =
  595. as->substream[1].need_setup_ep = true;
  596. }
  597. }
  598. } else {
  599. /*
  600. * otherwise we keep the rest of the system in the dark
  601. * to keep this transparent
  602. */
  603. if (!chip->num_suspended_intf++)
  604. chip->autosuspended = 1;
  605. }
  606. list_for_each_entry(mixer, &chip->mixer_list, list)
  607. snd_usb_mixer_inactivate(mixer);
  608. return 0;
  609. }
  610. static int usb_audio_resume(struct usb_interface *intf)
  611. {
  612. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  613. struct usb_mixer_interface *mixer;
  614. int err = 0;
  615. if (chip == (void *)-1L)
  616. return 0;
  617. if (--chip->num_suspended_intf)
  618. return 0;
  619. /*
  620. * ALSA leaves material resumption to user space
  621. * we just notify and restart the mixers
  622. */
  623. list_for_each_entry(mixer, &chip->mixer_list, list) {
  624. err = snd_usb_mixer_activate(mixer);
  625. if (err < 0)
  626. goto err_out;
  627. }
  628. if (!chip->autosuspended)
  629. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  630. chip->autosuspended = 0;
  631. err_out:
  632. return err;
  633. }
  634. #else
  635. #define usb_audio_suspend NULL
  636. #define usb_audio_resume NULL
  637. #endif /* CONFIG_PM */
  638. static struct usb_device_id usb_audio_ids [] = {
  639. #include "quirks-table.h"
  640. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  641. .bInterfaceClass = USB_CLASS_AUDIO,
  642. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  643. { } /* Terminating entry */
  644. };
  645. MODULE_DEVICE_TABLE(usb, usb_audio_ids);
  646. /*
  647. * entry point for linux usb interface
  648. */
  649. static struct usb_driver usb_audio_driver = {
  650. .name = "snd-usb-audio",
  651. .probe = usb_audio_probe,
  652. .disconnect = usb_audio_disconnect,
  653. .suspend = usb_audio_suspend,
  654. .resume = usb_audio_resume,
  655. .id_table = usb_audio_ids,
  656. .supports_autosuspend = 1,
  657. };
  658. module_usb_driver(usb_audio_driver);