card.c 19 KB

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