card.c 21 KB

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