card.c 19 KB

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