card.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * (Tentative) USB Audio Driver for ALSA
  4. *
  5. * Copyright (c) 2002 by Takashi Iwai <[email protected]>
  6. *
  7. * Many codes borrowed from audio.c by
  8. * Alan Cox ([email protected])
  9. * Thomas Sailer ([email protected])
  10. *
  11. * Audio Class 3.0 support by Ruslan Bilovol <[email protected]>
  12. *
  13. * NOTES:
  14. *
  15. * - the linked URBs would be preferred but not used so far because of
  16. * the instability of unlinking.
  17. * - type II is not supported properly. there is no device which supports
  18. * this type *correctly*. SB extigy looks as if it supports, but it's
  19. * indeed an AC3 stream packed in SPDIF frames (i.e. no real AC3 stream).
  20. */
  21. #include <linux/bitops.h>
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include <linux/ctype.h>
  27. #include <linux/usb.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mutex.h>
  30. #include <linux/usb/audio.h>
  31. #include <linux/usb/audio-v2.h>
  32. #include <linux/usb/audio-v3.h>
  33. #include <linux/module.h>
  34. #include <sound/control.h>
  35. #include <sound/core.h>
  36. #include <sound/info.h>
  37. #include <sound/pcm.h>
  38. #include <sound/pcm_params.h>
  39. #include <sound/initval.h>
  40. #include "usbaudio.h"
  41. #include "card.h"
  42. #include "midi.h"
  43. #include "mixer.h"
  44. #include "proc.h"
  45. #include "quirks.h"
  46. #include "endpoint.h"
  47. #include "helper.h"
  48. #include "pcm.h"
  49. #include "format.h"
  50. #include "power.h"
  51. #include "stream.h"
  52. #include "media.h"
  53. #include <trace/hooks/audio_usboffload.h>
  54. MODULE_AUTHOR("Takashi Iwai <[email protected]>");
  55. MODULE_DESCRIPTION("USB Audio");
  56. MODULE_LICENSE("GPL");
  57. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  58. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  59. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;/* Enable this card */
  60. /* Vendor/product IDs for this card */
  61. static int vid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  62. static int pid[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = -1 };
  63. static int device_setup[SNDRV_CARDS]; /* device parameter for this card */
  64. static bool ignore_ctl_error;
  65. static bool autoclock = true;
  66. static bool lowlatency = true;
  67. static char *quirk_alias[SNDRV_CARDS];
  68. static char *delayed_register[SNDRV_CARDS];
  69. static bool implicit_fb[SNDRV_CARDS];
  70. static unsigned int quirk_flags[SNDRV_CARDS];
  71. bool snd_usb_use_vmalloc = true;
  72. bool snd_usb_skip_validation;
  73. module_param_array(index, int, NULL, 0444);
  74. MODULE_PARM_DESC(index, "Index value for the USB audio adapter.");
  75. module_param_array(id, charp, NULL, 0444);
  76. MODULE_PARM_DESC(id, "ID string for the USB audio adapter.");
  77. module_param_array(enable, bool, NULL, 0444);
  78. MODULE_PARM_DESC(enable, "Enable USB audio adapter.");
  79. module_param_array(vid, int, NULL, 0444);
  80. MODULE_PARM_DESC(vid, "Vendor ID for the USB audio device.");
  81. module_param_array(pid, int, NULL, 0444);
  82. MODULE_PARM_DESC(pid, "Product ID for the USB audio device.");
  83. module_param_array(device_setup, int, NULL, 0444);
  84. MODULE_PARM_DESC(device_setup, "Specific device setup (if needed).");
  85. module_param(ignore_ctl_error, bool, 0444);
  86. MODULE_PARM_DESC(ignore_ctl_error,
  87. "Ignore errors from USB controller for mixer interfaces.");
  88. module_param(autoclock, bool, 0444);
  89. MODULE_PARM_DESC(autoclock, "Enable auto-clock selection for UAC2 devices (default: yes).");
  90. module_param(lowlatency, bool, 0444);
  91. MODULE_PARM_DESC(lowlatency, "Enable low latency playback (default: yes).");
  92. module_param_array(quirk_alias, charp, NULL, 0444);
  93. MODULE_PARM_DESC(quirk_alias, "Quirk aliases, e.g. 0123abcd:5678beef.");
  94. module_param_array(delayed_register, charp, NULL, 0444);
  95. MODULE_PARM_DESC(delayed_register, "Quirk for delayed registration, given by id:iface, e.g. 0123abcd:4.");
  96. module_param_array(implicit_fb, bool, NULL, 0444);
  97. MODULE_PARM_DESC(implicit_fb, "Apply generic implicit feedback sync mode.");
  98. module_param_array(quirk_flags, uint, NULL, 0444);
  99. MODULE_PARM_DESC(quirk_flags, "Driver quirk bit flags.");
  100. module_param_named(use_vmalloc, snd_usb_use_vmalloc, bool, 0444);
  101. MODULE_PARM_DESC(use_vmalloc, "Use vmalloc for PCM intermediate buffers (default: yes).");
  102. module_param_named(skip_validation, snd_usb_skip_validation, bool, 0444);
  103. MODULE_PARM_DESC(skip_validation, "Skip unit descriptor validation (default: no).");
  104. /*
  105. * we keep the snd_usb_audio_t instances by ourselves for merging
  106. * the all interfaces on the same card as one sound device.
  107. */
  108. static DEFINE_MUTEX(register_mutex);
  109. static struct snd_usb_audio *usb_chip[SNDRV_CARDS];
  110. static struct usb_driver usb_audio_driver;
  111. static struct snd_usb_audio_vendor_ops *usb_vendor_ops;
  112. int snd_vendor_set_ops(struct snd_usb_audio_vendor_ops *ops)
  113. {
  114. if ((!ops->set_interface) ||
  115. (!ops->set_pcm_intf) ||
  116. (!ops->set_pcm_connection))
  117. return -EINVAL;
  118. usb_vendor_ops = ops;
  119. return 0;
  120. }
  121. EXPORT_SYMBOL_GPL(snd_vendor_set_ops);
  122. struct snd_usb_audio_vendor_ops *snd_vendor_get_ops(void)
  123. {
  124. return usb_vendor_ops;
  125. }
  126. int snd_vendor_set_interface(struct usb_device *udev,
  127. struct usb_host_interface *intf,
  128. int iface, int alt)
  129. {
  130. struct snd_usb_audio_vendor_ops *ops = snd_vendor_get_ops();
  131. if (ops)
  132. return ops->set_interface(udev, intf, iface, alt);
  133. return 0;
  134. }
  135. int snd_vendor_set_pcm_intf(struct usb_interface *intf, int iface, int alt,
  136. int direction, struct snd_usb_substream *subs)
  137. {
  138. struct snd_usb_audio_vendor_ops *ops = snd_vendor_get_ops();
  139. if (ops)
  140. return ops->set_pcm_intf(intf, iface, alt, direction, subs);
  141. return 0;
  142. }
  143. int snd_vendor_set_pcm_connection(struct usb_device *udev,
  144. enum snd_vendor_pcm_open_close onoff,
  145. int direction)
  146. {
  147. struct snd_usb_audio_vendor_ops *ops = snd_vendor_get_ops();
  148. if (ops)
  149. return ops->set_pcm_connection(udev, onoff, direction);
  150. return 0;
  151. }
  152. /*
  153. * disconnect streams
  154. * called from usb_audio_disconnect()
  155. */
  156. static void snd_usb_stream_disconnect(struct snd_usb_stream *as)
  157. {
  158. int idx;
  159. struct snd_usb_substream *subs;
  160. for (idx = 0; idx < 2; idx++) {
  161. subs = &as->substream[idx];
  162. if (!subs->num_formats)
  163. continue;
  164. subs->data_endpoint = NULL;
  165. subs->sync_endpoint = NULL;
  166. }
  167. }
  168. static int snd_usb_create_stream(struct snd_usb_audio *chip, int ctrlif, int interface)
  169. {
  170. struct usb_device *dev = chip->dev;
  171. struct usb_host_interface *alts;
  172. struct usb_interface_descriptor *altsd;
  173. struct usb_interface *iface = usb_ifnum_to_if(dev, interface);
  174. if (!iface) {
  175. dev_err(&dev->dev, "%u:%d : does not exist\n",
  176. ctrlif, interface);
  177. return -EINVAL;
  178. }
  179. alts = &iface->altsetting[0];
  180. altsd = get_iface_desc(alts);
  181. /*
  182. * Android with both accessory and audio interfaces enabled gets the
  183. * interface numbers wrong.
  184. */
  185. if ((chip->usb_id == USB_ID(0x18d1, 0x2d04) ||
  186. chip->usb_id == USB_ID(0x18d1, 0x2d05)) &&
  187. interface == 0 &&
  188. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC &&
  189. altsd->bInterfaceSubClass == USB_SUBCLASS_VENDOR_SPEC) {
  190. interface = 2;
  191. iface = usb_ifnum_to_if(dev, interface);
  192. if (!iface)
  193. return -EINVAL;
  194. alts = &iface->altsetting[0];
  195. altsd = get_iface_desc(alts);
  196. }
  197. if (usb_interface_claimed(iface)) {
  198. dev_dbg(&dev->dev, "%d:%d: skipping, already claimed\n",
  199. ctrlif, interface);
  200. return -EINVAL;
  201. }
  202. if ((altsd->bInterfaceClass == USB_CLASS_AUDIO ||
  203. altsd->bInterfaceClass == USB_CLASS_VENDOR_SPEC) &&
  204. altsd->bInterfaceSubClass == USB_SUBCLASS_MIDISTREAMING) {
  205. int err = __snd_usbmidi_create(chip->card, iface,
  206. &chip->midi_list, NULL,
  207. chip->usb_id);
  208. if (err < 0) {
  209. dev_err(&dev->dev,
  210. "%u:%d: cannot create sequencer device\n",
  211. ctrlif, interface);
  212. return -EINVAL;
  213. }
  214. return usb_driver_claim_interface(&usb_audio_driver, iface,
  215. USB_AUDIO_IFACE_UNUSED);
  216. }
  217. if ((altsd->bInterfaceClass != USB_CLASS_AUDIO &&
  218. altsd->bInterfaceClass != USB_CLASS_VENDOR_SPEC) ||
  219. altsd->bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING) {
  220. dev_dbg(&dev->dev,
  221. "%u:%d: skipping non-supported interface %d\n",
  222. ctrlif, interface, altsd->bInterfaceClass);
  223. /* skip non-supported classes */
  224. return -EINVAL;
  225. }
  226. if (snd_usb_get_speed(dev) == USB_SPEED_LOW) {
  227. dev_err(&dev->dev, "low speed audio streaming not supported\n");
  228. return -EINVAL;
  229. }
  230. if (! snd_usb_parse_audio_interface(chip, interface)) {
  231. usb_set_interface(dev, interface, 0); /* reset the current interface */
  232. return usb_driver_claim_interface(&usb_audio_driver, iface,
  233. USB_AUDIO_IFACE_UNUSED);
  234. }
  235. return 0;
  236. }
  237. /*
  238. * parse audio control descriptor and create pcm/midi streams
  239. */
  240. static int snd_usb_create_streams(struct snd_usb_audio *chip, int ctrlif)
  241. {
  242. struct usb_device *dev = chip->dev;
  243. struct usb_host_interface *host_iface;
  244. struct usb_interface_descriptor *altsd;
  245. int i, protocol;
  246. /* find audiocontrol interface */
  247. host_iface = &usb_ifnum_to_if(dev, ctrlif)->altsetting[0];
  248. altsd = get_iface_desc(host_iface);
  249. protocol = altsd->bInterfaceProtocol;
  250. switch (protocol) {
  251. default:
  252. dev_warn(&dev->dev,
  253. "unknown interface protocol %#02x, assuming v1\n",
  254. protocol);
  255. fallthrough;
  256. case UAC_VERSION_1: {
  257. struct uac1_ac_header_descriptor *h1;
  258. int rest_bytes;
  259. h1 = snd_usb_find_csint_desc(host_iface->extra,
  260. host_iface->extralen,
  261. NULL, UAC_HEADER);
  262. if (!h1 || h1->bLength < sizeof(*h1)) {
  263. dev_err(&dev->dev, "cannot find UAC_HEADER\n");
  264. return -EINVAL;
  265. }
  266. rest_bytes = (void *)(host_iface->extra +
  267. host_iface->extralen) - (void *)h1;
  268. /* just to be sure -- this shouldn't hit at all */
  269. if (rest_bytes <= 0) {
  270. dev_err(&dev->dev, "invalid control header\n");
  271. return -EINVAL;
  272. }
  273. if (rest_bytes < sizeof(*h1)) {
  274. dev_err(&dev->dev, "too short v1 buffer descriptor\n");
  275. return -EINVAL;
  276. }
  277. if (!h1->bInCollection) {
  278. dev_info(&dev->dev, "skipping empty audio interface (v1)\n");
  279. return -EINVAL;
  280. }
  281. if (rest_bytes < h1->bLength) {
  282. dev_err(&dev->dev, "invalid buffer length (v1)\n");
  283. return -EINVAL;
  284. }
  285. if (h1->bLength < sizeof(*h1) + h1->bInCollection) {
  286. dev_err(&dev->dev, "invalid UAC_HEADER (v1)\n");
  287. return -EINVAL;
  288. }
  289. for (i = 0; i < h1->bInCollection; i++)
  290. snd_usb_create_stream(chip, ctrlif, h1->baInterfaceNr[i]);
  291. break;
  292. }
  293. case UAC_VERSION_2:
  294. case UAC_VERSION_3: {
  295. struct usb_interface_assoc_descriptor *assoc =
  296. usb_ifnum_to_if(dev, ctrlif)->intf_assoc;
  297. if (!assoc) {
  298. /*
  299. * Firmware writers cannot count to three. So to find
  300. * the IAD on the NuForce UDH-100, also check the next
  301. * interface.
  302. */
  303. struct usb_interface *iface =
  304. usb_ifnum_to_if(dev, ctrlif + 1);
  305. if (iface &&
  306. iface->intf_assoc &&
  307. iface->intf_assoc->bFunctionClass == USB_CLASS_AUDIO &&
  308. iface->intf_assoc->bFunctionProtocol == UAC_VERSION_2)
  309. assoc = iface->intf_assoc;
  310. }
  311. if (!assoc) {
  312. dev_err(&dev->dev, "Audio class v2/v3 interfaces need an interface association\n");
  313. return -EINVAL;
  314. }
  315. if (protocol == UAC_VERSION_3) {
  316. int badd = assoc->bFunctionSubClass;
  317. if (badd != UAC3_FUNCTION_SUBCLASS_FULL_ADC_3_0 &&
  318. (badd < UAC3_FUNCTION_SUBCLASS_GENERIC_IO ||
  319. badd > UAC3_FUNCTION_SUBCLASS_SPEAKERPHONE)) {
  320. dev_err(&dev->dev,
  321. "Unsupported UAC3 BADD profile\n");
  322. return -EINVAL;
  323. }
  324. chip->badd_profile = badd;
  325. }
  326. for (i = 0; i < assoc->bInterfaceCount; i++) {
  327. int intf = assoc->bFirstInterface + i;
  328. if (intf != ctrlif)
  329. snd_usb_create_stream(chip, ctrlif, intf);
  330. }
  331. break;
  332. }
  333. }
  334. return 0;
  335. }
  336. /*
  337. * Profile name preset table
  338. */
  339. struct usb_audio_device_name {
  340. u32 id;
  341. const char *vendor_name;
  342. const char *product_name;
  343. const char *profile_name; /* override card->longname */
  344. };
  345. #define PROFILE_NAME(vid, pid, vendor, product, profile) \
  346. { .id = USB_ID(vid, pid), .vendor_name = (vendor), \
  347. .product_name = (product), .profile_name = (profile) }
  348. #define DEVICE_NAME(vid, pid, vendor, product) \
  349. PROFILE_NAME(vid, pid, vendor, product, NULL)
  350. /* vendor/product and profile name presets, sorted in device id order */
  351. static const struct usb_audio_device_name usb_audio_names[] = {
  352. /* HP Thunderbolt Dock Audio Headset */
  353. PROFILE_NAME(0x03f0, 0x0269, "HP", "Thunderbolt Dock Audio Headset",
  354. "HP-Thunderbolt-Dock-Audio-Headset"),
  355. /* HP Thunderbolt Dock Audio Module */
  356. PROFILE_NAME(0x03f0, 0x0567, "HP", "Thunderbolt Dock Audio Module",
  357. "HP-Thunderbolt-Dock-Audio-Module"),
  358. /* Two entries for Gigabyte TRX40 Aorus Master:
  359. * TRX40 Aorus Master has two USB-audio devices, one for the front
  360. * headphone with ESS SABRE9218 DAC chip, while another for the rest
  361. * I/O (the rear panel and the front mic) with Realtek ALC1220-VB.
  362. * Here we provide two distinct names for making UCM profiles easier.
  363. */
  364. PROFILE_NAME(0x0414, 0xa000, "Gigabyte", "Aorus Master Front Headphone",
  365. "Gigabyte-Aorus-Master-Front-Headphone"),
  366. PROFILE_NAME(0x0414, 0xa001, "Gigabyte", "Aorus Master Main Audio",
  367. "Gigabyte-Aorus-Master-Main-Audio"),
  368. /* Gigabyte TRX40 Aorus Pro WiFi */
  369. PROFILE_NAME(0x0414, 0xa002,
  370. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  371. /* Creative/E-Mu devices */
  372. DEVICE_NAME(0x041e, 0x3010, "Creative Labs", "Sound Blaster MP3+"),
  373. /* Creative/Toshiba Multimedia Center SB-0500 */
  374. DEVICE_NAME(0x041e, 0x3048, "Toshiba", "SB-0500"),
  375. DEVICE_NAME(0x046d, 0x0990, "Logitech, Inc.", "QuickCam Pro 9000"),
  376. DEVICE_NAME(0x05e1, 0x0408, "Syntek", "STK1160"),
  377. DEVICE_NAME(0x05e1, 0x0480, "Hauppauge", "Woodbury"),
  378. /* ASUS ROG Zenith II: this machine has also two devices, one for
  379. * the front headphone and another for the rest
  380. */
  381. PROFILE_NAME(0x0b05, 0x1915, "ASUS", "Zenith II Front Headphone",
  382. "Zenith-II-Front-Headphone"),
  383. PROFILE_NAME(0x0b05, 0x1916, "ASUS", "Zenith II Main Audio",
  384. "Zenith-II-Main-Audio"),
  385. /* ASUS ROG Strix */
  386. PROFILE_NAME(0x0b05, 0x1917,
  387. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  388. /* ASUS PRIME TRX40 PRO-S */
  389. PROFILE_NAME(0x0b05, 0x1918,
  390. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  391. /* Dell WD15 Dock */
  392. PROFILE_NAME(0x0bda, 0x4014, "Dell", "WD15 Dock", "Dell-WD15-Dock"),
  393. /* Dell WD19 Dock */
  394. PROFILE_NAME(0x0bda, 0x402e, "Dell", "WD19 Dock", "Dell-WD15-Dock"),
  395. DEVICE_NAME(0x0ccd, 0x0028, "TerraTec", "Aureon5.1MkII"),
  396. /*
  397. * The original product_name is "USB Sound Device", however this name
  398. * is also used by the CM106 based cards, so make it unique.
  399. */
  400. DEVICE_NAME(0x0d8c, 0x0102, NULL, "ICUSBAUDIO7D"),
  401. DEVICE_NAME(0x0d8c, 0x0103, NULL, "Audio Advantage MicroII"),
  402. /* MSI TRX40 Creator */
  403. PROFILE_NAME(0x0db0, 0x0d64,
  404. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  405. /* MSI TRX40 */
  406. PROFILE_NAME(0x0db0, 0x543d,
  407. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  408. DEVICE_NAME(0x0fd9, 0x0008, "Hauppauge", "HVR-950Q"),
  409. /* Stanton/N2IT Final Scratch v1 device ('Scratchamp') */
  410. DEVICE_NAME(0x103d, 0x0100, "Stanton", "ScratchAmp"),
  411. DEVICE_NAME(0x103d, 0x0101, "Stanton", "ScratchAmp"),
  412. /* aka. Serato Scratch Live DJ Box */
  413. DEVICE_NAME(0x13e5, 0x0001, "Rane", "SL-1"),
  414. /* Lenovo ThinkStation P620 Rear Line-in, Line-out and Microphone */
  415. PROFILE_NAME(0x17aa, 0x1046, "Lenovo", "ThinkStation P620 Rear",
  416. "Lenovo-ThinkStation-P620-Rear"),
  417. /* Lenovo ThinkStation P620 Internal Speaker + Front Headset */
  418. PROFILE_NAME(0x17aa, 0x104d, "Lenovo", "ThinkStation P620 Main",
  419. "Lenovo-ThinkStation-P620-Main"),
  420. /* Asrock TRX40 Creator */
  421. PROFILE_NAME(0x26ce, 0x0a01,
  422. "Realtek", "ALC1220-VB-DT", "Realtek-ALC1220-VB-Desktop"),
  423. DEVICE_NAME(0x2040, 0x7200, "Hauppauge", "HVR-950Q"),
  424. DEVICE_NAME(0x2040, 0x7201, "Hauppauge", "HVR-950Q-MXL"),
  425. DEVICE_NAME(0x2040, 0x7210, "Hauppauge", "HVR-950Q"),
  426. DEVICE_NAME(0x2040, 0x7211, "Hauppauge", "HVR-950Q-MXL"),
  427. DEVICE_NAME(0x2040, 0x7213, "Hauppauge", "HVR-950Q"),
  428. DEVICE_NAME(0x2040, 0x7217, "Hauppauge", "HVR-950Q"),
  429. DEVICE_NAME(0x2040, 0x721b, "Hauppauge", "HVR-950Q"),
  430. DEVICE_NAME(0x2040, 0x721e, "Hauppauge", "HVR-950Q"),
  431. DEVICE_NAME(0x2040, 0x721f, "Hauppauge", "HVR-950Q"),
  432. DEVICE_NAME(0x2040, 0x7240, "Hauppauge", "HVR-850"),
  433. DEVICE_NAME(0x2040, 0x7260, "Hauppauge", "HVR-950Q"),
  434. DEVICE_NAME(0x2040, 0x7270, "Hauppauge", "HVR-950Q"),
  435. DEVICE_NAME(0x2040, 0x7280, "Hauppauge", "HVR-950Q"),
  436. DEVICE_NAME(0x2040, 0x7281, "Hauppauge", "HVR-950Q-MXL"),
  437. DEVICE_NAME(0x2040, 0x8200, "Hauppauge", "Woodbury"),
  438. { } /* terminator */
  439. };
  440. static const struct usb_audio_device_name *
  441. lookup_device_name(u32 id)
  442. {
  443. static const struct usb_audio_device_name *p;
  444. for (p = usb_audio_names; p->id; p++)
  445. if (p->id == id)
  446. return p;
  447. return NULL;
  448. }
  449. /*
  450. * free the chip instance
  451. *
  452. * here we have to do not much, since pcm and controls are already freed
  453. *
  454. */
  455. static void snd_usb_audio_free(struct snd_card *card)
  456. {
  457. struct snd_usb_audio *chip = card->private_data;
  458. snd_usb_endpoint_free_all(chip);
  459. mutex_destroy(&chip->mutex);
  460. if (!atomic_read(&chip->shutdown))
  461. dev_set_drvdata(&chip->dev->dev, NULL);
  462. }
  463. static void usb_audio_make_shortname(struct usb_device *dev,
  464. struct snd_usb_audio *chip,
  465. const struct snd_usb_audio_quirk *quirk)
  466. {
  467. struct snd_card *card = chip->card;
  468. const struct usb_audio_device_name *preset;
  469. const char *s = NULL;
  470. preset = lookup_device_name(chip->usb_id);
  471. if (preset && preset->product_name)
  472. s = preset->product_name;
  473. else if (quirk && quirk->product_name)
  474. s = quirk->product_name;
  475. if (s && *s) {
  476. strscpy(card->shortname, s, sizeof(card->shortname));
  477. return;
  478. }
  479. /* retrieve the device string as shortname */
  480. if (!dev->descriptor.iProduct ||
  481. usb_string(dev, dev->descriptor.iProduct,
  482. card->shortname, sizeof(card->shortname)) <= 0) {
  483. /* no name available from anywhere, so use ID */
  484. sprintf(card->shortname, "USB Device %#04x:%#04x",
  485. USB_ID_VENDOR(chip->usb_id),
  486. USB_ID_PRODUCT(chip->usb_id));
  487. }
  488. strim(card->shortname);
  489. }
  490. static void usb_audio_make_longname(struct usb_device *dev,
  491. struct snd_usb_audio *chip,
  492. const struct snd_usb_audio_quirk *quirk)
  493. {
  494. struct snd_card *card = chip->card;
  495. const struct usb_audio_device_name *preset;
  496. const char *s = NULL;
  497. int len;
  498. preset = lookup_device_name(chip->usb_id);
  499. /* shortcut - if any pre-defined string is given, use it */
  500. if (preset && preset->profile_name)
  501. s = preset->profile_name;
  502. if (s && *s) {
  503. strscpy(card->longname, s, sizeof(card->longname));
  504. return;
  505. }
  506. if (preset && preset->vendor_name)
  507. s = preset->vendor_name;
  508. else if (quirk && quirk->vendor_name)
  509. s = quirk->vendor_name;
  510. *card->longname = 0;
  511. if (s && *s) {
  512. strscpy(card->longname, s, sizeof(card->longname));
  513. } else {
  514. /* retrieve the vendor and device strings as longname */
  515. if (dev->descriptor.iManufacturer)
  516. usb_string(dev, dev->descriptor.iManufacturer,
  517. card->longname, sizeof(card->longname));
  518. /* we don't really care if there isn't any vendor string */
  519. }
  520. if (*card->longname) {
  521. strim(card->longname);
  522. if (*card->longname)
  523. strlcat(card->longname, " ", sizeof(card->longname));
  524. }
  525. strlcat(card->longname, card->shortname, sizeof(card->longname));
  526. len = strlcat(card->longname, " at ", sizeof(card->longname));
  527. if (len < sizeof(card->longname))
  528. usb_make_path(dev, card->longname + len, sizeof(card->longname) - len);
  529. switch (snd_usb_get_speed(dev)) {
  530. case USB_SPEED_LOW:
  531. strlcat(card->longname, ", low speed", sizeof(card->longname));
  532. break;
  533. case USB_SPEED_FULL:
  534. strlcat(card->longname, ", full speed", sizeof(card->longname));
  535. break;
  536. case USB_SPEED_HIGH:
  537. strlcat(card->longname, ", high speed", sizeof(card->longname));
  538. break;
  539. case USB_SPEED_SUPER:
  540. strlcat(card->longname, ", super speed", sizeof(card->longname));
  541. break;
  542. case USB_SPEED_SUPER_PLUS:
  543. strlcat(card->longname, ", super speed plus", sizeof(card->longname));
  544. break;
  545. default:
  546. break;
  547. }
  548. }
  549. /*
  550. * create a chip instance and set its names.
  551. */
  552. static int snd_usb_audio_create(struct usb_interface *intf,
  553. struct usb_device *dev, int idx,
  554. const struct snd_usb_audio_quirk *quirk,
  555. unsigned int usb_id,
  556. struct snd_usb_audio **rchip)
  557. {
  558. struct snd_card *card;
  559. struct snd_usb_audio *chip;
  560. int err;
  561. char component[14];
  562. *rchip = NULL;
  563. switch (snd_usb_get_speed(dev)) {
  564. case USB_SPEED_LOW:
  565. case USB_SPEED_FULL:
  566. case USB_SPEED_HIGH:
  567. case USB_SPEED_WIRELESS:
  568. case USB_SPEED_SUPER:
  569. case USB_SPEED_SUPER_PLUS:
  570. break;
  571. default:
  572. dev_err(&dev->dev, "unknown device speed %d\n", snd_usb_get_speed(dev));
  573. return -ENXIO;
  574. }
  575. err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
  576. sizeof(*chip), &card);
  577. if (err < 0) {
  578. dev_err(&dev->dev, "cannot create card instance %d\n", idx);
  579. return err;
  580. }
  581. chip = card->private_data;
  582. mutex_init(&chip->mutex);
  583. init_waitqueue_head(&chip->shutdown_wait);
  584. chip->index = idx;
  585. chip->dev = dev;
  586. chip->card = card;
  587. chip->setup = device_setup[idx];
  588. chip->generic_implicit_fb = implicit_fb[idx];
  589. chip->autoclock = autoclock;
  590. chip->lowlatency = lowlatency;
  591. atomic_set(&chip->active, 1); /* avoid autopm during probing */
  592. atomic_set(&chip->usage_count, 0);
  593. atomic_set(&chip->shutdown, 0);
  594. chip->usb_id = usb_id;
  595. INIT_LIST_HEAD(&chip->pcm_list);
  596. INIT_LIST_HEAD(&chip->ep_list);
  597. INIT_LIST_HEAD(&chip->iface_ref_list);
  598. INIT_LIST_HEAD(&chip->clock_ref_list);
  599. INIT_LIST_HEAD(&chip->midi_list);
  600. INIT_LIST_HEAD(&chip->mixer_list);
  601. if (quirk_flags[idx])
  602. chip->quirk_flags = quirk_flags[idx];
  603. else
  604. snd_usb_init_quirk_flags(chip);
  605. card->private_free = snd_usb_audio_free;
  606. strcpy(card->driver, "USB-Audio");
  607. sprintf(component, "USB%04x:%04x",
  608. USB_ID_VENDOR(chip->usb_id), USB_ID_PRODUCT(chip->usb_id));
  609. snd_component_add(card, component);
  610. usb_audio_make_shortname(dev, chip, quirk);
  611. usb_audio_make_longname(dev, chip, quirk);
  612. snd_usb_audio_create_proc(chip);
  613. *rchip = chip;
  614. return 0;
  615. }
  616. /* look for a matching quirk alias id */
  617. static bool get_alias_id(struct usb_device *dev, unsigned int *id)
  618. {
  619. int i;
  620. unsigned int src, dst;
  621. for (i = 0; i < ARRAY_SIZE(quirk_alias); i++) {
  622. if (!quirk_alias[i] ||
  623. sscanf(quirk_alias[i], "%x:%x", &src, &dst) != 2 ||
  624. src != *id)
  625. continue;
  626. dev_info(&dev->dev,
  627. "device (%04x:%04x): applying quirk alias %04x:%04x\n",
  628. USB_ID_VENDOR(*id), USB_ID_PRODUCT(*id),
  629. USB_ID_VENDOR(dst), USB_ID_PRODUCT(dst));
  630. *id = dst;
  631. return true;
  632. }
  633. return false;
  634. }
  635. static int check_delayed_register_option(struct snd_usb_audio *chip)
  636. {
  637. int i;
  638. unsigned int id, inum;
  639. for (i = 0; i < ARRAY_SIZE(delayed_register); i++) {
  640. if (delayed_register[i] &&
  641. sscanf(delayed_register[i], "%x:%x", &id, &inum) == 2 &&
  642. id == chip->usb_id)
  643. return inum;
  644. }
  645. return -1;
  646. }
  647. static const struct usb_device_id usb_audio_ids[]; /* defined below */
  648. /* look for the last interface that matches with our ids and remember it */
  649. static void find_last_interface(struct snd_usb_audio *chip)
  650. {
  651. struct usb_host_config *config = chip->dev->actconfig;
  652. struct usb_interface *intf;
  653. int i;
  654. if (!config)
  655. return;
  656. for (i = 0; i < config->desc.bNumInterfaces; i++) {
  657. intf = config->interface[i];
  658. if (usb_match_id(intf, usb_audio_ids))
  659. chip->last_iface = intf->altsetting[0].desc.bInterfaceNumber;
  660. }
  661. usb_audio_dbg(chip, "Found last interface = %d\n", chip->last_iface);
  662. }
  663. /* look for the corresponding quirk */
  664. static const struct snd_usb_audio_quirk *
  665. get_alias_quirk(struct usb_device *dev, unsigned int id)
  666. {
  667. const struct usb_device_id *p;
  668. for (p = usb_audio_ids; p->match_flags; p++) {
  669. /* FIXME: this checks only vendor:product pair in the list */
  670. if ((p->match_flags & USB_DEVICE_ID_MATCH_DEVICE) ==
  671. USB_DEVICE_ID_MATCH_DEVICE &&
  672. p->idVendor == USB_ID_VENDOR(id) &&
  673. p->idProduct == USB_ID_PRODUCT(id))
  674. return (const struct snd_usb_audio_quirk *)p->driver_info;
  675. }
  676. return NULL;
  677. }
  678. /* register card if we reach to the last interface or to the specified
  679. * one given via option
  680. */
  681. static int try_to_register_card(struct snd_usb_audio *chip, int ifnum)
  682. {
  683. if (check_delayed_register_option(chip) == ifnum ||
  684. chip->last_iface == ifnum ||
  685. usb_interface_claimed(usb_ifnum_to_if(chip->dev, chip->last_iface)))
  686. return snd_card_register(chip->card);
  687. return 0;
  688. }
  689. /*
  690. * probe the active usb device
  691. *
  692. * note that this can be called multiple times per a device, when it
  693. * includes multiple audio control interfaces.
  694. *
  695. * thus we check the usb device pointer and creates the card instance
  696. * only at the first time. the successive calls of this function will
  697. * append the pcm interface to the corresponding card.
  698. */
  699. static int usb_audio_probe(struct usb_interface *intf,
  700. const struct usb_device_id *usb_id)
  701. {
  702. struct usb_device *dev = interface_to_usbdev(intf);
  703. const struct snd_usb_audio_quirk *quirk =
  704. (const struct snd_usb_audio_quirk *)usb_id->driver_info;
  705. struct snd_usb_audio *chip;
  706. int i, err;
  707. struct usb_host_interface *alts;
  708. int ifnum;
  709. u32 id;
  710. pr_info("%s : audio probe start!\n",__func__);
  711. alts = &intf->altsetting[0];
  712. ifnum = get_iface_desc(alts)->bInterfaceNumber;
  713. id = USB_ID(le16_to_cpu(dev->descriptor.idVendor),
  714. le16_to_cpu(dev->descriptor.idProduct));
  715. if (get_alias_id(dev, &id))
  716. quirk = get_alias_quirk(dev, id);
  717. if (quirk && quirk->ifnum >= 0 && ifnum != quirk->ifnum)
  718. return -ENXIO;
  719. if (quirk && quirk->ifnum == QUIRK_NODEV_INTERFACE)
  720. return -ENODEV;
  721. err = snd_usb_apply_boot_quirk(dev, intf, quirk, id);
  722. if (err < 0)
  723. return err;
  724. /*
  725. * found a config. now register to ALSA
  726. */
  727. /* check whether it's already registered */
  728. chip = NULL;
  729. mutex_lock(&register_mutex);
  730. for (i = 0; i < SNDRV_CARDS; i++) {
  731. if (usb_chip[i] && usb_chip[i]->dev == dev) {
  732. if (atomic_read(&usb_chip[i]->shutdown)) {
  733. dev_err(&dev->dev, "USB device is in the shutdown state, cannot create a card instance\n");
  734. err = -EIO;
  735. goto __error;
  736. }
  737. chip = usb_chip[i];
  738. atomic_inc(&chip->active); /* avoid autopm */
  739. break;
  740. }
  741. }
  742. if (! chip) {
  743. err = snd_usb_apply_boot_quirk_once(dev, intf, quirk, id);
  744. if (err < 0)
  745. goto __error;
  746. /* it's a fresh one.
  747. * now look for an empty slot and create a new card instance
  748. */
  749. for (i = 0; i < SNDRV_CARDS; i++)
  750. if (!usb_chip[i] &&
  751. (vid[i] == -1 || vid[i] == USB_ID_VENDOR(id)) &&
  752. (pid[i] == -1 || pid[i] == USB_ID_PRODUCT(id))) {
  753. if (enable[i]) {
  754. err = snd_usb_audio_create(intf, dev, i, quirk,
  755. id, &chip);
  756. if (err < 0)
  757. goto __error;
  758. break;
  759. } else if (vid[i] != -1 || pid[i] != -1) {
  760. dev_info(&dev->dev,
  761. "device (%04x:%04x) is disabled\n",
  762. USB_ID_VENDOR(id),
  763. USB_ID_PRODUCT(id));
  764. err = -ENOENT;
  765. goto __error;
  766. }
  767. }
  768. if (!chip) {
  769. dev_err(&dev->dev, "no available usb audio device\n");
  770. err = -ENODEV;
  771. goto __error;
  772. }
  773. find_last_interface(chip);
  774. }
  775. if (chip->num_interfaces >= MAX_CARD_INTERFACES) {
  776. dev_info(&dev->dev, "Too many interfaces assigned to the single USB-audio card\n");
  777. err = -EINVAL;
  778. goto __error;
  779. }
  780. dev_set_drvdata(&dev->dev, chip);
  781. if (ignore_ctl_error)
  782. chip->quirk_flags |= QUIRK_FLAG_IGNORE_CTL_ERROR;
  783. if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
  784. usb_disable_autosuspend(interface_to_usbdev(intf));
  785. trace_android_vh_audio_usb_offload_connect(intf, chip);
  786. /*
  787. * For devices with more than one control interface, we assume the
  788. * first contains the audio controls. We might need a more specific
  789. * check here in the future.
  790. */
  791. if (!chip->ctrl_intf)
  792. chip->ctrl_intf = alts;
  793. err = 1; /* continue */
  794. if (quirk && quirk->ifnum != QUIRK_NO_INTERFACE) {
  795. /* need some special handlings */
  796. err = snd_usb_create_quirk(chip, intf, &usb_audio_driver, quirk);
  797. if (err < 0)
  798. goto __error;
  799. }
  800. if (err > 0) {
  801. /* create normal USB audio interfaces */
  802. err = snd_usb_create_streams(chip, ifnum);
  803. if (err < 0)
  804. goto __error;
  805. err = snd_usb_create_mixer(chip, ifnum);
  806. if (err < 0)
  807. goto __error;
  808. }
  809. if (chip->need_delayed_register) {
  810. dev_info(&dev->dev,
  811. "Found post-registration device assignment: %08x:%02x\n",
  812. chip->usb_id, ifnum);
  813. chip->need_delayed_register = false; /* clear again */
  814. }
  815. err = try_to_register_card(chip, ifnum);
  816. if (err < 0)
  817. goto __error_no_register;
  818. pr_info("%s : card %d is registered.\n", __func__, chip->card->number);
  819. if (chip->quirk_flags & QUIRK_FLAG_SHARE_MEDIA_DEVICE) {
  820. /* don't want to fail when snd_media_device_create() fails */
  821. snd_media_device_create(chip, intf);
  822. }
  823. if (quirk)
  824. chip->quirk_type = quirk->type;
  825. usb_chip[chip->index] = chip;
  826. chip->intf[chip->num_interfaces] = intf;
  827. chip->num_interfaces++;
  828. usb_set_intfdata(intf, chip);
  829. atomic_dec(&chip->active);
  830. mutex_unlock(&register_mutex);
  831. return 0;
  832. __error:
  833. /* in the case of error in secondary interface, still try to register */
  834. if (chip)
  835. try_to_register_card(chip, ifnum);
  836. __error_no_register:
  837. if (chip) {
  838. /* chip->active is inside the chip->card object,
  839. * decrement before memory is possibly returned.
  840. */
  841. atomic_dec(&chip->active);
  842. if (!chip->num_interfaces)
  843. snd_card_free(chip->card);
  844. }
  845. mutex_unlock(&register_mutex);
  846. return err;
  847. }
  848. /*
  849. * we need to take care of counter, since disconnection can be called also
  850. * many times as well as usb_audio_probe().
  851. */
  852. static void usb_audio_disconnect(struct usb_interface *intf)
  853. {
  854. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  855. struct snd_card *card;
  856. struct list_head *p;
  857. if (chip == USB_AUDIO_IFACE_UNUSED)
  858. return;
  859. card = chip->card;
  860. trace_android_rvh_audio_usb_offload_disconnect(intf);
  861. mutex_lock(&register_mutex);
  862. if (atomic_inc_return(&chip->shutdown) == 1) {
  863. struct snd_usb_stream *as;
  864. struct snd_usb_endpoint *ep;
  865. struct usb_mixer_interface *mixer;
  866. /* wait until all pending tasks done;
  867. * they are protected by snd_usb_lock_shutdown()
  868. */
  869. wait_event(chip->shutdown_wait,
  870. !atomic_read(&chip->usage_count));
  871. snd_card_disconnect(card);
  872. /* release the pcm resources */
  873. list_for_each_entry(as, &chip->pcm_list, list) {
  874. snd_usb_stream_disconnect(as);
  875. }
  876. /* release the endpoint resources */
  877. list_for_each_entry(ep, &chip->ep_list, list) {
  878. snd_usb_endpoint_release(ep);
  879. }
  880. /* release the midi resources */
  881. list_for_each(p, &chip->midi_list) {
  882. snd_usbmidi_disconnect(p);
  883. }
  884. /*
  885. * Nice to check quirk && quirk->shares_media_device and
  886. * then call the snd_media_device_delete(). Don't have
  887. * access to the quirk here. snd_media_device_delete()
  888. * accesses mixer_list
  889. */
  890. snd_media_device_delete(chip);
  891. /* release mixer resources */
  892. list_for_each_entry(mixer, &chip->mixer_list, list) {
  893. snd_usb_mixer_disconnect(mixer);
  894. }
  895. }
  896. if (chip->quirk_flags & QUIRK_FLAG_DISABLE_AUTOSUSPEND)
  897. usb_enable_autosuspend(interface_to_usbdev(intf));
  898. chip->num_interfaces--;
  899. if (chip->num_interfaces <= 0) {
  900. usb_chip[chip->index] = NULL;
  901. mutex_unlock(&register_mutex);
  902. snd_card_free_when_closed(card);
  903. } else {
  904. mutex_unlock(&register_mutex);
  905. }
  906. }
  907. /* lock the shutdown (disconnect) task and autoresume */
  908. int snd_usb_lock_shutdown(struct snd_usb_audio *chip)
  909. {
  910. int err;
  911. atomic_inc(&chip->usage_count);
  912. if (atomic_read(&chip->shutdown)) {
  913. err = -EIO;
  914. goto error;
  915. }
  916. err = snd_usb_autoresume(chip);
  917. if (err < 0)
  918. goto error;
  919. return 0;
  920. error:
  921. if (atomic_dec_and_test(&chip->usage_count))
  922. wake_up(&chip->shutdown_wait);
  923. return err;
  924. }
  925. /* autosuspend and unlock the shutdown */
  926. void snd_usb_unlock_shutdown(struct snd_usb_audio *chip)
  927. {
  928. snd_usb_autosuspend(chip);
  929. if (atomic_dec_and_test(&chip->usage_count))
  930. wake_up(&chip->shutdown_wait);
  931. }
  932. int snd_usb_autoresume(struct snd_usb_audio *chip)
  933. {
  934. int i, err;
  935. if (atomic_read(&chip->shutdown))
  936. return -EIO;
  937. if (atomic_inc_return(&chip->active) != 1)
  938. return 0;
  939. for (i = 0; i < chip->num_interfaces; i++) {
  940. err = usb_autopm_get_interface(chip->intf[i]);
  941. if (err < 0) {
  942. /* rollback */
  943. while (--i >= 0)
  944. usb_autopm_put_interface(chip->intf[i]);
  945. atomic_dec(&chip->active);
  946. return err;
  947. }
  948. }
  949. return 0;
  950. }
  951. EXPORT_SYMBOL_GPL(snd_usb_autoresume);
  952. void snd_usb_autosuspend(struct snd_usb_audio *chip)
  953. {
  954. int i;
  955. if (atomic_read(&chip->shutdown))
  956. return;
  957. if (!atomic_dec_and_test(&chip->active))
  958. return;
  959. for (i = 0; i < chip->num_interfaces; i++)
  960. usb_autopm_put_interface(chip->intf[i]);
  961. }
  962. EXPORT_SYMBOL_GPL(snd_usb_autosuspend);
  963. static int usb_audio_suspend(struct usb_interface *intf, pm_message_t message)
  964. {
  965. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  966. struct snd_usb_stream *as;
  967. struct snd_usb_endpoint *ep;
  968. struct usb_mixer_interface *mixer;
  969. struct list_head *p;
  970. if (chip == USB_AUDIO_IFACE_UNUSED)
  971. return 0;
  972. if (!chip->num_suspended_intf++) {
  973. list_for_each_entry(as, &chip->pcm_list, list)
  974. snd_usb_pcm_suspend(as);
  975. list_for_each_entry(ep, &chip->ep_list, list)
  976. snd_usb_endpoint_suspend(ep);
  977. list_for_each(p, &chip->midi_list)
  978. snd_usbmidi_suspend(p);
  979. list_for_each_entry(mixer, &chip->mixer_list, list)
  980. snd_usb_mixer_suspend(mixer);
  981. }
  982. if (!PMSG_IS_AUTO(message) && !chip->system_suspend) {
  983. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
  984. chip->system_suspend = chip->num_suspended_intf;
  985. }
  986. return 0;
  987. }
  988. static int usb_audio_resume(struct usb_interface *intf)
  989. {
  990. struct snd_usb_audio *chip = usb_get_intfdata(intf);
  991. struct snd_usb_stream *as;
  992. struct usb_mixer_interface *mixer;
  993. struct list_head *p;
  994. int err = 0;
  995. if (chip == USB_AUDIO_IFACE_UNUSED)
  996. return 0;
  997. atomic_inc(&chip->active); /* avoid autopm */
  998. if (chip->num_suspended_intf > 1)
  999. goto out;
  1000. list_for_each_entry(as, &chip->pcm_list, list) {
  1001. err = snd_usb_pcm_resume(as);
  1002. if (err < 0)
  1003. goto err_out;
  1004. }
  1005. /*
  1006. * ALSA leaves material resumption to user space
  1007. * we just notify and restart the mixers
  1008. */
  1009. list_for_each_entry(mixer, &chip->mixer_list, list) {
  1010. err = snd_usb_mixer_resume(mixer);
  1011. if (err < 0)
  1012. goto err_out;
  1013. }
  1014. list_for_each(p, &chip->midi_list) {
  1015. snd_usbmidi_resume(p);
  1016. }
  1017. out:
  1018. if (chip->num_suspended_intf == chip->system_suspend) {
  1019. snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
  1020. chip->system_suspend = 0;
  1021. }
  1022. chip->num_suspended_intf--;
  1023. err_out:
  1024. atomic_dec(&chip->active); /* allow autopm after this point */
  1025. return err;
  1026. }
  1027. static const struct usb_device_id usb_audio_ids [] = {
  1028. #include "quirks-table.h"
  1029. { .match_flags = (USB_DEVICE_ID_MATCH_INT_CLASS | USB_DEVICE_ID_MATCH_INT_SUBCLASS),
  1030. .bInterfaceClass = USB_CLASS_AUDIO,
  1031. .bInterfaceSubClass = USB_SUBCLASS_AUDIOCONTROL },
  1032. { } /* Terminating entry */
  1033. };
  1034. MODULE_DEVICE_TABLE(usb, usb_audio_ids);
  1035. /*
  1036. * entry point for linux usb interface
  1037. */
  1038. static struct usb_driver usb_audio_driver = {
  1039. .name = "snd-usb-audio",
  1040. .probe = usb_audio_probe,
  1041. .disconnect = usb_audio_disconnect,
  1042. .suspend = usb_audio_suspend,
  1043. .resume = usb_audio_resume,
  1044. .reset_resume = usb_audio_resume,
  1045. .id_table = usb_audio_ids,
  1046. .supports_autosuspend = 1,
  1047. };
  1048. module_usb_driver(usb_audio_driver);