clock.c 16 KB

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