clock.c 18 KB

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