card.c 19 KB

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