implicit.c 15 KB

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