card.c 21 KB

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