clock.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 struct uac_clock_source_descriptor *
  35. snd_usb_find_clock_source(struct usb_host_interface *ctrl_iface,
  36. int clock_id)
  37. {
  38. struct uac_clock_source_descriptor *cs = NULL;
  39. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  40. ctrl_iface->extralen,
  41. cs, UAC2_CLOCK_SOURCE))) {
  42. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
  43. return cs;
  44. }
  45. return NULL;
  46. }
  47. static struct uac3_clock_source_descriptor *
  48. snd_usb_find_clock_source_v3(struct usb_host_interface *ctrl_iface,
  49. int clock_id)
  50. {
  51. struct uac3_clock_source_descriptor *cs = NULL;
  52. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  53. ctrl_iface->extralen,
  54. cs, UAC3_CLOCK_SOURCE))) {
  55. if (cs->bClockID == clock_id)
  56. return cs;
  57. }
  58. return NULL;
  59. }
  60. static struct uac_clock_selector_descriptor *
  61. snd_usb_find_clock_selector(struct usb_host_interface *ctrl_iface,
  62. int clock_id)
  63. {
  64. struct uac_clock_selector_descriptor *cs = NULL;
  65. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  66. ctrl_iface->extralen,
  67. cs, UAC2_CLOCK_SELECTOR))) {
  68. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id) {
  69. if (cs->bLength < 5 + cs->bNrInPins)
  70. return NULL;
  71. return cs;
  72. }
  73. }
  74. return NULL;
  75. }
  76. static struct uac3_clock_selector_descriptor *
  77. snd_usb_find_clock_selector_v3(struct usb_host_interface *ctrl_iface,
  78. int clock_id)
  79. {
  80. struct uac3_clock_selector_descriptor *cs = NULL;
  81. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  82. ctrl_iface->extralen,
  83. cs, UAC3_CLOCK_SELECTOR))) {
  84. if (cs->bClockID == clock_id)
  85. return cs;
  86. }
  87. return NULL;
  88. }
  89. static struct uac_clock_multiplier_descriptor *
  90. snd_usb_find_clock_multiplier(struct usb_host_interface *ctrl_iface,
  91. int clock_id)
  92. {
  93. struct uac_clock_multiplier_descriptor *cs = NULL;
  94. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  95. ctrl_iface->extralen,
  96. cs, UAC2_CLOCK_MULTIPLIER))) {
  97. if (cs->bLength >= sizeof(*cs) && cs->bClockID == clock_id)
  98. return cs;
  99. }
  100. return NULL;
  101. }
  102. static struct uac3_clock_multiplier_descriptor *
  103. snd_usb_find_clock_multiplier_v3(struct usb_host_interface *ctrl_iface,
  104. int clock_id)
  105. {
  106. struct uac3_clock_multiplier_descriptor *cs = NULL;
  107. while ((cs = snd_usb_find_csint_desc(ctrl_iface->extra,
  108. ctrl_iface->extralen,
  109. cs, UAC3_CLOCK_MULTIPLIER))) {
  110. if (cs->bClockID == clock_id)
  111. return cs;
  112. }
  113. return NULL;
  114. }
  115. static int uac_clock_selector_get_val(struct snd_usb_audio *chip, int selector_id)
  116. {
  117. unsigned char buf;
  118. int ret;
  119. ret = snd_usb_ctl_msg(chip->dev, usb_rcvctrlpipe(chip->dev, 0),
  120. UAC2_CS_CUR,
  121. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_IN,
  122. UAC2_CX_CLOCK_SELECTOR << 8,
  123. snd_usb_ctrl_intf(chip) | (selector_id << 8),
  124. &buf, sizeof(buf));
  125. if (ret < 0)
  126. return ret;
  127. return buf;
  128. }
  129. static int uac_clock_selector_set_val(struct snd_usb_audio *chip, int selector_id,
  130. unsigned char pin)
  131. {
  132. int ret;
  133. ret = snd_usb_ctl_msg(chip->dev, usb_sndctrlpipe(chip->dev, 0),
  134. UAC2_CS_CUR,
  135. USB_RECIP_INTERFACE | USB_TYPE_CLASS | USB_DIR_OUT,
  136. UAC2_CX_CLOCK_SELECTOR << 8,
  137. snd_usb_ctrl_intf(chip) | (selector_id << 8),
  138. &pin, sizeof(pin));
  139. if (ret < 0)
  140. return ret;
  141. if (ret != sizeof(pin)) {
  142. usb_audio_err(chip,
  143. "setting selector (id %d) unexpected length %d\n",
  144. selector_id, ret);
  145. return -EINVAL;
  146. }
  147. ret = uac_clock_selector_get_val(chip, selector_id);
  148. if (ret < 0)
  149. return ret;
  150. if (ret != pin) {
  151. usb_audio_err(chip,
  152. "setting selector (id %d) to %x failed (current: %d)\n",
  153. selector_id, pin, ret);
  154. return -EINVAL;
  155. }
  156. return ret;
  157. }
  158. static bool uac_clock_source_is_valid(struct snd_usb_audio *chip,
  159. int protocol,
  160. int source_id)
  161. {
  162. int err;
  163. unsigned char data;
  164. struct usb_device *dev = chip->dev;
  165. u32 bmControls;
  166. if (protocol == UAC_VERSION_3) {
  167. struct uac3_clock_source_descriptor *cs_desc =
  168. snd_usb_find_clock_source_v3(chip->ctrl_intf, source_id);
  169. if (!cs_desc)
  170. return 0;
  171. bmControls = le32_to_cpu(cs_desc->bmControls);
  172. } else { /* UAC_VERSION_1/2 */
  173. struct uac_clock_source_descriptor *cs_desc =
  174. snd_usb_find_clock_source(chip->ctrl_intf, source_id);
  175. if (!cs_desc)
  176. return 0;
  177. bmControls = cs_desc->bmControls;
  178. }
  179. /* If a clock source can't tell us whether it's valid, we assume it is */
  180. if (!uac_v2v3_control_is_readable(bmControls,
  181. UAC2_CS_CONTROL_CLOCK_VALID))
  182. return 1;
  183. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  184. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  185. UAC2_CS_CONTROL_CLOCK_VALID << 8,
  186. snd_usb_ctrl_intf(chip) | (source_id << 8),
  187. &data, sizeof(data));
  188. if (err < 0) {
  189. dev_warn(&dev->dev,
  190. "%s(): cannot get clock validity for id %d\n",
  191. __func__, source_id);
  192. return 0;
  193. }
  194. return !!data;
  195. }
  196. static int __uac_clock_find_source(struct snd_usb_audio *chip, int entity_id,
  197. unsigned long *visited, bool validate)
  198. {
  199. struct uac_clock_source_descriptor *source;
  200. struct uac_clock_selector_descriptor *selector;
  201. struct uac_clock_multiplier_descriptor *multiplier;
  202. entity_id &= 0xff;
  203. if (test_and_set_bit(entity_id, visited)) {
  204. usb_audio_warn(chip,
  205. "%s(): recursive clock topology detected, id %d.\n",
  206. __func__, entity_id);
  207. return -EINVAL;
  208. }
  209. /* first, see if the ID we're looking for is a clock source already */
  210. source = snd_usb_find_clock_source(chip->ctrl_intf, entity_id);
  211. if (source) {
  212. entity_id = source->bClockID;
  213. if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_2,
  214. entity_id)) {
  215. usb_audio_err(chip,
  216. "clock source %d is not valid, cannot use\n",
  217. entity_id);
  218. return -ENXIO;
  219. }
  220. return entity_id;
  221. }
  222. selector = snd_usb_find_clock_selector(chip->ctrl_intf, entity_id);
  223. if (selector) {
  224. int ret, i, cur;
  225. /* the entity ID we are looking for is a selector.
  226. * find out what it currently selects */
  227. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  228. if (ret < 0)
  229. return ret;
  230. /* Selector values are one-based */
  231. if (ret > selector->bNrInPins || ret < 1) {
  232. usb_audio_err(chip,
  233. "%s(): selector reported illegal value, id %d, ret %d\n",
  234. __func__, selector->bClockID, ret);
  235. return -EINVAL;
  236. }
  237. cur = ret;
  238. ret = __uac_clock_find_source(chip, selector->baCSourceID[ret - 1],
  239. visited, validate);
  240. if (!validate || ret > 0 || !chip->autoclock)
  241. return ret;
  242. /* The current clock source is invalid, try others. */
  243. for (i = 1; i <= selector->bNrInPins; i++) {
  244. int err;
  245. if (i == cur)
  246. continue;
  247. ret = __uac_clock_find_source(chip, selector->baCSourceID[i - 1],
  248. visited, true);
  249. if (ret < 0)
  250. continue;
  251. err = uac_clock_selector_set_val(chip, entity_id, i);
  252. if (err < 0)
  253. continue;
  254. usb_audio_info(chip,
  255. "found and selected valid clock source %d\n",
  256. ret);
  257. return ret;
  258. }
  259. return -ENXIO;
  260. }
  261. /* FIXME: multipliers only act as pass-thru element for now */
  262. multiplier = snd_usb_find_clock_multiplier(chip->ctrl_intf, entity_id);
  263. if (multiplier)
  264. return __uac_clock_find_source(chip, multiplier->bCSourceID,
  265. visited, validate);
  266. return -EINVAL;
  267. }
  268. static int __uac3_clock_find_source(struct snd_usb_audio *chip, int entity_id,
  269. unsigned long *visited, bool validate)
  270. {
  271. struct uac3_clock_source_descriptor *source;
  272. struct uac3_clock_selector_descriptor *selector;
  273. struct uac3_clock_multiplier_descriptor *multiplier;
  274. entity_id &= 0xff;
  275. if (test_and_set_bit(entity_id, visited)) {
  276. usb_audio_warn(chip,
  277. "%s(): recursive clock topology detected, id %d.\n",
  278. __func__, entity_id);
  279. return -EINVAL;
  280. }
  281. /* first, see if the ID we're looking for is a clock source already */
  282. source = snd_usb_find_clock_source_v3(chip->ctrl_intf, entity_id);
  283. if (source) {
  284. entity_id = source->bClockID;
  285. if (validate && !uac_clock_source_is_valid(chip, UAC_VERSION_3,
  286. entity_id)) {
  287. usb_audio_err(chip,
  288. "clock source %d is not valid, cannot use\n",
  289. entity_id);
  290. return -ENXIO;
  291. }
  292. return entity_id;
  293. }
  294. selector = snd_usb_find_clock_selector_v3(chip->ctrl_intf, entity_id);
  295. if (selector) {
  296. int ret, i, cur;
  297. /* the entity ID we are looking for is a selector.
  298. * find out what it currently selects */
  299. ret = uac_clock_selector_get_val(chip, selector->bClockID);
  300. if (ret < 0)
  301. return ret;
  302. /* Selector values are one-based */
  303. if (ret > selector->bNrInPins || ret < 1) {
  304. usb_audio_err(chip,
  305. "%s(): selector reported illegal value, id %d, ret %d\n",
  306. __func__, selector->bClockID, ret);
  307. return -EINVAL;
  308. }
  309. cur = ret;
  310. ret = __uac3_clock_find_source(chip, selector->baCSourceID[ret - 1],
  311. visited, validate);
  312. if (!validate || ret > 0 || !chip->autoclock)
  313. return ret;
  314. /* The current clock source is invalid, try others. */
  315. for (i = 1; i <= selector->bNrInPins; i++) {
  316. int err;
  317. if (i == cur)
  318. continue;
  319. ret = __uac3_clock_find_source(chip, selector->baCSourceID[i - 1],
  320. visited, true);
  321. if (ret < 0)
  322. continue;
  323. err = uac_clock_selector_set_val(chip, entity_id, i);
  324. if (err < 0)
  325. continue;
  326. usb_audio_info(chip,
  327. "found and selected valid clock source %d\n",
  328. ret);
  329. return ret;
  330. }
  331. return -ENXIO;
  332. }
  333. /* FIXME: multipliers only act as pass-thru element for now */
  334. multiplier = snd_usb_find_clock_multiplier_v3(chip->ctrl_intf,
  335. entity_id);
  336. if (multiplier)
  337. return __uac3_clock_find_source(chip, multiplier->bCSourceID,
  338. visited, validate);
  339. return -EINVAL;
  340. }
  341. /*
  342. * For all kinds of sample rate settings and other device queries,
  343. * the clock source (end-leaf) must be used. However, clock selectors,
  344. * clock multipliers and sample rate converters may be specified as
  345. * clock source input to terminal. This functions walks the clock path
  346. * to its end and tries to find the source.
  347. *
  348. * The 'visited' bitfield is used internally to detect recursive loops.
  349. *
  350. * Returns the clock source UnitID (>=0) on success, or an error.
  351. */
  352. int snd_usb_clock_find_source(struct snd_usb_audio *chip, int protocol,
  353. int entity_id, bool validate)
  354. {
  355. DECLARE_BITMAP(visited, 256);
  356. memset(visited, 0, sizeof(visited));
  357. switch (protocol) {
  358. case UAC_VERSION_2:
  359. return __uac_clock_find_source(chip, entity_id, visited,
  360. validate);
  361. case UAC_VERSION_3:
  362. return __uac3_clock_find_source(chip, entity_id, visited,
  363. validate);
  364. default:
  365. return -EINVAL;
  366. }
  367. }
  368. static int set_sample_rate_v1(struct snd_usb_audio *chip, int iface,
  369. struct usb_host_interface *alts,
  370. struct audioformat *fmt, int rate)
  371. {
  372. struct usb_device *dev = chip->dev;
  373. unsigned int ep;
  374. unsigned char data[3];
  375. int err, crate;
  376. if (get_iface_desc(alts)->bNumEndpoints < 1)
  377. return -EINVAL;
  378. ep = get_endpoint(alts, 0)->bEndpointAddress;
  379. /* if endpoint doesn't have sampling rate control, bail out */
  380. if (!(fmt->attributes & UAC_EP_CS_ATTR_SAMPLE_RATE))
  381. return 0;
  382. data[0] = rate;
  383. data[1] = rate >> 8;
  384. data[2] = rate >> 16;
  385. if ((err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC_SET_CUR,
  386. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  387. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  388. data, sizeof(data))) < 0) {
  389. dev_err(&dev->dev, "%d:%d: cannot set freq %d to ep %#x\n",
  390. iface, fmt->altsetting, rate, ep);
  391. return err;
  392. }
  393. /* Don't check the sample rate for devices which we know don't
  394. * support reading */
  395. if (snd_usb_get_sample_rate_quirk(chip))
  396. return 0;
  397. /* the firmware is likely buggy, don't repeat to fail too many times */
  398. if (chip->sample_rate_read_error > 2)
  399. return 0;
  400. if ((err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC_GET_CUR,
  401. USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_IN,
  402. UAC_EP_CS_ATTR_SAMPLE_RATE << 8, ep,
  403. data, sizeof(data))) < 0) {
  404. dev_err(&dev->dev, "%d:%d: cannot get freq at ep %#x\n",
  405. iface, fmt->altsetting, ep);
  406. chip->sample_rate_read_error++;
  407. return 0; /* some devices don't support reading */
  408. }
  409. crate = data[0] | (data[1] << 8) | (data[2] << 16);
  410. if (crate != rate) {
  411. dev_warn(&dev->dev, "current rate %d is different from the runtime rate %d\n", crate, rate);
  412. // runtime->rate = crate;
  413. }
  414. return 0;
  415. }
  416. static int get_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
  417. int altsetting, int clock)
  418. {
  419. struct usb_device *dev = chip->dev;
  420. __le32 data;
  421. int err;
  422. err = snd_usb_ctl_msg(dev, usb_rcvctrlpipe(dev, 0), UAC2_CS_CUR,
  423. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  424. UAC2_CS_CONTROL_SAM_FREQ << 8,
  425. snd_usb_ctrl_intf(chip) | (clock << 8),
  426. &data, sizeof(data));
  427. if (err < 0) {
  428. dev_warn(&dev->dev, "%d:%d: cannot get freq (v2/v3): err %d\n",
  429. iface, altsetting, err);
  430. return 0;
  431. }
  432. return le32_to_cpu(data);
  433. }
  434. static int set_sample_rate_v2v3(struct snd_usb_audio *chip, int iface,
  435. struct usb_host_interface *alts,
  436. struct audioformat *fmt, int rate)
  437. {
  438. struct usb_device *dev = chip->dev;
  439. __le32 data;
  440. int err, cur_rate, prev_rate;
  441. int clock;
  442. bool writeable;
  443. u32 bmControls;
  444. clock = snd_usb_clock_find_source(chip, fmt->protocol,
  445. fmt->clock, true);
  446. if (clock < 0)
  447. return clock;
  448. prev_rate = get_sample_rate_v2v3(chip, iface, fmt->altsetting, clock);
  449. if (prev_rate == rate)
  450. return 0;
  451. if (fmt->protocol == UAC_VERSION_3) {
  452. struct uac3_clock_source_descriptor *cs_desc;
  453. cs_desc = snd_usb_find_clock_source_v3(chip->ctrl_intf, clock);
  454. bmControls = le32_to_cpu(cs_desc->bmControls);
  455. } else {
  456. struct uac_clock_source_descriptor *cs_desc;
  457. cs_desc = snd_usb_find_clock_source(chip->ctrl_intf, clock);
  458. bmControls = cs_desc->bmControls;
  459. }
  460. writeable = uac_v2v3_control_is_writeable(bmControls,
  461. UAC2_CS_CONTROL_SAM_FREQ);
  462. if (writeable) {
  463. data = cpu_to_le32(rate);
  464. err = snd_usb_ctl_msg(dev, usb_sndctrlpipe(dev, 0), UAC2_CS_CUR,
  465. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  466. UAC2_CS_CONTROL_SAM_FREQ << 8,
  467. snd_usb_ctrl_intf(chip) | (clock << 8),
  468. &data, sizeof(data));
  469. if (err < 0) {
  470. usb_audio_err(chip,
  471. "%d:%d: cannot set freq %d (v2/v3): err %d\n",
  472. iface, fmt->altsetting, rate, err);
  473. return err;
  474. }
  475. cur_rate = get_sample_rate_v2v3(chip, iface,
  476. fmt->altsetting, clock);
  477. } else {
  478. cur_rate = prev_rate;
  479. }
  480. if (cur_rate != rate) {
  481. if (!writeable) {
  482. usb_audio_warn(chip,
  483. "%d:%d: freq mismatch (RO clock): req %d, clock runs @%d\n",
  484. iface, fmt->altsetting, rate, cur_rate);
  485. return -ENXIO;
  486. }
  487. usb_audio_dbg(chip,
  488. "current rate %d is different from the runtime rate %d\n",
  489. cur_rate, rate);
  490. }
  491. /* Some devices doesn't respond to sample rate changes while the
  492. * interface is active. */
  493. if (rate != prev_rate) {
  494. usb_set_interface(dev, iface, 0);
  495. snd_usb_set_interface_quirk(dev);
  496. usb_set_interface(dev, iface, fmt->altsetting);
  497. snd_usb_set_interface_quirk(dev);
  498. }
  499. return 0;
  500. }
  501. int snd_usb_init_sample_rate(struct snd_usb_audio *chip, int iface,
  502. struct usb_host_interface *alts,
  503. struct audioformat *fmt, int rate)
  504. {
  505. switch (fmt->protocol) {
  506. case UAC_VERSION_1:
  507. default:
  508. return set_sample_rate_v1(chip, iface, alts, fmt, rate);
  509. case UAC_VERSION_2:
  510. case UAC_VERSION_3:
  511. return set_sample_rate_v2v3(chip, iface, alts, fmt, rate);
  512. }
  513. }