card.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  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. return usb_driver_claim_interface(&usb_audio_driver, iface,
  174. USB_AUDIO_IFACE_UNUSED);
  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. return usb_driver_claim_interface(&usb_audio_driver, iface,
  192. USB_AUDIO_IFACE_UNUSED);
  193. }
  194. return 0;
  195. }
  196. /*
  197. * parse audio control descriptor and create pcm/midi streams
  198. */
  199. static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
  200. {
  201. struct usb_device *dev = chip->dev;
  202. struct usb_host_interface *host_iface;
  203. struct usb_interface_descriptor *altsd;
  204. int i, protocol;
  205. /* find audiocontrol interface */
  206. host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
  207. altsd = get_iface_desc(host_iface);
  208. protocol = altsd->bInterfaceProtocol;
  209. switch (protocol) {
  210. default:
  211. dev_warn(&dev->dev,
  212. "unknown interface protocol %#02x, assuming v1\n",
  213. protocol);
  214. /* fall through */
  215. case UAC_VERSION_1: {
  216. struct uac1_ac_header_descriptor *h1;
  217. int rest_bytes;
  218. h1 = snd_usb_find_csint_desc(host_iface->extra,
  219. host_iface->extralen,
  220. NULL, UAC_HEADER);
  221. if (!h1 || h1->bLength < sizeof(*h1)) {
  222. dev_err(&dev->dev, "cannot find UAC_HEADER\n");
  223. return -EINVAL;
  224. }
  225. rest_bytes = (void *)(host_iface->extra +
  226. host_iface->extralen) - (void *)h1;
  227. /* just to be sure -- this shouldn't hit at all */
  228. if (rest_bytes <= 0) {
  229. dev_err(&dev->dev, "invalid control header\n");
  230. return -EINVAL;
  231. }
  232. if (rest_bytes < sizeof(*h1)) {
  233. dev_err(&dev->dev, "too short v1 buffer descriptor\n");
  234. return -EINVAL;
  235. }
  236. if (!h1->bInCollection) {
  237. dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
  238. return -EINVAL;
  239. }
  240. if (rest_bytes < h1->bLength) {
  241. dev_err(&dev->dev, "invalid buffer length (v1)\n");
  242. return -EINVAL;
  243. }
  244. if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
  245. dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
  246. return -EINVAL;
  247. }
  248. for (i = 0; i < h1->bInCollection; i++)
  249. snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
  250. break;
  251. }
  252. case UAC_VERSION_2:
  253. case UAC_VERSION_3: {
  254. struct usb_interface_assoc_descriptor *assoc =
  255. usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  256. if (!assoc) {
  257. /*
  258. * Firmware writers cannot count to three. So to find
  259. * the IAD on the NuForce UDH-100, also check the next
  260. * interface.
  261. */
  262. struct usb_interface *iface =
  263. usb_ifnum_to_if(dev, ctrlif + 1);
  264. if (iface &&
  265. iface->intf_assoc &&
  266. iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
  267. iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
  268. assoc = iface->intf_assoc;
  269. }
  270. if (!assoc) {
  271. dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
  272. return -EINVAL;
  273. }
  274. if (protocol == UAC_VERSION_3) {
  275. int badd = assoc->bFunctionSubClass;
  276. if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
  277. (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
  278. badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
  279. dev_err(&dev->dev,
  280. "Unsupported UAC3 BADD profile\n");
  281. return -EINVAL;
  282. }
  283. chip->badd_profile = badd;
  284. }
  285. for (i = 0; i < assoc->bInterfaceCount; i++) {
  286. int intf = assoc->bFirstInterface + i;
  287. if (intf != ctrlif)
  288. snd_usb_create_stream(chip, ctrlif, intf);
  289. }
  290. break;
  291. }
  292. }
  293. return 0;
  294. }
  295. /*
  296. * free the chip instance
  297. *
  298. * here we have to do not much, since pcm and controls are already freed
  299. *
  300. */
  301. static void snd_usb_audio_free(struct snd_card *card)
  302. {
  303. struct snd_usb_audio *chip = card->private_data;
  304. struct snd_usb_endpoint *ep, *n;
  305. list_for_each_entry_safe(ep, n, &chip->ep_list, list)
  306. snd_usb_endpoint_free(ep);
  307. mutex_destroy(&chip->mutex);
  308. if (!atomic_read(&chip->shutdown))
  309. dev_set_drvdata(&chip->dev->dev, NULL);
  310. }
  311. static void usb_audio_make_shortname(struct usb_device *dev,
  312. struct snd_usb_audio *chip,
  313. const struct snd_usb_audio_quirk *quirk)
  314. {
  315. struct snd_card *card = chip->card;
  316. if (quirk && quirk->product_name && *quirk->product_name) {
  317. strlcpy(card->shortname, quirk->product_name,
  318. sizeof(card->shortname));
  319. return;
  320. }
  321. /* retrieve the device string as shortname */
  322. if (!dev->descriptor.iProduct ||
  323. usb_string(dev, dev->descriptor.iProduct,
  324. card->shortname, sizeof(card->shortname)) <= 0) {
  325. /* no name available from anywhere, so use ID */
  326. sprintf(card->shortname, "USB Device %#04x:%#04x",
  327. USB_ID_VENDOR(chip->usb_id),
  328. USB_ID_PRODUCT(chip->usb_id));
  329. }
  330. strim(card->shortname);
  331. }
  332. static void usb_audio_make_longname(struct usb_device *dev,
  333. struct snd_usb_audio *chip,
  334. const struct snd_usb_audio_quirk *quirk)
  335. {
  336. struct snd_card *card = chip->card;
  337. int len;
  338. /* shortcut - if any pre-defined string is given, use it */
  339. if (quirk && quirk->profile_name && *quirk->profile_name) {
  340. strlcpy(card->longname, quirk->profile_name,
  341. sizeof(card->longname));
  342. return;
  343. }
  344. if (quirk && quirk->vendor_name && *quirk->vendor_name) {
  345. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  346. } else {
  347. /* retrieve the vendor and device strings as longname */
  348. if (dev->descriptor.iManufacturer)
  349. len = usb_string(dev, dev->descriptor.iManufacturer,
  350. card->longname, sizeof(card->longname));
  351. else
  352. len = 0;
  353. /* we don't really care if there isn't any vendor string */
  354. }
  355. if (len > 0) {
  356. strim(card->longname);
  357. if (*card->longname)
  358. strlcat(card->longname, " ", sizeof(card->longname));
  359. }
  360. strlcat(card->longname, card->shortname, sizeof(card->longname));
  361. len = strlcat(card->longname, " at ", sizeof(card->longname));
  362. if (len < sizeof(card->longname))
  363. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  364. switch (snd_usb_get_speed(dev)) {
  365. case USB_SPEED_LOW:
  366. strlcat(card->longname, ", low speed", sizeof(card->longname));
  367. break;
  368. case USB_SPEED_FULL:
  369. strlcat(card->longname, ", full speed", sizeof(card->longname));
  370. break;
  371. case USB_SPEED_HIGH:
  372. strlcat(card->longname, ", high speed", sizeof(card->longname));
  373. break;
  374. case USB_SPEED_SUPER:
  375. strlcat(card->longname, ", super speed", sizeof(card->longname));
  376. break;
  377. case USB_SPEED_SUPER_PLUS:
  378. strlcat(card->longname, ", super speed plus", sizeof(card->longname));
  379. break;
  380. default:
  381. break;
  382. }
  383. }
  384. /*
  385. * create a chip instance and set its names.
  386. */
  387. static int snd_usb_audio_create(struct usb_interface *intf,
  388. struct usb_device *dev, int idx,
  389. const struct snd_usb_audio_quirk *quirk,
  390. unsigned int usb_id,
  391. struct snd_usb_audio **rchip)
  392. {
  393. struct snd_card *card;
  394. struct snd_usb_audio *chip;
  395. int err;
  396. char component[14];
  397. *rchip = NULL;
  398. switch (snd_usb_get_speed(dev)) {
  399. case USB_SPEED_LOW:
  400. case USB_SPEED_FULL:
  401. case USB_SPEED_HIGH:
  402. case USB_SPEED_WIRELESS:
  403. case USB_SPEED_SUPER:
  404. case USB_SPEED_SUPER_PLUS:
  405. break;
  406. default:
  407. dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
  408. return -ENXIO;
  409. }
  410. err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
  411. sizeof(*chip), &card);
  412. if (err < 0) {
  413. dev_err(&dev->dev, "cannot create card instance %d\n", idx);
  414. return err;
  415. }
  416. chip = card->private_data;
  417. mutex_init(&chip->mutex);
  418. init_waitqueue_head(&chip->shutdown_wait);
  419. chip->index = idx;
  420. chip->dev = dev;
  421. chip->card = card;
  422. chip->setup = device_setup[idx];
  423. chip->autoclock = autoclock;
  424. atomic_set(&chip->active, 1); /* avoid autopm during probing */
  425. atomic_set(&chip->usage_count, 0);
  426. atomic_set(&chip->shutdown, 0);
  427. chip->usb_id = usb_id;
  428. INIT_LIST_HEAD(&chip->pcm_list);
  429. INIT_LIST_HEAD(&chip->ep_list);
  430. INIT_LIST_HEAD(&chip->midi_list);
  431. INIT_LIST_HEAD(&chip->mixer_list);
  432. card->private_free = snd_usb_audio_free;
  433. strcpy(card->driver, "USB-Audio");
  434. sprintf(component, "USB%04x:%04x",
  435. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  436. snd_component_add(card, component);
  437. usb_audio_make_shortname(dev, chip, quirk);
  438. usb_audio_make_longname(dev, chip, quirk);
  439. snd_usb_audio_create_proc(chip);
  440. *rchip = chip;
  441. return 0;
  442. }
  443. /* look for a matching quirk alias id */
  444. static bool get_alias_id(struct usb_device *dev, unsigned int *id)
  445. {
  446. int i;
  447. unsigned int src, dst;
  448. for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
  449. if (!quirk_alias[i] ||
  450. sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
  451. src != *id)
  452. continue;
  453. dev_info(&dev->dev,
  454. "device (%04x:%04x): applying quirk alias %04x:%04x\n",
  455. USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
  456. USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
  457. *id = dst;
  458. return true;
  459. }
  460. return false;
  461. }
  462. static const struct usb_device_id usb_audio_ids[]; /* defined below */
  463. /* look for the corresponding quirk */
  464. static const struct snd_usb_audio_quirk *
  465. get_alias_quirk(struct usb_device *dev, unsigned int id)
  466. {
  467. const struct usb_device_id *p;
  468. for (p = usb_audio_ids; p->match_flags; p++) {
  469. /* FIXME: this checks only vendor:product pair in the list */
  470. if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
  471. USB_DEVICE_ID_MATCH_DEVICE &&
  472. p->idVendor == USB_ID_VENDOR(id) &&
  473. p->idProduct == USB_ID_PRODUCT(id))
  474. return (const struct snd_usb_audio_quirk *)p->driver_info;
  475. }
  476. return NULL;
  477. }
  478. /*
  479. * probe the active usb device
  480. *
  481. * note that this can be called multiple times per a device, when it
  482. * includes multiple audio control interfaces.
  483. *
  484. * thus we check the usb device pointer and creates the card instance
  485. * only at the first time. the successive calls of this function will
  486. * append the pcm interface to the corresponding card.
  487. */
  488. static int usb_audio_probe(struct usb_interface *intf,
  489. const struct usb_device_id *usb_id)
  490. {
  491. struct usb_device *dev = interface_to_usbdev(intf);
  492. const struct snd_usb_audio_quirk *quirk =
  493. (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  494. struct snd_usb_audio *chip;
  495. int i, err;
  496. struct usb_host_interface *alts;
  497. int ifnum;
  498. u32 id;
  499. alts = &intf->altsetting[0];
  500. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  501. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  502. le16_to_cpu(dev->descriptor.idProduct));
  503. if (get_alias_id(dev, &id))
  504. quirk = get_alias_quirk(dev, id);
  505. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  506. return -ENXIO;
  507. err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
  508. if (err < 0)
  509. return err;
  510. /*
  511. * found a config. now register to ALSA
  512. */
  513. /* check whether it's already registered */
  514. chip = NULL;
  515. mutex_lock(&register_mutex);
  516. for (i = 0; i < SNDRV_CARDS; i++) {
  517. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  518. if (atomic_read(&usb_chip[i]->shutdown)) {
  519. dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
  520. err = -EIO;
  521. goto __error;
  522. }
  523. chip = usb_chip[i];
  524. atomic_inc(&chip->active); /* avoid autopm */
  525. break;
  526. }
  527. }
  528. if (! chip) {
  529. /* it's a fresh one.
  530. * now look for an empty slot and create a new card instance
  531. */
  532. for (i = 0; i < SNDRV_CARDS; i++)
  533. if (!usb_chip[i] &&
  534. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  535. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  536. if (enable[i]) {
  537. err = snd_usb_audio_create(intf, dev, i, quirk,
  538. id, &chip);
  539. if (err < 0)
  540. goto __error;
  541. chip->pm_intf = intf;
  542. break;
  543. } else if (vid[i] != -1 || pid[i] != -1) {
  544. dev_info(&dev->dev,
  545. "device (%04x:%04x) is disabled\n",
  546. USB_ID_VENDOR(id),
  547. USB_ID_PRODUCT(id));
  548. err = -ENOENT;
  549. goto __error;
  550. }
  551. }
  552. if (!chip) {
  553. dev_err(&dev->dev, "no available usb audio device\n");
  554. err = -ENODEV;
  555. goto __error;
  556. }
  557. }
  558. dev_set_drvdata(&dev->dev, chip);
  559. /*
  560. * For devices with more than one control interface, we assume the
  561. * first contains the audio controls. We might need a more specific
  562. * check here in the future.
  563. */
  564. if (!chip->ctrl_intf)
  565. chip->ctrl_intf = alts;
  566. chip->txfr_quirk = 0;
  567. err = 1; /* continue */
  568. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  569. /* need some special handlings */
  570. err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
  571. if (err < 0)
  572. goto __error;
  573. }
  574. if (err > 0) {
  575. /* create normal USB audio interfaces */
  576. err = snd_usb_create_streams(chip, ifnum);
  577. if (err < 0)
  578. goto __error;
  579. err = snd_usb_create_mixer(chip, ifnum, ignore_ctl_error);
  580. if (err < 0)
  581. goto __error;
  582. }
  583. /* we are allowed to call snd_card_register() many times, but first
  584. * check to see if a device needs to skip it or do anything special
  585. */
  586. if (!snd_usb_registration_quirk(chip, ifnum)) {
  587. err = snd_card_register(chip->card);
  588. if (err < 0)
  589. goto __error;
  590. }
  591. usb_chip[chip->index] = chip;
  592. chip->num_interfaces++;
  593. usb_set_intfdata(intf, chip);
  594. atomic_dec(&chip->active);
  595. mutex_unlock(&register_mutex);
  596. return 0;
  597. __error:
  598. if (chip) {
  599. /* chip->active is inside the chip->card object,
  600. * decrement before memory is possibly returned.
  601. */
  602. atomic_dec(&chip->active);
  603. if (!chip->num_interfaces)
  604. snd_card_free(chip->card);
  605. }
  606. mutex_unlock(&register_mutex);
  607. return err;
  608. }
  609. /*
  610. * we need to take care of counter, since disconnection can be called also
  611. * many times as well as usb_audio_probe().
  612. */
  613. static void usb_audio_disconnect(struct usb_interface *intf)
  614. {
  615. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  616. struct snd_card *card;
  617. struct list_head *p;
  618. if (chip == USB_AUDIO_IFACE_UNUSED)
  619. return;
  620. card = chip->card;
  621. mutex_lock(&register_mutex);
  622. if (atomic_inc_return(&chip->shutdown) == 1) {
  623. struct snd_usb_stream *as;
  624. struct snd_usb_endpoint *ep;
  625. struct usb_mixer_interface *mixer;
  626. /* wait until all pending tasks done;
  627. * they are protected by snd_usb_lock_shutdown()
  628. */
  629. wait_event(chip->shutdown_wait,
  630. !atomic_read(&chip->usage_count));
  631. snd_card_disconnect(card);
  632. /* release the pcm resources */
  633. list_for_each_entry(as, &chip->pcm_list, list) {
  634. snd_usb_stream_disconnect(as);
  635. }
  636. /* release the endpoint resources */
  637. list_for_each_entry(ep, &chip->ep_list, list) {
  638. snd_usb_endpoint_release(ep);
  639. }
  640. /* release the midi resources */
  641. list_for_each(p, &chip->midi_list) {
  642. snd_usbmidi_disconnect(p);
  643. }
  644. /* release mixer resources */
  645. list_for_each_entry(mixer, &chip->mixer_list, list) {
  646. snd_usb_mixer_disconnect(mixer);
  647. }
  648. }
  649. chip->num_interfaces--;
  650. if (chip->num_interfaces <= 0) {
  651. usb_chip[chip->index] = NULL;
  652. mutex_unlock(&register_mutex);
  653. snd_card_free_when_closed(card);
  654. } else {
  655. mutex_unlock(&register_mutex);
  656. }
  657. }
  658. /* lock the shutdown (disconnect) task and autoresume */
  659. int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
  660. {
  661. int err;
  662. atomic_inc(&chip->usage_count);
  663. if (atomic_read(&chip->shutdown)) {
  664. err = -EIO;
  665. goto error;
  666. }
  667. err = snd_usb_autoresume(chip);
  668. if (err < 0)
  669. goto error;
  670. return 0;
  671. error:
  672. if (atomic_dec_and_test(&chip->usage_count))
  673. wake_up(&chip->shutdown_wait);
  674. return err;
  675. }
  676. /* autosuspend and unlock the shutdown */
  677. void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
  678. {
  679. snd_usb_autosuspend(chip);
  680. if (atomic_dec_and_test(&chip->usage_count))
  681. wake_up(&chip->shutdown_wait);
  682. }
  683. #ifdef CONFIG_PM
  684. int snd_usb_autoresume(struct snd_usb_audio *chip)
  685. {
  686. if (atomic_read(&chip->shutdown))
  687. return -EIO;
  688. if (atomic_inc_return(&chip->active) == 1)
  689. return usb_autopm_get_interface(chip->pm_intf);
  690. return 0;
  691. }
  692. void snd_usb_autosuspend(struct snd_usb_audio *chip)
  693. {
  694. if (atomic_read(&chip->shutdown))
  695. return;
  696. if (atomic_dec_and_test(&chip->active))
  697. usb_autopm_put_interface(chip->pm_intf);
  698. }
  699. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  700. {
  701. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  702. struct snd_usb_stream *as;
  703. struct usb_mixer_interface *mixer;
  704. struct list_head *p;
  705. if (chip == USB_AUDIO_IFACE_UNUSED)
  706. return 0;
  707. if (!chip->num_suspended_intf++) {
  708. list_for_each_entry(as, &chip->pcm_list, list) {
  709. snd_pcm_suspend_all(as->pcm);
  710. snd_usb_pcm_suspend(as);
  711. as->substream[0].need_setup_ep =
  712. as->substream[1].need_setup_ep = true;
  713. }
  714. list_for_each(p, &chip->midi_list)
  715. snd_usbmidi_suspend(p);
  716. list_for_each_entry(mixer, &chip->mixer_list, list)
  717. snd_usb_mixer_suspend(mixer);
  718. }
  719. if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
  720. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  721. chip->system_suspend = chip->num_suspended_intf;
  722. }
  723. return 0;
  724. }
  725. static int __usb_audio_resume(struct usb_interface *intf, bool reset_resume)
  726. {
  727. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  728. struct snd_usb_stream *as;
  729. struct usb_mixer_interface *mixer;
  730. struct list_head *p;
  731. int err = 0;
  732. if (chip == USB_AUDIO_IFACE_UNUSED)
  733. return 0;
  734. atomic_inc(&chip->active); /* avoid autopm */
  735. if (chip->num_suspended_intf > 1)
  736. goto out;
  737. list_for_each_entry(as, &chip->pcm_list, list) {
  738. err = snd_usb_pcm_resume(as);
  739. if (err < 0)
  740. goto err_out;
  741. }
  742. /*
  743. * ALSA leaves material resumption to user space
  744. * we just notify and restart the mixers
  745. */
  746. list_for_each_entry(mixer, &chip->mixer_list, list) {
  747. err = snd_usb_mixer_resume(mixer, reset_resume);
  748. if (err < 0)
  749. goto err_out;
  750. }
  751. list_for_each(p, &chip->midi_list) {
  752. snd_usbmidi_resume(p);
  753. }
  754. out:
  755. if (chip->num_suspended_intf == chip->system_suspend) {
  756. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  757. chip->system_suspend = 0;
  758. }
  759. chip->num_suspended_intf--;
  760. err_out:
  761. atomic_dec(&chip->active); /* allow autopm after this point */
  762. return err;
  763. }
  764. static int usb_audio_resume(struct usb_interface *intf)
  765. {
  766. return __usb_audio_resume(intf, false);
  767. }
  768. static int usb_audio_reset_resume(struct usb_interface *intf)
  769. {
  770. return __usb_audio_resume(intf, true);
  771. }
  772. #else
  773. #define usb_audio_suspend NULL
  774. #define usb_audio_resume NULL
  775. #define usb_audio_reset_resume NULL
  776. #endif /* CONFIG_PM */
  777. static const struct usb_device_id usb_audio_ids [] = {
  778. #include "quirks-table.h"
  779. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  780. .bInterfaceClass = USB_CLASS_AUDIO,
  781. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  782. { } /* Terminating entry */
  783. };
  784. MODULE_DEVICE_TABLE(usb, usb_audio_ids);
  785. /*
  786. * entry point for linux usb interface
  787. */
  788. static struct usb_driver usb_audio_driver = {
  789. .name = "snd-usb-audio",
  790. .probe = usb_audio_probe,
  791. .disconnect = usb_audio_disconnect,
  792. .suspend = usb_audio_suspend,
  793. .resume = usb_audio_resume,
  794. .reset_resume = usb_audio_reset_resume,
  795. .id_table = usb_audio_ids,
  796. .supports_autosuspend = 1,
  797. };
  798. module_usb_driver(usb_audio_driver);