card.c 23 KB

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