implicit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. //
  3. // Special handling for implicit feedback mode
  4. //
  5. #include <linux/init.h>
  6. #include <linux/usb.h>
  7. #include <linux/usb/audio.h>
  8. #include <linux/usb/audio-v2.h>
  9. #include <sound/core.h>
  10. #include <sound/pcm.h>
  11. #include <sound/pcm_params.h>
  12. #include "usbaudio.h"
  13. #include "card.h"
  14. #include "helper.h"
  15. #include "pcm.h"
  16. #include "implicit.h"
  17. enum {
  18. IMPLICIT_FB_NONE,
  19. IMPLICIT_FB_GENERIC,
  20. IMPLICIT_FB_FIXED,
  21. IMPLICIT_FB_BOTH, /* generic playback + capture (for BOSS) */
  22. };
  23. struct snd_usb_implicit_fb_match {
  24. unsigned int id;
  25. unsigned int iface_class;
  26. unsigned int ep_num;
  27. unsigned int iface;
  28. int type;
  29. };
  30. #define IMPLICIT_FB_GENERIC_DEV(vend, prod) \
  31. { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_GENERIC }
  32. #define IMPLICIT_FB_FIXED_DEV(vend, prod, ep, ifnum) \
  33. { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_FIXED, .ep_num = (ep),\
  34. .iface = (ifnum) }
  35. #define IMPLICIT_FB_BOTH_DEV(vend, prod, ep, ifnum) \
  36. { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_BOTH, .ep_num = (ep),\
  37. .iface = (ifnum) }
  38. #define IMPLICIT_FB_SKIP_DEV(vend, prod) \
  39. { .id = USB_ID(vend, prod), .type = IMPLICIT_FB_NONE }
  40. /* Implicit feedback quirk table for playback */
  41. static const struct snd_usb_implicit_fb_match playback_implicit_fb_quirks[] = {
  42. /* Fixed EP */
  43. /* FIXME: check the availability of generic matching */
  44. IMPLICIT_FB_FIXED_DEV(0x0763, 0x2030, 0x81, 3), /* M-Audio Fast Track C400 */
  45. IMPLICIT_FB_FIXED_DEV(0x0763, 0x2031, 0x81, 3), /* M-Audio Fast Track C600 */
  46. IMPLICIT_FB_FIXED_DEV(0x0763, 0x2080, 0x81, 2), /* M-Audio FastTrack Ultra */
  47. IMPLICIT_FB_FIXED_DEV(0x0763, 0x2081, 0x81, 2), /* M-Audio FastTrack Ultra */
  48. IMPLICIT_FB_FIXED_DEV(0x2466, 0x8010, 0x81, 2), /* Fractal Audio Axe-Fx III */
  49. IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0001, 0x81, 2), /* Solid State Logic SSL2 */
  50. IMPLICIT_FB_FIXED_DEV(0x31e9, 0x0002, 0x81, 2), /* Solid State Logic SSL2+ */
  51. IMPLICIT_FB_FIXED_DEV(0x0499, 0x172f, 0x81, 2), /* Steinberg UR22C */
  52. IMPLICIT_FB_FIXED_DEV(0x0d9a, 0x00df, 0x81, 2), /* RTX6001 */
  53. IMPLICIT_FB_FIXED_DEV(0x19f7, 0x000a, 0x84, 3), /* RODE AI-1 */
  54. IMPLICIT_FB_FIXED_DEV(0x22f0, 0x0006, 0x81, 3), /* Allen&Heath Qu-16 */
  55. IMPLICIT_FB_FIXED_DEV(0x1686, 0xf029, 0x82, 2), /* Zoom UAC-2 */
  56. IMPLICIT_FB_FIXED_DEV(0x2466, 0x8003, 0x86, 2), /* Fractal Audio Axe-Fx II */
  57. IMPLICIT_FB_FIXED_DEV(0x0499, 0x172a, 0x86, 2), /* Yamaha MODX */
  58. /* Special matching */
  59. { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_AUDIO,
  60. .type = IMPLICIT_FB_NONE }, /* MicroBook IIc */
  61. /* ep = 0x84, ifnum = 0 */
  62. { .id = USB_ID(0x07fd, 0x0004), .iface_class = USB_CLASS_VENDOR_SPEC,
  63. .type = IMPLICIT_FB_FIXED,
  64. .ep_num = 0x84, .iface = 0 }, /* MOTU MicroBook II */
  65. {} /* terminator */
  66. };
  67. /* Implicit feedback quirk table for capture: only FIXED type */
  68. static const struct snd_usb_implicit_fb_match capture_implicit_fb_quirks[] = {
  69. {} /* terminator */
  70. };
  71. /* set up sync EP information on the audioformat */
  72. static int add_implicit_fb_sync_ep(struct snd_usb_audio *chip,
  73. struct audioformat *fmt,
  74. int ep, int ep_idx, int ifnum,
  75. const struct usb_host_interface *alts)
  76. {
  77. struct usb_interface *iface;
  78. if (!alts) {
  79. iface = usb_ifnum_to_if(chip->dev, ifnum);
  80. if (!iface || iface->num_altsetting < 2)
  81. return 0;
  82. alts = &iface->altsetting[1];
  83. }
  84. fmt->sync_ep = ep;
  85. fmt->sync_iface = ifnum;
  86. fmt->sync_altsetting = alts->desc.bAlternateSetting;
  87. fmt->sync_ep_idx = ep_idx;
  88. fmt->implicit_fb = 1;
  89. usb_audio_dbg(chip,
  90. "%d:%d: added %s implicit_fb sync_ep %x, iface %d:%d\n",
  91. fmt->iface, fmt->altsetting,
  92. (ep & USB_DIR_IN) ? "playback" : "capture",
  93. fmt->sync_ep, fmt->sync_iface, fmt->sync_altsetting);
  94. return 1;
  95. }
  96. /* Check whether the given UAC2 iface:altset points to an implicit fb source */
  97. static int add_generic_uac2_implicit_fb(struct snd_usb_audio *chip,
  98. struct audioformat *fmt,
  99. unsigned int ifnum,
  100. unsigned int altsetting)
  101. {
  102. struct usb_host_interface *alts;
  103. struct usb_endpoint_descriptor *epd;
  104. alts = snd_usb_get_host_interface(chip, ifnum, altsetting);
  105. if (!alts)
  106. return 0;
  107. if (alts->desc.bInterfaceClass != USB_CLASS_AUDIO ||
  108. alts->desc.bInterfaceSubClass != USB_SUBCLASS_AUDIOSTREAMING ||
  109. alts->desc.bInterfaceProtocol != UAC_VERSION_2 ||
  110. alts->desc.bNumEndpoints < 1)
  111. return 0;
  112. epd = get_endpoint(alts, 0);
  113. if (!usb_endpoint_is_isoc_in(epd) ||
  114. (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  115. USB_ENDPOINT_USAGE_IMPLICIT_FB)
  116. return 0;
  117. return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
  118. ifnum, alts);
  119. }
  120. static bool roland_sanity_check_iface(struct usb_host_interface *alts)
  121. {
  122. if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  123. (alts->desc.bInterfaceSubClass != 2 &&
  124. alts->desc.bInterfaceProtocol != 2) ||
  125. alts->desc.bNumEndpoints < 1)
  126. return false;
  127. return true;
  128. }
  129. /* Like the UAC2 case above, but specific to Roland with vendor class and hack */
  130. static int add_roland_implicit_fb(struct snd_usb_audio *chip,
  131. struct audioformat *fmt,
  132. struct usb_host_interface *alts)
  133. {
  134. struct usb_endpoint_descriptor *epd;
  135. if (!roland_sanity_check_iface(alts))
  136. return 0;
  137. /* only when both streams are with ASYNC type */
  138. epd = get_endpoint(alts, 0);
  139. if (!usb_endpoint_is_isoc_out(epd) ||
  140. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  141. return 0;
  142. /* check capture EP */
  143. alts = snd_usb_get_host_interface(chip,
  144. alts->desc.bInterfaceNumber + 1,
  145. alts->desc.bAlternateSetting);
  146. if (!alts || !roland_sanity_check_iface(alts))
  147. return 0;
  148. epd = get_endpoint(alts, 0);
  149. if (!usb_endpoint_is_isoc_in(epd) ||
  150. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  151. return 0;
  152. chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
  153. return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
  154. alts->desc.bInterfaceNumber, alts);
  155. }
  156. /* capture quirk for Roland device; always full-duplex */
  157. static int add_roland_capture_quirk(struct snd_usb_audio *chip,
  158. struct audioformat *fmt,
  159. struct usb_host_interface *alts)
  160. {
  161. struct usb_endpoint_descriptor *epd;
  162. if (!roland_sanity_check_iface(alts))
  163. return 0;
  164. epd = get_endpoint(alts, 0);
  165. if (!usb_endpoint_is_isoc_in(epd) ||
  166. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  167. return 0;
  168. alts = snd_usb_get_host_interface(chip,
  169. alts->desc.bInterfaceNumber - 1,
  170. alts->desc.bAlternateSetting);
  171. if (!alts || !roland_sanity_check_iface(alts))
  172. return 0;
  173. epd = get_endpoint(alts, 0);
  174. if (!usb_endpoint_is_isoc_out(epd))
  175. return 0;
  176. return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
  177. alts->desc.bInterfaceNumber, alts);
  178. }
  179. /* Playback and capture EPs on Pioneer devices share the same iface/altset
  180. * for the implicit feedback operation
  181. */
  182. static bool is_pioneer_implicit_fb(struct snd_usb_audio *chip,
  183. struct usb_host_interface *alts)
  184. {
  185. struct usb_endpoint_descriptor *epd;
  186. if (USB_ID_VENDOR(chip->usb_id) != 0x2b73 &&
  187. USB_ID_VENDOR(chip->usb_id) != 0x08e4)
  188. return false;
  189. if (alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC)
  190. return false;
  191. if (alts->desc.bNumEndpoints != 2)
  192. return false;
  193. epd = get_endpoint(alts, 0);
  194. if (!usb_endpoint_is_isoc_out(epd) ||
  195. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  196. return false;
  197. epd = get_endpoint(alts, 1);
  198. if (!usb_endpoint_is_isoc_in(epd) ||
  199. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC ||
  200. ((epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  201. USB_ENDPOINT_USAGE_DATA &&
  202. (epd->bmAttributes & USB_ENDPOINT_USAGE_MASK) !=
  203. USB_ENDPOINT_USAGE_IMPLICIT_FB))
  204. return false;
  205. return true;
  206. }
  207. static int __add_generic_implicit_fb(struct snd_usb_audio *chip,
  208. struct audioformat *fmt,
  209. int iface, int altset)
  210. {
  211. struct usb_host_interface *alts;
  212. struct usb_endpoint_descriptor *epd;
  213. alts = snd_usb_get_host_interface(chip, iface, altset);
  214. if (!alts)
  215. return 0;
  216. if ((alts->desc.bInterfaceClass != USB_CLASS_VENDOR_SPEC &&
  217. alts->desc.bInterfaceClass != USB_CLASS_AUDIO) ||
  218. alts->desc.bNumEndpoints < 1)
  219. return 0;
  220. epd = get_endpoint(alts, 0);
  221. if (!usb_endpoint_is_isoc_in(epd) ||
  222. (epd->bmAttributes & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  223. return 0;
  224. return add_implicit_fb_sync_ep(chip, fmt, epd->bEndpointAddress, 0,
  225. iface, alts);
  226. }
  227. /* More generic quirk: look for the sync EP next to the data EP */
  228. static int add_generic_implicit_fb(struct snd_usb_audio *chip,
  229. struct audioformat *fmt,
  230. struct usb_host_interface *alts)
  231. {
  232. if ((fmt->ep_attr & USB_ENDPOINT_SYNCTYPE) != USB_ENDPOINT_SYNC_ASYNC)
  233. return 0;
  234. if (__add_generic_implicit_fb(chip, fmt,
  235. alts->desc.bInterfaceNumber + 1,
  236. alts->desc.bAlternateSetting))
  237. return 1;
  238. return __add_generic_implicit_fb(chip, fmt,
  239. alts->desc.bInterfaceNumber - 1,
  240. alts->desc.bAlternateSetting);
  241. }
  242. static const struct snd_usb_implicit_fb_match *
  243. find_implicit_fb_entry(struct snd_usb_audio *chip,
  244. const struct snd_usb_implicit_fb_match *match,
  245. const struct usb_host_interface *alts)
  246. {
  247. for (; match->id; match++)
  248. if (match->id == chip->usb_id &&
  249. (!match->iface_class ||
  250. (alts->desc.bInterfaceClass == match->iface_class)))
  251. return match;
  252. return NULL;
  253. }
  254. /* Setup an implicit feedback endpoint from a quirk. Returns 0 if no quirk
  255. * applies. Returns 1 if a quirk was found.
  256. */
  257. static int audioformat_implicit_fb_quirk(struct snd_usb_audio *chip,
  258. struct audioformat *fmt,
  259. struct usb_host_interface *alts)
  260. {
  261. const struct snd_usb_implicit_fb_match *p;
  262. unsigned int attr = fmt->ep_attr & USB_ENDPOINT_SYNCTYPE;
  263. p = find_implicit_fb_entry(chip, playback_implicit_fb_quirks, alts);
  264. if (p) {
  265. switch (p->type) {
  266. case IMPLICIT_FB_GENERIC:
  267. return add_generic_implicit_fb(chip, fmt, alts);
  268. case IMPLICIT_FB_NONE:
  269. return 0; /* No quirk */
  270. case IMPLICIT_FB_FIXED:
  271. return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
  272. p->iface, NULL);
  273. }
  274. }
  275. /* Special handling for devices with capture quirks */
  276. p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
  277. if (p) {
  278. switch (p->type) {
  279. case IMPLICIT_FB_FIXED:
  280. return 0; /* no quirk */
  281. case IMPLICIT_FB_BOTH:
  282. chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
  283. return add_generic_implicit_fb(chip, fmt, alts);
  284. }
  285. }
  286. /* Generic UAC2 implicit feedback */
  287. if (attr == USB_ENDPOINT_SYNC_ASYNC &&
  288. alts->desc.bInterfaceClass == USB_CLASS_AUDIO &&
  289. alts->desc.bInterfaceProtocol == UAC_VERSION_2 &&
  290. alts->desc.bNumEndpoints == 1) {
  291. if (add_generic_uac2_implicit_fb(chip, fmt,
  292. alts->desc.bInterfaceNumber + 1,
  293. alts->desc.bAlternateSetting))
  294. return 1;
  295. }
  296. /* Roland/BOSS implicit feedback with vendor spec class */
  297. if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
  298. if (add_roland_implicit_fb(chip, fmt, alts) > 0)
  299. return 1;
  300. }
  301. /* Pioneer devices with vendor spec class */
  302. if (is_pioneer_implicit_fb(chip, alts)) {
  303. chip->quirk_flags |= QUIRK_FLAG_PLAYBACK_FIRST;
  304. return add_implicit_fb_sync_ep(chip, fmt,
  305. get_endpoint(alts, 1)->bEndpointAddress,
  306. 1, alts->desc.bInterfaceNumber,
  307. alts);
  308. }
  309. /* Try the generic implicit fb if available */
  310. if (chip->generic_implicit_fb ||
  311. (chip->quirk_flags & QUIRK_FLAG_GENERIC_IMPLICIT_FB))
  312. return add_generic_implicit_fb(chip, fmt, alts);
  313. /* No quirk */
  314. return 0;
  315. }
  316. /* same for capture, but only handling FIXED entry */
  317. static int audioformat_capture_quirk(struct snd_usb_audio *chip,
  318. struct audioformat *fmt,
  319. struct usb_host_interface *alts)
  320. {
  321. const struct snd_usb_implicit_fb_match *p;
  322. p = find_implicit_fb_entry(chip, capture_implicit_fb_quirks, alts);
  323. if (p && (p->type == IMPLICIT_FB_FIXED || p->type == IMPLICIT_FB_BOTH))
  324. return add_implicit_fb_sync_ep(chip, fmt, p->ep_num, 0,
  325. p->iface, NULL);
  326. /* Roland/BOSS need full-duplex streams */
  327. if (USB_ID_VENDOR(chip->usb_id) == 0x0582) {
  328. if (add_roland_capture_quirk(chip, fmt, alts) > 0)
  329. return 1;
  330. }
  331. if (is_pioneer_implicit_fb(chip, alts))
  332. return 1; /* skip the quirk, also don't handle generic sync EP */
  333. return 0;
  334. }
  335. /*
  336. * Parse altset and set up implicit feedback endpoint on the audioformat
  337. */
  338. int snd_usb_parse_implicit_fb_quirk(struct snd_usb_audio *chip,
  339. struct audioformat *fmt,
  340. struct usb_host_interface *alts)
  341. {
  342. if (chip->quirk_flags & QUIRK_FLAG_SKIP_IMPLICIT_FB)
  343. return 0;
  344. if (fmt->endpoint & USB_DIR_IN)
  345. return audioformat_capture_quirk(chip, fmt, alts);
  346. else
  347. return audioformat_implicit_fb_quirk(chip, fmt, alts);
  348. }
  349. /*
  350. * Return the score of matching two audioformats.
  351. * Veto the audioformat if:
  352. * - It has no channels for some reason.
  353. * - Requested PCM format is not supported.
  354. * - Requested sample rate is not supported.
  355. */
  356. static int match_endpoint_audioformats(struct snd_usb_substream *subs,
  357. const struct audioformat *fp,
  358. int rate, int channels,
  359. snd_pcm_format_t pcm_format)
  360. {
  361. int i, score;
  362. if (fp->channels < 1)
  363. return 0;
  364. if (!(fp->formats & pcm_format_to_bits(pcm_format)))
  365. return 0;
  366. if (fp->rates & SNDRV_PCM_RATE_CONTINUOUS) {
  367. if (rate < fp->rate_min || rate > fp->rate_max)
  368. return 0;
  369. } else {
  370. for (i = 0; i < fp->nr_rates; i++) {
  371. if (fp->rate_table[i] == rate)
  372. break;
  373. }
  374. if (i >= fp->nr_rates)
  375. return 0;
  376. }
  377. score = 1;
  378. if (fp->channels == channels)
  379. score++;
  380. return score;
  381. }
  382. static struct snd_usb_substream *
  383. find_matching_substream(struct snd_usb_audio *chip, int stream, int ep_num,
  384. int fmt_type)
  385. {
  386. struct snd_usb_stream *as;
  387. struct snd_usb_substream *subs;
  388. list_for_each_entry(as, &chip->pcm_list, list) {
  389. subs = &as->substream[stream];
  390. if (as->fmt_type == fmt_type && subs->ep_num == ep_num)
  391. return subs;
  392. }
  393. return NULL;
  394. }
  395. /*
  396. * Return the audioformat that is suitable for the implicit fb
  397. */
  398. const struct audioformat *
  399. snd_usb_find_implicit_fb_sync_format(struct snd_usb_audio *chip,
  400. const struct audioformat *target,
  401. const struct snd_pcm_hw_params *params,
  402. int stream,
  403. bool *fixed_rate)
  404. {
  405. struct snd_usb_substream *subs;
  406. const struct audioformat *fp, *sync_fmt = NULL;
  407. int score, high_score;
  408. /* Use the original audioformat as fallback for the shared altset */
  409. if (target->iface == target->sync_iface &&
  410. target->altsetting == target->sync_altsetting)
  411. sync_fmt = target;
  412. subs = find_matching_substream(chip, stream, target->sync_ep,
  413. target->fmt_type);
  414. if (!subs)
  415. goto end;
  416. high_score = 0;
  417. list_for_each_entry(fp, &subs->fmt_list, list) {
  418. score = match_endpoint_audioformats(subs, fp,
  419. params_rate(params),
  420. params_channels(params),
  421. params_format(params));
  422. if (score > high_score) {
  423. sync_fmt = fp;
  424. high_score = score;
  425. }
  426. }
  427. end:
  428. if (fixed_rate)
  429. *fixed_rate = snd_usb_pcm_has_fixed_rate(subs);
  430. return sync_fmt;
  431. }