clock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Clock domain and sample rate management functions
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/init.h>
  7. #include <linux/string.h>
  8. #include <linux/usb.h>
  9. #include <linux/usb/audio.h>
  10. #include <linux/usb/audio-v2.h>
  11. #include <linux/usb/audio-v3.h>
  12. #include <sound/core.h>
  13. #include <sound/info.h>
  14. #include <sound/pcm.h>
  15. #include "usbaudio.h"
  16. #include "card.h"
  17. #include "helper.h"
  18. #include "clock.h"
  19. #include "quirks.h"
  20. union uac23_clock_source_desc {
  21. struct uac_clock_source_descriptor v2;
  22. struct uac3_clock_source_descriptor v3;
  23. };
  24. union uac23_clock_selector_desc {
  25. struct uac_clock_selector_descriptor v2;
  26. struct uac3_clock_selector_descriptor v3;
  27. };
  28. union uac23_clock_multiplier_desc {
  29. struct uac_clock_multiplier_descriptor v2;
  30. struct uac_clock_multiplier_descriptor v3;
  31. };
  32. /* check whether the descriptor bLength has the minimal length */
  33. #define DESC_LENGTH_CHECK(p, proto) \
  34. ((proto) == UAC_VERSION_3 ? \
  35. ((p)->v3.bLength >= sizeof((p)->v3)) : \
  36. ((p)->v2.bLength >= sizeof((p)->v2)))
  37. #define GET_VAL(p, proto, field) \
  38. ((proto) == UAC_VERSION_3 ? (p)->v3.field : (p)->v2.field)
  39. static void *find_uac_clock_desc(struct usb_host_interface *iface, int id,
  40. bool (*validator)(void *, int, int),
  41. u8 type, int proto)
  42. {
  43. void *cs = NULL;
  44. while ((cs = snd_usb_find_csint_desc(iface->extra, iface->extralen,
  45. cs, type))) {
  46. if (validator(cs, id, proto))
  47. return cs;
  48. }
  49. return NULL;
  50. }
  51. static bool validate_clock_source(void *p, int id, int proto)
  52. {
  53. union uac23_clock_source_desc *cs = p;
  54. if (!DESC_LENGTH_CHECK(cs, proto))
  55. return false;
  56. return GET_VAL(cs, proto, bClockID) == id;
  57. }
  58. static bool validate_clock_selector(void *p, int id, int proto)
  59. {
  60. union uac23_clock_selector_desc *cs = p;
  61. if (!DESC_LENGTH_CHECK(cs, proto))
  62. return false;
  63. if (GET_VAL(cs, proto, bClockID) != id)
  64. return false;
  65. /* additional length check for baCSourceID array (in bNrInPins size)
  66. * and two more fields (which sizes depend on the protocol)
  67. */
  68. if (proto == UAC_VERSION_3)
  69. return cs->v3.bLength >= sizeof(cs->v3) + cs->v3.bNrInPins +
  70. 4 /* bmControls */ + 2 /* wCSelectorDescrStr */;
  71. else
  72. return cs->v2.bLength >= sizeof(cs->v2) + cs->v2.bNrInPins +
  73. 1 /* bmControls */ + 1 /* iClockSelector */;
  74. }
  75. static bool validate_clock_multiplier(void *p, int id, int proto)
  76. {
  77. union uac23_clock_multiplier_desc *cs = p;
  78. if (!DESC_LENGTH_CHECK(cs, proto))
  79. return false;
  80. return GET_VAL(cs, proto, bClockID) == id;
  81. }
  82. #define DEFINE_FIND_HELPER(name, obj, validator, type2, type3) \
  83. static obj *name(struct snd_usb_audio *chip, int id, \
  84. const struct audioformat *fmt) \
  85. { \
  86. struct usb_host_interface *ctrl_intf = \
  87. snd_usb_find_ctrl_interface(chip, fmt->iface); \
  88. return find_uac_clock_desc(ctrl_intf, id, validator, \
  89. fmt->protocol == UAC_VERSION_3 ? (type3) : (type2), \
  90. fmt->protocol); \
  91. }
  92. DEFINE_FIND_HELPER(snd_usb_find_clock_source,
  93. union uac23_clock_source_desc, validate_clock_source,
  94. UAC2_CLOCK_SOURCE, UAC3_CLOCK_SOURCE);
  95. DEFINE_FIND_HELPER(snd_usb_find_clock_selector,
  96. union uac23_clock_selector_desc, validate_clock_selector,
  97. UAC2_CLOCK_SELECTOR, UAC3_CLOCK_SELECTOR);
  98. DEFINE_FIND_HELPER(snd_usb_find_clock_multiplier,
  99. union uac23_clock_multiplier_desc, validate_clock_multiplier,
  100. UAC2_CLOCK_MULTIPLIER, UAC3_CLOCK_MULTIPLIER);
  101. static int uac_clock_selector_get_val(struct snd_usb_audio *chip,
  102. int selector_id, int iface_no)
  103. {
  104. struct usb_host_interface *ctrl_intf;
  105. unsigned char buf;
  106. int ret;
  107. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  108. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
  109. UAC2_CS_CUR,
  110. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  111. UAC2_CX_CLOCK_SELECTOR << 8,
  112. snd_usb_ctrl_intf(ctrl_intf) | (selector_id << 8),
  113. &buf, sizeof(buf));
  114. if (ret < 0)
  115. return ret;
  116. return buf;
  117. }
  118. static int uac_clock_selector_set_val(struct snd_usb_audio *chip,
  119. int selector_id, unsigned char pin, int iface_no)
  120. {
  121. struct usb_host_interface *ctrl_intf;
  122. int ret;
  123. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface_no);
  124. ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
  125. UAC2_CS_CUR,
  126. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  127. UAC2_CX_CLOCK_SELECTOR << 8,
  128. snd_usb_ctrl_intf(ctrl_intf) | (selector_id << 8),
  129. &pin, sizeof(pin));
  130. if (ret < 0)
  131. return ret;
  132. if (ret != sizeof(pin)) {
  133. usb_audio_err(chip,
  134. "setting selector (id %d) unexpected length %d\n",
  135. selector_id, ret);
  136. return -EINVAL;
  137. }
  138. ret = uac_clock_selector_get_val(chip, selector_id, iface_no);
  139. if (ret < 0)
  140. return ret;
  141. if (ret != pin) {
  142. usb_audio_err(chip,
  143. "setting selector (id %d) to %x failed (current: %d)\n",
  144. selector_id, pin, ret);
  145. return -EINVAL;
  146. }
  147. return ret;
  148. }
  149. static bool uac_clock_source_is_valid_quirk(struct snd_usb_audio *chip,
  150. const struct audioformat *fmt,
  151. int source_id)
  152. {
  153. bool ret = false;
  154. int count;
  155. unsigned char data;
  156. struct usb_device *dev = chip->dev;
  157. union uac23_clock_source_desc *cs_desc;
  158. struct usb_host_interface *ctrl_intf;
  159. ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
  160. cs_desc = snd_usb_find_clock_source(chip, source_id, fmt);
  161. if (!cs_desc)
  162. return false;
  163. if (fmt->protocol == UAC_VERSION_2) {
  164. /*
  165. * Assume the clock is valid if clock source supports only one
  166. * single sample rate, the terminal is connected directly to it
  167. * (there is no clock selector) and clock type is internal.
  168. * This is to deal with some Denon DJ controllers that always
  169. * reports that clock is invalid.
  170. */
  171. if (fmt->nr_rates == 1 &&
  172. (fmt->clock & 0xff) == cs_desc->v2.bClockID &&
  173. (cs_desc->v2.bmAttributes & 0x3) !=
  174. UAC_CLOCK_SOURCE_TYPE_EXT)
  175. return true;
  176. }
  177. /*
  178. * MOTU MicroBook IIc
  179. * Sample rate changes takes more than 2 seconds for this device. Clock
  180. * validity request returns false during that period.
  181. */
  182. if (chip->usb_id == USB_ID(0x07fd, 0x0004)) {
  183. count = 0;
  184. while ((!ret) && (count < 50)) {
  185. int err;
  186. msleep(100);
  187. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  188. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  189. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  190. snd_usb_ctrl_intf(ctrl_intf) | (source_id << 8),
  191. &data, sizeof(data));
  192. if (err < 0) {
  193. dev_warn(&dev->dev,
  194. "%s(): cannot get clock validity for id %d\n",
  195. __func__, source_id);
  196. return false;
  197. }
  198. ret = !!data;
  199. count++;
  200. }
  201. }
  202. return ret;
  203. }
  204. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
  205. const struct audioformat *fmt,
  206. int source_id)
  207. {
  208. int err;
  209. unsigned char data;
  210. struct usb_device *dev = chip->dev;
  211. u32 bmControls;
  212. union uac23_clock_source_desc *cs_desc;
  213. struct usb_host_interface *ctrl_intf;
  214. ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
  215. cs_desc = snd_usb_find_clock_source(chip, source_id, fmt);
  216. if (!cs_desc)
  217. return false;
  218. if (fmt->protocol == UAC_VERSION_3)
  219. bmControls = le32_to_cpu(cs_desc->v3.bmControls);
  220. else
  221. bmControls = cs_desc->v2.bmControls;
  222. /* If a clock source can't tell us whether it's valid, we assume it is */
  223. if (!uac_v2v3_control_is_readable(bmControls,
  224. UAC2_CS_CONTROL_CLOCK_VALID))
  225. return true;
  226. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  227. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  228. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  229. snd_usb_ctrl_intf(ctrl_intf) | (source_id << 8),
  230. &data, sizeof(data));
  231. if (err < 0) {
  232. dev_warn(&dev->dev,
  233. "%s(): cannot get clock validity for id %d\n",
  234. __func__, source_id);
  235. return false;
  236. }
  237. if (data)
  238. return true;
  239. else
  240. return uac_clock_source_is_valid_quirk(chip, fmt, source_id);
  241. }
  242. static int __uac_clock_find_source(struct snd_usb_audio *chip,
  243. const struct audioformat *fmt, int entity_id,
  244. unsigned long *visited, bool validate)
  245. {
  246. union uac23_clock_source_desc *source;
  247. union uac23_clock_selector_desc *selector;
  248. union uac23_clock_multiplier_desc *multiplier;
  249. int ret, i, cur, err, pins, clock_id;
  250. const u8 *sources;
  251. int proto = fmt->protocol;
  252. bool readable, writeable;
  253. u32 bmControls;
  254. entity_id &= 0xff;
  255. if (test_and_set_bit(entity_id, visited)) {
  256. usb_audio_warn(chip,
  257. "%s(): recursive clock topology detected, id %d.\n",
  258. __func__, entity_id);
  259. return -EINVAL;
  260. }
  261. /* first, see if the ID we're looking at is a clock source already */
  262. source = snd_usb_find_clock_source(chip, entity_id, fmt);
  263. if (source) {
  264. entity_id = GET_VAL(source, proto, bClockID);
  265. if (validate && !uac_clock_source_is_valid(chip, fmt,
  266. entity_id)) {
  267. usb_audio_err(chip,
  268. "clock source %d is not valid, cannot use\n",
  269. entity_id);
  270. return -ENXIO;
  271. }
  272. return entity_id;
  273. }
  274. selector = snd_usb_find_clock_selector(chip, entity_id, fmt);
  275. if (selector) {
  276. pins = GET_VAL(selector, proto, bNrInPins);
  277. clock_id = GET_VAL(selector, proto, bClockID);
  278. sources = GET_VAL(selector, proto, baCSourceID);
  279. cur = 0;
  280. if (proto == UAC_VERSION_3)
  281. bmControls = le32_to_cpu(*(__le32 *)(&selector->v3.baCSourceID[0] + pins));
  282. else
  283. bmControls = *(__u8 *)(&selector->v2.baCSourceID[0] + pins);
  284. readable = uac_v2v3_control_is_readable(bmControls,
  285. UAC2_CX_CLOCK_SELECTOR);
  286. writeable = uac_v2v3_control_is_writeable(bmControls,
  287. UAC2_CX_CLOCK_SELECTOR);
  288. if (pins == 1) {
  289. ret = 1;
  290. goto find_source;
  291. }
  292. /* for now just warn about buggy device */
  293. if (!readable)
  294. usb_audio_warn(chip,
  295. "%s(): clock selector control is not readable, id %d\n",
  296. __func__, clock_id);
  297. /* the entity ID we are looking at is a selector.
  298. * find out what it currently selects */
  299. ret = uac_clock_selector_get_val(chip, clock_id, fmt->iface);
  300. if (ret < 0) {
  301. if (!chip->autoclock)
  302. return ret;
  303. goto find_others;
  304. }
  305. /* Selector values are one-based */
  306. if (ret > pins || ret < 1) {
  307. usb_audio_err(chip,
  308. "%s(): selector reported illegal value, id %d, ret %d\n",
  309. __func__, clock_id, ret);
  310. if (!chip->autoclock)
  311. return -EINVAL;
  312. goto find_others;
  313. }
  314. find_source:
  315. cur = ret;
  316. ret = __uac_clock_find_source(chip, fmt,
  317. sources[ret - 1],
  318. visited, validate);
  319. if (ret > 0) {
  320. /* Skip setting clock selector again for some devices */
  321. if (chip->quirk_flags & QUIRK_FLAG_SKIP_CLOCK_SELECTOR ||
  322. !writeable)
  323. return ret;
  324. err = uac_clock_selector_set_val(chip, entity_id, cur, fmt->iface);
  325. if (err < 0) {
  326. if (pins == 1) {
  327. usb_audio_dbg(chip,
  328. "%s(): selector returned an error, "
  329. "assuming a firmware bug, id %d, ret %d\n",
  330. __func__, clock_id, err);
  331. return ret;
  332. }
  333. return err;
  334. }
  335. }
  336. if (!validate || ret > 0 || !chip->autoclock)
  337. return ret;
  338. find_others:
  339. if (!writeable)
  340. return -ENXIO;
  341. /* The current clock source is invalid, try others. */
  342. for (i = 1; i <= pins; i++) {
  343. if (i == cur)
  344. continue;
  345. ret = __uac_clock_find_source(chip, fmt,
  346. sources[i - 1],
  347. visited, true);
  348. if (ret < 0)
  349. continue;
  350. err = uac_clock_selector_set_val(chip, entity_id, i, fmt->iface);
  351. if (err < 0)
  352. continue;
  353. usb_audio_info(chip,
  354. "found and selected valid clock source %d\n",
  355. ret);
  356. return ret;
  357. }
  358. return -ENXIO;
  359. }
  360. /* FIXME: multipliers only act as pass-thru element for now */
  361. multiplier = snd_usb_find_clock_multiplier(chip, entity_id, fmt);
  362. if (multiplier)
  363. return __uac_clock_find_source(chip, fmt,
  364. GET_VAL(multiplier, proto, bCSourceID),
  365. visited, validate);
  366. return -EINVAL;
  367. }
  368. /*
  369. * For all kinds of sample rate settings and other device queries,
  370. * the clock source (end-leaf) must be used. However, clock selectors,
  371. * clock multipliers and sample rate converters may be specified as
  372. * clock source input to terminal. This functions walks the clock path
  373. * to its end and tries to find the source.
  374. *
  375. * The 'visited' bitfield is used internally to detect recursive loops.
  376. *
  377. * Returns the clock source UnitID (>=0) on success, or an error.
  378. */
  379. int snd_usb_clock_find_source(struct snd_usb_audio *chip,
  380. const struct audioformat *fmt, bool validate)
  381. {
  382. DECLARE_BITMAP(visited, 256);
  383. memset(visited, 0, sizeof(visited));
  384. switch (fmt->protocol) {
  385. case UAC_VERSION_2:
  386. case UAC_VERSION_3:
  387. return __uac_clock_find_source(chip, fmt, fmt->clock, visited,
  388. validate);
  389. default:
  390. return -EINVAL;
  391. }
  392. }
  393. static int set_sample_rate_v1(struct snd_usb_audio *chip,
  394. const struct audioformat *fmt, int rate)
  395. {
  396. struct usb_device *dev = chip->dev;
  397. unsigned char data[3];
  398. int err, crate;
  399. /* if endpoint doesn't have sampling rate control, bail out */
  400. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
  401. return 0;
  402. data[0] = rate;
  403. data[1] = rate >> 8;
  404. data[2] = rate >> 16;
  405. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  406. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  407. UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
  408. fmt->endpoint, data, sizeof(data));
  409. if (err < 0) {
  410. dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
  411. fmt->iface, fmt->altsetting, rate, fmt->endpoint);
  412. return err;
  413. }
  414. /* Don't check the sample rate for devices which we know don't
  415. * support reading */
  416. if (chip->quirk_flags & QUIRK_FLAG_GET_SAMPLE_RATE)
  417. return 0;
  418. /* the firmware is likely buggy, don't repeat to fail too many times */
  419. if (chip->sample_rate_read_error > 2)
  420. return 0;
  421. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  422. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  423. UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
  424. fmt->endpoint, data, sizeof(data));
  425. if (err < 0) {
  426. dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
  427. fmt->iface, fmt->altsetting, fmt->endpoint);
  428. chip->sample_rate_read_error++;
  429. return 0; /* some devices don't support reading */
  430. }
  431. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  432. if (!crate) {
  433. dev_info(&dev->dev, "failed to read current rate; disabling the check\n");
  434. chip->sample_rate_read_error = 3; /* three strikes, see above */
  435. return 0;
  436. }
  437. if (crate != rate) {
  438. dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
  439. // runtime->rate = crate;
  440. }
  441. return 0;
  442. }
  443. static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
  444. int altsetting, int clock)
  445. {
  446. struct usb_device *dev = chip->dev;
  447. __le32 data;
  448. int err;
  449. struct usb_host_interface *ctrl_intf;
  450. ctrl_intf = snd_usb_find_ctrl_interface(chip, iface);
  451. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  452. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  453. UAC2_CS_CONTROL_SAM_FREQ << 8,
  454. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  455. &data, sizeof(data));
  456. if (err < 0) {
  457. dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
  458. iface, altsetting, err);
  459. return 0;
  460. }
  461. return le32_to_cpu(data);
  462. }
  463. /*
  464. * Try to set the given sample rate:
  465. *
  466. * Return 0 if the clock source is read-only, the actual rate on success,
  467. * or a negative error code.
  468. *
  469. * This function gets called from format.c to validate each sample rate, too.
  470. * Hence no message is shown upon error
  471. */
  472. int snd_usb_set_sample_rate_v2v3(struct snd_usb_audio *chip,
  473. const struct audioformat *fmt,
  474. int clock, int rate)
  475. {
  476. bool writeable;
  477. u32 bmControls;
  478. __le32 data;
  479. int err;
  480. union uac23_clock_source_desc *cs_desc;
  481. struct usb_host_interface *ctrl_intf;
  482. ctrl_intf = snd_usb_find_ctrl_interface(chip, fmt->iface);
  483. cs_desc = snd_usb_find_clock_source(chip, clock, fmt);
  484. if (!cs_desc)
  485. return 0;
  486. if (fmt->protocol == UAC_VERSION_3)
  487. bmControls = le32_to_cpu(cs_desc->v3.bmControls);
  488. else
  489. bmControls = cs_desc->v2.bmControls;
  490. writeable = uac_v2v3_control_is_writeable(bmControls,
  491. UAC2_CS_CONTROL_SAM_FREQ);
  492. if (!writeable)
  493. return 0;
  494. data = cpu_to_le32(rate);
  495. err = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0), UAC2_CS_CUR,
  496. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  497. UAC2_CS_CONTROL_SAM_FREQ << 8,
  498. snd_usb_ctrl_intf(ctrl_intf) | (clock << 8),
  499. &data, sizeof(data));
  500. if (err < 0)
  501. return err;
  502. return get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
  503. }
  504. static int set_sample_rate_v2v3(struct snd_usb_audio *chip,
  505. const struct audioformat *fmt, int rate)
  506. {
  507. int cur_rate, prev_rate;
  508. int clock;
  509. /* First, try to find a valid clock. This may trigger
  510. * automatic clock selection if the current clock is not
  511. * valid.
  512. */
  513. clock = snd_usb_clock_find_source(chip, fmt, true);
  514. if (clock < 0) {
  515. /* We did not find a valid clock, but that might be
  516. * because the current sample rate does not match an
  517. * external clock source. Try again without validation
  518. * and we will do another validation after setting the
  519. * rate.
  520. */
  521. clock = snd_usb_clock_find_source(chip, fmt, false);
  522. /* Hardcoded sample rates */
  523. if (chip->quirk_flags & QUIRK_FLAG_IGNORE_CLOCK_SOURCE)
  524. return 0;
  525. if (clock < 0)
  526. return clock;
  527. }
  528. prev_rate = get_sample_rate_v2v3(chip, fmt->iface, fmt->altsetting, clock);
  529. if (prev_rate == rate)
  530. goto validation;
  531. cur_rate = snd_usb_set_sample_rate_v2v3(chip, fmt, clock, rate);
  532. if (cur_rate < 0) {
  533. usb_audio_err(chip,
  534. "%d:%d: cannot set freq %d (v2/v3): err %d\n",
  535. fmt->iface, fmt->altsetting, rate, cur_rate);
  536. return cur_rate;
  537. }
  538. if (!cur_rate)
  539. cur_rate = prev_rate;
  540. if (cur_rate != rate) {
  541. usb_audio_dbg(chip,
  542. "%d:%d: freq mismatch: req %d, clock runs @%d\n",
  543. fmt->iface, fmt->altsetting, rate, cur_rate);
  544. /* continue processing */
  545. }
  546. /* FIXME - TEAC devices require the immediate interface setup */
  547. if (USB_ID_VENDOR(chip->usb_id) == 0x0644) {
  548. bool cur_base_48k = (rate % 48000 == 0);
  549. bool prev_base_48k = (prev_rate % 48000 == 0);
  550. if (cur_base_48k != prev_base_48k) {
  551. usb_set_interface(chip->dev, fmt->iface, fmt->altsetting);
  552. if (chip->quirk_flags & QUIRK_FLAG_IFACE_DELAY)
  553. msleep(50);
  554. }
  555. }
  556. validation:
  557. /* validate clock after rate change */
  558. if (!uac_clock_source_is_valid(chip, fmt, clock))
  559. return -ENXIO;
  560. return 0;
  561. }
  562. int snd_usb_init_sample_rate(struct snd_usb_audio *chip,
  563. const struct audioformat *fmt, int rate)
  564. {
  565. usb_audio_dbg(chip, "%d:%d Set sample rate %d, clock %d\n",
  566. fmt->iface, fmt->altsetting, rate, fmt->clock);
  567. switch (fmt->protocol) {
  568. case UAC_VERSION_1:
  569. default:
  570. return set_sample_rate_v1(chip, fmt, rate);
  571. case UAC_VERSION_3:
  572. if (chip->badd_profile >= UAC3_FUNCTION_SUBCLASS_GENERIC_IO) {
  573. if (rate != UAC3_BADD_SAMPLING_RATE)
  574. return -ENXIO;
  575. else
  576. return 0;
  577. }
  578. fallthrough;
  579. case UAC_VERSION_2:
  580. return set_sample_rate_v2v3(chip, fmt, rate);
  581. }
  582. }