card.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /*
  2. * (Tentative) USB Audio Driver for ALSA
  3. *
  4. * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
  5. *
  6. * Many codes borrowed from audio.c by
  7. * Alan Cox (alan@lxorguk.ukuu.org.uk)
  8. * Thomas Sailer (sailer@ife.ee.ethz.ch)
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. *
  26. * NOTES:
  27. *
  28. * - the linked URBs would be preferred but not used so far because of
  29. * the instability of unlinking.
  30. * - type II is not supported properly. there is no device which supports
  31. * this type *correctly*. SB extigy looks as if it supports, but it's
  32. * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
  33. */
  34. #include <linux/bitops.h>
  35. #include <linux/init.h>
  36. #include <linux/list.h>
  37. #include <linux/slab.h>
  38. #include <linux/string.h>
  39. #include <linux/ctype.h>
  40. #include <linux/usb.h>
  41. #include <linux/moduleparam.h>
  42. #include <linux/mutex.h>
  43. #include <linux/usb/audio.h>
  44. #include <linux/usb/audio-v2.h>
  45. #include <linux/module.h>
  46. #include <sound/control.h>
  47. #include <sound/core.h>
  48. #include <sound/info.h>
  49. #include <sound/pcm.h>
  50. #include <sound/pcm_params.h>
  51. #include <sound/initval.h>
  52. #include "usbaudio.h"
  53. #include "card.h"
  54. #include "midi.h"
  55. #include "mixer.h"
  56. #include "proc.h"
  57. #include "quirks.h"
  58. #include "endpoint.h"
  59. #include "helper.h"
  60. #include "debug.h"
  61. #include "pcm.h"
  62. #include "format.h"
  63. #include "power.h"
  64. #include "stream.h"
  65. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  66. MODULE_DESCRIPTION("USB Audio");
  67. MODULE_LICENSE("GPL");
  68. MODULE_SUPPORTED_DEVICE("{{Generic,USB Audio}}");
  69. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  70. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  71. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
  72. /* Vendor/product IDs for this card */
  73. static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  74. static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  75. static int nrpacks = 8; /* max. number of packets per urb */
  76. static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
  77. static bool ignore_ctl_error;
  78. static bool autoclock = true;
  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_array(device_setup, int, NULL, 0444);
  92. MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
  93. module_param(ignore_ctl_error, bool, 0444);
  94. MODULE_PARM_DESC(ignore_ctl_error,
  95. "Ignore errors from USB controller for mixer interfaces.");
  96. module_param(autoclock, bool, 0444);
  97. MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
  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. subs->interface = -1;
  120. subs->data_endpoint = NULL;
  121. subs->sync_endpoint = NULL;
  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_interface(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. /*
  219. * Firmware writers cannot count to three. So to find
  220. * the IAD on the NuForce UDH-100, also check the next
  221. * interface.
  222. */
  223. struct usb_interface *iface =
  224. usb_ifnum_to_if(dev, ctrlif + 1);
  225. if (iface &&
  226. iface->intf_assoc &&
  227. iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
  228. iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
  229. assoc = iface->intf_assoc;
  230. }
  231. if (!assoc) {
  232. snd_printk(KERN_ERR "Audio class v2 interfaces need an interface association\n");
  233. return -EINVAL;
  234. }
  235. for (i = 0; i < assoc->bInterfaceCount; i++) {
  236. int intf = assoc->bFirstInterface + i;
  237. if (intf != ctrlif)
  238. snd_usb_create_stream(chip, ctrlif, intf);
  239. }
  240. break;
  241. }
  242. }
  243. return 0;
  244. }
  245. /*
  246. * free the chip instance
  247. *
  248. * here we have to do not much, since pcm and controls are already freed
  249. *
  250. */
  251. static int snd_usb_audio_free(struct snd_usb_audio *chip)
  252. {
  253. mutex_destroy(&chip->mutex);
  254. kfree(chip);
  255. return 0;
  256. }
  257. static int snd_usb_audio_dev_free(struct snd_device *device)
  258. {
  259. struct snd_usb_audio *chip = device->device_data;
  260. return snd_usb_audio_free(chip);
  261. }
  262. static void remove_trailing_spaces(char *str)
  263. {
  264. char *p;
  265. if (!*str)
  266. return;
  267. for (p = str + strlen(str) - 1; p >= str && isspace(*p); p--)
  268. *p = 0;
  269. }
  270. /*
  271. * create a chip instance and set its names.
  272. */
  273. static int snd_usb_audio_create(struct usb_device *dev, int idx,
  274. const struct snd_usb_audio_quirk *quirk,
  275. struct snd_usb_audio **rchip)
  276. {
  277. struct snd_card *card;
  278. struct snd_usb_audio *chip;
  279. int err, len;
  280. char component[14];
  281. static struct snd_device_ops ops = {
  282. .dev_free = snd_usb_audio_dev_free,
  283. };
  284. *rchip = NULL;
  285. switch (snd_usb_get_speed(dev)) {
  286. case USB_SPEED_LOW:
  287. case USB_SPEED_FULL:
  288. case USB_SPEED_HIGH:
  289. case USB_SPEED_SUPER:
  290. break;
  291. default:
  292. snd_printk(KERN_ERR "unknown device speed %d\n", snd_usb_get_speed(dev));
  293. return -ENXIO;
  294. }
  295. err = snd_card_create(index[idx], id[idx], THIS_MODULE, 0, &card);
  296. if (err < 0) {
  297. snd_printk(KERN_ERR "cannot create card instance %d\n", idx);
  298. return err;
  299. }
  300. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  301. if (! chip) {
  302. snd_card_free(card);
  303. return -ENOMEM;
  304. }
  305. mutex_init(&chip->mutex);
  306. init_rwsem(&chip->shutdown_rwsem);
  307. chip->index = idx;
  308. chip->dev = dev;
  309. chip->card = card;
  310. chip->setup = device_setup[idx];
  311. chip->nrpacks = nrpacks;
  312. chip->autoclock = autoclock;
  313. chip->probing = 1;
  314. chip->usb_id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  315. le16_to_cpu(dev->descriptor.idProduct));
  316. INIT_LIST_HEAD(&chip->pcm_list);
  317. INIT_LIST_HEAD(&chip->ep_list);
  318. INIT_LIST_HEAD(&chip->midi_list);
  319. INIT_LIST_HEAD(&chip->mixer_list);
  320. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  321. snd_usb_audio_free(chip);
  322. snd_card_free(card);
  323. return err;
  324. }
  325. strcpy(card->driver, "USB-Audio");
  326. sprintf(component, "USB%04x:%04x",
  327. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  328. snd_component_add(card, component);
  329. /* retrieve the device string as shortname */
  330. if (quirk && quirk->product_name && *quirk->product_name) {
  331. strlcpy(card->shortname, quirk->product_name, sizeof(card->shortname));
  332. } else {
  333. if (!dev->descriptor.iProduct ||
  334. usb_string(dev, dev->descriptor.iProduct,
  335. card->shortname, sizeof(card->shortname)) <= 0) {
  336. /* no name available from anywhere, so use ID */
  337. sprintf(card->shortname, "USB Device %#04x:%#04x",
  338. USB_ID_VENDOR(chip->usb_id),
  339. USB_ID_PRODUCT(chip->usb_id));
  340. }
  341. }
  342. remove_trailing_spaces(card->shortname);
  343. /* retrieve the vendor and device strings as longname */
  344. if (quirk && quirk->vendor_name && *quirk->vendor_name) {
  345. len = strlcpy(card->longname, quirk->vendor_name, sizeof(card->longname));
  346. } else {
  347. if (dev->descriptor.iManufacturer)
  348. len = usb_string(dev, dev->descriptor.iManufacturer,
  349. card->longname, sizeof(card->longname));
  350. else
  351. len = 0;
  352. /* we don't really care if there isn't any vendor string */
  353. }
  354. if (len > 0) {
  355. remove_trailing_spaces(card->longname);
  356. if (*card->longname)
  357. strlcat(card->longname, " ", sizeof(card->longname));
  358. }
  359. strlcat(card->longname, card->shortname, sizeof(card->longname));
  360. len = strlcat(card->longname, " at ", sizeof(card->longname));
  361. if (len < sizeof(card->longname))
  362. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  363. switch (snd_usb_get_speed(dev)) {
  364. case USB_SPEED_LOW:
  365. strlcat(card->longname, ", low speed", sizeof(card->longname));
  366. break;
  367. case USB_SPEED_FULL:
  368. strlcat(card->longname, ", full speed", sizeof(card->longname));
  369. break;
  370. case USB_SPEED_HIGH:
  371. strlcat(card->longname, ", high speed", sizeof(card->longname));
  372. break;
  373. case USB_SPEED_SUPER:
  374. strlcat(card->longname, ", super speed", sizeof(card->longname));
  375. break;
  376. default:
  377. break;
  378. }
  379. snd_usb_audio_create_proc(chip);
  380. *rchip = chip;
  381. return 0;
  382. }
  383. /*
  384. * probe the active usb device
  385. *
  386. * note that this can be called multiple times per a device, when it
  387. * includes multiple audio control interfaces.
  388. *
  389. * thus we check the usb device pointer and creates the card instance
  390. * only at the first time. the successive calls of this function will
  391. * append the pcm interface to the corresponding card.
  392. */
  393. static struct snd_usb_audio *
  394. snd_usb_audio_probe(struct usb_device *dev,
  395. struct usb_interface *intf,
  396. const struct usb_device_id *usb_id)
  397. {
  398. const struct snd_usb_audio_quirk *quirk = (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  399. int i, err;
  400. struct snd_usb_audio *chip;
  401. struct usb_host_interface *alts;
  402. int ifnum;
  403. u32 id;
  404. alts = &intf->altsetting[0];
  405. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  406. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  407. le16_to_cpu(dev->descriptor.idProduct));
  408. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  409. goto __err_val;
  410. if (snd_usb_apply_boot_quirk(dev, intf, quirk) < 0)
  411. goto __err_val;
  412. /*
  413. * found a config. now register to ALSA
  414. */
  415. /* check whether it's already registered */
  416. chip = NULL;
  417. mutex_lock(&register_mutex);
  418. for (i = 0; i < SNDRV_CARDS; i++) {
  419. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  420. if (usb_chip[i]->shutdown) {
  421. snd_printk(KERN_ERR "USB device is in the shutdown state, cannot create a card instance\n");
  422. goto __error;
  423. }
  424. chip = usb_chip[i];
  425. chip->probing = 1;
  426. break;
  427. }
  428. }
  429. if (! chip) {
  430. /* it's a fresh one.
  431. * now look for an empty slot and create a new card instance
  432. */
  433. for (i = 0; i < SNDRV_CARDS; i++)
  434. if (enable[i] && ! usb_chip[i] &&
  435. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  436. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  437. if (snd_usb_audio_create(dev, i, quirk, &chip) < 0) {
  438. goto __error;
  439. }
  440. snd_card_set_dev(chip->card, &intf->dev);
  441. chip->pm_intf = intf;
  442. break;
  443. }
  444. if (!chip) {
  445. printk(KERN_ERR "no available usb audio device\n");
  446. goto __error;
  447. }
  448. }
  449. /*
  450. * For devices with more than one control interface, we assume the
  451. * first contains the audio controls. We might need a more specific
  452. * check here in the future.
  453. */
  454. if (!chip->ctrl_intf)
  455. chip->ctrl_intf = alts;
  456. chip->txfr_quirk = 0;
  457. err = 1; /* continue */
  458. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  459. /* need some special handlings */
  460. if ((err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk)) < 0)
  461. goto __error;
  462. }
  463. if (err > 0) {
  464. /* create normal USB audio interfaces */
  465. if (snd_usb_create_streams(chip, ifnum) < 0 ||
  466. snd_usb_create_mixer(chip, ifnum, ignore_ctl_error) < 0) {
  467. goto __error;
  468. }
  469. }
  470. /* we are allowed to call snd_card_register() many times */
  471. if (snd_card_register(chip->card) < 0) {
  472. goto __error;
  473. }
  474. usb_chip[chip->index] = chip;
  475. chip->num_interfaces++;
  476. chip->probing = 0;
  477. mutex_unlock(&register_mutex);
  478. return chip;
  479. __error:
  480. if (chip) {
  481. if (!chip->num_interfaces)
  482. snd_card_free(chip->card);
  483. chip->probing = 0;
  484. }
  485. mutex_unlock(&register_mutex);
  486. __err_val:
  487. return NULL;
  488. }
  489. /*
  490. * we need to take care of counter, since disconnection can be called also
  491. * many times as well as usb_audio_probe().
  492. */
  493. static void snd_usb_audio_disconnect(struct usb_device *dev,
  494. struct snd_usb_audio *chip)
  495. {
  496. struct snd_card *card;
  497. struct list_head *p, *n;
  498. if (chip == (void *)-1L)
  499. return;
  500. card = chip->card;
  501. down_write(&chip->shutdown_rwsem);
  502. chip->shutdown = 1;
  503. up_write(&chip->shutdown_rwsem);
  504. mutex_lock(&register_mutex);
  505. chip->num_interfaces--;
  506. if (chip->num_interfaces <= 0) {
  507. snd_card_disconnect(card);
  508. /* release the pcm resources */
  509. list_for_each(p, &chip->pcm_list) {
  510. snd_usb_stream_disconnect(p);
  511. }
  512. /* release the endpoint resources */
  513. list_for_each_safe(p, n, &chip->ep_list) {
  514. snd_usb_endpoint_free(p);
  515. }
  516. /* release the midi resources */
  517. list_for_each(p, &chip->midi_list) {
  518. snd_usbmidi_disconnect(p);
  519. }
  520. /* release mixer resources */
  521. list_for_each(p, &chip->mixer_list) {
  522. snd_usb_mixer_disconnect(p);
  523. }
  524. usb_chip[chip->index] = NULL;
  525. mutex_unlock(&register_mutex);
  526. snd_card_free_when_closed(card);
  527. } else {
  528. mutex_unlock(&register_mutex);
  529. }
  530. }
  531. /*
  532. * new 2.5 USB kernel API
  533. */
  534. static int usb_audio_probe(struct usb_interface *intf,
  535. const struct usb_device_id *id)
  536. {
  537. struct snd_usb_audio *chip;
  538. chip = snd_usb_audio_probe(interface_to_usbdev(intf), intf, id);
  539. if (chip) {
  540. usb_set_intfdata(intf, chip);
  541. return 0;
  542. } else
  543. return -EIO;
  544. }
  545. static void usb_audio_disconnect(struct usb_interface *intf)
  546. {
  547. snd_usb_audio_disconnect(interface_to_usbdev(intf),
  548. usb_get_intfdata(intf));
  549. }
  550. #ifdef CONFIG_PM
  551. int snd_usb_autoresume(struct snd_usb_audio *chip)
  552. {
  553. int err = -ENODEV;
  554. down_read(&chip->shutdown_rwsem);
  555. if (chip->probing)
  556. err = 0;
  557. else if (!chip->shutdown)
  558. err = usb_autopm_get_interface(chip->pm_intf);
  559. up_read(&chip->shutdown_rwsem);
  560. return err;
  561. }
  562. void snd_usb_autosuspend(struct snd_usb_audio *chip)
  563. {
  564. down_read(&chip->shutdown_rwsem);
  565. if (!chip->shutdown && !chip->probing)
  566. usb_autopm_put_interface(chip->pm_intf);
  567. up_read(&chip->shutdown_rwsem);
  568. }
  569. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  570. {
  571. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  572. struct snd_usb_stream *as;
  573. struct usb_mixer_interface *mixer;
  574. if (chip == (void *)-1L)
  575. return 0;
  576. if (!PMSG_IS_AUTO(message)) {
  577. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  578. if (!chip->num_suspended_intf++) {
  579. list_for_each_entry(as, &chip->pcm_list, list) {
  580. snd_pcm_suspend_all(as->pcm);
  581. as->substream[0].need_setup_ep =
  582. as->substream[1].need_setup_ep = true;
  583. }
  584. }
  585. } else {
  586. /*
  587. * otherwise we keep the rest of the system in the dark
  588. * to keep this transparent
  589. */
  590. if (!chip->num_suspended_intf++)
  591. chip->autosuspended = 1;
  592. }
  593. list_for_each_entry(mixer, &chip->mixer_list, list)
  594. snd_usb_mixer_inactivate(mixer);
  595. return 0;
  596. }
  597. static int usb_audio_resume(struct usb_interface *intf)
  598. {
  599. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  600. struct usb_mixer_interface *mixer;
  601. int err = 0;
  602. if (chip == (void *)-1L)
  603. return 0;
  604. if (--chip->num_suspended_intf)
  605. return 0;
  606. /*
  607. * ALSA leaves material resumption to user space
  608. * we just notify and restart the mixers
  609. */
  610. list_for_each_entry(mixer, &chip->mixer_list, list) {
  611. err = snd_usb_mixer_activate(mixer);
  612. if (err < 0)
  613. goto err_out;
  614. }
  615. if (!chip->autosuspended)
  616. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  617. chip->autosuspended = 0;
  618. err_out:
  619. return err;
  620. }
  621. #else
  622. #define usb_audio_suspend NULL
  623. #define usb_audio_resume NULL
  624. #endif /* CONFIG_PM */
  625. static struct usb_device_id usb_audio_ids [] = {
  626. #include "quirks-table.h"
  627. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  628. .bInterfaceClass = USB_CLASS_AUDIO,
  629. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  630. { } /* Terminating entry */
  631. };
  632. MODULE_DEVICE_TABLE(usb, usb_audio_ids);
  633. /*
  634. * entry point for linux usb interface
  635. */
  636. static struct usb_driver usb_audio_driver = {
  637. .name = "snd-usb-audio",
  638. .probe = usb_audio_probe,
  639. .disconnect = usb_audio_disconnect,
  640. .suspend = usb_audio_suspend,
  641. .resume = usb_audio_resume,
  642. .id_table = usb_audio_ids,
  643. .supports_autosuspend = 1,
  644. };
  645. static int __init snd_usb_audio_init(void)
  646. {
  647. if (nrpacks < 1 || nrpacks > MAX_PACKS) {
  648. printk(KERN_WARNING "invalid nrpacks value.\n");
  649. return -EINVAL;
  650. }
  651. return usb_register(&usb_audio_driver);
  652. }
  653. static void __exit snd_usb_audio_cleanup(void)
  654. {
  655. usb_deregister(&usb_audio_driver);
  656. }
  657. module_init(snd_usb_audio_init);
  658. module_exit(snd_usb_audio_cleanup);